Skip to content

Commit 8ddbab6

Browse files
committed
Merge commit '76ef54e540df23343c01b95c3d14b8bc70843857'
2 parents fc347c4 + 76ef54e commit 8ddbab6

4 files changed

Lines changed: 47 additions & 15 deletions

File tree

src/backend/fallback.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,20 @@
7979
}
8080
}
8181

82+
if (opt.graphical) {
83+
throw "CPU fallback for graphical output is not supported!";
84+
}
85+
8286
var ctx = {
8387
thread: {
8488
x: 0,
8589
y: 0,
8690
z: 0
8791
},
88-
dimensions: threadDim
92+
dimensions: threadDim,
93+
color: function(r, g, b, a) {
94+
console.warn("color() does nothing on fallback mode");
95+
}
8996
};
9097

9198
for (ctx.thread.z=0; ctx.thread.z<threadDim[2]; ctx.thread.z++) {

src/backend/functionNode_webgl.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ var functionNode_webgl = (function() {
138138
retArr.push("\n");
139139
}
140140

141+
if(funcParam.isRootKernel) {
142+
retArr.push("\nreturn vec4(0.0);");
143+
}
144+
141145
// Function closing
142146
retArr.push("}\n");
143147
return retArr;
@@ -339,13 +343,13 @@ var functionNode_webgl = (function() {
339343
}
340344

341345
function ast_IfStatement(ifNode, retArr, funcParam) {
342-
retArr.push(" if(");
346+
retArr.push("if(");
343347
ast_generic(ifNode.test, retArr, funcParam);
344348
retArr.push(")");
345349
ast_generic(ifNode.consequent, retArr, funcParam);
346350

347351
if (ifNode.alternate) {
348-
retArr.push("else");
352+
retArr.push("else ");
349353
ast_generic(ifNode.alternate, retArr, funcParam);
350354
}
351355
return retArr;
@@ -489,8 +493,9 @@ var functionNode_webgl = (function() {
489493
);
490494
}
491495

492-
// The math prefix to use
496+
// The prefixes to use
493497
var jsMathPrefix = "Math.";
498+
var localPrefix = "this.";
494499

495500
/// Prases the abstract syntax tree, binary expression
496501
///
@@ -509,6 +514,11 @@ var functionNode_webgl = (function() {
509514
funcName = funcName.slice( jsMathPrefix.length );
510515
}
511516

517+
// Its a local function, remove this
518+
if( funcName.indexOf(localPrefix) === 0 ) {
519+
funcName = funcName.slice( localPrefix.length );
520+
}
521+
512522
// Register the function into the called registry
513523
if( funcParam.calledFunctions.indexOf(funcName) < 0 ) {
514524
funcParam.calledFunctions.push(funcName);

src/backend/glsl.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
}
1818

1919
function dimToTexSize(gl, dimensions) {
20+
if (dimensions.length == 2) {
21+
return dimensions;
22+
}
23+
2024
var numTexels = dimensions[0];
2125
for (var i=1; i<dimensions.length; i++) {
2226
numTexels *= dimensions[i];
@@ -296,12 +300,14 @@
296300
' return get(tex, texSize, texDim, 0.0, 0.0, x);',
297301
'}',
298302
'',
299-
'float color(float r, float g, float b) {',
300-
' return decode32(vec4(r,g,b,1.0));',
303+
'const bool outputToColor = ' + (opt.graphical? 'true' : 'false') + ';',
304+
'vec4 actualColor;',
305+
'void color(float r, float g, float b, float a) {',
306+
' actualColor = vec4(r,g,b,a);',
301307
'}',
302308
'',
303-
'float color(float r, float g, float b, float a) {',
304-
' return decode32(vec4(r,g,b,a));',
309+
'void color(float r, float g, float b) {',
310+
' color(r,g,b,1.0);',
305311
'}',
306312
'',
307313
paramStr,
@@ -311,7 +317,12 @@
311317
'void main(void) {',
312318
' index = floor(vTexCoord.s * float(uTexSize.x)) + floor(vTexCoord.t * float(uTexSize.y)) * uTexSize[0];',
313319
' threadId = indexTo3D(index, uOutputDim);',
314-
' gl_FragColor = kernel();',
320+
' vec4 outputColor = kernel();',
321+
' if (outputToColor == true) {',
322+
' gl_FragColor = actualColor;',
323+
' } else {',
324+
' gl_FragColor = outputColor;',
325+
' }',
315326
'}'
316327
].join('\n');
317328

@@ -335,6 +346,10 @@
335346
throw "Error compiling fragment shader";
336347
}
337348

349+
if (opt.debug) {
350+
console.log(fragShaderSrc);
351+
}
352+
338353
program = gl.createProgram();
339354
gl.attachShader(program, vertShader);
340355
gl.attachShader(program, fragShader);

src/gpu.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ var GPU = (function() {
2525
canvas.width = 2;
2626
canvas.height = 2;
2727
var glOpt = {
28-
depth: false,
28+
depth: false,
2929
antialias: false
30-
};
30+
};
3131

3232
gl = canvas.getContext("experimental-webgl", glOpt) || canvas.getContext("webgl", glOpt);
3333
}
@@ -53,7 +53,7 @@ var GPU = (function() {
5353
return this.canvas;
5454
};
5555

56-
///
56+
///
5757
/// Function: createKernel
5858
///
5959
/// The core GPU.js function
@@ -76,7 +76,7 @@ var GPU = (function() {
7676
///
7777
/// Returns:
7878
/// callable function to run
79-
///
79+
///
8080
function createKernel(kernel, paramObj) {
8181
//
8282
// basic parameters safety checks
@@ -122,8 +122,8 @@ var GPU = (function() {
122122
///
123123
/// Adds additional functions, that the kernel may call.
124124
///
125-
/// Parameters:
126-
/// jsFunction - {JS Function} JS Function to do conversion
125+
/// Parameters:
126+
/// jsFunction - {JS Function} JS Function to do conversion
127127
/// paramTypeArray - {[String,...]} Parameter type array, assumes all parameters are "float" if null
128128
/// returnType - {String} The return type, assumes "float" if null
129129
///

0 commit comments

Comments
 (0)