@@ -400,6 +400,22 @@ GLSL.prototype.transforms = {
400
400
return `${ arg . type } `
401
401
} ) . join ( '_' )
402
402
403
+ //sort arguments by qualifier
404
+ var inArgs = [ ]
405
+ var outArgs = [ ]
406
+ args . components . forEach ( function ( arg , index ) {
407
+ arg . index = index ;
408
+ if ( arg . qualifier . slice ( 0 , 2 ) === 'in' ) {
409
+ inArgs . push ( arg )
410
+ }
411
+ if ( arg . qualifier . slice ( - 3 ) === 'out' ) {
412
+ outArgs . push ( arg )
413
+ }
414
+ } )
415
+ if ( outArgs . length === 0 ) {
416
+ outArgs = null ;
417
+ }
418
+
403
419
//if main name is registered - provide type-scoped name of function
404
420
if ( this . functions [ name ] && argTypesSfx ) {
405
421
name = `${ name } _${ argTypesSfx } `
@@ -410,7 +426,24 @@ GLSL.prototype.transforms = {
410
426
411
427
//create function body
412
428
result += `function ${ name } (${ args } ) {\n`
429
+
430
+ //input parameters are copied
431
+ inArgs . forEach ( function ( arg ) {
432
+ if ( / ^ ( v e c | m a t ) / . test ( arg . type ) ) {
433
+ result += `${ arg } = ${ arg } .slice();\n`
434
+ }
435
+ } )
436
+
437
+ var scope = this . scopes [ this . currentScope ] ;
438
+ scope . callName = name ;
439
+ scope . outArgs = outArgs ;
440
+
413
441
result += this . process ( node . children [ 2 ] )
442
+
443
+ if ( outArgs && outType === 'void' ) {
444
+ result += `\n${ name } .__out__ = [${ outArgs . join ( ', ' ) } ];`
445
+ }
446
+
414
447
result = result . replace ( / \n / g, '\n\t' )
415
448
result += '\n}'
416
449
@@ -423,6 +456,8 @@ GLSL.prototype.transforms = {
423
456
complexity : 999
424
457
} )
425
458
459
+ result . outArgs = outArgs ;
460
+
426
461
//register function descriptor
427
462
this . functions [ name ] = result
428
463
@@ -480,6 +515,14 @@ GLSL.prototype.transforms = {
480
515
//case of function args - drop var
481
516
if ( node . parent . type === 'functionargs' ) {
482
517
result = this . process ( decllist )
518
+
519
+ var qualifier = node . token . data
520
+ if ( qualifier === result . type ) {
521
+ result . qualifier = 'in'
522
+ } else {
523
+ result . qualifier = qualifier
524
+ }
525
+
483
526
return result
484
527
}
485
528
//default type, like variable decl etc
@@ -723,7 +766,20 @@ GLSL.prototype.transforms = {
723
766
724
767
return : function ( node ) {
725
768
var expr = this . process ( node . children [ 0 ] )
726
- return Descriptor ( 'return' + ( expr . visible ? ' ' + expr : '' ) , { type : expr . type } )
769
+ var result ;
770
+ var scope = this . scopes [ this . currentScope ] ;
771
+ if ( scope . outArgs ) {
772
+ var outStmt = `${ scope . callName } .__out__ = [${ scope . outArgs . join ( ', ' ) } ]`
773
+
774
+ if ( expr . visible ) {
775
+ result = `${ scope . callName } .__return__ = ${ expr } ;\n${ outStmt } ;\nreturn ${ scope . callName } .__return__`
776
+ } else {
777
+ result = `${ outStmt } ;\nreturn`
778
+ }
779
+ } else {
780
+ result = 'return' + ( expr . visible ? ' ' + expr : '' ) ;
781
+ }
782
+ return Descriptor ( result , { type : expr . type } )
727
783
} ,
728
784
729
785
continue : function ( ) { return Descriptor ( 'continue' ) } ,
@@ -1064,19 +1120,23 @@ GLSL.prototype.transforms = {
1064
1120
1065
1121
//someFn()
1066
1122
else {
1067
- var type , optimize = true
1123
+ var type , optimize = true , outArgs = null
1068
1124
1069
1125
//registered fn()
1070
- if ( this . functions [ callName ] ) {
1126
+ var fn = this . functions [ callName ]
1127
+ if ( fn ) {
1071
1128
var sfx = argTypes . join ( '_' )
1072
1129
if ( sfx && this . functions [ `${ callName } _${ sfx } ` ] ) {
1073
- type = this . functions [ `${ callName } _${ sfx } ` ] . type
1130
+ fn = this . functions [ `${ callName } _${ sfx } ` ]
1131
+ type = fn . type
1132
+ outArgs = fn . outArgs
1074
1133
callName = Descriptor ( `${ callName } _${ sfx } ` , {
1075
1134
complexity : callName . complexity
1076
1135
} )
1077
1136
}
1078
- else if ( this . functions [ callName ] ) {
1079
- type = this . functions [ callName ] . type
1137
+ else {
1138
+ type = fn . type
1139
+ outArgs = fn . outArgs
1080
1140
}
1081
1141
}
1082
1142
@@ -1104,15 +1164,23 @@ GLSL.prototype.transforms = {
1104
1164
type = null
1105
1165
optimize = false
1106
1166
}
1107
- var res = Descriptor ( `${ callName } (${ argValues . join ( ', ' ) } )` , {
1167
+
1168
+ var res = `${ callName } (${ argValues . join ( ', ' ) } )` ;
1169
+ if ( outArgs ) {
1170
+ var outList = outArgs . map ( function ( arg ) {
1171
+ return argValues [ arg . index ]
1172
+ } ) ;
1173
+ res = `(${ res } , [${ outList . join ( ', ' ) } ] = ${ callName } .__out__, ${ callName } .__return__)`
1174
+ }
1175
+
1176
+ return Descriptor ( res , {
1108
1177
type : type || callName . type ,
1109
1178
complexity : 999 /* argValues.reduce(function (prev, curr) {
1110
1179
return curr.complexity+prev
1111
1180
}, callName.complexity||999) */ ,
1112
1181
optimize : optimize
1113
1182
} )
1114
1183
1115
- return res
1116
1184
}
1117
1185
}
1118
1186
} ,
0 commit comments