forked from Unity-Technologies/Graphics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVFXCodeGenerator.cs
More file actions
868 lines (740 loc) · 45.4 KB
/
Copy pathVFXCodeGenerator.cs
File metadata and controls
868 lines (740 loc) · 45.4 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
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.VFX;
using UnityEditor.ShaderGraph;
using UnityEditor.Graphing.Util;
using UnityEditor.ShaderGraph.Serialization;
using Object = UnityEngine.Object;
using System.Text.RegularExpressions;
using System.Globalization;
using UnityEngine.Profiling;
namespace UnityEditor.VFX
{
static class VFXCodeGenerator
{
private static string GetIndent(string src, int index)
{
var indent = "";
index--;
while (index > 0 && (src[index] == ' ' || src[index] == '\t'))
{
indent = src[index] + indent;
index--;
}
return indent;
}
//This function insure to keep padding while replacing a specific string
private static void ReplaceMultiline(StringBuilder target, string targetQuery, StringBuilder value)
{
Profiler.BeginSample("ReplaceMultiline");
string[] delim = { System.Environment.NewLine, "\n" };
var valueLines = value.ToString().Split(delim, System.StringSplitOptions.None);
if (valueLines.Length <= 1)
{
target.Replace(targetQuery, value.ToString());
}
else
{
while (true)
{
var targetCopy = target.ToString();
var index = targetCopy.IndexOf(targetQuery);
if (index == -1)
{
break;
}
var indent = GetIndent(targetCopy, index);
var currentValue = new StringBuilder();
foreach (var line in valueLines)
{
currentValue.Append(indent + line + '\n');
}
target.Replace(indent + targetQuery, currentValue.ToString());
}
}
Profiler.EndSample();
}
internal static VFXShaderWriter GenerateLoadAttribute(string matching, VFXContext context)
{
var r = new VFXShaderWriter();
var regex = new Regex(matching);
var attributesFromContext = context.GetData().GetAttributes().Where(o => regex.IsMatch(o.attrib.name)).ToArray();
var attributesSource = attributesFromContext.Where(a => context.GetData().IsSourceAttributeUsed(a.attrib, context)).ToArray();
var attributesCurrent = attributesFromContext.Where(a => context.GetData().IsCurrentAttributeUsed(a.attrib, context) || (context.contextType == VFXContextType.Init && context.GetData().IsAttributeStored(a.attrib))).ToArray();
//< Current Attribute
foreach (var attribute in attributesCurrent.Select(o => o.attrib))
{
var name = attribute.GetNameInCode(VFXAttributeLocation.Current);
if (attribute.name != VFXAttribute.EventCount.name)
{
if (context.contextType != VFXContextType.Init && context.GetData().IsAttributeStored(attribute))
{
r.WriteAssignement(attribute.type, name, context.GetData().GetLoadAttributeCode(attribute, VFXAttributeLocation.Current));
}
else
{
r.WriteAssignement(attribute.type, name, attribute.value.GetCodeString(null));
}
}
else
{
var linkedOutCount = context.allLinkedOutputSlot.Count();
r.WriteAssignement(attribute.type, name, attribute.value.GetCodeString(null));
for (uint i = 0; i < linkedOutCount; ++i)
{
r.WriteLine();
r.WriteFormat("uint {0}_{1} = 0u;", VFXAttribute.EventCount.name, VFXCodeGeneratorHelper.GeneratePrefix(i));
}
}
r.WriteLine();
}
//< Source Attribute (default temporary behavior, source is always the initial current value except for init context)
foreach (var attribute in attributesSource.Select(o => o.attrib))
{
var name = attribute.GetNameInCode(VFXAttributeLocation.Source);
if (context.contextType == VFXContextType.Init)
{
r.WriteAssignement(attribute.type, name, context.GetData().GetLoadAttributeCode(attribute, VFXAttributeLocation.Source));
}
else
{
if (attributesCurrent.Any(o => o.attrib.name == attribute.name))
{
var reference = new VFXAttributeExpression(new VFXAttribute(attribute.name, attribute.value), VFXAttributeLocation.Current);
r.WriteAssignement(reference.valueType, name, reference.GetCodeString(null));
}
else
{
r.WriteAssignement(attribute.type, name, attribute.value.GetCodeString(null));
}
}
r.WriteLine();
}
return r;
}
private const string eventListOutName = "eventListOut";
static private VFXShaderWriter GenerateStoreAttribute(string matching, VFXContext context, uint linkedOutCount)
{
var r = new VFXShaderWriter();
var regex = new Regex(matching);
var attributesFromContext = context.GetData().GetAttributes().Where(o => regex.IsMatch(o.attrib.name) &&
context.GetData().IsAttributeStored(o.attrib) &&
(context.contextType == VFXContextType.Init || context.GetData().IsCurrentAttributeWritten(o.attrib, context))).ToArray();
foreach (var attribute in attributesFromContext.Select(o => o.attrib))
{
r.Write(context.GetData().GetStoreAttributeCode(attribute, new VFXAttributeExpression(attribute).GetCodeString(null)));
r.WriteLine(';');
}
if (regex.IsMatch(VFXAttribute.EventCount.name))
{
for (uint i = 0; i < linkedOutCount; ++i)
{
var prefix = VFXCodeGeneratorHelper.GeneratePrefix(i);
r.WriteLineFormat("for (uint i_{0} = 0; i_{0} < {1}_{0}; ++i_{0}) {2}_{0}.Append(index);", prefix, VFXAttribute.EventCount.name, eventListOutName);
}
}
return r;
}
static internal VFXShaderWriter GenerateLoadParameter(string matching, VFXNamedExpression[] namedExpressions, Dictionary<VFXExpression, string> expressionToName)
{
var r = new VFXShaderWriter();
var regex = new Regex(matching);
var filteredNamedExpressions = namedExpressions.Where(o => regex.IsMatch(o.name) &&
!(expressionToName.ContainsKey(o.exp) && expressionToName[o.exp] == o.name)); // if parameter already in the global scope, there's nothing to do
bool needScope = false;
foreach (var namedExpression in filteredNamedExpressions)
{
r.WriteVariable(namedExpression.exp.valueType, namedExpression.name, "0");
r.WriteLine();
needScope = true;
}
if (needScope)
{
var expressionToNameLocal = new Dictionary<VFXExpression, string>(expressionToName);
r.EnterScope();
foreach (var namedExpression in filteredNamedExpressions)
{
if (!expressionToNameLocal.ContainsKey(namedExpression.exp))
{
r.WriteVariable(namedExpression.exp, expressionToNameLocal);
r.WriteLine();
}
r.WriteAssignement(namedExpression.exp.valueType, namedExpression.name, expressionToNameLocal[namedExpression.exp]);
r.WriteLine();
}
r.ExitScope();
}
return r;
}
static public StringBuilder Build(VFXContext context, VFXCompilationMode compilationMode, VFXContextCompiledData contextData, HashSet<string> dependencies)
{
var templatePath = string.Format("{0}.template", context.codeGeneratorTemplate);
dependencies.Add(AssetDatabase.AssetPathToGUID(templatePath));
return Build(context, templatePath, compilationMode, contextData, dependencies);
}
static private void GetFunctionName(VFXBlock block, out string functionName, out string comment)
{
var settings = block.GetSettings(true).ToArray();
if (settings.Length > 0)
{
comment = "";
int hash = 0;
foreach (var setting in settings)
{
var value = setting.value;
hash = (hash * 397) ^ value.GetHashCode();
comment += string.Format("{0}:{1} ", setting.field.Name, value.ToString());
}
functionName = string.Format("{0}_{1}", block.GetType().Name, hash.ToString("X"));
}
else
{
comment = null;
functionName = block.GetType().Name;
}
}
static private string FormatPath(string path)
{
return Path.GetFullPath(path)
.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar)
#if !UNITY_EDITOR_LINUX
.ToLowerInvariant()
#endif
;
}
static IEnumerable<Match> GetUniqueMatches(string regexStr, string src)
{
var regex = new Regex(regexStr);
var matches = regex.Matches(src);
return matches.Cast<Match>().GroupBy(m => m.Groups[0].Value).Select(g => g.First());
}
static private StringBuilder GetFlattenedTemplateContent(string path, List<string> includes, IEnumerable<string> defines, HashSet<string> dependencies)
{
var formattedPath = FormatPath(path);
if (includes.Contains(formattedPath))
{
var includeHierarchy = new StringBuilder(string.Format("Cyclic VFXInclude dependency detected: {0}\n", formattedPath));
foreach (var str in Enumerable.Reverse<string>(includes))
includeHierarchy.Append(str + '\n');
throw new InvalidOperationException(includeHierarchy.ToString());
}
includes.Add(formattedPath);
var templateContent = new StringBuilder(System.IO.File.ReadAllText(formattedPath));
foreach (var match in GetUniqueMatches("\\${VFXInclude(RP|)\\(\\\"(.*?)\\\"\\)(,.*)?}", templateContent.ToString()))
{
var groups = match.Groups;
var renderPipelineInclude = groups[1].Value == "RP";
var includePath = groups[2].Value;
if (groups.Count > 3 && !String.IsNullOrEmpty(groups[2].Value))
{
var allDefines = groups[3].Value.Split(new char[] { ',', ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
var neededDefines = allDefines.Where(d => d[0] != '!');
var forbiddenDefines = allDefines.Except(neededDefines).Select(d => d.Substring(1));
if (!neededDefines.All(d => defines.Contains(d)) || forbiddenDefines.Any(d => defines.Contains(d)))
{
ReplaceMultiline(templateContent, groups[0].Value, new StringBuilder());
continue;
}
}
string absolutePath;
if (renderPipelineInclude)
absolutePath = VFXLibrary.currentSRPBinder.templatePath + "/" + includePath;
else
absolutePath = VisualEffectGraphPackageInfo.assetPackagePath + "/" + includePath;
dependencies.Add(AssetDatabase.AssetPathToGUID(absolutePath));
var includeBuilder = GetFlattenedTemplateContent(absolutePath, includes, defines, dependencies);
ReplaceMultiline(templateContent, groups[0].Value, includeBuilder);
}
includes.Remove(formattedPath);
return templateContent;
}
static private void SubstituteMacros(StringBuilder builder)
{
var definesToCode = new Dictionary<string, string>();
var source = builder.ToString();
Regex beginRegex = new Regex("\\${VFXBegin:(.*)}");
int currentPos = -1;
int builderOffset = 0;
while ((currentPos = source.IndexOf("${")) != -1)
{
int endPos = source.IndexOf('}', currentPos);
if (endPos == -1)
throw new FormatException("Ill-formed VFX tag (Missing closing brace");
var tag = source.Substring(currentPos, endPos - currentPos + 1);
// Replace any tag found
string macro;
if (definesToCode.TryGetValue(tag, out macro))
{
builder.Remove(currentPos + builderOffset, tag.Length);
var indentedMacro = macro.Replace("\n", "\n" + GetIndent(source, currentPos));
builder.Insert(currentPos + builderOffset, indentedMacro);
}
else
{
const string endStr = "${VFXEnd}";
var match = beginRegex.Match(source, currentPos, tag.Length);
if (match.Success)
{
var macroStartPos = match.Index + match.Length;
var macroEndCodePos = source.IndexOf(endStr, macroStartPos);
if (macroEndCodePos == -1)
throw new FormatException("${VFXBegin} found without ${VFXEnd}");
var defineStr = "${" + match.Groups[1].Value + "}";
definesToCode[defineStr] = source.Substring(macroStartPos, macroEndCodePos - macroStartPos);
// Remove the define in builder
builder.Remove(match.Index + builderOffset, macroEndCodePos - match.Index + endStr.Length);
}
else if (tag == endStr)
throw new FormatException("${VFXEnd} found without ${VFXBegin}");
else // Remove undefined tag
builder.Remove(currentPos + builderOffset, tag.Length);
}
builderOffset += currentPos;
source = builder.ToString(builderOffset, builder.Length - builderOffset);
}
}
internal static void BuildContextBlocks(VFXContext context, VFXContextCompiledData contextData,
out string blockFunctionContent,
out string blockCallFunctionContent)
{
var linkedEventOut = context.allLinkedOutputSlot.Where(s => ((VFXModel)s.owner).GetFirstOfType<VFXContext>().CanBeCompiled()).ToList();
//< Block processor
var blockFunction = new VFXShaderWriter();
var blockCallFunction = new VFXShaderWriter();
var blockDeclared = new HashSet<string>();
var expressionToName = context.GetData().GetAttributes().ToDictionary(o => new VFXAttributeExpression(o.attrib) as VFXExpression, o => (new VFXAttributeExpression(o.attrib)).GetCodeString(null));
expressionToName = expressionToName.Union(contextData.uniformMapper.expressionToCode).ToDictionary(s => s.Key, s => s.Value);
int cpt = 0;
foreach (var current in context.activeFlattenedChildrenWithImplicit)
{
BuildBlock(contextData, linkedEventOut, blockFunction, blockCallFunction, blockDeclared, expressionToName, current, ref cpt);
}
blockFunctionContent = blockFunction.builder.ToString();
blockCallFunctionContent = blockCallFunction.builder.ToString();
}
internal static void BuildParameterBuffer(VFXContextCompiledData contextData, IEnumerable<string> filteredOutTextures, out string parameterBufferContent)
{
var parameterBuffer = new VFXShaderWriter();
parameterBuffer.WriteCBuffer(contextData.uniformMapper, "parameters");
parameterBuffer.WriteLine();
parameterBuffer.WriteBufferTypeDeclaration(contextData.graphicsBufferUsage.Values.Distinct());
parameterBuffer.WriteLine();
parameterBuffer.WriteBuffer(contextData.uniformMapper, contextData.graphicsBufferUsage);
parameterBuffer.WriteLine();
parameterBuffer.WriteTexture(contextData.uniformMapper, filteredOutTextures);
parameterBufferContent = parameterBuffer.ToString();
}
internal static void BuildVertexProperties(VFXContext context, VFXContextCompiledData contextData, out string vertexProperties)
{
var expressionToName = context.GetData().GetAttributes().ToDictionary(o => new VFXAttributeExpression(o.attrib) as VFXExpression, o => (new VFXAttributeExpression(o.attrib)).GetCodeString(null));
expressionToName = expressionToName.Union(contextData.uniformMapper.expressionToCode).ToDictionary(s => s.Key, s => s.Value);
var mainParameters = contextData.gpuMapper.CollectExpression(-1).ToArray();
var additionalVertexProperties = new VFXShaderWriter();
foreach (string vertexParameter in context.vertexParameters)
{
var filteredNamedExpression = mainParameters.FirstOrDefault(o => vertexParameter == o.name &&
!(expressionToName.ContainsKey(o.exp) && expressionToName[o.exp] == o.name)); // if parameter already in the global scope, there's nothing to do
if (filteredNamedExpression.exp != null)
{
additionalVertexProperties.WriteVariable(filteredNamedExpression.exp.valueType, filteredNamedExpression.name + "__", "0");
var expressionToNameLocal = new Dictionary<VFXExpression, string>(expressionToName);
additionalVertexProperties.EnterScope();
{
if (!expressionToNameLocal.ContainsKey(filteredNamedExpression.exp))
{
additionalVertexProperties.WriteVariable(filteredNamedExpression.exp, expressionToNameLocal);
additionalVertexProperties.WriteLine();
}
additionalVertexProperties.WriteAssignement(filteredNamedExpression.exp.valueType, filteredNamedExpression.name + "__", expressionToNameLocal[filteredNamedExpression.exp]);
additionalVertexProperties.WriteLine();
}
additionalVertexProperties.ExitScope();
}
}
vertexProperties = additionalVertexProperties.ToString();
}
internal static void BuildVertexPropertiesAssign(VFXContext context, VFXContextCompiledData contextData, out string buildVertexPropertiesGeneration)
{
var expressionToName = context.GetData().GetAttributes().ToDictionary(o => new VFXAttributeExpression(o.attrib) as VFXExpression, o => (new VFXAttributeExpression(o.attrib)).GetCodeString(null));
expressionToName = expressionToName.Union(contextData.uniformMapper.expressionToCode).ToDictionary(s => s.Key, s => s.Value);
var mainParameters = contextData.gpuMapper.CollectExpression(-1).ToArray();
var vertexInputsGeneration = new VFXShaderWriter();
foreach (string vertexParameter in context.vertexParameters)
{
var filteredNamedExpression = mainParameters.FirstOrDefault(o => vertexParameter == o.name);
if (filteredNamedExpression.exp == null)
throw new InvalidOperationException(string.Format("Cannot find vertex property : {0}", vertexParameter));
// If the parameter is in the global scope, read from the cbuffer directly (no suffix).
if (!(expressionToName.ContainsKey(filteredNamedExpression.exp) && expressionToName[filteredNamedExpression.exp] == filteredNamedExpression.name))
vertexInputsGeneration.WriteAssignement(filteredNamedExpression.exp.valueType, $"properties.{filteredNamedExpression.name}", $"{filteredNamedExpression.name}__");
else
vertexInputsGeneration.WriteAssignement(filteredNamedExpression.exp.valueType, $"properties.{filteredNamedExpression.name}", $"{filteredNamedExpression.name}");
vertexInputsGeneration.WriteLine();
}
buildVertexPropertiesGeneration = vertexInputsGeneration.ToString();
}
internal static void BuildInterpolatorBlocks(VFXContext context, VFXContextCompiledData contextData,
out string interpolatorsGeneration)
{
var expressionToName = context.GetData().GetAttributes().ToDictionary(o => new VFXAttributeExpression(o.attrib) as VFXExpression, o => (new VFXAttributeExpression(o.attrib)).GetCodeString(null));
expressionToName = expressionToName.Union(contextData.uniformMapper.expressionToCode).ToDictionary(s => s.Key, s => s.Value);
var mainParameters = contextData.gpuMapper.CollectExpression(-1).ToArray();
var additionalInterpolantsGeneration = new VFXShaderWriter();
var additionalInterpolantsPreparation = new VFXShaderWriter();
foreach (string fragmentParameter in context.fragmentParameters)
{
var filteredNamedExpression = mainParameters.FirstOrDefault(o => fragmentParameter == o.name &&
!(expressionToName.ContainsKey(o.exp) && expressionToName[o.exp] == o.name)); // if parameter already in the global scope, there's nothing to do
if (filteredNamedExpression.exp != null)
{
additionalInterpolantsGeneration.WriteVariable(filteredNamedExpression.exp.valueType, filteredNamedExpression.name + "__", "0");
var expressionToNameLocal = new Dictionary<VFXExpression, string>(expressionToName);
additionalInterpolantsGeneration.EnterScope();
{
if (!expressionToNameLocal.ContainsKey(filteredNamedExpression.exp))
{
additionalInterpolantsGeneration.WriteVariable(filteredNamedExpression.exp, expressionToNameLocal);
additionalInterpolantsGeneration.WriteLine();
}
additionalInterpolantsGeneration.WriteAssignement(filteredNamedExpression.exp.valueType, filteredNamedExpression.name + "__", expressionToNameLocal[filteredNamedExpression.exp]);
additionalInterpolantsGeneration.WriteLine();
}
additionalInterpolantsGeneration.ExitScope();
additionalInterpolantsGeneration.WriteAssignement(filteredNamedExpression.exp.valueType, "output." + filteredNamedExpression.name, filteredNamedExpression.name + "__");
additionalInterpolantsPreparation.WriteVariable(filteredNamedExpression.exp.valueType, filteredNamedExpression.name, "i." + filteredNamedExpression.name);
}
}
interpolatorsGeneration = additionalInterpolantsGeneration.ToString();
}
internal static void BuildFragInputsGeneration(VFXContext context, VFXContextCompiledData contextData, bool useFragInputs, out string buildFragInputsGeneration)
{
var expressionToName = context.GetData().GetAttributes().ToDictionary(o => new VFXAttributeExpression(o.attrib) as VFXExpression, o => (new VFXAttributeExpression(o.attrib)).GetCodeString(null));
expressionToName = expressionToName.Union(contextData.uniformMapper.expressionToCode).ToDictionary(s => s.Key, s => s.Value);
var mainParameters = contextData.gpuMapper.CollectExpression(-1).ToArray();
var fragInputsGeneration = new VFXShaderWriter();
foreach (string fragmentParameter in context.fragmentParameters)
{
var filteredNamedExpression = mainParameters.FirstOrDefault(o => fragmentParameter == o.name);
if (filteredNamedExpression.exp == null)
throw new InvalidOperationException("FragInputs generation failed to find expected parameter: " + fragmentParameter);
var isInterpolant = !(expressionToName.ContainsKey(filteredNamedExpression.exp) && expressionToName[filteredNamedExpression.exp] == filteredNamedExpression.name);
var surfaceSetter = useFragInputs ? "output.vfx" : "output";
fragInputsGeneration.WriteAssignement(filteredNamedExpression.exp.valueType, $"{surfaceSetter}.{filteredNamedExpression.name}", $"{(isInterpolant ? "input." : string.Empty)}{filteredNamedExpression.name}");
fragInputsGeneration.WriteLine();
}
buildFragInputsGeneration = fragInputsGeneration.ToString();
}
internal static void BuildPixelPropertiesAssign(VFXContext context, VFXContextCompiledData contextData, bool useFragInputs, out string buildFragInputsGeneration)
{
var expressionToName = context.GetData().GetAttributes().ToDictionary(o => new VFXAttributeExpression(o.attrib) as VFXExpression, o => (new VFXAttributeExpression(o.attrib)).GetCodeString(null));
expressionToName = expressionToName.Union(contextData.uniformMapper.expressionToCode).ToDictionary(s => s.Key, s => s.Value);
var mainParameters = contextData.gpuMapper.CollectExpression(-1).ToArray();
var fragInputsGeneration = new VFXShaderWriter();
foreach (string fragmentParameter in context.fragmentParameters)
{
var filteredNamedExpression = mainParameters.FirstOrDefault(o => fragmentParameter == o.name);
var surfaceGetter = useFragInputs ? "fragInputs.vfx" : "fragInputs";
fragInputsGeneration.WriteAssignement(filteredNamedExpression.exp.valueType, $"properties.{filteredNamedExpression.name}", $"{surfaceGetter}.{filteredNamedExpression.name}");
fragInputsGeneration.WriteLine();
}
buildFragInputsGeneration = fragInputsGeneration.ToString();
}
static private StringBuilder Build(VFXContext context, string templatePath, VFXCompilationMode compilationMode, VFXContextCompiledData contextData, HashSet<string> dependencies)
{
if (!context.SetupCompilation())
return null;
if (context is VFXShaderGraphParticleOutput shaderGraphContext &&
shaderGraphContext.GetOrRefreshShaderGraphObject() != null &&
shaderGraphContext.GetOrRefreshShaderGraphObject().generatesWithShaderGraph &&
VFXViewPreference.generateOutputContextWithShaderGraph)
{
var result = TryBuildFromShaderGraph(shaderGraphContext, contextData);
// If the ShaderGraph generation path was successful, use the result, otherwise fall back to the VFX generation path.
if (result != null)
{
context.EndCompilation();
return result;
}
}
var stringBuilder = GetFlattenedTemplateContent(templatePath, new List<string>(), context.additionalDefines, dependencies);
var allCurrentAttributes = context.GetData().GetAttributes().Where(a =>
(context.GetData().IsCurrentAttributeUsed(a.attrib, context)) ||
(context.contextType == VFXContextType.Init && context.GetData().IsAttributeStored(a.attrib))); // In init, needs to declare all stored attributes for intialization
var allSourceAttributes = context.GetData().GetAttributes().Where(a => (context.GetData().IsSourceAttributeUsed(a.attrib, context)));
var globalDeclaration = new VFXShaderWriter();
globalDeclaration.WriteBufferTypeDeclaration(contextData.graphicsBufferUsage.Values.Distinct());
globalDeclaration.WriteLine();
globalDeclaration.WriteCBuffer(contextData.uniformMapper, "parameters");
globalDeclaration.WriteLine();
globalDeclaration.WriteBuffer(contextData.uniformMapper, contextData.graphicsBufferUsage);
globalDeclaration.WriteLine();
globalDeclaration.WriteTexture(contextData.uniformMapper);
globalDeclaration.WriteAttributeStruct(allCurrentAttributes.Select(a => a.attrib), "VFXAttributes");
globalDeclaration.WriteLine();
globalDeclaration.WriteAttributeStruct(allSourceAttributes.Select(a => a.attrib), "VFXSourceAttributes");
globalDeclaration.WriteLine();
var linkedEventOut = context.allLinkedOutputSlot.Where(s => ((VFXModel)s.owner).GetFirstOfType<VFXContext>().CanBeCompiled()).ToList();
globalDeclaration.WriteEventBuffers(eventListOutName, linkedEventOut.Count);
//< Block processor
var blockFunction = new VFXShaderWriter();
var blockCallFunction = new VFXShaderWriter();
var blockDeclared = new HashSet<string>();
var expressionToName = context.GetData().GetAttributes().ToDictionary(o => new VFXAttributeExpression(o.attrib) as VFXExpression, o => (new VFXAttributeExpression(o.attrib)).GetCodeString(null));
expressionToName = expressionToName.Union(contextData.uniformMapper.expressionToCode).ToDictionary(s => s.Key, s => s.Value);
int cpt = 0;
foreach (var current in context.activeFlattenedChildrenWithImplicit)
{
BuildBlock(contextData, linkedEventOut, blockFunction, blockCallFunction, blockDeclared, expressionToName, current, ref cpt);
}
//< Final composition
var globalIncludeContent = new VFXShaderWriter();
globalIncludeContent.WriteLine("#define NB_THREADS_PER_GROUP 64");
globalIncludeContent.WriteLine("#define HAS_VFX_ATTRIBUTES 1");
globalIncludeContent.WriteLine("#define VFX_PASSDEPTH_ACTUAL (0)");
globalIncludeContent.WriteLine("#define VFX_PASSDEPTH_MOTION_VECTOR (1)");
globalIncludeContent.WriteLine("#define VFX_PASSDEPTH_SELECTION (2)");
globalIncludeContent.WriteLine("#define VFX_PASSDEPTH_SHADOW (3)");
foreach (var attribute in allCurrentAttributes)
globalIncludeContent.WriteLineFormat("#define VFX_USE_{0}_{1} 1", attribute.attrib.name.ToUpper(CultureInfo.InvariantCulture), "CURRENT");
foreach (var attribute in allSourceAttributes)
globalIncludeContent.WriteLineFormat("#define VFX_USE_{0}_{1} 1", attribute.attrib.name.ToUpper(CultureInfo.InvariantCulture), "SOURCE");
foreach (var additionnalHeader in context.additionalDataHeaders)
globalIncludeContent.WriteLine(additionnalHeader);
foreach (var additionnalDefine in context.additionalDefines)
globalIncludeContent.WriteLineFormat("#define {0}{1}", additionnalDefine, additionnalDefine.Contains(' ') ? "" : " 1");
var renderTemplatePipePath = VFXLibrary.currentSRPBinder.templatePath;
var renderRuntimePipePath = VFXLibrary.currentSRPBinder.runtimePath;
if (!context.codeGeneratorCompute && !string.IsNullOrEmpty(renderTemplatePipePath))
{
string renderPipePasses = renderTemplatePipePath + "/VFXPasses.template";
globalIncludeContent.Write(GetFlattenedTemplateContent(renderPipePasses, new List<string>(), context.additionalDefines, dependencies));
}
if (context.GetData() is ISpaceable)
{
var spaceable = context.GetData() as ISpaceable;
globalIncludeContent.WriteLineFormat("#define {0} 1", spaceable.space == VFXCoordinateSpace.World ? "VFX_WORLD_SPACE" : "VFX_LOCAL_SPACE");
}
globalIncludeContent.WriteLineFormat("#include \"{0}/VFXDefines.hlsl\"", renderRuntimePipePath);
var perPassIncludeContent = new VFXShaderWriter();
string renderPipeCommon = context.doesIncludeCommonCompute ? "Packages/com.unity.visualeffectgraph/Shaders/Common/VFXCommonCompute.hlsl" : renderRuntimePipePath + "/VFXCommon.hlsl";
perPassIncludeContent.WriteLine("#include \"" + renderPipeCommon + "\"");
perPassIncludeContent.WriteLine("#include \"Packages/com.unity.visualeffectgraph/Shaders/VFXCommon.hlsl\"");
if (!context.codeGeneratorCompute)
{
perPassIncludeContent.WriteLine("#include \"Packages/com.unity.visualeffectgraph/Shaders/VFXCommonOutput.hlsl\"");
}
// Per-block includes
var includes = Enumerable.Empty<string>();
foreach (var block in context.activeFlattenedChildrenWithImplicit)
includes = includes.Concat(block.includes);
var uniqueIncludes = new HashSet<string>(includes);
foreach (var includePath in uniqueIncludes)
perPassIncludeContent.WriteLine(string.Format("#include \"{0}\"", includePath));
ReplaceMultiline(stringBuilder, "${VFXGlobalInclude}", globalIncludeContent.builder);
ReplaceMultiline(stringBuilder, "${VFXGlobalDeclaration}", globalDeclaration.builder);
ReplaceMultiline(stringBuilder, "${VFXPerPassInclude}", perPassIncludeContent.builder);
ReplaceMultiline(stringBuilder, "${VFXGeneratedBlockFunction}", blockFunction.builder);
ReplaceMultiline(stringBuilder, "${VFXProcessBlocks}", blockCallFunction.builder);
var mainParameters = contextData.gpuMapper.CollectExpression(-1).ToArray();
foreach (var match in GetUniqueMatches("\\${VFXLoadParameter:{(.*?)}}", stringBuilder.ToString()))
{
var str = match.Groups[0].Value;
var pattern = match.Groups[1].Value;
var loadParameters = GenerateLoadParameter(pattern, mainParameters, expressionToName);
ReplaceMultiline(stringBuilder, str, loadParameters.builder);
}
var additionalInterpolantsGeneration = new VFXShaderWriter();
var additionalInterpolantsDeclaration = new VFXShaderWriter();
var additionalInterpolantsPreparation = new VFXShaderWriter();
int normSemantic = 0;
foreach (string fragmentParameter in context.fragmentParameters)
{
var filteredNamedExpression = mainParameters.FirstOrDefault(o => fragmentParameter == o.name &&
!(expressionToName.ContainsKey(o.exp) && expressionToName[o.exp] == o.name)); // if parameter already in the global scope, there's nothing to do
if (filteredNamedExpression.exp != null)
{
if (!filteredNamedExpression.exp.Is(VFXExpression.Flags.Constant))
{
additionalInterpolantsDeclaration.WriteDeclaration(filteredNamedExpression.exp.valueType, filteredNamedExpression.name, $"NORMAL{normSemantic++}");
additionalInterpolantsGeneration.WriteVariable(filteredNamedExpression.exp.valueType, filteredNamedExpression.name + "__", "0");
var expressionToNameLocal = new Dictionary<VFXExpression, string>(expressionToName);
additionalInterpolantsGeneration.EnterScope();
{
if (!expressionToNameLocal.ContainsKey(filteredNamedExpression.exp))
{
additionalInterpolantsGeneration.WriteVariable(filteredNamedExpression.exp, expressionToNameLocal);
additionalInterpolantsGeneration.WriteLine();
}
additionalInterpolantsGeneration.WriteAssignement(filteredNamedExpression.exp.valueType, filteredNamedExpression.name + "__", expressionToNameLocal[filteredNamedExpression.exp]);
additionalInterpolantsGeneration.WriteLine();
}
additionalInterpolantsGeneration.ExitScope();
additionalInterpolantsGeneration.WriteAssignement(filteredNamedExpression.exp.valueType, "o." + filteredNamedExpression.name, filteredNamedExpression.name + "__");
additionalInterpolantsPreparation.WriteVariable(filteredNamedExpression.exp.valueType, filteredNamedExpression.name, "i." + filteredNamedExpression.name);
}
else
additionalInterpolantsPreparation.WriteVariable(filteredNamedExpression.exp.valueType, filteredNamedExpression.name, filteredNamedExpression.exp.GetCodeString(null));
}
}
ReplaceMultiline(stringBuilder, "${VFXAdditionalInterpolantsGeneration}", additionalInterpolantsGeneration.builder);
ReplaceMultiline(stringBuilder, "${VFXAdditionalInterpolantsDeclaration}", additionalInterpolantsDeclaration.builder);
ReplaceMultiline(stringBuilder, "${VFXAdditionalInterpolantsPreparation}", additionalInterpolantsPreparation.builder);
//< Load Attribute
if (stringBuilder.ToString().Contains("${VFXLoadAttributes}"))
{
var loadAttributes = GenerateLoadAttribute(".*", context);
ReplaceMultiline(stringBuilder, "${VFXLoadAttributes}", loadAttributes.builder);
}
foreach (var match in GetUniqueMatches("\\${VFXLoadAttributes:{(.*?)}}", stringBuilder.ToString()))
{
var str = match.Groups[0].Value;
var pattern = match.Groups[1].Value;
var loadAttributes = GenerateLoadAttribute(pattern, context);
ReplaceMultiline(stringBuilder, str, loadAttributes.builder);
}
//< Store Attribute
if (stringBuilder.ToString().Contains("${VFXStoreAttributes}"))
{
var storeAttribute = GenerateStoreAttribute(".*", context, (uint)linkedEventOut.Count);
ReplaceMultiline(stringBuilder, "${VFXStoreAttributes}", storeAttribute.builder);
}
foreach (var match in GetUniqueMatches("\\${VFXStoreAttributes:{(.*?)}}", stringBuilder.ToString()))
{
var str = match.Groups[0].Value;
var pattern = match.Groups[1].Value;
var storeAttributes = GenerateStoreAttribute(pattern, context, (uint)linkedEventOut.Count);
ReplaceMultiline(stringBuilder, str, storeAttributes.builder);
}
foreach (var addionalReplacement in context.additionalReplacements)
{
ReplaceMultiline(stringBuilder, addionalReplacement.Key, addionalReplacement.Value.builder);
}
// Replace defines
SubstituteMacros(stringBuilder);
if (VFXViewPreference.advancedLogs)
Debug.LogFormat("GENERATED_OUTPUT_FILE_FOR : {0}\n{1}", context.ToString(), stringBuilder.ToString());
context.EndCompilation();
return stringBuilder;
}
private static StringBuilder TryBuildFromShaderGraph(VFXShaderGraphParticleOutput context, VFXContextCompiledData contextData)
{
var stringBuilder = new StringBuilder();
// Reconstruct the ShaderGraph.
var path = AssetDatabase.GetAssetPath(context.GetOrRefreshShaderGraphObject());
List<PropertyCollector.TextureInfo> configuredTextures;
AssetCollection assetCollection = new AssetCollection();
MinimalGraphData.GatherMinimalDependenciesFromFile(path, assetCollection);
var textGraph = File.ReadAllText(path, Encoding.UTF8);
var graph = new GraphData
{
messageManager = new MessageManager(),
assetGuid = AssetDatabase.AssetPathToGUID(path)
};
MultiJson.Deserialize(graph, textGraph);
graph.OnEnable();
graph.ValidateGraph();
// Check the validity of the shader graph (unsupported keywords or shader property usage).
if (VFXLibrary.currentSRPBinder == null || !VFXLibrary.currentSRPBinder.IsGraphDataValid(graph))
return null;
var target = graph.activeTargets.Where(o =>
{
if (o.SupportsVFX())
{
//We are assuming the target has been implemented in the same package than srp binder.
var srpBinderAssembly = VFXLibrary.currentSRPBinder.GetType().Assembly;
var targetAssembly = o.GetType().Assembly;
if (srpBinderAssembly == targetAssembly)
return true;
}
return false;
}).FirstOrDefault();
if (target == null || !target.TryConfigureContextData(context, contextData))
return null;
// Use ShaderGraph to generate the VFX shader.
var text = ShaderGraphImporter.GetShaderText(path, out configuredTextures, assetCollection, graph, GenerationMode.VFX, new[] { target });
// Append the shader + strip the name header (VFX stamps one in later on).
stringBuilder.Append(text);
stringBuilder.Remove(0, text.IndexOf("{", StringComparison.Ordinal));
return stringBuilder;
}
private static void BuildBlock(VFXContextCompiledData contextData, List<VFXSlot> linkedEventOut, VFXShaderWriter blockFunction, VFXShaderWriter blockCallFunction, HashSet<string> blockDeclared, Dictionary<VFXExpression, string> expressionToName, VFXBlock block, ref int blockIndex)
{
var parameters = block.mergedAttributes.Select(o =>
{
return new VFXShaderWriter.FunctionParameter
{
name = o.attrib.name,
expression = new VFXAttributeExpression(o.attrib) as VFXExpression,
mode = o.mode
};
}).ToList();
foreach (var parameter in block.parameters)
{
var expReduced = contextData.gpuMapper.FromNameAndId(parameter.name, blockIndex);
if (VFXExpression.IsTypeValidOnGPU(expReduced.valueType))
{
parameters.Add(new VFXShaderWriter.FunctionParameter
{
name = parameter.name,
expression = expReduced,
mode = VFXAttributeMode.None
});
}
}
string methodName, commentMethod;
GetFunctionName(block, out methodName, out commentMethod);
if (!blockDeclared.Contains(methodName))
{
blockDeclared.Add(methodName);
blockFunction.WriteBlockFunction(contextData.gpuMapper,
methodName,
block.source,
parameters,
commentMethod);
}
//< Parameters (computed and/or extracted from uniform)
var expressionToNameLocal = expressionToName;
bool needScope = parameters.Any(o => !expressionToNameLocal.ContainsKey(o.expression));
if (needScope)
{
expressionToNameLocal = new Dictionary<VFXExpression, string>(expressionToNameLocal);
blockCallFunction.EnterScope();
foreach (var exp in parameters.Select(o => o.expression))
{
if (expressionToNameLocal.ContainsKey(exp))
{
continue;
}
blockCallFunction.WriteVariable(exp, expressionToNameLocal);
}
}
var indexEventCount = parameters.FindIndex(o => o.name == VFXAttribute.EventCount.name);
if (indexEventCount != -1)
{
if ((parameters[indexEventCount].mode & VFXAttributeMode.Read) != 0)
throw new InvalidOperationException(string.Format("{0} isn't expected as read (special case)", VFXAttribute.EventCount.name));
blockCallFunction.WriteLine(string.Format("{0} = 0u;", VFXAttribute.EventCount.GetNameInCode(VFXAttributeLocation.Current)));
}
blockCallFunction.WriteCallFunction(methodName,
parameters,
contextData.gpuMapper,
expressionToNameLocal);
if (indexEventCount != -1)
{
foreach (var outputSlot in block.outputSlots.SelectMany(o => o.LinkedSlots))
{
var eventIndex = linkedEventOut.IndexOf(outputSlot);
if (eventIndex != -1)
blockCallFunction.WriteLineFormat("{0}_{1} += {2};", VFXAttribute.EventCount.name, VFXCodeGeneratorHelper.GeneratePrefix((uint)eventIndex), VFXAttribute.EventCount.GetNameInCode(VFXAttributeLocation.Current));
}
}
if (needScope)
blockCallFunction.ExitScope();
blockIndex++;
}
}
}