@@ -401,12 +401,19 @@ internal static Dictionary<VFXExpression, string> BuildExpressionToName(VFXConte
401
401
402
402
internal static void BuildContextBlocks ( VFXContext context , VFXTaskCompiledData taskData , Dictionary < VFXExpression , string > expressionToName ,
403
403
out VFXShaderWriter blockFunction ,
404
- out VFXShaderWriter blockCallFunction )
404
+ out VFXShaderWriter blockCallFunction ,
405
+ out VFXShaderWriter blockIncludes ,
406
+ out VFXShaderWriter blockDefines )
405
407
{
406
408
//< Block processor
407
409
blockFunction = new VFXShaderWriter ( ) ;
408
410
blockCallFunction = new VFXShaderWriter ( ) ;
411
+ blockIncludes = new VFXShaderWriter ( ) ;
412
+ blockDefines = new VFXShaderWriter ( ) ;
413
+
409
414
var blockDeclared = new HashSet < string > ( ) ;
415
+ var includesProcessed = new HashSet < string > ( ) ;
416
+ var defineProcessed = new HashSet < string > ( ) ;
410
417
411
418
int cpt = 0 ;
412
419
foreach ( var current in context . activeFlattenedChildrenWithImplicit )
@@ -415,14 +422,42 @@ internal static void BuildContextBlocks(VFXContext context, VFXTaskCompiledData
415
422
if ( current is IHLSLCodeHolder hlslCodeHolder )
416
423
{
417
424
blockFunction . Write ( hlslCodeHolder . customCode ) ;
425
+ foreach ( var includePath in hlslCodeHolder . includes )
426
+ {
427
+ if ( includesProcessed . Add ( includePath ) )
428
+ {
429
+ blockIncludes . WriteLine ( $ "#include \" { includePath } \" ") ;
430
+ }
431
+ }
432
+ }
433
+
434
+ foreach ( var define in current . defines )
435
+ {
436
+ if ( defineProcessed . Add ( define ) )
437
+ {
438
+ blockDefines . WriteLineFormat ( "#define {0}{1}" , define , define . Contains ( ' ' ) ? "" : " 1" ) ;
439
+ }
418
440
}
419
441
BuildBlock ( taskData , blockFunction , blockCallFunction , blockDeclared , expressionToName , current , ref cpt ) ;
420
442
}
421
443
422
444
// Custom HLSL Operators
423
- foreach ( var group in taskData . hlslCodeHolders . GroupBy ( x => x . customCode . GetHashCode ( ) ) )
445
+ var customCodeProcessed = new HashSet < string > ( ) ;
446
+ foreach ( var hlslCodeHolder in taskData . hlslCodeHolders )
424
447
{
425
- blockFunction . Write ( group . First ( ) . customCode ) ;
448
+ var customCode = hlslCodeHolder . customCode ;
449
+ if ( customCodeProcessed . Add ( customCode ) )
450
+ {
451
+ blockFunction . Write ( customCode ) ;
452
+ }
453
+
454
+ foreach ( var includePath in hlslCodeHolder . includes )
455
+ {
456
+ if ( includesProcessed . Add ( includePath ) )
457
+ {
458
+ blockIncludes . WriteLine ( $ "#include \" { includePath } \" ") ;
459
+ }
460
+ }
426
461
}
427
462
}
428
463
@@ -681,7 +716,7 @@ static private StringBuilder Build(
681
716
globalDeclaration . WriteEventBuffers ( eventListOutName , taskData . linkedEventOut . Length ) ;
682
717
683
718
var expressionToName = BuildExpressionToName ( context , taskData ) ;
684
- BuildContextBlocks ( context , taskData , expressionToName , out var blockFunction , out var blockCallFunction ) ;
719
+ BuildContextBlocks ( context , taskData , expressionToName , out var blockFunction , out var blockCallFunction , out var blockIncludes , out var blockDefines ) ;
685
720
686
721
//< Final composition
687
722
var globalIncludeContent = new VFXShaderWriter ( ) ;
@@ -743,25 +778,8 @@ static private StringBuilder Build(
743
778
{
744
779
perPassIncludeContent . WriteLine ( "#include \" Packages/com.unity.visualeffectgraph/Shaders/VFXCommonOutput.hlsl\" " ) ;
745
780
}
746
-
747
- // Per-block defines
748
- var defines = Enumerable . Empty < string > ( ) ;
749
- foreach ( var block in context . activeFlattenedChildrenWithImplicit )
750
- defines = defines . Concat ( block . defines ) ;
751
- var uniqueDefines = new HashSet < string > ( defines ) ;
752
- foreach ( var define in uniqueDefines )
753
- globalIncludeContent . WriteLineFormat ( "#define {0}{1}" , define , define . Contains ( ' ' ) ? "" : " 1" ) ;
754
-
755
- // Per-block includes
756
- var includes = Enumerable . Empty < string > ( ) ;
757
- foreach ( var block in context . activeFlattenedChildrenWithImplicit . OfType < IHLSLCodeHolder > ( ) )
758
- includes = includes . Concat ( block . includes ) ;
759
- foreach ( var hlslHolder in taskData . hlslCodeHolders )
760
- includes = includes . Concat ( hlslHolder . includes ) ;
761
- var uniqueIncludes = new HashSet < string > ( includes ) ;
762
- foreach ( var includePath in uniqueIncludes )
763
- perPassIncludeContent . WriteLine ( string . Format ( "#include \" {0}\" " , includePath ) ) ;
764
-
781
+ globalIncludeContent . Write ( blockDefines . builder . ToString ( ) ) ;
782
+ perPassIncludeContent . Write ( blockIncludes . builder . ToString ( ) ) ;
765
783
766
784
ReplaceMultiline ( stringBuilder , "${VFXGlobalInclude}" , globalIncludeContent . builder ) ;
767
785
ReplaceMultiline ( stringBuilder , "${VFXGlobalDeclaration}" , globalDeclaration . builder ) ;
0 commit comments