////////////////////////////////////////////////////////////////
//
// CFileTextureItem::CreateUnderlyingData
//
// Blank sized
//
////////////////////////////////////////////////////////////////
void CFileTextureItem::CreateUnderlyingData(bool bMipMaps, uint uiSizeX, uint uiSizeY, ERenderFormat format, ETextureType textureType, uint uiVolumeDepth)
{
    assert(!m_pD3DTexture);

    D3DFORMAT D3DFormat = (D3DFORMAT)format;
    int       iMipMaps = bMipMaps ? D3DX_DEFAULT : 1;

    m_uiSizeX = uiSizeX;
    m_uiSizeY = uiSizeY;
    m_uiSurfaceSizeX = uiSizeX;
    m_uiSurfaceSizeY = uiSizeY;

    if (textureType == D3DRTYPE_VOLUMETEXTURE)
    {
        if (FAILED(D3DXCreateVolumeTexture(m_pDevice,                  //__in   LPDIRECT3DDEVICE9 pDevice,
                                           uiSizeX,                    //__in   UINT Width,
                                           uiSizeY,                    //__in   UINT Height,
                                           uiVolumeDepth,              //__in   UINT Depth,
                                           iMipMaps,                   //__in   UINT MipLevels,
                                           0,                          //__in   DWORD Usage,
                                           D3DFormat,                  //__in   D3DFORMAT Format,
                                           D3DPOOL_MANAGED,            //__in   D3DPOOL Pool,
                                           (IDirect3DVolumeTexture9**)&m_pD3DTexture)))
            return;
    }
    else if (textureType == D3DRTYPE_CUBETEXTURE)
    {
        if (FAILED(D3DXCreateCubeTexture(m_pDevice,                  //__in   LPDIRECT3DDEVICE9 pDevice,
                                         uiSizeX,                    //__in   UINT Width,
                                         iMipMaps,                   //__in   UINT MipLevels,
                                         0,                          //__in   DWORD Usage,
                                         D3DFormat,                  //__in   D3DFORMAT Format,
                                         D3DPOOL_MANAGED,            //__in   D3DPOOL Pool,
                                         (IDirect3DCubeTexture9**)&m_pD3DTexture)))
            return;
    }
    else
    {
        if (FAILED(D3DXCreateTexture(m_pDevice,                  //__in   LPDIRECT3DDEVICE9 pDevice,
                                     uiSizeX,                    //__in   UINT Width,
                                     uiSizeY,                    //__in   UINT Height,
                                     iMipMaps,                   //__in   UINT MipLevels,
                                     0,                          //__in   DWORD Usage,
                                     D3DFormat,                  //__in   D3DFORMAT Format,
                                     D3DPOOL_MANAGED,            //__in   D3DPOOL Pool,
                                     (IDirect3DTexture9**)&m_pD3DTexture)))
            return;

        // Update surface size if it's a normal texture
        D3DSURFACE_DESC desc;
        ((IDirect3DTexture9*)m_pD3DTexture)->GetLevelDesc(0, &desc);
        m_uiSurfaceSizeX = desc.Width;
        m_uiSurfaceSizeY = desc.Height;
    }

    // Calc memory usage
    m_iMemoryKBUsed = CRenderItemManager::CalcD3DResourceMemoryKBUsage(m_pD3DTexture);
}
CRenderTarget::CRenderTarget		()
{
	param_blur			= 0.f;
	param_gray			= 0.f;
	param_noise			= 0.f;
	param_duality_h		= 0.f;
	param_duality_v		= 0.f;
	param_noise_fps		= 25.f;
	param_noise_scale	= 1.f;

	im_noise_time		= 1/100;
	im_noise_shift_w	= 0;
	im_noise_shift_h	= 0;

	param_color_base	= color_rgba(127,127,127,	0);
	param_color_gray	= color_rgba(85,85,85,		0);
	param_color_add		= color_rgba(0,0,0,			0);

	dwAccumulatorClearMark			= 0;
	Device.Resources->Evict			();

	// Blenders
	b_occq							= xr_new<CBlender_light_occq>			();
	b_accum_mask					= xr_new<CBlender_accum_direct_mask>	();
	b_accum_direct					= xr_new<CBlender_accum_direct>			();
	b_accum_point					= xr_new<CBlender_accum_point>			();
	b_accum_spot					= xr_new<CBlender_accum_spot>			();
	b_accum_reflected				= xr_new<CBlender_accum_reflected>		();
	b_bloom							= xr_new<CBlender_bloom_build>			();
	b_luminance						= xr_new<CBlender_luminance>			();
	b_combine						= xr_new<CBlender_combine>				();

	//	NORMAL
	{
		u32		w=Device.dwWidth, h=Device.dwHeight;
		rt_Position.create			(r2_RT_P,		w,h,D3DFMT_A16B16G16R16F);
		rt_Normal.create			(r2_RT_N,		w,h,D3DFMT_A16B16G16R16F);

		// select albedo & accum
		if (RImplementation.o.mrtmixdepth)	
		{
			// NV50
			rt_Color.create			(r2_RT_albedo,	w,h,D3DFMT_A8R8G8B8		);
			rt_Accumulator.create	(r2_RT_accum,	w,h,D3DFMT_A16B16G16R16F);
		}
		else		
		{
			// can't - mix-depth
			if (RImplementation.o.fp16_blend) {
				// NV40
				rt_Color.create				(r2_RT_albedo,		w,h,D3DFMT_A16B16G16R16F);	// expand to full
				rt_Accumulator.create		(r2_RT_accum,		w,h,D3DFMT_A16B16G16R16F);
			} else {
				// R4xx, no-fp-blend,-> albedo_wo
				VERIFY						(RImplementation.o.albedo_wo);
				rt_Color.create				(r2_RT_albedo,		w,h,D3DFMT_A8R8G8B8		);	// normal
				rt_Accumulator.create		(r2_RT_accum,		w,h,D3DFMT_A16B16G16R16F);
				rt_Accumulator_temp.create	(r2_RT_accum_temp,	w,h,D3DFMT_A16B16G16R16F);
			}
		}

		// generic(LDR) RTs
		rt_Generic_0.create			(r2_RT_generic0,w,h,D3DFMT_A8R8G8B8		);
		rt_Generic_1.create			(r2_RT_generic1,w,h,D3DFMT_A8R8G8B8		);
	}

	// OCCLUSION
	s_occq.create					(b_occq,		"r2\\occq");

	// DIRECT (spot)
	D3DFORMAT						depth_format	= (D3DFORMAT)RImplementation.o.HW_smap_FORMAT;

	if (RImplementation.o.HW_smap)
	{
		D3DFORMAT	nullrt				= D3DFMT_R5G6B5;
		if (RImplementation.o.nullrt)	nullrt	= (D3DFORMAT)MAKEFOURCC('N','U','L','L');

		u32	size					=RImplementation.o.smapsize	;
		rt_smap_depth.create		(r2_RT_smap_depth,			size,size,depth_format	);
		rt_smap_surf.create			(r2_RT_smap_surf,			size,size,nullrt		);
		rt_smap_ZB					= NULL;
		s_accum_mask.create			(b_accum_mask,				"r2\\accum_mask");
		s_accum_direct.create		(b_accum_direct,			"r2\\accum_direct");
	}
	else
	{
		u32	size					=RImplementation.o.smapsize	;
		rt_smap_surf.create			(r2_RT_smap_surf,			size,size,D3DFMT_R32F);
		rt_smap_depth				= NULL;
		R_CHK						(HW.pDevice->CreateDepthStencilSurface	(size,size,D3DFMT_D24X8,D3DMULTISAMPLE_NONE,0,TRUE,&rt_smap_ZB,NULL));
		s_accum_mask.create			(b_accum_mask,				"r2\\accum_mask");
		s_accum_direct.create		(b_accum_direct,			"r2\\accum_direct");
	}

	// POINT
	{
		s_accum_point.create		(b_accum_point,				"r2\\accum_point_s");
		accum_point_geom_create		();
		g_accum_point.create		(D3DFVF_XYZ,				g_accum_point_vb, g_accum_point_ib);
		accum_omnip_geom_create		();
		g_accum_omnipart.create		(D3DFVF_XYZ,				g_accum_omnip_vb, g_accum_omnip_ib);
	}

	// SPOT
	{
		s_accum_spot.create			(b_accum_spot,				"r2\\accum_spot_s",	"lights\\lights_spot01");
		accum_spot_geom_create		();
		g_accum_spot.create			(D3DFVF_XYZ,				g_accum_spot_vb, g_accum_spot_ib);
	}

	// REFLECTED
	{
		s_accum_reflected.create	(b_accum_reflected,			"r2\\accum_refl");
	}

	// BLOOM
	{
		D3DFORMAT	fmt				= D3DFMT_A8R8G8B8;			//;		// D3DFMT_X8R8G8B8
		u32	w=BLOOM_size_X, h=BLOOM_size_Y;
		u32 fvf_build				= D3DFVF_XYZRHW|D3DFVF_TEX4|D3DFVF_TEXCOORDSIZE2(0)|D3DFVF_TEXCOORDSIZE2(1)|D3DFVF_TEXCOORDSIZE2(2)|D3DFVF_TEXCOORDSIZE2(3);
		u32 fvf_filter				= (u32)D3DFVF_XYZRHW|D3DFVF_TEX8|D3DFVF_TEXCOORDSIZE4(0)|D3DFVF_TEXCOORDSIZE4(1)|D3DFVF_TEXCOORDSIZE4(2)|D3DFVF_TEXCOORDSIZE4(3)|D3DFVF_TEXCOORDSIZE4(4)|D3DFVF_TEXCOORDSIZE4(5)|D3DFVF_TEXCOORDSIZE4(6)|D3DFVF_TEXCOORDSIZE4(7);
		rt_Bloom_1.create			(r2_RT_bloom1,	w,h,		fmt);
		rt_Bloom_2.create			(r2_RT_bloom2,	w,h,		fmt);
		g_bloom_build.create		(fvf_build,		RCache.Vertex.Buffer(), RCache.QuadIB);
		g_bloom_filter.create		(fvf_filter,	RCache.Vertex.Buffer(), RCache.QuadIB);
		s_bloom_dbg_1.create		("effects\\screen_set",		r2_RT_bloom1);
		s_bloom_dbg_2.create		("effects\\screen_set",		r2_RT_bloom2);
		s_bloom.create				(b_bloom,					"r2\\bloom");
		f_bloom_factor				= 0.5f;
	}

	// TONEMAP
	{
		rt_LUM_64.create			(r2_RT_luminance_t64,	64, 64,	D3DFMT_A16B16G16R16F	);
		rt_LUM_8.create				(r2_RT_luminance_t8,	8,	8,	D3DFMT_A16B16G16R16F	);
		s_luminance.create			(b_luminance,				"r2\\luminance");
		f_luminance_adapt			= 0.5f;

		t_LUM_src.create			(r2_RT_luminance_src);
		t_LUM_dest.create			(r2_RT_luminance_cur);

		// create pool
		for (u32 it=0; it<4; it++)	{
			string256					name;
			sprintf						(name,"%s_%d",	r2_RT_luminance_pool,it	);
			rt_LUM_pool[it].create		(name,	1,	1,	D3DFMT_R32F				);
			u_setrt						(rt_LUM_pool[it],	0,	0,	0			);
			CHK_DX						(HW.pDevice->Clear( 0L, NULL, D3DCLEAR_TARGET,	0x7f7f7f7f,	1.0f, 0L));
		}
		u_setrt						( Device.dwWidth,Device.dwHeight,HW.pBaseRT,NULL,NULL,HW.pBaseZB);
	}

	// COMBINE
	{
		static D3DVERTEXELEMENT9 dwDecl[] =
		{
			{ 0, 0,  D3DDECLTYPE_FLOAT4,	D3DDECLMETHOD_DEFAULT, 	D3DDECLUSAGE_POSITION,	0 },	// pos+uv
			D3DDECL_END()
		};
		s_combine.create					(b_combine,					"r2\\combine");
		s_combine_dbg_0.create				("effects\\screen_set",		r2_RT_smap_surf		);	
		s_combine_dbg_1.create				("effects\\screen_set",		r2_RT_luminance_t8	);
		s_combine_dbg_Accumulator.create	("effects\\screen_set",		r2_RT_accum			);
		g_combine_VP.create					(dwDecl,		RCache.Vertex.Buffer(), RCache.QuadIB);
		g_combine.create					(FVF::F_TL,		RCache.Vertex.Buffer(), RCache.QuadIB);
		g_combine_2UV.create				(FVF::F_TL2uv,	RCache.Vertex.Buffer(), RCache.QuadIB);

		u32 fvf_aa_blur				= D3DFVF_XYZRHW|D3DFVF_TEX4|D3DFVF_TEXCOORDSIZE2(0)|D3DFVF_TEXCOORDSIZE2(1)|D3DFVF_TEXCOORDSIZE2(2)|D3DFVF_TEXCOORDSIZE2(3);
		g_aa_blur.create			(fvf_aa_blur,	RCache.Vertex.Buffer(), RCache.QuadIB);

		u32 fvf_aa_AA				= D3DFVF_XYZRHW|D3DFVF_TEX7|D3DFVF_TEXCOORDSIZE2(0)|D3DFVF_TEXCOORDSIZE2(1)|D3DFVF_TEXCOORDSIZE2(2)|D3DFVF_TEXCOORDSIZE2(3)|D3DFVF_TEXCOORDSIZE2(4)|D3DFVF_TEXCOORDSIZE4(5)|D3DFVF_TEXCOORDSIZE4(6);
		g_aa_AA.create				(fvf_aa_AA,		RCache.Vertex.Buffer(), RCache.QuadIB);

		t_envmap_0.create			(r2_T_envs0);
		t_envmap_1.create			(r2_T_envs1);
	}

	// Build textures
	{
		// Build material(s)
		{
			// Surface
			R_CHK						(D3DXCreateVolumeTexture(HW.pDevice,TEX_material_LdotN,TEX_material_LdotH,4,1,0,D3DFMT_A8L8,D3DPOOL_MANAGED,&t_material_surf));
			t_material					= Device.Resources->_CreateTexture(r2_material);
			t_material->surface_set		(t_material_surf);

			// Fill it (addr: x=dot(L,N),y=dot(L,H))
			D3DLOCKED_BOX				R;
			R_CHK						(t_material_surf->LockBox	(0,&R,0,0));
			for (u32 slice=0; slice<4; slice++)
			{
				for (u32 y=0; y<TEX_material_LdotH; y++)
				{
					for (u32 x=0; x<TEX_material_LdotN; x++)
					{
						u16*	p	=	(u16*)		(LPBYTE (R.pBits) + slice*R.SlicePitch + y*R.RowPitch + x*2);
						float	ld	=	float(x)	/ float	(TEX_material_LdotN-1);
						float	ls	=	float(y)	/ float	(TEX_material_LdotH-1) + EPS_S;
						ls			*=	powf(ld,1/32.f);
						float	fd,fs;

						switch	(slice)
						{
						case 0:	{ // looks like OrenNayar
							fd	= powf(ld,0.75f);		// 0.75
							fs	= powf(ls,16.f)*.5f;
								}	break;
						case 1:	{// looks like Blinn
							fd	= powf(ld,0.90f);		// 0.90
							fs	= powf(ls,24.f);
								}	break;
						case 2:	{ // looks like Phong
							fd	= ld;					// 1.0
							fs	= powf(ls*1.01f,128.f	);
								}	break;
						case 3:	{ // looks like Metal
							float	s0	=	_abs	(1-_abs	(0.05f*_sin(33.f*ld)+ld-ls));
							float	s1	=	_abs	(1-_abs	(0.05f*_cos(33.f*ld*ls)+ld-ls));
							float	s2	=	_abs	(1-_abs	(ld-ls));
							fd		=	ld;				// 1.0
							fs		=	powf	(_max(_max(s0,s1),s2), 24.f);
							fs		*=	powf	(ld,1/7.f);
								}	break;
						default:
							fd	= fs = 0;
						}
						s32		_d	=	clampr	(iFloor	(fd*255.5f),	0,255);
						s32		_s	=	clampr	(iFloor	(fs*255.5f),	0,255);
						if ((y==(TEX_material_LdotH-1)) && (x==(TEX_material_LdotN-1)))	{ _d = 255; _s=255;	}
						*p			=	u16		(_s*256 + _d);
					}
				}
			}
			R_CHK		(t_material_surf->UnlockBox	(0));
			// #ifdef DEBUG
			// R_CHK	(D3DXSaveTextureToFile	("x:\\r2_material.dds",D3DXIFF_DDS,t_material_surf,0));
			// #endif
		}

		// Build noise table
		if (1)
		{
			// Surfaces
			D3DLOCKED_RECT				R[TEX_jitter_count];
			for (int it=0; it<TEX_jitter_count; it++)
			{
				string_path					name;
				sprintf						(name,"%s%d",r2_jitter,it);
				R_CHK	(D3DXCreateTexture	(HW.pDevice,TEX_jitter,TEX_jitter,1,0,D3DFMT_Q8W8V8U8,D3DPOOL_MANAGED,&t_noise_surf[it]));
				t_noise[it]					= Device.Resources->_CreateTexture	(name);
				t_noise[it]->surface_set	(t_noise_surf[it]);
				R_CHK						(t_noise_surf[it]->LockRect	(0,&R[it],0,0));
			}

			// Fill it,
			for (u32 y=0; y<TEX_jitter; y++)
			{
				for (u32 x=0; x<TEX_jitter; x++)
				{
					DWORD	data	[TEX_jitter_count];
					generate_jitter	(data,TEX_jitter_count);
					for (u32 it=0; it<TEX_jitter_count; it++)
					{
						u32*	p	=	(u32*)	(LPBYTE (R[it].pBits) + y*R[it].Pitch + x*4);
								*p	=	data	[it];
					}
				}
			}
			for (int it=0; it<TEX_jitter_count; it++)	{
				R_CHK						(t_noise_surf[it]->UnlockRect(0));
			}
		}
	}

	// PP
	s_postprocess.create				("postprocess");
	g_postprocess.create				(D3DFVF_XYZRHW|D3DFVF_DIFFUSE|D3DFVF_SPECULAR|D3DFVF_TEX3,RCache.Vertex.Buffer(),RCache.QuadIB);

	// Menu
	s_menu.create						("distort");
	g_menu.create						(FVF::F_TL,RCache.Vertex.Buffer(),RCache.QuadIB);

	// 
	dwWidth		= Device.dwWidth;
	dwHeight	= Device.dwHeight;
}
Exemple #3
0
CRenderTarget::CRenderTarget		()
{
	param_blur			= 0.f;
	param_gray			= 0.f;
	param_noise			= 0.f;
	param_duality_h		= 0.f;
	param_duality_v		= 0.f;
	param_noise_fps		= 25.f;
	param_noise_scale	= 1.f;

	im_noise_time		= 1/100;
	im_noise_shift_w	= 0;
	im_noise_shift_h	= 0;

	param_color_base	= color_rgba(127,127,127,	0);
	param_color_gray	= color_rgba(85,85,85,		0);
	param_color_add.set( 0.0f, 0.0f, 0.0f );

	dwAccumulatorClearMark			= 0;
	dxRenderDeviceRender::Instance().Resources->Evict			();

	// Blenders
	b_occq							= new CBlender_light_occq();
	b_accum_mask					= new CBlender_accum_direct_mask();
	b_accum_direct					= new CBlender_accum_direct();
	b_accum_point					= new CBlender_accum_point();
	b_accum_spot					= new CBlender_accum_spot();
	b_accum_reflected				= new CBlender_accum_reflected();
	b_bloom							= new CBlender_bloom_build();
	b_ssao							= new CBlender_SSAO();
	b_luminance						= new CBlender_luminance();
	b_combine						= new CBlender_combine();

	//	NORMAL
	{
		u32		w=Device.dwWidth, h=Device.dwHeight;
		rt_Position.create			(r2_RT_P,		w,h,D3DFMT_A16B16G16R16F);
		rt_Normal.create			(r2_RT_N,		w,h,D3DFMT_A16B16G16R16F);

		// select albedo & accum
		if (RImplementation.o.mrtmixdepth)	
		{
			// NV50
			rt_Color.create			(r2_RT_albedo,	w,h,D3DFMT_A8R8G8B8		);
			rt_Accumulator.create	(r2_RT_accum,	w,h,D3DFMT_A16B16G16R16F);
		}
		else		
		{
			// can't - mix-depth
			if (RImplementation.o.fp16_blend) {
				// NV40
				rt_Color.create				(r2_RT_albedo,		w,h,D3DFMT_A16B16G16R16F);	// expand to full
				rt_Accumulator.create		(r2_RT_accum,		w,h,D3DFMT_A16B16G16R16F);
			} else {
				// R4xx, no-fp-blend,-> albedo_wo
				VERIFY						(RImplementation.o.albedo_wo);
				rt_Color.create				(r2_RT_albedo,		w,h,D3DFMT_A8R8G8B8		);	// normal
				rt_Accumulator.create		(r2_RT_accum,		w,h,D3DFMT_A16B16G16R16F);
				rt_Accumulator_temp.create	(r2_RT_accum_temp,	w,h,D3DFMT_A16B16G16R16F);
			}
		}

		// generic(LDR) RTs
		rt_Generic_0.create			(r2_RT_generic0,w,h,D3DFMT_A8R8G8B8		);
		rt_Generic_1.create			(r2_RT_generic1,w,h,D3DFMT_A8R8G8B8		);
		//	Igor: for volumetric lights
		//rt_Generic_2.create			(r2_RT_generic2,w,h,D3DFMT_A8R8G8B8		);
		//	temp: for higher quality blends
		if (RImplementation.o.advancedpp)
			rt_Generic_2.create			(r2_RT_generic2,w,h,D3DFMT_A16B16G16R16F);
	}

	// OCCLUSION
	s_occq.create					(b_occq,		"r2\\occq");

	// DIRECT (spot)
	D3DFORMAT						depth_format	= (D3DFORMAT)RImplementation.o.HW_smap_FORMAT;

	if (RImplementation.o.HW_smap)
	{
		D3DFORMAT	nullrt				= D3DFMT_R5G6B5;
		if (RImplementation.o.nullrt)	nullrt	= (D3DFORMAT)MAKEFOURCC('N','U','L','L');

		u32	size					=RImplementation.o.smapsize	;
		rt_smap_depth.create		(r2_RT_smap_depth,			size,size,depth_format	);
		rt_smap_surf.create			(r2_RT_smap_surf,			size,size,nullrt		);
		rt_smap_ZB					= NULL;
		s_accum_mask.create			(b_accum_mask,				"r2\\accum_mask");
		s_accum_direct.create		(b_accum_direct,			"r2\\accum_direct");
		if (RImplementation.o.advancedpp)
			s_accum_direct_volumetric.create("accum_volumetric_sun");
	}
	else
	{
		u32	size					=RImplementation.o.smapsize	;
		rt_smap_surf.create			(r2_RT_smap_surf,			size,size,D3DFMT_R32F);
		rt_smap_depth				= NULL;
		R_CHK						(HW.pDevice->CreateDepthStencilSurface	(size,size,D3DFMT_D24X8,D3DMULTISAMPLE_NONE,0,TRUE,&rt_smap_ZB,NULL));
		s_accum_mask.create			(b_accum_mask,				"r2\\accum_mask");
		s_accum_direct.create		(b_accum_direct,			"r2\\accum_direct");
		if (RImplementation.o.advancedpp)
			s_accum_direct_volumetric.create("accum_volumetric_sun");
	}

	// POINT
	{
		s_accum_point.create		(b_accum_point,				"r2\\accum_point_s");
		accum_point_geom_create		();
		g_accum_point.create		(D3DFVF_XYZ,				g_accum_point_vb, g_accum_point_ib);
		accum_omnip_geom_create		();
		g_accum_omnipart.create		(D3DFVF_XYZ,				g_accum_omnip_vb, g_accum_omnip_ib);
	}

	// SPOT
	{
		s_accum_spot.create			(b_accum_spot,				"r2\\accum_spot_s",	"lights\\lights_spot01");
		accum_spot_geom_create		();
		g_accum_spot.create			(D3DFVF_XYZ,				g_accum_spot_vb, g_accum_spot_ib);
	}

	{
		s_accum_volume.create("accum_volumetric", "lights\\lights_spot01");
		accum_volumetric_geom_create();
		g_accum_volumetric.create( D3DFVF_XYZ, g_accum_volumetric_vb, g_accum_volumetric_ib);
	}
			

	// REFLECTED
	{
		s_accum_reflected.create	(b_accum_reflected,			"r2\\accum_refl");
	}

	// BLOOM
	{
		D3DFORMAT	fmt				= D3DFMT_A8R8G8B8;			//;		// D3DFMT_X8R8G8B8
		u32	w=BLOOM_size_X, h=BLOOM_size_Y;
		u32 fvf_build				= D3DFVF_XYZRHW|D3DFVF_TEX4|D3DFVF_TEXCOORDSIZE2(0)|D3DFVF_TEXCOORDSIZE2(1)|D3DFVF_TEXCOORDSIZE2(2)|D3DFVF_TEXCOORDSIZE2(3);
		u32 fvf_filter				= (u32)D3DFVF_XYZRHW|D3DFVF_TEX8|D3DFVF_TEXCOORDSIZE4(0)|D3DFVF_TEXCOORDSIZE4(1)|D3DFVF_TEXCOORDSIZE4(2)|D3DFVF_TEXCOORDSIZE4(3)|D3DFVF_TEXCOORDSIZE4(4)|D3DFVF_TEXCOORDSIZE4(5)|D3DFVF_TEXCOORDSIZE4(6)|D3DFVF_TEXCOORDSIZE4(7);
		rt_Bloom_1.create			(r2_RT_bloom1,	w,h,		fmt);
		rt_Bloom_2.create			(r2_RT_bloom2,	w,h,		fmt);
		g_bloom_build.create		(fvf_build,		RCache.Vertex.Buffer(), RCache.QuadIB);
		g_bloom_filter.create		(fvf_filter,	RCache.Vertex.Buffer(), RCache.QuadIB);
		s_bloom_dbg_1.create		("effects\\screen_set",		r2_RT_bloom1);
		s_bloom_dbg_2.create		("effects\\screen_set",		r2_RT_bloom2);
		s_bloom.create				(b_bloom,					"r2\\bloom");
		f_bloom_factor				= 0.5f;
	}

	//HBAO
	if (RImplementation.o.ssao_opt_data)
	{
		u32		w = 0;
		u32		h = 0;
		if (RImplementation.o.ssao_half_data)
		{
			w = Device.dwWidth / 2;
			h = Device.dwHeight / 2;
		}
		else
		{
			w = Device.dwWidth;
			h = Device.dwHeight;
		}
		D3DFORMAT	fmt = HW.Caps.id_vendor==0x10DE?D3DFMT_R32F:D3DFMT_R16F;

		rt_half_depth.create		(r2_RT_half_depth, w, h, fmt);
		s_ssao.create				(b_ssao, "r2\\ssao");
	}

	//SSAO
	if (RImplementation.o.ssao_blur_on)
	{
		u32		w = Device.dwWidth, h = Device.dwHeight;
		rt_ssao_temp.create			(r2_RT_ssao_temp, w, h, D3DFMT_G16R16F);
		s_ssao.create				(b_ssao, "r2\\ssao");
	}

	// TONEMAP
	{
		rt_LUM_64.create			(r2_RT_luminance_t64,	64, 64,	D3DFMT_A16B16G16R16F	);
		rt_LUM_8.create				(r2_RT_luminance_t8,	8,	8,	D3DFMT_A16B16G16R16F	);
		s_luminance.create			(b_luminance,				"r2\\luminance");
		f_luminance_adapt			= 0.5f;

		t_LUM_src.create			(r2_RT_luminance_src);
		t_LUM_dest.create			(r2_RT_luminance_cur);

		// create pool
		for (u32 it=0; it<HW.Caps.iGPUNum*2; it++)	{
			string256					name;
			sprintf						(name,"%s_%d",	r2_RT_luminance_pool,it	);
			rt_LUM_pool[it].create		(name,	1,	1,	D3DFMT_R32F				);
			u_setrt						(rt_LUM_pool[it],	0,	0,	0			);
			CHK_DX						(HW.pDevice->Clear( 0L, NULL, D3DCLEAR_TARGET,	0x7f7f7f7f,	1.0f, 0L));
		}
		u_setrt						( Device.dwWidth,Device.dwHeight,HW.pBaseRT,NULL,NULL,HW.pBaseZB);
	}

	// COMBINE
	{
		static D3DVERTEXELEMENT9 dwDecl[] =
		{
			{ 0, 0,  D3DDECLTYPE_FLOAT4,	D3DDECLMETHOD_DEFAULT, 	D3DDECLUSAGE_POSITION,	0 },	// pos+uv
			{ 0, 16, D3DDECLTYPE_D3DCOLOR,	D3DDECLMETHOD_DEFAULT, 	D3DDECLUSAGE_COLOR,		0 },
			{ 0, 20, D3DDECLTYPE_FLOAT2,	D3DDECLMETHOD_DEFAULT, 	D3DDECLUSAGE_TEXCOORD,	0 },
			D3DDECL_END()
		};
		s_combine.create					(b_combine,					"r2\\combine");
		s_combine_volumetric.create			("combine_volumetric");
		s_combine_dbg_0.create				("effects\\screen_set",		r2_RT_smap_surf		);	
		s_combine_dbg_1.create				("effects\\screen_set",		r2_RT_luminance_t8	);
		s_combine_dbg_Accumulator.create	("effects\\screen_set",		r2_RT_accum			);
		g_combine_VP.create					(dwDecl,		RCache.Vertex.Buffer(), RCache.QuadIB);
		g_combine.create					(FVF::F_TL,		RCache.Vertex.Buffer(), RCache.QuadIB);
		g_combine_2UV.create				(FVF::F_TL2uv,	RCache.Vertex.Buffer(), RCache.QuadIB);

		u32 fvf_aa_blur				= D3DFVF_XYZRHW|D3DFVF_TEX4|D3DFVF_TEXCOORDSIZE2(0)|D3DFVF_TEXCOORDSIZE2(1)|D3DFVF_TEXCOORDSIZE2(2)|D3DFVF_TEXCOORDSIZE2(3);
		g_aa_blur.create			(fvf_aa_blur,	RCache.Vertex.Buffer(), RCache.QuadIB);

		u32 fvf_aa_AA				= D3DFVF_XYZRHW|D3DFVF_TEX7|D3DFVF_TEXCOORDSIZE2(0)|D3DFVF_TEXCOORDSIZE2(1)|D3DFVF_TEXCOORDSIZE2(2)|D3DFVF_TEXCOORDSIZE2(3)|D3DFVF_TEXCOORDSIZE2(4)|D3DFVF_TEXCOORDSIZE4(5)|D3DFVF_TEXCOORDSIZE4(6);
		g_aa_AA.create				(fvf_aa_AA,		RCache.Vertex.Buffer(), RCache.QuadIB);

		t_envmap_0.create			(r2_T_envs0);
		t_envmap_1.create			(r2_T_envs1);
	}

	// Build textures
	{
		// Build material(s)
		{
			// Surface
			R_CHK						(D3DXCreateVolumeTexture(HW.pDevice,TEX_material_LdotN,TEX_material_LdotH,4,1,0,D3DFMT_A8L8,D3DPOOL_MANAGED,&t_material_surf));
			t_material					= dxRenderDeviceRender::Instance().Resources->_CreateTexture(r2_material);
			t_material->surface_set		(t_material_surf);

			// Fill it (addr: x=dot(L,N),y=dot(L,H))
			D3DLOCKED_BOX				R;
			R_CHK						(t_material_surf->LockBox	(0,&R,0,0));
			for (u32 slice=0; slice<4; slice++)
			{
				for (u32 y=0; y<TEX_material_LdotH; y++)
				{
					for (u32 x=0; x<TEX_material_LdotN; x++)
					{
						u16*	p	=	(u16*)		(LPBYTE (R.pBits) + slice*R.SlicePitch + y*R.RowPitch + x*2);
						float	ld	=	float(x)	/ float	(TEX_material_LdotN-1);
						float	ls	=	float(y)	/ float	(TEX_material_LdotH-1) + EPS_S;
						ls			*=	powf(ld,1/32.f);
						float	fd,fs;

						switch	(slice)
						{
						case 0:	{ // looks like OrenNayar
							fd	= powf(ld,0.75f);		// 0.75
							fs	= powf(ls,16.f)*.5f;
								}	break;
						case 1:	{// looks like Blinn
							fd	= powf(ld,0.90f);		// 0.90
							fs	= powf(ls,24.f);
								}	break;
						case 2:	{ // looks like Phong
							fd	= ld;					// 1.0
							fs	= powf(ls*1.01f,128.f	);
								}	break;
						case 3:	{ // looks like Metal
							float	s0	=	_abs	(1-_abs	(0.05f*_sin(33.f*ld)+ld-ls));
							float	s1	=	_abs	(1-_abs	(0.05f*_cos(33.f*ld*ls)+ld-ls));
							float	s2	=	_abs	(1-_abs	(ld-ls));
							fd		=	ld;				// 1.0
							fs		=	powf	(_max(_max(s0,s1),s2), 24.f);
							fs		*=	powf	(ld,1/7.f);
								}	break;
						default:
							fd	= fs = 0;
						}
						s32		_d	=	clampr	(iFloor	(fd*255.5f),	0,255);
						s32		_s	=	clampr	(iFloor	(fs*255.5f),	0,255);
						if ((y==(TEX_material_LdotH-1)) && (x==(TEX_material_LdotN-1)))	{ _d = 255; _s=255;	}
						*p			=	u16		(_s*256 + _d);
					}
				}
			}
			R_CHK		(t_material_surf->UnlockBox	(0));
			// #ifdef DEBUG
			// R_CHK	(D3DXSaveTextureToFile	("x:\\r2_material.dds",D3DXIFF_DDS,t_material_surf,0));
			// #endif
		}

		// Build noise table
		if (1)
		{
			// Surfaces
			D3DLOCKED_RECT				R[TEX_jitter_count];
			for (int it1=0; it1<TEX_jitter_count-1; it1++)
			{
				string_path					name;
				sprintf						(name,"%s%d",r2_jitter,it1);
				R_CHK	(D3DXCreateTexture	(HW.pDevice,TEX_jitter,TEX_jitter,1,0,D3DFMT_Q8W8V8U8,D3DPOOL_MANAGED,&t_noise_surf[it1]));
				t_noise[it1]					= dxRenderDeviceRender::Instance().Resources->_CreateTexture	(name);
				t_noise[it1]->surface_set	(t_noise_surf[it1]);
				R_CHK						(t_noise_surf[it1]->LockRect	(0,&R[it1],0,0));
			}	

			// Fill it,
			for (u32 y=0; y<TEX_jitter; y++)
			{
				for (u32 x=0; x<TEX_jitter; x++)
				{
					DWORD	data	[TEX_jitter_count-1];
					generate_jitter	(data,TEX_jitter_count-1);
					for (u32 it2=0; it2<TEX_jitter_count-1; it2++)
					{
						u32*	p	=	(u32*)	(LPBYTE (R[it2].pBits) + y*R[it2].Pitch + x*4);
								*p	=	data	[it2];
					}
				}
			}
			
			for (int it3=0; it3<TEX_jitter_count-1; it3++)	{
				R_CHK						(t_noise_surf[it3]->UnlockRect(0));
			}		

			// generate HBAO jitter texture (last)
			int it = TEX_jitter_count - 1;
			string_path					name;
			sprintf						(name,"%s%d",r2_jitter,it);
			R_CHK	(D3DXCreateTexture	(HW.pDevice,TEX_jitter,TEX_jitter,1,0,D3DFMT_A32B32G32R32F,D3DPOOL_MANAGED,&t_noise_surf[it]));
			t_noise[it]					= dxRenderDeviceRender::Instance().Resources->_CreateTexture	(name);
			t_noise[it]->surface_set	(t_noise_surf[it]);
			R_CHK						(t_noise_surf[it]->LockRect	(0,&R[it],0,0));
			
			// Fill it,
			for (u32 y=0; y<TEX_jitter; y++)
			{
				for (u32 x=0; x<TEX_jitter; x++)
				{
					float numDir = 1.0f;
					switch (ps_r_ssao)
					{
						case 1: numDir = 4.0f; break;
						case 2: numDir = 6.0f; break;
						case 3: numDir = 8.0f; break;
					}
					float angle = 2 * PI * ::Random.randF(0.0f, 1.0f) / numDir;
					float dist = ::Random.randF(0.0f, 1.0f);
					//float dest[4];
					
					float*	p	=	(float*)	(LPBYTE (R[it].pBits) + y*R[it].Pitch + x*4*sizeof(float));
					*p = (float)(_cos(angle));
					*(p+1) = (float)(_sin(angle));
					*(p+2) = (float)(dist);
					*(p+3) = 0;
					
					//generate_hbao_jitter	(data,TEX_jitter*TEX_jitter);
				}
			}			
			R_CHK						(t_noise_surf[it]->UnlockRect(0));
		}
	}

	// PP
	s_postprocess.create				("postprocess");
	g_postprocess.create				(D3DFVF_XYZRHW|D3DFVF_DIFFUSE|D3DFVF_SPECULAR|D3DFVF_TEX3,RCache.Vertex.Buffer(),RCache.QuadIB);

	// Menu
	s_menu.create						("distort");
	g_menu.create						(FVF::F_TL,RCache.Vertex.Buffer(),RCache.QuadIB);

	//	Igor: TMP
	//	Create an RT for online screenshot makining
	//u32		w = Device.dwWidth, h = Device.dwHeight;
	//HW.pDevice->CreateOffscreenPlainSurface(Device.dwWidth,Device.dwHeight,D3DFMT_A8R8G8B8,D3DPOOL_SYSTEMMEM,&pFB,NULL);
	//HW.pDevice->CreateOffscreenPlainSurface(Device.dwWidth,Device.dwHeight,rt_Color->fmt,D3DPOOL_SYSTEMMEM,&pFB,NULL);
	D3DSURFACE_DESC	desc;
	HW.pBaseRT->GetDesc(&desc);
	HW.pDevice->CreateOffscreenPlainSurface(Device.dwWidth,Device.dwHeight,desc.Format,D3DPOOL_SYSTEMMEM,&pFB,NULL);

	// 
	dwWidth		= Device.dwWidth;
	dwHeight	= Device.dwHeight;
}
Exemple #4
0
HRESULT ScalingEffect::ParseParameters(LPD3DXEFFECTCOMPILER lpEffectCompiler)
{
    HRESULT hr = S_OK;

    if(m_pEffect == NULL)
        return E_FAIL;

    // Look at parameters for semantics and annotations that we know how to interpret
    D3DXPARAMETER_DESC ParamDesc;
    D3DXPARAMETER_DESC AnnotDesc;
    D3DXHANDLE hParam;
    D3DXHANDLE hAnnot;
    LPDIRECT3DBASETEXTURE9 pTex = NULL;

    for(UINT iParam = 0; iParam < m_EffectDesc.Parameters; iParam++) {
        LPCSTR pstrName = NULL;
        LPCSTR pstrFunction = NULL;
        LPCSTR pstrTarget = NULL;
        LPCSTR pstrTextureType = NULL;
        INT Width = D3DX_DEFAULT;
        INT Height= D3DX_DEFAULT;
        INT Depth = D3DX_DEFAULT;

        hParam = m_pEffect->GetParameter(NULL, iParam);
        m_pEffect->GetParameterDesc(hParam, &ParamDesc);

	if(ParamDesc.Semantic != NULL) {
	    if(ParamDesc.Class == D3DXPC_MATRIX_ROWS || ParamDesc.Class == D3DXPC_MATRIX_COLUMNS) {
		if(strcmpi(ParamDesc.Semantic, "world") == 0)
		    m_MatWorldEffectHandle = hParam;
		else if(strcmpi(ParamDesc.Semantic, "view") == 0)
		    m_MatViewEffectHandle = hParam;
		else if(strcmpi(ParamDesc.Semantic, "projection") == 0)
		    m_MatProjEffectHandle = hParam;
		else if(strcmpi(ParamDesc.Semantic, "worldview") == 0)
		    m_MatWorldViewEffectHandle = hParam;
		else if(strcmpi(ParamDesc.Semantic, "viewprojection") == 0)
		    m_MatViewProjEffectHandle = hParam;
		else if(strcmpi(ParamDesc.Semantic, "worldviewprojection") == 0)
		    m_MatWorldViewProjEffectHandle = hParam;
	    }

	    else if(ParamDesc.Class == D3DXPC_VECTOR && ParamDesc.Type == D3DXPT_FLOAT) {
		if(strcmpi(ParamDesc.Semantic, "sourcedims") == 0)
		    m_SourceDimsEffectHandle = hParam;
		else if(strcmpi(ParamDesc.Semantic, "texelsize") == 0)
		    m_TexelSizeEffectHandle = hParam;
	    }

	    else if(ParamDesc.Class == D3DXPC_SCALAR && ParamDesc.Type == D3DXPT_FLOAT) {
		if(strcmpi(ParamDesc.Semantic, "SCALING") == 0)
		    m_pEffect->GetFloat(hParam, &m_scale);
	    }

		else if (ParamDesc.Class == D3DXPC_STRUCT) {
		if(strcmpi(ParamDesc.Semantic, "VIDPARAMS") == 0)
		{
		    m_VideoInputHandle = hParam;
			m_VideoSizeHandle = m_pEffect->GetParameterByName(m_VideoInputHandle,"video_size");
			m_TextureSizeHandle = m_pEffect->GetParameterByName(m_VideoInputHandle,"texture_size");
			m_OutputSizeHandle = m_pEffect->GetParameterByName(m_VideoInputHandle,"output_size");
			m_FrameCount = m_pEffect->GetParameterByName(m_VideoInputHandle,"frame_count");
		}

	    }
	


	    else if(ParamDesc.Class == D3DXPC_OBJECT && ParamDesc.Type == D3DXPT_TEXTURE) {
		if(strcmpi(ParamDesc.Semantic, "SOURCETEXTURE") == 0)
		    m_SourceTextureEffectHandle = hParam;
		if(strcmpi(ParamDesc.Semantic, "WORKINGTEXTURE") == 0)
		    m_WorkingTexture1EffectHandle = hParam;
		if(strcmpi(ParamDesc.Semantic, "WORKINGTEXTURE1") == 0)
		    m_WorkingTexture2EffectHandle = hParam;
		if(strcmpi(ParamDesc.Semantic, "HQ2XLOOKUPTEXTURE") == 0)
		    m_Hq2xLookupTextureHandle = hParam;
	    }

	    else if(ParamDesc.Class == D3DXPC_OBJECT && ParamDesc.Type == D3DXPT_STRING) {
	        LPCSTR pstrTechnique = NULL;

		if(strcmpi(ParamDesc.Semantic, "COMBINETECHNIQUE") == 0) {
		    m_pEffect->GetString(hParam, &pstrTechnique);
		    m_CombineTechniqueEffectHandle = m_pEffect->GetTechniqueByName(pstrTechnique);
		}
		else if(strcmpi(ParamDesc.Semantic, "PREPROCESSTECHNIQUE") == 0) {
		    m_pEffect->GetString(hParam, &pstrTechnique);
		    m_PreprocessTechnique1EffectHandle = m_pEffect->GetTechniqueByName(pstrTechnique);
		}
		else if(strcmpi(ParamDesc.Semantic, "PREPROCESSTECHNIQUE1") == 0) {
		    m_pEffect->GetString(hParam, &pstrTechnique);
		    m_PreprocessTechnique2EffectHandle = m_pEffect->GetTechniqueByName(pstrTechnique);
		}
		else if(strcmpi(ParamDesc.Semantic, "NAME") == 0)
		    m_pEffect->GetString(hParam, &m_strName);

	    }
		


	}

	for(UINT iAnnot = 0; iAnnot < ParamDesc.Annotations; iAnnot++) {
            hAnnot = m_pEffect->GetAnnotation (hParam, iAnnot);
            m_pEffect->GetParameterDesc(hAnnot, &AnnotDesc);
            if(strcmpi(AnnotDesc.Name, "name") == 0)
                m_pEffect->GetString(hAnnot, &pstrName);
            else if(strcmpi(AnnotDesc.Name, "function") == 0)
                m_pEffect->GetString(hAnnot, &pstrFunction);
            else if(strcmpi(AnnotDesc.Name, "target") == 0)
                m_pEffect->GetString(hAnnot, &pstrTarget);
            else if(strcmpi(AnnotDesc.Name, "width") == 0)
                m_pEffect->GetInt(hAnnot, &Width);
            else if(strcmpi(AnnotDesc.Name, "height") == 0)
                m_pEffect->GetInt(hAnnot, &Height);
            else if(strcmpi(AnnotDesc.Name, "depth") == 0)
                m_pEffect->GetInt(hAnnot, &Depth);
            else if(strcmpi(AnnotDesc.Name, "type") == 0)
                m_pEffect->GetString(hAnnot, &pstrTextureType);

        }
 
	if(pstrFunction != NULL) {
	    LPD3DXBUFFER pTextureShader = NULL;
	    LPD3DXBUFFER lpErrors = 0;

	    if(pstrTarget == NULL || strcmp(pstrTarget,"tx_1_1"))
                pstrTarget = "tx_1_0";

	    if(SUCCEEDED(hr = lpEffectCompiler->CompileShader(
				hAnnot, pstrTarget,
				0, &pTextureShader, &lpErrors, NULL))) {
		SAFE_RELEASE(lpErrors);

		if(Width == D3DX_DEFAULT)
                    Width = 64;
		if(Height == D3DX_DEFAULT)
                    Height = 64;
		if(Depth == D3DX_DEFAULT)
                    Depth = 64;

#if D3DX_SDK_VERSION >= 22
		LPD3DXTEXTURESHADER ppTextureShader;
		D3DXCreateTextureShader((DWORD *)pTextureShader->GetBufferPointer(), &ppTextureShader);
#endif

		if(pstrTextureType != NULL) {
                    if(strcmpi(pstrTextureType, "volume") == 0) {
                        LPDIRECT3DVOLUMETEXTURE9 pVolumeTex = NULL;
                        if(SUCCEEDED(hr = D3DXCreateVolumeTexture(m_pd3dDevice,
                        	Width, Height, Depth, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &pVolumeTex))) {
#if D3DX_SDK_VERSION >= 22
                    	    if(SUCCEEDED(hr = D3DXFillVolumeTextureTX(pVolumeTex, ppTextureShader))) {
#else
                    	    if(SUCCEEDED(hr = D3DXFillVolumeTextureTX(pVolumeTex,
					(DWORD *)pTextureShader->GetBufferPointer(), NULL, 0))) {
#endif
                                pTex = pVolumeTex;
                            }
                        }
                    } else if(strcmpi(pstrTextureType, "cube") == 0) {
                        LPDIRECT3DCUBETEXTURE9 pCubeTex = NULL;
                        if(SUCCEEDED(hr = D3DXCreateCubeTexture(m_pd3dDevice,
                        	Width, D3DX_DEFAULT, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &pCubeTex))) {
#if D3DX_SDK_VERSION >= 22
                            if(SUCCEEDED(hr = D3DXFillCubeTextureTX(pCubeTex, ppTextureShader))) {
#else
                            if(SUCCEEDED(hr = D3DXFillCubeTextureTX(pCubeTex,
					(DWORD *)pTextureShader->GetBufferPointer(), NULL, 0))) {
#endif
                                pTex = pCubeTex;
                            }
                        }
                    }
		} else {
                    LPDIRECT3DTEXTURE9 p2DTex = NULL;
                    if(SUCCEEDED(hr = D3DXCreateTexture(m_pd3dDevice, Width, Height,
                    	    D3DX_DEFAULT, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &p2DTex))) {
#if D3DX_SDK_VERSION >= 22
                        if(SUCCEEDED(hr = D3DXFillTextureTX(p2DTex, ppTextureShader))) {
#else
                        if(SUCCEEDED(hr = D3DXFillTextureTX(p2DTex,
				(DWORD *)pTextureShader->GetBufferPointer(), NULL, 0))) {
#endif
                            pTex = p2DTex;
                        }
                    }
		}
		m_pEffect->SetTexture(m_pEffect->GetParameter(NULL, iParam), pTex);
		SAFE_RELEASE(pTex);
		SAFE_RELEASE(pTextureShader);
#if D3DX_SDK_VERSION >= 22
		SAFE_RELEASE(ppTextureShader);
#endif
	    } else {
		if(lpErrors) {
		    m_strErrors += (char*) lpErrors->GetBufferPointer();
		}

		m_strErrors += "Could not compile texture shader ";
		m_strErrors += pstrFunction;

		SAFE_RELEASE(lpErrors);
		return hr;
	    }
        }
    }

    return S_OK;
}
HRESULT ScalingEffect::ParseParameters(LPD3DXEFFECTCOMPILER lpEffectCompiler)
{
    HRESULT hr = S_OK;

    if(m_pEffect == NULL)
        return E_FAIL;

    // Look at parameters for semantics and annotations that we know how to interpret
    D3DXPARAMETER_DESC ParamDesc;
    D3DXPARAMETER_DESC AnnotDesc;
    D3DXHANDLE hParam;
    D3DXHANDLE hAnnot;
    LPDIRECT3DBASETEXTURE9 pTex = NULL;

    for(UINT iParam = 0; iParam < m_EffectDesc.Parameters; iParam++) {
        LPCSTR pstrName = NULL;
        LPCSTR pstrFunction = NULL;
        LPCSTR pstrTarget = NULL;
        LPCSTR pstrTextureType = NULL;
        INT Width = D3DX_DEFAULT;
        INT Height= D3DX_DEFAULT;
        INT Depth = D3DX_DEFAULT;

        hParam = m_pEffect->GetParameter(NULL, iParam);
        m_pEffect->GetParameterDesc(hParam, &ParamDesc);

	if(ParamDesc.Semantic != NULL) {
	    if(ParamDesc.Class == D3DXPC_MATRIX_ROWS || ParamDesc.Class == D3DXPC_MATRIX_COLUMNS) {
		if(strcmpi(ParamDesc.Semantic, "world") == 0)
		    m_MatWorldEffectHandle = hParam;
		else if(strcmpi(ParamDesc.Semantic, "view") == 0)
		    m_MatViewEffectHandle = hParam;
		else if(strcmpi(ParamDesc.Semantic, "projection") == 0)
		    m_MatProjEffectHandle = hParam;
		else if(strcmpi(ParamDesc.Semantic, "worldview") == 0)
		    m_MatWorldViewEffectHandle = hParam;
		else if(strcmpi(ParamDesc.Semantic, "viewprojection") == 0)
		    m_MatViewProjEffectHandle = hParam;
		else if(strcmpi(ParamDesc.Semantic, "worldviewprojection") == 0)
		    m_MatWorldViewProjEffectHandle = hParam;
	    }

	    else if(ParamDesc.Class == D3DXPC_VECTOR && ParamDesc.Type == D3DXPT_FLOAT) {
		if(strcmpi(ParamDesc.Semantic, "sourcedims") == 0)
		    m_SourceDimsEffectHandle = hParam;
		else if(strcmpi(ParamDesc.Semantic, "texelsize") == 0)
		    m_TexelSizeEffectHandle = hParam;
		else if(strcmpi(ParamDesc.Semantic, "inputdims") == 0) {
		    if(m_iDim[0] && m_iDim[1] && ((hr = m_pEffect->SetFloatArray(hParam, m_iDim, 2)) != D3D_OK)) {
			m_strErrors += "Could not set inputdims parameter!";
			return hr;
		    }
		}
	    }

	    else if(ParamDesc.Class == D3DXPC_SCALAR && ParamDesc.Type == D3DXPT_FLOAT) {
		if(strcmpi(ParamDesc.Semantic, "SCALING") == 0)
		    m_pEffect->GetFloat(hParam, &m_scale);
	    }

	    else if(ParamDesc.Class == D3DXPC_SCALAR && ParamDesc.Type == D3DXPT_BOOL) {
		if(strcmpi(ParamDesc.Semantic, "FORCEUPDATE") == 0)
		    m_pEffect->GetBool(hParam, &m_forceupdate);
	    }

	    else if(ParamDesc.Class == D3DXPC_OBJECT && ParamDesc.Type == D3DXPT_TEXTURE) {
		if(strcmpi(ParamDesc.Semantic, "SOURCETEXTURE") == 0)
		    m_SourceTextureEffectHandle = hParam;
		if(strcmpi(ParamDesc.Semantic, "WORKINGTEXTURE") == 0)
		    m_WorkingTexture1EffectHandle = hParam;
		if(strcmpi(ParamDesc.Semantic, "WORKINGTEXTURE1") == 0)
		    m_WorkingTexture2EffectHandle = hParam;
		if(strcmpi(ParamDesc.Semantic, "HQ2XLOOKUPTEXTURE") == 0)
		    m_Hq2xLookupTextureHandle = hParam;
	    }

	    else if(ParamDesc.Class == D3DXPC_OBJECT && ParamDesc.Type == D3DXPT_STRING) {
	        LPCSTR pstrTechnique = NULL;

		if(strcmpi(ParamDesc.Semantic, "COMBINETECHNIQUE") == 0) {
		    m_pEffect->GetString(hParam, &pstrTechnique);
		    m_CombineTechniqueEffectHandle = m_pEffect->GetTechniqueByName(pstrTechnique);
		}
		else if(strcmpi(ParamDesc.Semantic, "PREPROCESSTECHNIQUE") == 0) {
		    m_pEffect->GetString(hParam, &pstrTechnique);
		    m_PreprocessTechnique1EffectHandle = m_pEffect->GetTechniqueByName(pstrTechnique);
		}
		else if(strcmpi(ParamDesc.Semantic, "PREPROCESSTECHNIQUE1") == 0) {
		    m_pEffect->GetString(hParam, &pstrTechnique);
		    m_PreprocessTechnique2EffectHandle = m_pEffect->GetTechniqueByName(pstrTechnique);
		}
		else if(strcmpi(ParamDesc.Semantic, "NAME") == 0)
		    m_pEffect->GetString(hParam, &m_strName);

	    }


	}

	for(UINT iAnnot = 0; iAnnot < ParamDesc.Annotations; iAnnot++) {
            hAnnot = m_pEffect->GetAnnotation (hParam, iAnnot);
            m_pEffect->GetParameterDesc(hAnnot, &AnnotDesc);
            if(strcmpi(AnnotDesc.Name, "name") == 0)
                m_pEffect->GetString(hAnnot, &pstrName);
            else if(strcmpi(AnnotDesc.Name, "function") == 0)
                m_pEffect->GetString(hAnnot, &pstrFunction);
            else if(strcmpi(AnnotDesc.Name, "target") == 0)
                m_pEffect->GetString(hAnnot, &pstrTarget);
            else if(strcmpi(AnnotDesc.Name, "width") == 0)
                m_pEffect->GetInt(hAnnot, &Width);
            else if(strcmpi(AnnotDesc.Name, "height") == 0)
                m_pEffect->GetInt(hAnnot, &Height);
            else if(strcmpi(AnnotDesc.Name, "depth") == 0)
                m_pEffect->GetInt(hAnnot, &Depth);
            else if(strcmpi(AnnotDesc.Name, "type") == 0)
                m_pEffect->GetString(hAnnot, &pstrTextureType);

        }

	// Not used in DOSBox
/*	if(pstrName != NULL) {
            pTex = NULL;
            DXUtil_ConvertAnsiStringToGenericCch(strPath, pstrName, MAX_PATH);
            if(pstrTextureType != NULL) {
                if(strcmpi(pstrTextureType, "volume") == 0) {
                    LPDIRECT3DVOLUMETEXTURE9 pVolumeTex = NULL;
                    if(SUCCEEDED(hr = D3DXCreateVolumeTextureFromFileEx(m_pd3dDevice, strPath,
                            Width, Height, Depth, 1, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED,
                            D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, &pVolumeTex))) {
                        pTex = pVolumeTex;
                    }
                    else {
                        m_strErrors += "Could not load volume texture ";
                        m_strErrors += pstrName;
                    }
                }
                else if(strcmpi(pstrTextureType, "cube") == 0) {
                    LPDIRECT3DCUBETEXTURE9 pCubeTex = NULL;
                    if(SUCCEEDED(hr = D3DXCreateCubeTextureFromFileEx(m_pd3dDevice, strPath,
                            Width, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED,
                            D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, &pCubeTex))) {
                        pTex = pCubeTex;
                    }
                    else {
                        m_strErrors += "Could not load cube texture ";
                        m_strErrors += pstrName;
                    }
                }
            }
            else {
                LPDIRECT3DTEXTURE9 p2DTex = NULL;
                if(SUCCEEDED(hr = D3DXCreateTextureFromFileEx(m_pd3dDevice, strPath,
                        Width, Height, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED,
                        D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, &p2DTex))) {
                    pTex = p2DTex;
                }
                else {
                    m_strErrors += "Could not load texture ";
                    m_strErrors += pstrName;
                    m_strErrors += "\n";
                }
            }

            // Apply successfully loaded texture to effect
            if(SUCCEEDED(hr) && pTex != NULL) {
                m_pEffect->SetTexture(m_pEffect->GetParameter(NULL, iParam), pTex);
                SAFE_RELEASE(pTex);
            }
        }
	else */
	if(pstrFunction != NULL) {
	    LPD3DXBUFFER pTextureShader = NULL;
	    LPD3DXBUFFER lpErrors = 0;

	    if(pstrTarget == NULL || strcmp(pstrTarget,"tx_1_1"))
                pstrTarget = "tx_1_0";

	    if(SUCCEEDED(hr = lpEffectCompiler->CompileShader(
				pstrFunction, pstrTarget,
				0, &pTextureShader, &lpErrors, NULL))) {
		SAFE_RELEASE(lpErrors);

		if((UINT)Width == D3DX_DEFAULT)
                    Width = 64;
		if((UINT)Height == D3DX_DEFAULT)
                    Height = 64;
		if((UINT)Depth == D3DX_DEFAULT)
                    Depth = 64;

#if D3DX_SDK_VERSION >= 22
		LPD3DXTEXTURESHADER ppTextureShader;
		D3DXCreateTextureShader((DWORD *)pTextureShader->GetBufferPointer(), &ppTextureShader);
#endif

		if(pstrTextureType != NULL) {
                    if(strcmpi(pstrTextureType, "volume") == 0) {
                        LPDIRECT3DVOLUMETEXTURE9 pVolumeTex = NULL;
                        if(SUCCEEDED(hr = D3DXCreateVolumeTexture(m_pd3dDevice,
                                Width, Height, Depth, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &pVolumeTex))) {
#if D3DX_SDK_VERSION >= 22
                           if(SUCCEEDED(hr = D3DXFillVolumeTextureTX(pVolumeTex, ppTextureShader))) {
#else
                           if(SUCCEEDED(hr = D3DXFillVolumeTextureTX(pVolumeTex,
					(DWORD *)pTextureShader->GetBufferPointer(), NULL, 0))) {
#endif
                                pTex = pVolumeTex;
                            }
                        }
                    } else if(strcmpi(pstrTextureType, "cube") == 0) {
                        LPDIRECT3DCUBETEXTURE9 pCubeTex = NULL;
                        if(SUCCEEDED(hr = D3DXCreateCubeTexture(m_pd3dDevice,
                                Width, D3DX_DEFAULT, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &pCubeTex))) {
#if D3DX_SDK_VERSION >= 22
                            if(SUCCEEDED(hr = D3DXFillCubeTextureTX(pCubeTex, ppTextureShader))) {
#else
                            if(SUCCEEDED(hr = D3DXFillCubeTextureTX(pCubeTex,
                                (DWORD *)pTextureShader->GetBufferPointer(), NULL, 0))) {
#endif
                                pTex = pCubeTex;
                            }
                        }
                    }
		} else {
                    LPDIRECT3DTEXTURE9 p2DTex = NULL;
                    if(SUCCEEDED(hr = D3DXCreateTexture(m_pd3dDevice, Width, Height,
                                D3DX_DEFAULT, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &p2DTex))) {
#if D3DX_SDK_VERSION >= 22
                        if(SUCCEEDED(hr = D3DXFillTextureTX(p2DTex, ppTextureShader))) {
#else
                        if(SUCCEEDED(hr = D3DXFillTextureTX(p2DTex,
				(DWORD *)pTextureShader->GetBufferPointer(), NULL, 0))) {
#endif
                            pTex = p2DTex;
                        }
                    }
		}
		m_pEffect->SetTexture(m_pEffect->GetParameter(NULL, iParam), pTex);
		SAFE_RELEASE(pTex);
		SAFE_RELEASE(pTextureShader);
#if D3DX_SDK_VERSION >= 22
		SAFE_RELEASE(ppTextureShader);
#endif
	    } else {
		if(lpErrors) {
		    m_strErrors += (char*) lpErrors->GetBufferPointer();
		}

		m_strErrors += "Could not compile texture shader ";
		m_strErrors += pstrFunction;

		SAFE_RELEASE(lpErrors);
		return hr;
	    }
        }
    }

    return S_OK;
}