Skip to content

Commit

Permalink
timestamp expertiments
Browse files Browse the repository at this point in the history
  • Loading branch information
aardgoose committed Oct 27, 2023
1 parent dc9e965 commit 7fa8c96
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions examples/jsm/renderers/webgpu/WebGPUBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,15 @@ class WebGPUBackend extends Backend {
const encoder = device.createCommandEncoder( { label: 'renderContext_' + renderContext.id } );
const currentPass = encoder.beginRenderPass( descriptor );


// timestamps

const tsQuerySet = device.createQuerySet( { type: 'timestamp', count: 2 } );

renderContextData.tsQuerySet = tsQuerySet;

encoder.writeTimestamp( tsQuerySet, 0 );

//

renderContextData.descriptor = descriptor;
Expand All @@ -358,7 +367,7 @@ class WebGPUBackend extends Backend {

}

finishRender( renderContext ) {
async finishRender( renderContext ) {

const renderContextData = this.get( renderContext );
const occlusionQueryCount = renderContext.occlusionQueryCount;
Expand Down Expand Up @@ -413,11 +422,33 @@ class WebGPUBackend extends Backend {

}

renderContextData.encoder.writeTimestamp( renderContextData.tsQuerySet, 1 );

const tsQueryBuffer = this.device.createBuffer( {
size: 2 * 8,
usage: GPUBufferUsage.QUERY_RESOLVE | GPUBufferUsage.COPY_SRC
} );

const tsReadBuffer = this.device.createBuffer( {
size: 2 * 8,
usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ
} );

renderContextData.encoder.resolveQuerySet( renderContextData.tsQuerySet, 0, 2 , tsQueryBuffer, 0 );
renderContextData.encoder.copyBufferToBuffer( tsQueryBuffer, 0, tsReadBuffer, 0, 2* 8 );

this.device.queue.submit( [ renderContextData.encoder.finish() ] );

//

if ( renderContext.textures !== null ) {
await tsReadBuffer.mapAsync( GPUMapMode.READ );

const tsBuffer = tsReadBuffer.getMappedRange();
const tsResults = new BigUint64Array( tsBuffer );

console.log( 'time:', new Intl.NumberFormat().format( ( tsResults[ 1 ] - tsResults[ 0 ] ) / 1000n ), 'us' );

if ( renderContext.texture !== null && renderContext.texture.generateMipmaps === true ) {

const textures = renderContext.textures;

Expand Down

0 comments on commit 7fa8c96

Please sign in to comment.