
64,000 Rigid Body Simulations running on the GPU
View Post on My Blog
View Source on GitHub
Excerpt:
The collision calculations are being performed by a Unity Compute Shader. A Unity Command Buffer is used to dispatch the compute shader and the subsequent render commands. In my implementation, no per voxel data is transferred between the GPU and the CPU at runtime, and no C# code is executed at runtime. Voxel forces are modeled by 8 particles per cube - 1 per corner. Every frame, the Compute Shader computes and sums the particle forces to calculate every cubes’ position and velocity. Performance will likely be improved through research and optimization of the per particle Kernels within the Compute Shader. The compute shader’s implementation is based off of Takahiro Harada’s Chapter in GPU Gems 3: Real-Time Rigid Body Simulation on the GPU.
After the physics step, rendering takes place without copying the position and rotation data onto the CPU by using Unity’s CommandBuffer.DrawMeshInstancedIndirect. At the time of writing this API is brand new and seems to have some bugs. For example, I cannot seem to render anything using this method on a Mac. Nothing renders when the Camera’s clear mode is set to Solid Color (the image above is Skybox). Some of these bugs can be resolved by excluding rendering from the command buffer and rendering using Graphics.DrawMeshInstancedIndirect instead. Despite these bugs, I am very thankful to Unity for providing these APIs and empowering my creative development.