Graphics Reference
In-Depth Information
A Comment on Shader Code Efficiency
GLSL gives you some clever ways to make your code execute super efficiently on
graphics hardware. As with many such things in computing, however, it often makes
the code harder to read. For example, rather than creating two separate variables above,
thisX and thisY , and then squaring each to compute thisPos.z as shown previously, it
would be more efficient to say
vec2 thisXY = thisPos.xy;
thisPos.z = 0.3 * sin( dot( thisXY, thisXY ) );
Similarly, the computation for the tangent vectors could be expressed more efficiently as
xtangent.z = 2. * 0.3 * thisX * cos( dot( thisXY, thisXY ) );
ytangent.z = 2. * 0.3 * thisY * cos( dot( thisXY, thisXY ) );
But, at least for some, this would make the code less readable. For this topic, we have
often taken our own code and re-written it to be more readable, even though that may
make it less efficient. We're sure you will find lots of examples of this. Don't email us
about it—we already know.
Fragment Shaders
Sometimes called pixel shaders (e.g., in Cg), fragment shaders operate on a frag-
ment to determine the color of its pixel. We know that rasterization opera-
tions interpolate quantities such as colors, depths, and texture coordinates.
Fragment shaders use these interpolated values, as well as many other kinds
of information, to determine the color of each fragment's pixel.
The rasterizer interpolates any variables that have been defined in the
geometry processing stages and passed to the fragment shader. These inter-
polated values may be used in any kind of fragment computation you want.
These computations are performed on several fragments in parallel, with the
width of the parallelization depending on the particular graphics card you
use. This parallelization lets a fragment shader operate with the same kind of
acceleration as graphics cards do for the fixed-function pipeline.
As we saw for vertex shaders, many operations that were automatically
handled by the fixed-function pipeline are now the responsibility of the shader
programmer. A GLSL fragment shader replaces or adds the following operations:
Color computation.
Texturing.
Per-pixel lighting.
Fog.
Discarding pixels in fragments.
Search WWH ::




Custom Search