I was looking into ways to implement multiple lights shading and I've noticed Unity uses multiple passes to achieve it. It performs a base pass to apply the brightest directional pixel light + 4 vertex lights + SHs and an additional pass for each pixel light, up to a limit defined by the user on the editor.I'm wondering, since I previously know how many pixels lights I need to shade, why not doing it in a single pass like vertex lights are done? What are the problems of doing so?
Multiple Lights Shading - One Pass vs Multiple Passes
lighting;shader
It's completely possible to do all pixel lights in the fragment shader (or, say, do 4 at a time) by looping over an array. However, this comes at a great performance cost: You're going to be calculating lighting for every light on every fragment of the scene, even if that geometry isn't actually affected by the light's influence.I suspect that is reason why Unity doesn't do so: it probably culls the objects and submits only geometry that might be influenced by the respective light on each pass, meaning that shading for a light is only evaluated in the fragments where it may have an effect. This is a trade off between processing less fragments but duplicating the geometry cost.