OpenGL Shading Language GLSL Is a high level shading language based on the C programming language. It was created by the OpenGL ARB to give developers more direct control of the graphics pipeline without having to use assembly language or hardware-specific languages. Originally introduced as an extension to OpenGL 1.4, the OpenGL ARB formally included GLSL into the OpenGL 2.0 core. OpenGL 2.0 is the first major revision to OpenGL since the creation of OpenGL 1.0 in 1992. OpenGL 4.0 has just been announced! OpenGL 1.5 Logical Diagram OpenGL 2.0 Logical Diagram
Vertex Processor Capabilities Lighting, material and geometry flexibility Vertex processor can do general processing, including things like: Vertex transformation Normal transformation, normalization and rescaling Lighting Colour material application Clamping of colours Texture co-ordinate generation Texture co-ordinate transformation Vertex Processor Overview Fragment Processor Capabilities Fragment Processor Overview Fragment processor can do general processing, including things like: Operations on interpolated values!pixel zoom Texture access :- Scale and bias Texture application :- Colour table lookup Fog! Convolution Colour sum!colour matrix
Vertex Processor Input Vertex shader is executed once each time a vertex position is specified Via glvertex or gldrawarrays or other vertex array calls Per-vertex input values are called attributes E.g., colour, normal, position, arbitrary values Change every vertex Passed through normal OpenGL mechanisms (per-vertex API or vertex arrays) More persistent input values are called uniforms E.g., lights, material properties, arbitrary values Can come from OpenGL state or from the application Constant across at least one primitive, typically constant for many primitives Passed through new OpenGL API calls Vertex Processor Output Vertex shader uses input values to compute output values Vertex shader must compute gl_position Mandatory, needed by the rasterizer Can use built-in function ftransform() to get invariance with fixed functionality Vertex shader may compute: gl_clipvertex (if user clipping is to be performed) gl_pointsize (if point parameters are to be used) Vertex Processor Output Other output values are called varying variables E.g., colour, texture co-ordinates, arbitrary data Will be interpolated in a perspective-correct fashion across the primitives Defined by the vertex shader Can be of type float, vec2, vec3, vec4, mat2, mat3, mat4, or arrays of these Output of vertex processor feeds into OpenGL fixed functionality If a fragment shader is active, output of vertex shader must match input of fragment shader If no fragment shader is active, output of vertex shader must match the needs of fixed functionality fragment processing Vertex Processor Definition The vertex processor executes the vertex shader The vertex processor has knowledge of only the current vertex An implementation may have multiple vertex processors operating in parallel
Fragment Processor Input Output of vertex shader is the input to the fragment shader Compatibility is checked when linking occurs Compatibility between the two is based on varying variables that are defined in both shaders and that match in type and name Fragment shader is executed for each fragment produced by rasterization For each fragment, fragment shader has access to the interpolated value for each varying variable Colour, normal, texture coordinates, arbitrary values Fragment Processor Input Fragment shader may access: gl_frontfacing contains facingness of primitive that produced the fragment gl_fragcoord contains computed window relative coordinates x, y, z, 1/w Uniform variables are also available OpenGL state or supplied by the application, same as for vertex shader If no vertex shader is active, fragment shader get the results of OpenGL fixed functionality Fragment Processor Output Output of the fragment processor goes on to the fixed function fragment operations and frame buffer operations using built-in variables gl_fragcolor computed R, G, B, A for the fragment gl_fragdepth computed depth value for the fragment Both values are destined for writing into the frame buffer if back end tests all pass Clamping or format conversion to the target buffer is done automatically outside of the fragment shader Fragment Processor Definition The fragment processor executes the fragment shader The fragment processor has knowledge of only the current fragment An implementation may have multiple fragment processors operating in parallel
GLSL Language Syntax GLSL Language syntax is very similar to that of Renderman SL Most of the usual data types are present Other elements are available to fit into the GL pipeline marble.vertex 1 uniform vec3 LightPosition; 2 varying vec3 vpos; 3 varying float LightIntensity; 4 5 const float SpecularContribution = 0.3; 6 const float DiffuseContribution = 1.1 - SpecularContribution; 7 8 void main() 9 { 10 vec3 ecposition = vec3(gl_modelviewmatrix * gl_vertex); 11 vec3 tnorm = normalize(gl_normalmatrix * gl_normal); 12 vec3 lightvec = normalize(lightposition - ecposition); 13 vec3 reflectvec = reflect(-lightvec, tnorm); 14 vec3 viewvec = normalize(-ecposition); 15 float diffuse = max(dot(lightvec, tnorm), 0.0); 16 float spec = 0.0; 17 18 if (diffuse > 0.0) { 19 spec = dot(reflectvec, viewvec); 20 spec = pow(spec, 16.0); 21 } 22 23 LightIntensity = DiffuseContribution * diffuse + 24 SpecularContribution * spec; 25 26 gl_position = ftransform(); 27 vpos = gl_vertex.xyz; 28 29 gl_frontcolor = vec4(lightintensity, LightIntensity, LightIntensity, 1.0); 30 31 32 } marble.fragment 1 uniform vec3 stretch; 2 uniform vec3 color_a, color_b; 3 varying vec3 vpos; 4 varying float LightIntensity; 5 6 void main() { 7 float f; 8 vec3 color; 9 10 f = float(noise3(vpos * stretch)); 11 f = abs(f) + 0.8; 12 f = pow(f, 7.0); 13 f = clamp(f, 0.0, 1.0); 14 color = mix(color_a, color_b, f); 15 color *= LightIntensity; 16 17 gl_fragcolor = vec4(color, 1); 18 }
Source Code When Using GLSL the Shader source code is loaded into the program One for each Fragment and Vertex Shader required. We can load as many as we need. This is then compiled and sent to the graphics card. We then load the appropriate shader at render time to do the drawing. Source Code The source code for a shader consists of an array of strings Each string may contain multiple lines of source code, separated by new-lines A line of source code may be made of multiple strings Compiler diagnostic messages identify the source string and the line within the string that caused the error Source strings are numbered starting from 0 When parsing, current line number is number of new-lines processed plus 1 Loading Shader Code 1 bool ReadShader( 2 const std::string &_fname, 3 char **io_sourcebuffer 4 ) 5 { 6 std::fstream FileIn; 7 FileIn.open(_fname.c_str(),std::ios::in); 8 if (!FileIn.is_open()) 9 { 10 std::cout <<"File : "<<_fname<<" Not found"<<std::endl; 11 return false; 12 } 13 // grab the file lenght 14 FileIn.seekg (0, std::ios::end); 15 int length = FileIn.tellg(); 16 FileIn.seekg (0, std::ios::beg); 17 18 // allocate memory: 19 *io_sourcebuffer = new char [length]; 20 21 // read data as a block: 22 FileIn.read (*io_sourcebuffer,length); 23 24 FileIn.close(); 25 26 return true; 27 } Checking to see if GLSL available 1 void InitGLSL(void) 2 { 3 4 glewinit(); 5 if (GLEW_ARB_vertex_shader && GLEW_ARB_fragment_shader) 6 printf("ready for GLSL\n"); 7 else { 8 printf("no GLSL support\n"); 9 exit(1); 10 } 11 12 }
1 GLhandleARB InstallShaders( 2 const std::string &_vertexshader, 3 const std::string &_fragmentshader 4 ) 5 { 6 GLhandleARB vertex; 7 GLhandleARB fragment; 8 GLhandleARB shaderprog; 9 GLint vertcompiled; 10 GLint fragcompiled; // status values 11 GLint linked; 12 13 // Create a vertex shader object and a fragment shader object 14 vertex = glcreateshaderobjectarb(gl_vertex_shader_arb); 15 fragment = glcreateshaderobjectarb(gl_fragment_shader_arb); 16 std::cout <<"Shader ID's VS="<<vertex<<" FS="<<fragment<<std::endl; 17 // Load source code strings into shaders 18 char *shadersource; 19 ReadShader(_fragmentShader,&shadersource); 20 glshadersourcearb(fragment, 1, (const GLcharARB**)&shadersource, NULL); 21 glcompileshaderarb(fragment); 22 PrintOpenGLError(); // Check for OpenGL errors 23 glgetobjectparameterivarb(fragment,gl_object_compile_status_arb, &fragcompiled); 24 PrintInfoLog(fragment); 25 delete [] shadersource; 26 27 ReadShader(_vertexShader,&shadersource); 28 glshadersourcearb(vertex, 1, (const GLcharARB**)&shadersource, NULL); 29 glcompileshaderarb(vertex); 30 PrintOpenGLError(); // Check for OpenGL errors 31 glgetobjectparameterivarb(vertex,gl_object_compile_status_arb, &vertcompiled); 32 PrintInfoLog(vertex); 33 34 delete [] shadersource; 35 36 // compile the shader and print out the results 37 if (!vertcompiled!fragcompiled) 38 { 39 std::cout << "Compilation error"<<" Vert = "<<vertcompiled<<" Frag = "<<fragcompiled<< std::endl; return 0; 40 41 } 42 // Create a program object and attach the two compiled shaders 43 44 shaderprog = glcreateprogramobjectarb(); 45 glattachobjectarb(shaderprog, vertex); 46 glattachobjectarb(shaderprog, fragment); 47 // Link the program object and print out the info log 48 gllinkprogramarb(shaderprog); 49 PrintOpenGLError(); // Check for OpenGL errors 50 51 glgetobjectparameterivarb(shaderprog,gl_object_link_status_arb, &linked); 52 PrintInfoLog(shaderProg); 53 54 if (!linked) 55 { 56 std::cout << "link error"<<std::endl; 57 return false; 58 } 59 60 // Install program object as part of current state 61 62 gluseprogramobjectarb(shaderprog); 63 return shaderprog; 64 } Using the Shaders Once the shader is loaded and made active it replaces the Fixed Function OpenGL Pipeline 1 void LoadShaders( 2 GLhandleARB &_currentshader 3 ) 4 { 5 _currentshader=installshaders("../shaders/marble.vertex","../shaders/marble.fragment"); 6 gluniform3farb(getuniloc(_currentshader, "LightPosition"), 0.0, 1.0, 0.0); 7 gluniform3farb(getuniloc(_currentshader, "color_a"), 1.0, 0.3, 0.1); 8 gluniform3farb(getuniloc(_currentshader, "color_b"), 0.2, 0.3, 0.2); 9 gluniform3farb(getuniloc(_currentshader, "stretch"),2, 2, 2); 10 } 1 case SDLK_1 : gluseprogramobjectarb(currentshader); break; 2 case SDLK_2 : gluseprogramobjectarb(null); break; 3 4 case SDLK_EQUALS : 5 Stretch+=0.5; 6 gluniform3farb(getuniloc(currentshader, "stretch"),stretch,stretch,stretch); 7 break; 8 9 case SDLK_MINUS : 10 Stretch-=0.5; 11 gluniform3farb(getuniloc(currentshader, "stretch"),stretch,stretch,stretch); 12 break; minimal Shaders Using Multiple Shaders 1 // Min Fragment Shader 2 3 void main() 4 { 5 // set to red 6 gl_fragcolor=vec4(1.0,0.0,0.0,1.0); 7 } 8 9 // min vertex shader 10 11 void main() 12 { 13 // set the position using built in 14 gl_position= ftransform(); 15 } For each shader we need to load, compile and link We then store the Handle to the shader Object. When we wish to use the shader object we use a call to gluseprogramobjectarb
Setting Shader Parameters To Set parameters to a shader we use the following code to get access to the shader Object and Parameter Setting Shader Parameters Depending Upon the Parameter to be set we use the following 1 GLint getuniloc(glhandlearb program, const GLcharARB *name) 2 { 3 GLint loc; 4 5 loc = glgetuniformlocationarb(program, name); 6 7 if (loc == -1) 8 printf("no such uniform named \"%s\"\n", name); 9 10 return loc; 11 } 1 p[2] = LoadShaders("../Shaders/brick.vert","../Shaders/brick.frag"); 2 gluseprogramobjectarb(p[2]); 3 4 gluniform3farb(getuniloc(p[2], "BrickColor"), 1.0, 0.3, 0.2); 5 gluniform3farb(getuniloc(p[2], "MortarColor"), 0.85, 0.86, 0.84); 6 gluniform2farb(getuniloc(p[2], "BrickSize"), 0.30, 0.15); 7 gluniform2farb(getuniloc(p[2], "BrickPct"), 0.90, 0.85); 8 gluniform3farb(getuniloc(p[2], "LightPosition"), 0.0, 2.0, 4.0); Brick Fragment Shader 1 // See 3Dlabs-License.txt for license information 2 uniform vec3 BrickColor, MortarColor; 3 uniform vec2 BrickSize; 4 uniform vec2 BrickPct; 5 6 varying vec2 MCposition; 7 varying float LightIntensity; 8 9 void main(void) 10 { 11 vec3 color; 12 vec2 position, usebrick; 13 14 position = MCposition / BrickSize; 15 16 if (fract(position.y * 0.5) > 0.5) 17 position.x += 0.5; 18 19 position = fract(position); 20 21 usebrick = step(position, BrickPct); 22 23 color = mix(mortarcolor, BrickColor, usebrick.x * usebrick.y ); 24 color *= LightIntensity; 25 gl_fragcolor = vec4 (color, 1.0); 26 } Brick Vertex Shader 1 // Copyright (c) 2002-2004 3Dlabs Inc. Ltd. 2 uniform vec3 LightPosition; 3 4 const float SpecularContribution = 0.7; 5 const float DiffuseContribution = 1.0 - SpecularContribution; 6 7 varying float LightIntensity; 8 varying vec2 MCposition; 9 10 void main(void) 11 { 12 vec3 ecposition = vec3 (gl_modelviewmatrix * gl_vertex); 13 vec3 tnorm = normalize(gl_normalmatrix * gl_normal); 14 vec3 lightvec = normalize(lightposition - ecposition); 15 vec3 reflectvec = reflect(-lightvec, tnorm); 16 vec3 viewvec = normalize(-ecposition); 17 float diffuse = max(dot(lightvec, tnorm), 0.0); 18 float spec = 0.0; 19 20 if (diffuse > 0.0) 21 { 22 spec = max(dot(reflectvec, viewvec), 0.0); 23 spec = pow(spec, 16.0); 24 } 25 26 LightIntensity = DiffuseContribution * diffuse + 27 SpecularContribution * spec; 28 29 MCposition = gl_vertex.xy; 30 gl_position = ftransform(); 31 }
Shader Basic Structure A shader is a sequence of declarations and function bodies Curly braces are used to group sequences of statements A shader must have a main function Statements end with a semi-colon Basic Types float, vec2, vec3, vec4 1, 2, 3, or 4 floating point values Preferred data types for most processing int, ivec2, ivec3, ivec4 1, 2, 3, or 4 integer values Integer for loops and array index bool, bvec2, bvec3, bvec4 1, 2, 3, or 4 boolean values As in C++, contains true or false mat2, mat3, mat4 Floating point square matrix Used to perform transformation operations Basic Types void Used for functions that do not return a value sampler1d, sampler2d, sampler3d Handles for accessing 1D, 2D, and 3D textures samplercube Handle for accessing a cube map texture sampler1dshadow, sampler2dshadow Handles for accessing 1D or 2D depth textures with an implicit comparison operation All Used in conjunction with texture access functions 1 float ramp[10]; 2 vec4 colors[4]; 3 bool results[3]; Arrays structures can be aggregated into arrays Only 1D arrays are supported Size of array can be expressed as an integral constant expression within square brackets ([ ]) Arrays can be declared without a size, and then re-declared later with the same type and a size Using an index that goes beyond an array s bounds results in undefined behavior
structs 1 struct surfacematerial 2 { 3 float ambient; 4 float diffuse; 5 float specular; 6 vec3 basecolor; 7 } surf; 8 9 surfmaterial surf1, surf2; User-defined types can be created using struct with previously defined types Creates a new type called surfacematerial Defines variables of this type called surf, surf1, and surf2 Structures can include arrays Fields are selected using the period (. ) Structure Constructors Constructor for a structure is available once structure is defined Example: 1 struct light 2 { 3 float intensity; 4 vec3 position; 5 }; 6 7 light newlight = light(3.0, vec3(1.0, 2.0, 3.0)); Type Qualifiers const variable is a constant and can only be written during its declaration attribute per-vertex data values provided to the vertex shader uniform (relatively) constant data provided by the application or by OpenGL for use in the shader varying a perspective-correct interpolated value output for vertex shader input for fragment shader in for function parameters copied into a function, but not copied out out for function parameters copied out of a function, but not copied in inout for function parameters copied into and out of a function Vector Components Vector components can be referred to using array syntax or a single letter: [0], [1], [2], [3] r, g, b, a x, y, z, w s, t, p, q This syntax can be used to extract, duplicate, or swizzle components 1 vec4 pos = vec4(1.0, 2.0, 3.0, 4.0); 2 vec4 swiz= pos.wzyx; // swiz = (4.0, 3.0, 2.0, 1.0) 3 vec4 dup = pos.xxyy; // dup = (1.0, 1.0, 2.0, 2.0) 4 5 pos.xw = vec2(5.0, 6.0); // pos = (5.0, 2.0, 3.0, 6.0) 6 pos.wx = vec2(7.0, 8.0); // pos = (8.0, 2.0, 3.0, 7.0) 7 pos.xx = vec2(3.0, 4.0); // illegal - 'x' used twice
Matrix Components Matrix components can be accessed using array subscripting syntax A single subscript selects a single column A second subscript selects a component within a column 1 mat4 m; 2 m[1] = vec4(2.0); // sets the second column to all 2.0 3 m[0][0] = 1.0; // sets the upper left element to 1.0 4 m[2][3] = 2.0; // sets the 4th element of the third 5 // column to 2.0 Declaration Function Examples 1 vec3 computecolor (in vec3 c1, in vec3 c2); 2 float radians (float degrees); Definition 1 float myfunc (in float f1, // f1 is copied in 2 inout float f2) // f2 is copied in and out 3 { 4 float myresult; 5 6 // do computations 7 8 return myresult; 9 } Vertex Shader Built-in Variables The following special variables are available in a vertex shader: 1 vec4 gl_position; // must be written to 2 float gl_pointsize; // may be written to 3 vec4 gl_clipvertex; // may be written to Every execution of a vertex shader must write the homogeneous vertex position into gl_position Can use the built-in function ftransform() to achieve invariance with fixed functionality Vertex shaders may write the size of points to be rasterized (measured in pixels) into the built-in variable gl_pointsize Vertex shaders may write the transformed coordinate to be used in conjunction with user clipping planes into gl_clipvertex Vertex Shader Built-in Attributes The following are available from a vertex shader for accessing standard OpenGL vertex attributes: 1 attribute vec4 gl_color; 2 attribute vec4 gl_secondarycolor; 3 attribute vec3 gl_normal; 4 attribute vec4 gl_vertex; 5 attribute vec4 gl_multitexcoord0; 6 attribute vec4 gl_multitexcoord1; 7 attribute vec4 gl_multitexcoord2; 8 attribute vec4 gl_multitexcoord3; 9 attribute vec4 gl_multitexcoord4; 10 attribute vec4 gl_multitexcoord5; 11 attribute vec4 gl_multitexcoord6; 12 attribute vec4 gl_multitexcoord7; 13 attribute float gl_fogcoord;
Built-in Functions Trigonometry/angle radians, degrees, sin, cos, tan, asin, acos, atan Exponential pow, exp2, log2, sqrt, inversesqrt Common abs, sign, floor, ceil, fract, mod, min, max, clamp, mix, step, smoothstep Geometric and matrix length, distance, dot, cross, normalize, ftransform, faceforward, reflect, matrixcompmult Vector relational lessthan, lessthanequal, greaterthan, greaterthanequal, equal, any, all Texture lookup texture1d/2d/3d, texture1d/2d/3dproj, texturecube, texture1d/2dshadow, texture1d/2dshadowproj Fragment shader only dfdx, dfdy, fwidth Noise noise1/2/3/4 Accessing Built In Pipeline GLSL has the ability to access the built in Light and Material Properties Two main structures for this are gl_frontmaterial gl_lightsource[gl_maxlights] The parameters for these (and others) are show in the following slide 1 /// @brief[in] the vertex normal 2 varying vec3 v_n; 3 4 /// @brief[in] the number of active lights to evaluate 5 /// this must be set before the shader is used. 6 uniform int numlights; 7 8 void main () 9 { Blinn Shader 10 // set the output colour to black 11 vec4 colour= vec4(0.0); 12 // normalize the vertex normal 13 vec3 N = normalize(v_n); 14 // The Light source vector 15 vec3 L; 16 // the Halfway vector (used for speed) 17 vec3 H; 18 // pre declare the colour contribution values 19 vec4 ambient; 20 vec4 diffuse; 21 vec4 specular; 22 23 // now loop for active lights 24 25 for (int i=0; i<numlights; ++i) 26 { 27 // get the Light vector 28 L = normalize(gl_lightsource[i].position.xyz); 29 // get the halfway vector 30 H = normalize(gl_lightsource[i].halfvector.xyz); 31 // ambient just added 32 ambient = gl_frontmaterial.ambient *gl_lightsource[i].ambient; 33 // calculate diffuse based on Lambert's law (L.N) 34 diffuse = gl_frontmaterial.diffuse *gl_lightsource[i].diffuse * max(dot(l, N), 0.0); 35 // calculate specular based on H.NˆShininess 36 specular = gl_frontmaterial.specular *gl_lightsource[i].specular * pow(max(dot(h, N), 0.0), gl_frontmaterial.shininess); combine contribution for the light 37 // 38 colour+=ambient+diffuse+specular; 39 } 40 // finally set the colour clamping between 0 and 1 41 gl_fragcolor = clamp(vec4(colour),0.0,1.0); 42 43 }
References OpenGL Shading Language Addison-Wesley R J Rost 3DLabs OpenGL Shading Language Master Class Lecture Notes 2004 www.lighthouse3d.com Apple GLSL Shader Examples