summaryrefslogtreecommitdiff
path: root/Source/Core/VideoBackends/Metal/MTLObjectCache.h
blob: 8404a6db575d2199a2b2adc869248ea3282cb4bd (plain)
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
101
102
103
104
105
// Copyright 2022 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include <Metal/Metal.h>
#include <memory>

#include "VideoBackends/Metal/MRCHelpers.h"

#include "VideoCommon/BPMemory.h"
#include "VideoCommon/RenderState.h"

struct AbstractPipelineConfig;
class AbstractPipeline;

namespace Metal
{
class Shader;
extern MRCOwned<id<MTLDevice>> g_device;
extern MRCOwned<id<MTLCommandQueue>> g_queue;

struct DepthStencilSelector
{
  u8 value;

  DepthStencilSelector() : value(0) {}
  DepthStencilSelector(bool update_enable, enum CompareMode cmp)
      : value(update_enable | (static_cast<u32>(cmp) << 1))
  {
  }
  DepthStencilSelector(DepthState state)
      : DepthStencilSelector(state.testenable ? state.updateenable : false,
                             state.testenable ? state.func : CompareMode::Always)
  {
  }

  bool UpdateEnable() const { return value & 1; }
  enum CompareMode CompareMode() const { return static_cast<enum CompareMode>(value >> 1); }

  bool operator==(const DepthStencilSelector& other) const { return value == other.value; }
  static constexpr size_t N_VALUES = 1 << 4;
};

struct SamplerSelector
{
  u8 value;
  SamplerSelector() : value(0) {}
  SamplerSelector(SamplerState state)
  {
    value = (static_cast<u32>(state.tm0.min_filter.Value()) << 0) |
            (static_cast<u32>(state.tm0.mag_filter.Value()) << 1) |
            (static_cast<u32>(state.tm0.mipmap_filter.Value()) << 2) |
            (static_cast<u32>(state.tm0.anisotropic_filtering) << 3);
    value |= (static_cast<u32>(state.tm0.wrap_u.Value()) +
              3 * static_cast<u32>(state.tm0.wrap_v.Value()))
             << 4;
  }
  FilterMode MinFilter() const { return static_cast<FilterMode>(value & 1); }
  FilterMode MagFilter() const { return static_cast<FilterMode>((value >> 1) & 1); }
  FilterMode MipFilter() const { return static_cast<FilterMode>((value >> 2) & 1); }
  WrapMode WrapU() const { return static_cast<WrapMode>((value >> 4) % 3); }
  WrapMode WrapV() const { return static_cast<WrapMode>((value >> 4) / 3); }
  bool AnisotropicFiltering() const { return ((value >> 3) & 1); }

  bool operator==(const SamplerSelector& other) const { return value == other.value; }
  static constexpr size_t N_VALUES = (1 << 4) * 9;
};

class ObjectCache
{
  ObjectCache();

public:
  ~ObjectCache();

  static void Initialize(MRCOwned<id<MTLDevice>> device);
  static void Shutdown();

  id<MTLDepthStencilState> GetDepthStencil(DepthStencilSelector sel) { return m_dss[sel.value]; }

  id<MTLSamplerState> GetSampler(SamplerSelector sel)
  {
    if (!m_samplers[sel.value]) [[unlikely]]
      m_samplers[sel.value] = CreateSampler(sel);
    return m_samplers[sel.value];
  }

  id<MTLSamplerState> GetSampler(SamplerState state) { return GetSampler(SamplerSelector(state)); }

  void ReloadSamplers();

  std::unique_ptr<AbstractPipeline> CreatePipeline(const AbstractPipelineConfig& config);
  void ShaderDestroyed(const Shader* shader);

private:
  class Internal;
  std::unique_ptr<Internal> m_internal;
  MRCOwned<id<MTLSamplerState>> CreateSampler(SamplerSelector sel);
  MRCOwned<id<MTLDepthStencilState>> m_dss[DepthStencilSelector::N_VALUES];
  MRCOwned<id<MTLSamplerState>> m_samplers[SamplerSelector::N_VALUES];
};

extern std::unique_ptr<ObjectCache> g_object_cache;
}  // namespace Metal