570 likes | 843 Vues
Computer Graphics 3 Lecture 5: OpenGL Shading Language (GLSL). Dr. Benjamin Mora. University of Wales Swansea. 1. Benjamin Mora. Content. Introduction. How to include GLSL programs in your code. GLEW, API Variables and Types. Vertex Processor. Fragment Processor. Samplers
E N D
Computer Graphics 3Lecture 5:OpenGL Shading Language(GLSL) Dr. Benjamin Mora University of Wales Swansea 1 Benjamin Mora
Content • Introduction. • How to include GLSL programs in your code. • GLEW, API • Variables and Types. • Vertex Processor. • Fragment Processor. • Samplers • Built-in functions. • Applications. University of Wales Swansea 2 Benjamin Mora
Introduction University of Wales Swansea 3 Benjamin Mora
Vertex Programs: User-Defined Vertex Processing Transform And Lighting Setup Rasterization Texture Fetch, Fragment Shading Fragment Programs: User-Defined Per-Pixel Processing Frame Buffer Blending Vertex and Fragment Programs Vertices Tests (z, stencil…) University of Wales Swansea 4 Benjamin Mora
Introduction • Introduced with OpenGL 2.0. • High Level Language. • Real shaders implementation hidden inside drivers. • C/C++ like coding. • Some differences. • Stronger typing. • Language still heavily influenced by current hardware design. • Still somehow messy… • Compatible with future pipelines. • Replaces fixed vertex and pixel pipelines. • Geometry shader available as an extension. • OpenGL 3.0 adds some functionalities. University of Wales Swansea 5 Benjamin Mora
How to code/debug • Understanding of the whole pipeline needed. • Thorough analysis of the algorithm/solution first. • Favor simple solutions unless big performance issues. • Start with a very simple shader that achieves a simple task. • Test and iterate your code until program is finalized. • Frame rate proportional to code efficiency. • Thoroughly analyze your problem again… • Check for redundancies, memory access, etc… • Use textures for emulating/pre-computing complex functions University of Wales Swansea 6 Benjamin Mora
How to code/debug • Debugging: • Again difficult. • Can test variables/temporary results by returning specific colors. • Tools: • RenderMonkey (ATI). • glslDevil • http://www.vis.uni-stuttgart.de/glsldevil/ • gDEBugger (30-days trial version). University of Wales Swansea 7 Benjamin Mora
Shader Loading • Shaders should be ideally stored in a separate text file. • Needs to be loaded into your program as a string (char *) variable, and then sent to the API. • Several different shaders can be stored by the API and interchanged during rendering. • Cannot be changed between glBegin(…) and glEnd() calls. VertexProgram.shader myProgram.c void main() { gl_Position=gl_ModelviewProjectionMatrix * gl_Vertex; } … Char *myVertexProgram; LoadText(myVertexProgram, “VertexProgram.shader”); //Use now the OpenGL 2.0 API //to compile and enable the program … University of Wales Swansea 8 Benjamin Mora
Shader Loading • Loading a shader object (vertex or fragment) requires several steps: • Create a shader object • glCreateShader. • Associate the source code to the shader object • glShaderSource. • Compile the shader. • glCompileShader • Attach the shader to a program object (container for shaders) • glAttachShader • Link the compiled shader to a program. • glLinkShader • Replace the fixed pipeline with the program object. • glUseProgram. University of Wales Swansea 9 Benjamin Mora
Shader Loading:Example char *myVertexProgram; char *myFragmentProgram; GLuint vShader, fShader, program; … vShader=glCreateShader(GL_VERTEX_SHADER); fShader=glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(vShader, 1, & myVertexProgram, NULL); glShaderSource(fShader, 1, & myFragmentProgram, NULL); glCompileShader(vShader); glCompileShader(fShader); //Source strings can now be deleted University of Wales Swansea 10 Benjamin Mora
Shader Loading:Example program=glCreateProgram(); //Creates a program object glAttachShader(program, vShader); glAttachShader(program, fShader); glLinkProgram(program); glUseProgram(program); //can come back to a fixed pipeline by passing NULL instead … //Don’t forget : //Objects must be deleted when not needed anymore University of Wales Swansea 11 Benjamin Mora
Variables and Types University of Wales Swansea 12 Benjamin Mora
Types • Simple types. • Structures (struct keyword) and arrays possible. • No Pointer! • Implicit conversion generally not possible. • Scalar types: • float, bool, int • int at least in the range [-65535, 65535] • Declaration: • float f,g=1.0; University of Wales Swansea 13 Benjamin Mora
Types • Vector types: • vec2, vec3, vec4: Vectors of floats. • bvec2, bvec3, bvec4: Vectors of booleans. • ivec2, ivec3, ivec4: Vectors of integers. • Declaration: vec3 v=vec3(1.0,0.0,3.0); • Vector components: • .xyzw, for vectors representing positions. • .rgba, for vectors representing colors • .stqp, for vectors representing texture coordinates. • Designation not compulsory. University of Wales Swansea 14 Benjamin Mora
Types • Swizzling examples: • float f; vec4 v; vec2 v2=v.ww; vec3 v3=v.xzy; v2=vec2(3.0,-1.0); v2=texture1D(sampler,coordinate).xy; v=v+f; //f is added to the 4 components of v! v+=v; //Component-wise addition University of Wales Swansea 15 Benjamin Mora
Types • Matrices (of floats, square): • mat2, mat3, mat4; • mat4 m; vec4 v=m[2]; float f=m[2][2]; • Row and columns inverted in OpenGL conventions! • m[2] is the third column of the matrix. • Don’t use oversized vector and matrices if not required. University of Wales Swansea 16 Benjamin Mora
Types • Structure : • Struct light { vec3 position; vec3 color; float watt; //could be actually stored with color } light myLight; • No typedef! University of Wales Swansea 17 Benjamin Mora
Types • Arrays: • vec3 vertices[20]; • vec3 vertices2[]; //Also possible. Size must be determinable at compilation //time. See manual & specs. • Special case: texture coordinate array. • Internally declared as: • varyingvec4 gl_TexCoord[]; University of Wales Swansea 18 Benjamin Mora
Types • Samplers • Texture variables. • sampler1D • sampler2D • sampler3D • samplerCube • sampler1DShadow • sampler2DShadow • Declaration: • Uniformsampler2D brick; vec4 col=texture2D(brick, texCoordinate); University of Wales Swansea 19 Benjamin Mora
Types: Samplers • Example • C/C++ core program: glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, marbleTex); texLoc=glGetUniformLocation(myProgram, “marbleTexture”); glUniform1i(texLoc,0); • Vertex Program: varying vec2 coord; … coord = gl_MultiTexCoord0.st;//Get the tex coordinates. University of Wales Swansea 20 Benjamin Mora
Types: Samplers • Example • Fragment Program: varying vec2 coord; uniform sampler2D marbleTexture; //texture object. … gl_FragColor = texture2D(marbleTexture, coord); University of Wales Swansea 21 Benjamin Mora
Types • Qualifiers: • attribute • For frequently changing variables, typically what would be passed to OpenGL between glBegin(…) and glEnd(). • Built-in attributes include gl_Vertex, gl_Normal,… • uniform • For not-so-frequently changing variables, typically what would be passed to OpenGL outside of a glBegin(…)/glEnd() section. • At most changed once per primitive. • Read-only. University of Wales Swansea 22 Benjamin Mora
Types • Qualifiers: • varying • For variables passed from the vertex shader to the fragment shader. These variables undergo a linear interpolation. • Variable declation must be consistent across the vertex and fragment programs. • Perspectively correct. • const • Variable value fixed at compilation time. Cannot be modifier • The first 3 qualifiers must be global variables. • No qualifier means a read/write variable local to the shader. University of Wales Swansea 23 Benjamin Mora
Types • Functions: • Functions can be written locally. • Call by value-return. • Parameter qualifiers: • in • out • inout • const • In addition to previous qualifiers • Example: • float norm(invec3 v) {… University of Wales Swansea 24 Benjamin Mora
Built-In Variables • GLSL pre-declares many (useful) variables. • Input/Output variables are used for communication between programs. • Additional attributes can also be specified. • Implementation dependent. • A minimum number defined by OpenGL. • glGet(GL_MAX_VERTEX_ATTRIBS); • See later. University of Wales Swansea 25 Benjamin Mora
Predefined Vertex Variables • attributevec4 gl_Color; • attributevec4 gl_SecondaryColor; • attributevec3 gl_Normal; • attributevec4 gl_MultiTexCoord0; • attributevec4 gl_MultiTexCoord1; • const int gl_MaxTextureCoords; • … • attributevec4 gl_FogCoord; University of Wales Swansea 26 Benjamin Mora
Vertex Ouput Variables • vec4 gl_Position; • vec4 gl_ClipVertex; • float gl_PointSize; University of Wales Swansea 27 Benjamin Mora
Vertex Varying Ouput Variables • varying vec4 gl_FrontColor; • varying vec4 gl_BackColor; • varying vec4 gl_FrontSecondary; • varying vec4 gl_BackSecondary; • varying vec4 gl_TexCoord[]; • float gl_FogFragCoord; University of Wales Swansea 28 Benjamin Mora
Special Fragment Input Variables • varying vec4 gl_Color; • varying vec4 gl_SecondaryColor; • varying vec4 gl_TexCoord[]; • varying float gl_FogFragCoord; University of Wales Swansea 29 Benjamin Mora
Special Fragment Input Variables • bool gl_FrontFacing; • vec4 gl_FragCoord; University of Wales Swansea 30 Benjamin Mora
Fragment Output Variables • vec4 gl_FragColor; • vec4 gl_FragData; • float gl_FragDepth; • //gl_FragCoord.z by default • These variables have a global scope. University of Wales Swansea 31 Benjamin Mora
Built-In Constants • const intgl_MaxClipPlanes; • const intgl_MaxCombinedTextureImageUnits; • const intgl_MaxFragmentUniformComponents; • const int gl_MaxVertexAttribs; • const int gl_MaxVaryingFloats; • const int gl_MaxDrawBuffers; • const int gl_MaxTextureCoords; • const int gl_MaxTextureUnits; • const int gl_MaxTextureImageUnits; • const int gl_MaxVertexTextureImageUnits; • const int gl_MaxLights; University of Wales Swansea 32 Benjamin Mora
Built-In Uniform Variables • uniform mat4 gl_ModelViewMatrix; • uniform mat4 gl_ModelViewProjectionMatrix; • uniform mat4 gl_ProjectionMatrix; • uniform mat4 gl_TextureMatrix[gl_MaxTextureCoords]; • uniform mat4 gl_ModelViewMatrixInverse; • uniform mat4 gl_ModelViewProjectionMatrixInverse; • uniform mat4 gl_ProjectionMatrixInverse; • uniform mat4 gl_TextureMatrixInverse[gl_MaxTextureCoords]; University of Wales Swansea 33 Benjamin Mora
Built-In Uniform Variables • uniform mat4 gl_ModelViewMatrixTranspose; • uniform mat4 gl_ModelViewProjectionMatrixTranspose; • uniform mat4 gl_ProjectionMatrixTranspose; • uniform mat4 gl_TextureMatrixTranspose[gl_MaxTextureCoords]; • uniform mat4 gl_ModelViewMatrixInverseTranspose; • uniform mat4 gl_ModelViewProjectionMatrixInverseTranspose; • uniform mat4 gl_ProjectionMatrixInverseTranspose; • uniform mat4 gl_TextureMatrixInverseTranspose[gl_MaxTextureCoords]; • uniform mat3 gl_NormalMatrix; • uniform float gl_NormalScale; University of Wales Swansea 34 Benjamin Mora
Built-In Uniform Variables • struct gl_LightSourceParameters { vec4 ambient; vec4 diffuse; vec4 specular; vec4 position; vec4 halfVector; vec3 spotDirection; float spotExponent; float spotCutoff; float spotCosCutoff; float constantAttenuation; float linearAttenuation; float quadraticAttenuation; }; • uniform gl_LightSourceParametersgl_LightSource[gl_MaxLights]; • etc… University of Wales Swansea 35 Benjamin Mora
Vertex and FragmentProcessors University of Wales Swansea 36 Benjamin Mora
Vertex and Fragment Processors • Replaces the fixed pipeline. • Input/Ouput Data: Attribute or Uniform variables. • Built-In or User defined. • Uses “Varying Data” for the communication of Linearly interpolated Values between the vertex and the fragment program. University of Wales Swansea 37 Benjamin Mora
Vertex Processor • Modelview and projection matrices not applied. • Normals not transformed to eye-coordinate. • Normals not normalized. • Texture coordinates not processed. • Lighting not performed. • Color material computations not performed. • … University of Wales Swansea 38 Benjamin Mora
Vertex Processor • After the vertex program, the following fixed functionalities are still applied: • Color clamping. • Perspective division. • Viewport mapping. • Depth range scaling. • Additional Vertex Attributes can be send from the main program. • Additional colors, tangents, curvatures… University of Wales Swansea 39 Benjamin Mora
Passing More Vertex Attributes Main C/C++ program: • Texture coordinates can be used. • Not best. • glVertexAttrib function. • void glVertexAttrib2dv(GLuint index, const GLdouble *v); • void glVertexAttrib4s(GLuint index, GLshort v0, GLshort v1, GLshort v2, GLshort v3) ; • void glVertexAttrib4fv(GLuint index, const GLfloat *v); • etc… • Index at least in the range [0..16] • Attrib 0 indicates the completion of a vertex. • Version for normalized data available… University of Wales Swansea 40 Benjamin Mora
Passing More Vertex Attributes • How to associate a fragment program variable with an attribute index in the C/C++ program? • Use glBindAttribLocation function. • void glBindAttribLocation(GLuint program, GLuint index, const GLchar *name); • glBindAttribLocation(myProgram, 1, “objectTangent”); • Must be done before calling the linker. University of Wales Swansea 41 Benjamin Mora
Passing More Vertex Attributes Main C/C++ program: • glVertexAttribPointer. • void glVertexAttribPointer(GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const GLvoid *pointer); • Similar to vertex arrays. Arrays can now be stored in video memory and processed optimally. • Vertex attrib arrays possible. • Enabling/Disabling Attrib arrays: • void glEnableVertexAttribArray(GLuint index); • void glDisableVertexAttribArray(GLuint index); • Arrays used when making a call to glDrawArrays, glDrawElements, etc… University of Wales Swansea 42 Benjamin Mora
Passing More Vertex Attributes Uniform variables: • Setup in a different way than attribute variables. • After linking the program, the main application (C/C++) must query the location of the uniform variable, and then set its value. • GLint glGetUniformLocation (GLuint program, const GLchar *name) : • Look for a specific variable. • Returns the location. • void glUniform{1|2|3|4}{f|i} (Glint location, TYPE v); • Set the uniform value. Should not happen between glBegin/glEnd. University of Wales Swansea 43 Benjamin Mora
Fragment Processor • The fragment program mainly processes interpolated information generated from the vertex program. • e.g. gl_Color. • The fragment program must replace/code: • Texture mapping environments & functions. • Texture application. • Color application/generation. • Shading. • Fog application. University of Wales Swansea 44 Benjamin Mora
Built-In Functions University of Wales Swansea 45 Benjamin Mora
Built-In Functions • Easy Shader Development. • Readability. • Simplicity. • Common functions needed for graphics. • Mask the actual hardware implementation. • The compiler has to be efficient/clever. • No warranty that a function is hardware accelerated. • Non-accelerated functions could be slower. • Most of them available from both programs. University of Wales Swansea 46 Benjamin Mora
Built-In Functions • genType = float | vec2 | vec3 | vec4 • Trigonometry Functions. • genType sin( genType ); • genType cos( genType ); • genType tan( genType ); • genType asin( genType ); • genType acos( genType ); • genType atan( genType, genType ); • genType atan( genType ); • genType radians( genType ); • genType degrees( genType ); University of Wales Swansea 47 Benjamin Mora
Built-In Functions • Inverse, Exponential and square root functions. • genType pow( genType, genType ); • genType exp( genType ); • genType log( genType ); • genType exp2( genType ); • genType log2( genType ); • genType sqrt( genType ); • genType inversesqrt( genType ); University of Wales Swansea 48 Benjamin Mora
Built-In Functions • Common functions • Min, Max, Clamping, Linear interpolation (Mix), modulo, floor, frac, step functions. • genType abs( genType ); • genType ceil( genType ); • genType clamp( genType, genType, genType ); • genType clamp( genType, float, float ); • genType floor( genType ); • genType fract( genType ); • genType max( genType, genType ); • genType max( genType, float ); University of Wales Swansea 49 Benjamin Mora
Built-In Functions • Common functions • genType mix( genType, genType, genType ); • genType mix( genType, genType, float ); • genType mod( genType, genType ); • genType mod( genType, float ); • genType sign( genType ); • genType smoothstep( genType, genType, genType ); • genType smoothstep( float, float, genType ); • genType step( genType, genType ); • genType step( float, genType ); University of Wales Swansea 50 Benjamin Mora