i
i
i
i
i
i
i
i
464 18. Using Graphics Hardware
main difference is in the use of the varying variables, v and n. In the fragment
shader, the view vectors and normal values are interpolated across the surface of
the model between neighboring vertices. The results are shown in Figure 18.8.
Immediately, you should notice the Phong highlight on the quadrilateral, which
only contains four vertices. Because the shading is being calculated at the frag-
ment level using the Phong equation with the interpolated (i.e., varying) data,
more consistent and accurate Phong shading is produced with far less geometry.
18.3.5 General Purpose Computing on the GPU
After studying the vertex and fragment shader examples, you may be wondering
if you can write programs to perform other types of computations on the GPU.
Obviously, the answer is yes, as many problems can be coded to run on the GPU
given the various languages available for programming on the GPU. However, a
few facts are important to remember. Foremost, floating point math processing
on graphics hardware is not currently double-precision. Secondly, you will likely
need to transform your problem into a form that fits within a graphics-related
framework. In other words, you will need to use the graphics APIs to set up the
problem, use texture maps as data rather than traditional memory, and write vertex
and fragment shaders to frame and solve your problem.
Having stated that, the GPU may still be an attractive platform for computa-
tion, since the ratio of transistors that are dedicated to performing computation
is much higher on the GPU than it is on the CPU. In many cases, algorithms
running on GPUs run faster than on a CPU. Furthermore, GPUs perform SIMD
computation, which is especially true at the fragment-processing level. In fact,
it can often help to think about the computation occurring on the fragment pro-
cessor as a highly parallel version of a generic foreach construct, performing
simultaneous operations on a set of elements.
There has been a large amount of investigation to perform General Purpose
computation on GPUs, often referred to as GPGPU. Among other things, re-
searchers are using the GPU as a means to simulate the dynamics of clouds (Harris
et al., 2003), implement ray tracers (Purcell et al., 2002; N. A. Carr et al., 2002),
compute radiosity (Coombe et al., 2004), perform 3D segmentation using level
sets (A. E. Lefohn et al., 2003), or solve the Navier-Stokes equations (Harris,
2004).
General purpose computation is often performed on the GPU using multiple
rendering “passes,” and most computation is done using the fragment processor
due to its highly data-parallel setup. Each pass, called a kernel, completes a por-
tion of the computation. Kernels work on streams of data with several kernels