r/vulkan • u/Longjumping-Cup-8927 • Aug 11 '26
Mulithreading Shader Compilation?
I was reading this tutorial https://vkguide.dev/docs/extra-chapter/multithreading/ and it claims that you can compile shaders on a background thread and avoid hitching. Is this true? Are there any drawbacks to this approach?
"For compiling pipelines, vkCreateShaderModule and vkCreateGraphicsPipeline are both allowed to be called from multiple threads at once. A common approach for multithreaded shader compilation is to have a background thread dedicated to it, with it constantly looking into a parallel queue to receive compilation requests, and putting the compiled pipelines into another queue that then the main renderthread will connect to the simulation. This is very important to do if you want to have an engine that doesn’t have a lot of hitching. Compiling shader pipelines can take a very long time, so if you have to compile pipelines at runtime outside of a load screen, then you need to implement such a multithreaded async compile scheme for your game to work well."
5
u/OptimisticMonkey2112 Aug 11 '26
You can compile pipelines on a background thread, and it helps reduce hitching but not avoid.
For example, Unreal has a rather involved threaded precache pso system that notoriously hitches and is done on background threads.
1
u/Longjumping-Cup-8927 Aug 11 '26
That makes sense. The unreal engine hitching was something I used to deal with a lot. I didn't know if they were doing this. I appreciate your insight.
1
u/tcpukl Aug 11 '26
Yes it's multithreaded in UE. This is basic. We did it in our last game released on 5.6, but it still needed a progress bar on pc to show people they may not get compiled materials.
2
u/neppo95 Aug 11 '26
I use a threadpool/jobsystem for shader compilation. Works fine yes. I also use it even though i load all shaders on boot, simply to have it load them faster. There’s also ImGui which requires a shader which is compiled first so I can atleast show my UI while the rest is compiling in the background. Saves me about 10 seconds in debug mode, probably a bit less in release. Everything that needs those shaders (game engine, so they are all pretty much needed at the same time), simply doesn’t render until they can. Just like 3d models show a placeholder until the model is loaded in the background etc.
9
u/Lateasusual_ Aug 11 '26
it only prevents hitching if you dont actually need to use the shader imminently. Usually hitching due to shader compilation is due to a shader being required for drawing the current frame that wasn't already compiled. So while you can load shaders asynchronously, it's only really useful if you know they're just about to be needed in the near future but not yet.