Skip to content
Draft
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
43 changes: 42 additions & 1 deletion crates/xcl/src/hle/d3d12x/console_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,14 @@ pub struct Taken(Heap);
/// Stop finding a heap by object address without freeing memory the object may still use.
pub fn take(obj: usize) -> Option<Taken> {
let mut heaps = HEAPS.lock().unwrap();
heaps.iter().position(|h| h.obj == obj).map(|i| Taken(heaps.swap_remove(i)))
let heap = heaps.iter().position(|h| h.obj == obj).map(|i| heaps.swap_remove(i));
drop(heaps);
if let Some(heap) = heap {
super::vram::forget_srv_heap(heap.generation);
Some(Taken(heap))
} else {
None
}
}

/// Return a taken heap's slots and shadow memory after its native object is gone.
Expand Down Expand Up @@ -763,6 +770,19 @@ pub fn copy_words(src: &Slot, dst: &Slot) {
}
}

/// The tracking-only form of `copy_words`, retaining the exact source and destination bytes.
pub fn copy_words_captured(src: &Slot, dst: &Slot) -> ([u32; 8], [u32; 8]) {
let mut source = [0u32; 8];
unsafe { std::ptr::copy_nonoverlapping(src.at as *const u32, source.as_mut_ptr(), 8) };
let mut destination = source;
if destination[3] >> 28 >= 8 {
destination[0] = dst.index;
}
write(dst, &destination);
note_resource(dst, resource_at(src.at).unwrap_or(0));
(source, destination)
}

/// `XCL_TRACE_FRAME`: what the slots of each shader-visible view heap hold – images whose
/// PC index is the slot's own (made or copied through the device) or another (copied by the
/// title itself, as memory), buffers, empty.
Expand Down Expand Up @@ -835,6 +855,27 @@ pub fn words_at(addr32: u32) -> Option<[u32; 8]> {
Some(w)
}

/// Exact console words currently in view slots identified by heap generation and slot number.
/// Build the generation index once so validating S tracked slots across H heaps is O(S + H), and
/// keep `HEAPS` held while reading so the backing storage cannot be freed underneath us.
pub(super) fn view_slot_words(slots: &[(u64, u32)]) -> Vec<Option<[u32; 8]>> {
let heaps = HEAPS.lock().unwrap();
let by_generation: std::collections::HashMap<_, _> = heaps
.iter()
.filter(|heap| !heap.sampler)
.map(|heap| (heap.generation, heap))
.collect();
slots
.iter()
.map(|&(generation, k)| {
let h = by_generation.get(&generation).filter(|heap| k < heap.n)?;
let mut words = [0u32; 8];
unsafe { std::ptr::copy_nonoverlapping((h.shadow + (k * SLOT) as usize) as *const u32, words.as_mut_ptr(), 8) };
Some(words)
})
.collect()
}

// ---------------------------------------------------------------------------------------
// Shared shader-visible PC sampler heap (`GLOBAL_SAMPLER`, up to D3D12's 2048 limit):
// allows 16-byte S# descriptors copied via raw CPU `memcpy` between CPU-only and
Expand Down
31 changes: 27 additions & 4 deletions crates/xcl/src/hle/d3d12x/real.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2072,12 +2072,14 @@ unsafe extern "system" fn dev_create_cbv_x(this: *mut c_void, desc: *const D3D12
let f: unsafe extern "system" fn(*mut c_void, *const D3D12_CONSTANT_BUFFER_VIEW_DESC, u64) = std::mem::transmute(orig_fn(this, 17));
return f(this, desc, cpu);
};
let _tracked_write_lock = super::vram::lock_tracked_srv_write();
let (va, size) = desc.as_ref().map_or((0, 0), |d| (d.BufferLocation, d.SizeInBytes));
let in_uma = crate::gpu::uma::get().is_some_and(|u| u.contains(va as usize));
if !in_uma {
note_buffer_view();
}
let (va, size) = if in_uma { (va, size) } else { (0, 0) };
super::vram::clear_tracked_srv(&slot);
super::console_heap::write(&slot, &crate::gpu::desc::cbv_words(va, size));
}

Expand All @@ -2095,21 +2097,31 @@ pub(super) unsafe extern "system" fn dev_create_sampler_x(this: *mut c_void, des
/// Copy `n` console slots from `src` to `dst` (both in console heaps of one kind): the PC
/// descriptors (2 per view slot) and the slots. False: not two console ranges.
unsafe fn copy_console_slots(this: *mut c_void, n: u32, dst: u64, src: u64, ty: u32) -> bool {
use super::console_heap::{copy_words, slot, SAMPLER_SLOT, SLOT};
use super::console_heap::{copy_words, copy_words_captured, slot, SAMPLER_SLOT, SLOT};
let (Some(d0), Some(s0)) = (slot(dst), slot(src)) else { return false };
if d0.sampler != s0.sampler {
return false;
}
let _tracked_write_lock = (!d0.sampler).then(super::vram::lock_tracked_srv_write).flatten();
let step = if d0.sampler { SAMPLER_SLOT } else { SLOT } as u64;
let ty = pc_heap_type(ty);
let f: unsafe extern "system" fn(*mut c_void, u32, u64, u64, u32) = std::mem::transmute(orig_fn(this, 24));
f(this, if d0.sampler { n } else { 2 * n }, d0.pc_srv, s0.pc_srv, ty);
super::console_heap::publish_range(&d0, n);
let step = if d0.sampler { SAMPLER_SLOT } else { SLOT } as u64;
let mut tracked_copies = (!d0.sampler && super::vram::srv_tracking_enabled()).then(Vec::new);
for k in 0..n as u64 {
if let (Some(d), Some(s)) = (slot(dst + k * step), slot(src + k * step)) {
copy_words(&s, &d);
if let Some(copies) = tracked_copies.as_mut() {
let (source_words, destination_words) = copy_words_captured(&s, &d);
copies.push((s, d, source_words, destination_words));
} else {
copy_words(&s, &d);
}
}
}
if let Some(copies) = tracked_copies {
super::vram::copy_tracked_srvs(this as usize, &copies);
}
true
}

Expand Down Expand Up @@ -2408,6 +2420,8 @@ unsafe extern "system" fn dev_create_srv_x(this: *mut c_void, res: *mut c_void,
}
let desc = copy.as_ref().map_or(desc, |c| c as *const _);
let Some(slot) = super::console_heap::slot(cpu) else { return f(this, res, desc, cpu) };
let _tracked_write_lock = super::vram::lock_tracked_srv_write();
super::vram::clear_tracked_srv(&slot);
// Raytracing acceleration structure SRV (pResource == NULL, Location == console GPU VA):
// translated shaders read BVH nodes from UMA (`bvh_intersect`), never from a PC DXR SRV,
// so write the RDNA2 BVH descriptor directly and skip forwarding the console VA to PC.
Expand Down Expand Up @@ -2481,7 +2495,10 @@ unsafe extern "system" fn dev_create_srv_x(this: *mut c_void, res: *mut c_void,
}
let rdesc = r.GetDesc();
match srv_tsharp_from_desc(slot.index, &rdesc, desc.as_ref()) {
Some(t) => super::console_heap::write(&slot, &t),
Some(t) => {
super::console_heap::write(&slot, &t);
super::vram::track_srv(this as usize, res as usize, &slot, desc.as_ref().copied(), t);
}
None => {
note_buffer_view();
super::console_heap::write(&slot, &[0; 8]);
Expand Down Expand Up @@ -2549,6 +2566,8 @@ unsafe extern "system" fn dev_create_srv_at_x(this: *mut c_void, rdesc: *const u
if v.ViewDimension == D3D12_SRV_DIMENSION_BUFFER {
let _detail = crate::stall::detail_span(crate::stall::StallKind::DetailViewDescriptor);
let Some(slot) = super::console_heap::slot(cpu) else { return };
let _tracked_write_lock = super::vram::lock_tracked_srv_write();
super::vram::clear_tracked_srv(&slot);
let b = &v.Anonymous.Buffer;
let raw = b.Flags.0 & D3D12_BUFFER_SRV_FLAG_RAW.0 != 0;
let words = buffer_words_at(va, v.Format, b.FirstElement, b.NumElements, b.StructureByteStride, raw);
Expand All @@ -2568,6 +2587,8 @@ unsafe extern "system" fn dev_create_uav_at_x(this: *mut c_void, rdesc: *const u
let _detail = crate::stall::detail_span(crate::stall::StallKind::DetailViewDescriptor);
if v.ViewDimension == D3D12_UAV_DIMENSION_BUFFER {
let Some(slot) = super::console_heap::slot(cpu) else { return };
let _tracked_write_lock = super::vram::lock_tracked_srv_write();
super::vram::clear_tracked_srv(&slot);
let b = &v.Anonymous.Buffer;
let raw = b.Flags.0 & D3D12_BUFFER_UAV_FLAG_RAW.0 != 0;
let words = buffer_words_at(va, v.Format, b.FirstElement, b.NumElements, b.StructureByteStride, raw);
Expand Down Expand Up @@ -2823,6 +2844,8 @@ unsafe extern "system" fn dev_create_uav_x(this: *mut c_void, res: *mut c_void,
}
};
let Some(slot) = super::console_heap::slot(cpu) else { return pc_uav(cpu) };
let _tracked_write_lock = super::vram::lock_tracked_srv_write();
super::vram::clear_tracked_srv(&slot);
pc_uav(slot.pc_uav);
super::console_heap::publish(&slot, true);
super::console_heap::note_resource(&slot, res as usize);
Expand Down
Loading