@xysfe/memento-core
Version:
record and replay the web
90 lines (87 loc) • 3.69 kB
JavaScript
import { __awaiter, __generator, __spread } from '../../../node_modules/tslib/tslib.es6.js';
import { CanvasContext } from '../../types.js';
import { deserializeArg, variableListFor } from './deserialize-args.js';
function getContext(target, type) {
try {
if (type === CanvasContext.WebGL) {
return (target.getContext('webgl') || target.getContext('experimental-webgl'));
}
return target.getContext('webgl2');
}
catch (e) {
return null;
}
}
var WebGLVariableConstructorsNames = [
'WebGLActiveInfo',
'WebGLBuffer',
'WebGLFramebuffer',
'WebGLProgram',
'WebGLRenderbuffer',
'WebGLShader',
'WebGLShaderPrecisionFormat',
'WebGLTexture',
'WebGLUniformLocation',
'WebGLVertexArrayObject',
];
function saveToWebGLVarMap(ctx, result) {
if (!(result === null || result === void 0 ? void 0 : result.constructor))
return;
var name = result.constructor.name;
if (!WebGLVariableConstructorsNames.includes(name))
return;
var variables = variableListFor(ctx, name);
if (!variables.includes(result))
variables.push(result);
}
function webglMutation(_a) {
var mutation = _a.mutation, target = _a.target, type = _a.type, imageMap = _a.imageMap, errorHandler = _a.errorHandler;
return __awaiter(this, void 0, Promise, function () {
var ctx, original, args, result, debugMode, webglError, error_1;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_b.trys.push([0, 2, , 3]);
ctx = getContext(target, type);
if (!ctx)
return [2];
if (mutation.setter) {
ctx[mutation.property] = mutation.args[0];
return [2];
}
original = ctx[mutation.property];
return [4, Promise.all(mutation.args.map(deserializeArg(imageMap, ctx)))];
case 1:
args = _b.sent();
result = original.apply(ctx, args);
saveToWebGLVarMap(ctx, result);
debugMode = false;
if (debugMode) {
if (mutation.property === 'compileShader') {
if (!ctx.getShaderParameter(args[0], ctx.COMPILE_STATUS))
console.warn('something went wrong in replay', ctx.getShaderInfoLog(args[0]));
}
else if (mutation.property === 'linkProgram') {
ctx.validateProgram(args[0]);
if (!ctx.getProgramParameter(args[0], ctx.LINK_STATUS))
console.warn('something went wrong in replay', ctx.getProgramInfoLog(args[0]));
}
webglError = ctx.getError();
if (webglError !== ctx.NO_ERROR) {
console.warn.apply(console, __spread(['WEBGL ERROR',
webglError,
'on command:',
mutation.property], args));
}
}
return [3, 3];
case 2:
error_1 = _b.sent();
errorHandler(mutation, error_1);
return [3, 3];
case 3: return [2];
}
});
});
}
export { webglMutation as default };