mirror of
https://github.com/galacean/engine.git
synced 2026-06-08 16:52:58 +08:00
Merge pull request #2774 from zhuxudong/fix/context-lost-immediately
fix: context lost immediately
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user