Benchmark
This commit is contained in:
+42
-1
@@ -21,6 +21,7 @@ import {
|
||||
init_font,
|
||||
init_window,
|
||||
load_texture,
|
||||
render_debug,
|
||||
resize_canvas,
|
||||
} from "./renderer/mod.ts";
|
||||
import { is_stopped, show_fatal_error } from "./fatal.ts";
|
||||
@@ -37,6 +38,10 @@ function confirm_leaving(confirm: boolean) {
|
||||
self.onbeforeunload = confirm ? (event) => event.preventDefault() : null;
|
||||
}
|
||||
|
||||
// with render_debug.uncapped, the most frames between two of the display's refreshes. it's the highest fps that shows,
|
||||
// 960 on a 60 hz display, and what the gpu can be behind by
|
||||
const UNCAPPED_FRAMES_PER_REFRESH = 16;
|
||||
|
||||
class ClientLoop {
|
||||
running = false;
|
||||
last_time = 0;
|
||||
@@ -53,6 +58,7 @@ class ClientLoop {
|
||||
}
|
||||
});
|
||||
|
||||
this.#next_task.port1.onmessage = () => this.loop(performance.now());
|
||||
this.running = true;
|
||||
const now = performance.now();
|
||||
this.last_time = now;
|
||||
@@ -84,6 +90,41 @@ class ClientLoop {
|
||||
console.log("Game started");
|
||||
}
|
||||
|
||||
// with render_debug.uncapped: frames since the display last refreshed, and whether the next one waits for it
|
||||
#frames_this_refresh = 0;
|
||||
#waiting_for_refresh = false;
|
||||
#refresh_requested = false;
|
||||
#next_task = new MessageChannel();
|
||||
|
||||
// with the display's refresh, or with render_debug.uncapped right away, so frames aren't held to vsync and show what
|
||||
// they cost. up to UNCAPPED_FRAMES_PER_REFRESH, then it waits for the next refresh, which the browser holds back
|
||||
// while the gpu is behind, so frames the gpu can't keep up with don't pile up. the browser saying a frame is done
|
||||
// can't be waited on instead, firefox only says so every 100 ms when nothing else is sent to the gpu
|
||||
#next_frame() {
|
||||
if (!render_debug.uncapped) {
|
||||
requestAnimationFrame((time) => this.loop(time));
|
||||
return;
|
||||
}
|
||||
if (!this.#refresh_requested) {
|
||||
this.#refresh_requested = true;
|
||||
requestAnimationFrame(() => {
|
||||
this.#refresh_requested = false;
|
||||
this.#frames_this_refresh = 0;
|
||||
if (this.#waiting_for_refresh) {
|
||||
this.#waiting_for_refresh = false;
|
||||
this.loop(performance.now());
|
||||
}
|
||||
});
|
||||
}
|
||||
this.#frames_this_refresh += 1;
|
||||
if (this.#frames_this_refresh < UNCAPPED_FRAMES_PER_REFRESH) {
|
||||
// a task of its own, so input and the workers' messages get handled in between
|
||||
this.#next_task.port2.postMessage(null);
|
||||
} else {
|
||||
this.#waiting_for_refresh = true;
|
||||
}
|
||||
}
|
||||
|
||||
loop(time: number) {
|
||||
if (!this.running || is_stopped()) {
|
||||
return;
|
||||
@@ -115,7 +156,7 @@ class ClientLoop {
|
||||
InputManager.update();
|
||||
|
||||
this.last_time = time;
|
||||
requestAnimationFrame((time) => this.loop(time));
|
||||
this.#next_frame();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user