@@ -404,7 +404,7 @@ GLSL.prototype.transforms = {
404
404
var inArgs = [ ]
405
405
var outArgs = [ ]
406
406
args . components . forEach ( function ( arg , index ) {
407
- arg . index = index ;
407
+ arg . index = index
408
408
if ( arg . qualifier . slice ( 0 , 2 ) === 'in' ) {
409
409
inArgs . push ( arg )
410
410
}
@@ -413,7 +413,7 @@ GLSL.prototype.transforms = {
413
413
}
414
414
} )
415
415
if ( outArgs . length === 0 ) {
416
- outArgs = null ;
416
+ outArgs = null
417
417
}
418
418
419
419
//if main name is registered - provide type-scoped name of function
@@ -427,20 +427,24 @@ GLSL.prototype.transforms = {
427
427
//create function body
428
428
result += `function ${ name } (${ args } ) {\n`
429
429
430
- //input parameters are copied
430
+ //guard input parameters from being mutated
431
431
inArgs . forEach ( function ( arg ) {
432
432
if ( / ^ ( v e c | m a t ) / . test ( arg . type ) ) {
433
433
result += `${ arg } = ${ arg } .slice();\n`
434
434
}
435
435
} )
436
436
437
- var scope = this . scopes [ this . currentScope ] ;
438
- scope . callName = name ;
439
- scope . outArgs = outArgs ;
437
+ //populate current scope information
438
+ //this is used by the `return` transform
439
+ var scope = this . scopes [ this . currentScope ]
440
+ scope . callName = name
441
+ scope . outArgs = outArgs
440
442
441
443
result += this . process ( node . children [ 2 ] )
442
444
443
445
if ( outArgs && outType === 'void' ) {
446
+ //the output list is usually created when transforming the `return` statement
447
+ //but this function does not have a `return` at the end
444
448
result += `\n${ name } .__out__ = [${ outArgs . join ( ', ' ) } ];`
445
449
}
446
450
@@ -456,7 +460,9 @@ GLSL.prototype.transforms = {
456
460
complexity : 999
457
461
} )
458
462
459
- result . outArgs = outArgs ;
463
+ //save the output arguments list
464
+ //this is used by the `call` transform
465
+ result . outArgs = outArgs
460
466
461
467
//register function descriptor
462
468
this . functions [ name ] = result
@@ -516,6 +522,7 @@ GLSL.prototype.transforms = {
516
522
if ( node . parent . type === 'functionargs' ) {
517
523
result = this . process ( decllist )
518
524
525
+ // in/out/inout
519
526
var qualifier = node . token . data
520
527
if ( qualifier === result . type ) {
521
528
result . qualifier = 'in'
@@ -766,18 +773,24 @@ GLSL.prototype.transforms = {
766
773
767
774
return : function ( node ) {
768
775
var expr = this . process ( node . children [ 0 ] )
769
- var result ;
770
- var scope = this . scopes [ this . currentScope ] ;
776
+ var result
777
+ var scope = this . scopes [ this . currentScope ]
771
778
if ( scope . outArgs ) {
772
779
var outStmt = `${ scope . callName } .__out__ = [${ scope . outArgs . join ( ', ' ) } ]`
773
780
774
781
if ( expr . visible ) {
782
+ // func.__return__ = <expression>;
783
+ // func.__out__ = [outArg1, outArg2, ...];
784
+ // return func.__return__;
775
785
result = `${ scope . callName } .__return__ = ${ expr } ;\n${ outStmt } ;\nreturn ${ scope . callName } .__return__`
776
786
} else {
787
+ // func.__out__ = [outArg1, outArg2, ...];
788
+ // return;
777
789
result = `${ outStmt } ;\nreturn`
778
790
}
779
791
} else {
780
- result = 'return' + ( expr . visible ? ' ' + expr : '' ) ;
792
+ // return <expression>;
793
+ result = 'return' + ( expr . visible ? ' ' + expr : '' )
781
794
}
782
795
return Descriptor ( result , { type : expr . type } )
783
796
} ,
@@ -1165,11 +1178,13 @@ GLSL.prototype.transforms = {
1165
1178
optimize = false
1166
1179
}
1167
1180
1168
- var res = `${ callName } (${ argValues . join ( ', ' ) } )` ;
1181
+ var res = `${ callName } (${ argValues . join ( ', ' ) } )`
1169
1182
if ( outArgs ) {
1183
+ // calling func(in a, out b, out c):
1184
+ // (func(a, b, c), [b, c] = func.__out__, func.__return__)
1170
1185
var outList = outArgs . map ( function ( arg ) {
1171
1186
return argValues [ arg . index ]
1172
- } ) ;
1187
+ } )
1173
1188
res = `(${ res } , [${ outList . join ( ', ' ) } ] = ${ callName } .__out__, ${ callName } .__return__)`
1174
1189
}
1175
1190
0 commit comments