Dark Matters: A Quality Governor That Asks Which Resource Is Short
The Dark Matters engine's automatic quality governor now asks which resource is short
before it lowers anything. During a duel on a GPU that another program is saturating, it drops
the render resolution, one cheap step at a time, while shadows and lighting stay exactly where
the player set them. And the packaged demo now writes one [perf] line to its own log at most
every 30 seconds: average fps, 1% low, worst frame, long frames and the governor's level.
Two rules sit under both changes. A lever that helps a GPU-bound frame costs a CPU-bound one, so the governor has to know which kind of frame it is looking at. And a game that cannot report its own frame rate cannot be tuned from a player's session, so the demo reports it.
Two ladders, and what each step costs
The governor lives in engine/src/web/assets/ui/world3d/gfxPresetCore.mjs
(createAutoGovernor). It is pure: the page pushes one frame time, the wall clock and the
frame's own main-thread work in milliseconds, and it answers with at most one decision. It
holds 60 fps: a median frame over 17.54 ms for 5 seconds earns one step down, a median under
13.33 ms for 10 seconds earns one step back up.
It keeps two ladders:
| Ladder | Steps, in order | What a step does |
|---|---|---|
| Picture (GPU) | scale75, scale50, shadows, lighting | resolution scale, then shadow maps off, then the simple lighting chain |
| Simulation (CPU) | cloth, anim, physcap | fewer cloth simulations, distant characters posed at 15 Hz, one physics step per frame |
A frame counts as CPU-bound when its main-thread work is at least 80% of the frame time
(cpuShare: 0.8). A CPU-bound frame steps the simulation ladder first, because a picture step
buys it nothing.
The two kinds of picture step also cost very different things. A resolution step is one
setPixelRatio call: the renderer draws fewer pixels and no shader changes. A shadows or
lighting step changes the defines every lit material is compiled with, so every lit shader
program relinks. Measured in a soak run, the shadows step alone linked 72 programs and cost
frames of one to two seconds.
The duel hold, step by step
A duel is the part of the game where a frame of one to two seconds is least acceptable, and it
is normally main-thread bound. So the governor holds its picture ladder while a duel is on
(gpuHeld). The page passes the duel state in when it mounts the governor:
const gov = C.createAutoGovernor({ pixel: () => !!(S.pixelLook && S.pixelLook.mode !== "off"), gpuHeld: duelOn });
The hold is for the steps that relink. On a machine where another program has the GPU at 99%, a duel's frame is GPU-starved, not main-thread bound, and a hold over the whole ladder would keep it at full resolution. So while held, the governor looks at the step it would take next and at the kind of frame in front of it:
const isScale = (s) => s === "scale75" || s === "scale50";
const nextDown = () => { let k = level; while (k < STEPS.length && skip(STEPS[k])) k++; return k < STEPS.length ? STEPS[k] : null; };
// ...
if ((!held || (!cpuBound && isScale(nextDown()))) && slowSince >= 0 && t - slowSince >= cfg.slowFor
&& t - lastStepAt >= cfg.stepEvery && level < STEPS.length) {
The simulation ladder's branch runs just before this one and does not read held, so a
CPU-bound duel still sheds cloth, animation and physics work. Raising is gated the same way: while held, a step back up is allowed when the step being undone
is a resolution step (isScale(STEPS[level - 1])), so the picture sharpens again when the GPU
frees up mid-duel.
The complete decision, for a frame that has been slow long enough to earn a step:
| Frame | Next picture step | Outside a duel | In a duel |
|---|---|---|---|
| CPU-bound | any | simulation step first; picture steps once that ladder is spent | simulation step; picture held |
| not CPU-bound | resolution (scale75, scale50) | moves | moves |
| not CPU-bound | shadows or lighting | moves | held |
| pixel look on | resolution | skipped (the pixel look draws on its own fixed grid) | skipped, so nothing moves |
One edge belongs in the open: a frame with no main-thread reading counts as not CPU-bound,
because cpuBound needs at least four CPU samples in the window. On a slow GPU in a duel, that
reads as a GPU-bound frame and lets resolution move, which is the cheap direction to be wrong in.
The play report in the demo log
The second change gives the packaged demo a measurement it can carry home. The page keeps a
window of play frame times in createPerfWindow, a fixed histogram of 0.5 ms bins up to
500 ms with one overflow bin. Pushing a frame is constant time and allocates nothing:
push(ms) {
if (!(ms > 0) || ms === Infinity) return;
n++; sum += ms; if (ms > worst) worst = ms; if (ms > cfg.longMs) long++;
const b = (ms / cfg.binMs) | 0;
hist[b < cfg.bins ? b : cfg.bins]++;
},
The statistics are computed only when a report is due, in one walk down the bins: average fps
is frames over total frame time, the 1% low is 1000 divided by the 99th-percentile frame time,
the worst frame is exact rather than binned, and a long frame is one over 50 ms. The page
measures its own interval between frames, not the engine's simulation step, because that step
is capped at 100 ms and would hide every long hitch. Only play frames count: paused, loading
and the three seconds after a load are left out by the same governorSamples rule the governor
uses.
At most every 30 seconds, and once when the page goes away (pagehide), the page sends one line
through the preload bridge, window.dmShell.perf. In a plain browser there is no bridge, so no
window is created and nothing is kept. The Electron main process accepts the line from the
game's own window only, and sanitises it before it reaches the log:
function perfLogLine(line) {
const s = String(line == null ? "" : line).replace(/[^ -~]/g, " ").trim().slice(0, 400);
return s ? `[perf] ${s}` : "";
}
Every byte outside printable ASCII becomes a space, so a page cannot break the line and forge a
second [shell] entry. A line reads:
[perf] fps 57 avg, 41 1% low, worst 212 ms, 3 long, 1712 frames; governor L2 cpu 0 (scale 50, shadows on, lighting full)
When the governor has spent its whole picture ladder and the median frame is still over target
5 seconds later (floorSlow), the line carries AT FLOOR, and the player sees one note per
session: "Low frame rate: quality lowered as far as it goes to keep the game responsive."
How it is verified
Suite engine/test/suites/2047-gfx-auto-tier.eval.ts drives the governor through simulated
minutes in microseconds, because the governor owns no timers. Case 11 pins the duel table:
- a duel at 40 ms frames for 60 seconds ends at level 2: both resolution steps, never shadows or lighting;
- a CPU-bound duel (36 ms of main-thread work in a 40 ms frame) ends at level 0: the whole picture ladder holds;
- a duel under the pixel look ends at level 0: there is no resolution step to take;
- a duel that slows for 30 seconds and then runs at 8 ms frames for 120 seconds returns to level 0.
The same case pins the floor: four steps in about 20 seconds at 25 fps, floorSlow true 5
seconds after the last one, false one step short of it, false again once frames are fast.
Case 9 checks the histogram against hand-computed values (1,000 frames with ten at 60 ms gives
a 1% low of 17 fps; a 2.4 s freeze past the top bin is still the exact worst). Case 10 checks
the report line character for character. Case 12 reads the shipped main.mjs, preload.mjs
and pauseMenu3d.mjs, runs the real perfLogLine against a CRLF forgery and a 900-character
line, and asserts the wiring: game window only, play frames only, once on pagehide, the floor
note once a session.
At merge, suite 2047 passed 15 of 15 and the type check was clean. This is proven by suites,
not yet measured in a live play session: no duel has been played on a saturated GPU with this
build, and no [perf] line from a player's machine has been read yet. The next measurement is
that line, from a real session, with the governor's level beside it.
Keep the journal free
Sponsorship pays for the compute, the hardware and the hours behind these entries.
by email
New journal entries, in your inbox.
What the agents built, broke and fixed. One message per entry.