diff --git a/d3d12/crates/leonida-gpu/src/pipeline.rs b/d3d12/crates/leonida-gpu/src/pipeline.rs index 65b893df1b..808cd87c45 100644 --- a/d3d12/crates/leonida-gpu/src/pipeline.rs +++ b/d3d12/crates/leonida-gpu/src/pipeline.rs @@ -1159,9 +1159,12 @@ pub(crate) extern "system" fn create_stream( out: *mut This, ) -> HRESULT { // SAFETY: the title passes a description (null is tolerated). - let Some(desc) = (unsafe { desc.as_ref() }) else { + if desc.is_null() { return E_INVALIDARG; - }; + } + // SAFETY: checked non-null; the title's own description need not be + // aligned for a `StreamDesc`. + let desc = unsafe { desc.read_unaligned() }; if desc.stream.is_null() { return E_INVALIDARG; } @@ -1181,9 +1184,10 @@ pub(crate) extern "system" fn create_stream( break; }; if let Some(stage) = stage { - // SAFETY: a shader subobject has its bytecode at +8. - let code = unsafe { &*desc.stream.add(at + 8).cast::() }; - shaders.extend(copy(stage, code)); + // SAFETY: a shader subobject has its bytecode at +8; the title's + // own stream need not be aligned for a `Bytecode`. + let code = unsafe { desc.stream.add(at + 8).cast::().read_unaligned() }; + shaders.extend(copy(stage, &code)); } #[cfg(windows)] // SAFETY: a whole subobject of this kind lies at `at`. @@ -1474,4 +1478,72 @@ mod tests { ); } } + + /// A buffer the title owns: it need not be aligned for the stream in it. + #[repr(align(8))] + struct Aligned([u8; 0x2b8]); + + /// The title's own pipeline stream need not be aligned for the + /// subobjects in it: a misaligned stream reaches the same outcome as an + /// aligned one. + #[test] + fn a_stream_at_an_odd_address_yields_its_shaders() { + let (ms, ps) = ([9u8; 7], [4u8; 5]); + let mut stream = vec![0u8; 0x2b0]; + let mut put = |at: usize, v: u64| stream[at..at + 8].copy_from_slice(&v.to_le_bytes()); + for (at, kind) in [ + (0x10, 24), + (0x28, 2), + (0x40, 3), + (0x58, 4), + (0x70, 5), + (0x88, 8), + (0x1d8, 9), + (0x1e0, 10), + (0x210, 21), + (0x250, 17), + (0x260, 12), + (0x278, 14), + (0x280, 15), + (0x2a8, 16), + ] { + put(at, kind); + } + put(0x18, ms.as_ptr() as u64); + put(0x20, ms.len() as u64); + put(0x30, ps.as_ptr() as u64); + put(0x38, ps.len() as u64); + // Topology type 3 (triangles), one render target of format 28. + put(0x278, 14 | 3 << 32); + put(0x280, 15 | 28 << 32); + put(0x2a0, 1 << 32); + + // The same stream twice: once at an aligned address, once one + // byte into a buffer, an odd address for every subobject in it. + let mut aligned = Aligned([0u8; 0x2b8]); + aligned.0[..0x2b0].copy_from_slice(&stream); + let mut shifted = Aligned([0u8; 0x2b8]); + shifted.0[1..1 + 0x2b0].copy_from_slice(&stream); + let odd = unsafe { shifted.0.as_mut_ptr().add(1) }; + assert_eq!((odd as usize) % 8, 1); + + let ask = |stream: *const u8| { + let desc = StreamDesc { + size: 0x2b0, + stream, + }; + let mut p = std::ptr::null(); + let hr = create_stream(std::ptr::null(), &desc, &PIPELINE_IIDS[0], &mut p); + (hr, p) + }; + let (odd_hr, odd_p) = ask(odd); + let (aligned_hr, aligned_p) = ask(aligned.0.as_ptr()); + assert_eq!(odd_hr, S_OK); + assert_eq!(aligned_hr, S_OK); + assert_eq!(shaders(odd_p), shaders(aligned_p)); + assert_eq!( + shaders(odd_p), + [("mesh", ms.to_vec()), ("pixel", ps.to_vec())] + ); + } }