Spellcaster Studios

Make it happen…

Optimization: Shader Cache

The shader system on “Cantrip” has some sophistication to it, and part of it is the fact that it generates the specific necessary shaders from a uber-shader (a big shader full of conditions) and a render environment that trims that uber-shader to smaller ones, depending on need…

The problem with this approach is that everytime there’s a new shader requested (for example, a 3d model with a point light and a spot light on it, with two texture maps and no normals), the system has to generate a shader, which sometimes leads to a small stall (a shader takes some milliseconds to generate)…

So I’ve added a shader cache. I can’t store the generated shaders (well, on Direct3D I could store the byte code, but even that would cause issues, and in OpenGL I don’t even have that possibility), so I store the second-best thing: the generation parameters, so when the game starts, the system will generate all the shaders that might be needed for the game, preventing the gameplay stalls…

This works rather well, and allows for a more fluid gaming experience, and the expense of a slightly larger startup time (some seconds only). Just my initial setup (not all shaders accounted, this list will grow automatically, so I don’t have to see every permutation I’m using) has 110 shaders and takes about 15 seconds…

No screenshot though, this is all done on code! Smile

Now listening to “Shatter Me” by “Lindsey Stirling”

Comment