ComputeShaderCacheHashTable.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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="gpuVa">GPU virtual address of the compute shader</param>
  36. /// <param name="program">Cached host program for the given state, if found</param>
  37. /// <param name="cachedGuestCode">Cached guest code, if any found</param>
  38. /// <returns>True if a cached host program was found, false otherwise</returns>
  39. public bool TryFind(
  40. GpuChannel channel,
  41. GpuChannelPoolState poolState,
  42. ulong gpuVa,
  43. out CachedShaderProgram program,
  44. out byte[] cachedGuestCode)
  45. {
  46. program = null;
  47. ShaderCodeAccessor codeAccessor = new ShaderCodeAccessor(channel.MemoryManager, gpuVa);
  48. bool hasSpecList = _cache.TryFindItem(codeAccessor, out var specList, out cachedGuestCode);
  49. return hasSpecList && specList.TryFindForCompute(channel, poolState, out program);
  50. }
  51. /// <summary>
  52. /// Gets all programs that have been added to the table.
  53. /// </summary>
  54. /// <returns>Programs added to the table</returns>
  55. public IEnumerable<CachedShaderProgram> GetPrograms()
  56. {
  57. foreach (var program in _shaderPrograms)
  58. {
  59. yield return program;
  60. }
  61. }
  62. }
  63. }