ShaderSpecializationList.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. ref GpuChannelPoolState poolState,
  30. ref GpuChannelGraphicsState graphicsState,
  31. out CachedShaderProgram program)
  32. {
  33. foreach (var entry in _entries)
  34. {
  35. bool usesDrawParameters = entry.Shaders[1]?.Info.UsesDrawParameters ?? false;
  36. if (entry.SpecializationState.MatchesGraphics(channel, ref poolState, ref graphicsState, usesDrawParameters, true))
  37. {
  38. program = entry;
  39. return true;
  40. }
  41. }
  42. program = default;
  43. return false;
  44. }
  45. /// <summary>
  46. /// Tries to find an existing compute program on the cache.
  47. /// </summary>
  48. /// <param name="channel">GPU channel</param>
  49. /// <param name="poolState">Texture pool state</param>
  50. /// <param name="computeState">Compute state</param>
  51. /// <param name="program">Cached program, if found</param>
  52. /// <returns>True if a compatible program is found, false otherwise</returns>
  53. public bool TryFindForCompute(GpuChannel channel, GpuChannelPoolState poolState, GpuChannelComputeState computeState, out CachedShaderProgram program)
  54. {
  55. foreach (var entry in _entries)
  56. {
  57. if (entry.SpecializationState.MatchesCompute(channel, ref poolState, computeState, true))
  58. {
  59. program = entry;
  60. return true;
  61. }
  62. }
  63. program = default;
  64. return false;
  65. }
  66. public IEnumerator<CachedShaderProgram> GetEnumerator()
  67. {
  68. return _entries.GetEnumerator();
  69. }
  70. IEnumerator IEnumerable.GetEnumerator()
  71. {
  72. return GetEnumerator();
  73. }
  74. }
  75. }