From f62e07c36ae955c5bb025ea70a731c8b693e38e0 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Mon, 19 Oct 2026 04:40:00 +0200 Subject: [PATCH] Read GPU readbacks without assuming the mapping's alignment A Vega 11 on Windows (Vulkan) panicked at launch in bytemuck's cast_slice with TargetAlignmentGreaterAndInputNotAligned, starting with the release that shipped occlusion culling. The section-test results and the GPU timer's timestamps were cast in place from the mapped range, which isn't guaranteed to be 4- or 8-byte aligned. Copy the words out instead. Fixes #953 Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/gfx/cull.rs | 6 ++++-- crates/horizon-client/src/gfx/timing.rs | 3 ++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/crates/horizon-client/src/gfx/cull.rs b/crates/horizon-client/src/gfx/cull.rs index 6926aab70..bcb738de3 100644 --- a/crates/horizon-client/src/gfx/cull.rs +++ b/crates/horizon-client/src/gfx/cull.rs @@ -273,7 +273,9 @@ impl Cull { } let mapped = r.buffer.slice(..r.slots.len() as u64 * 4); if let Ok(data) = mapped.get_mapped_range() { - let results: &[u32] = bytemuck::cast_slice(&data); + // The mapping isn't guaranteed to be 4-byte aligned (a Vega 11 + // on Windows panicked in cast_slice), so copy the words out. + let results: Vec = bytemuck::pod_collect_to_vec(&data); let (mut hidden, mut indices) = (0, 0u64); for (&result, &n) in results.iter().zip(&r.indices) { if result != r.stamp { @@ -283,7 +285,7 @@ impl Cull { } let share = indices as f32 / r.total.max(1) as f32; self.hidden = ([hidden, results.len()], share); - for (&slot, &result) in r.slots.iter().zip(results) { + for (&slot, &result) in r.slots.iter().zip(&results) { let slot = slot as usize; if slot >= self.seen.len() { self.seen.resize(slot + 1, false); diff --git a/crates/horizon-client/src/gfx/timing.rs b/crates/horizon-client/src/gfx/timing.rs index 5d2354234..8f8eee6ee 100644 --- a/crates/horizon-client/src/gfx/timing.rs +++ b/crates/horizon-client/src/gfx/timing.rs @@ -147,7 +147,8 @@ impl GpuTimer { continue; } if let Ok(data) = slot.buffer.slice(..).get_mapped_range() { - let stamps: &[u64] = bytemuck::cast_slice(&data); + // Read unaligned: the mapping isn't guaranteed to be 8-byte aligned. + let stamps: [u64; 2] = bytemuck::pod_read_unaligned(&data[..16]); let ticks = stamps[1].saturating_sub(stamps[0]); out.push((ticks as f64 * f64::from(self.period_ns) / 1e6) as f32); }