paris master class 2011 - 01 deferred lighting, msaa

51
Deferred Lighting, MSAA Wolfgang Engel Confetti Special Effects Inc., Carlsbad Paris Master Class

Upload: wolfgang-engel

Post on 28-Jul-2015

143 views

Category:

Engineering


2 download

TRANSCRIPT

Page 1: Paris Master Class 2011 - 01 Deferred Lighting, MSAA

Deferred Lighting, MSAA

Wolfgang EngelConfetti Special Effects Inc., Carlsbad

Paris Master Class

Page 2: Paris Master Class 2011 - 01 Deferred Lighting, MSAA
Page 3: Paris Master Class 2011 - 01 Deferred Lighting, MSAA
Page 4: Paris Master Class 2011 - 01 Deferred Lighting, MSAA
Page 5: Paris Master Class 2011 - 01 Deferred Lighting, MSAA
Page 6: Paris Master Class 2011 - 01 Deferred Lighting, MSAA
Page 7: Paris Master Class 2011 - 01 Deferred Lighting, MSAA
Page 8: Paris Master Class 2011 - 01 Deferred Lighting, MSAA

Deferred Lighting

Page 9: Paris Master Class 2011 - 01 Deferred Lighting, MSAA

Deferred Lighting

Page 10: Paris Master Class 2011 - 01 Deferred Lighting, MSAA

Agenda

• Rendering Many Lights History• Light Pre-Pass (LPP)• LPP Implementation

• Efficient Light rendering on DX 9, 10, 11• Balance Quality / Performance

• MSAA Implementation on DX 10.0, 10.1, XBOX 360, 11

Page 11: Paris Master Class 2011 - 01 Deferred Lighting, MSAA

Z Pre-Pass

Page 12: Paris Master Class 2011 - 01 Deferred Lighting, MSAA

Rendering Many Lights History

• Forward / Z Pre-Pass rendering– Re-render geometry for each light

-> lots of geometry throughput (still an option on older hardware) -> DOOM III

– Write pixel shader with four or eight lights -> draw lights per-object -> need to split up geometry following light distribution

– Store light properties in textures and index into this texture -> dependent texture look-up and lights are not fully dynamic

Page 13: Paris Master Class 2011 - 01 Deferred Lighting, MSAA

Rendering Many Lights History

• Deferred Shading / RenderingSplit up rendering into a geometry pass and a lighting pass -> makes lights independent from geometry

• Geometry pass stores all material and light properties

Killzone 2’s G-Buffer Layout (courtesy of Michal Valient)

Page 14: Paris Master Class 2011 - 01 Deferred Lighting, MSAA

Rendering Many Lights HistoryDeferred Shading / Rendering

Depth Buffer

DeferredLighting

Forward Rendering

Switch off depth write

Specular /Motion VecNormals Albedo /

Shadow

Sort Back-To-Front

Render opaque objects Transparent objects

Page 15: Paris Master Class 2011 - 01 Deferred Lighting, MSAA

Rendering Many Lights History• Advantages:

– Only one geometry pass for the main view (probably more than a dozen for other views like shadows, reflections, transparent objects etc.)

– Lights are blit and therefore only limited by memory bandwidth• Disadvantages:

– Memory bandwidth (reading four render targets for each light)– Recalculate full lighting equation for every light– Limited material representation in G-Buffer– MSAA difficult compared to Forward Renderer

Page 16: Paris Master Class 2011 - 01 Deferred Lighting, MSAA

Light Pre-Pass

• Light Pre-Pass / Deferred Lighting

NormalsSpecular Power Depth

Light Buffer

Frame Buffer

Render opaque Geometry sorted front-to-back

Blit Lights into Light Buffer (sorted front-to-back)

Render opaque Geometry sorted front-to-backorBlit ambient term and other lighting terms into final image

Color

Page 17: Paris Master Class 2011 - 01 Deferred Lighting, MSAA

Light Pre-Pass

• Version A (only opaque objects):– Geometry pass: fill up normal and depth buffer– Lighting pass: store light properties in light buffer– 2. Geometry pass: fetch light buffer and apply

different material terms per surface by re-constructing the lighting equation

Page 18: Paris Master Class 2011 - 01 Deferred Lighting, MSAA

Light Pre-Pass

• Version B (similar to S.T.A.L.K.E.R: Clear Skies [Lobanchikov]):– Geometry pass: fill up normal + spec. power and

depth buffer and a color buffer for the ambient pass– Lighting pass: store light properties in light buffer– Ambient + Resolve (MSAA) pass: fetch light buffer

use its content as diffuse and specular content and add the ambient term while resolving into the main buffer

Page 19: Paris Master Class 2011 - 01 Deferred Lighting, MSAA

Light Pre-Pass

S.T.A.L.K.E.R: Clear Skies

Page 20: Paris Master Class 2011 - 01 Deferred Lighting, MSAA

Light Pre-Pass

• Light Properties that are stored in light buffer

• Light buffer layout

• Dred/green/blue is the light color

Page 21: Paris Master Class 2011 - 01 Deferred Lighting, MSAA

Light Pre-Pass

• Specular stored as luminance • Reconstructed with diffuse chromacity

Page 22: Paris Master Class 2011 - 01 Deferred Lighting, MSAA

Light Pre-Pass

CryEngine 3: On the right the approx. specular term of the light buffer and on the lefta correct specular term with its own specular color (courtesy of Martin Mittring)

Page 23: Paris Master Class 2011 - 01 Deferred Lighting, MSAA

Light Pre-Pass

CryEngine 3: On the right the approx. specular term of the light buffer and on the leftthe final image (courtesy of Martin Mittring)

Page 24: Paris Master Class 2011 - 01 Deferred Lighting, MSAA

Light Pre-Pass

• Advantage of Version A: offers more material variety

• Version B faster: does not need to render scene geometry a second time

Page 25: Paris Master Class 2011 - 01 Deferred Lighting, MSAA

Light Pre-Pass Implementation

• Memory Bandwidth Optimizations (DirectX 9)– Depth-fail Stencil lights: render light volume in stencil and

then blit light [Hargreaves][Valient]– Geometry lights: render bounding geometry -> never get

inside light -> avoid depth func change [Thibieroz04]– Scissor lights: construct scissor rectangle from bounding

volume and set it [Placeres] (PS3: depth bound testing ~ scissor in 3D)

– Batched lights: sort lights by size, x and y position in screenspace. Render close lights in batches of 4, 8, 16

Dis

tanc

e fr

om C

amer

a

Page 26: Paris Master Class 2011 - 01 Deferred Lighting, MSAA

Light Pre-Pass Implementation

• Memory Bandwidth Optimizations (DirectX 10, 10.1, 11)– Light bounds calculated in Geometry Shader

• GS bounding box: construct bounding box around light in the geometry shader

• Render only light for what is in this box• Check out implementation in the Deferred Lighting

example provided

– Implement lighting with the compute shader

Page 27: Paris Master Class 2011 - 01 Deferred Lighting, MSAA

Light Pre-Pass Implementation

Resistance 2TM in-game screenshot; first row on the left is the depth buffer, on the right is the normal buffer; in the second row is the diffuse light buffer and on the right is the specular light buffer; in the last row is the final result.

Page 28: Paris Master Class 2011 - 01 Deferred Lighting, MSAA

Light Pre-Pass Implementation

UnchartedTM in-game screenshot

Page 29: Paris Master Class 2011 - 01 Deferred Lighting, MSAA

Light Pre-Pass Implementation

BlurTM in-game screenshot

Page 30: Paris Master Class 2011 - 01 Deferred Lighting, MSAA

Light Pre-Pass Implementation

• Balance Quality / Performance– Stop rendering dynamic lights after a certain

range for example 40 meters and render glow cards instead

– Use smaller light buffer for distant lights and scale up

Page 31: Paris Master Class 2011 - 01 Deferred Lighting, MSAA

Light Zoning

• Deferred light source w/o shadows tend to bleed:– Shadows are expensive

• Problem: e.g. light shines on other side of wall on the floor -> have special light types that deal with the problem like a 180 degree spotlight; artists have to place this

• Solution 1: Advanced interzone lighting analysis [Lengyel]

Page 32: Paris Master Class 2011 - 01 Deferred Lighting, MSAA

Light Zoning

• Solution 2 [Kaplanyan]: use artist-definedclipping geometry: clip volumes– Mask the stencil in addition to light volume

masking– Very cheap providing fourfold stencil tagging

speed

Page 33: Paris Master Class 2011 - 01 Deferred Lighting, MSAA

MSAA

• MSAA Light Pre-Pass Version A• MSAA Light Pre-Pass Version B• Edge Detection to run per-sample

– Centroid Sampling trick– Normal Sampling trick

Page 34: Paris Master Class 2011 - 01 Deferred Lighting, MSAA

MSAA

Multisample Anti-Aliasing (courtesy of Nicolas Thibieroz)

Page 35: Paris Master Class 2011 - 01 Deferred Lighting, MSAA

MSAA

• LPP Version A1. Geometry pass: render into MSAA’ed normal and

depth buffer2. Lighting pass (ideal world): render by reading each

sample in the MSAA’ed buffer and write into each sample in the MSAA’ed light buffer

3. Second Geometry pass: render geometry into MSAA’ed accumulation buffer by reading the MSAA’ed light buffer, depth and normal buffer and re-constructing the lighting equation

4. Resolve: into main buffer

Page 36: Paris Master Class 2011 - 01 Deferred Lighting, MSAA

MSAA

• LPP Version B1. Geometry pass: render into MSAA’ed normal,

depth and color buffer2. Lighting pass (ideal world): render by reading

each sample in the MSAA’ed buffer and write into a sample in the MSAA’ed light buffer

3. Ambient pass: resolve light buffer and color buffer into main buffer by adding the ambient term

Page 37: Paris Master Class 2011 - 01 Deferred Lighting, MSAA

MSAA

• Lighting pass: MSAA lighting is required e.g. one sample is covered by a green light and three by a red light

• Per sample is expensive- > optimize by detecting polygon edges– Run screen-space edge detection filter with

normal and/or depth buffer– Or use centroid sampling

Page 38: Paris Master Class 2011 - 01 Deferred Lighting, MSAA

• Edge Detection - Centroid SamplingSample location with MSAA commonly center of pixel, except with centroid sampling -> sample location within the primitive

Edge detection with centroid sampling (courtesy of Nicolas Thibieroz)

MSAA

Page 39: Paris Master Class 2011 - 01 Deferred Lighting, MSAA

MSAA

• Edge Detection - Centroid Sampling Trick II– Sample without and with centroid sampling -> find

out if the second sample coordinate is offset [Thieberoz]

– Check the fractional part of the position value if it equals 0.5 -> no polygon edge [Persson]

Page 40: Paris Master Class 2011 - 01 Deferred Lighting, MSAA

MSAA

• Edge Detection - Centroid sampling Trick III:Disclaimer: – Probably only works with 2xMSAA– PC Hardware might return the center point for

4xMSAA [Shishkovtsov]

Page 41: Paris Master Class 2011 - 01 Deferred Lighting, MSAA

MSAA• Edge Detection – MSAA’ed Normal Buffer• Normals in MSSA’ed buffer; e.g. 4 normals for

4xMSAA– If directions are similar -> averaged length is close to

one– If directions are different -> averaged length is

decreasedclip( abs(L-P)-epsilon )L – sample normal buffer with linear samplingP – sample normal buffer with point sampling

• Assumes normals are in buffer with x,y and z value -> x and y alone wouldn’t work [Pranckevičius]

Page 42: Paris Master Class 2011 - 01 Deferred Lighting, MSAA

MSAA

• Store result in stencil buffer • Two shaders:

– run the per-sample shader only on edges – rest -> run per-pixel shader

// if MSAA is usedfor (int p = 0; p < 2; p++){…

renderer->setDepthState(stencilTest, (p == 0)? 0x1 : 0x0);renderer->setShader(lighting[p]);

…}

Page 43: Paris Master Class 2011 - 01 Deferred Lighting, MSAA

MSAA… // shader that fills the G-Buffer struct PsIn { centroid float4 position : SV_Position;…}; // find polygon edge with centroid samplingOut.base.a = dot(abs(frac(In.position.xy) - 0.5), 1000.0);// shader that resolves the color buffer with the edge data in alpha// resolve color buffer and write out 1 into a non-MSAA’ed render targetreturn (base.a > 0.0);// shader that creates the stencil buffer maskclip(BackBuffer.Sample(filter, In.texCoord).a - 0.5);…

Page 44: Paris Master Class 2011 - 01 Deferred Lighting, MSAA

MSAA• SV_Position outputs the sample location -> center of the pixel

(without MSAA)– top-left pixel (0.5, 0.5)– bottom-right (width - 0.5, height - 0.5)

float2 screenPos = In.Pos.xy;screenPos /= float2( m_FBWidth, m_FBHeight );float4 outColor = g_txColor.Sample(g_SamplePoint, screenPos );

• Casting to int will return integer coordinates for the pixel given round-towards-zero semantics -> true for any sample location within the pixel

float2 screenPos = In.Pos.xy;int3 iScreenPos = int3( int2(screenPos), 0 );float4 outColor = g_txColor.Load( iScreenPos );

• Accessing samples requires uint uSample : SV_SAMPLEINDEX; // Sample frequency

// Sample GBuffersC = Color.Load( nScreenCoordinates, In.uSample);

Page 45: Paris Master Class 2011 - 01 Deferred Lighting, MSAA

MSAA

• DirectX 10.1, 11, XBOX 360: execute pixel shader per sample

struct PsIn {… uint uSample : SV_SAMPLEINDEX; // Sample frequency}; float4 PSLightPass_EdgeSampleOnly(PsIn In) : SV_TARGET{ // Sample GBuffers C = Color.Load( nScreenCoordinates, In.uSample); Norm = Normal.Load( nScreenCoordinates, In.uSample); D = Depth.Load( nScreenCoordinates, In.uSample);

// extract data from GBuffers //…

// do the lighting return LightEquation(…);}

Page 46: Paris Master Class 2011 - 01 Deferred Lighting, MSAA

MSAA

• DirectX 9: – Can’t run shader at sample frequency or support

of mask– no MSAA’ed depth buffer read and write

• DirectX 10– Can write with a mask into samples and read from

samples -> shader runs per-pixel– No MSAA’ed depth buffer read and write officially

(maybe if you ask your hardware support engineer )

Page 47: Paris Master Class 2011 - 01 Deferred Lighting, MSAA

MSAA

• MSAA has best quality settings• Might be quite expensive

• Morphological Anti-Aliasing• http://www.iryoku.com/mlaa/• Drawback: doesn’t work well on moving

objects

Page 48: Paris Master Class 2011 - 01 Deferred Lighting, MSAA

Homework

• Read the written version of my SIGGRAPH talk• Read the overview on different Deferred

Lighting Approaches by Naty Hoffman: http://www.realtimerendering.com/blog/deferred-lighting-approaches/

• Read in Real-Time Rendering Section 7.9.2

Page 49: Paris Master Class 2011 - 01 Deferred Lighting, MSAA

Further Reading

• Fabio Policarpo et al., Deferred Shading Tutorial, http://www710.univ-lyon1.fr/~jciehl/Public/educ/GAMA/2007/Deferred_Shading_Tutorial_SBGAMES2005.pdf

• NVIDIA SDK Deferred Shading Example: http://developer.download.nvidia.com/SDK/9.5/Samples/featured_samples.html-> Shows how to project a box around a point

light into clip space so that the surrounding rectangular can be calculated

Page 50: Paris Master Class 2011 - 01 Deferred Lighting, MSAA

References[Brennan] Chris Brennan, “Per-Pixel Fresnel Term”, Direct3D ShaderX – Vertex and PixelShader Tips and Tricks, Wordware, 2002, ISBN 1-55622-041-3[Engel] Wolfgang Engel, “Programming Vertex and Pixel Shaders,” pp. 123 – 127, Charles River Media, 2004, ISBN 1-58450-349-1[Hargreaves] Shawn Hargreaves, “Deferred Shading”, http://www.talula.demon.co.uk/DeferredShading.pdf[Hoffman] Naty Hoffman, “Deferred lighting approaches”, http://www.realtimerendering.com/blog/deferred-lighting-approaches/#more-94[Kaplanyan], Anton Kaplanyan, CryENGINE 3: reaching the speed of light, SIGGRAPH 2010, http://www.crytek.com/cryengine/presentations/CryENGINE3-reaching-the-speed-of-light [Lee] Mark Lee, “Resistance 2 Prelighting”, http://www.insomniacgames.com/tech/articles/0409/files/GDC09_Lee_Prelighting.pdf[Lobanchikov] Igor A. Lobanchikov, “ GSC Game World‘s S.T.A.L.K.E.R : Clear Sky – a showcase for Direct3D 10.0/1”, http://developer.amd.com/gpu_assets/01GDC09AD3DDStalkerClearSky210309.ppt[Mittring] Martin Mittring, “A bit more Deferred – Cry Engine 3”, http://www.slideshare.net/guest11b095/a-bit-more-deferred-cry-engine3[Moeller] Tomas Akenine-Moeller, Eric Haines, Naty Hoffman, “Real-TimeRendering,” AK Peters, 2008, ISBN 978-1-56881-424-7, pp. 214 – 215[Palestra] Christophe Balestra, Pål-Kristian Engstad, “The technology of Uncharted:Drake’s Fortune”, http://www.naughtydog.com/corporate/press/GDC%202008/UnchartedTechGDC2008.pdf[Persson] Emil Persson, “Deferred Shading 2”, http://www.humus.name/index.php?page=3D[Pettineo] Matt Pettineo, “Scintillating Snippets: Reconstructing Position From Depth”, http://mynameismjp.wordpress.com/2009/03/10/reconstructing-position-from-depth/[Placeres] Frank Puig Placeres, “Overcoming Deferred Shading Drawbacks,” pp. 115 – 130, ShaderX5[Shishkovtsov] Oles Shishkovtsov, “Making some use out of hardware multisampling”; http://oles-rants.blogspot.com/2008/08/making-some-use-out-of-hardware.html [Pranckevičius ] Aras Pranckevičius, “Compact Normal Storage for small G-Buffers”, http://aras-p.info/texts/CompactNormalStorage.html[Swoboda] Matt Swoboda, “Deferred Lighting and Post Processing on PLAYSTATION®3, http://research.scee.net/presentations[Thibieroz04] Nick Thibieroz, “Deferred Shading with Multiple-Render-Targets” pp. 251 – 269, ShaderX2 – Shader Programming Tips & Tricks with DirectX9 [Thibieroz] Nick Thibieroz, “Deferred Shading with Multisampling Anti-Aliasing in DirectX 10” , ShaderX7 – Advanced Rendering Techniques, pp. ??? - ??? [Tovey] Steven J. Tovey, Stephen McAuley, “Parallelized Light Pre-Pass Rendering withthe Cell Broadband EngineTM”, to appear in GPU Pro – Advanced Rendering Techniques,AK Peters, March 2010.

[Valient07] Michael Valient, “Deferred Rendering in Killzone 2,” www.guerilla-games.com/publications/dr_kz2_rsx_dev07.pdf

Page 51: Paris Master Class 2011 - 01 Deferred Lighting, MSAA

References [Thibieroz] Nick Thibieroz, “Deferred Shading with Multisampling Anti-Aliasing in DirectX 10” , ShaderX7 – Advanced Rendering Techniques, pp. 225- 245 [Tovey] Steven J. Tovey, Stephen McAuley, “Parallelized Light Pre-Pass Rendering with the Cell Broadband EngineTM”, to appear in GPU Pro – Advanced Rendering Techniques, AK Peters, March 2010.

[Valient07] Michael Valient, “Deferred Rendering in Killzone 2,” www.guerilla-games.com/publications/dr_kz2_rsx_dev07.pdf