summaryrefslogtreecommitdiffstats
path: root/src/video_core/shader/const_buffer_locker.h
blob: 0bc25778112371c3b18e996b606bdfe64cbe423e (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright 2019 yuzu Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

#pragma once

#include <unordered_map>
#include "common/common_types.h"
#include "common/hash.h"
#include "video_core/engines/const_buffer_engine_interface.h"

namespace VideoCommon::Shader {

using KeyMap = std::unordered_map<std::pair<u32, u32>, u32, Common::PairHash>;
using BoundSamplerMap = std::unordered_map<u32, Tegra::Engines::SamplerDescriptor>;
using BindlessSamplerMap =
    std::unordered_map<std::pair<u32, u32>, Tegra::Engines::SamplerDescriptor, Common::PairHash>;

class ConstBufferLocker {
public:
    explicit ConstBufferLocker(Tegra::Engines::ShaderType shader_stage);

    explicit ConstBufferLocker(Tegra::Engines::ShaderType shader_stage,
                               Tegra::Engines::ConstBufferEngineInterface* engine);

    // Checks if an engine is setup, it may be possible that during disk shader
    // cache run, the engines have not been created yet.
    bool IsEngineSet() const;

    // Use this to set/change the engine used for this shader.
    void SetEngine(Tegra::Engines::ConstBufferEngineInterface* engine);

    // Retrieves a key from the locker, if it's registered, it will give the
    // registered value, if not it will obtain it from maxwell3d and register it.
    std::optional<u32> ObtainKey(u32 buffer, u32 offset);

    std::optional<Tegra::Engines::SamplerDescriptor> ObtainBoundSampler(u32 offset);

    std::optional<Tegra::Engines::SamplerDescriptor> ObtainBindlessSampler(u32 buffer, u32 offset);

    // Manually inserts a key.
    void InsertKey(u32 buffer, u32 offset, u32 value);

    void InsertBoundSampler(u32 offset, Tegra::Engines::SamplerDescriptor sampler);

    void InsertBindlessSampler(u32 buffer, u32 offset, Tegra::Engines::SamplerDescriptor sampler);

    // Retrieves the number of keys registered.
    std::size_t NumKeys() const {
        if (!keys) {
            return 0;
        }
        return keys->size();
    }

    std::size_t NumBoundSamplers() const {
        if (!bound_samplers) {
            return 0;
        }
        return bound_samplers->size();
    }

    std::size_t NumBindlessSamplers() const {
        if (!bindless_samplers) {
            return 0;
        }
        return bindless_samplers->size();
    }

    // Gives an accessor to the key's database.
    // Pre: NumKeys > 0
    const KeyMap& AccessKeys() const {
        return *keys;
    }

    // Gives an accessor to the sampler's database.
    // Pre: NumBindlessSamplers > 0
    const BoundSamplerMap& AccessBoundSamplers() const {
        return *bound_samplers;
    }

    // Gives an accessor to the sampler's database.
    // Pre: NumBindlessSamplers > 0
    const BindlessSamplerMap& AccessBindlessSamplers() const {
        return *bindless_samplers;
    }

    // Checks keys & samplers against engine's current const buffers. Returns true if they
    // are the same value, false otherwise;
    bool IsConsistant() const;

private:
    Tegra::Engines::ConstBufferEngineInterface* engine;
    Tegra::Engines::ShaderType shader_stage;
    // All containers are lazy initialized as most shaders don't use them.
    std::shared_ptr<KeyMap> keys{};
    std::shared_ptr<BoundSamplerMap> bound_samplers{};
    std::shared_ptr<BindlessSamplerMap> bindless_samplers{};
};
} // namespace VideoCommon::Shader