HLSL beginner again! Wont compile :(

Started by
6 comments, last by jollyjeffers 18 years, 1 month ago
Hi, After momentary success with my first effect file, I got all cocky and tried to modify it. I've broken it :( I'm not really trying to do anything special, just sample 2 textures and blend em. Heres the fx file:
// BLENDFX.fx  - mixes two textures 50% each

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


// Light intensity
float4 I_a = { 0.0f, 0.0f, 0.0f, 1.0f };    // ambient
float4 I_d = { 1.0f, 1.0f, 1.0f, 1.0f };    // diffuse


// 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

// Texture
texture Tex0;
texture Tex1;

// Transformations
float4x4 WorldView  : WORLD;
float4x4 Projection : PROJECTION;

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

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

    float3 L = -LightDir;

    float3 P = mul(float4(Pos, 1), (float4x3)WorldView);  // position (view space)
    float3 N = normalize(mul(Norm, (float3x3)WorldView)); // normal (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.Tex0 = Tex0;                                       
    Out.Tex1 = Tex1;                                       

    return Out;
}

sampler Sampler0 = sampler_state
{
    Texture   = (Tex0);
    MipFilter = LINEAR;
    MinFilter = LINEAR;
    MagFilter = LINEAR;
};
sampler Sampler1 = sampler_state
{
    Texture   = (Tex1);
    MipFilter = LINEAR;
    MinFilter = LINEAR;
    MagFilter = LINEAR;
};

float4 PS(
    float4 Diff : COLOR0,
    float2 Tex0 : TEXCOORD0,
    float2 Tex1 : TEXCOORD1 ) : COLOR
{
	float4 color0 = tex2D(Sampler0, Tex0) * 0.5;
	float4 color1 = tex2D(Sampler1, Tex1) * 0.5;
	float4 OutColor = (color0 + color1 ) * diff;
 
    return OutColor;
}


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

If I put return color1; or return color2; in the pixel shader, it works and I can see the each texture in turn. Whats wrong with my simple averaging calc? <sigh> Thanks! Simon
Advertisement
I'm not a human compiler, what's the error?!
Hehe,

well, thats a very good point. What error? I just get E_FAIL from this function:

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

How do I debug shaders? I have shader debugging turned on in my dx control panel, dunno what that does tho! And who stole EffectEdit? I could use that now..... anywhere I can download it from?
You could use the fxc.exe utility (included with the SDK) to compile shaders on the command line to check for syntax.

You could also just look at the debug spew when you call D3DXCreateEffectFromFile. The output in the debug spew will basically be the same output the fxc utility produces. There is a section in the SDK docs about debugging shaders.

neneboricua
If you read teh docs you will find that last parameter of that call is a buffer that the compiler whacks the compile errors in.

Dave
Wow! it works!

I was missing the fact that you need to debug shaders using the REF device. Snoring! zzzzz



Dave - What do I do with that buffer to view the errors otherwise?

Si
May I suggest you download FXComposer (developer.nvidia.com). It provide a lot of analisys tools and will help you testing your shaders in a nice environment.

I started once writing an article about using the FX Framework, using FFP inside a shader and integrating FX with your engine. I proposed it to Gamedev but never got a response. As the subject is long I proposed it as a book to a publisher but was declined. Now I'm thinking about finishing the book and sell it as an eBook so maybe someone may find it useful.

Luck!
Guimo


Quote:Original post by sipickles
What do I do with that buffer to view the errors otherwise?
I use something along the lines of:
LPD3DXBUFFER errors = NULL;if( FAILED( /* compile shader */ ) ){    OutputDebugString( L"--- BEGIN ERRORS ---\n" );    char* errors = reinterpret_cast< char* >( errors->GetBufferPointer() );    OutputDebugStringA( errors );    OutputDebugString( L"--- END ERRORS ---\n" );}SAFE_RELEASE( errors );


However, the best way is (if you're using VStudio) to add a "custom build step" that compiles your shaders as part of the build process. Then it'll stop you running your program if the shader is fubar'd. Not only that, but it'll integrate it with other compiler errors so if you double-click it'll open the offending shader and highlight the dodgy line [grin]

Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

This topic is closed to new replies.

Advertisement