From 6603fdcb7b28f7483a359479ed1d45863cdbc969 Mon Sep 17 00:00:00 2001 From: Moritz Friedrich <104226130+storrmzz@users.noreply.github.com> Date: Sun, 29 Nov 2026 00:46:05 +0100 Subject: [PATCH] D3D12.X: copy root constants without a temporary buffer --- crates/xcl/src/hle/d3d12x/bind.rs | 75 +++++++++++++++++++++++++++++-- 1 file changed, 71 insertions(+), 4 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/bind.rs b/crates/xcl/src/hle/d3d12x/bind.rs index b50b835091..ef3efe4a64 100644 --- a/crates/xcl/src/hle/d3d12x/bind.rs +++ b/crates/xcl/src/hle/d3d12x/bind.rs @@ -1476,8 +1476,9 @@ pub(super) unsafe extern "system" fn set_graphics_root_constants(this: *mut c_vo let _detail = crate::stall::detail_span(crate::stall::StallKind::DetailBind); let _t = crate::hle::api_cost::time_if(super::look_stats::deep(), crate::hle::api_cost::Call::BindState); if !data.is_null() { - let bytes: Vec = std::slice::from_raw_parts(data, n as usize).iter().flat_map(|w| w.to_le_bytes()).collect(); - put_in(this, true, i, offset, &bytes); + // Windows x64 words are already little-endian; put_in copies them before returning. + let bytes = std::slice::from_raw_parts(data.cast::(), n as usize * std::mem::size_of::()); + put_in(this, true, i, offset, bytes); } } @@ -4550,8 +4551,9 @@ pub(super) unsafe extern "system" fn set_compute_root_constants(this: *mut c_voi let _detail = crate::stall::detail_span(crate::stall::StallKind::DetailBind); let _t = crate::hle::api_cost::time_if(super::look_stats::deep(), crate::hle::api_cost::Call::BindState); if !data.is_null() { - let bytes: Vec = std::slice::from_raw_parts(data, n as usize).iter().flat_map(|w| w.to_le_bytes()).collect(); - put(this, i, offset, &bytes); + // Windows x64 words are already little-endian; put copies them before returning. + let bytes = std::slice::from_raw_parts(data.cast::(), n as usize * std::mem::size_of::()); + put(this, i, offset, bytes); } } @@ -5284,6 +5286,71 @@ mod tests { use std::sync::mpsc; use std::time::Duration; + #[test] + fn root_constants_copy_words_at_offsets_and_own_the_result() { + let key = Box::new(0u8); + let list = (&*key as *const u8).cast_mut().cast::(); + let layout = Arc::new(console_list::Layout { + params: vec![Param::Constants { at: 0, count: 8 }, Param::Constants { at: 32, count: 8 }], + size: 64, + ..Default::default() + }); + with(list, |s| { + s.layout = Some(layout.clone()); + s.layout_gfx = Some(layout); + s.args = vec![0xA5; 64]; + s.args_gfx = vec![0xA5; 64]; + }); + for gfx in [false, true] { + let setter = if gfx { set_graphics_root_constants } else { set_compute_root_constants }; + for parameter in 0..2u32 { + for n in 0..=8u32 { + for offset in 0..=8 - n { + with(list, |s| { s.args.fill(0xA5); s.args_gfx.fill(0xA5); }); + let mut words = [0x0123_4567, 0x89AB_CDEF, 0, u32::MAX, 0x8000_0000, 1, 0xFF00_FF00, 0x00FF_00FF]; + let mut expected = vec![0xA5; 64]; + let at = (parameter * 32 + offset * 4) as usize; + for (dest, word) in expected[at..at + n as usize * 4].chunks_exact_mut(4).zip(&words) { + dest.copy_from_slice(&word.to_le_bytes()); + } + unsafe { setter(list, parameter, n, words.as_ptr(), offset); } + words.fill(0); + std::hint::black_box(&words); + with(list, |s| { + let (written, other) = if gfx { (&s.args_gfx, &s.args) } else { (&s.args, &s.args_gfx) }; + assert_eq!(written, &expected, "gfx={gfx} parameter={parameter} n={n} offset={offset}"); + assert_eq!(other, &[0xA5; 64]); + }); + } + } + } + } + drop(STATES.remove(list as usize)); + } + + #[test] + fn root_constants_null_and_invalid_parameter_leave_arguments_unchanged() { + let key = Box::new(0u8); + let list = (&*key as *const u8).cast_mut().cast::(); + let layout = Arc::new(console_list::Layout { + params: vec![Param::Constants { at: 0, count: 4 }], size: 16, ..Default::default() + }); + with(list, |s| { + s.layout = Some(layout.clone()); s.layout_gfx = Some(layout); + s.args = vec![0xA5; 16]; s.args_gfx = vec![0xA5; 16]; + }); + for setter in [set_graphics_root_constants, set_compute_root_constants] { + unsafe { + setter(list, 0, 0, std::ptr::null(), 0); + setter(list, 0, 4, std::ptr::null(), 0); + setter(list, 1, 1, &0x1234_5678, 0); + setter(list, 0, 1, &0x1234_5678, 4); + } + } + with(list, |s| { assert_eq!(s.args, [0xA5; 16]); assert_eq!(s.args_gfx, [0xA5; 16]); }); + drop(STATES.remove(list as usize)); + } + #[test] fn aligned_list_addresses_reach_many_bind_shards() { let shards = (0..256).map(|index| state_shard_index(0x7100_0000 + index * 16)).collect::>();