Skip to content

Commit e435894

Browse files
committed
Add support for numeric constants
1 parent b9deece commit e435894

3 files changed

Lines changed: 65 additions & 39 deletions

File tree

src/backend/functionNode_webgl.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,11 @@ var functionNode_webgl = (function() {
542542

543543
return retArr;
544544
}
545+
546+
// The prefixes to use
547+
var jsMathPrefix = "Math.";
548+
var localPrefix = "this.";
549+
var constantsPrefix = "this.constants.";
545550

546551
function ast_MemberExpression(mNode, retArr, funcParam) {
547552
if(mNode.computed) {
@@ -571,7 +576,12 @@ var functionNode_webgl = (function() {
571576

572577
// Unroll the member expression
573578
var unrolled = ast_MemberExpression_unroll(mNode);
574-
var unrolled_lc = unrolled.toLowerCase()
579+
var unrolled_lc = unrolled.toLowerCase();
580+
581+
// Its a constant, remove this.constants.
582+
if( unrolled.indexOf(constantsPrefix) === 0 ) {
583+
unrolled = 'constants_'+unrolled.slice( constantsPrefix.length );
584+
}
575585

576586
if (unrolled_lc == "this.thread.x") {
577587
retArr.push('threadId.x');
@@ -633,10 +643,6 @@ var functionNode_webgl = (function() {
633643
);
634644
}
635645

636-
// The prefixes to use
637-
var jsMathPrefix = "Math.";
638-
var localPrefix = "this.";
639-
640646
/// Prases the abstract syntax tree, binary expression
641647
///
642648
/// @param ast the AST object to parse

src/backend/mode_cpu.js

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
}
4949
}
5050

51-
var threadDim = clone(opt.dimensions);
51+
var threadDim = GPUUtils.clone(opt.dimensions);
5252

5353
while (threadDim.length < 3) {
5454
threadDim.push(1);
@@ -62,20 +62,6 @@
6262
}
6363
}
6464

65-
var canvas;
66-
var canvasCtx;
67-
var imageData;
68-
var data;
69-
if (opt.graphical) {
70-
canvas = gpu.getCanvas('cpu');
71-
canvas.width = threadDim[0];
72-
canvas.height = threadDim[1];
73-
74-
canvasCtx = canvas.getContext("2d");
75-
imageData = canvasCtx.createImageData(threadDim[0], threadDim[1]);
76-
data = new Uint8ClampedArray(threadDim[0]*threadDim[1]*4);
77-
}
78-
7965
var ctx = {
8066
thread: {
8167
x: 0,
@@ -86,32 +72,47 @@
8672
x: threadDim[0],
8773
y: threadDim[1],
8874
z: threadDim[2]
89-
}
75+
},
76+
constants: opt.constants
9077
};
9178

92-
ctx.color = function(r, g, b, a) {
93-
if (a == undefined) {
94-
a = 1.0;
95-
}
79+
var canvas;
80+
var canvasCtx;
81+
var imageData;
82+
var data;
83+
if (opt.graphical) {
84+
canvas = gpu.getCanvas('cpu');
85+
canvas.width = threadDim[0];
86+
canvas.height = threadDim[1];
9687

97-
r = Math.floor(r * 255);
98-
g = Math.floor(g * 255);
99-
b = Math.floor(b * 255);
100-
a = Math.floor(a * 255);
88+
canvasCtx = canvas.getContext("2d");
89+
imageData = canvasCtx.createImageData(threadDim[0], threadDim[1]);
90+
data = new Uint8ClampedArray(threadDim[0]*threadDim[1]*4);
91+
92+
ctx.color = function(r, g, b, a) {
93+
if (a == undefined) {
94+
a = 1.0;
95+
}
10196

102-
var width = ctx.dimensions.x;
103-
var height = ctx.dimensions.y;
97+
r = Math.floor(r * 255);
98+
g = Math.floor(g * 255);
99+
b = Math.floor(b * 255);
100+
a = Math.floor(a * 255);
104101

105-
var x = ctx.thread.x;
106-
var y = height - ctx.thread.y - 1;
102+
var width = ctx.dimensions.x;
103+
var height = ctx.dimensions.y;
107104

108-
var index = x + y*width;
105+
var x = ctx.thread.x;
106+
var y = height - ctx.thread.y - 1;
109107

110-
data[index*4+0] = r;
111-
data[index*4+1] = g;
112-
data[index*4+2] = b;
113-
data[index*4+3] = a;
114-
};
108+
var index = x + y*width;
109+
110+
data[index*4+0] = r;
111+
data[index*4+1] = g;
112+
data[index*4+2] = b;
113+
data[index*4+3] = a;
114+
};
115+
}
115116

116117
for (ctx.thread.z=0; ctx.thread.z<threadDim[2]; ctx.thread.z++) {
117118
for (ctx.thread.y=0; ctx.thread.y<threadDim[1]; ctx.thread.y++) {
@@ -154,6 +155,11 @@
154155
opt.loopMaxIterations = max;
155156
return ret;
156157
};
158+
159+
ret.constants = function(constants) {
160+
opt.constants = constants;
161+
return ret;
162+
};
157163

158164
ret.wraparound = function() {
159165
opt.wraparound = false;

src/backend/mode_gpu.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,14 @@
163163
var program = programCache[programCacheKey];
164164

165165
if (program === undefined) {
166+
var constantsStr = '';
167+
if (opt.constants) {
168+
for (var name in opt.constants) {
169+
var value = opt.constants[name];
170+
constantsStr += 'const float constants_' + name + '=' + parseInt(value) + '.0;\n';
171+
}
172+
}
173+
166174
var paramStr = '';
167175

168176
var paramType = [];
@@ -289,6 +297,7 @@
289297
'}',
290298
'',
291299
paramStr,
300+
constantsStr,
292301
builder.webglString("kernel", opt),
293302
'',
294303
'void main(void) {',
@@ -490,6 +499,11 @@
490499
opt.loopMaxIterations = max;
491500
return ret;
492501
};
502+
503+
ret.constants = function(constants) {
504+
opt.constants = constants;
505+
return ret;
506+
};
493507

494508
ret.wraparound = function(flag) {
495509
opt.wraparound = flag;

0 commit comments

Comments
 (0)