Skip to content

Commit df84a10

Browse files
committed
Implement for loop with expression limit and disable getPixels in graphical
1 parent c2cbe2a commit df84a10

3 files changed

Lines changed: 125 additions & 37 deletions

File tree

gulpfile.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ var gulp = require('gulp');
22
var concat = require('gulp-concat');
33
var rename = require("gulp-rename");
44
var uglify = require('gulp-uglify');
5+
var gutil = require('gulp-util');
56

67
gulp.task('build', function() {
78
return gulp.src([
@@ -26,7 +27,7 @@ gulp.task('minify', ['build'], function() {
2627
.pipe(uglify({
2728
mangle: false,
2829
preserveComments: "license"
29-
}))
30+
}).on('error', gutil.log))
3031
.pipe(gulp.dest('bin'));
3132
});
3233

src/backend/functionNode_webgl.js

Lines changed: 107 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,23 @@ var functionNode_webgl = (function() {
33

44
var gpu, jsFunctionString;
55

6+
function isIdentifierKernelParam(paramName, ast, funcParam) {
7+
return funcParam.paramNames.indexOf(paramName) != -1;
8+
}
9+
10+
function ensureIndentifierType(paramName, expectedType, ast, funcParam) {
11+
var start = ast.loc.start;
12+
13+
if (!isIdentifierKernelParam(paramName, funcParam) && expectedType != 'float') {
14+
throw "Error unxpected identifier " + paramName + " on line " + start.line;
15+
} else {
16+
var actualType = funcParam.paramType[funcParam.paramNames.indexOf(paramName)];
17+
if (actualType != expectedType) {
18+
throw "Error unxpected identifier " + paramName + " on line " + start.line;
19+
}
20+
}
21+
}
22+
623
///
724
/// Function: functionNode_webgl
825
///
@@ -81,6 +98,8 @@ var functionNode_webgl = (function() {
8198
return ast_ContinueStatement(ast, retArr, funcParam);
8299
case "ForStatement":
83100
return ast_ForStatement(ast, retArr, funcParam);
101+
case "WhileStatement":
102+
return ast_WhileStatement(ast, retArr, funcParam);
84103
case "VariableDeclaration":
85104
return ast_VariableDeclaration(ast, retArr, funcParam);
86105
case "VariableDeclarator":
@@ -148,24 +167,26 @@ var functionNode_webgl = (function() {
148167

149168
// Setup function return type and name
150169
if(funcParam.isRootKernel) {
151-
retArr.push("vec4");
170+
retArr.push("float");
152171
} else {
153172
retArr.push(funcParam.returnType);
154173
}
155174
retArr.push(" ");
156175
retArr.push(funcParam.functionName);
157176
retArr.push("(");
158177

159-
// Arguments handling
160-
for( var i = 0; i < funcParam.paramNames.length; ++i ) {
161-
if( i > 0 ) {
162-
retArr.push(", ");
178+
if(!funcParam.isRootKernel) {
179+
// Arguments handling
180+
for( var i = 0; i < funcParam.paramNames.length; ++i ) {
181+
if( i > 0 ) {
182+
retArr.push(", ");
183+
}
184+
185+
retArr.push( funcParam.paramType[i] );
186+
retArr.push(" ");
187+
retArr.push("user_");
188+
retArr.push( funcParam.paramNames[i] );
163189
}
164-
165-
retArr.push( funcParam.paramType[i] );
166-
retArr.push(" ");
167-
retArr.push("user_");
168-
retArr.push( funcParam.paramNames[i] );
169190
}
170191

171192
// Function opening
@@ -178,7 +199,7 @@ var functionNode_webgl = (function() {
178199
}
179200

180201
if(funcParam.isRootKernel) {
181-
retArr.push("\nreturn vec4(0.0);");
202+
retArr.push("\nreturn 0.0;");
182203
}
183204

184205
// Function closing
@@ -194,15 +215,9 @@ var functionNode_webgl = (function() {
194215
///
195216
/// @returns the appened retArr
196217
function ast_ReturnStatement(ast, retArr, funcParam) {
197-
if( funcParam.isRootKernel ) {
198-
retArr.push("return encode32(");
199-
ast_generic(ast.argument, retArr, funcParam);
200-
retArr.push("); ");
201-
} else {
202-
retArr.push("return ");
203-
ast_generic(ast.argument, retArr, funcParam);
204-
retArr.push(";");
205-
}
218+
retArr.push("return ");
219+
ast_generic(ast.argument, retArr, funcParam);
220+
retArr.push(";");
206221

207222
//throw ast_errorOutput(
208223
// "Non main function return, is not supported : "+funcParam.currentFunctionNamespace,
@@ -309,15 +324,79 @@ var functionNode_webgl = (function() {
309324
ast, funcParam
310325
);
311326
}
312-
retArr.push("for (float ");
313-
ast_generic(forNode.init, retArr, funcParam);
314-
retArr.push(";");
315-
ast_generic(forNode.test, retArr, funcParam);
316-
retArr.push(";");
317-
ast_generic(forNode.update, retArr, funcParam);
318-
retArr.push(")");
319-
ast_generic(forNode.body, retArr, funcParam);
327+
328+
if (forNode.test && forNode.test.type == "BinaryExpression") {
329+
console.log(forNode);
330+
if (forNode.test.right.type == "Identifier") {
331+
retArr.push("for (float ");
332+
ast_generic(forNode.init, retArr, funcParam);
333+
retArr.push(";");
334+
ast_generic(forNode.test.left, retArr, funcParam);
335+
retArr.push(forNode.test.operator);
336+
retArr.push("LOOP_MAX");
337+
retArr.push(";");
338+
ast_generic(forNode.update, retArr, funcParam);
339+
retArr.push(")");
340+
341+
retArr.push("{\n");
342+
retArr.push("if (");
343+
ast_generic(forNode.test.left, retArr, funcParam);
344+
retArr.push(forNode.test.operator);
345+
ast_generic(forNode.test.right, retArr, funcParam);
346+
retArr.push(") {\n");
347+
for (var i = 0; i < forNode.body.body.length; i++) {
348+
ast_generic(forNode.body.body[i], retArr, funcParam);
349+
}
350+
retArr.push("} else {\n");
351+
retArr.push("break;\n");
352+
retArr.push("}\n");
353+
retArr.push("}\n");
354+
355+
return retArr;
356+
} else {
357+
retArr.push("for (float ");
358+
ast_generic(forNode.init, retArr, funcParam);
359+
retArr.push(";");
360+
ast_generic(forNode.test, retArr, funcParam);
361+
retArr.push(";");
362+
ast_generic(forNode.update, retArr, funcParam);
363+
retArr.push(")");
364+
ast_generic(forNode.body, retArr, funcParam);
365+
return retArr;
366+
}
367+
}
368+
369+
throw ast_errorOutput(
370+
"Invalid for statment",
371+
ast, funcParam
372+
);
373+
}
374+
375+
/// Prases the abstract syntax tree, genericially to its respective function
376+
///
377+
/// @param ast the AST object to parse
378+
///
379+
/// @returns the prased openclgl string
380+
function ast_WhileStatement(whileNode, retArr, funcParam) {
381+
throw ast_errorOutput(
382+
"While statements are not allowed",
383+
ast, funcParam
384+
);
385+
386+
/*
387+
if (whileNode.type != "WhileStatement") {
388+
throw ast_errorOutput(
389+
"Invalid while statment",
390+
ast, funcParam
391+
);
392+
}
393+
retArr.push("while (");
394+
ast_generic(whileNode.test, retArr, funcParam);
395+
retArr.push(") {\n");
396+
ast_generic(whileNode.body, retArr, funcParam);
397+
retArr.push("}\n");
320398
return retArr;
399+
*/
321400
}
322401

323402
function ast_AssignmentExpression(assNode, retArr, funcParam) {

src/backend/glsl.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,6 @@
150150
var builder = this.functionBuilder;
151151
var endianness = this.endianness;
152152

153-
var kernelNode = new functionNode(gpu, "kernel", kernel);
154-
kernelNode.paramNames = [];
155-
kernelNode.paramType = [];
156-
kernelNode.isRootKernel = true;
157-
builder.addFunctionNode(kernelNode);
158-
159153
var funcStr = kernel.toString();
160154
if( !validateStringIsFunction(funcStr) ) {
161155
throw "Unable to get body of kernel function";
@@ -197,8 +191,10 @@
197191
if (program === undefined) {
198192
var paramStr = '';
199193

194+
var paramType = [];
200195
for (var i=0; i<paramNames.length; i++) {
201196
var argType = getArgumentType(arguments[i]);
197+
paramType.push(argType);
202198
if (opt.hardcodeConstants) {
203199
if (argType == "Array" || argType == "Texture") {
204200
var paramDim = getDimensions(arguments[i], true);
@@ -221,6 +217,12 @@
221217
}
222218
}
223219

220+
var kernelNode = new functionNode(gpu, "kernel", kernel);
221+
kernelNode.paramNames = paramNames;
222+
kernelNode.paramType = paramType;
223+
kernelNode.isRootKernel = true;
224+
builder.addFunctionNode(kernelNode);
225+
224226
var vertShaderSrc = [
225227
'precision highp float;',
226228
'precision highp int;',
@@ -240,6 +242,8 @@
240242
'precision highp float;',
241243
'precision highp int;',
242244
'',
245+
'#define LOOP_MAX 100.0',
246+
'',
243247
opt.hardcodeConstants ? 'vec3 uOutputDim = vec3('+threadDim[0]+','+threadDim[1]+', '+ threadDim[2]+');' : 'uniform vec3 uOutputDim;',
244248
opt.hardcodeConstants ? 'vec2 uTexSize = vec2('+texSize[0]+','+texSize[1]+');' : 'uniform vec2 uTexSize;',
245249
'varying vec2 vTexCoord;',
@@ -312,12 +316,11 @@
312316
'',
313317
paramStr,
314318
builder.webglString("kernel"),
315-
//compileToGlsl(funcStr, {}),
316319
'',
317320
'void main(void) {',
318321
' index = floor(vTexCoord.s * float(uTexSize.x)) + floor(vTexCoord.t * float(uTexSize.y)) * uTexSize[0];',
319322
' threadId = indexTo3D(index, uOutputDim);',
320-
' vec4 outputColor = kernel();',
323+
' vec4 outputColor = encode32(kernel());',
321324
' if (outputToColor == true) {',
322325
' gl_FragColor = actualColor;',
323326
' } else {',
@@ -471,6 +474,11 @@
471474
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
472475

473476
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
477+
478+
if (opt.graphical) {
479+
return;
480+
}
481+
474482
var bytes = new Uint8Array(texSize[0]*texSize[1]*4);
475483
gl.readPixels(0, 0, texSize[0], texSize[1], gl.RGBA, gl.UNSIGNED_BYTE, bytes);
476484
var result = Array.prototype.slice.call(new Float32Array(bytes.buffer));

0 commit comments

Comments
 (0)