• Advertisement
Sign in to follow this  

Multiple small shaders or larger shader with conditionals?

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

Hey all,

 

I was wondering if it was better to have my multiple smaller shaders, for example a shader for tiles without lighting, one for self-illumination, another for normal mapping and self-illumination, etc, or if it was fine to hale a larger one using IF/ELSE for different functionality.

 

I read that conditionals in shaders should be minimized due to their cost, but I wonder if it is better to hahe the IF/ELSE on the CPU side or if a few IFs will keep the shaders fast enough.

 

I'm only making a 2D tiled game.

 

Thanks for any input!

Share this post


Link to post
Share on other sites
Advertisement

You can use #ifdef, #else, #elif, #endif, #define for that purpose, which will output different shaders depending on the defines you supply - think that would be better than conditionals :D

 

Like:

#ifdef NORMALMAPPING
.. do normalmapping stuff here
#endif

.. So you just write all combinations into one shader, and compile it with different defines to get all the combinations :)

Share this post


Link to post
Share on other sites

1) If your GPU is not your performance bottleneck, it may not matter what you do.

 


I wonder if it is better to hahe the IF/ELSE on the CPU side or if a few IFs will keep the shaders fast enough.

 

2) Consider that conditionals in a pixel shader will be executed possibly millions of times per frame, compared to a handful of times on the CPU to switch shaders.*

 

3) If you end up using different shaders (recommended (the way vinterberg described is a good way to approach it)), then make sure you group your draw calls by shader so you're not switching shaders all the time (of course taking into account any draw order required for proper z-order rendering).

 

 

*if you're using effects on DX9, pre-shaders could help alleviate some of the performance cost of conditionals in shaders, if you're branching on shader constants.

Share this post


Link to post
Share on other sites

Any recent desktop gpu will be able to handle whatever you're doing in a tiled 2D scenario. Unless you grind out some ray marching or use a really low end gpu it probably doesn't matter. A tiled game is probably going to have close to perfect overdraw right? 

 

Using a single shader to do everything is likely going to be easiest to write so I'd do that. If you're branching on constants, you'll be fine.

Share this post


Link to post
Share on other sites

Somebody correct me if I'm wrong, but if every path in a shader takes the same branch it is nearly (not entirely) the same cost as if the changes were compiled in with defines. If you are rendering an object with a specific set of branches that every thread will take it may not be a big deal. If threads take different branches you will eat the cost of all branches.

Edited by DementedCarrot

Share this post


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

  • Advertisement