Sunstone dev log 25: automatic exposure
I used compute shaders to calculate an average scene luminance, and fed that to my exposure calculation. Check it out:

It’s very… grey…
I think the automatic exposure is doing a good job at taking the average luminance to around that of middle grey at the expense of the subject.
I’m not sure how to fix this in general. I tried some weighted averages, where samples from the image center count more than samples at the edges. But a weighting that worked well for one scene (e.g. the helmet) didn’t work for another (e.g. bistro). Maybe a histogram-based approach would be better?
What I built
A compute shader that sums log-luminance
The radiance buffer is divided into 64x64 tiles, each of which is processed by a separate workgroup. There is a storage buffer for results with one entry per workgroup. Each workgroup has a shared accumulator that its invocations add to. Once all invocations finish adding their log-luminance to the accumulator, the leader stores this value in the storage buffer.
A compute shader that averages log-luminance
A single shader invocation that loops over the storage buffer from the previous step, accumulating a total, and then divides by the number of pixels.
What I learned
You need to use the geometric mean of luminance to get a reasonable result.
Reinhard et al. (2002) suggest using the geometric mean (with a typo in the equation). They cite Tumblin and Rushmeier (1993), which was the earliest example of this technique I could find. Seems that our perception of brightness is roughly logarithmic with respect to luminance.
Next steps
- Review use of physical units
- Add imgui so I can manually tweak exposure
- Test a tone mapping operator
- Improve autoexposure somehow
References
- Reinhard, E., Stark, M., Shirley, P., & Ferwerda, J. (2002). Photographic tone reproduction for digital images. ACM Transactions on Graphics, 21(3), 267–276. https://doi.org/10.1145/566654.566575
- Tumblin, J., & Rushmeier, H. (1993). Tone reproduction for realistic images. IEEE Computer graphics and Applications, 13(6), 42-48.