Introduction to Programmable GPUs CPSC 314 Introduction to

Introduction to Programmable GPUs CPSC 314 Introduction to

Introduction to Programmable GPUs CPSC 314 Introduction to GPU Programming | CS314 Gordon Wetzstein, 09/03/09 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:3012:30 Introduction to GPU Programming | CS314 Gordon Wetzstein, 09/03/09 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 [courtesy NVIDIA] 800 GFLOPS vs 80 GFLOPS 86.4 GB/s vs 8.4 GB/s Introduction to GPU Programming | CS314 Gordon Wetzstein, 09/03/09 GPUs vs CPUs [courtesy NVIDIA] 800 GFLOPS vs 80 GFLOPS 86.4 GB/s vs 8.4 GB/s Introduction to GPU Programming | CS314 Gordon Wetzstein, 09/03/09

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

Test Introduction to GPU Programming | CS314 Perspective Perspective Transform. Clipping Blending Blending Framebuffer Gordon Wetzstein, 09/03/09 Programmable Pipeline now: programmable rendering pipeline! vertex shader Geometry Geometry Database Database Scan Scan

Conversion Model/View Model/View Transform. Transform. Lighting Lighting Texturing Texturing Depth Depth Test Test Perspective Perspective Transform. Clipping Blending Blending Framebuffer

fragment shader Introduction to GPU Programming | CS314 Gordon Wetzstein, 09/03/09 Vertex Shader performs all per-vertex computation (transform & lighting): model and view transform perspective transform texture coordinate transform per-vertex lighting Introduction to GPU Programming | CS314 Gordon Wetzstein, 09/03/09 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 Introduction to GPU Programming | CS314 Gordon Wetzstein, 09/03/09 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 transition zone: weight for M1 between 0..1 weight for M2 between 0..1 Introduction to GPU Programming | CS314 lower arm: weight for M1=0 weight for M2=1 [courtesy NVIDIA] Gordon Wetzstein, 09/03/09 Fragment Shader performs all per-fragment computation: texture mapping fog input (interpolated over primitives by rasterizer): texture coordinates color output: fragment color

Introduction to GPU Programming | CS314 Gordon Wetzstein, 09/03/09 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

Introduction to GPU Programming | CS314 Gordon Wetzstein, 09/03/09 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 Introduction to GPU Programming | CS314 Gordon Wetzstein, 09/03/09 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= v1+(1-)v2 = (v1-v2)+ v2

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 DP4 DP4 DP4

o[HPOS].x, o[HPOS].y, o[HPOS].z, o[HPOS].w, R6, R6, R6, R6, c[4] c[5] c[6] c[7] ; ; ; ; Introduction to GPU Programming | CS314 Gordon Wetzstein, 09/03/09 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 [courtesy NVIDIA] Cg History Introduction to GPU Programming | CS314 Gordon Wetzstein, 09/03/09 [courtesy NVIDIA] Cg How does it work? Introduction to GPU Programming | CS314 Gordon Wetzstein, 09/03/09 Cg Integration into OpenGL void initShader(void) { void displayLoop(void) {

// setup transformation // get fragment shader profile _cgFragmentProfile = \ cgGLGetLatestProfile(CG_GL_FRAGMENT); // enable shader and set parameters cgGLEnableProfile( _cgFragmentProfile ); cgGLBindProgram( _cgProgram ); // init Cg context _cgContext = cgCreateContext(); // set Cg texture cgGLSetTextureParameter(_cgTexture, _textureID); cgGLEnableTextureParameter(_cgTexture); // load shader from file _cgProgram = \ cgCreateProgramFromFile( _cgContext, CG_SOURCE, MyShader.cg", _cgFragmentProfile, NULL, NULL); // set gamma cgGLSetParameter1f(_cgParameter, _parameter);

// draw geometry // upload shader on GPU cgGLLoadProgram( _cgProgram ); // disable Cg texture and profile cgGLDisableTextureParameter(_cgTexture); cgGLDisableProfile( _cgFragmentProfile ); // get handles to shader parameters _cgTexture = \ cgGetNamedParameter(_cgProgram, "texture"); _cgParameter = \ cgGetNamedParameter(_cgProgram, parameter"); } Introduction to GPU Programming | CS314 // swap buffers } Gordon Wetzstein, 09/03/09 Fragment Shader: gamma mapping D

EM O Cg Example Fragment Shader 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 ); } Introduction to GPU Programming | CS314 Gordon Wetzstein, 09/03/09 Vertex Shader: animated teapot void main( // input float4 position float3 normal

: POSITION, // position in object coordinates : NORMAL, // normal // user parameters uniform float4x4 objectMatrix, uniform float4x4 objectMatrixIT, uniform float4x4 modelViewMatrix, uniform float4x4 modelViewMatrixIT, uniform float4x4 projectionMatrix, uniform float deformation, uniform float3 lightPosition, uniform float3 lightAmbient, uniform float3 lightDiffuse, uniform float3 lightSpecular, uniform float3 lightAttenuation, uniform float3 materialEmission, uniform float3 materialAmbient, uniform float3 materialDiffuse, uniform float3 materialSpecular, uniform float materialShininess, // output out float4 outPosition out float4 outColor D EM O

Cg Example Vertex Shader // object coordinate system matrix // object coordinate system matrix inverse transpose // modelview matrix // modelview matrix inverse transpose // projection matrix // deformation parameter // light position // light ambient parameter // light diffuse parameter // light specular parameter // light attenuation parameter - constant, linear, quadratic // material emission parameter // material ambient parameter // material diffuse parameter // material specular parameter // material shininess parameter : POSITION, // position in clip space : COLOR ) // out color { Introduction to GPU Programming | CS314 Gordon Wetzstein, 09/03/09

Cg Example Vertex Shader // transform position from object space to clip space float4 positionObject = mul(objectMatrix, position); // 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; // 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); // attenuation factor float distanceLightVertex = \

length(P-lightPositionWorld.xyz); float attenuationFactor = \ 1 / ( lightAttenuation.x + \ distanceLightVertex*lightAttenuation.y + \ distanceLightVertex*distanceLightVertex*\ lightAttenuation.z ); // 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); // set output color outColor.rgb = materialEmission + \ ambient + \ attenuationFactor * \ ( diffuse + specular ); outColor.w = 1; // 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; Introduction to GPU Programming | CS314 } Gordon Wetzstein, 09/03/09 vertex shader void main( float4 position float3 normal : POSITION, // position in object coordinates : NORMAL, // normal // user parameters // output out float4 outTexCoord0 out float4 outTexCoord1 out float4 outTexCoord2 out float4 outPosition : TEXCOORD0, : TEXCOORD1, : TEXCOORD2,

: POSITION) D EM O Cg Example Phong Shading // world normal // world position // world light 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)); } Introduction to GPU Programming | CS314

Gordon Wetzstein, 09/03/09 fragment shader void main( float4 normal : TEXCOORD0, float4 position : TEXCOORD1, float4 lightPosition : TEXCOORD2, out float4 outColor : COLOR ) { // compute the ambient term // normal // position // light position D EM O Cg Example Phong Shading

// compute the diffuse term // compute the specular term // attenuation factor // set output color outColor.rgb = materialEmission + ambient + attenuationFactor * (diffuse + specular); } Introduction to GPU Programming | CS314 Gordon Wetzstein, 09/03/09 GPGPU general purpose computation on the GPU in the past: access via shading languages and rendering pipeline D EM O now: access via cuda interface in C environment Introduction to GPU Programming | CS314 Gordon Wetzstein, 09/03/09

GPGPU Applications [courtesy NVIDIA] Introduction to GPU Programming | CS314 Gordon Wetzstein, 09/03/09

Recently Viewed Presentations

  • Models of Psychological Function &amp; Dysfunction (II)

    Models of Psychological Function & Dysfunction (II)

    Cognitive appraisal is a mediator between the event and the response. Cognitive Appraisal. Appraisal of the situation requires mental activity involving judgment, discrimination, and choice of activity, based largely on past experience. Process of evaluating whether a particular encounter with...
  • Folie 1 - euwir.jura.uni-koeln.de

    Folie 1 - euwir.jura.uni-koeln.de

    Richter am OLG Nationales Kartellrecht § 21 GWB §§ 19 I, II, 20 I GWB §§ 36 I, 40 GWB Verbot von Kartellabspra-chen Boykottaufruf Diskriminierungs- und Behinderungsverbot etc. Kartellbehördliche Kontrolle von Unter-nehmensfusionen Unternehmen Unternehmen marktbeherrschende Unternehmen Unternehmen + § 35...
  • The Body of a News Story - College of Charleston

    The Body of a News Story - College of Charleston

    The Body of a News Story Chapter 9 Storytelling Styles Organizing the Story (Four Styles) Inverted pyramid—summary lead then information in descending order of importance (dates back to the American Civil War) Hour glass style—starts with inverted pyramid, then has...
  • Towards an Electronic Kilogram - The Budker Group

    Towards an Electronic Kilogram - The Budker Group

    Towards an Electronic Kilogram Physics 250 Victor Acosta NOTE: Almost everything in this report was taken from NIST websites. The Old Standard: "Le Gran K" Held In Sevres, France Only 1/7 SI units using physical artifact Δm/m = 30 ppb...
  • HOPE/ ZELL Miller Scholarship

    HOPE/ ZELL Miller Scholarship

    BUZZPORT. In the FINANCIAL AID box, select . Requirements and Eligibility. and select 2012-2013 Aid Year. Select "Academic Progress Tab" GSFC Administrative Review of HOPE or Zell Miller Scholarship Program Eligibility. Submit written letter to GSFC for an Administrative Review...
  • How do Geopolitics and Critical Geopolitics Help us ...

    How do Geopolitics and Critical Geopolitics Help us ...

    Nicholas Spykman's Rimland Theory: Spykman thought that the Rimland, the strip of coastal land that encircles Eurasia, is more important than the central Asian zone (the so-called Heartland) for the control of the Eurasian continent.
  • From Exodus to Exile Timeline of Major Events

    From Exodus to Exile Timeline of Major Events

    Moses led the Hebrews across the Red Sea and into the Sinai Region. On Mount Sinai, God appeared to Moses again. This time, God shared with him the Ten Commandments. The Ten Commandments served to emphasize the nature of God's...
  • Classic Civilizations - West Valley Central School

    Classic Civilizations - West Valley Central School

    Classical Era/Period/Age (600 BCE- 900 CE)- Period of time in which complex civilizations expanded to establish large land empires and were more interconnected through trade than earlier civilizations. These civilizations made great contributions to our collective learning as a result...