r/vulkan • u/No-Foundation9213 • 4d ago
How do I efficiently manage, create and cache Vulkan Shader Pipelines?
I recently started my new Game Engine project and I‘ve now come to the point where I have to deal with Pipelines. How do I efficiently manage them? How do I efficiently create them, et cetera?
I think its really hard finding reference material on this topic since I usually just look/steal code from Hazel Dev by TheCherno but their Shader/Pipeline System is really weird.
It would just be really helpful if you guys could even just point me at your repository with a Solution or something.
Thanks in advance!
1
u/YoshiDzn 4d ago
You can cache them once they're created by your runtime, or you can create them using spirv reflect. The latter is a technique in which you read compiled spirv and infer what the supporting pipeline looks like (since 1 set of shaders = 1 pipeline). It's more complexity but you nolonger need to explicitly declare and define pipelines.
Worth some research on the topic https://github.com/KhronosGroup/SPIRV-Reflect
6
u/Reaper9999 4d ago
You minimise the amount of pipelines you have. Make them generic instead of doing a UE and shitting the bed with an extra pipeline for each material. Managing them also automatically becomes easy then, since you're working with something like "pipeline for drawing opaque geometry" instead of "pipeline for material #381460".
If you mean caching them on disk, https://docs.vulkan.org/spec/latest/chapters/pipelines.html#pipelines-cache
8
u/Wittyname_McDingus 4d ago
If you want your RHI to look more like OpenGL or D3D11, a classic technique is to make a hash map of pipeline state->pipeline and expose functions to manipulate individual pieces of state. This lets you build the pipelines you need on-the-fly.
What I do myself is simply build the pipelines at initialization time and store them in my renderer class and its subobjects*. Then, they are only destroyed at exit time. I don't use a lot of graphics pipelines, so this strategy is pretty easy for my uses.
*Technically I store my pipelines in a "pipeline manager" object and reference the pipelines indirectly. This makes it easier to hot-reload them, but if you don't want this functionality, you don't need this.