Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebGPURenderer: Handle Device Lost Event #29767

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/renderers/common/Backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ class Backend {

}

dispose() { }

}

export default Backend;
27 changes: 27 additions & 0 deletions src/renderers/common/Renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ class Renderer {

this._handleObjectFunction = this._renderObjectDirect;

this._isDeviceLost = false;
this.onDeviceLost = this._onDeviceLost;

this._initialized = false;
this._initPromise = null;

Expand Down Expand Up @@ -265,6 +268,8 @@ class Renderer {

async compileAsync( scene, camera, targetScene = null ) {

if ( this._isDeviceLost === true ) return;

if ( this._initialized === false ) await this.init();

// preserve render tree
Expand Down Expand Up @@ -418,6 +423,23 @@ class Renderer {

}

_onDeviceLost( info ) {

let errorMessage = `THREE.WebGPURenderer: ${info.api} Device Lost:\n\nMessage: ${info.message}`;

if ( info.reason ) {

errorMessage += `\nReason: ${info.reason}`;

}

console.error( errorMessage );

this._isDeviceLost = true;

}


_renderBundle( bundle, sceneRef, lightsNode ) {

const { bundleGroup, camera, renderList } = bundle;
Expand Down Expand Up @@ -552,6 +574,8 @@ class Renderer {

_renderScene( scene, camera, useFrameBufferTarget = true ) {

if ( this._isDeviceLost === true ) return;

const frameBufferTarget = useFrameBufferTarget ? this._getFrameBufferTarget() : null;

// preserve render tree
Expand Down Expand Up @@ -1120,6 +1144,7 @@ class Renderer {
dispose() {

this.info.dispose();
this.backend.dispose();
Mugen87 marked this conversation as resolved.
Show resolved Hide resolved

this._animation.dispose();
this._objects.dispose();
Expand Down Expand Up @@ -1163,6 +1188,8 @@ class Renderer {

compute( computeNodes ) {

if ( this.isDeviceLost === true ) return;

if ( this._initialized === false ) {

console.warn( 'THREE.Renderer: .compute() called before the backend is initialized. Try using .computeAsync() instead.' );
Expand Down
25 changes: 25 additions & 0 deletions src/renderers/webgl-fallback/WebGLBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,25 @@ class WebGLBackend extends Backend {

const glContext = ( parameters.context !== undefined ) ? parameters.context : renderer.domElement.getContext( 'webgl2' );

function onContextLost( event ) {

event.preventDefault();

const contextLossInfo = {
api: 'WebGL',
message: event.statusMessage || 'Unknown reason',
reason: null,
originalEvent: event
};

renderer.onDeviceLost( contextLossInfo );

}

this._onContextLost = onContextLost;

renderer.domElement.addEventListener( 'webglcontextlost', onContextLost, false );
Mugen87 marked this conversation as resolved.
Show resolved Hide resolved

this.gl = glContext;

this.extensions = new WebGLExtensions( this );
Expand Down Expand Up @@ -1656,6 +1675,12 @@ class WebGLBackend extends Backend {

}

dispose() {

this.renderer.domElement.removeEventListener( 'webglcontextlost', this._onContextLost );

}

}

export default WebGLBackend;
13 changes: 13 additions & 0 deletions src/renderers/webgpu/WebGPUBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,19 @@ class WebGPUBackend extends Backend {

}

device.lost.then( ( info ) => {

const deviceLossInfo = {
api: 'WebGPU',
message: info.message || 'Unknown reason',
reason: info.reason || null,
originalEvent: info
};

renderer.onDeviceLost( deviceLossInfo );

} );

const context = ( parameters.context !== undefined ) ? parameters.context : renderer.domElement.getContext( 'webgpu' );

this.device = device;
Expand Down