dds_png: compress scanlines without staging the entire image #2249
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2249"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from ubiquitarianism/leonida. Originally posted by kirigayakazima on 2026-11-26T10:23:59Z.
Claimed in the coordination issue as: DDS-PNG-BOUNDED-SCANLINES
What and why
Feed PNG filter bytes and RGBA rows to one zlib compressor incrementally. A 1024×1024 zero-filled synthetic texture on Windows reduced peak allocations measured by tracemalloc after source allocation from 4,579,376 to 384,418 bytes. Existing tests also decode and compare the image data.
Tested
Windows, Python 3.14.7. The regression failed on the parent commit and passed with this change.
python -I tests/test_dds_png.pypython -I tests/test_dds_subresources.pypython -I tests/test_dds_png_dx10.pygit diff --checkand the changed-path no-game-data scan passed.GPUs tested: N/A (synthetic CPU tests).
AI-assisted: Yes.
Checklist
Imported from ubiquitarianism/leonida. Originally posted by DeadlyChirp on 2026-11-26T10:42:32Z.
Tested on Windows 11, Python 3.12.10, with this PR merged onto current upstream main.
tests/test_dds_png.py: 101 passed, 143 subtests passed (main: 100 passed).(AI-assisted test run; output checked by hand.)
Imported from ubiquitarianism/leonida. Originally posted by lixonik on 2026-11-26T10:58:29Z.
Review: COMMENTED
Windows 11: 101/101 DDS/PNG tests passed. I compared synthetic 2048×2048 zeros, 1024×1024 seeded random RGBA, an odd rectangle, and a narrow image against the base. Each PNG was byte-identical, and inflating IDAT recovered the input RGBA. Tracemalloc peak after allocating the source fell from 16.29 to 0.30 MiB for the zero image. The random case went from 16.01 to 16.25 MiB, consistent with keeping the entire compressed IDAT in memory. No correctness findings from these checks.
AI-assisted.
Imported from ubiquitarianism/leonida. Originally posted by wavehs on 2026-11-26T19:50:27Z.
Review: COMMENTED
macOS arm64 check of #2249 (dds_png scanline streaming). A small Python encoder change; runs here with the standard library only.
Tested head
a9ec081f1merged ontomainbdbc7e923as merge7ed2ab4f4(clean, no conflicts).Environment: Apple M3 Pro arm64, macOS 27.2.1, CPython 3.15.2 (zlib from the system CPython build).
On the merge
python -I tests/test_dds_png.py: 101 tests OK, including the newtest_compressible_png_has_bounded_scanline_storage(subprocess,tracemallocpeak < 1 MB for a 1024x1024 RGBA source).encode_png: walking the chunks, the IDAT decompresses to one filter byte0per scanline and the exact RGBA bytes, so the streaming is spec-compliant and not losing the row separators.zlib.compress(raw)(chunked deflate may differ); no test depends on that, and the PNG decodes to the same pixels.Negative control (new test kept,
tools/dds_png.pyreverted tomain)FAILED (failures=1)- the memory test, because the old code allocatesheight * (stride + 1)(~4 MB here) before compressing. Restored: OK.Nits
< 1024 * 1024bytes) across a subprocess. With a 4 MB source and the streaming encoder the measured peak is far below, so there is room, but on a CPython/zlib build with a much larger compressor ortracemallocoverhead this could be brittle. A relative bound (e.g.< len(rgba) / 2) would keep the intent ("does not stage the image") without pinning a number that depends on the compression library.compressor.compress(b"\x00")followed by the row is clear;compressed.extendon abytearrayis fine. The whole compressed stream is still held in memory (necessary for one IDAT), which the comment on the test could say so nobody reads it as "constant memory".Not run: the DDS side of the tool (BC decode/encode paths) beyond what the existing 101 tests cover, and no GPU; this change is PNG-only.
python -I scripts/check_no_game_data.pyon both changed files: clean.No game files. AI-assisted.
Imported from ubiquitarianism/leonida. Originally posted by tiguan26 on 2026-11-27T04:34:06Z.
Review: APPROVED
Approving
a9ec081f1.Scanlines go to a streaming
zlibcompressor one by one with their filter byte, instead of being copied into a second image-sized buffer first; the decompressed data is the same, so the PNG is too.Merged onto
maind1dff7949in one batch with 22 other tool PRs of this series (no conflicts), on Linux with Python 3.13.tests/test_dds_png.py: 101 passed (main: 100). AI-assisted (Claude Code, Claude Opus 5.5).Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-28T19:31:35Z.
Red/green: tests/test_dds_png.py fails on main (4579376 bytes of scanline storage, not below 1048576) and passes with the PR merged, on GitHub Actions Linux and natively on Windows.
Targeted batch (PR tests, tests importing the changed modules, docs tests, translator when touched) merged on main
7daa4a4a5in GitHub Actions: no new failure.AI-assisted (Claude Code, Claude Opus 5.5)