shader basics chap. 2 of orange book. 2 contents why write shaders opengl programmable processors...

31
Shader Basics Chap. 2 of Orange Book

Upload: ursula-miles

Post on 03-Jan-2016

232 views

Category:

Documents


1 download

TRANSCRIPT

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

Shader Basics

Chap. 2 of Orange Book

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

2

Contents

Why write shadersOpenGL programmable processorsLanguage overviewSystem overview

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

3

Visual Summary of Fixed Functionality

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

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 imagesImage processing: convolution, unsharp masking, complex blending, …Animation effects: key frame interpolation, particle systems, procedurally defined motionUser programmable anti-aliasing methodsGeneral computation: sorting, mathematical modeling, fluid dynamics, …

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

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

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

GL Version Query

Fall 2009 revised 6

#include <gl/glew.h>#include <gl/glut.h>

#include <iostream>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

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

7

Fixed Functionality of OpenGL

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

8

Programmable Processors

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

9

Tasks of a Vertex Processor

Vertex transformationNormal transformation and normalizationTexture coordinate generationTexture coordinate transformationLightingColor material application

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

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

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

11

VertexProcessor

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

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

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

13

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

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

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

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.

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

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.

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

17

Varying Variables

Variables that define data that is passed from the vertex processor to the fragment processor are called 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.

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

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.

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

19

Tasks of a Fragment Processor

Operations on interpolated valuesTexture accessTexture applicationFogColor sum

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

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.

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

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

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

22

FragmentProcessor

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

23

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

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

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

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 rootCommon 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 antialiasingFunctions for accessing values in texture memoryFunctions that return noise values for procedural texturing effects

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

26

Differences – does not support

automatic promotion of data typespointers, strings, or characters, double-precision floats; byte, short, or long integers; or unsigned data typesunions, 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.

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

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

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

28

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

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.

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

30

glAttachShaderglBindAttribLocationglCompileShaderglCreateProgramglCreateShaderglDeleteProgramglDeleteShaderglDetachShaderglDisableVertexAttribArrayglEnableVertexAttribArray glGetActiveAttribglGetActiveUniformglGetAttachedShadersglGetAttribLocation

glGetProgramglGetProgramInfoLogglGetShaderglGetShaderInfoLogglGetShaderSourceglGetUniformglGetUniformLocationglGetVertexAttribglGetVertexAttribPointerglIsProgramglIsShaderglLinkProgramglShaderSourceglUniformglUseProgramglValidateProgramglVertexAttribglVertexAttribPointer

GLSL API

More details later

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

31

Key Benefits

Tight integration with OpenGLRuntime compilationNo reliance on cross-vendor assembly languageUnconstrained opportunities for compiler optimization plus optimal performance on a wider range of hardwareA truly open, cross-platform standardOne high-level language for all programmable graphics processingSupport for modular programmingNo additional libraries or executables