Course 17: State-of-the-Art in Hardware Rendering. The OpenGL Shading Language Randi Rost 3Dlabs, Inc.

Similar documents
Introduction to the OpenGL Shading Language

Introduction to Shaders for Visualization. The Basic Computer Graphics Pipeline

GLSL Introduction. Fu-Chung Huang. Thanks for materials from many other people

The Graphics Pipeline and OpenGL III: OpenGL Shading Language (GLSL 1.10)!

COURSE 17: STATE-OF-THE-ART IN SHADING HARDWARE1 CHAPTER 6: THE OPENGL SHADING LANGUAGE 2. Randi J. Rost 3Dlabs, Inc.

12.2 Programmable Graphics Hardware

Programmable Graphics Hardware

Introduction to Shaders.

Today. Rendering - III. Outline. Texturing: The 10,000m View. Texture Coordinates. Specifying Texture Coordinates in GL

E.Order of Operations

The Transition from RenderMan to the OpenGL Shading Language (GLSL)

Shader Programs. Lecture 30 Subsections 2.8.2, Robb T. Koether. Hampden-Sydney College. Wed, Nov 16, 2011

Design Focus. GLSL Language Details. Additions from C++ Additions for Graphics

GLSL Language Details

Shader Programming. Daniel Wesslén, Stefan Seipel, Examples

EDAF80 Introduction to Computer Graphics. Seminar 3. Shaders. Michael Doggett. Slides by Carl Johan Gribel,

GLSL Introduction. Fu-Chung Huang. Thanks for materials from many other people

Programmable GPUs. Real Time Graphics 11/13/2013. Nalu 2004 (NVIDIA Corporation) GeForce 6. Virtua Fighter 1995 (SEGA Corporation) NV1

Stored Texture Shaders

Supplement to Lecture 22

Technical Game Development II. Reference: Rost, OpenGL Shading Language, 2nd Ed., AW, 2006 The Orange Book Also take CS 4731 Computer Graphics

The Graphics Pipeline and OpenGL III: OpenGL Shading Language (GLSL 1.10)!

Preparing for Texture Access. Stored Texture Shaders. Accessing Texture Maps. Vertex Shader Texture Access

Programmable Graphics Hardware

GLSL 1: Basics. J.Tumblin-Modified SLIDES from:

Information Coding / Computer Graphics, ISY, LiTH GLSL. OpenGL Shading Language. Language with syntax similar to C

Technical Game Development II. Reference: Rost, OpenGL Shading Language, 2nd Ed., AW, 2006 The Orange Book Also take CS 4731 Computer Graphics

Programming with OpenGL Part 3: Shaders. Ed Angel Professor of Emeritus of Computer Science University of New Mexico

INF3320 Computer Graphics and Discrete Geometry

Stripes, Rings, and Dots!

Programming shaders & GPUs Christian Miller CS Fall 2011

C P S C 314 S H A D E R S, O P E N G L, & J S RENDERING PIPELINE. Mikhail Bessmeltsev

OpenGL Programmable Shaders

Technical Game Development II. Reference: Rost, OpenGL Shading Language, 2nd Ed., AW, 2006 The Orange Book IMGD 4000 (D 10) 1

Lecture 2. Shaders, GLSL and GPGPU

Overview Discussion September 2001 Copyright 3Dlabs, Page 1

Tutorial on GPU Programming #2. Joong-Youn Lee Supercomputing Center, KISTI

Some advantages come from the limited environment! No classes. Stranight ans simple code. Remarkably. Avoids most of the bad things with C/C++.

X. GPU Programming. Jacobs University Visualization and Computer Graphics Lab : Advanced Graphics - Chapter X 1

SGI OpenGL Shader. Marc Olano SGI

Real-Time Rendering (Echtzeitgraphik) Michael Wimmer

Lecture 09: Shaders (Part 1)

Graphics Hardware. Computer Graphics COMP 770 (236) Spring Instructor: Brandon Lloyd 2/26/07 1

Lecture 5 Vertex and Fragment Shaders-1. CITS3003 Graphics & Animation

Programming with OpenGL Shaders I. Adapted From: Ed Angel Professor of Emeritus of Computer Science University of New Mexico

Sign up for crits! Announcments

Advanced Graphics. The Shader knows. Alex Benton, University of Cambridge Supported in part by Google UK, Ltd

Advanced Lighting Techniques Due: Monday November 2 at 10pm

The Basic Computer Graphics Pipeline, OpenGL-style. Introduction to the OpenGL Shading Language (GLSL)

Sung-Eui Yoon ( 윤성의 )

Programmable GPUs Outline

Introduction to the OpenGL Shading Language (GLSL)

Today s Agenda. Basic design of a graphics system. Introduction to OpenGL

WebGL and GLSL Basics. CS559 Fall 2015 Lecture 10 October 6, 2015

Grafica Computazionale: Lezione 30. Grafica Computazionale. Hiding complexity... ;) Introduction to OpenGL. lezione30 Introduction to OpenGL

Intro to GPU Programming (OpenGL Shading Language) Cliff Lindsay Ph.D. Student CS WPI

CMPS160 Shader-based OpenGL Programming. All slides originally from Prabath Gunawardane, et al. unless noted otherwise

Programming Guide. Aaftab Munshi Dan Ginsburg Dave Shreiner. TT r^addison-wesley

CS770/870 Spring 2017 Open GL Shader Language GLSL

CS770/870 Spring 2017 Open GL Shader Language GLSL

Programming with OpenGL Shaders I. Adapted From: Ed Angel Professor of Emeritus of Computer Science University of New Mexico

Shaders (some slides taken from David M. course)

OPENGL RENDERING PIPELINE

Bump Mapping. Mike Bailey. Oregon State University. What is Bump-Mapping?

Lab 9 - Metal and Glass

Shader Programming 1. Examples. Vertex displacement mapping. Daniel Wesslén 1. Post-processing, animated procedural textures

CSE 167: Lecture #8: GLSL. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2012

Artistic Rendering with Graphics Shaders

Pipeline Operations. CS 4620 Lecture Steve Marschner. Cornell CS4620 Spring 2018 Lecture 11

Computergrafik. Matthias Zwicker Universität Bern Herbst 2016

Information Coding / Computer Graphics, ISY, LiTH GLSL. OpenGL Shading Language. Language with syntax similar to C

WebGL and GLSL Basics. CS559 Fall 2016 Lecture 14 October

CS 130 Final. Fall 2015

Deferred Rendering Due: Wednesday November 15 at 10pm

0.Features of the OpenGL Shading Language

GPU Programming EE Final Examination

Pipeline Operations. CS 4620 Lecture 14

0.Features of the OpenGL Shading Language

Lecture 17: Shading in OpenGL. CITS3003 Graphics & Animation

Mobile Application Programing: Android. OpenGL Operation

CHAPTER 1 Graphics Systems and Models 3

Advanced Shading and Texturing

GLSL Programming. Nicolas Holzschuch

CMPS160 Shader-based OpenGL Programming

Graphics Programming. Computer Graphics, VT 2016 Lecture 2, Chapter 2. Fredrik Nysjö Centre for Image analysis Uppsala University

Lets assume each object has a defined colour. Hence our illumination model is looks unrealistic.

Rasterization Overview

CMSC427 Advanced shading getting global illumination by local methods. Credit: slides Prof. Zwicker

TSBK 07! Computer Graphics! Ingemar Ragnemalm, ISY

Tuesday, 23 March OpenGL Shading Language

Zeyang Li Carnegie Mellon University

GLSL v1.20. Scott MacHaffie Schrödinger, Inc.

The Graphics Pipeline

COMP371 COMPUTER GRAPHICS

Module 13C: Using The 3D Graphics APIs OpenGL ES

Blis: Better Language for Image Stuff Project Proposal Programming Languages and Translators, Spring 2017

Rendering Objects. Need to transform all geometry then

The OpenGL Shading Language

GLSL. OpenGL Shading Language. OpenGL 1.5 Logical Diagram. OpenGL 2.0 Logical Diagram

Texture Mapping and Special Effects

Transcription:

Course 17: State-of-the-Art in Hardware Rendering The OpenGL Shading Language Randi Rost 3Dlabs, Inc.

The OpenGL Shading Language

OpenGL 2.0 OpenGL 2.0 is a vision of where we think OpenGL should be headed An ARB GL2 working group has been formed and is meeting weekly Extensions have been proposed to support the OpenGL Shading Language 3Dlabs, ATI, and others are implementing the proposed extensions OpenGL 2.0 is more than shading stuff, but the shading stuff is most important

OpenGL Shading Language Goals Define a vertex/fragment language that Is specific for OpenGL Exposes flexibility of (near) future hardware Provides hardware independence Is easy to use Will stand the test of time

OpenGL Shading Language Summary Integrated intimately with OpenGL 1.x Incremental replacement of fixed functions C-based, vector and matrix types Virtualizes (some) pipeline resources Same language for vertex and fragment Shaders are treated as objects Shaders are attached to a program object

Vertex Processor A programmable unit that replaces: Vertex transformation Normal transformation, normalization, and rescaling Lighting Color material application Color clamping Texgen & texture coordinate transformation

Vertex Processor Does NOT replace: Perspective projection and viewport mapping Frustrum and user clipping Backface culling Primitive assembly Two sided lighting selection Polygon mode & offset

Fragment processor Replaces: Operations on interpolated values Texture access and application Fog Color sum Pixel zoom Scale and bias Color table lookup Convolution Color matrix

Fragment processor Does NOT replace: Shading model Coverage Histogram Minmax Pixel packing and unpacking Stipple

Fragment Processor Does NOT replace: Pixel ownership test Scissor test Alpha test, Stencil test, Depth test Blending Dithering Logical ops (But these could be done programmably)

Shader Management Shaders and Programs OpenGL processor source code is a shader Shaders are defined as an array of strings A container for shader objects is a program

Shader Management // Create a shader (shaderid(!= 0 if success) shadera = glcreateshaderobjectgl2( shadertype ); // Load source code glloadshadergl2( shadera, numstrings,, strings ); glappendshadergl2( shadera,, string ); // Compile (status = TRUE if success) status = glcompileshadergl2( shadera ); // Get information string from compile infolog = glgetinfologgl2( shadera );

Shader Management // Create a program object programz = glcreateprogramobjectgl2(); // Attach shader object(s) status = glattachshaderobjectgl2( programz, shadera ); status = glattachshaderobjectgl2( programz, shaderb ); // Link the program status = gllinkprogramgl2( programz );

Shader Management // get information on link infolog = glgetinfologgl2( programz ); glgetobjectparameterfvgl2( programz, GL_SHADER_RELATIVE_SIZE_GL2, size ); // Make program current status = gluseprogramobjectgl2( programz );

Vertex Processor Vertex Normal Colors & Materials TexCoordn EdgeFlag Uniform Vertex Shader Temporaries Position ClipVertex FrontColor0 BackColor0 FrontColor1 BackColor1 TexCoordn EyeZ EdgeFlag

Vertex Processor Vertex o Attribute0 Attribute1 Attribute2 Attributen EdgeFlag Uniform Vertex Shader Temporaries Position ClipVertex Varying0 Varying1 Varying2 Varyingn EdgeFlag

Rasterization Rasterization Rasterization EdgeFlag Primitive Primitive Assembly Assembly ClipVertex ClipVertex Position Position Associated Associated Data Data Flatshade Flatshade Culling or Culling or Clipping Clipping Face Face Processing Processing Associated Associated Data Data Coord Point Point Line Line Polygon Polygon Rasterization Rasterization Bitmap Bitmap Rasterization Rasterization Pixel Pixel Rectangle Rectangle Rasterization Rasterization

Fragment Processor FrontColor0 BackColor0 FrontColor1 BackColor1 Uniform Texture TexCoordn EyeZ Depth Stencil FrontFacing Coord Fragment Shader Temporaries Color Depth Stencil FrontFacing Coord

Fragment Processor Varying0 Varying1 Varying2 Uniform Texture Varyingn FragDepth FragStencil FrontFacing FragCoord Fragment Shader Temporaries FragColor FragDatan FragDepth FragStencil FrontFacing FragCoord

Data Types float vec2, vec3, vec4 mat2, mat3, mat4 int bool As in C++, true or false arrays [] to access array element

Type Qualifiers const attribute uniform varying

Expressions constants variables scalar/vector/matrix operations unary and binary ops: +=, -=, *=, /=, %=, <<=, >>=, &=, ^=, =, ++, --,, &&,, ^^, <=, >=, ==,!=, <<, >>,,, =, (), [],,.,!, -,,+, *, /, %, <, >,, ^, &,?

Expressions component accessors for vectors and matrices {x,y,z,w},{r,g,b,a},{s,t,p,q},{0,1,2,3}.rc (matrix element) row and column accessors for matrices.r_ (matrix row)._c (matrix column)

Flow Control expression? trueexpression : falseexpression if, if-else for, while, do-while return, break, continue, kill function calls

User-defined Functions Pass by reference Output keyword Function overloading One return value (can be any type) Scope rules same as C

Built-in in Functions sin, cos,, tan, asin,, etc... pow,, log2, exp2, sqrt, inversesqrt abs, sign, floor, ceil, frac,, mod min, max, clamp mix, step, smoothstep length, distance, normalize, dot, cross texture, lod, dpdx, dpdy, fwidth,, kill, noise

Preprocessor #define #ifdef #else #elif # #endif # #pragma

About These Examples OpenGL Shading Language implementations are just emerging We describe implemented, working shaders rather than theoretical ones We do not claim these are the best examples for each category Focus is on illustrating the language rather than describing the algorithm Demo hardware is the 3Dlabs Wildcat VP870

Example 1: Wood Shader

Wood Shader Overview Part of ogl2demo developed by 3Dlabs Uses a tileable,, 4-octave 4 noise function that is precomputed and stored in a 3D texture Vertex shader is trivial Fragment shader indexes the 3D texture and uses the result to procedurally generate a 3D wood texture

Theory of Wood The wood is composed of light and dark areas alternating in concentric cylinders surrounding a central axis. Noise is added to warp the cylinders to create a more natural-looking looking pattern The center of the tree is taken to be the y- y axis Throughout the wood there is a high- frequency grain pattern

Wood Vertex Shader varying float lightintensity; varying vec3 Position; uniform vec3 LightPosition; uniform float Scale; void main(void) { vec4 pos = gl_modelviewmatrix * gl_vertex; Position = vec3(gl gl_vertex) * Scale; vec3 tnorm = normalize(gl gl_normalmatrix * gl_normal); lightintensity = abs(dot(normalize(lightposition vec3(pos)), tnorm) ) * 1.5); gl_position = gl_modelviewprojectionmatrix * gl_vertex; }

Wood Fragment Shader (1 of 3) varying float lightintensity; varying vec3 Position; uniform vec3 LightWood; uniform vec3 DarkWood; uniform float RingNoise; uniform float RingFreq; uniform float LightGrains; uniform float DarkGrains; uniform float GrainThreshold; uniform vec3 NoiseScale; uniform float Noisiness; uniform float GrainScale;

Wood Fragment Shader (2 of 3) void main (void) { vec3 location; vec3 noisevec; vec3 color; float dist; float r; noisevec = texture3(6, Position * NoiseScale) ) * Noisiness; location = vec3 (Position + noisevec); dist = sqrt(location.x * location.x + location.z * location.z) * RingFreq; r = fract(dist + noisevec.0 + noisevec.1 + noisevec.2) * 2.0;

Wood Fragment Shader (3 of 3) if (r > 1.0) r = 2.0 - r; color = mix(lightwood LightWood, DarkWood,, r); r = fract((position.x + Position.z) * GrainScale + 0.5); noisevec.2 *= r; if (r < GrainThreshold) color += LightWood * LightGrains * noisevec.2; else color -= LightWood * DarkGrains * noisevec.2; color = clamp(color * lightintensity,, 0.0, 1.0); } gl_fragcolor = vec4(color, 1.0);

Wood Shader Demo

Example 2: Bumpy/Shiny Shader

Bumpy/Shiny Shader Overview Part of ogl2demo developed by 3Dlabs Vertex shader is trivial Fragment shader computes procedural bumps and the final index into the environment map Environment maps courtesy of Jerome Dewhurst, jerome@photographica.co.uk, http://www.photographica.co.uk photographica.co..co.uk

Bumpy/Shiny Algorithm Compute normalized eye vector and normalized normal in vertex shader Compute reflection direction R in fragment shader Use R to obtain altitude and azimuth angles at each fragment Use altitude as environment map vertical index, azimuth as horizontal index Use a cheesy hack to perturb this lookup if the fragment is within a bump

Environment Map A single 2D texture containing an equirectangular image (360 o horiz,, 180 o vert)

Bumpy/Shiny Vertex Shader varying vec3 Normal; varying vec3 EyeDir; void main(void) { gl_position = gl_modelviewprojectionmatrix * gl_vertex; gl_texcoord0 = gl_multitexcoord0; Normal = normalize(gl gl_normalmatrix * gl_normal); EyeDir = normalize(vec3(gl gl_modelviewmatrix * gl_vertex)); }

Bumpy/Shiny Fragment Shader (1 of 3) uniform vec3 Xunitvec; uniform vec3 Yunitvec; varying vec3 Normal; varying vec3 EyeDir; const float Density = 6.0; const float Coeff1 = 5.0; float Size = 0.15; void main (void) { // Compute reflection vector. Should really normalize // EyeDir and Normal, but this is OK for some objects vec3 reflectdir = reflect(eyedir EyeDir,, Normal);

Bumpy/Shiny Fragment Shader (2 of 3) // Compute cosine of altitude and azimuth angles vec2 index; index.1 = dot(normalize(reflectdir reflectdir), Yunitvec); reflectdir.y = 0.0; index.0 = dot(normalize(reflectdir reflectdir), Xunitvec); // Compute bump locations vec2 c = Density * vec2(gl gl_texcoord0); vec2 offset = fract(c) - vec2(0.5); float d = offset.x * offset.x + offset.y * offset.y; if (d >= Size) offset = vec2(0.0);

Wood Fragment Shader (3 of 3) // Translate index to (0.5, 0.5) and make offset noṉ-linear non index = (index + 1.0) * 0.5 Coeff1 * offset * offset * offset; // Do a lookup into the environment map vec3 envcolor = texture3(4, index); } gl_fragcolor = vec4(envcolor envcolor,, 1.0);

Bumpy/Shiny Shader Demo

Example 3: BRDF Shader

Rendering BRDF Data With PTMs BRDF data is from Cornell Samples are automotive paints from Ford BRDF data was encoded in a polynomial texture map (PTM) by Hewlett-Packard Shaders and textures used were provided courtesy of Hewlett-Packard Example shown is mystique lacquer, which changes color with viewing angle Realism is the goal

Creating a BRDF PTM For each texel in the PTM: Sample the BRDF data varying view angle and light position to obtain the data points to be represented Perform a biquadric least squares curve fit Store the coefficient values of the resulting polynomial in the texture(s) e.g., f(u,v) = Au 2 + Bv 2 + Cuv + Du + Ev + F This process is partly black magic

Application Setup At each vertex, application passes vertex, normal, tangent, and binormal For RGB PTMs and BRDF RGB PTMs, there are separate coefficient textures for each of R, G, and B Application enables red channel, binds red coefficient textures, renders geometry Repeat for green and blue channels using same shaders

BRDF PTM Vertex Shader Overview Infinite viewer, local light source Goal of vertex shader is to produce 2D parameterization of light position Texture coordinate used to reference PTM Texture coordinate used to reference light factor Half angle/difference vector formulation is used to achieve better quality Use user-defined attributes to pass tangent and binormal

BRDF PTM Vertex Shader (1 of 4) // VP_PTM_3DLABS_HuHvDuDv HuHvDuDv_PosInf uniform vec3 LightPos; uniform vec3 EyeDir; attribute vec3 Tangent; attribute vec3 Binormal; varying float LdotT; varying float LdotB; varying float LdotN; void main(void) { vec3 lighttemp; vec3 halfangletemp; vec3 tprime; vec3 bprime;

BRDF PTM Vertex Shader (2 of 4) // Transform vertex gl_position = gl_modelviewprojectionmatrix * gl_vertex; lighttemp = normalize(lightpos - vec3(gl gl_vertex)); // Calculate the Half Angle vector halfangletemp = normalize(eyedir + lighttemp); // Multiply the Half Angle vector by NOISE_FACTOR // to avoid noisy BRDF data halfangletemp = halfangletemp * 0.9; // Calculate T' and B' // T' = T - (T.H)H tprime = Tangent (halfangletemp * dot(tangent, halfangletemp)); tprime = normalize(tprime tprime);

BRDF PTM Vertex Shader (3 of 4) // B' = H x T' bprime = cross(halfangletemp halfangletemp, tprime); // Hu = Dot(Light, T') // Hv = Dot(Light, B') LdotT = dot(lighttemp lighttemp, tprime); LdotB = dot(lighttemp lighttemp, bprime); // Du = Dot(HalfAngle HalfAngle,, T) // Dv = Dot(HalfAngle HalfAngle,, B) // Remap [-1.0..1.0] to [0.0..1.0] gl_texcoord0.s = dot(tangent, halfangletemp) ) * 0.5 + 0.5; gl_texcoord0.t = dot(binormal Binormal, halfangletemp) ) * 0.5 + 0.5;

BRDF PTM Vertex Shader (4 of 4) gl_texcoord0.q = 1.0; } // "S" Text Coord3: Dot(Light, Normal); LdotN = dot(lighttemp lighttemp, gl_normal) * 0.5 + 0.5;

BRDF PTM Fragment Shader Overview Goal of fragment shader is Look up polynomial coefficients A, B, C, D, E, F based on incoming texture coordinate Apply coefficient scale and bias factors Compute the polynomial Use LdotN value to look up a light factor Multiply and clamp to produce final color

BRDF PTM Fragment Shader (1 of 3) // PP_PTM_3DLABS_RGB const int ABCtexture = 0; const int DEFtexture = 1; const int lighttexture = 3; uniform vec3 ABCscale, ABCbias; uniform vec3 DEFscale, DEFbias; varying float LdotT; // passes the computed L*Tangent value varying float LdotB; // passes the computed L*Binormal value varying float LdotN; // passes the computed L*Normal value void main(void) { vec3 ABCcoef, DEFcoef; vec3 ptvec; float ptval;

BRDF PTM Fragment Shader (2 of 3) // Read coefficient values and apply scale and bias factors ABCcoef = (texture3(abctexture ABCtexture, gl_texcoord0, 0.0) - ABCbias) ) * ABCscale; DEFcoef = (texture3(deftexture DEFtexture, gl_texcoord0, 0.0) - DEFbias) ) * DEFscale; // Compute polynomial ptval = ABCcoef.0 * LdotT * LdotT + ABCcoef.1 * LdotB * LdotB + ABCcoef.2 * LdotT * LdotB + DEFcoef.0 * LdotT + DEFcoef.1 * LdotB + DEFcoef.2;

BRDF PTM Fragment Shader (3 of 3) // Turn result into a vec3 ptvec = vec3 (ptval( ptval); // Multiply result * light factor ptvec *= texture3(lighttexture lighttexture, LdotN); vec3 fragc = clamp(ptvec ptvec,, vec3 (0.0), vec3 (1.0)); } // Clamp result and assign it to gl_fragcolor gl_fragcolor = vec4 (fragc( fragc,, 1.0);

BRDF Demo

Other Demos Time permitting

Feedback We're at the point where we can apply an OGL2 shader through our (Houdini) interface and (given an equivalent VEX shader) watch the software renderer (Mantra) draw the same thing but much, much slower :-). It's one of those jaw dropping "wow" moments actually, so we thank you for making that happen!... It rocks. Having read the original white paper still did not prepare us to see it actually working. The ease with which we can now define & adjust OGL2 shaders is astonishing. Unsolicited email from Paul Salvini, CTO, Side Effects Software

More Info www.3dlabs.com/developer/ogl2 These slides will be posted there in a few days Updated course notes will also be there eventually OpenGL BOF Wednesday 4:30 p.m. Various ISVs will be speaking in the 3Dlabs booth Tue-Thu: Thu: LightWork Design, Vital Images, Volume Graphics, Terrex,, University of Magdeburg