Indirect Rendering With Compute Shaders
An example of rendering numerous instances in Unity3D using Compute shaders for culling and LOD'ing and Graphics.DrawMeshInstancedIndirect to draw.
- Tested on Metal (Mac) and D3D11.
- Quasirandom squences to place the instances
- Draw things using Graphics.DrawMeshInstancedIndirect
- GPU Sorting with Bitonic sorting
- Compute shader: Frustum and shadow caster culling
- Compute shader: Occlusion culling with HierarchicalZBuffer
- Compute shader: Detail (Screen Size) Culling
- Compute shader: LOD objects using the distance from camera to object
- Simple shadow mode: Only use one shadow mesh for each instance type to lower setpass calls
- "_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 contains the depth texture and shadow map which are used when doing frustum and 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
- 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)
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.