ShaderCache.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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 sharedMemorySize, 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, sharedMemorySize, 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 sharedMemorySize, 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.SharedMemorySize, sharedMemorySize.ToString(CultureInfo.InvariantCulture));
  161. program.Replace(DefineNames.LocalSizeX, localSizeX.ToString(CultureInfo.InvariantCulture));
  162. program.Replace(DefineNames.LocalSizeY, localSizeY.ToString(CultureInfo.InvariantCulture));
  163. program.Replace(DefineNames.LocalSizeZ, localSizeZ.ToString(CultureInfo.InvariantCulture));
  164. _dumper.Dump(code, compute: true, out string fullPath, out string codePath);
  165. if (fullPath != null && codePath != null)
  166. {
  167. program.Prepend("// " + codePath);
  168. program.Prepend("// " + fullPath);
  169. }
  170. return new CachedShader(program, codeCached);
  171. }
  172. private CachedShader TranslateGraphicsShader(TranslationFlags flags, ulong gpuVa, ulong gpuVaA = 0)
  173. {
  174. if (gpuVa == 0)
  175. {
  176. return new CachedShader(null, null);
  177. }
  178. ShaderProgram program;
  179. int[] codeCached = null;
  180. if (gpuVaA != 0)
  181. {
  182. Span<byte> codeA = _context.MemoryAccessor.Read(gpuVaA, MaxProgramSize);
  183. Span<byte> codeB = _context.MemoryAccessor.Read(gpuVa, MaxProgramSize);
  184. program = Translator.Translate(codeA, codeB, GetShaderCapabilities(), flags);
  185. // TODO: We should also check "codeA" into account.
  186. codeCached = MemoryMarshal.Cast<byte, int>(codeB.Slice(0, program.Size)).ToArray();
  187. _dumper.Dump(codeA, compute: false, out string fullPathA, out string codePathA);
  188. _dumper.Dump(codeB, compute: false, out string fullPathB, out string codePathB);
  189. if (fullPathA != null && fullPathB != null && codePathA != null && codePathB != null)
  190. {
  191. program.Prepend("// " + codePathB);
  192. program.Prepend("// " + fullPathB);
  193. program.Prepend("// " + codePathA);
  194. program.Prepend("// " + fullPathA);
  195. }
  196. }
  197. else
  198. {
  199. Span<byte> code = _context.MemoryAccessor.Read(gpuVa, MaxProgramSize);
  200. program = Translator.Translate(code, GetShaderCapabilities(), flags);
  201. codeCached = MemoryMarshal.Cast<byte, int>(code.Slice(0, program.Size)).ToArray();
  202. _dumper.Dump(code, compute: false, out string fullPath, out string codePath);
  203. if (fullPath != null && codePath != null)
  204. {
  205. program.Prepend("// " + codePath);
  206. program.Prepend("// " + fullPath);
  207. }
  208. }
  209. if (program.Stage == ShaderStage.Geometry)
  210. {
  211. PrimitiveType primitiveType = _context.Methods.PrimitiveType;
  212. string inPrimitive = "points";
  213. switch (primitiveType)
  214. {
  215. case PrimitiveType.Points:
  216. inPrimitive = "points";
  217. break;
  218. case PrimitiveType.Lines:
  219. case PrimitiveType.LineLoop:
  220. case PrimitiveType.LineStrip:
  221. inPrimitive = "lines";
  222. break;
  223. case PrimitiveType.LinesAdjacency:
  224. case PrimitiveType.LineStripAdjacency:
  225. inPrimitive = "lines_adjacency";
  226. break;
  227. case PrimitiveType.Triangles:
  228. case PrimitiveType.TriangleStrip:
  229. case PrimitiveType.TriangleFan:
  230. inPrimitive = "triangles";
  231. break;
  232. case PrimitiveType.TrianglesAdjacency:
  233. case PrimitiveType.TriangleStripAdjacency:
  234. inPrimitive = "triangles_adjacency";
  235. break;
  236. }
  237. program.Replace(DefineNames.InputTopologyName, inPrimitive);
  238. }
  239. ulong address = _context.MemoryManager.Translate(gpuVa);
  240. return new CachedShader(program, codeCached);
  241. }
  242. private void BackpropQualifiers(GraphicsShader program)
  243. {
  244. ShaderProgram fragmentShader = program.Shader[4].Program;
  245. bool isFirst = true;
  246. for (int stage = 3; stage >= 0; stage--)
  247. {
  248. if (program.Shader[stage].Program == null)
  249. {
  250. continue;
  251. }
  252. // We need to iterate backwards, since we do name replacement,
  253. // and it would otherwise replace a subset of the longer names.
  254. for (int attr = 31; attr >= 0; attr--)
  255. {
  256. string iq = fragmentShader?.Info.InterpolationQualifiers[attr].ToGlslQualifier() ?? string.Empty;
  257. if (isFirst && iq != string.Empty)
  258. {
  259. program.Shader[stage].Program.Replace($"{DefineNames.OutQualifierPrefixName}{attr}", iq);
  260. }
  261. else
  262. {
  263. program.Shader[stage].Program.Replace($"{DefineNames.OutQualifierPrefixName}{attr} ", string.Empty);
  264. }
  265. }
  266. isFirst = false;
  267. }
  268. }
  269. private ShaderCapabilities GetShaderCapabilities()
  270. {
  271. return new ShaderCapabilities(
  272. _context.Capabilities.MaximumViewportDimensions,
  273. _context.Capabilities.MaximumComputeSharedMemorySize,
  274. _context.Capabilities.StorageBufferOffsetAlignment);
  275. }
  276. }
  277. }