Merge pull request #2774 from zhuxudong/fix/context-lost-immediately

fix: context lost immediately
This commit is contained in:
ChenMo
2025-07-21 20:50:39 +08:00
committed by GitHub
3 changed files with 13 additions and 8 deletions

View File

@@ -131,7 +131,6 @@ export class Engine extends EventDispatcher {
private _destroyed: boolean = false;
private _frameInProcess: boolean = false;
private _waitingDestroy: boolean = false;
private _isDeviceLost: boolean = false;
private _waitingGC: boolean = false;
private _postProcessPasses = new Array<PostProcessPass>();
private _activePostProcessPasses = new Array<PostProcessPass>();
@@ -381,7 +380,7 @@ export class Engine extends EventDispatcher {
}
// Render scene and fire `onBeginRender` and `onEndRender`
if (!this._isDeviceLost) {
if (!this._hardwareRenderer.isContextLost()) {
this._render(scenes);
}
@@ -652,7 +651,6 @@ export class Engine extends EventDispatcher {
}
private _onDeviceLost(): void {
this._isDeviceLost = true;
// Lose graphic resources
this.resourceManager._lostGraphicResources();
console.log("Device lost.");
@@ -677,7 +675,6 @@ export class Engine extends EventDispatcher {
.then(() => {
console.log("Graphic resource content restored.\n\n" + "Device restored.");
this.dispatch("devicerestored", this);
this._isDeviceLost = false;
})
.catch((error) => {
console.error(error);

View File

@@ -254,7 +254,6 @@ export class ShaderProgram {
// Create program and link shader
const program = gl.createProgram();
if (!program) {
console.warn("Context lost while create program.");
return null;
}
@@ -285,7 +284,6 @@ export class ShaderProgram {
const shader = gl.createShader(shaderType);
if (!shader) {
console.warn("Context lost while create shader.");
return null;
}
@@ -469,7 +467,8 @@ export class ShaderProgram {
private _getUniformInfos(): WebGLActiveInfo[] {
const gl = this._gl;
const program = this._glProgram;
const uniformCount = gl.getProgramParameter(program, gl.ACTIVE_UNIFORMS);
// uniformCount is `null` when context lost.
const uniformCount = gl.getProgramParameter(program, gl.ACTIVE_UNIFORMS) ?? 0;
const uniformInfos = new Array<WebGLActiveInfo>(uniformCount);
for (let i = 0; i < uniformCount; ++i) {
@@ -485,7 +484,8 @@ export class ShaderProgram {
const program = this._glProgram;
const attributeInfos = new Array<WebGLActiveInfo>();
const attributeCount = gl.getProgramParameter(program, gl.ACTIVE_ATTRIBUTES);
// attributeCount is `null` when context lost.
const attributeCount = gl.getProgramParameter(program, gl.ACTIVE_ATTRIBUTES) ?? 0;
for (let i = 0; i < attributeCount; ++i) {
const info = gl.getActiveAttrib(program, i);
attributeInfos[i] = info;

View File

@@ -542,6 +542,14 @@ export class WebGLGraphicDevice implements IHardwareRenderer {
extension.restoreContext();
}
/**
* @remarks
* WebGL context loss and restore can happen at any GPU execution point. refs to: https://www.khronos.org/webgl/wiki/HandlingContextLost
*/
isContextLost() {
return this.gl.isContextLost();
}
resetState(): void {
this._readFrameBuffer = null;
this._enableGlobalDepthBias = false;