Programming Graphics Hardware Overview of the Tutorial: Afternoon
Total Page:16
File Type:pdf, Size:1020Kb
Tutorial 5 ProgrammingProgramming GraphicsGraphics HardwareHardware Randy Fernando, Mark Harris, Matthias Wloka, Cyril Zeller Overview of the Tutorial: Morning 8:30 Introduction to the Hardware Graphics Pipeline Cyril Zeller 9:30 Controlling the GPU from the CPU: the 3D API Cyril Zeller 10:15 Break 10:45 Programming the GPU: High-level Shading Languages Randy Fernando 12:00 Lunch Tutorial 5: Programming Graphics Hardware Overview of the Tutorial: Afternoon 12:00 Lunch 14:00 Optimizing the Graphics Pipeline Matthias Wloka 14:45 Advanced Rendering Techniques Matthias Wloka 15:45 Break 16:15 General-Purpose Computation Using Graphics Hardware Mark Harris 17:30 End Tutorial 5: Programming Graphics Hardware Tutorial 5: Programming Graphics Hardware IntroductionIntroduction toto thethe HardwareHardware GraphicsGraphics PipelinePipeline Cyril Zeller Overview Concepts: Real-time rendering Hardware graphics pipeline Evolution of the PC hardware graphics pipeline: 1995-1998: Texture mapping and z-buffer 1998: Multitexturing 1999-2000: Transform and lighting 2001: Programmable vertex shader 2002-2003: Programmable pixel shader 2004: Shader model 3.0 and 64-bit color support PC graphics software architecture Performance numbers Tutorial 5: Programming Graphics Hardware Real-Time Rendering Graphics hardware enables real-time rendering Real-time means display rate at more than 10 images per second 3D Scene = Image = Collection of Array of pixels 3D primitives (triangles, lines, points) Tutorial 5: Programming Graphics Hardware Hardware Graphics Pipeline Application Geometry Rasterization 3D Triangles 2D Triangles Pixels Stage Stage Stage For each For each triangle: triangle vertex: Rasterize Transform triangle 3D position Interpolate into vertex attributes screen position across triangle Compute Shade attributes pixels Resolve visibility Tutorial 5: Programming Graphics Hardware PC Architecture Motherboard Central Processor Unit (CPU) System Memory Bus Port (PCI, AGP, PCIe) Video Memory Graphics Processor Unit (GPU) Video Board Tutorial 5: Programming Graphics Hardware 1995-1998: Texture Mapping and Z-Buffer CPU GPU Application / Geometry Stage Rasterization Stage Raster Texture Rasterizer Operations Unit Unit 2D Triangles Bus Frame 2D Triangles Textures Textures (PCI) Buffer System Memory Video Memory PCI: Peripheral Component Interconnect 3dfx’s Voodoo Tutorial 5: Programming Graphics Hardware Texture Mapping Triangle Meshtextured with Base Texture + = Tutorial 5: Programming Graphics Hardware Texture Mapping: Texture Coordinates Interpolation Screen Space Texture Space Texel x u (x1, y1) y v (u1, v1) (x, y) (x0, y0) (u, v) (x2, y2) (u0, v0) (u2, v2) Tutorial 5: Programming Graphics Hardware Texture Mapping: Perspective-Correct Interpolation Perspective-Incorrect Perspective-Correct Tutorial 5: Programming Graphics Hardware Texture Mapping: Magnification Screen Space Texture Space Pixel’s texture footprint Pixel x u y v or Nearest-Point Sampling Bilinear Filtering Tutorial 5: Programming Graphics Hardware Texture Mapping: Minification Screen Space Texture Space Pixel’s texture footprint Pixel x u y v Tutorial 5: Programming Graphics Hardware Texture Mapping: Mipmapping or Bilinear Filtering Trilinear Filtering Tutorial 5: Programming Graphics Hardware Texture Mapping: Anisotropic Filtering or Bilinear Filtering Trilinear Filtering Tutorial 5: Programming Graphics Hardware Texture Mapping: Addressing Modes Wrap Mirror Tutorial 5: Programming Graphics Hardware Texture Mapping: Addressing Modes Clamp Border Tutorial 5: Programming Graphics Hardware Raster Operations Unit (ROP) Raster Operations stencil buffer value Unit at (x, y) tested against Texture Rasterizer Fragments Scissor Test reference value Unit Fragment Alpha Test tested against Frame Buffer Screen Position (x, y) scissor rectangle Stencil Test Stencil Buffer tested against Alpha Value a reference value Z Test Z-Buffer tested against Depth z z-buffer value at (x, y) Color Buffer (visibility test) Alpha Blending Pixels blended with color buffer value at (x, y): Color (r, g, b) Ksrc * Colorsrc + Kdst * Colorsrc (src = fragment dst = color buffer) Tutorial 5: Programming Graphics Hardware 1998: Multitexturing CPU GPU Application / Geometry Stage Rasterization Stage Raster Multitexture Rasterizer Operations Unit Unit 2D Triangles Bus Frame 2D Triangles Textures Textures (AGP) Buffer System Memory Video Memory AGP: Accelerated Graphics Port NVIDIA’s TNT, ATI’s Rage Tutorial 5: Programming Graphics Hardware AGP PCI uses a parallel connection AGP uses a serial connection → Fewer pins, simpler protocol → Cheaper, more scalable PCI uses a shared-bus protocol AGP uses a point-to-point protocol → Bandwidth is not shared among devices AGP uses a dedicated system memory called AGP memory or non-local video memory The GPU can lookup textures that resides in AGP memory Its size is called the AGP aperture Bandwidth: AGP = 2 x PCI (AGP2x = 2 x AGP, etc.) Tutorial 5: Programming Graphics Hardware Multitexturing Base Texturemodulated by Light Map X from UT2004 (c) Epic Games Inc. = Used with permission Tutorial 5: Programming Graphics Hardware 1999-2000: Transform and Lighting CPU GPU “Fixed Function Pipeline” Application Geometry Stage Rasterization Stage Stage Raster Transform Register Rasterizer Operations and Combiner Unit Lighting Unit Texture Unit 3D Triangles Textures Frame Bus 3D Triangles Textures (AGP) Buffer System Memory Video Memory Register Combiner: Offers many more texture/color combinations NVIDIA’s GeForce 256 and GeForce2, ATI’s Radeon 7500, S3’s Savage3D Tutorial 5: Programming Graphics Hardware Transform and Lighting Unit (TnL) Transform and Lighting Unit Transform World World View Perspective Model Camera Projection Screen Matrix Space Matrix Division or or Projection or or and Object Eye Matrix Clip Window Model-View Viewport Space Space Space Space Matrix Matrix Lighting Material Properties Vertex Light Properties Diffuse and Specular Color Vertex Color Tutorial 5: Programming Graphics Hardware Bump Mapping Bump mapping is about fetching the normal from a texture (called a normal map) instead of using the interpolated normal to compute lighting at a given pixel += Normal Map Diffuse light without bump Diffuse light with bumps Tutorial 5: Programming Graphics Hardware Cube Texture Mapping Cubemap (covering Environment Mapping the six faces z (x (the reflection vector of a cube) ,y,z y ) is used to lookup x the cubemap) Cubemap lookup (with direction (x, y, z)) Tutorial 5: Programming Graphics Hardware Projective Texture Mapping Projected Texture (x, y, z, w) (x/w, y/w) Texture Projection Projective Texture lookup Tutorial 5: Programming Graphics Hardware 2001: Programmable Vertex Shader CPU GPU Application Geometry Stage Rasterization Stage Stage Raster Register Vertex Shader Rasterizer Operations (no flow control) (with Z-Cull) Combiner Unit Texture Shader 3D Triangles Textures Frame Bus 3D Triangles Textures (AGP) Buffer System Memory Video Memory Z-Cull: Predicts which fragments will fail the Z test and discards them Texture Shader: Offers more texture addressing and operations NVIDIA’s GeForce3 and GeForce4 Ti, ATI’s Radeon 8500 Tutorial 5: Programming Graphics Hardware Vertex Shader A programming processor for any per-vertex computation void VertexShader( // Input per vertex in float4 positionInModelSpace, in float2 textureCoordinates, in float3 normal, // Input per batch of triangles uniform float4x4 modelToProjection, uniform float3 lightDirection, // Output per vertex out float4 positionInProjectionSpace, out float2 textureCoordinatesOutput, out float3 color ) { // Vertex transformation positionInProjectionSpace = mul(modelToProjection, positionInModelSpace); // Texture coordinates copy textureCoordinatesOutput = textureCoordinates; // Vertex color computation color = dot(lightDirection, normal); } Tutorial 5: Programming Graphics Hardware Volume Texture Mapping z (x,y,z) y x Volume Texture Noise Perturbation (3D Noise) Volume Texture lookup (with position (x, y, z)) Tutorial 5: Programming Graphics Hardware Hardware Shadow Mapping Shadow Map Computation Store The shadow map contains z/w the depth z/w of the 3D points visible (x, y, z, w) from the light’s point of view Spot (x/w, y/w) light Shadow map Shadow Rendering Lookup value A 3D point (x, y, z, w) is in shadow if: z/w < value of shadow map at (x/w, y/w) (x, y, z, w) Spot A hardware shadow map lookup (x/w, y/w) light returns the value of this comparison between 0 and 1 Tutorial 5: Programming Graphics Hardware Antialiasing: Definition Aliasing: Undesirable visual artifacts due to insufficient sampling of: Primitives (triangles, lines, etc.) → jagged edges Textures or shaders → pixelation, moiré patterns Those artifacts are even more noticeable on animated images Antialiasing: Method to reduce aliasing Texture antialiasing is largely handled by proper mipmapping and anisotropic filtering Shader antialiasing can be tricky (especially with conditionals) Tutorial 5: Programming Graphics Hardware Antialiasing: Supersampling and Multisampling Supersampling: Pixel Center Compute color and Z at higher resolution and Sample display averaged color to smooth out the visual artifacts Multisampling: Same thing except only Z is computed at higher resolution As a result, multisampling performs antialiasing on primitive edges only Tutorial 5: Programming Graphics Hardware 2002-2003: Programmable Pixel Shader CPU GPU Application Geometry Stage Rasterization Stage Stage Raster Vertex Shader Rasterizer Pixel Shader (static