Skip to content

Commit

Permalink
Improve timestamp stability
Browse files Browse the repository at this point in the history
  • Loading branch information
toji committed Nov 21, 2023
1 parent 538ebf7 commit ad61137
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/sample/computeBoids/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ const init: SampleInit = async ({ canvas, pageState, gui }) => {
let t = 0;
let computePassDurationSum = 0;
let renderPassDurationSum = 0;
let timerSamples = 0;
function frame() {
// Sample is no longer the active page.
if (!pageState.active) return;
Expand Down Expand Up @@ -311,25 +312,34 @@ const init: SampleInit = async ({ canvas, pageState, gui }) => {
if (hasTimestampQuery) {
resultBuffer.mapAsync(GPUMapMode.READ).then(() => {
const times = new BigInt64Array(resultBuffer.getMappedRange());
computePassDurationSum += Number(times[1] - times[0]);
renderPassDurationSum += Number(times[3] - times[2]);
const computePassDuration = Number(times[1] - times[0]);
const renderPassDuration = Number(times[3] - times[2]);

// In some cases the timestamps may wrap around and produce a negative
// number as the GPU resets it's timings. These can safely be ignored.
if (computePassDuration > 0 && renderPassDuration > 0) {
computePassDurationSum += computePassDuration;
renderPassDurationSum += renderPassDuration;
timerSamples++;
}
resultBuffer.unmap();

// Periodically update the text for the timer stats
const kNumTimerSamples = 100;
if (t % kNumTimerSamples === 0) {
const kNumTimerSamplesPerUpdate = 100;
if (timerSamples >= kNumTimerSamplesPerUpdate) {
const avgComputeMicroseconds = Math.round(
computePassDurationSum / kNumTimerSamples / 1000
computePassDurationSum / timerSamples / 1000
);
const avgRenderMicroseconds = Math.round(
renderPassDurationSum / kNumTimerSamples / 1000
renderPassDurationSum / timerSamples / 1000
);
perfDisplay.textContent = `\
avg compute pass duration: ${avgComputeMicroseconds}µs
avg render pass duration: ${avgRenderMicroseconds}µs
spare readback buffers: ${spareResultBuffers.length}`;
computePassDurationSum = 0;
renderPassDurationSum = 0;
timerSamples = 0;
}
spareResultBuffers.push(resultBuffer);
});
Expand Down

0 comments on commit ad61137

Please sign in to comment.