Can an HLSL pixel shader check if a texture stage is bound?

Started by
4 comments, last by SGreth 18 years, 2 months ago
In short I'm looking to check if a texture is bound to texture stage 0. If it is, sample that register, otherwise, just use the vertex color. I couldn't find any built-in HLSL functions to query if a texture stage was bound or not. (note: this shader doesn't work, it's just pseudo-code.

texture tex0 : Texture1;
sampler2D SpriteSampler = sampler_state
{
	texture = (tex0);
	mipfilter = LINEAR;	
};

float4 PS(VS_OUTPUT In) : COLOR
{		
    float4 color;	
    if(SpriteSampler != NULL)
    {
        color = tex2D(SpriteSampler, In.TexCoord);
    }
    else
    {
        color = In.Color;
    }
        
    return color;
} // end of PS
Thanks, ~S'Greth
"The difference between insanity and genius is measured only by success."~Bruce Feirstein
Advertisement
Testing if the sampler is NULL won't work, but you could get the same result by making two techniques, one using the texture and one using the vertex color, and bind the appropriate technique from the app, which knows if a texture has been bound to that stage or not.
Just to elaborate on jamesw's reply...

The following should allow the application to vary the output based on a single SetTechnique() call (it always sets the texture). In this simple instance it might be a bit overkill, but it is one way of doing things [smile]

texture2D TheTexture;float ps( in VS_OUTPUT v, uniform bool UseTextures ) : COLOR{   //...   if( UseTextures )   {      return tex2D( ... );   }   else   {      return v.Color;   }}technique TexturedVersion{   pass p0   {      Texture[0] = (TheTexture);      PixelShader = compile ps_2_0 ps( true );   }}technique NonTexturedVersion{   pass p0   {      Texture[0] = NULL; //redundant really, but just for clarity      PixelShader = compile ps_2_0 ps( false );   }}


hth
Jack

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

Yeah, this morning I was considering either doing that, or setting a global shader boolean parameter indicating if the sampler should be used. I'll just bind the two technique types as that would be less of a performance hit.

Thanks guys,
~S'Greth
"The difference between insanity and genius is measured only by success."~Bruce Feirstein
Quote:Original post by SGreth
setting a global shader boolean parameter indicating if the sampler should be used.
I'm pretty sure this would actually translate to a case of branching (or emulated branching) inside the shader. In this simple example it probably wouldn't hurt, but in a bigger shader it could be painful.

With the uniform constants via the effects framework you'll actually end up with two different shaders - one compiled for each path through the function. It's effectively a form of dead code elimination.

hth
Jack

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

Yeah, the option of having dead code, or an branch for every vertex...the choice was obvious :)
"The difference between insanity and genius is measured only by success."~Bruce Feirstein

This topic is closed to new replies.

Advertisement