Using Spherical Harmonics for IBL Probes
This is a follow-up on the progress of my previous post on Spherical Harmonics. So I was working on my own spherical harmonics maths library even though there are numerous open source libraries out there for the same, including DirectXMaths which I am already using in my engine for SSE & Vector maths. The main objective of that task was the learning and it was indeed a good learning exercise.However, for SH based IBL probes in my engine, I used the DirectX maths library functions only because they are to the point, faster and SSE vector maths.
Spherical Harmonics are a nice way to approximate functions over a unit sphere and can be used to approximate lighting in a scene. Depending on the frequency of lighting detail we need we decide what order of SH we need to use, which in turn decides the number of coefficients needed.
I used 3rd order SH with 9 coefficients to approximate both local and global diffuse probes. And you can see the results below:
Cubemap based Diffuse probe 128x128x6, SH Probe using 9 coefficients and Image diffs are shown side by side.
So we can see there are differences for sure but do they matter? That depends on actual game environment & scenes. Even for above scene even though image diffs showed a big difference when I turned on proper material values results with SH probes looked were good enough.
If we do the math 1 cube map takes approx ~1 MB memory even more if we use 256 x 256 resolution but SH order 3 takes only 7 color values (RGBA). That’s one hell of a compression. This also enables us to spam SH probes all over the maps or scenes to use it as a sort of real-time GI solution.
Right now I have kept both the implementations in the engine and I can easily switch between them or do image diffs for any light probe.
(I wanted to do a detailed semi-tutorial post for Spherical Harmonics too but I don’t have much free time nowadays and I am FINALLY working on real-time GI in the engine which is something I wanted to do since forever & just can’t wait to get first stable version up and running. If I get free time in near future I will come back to this and do that post to gather all the SH related info we need for gfx in one single post.).