Skip to content

Commit 630bd94

Browse files
feat: Completely new type checking system, and refined state management
1 parent 82e4966 commit 630bd94

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1654
-740
lines changed

examples/random.html

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<html>
2+
<body>
3+
<h2>CPU Random</h2>
4+
<canvas id="cpu-random-output"></canvas>
5+
<h2>GPU Random</h2>
6+
<canvas id="gpu-random-output"></canvas>
7+
</body>
8+
<script src="../bin/gpu-browser.js"></script>
9+
<script>
10+
function drawRandomFunction() {
11+
this.color(Math.random(), Math.random(), Math.random(), 1);
12+
}
13+
14+
const cpu = new GPU({ mode: 'cpu', canvas: document.getElementById('cpu-random-output') });
15+
const gpu = new GPU({ mode: 'gpu', canvas: document.getElementById('gpu-random-output') });
16+
17+
const cpuDrawRandom = cpu.createKernel(drawRandomFunction)
18+
.setGraphical(true)
19+
.setOutput([100, 100]);
20+
21+
const gpuDrawRandom = gpu.createKernel(drawRandomFunction)
22+
.setGraphical(true)
23+
.setOutput([100, 100]);
24+
25+
setInterval(() => {
26+
cpuDrawRandom();
27+
gpuDrawRandom();
28+
}, 10);
29+
</script>
30+
</html>

examples/raytracer.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<meta http-equiv="X-UA-Compatible" content="IE=edge">
77
<title>GPU.js Raytracer</title>
88
<meta name="author" content="Stacey Tay">
9-
<meta name="description" content="A simple raytracer built with CPU.js.">
9+
<meta name="description" content="A simple raytracer built with GPU.js.">
1010
<link href='https://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'>
1111
<style>
1212
html, body {

examples/simple-typescript.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {GPU, KernelFunction, IKernelRunShortcut} from "../src";
1+
import { GPU, KernelFunction, IKernelRunShortcut } from "../src";
22

33
const gpu = new GPU({ mode: 'gpu' });
44

@@ -8,10 +8,9 @@ const kernelFunction: KernelFunction = function(anInt: number, anArray: number[]
88
return x;
99
};
1010

11-
const kernel: IKernel = gpu.createKernel(kernelFunction, {
12-
output: [1]
13-
});
11+
const kernel: IKernelRunShortcut = gpu.createKernel(kernelFunction)
12+
.setOutput([1]);
1413

15-
const result = kernel(4, [5], [[6]], [[[7]]]);
14+
const result = kernel(1, [.25], [[1.5]]);
1615

17-
console.log(result[0]);
16+
console.log(result[0]); // 3

src/backend/cpu/function-node.js

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,8 @@ const {
66
* @desc [INTERNAL] Represents a single function, inside JS
77
*
88
* <p>This handles all the raw state, converted state, etc. Of a single function.</p>
9-
*
10-
* @prop jsFunction - {Function} The JS Function the node represents
11-
* @prop source - {String} jsFunction.toString()
12-
* @prop argumentNames - {String[]} Parameter names of the function
13-
* @prop argumentTypes - {String[]} Shader land parameters type assumption
14-
* @prop isRootKernel - {Boolean} Special indicator, for kernel function
15-
* @prop calledFunctions - {String[]} List of all the functions called
16-
* @prop initVariables - {String[]} List of variables initialized in the function
17-
* @prop readVariables - {String[]} List of variables read operations occur
18-
* @prop writeVariables - {String[]} List of variables write operations occur
19-
*
209
*/
2110
class CPUFunctionNode extends FunctionNode {
22-
23-
/**
24-
*
25-
* @param {string} fn
26-
* @param {object} settings
27-
*/
2811
constructor(fn, settings) {
2912
settings = settings || {};
3013
super(fn, settings);

src/backend/function-builder.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,13 @@ class FunctionBuilder {
6060

6161
let functionNodes = null;
6262
if (kernel.functions) {
63-
functionNodes = kernel.functions.map(fn => new FunctionNode(fn.source, {
63+
functionNodes = kernel.functions.map((fn) => new FunctionNode(fn.source, {
6464
returnType: fn.returnType,
6565
argumentTypes: fn.argumentTypes,
6666
output: kernel.output,
67-
plugins
67+
plugins,
68+
constants,
69+
constantTypes,
6870
}));
6971
}
7072

@@ -303,4 +305,4 @@ class FunctionBuilder {
303305

304306
module.exports = {
305307
FunctionBuilder
306-
};
308+
};

src/backend/function-node.js

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ const acorn = require('acorn');
1111
class FunctionNode {
1212
/**
1313
*
14-
* @param {string} source
15-
* @param {IFunctionSettings} settings
14+
* @param {string|object} source
15+
* @param {IFunctionSettings} [settings]
1616
*/
1717
constructor(source, settings) {
1818
if (!source) {
@@ -38,18 +38,17 @@ class FunctionNode {
3838
this.onNestedFunction = null;
3939
this.loopMaxIterations = null;
4040
this.argumentNames = (typeof this.source === 'string' ? utils.getArgumentNamesFromString(this.source) : null);
41-
this.argumentTypes = {};
41+
this.argumentTypes = [];
4242
this.argumentSizes = [];
4343
this.returnType = null;
4444
this.output = [];
4545
this.plugins = null;
4646

4747
if (settings) {
48-
for (const p in this) {
48+
for (const p in settings) {
49+
if (!settings.hasOwnProperty(p)) continue;
4950
if (!this.hasOwnProperty(p)) continue;
50-
if (settings[p]) {
51-
this[p] = settings[p];
52-
}
51+
this[p] = settings[p];
5352
}
5453
}
5554

@@ -74,12 +73,7 @@ class FunctionNode {
7473
}
7574

7675
if (this.argumentTypes.length > 0 && this.argumentTypes.length !== this.argumentNames.length) {
77-
throw new Error(
78-
'Invalid argument type array length, against function length -> (' +
79-
this.argumentTypes.length + ',' +
80-
this.argumentNames.length +
81-
')'
82-
);
76+
throw new Error(`argumentTypes count of ${ this.argumentTypes.length } exceeds ${ this.argumentNames.length }`);
8377
}
8478

8579
if (this.output.length < 1) {
@@ -198,37 +192,43 @@ class FunctionNode {
198192

199193
/**
200194
* @desc Return the type of parameter sent to subKernel/Kernel.
201-
* @param {String} argumentName - Name of the parameter
195+
* @param {String} name - Name of the parameter
202196
* @returns {String} Type of the parameter
203197
*/
204-
getVariableType(argumentName) {
205-
const argumentIndex = this.argumentNames.indexOf(argumentName);
198+
getVariableType(name) {
199+
let type = null;
200+
const argumentIndex = this.argumentNames.indexOf(name);
206201
if (argumentIndex === -1) {
207-
if (this.declarations.hasOwnProperty(argumentName)) {
208-
return this.declarations[argumentName];
209-
} else {
210-
return 'Number';
202+
if (this.declarations[name]) {
203+
return this.declarations[name];
211204
}
212205
} else {
213-
if (!this.parent) {
214-
if (this.argumentTypes[argumentIndex]) return this.argumentTypes[argumentIndex];
215-
} else {
216-
if (this.argumentTypes[argumentIndex]) return this.argumentTypes[argumentIndex];
206+
const argumentType = this.argumentTypes[argumentIndex];
207+
if (argumentType) {
208+
type = argumentType;
209+
} else if (this.parent) {
217210
const calledFunctionArguments = this.parent.calledFunctionsArguments[this.name];
218211
for (let i = 0; i < calledFunctionArguments.length; i++) {
219212
const calledFunctionArgument = calledFunctionArguments[i];
220213
if (calledFunctionArgument[argumentIndex] !== null) {
221-
return this.argumentTypes[argumentIndex] = calledFunctionArgument[argumentIndex].type;
214+
type = calledFunctionArgument[argumentIndex].type;
215+
this.argumentTypes[argumentIndex] = type;
216+
break;
222217
}
223218
}
224219
}
225220
}
226-
return 'Number';
221+
return type === 'Float' ? 'Number' : type;
227222
}
228223

229224
getConstantType(constantName) {
230225
if (this.constantTypes[constantName]) {
231-
return this.constantTypes[constantName];
226+
const type = this.constantTypes[constantName];
227+
if (type === 'Float') {
228+
return 'Number';
229+
} else {
230+
return type;
231+
}
232232
}
233233
return null;
234234
}

src/backend/kernel.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ class Kernel {
9797

9898
/**
9999
*
100-
* @type {IFunction[]}
100+
* @type {IGPUFunction[]}
101101
*/
102102
this.functions = null;
103103

@@ -202,8 +202,8 @@ class Kernel {
202202
this.argumentSizes = [];
203203
for (let i = 0; i < args.length; i++) {
204204
const arg = args[i];
205-
206-
this.argumentTypes.push(utils.getVariableType(arg));
205+
const argType = utils.getVariableType(arg);
206+
this.argumentTypes.push(argType === 'Integer' ? 'Number' : argType);
207207
this.argumentSizes.push(arg.constructor === Input ? arg.size : null);
208208
}
209209

@@ -219,7 +219,7 @@ class Kernel {
219219
this.constantTypes = {};
220220
if (this.constants) {
221221
for (let p in this.constants) {
222-
this.constantTypes[p] = utils.getVariableType(this.constants[p], true);
222+
this.constantTypes[p] = utils.getVariableType(this.constants[p]);
223223
}
224224
}
225225
}

0 commit comments

Comments
 (0)