mirror of
https://github.com/wheremyfoodat/Panda3DS.git
synced 2025-04-06 06:05:40 +12:00
Add vulkan sampler cache
This commit is contained in:
parent
14b1d7d8a8
commit
7a86595a1b
3 changed files with 61 additions and 0 deletions
|
@ -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
|
||||
)
|
||||
|
||||
|
|
28
include/renderer_vk/vk_sampler_cache.hpp
Normal file
28
include/renderer_vk/vk_sampler_cache.hpp
Normal file
|
@ -0,0 +1,28 @@
|
|||
#pragma once
|
||||
|
||||
#include <optional>
|
||||
#include <unordered_map>
|
||||
|
||||
#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<std::size_t, vk::UniqueSampler> samplerMap;
|
||||
|
||||
explicit SamplerCache(vk::Device device);
|
||||
|
||||
public:
|
||||
~SamplerCache() = default;
|
||||
|
||||
SamplerCache(SamplerCache&&) = default;
|
||||
|
||||
const vk::Sampler& getSampler(const vk::SamplerCreateInfo& samplerInfo);
|
||||
|
||||
static std::optional<SamplerCache> create(vk::Device device);
|
||||
};
|
||||
} // namespace Vulkan
|
31
src/core/renderer_vk/vk_sampler_cache.cpp
Normal file
31
src/core/renderer_vk/vk_sampler_cache.cpp
Normal file
|
@ -0,0 +1,31 @@
|
|||
#include "renderer_vk/vk_sampler_cache.hpp"
|
||||
|
||||
#include <vulkan/vulkan_hash.hpp>
|
||||
|
||||
#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<vk::SamplerCreateInfo>()(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> SamplerCache::create(vk::Device device) {
|
||||
SamplerCache newSamplerCache(device);
|
||||
|
||||
return {std::move(newSamplerCache)};
|
||||
}
|
||||
} // namespace Vulkan
|
Loading…
Add table
Reference in a new issue