Skip to content
Closed
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
8 changes: 8 additions & 0 deletions docs/textures.md
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,14 @@ each mip with a 2x2 box downsample of its parent (at 4x4 block resolution for BC
resolution for uncompressed). A correct chain is 0–4 levels, as in the SixNG notes; the
control is the first tail mip detiled at origin (0,0).

Signed BC6H final scaling truncates toward zero before assigning the half-float sign:
an interpolated integer of -1 therefore becomes positive zero (`0x0000`), not negative
zero (`0x8000`). This follows [Microsoft's BC6H finalization algorithm](https://learn.microsoft.com/en-us/windows/win32/direct3d11/bc6h-format#unquantization-of-color-endpoints).
`tests/test_ttpf_mips.py` checks the signed finite range and the full unsigned range
against an independent signed-integer scaling oracle, plus synthetic mode-14 blocks
crossing zero at every interpolation weight. The separate signed minimum-endpoint
behavior is tracked in [#6120](https://github.com/ItsNotRexed/Leonida/pull/6120).

| Pool # | Descriptor | Mips | non_tail / first_tail | Max error | Tail control |
|---|---|---:|---:|---:|---:|
| `interiors_0_3` #19 | 512×256 BC1 | 7 | 1 / 3 | 0.38 | 126.06 |
Expand Down
35 changes: 35 additions & 0 deletions tests/test_ttpf_mips.py
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,41 @@ def test_bc6h_modes_uf16_sf16_and_reserved(self):
self.assertGreater(px1e[0][0], 0.0)
self.assertEqual(px1e[2][0], -px1e[0][0])

def test_bc6h_finish_scaling_matches_signed_integer_oracle(self):
# Microsoft BC6H: truncate the signed scale toward zero, then encode
# the sign of that integer, not the sign before scaling.
# https://learn.microsoft.com/en-us/windows/win32/direct3d11/bc6h-format
lut = ttpf._f16_lut()
# The separate -32768 endpoint/clamping defect is covered by PR #6120.
for value in range(-32767, 32768):
scaled = int(value * 31 / 32)
expected = abs(scaled) + (0x8000 if scaled < 0 else 0)
actual = struct.unpack("<H", struct.pack("<e", ttpf._bc6h_finish(value, True, lut)))[0]
self.assertEqual(actual, expected, f"signed interpolant {value}")
for value in range(65536):
expected = value * 31 // 64
actual = struct.unpack("<H", struct.pack("<e", ttpf._bc6h_finish(value, False, lut)))[0]
self.assertEqual(actual, expected, f"unsigned interpolant {value}")

def test_bc6h_signed_mode14_zero_crossing_interpolants(self):
weights = (0, 4, 9, 13, 17, 21, 26, 30, 34, 38, 43, 47, 51, 55, 60, 64)
for base in range(-8, 9):
for delta in range(-8, 8):
block = pack_bc6h_block(
0x0F, fields=[[base & 0xFFFF, delta & 0xF, 0, 0],
[0, 0, 0, 0], [0, 0, 0, 0]],
indices=range(16),
)
pixels = ttpf.decode_pixels(96, block, 1, 1)
expected = []
for weight in weights:
interpolant = (base * 64 + delta * weight + 32) // 64
scaled = int(interpolant * 31 / 32)
expected.append(abs(scaled) + (0x8000 if scaled < 0 else 0))
words = [struct.unpack("<3H", struct.pack("<3e", *pixel)) for pixel in pixels]
self.assertEqual(words, [(word, 0, 0) for word in expected],
f"base {base}, delta {delta}")

def test_hdr_tonemap_metric(self):
same = [(0.0, 1.0, 500.0), (1000.0, 0.25, 65504.0)]
self.assertEqual(ttpf.mean_abs_error(same, same, hdr=True), 0.0)
Expand Down
2 changes: 1 addition & 1 deletion tools/ttpf_mips.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ def _bc6h_finish(val, signed, lut):
h = (v * 31) >> 5
if h > 0x7BFF:
h = 0x7BFF
return lut[h | 0x8000] if neg else lut[h]
return lut[h | 0x8000] if neg and h else lut[h]


def decode_bc6h_block(data, at, *, signed=False):
Expand Down