Presentation is loading. Please wait.

Presentation is loading. Please wait.

Shader Basics Chap. 2 of Orange Book. 2 Contents Why write shaders OpenGL programmable processors Language overview System overview.

Similar presentations


Presentation on theme: "Shader Basics Chap. 2 of Orange Book. 2 Contents Why write shaders OpenGL programmable processors Language overview System overview."— Presentation transcript:

1 Shader Basics Chap. 2 of Orange Book

2 2 Contents Why write shaders OpenGL programmable processors Language overview System overview

3 3 Visual Summary of Fixed Functionality

4 4 Why Write Shaders Increasingly realistic materials: metals, stone, wood, paints, … Increasingly realistic lighting effects: area lights, soft shadows, … Natural phenomena: fire, smoke, water, clouds, … Advanced rendering effects: global illumination, ray-tracing, … Non-photorealistic materials: painterly effects, pen-and-ink drawings, simulation of illustration techniques, … New uses for texture memory storage of normals, gloss values, polynomial coefficients, … Procedural textures: dynamically generated 2D and 3D textures, not static texture images Image processing: convolution, unsharp masking, complex blending, … Animation effects: key frame interpolation, particle systems, procedurally defined motion User programmable anti-aliasing methods General computation: sorting, mathematical modeling, fluid dynamics, …

5 5 About GLSL The OpenGL Shading Language is a high-level procedural language. As of OpenGL 2.0, it is part of standard OpenGL, the leading cross- platform, operating-environment-independent API for 3D graphics and imaging. The same language, with a small set of differences, is used for both vertex and fragment shaders. It is based on C and C++ syntax and flow control. It natively supports vector and matrix operations since these are inherent to many graphics algorithms. It is stricter with types than C and C++, and functions are called by value-return. It uses type qualifiers rather than reads and writes to manage input and output. It imposes no practical limits to a shader's length, nor does the shader length need to be queried. Know your environment

6 GL Version Query Fall 2009 revised6 #include using namespace std; int main (int argc, char** argv) { glutInit (&argc,argv); glewInit(); glutCreateWindow ("t"); cout << "GL vendor: " << glGetString (GL_VENDOR) << endl; cout << "GL renderer: " << glGetString (GL_RENDERER) << endl; cout << "GL version: " << glGetString (GL_VERSION) << endl; cout << "GLSL version: " << glGetString (GL_SHADING_LANGUAGE_VERSION) << endl; //cout << "GL extension: " << glGetString (GL_EXTENSIONS) << endl; } BACK

7 7 Fixed Functionality of OpenGL

8 8 Programmable Processors

9 9 Tasks of a Vertex Processor Vertex transformation Normal transformation and normalization Texture coordinate generation Texture coordinate transformation Lighting Color material application

10 10 Vertex Processor The design of the vertex processor is focused on the T&L functions of a single vertex. Vertex shaders must compute the homogeneous position of the coordinate in clip space and store the result in the special output variable gl_Position. Values to be used during user clipping and point rasterization can be stored in the special output variables gl_ClipVertex and gl_PointSize. Conceptually, the vertex processor operates on one vertex at a time (but an implementation may have multiple vertex processors that operate in parallel). The vertex shader is executed once for each vertex passed to OpenGL. T&L: transformation & lighting

11 11 Vertex Processor

12 12 Vertex Processor OpenGL operations that remain as fixed functionality in between the vertex processor and the fragment processor include: perspective divide viewport mapping primitive assembly frustum and user clipping backface culling two-sided lighting selection polygon mode polygon offset selection of flat or smooth shading depth range

13 13

14 14 Attribute Variables There are two types of attribute variables: built in and user defined. Standard built-in attribute variables in OpenGL include color, surface normal, texture coordinates, and vertex position. Within the OpenGL API, generic vertex attributes are defined and referenced by numbers from 0 up to some maximum value. The command glVertexAttrib sends generic vertex attributes to OpenGL by specifying the index of the generic attribute to be modified and the value for that generic attribute. glBindAttribLocation allows an application to tie together the index of a generic vertex attribute and the name with which to associate that attribute in a vertex shader. Variables defined in a vertex shader

15 15 Uniform Variables … pass data values from the application to either the vertex processor or the fragment processor. A shader can be written so that it is parameterized with uniform variables. The application can provide initial values for these uniform variables, But uniform variables cannot be specified between calls to glBegin and glEnd, so they can change at most once per primitive.

16 16 Uniform Variables (cont) supports both built-in and user-defined uniform variables. Vertex shaders and fragment shaders can access current OpenGL state through built-in uniform variables containing the reserved prefix "gl_". Applications can make arbitrary data values available directly to a shader through user-defined uniform variables. glGetUniformLocation obtains the location of a user-defined uniform variable that has been defined as part of a shader.

17 17 Varying Variables Variables that define data that is passed from the vertex processor to the fragment processor are called VARYING VARIABLES.VARYING VARIABLES Both built-in and user-defined varying variables are supported. They are called varying variables because the values are potentially different at each vertex and perspective-correct interpolation [ref] is performed to provide a value at each fragment for use by the fragment shader.ref

18 18 Varying Variables (cont) Built-in varying variables include those defined for the standard OpenGL color and texture coordinate values. A vertex shader can use a user-defined varying variable to pass along anything that needs to be interpolated: colors, normals (useful for per- fragment lighting computations), texture coordinates, model coordinates, and other arbitrary values.

19 19 Tasks of a Fragment Processor Operations on interpolated values Texture access Texture application Fog Color sum

20 20 Fragment Processor A fragment shader typically writes into one or both of the special variables gl_FragColor or gl_FragDepth. To support parallelism at the fragment-processing level, fragment shaders are written in a way that expresses the computation required for a single fragment, and access to neighboring fragments is not allowed. The fragment processor does not replace the fixed functionality graphics operations that occur at the back end of the OpenGL pixel processing pipeline: coverage, pixel ownership test, stippling, tests (scissor, alpha, depth, stencil), alpha blending, logical operations, dithering, and plane masking.

21 21 Input Variables The window coordinate position of the fragment is communicated through the special input variable gl_FragCoord. An indicator of whether the fragment was generated by rasterizing a front-facing primitive is communicated through the special input variable gl_FrontFacing. A fragment shader is free to read multiple values from a single texture or multiple values from multiple textures. The result of one texture access can be used as the basis for performing another texture access (a DEPENDENT TEXTURE READ).DEPENDENT TEXTURE READ There is no inherent limitation on the number of such dependent reads that are possible, so ray-casting algorithms can be implemented in a fragment shader.

22 22 Fragment Processor

23 23

24 24 Language Overview C-basis: GLSL is based on the syntax of the ANSI C programming language, Addition to C: Vector types are supported for floating-point, integer, and Boolean values. Operators work as readily on vector types as they do on scalars. Floating-point matrix types are also supported as basic types. Samplers are a special type of opaque variable that access a particular texture map. 1D|2D|3D texture map, cube map textures and shadow textures are supported

25 25 Overview (cont) GLSL defines built-in functions for a variety of operations: Trigonometric operations: sine, cosine, tangent, … Exponential operations: power, exponential, logarithm, square root, and inverse square root Common math operations: absolute value, floor, ceiling, fractional part, modulus, … Geometric operations: length, distance, dot product, cross product, normalization, … Relational operations based on vectors: component-wise operations such as greater than, less than, equal to, … Specialized fragment shader functions for computing derivatives and estimating filter widths for antialiasing Functions for accessing values in texture memory Functions that return noise values for procedural texturing effects

26 26 Differences – does not support automatic promotion of data types pointers, strings, or characters, double-precision floats; byte, short, or long integers; or unsigned data types unions, enumerated types, bit fields in structures, and bitwise operators. Finally, the language is not file based, so you won't see any #include directives or other references to file names.

27 27 Differences constructors, rather than type casts, are used for data type conversion. unlike the call-by-value calling convention used by C, GLSL uses CALL BY VALUE- RETURN. Input parameters are copied into the function at call time, and output parameters are copied back to the caller before the function exits. Qualifiers: in, out, inout

28 28

29 29 To install and use OpenGL shaders, do the following: 1. Create one or more (empty) shader objects by calling glCreateShader. 2. Provide source code for these shaders by calling glShaderSource. 3. Compile each of the shaders by calling glCompileShader. 4. Create a program object by calling glCreateProgram. 5. Attach all the shader objects to the program object by calling glAttachShader. 6. Link the program object by calling glLinkProgram. 7. Install the executable program as part of OpenGL's current state by calling glUseProgram.

30 30 glAttachShader glBindAttribLocation glCompileShader glCreateProgram glCreateShader glDeleteProgram glDeleteShader glDetachShader glDisableVertexAttribArray glEnableVertexAttribArray glGetActiveAttrib glGetActiveUniform glGetAttachedShaders glGetAttribLocation glGetProgram glGetProgramInfoLog glGetShader glGetShaderInfoLog glGetShaderSource glGetUniform glGetUniformLocation glGetVertexAttrib glGetVertexAttribPointer glIsProgram glIsShader glLinkProgram glShaderSource glUniform glUseProgram glValidateProgram glVertexAttrib glVertexAttribPointer GLSL API More details later

31 31 Key Benefits Tight integration with OpenGL Runtime compilation No reliance on cross-vendor assembly language Unconstrained opportunities for compiler optimization plus optimal performance on a wider range of hardware A truly open, cross-platform standard One high-level language for all programmable graphics processing Support for modular programming No additional libraries or executables


Download ppt "Shader Basics Chap. 2 of Orange Book. 2 Contents Why write shaders OpenGL programmable processors Language overview System overview."

Similar presentations


Ads by Google