Debian Codegraph @

Source/Core/VideoBackends/Metal/MTLObjectCache.h

raw symbols SCIP
1 // Copyright 2022 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #pragma once
5
6 #include <Metal/Metal.h>
7 #include <memory>
8
9 #include "VideoBackends/Metal/MRCHelpers.h"
10
11 #include "VideoCommon/BPMemory.h"
12 #include "VideoCommon/RenderState.h"
13
14 struct AbstractPipelineConfig;
15 class AbstractPipeline;
16
17 namespace Metal
18 {
19 class Shader;
20 extern MRCOwned<id<MTLDevice>> g_device;
21 extern MRCOwned<id<MTLCommandQueue>> g_queue;
22
23 struct DepthStencilSelector
24 {
25 u8 value;
26
27 DepthStencilSelector() : value(0) {}
28 DepthStencilSelector(bool update_enable, enum CompareMode cmp)
29 : value(update_enable | (static_cast<u32>(cmp) << 1))
30 {
31 }
32 DepthStencilSelector(DepthState state)
33 : DepthStencilSelector(state.test_enable ? state.update_enable : false,
34 state.test_enable ? state.func : CompareMode::Always)
35 {
36 }
37
38 bool UpdateEnable() const { return value & 1; }
39 enum CompareMode CompareMode() const { return static_cast<enum CompareMode>(value >> 1); }
40
41 bool operator==(const DepthStencilSelector& other) const { return value == other.value; }
42 static constexpr size_t N_VALUES = 1 << 4;
43 };
44
45 struct SamplerSelector
46 {
47 u16 value;
48 SamplerSelector() : value(0) {}
49 SamplerSelector(SamplerState state)
50 {
51 value = (static_cast<u32>(state.tm0.min_filter.Value()) << 0) |
52 (static_cast<u32>(state.tm0.mag_filter.Value()) << 1) |
53 (static_cast<u32>(state.tm0.mipmap_filter.Value()) << 2) |
54 (static_cast<u32>(state.tm0.anisotropic_filtering) << 3);
55 value |= (static_cast<u32>(state.tm0.wrap_u.Value()) +
56 3 * static_cast<u32>(state.tm0.wrap_v.Value()))
57 << 7;
58 }
59 FilterMode MinFilter() const { return static_cast<FilterMode>(value & 1); }
60 FilterMode MagFilter() const { return static_cast<FilterMode>((value >> 1) & 1); }
61 FilterMode MipFilter() const { return static_cast<FilterMode>((value >> 2) & 1); }
62 WrapMode WrapU() const { return static_cast<WrapMode>((value >> 7) % 3); }
63 WrapMode WrapV() const { return static_cast<WrapMode>((value >> 7) / 3); }
64 u32 AnisotropicFiltering() const { return ((value >> 3) & 0xf); }
65
66 bool operator==(const SamplerSelector& other) const { return value == other.value; }
67 static constexpr size_t N_VALUES = (1 << 7) * 9;
68 };
69
70 class ObjectCache
71 {
72 ObjectCache();
73
74 public:
75 ~ObjectCache();
76
77 static void Initialize(MRCOwned<id<MTLDevice>> device);
78 static void Shutdown();
79
80 id<MTLDepthStencilState> GetDepthStencil(DepthStencilSelector sel) { return m_dss[sel.value]; }
81
82 id<MTLSamplerState> GetSampler(SamplerSelector sel)
83 {
84 if (!m_samplers[sel.value]) [[unlikely]]
85 m_samplers[sel.value] = CreateSampler(sel);
86 return m_samplers[sel.value];
87 }
88
89 id<MTLSamplerState> GetSampler(SamplerState state) { return GetSampler(SamplerSelector(state)); }
90
91 void ReloadSamplers();
92
93 std::unique_ptr<AbstractPipeline> CreatePipeline(const AbstractPipelineConfig& config);
94 void ShaderDestroyed(const Shader* shader);
95
96 private:
97 class Internal;
98 std::unique_ptr<Internal> m_internal;
99 MRCOwned<id<MTLSamplerState>> CreateSampler(SamplerSelector sel);
100 MRCOwned<id<MTLDepthStencilState>> m_dss[DepthStencilSelector::N_VALUES];
101 MRCOwned<id<MTLSamplerState>> m_samplers[SamplerSelector::N_VALUES];
102 };
103
104 extern std::unique_ptr<ObjectCache> g_object_cache;
105 } // namespace Metal