Debian Codegraph @

Source/Core/VideoCommon/GraphicsModSystem/Runtime/CustomShaderCache.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 <list>
7 #include <map>
8 #include <memory>
9 #include <optional>
10
11 #include "Common/HookableEvent.h"
12
13 #include "VideoCommon/AbstractPipeline.h"
14 #include "VideoCommon/AbstractShader.h"
15 #include "VideoCommon/AsyncShaderCompiler.h"
16 #include "VideoCommon/GXPipelineTypes.h"
17 #include "VideoCommon/PixelShaderGen.h"
18 #include "VideoCommon/ShaderGenCommon.h"
19 #include "VideoCommon/UberShaderPixel.h"
20
21 struct CustomShaderInstance
22 {
23 CustomPixelShaderContents pixel_contents;
24
25 bool operator==(const CustomShaderInstance& other) const = default;
26 };
27
28 class CustomShaderCache
29 {
30 public:
31 CustomShaderCache();
32 ~CustomShaderCache();
33 CustomShaderCache(const CustomShaderCache&) = delete;
34 CustomShaderCache(CustomShaderCache&&) = delete;
35 CustomShaderCache& operator=(const CustomShaderCache&) = delete;
36 CustomShaderCache& operator=(CustomShaderCache&&) = delete;
37
38 // Changes the shader host config. Shaders should be reloaded afterwards.
39 void SetHostConfig(const ShaderHostConfig& host_config) { m_host_config.bits = host_config.bits; }
40
41 // Retrieves all pending shaders/pipelines from the async compiler.
42 void RetrieveAsyncShaders();
43
44 // Reloads/recreates all shaders and pipelines.
45 void Reload();
46
47 // The optional will be empty if this pipeline is now background compiling.
48 std::optional<const AbstractPipeline*>
49 GetPipelineAsync(const VideoCommon::GXPipelineUid& uid,
50 const CustomShaderInstance& custom_shaders,
51 const AbstractPipelineConfig& pipeline_config);
52 std::optional<const AbstractPipeline*>
53 GetPipelineAsync(const VideoCommon::GXUberPipelineUid& uid,
54 const CustomShaderInstance& custom_shaders,
55 const AbstractPipelineConfig& pipeline_config);
56
57 private:
58 // Configuration bits.
59 APIType m_api_type = APIType::Nothing;
60 ShaderHostConfig m_host_config = {};
61 std::unique_ptr<VideoCommon::AsyncShaderCompiler> m_async_shader_compiler;
62 std::unique_ptr<VideoCommon::AsyncShaderCompiler> m_async_uber_shader_compiler;
63
64 void AsyncCreatePipeline(const VideoCommon::GXPipelineUid& uid,
65 const CustomShaderInstance& custom_shaders,
66 const AbstractPipelineConfig& pipeline_config);
67 void AsyncCreatePipeline(const VideoCommon::GXUberPipelineUid& uid,
68 const CustomShaderInstance& custom_shaders,
69 const AbstractPipelineConfig& pipeline_config);
70
71 // Shader/Pipeline cache helper
72 template <typename Uid, typename ValueType>
73 struct Cache
74 {
75 struct CacheHolder
76 {
77 std::unique_ptr<ValueType> value = nullptr;
78 bool pending = true;
79 };
80 using CacheElement = std::pair<CustomShaderInstance, CacheHolder>;
81 using CacheList = std::list<CacheElement>;
82 std::map<Uid, CacheList> uid_to_cachelist;
83
84 const CacheHolder* GetHolder(const Uid& uid, const CustomShaderInstance& custom_shaders) const
85 {
86 if (auto uuid_it = uid_to_cachelist.find(uid); uuid_it != uid_to_cachelist.end())
87 {
88 for (const auto& [custom_shader_val, holder] : uuid_it->second)
89 {
90 if (custom_shaders == custom_shader_val)
91 {
92 return &holder;
93 }
94 }
95 }
96
97 return nullptr;
98 }
99
100 typename CacheList::iterator InsertElement(const Uid& uid,
101 const CustomShaderInstance& custom_shaders)
102 {
103 CacheList& cachelist = uid_to_cachelist[uid];
104 CacheElement e{custom_shaders, CacheHolder{}};
105 return cachelist.emplace(cachelist.begin(), std::move(e));
106 }
107 };
108
109 Cache<PixelShaderUid, AbstractShader> m_ps_cache;
110 Cache<UberShader::PixelShaderUid, AbstractShader> m_uber_ps_cache;
111 Cache<VideoCommon::GXPipelineUid, AbstractPipeline> m_pipeline_cache;
112 Cache<VideoCommon::GXUberPipelineUid, AbstractPipeline> m_uber_pipeline_cache;
113
114 using PipelineIterator = Cache<VideoCommon::GXPipelineUid, AbstractPipeline>::CacheList::iterator;
115 using UberPipelineIterator =
116 Cache<VideoCommon::GXUberPipelineUid, AbstractPipeline>::CacheList::iterator;
117 using PixelShaderIterator = Cache<PixelShaderUid, AbstractShader>::CacheList::iterator;
118 using UberPixelShaderIterator =
119 Cache<UberShader::PixelShaderUid, AbstractShader>::CacheList::iterator;
120
121 void NotifyPipelineFinished(PipelineIterator iterator,
122 std::unique_ptr<AbstractPipeline> pipeline);
123 void NotifyPipelineFinished(UberPipelineIterator iterator,
124 std::unique_ptr<AbstractPipeline> pipeline);
125
126 std::unique_ptr<AbstractShader>
127 CompilePixelShader(const PixelShaderUid& uid, const CustomShaderInstance& custom_shaders) const;
128 void NotifyPixelShaderFinished(PixelShaderIterator iterator,
129 std::unique_ptr<AbstractShader> shader);
130 std::unique_ptr<AbstractShader>
131 CompilePixelShader(const UberShader::PixelShaderUid& uid,
132 const CustomShaderInstance& custom_shaders) const;
133 void NotifyPixelShaderFinished(UberPixelShaderIterator iterator,
134 std::unique_ptr<AbstractShader> shader);
135
136 void QueuePixelShaderCompile(const PixelShaderUid& uid,
137 const CustomShaderInstance& custom_shaders);
138 void QueuePixelShaderCompile(const UberShader::PixelShaderUid& uid,
139 const CustomShaderInstance& custom_shaders);
140
141 Common::EventHook m_frame_end_handler;
142 };