Split The code is public; the app answers only to the crew.

Hold 60 fps on a laptop and a phone: adapt the render scale to the frame time #14

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

vesper/cs:vesper/12-hold-60-fps-on-a-laptop-and-a-phone-adap → main 1 commit fork

  • Merged. vesper/12-hold-60-fps-on-a-laptop-and-a-phone-adap was merged into main as 1b0a485.

Review

Sign in to review this change.
7 files changed +268 −64
README.md +8 −1
@@ -66,12 +66,19 @@ the scoreboard, <kbd>Esc</kbd> pauses. On a touch screen the left of the view
6666 is a thumb stick, the right looks around, and the buttons along the bottom do
6767 the rest.
6868
69 The counter in the corner of the view is the measured frame time. When a
70 machine cannot hold 60 fps the scene is drawn into a smaller buffer — half the
71 device pixel ratio at the worst, stepped at most twice a second, the HUD
72 text still at full resolution — and the counter says what fraction of the
73 pixels you are getting. Nothing on the page moves when it changes.
74
6975 ## Shape
7076
7177 - `src/client/` — the game, in modules named for one thing each: `math`, `map`,
7278 `physics`, `weapons`, `combat`, `match` (the rules), `world` (the simulation),
7379 `bots`, `observe`, `action`, `policy`, `weights`, `models`, `textures`,
74 `press` (the input latch), `renderer`, `hud`, `input`, `audio`, `main`.
80 `press` (the input latch), `quality` (the render scale the frame time buys),
81 `renderer`, `hud`, `input`, `audio`, `main`.
7582 Everything except the last five is headless and tested.
7683 - `src/rl/` — training, which the game never imports: `arena` is the environment
7784 (a real duel on de_forge with the round and the economy taken out) and `ppo` is
src/client/main.ts +29 −1
@@ -11,6 +11,7 @@ import { drawHud } from "./hud.ts";
1111 import { attachButtons, createControls } from "./input.ts";
1212 import { clamp } from "./math.ts";
1313 import { COMPETITIVE, SHORT, type Rules } from "./match.ts";
14 import { MAX_SCALE, nextRenderScale } from "./quality.ts";
1415 import { createRenderer, type Camera } from "./renderer.ts";
1516 import { buyable } from "./weapons.ts";
1617 import {
@@ -19,6 +20,15 @@ import {
1920
2021 const TICK = 1 / 64;
2122
23 /**
24 * How often the render scale is allowed to move. Twice a second is soon
25 * enough to catch a machine falling off 60 fps and slow enough that the
26 * smoothed average has mostly caught up with the last step — a decision made
27 * on an average that still remembers the old buffer is how a scaler
28 * overshoots.
29 */
30 const SCALE_EVERY_MS = 500;
31
2232 const $ = <T extends HTMLElement>(id: string): T => document.getElementById(id) as T;
2333
2434 function boot(): void {
@@ -49,6 +59,8 @@ function boot(): void {
4959 let running = false;
5060 let lastSound = 0;
5161 let frameAverage = 16.7;
62 let renderScale = MAX_SCALE;
63 let scaledAt = 0;
5264 let posted = -1;
5365
5466 const show = (element: HTMLElement, on: boolean): void => {
@@ -241,8 +253,20 @@ function boot(): void {
241253 pitch: view.pitch - view.punch[1] * 0.4,
242254 fov: view.scoped ? 40 : 90,
243255 };
256 // Pixels are the cheapest thing to give up, so give them up on a timer and
257 // from the smoothed average — never from the one frame that just ran long.
258 if (now - scaledAt >= SCALE_EVERY_MS) {
259 scaledAt = now;
260 const next = nextRenderScale(renderScale, frameAverage);
261 if (next !== renderScale) {
262 renderScale = next;
263 renderer.setScale(next);
264 }
265 }
244266 renderer.draw(world, camera, elapsed);
245267
268 // The HUD keeps the full device ratio whatever the scene costs: text that
269 // goes soft reads as a broken page, and 2D text is not what is slow.
246270 const dpr = Math.min(window.devicePixelRatio || 1, 2);
247271 const w = Math.round(hudCanvas.clientWidth * dpr);
248272 const h = Math.round(hudCanvas.clientHeight * dpr);
@@ -251,7 +275,11 @@ function boot(): void {
251275 hudCanvas.height = h;
252276 }
253277 drawHud(hudCtx, world, w, h, controls.scoreboard);
254 $("fps").textContent = `${(1000 / Math.max(1, frameAverage)).toFixed(0)} fps · ${frameAverage.toFixed(1)} ms`;
278 // The fps is the frame time and nothing else; the scale is an aside, shown
279 // only when it is costing you pixels, so a slow machine says why.
280 const note = renderScale < MAX_SCALE ? ` · ${Math.round(renderScale * 100)}% res` : "";
281 $("fps").textContent =
282 `${(1000 / Math.max(1, frameAverage)).toFixed(0)} fps · ${frameAverage.toFixed(1)} ms${note}`;
255283 }
256284 requestAnimationFrame(frame);
257285 }
src/client/quality.test.ts added +91
@@ -0,0 +1,91 @@
1 import { expect, test } from "bun:test";
2 import {
3 FAST_MS, MAX_SCALE, MIN_SCALE, nextRenderScale, SLOW_MS, TARGET_MS,
4 } from "./quality.ts";
5
6 /** Run the decision `frames` times at one frame time, as the loop would. */
7 const settle = (scale: number, frameMs: number, frames: number): number => {
8 let out = scale;
9 for (let i = 0; i < frames; i += 1) out = nextRenderScale(out, frameMs);
10 return out;
11 };
12
13 test("a long frame lowers the scale", () => {
14 expect(nextRenderScale(1, 33)).toBeLessThan(1);
15 expect(nextRenderScale(0.8, SLOW_MS + 1)).toBeLessThan(0.8);
16 });
17
18 test("a short frame raises it again", () => {
19 expect(nextRenderScale(0.6, 8)).toBeGreaterThan(0.6);
20 expect(nextRenderScale(0.6, FAST_MS - 0.1)).toBeGreaterThan(0.6);
21 });
22
23 test("a frame time at the target leaves it alone", () => {
24 expect(nextRenderScale(0.7, TARGET_MS)).toBe(0.7);
25 expect(nextRenderScale(1, TARGET_MS)).toBe(1);
26 });
27
28 test("the dead band holds from its low edge to its high one, target included", () => {
29 for (const ms of [FAST_MS, TARGET_MS * 0.95, TARGET_MS, TARGET_MS * 1.1, SLOW_MS]) {
30 expect(nextRenderScale(0.7, ms)).toBe(0.7);
31 }
32 expect(FAST_MS).toBeLessThan(TARGET_MS);
33 expect(SLOW_MS).toBeGreaterThan(TARGET_MS);
34 });
35
36 test("a display locked to 60 Hz reads as the target and is left where it is", () => {
37 // 16.67 ms is both "exactly fast enough" and "all the headroom I can see".
38 expect(nextRenderScale(0.6, 1000 / 60)).toBe(0.6);
39 expect(nextRenderScale(0.6, 1000 / 30)).toBeLessThan(0.6);
40 expect(nextRenderScale(0.6, 1000 / 120)).toBeGreaterThan(0.6);
41 });
42
43 test("it never leaves the clamp, however long the machine stays slow or fast", () => {
44 expect(settle(1, 60, 40)).toBe(MIN_SCALE);
45 expect(settle(MIN_SCALE, 120, 10)).toBe(MIN_SCALE);
46 expect(settle(MIN_SCALE, 4, 100)).toBe(MAX_SCALE);
47 expect(settle(1, 4, 10)).toBe(MAX_SCALE);
48 });
49
50 test("a scale out of range or a frame time that is not one is repaired, not obeyed", () => {
51 expect(nextRenderScale(3, TARGET_MS)).toBe(MAX_SCALE);
52 expect(nextRenderScale(0.01, TARGET_MS)).toBe(MIN_SCALE);
53 expect(nextRenderScale(Number.NaN, TARGET_MS)).toBe(MAX_SCALE);
54 for (const ms of [Number.NaN, 0, -5, Number.POSITIVE_INFINITY]) {
55 expect(nextRenderScale(0.75, ms)).toBe(0.75);
56 }
57 });
58
59 test("falling is faster than climbing, so a spike costs pixels before it costs frames", () => {
60 const dropped = 1 - nextRenderScale(1, 40);
61 const gained = nextRenderScale(0.5, 4) - 0.5;
62 expect(dropped).toBeGreaterThan(gained);
63 });
64
65 test("the steps stay on a coarse grid instead of drifting", () => {
66 let scale = 1;
67 for (let i = 0; i < 5; i += 1) {
68 scale = nextRenderScale(scale, 40);
69 expect(scale * 100).toBeCloseTo(Math.round(scale * 100), 9);
70 }
71 expect(scale).toBe(0.5);
72 });
73
74 test("a machine that misses vsync at full scale lands between the clamps, not on one", () => {
75 // Pixels scale with the square of the scale, so a frame time that follows
76 // the pixel count is the honest toy: at scale 1 it takes 24 ms, and the
77 // loop should find the largest scale that keeps it under the band.
78 const frameMs = (scale: number): number => 6 + 18 * scale * scale;
79 let scale = MAX_SCALE;
80 for (let i = 0; i < 60; i += 1) scale = nextRenderScale(scale, frameMs(scale));
81 expect(frameMs(scale)).toBeLessThanOrEqual(SLOW_MS);
82 expect(scale).toBeGreaterThan(MIN_SCALE);
83 expect(scale).toBeLessThan(MAX_SCALE);
84 // And having found it, it stays: the dead band is a fixed point, not a corner
85 // the scale walks back and forth across.
86 const settled = scale;
87 for (let i = 0; i < 20; i += 1) {
88 scale = nextRenderScale(scale, frameMs(scale));
89 expect(scale).toBe(settled);
90 }
91 });
src/client/quality.ts added +64
@@ -0,0 +1,64 @@
1 /**
2 * Quality: how many pixels the next second of frames is worth.
3 *
4 * The fragment shader does a texture-array fetch and a fog term for every
5 * pixel it draws, so the drawing buffer is the knob that costs the most and
6 * hurts the least. A phone at devicePixelRatio 3 asks for four times the
7 * pixels of a laptop at 1.5 for the same CSS size; rendering all of them and
8 * landing at 30 fps is a worse picture than rendering half of them at 60.
9 *
10 * So the scale is a decision made from the frame time the loop already
11 * measures. The rules that keep it from hunting:
12 *
13 * - A dead band around the target, wide enough that a frame time sitting on
14 * 60 fps changes nothing. Above `SLOW_MS` frames are being missed and the
15 * scale steps down; below `FAST_MS` there is measured headroom and it steps
16 * back up; in between nothing happens.
17 * - The honest cost of that band: a display locked to 60 Hz reports one vsync
18 * period however early the frame finished, so a machine that stepped down
19 * and then got its headroom back cannot prove it and keeps the smaller
20 * buffer. The climb back is for the machines that can show it — a 120 Hz
21 * phone, a compositor that is not throttling us — and for the case that
22 * matters more anyway, which is falling off 60 fps and not staying there.
23 * - Asymmetric steps. Down is twice as big as up: losing frames is worse than
24 * losing pixels, and a slow climb back is a smaller wobble than a fast one.
25 * - A clamp. Never below half the device ratio (the picture would be mush)
26 * and never above it (the buffer is already as crisp as the screen).
27 *
28 * The rate limit lives in the caller: this is a pure function of the current
29 * scale and a smoothed frame time, and the loop calls it a few times a second
30 * so that a step is a step and not a stutter.
31 */
32
33 /** The frame time 60 fps asks for, in milliseconds. */
34 export const TARGET_MS = 1000 / 60;
35
36 /** Above this the frames are late: step down. */
37 export const SLOW_MS = TARGET_MS * 1.25;
38
39 /** Below this there is measured room to spend: step up. */
40 export const FAST_MS = TARGET_MS * 0.9;
41
42 /** Fraction of the capped device pixel ratio. Never collapses, never oversamples. */
43 export const MIN_SCALE = 0.5;
44 export const MAX_SCALE = 1;
45
46 const DOWN_STEP = 0.1;
47 const UP_STEP = 0.05;
48
49 /** Keep the scale on a coarse grid so float drift cannot invent a new one. */
50 const quantize = (scale: number): number =>
51 Math.min(MAX_SCALE, Math.max(MIN_SCALE, Math.round(scale * 100) / 100));
52
53 /**
54 * The scale the next frames should render at, given the one they render at
55 * now and the smoothed frame time they have been taking. A frame time that is
56 * neither a number nor a duration changes nothing.
57 */
58 export function nextRenderScale(scale: number, frameMs: number): number {
59 const current = Number.isFinite(scale) ? quantize(scale) : MAX_SCALE;
60 if (!Number.isFinite(frameMs) || frameMs <= 0) return current;
61 if (frameMs > SLOW_MS) return quantize(current - DOWN_STEP);
62 if (frameMs < FAST_MS) return quantize(current + UP_STEP);
63 return current;
64 }
src/client/renderer.ts +18 −4
@@ -7,6 +7,7 @@
77
88 import { multiply, perspective, viewMatrix, modelMatrix, type Vec3 } from "./math.ts";
99 import { bombMesh, levelMesh, playerMesh, viewModelMesh, type Mesh } from "./models.ts";
10 import { MAX_SCALE, MIN_SCALE } from "./quality.ts";
1011 import { LAYERS, TEX_SIZE, materialTextures } from "./textures.ts";
1112 import type { World } from "./world.ts";
1213 import { hullHeight } from "./physics.ts";
@@ -132,6 +133,12 @@ const LINE_CAPACITY = 4096; // vertices
132133 export type Renderer = {
133134 draw(world: World, camera: Camera, dt: number): void;
134135 resize(): void;
136 /**
137 * The fraction of the device pixel ratio the drawing buffer uses. `quality`
138 * decides it from the frame time; the CSS size of the canvas never moves,
139 * so nothing on the page reflows when it changes.
140 */
141 setScale(scale: number): void;
135142 readonly gl: WebGL2RenderingContext;
136143 };
137144
@@ -188,16 +195,23 @@ export function createRenderer(canvas: HTMLCanvasElement): Renderer | null {
188195 gl.cullFace(gl.BACK);
189196 gl.clearColor(SKY[0], SKY[1], SKY[2], 1);
190197
198 let scale = MAX_SCALE;
199
191200 const resize = (): void => {
192 const dpr = Math.min(window.devicePixelRatio || 1, 2);
193 const width = Math.round(canvas.clientWidth * dpr);
194 const height = Math.round(canvas.clientHeight * dpr);
201 const dpr = Math.min(window.devicePixelRatio || 1, 2) * scale;
202 const width = Math.max(1, Math.round(canvas.clientWidth * dpr));
203 const height = Math.max(1, Math.round(canvas.clientHeight * dpr));
195204 if (canvas.width !== width || canvas.height !== height) {
196205 canvas.width = width;
197206 canvas.height = height;
198207 }
199208 };
200209
210 const setScale = (next: number): void => {
211 if (!Number.isFinite(next)) return;
212 scale = Math.min(MAX_SCALE, Math.max(MIN_SCALE, next));
213 };
214
201215 let bobPhase = 0;
202216
203217 const draw = (world: World, camera: Camera, dt: number): void => {
@@ -325,5 +339,5 @@ export function createRenderer(canvas: HTMLCanvasElement): Renderer | null {
325339 gl.bindVertexArray(null);
326340 };
327341
328 return { draw, resize, gl };
342 return { draw, resize, setScale, gl };
329343 }
static/app.285984cb3a.js added +58

Diff not shown — a line 56,219 characters wide (limit 2,000). +58

View file
static/app.baf3356f9f.js deleted −58

Diff not shown — a line 55,944 characters wide (limit 2,000). −58

View file

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs