forked from Unity-Technologies/Graphics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVFXGraphCompiledData.cs
More file actions
1292 lines (1129 loc) · 60.6 KB
/
Copy pathVFXGraphCompiledData.cs
File metadata and controls
1292 lines (1129 loc) · 60.6 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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using UnityEditor.VFX;
using UnityEngine;
using UnityEngine.VFX;
using UnityEngine.Profiling;
using UnityEngine.Rendering;
using Object = UnityEngine.Object;
using System.IO;
using System.Collections.ObjectModel;
namespace UnityEditor.VFX
{
struct VFXContextCompiledData
{
public VFXExpressionMapper cpuMapper;
public VFXExpressionMapper gpuMapper;
public VFXUniformMapper uniformMapper;
public ReadOnlyDictionary<VFXExpression, Type> graphicsBufferUsage;
public VFXMapping[] parameters;
public int indexInShaderSource;
}
enum VFXCompilationMode
{
Edition,
Runtime,
}
class VFXDependentBuffersData
{
public Dictionary<VFXData, int> attributeBuffers = new Dictionary<VFXData, int>();
public Dictionary<VFXData, int> stripBuffers = new Dictionary<VFXData, int>();
public Dictionary<VFXData, int> eventBuffers = new Dictionary<VFXData, int>();
public Dictionary<VFXData, int> boundsBuffers = new Dictionary<VFXData, int>();
}
class VFXGraphCompiledData
{
// 3: Serialize material
// 4: Bounds helper change
// 5: HasAttributeBuffer flag
public const uint compiledVersion = 5;
public VFXGraphCompiledData(VFXGraph graph)
{
if (graph == null)
throw new ArgumentNullException("VFXGraph cannot be null");
m_Graph = graph;
}
private struct GeneratedCodeData
{
public VFXContext context;
public bool computeShader;
public System.Text.StringBuilder content;
public VFXCompilationMode compilMode;
}
private static VFXExpressionObjectValueContainerDesc<T> CreateObjectValueDesc<T>(VFXExpression exp, int expIndex)
{
var desc = new VFXExpressionObjectValueContainerDesc<T>();
desc.instanceID = exp.Get<int>();
return desc;
}
private static VFXExpressionValueContainerDesc<T> CreateValueDesc<T>(VFXExpression exp, int expIndex)
{
var desc = new VFXExpressionValueContainerDesc<T>();
desc.value = exp.Get<T>();
return desc;
}
private void SetValueDesc<T>(VFXExpressionValueContainerDesc desc, VFXExpression exp)
{
((VFXExpressionValueContainerDesc<T>)desc).value = exp.Get<T>();
}
private void SetObjectValueDesc<T>(VFXExpressionValueContainerDesc desc, VFXExpression exp)
{
((VFXExpressionObjectValueContainerDesc<T>)desc).instanceID = exp.Get<int>();
}
public uint FindReducedExpressionIndexFromSlotCPU(VFXSlot slot)
{
if (m_ExpressionGraph == null)
{
return uint.MaxValue;
}
var targetExpression = slot.GetExpression();
if (targetExpression == null)
{
return uint.MaxValue;
}
if (!m_ExpressionGraph.CPUExpressionsToReduced.ContainsKey(targetExpression))
{
return uint.MaxValue;
}
var ouputExpression = m_ExpressionGraph.CPUExpressionsToReduced[targetExpression];
return (uint)m_ExpressionGraph.GetFlattenedIndex(ouputExpression);
}
private static void FillExpressionDescs(VFXExpressionGraph graph, List<VFXExpressionDesc> outExpressionCommonDescs, List<VFXExpressionDesc> outExpressionPerSpawnEventDescs, List<VFXExpressionValueContainerDesc> outValueDescs)
{
var flatGraph = graph.FlattenedExpressions;
var numFlattenedExpressions = flatGraph.Count;
var maxCommonExpressionIndex = (uint)numFlattenedExpressions;
for (int i = 0; i < numFlattenedExpressions; ++i)
{
var exp = flatGraph[i];
if (exp.Is(VFXExpression.Flags.PerSpawn) && maxCommonExpressionIndex == numFlattenedExpressions)
maxCommonExpressionIndex = (uint)i;
if (!exp.Is(VFXExpression.Flags.PerSpawn) && maxCommonExpressionIndex != numFlattenedExpressions)
throw new InvalidOperationException("Not contiguous expression VFXExpression.Flags.PerSpawn detected");
// Must match data in C++ expression
if (exp.Is(VFXExpression.Flags.Value))
{
VFXExpressionValueContainerDesc value;
switch (exp.valueType)
{
case VFXValueType.Float: value = CreateValueDesc<float>(exp, i); break;
case VFXValueType.Float2: value = CreateValueDesc<Vector2>(exp, i); break;
case VFXValueType.Float3: value = CreateValueDesc<Vector3>(exp, i); break;
case VFXValueType.Float4: value = CreateValueDesc<Vector4>(exp, i); break;
case VFXValueType.Int32: value = CreateValueDesc<int>(exp, i); break;
case VFXValueType.Uint32: value = CreateValueDesc<uint>(exp, i); break;
case VFXValueType.Texture2D:
case VFXValueType.Texture2DArray:
case VFXValueType.Texture3D:
case VFXValueType.TextureCube:
case VFXValueType.TextureCubeArray:
value = CreateObjectValueDesc<Texture>(exp, i);
break;
case VFXValueType.CameraBuffer: value = CreateObjectValueDesc<Texture>(exp, i); break;
case VFXValueType.Matrix4x4: value = CreateValueDesc<Matrix4x4>(exp, i); break;
case VFXValueType.Curve: value = CreateValueDesc<AnimationCurve>(exp, i); break;
case VFXValueType.ColorGradient: value = CreateValueDesc<Gradient>(exp, i); break;
case VFXValueType.Mesh: value = CreateObjectValueDesc<Mesh>(exp, i); break;
case VFXValueType.SkinnedMeshRenderer: value = CreateObjectValueDesc<SkinnedMeshRenderer>(exp, i); break;
case VFXValueType.Boolean: value = CreateValueDesc<bool>(exp, i); break;
case VFXValueType.Buffer: value = CreateValueDesc<GraphicsBuffer>(exp, i); break;
default: throw new InvalidOperationException("Invalid type : " + exp.valueType);
}
value.expressionIndex = (uint)i;
outValueDescs.Add(value);
}
var outExpressionsDesc = i >= maxCommonExpressionIndex ? outExpressionPerSpawnEventDescs : outExpressionCommonDescs;
outExpressionsDesc.Add(new VFXExpressionDesc
{
op = exp.operation,
data = exp.GetOperands(graph).ToArray(),
});
}
}
private static void CollectExposedDesc(List<VFXMapping> outExposedParameters, string name, VFXSlot slot, VFXExpressionGraph graph)
{
var expression = slot.valueType != VFXValueType.None ? slot.GetInExpression() : null;
if (expression != null)
{
var exprIndex = graph.GetFlattenedIndex(expression);
if (exprIndex == -1)
throw new InvalidOperationException("Unable to retrieve value from exposed for " + name);
outExposedParameters.Add(new VFXMapping()
{
name = name,
index = exprIndex
});
}
else
{
foreach (var child in slot.children)
{
CollectExposedDesc(outExposedParameters, name + "_" + child.name, child, graph);
}
}
}
private static void FillExposedDescs(List<VFXMapping> outExposedParameters, VFXExpressionGraph graph, IEnumerable<VFXParameter> parameters)
{
foreach (var parameter in parameters)
{
if (parameter.exposed && !parameter.isOutput)
{
CollectExposedDesc(outExposedParameters, parameter.exposedName, parameter.GetOutputSlot(0), graph);
}
}
}
class VFXSpawnContextLayer
{
public VFXContext context;
public int depth;
}
private static List<VFXSpawnContextLayer> CollectContextParentRecursively(IEnumerable<VFXContext> inputList, ref SubgraphInfos subgraphContexts, int currentDepth = 0)
{
var contextEffectiveInputLinks = subgraphContexts.contextEffectiveInputLinks;
var contextList = inputList.SelectMany(o => contextEffectiveInputLinks[o].SelectMany(t => t))
.Select(t => t.context).Distinct()
.Select(c => new VFXSpawnContextLayer()
{
context = c,
depth = currentDepth
}).ToList();
if (contextList.Any(o => contextEffectiveInputLinks[o.context].Any()))
{
var parentContextList = CollectContextParentRecursively(contextList.Select(c => c.context), ref subgraphContexts, currentDepth + 1);
foreach (var parentContextEntry in parentContextList)
{
var currentEntry = contextList.FirstOrDefault(o => o.context == parentContextEntry.context);
if (currentEntry == null)
{
contextList.Add(parentContextEntry);
}
else if (parentContextEntry.depth > currentEntry.depth)
{
currentEntry.depth = parentContextEntry.depth;
}
}
}
return contextList;
}
private static VFXContext[] CollectSpawnersHierarchy(IEnumerable<VFXContext> vfxContext, ref SubgraphInfos subgraphContexts)
{
var initContext = vfxContext.Where(o => o.contextType == VFXContextType.Init || o.contextType == VFXContextType.OutputEvent).ToList();
var spawnerHierarchy = CollectContextParentRecursively(initContext, ref subgraphContexts);
var spawnerList = spawnerHierarchy.Where(o => o.context.contextType == VFXContextType.Spawner)
.OrderByDescending(o => o.depth)
.Select(o => o.context).ToArray();
return spawnerList;
}
struct SpawnInfo
{
public int bufferIndex;
public int systemIndex;
}
private static VFXCPUBufferData ComputeArrayOfStructureInitialData(IEnumerable<VFXLayoutElementDesc> layout)
{
var data = new VFXCPUBufferData();
foreach (var element in layout)
{
var attribute = VFXAttribute.AllAttribute.FirstOrDefault(o => o.name == element.name);
bool useAttribute = attribute.name == element.name;
if (element.type == VFXValueType.Boolean)
{
var v = useAttribute ? attribute.value.Get<bool>() : default(bool);
data.PushBool(v);
}
else if (element.type == VFXValueType.Float)
{
var v = useAttribute ? attribute.value.Get<float>() : default(float);
data.PushFloat(v);
}
else if (element.type == VFXValueType.Float2)
{
var v = useAttribute ? attribute.value.Get<Vector2>() : default(Vector2);
data.PushFloat(v.x);
data.PushFloat(v.y);
}
else if (element.type == VFXValueType.Float3)
{
var v = useAttribute ? attribute.value.Get<Vector3>() : default(Vector3);
data.PushFloat(v.x);
data.PushFloat(v.y);
data.PushFloat(v.z);
}
else if (element.type == VFXValueType.Float4)
{
var v = useAttribute ? attribute.value.Get<Vector4>() : default(Vector4);
data.PushFloat(v.x);
data.PushFloat(v.y);
data.PushFloat(v.z);
data.PushFloat(v.w);
}
else if (element.type == VFXValueType.Int32)
{
var v = useAttribute ? attribute.value.Get<int>() : default(int);
data.PushInt(v);
}
else if (element.type == VFXValueType.Uint32)
{
var v = useAttribute ? attribute.value.Get<uint>() : default(uint);
data.PushUInt(v);
}
else
{
throw new NotImplementedException();
}
}
return data;
}
void RecursePutSubgraphParent(Dictionary<VFXSubgraphContext, VFXSubgraphContext> parents, List<VFXSubgraphContext> subgraphs, VFXSubgraphContext subgraph)
{
foreach (var subSubgraph in subgraph.subChildren.OfType<VFXSubgraphContext>().Where(t => t.subgraph != null))
{
subgraphs.Add(subSubgraph);
parents[subSubgraph] = subgraph;
RecursePutSubgraphParent(parents, subgraphs, subSubgraph);
}
}
static List<VFXContextLink>[] ComputeContextEffectiveLinks(VFXContext context, ref SubgraphInfos subgraphInfos)
{
List<VFXContextLink>[] result = new List<VFXContextLink>[context.inputFlowSlot.Length];
Dictionary<string, int> eventNameIndice = new Dictionary<string, int>();
for (int i = 0; i < context.inputFlowSlot.Length; ++i)
{
result[i] = new List<VFXContextLink>();
VFXSubgraphContext parentSubgraph = null;
subgraphInfos.spawnerSubgraph.TryGetValue(context, out parentSubgraph);
List<VFXContext> subgraphAncestors = new List<VFXContext>();
subgraphAncestors.Add(context);
while (parentSubgraph != null)
{
subgraphAncestors.Add(parentSubgraph);
if (!subgraphInfos.subgraphParents.TryGetValue(parentSubgraph, out parentSubgraph))
{
parentSubgraph = null;
}
}
List<List<int>> defaultEventPaths = new List<List<int>>();
defaultEventPaths.Add(new List<int>(new int[] { i }));
List<List<int>> newEventPaths = new List<List<int>>();
var usedContexts = new List<VFXContext>();
for (int j = 0; j < subgraphAncestors.Count; ++j)
{
var sg = subgraphAncestors[j];
var nextSg = j < subgraphAncestors.Count - 1 ? subgraphAncestors[j + 1] as VFXSubgraphContext : null;
foreach (var path in defaultEventPaths)
{
int currentFlowIndex = path.Last();
var eventSlot = sg.inputFlowSlot[currentFlowIndex];
var eventSlotSpawners = eventSlot.link.Where(t => !(t.context is VFXBasicEvent));
if (eventSlotSpawners.Any())
{
foreach (var evt in eventSlotSpawners)
{
result[i].Add(evt);
}
}
var eventSlotEvents = eventSlot.link.Where(t => t.context is VFXBasicEvent);
if (eventSlotEvents.Any())
{
foreach (var evt in eventSlotEvents)
{
string eventName = (evt.context as VFXBasicEvent).eventName;
switch (eventName)
{
case VisualEffectAsset.PlayEventName:
if (nextSg != null)
newEventPaths.Add(path.Concat(new int[] { 0 }).ToList());
else
result[i].Add(evt);
break;
case VisualEffectAsset.StopEventName:
if (nextSg != null)
newEventPaths.Add(path.Concat(new int[] { 1 }).ToList());
else
result[i].Add(evt);
break;
default:
{
if (nextSg != null)
{
int eventIndex = nextSg.GetInputFlowIndex(eventName);
if (eventIndex != -1)
newEventPaths.Add(path.Concat(new int[] { eventIndex }).ToList());
}
else
{
result[i].Add(evt);
}
}
break;
}
}
}
else if (!eventSlot.link.Any())
{
if (!(sg is VFXSubgraphContext))
{
if (nextSg != null)
{
int fixedSlotIndex = currentFlowIndex > 1 ? currentFlowIndex : nextSg.GetInputFlowIndex(currentFlowIndex == 1 ? VisualEffectAsset.StopEventName : VisualEffectAsset.PlayEventName);
if (fixedSlotIndex >= 0)
newEventPaths.Add(path.Concat(new int[] { fixedSlotIndex }).ToList());
}
else
newEventPaths.Add(path.Concat(new int[] { currentFlowIndex }).ToList());
}
else
{
var sgsg = sg as VFXSubgraphContext;
var eventName = sgsg.GetInputFlowName(currentFlowIndex);
var eventCtx = sgsg.GetEventContext(eventName);
if (eventCtx != null)
result[i].Add(new VFXContextLink() { slotIndex = 0, context = eventCtx });
}
}
}
defaultEventPaths.Clear();
defaultEventPaths.AddRange(newEventPaths);
newEventPaths.Clear();
}
}
return result;
}
private class ProcessChunk
{
public int startIndex;
public int endIndex;
}
static VFXMapping[] ComputePreProcessExpressionForSpawn(IEnumerable<VFXExpression> expressionPerSpawnToProcess, VFXExpressionGraph graph)
{
var allExpressions = new HashSet<VFXExpression>();
foreach (var expression in expressionPerSpawnToProcess)
VFXExpression.CollectParentExpressionRecursively(expression, allExpressions);
var expressionIndexes = allExpressions.
Where(o => o.Is(VFXExpression.Flags.PerSpawn)) //Filter only per spawn part of graph
.Select(o => graph.GetFlattenedIndex(o))
.OrderBy(i => i);
//Additional verification of appropriate expected expression index
//In flatten expression, all common expressions are sorted first, then, we have chunk of additional preprocess
//We aren't supposed to happen a chunk which is running common expression here.
if (expressionIndexes.Any(i => i < graph.CommonExpressionCount))
{
var expressionInCommon = allExpressions
.Where(o => graph.GetFlattenedIndex(o) < graph.CommonExpressionCount)
.OrderBy(o => graph.GetFlattenedIndex(o));
Debug.LogErrorFormat("Unexpected preprocess expression detected : {0} (count)", expressionInCommon.Count());
}
var processChunk = new List<ProcessChunk>();
int previousIndex = int.MinValue;
foreach (var indice in expressionIndexes)
{
if (indice != previousIndex + 1)
processChunk.Add(new ProcessChunk()
{
startIndex = indice,
endIndex = indice + 1
});
else
processChunk.Last().endIndex = indice + 1;
previousIndex = indice;
}
return processChunk.SelectMany((o, i) =>
{
var prefix = VFXCodeGeneratorHelper.GeneratePrefix((uint)i);
return new[]
{
new VFXMapping
{
name = "start_" + prefix,
index = o.startIndex
},
new VFXMapping
{
name = "end_" + prefix,
index = o.endIndex
}
};
}).ToArray();
}
private static VFXEditorTaskDesc[] BuildEditorTaksDescFromBlockSpawner(IEnumerable<VFXBlock> blocks, VFXContextCompiledData contextData, VFXExpressionGraph graph)
{
var taskDescList = new List<VFXEditorTaskDesc>();
int index = 0;
foreach (var b in blocks)
{
var spawnerBlock = b as VFXAbstractSpawner;
if (spawnerBlock == null)
{
throw new InvalidCastException("Unexpected block type in spawnerContext");
}
if (spawnerBlock.spawnerType == VFXTaskType.CustomCallbackSpawner && spawnerBlock.customBehavior == null)
{
throw new InvalidOperationException("VFXAbstractSpawner excepts a custom behavior for custom callback type");
}
if (spawnerBlock.spawnerType != VFXTaskType.CustomCallbackSpawner && spawnerBlock.customBehavior != null)
{
throw new InvalidOperationException("VFXAbstractSpawner only expects a custom behavior for custom callback type");
}
var mappingList = new List<VFXMapping>();
var expressionPerSpawnToProcess = new List<VFXExpression>();
foreach (var namedExpression in contextData.cpuMapper.CollectExpression(index, false))
{
mappingList.Add(new VFXMapping()
{
index = graph.GetFlattenedIndex(namedExpression.exp),
name = namedExpression.name
});
if (namedExpression.exp.Is(VFXExpression.Flags.PerSpawn))
expressionPerSpawnToProcess.Add(namedExpression.exp);
}
if (expressionPerSpawnToProcess.Any())
{
var mappingPreProcess = ComputePreProcessExpressionForSpawn(expressionPerSpawnToProcess, graph);
var preProcessTask = new VFXEditorTaskDesc
{
type = UnityEngine.VFX.VFXTaskType.EvaluateExpressionsSpawner,
buffers = new VFXMapping[0],
values = mappingPreProcess,
parameters = contextData.parameters,
externalProcessor = null
};
taskDescList.Add(preProcessTask);
}
Object processor = null;
if (spawnerBlock.customBehavior != null)
processor = spawnerBlock.customBehavior;
taskDescList.Add(new VFXEditorTaskDesc
{
type = (UnityEngine.VFX.VFXTaskType)spawnerBlock.spawnerType,
buffers = new VFXMapping[0],
values = mappingList.ToArray(),
parameters = contextData.parameters,
externalProcessor = processor
});
index++;
}
return taskDescList.ToArray();
}
private static void FillSpawner(Dictionary<VFXContext, SpawnInfo> outContextSpawnToSpawnInfo,
Dictionary<VFXContext, uint> outDataToSystemIndex,
List<VFXCPUBufferDesc> outCpuBufferDescs,
List<VFXEditorSystemDesc> outSystemDescs,
IEnumerable<VFXContext> contexts,
VFXExpressionGraph graph,
Dictionary<VFXContext, VFXContextCompiledData> contextToCompiledData,
ref SubgraphInfos subgraphInfos,
VFXSystemNames systemNames = null)
{
var spawners = CollectSpawnersHierarchy(contexts, ref subgraphInfos);
foreach (var it in spawners.Select((spawner, index) => new { spawner, index }))
{
outContextSpawnToSpawnInfo.Add(it.spawner, new SpawnInfo() { bufferIndex = outCpuBufferDescs.Count, systemIndex = it.index });
outCpuBufferDescs.Add(new VFXCPUBufferDesc()
{
capacity = 1u,
stride = graph.GlobalEventAttributes.First().offset.structure,
layout = graph.GlobalEventAttributes.ToArray(),
initialData = ComputeArrayOfStructureInitialData(graph.GlobalEventAttributes)
});
}
foreach (var spawnContext in spawners)
{
var buffers = new List<VFXMapping>();
buffers.Add(new VFXMapping()
{
index = outContextSpawnToSpawnInfo[spawnContext].bufferIndex,
name = "spawner_output"
});
for (int indexSlot = 0; indexSlot < 2 && indexSlot < spawnContext.inputFlowSlot.Length; ++indexSlot)
{
foreach (var input in subgraphInfos.contextEffectiveInputLinks[spawnContext][indexSlot])
{
var inputContext = input.context;
if (outContextSpawnToSpawnInfo.ContainsKey(inputContext))
{
buffers.Add(new VFXMapping()
{
index = outContextSpawnToSpawnInfo[inputContext].bufferIndex,
name = "spawner_input_" + (indexSlot == 0 ? "OnPlay" : "OnStop")
});
}
}
}
var contextData = contextToCompiledData[spawnContext];
var contextExpressions = contextData.cpuMapper.CollectExpression(-1);
var systemValueMappings = new List<VFXMapping>();
var expressionPerSpawnToProcess = new List<VFXExpression>();
foreach (var contextExpression in contextExpressions)
{
var expressionIndex = graph.GetFlattenedIndex(contextExpression.exp);
systemValueMappings.Add(new VFXMapping(contextExpression.name, expressionIndex));
if (contextExpression.exp.Is(VFXExpression.Flags.PerSpawn))
{
expressionPerSpawnToProcess.Add(contextExpression.exp);
}
}
if (expressionPerSpawnToProcess.Any())
{
var addiionnalValues = ComputePreProcessExpressionForSpawn(expressionPerSpawnToProcess, graph);
systemValueMappings.AddRange(addiionnalValues);
}
string nativeName = string.Empty;
if (systemNames != null)
nativeName = systemNames.GetUniqueSystemName(spawnContext);
else
throw new InvalidOperationException("system names manager cannot be null");
outDataToSystemIndex.Add(spawnContext, (uint)outSystemDescs.Count);
contextToCompiledData[spawnContext] = contextData;
outSystemDescs.Add(new VFXEditorSystemDesc()
{
values = systemValueMappings.ToArray(),
buffers = buffers.ToArray(),
capacity = 0u,
name = nativeName,
flags = VFXSystemFlag.SystemDefault,
layer = uint.MaxValue,
tasks = BuildEditorTaksDescFromBlockSpawner(spawnContext.activeFlattenedChildrenWithImplicit, contextData, graph)
});
}
}
struct SubgraphInfos
{
public Dictionary<VFXSubgraphContext, VFXSubgraphContext> subgraphParents;
public Dictionary<VFXContext, VFXSubgraphContext> spawnerSubgraph;
public List<VFXSubgraphContext> subgraphs;
public Dictionary<VFXContext, List<VFXContextLink>[]> contextEffectiveInputLinks;
public List<VFXContextLink> GetContextEffectiveOutputLinks(VFXContext context, int slot)
{
List<VFXContextLink> effectiveOuts = new List<VFXContextLink>();
foreach (var kv in contextEffectiveInputLinks)
{
for (int i = 0; i < kv.Value.Length; ++i)
{
foreach (var link in kv.Value[i])
{
if (link.context == context && link.slotIndex == slot)
effectiveOuts.Add(new VFXContextLink() { context = kv.Key, slotIndex = i });
}
}
}
return effectiveOuts;
}
}
private static void FillEvent(List<EventDesc> outEventDesc, Dictionary<VFXContext, SpawnInfo> contextSpawnToSpawnInfo, IEnumerable<VFXContext> contexts, IEnumerable<VFXData> compilableData, ref SubgraphInfos subgraphInfos)
{
var contextEffectiveInputLinks = subgraphInfos.contextEffectiveInputLinks;
var allPlayNotLinked = contextSpawnToSpawnInfo.Where(o => !contextEffectiveInputLinks[o.Key][0].Any()).Select(o => o.Key).ToList();
var allStopNotLinked = contextSpawnToSpawnInfo.Where(o => !contextEffectiveInputLinks[o.Key][1].Any()).Select(o => o.Key).ToList();
var eventDescTemp = new EventDesc[]
{
new EventDesc() { name = VisualEffectAsset.PlayEventName, startSystems = allPlayNotLinked, stopSystems = new List<VFXContext>(), initSystems = new List<VFXContext>() },
new EventDesc() { name = VisualEffectAsset.StopEventName, startSystems = new List<VFXContext>(), stopSystems = allStopNotLinked, initSystems = new List<VFXContext>() },
}.ToList();
var specialNames = new HashSet<string>(new string[] { VisualEffectAsset.PlayEventName, VisualEffectAsset.StopEventName });
var events = contexts.Where(o => o.contextType == VFXContextType.Event);
foreach (var evt in events)
{
var eventName = (evt as VFXBasicEvent).eventName;
if (subgraphInfos.spawnerSubgraph.ContainsKey(evt) && specialNames.Contains(eventName))
continue;
List<VFXContextLink> effectiveOuts = subgraphInfos.GetContextEffectiveOutputLinks(evt, 0);
foreach (var link in effectiveOuts)
{
var eventIndex = eventDescTemp.FindIndex(o => o.name == eventName);
if (eventIndex == -1)
{
eventIndex = eventDescTemp.Count;
eventDescTemp.Add(new EventDesc
{
name = eventName,
startSystems = new List<VFXContext>(),
stopSystems = new List<VFXContext>(),
initSystems = new List<VFXContext>()
});
}
var eventDesc = eventDescTemp[eventIndex];
if (link.context.contextType == VFXContextType.Spawner)
{
if (contextSpawnToSpawnInfo.ContainsKey(link.context))
{
var startSystem = link.slotIndex == 0;
if (startSystem)
{
eventDesc.startSystems.Add(link.context);
}
else
{
eventDesc.stopSystems.Add(link.context);
}
}
}
else if (link.context.contextType == VFXContextType.Init)
{
eventDesc.initSystems.Add(link.context);
}
else
{
throw new InvalidOperationException(string.Format("Unexpected link context : " + link.context.contextType));
}
}
}
outEventDesc.AddRange(eventDescTemp);
}
private void GenerateShaders(List<GeneratedCodeData> outGeneratedCodeData, VFXExpressionGraph graph, IEnumerable<VFXContext> contexts, Dictionary<VFXContext, VFXContextCompiledData> contextToCompiledData, VFXCompilationMode compilationMode, HashSet<string> dependencies)
{
Profiler.BeginSample("VFXEditor.GenerateShaders");
try
{
foreach (var context in contexts)
{
var gpuMapper = graph.BuildGPUMapper(context);
var uniformMapper = new VFXUniformMapper(gpuMapper, context.doesGenerateShader);
// Add gpu and uniform mapper
var contextData = contextToCompiledData[context];
contextData.gpuMapper = gpuMapper;
contextData.uniformMapper = uniformMapper;
contextData.graphicsBufferUsage = graph.GraphicsBufferTypeUsage;
contextToCompiledData[context] = contextData;
if (context.doesGenerateShader)
{
var generatedContent = VFXCodeGenerator.Build(context, compilationMode, contextData, dependencies);
if (generatedContent != null)
{
outGeneratedCodeData.Add(new GeneratedCodeData()
{
context = context,
computeShader = context.codeGeneratorCompute,
compilMode = compilationMode,
content = generatedContent
});
}
}
}
var resource = m_Graph.GetResource();
}
finally
{
Profiler.EndSample();
}
}
private static VFXShaderSourceDesc[] SaveShaderFiles(VisualEffectResource resource,
List<GeneratedCodeData> generatedCodeData,
Dictionary<VFXContext, VFXContextCompiledData> contextToCompiledData,
VFXSystemNames systemNames)
{
Profiler.BeginSample("VFXEditor.SaveShaderFiles");
try
{
var descs = new VFXShaderSourceDesc[generatedCodeData.Count];
var assetName = string.Empty;
if (resource.asset != null)
{
assetName = resource.asset.name; //Most Common case, asset is already available
}
else
{
var assetPath = AssetDatabase.GetAssetPath(resource); //Can occur during Copy/Past or Rename
if (!string.IsNullOrEmpty(assetPath))
{
assetName = Path.GetFileNameWithoutExtension(assetPath);
}
else if (resource.name != null) //Unable to retrieve asset path, last fallback use serialized resource name
{
assetName = resource.name;
}
}
for (int i = 0; i < generatedCodeData.Count; ++i)
{
var generated = generatedCodeData[i];
var systemName = systemNames.GetUniqueSystemName(generated.context.GetData());
var contextLetter = generated.context.letter;
var contextName = string.IsNullOrEmpty(generated.context.label) ? generated.context.libraryName : generated.context.label;
var shaderName = string.Empty;
var fileName = string.Empty;
if (contextLetter == '\0')
{
fileName = string.Format("[{0}] [{1}] {2}", assetName, systemName, contextName);
shaderName = string.Format("Hidden/VFX/{0}/{1}/{2}", assetName, systemName, contextName);
}
else
{
fileName = string.Format("[{0}] [{1}]{2} {3}", assetName, systemName, contextLetter, contextName);
shaderName = string.Format("Hidden/VFX/{0}/{1}/{2}/{3}", assetName, systemName, contextLetter, contextName);
}
if (!generated.computeShader)
{
generated.content.Insert(0, "Shader \"" + shaderName + "\"\n");
}
descs[i].source = generated.content.ToString();
descs[i].name = fileName;
descs[i].compute = generated.computeShader;
}
for (int i = 0; i < generatedCodeData.Count; ++i)
{
var generated = generatedCodeData[i];
var contextData = contextToCompiledData[generated.context];
contextData.indexInShaderSource = i;
contextToCompiledData[generated.context] = contextData;
}
return descs;
}
finally
{
Profiler.EndSample();
}
}
public void FillDependentBuffer(
IEnumerable<VFXData> compilableData,
List<VFXGPUBufferDesc> bufferDescs,
VFXDependentBuffersData buffers)
{
// TODO This should be in VFXDataParticle
foreach (var data in compilableData.OfType<VFXDataParticle>())
{
int attributeBufferIndex = -1;
if (data.attributeBufferSize > 0)
{
attributeBufferIndex = bufferDescs.Count;
bufferDescs.Add(data.attributeBufferDesc);
}
buffers.attributeBuffers.Add(data, attributeBufferIndex);
int stripBufferIndex = -1;
if (data.hasStrip)
{
stripBufferIndex = bufferDescs.Count;
uint stripCapacity = (uint)data.GetSettingValue("stripCapacity");
bufferDescs.Add(new VFXGPUBufferDesc() { type = ComputeBufferType.Default, size = stripCapacity * 5, stride = 4 });
}
buffers.stripBuffers.Add(data, stripBufferIndex);
int boundsBufferIndex = -1;
if (data.NeedsComputeBounds())
{
boundsBufferIndex = bufferDescs.Count;
bufferDescs.Add(new VFXGPUBufferDesc() { type = ComputeBufferType.Default, size = 6, stride = 4 });
}
buffers.boundsBuffers.Add(data, boundsBufferIndex);
}
//Prepare GPU event buffer
foreach (var data in compilableData.SelectMany(o => o.dependenciesOut).Distinct().OfType<VFXDataParticle>())
{
var eventBufferIndex = -1;
uint capacity = (uint)data.GetSettingValue("capacity");
if (capacity > 0)
{
eventBufferIndex = bufferDescs.Count;
bufferDescs.Add(new VFXGPUBufferDesc() { type = ComputeBufferType.Append, size = capacity, stride = 4 });
}
buffers.eventBuffers.Add(data, eventBufferIndex);
}
}
VFXRendererSettings GetRendererSettings(VFXRendererSettings initialSettings, IEnumerable<IVFXSubRenderer> subRenderers)
{
var settings = initialSettings;
settings.shadowCastingMode = subRenderers.Any(r => r.hasShadowCasting) ? ShadowCastingMode.On : ShadowCastingMode.Off;
settings.motionVectorGenerationMode = subRenderers.Any(r => r.hasMotionVector) ? MotionVectorGenerationMode.Object : MotionVectorGenerationMode.Camera;
return settings;
}
private class VFXImplicitContextOfExposedExpression : VFXContext
{
private VFXExpressionMapper mapper;
public VFXImplicitContextOfExposedExpression() : base(VFXContextType.None, VFXDataType.None, VFXDataType.None) { }
private static void CollectExposedExpression(List<VFXExpression> expressions, VFXSlot slot)
{
var expression = slot.valueType != VFXValueType.None ? slot.GetInExpression() : null;
if (expression != null)
expressions.Add(expression);
else
{
foreach (var child in slot.children)
CollectExposedExpression(expressions, child);
}
}
public void FillExpression(VFXGraph graph)
{
var allExposedParameter = graph.children.OfType<VFXParameter>().Where(o => o.exposed);
var expressionsList = new List<VFXExpression>();
foreach (var parameter in allExposedParameter)
CollectExposedExpression(expressionsList, parameter.outputSlots[0]);
mapper = new VFXExpressionMapper();
for (int i = 0; i < expressionsList.Count; ++i)
mapper.AddExpression(expressionsList[i], "ImplicitExposedExpression", i);
}
public override VFXExpressionMapper GetExpressionMapper(VFXDeviceTarget target)
{
return target == VFXDeviceTarget.CPU ? mapper : null;
}
}
void ComputeEffectiveInputLinks(ref SubgraphInfos subgraphInfos, IEnumerable<VFXContext> compilableContexts)
{
var contextEffectiveInputLinks = subgraphInfos.contextEffectiveInputLinks;
foreach (var context in compilableContexts.Where(t => !(t is VFXSubgraphContext)))
{
contextEffectiveInputLinks[context] = ComputeContextEffectiveLinks(context, ref subgraphInfos);
ComputeEffectiveInputLinks(ref subgraphInfos, contextEffectiveInputLinks[context].SelectMany(t => t).Select(t => t.context).Where(t => !contextEffectiveInputLinks.ContainsKey(t)));
}
}
struct EventDesc
{
public string name;
public List<VFXContext> startSystems;
public List<VFXContext> stopSystems;
public List<VFXContext> initSystems;
}
static IEnumerable<uint> ConvertDataToSystemIndex(IEnumerable<VFXContext> input, Dictionary<VFXContext, uint> contextToSystemIndex)
{
foreach (var data in input)
if (contextToSystemIndex.TryGetValue(data, out var index))
yield return index;
}
private void CleanRuntimeData()
{
if (m_Graph.visualEffectResource != null)
m_Graph.visualEffectResource.ClearRuntimeData();
m_ExpressionGraph = new VFXExpressionGraph();
m_ExpressionValues = new VFXExpressionValueContainerDesc[] { };
}
public void Compile(VFXCompilationMode compilationMode, bool forceShaderValidation)
{