O'Reilly logo

Graphics Shaders, 2nd Edition by Steve Cunningham, Mike Bailey

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

91
The GLSL Shader
Language
5
As shader capabilities in graphics hardware have become more exible, shader
languages have been developed to give the graphics programmer access to
these capabilities. The GLSL shading language was designed to be device inde-
pendent and has been part of the OpenGL standardfrom OpenGL 2.0 forward.
It accomplishes its device independence by having compilers built into the
graphics card driver translate the GLSL code into the specic device instruc-
tions for that card. The actual process of aaching shaders to shader programs,
compiling them, and linking them to be downloaded into the graphics card is
part of the GLSL API, covered in Chapter 14.
GLSL is a very C-like language, with most of the same fundamental
code structure and operators that are found in that language. Thus, there
are no challenges to the graphics programmer in understanding the control
ow, basic operations, or basic data types in the language. However, there
are some areas where GLSL extends the capabilities of C, some areas where
92
5. The GLSL Shader Language
GLSL omits some of the capabilities
of C, and some areas where GLSL
has language features that remind
us of the best of earlier generations
of computer languages. This chap-
ter focuses on these dierences and
discusses why they are needed for
the shader environment. There is a
tendency for any discussion like this
to have a strong avor of a language
manual, and you might nd that
you use this chapter more as a refer-
ence than as general reading.
We introduced a number
of GLSL language features in
Chapter 3, but here we take a more thorough approach to the language and
describe it more formally. We are working from the GLSL language specica-
tion [23] and include those features and capabilities that we believe are most
useful to you, but we are not absolutely complete in our coverage. Once you
are familiar with a good working set of GLSL, you probably should read the
GLSL specication to see what else is there—especially since the language will
continue to evolve over time.
1
We are indebted to the GLSL Shader Language Specication document
both for the overall information it contains and for its excellent tables of GLSL
functions and operations that we have borrowed from extensively.
Factors that Shape Shader Languages
Shader languages operate in a dierent environment and with dierent goals
than general-purpose languages. Their environment is the processing capabil-
ity of graphics cards, which diers in some important ways from the capability
of a general CPU, and their goals are tightly focused on supporting graphics
operations, rather than more general kinds of computations. These capabilities
shape the language in signicant ways, and it is important that you under-
stand their impacts as you write shaders.
GLSL shader capabilities are very much
a moving target. This chapter and all
our examples are based on GLSL 4.1.
However, we also include many features
that are deprecated in that standard but are
available in compatibility mode, because
they may be helpful to someone learning to
work with shaders for the first time.
In order to keep current on GLSL,
you should consult [32] from time to time.
1
You will not need a new copy of glman,
however, because OpenGL will compile
only the GLSL shaders, but you may need
to get a new OpenGL driver.
1. Good resources: “OpenGL.” Khronos. Available at hp://www.khronos.org/opengl/.
“OpenGL 4.2 API Quick Reference Card.” Khronos. Available at hp://www.khronos.org/les/
opengl42-quick-reference-card.pdf, 2010.
“OpenGL Shading Language.” OpenGL. Available at hp://www.opengl.org/documentation/glsl/,
2011.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required