Hello,
I'm currently adding lighting to my game engine, and it's already fine with 1 light (directional, point or spot...).
However, how can I handle multiple lights efficiently?
I know I can write a shader with uniform arrays where each element is a light struct, or multiple arrays or primitives.
Then in a for loop, each light would contribute to the final pixel color.
uniform Light u_Lights[32];
//...
for(int i = 0; i < 32; ++i)
{
outputColor = /* light contribution */
}
However, if I want to support N lights, would I have to write my shaders so they take N lights in those uniform arrays?
If I create 2 lights only, will I have to loop through 32 lights just to avoid branching? Unless branching is OK if N remains constant?
Then, what if N can change at runtime?
Do I have to recompile every shader that uses lights just to reset the number of lights as a compile-time constant?
That's a lot of questions, but they generally refer to the same problem: compile-time VS runtime performance.
What would be the best, general purpose approach?