HLSL - beginner needs help with effects.

Started by
2 comments, last by sipickles 18 years, 1 month ago
Hello. I'm trying to use effects for the first time, after some reasonable success with vertex and pixel shaders outside effects. When I switch to shaders, my terrain appears drawn and lit correctly, but untextured. It is correctly textured in the Fixed pipeline I have set my active texture to sampler 0 i think..... what am I missing? Also for some reason, the rendering REALLY slows using the effect. I didnt have this trouble with shaders!

HRESULT cTerrain::CreateShaders( void )
{
HRESULT hr;

    LPD3DXBUFFER pShader = NULL;

    hr = D3DXCreateEffectFromFile(
        g_device,
		L"data\\shaders\\simpleFX.fx",
        NULL, // NULL terminated list of D3DXMACRos
        NULL, // #include handler
        D3DXSHADER_DEBUG,
        NULL, // memory pool
        &m_effect,
        NULL);

    if(FAILED(hr))
    {
		SAFE_RELEASE(m_effect);
		return hr;
    }

	D3DXHANDLE hTech = m_effect->GetTechniqueByName("tVertexAndPixelShader");
    hr = m_effect->ValidateTechnique(hTech);
    
	if(FAILED(hr))
    {
        return hr;
    }

	return S_OK;
}
bool cTerrain::Render()
{
	D3DXMATRIX I;
	D3DXMatrixIdentity(&I);
	g_device->SetTexture(0, m_backgroundTexture );
//	g_device->SetTexture(1, m_detailTexture );

	HRESULT hr;	
		
	if( m_effect && g_useShaders )
	{
		m_effect->SetMatrix("World", &I); 
		m_effect->SetMatrix("View", g_camera->GetViewMatrixPtr() ); 
		m_effect->SetMatrix("Projection", g_simulation->GetProjMatrixPtr() ); 

		m_effect->SetTechnique("tVertexAndPixelShader"); 
		
		
		UINT numPasses;//, iPass;
		hr = m_effect->Begin( &numPasses, 0 );
	//	for( iPass = 0; iPass < numPasses; iPass ++ )

		hr = m_effect->BeginPass( 0 );
		for ( unsigned int i = 0; i < m_visibleLeafList.size(); i++)
			m_visibleLeafList.at( i )->Render();

		m_effect->EndPass();
		m_effect->End();
			  
	}
	else
	{
		g_device->SetTransform( D3DTS_WORLD, &I );
		for ( unsigned int i = 0; i < m_visibleLeafList.size(); i++)
			m_visibleLeafList.at( i )->Render();
	}


	return true;
}

Heres the shader... from a SDK sample.


// Light direction (view space)
static float3 LightDir < string UIDirectional = "Light Direction"; > = 
    normalize(float3(-1,0,1)); 



// Light intensity
float4 I_a = { 0.1f, 0.1f, 0.1f, 1.0f };    // ambient
float4 I_d = { 1.0f, 1.0f, 1.0f, 1.0f };    // diffuse
//float4 I_s = { 1.0f, 1.0f, 1.0f, 1.0f };    // specular

// Material reflectivity
float4 k_a : MATERIALAMBIENT = { 1.0f, 1.0f, 1.0f, 1.0f };    // ambient
float4 k_d : MATERIALDIFFUSE = { 1.0f, 1.0f, 1.0f, 1.0f };    // diffuse
//float4 k_s : MATERIALSPECULAR= { 1.0f, 1.0f, 1.0f, 1.0f };    // specular
//int    n   : MATERIALPOWER = 32;                            // power

// Texture
texture Tex0 < string name = "G:\Game Programming\C\_Dev\Client 20060117\terrain\myTerrain\data\texture.tga"; >;

// Transformations
float4x4 World      : WORLD;
float4x4 View       : VIEW;
float4x4 Projection : PROJECTION;

struct VS_OUTPUT
{
    float4 Pos  : POSITION;
    float4 Diff : COLOR0;
    //float4 Spec : COLOR1;
    float2 Tex  : TEXCOORD0;
};

VS_OUTPUT VS(
    float3 Pos  : POSITION, 
    float3 Norm : NORMAL, 
    float2 Tex  : TEXCOORD0)
{
    VS_OUTPUT Out = (VS_OUTPUT)0;

    float3 L = -LightDir;

    float4x4 WorldView = mul(World, View);

    float3 P = mul(float4(Pos, 1), (float4x3)WorldView);  // position (view space)
    float3 N = normalize(mul(Norm, (float3x3)WorldView)); // normal (view space)

  //  float3 R = normalize(2 * dot(N, L) * N - L);          // reflection vector (view space)
 //   float3 V = -normalize(P);                             // view direction (view space)

    Out.Pos  = mul(float4(P, 1), Projection);             // position (projected)
    Out.Diff = I_a * k_a + I_d * k_d * max(0, dot(N, L)); // diffuse + ambient
    //Out.Spec = I_s * k_s * pow(max(0, dot(R, V)), n/4);   // specular
    Out.Tex  = Tex;                                       

    return Out;
}

sampler Sampler = sampler_state
{
    Texture   = (Tex0);
    MipFilter = LINEAR;
    MinFilter = LINEAR;
    MagFilter = LINEAR;
};


float4 PS(
    float4 Diff : COLOR0,
    //float4 Spec : COLOR1,
    float2 Tex  : TEXCOORD0) : COLOR
{
    return tex2D(Sampler, Tex) * Diff;// + Spec;
}


technique tVertexAndPixelShader
{
    pass P0
    {
		CullMode     = None;
        
        // Shaders
        VertexShader = compile vs_1_1 VS();
        PixelShader  = compile ps_1_1 PS();
    }  
}

Thanks for your advice Si
Advertisement
This line: g_device->SetTexture(0, m_backgroundTexture );

should actually be: m_effect->SetTexture("Tex0", m_backgroundTexture);

You need to assign the texture using the effect interface, and not the device interface.

Theres also an option to call the device call g_device->SetTexture(0, m_backgroundTexture ); AFTER the call the m_effect->BeginPass(), but that's not very elegant.

Hope this helps :).
Sirob Yes.» - status: Work-O-Rama.
Ok the slow rendering was caused by:

#define DEBUG_VS // Uncomment this line to debug vertex shaders
#define DEBUG_PS // Uncomment this line to debug pixel shaders

which dont provide any help as far as I can find!
Hi Sirob!

Knew I could rely on you!

Thats sorted it.... just a case of info overload for my poor brain i think!

This topic is closed to new replies.

Advertisement