Avoid global variables for the shader cache

This commit is contained in:
Guillaume Meunier
2025-10-25 10:30:29 +02:00
parent a9f774c67c
commit 1e03b6e672
8 changed files with 53 additions and 15 deletions
+1
View File
@@ -28,6 +28,7 @@
#include "utils/files.h"
#include "utils/i18n.h"
#include "vk/check.h"
#include "vk/shader.h"
#include "wifi_lock.h"
#include "xr/actionset.h"
#include "xr/check.h"
+2 -4
View File
@@ -75,11 +75,9 @@ struct image_loader
return load(pixels.data(), pixels.size() * sizeof(T), extent, format, name);
}
template <typename... Args>
std::shared_ptr<loaded_image> operator()(Args &&... args)
std::shared_ptr<loaded_image> operator()(std::span<const std::byte> bytes, bool srgb, const std::string & name = "", bool premultiply = false)
{
auto result = std::make_shared<loaded_image>(load(std::forward<Args>(args)...));
return result;
return std::make_shared<loaded_image>(load(bytes, srgb, name, premultiply));
}
private:
+2 -3
View File
@@ -812,9 +812,8 @@ public:
primitive_ref.vertex_shader = "lit_skinned.vert";
// For attributes types, see https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#meshes-overview (§ 3.7.2.1)
const std::vector<shader::input> & shader_inputs = load_shader(device, primitive_ref.vertex_shader)->inputs;
for (const auto & input: shader_inputs)
auto shader = shader_loader{device}(primitive_ref.vertex_shader);
for (const auto & input: shader->inputs)
{
std::string semantic = input.name;
std::transform(semantic.begin(), semantic.end(), semantic.begin(), [](auto c) { return std::toupper(c); });
+3 -2
View File
@@ -276,6 +276,7 @@ scene_renderer::scene_renderer(
device(device),
physical_device_properties(physical_device.getProperties()),
queue(queue),
shader_cache(device),
layout_0(create_descriptor_set_layout(layout_bindings_0, vk::DescriptorSetLayoutCreateFlagBits::ePushDescriptorKHR))
{
// Create the default material
@@ -669,8 +670,8 @@ vk::raii::Pipeline scene_renderer::create_pipeline(const pipeline_info & info)
{
spdlog::debug("Creating pipeline");
auto vertex_shader = load_shader(device, info.vertex_shader_name);
auto fragment_shader = load_shader(device, info.fragment_shader_name);
auto vertex_shader = shader_loader{device}(info.vertex_shader_name);
auto fragment_shader = shader_loader{device}(info.fragment_shader_name);
auto specialization = make_specialization_constants(
int32_t(info.nb_texcoords),
+3
View File
@@ -19,6 +19,8 @@
#pragma once
#include "render/vertex_layout.h"
#include "utils/cache.h"
#include "vk/shader.h"
#include <array>
#include <cstdint>
#include <glm/mat4x4.hpp>
@@ -149,6 +151,7 @@ class scene_renderer
std::unordered_map<renderpass_info, renderpass> renderpasses;
std::unordered_map<output_image_info, output_image> output_images;
std::unordered_map<pipeline_info, vk::raii::Pipeline> pipelines;
utils::cache<std::string, shader, shader_loader> shader_cache;
output_image & get_output_image_data(const output_image_info & info);
vk::raii::Pipeline & get_pipeline(const pipeline_info & info);
+2
View File
@@ -19,6 +19,7 @@
#pragma once
#include "utils/thread_safe.h"
#include <concepts>
#include <memory>
#include <unordered_map>
@@ -38,6 +39,7 @@ public:
{}
template <typename... Args>
requires std::invocable<Loader, Args...>
std::shared_ptr<Asset> load(const Key & key, Args &&... args)
{
auto _ = entries.lock();
+30 -5
View File
@@ -39,7 +39,11 @@ std::vector<T *> enumerate(F && f, SpvReflectShaderModule & reflected_shader_mod
return data;
}
std::shared_ptr<shader> load_shader(vk::raii::Device & device, std::span<const uint32_t> spirv)
shader_loader::shader_loader(vk::raii::Device & device) :
device(device)
{}
std::shared_ptr<shader> shader_loader::operator()(std::span<const uint32_t> spirv)
{
vk::ShaderModuleCreateInfo create_info{
.codeSize = spirv.size_bytes(),
@@ -69,6 +73,14 @@ std::shared_ptr<shader> load_shader(vk::raii::Device & device, std::span<const u
for (SpvReflectSpecializationConstant * variable: enumerate<SpvReflectSpecializationConstant>(spvReflectEnumerateSpecializationConstants, reflect))
specialization_constants.emplace_back(variable->constant_id, variable->name);
for (SpvReflectDescriptorSet * variable: enumerate<SpvReflectDescriptorSet>(spvReflectEnumerateDescriptorSets, reflect))
{
}
for (SpvReflectDescriptorBinding * variable: enumerate<SpvReflectDescriptorBinding>(spvReflectEnumerateDescriptorBindings, reflect))
{
}
spvReflectDestroyShaderModule(&reflect);
}
else
@@ -77,10 +89,23 @@ std::shared_ptr<shader> load_shader(vk::raii::Device & device, std::span<const u
return std::make_shared<shader>(vk::raii::ShaderModule{device, create_info}, inputs, specialization_constants);
}
std::shared_ptr<shader> shader_loader::operator()(const std::string & name)
{
std::span<const uint32_t> spirv;
try
{
spirv = shaders.at(name);
}
catch (std::exception & e)
{
spdlog::error("Cannot load shader {}: {}", name, e.what());
throw;
}
return (*this)(shaders.at(name));
}
std::shared_ptr<shader> load_shader(vk::raii::Device & device, const std::string & name)
{
using loader_type = std::shared_ptr<shader>(vk::raii::Device &, std::span<const uint32_t>);
static utils::cache<std::string, shader, loader_type *> shader_cache{(loader_type *)load_shader};
return shader_cache.load(name, device, shaders.at(name));
return shader_loader{device}(name);
}
+10 -1
View File
@@ -54,5 +54,14 @@ struct shader
}
};
std::shared_ptr<shader> load_shader(vk::raii::Device & device, std::span<const uint32_t> spirv);
struct shader_loader
{
vk::raii::Device & device;
shader_loader(vk::raii::Device & device);
std::shared_ptr<shader> operator()(std::span<const uint32_t> spirv);
std::shared_ptr<shader> operator()(const std::string & name);
};
std::shared_ptr<shader> load_shader(vk::raii::Device & device, const std::string & name);