Files
bworld/client/renderer/gpu_timing.ts
T
2026-10-12 15:09:22 -03:00

112 lines
3.7 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// how long the gpu spends on each render pass, with timestamp queries, for the f3 performance window. passes are
// labeled with the section they started in (see gpu_section in core.ts), and the results come back a few frames
// later, when the gpu is done. chrome rounds timestamps to 0.1 ms unless it's started with
// --enable-webgpu-developer-features
// passes timed per frame, the rest of a frame's aren't
const MAX_PASSES = 32;
// frames whose results can be on their way back at once. when they all are, the next frame isn't timed
const READBACK_BUFFERS = 3;
const WINDOW_MS = 1000;
export class GpuTimer {
readonly supported: boolean;
// ms per frame by label, averaged over the last window
results = new Map<string, number>();
total = 0;
#device: GPUDevice;
#query_set: GPUQuerySet | undefined;
#resolve_buffer: GPUBuffer | undefined;
#free: GPUBuffer[] = [];
#created = 0;
// this frame's
#labels: string[] = [];
#readback: GPUBuffer | undefined;
#sums = new Map<string, number>();
#frames = 0;
#window_start = performance.now();
constructor(device: GPUDevice) {
this.#device = device;
this.supported = device.features.has("timestamp-query");
if (this.supported) {
this.#query_set = device.createQuerySet({ type: "timestamp", count: MAX_PASSES * 2 });
this.#resolve_buffer = device.createBuffer({
size: MAX_PASSES * 16,
usage: GPUBufferUsage.QUERY_RESOLVE | GPUBufferUsage.COPY_SRC,
});
}
}
begin_frame() {
this.#labels = [];
this.#readback = undefined;
if (!this.supported) return;
this.#readback = this.#free.pop();
if (!this.#readback && this.#created < READBACK_BUFFERS) {
this.#created += 1;
this.#readback = this.#device.createBuffer({
size: MAX_PASSES * 16,
usage: GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST,
});
}
}
// for a pass about to begin
pass_writes(label: string): GPURenderPassTimestampWrites | undefined {
if (!this.#readback || this.#labels.length >= MAX_PASSES) return undefined;
const i = this.#labels.length;
this.#labels.push(label);
return { querySet: this.#query_set!, beginningOfPassWriteIndex: i * 2, endOfPassWriteIndex: i * 2 + 1 };
}
// after the last pass ends, before the encoder finishes
resolve(encoder: GPUCommandEncoder) {
if (!this.#readback || this.#labels.length === 0) return;
const count = this.#labels.length * 2;
encoder.resolveQuerySet(this.#query_set!, 0, count, this.#resolve_buffer!, 0);
encoder.copyBufferToBuffer(this.#resolve_buffer!, 0, this.#readback, 0, count * 8);
}
// after the submit
read() {
const buffer = this.#readback;
if (!buffer) return;
const labels = this.#labels;
if (labels.length === 0) {
this.#free.push(buffer);
return;
}
buffer.mapAsync(GPUMapMode.READ, 0, labels.length * 16).then(() => {
const times = new BigUint64Array(buffer.getMappedRange(0, labels.length * 16));
const frame = new Map<string, number>();
labels.forEach((label, i) => {
const ms = Math.max(0, Number(times[i * 2 + 1] - times[i * 2])) / 1e6;
frame.set(label, (frame.get(label) ?? 0) + ms);
});
buffer.unmap();
this.#free.push(buffer);
this.#add_frame(frame);
}).catch(() => {
// the device was lost, nothing to time anymore
});
}
#add_frame(frame: Map<string, number>) {
for (const [label, ms] of frame) this.#sums.set(label, (this.#sums.get(label) ?? 0) + ms);
this.#frames += 1;
const now = performance.now();
if (now - this.#window_start < WINDOW_MS) return;
this.results = new Map([...this.#sums].map(([label, ms]) => [label, ms / this.#frames]));
this.total = [...this.results.values()].reduce((sum, ms) => sum + ms, 0);
this.#sums.clear();
this.#frames = 0;
this.#window_start = now;
}
}