Skip to content
This repository has been archived by the owner on Aug 16, 2018. It is now read-only.

Commit

Permalink
Implement for loop with expression limit and disable getPixels in gra…
Browse files Browse the repository at this point in the history
…phical
  • Loading branch information
fuzzie360 committed Feb 25, 2016
1 parent c2cbe2a commit df84a10
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 37 deletions.
3 changes: 2 additions & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var gulp = require('gulp');
var concat = require('gulp-concat');
var rename = require("gulp-rename");
var uglify = require('gulp-uglify');
var gutil = require('gulp-util');

gulp.task('build', function() {
return gulp.src([
Expand All @@ -26,7 +27,7 @@ gulp.task('minify', ['build'], function() {
.pipe(uglify({
mangle: false,
preserveComments: "license"
}))
}).on('error', gutil.log))
.pipe(gulp.dest('bin'));
});

Expand Down
135 changes: 107 additions & 28 deletions src/backend/functionNode_webgl.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,23 @@ var functionNode_webgl = (function() {

var gpu, jsFunctionString;

function isIdentifierKernelParam(paramName, ast, funcParam) {
return funcParam.paramNames.indexOf(paramName) != -1;
}

function ensureIndentifierType(paramName, expectedType, ast, funcParam) {
var start = ast.loc.start;

if (!isIdentifierKernelParam(paramName, funcParam) && expectedType != 'float') {
throw "Error unxpected identifier " + paramName + " on line " + start.line;
} else {
var actualType = funcParam.paramType[funcParam.paramNames.indexOf(paramName)];
if (actualType != expectedType) {
throw "Error unxpected identifier " + paramName + " on line " + start.line;
}
}
}

///
/// Function: functionNode_webgl
///
Expand Down Expand Up @@ -81,6 +98,8 @@ var functionNode_webgl = (function() {
return ast_ContinueStatement(ast, retArr, funcParam);
case "ForStatement":
return ast_ForStatement(ast, retArr, funcParam);
case "WhileStatement":
return ast_WhileStatement(ast, retArr, funcParam);
case "VariableDeclaration":
return ast_VariableDeclaration(ast, retArr, funcParam);
case "VariableDeclarator":
Expand Down Expand Up @@ -148,24 +167,26 @@ var functionNode_webgl = (function() {

// Setup function return type and name
if(funcParam.isRootKernel) {
retArr.push("vec4");
retArr.push("float");
} else {
retArr.push(funcParam.returnType);
}
retArr.push(" ");
retArr.push(funcParam.functionName);
retArr.push("(");

// Arguments handling
for( var i = 0; i < funcParam.paramNames.length; ++i ) {
if( i > 0 ) {
retArr.push(", ");
if(!funcParam.isRootKernel) {
// Arguments handling
for( var i = 0; i < funcParam.paramNames.length; ++i ) {
if( i > 0 ) {
retArr.push(", ");
}

retArr.push( funcParam.paramType[i] );
retArr.push(" ");
retArr.push("user_");
retArr.push( funcParam.paramNames[i] );
}

retArr.push( funcParam.paramType[i] );
retArr.push(" ");
retArr.push("user_");
retArr.push( funcParam.paramNames[i] );
}

// Function opening
Expand All @@ -178,7 +199,7 @@ var functionNode_webgl = (function() {
}

if(funcParam.isRootKernel) {
retArr.push("\nreturn vec4(0.0);");
retArr.push("\nreturn 0.0;");
}

// Function closing
Expand All @@ -194,15 +215,9 @@ var functionNode_webgl = (function() {
///
/// @returns the appened retArr
function ast_ReturnStatement(ast, retArr, funcParam) {
if( funcParam.isRootKernel ) {
retArr.push("return encode32(");
ast_generic(ast.argument, retArr, funcParam);
retArr.push("); ");
} else {
retArr.push("return ");
ast_generic(ast.argument, retArr, funcParam);
retArr.push(";");
}
retArr.push("return ");
ast_generic(ast.argument, retArr, funcParam);
retArr.push(";");

//throw ast_errorOutput(
// "Non main function return, is not supported : "+funcParam.currentFunctionNamespace,
Expand Down Expand Up @@ -309,15 +324,79 @@ var functionNode_webgl = (function() {
ast, funcParam
);
}
retArr.push("for (float ");
ast_generic(forNode.init, retArr, funcParam);
retArr.push(";");
ast_generic(forNode.test, retArr, funcParam);
retArr.push(";");
ast_generic(forNode.update, retArr, funcParam);
retArr.push(")");
ast_generic(forNode.body, retArr, funcParam);

if (forNode.test && forNode.test.type == "BinaryExpression") {
console.log(forNode);
if (forNode.test.right.type == "Identifier") {
retArr.push("for (float ");
ast_generic(forNode.init, retArr, funcParam);
retArr.push(";");
ast_generic(forNode.test.left, retArr, funcParam);
retArr.push(forNode.test.operator);
retArr.push("LOOP_MAX");
retArr.push(";");
ast_generic(forNode.update, retArr, funcParam);
retArr.push(")");

retArr.push("{\n");
retArr.push("if (");
ast_generic(forNode.test.left, retArr, funcParam);
retArr.push(forNode.test.operator);
ast_generic(forNode.test.right, retArr, funcParam);
retArr.push(") {\n");
for (var i = 0; i < forNode.body.body.length; i++) {
ast_generic(forNode.body.body[i], retArr, funcParam);
}
retArr.push("} else {\n");
retArr.push("break;\n");
retArr.push("}\n");
retArr.push("}\n");

return retArr;
} else {
retArr.push("for (float ");
ast_generic(forNode.init, retArr, funcParam);
retArr.push(";");
ast_generic(forNode.test, retArr, funcParam);
retArr.push(";");
ast_generic(forNode.update, retArr, funcParam);
retArr.push(")");
ast_generic(forNode.body, retArr, funcParam);
return retArr;
}
}

throw ast_errorOutput(
"Invalid for statment",
ast, funcParam
);
}

/// Prases the abstract syntax tree, genericially to its respective function
///
/// @param ast the AST object to parse
///
/// @returns the prased openclgl string
function ast_WhileStatement(whileNode, retArr, funcParam) {
throw ast_errorOutput(
"While statements are not allowed",
ast, funcParam
);

/*
if (whileNode.type != "WhileStatement") {
throw ast_errorOutput(
"Invalid while statment",
ast, funcParam
);
}
retArr.push("while (");
ast_generic(whileNode.test, retArr, funcParam);
retArr.push(") {\n");
ast_generic(whileNode.body, retArr, funcParam);
retArr.push("}\n");
return retArr;
*/
}

function ast_AssignmentExpression(assNode, retArr, funcParam) {
Expand Down
24 changes: 16 additions & 8 deletions src/backend/glsl.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,6 @@
var builder = this.functionBuilder;
var endianness = this.endianness;

var kernelNode = new functionNode(gpu, "kernel", kernel);
kernelNode.paramNames = [];
kernelNode.paramType = [];
kernelNode.isRootKernel = true;
builder.addFunctionNode(kernelNode);

var funcStr = kernel.toString();
if( !validateStringIsFunction(funcStr) ) {
throw "Unable to get body of kernel function";
Expand Down Expand Up @@ -197,8 +191,10 @@
if (program === undefined) {
var paramStr = '';

var paramType = [];
for (var i=0; i<paramNames.length; i++) {
var argType = getArgumentType(arguments[i]);
paramType.push(argType);
if (opt.hardcodeConstants) {
if (argType == "Array" || argType == "Texture") {
var paramDim = getDimensions(arguments[i], true);
Expand All @@ -221,6 +217,12 @@
}
}

var kernelNode = new functionNode(gpu, "kernel", kernel);
kernelNode.paramNames = paramNames;
kernelNode.paramType = paramType;
kernelNode.isRootKernel = true;
builder.addFunctionNode(kernelNode);

var vertShaderSrc = [
'precision highp float;',
'precision highp int;',
Expand All @@ -240,6 +242,8 @@
'precision highp float;',
'precision highp int;',
'',
'#define LOOP_MAX 100.0',
'',
opt.hardcodeConstants ? 'vec3 uOutputDim = vec3('+threadDim[0]+','+threadDim[1]+', '+ threadDim[2]+');' : 'uniform vec3 uOutputDim;',
opt.hardcodeConstants ? 'vec2 uTexSize = vec2('+texSize[0]+','+texSize[1]+');' : 'uniform vec2 uTexSize;',
'varying vec2 vTexCoord;',
Expand Down Expand Up @@ -312,12 +316,11 @@
'',
paramStr,
builder.webglString("kernel"),
//compileToGlsl(funcStr, {}),
'',
'void main(void) {',
' index = floor(vTexCoord.s * float(uTexSize.x)) + floor(vTexCoord.t * float(uTexSize.y)) * uTexSize[0];',
' threadId = indexTo3D(index, uOutputDim);',
' vec4 outputColor = kernel();',
' vec4 outputColor = encode32(kernel());',
' if (outputToColor == true) {',
' gl_FragColor = actualColor;',
' } else {',
Expand Down Expand Up @@ -471,6 +474,11 @@
gl.bindFramebuffer(gl.FRAMEBUFFER, null);

gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);

if (opt.graphical) {
return;
}

var bytes = new Uint8Array(texSize[0]*texSize[1]*4);
gl.readPixels(0, 0, texSize[0], texSize[1], gl.RGBA, gl.UNSIGNED_BYTE, bytes);
var result = Array.prototype.slice.call(new Float32Array(bytes.buffer));
Expand Down

0 comments on commit df84a10

Please sign in to comment.