Monday, September 20, 2010

Dynamically Reloaded Shaders

I first saw this technique from a brilliant engineer I worked with at LucasArts, Dominic Hamon.

Shaders are small programs that describe the lighting calculations for a surface. Due to their small size and the diverse array of potential graphics hardware, shaders are usually compiled and targeted at run-time, rather than statically. Most software I've seen takes little advantage of that fact.

Because shaders often have to be tuned to get exactly the right visual effect, many small changes occur during development. In addition, since step-through debugging and inspection of variable values usually isn't possible, one of the easiest ways to debug shaders is to visualize their calculations by outputting temporary values as surface color. Both of these lead to small frequent changes in shader files.

Since most software I have used or developed simply loads the shader on launch, this means that each small change to a shader requires an application restart to refresh. However, because the shaders can be loaded and compiled dynamically, there is no reason that changes can't be detected and incorporated while the application is running.

My implementation of this uses Windows file change notifications to track source files. When a shader is created, all references to it are tracked, and its source files are added to a registry of tracked files. If a change notification is received for a tracked file, we reload it, recompile all shaders depending on it, and replace all references to the previous version with references to the new.

My next plans for this include polishing the Windows interface (the guts are solid), implementing Mac and Linux version, and using this as a framework for the shader assignments in CIS 665, a class I help teach on GPUs. I feel like this could substantially increase student productivity.

No comments:

Post a Comment