A Shading Language

The above shader contains only a simple model and thus it is not very flexible. It can be generalized by adding more parameters, by making the parameters into tuples, by making them recursive structures, and so on. The generalization process eventually leads to a shader that includes a small interpreter that implements a shading language. The core of Pixar's RenderMan(tm) system is exactly this [RenderMan]. Here is a program written in `the' shading language---a shader---that produces a checkerboard similar to the previous example.

This is not C code. This example is taken from [RenderMan] page 345.

RenderMan has a number of features that make it particularly easy to write shaders. Short vectors are highly integrated (arithmetic operators work on vectors, and scalars and vectors); it has special global variables (eg s and t above); it knows how to sum over lights (eg ambient, illuminance); and can even do derivatives (implemented numerically) with respect to the special variables.

Note that the above program is a (static) argument to the shading interpreter, while the code in the previous shading procedure example is the interpreter.

Pixar's implementation (prman) uses a buffered interpreter (see buffering) to evaluate a shading procedure (which describes how to shade one point on a surface) over a mesh of micropolygons. Pixar's implementation performed very well when it was originally designed (usually within 90% [Peachey94] of compilation, though that was many years ago).

Pixar could have built an implementation that compiled shaders into C, then compiled them with the local C compiler, and finally dynamically loaded the code. But this requires painfully tricky and non-portable code. Furthermore, if the system were interactive the time spent compiling code might introduce an unacceptably long pause.

Recent research from Microsoft indicates compiling shaders and specializing them to particular surface properties has great benefits (as much as 100x) for interactive modeling.

What if Alvy Ray Smith and Darwyn Peachey had had tools like DCG and `C [tick-C]? The portability (OS and architecture) code could be isolated, while bringing the overhead (\\sigma) way down.

This concludes our discussion of compilation and RTCG. Next, compiler generation via partial evaluation is presented as a tool for achieving lightweight languages.