Shadow volumes and deferred rendering · Deferred shading • Defers shading until the second pass...

37
Shadow volumes and deferred rendering Computer Graphics CSE 167 Lecture 16

Transcript of Shadow volumes and deferred rendering · Deferred shading • Defers shading until the second pass...

Page 1: Shadow volumes and deferred rendering · Deferred shading • Defers shading until the second pass – Only fragments determined to be visible are shaded • First pass – Fill the

Shadow volumes and deferred rendering

Computer GraphicsCSE 167

Lecture 16

Page 2: Shadow volumes and deferred rendering · Deferred shading • Defers shading until the second pass – Only fragments determined to be visible are shaded • First pass – Fill the

CSE 167: Computer graphics

• Shadow volumes• Deferred rendering

– Deferred shading– Bloom effect– Glow effect– Screen space ambient occlusion

CSE 167, Winter 2018 2Based on slides courtesy of Jurgen Schulze

Page 3: Shadow volumes and deferred rendering · Deferred shading • Defers shading until the second pass – Only fragments determined to be visible are shaded • First pass – Fill the

Shadow volumes

3NVIDIA md2shader demo 

CSE 167, Winter 2018

Page 4: Shadow volumes and deferred rendering · Deferred shading • Defers shading until the second pass – Only fragments determined to be visible are shaded • First pass – Fill the

Shadow volumes

• A single point light source splits the world in two– Shadowed regions– Unshadowed regions– Volumetric shadow technique

• A shadow volume is the boundary between these shadowed and unshadowed regions– Determine if an object is inside the boundary of the shadowed region and know the object is shadowed

CSE 167, Winter 2018 4

Page 5: Shadow volumes and deferred rendering · Deferred shading • Defers shading until the second pass – Only fragments determined to be visible are shaded • First pass – Fill the

Shadow volumes

• Many variations of the algorithm exist• Most popular ones use the stencil buffer

– Depth Pass– Depth Fail (a.k.a. Carmack’s Reverse, developed for Doom 3)

– Exclusive‐Or (limited to non‐overlapping shadows)

• Most algorithms designed for hard shadows• Algorithms for soft shadows exist

CSE 167, Winter 2018 5

Page 6: Shadow volumes and deferred rendering · Deferred shading • Defers shading until the second pass – Only fragments determined to be visible are shaded • First pass – Fill the

Shadow volumes

CSE 167, Winter 2018 6

Shadowingobject

Partiallyshadowed object

Lightsource

Eye position(note that shadows are independent of the eye position)

Surface insideshadow volume(shadowed)

Surface outsideshadow volume(illuminated)

Shadowvolume(infinite extent)

Page 7: Shadow volumes and deferred rendering · Deferred shading • Defers shading until the second pass – Only fragments determined to be visible are shaded • First pass – Fill the

Shadow volumes

• Given the scene and a light source position, determine the geometry of the shadow volume

• High‐level algorithm– Render the scene in two passes

• Draw scene with the light enabled,updating only fragments in unshadowed region

• Draw scene with the light disabled,updated only fragments in shadowed region

CSE 167, Winter 2018 7

Page 8: Shadow volumes and deferred rendering · Deferred shading • Defers shading until the second pass – Only fragments determined to be visible are shaded • First pass – Fill the

Shadow volume construction

• Need to generate shadow polygons to bound shadow volume

• Extrude silhouette edges from light source

CSE 167, Winter 2018 8Extruded shadow volumes

Page 9: Shadow volumes and deferred rendering · Deferred shading • Defers shading until the second pass – Only fragments determined to be visible are shaded • First pass – Fill the

Shadow volume construction

• Done on the CPU• Silhouette edge detection

– An edge is a silhouette if one adjacent triangle is front facing, the other back facing with respect to the light

• Extrude polygons from silhouette edges

CSE 167, Winter 2018 9

Page 10: Shadow volumes and deferred rendering · Deferred shading • Defers shading until the second pass – Only fragments determined to be visible are shaded • First pass – Fill the

Stenciled shadow volumes

• Advantages– Support omnidirectional lights– Exact shadow boundaries

• Disadvantages– Fill‐rate intensive– Expensive to compute shadow volume geometry– Hard shadow boundaries, not soft shadows– Difficult to implement robustly

CSE 167, Winter 2018 10

Source: Zach Lynn

Page 11: Shadow volumes and deferred rendering · Deferred shading • Defers shading until the second pass – Only fragments determined to be visible are shaded • First pass – Fill the

The stencil buffer

• Per‐pixel 2D buffer on the GPU• Similarities to depth buffer in way it is stored and accessed

• Stores an integer value per pixel, typically 8 bits• Like a stencil, allows to block pixels from being drawn

• Typical uses:– Shadow mapping– Planar reflections– Portal rendering

CSE 167, Winter 2018 11

Page 12: Shadow volumes and deferred rendering · Deferred shading • Defers shading until the second pass – Only fragments determined to be visible are shaded • First pass – Fill the

The stencil buffer

• Using the stencil buffer, rendering a stencil mirror tutorialhttp://www.youtube.com/watch?v=3xzq‐YEOIsk

CSE 167, Winter 2018 12Source: Adrian‐Florin Visan

Page 13: Shadow volumes and deferred rendering · Deferred shading • Defers shading until the second pass – Only fragments determined to be visible are shaded • First pass – Fill the

Tagging pixels as shadowed or unshadowed

• The stenciling approach– Clear stencil buffer to zero and depth buffer to 1.0– Render scene to leave depth buffer with closest Z values

– Render shadow volume into frame buffer with depth testing but without updating color and depth, but inverting a stencil bit (Exclusive‐Or method)

– This leaves stencil bit set within shadow

CSE 167, Winter 2018 13

Page 14: Shadow volumes and deferred rendering · Deferred shading • Defers shading until the second pass – Only fragments determined to be visible are shaded • First pass – Fill the

Stencil inverting of shadow volume

CSE 167, Winter 2018 14

Eye position

Lightsource

Shadowingobject

Two inverts, left zero

One invert, left one

Zero inverts, left zero

Page 15: Shadow volumes and deferred rendering · Deferred shading • Defers shading until the second pass – Only fragments determined to be visible are shaded • First pass – Fill the

Visualizing stenciled shadow volume tagging

CSE 167, Winter 2018 15

red = stencil value of 1green  = stencil value of 0

Shadowed scene Stencil buffer contents

GLUT shadowvol example credit: Tom McReynolds

Page 16: Shadow volumes and deferred rendering · Deferred shading • Defers shading until the second pass – Only fragments determined to be visible are shaded • First pass – Fill the

Shadow volumes with intersecting polygons

• Use a stencil enter/leave counting approach– Draw shadow volume twice using face culling

• 1st pass: render front faces and increment when depth test passes

• 2nd pass: render back faces and decrement when depth test passes

– This two‐pass way is more expensive than inverting

• Use inverting if all shadow volumes have no polygon intersections

CSE 167, Winter 2018 16

Page 17: Shadow volumes and deferred rendering · Deferred shading • Defers shading until the second pass – Only fragments determined to be visible are shaded • First pass – Fill the

Increment/Decrement stencil volumes

CSE 167, Winter 2018 17

Shadowing objectLightsource 

Eyeposition 

zero

zero

+1

+1+2 +2

+3

Page 18: Shadow volumes and deferred rendering · Deferred shading • Defers shading until the second pass – Only fragments determined to be visible are shaded • First pass – Fill the

Shadow volume demo

http://www.paulsprojects.net/opengl/shadvol/shadvol.html

CSE 167, Winter 2018 18

Page 19: Shadow volumes and deferred rendering · Deferred shading • Defers shading until the second pass – Only fragments determined to be visible are shaded • First pass – Fill the

Shadow volumes

• Resources– http://en.wikipedia.org/wiki/Shadow_volume– https://www.gamedev.net/articles/programming/graphics/the‐theory‐of‐stencil‐shadow‐volumes‐r1873/

CSE 167, Winter 2018 19

Page 20: Shadow volumes and deferred rendering · Deferred shading • Defers shading until the second pass – Only fragments determined to be visible are shaded • First pass – Fill the

Deferred rendering

CSE 167, Winter 2018 20

Page 21: Shadow volumes and deferred rendering · Deferred shading • Defers shading until the second pass – Only fragments determined to be visible are shaded • First pass – Fill the

Deferred rendering

• Different than forward rendering (traditional rendering, what we have been covering)

• Deferred rendering defers the final computation until a second pass– First pass

• Data required for the final computation is computed and stored in the geometry buffer (G‐buffer)

– Examples: depth (or position, though position can be calculated from depth), surface normal vector, surface color, surface texture, additional surface properties

– Second pass• An algorithm, typically implemented as a shader, generates the final image using the G‐buffer and possibly other data

21CSE 167, Winter 2018

Page 22: Shadow volumes and deferred rendering · Deferred shading • Defers shading until the second pass – Only fragments determined to be visible are shaded • First pass – Fill the

Deferred shading

• Defers shading until the second pass– Only fragments determined to be visible are shaded

• First pass– Fill the G‐buffer with data required to compute shading

• Position, surface normal vector, surface properties

• Second pass– Compute shading for each pixel given light sources and the G‐buffer

22CSE 167, Winter 2018

Page 23: Shadow volumes and deferred rendering · Deferred shading • Defers shading until the second pass – Only fragments determined to be visible are shaded • First pass – Fill the

Deferred shading• Advantages

– Decouples lighting from scene geometry– Reduces per pixel shading complexity

• Forward rendering O(num_fragments * num_lights)– Fragments from all objects

• Deferred rendering O(num_lights)– Only the fragment that is visible

• For example, greater than 1000 lights can be rendered at 60 fps• Disadvantages

– More expensive (memory, bandwidth, shader instructions)• Tutorial:

– http://gamedevs.org/uploads/deferred‐shading‐tutorial.pdf

23CSE 167, Winter 2018

Page 24: Shadow volumes and deferred rendering · Deferred shading • Defers shading until the second pass – Only fragments determined to be visible are shaded • First pass – Fill the

Deferred shading

• Example G‐buffer

CSE 167, Winter 2018 24

Normal vector(color encoded)

Specular color

Color

Depth(color encoded)

Page 25: Shadow volumes and deferred rendering · Deferred shading • Defers shading until the second pass – Only fragments determined to be visible are shaded • First pass – Fill the

Deferred shading

• Example result

CSE 167, Winter 2018 25

Page 26: Shadow volumes and deferred rendering · Deferred shading • Defers shading until the second pass – Only fragments determined to be visible are shaded • First pass – Fill the

Bloom effect• Computer displays have limited dynamic range• Bloom gives a scene a look of bright lighting and 

overexposure• Provides visual cues about brightness and atmosphere

– Caused by light scattering in atmosphere, or within our eyes

26Left: no bloom, right: bloom. Source: http://jmonkeyengine.org

CSE 167, Winter 2018

Page 27: Shadow volumes and deferred rendering · Deferred shading • Defers shading until the second pass – Only fragments determined to be visible are shaded • First pass – Fill the

Bloom effect• Step 1: Extract all highlights of the 

rendered scene, superimpose them and make them more intense– Operates on G‐buffer– Often done with G‐buffer smaller 

(lower resolution) than frame buffer– Highlights found by thresholding 

luminance• Step 2: Blur G‐buffer (e.g., using a 

Gaussian low pass filter)• Step 3: Composite off‐screen buffer 

with back buffer

CSE 167, Winter 2018 27

Bloom shader render stepsSource: http://www.klopfenstein.net

Page 28: Shadow volumes and deferred rendering · Deferred shading • Defers shading until the second pass – Only fragments determined to be visible are shaded • First pass – Fill the

Bloom effect

• Example result

CSE 167, Winter 2018 28

With bloomWithout bloom

Page 29: Shadow volumes and deferred rendering · Deferred shading • Defers shading until the second pass – Only fragments determined to be visible are shaded • First pass – Fill the

Glow effect

• Use a G‐buffer for “glow sources”– Render entire scene to back buffer– Render only glowing objects to a smaller (lower resolution) off‐screen G‐buffer (can be combined with bloom G‐buffer)

– Blur G‐buffer (e.g., using a Gaussian low pass filter)– Composite off‐screen buffer with back buffer

CSE 167, Winter 2018 29

Page 30: Shadow volumes and deferred rendering · Deferred shading • Defers shading until the second pass – Only fragments determined to be visible are shaded • First pass – Fill the

Example: glowing lava

https://www.youtube.com/watch?v=hmsMk‐skquI

CSE 167, Winter 2018 30

Page 31: Shadow volumes and deferred rendering · Deferred shading • Defers shading until the second pass – Only fragments determined to be visible are shaded • First pass – Fill the

Bloom and glow references

• Bloom Tutorialhttp://prideout.net/archive/bloom/

• GPU Gems Chapter on Glowhttp://http.developer.nvidia.com/GPUGems/gpugems_ch21.html

• GLSL Shader for Gaussian Blurhttp://www.ozone3d.net/tutorials/image_filtering_p2.php

31CSE 167, Winter 2018

Page 32: Shadow volumes and deferred rendering · Deferred shading • Defers shading until the second pass – Only fragments determined to be visible are shaded • First pass – Fill the

Ambient occlusion

• Coarse approximation of global illumination• Often referred to as "sky light"• Global method

– Illumination at each point is a function of other geometry in the scene

• Appearance is similar to what objects appear as on an overcast day– Assumption: concave objects are hit by less light than convex ones

CSE 167, Winter 2018 32

Page 33: Shadow volumes and deferred rendering · Deferred shading • Defers shading until the second pass – Only fragments determined to be visible are shaded • First pass – Fill the

Screen space ambient occlusion (SSAO)

• Screen space = deferred rendering• Approximates ambient occlusion in real time• Developed by Vladimir Kajalin (Crytek)• First use in PC game Crysis (2007)

CSE 167, Winter 2018 33

Page 34: Shadow volumes and deferred rendering · Deferred shading • Defers shading until the second pass – Only fragments determined to be visible are shaded • First pass – Fill the

Basic SSAO algorithm

• First pass– Render scene normally and copy Z values to G‐buffer alpha channel 

• Second pass– Pixel shader samples depth values around the processed fragment and computes amount of occlusion, stores result in G‐buffer red channel

– Occlusion depends on depth difference between sampled fragment and currently processed fragment

CSE 167, Winter 2018 34

Page 35: Shadow volumes and deferred rendering · Deferred shading • Defers shading until the second pass – Only fragments determined to be visible are shaded • First pass – Fill the

SSAO algorithm with surface normals

• First pass– Render scene normally and copy Z values to G‐buffer alpha channel and surface normal vectors to G‐buffer RGB channels

• Second pass– Use Z values and surface normal vectors to compute occlusion between current pixel and several samples around that pixel

CSE 167, Winter 2018 35With SSAOWithout SSAO

Page 36: Shadow volumes and deferred rendering · Deferred shading • Defers shading until the second pass – Only fragments determined to be visible are shaded • First pass – Fill the

Screen space ambient occlusion (SSAO)

• Advantages– Deferred rendering algorithm: independent of scene complexity– No pre‐processing, no memory allocation in RAM– Works with dynamic scenes– Works in the same way for every pixel– No CPU usage: executed completely on GPU

• Disadvantages– Local and view‐dependent (dependent on adjacent texel depths)– Hard to correctly smooth/blur out noise without interfering with 

depth discontinuities, such as object edges, which should not be smoothed out

CSE 167, Winter 2018 36

Page 37: Shadow volumes and deferred rendering · Deferred shading • Defers shading until the second pass – Only fragments determined to be visible are shaded • First pass – Fill the

SSAO References

• Nvidia’s documentation– http://developer.download.nvidia.com/SDK/10.5/direct3d/Source/ScreenSpaceAO/doc/ScreenSpaceAO.pdf

CSE 167, Winter 2018 37