Jump to content
  • Advertisement
Sign in to follow this  
Harry Hunt

Modular shader

This topic is 4235 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! I have written a modular shader in HLSL and using SM 2.0. What makes it modular is that the individual elements that make up the shader are controlled via boolean constants such as HasNormalMap, HasDetailMap, etc. Currently I have a couple of those kinds of statements in my HLSL code:
if (g_bHasNormalMap) 
{
  // do something
} 
else
{
  // do something else
}
Now from what I know, SM 2.0 does not support dynamic branching and when I compile my shaders, the resulting asm lacks any kind of branching instructions. So my question is, what would be better: a) Keep using ifs even if that means more shader code is executed than actually necessary but only have one shader loaded for all surface types OR b) Use preprocessor directives instead and have one unique shader per surface type that only does the computations that are really necessary Thanks a lot in advance!

Share this post


Link to post
Share on other sites
Advertisement
I would personally go with the compile-time branching over a more complex shader, but ultimately you'll need to get some hard profiling statistics to really make an informed decision.

The problem with compile-time is the potential for combinatorial explosion - but this can be detected fairly easily in a profiling tool (Nvidia/ATI have some and there is always PIX for Windows) and if state-management appears high in your profiler then there are quite a lot of algorithmic things you can do to reduce it. Optimal state transitions, lazy state changes, cached states, aggressive culling etc...etc...

If you implement it that way and still find that a few shaders are causing you grief then you could fairly easily reimplement your shader with dynamic/run-time (emulated) branching. Whilst it might make for fugly code, you can always set up a compile-time branch for if( UseDynamicBranching ){ /**/ } else { /**/ } and it should just drop into your existing framework [grin]

hth
Jack

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!