Benchmark

This commit is contained in:
2026-10-28 11:42:58 -03:00
parent e5221ba681
commit 8fc08ab41e
15 changed files with 1356 additions and 41 deletions
+26 -6
View File
@@ -99,8 +99,9 @@ let pending_destroy: GPUBuffer[] = [];
// switches for finding out what a frame costs, see the f3 performance window. msaa is changed at the start of the next
// frame. split_passes gives each section (see gpu_section) passes of its own so the gpu times them apart, which costs
// a little since every pass loads and stores the render targets
export const render_debug = { msaa: false, split_passes: false };
// a little since every pass loads and stores the render targets. uncapped starts frames back to back instead of with
// the display's refresh, see the game loop in main.ts
export const render_debug = { msaa: false, split_passes: false, uncapped: false };
// what was drawn, for the last whole frame
export interface RenderStats {
draw_calls: number;
@@ -111,6 +112,11 @@ const empty_stats = (): RenderStats => ({ draw_calls: 0, passes: 0, quads: { sol
export let render_stats = empty_stats();
let frame_stats = empty_stats();
export let gpu_timer: GpuTimer | undefined;
// what the gpu is, for the benchmark's report
export let adapter_info: GPUAdapterInfo | undefined;
// see set_canvas_size
let fixed_canvas_size: { width: number; height: number } | undefined;
let resize_pending = false;
// what the passes starting now are timed as
let gpu_label = "other";
@@ -289,6 +295,7 @@ export async function init_window(canvas_element: HTMLCanvasElement) {
if (!adapter) {
throw new Error("No WebGPU adapter found");
}
adapter_info = adapter.info;
device = await adapter.requestDevice({
requiredFeatures: adapter.features.has("timestamp-query") ? ["timestamp-query"] : [],
});
@@ -365,6 +372,10 @@ export function update_lightmap(daylight = 1) {
}
export function begin_drawing() {
if (resize_pending) {
resize_pending = false;
resize_canvas();
}
const wanted_samples = render_debug.msaa ? SAMPLE_COUNT : 1;
if (wanted_samples !== sample_count) {
sample_count = wanted_samples;
@@ -644,16 +655,25 @@ export function destroy_buffer(buffer: GPUBuffer) {
}
export function resize_canvas() {
const width = self.innerWidth;
const height = self.innerHeight;
const width = fixed_canvas_size?.width ?? self.innerWidth;
const height = fixed_canvas_size?.height ?? self.innerHeight;
// a fixed size is shown as big as it fits in the window
const scale = Math.min(self.innerWidth / width, self.innerHeight / height);
if (canvas.width !== width || canvas.height !== height) {
canvas.width = width;
canvas.height = height;
canvas.style.width = canvas.width + "px";
canvas.style.height = canvas.height + "px";
// render targets get recreated at the start of the next frame
}
canvas.style.width = width * scale + "px";
canvas.style.height = height * scale + "px";
}
// frames are drawn at this size whatever the window's, like the benchmark's, or at the window's again when it's
// undefined. it changes at the start of the next frame, since resizing the canvas takes away the texture being drawn to
export function set_canvas_size(size: { width: number; height: number } | undefined) {
fixed_canvas_size = size;
resize_pending = true;
}
export function get_current_texture(): GPUTexture | null {
+7
View File
@@ -17,6 +17,9 @@ export class GpuTimer {
// ms per frame by label, averaged over the last window
results = new Map<string, number>();
total = 0;
// every timed frame's total, while something reads them, like the benchmark. a frame goes to the array that was
// here when it was submitted, however late its time comes back
samples: number[] | undefined;
#device: GPUDevice;
#query_set: GPUQuerySet | undefined;
@@ -82,16 +85,20 @@ export class GpuTimer {
this.#free.push(buffer);
return;
}
const samples = this.samples;
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>();
let total = 0;
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);
total += ms;
});
buffer.unmap();
this.#free.push(buffer);
this.#add_frame(frame);
samples?.push(total);
}).catch(() => {
// the device was lost, nothing to time anymore
});