124 lines
4.5 KiB
TypeScript
124 lines
4.5 KiB
TypeScript
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";
|
|
|
|
// f3: every entity's fields, editable, and what frames cost
|
|
export class DebugOverlay {
|
|
render(client: Client) {
|
|
this.#render_performance(client);
|
|
|
|
DebugUI.begin("Entities", 10, 10, 300);
|
|
DebugUI.text(`${format_clock(client.level.time)} (tick ${client.level.time})`);
|
|
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;
|
|
let meshed = 0;
|
|
let waiting = 0;
|
|
let bytes = 0;
|
|
for (const chunk of level.chunks.values()) {
|
|
const meshes = Object.values(chunk.meshes);
|
|
if (meshes.length > 0) meshed += 1;
|
|
if (chunk.dirty) waiting += 1;
|
|
for (const mesh of meshes) bytes += mesh.vertex_buffer.size + (mesh.index_buffer?.size ?? 0);
|
|
}
|
|
text(`chunks: ${level.chunks.size} loaded, ${meshed} meshed, ${level_renderer.visible_chunks} in view`);
|
|
text(` ${waiting} 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(() => {});
|
|
}
|
|
}
|
|
|
|
// 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();
|
|
}
|
|
}
|
|
}
|