A shader program capable of execution on a GPU is analyzed for constant
expressions. These constant expressions are replaced with references to
registers or memory addresses on the GPU. A preshader is created that
comprises two executable files. The first executable file contains the
shader program with the each constant expression removed and replaced
with a unique reference accessible by the GPU. The first file is
executable at the GPU. A second file contains the removed constant
expressions along with instructions to place the values generated at the
associated reference. The second executable file is executable at a CPU.
When the preshader is executed, an instance of the first file is executed
at the GPU for each vertex or pixel that is displayed. One instance of
the second file is executed at the CPU. As the preshader is executed, the
constant expressions in the second file are evaluated and the resulting
intermediate values are passed to each instance of the first file on the
GPU.