re: eng_02_l's real block is adjacent -- the connectivity heuristic rejects it

debug_find_index_buffer scans the container for an index buffer that validates
against a capture-proven vertex buffer. For eng_02_l nothing validates with the
connectivity test on; with it off the nearest hit is exact pad-0 adjacency
(vb-ib = 144 = 72*2). The block's mean_edge/diag is 0.417 against a 0.28 cap --
the documented false positive for coarse LODs, now caught with ground truth.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-08-12 02:31:53 +00:00
parent dde52c02e7
commit 845b63d7dd
3 changed files with 155 additions and 0 deletions

View File

@@ -0,0 +1,27 @@
//! Where could a resource's index buffer be, given the vertex buffer a runtime
//! capture proves the engine drew from? Tests the anchor scan's adjacency
//! assumption against ground truth.
use sylpheed_formats::mesh::{debug_find_index_buffer, debug_resource_params};
fn main() {
let a: Vec<String> = std::env::args().collect();
let bytes = std::fs::read(&a[1]).unwrap();
for pair in a[2..].iter() {
let (name, off) = pair.split_once('@').unwrap();
let vb = usize::from_str_radix(off.trim_start_matches("0x"), 16).unwrap();
let params = debug_resource_params(&bytes, name);
let markers = params.as_ref().map(|(m, _)| m.clone()).unwrap_or_default();
println!("{name} @ 0x{vb:x} markers={markers:?}");
let hits = debug_find_index_buffer(&bytes, name, vb);
if hits.is_empty() {
println!(" no index buffer anywhere in the container validates this block");
}
let mut hits = hits;
hits.sort_by_key(|(_, d)| d.abs());
for (ib, d) in hits.iter().take(8) {
println!(" ib 0x{ib:x} vb-ib = {d} bytes");
}
if hits.len() > 8 {
println!("{} total", hits.len());
}
}
}

View File

@@ -673,6 +673,100 @@ pub fn debug_try_anchor(
None
}
/// Diagnostic: for a resource whose vertex buffer is *known* (a runtime capture
/// names it), where could its index buffer be? The anchor scan assumes the index
/// buffer sits immediately before the vertex buffer; this scans the whole
/// container instead and returns every offset that validates as this resource's
/// index buffer. An empty result means the block is unreadable at that `vb` for
/// another reason; a hit far from `vb` means the adjacency assumption is what
/// fails. Returns `(ib_offset, signed distance vb - ib)` pairs.
pub fn debug_find_index_buffer(bytes: &[u8], name: &str, vb: usize) -> Vec<(usize, i64)> {
let Some(decl) = decl_of(bytes, name) else {
return Vec::new();
};
let markers = decl.1;
let decl = decl.0;
let Some(&(vtx_count, index_count)) = markers.first() else {
return Vec::new();
};
let mut out = Vec::new();
let end = bytes.len().saturating_sub(index_count * 2);
let mut ib = 0usize;
while ib < end {
// Cheap prefilter: the first few indices must be in range, and a real
// index buffer is not a run of zeros.
let ok = (0..6).all(|k| (be16(bytes, ib + k * 2) as usize) < vtx_count)
&& (0..6).any(|k| be16(bytes, ib + k * 2) != 0);
// `SOFT_IB=1` drops the connectivity requirement, to separate "no index
// buffer fits" from "our connectivity test is too strict".
let strict = std::env::var("SOFT_IB").is_err();
if ok && validate_block(bytes, ib, vb, vtx_count, index_count, &decl, 0.0, strict) {
out.push((ib, vb as i64 - ib as i64));
}
ib += 2;
}
out
}
/// Internal: the descriptor parameters the diagnostics need.
fn decl_of(bytes: &[u8], name: &str) -> Option<(VertexDecl, Vec<(usize, usize)>)> {
if bytes.len() < 16 || &bytes[..4] != b"XPR2" {
return None;
}
let mut cur = Cursor::new(bytes);
let header = Xpr2Header::read(&mut cur).ok()?;
const DIR_BASE: usize = 0x10;
for _ in 0..header.num_resources {
let Ok(e) = Xpr2ResourceEntry::read(&mut cur) else { break };
if &e.type_tag != b"XBG7" {
continue;
}
let desc = e.data_offset as usize + DIR_BASE;
let desc_end = (desc + e.descriptor_size as usize).min(bytes.len());
if desc >= bytes.len() || desc_end <= desc {
continue;
}
let rname = read_cstr(bytes, e.name_offset as usize + DIR_BASE)
.unwrap_or_else(|| "XBG7".to_string());
if rname != name {
continue;
}
let d = &bytes[desc..desc_end];
return Some((parse_vertex_decl(d)?, all_index_markers(d)));
}
None
}
/// Diagnostic: a resource's `(vtx_count, idx_count)` markers and vertex stride,
/// as the stage anchor scan reads them from the descriptor.
pub fn debug_resource_params(bytes: &[u8], name: &str) -> Option<(Vec<(usize, usize)>, usize)> {
if bytes.len() < 16 || &bytes[..4] != b"XPR2" {
return None;
}
let mut cur = Cursor::new(bytes);
let header = Xpr2Header::read(&mut cur).ok()?;
const DIR_BASE: usize = 0x10;
for _ in 0..header.num_resources {
let Ok(e) = Xpr2ResourceEntry::read(&mut cur) else { break };
if &e.type_tag != b"XBG7" {
continue;
}
let desc = e.data_offset as usize + DIR_BASE;
let desc_end = (desc + e.descriptor_size as usize).min(bytes.len());
if desc >= bytes.len() || desc_end <= desc {
continue;
}
let rname = read_cstr(bytes, e.name_offset as usize + DIR_BASE)
.unwrap_or_else(|| "XBG7".to_string());
if rname != name {
continue;
}
let d = &bytes[desc..desc_end];
return Some((all_index_markers(d), parse_vertex_decl(d)?.stride));
}
None
}
/// Diagnostic: the candidate vertex-buffer starts the stage anchor scan will
/// consider for a given `stride`, for one container. A runtime capture names the
/// offsets the engine really drew from (see `examples/shared_vbase_check.rs`), so