Make benchmark work on native

This commit is contained in:
2026-11-23 20:01:25 -03:00
parent 90dd86b336
commit ced27d3400
5 changed files with 21 additions and 17 deletions
+9 -10
View File
@@ -13,6 +13,7 @@ import type { GameRenderer } from "@/client/rendering/game_renderer.ts";
import { block_visuals } from "@/client/rendering/visuals.ts"; import { block_visuals } from "@/client/rendering/visuals.ts";
import { draw_item } from "@/client/rendering/render_utils.ts"; import { draw_item } from "@/client/rendering/render_utils.ts";
import { InputManager } from "@/client/input_manager.ts"; import { InputManager } from "@/client/input_manager.ts";
import { platform } from "@/client/platform.ts";
import { type FrameSample, profiler } from "@/client/profiler.ts"; import { type FrameSample, profiler } from "@/client/profiler.ts";
import { RemotePlayer } from "@/client/entity/remote_player.ts"; import { RemotePlayer } from "@/client/entity/remote_player.ts";
import { RemoteEntity } from "@/client/entity/remote_entity.ts"; import { RemoteEntity } from "@/client/entity/remote_entity.ts";
@@ -129,8 +130,9 @@ export class RenderBenchmark {
// a line of names for each row of the gui scene's slots // a line of names for each row of the gui scene's slots
#gui_lines: string[]; #gui_lines: string[];
#system: SystemInfo; #system: SystemInfo;
// the page was hidden, which stops frames // the game was hidden, which held frames back
#hidden = false; #hidden = false;
#stop_watching_shown: () => void;
#saved_debug = { ...render_debug }; #saved_debug = { ...render_debug };
#saved_draw: Record<string, boolean>; #saved_draw: Record<string, boolean>;
@@ -157,7 +159,7 @@ export class RenderBenchmark {
for (const key of Object.keys(draw) as (keyof typeof draw)[]) draw[key] = true; for (const key of Object.keys(draw) as (keyof typeof draw)[]) draw[key] = true;
Object.assign(render_debug, { msaa: false, split_passes: false, uncapped: false }); Object.assign(render_debug, { msaa: false, split_passes: false, uncapped: false });
set_canvas_size(RESOLUTION); set_canvas_size(RESOLUTION);
document.addEventListener("visibilitychange", this.#on_visibility); this.#stop_watching_shown = platform.on_shown(() => this.#hidden = true);
this.#flow = this.#run(); this.#flow = this.#run();
} }
@@ -167,7 +169,7 @@ export class RenderBenchmark {
if (InputManager.is_key_pressed("Escape")) { if (InputManager.is_key_pressed("Escape")) {
this.#end("The benchmark was stopped"); this.#end("The benchmark was stopped");
} else if (this.#hidden) { } else if (this.#hidden) {
this.#end("The benchmark stopped since the page was hidden, which holds frames back"); this.#end("The benchmark stopped since the game was hidden, which holds frames back");
} else { } else {
this.#flow.next(); this.#flow.next();
} }
@@ -216,14 +218,10 @@ export class RenderBenchmark {
set_canvas_size(undefined); set_canvas_size(undefined);
profiler.samples = undefined; profiler.samples = undefined;
if (gpu_timer) gpu_timer.samples = undefined; if (gpu_timer) gpu_timer.samples = undefined;
document.removeEventListener("visibilitychange", this.#on_visibility); this.#stop_watching_shown();
this.#on_end(result); this.#on_end(result);
} }
#on_visibility = () => {
if (document.hidden) this.#hidden = true;
};
// the whole benchmark, a frame at each yield // the whole benchmark, a frame at each yield
*#run(): Generator<void, void> { *#run(): Generator<void, void> {
yield* this.#build(); yield* this.#build();
@@ -559,8 +557,9 @@ function system_info(workers: number): SystemInfo {
gpu_features: [...device.features].sort(), gpu_features: [...device.features].sort(),
timestamps: gpu_timer?.supported ?? false, timestamps: gpu_timer?.supported ?? false,
canvas_format: navigator.gpu.getPreferredCanvasFormat(), canvas_format: navigator.gpu.getPreferredCanvasFormat(),
device_pixel_ratio: devicePixelRatio, // the desktop app's window is drawn a pixel to a pixel
screen: `${screen.width}x${screen.height}`, device_pixel_ratio: globalThis.devicePixelRatio ?? 1,
screen: globalThis.screen && `${screen.width}x${screen.height}`,
workers, workers,
}; };
const hints = (navigator as { userAgentData?: UserAgentData }).userAgentData; const hints = (navigator as { userAgentData?: UserAgentData }).userAgentData;
+2 -1
View File
@@ -62,7 +62,8 @@ export interface SystemInfo {
// the median fps where nothing is drawn, which is the display's refresh when the browser holds frames to it // the median fps where nothing is drawn, which is the display's refresh when the browser holds frames to it
refresh_hz?: number; refresh_hz?: number;
device_pixel_ratio: number; device_pixel_ratio: number;
screen: string; // the browser's, the desktop app doesn't know it
screen?: string;
// meshing workers, see ChunkWorkerPool // meshing workers, see ChunkWorkerPool
workers: number; workers: number;
} }
+4 -2
View File
@@ -92,9 +92,11 @@ export const browser_platform: Platform = {
return localStorage; return localStorage;
}, },
on_shown(shown) { on_shown(shown) {
document.addEventListener("visibilitychange", () => { const listener = () => {
if (!document.hidden) shown(); if (!document.hidden) shown();
}); };
document.addEventListener("visibilitychange", listener);
return () => document.removeEventListener("visibilitychange", listener);
}, },
// sprinting is ctrl and walking is w, and ctrl+w closes the tab without the page getting a say. in fullscreen // sprinting is ctrl and walking is w, and ctrl+w closes the tab without the page getting a say. in fullscreen
// the keyboard lock catches it instead (see browser_input.ts) // the keyboard lock catches it instead (see browser_input.ts)
+3 -2
View File
@@ -30,7 +30,7 @@ let lost_mouse = false;
let shown = true; let shown = true;
let next_frame: ((time: number) => void) | undefined; let next_frame: ((time: number) => void) | undefined;
let frame_waiting = false; let frame_waiting = false;
const shown_listeners: (() => void)[] = []; const shown_listeners = new Set<() => void>();
function run_frame() { function run_frame() {
frame_waiting = false; frame_waiting = false;
@@ -78,7 +78,8 @@ function native_platform(page: URL, stored: Record<string, string>, credits: str
}, },
request_frame, request_frame,
on_shown(listener) { on_shown(listener) {
shown_listeners.push(listener); shown_listeners.add(listener);
return () => shown_listeners.delete(listener);
}, },
// kept by the window, which saves it with the app's data // kept by the window, which saves it with the app's data
storage: { storage: {
+3 -2
View File
@@ -14,8 +14,9 @@ export interface Platform {
restart(url: URL): void; restart(url: URL): void;
// calls frame once it's time for the next frame, with the time like performance.now() // calls frame once it's time for the next frame, with the time like performance.now()
request_frame(frame: (time: number) => void): void; request_frame(frame: (time: number) => void): void;
// calls shown when the game can be seen again after being hidden, there are no frames meanwhile // calls shown when the game can be seen again after being hidden, there are no frames meanwhile. returns what
on_shown(shown: () => void): void; // stops calling it
on_shown(shown: () => void): () => void;
// what's remembered from one time to the next, like the servers whose mods the player trusts. it can throw, like a // what's remembered from one time to the next, like the servers whose mods the player trusts. it can throw, like a
// browser's in a private window // browser's in a private window
storage: Pick<Storage, "getItem" | "setItem">; storage: Pick<Storage, "getItem" | "setItem">;