Measure the 0x80 alpha convention on the disc instead of asserting it in prose

The convention has been carried by comments and an inline expression. This
measures it, across every decoded texture on all 27 levels.

The distribution is what the GS alpha rules predict, and it is strongly bimodal:

    alpha 255   22,690,698 texels
    alpha 0        227,497
    a long tail of partial alpha between them

So the 0x80 -> opaque rule is not a rare corner case on this disc; it is what
almost every texel does, and the interesting content is entirely in the tail.

The useful part is the tail. It contains 253 -- twice -- and odd values across
the upper half: 129, 131, 133 ... 251, 253. 254 does not appear, and neither does
any other even-only pattern above 0x80.

That is the discriminator. The old texture::scale_alpha was `a * 2`, so every
value it produced was even by construction, and a raw CLUT alpha of 0x7F would
have decoded to 254. The corrected rule gives 253. The histogram therefore
discriminates between two expressions of one rule using disc data rather than
assertion, which is the point: this test fails if the two ever drift apart again,
and it would have failed on the old code.

tfrag_textures_decode_to_plausible_images now asserts that every decoded alpha is
a value gs::alpha_to_8 can produce, and that at least one fully opaque texel
decodes -- so if the 0x80 rule is ever removed rather than altered, the test says
so rather than quietly passing.

Worth noting what this histogram is not. It is what the decoder produced, not an
independent oracle -- the disc does not carry a reference decode of itself. What it
establishes is that the correction is consistent with the data the disc actually
contains, and more usefully, that the old expression was producing a value the
data has no reason to contain. Two texels out of 23 million is not much of a
sample, but it is the whole population of the case that matters.

The UV convention, checked next, has exactly one implementation and needed no
change. Not every duplicated rule I went looking for turned out to be duplicated.
This commit is contained in:
2026-10-14 04:38:02 +02:00
parent 82d7c4f8a7
commit 777a6a8d47
+28
View File
@@ -748,6 +748,7 @@ fn tfrag_textures_decode_to_plausible_images() {
}
let mut decoded = 0usize;
let mut alphas: std::collections::BTreeMap<u8, usize> = std::collections::BTreeMap::new();
let mut opaque = 0usize;
let mut multi_colour = 0usize;
let mut flat = 0usize;
@@ -801,6 +802,9 @@ fn tfrag_textures_decode_to_plausible_images() {
if op == texels as usize {
opaque += 1;
}
for px in t.rgba.chunks_exact(4) {
*alphas.entry(px[3]).or_insert(0usize) += 1;
}
}
}
@@ -822,6 +826,30 @@ fn tfrag_textures_decode_to_plausible_images() {
opaque_pct * 100.0,
detail_pct * 100.0
);
// The 0x80 convention, measured on the disc rather than asserted in prose.
// Every decoded alpha must be a value gs::alpha_to_8 can produce -- and 254
// cannot be produced by it, since (v*255+64)/128 never lands on 254 for an
// integer v. 254 is precisely what the old `a * 2` produced for a raw 0x7F,
// so this line fails if that bug is ever reintroduced or the two expressions
// of the rule drift apart again.
let legal: std::collections::HashSet<u8> =
(0..=255u8).map(gc_formats::gs::alpha_to_8).collect();
let illegal: Vec<(u8, usize)> = alphas
.iter()
.filter(|(a, _)| !legal.contains(a))
.map(|(a, n)| (*a, *n))
.collect();
eprintln!("decoded alpha histogram: {alphas:?}");
assert!(
illegal.is_empty(),
"{illegal:?} are not producible by gs::alpha_to_8 -- the CLUT alpha \
conversion and the GS rule have diverged again"
);
assert!(
alphas.contains_key(&0xff),
"no fully opaque texel decoded; the 0x80 -> opaque rule is not exercised"
);
}
/// LOD linkage: every LOD-01 and LOD-0 vertex-info entry must resolve to a