Skip to content

Instantly share code, notes, and snippets.

@haxiomic
Last active November 9, 2023 20:22
Show Gist options
  • Save haxiomic/34cb310e5019861220e3d53035dbe211 to your computer and use it in GitHub Desktop.
Save haxiomic/34cb310e5019861220e3d53035dbe211 to your computer and use it in GitHub Desktop.
Log all WebGL calls, print constant names
export function WebGLLogger(gl: WebGL2RenderingContext, filter: Array<string | RegExp> | null = null) {
let constantNameMap = new Map<number, string>();
// enumerate gl
for (let key in gl) {
let value = (gl as any)[key];
if (typeof value === 'number' && key.toUpperCase() === key) {
constantNameMap.set(value, key);
continue;
}
if (filter != null) {
if (!filter.some(f => {
if (typeof f === 'string') {
return key === f;
} else if (f instanceof RegExp) {
return f.test(key);
}
return false;
})) {
continue;
}
}
if (typeof value === 'function') {
let originalFunction: Function = value;
// override function
function overrideFn() {
let argStr = Array.from(arguments).map(arg => {
if (typeof arg === 'number') {
if (arg > 6 || (key.startsWith('draw') && arg > 0)) {
let name = constantNameMap.get(arg);
if (name != null) {
return name;
}
}
}
if (arg instanceof WebGLUniformLocation) {
return (arg as any).name ?? arg;
}
return arg;
});
let result = originalFunction.apply(gl, arguments);
if (key === 'getUniformLocation') {
result.name = arguments[1];
}
console.log(key, argStr);
return result;
}
(gl as any)[key] = overrideFn;
}
}
console.log('WebGLLogger', constantNameMap, filter);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment