GPU: marked UAV refreshes cover whole 64 KiB pages (#2172)
A marked UAV view shorter than 64 KiB, or not page-aligned, refreshed only its own range, and Shadow::update split tracked refreshes at the ranges earlier lists' shaders write. Either way Tracked::take saw only part of the enclosing 64 KiB page, so the page was neither guarded nor taken: it stayed changed, and copy_back_marked kept the title's bytes, dropping every GPU write to the page. Marked UAV refreshes (table views, root UAVs, heap hulls) now widen to the 64 KiB pages they touch, cut to the copy (Shadow::page_range); the range declared as written stays exact. Tracked copies take the refreshed range in one call, still updating the snapshots under ranges shaders write. Only pages the title changed are copied, and no shader of the submission has run yet, so the wider refresh overwrites no GPU writes.
This commit is contained in:
1 parent
4989260385
commit
531936636b
3 files changed
+477
-70
No files matched your search
@@ -315,9 +315,14 @@ impl Uploads {
|
||||
/// world submission's heaps reach ~9,000 copies, a few of them written.
|
||||
pub(crate) fn refresh(viewed: &Viewed, mut add: impl FnMut(&Arc<Shadow>, usize, usize)) {
|
||||
let inner = viewed.inner.lock().unwrap_or_else(|e| e.into_inner());
|
||||
for (shadow, address, len, _, bit) in inner.hulls.values() {
|
||||
for (shadow, address, len, uav, bit) in inner.hulls.values() {
|
||||
if !crate::watch::clean_bit(*bit) {
|
||||
add(shadow, *address as usize, *len as usize);
|
||||
let (address, len) = if *uav && shadow.marked() {
|
||||
shadow.page_range(*address, *len)
|
||||
} else {
|
||||
(*address, *len)
|
||||
};
|
||||
add(shadow, address as usize, len as usize);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -912,7 +912,12 @@ impl State {
|
||||
not_run("a root view 4 GiB or more into its copy");
|
||||
return None;
|
||||
};
|
||||
shadow.refresh(list, address, shadow.left(address));
|
||||
let (refresh_at, refresh_len) = if uav && shadow.marked() {
|
||||
shadow.page_range(address, shadow.left(address))
|
||||
} else {
|
||||
(address, shadow.left(address))
|
||||
};
|
||||
shadow.refresh(list, refresh_at, refresh_len);
|
||||
if uav {
|
||||
// Writes through the view are marked wherever they land in the
|
||||
// copy; else declared for 1 MiB.
|
||||
@@ -1874,8 +1879,14 @@ fn readable(list: &HostList, u: &UsedResource, view: &View, marked: bool) {
|
||||
if let Some((shadow, address, len)) = range
|
||||
&& len > 0
|
||||
{
|
||||
shadow.refresh(list, address, len);
|
||||
if u.kind == ResourceType::Uav {
|
||||
let uav = u.kind == ResourceType::Uav;
|
||||
let (refresh_at, refresh_len) = if uav && marked && shadow.marked() {
|
||||
shadow.page_range(address, len)
|
||||
} else {
|
||||
(address, len)
|
||||
};
|
||||
shadow.refresh(list, refresh_at, refresh_len);
|
||||
if uav {
|
||||
shadow.gpu_writes(list, address, len, marked);
|
||||
}
|
||||
}
|
||||
@@ -2034,6 +2045,379 @@ void main(uint3 id : SV_DispatchThreadID) {
|
||||
}
|
||||
"#;
|
||||
|
||||
const MARKED_WRITE_SOURCE: &str = r#"
|
||||
#define RS "DescriptorTable(UAV(u0)), SRV(t0), UAV(u1), RootConstants(num32BitConstants=3, b0)"
|
||||
RWByteAddressBuffer dst : register(u0);
|
||||
ByteAddressBuffer marks : register(t0);
|
||||
RWByteAddressBuffer writes : register(u1);
|
||||
cbuffer c : register(b0) { uint slot; uint byte_offset; uint value; }
|
||||
[RootSignature(RS)]
|
||||
[numthreads(1, 1, 1)]
|
||||
void main() {
|
||||
dst.Store(byte_offset, value);
|
||||
uint2 m = marks.Load2(slot * 8);
|
||||
uint bit = m.x + ((m.y + byte_offset) >> 16);
|
||||
uint old;
|
||||
writes.InterlockedOr((bit >> 5) * 4, 1u << (bit & 31), old);
|
||||
}
|
||||
"#;
|
||||
|
||||
fn buffer_at_address(address: u64, bytes: u64) -> This {
|
||||
let desc: [u32; 14] = [
|
||||
1,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
bytes as u32,
|
||||
(bytes >> 32) as u32,
|
||||
1,
|
||||
1 | 1 << 16,
|
||||
0,
|
||||
1,
|
||||
0,
|
||||
1,
|
||||
0,
|
||||
0,
|
||||
];
|
||||
let mut out = std::ptr::null();
|
||||
let iid = crate::object::guid(
|
||||
0x696442be,
|
||||
0xa72e,
|
||||
0x4059,
|
||||
[0xbc, 0x79, 0x5b, 0x5c, 0x98, 0x04, 0x0f, 0xad],
|
||||
);
|
||||
let made = crate::resource::create_at_address(
|
||||
std::ptr::null(),
|
||||
address,
|
||||
desc.as_ptr().cast(),
|
||||
0,
|
||||
std::ptr::null(),
|
||||
&iid,
|
||||
&mut out,
|
||||
);
|
||||
assert_eq!(made, S_OK);
|
||||
out
|
||||
}
|
||||
|
||||
fn view_heap(
|
||||
count: u32,
|
||||
) -> (
|
||||
This,
|
||||
&'static crate::descriptor::Heap,
|
||||
&'static crate::descriptor::host::HostHeap,
|
||||
) {
|
||||
let null = std::ptr::null();
|
||||
let heap_desc: [u32; 8] = [0, count, 1, 0, 0, 0, 0, 0];
|
||||
let mut heap = null;
|
||||
let iid = crate::object::guid(
|
||||
0x8efb471d,
|
||||
0x616c,
|
||||
0x4f49,
|
||||
[0x90, 0xf7, 0x12, 0x7b, 0xb7, 0x63, 0xfa, 0x51],
|
||||
);
|
||||
assert_eq!(
|
||||
crate::descriptor::create_heap(null, heap_desc.as_ptr().cast(), &iid, &mut heap),
|
||||
S_OK
|
||||
);
|
||||
let title_heap = crate::descriptor::heap(heap);
|
||||
let heap_host = title_heap.host().expect("a host view heap");
|
||||
(heap, title_heap, heap_host)
|
||||
}
|
||||
|
||||
fn dispatch_marked_uav(
|
||||
list: &HostList,
|
||||
heap_host: &crate::descriptor::host::HostHeap,
|
||||
(root, pso): &(ID3D12RootSignature, ID3D12PipelineState),
|
||||
table_slot: u32,
|
||||
mark_slot: u32,
|
||||
byte_offset: u32,
|
||||
value: u32,
|
||||
) {
|
||||
let marks = heap_host.marks().expect("marks table");
|
||||
let writes = crate::shadow::view_writes(false).expect("view_writes bitmap");
|
||||
// SAFETY: live resource.
|
||||
let marks_gpu = unsafe { marks.GetGPUVirtualAddress() };
|
||||
list.keep(marks);
|
||||
let constants = [mark_slot, byte_offset, value];
|
||||
// SAFETY: an open list and live objects.
|
||||
unsafe {
|
||||
list.list
|
||||
.SetDescriptorHeaps(&[Some(heap_host.heap.clone())]);
|
||||
list.list.SetComputeRootSignature(root);
|
||||
list.list
|
||||
.SetComputeRootDescriptorTable(0, heap_host.gpu(table_slot));
|
||||
list.list.SetComputeRootShaderResourceView(1, marks_gpu);
|
||||
list.list.SetComputeRootUnorderedAccessView(2, writes);
|
||||
list.list
|
||||
.SetComputeRoot32BitConstants(3, 3, constants.as_ptr().cast(), 0);
|
||||
list.list.SetPipelineState(pso);
|
||||
list.list.Dispatch(1, 1, 1);
|
||||
}
|
||||
}
|
||||
|
||||
/// Sub-64 KiB marked UAV writes must expand `Shadow::refresh` to the
|
||||
/// enclosing 64 KiB page so `Tracked::take` guards it before execution
|
||||
/// and `copy_back_marks` writes GPU bytes back while keeping neighbor
|
||||
/// guest bytes intact.
|
||||
#[test]
|
||||
fn marked_subpage_uav_writes_copy_back_and_keep_neighbor_bytes() {
|
||||
let Some(host) = crate::host::host() else {
|
||||
return;
|
||||
};
|
||||
let p = crate::watch::tests::pool(1);
|
||||
// SAFETY: committed write-watched page of 64 KiB.
|
||||
unsafe {
|
||||
((p + 0x100) as *mut u32).write_volatile(0x1122_3344);
|
||||
((p + 0x2000) as *mut u32).write_volatile(0x5566_7788);
|
||||
}
|
||||
let buf = buffer_at_address(p as u64, crate::watch::PAGE as u64);
|
||||
let (_, title_heap, heap_host) = view_heap(8);
|
||||
let _ = host.log_messages();
|
||||
|
||||
let uav_desc: [u32; 10] = [39, 1, 1024, 0, 64, 0, 0, 0, 1, 0];
|
||||
let slot0 = title_heap.start;
|
||||
crate::descriptor::create_uav(std::ptr::null(), buf, std::ptr::null(), &uav_desc, slot0);
|
||||
let view = crate::descriptor::view(slot0).expect("subpage uav view");
|
||||
let _ = heap_host.mirror(0, slot0, true);
|
||||
|
||||
let shader = compile_cs(MARKED_WRITE_SOURCE);
|
||||
let list = host.new_list().unwrap();
|
||||
readable(&list, &used(ResourceType::Uav), &view, true);
|
||||
dispatch_marked_uav(&list, heap_host, &shader, 0, 0, 0, 0xDEAD_BEEF);
|
||||
list.close();
|
||||
host.execute(&[&list]);
|
||||
|
||||
assert_eq!(host.log_messages(), None);
|
||||
// SAFETY: bytes inside our page.
|
||||
unsafe {
|
||||
assert_eq!(((p + 4096) as *const u32).read_volatile(), 0xDEAD_BEEF);
|
||||
assert_eq!(((p + 0x100) as *const u32).read_volatile(), 0x1122_3344);
|
||||
assert_eq!(((p + 0x2000) as *const u32).read_volatile(), 0x5566_7788);
|
||||
}
|
||||
crate::shadow::unregister(buf as usize, p as u64);
|
||||
}
|
||||
|
||||
/// A title copy to a disjoint offset on the same 64 KiB page while a
|
||||
/// sub-64 KiB marked UAV is pending arms `Tracked::saved`, and
|
||||
/// `copy_back_marks` 3-way merges GPU, CPU, and pre-existing bytes.
|
||||
#[test]
|
||||
fn marked_subpage_uav_and_cpu_disjoint_write_three_way_merge() {
|
||||
let Some(host) = crate::host::host() else {
|
||||
return;
|
||||
};
|
||||
let p = crate::watch::tests::pool(1);
|
||||
// SAFETY: committed write-watched page of 64 KiB.
|
||||
unsafe {
|
||||
((p + 0x100) as *mut u32).write_volatile(0x1122_3344);
|
||||
}
|
||||
let buf = buffer_at_address(p as u64, crate::watch::PAGE as u64);
|
||||
let (_, title_heap, heap_host) = view_heap(8);
|
||||
let _ = host.log_messages();
|
||||
|
||||
let uav_desc: [u32; 10] = [39, 1, 1024, 0, 64, 0, 0, 0, 1, 0];
|
||||
let slot0 = title_heap.start;
|
||||
crate::descriptor::create_uav(std::ptr::null(), buf, std::ptr::null(), &uav_desc, slot0);
|
||||
let view = crate::descriptor::view(slot0).expect("subpage uav view");
|
||||
let _ = heap_host.mirror(0, slot0, true);
|
||||
|
||||
let cpu_val = 0xCAFE_BABEu32.to_le_bytes();
|
||||
let shader = compile_cs(MARKED_WRITE_SOURCE);
|
||||
let list = host.new_list().unwrap();
|
||||
readable(&list, &used(ResourceType::Uav), &view, true);
|
||||
list.defer_copy((p + 0x2000) as *mut u8, cpu_val.as_ptr(), cpu_val.len());
|
||||
dispatch_marked_uav(&list, heap_host, &shader, 0, 0, 0, 0xDEAD_BEEF);
|
||||
list.close();
|
||||
host.execute(&[&list]);
|
||||
|
||||
assert_eq!(host.log_messages(), None);
|
||||
// SAFETY: bytes inside our page.
|
||||
unsafe {
|
||||
assert_eq!(((p + 4096) as *const u32).read_volatile(), 0xDEAD_BEEF);
|
||||
assert_eq!(((p + 0x2000) as *const u32).read_volatile(), 0xCAFE_BABE);
|
||||
assert_eq!(((p + 0x100) as *const u32).read_volatile(), 0x1122_3344);
|
||||
}
|
||||
crate::shadow::unregister(buf as usize, p as u64);
|
||||
}
|
||||
|
||||
/// Unaligned and `< 64 KiB` / cross-page `Shadow`s clamp `page_range`
|
||||
/// cleanly and copy back marked sub-range UAV writes across the page
|
||||
/// boundary without disturbing surrounding title memory.
|
||||
#[test]
|
||||
fn marked_subpage_uav_unaligned_and_cross_page_shadow_bounds() {
|
||||
let Some(host) = crate::host::host() else {
|
||||
return;
|
||||
};
|
||||
let p = crate::watch::tests::pool(2);
|
||||
let shadow_addr = p as u64 + 0xFF00;
|
||||
let shadow_len = 0x300u64;
|
||||
// Pure `Shadow::page_range` checks: single-page unaligned `< 64 KiB`.
|
||||
let small = p as u64 + 0x1000;
|
||||
crate::shadow::register(1, small, 0x2000);
|
||||
let small_copy = crate::shadow::find(small, 0x2000).expect("small shadow");
|
||||
assert_eq!(small_copy.page_range(small + 0x400, 0x100), (small, 0x2000));
|
||||
assert_eq!(small_copy.page_range(small, 0), (small, 0));
|
||||
assert_eq!(
|
||||
small_copy.page_range(small + 0x3000, 0x100),
|
||||
(small + 0x2000, 0)
|
||||
);
|
||||
assert_eq!(
|
||||
small_copy.page_range(u64::MAX - 8, 128),
|
||||
(small + 0x2000, 0)
|
||||
);
|
||||
crate::shadow::unregister(1, small);
|
||||
|
||||
// SAFETY: sentinel bytes outside `[shadow_addr, shadow_addr + shadow_len)`.
|
||||
unsafe {
|
||||
((p + 0xFE00) as *mut u32).write_volatile(0xA1A2_A3A4);
|
||||
((p + 0x1_0300) as *mut u32).write_volatile(0xB1B2_B3B4);
|
||||
}
|
||||
let buf = buffer_at_address(shadow_addr, shadow_len);
|
||||
let shadow = crate::shadow::find(shadow_addr, shadow_len).expect("cross-page shadow");
|
||||
assert_eq!(shadow.page_range(shadow_addr, 0x80), (shadow_addr, 0x100));
|
||||
assert_eq!(
|
||||
shadow.page_range(p as u64 + 0x1_0000, 0x80),
|
||||
(p as u64 + 0x1_0000, 0x200)
|
||||
);
|
||||
assert_eq!(
|
||||
shadow.page_range(shadow_addr + 0x80, 0x100),
|
||||
(shadow_addr, shadow_len)
|
||||
);
|
||||
|
||||
let (_, title_heap, heap_host) = view_heap(8);
|
||||
let _ = host.log_messages();
|
||||
// UAV `[p + 0xFF80 .. p + 0x1_0080)` crosses the 64 KiB page boundary.
|
||||
let uav_desc: [u32; 10] = [39, 1, 32, 0, 64, 0, 0, 0, 1, 0];
|
||||
let slot0 = title_heap.start;
|
||||
crate::descriptor::create_uav(std::ptr::null(), buf, std::ptr::null(), &uav_desc, slot0);
|
||||
let view = crate::descriptor::view(slot0).expect("cross-page subrange uav");
|
||||
let _ = heap_host.mirror(0, slot0, true);
|
||||
|
||||
let shader = compile_cs(MARKED_WRITE_SOURCE);
|
||||
let list = host.new_list().unwrap();
|
||||
readable(&list, &used(ResourceType::Uav), &view, true);
|
||||
dispatch_marked_uav(&list, heap_host, &shader, 0, 0, 0, 0x1234_5678);
|
||||
dispatch_marked_uav(&list, heap_host, &shader, 0, 0, 0x80, 0x9ABC_DEF0);
|
||||
list.close();
|
||||
host.execute(&[&list]);
|
||||
|
||||
assert_eq!(host.log_messages(), None);
|
||||
// SAFETY: bytes inside our 2-page pool.
|
||||
unsafe {
|
||||
assert_eq!(((p + 0xFF80) as *const u32).read_volatile(), 0x1234_5678);
|
||||
assert_eq!(((p + 0x1_0000) as *const u32).read_volatile(), 0x9ABC_DEF0);
|
||||
assert_eq!(((p + 0xFE00) as *const u32).read_volatile(), 0xA1A2_A3A4);
|
||||
assert_eq!(((p + 0x1_0300) as *const u32).read_volatile(), 0xB1B2_B3B4);
|
||||
}
|
||||
crate::shadow::unregister(buf as usize, shadow_addr);
|
||||
}
|
||||
|
||||
/// Multiple sub-64 KiB marked UAVs and a preceding `Step::Copy` on the
|
||||
/// same 64 KiB page within one command list all land in title memory.
|
||||
#[test]
|
||||
fn marked_subpage_uavs_after_copy_in_same_list() {
|
||||
let Some(host) = crate::host::host() else {
|
||||
return;
|
||||
};
|
||||
let p = crate::watch::tests::pool(1);
|
||||
let buf = buffer_at_address(p as u64, crate::watch::PAGE as u64);
|
||||
let (_, title_heap, heap_host) = view_heap(8);
|
||||
let _ = host.log_messages();
|
||||
|
||||
let uav1_desc: [u32; 10] = [39, 1, 1024, 0, 64, 0, 0, 0, 1, 0];
|
||||
let uav2_desc: [u32; 10] = [39, 1, 8192, 0, 64, 0, 0, 0, 1, 0];
|
||||
let slot0 = title_heap.start;
|
||||
let slot1 = title_heap.start + 32;
|
||||
crate::descriptor::create_uav(std::ptr::null(), buf, std::ptr::null(), &uav1_desc, slot0);
|
||||
crate::descriptor::create_uav(std::ptr::null(), buf, std::ptr::null(), &uav2_desc, slot1);
|
||||
let view1 = crate::descriptor::view(slot0).expect("uav1");
|
||||
let view2 = crate::descriptor::view(slot1).expect("uav2");
|
||||
let _ = heap_host.mirror(0, slot0, true);
|
||||
let _ = heap_host.mirror(1, slot1, true);
|
||||
|
||||
let copy_src = 0x1111_1111u32.to_le_bytes();
|
||||
let shader = compile_cs(MARKED_WRITE_SOURCE);
|
||||
let list = host.new_list().unwrap();
|
||||
list.defer_copy((p + 0x200) as *mut u8, copy_src.as_ptr(), copy_src.len());
|
||||
readable(&list, &used(ResourceType::Uav), &view1, true);
|
||||
dispatch_marked_uav(&list, heap_host, &shader, 0, 0, 0, 0x2222_2222);
|
||||
readable(&list, &used(ResourceType::Uav), &view2, true);
|
||||
dispatch_marked_uav(&list, heap_host, &shader, 1, 1, 0, 0x3333_3333);
|
||||
list.close();
|
||||
host.execute(&[&list]);
|
||||
|
||||
assert_eq!(host.log_messages(), None);
|
||||
// SAFETY: bytes inside our page.
|
||||
unsafe {
|
||||
assert_eq!(((p + 0x200) as *const u32).read_volatile(), 0x1111_1111);
|
||||
assert_eq!(((p + 0x1000) as *const u32).read_volatile(), 0x2222_2222);
|
||||
assert_eq!(((p + 0x8000) as *const u32).read_volatile(), 0x3333_3333);
|
||||
}
|
||||
crate::shadow::unregister(buf as usize, p as u64);
|
||||
}
|
||||
|
||||
/// A root UAV bound at a non-64 KiB-aligned offset (`State::root_view`)
|
||||
/// and a bindless heap UAV hull (`crate::bindless::refresh`) both expand
|
||||
/// their refresh to enclosing 64 KiB page bounds.
|
||||
#[test]
|
||||
fn marked_unaligned_root_uav_and_bindless_subpage_refresh() {
|
||||
let Some(host) = crate::host::host() else {
|
||||
return;
|
||||
};
|
||||
let p = crate::watch::tests::pool(2);
|
||||
// SAFETY: committed write-watched 2-page buffer.
|
||||
unsafe {
|
||||
((p + 0x100) as *mut u32).write_volatile(0x1122_3344);
|
||||
((p + 0x1_0100) as *mut u32).write_volatile(0x5566_7788);
|
||||
}
|
||||
let buf = buffer_at_address(p as u64, (2 * crate::watch::PAGE) as u64);
|
||||
let (heap, title_heap, heap_host) = view_heap(8);
|
||||
let _ = host.log_messages();
|
||||
|
||||
let shader = compile_cs(MARKED_WRITE_SOURCE);
|
||||
let list = host.new_list().unwrap();
|
||||
let mut state = State::default();
|
||||
state.set_heaps(&[heap]);
|
||||
state.set_view(true, 0, p as u64 + 4096);
|
||||
let (index, offset) = state.root_view(&list, true, 0, true).expect("root uav");
|
||||
assert_eq!(offset, 4096);
|
||||
dispatch_marked_uav(&list, heap_host, &shader, index, index, offset, 0xBEEF_CAFE);
|
||||
list.close();
|
||||
host.execute(&[&list]);
|
||||
|
||||
assert_eq!(host.log_messages(), None);
|
||||
// SAFETY: bytes inside our 2-page pool.
|
||||
unsafe {
|
||||
assert_eq!(((p + 4096) as *const u32).read_volatile(), 0xBEEF_CAFE);
|
||||
assert_eq!(((p + 0x100) as *const u32).read_volatile(), 0x1122_3344);
|
||||
assert_eq!(((p + 0x1_0100) as *const u32).read_volatile(), 0x5566_7788);
|
||||
}
|
||||
crate::shadow::unregister(buf as usize, p as u64);
|
||||
|
||||
// Bindless refresh expands marked UAV hulls to `page_range` while keeping SRV hulls exact.
|
||||
let p2 = crate::watch::tests::pool(1);
|
||||
let buf2 = buffer_at_address(p2 as u64, crate::watch::PAGE as u64);
|
||||
let uav_desc: [u32; 10] = [39, 1, 1024, 0, 64, 0, 0, 0, 1, 0];
|
||||
let srv_desc: [u32; 10] = [39, 1, 0x1688, 0, 2048, 0, 64, 0, 1, 0];
|
||||
crate::descriptor::create_uav(
|
||||
std::ptr::null(),
|
||||
buf2,
|
||||
std::ptr::null(),
|
||||
&uav_desc,
|
||||
title_heap.start + 32,
|
||||
);
|
||||
crate::descriptor::create_srv(std::ptr::null(), buf2, &srv_desc, title_heap.start + 64);
|
||||
let uav_view = crate::descriptor::view(title_heap.start + 32).expect("bindless uav");
|
||||
let srv_view = crate::descriptor::view(title_heap.start + 64).expect("bindless srv");
|
||||
let viewed = crate::bindless::Viewed::default();
|
||||
viewed.set(0, Some(&uav_view), true);
|
||||
viewed.set(1, Some(&srv_view), true);
|
||||
let mut refreshed = Vec::new();
|
||||
crate::bindless::refresh(&viewed, |_, a, l| refreshed.push((a, l)));
|
||||
refreshed.sort_unstable();
|
||||
assert_eq!(refreshed, vec![(p2, crate::watch::PAGE), (p2 + 8192, 256)]);
|
||||
crate::shadow::unregister(buf2 as usize, p2 as u64);
|
||||
}
|
||||
|
||||
fn compile() -> (ID3D12RootSignature, ID3D12PipelineState) {
|
||||
compile_cs(SOURCE)
|
||||
}
|
||||
|
||||
@@ -370,6 +370,23 @@ impl Shadow {
|
||||
segments.insert(first, (start, grown));
|
||||
}
|
||||
|
||||
/// `[address, address + len)` expanded outward to 64 KiB page
|
||||
/// boundaries and cut to the copy (`[start, start + len)`): whole pages
|
||||
/// so [`crate::watch::Tracked::take`] guards them for
|
||||
/// [`Shadow::copy_back_marked`].
|
||||
pub(crate) fn page_range(&self, address: u64, len: u64) -> (u64, u64) {
|
||||
let end = self.start.saturating_add(self.len);
|
||||
let from = address.clamp(self.start, end);
|
||||
let to = address.saturating_add(len).clamp(from, end);
|
||||
if from >= to {
|
||||
return (from, 0);
|
||||
}
|
||||
let page = PAGE as u64;
|
||||
let from = (from & !(page - 1)).max(self.start);
|
||||
let to = to.checked_next_multiple_of(page).unwrap_or(end).min(end);
|
||||
(from, to - from)
|
||||
}
|
||||
|
||||
/// After the submission ran: copies the bytes its shaders wrote back
|
||||
/// into title memory (unmapped pages left alone), and only those: the
|
||||
/// title's threads write the same buffers while the GPU runs (its
|
||||
@@ -547,53 +564,59 @@ impl Shadow {
|
||||
.copied()
|
||||
.filter(|&(a, e)| a < to && e > from)
|
||||
.collect();
|
||||
if self.tracked.is_some() {
|
||||
return self.update_tracked(from, to, !busy.is_empty());
|
||||
}
|
||||
if !busy.is_empty() {
|
||||
// Around ranges shaders are writing: the gaps, each as below
|
||||
// (in the world most submissions have such ranges; copying the
|
||||
// gaps whole cost ~1 s per frame). Under them, what the title
|
||||
// wrote since it was taken (`update_busy`).
|
||||
// gaps whole cost ~1 s per frame). Under them nothing: what the
|
||||
// title wrote there is not known.
|
||||
busy.sort_unstable();
|
||||
let (mut at, mut bytes) = (from, 0);
|
||||
for (a, e) in busy {
|
||||
let (a, e) = (a.max(from), e.min(to));
|
||||
if a > at {
|
||||
bytes += self.update_free(at, a);
|
||||
}
|
||||
if e > a.max(at) {
|
||||
bytes += self.update_busy(a.max(at), e);
|
||||
bytes += self.update_untracked(at, a);
|
||||
}
|
||||
at = at.max(e);
|
||||
}
|
||||
if at < to {
|
||||
bytes += self.update_free(at, to);
|
||||
bytes += self.update_untracked(at, to);
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
self.update_free(from, to)
|
||||
self.update_untracked(from, to)
|
||||
}
|
||||
|
||||
/// [`Shadow::update`] of `[from, to)` where a list submitted earlier
|
||||
/// declared that shaders write. A submission waits for the one before it,
|
||||
/// so none of this submission's shaders has run yet: the pages the title
|
||||
/// wrote since they were taken are copied as anywhere else. Skipped, the
|
||||
/// title's constants there stayed stale for the whole submission (a
|
||||
/// dispatch read its sizes as zeros and looped until the device was
|
||||
/// removed). The same bytes go into the snapshot `writing` took, so
|
||||
/// copy-back neither takes them for the GPU's nor puts them back over a
|
||||
/// later title write.
|
||||
fn update_busy(&self, from: usize, to: usize) -> usize {
|
||||
/// [`Shadow::update`] of `[from, to)` (inside the buffer) where the
|
||||
/// memory is tracked: the pages the title wrote since they were taken,
|
||||
/// in one [`crate::watch::Tracked::take`], so that a page wholly in the
|
||||
/// range is guarded even where shaders write part of it (split at those
|
||||
/// ranges it stayed changed, and [`Shadow::copy_back_marked`] dropped
|
||||
/// what they wrote).
|
||||
///
|
||||
/// `busy`: a list submitted earlier declared that shaders write in the
|
||||
/// range. A submission waits for the one before it, so none of this
|
||||
/// submission's shaders has run yet: the pages the title wrote are
|
||||
/// copied as anywhere else. Skipped, the title's constants there stayed
|
||||
/// stale for the whole submission (a dispatch read its sizes as zeros
|
||||
/// and looped until the device was removed). The same bytes go into the
|
||||
/// snapshot `writing` took, so copy-back neither takes them for the
|
||||
/// GPU's nor puts them back over a later title write.
|
||||
fn update_tracked(&self, from: usize, to: usize, busy: bool) -> usize {
|
||||
let Some(t) = &self.tracked else {
|
||||
return 0;
|
||||
};
|
||||
let runs = t.take(from, to);
|
||||
if runs.is_empty() {
|
||||
return 0;
|
||||
}
|
||||
#[cfg(feature = "diagnostics")]
|
||||
let started = std::time::Instant::now();
|
||||
let (runs, unmapped) = t.take_split(from, to);
|
||||
#[cfg(feature = "diagnostics")]
|
||||
crate::copystat::add(crate::copystat::C::WatchUs, started.elapsed().as_micros() as u64);
|
||||
let start = self.start as usize;
|
||||
let mut before = self.before.lock().unwrap_or_else(|e| e.into_inner());
|
||||
let mut copied = 0;
|
||||
for (a, b) in runs {
|
||||
copied += self.copy(a, b);
|
||||
let mut before = busy.then(|| self.before.lock().unwrap_or_else(|e| e.into_inner()));
|
||||
let mut sync_before = |a: usize, b: usize| {
|
||||
let Some(before) = &mut before else { return };
|
||||
let (a, b) = (a - start, b - start);
|
||||
for (at, bytes) in before.0.iter_mut() {
|
||||
let (s, e) = ((*at).max(a), (*at + bytes.len()).min(b));
|
||||
@@ -603,49 +626,44 @@ impl Shadow {
|
||||
bytes[s - *at..e - *at].copy_from_slice(host);
|
||||
}
|
||||
}
|
||||
};
|
||||
let mut copied = 0;
|
||||
for (a, b) in runs {
|
||||
copied += self.copy(a, b);
|
||||
sync_before(a, b);
|
||||
}
|
||||
for (a, b) in unmapped {
|
||||
// SAFETY: inside the mapping (`take_split` cuts to the buffer).
|
||||
unsafe { self.data.add(a - start).write_bytes(0, b - a) };
|
||||
sync_before(a, b);
|
||||
#[cfg(feature = "diagnostics")]
|
||||
crate::copystat::add(crate::copystat::C::Unmapped, (b - a) as u64);
|
||||
}
|
||||
// Parts on memory that is not the runtime's: whole, as below.
|
||||
for (a, b) in t.untracked(from, to) {
|
||||
#[cfg(feature = "diagnostics")]
|
||||
let started = std::time::Instant::now();
|
||||
copied += self.copy(a, b);
|
||||
sync_before(a, b);
|
||||
#[cfg(feature = "diagnostics")]
|
||||
{
|
||||
use crate::copystat::{C, add};
|
||||
add(C::WholeBytes, (b - a) as u64);
|
||||
add(C::WholeUs, started.elapsed().as_micros() as u64);
|
||||
}
|
||||
}
|
||||
#[cfg(feature = "diagnostics")]
|
||||
{
|
||||
use crate::copystat::{C, add};
|
||||
add(C::Watches, 1);
|
||||
add(C::Checked, (to - from) as u64);
|
||||
add(C::Unchanged, (to - from).saturating_sub(copied) as u64);
|
||||
}
|
||||
copied
|
||||
}
|
||||
|
||||
/// [`Shadow::update`] of `[from, to)` (inside the buffer) where no
|
||||
/// shader writes: the pages changed since taken, or all of it where the
|
||||
/// memory is not tracked.
|
||||
fn update_free(&self, from: usize, to: usize) -> usize {
|
||||
if let Some(t) = &self.tracked {
|
||||
#[cfg(feature = "diagnostics")]
|
||||
let started = std::time::Instant::now();
|
||||
let (runs, unmapped) = t.take_split(from, to);
|
||||
#[cfg(feature = "diagnostics")]
|
||||
crate::copystat::add(crate::copystat::C::WatchUs, started.elapsed().as_micros() as u64);
|
||||
// Cut to the range: next to it shaders may be writing the copy.
|
||||
let mut copied: usize = runs.into_iter().map(|(a, b)| self.copy(a, b)).sum();
|
||||
for (a, b) in unmapped {
|
||||
// SAFETY: inside the mapping (`take_split` cuts to the buffer).
|
||||
unsafe { self.data.add(a - self.start as usize).write_bytes(0, b - a) };
|
||||
#[cfg(feature = "diagnostics")]
|
||||
crate::copystat::add(crate::copystat::C::Unmapped, (b - a) as u64);
|
||||
}
|
||||
// Parts on memory that is not the runtime's: whole, as below.
|
||||
for (a, b) in t.untracked(from, to) {
|
||||
#[cfg(feature = "diagnostics")]
|
||||
let started = std::time::Instant::now();
|
||||
copied += self.copy(a, b);
|
||||
#[cfg(feature = "diagnostics")]
|
||||
{
|
||||
use crate::copystat::{C, add};
|
||||
add(C::WholeBytes, (b - a) as u64);
|
||||
add(C::WholeUs, started.elapsed().as_micros() as u64);
|
||||
}
|
||||
}
|
||||
#[cfg(feature = "diagnostics")]
|
||||
{
|
||||
use crate::copystat::{C, add};
|
||||
add(C::Watches, 1);
|
||||
add(C::Checked, (to - from) as u64);
|
||||
add(C::Unchanged, (to - from).saturating_sub(copied) as u64);
|
||||
}
|
||||
return copied;
|
||||
}
|
||||
/// [`Shadow::update`] of untracked `[from, to)` (inside the buffer).
|
||||
fn update_untracked(&self, from: usize, to: usize) -> usize {
|
||||
#[cfg(feature = "diagnostics")]
|
||||
let started = std::time::Instant::now();
|
||||
self.copy(from, to);
|
||||
|
||||
Reference in new issue
Block a user