An example of drawing numerous instances in Unity3D using Compute shaders and Graphics.DrawMeshInstancedIndirect.
This project has only been tested on a Macbook Pro using Metal. When using DirectX, it is very likely you need to add the arguments buffer into the instance rendering shader and add the offset to unity_InstanceID to get the correct instance.
- Quasirandom squences to place the instances
- Use compute buffers and compute shaders
- Draw things using Graphics.DrawMeshInstancedIndirect
- GPU Sorting with Bitonic sorting
- Compute shader: Frustum culling
- Compute shader: Occlusion culling with HierarchicalZBuffer
- Compute shader: LOD objects using the distance from camera to object
- Extending camera frustum towards light to include shadow casting objects
- Use LOD02 mesh for all shadows
- "_Example.cs" script on the "Example" game object:
- Creates the instance data and sends it to the Indirect Renderer class.
- "IndirectRenderer.cs" on the "MainCamera" game object:
- Is the main class in the project. It initializes all the buffers and compute shaders and then dispatches the compute shaders and draws the objects when Unity calls the PreCull() function.
- "HiZBuffer.cs" script on the "OccluderCamera" game object:
- Creates a hierarchial Z-Buffer texture that is used when doing occlusion culling.
- Improve the instance sorting:
- On my Macbook pro (mid 2014) sorting takes approx 50% of the GPU time
- The GPU sorting must support instance numbers that are not in the non power of two (right now I'm padding the data to become POT)
- Find a better performant approach for the CPU Sorting
- Try to make the compute buffers smaller in size. Ex: Pack the three floats, used for bounds size, into one uint.
- Create the Hi-Z Texture with compute shaders
- Try out Raster Occlusion instead of Hi-Z (See "NVidia Siggraph 2014" & "Github - nvpro-samples" below)
- Try and profile structure buffers with structs divisible by 128 bits (sizeof float) => https://developer.nvidia.com/content/understanding-structured-buffer-performance
The Unreasonable Effectiveness of Quasirandom Sequences
Kostas Anagnostou - GPU Driven Rendering Experiments
Kostas Anagnostou - Experiments in GPU-based occlusion culling
Kostas Anagnostou - Experiments in GPU-based occlusion culling part 2
Sakib Saikia - Going Indirect on UE3
RasterGrid - Hierarchical-Z map based occlusion culling
StackOverflow - Hierachical Z-Buffering for occlusion culling
bazhenovc - GPU Driven Occlusion Culling in Life is Feudal
NVIDIA - Siggraph 2014 - Scene Rendering Techniques
Github - nvpro-samples/gl_occlusion_culling
GPU Gems 2 - Hardware Occlusion Queries Made Useful
ARM Developer Center - hiz_cull.cs
L. Spiro - Tightly Culling Shadow Casters for Directional Lights (Part 1)
L. Spiro - Tightly Culling Shadow Casters for Directional Lights (Part 2)
nonoptimalrobot - Shadow Volume Culling
Copyright (C) 2017-2018 Elvar Orn Unnthorsson
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.