ShaderCache.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  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.IsTextureBuffer
  236. => Convert.ToInt32(QueryIsTextureBuffer(state, 0, index, compute: true)),
  237. QueryInfoName.IsTextureRectangle
  238. => Convert.ToInt32(QueryIsTextureRectangle(state, 0, index, compute: true)),
  239. QueryInfoName.TextureFormat
  240. => (int)QueryTextureFormat(state, 0, index, compute: true),
  241. _
  242. => QueryInfoCommon(info)
  243. };
  244. }
  245. TranslatorCallbacks callbacks = new TranslatorCallbacks(QueryInfo, PrintLog);
  246. ShaderProgram program;
  247. ReadOnlySpan<byte> code = _context.MemoryAccessor.GetSpan(gpuVa, MaxProgramSize);
  248. program = Translator.Translate(code, callbacks, DefaultFlags | TranslationFlags.Compute);
  249. int[] codeCached = MemoryMarshal.Cast<byte, int>(code.Slice(0, program.Size)).ToArray();
  250. _dumper.Dump(code, compute: true, out string fullPath, out string codePath);
  251. if (fullPath != null && codePath != null)
  252. {
  253. program.Prepend("// " + codePath);
  254. program.Prepend("// " + fullPath);
  255. }
  256. return new CachedShader(program, codeCached);
  257. }
  258. /// <summary>
  259. /// Translates the binary Maxwell shader code to something that the host API accepts.
  260. /// </summary>
  261. /// <remarks>
  262. /// This will combine the "Vertex A" and "Vertex B" shader stages, if specified, into one shader.
  263. /// </remarks>
  264. /// <param name="state">Current GPU state</param>
  265. /// <param name="stage">Shader stage</param>
  266. /// <param name="gpuVa">GPU virtual address of the shader code</param>
  267. /// <param name="gpuVaA">Optional GPU virtual address of the "Vertex A" shader code</param>
  268. /// <returns>Compiled graphics shader code</returns>
  269. private CachedShader TranslateGraphicsShader(GpuState state, ShaderStage stage, ulong gpuVa, ulong gpuVaA = 0)
  270. {
  271. if (gpuVa == 0)
  272. {
  273. return null;
  274. }
  275. int QueryInfo(QueryInfoName info, int index)
  276. {
  277. return info switch
  278. {
  279. QueryInfoName.IsTextureBuffer
  280. => Convert.ToInt32(QueryIsTextureBuffer(state, (int)stage - 1, index, compute: false)),
  281. QueryInfoName.IsTextureRectangle
  282. => Convert.ToInt32(QueryIsTextureRectangle(state, (int)stage - 1, index, compute: false)),
  283. QueryInfoName.PrimitiveTopology
  284. => (int)QueryPrimitiveTopology(),
  285. QueryInfoName.TextureFormat
  286. => (int)QueryTextureFormat(state, (int)stage - 1, index, compute: false),
  287. _
  288. => QueryInfoCommon(info)
  289. };
  290. }
  291. TranslatorCallbacks callbacks = new TranslatorCallbacks(QueryInfo, PrintLog);
  292. ShaderProgram program;
  293. int[] codeCached = null;
  294. if (gpuVaA != 0)
  295. {
  296. ReadOnlySpan<byte> codeA = _context.MemoryAccessor.GetSpan(gpuVaA, MaxProgramSize);
  297. ReadOnlySpan<byte> codeB = _context.MemoryAccessor.GetSpan(gpuVa, MaxProgramSize);
  298. program = Translator.Translate(codeA, codeB, callbacks, DefaultFlags);
  299. // TODO: We should also take "codeA" into account.
  300. codeCached = MemoryMarshal.Cast<byte, int>(codeB.Slice(0, program.Size)).ToArray();
  301. _dumper.Dump(codeA, compute: false, out string fullPathA, out string codePathA);
  302. _dumper.Dump(codeB, compute: false, out string fullPathB, out string codePathB);
  303. if (fullPathA != null && fullPathB != null && codePathA != null && codePathB != null)
  304. {
  305. program.Prepend("// " + codePathB);
  306. program.Prepend("// " + fullPathB);
  307. program.Prepend("// " + codePathA);
  308. program.Prepend("// " + fullPathA);
  309. }
  310. }
  311. else
  312. {
  313. ReadOnlySpan<byte> code = _context.MemoryAccessor.GetSpan(gpuVa, MaxProgramSize);
  314. program = Translator.Translate(code, callbacks, DefaultFlags);
  315. codeCached = MemoryMarshal.Cast<byte, int>(code.Slice(0, program.Size)).ToArray();
  316. _dumper.Dump(code, compute: false, out string fullPath, out string codePath);
  317. if (fullPath != null && codePath != null)
  318. {
  319. program.Prepend("// " + codePath);
  320. program.Prepend("// " + fullPath);
  321. }
  322. }
  323. ulong address = _context.MemoryManager.Translate(gpuVa);
  324. return new CachedShader(program, codeCached);
  325. }
  326. /// <summary>
  327. /// Gets the primitive topology for the current draw.
  328. /// This is required by geometry shaders.
  329. /// </summary>
  330. /// <returns>Primitive topology</returns>
  331. private InputTopology QueryPrimitiveTopology()
  332. {
  333. switch (_context.Methods.PrimitiveType)
  334. {
  335. case PrimitiveType.Points:
  336. return InputTopology.Points;
  337. case PrimitiveType.Lines:
  338. case PrimitiveType.LineLoop:
  339. case PrimitiveType.LineStrip:
  340. return InputTopology.Lines;
  341. case PrimitiveType.LinesAdjacency:
  342. case PrimitiveType.LineStripAdjacency:
  343. return InputTopology.LinesAdjacency;
  344. case PrimitiveType.Triangles:
  345. case PrimitiveType.TriangleStrip:
  346. case PrimitiveType.TriangleFan:
  347. return InputTopology.Triangles;
  348. case PrimitiveType.TrianglesAdjacency:
  349. case PrimitiveType.TriangleStripAdjacency:
  350. return InputTopology.TrianglesAdjacency;
  351. }
  352. return InputTopology.Points;
  353. }
  354. /// <summary>
  355. /// Check if the target of a given texture is texture buffer.
  356. /// This is required as 1D textures and buffer textures shares the same sampler type on binary shader code,
  357. /// but not on GLSL.
  358. /// </summary>
  359. /// <param name="state">Current GPU state</param>
  360. /// <param name="stageIndex">Index of the shader stage</param>
  361. /// <param name="handle">Index of the texture (this is the shader "fake" handle)</param>
  362. /// <param name="compute">Indicates whenever the texture descriptor is for the compute or graphics engine</param>
  363. /// <returns>True if the texture is a buffer texture, false otherwise</returns>
  364. private bool QueryIsTextureBuffer(GpuState state, int stageIndex, int handle, bool compute)
  365. {
  366. return GetTextureDescriptor(state, stageIndex, handle, compute).UnpackTextureTarget() == TextureTarget.TextureBuffer;
  367. }
  368. /// <summary>
  369. /// Check if the target of a given texture is texture rectangle.
  370. /// This is required as 2D textures and rectangle textures shares the same sampler type on binary shader code,
  371. /// but not on GLSL.
  372. /// </summary>
  373. /// <param name="state">Current GPU state</param>
  374. /// <param name="stageIndex">Index of the shader stage</param>
  375. /// <param name="handle">Index of the texture (this is the shader "fake" handle)</param>
  376. /// <param name="compute">Indicates whenever the texture descriptor is for the compute or graphics engine</param>
  377. /// <returns>True if the texture is a rectangle texture, false otherwise</returns>
  378. private bool QueryIsTextureRectangle(GpuState state, int stageIndex, int handle, bool compute)
  379. {
  380. var descriptor = GetTextureDescriptor(state, stageIndex, handle, compute);
  381. TextureTarget target = descriptor.UnpackTextureTarget();
  382. bool is2DTexture = target == TextureTarget.Texture2D ||
  383. target == TextureTarget.Texture2DRect;
  384. return !descriptor.UnpackTextureCoordNormalized() && is2DTexture;
  385. }
  386. /// <summary>
  387. /// Queries the format of a given texture.
  388. /// </summary>
  389. /// <param name="state">Current GPU state</param>
  390. /// <param name="stageIndex">Index of the shader stage. This is ignored if <paramref name="compute"/> is true</param>
  391. /// <param name="handle">Index of the texture (this is the shader "fake" handle)</param>
  392. /// <param name="compute">Indicates whenever the texture descriptor is for the compute or graphics engine</param>
  393. /// <returns>The texture format</returns>
  394. private TextureFormat QueryTextureFormat(GpuState state, int stageIndex, int handle, bool compute)
  395. {
  396. return QueryTextureFormat(GetTextureDescriptor(state, stageIndex, handle, compute));
  397. }
  398. /// <summary>
  399. /// Queries the format of a given texture.
  400. /// </summary>
  401. /// <param name="descriptor">Descriptor of the texture from the texture pool</param>
  402. /// <returns>The texture format</returns>
  403. private static TextureFormat QueryTextureFormat(TextureDescriptor descriptor)
  404. {
  405. if (!FormatTable.TryGetTextureFormat(descriptor.UnpackFormat(), descriptor.UnpackSrgb(), out FormatInfo formatInfo))
  406. {
  407. return TextureFormat.Unknown;
  408. }
  409. return formatInfo.Format switch
  410. {
  411. Format.R8Unorm => TextureFormat.R8Unorm,
  412. Format.R8Snorm => TextureFormat.R8Snorm,
  413. Format.R8Uint => TextureFormat.R8Uint,
  414. Format.R8Sint => TextureFormat.R8Sint,
  415. Format.R16Float => TextureFormat.R16Float,
  416. Format.R16Unorm => TextureFormat.R16Unorm,
  417. Format.R16Snorm => TextureFormat.R16Snorm,
  418. Format.R16Uint => TextureFormat.R16Uint,
  419. Format.R16Sint => TextureFormat.R16Sint,
  420. Format.R32Float => TextureFormat.R32Float,
  421. Format.R32Uint => TextureFormat.R32Uint,
  422. Format.R32Sint => TextureFormat.R32Sint,
  423. Format.R8G8Unorm => TextureFormat.R8G8Unorm,
  424. Format.R8G8Snorm => TextureFormat.R8G8Snorm,
  425. Format.R8G8Uint => TextureFormat.R8G8Uint,
  426. Format.R8G8Sint => TextureFormat.R8G8Sint,
  427. Format.R16G16Float => TextureFormat.R16G16Float,
  428. Format.R16G16Unorm => TextureFormat.R16G16Unorm,
  429. Format.R16G16Snorm => TextureFormat.R16G16Snorm,
  430. Format.R16G16Uint => TextureFormat.R16G16Uint,
  431. Format.R16G16Sint => TextureFormat.R16G16Sint,
  432. Format.R32G32Float => TextureFormat.R32G32Float,
  433. Format.R32G32Uint => TextureFormat.R32G32Uint,
  434. Format.R32G32Sint => TextureFormat.R32G32Sint,
  435. Format.R8G8B8A8Unorm => TextureFormat.R8G8B8A8Unorm,
  436. Format.R8G8B8A8Snorm => TextureFormat.R8G8B8A8Snorm,
  437. Format.R8G8B8A8Uint => TextureFormat.R8G8B8A8Uint,
  438. Format.R8G8B8A8Sint => TextureFormat.R8G8B8A8Sint,
  439. Format.R16G16B16A16Float => TextureFormat.R16G16B16A16Float,
  440. Format.R16G16B16A16Unorm => TextureFormat.R16G16B16A16Unorm,
  441. Format.R16G16B16A16Snorm => TextureFormat.R16G16B16A16Snorm,
  442. Format.R16G16B16A16Uint => TextureFormat.R16G16B16A16Uint,
  443. Format.R16G16B16A16Sint => TextureFormat.R16G16B16A16Sint,
  444. Format.R32G32B32A32Float => TextureFormat.R32G32B32A32Float,
  445. Format.R32G32B32A32Uint => TextureFormat.R32G32B32A32Uint,
  446. Format.R32G32B32A32Sint => TextureFormat.R32G32B32A32Sint,
  447. Format.R10G10B10A2Unorm => TextureFormat.R10G10B10A2Unorm,
  448. Format.R10G10B10A2Uint => TextureFormat.R10G10B10A2Uint,
  449. Format.R11G11B10Float => TextureFormat.R11G11B10Float,
  450. _ => TextureFormat.Unknown
  451. };
  452. }
  453. /// <summary>
  454. /// Gets the texture descriptor for a given texture on the pool.
  455. /// </summary>
  456. /// <param name="state">Current GPU state</param>
  457. /// <param name="stageIndex">Index of the shader stage. This is ignored if <paramref name="compute"/> is true</param>
  458. /// <param name="handle">Index of the texture (this is the shader "fake" handle)</param>
  459. /// <param name="compute">Indicates whenever the texture descriptor is for the compute or graphics engine</param>
  460. /// <returns>Texture descriptor</returns>
  461. private TextureDescriptor GetTextureDescriptor(GpuState state, int stageIndex, int handle, bool compute)
  462. {
  463. if (compute)
  464. {
  465. return _context.Methods.TextureManager.GetComputeTextureDescriptor(state, handle);
  466. }
  467. else
  468. {
  469. return _context.Methods.TextureManager.GetGraphicsTextureDescriptor(state, stageIndex, handle);
  470. }
  471. }
  472. /// <summary>
  473. /// Returns information required by both compute and graphics shader compilation.
  474. /// </summary>
  475. /// <param name="info">Information queried</param>
  476. /// <returns>Requested information</returns>
  477. private int QueryInfoCommon(QueryInfoName info)
  478. {
  479. return info switch
  480. {
  481. QueryInfoName.StorageBufferOffsetAlignment
  482. => _context.Capabilities.StorageBufferOffsetAlignment,
  483. QueryInfoName.SupportsNonConstantTextureOffset
  484. => Convert.ToInt32(_context.Capabilities.SupportsNonConstantTextureOffset),
  485. _
  486. => 0
  487. };
  488. }
  489. /// <summary>
  490. /// Prints a warning from the shader code translator.
  491. /// </summary>
  492. /// <param name="message">Warning message</param>
  493. private static void PrintLog(string message)
  494. {
  495. Logger.PrintWarning(LogClass.Gpu, $"Shader translator: {message}");
  496. }
  497. /// <summary>
  498. /// Disposes the shader cache, deleting all the cached shaders.
  499. /// It's an error to use the shader cache after disposal.
  500. /// </summary>
  501. public void Dispose()
  502. {
  503. foreach (List<ComputeShader> list in _cpPrograms.Values)
  504. {
  505. foreach (ComputeShader shader in list)
  506. {
  507. shader.HostProgram.Dispose();
  508. shader.Shader?.HostShader.Dispose();
  509. }
  510. }
  511. foreach (List<GraphicsShader> list in _gpPrograms.Values)
  512. {
  513. foreach (GraphicsShader shader in list)
  514. {
  515. shader.HostProgram.Dispose();
  516. foreach (CachedShader cachedShader in shader.Shaders)
  517. {
  518. cachedShader?.HostShader.Dispose();
  519. }
  520. }
  521. }
  522. }
  523. }
  524. }