Skip to content

Instantly share code, notes, and snippets.

@ssube
Created December 9, 2014 22:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ssube/4d972e95a1093748fc60 to your computer and use it in GitHub Desktop.
Save ssube/4d972e95a1093748fc60 to your computer and use it in GitHub Desktop.
var BlurShader = (function () {
function BlurShader() {
}
BlurShader.prototype.apply = function (d, o, s, xmin, xmax, ymin, ymax) {
for (var y = ymin; y < ymax; ++y) {
for (var x = xmin; x < xmax; ++x) {
var p = y * s + x;
var a = d[p] * 2 + d[p - 1] + d[p + 1] + d[p - s] + d[p + s];
a = (1000 * (a + 500)) / 6000;
o[p] = a | 0;
}
}
};
return BlurShader;
})();
module.exports = BlurShader;
var Image = (function () {
function Image(width, height, data) {
if (typeof data === "undefined") { data = null; }
this.width = width;
this.height = height;
this.stride = width + 2;
var area = this.stride * (this.height + 2);
if (data === null) {
this.data = new Array(area);
} else if (data.length != area) {
throw new Error("Invalid data buffer.");
} else {
this.data = data;
}
}
Image.random = function (newWidth, newHeight) {
return new Image(newWidth, newHeight).process(Image.generator);
};
Image.prototype.process = function (f) {
var w = this.width, h = this.height, s = this.stride;
var d = this.data;
var n = new Array(d.length);
f.apply(d, n, s, 1, w, 1, h);
return new Image(w, h, n);
};
Image.prototype.processInline = function () {
var w = this.width, h = this.height, s = this.stride;
var d = this.data;
var n = new Array(d.length);
for (var y = 1; y < h; ++y) {
for (var x = 1; x < w; ++x) {
var p = y * s + x;
var a = d[p] * 2 + d[p - 1] + d[p + 1] + d[p - s] + d[p + s];
n[p] = (1000 * (a + 500)) / 6000;
}
}
return new Image(w, h, n);
};
Image.generator = {
apply: function (i, o, s, xmin, xmax, ymin, ymax) {
for (var y = ymin; y < ymax; ++y) {
for (var x = xmin; x < xmax; ++x) {
var p = y * s + x;
o[p] = Math.random();
}
}
}
};
return Image;
})();
module.exports = Image;
var ShaderBench = require("./ShaderBench");
console.log("Instantiating benchmark...");
var benchmark = new ShaderBench();
console.log("Executing loops with callback...");
for (var i = 0; i < 100; ++i) {
console.log("Executing iteration " + i);
benchmark.testProcessProc();
}
/*console.log("Executing loops with inline...");
for (var i = 0; i < 100; ++i) {
console.log("Executing iteration " + i);
benchmark.testProcessInline();
}*/
var ShaderBench = require("./ShaderBench");
console.log("Instantiating benchmark...");
var benchmark = new ShaderBench();
console.log("Executing loops with callback...");
for (var i = 0; i < 100; ++i) {
console.log("Executing iteration " + i);
benchmark.testProcessProc();
}
/*console.log("Executing loops with inline...");
for (var i = 0; i < 100; ++i) {
console.log("Executing iteration " + i);
benchmark.testProcessInline();
}*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment