Skip to content

Commit 96e5149

Browse files
WebGPUBackend: Simplify Timestamp Queries and Ensure Work Done (#29970)
* WebGPUBackend: Simplify Timestamp Queries and Ensure Work Done * cleanup
1 parent 66bba62 commit 96e5149

File tree

1 file changed

+22
-40
lines changed

1 file changed

+22
-40
lines changed

src/renderers/webgpu/WebGPUBackend.js

Lines changed: 22 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,7 +1166,7 @@ class WebGPUBackend extends Backend {
11661166
}
11671167

11681168

1169-
async initTimestampQuery( renderContext, descriptor ) {
1169+
initTimestampQuery( renderContext, descriptor ) {
11701170

11711171
if ( ! this.trackTimestamp ) return;
11721172

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

11771177

1178-
// Push an error scope to catch any errors during query set creation
1179-
this.device.pushErrorScope( 'out-of-memory' );
1180-
1181-
const timeStampQuerySet = await this.device.createQuerySet( { type: 'timestamp', count: 2, label: `timestamp_renderContext_${renderContext.id}` } );
1182-
1183-
// Pop the error scope and check for errors
1184-
const error = await this.device.popErrorScope();
1185-
1186-
if ( error ) {
1187-
1188-
if ( ! renderContextData.attemptingTimeStampQuerySetFailed ) {
1189-
1190-
console.error( `[GPUOutOfMemoryError][renderContext_${renderContext.id}]:\nFailed to create timestamp query set. This may be because timestamp queries are already running in other tabs.` );
1191-
renderContextData.attemptingTimeStampQuerySetFailed = true;
1192-
1193-
}
1194-
1195-
renderContextData.timeStampQuerySet = null; // Mark as unavailable
1196-
return;
1197-
1198-
}
1178+
const type = renderContext.isComputeNode ? 'compute' : 'render';
1179+
const timeStampQuerySet = this.device.createQuerySet( { type: 'timestamp', count: 2, label: `timestamp_${type}_${renderContext.id}` } );
11991180

12001181
const timestampWrites = {
12011182
querySet: timeStampQuerySet,
@@ -1219,7 +1200,6 @@ class WebGPUBackend extends Backend {
12191200

12201201
const renderContextData = this.get( renderContext );
12211202

1222-
if ( ! renderContextData.timeStampQuerySet ) return;
12231203

12241204
const size = 2 * BigInt64Array.BYTES_PER_ELEMENT;
12251205

@@ -1235,18 +1215,21 @@ class WebGPUBackend extends Backend {
12351215
label: 'timestamp result buffer',
12361216
size: size,
12371217
usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ,
1238-
} ),
1239-
isMappingPending: false,
1218+
} )
12401219
};
12411220

12421221
}
12431222

1244-
const { resolveBuffer, resultBuffer, isMappingPending } = renderContextData.currentTimestampQueryBuffers;
1223+
const { resolveBuffer, resultBuffer } = renderContextData.currentTimestampQueryBuffers;
12451224

1246-
if ( isMappingPending === true ) return;
12471225

12481226
encoder.resolveQuerySet( renderContextData.timeStampQuerySet, 0, 2, resolveBuffer, 0 );
1249-
encoder.copyBufferToBuffer( resolveBuffer, 0, resultBuffer, 0, size );
1227+
1228+
if ( resultBuffer.mapState === 'unmapped' ) {
1229+
1230+
encoder.copyBufferToBuffer( resolveBuffer, 0, resultBuffer, 0, size );
1231+
1232+
}
12501233

12511234
}
12521235

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

12571240
const renderContextData = this.get( renderContext );
12581241

1259-
if ( ! renderContextData.timeStampQuerySet ) return;
1260-
12611242
if ( renderContextData.currentTimestampQueryBuffers === undefined ) return;
12621243

1263-
const { resultBuffer, isMappingPending } = renderContextData.currentTimestampQueryBuffers;
1244+
const { resultBuffer } = renderContextData.currentTimestampQueryBuffers;
12641245

1265-
if ( isMappingPending === true ) return;
1246+
await this.device.queue.onSubmittedWorkDone();
12661247

1267-
renderContextData.currentTimestampQueryBuffers.isMappingPending = true;
1248+
if ( resultBuffer.mapState === 'unmapped' ) {
12681249

1269-
resultBuffer.mapAsync( GPUMapMode.READ ).then( () => {
1250+
resultBuffer.mapAsync( GPUMapMode.READ ).then( () => {
12701251

1271-
const times = new BigUint64Array( resultBuffer.getMappedRange() );
1272-
const duration = Number( times[ 1 ] - times[ 0 ] ) / 1000000;
1252+
const times = new BigUint64Array( resultBuffer.getMappedRange() );
1253+
const duration = Number( times[ 1 ] - times[ 0 ] ) / 1000000;
12731254

12741255

1275-
this.renderer.info.updateTimestamp( type, duration );
1256+
this.renderer.info.updateTimestamp( type, duration );
12761257

1277-
resultBuffer.unmap();
1258+
resultBuffer.unmap();
12781259

1279-
renderContextData.currentTimestampQueryBuffers.isMappingPending = false;
12801260

1281-
} );
1261+
} );
1262+
1263+
}
12821264

12831265
}
12841266

0 commit comments

Comments
 (0)