Skip to content

Commit b056bc1

Browse files
authored
Address Comments for Timestamp example (#481)
It was mentioned that a previous PR did the wrong thing by possibly applying a time more than once per timer query. This PR addresses that issue by making the TimestampQueryManager take a callback at initialization time that is called, only when a value is available. The value is provided to the callback. This pattern is similar to ResizeObserver I agree with another comment, I kind of feel like it would be more useful to support multiple queries. But, maybe as a sample it's fine to support just 1.
1 parent 530c33a commit b056bc1

File tree

2 files changed

+15
-17
lines changed

2 files changed

+15
-17
lines changed

sample/timestampQuery/TimestampQueryManager.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ export default class TimestampQueryManager {
1414
// A buffer to map this result back to CPU
1515
#timestampMapBuffer: GPUBuffer;
1616

17-
// Last queried elapsed time of the pass in nanoseconds.
18-
passDurationMeasurementNs: number;
17+
// Callback to call when results are available.
18+
#callback: (deltaTimeMs: number) => void;
1919

2020
// Device must have the "timestamp-query" feature
21-
constructor(device: GPUDevice) {
21+
constructor(device: GPUDevice, callback: (deltaTimeNs: number) => void) {
2222
this.timestampSupported = device.features.has('timestamp-query');
2323
if (!this.timestampSupported) return;
2424

25-
this.passDurationMeasurementNs = 0;
25+
this.#callback = callback;
2626

2727
// Create timestamp queries
2828
this.#timestampQuerySet = device.createQuerySet({
@@ -101,7 +101,7 @@ export default class TimestampQueryManager {
101101
// It's possible elapsedNs is negative which means it's invalid
102102
// (see spec https://gpuweb.github.io/gpuweb/#timestamp)
103103
if (elapsedNs >= 0) {
104-
this.passDurationMeasurementNs = elapsedNs;
104+
this.#callback(elapsedNs);
105105
}
106106
buffer.unmap();
107107
});

sample/timestampQuery/main.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,15 @@ quitIfWebGPUNotAvailable(adapter, device);
3333
// NB: Look for 'timestampQueryManager' in this file to locate parts of this
3434
// snippets that are related to timestamps. Most of the logic is in
3535
// TimestampQueryManager.ts.
36-
const timestampQueryManager = new TimestampQueryManager(device);
36+
const timestampQueryManager = new TimestampQueryManager(device, (elapsedNs) => {
37+
// Convert from nanoseconds to milliseconds:
38+
const elapsedMs = Number(elapsedNs) * 1e-6;
39+
renderPassDurationCounter.addSample(elapsedMs);
40+
perfDisplay.innerHTML = `Render Pass duration: ${renderPassDurationCounter
41+
.getAverage()
42+
.toFixed(3)} ms ± ${renderPassDurationCounter.getStddev().toFixed(3)} ms`;
43+
});
44+
3745
const renderPassDurationCounter = new PerfCounter();
3846

3947
const context = canvas.getContext('webgpu') as GPUCanvasContext;
@@ -209,17 +217,7 @@ function frame() {
209217

210218
device.queue.submit([commandEncoder.finish()]);
211219

212-
if (timestampQueryManager.timestampSupported) {
213-
// Show the last successfully downloaded elapsed time.
214-
const elapsedNs = timestampQueryManager.passDurationMeasurementNs;
215-
// Convert from nanoseconds to milliseconds:
216-
const elapsedMs = Number(elapsedNs) * 1e-6;
217-
renderPassDurationCounter.addSample(elapsedMs);
218-
perfDisplay.innerHTML = `Render Pass duration: ${renderPassDurationCounter
219-
.getAverage()
220-
.toFixed(3)} ms ± ${renderPassDurationCounter.getStddev().toFixed(3)} ms`;
221-
}
222-
220+
// Try to download the time stamp.
223221
timestampQueryManager.tryInitiateTimestampDownload();
224222

225223
requestAnimationFrame(frame);

0 commit comments

Comments
 (0)