Programmable Pipelines Black Box View vertices CPU vertices
Programmable Pipelines
Black Box View vertices CPU vertices Geometry Processor fragments Rasterizer Angel: Interactive Computer Graphics 5 E © Addison Wesley 2009 fragments Fragment Processor Frame Buffer 2
Programmable Shaders • Replace fixed function vertex and fragment processing by programmable processors called shaders • Can replace either or both • If we use a programmable shader we must do all required functions of the fixed function processor Angel: Interactive Computer Graphics 5 E © Addison Wesley 2009 3
Geometric Calculations • Geometric data: set of vertices + type Can come from program, evaluator, display list type: point, line, polygon Vertex data can be • (x, y, z, w) coordinates of a vertex (gl. Vertex) • Normal vector • Texture Coordinates • RGBA color • Other data: color indices, edge flags • Additional user defined data in GLSL Angel: Interactive Computer Graphics 5 E © Addison Wesley 2009 4
Per-Vertex Operations • Vertex locations are transformed by the model view matrix into eye coordinates • Normals must be transformed with the inverse transpose of the model view matrix so that v·n=v’ ·n’ in both spaces Assumes there is no scaling May have to use autonormalization • Textures coordinates are generated if autotexture enabled and the texture matrix is applied Angel: Interactive Computer Graphics 5 E © Addison Wesley 2009 5
Vertex Processor • Takes in vertices Position attribute Possibly color Open. GL state • Produces Position in clip coordinates Vertex color Angel: Interactive Computer Graphics 5 E © Addison Wesley 2009 6
Rasterization • Geometric entities are rasterized into fragments • Each fragment corresponds to a point on an integer grid: a displayed pixel • Hence each fragment is a potential pixel • Each fragment has A color Possibly a depth value Texture coordinates Angel: Interactive Computer Graphics 5 E © Addison Wesley 2009 7
Fragment Operations • Texture generation • Fog • Antialiasing • Scissoring • Alpha test • Blending • Dithering • Logical Operation • Masking Angel: Interactive Computer Graphics 5 E © Addison Wesley 2009 8
Fragment Processor • Takes in output of rasterizer (fragments) Vertex values have been interpolated over primitive by rasterizer • Outputs a fragment Color Texture • Fragments still go through fragment tests Hidden surface removal alpha Angel: Interactive Computer Graphics 5 E © Addison Wesley 2009 9
Fragment Shader Applications Per fragment lighting calculations per vertex lighting per fragment lighting Angel: Interactive Computer Graphics 5 E © Addison Wesley 2009 10
Fragment Shader Applications Texture mapping smooth shading environment mapping Angel: Interactive Computer Graphics 5 E © Addison Wesley 2009 bump mapping 11
GLSL • Open. GL Shading Language • Part of Open. GL 2. 0 • High level C like language • New data types Matrices Vectors Samplers • Open. GL state available through built in variables Angel: Interactive Computer Graphics 5 E © Addison Wesley 2009 12
Simple Vertex Shader const vec 4 red = vec 4(1. 0, 0. 0, 1. 0); void main(void) { gl_Position = gl_Projection. Matrix *gl_Model. View. Martrix*gl_Vertex; gl_Front. Color = red; } Angel: Interactive Computer Graphics 5 E © Addison Wesley 2009 13
Execution Model Angel: Interactive Computer Graphics 5 E © Addison Wesley 2009 14
Simple Fragment Program void main(void) { gl_Frag. Color = gl_Color; } Angel: Interactive Computer Graphics 5 E © Addison Wesley 2009 15
Execution Model Angel: Interactive Computer Graphics 5 E © Addison Wesley 2009 16
Data Types • C types: int, float, bool • Vectors: float vec 2, vec 3, vec 4 Also int (ivec) and boolean (bvec) • Matrices: mat 2, mat 3, mat 4 Stored by columns Standard referencing m[row][column] • C++ style constructors vec 3 a =vec 3(1. 0, 2. 0, 3. 0) vec 2 b = vec 2(a) Angel: Interactive Computer Graphics 5 E © Addison Wesley 2009 17
Pointers • There are no pointers in GLSL • We can use C structs which can be copied back from functions • Because matrices and vectors are basic types they can be passed into and output from GLSL functions, e. g. matrix 3 func(matrix 3 a) Angel: Interactive Computer Graphics 5 E © Addison Wesley 2009 18
Qualifiers • GLSL has many of the same qualifiers such as const as C/C++ • Need others due to the nature of the execution model • Variables can change Once per primitive Once per vertex Once per fragment At any time in the application • Vertex attributes are interpolated by the rasterizer into fragment attributes Angel: Interactive Computer Graphics 5 E © Addison Wesley 2009 19
Attribute Qualifier • Attribute qualified variables can change at most once per vertex Cannot be used in fragment shaders • Built in (Open. GL state variables) gl_Color gl_Model. View. Matrix • User defined (in application program) attribute float temperature attribute vec 3 velocity Angel: Interactive Computer Graphics 5 E © Addison Wesley 2009 20
Uniform Qualified • Variables that are constant for an entire primitive • Can be changed in application outside scope of gl. Begin and gl. End • Cannot be changed in shader • Used to pass information to shader such as the bounding box of a primitive Angel: Interactive Computer Graphics 5 E © Addison Wesley 2009 21
Varying Qualified • Variables that are passed from vertex shader to fragment shader • Automatically interpolated by the rasterizer • Built in Vertex colors Texture coordinates • User defined Requires a user defined fragment shader Angel: Interactive Computer Graphics 5 E © Addison Wesley 2009 22
Example: Vertex Shader const vec 4 red = vec 4(1. 0, 0. 0, 1. 0); varying vec 3 color_out; void main(void) { gl_Position = gl_Model. View. Projection. Matrix*gl_Vertex; color_out = red; } Angel: Interactive Computer Graphics 5 E © Addison Wesley 2009 23
Required Fragment Shader varying vec 3 color_out; void main(void) { gl_Frag. Color = color_out; } Angel: Interactive Computer Graphics 5 E © Addison Wesley 2009 24
Passing values in functions • call by value-return • Variables are copied in • Returned values are copied back • Three possibilities in out inout Angel: Interactive Computer Graphics 5 E © Addison Wesley 2009 25
Operators and Functions • Standard C functions Trigonometric Arithmetic Normalize, reflect, length • Overloading of vector and matrix types mat 4 a; vec 4 b, c, d; c = b*a; // a column vector stored as a 1 d array d = a*b; // a row vector stored as a 1 d array Angel: Interactive Computer Graphics 5 E © Addison Wesley 2009 26
Linking Shaders to Open. GL • Open. GL Extensions ARB_shader_objects ARB_vertex_shader ARB_fragment_shader • Open. GL 2. 0 Almost identical to using extensions Avoids extension suffixes on function names Angel: Interactive Computer Graphics 5 E © Addison Wesley 2009 27
Program Object • Container for shaders Can contain multiple shaders Other GLSL functions GLuint my. Prog. Obj; my. Prog. Obj = gl. Create. Program(); /* define shader objects here */ gl. Use. Program(my. Prog. Obj); gl. Link. Program(my. Prog. Obj); Angel: Interactive Computer Graphics 5 E © Addison Wesley 2009 28
Reading a Shader • Shader are added to the program object and compiled • Usual method of passing a shader is as a null terminated string using the function gl. Shader. Source • If the shader is in a file, we can write a reader to convert the file to a string Angel: Interactive Computer Graphics 5 E © Addison Wesley 2009 29
Shader Reader #include <stdio. h> char* read. Shader. Source(const char* shader. File) { FILE* fp = fopen(shader. File, "r"); char* buf; long size; if(fp == NULL) return(NULL); fseek(fp, 0 L, SEEK_END); /* end of file */ size = ftell(fp); fseek(fp, )L, SEEK_SET); /* start of file */ Angel: Interactive Computer Graphics 5 E © Addison Wesley 2009 30
Shader Reader (cont) • buf = (char*) malloc(stat. Buf. st_size + 1 * sizeof(char)); fread(buf, 1, size, fp); buf[size] = '