Skip to content

grid3d 0.5.3: hardening, a slow 3D view goes back to 2D, and a 2D/3D switch that reads as a choice - #84

Merged
dmccoystephenson merged 11 commits into
mainfrom
feat/grid3d-0.5.3
Nov 21, 2026
Merged

dmccoystephenson merged 11 commits into
mainfrom
feat/grid3d-0.5.3

Conversation

@dmccoystephenson

Copy link
Copy Markdown
Member

Summary

This builds on 0.5.2 (portrait framing and touch keys) and is released as 0.5.3. It has eleven commits: the grid3d hardening (01-07), two changes to the page template (08-09), a cut in the Python cost of a frame (10) and the version bump (11).

Hardening (01-07)

  • Turning 3D off while it loads, then on again, left a blank page (templates/pygbag.tmpl). Strata 0.9.0's grid-relay.js forgets a close() that arrives while its module loads: it finishes ready on a hidden canvas, and a later request() returns at once without showing it. The template now closes and reopens a relay it finds ready on a hidden canvas, stops a relay that finishes loading after 2D was chosen, and counts the 3D view as drawing only when its canvas is shown. A Node test drives the template's script against a stand-in for the 0.9.0 relay.
  • The wire encoders refuse what the relay refuses (web.py, WireError), with the relay's status, where the game made the mistake: counts over the limits, non-finite numbers, coordinates past ±10⁶, bad lights, bad HUD colours, and a camera whose eye is on its target. Label text: a lone surrogate is sent as ? and a NUL is dropped on both backends. golden/wire-1.0.hex pins one message of each kind.
  • Picking with a camera looking straight up or down, or with its eye on its target, raises a ValueError that says so (it used to be a ZeroDivisionError).
  • ScreenRecorder answers every public call and constant that Graphik has (beginFrame/endFrame, drawImage → RecordedImage, getVersion, the colours). A test compares the two, so a new Graphik method can't be missed.
  • relay.py checks every tarball member before it writes anything, refuses a link that leads out of --dest, caps the relay at 64 MB, accepts only a release version in --strata, refuses bad SHA256SUMS lines, and makes --replace update an earlier relay's files (never index.html).
  • CI: a grid3d render (Strata, llvmpipe) job runs the grid3d tests with GRAPHIK_REQUIRE_STRATA=1, so the golden-image and mid-tick render tests run on every PR instead of being skipped.
  • docs/grid3d.md: an API reference for every public name, with an example for each area. It also covers 0.5.2's names: fitCamera(top, bottom), setInsets, touchScreen(), and a Touch keys section.

When the hardening was put on top of 0.5.2, __init__.py was the only conflict, in two places: RecordedImage was added to __all__ alongside 0.5.2's touch-key names, and the docstring's pointer to docs/grid3d.md was moved after 0.5.2's touchkeys.py sentence. The template, test_grid3d_web.py and the README merged without conflicts, and both behaviours stay in place: touch() and area() sit beside the relay workaround.

A 3D view whose drawing stays too slow goes back to 2D (08)

Strata's kit handles slow frames itself. It first lowers its pixel density to 1, then its quality tier. When it has nothing left to give up, it calls onSlow, and the relay passes that to StrataLoader.slow(), which does nothing on graphik's page. The template's "slow" reason was never used.

The page now watches the drawing itself once the kit is at quality 0 and pixel density 1. Every second it takes three readings:

  • the typical frame: the median gap between the page's own requestAnimationFrame calls, with stalls far past the median cut;
  • the relay's own drawing: cpuMs from its stats(), reset each second;
  • the kit's own average frame: frameMs from the same stats().

When for 5 seconds the typical frame and the kit's average are both over 66 ms, and the drawing is at least a quarter of the typical frame, the page goes back to 2D. The note says: "Back to 2D: this device draws the 3D view too slowly. Press 3D to try it again."

  • The game's own pauses don't count. A first version used the kit's average frame time. On a real GPU (an Intel UHD 630), it sent Apex back to 2D (64 x 64 on the desktop, and 29 x 29 on a phone with a 4x CPU throttle), although the relay drew those frames in about 2.5 ms (median frame 16.7 ms). The cause was 180-1800 ms pauses while the game built and sent boards in Python, which inflate the average but not the median. A game that is slow every frame while the drawing is cheap stays in 3D too: 2D wouldn't help it. A second that is nearly all stall neither counts nor restarts the count.
  • Pygbag's boot is not counted. Counting starts 3 seconds after the game's first message to the 3D view, so the boot, the first shader compile and the kit's own steps down are left out. It stops while the tab is hidden.
  • This is not the player's choice and not a failure. Nothing is stored in localStorage, and the button still offers 3D. Once the player chooses 3D again, the page leaves 3D on, however slow, until a reload.
  • ?slow=0 in the address turns the switch off, for testing on a machine that draws in software. Graphik3D.slowed() returns 1 once the switch has happened, and Graphik3D.speed() gives the last second's readings.
  • Why the kit's average is in the rule too. Without it, the games' browser test harnesses went to 2D mid-run. They pin the kit's frameMs to keep 3D on in software GL. On that machine Apex's relay really does draw a 29 x 29 board in 46-330 ms a frame; speed() showed typical frames of 50-480 ms. An intermediate build that read only the page's frames and cpuMs ignored that pin. The game then rightly closed its 3D view, its Python 3D frames dropped to 0-2 a run, and keys aimed at the 3D HUD stopped showing. With all three readings required, the pin is honoured again, and a Node test covers it.
  • Limitation: a device whose GPU, not its CPU, is the bottleneck shows a long typical frame with little relay CPU, so it stays in 3D at the kit's lowest tier.

The 2D/3D button reads as a choice (09)

While 3D was on, the button read "2D", which looked like a status. It is now a switch showing both views, 2D and 3D, with the current one highlighted.

  • Screen readers: it is a toggle named "3D view" with aria-pressed set while 3D is on. When 3D can't start, it reads "3D view unavailable" and aria-disabled is set.
  • Size and layout: it is at least 44 CSS pixels each way (it was 36 px tall); on a 390 px phone it measures 94 x 44. The note moved below it, and buttonArea() measures it as drawn.
  • State for scripts and tests: data-view gives what is on show (2d, 3d or unavailable).

A duplicated comment opener in the template was also removed.

Frame cost in Python (10)

On a 64 x 64 board, most of a desktop frame's Python time went into building one RenderItem object per shape. The games build a fresh Scene every frame, so caching the board's items wouldn't help them; packing the shapes fast does:

  • Grid3DView.drawScene packs a scene's shapes into a ctypes array of RenderItems in one pass (view.renderItems(); submit_primitives takes the array without a copy). The bytes are identical to building the objects one by one. A 3-part colour, or a binding whose RenderItem isn't the 44-byte structure, goes the old way.
  • The web backend encodes a BOARD's shapes in one pass, with the relay's checks made on all the values at once. On any failure it falls back to the shape-by-shape path, which raises the precise error.
  • Grid3DWebView.setBoard no longer resends a BOARD identical to the last one, so the relay doesn't rebuild an unchanged board.

Python time per call, median of 30, measured on an Apex-like board (Linux, Python 3.12; engine calls stubbed out so the GPU isn't counted; [before → after]):

29 x 29 (728 shapes) 64 x 64 (3,499 shapes)
desktop drawScene 3.32 → 0.49 ms 22.96 → 2.95 ms
web drawScene, board unchanged 1.74 → 1.34 ms 10.17 → 7.28 ms
web setBoard, board unchanged: bytes sent 30,016 → 96 143,488 → 96

The game's own scene building (a fresh GridBoard and board.scene(), about 19 ms at 64 x 64) is unchanged, and so is the web setBoard time, which is mostly the board's shapes (about 20 ms at 64 x 64). These numbers are from a busy shared machine, so treat them as ratios.

Tests

  • python -m pytest: 493 passed. With GRAPHIK_REQUIRE_STRATA=1 (Strata 0.8.0 on llvmpipe): 493 passed, none skipped. The suite was also run on the tree after each commit: 07 (471), 08 (481), 09 (484), 10 (493), 11 (493).

  • Node template tests (test_grid3d_template_js.py), 15 in all:

    • three from the hardening;
    • ten for the slow switch:
      • slow drawing goes back to 2D, with the reason, after the grace period plus 5 s;
      • nothing counts before the kit reaches its lowest tier and density 1, or before the game's first message;
      • a fast second restarts the count;
      • a game that stalls the page (900 ms every 1.2 s, or 1.8 s every 2 s) while drawing is fast stays in 3D;
      • a game that is slow every frame with cheap drawing stays in 3D;
      • slow drawing with stalls in between still switches;
      • a kit reporting fast frames keeps 3D;
      • ?slow=0 turns the switch off;
      • after a slow switch the player can choose 3D and keep it;
    • two for the button.
  • test_grid3d_frame_cost.py (9 tests):

    • one-pass RenderItems byte for byte, against a stand-in 44-byte structure and the real binding;
    • the fallbacks: a 3-part colour, a stand-in binding, an empty list;
    • the one-pass BOARD byte for byte, and refusing NaN, infinity and out-of-range values as before;
    • setBoard with an unchanged board sends no BOARD, but still sends VIEW and PIECES.
  • The three games' browser e2e, built with this tree on Strata 0.9.0's relay:

    • Apex phone 94/94, Apex desktop 45/45, Tic-Tak-Toe 161/161, Ophidian 72/72.
  • Ophidian's phone e2e, run on Strata 0.9.0's relay and again with 0.9.1's integrated grid-relay.js, strata-kit.js and strata-loader.js copied over 0.9.0's (390 x 844, touch), 78/79 on each. It covers:

    • 3D by default, with 3D lit and aria-pressed=true;
    • the button, 94 x 44, on screen and clear of HUD text in 3D and in the shop;
    • every touch key in 3D and in 2D, and swipes and taps;
    • back to 2D with 2D lit;
    • 2D remembered across a reload; ?view=2d; the engine or relay script blocked, which shows "3D unavailable";
    • desktop framing kept;
    • the slow block.

    The one failure is a check written for an older Ophidian 2D layout; the game's own harness passes it.

  • The slow switch in the browser (e2e-slow.cjs, a DPR-1 phone), on both relays, 8/8 each:

    • Unthrottled, 60 s in 3D did not trip it: typical frame about 28 ms (software GL).
    • With 900 ms stalls every 2 s, 30 s in 3D did not trip it.
    • With the CPU throttled 20x through CDP, frames went to about 110 ms with the relay drawing 50-67 ms of each. The page went back to 2D 7 s after the throttle began, with the note shown, nothing stored, and 2D lit. The 2D game ran on.
    • Pressing 3D brought 3D back, and it stayed on for 30 s under the same throttle.
  • Startup: on 0.9.1, the relay starts its frame loop on the first accepted message. The page's own slow check only starts 3 s after that message. No startup penalty from graphik was seen; in both runs the kit was already at q0 by the first reading, on software GL.

drafted by Claude on behalf of Daniel Stephenson

🤖 Generated with Claude Code

dmccoystephenson and others added 11 commits November 20, 2026 18:02
…lank page

Strata 0.9.0's grid-relay.js forgets a close() that arrives while its
module loads: it finishes "ready" on a hidden canvas, and a later
request() returns at once without showing it, so the template hid the 2D
canvas with nothing in its place. The template now closes and reopens a
relay found ready on a hidden canvas, stops a relay that finishes loading
after 2D was chosen, and counts the 3D view as drawing only when its
canvas is shown. A Node test drives the template's script against a
stand-in for the 0.9.0 relay.

drafted by Claude on behalf of Daniel Stephenson

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LYNeuoCqxFpUXrFhxnTg89
…made the mistake

Counts over the relay's limits, non-finite numbers, coordinates past
1e6, lights with no radius or a negative colour or intensity, HUD colours
out of range and a camera whose eye is on its target now raise WireError
with the relay's status. A lone surrogate in a label is sent as "?" and a
NUL is dropped on both backends; measureText no longer counts NULs.
golden/wire-1.0.hex pins one message of each kind, and edge-case tests
cover picking, board edges, viewports and the Animator.

drafted by Claude on behalf of Daniel Stephenson

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LYNeuoCqxFpUXrFhxnTg89
…a raises a ValueError that says so

Before, such a camera failed with ZeroDivisionError deep in cameraBasis.

drafted by Claude on behalf of Daniel Stephenson

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LYNeuoCqxFpUXrFhxnTg89
beginFrame and endFrame no longer raise AttributeError, drawImage is
recorded (RecordedImage, exported next to 0.5.2's touch-key names), and
getVersion and the colour constants are there. A test compares the
recorder with Graphik's public names.

drafted by Claude on behalf of Daniel Stephenson

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LYNeuoCqxFpUXrFhxnTg89
…t it fetches

A refused tarball no longer leaves files behind, a linked folder in --dest
cannot carry a file out, the relay folder is capped at 64 MB, --strata
takes only a release version, SHA256SUMS lines that disagree or are not
SHA-256 are refused, and --replace updates an earlier relay's files
(never index.html).

drafted by Claude on behalf of Daniel Stephenson

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LYNeuoCqxFpUXrFhxnTg89
The job installs the strata extra and Mesa and runs the grid3d tests with
GRAPHIK_REQUIRE_STRATA=1, so the golden-image and mid-tick render tests
run on every pull request instead of skipping.

drafted by Claude on behalf of Daniel Stephenson

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LYNeuoCqxFpUXrFhxnTg89
Every public class and function is listed, grouped by area, with an
example for each group; 0.5.2's portrait framing (fitCamera top and
bottom, setInsets), touchScreen() and the touch keys are covered in a
Touch keys section. The README and the package docstring link to it.

drafted by Claude on behalf of Daniel Stephenson

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LYNeuoCqxFpUXrFhxnTg89
The relay's onSlow calls StrataLoader.slow(), which does nothing on
graphik's page, and the template's "slow" reason was never used. The
page now watches the drawing itself once Strata's kit is at its lowest
quality tier and pixel density 1: second by second, the typical frame
(the median gap between the page's requestAnimationFrame calls, long
stalls cut), the relay's own drawing a frame (its stats' cpuMs) and the
kit's own average frame (frameMs). When for 5 seconds the typical frame
and the kit's average are over 66 ms and the drawing is at least a
quarter of the typical frame, the page goes back to 2D and the note says "Back to 2D:
this device draws the 3D view too slowly. Press 3D to try it again." A
game that stops the page now and then (building and sending a board in
Python), or that is slow every frame while the drawing is cheap, stays in
3D: 2D would not help it. Counting starts 3 seconds after the game's
first message to the 3D view (pygbag's boot and the first shader compile
are not counted) and stops while the tab is hidden. The switch is not
remembered as the player's choice and is not a failure: the button still
offers 3D, and once the player chooses 3D again the page leaves it alone
until it is reloaded. ?slow=0 in the address turns the switch off (for
testing on a machine that draws in software); Graphik3D.slowed() says it
happened and Graphik3D.speed() gives the last second's readings. Node
tests drive it against a stand-in relay and page frames:
slow drawing switches (stalls or not); a stalling game with fast
drawing, a slow game with cheap drawing, and a kit reporting fast frames
(as browser test harnesses pin it on software GL) don't.

drafted by Claude on behalf of Daniel Stephenson

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LYNeuoCqxFpUXrFhxnTg89
The button read "2D" while the 3D view was on, like a status. It is now a
switch with both views on it, 2D and 3D, the current one highlighted; to
a screen reader it is a toggle named "3D view", aria-pressed while 3D is
on ("3D view unavailable" when 3D cannot start), and data-view says what
is on show. It is at least 44 CSS pixels each way (it was 36 tall), the
note sits below it, and buttonArea() measures it as drawn. A duplicated
comment opener in the template was removed.

drafted by Claude on behalf of Daniel Stephenson

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LYNeuoCqxFpUXrFhxnTg89
…oard the relay has

On a 64 x 64 board most of a desktop frame's Python went into building
one RenderItem object per shape. Grid3DView.drawScene now packs a
scene's shapes into a ctypes array of RenderItems in one pass (view.py
renderItems(); submit_primitives takes it without a copy), byte for byte
what the objects gave, about twelve times faster; a 3-part colour or a
binding whose RenderItem is not the 44-byte structure goes the old way.
The web backend encodes a BOARD's shapes in one pass too, with the
relay's checks made on all the values at once (any failure goes through
the shape-by-shape path for its message), and Grid3DWebView.setBoard no
longer sends a BOARD identical to the last one, so the relay doesn't
rebuild an unchanged board. Games build a fresh Scene every frame, so
the shapes are packed fast rather than cached.

drafted by Claude on behalf of Daniel Stephenson

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LYNeuoCqxFpUXrFhxnTg89
The version and the README's install lines say 0.5.3: the grid3d
hardening, the slow fallback to 2D and the 2D/3D switch.

drafted by Claude on behalf of Daniel Stephenson

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LYNeuoCqxFpUXrFhxnTg89
@dmccoystephenson
dmccoystephenson merged commit 42a80db into main Nov 21, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant