@@ -79,7 +79,12 @@ module.exports = class WebGLFunctionNode extends FunctionNodeBase {
79
79
return retArr ;
80
80
}
81
81
82
- retArr . push ( this . returnType ) ;
82
+ const returnType = this . returnType ;
83
+ const type = typeMap [ returnType ] ;
84
+ if ( ! type ) {
85
+ throw new Error ( `unknown type ${ returnType } ` ) ;
86
+ }
87
+ retArr . push ( type ) ;
83
88
retArr . push ( ' ' ) ;
84
89
retArr . push ( this . functionName ) ;
85
90
retArr . push ( '(' ) ;
@@ -120,7 +125,12 @@ module.exports = class WebGLFunctionNode extends FunctionNodeBase {
120
125
retArr . push ( 'void' ) ;
121
126
this . kernalAst = ast ;
122
127
} else {
123
- retArr . push ( this . returnType ) ;
128
+ const returnType = this . returnType ;
129
+ const type = typeMap [ returnType ] ;
130
+ if ( ! type ) {
131
+ throw new Error ( `unknown type ${ returnType } ` ) ;
132
+ }
133
+ retArr . push ( type ) ;
124
134
}
125
135
retArr . push ( ' ' ) ;
126
136
retArr . push ( this . functionName ) ;
@@ -134,27 +144,12 @@ module.exports = class WebGLFunctionNode extends FunctionNodeBase {
134
144
if ( i > 0 ) {
135
145
retArr . push ( ', ' ) ;
136
146
}
137
- const type = this . getParamType ( paramName ) ;
138
- switch ( type ) {
139
- case 'TextureVec4' :
140
- case 'Texture' :
141
- case 'Input' :
142
- case 'Array' :
143
- retArr . push ( 'sampler2D' ) ;
144
- break ;
145
- case 'vec2' :
146
- retArr . push ( 'vec2' ) ;
147
- break ;
148
- case 'vec3' :
149
- retArr . push ( 'vec3' ) ;
150
- break ;
151
- case 'vec4' :
152
- retArr . push ( 'vec4' ) ;
153
- break ;
154
- default :
155
- retArr . push ( 'float' ) ;
147
+ const paramType = this . getParamType ( paramName ) ;
148
+ const type = typeMap [ paramType ] ;
149
+ if ( ! type ) {
150
+ throw new Error ( `unknown type ${ paramType } ` ) ;
156
151
}
157
-
152
+ retArr . push ( type ) ;
158
153
retArr . push ( ' ' ) ;
159
154
retArr . push ( 'user_' ) ;
160
155
retArr . push ( paramName ) ;
@@ -649,45 +644,35 @@ module.exports = class WebGLFunctionNode extends FunctionNodeBase {
649
644
}
650
645
const retDeclaration = [ ] ;
651
646
this . astGeneric ( declaration , retDeclaration ) ;
652
- let type = 'float ' ;
647
+ let returnType = 'Number ' ;
653
648
switch ( retDeclaration [ 2 ] ) {
654
649
case 'getImage2D(' :
655
650
case 'getImage3D(' :
656
- if ( i === 0 ) {
657
- retArr . push ( 'vec4 ' ) ;
658
- }
659
- type = 'vec4' ;
651
+ returnType = 'Array(4)' ;
660
652
break ;
661
653
default :
662
654
if ( i === 0 ) {
663
655
if ( declaration . init ) {
664
656
if ( declaration . init . name && this . declarations [ declaration . init . name ] ) {
665
- type = this . declarations [ declaration . init . name ] ;
666
- retArr . push ( type + ' ' ) ;
667
- } else if ( declaration . init . type ) {
668
- switch ( declaration . init . type ) {
669
- case 'ArrayExpression' :
670
- type = 'vec' + declaration . init . elements . length ;
671
- retArr . push ( type + ' ' ) ;
672
- break ;
673
- case 'CallExpression' :
674
- const node = this . builder . nodeMap [ declaration . init . callee . name ] ;
675
- if ( node && node . returnType ) {
676
- type = node . returnType ;
677
- retArr . push ( type + ' ' ) ;
678
- }
679
- break ;
680
- default :
681
- retArr . push ( 'float ' ) ;
657
+ returnType = this . declarations [ declaration . init . name ] ;
658
+ } else if ( declaration . init . type === 'ArrayExpression' ) {
659
+ returnType = `Array(${ declaration . init . elements . length } )` ;
660
+ } else if ( declaration . init . type === 'CallExpression' ) {
661
+ const node = this . builder . nodeMap [ declaration . init . callee . name ] ;
662
+ if ( node && node . returnType ) {
663
+ returnType = node . returnType ;
682
664
}
683
- } else {
684
- retArr . push ( 'float ' ) ;
685
665
}
686
666
}
687
667
}
688
668
break ;
689
669
}
690
- this . declarations [ declaration . id . name ] = type ;
670
+ const type = typeMap [ returnType ] ;
671
+ if ( ! type ) {
672
+ throw new Error ( `type ${ returnType } not handled` ) ;
673
+ }
674
+ retArr . push ( type + ' ' ) ;
675
+ this . declarations [ declaration . id . name ] = returnType ;
691
676
retArr . push . apply ( retArr , retDeclaration ) ;
692
677
}
693
678
retArr . push ( ';' ) ;
@@ -940,9 +925,9 @@ module.exports = class WebGLFunctionNode extends FunctionNodeBase {
940
925
variableType = this . getConstantType ( mNode . object . property . name ) ;
941
926
}
942
927
switch ( variableType ) {
943
- case 'vec2 ' :
944
- case 'vec3 ' :
945
- case 'vec4 ' :
928
+ case 'Array(2) ' :
929
+ case 'Array(3) ' :
930
+ case 'Array(4) ' :
946
931
// Get from local vec4
947
932
this . astGeneric ( mNode . object , retArr ) ;
948
933
retArr . push ( '[' ) ;
@@ -1194,7 +1179,7 @@ module.exports = class WebGLFunctionNode extends FunctionNodeBase {
1194
1179
} else {
1195
1180
functionArguments . push ( {
1196
1181
name : argument . name ,
1197
- type : this . paramTypes [ paramIndex ]
1182
+ type : this . paramTypes [ paramIndex ] || 'Number'
1198
1183
} ) ;
1199
1184
}
1200
1185
} else {
@@ -1273,6 +1258,18 @@ module.exports = class WebGLFunctionNode extends FunctionNodeBase {
1273
1258
}
1274
1259
} ;
1275
1260
1261
+ const typeMap = {
1262
+ 'TextureVec4' : 'sampler2D' ,
1263
+ 'Texture' : 'sampler2D' ,
1264
+ 'Input' : 'sampler2D' ,
1265
+ 'Array' : 'sampler2D' ,
1266
+ 'Array(2)' : 'vec2' ,
1267
+ 'Array(3)' : 'vec3' ,
1268
+ 'Array(4)' : 'vec4' ,
1269
+ 'Number' : 'float' ,
1270
+ 'Integer' : 'float'
1271
+ } ;
1272
+
1276
1273
function isIdentifierKernelParam ( paramName , ast , funcParam ) {
1277
1274
return funcParam . paramNames . indexOf ( paramName ) !== - 1 ;
1278
1275
}
0 commit comments