From e833cc999cfd213aa0f06ab930b03604c36cd5bc Mon Sep 17 00:00:00 2001 From: Robert Plummer Date: Sat, 24 Jun 2017 21:02:04 -0400 Subject: [PATCH] #84 add createKernels for cpu, with all tests passing --- src/backend/cpu/function-builder.js | 2 +- src/backend/cpu/kernel.js | 41 ++++++++++++++--------------- src/backend/kernel-run-shortcut.js | 6 +++-- 3 files changed, 25 insertions(+), 24 deletions(-) diff --git a/src/backend/cpu/function-builder.js b/src/backend/cpu/function-builder.js index e307acd4..2b22c689 100644 --- a/src/backend/cpu/function-builder.js +++ b/src/backend/cpu/function-builder.js @@ -17,7 +17,7 @@ module.exports = class CPUFunctionBuilder extends FunctionBuilderBase { if (node.isSubKernel) { ret += `var ${ node.functionName } = ` + node.jsFunctionString.replace('return', `return ${ node.functionName }Result[this.thread.z][this.thread.y][this.thread.x] =`) + '.bind(this);\n'; } else { - ret += node.jsFunctionString + ';\n'; + ret += `var ${ node.functionName } = ${ node.jsFunctionString };\n`; } } return ret; diff --git a/src/backend/cpu/kernel.js b/src/backend/cpu/kernel.js index 41b65dee..1f59ba64 100644 --- a/src/backend/cpu/kernel.js +++ b/src/backend/cpu/kernel.js @@ -22,7 +22,7 @@ module.exports = class CPUKernel extends KernelBase { z: null }; - this.run = function() { + this.run = function () { this.run = null; this.build(); return this.run.apply(this, arguments); @@ -88,17 +88,27 @@ module.exports = class CPUKernel extends KernelBase { threadDim.push(1); } + if (this.graphical) { + const canvas = this.canvas; + this.runDimensions.x = canvas.width = threadDim[0]; + this.runDimensions.y = canvas.height = threadDim[1]; + this._canvasCtx = canvas.getContext('2d'); + this._imageData = this._canvasCtx.createImageData(threadDim[0], threadDim[1]); + this._colorData = new Uint8ClampedArray(threadDim[0] * threadDim[1] * 4); + } + const kernelString = ` +${ this.subKernelOutputVariableNames === null + ? '' + : this.subKernelOutputVariableNames.map((name) => ` var ${ name } = null;\n`).join('') +} ${ builder.getPrototypeString() } - if (this.graphical) { - this._imageData = this._canvasCtx.createImageData(${ threadDim[0] }, ${ threadDim[1] }); - this._colorData = new Uint8ClampedArray(${ threadDim[0] * threadDim[1] * 4 }); - } - + var fn = function fn(${ this.paramNames.join(', ') }) { ${ this._fnBody } }.bind(this); + return function (${ this.paramNames.join(', ') }) { var ret = new Array(${ threadDim[2] }); ${ this.subKernelOutputVariableNames === null ? '' - : this.subKernelOutputVariableNames.map((name) => ` var ${ name } = new Array(${ threadDim[2] });\n`).join('') + : this.subKernelOutputVariableNames.map((name) => ` ${ name } = new Array(${ threadDim[2] });\n`).join('') } for (this.thread.z = 0; this.thread.z < ${ threadDim[2] }; this.thread.z++) { ret[this.thread.z] = new Array(${ threadDim[1] }); @@ -113,7 +123,7 @@ ${ this.subKernelOutputVariableNames === null : this.subKernelOutputVariableNames.map((name) => ` ${ name }[this.thread.z][this.thread.y] = new Array(${ threadDim[0] });\n`).join('') } for (this.thread.x = 0; this.thread.x < ${ threadDim[0] }; this.thread.x++) { - ret[this.thread.z][this.thread.y][this.thread.x] = fn.apply(this, arguments); + ret[this.thread.z][this.thread.y][this.thread.x] = fn(${ this.paramNames.join(', ') }); } } } @@ -152,10 +162,7 @@ ${ this.subKernelOutputVariableNames === null ${ this.subKernelOutputVariableNames.map((name) => `${ name }: ${ name }`).join(',\n') } };` } - - function fn(${ this.paramNames.join(',') }) { - ${ this._fnBody } - }`; + }.bind(this);`; if (this.debug) { console.log('Options:'); @@ -164,15 +171,7 @@ ${ this.subKernelOutputVariableNames === null console.log(kernelString); } - this.run = new Function(this.paramNames, kernelString).bind(this); - - - if (this.graphical) { - const canvas = this.canvas; - this.runDimensions.x = canvas.width = threadDim[0]; - this.runDimensions.y = canvas.height = threadDim[1]; - this._canvasCtx = canvas.getContext('2d'); - } + this.run = new Function([], kernelString).bind(this)(); } color(r, g, b, a) { diff --git a/src/backend/kernel-run-shortcut.js b/src/backend/kernel-run-shortcut.js index 897b4e67..ca085d74 100644 --- a/src/backend/kernel-run-shortcut.js +++ b/src/backend/kernel-run-shortcut.js @@ -1,7 +1,7 @@ module.exports = function kernelRunShortcut(kernel) { const shortcut = function() { - return this.run.apply(this, arguments); - }.bind(kernel); + return kernel.run.apply(kernel, arguments); + }; allPropertiesOf(kernel).forEach((key) => { if (key[0] === '_' && key[1] === '_') return; @@ -24,6 +24,8 @@ module.exports = function kernelRunShortcut(kernel) { } }); + shortcut.kernel = kernel; + return shortcut; };