ShaderCache.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. using Ryujinx.Graphics.GAL;
  2. using Ryujinx.Graphics.Gpu.State;
  3. using Ryujinx.Graphics.Shader;
  4. using Ryujinx.Graphics.Shader.Translation;
  5. using System;
  6. using System.Collections.Generic;
  7. namespace Ryujinx.Graphics.Gpu.Shader
  8. {
  9. /// <summary>
  10. /// Memory cache of shader code.
  11. /// </summary>
  12. class ShaderCache : IDisposable
  13. {
  14. private const TranslationFlags DefaultFlags = TranslationFlags.DebugMode;
  15. private readonly GpuContext _context;
  16. private readonly ShaderDumper _dumper;
  17. private readonly Dictionary<ulong, List<ShaderBundle>> _cpPrograms;
  18. private readonly Dictionary<ShaderAddresses, List<ShaderBundle>> _gpPrograms;
  19. /// <summary>
  20. /// Creates a new instance of the shader cache.
  21. /// </summary>
  22. /// <param name="context">GPU context that the shader cache belongs to</param>
  23. public ShaderCache(GpuContext context)
  24. {
  25. _context = context;
  26. _dumper = new ShaderDumper();
  27. _cpPrograms = new Dictionary<ulong, List<ShaderBundle>>();
  28. _gpPrograms = new Dictionary<ShaderAddresses, List<ShaderBundle>>();
  29. }
  30. /// <summary>
  31. /// Gets a compute shader from the cache.
  32. /// </summary>
  33. /// <remarks>
  34. /// This automatically translates, compiles and adds the code to the cache if not present.
  35. /// </remarks>
  36. /// <param name="state">Current GPU state</param>
  37. /// <param name="gpuVa">GPU virtual address of the binary shader code</param>
  38. /// <param name="localSizeX">Local group size X of the computer shader</param>
  39. /// <param name="localSizeY">Local group size Y of the computer shader</param>
  40. /// <param name="localSizeZ">Local group size Z of the computer shader</param>
  41. /// <param name="localMemorySize">Local memory size of the compute shader</param>
  42. /// <param name="sharedMemorySize">Shared memory size of the compute shader</param>
  43. /// <returns>Compiled compute shader code</returns>
  44. public ShaderBundle GetComputeShader(
  45. GpuState state,
  46. ulong gpuVa,
  47. int localSizeX,
  48. int localSizeY,
  49. int localSizeZ,
  50. int localMemorySize,
  51. int sharedMemorySize)
  52. {
  53. bool isCached = _cpPrograms.TryGetValue(gpuVa, out List<ShaderBundle> list);
  54. if (isCached)
  55. {
  56. foreach (ShaderBundle cachedCpShader in list)
  57. {
  58. if (IsShaderEqual(cachedCpShader, gpuVa))
  59. {
  60. return cachedCpShader;
  61. }
  62. }
  63. }
  64. ShaderCodeHolder shader = TranslateComputeShader(
  65. state,
  66. gpuVa,
  67. localSizeX,
  68. localSizeY,
  69. localSizeZ,
  70. localMemorySize,
  71. sharedMemorySize);
  72. shader.HostShader = _context.Renderer.CompileShader(shader.Program);
  73. IProgram hostProgram = _context.Renderer.CreateProgram(new IShader[] { shader.HostShader });
  74. ShaderBundle cpShader = new ShaderBundle(hostProgram, shader);
  75. if (!isCached)
  76. {
  77. list = new List<ShaderBundle>();
  78. _cpPrograms.Add(gpuVa, list);
  79. }
  80. list.Add(cpShader);
  81. return cpShader;
  82. }
  83. /// <summary>
  84. /// Gets a graphics shader program from the shader cache.
  85. /// This includes all the specified shader stages.
  86. /// </summary>
  87. /// <remarks>
  88. /// This automatically translates, compiles and adds the code to the cache if not present.
  89. /// </remarks>
  90. /// <param name="state">Current GPU state</param>
  91. /// <param name="addresses">Addresses of the shaders for each stage</param>
  92. /// <returns>Compiled graphics shader code</returns>
  93. public ShaderBundle GetGraphicsShader(GpuState state, ShaderAddresses addresses)
  94. {
  95. bool isCached = _gpPrograms.TryGetValue(addresses, out List<ShaderBundle> list);
  96. if (isCached)
  97. {
  98. foreach (ShaderBundle cachedGpShaders in list)
  99. {
  100. if (IsShaderEqual(cachedGpShaders, addresses))
  101. {
  102. return cachedGpShaders;
  103. }
  104. }
  105. }
  106. ShaderCodeHolder[] shaders = new ShaderCodeHolder[Constants.ShaderStages];
  107. if (addresses.VertexA != 0)
  108. {
  109. shaders[0] = TranslateGraphicsShader(state, ShaderStage.Vertex, addresses.Vertex, addresses.VertexA);
  110. }
  111. else
  112. {
  113. shaders[0] = TranslateGraphicsShader(state, ShaderStage.Vertex, addresses.Vertex);
  114. }
  115. shaders[1] = TranslateGraphicsShader(state, ShaderStage.TessellationControl, addresses.TessControl);
  116. shaders[2] = TranslateGraphicsShader(state, ShaderStage.TessellationEvaluation, addresses.TessEvaluation);
  117. shaders[3] = TranslateGraphicsShader(state, ShaderStage.Geometry, addresses.Geometry);
  118. shaders[4] = TranslateGraphicsShader(state, ShaderStage.Fragment, addresses.Fragment);
  119. List<IShader> hostShaders = new List<IShader>();
  120. for (int stage = 0; stage < Constants.ShaderStages; stage++)
  121. {
  122. ShaderProgram program = shaders[stage]?.Program;
  123. if (program == null)
  124. {
  125. continue;
  126. }
  127. IShader hostShader = _context.Renderer.CompileShader(program);
  128. shaders[stage].HostShader = hostShader;
  129. hostShaders.Add(hostShader);
  130. }
  131. IProgram hostProgram = _context.Renderer.CreateProgram(hostShaders.ToArray());
  132. ShaderBundle gpShaders = new ShaderBundle(hostProgram, shaders);
  133. if (!isCached)
  134. {
  135. list = new List<ShaderBundle>();
  136. _gpPrograms.Add(addresses, list);
  137. }
  138. list.Add(gpShaders);
  139. return gpShaders;
  140. }
  141. /// <summary>
  142. /// Checks if compute shader code in memory is equal to the cached shader.
  143. /// </summary>
  144. /// <param name="cpShader">Cached compute shader</param>
  145. /// <param name="gpuVa">GPU virtual address of the shader code in memory</param>
  146. /// <returns>True if the code is different, false otherwise</returns>
  147. private bool IsShaderEqual(ShaderBundle cpShader, ulong gpuVa)
  148. {
  149. return IsShaderEqual(cpShader.Shaders[0], gpuVa);
  150. }
  151. /// <summary>
  152. /// Checks if graphics shader code from all stages in memory are equal to the cached shaders.
  153. /// </summary>
  154. /// <param name="gpShaders">Cached graphics shaders</param>
  155. /// <param name="addresses">GPU virtual addresses of all enabled shader stages</param>
  156. /// <returns>True if the code is different, false otherwise</returns>
  157. private bool IsShaderEqual(ShaderBundle gpShaders, ShaderAddresses addresses)
  158. {
  159. for (int stage = 0; stage < gpShaders.Shaders.Length; stage++)
  160. {
  161. ShaderCodeHolder shader = gpShaders.Shaders[stage];
  162. ulong gpuVa = 0;
  163. switch (stage)
  164. {
  165. case 0: gpuVa = addresses.Vertex; break;
  166. case 1: gpuVa = addresses.TessControl; break;
  167. case 2: gpuVa = addresses.TessEvaluation; break;
  168. case 3: gpuVa = addresses.Geometry; break;
  169. case 4: gpuVa = addresses.Fragment; break;
  170. }
  171. if (!IsShaderEqual(shader, gpuVa, addresses.VertexA))
  172. {
  173. return false;
  174. }
  175. }
  176. return true;
  177. }
  178. /// <summary>
  179. /// Checks if the code of the specified cached shader is different from the code in memory.
  180. /// </summary>
  181. /// <param name="shader">Cached shader to compare with</param>
  182. /// <param name="gpuVa">GPU virtual address of the binary shader code</param>
  183. /// <param name="gpuVaA">Optional GPU virtual address of the "Vertex A" binary shader code</param>
  184. /// <returns>True if the code is different, false otherwise</returns>
  185. private bool IsShaderEqual(ShaderCodeHolder shader, ulong gpuVa, ulong gpuVaA = 0)
  186. {
  187. if (shader == null)
  188. {
  189. return true;
  190. }
  191. ReadOnlySpan<byte> memoryCode = _context.MemoryAccessor.GetSpan(gpuVa, shader.Code.Length);
  192. bool equals = memoryCode.SequenceEqual(shader.Code);
  193. if (equals && shader.Code2 != null)
  194. {
  195. memoryCode = _context.MemoryAccessor.GetSpan(gpuVaA, shader.Code2.Length);
  196. equals = memoryCode.SequenceEqual(shader.Code2);
  197. }
  198. return equals;
  199. }
  200. /// <summary>
  201. /// Translates the binary Maxwell shader code to something that the host API accepts.
  202. /// </summary>
  203. /// <param name="state">Current GPU state</param>
  204. /// <param name="gpuVa">GPU virtual address of the binary shader code</param>
  205. /// <param name="localSizeX">Local group size X of the computer shader</param>
  206. /// <param name="localSizeY">Local group size Y of the computer shader</param>
  207. /// <param name="localSizeZ">Local group size Z of the computer shader</param>
  208. /// <param name="localMemorySize">Local memory size of the compute shader</param>
  209. /// <param name="sharedMemorySize">Shared memory size of the compute shader</param>
  210. /// <returns>Compiled compute shader code</returns>
  211. private ShaderCodeHolder TranslateComputeShader(
  212. GpuState state,
  213. ulong gpuVa,
  214. int localSizeX,
  215. int localSizeY,
  216. int localSizeZ,
  217. int localMemorySize,
  218. int sharedMemorySize)
  219. {
  220. if (gpuVa == 0)
  221. {
  222. return null;
  223. }
  224. GpuAccessor gpuAccessor = new GpuAccessor(_context, state, localSizeX, localSizeY, localSizeZ, localMemorySize, sharedMemorySize);
  225. ShaderProgram program;
  226. program = Translator.Translate(gpuVa, gpuAccessor, DefaultFlags | TranslationFlags.Compute);
  227. byte[] code = _context.MemoryAccessor.ReadBytes(gpuVa, program.Size);
  228. _dumper.Dump(code, compute: true, out string fullPath, out string codePath);
  229. if (fullPath != null && codePath != null)
  230. {
  231. program.Prepend("// " + codePath);
  232. program.Prepend("// " + fullPath);
  233. }
  234. return new ShaderCodeHolder(program, code);
  235. }
  236. /// <summary>
  237. /// Translates the binary Maxwell shader code to something that the host API accepts.
  238. /// </summary>
  239. /// <remarks>
  240. /// This will combine the "Vertex A" and "Vertex B" shader stages, if specified, into one shader.
  241. /// </remarks>
  242. /// <param name="state">Current GPU state</param>
  243. /// <param name="stage">Shader stage</param>
  244. /// <param name="gpuVa">GPU virtual address of the shader code</param>
  245. /// <param name="gpuVaA">Optional GPU virtual address of the "Vertex A" shader code</param>
  246. /// <returns>Compiled graphics shader code</returns>
  247. private ShaderCodeHolder TranslateGraphicsShader(GpuState state, ShaderStage stage, ulong gpuVa, ulong gpuVaA = 0)
  248. {
  249. if (gpuVa == 0)
  250. {
  251. return null;
  252. }
  253. GpuAccessor gpuAccessor = new GpuAccessor(_context, state, (int)stage - 1);
  254. if (gpuVaA != 0)
  255. {
  256. ShaderProgram program = Translator.Translate(gpuVaA, gpuVa, gpuAccessor, DefaultFlags);
  257. byte[] codeA = _context.MemoryAccessor.ReadBytes(gpuVaA, program.SizeA);
  258. byte[] codeB = _context.MemoryAccessor.ReadBytes(gpuVa, program.Size);
  259. _dumper.Dump(codeA, compute: false, out string fullPathA, out string codePathA);
  260. _dumper.Dump(codeB, compute: false, out string fullPathB, out string codePathB);
  261. if (fullPathA != null && fullPathB != null && codePathA != null && codePathB != null)
  262. {
  263. program.Prepend("// " + codePathB);
  264. program.Prepend("// " + fullPathB);
  265. program.Prepend("// " + codePathA);
  266. program.Prepend("// " + fullPathA);
  267. }
  268. return new ShaderCodeHolder(program, codeB, codeA);
  269. }
  270. else
  271. {
  272. ShaderProgram program = Translator.Translate(gpuVa, gpuAccessor, DefaultFlags);
  273. byte[] code = _context.MemoryAccessor.ReadBytes(gpuVa, program.Size);
  274. _dumper.Dump(code, compute: false, out string fullPath, out string codePath);
  275. if (fullPath != null && codePath != null)
  276. {
  277. program.Prepend("// " + codePath);
  278. program.Prepend("// " + fullPath);
  279. }
  280. return new ShaderCodeHolder(program, code);
  281. }
  282. }
  283. /// <summary>
  284. /// Disposes the shader cache, deleting all the cached shaders.
  285. /// It's an error to use the shader cache after disposal.
  286. /// </summary>
  287. public void Dispose()
  288. {
  289. foreach (List<ShaderBundle> list in _cpPrograms.Values)
  290. {
  291. foreach (ShaderBundle bundle in list)
  292. {
  293. bundle.Dispose();
  294. }
  295. }
  296. foreach (List<ShaderBundle> list in _gpPrograms.Values)
  297. {
  298. foreach (ShaderBundle bundle in list)
  299. {
  300. bundle.Dispose();
  301. }
  302. }
  303. }
  304. }
  305. }