Skip to content

Commit 37c9a04

Browse files
fix: Lookup types from function nodes and use those when transpiling
1 parent 4f26639 commit 37c9a04

File tree

2 files changed

+90
-8
lines changed

2 files changed

+90
-8
lines changed

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

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -660,14 +660,29 @@ module.exports = class WebGLFunctionNode extends FunctionNodeBase {
660660
break;
661661
default:
662662
if (i === 0) {
663-
if (declaration.init && declaration.init.name && this.declarations[declaration.init.name]) {
664-
type = this.declarations[declaration.init.name];
665-
retArr.push(type + ' ');
666-
} else if (declaration.init && declaration.init.type && declaration.init.type === 'ArrayExpression') {
667-
type = 'vec' + declaration.init.elements.length;
668-
retArr.push(type + ' ');
669-
} else {
670-
retArr.push('float ');
663+
if (declaration.init) {
664+
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 ');
682+
}
683+
} else {
684+
retArr.push('float ');
685+
}
671686
}
672687
}
673688
break;

test/features/add-typed-functions.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,76 @@
1414
.setOutput([1]);
1515
var result = kernel();
1616
QUnit.assert.equal(result[0], 3);
17+
gpu.destroy();
1718
}
1819

1920
QUnit.test( 'add typed functions - vec2 - (auto)', function() {
2021
vec2Test(null);
2122
});
23+
QUnit.test( 'add typed functions - vec2 - (webgl2)', function() {
24+
vec2Test('webgl2');
25+
});
26+
QUnit.test( 'add typed functions - vec2 - (webgl)', function() {
27+
vec2Test('webgl');
28+
});
29+
})();
30+
31+
(function() {
32+
function vec3Test(mode) {
33+
var gpu = new GPU({ mode: mode });
34+
function typedFunction() {
35+
return [1, 2, 3];
36+
}
37+
gpu.addFunction(typedFunction, {
38+
returnType: 'vec3'
39+
});
40+
var kernel = gpu.createKernel(function() {
41+
var result = typedFunction();
42+
return result[0] + result[1] + result[2];
43+
})
44+
.setOutput([1]);
45+
var result = kernel();
46+
QUnit.assert.equal(result[0], 6);
47+
gpu.destroy();
48+
}
49+
50+
QUnit.test( 'add typed functions - vec3 - (auto)', function() {
51+
vec3Test(null);
52+
});
53+
QUnit.test( 'add typed functions - vec3 - (webgl2)', function() {
54+
vec3Test('webgl2');
55+
});
56+
QUnit.test( 'add typed functions - vec3 - (webgl)', function() {
57+
vec3Test('webgl');
58+
});
59+
})();
60+
61+
(function() {
62+
function vec4Test(mode) {
63+
var gpu = new GPU({ mode: mode });
64+
function typedFunction() {
65+
return [1, 2, 3, 4];
66+
}
67+
gpu.addFunction(typedFunction, {
68+
returnType: 'vec4'
69+
});
70+
var kernel = gpu.createKernel(function() {
71+
var result = typedFunction();
72+
return result[0] + result[1] + result[2] + result[3];
73+
})
74+
.setOutput([1]);
75+
var result = kernel();
76+
QUnit.assert.equal(result[0], 10);
77+
gpu.destroy();
78+
}
79+
80+
QUnit.test( 'add typed functions - vec4 - (auto)', function() {
81+
vec4Test(null);
82+
});
83+
QUnit.test( 'add typed functions - vec4 - (webgl2)', function() {
84+
vec4Test('webgl2');
85+
});
86+
QUnit.test( 'add typed functions - vec4 - (webgl)', function() {
87+
vec4Test('webgl');
88+
});
2289
})();

0 commit comments

Comments
 (0)