ShaderCache.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. using System.Globalization;
  8. using System.Runtime.InteropServices;
  9. namespace Ryujinx.Graphics.Gpu.Shader
  10. {
  11. class ShaderCache
  12. {
  13. private const int MaxProgramSize = 0x100000;
  14. private GpuContext _context;
  15. private ShaderDumper _dumper;
  16. private Dictionary<ulong, List<ComputeShader>> _cpPrograms;
  17. private Dictionary<ShaderAddresses, List<GraphicsShader>> _gpPrograms;
  18. public ShaderCache(GpuContext context)
  19. {
  20. _context = context;
  21. _dumper = new ShaderDumper(context);
  22. _cpPrograms = new Dictionary<ulong, List<ComputeShader>>();
  23. _gpPrograms = new Dictionary<ShaderAddresses, List<GraphicsShader>>();
  24. }
  25. public ComputeShader GetComputeShader(ulong gpuVa, int localSizeX, int localSizeY, int localSizeZ)
  26. {
  27. bool isCached = _cpPrograms.TryGetValue(gpuVa, out List<ComputeShader> list);
  28. if (isCached)
  29. {
  30. foreach (ComputeShader cachedCpShader in list)
  31. {
  32. if (!IsShaderDifferent(cachedCpShader, gpuVa))
  33. {
  34. return cachedCpShader;
  35. }
  36. }
  37. }
  38. CachedShader shader = TranslateComputeShader(gpuVa, localSizeX, localSizeY, localSizeZ);
  39. IShader hostShader = _context.Renderer.CompileShader(shader.Program);
  40. IProgram hostProgram = _context.Renderer.CreateProgram(new IShader[] { hostShader });
  41. ulong address = _context.MemoryManager.Translate(gpuVa);
  42. ComputeShader cpShader = new ComputeShader(hostProgram, shader);
  43. if (!isCached)
  44. {
  45. list = new List<ComputeShader>();
  46. _cpPrograms.Add(gpuVa, list);
  47. }
  48. list.Add(cpShader);
  49. return cpShader;
  50. }
  51. public GraphicsShader GetGraphicsShader(ShaderAddresses addresses, bool dividePosXY)
  52. {
  53. bool isCached = _gpPrograms.TryGetValue(addresses, out List<GraphicsShader> list);
  54. if (isCached)
  55. {
  56. foreach (GraphicsShader cachedGpShaders in list)
  57. {
  58. if (!IsShaderDifferent(cachedGpShaders, addresses))
  59. {
  60. return cachedGpShaders;
  61. }
  62. }
  63. }
  64. GraphicsShader gpShaders = new GraphicsShader();
  65. TranslationFlags flags =
  66. TranslationFlags.DebugMode |
  67. TranslationFlags.Unspecialized;
  68. if (dividePosXY)
  69. {
  70. flags |= TranslationFlags.DividePosXY;
  71. }
  72. if (addresses.VertexA != 0)
  73. {
  74. gpShaders.Shader[0] = TranslateGraphicsShader(flags, addresses.Vertex, addresses.VertexA);
  75. }
  76. else
  77. {
  78. gpShaders.Shader[0] = TranslateGraphicsShader(flags, addresses.Vertex);
  79. }
  80. gpShaders.Shader[1] = TranslateGraphicsShader(flags, addresses.TessControl);
  81. gpShaders.Shader[2] = TranslateGraphicsShader(flags, addresses.TessEvaluation);
  82. gpShaders.Shader[3] = TranslateGraphicsShader(flags, addresses.Geometry);
  83. gpShaders.Shader[4] = TranslateGraphicsShader(flags, addresses.Fragment);
  84. BackpropQualifiers(gpShaders);
  85. List<IShader> hostShaders = new List<IShader>();
  86. for (int stage = 0; stage < gpShaders.Shader.Length; stage++)
  87. {
  88. ShaderProgram program = gpShaders.Shader[stage].Program;
  89. if (program == null)
  90. {
  91. continue;
  92. }
  93. IShader hostShader = _context.Renderer.CompileShader(program);
  94. gpShaders.Shader[stage].Shader = hostShader;
  95. hostShaders.Add(hostShader);
  96. }
  97. gpShaders.HostProgram = _context.Renderer.CreateProgram(hostShaders.ToArray());
  98. if (!isCached)
  99. {
  100. list = new List<GraphicsShader>();
  101. _gpPrograms.Add(addresses, list);
  102. }
  103. list.Add(gpShaders);
  104. return gpShaders;
  105. }
  106. private bool IsShaderDifferent(ComputeShader cpShader, ulong gpuVa)
  107. {
  108. return IsShaderDifferent(cpShader.Shader, gpuVa);
  109. }
  110. private bool IsShaderDifferent(GraphicsShader gpShaders, ShaderAddresses addresses)
  111. {
  112. for (int stage = 0; stage < gpShaders.Shader.Length; stage++)
  113. {
  114. CachedShader shader = gpShaders.Shader[stage];
  115. if (shader.Code == null)
  116. {
  117. continue;
  118. }
  119. ulong gpuVa = 0;
  120. switch (stage)
  121. {
  122. case 0: gpuVa = addresses.Vertex; break;
  123. case 1: gpuVa = addresses.TessControl; break;
  124. case 2: gpuVa = addresses.TessEvaluation; break;
  125. case 3: gpuVa = addresses.Geometry; break;
  126. case 4: gpuVa = addresses.Fragment; break;
  127. }
  128. if (IsShaderDifferent(shader, gpuVa))
  129. {
  130. return true;
  131. }
  132. }
  133. return false;
  134. }
  135. private bool IsShaderDifferent(CachedShader shader, ulong gpuVa)
  136. {
  137. for (int index = 0; index < shader.Code.Length; index++)
  138. {
  139. if (_context.MemoryAccessor.ReadInt32(gpuVa + (ulong)index * 4) != shader.Code[index])
  140. {
  141. return true;
  142. }
  143. }
  144. return false;
  145. }
  146. private CachedShader TranslateComputeShader(ulong gpuVa, int localSizeX, int localSizeY, int localSizeZ)
  147. {
  148. if (gpuVa == 0)
  149. {
  150. return null;
  151. }
  152. ShaderProgram program;
  153. const TranslationFlags flags =
  154. TranslationFlags.Compute |
  155. TranslationFlags.DebugMode |
  156. TranslationFlags.Unspecialized;
  157. Span<byte> code = _context.MemoryAccessor.Read(gpuVa, MaxProgramSize);
  158. program = Translator.Translate(code, GetShaderCapabilities(), flags);
  159. int[] codeCached = MemoryMarshal.Cast<byte, int>(code.Slice(0, program.Size)).ToArray();
  160. program.Replace(DefineNames.LocalSizeX, localSizeX.ToString(CultureInfo.InvariantCulture));
  161. program.Replace(DefineNames.LocalSizeY, localSizeY.ToString(CultureInfo.InvariantCulture));
  162. program.Replace(DefineNames.LocalSizeZ, localSizeZ.ToString(CultureInfo.InvariantCulture));
  163. _dumper.Dump(code, compute: true, out string fullPath, out string codePath);
  164. if (fullPath != null && codePath != null)
  165. {
  166. program.Prepend("// " + codePath);
  167. program.Prepend("// " + fullPath);
  168. }
  169. return new CachedShader(program, codeCached);
  170. }
  171. private CachedShader TranslateGraphicsShader(TranslationFlags flags, ulong gpuVa, ulong gpuVaA = 0)
  172. {
  173. if (gpuVa == 0)
  174. {
  175. return new CachedShader(null, null);
  176. }
  177. ShaderProgram program;
  178. int[] codeCached = null;
  179. if (gpuVaA != 0)
  180. {
  181. Span<byte> codeA = _context.MemoryAccessor.Read(gpuVaA, MaxProgramSize);
  182. Span<byte> codeB = _context.MemoryAccessor.Read(gpuVa, MaxProgramSize);
  183. program = Translator.Translate(codeA, codeB, GetShaderCapabilities(), flags);
  184. // TODO: We should also check "codeA" into account.
  185. codeCached = MemoryMarshal.Cast<byte, int>(codeB.Slice(0, program.Size)).ToArray();
  186. _dumper.Dump(codeA, compute: false, out string fullPathA, out string codePathA);
  187. _dumper.Dump(codeB, compute: false, out string fullPathB, out string codePathB);
  188. if (fullPathA != null && fullPathB != null && codePathA != null && codePathB != null)
  189. {
  190. program.Prepend("// " + codePathB);
  191. program.Prepend("// " + fullPathB);
  192. program.Prepend("// " + codePathA);
  193. program.Prepend("// " + fullPathA);
  194. }
  195. }
  196. else
  197. {
  198. Span<byte> code = _context.MemoryAccessor.Read(gpuVa, MaxProgramSize);
  199. program = Translator.Translate(code, GetShaderCapabilities(), flags);
  200. codeCached = MemoryMarshal.Cast<byte, int>(code.Slice(0, program.Size)).ToArray();
  201. _dumper.Dump(code, compute: false, out string fullPath, out string codePath);
  202. if (fullPath != null && codePath != null)
  203. {
  204. program.Prepend("// " + codePath);
  205. program.Prepend("// " + fullPath);
  206. }
  207. }
  208. if (program.Stage == ShaderStage.Geometry)
  209. {
  210. PrimitiveType primitiveType = _context.Methods.PrimitiveType;
  211. string inPrimitive = "points";
  212. switch (primitiveType)
  213. {
  214. case PrimitiveType.Points:
  215. inPrimitive = "points";
  216. break;
  217. case PrimitiveType.Lines:
  218. case PrimitiveType.LineLoop:
  219. case PrimitiveType.LineStrip:
  220. inPrimitive = "lines";
  221. break;
  222. case PrimitiveType.LinesAdjacency:
  223. case PrimitiveType.LineStripAdjacency:
  224. inPrimitive = "lines_adjacency";
  225. break;
  226. case PrimitiveType.Triangles:
  227. case PrimitiveType.TriangleStrip:
  228. case PrimitiveType.TriangleFan:
  229. inPrimitive = "triangles";
  230. break;
  231. case PrimitiveType.TrianglesAdjacency:
  232. case PrimitiveType.TriangleStripAdjacency:
  233. inPrimitive = "triangles_adjacency";
  234. break;
  235. }
  236. program.Replace(DefineNames.InputTopologyName, inPrimitive);
  237. }
  238. ulong address = _context.MemoryManager.Translate(gpuVa);
  239. return new CachedShader(program, codeCached);
  240. }
  241. private void BackpropQualifiers(GraphicsShader program)
  242. {
  243. ShaderProgram fragmentShader = program.Shader[4].Program;
  244. bool isFirst = true;
  245. for (int stage = 3; stage >= 0; stage--)
  246. {
  247. if (program.Shader[stage].Program == null)
  248. {
  249. continue;
  250. }
  251. // We need to iterate backwards, since we do name replacement,
  252. // and it would otherwise replace a subset of the longer names.
  253. for (int attr = 31; attr >= 0; attr--)
  254. {
  255. string iq = fragmentShader?.Info.InterpolationQualifiers[attr].ToGlslQualifier() ?? string.Empty;
  256. if (isFirst && iq != string.Empty)
  257. {
  258. program.Shader[stage].Program.Replace($"{DefineNames.OutQualifierPrefixName}{attr}", iq);
  259. }
  260. else
  261. {
  262. program.Shader[stage].Program.Replace($"{DefineNames.OutQualifierPrefixName}{attr} ", string.Empty);
  263. }
  264. }
  265. isFirst = false;
  266. }
  267. }
  268. private ShaderCapabilities GetShaderCapabilities()
  269. {
  270. return new ShaderCapabilities(
  271. _context.Capabilities.MaximumViewportDimensions,
  272. _context.Capabilities.MaximumComputeSharedMemorySize,
  273. _context.Capabilities.StorageBufferOffsetAlignment);
  274. }
  275. }
  276. }