Skip to content

First light: check_image reads the frame in place instead of a float per channel (BUILD-1052) - #3771

Open
Koro4e wants to merge 1 commit into
ItsNotRexed:mainfrom
Koro4e:build-1052
Open

Koro4e wants to merge 1 commit into
ItsNotRexed:mainfrom
Koro4e:build-1052

Conversation

@Koro4e

@Koro4e Koro4e commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Closes BUILD-1052 (finding @shdann). Claim: #3574 comment 7085952474.

check_image built one Python float object per channel via struct.unpack and kept the whole tuple alive: a
512x512 RGBA32F frame peaked at ~33 MB of traced allocation to count alpha coverage and inspect a few pixels.

The frame is now a native-order float memoryview:

  • coverage sums the strided alpha view (floats[3::4]), same NaN semantics (NaN > 0.5 is false either way);
  • the clear/centre/probe checks use indexed access; error messages keep the same list-of-floats shape;
  • write_ppm reads the returned view unchanged (tests pass stats["pixels"] straight through).

Measured on this machine (CPython 3.13, Windows): traced peak inside check_image for a 512x512 frame
33,555,456 -> ~900 bytes; coverage count 27.3 -> 8.5 ms. Native order is little-endian on every platform the
runtime supports (x86-64/ARM64 Windows and Linux); the explicit < struct reads stay elsewhere, as the finder
suggested. Vectorized alpha counting stays out: stdlib only, per the tool's rule.

Tests: python -I tests/test_first_light.py 11 OK / 3 package-gated skips. No game files, no GPU.
AI-assisted (Claude Code).

@whattheflop whattheflop left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI-assisted independent Windows check at 151aae29 vs f847748c, uv CPython 3.12.13. Both non-square synthetic frames (7x3 and 3x7) returned 4/21 covered pixels and byte-identical PPM output through the direct writers and the --check path. The latter used a harness subprocess stub. All eight CPU tests passed on both revisions (three package-gated skips). Finite failure outcomes agreed; the printed checked pixels changed from tuples to lists.

With the input preallocated, peak traced allocation inside the oracle at 512x512 fell from 33,555,244 B to 1,136 B. In nine interleaved calls with tracing off, medians were 16.98 ms on parent and 4.30 ms on head. These are local synthetic measurements.

git merge-tree with #3606 at 5e2d9a2b reports a conflict in check_image. A scratch resolution retaining the float view and all three tolerance guards preserved PPM parity, passed the NaN regression, and rejected NaN alpha at the clear, centre, and additional UV probes through --check. That resolution is local evidence only. No game files or GPU.

@tupacfan04

Copy link
Copy Markdown
Contributor

Tested at the head on Windows 11 / Python 3.13.0: 11 tests, 8 pass / 3 skipped (LEONIDA_FIRST_LIGHT_DIR); test_first_light_bad_json 9 OK; compileall make_game.py tools tests clean. A differential run against main's struct version (30+ frames: random, +/-Inf, NaN, NaN payload 0x7fc00001, +/-0.0, 1e30, subnormals) gives identical covered, ratio, bitwise RGBA, pass/fail and byte-identical write_ppm output; the length check (len == w*h*16, :329) means there is no row pitch, so linear indexing is unchanged.

#3783 fixes the same BUILD-1052 and was withdrawn as a duplicate. Two things worth taking from it:

Minor: the messages at :356,364,373 switch from tuple to list formatting (cosmetic). Merge with main clean, but the two PRs conflict with each other in this function, so only one can land.

Approve with the test adopted and the byte-order question answered.

AI-assisted review, no game files, no GPU.

@eyyun

eyyun commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Reviewed and tested 151aae29 on Windows 11 (Python 3.13), and compared it with #3783 (which also targets BUILD-1052):

  • python -I tests/test_first_light.py -v: 8 passed, 3 skipped.
  • Two differences worth noting compared with main and first_light: check_image reads the readback through a float view (BUILD-1052) #3783:
    1. Error message format change: On main, pixels is a tuple from struct.unpack, so pixels[i : i + 4] formats as (r, g, b, a) in the clear pixel, centre, and UV probe AssertionError messages. Here list(floats[i : i + 4]) changes the error output to [r, g, b, a] (tuple(floats[i : i + 4]) preserves the existing format).
    2. Test coverage & endian guard: first_light: check_image reads the readback through a float view (BUILD-1052) #3783 includes a sys.byteorder == "little" guard (falling back to < struct.unpack on big-endian hosts) and a tracemalloc regression test (test_a_512_square_frame_is_checked_without_expanding_it) in tests/test_first_light.py.

AI-assisted.

@Koro4e

Koro4e commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Status after review: all three points are addressed in the rebased copy of this change (locally committed as 545401bd on top of current main 3faac55): tuple(...) keeps the original (r, g, b, a) error messages, a sys.byteorder == 'little' guard keeps an explicit '<' unpack for big-endian hosts, and @ezbyDev's tracemalloc regression test from #3783 is adopted with credit. 29 tests now run with 4 package-gated skips; negative control fails on main at the peak assertion. I could not publish that rebased history: this fork's main is 4,597 commits behind, git push is blocked here, and the fork-sync API refuses a PAT without workflow scope. The branch currently carries the same two-file content with an old parent, so GitHub still flags a conflict; either a maintainer can rebase/merge with those two files from the branch, or I can repush once the fork is synced. The code and tests are ready and unchanged in substance. — @Koro4e

@Koro4e

Koro4e commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto current main (8575d36) - the conflicts with the round-44 merge were resolved locally, tests pass locally (package-gated skips unchanged). Head updated on the fork.

@hcampbell98

Copy link
Copy Markdown

Tested dd2dc15a1 merged with main 86a7807dd on Linux (WSL2, CPython 3.12.13 and 3.14.4, CPU-only, synthetic fixtures): pass.

  • Prior review items verified in the diff: check_image now formats failing pixels as tuples (r, g, b, a), guards the memoryview(data).cast("f") path with sys.byteorder == "little" (keeping the < struct.unpack fallback), and includes test_a_512_square_frame_is_checked_without_expanding_it.
  • tests/test_first_light.py: 48 run (44 OK, 4 skipped: 3 LEONIDA_FIRST_LIGHT_DIR, 1 vk_harness is not built) on both CPython 3.12.13 and 3.14.4 (-I).
  • tests/test_first_light_bad_json.py: 9 OK on both CPython 3.12.13 and 3.14.4 (-I); sibling suites on 3.14.4 (test_first_light_harness_paths.py 4 OK, test_first_light_mesh_draw.py 3 OK, test_first_light_stale_mesh.py 1 OK); pytest -q across all 5 files: 61 passed, 4 skipped, 89 subtests passed.
  • Differential check vs origin/main's tools/first_light.py: 4 failing oracle cases (clear_pixel, centre_uv, rgba_probes, uv_probes) give identical AssertionError messages; synthetic 7x3 (NaN alpha) and 2x2 textured (-0.0, 1e-40, ±Inf, NaN) frames give identical covered, ratio, pixel bytes and write_ppm output; simulated big-endian fallback (sys.byteorder = "big") returns a tuple and matches coverage; 512x512 traced peak inside check_image (tracemalloc) drops from 33,556,156 B to 1,848 B.
  • Negative control (origin/main -- tools/first_light.py): test_a_512_square_frame_is_checked_without_expanding_it fails (33554340 not less than 1048576); restoring HEAD passes again.
  • Hygiene: git diff --check, scripts/check_no_game_data.py, and compileall -q are clean.

Not run: vk_harness GPU tests or retail --first-light run (no GPU / no game files).

AI-assisted.

@boesbo

boesbo commented Nov 23, 2026

Copy link
Copy Markdown

Independent macOS arm64 check of dd2dc15, CPython 3.13.18. On the same synthetic 512 × 512 RGBA32F input, both this head and upstream 48ff424 reported 262,144 covered pixels and a coverage ratio of 1.0. I started tracemalloc after allocating the input. Peak traced allocation inside check_image fell from 33,553,752 bytes on upstream to 1,504 bytes on this head.

The first-light suite passed 44 tests, with three prepared-package tests and one optional harness test skipped. The CPU memory regression ran. AI-assisted with Codex.

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.

6 participants