Skip to content

Commit

Permalink
Fix input array detection when using typed arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
fuzzie360 committed Dec 25, 2016
1 parent 61d009f commit f9b66e4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
19 changes: 9 additions & 10 deletions src/backend/mode_gpu.js
Expand Up @@ -15,10 +15,10 @@

function getDimensions(x, pad) {
var ret;
if (Array.isArray(x)) {
if (GPUUtils.isArray(x)) {
var dim = [];
var temp = x;
while (Array.isArray(temp)) {
while (GPUUtils.isArray(temp)) {
dim.push(temp.length);
temp = temp[0];
}
Expand Down Expand Up @@ -57,15 +57,15 @@
return ret;
}

function flatten(arr, padding) {
if (Array.isArray(arr[0])) {
if (Array.isArray(arr[0][0])) {
function flatten(arr) {
if (GPUUtils.isArray(arr[0])) {
if (GPUUtils.isArray(arr[0][0])) {
return [].concat.apply([], [].concat.apply([], arr));
} else {
return [].concat.apply([], arr);
}
} else {
return GPUUtils.clone(arr);
return arr;
}
}

Expand Down Expand Up @@ -509,14 +509,13 @@
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);

var paramArray = flatten(arguments[textureCount]);
var paramLength = paramSize[0] * paramSize[1];
if (opt.floatTextures) {
paramLength *= 4;
}
while (paramArray.length < paramLength) {
paramArray.push(0);
}

var paramArray = new Float32Array(paramLength);
paramArray.set(flatten(arguments[textureCount]))

var argBuffer;
if (opt.floatTextures) {
Expand Down
19 changes: 18 additions & 1 deletion src/utils.js
Expand Up @@ -210,6 +210,23 @@ var GPUUtils = (function() {
}
GPUUtils.functionBinder = functionBinder;

///
/// Function: isArray
///
/// Checks if is an array or Array-like object
///
/// Parameters:
/// arg - {Object} The argument object to check if is array
///
/// Returns:
/// {Boolean} true if is array or Array-like object
///
function isArray(arr) {
var tag = Object.prototype.toString.call(arr);
return tag.indexOf('Array]', tag.length - 6) !== -1;
}
GPUUtils.isArray = isArray;

///
/// Function: getArgumentType
///
Expand All @@ -222,7 +239,7 @@ var GPUUtils = (function() {
/// {String} Argument type Array/Number/Texture/Unknown
///
function getArgumentType(arg) {
if (Array.isArray(arg)) {
if (GPUUtils.isArray(arg)) {
return 'Array';
} else if (typeof arg == "number") {
return 'Number';
Expand Down

0 comments on commit f9b66e4

Please sign in to comment.