summaryrefslogtreecommitdiffstats
path: root/src/video_core/sampler_cache.h
blob: cbe3ad071182e2c3b7f762b7c6bf30f4d209efbe (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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