summaryrefslogtreecommitdiff
path: root/Source/Core/VideoCommon/GraphicsModSystem/Runtime/CustomShaderCache.cpp
blob: de68bffa638d94ac857c2b9e525a29452569b3fd (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
// Copyright 2022 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include "VideoCommon/GraphicsModSystem/Runtime/CustomShaderCache.h"
#include "VideoCommon/AbstractGfx.h"
#include "VideoCommon/VideoConfig.h"

CustomShaderCache::CustomShaderCache()
{
  m_api_type = g_ActiveConfig.backend_info.api_type;
  m_host_config.bits = ShaderHostConfig::GetCurrent().bits;

  m_async_shader_compiler = g_gfx->CreateAsyncShaderCompiler();
  m_async_shader_compiler->StartWorkerThreads(1);  // TODO

  m_async_uber_shader_compiler = g_gfx->CreateAsyncShaderCompiler();
  m_async_uber_shader_compiler->StartWorkerThreads(1);  // TODO

  m_frame_end_handler = AfterFrameEvent::Register([this](Core::System&) { RetrieveAsyncShaders(); },
                                                  "RetrieveAsyncShaders");
}

CustomShaderCache::~CustomShaderCache()
{
  if (m_async_shader_compiler)
    m_async_shader_compiler->StopWorkerThreads();

  if (m_async_uber_shader_compiler)
    m_async_uber_shader_compiler->StopWorkerThreads();
}

void CustomShaderCache::RetrieveAsyncShaders()
{
  m_async_shader_compiler->RetrieveWorkItems();
  m_async_uber_shader_compiler->RetrieveWorkItems();
}

void CustomShaderCache::Reload()
{
  while (m_async_shader_compiler->HasPendingWork() || m_async_shader_compiler->HasCompletedWork())
  {
    m_async_shader_compiler->RetrieveWorkItems();
  }

  while (m_async_uber_shader_compiler->HasPendingWork() ||
         m_async_uber_shader_compiler->HasCompletedWork())
  {
    m_async_uber_shader_compiler->RetrieveWorkItems();
  }

  m_ps_cache = {};
  m_uber_ps_cache = {};
  m_pipeline_cache = {};
  m_uber_pipeline_cache = {};
}

std::optional<const AbstractPipeline*>
CustomShaderCache::GetPipelineAsync(const VideoCommon::GXPipelineUid& uid,
                                    const CustomShaderInstance& custom_shaders,
                                    const AbstractPipelineConfig& pipeline_config)
{
  if (auto holder = m_pipeline_cache.GetHolder(uid, custom_shaders))
  {
    if (holder->pending)
      return std::nullopt;
    return holder->value.get();
  }
  AsyncCreatePipeline(uid, custom_shaders, pipeline_config);
  return std::nullopt;
}

std::optional<const AbstractPipeline*>
CustomShaderCache::GetPipelineAsync(const VideoCommon::GXUberPipelineUid& uid,
                                    const CustomShaderInstance& custom_shaders,
                                    const AbstractPipelineConfig& pipeline_config)
{
  if (auto holder = m_uber_pipeline_cache.GetHolder(uid, custom_shaders))
  {
    if (holder->pending)
      return std::nullopt;
    return holder->value.get();
  }
  AsyncCreatePipeline(uid, custom_shaders, pipeline_config);
  return std::nullopt;
}

void CustomShaderCache::AsyncCreatePipeline(const VideoCommon::GXPipelineUid& uid,

                                            const CustomShaderInstance& custom_shaders,
                                            const AbstractPipelineConfig& pipeline_config)
{
  class PipelineWorkItem final : public VideoCommon::AsyncShaderCompiler::WorkItem
  {
  public:
    PipelineWorkItem(CustomShaderCache* shader_cache, const VideoCommon::GXPipelineUid& uid,
                     const CustomShaderInstance& custom_shaders, PipelineIterator iterator,
                     const AbstractPipelineConfig& pipeline_config)
        : m_shader_cache(shader_cache), m_uid(uid), m_iterator(iterator), m_config(pipeline_config),
          m_custom_shaders(custom_shaders)
    {
      SetStagesReady();
    }

    void SetStagesReady()
    {
      m_stages_ready = true;

      PixelShaderUid ps_uid = m_uid.ps_uid;
      ClearUnusedPixelShaderUidBits(m_shader_cache->m_api_type, m_shader_cache->m_host_config,
                                    &ps_uid);

      if (auto holder = m_shader_cache->m_ps_cache.GetHolder(ps_uid, m_custom_shaders))
      {
        // If the pixel shader is no longer pending compilation
        // and the shader compilation succeeded, set
        // the pipeline to use the new pixel shader.
        // Otherwise, use the existing shader.
        if (!holder->pending && holder->value.get())
        {
          m_config.pixel_shader = holder->value.get();
        }
        m_stages_ready &= !holder->pending;
      }
      else
      {
        m_stages_ready &= false;
        m_shader_cache->QueuePixelShaderCompile(ps_uid, m_custom_shaders);
      }
    }

    bool Compile() override
    {
      if (m_stages_ready)
      {
        m_pipeline = g_gfx->CreatePipeline(m_config);
      }
      return true;
    }

    void Retrieve() override
    {
      if (m_stages_ready)
      {
        m_shader_cache->NotifyPipelineFinished(m_iterator, std::move(m_pipeline));
      }
      else
      {
        // Re-queue for next frame.
        auto wi = m_shader_cache->m_async_shader_compiler->CreateWorkItem<PipelineWorkItem>(
            m_shader_cache, m_uid, m_custom_shaders, m_iterator, m_config);
        m_shader_cache->m_async_shader_compiler->QueueWorkItem(std::move(wi), 0);
      }
    }

  private:
    CustomShaderCache* m_shader_cache;
    std::unique_ptr<AbstractPipeline> m_pipeline;
    VideoCommon::GXPipelineUid m_uid;
    PipelineIterator m_iterator;
    AbstractPipelineConfig m_config;
    CustomShaderInstance m_custom_shaders;
    bool m_stages_ready;
  };

  auto list_iter = m_pipeline_cache.InsertElement(uid, custom_shaders);
  auto work_item = m_async_shader_compiler->CreateWorkItem<PipelineWorkItem>(
      this, uid, custom_shaders, list_iter, pipeline_config);
  m_async_shader_compiler->QueueWorkItem(std::move(work_item), 0);
}

void CustomShaderCache::AsyncCreatePipeline(const VideoCommon::GXUberPipelineUid& uid,

                                            const CustomShaderInstance& custom_shaders,
                                            const AbstractPipelineConfig& pipeline_config)
{
  class PipelineWorkItem final : public VideoCommon::AsyncShaderCompiler::WorkItem
  {
  public:
    PipelineWorkItem(CustomShaderCache* shader_cache, const VideoCommon::GXUberPipelineUid& uid,
                     const CustomShaderInstance& custom_shaders, UberPipelineIterator iterator,
                     const AbstractPipelineConfig& pipeline_config)
        : m_shader_cache(shader_cache), m_uid(uid), m_iterator(iterator), m_config(pipeline_config),
          m_custom_shaders(custom_shaders)
    {
      SetStagesReady();
    }

    void SetStagesReady()
    {
      m_stages_ready = true;

      UberShader::PixelShaderUid ps_uid = m_uid.ps_uid;
      ClearUnusedPixelShaderUidBits(m_shader_cache->m_api_type, m_shader_cache->m_host_config,
                                    &ps_uid);

      if (auto holder = m_shader_cache->m_uber_ps_cache.GetHolder(ps_uid, m_custom_shaders))
      {
        if (!holder->pending && holder->value.get())
        {
          m_config.pixel_shader = holder->value.get();
        }
        m_stages_ready &= !holder->pending;
      }
      else
      {
        m_stages_ready &= false;
        m_shader_cache->QueuePixelShaderCompile(ps_uid, m_custom_shaders);
      }
    }

    bool Compile() override
    {
      if (m_stages_ready)
      {
        if (m_config.pixel_shader == nullptr || m_config.vertex_shader == nullptr)
          return false;

        m_pipeline = g_gfx->CreatePipeline(m_config);
      }
      return true;
    }

    void Retrieve() override
    {
      if (m_stages_ready)
      {
        m_shader_cache->NotifyPipelineFinished(m_iterator, std::move(m_pipeline));
      }
      else
      {
        // Re-queue for next frame.
        auto wi = m_shader_cache->m_async_uber_shader_compiler->CreateWorkItem<PipelineWorkItem>(
            m_shader_cache, m_uid, m_custom_shaders, m_iterator, m_config);
        m_shader_cache->m_async_uber_shader_compiler->QueueWorkItem(std::move(wi), 0);
      }
    }

  private:
    CustomShaderCache* m_shader_cache;
    std::unique_ptr<AbstractPipeline> m_pipeline;
    VideoCommon::GXUberPipelineUid m_uid;
    UberPipelineIterator m_iterator;
    AbstractPipelineConfig m_config;
    CustomShaderInstance m_custom_shaders;
    bool m_stages_ready;
  };

  auto list_iter = m_uber_pipeline_cache.InsertElement(uid, custom_shaders);
  auto work_item = m_async_uber_shader_compiler->CreateWorkItem<PipelineWorkItem>(
      this, uid, custom_shaders, list_iter, pipeline_config);
  m_async_uber_shader_compiler->QueueWorkItem(std::move(work_item), 0);
}

void CustomShaderCache::NotifyPipelineFinished(PipelineIterator iterator,
                                               std::unique_ptr<AbstractPipeline> pipeline)
{
  iterator->second.pending = false;
  iterator->second.value = std::move(pipeline);
}

void CustomShaderCache::NotifyPipelineFinished(UberPipelineIterator iterator,
                                               std::unique_ptr<AbstractPipeline> pipeline)
{
  iterator->second.pending = false;
  iterator->second.value = std::move(pipeline);
}

void CustomShaderCache::QueuePixelShaderCompile(const PixelShaderUid& uid,

                                                const CustomShaderInstance& custom_shaders)
{
  class PixelShaderWorkItem final : public VideoCommon::AsyncShaderCompiler::WorkItem
  {
  public:
    PixelShaderWorkItem(CustomShaderCache* shader_cache, const PixelShaderUid& uid,
                        const CustomShaderInstance& custom_shaders, PixelShaderIterator iter)
        : m_shader_cache(shader_cache), m_uid(uid), m_custom_shaders(custom_shaders), m_iter(iter)
    {
    }

    bool Compile() override
    {
      m_shader = m_shader_cache->CompilePixelShader(m_uid, m_custom_shaders);
      return true;
    }

    void Retrieve() override
    {
      m_shader_cache->NotifyPixelShaderFinished(m_iter, std::move(m_shader));
    }

  private:
    CustomShaderCache* m_shader_cache;
    std::unique_ptr<AbstractShader> m_shader;
    PixelShaderUid m_uid;
    CustomShaderInstance m_custom_shaders;
    PixelShaderIterator m_iter;
  };

  auto list_iter = m_ps_cache.InsertElement(uid, custom_shaders);
  auto work_item = m_async_shader_compiler->CreateWorkItem<PixelShaderWorkItem>(
      this, uid, custom_shaders, list_iter);
  m_async_shader_compiler->QueueWorkItem(std::move(work_item), 0);
}

void CustomShaderCache::QueuePixelShaderCompile(const UberShader::PixelShaderUid& uid,

                                                const CustomShaderInstance& custom_shaders)
{
  class PixelShaderWorkItem final : public VideoCommon::AsyncShaderCompiler::WorkItem
  {
  public:
    PixelShaderWorkItem(CustomShaderCache* shader_cache, const UberShader::PixelShaderUid& uid,
                        const CustomShaderInstance& custom_shaders, UberPixelShaderIterator iter)
        : m_shader_cache(shader_cache), m_uid(uid), m_custom_shaders(custom_shaders), m_iter(iter)
    {
    }

    bool Compile() override
    {
      m_shader = m_shader_cache->CompilePixelShader(m_uid, m_custom_shaders);
      return true;
    }

    void Retrieve() override
    {
      m_shader_cache->NotifyPixelShaderFinished(m_iter, std::move(m_shader));
    }

  private:
    CustomShaderCache* m_shader_cache;
    std::unique_ptr<AbstractShader> m_shader;
    UberShader::PixelShaderUid m_uid;
    CustomShaderInstance m_custom_shaders;
    UberPixelShaderIterator m_iter;
  };

  auto list_iter = m_uber_ps_cache.InsertElement(uid, custom_shaders);
  auto work_item = m_async_uber_shader_compiler->CreateWorkItem<PixelShaderWorkItem>(
      this, uid, custom_shaders, list_iter);
  m_async_uber_shader_compiler->QueueWorkItem(std::move(work_item), 0);
}

std::unique_ptr<AbstractShader>
CustomShaderCache::CompilePixelShader(const PixelShaderUid& uid,
                                      const CustomShaderInstance& custom_shaders) const
{
  const ShaderCode source_code = GeneratePixelShaderCode(
      m_api_type, m_host_config, uid.GetUidData(), custom_shaders.pixel_contents);
  return g_gfx->CreateShaderFromSource(ShaderStage::Pixel, source_code.GetBuffer(),
                                       "Custom Pixel Shader");
}

std::unique_ptr<AbstractShader>
CustomShaderCache::CompilePixelShader(const UberShader::PixelShaderUid& uid,
                                      const CustomShaderInstance& custom_shaders) const
{
  const ShaderCode source_code =
      GenPixelShader(m_api_type, m_host_config, uid.GetUidData(), custom_shaders.pixel_contents);
  return g_gfx->CreateShaderFromSource(ShaderStage::Pixel, source_code.GetBuffer(),
                                       "Custom Uber Pixel Shader");
}

void CustomShaderCache::NotifyPixelShaderFinished(PixelShaderIterator iterator,
                                                  std::unique_ptr<AbstractShader> shader)
{
  iterator->second.pending = false;
  iterator->second.value = std::move(shader);
}

void CustomShaderCache::NotifyPixelShaderFinished(UberPixelShaderIterator iterator,
                                                  std::unique_ptr<AbstractShader> shader)
{
  iterator->second.pending = false;
  iterator->second.value = std::move(shader);
}