1 / 64

Programmable Shading

Programmable Shading. Pedro V. Sander. Today’s plan. Programmable shading: GLSL RenderMonkey Efficient rendering: Triangle order optimization. We will learn to program: Vertex shaders Pixel/fragment shaders Languages: CG (independent, nvidia) HLSL (w/ DirectX) GLSL (w/ OpenGL)

conor
Download Presentation

Programmable Shading

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programmable Shading Pedro V. Sander

  2. Today’s plan • Programmable shading: • GLSL • RenderMonkey • Efficient rendering: • Triangle order optimization Programmable Shading

  3. We will learn to program: Vertex shaders Pixel/fragment shaders Languages: CG (independent, nvidia) HLSL (w/ DirectX) GLSL (w/ OpenGL) We will use GLSL Memory Vertex Buffer Input Assembler Index Buffer Vertex Shader Texture Geometry Shader Texture Buffer Stream Out Setup/ Rasterization Pixel Shader Texture Depth Output Merger Color Outline Programmable Shading

  4. GLSL Programmable Shading

  5. OpenGL Shading • High level language • OpenGL Shading Language = GLSL • Integrated into OpenGL API (no extra run-time) Programmable Shading

  6. GLSL Main Objectives • Integrated intimately with OpenGL 1.4 • Enables incremental replacement of fixed functionality • C-based - with comprehensive vector and matrix types • Same language for vertex shaders and fragment shaders Programmable Shading

  7. Organization • GLSL • API • Vertex Shading • Fragment Shading • RenderMonkey • Some GLSL demos in RenderMonkey Programmable Shading

  8. API-integrated • Compiler built into driver • Presumably they know your card best • ATI/Nvidia must produce (good) compilers • Use built-in parameters (glColor, glNormal, …) • Add your own • There are other options • Cg, ASHLI, RapidMind, … • But with loss of OpenGL integration Programmable Shading

  9. Using High-level Code • Create shader objectS = glCreateShader(GL_VERTEX_SHADER_ARB) • Vertex or Fragment • Load shader into objectglShaderSource(S, n, shaderArray, lenArray) • Array of strings • Compile objectglCompileShader(S) Programmable Shading

  10. Using High-level Code • Create program objectP = glCreateProgram() • Attach all shader objectsglAttachShader(P, S)Vertex, Fragment or both • Link togetherglLinkProgram(P) • UseglUseProgramObject(P) Programmable Shading

  11. Using High-level Code • Setting variables for your shader programs i=glGetAttribLocation(P,myAttrib)glVertexAttrib1f(i,value)i=glGetUniformLocation(P,myAttrib)glUniform1f(i,value) (details later) Programmable Shading

  12. Initial example: Blend Positions Programmable Shading

  13. High-level Code void main() { float Kin = gl_Color.r; // key input // screen position from vertex and texture vec4 Vp = ftransform(); vec4 Tp = vec4(gl_MultiTexCoord0.xy*1.8-.9, 0.,1.); // interpolate between Vp and Tp gl_Position = mix(Tp,Vp,pow(1.-Kin,8.)); // copy to output gl_TexCoord[0] = gl_MultiTexCoord0; gl_TexCoord[1] = Vp; gl_TexCoord[3] = vec4(Kin); } Programmable Shading

  14. Main Function void main() { float Kin = gl_Color.r; // key input // screen position from vertex and texture vec4 Vp = ftransform(); vec4 Tp = vec4(gl_MultiTexCoord0.xy*1.8-.9, 0.,1.); // interpolate between Vp and Tp gl_Position = mix(Tp,Vp,pow(1.-Kin,8.)); // copy to output gl_TexCoord[0] = gl_MultiTexCoord0; gl_TexCoord[1] = Vp; gl_TexCoord[3] = vec4(Kin); } Programmable Shading

  15. Use Standard OpenGL State void main() { float Kin = gl_Color.r; // key input // screen position from vertex and texture vec4 Vp = ftransform(); vec4 Tp = vec4(gl_MultiTexCoord0.xy*1.8-.9, 0.,1.); // interpolate between Vp and Tp gl_Position = mix(Tp,Vp,pow(1.-Kin,8.)); // copy to output gl_TexCoord[0] = gl_MultiTexCoord0; gl_TexCoord[1] = Vp; gl_TexCoord[3] = vec4(Kin); } Programmable Shading

  16. Built-in Types void main() { float Kin = gl_Color.r; // key input // screen position from vertex and texture vec4 Vp = ftransform(); vec4 Tp = vec4(gl_MultiTexCoord0.xy*1.8-.9, 0.,1.); // interpolate between Vp and Tp gl_Position = mix(Tp,Vp,pow(1.-Kin,8.)); // copy to output gl_TexCoord[0] = gl_MultiTexCoord0; gl_TexCoord[1]= Vp; gl_TexCoord[3]= vec4(Kin); } Programmable Shading

  17. Swizzle / Channel Selection void main() { float Kin = gl_Color.r; // key input // screen position from vertex and texture vec4 Vp = ftransform(); vec4 Tp = vec4(gl_MultiTexCoord0.xy*1.8-.9, 0.,1.); // interpolate between Vp and Tp gl_Position = mix(Tp,Vp,pow(1.-Kin,8.)); // copy to output gl_TexCoord[0] = gl_MultiTexCoord0; gl_TexCoord[1] = Vp; gl_TexCoord[3] = vec4(Kin); } Programmable Shading

  18. Vector Construction void main() { float Kin = gl_Color.r; // key input // screen position from vertex and texture vec4 Vp = ftransform(); vec4 Tp = vec4(gl_MultiTexCoord0.xy*1.8-.9, 0.,1.); // interpolate between Vp and Tp gl_Position = mix(Tp,Vp,pow(1.-Kin,8.)); // copy to output gl_TexCoord[0] = gl_MultiTexCoord0; gl_TexCoord[1] = Vp; gl_TexCoord[3] = vec4(Kin); } Programmable Shading

  19. Built-in Functions void main() { float Kin = gl_Color.r; // key input // screen position from vertex and texture vec4 Vp = ftransform(); vec4 Tp = vec4(gl_MultiTexCoord0.xy*1.8-.9, 0.,1.); // interpolate between Vp and Tp gl_Position = mix(Tp,Vp,pow(1.-Kin,8.)); // copy to output gl_TexCoord[0] = gl_MultiTexCoord0; gl_TexCoord[1] = Vp; gl_TexCoord[3] = vec4(Kin); } Programmable Shading

  20. Language details • Data types • Variables • Qualifiers • Functions • Built-in variables Programmable Shading

  21. Operators • grouping: () • array subscript: [] • function call and constructor: () • field selector and swizzle: . • postfix: ++ -- • prefix: ++ -- + - ! Programmable Shading

  22. Operators • binary: * / + - • relational: < <= > >= • equality: == != • logical: && ^^ || • selection: ?: • assignment: = *= /= += -= Programmable Shading

  23. Reserved Operators • prefix: ~ • binary: % • bitwise: << >> & ^ | • assignment: %= <<= >>= &= ^= |= Programmable Shading

  24. GLSL Data Types • Three basic data types in GLSL: • float, bool, int • float and int behave as in C • bool can take true or false • Vectors with 2,3 or 4 components, declared as: • vec{2,3,4}: a vector of 2, 3,or 4 floats • bvec{2,3,4}: bool vector • ivec{2,3,4}: integer vector • Square matrices 2x2, 3x3 and 4x4: • mat2 • mat3 • mat4 Programmable Shading

  25. GLSL Data Types • A set of special types are available for texture access, called sampler • sampler1D - for 1D textures • sampler2D - for 2D textures • sampler3D - for 3D textures • samplerCube - for cube map textures • Arrays can be declared and accessed as in C • Can’t be initialized when declared Programmable Shading

  26. GLSL Data Types • Structures are supported with exactly the same syntax as C struct dirlight { vec3 direction; vec3 color; }; Programmable Shading

  27. GLSL Variables • Declaring variables in GLSL is mostly the same as in C • Differences: GLSL relies heavily on constructor for initialization and type casting float a,b; // two vector (yes, the comments are like in C) int c = 2; // c is initialized with 2 bool d = true; // d is true float b = 2; // incorrect, there is no automatic type casting float e = (float)2;// incorrect, requires constructors for type casting int a = 2; float c = float(a); // correct. c is 2.0 vec3 f; // declaring f as a vec3 vec3 g = vec3(1.0,2.0,3.0); // declaring and initializing g Programmable Shading

  28. GLSL Variables • GLSL is pretty flexible when initializing variables using other variables vec2 a = vec2(1.0,2.0); vec2 b = vec2(3.0,4.0); vec4 c = vec4(a,b) // c = vec4(1.0,2.0,3.0,4.0); vec2 g = vec2(1.0,2.0); float h = 3.0; vec3 j = vec3(g,h); Programmable Shading

  29. GLSL Variables • Matrices also follow this pattern • The declaration and initialization of structures is demonstrated below mat4 m = mat4(1.0) // initializing the diagonal of the matrix with 1.0 vec2 a = vec2(1.0,2.0); vec2 b = vec2(3.0,4.0); mat2 n = mat2(a,b); // matrices are assigned in column major order mat2 k = mat2(1.0,0.0,1.0,0.0); // all elements are specified struct dirlight { // type definition vec3 direction; vec3 color; }; dirlight d1; dirlight d2 = dirlight(vec3(1.0,1.0,0.0),vec3(0.8,0.8,0.4)); Programmable Shading

  30. GLSL Variables • One can do “swizzling” • x,y,z,w for vectors components; • r,g,b,a for color components; • s,t,p,q for texture coordinates. vec4 a = vec4(1.0,2.0,3.0,4.0); float posX = a.x; float posY = a[1]; vec2 posXY = a.xy; float depth = a.w; Programmable Shading

  31. Variable qualifiers • attribute • Changes per-vertex • eg. position, normal etc. • uniform • Does not change between vertices of a batch • eg light position, texture unit, other constants • varying • Passed from VS to FS, interpolated • eg texture coordinates, vertex color Programmable Shading

  32. GLSL Varying Variables • Varying variables are interpolated from vertices, during rasterization • Define varying variables in both vertex and fragment shaders • Varying variables must be written in the vertex shader • Varying variables can only be read in fragment shaders varying vec3 normal; Programmable Shading

  33. GLSL Varying Variables • Example:Interpolated color is the varying variable Programmable Shading

  34. GLSL Uniform Variables • Uniform variables • Constant (cannot be changed between glBegin/End) • For OpenGL to communicate with shader • Uniform variables are suitable for values that remain constant along a primitive, frame, or even the whole scene • Uniform variables can be read (but not written) in both vertex and fragment shaders Programmable Shading

  35. GLSL Uniform Variables • First get the memory location of the variable: Parameters: • program - the handler to the program • name - the name of the variable. The return value is the location of the variable, which can be used to assign values to it. GLint glGetUniformLocationARB(GLhandleARB program, const char name); Programmable Shading

  36. GLSL Uniform Variables • Then set values from OpenGL • Parameters: • location - the previously queried location. • v0,v1,v2,v3 - float values. (replace “f” by “i” for integer equivalents;matrices can also be set in a similar way) void glUniform1fARB(GLint location, GLfloat v0);void glUniform2fARB(GLint location, GLfloat v0, GLfloat v1);void glUniform3fARB(GLint location, GLfloat v0, GLfloat v1, GLfloat v2);void glUniform4fARB(GLint location, GLfloat v0, GLfloat v1, GLfloat v2, GLfloat v3); Programmable Shading

  37. GLSL Uniform Variables • Values are kept until program is re-linked • After new link, all values reset to 0 Programmable Shading

  38. GLSL Attribute Variables • Attribute variables also allow your C program to communicate with shaders • Updated at any time • Read from vertex shader • Not accessed by fragment shader • Set just like uniform variables: Get location: Write: GLint glGetAttribLocationARB(GLhandleARB program,char *name); void glVertexAttrib1fARB(GLint location, GLfloat v0);void glVertexAttrib2fARB(GLint location, GLfloat v0, GLfloat v1);void glVertexAttrib3fARB(GLint location, GLfloat v0, GLfloat v1,GLfloat v2);void glVertexAttrib4fARB(GLint location, GLfloat v0, GLfloat v1,,GLfloat v2, GLfloat v3); Programmable Shading

  39. GLSL Attribute Variables • An example: Vertex shader has: attribute float height; In the main OpenGL program, we can do the following: loc = glGetAttribLocationARB(p,"height"); glBegin(GL_TRIANGLE_STRIP); glVertexAttrib1fARB(loc,2.0); glVertex2f(-1,1); glVertexAttrib1fARB(loc,2.0); glVertex2f(1,1); glVertexAttrib1fARB(loc,-2.0); glVertex2f(-1,-1); glVertexAttrib1fARB(loc,-2.0); glVertex2f(1,-1); glEnd(); Programmable Shading

  40. Flow Control • expression ? trueExpression : falseExpression • if, if-else • for, while, do-while • return, break, continue • discard (fragment only) Programmable Shading

  41. GLSL Functions • Shader structure in functions (like C) • Must have a main function: void main() • Optional user defined functions • Functions may have return values • Any type, except array Programmable Shading

  42. GLSL Functions • Function parameters have the following qualifiers: • in - for input parameters • out - for outputs of the function (can also use the return statement) • inout - for parameters that are both input and output of a function • If no qualifier is specified, default is in. Programmable Shading

  43. GLSL Functions • A few final notes: • A function can be overloaded as long as the list of parameters is different • Recursion behavior is undefined by specification • Finally, let’s look at an example vec4 toonify(in float intensity) { vec4 color; if (intensity > 0.98) color = vec4(0.8,0.8,0.8,1.0); else if (intensity > 0.5) color = vec4(0.4,0.4,0.8,1.0); else if (intensity > 0.25) color = vec4(0.2,0.2,0.4,1.0); else color = vec4(0.1,0.1,0.1,1.0); return(color); } Programmable Shading

  44. Built-in variables • Attributes & uniforms • Facilitates programming • OpenGL state mapped to variables • Some special variables are required to be written to, others are optional Programmable Shading

  45. Some of the special built-ins • Vertex shader vec4 gl_Position; // must be written float gl_PointSize; // may be written • Fragment shader float gl_FragColor; // may be written float gl_FragDepth; // may be read/written bool gl_FrontFacing; // may be read Programmable Shading

  46. Attributes • Built-in attribute vec4 gl_Vertex; attribute vec3 gl_Normal; attribute vec4 gl_Color; attribute vec4 gl_SecondaryColor; attribute vec4 gl_MultiTexCoordn; attribute float gl_FogCoord; • User-defined attribute vec3 myTangent; attribute vec3 myBinormal; Etc… Programmable Shading

  47. Built-in Uniforms uniform mat4 gl_ModelViewMatrix; uniform mat4 gl_ProjectionMatrix; uniform mat4 gl_ModelViewProjectionMatrix; uniform mat3 gl_NormalMatrix; uniform mat4 gl_TextureMatrix[n]; struct gl_MaterialParameters { vec4 emission; vec4 ambient; vec4 diffuse; vec4 specular; float shininess; }; uniform gl_MaterialParameters gl_FrontMaterial; uniform gl_MaterialParameters gl_BackMaterial; Programmable Shading

  48. Built-in Uniforms 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]; Programmable Shading

  49. Built-in Varyings varying vec4 gl_FrontColor// vertex varying vec4 gl_BackColor;// vertex varying vec4 gl_FrontSecColor;//vertex varying vec4 gl_BackSecColor;// vertex varying vec4 gl_Color; // fragment varying vec4 gl_SecondaryColor; // fragment varying vec4 gl_TexCoord[];// both varying float gl_FogFragCoord;// both Programmable Shading

  50. Built-in functions • Angles & Trigonometry • radians, degrees, sin, cos, tan, asin, acos, atan • Exponentials • pow, exp2, log2, sqrt, inversesqrt • Common • abs, sign, floor, ceil, fract, mod, min, max, clamp Programmable Shading

More Related