sampler_cache.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright 2019 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <cstddef>
  6. #include <unordered_map>
  7. #include "video_core/textures/texture.h"
  8. namespace VideoCommon {
  9. struct SamplerCacheKey final : public Tegra::Texture::TSCEntry {
  10. std::size_t Hash() const;
  11. bool operator==(const SamplerCacheKey& rhs) const;
  12. bool operator!=(const SamplerCacheKey& rhs) const {
  13. return !operator==(rhs);
  14. }
  15. };
  16. } // namespace VideoCommon
  17. namespace std {
  18. template <>
  19. struct hash<VideoCommon::SamplerCacheKey> {
  20. std::size_t operator()(const VideoCommon::SamplerCacheKey& k) const noexcept {
  21. return k.Hash();
  22. }
  23. };
  24. } // namespace std
  25. namespace VideoCommon {
  26. template <typename SamplerType, typename SamplerStorageType>
  27. class SamplerCache {
  28. public:
  29. SamplerType GetSampler(const Tegra::Texture::TSCEntry& tsc) {
  30. const auto [entry, is_cache_miss] = cache.try_emplace(SamplerCacheKey{tsc});
  31. auto& sampler = entry->second;
  32. if (is_cache_miss) {
  33. sampler = CreateSampler(tsc);
  34. }
  35. return ToSamplerType(sampler);
  36. }
  37. protected:
  38. virtual SamplerStorageType CreateSampler(const Tegra::Texture::TSCEntry& tsc) const = 0;
  39. virtual SamplerType ToSamplerType(const SamplerStorageType& sampler) const = 0;
  40. private:
  41. std::unordered_map<SamplerCacheKey, SamplerStorageType> cache;
  42. };
  43. } // namespace VideoCommon