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 <noreply@anthropic.com>
This commit is contained in:
@@ -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<u32> = 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);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user