BC6H: spec-correct decoder in place of texture2ddecoder's #23

Manually merged
github merged 1 commit from gh-pr/23/head into gh-pr/23/base 2026-11-26 20:03:43 +00:00
Owner

@⁠holdmysocks on GitHub · 2026-11-23 01:35 UTC · original
holdmysocks:claude/interesting-mcclintock-bff8f2 → master · +243 −3 · 3 files · 1 commits · merged 2026-11-23 01:59 UTC

What

Plain BC6H textures (Fmt::Bc6u / Fmt::Bc6s, mostly HDR lighting textures) were decoded with texture2ddecoder::decode_bc6. This replaces that path with a decoder that follows the BC6H specification, for UF16 and SF16.

  • New crates/sixng-core/src/bc6h.rs: decode_block (half-float bits) and decode_image (same signature and 0xAARRGGBB output as texture2ddecoder::decode_bc6). It reads block fields with the existing bc6pack::unpack.
  • texture.rs: the two BC6 lines in to_rgba call it; f16 is now pub(crate).
  • lib.rs: registers the module.

export.rs and bc6pack.rs are not touched. The 8-bit conversion is unchanged (clamp to 0..1, truncate), so only the cases below differ.

Why

texture2ddecoder 0.1.2 differs from the specification in three places:

  1. Unsigned unquantize maps 1 << (bits - 1) to 0xFFFF. The specification maps (1 << bits) - 1 there, so half-range endpoints came out at full brightness.
  2. The 16-bit one-region mode (code 15) builds its endpoint mask (1 << 16) - 1 in u16. That is 0 in release builds, so the second endpoint decoded as 0.
  3. Signed blocks are treated as sign-magnitude after a two's complement sign extension, so negative endpoints came out wrong.

Checks

  • 5 unit tests in bc6h::tests: hand-computed values for cases 1 and 2 and for signed endpoints, image size handling, and random unsigned blocks that must equal texture2ddecoder exactly outside its bug cases. All 83 sixng-core tests pass.
  • Before/after on data/ttdb/lighting/lighting.ttpf --all, mip 0, previous release build against this branch:
Format Textures Changed
BC6H_UF16 401 234
BC6H_SF16 25 17
BC7 / BC1 730 0

The median changed texture differs in 0.06 % of its pixels, and 243 of the 251 have a pixel that differs by 64 levels or more. Two textures were garbled before and are clean now: 49794f046bee0618 (UF16, 1024², 98 % of pixels changed) and 60664f594ae618bc (SF16, 512², 94 %).

Notes for review

  • BC6H BCPack output goes through the same to_rgba, so it changes too. That was not checked separately.
  • Cached PNGs and thumbnails of BC6H textures from older builds stay wrong until they are decoded again.
  • 162a74fbc9e1c232 (SF16, 2048x1024) looks like noisy line art before and after; not looked into.
**@⁠holdmysocks** on GitHub · 2026-11-23 01:35 UTC · [original](https://github.com/holdmysocks/SixNG/pull/23) `holdmysocks:claude/interesting-mcclintock-bff8f2` → `master` · +243 −3 · 3 files · 1 commits · merged 2026-11-23 01:59 UTC ## What Plain BC6H textures (`Fmt::Bc6u` / `Fmt::Bc6s`, mostly HDR lighting textures) were decoded with `texture2ddecoder::decode_bc6`. This replaces that path with a decoder that follows the BC6H specification, for UF16 and SF16. - New `crates/sixng-core/src/bc6h.rs`: `decode_block` (half-float bits) and `decode_image` (same signature and `0xAARRGGBB` output as `texture2ddecoder::decode_bc6`). It reads block fields with the existing `bc6pack::unpack`. - `texture.rs`: the two BC6 lines in `to_rgba` call it; `f16` is now `pub(crate)`. - `lib.rs`: registers the module. `export.rs` and `bc6pack.rs` are not touched. The 8-bit conversion is unchanged (clamp to 0..1, truncate), so only the cases below differ. ## Why texture2ddecoder 0.1.2 differs from the specification in three places: 1. Unsigned unquantize maps `1 << (bits - 1)` to 0xFFFF. The specification maps `(1 << bits) - 1` there, so half-range endpoints came out at full brightness. 2. The 16-bit one-region mode (code 15) builds its endpoint mask `(1 << 16) - 1` in u16. That is 0 in release builds, so the second endpoint decoded as 0. 3. Signed blocks are treated as sign-magnitude after a two's complement sign extension, so negative endpoints came out wrong. ## Checks - 5 unit tests in `bc6h::tests`: hand-computed values for cases 1 and 2 and for signed endpoints, image size handling, and random unsigned blocks that must equal texture2ddecoder exactly outside its bug cases. All 83 `sixng-core` tests pass. - Before/after on `data/ttdb/lighting/lighting.ttpf --all`, mip 0, previous release build against this branch: | Format | Textures | Changed | |---|---|---| | BC6H_UF16 | 401 | 234 | | BC6H_SF16 | 25 | 17 | | BC7 / BC1 | 730 | 0 | The median changed texture differs in 0.06 % of its pixels, and 243 of the 251 have a pixel that differs by 64 levels or more. Two textures were garbled before and are clean now: `49794f046bee0618` (UF16, 1024², 98 % of pixels changed) and `60664f594ae618bc` (SF16, 512², 94 %). ## Notes for review - BC6H BCPack output goes through the same `to_rgba`, so it changes too. That was not checked separately. - Cached PNGs and thumbnails of BC6H textures from older builds stay wrong until they are decoded again. - `162a74fbc9e1c232` (SF16, 2048x1024) looks like noisy line art before and after; not looked into.
texture2ddecoder 0.1.2 decodes BC6H wrongly in three places: unsigned
unquantize maps 1 << (bits - 1) to 0xFFFF where the specification maps
(1 << bits) - 1; the 16-bit one-region mode builds its endpoint mask in
u16, which overflows to 0 in release builds, so the second endpoint is 0;
and signed blocks are treated as sign-magnitude.

bc6h.rs decodes blocks from bc6pack::unpack per the specification (UF16
and SF16) and texture.rs uses it for Fmt::Bc6u / Bc6s. The 8-bit output
convention is unchanged (clamp to 0..1, truncate).

On data/ttdb/lighting/lighting.ttpf, 234 of 401 BC6H_UF16 and 17 of 25
BC6H_SF16 mip-0 PNGs change; the other formats are byte-identical.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 20:03:41 +00:00
github manually merged commit 2979c57fde into gh-pr/23/base 2026-11-26 20:03:43 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.