ShaderCache.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. using Ryujinx.Common.Logging;
  2. using Ryujinx.Graphics.GAL;
  3. using Ryujinx.Graphics.Gpu.Image;
  4. using Ryujinx.Graphics.Gpu.State;
  5. using Ryujinx.Graphics.Shader;
  6. using Ryujinx.Graphics.Shader.Translation;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Runtime.InteropServices;
  10. namespace Ryujinx.Graphics.Gpu.Shader
  11. {
  12. using TextureDescriptor = Image.TextureDescriptor;
  13. /// <summary>
  14. /// Memory cache of shader code.
  15. /// </summary>
  16. class ShaderCache : IDisposable
  17. {
  18. private const int MaxProgramSize = 0x100000;
  19. private const TranslationFlags DefaultFlags = TranslationFlags.DebugMode;
  20. private GpuContext _context;
  21. private ShaderDumper _dumper;
  22. private Dictionary<ulong, List<ComputeShader>> _cpPrograms;
  23. private Dictionary<ShaderAddresses, List<GraphicsShader>> _gpPrograms;
  24. /// <summary>
  25. /// Creates a new instance of the shader cache.
  26. /// </summary>
  27. /// <param name="context">GPU context that the shader cache belongs to</param>
  28. public ShaderCache(GpuContext context)
  29. {
  30. _context = context;
  31. _dumper = new ShaderDumper();
  32. _cpPrograms = new Dictionary<ulong, List<ComputeShader>>();
  33. _gpPrograms = new Dictionary<ShaderAddresses, List<GraphicsShader>>();
  34. }
  35. /// <summary>
  36. /// Gets a compute shader from the cache.
  37. /// </summary>
  38. /// <remarks>
  39. /// This automatically translates, compiles and adds the code to the cache if not present.
  40. /// </remarks>
  41. /// <param name="state">Current GPU state</param>
  42. /// <param name="gpuVa">GPU virtual address of the binary shader code</param>
  43. /// <param name="localSizeX">Local group size X of the computer shader</param>
  44. /// <param name="localSizeY">Local group size Y of the computer shader</param>
  45. /// <param name="localSizeZ">Local group size Z of the computer shader</param>
  46. /// <param name="localMemorySize">Local memory size of the compute shader</param>
  47. /// <param name="sharedMemorySize">Shared memory size of the compute shader</param>
  48. /// <returns>Compiled compute shader code</returns>
  49. public ComputeShader GetComputeShader(
  50. GpuState state,
  51. ulong gpuVa,
  52. int localSizeX,
  53. int localSizeY,
  54. int localSizeZ,
  55. int localMemorySize,
  56. int sharedMemorySize)
  57. {
  58. bool isCached = _cpPrograms.TryGetValue(gpuVa, out List<ComputeShader> list);
  59. if (isCached)
  60. {
  61. foreach (ComputeShader cachedCpShader in list)
  62. {
  63. if (!IsShaderDifferent(cachedCpShader, gpuVa))
  64. {
  65. return cachedCpShader;
  66. }
  67. }
  68. }
  69. CachedShader shader = TranslateComputeShader(
  70. state,
  71. gpuVa,
  72. localSizeX,
  73. localSizeY,
  74. localSizeZ,
  75. localMemorySize,
  76. sharedMemorySize);
  77. shader.HostShader = _context.Renderer.CompileShader(shader.Program);
  78. IProgram hostProgram = _context.Renderer.CreateProgram(new IShader[] { shader.HostShader });
  79. ComputeShader cpShader = new ComputeShader(hostProgram, shader);
  80. if (!isCached)
  81. {
  82. list = new List<ComputeShader>();
  83. _cpPrograms.Add(gpuVa, list);
  84. }
  85. list.Add(cpShader);
  86. return cpShader;
  87. }
  88. /// <summary>
  89. /// Gets a graphics shader program from the shader cache.
  90. /// This includes all the specified shader stages.
  91. /// </summary>
  92. /// <remarks>
  93. /// This automatically translates, compiles and adds the code to the cache if not present.
  94. /// </remarks>
  95. /// <param name="state">Current GPU state</param>
  96. /// <param name="addresses">Addresses of the shaders for each stage</param>
  97. /// <returns>Compiled graphics shader code</returns>
  98. public GraphicsShader GetGraphicsShader(GpuState state, ShaderAddresses addresses)
  99. {
  100. bool isCached = _gpPrograms.TryGetValue(addresses, out List<GraphicsShader> list);
  101. if (isCached)
  102. {
  103. foreach (GraphicsShader cachedGpShaders in list)
  104. {
  105. if (!IsShaderDifferent(cachedGpShaders, addresses))
  106. {
  107. return cachedGpShaders;
  108. }
  109. }
  110. }
  111. GraphicsShader gpShaders = new GraphicsShader();
  112. if (addresses.VertexA != 0)
  113. {
  114. gpShaders.Shaders[0] = TranslateGraphicsShader(state, ShaderStage.Vertex, addresses.Vertex, addresses.VertexA);
  115. }
  116. else
  117. {
  118. gpShaders.Shaders[0] = TranslateGraphicsShader(state, ShaderStage.Vertex, addresses.Vertex);
  119. }
  120. gpShaders.Shaders[1] = TranslateGraphicsShader(state, ShaderStage.TessellationControl, addresses.TessControl);
  121. gpShaders.Shaders[2] = TranslateGraphicsShader(state, ShaderStage.TessellationEvaluation, addresses.TessEvaluation);
  122. gpShaders.Shaders[3] = TranslateGraphicsShader(state, ShaderStage.Geometry, addresses.Geometry);
  123. gpShaders.Shaders[4] = TranslateGraphicsShader(state, ShaderStage.Fragment, addresses.Fragment);
  124. List<IShader> hostShaders = new List<IShader>();
  125. for (int stage = 0; stage < gpShaders.Shaders.Length; stage++)
  126. {
  127. ShaderProgram program = gpShaders.Shaders[stage]?.Program;
  128. if (program == null)
  129. {
  130. continue;
  131. }
  132. IShader hostShader = _context.Renderer.CompileShader(program);
  133. gpShaders.Shaders[stage].HostShader = hostShader;
  134. hostShaders.Add(hostShader);
  135. }
  136. gpShaders.HostProgram = _context.Renderer.CreateProgram(hostShaders.ToArray());
  137. if (!isCached)
  138. {
  139. list = new List<GraphicsShader>();
  140. _gpPrograms.Add(addresses, list);
  141. }
  142. list.Add(gpShaders);
  143. return gpShaders;
  144. }
  145. /// <summary>
  146. /// Checks if compute shader code in memory is different from the cached shader.
  147. /// </summary>
  148. /// <param name="cpShader">Cached compute shader</param>
  149. /// <param name="gpuVa">GPU virtual address of the shader code in memory</param>
  150. /// <returns>True if the code is different, false otherwise</returns>
  151. private bool IsShaderDifferent(ComputeShader cpShader, ulong gpuVa)
  152. {
  153. return IsShaderDifferent(cpShader.Shader, gpuVa);
  154. }
  155. /// <summary>
  156. /// Checks if graphics shader code from all stages in memory is different from the cached shaders.
  157. /// </summary>
  158. /// <param name="gpShaders">Cached graphics shaders</param>
  159. /// <param name="addresses">GPU virtual addresses of all enabled shader stages</param>
  160. /// <returns>True if the code is different, false otherwise</returns>
  161. private bool IsShaderDifferent(GraphicsShader gpShaders, ShaderAddresses addresses)
  162. {
  163. for (int stage = 0; stage < gpShaders.Shaders.Length; stage++)
  164. {
  165. CachedShader shader = gpShaders.Shaders[stage];
  166. ulong gpuVa = 0;
  167. switch (stage)
  168. {
  169. case 0: gpuVa = addresses.Vertex; break;
  170. case 1: gpuVa = addresses.TessControl; break;
  171. case 2: gpuVa = addresses.TessEvaluation; break;
  172. case 3: gpuVa = addresses.Geometry; break;
  173. case 4: gpuVa = addresses.Fragment; break;
  174. }
  175. if (IsShaderDifferent(shader, gpuVa))
  176. {
  177. return true;
  178. }
  179. }
  180. return false;
  181. }
  182. /// <summary>
  183. /// Checks if the code of the specified cached shader is different from the code in memory.
  184. /// </summary>
  185. /// <param name="shader">Cached shader to compare with</param>
  186. /// <param name="gpuVa">GPU virtual address of the binary shader code</param>
  187. /// <returns>True if the code is different, false otherwise</returns>
  188. private bool IsShaderDifferent(CachedShader shader, ulong gpuVa)
  189. {
  190. if (shader == null)
  191. {
  192. return false;
  193. }
  194. ReadOnlySpan<byte> memoryCode = _context.MemoryAccessor.GetSpan(gpuVa, (ulong)shader.Code.Length * 4);
  195. return !MemoryMarshal.Cast<byte, int>(memoryCode).SequenceEqual(shader.Code);
  196. }
  197. /// <summary>
  198. /// Translates the binary Maxwell shader code to something that the host API accepts.
  199. /// </summary>
  200. /// <param name="state">Current GPU state</param>
  201. /// <param name="gpuVa">GPU virtual address of the binary shader code</param>
  202. /// <param name="localSizeX">Local group size X of the computer shader</param>
  203. /// <param name="localSizeY">Local group size Y of the computer shader</param>
  204. /// <param name="localSizeZ">Local group size Z of the computer shader</param>
  205. /// <param name="localMemorySize">Local memory size of the compute shader</param>
  206. /// <param name="sharedMemorySize">Shared memory size of the compute shader</param>
  207. /// <returns>Compiled compute shader code</returns>
  208. private CachedShader TranslateComputeShader(
  209. GpuState state,
  210. ulong gpuVa,
  211. int localSizeX,
  212. int localSizeY,
  213. int localSizeZ,
  214. int localMemorySize,
  215. int sharedMemorySize)
  216. {
  217. if (gpuVa == 0)
  218. {
  219. return null;
  220. }
  221. int QueryInfo(QueryInfoName info, int index)
  222. {
  223. return info switch
  224. {
  225. QueryInfoName.ComputeLocalSizeX
  226. => localSizeX,
  227. QueryInfoName.ComputeLocalSizeY
  228. => localSizeY,
  229. QueryInfoName.ComputeLocalSizeZ
  230. => localSizeZ,
  231. QueryInfoName.ComputeLocalMemorySize
  232. => localMemorySize,
  233. QueryInfoName.ComputeSharedMemorySize
  234. => sharedMemorySize,
  235. QueryInfoName.TextureFormat
  236. => (int)QueryComputeTextureFormat(state, index),
  237. _
  238. => QueryInfoCommon(info)
  239. };
  240. }
  241. TranslatorCallbacks callbacks = new TranslatorCallbacks(QueryInfo, PrintLog);
  242. ShaderProgram program;
  243. ReadOnlySpan<byte> code = _context.MemoryAccessor.GetSpan(gpuVa, MaxProgramSize);
  244. program = Translator.Translate(code, callbacks, DefaultFlags | TranslationFlags.Compute);
  245. int[] codeCached = MemoryMarshal.Cast<byte, int>(code.Slice(0, program.Size)).ToArray();
  246. _dumper.Dump(code, compute: true, out string fullPath, out string codePath);
  247. if (fullPath != null && codePath != null)
  248. {
  249. program.Prepend("// " + codePath);
  250. program.Prepend("// " + fullPath);
  251. }
  252. return new CachedShader(program, codeCached);
  253. }
  254. /// <summary>
  255. /// Translates the binary Maxwell shader code to something that the host API accepts.
  256. /// </summary>
  257. /// <remarks>
  258. /// This will combine the "Vertex A" and "Vertex B" shader stages, if specified, into one shader.
  259. /// </remarks>
  260. /// <param name="state">Current GPU state</param>
  261. /// <param name="stage">Shader stage</param>
  262. /// <param name="gpuVa">GPU virtual address of the shader code</param>
  263. /// <param name="gpuVaA">Optional GPU virtual address of the "Vertex A" shader code</param>
  264. /// <returns>Compiled graphics shader code</returns>
  265. private CachedShader TranslateGraphicsShader(GpuState state, ShaderStage stage, ulong gpuVa, ulong gpuVaA = 0)
  266. {
  267. if (gpuVa == 0)
  268. {
  269. return null;
  270. }
  271. int QueryInfo(QueryInfoName info, int index)
  272. {
  273. return info switch
  274. {
  275. QueryInfoName.IsTextureBuffer
  276. => Convert.ToInt32(QueryIsTextureBuffer(state, (int)stage - 1, index)),
  277. QueryInfoName.IsTextureRectangle
  278. => Convert.ToInt32(QueryIsTextureRectangle(state, (int)stage - 1, index)),
  279. QueryInfoName.PrimitiveTopology
  280. => (int)QueryPrimitiveTopology(),
  281. QueryInfoName.TextureFormat
  282. => (int)QueryGraphicsTextureFormat(state, (int)stage - 1, index),
  283. _
  284. => QueryInfoCommon(info)
  285. };
  286. }
  287. TranslatorCallbacks callbacks = new TranslatorCallbacks(QueryInfo, PrintLog);
  288. ShaderProgram program;
  289. int[] codeCached = null;
  290. if (gpuVaA != 0)
  291. {
  292. ReadOnlySpan<byte> codeA = _context.MemoryAccessor.GetSpan(gpuVaA, MaxProgramSize);
  293. ReadOnlySpan<byte> codeB = _context.MemoryAccessor.GetSpan(gpuVa, MaxProgramSize);
  294. program = Translator.Translate(codeA, codeB, callbacks, DefaultFlags);
  295. // TODO: We should also take "codeA" into account.
  296. codeCached = MemoryMarshal.Cast<byte, int>(codeB.Slice(0, program.Size)).ToArray();
  297. _dumper.Dump(codeA, compute: false, out string fullPathA, out string codePathA);
  298. _dumper.Dump(codeB, compute: false, out string fullPathB, out string codePathB);
  299. if (fullPathA != null && fullPathB != null && codePathA != null && codePathB != null)
  300. {
  301. program.Prepend("// " + codePathB);
  302. program.Prepend("// " + fullPathB);
  303. program.Prepend("// " + codePathA);
  304. program.Prepend("// " + fullPathA);
  305. }
  306. }
  307. else
  308. {
  309. ReadOnlySpan<byte> code = _context.MemoryAccessor.GetSpan(gpuVa, MaxProgramSize);
  310. program = Translator.Translate(code, callbacks, DefaultFlags);
  311. codeCached = MemoryMarshal.Cast<byte, int>(code.Slice(0, program.Size)).ToArray();
  312. _dumper.Dump(code, compute: false, out string fullPath, out string codePath);
  313. if (fullPath != null && codePath != null)
  314. {
  315. program.Prepend("// " + codePath);
  316. program.Prepend("// " + fullPath);
  317. }
  318. }
  319. ulong address = _context.MemoryManager.Translate(gpuVa);
  320. return new CachedShader(program, codeCached);
  321. }
  322. /// <summary>
  323. /// Gets the primitive topology for the current draw.
  324. /// This is required by geometry shaders.
  325. /// </summary>
  326. /// <returns>Primitive topology</returns>
  327. private InputTopology QueryPrimitiveTopology()
  328. {
  329. switch (_context.Methods.PrimitiveType)
  330. {
  331. case PrimitiveType.Points:
  332. return InputTopology.Points;
  333. case PrimitiveType.Lines:
  334. case PrimitiveType.LineLoop:
  335. case PrimitiveType.LineStrip:
  336. return InputTopology.Lines;
  337. case PrimitiveType.LinesAdjacency:
  338. case PrimitiveType.LineStripAdjacency:
  339. return InputTopology.LinesAdjacency;
  340. case PrimitiveType.Triangles:
  341. case PrimitiveType.TriangleStrip:
  342. case PrimitiveType.TriangleFan:
  343. return InputTopology.Triangles;
  344. case PrimitiveType.TrianglesAdjacency:
  345. case PrimitiveType.TriangleStripAdjacency:
  346. return InputTopology.TrianglesAdjacency;
  347. }
  348. return InputTopology.Points;
  349. }
  350. /// <summary>
  351. /// Check if the target of a given texture is texture buffer.
  352. /// This is required as 1D textures and buffer textures shares the same sampler type on binary shader code,
  353. /// but not on GLSL.
  354. /// </summary>
  355. /// <param name="state">Current GPU state</param>
  356. /// <param name="stageIndex">Index of the shader stage</param>
  357. /// <param name="index">Index of the texture (this is the shader "fake" handle)</param>
  358. /// <returns>True if the texture is a buffer texture, false otherwise</returns>
  359. private bool QueryIsTextureBuffer(GpuState state, int stageIndex, int index)
  360. {
  361. return GetGraphicsTextureDescriptor(state, stageIndex, index).UnpackTextureTarget() == TextureTarget.TextureBuffer;
  362. }
  363. /// <summary>
  364. /// Check if the target of a given texture is texture rectangle.
  365. /// This is required as 2D textures and rectangle textures shares the same sampler type on binary shader code,
  366. /// but not on GLSL.
  367. /// </summary>
  368. /// <param name="state">Current GPU state</param>
  369. /// <param name="stageIndex">Index of the shader stage</param>
  370. /// <param name="index">Index of the texture (this is the shader "fake" handle)</param>
  371. /// <returns>True if the texture is a rectangle texture, false otherwise</returns>
  372. private bool QueryIsTextureRectangle(GpuState state, int stageIndex, int index)
  373. {
  374. var descriptor = GetGraphicsTextureDescriptor(state, stageIndex, index);
  375. TextureTarget target = descriptor.UnpackTextureTarget();
  376. bool is2DTexture = target == TextureTarget.Texture2D ||
  377. target == TextureTarget.Texture2DRect;
  378. return !descriptor.UnpackTextureCoordNormalized() && is2DTexture;
  379. }
  380. /// <summary>
  381. /// Queries the format of a given texture.
  382. /// </summary>
  383. /// <param name="state">Current GPU state</param>
  384. /// <param name="index">Index of the texture (this is the shader "fake" handle)</param>
  385. /// <returns>The texture format</returns>
  386. private TextureFormat QueryComputeTextureFormat(GpuState state, int index)
  387. {
  388. return QueryTextureFormat(GetComputeTextureDescriptor(state, index));
  389. }
  390. /// <summary>
  391. /// Queries the format of a given texture.
  392. /// </summary>
  393. /// <param name="state">Current GPU state</param>
  394. /// <param name="stageIndex">Index of the shader stage</param>
  395. /// <param name="index">Index of the texture (this is the shader "fake" handle)</param>
  396. /// <returns>The texture format</returns>
  397. private TextureFormat QueryGraphicsTextureFormat(GpuState state, int stageIndex, int index)
  398. {
  399. return QueryTextureFormat(GetGraphicsTextureDescriptor(state, stageIndex, index));
  400. }
  401. /// <summary>
  402. /// Queries the format of a given texture.
  403. /// </summary>
  404. /// <param name="descriptor">Descriptor of the texture from the texture pool</param>
  405. /// <returns>The texture format</returns>
  406. private static TextureFormat QueryTextureFormat(TextureDescriptor descriptor)
  407. {
  408. if (!FormatTable.TryGetTextureFormat(descriptor.UnpackFormat(), descriptor.UnpackSrgb(), out FormatInfo formatInfo))
  409. {
  410. return TextureFormat.Unknown;
  411. }
  412. return formatInfo.Format switch
  413. {
  414. Format.R8Unorm => TextureFormat.R8Unorm,
  415. Format.R8Snorm => TextureFormat.R8Snorm,
  416. Format.R8Uint => TextureFormat.R8Uint,
  417. Format.R8Sint => TextureFormat.R8Sint,
  418. Format.R16Float => TextureFormat.R16Float,
  419. Format.R16Unorm => TextureFormat.R16Unorm,
  420. Format.R16Snorm => TextureFormat.R16Snorm,
  421. Format.R16Uint => TextureFormat.R16Uint,
  422. Format.R16Sint => TextureFormat.R16Sint,
  423. Format.R32Float => TextureFormat.R32Float,
  424. Format.R32Uint => TextureFormat.R32Uint,
  425. Format.R32Sint => TextureFormat.R32Sint,
  426. Format.R8G8Unorm => TextureFormat.R8G8Unorm,
  427. Format.R8G8Snorm => TextureFormat.R8G8Snorm,
  428. Format.R8G8Uint => TextureFormat.R8G8Uint,
  429. Format.R8G8Sint => TextureFormat.R8G8Sint,
  430. Format.R16G16Float => TextureFormat.R16G16Float,
  431. Format.R16G16Unorm => TextureFormat.R16G16Unorm,
  432. Format.R16G16Snorm => TextureFormat.R16G16Snorm,
  433. Format.R16G16Uint => TextureFormat.R16G16Uint,
  434. Format.R16G16Sint => TextureFormat.R16G16Sint,
  435. Format.R32G32Float => TextureFormat.R32G32Float,
  436. Format.R32G32Uint => TextureFormat.R32G32Uint,
  437. Format.R32G32Sint => TextureFormat.R32G32Sint,
  438. Format.R8G8B8A8Unorm => TextureFormat.R8G8B8A8Unorm,
  439. Format.R8G8B8A8Snorm => TextureFormat.R8G8B8A8Snorm,
  440. Format.R8G8B8A8Uint => TextureFormat.R8G8B8A8Uint,
  441. Format.R8G8B8A8Sint => TextureFormat.R8G8B8A8Sint,
  442. Format.R16G16B16A16Float => TextureFormat.R16G16B16A16Float,
  443. Format.R16G16B16A16Unorm => TextureFormat.R16G16B16A16Unorm,
  444. Format.R16G16B16A16Snorm => TextureFormat.R16G16B16A16Snorm,
  445. Format.R16G16B16A16Uint => TextureFormat.R16G16B16A16Uint,
  446. Format.R16G16B16A16Sint => TextureFormat.R16G16B16A16Sint,
  447. Format.R32G32B32A32Float => TextureFormat.R32G32B32A32Float,
  448. Format.R32G32B32A32Uint => TextureFormat.R32G32B32A32Uint,
  449. Format.R32G32B32A32Sint => TextureFormat.R32G32B32A32Sint,
  450. Format.R10G10B10A2Unorm => TextureFormat.R10G10B10A2Unorm,
  451. Format.R10G10B10A2Uint => TextureFormat.R10G10B10A2Uint,
  452. Format.R11G11B10Float => TextureFormat.R11G11B10Float,
  453. _ => TextureFormat.Unknown
  454. };
  455. }
  456. /// <summary>
  457. /// Gets the texture descriptor for a given texture on the pool.
  458. /// </summary>
  459. /// <param name="state">Current GPU state</param>
  460. /// <param name="handle">Index of the texture (this is the shader "fake" handle)</param>
  461. /// <returns>Texture descriptor</returns>
  462. private TextureDescriptor GetComputeTextureDescriptor(GpuState state, int handle)
  463. {
  464. return _context.Methods.TextureManager.GetComputeTextureDescriptor(state, handle);
  465. }
  466. /// <summary>
  467. /// Gets the texture descriptor for a given texture on the pool.
  468. /// </summary>
  469. /// <param name="state">Current GPU state</param>
  470. /// <param name="stageIndex">Index of the shader stage</param>
  471. /// <param name="handle">Index of the texture (this is the shader "fake" handle)</param>
  472. /// <returns>Texture descriptor</returns>
  473. private TextureDescriptor GetGraphicsTextureDescriptor(GpuState state, int stageIndex, int handle)
  474. {
  475. return _context.Methods.TextureManager.GetGraphicsTextureDescriptor(state, stageIndex, handle);
  476. }
  477. /// <summary>
  478. /// Returns information required by both compute and graphics shader compilation.
  479. /// </summary>
  480. /// <param name="info">Information queried</param>
  481. /// <returns>Requested information</returns>
  482. private int QueryInfoCommon(QueryInfoName info)
  483. {
  484. return info switch
  485. {
  486. QueryInfoName.StorageBufferOffsetAlignment
  487. => _context.Capabilities.StorageBufferOffsetAlignment,
  488. QueryInfoName.SupportsNonConstantTextureOffset
  489. => Convert.ToInt32(_context.Capabilities.SupportsNonConstantTextureOffset),
  490. _
  491. => 0
  492. };
  493. }
  494. /// <summary>
  495. /// Prints a warning from the shader code translator.
  496. /// </summary>
  497. /// <param name="message">Warning message</param>
  498. private static void PrintLog(string message)
  499. {
  500. Logger.PrintWarning(LogClass.Gpu, $"Shader translator: {message}");
  501. }
  502. /// <summary>
  503. /// Disposes the shader cache, deleting all the cached shaders.
  504. /// It's an error to use the shader cache after disposal.
  505. /// </summary>
  506. public void Dispose()
  507. {
  508. foreach (List<ComputeShader> list in _cpPrograms.Values)
  509. {
  510. foreach (ComputeShader shader in list)
  511. {
  512. shader.HostProgram.Dispose();
  513. shader.Shader?.HostShader.Dispose();
  514. }
  515. }
  516. foreach (List<GraphicsShader> list in _gpPrograms.Values)
  517. {
  518. foreach (GraphicsShader shader in list)
  519. {
  520. shader.HostProgram.Dispose();
  521. foreach (CachedShader cachedShader in shader.Shaders)
  522. {
  523. cachedShader?.HostShader.Dispose();
  524. }
  525. }
  526. }
  527. }
  528. }
  529. }