196 lines
7.4 KiB
TypeScript
196 lines
7.4 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";
|
|
import { RenderBenchmark } from "$/client/benchmark/benchmark.ts";
|
|
import { format_report } from "$/client/benchmark/report.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;
|
|
// the last benchmark's, see client/benchmark/
|
|
#benchmark_report: string | undefined;
|
|
#benchmark_status = "";
|
|
|
|
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");
|
|
if (DebugUI.button("Run the render benchmark")) this.#start_benchmark(client);
|
|
if (this.#benchmark_report && DebugUI.button("Copy the benchmark's results")) this.#copy_benchmark();
|
|
if (this.#benchmark_status) DebugUI.text(this.#benchmark_status);
|
|
// 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`);
|
|
text(`entities: ${level.entities.size} loaded, ${level_renderer.drawn_entities} in view`);
|
|
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"));
|
|
copy_text(lines.join("\n")).catch(() => {});
|
|
}
|
|
}
|
|
|
|
#start_benchmark(client: Client) {
|
|
try {
|
|
client.benchmark = new RenderBenchmark(client.game_renderer, client.connection.mods, (result) => {
|
|
client.benchmark = undefined;
|
|
if (typeof result === "string") {
|
|
this.#benchmark_status = result;
|
|
return;
|
|
}
|
|
this.#benchmark_report = format_report(result);
|
|
console.log(this.#benchmark_report);
|
|
this.#copy_benchmark();
|
|
});
|
|
this.#benchmark_status = "";
|
|
} catch (e) {
|
|
this.#benchmark_status = e instanceof Error ? e.message : String(e);
|
|
}
|
|
}
|
|
|
|
// right after the benchmark, firefox and safari only let the page copy from a click
|
|
#copy_benchmark() {
|
|
copy_text(this.#benchmark_report!).then(
|
|
() => this.#benchmark_status = "The benchmark's results are on the clipboard",
|
|
() => this.#benchmark_status = "Click above to copy the benchmark's results",
|
|
);
|
|
}
|
|
|
|
// 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();
|
|
}
|
|
}
|
|
}
|
|
|
|
// pages that aren't https or localhost have no clipboard api, like a server on another computer, but the old way
|
|
// still copies from a click
|
|
function copy_text(text: string): Promise<void> {
|
|
if (navigator.clipboard) return navigator.clipboard.writeText(text);
|
|
const area = document.createElement("textarea");
|
|
area.value = text;
|
|
document.body.append(area);
|
|
area.select();
|
|
const copied = document.execCommand("copy");
|
|
area.remove();
|
|
return copied ? Promise.resolve() : Promise.reject(new Error("Couldn't copy"));
|
|
}
|