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
+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);