Debian Codegraph @

Source/Core/VideoCommon/GraphicsModSystem/Runtime/CustomShaderCache.cpp

raw symbols SCIP
1 // Copyright 2022 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "VideoCommon/GraphicsModSystem/Runtime/CustomShaderCache.h"
5
6 #include <utility>
7
8 #include "VideoCommon/AbstractGfx.h"
9 #include "VideoCommon/VideoConfig.h"
10 #include "VideoCommon/VideoEvents.h"
11
12 CustomShaderCache::CustomShaderCache()
13 {
14 m_api_type = g_backend_info.api_type;
15 m_host_config.bits = ShaderHostConfig::GetCurrent().bits;
16
17 m_async_shader_compiler = g_gfx->CreateAsyncShaderCompiler();
18 m_async_shader_compiler->StartWorkerThreads(1); // TODO
19
20 m_async_uber_shader_compiler = g_gfx->CreateAsyncShaderCompiler();
21 m_async_uber_shader_compiler->StartWorkerThreads(1); // TODO
22
23 m_frame_end_handler = GetVideoEvents().after_frame_event.Register(
24 [this](Core::System&) { RetrieveAsyncShaders(); });
25 }
26
27 CustomShaderCache::~CustomShaderCache()
28 {
29 if (m_async_shader_compiler)
30 {
31 m_async_shader_compiler->StopWorkerThreads();
32 m_async_shader_compiler->ClearAllWork();
33 }
34
35 if (m_async_uber_shader_compiler)
36 {
37 m_async_uber_shader_compiler->StopWorkerThreads();
38 m_async_uber_shader_compiler->ClearAllWork();
39 }
40 }
41
42 void CustomShaderCache::RetrieveAsyncShaders()
43 {
44 m_async_shader_compiler->RetrieveWorkItems();
45 m_async_uber_shader_compiler->RetrieveWorkItems();
46 }
47
48 void CustomShaderCache::Reload()
49 {
50 while (m_async_shader_compiler->HasPendingWork() || m_async_shader_compiler->HasCompletedWork())
51 {
52 m_async_shader_compiler->RetrieveWorkItems();
53 }
54
55 while (m_async_uber_shader_compiler->HasPendingWork() ||
56 m_async_uber_shader_compiler->HasCompletedWork())
57 {
58 m_async_uber_shader_compiler->RetrieveWorkItems();
59 }
60
61 m_ps_cache = {};
62 m_uber_ps_cache = {};
63 m_pipeline_cache = {};
64 m_uber_pipeline_cache = {};
65 }
66
67 std::optional<const AbstractPipeline*>
68 CustomShaderCache::GetPipelineAsync(const VideoCommon::GXPipelineUid& uid,
69 const CustomShaderInstance& custom_shaders,
70 const AbstractPipelineConfig& pipeline_config)
71 {
72 if (auto holder = m_pipeline_cache.GetHolder(uid, custom_shaders))
73 {
74 if (holder->pending)
75 return std::nullopt;
76 return holder->value.get();
77 }
78 AsyncCreatePipeline(uid, custom_shaders, pipeline_config);
79 return std::nullopt;
80 }
81
82 std::optional<const AbstractPipeline*>
83 CustomShaderCache::GetPipelineAsync(const VideoCommon::GXUberPipelineUid& uid,
84 const CustomShaderInstance& custom_shaders,
85 const AbstractPipelineConfig& pipeline_config)
86 {
87 if (auto holder = m_uber_pipeline_cache.GetHolder(uid, custom_shaders))
88 {
89 if (holder->pending)
90 return std::nullopt;
91 return holder->value.get();
92 }
93 AsyncCreatePipeline(uid, custom_shaders, pipeline_config);
94 return std::nullopt;
95 }
96
97 void CustomShaderCache::AsyncCreatePipeline(const VideoCommon::GXPipelineUid& uid,
98
99 const CustomShaderInstance& custom_shaders,
100 const AbstractPipelineConfig& pipeline_config)
101 {
102 class PipelineWorkItem final : public VideoCommon::AsyncShaderCompiler::WorkItem
103 {
104 public:
105 PipelineWorkItem(CustomShaderCache* shader_cache, VideoCommon::GXPipelineUid uid,
106 CustomShaderInstance custom_shaders, PipelineIterator iterator,
107 AbstractPipelineConfig pipeline_config)
108 : m_shader_cache(shader_cache), m_uid(std::move(uid)), m_iterator(iterator),
109 m_config(std::move(pipeline_config)), m_custom_shaders(std::move(custom_shaders))
110 {
111 SetStagesReady();
112 }
113
114 void SetStagesReady()
115 {
116 m_stages_ready = true;
117
118 PixelShaderUid ps_uid = m_uid.ps_uid;
119 ClearUnusedPixelShaderUidBits(m_shader_cache->m_api_type, m_shader_cache->m_host_config,
120 &ps_uid);
121
122 if (auto holder = m_shader_cache->m_ps_cache.GetHolder(ps_uid, m_custom_shaders))
123 {
124 // If the pixel shader is no longer pending compilation
125 // and the shader compilation succeeded, set
126 // the pipeline to use the new pixel shader.
127 // Otherwise, use the existing shader.
128 if (!holder->pending && holder->value.get())
129 {
130 m_config.pixel_shader = holder->value.get();
131 }
132 m_stages_ready &= !holder->pending;
133 }
134 else
135 {
136 m_stages_ready &= false;
137 m_shader_cache->QueuePixelShaderCompile(ps_uid, m_custom_shaders);
138 }
139 }
140
141 bool Compile() override
142 {
143 if (m_stages_ready)
144 {
145 m_pipeline = g_gfx->CreatePipeline(m_config);
146 }
147 return true;
148 }
149
150 void Retrieve() override
151 {
152 if (m_stages_ready)
153 {
154 m_shader_cache->NotifyPipelineFinished(m_iterator, std::move(m_pipeline));
155 }
156 else
157 {
158 // Re-queue for next frame.
159 auto wi = m_shader_cache->m_async_shader_compiler->CreateWorkItem<PipelineWorkItem>(
160 m_shader_cache, m_uid, m_custom_shaders, m_iterator, m_config);
161 m_shader_cache->m_async_shader_compiler->QueueWorkItem(std::move(wi), 0);
162 }
163 }
164
165 private:
166 CustomShaderCache* m_shader_cache;
167 std::unique_ptr<AbstractPipeline> m_pipeline;
168 VideoCommon::GXPipelineUid m_uid;
169 PipelineIterator m_iterator;
170 AbstractPipelineConfig m_config;
171 CustomShaderInstance m_custom_shaders;
172 bool m_stages_ready;
173 };
174
175 auto list_iter = m_pipeline_cache.InsertElement(uid, custom_shaders);
176 auto work_item = m_async_shader_compiler->CreateWorkItem<PipelineWorkItem>(
177 this, uid, custom_shaders, list_iter, pipeline_config);
178 m_async_shader_compiler->QueueWorkItem(std::move(work_item), 0);
179 }
180
181 void CustomShaderCache::AsyncCreatePipeline(const VideoCommon::GXUberPipelineUid& uid,
182
183 const CustomShaderInstance& custom_shaders,
184 const AbstractPipelineConfig& pipeline_config)
185 {
186 class PipelineWorkItem final : public VideoCommon::AsyncShaderCompiler::WorkItem
187 {
188 public:
189 PipelineWorkItem(CustomShaderCache* shader_cache, VideoCommon::GXUberPipelineUid uid,
190 CustomShaderInstance custom_shaders, UberPipelineIterator iterator,
191 AbstractPipelineConfig pipeline_config)
192 : m_shader_cache(shader_cache), m_uid(std::move(uid)), m_iterator(iterator),
193 m_config(std::move(pipeline_config)), m_custom_shaders(std::move(custom_shaders))
194 {
195 SetStagesReady();
196 }
197
198 void SetStagesReady()
199 {
200 m_stages_ready = true;
201
202 UberShader::PixelShaderUid ps_uid = m_uid.ps_uid;
203 ClearUnusedPixelShaderUidBits(m_shader_cache->m_api_type, m_shader_cache->m_host_config,
204 &ps_uid);
205
206 if (auto holder = m_shader_cache->m_uber_ps_cache.GetHolder(ps_uid, m_custom_shaders))
207 {
208 if (!holder->pending && holder->value.get())
209 {
210 m_config.pixel_shader = holder->value.get();
211 }
212 m_stages_ready &= !holder->pending;
213 }
214 else
215 {
216 m_stages_ready &= false;
217 m_shader_cache->QueuePixelShaderCompile(ps_uid, m_custom_shaders);
218 }
219 }
220
221 bool Compile() override
222 {
223 if (m_stages_ready)
224 {
225 if (m_config.pixel_shader == nullptr || m_config.vertex_shader == nullptr)
226 return false;
227
228 m_pipeline = g_gfx->CreatePipeline(m_config);
229 }
230 return true;
231 }
232
233 void Retrieve() override
234 {
235 if (m_stages_ready)
236 {
237 m_shader_cache->NotifyPipelineFinished(m_iterator, std::move(m_pipeline));
238 }
239 else
240 {
241 // Re-queue for next frame.
242 auto wi = m_shader_cache->m_async_uber_shader_compiler->CreateWorkItem<PipelineWorkItem>(
243 m_shader_cache, m_uid, m_custom_shaders, m_iterator, m_config);
244 m_shader_cache->m_async_uber_shader_compiler->QueueWorkItem(std::move(wi), 0);
245 }
246 }
247
248 private:
249 CustomShaderCache* m_shader_cache;
250 std::unique_ptr<AbstractPipeline> m_pipeline;
251 VideoCommon::GXUberPipelineUid m_uid;
252 UberPipelineIterator m_iterator;
253 AbstractPipelineConfig m_config;
254 CustomShaderInstance m_custom_shaders;
255 bool m_stages_ready;
256 };
257
258 auto list_iter = m_uber_pipeline_cache.InsertElement(uid, custom_shaders);
259 auto work_item = m_async_uber_shader_compiler->CreateWorkItem<PipelineWorkItem>(
260 this, uid, custom_shaders, list_iter, pipeline_config);
261 m_async_uber_shader_compiler->QueueWorkItem(std::move(work_item), 0);
262 }
263
264 void CustomShaderCache::NotifyPipelineFinished(PipelineIterator iterator,
265 std::unique_ptr<AbstractPipeline> pipeline)
266 {
267 iterator->second.pending = false;
268 iterator->second.value = std::move(pipeline);
269 }
270
271 void CustomShaderCache::NotifyPipelineFinished(UberPipelineIterator iterator,
272 std::unique_ptr<AbstractPipeline> pipeline)
273 {
274 iterator->second.pending = false;
275 iterator->second.value = std::move(pipeline);
276 }
277
278 void CustomShaderCache::QueuePixelShaderCompile(const PixelShaderUid& uid,
279
280 const CustomShaderInstance& custom_shaders)
281 {
282 class PixelShaderWorkItem final : public VideoCommon::AsyncShaderCompiler::WorkItem
283 {
284 public:
285 PixelShaderWorkItem(CustomShaderCache* shader_cache, const PixelShaderUid& uid,
286 CustomShaderInstance custom_shaders, PixelShaderIterator iter)
287 : m_shader_cache(shader_cache), m_uid(uid), m_custom_shaders(std::move(custom_shaders)),
288 m_iter(iter)
289 {
290 }
291
292 bool Compile() override
293 {
294 m_shader = m_shader_cache->CompilePixelShader(m_uid, m_custom_shaders);
295 return true;
296 }
297
298 void Retrieve() override
299 {
300 m_shader_cache->NotifyPixelShaderFinished(m_iter, std::move(m_shader));
301 }
302
303 private:
304 CustomShaderCache* m_shader_cache;
305 std::unique_ptr<AbstractShader> m_shader;
306 PixelShaderUid m_uid;
307 CustomShaderInstance m_custom_shaders;
308 PixelShaderIterator m_iter;
309 };
310
311 auto list_iter = m_ps_cache.InsertElement(uid, custom_shaders);
312 auto work_item = m_async_shader_compiler->CreateWorkItem<PixelShaderWorkItem>(
313 this, uid, custom_shaders, list_iter);
314 m_async_shader_compiler->QueueWorkItem(std::move(work_item), 0);
315 }
316
317 void CustomShaderCache::QueuePixelShaderCompile(const UberShader::PixelShaderUid& uid,
318
319 const CustomShaderInstance& custom_shaders)
320 {
321 class PixelShaderWorkItem final : public VideoCommon::AsyncShaderCompiler::WorkItem
322 {
323 public:
324 PixelShaderWorkItem(CustomShaderCache* shader_cache, const UberShader::PixelShaderUid& uid,
325 CustomShaderInstance custom_shaders, UberPixelShaderIterator iter)
326 : m_shader_cache(shader_cache), m_uid(uid), m_custom_shaders(std::move(custom_shaders)),
327 m_iter(iter)
328 {
329 }
330
331 bool Compile() override
332 {
333 m_shader = m_shader_cache->CompilePixelShader(m_uid, m_custom_shaders);
334 return true;
335 }
336
337 void Retrieve() override
338 {
339 m_shader_cache->NotifyPixelShaderFinished(m_iter, std::move(m_shader));
340 }
341
342 private:
343 CustomShaderCache* m_shader_cache;
344 std::unique_ptr<AbstractShader> m_shader;
345 UberShader::PixelShaderUid m_uid;
346 CustomShaderInstance m_custom_shaders;
347 UberPixelShaderIterator m_iter;
348 };
349
350 auto list_iter = m_uber_ps_cache.InsertElement(uid, custom_shaders);
351 auto work_item = m_async_uber_shader_compiler->CreateWorkItem<PixelShaderWorkItem>(
352 this, uid, custom_shaders, list_iter);
353 m_async_uber_shader_compiler->QueueWorkItem(std::move(work_item), 0);
354 }
355
356 std::unique_ptr<AbstractShader>
357 CustomShaderCache::CompilePixelShader(const PixelShaderUid& uid,
358 const CustomShaderInstance& custom_shaders) const
359 {
360 const ShaderCode source_code =
361 GeneratePixelShaderCode(m_api_type, m_host_config, uid.GetUidData(), {});
362 return g_gfx->CreateShaderFromSource(ShaderStage::Pixel, source_code.GetBuffer(), nullptr,
363 "Custom Pixel Shader");
364 }
365
366 std::unique_ptr<AbstractShader>
367 CustomShaderCache::CompilePixelShader(const UberShader::PixelShaderUid& uid,
368 const CustomShaderInstance& custom_shaders) const
369 {
370 const ShaderCode source_code = GenPixelShader(m_api_type, m_host_config, uid.GetUidData());
371 return g_gfx->CreateShaderFromSource(ShaderStage::Pixel, source_code.GetBuffer(), nullptr,
372 "Custom Uber Pixel Shader");
373 }
374
375 void CustomShaderCache::NotifyPixelShaderFinished(PixelShaderIterator iterator,
376 std::unique_ptr<AbstractShader> shader)
377 {
378 iterator->second.pending = false;
379 iterator->second.value = std::move(shader);
380 }
381
382 void CustomShaderCache::NotifyPixelShaderFinished(UberPixelShaderIterator iterator,
383 std::unique_ptr<AbstractShader> shader)
384 {
385 iterator->second.pending = false;
386 iterator->second.value = std::move(shader);
387 }