forked from Unity-Technologies/UnityDataTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShader.cs
More file actions
264 lines (214 loc) · 9.61 KB
/
Copy pathShader.cs
File metadata and controls
264 lines (214 loc) · 9.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
using System.Collections.Generic;
using UnityDataTools.FileSystem.TypeTreeReaders;
namespace UnityDataTools.Analyzer.SerializedObjects;
public class Shader
{
public string Name { get; init; }
public int DecompressedSize { get; init; }
public IReadOnlyList<SubShader> SubShaders { get; init; }
public IReadOnlyList<string> Keywords { get; init; }
private Shader() {}
public class SubShader
{
public IReadOnlyList<Pass> Passes { get; init; }
public class Pass
{
public string Name { get; init; }
// The key is the program type (vertex, fragment...)
public IReadOnlyDictionary<string, IReadOnlyList<SubProgram>> Programs { get; init; }
public class SubProgram
{
public int HwTier { get; init; }
public int Api { get; init; }
public uint BlobIndex { get; init; }
// Keyword index in ShaderData.Keywords
public IReadOnlyList<int> Keywords { get; init; }
private SubProgram() {}
public static SubProgram Read(KeywordSet keywordSet, RandomAccessReader reader, Dictionary<int, string> keywordNames, int hwTier = -1)
{
var api = reader["m_GpuProgramType"].GetValue<sbyte>();
var blobIndex = reader["m_BlobIndex"].GetValue<uint>();
var keywords = new List<int>();
hwTier = hwTier != -1 ? hwTier : reader["m_ShaderHardwareTier"].GetValue<sbyte>();
if (reader.HasChild("m_KeywordIndices"))
{
var indices = reader["m_KeywordIndices"].GetValue<ushort[]>();
foreach (var index in indices)
{
if (keywordNames.TryGetValue(index, out var name))
{
keywords.Add(keywordSet.GetKeywordIndex(name));
}
}
}
else
{
foreach (var index in reader["m_GlobalKeywordIndices"].GetValue<ushort[]>())
{
if (keywordNames.TryGetValue(index, out var name))
{
keywords.Add(keywordSet.GetKeywordIndex(name));
}
}
foreach (var index in reader["m_LocalKeywordIndices"].GetValue<ushort[]>())
{
if (keywordNames.TryGetValue(index, out var name))
{
keywords.Add(keywordSet.GetKeywordIndex(name));
}
}
}
return new SubProgram() { Api = api, BlobIndex = blobIndex, HwTier = hwTier , Keywords = keywords };
}
}
public static Pass Read(KeywordSet keywordSet, RandomAccessReader reader, Dictionary<int, string> keywordNames)
{
string name = null;
Dictionary<string, IReadOnlyList<SubProgram>> programsPerType = new();
if (keywordNames == null)
{
keywordNames = new();
var nameIndices = reader["m_NameIndices"];
foreach (var nameIndex in nameIndices)
{
keywordNames[nameIndex["second"].GetValue<int>()] =
nameIndex["first"].GetValue<string>();
}
}
if (reader.HasChild("m_State"))
{
name = reader["m_State"]["m_Name"].GetValue<string>();
}
foreach (var progType in s_progTypes)
{
if (!reader.HasChild(progType.fieldName))
{
continue;
}
var program = reader[progType.fieldName];
// Starting in some Unity 2021.3 version, programs are stored in m_PlayerSubPrograms instead of m_SubPrograms.
if (program.HasChild("m_PlayerSubPrograms"))
{
int numSubPrograms = 0;
var subPrograms = program["m_PlayerSubPrograms"];
// And they are stored per hardware tiers.
foreach (var tierProgram in subPrograms)
{
// Count total number of programs.
numSubPrograms += tierProgram.GetArraySize();
}
// Preallocate enough elements to avoid allocations.
var programs = new List<SubProgram>(numSubPrograms);
for (int hwTier = 0; hwTier < subPrograms.GetArraySize(); ++hwTier)
{
foreach (var subProgram in subPrograms[hwTier])
{
programs.Add(SubProgram.Read(keywordSet, subProgram, keywordNames, hwTier));
}
}
if (programs.Count > 0)
{
programsPerType[progType.typeName] = programs;
}
}
else
{
var subPrograms = program["m_SubPrograms"];
if (subPrograms.Count > 0)
{
var programs = new List<SubProgram>(subPrograms.GetArraySize());
foreach (var subProgram in subPrograms)
{
programs.Add(SubProgram.Read(keywordSet, subProgram, keywordNames));
}
programsPerType[progType.typeName] = programs;
}
}
}
return new Pass() { Name = name, Programs = programsPerType };
}
}
public static SubShader Read(KeywordSet keywordSet, RandomAccessReader reader, Dictionary<int, string> keywordNames)
{
var passesReader = reader["m_Passes"];
var passes = new List<Pass>(passesReader.GetArraySize());
foreach (var pass in passesReader)
{
passes.Add(Pass.Read(keywordSet, pass, keywordNames));
}
return new SubShader() {Passes = passes};
}
}
public static Shader Read(RandomAccessReader reader)
{
Dictionary<int, string> keywordNames = null;
KeywordSet keywordSet = new KeywordSet();
var parsedForm = reader["m_ParsedForm"];
// Starting in some Unity 2021 version, keyword names are stored in m_KeywordNames.
if (parsedForm.HasChild("m_KeywordNames"))
{
keywordNames = new();
int i = 0;
foreach (var keyword in parsedForm["m_KeywordNames"])
{
keywordNames[i++] = keyword.GetValue<string>();
}
}
var subShadersReader = parsedForm["m_SubShaders"];
List<SubShader> subShaders = new (subShadersReader.GetArraySize());
foreach (var subShader in subShadersReader)
{
subShaders.Add(SubShader.Read(keywordSet, subShader, keywordNames));
}
int decompressedSize = 0;
if (reader["decompressedLengths"].IsArrayOfObjects)
{
// The decompressed lengths are stored per graphics API.
foreach (var apiLengths in reader["decompressedLengths"])
{
foreach (var blockSize in apiLengths.GetValue<int[]>())
{
decompressedSize += blockSize;
}
}
// Take the average (not ideal, but better than nothing).
decompressedSize /= reader["decompressedLengths"].GetArraySize();
}
else
{
foreach (var blockSize in reader["decompressedLengths"].GetValue<int[]>())
{
decompressedSize += blockSize;
}
}
var name = parsedForm["m_Name"].GetValue<string>();
return new Shader() { DecompressedSize = decompressedSize, Name = name, SubShaders = subShaders, Keywords = keywordSet.Keywords };
}
private static readonly IReadOnlyList<(string fieldName, string typeName)> s_progTypes = new List<(string fieldName, string typeName)>()
{
("progVertex", "vertex"),
("progFragment", "fragment"),
("progGeometry", "geometry"),
("progHull", "hull"),
("progDomain", "domain"),
("progRayTracing", "ray tracing"),
};
public class KeywordSet
{
public IReadOnlyList<string> Keywords => m_Keywords;
private List<string> m_Keywords = new();
private Dictionary<string, int> m_KeywordToIndex = new();
public int GetKeywordIndex(string name)
{
int index;
if (m_KeywordToIndex.TryGetValue(name, out index))
{
return index;
}
index = Keywords.Count;
m_Keywords.Add(name);
m_KeywordToIndex[name] = index;
return index;
}
}
}