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
79 changes: 79 additions & 0 deletions crates/xcl/src/hle/d3d12x/placements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,21 @@ impl Registry {
fn superseded(&self, res: usize) -> bool {
self.live.get(&res).is_some_and(|p| !p.mapped && !p.released)
}

/// Whether the title holds `res` and no newer placement supersedes it. A superseded target is
/// not current from the placement over it on, though its tiles leave the shared pages later
/// (`late_release::hold_unmap`).
fn current(&self, res: usize) -> bool {
self.live.get(&res).is_some_and(|p| p.mapped && !p.released)
}

/// Whether `res` is current and a texture of `view` at its address is a second description
/// of its texels (`Desc::same_texels`, the rule `place` keeps the two on one memory by). A
/// texture of another shape that only has as many tiles is not: it is something else the
/// title put in that memory.
fn current_view(&self, res: usize, view: &Desc) -> bool {
self.current(res) && self.live.get(&res).is_some_and(|p| view.same_texels(&p.desc))
}
}

static REGISTRY: Mutex<Option<Registry>> = Mutex::new(None);
Expand Down Expand Up @@ -538,6 +553,15 @@ pub(super) unsafe fn unmap_if_superseded(res: &ID3D12Resource) {
}
}

/// Whether placement `res` is current and a texture of `view` is a view of its texels
/// (`Registry::current_view`): one a sampled texture of `view` placed in its memory is paired
/// with (`vram::pair_bytes`). Takes the registry's lock, so it is not for a caller that holds
/// `vram`'s pages (`unmap_if_superseded` takes that lock under this one).
pub(super) fn current_view(res: usize, view: &D3D12_RESOURCE_DESC) -> bool {
let view = Desc::of(view);
REGISTRY.lock().unwrap().as_ref().is_some_and(|r| r.current_view(res, &view))
}

/// Whether any live target is currently superseded (`SUPERSEDED != 0`).
pub(super) fn any_superseded() -> bool {
SUPERSEDED.load(Ordering::Relaxed) != 0
Expand Down Expand Up @@ -829,4 +853,59 @@ mod tests {
assert!(!r.superseded(10));
assert_eq!(r.unmapped, 0);
}

/// `XCL_PAIR_PAGES=1`: a 1280x720 R9G9B9E5 view (60 pages) placed at the address of a live
/// R32_UINT target (57 pages) lies around it and supersedes it; capped to the target's pages it
/// is on the same pages, and the two are one memory under two descriptions.
#[test]
fn a_capped_pair_is_on_the_same_pages() {
let mut r = Registry::default();
let x = 0x12_3a00_0000usize;
place(&mut r, 1, x, (x, x + 57 * K64), Kind::Target);
let (_, c) = place(&mut r, 2, x, (x, x + 60 * K64), Kind::Sampled);
assert_eq!(overlaps(&c), [(1, Overlap::Around)]);
assert!(supersedes(c[0].overlap, Kind::Target, Backing::Pages(1), false, true));
let (_, c) = place(&mut r, 3, x, (x, x + 57 * K64), Kind::Sampled);
assert_eq!(overlaps(&c), [(1, Overlap::Same), (2, Overlap::Inside)]);
assert!(!supersedes(c[0].overlap, Kind::Target, Backing::Pages(1), false, true));
}

/// `XCL_PAIR_PAGES=1` pairs a sampled texture with a current target only: a superseded one is
/// not current from the placement over it on (before its tiles are unmapped), one the title
/// uses again is, and one it released is not.
#[test]
fn a_superseded_or_released_target_is_not_current() {
let mut r = Registry::default();
let x = 0x12_3a00_0000usize;
retiring(&mut r, 1, x, 57 * K64, Kind::Target);
assert!(r.current(1));
assert!(!r.current(9), "never placed");
assert_eq!(retiring(&mut r, 2, x + 8 * K64, 4 * K64, Kind::Sampled).0, [1]);
assert!(!r.current(1), "superseded");
assert!(r.current(2));
assert_eq!(r.use_again(1), [2]);
assert!(r.current(1), "used again");
assert!(r.release(1).is_empty());
assert!(!r.current(1), "released");
}

/// `XCL_PAIR_PAGES=1` pairs a sampled texture with a current target it is a view of: the same
/// size and levels and a texel of the same size (R9G9B9E5 over R32_UINT). A texture of another
/// shape with as many tiles (720x1280 over 1280x720), with other levels or with another texel
/// size is not a view of it, and neither is any texture once the target is superseded.
#[test]
fn only_a_view_of_a_current_targets_texels_pairs_with_it() {
let mut r = Registry::default();
let x = 0x12_3a00_0000usize;
let d = |width: u64, height: u32, mips: u16, format: i32| Desc { width, height, depth: 1, mips, format, flags: 0 };
r.place(1, x, (x, x + 57 * K64), Backing::Pages(1), Kind::Target, d(1280, 720, 1, 42), Instant::now(), false, true);
assert!(r.current_view(1, &d(1280, 720, 1, 67)));
assert!(r.current_view(1, &d(1280, 720, 1, 42)));
assert!(!r.current_view(1, &d(720, 1280, 1, 67)), "another shape");
assert!(!r.current_view(1, &d(1280, 720, 2, 67)), "other levels");
assert!(!r.current_view(1, &d(1280, 720, 1, 10)), "a texel of another size");
assert!(!r.current_view(9, &d(1280, 720, 1, 67)), "never placed");
assert_eq!(retiring(&mut r, 2, x + 8 * K64, 4 * K64, Kind::Sampled).0, [1]);
assert!(!r.current_view(1, &d(1280, 720, 1, 67)), "superseded");
}
}
67 changes: 63 additions & 4 deletions crates/xcl/src/hle/d3d12x/texture_upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ pub(super) struct Item {
pub known: Option<Known>,
/// where its pages are read, for a texture in a GPU view whose pages the title mapped
pub pages: Option<Arc<PageMap>>,
/// `XCL_PAIR_PAGES=1`: the tiles `[0, n)` (in the resource's tile order) a capped texture owns
/// (`vram::pair_capped_tiles`); those after them are the pages of the texture the title placed
/// there, and none of its texels go up into them (`owned_parts`). None: all of it
pub own_tiles: Option<u32>,
}

// ---------------------------------------------------------------------------------------
Expand Down Expand Up @@ -468,7 +472,9 @@ unsafe fn plan_item(dev: &ID3D12Device, k: usize, it: &Item, at: &mut u64, out:
dev.GetCopyableFootprints(&rd, 0, 1, 0, Some(&mut fp), None, None, None);
}
let format: DXGI_FORMAT = fp.Footprint.Format;
let mut push = |slice: usize, level: usize, rect: Rect, depth: usize| {
// (a capped texture: only the parts on its own tiles)
let own = it.own_tiles.and_then(|own| owned_parts(dev, &it.res, own, n, s.side));
let mut push_part = |slice: usize, level: usize, rect: Rect, depth: usize| {
let (w, h) = (rect.x1 - rect.x0, rect.y1 - rect.y0);
let pitch = (w * bpe).next_multiple_of(D3D12_TEXTURE_DATA_PITCH_ALIGNMENT as usize);
*at = at.next_multiple_of(D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT as u64);
Expand All @@ -484,6 +490,16 @@ unsafe fn plan_item(dev: &ID3D12Device, k: usize, it: &Item, at: &mut u64, out:
});
*at += (pitch * h * depth) as u64;
};
let mut push = |slice: usize, level: usize, rect: Rect, depth: usize| match &own {
None => push_part(slice, level, rect, depth),
// (a 3D level goes up whole or not at all)
Some(own) => own.get(slice * mips + level).into_iter().flatten().for_each(|o| {
let r = Rect { x0: rect.x0.max(o.x0), y0: rect.y0.max(o.y0), x1: rect.x1.min(o.x1), y1: rect.y1.min(o.y1) };
if r.x0 < r.x1 && r.y0 < r.y1 && (!volume || r == rect) {
push_part(slice, level, r, depth);
}
}),
};
if volume {
for (i, l) in c.levels.iter().enumerate() {
push(0, i, Rect { x0: 0, y0: 0, x1: l.width, y1: l.height }, l.depth);
Expand Down Expand Up @@ -526,6 +542,49 @@ unsafe fn plan_item(dev: &ID3D12Device, k: usize, it: &Item, at: &mut u64, out:
Ok(())
}

/// The parts of each of the `subs` subresources of reserved `res` on its tiles `[0, own)`
/// (`Item::own_tiles`), in elements of `side` texels: its rows of tiles before the first tile it
/// does not own, then the tiles of that row before it. A subresource with more than one layer of
/// tiles (3D) or in the packed mips is all of it when it owns all of their tiles, else nothing.
/// None: `res` has no tiles (not reserved), so it shares none.
unsafe fn owned_parts(dev: &ID3D12Device, res: &ID3D12Resource, own: u32, subs: usize, side: u32) -> Option<Vec<Vec<Rect>>> {
const ALL: Rect = Rect { x0: 0, y0: 0, x1: usize::MAX, y1: usize::MAX };
let (mut total, mut packed, mut shape, mut n) = (0u32, D3D12_PACKED_MIP_INFO::default(), D3D12_TILE_SHAPE::default(), subs as u32);
let mut tilings = vec![D3D12_SUBRESOURCE_TILING::default(); subs];
dev.GetResourceTiling(res, Some(&mut total), Some(&mut packed), Some(&mut shape), Some(&mut n), 0, tilings.as_mut_ptr());
if total == 0 {
return None;
}
let (w, h) = ((shape.WidthInTexels / side.max(1)) as usize, (shape.HeightInTexels / side.max(1)) as usize);
// (`packed` is slice 0's; each array slice has its own packed mips, at the same place in its run
// of tiles)
let mips = (res.GetDesc().MipLevels as usize).max(1);
let per_slice = total as u64 / (subs / mips).max(1) as u64;
let packed_owned = |slice: usize| packed.StartTileIndexInOverallResource as u64 + slice as u64 * per_slice + packed.NumTilesForPackedMips as u64 <= own as u64;
// (a level from `NumStandardMips` on is in its slice's packed mips: WARP gives those of slice 1 on
// a start of slice 0's and no tiles, not D3D12_PACKED_TILE)
let parts = (0..subs).map(|i| match tilings.get(i).filter(|t| i < n as usize && i % mips < packed.NumStandardMips as usize && t.StartTileIndexInOverallResource != u32::MAX) {
// (u32::MAX: D3D12_PACKED_TILE, in the packed mips)
None => if packed_owned(i / mips) { vec![ALL] } else { Vec::new() },
Some(t) => {
let (across, tiles) = (t.WidthInTiles as usize, t.WidthInTiles as usize * t.HeightInTiles as usize * t.DepthInTiles as usize);
let k = own.saturating_sub(t.StartTileIndexInOverallResource) as usize;
if k >= tiles {
vec![ALL]
} else if t.DepthInTiles > 1 || across == 0 {
Vec::new()
} else {
let (rows, more) = (k / across, k % across);
[Rect { x0: 0, y0: 0, x1: usize::MAX, y1: rows * h }, Rect { x0: 0, y0: rows * h, x1: more * w, y1: (rows + 1) * h }]
.into_iter()
.filter(|r| r.x0 < r.x1 && r.y0 < r.y1)
.collect()
}
}
});
Some(parts.collect())
}

/// What a worker does at a time.
enum Work {
/// blocks of one row of them
Expand Down Expand Up @@ -1028,7 +1087,7 @@ mod tests {
let want = super::super::title_textures::subresources(mem.as_ptr() as usize, &m.tex);
for round in 0..5 {
let changed = (round > 0).then(|| some_ranges(size, n * 11 + round));
let items = [Item { res: m.res.clone(), src: mem.as_ptr() as usize, tex: Arc::clone(&m.tex), states: vec![D3D12_RESOURCE_STATE_COMMON], changed: changed.clone(), known: None, pages: None }];
let items = [Item { res: m.res.clone(), src: mem.as_ptr() as usize, tex: Arc::clone(&m.tex), states: vec![D3D12_RESOURCE_STATE_COMMON], changed: changed.clone(), known: None, pages: None, own_tiles: None }];
let (mut pieces, mut bytes) = (Vec::new(), 0u64);
plan_item(&dev, 0, &items[0], &mut bytes, &mut pieces).unwrap_or_else(|e| panic!("{kind:?}: {e}"));
if round == 0 {
Expand Down Expand Up @@ -1081,7 +1140,7 @@ mod tests {
let s = m.tex.texels().unwrap();
let size = m.tex.size() as usize;
let first = random_bytes(size, n as u64 * 31 + 3);
let item = |mem: &[u8], changed| Item { res: m.res.clone(), src: mem.as_ptr() as usize, tex: Arc::clone(&m.tex), states: vec![D3D12_RESOURCE_STATE_COMMON], changed, known: None, pages: None };
let item = |mem: &[u8], changed| Item { res: m.res.clone(), src: mem.as_ptr() as usize, tex: Arc::clone(&m.tex), states: vec![D3D12_RESOURCE_STATE_COMMON], changed, known: None, pages: None, own_tiles: None };
let (_, all) = run(&[item(&first, None)]);
let mut want = super::super::title_textures::subresources(first.as_ptr() as usize, &m.tex);
assert!(read_back(&dev, &queue, &m.res, want.len()) == want.iter().map(|w| w.data.clone()).collect::<Vec<_>>(), "{kind:?}: all of it");
Expand Down Expand Up @@ -1171,7 +1230,7 @@ mod tests {
let pages: Vec<usize> = (0..size / PAGE).map(|k| src + k * PAGE).collect();
let look = |of: &[usize]| changed(&[Written { src, size, dirty: of, known: &known, pages: None }], true).0.remove(0);
let upload = |changed: Option<Vec<(usize, usize)>>| {
let it = [Item { res: m.res.clone(), src, tex: Arc::clone(&m.tex), states: vec![D3D12_RESOURCE_STATE_COMMON], changed, known: Some(Arc::clone(&known)), pages: None }];
let it = [Item { res: m.res.clone(), src, tex: Arc::clone(&m.tex), states: vec![D3D12_RESOURCE_STATE_COMMON], changed, known: Some(Arc::clone(&known)), pages: None, own_tiles: None }];
let (mut p, mut bytes) = (Vec::new(), 0u64);
plan_item(&dev, 0, &it[0], &mut bytes, &mut p).unwrap();
let mut buf = vec![0u8; bytes as usize];
Expand Down
Loading
Loading