@@ -297,8 +297,9 @@ function shadergenerator(p5, fn) {
297
297
this . useTemp = true ;
298
298
}
299
299
300
- assertUsedInConditional ( ) {
300
+ assertUsedInConditional ( branch ) {
301
301
this . usedInConditional = true ;
302
+ this . usedIn . push ( branch ) ;
302
303
this . forceTemporaryVariable ( ) ;
303
304
}
304
305
@@ -311,7 +312,7 @@ function shadergenerator(p5, fn) {
311
312
const isUsedSatisfied = ( ) => statement . usedInSatisfied . length >= 1 ;
312
313
const isDepsSatisfied = ( ) => statement . dependsOn . length === statement . dependsOnSatisfied . length ;
313
314
if ( statement . insertionPoint > - 1 || ! statement . usedIn . length ) return ;
314
- if ( statement . dependsOn . includes ( this ) && ! statement . dependsOnSatisfied . includes ( this ) ) {
315
+ if ( statement . dependsOn . some ( d => d . node === this ) && ! statement . dependsOnSatisfied . includes ( this ) ) {
315
316
statement . dependsOnSatisfied . push ( this ) ;
316
317
}
317
318
if ( statement . usedIn . includes ( this ) && ! statement . usedInSatisfied . includes ( this ) ) {
@@ -330,22 +331,17 @@ function shadergenerator(p5, fn) {
330
331
let oldLength = context . declarations . length ;
331
332
result = this . getTemporaryVariable ( context ) ;
332
333
let diff = context . declarations . length - 1 - oldLength ;
333
- if ( Array . isArray ( this . dependsOn ) ) {
334
- this . dependsOn . forEach ( d => {
335
- context . updateComponents ( d , diff > 0 ? diff : undefined ) ;
336
- } ) ;
337
- } else {
338
- // Update the incorrect components
339
- this . dependsOn . nodesArray . forEach ( ( nodeDependency , i ) => {
340
- const originalComponents = this . dependsOn [ i ] ;
341
- const currentComponents = nodeDependency . componentNames . map ( name => nodeDependency [ name ] ) ;
342
- if ( ! originalComponents ) return ;
343
- const dependencies = originalComponents . map ( ( component , i ) =>
344
- component === currentComponents [ i ]
345
- )
346
- context . updateComponents ( nodeDependency , diff > 0 ? diff : undefined , dependencies ) ;
347
- } )
348
- }
334
+ diff = diff > 0 ? diff : undefined ;
335
+ this . dependsOn . forEach ( dependency => {
336
+ if ( dependency . isVector ) {
337
+ const dependencies = dependency . originalComponents . map ( ( component , i ) =>
338
+ component === dependency . currentComponents [ i ]
339
+ ) ;
340
+ context . updateComponents ( dependency . node , diff , dependencies ) ;
341
+ } else {
342
+ context . updateComponents ( dependency . node , diff ) ;
343
+ }
344
+ } ) ;
349
345
} else {
350
346
result = this . toGLSL ( context ) ;
351
347
}
@@ -600,18 +596,6 @@ function shadergenerator(p5, fn) {
600
596
601
597
super ( isInternal , functionSignature . returnType ) ;
602
598
603
- if ( userArgs . find ( arg => isVectorNode ( arg ) ) ) {
604
- this . dependsOn = { nodesArray : [ ] } ;
605
- }
606
- userArgs . forEach ( ( arg , i ) => {
607
- arg . usedIn . push ( this ) ;
608
- let arr = Array . isArray ( this . dependsOn ) ? this . dependsOn : this . dependsOn . nodesArray ;
609
- if ( isVectorType ( arg ) ) {
610
- this . dependsOn [ i ] = [ ] ;
611
- arg . componentNames . forEach ( name => this . dependsOn [ i ] . push ( arg [ name ] ) ) ;
612
- }
613
- arr . push ( arg ) ;
614
- } ) ;
615
599
this . name = name ;
616
600
this . args = userArgs ;
617
601
this . argumentTypes = functionSignature . args ;
@@ -873,7 +857,7 @@ function shadergenerator(p5, fn) {
873
857
874
858
toGLSL ( context ) {
875
859
const oldLength = context . declarations . length ;
876
- this . dependsOn . forEach ( dep => context . updateComponents ( dep ) ) ;
860
+ this . dependsOn . forEach ( dep => context . updateComponents ( dep . node ) ) ;
877
861
const newLength = context . declarations . length ;
878
862
const diff = newLength - oldLength ;
879
863
this . insertionPoint += diff ;
@@ -939,14 +923,14 @@ function shadergenerator(p5, fn) {
939
923
}
940
924
node = node . parent ? node . parent : node ;
941
925
value = value . parent ? value . parent : value ;
942
- if ( [ node , value ] . some ( n => this . dependsOn . includes ( n ) ) ) {
926
+ if ( [ node , value ] . some ( n => this . dependsOn . some ( d => d . node === n ) ) ) {
943
927
return ;
944
928
}
945
- node . assertUsedInConditional ( ) ;
946
- this . dependsOn . push ( node )
929
+ node . assertUsedInConditional ( this ) ;
930
+ this . dependsOn . push ( makeDependencyObject ( node ) )
947
931
if ( value . shouldUseTemporaryVariable ( ) ) {
948
- value . assertUsedInConditional ( ) ;
949
- this . dependsOn . push ( value ) ;
932
+ value . assertUsedInConditional ( this ) ;
933
+ this . dependsOn . push ( makeDependencyObject ( value ) ) ;
950
934
}
951
935
}
952
936
@@ -1071,7 +1055,7 @@ function shadergenerator(p5, fn) {
1071
1055
}
1072
1056
1073
1057
function isConditionalNode ( node ) {
1074
- return ( node instanceof ConditionalNode )
1058
+ return ( node instanceof ConditionalNode || node instanceof BranchNode )
1075
1059
}
1076
1060
1077
1061
function hasTemporaryVariable ( node ) {
@@ -1302,6 +1286,28 @@ function shadergenerator(p5, fn) {
1302
1286
}
1303
1287
1304
1288
// User function helpers
1289
+ function makeDependencyObject ( dep ) {
1290
+ if ( isVectorType ( dep ) ) {
1291
+ return {
1292
+ node : dep ,
1293
+ isVector : true ,
1294
+ originalComponents : [ ...dep . componentNames . map ( name => dep [ name ] ) ] ,
1295
+ get currentComponents ( ) {
1296
+ return dep . componentNames . map ( name => dep [ name ] ) ;
1297
+ }
1298
+ } ;
1299
+ } else {
1300
+ return {
1301
+ node : dep ,
1302
+ isVector : false
1303
+ } ;
1304
+ }
1305
+ }
1306
+
1307
+ function makeDependencyArray ( dependencies ) {
1308
+ return dependencies . map ( dep => makeDependencyObject ( dep ) ) ;
1309
+ }
1310
+
1305
1311
function conformVectorParameters ( value , vectorDimensions ) {
1306
1312
// Allow arguments as arrays or otherwise. The following are all equivalent:
1307
1313
// ([0,0,0,0]) (0,0,0,0) (0) ([0])
@@ -1423,11 +1429,19 @@ function shadergenerator(p5, fn) {
1423
1429
function fnNodeConstructor ( name , userArgs , properties , isInternal ) {
1424
1430
let node = new FunctionCallNode ( name , userArgs , properties , isInternal ) ;
1425
1431
node = dynamicAddSwizzleTrap ( node ) ;
1426
- if ( node . args . some ( arg => arg . isUsedInConditional ( ) ) ) {
1427
- GLOBAL_SHADER . context . ifs . forEach ( statement => {
1428
- statement . usedIn . push ( node ) ;
1432
+ node . dependsOn = makeDependencyArray ( node . args ) ;
1433
+ const dependsOnConditionals = node . args . map ( arg => {
1434
+ const conditionals = arg . usedIn . filter ( n => isConditionalNode ( n ) ) . map ( c => {
1435
+ if ( c instanceof BranchNode ) {
1436
+ return c . parent ;
1437
+ } else {
1438
+ return c ;
1439
+ }
1429
1440
} ) ;
1430
- }
1441
+ return conditionals ;
1442
+ } ) . flat ( ) ;
1443
+ dependsOnConditionals . forEach ( conditional => conditional . usedIn . push ( node ) ) ;
1444
+
1431
1445
return node ;
1432
1446
}
1433
1447
0 commit comments