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
2 changes: 1 addition & 1 deletion crates/xcl/src/hle/d3d12x/depth_uav.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1017,7 +1017,7 @@ pub(super) unsafe fn dsv_ranges_copied(dev: *mut c_void, nd: u32, dsts: *const u
}

/// Destination and source ranges cut where either one ends: (dst, src, n) runs.
fn pieces(dsts: &[(u64, u32)], srcs: &[(u64, u32)], inc: u64) -> Vec<(u64, u64, u32)> {
pub(super) fn pieces(dsts: &[(u64, u32)], srcs: &[(u64, u32)], inc: u64) -> Vec<(u64, u64, u32)> {
let (mut out, mut d, mut s, mut dk, mut sk) = (Vec::new(), 0, 0, 0u32, 0u32);
while d < dsts.len() && s < srcs.len() {
let n = (dsts[d].1 - dk).min(srcs[s].1 - sk);
Expand Down
72 changes: 72 additions & 0 deletions crates/xcl/src/hle/d3d12x/list_x.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5012,6 +5012,30 @@ pub(super) unsafe extern "system" fn dispatch_x(this: *mut c_void, x: u32, y: u3
super::bind::dispatch_flags(this, x, y, z, flags);
}

/// The textures of the texture barriers in the `n` barrier groups at `groups` (list #146), in
/// their order; groups and barrier arrays that are not readable memory are passed over.
unsafe fn barrier_group_textures(n: u32, groups: *const c_void) -> Vec<usize> {
let (mut out, mut seen) = (Vec::new(), HashSet::new());
let count = n as usize;
if count == 0 || groups.is_null() || !crate::gpu::shader::readable(groups as usize, count * std::mem::size_of::<D3D12_BARRIER_GROUP>()) {
return out;
}
for g in std::slice::from_raw_parts(groups as *const D3D12_BARRIER_GROUP, count) {
let m = g.NumBarriers as usize;
let p = g.Anonymous.pTextureBarriers;
if g.Type != D3D12_BARRIER_TYPE_TEXTURE || m == 0 || p.is_null() || !crate::gpu::shader::readable(p as usize, m * std::mem::size_of::<D3D12_TEXTURE_BARRIER>()) {
continue;
}
for b in std::slice::from_raw_parts(p, m) {
let r: usize = transmute_copy(&b.pResource);
if r != 0 && seen.insert(r) {
out.push(r);
}
}
}
out
}

unsafe fn emit_uav_barrier_pc(this: *mut c_void) {
let b = D3D12_RESOURCE_BARRIER {
Type: D3D12_RESOURCE_BARRIER_TYPE_UAV,
Expand All @@ -5029,6 +5053,12 @@ unsafe fn emit_uav_barrier_pc(this: *mut c_void) {
/// (`barrier_groups`) routed through `resource_barrier_x`.
pub(super) unsafe extern "system" fn uav_barrier_x(this: *mut c_void, n: u32, barriers: *const c_void) {
let _t = crate::hle::api_cost::time_if(super::look_stats::deep(), crate::hle::api_cost::Call::ListBarrier);
// (a superseded target the title moves is used again: `placements`)
if super::placements::any_superseded() {
for r in barrier_group_textures(n, barriers) {
super::placements::note_use(r);
}
}
emit_uav_barrier_pc(this);
if super::barrier_groups::enabled() {
let moves = super::barrier_groups::transitions(n, barriers, list_type(this) == D3D12_COMMAND_LIST_TYPE_DIRECT);
Expand Down Expand Up @@ -7119,6 +7149,8 @@ pub(super) unsafe extern "system" fn clear_depth_stencil_x(this: *mut c_void, ds
);
}
}
// (a superseded target the title clears is used again: `placements`)
super::placements::note_view_use(dsv);
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)
}

Expand Down Expand Up @@ -12389,4 +12421,44 @@ mod tests {
assert!(got.iter().all(|&v| v == 0xDEAD_BEEF), "the UAV was not cleared: {:x?}", &got[..4]);
}
}

/// List #146's groups: the textures of the texture barriers, each once, and nothing of the
/// buffer and global groups or of a group without an array.
#[test]
fn the_texture_barriers_of_barrier_groups_name_their_textures() {
unsafe {
let tex = |r: usize| {
let mut b: D3D12_TEXTURE_BARRIER = std::mem::zeroed();
std::ptr::write(&mut b.pResource as *mut _ as *mut usize, r);
b
};
let first = [tex(0x1000), tex(0), tex(0x2000)];
let second = [tex(0x2000), tex(0x3000)];
let mut buffer: D3D12_BUFFER_BARRIER = std::mem::zeroed();
std::ptr::write(&mut buffer.pResource as *mut _ as *mut usize, 0x9000);
let global: D3D12_GLOBAL_BARRIER = std::mem::zeroed();
let groups = [
D3D12_BARRIER_GROUP { Type: D3D12_BARRIER_TYPE_TEXTURE, NumBarriers: 3, Anonymous: D3D12_BARRIER_GROUP_0 { pTextureBarriers: first.as_ptr() } },
D3D12_BARRIER_GROUP { Type: D3D12_BARRIER_TYPE_BUFFER, NumBarriers: 1, Anonymous: D3D12_BARRIER_GROUP_0 { pBufferBarriers: &buffer } },
D3D12_BARRIER_GROUP { Type: D3D12_BARRIER_TYPE_GLOBAL, NumBarriers: 1, Anonymous: D3D12_BARRIER_GROUP_0 { pGlobalBarriers: &global } },
D3D12_BARRIER_GROUP { Type: D3D12_BARRIER_TYPE_TEXTURE, NumBarriers: 2, Anonymous: D3D12_BARRIER_GROUP_0 { pTextureBarriers: second.as_ptr() } },
D3D12_BARRIER_GROUP { Type: D3D12_BARRIER_TYPE_TEXTURE, NumBarriers: 4, Anonymous: D3D12_BARRIER_GROUP_0 { pTextureBarriers: std::ptr::null() } },
];
let at = groups.as_ptr() as *const c_void;
assert_eq!(barrier_group_textures(5, at), vec![0x1000, 0x2000, 0x3000]);
assert_eq!(barrier_group_textures(3, at), vec![0x1000, 0x2000], "the groups past the count are not read");
assert!(barrier_group_textures(0, at).is_empty());
assert!(barrier_group_textures(5, std::ptr::null()).is_empty());

// (no limit on the groups of a call or the barriers of a group: the 257th and the 4097th)
let none = D3D12_BARRIER_GROUP { Type: D3D12_BARRIER_TYPE_TEXTURE, NumBarriers: 0, Anonymous: D3D12_BARRIER_GROUP_0 { pTextureBarriers: std::ptr::null() } };
let mut many: Vec<D3D12_BARRIER_GROUP> = (0..256).map(|_| none).collect();
many.push(D3D12_BARRIER_GROUP { Type: D3D12_BARRIER_TYPE_TEXTURE, NumBarriers: 2, Anonymous: D3D12_BARRIER_GROUP_0 { pTextureBarriers: second.as_ptr() } });
assert_eq!(barrier_group_textures(257, many.as_ptr() as *const c_void), vec![0x2000, 0x3000]);
let mut long: Vec<D3D12_TEXTURE_BARRIER> = (0..4096).map(|_| tex(0x1000)).collect();
long.push(tex(0x4000));
let group = D3D12_BARRIER_GROUP { Type: D3D12_BARRIER_TYPE_TEXTURE, NumBarriers: 4097, Anonymous: D3D12_BARRIER_GROUP_0 { pTextureBarriers: long.as_ptr() } };
assert_eq!(barrier_group_textures(1, &group as *const _ as *const c_void), vec![0x1000, 0x4000]);
}
}
}
89 changes: 87 additions & 2 deletions crates/xcl/src/hle/d3d12x/placements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
//! A placement over other pages of a live target supersedes it (`supersedes`; `XCL_PLACE_RETIRE=0`
//! turns it off): what is uploaded into or drawn to the newer one then no longer shows in the
//! older one's tiles. Its tiles leave the shared pages once the queues are past the work submitted
//! before (`late_release::hold_unmap`), and come back when the title uses it again (a barrier or a
//! copy of it: `note_use`) or releases the newer ones over it while it still holds it.
//! before (`late_release::hold_unmap`), and come back when the title uses it again (`note_use`: a
//! barrier of it, also a texture barrier in list #146's groups; a copy of it; its view bound as a
//! render target or depth buffer, or cleared) or releases the newer ones over it while it still
//! holds it.

use std::collections::{BTreeMap, HashMap};
use std::ffi::c_void;
Expand Down Expand Up @@ -579,11 +581,94 @@ pub(super) fn note_use(res: usize) {
drop(held);
}

/// RTV or DSV handle → the placed texture it names (`note_view`; the two kinds of handle are in
/// different heaps). The view is made long before a newer placement supersedes its texture, so
/// the handles of all placed ones are kept; an entry whose texture is gone names no live
/// placement and does nothing (`Registry::use_again`). Addresses only, no reference: `present`
/// keeps one for each RTV, and keeps none with `present = "off"`.
static VIEWS: Mutex<Option<HashMap<usize, usize>>> = Mutex::new(None);

/// CreateRenderTargetView or CreateDepthStencilView of `res` at `handle`: kept when `res` is a
/// placed texture.
pub(super) fn note_view(handle: usize, res: usize) {
let placed = res != 0 && REGISTRY.lock().unwrap().as_ref().is_some_and(|r| r.live.contains_key(&res));
let mut g = VIEWS.lock().unwrap();
if placed {
g.get_or_insert_with(HashMap::new).insert(handle, res);
} else if let Some(m) = g.as_mut() {
m.remove(&handle);
}
}

/// `n` RTVs or DSVs copied from `src` to `dst` (handles `inc` apart): each destination names the
/// placed texture its source names, or none.
pub(super) fn views_copied(n: u32, dst: u64, src: u64, inc: u64) {
let mut g = VIEWS.lock().unwrap();
let Some(m) = g.as_mut() else { return };
for k in 0..n as u64 {
let to = (dst + k * inc) as usize;
match m.get(&((src + k * inc) as usize)).copied() {
Some(res) => m.insert(to, res),
None => m.remove(&to),
};
}
}

/// The placed texture the RTV or DSV at `handle` names.
fn view_texture(handle: usize) -> Option<usize> {
VIEWS.lock().unwrap().as_ref()?.get(&handle).copied()
}

/// The title binds or clears the target of the RTV or DSV at `handle`: a use of it (`note_use`).
pub(super) fn note_view_use(handle: usize) {
if !any_superseded() {
return;
}
if let Some(res) = view_texture(handle) {
note_use(res);
}
}

#[cfg(test)]
mod tests {
use super::*;

const K64: usize = 0x1_0000;

/// A view is kept for a placed texture only, and a handle written again (made there, or
/// copied there) names the new one.
#[test]
fn a_view_handle_names_the_placed_texture_it_was_last_made_of() {
// (addresses and handles no other test uses: the registry and the table are the module's)
let (a, b, other) = (0x7e57_0000_1000usize, 0x7e57_0000_2000usize, 0x7e57_0000_3000usize);
let (h1, h2) = (0x7e57_d000usize, 0x7e57_d020usize);
{
let mut g = REGISTRY.lock().unwrap();
let r = g.get_or_insert_with(Registry::default);
r.place(a, 0x7e57_4000_0000, (0x7e57_4000_0000, 0x7e57_4000_0000 + 6 * K64), Backing::Pages(0x7e57), Kind::Target, Desc::default(), Instant::now(), false, false);
r.place(b, 0x7e57_5000_0000, (0x7e57_5000_0000, 0x7e57_5000_0000 + 8 * K64), Backing::Pages(0x7e57), Kind::Target, Desc::default(), Instant::now(), false, false);
}
note_view(h1, a);
note_view(h2, other);
assert_eq!((view_texture(h1), view_texture(h2)), (Some(a), None), "a view of a texture that is not placed is not kept");
note_view(h1, b);
assert_eq!(view_texture(h1), Some(b));
// (copies: two handles 0x20 apart from h1, then from a handle that names nothing)
let (h3, nothing) = (0x7e57_d100usize, 0x7e57_d200usize);
note_view(h2, a);
views_copied(2, h3 as u64, h1 as u64, 0x20);
assert_eq!((view_texture(h3), view_texture(h3 + 0x20)), (Some(b), Some(a)));
views_copied(1, h3 as u64, nothing as u64, 0x20);
assert_eq!((view_texture(h3), view_texture(h3 + 0x20)), (None, Some(a)), "a copy of a view that names no placed texture");
note_view(h2, other);
note_view(h3 + 0x20, other);
note_view(h1, other);
assert_eq!(view_texture(h1), None, "the handle names a texture that is not placed now");
let mut g = REGISTRY.lock().unwrap();
let r = g.as_mut().unwrap();
r.remove(a);
r.remove(b);
}
const MIB: usize = 1 << 20;

/// A placement looked at, not retiring: (its sequence number, its cases).
Expand Down
Loading
Loading