Advanced Stencil Shadow and Penumbral Wedge Rendering · S il Sh d CStencil Shadow Cons • Too...

90
Advanced Stencil Shadow Advanced Stencil Shadow and and and and Penumbral Wedge Rendering Penumbral Wedge Rendering E i L l E i L l Eric Lengyel Eric Lengyel

Transcript of Advanced Stencil Shadow and Penumbral Wedge Rendering · S il Sh d CStencil Shadow Cons • Too...

Advanced Stencil ShadowAdvanced Stencil Shadowandandandand

Penumbral Wedge RenderingPenumbral Wedge Rendering

E i L lE i L lEric LengyelEric Lengyel

O iO iOverviewOverview

•• Stencil shadow optimizationsStencil shadow optimizations•• Penumbral wedge renderingPenumbral wedge renderingg gg g•• DemonstrationDemonstration

QuestionsQuestions•• QuestionsQuestions

S il Sh d PS il Sh d PStencil Shadow ProsStencil Shadow Pros

•• Very accurate and robustVery accurate and robust•• Nearly artifactNearly artifact--freefreeyy

–– Faceting near the silhouette edgesFaceting near the silhouette edges

is the only problemis the only problem

•• Work for point lights and Work for point lights and directional lights equally welldirectional lights equally welldirectional lights equally welldirectional lights equally well

•• Low memory usageLow memory usage

S il Sh d CS il Sh d CStencil Shadow ConsStencil Shadow Cons

•• Too accurate Too accurate —— hard edgeshard edges–– Need a way to softenNeed a way to soften

•• Very fillVery fill--intensiveintensive–– Scissor and depth bounds test helpScissor and depth bounds test help

•• Significant CPU work requiredSignificant CPU work required–– Silhouette determinationSilhouette determinationSilhouette determinationSilhouette determination

–– Building shadow volumesBuilding shadow volumes

H d SH d SHardware SupportHardware Support

•• GL_EXT_stencil_two_sideGL_EXT_stencil_two_side•• GL ATI separate stencil funcGL ATI separate stencil func_ _ p _ __ _ p _ _

–– Both allow different stencil operations to be executed for Both allow different stencil operations to be executed for

front and back facing polygonsfront and back facing polygons

•• GL_EXT_depth_bounds_testGL_EXT_depth_bounds_test–– Helps reduce frame buffer writesHelps reduce frame buffer writespp

•• DoubleDouble--speed renderingspeed rendering

S i O i i iS i O i i iScissor OptimizationsScissor Optimizations

•• Most important fillMost important fill--rate rate optimization for stencil shadowsoptimization for stencil shadows

•• Even more important for Even more important for penumbral wedge shadowspenumbral wedge shadowsp gp g

•• Hardware does not generate Hardware does not generate fragments outside the scissor fragments outside the scissor fragments outside the scissor fragments outside the scissor rectangle rectangle —— very fastvery fast

S i O i i iS i O i i iScissor OptimizationsScissor Optimizations

•• Scissor rectangle can be appliedScissor rectangle can be appliedon a peron a per--light basis or even alight basis or even aperper--geometry basisgeometry basis

•• Requires that lights have a finite Requires that lights have a finite q gq gvolume of influencevolume of influence

Li h S iLi h S iLight ScissorLight ScissorLightLight

View FrustumView Frustum

Image PlaneImage Plane

CameraCamera

Image PlaneImage Plane

CameraCamera

Li h S iLi h S iLight ScissorLight Scissor

•• Project light volume onto the Project light volume onto the image planeimage plane

•• Intersect extents with the viewport Intersect extents with the viewport to get light’s scissor rectangleto get light’s scissor rectangleg g gg g g

•• Mathematical details at:Mathematical details at:–– http://www.gamasutra.com/features/http://www.gamasutra.com/features/p // g / /p // g / /

20021011/lengyel_01.htm20021011/lengyel_01.htm

N Li ht S iN Li ht S iNo Light ScissorNo Light Scissor

Shadow volumes extendShadow volumes extendto edges of viewportto edges of viewport

Light ScissorLight Scissor

Shadow volume fillShadow volume fillShadow volume fillShadow volume fillreduced significantlyreduced significantly

D h B d TD h B d TDepth Bounds TestDepth Bounds Test

LightLightMax DepthMax Depth

Min DepthMin Depth

View FrustumView Frustum

Min DepthMin Depth

CameraCamera

View FrustumView Frustum

CameraCamera

D h B d TD h B d TDepth Bounds TestDepth Bounds Test

•• Like a Like a zz scissor, but...scissor, but...•• Operates on values already in the Operates on values already in the p yp y

depth buffer, depth buffer, notnot the depth of the the depth of the incoming fragmentincoming fragmentg gg g

•• Saves writes to the stencil buffer Saves writes to the stencil buffer when shadowwhen shadow--receiving geometry receiving geometry when shadowwhen shadow receiving geometry receiving geometry is out of rangeis out of range

D h B d TD h B d TDepth Bounds TestDepth Bounds Test

Max DepthMax Depth

RejectedRejectedRejectedRejectedFragmentsFragments

ShadowShadowVolumeVolume

CCMin DepthMin Depth

CameraCamera ShadowShadowReceiverReceiver

N D th B d T tN D th B d T tNo Depth Bounds TestNo Depth Bounds Test

Shadow volumes extendShadow volumes extendcloser to and furthercloser to and furtherfrom camera thanfrom camera thannecessarynecessarynecessarynecessary

Depth Bounds TestDepth Bounds Test

Shadow volume fillShadow volume fillShadow volume fillShadow volume filloutside depth boundsoutside depth boundsis removedis removed

N D th B d T tN D th B d T tNo Depth Bounds TestNo Depth Bounds Test

A lot of extra shadowA lot of extra shadowvolume fill where wevolume fill where weknow it can’t have anyknow it can’t have anyeffecteffecteffecteffect

Depth Bounds TestDepth Bounds Test

Parts that can’tParts that can’tParts that can tParts that can tpossibly intersect thepossibly intersect theenvironment removedenvironment removed

D h B d TD h B d TDepth Bounds TestDepth Bounds Test

•• Depths bounds specified in Depths bounds specified in viewport coordinatesviewport coordinates

•• To get these from camera space, To get these from camera space, we need to apply projection matrix we need to apply projection matrix pp y p jpp y p jand viewport transformationand viewport transformation

•• Apply to points (0 0Apply to points (0 0 zz 1)1)•• Apply to points (0,0,Apply to points (0,0,zz,1),1)

D h B d TD h B d TDepth Bounds TestDepth Bounds Test

•• Let P be the projection matrix and Let P be the projection matrix and let [let [ddminmin, , ddmaxmax] be the depth range] be the depth range

•• Viewport depth Viewport depth dd corresponding to corresponding to camera space camera space zz is given byis given bypp g yg y

max min 33 34 max mind d P z P d dd

max min 33 34 max min

43 442 2d

P z P

G S iG S iGeometry ScissorGeometry Scissor

•• We can do much better than a We can do much better than a single scissor rectangle per lightsingle scissor rectangle per light

•• Calculate a scissor rectangle for Calculate a scissor rectangle for each geometry casting a shadoweach geometry casting a shadowg y gg y g

G S iG S iGeometry ScissorGeometry Scissor

•• Define a bounding box for the lightDefine a bounding box for the light–– Doesn’t need to contain the entire sphere of influence, just all Doesn’t need to contain the entire sphere of influence, just all

geometry that can receive shadowsgeometry that can receive shadows

–– For indoor scenes, the bounding box is usually determined by For indoor scenes, the bounding box is usually determined by

the locations of wallsthe locations of walls

G S iG S iGeometry ScissorGeometry Scissor

LightLightSphereSphere

BoundingBoundingggBoxBox

G S iG S iGeometry ScissorGeometry Scissor

•• For each geometry, define a simple For each geometry, define a simple bounding polyhedron for its bounding polyhedron for its shadow volumeshadow volume–– Construct a pyramid with its apex at the light’s position and Construct a pyramid with its apex at the light’s position and

its base far enough away to be outside the light’s sphere of its base far enough away to be outside the light’s sphere of

influenceinfluence

d b h bl dd b h bl d–– Want pyramid to be as tight as possible around geometryWant pyramid to be as tight as possible around geometry

G S iG S iGeometry ScissorGeometry Scissor

ShadowShadow

LightLightSphereSphere

VolumeVolumeBoundingBounding

PolyhedronPolyhedronpp PolyhedronPolyhedron

Light’sLight’sBoundingBounding

BoxBox

G S iG S iGeometry ScissorGeometry Scissor

•• Clip shadow volume’s bounding Clip shadow volume’s bounding polyhedron to light’s bounding boxpolyhedron to light’s bounding box

•• Project vertices of resulting Project vertices of resulting polyhedron onto image planepolyhedron onto image planep y g pp y g p

•• This produces the boundary of a This produces the boundary of a much smaller scissor rectanglemuch smaller scissor rectanglemuch smaller scissor rectanglemuch smaller scissor rectangle

•• Also gives us a much smaller depth Also gives us a much smaller depth bounds rangebounds rangebounds rangebounds range

G S iG S iGeometry ScissorGeometry Scissor

LightLightSphereSphere

ClippedClippedBoundingBounding

PolyhedronPolyhedronpp PolyhedronPolyhedron

Light’sLight’sBoundingBounding

BoxBox

G S iG S iGeometry ScissorGeometry Scissor

DepthDepthDepthDepthBoundsBounds

Scissor RectangleScissor Rectangle

Image PlaneImage Plane

CameraCamera

G S iG S iGeometry ScissorGeometry Scissor

DepthDepthBoundsBounds

ScissorScissor

Image PlaneImage Plane

CameraCamera

N G t S iN G t S iNo Geometry ScissorNo Geometry Scissor

Light scissor rectangleLight scissor rectangleg gg gand depth bounds testand depth bounds testare no help at all inare no help at all inthis casethis casethis casethis case

Geometry ScissorGeometry Scissor

Shadow volume fillShadow volume fillShadow volume fillShadow volume filldrastically reduceddrastically reduced

S i d D h B dS i d D h B dScissor and Depth BoundsScissor and Depth Bounds

•• Performance increase for ordinary Performance increase for ordinary stencil shadows not spectacularstencil shadows not spectacular

•• RealReal--world scenes get about 5world scenes get about 5--8% 8% faster using perfaster using per--geometry scissor geometry scissor g pg p g yg yand depth bounds testand depth bounds test

•• Hardware is doing very little work Hardware is doing very little work •• Hardware is doing very little work Hardware is doing very little work per fragment, so reducing number per fragment, so reducing number of fragments is not a huge winof fragments is not a huge winof fragments is not a huge winof fragments is not a huge win

S i d D h B dS i d D h B dScissor and Depth BoundsScissor and Depth Bounds

•• For penumbral wedge rendering, For penumbral wedge rendering, it’s a different storyit’s a different story

•• We will do much more work per We will do much more work per fragment, so eliminating a lot of fragment, so eliminating a lot of g , gg , gfragments really helpsfragments really helps

•• RealReal--world scenes can get 40world scenes can get 40--45% 45% •• RealReal world scenes can get 40world scenes can get 40 45% 45% faster using perfaster using per--geometry scissor geometry scissor and depth bounds testand depth bounds testand depth bounds testand depth bounds test

P b l W d Sh dP b l W d Sh dPenumbral Wedge ShadowsPenumbral Wedge Shadows

•• Generates soft shadows forGenerates soft shadows forarea light sourcesarea light sources

•• Based on original work by Tomas Based on original work by Tomas AkenineAkenine--Möller and Ulf Assarsson:Möller and Ulf Assarsson:–– http://graphics.cs.lth.se/http://graphics.cs.lth.se/

research/shadows/research/shadows/

•• A new rendering algorithm followsA new rendering algorithm follows

P b l W d Sh dP b l W d Sh dPenumbral Wedge ShadowsPenumbral Wedge Shadows

•• General procedureGeneral procedure–– First render ordinary stencil shadowsFirst render ordinary stencil shadows

–– For each silhouette edge, generate a wedge that represents the For each silhouette edge, generate a wedge that represents the

extent of the penumbraextent of the penumbra

–– For each wedge, apply a correction to the stencil shadows For each wedge, apply a correction to the stencil shadows

that softens the hard shadow outlinethat softens the hard shadow outline

P b l W d Sh dP b l W d Sh dPenumbral Wedge ShadowsPenumbral Wedge Shadows

Area LightArea Light

Shadow CastorShadow Castor

Inner PenumbraInner PenumbraOuter PenumbraOuter Penumbra

A P b l W dA P b l W dA Penumbral WedgeA Penumbral Wedge

Inner PenumbraInner Penumbra Outer PenumbraOuter Penumbra

ExtrudedExtrudedSilhouetteSilhouette

EdgeEdgeEdgeEdge

A P b l W dA P b l W dA Penumbral WedgeA Penumbral Wedge

S f Sh d C iS f Sh d C iSoft Shadow CorrectionSoft Shadow Correction

•• Darken area inside outer penumbraDarken area inside outer penumbra•• Lighten area inside inner penumbraLighten area inside inner penumbrag pg p

S f Sh d C iS f Sh d C iSoft Shadow CorrectionSoft Shadow Correction

•• Lighting pass for ordinary stencil Lighting pass for ordinary stencil shadows uses stencil testshadows uses stencil test–– 0 in stencil buffer at a particular pixel means light can reach 0 in stencil buffer at a particular pixel means light can reach

that pixelthat pixel

–– Nonzero means pixel is in shadowNonzero means pixel is in shadow

S f Sh d C iS f Sh d C iSoft Shadow CorrectionSoft Shadow Correction

•• For soft shadows, we use alpha For soft shadows, we use alpha blending during lighting passblending during lighting pass–– Value in the alpha channel represents how much of the area Value in the alpha channel represents how much of the area

light is coveredlight is covered

–– 0 means entire light source visible from a particular pixel0 means entire light source visible from a particular pixel

–– 1 means no part of light source is visible (fully shadowed)1 means no part of light source is visible (fully shadowed)

S f Sh d C iS f Sh d C iSoft Shadow CorrectionSoft Shadow CorrectionArea LightArea Lightgg

h dh dShadow CastorShadow Castor

Inner PenumbraInner PenumbraOuter PenumbraOuter Penumbra

0.50.5 1.01.00.00.0 0.50.5 1.01.00.00.0Alpha ValuesAlpha Values

S f Sh d C iS f Sh d C iSoft Shadow CorrectionSoft Shadow Correction

•• After rendering stencil shadows, After rendering stencil shadows, the stencil buffer contains integer the stencil buffer contains integer valuesvalues

•• Each value represents the number Each value represents the number ppof shadow volumes covering a of shadow volumes covering a particular pixelparticular pixelp pp p

S f Sh d C iS f Sh d C iSoft Shadow CorrectionSoft Shadow Correction

•• To make fractional corrections, we To make fractional corrections, we need to be able to treat the need to be able to treat the integer stencil values as either integer stencil values as either fixedfixed--point or floatingpoint or floating--point point numbersnumbers

•• We have two options...We have two options...pp

S f Sh d C iS f Sh d C iSoft Shadow CorrectionSoft Shadow Correction

•• Option 1Option 1 Render the shadow Render the shadow volumes into a 16volumes into a 16--bit floatingbit floating--point point render target instead of the render target instead of the ordinary stencil bufferordinary stencil buffer

•• Option 2Option 2 Copy the stencil values Copy the stencil values into the alpha channel and shift into the alpha channel and shift ppthem left by some number of them left by some number of fraction bitsfraction bits

S f Sh d C iS f Sh d C iSoft Shadow CorrectionSoft Shadow Correction

•• Rendering shadow volumes into a Rendering shadow volumes into a floatingfloating--point render targetpoint render target–– Requires hardware that can do thisRequires hardware that can do this

–– We need floatingWe need floating--point blendingpoint blending

–– We lose twoWe lose two--sided rendering unless we can access a facing sided rendering unless we can access a facing

registerregister

–– We lose doubleWe lose double--speed renderingspeed rendering

S f Sh d C iS f Sh d C iSoft Shadow CorrectionSoft Shadow Correction

•• Copying stencil values to alphaCopying stencil values to alpha–– Requires the OpenGL extension Requires the OpenGL extension

GL_NV_copy_depth_to_colorGL_NV_copy_depth_to_color–– After copying, we need to scale the alpha values since a 1 in After copying, we need to scale the alpha values since a 1 in

the stencil buffer is now 1/255 in the alpha channelthe stencil buffer is now 1/255 in the alpha channel

–– Scaling by 31.875 gives us 3.5 bit fixedScaling by 31.875 gives us 3.5 bit fixed--point in the alpha point in the alpha

h lh lchannelchannel

S f Sh d C iS f Sh d C iSoft Shadow CorrectionSoft Shadow Correction

•• Now we need to make fractional Now we need to make fractional corrections to the stencil valuescorrections to the stencil values–– For each inner half of a penumbral wedge, we subtract a For each inner half of a penumbral wedge, we subtract a

fractionfraction

–– For each outer half of a penumbral wedge, we add a fractionFor each outer half of a penumbral wedge, we add a fraction

–– Value becomes 0.5 at original stencil boundariesValue becomes 0.5 at original stencil boundaries

P b l W d R d iP b l W d R d iPenumbral Wedge RenderingPenumbral Wedge Rendering

•• How do we know which pixels need How do we know which pixels need a correction?a correction?

•• Each penumbral wedge is divided Each penumbral wedge is divided into two halvesinto two halves–– The inner halfThe inner half--wedgewedge

–– The outer halfThe outer half--wedgewedge

–– Both halves are bounded on one side by the extruded Both halves are bounded on one side by the extruded

silhouette edge used for stencil shadowssilhouette edge used for stencil shadows

P b l W d R d iP b l W d R d iPenumbral Wedge RenderingPenumbral Wedge Rendering

Inner HalfInner Half--wedgewedge Outer HalfOuter Half--wedgewedge

ExtrudedExtrudedSilhouetteSilhouette

EdgeEdgeEdgeEdge

P b l W d R d iP b l W d R d iPenumbral Wedge RenderingPenumbral Wedge Rendering

•• In the vertex program, we compute In the vertex program, we compute the three outside bounding planes the three outside bounding planes of a halfof a half--wedgewedge

•• Send these planes to the fragment Send these planes to the fragment p gp gprogram in viewport space!program in viewport space!–– Allows us to do a quick test to determine whether a viewportAllows us to do a quick test to determine whether a viewport--

space point is outside the halfspace point is outside the half--wedgewedge

P b l W d R d iP b l W d R d iPenumbral Wedge RenderingPenumbral Wedge Rendering

•• In the fragment program, we test In the fragment program, we test the viewportthe viewport--space position of the space position of the point in the frame buffer against point in the frame buffer against three halfthree half--wedge bounding planeswedge bounding planes

•• We will use the depth test to We will use the depth test to reject points on the wrong side of reject points on the wrong side of j p gj p gthe extruded silhouette edgethe extruded silhouette edge

P b l W d R d iP b l W d R d iPenumbral Wedge RenderingPenumbral Wedge Rendering

•• What’s a viewportWhat’s a viewport--space point in space point in the frame buffer?the frame buffer?–– The The xx and and yy viewport coordinates are available to fragment viewport coordinates are available to fragment

programs in the programs in the fragment.positionfragment.position registerregister

–– We need to read the We need to read the zz coordinate from a depth texturecoordinate from a depth texture

–– The coordinates (The coordinates (xx, , yy, , zz, 1) give the location of the point , 1) give the location of the point

already renderedalready rendered

P b l W d R d iP b l W d R d iPenumbral Wedge RenderingPenumbral Wedge Rendering•• Bounding plane testsBounding plane tests•• Bounding plane testsBounding plane tests

!!ARBfp1.0!!ARBfp1.0TEMPTEMP vssp, temp;vssp, temp;TEXTEX vssp.z, fragment.position, texture[0], RECT;vssp.z, fragment.position, texture[0], RECT;SWZSWZ vssp xyw fragment position x y 0 1;vssp xyw fragment position x y 0 1;SWZSWZ vssp.xyw, fragment.position, x, y, 0, 1;vssp.xyw, fragment.position, x, y, 0, 1;DP4DP4 temp.x, vssp, fragment.texcoord[0];temp.x, vssp, fragment.texcoord[0];DP4DP4 temp.y, vssp, fragment.texcoord[1];temp.y, vssp, fragment.texcoord[1];

44 f d[2]f d[2]DP4DP4 temp.z, vssp, fragment.texcoord[2];temp.z, vssp, fragment.texcoord[2];KILKIL temp.xyzz;temp.xyzz;

P b l W d R d iP b l W d R d iPenumbral Wedge RenderingPenumbral Wedge Rendering•• EarlyEarly out code sequence (Nvidia)out code sequence (Nvidia)•• EarlyEarly--out code sequence (Nvidia)out code sequence (Nvidia)

!!ARBfp1.0!!ARBfp1.0OPTION NV_fragment_program2OPTION NV_fragment_program2TEMPTEMP vssp, temp;vssp, temp;TEXTEX vssp z fragment position texture[0] RECT;vssp z fragment position texture[0] RECT;TEXTEX vssp.z, fragment.position, texture[0], RECT;vssp.z, fragment.position, texture[0], RECT;SWZSWZ vssp.xyw, fragment.position, x, y, 0, 1;vssp.xyw, fragment.position, x, y, 0, 1;DP4DP4CC temp.x, vssp, fragment.texcoord[0];temp.x, vssp, fragment.texcoord[0];

44 f d[1]f d[1]DP4DP4CC temp.y, vssp, fragment.texcoord[1];temp.y, vssp, fragment.texcoord[1];DP4DP4CC temp.z, vssp, fragment.texcoord[2];temp.z, vssp, fragment.texcoord[2];RETRET (LE.xyzz);(LE.xyzz);

P b l W d R d iP b l W d R d iPenumbral Wedge RenderingPenumbral Wedge Rendering

•• In preceding code, In preceding code, texture[0]texture[0] is is a copy of the depth buffera copy of the depth buffer

•• Texture coordinates 0, 1, 2 hold Texture coordinates 0, 1, 2 hold the 4the 4--component plane vectors for component plane vectors for p pp pthe three outside bounding planesthe three outside bounding planes–– If the dot product between the surface point and any plane is If the dot product between the surface point and any plane is

negative, then the point is outside the halfnegative, then the point is outside the half--wedgewedge

P b l W d R d iP b l W d R d iPenumbral Wedge RenderingPenumbral Wedge Rendering

•• Still have extruded silhouette Still have extruded silhouette plane to worry aboutplane to worry about–– We take care of it using the We take care of it using the zz testtest

–– Render inner halfRender inner half--wedges and outer halfwedges and outer half--wedges separatelywedges separately

–– For both groups of halfFor both groups of half--wedges divide into two batches...wedges divide into two batches...

P b l W d R d iP b l W d R d iPenumbral Wedge RenderingPenumbral Wedge Rendering

•• Sort halfSort half--wedges into two batches:wedges into two batches:–– 1) Those for which camera is on the 1) Those for which camera is on the positivepositive side of the side of the

silhouette edgesilhouette edge

–– 2) Those for which camera is on the 2) Those for which camera is on the negativenegative side of the side of the

silhouette edgesilhouette edge

•• Extruded silhouette plane normalExtruded silhouette plane normalalways points outward from always points outward from shadow volumeshadow volume

P b l W d R d iP b l W d R d iPenumbral Wedge RenderingPenumbral Wedge Rendering

Outer HalfOuter Half--wedgewedgeInner HalfInner Half--wedgewedge

Normals pointNormals point Normals pointNormals pointNormals pointNormals pointoutwardoutward

Normals pointNormals pointinwardinward

SilhouetteSilhouetteplaneplane

R d i O H lfR d i O H lf ddRendering Outer HalfRendering Outer Half--wedgeswedges

•• HalfHalf--wedges for which camera is on wedges for which camera is on positivepositive side of silhouette planeside of silhouette plane–– Render Render frontfront faces when faces when zz test test failsfails

•• HalfHalf--wedges for which camera is on wedges for which camera is on negativenegative side of silhouette planeside of silhouette planenegativenegative side of silhouette planeside of silhouette plane–– Render Render backback faces when faces when zz test test passespasses

R d i O H lfR d i O H lf ddRendering Outer HalfRendering Outer Half--wedgeswedgesCamera onCamera onCamera onCamera on

positive sidepositive sidenegative sidenegative side

R d i I H lfR d i I H lf ddRendering Inner HalfRendering Inner Half--wedgeswedges

•• HalfHalf--wedges for which camera is on wedges for which camera is on positivepositive side of silhouette planeside of silhouette plane–– Render Render frontfront faces when faces when zz test test passespasses

•• HalfHalf--wedges for which camera is on wedges for which camera is on negativenegative side of silhouette planeside of silhouette planenegativenegative side of silhouette planeside of silhouette plane–– Render Render backback faces when faces when zz test test failsfails

R d i I H lfR d i I H lf ddRendering Inner HalfRendering Inner Half--wedgeswedgesCamera onCamera on Camera onCamera on

negative sidenegative side positive sidepositive side

P b l W d R d iP b l W d R d iPenumbral Wedge RenderingPenumbral Wedge Rendering

P b l W d R d iP b l W d R d iPenumbral Wedge RenderingPenumbral Wedge Rendering

•• How much do we add or subtract?How much do we add or subtract?•• For each pixel covered by an inner For each pixel covered by an inner p yp y

halfhalf--wedge, we subtract the wedge, we subtract the fraction of light that is visiblefraction of light that is visiblegg

•• For each pixel covered by an outer For each pixel covered by an outer halfhalf--wedge we add the fraction of wedge we add the fraction of halfhalf wedge, we add the fraction of wedge, we add the fraction of light that is occludedlight that is occluded

A Li h O l iA Li h O l iArea Light OcclusionArea Light Occlusion

•• First, we need to transform the First, we need to transform the surface point into local light spacesurface point into local light space

•• Recall that we have the coordinates Recall that we have the coordinates in viewport space:in viewport space:p pp p

TEXTEX vssp.z, fragment.position, texture[0], RECT;vssp.z, fragment.position, texture[0], RECT;p , g p , [ ], ;p , g p , [ ], ;SWZSWZ vssp.xyw, fragment.position, x, y, 0, 1;vssp.xyw, fragment.position, x, y, 0, 1;

A Li h O l iA Li h O l iArea Light OcclusionArea Light Occlusion

•• Precalculate the transformation Precalculate the transformation from viewport space to light spacefrom viewport space to light space

•• Apply in fragment program:Apply in fragment program:

TEMPTEMP lssp;lssp;DP4DP4 lssp.x, vssp, xform_light[0];lssp.x, vssp, xform_light[0];DP4DP4 lssp.y, vssp, xform light[1];lssp.y, vssp, xform light[1];ssp.y, ssp, o _ g t[ ];ssp.y, ssp, o _ g t[ ];DP4DP4 lssp.z, vssp, xform_light[2];lssp.z, vssp, xform_light[2];DP4DP4 lssp.w, vssp, xform_light[3];lssp.w, vssp, xform_light[3];DIVDIV lssp lssp lssplssp lssp lsspDIVDIV lssp, lssp, lssp.w;lssp, lssp, lssp.w;

A Li h O l iA Li h O l iArea Light OcclusionArea Light Occlusion

•• Division by the Division by the ww coordinate is coordinate is necessary because we passed necessary because we passed through the inverse of the through the inverse of the projection matrix between projection matrix between viewport space and light spaceviewport space and light space

•• In light space, the In light space, the zz axis is axis is g p ,g p ,perpendicular to the plane of the perpendicular to the plane of the area lightarea lightgg

A Li h O l iA Li h O l iArea Light OcclusionArea Light Occlusion

•• We also adjust the transformation We also adjust the transformation to light space so that an arbitrarilyto light space so that an arbitrarily--sized rectangular light area is sized rectangular light area is mapped into [mapped into [––1,1] in both 1,1] in both xx and and yydirectionsdirections yy

(1,1)(1,1)

xx

( )( )

((––1,1,––1)1)

A Li h O l iA Li h O l iArea Light OcclusionArea Light Occlusion

•• Next, we need to project the Next, we need to project the endpoints of the silhouette edge endpoints of the silhouette edge onto the light planeonto the light plane

•• The vertex program can transform The vertex program can transform p gp gthese points from object space to these points from object space to light space and pass them to the light space and pass them to the g p pg p pfragment programfragment program

A Li h O l iA Li h O l iArea Light OcclusionArea Light Occlusion

Area LightArea Light

Silhouette EdgeSilhouette Edge

zz

Silhouette EdgeSilhouette Edge

Surface PointSurface Point

A Li h O l iA Li h O l iArea Light OcclusionArea Light Occlusion

•• But what if one of the endpoints But what if one of the endpoints can’t be projected because it can’t be projected because it doesn’t lie between the surface doesn’t lie between the surface point and the light plane?point and the light plane?

•• Solution: clip the silhouette edge Solution: clip the silhouette edge to a local “near plane” firstto a local “near plane” firstpp

A Li h O l iA Li h O l iArea Light OcclusionArea Light Occlusion

Area LightArea Light

Silhouette EdgeSilhouette Edge

zz

Silhouette EdgeSilhouette Edge

ClippedClippedEndpointEndpoint

Surface PointSurface Point

Near PlaneNear PlaneEndpointEndpoint

Surface PointSurface Point

A Li h O l iA Li h O l iArea Light OcclusionArea Light Occlusion

•• Once endpoints have been Once endpoints have been projected onto the light plane,projected onto the light plane,we are in a 2D worldwe are in a 2D world

•• Next step is to clip the projected Next step is to clip the projected p p p jp p p jedge to the [edge to the [––1,1] square1,1] square

A Li h O l iA Li h O l iArea Light OcclusionArea Light Occlusion

(1,1)(1,1)Area LightArea Light

(0,0)(0,0)

(( 11 1)1)((––1,1,––1)1)ClippedClippedEndpointEndpoint

A Li h O l iA Li h O l iArea Light OcclusionArea Light Occlusion

•• Earlier implementations perform Earlier implementations perform clipping in 3D against the four clipping in 3D against the four planes connecting the area light to planes connecting the area light to the lightthe light--space surface pointspace surface point–– Requires 36 fragment program instructionsRequires 36 fragment program instructions

–– But robust, and didn’t need near plane clip stepBut robust, and didn’t need near plane clip step

A Li h O l iA Li h O l iArea Light OcclusionArea Light Occlusion

•• However, clipping against near However, clipping against near plane in 3D first and then clipping plane in 3D first and then clipping against the four sides of the light against the four sides of the light area in 2D is much fasterarea in 2D is much faster–– Total of 23 fragment program instructionsTotal of 23 fragment program instructions

–– Also robustAlso robust

A Li h O l iA Li h O l iArea Light OcclusionArea Light Occlusion

•• Last step is to determine how Last step is to determine how much of the light source is much of the light source is occluded by the extruded occluded by the extruded silhouette edgesilhouette edge

•• We do this by calculating the area We do this by calculating the area of the sector subtended by the of the sector subtended by the yyclipped edgeclipped edge

A Li h O l iA Li h O l iArea Light OcclusionArea Light Occlusion

(1,1)(1,1)Area LightArea Light

(0,0)(0,0)OccludedOccluded

AreaArea

(( 11 1)1)

AreaArea

((––1,1,––1)1)

A Li h O l iA Li h O l iArea Light OcclusionArea Light Occlusion

•• The occluded area is equal to the The occluded area is equal to the total area between the two line total area between the two line segments connecting the center of segments connecting the center of the light and the two endpoints the light and the two endpoints minus the area of the triangle minus the area of the triangle formed by the center and the two formed by the center and the two endpointsendpoints

•• Calling the endpoints ECalling the endpoints E11 and Eand E22......g pg p 11 22

A Li h O l iA Li h O l iArea Light OcclusionArea Light Occlusion

Area LightArea Light TriangleTriangleAA

(0,0)(0,0)EE22

AreaArea

0.5 (E0.5 (E11 x Ex E22))(( 11 22))

OccludedOccludedAreaArea

EE11

AreaArea

A Li h O l iA Li h O l iArea Light OcclusionArea Light Occlusion

•• Total area between positive Total area between positive xx axis axis and the direction to any endpoint and the direction to any endpoint is fetched from a cube map textureis fetched from a cube map texture

(0 0)(0 0)(0,0)(0,0) xx

A Li h O l iA Li h O l iArea Light OcclusionArea Light Occlusion

•• We look up the sector area for We look up the sector area for both endpoints and subtract to get both endpoints and subtract to get the area between the linesthe area between the lines

•• Then subtract the triangle area, Then subtract the triangle area, g ,g ,given by half the cross product given by half the cross product between the two endpointsbetween the two endpointspp

•• Scale everything by 1/8 for 3.5 bit Scale everything by 1/8 for 3.5 bit fixed point resultfixed point resultfixed point resultfixed point result

A Li h O l iA Li h O l iArea Light OcclusionArea Light Occlusion

# Endpoints are stored in edge.xy and edge.zw# Endpoints are stored in edge.xy and edge.zw

# Look up sector areas# Look up sector areasTEXTEX area.x, edge.xyxx, texture[1], CUBE;area.x, edge.xyxx, texture[1], CUBE;TEXTEX area.y, edge.zwzz, texture[1], CUBE;area.y, edge.zwzz, texture[1], CUBE;

# Subtract areas and scale by 1/8# Subtract areas and scale by 1/8SUBSUB area.z, area.x, area.y;area.z, area.x, area.y;MADCMADC area, |area.z|, 0.125, {area, |area.z|, 0.125, {--0.0625, 0.0625, --0.125, 0.0, 0.0};0.125, 0.0, 0.0};

# If area > 0.5, replace with 1 # If area > 0.5, replace with 1 -- areaareaMOVMOV area.w (GT.x), area.w (GT.x), --area.y;area.y;

A Li h O l iA Li h O l iArea Light OcclusionArea Light Occlusion

# Calculate area of triangle# Calculate area of triangleMULMUL temp.xy, edge.xyxy, edge.wzwz;temp.xy, edge.xyxy, edge.wzwz;SUBSUB temp.w, temp.x, temp.y;temp.w, temp.x, temp.y;

# Fractional area of triangle relative to whole # Fractional area of triangle relative to whole light area is 1/8 of cross productlight area is 1/8 of cross product

# Subtract it from total area and scale by# Subtract it from total area and scale by# additional factor of 1/8 for fixed point# additional factor of 1/8 for fixed pointMADMAD result.color, |temp.w|, result.color, |temp.w|, --0.015625, area.w;0.015625, area.w;

A Li h O l iA Li h O l iArea Light OcclusionArea Light Occlusion

•• Occluded areas from multiple Occluded areas from multiple wedges add togetherwedges add together

(0,0)(0,0)OccludedOccluded

AreaArea

P b l W d R d iP b l W d R d iPenumbral Wedge RenderingPenumbral Wedge Rendering

•• After all wedges have been After all wedges have been rendered, scale the alpha channel rendered, scale the alpha channel by 8 to get pure fractionby 8 to get pure fraction–– Render a fullRender a full--screen quad 3 timesscreen quad 3 times

–– Double alpha each timeDouble alpha each time

–– Restricted to light scissor rectangleRestricted to light scissor rectangle

–– Color channels masked offColor channels masked off

P b l W d R d iP b l W d R d iPenumbral Wedge RenderingPenumbral Wedge Rendering

•• If the value was greater than one, If the value was greater than one, then it’s saturated to one, then it’s saturated to one, corresponding to fully shadowedcorresponding to fully shadowed

•• Then render lighting pass, Then render lighting pass, g g p ,g g p ,multiplying source color by one multiplying source color by one minus destination alphaminus destination alphapp

glBlendFunc(GL_ONE_MINUS_DST_ALPHA, GL_ONE);glBlendFunc(GL_ONE_MINUS_DST_ALPHA, GL_ONE);

P b l W d R d iP b l W d R d iPenumbral Wedge RenderingPenumbral Wedge Rendering

•• Using a floatingUsing a floating--point visibility point visibility buffer avoids scaling stepbuffer avoids scaling step

•• Visibility values still need to be Visibility values still need to be copied to alpha channel from copied to alpha channel from p pp prender targetrender target

S ll Li ht AS ll Li ht ASmall Light AreaSmall Light Area

Shadows sharper,Shadows sharper,p ,p ,rendering fasterrendering faster

Large Light AreaLarge Light Area

Shadows softerShadows softerShadows softer,Shadows softer,interact more,interact more,rendering slowerrendering slower

S iS i b l Sh db l Sh dSemiSemi--penumbral Shadowspenumbral Shadows

•• Method for speeding up penumbral Method for speeding up penumbral wedge soft shadowswedge soft shadows

•• Only render outer halfOnly render outer half--wedgeswedges•• Less correct but still looks goodLess correct but still looks good•• Less correct, but still looks goodLess correct, but still looks good•• Lose the ability to cast shadows Lose the ability to cast shadows

that have no point of 100% light that have no point of 100% light that have no point of 100% light that have no point of 100% light occlusionocclusion

S iS i b l Sh db l Sh dSemiSemi--penumbral Shadowspenumbral Shadows

Instead of fullInstead of fullpenumbra:penumbra:

Render outer halfRender outer halfof penumbra only:of penumbra only:of penumbra only:of penumbra only:

I d tI d tInner and outerInner and outerhalfhalf--wedgeswedgesrenderedrendered

Only outer halfOnly outer half--wedges renderedwedges rendered

Q i ?Q i ?Questions?Questions?

•• [email protected]@terathon.com

•• Slides and source code available:Slides and source code available:

http://www.terathon.com/http://www.terathon.com/