GPU Shadingolano/class/435-06-8/GPUs... · 2015-08-25 · So what is real-time shading? More...
Transcript of GPU Shadingolano/class/435-06-8/GPUs... · 2015-08-25 · So what is real-time shading? More...
![Page 1: GPU Shadingolano/class/435-06-8/GPUs... · 2015-08-25 · So what is real-time shading? More realistic appearance Bumps, anisotropic surfaces, PRT, … Non-realistic appearance Cartoon,](https://reader034.fdocuments.us/reader034/viewer/2022042417/5f32baef668bfd197d233494/html5/thumbnails/1.jpg)
GPU Shading
CMSC 435/634
![Page 2: GPU Shadingolano/class/435-06-8/GPUs... · 2015-08-25 · So what is real-time shading? More realistic appearance Bumps, anisotropic surfaces, PRT, … Non-realistic appearance Cartoon,](https://reader034.fdocuments.us/reader034/viewer/2022042417/5f32baef668bfd197d233494/html5/thumbnails/2.jpg)
So what is real-time shading?
More realistic appearanceBumps, anisotropic surfaces, PRT, …
Non-realistic appearanceCartoon, sketch, illustration, …
Animated appearanceCharacter skin, water, clouds
VisualizationData on surfaces, Volume rendering, …
General computation
![Page 3: GPU Shadingolano/class/435-06-8/GPUs... · 2015-08-25 · So what is real-time shading? More realistic appearance Bumps, anisotropic surfaces, PRT, … Non-realistic appearance Cartoon,](https://reader034.fdocuments.us/reader034/viewer/2022042417/5f32baef668bfd197d233494/html5/thumbnails/3.jpg)
Put another way…
![Page 4: GPU Shadingolano/class/435-06-8/GPUs... · 2015-08-25 · So what is real-time shading? More realistic appearance Bumps, anisotropic surfaces, PRT, … Non-realistic appearance Cartoon,](https://reader034.fdocuments.us/reader034/viewer/2022042417/5f32baef668bfd197d233494/html5/thumbnails/4.jpg)
Non-real time vs. Real time
Not real-timeGeneral CPU
Seconds to hoursper frame
Thousands of lines
“Unlimited”computation, texture,memory, …
Real-timeGraphics hardware
Tens of frames persecond
Thousands ofinstructionsLimited computation,texture, memory, …
![Page 5: GPU Shadingolano/class/435-06-8/GPUs... · 2015-08-25 · So what is real-time shading? More realistic appearance Bumps, anisotropic surfaces, PRT, … Non-realistic appearance Cartoon,](https://reader034.fdocuments.us/reader034/viewer/2022042417/5f32baef668bfd197d233494/html5/thumbnails/5.jpg)
Non-real time vs. Real-time
Non-real time Real-time
Texture/Buffer
Vertex
Geometry
Fragment
Application
DisplayedPixels
Light
DisplayedPixels
Application
Displacement
Surface
Volume
Atmosphere
Imager
![Page 6: GPU Shadingolano/class/435-06-8/GPUs... · 2015-08-25 · So what is real-time shading? More realistic appearance Bumps, anisotropic surfaces, PRT, … Non-realistic appearance Cartoon,](https://reader034.fdocuments.us/reader034/viewer/2022042417/5f32baef668bfd197d233494/html5/thumbnails/6.jpg)
History (not real-time)
Testbed (1981)Shade Trees (1984)Image Synthesizer (1985)RenderMan (1990)
![Page 7: GPU Shadingolano/class/435-06-8/GPUs... · 2015-08-25 · So what is real-time shading? More realistic appearance Bumps, anisotropic surfaces, PRT, … Non-realistic appearance Cartoon,](https://reader034.fdocuments.us/reader034/viewer/2022042417/5f32baef668bfd197d233494/html5/thumbnails/7.jpg)
History (real-time)
Custom HW (1998)
Multi-pass standard HW (2000)
Register combiners (2000)
Vertex programs (2001)
Compiling to mixed HW (2001)
Fragment programs (2002)
Standardized languages (2003-2004)
Geometry shaders (2006)
![Page 8: GPU Shadingolano/class/435-06-8/GPUs... · 2015-08-25 · So what is real-time shading? More realistic appearance Bumps, anisotropic surfaces, PRT, … Non-realistic appearance Cartoon,](https://reader034.fdocuments.us/reader034/viewer/2022042417/5f32baef668bfd197d233494/html5/thumbnails/8.jpg)
Choices
OS: Windows, Mac, LinuxGPU: ATI, NVIDIAAPI: DirectX, OpenGLLanguage: HLSL, GLSL, CgCompiler: DirectX, OpenGL, Cg, ASHLIRuntime: CgFX, ASHLI, OSG (&others), sample code
![Page 9: GPU Shadingolano/class/435-06-8/GPUs... · 2015-08-25 · So what is real-time shading? More realistic appearance Bumps, anisotropic surfaces, PRT, … Non-realistic appearance Cartoon,](https://reader034.fdocuments.us/reader034/viewer/2022042417/5f32baef668bfd197d233494/html5/thumbnails/9.jpg)
Major Commonalities
Vertex & Fragment/PixelC-like, if/while/forStructs & arraysFloat + small vector and matrix
Swizzle & mask (a.xyz = b.xxw)
Common math & shading functions
![Page 10: GPU Shadingolano/class/435-06-8/GPUs... · 2015-08-25 · So what is real-time shading? More realistic appearance Bumps, anisotropic surfaces, PRT, … Non-realistic appearance Cartoon,](https://reader034.fdocuments.us/reader034/viewer/2022042417/5f32baef668bfd197d233494/html5/thumbnails/10.jpg)
Procedure I/O
VertexIn: [position, normal, matrices, texturecoordinates, …]
Out: position, [arbitrary others]
FragmentIn: position, [arbitrary others]
Out: color, [depth, data]
![Page 11: GPU Shadingolano/class/435-06-8/GPUs... · 2015-08-25 · So what is real-time shading? More realistic appearance Bumps, anisotropic surfaces, PRT, … Non-realistic appearance Cartoon,](https://reader034.fdocuments.us/reader034/viewer/2022042417/5f32baef668bfd197d233494/html5/thumbnails/11.jpg)
Major Differences
Profiles vs. required feature set“Virtualization”Generate low-level vs. directcompilation
![Page 12: GPU Shadingolano/class/435-06-8/GPUs... · 2015-08-25 · So what is real-time shading? More realistic appearance Bumps, anisotropic surfaces, PRT, … Non-realistic appearance Cartoon,](https://reader034.fdocuments.us/reader034/viewer/2022042417/5f32baef668bfd197d233494/html5/thumbnails/12.jpg)
Notable Minor Differences
:NORMAL vs. predefined & attributehalf, fixedfloat3 vs. vec3mul(matrix, matrix) vs. matrix*matrix
![Page 13: GPU Shadingolano/class/435-06-8/GPUs... · 2015-08-25 · So what is real-time shading? More realistic appearance Bumps, anisotropic surfaces, PRT, … Non-realistic appearance Cartoon,](https://reader034.fdocuments.us/reader034/viewer/2022042417/5f32baef668bfd197d233494/html5/thumbnails/13.jpg)
Some OpenGL Code
OpenGLGLSL / vertex & fragment programLow-level / vertex & fragment shaderC interface
![Page 14: GPU Shadingolano/class/435-06-8/GPUs... · 2015-08-25 · So what is real-time shading? More realistic appearance Bumps, anisotropic surfaces, PRT, … Non-realistic appearance Cartoon,](https://reader034.fdocuments.us/reader034/viewer/2022042417/5f32baef668bfd197d233494/html5/thumbnails/14.jpg)
Blend Positions
![Page 15: GPU Shadingolano/class/435-06-8/GPUs... · 2015-08-25 · So what is real-time shading? More realistic appearance Bumps, anisotropic surfaces, PRT, … Non-realistic appearance Cartoon,](https://reader034.fdocuments.us/reader034/viewer/2022042417/5f32baef668bfd197d233494/html5/thumbnails/15.jpg)
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);}
![Page 16: GPU Shadingolano/class/435-06-8/GPUs... · 2015-08-25 · So what is real-time shading? More realistic appearance Bumps, anisotropic surfaces, PRT, … Non-realistic appearance Cartoon,](https://reader034.fdocuments.us/reader034/viewer/2022042417/5f32baef668bfd197d233494/html5/thumbnails/16.jpg)
Low-level code
!!ARBvp1.0 # screen position from vertexTEMP Vp;DP4 Vp.x, state.matrix.mvp.row[0], vertex.position;DP4 Vp.y, state.matrix.mvp.row[1], vertex.position;DP4 Vp.z, state.matrix.mvp.row[2], vertex.position;DP4 Vp.w, state.matrix.mvp.row[3], vertex.position; # screen position from textureTEMP Tp;MAD Tp, vertex.texcoord,{1.8,1.8,0,0},{-.9,-.9,0,1}; # interpolateMAD Tp, Tp, -vertex.color.x, Tp;MAD result.position, Tp, vertex.color.x, Tp; # copy to outputMOV result.texcoord[0], vertex.texcoord;MOV result.texcoord[1], Vp;MOV result.texcoord[2], vertex.color.x; END
![Page 17: GPU Shadingolano/class/435-06-8/GPUs... · 2015-08-25 · So what is real-time shading? More realistic appearance Bumps, anisotropic surfaces, PRT, … Non-realistic appearance Cartoon,](https://reader034.fdocuments.us/reader034/viewer/2022042417/5f32baef668bfd197d233494/html5/thumbnails/17.jpg)
Using high-level code
Create shader objectS = glCreateShader (GL_VERTEX_SHADER)
Vertex or Fragment (or Geometry)
Load shader into objectglShaderSource(S, n, shaderArray, lenArray)
Array of stringsNULL lenArray or 0 length = \0 terminated
Compile objectglCompileShader(S);
Can check errors
![Page 18: GPU Shadingolano/class/435-06-8/GPUs... · 2015-08-25 · So what is real-time shading? More realistic appearance Bumps, anisotropic surfaces, PRT, … Non-realistic appearance Cartoon,](https://reader034.fdocuments.us/reader034/viewer/2022042417/5f32baef668bfd197d233494/html5/thumbnails/18.jpg)
Using high-level code (2)
Create program objectP = glCreateProgram ()
Attach all shader objectsglAttachObject(P, S)
Vertex, Fragment or both
Link togetherglLinkProgram(P)
UseglUseProgram (P)
![Page 19: GPU Shadingolano/class/435-06-8/GPUs... · 2015-08-25 · So what is real-time shading? More realistic appearance Bumps, anisotropic surfaces, PRT, … Non-realistic appearance Cartoon,](https://reader034.fdocuments.us/reader034/viewer/2022042417/5f32baef668bfd197d233494/html5/thumbnails/19.jpg)
Vertex Lighting
![Page 20: GPU Shadingolano/class/435-06-8/GPUs... · 2015-08-25 · So what is real-time shading? More realistic appearance Bumps, anisotropic surfaces, PRT, … Non-realistic appearance Cartoon,](https://reader034.fdocuments.us/reader034/viewer/2022042417/5f32baef668bfd197d233494/html5/thumbnails/20.jpg)
Vertex Lighting
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) ; …
![Page 21: GPU Shadingolano/class/435-06-8/GPUs... · 2015-08-25 · So what is real-time shading? More realistic appearance Bumps, anisotropic surfaces, PRT, … Non-realistic appearance Cartoon,](https://reader034.fdocuments.us/reader034/viewer/2022042417/5f32baef668bfd197d233494/html5/thumbnails/21.jpg)
Vertex Lighting
… // accumulate contribution from each light gl_FrontColor = gl_FrontMaterial.emission; 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_FrontLightProduct[i].ambient; if (diff > 0.) { gl_FrontColor += gl_FrontLightProduct[i].diffuse * diff; gl_FrontColor += gl_FrontLightProduct[i].specular *
max(pow(dot(N,H),gl_FrontMaterial.shininess),0.);
} } …
![Page 22: GPU Shadingolano/class/435-06-8/GPUs... · 2015-08-25 · So what is real-time shading? More realistic appearance Bumps, anisotropic surfaces, PRT, … Non-realistic appearance Cartoon,](https://reader034.fdocuments.us/reader034/viewer/2022042417/5f32baef668bfd197d233494/html5/thumbnails/22.jpg)
Vertex Lighting
… // standard texture coordinate and position stuff gl_TexCoord[0] = gl_TextureMatrix[0]*gl_MultiTexCoord0; gl_Position = ftransform();}
![Page 23: GPU Shadingolano/class/435-06-8/GPUs... · 2015-08-25 · So what is real-time shading? More realistic appearance Bumps, anisotropic surfaces, PRT, … Non-realistic appearance Cartoon,](https://reader034.fdocuments.us/reader034/viewer/2022042417/5f32baef668bfd197d233494/html5/thumbnails/23.jpg)
Fragment Brick
![Page 24: GPU Shadingolano/class/435-06-8/GPUs... · 2015-08-25 · So what is real-time shading? More realistic appearance Bumps, anisotropic surfaces, PRT, … Non-realistic appearance Cartoon,](https://reader034.fdocuments.us/reader034/viewer/2022042417/5f32baef668bfd197d233494/html5/thumbnails/24.jpg)
widthgap
height
gap
brickmortar
Brick
![Page 25: GPU Shadingolano/class/435-06-8/GPUs... · 2015-08-25 · So what is real-time shading? More realistic appearance Bumps, anisotropic surfaces, PRT, … Non-realistic appearance Cartoon,](https://reader034.fdocuments.us/reader034/viewer/2022042417/5f32baef668bfd197d233494/html5/thumbnails/25.jpg)
Brick Shader
// shader constants, could be passed in to allow modificationfloat width=.25, height = .1, gap = .01;vec4 brick = vec4(1.,0.,0.,1.);vec4 mortar = vec4(.5,.5,.5,1.);
void main() {
/* … compute brick color … */
gl_FragColor *= gl_Color;}
![Page 26: GPU Shadingolano/class/435-06-8/GPUs... · 2015-08-25 · So what is real-time shading? More realistic appearance Bumps, anisotropic surfaces, PRT, … Non-realistic appearance Cartoon,](https://reader034.fdocuments.us/reader034/viewer/2022042417/5f32baef668bfd197d233494/html5/thumbnails/26.jpg)
Brick Color
Where am I in my brick?“brick coordinates”
bs
bt
/* compute bs and bt brick coordinates */
// pick color for this pixel, brick or mortarif (bs < gap || bt < gap) gl_FragColor = mortar;else gl_FragColor = brick;
![Page 27: GPU Shadingolano/class/435-06-8/GPUs... · 2015-08-25 · So what is real-time shading? More realistic appearance Bumps, anisotropic surfaces, PRT, … Non-realistic appearance Cartoon,](https://reader034.fdocuments.us/reader034/viewer/2022042417/5f32baef668bfd197d233494/html5/thumbnails/27.jpg)
Brick Coordinates
// find row and column for this pixelfloat bs = gl_TexCoord[0].x, bt = gl_TexCoord[0].y;
// offset even rows by half a columnif (mod(bt,2.*height)<height) bs += width/2.;
// wrap texture coordinates to get “brick coordinates”bs = mod(bs,width);bt = mod(bt,height);
![Page 28: GPU Shadingolano/class/435-06-8/GPUs... · 2015-08-25 · So what is real-time shading? More realistic appearance Bumps, anisotropic surfaces, PRT, … Non-realistic appearance Cartoon,](https://reader034.fdocuments.us/reader034/viewer/2022042417/5f32baef668bfd197d233494/html5/thumbnails/28.jpg)
Shader Design Strategies
Learn and adapt from RenderManNoise
Layers
Multiple PassesBaked computation
![Page 29: GPU Shadingolano/class/435-06-8/GPUs... · 2015-08-25 · So what is real-time shading? More realistic appearance Bumps, anisotropic surfaces, PRT, … Non-realistic appearance Cartoon,](https://reader034.fdocuments.us/reader034/viewer/2022042417/5f32baef668bfd197d233494/html5/thumbnails/29.jpg)
Noise
Controlled, repeatable randomnessNoise functions generally not implemented
Can use texture or compute
![Page 30: GPU Shadingolano/class/435-06-8/GPUs... · 2015-08-25 · So what is real-time shading? More realistic appearance Bumps, anisotropic surfaces, PRT, … Non-realistic appearance Cartoon,](https://reader034.fdocuments.us/reader034/viewer/2022042417/5f32baef668bfd197d233494/html5/thumbnails/30.jpg)
Layers
IncrementalEasier to write
Easier to visually debug
See Steve May s RManNoteshttp://accad.osu.edu/~smay/RManNotes/
![Page 31: GPU Shadingolano/class/435-06-8/GPUs... · 2015-08-25 · So what is real-time shading? More realistic appearance Bumps, anisotropic surfaces, PRT, … Non-realistic appearance Cartoon,](https://reader034.fdocuments.us/reader034/viewer/2022042417/5f32baef668bfd197d233494/html5/thumbnails/31.jpg)
Multiple Passes
UsesNon-localcommunicationExceed resourceconstraints
MethodsProjectionGeometry Images
Texture Atlas
Chapter 14: Real-Time Shading Course Notes
SIGGRAPH 2003
![Page 32: GPU Shadingolano/class/435-06-8/GPUs... · 2015-08-25 · So what is real-time shading? More realistic appearance Bumps, anisotropic surfaces, PRT, … Non-realistic appearance Cartoon,](https://reader034.fdocuments.us/reader034/viewer/2022042417/5f32baef668bfd197d233494/html5/thumbnails/32.jpg)
Baked Computation
Texture = arbitrary vector-valuedfunction of 1-3 variablesOften cheaper to precompute & look up
Noise textures
Precomputed radiance transfer
BRDF factorizations
…
![Page 33: GPU Shadingolano/class/435-06-8/GPUs... · 2015-08-25 · So what is real-time shading? More realistic appearance Bumps, anisotropic surfaces, PRT, … Non-realistic appearance Cartoon,](https://reader034.fdocuments.us/reader034/viewer/2022042417/5f32baef668bfd197d233494/html5/thumbnails/33.jpg)
Precomputation Tricks
Fix some degrees of freedomE.g. Isotropic BRDFs only
Factor into several functionsProject input to another space
Tangent spaceWorld space
Project output to another spaceSpherical harmonicsWavelets
![Page 34: GPU Shadingolano/class/435-06-8/GPUs... · 2015-08-25 · So what is real-time shading? More realistic appearance Bumps, anisotropic surfaces, PRT, … Non-realistic appearance Cartoon,](https://reader034.fdocuments.us/reader034/viewer/2022042417/5f32baef668bfd197d233494/html5/thumbnails/34.jpg)
Advanced Uses
VisualizationApproximations to global illuminationSurfaces with volume shellsPoint-based renderingGeometry shading