computer graphics 3 lecture 5: opengl shading language (glsl)

Post on 16-Jan-2016

67 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

Computer Graphics 3Lecture 5:OpenGL

Shading Language(GLSL)

Benjamin Mora 1University of Wales

Swansea

Dr. Benjamin Mora

Content

2Benjamin MoraUniversity of Wales

Swansea

• Introduction.

• How to include GLSL programs in your code.– GLEW, API

• Variables and Types.

• Vertex Processor.

• Fragment Processor.

• Samplers

• Built-in functions.

• Applications.

Introduction

3Benjamin MoraUniversity of Wales

Swansea

Vertex and Fragment Programs

4Benjamin MoraUniversity of Wales

Swansea

Setup

Rasterization

Frame Buffer Blending

Texture Fetch, Fragment Shading

Tests (z, stencil…)

Vertices

Transform And Lighting

Vertex Programs:User-Defined Vertex

Processing

Fragment Programs:User-Defined

Per-Pixel Processing

Introduction

5Benjamin MoraUniversity of Wales

Swansea

• 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.

How to code/debug

6Benjamin MoraUniversity of Wales

Swansea

• 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

How to code/debug

7Benjamin MoraUniversity of Wales

Swansea

• 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).

Shader Loading

8Benjamin MoraUniversity of Wales

Swansea

• 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.

…Char *myVertexProgram;LoadText(myVertexProgram,

“VertexProgram.shader”);//Use now the OpenGL 2.0 API //to compile and enable the program…

myProgram.c

void main(){ gl_Position=gl_ModelviewProjectionMatrix

* gl_Vertex;}

VertexProgram.shader

Shader Loading

9Benjamin MoraUniversity of Wales

Swansea

• 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.

Shader Loading:Example

10Benjamin MoraUniversity of Wales

Swansea

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

Shader Loading:Example

11Benjamin MoraUniversity of Wales

Swansea

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

Variables and Types

12Benjamin MoraUniversity of Wales

Swansea

Types

13Benjamin MoraUniversity of Wales

Swansea

• 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;

Types

14Benjamin MoraUniversity of Wales

Swansea

• 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.

Types

15Benjamin MoraUniversity of Wales

Swansea

• 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

Types

16Benjamin MoraUniversity of Wales

Swansea

• 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.

Types

17Benjamin MoraUniversity of Wales

Swansea

• Structure :– Struct light {

vec3 position;

vec3 color;

float watt; //could be actually stored with color

}

light myLight;

• No typedef!

Types

18Benjamin MoraUniversity of Wales

Swansea

• 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[];

Types

19Benjamin MoraUniversity of Wales

Swansea

• Samplers– Texture variables.

• sampler1D• sampler2D• sampler3D• samplerCube• sampler1DShadow• sampler2DShadow

– Declaration:• Uniform sampler2D brick;

vec4 col=texture2D(brick, texCoordinate);

Types: Samplers

20Benjamin MoraUniversity of Wales

Swansea

• 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.

Types: Samplers

21Benjamin MoraUniversity of Wales

Swansea

• Example– Fragment Program:

varying vec2 coord;

uniform sampler2D marbleTexture; //texture object.

gl_FragColor = texture2D(marbleTexture, coord);

Types

22Benjamin MoraUniversity of Wales

Swansea

• 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.

Types

23Benjamin MoraUniversity of Wales

Swansea

• 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.

Types

24Benjamin MoraUniversity of Wales

Swansea

• 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) {…

Built-In Variables

25Benjamin MoraUniversity of Wales

Swansea

• 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.

Predefined Vertex Variables

26Benjamin MoraUniversity of Wales

Swansea

• 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;

Vertex Ouput Variables

27Benjamin MoraUniversity of Wales

Swansea

• vec4 gl_Position;

• vec4 gl_ClipVertex;

• float gl_PointSize;

Vertex Varying Ouput Variables

28Benjamin MoraUniversity of Wales

Swansea

• varying vec4 gl_FrontColor;

• varying vec4 gl_BackColor;

• varying vec4 gl_FrontSecondary;

• varying vec4 gl_BackSecondary;

• varying vec4 gl_TexCoord[];

• float gl_FogFragCoord;

Special Fragment Input Variables

29Benjamin MoraUniversity of Wales

Swansea

• varying vec4 gl_Color;

• varying vec4 gl_SecondaryColor;

• varying vec4 gl_TexCoord[];

• varying float gl_FogFragCoord;

Special Fragment Input Variables

30Benjamin MoraUniversity of Wales

Swansea

• bool gl_FrontFacing;

• vec4 gl_FragCoord;

Fragment Output Variables

31Benjamin MoraUniversity of Wales

Swansea

• vec4 gl_FragColor;

• vec4 gl_FragData;

• float gl_FragDepth;– //gl_FragCoord.z by default

• These variables have a global scope.

Built-In Constants

32Benjamin MoraUniversity of Wales

Swansea

• 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;

Built-In Uniform Variables

33Benjamin MoraUniversity of Wales

Swansea

• 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];

Built-In Uniform Variables

34Benjamin MoraUniversity of Wales

Swansea

• 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;

Built-In Uniform Variables

35Benjamin MoraUniversity of Wales

Swansea

• 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…

Vertex and FragmentProcessors

36Benjamin MoraUniversity of Wales

Swansea

Vertex and Fragment Processors

37Benjamin MoraUniversity of Wales

Swansea

• 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.

Vertex Processor

38Benjamin MoraUniversity of Wales

Swansea

• 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.

• …

Vertex Processor

39Benjamin MoraUniversity of Wales

Swansea

• 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…

Passing More Vertex Attributes

40Benjamin MoraUniversity of Wales

Swansea

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…

Passing More Vertex Attributes

41Benjamin MoraUniversity of Wales

Swansea

• 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.

Passing More Vertex Attributes

42Benjamin MoraUniversity of Wales

Swansea

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…

Passing More Vertex Attributes

43Benjamin MoraUniversity of Wales

Swansea

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.

Fragment Processor

44Benjamin MoraUniversity of Wales

Swansea

• 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.

Built-In Functions

45Benjamin MoraUniversity of Wales

Swansea

Built-In Functions

46Benjamin MoraUniversity of Wales

Swansea

• 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.

Built-In Functions

47Benjamin MoraUniversity of Wales

Swansea

• 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 );

Built-In Functions

48Benjamin MoraUniversity of Wales

Swansea

• 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 );

Built-In Functions

49Benjamin MoraUniversity of Wales

Swansea

• 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 );

Built-In Functions

50Benjamin MoraUniversity of Wales

Swansea

• 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 );

Built-In Functions

51Benjamin MoraUniversity of Wales

Swansea

• 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 );

Built-In Functions

52Benjamin MoraUniversity of Wales

Swansea

• 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] );

Built-In Functions

53Benjamin MoraUniversity of Wales

Swansea

• 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] );

Built-In Functions

54Benjamin MoraUniversity of Wales

Swansea

• 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 );

Built-In Functions

55Benjamin MoraUniversity of Wales

Swansea

• 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 );

Built-In Functions

56Benjamin MoraUniversity of Wales

Swansea

• Other functions:– float noise1( genType );– vec2 noise2( genType );– vec3 noise3( genType );– vec4 noise4( genType );– genType dFdx( genType );– genType dFdy( genType );– genType fwidth( genType );– …

Application: Phong Shading

57Benjamin MoraUniversity of Wales

Swansea

Ian Fergusson, https://www.cis.strath.ac.uk/teaching/ug/classes/52.359/lect13.pdf

top related