PDA

View Full Version : Pixel shader constants, shader 3.0


Spudman
04-05-2006, 04:01 AM
I load a pixel shader with 'SetPixelShader' in DX9. This shader contains
several conditionals... eg....

if_le v0.x, c_TestConstant.z

else

endif

Since the constant is defined when the shader is loaded, is this 'compiled out'
or does the compare still occur at each pixel ( and therefore still take cycles...) ???

The reason I need to do this is that I want to roll a set of shaders into a
single uber-shader.

Thanks..

Axel
04-05-2006, 06:13 AM
The driver may create multiple shader in this case and set them accordingly to the constant states, but there will be a point where the driver decides that its more expensive to do the caching of the shaders than actually execute the branch on hardware.

But I wouldn't worry about that all that much, a static branch is pretty cheap on all GPUs that support it :)

jofferman
04-05-2006, 07:46 AM
But I wouldn't worry about that all that much, a static branch is pretty cheap on all GPUs that support it

The problem is that Spudman's example is NOT a static branch.
(static branches are evaluated at compile time)

Dynamic branches are pretty bad for performance on nVidia hardware, specially if you have lots of branches (like we did). ATi (ps3.0) hardware, on the other hand, seems to handle dynamic branching fairly well. Still, dynamic branching is by no means free - so avoid it whenever you can.

Spudman
04-05-2006, 02:14 PM
Ooopss... bad example this, it isn't a static branch, what I really meant was
to give a static branch, in this case can I assume that the compiler will
try to produce the variants at compile time ? - I have the situation of 16
pixel shaders, all slightly different, which are switched between quite rapidly and I want to group them all in one shader, and switch between them on a
shader constant.

Axel
04-05-2006, 02:59 PM
The problem is that Spudman's example is NOT a static branch.
I'm not familiar with ASM-language shaders. His post sounded like its a static branch.

(static branches are evaluated at compile time)
Thats not true. The hardware must also have instructions for static branches, because you can end up with pretty much permutations if you have 10 branches or something like that in your shader ;)

Dynamic branches are pretty bad for performance on nVidia hardware, specially if you have lots of branches (like we did). ATi (ps3.0) hardware, on the other hand, seems to handle dynamic branching fairly well. Still, dynamic branching is by no means free - so avoid it whenever you can.
Yeah, I know.

I have the situation of 16 pixel shaders, all slightly different, which are switched between quite rapidly and I want to group them all in one shader, and switch between them on a shader constant.
It's hard to tell. I would try out whats faster, but I don't think the driver will not cache 16 permutations. But then you should render all permutations that you will possibly use in your scene before rendering the first frame once to warm up the caches.

jofferman
04-06-2006, 01:54 AM
Thats not true. The hardware must also have instructions for static branches, because you can end up with pretty much permutations if you have 10 branches or something like that in your shader

No, the hardware doesn't have instructions for static branching. However, fxc will evaluate static branches during shader compilation. If you want fxc to generate a shader for each of permutation of your static branches, you'll have to create a technique for each of those permutations - which can be very tedious.

We have an inhouse tool that automatically generates all permutations of every shader we use, since there are so many permutations. Our latest game (Tomb Raider: Legend) has, in total, some 20,000 shader permutations!

Jim.