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

Slides:



Advertisements
Similar presentations
COMPUTER GRAPHICS SOFTWARE.
Advertisements

CS 352: Computer Graphics Chapter 7: The Rendering Pipeline.
GLSL Basics Discussion Lecture for CS 418 Spring 2015 TA: Zhicheng Yan, Sushma S Kini, Mary Pietrowicz.
Graphics Pipeline.
Status – Week 257 Victor Moya. Summary GPU interface. GPU interface. GPU state. GPU state. API/Driver State. API/Driver State. Driver/CPU Proxy. Driver/CPU.
RealityEngine Graphics Kurt Akeley Silicon Graphics Computer Systems.
CS 4363/6353 BASIC RENDERING. THE GRAPHICS PIPELINE OVERVIEW Vertex Processing Coordinate transformations Compute color for each vertex Clipping and Primitive.
Computer Graphic Creator: Mohsen Asghari Session 2 Fall 2014.
Informationsteknologi Wednesday, December 12, 2007Computer Graphics - Class 171 Today’s class OpenGL Shading Language.
The Programmable Graphics Hardware Pipeline Doug James Asst. Professor CS & Robotics.
GLSL I May 28, 2007 (Adapted from Ed Angel’s lecture slides)
GLSL I Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts Director, Arts Technology Center University of New Mexico.
3D Graphics Processor Architecture Victor Moya. PhD Project Research on architecture improvements for future Graphic Processor Units (GPUs). Research.
Status – Week 277 Victor Moya.
The Graphics Pipeline CS2150 Anthony Jones. Introduction What is this lecture about? – The graphics pipeline as a whole – With examples from the video.
Mohan Sridharan Based on slides created by Edward Angel GLSL I 1 CS4395: Computer Graphics.
Status – Week 260 Victor Moya. Summary shSim. shSim. GPU design. GPU design. Future Work. Future Work. Rumors and News. Rumors and News. Imagine. Imagine.
GPU Graphics Processing Unit. Graphics Pipeline Scene Transformations Lighting & Shading ViewingTransformations Rasterization GPUs evolved as hardware.
Shadows Computer Graphics. Shadows Shadows Extended light sources produce penumbras In real-time, we only use point light sources –Extended light sources.
REAL-TIME VOLUME GRAPHICS Christof Rezk Salama Computer Graphics and Multimedia Group, University of Siegen, Germany Eurographics 2006 Real-Time Volume.
GPU Programming Robert Hero Quick Overview (The Old Way) Graphics cards process Triangles Graphics cards process Triangles Quads.
Programmable Pipelines. Objectives Introduce programmable pipelines ­Vertex shaders ­Fragment shaders Introduce shading languages ­Needed to describe.
OpenGL Shading Language (Advanced Computer Graphics) Ernest Tatum.
Programmable Pipelines. 2 Objectives Introduce programmable pipelines ­Vertex shaders ­Fragment shaders Introduce shading languages ­Needed to describe.
OpenGL Conclusions OpenGL Programming and Reference Guides, other sources CSCI 6360/4360.
1 Graphics CSCI 343, Fall 2015 Lecture 4 More on WebGL.
Computer Graphics The Rendering Pipeline - Review CO2409 Computer Graphics Week 15.
Shadow Mapping Chun-Fa Chang National Taiwan Normal University.
GRAPHICS PIPELINE & SHADERS SET09115 Intro to Graphics Programming.
Programmable Pipelines Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts Director, Arts Technology Center University.
Review of OpenGL Basics
09/16/03CS679 - Fall Copyright Univ. of Wisconsin Last Time Environment mapping Light mapping Project Goals for Stage 1.
Advanced Computer Graphics Spring 2014 K. H. Ko School of Mechatronics Gwangju Institute of Science and Technology.
OpenGL Shading Language (GLSL)
CSE 381 – Advanced Game Programming GLSL. Rendering Revisited.
Review on Graphics Basics. Outline Polygon rendering pipeline Affine transformations Projective transformations Lighting and shading From vertices to.
OpenGL-ES 3.0 And Beyond Boston Photo credit :Johnson Cameraface OpenGL Basics.
OpenGL Shading Language (GLSL)
Subject Name: Computer Graphics Subject Code: Textbook: “Computer Graphics”, C Version By Hearn and Baker Credits: 6 1.
Week 3 Lecture 4: Part 2: GLSL I Based on Interactive Computer Graphics (Angel) - Chapter 9.
What are shaders? In the field of computer graphics, a shader is a computer program that runs on the graphics processing unit(GPU) and is used to do shading.
Background image by chromosphere.deviantart.com Fella in following slides by devart.deviantart.com DM2336 Programming hardware shaders Dioselin Gonzalez.
OpenGL Shading Language
Programming with OpenGL Part 3: Shaders Ed Angel Professor of Emeritus of Computer Science University of New Mexico 1 E. Angel and D. Shreiner: Interactive.
GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global.
An Introduction to the Cg Shading Language Marco Leon Brandeis University Computer Science Department.
GLSL Review Monday, Nov OpenGL pipeline Command Stream Vertex Processing Geometry processing Rasterization Fragment processing Fragment Ops/Blending.
COMP 175 | COMPUTER GRAPHICS Remco Chang1/XX13 – GLSL Lecture 13: OpenGL Shading Language (GLSL) COMP 175: Computer Graphics April 12, 2016.
Computer Graphics (Fall 2003) COMS 4160, Lecture 5: OpenGL 1 Ravi Ramamoorthi Many slides courtesy Greg Humphreys.
Shader.
Programmable Pipelines
Computer Graphics Lecture 32
Graphics Processing Unit
Chapter 6 GPU, Shaders, and Shading Languages
The Graphics Rendering Pipeline
Objectives Simple Shaders Programming shaders with GLSL
Understanding Theory and application of 3D
GLSL I Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts Director, Arts Technology Center University of New Mexico.
Day 05 Shader Basics.
Chapter VI OpenGL ES and Shader
Graphics Processing Unit
Lecture 13 Clipping & Scan Conversion
Shader Basics Chap. 2 of Orange Book.
Programming with OpenGL Part 3: Shaders
CIS 441/541: Introduction to Computer Graphics Lecture 15: shaders
CS 480/680 Computer Graphics GLSL Overview.
Language Definitions Chap. 3 of Orange Book.
CS 480/680 Part 1: Model Loading.
OpenGL-Rendering Pipeline
CS 480/680 Fall 2011 Dr. Frederick C Harris, Jr. Computer Graphics
Presentation transcript:

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

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

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 Fixed Functionality of OpenGL

8 Programmable Processors

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

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 Vertex Processor

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

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 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 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 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 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 Tasks of a Fragment Processor Operations on interpolated values Texture access Texture application Fog Color sum

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 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 Fragment Processor

23

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

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