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

Similar documents
Programmable Graphics Hardware

12.2 Programmable Graphics Hardware

Teaching Cg. This presentation introduces Cg ( C for graphics ) and explains why it would be useful when teaching a computer graphics course.

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

Supplement to Lecture 22

3D buzzwords. Adding programmability to the pipeline 6/7/16. Bandwidth Gravity of modern computer systems

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

Programming shaders & GPUs Christian Miller CS Fall 2011

Shaders. Introduction. OpenGL Grows via Extensions. OpenGL Extensions. OpenGL 2.0 Added Shaders. Shaders Enable Many New Effects

Programmable Graphics Hardware

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

CS GPU and GPGPU Programming Lecture 7: Shading and Compute APIs 1. Markus Hadwiger, KAUST

Introduction to Programmable GPUs CPSC 314. Introduction to GPU Programming CS314 Gordon Wetzstein, 09/03/09

Introduction to the OpenGL Shading Language

Shaders. Slide credit to Prof. Zwicker

Introduction to Shaders.

Shaders CSCI 4239/5239 Advanced Computer Graphics Spring 2014

COMP371 COMPUTER GRAPHICS

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

CS GPU and GPGPU Programming Lecture 3: GPU Architecture 2. Markus Hadwiger, KAUST

CS GPU and GPGPU Programming Lecture 3: GPU Architecture 2. Markus Hadwiger, KAUST

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

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

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

Programmable shader. Hanyang University

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

Shaders CSCI 4229/5229 Computer Graphics Fall 2017

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

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

Basics of GPU-Based Programming

SHADER PROGRAMMING. Based on Jian Huang s lecture on Shader Programming

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

Introduction to Programmable GPUs CPSC 314. Real Time Graphics

Objectives. Open GL Shading Language (GLSL)

Graphics Hardware. Graphics Processing Unit (GPU) is a Subsidiary hardware. With massively multi-threaded many-core. Dedicated to 2D and 3D graphics

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

OPENGL RENDERING PIPELINE

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

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

Getting Started with Cg. Release 1.2 February 2004

Programmable Graphics Hardware

CS4621/5621 Fall Computer Graphics Practicum Intro to OpenGL/GLSL

Cg 2.0. Mark Kilgard

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

Advanced Computer Graphics (CS & SE ) Lecture 7

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

CSE 167: Introduction to Computer Graphics Lecture #13: GLSL. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2015

Computer Graphics Coursework 1

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

Dave Shreiner, ARM March 2009

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

Computer Graphics (CS 543) Lecture 10: Soft Shadows (Maps and Volumes), Normal and Bump Mapping

The Rasterization Pipeline

Real - Time Rendering. Graphics pipeline. Michal Červeňanský Juraj Starinský

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

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

CMPS160 Shader-based OpenGL Programming

OpenGL shaders and programming models that provide object persistence

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

Lecturer Athanasios Nikolaidis

Sung-Eui Yoon ( 윤성의 )

Cg: A system for programming graphics hardware in a C-like language

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

1 of 5 9/10/ :11 PM

Methodology for Lecture. Importance of Lighting. Outline. Shading Models. Brief primer on Color. Foundations of Computer Graphics (Spring 2010)

Pipeline Operations. CS 4620 Lecture 14

Introduction to the OpenGL Shading Language (GLSL)

Mobile graphics API Overview

Computer Graphics (CS 543) Lecture 1 (Part 1): Introduction to Computer Graphics

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

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

Computergrafik. Matthias Zwicker Universität Bern Herbst 2016

Tuesday, 23 March OpenGL Shading Language

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

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

Lab 9 - Metal and Glass

Introduction to OpenGL

Programming Graphics Hardware

Homework 3: Programmable Shaders

Computer Graphics Disciplines. Grading. Textbooks. Course Overview. Assignment Policies. Computer Graphics Goals I

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

CIS 536/636 Introduction to Computer Graphics. Kansas State University. CIS 536/636 Introduction to Computer Graphics

Computer Graphics (CS 543) Lecture 8a: Per-Vertex lighting, Shading and Per-Fragment lighting

Lecture 2. Shaders, GLSL and GPGPU

by Emmanuel Agu Dr. Emmanuel Agu (professor, Emmanuel ) Research areas

Zeyang Li Carnegie Mellon University

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

Sign up for crits! Announcments

TSBK 07! Computer Graphics! Ingemar Ragnemalm, ISY

Shading Languages for Graphics Hardware

frame buffer depth buffer stencil buffer

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

Programming Graphics Hardware

CS559 Computer Graphics Fall 2015

Lecture 13 of 41. Surface Detail 4 of 5: Pixel & Vertex Shaders Lab 2b: Shading in Direct3D

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

Lecture 09: Shaders (Part 1)

GpuPy: Accelerating NumPy With a GPU

lecture 18 - ray tracing - environment mapping - refraction

Transcription:

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

Talk Summary Topic Coverage Define Shading Languages (loosely) High Level View of GPU Functional Aspects of GPU Example Shaders (GPU programs) 2

Who Am I? Ph.D. Student @ WPI Advisor = Emmanuel Interests: Computational Photography Real time Rendering Photorealistic Rendering GPU Algorithms Done: Published Papers, M.S. Thesis 3

Some Work We ve Done Samples 4

Back To Lecture Q: What is a Programmable GPU & Why do we need it? A: OpenGL Fixed Function: Can only select from pre defined effects (90 s) E.g. Only two shading models pre defined Industry needs flexibility (new effects) GPU Shaders = programmability + access to GPU internals 5

History Of Real Time Graphics Virtua Fighter Dead or Alive 3 Dawn (SEGA Corporation) (Tecmo Corporation) (NVIDIA Corporation) NV1 50K triangles/sec 1M pixel ops/sec Xbox (NV2A) 100M triangles/sec 1G pixel ops/sec GeForce FX (NV30) 200M triangles/sec 2G pixel ops/sec 1995 2001 2003 6

Examples of New Effects Complex Materials Lighting Environments Shadowing Advanced Mapping 7

History of Shading Languages Big Players RenderMan Pixar, software based in toy story Cg nvidia, 1st commercial SL HLSL M$/NVidia, Cg & Xbox project (Cg/HLSL Fork) GLSL SGI, ARB/3DLabs Stanford RTSL Academic SLs Several others more recently 8

The Motivation for Shading Languages Graphics hardware has become increasingly more powerful Programming powerful hardware with assembly code is hard Most GPUs supports programs more than 1,000 assembly instructions long Programmers need the benefits of a high level language: language Easier programming Easier code reuse Easier debugging Assembly DP3 RSQ MUL MOV MUL DP3 RSQ MUL ADD DP3 RSQ MUL DP3 MAX MOV MOV LIT... R0, c[11].xyzx, c[11].xyzx; R0, R0.x; R0, R0.x, c[11].xyzx; R1, c[3]; R1, R1.x, c[0].xyzx; R2, R1.xyzx, R1.xyzx; R2, R2.x; R1, R2.x, R1.xyzx; R2, R0.xyzx, R1.xyzx; R3, R2.xyzx, R2.xyzx; R3, R3.x; R2, R3.x, R2.xyzx; R2, R1.xyzx, R2.xyzx; R2, c[3].z, R2.x; R2.z, c[3].y; R2.w, c[3].y; R2, R2; float3 cspecular = pow(max(0, dot(nf, H)), phongexp).xxx; float3 cplastic = Cd * (cambient + cdiffuse) + Cs * cspecular; 9

Where Can I Use Shader Programming Students who learn Cg can apply their skills in a variety of situations Actual Screenshots of Linux Same Shader on Graphics APIs Mac OS X OpenGL DirectX Different Platforms (2 of 5 on ATI Hardware) Operating Systems Windows Linux OpenGL DirectX 9.0 compliant GPU DirectX 8.0 compliant GPU Mac OS Graphics Hardware NVIDIA GPUs ATI GPUs Other GPUs that support OpenGL and DirectX 9 10

Shader Pipeline Like Traditional Hardware Graphics Pipeline But: Has Programmable Stages Control Primitives In Pipeline e.g. Skinning, Animation Not Limited In Rendering Style Whatever Material Desired Decouple Rendering From Application 11

Shader Pipeline Programmable Graphics Pipeline 12

Programmable Pipeline Programmable Functionality Exposed via small programs Language similar to c/c++ Hardware support highly variable Vertex Shaders Input: Application geometry & per vertex attributes Transform input in a meaningful way Fragment Shaders Input: Perspective Correct Attributes (interpolated) Transform input into color or discard 13

Recent Advances Geometry Shaders Texture Fetching Within Vertex Shaders 14

In General Some Fixed Functions Are Bypassed Vertex Tasks Vertex Transformation Normal Transformation, Normalization Lighting Texture Coordinate Generation and Transformation Fragment Tasks Texture accesses Fog Discard Fragment 15

Rendering Pipeline All Operations Performed By Programmer Same Stages As Fixed Function Inject Code: Vertex & Fragment Programs 16 Image courtesy of Nvidia

Rendering Pipeline 17 Image courtesy of Nvidia

Anatomy Of GLSL: OpenGL State Built in Variables Always prefaced with gl_ Accessible to both vertex and fragment shaders Uniform Variables Matrices (i.e. ModelViewMatrix, ProjectionMatrix, inverses, transposes) Materials (in MaterialParameters struct, ambient, diffuse, etc.) Lights (in LightSourceParameters struct, specular, position, etc.) Varying Variables FrontColor for colors TexCoord[] for texture coordinates 18

Anatomy Of GLSL: Special Vars Vertex Shaders Have access to several vertex attributes: gl_color, gl_normal, gl_vertex, etc. Also write to special output variables: gl_position, gl_pointsize, etc. Fragment Shaders Have access to special input variables: gl_fragcoord, gl_frontfacing, etc. Also write to special output variables: gl_fragcolor, gl_fragdepth, etc. user defined attribs. gl_color gl_normal gl_vertex Texture Maps gl_color gl_vertex Texture Maps Vertex Shader Standar OpenGL attribs. Fragment Shader gl_color user defined attribs. Built in MV MVP user defined attribs. (normal) Built in MV MVP 19

Example: Phong Shader Questions? Goals Phong Illumination Review (1 slide) C/C++ Application Setup Vertex Shader Fragment Shader Debugging 20

Phong Shader Review Illum = ambient + diffuse + specular = Ka x I + Kd x I x (cos θ ) + Ks x I x cosn(φ ) n l θ φ r e φ e Specular Lobe r v 21 [Diagram Courtesy of E. Agu]

Phong Shader: Setup Steps Step 1: Create Shaders Create handles to shaders Step 2: Specify Shaders load strings that contain shader source Step 3: Compiling Shaders Actually compile source (check for errors) Step 4: Creating Program Objects Program object controls the shaders Step 5: Attach Shaders to Programs Attach shaders to program obj via handle Step 6: Link Shaders to Programs Another step similar to attach Step 7: Enable Program Finally, let GPU know shaders are ready 22

Phong Shader: App Setup GLhandleARB phongvs, phongkfs, phongprog; // handles to objects // Step 1: Create a vertex & fragment shader object phongvs = glcreateshaderobjectarb(gl_vertex_shader_arb); phongfs = glcreateshaderobjectarb(gl_fragment_shader_arb); // Step 2: Load source code strings into shaders glshadersourcearb(phongvs, 1, &phongvs_string, NULL); glshadersourcearb(phongfs, 1, &phongfs_string, NULL); // Step 3: Compile the vertex, fragment shaders. glcompileshaderarb(phongvs); glcompileshaderarb(phongfs); // Step 4: Create a program object phongprog = glcreateprogramobjectarb(); // Step 5: Attach the two compiled shaders glattachobjectarb(phongprog, phongvs); glattachobjectarb(phongprog, phongfs); // Step 6: Link the program object gllinkprogramarb(phongprog); // Step 7: Finally, install program object as part of current state gluseprogramobjectarb(phongprog); 23

Phong Shader: Vertex This Shader Does Gives eye space location for v Transform Surface Normal Transform Vertex Location l θ n e φ r v varying vec3 N; varying vec3 v; void main(void) { v = vec3(gl_modelviewmatrix * gl_vertex); N = normalize(gl_normalmatrix * gl_normal); } Created For Use Within Frag Shader gl_position = gl_modelviewprojectionmatrix * gl_vertex; (Update OpenGL Built in Variable for Vertex Position) 24

Phong Shader: Fragment varying vec3 N; varying vec3 v; l Passed in From VS θ void main (void) { // we are in Eye Coordinates, so EyePos is (0,0,0) vec3 L = normalize(gl_lightsource[0].position.xyz - v); vec3 E = normalize(-v); vec3 R = normalize(-reflect(l,n)); n e φ r v //calculate Ambient Term: vec4 Iamb = gl_frontlightproduct[0].ambient; //calculate Diffuse Term: vec4 Idiff = gl_frontlightproduct[0].diffuse * max(dot(n,l), 0.0); // calculate Specular Term: vec4 Ispec = gl_frontlightproduct[0].specular * pow(max(dot(r,e),0.0), gl_frontmaterial.shininess); } // write Total Color: gl_fragcolor = gl_frontlightmodelproduct.scenecolor + Iamb + Idiff + Ispec; 25

Phong Shader: Debugging **Many things will silently fail during setup** No good automatic debugging tools for GLSL yet exist Common show stoppers: Typos in shader source Assuming implicit type conversion Attempting to pass data to undeclared varying/uniform variables Extremely important to check error codes, use status functions like: glgetobjectparameter{i f}varb (GLhandleARB shader, GLenum whattocheck, GLfloat *statusvals) Subtle Problems Type over flow Shader too long Use too many registers 26

Phong Shader: Demo Click Me! 27

GPU: More Than RT Pipeline Character Animation Ray Tracing General Purpose Programming Game Physics 28

Future Of GPUs Super Computers On The Desktop GPUs = Order Of Magnitude Than CPUs Mobile Computing Realistic Rendering On Phones Mobile Applications: Automotive Computing Wearable Computers Cameras, Phones, E paper, Bots,... 29

Questions? 30

References OpenGL Shading Language (Orange Book), Randi Rost, 2004 Intro GLSL, Talk Slides Randi Rost 3DLabs, 2005 Intro GLSL, Teaching Slide, Mike Bailey (my ugrad graphics teacher) U of O, 2006 Intro GLSL, Teaching Slides, Keith O connor, GV2 (U of Dublin) OpenGL Shading Language, Teaching Slides, Jerry Talton, Stanford, 2006 Real time Shading, John Hart, 2002, AK Peters OpenGL 2.0 Specification, OpenGL ARB, 2004, OpenGL.org OpenGL Shading Language Specification, 2004, OpenGL.org The Cg Tutorial: The Definitive Guide to Programmable Real Time Graphics, Randima Fernando, Mark Kilgard, 2003 31

Shader Vertex Processing All value are inputs to Shaders Attribute0 Uniform Texture Position Attribute1 ClipVertex Attribute2 PointSize Attributen Vertex Shader Varying0 Varying1 Varying2 Temporaries EdgeFlag Varyingn EdgeFlag 32

Shader Fragment Processing Varying0 Uniform Texture Varying1 Varying2 Varyingn Fragmen t Shader FragDepth FragCoord FrontFacing FragColor FragDepth Temporaries FragCoord FrontFacing Same as vertex, all values are input into shader 33