Skip to content

Commit 5dcfed0

Browse files
committed
Extra floating point safety?
1 parent 3ef17b5 commit 5dcfed0

1 file changed

Lines changed: 20 additions & 6 deletions

File tree

src/backend/mode_gpu.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,20 @@
5353
return ret;
5454
}
5555

56-
function flatten(arr) {
56+
function flatten(arr, padding) {
57+
function zeros(n) {
58+
return Array.apply(null, Array(n)).map(Number.prototype.valueOf,0);
59+
}
60+
function concatWithPadding(a, b) {
61+
return [].concat(a, zeros(padding), b);
62+
}
63+
5764
if (Array.isArray(arr[0])) {
58-
return [].concat.apply([], arr);
65+
if (padding) {
66+
return (concatWithPadding.apply([], arr)).concat(zeros(padding));
67+
} else {
68+
return [].concat.apply([], arr);
69+
}
5970
} else {
6071
return arr;
6172
}
@@ -272,6 +283,7 @@
272283
'vec3 threadId;',
273284
'',
274285
'vec3 indexTo3D(float idx, vec3 texDim) {',
286+
' idx = floor(idx + 0.5);',
275287
' float z = floor(idx / (texDim.x * texDim.y));',
276288
' idx -= z * texDim.x * texDim.y;',
277289
' float y = floor(idx / texDim.x);',
@@ -283,9 +295,10 @@
283295
' vec3 xyz = vec3(floor(x + 0.5), floor(y + 0.5), floor(z + 0.5));',
284296
(opt.wraparound ? ' xyz = mod(xyz, texDim);' : ''),
285297
' float index = (xyz.z * texDim.x * texDim.y) + (xyz.y * texDim.x) + xyz.x;',
286-
' float t = (floor(index / texSize.x) + 0.5) / texSize.y;',
287-
' float s = (mod(index, texSize.x) + 0.5) / texSize.x;',
288-
' return decode32(texture2D(tex, vec2(s, t)));',
298+
' float t = (floor(index / texSize.x) + 0.5);',
299+
' float s = mod(index, texSize.x);',
300+
' s = (s < 0.5) ? 0.0 : s + 0.5;',
301+
' return decode32(texture2D(tex, vec2(s / texSize.x, t / texSize.y)));',
289302
'}',
290303
'',
291304
'float get(sampler2D tex, vec2 texSize, vec3 texDim, float y, float x) {',
@@ -311,7 +324,7 @@
311324
builder.webglString("kernel", opt),
312325
'',
313326
'void main(void) {',
314-
' index = floor(vTexCoord.s * float(uTexSize.x)) + floor(vTexCoord.t * float(uTexSize.y)) * uTexSize[0];',
327+
' index = floor(vTexCoord.s * float(uTexSize.x)) + floor(vTexCoord.t * float(uTexSize.y)) * uTexSize.x;',
315328
' threadId = indexTo3D(index, uOutputDim);',
316329
' vec4 outputColor = encode32(kernel());',
317330
' if (outputToColor == true) {',
@@ -403,6 +416,7 @@
403416
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
404417
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
405418

419+
406420
var paramArray = flatten(arguments[textureCount]);
407421
while (paramArray.length < paramSize[0] * paramSize[1]) {
408422
paramArray.push(0);

0 commit comments

Comments
 (0)