Title: Shader Basics
1Shader Basics
2Contents
- Why write shaders
- OpenGL programmable processors
- Language overview
- System overview
3Visual Summary of Fixed Functionality
4Why Write Shaders
- Increasingly realistic materials metals, stone,
wood, paints, - Increasingly realistic lighting effects area
lights, soft shadows, - Natural phenomena fire, smoke, water, clouds,
- Advanced rendering effects global illumination,
ray-tracing, - Non-photorealistic materials painterly effects,
pen-and-ink drawings, simulation of illustration
techniques, - New uses for texture memory storage of normals,
gloss values, polynomial coefficients, - Procedural textures dynamically generated 2D and
3D textures, not static texture images - Image processing convolution, unsharp masking,
complex blending, - Animation effects key frame interpolation,
particle systems, procedurally defined motion - User programmable anti-aliasing methods
- General computation sorting, mathematical
modeling, fluid dynamics,
5About GLSL
- The OpenGL Shading Language is a high-level
procedural language. - As of OpenGL 2.0, it is part of standard OpenGL,
the leading cross-platform, operating-environment-
independent API for 3D graphics and imaging. - The same language, with a small set of
differences, is used for both vertex and fragment
shaders. - It is based on C and C syntax and flow control.
- It natively supports vector and matrix operations
since these are inherent to many graphics
algorithms. - It is stricter with types than C and C, and
functions are called by value-return. - It uses type qualifiers rather than reads and
writes to manage input and output. - It imposes no practical limits to a shader's
length, nor does the shader length need to be
queried.
6Programmable Processors
7Tasks of a Vertex Processor
- Vertex transformation
- Normal transformation and normalization
- Texture coordinate generation
- Texture coordinate transformation
- Lighting
- Color material application
8Vertex Processor
- The design of the vertex processor is focused on
the TL functions of a single vertex. - Vertex shaders must compute the homogeneous
position of the coordinate in clip space and
store the result in the special output variable
gl_Position. - Values to be used during user clipping and point
rasterization can be stored in the special output
variables gl_ClipVertex and gl_PointSize. - Conceptually, the vertex processor operates on
one vertex at a time (but an implementation may
have multiple vertex processors that operate in
parallel). The vertex shader is executed once for
each vertex passed to OpenGL.
9Vertex Processor
10Vertex Processor
- OpenGL operations that remain as fixed
functionality in between the vertex processor and
the fragment processor include - perspective divide
- viewport mapping
- primitive assembly
- frustum and user clipping
- backface culling
- two-sided lighting selection
- polygon mode
- polygon offset
- selection of flat or smooth shading
- depth range.
11(No Transcript)
12Attribute Variables
Variables defined in a vertex shader
- There are two types of attribute variables built
in and user defined. Standard built-in attribute
variables in OpenGL include color, surface
normal, texture coordinates, and vertex position. - Within the OpenGL API, generic vertex attributes
are defined and referenced by numbers from 0 up
to some maximum value. The command glVertexAttrib
sends generic vertex attributes to OpenGL by
specifying the index of the generic attribute to
be modified and the value for that generic
attribute. - glBindAttribLocation allows an application to tie
together the index of a generic vertex attribute
and the name with which to associate that
attribute in a vertex shader.
13Uniform Variables
- pass data values from the application to either
the vertex processor or the fragment processor. - A shader can be written so that it is
parameterized with uniform variables. The
application can provide initial values for these
uniform variables, - But uniform variables cannot be specified between
calls to glBegin and glEnd, so they can change at
most once per primitive.
14Uniform Variables (cont)
- supports both built-in and user-defined uniform
variables. Vertex shaders and fragment shaders
can access current OpenGL state through built-in
uniform variables containing the reserved prefix
"gl_". - Applications can make arbitrary data values
available directly to a shader through
user-defined uniform variables. - glGetUniformLocation obtains the location of a
user-defined uniform variable that has been
defined as part of a shader.
15Varying Variables
- Variables that define data that is passed from
the vertex processor to the fragment processor
are called VARYING VARIABLES. - Both built-in and user-defined varying variables
are supported. They are called varying variables
because the values are potentially different at
each vertex and perspective-correct interpolation
ref is performed to provide a value at each
fragment for use by the fragment shader.
16Varying Variables (cont)
- Built-in varying variables include those defined
for the standard OpenGL color and texture
coordinate values. A vertex shader can use a
user-defined varying variable to pass along
anything that needs to be interpolated colors,
normals (useful for per-fragment lighting
computations), texture coordinates, model
coordinates, and other arbitrary values.
17Tasks of a Fragment Processor
- Operations on interpolated values
- Texture access
- Texture application
- Fog
- Color sum
18Fragment Processor
- A fragment shader typically writes into one or
both of the special variables gl_FragColor or
gl_FragDepth. - To support parallelism at the fragment-processing
level, fragment shaders are written in a way that
expresses the computation required for a single
fragment, and access to neighboring fragments is
not allowed. - The fragment processor does not replace the fixed
functionality graphics operations that occur at
the back end of the OpenGL pixel processing
pipeline - coverage, pixel ownership test, stippling, tests
(scissor, alpha, depth, stencil), alpha blending,
logical operations, dithering, and plane masking.
19Input Variables
- The window coordinate position of the fragment is
communicated through the special input variable
gl_FragCoord. An indicator of whether the
fragment was generated by rasterizing a
front-facing primitive is communicated through
the special input variable gl_FrontFacing. - A fragment shader is free to read multiple values
from a single texture or multiple values from
multiple textures. - The result of one texture access can be used as
the basis for performing another texture access
(a DEPENDENT TEXTURE READ). - There is no inherent limitation on the number of
such dependent reads that are possible, so
ray-casting algorithms can be implemented in a
fragment shader.
20Fragment Processor
21(No Transcript)
22Language Overview
- C-basis GLSL is based on the syntax of the ANSI
C programming language, - Addition to C Vector types are supported for
floating-point, integer, and Boolean values.
Operators work as readily on vector types as they
do on scalars. - Floating-point matrix types are also supported as
basic types. - Samplers are a special type of opaque variable
that access a particular texture map. 1D2D3D
texture map, cube map textures and shadow
textures are supported
23Overview (cont)
- GLSL defines built-in functions for a variety of
operations - Trigonometric operations sine, cosine, tangent,
- Exponential operations power, exponential,
logarithm, square root, and inverse square root - Common math operations absolute value, floor,
ceiling, fractional part, modulus, - Geometric operations length, distance, dot
product, cross product, normalization, - Relational operations based on vectors
component-wise operations such as greater than,
less than, equal to, - Specialized fragment shader functions for
computing derivatives and estimating filter
widths for antialiasing - Functions for accessing values in texture memory
- Functions that return noise values for procedural
texturing effects
24Differences does not support
- automatic promotion of data types
- pointers, strings, or characters,
- double-precision floats byte, short, or long
integers or unsigned data types - unions, enumerated types, bit fields in
structures, and bitwise operators. - Finally, the language is not file based, so you
won't see any include directives or other
references to file names.
25Differences
- constructors, rather than type casts, are used
for data type conversion. - unlike the call-by-value calling convention used
by C, GLSL uses CALL BY VALUE-RETURN. - Input parameters are copied into the function at
call time, and output parameters are copied back
to the caller before the function exits. - Qualifiers in, out, inout
26(No Transcript)
27To install and use OpenGL shaders, do the
following
- Create one or more (empty) shader objects by
calling glCreateShader. - Provide source code for these shaders by calling
glShaderSource. - Compile each of the shaders by calling
glCompileShader. - Create a program object by calling
glCreateProgram. - Attach all the shader objects to the program
object by calling glAttachShader. - Link the program object by calling glLinkProgram.
- Install the executable program as part of
OpenGL's current state by calling glUseProgram.
28GLSL API
glGetProgram glGetProgramInfoLog glGetShader glGet
ShaderInfoLog glGetShaderSource glGetUniform glGet
UniformLocation glGetVertexAttrib glGetVertexAttri
bPointer glIsProgram glIsShader glLinkProgram glSh
aderSource glUniform glUseProgram glValidateProgra
m glVertexAttrib glVertexAttribPointer
glAttachShader glBindAttribLocation glCompileShade
r glCreateProgram glCreateShader glDeleteProgram g
lDeleteShader glDetachShader glDisableVertexAttrib
Array glEnableVertexAttribArray
glGetActiveAttrib glGetActiveUniform glGetAttache
dShaders glGetAttribLocation
More details later
29Key Benefits
- Tight integration with OpenGL
- Runtime compilation
- No reliance on cross-vendor assembly language
- Unconstrained opportunities for compiler
optimization plus optimal performance on a wider
range of hardware - A truly open, cross-platform standard
- One high-level language for all programmable
graphics processing - Support for modular programming
- No additional libraries or executables