Download presentation
Presentation is loading. Please wait.
Published byJob York Modified over 9 years ago
1
Computer Graphics 3 Lecture 5: OpenGL Shading Language (GLSL)
Dr. Benjamin Mora University of Wales Swansea 1 Benjamin Mora
2
University of Wales Swansea
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
3
University of Wales Swansea
Introduction University of Wales Swansea 3 Benjamin Mora
4
Vertex and Fragment Programs
Vertices Transform And Lighting Vertex Programs: User-Defined Vertex Processing Setup Rasterization Tests (z, stencil…) Texture Fetch, Fragment Shading Fragment Programs: User-Defined Per-Pixel Processing Frame Buffer Blending University of Wales Swansea 4 Benjamin Mora
5
University of Wales Swansea
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
6
University of Wales Swansea
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
7
University of Wales Swansea
How to code/debug Debugging: Again difficult. Can test variables/temporary results by returning specific colors. Tools: RenderMonkey (ATI). glslDevil gDEBugger (30-days trial version). University of Wales Swansea 7 Benjamin Mora
8
University of Wales Swansea
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
9
University of Wales Swansea
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
10
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
11
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
12
University of Wales Swansea
Variables and Types University of Wales Swansea 12 Benjamin Mora
13
University of Wales Swansea
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
14
University of Wales Swansea
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
15
University of Wales Swansea
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
16
University of Wales Swansea
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
17
University of Wales Swansea
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
18
University of Wales Swansea
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: varying vec4 gl_TexCoord[]; University of Wales Swansea 18 Benjamin Mora
19
University of Wales Swansea
Types Samplers Texture variables. sampler1D sampler2D sampler3D samplerCube sampler1DShadow sampler2DShadow Declaration: Uniform sampler2D brick; vec4 col=texture2D(brick, texCoordinate); University of Wales Swansea 19 Benjamin Mora
20
University of Wales Swansea
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
21
University of Wales Swansea
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
22
University of Wales Swansea
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
23
University of Wales Swansea
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
24
University of Wales Swansea
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(in vec3 v) {… University of Wales Swansea 24 Benjamin Mora
25
University of Wales Swansea
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
26
Predefined Vertex Variables
attribute vec4 gl_Color; attribute vec4 gl_SecondaryColor; attribute vec3 gl_Normal; attribute vec4 gl_MultiTexCoord0; attribute vec4 gl_MultiTexCoord1; const int gl_MaxTextureCoords; … attribute vec4 gl_FogCoord; University of Wales Swansea 26 Benjamin Mora
27
Vertex Ouput Variables
vec4 gl_Position; vec4 gl_ClipVertex; float gl_PointSize; University of Wales Swansea 27 Benjamin Mora
28
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
29
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
30
Special Fragment Input Variables
bool gl_FrontFacing; vec4 gl_FragCoord; University of Wales Swansea 30 Benjamin Mora
31
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
32
University of Wales Swansea
Built-In Constants const int gl_MaxClipPlanes; const int gl_MaxCombinedTextureImageUnits; const int gl_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
33
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
34
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
35
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_LightSourceParameters gl_LightSource[gl_MaxLights]; etc… University of Wales Swansea 35 Benjamin Mora
36
Vertex and Fragment Processors
University of Wales Swansea 36 Benjamin Mora
37
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
38
University of Wales Swansea
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
39
University of Wales Swansea
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
40
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
41
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
42
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
43
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
44
University of Wales Swansea
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
45
University of Wales Swansea
Built-In Functions University of Wales Swansea 45 Benjamin Mora
46
University of Wales Swansea
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
47
University of Wales Swansea
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
48
University of Wales Swansea
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
49
University of Wales Swansea
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
50
University of Wales Swansea
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
51
University of Wales Swansea
Built-In Functions 3D functions and Matrix functions. dot product, length, multiplications… vec4 ftransform(); //Vertex ONLY. Same transform as //done with a fixed pipeline. A direct ModelviewProjection //multiplication may lead to a slightly different result. vec3 cross( vec3, vec3 ); float distance( genType, genType ); float dot( genType, genType ); genType faceforward ( genType V, genType I, genType N ); float length( genType ); genType normalize( genType ); genType reflect( genType I, genType N ); genType refract( genType I, genType N, float eta ); mat matrixCompMult( mat, mat ); University of Wales Swansea 51 Benjamin Mora
52
University of Wales Swansea
Built-In Functions Texture Lookup functions //Optional bias term is Fragment ONLY vec4 texture1D( sampler1D, float [,float bias] ); vec4 texture1DProj( sampler1D, vec2 [,float bias] ); vec4 texture1DProj( sampler1D, vec4 [,float bias] ); vec4 texture2D( sampler2D, vec2 [,float bias] ); vec4 texture2DProj( sampler2D, vec3 [,float bias] ); vec4 texture2DProj( sampler2D, vec4 [,float bias] ); University of Wales Swansea 52 Benjamin Mora
53
University of Wales Swansea
Built-In Functions Texture Lookup functions vec4 texture3D( sampler3D, vec3 [,float bias] ); vec4 texture3DProj( sampler3D, vec4 [,float bias] ); vec4 textureCube( samplerCube, vec3 [,float bias] ); vec4 shadow1D( sampler1DShadow, vec3 [,float bias] ); vec4 shadow2D( sampler2DShadow, vec3 [,float bias] ); vec4 shadow1DProj( sampler1DShadow, vec4 [,float bias] ); vec4 shadow2DProj( sampler2DShadow, vec4 [,float bias] ); University of Wales Swansea 53 Benjamin Mora
54
University of Wales Swansea
Built-In Functions Texture Lookup functions //Vertex ONLY; ensure //GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS > 0 vec4 texture1DLod( sampler1D, float, float lod ); vec4 texture1DProjLod( sampler1D, vec2, float lod ); vec4 texture1DProjLod( sampler1D, vec4, float lod ); vec4 texture2DLod( sampler2D, vec2, float lod ); vec4 texture2DProjLod( sampler2D, vec3, float lod ); vec4 texture2DProjLod( sampler2D, vec4, float lod ); University of Wales Swansea 54 Benjamin Mora
55
University of Wales Swansea
Built-In Functions Texture Lookup functions //Vertex ONLY; ensure //GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS > 0 vec4 texture3DProjLod( sampler3D, vec4, float lod ); vec4 textureCubeLod( samplerCube, vec3, float lod ); vec4 shadow1DLod( sampler1DShadow, vec3, float lod ); vec4 shadow2DLod( sampler2DShadow, vec3, float lod ); vec4 shadow1DProjLod( sampler1DShadow, vec4, float lod ); vec4 shadow2DProjLod( sampler2DShadow, vec4, float lod ); University of Wales Swansea 55 Benjamin Mora
56
University of Wales Swansea
Built-In Functions Other functions: float noise1( genType ); vec2 noise2( genType ); vec3 noise3( genType ); vec4 noise4( genType ); genType dFdx( genType ); genType dFdy( genType ); genType fwidth( genType ); … University of Wales Swansea 56 Benjamin Mora
57
Application: Phong Shading
Show an extreme example with a specular lighting. Ian Fergusson, University of Wales Swansea 57 Benjamin Mora
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.