From 52369d494c2240fe73ed21c57b05470d35522874 Mon Sep 17 00:00:00 2001 From: Jannik <72194488+JannikGM@users.noreply.github.com> Date: Wed, 23 Aug 2023 17:26:04 +0200 Subject: [PATCH 1/2] Report WebGL errors --- src/backend/commands/baseCommand.ts | 7 ++++-- src/backend/spies/commandSpy.ts | 32 ++++++++++++++++++++++-- src/backend/types/functionInformation.ts | 1 + 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/backend/commands/baseCommand.ts b/src/backend/commands/baseCommand.ts index 9c5bf96..3c0cfeb 100644 --- a/src/backend/commands/baseCommand.ts +++ b/src/backend/commands/baseCommand.ts @@ -17,7 +17,7 @@ export abstract class BaseCommand { // Includes uniform functions special cases to prevent lots of inheritence. const text = (functionInformation.name.indexOf("uniform") === 0) ? this.stringifyUniform(functionInformation.arguments) : - this.stringify(functionInformation.arguments, functionInformation.result); + this.stringify(functionInformation.arguments, functionInformation.result, functionInformation.errors); const commandCapture = { id: commandCaptureId, @@ -81,7 +81,7 @@ export abstract class BaseCommand { // Nothing by default. } - protected stringify(args: IArguments, result: any): string { + protected stringify(args: IArguments, result: any, errors: any[]): string { let stringified = this.spiedCommandName; if (args && args.length > 0) { stringified += ": " + this.stringifyArgs(args).join(", "); @@ -89,6 +89,9 @@ export abstract class BaseCommand { if (result !== undefined && result !== null) { stringified += " -> " + this.stringifyResult(result); } + if (errors.length > 0) { + stringified += " ~> " + errors.map(error => this.stringifyValue(error)).join(", "); + } return stringified; } diff --git a/src/backend/spies/commandSpy.ts b/src/backend/spies/commandSpy.ts index 3948d45..686cbba 100644 --- a/src/backend/spies/commandSpy.ts +++ b/src/backend/spies/commandSpy.ts @@ -113,13 +113,41 @@ export class CommandSpy { // tslint:disable-next-line:only-arrow-functions return function () { const before = Time.now; - const result = OriginFunctionHelper.executeOriginFunction(self.spiedCommandRunningContext, self.spiedCommandName, arguments); + let result = OriginFunctionHelper.executeOriginFunction(self.spiedCommandRunningContext, self.spiedCommandName, arguments); const after = Time.now; - const functionInformation = { + // Initialize error logging + const gl = self.spiedCommandRunningContext; + if (!gl.currentErrors) { + gl.currentErrors = []; + } + + const newErrors = [] + if (self.spiedCommandName === 'getError') { + // Inject one of the collected errors + result = gl.currentErrors.shift() ?? gl.NO_ERROR; + } else { + // Record new errors + while (true) { + const errorResult = OriginFunctionHelper.executeOriginFunction(gl, "getError", undefined); + if (errorResult === gl.NO_ERROR) { + break; + } + newErrors.push(errorResult); + } + // Move new errors to the accumulated list + newErrors.forEach(newError => { + if (!gl.currentErrors.includes(newError)) { + gl.currentErrors.push(newError); + } + }); + } + + const functionInformation: IFunctionInformation = { name: self.spiedCommandName, arguments, result, + errors: newErrors, startTime: before, endTime: after, }; diff --git a/src/backend/types/functionInformation.ts b/src/backend/types/functionInformation.ts index d1694e9..5a9e951 100644 --- a/src/backend/types/functionInformation.ts +++ b/src/backend/types/functionInformation.ts @@ -6,6 +6,7 @@ export interface IFunctionInformation { readonly name: string; readonly arguments: IArguments; readonly result: any; + readonly errors: any[]; readonly startTime: number; readonly endTime: number; } From e83e110e195ab789c3b1a348bbded090710ae649 Mon Sep 17 00:00:00 2001 From: Jannik <72194488+JannikGM@users.noreply.github.com> Date: Wed, 23 Aug 2023 19:05:20 +0200 Subject: [PATCH 2/2] Add sample for WebGL getError --- sample/js/error.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 sample/js/error.js diff --git a/sample/js/error.js b/sample/js/error.js new file mode 100644 index 0000000..ae9c486 --- /dev/null +++ b/sample/js/error.js @@ -0,0 +1,17 @@ +const canvas = document.getElementById("renderCanvas"); +const gl = canvas.getContext("webgl"); + +function loop() { + gl.clearColor(0.3, 0.3, 0.3, 1.0); // Clear to black, fully opaque + gl.clearDepth(1.0); // Clear everything + gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) + + console.log(gl.getError(), "before") + gl.clear(0xFFFF) + console.log(gl.getError(), "after") + + window.requestAnimationFrame(function() { + loop(); + }); +} +loop();