Sign in to follow this  
mafiesto4

Shader Permutations

Recommended Posts

mafiesto4    640

Hey,

I wonder what is the best way to handle multiple shader permutations? I wrote a blog post http://flaxengine.com/blog/flax-facts-4-shaders-parser/ where I described how we do it in Flax Engine.

The thing is we want to avoid having too many shader versions and to try compile shaders offline (reduce amount of compilations at runtime). It's a little bit manual kind of job because you have to write all possible sets of permutating macros but it has some advantages, eg. it's quite deterministic and you can provide different amount of macros per permutation:

META_PS(false)
META_PERMUTATION_2(NO_SPECULAR=1, USE_IES_PROFILE=0)
META_PERMUTATION_4(NO_SPECULAR=0, USE_IES_PROFILE=1, SKIP_STH=1, BUMP_COLORS=4)
META_PERMUTATION_3(NO_SPECULAR=1, USE_IES_PROFILE=1, BUMP_COLOR=2)
void PS_Spot(Model_VS2PS input, out float4 output : SV_Target0)
{ 

    ...

    GBuffer gBuffer = SampleGBuffer(uv);
    output = GetLighting(Light, gBuffer, shadow, true, true);

#ifdef BUMP_COLORS
    output *= BUMP_COLORS;
#endif

#if USE_IES_PROFILE
    output *= ComputeLightProfileMultiplier(IESTexture, gBuffer.WorldPos, Light.LightPos, -Light.LightDir);
#endif
} 

Plus we don't have to store used macros after shader compilation but just access permutations by index so it's faster to pick a proper shader version during rendering.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this