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
21 changes: 16 additions & 5 deletions crates/xcl/src/gpu/recomp/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@ pub fn run_with_statics(
tex_back.push((i, rb));
}
list.Close().map_err(e)?;
gpu.queue.ExecuteCommandLists(&[Some(list.cast().map_err(e)?)]);
submit_list(&gpu.queue, &list)?;
gpu.value += 1;
wait_for_submission(dev, &gpu.queue, &gpu.fence, gpu.value, "kernel dispatch")?;
let mut p = std::ptr::null_mut();
Expand Down Expand Up @@ -1222,7 +1222,7 @@ fn draw_indexed_on(
};
list.CopyTextureRegion(&dst, 0, 0, 0, &src, None);
list.Close().map_err(e)?;
queue.ExecuteCommandLists(&[Some(list.cast().map_err(e)?)]);
submit_list(&queue, &list)?;
wait_for_submission(dev, &queue, &fence, 1, "draw")?;
let mut p = std::ptr::null_mut();
readback.Map(0, None, Some(&mut p)).map_err(e)?;
Expand Down Expand Up @@ -1425,7 +1425,7 @@ pub fn mesh_draw_split(
list.CopyTextureRegion(&dst, 0, 0, 0, &src, None);
list.CopyBufferRegion(&readback, total, &res_buf, 0, res_bytes);
list.Close().map_err(e)?;
gpu.queue.ExecuteCommandLists(&[Some(list.cast().map_err(e)?)]);
submit_list(&gpu.queue, &list)?;
gpu.value += 1;
wait_for_submission(dev, &gpu.queue, &gpu.fence, gpu.value, "mesh draw")?;
let mut q = std::ptr::null_mut();
Expand Down Expand Up @@ -1561,7 +1561,7 @@ fn run_in_region(dxil: &[u8], state: &KernelState, arena: &Arena) -> Result<Kern
}
list.CopyBufferRegion(&readback, 0, &res_buf, 0, res_bytes);
list.Close().map_err(e)?;
gpu.queue.ExecuteCommandLists(&[Some(list.cast().map_err(e)?)]);
submit_list(&gpu.queue, &list)?;
gpu.value += 1;
wait_for_submission(dev, &gpu.queue, &gpu.fence, gpu.value, "arena dispatch")?;
let mut q = std::ptr::null_mut();
Expand Down Expand Up @@ -1779,7 +1779,7 @@ fn dispatch_with(
list.ResourceBarrier(&[transition(&mem_buf, D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COPY_SOURCE)]);
list.CopyBufferRegion(&readback, 0, &mem_buf, 0, mem_bytes);
list.Close().map_err(e)?;
queue.ExecuteCommandLists(&[Some(list.cast().map_err(e)?)]);
submit_list(queue, &list)?;
*value += 1;
wait_for_submission(dev, queue, fence, *value, "compute dispatch")?;
let mut q = std::ptr::null_mut();
Expand All @@ -1798,3 +1798,14 @@ fn dispatch_with(
Ok((out, ms))
}
}

/// Submit `list` to `queue`. Under Wine, if another test (e.g. `real::init()`) adopted the
/// process-wide singleton `ID3D12Device` after `queue` was created, `list` carries XCL's
/// extended vtable while `queue` does not go through `real::q_execute_command_lists`, so
/// restore `list`'s native vtable for the call.
unsafe fn submit_list(queue: &ID3D12CommandQueue, list: &ID3D12GraphicsCommandList) -> Result<(), String> {
let cmd: ID3D12CommandList = list.cast().map_err(e)?;
let _guard = crate::hle::d3d12x::real::UnadoptGuard::new(cmd.as_raw());
queue.ExecuteCommandLists(&[Some(cmd)]);
Ok(())
}
26 changes: 20 additions & 6 deletions crates/xcl/src/hle/d3d12x/alloc_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ pub unsafe fn executed(queue: *mut c_void, lists: &[*mut c_void]) {
let key = queue as usize;
if !s.fences.contains_key(&key) {
let mut dev: Option<ID3D12Device> = None;
let Some(f) = q.GetDevice(&mut dev).ok().and(dev).and_then(|d| d.CreateFence::<ID3D12Fence>(0, D3D12_FENCE_FLAG_NONE).ok()) else { return };
let Some(f) = q.GetDevice(&mut dev).ok().and(dev).and_then(|d| super::real::create_raw_fence(&d).ok()) else { return };
s.fences.insert(key, (f, 0));
}
let (fence, value) = {
Expand Down Expand Up @@ -452,12 +452,15 @@ mod tests {
if !super::super::real::is_adopted(a) {
super::super::real::adopt(a, super::super::real::Kind::CommandAllocator);
}
let list: ID3D12GraphicsCommandList = dev.CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, &alloc, None).unwrap();
let list: ID3D12GraphicsCommandList = {
let _alloc_guard = super::super::real::UnadoptGuard::new(a);
dev.CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, &alloc, None).unwrap()
};
let l = list.as_raw();
created_on(l as usize, a as usize, D3D12_COMMAND_LIST_TYPE_DIRECT.0);
list.Close().unwrap();
let lists = [Some(list.cast::<ID3D12CommandList>().unwrap())];
let fence: ID3D12Fence = dev.CreateFence(0, D3D12_FENCE_FLAG_NONE).unwrap();
let fence: ID3D12Fence = super::super::real::create_raw_fence(&dev).unwrap();
let done = |v: u64| {
queue.Signal(&fence, v).unwrap();
let start = std::time::Instant::now();
Expand All @@ -467,7 +470,10 @@ mod tests {
assert!(fence.GetCompletedValue() >= v, "the queue finishes");
};

queue.ExecuteCommandLists(&lists);
{
let _list_guard = super::super::real::UnadoptGuard::new(l);
queue.ExecuteCommandLists(&lists);
}
executed(queue.as_raw(), &[l]);
done(1);

Expand All @@ -476,7 +482,10 @@ mod tests {
let second = current(a as usize);
assert_ne!(second, a as usize, "the list still holds its recording: another PC allocator takes the place");
// … and submits the recording again
queue.ExecuteCommandLists(&lists);
{
let _list_guard = super::super::real::UnadoptGuard::new(l);
queue.ExecuteCommandLists(&lists);
}
executed(queue.as_raw(), &[l]);
done(2);
assert!(dev.GetDeviceRemovedReason().is_ok(), "the recording was still there");
Expand All @@ -485,10 +494,14 @@ mod tests {
let second_alloc = ID3D12CommandAllocator::from_raw_borrowed(&(second as *mut c_void)).unwrap().clone();
assert_eq!(reset_list(l as usize, a as usize, D3D12_COMMAND_LIST_TYPE_DIRECT.0, |pc| {
assert_eq!(pc, second);
let _alloc_guard = super::super::real::UnadoptGuard::new(pc as *mut c_void);
list.Reset(&second_alloc, None).map_or_else(|e| e.code().0, |_| 0)
}), 0);
list.Close().unwrap();
queue.ExecuteCommandLists(&lists);
{
let _list_guard = super::super::real::UnadoptGuard::new(l);
queue.ExecuteCommandLists(&lists);
}
executed(queue.as_raw(), &[l]);
done(3);

Expand All @@ -501,6 +514,7 @@ mod tests {
// it stays (the next allocator at its address is its own)
assert_eq!(reset_list(l as usize, a as usize, D3D12_COMMAND_LIST_TYPE_DIRECT.0, |pc| {
assert_eq!(pc, a as usize);
let _alloc_guard = super::super::real::UnadoptGuard::new(pc as *mut c_void);
list.Reset(&alloc, None).map_or_else(|e| e.code().0, |_| 0)
}), 0);
list.Close().unwrap();
Expand Down
2 changes: 1 addition & 1 deletion crates/xcl/src/hle/d3d12x/cpu_writes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ pub(super) unsafe fn after(queue: *mut c_void, writes: Vec<Write>) {
return;
}
let Some(dev) = dev else { return };
let Ok(fence) = dev.CreateFence::<ID3D12Fence>(0, D3D12_FENCE_FLAG_NONE) else { return };
let Ok(fence) = super::real::create_raw_fence(&dev) else { return };
v.insert(QueueFence { fence, value: 0 })
}
};
Expand Down
2 changes: 1 addition & 1 deletion crates/xcl/src/hle/d3d12x/late_release.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl Held {
let key = q.as_raw() as usize;
if !self.fences.contains_key(&key) {
let mut dev: Option<ID3D12Device> = None;
let made = q.GetDevice(&mut dev).ok().and(dev).and_then(|d| d.CreateFence::<ID3D12Fence>(0, D3D12_FENCE_FLAG_NONE).ok());
let made = q.GetDevice(&mut dev).ok().and(dev).and_then(|d| super::real::create_raw_fence(&d).ok());
let Some(f) = made else {
ok = false;
break;
Expand Down
56 changes: 43 additions & 13 deletions crates/xcl/src/hle/d3d12x/list_x.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1661,7 +1661,7 @@ unsafe fn ensure_cell_fence(va: u64, dev: &ID3D12Device, dev_raw: usize) -> Opti
}
}
}
let created: ID3D12Fence = dev.CreateFence(0, D3D12_FENCE_FLAG_NONE).ok()?;
let created: ID3D12Fence = real::create_raw_fence(&dev).ok()?;
let mut g = SYNC_GRAPH.lock().unwrap();
let cell = g.get_or_insert_with(SyncGraph::default).cells.entry(va).or_default();
if cell.non_monotonic {
Expand Down Expand Up @@ -2306,7 +2306,7 @@ pub(super) unsafe fn copy_scratch_submitted(queue: *mut c_void, lists: &[*mut c_
if let std::collections::hash_map::Entry::Vacant(entry) = cache.fences.entry(key) {
let mut dev: Option<ID3D12Device> = None;
q.GetDevice(&mut dev).ok()?;
entry.insert((dev?.CreateFence::<ID3D12Fence>(0, D3D12_FENCE_FLAG_NONE).ok()?, 0));
entry.insert((real::create_raw_fence(&dev?).ok()?, 0));
}
let (fence, value) = cache.fences.get_mut(&key)?;
let next = *value + 1;
Expand Down Expand Up @@ -2689,7 +2689,10 @@ pub(super) unsafe fn record_deferred(wrapper: *mut c_void) {
None => super::alloc_pool::title_reset(own).unwrap_or_else(|| real::reset_pc_allocator(own)),
};
if hr >= 0 {
hr = super::alloc_pool::reset_list(seg as usize, own as usize, ty.0, |pc_alloc| pc::<unsafe extern "system" fn(*mut c_void, usize, usize) -> i32>(seg, PC_RESET)(seg, pc_alloc, 0));
hr = super::alloc_pool::reset_list(seg as usize, own as usize, ty.0, |pc_alloc| {
let _alloc_guard = real::UnadoptGuard::new(pc_alloc as *mut c_void);
pc::<unsafe extern "system" fn(*mut c_void, usize, usize) -> i32>(seg, PC_RESET)(seg, pc_alloc, 0)
});
}
real::preserve_vtable(seg, ext);
if hr < 0 {
Expand Down Expand Up @@ -2806,6 +2809,8 @@ unsafe fn split(list: *mut c_void, dispatch: Option<Deferred>) -> Option<*mut c_
if let Some(spare) = super::title_list::pop_spare(wrapper) {
let ext_spare = *(spare as *const usize);
let reset_hr = super::alloc_pool::reset_list(spare as usize, alloc, ty.0, |pc_alloc| {
let _alloc_guard = real::UnadoptGuard::new(pc_alloc as *mut c_void);
let _pso_guard = real::UnadoptGuard::new(init_pso as *mut c_void);
pc::<unsafe extern "system" fn(*mut c_void, usize, usize) -> i32>(spare, PC_RESET)(spare, pc_alloc, init_pso)
});
real::preserve_vtable(spare, ext_spare);
Expand Down Expand Up @@ -2960,7 +2965,10 @@ unsafe fn deferred_segment(wrapper: *mut c_void, list: *mut c_void, ty: D3D12_CO
let emptied = |p: &*mut c_void| {
let p = *p;
let ext = *(p as *const usize);
let hr = super::alloc_pool::reset_list(p as usize, own, ty.0, |pc_alloc| pc::<unsafe extern "system" fn(*mut c_void, usize, usize) -> i32>(p, PC_RESET)(p, pc_alloc, 0));
let hr = super::alloc_pool::reset_list(p as usize, own, ty.0, |pc_alloc| {
let _alloc_guard = real::UnadoptGuard::new(pc_alloc as *mut c_void);
pc::<unsafe extern "system" fn(*mut c_void, usize, usize) -> i32>(p, PC_RESET)(p, pc_alloc, 0)
});
real::preserve_vtable(p, ext);
hr >= 0
};
Expand Down Expand Up @@ -3331,7 +3339,10 @@ pub unsafe extern "system" fn kickoff_x(this: *mut c_void) {
if let Some((_, ref prior, _)) = kickoff_split {
if !prior.is_empty() {
execute_with_waits_on(q_raw, prior, |chunk| {
exec(q_raw, chunk.len() as u32, chunk.as_ptr());
{
let _list_guards: Vec<_> = chunk.iter().filter_map(|&l| real::UnadoptGuard::new(l)).collect();
exec(q_raw, chunk.len() as u32, chunk.as_ptr());
}
copy_scratch_submitted(q_raw, chunk);
super::alloc_pool::executed(q_raw, chunk);
signal_write64_fences(q_raw, chunk);
Expand All @@ -3350,7 +3361,10 @@ pub unsafe extern "system" fn kickoff_x(this: *mut c_void) {
}

let one = [this];
exec(q_raw, 1, one.as_ptr());
{
let _list_guard = real::UnadoptGuard::new(this);
exec(q_raw, 1, one.as_ptr());
}
copy_scratch_submitted(q_raw, &one);
super::alloc_pool::executed(q_raw, &one);
signal_write64_pairs(q_raw, &write64_signals);
Expand Down Expand Up @@ -3403,6 +3417,8 @@ pub unsafe extern "system" fn kickoff_x(this: *mut c_void) {
let ext_closed = *(target as *const usize);
let init_pso = super::pso_async::resolve_for_bind(bound_pso);
let reset_hr = super::alloc_pool::reset_list(target as usize, alloc, list_type(target).0, |pc_alloc| {
let _alloc_guard = real::UnadoptGuard::new(pc_alloc as *mut c_void);
let _pso_guard = real::UnadoptGuard::new(init_pso as *mut c_void);
pc::<unsafe extern "system" fn(*mut c_void, usize, usize) -> i32>(target, PC_RESET)(target, pc_alloc, init_pso)
});
real::preserve_vtable(target, ext_closed);
Expand Down Expand Up @@ -4063,6 +4079,8 @@ pub(super) unsafe fn reset_pc(this: *mut c_void, alloc: usize, pso: usize) -> i3
// (the PC allocator the title's allocator stands for now: `alloc_pool`)
let ty = LISTS.lock().unwrap().as_ref().and_then(|m| m.get(&(this as usize))).and_then(|s| s.list_type).map_or(-1, |t| t.0);
let hr = super::alloc_pool::reset_list(this as usize, alloc, ty, |pc_alloc| {
let _alloc_guard = real::UnadoptGuard::new(pc_alloc as *mut c_void);
let _pso_guard = real::UnadoptGuard::new(resolved as *mut c_void);
pc::<unsafe extern "system" fn(*mut c_void, usize, usize) -> i32>(this, PC_RESET)(this, pc_alloc, resolved)
});
real::preserve_vtable(this, ext);
Expand Down Expand Up @@ -6948,15 +6966,18 @@ mod tests {
let release: unsafe extern "system" fn(*mut c_void) -> u32 = std::mem::transmute(slot(2));
let exec: unsafe extern "system" fn(*mut c_void, u32, *const *mut c_void) =
std::mem::transmute(*(*(q.as_raw() as *const *const usize)).add(PC_QUEUE_EXECUTE_COMMAND_LISTS));
let gate: ID3D12Fence = dev.CreateFence(0, D3D12_FENCE_FLAG_NONE).unwrap();
let done: ID3D12Fence = dev.CreateFence(0, D3D12_FENCE_FLAG_NONE).unwrap();
let gate: ID3D12Fence = real::create_raw_fence(dev).unwrap();
let done: ID3D12Fence = real::create_raw_fence(dev).unwrap();
// Close, then submit as ExecuteCommandLists does: the PC lists the title's list stands for
let submit = || {
assert_eq!(close(w), 0);
let mut segs = Vec::new();
assert!(super::super::title_list::push_segments(w, &mut segs));
let again = super::super::alloc_pool::note_resubmits(q.as_raw(), &segs);
exec(q.as_raw(), segs.len() as u32, segs.as_ptr());
{
let _list_guards: Vec<_> = segs.iter().filter_map(|&l| real::UnadoptGuard::new(l)).collect();
exec(q.as_raw(), segs.len() as u32, segs.as_ptr());
}
super::super::alloc_pool::executed(q.as_raw(), &segs);
(segs, again)
};
Expand Down Expand Up @@ -7020,7 +7041,7 @@ mod tests {
let queue: ID3D12CommandQueue = dev.CreateCommandQueue(&D3D12_COMMAND_QUEUE_DESC { Type: D3D12_COMMAND_LIST_TYPE_COMPUTE, ..Default::default() }).unwrap();
let alloc: ID3D12CommandAllocator = dev.CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_COMPUTE).unwrap();
let raw: ID3D12GraphicsCommandList = dev.CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_COMPUTE, &alloc, None).unwrap();
let done: ID3D12Fence = dev.CreateFence(0, D3D12_FENCE_FLAG_NONE).unwrap();
let done: ID3D12Fence = real::create_raw_fence(&dev).unwrap();

let standin = 0x7E57_D001usize;
static BUILD: AtomicBool = AtomicBool::new(false);
Expand Down Expand Up @@ -7069,7 +7090,10 @@ mod tests {

type ExecFn = unsafe extern "system" fn(*mut c_void, u32, *const *mut c_void);
let exec: ExecFn = std::mem::transmute(*(*(queue.as_raw() as *const *const usize)).add(PC_QUEUE_EXECUTE_COMMAND_LISTS));
exec(queue.as_raw(), segs.len() as u32, segs.as_ptr());
{
let _list_guards: Vec<_> = segs.iter().filter_map(|&l| real::UnadoptGuard::new(l)).collect();
exec(queue.as_raw(), segs.len() as u32, segs.as_ptr());
}
queue.Signal(&done, 1).unwrap();
crate::gpu::fence::wait(&done, 1, &dev, None, 2000).expect("the split list runs");

Expand Down Expand Up @@ -7188,7 +7212,10 @@ mod tests {
execute_with_waits_on(q_comp.as_raw(), &segs_c, |chunk| {
type ExecFn = unsafe extern "system" fn(*mut c_void, u32, *const *mut c_void);
let exec: ExecFn = std::mem::transmute(*(*(q_comp.as_raw() as *const *const usize)).add(PC_QUEUE_EXECUTE_COMMAND_LISTS));
exec(q_comp.as_raw(), chunk.len() as u32, chunk.as_ptr());
{
let _guards: Vec<_> = chunk.iter().filter_map(|&l| real::UnadoptGuard::new(l)).collect();
exec(q_comp.as_raw(), chunk.len() as u32, chunk.as_ptr());
}
signal_write64_fences(q_comp.as_raw(), chunk);
if chunk.contains(&pc_c0) {
cell_a.store(frame, Ordering::Release);
Expand All @@ -7201,7 +7228,10 @@ mod tests {
execute_with_waits_on(q_dir.as_raw(), &one_d, |chunk| {
type ExecFn = unsafe extern "system" fn(*mut c_void, u32, *const *mut c_void);
let exec: ExecFn = std::mem::transmute(*(*(q_dir.as_raw() as *const *const usize)).add(PC_QUEUE_EXECUTE_COMMAND_LISTS));
exec(q_dir.as_raw(), chunk.len() as u32, chunk.as_ptr());
{
let _guards: Vec<_> = chunk.iter().filter_map(|&l| real::UnadoptGuard::new(l)).collect();
exec(q_dir.as_raw(), chunk.len() as u32, chunk.as_ptr());
}
signal_write64_fences(q_dir.as_raw(), chunk);
cell_b.store(frame, Ordering::Release);
});
Expand Down
2 changes: 1 addition & 1 deletion crates/xcl/src/hle/d3d12x/present.rs
Original file line number Diff line number Diff line change
Expand Up @@ -947,7 +947,7 @@ unsafe fn new_presenter(queue: usize) -> Option<Presenter> {
let actual_slots = if spare_alloc.is_some() { 2 } else { 1 };
let list: ID3D12GraphicsCommandList = dev.CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, &alloc, None).ok()?;
list.Close().ok()?;
let fence: ID3D12Fence = dev.CreateFence(0, D3D12_FENCE_FLAG_NONE).ok()?;
let fence: ID3D12Fence = super::real::create_raw_fence(dev).ok()?;
let event = CreateEventW(None, false, false, None).ok()?;
Some(Presenter {
queue, dev: dev.clone(), sc: None, w: 0, h: 0, fmt: DXGI_FORMAT_UNKNOWN,
Expand Down
Loading
Loading