# How to properly set up XNA shader?

I'm trying to get a basic shader to work in XNA. I pass in a bool value called "TextureEnabled" to indicate whether or not it should expect some UV texture coordinates. But regardless of what I pass, it is always expecting the UV value, and it throws and InvalidOperationException -"TextureCoordinate0 is missing":

VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{

float4 worldPosition = mul(input.Position, World);
float4x4 viewProjection = mul(View, Projection);
output.Position = mul(worldPosition, viewProjection);

if (TextureEnabled == true)
{
output.UV = input.UV; //**// always executes**
}
else
{
output.UV = float2(1, 0);
}

return output;


}

Here's the declaration at the top of the effect file:

float4x4 World;
float4x4 View;
float4x4 Projection;
texture BasicTexture;
bool TextureEnabled = false;


I'm even setting the effect parameter in my XNA Draw() method:

paramName = "TextureEnabled";
effect.Parameters[paramName].SetValue(false);


If I just hard-code output.UV = float2(1, 0) instead of output.UV = input.UV, it does not throw an exception, so I know that has to be the problem. Its almost like the TextureEnabled variable isn't getting set up (or not evaluated), even though I can see SetValue(false) being called in the debugger. What am I doing wrong?

Here's the relevant source files, if needed: http://www.mediafire.com/?g85tc5d9582vs8g

If it's in the vertex shader's input structure the shader needs it in the vertex stream, whether you use it or not. XNA is enforcing that.

You're implementing optional texturing improperly. If you do it this way, you'll need to supply texture coordinates always. Also, branching is still to be avoided.

Instead, have two techniques, referencing two different vertex shaders, with two different input structures. One input structure has texture coordinates, the other does not. Then, choose the correct technique for drawing. You don't need to have a 'texture enabled' constant in your shader at all.

• Hmmm, yeah I am just learning at this point, its not like I'm going to use this code in a project. This if statement is just throwing me off because it seems like it should work. After trying a few more things its clear that TextureEnabled is getting evaluated to "true" even though I'm setting it to false, I just don't understand how... – Mossen Aug 1 '11 at 4:08
• Frankly, I didn't even know boolean types existed in HLSL. – Blecki Aug 1 '11 at 4:37