Today Shadow Volume Algorithms Vertex and Pixel Shaders

  • Slides: 29
Download presentation
Today • Shadow Volume Algorithms • Vertex and Pixel Shaders 9/25/2001 CS 638, Fall

Today • Shadow Volume Algorithms • Vertex and Pixel Shaders 9/25/2001 CS 638, Fall 2001

Shadow Volumes • A shadow volume for an object and light is the volume

Shadow Volumes • A shadow volume for an object and light is the volume of space that is shadowed – That is, all points in the volume are in shadow for that light/object pair • Creating the volume: – Find silhouette edges of shadowing object as seen by the light source – Extrude these edges away from the light, forming polygons – Clip the polygons to the view volume 9/25/2001 CS 638, Fall 2001

Shadow Volume 9/25/2001 CS 638, Fall 2001

Shadow Volume 9/25/2001 CS 638, Fall 2001

Practicalities • Silhouette edges can be found by looking at polygon normals – Silhouette

Practicalities • Silhouette edges can be found by looking at polygon normals – Silhouette edges are those between a front facing face and back facing face (from light’s point of view) – The result is a sequence of edges with common vertices • Assuming convex shadowing objects • Extend all the vertices of silhouette edges away from the light source • Clip them to the view volume, and form the polygons that bound the shadow volume • Final result are a set of shadow volume boundary polygons 9/25/2001 CS 638, Fall 2001

Key Observation • All points inside a shadow volume are in shadow • Along

Key Observation • All points inside a shadow volume are in shadow • Along a ray from the eye, we can track the shadow state by looking at intersections with shadow volume boundaries – Assume the eye is not in shadow – Each time the ray crosses a front facing shadow polygon, add one to a counter – Each time the ray crosses a back facing shadow polygon, subtract one from a counter – Places where the counter is zero are lit, others are shadowed • We need to count the number of shadow polygons in front of a point. Which buffer can count for us? 9/25/2001 CS 638, Fall 2001

Using Shadow Volumes +1 -1 0 1 9/25/2001 -1 +1 0 CS 638, Fall

Using Shadow Volumes +1 -1 0 1 9/25/2001 -1 +1 0 CS 638, Fall 2001 1

Real-Time Shadow Volumes • Compute shadow volumes per frame – Not a problem for

Real-Time Shadow Volumes • Compute shadow volumes per frame – Not a problem for only a few moving objects – Use simplified object geometry to speed things up • Four pass algorithm – – Render scene with ambient light only (everything shadowed) Render front facing shadow polygons to increment stencil buffer Render back facing shadow polygons to decrement stencil buffer Render scene again only for non-shadowed (stencil=0) regions • Horrible details follow… 9/25/2001 CS 638, Fall 2001

Details • Turn off any light sources and render scene with ambient light only,

Details • Turn off any light sources and render scene with ambient light only, depth on, color buffer active • Disable the color and depth buffers for writing, but leave the depth test active • Initialize the stencil buffer to 0 or 1 depending on whether the viewer is in shadow or not (ray cast) • Set the stencil test to always pass and the operation to increment if depth test passes • Enable back face culling • Render the shadow volumes - this will increment the stencil for every front facing polygon that is in front of a visible surface • Cont… 9/25/2001 CS 638, Fall 2001

Details • Enable front face culling, disable back face culling • Set the stencil

Details • Enable front face culling, disable back face culling • Set the stencil operation to decrement if the depth test passes (and leave the test as always pass) • Render the shadow volumes - this decrements for all the back facing shadow polygons that are in front of visible objects. The stencil buffer now has positive values for placing in shadow • Set the stencil function to equality with 0, operations to keep • Clear the depth buffer, and enable it and the color buffer for writing • Render the scene with the lights turned on • Voila, we’re done 9/25/2001 CS 638, Fall 2001

Variant • Render fully lit, then add shadows (blend a dark polygon) where the

Variant • Render fully lit, then add shadows (blend a dark polygon) where the stencil buffer is set – http: //www. gamasutra. com/features/19991115/bestimt_freitag_01. htm 9/25/2001 CS 638, Fall 2001

Why Use Shadow Volumes? • They correctly account for shadows of all surfaces on

Why Use Shadow Volumes? • They correctly account for shadows of all surfaces on all other surfaces – No shadow where there shouldn’t be shadow – No problem with multiple light sources • Adaptable quality by creating shadow volumes with approximate geometry • Shadow volumes for static object/light pairs can be precomputed and don’t change • More expensive than light maps, but not too bad on current hardware – Can’t combine the techniques. Why not? 9/25/2001 CS 638, Fall 2001

Other Shadow Algorithms • There are other algorithms suitable for non-real-time situations, or requiring

Other Shadow Algorithms • There are other algorithms suitable for non-real-time situations, or requiring special hardware • Two interesting cases: – Derive shadow polygons from light sources • Compute view from light position • Transform visible polygons into world space • Merge them with existing polygons to indicate non-shadowed polygons – Shadow z-buffer • Compute z-buffer from light viewpoint • Render normal view, compare world locations of points in the z-buffer and shadow z-buffer – points with same location are lit 9/25/2001 CS 638, Fall 2001

Vertex and Pixel Shaders • In traditional graphics, a shader is a program that

Vertex and Pixel Shaders • In traditional graphics, a shader is a program that computes something about each vertex or pixel – Renderman shading language, for instance, computes the color of each pixel with a program • Trends in graphics APIs and hardware bringing shaders into real-time contexts – Direct. X 8. 0 – Nvidia Ge. Force III – Nvidia Open. GL extensions • http: //developer. nvidia. com/ for info 9/25/2001 CS 638, Fall 2001

Modified Pipeline (Direct. X 8. 0) Figure 1: A vertex shader's position in the

Modified Pipeline (Direct. X 8. 0) Figure 1: A vertex shader's position in the DX 8 rendering pipeline. 9/25/2001 CS 638, Fall 2001

Vertex Shaders • Vertex shaders are implemented in hardware in new generation cards •

Vertex Shaders • Vertex shaders are implemented in hardware in new generation cards • They get as input the (x, y, z) vertex location, its texture coordinates (s, t), its color, normal, etc • They have access to some registers – NOT retained from one vertex to the next • They have access to some constant memory – Programmer specifies what’s in that memory • They run a program – A sequence of instructions that compute on the available data 9/25/2001 CS 638, Fall 2001

IO for Vertex Shaders Figure 2: The inputs and outputs of vertex shaders. Arrows

IO for Vertex Shaders Figure 2: The inputs and outputs of vertex shaders. Arrows indicate read-only, write-only, or read-write. 9/25/2001 CS 638, Fall 2001

Vertex Program Ops • All operations work on vectors – Scalars are stored as

Vertex Program Ops • All operations work on vectors – Scalars are stored as vectors with the same value in each coordinate • Nvidia hardware provides an instruction set with 17 operations, including, but not limited to: – Add ADD, Multiply (by scalar, and vector element-wise), Multiply and add MAD, Reciprocal square root RSQ, Dot product DP 3, … – LIT which implements the Phong lighting model in one instruction – Can re-arrange (swizzle) and negate vectors before doing op • Matrices can be automatically mapped into registers • No branches, but can be done with other instructions – Set a value to 0/1 based on a comparison, then multiply 9/25/2001 CS 638, Fall 2001

Vertex Programming Example • Morph between a cube and sphere while doing lighting with

Vertex Programming Example • Morph between a cube and sphere while doing lighting with a directional light source (gray output) • Cube position and normal in attributes (input) 0, 1 • Sphere position and normal in attributes 2, 3 • Blend factor in attribute 15 • Inverse transpose modelview matrix in constants 12 -14 – Used to transform normal vectors into eye space • Composite matrix is in 4 -7 – Used to convert from object to homogeneous screen space • Light dir in 20, half-angle vector in 22, specular power, ambient, diffuse and specular coefficients all in 21 9/25/2001 CS 638, Fall 2001

Vertex Program Example • # blend normal and position v=v 1+(1 - )v 2

Vertex Program Example • # blend normal and position v=v 1+(1 - )v 2 MOV R 3, v[3] ; MOV R 5, v[2] ; ADD R 8, v[1], -R 3 ; ADD R 6, v[0], -R 5 ; MAD R 8, v[15]. x, R 8, R 3 MAD R 6, v[15]. x, R 6, R 5 ; • # transform normal to eye space DP 3 R 9. x, R 8, c[12] ; DP 3 R 9. y, R 8, c[13] ; DP 3 R 9. z, R 8, c[14] ; • # transform position and output DP 4 o[HPOS]. x, R 6, c[4] DP 4 o[HPOS]. y, R 6, c[5] DP 4 o[HPOS]. z, R 6, c[6] DP 4 o[HPOS]. w, R 6, c[7] 9/25/2001 • # normalize normal DP 3 R 9. w, R 9 ; RSQ R 9. w, R 9. w ; MUL R 9, R 9. w, R 9 ; • # apply lighting and output color DP 3 R 0. x, R 9, c[20] ; DP 3 R 0. y, R 9, c[22] ; MOV R 0. zw, c[21] ; LIT R 1, R 0 ; DP 3 o[COL 0], c[21], R 1 ; ; ; CS 638, Fall 2001

Shading Languages • Programming shading hardware is still a difficult process – Akin to

Shading Languages • Programming shading hardware is still a difficult process – Akin to writing assembly language programs • Shading languages and accompanying compilers allow user to write shaders in high level languages – For example, take the Renderman shading language and compile it for the Nvidia hardware – Proudfoot et. al. in SIGGRAPH 2001 9/25/2001 CS 638, Fall 2001

Pixel Shaders • Pixel shaders operate on fragments in place of the texturing hardware

Pixel Shaders • Pixel shaders operate on fragments in place of the texturing hardware – After rasterization, before any fragment tests or blending • Compute RGBA values for the fragment: – Fragment color, textures, fog color as inputs in registers – Temporary storage in registers • Each holds RGBA – A series of combiners that take the values in registers and perform operations on them • Referred to as a register combiner architecture • Supported by Direct. X 8. 0 and Open. GL Extensions 9/25/2001 CS 638, Fall 2001

Overall Architecture The pictures I used in lecture are copyright Nvidia Download the slides

Overall Architecture The pictures I used in lecture are copyright Nvidia Download the slides from the link below Slide 7 from Nvidia http: //developer. nvidia. com/docs/IO/1382/ATT/Register. Combiners. ppt 9/25/2001 CS 638, Fall 2001

General Register Combiner Slide 9 from Nvidia 9/25/2001 CS 638, Fall 2001

General Register Combiner Slide 9 from Nvidia 9/25/2001 CS 638, Fall 2001

General Combiner Operations Slide 16 from Nvidia 9/25/2001 CS 638, Fall 2001 Dot is

General Combiner Operations Slide 16 from Nvidia 9/25/2001 CS 638, Fall 2001 Dot is dot product Mult is element-wise multiply

Final Combiner RGB Out Slide 24 from Nvidia 9/25/2001 CS 638, Fall 2001

Final Combiner RGB Out Slide 24 from Nvidia 9/25/2001 CS 638, Fall 2001

Multitexture Light Map with Fog • • Diffuse color CD provided per vertex Texture

Multitexture Light Map with Fog • • Diffuse color CD provided per vertex Texture 0 is diffuse map TD, Texture 1 is light map TL CF is fog color, AF is fog factor Computes AF * CD * TL + (1 – AF) * CF Uses two textures, but only one general combiner Advantages: performance (single pass, no blending) Disadvantages: none 9/25/2001 CS 638, Fall 2001

General Combiner Setup Slide 40 from Nvidia 9/25/2001 CS 638, Fall 2001

General Combiner Setup Slide 40 from Nvidia 9/25/2001 CS 638, Fall 2001

Final Combiner Setup Slide 41 from Nvidia 9/25/2001 CS 638, Fall 2001

Final Combiner Setup Slide 41 from Nvidia 9/25/2001 CS 638, Fall 2001

Other Examples • There are other example uses at the Nvidia web site, including:

Other Examples • There are other example uses at the Nvidia web site, including: – Two ways of doing bump mapping with register combiners – Shadow volume construction with vertex shaders 9/25/2001 CS 638, Fall 2001