mirror of https://git.suyu.dev/suyu/suyu
video_core: Abstract vk_sampler_cache into a templated class
parent
628153cccd
commit
c5047540c9
@ -0,0 +1,21 @@
|
||||
// Copyright 2019 yuzu Emulator Project
|
||||
// Licensed under GPLv2 or any later version
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#include "common/cityhash.h"
|
||||
#include "common/common_types.h"
|
||||
#include "video_core/sampler_cache.h"
|
||||
|
||||
namespace VideoCommon {
|
||||
|
||||
std::size_t SamplerCacheKey::Hash() const {
|
||||
static_assert(sizeof(raw) % sizeof(u64) == 0);
|
||||
return static_cast<std::size_t>(
|
||||
Common::CityHash64(reinterpret_cast<const char*>(raw.data()), sizeof(raw) / sizeof(u64)));
|
||||
}
|
||||
|
||||
bool SamplerCacheKey::operator==(const SamplerCacheKey& rhs) const {
|
||||
return raw == rhs.raw;
|
||||
}
|
||||
|
||||
} // namespace VideoCommon
|
@ -0,0 +1,60 @@
|
||||
// Copyright 2019 yuzu Emulator Project
|
||||
// Licensed under GPLv2 or any later version
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "video_core/textures/texture.h"
|
||||
|
||||
namespace VideoCommon {
|
||||
|
||||
struct SamplerCacheKey final : public Tegra::Texture::TSCEntry {
|
||||
std::size_t Hash() const;
|
||||
|
||||
bool operator==(const SamplerCacheKey& rhs) const;
|
||||
|
||||
bool operator!=(const SamplerCacheKey& rhs) const {
|
||||
return !operator==(rhs);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace VideoCommon
|
||||
|
||||
namespace std {
|
||||
|
||||
template <>
|
||||
struct hash<VideoCommon::SamplerCacheKey> {
|
||||
std::size_t operator()(const VideoCommon::SamplerCacheKey& k) const noexcept {
|
||||
return k.Hash();
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace std
|
||||
|
||||
namespace VideoCommon {
|
||||
|
||||
template <typename SamplerType, typename SamplerStorageType>
|
||||
class SamplerCache {
|
||||
public:
|
||||
SamplerType GetSampler(const Tegra::Texture::TSCEntry& tsc) {
|
||||
const auto [entry, is_cache_miss] = cache.try_emplace(SamplerCacheKey{tsc});
|
||||
auto& sampler = entry->second;
|
||||
if (is_cache_miss) {
|
||||
sampler = CreateSampler(tsc);
|
||||
}
|
||||
return ToSamplerType(sampler);
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual SamplerStorageType CreateSampler(const Tegra::Texture::TSCEntry& tsc) const = 0;
|
||||
|
||||
virtual SamplerType ToSamplerType(const SamplerStorageType& sampler) const = 0;
|
||||
|
||||
private:
|
||||
std::unordered_map<SamplerCacheKey, SamplerStorageType> cache;
|
||||
};
|
||||
|
||||
} // namespace VideoCommon
|
Loading…
Reference in New Issue