ShaderCache.h 11 KB

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