ComputeShaderCacheHashTable.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using Ryujinx.Graphics.Gpu.Shader.HashTable;
  2. using System.Collections.Generic;
  3. namespace Ryujinx.Graphics.Gpu.Shader
  4. {
  5. /// <summary>
  6. /// Compute shader cache hash table.
  7. /// </summary>
  8. class ComputeShaderCacheHashTable
  9. {
  10. private readonly PartitionedHashTable<ShaderSpecializationList> _cache;
  11. private readonly List<CachedShaderProgram> _shaderPrograms;
  12. /// <summary>
  13. /// Creates a new compute shader cache hash table.
  14. /// </summary>
  15. public ComputeShaderCacheHashTable()
  16. {
  17. _cache = new PartitionedHashTable<ShaderSpecializationList>();
  18. _shaderPrograms = new List<CachedShaderProgram>();
  19. }
  20. /// <summary>
  21. /// Adds a program to the cache.
  22. /// </summary>
  23. /// <param name="program">Program to be added</param>
  24. public void Add(CachedShaderProgram program)
  25. {
  26. var specList = _cache.GetOrAdd(program.Shaders[0].Code, new ShaderSpecializationList());
  27. specList.Add(program);
  28. _shaderPrograms.Add(program);
  29. }
  30. /// <summary>
  31. /// Tries to find a cached program.
  32. /// </summary>
  33. /// <param name="channel">GPU channel</param>
  34. /// <param name="poolState">Texture pool state</param>
  35. /// <param name="computeState">Compute state</param>
  36. /// <param name="gpuVa">GPU virtual address of the compute shader</param>
  37. /// <param name="program">Cached host program for the given state, if found</param>
  38. /// <param name="cachedGuestCode">Cached guest code, if any found</param>
  39. /// <returns>True if a cached host program was found, false otherwise</returns>
  40. public bool TryFind(
  41. GpuChannel channel,
  42. GpuChannelPoolState poolState,
  43. GpuChannelComputeState computeState,
  44. ulong gpuVa,
  45. out CachedShaderProgram program,
  46. out byte[] cachedGuestCode)
  47. {
  48. program = null;
  49. ShaderCodeAccessor codeAccessor = new ShaderCodeAccessor(channel.MemoryManager, gpuVa);
  50. bool hasSpecList = _cache.TryFindItem(codeAccessor, out var specList, out cachedGuestCode);
  51. return hasSpecList && specList.TryFindForCompute(channel, poolState, computeState, out program);
  52. }
  53. /// <summary>
  54. /// Gets all programs that have been added to the table.
  55. /// </summary>
  56. /// <returns>Programs added to the table</returns>
  57. public IEnumerable<CachedShaderProgram> GetPrograms()
  58. {
  59. foreach (var program in _shaderPrograms)
  60. {
  61. yield return program;
  62. }
  63. }
  64. }
  65. }