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

148 lines
5.5 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
import type { Client } from "$/client/client.ts";
import { DebugUI } from "$/client/debug_ui.ts";
import { format_clock } from "$/common/time.ts";
import { profiler, type SectionStats } from "$/client/profiler.ts";
import { canvas, gpu_timer, render_debug, render_stats } from "$/client/renderer/mod.ts";
import { RENDER_LAYERS } from "$/common/everything_registry.ts";
import { atlas_png } from "$/client/atlas.ts";
// how often the totals that go through every chunk are worked out again, so the overlay doesn't slow the frames it's
// measuring
const CHUNK_STATS_MS = 1000;
// f3: every entity's fields, editable, and what frames cost
export class DebugOverlay {
#chunk_stats = { meshed: 0, bytes: 0 };
#chunk_stats_time = -Infinity;
render(client: Client) {
this.#render_performance(client);
DebugUI.begin("Entities", 10, 10, 300);
DebugUI.text(`${format_clock(client.level.time)} (tick ${client.level.time})`);
if (DebugUI.button("Copy texture atlas")) {
// the promise goes in as is, so the copy still counts as coming from the click while the png encodes
const png = atlas_png();
if (png) navigator.clipboard?.write([new ClipboardItem({ "image/png": png })]).catch(() => {});
}
DebugUI.separator();
for (const entity of client.level.entities.values()) {
if (DebugUI.collapsing_header(`${entity.constructor.name} - ${entity.id}`)) {
this.#render_fields(entity);
}
}
if (DebugUI.collapsing_header("Camera")) {
this.#render_fields(client.camera);
}
if (DebugUI.collapsing_header("Options")) {
this.#render_fields(client.options);
}
DebugUI.end();
}
// where a frame's time goes. if the time between frames is much more than the javascript takes, the frame is
// waiting on the gpu (or vsync), and the gpu times say what it's doing
#render_performance(client: Client) {
const ms = (value: number) => value.toFixed(2);
const stats = (name: string, { avg, max }: SectionStats) => `${name}: ${ms(avg)} ms (max ${ms(max)})`;
const { interval, frame } = profiler;
DebugUI.begin("Performance", 320, 10, 340, 600);
const copy = DebugUI.button("Copy");
// what's shown, for the copy button
const lines: string[] = [];
const text = (line: string) => {
lines.push(line);
DebugUI.text(line);
};
const separator = () => {
lines.push("");
DebugUI.separator();
};
const msaa = render_debug.msaa ? "4x" : "off";
text(`${interval.fps.toFixed(0)} fps, ${canvas.width}x${canvas.height}, msaa ${msaa}`);
text(stats("between frames", interval));
text(stats("javascript", frame));
text(`waiting: ${ms(Math.max(0, interval.avg - frame.avg))} ms`);
separator();
text("cpu per frame");
for (const [name, section] of profiler.cpu) text(stats(` ${name}`, section));
separator();
if (!gpu_timer?.supported) {
text("gpu: no timestamp-query on this device");
} else {
text(`gpu per frame: ${ms(gpu_timer.total)} ms`);
if (!render_debug.split_passes) text(" (split_passes times each section)");
for (const [label, time] of gpu_timer.results) text(` ${label}: ${ms(time)} ms`);
}
separator();
const { draw_calls, passes, quads } = render_stats;
text(`draw calls: ${draw_calls}, passes: ${passes}`);
for (const layer of RENDER_LAYERS) text(` ${layer}: ${quads[layer].toLocaleString()} quads`);
const level = client.level;
const level_renderer = client.game_renderer.level_renderer;
const { meshed, bytes } = this.#count_chunks(client);
text(`chunks: ${level.chunks.size} loaded, ${meshed} meshed, ${level_renderer.visible_chunks} in view`);
text(` ${level.dirty_count} waiting to mesh, ${level.meshes_in_flight} meshing`);
text(` meshes: ${(bytes / 1024 / 1024).toFixed(1)} MB`);
separator();
text("switches");
this.#render_fields(render_debug);
this.#render_fields(level_renderer.draw);
DebugUI.end();
if (copy) {
const switches = { ...render_debug, ...level_renderer.draw };
lines.push(Object.entries(switches).map(([key, on]) => ` ${key}: ${on}`).join("\n"));
navigator.clipboard?.writeText(lines.join("\n")).catch(() => {});
}
}
// meshed chunks and what their meshes take on the gpu, once a second
#count_chunks(client: Client) {
const now = performance.now();
if (now - this.#chunk_stats_time < CHUNK_STATS_MS) return this.#chunk_stats;
this.#chunk_stats_time = now;
let meshed = 0;
let bytes = 0;
for (const chunk of client.level.chunks.values()) {
const meshes = Object.values(chunk.meshes);
if (meshes.length > 0) meshed += 1;
for (const mesh of meshes) bytes += mesh.vertex_buffer.size + (mesh.index_buffer?.size ?? 0);
}
this.#chunk_stats = { meshed, bytes };
return this.#chunk_stats;
}
// deno-lint-ignore no-explicit-any
#render_fields(object: any) {
for (const key in object) {
const value = object[key];
if (typeof value === "number") {
object[key] = DebugUI.float_input(key, value);
} else if (typeof value === "string") {
object[key] = DebugUI.text_input(key, value);
} else if (typeof value === "boolean") {
object[key] = DebugUI.checkbox(key, value);
} else if (Array.isArray(value)) {
DebugUI.text(`${key}: ${JSON.stringify(value.slice(0, 10))}`);
} else if (value && typeof value === "object" && value.constructor !== Object) {
// other objects like the level point back at this one, only name them
DebugUI.text(`${key}: ${value.constructor.name}`);
} else {
DebugUI.text(`${key}: ${JSON.stringify(value)}`);
}
DebugUI.separator();
}
}
}