summaryrefslogtreecommitdiff
path: root/Source/Core/VideoCommon/GraphicsModSystem/Runtime/CustomShaderCache.h
blob: ff2aba28237943b4dc9ee48e41d201c2aa3702e9 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright 2022 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include <array>
#include <list>
#include <map>
#include <memory>
#include <optional>
#include <string>
#include <string_view>

#include "VideoCommon/AbstractPipeline.h"
#include "VideoCommon/AbstractShader.h"
#include "VideoCommon/AsyncShaderCompiler.h"
#include "VideoCommon/GXPipelineTypes.h"
#include "VideoCommon/PixelShaderGen.h"
#include "VideoCommon/ShaderGenCommon.h"
#include "VideoCommon/UberShaderPixel.h"
#include "VideoCommon/VideoEvents.h"

struct CustomShaderInstance
{
  CustomPixelShaderContents pixel_contents;

  bool operator==(const CustomShaderInstance& other) const = default;
};

class CustomShaderCache
{
public:
  CustomShaderCache();
  ~CustomShaderCache();
  CustomShaderCache(const CustomShaderCache&) = delete;
  CustomShaderCache(CustomShaderCache&&) = delete;
  CustomShaderCache& operator=(const CustomShaderCache&) = delete;
  CustomShaderCache& operator=(CustomShaderCache&&) = delete;

  // Changes the shader host config. Shaders should be reloaded afterwards.
  void SetHostConfig(const ShaderHostConfig& host_config) { m_host_config.bits = host_config.bits; }

  // Retrieves all pending shaders/pipelines from the async compiler.
  void RetrieveAsyncShaders();

  // Reloads/recreates all shaders and pipelines.
  void Reload();

  // The optional will be empty if this pipeline is now background compiling.
  std::optional<const AbstractPipeline*>
  GetPipelineAsync(const VideoCommon::GXPipelineUid& uid,
                   const CustomShaderInstance& custom_shaders,
                   const AbstractPipelineConfig& pipeline_config);
  std::optional<const AbstractPipeline*>
  GetPipelineAsync(const VideoCommon::GXUberPipelineUid& uid,
                   const CustomShaderInstance& custom_shaders,
                   const AbstractPipelineConfig& pipeline_config);

private:
  // Configuration bits.
  APIType m_api_type = APIType::Nothing;
  ShaderHostConfig m_host_config = {};
  std::unique_ptr<VideoCommon::AsyncShaderCompiler> m_async_shader_compiler;
  std::unique_ptr<VideoCommon::AsyncShaderCompiler> m_async_uber_shader_compiler;

  void AsyncCreatePipeline(const VideoCommon::GXPipelineUid& uid,
                           const CustomShaderInstance& custom_shaders,
                           const AbstractPipelineConfig& pipeline_config);
  void AsyncCreatePipeline(const VideoCommon::GXUberPipelineUid& uid,
                           const CustomShaderInstance& custom_shaders,
                           const AbstractPipelineConfig& pipeline_config);

  // Shader/Pipeline cache helper
  template <typename Uid, typename ValueType>
  struct Cache
  {
    struct CacheHolder
    {
      std::unique_ptr<ValueType> value = nullptr;
      bool pending = true;
    };
    using CacheElement = std::pair<CustomShaderInstance, CacheHolder>;
    using CacheList = std::list<CacheElement>;
    std::map<Uid, CacheList> uid_to_cachelist;

    const CacheHolder* GetHolder(const Uid& uid, const CustomShaderInstance& custom_shaders) const
    {
      if (auto uuid_it = uid_to_cachelist.find(uid); uuid_it != uid_to_cachelist.end())
      {
        for (const auto& [custom_shader_val, holder] : uuid_it->second)
        {
          if (custom_shaders == custom_shader_val)
          {
            return &holder;
          }
        }
      }

      return nullptr;
    }

    typename CacheList::iterator InsertElement(const Uid& uid,
                                               const CustomShaderInstance& custom_shaders)
    {
      CacheList& cachelist = uid_to_cachelist[uid];
      CacheElement e{custom_shaders, CacheHolder{}};
      return cachelist.emplace(cachelist.begin(), std::move(e));
    }
  };

  Cache<PixelShaderUid, AbstractShader> m_ps_cache;
  Cache<UberShader::PixelShaderUid, AbstractShader> m_uber_ps_cache;
  Cache<VideoCommon::GXPipelineUid, AbstractPipeline> m_pipeline_cache;
  Cache<VideoCommon::GXUberPipelineUid, AbstractPipeline> m_uber_pipeline_cache;

  using PipelineIterator = Cache<VideoCommon::GXPipelineUid, AbstractPipeline>::CacheList::iterator;
  using UberPipelineIterator =
      Cache<VideoCommon::GXUberPipelineUid, AbstractPipeline>::CacheList::iterator;
  using PixelShaderIterator = Cache<PixelShaderUid, AbstractShader>::CacheList::iterator;
  using UberPixelShaderIterator =
      Cache<UberShader::PixelShaderUid, AbstractShader>::CacheList::iterator;

  void NotifyPipelineFinished(PipelineIterator iterator,
                              std::unique_ptr<AbstractPipeline> pipeline);
  void NotifyPipelineFinished(UberPipelineIterator iterator,
                              std::unique_ptr<AbstractPipeline> pipeline);

  std::unique_ptr<AbstractShader>
  CompilePixelShader(const PixelShaderUid& uid, const CustomShaderInstance& custom_shaders) const;
  void NotifyPixelShaderFinished(PixelShaderIterator iterator,
                                 std::unique_ptr<AbstractShader> shader);
  std::unique_ptr<AbstractShader>
  CompilePixelShader(const UberShader::PixelShaderUid& uid,
                     const CustomShaderInstance& custom_shaders) const;
  void NotifyPixelShaderFinished(UberPixelShaderIterator iterator,
                                 std::unique_ptr<AbstractShader> shader);

  void QueuePixelShaderCompile(const PixelShaderUid& uid,
                               const CustomShaderInstance& custom_shaders);
  void QueuePixelShaderCompile(const UberShader::PixelShaderUid& uid,
                               const CustomShaderInstance& custom_shaders);

  Common::EventHook m_frame_end_handler;
};