From 7a86595a1b7b558e4742bc7911932b5271c14118 Mon Sep 17 00:00:00 2001 From: Wunkolo Date: Sun, 20 Aug 2023 23:35:52 -0700 Subject: [PATCH] Add vulkan sampler cache --- CMakeLists.txt | 2 ++ include/renderer_vk/vk_sampler_cache.hpp | 28 ++++++++++++++++++++ src/core/renderer_vk/vk_sampler_cache.cpp | 31 +++++++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 include/renderer_vk/vk_sampler_cache.hpp create mode 100644 src/core/renderer_vk/vk_sampler_cache.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 1218b97b..b953175e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -242,6 +242,7 @@ if(ENABLE_VULKAN) include/renderer_vk/vk_api.hpp include/renderer_vk/vk_debug.hpp include/renderer_vk/vk_descriptor_heap.hpp include/renderer_vk/vk_descriptor_update_batch.hpp + include/renderer_vk/vk_sampler_cache.hpp include/renderer_vk/vk_memory.hpp include/renderer_vk/vk_pica.hpp ) @@ -249,6 +250,7 @@ if(ENABLE_VULKAN) src/core/renderer_vk/vk_api.cpp src/core/renderer_vk/vk_debug.cpp src/core/renderer_vk/vk_descriptor_heap.cpp src/core/renderer_vk/vk_descriptor_update_batch.cpp + src/core/renderer_vk/vk_sampler_cache.cpp src/core/renderer_vk/vk_memory.cpp src/core/renderer_vk/vk_pica.cpp ) diff --git a/include/renderer_vk/vk_sampler_cache.hpp b/include/renderer_vk/vk_sampler_cache.hpp new file mode 100644 index 00000000..8cb27689 --- /dev/null +++ b/include/renderer_vk/vk_sampler_cache.hpp @@ -0,0 +1,28 @@ +#pragma once + +#include +#include + +#include "helpers.hpp" +#include "vk_api.hpp" + +namespace Vulkan { + // Implements a simple pool of reusable sampler objects + class SamplerCache { + private: + const vk::Device device; + + std::unordered_map samplerMap; + + explicit SamplerCache(vk::Device device); + + public: + ~SamplerCache() = default; + + SamplerCache(SamplerCache&&) = default; + + const vk::Sampler& getSampler(const vk::SamplerCreateInfo& samplerInfo); + + static std::optional create(vk::Device device); + }; +} // namespace Vulkan \ No newline at end of file diff --git a/src/core/renderer_vk/vk_sampler_cache.cpp b/src/core/renderer_vk/vk_sampler_cache.cpp new file mode 100644 index 00000000..884264b1 --- /dev/null +++ b/src/core/renderer_vk/vk_sampler_cache.cpp @@ -0,0 +1,31 @@ +#include "renderer_vk/vk_sampler_cache.hpp" + +#include + +#include "helpers.hpp" + +namespace Vulkan { + + SamplerCache::SamplerCache(vk::Device device) : device(device) {} + + const vk::Sampler& SamplerCache::getSampler(const vk::SamplerCreateInfo& samplerInfo) { + const std::size_t samplerHash = std::hash()(samplerInfo); + + // Cache hit + if (samplerMap.contains(samplerHash)) { + return samplerMap.at(samplerHash).get(); + } + + if (auto createResult = device.createSamplerUnique(samplerInfo); createResult.result == vk::Result::eSuccess) { + return (samplerMap[samplerHash] = std::move(createResult.value)).get(); + } else { + Helpers::panic("Error creating sampler: %s\n", vk::to_string(createResult.result).c_str()); + } + } + + std::optional SamplerCache::create(vk::Device device) { + SamplerCache newSamplerCache(device); + + return {std::move(newSamplerCache)}; + } +} // namespace Vulkan \ No newline at end of file