GPU Shading and Rendering: OpenGL Shading Language Marc Olano UMBC.

Slides:



Advertisements
Similar presentations
COMPUTER GRAPHICS SOFTWARE.
Advertisements

Perspective aperture ygyg yryr n zgzg y s = y g (n/z g ) ysys y s = y r (n/z r ) zrzr.
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.
Shading CMSC 435/634. RenderMan Light Displacement Surface Volume Imager.
Informationsteknologi Wednesday, December 12, 2007Computer Graphics - Class 171 Today’s class OpenGL Shading Language.
MAT 594CM S10Fundamentals of Spatial ComputingAngus Forbes Week 4 : GLSL Shaders Topics: Shader programs, vertex & fragment shaders, passing data into.
MAT 594CM S10Fundamentals of Spatial ComputingAngus Forbes Week 5 : GLSL Shaders Topics: Shader syntax, passing textures into shaders, per-pixel lighting,
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.
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.
GAM532 DPS932 – Week 1 Rendering Pipeline and Shaders.
OpenGL 3.0 Texture Arrays Presentation: Olivia Terrell, Dec. 4, 2008.
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.
Real-time Graphical Shader Programming with Cg (HLSL)
OpenGL Shading Language (Advanced Computer Graphics) Ernest Tatum.
GPU Shading and Rendering Shading Technology 8:30 Introduction (:30–Olano) 9:00 Direct3D 10 (:45–Blythe) Languages, Systems and Demos 10:30 RapidMind.
Programmable Pipelines. 2 Objectives Introduce programmable pipelines ­Vertex shaders ­Fragment shaders Introduce shading languages ­Needed to describe.
Shading CMSC 435/634. RenderMan Light Displacement Surface Volume Imager.
Shading CMSC 435/634.
CSE 690: GPGPU Lecture 6: Cg Tutorial Klaus Mueller Computer Science, Stony Brook University.
Lecture by: Martin Deschamps CSE 4431
© Copyright OpenGL ARB, Page 1 Ecosystem Working Group Status Update Ecosystem Working Group Status Update Chair: Benj Lipchak, ATI.
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.
Shaders in OpenGL Marshall Hahn. Introduction to Shaders in OpenGL In this talk, the basics of OpenGL Shading Language will be covered. This includes.
CS418 Computer Graphics John C. Hart
Shading in OpenGL Ed Angel Professor Emeritus of Computer Science University of New Mexico 1 E. Angel and D. Shreiner: Interactive Computer Graphics 6E.
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)
GLSL II.
GLSL II Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts Director, Arts Technology Center University of New.
Week 3 Lecture 4: Part 2: GLSL I Based on Interactive Computer Graphics (Angel) - Chapter 9.
 Learn some important functions and process in OpenGL ES  Draw some triangles on the screen  Do some transformation on each triangle in each frame.
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.
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.
OpenGl Shaders Lighthouse3d.com.
GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global.
GPU Computing for GIS James Mower Department of Geography and Planning University at Albany.
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.
Shaders, part 2 alexandri zavodny.
Shader.
Programmable Pipelines
Graphics Processing Unit
Chapter 6 GPU, Shaders, and Shading Languages
Shading CMSC 435/634.
GLSL I Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts Director, Arts Technology Center University of New Mexico.
Introduction to Computer Graphics with WebGL
Day 05 Shader Basics.
Chapter VI OpenGL ES and Shader
Introduction to Shaders
ICG 2018 Fall Homework1 Guidance
Computer Graphics Practical Lesson 10
Programming with OpenGL Part 3: Shaders
Debugging Tools Tim Purcell NVIDIA.
CIS 441/541: Introduction to Computer Graphics Lecture 15: shaders
CS 480/680 Computer Graphics GLSL Overview.
OpenGL-Rendering Pipeline
CS 480/680 Fall 2011 Dr. Frederick C Harris, Jr. Computer Graphics
Presentation transcript:

GPU Shading and Rendering: OpenGL Shading Language Marc Olano UMBC

OpenGL Shading High level language –OpenGL Shading Language = GLslang = GLSL Integrated into OpenGL API (no extra run-time)

Organization API Vertex Shading Fragment Shading Lots of demos. –2-year old Apple PowerBook G4/1.5GHz –ATI Mobility Radeon 9700

API-integrated Compiler built into driver –Presumably they know your card best –IHV’s must produce (good) compilers Use built-in parameters (glColor, glNormal, …) –Add your own Other options can still produce low-level code –Cg, ASHLI, RapidMind, … –With loss of integration

Using High-level Code Create shader object S = glCreateShader(GL_VERTEX_SHADER) S = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB) –Vertex or Fragment Load shader into object glShaderSource(S, n, shaderArray, lenArray) glShaderSourceARB(S, n, shaderArray, lenArray) –Array of strings Compile object glCompileShader(S) glCompileShaderARB(S)

Loading Shaders glShaderSource(S, n, shaderArray, lenArray) –One string containing entire mmap’d file –Strings as #includes Varying variables between vertex and fragment –Strings as lines Null-terminated if lenArray is Null or length=-1

Using High-level Code (2) Create program object P = glCreateProgram() P = glCreateProgramObjectARB() Attach all shader objects glAttachShader(P, S) glAttachObjectARB(P, S) –Vertex, Fragment or both Link together glLinkProgram(P) glLinkProgramARB(P) Use glUseProgramObject(P) glUseProgramObjectARB(P)

Using High-level Code (3) Where is my attributes/uniforms parameter? i=glGetAttribLocation(P,”myAttrib”) i=glGetUniformLocation(P,”myAttrib”) Set them glVertexAttrib1f(i,value) glVertexAttribPointer(i,…) glUniform1f(i,value)

Using Low-level Code Load shader glProgramStringARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, length, shader) –Vertex or fragment –Single string (vs. array) Enable glEnable(GL_VERTEX_PROGRAM_ARB)

Useful Tools Shader debugger –Immediate updates –Choose model/texture –Tweak parameters –Examine/dump frames Several available –Not hard to build OpenGL debugger –Trace of calls made –Examine resources –Breakpoints/actions –Graph performance A couple of choices

gDEBugger – A Professional OpenGL Debugger and Profiler Provides graphic pipeline information needed to find bugs and to optimize application performance: –Shortens debugging and profiling time –Improves application quality –Optimizes application performance

Free gDEBugger License for Academic Users! OpenGL ARB and Graphic Remedy Academic Program: –Annual program for all OpenGL Academic users –License of the full feature version for one year –Includes all software updates –A limited number of free licenses available for non-commercial developers who are not in academia More details:

Non-windows OS Linux –gDEBugger in progress Apple OpenGL Profiler and Driver Monitor –Free part of OS / Developer tools

Vertex Demo: Blend Positions

High-level Code void main() { float Kin = gl_Color.r; // key input // screen position from vertex and texture vec4 Vp = ftransform(); vec4 Tp = vec4(gl_MultiTexCoord0.xy*1.8-.9, 0.,1.); // interpolate between Vp and Tp gl_Position = mix(Tp,Vp,pow(1.-Kin,8.)); // copy to output gl_TexCoord[0] = gl_MultiTexCoord0; gl_TexCoord[1] = Vp; gl_TexCoord[3] = vec4(Kin); }

Main Function void main() { float Kin = gl_Color.r; // key input // screen position from vertex and texture vec4 Vp = ftransform(); vec4 Tp = vec4(gl_MultiTexCoord0.xy*1.8-.9, 0.,1.); // interpolate between Vp and Tp gl_Position = mix(Tp,Vp,pow(1.-Kin,8.)); // copy to output gl_TexCoord[0] = gl_MultiTexCoord0; gl_TexCoord[1] = Vp; gl_TexCoord[3] = vec4(Kin);}

Use Standard OpenGL State void main() { gl_Color float Kin = gl_Color.r; // key input // screen position from vertex and texture vec4 Vp = ftransform(); gl_MultiTexCoord0 vec4 Tp = vec4(gl_MultiTexCoord0.xy*1.8-.9, 0.,1.); // interpolate between Vp and Tp gl_Position gl_Position = mix(Tp,Vp,pow(1.-Kin,8.)); // copy to output gl_TexCoordgl_MultiTexCoord0 gl_TexCoord[0] = gl_MultiTexCoord0; gl_TexCoord gl_TexCoord[1] = Vp; gl_TexCoord gl_TexCoord[3] = vec4(Kin); }

Built-in Types void main() { float float Kin = gl_Color.r; // key input // screen position from vertex and texture vec4 vec4 Vp = ftransform(); vec4 vec4 Tp = vec4(gl_MultiTexCoord0.xy*1.8-.9, 0.,1.); // interpolate between Vp and Tp gl_Position = mix(Tp,Vp,pow(1.-Kin,8.)); // copy to output [0] gl_TexCoord[0] = gl_MultiTexCoord0; [1] gl_TexCoord[1] = Vp; [3] gl_TexCoord[3] = vec4(Kin); }

Swizzle / Channel Selection void main() {.r float Kin = gl_Color.r; // key input // screen position from vertex and texture vec4 Vp = ftransform();.xy vec4 Tp = vec4(gl_MultiTexCoord0.xy*1.8-.9, 0.,1.); // interpolate between Vp and Tp gl_Position = mix(Tp,Vp,pow(1.-Kin,8.)); // copy to output gl_TexCoord[0] = gl_MultiTexCoord0; gl_TexCoord[1] = Vp; gl_TexCoord[3] = vec4(Kin); }

Vector Construction void main() { float Kin = gl_Color.r; // key input // screen position from vertex and texture vec4 Vp = ftransform(); vec4(gl_MultiTexCoord0.xy*1.8-.9, 0.,1.); vec4 Tp = vec4(gl_MultiTexCoord0.xy*1.8-.9, 0.,1.); // interpolate between Vp and Tp gl_Position = mix(Tp,Vp,pow(1.-Kin,8.)); // copy to output gl_TexCoord[0] = gl_MultiTexCoord0; gl_TexCoord[1] = Vp; vec4(Kin) gl_TexCoord[3] = vec4(Kin); }

Built-in Functions void main() { float Kin = gl_Color.r; // key input // screen position from vertex and texture ftransform() vec4 Vp = ftransform(); vec4 Tp = vec4(gl_MultiTexCoord0.xy*1.8-.9, 0.,1.); // interpolate between Vp and Tp mix(pow()) gl_Position = mix(Tp,Vp,pow(1.-Kin,8.)); // copy to output gl_TexCoord[0] = gl_MultiTexCoord0; gl_TexCoord[1] = Vp; gl_TexCoord[3] = vec4(Kin); }

Vertex + Fragment Demo: Fresnel Environment Map

Trick #1: Where is the Eye Object Space ModelView Matrix Eye Space Projection Matrix Projection Matrix Clip Space Where is the Eye in Eye Space? –(0,0,0)? Not necessarily! Know where it is in Clip Space –(0,0,-1,0), looking in the (0,0,1,0) direction Assuming GL_LESS depth test –Invert projection to find the eye! –Works for any eye position, or even parallel projection.

Trick #2: Subtract Homogeneous Points Homogeneous point: vec4(V.xyz, V.w) –3D equivalent: V.xyz/V.w –Defers division, makes perspective, translation, and many things happy Vector subtraction: V–E –V.xyz/V.w – E.xyz/E.w –(V.xyz*E.w – E.xyz*V.w)/(V.w*E.w)

Trick #3: Skip Division for Normalize normalize(V.xyz/V.w) = normalize(V.xyz) –If V.w isn’t negative Put it all together: –normalize(V-E) –= normalize(V.xyz*E.w - E.xyz*V.w)

OpenGL State Demo: Vertex Lighting

Lighting Vectors in Eye Space void main() { // convert shading-related vectors to eye space vec4 P = gl_ModelViewMatrix*gl_Vertex; vec4 E = gl_ProjectionMatrixInverse*vec4(0,0,-1,0); vec3 V = normalize(E.xyz*P.w-P.xyz*E.w); vec3 N = normalize(gl_NormalMatrix*gl_Normal); …

Accumulate Each Light … // accumulate contribution from each light gl_FrontColor = vec4(0); for(int i=0; i<gl_MaxLights; i++) { vec3 L = normalize(gl_LightSource[i].position.xyz*P.w - P.xyz*gl_LightSource[i].position.w); vec3 H = normalize(L+V); float diff = dot(N,L); gl_FrontColor += gl_LightSource[i].ambient; if (diff > 0.) { gl_FrontColor += gl_LightSource[i].diffuse * diff; gl_FrontColor += gl_LightSource[i].specular * max(pow(dot(N,H), gl_FrontMaterialShininess),0.); } …

Standard Vertex Shader Stuff … // standard texture coordinate and position stuff gl_TexCoord[0] = gl_TextureMatrix[0]*gl_MultiTexCoord0; gl_Position = ftransform(); }

Noise Controlled, repeatable randomness –Still spotty implementation –Can use texture or compute

Noise Characteristics Repeatable Locally continuous but distant points uncorrolated values [-1,1], average 0 1/2 – 1 cycle per unit Versions for n-D input

Noise Subtleties Many noise functions based on a lattice –Like a spline between integer coordinates –Hash of integer coordinates  control points Interpolating values easy but poor –Even with higher-order interpolation Perlin’s noise –Passes through 0 at each integer –Hash gives gradient

Modified Noise [Olano 2005] Three relatively independent modifications –New computable hash –Change gradient computation –Reorder computation Variety of computation/texture options –Can just store in a texture –Can compute with some texture accesses –Can compute with no texture accesses

Computable Hash Normal hash chains access to permutation texture Want totally computable hash –mod(k*x 2, m) –Still chain for higher-D hash(floor(P.x) + hash(floor(P.y))) –Not quite as good, but cheap & computable Noise usually not used alone

Gradient 3D Gradient = (±fract(P.x), ±fract(P.y)) –Each sign from one bit of hash Made slightly more difficult without bitwise ops –Allows noise(x) = noise(x,0) If 2D noise is stored in a texture Can share the same texture for 1D noise as well –Not normally true!

Reordered Computation Refactor to be able to build n-D noise from two shifted calls to n-1 D noise –If 2D noise is stored in a texture –Can build 3D noise from 2 texture accesses –Can build 4D noise from 4 texture accesses

Shader Design Strategies Learn and adapt from RenderMan –Noise –Layers Multiple Passes Baked computation