Visibility Optimization for Games

Download Visibility Optimization for Games

Post on 13-Jan-2015




1 download

Embed Size (px)


A presentation held by Umbra Software lead programmer Sampo Lappalainen at China Game Developer Conference 2011.


<ul><li> 1. Visibility Optimization for Games Sampo Lappalainen Lead Programmer Umbra Software Ltd.</li></ul> <p> 2. Introduction </p> <ul><li>Background in graphics programming </li></ul> <ul><li>Hybrid Graphics, NVIDIA, Umbra Software </li></ul> <ul><li>With Umbra since 2008 </li></ul> <ul><li>Graphics middleware for console and PC games </li></ul> <ul><li>Emphasis on visibility </li></ul> <p> 3. Roadmap </p> <ul><li>Motivation </li></ul> <ul><li>Theory </li></ul> <ul><li>Practice </li></ul> <ul><li>Other applications </li></ul> <ul><li>Demo </li></ul> <p> 4. MOTIVATION </p> <ul><li>Why is visibility optimization important? </li></ul> <p> 5. Game World 6. Our Villain 7. Our Hero 8. Screen Shot 9. Game Worlds </p> <ul><li>Game developers want to make impressive game worlds </li></ul> <ul><li>Hardware sets limits on what can and cant be done. </li></ul> <ul><li>Game developers need to push the hardware to its limits. </li></ul> <p> 10. Visibility Optimization </p> <ul><li>The most effective way to gain performance in games. </li></ul> <ul><li>Two basic ways to do visibility optimization: </li></ul> <ul><li><ul><li>art and level design </li></ul></li></ul> <ul><li><ul><li>technology </li></ul></li></ul> <ul><li>Games use a mix of both. </li></ul> <p> 11. Visibility Optimization by Level Design </p> <ul><li>Artists design game worlds so that performance is acceptable. </li></ul> <ul><li>Can be done in numerous ways e.g.: </li></ul> <ul><li><ul><li>limiting view distance </li></ul></li></ul> <ul><li><ul><li>limiting polygon or object count </li></ul></li></ul> <ul><li><ul><li>modeling portals and cells </li></ul></li></ul> <p> 12. Visibility Optimization by Level Design 13. Visibility Optimization by Level Design </p> <ul><li>Time consuming and usually boring work. </li></ul> <ul><li>Sets huge limits on what can and cannot be done. </li></ul> <ul><li>May lead to monotonic level design. </li></ul> <ul><li>Manual and non-recurring work. </li></ul> <p> 14. Visibility Optimization by Technology 15. Visibility Optimization by Technology 16. Visibility Optimization by Technology </p> <ul><li>Gains: </li></ul> <ul><li><ul><li>No time wasted on rendering objects that dont contribute to the output image (no state changes, no draw calls etc). </li></ul></li></ul> <ul><li><ul><li>AI, physics, game logic etc. can be done at lower accuracy (or skipped all together) for hidden objects. </li></ul></li></ul> <p> 17. THEORY </p> <ul><li>Walkthrough of the key concepts </li></ul> <p> 18. Terminology </p> <ul><li>Culling removing hidden objects from rendering </li></ul> <ul><li>Target object that can be hidden by others </li></ul> <ul><li>Occluder an object that blocks visibility</li></ul> <ul><li>Rendering artifact A non-intended glitch in the output image </li></ul> <p> 19. Metrics for comparison </p> <ul><li>GPU cost </li></ul> <ul><li>CPU cost </li></ul> <ul><li>Overall frame time </li></ul> <ul><li>Memory usage </li></ul> <ul><li>Precomputation time </li></ul> <ul><li>Manual work </li></ul> <ul><li>Culling power </li></ul> <p> 20. Backface culling </p> <ul><li>Taken care of by the HW </li></ul> <ul><li>Culling entire triangles based on their winding </li></ul> <ul><li>No need to render the insides of an object </li></ul> <p> 21. Depth buffering </p> <ul><li>Taken care of by the HW </li></ul> <ul><li>A two dimensional buffer for storing z-values for each screen pixel </li></ul> <ul><li>Before processing shaders for a pixel to be rendered, test the z-value. </li></ul> <ul><li>Allows drawing of unsorted geometry, however sorting still greatly improves performance </li></ul> <p> 22. Hierarchicaldepth buffering </p> <ul><li>Replace depth buffer with a depthpyramid </li></ul> <ul><li><ul><li>Bottom of the pyramid: full-resolution depth buffer </li></ul></li></ul> <ul><li><ul><li>Higher levels: smaller resolution depth buffers where a single pixel represents the maximum z-value in a group of pixels in the below level </li></ul></li></ul> <ul><li>Hierarchically rasterize the polygon starting from the highest level </li></ul> <ul><li><ul><li>If polygon is further than the recorded pixel, early exit </li></ul></li></ul> <ul><li><ul><li>If polygon is closer, hierarchically test the lower levels </li></ul></li></ul> <ul><li><ul><li>If the bottom of the pyramid is reached and the polygon is still closer, propagate the value up the pyramid </li></ul></li></ul> <p> 23. Spatial hierarchies </p> <ul><li>Enabled culling large portions of the game world with a single quick test </li></ul> <ul><li>Dynamic objects can be moved in the hierarchy runtime </li></ul> <ul><li>BSP-tree, kd-tree </li></ul> <p> 24. Spatialhierarchies 25. View frustum culling </p> <ul><li>Culling objects that are outside the camera view cone </li></ul> <ul><li>Test using object bounds </li></ul> <ul><li>Tremendous speed-up using an hierarchy </li></ul> <p> 26. View Frustum Culling 27. View Frustum Culling 28. Potentially Visible Set - PVS </p> <ul><li>A data structure that definesfrom-region-visibilityfor a scene </li></ul> <ul><li>Computed in pre-process </li></ul> <ul><li>Scene is divided intoCells </li></ul> <ul><li>Compute a bit matrix that lists all the visible objects for each cell </li></ul> <ul><li>Runtime a simple matrix lookup </li></ul> <ul><li>How to find a good sub-division for a scene? </li></ul> <ul><li>Cannot handle dynamic occluders </li></ul> <ul><li>Target volume: extension to handle dynamic targets </li></ul> <p> 29. Portals </p> <ul><li>Place portals in the scene that connect the cells to form aportal graph </li></ul> <ul><li>In runtime, find the portals of the current cell that are in the frustum </li></ul> <ul><li>Traverse through all found portals to the adjacent cells and find all portals that are visible to the camera through the original portal </li></ul> <ul><li>Same limitations with dynamic objects as with PVS systems </li></ul> <p> 30. Rasterization-based </p> <ul><li>Renderoccluder geometryinto a software coverage buffer </li></ul> <ul><li>Test visibility usingtest geometry </li></ul> <ul><li>Usetemporal coherenceto determine the initial set to be rendered </li></ul> <ul><li>Handles both dynamic targets and occluders as long as they have occluder geometry </li></ul> <p> 31. Testing from coverage buffer 32. Testing from coverage buffer 33. Testing from coverage buffer 34. Testing from coverage buffer 35. Testing from coverage buffer 36. Testing from coverage buffer 37. Testing from coverage buffer 38. Testing from coverage buffer 39. Occlusion Queries </p> <ul><li>Supported by GPUs since 2001. </li></ul> <ul><li>GPU answers the question: how many pixels would have been visible if this object would have been rendered? </li></ul> <ul><li>Instead of rasterizing your own depth buffer, use the GPU depth buffer instead </li></ul> <ul><li>Normally the query is done using bounding volumes (effective but not necessary). </li></ul> <ul><li>No need for artist generated occluder geometry </li></ul> <ul><li>GPU-CPU synchronization needed </li></ul> <p> 40. Occlusion Queries </p> <ul><li>Determine the set of visible objects against the actual rendered geometry: </li></ul> <ul><li><ul><li>all pixels can be used as occluding material! </li></ul></li></ul> <p> 41. Using Occlusion Queries </p> <ul><li>Occlusion queries are a really powerful tool for visibility optimization. </li></ul> <ul><li>Like all other features of the GPU occlusion queries can be used ineffectively. </li></ul> <ul><li>Special tricks are needed to get the most out of occlusion queries. </li></ul> <p> 42. Issuing Occlusion Queries disableColorWrite(); disableDepthWrite(); startQueryCounter(); renderObjectBounds(); stopQueryCounter(); enableColorWrite(); enableDepthWrite(); if (query-&gt;getResult() &gt; 0) renderObject(); 43. CPU-GPU synchronization </p> <ul><li>With normal draw calls the CPU issues a command to the GPU and can continue processing as usual (Parallel processing). </li></ul> <ul><li>With occlusion queries the CPU needs to get query results back to be able to know if the object was visible or not. </li></ul> <ul><li>The CPU needs to wait for the query results to be available. </li></ul> <ul><li>No parallel processing (which is really bad). </li></ul> <p> 44. Issuing Occlusion Queries 45. Issuing Occlusion Queries 46. Issuing Occlusion Queries 47. Issuing Occlusion Queries </p> <ul><li>Fortunately GPU design has a solution for this problem. </li></ul> <ul><li>GPUs can store multiple occlusion query results. </li></ul> <ul><li>Occlusion queries can be batched. </li></ul> <ul><li>Some GPUs have a limit on how many query results can be stored. </li></ul> <p> 48. Batching Occlusion Queries disableColorWrite(); disableDepthWrite(); for (each query) { startQueryCounter(); renderObjectBounds(); stopQueryCounter(); } enableDepthWrite(); enableColorWrite(); for (each query) { if (query-&gt;getResult() &gt; 0) renderObject(); } 49. Batching Occlusion Queries 50. Latent Occlusion Queries </p> <ul><li>Some stalls may be introduced between frames. </li></ul> <ul><li>The last query result needs to be read back before continuing. </li></ul> <ul><li>Avoid GPU stalls by using the query results from the previous frame. </li></ul> <ul><li>Read back the query results at the beginning of each frame. </li></ul> <ul><li>Sounds like a perfect solution? </li></ul> <p> 51. Latent Occlusion Queries 52. Latent Occlusion Queries </p> <ul><li>There are downsides to this. </li></ul> <ul><li>Visible popping artifacts when objects come visible. </li></ul> <ul><li>If the camera is moving slowly and FPS is good, no problem. </li></ul> <ul><li>When multiple objects become visible FPS typically drops (theres a lot more to render) </li></ul> <ul><li>For example when a door is opened. </li></ul> <p> 53. Latent Occlusion Queries 54. Latent Occlusion Queries 55. Latent Occlusion Queries 56. Latent Occlusion Queries </p> <ul><li>Queries done to hierarchy nodes produce even larger artifacts </li></ul> <ul><li>Growing bounds helps, but is difficult to get to work with hierarchical queries </li></ul> <ul><li>The stall in using occlusion query results on the same frame may be as short as 0.1ms (on XBOX 360) </li></ul> <ul><li>In this a price developers are ready to pay for artifact free occlusion culling? </li></ul> <p> 57. Parallelism </p> <ul><li>Most gaming platforms today come with more than one CPU </li></ul> <ul><li>Using the same algorithm for multiple cameras (splitscreen, AI bots, light sources) </li></ul> <ul><li>Tile-based rasterization </li></ul> <ul><li>Parallel data structure traverse </li></ul> <p> 58. PRACTICE </p> <ul><li>What kind of systems have really been used? </li></ul> <p> 59. Binary Space Partitioning </p> <ul><li>As made famous by Doom and the Quake series </li></ul> <ul><li>A tree data structure for representing the scene </li></ul> <ul><li>Gordon and Chen 1991 paper used in Doom ( ) </li></ul> <ul><li>Tellers 1992 PhD thesis used in Quake ( ) </li></ul> <p> 60. Binary space partitioning </p> <ul><li>Before Doom BSPs were used to do sorting for the painters algorithm (back-to-front) </li></ul> <ul><li>Painters algorithm is too slow for large scenes </li></ul> <ul><li>Solution: change the order to front-to-back and keep track on which pixels have been drawn </li></ul> <ul><li>Quake introduced a pre-process step for computing a PVS based on the BSP model </li></ul> <p> 61. Umbra 1 </p> <ul><li>Used in Star Wars Galaxies, EverQuest 2, Age of Conan, Kingdom Heroes 2, Tian Xia 2 </li></ul> <ul><li>A data structure that supports dynamic and static visibility </li></ul> <ul><li>Software rasterizer and occlusion queries supported </li></ul> <p> 62. Umbra 1 </p> <ul><li>Database </li></ul> <ul><li><ul><li>Spatial bounding volume hierarchy </li></ul></li></ul> <ul><li><ul><li>User updates </li></ul></li></ul> <ul><li>Visibility traverse </li></ul> <ul><li><ul><li>Input: camera parameters </li></ul></li></ul> <ul><li><ul><li>Output: visible object set </li></ul></li></ul> <ul><li><ul><li>Hierarchical visibility testing: a single query can hide large parts of the scene </li></ul></li></ul> <p> 63. Hierarchical Culling </p> <ul><li>In typical game scenes most of the scene is hidden at any given point of view </li></ul> <ul><li>Problem: </li></ul> <ul><li><ul><li>the size of the whole scene effects performance ( input sensitive system ). </li></ul></li></ul> <ul><li>Only the visible objects are supposed to effect performance ( output sensitive system ). </li></ul> <p> 64. Hierarchical Culling 65. Hierarchical Culling </p> <ul><li>Solution: </li></ul> <ul><li><ul><li>build a spatial hierarchy for the objects in the scene </li></ul></li></ul> <ul><li>Culling hidden parts of the scene in constant time </li></ul> <ul><li>Occlude groups of objects: if a hierarchy node is hidden all nodes below it are also hidden </li></ul> <p> 66. Hierarchy Traversal </p> <ul><li>Traverse the hierarchy to determine visibility </li></ul> <ul><li>Use temporal coherency </li></ul> <ul><li>On first frame, start from the root </li></ul> <ul><li>Store nodes where traversal ended and start traversing them on the next frame </li></ul> <ul><li>Nodes form a visibility barrier </li></ul> <p> 67. Hierarchy Traversal 68. Hierarchy Traversal 69. Hierarchy Traversal 70. Dynamic Objects </p> <ul><li>Object geometry may change (e.g. due to LODing). </li></ul> <ul><li>Objects may move </li></ul> <ul><li>If object geometry changes it may not fit into its old bounds </li></ul> <ul><li>Move the object upwards in the hierarchy so that the bounds can fit inside a node </li></ul> <ul><li>Push the object back down once there is idle time </li></ul> <p> 71. Dynamic Objects </p> <ul><li>If the object moves temporal bounding volumes can be used. </li></ul> <ul><li>Use history info to predict the object movement. </li></ul> <ul><li>The TBV doesnt have to be updated every frame. </li></ul> <p> 72. Dynamic Objects 73. Dynamic Objects 74. Umbra 2 </p> <ul><li>Multi-core version of the previous tech </li></ul> <ul><li>Used in e.g. Mass Effect 2, Dragon Age series, Alan Wake </li></ul> <p> 75. Multi-core culling </p> <ul><li>Two subtasks: rendering and visibility traversal </li></ul> <ul><li>Rendering issues rendering calls and occlusion queries. </li></ul> <ul><li>Visibility processing takes care of hierarchy processing and high level culling (e.g. vf culling). </li></ul> <p> 76. Multi-core culling </p> <ul><li>Game tread needs to do updates before our visibility thread can continue (camera and object updates) </li></ul> <ul><li>Visibility thread updates the hierarchy </li></ul> <ul><li>After update the hierarchy can be traversed </li></ul> <p> 77. Multi-core culling 78. Multi-core culling </p> <ul><li>While the visibility thread is idle it can update the hierarchy: </li></ul> <ul><li><ul><li>lazy hierarchy building </li></ul></li></ul> <ul><li><ul><li>collapsing nodes </li></ul></li></ul> <ul><li><ul><li>visibility barrier updates </li></ul></li></ul> <ul><li><ul><li>moving dynamic objects down etc. </li></ul></li></ul> <p> 79. Umbra 3 </p> <ul><li>Used by Unity 3D,Secret Studio </li></ul> <ul><li>Collection of visibility algorithms </li></ul> <ul><li><ul><li>Umbra 1-2 feature sets </li></ul></li></ul> <ul><li><ul><li>Automatic portal generation in pre-process </li></ul></li></ul> <ul><li><ul><li>CPU rasterization and ray-tracing based portal culling algorithms </li></ul></li></ul> <ul><li><ul><li>PVS culling for low end systems </li></ul></li></ul> <p> 80. Umbra 3 </p> <ul><li>Uses real geometry, no need for artists to create occluder geometry </li></ul> <ul><li>Support for streaming, distance queries, intersection queries </li></ul> <p> 81. Automatic portal generation </p> <ul><li>Works with both outdoor and indoor scenes </li></ul> <ul><li>Conservative occlusion </li></ul> <ul><li>The output is a graph where the nodes are cells and the edges are the portals </li></ul> <ul><li>Optionally a PVS can be computed </li></ul> <ul><li>Incremental updates </li></ul> <p> 82. Umbra 3 recursive portal culling </p> <ul><li>Recursive traverse of the portal graph from the camera view point, ray tracing </li></ul> <ul><li>Very accurate culling results </li></ul> <ul><li>Too slow for whole scene culling, currently used for reference and for dynamic object culling </li></ul> <p> 83. 84. Umbra 3 optimized portal culling </p> <ul><li>Rasterize the portals into a coverage buffer </li></ul> <ul><li>Fast enough for even outdoor scenes </li></ul> <ul><li>In some cases over-estimates the visible set </li></ul> <p> 85. 86. Umbra 3 PVS culling </p> <ul><li>Extremely fast </li></ul> <ul><li><ul><li>Needed for low end systems such as smart phones </li></ul></li></ul> <ul><li><ul><li>Can be used to determine visibility for e.g. hunderds of AI bots </li></ul></li></ul> <ul><li>The longer time spent computing, the more accurate the result </li></ul> <p> 87. Killzone 3 </p> <ul><li>See Practical occlusion culling for PS3: </li></ul> <ul><li>Solution implemented spesifically for PlayStation 3 </li></ul> <ul><li>Rasterizes a 720p tiled depth buffer on the SPUs </li></ul> <ul><li>Performs occlusion tests to a downsampled depth buffer using object bounds </li></ul> <ul><li>Occluder mesh selection done by artists </li></ul> <p> 88. Battlefield 3 </p> <ul><li>See Culling the Battlefield: CullingTheBattlefield .pdf </li></ul> <ul><li>A cross-platform (XBOX360, PS3, PC) solution </li></ul> <ul><li>SIMD optimized frustum culling </li></ul> <ul><li>Software rasterizer for occlusion culling done to a 256x116depth buffer </li></ul> <ul><li>Occluder geometry hand made by artists </li></ul> <p> 89. OTHER APPLICATIONS </p> <ul><li>What else can I use it for? </li></ul> <p> 90. Lighting &amp; shadows </p> <ul><li>When applied from a light sources point of view a visibility algorithm can be used for finding shadow casters </li></ul> <ul><li> Shadow Caster Occlusion Culling for Efficient Shadow mapping ( ) </li></ul> <p> 91. Streaming </p> <ul><li>Large game worlds have so much content that it cannot fit in the memory of a gaming platform </li></ul> <ul><li>Loading between zones takes away immersion </li></ul> <ul><li>A from-region visibility algorithm can be used to do visibility-based streaming over the network or from a storage media </li></ul> <p> 92. AI </p> <ul><li>A visibility algorithm can be used to drive AI logic </li></ul> <ul><li>Data structures used in visibility determination can be modified to be used for distance or intersection testing </li></ul> <p> 93. Sound occlusion </p> <ul><li>Distance and intersection tests can be used to simulate the behaviour of sound </li></ul> <ul><li>Precomputing visibility and audio have a lot of overlap and make for an interesting field of study </li></ul> <p> 94. FIN </p> <ul><li>Sampo Lappalainen </li></ul> <ul><li>[email_address] </li></ul> <ul><li> </li></ul>