This post has been published also in Coherent Labs’s blog – the company I co-founded and work for.
A challenge in modern graphics programming is the management of complicated shaders. The huge amount of materials, lights and assorted conditions lead to a combinatorial explosion in shader code-paths.
There are many ways to cope with this problem and a lot of techniques have been developed.
Some engines like Unreal have taken the way lead by 3D modelling applications and allow designers to ‘compose’ shaders from pre-created nodes that they link in shade trees. An extensive description of the technique can be found in the paper “Abstract Shade Trees” by McGuire et al.. This way however the “Material editor” of the application usually has to be some sort of tree editor. Shaders generated this way might have performance issues if the designer didn’t pay attention but of course they are the ones that give major freedom to that said artist.
Another technique is building shaders on-the-fly from C++ code as shown in “Shader Metaprogramming” by McCool et al.. I’ve never tried such a shader definition although I find it very compelling due mostly to it’s technical implementation. You’d have to rebuild and relink C++ code on the fly to allow for interactive iterations when developing or debugging which is not very difficult to achieve but seems a bit awkward to me. The gains in code portability however should not be underestimated.
In the following posts I’ll explain the syntax and what I tried to achieve with it as well as the implementation of the translator.