Shaders optimization
Introduction
Thanks to a night at Encelo’s flat that meant some discussions (several beers…
) and a bit of testing I’ve improved a lot the performance of almost all the shaders!
I’ve continued to work on optimization for another day, and now I’m really satisfied of the results I’ve obtained, in most of cases the improvements are amazing, as you will see in the benchmarks I’m going to publish in the next posts.
In this discussion I’m going to talk about a couple of optimizations for shaders coding.
Optimizing the two phases shaders
As I’ve described in my previous posts, the first three thermal erosion shaders are based on two different phases.
In my first implementation I put the code of both phases in a single shader and the program decided what phase to execute according to a uniform variable “phase” and an if branch.
I’ve discovered this is a bad way of coding, the best way is split the two phases in two different shaders, attach them to two different shader programs and call the appropriate program inside the GL application keeping the logic branch there.
Optimizing the shaders execution
Thanks to the previous optimization I’ve started to attach every single shader to a different shader program, and (re)call the program when needed, i.e. the associated key is pressed.
Before of that I used a single shader program and I attached to it a different shader every time it was needed, this way required a linking at every execution and I suppose it requires several other operations hidden inside the GL/graphic pipeline, taking so much more time to execute the shader.
Some minor optimizations
Some minor suggestions to keep in mind are:
- use GLSL functions also for simple code as the cross product
- avoid type casting and use floats all the times that are required computations (usually GLSL functions requires float and so they built-in variables are)
- use the latest video drivers, updating my NVIDIA video drivers from 100 to 169 version improved thermal erosion shaders by about 10%.







