Skip to main content
GameDev.net gamedev.net
🔒 Locked

HLSL incorrect compilation

Started by stevenmarky Aug 6, 2007 at 5:43 AM 1 replies 1.9k views
Original Post
stevenmarky
stevenmarky
I'm trying to write a HLSL pixel shader which simply outputs the texture set at stage 1 (set via IDirect3DDevice9::SetTexture(1, texture). The source is as follows:

texture texture0; // texture
texture texture1; // normal map

sampler2D texSampler0 : TEXUNIT0 = sampler_state
{
    Texture = <texture0>;
    MIPFILTER = LINEAR;
    MAGFILTER = LINEAR;
    MINFILTER = LINEAR;
};

sampler2D texSampler1 : TEXUNIT1 = sampler_state
{
   Texture = <texture1>;
   MIPFILTER = LINEAR;
   MAGFILTER = LINEAR;
   MINFILTER = LINEAR;
};

struct PS_INPUT
{
    float4 Position   : POSITION;
    float3 Texture : TEXCOORD0;  
};

struct PS_OUTPUT
{
    float4 color : COLOR0;  
};

PS_OUTPUT main( in PS_INPUT In )
{
    PS_OUTPUT Out;
    Out.color = tex2D(texSampler1, In.Texture);
    return Out;                    
}




The problem is, it outputs the texture at stage 0. I did some investigation and compiled the HLSL to assembly code, this is what it produces:

    ps_2_0
    dcl t0.xy
    dcl_2d s0
    texld r0, t0, s0
    mov oC0, r0




Whilst what I actually want is:

    ps_2_0
    dcl t0.xy 
    dcl_2d s1
    texld r0, t0, s1
    mov oC0, r0 




I fiddled around for a while and realised if I used both the textures in the pixel shader, I could access the texture on stage 1; for example if I do:

    Out.color = tex2D(texSampler1, In.Texture) + tex2D(texSampler0, In.Texture) * 0.1;
Then the output is pretty much the second texture. Obviously I don't want to have to do this to my HLSL each time I want to access a particular texture. Is something wrong with my HLSL? Why does it always output the first texture unless I use both textures? Any help appreciated.
Schrompf
Schrompf
Nothing wrong here. You're just relying on an undefined practise here. The HLSL compiler does not explicitely specify a rule for how textures are assigned to samplers. It's just you who assumes that the textures are assigned to samplers in the sequence they're declared starting from zero.

In your present shader, Texture 0 is not used anywhere and thus removed by the optimizer. Texture1 is then the only one and consequently assigned the sampler 0, despite its name. You might solve this issue by explicitly specifying a sampler register using "register( s0)" - the documentation knows the exact syntax. The second option (and the better one in my opinion) is to query the ConstantTable for the sampler register the textures were assigned to, and use that index to bind a texture from the code.

[edit] Strange syntax. Have never seen a "texture" declaration in HLSL code. Maybe this is DX10 and you already specified a sampler register using that TEXUNIT1 macro. If yes, I have no other idea what could be the problem.

Bye, Thomas
----------
Gonna try that "Indie" stuff I keep hearing about. Let's start with Splatter.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.