glsl opengl shading language
play

GLSL: OpenGL Shading Language Stefan Bruckner GLSL: Intro OpenGL - PowerPoint PPT Presentation

GLSL: OpenGL Shading Language Stefan Bruckner GLSL: Intro OpenGL Shading Language (GLSL) Current version 4.2 Specification: http://www.opengl.org/documentation/specs/ High level shading language Compiles directly to the available GLSL


  1. GLSL: OpenGL Shading Language Stefan Bruckner

  2. GLSL: Intro OpenGL Shading Language (GLSL) Current version 4.2 Specification: http://www.opengl.org/documentation/specs/ High level shading language Compiles directly to the available GLSL OpenGL Version Version hardware 1.10.59 2.0 Previously: program in assembly 1.20.8 2.1 1.30.10 3.0 for every vendor 1.40.08 3.1 1.50.11 3.2 3.30.6 3.3 4.00.9 4.0 4.10.6 4.1 4.20.6 4.2

  3. GLSL: Intro GLSL OpenGL Changes Year Version Version 1.10.59 2.0 2004 Vertex Arrays 1.20.8 2.1 2006 Added PBO 1.30.10 3.0 2008 New render targets/FBO/VBO New matrix standards 1.40.08 3.1 2009 - No ftransform()! 1.50.11 3.2 2009 GS/layout 3.30.6 3.3 2010 Occlusion queries/sampler objects Individual blend equations for each color output/texture 4.00.9 4.0 2010 buffers/TS Atomic counters/bind programs individually to programmable 4.10.6 4.1 2010 stages/viewport arrays Read and write images/binding points from GLSL/TFO with 4.20.6 4.2 2011 instanced rendering

  4. GLSL: Intro Shader programs are programs that run in parallel on the graphics card to render graphics Newest cards (NVIDIA 560, 2011) has 512 stream processors that are divided between the different shader types In theory 512 pixels can be colored at the same time

  5. GLSL Shader stream processors are grouped by pairs of 8 (8x2)

  6. GLSL Graphics Pipeline TS VS GS FS

  7. GLSL: Shader Programs

  8. GLSL: Shader Programs Types of shader programs Vertex Program Tessellation Program (latest addition) Geometry Program Fragment Program Output from vertex program is used as input to tessellation/geometry program Output from geometry program is used as input to fragment program Vertex Tessellation Geometry Fragment

  9. GLSL: Vertex Program Operates on vertices in vec4 position; void main(void) { Input: vertex gl_Position = position; } Output: vertex Used for Moving/Displacing vertices Defining/Modifying normals Texture coordinates [2D Wave map displacement] Changing color of vertices Note: vertices/texture coord./normals arrive untransformed (in object coordinate system)

  10. GLSL: Tesselation Program Operates on vertices Input: a patch (quads, triangles, lines) Output: subdivide the patch into (thousands of) primitives of the patch type Used for [Triangle tessellation] Subdivision of primitives/patches control: LOD primitives layout(vertices = 4) out; uniform float inner_level; Terrain rendering uniform float outer_level; #define id gl_InvocationID void main(){ Parametric surfaces gl_TessLevelInner[0] = inner_level; gl_TessLevelInner[1] = inner_level; gl_TessLevelOuter[0] = outer_level; eval: gl_TessLevelOuter[1] = outer_level; layout(quads, equal_spacing, ccw) in; gl_TessLevelOuter[2] = outer_level; uniform mat4 projection; gl_TessLevelOuter[3] = outer_level; uniform mat4 modelview; gl_out[id].gl_Position = gl_in[id].gl_Position; void main(){ } float u = gl_TessCoord.x; float v = gl_TessCoord.y; float omu = 1-u; float omv = 1-v; gl_Position = projection * modelview * *Func (omu, omv gl_in[0].gl_Position, gl_in[1].gl_Position ,gl_in[2].gl_Position,gl_in[3].gl_Position);}

  11. GLSL: Geometry Program Operates on vertices Input: vertices (a fixed number) Output: vertices/lines/polygons Used for Creating new vertices/lines/polygons [Sphere splatting] Changing color for each primitive Defining/Modifying normals for each primitive Texture coordinates for each primitive GS VS geometry: layout(triangles) in; layout(triangle_strip, max_vertices = 3) out; void main(){ gl_Position = gl_in[0].gl_Position; EmitVertex(); gl_Position = gl_in[1].gl_Position; EmitVertex(); gl_Position = gl_in[2].gl_Position; EmitVertex(); EndPrimitive(); }

  12. GLSL: Fragment Program Operates on fragments Input: fragment location/color/normal/texturecoord Output: fragment color, opacity (+fragment depth) Used for Setting the color of pixels Calculating depth values Note: no automatic lighting or texturing [Depth of field] fragment: out vec4 fragment; void main(){ fragment = CalcShading(); }

  13. GLSL: Program Chain Example OpenGL draws one vertex Vertex program chooses a color according to a texture

  14. GLSL: Program Chain Example Geometry program takes the vertex and uses it as a center for a sphere Emits triangles for this sphere with color and correct normals and texture coordinates

  15. GLSL: Program Chain Example Fragment program fills the triangles using texturing and phong shading

  16. GLSL: Setting up Create a program glCreateProgram() Returns a program object reference Shader objects will be attached to this program You can create several programs and attach the same shader to multiple programs Create a Shader glCreateShader(type) GL_VERTEX_SHADER GL_GEOMETRY_SHADER GL_FRAGMENT_SHADER GL_TESS_CONTROL_SHADER GL_TESS_EVALUATION_SHADER Returns a shader object reference

  17. GLSL: Setting up Upload the source glShaderSource(sid, n, *src, *srclen) Attaching a shader to a program glAttachShader(pid, sid) Compiling glCompileShader(sid) Linking the program glLinkProgram() Starting a program is done like this: glUseProgram(pid) glUseProgram(0) //no program’s used

  18. GLSL: Setting up Detaching shaders glDetachShader(pid, sid) Deleting shaders and programs glDeleteShader(sid) glDeleteProgram(pid)

  19. GLSL: Uniforms and Attributes A shader program can receive data through uniforms and attributes Uniforms Variables that apply to all vertices and does not change over a primitive Attributes Variables that can change from vertex to vertex Similar to glColor(…) and glNormal(…) Interpolated between vertices

  20. GLSL: Data Types GLSL supports many different data types Single value bool, int, float 2 element vectors bvec2, vec2, ivec2 3 element vectors bvec3, vec3, ivec3 4 element vectors bvec4, vec4, ivec4

  21. GLSL: Data Types Matrices mat2, mat3, mat4 Textures sampler1D, sampler2D, sampler3D Structs struct name {…}; Others Texture arrays, texture buffers and depth textures

  22. GLSL: Variables Defining variables bvec2 b = bvec2(true, false); vec3 n = vec3(1.0, 0.0, 0.0); mat2 m = mat2(1.0, 0.0, 0.0, 1.0); struct light { vec3 position; vec3 color; };

  23. GLSL: Vectors There are different ways of accessing the values of a vector vec4 v = vec4(…); float a = v.x; or v.r; or v.s; float b = v.y; or v.g; or v.t; float c = v.z; or v.b; or v.p; float d = v.w; or v.a; or v.q; Swizzling vec4 v = vec4(a, b, c, d); vec4 n = v.xxyy; //n = {a, a, b, b} vec2 j = v.zw; //j = {c, d}

  24. GLSL: Matrices Matrices also have different ways of accessing the values mat4 m = … float m00 = m[0][0]; //column 0, row 0 vec4 clm = m[1]; // column 1

  25. GLSL: Qualifiers GLSL has several qualifiers to define the type variable const – a compile time constant attribute – a variable that changes per vertex. Only applies to vertex shaders uniform – a variable that may change per vertex. Can only be changed outside of a glBegin/glEndblock varying – interpolated data. A value that has been defined in a vertex shader will be interpolated over the primitive.

  26. GLSL: Setting Attributes and Uniforms out vec2 coord; //varying attribute uniform mat4 projmat; //uniform attribute attribute float weight; //per vertex attribute uniform sampler2D depth; //texture2D In OpenGL we need to do this to set a uniform GLuint glGetUniformLocation(pid, “projmat”) Returns a location to the variable To set a value glUniform<1234><fi>(GLint loc, GLfloat v0, …) glUniform<1234><fi>v(GLint loc, GLsizei n, GLfloat v) glUniformMatrix<234>fv(GLuint loc, GLsizei count, GLboolean transpose, GLfloat *v)

  27. GLSL: Setting Attributes and Uniforms The procedure for setting an attribute is similar GLint glGetAttribLocation(GLuint program, char *name) Setting the attribute glVertexAttrib<1234><fi>(GLint loc, GLfloat v0, …) glVertexAttrib<1234><fi>v(GLint loc, GLfloat v) Texture In GLSL you refer to a texture using a sampler but in OpenGL you define them using integers glUniform1i(ul, n) n is the number of the texture unit the texture is bound to

  28. GLSL: Setting Attributes and Uniforms Setting structs in our program ul = glGetUniformLocation(pid, “lighting.position”) glUniform3f(ul, x, y, z) ul = glGetUniformLocation(pid, “lighting.color”) glUniform3f(ul, r, g, b)

  29. GLSL: Operators Most math functions are available and valid for the built in data types vec4 v; vec4 w; mat4 m; vec4 q = v * w + v / 2.0 + w * 3.0; mat4 c = q * m Also special methods such as sin, cos, log, sqrt, abs, min, max, dot, length, cross, normalize, ftransform

  30. GLSL: Built-in variables OpenGL < 3.1 OpenGL has several built-in attributes gl_Vertex – vertex defined by glVertex gl_Color – color defined by glColor gl_Normal – normal defined by glNormal … And built-in uniforms gl_ModelViewMatrix gl_ProjectionMatrix gl_TextureMatrix … OpenGL > 3.1 layout(location=0) in vec4 vertices; layout(binding=1) uniform mat4 projmat;

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend