Jump to content
  • Advertisement
Sign in to follow this  
lonewolff

Basic shader efficiency question

This topic is 983 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

Hi Guys,

 

If you have two shaders that are not too dissimilar in operation, but are different enough to warrant separate shaders, would it be more efficient to switch between the shaders in the draw loop to do their thing or would it be faster in operation to have a single multi-purpose shader and update a constant buffer with a variable to switch what the shader does? Thus saving a change in state in the render loop?

 

Love to hear your thoughts on this :)

Share this post


Link to post
Share on other sites
Advertisement
Consider writing a single shader file, but have #if to control enabling or disabling a feature in the shader, you then compile the shader multiple times with the different features enabled or disabled. That way you can reduce duplicate code, which is problem behind having multiple shaders, but you can have efficient shaders that don't do any extra work at runtime.

That is what the makers of infinity blade did, they called their shader the ubershader. It is mentioned just past halfway down the page on this article.
https://de45xmedrsdbp.cloudfront.net/Resources/files/Smedberg_Niklas_Bringing_AAA_Graphics-v2-1449610220.pdf

Share this post


Link to post
Share on other sites
If you compile different permutations of one big "uber" shader, you'll still have to switch between the permutations (I believe). If the differences are really small and you don't want to switch, I'd go for a form of branching, using a extern variable like you suggested, and use 1 shader. You then only have to update the shader constant and "commit" the change, between the 2 lists of objects you're rendering with the shader.

Share this post


Link to post
Share on other sites

If you compile different permutations of one big "uber" shader, you'll still have to switch between the permutations (I believe). If the differences are really small and you don't want to switch, I'd go for a form of branching, using a extern variable like you suggested, and use 1 shader. You then only have to update the shader constant and "commit" the change, between the 2 lists of objects you're rendering with the shader.

But if you have to make separate draw calls anyway then switching shaders (sort the objects by shader to reduce switching) is typically faster. We can only use words like “typical” because it still depends on the scene, but in general it should be faster.


L. Spiro

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!