HLSL branching help [RESOLVED]

Started by
3 comments, last by stanlo 16 years, 11 months ago
I'm having trouble with my pixel shader unnecessarily compiling extra assembly instructions. An example of what I'm trying to do: I'd like one technique that uses a texture and one that doesn't, and have a "useTexture" boolean parameter. I would have on pixel shader function that's specified in two different techniques with different parameters. Inside the pixel shader is an if statement that's something like:

if (useTexture)
 color *= tex2D(sampler, texCoord);
and the technique declaration looks like:

PixelShader = compile ps_2_0 Foo(true/false);
Now maybe I don't understand shader compilation very well, but it seems that it should be able to predict away the if statement. If useTexture is a constant it works fine, but if it's a uniform parameter it becomes needlessly longer. It's defined in the technique so it won't ever change right? I tested this in NVShaderPerf. I couldn't figure out how to make fxc.exe let me specify the parameters I was passing into the function. I know uniform sounds like "constant for a draw call" rather than "constant forever", but there should be a way to do this easily right? I want to specify how a pixel shader function works in a technique, and I want it to be fast. edit: Ok, I guess it is optimized out like I thought it should be. I was worried that I was compiling conditionals in since I wasn't looking at the right assembly. I should have passed /T:fx_#_# instead of /T:ps_#_#. I sure feel silly now! Sorry for the waste of a thread. [Edited by - stanlo on April 24, 2007 6:02:29 PM]
Advertisement
I don't know why it'd do that. Check the BasicHLSL sample in the SDK. It uses parameters in the way you want (if I understand correctly), and IIRC it works the way you want. See if you're doing anything different.
If you take RenderScenePS in the sample it compiles to six instructions, but it should be able to compile to only one or two depending on useTexture. Does it actually compile to the correct number (one or two), or does it do six despite the parameters always being the same for a technique?
Edited into OP

[Edited by - stanlo on April 24, 2007 7:55:54 PM]
double post

[Edited by - stanlo on April 24, 2007 7:56:36 PM]

This topic is closed to new replies.

Advertisement