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

WebGPUBackend: Simplify Timestamp Queries and Ensure Work Done #29970

Merged
Merged
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
62 changes: 22 additions & 40 deletions src/renderers/webgpu/WebGPUBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -1166,7 +1166,7 @@ class WebGPUBackend extends Backend {
}


async initTimestampQuery( renderContext, descriptor ) {
initTimestampQuery( renderContext, descriptor ) {

if ( ! this.trackTimestamp ) return;

Expand All @@ -1175,27 +1175,8 @@ class WebGPUBackend extends Backend {
if ( ! renderContextData.timeStampQuerySet ) {


// Push an error scope to catch any errors during query set creation
this.device.pushErrorScope( 'out-of-memory' );

const timeStampQuerySet = await this.device.createQuerySet( { type: 'timestamp', count: 2, label: `timestamp_renderContext_${renderContext.id}` } );

// Pop the error scope and check for errors
const error = await this.device.popErrorScope();

if ( error ) {

if ( ! renderContextData.attemptingTimeStampQuerySetFailed ) {

console.error( `[GPUOutOfMemoryError][renderContext_${renderContext.id}]:\nFailed to create timestamp query set. This may be because timestamp queries are already running in other tabs.` );
renderContextData.attemptingTimeStampQuerySetFailed = true;

}

renderContextData.timeStampQuerySet = null; // Mark as unavailable
return;

}
const type = renderContext.isComputeNode ? 'compute' : 'render';
const timeStampQuerySet = this.device.createQuerySet( { type: 'timestamp', count: 2, label: `timestamp_${type}_${renderContext.id}` } );

const timestampWrites = {
querySet: timeStampQuerySet,
Expand All @@ -1219,7 +1200,6 @@ class WebGPUBackend extends Backend {

const renderContextData = this.get( renderContext );

if ( ! renderContextData.timeStampQuerySet ) return;

const size = 2 * BigInt64Array.BYTES_PER_ELEMENT;

Expand All @@ -1235,18 +1215,21 @@ class WebGPUBackend extends Backend {
label: 'timestamp result buffer',
size: size,
usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ,
} ),
isMappingPending: false,
} )
};

}

const { resolveBuffer, resultBuffer, isMappingPending } = renderContextData.currentTimestampQueryBuffers;
const { resolveBuffer, resultBuffer } = renderContextData.currentTimestampQueryBuffers;

if ( isMappingPending === true ) return;

encoder.resolveQuerySet( renderContextData.timeStampQuerySet, 0, 2, resolveBuffer, 0 );
encoder.copyBufferToBuffer( resolveBuffer, 0, resultBuffer, 0, size );

if ( resultBuffer.mapState === 'unmapped' ) {

encoder.copyBufferToBuffer( resolveBuffer, 0, resultBuffer, 0, size );

}

}

Expand All @@ -1256,29 +1239,28 @@ class WebGPUBackend extends Backend {

const renderContextData = this.get( renderContext );

if ( ! renderContextData.timeStampQuerySet ) return;

if ( renderContextData.currentTimestampQueryBuffers === undefined ) return;

const { resultBuffer, isMappingPending } = renderContextData.currentTimestampQueryBuffers;
const { resultBuffer } = renderContextData.currentTimestampQueryBuffers;

if ( isMappingPending === true ) return;
await this.device.queue.onSubmittedWorkDone();

renderContextData.currentTimestampQueryBuffers.isMappingPending = true;
if ( resultBuffer.mapState === 'unmapped' ) {

resultBuffer.mapAsync( GPUMapMode.READ ).then( () => {
resultBuffer.mapAsync( GPUMapMode.READ ).then( () => {

const times = new BigUint64Array( resultBuffer.getMappedRange() );
const duration = Number( times[ 1 ] - times[ 0 ] ) / 1000000;
const times = new BigUint64Array( resultBuffer.getMappedRange() );
const duration = Number( times[ 1 ] - times[ 0 ] ) / 1000000;


this.renderer.info.updateTimestamp( type, duration );
this.renderer.info.updateTimestamp( type, duration );

resultBuffer.unmap();
resultBuffer.unmap();

renderContextData.currentTimestampQueryBuffers.isMappingPending = false;

} );
} );

}

}

Expand Down