An Introduction to the Open GL Shading Language










![Language Basics: operators Grouping, function/constructor () Array/component indexing [] Component/member selection. Unary ++ Binary Language Basics: operators Grouping, function/constructor () Array/component indexing [] Component/member selection. Unary ++ Binary](https://slidetodoc.com/presentation_image/8319ece77a50553e8385bbf7af95cb8a/image-11.jpg)



















- Slides: 30
An Introduction to the Open. GL Shading Language Benj Lipchak Rob Simpson Bill Licea-Kane
Fixed Functionality Pipeline Triangles/Lines/Points API Primitive Processing Vertices Vertex Buffer Objects Alpha Test 2 January 2008 Transform and Lighting Primitive Assembly Rasterizer Texture Environment Color Sum Fog Depth Stencil Color Buffer Blend Dither An Introduction to the Open. GL Shading Language Frame Buffer
Programmable Shader Pipeline Triangles/Lines/Points API Primitive Processing Vertices Vertex Shader Vertex Buffer Objects January 2008 Rasterizer Fragment Shader Alpha Test 3 Primitive Assembly Depth Stencil Color Buffer Blend Dither An Introduction to the Open. GL Shading Language Frame Buffer
Programmer’s Model Attributes (m * vec 4) Vertex Uniforms (p * vec 4) Vertex Shader Primitive Assembly & Rasterize Varyings (n * vec 4) Fragment Uniforms (q * vec 4) Fragment Shader Per-Sample Operations 4 January 2008 An Introduction to the Open. GL Shading Language
Vertex Shader Environment Uniforms Textures Attribute 0 Varying 0 Attribute 1 Varying 1 Attribute 2 Varying 2 Attribute 3 Attribute 4 Vertex Shader Varying 3 Varying 4 Attribute 5 Varying 5 … … Attribute m Varying n Clip position Point size Temporary variables 5 January 2008 An Introduction to the Open. GL Shading Language
Fragment Shader Environment Uniforms Textures Varying 0 Varying 1 Varying 2 Fragment Color(s) Varying 3 Varying 4 Varying 5 Fragment Shader Fragment Depth … Varying n Window coord Front facing flag Point coord Temporary variables 6 January 2008 An Introduction to the Open. GL Shading Language
Precursors to GLSL Texture combiners EXT_texture_env_combine Vendor-specific assembly-like programmable shaders EXT_vertex_shader ATI_fragment_shader, ATI_text_fragment_shader NV_*_program* Standardized low-level programmable shaders ARB_vertex_program ARB_fragment_program Not to be confused with GLSL extensions! ARB_vertex_shader ARB_fragment_shader 7 January 2008 An Introduction to the Open. GL Shading Language
Hello World! void main(void) { // This is our Hello World vertex shader // Standard MVP transform gl_Position = gl_Model. View. Projection. Matrix * gl_Vertex; } void main(void) { // This is our Hello World fragment shader // Set to a constant color (hint: look at it upside down) gl_Frag. Color = vec 4(0. 7734); } 8 January 2008 An Introduction to the Open. GL Shading Language
Language Basics: variables types Scalar void float int bool Floating point: vec 2 vec 3 Vector vec 4 Integer: ivec 2 ivec 3 ivec 4 Boolean: bvec 2 bvec 3 bvec 4 Matrix mat 2 x 3 mat 4 mat 2 x 4 == mat 3 x 2 mat 2 x 2 mat 3 x 4 mat 3 x 3 mat 4 x 2 mat 4 x 4 mat 4 x 3 Containers Structures: struct Arrays: [] 9 January 2008 An Introduction to the Open. GL Shading Language
Language Basics: storage qualifiers const Local constants defined within shader uniform Constant shader parameters that can be changed between draws Do not change per-vertex or per-fragment attribute Per-vertex values (position, normal, color, etc. ) varying Values output by the vertex shader, input by the fragment shader Interpolated during rasterization 10 January 2008 An Introduction to the Open. GL Shading Language
Language Basics: operators Grouping, function/constructor () Array/component indexing [] Component/member selection. Unary ++ Binary * / Relational < <= Logical && Ternary conditional ? : Assignment = Sequence , 11 January 2008 -- + + - ^^ *= ! > >= == || /= += -= An Introduction to the Open. GL Shading Language !=
Language Basics: constructors Used to initialize a structure or built-in type – Built-in type initialization: vec 3 my. RGB = vec 3(0. 25, 0. 75); – Structure initialization: struct S { int a; float b; }; S s = S(2, 3. 5); Provide enough components of correct type vec 2 my. YZ = vec 2(0. 5, 0. 75); vec 4 my. Pos = vec 4(0. 25, my. YZ, 1. 0); Also provides explicit type conversions – no casting in GLSL! – Only int to float implicit conversions are allowed float num. Texels = count. Texels(); if (!bool(num. Texels)) discard; // non-zero value -> true 12 January 2008 An Introduction to the Open. GL Shading Language
Language Basics: swizzles Components from {xyzw}, {rgba}, or {stpq} Writemask or swizzle during assignment vec 4 foo = vec 4(1. 0); foo. xyz = vec 3(0. 25, 0. 75); foo. wzyx = foo; // reverse the components Swizzle or replicate components on right hand side foo = foo. wzyx; // another way to reverse components foo = foo. xxyy; // components reusable on right side Use indexing for vector and matrix component selection mat 4 my. Matrix = mat 4(1. 0); foo. x = foo[2]; // same as foo. x = foo. z; foo = my. Matrix[0]; // first column of matrix foo. x = my. Matrix[0][0]; // first column, first row 13 January 2008 An Introduction to the Open. GL Shading Language
Language Basics: flow control for while do – Loops can have break, continue if else Function calls – Can have return – More on function calls in next slide The above can all be nested! Note: no unstructured jumps (a. k. a goto) discard – Only available in fragment shaders – “Kills” the fragment, no further processing in the pipeline 14 January 2008 An Introduction to the Open. GL Shading Language
Language Basics: function calls Special storage qualifiers apply to function parameters, e. g. : bool f(in vec 2 input. Vec, out float ret. Val) {. . . } in: Parameter is copied in to the function but not copied out (default) const in: Parameter is copied in to the function and cannot change out: Parameter is copied out of the function but not copied in inout: Parameter is both copied in and copied out Notes Recursion is strictly forbidden! Functions can return a value or void 15 January 2008 An Introduction to the Open. GL Shading Language
Language Basics: VS built-in variables Inputs attribute attribute vec 4 gl_Vertex vec 3 gl_Normal vec 4 gl_Color vec 4 gl_Secondary. Color vec 4 gl_Multi. Tex. Coordn (0 -7) float gl_Fog. Coord Outputs vec 4 gl_Position: must be written! float gl_Point. Size vec 4 gl_Clip. Vertex varying vec 4 gl_Front. Color varying vec 4 gl_Back. Color varying vec 4 gl_Front. Secondary. Color varying vec 4 gl_Back. Secondary. Color varying vec 4 gl_Tex. Coord[n] varying float gl_Fog. Frag. Coord 16 January 2008 An Introduction to the Open. GL Shading Language
Language Basics: FS built-in variables Inputs vec 4 gl_Frag. Coord bool gl_Front. Facing varying vec 4 gl_Color varying vec 4 gl_Secondary. Color varying vec 4 gl_Tex. Coord[n] varying float gl_Fog. Frag. Coord varying vec 2 gl_Point. Coord Outputs vec 4 gl_Frag. Color vec 4 gl_Frag. Data[n] float gl_Frag. Depth 17 January 2008 An Introduction to the Open. GL Shading Language
Language Basics: more built-ins… Refer to the GLSL cheat sheet for built-in uniforms and functions! 18 January 2008 An Introduction to the Open. GL Shading Language
Starter Shaders: color manipulation // simple. fs // // copy primary color void main(void) { // Copy the primary color gl_Frag. Color = gl_Color; } // colorinvert. fs // // invert like a color negative void main(void) { // invert color components gl_Frag. Color. rgb = 1. 0 - gl_Color. rgb; gl_Frag. Color. a = 1. 0; } 19 January 2008 An Introduction to the Open. GL Shading Language
Starter Shaders: color manipulation // grayscale. fs // // convert RGB to grayscale void main(void) { // Convert to grayscale using NTSC conversion weights float gray = dot(gl_Color. rgb, vec 3(0. 299, 0. 587, 0. 114)); // replicate grayscale to RGB components gl_Frag. Color = vec 4(gray, 1. 0); } // sepia. fs // // convert RGB to sepia tone void main(void) { // Convert to grayscale using NTSC conversion weights float gray = dot(gl_Color. rgb, vec 3(0. 299, 0. 587, 0. 114)); // convert grayscale to sepia gl_Frag. Color = vec 4(gray * vec 3(1. 2, 1. 0, 0. 8), 1. 0); } 20 January 2008 An Introduction to the Open. GL Shading Language
Starter Shaders: color manipulation // heatsig. fs // // map grayscale to heat signature uniform sampler 1 D sampler 0; void main(void) { // Convert to grayscale using NTSC conversion weights float gray = dot(gl_Color. rgb, vec 3(0. 299, 0. 587, 0. 114)); // look up heatsig value gl_Frag. Color = texture 1 D(sampler 0, gray); } 21 January 2008 An Introduction to the Open. GL Shading Language
Starter Shaders: color manipulation // fog. fs // // per-pixel fog uniform float density; void main(void) { const vec 4 fog. Color = vec 4(0. 5, 0. 8, 0. 5, 1. 0); // calculate 2 nd order exponential fog factor // based on fragment's Z distance const float e = 2. 71828; float fog. Factor = (density * gl_Frag. Coord. z); fog. Factor *= fog. Factor; fog. Factor = clamp(pow(e, -fog. Factor), 0. 0, 1. 0); // Blend fog color with incoming color gl_Frag. Color = mix(fog. Color, gl_Color, fog. Factor); } 22 January 2008 An Introduction to the Open. GL Shading Language
Starter Shaders: convolution // passthrough. fs // // pass through a single texel value uniform sampler 2 D sampler 0; void main(void) { gl_Frag. Color = texture 2 D(sampler 0, gl_Tex. Coord[0]. st); } 23 January 2008 An Introduction to the Open. GL Shading Language
Starter Shaders: convolution // blur. fs // // blur (low-pass) 3 x 3 kernel uniform sampler 2 D sampler 0; uniform vec 2 tc_offset[9]; void main(void) { vec 4 sample[9]; for (int i = 0; i < 9; i++) { sample[i] = texture 2 D(sampler 0, gl_Tex. Coord[0]. st + tc_offset[i]); } // // // 1 2 1 2 1 / 13 gl_Frag. Color = (sample[0] + (2. 0*sample[1]) + sample[2] + (2. 0*sample[3]) + sample[4] + (2. 0*sample[5]) + sample[6] + (2. 0*sample[7]) + sample[8]) / 13. 0; } 24 January 2008 An Introduction to the Open. GL Shading Language
Starter Shaders: convolution 1 2 1 2 / 13 1 2 1 Blur Sharpen -1 -1 9 -1 -1 La. Placian -1 -1 8 -1 -1 Dilation max(kernel) Erosion min(kernel) 25 January 2008 An Introduction to the Open. GL Shading Language
Starter Shaders: vertex shaders // simple. vs // // Generic vertex transformation, // copy primary color void main(void) { // normal MVP transform gl_Position = gl_Model. View. Projection. Matrix * gl_Vertex; // Copy the primary color gl_Front. Color = gl_Color; } 26 January 2008 An Introduction to the Open. GL Shading Language
Starter Shaders: vertex shaders // // // diffuse. vs Generic vertex transformation, diffuse lighting based on one white light uniform vec 3 light. Pos[1]; void main(void) { // normal MVP transform gl_Position = gl_Model. View. Projection. Matrix * gl_Vertex; vec 3 N = normalize(gl_Normal. Matrix * gl_Normal); vec 4 V = gl_Model. View. Matrix * gl_Vertex; vec 3 L = normalize(light. Pos[0] - V. xyz); // output the diffuse color float Ndot. L = dot(N, L); gl_Front. Color = gl_Color * vec 4(max(0. 0, Ndot. L)); } 27 January 2008 An Introduction to the Open. GL Shading Language
Starter Shaders: vertex shaders // ptsize. vs // // Generic vertex transformation, // attenuated point size void main(void) { // normal MVP transform gl_Position = gl_Model. View. Projection. Matrix * gl_Vertex; vec 4 V = gl_Model. View. Matrix * gl_Vertex; gl_Front. Color = gl_Color; // calculate point size based on distance from eye float pt. Size = length(V); pt. Size = pt. Size * pt. Size; gl_Point. Size = 20000000. 0 / pt. Size; } 28 January 2008 An Introduction to the Open. GL Shading Language
Starter Shaders: vertex shaders // stretch. vs // // Generic vertex transformation, // followed by squash/stretch uniform vec 3 light. Pos[1]; uniform vec 3 squash. Stretch; void main(void) { // normal MVP transform, followed by squash/stretch vec 4 stretched. Coord = gl_Vertex; stretched. Coord. xyz *= squash. Stretch; gl_Position = gl_Model. View. Projection. Matrix * stretched. Coord; . . . } 29 January 2008 An Introduction to the Open. GL Shading Language
Useful References http: //www. 3 dshaders. com/ Home page for the “orange book” focused solely on GLSL http: //www. opengl. org/sdk/ Open. GL SDK, including links to the below resources http: //www. opengl. org/sdk/libs/Open. Scene. Graph/glsl_quickref. pdf one double-sided page cheat sheet to GLSL – indispensible! http: //www. opengl. org/registry/doc/GLSLang. Spec. Full. 1. 20. 8. pdf This is the ultimate authority: the GLSL specification document http: //www. opengl. org/sdk/docs/books/Super. Bible/ Full reference and tutorial to Open. GL 2. 1 All sample code downloadable for Windows, Mac OS X, and Linux 30 January 2008 An Introduction to the Open. GL Shading Language