dds_png: compress scanlines without staging the entire image #2249

Merged
kirigayakazima merged 1 commit from upstream-pr/2249 into main 2026-11-28 19:31:39 +00:00
kirigayakazima commented 2026-11-26 10:23:59 +00:00 (Migrated from github.com)

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.py
  • python -I tests/test_dds_subresources.py
  • python -I tests/test_dds_png_dx10.py
  • git diff --check and the changed-path no-game-data scan passed.
  • Shared main baseline: 498 semantic test files, 6,351 tests passed.

GPUs tested: N/A (synthetic CPU tests).

AI-assisted: Yes.

Checklist

  • Based on current main and merges cleanly with the tested base.
  • One topic.
  • No game data, keys or decryption.
  • Translator contract table: N/A.
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5508234077 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2249). Originally posted by [kirigayakazima](https://github.com/kirigayakazima) on 2026-11-26T10:23:59Z. --- Claimed in the coordination issue as: [DDS-PNG-BOUNDED-SCANLINES](https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7189018014) ### 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.py` - `python -I tests/test_dds_subresources.py` - `python -I tests/test_dds_png_dx10.py` - `git diff --check` and the changed-path no-game-data scan passed. - Shared main baseline: 498 semantic test files, 6,351 tests passed. GPUs tested: N/A (synthetic CPU tests). AI-assisted: Yes. ### Checklist - [x] Based on current main and merges cleanly with the tested base. - [x] One topic. - [x] No game data, keys or decryption. - Translator contract table: N/A.
DeadlyChirp commented 2026-11-26 10:42:32 +00:00 (Migrated from github.com)

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.)

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7189726978 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/2249#issuecomment-7189726978). Originally posted by [DeadlyChirp](https://github.com/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.)
lixonik commented 2026-11-26 10:58:29 +00:00 (Migrated from github.com)

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.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6072925273 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2249#pullrequestreview-6072925273). Originally posted by [lixonik](https://github.com/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.
wavehs commented 2026-11-26 19:50:27 +00:00 (Migrated from github.com)

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 a9ec081f1 merged onto main bdbc7e923 as merge 7ed2ab4f4 (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 new test_compressible_png_has_bounded_scanline_storage (subprocess, tracemalloc peak < 1 MB for a 1024x1024 RGBA source).
  • I also round-tripped a hand-built 3x5 image through encode_png: walking the chunks, the IDAT decompresses to one filter byte 0 per scanline and the exact RGBA bytes, so the streaming is spec-compliant and not losing the row separators.
  • Output bytes need not be identical to the old one-shot 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.py reverted to main)

  • FAILED (failures=1) - the memory test, because the old code allocates height * (stride + 1) (~4 MB here) before compressing. Restored: OK.

Nits

  • The new test asserts an absolute peak (< 1024 * 1024 bytes) 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 or tracemalloc overhead 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.extend on a bytearray is 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.py on both changed files: clean.

No game files. AI-assisted.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6079773040 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2249#pullrequestreview-6079773040). Originally posted by [wavehs](https://github.com/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 `a9ec081f1` merged onto `main` `bdbc7e923` as merge `7ed2ab4f4` (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 new `test_compressible_png_has_bounded_scanline_storage` (subprocess, `tracemalloc` peak < 1 MB for a 1024x1024 RGBA source). - I also round-tripped a hand-built 3x5 image through `encode_png`: walking the chunks, the IDAT decompresses to one filter byte `0` per scanline and the exact RGBA bytes, so the streaming is spec-compliant and not losing the row separators. - Output bytes need not be identical to the old one-shot `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.py` reverted to `main`)** - `FAILED (failures=1)` - the memory test, because the old code allocates `height * (stride + 1)` (~4 MB here) before compressing. Restored: OK. **Nits** - The new test asserts an absolute peak (`< 1024 * 1024` bytes) 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 or `tracemalloc` overhead 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.extend` on a `bytearray` is 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.py` on both changed files: clean. No game files. AI-assisted.
tiguan26 commented 2026-11-27 04:34:06 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by tiguan26 on 2026-11-27T04:34:06Z.

Review: APPROVED


Approving a9ec081f1.

Scanlines go to a streaming zlib compressor 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 main d1dff7949 in 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).

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6083525793 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2249#pullrequestreview-6083525793). Originally posted by [tiguan26](https://github.com/tiguan26) on 2026-11-27T04:34:06Z. **Review:** APPROVED --- Approving a9ec081f1. Scanlines go to a streaming `zlib` compressor 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 `main` `d1dff7949` in 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).
MoonEyes commented 2026-11-28 19:31:35 +00:00 (Migrated from github.com)

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 7daa4a4a5 in GitHub Actions: no new failure.
AI-assisted (Claude Code, Claude Opus 5.5)

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7244610070 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2249#issuecomment-7244610070). Originally posted by [MoonEyes](https://github.com/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 7daa4a4a5 in GitHub Actions: no new failure. AI-assisted (Claude Code, Claude Opus 5.5)
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
ProjectLeonida/mandy!2249
No description provided.