-
Notifications
You must be signed in to change notification settings - Fork 877
Expand file tree
/
Copy pathVFXExpressionGraph.cs
More file actions
316 lines (263 loc) · 14.3 KB
/
Copy pathVFXExpressionGraph.cs
File metadata and controls
316 lines (263 loc) · 14.3 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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.VFX;
using UnityEngine.Profiling;
using Object = UnityEngine.Object;
using UnityEditor.Graphs;
using System.Collections.ObjectModel;
namespace UnityEditor.VFX
{
enum VFXDeviceTarget
{
CPU,
GPU,
}
class VFXExpressionGraph
{
private struct ExpressionData
{
public int depth;
public int index;
}
public VFXExpressionGraph()
{ }
private void AddExpressionDataRecursively(Dictionary<VFXExpression, ExpressionData> dst, VFXExpression exp, int depth = 0)
{
ExpressionData data;
if (!dst.TryGetValue(exp, out data) || data.depth < depth)
{
data.index = -1; // Will be overridden later on
data.depth = depth;
dst[exp] = data;
foreach (var parent in exp.parents)
AddExpressionDataRecursively(dst, parent, depth + 1);
}
}
private void CompileExpressionContext(IEnumerable<VFXContext> contexts,
VFXExpressionContextOption options,
VFXDeviceTarget target)
{
var expressionContext = new VFXExpression.Context(options, m_GlobalEventAttributes);
var contextsToExpressions = target == VFXDeviceTarget.GPU ? m_ContextsToGPUExpressions : m_ContextsToCPUExpressions;
var expressionsToReduced = target == VFXDeviceTarget.GPU ? m_GPUExpressionsToReduced : m_CPUExpressionsToReduced;
foreach (var context in contexts)
{
var mapper = context.GetExpressionMapper(target);
if (mapper != null)
{
foreach (var exp in mapper.expressions)
expressionContext.RegisterExpression(exp);
contextsToExpressions.Add(context, mapper);
}
}
expressionContext.Compile();
foreach (var exp in expressionContext.RegisteredExpressions)
{
var reduced = expressionContext.GetReduced(exp);
if (expressionsToReduced.TryGetValue(exp, out var previousReducedExpression))
{
if (reduced != previousReducedExpression)
throw new InvalidOperationException("Unexpected diverging expression reduction");
continue;
}
expressionsToReduced.Add(exp, reduced);
}
var allReduced = expressionContext.BuildAllReduced();
m_Expressions.UnionWith(allReduced);
foreach (var exp in expressionsToReduced.Values)
AddExpressionDataRecursively(m_ExpressionsData, exp);
var graphicsBufferUsageType = m_GraphicsBufferUsageType
.Concat(expressionContext.GraphicsBufferUsageType)
.GroupBy(o => o.Key).ToArray();
m_GraphicsBufferUsageType.Clear();
foreach (var expression in graphicsBufferUsageType)
{
var types = expression.Select(o => o.Value);
if (types.Count() != 1)
throw new InvalidOperationException("Diverging type usage for GraphicsBuffer : " + types.Select(o => o.ToString()).Aggregate((a, b) => a + b));
m_GraphicsBufferUsageType.Add(expression.Key, types.First());
}
if (target == VFXDeviceTarget.GPU)
{
m_CustomHLSLExpressions = expressionContext.hlslCodeHolders;
}
}
public void CompileExpressions(VFXGraph graph, VFXExpressionContextOption options, bool filterOutInvalidContexts = false)
{
var models = new HashSet<ScriptableObject>();
graph.CollectDependencies(models, false);
var contexts = models.OfType<VFXContext>();
if (filterOutInvalidContexts)
contexts = contexts.Where(c => c.CanBeCompiled());
CompileExpressions(contexts, options);
}
private static void ComputeEventAttributeDescs(List<VFXLayoutElementDesc> globalEventAttributes, IEnumerable<VFXContext> contexts)
{
globalEventAttributes.Clear();
//SpawnCount should always be added first : spawnCount is an implicit parameter from eventAttribute
globalEventAttributes.Add(new VFXLayoutElementDesc()
{
name = VFXAttribute.SpawnCount.name,
type = VFXAttribute.SpawnCount.type
});
IEnumerable<VFXLayoutElementDesc> globalAttribute = Enumerable.Empty<VFXLayoutElementDesc>();
foreach (var context in contexts.Where(o => o.contextType == VFXContextType.Spawner || o.contextType == VFXContextType.Event))
{
var attributesToStoreFromOutputContext = context.outputContexts.Select(o => o.GetData()).Where(o => o != null)
.SelectMany(o => o.GetAttributes().Where(a => (a.mode & VFXAttributeMode.ReadSource) != 0));
var attributesReadInSpawnContext = context.GetData().GetAttributes().Where(a => (a.mode & VFXAttributeMode.Read) != 0);
var attributesInGlobal = attributesToStoreFromOutputContext.Concat(attributesReadInSpawnContext).GroupBy(o => o.attrib.name);
foreach (var attribute in attributesInGlobal.Select(o => o.First()))
{
if (!globalEventAttributes.Any(o => o.name == attribute.attrib.name))
{
globalEventAttributes.Add(new VFXLayoutElementDesc()
{
name = attribute.attrib.name,
type = attribute.attrib.type
});
}
}
}
var structureLayoutTotalSize = (uint)globalEventAttributes.Sum(e => (long)VFXExpression.TypeToSize(e.type));
var currentLayoutSize = 0u;
var listWithOffset = new List<VFXLayoutElementDesc>();
globalEventAttributes.ForEach(e =>
{
e.offset.element = currentLayoutSize;
e.offset.structure = structureLayoutTotalSize;
currentLayoutSize += (uint)VFXExpression.TypeToSize(e.type);
listWithOffset.Add(e);
});
globalEventAttributes.Clear();
globalEventAttributes.AddRange(listWithOffset);
}
public void CompileExpressions(IEnumerable<VFXContext> contexts, VFXExpressionContextOption options)
{
Profiler.BeginSample("VFXEditor.CompileExpressionGraph");
try
{
ComputeEventAttributeDescs(m_GlobalEventAttributes, contexts);
m_Expressions.Clear();
m_FlattenedExpressions.Clear();
m_CommonExpressionCount = 0u;
m_ExpressionsData.Clear();
m_ContextsToGPUExpressions.Clear();
m_ContextsToCPUExpressions.Clear();
m_GPUExpressionsToReduced.Clear();
m_CPUExpressionsToReduced.Clear();
var spawnerContexts = contexts.Where(o => o.contextType == VFXContextType.Spawner);
var otherContexts = contexts.Where(o => o.contextType != VFXContextType.Spawner);
CompileExpressionContext(spawnerContexts, options | VFXExpressionContextOption.PatchReadToEventAttribute, VFXDeviceTarget.CPU);
CompileExpressionContext(otherContexts, options, VFXDeviceTarget.CPU);
CompileExpressionContext(contexts, options | VFXExpressionContextOption.GPUDataTransformation, VFXDeviceTarget.GPU);
var sortedList = m_ExpressionsData.Where(kvp =>
{
var exp = kvp.Key;
return !exp.IsAny(VFXExpression.Flags.NotCompilableOnCPU); // remove per element expression from flattened data // TODO Remove uniform constants too
});
var expressionPerSpawn = sortedList.Where(o => o.Key.Is(VFXExpression.Flags.PerSpawn));
var expressionNotPerSpawn = sortedList.Where(o => !o.Key.Is(VFXExpression.Flags.PerSpawn));
//m_FlattenedExpressions is a concatenation of [sorted all expression !PerSpawn] & [sorted all expression Per Spawn]
//It's more convenient for two reasons :
// - Reduces process chunk for ComputePreProcessExpressionForSpawn
// - Allows to determine the maximum index of expression while processing main expression evaluation
sortedList = expressionNotPerSpawn.OrderByDescending(o => o.Value.depth);
sortedList = sortedList.Concat(expressionPerSpawn.OrderByDescending(o => o.Value.depth));
m_FlattenedExpressions = sortedList.Select(o => o.Key).ToList();
m_CommonExpressionCount = (uint)expressionNotPerSpawn.Count();
// update index in expression data
for (int i = 0; i < m_FlattenedExpressions.Count; ++i)
{
var data = m_ExpressionsData[m_FlattenedExpressions[i]];
data.index = i;
m_ExpressionsData[m_FlattenedExpressions[i]] = data;
}
if (VFXViewPreference.advancedLogs)
Debug.Log(string.Format("RECOMPILE EXPRESSION GRAPH - NB EXPRESSIONS: {0} - NB CPU END EXPRESSIONS: {1} - NB GPU END EXPRESSIONS: {2}", m_Expressions.Count, m_CPUExpressionsToReduced.Count, m_GPUExpressionsToReduced.Count));
}
finally
{
Profiler.EndSample();
}
}
public int GetFlattenedIndex(VFXExpression exp)
{
if (m_ExpressionsData.ContainsKey(exp))
return m_ExpressionsData[exp].index;
return -1;
}
public VFXExpression GetReduced(VFXExpression exp, VFXDeviceTarget target)
{
VFXExpression reduced;
var expressionToReduced = target == VFXDeviceTarget.GPU ? m_GPUExpressionsToReduced : m_CPUExpressionsToReduced;
expressionToReduced.TryGetValue(exp, out reduced);
return reduced;
}
public VFXExpressionMapper BuildCPUMapper(VFXContext context)
{
return BuildMapper(context, m_ContextsToCPUExpressions, VFXDeviceTarget.CPU);
}
public VFXExpressionMapper BuildGPUMapper(VFXContext context)
{
return BuildMapper(context, m_ContextsToGPUExpressions, VFXDeviceTarget.GPU);
}
public List<string> GetAllNames(VFXExpression exp)
{
List<string> names = new List<string>();
foreach (var mapper in m_ContextsToCPUExpressions.Values.Concat(m_ContextsToGPUExpressions.Values))
{
names.AddRange(mapper.GetData(exp).Select(o => o.fullName));
}
return names;
}
private VFXExpressionMapper BuildMapper(VFXContext context, Dictionary<VFXContext, VFXExpressionMapper> dictionnary, VFXDeviceTarget target)
{
VFXExpression.Flags check = target == VFXDeviceTarget.GPU ? VFXExpression.Flags.InvalidOnGPU | VFXExpression.Flags.PerElement : VFXExpression.Flags.InvalidOnCPU;
VFXExpressionMapper outMapper = new VFXExpressionMapper();
VFXExpressionMapper inMapper;
dictionnary.TryGetValue(context, out inMapper);
if (inMapper != null)
{
foreach (var exp in inMapper.expressions)
{
var reduced = GetReduced(exp, target);
if (reduced.Is(check))
{
var message = $"The expression \"{reduced.GetType().Name}\" is not valid as it have the flag: {check}";
if (context.GetGraph().compileReporter is { } compileReporter)
{
compileReporter.RegisterError("CompileReduceExpressionFail", VFXErrorType.Error, message, context);
}
throw new InvalidOperationException(message);
}
var mappedDataList = inMapper.GetData(exp);
foreach (var mappedData in mappedDataList)
outMapper.AddExpression(reduced, mappedData);
}
}
return outMapper;
}
public HashSet<VFXExpression> Expressions => m_Expressions;
public List<VFXExpression> FlattenedExpressions => m_FlattenedExpressions;
public uint CommonExpressionCount => m_CommonExpressionCount;
public Dictionary<VFXExpression, VFXExpression> GPUExpressionsToReduced => m_GPUExpressionsToReduced;
public Dictionary<VFXExpression, VFXExpression> CPUExpressionsToReduced => m_CPUExpressionsToReduced;
public IEnumerable<VFXLayoutElementDesc> GlobalEventAttributes => m_GlobalEventAttributes;
public ReadOnlyDictionary<VFXExpression, Type> GraphicsBufferTypeUsage => new ReadOnlyDictionary<VFXExpression, Type>(m_GraphicsBufferUsageType);
public IHLSLCodeHolder[] customHLSLExpressions => m_CustomHLSLExpressions;
private IHLSLCodeHolder[] m_CustomHLSLExpressions;
private Dictionary<VFXExpression, Type> m_GraphicsBufferUsageType = new Dictionary<VFXExpression, Type>();
private HashSet<VFXExpression> m_Expressions = new HashSet<VFXExpression>();
private Dictionary<VFXExpression, VFXExpression> m_CPUExpressionsToReduced = new Dictionary<VFXExpression, VFXExpression>();
private Dictionary<VFXExpression, VFXExpression> m_GPUExpressionsToReduced = new Dictionary<VFXExpression, VFXExpression>();
private List<VFXExpression> m_FlattenedExpressions = new List<VFXExpression>();
private uint m_CommonExpressionCount;
private Dictionary<VFXExpression, ExpressionData> m_ExpressionsData = new Dictionary<VFXExpression, ExpressionData>();
private Dictionary<VFXContext, VFXExpressionMapper> m_ContextsToCPUExpressions = new Dictionary<VFXContext, VFXExpressionMapper>();
private Dictionary<VFXContext, VFXExpressionMapper> m_ContextsToGPUExpressions = new Dictionary<VFXContext, VFXExpressionMapper>();
private List<VFXLayoutElementDesc> m_GlobalEventAttributes = new List<VFXLayoutElementDesc>();
}
}