ShaderSpecializationList.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. namespace Ryujinx.Graphics.Gpu.Shader
  4. {
  5. /// <summary>
  6. /// List of cached shader programs that differs only by specialization state.
  7. /// </summary>
  8. class ShaderSpecializationList : IEnumerable<CachedShaderProgram>
  9. {
  10. private readonly List<CachedShaderProgram> _entries = new List<CachedShaderProgram>();
  11. /// <summary>
  12. /// Adds a program to the list.
  13. /// </summary>
  14. /// <param name="program">Program to be added</param>
  15. public void Add(CachedShaderProgram program)
  16. {
  17. _entries.Add(program);
  18. }
  19. /// <summary>
  20. /// Tries to find an existing 3D program on the cache.
  21. /// </summary>
  22. /// <param name="channel">GPU channel</param>
  23. /// <param name="poolState">Texture pool state</param>
  24. /// <param name="graphicsState">Graphics state</param>
  25. /// <param name="program">Cached program, if found</param>
  26. /// <returns>True if a compatible program is found, false otherwise</returns>
  27. public bool TryFindForGraphics(
  28. GpuChannel channel,
  29. GpuChannelPoolState poolState,
  30. GpuChannelGraphicsState graphicsState,
  31. out CachedShaderProgram program)
  32. {
  33. foreach (var entry in _entries)
  34. {
  35. if (entry.SpecializationState.MatchesGraphics(channel, poolState, graphicsState, true))
  36. {
  37. program = entry;
  38. return true;
  39. }
  40. }
  41. program = default;
  42. return false;
  43. }
  44. /// <summary>
  45. /// Tries to find an existing compute program on the cache.
  46. /// </summary>
  47. /// <param name="channel">GPU channel</param>
  48. /// <param name="poolState">Texture pool state</param>
  49. /// <param name="program">Cached program, if found</param>
  50. /// <returns>True if a compatible program is found, false otherwise</returns>
  51. public bool TryFindForCompute(GpuChannel channel, GpuChannelPoolState poolState, out CachedShaderProgram program)
  52. {
  53. foreach (var entry in _entries)
  54. {
  55. if (entry.SpecializationState.MatchesCompute(channel, poolState, true))
  56. {
  57. program = entry;
  58. return true;
  59. }
  60. }
  61. program = default;
  62. return false;
  63. }
  64. public IEnumerator<CachedShaderProgram> GetEnumerator()
  65. {
  66. return _entries.GetEnumerator();
  67. }
  68. IEnumerator IEnumerable.GetEnumerator()
  69. {
  70. return GetEnumerator();
  71. }
  72. }
  73. }