Skip to content

Commit

Permalink
Deploying to gh-pages from @ b056bc1 🚀
Browse files Browse the repository at this point in the history
  • Loading branch information
kainino0x committed Dec 11, 2024
1 parent 4c3fb08 commit a476c6e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 34 deletions.
10 changes: 5 additions & 5 deletions sample/timestampQuery/TimestampQueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ export default class TimestampQueryManager {
// A buffer to map this result back to CPU
#timestampMapBuffer: GPUBuffer;

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

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

this.passDurationMeasurementNs = 0;
this.#callback = callback;

// Create timestamp queries
this.#timestampQuerySet = device.createQuerySet({
Expand Down Expand Up @@ -101,7 +101,7 @@ export default class TimestampQueryManager {
// It's possible elapsedNs is negative which means it's invalid
// (see spec https://gpuweb.github.io/gpuweb/#timestamp)
if (elapsedNs >= 0) {
this.passDurationMeasurementNs = elapsedNs;
this.#callback(elapsedNs);
}
buffer.unmap();
});
Expand Down
30 changes: 14 additions & 16 deletions sample/timestampQuery/main.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sample/timestampQuery/main.js.map

Large diffs are not rendered by default.

22 changes: 10 additions & 12 deletions sample/timestampQuery/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,15 @@ quitIfWebGPUNotAvailable(adapter, device);
// NB: Look for 'timestampQueryManager' in this file to locate parts of this
// snippets that are related to timestamps. Most of the logic is in
// TimestampQueryManager.ts.
const timestampQueryManager = new TimestampQueryManager(device);
const timestampQueryManager = new TimestampQueryManager(device, (elapsedNs) => {
// Convert from nanoseconds to milliseconds:
const elapsedMs = Number(elapsedNs) * 1e-6;
renderPassDurationCounter.addSample(elapsedMs);
perfDisplay.innerHTML = `Render Pass duration: ${renderPassDurationCounter
.getAverage()
.toFixed(3)} ms ± ${renderPassDurationCounter.getStddev().toFixed(3)} ms`;
});

const renderPassDurationCounter = new PerfCounter();

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

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

if (timestampQueryManager.timestampSupported) {
// Show the last successfully downloaded elapsed time.
const elapsedNs = timestampQueryManager.passDurationMeasurementNs;
// Convert from nanoseconds to milliseconds:
const elapsedMs = Number(elapsedNs) * 1e-6;
renderPassDurationCounter.addSample(elapsedMs);
perfDisplay.innerHTML = `Render Pass duration: ${renderPassDurationCounter
.getAverage()
.toFixed(3)} ms ± ${renderPassDurationCounter.getStddev().toFixed(3)} ms`;
}

// Try to download the time stamp.
timestampQueryManager.tryInitiateTimestampDownload();

requestAnimationFrame(frame);
Expand Down

0 comments on commit a476c6e

Please sign in to comment.