Debian Codegraph @

Source/Core/VideoCommon/ShaderCache.h

raw symbols SCIP
1 // Copyright 2018 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #pragma once
5
6 #include <array>
7 #include <cstddef>
8 #include <cstring>
9 #include <map>
10 #include <memory>
11 #include <optional>
12 #include <string>
13 #include <unordered_map>
14 #include <utility>
15
16 #include "Common/CommonTypes.h"
17 #include "Common/IOFile.h"
18 #include "Common/LinearDiskCache.h"
19
20 #include "VideoCommon/AbstractPipeline.h"
21 #include "VideoCommon/AbstractShader.h"
22 #include "VideoCommon/AsyncShaderCompiler.h"
23 #include "VideoCommon/GXPipelineTypes.h"
24 #include "VideoCommon/GeometryShaderGen.h"
25 #include "VideoCommon/PixelShaderGen.h"
26 #include "VideoCommon/RenderState.h"
27 #include "VideoCommon/TextureCacheBase.h"
28 #include "VideoCommon/TextureConversionShader.h"
29 #include "VideoCommon/TextureConverterShaderGen.h"
30 #include "VideoCommon/UberShaderPixel.h"
31 #include "VideoCommon/UberShaderVertex.h"
32 #include "VideoCommon/VertexShaderGen.h"
33 #include "VideoCommon/VideoEvents.h"
34
35 class NativeVertexFormat;
36 enum class AbstractTextureFormat : u32;
37 enum class APIType;
38 enum class TextureFormat;
39 enum class TLUTFormat;
40
41 namespace VideoCommon
42 {
43 class ShaderCache final
44 {
45 public:
46 ShaderCache();
47 ~ShaderCache();
48
49 // Perform at startup, create descriptor layouts, compiles all static shaders.
50 bool Initialize();
51 void Shutdown();
52
53 // Compiles/loads cached shaders.
54 void InitializeShaderCache();
55
56 // Changes the shader host config. Shaders should be reloaded afterwards.
57 void SetHostConfig(const ShaderHostConfig& host_config) { m_host_config.bits = host_config.bits; }
58
59 // Reloads/recreates all shaders and pipelines.
60 void Reload();
61
62 // Retrieves all pending shaders/pipelines from the async compiler.
63 void RetrieveAsyncShaders();
64
65 // Accesses ShaderGen shader caches
66 const AbstractPipeline* GetPipelineForUid(const GXPipelineUid& uid);
67 const AbstractPipeline* GetUberPipelineForUid(const GXUberPipelineUid& uid);
68
69 // Accesses ShaderGen shader caches asynchronously.
70 // The optional will be empty if this pipeline is now background compiling.
71 std::optional<const AbstractPipeline*> GetPipelineForUidAsync(const GXPipelineUid& uid);
72
73 // Shared shaders
74 const AbstractShader* GetScreenQuadVertexShader() const
75 {
76 return m_screen_quad_vertex_shader.get();
77 }
78 const AbstractShader* GetTextureCopyVertexShader() const
79 {
80 return m_texture_copy_vertex_shader.get();
81 }
82 const AbstractShader* GetEFBCopyVertexShader() const { return m_efb_copy_vertex_shader.get(); }
83 const AbstractShader* GetTexcoordGeometryShader() const
84 {
85 return m_texcoord_geometry_shader.get();
86 }
87 const AbstractShader* GetTextureCopyPixelShader() const
88 {
89 return m_texture_copy_pixel_shader.get();
90 }
91 const AbstractShader* GetColorGeometryShader() const { return m_color_geometry_shader.get(); }
92 const AbstractShader* GetColorPixelShader() const { return m_color_pixel_shader.get(); }
93
94 // EFB copy to RAM/VRAM pipelines
95 const AbstractPipeline*
96 GetEFBCopyToVRAMPipeline(const TextureConversionShaderGen::TCShaderUid& uid);
97 const AbstractPipeline* GetEFBCopyToRAMPipeline(const EFBCopyParams& uid);
98
99 // RGBA8 framebuffer copy pipelines
100 const AbstractPipeline* GetRGBA8CopyPipeline() const { return m_copy_rgba8_pipeline.get(); }
101 const AbstractPipeline* GetRGBA8StereoCopyPipeline() const
102 {
103 return m_rgba8_stereo_copy_pipeline.get();
104 }
105
106 // Palette texture conversion pipelines
107 const AbstractPipeline* GetPaletteConversionPipeline(TLUTFormat format);
108
109 // Texture reinterpret pipelines
110 const AbstractPipeline* GetTextureReinterpretPipeline(TextureFormat from_format,
111 TextureFormat to_format);
112
113 // Texture decoding compute shaders
114 const AbstractShader* GetTextureDecodingShader(TextureFormat format,
115 std::optional<TLUTFormat> palette_format);
116
117 private:
118 static constexpr size_t NUM_PALETTE_CONVERSION_SHADERS = 3;
119
120 void WaitForAsyncCompiler();
121 void LoadCaches();
122 void ClearCaches();
123 void LoadPipelineUIDCache();
124 void ClosePipelineUIDCache();
125 void CompileMissingPipelines();
126 void QueueUberShaderPipelines();
127 bool CompileSharedPipelines();
128
129 // GX shader compiler methods
130 std::unique_ptr<AbstractShader> CompileVertexShader(const VertexShaderUid& uid) const;
131 std::unique_ptr<AbstractShader>
132 CompileVertexUberShader(const UberShader::VertexShaderUid& uid) const;
133 std::unique_ptr<AbstractShader> CompilePixelShader(const PixelShaderUid& uid) const;
134 std::unique_ptr<AbstractShader>
135 CompilePixelUberShader(const UberShader::PixelShaderUid& uid) const;
136 const AbstractShader* InsertVertexShader(const VertexShaderUid& uid,
137 std::unique_ptr<AbstractShader> shader);
138 const AbstractShader* InsertVertexUberShader(const UberShader::VertexShaderUid& uid,
139 std::unique_ptr<AbstractShader> shader);
140 const AbstractShader* InsertPixelShader(const PixelShaderUid& uid,
141 std::unique_ptr<AbstractShader> shader);
142 const AbstractShader* InsertPixelUberShader(const UberShader::PixelShaderUid& uid,
143 std::unique_ptr<AbstractShader> shader);
144 const AbstractShader* CreateGeometryShader(const GeometryShaderUid& uid);
145 bool NeedsGeometryShader(const GeometryShaderUid& uid) const;
146
147 // Should we use geometry shaders for EFB copies?
148 bool UseGeometryShaderForEFBCopies() const;
149
150 // GX pipeline compiler methods
151 AbstractPipelineConfig
152 GetGXPipelineConfig(const NativeVertexFormat* vertex_format, const AbstractShader* vertex_shader,
153 const AbstractShader* geometry_shader, const AbstractShader* pixel_shader,
154 const RasterizationState& rasterization_state, const DepthState& depth_state,
155 const BlendingState& blending_state, AbstractPipelineUsage usage);
156 std::optional<AbstractPipelineConfig> GetGXPipelineConfig(const GXPipelineUid& uid);
157 std::optional<AbstractPipelineConfig> GetGXPipelineConfig(const GXUberPipelineUid& uid);
158 const AbstractPipeline* InsertGXPipeline(const GXPipelineUid& config,
159 std::unique_ptr<AbstractPipeline> pipeline);
160 const AbstractPipeline* InsertGXUberPipeline(const GXUberPipelineUid& config,
161 std::unique_ptr<AbstractPipeline> pipeline);
162 void AddSerializedGXPipelineUID(const SerializedGXPipelineUid& uid);
163 void AppendGXPipelineUID(const GXPipelineUid& config);
164
165 // ASync Compiler Methods
166 void QueueVertexShaderCompile(const VertexShaderUid& uid, u32 priority);
167 void QueueVertexUberShaderCompile(const UberShader::VertexShaderUid& uid, u32 priority);
168 void QueuePixelShaderCompile(const PixelShaderUid& uid, u32 priority);
169 void QueuePixelUberShaderCompile(const UberShader::PixelShaderUid& uid, u32 priority);
170 void QueuePipelineCompile(const GXPipelineUid& uid, u32 priority);
171 void QueueUberPipelineCompile(const GXUberPipelineUid& uid, u32 priority);
172
173 // Populating various caches.
174 template <ShaderStage stage, typename K, typename T>
175 void LoadShaderCache(T& cache, APIType api_type, const char* type, bool include_gameid);
176 template <typename T>
177 void ClearShaderCache(T& cache);
178 template <typename KeyType, typename DiskKeyType, typename T>
179 void LoadPipelineCache(T& cache, Common::LinearDiskCache<DiskKeyType, u8>& disk_cache,
180 APIType api_type, const char* type, bool include_gameid);
181 template <typename T, typename Y>
182 void ClearPipelineCache(T& cache, Y& disk_cache);
183
184 // Priorities for compiling. The lower the value, the sooner the pipeline is compiled.
185 // The shader cache is compiled last, as it is the least likely to be required. On demand
186 // shaders are always compiled before pending ubershaders, as we want to use the ubershader
187 // for as few frames as possible, otherwise we risk framerate drops.
188 enum : u32
189 {
190 COMPILE_PRIORITY_ONDEMAND_PIPELINE = 100,
191 COMPILE_PRIORITY_UBERSHADER_PIPELINE = 200,
192 COMPILE_PRIORITY_SHADERCACHE_PIPELINE = 300
193 };
194
195 // Configuration bits.
196 APIType m_api_type;
197 ShaderHostConfig m_host_config = {};
198 std::unique_ptr<AsyncShaderCompiler> m_async_shader_compiler;
199
200 // Shared shaders
201 std::unique_ptr<AbstractShader> m_screen_quad_vertex_shader;
202 std::unique_ptr<AbstractShader> m_texture_copy_vertex_shader;
203 std::unique_ptr<AbstractShader> m_efb_copy_vertex_shader;
204 std::unique_ptr<AbstractShader> m_texcoord_geometry_shader;
205 std::unique_ptr<AbstractShader> m_color_geometry_shader;
206 std::unique_ptr<AbstractShader> m_texture_copy_pixel_shader;
207 std::unique_ptr<AbstractShader> m_color_pixel_shader;
208
209 // GX Shader Caches
210 template <typename Uid>
211 struct ShaderModuleCache
212 {
213 struct Shader
214 {
215 std::unique_ptr<AbstractShader> shader;
216 bool pending = false;
217 };
218 std::map<Uid, Shader> shader_map;
219 Common::LinearDiskCache<Uid, u8> disk_cache;
220 };
221 ShaderModuleCache<VertexShaderUid> m_vs_cache;
222 ShaderModuleCache<GeometryShaderUid> m_gs_cache;
223 ShaderModuleCache<PixelShaderUid> m_ps_cache;
224 ShaderModuleCache<UberShader::VertexShaderUid> m_uber_vs_cache;
225 ShaderModuleCache<UberShader::PixelShaderUid> m_uber_ps_cache;
226
227 // GX Pipeline Caches - .first - pipeline, .second - pending
228 std::map<GXPipelineUid, std::pair<std::unique_ptr<AbstractPipeline>, bool>> m_gx_pipeline_cache;
229 std::map<GXUberPipelineUid, std::pair<std::unique_ptr<AbstractPipeline>, bool>>
230 m_gx_uber_pipeline_cache;
231 File::IOFile m_gx_pipeline_uid_cache_file;
232 Common::LinearDiskCache<SerializedGXPipelineUid, u8> m_gx_pipeline_disk_cache;
233 Common::LinearDiskCache<SerializedGXUberPipelineUid, u8> m_gx_uber_pipeline_disk_cache;
234
235 // EFB copy to VRAM/RAM pipelines
236 std::map<TextureConversionShaderGen::TCShaderUid, std::unique_ptr<AbstractPipeline>>
237 m_efb_copy_to_vram_pipelines;
238 std::map<EFBCopyParams, std::unique_ptr<AbstractPipeline>> m_efb_copy_to_ram_pipelines;
239
240 // Copy pipeline for RGBA8 textures
241 std::unique_ptr<AbstractPipeline> m_copy_rgba8_pipeline;
242 std::unique_ptr<AbstractPipeline> m_rgba8_stereo_copy_pipeline;
243
244 // Palette conversion pipelines
245 std::array<std::unique_ptr<AbstractPipeline>, NUM_PALETTE_CONVERSION_SHADERS>
246 m_palette_conversion_pipelines;
247
248 // Texture reinterpreting pipeline
249 std::map<std::pair<TextureFormat, TextureFormat>, std::unique_ptr<AbstractPipeline>>
250 m_texture_reinterpret_pipelines;
251
252 // Texture decoding shaders
253 std::map<std::pair<u32, u32>, std::unique_ptr<AbstractShader>> m_texture_decoding_shaders;
254
255 Common::EventHook m_frame_end_handler;
256 };
257
258 } // namespace VideoCommon
259
260 extern std::unique_ptr<VideoCommon::ShaderCache> g_shader_cache;