Jump to content
Site Stability Read more... ×
  • Advertisement
Sign in to follow this  

Shader light permutation

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

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
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!