A game about forced loneliness, made by TACStudios
1using System.Collections.Generic;
2using System.Linq;
3using UnityEditor.ShaderGraph;
4
5namespace UnityEditor.ShaderGraph.Internal
6{
7 sealed class ShaderGraphRequirementsPerKeyword : KeywordDependentCollection<
8 ShaderGraphRequirements,
9 ShaderGraphRequirementsPerKeyword.All,
10 ShaderGraphRequirementsPerKeyword.AllPermutations,
11 ShaderGraphRequirementsPerKeyword.ForPermutationIndex,
12 ShaderGraphRequirementsPerKeyword.Base,
13 ShaderGraphRequirementsPerKeyword.IRequirements,
14 ShaderGraphRequirementsPerKeyword.IRequirementsSet
15 >
16 {
17 public interface IRequirements : KeywordDependentCollection.IInstance, KeywordDependentCollection.ISet<IRequirements>
18 {
19 void SetRequirements(ShaderGraphRequirements value);
20
21 ShaderGraphRequirements requirements { get; set; }
22 }
23
24 public interface IRequirementsSet : KeywordDependentCollection.ISet<IRequirements>
25 {
26 }
27
28 public struct ForPermutationIndex : IRequirements, IRequirementsSet
29 {
30 private ShaderGraphRequirementsPerKeyword m_Source;
31 private int m_PermutationIndex;
32
33 public KeywordDependentCollection.KeywordPermutationInstanceType type => KeywordDependentCollection.KeywordPermutationInstanceType.Permutation;
34 public IEnumerable<IRequirements> instances => Enumerable.Repeat<IRequirements>(this, 1);
35 public int instanceCount => 1;
36 public int permutationIndex => m_PermutationIndex;
37
38 public ShaderGraphRequirements requirements
39 {
40 get => m_Source.GetOrCreateForPermutationIndex(m_PermutationIndex);
41 set => m_Source.SetForPermutationIndex(m_PermutationIndex, value);
42 }
43
44 public void SetRequirements(ShaderGraphRequirements value) => requirements = value;
45
46 internal ForPermutationIndex(ShaderGraphRequirementsPerKeyword source, int index)
47 {
48 m_Source = source;
49 m_PermutationIndex = index;
50 }
51 }
52
53 public struct Base : IRequirements, IRequirementsSet
54 {
55 private ShaderGraphRequirementsPerKeyword m_Source;
56
57 public int instanceCount => 1;
58 public int permutationIndex => -1;
59 public KeywordDependentCollection.KeywordPermutationInstanceType type => KeywordDependentCollection.KeywordPermutationInstanceType.Base;
60 public IEnumerable<IRequirements> instances => Enumerable.Repeat<IRequirements>(this, 1);
61
62 public ShaderGraphRequirements requirements
63 {
64 get => m_Source.baseStorage;
65 set => m_Source.baseStorage = value;
66 }
67
68 public void SetRequirements(ShaderGraphRequirements value) => requirements = value;
69
70 internal Base(ShaderGraphRequirementsPerKeyword source)
71 {
72 m_Source = source;
73 }
74 }
75
76 public struct All : IRequirementsSet
77 {
78 private ShaderGraphRequirementsPerKeyword m_Source;
79 public int instanceCount => m_Source.permutationCount + 1;
80
81 internal All(ShaderGraphRequirementsPerKeyword source)
82 {
83 m_Source = source;
84 }
85
86 public IEnumerable<IRequirements> instances
87 {
88 get
89 {
90 var self = this;
91 return m_Source.permutationStorages
92 .Select((v, i) => new ForPermutationIndex(self.m_Source, i) as IRequirements)
93 .Union(Enumerable.Repeat((IRequirements)m_Source.baseInstance, 1));
94 }
95 }
96 }
97
98 public struct AllPermutations : IRequirementsSet
99 {
100 private ShaderGraphRequirementsPerKeyword m_Source;
101 public int instanceCount => m_Source.permutationCount;
102
103 internal AllPermutations(ShaderGraphRequirementsPerKeyword source)
104 {
105 m_Source = source;
106 }
107
108 public IEnumerable<IRequirements> instances
109 {
110 get
111 {
112 var self = this;
113 return m_Source.permutationStorages
114 .Select((v, i) => new ForPermutationIndex(self.m_Source, i) as IRequirements);
115 }
116 }
117 }
118
119 public void UnionWith(ShaderGraphRequirementsPerKeyword other)
120 {
121 baseStorage = baseStorage.Union(other.baseStorage);
122 for (var i = 0; i < other.permutationCount; ++i)
123 SetForPermutationIndex(i,
124 GetOrCreateForPermutationIndex(i).Union(other.GetOrCreateForPermutationIndex(i)));
125 }
126
127 protected override All CreateAllSmartPointer() => new All(this);
128 protected override AllPermutations CreateAllPermutationsSmartPointer() => new AllPermutations(this);
129 protected override ForPermutationIndex CreateForPermutationSmartPointer(int index) => new ForPermutationIndex(this, index);
130 protected override Base CreateBaseSmartPointer() => new Base(this);
131 }
132}