dds_png: compress scanlines without staging the entire image #2249

Open
github-bot wants to merge 1 commit from github-bot/gh-2249 into main AGit
Owner

@⁠kirigayakazima on GitHub · 2026-11-26 10:23 UTC · original
kirigayakazima:codex/dds-png-bounded-scanlines → main · +26 −11 · 2 files · 1 commits · open

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.
**@⁠kirigayakazima** on GitHub · 2026-11-26 10:23 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2249) `kirigayakazima:codex/dds-png-bounded-scanlines` → `main` · +26 −11 · 2 files · 1 commits · open Claimed in the coordination issue as: [DDS-PNG-BOUNDED-SCANLINES](https://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608) ### 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.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 11:24:27 +00:00
Author
Owner

@⁠DeadlyChirp on GitHub · 2026-11-26 10:42 UTC · original

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

**@⁠DeadlyChirp** on GitHub · 2026-11-26 10:42 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2249#issuecomment-7189726978) 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.)
Author
Owner

@⁠lixonik on GitHub · 2026-11-26 10:58 UTC · original · reviewed

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.

**@⁠lixonik** on GitHub · 2026-11-26 10:58 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2249#pullrequestreview-6072925273) · **reviewed** 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.
Author
Owner

@⁠wavehs on GitHub · 2026-11-26 19:50 UTC · original · reviewed

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.

**@⁠wavehs** on GitHub · 2026-11-26 19:50 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2249#pullrequestreview-6079773040) · **reviewed** **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.
Author
Owner

@⁠tiguan26 on GitHub · 2026-11-27 04:34 UTC · original · approved these changes

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

**@⁠tiguan26** on GitHub · 2026-11-27 04:34 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2249#pullrequestreview-6083525793) · **approved these changes** 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).
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/2249/head:github-bot/gh-2249'
git switch 'github-bot/gh-2249'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2249'
git switch 'github-bot/gh-2249'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2249'
git switch 'github-bot/gh-2249'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2249'
git switch 'main'
git merge --squash 'github-bot/gh-2249'
git switch 'main'
git merge --ff-only 'github-bot/gh-2249'
git switch 'main'
git merge 'github-bot/gh-2249'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.