Shader Basics Chap. 2 of Orange Book.

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.
Understanding the graphics pipeline Lecture 2 Original Slides by: Suresh Venkatasubramanian Updates by Joseph Kider.
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.
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.
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.
The programmable pipeline Lecture 10 Slide Courtesy to Dr. Suresh Venkatasubramanian.
Mohan Sridharan Based on slides created by Edward Angel GLSL I 1 CS4395: Computer Graphics.
GPU Graphics Processing Unit. Graphics Pipeline Scene Transformations Lighting & Shading ViewingTransformations Rasterization GPUs evolved as hardware.
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.
The programmable pipeline Lecture 3.
CSE 690: GPGPU Lecture 6: Cg Tutorial Klaus Mueller Computer Science, Stony Brook University.
1 Graphics CSCI 343, Fall 2015 Lecture 4 More on WebGL.
Computer Graphics The Rendering Pipeline - Review CO2409 Computer Graphics Week 15.
Shader Basics Chap. 2 of Orange Book. 2 Contents Why write shaders OpenGL programmable processors Language overview System overview.
Shadow Mapping Chun-Fa Chang National Taiwan Normal University.
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.
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.
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.
- Introduction - Graphics Pipeline
Shader.
Programmable Pipelines
Computer Graphics Lecture 32
A Crash Course on Programmable Graphics Hardware
Graphics Processing Unit
Chapter 6 GPU, Shaders, and Shading Languages
The Graphics Rendering Pipeline
CS451Real-time Rendering Pipeline
Objectives Simple Shaders Programming shaders with GLSL
Understanding Theory and application of 3D
Models and Architectures
Models and Architectures
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
Introduction to Computer Graphics with WebGL
Lecture 13 Clipping & Scan Conversion
Models and Architectures
Models and Architectures
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.
Introduction to Computer Graphics with WebGL
OpenGL-Rendering Pipeline
CS 480/680 Fall 2011 Dr. Frederick C Harris, Jr. Computer Graphics
Presentation transcript:

Shader Basics Chap. 2 of Orange Book

Contents Why write shaders OpenGL programmable processors Language overview System overview

Visual Summary of Fixed Functionality Gouraud shading: interpolate intensity (rgb) from vertex rgb

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

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.

Programmable Processors

GLSL Resources Extension wrangler: glew Shader IDE Tutorials Shader designer (TyphoonLabs) RenderMonkey (AMD) Tutorials Lighthouse 3D Clockworkcoders Language Summary Orange book Quick reference guide

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

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.

Vertex Processor

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. Need to output the “gl_ClipVertex” (in eye coord) in vertex shader for this to work

Three Kinds of Variables Attribute: vertex shader only Uniform: for vertex and fragment shaders Varying: from vertex to fragment shaders

Attribute Variables Variables defined in a vertex shader 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.

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.

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. Either the i or the f variants may be used to provide values for uniform variables of type bool, bvec2, bvec3, bvec4, or arrays of these. (0, or 0.0 for false; true otherwise).

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.

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.

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

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.

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.

Fragment Processor

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

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 [often not implemented by GPU drivers]

Differences – does not support float f = 1; 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.

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

discard;

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

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

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