bcpack: read BC7 dictionary indices as the console's decoder does

The word after a BC7 dictionary entry's index widths is pixel 0's index,
not padding: the console's decoder (GDK BCPackDecompress) reads 32
indices starting there. Skipping it put every stored index one pixel
early, so BC7 blocks built from the dictionary decoded wrong (in the
origin fork's capture, 3.85 M of 4.70 M BC7 blocks of the title's
streams differed). With this reading, the decoder matches
BCPackDecompress byte for byte on a captured character-texture stream.

LARP_BCPACK_OLD_BC7=1 keeps the old reading for A/B. The switch is part
of the decoded-bytes store's identity, so cached entries of one reading
are never served to the other; the source change itself already
invalidates the store.

Ported from cool_fox 7243ed8f.
This commit is contained in:
ambrosia-works
2026-12-05 22:33:03 +01:00
parent 6887d56c3c
commit 7aecddd083
2 changed files with 59 additions and 7 deletions

View File

@ -137,9 +137,15 @@ impl Tables {
/// alpha 64 B and colour 256 B, BC4 64 B, BC5 64 B), 512 u16 BC7 control
/// words, then 255 dictionary entries per format in format order: BC1
/// 6 + 16 B, BC3 8 + 32 B, BC4 2 + 16 B, BC5 4 + 32 B, BC6H (u8 mode,
/// 12 u16, u8 mode, 16 i16), BC7 (u16 control, 24 u16, 2 u8, 2 B,
/// 16 + 15 i16). Little-endian, no padding. Every model must tile 65536.
/// 12 u16, u8 mode, 16 i16), BC7 (u16 control, 24 u16, 2 u8, 32 i16).
/// Little-endian, no padding. Every model must tile 65536.
pub fn parse(bytes: &[u8]) -> Result<Tables, Error> {
Tables::parse_with(bytes, old_bc7_reading())
}
/// [`Tables::parse`], with the BC7 dictionary's indices read as before
/// the fix when `old_bc7` is set (see [`old_bc7_reading`]).
pub fn parse_with(bytes: &[u8], old_bc7: bool) -> Result<Tables, Error> {
if bytes.len() != TABLES_SIZE {
return Err(Error("table file has the wrong size"));
}
@ -230,11 +236,23 @@ impl Tables {
bits: [r.u8(), r.u8()],
..Default::default()
};
// Unused: the widths again in all entries but the first.
r.u16();
// Only 15 slots for set 1, which no entry uses (0 bits).
for v in &mut ix.ix.as_flattened_mut()[..31] {
*v = r.u16() as i16;
// The 32 index slots follow the widths. The first word holds
// the widths again in all entries but the first, yet the
// console's decoder (GDK BCPack.dll, compared byte for byte on
// the title's streams) takes it as pixel 0's index: read the
// old way, the stored indices land one pixel early and BC7
// blocks built from the dictionary come out wrong. The last
// word spills into set 1, which no entry uses (0 bits).
if old_bc7 {
// The reading before the fix: skip the word, 31 indices.
r.u16();
for v in &mut ix.ix.as_flattened_mut()[..31] {
*v = r.u16() as i16;
}
} else {
for v in ix.ix.as_flattened_mut() {
*v = r.u16() as i16;
}
}
(ep, ix)
})
@ -260,6 +278,13 @@ impl Tables {
}
}
/// `LARP_BCPACK_OLD_BC7=1`: read the BC7 dictionary's indices as before the
/// fix (skip the word after the widths, 31 indices), for an A/B. Off by
/// default: that reading differs from the console's decoder.
pub fn old_bc7_reading() -> bool {
std::env::var_os("LARP_BCPACK_OLD_BC7").is_some_and(|v| v == "1")
}
/// Unpacks `src` (BC number `bc`, 1 and 3-7) into `dst`, whose length gives
/// the block count. The stream carries no size: bytes after the last one the
/// lanes need are ignored, a stream that runs out is an error.
@ -1168,3 +1193,28 @@ fn bc6<'a>(
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
/// The BC7 dictionary as the console's decoder reads it: the word after
/// the widths is pixel 0's index (the widths again in all entries but the
/// first), so set 1 holds only the spilled 32nd word. The old reading
/// (`LARP_BCPACK_OLD_BC7=1`) is the same words one pixel earlier.
#[test]
fn bc7_dictionary_indices_line_up() {
let (new, old) = (
Tables::parse_with(BUILTIN, false).unwrap(),
Tables::parse_with(BUILTIN, true).unwrap(),
);
for (k, ((_, ix), (_, was))) in new.bc7.iter().zip(&old.bc7).enumerate() {
assert!(ix.bits[1] != 0 || ix.ix[1][1..] == [0; 15], "entry {k}: {:?}", ix.ix);
if k > 0 {
let widths = i16::from(ix.bits[0]) | i16::from(ix.bits[1]) << 8;
assert_eq!(ix.ix[0][0], widths, "entry {k}");
}
assert_eq!(ix.ix.as_flattened()[1..], was.ix.as_flattened()[..31], "entry {k}");
}
}
}

View File

@ -173,6 +173,8 @@ fn identity() -> [u8; 32] {
hash.update(&(part.len() as u64).to_le_bytes());
hash.update(part);
}
// the A/B switch changes what the decoder writes for BC7
hash.update(&[u8::from(larp_formats::bcpack::old_bc7_reading())]);
*hash.finalize().as_bytes()
}