Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

94
Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Transcript of Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Page 1: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Visibility Optimization for Games

Sampo LappalainenLead ProgrammerUmbra Software Ltd.

Page 2: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Introduction

• Background in graphics programming• Hybrid Graphics, NVIDIA, Umbra Software• With Umbra since 2008• Graphics middleware for console and PC

games• Emphasis on visibility

Page 3: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Roadmap

1. Motivation2. Theory3. Practice4. Other applications5. Demo

Page 4: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

MOTIVATIONWhy is visibility optimization important?

Page 5: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Game World

Page 6: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Our Villain

Page 7: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Our Hero

Page 8: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Screen Shot

Page 9: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Game Worlds

• Game developers want to make impressive game worlds

• Hardware sets limits on what can and can’t be done.

• Game developers need to push the hardware to it’s limits.

Page 10: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Visibility Optimization

• The most effective way to gain performance in games.

• Two basic ways to do visibility optimization:– art and level design– technology

• Games use a mix of both.

Page 11: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Visibility Optimization by Level Design

• Artists design game worlds so that performance is acceptable.

• Can be done in numerous ways e.g.:– limiting view distance– limiting polygon or object count– modeling portals and cells

Page 12: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Visibility Optimization by Level Design

Page 13: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Visibility Optimization by Level Design

• Time consuming and usually boring work.• Sets huge limits on what can and cannot be

done.• May lead to monotonic level design.• Manual and non-recurring work.

Page 14: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Visibility Optimization by Technology

Page 15: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Visibility Optimization by Technology

Page 16: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Visibility Optimization by Technology

• Gains:– No time wasted on rendering objects that don’t

contribute to the output image (no state changes, no draw calls etc).

– AI, physics, game logic etc. can be done at lower accuracy (or skipped all together) for hidden objects.

Page 17: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

THEORYWalkthrough of the key concepts

Page 18: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Terminology

• Culling – removing hidden objects from rendering

• Target – object that can be hidden by others• Occluder – an object that blocks visibility • Rendering artifact – A non-intended glitch in

the output image

Page 19: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Metrics for comparison

• GPU cost• CPU cost• Overall frame time• Memory usage• Precomputation time• Manual work• Culling power

Page 20: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Backface culling

• Taken care of by the HW• Culling entire triangles

based on their winding• No need to render the

insides of an object

Page 21: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Depth buffering

• Taken care of by the HW• A two dimensional buffer for storing z-values

for each screen pixel• Before processing shaders for a pixel to be

rendered, test the z-value.• Allows drawing of unsorted geometry,

however sorting still greatly improves performance

Page 22: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Hierarchical depth buffering

• Replace depth buffer with a depth pyramid– Bottom of the pyramid: full-resolution depth buffer– Higher levels: smaller resolution depth buffers where a single pixel

represents the maximum z-value in a group of pixels in the below level

• Hierarchically rasterize the polygon starting from the highest level– If polygon is further than the recorded pixel, early exit– If polygon is closer, hierarchically test the lower levels– If the bottom of the pyramid is reached and the polygon is still closer,

propagate the value up the pyramid

Page 23: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Spatial hierarchies

• Enabled culling large portions of the game world with a single quick test

• Dynamic objects can be moved in the hierarchy runtime

• BSP-tree, kd-tree

Page 24: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Spatial hierarchies

Page 25: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

View frustum culling

• Culling objects that are outside the camera view cone

• Test using object bounds• Tremendous speed-up using an hierarchy

Page 26: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

View Frustum Culling

Page 27: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

View Frustum Culling

Page 28: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Potentially Visible Set - PVS

• A data structure that defines from-region-visibility for a scene• Computed in pre-process• Scene is divided into Cells• Compute a bit matrix that lists all the visible objects for each

cell• Runtime a simple matrix lookup• How to find a good sub-division for a scene?• Cannot handle dynamic occluders• Target volume: extension to handle dynamic targets

Page 29: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Portals

• Place portals in the scene that connect the cells to form a portal graph

• In runtime, find the portals of the current cell that are in the frustum

• Traverse through all found portals to the adjacent cells and find all portals that are visible to the camera through the original portal

• Same limitations with dynamic objects as with PVS systems

Page 30: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Rasterization-based

• Render occluder geometry into a software coverage buffer

• Test visibility using test geometry• Use temporal coherence to determine the

initial set to be rendered• Handles both dynamic targets and occluders

as long as they have occluder geometry

Page 31: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Testing from coverage buffer

Page 32: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Testing from coverage buffer

Page 33: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Testing from coverage buffer

Page 34: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Testing from coverage buffer

Page 35: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Testing from coverage buffer

Page 36: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Testing from coverage buffer

Page 37: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Testing from coverage buffer

Page 38: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Testing from coverage buffer

Page 39: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Occlusion Queries

• Supported by GPUs since 2001.• GPU answers the question: “how many pixels would have

been visible if this object would have been rendered”?• Instead of rasterizing your own depth buffer, use the GPU

depth buffer instead• Normally the query is done using bounding volumes (effective

but not necessary).• No need for artist generated occluder geometry• GPU-CPU synchronization needed

Page 40: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Occlusion Queries

• Determine the set of visible objects against the actual rendered geometry:– all pixels can be used as occluding material!

Page 41: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Using Occlusion Queries

• Occlusion queries are a really powerful tool for visibility optimization.

• Like all other features of the GPU occlusion queries can be used ineffectively.

• Special tricks are needed to get the most out of occlusion queries.

Page 42: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Issuing Occlusion Queries

disableColorWrite();disableDepthWrite();startQueryCounter();renderObjectBounds();stopQueryCounter();

enableColorWrite();enableDepthWrite();

if (query->getResult() > 0)renderObject();

Page 43: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

CPU-GPU synchronization

• With normal draw calls the CPU issues a command to the GPU and can continue processing as usual (Parallel processing).

• With occlusion queries the CPU needs to get query results back to be able to know if the object was visible or not.

• The CPU needs to wait for the query results to be available.

• No parallel processing (which is really bad).

Page 44: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Issuing Occlusion Queries

Page 45: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Issuing Occlusion Queries

Page 46: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Issuing Occlusion Queries

Page 47: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Issuing Occlusion Queries

• Fortunately GPU design has a solution for this problem.

• GPUs can store multiple occlusion query results.

• Occlusion queries can be batched.• Some GPUs have a limit on how many query

results can be stored.

Page 48: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Batching Occlusion QueriesdisableColorWrite();disableDepthWrite();

for (each query){

startQueryCounter();renderObjectBounds();stopQueryCounter();

}

enableDepthWrite();enableColorWrite();

for (each query){

if (query->getResult() > 0)renderObject();

}

Page 49: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Batching Occlusion Queries

Page 50: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Latent Occlusion Queries

• Some stalls may be introduced between frames.• The last query result needs to be read back before

continuing.• Avoid GPU stalls by using the query results from the

previous frame.• Read back the query results at the beginning of each

frame.• Sounds like a perfect solution?

Page 51: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Latent Occlusion Queries

Page 52: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Latent Occlusion Queries

• There are downsides to this.• Visible popping artifacts when objects come

visible.• If the camera is moving slowly and FPS is

good, no problem.• When multiple objects become visible FPS

typically drops (there’s a lot more to render)• For example when a door is opened.

Page 53: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Latent Occlusion Queries

Page 54: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Latent Occlusion Queries

Page 55: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Latent Occlusion Queries

Page 56: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Latent Occlusion Queries

• Queries done to hierarchy nodes produce even larger artifacts

• Growing bounds helps, but is difficult to get to work with hierarchical queries

• The stall in using occlusion query results on the same frame may be as short as 0.1ms (on XBOX 360)

• In this a price developers are ready to pay for artifact free occlusion culling?

Page 57: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Parallelism

• Most gaming platforms today come with more than one CPU

• Using the same algorithm for multiple cameras (splitscreen, AI bots, light sources)

• Tile-based rasterization• Parallel data structure traverse

Page 58: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

PRACTICEWhat kind of systems have really been used?

Page 59: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Binary Space Partitioning

• As made famous by Doom and the Quake series• A tree data structure for representing the scene• Gordon and Chen 1991 paper used in Doom (

http://www.rothschild.haifa.ac.il/~gordon/ftb-bsp.pdf)

• Teller’s 1992 PhD thesis used in Quake (http://people.csail.mit.edu/seth/pubs/pubs.html)

Page 60: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Binary space partitioning

• Before Doom BSP’s were used to do sorting for the painter’s algorithm (back-to-front)

• Painter’s algorithm is too slow for large scenes• Solution: change the order to front-to-back

and keep track on which pixels have been drawn

• Quake introduced a pre-process step for computing a PVS based on the BSP model

Page 61: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Umbra 1

• Used in Star Wars Galaxies, EverQuest 2, Age of Conan, Kingdom Heroes 2, Tian Xia 2

• A data structure that supports dynamic and static visibility

• Software rasterizer and occlusion queries supported

Page 62: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Umbra 1

• Database– Spatial bounding volume hierarchy– User updates

• Visibility traverse– Input: camera parameters– Output: visible object set– Hierarchical visibility testing: a single query can

hide large parts of the scene

Page 63: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Hierarchical Culling

• In typical game scenes most of the scene is hidden at any given point of view

• Problem:– the size of the whole scene effects performance

(input sensitive system).

• Only the visible objects are supposed to effect performance (output sensitive system).

Page 64: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Hierarchical Culling

Page 65: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Hierarchical Culling

• Solution:– build a spatial hierarchy for the objects in the

scene

• Culling hidden parts of the scene in constant time

• Occlude groups of objects: if a hierarchy node is hidden all nodes below it are also hidden

Page 66: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Hierarchy Traversal

• Traverse the hierarchy to determine visibility• Use temporal coherency• On first frame, start from the root• Store nodes where traversal ended and start

traversing them on the next frame• Nodes form a visibility barrier

Page 67: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Hierarchy Traversal

Page 68: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Hierarchy Traversal

Page 69: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Hierarchy Traversal

Page 70: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Dynamic Objects

• Object geometry may change (e.g. due to LODing).• Objects may move• If object geometry changes it may not fit into its old

bounds• Move the object upwards in the hierarchy so that the

bounds can fit inside a node• Push the object back down once there is idle time

Page 71: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Dynamic Objects

• If the object moves temporal bounding volumes can be used.

• Use history info to predict the object movement.

• The TBV doesn’t have to be updated every frame.

Page 72: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Dynamic Objects

Page 73: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Dynamic Objects

Page 74: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Umbra 2

• Multi-core version of the previous tech• Used in e.g. Mass Effect 2, Dragon Age series,

Alan Wake

Page 75: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Multi-core culling

• Two subtasks: rendering and visibility traversal

• Rendering issues rendering calls and occlusion queries.

• Visibility processing takes care of hierarchy processing and high level culling (e.g. vf culling).

Page 76: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Multi-core culling

• Game tread needs to do updates before our visibility thread can continue (camera and object updates)

• Visibility thread updates the hierarchy• After update the hierarchy can be traversed

Page 77: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Multi-core culling

Page 78: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Multi-core culling

• While the visibility thread is idle it can update the hierarchy:– lazy hierarchy building– collapsing nodes– visibility barrier updates– moving dynamic objects down etc.

Page 79: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Umbra 3

• Used by Unity 3D, Secret Studio• Collection of visibility algorithms– Umbra 1-2 feature sets– Automatic portal generation in pre-process– CPU rasterization and ray-tracing based portal

culling algorithms– PVS culling for low end systems

Page 80: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Umbra 3

• Uses real geometry, no need for artists to create occluder geometry

• Support for streaming, distance queries, intersection queries

Page 81: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Automatic portal generation

• Works with both outdoor and indoor scenes• Conservative occlusion• The output is a graph where the nodes are

cells and the edges are the portals• Optionally a PVS can be computed• Incremental updates

Page 82: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Umbra 3 recursive portal culling

• Recursive traverse of the portal graph from the camera view point, ray tracing

• Very accurate culling results• Too slow for whole scene culling, currently

used for reference and for dynamic object culling

Page 83: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.
Page 84: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Umbra 3 optimized portal culling

• Rasterize the portals into a coverage buffer• Fast enough for even outdoor scenes• In some cases over-estimates the visible set

Page 85: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.
Page 86: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Umbra 3 PVS culling

• Extremely fast– Needed for low end systems such as smart phones– Can be used to determine visibility for e.g.

hunderds of AI bots

• The longer time spent computing, the more accurate the result

Page 87: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Killzone 3

• See ”Practical occlusion culling for PS3”: http://gdcvault.com/play/1014356/Practical-Occlusion-Culling-on

• Solution implemented spesifically for PlayStation 3• Rasterizes a 720p tiled depth buffer on the SPU’s• Performs occlusion tests to a downsampled depth

buffer using object bounds• Occluder mesh selection done by artists

Page 88: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Battlefield 3

• See ”Culling the Battlefield”: http://publications.dice.se/attachments/CullingTheBattlefield.pdf

• A cross-platform (XBOX360, PS3, PC) solution• SIMD optimized frustum culling• Software rasterizer for occlusion culling done

to a 256x116 depth buffer• Occluder geometry hand made by artists

Page 89: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

OTHER APPLICATIONSWhat else can I use it for?

Page 90: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Lighting & shadows

• When applied from a light sources point of view a visibility algorithm can be used for finding shadow casters

• ”Shadow Caster Occlusion Culling for Efficient Shadow mapping” (http://www.cg.tuwien.ac.at/research/publications/2011/bittner-2011-scc/bittner-2011-scc-paper.pdf)

Page 91: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Streaming

• Large game worlds have so much content that it cannot fit in the memory of a gaming platform

• Loading between zones takes away immersion• A from-region visibility algorithm can be used

to do visibility-based streaming over the network or from a storage media

Page 92: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

AI

• A visibility algorithm can be used to drive AI logic

• Data structures used in visibility determination can be modified to be used for distance or intersection testing

Page 93: Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.

Sound occlusion

• Distance and intersection tests can be used to simulate the behaviour of sound

• Precomputing visibility and audio have a lot of overlap and make for an interesting field of study