#gamedev #shaders #wgsl
In this video I decided to push the performance limits of my laptop and try to code some Compute Shaders to simulate extremely large amounts of units for a battle simulator. For those of you who don't know, Compute Shaders leverage thread parallelism and GPU acceleration to perform tasks which would usually be done on the CPU, on the GPU in order to speed up tasks. Although they usually wouldn't be used in a battle simulator like I'm doing in this video, compute shaders can very much so be used to simulate battles. In the end, we were able to simulate 1 million units at 60 FPS due to various optimizations which I wrote both on the CPU software side of the game, as well as optimizations which I made within the actual GPU compute side of the game. The most important optimization which I made was spatial hashing, which reduced the amount of checks which we need to perform between the units from a bruteforce approach to a much more manageable partitioned grid approach. Since all of the operations in compute shaders need to be parallelized at the risk of not fully utilizing the GPU, we need to implement spatial partitioning in a more complicated way than it would be on the CPU. Particularly, I also had to learn a parallelized sorting algorithm called Bitonic Merge Sort in order to get the Spatial Partioning system to operate correctly. Even with spatial partitioning, we can increase the framerate even further by lowering the physics timestep of the game and instead just interpolating between the updates. Now that was a broad overview of this entire video, related links will be below and the code is also on my github which I will also be linking below.
Using GPU Compute Shaders to Simulate HUGE Armies
Join my Discord!
https://discord.gg/cRba8sDqAy
Timestamps
0:00 - Intro
1:06 - Bevy Compute Conway's Game of Life Example
1:44 - Parallelism and different types of Shaders on the GPU
2:50 - How Compute Shaders Retain Data(Buffers and Images)
3:23 - Basic Compute Shader Pipeline Setup
5:09 - How do Compute Shaders actually... "Compute"
7:21 - Brute Force Compute Boids Simulation
9:45 - Why we should improve on Brute Force with Spatial Partitioning
10:39 - Spatial Partitioning CPU(Software) explanation
11:39 - Spatial Partitioning GPU(Hardware) explanation
14:16 - Bitonic Merge Sort Introduction
15:52 - Bitonic Sequences
16:42 - Bitonic Merge Sort Explanation
17:30 - Bitonic Merge Explanation
20:40 - Spatial Partitioning Implementation
22:35 - Frame Procrastination(Slow Fixed Update Interpolation)
24:20 - Battle Simulator Logic
26:30 - Outro
Links:
Source Code: https://github.com/Biped-Potato/compute_battle_sim
Fix your timestep(Gaffer on Games): https://gafferongames.com/post/fix_your_timestep/
Helpful article about WGSL compute shaders: https://webgpufundamentals.org/webgpu/lessons/webgpu-compute-shaders.html
Sebastian Lague Fluid Simulation Video(Where I sort of deviated from his spatial hashing algo):
https://www.youtube.com/watch?v=rSKMYc1CQHE
Music Used
Music used in this video:
♪ Corbyn Kites - Instant Crush
Link:
https://www.youtube.com/watch?v=vad0YbV9wm4
♪ Evan King(Context Sensitive) - Beyond Light
Link:
https://www.youtube.com/watch?v=pybJxqpqXLg
♪ Marshmellow (Prod. by Lukrembo)
Link :
https://youtu.be/y7KYdqVND4o
♪ Biscuit (Prod. by Lukrembo)
Link :
https://youtu.be/EtZ2m2Zm3vY
LAKEY INSPIRED - ♪ Better Days
Link :
https://www.youtube.com/watch?v=RXLzvo6kvVQ