Jump to content
  • Advertisement
Sign in to follow this  
grill8

One more quick question about DX 9 Shaders.

This topic is 3612 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!