Shader Programs Lecture 30 Subsections 2.8.2, 2.8.3 Robb T. Koether Hampden-Sydney College Wed, Nov 16, 2011 Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 1 / 43
Outline 1 The Fixed Graphics Pipeline Vertex Shaders Fragment Shaders 2 The GLSL Language GLSL Data Types GLSL Variables Examples 3 Assignment Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 2 / 43
Outline 1 The Fixed Graphics Pipeline Vertex Shaders Fragment Shaders 2 The GLSL Language GLSL Data Types GLSL Variables Examples 3 Assignment Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 3 / 43
The Fixed Graphics Pipeline The fixed graphics pipeline. Process vertices. Assemble primitives. Transform vertices to clip coordinates. Clip primitives. Perform perspective division. Rasterize primitives. Process fragments. Write fragments to the framebuffer. Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 4 / 43
The Fixed Graphics Pipeline The fixed graphics pipeline. Process vertices. Assemble primitives. Transform vertices to clip coordinates. Clip primitives. Perform perspective division. Rasterize primitives. Process fragments. Write fragments to the framebuffer. Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 5 / 43
The Fixed Graphics Pipeline The fixed graphics pipeline vertex processor Transforms the vertex to eye coordinates. Transforms the normal to eye coordinates. Generates texture coordinates. Applies color material. Performs lighting calculations. Transforms the vertex to clip coordinates. Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 6 / 43
The Fixed Graphics Pipeline The fixed graphics pipeline fragment processor Applies the interpolated shade. Applies the interpolated textures. Does a few other things (e.g., fog) that we haven t talked about. Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 7 / 43
Outline 1 The Fixed Graphics Pipeline Vertex Shaders Fragment Shaders 2 The GLSL Language GLSL Data Types GLSL Variables Examples 3 Assignment Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 8 / 43
Vertex Shaders Definition (Vertex Shader) A vertex shader is a program that performs its own calculations on each vertex instead of using the calculations of the fixed pipeline. It is mandatory that the vertex shader compute the clip coordinates of the vertex. All other calculations (lighting, textures, etc.) are optional. Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 9 / 43
Vertex Shaders Example (A Minimal Vertex Shader) void main() { gl_position = gl_modelviewprojectionmatrix * gl_vertex; } Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 10 / 43
Vertex Shaders Example (A Minimal Vertex Shader) void main() { gl_position = ftransform(); } The ftransform() function will do the same thing. Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 11 / 43
Outline 1 The Fixed Graphics Pipeline Vertex Shaders Fragment Shaders 2 The GLSL Language GLSL Data Types GLSL Variables Examples 3 Assignment Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 12 / 43
Fragment Shaders Definition (Fragment Shader) A fragment shader is a program that performs its own calculations on each fragment of a primitive instead of using the interpolated values of the fixed pipeline. It is mandatory that the fragment shader compute the color of the fragment. All other calculations are optional. Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 13 / 43
Fragment Shaders Example (A Minimal Fragment Shader) void main() { gl_fragcolor = gl_color; } Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 14 / 43
Minimal Shaders Lecture 30 Demo 1 This demo consists of three programs: Lecture 30 Demo 1.vert Lecture 30 Demo 1.frag Lecture 30 Demo 1.cpp The shader programs are minimal shaders. Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 15 / 43
Outline 1 The Fixed Graphics Pipeline Vertex Shaders Fragment Shaders 2 The GLSL Language GLSL Data Types GLSL Variables Examples 3 Assignment Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 16 / 43
Outline 1 The Fixed Graphics Pipeline Vertex Shaders Fragment Shaders 2 The GLSL Language GLSL Data Types GLSL Variables Examples 3 Assignment Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 17 / 43
GLSL Data Types GLSL is the OpenGL Shading Language. GLSL provides several variable types. Shader variables must be of these types. The three primitive types are bool int float Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 18 / 43
GLSL Data Types GLSL has several vector types Boolean vectors - bvec2, bvec3, bvec4. Integer vectors - ivec2, ivec3, ivec4. Floating-point vectors - vec2, vec3, vec4. GLSL has three floating-point matrix types 2 2 matrices - mat2. 3 3 matrices - mat3. 4 4 matrices - mat4. Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 19 / 43
GLSL Data Types Example (GLSL Data Types) // Vertex Shader void main() { gl_position = ftransform(); gl_frontcolor = vec4(1.0, 0.0, 1.0, 1.0); } // Fragment Shader void main() { gl_fragcolor = gl_color; } Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 20 / 43
Outline 1 The Fixed Graphics Pipeline Vertex Shaders Fragment Shaders 2 The GLSL Language GLSL Data Types GLSL Variables Examples 3 Assignment Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 21 / 43
GLSL Variables There are four categories of variable. const uniform attribute varying Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 22 / 43
GLSL Variables Definition (const Variable) A const variable is constant at compile time and, therefore, constant throughout the program. A const is defined within a shader and its scope is limited to that shader. Application Vertex Shader const int s = 0; Fragment Shader const int t = 1; Framebuffer Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 23 / 43
GLSL Variables Definition (uniform Variable) A uniform variable may be modified by the application program, but its value is constant over the scope of a primitive, i.e., throughout a glbegin()/glend() pair. A uniform variable holds a value that is associated with the primitive. Vertex Shader uniform int s; Application s gluniform(sloc, s); gluniform(tloc, t); t Fragment Shader uniform int t; Framebuffer Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 23 / 43
GLSL Variables Definition (attribute Variable) An attribute variable may be modified for each vertex, but its value is constant during the processing of that vertex. An attribute variable holds a value that is associated with the vertex. Vertex Shader attribute int s; Application s glvertexattrib(sloc, s); Fragment Shader Framebuffer Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 23 / 43
GLSL Variables Definition (varying Variable) A varying variable is given a value by the vertex shader. Its values at the vertices are interpolated over the primitive. A varying variable holds a value that will be sent to the fragment shader. Application Vertex Shader varying float s; s Fragment Shader varying float s; Framebuffer Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 23 / 43
Outline 1 The Fixed Graphics Pipeline Vertex Shaders Fragment Shaders 2 The GLSL Language GLSL Data Types GLSL Variables Examples 3 Assignment Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 24 / 43
GLSL Data Types Example (Constants) const vec4 blue = vec4(0.0, 0.0, 1.0, 1.0); void main() { gl_position = ftransform(); gl_frontcolor = blue; } Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 25 / 43
Constants Lecture 30 Demo 2 This vertex shader program uses a constant to set the color to blue. Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 26 / 43
GLSL Data Types Example (Uniform Variables) const vec4 blue = vec4(0.0, 0.0, 1.0, 1.0); uniform float s; void main() { gl_position = ftransform(); gl_frontcolor = mix(gl_color, blue, s); } Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 27 / 43
Uniform Variables For uniform variables, it is necessary to establish a means of communicating their values from the application to the shader programs. In the application program, use glgetuniformlocation() to receive a storage location for the variable in the GPU (once and for all). Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 28 / 43
Uniform Variables glgetuniformlocation() unsigned int glgetuniformlocation(unsigned int progobj, string var_name); progobj The program reference number (to be discussed later). var_name The name of the variable in the shader. Return value A reference number for the location of the variable in the GPU. Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 29 / 43
Uniform Variables Then it is necessary to pass the current value from the application program to the GPU. Use gluniform() to do this (as often as the value changes). Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 30 / 43
Uniform Variables gluniform() void gluniform(unsigned int loc, type value); loc The reference number returned by glgetuniformlocation(). value The value to be passed to the shader. Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 31 / 43
Uniform Variables Example (Uniform Variables) // In the application float time; unsigned int tloc = glgetuniformlocation(myprog, "t"); gluniform1f(tloc, time); // In the vertex shader uniform float t; void main() { gl_frontcolor = mix(red, blue, sin(t)); }. Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 32 / 43
Uniform Variables Lecture 30 Demo 3 This vertex shader program uses a uniform variable to change the color. Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 33 / 43
GLSL Variables Example (Varying Variables) // In the vertex shader varying vec3 vertex_color; void main() { vertex_color = gl_normal; }. // In the fragment shader varying vec3 vertex_color; void main() { gl_fragcolor = vertex_color; }. Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 34 / 43
Varying Variables Lecture 30 Demo 4 These shader programs use a varying variable to change the color. Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 35 / 43
Attribute Variables Attribute variables may be given a different value for each vertex in a primitive. Otherwise, they are handled much like uniform variables. For attribute variables, it is necessary to establish a means of communicating their values from the application to the shader programs. In the application program, use glgetattriblocation() to receive a storage location for the variable in the GPU (once and for all). Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 36 / 43
Attribute Variables glgetuniformlocation() int glgetattriblocation(unsigned int progobj, string var_name); progobj The program reference number (to be discussed on Friday). var_name The name of the variable in the shader. Return value A reference number for the location of the variable in the GPU. Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 37 / 43
Attribute Variables Then it is necessary to pass the current value from the application program to the GPU. Use glvertexattrib() to do this (as often as the value changes). Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 38 / 43
Attribute Variables glvertexattrib() void glvertexattrib(unsigned int loc, type value); loc The reference number returned by glgetattriblocation(). value The value to be passed to the shader. Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 39 / 43
GLSL Variables Example (Attribute Variables) // In the vertex shader attribute vec3 color; void main() { gl_frontcolor = mix(gl_color, color, 0.5); }. Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 40 / 43
Attribute Variables Lecture 30 Demo 5 This vertex shader program uses an attribute variable to change the color of the upper vertex of each vertex in a triangle strip. Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 41 / 43
Outline 1 The Fixed Graphics Pipeline Vertex Shaders Fragment Shaders 2 The GLSL Language GLSL Data Types GLSL Variables Examples 3 Assignment Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 42 / 43
Homework Homework Read Subsection 2.8.2 The vertex shader. Read Subsection 2.8.3 The fragment shader. Robb T. Koether (Hampden-Sydney College) Shader Programs Wed, Nov 16, 2011 43 / 43