One more quick question about DX 9 Shaders.

Started by
2 comments, last by grill8 15 years, 4 months ago
Hello, I have one more quick question about DX 9 Shaders. Ok, so say you have a variable A passed to a shader. The shader needs a bunch of additional information dependent solely on A, for example ... A*A, A/3, A*A + 7 etc. These are just examples. Yes, you could calculate them in the shader but they only need to be calculated once per BATCH and not once per vertex/pixel so that is inneficient ... Yes, you could calculate them on the CPU and pass them to the shader but that is sloppy and could quickly lead to confusions and misnamed confusing variables etc.... Neatness is important. So ... How does one do calculations IN A SHADER that are done only once per BATCH and not once per vertex/pixel. The only way I can think of is using an DX effect file and putting the calculations in a technique/pass but it is my understanding that only certain operations are ok in techniques/passes so that wouldn't work anyways. Is this understanding correct? Thank you for your help. Jeremy
Advertisement
DX introduces a concept called a Preshader, to solve exactly this issue. A preshader is executed by the CPU, but simulates the results as they would be calculated on the GPU, and the results are stored in constants. This is done automatically (automagically) at the start of each batch. The code for the preshader is created by the HLSL compiler, which automatically moves calculations that are constant over an entire batch to the preshader.

There's really no work required of you to utilize this optimization. You can use calculated constants freely in your HLSL shaders and their calculation will be automatically optimized into the preshader.

Hope this helps.
Sirob Yes.» - status: Work-O-Rama.
Just want to add that pre shaders are only working if you use the effect framework. If you compile only your shaders the calculations will be executed on the GPU for every vertex/pixel.
Thank you very much. That is very helpful. Just what I need.

Question:
I am just trying to figure out how in depth and advanced the preshader is. Does it calculate and store ANY values that remain constants over the batch ... even the results of your own custom function calls and/or that use built in functions exp/pow etc. as well?

Thank you.
Jeremy


This topic is closed to new replies.

Advertisement