Skip to content

Commit d561fcf

Browse files
feat: Migrate to use type strings for arguments and returnType
1 parent 37c9a04 commit d561fcf

File tree

8 files changed

+196
-177
lines changed

8 files changed

+196
-177
lines changed

src/backend/function-node-base.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ module.exports = class BaseFunctionNode {
138138
if (paramTypes.hasOwnProperty(key)) {
139139
return paramTypes[key];
140140
} else {
141-
return 'float';
141+
return 'Number';
142142
}
143143
});
144144
}
@@ -151,7 +151,7 @@ module.exports = class BaseFunctionNode {
151151
// Return type handling
152152
//
153153
if (!this.returnType) {
154-
this.returnType = returnType || 'float';
154+
this.returnType = returnType || 'Number';
155155
}
156156
}
157157

@@ -352,7 +352,7 @@ module.exports = class BaseFunctionNode {
352352
if (this.declarations.hasOwnProperty(paramName)) {
353353
return this.declarations[paramName];
354354
} else {
355-
return null;
355+
return 'Number';
356356
}
357357
} else {
358358
if (!this.parent) {
@@ -368,7 +368,7 @@ module.exports = class BaseFunctionNode {
368368
}
369369
}
370370
}
371-
return null;
371+
return 'Number';
372372
}
373373

374374
getConstantType(constantName) {

src/backend/web-gl/function-node.js

Lines changed: 48 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,12 @@ module.exports = class WebGLFunctionNode extends FunctionNodeBase {
7979
return retArr;
8080
}
8181

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);
8388
retArr.push(' ');
8489
retArr.push(this.functionName);
8590
retArr.push('(');
@@ -120,7 +125,12 @@ module.exports = class WebGLFunctionNode extends FunctionNodeBase {
120125
retArr.push('void');
121126
this.kernalAst = ast;
122127
} 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);
124134
}
125135
retArr.push(' ');
126136
retArr.push(this.functionName);
@@ -134,27 +144,12 @@ module.exports = class WebGLFunctionNode extends FunctionNodeBase {
134144
if (i > 0) {
135145
retArr.push(', ');
136146
}
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 }`);
156151
}
157-
152+
retArr.push(type);
158153
retArr.push(' ');
159154
retArr.push('user_');
160155
retArr.push(paramName);
@@ -649,45 +644,35 @@ module.exports = class WebGLFunctionNode extends FunctionNodeBase {
649644
}
650645
const retDeclaration = [];
651646
this.astGeneric(declaration, retDeclaration);
652-
let type = 'float';
647+
let returnType = 'Number';
653648
switch (retDeclaration[2]) {
654649
case 'getImage2D(':
655650
case 'getImage3D(':
656-
if (i === 0) {
657-
retArr.push('vec4 ');
658-
}
659-
type = 'vec4';
651+
returnType = 'Array(4)';
660652
break;
661653
default:
662654
if (i === 0) {
663655
if (declaration.init) {
664656
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;
682664
}
683-
} else {
684-
retArr.push('float ');
685665
}
686666
}
687667
}
688668
break;
689669
}
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;
691676
retArr.push.apply(retArr, retDeclaration);
692677
}
693678
retArr.push(';');
@@ -940,9 +925,9 @@ module.exports = class WebGLFunctionNode extends FunctionNodeBase {
940925
variableType = this.getConstantType(mNode.object.property.name);
941926
}
942927
switch (variableType) {
943-
case 'vec2':
944-
case 'vec3':
945-
case 'vec4':
928+
case 'Array(2)':
929+
case 'Array(3)':
930+
case 'Array(4)':
946931
// Get from local vec4
947932
this.astGeneric(mNode.object, retArr);
948933
retArr.push('[');
@@ -1194,7 +1179,7 @@ module.exports = class WebGLFunctionNode extends FunctionNodeBase {
11941179
} else {
11951180
functionArguments.push({
11961181
name: argument.name,
1197-
type: this.paramTypes[paramIndex]
1182+
type: this.paramTypes[paramIndex] || 'Number'
11981183
});
11991184
}
12001185
} else {
@@ -1273,6 +1258,18 @@ module.exports = class WebGLFunctionNode extends FunctionNodeBase {
12731258
}
12741259
};
12751260

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+
12761273
function isIdentifierKernelParam(paramName, ast, funcParam) {
12771274
return funcParam.paramNames.indexOf(paramName) !== -1;
12781275
}

src/backend/web-gl2/function-node.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ module.exports = class WebGL2FunctionNode extends WebGLFunctionNode {
2525
console.log(this);
2626
}
2727
if (this.prototypeOnly) {
28-
return WebGL2FunctionNode.astFunctionPrototype(this.getJsAST(), [], this).join('').trim();
28+
return this.astFunctionPrototype(this.getJsAST(), [], this).join('').trim();
2929
} else {
3030
this.functionStringArray = this.astGeneric(this.getJsAST(), [], this);
3131
}
@@ -55,7 +55,11 @@ module.exports = class WebGL2FunctionNode extends WebGLFunctionNode {
5555
retArr.push('void');
5656
this.kernalAst = ast;
5757
} else {
58-
retArr.push(this.returnType);
58+
const type = typeMap[this.returnType];
59+
if (!type) {
60+
throw new Error(`Unknown type ${ this.returnType }`);
61+
}
62+
retArr.push(type);
5963
}
6064
retArr.push(' ');
6165
retArr.push(this.functionName);
@@ -166,6 +170,18 @@ module.exports = class WebGL2FunctionNode extends WebGLFunctionNode {
166170
}
167171
};
168172

173+
const typeMap = {
174+
'TextureVec4': 'sampler2D',
175+
'Texture': 'sampler2D',
176+
'Input': 'sampler2D',
177+
'Array': 'sampler2D',
178+
'Array(2)': 'vec2',
179+
'Array(3)': 'vec3',
180+
'Array(4)': 'vec4',
181+
'Number': 'float',
182+
'Integer': 'float'
183+
};
184+
169185
/**
170186
* @ignore
171187
* @function

test/all.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@
4747
<script src="features/float-output.js"></script>
4848
<script src="features/offscreen-canvas.js"></script>
4949
<script src="features/to-string.js"></script>
50+
<script src="features/type-management.js"></script>
5051

5152
<!-- internal -->
5253
<script src="internal/context-inheritance.js"></script>
53-
<script src="internal/deep-type-detection.js"></script>
5454
<script src="internal/function-builder.js"></script>
5555
<script src="internal/function-node.js"></script>
5656
<script src="internal/kernel-base.js"></script>

0 commit comments

Comments
 (0)