From 192f5e4e9fd9f5bddb5e6cc44dbcdba6579c5644 Mon Sep 17 00:00:00 2001 From: PancakeTAS Date: Sun, 6 Jul 2025 20:41:19 +0200 Subject: [PATCH] cache pipelines to improve loading speeds --- lsfg-vk-gen/include/pool/shaderpool.hpp | 14 ++++++++++++++ lsfg-vk-gen/src/pool/shaderpool.cpp | 15 +++++++++++++++ lsfg-vk-gen/src/shaderchains/alpha.cpp | 12 +++++++----- lsfg-vk-gen/src/shaderchains/beta.cpp | 13 ++++++++----- lsfg-vk-gen/src/shaderchains/delta.cpp | 12 +++++++----- lsfg-vk-gen/src/shaderchains/downsample.cpp | 2 +- lsfg-vk-gen/src/shaderchains/epsilon.cpp | 12 +++++++----- lsfg-vk-gen/src/shaderchains/extract.cpp | 2 +- lsfg-vk-gen/src/shaderchains/gamma.cpp | 14 +++++++++----- lsfg-vk-gen/src/shaderchains/magic.cpp | 2 +- lsfg-vk-gen/src/shaderchains/merge.cpp | 2 +- lsfg-vk-gen/src/shaderchains/zeta.cpp | 12 +++++++----- 12 files changed, 78 insertions(+), 34 deletions(-) diff --git a/lsfg-vk-gen/include/pool/shaderpool.hpp b/lsfg-vk-gen/include/pool/shaderpool.hpp index aeb5ba2..8477887 100644 --- a/lsfg-vk-gen/include/pool/shaderpool.hpp +++ b/lsfg-vk-gen/include/pool/shaderpool.hpp @@ -2,6 +2,7 @@ #define SHADERPOOL_HPP #include "core/device.hpp" +#include "core/pipeline.hpp" #include "core/shadermodule.hpp" #include "pool/extract.hpp" @@ -39,9 +40,22 @@ namespace LSFG::Pool { Core::ShaderModule getShader( const Core::Device& device, const std::string& name, const std::vector>& types); + + /// + /// Retrieve a pipeline shader module by name or create it. + /// + /// @param device Vulkan device + /// @param name Name of the shader module + /// @return Pipeline shader module or empty + /// + /// @throws LSFG::vulkan_error if the shader module cannot be created. + /// + Core::Pipeline getPipeline( + const Core::Device& device, const std::string& name); private: Extractor extractor; std::unordered_map shaders; + std::unordered_map pipelines; }; } diff --git a/lsfg-vk-gen/src/pool/shaderpool.cpp b/lsfg-vk-gen/src/pool/shaderpool.cpp index 46dd7c1..33cce03 100644 --- a/lsfg-vk-gen/src/pool/shaderpool.cpp +++ b/lsfg-vk-gen/src/pool/shaderpool.cpp @@ -63,3 +63,18 @@ Core::ShaderModule ShaderPool::getShader( shaders[name] = shader; return shader; } + +Core::Pipeline ShaderPool::getPipeline( + const Core::Device& device, const std::string& name) { + auto it = pipelines.find(name); + if (it != pipelines.end()) + return it->second; + + // grab the shader module + auto shader = this->getShader(device, name, {}); + + // create the pipeline + Core::Pipeline pipeline(device, shader); + pipelines[name] = pipeline; + return pipeline; +} diff --git a/lsfg-vk-gen/src/shaderchains/alpha.cpp b/lsfg-vk-gen/src/shaderchains/alpha.cpp index 61f75d3..3b8b10c 100644 --- a/lsfg-vk-gen/src/shaderchains/alpha.cpp +++ b/lsfg-vk-gen/src/shaderchains/alpha.cpp @@ -25,13 +25,15 @@ Alpha::Alpha(const Core::Device& device, Pool::ShaderPool& shaderpool, { 4, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE }, { 4, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE } }) }}; - for (size_t i = 0; i < 4; i++) { - this->pipelines.at(i) = Core::Pipeline(device, - this->shaderModules.at(i)); - if (i == 3) continue; // last shader is special + this->pipelines = {{ + shaderpool.getPipeline(device, "alpha/0.spv"), + shaderpool.getPipeline(device, "alpha/1.spv"), + shaderpool.getPipeline(device, "alpha/2.spv"), + shaderpool.getPipeline(device, "alpha/3.spv") + }}; + for (size_t i = 0; i < 3; i++) this->descriptorSets.at(i) = Core::DescriptorSet(device, pool, this->shaderModules.at(i)); - } for (size_t i = 0; i < 3; i++) this->specialDescriptorSets.at(i) = Core::DescriptorSet(device, pool, this->shaderModules.at(3)); diff --git a/lsfg-vk-gen/src/shaderchains/beta.cpp b/lsfg-vk-gen/src/shaderchains/beta.cpp index a9e012f..55e8f82 100644 --- a/lsfg-vk-gen/src/shaderchains/beta.cpp +++ b/lsfg-vk-gen/src/shaderchains/beta.cpp @@ -35,13 +35,16 @@ Beta::Beta(const Core::Device& device, Pool::ShaderPool& shaderpool, { 2, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE }, { 6, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE } }) }}; - for (size_t i = 0; i < 5; i++) { - this->pipelines.at(i) = Core::Pipeline(device, - this->shaderModules.at(i)); - if (i == 0 || i == 4) continue; // first shader has special logic + this->pipelines = {{ + shaderpool.getPipeline(device, "beta/0.spv"), + shaderpool.getPipeline(device, "beta/1.spv"), + shaderpool.getPipeline(device, "beta/2.spv"), + shaderpool.getPipeline(device, "beta/3.spv"), + shaderpool.getPipeline(device, "beta/4.spv") + }}; + for (size_t i = 1; i < 4; i++) this->descriptorSets.at(i - 1) = Core::DescriptorSet(device, pool, this->shaderModules.at(i)); - } for (size_t i = 0; i < 3; i++) this->specialDescriptorSets.at(i) = Core::DescriptorSet(device, pool, this->shaderModules.at(0)); diff --git a/lsfg-vk-gen/src/shaderchains/delta.cpp b/lsfg-vk-gen/src/shaderchains/delta.cpp index 333492d..67c3eae 100644 --- a/lsfg-vk-gen/src/shaderchains/delta.cpp +++ b/lsfg-vk-gen/src/shaderchains/delta.cpp @@ -29,13 +29,15 @@ Delta::Delta(const Core::Device& device, Pool::ShaderPool& shaderpool, { 3, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE }, { 1, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE } }) }}; - for (size_t i = 0; i < 4; i++) { - this->pipelines.at(i) = Core::Pipeline(device, - this->shaderModules.at(i)); - if (i == 3) continue; + this->pipelines = {{ + shaderpool.getPipeline(device, "delta/0.spv"), + shaderpool.getPipeline(device, "delta/1.spv"), + shaderpool.getPipeline(device, "delta/2.spv"), + shaderpool.getPipeline(device, "delta/3.spv") + }}; + for (size_t i = 0; i < 3; i++) this->descriptorSets.at(i) = Core::DescriptorSet(device, pool, this->shaderModules.at(i)); - } for (size_t i = 0; i < genc; i++) this->nDescriptorSets.emplace_back(device, pool, this->shaderModules.at(3)); diff --git a/lsfg-vk-gen/src/shaderchains/downsample.cpp b/lsfg-vk-gen/src/shaderchains/downsample.cpp index 7af81fe..4d54283 100644 --- a/lsfg-vk-gen/src/shaderchains/downsample.cpp +++ b/lsfg-vk-gen/src/shaderchains/downsample.cpp @@ -14,7 +14,7 @@ Downsample::Downsample(const Core::Device& device, Pool::ShaderPool& shaderpool, { 1, VK_DESCRIPTOR_TYPE_SAMPLER }, { 1, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE }, { 7, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE } }); - this->pipeline = Core::Pipeline(device, this->shaderModule); + this->pipeline = shaderpool.getPipeline(device, "downsample.spv"); for (size_t i = 0; i < 2; i++) this->descriptorSets.at(i) = Core::DescriptorSet(device, pool, this->shaderModule); diff --git a/lsfg-vk-gen/src/shaderchains/epsilon.cpp b/lsfg-vk-gen/src/shaderchains/epsilon.cpp index 59c87d7..b9cdf43 100644 --- a/lsfg-vk-gen/src/shaderchains/epsilon.cpp +++ b/lsfg-vk-gen/src/shaderchains/epsilon.cpp @@ -31,13 +31,15 @@ Epsilon::Epsilon(const Core::Device& device, Pool::ShaderPool& shaderpool, { 6, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE }, { 1, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE } }) }}; - for (size_t i = 0; i < 4; i++) { - this->pipelines.at(i) = Core::Pipeline(device, - this->shaderModules.at(i)); - if (i == 3) continue; + this->pipelines = {{ + shaderpool.getPipeline(device, "epsilon/0.spv"), + shaderpool.getPipeline(device, "epsilon/1.spv"), + shaderpool.getPipeline(device, "epsilon/2.spv"), + shaderpool.getPipeline(device, "epsilon/3.spv") + }}; + for (size_t i = 0; i < 3; i++) this->descriptorSets.at(i) = Core::DescriptorSet(device, pool, this->shaderModules.at(i)); - } for (size_t i = 0; i < genc; i++) this->nDescriptorSets.emplace_back(device, pool, this->shaderModules.at(3)); diff --git a/lsfg-vk-gen/src/shaderchains/extract.cpp b/lsfg-vk-gen/src/shaderchains/extract.cpp index 8251bd8..041bd72 100644 --- a/lsfg-vk-gen/src/shaderchains/extract.cpp +++ b/lsfg-vk-gen/src/shaderchains/extract.cpp @@ -16,7 +16,7 @@ Extract::Extract(const Core::Device& device, Pool::ShaderPool& shaderpool, { 2, VK_DESCRIPTOR_TYPE_SAMPLER }, { 3, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE }, { 1, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE } }); - this->pipeline = Core::Pipeline(device, this->shaderModule); + this->pipeline = shaderpool.getPipeline(device, "extract.spv"); for (size_t i = 0; i < genc; i++) this->nDescriptorSets.emplace_back(device, pool, this->shaderModule); diff --git a/lsfg-vk-gen/src/shaderchains/gamma.cpp b/lsfg-vk-gen/src/shaderchains/gamma.cpp index 66ba284..4e73c60 100644 --- a/lsfg-vk-gen/src/shaderchains/gamma.cpp +++ b/lsfg-vk-gen/src/shaderchains/gamma.cpp @@ -47,13 +47,17 @@ Gamma::Gamma(const Core::Device& device, Pool::ShaderPool& shaderpool, { 2, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE }, { 1, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE } }) }}; - for (size_t i = 0; i < 6; i++) { - this->pipelines.at(i) = Core::Pipeline(device, - this->shaderModules.at(i)); - if (i == 0 || i >= 4) continue; // first shader has special logic + this->pipelines = {{ + shaderpool.getPipeline(device, "gamma/0.spv"), + shaderpool.getPipeline(device, "gamma/1.spv"), + shaderpool.getPipeline(device, "gamma/2.spv"), + shaderpool.getPipeline(device, "gamma/3.spv"), + shaderpool.getPipeline(device, "gamma/4.spv"), + shaderpool.getPipeline(device, "gamma/5.spv") + }}; + for (size_t i = 1; i < 4; i++) this->descriptorSets.at(i - 1) = Core::DescriptorSet(device, pool, this->shaderModules.at(i)); - } for (size_t i = 0; i < genc; i++) this->n1DescriptorSets.emplace_back(device, pool, this->shaderModules.at(4)); diff --git a/lsfg-vk-gen/src/shaderchains/magic.cpp b/lsfg-vk-gen/src/shaderchains/magic.cpp index 1b74a35..e87f26b 100644 --- a/lsfg-vk-gen/src/shaderchains/magic.cpp +++ b/lsfg-vk-gen/src/shaderchains/magic.cpp @@ -22,7 +22,7 @@ Magic::Magic(const Core::Device& device, Pool::ShaderPool& shaderpool, { 2, VK_DESCRIPTOR_TYPE_SAMPLER }, { 4+4+2+1, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE }, { 3+3+2, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE } }); - this->pipeline = Core::Pipeline(device, this->shaderModule); + this->pipeline = shaderpool.getPipeline(device, "magic.spv"); for (size_t i = 0; i < genc; i++) { this->nDescriptorSets.emplace_back(); for (size_t j = 0; j < 3; j++) diff --git a/lsfg-vk-gen/src/shaderchains/merge.cpp b/lsfg-vk-gen/src/shaderchains/merge.cpp index 6b17bdb..5f9290a 100644 --- a/lsfg-vk-gen/src/shaderchains/merge.cpp +++ b/lsfg-vk-gen/src/shaderchains/merge.cpp @@ -22,7 +22,7 @@ Merge::Merge(const Core::Device& device, Pool::ShaderPool& shaderpool, { 2, VK_DESCRIPTOR_TYPE_SAMPLER }, { 5, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE }, { 1, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE } }); - this->pipeline = Core::Pipeline(device, this->shaderModule); + this->pipeline = shaderpool.getPipeline(device, "merge.spv"); for (size_t i = 0; i < genc; i++) { this->nDescriptorSets.emplace_back(); for (size_t j = 0; j < 2; j++) diff --git a/lsfg-vk-gen/src/shaderchains/zeta.cpp b/lsfg-vk-gen/src/shaderchains/zeta.cpp index a5350a8..7f740c3 100644 --- a/lsfg-vk-gen/src/shaderchains/zeta.cpp +++ b/lsfg-vk-gen/src/shaderchains/zeta.cpp @@ -31,13 +31,15 @@ Zeta::Zeta(const Core::Device& device, Pool::ShaderPool& shaderpool, { 6, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE }, { 1, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE } }) }}; - for (size_t i = 0; i < 4; i++) { - this->pipelines.at(i) = Core::Pipeline(device, - this->shaderModules.at(i)); - if (i == 3) continue; + this->pipelines = {{ + shaderpool.getPipeline(device, "zeta/0.spv"), + shaderpool.getPipeline(device, "zeta/1.spv"), + shaderpool.getPipeline(device, "zeta/2.spv"), + shaderpool.getPipeline(device, "zeta/3.spv") + }}; + for (size_t i = 0; i < 3; i++) this->descriptorSets.at(i) = Core::DescriptorSet(device, pool, this->shaderModules.at(i)); - } for (size_t i = 0; i < genc; i++) this->nDescriptorSets.emplace_back(device, pool, this->shaderModules.at(3));