Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions crates/xcl/src/hle/d3d12x/bind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ struct State {
/// untranslated pipeline – created with it – since SetGraphicsRootSignature binds nothing
rs_gfx: usize,
pso: usize,
/// 32-bit addresses of the view heap and the sampler heap set (0: none)
heaps: [u32; 2],
allocator: usize,
/// unified memory of this recording (io, argument tables)
uma: Vec<usize>,
Expand Down Expand Up @@ -82,6 +84,7 @@ pub(super) fn reset(list: *mut c_void, allocator: usize, initial_pso: usize) {
s.layout_gfx = None;
s.args_gfx.clear();
s.rs_gfx = 0;
s.heaps = [0; 2];
(old, std::mem::take(&mut s.uma))
});
console_list::hold_until_reset(old, blocks);
Expand All @@ -97,15 +100,16 @@ pub(super) fn clear_state(list: *mut c_void, pso: usize) {
s.layout_gfx = None;
s.args_gfx.clear();
s.rs_gfx = 0;
s.heaps = [0; 2];
});
}

/// Carry bound pipeline and root-signature / root-argument state from `from` to `to` across a
/// deferred mid-list split (`to` records into `allocator` with its own fresh UMA arena while
/// `from` retains its previously allocated blocks until `allocator` resets).
pub(super) fn inherit_for_split(from: *mut c_void, to: *mut c_void, allocator: usize) {
let (layout, args, layout_gfx, args_gfx, rs_gfx, pso) = with(from, |s| {
(s.layout.clone(), s.args.clone(), s.layout_gfx.clone(), s.args_gfx.clone(), s.rs_gfx, s.pso)
let (layout, args, layout_gfx, args_gfx, rs_gfx, pso, heaps) = with(from, |s| {
(s.layout.clone(), s.args.clone(), s.layout_gfx.clone(), s.args_gfx.clone(), s.rs_gfx, s.pso, s.heaps)
});
let (old, blocks) = with(to, |s| {
let old = std::mem::replace(&mut s.allocator, allocator);
Expand All @@ -116,6 +120,7 @@ pub(super) fn inherit_for_split(from: *mut c_void, to: *mut c_void, allocator: u
s.args_gfx = args_gfx;
s.rs_gfx = rs_gfx;
s.pso = pso;
s.heaps = heaps;
(old, std::mem::take(&mut s.uma))
});
console_list::hold_until_reset(old, blocks);
Expand Down Expand Up @@ -151,6 +156,16 @@ pub(crate) fn remove_state_for_test(list: *mut c_void) {
STATES.lock().unwrap().as_mut().and_then(|m| m.remove(&(list as usize)));
}

/// SetDescriptorHeaps: the console addresses of `heaps`, for shaders that index them themselves
/// (`Layout::heaps`).
pub(super) fn set_heaps(list: *mut c_void, heaps: &[usize]) {
let mut bases = [0u32; 2];
for (sampler, base) in heaps.iter().filter_map(|&h| super::console_heap::base(h)) {
bases[sampler as usize] = base;
}
with(list, |s| s.heaps = bases);
}

/// bytes of parameter `i` at constant `offset` (of the compute or the graphics arguments)
fn put_in(list: *mut c_void, gfx: bool, i: u32, offset: u32, bytes: &[u8]) {
with(list, |s| {
Expand Down Expand Up @@ -888,7 +903,10 @@ unsafe fn bind_abi(this: *mut c_void, list: &ID3D12GraphicsCommandList, pso: usi
let u = crate::gpu::uma::get().ok_or("no unified memory")?;
let windows = super::real::uma_window_vas();
let first = *windows.first().ok_or("unified memory is not open on the GPU")?;
let (layout, args) = with(this, |s| if gfx { (s.layout_gfx.clone(), s.args_gfx.clone()) } else { (s.layout.clone(), s.args.clone()) });
let (layout, args, heaps) = with(this, |s| {
let (layout, args) = if gfx { (&s.layout_gfx, &s.args_gfx) } else { (&s.layout, &s.args) };
(layout.clone(), args.clone(), s.heaps)
});
// 32-bit console addresses: window 0; pieces of the list's arena (not a 64 KiB grain each)
let copy = |bytes: &[u8]| -> Result<usize, String> {
let p = with(this, |s| s.arena.take(u, bytes.len().max(16), 256, &mut s.uma)).ok_or("unified memory is full")?;
Expand All @@ -902,8 +920,8 @@ unsafe fn bind_abi(this: *mut c_void, list: &ID3D12GraphicsCommandList, pso: usi
let inline = layout.as_ref().map_or(0, |l| if gfx { l.inline_gfx } else { console_list::inline_for(l, user_sgprs) } as usize).min(args.len());
let table_at = copy(&args[inline..])?;
let mut io = match layout.as_ref() {
Some(l) if gfx => console_list::user_data_gfx(l, &args, table_at as u64),
Some(l) => console_list::user_data_for(l, user_sgprs, &args, table_at as u64),
Some(l) if gfx => console_list::with_heaps(l, None, console_list::user_data_gfx(l, &args, table_at as u64), heaps),
Some(l) => console_list::with_heaps(l, user_sgprs, console_list::user_data_for(l, user_sgprs, &args, table_at as u64), heaps),
None => vec![table_at as u32, (table_at >> 32) as u32],
};
io.resize(crate::gpu::recomp::KERNEL_SGPRS, 0);
Expand Down
5 changes: 5 additions & 0 deletions crates/xcl/src/hle/d3d12x/console_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,11 @@ pub fn unregister(obj: usize) {
}
}

/// Whether heap `obj` holds samplers, and the 32-bit console address of its slots.
pub fn base(obj: usize) -> Option<(bool, u32)> {
HEAPS.lock().unwrap().iter().find(|h| h.obj == obj).map(|h| (h.sampler, h.shadow as u32))
}

/// Start of the console slots of heap `obj` (CPU handle = GPU handle on the console).
pub fn start(obj: usize) -> Option<u64> {
HEAPS.lock().unwrap().iter().find(|h| h.obj == obj).map(|h| h.shadow as u64)
Expand Down
53 changes: 52 additions & 1 deletion crates/xcl/src/hle/d3d12x/console_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,15 @@ pub struct Layout {
pub size: u32,
pub inline: u32,
pub inline_gfx: u32,
/// the shaders index the view heap / the sampler heap themselves (the root signature's
/// CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED / SAMPLER_HEAP_DIRECTLY_INDEXED): they take the 32-bit
/// address of each from a user SGPR after the arguments, the view heap's first
pub heaps: [bool; 2],
}

const CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED: u32 = 0x400;
const SAMPLER_HEAP_DIRECTLY_INDEXED: u32 = 0x800;

/// User SGPRs the console's compiler gives root arguments: compute s4…s15 (s0…s3 are the
/// driver's), graphics stages (VS, PS) s8…s31 (s0…s7: the driver's, s[4:5] of a VS the
/// fetch shader) – both measured on its output (`tests/console_shaders.rs`).
Expand All @@ -100,7 +107,8 @@ pub fn layout_from_blob(blob: &[u8]) -> Option<Layout> {
};
let r = |o: usize| rts0.get(o..o + 4).map(|b| u32::from_le_bytes(b.try_into().unwrap()));
let (n, poff) = (r(4)? as usize, r(8)? as usize);
let mut l = Layout::default();
let flags = r(20).unwrap_or(0);
let mut l = Layout { heaps: [CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED, SAMPLER_HEAP_DIRECTLY_INDEXED].map(|f| flags & f != 0), ..Default::default() };
// the parameters go into user SGPRs in order while each fits whole; from the first one
// that does not, the rest go to the table (packed from its start) – as the console's
// compiler expects them (HANDOFF: root argument binding)
Expand Down Expand Up @@ -170,6 +178,19 @@ pub fn user_data_gfx(l: &Layout, args: &[u8], table_at: u64) -> Vec<u32> {
sgprs(FIRST_SGPR_GFX, l.inline_gfx, args, table_at)
}

/// `sgprs` (`user_data_for` / `user_data_gfx`) and the addresses of the view heap and the sampler
/// heap set that the shaders index (`Layout::heaps`). A compute shader without registers left
/// for them (`user_sgprs`: its count) gets none.
pub fn with_heaps(l: &Layout, user_sgprs: Option<u32>, mut sgprs: Vec<u32>, heaps: [u32; 2]) -> Vec<u32> {
for (_, base) in l.heaps.iter().zip(heaps).filter(|(&used, _)| used) {
if user_sgprs.is_some_and(|n| sgprs.len() as u32 >= n) {
break;
}
sgprs.push(base);
}
sgprs
}

fn sgprs(first: usize, inline: u32, args: &[u8], table_at: u64) -> Vec<u32> {
let mut s = vec![0u32; first];
s[0] = table_at as u32;
Expand Down Expand Up @@ -973,6 +994,36 @@ mod tests {
r.iter().flat_map(|w| w.to_le_bytes()).collect()
}

/// Shaders that index the heaps set themselves get the heaps' addresses after their
/// arguments, the view heap's first; a compute shader without registers for them does not.
#[test]
fn heap_addresses_follow_the_arguments() {
let layout = |flags: u32, p: &[(u32, u32)]| {
let mut blob = rts0(p);
blob[20..24].copy_from_slice(&flags.to_le_bytes());
layout_from_blob(&blob).unwrap()
};
let heaps = [0x6000_0000, 0x6381_0000];
// a table and two constants
let args: Vec<u8> = [0x80u32, 5, 0].iter().flat_map(|w| w.to_le_bytes()).collect();
let gfx = |flags: u32| {
let l = layout(flags, &[(0, 0), (1, 2)]);
with_heaps(&l, None, user_data_gfx(&l, &args, 0x200), heaps)[8..].to_vec()
};
assert_eq!(gfx(1), [0x80, 5, 0]);
assert_eq!(gfx(0x801), [0x80, 5, 0, 0x6381_0000]);
assert_eq!(gfx(0x401), [0x80, 5, 0, 0x6000_0000]);
assert_eq!(gfx(0xC01), [0x80, 5, 0, 0x6000_0000, 0x6381_0000]);
let cs = |flags: u32, user: u32| {
let l = layout(flags, &[(0, 0), (1, 2)]);
with_heaps(&l, Some(user), user_data_for(&l, Some(user), &args, 0x200), heaps)[4..].to_vec()
};
assert_eq!(cs(0xC00, 9), [0x80, 5, 0, 0x6000_0000, 0x6381_0000]);
assert_eq!(cs(0xC00, 8), [0x80, 5, 0, 0x6000_0000]);
assert_eq!(cs(0xC00, 7), [0x80, 5, 0], "no register left");
assert_eq!(cs(0, 9), [0x80, 5, 0]);
}

/// A blob whose constant count makes the arguments overflow u32 (0x4000_0000) or take
/// gigabytes (0x3FFF_FFFF: 0xFFFF_FFFC bytes, allocated at every Set*RootSignature) has no
/// layout; the largest one allowed still has one.
Expand Down
1 change: 1 addition & 0 deletions crates/xcl/src/hle/d3d12x/list_x.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3469,6 +3469,7 @@ pub(super) unsafe fn ensure_view_heap(this: *mut c_void) {
pub(super) unsafe extern "system" fn set_descriptor_heaps_x(this: *mut c_void, n: u32, heaps: *const usize) {
const PC_SET_DESCRIPTOR_HEAPS: usize = 28;
let f = pc::<unsafe extern "system" fn(*mut c_void, u32, *const usize)>(this, PC_SET_DESCRIPTOR_HEAPS);
super::bind::set_heaps(this, if heaps.is_null() { &[] } else { std::slice::from_raw_parts(heaps, n as usize) });
if heaps.is_null() || n == 0 {
// no heaps: nothing bound any more (`ensure_view_heap` binds the shared heap again
// before a translated draw or dispatch reads the memory windows from it)
Expand Down
Loading