Real-Time Shading

Compiling a shading language to run on graphics hardware is quite similar to compiling any other language for any other somewhat quirky architecture. Anyone wanting to write a shading compiler of their own should look into the vast literature on general purpose compilers, as most of it applies [3, 8, 50, 121, 122].
In this chapter, we will describe the basics of shading compiler construction to give some basis for the descriptions that follow. Our intent is to give enough background to support the coming chapters, give an idea of what current shading compilers can or cannot map efficiently to rendering hardware, and to give a starting point for those who might want to explore further.
Most compilers are organized in a series of passes over all or part of the program being compiled. Each of these passes performs a relatively small and understandable transformation to the program. This breakdown makes the compiler easier to write, debug, and extend.
Typically, a parsing stage converts the program text into a relatively high-level intermediate representation. Some number of passes will operate on this representation, transforming, simplifying, and optimizing the program. Then the high-level intermediate representation is converted to a lower-level intermediate form, where it is again operated on by some number of additional passes. Each intermediate representation is a step closer to the final form. Some compilers may go straight from parsing to assembly language or something very close to it, while others work...