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