directxhlsldirectx-9

TEXCOORD0 in my HLSL shader always points to (0,0)


i'm working on directx9 and i'm trying to get my hand on shaders, here is my shader:

sampler texLastPass : register( s0 );

float4 main(in float2 t : TEXCOORD0) : COLOR
{
    float4 color = tex2D(texLastPass, t);
    return float4(color.rgb, 1.0f);
}

In theory there should be no change with it, but instead my whole objects is filled with pixel that exists on position (0, 0) in texture.

So why provided pixel coordinates to shader are wrong?


Solution

  • Your shader code has a sampler, but where is the texture? From Microsoft Docs:

    Three things are required to sample a texture:
    
    * A texture
    * A sampler (with sampler state)
    * A sampling instruction
    

    Since you are missing the binding to the texture, Direct3D 9 is going to provide you a value of 0,0,0 for all texture references.

    texture tex0 : register( t0 );
    

    Without this it still builds because it implicitly assumes you mean t0.

    You are using 'explicit binding' in this shader, which is perfectly fine and recommended if you are avoiding the use of the legacy Effects system. That said, Direct3D 9 developer education materials and samples were heavily based on Effects which uses a slightly different syntax, and therefore a source of confusion.

    You don't show your C/C++ code, so it could be related to your binding code as well.

    Note that if writing Direct3D 9 code these days, a few important things to keep in mind:

    You should take a look at DirectX 11 as a better starting point which has much better debugging support, tools support, and open source libraries. See this blog post.