OpenGL Shaders
OpenGL Shaders
OpenGL vs OpenGLES
• OpenGL built for desktop applications
• Fewer power/heat concerns
• OpenGLES created for embedded/
mobile applications
• Concerns about power/heat
• Has fewer features than OpenGL
Shaders
• Small arbitrary programs that
run on GPU
• Massively parallel
• Four kinds: vertex, geometry,
tessellation, fragment
• Now used for GPGPU
calculations, but we’re
focusing on the graphics
aspect!
Data Types
Allows for storage of:
• scalars: bool, int, uint, float,
double
• vectors: bvecn, ivecn, uvecn, vecn,
dvecn
• matrices: matnxm, matn
http://www.shaderific.com/glsl-functions/
Flow Control
All the usual suspects:
• If-else/Switch
• For/While/Do-while (avoid!)
• Break/Return/Continue
• Discard (only in fragment shader)
Swizzling
Access vector components individually
vec4 a_vector;
a_vector.x + a_vector.y;
Example:
layout(location = attribute index)
associates buffer to use with VAO index
(defined earlier)
Overrides glBindAttribLocation
Note: Currently not available in ELSL
Storage Qualifiers
in or out determines if assignment is
being inputted or outputted
UV coords, normals,
colors, …
Fragment Shader
Runs in parallel on each fragment (pixel)
• rasterization: one triangle -> many
fragments
Environment-mapping
Fragment Shader In and Outs
Open GL Tutorial
http://www.opengl-tutorial.org/beginners-
tutorials/tutorial-3-matrices/
http://www.opengl-tutorial.org/beginners-
tutorials/tutorial-4-a-colored-cube/
Using Shaders
Must compile and link shaders to make
executable:
Shaders GPU CPU
Inputs Vertex VBOs
Attributes
position vertPos[]
normal vertNormals[]
1
2
3 Uniforms
Uniforms
Global view
Memory lightPos
view
lightPos
1
2
3
Shaders GPU CPU
Inputs Vertex VBOs
Attributes
position vertPos[]
normal vertNormals[]
1
2
3 Uniforms
Uniforms
Global view
Memory lightPos
view
lightPos
1
2
3 when shader is compiled
Shaders GPU CPU
Inputs Vertex VBOs
Attributes
position vertPos[]
normal vertNormals[]
1
2
3 Uniforms
Uniforms
Global view
Memory lightPos
view
lightPos
1
2
3 glBindAttribLocation()
Shaders GPU CPU
Inputs Vertex VBOs
Attributes
position vertPos[]
normal vertNormals[]
1
2
3 Uniforms
Uniforms
Global view (2)
Memory lightPos (1)
view
lightPos
1
2
3 glGetUniformLocation()
Shaders GPU CPU
Inputs Vertex VBOs
Attributes
position vertPos[]
normal vertNormals[]
1
2
3 Uniforms
Uniforms
Global view (2)
Memory lightPos (1)
view
lightPos
1 at render time:
2 glVertexAttribPointer()
3
Shaders GPU CPU
Inputs Vertex VBOs
Attributes
position vertPos[]
normal vertNormals[]
1
2
3 Uniforms
Uniforms
Global view (2)
Memory lightPos (1)
view
lightPos
1 at render time:
2 glVertexAttribPointer()
3 glUniform**()
Shaders GPU CPU
Inputs Vertex VBOs
Attributes
position vertPos[]
normal vertNormals[]
1
2
3 Uniforms
Uniforms
Global view (2)
Memory lightPos (1)
view
lightPos
1
2
3 VAOs store the VBO state
Switching VAOs, Shaders, VBOs
Possible to switch between VAOs, shaders
and VBOs at any time
1. Set up all resources at beginning of
program
2. Bind or unbind as necessary within the
main loop (batch as much as possible)
3. Only update uniforms if values have
changed
GLEW
OpenGL Extension Wrangler
Take UV coordinates:
in vec2 uvcoords;
Substance Designer
Shaders in Art Pipelines
https://www.shadertoy.com/