Optimizing Point Lights

Even though its’ a very old topic, I thought I’d throw in a few musings as I have some experience on the subject.

The reason why having many lights is hard on your GPU - you end up looping over each light for each pixel on the screen.

Even if your light has no contribution, that is - the pixel being shaded is too far away from the light - it will still do the computations.

With the branching early exit that @DolphinIQ has suggested - you will potentially skip all of those extra computations. However, you’ll pay the branching cost, which traditionally has been quite high for GPUs. It’s much better now, at least on desktop. I don’t know about mobile though.

So, no matter how well you do with your early exists, you will still have to at least read some data about the light, do some computations and only then have enough info to decide whether to exit or not.

If you have 1000 lights which will be optimized via early exit, that’s still 1000 distance calculations and 1000 conditionals as a minimum.

The Forward+ that was mentioned, or more accurately I supposed “clustered” or “tiled” rendering, breaks the whole viewport (the picture to be rendered) into small rectangular regions, and then stores small amount of data for each region. This data will be your pre-computed list of lights that actually affect pixels in that region. For any given scene - you might have 1000 lights, but it’s pretty rare that any given pixel will receive light from more than 2-3 lights at the same time.

The technique allows us to consider a very practical number of lights for each pixel now, at the cost of some pre-computation. Problem with clustered rendering and JS, JS is slow, and the technique gets better and better as you split your viewport into more pieces. That’s a lot of computations. You can do those computation on GPU, but without compute shaders it’s reeeealy painful. In C++ you can take advantage of compiler optimizations and threading, but in JS - no such luck. And please don’t get me started on WebWorkers - they are nice, but they are not anywhere nearly as powerful as native threads.

4 Likes