Sign in to follow this  

Shader light permutation

Recommended Posts

I'm starting to implement an automatic shader permutation system (for a forward renderer) and I'd like some advice for implementing multiple lights on the shader side.

Most other things are simple, if a texture is used get the color from the texture, etc.

So far I'm thinking something like this:
Pixel and vertex shaders have 4 "slots" of 3 registers each, each slot can contain the information for 1 light.
The shaders can support a maximum of 4 lights of any combination.
This results in 35 unique permutations.
The bitfield that represents a shader permutation has 6 bits to represent which permutation to use.
The pixel shader itself would look like this (pseudocode)
float4 color = float4(0,0,0,0)

if defined light1point
color += calculatePointLight(register0) // position
else if defined light1dir
color + = calculateDirLight(register1) // direction
else if defined light1spot
color += calculateSpotLight(register0, register1, register2) // position, direction, misc info (cone angle, etc)

if defined light2point
color += calculatePointLight(register3) // position for second light
else if defined light4spot
color += calculateSpotLight(register9, register10, register11)

Is this a decent way to create a permutable shader?

Share this post

Link to post
Share on other sites
What you're doing definitely works, and produces shaders with the best performance. Of course you should always be aware of how many permutations you need to compile, and how long it's going to take you to iterate on them. 36 can be a lot when you have something like 4-8 other features you need to permute on.

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