ShaderSpecializationList.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. bool usesDrawParameters = entry.Shaders[1]?.Info.UsesDrawParameters ?? false;
  36. if (entry.SpecializationState.MatchesGraphics(channel, poolState, 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="program">Cached program, if found</param>
  51. /// <returns>True if a compatible program is found, false otherwise</returns>
  52. public bool TryFindForCompute(GpuChannel channel, GpuChannelPoolState poolState, out CachedShaderProgram program)
  53. {
  54. foreach (var entry in _entries)
  55. {
  56. if (entry.SpecializationState.MatchesCompute(channel, poolState, true))
  57. {
  58. program = entry;
  59. return true;
  60. }
  61. }
  62. program = default;
  63. return false;
  64. }
  65. public IEnumerator<CachedShaderProgram> GetEnumerator()
  66. {
  67. return _entries.GetEnumerator();
  68. }
  69. IEnumerator IEnumerable.GetEnumerator()
  70. {
  71. return GetEnumerator();
  72. }
  73. }
  74. }