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

Add timestamp query example #472

Merged
merged 5 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
29 changes: 29 additions & 0 deletions sample/timestampQuery/PerfCounter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// A minimalistic perf timer class that computes mean + stddev online
export default class PerfCounter {
sampleCount: number;
accumulated: number;
accumulatedSq: number;

constructor() {
this.sampleCount = 0;
this.accumulated = 0;
this.accumulatedSq = 0;
}

addSample(value: number) {
this.sampleCount += 1;
this.accumulated += value;
this.accumulatedSq += value * value;
}

getAverage(): number {
return this.sampleCount === 0 ? 0 : this.accumulated / this.sampleCount;
}

getStddev(): number {
if (this.sampleCount === 0) return 0;
const avg = this.getAverage();
const variance = this.accumulatedSq / this.sampleCount - avg * avg;
return Math.sqrt(Math.max(0.0, variance));
}
}
96 changes: 96 additions & 0 deletions sample/timestampQuery/TimestampQueryManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Regroups all timestamp-related operations and resources.
export default class TimestampQueryManager {
// The device may not support timestamp queries, on which case this whole
// class does nothing.
timestampSupported: boolean

Check failure on line 5 in sample/timestampQuery/TimestampQueryManager.ts

View workflow job for this annotation

GitHub Actions / build

Insert `;`

// Number of timestamp counters
timestampCount: number

Check failure on line 8 in sample/timestampQuery/TimestampQueryManager.ts

View workflow job for this annotation

GitHub Actions / build

Insert `;`

// The query objects. This is meant to be used in a ComputePassDescriptor's
// or RenderPassDescriptor's 'timestampWrites' field.
timestampQuerySet: GPUQuerySet

Check failure on line 12 in sample/timestampQuery/TimestampQueryManager.ts

View workflow job for this annotation

GitHub Actions / build

Insert `;`

// A buffer where to store query results
timestampBuffer: GPUBuffer

Check failure on line 15 in sample/timestampQuery/TimestampQueryManager.ts

View workflow job for this annotation

GitHub Actions / build

Insert `;`

// A buffer to map this result back to CPU
timestampMapBuffer: GPUBuffer

Check failure on line 18 in sample/timestampQuery/TimestampQueryManager.ts

View workflow job for this annotation

GitHub Actions / build

Insert `;`

// State used to avoid firing concurrent readback of timestamp values
hasOngoingTimestampReadback: boolean

Check failure on line 21 in sample/timestampQuery/TimestampQueryManager.ts

View workflow job for this annotation

GitHub Actions / build

Insert `;`

// Device must have the "timestamp-query" feature
constructor(device: GPUDevice, timestampCount: number) {
this.timestampSupported = device.features.has("timestamp-query");

Check failure on line 25 in sample/timestampQuery/TimestampQueryManager.ts

View workflow job for this annotation

GitHub Actions / build

Replace `"timestamp-query"` with `'timestamp-query'`
if (!this.timestampSupported) return;

this.timestampCount = timestampCount;

// Create timestamp queries
this.timestampQuerySet = device.createQuerySet({
type: "timestamp",

Check failure on line 32 in sample/timestampQuery/TimestampQueryManager.ts

View workflow job for this annotation

GitHub Actions / build

Replace `"timestamp"` with `'timestamp'`
count: timestampCount, // begin and end
});

// Create a buffer where to store the result of GPU queries
const timestampByteSize = 8; // timestamps are uint64
const timestampBufferSize = timestampCount * timestampByteSize;
this.timestampBuffer = device.createBuffer({
size: timestampBufferSize,
usage: GPUBufferUsage.COPY_SRC | GPUBufferUsage.QUERY_RESOLVE,
});

// Create a buffer to map the result back to the CPU
this.timestampMapBuffer = device.createBuffer({
size: timestampBufferSize,
usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ,
});

this.hasOngoingTimestampReadback = false;
}

// Resolve all timestamp queries and copy the result into the map buffer
resolveAll(commandEncoder: GPUCommandEncoder) {
if (!this.timestampSupported) return;

// After the end of the measured render pass, we resolve queries into a
// dedicated buffer.
commandEncoder.resolveQuerySet(
this.timestampQuerySet,
0 /* firstQuery */,
this.timestampCount /* queryCount */,
this.timestampBuffer,
0, /* destinationOffset */

Check failure on line 64 in sample/timestampQuery/TimestampQueryManager.ts

View workflow job for this annotation

GitHub Actions / build

Delete `,`
);

if (!this.hasOngoingTimestampReadback) {
// Copy values to the mapped buffer
commandEncoder.copyBufferToBuffer(
this.timestampBuffer, 0,

Check failure on line 70 in sample/timestampQuery/TimestampQueryManager.ts

View workflow job for this annotation

GitHub Actions / build

Insert `⏎·······`
this.timestampMapBuffer, 0,
this.timestampBuffer.size,
);
}
}

// Once resolved, we can read back the value of timestamps
readAsync(onTimestampReadBack: (timestamps: BigUint64Array) => void): void {
if (!this.timestampSupported) return;
if (this.hasOngoingTimestampReadback) return;

this.hasOngoingTimestampReadback = true;

const buffer = this.timestampMapBuffer;
void buffer.mapAsync(GPUMapMode.READ)
.then(() => {
const rawData = buffer.getMappedRange();
const timestamps = new BigUint64Array(rawData);

onTimestampReadBack(timestamps);

buffer.unmap();
this.hasOngoingTimestampReadback = false;
});
}
}
30 changes: 30 additions & 0 deletions sample/timestampQuery/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>webgpu-samples: timestampQuery</title>
<style>
:root {
color-scheme: light dark;
}
html, body {
margin: 0; /* remove default margin */
height: 100%; /* make body fill the browser window */
display: flex;
place-content: center center;
}
canvas {
width: 600px;
height: 600px;
max-width: 100%;
display: block;
}
</style>
<script defer src="main.js" type="module"></script>
<script defer type="module" src="../../js/iframe-helper.js"></script>
</head>
<body>
<canvas></canvas>
</body>
</html>
Loading