Shader compilation on various platforms

In mental ray, shaders are defined at rendering time by loading a software library that contains one or more shaders. The way that the shader source code in C or C++ is compiled to produce the library differs between operating systems. This page describes the mechanisms required for Linux, Mac OS X, and Microsoft Windows. Other Unix variants will be similar to Linux and Mac OS X; Windows compilation mechanisms differ based on the compiler you have purchased.

Compilation of shader libraries

The way that libraries are compiled can vary greatly based on the compiler. However, the distribution of the Free Software Foundation’s gcc compiler in Linux and Mac OS X permits similar mechanisms across those and other Unix-based systems. Compilation under Microsoft Windows differs based on the compiler product you purchased from Microsoft.

Linux and Mac OS X

The “make” software utility can simplify the compilation of shader libraries, since dependencies on source and auxiliary files can be used to determine when libraries need to be recompiled based on changes in source files. The input file for the make utility is called a Makefile. Here are example Makefiles for Linux and Mac OS X; copy them into the directory that contains the example shaders for the book.
Linux Makefile.linux
Mac OS X Makefile.macosx
Edit the Makefiles to reflect the directory structure of your installation. You can then use the “make” command with the Makefile appropriate for your operating system to compile all the shaders in the “shader” directory:
Linux make -f Makefile.linux
Mac OS X make -f Makefile.macosx
If you copy the appropriate Makefile for your system to a file simply called “Makefile,” you can execute “make” alone on the command line — the make command uses a file called “Makefile” by default.

Microsoft “Windows”

The Makefile mechanism is not available in a standard Windows configuration. The following batch file will compile all the book’s shaders in some configurations of Windows:
Windows (32-bit) all_shaders.bat
Windows (64-bit) all_shaders_64.bat
To simplify the definition of compilation and linking flags, separate files set variables to use as flags.
Windows (32-bit) set_variables.bat
Windows (64-bit) set_variables_64.bat
Depending upon the compiler version and the location of various software libraries, adjustments may be required to the batch proces through changes to the flag variables file. You can also use a commercial compiler application; follow its instructions to construct .dll files from the source C and C++ files using the batch compilation file as a guide.

You can also download a set of batch files that will individually compile each shader as well as the Python script that made them. The script could be tailored to simplify the compilation of your own set of shaders.
Windows batch_compile_windows.zip
Various systems (for example, the Cygwin Unix shell simulator) have been constructed to allow for more Unix-like development in Windows.

Additional documentation

Compilation of shaders is described on page 186 of Programming mental ray (3rd edition) and in the on-line documentation in subsection “Dynamic Linking of Shaders” of section “Using and Writing Shaders.” (See [Driemeyer 05b] in the Bibliography page.)
19 April 2008 23:08:59