Skip to content
Merged
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
172 changes: 172 additions & 0 deletions crates/xcl/src/hle/d3d12x/list_x.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4192,6 +4192,55 @@ pub(super) unsafe extern "system" fn clear_depth_stencil_x(this: *mut c_void, ds
pc::<unsafe extern "system" fn(*mut c_void, usize, u32, f32, u8, u32, *const windows::Win32::Foundation::RECT)>(this, PC_CLEAR_DEPTH_STENCIL_VIEW)(this, dsv, flags, depth, stencil, n, rects)
}

const PC_CLEAR_UAV_UINT: usize = 49;
const PC_CLEAR_UAV_FLOAT: usize = 50;

/// The handles a ClearUnorderedAccessView* of console code goes to the PC with.
#[derive(Debug, PartialEq)]
enum UavClearHandles {
/// both are console slots: their PC UAVs (GPU: in the heap lists bind; CPU: in the
/// title's CPU-only heap)
Console { gpu: u64, cpu: u64 },
/// neither is a console slot: PC handles already
Pc,
/// one console slot and one not, or a slot with no shader-visible UAV
Unusable,
}

fn uav_clear_handles(gpu: Option<super::console_heap::Slot>, cpu: Option<super::console_heap::Slot>) -> UavClearHandles {
match (gpu, cpu) {
(None, None) => UavClearHandles::Pc,
(Some(g), Some(c)) if !g.sampler && !c.sampler && g.pc_gpu_uav != 0 => UavClearHandles::Console { gpu: g.pc_gpu_uav, cpu: c.pc_uav },
_ => UavClearHandles::Unusable,
}
}

/// command list #53 / #54 ClearUnorderedAccessViewUint / Float(GPU handle, CPU handle,
/// resource, values[4], rects, rect list) (PC #49 / #50): console handles are addresses of
/// console heap slots (heap #9 / #10), not PC descriptors
unsafe fn clear_uav(this: *mut c_void, slot: usize, gpu: u64, cpu: u64, res: usize, values: *const c_void, n: u32, rects: *const windows::Win32::Foundation::RECT) {
let (gpu, cpu) = match uav_clear_handles(super::console_heap::slot(gpu), super::console_heap::slot(cpu)) {
UavClearHandles::Console { gpu, cpu } => (gpu, cpu),
UavClearHandles::Pc => (gpu, cpu),
UavClearHandles::Unusable => {
static N: AtomicU64 = AtomicU64::new(0);
if once(&N, 1) {
crate::xlog_warn!("D3D12.X: ClearUnorderedAccessView with GPU handle {gpu:#x} / CPU handle {cpu:#x}: not both a console slot with a shader-visible UAV – not done");
}
return;
}
};
pc::<unsafe extern "system" fn(*mut c_void, u64, u64, usize, *const c_void, u32, *const windows::Win32::Foundation::RECT)>(this, slot)(this, gpu, cpu, res, values, n, rects)
}

pub(super) unsafe extern "system" fn clear_uav_uint_x(this: *mut c_void, gpu: u64, cpu: u64, res: usize, values: *const u32, n: u32, rects: *const windows::Win32::Foundation::RECT) {
clear_uav(this, PC_CLEAR_UAV_UINT, gpu, cpu, res, values as *const c_void, n, rects)
}

pub(super) unsafe extern "system" fn clear_uav_float_x(this: *mut c_void, gpu: u64, cpu: u64, res: usize, values: *const f32, n: u32, rects: *const windows::Win32::Foundation::RECT) {
clear_uav(this, PC_CLEAR_UAV_FLOAT, gpu, cpu, res, values as *const c_void, n, rects)
}

const PC_RESOLVE_SUBRESOURCE: usize = 19;
const PC_RESOLVE_SUBRESOURCE_REGION: usize = 64;

Expand Down Expand Up @@ -6922,4 +6971,127 @@ mod tests {
assert_eq!(default_bound_heaps(Some(0x10), Some(0x20), &[0x10, 0x20], true), None);
assert_eq!(default_bound_heaps(Some(0x10), Some(0x20), &[0x10, 0x99], true), None);
}

/// ClearUnorderedAccessView* handles: two console slots → their PC UAVs, two PC handles as
/// they are, anything else not usable.
#[test]
fn uav_clear_handles_translate_console_slots() {
use super::super::console_heap::Slot;
let s = |pc_uav: u64, pc_gpu_uav: u64, sampler: bool| Some(Slot { at: 0x1000, index: 1, pc_srv: 0, pc_uav, pc_gpu_uav, sampler, shared_srv: 0, k: 0 });
assert_eq!(uav_clear_handles(s(0x10, 0x5010, false), s(0x20, 0, false)), UavClearHandles::Console { gpu: 0x5010, cpu: 0x20 });
assert_eq!(uav_clear_handles(None, None), UavClearHandles::Pc);
// a console slot beside a PC handle, a GPU slot of a heap that is not shader visible,
// sampler slots
assert_eq!(uav_clear_handles(s(0x10, 0x5010, false), None), UavClearHandles::Unusable);
assert_eq!(uav_clear_handles(None, s(0x20, 0, false)), UavClearHandles::Unusable);
assert_eq!(uav_clear_handles(s(0x10, 0, false), s(0x20, 0, false)), UavClearHandles::Unusable);
assert_eq!(uav_clear_handles(s(0x10, 0x5010, false), s(0, 0, true)), UavClearHandles::Unusable);
assert_eq!(uav_clear_handles(s(0, 0, true), s(0x20, 0, false)), UavClearHandles::Unusable);
}

/// Console code clears a UAV with its console heap slots (heap #9 / #10 give it their
/// addresses): the clear reaches the PC with the slots' UAVs. Before, the slot addresses
/// went to the PC method as handles. On WARP (a software adapter).
#[test]
fn clear_uav_with_console_handles_clears_the_slots_view() {
use super::super::console_heap;
use windows::Win32::Graphics::Direct3D::D3D_FEATURE_LEVEL_11_0;
use windows::Win32::Graphics::Dxgi::*;
if crate::gpu::uma::get().is_none() {
crate::gpu::recomp::runner::skip_test("no unified memory");
return;
}
unsafe {
let Ok(factory) = CreateDXGIFactory1::<IDXGIFactory4>() else {
crate::gpu::recomp::runner::skip_test("no WARP");
return;
};
let Ok(warp) = factory.EnumWarpAdapter::<IDXGIAdapter>() else {
crate::gpu::recomp::runner::skip_test("no WARP");
return;
};
let mut dev: Option<ID3D12Device> = None;
if D3D12CreateDevice(&warp, D3D_FEATURE_LEVEL_11_0, &mut dev).is_err() {
crate::gpu::recomp::runner::skip_test("no WARP");
return;
}
let dev = dev.unwrap();
const N: u32 = 64;
let desc = |flags| D3D12_RESOURCE_DESC {
Dimension: D3D12_RESOURCE_DIMENSION_BUFFER,
Width: 4 * N as u64,
Height: 1,
DepthOrArraySize: 1,
MipLevels: 1,
SampleDesc: DXGI_SAMPLE_DESC { Count: 1, Quality: 0 },
Layout: D3D12_TEXTURE_LAYOUT_ROW_MAJOR,
Flags: flags,
..Default::default()
};
let committed = |ty, flags, state| {
let mut r: Option<ID3D12Resource> = None;
dev.CreateCommittedResource(&D3D12_HEAP_PROPERTIES { Type: ty, ..Default::default() }, D3D12_HEAP_FLAG_NONE, &desc(flags), state, None, &mut r).unwrap();
r.unwrap()
};
let buf = committed(D3D12_HEAP_TYPE_DEFAULT, D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_UNORDERED_ACCESS);
let rb = committed(D3D12_HEAP_TYPE_READBACK, D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_COPY_DEST);

// the title's heaps of one view slot each (SRV, UAV): a CPU-only one and a shader-visible one
let inc = dev.GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
let heap = |flags| -> ID3D12DescriptorHeap {
dev.CreateDescriptorHeap(&D3D12_DESCRIPTOR_HEAP_DESC { Type: D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, NumDescriptors: 2, Flags: flags, NodeMask: 0 }).unwrap()
};
let (cpu_heap, gpu_heap) = (heap(D3D12_DESCRIPTOR_HEAP_FLAG_NONE), heap(D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE));
let uav = D3D12_UNORDERED_ACCESS_VIEW_DESC {
Format: DXGI_FORMAT_R32_UINT,
ViewDimension: D3D12_UAV_DIMENSION_BUFFER,
Anonymous: D3D12_UNORDERED_ACCESS_VIEW_DESC_0 { Buffer: D3D12_BUFFER_UAV { NumElements: N, ..Default::default() } },
};
for h in [&cpu_heap, &gpu_heap] {
let at = D3D12_CPU_DESCRIPTOR_HANDLE { ptr: h.GetCPUDescriptorHandleForHeapStart().ptr + inc as usize };
dev.CreateUnorderedAccessView(&buf, None, Some(&uav), at);
}
let (co, go) = (cpu_heap.as_raw() as usize, gpu_heap.as_raw() as usize);
assert!(console_heap::register(co, 1, cpu_heap.GetCPUDescriptorHandleForHeapStart().ptr as u64, 0, inc, false, false, false));
assert!(console_heap::register(go, 1, gpu_heap.GetCPUDescriptorHandleForHeapStart().ptr as u64, gpu_heap.GetGPUDescriptorHandleForHeapStart().ptr, inc, false, false, true));
let (console_cpu, console_gpu) = (console_heap::start(co).unwrap(), console_heap::start(go).unwrap());

let q: ID3D12CommandQueue = dev.CreateCommandQueue(&D3D12_COMMAND_QUEUE_DESC { Type: D3D12_COMMAND_LIST_TYPE_DIRECT, ..Default::default() }).unwrap();
let a: ID3D12CommandAllocator = dev.CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT).unwrap();
let l: ID3D12GraphicsCommandList = dev.CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, &a, None).unwrap();
l.SetDescriptorHeaps(&[Some(gpu_heap.clone())]);
// the title's call (#53) on the adopted list, then the PC vtable back
let raw = l.as_raw();
real::adopt(raw, real::Kind::CommandList);
clear_uav_uint_x(raw, console_gpu, console_cpu, buf.as_raw() as usize, [0xDEAD_BEEFu32; 4].as_ptr(), 0, std::ptr::null());
let ext = *(raw as *const *const usize);
*(raw as *mut usize) = *ext.sub(1);
let b = D3D12_RESOURCE_BARRIER {
Type: D3D12_RESOURCE_BARRIER_TYPE_TRANSITION,
Flags: D3D12_RESOURCE_BARRIER_FLAG_NONE,
Anonymous: D3D12_RESOURCE_BARRIER_0 {
Transition: ManuallyDrop::new(D3D12_RESOURCE_TRANSITION_BARRIER {
pResource: ManuallyDrop::new(Some(buf.clone())),
Subresource: D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES,
StateBefore: D3D12_RESOURCE_STATE_UNORDERED_ACCESS,
StateAfter: D3D12_RESOURCE_STATE_COPY_SOURCE,
}),
},
};
l.ResourceBarrier(&[b]);
l.CopyResource(&rb, &buf);
l.Close().unwrap();
q.ExecuteCommandLists(&[Some(l.cast().unwrap())]);
let fence: ID3D12Fence = dev.CreateFence(0, D3D12_FENCE_FLAG_NONE).unwrap();
q.Signal(&fence, 1).unwrap();
crate::gpu::fence::wait(&fence, 1, &dev, None, 5000).expect("the clear completes");
let mut p = std::ptr::null_mut();
rb.Map(0, None, Some(&mut p)).unwrap();
let got = std::slice::from_raw_parts(p as *const u32, N as usize).to_vec();
rb.Unmap(0, None);
console_heap::unregister(co);
console_heap::unregister(go);
assert!(got.iter().all(|&v| v == 0xDEAD_BEEF), "the UAV was not cleared: {:x?}", &got[..4]);
}
}
}
12 changes: 12 additions & 0 deletions crates/xcl/src/hle/d3d12x/real.rs
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,8 @@ fn console_slots(kind: Kind) -> Vec<(usize, usize)> {
(17, l::copy_resource_x as usize),
(19, l::resolve_subresource_x as usize),
(51, l::clear_depth_stencil_x as usize),
(53, l::clear_uav_uint_x as usize),
(54, l::clear_uav_float_x as usize),
(108, l::resolve_subresource_region_x as usize),
(13, b::draw_indexed_instanced as usize),
(14, b::dispatch as usize),
Expand Down Expand Up @@ -7546,6 +7548,16 @@ mod readback_routing_tests {
}
}

/// Console #53 / #54 ClearUnorderedAccessViewUint / Float translate their console handles
/// (not the PC #49 / #50 the remap alone gives).
#[test]
fn console_clear_uav_slots_translate_handles() {
use super::super::list_x as l;
let slots = console_slots(Kind::CommandList);
assert!(slots.contains(&(53, l::clear_uav_uint_x as usize)));
assert!(slots.contains(&(54, l::clear_uav_float_x as usize)));
}

/// Bug #6: `Kind::DmaList` must install `cl_release` so releasing a DMA command list removes
/// its entry from `list_x::LISTS`.
#[test]
Expand Down
Loading