Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ Don't modify this file.
- Keep concise code clear: no cryptic names, excessive compression, clever tricks, or obfuscation.
- Before adding code, reuse existing logic, remove redundancy, or simplify the design. Prefer deleting code over adding abstractions.

## Comments
- Do not write descriptive comments in .c implementation files. The code must explain itself through clear naming and straightforward logic.
- Do not add comments that restate what the code does, explain obvious implementation details, or introduce sections of code.
- Comments are allowed only when necessary to explain non-obvious reasoning, tricky algorithms, constraints, or workarounds.
- API documentation belongs in the .h header files. Document public functions, types, and parameters there when useful.
- Do not duplicate header documentation in the corresponding .c file.
## Feature efficiency = (Expected benefit % / Added LoC) × 1,000
* Reject low-ROI changes, even when technically beneficial.
* Prefer skipping a feature over significantly increasing code size for negligible gains.
* Every additional line of code must justify its maintenance and readability cost.
* Avoid speculative features, unnecessary abstractions, and premature generalization.
* Before implementation, report the expected benefit, estimated LoC, and feature efficiency.
* If the ROI is poor, recommend not implementing the change.

## Memory
- Libraries: no `malloc`/`free`; prefer caller-provided buffers. If a library needs more than the caller-provided buffer, return an error or a required-size value; don't allocate behind the caller's back.
Expand All @@ -43,6 +44,14 @@ Don't modify this file.
- Never sacrifice correctness or maintainability for unmeasured optimization.
- Do not perform or interpret GPU profiling while the LLM is running. Request that the user collect and provide a standalone GPU profile instead.

## Comments
- Do not write descriptive comments in .c implementation files. The code must explain itself through clear naming and straightforward logic.
- Do not add comments that restate what the code does, explain obvious implementation details, or introduce sections of code.
- Comments are allowed only when necessary to explain non-obvious reasoning, tricky algorithms, constraints, or workarounds.
- Keep comments short and local. A comment explains at most one branch and sits on it: on the branch's `if` / `else` line or its first statement, at most 2 lines. A comment above a construct must never describe more than the line it sits on; a self-evident branch gets no comment.
- API documentation belongs in the .h header files. Document public functions, types, and parameters there when useful.
- Do not duplicate header documentation in the corresponding .c file.

## Build
- Config: `cmake -B build -S . -DCMAKE_BUILD_TYPE=Release`
- Build: `cmake --build build`
Expand Down
61 changes: 46 additions & 15 deletions README.md

Large diffs are not rendered by default.

88 changes: 88 additions & 0 deletions failed_experiments.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Rice `k` and rank table per endpoint fits better. Kept.
| 20 | Color-pair sliding dictionary: a 1-bit flag + a `ceil(log2(min(pos,256)))`-bit reference to a matching block within the last 256 replaces the six color deltas when the two endpoint colors exactly repeat, plus a per-strip enable flag | round-trips (CPU + GPU); forced version was a net loss (25 improved / 44 regressed), the per-strip version: 44 improved / 5 regressed / 25 flat, the flat ones byte-identical to the baseline | 1.4587 → 1.4654 (+0.46%) avg; 1.4020 → 1.4073 (+0.38%) byte-weighted; best +2.95% (wall2), worst −0.14% (kodim15) | **kept — this is the new baseline** (the per-strip flag turns the forced-match net loss into a net win) |
| 21 | `bc4_index_family` endpoint-range thresholds: 30+ split variants on the 10-image bc4/bc5/bc3 suite (3-family 8/16 → 5/40; also 2- and 4-family splits) | round-trips (CPU + GPU); 10/10 images improved, best −1.04% (bc3 leak-stain alpha), worst −0.02%; 2- and 4-family counts all lose, the 3-family minimum is a wide plateau (t1 4..6 / t2 32..48 all ≥ −0.19%) | bc4 1.1244 → 1.1279, bc5 1.0756 → 1.0772, bc3 2.7149 → 2.7307 (suite averages); stream −0.235% overall | **kept — this is the new baseline for bc4/bc5/bc3** (the 74-image bc1 suite is untouched; the README's "obvious tuning lever" exercised) |
| 22 | 8 x 8 tiles: the 64 streams become an even 8 x 8 tile of the block grid (bounds from one shared helper, `gx` 8/4/2/1 by width, `gy = 64/gx`); same walk inside a tile; the even split keeps the trailing rows of non-tile-multiple images (a breaking stream-format change) | round-trips (CPU + GPU); 62/74 BC1 images improve, worst −2.9% (horizontal-grain wood), best +9%; CPU + GPU decompression inside noise | bc1 1.4676 → 1.4905 (+1.6%); bc4 1.1342 → 1.1351, bc5 1.0995 → 1.0985 (neutral); bc3 2.8199 → 2.8787 (+2.1%) | **kept — this is the new baseline** (tiles replace full-width strips; no header byte / API change, stream layout is versioned by the compressor) |
| 25 | Per-image tile-layout search (all formats) + per-tile local reference alphabet (BC1): trial-encode every `gx` 1..64 and keep the smallest stream; promote the 8/16/32 most frequent references to the front of a permuted global alphabet per tile, exact-size gated by a stream flag | round-trips (CPU + GPU, including a forced-local GPU case); ASan + Metal validation clean; CPU decode inside noise; encoder ~3.2x slower | bc1 1.4907 → 1.4993 (+0.58%); bc4 1.2839 → 1.2876 (+0.29%); bc5 1.0985 → 1.1007 (+0.20%); bc3 4.2240 → 4.2972 (+1.73%) | **reverted** — below the +1.0% combined target and not worth ~700 LoC + 3.2x encoder time; the local alphabet alone contributes ~+0.1% |

---

Expand Down Expand Up @@ -1058,6 +1059,93 @@ both are unmeasured, and the BC5 negative above is the stronger argument against

---

## 24. Stream-format validation from the plan's "16 color models" (the 18-model correction)

**Idea:** the header-compression plan describes the BC1 stream as "16 color models ... 19 models
total", and its H1 validation requires the top-table byte *multiset* to be identical between the
baseline and new streams (only the order may change). Built a scratch driver (session scratchpad,
not the repo) that walks 19 model headers, then reads the top-table size byte and hashes the
table entries, run over the whole suite with a baseline and a new build.

**Result:** the driver reported changed table multisets on 7 of the 74 BC1 images, and the
baseline stream appeared to hold a 16-entry table with an 8-symbol reference model while the
in-process encoder state said 255 entries — an apparent format corruption that no amount of
source reading reconciled (the encoder/decoder agree, every round-trip passes, the suite is
green).

**Root cause:** the stream actually carries **18** color models, not 16. The encoder and decoder
loops store per endpoint red×3 + green×3 + blue×3 (three context buckets each) = 9 per endpoint
× 2 endpoints = 18, then reference + mask + difference = **21 models total**. The "16" in the
plan (and in older README / code comments) is an arithmetic slip; the scratch driver inherited
it, so its walk ended 13 bytes early: its "reference model" was really the 11th color model, its
"table size byte" was the mask model's count byte (0x0f → "16 entries"), and the "table" it
hashed was the difference model's alphabet — plausible-looking garbage that differs between
builds only where the 255-byte header shrink shifts the window. With the corrected walk (21
models) **all 74 BC1 images have byte-identical table multisets** and identical per-component
payload bits, and the BC4/5 "diffs" were the same misparse (those formats have no top table;
their component-bit check had already passed for every image).

**Kept:** the 18 / 21 counts are corrected in the README and the code comments. Any tool that
parses the stream (the plan's validation, the tile plan's encoder trials, future stream
differs) must walk 21 models for BC1 — the model headers are self-describing, but a wrong model
count does not fail loudly: the drift lands mid-alphabet and reads plausible garbage until
something compares against ground truth.

---

## 25. Per-image tile layout search + per-tile local reference alphabet (implemented, measured, reverted)

**Idea (plan/tile_layout_and_local_reference.md):** two decode-cheap features sharing one
evaluation mechanism. (1) Exp 22 fixed an 8 x 8 tile grid; replace it with a per-image search:
try every `gx = 1, 2, 4, 8, 16, 32, 64` with `gx <= width_blocks`, `gy = 64 / gx`, encode the full
stream for each and emit the smallest (8 x 8 wins a tie, then the lower `layout_log2`). (2) BC1
per-tile local reference alphabet: count the references each tile emits, promote its 8 / 16 / 32
most frequent ones to the front of a permuted copy of the global alphabet, fit a local
`(k, q_escape)` and use it only when `2 + 8*S + 16 + local payload` beats the global payload.
A stream flag selects all-global (zero extra bits) or per-tile decisions; the encoder computes
both exact sizes and emits the smaller one.

**Stage 0 offline study** (`logs/study_layout_localref.csv`, the real pipeline with a forced
grid): per-tile reference entropy **7.043 bits** vs the global 7.49 (the concentration gate
passes), the local option wins on **61/74** images at 8 x 8, layout-only **+0.45%**, combined
**+0.514%** on the BC1 suite.

**Result (74-image BC1 suite + the BC4 / BC5 / BC3 suites, all CPU + GPU round-trips byte-exact):**

| suite | baseline (exp 22-24) | new | gain |
|---|---:|---:|---:|
| BC1 | 1.4907 | **1.4993** | **+0.58%** |
| BC4 | 1.2839 | **1.2876** | +0.29% |
| BC5 | 1.0985 | **1.1007** | +0.20% |
| BC3 | 4.2240 | **4.2972** | +1.73% |

- The BC1 gain clears the layout-only fallback bar (**>= +0.4%**) and the combined +0.5%
floor, but misses the combined +1.0% acceptance target. The local alphabet contributes only
about +0.1% on top of the layout search (the study predicted +0.06%); it is exact-size gated,
so it cannot regress a stream, but it is the first thing to drop if the format is simplified.
- Encoder cost: the full unit-test binary goes 11.7 s -> 36.9 s wall (~3.2x), all of it in
compression (8 trial encodes + 2 model inits per image). CPU decompression is inside noise
(ground.png 2323.5 -> 2351.6 MiB/s). No scratch buffer is needed: the winning layout is
re-encoded once at the end.
- Header: the layout byte has zero net cost in practice (it sits in the existing 4-byte header
padding); BC5 can grow by 2 bytes in the worst parity case, covered by `packed_bc5_maxsize`.
`packed_bc1_maxsize` / `packed_bc4_maxsize` are raised by 4 / 2 bytes for the same reason.
- Validation: ASan clean, Metal validation layers clean, BC1 CPU and GPU decode identical,
including a new synthetic GPU case that forces the local alphabet on.

**Reverted — not shipped.** The implementation worked (all CPU + GPU round-trips byte-exact,
ASan and Metal validation clean, deterministic), but the size gain does not justify the cost:
about **700 lines** changed in `src/bc_packed.c` (plus the Metal mirror and tests), a **3.2x**
encoder-time multiplier, a new header byte in every BC1 / BC4 / BC5 stream (BC3 twice), and a
format break (version 2.0.0) — for **+0.58%** on BC1, with the local-alphabet half of it worth
only ~+0.1%. This is below the plan's +1.0% combined acceptance target and too close to the
+0.5% floor to carry the complexity. The code, tests, shader changes and README updates were
reverted; the stage 0 study (`logs/study_layout_localref.csv`) and the numbers above remain as
the record. If this is ever revisited, start from the layout search alone (the +0.45% part) and
drop the per-tile local alphabet unless it grows beyond a marginal win.

---

## Open thread (not a failed experiment, just a lead)

The better-VQ-codebook lead is **resolved and closed**: exp. 13 measured the spatially-smooth
Expand Down
Binary file added images/bc4/Metal002_4K_Roughness.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/bc4/font_atlas.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
46 changes: 46 additions & 0 deletions logs/bc4_zip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# BC4 : bc_packed vs zip -9 baseline on raw standard BC4

General-purpose compressor baseline for the bc_packed BC4 stream ratios. Measured 2026-11-12
on the six-image `images/bc4` suite (v1.0.2); the bc_packed column re-measured 2026-11-13 on
v2.0.0 (endpoint MTF-8 + first-index model), and again 2026-11-13 on v2.1.0 (index-word
dictionary), which switches on for the font atlas and stays off for the five real-content maps.

**Method:** raw blocks built with stb_dxt v1.13 (`stb_compress_dxt_block`'s BC4 path, red
channel) exactly as the image suite builds them; `zip -9` per file; compressed size read from
the deflate entry (`unzip -v`), no container overhead. Ratio = raw bytes / compressed bytes —
the same numerator as the project's stream ratios, so the columns are directly comparable.

| Image | Size | Raw bytes | zip -9 bytes | zip ratio | bc_packed ratio | bc_packed stream bytes |
|---|---|---:|---:|---:|---:|---:|
| grey_roof_tiles_02_ao_1k.png | 1024×1024 | 524,288 | 504,935 | 1.0383 | 1.1592 | 452,300 |
| patterned_cobblestone_ao_1k.png | 1024×1024 | 524,288 | 516,041 | 1.0160 | 1.0920 | 480,104 |
| red_brick_ao_1k.png | 1024×1024 | 524,288 | 495,479 | 1.0581 | 1.1550 | 453,944 |
| rough_wood_ao_1k.png | 1024×1024 | 524,288 | 516,923 | 1.0142 | 1.1351 | 461,880 |
| Metal002_4K_Roughness.png | 4096×4096 | 8,388,608 | 8,031,124 | 1.0445 | 1.2195 | 6,878,820 |
| font_atlas.png | 1748×1748 | 1,527,752 | 596,663 | 2.5605 | 2.4040 | 635,504 |
| **average (per image)** | | | | **1.2886** | **1.3608** | |
| **byte-weighted** | | 12,013,512 | 10,661,165 | **1.1268** | **1.2831** | 9,362,552 |

## Notes

- v2.1.0 stream deltas: the five real-content maps gain only the always-present dictionary
slot (4-byte dummy rank model + 8-byte tile word, +12 B each; red_brick +16 B — its model
region lands 1 byte off the 4-byte alignment of the tile word), the font atlas drops
46,640 B.
- bc_packed beats zip -9 on the five real-content maps by +7.5% to +16.8% (byte-weighted
+13.9% across all six maps). AO / roughness content is ~99.9% unique 8-byte blocks
(red_brick: 65,442 / 65,536 distinct; the 4K roughness map: 1,047,560 / 1,048,576), which
leaves deflate no LZ material — only weak statistical compression. MTF-8 and the
index-word dictionary stay off for all five (the exact per-tile pricing keeps the plain
payload); their streams moved by header bytes only.
- zip -9 still wins on the font atlas, 2.5605 vs 2.4040: the v2.1.0 index-word dictionary
(enabled on the atlas tiles, one global rank model per channel) cut the gap from zip +14.3%
(v2.0.0) to +6.5%. The atlas's 57.5% byte-identical duplicate blocks (81,252 of 190,969
distinct, scattered in raster order) now cost a rank hit instead of a full index payload;
what remains is the escape payload of the unique words plus the endpoint stream, where
deflate's 32 KB window still reaches farther than a per-tile list of 255 words.
- The atlas is a small share of the raw bytes (12.7%), so its zip advantage no longer flips
the simple per-image average (the metric the image suite reports): bc_packed ahead by
+5.6% (1.3608 vs 1.2886), up from +3.5% before the dictionary. The README's "all
compression ratios are better than zlib" claim holds for the BC4 suite average, as for the
original four AO maps, BC1, BC5, and BC4 on a byte-weighted basis.
Loading
Loading