Skip to content

Instantly share code, notes, and snippets.

@JosePedroDias
Last active October 28, 2021 16:38
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 JosePedroDias/619388ae562ea9f89e3b8e4b188f6146 to your computer and use it in GitHub Desktop.
Save JosePedroDias/619388ae562ea9f89e3b8e4b188f6146 to your computer and use it in GitHub Desktop.
simple interceptor of webgl context calls (for when spector.js is an overkill)

the code should be called right after we obtain the GL context. Don't forget to reset/edit ignoreCommands if you intend to capture some of those.

example usages:

fakeGl.captureOperations(40)
...
fakeGl.ignoreCommands = []
fakeGl.setHistogramMode(true)
fakeGl.captureFrames(1)
...
fakeGl.setHistogramMode(false)
var originalGl = gl;
var changedGl = {};
var fakeGl = {
ignoreCommands: 'activeTexture bindBuffer bindFramebuffer bufferSubData clear clearColor uniform2f vertexAttribPointer viewport'.split(' '),
setHistogramMode(bool) {
if (bool) {
fakeGl.histogram = {};
} else {
var h = fakeGl.histogram;
delete fakeGl.histogram;
return h;
}
},
captureOperations: function(numOps) {
fakeGl.commandsLeftToCapture = numOps;
fakeGl.capture = true;
},
captureFrames: function(numFrames) {
fakeGl.framesToCapture = numFrames;
fakeGl.capture = true;
function onFrame() {
console.warn('***** GL frame #' + fakeGl.framesToCapture + ' *****');
--fakeGl.framesToCapture;
if (!fakeGl.framesToCapture) {
fakeGl.capture = false;
} else {
requestAnimationFrame(onFrame);
}
}
requestAnimationFrame(onFrame);
}
};
window.fakeGl = fakeGl;
function fakeMethod(methodName) {
return function() {
if (fakeGl.capture && fakeGl.ignoreCommands.indexOf(methodName) === -1) {
if (fakeGl.histogram) {
if (!fakeGl.histogram[methodName]) fakeGl.histogram[methodName] = 1;
else ++fakeGl.histogram[methodName];
} else {
var args = Array.prototype.slice.apply(arguments);
console.warn('gl.' + methodName, args);
}
if (fakeGl.commandsLeftToCapture) {
--fakeGl.commandsLeftToCapture;
if (!fakeGl.commandsLeftToCapture) { fakeGl.capture = false; }
}
}
return originalGl[methodName].apply(originalGl, arguments);
}
}
for (var k in gl) {
changedGl[k] = typeof gl[k] === 'function' ? fakeMethod(k) : originalGl[k];
}
changedGl.prototype = originalGl;
gl = changedGl;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment