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

Similar documents
Introduction to Programmable GPUs CPSC 314. Real Time Graphics

Modern Hardware II, Curves. Week 12, Wed Apr 7

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

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

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

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

Lighting and Shading II. Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 2015

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

CS 418: Interactive Computer Graphics. Basic Shading in WebGL. Eric Shaffer

Shaders. Slide credit to Prof. Zwicker

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

Programmable Graphics Hardware

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

CS452/552; EE465/505. Lighting & Shading

GPU Programmable Graphics Pipeline

Pipeline Operations. CS 4620 Lecture 14

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

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

Introduction to Computer Graphics with WebGL

Spring 2011 Prof. Hyesoon Kim

Chromatic Aberration. CEDEC 2001 Tokyo, Japan

Graphics Hardware. Instructor Stephen J. Guy

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

CS GPU and GPGPU Programming Lecture 2: Introduction; GPU Architecture 1. Markus Hadwiger, KAUST

Graphics Processing Units (GPUs) V1.2 (January 2010) Outline. High-Level Pipeline. 1. GPU Pipeline:

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

Basics of GPU-Based Programming

Spring 2009 Prof. Hyesoon Kim

Pat Hanrahan. Modern Graphics Pipeline. How Powerful are GPUs? Application. Command. Geometry. Rasterization. Fragment. Display.

CS770/870 Spring 2017 Open GL Shader Language GLSL

CS770/870 Spring 2017 Open GL Shader Language GLSL

Programmable Graphics Hardware

Lecture 17: Shading in OpenGL. CITS3003 Graphics & Animation

12.2 Programmable Graphics Hardware

Lecture 2. Shaders, GLSL and GPGPU

Objectives. Introduce the OpenGL shading Methods 1) Light and material functions on MV.js 2) per vertex vs per fragment shading 3) Where to carry out

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

Efficient and Scalable Shading for Many Lights

CS451Real-time Rendering Pipeline

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

CS 432 Interactive Computer Graphics

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

Objectives Shading in OpenGL. Front and Back Faces. OpenGL shading. Introduce the OpenGL shading methods. Discuss polygonal shading

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

Shaders (some slides taken from David M. course)

CS 4600 Fall Utah School of Computing

CHAPTER 1 Graphics Systems and Models 3

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

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

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

Sung-Eui Yoon ( 윤성의 )

Evolution of GPUs Chris Seitz

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

CS427 Multicore Architecture and Parallel Computing

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

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

Mattan Erez. The University of Texas at Austin

CS 381 Computer Graphics, Fall 2008 Midterm Exam Solutions. The Midterm Exam was given in class on Thursday, October 23, 2008.

Introduction to Shaders.

Rasterization Overview

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

Graphics Processing Units (GPUs) V2.2 (January 2013) Jan Kautz, Anthony Steed, Tim Weyrich

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

CG Programming: 3D Texturing

Supplement to Lecture 22

lecture 18 - ray tracing - environment mapping - refraction

Real-Time Rendering (Echtzeitgraphik) Michael Wimmer

COMP371 COMPUTER GRAPHICS

Vertex and Pixel Shaders:

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

OpenGL Programmable Shaders

Programming shaders & GPUs Christian Miller CS Fall 2011

Interactive Cloth Simulation. Matthias Wloka NVIDIA Corporation

GpuPy: Accelerating NumPy With a GPU

OPENGL RENDERING PIPELINE

Programming Graphics Hardware

CS 354R: Computer Game Technology

Scanline Rendering 2 1/42

2.11 Particle Systems

CS230 : Computer Graphics Lecture 4. Tamar Shinar Computer Science & Engineering UC Riverside

In-Game Special Effects and Lighting

Computer graphics 2: Graduate seminar in computational aesthetics

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

CS 130 Final. Fall 2015

Deferred Rendering Due: Wednesday November 15 at 10pm

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

CSE4030 Introduction to Computer Graphics

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

The Rasterization Pipeline

Chapter 7 - Light, Materials, Appearance

Cg 2.0. Mark Kilgard

CS 428: Fall Introduction to. OpenGL primer. Andrew Nealen, Rutgers, /13/2010 1

CS 4620 Program 3: Pipeline

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

Zeyang Li Carnegie Mellon University

Hardware Accelerated Volume Visualization. Leonid I. Dimitrov & Milos Sramek GMI Austrian Academy of Sciences

Recall: Indexing into Cube Map

A Real-Time Procedural Shading System for Programmable Graphics Hardware

- Rasterization. Geometry. Scan Conversion. Rasterization

Transcription:

Introduction to Programmable GPUs CPSC 314

News Homework: no new homework this week (focus on quiz prep) Quiz 2 this Friday topics: everything after transformations up until last Friday's lecture questions on rendering pipeline as a whole Office hours (Wolfgang) Thursday, Friday 11:30-12:30

Real Time Graphics Virtua Fighter 1995 Dead or Alive 3 2001 NV1 Xbox (NV2A) (SEGA Corporation) Nalu 2004 (NVIDIA Corporation) GeForce 6 (Tecmo Corporation) Human Head 2006 (NVIDIA Corporation) GeForce 7 Introduction to GPU Programming CS314 Dawn 2003 (NVIDIA Corporation) GeForce FX (NV30) Medusa 2008 (NVIDIA Corporation) GeForce GTX 200 Gordon Wetzstein, 09/03/09

GPUs vs CPUs 800 GFLOPS vs 80 GFLOPS 86.4 GB/s vs 8.4 GB/s [courtesy NVIDIA]

GPUs vs CPUs 800 GFLOPS vs 80 GFLOPS 86.4 GB/s vs 8.4 GB/s [courtesy NVIDIA]

Programmable Pipeline so far: have discussed rendering pipeline as specific set of stages with fixed functionality Geometry Database Model/View Transform. Lighting Perspective Transform. Clipping Scan Conversion Texturing Depth Test Blending Frame- buffer

Programmable Pipeline now: programmable rendering pipeline! vertex shader Geometry Database Model/View Transform. Lighting Perspective Transform. Clipping Scan Conversion Texturing Depth Test Blending Frame- buffer fragment shader

Vertex Shader performs all per-vertex computation (transform & lighting): model and view transform perspective transform texture coordinate transform per-vertex lighting

Vertex Shader input: vertex position and normal (sometimes tangent) (multi-)texture coordinate(s) modelview, projection, and texture matrix vertex material or color light sources color, position, direction etc. output: 2D vertex position transformed texture coordinates vertex color

Vertex Shader - Applications deformable surfaces: skinning different parts have different rigid transformations vertex positions are blended used in facial animations many transformations! upper arm: weight for M1=1 weight for M2=0 lower arm: weight for M1=0 weight for M2=1 transition zone: weight for M1 between 0..1 weight for M2 between 0..1 [courtesy NVIDIA]

Fragment Shader performs all per-fragment computation: texture mapping fog input (interpolated over primitives by rasterizer): texture coordinates color output: fragment color

Fragment Shader - Applications Not really shaders, but very similar to NPR! A Scanner Darkly, Warner Independent Pictures OpenVIDIA Image Processing Introduction to GPU Programming CS314 GPU raytracing, NVIDIA Volume Ray Casting, Peter Trier Gordon Wetzstein, 09/03/09

Vertex & Fragment Shader massively parallel computing by parallelization same shader is applied to all data (vertices or fragments) SIMD (single instruction multiple data) parallel programming issues: main advantage: high performance main disadvantage: no access to neighboring vertices/fragments

Vertex Shader - Instructions Arithmetic Operations on 4-vectors: ADD, MUL, MAD, MIN, MAX, DP3, DP4 Operations on Scalars RCP (1/x), RSQ (1/ x), EXP, LOG Specialty Instructions DST (distance: computes length of vector) LIT (quadratic falloff term for lighting) Later generation: Loops and conditional jumps

Vertex Shader - Example morph between cube and sphere & lighting vertex attributes: v[0..n], matrices c[1..n], registers R #blend normal and position # v= αv 1 +(1-α)v 2 = α(v 1 -v 2 )+ v 2 MOV R3, v[3] ; MOV R5, v[2] ; ADD R8, v[1], -R3 ; ADD R6, v[0], -R5 ; MAD R8, v[15].x, R8, R3 MAD R6, v[15].x, R6, R5 ; # transform normal to eye space DP3 R9.x, R8, c[12] ; DP3 R9.y, R8, c[13] ; DP3 R9.z, R8, c[14] ; # normalize normal DP3 R9.w, R9, R9 ; RSQ R9.w, R9.w ; MUL R9, R9.w, R9 ; # apply lighting and output color DP3 R0.x, R9, c[20] ; DP3 R0.y, R9, c[22] ; MOV R0.zw, c[21] ; LIT R1, R0 ; DP3 o[col0], c[21], R1 ; # transform position and output DP4 o[hpos].x, R6, c[4] ; DP4 o[hpos].y, R6, c[5] ; DP4 o[hpos].z, R6, c[6] ; DP4 o[hpos].w, R6, c[7] ;

Shading languages Cg (C for Graphics NVIDIA) GLSL (GL Shading Language OpenGL) HLSL (High Level Shading Language MS Direct3D) Introduction to GPU Programming CS314 Gordon Wetzstein, 09/03/09

Cg History [courtesy NVIDIA]

Cg How does it work? [courtesy NVIDIA]

Cg Integration into OpenGL void initshader(void) { // get fragment shader profile _cgfragmentprofile = \ cgglgetlatestprofile(cg_gl_fragment); // init Cg context _cgcontext = cgcreatecontext(); void displayloop(void) { // setup transformation // enable shader and set parameters cgglenableprofile( _cgfragmentprofile ); cgglbindprogram( _cgprogram ); } // load shader from file _cgprogram = \ cgcreateprogramfromfile( _cgcontext, CG_SOURCE, MyShader.cg", _cgfragmentprofile, NULL, NULL); // upload shader on GPU cgglloadprogram( _cgprogram ); // get handles to shader parameters _cgtexture = \ cggetnamedparameter(_cgprogram, "texture"); _cgparameter = \ cggetnamedparameter(_cgprogram, parameter"); } // set Cg texture cgglsettextureparameter(_cgtexture, _textureid); cgglenabletextureparameter(_cgtexture); // set gamma cgglsetparameter1f(_cgparameter, _parameter); // draw geometry // disable Cg texture and profile cggldisabletextureparameter(_cgtexture); cggldisableprofile( _cgfragmentprofile ); // swap buffers

Cg Example Fragment Shader Fragment Shader: gamma mapping DEMO void main( float4 texcoord : TEXCOORD, uniform samplerrect texture, uniform float gamma, out float4 color : COLOR ) { // perform texture look up float3 texturecolor = f4texrect( texture, texcoord.xy ).rgb; } // set output color color.rgb = pow( texturecolor, gamma );

Cg Example Vertex Shader Vertex Shader: animated teapot void main( // input float4 position : POSITION, // position in object coordinates float3 normal : NORMAL, // normal DEMO // user parameters uniform float4x4 objectmatrix, // object coordinate system matrix uniform float4x4 objectmatrixit, // object coordinate system matrix inverse transpose uniform float4x4 modelviewmatrix, // modelview matrix uniform float4x4 modelviewmatrixit, // modelview matrix inverse transpose uniform float4x4 projectionmatrix, // projection matrix uniform float deformation, // deformation parameter uniform float3 lightposition, // light position uniform float3 lightambient, // light ambient parameter uniform float3 lightdiffuse, // light diffuse parameter uniform float3 lightspecular, // light specular parameter uniform float3 lightattenuation, // light attenuation parameter - constant, linear, quadratic uniform float3 materialemission, // material emission parameter uniform float3 materialambient, // material ambient parameter uniform float3 materialdiffuse, // material diffuse parameter uniform float3 materialspecular, // material specular parameter uniform float materialshininess, // material shininess parameter { // output out float4 outposition : POSITION, // position in clip space out float4 outcolor : COLOR ) // out color

Cg Example Vertex Shader // transform position from object space to clip space float4 positionobject = mul(objectmatrix, position); // transform normal into world space float4 normalobject = mul(objectmatrixit, float4(normal,1)); float4 normalworld = mul(modelviewmatrixit, normalobject); // world position of light float4 lightpositionworld = \ mul(modelviewmatrix, float4(lightposition,1)); // assume viewer position is in origin float4 viewerpositionworld = float4(0.0, 0.0, 0.0, 1.0); // apply deformation positionobject.xyz = positionobject.xyz + \ deformation * normalize(normalobject.xyz); float4 positionworld = mul(modelviewmatrix, positionobject); outposition = mul(projectionmatrix, positionworld); // two vectors float3 P = positionworld.xyz; float3 N = normalize(normalworld.xyz); // compute the ambient term float3 ambient = materialambient*lightambient; // compute the diffuse term float3 L = normalize(lightpositionworld.xyz - P); float diffusefactor = max(dot(n, L), 0); float3 diffuse = materialdiffuse * lightdiffuse * diffusefactor; // compute the specular term float3 V = normalize( viewerpositionworld.xyz - \ positionworld.xyz); float3 H = normalize(l + V); float specularfactor = \ pow(max(dot(n, H), 0), materialshininess); if (diffusefactor <= 0) specularfactor = 0; float3 specular = \ materialspecular * \ lightspecular * \ specularfactor; // attenuation factor float distancelightvertex = \ length(p-lightpositionworld.xyz); float attenuationfactor = \ 1 / ( lightattenuation.x + \ distancelightvertex*lightattenuation.y + \ distancelightvertex*distancelightvertex*\ lightattenuation.z ); // set output color outcolor.rgb = materialemission + \ ambient + \ attenuationfactor * \ ( diffuse + specular ); outcolor.w = 1; }

Cg Example Phong Shading void main( float4 position : POSITION, // position in object coordinates float3 normal : NORMAL, // normal // user parameters vertex shader DEMO { } // output out float4 outtexcoord0 : TEXCOORD0, // world normal out float4 outtexcoord1 : TEXCOORD1, // world position out float4 outtexcoord2 : TEXCOORD2, // world light position out float4 outposition : POSITION) // position in clip space // transform position from object space to clip space // transform normal into world space // set world normal as out texture coordinate0 outtexcoord0 = normalworld; // set world position as out texture coordinate1 outtexcoord1 = positionworld; // world position of light outtexcoord2 = mul(modelviewmatrix, float4(lightposition,1));

Cg Example Phong Shading fragment shader void main( float4 normal : TEXCOORD0, // normal float4 position : TEXCOORD1, // position float4 lightposition : TEXCOORD2, // light position out float4 outcolor : COLOR ) { // compute the ambient term DEMO // compute the diffuse term // compute the specular term // attenuation factor } // set output color outcolor.rgb = materialemission + ambient + attenuationfactor * (diffuse + specular);

GPGPU general purpose computation on the GPU in the past: access via shading languages and rendering pipeline now: access via cuda interface in C environment DEMO

GPGPU Applications [courtesy NVIDIA]