Multithreaded CPU Rendering and Fluid Sim



As I was done with the fluid simulation and rendering, I was going to move on to working on gameplay. Its workload was mostly going to be on CPU so I looked into CPU multithreading to take advantage of modern gaming CPU with core count of 6-12.
I came across multiple GDC talks Parallelizing the Naughty Dog Engine Using Fibers - YouTube Destiny's Multithreaded Rendering Architecture - YouTube. I decided that rather than assigning a thread to each task (rendering thread, physics thread, audio thread, etc...), that I should use a method similar to thread pooling. I should have multiple general worker threads corresponding to the number of physical core count on the CPU that are spawned only once at the start of the program.
- Multiple worker threads are spawned (same as physical core count on the CPU). Each worker thread is bound to each physical CPU core.
- Now repeat the following.
- Jobs are submitted to a queue if its dependency counter is 0.
- An idle worker thread grabs a job at the front of the queue then executes it.
Going parallel means synchronization between workloads. Dependency graph/counter is used such that a job is only submitted to the queue once its dependency counter is set to 0
I know hyperthreading is enabled on most CPUs which are not ideal for game engine workloads in general. For now, I set thread affinity to every other CPU thread (SetThreadAffinityMask on Windows).
I parallelized the fluid and rendering workload by splitting up the workload into 8 chunks, which seemed like a reasonable workload distribution considering processor count on modern CPUs. I also tried to keep each job to granularity of about 50 to 200 microseconds.
This is a timeline graph of 2 frames I captured with Tracy profiler, with each row corresponding a thread.
Reduced execution time from 20ms (single-threaded) to 7ms (eight threads on Ryzen 7800X3D).
This is 256x256 Grid/Particles running in 60fps. I was barely able to run 128x128 in 30fps with a single thread.
Leave a comment
Log in with itch.io to leave a comment.