ShaderSpecializationList.cs 3.1 KB

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