mirror of
https://github.com/ivall/xcl.git
synced 2026-11-24 05:14:57 +01:00
Tests: read back mirrored textures across writes and page remapping
Exercise the merged GPU-view mirror path end to end on a synthetic R8 texture. Force watched Windows reservations outside the UMA windows, verify CPU rewrites reach the GPU, then verify a later lazy physical mapping overrides the mirror and unmapping restores it. Keep the GPU-view title address throughout and clean up reservations, physical pages and texture registration on failure. No retail data or runtime changes.
This commit is contained in:
@@ -1018,6 +1018,143 @@ unsafe fn subresources(va: usize, tex: &xg::Texture) -> Vec<Subresource> {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// Real uploads, dirty-page detection and a source changing after registration:
|
||||
/// bookkeeping alone cannot show that the bytes the GPU sees follow the mirror.
|
||||
#[test]
|
||||
fn gpu_mirror_upload_follows_cpu_writes_and_later_mapping_changes() {
|
||||
use crate::hle::kernelx;
|
||||
use windows::Win32::Graphics::Dxgi::Common::{DXGI_FORMAT_R8_UNORM, DXGI_SAMPLE_DESC};
|
||||
use windows_sys::Win32::System::Memory as mem;
|
||||
|
||||
let _vram_guard = super::super::vram::VRAM_TEST_LOCK.lock().unwrap();
|
||||
let _guard = TEST_LOCK.lock().unwrap();
|
||||
if super::super::backend() != super::super::Backend::Real {
|
||||
crate::gpu::recomp::runner::skip_test("no GPU");
|
||||
return;
|
||||
}
|
||||
// Larger than all four UMA windows together. These public allocations
|
||||
// must use the watched Windows fallback, reserving space rather than RAM.
|
||||
const SIZE: usize = 24usize << 30;
|
||||
const OFFSET: usize = 0x2_0000;
|
||||
const PAGE: usize = 0x1_0000;
|
||||
struct Reservation(usize);
|
||||
impl Drop for Reservation {
|
||||
fn drop(&mut self) {
|
||||
unsafe { kernelx::VirtualFree(self.0 as *mut c_void, 0, mem::MEM_RELEASE); }
|
||||
}
|
||||
}
|
||||
struct RegisteredTexture(usize);
|
||||
impl Drop for RegisteredTexture {
|
||||
fn drop(&mut self) { forget(self.0); }
|
||||
}
|
||||
struct PhysicalPage { id: usize, at: usize }
|
||||
impl Drop for PhysicalPage {
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
if kernelx::physical_page_at(self.at) {
|
||||
kernelx::MapTitlePhysicalPages(self.at as *mut c_void, 1, 0, 4, std::ptr::null_mut());
|
||||
}
|
||||
kernelx::FreeTitlePhysicalPages(std::ptr::null_mut(), 1, &mut self.id);
|
||||
}
|
||||
}
|
||||
}
|
||||
unsafe fn reserve(protect: u32) -> Reservation {
|
||||
let at = kernelx::VirtualAlloc(std::ptr::null(), SIZE, 0x2040_2000, protect) as usize;
|
||||
assert_ne!(at, 0, "reserve-only fallback must succeed");
|
||||
let reservation = Reservation(at);
|
||||
assert!(!crate::gpu::uma::get().unwrap().contains(at), "exercise the Windows fallback");
|
||||
assert!(kernelx::in_graphics_reservation(at), "fallback must be write-watched");
|
||||
reservation
|
||||
}
|
||||
unsafe fn read_r8(dev: &ID3D12Device, queue: &ID3D12CommandQueue, texture: &ID3D12Resource) -> Vec<u8> {
|
||||
own_work(|| {
|
||||
let mut footprint = D3D12_PLACED_SUBRESOURCE_FOOTPRINT::default();
|
||||
let mut total = 0;
|
||||
dev.GetCopyableFootprints(&texture.GetDesc(), 0, 1, 0, Some(&mut footprint), None, None, Some(&mut total));
|
||||
let mut readback = None;
|
||||
dev.CreateCommittedResource(
|
||||
&D3D12_HEAP_PROPERTIES { Type: D3D12_HEAP_TYPE_READBACK, ..Default::default() }, D3D12_HEAP_FLAG_NONE,
|
||||
&D3D12_RESOURCE_DESC { Dimension: D3D12_RESOURCE_DIMENSION_BUFFER, Width: total, Height: 1, DepthOrArraySize: 1, MipLevels: 1, SampleDesc: DXGI_SAMPLE_DESC { Count: 1, Quality: 0 }, Layout: D3D12_TEXTURE_LAYOUT_ROW_MAJOR, ..Default::default() },
|
||||
D3D12_RESOURCE_STATE_COPY_DEST, None, &mut readback,
|
||||
).unwrap();
|
||||
let readback: ID3D12Resource = readback.unwrap();
|
||||
let allocator: ID3D12CommandAllocator = dev.CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT).unwrap();
|
||||
let list: ID3D12GraphicsCommandList = dev.CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, &allocator, None).unwrap();
|
||||
let barrier = |before, after| D3D12_RESOURCE_BARRIER {
|
||||
Type: D3D12_RESOURCE_BARRIER_TYPE_TRANSITION,
|
||||
Anonymous: D3D12_RESOURCE_BARRIER_0 { Transition: std::mem::ManuallyDrop::new(D3D12_RESOURCE_TRANSITION_BARRIER { pResource: std::mem::transmute_copy(texture), Subresource: 0, StateBefore: before, StateAfter: after }) },
|
||||
..Default::default()
|
||||
};
|
||||
list.ResourceBarrier(&[barrier(D3D12_RESOURCE_STATE_COMMON, D3D12_RESOURCE_STATE_COPY_SOURCE)]);
|
||||
let dst = D3D12_TEXTURE_COPY_LOCATION { pResource: std::mem::transmute_copy(&readback), Type: D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT, Anonymous: D3D12_TEXTURE_COPY_LOCATION_0 { PlacedFootprint: footprint } };
|
||||
let src = D3D12_TEXTURE_COPY_LOCATION { pResource: std::mem::transmute_copy(texture), Type: D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX, Anonymous: D3D12_TEXTURE_COPY_LOCATION_0 { SubresourceIndex: 0 } };
|
||||
list.CopyTextureRegion(&dst, 0, 0, 0, &src, None);
|
||||
list.ResourceBarrier(&[barrier(D3D12_RESOURCE_STATE_COPY_SOURCE, D3D12_RESOURCE_STATE_COMMON)]);
|
||||
list.Close().unwrap();
|
||||
queue.ExecuteCommandLists(&[Some(list.cast().unwrap())]);
|
||||
let fence: ID3D12Fence = dev.CreateFence(0, D3D12_FENCE_FLAG_NONE).unwrap();
|
||||
queue.Signal(&fence, 1).unwrap();
|
||||
crate::gpu::fence::wait(&fence, 1, dev, None, 5_000).unwrap();
|
||||
let mut data = std::ptr::null_mut();
|
||||
readback.Map(0, None, Some(&mut data)).unwrap();
|
||||
let mut out = Vec::with_capacity(64 * 64);
|
||||
for y in 0..64 {
|
||||
out.extend_from_slice(std::slice::from_raw_parts((data as *const u8).add(footprint.Offset as usize + y * footprint.Footprint.RowPitch as usize), 64));
|
||||
}
|
||||
readback.Unmap(0, None);
|
||||
out
|
||||
})
|
||||
}
|
||||
unsafe {
|
||||
let raw_device = super::super::real::device_raw();
|
||||
let dev = ID3D12Device::from_raw_borrowed(&raw_device).unwrap();
|
||||
let queue: ID3D12CommandQueue = dev.CreateCommandQueue(&D3D12_COMMAND_QUEUE_DESC::default()).unwrap();
|
||||
let cpu = reserve(mem::PAGE_READWRITE);
|
||||
let gpu = reserve(0x801);
|
||||
let (src, va) = (cpu.0 + OFFSET, gpu.0 + OFFSET);
|
||||
assert_eq!(kernelx::mirrors::source(va), Some(src));
|
||||
assert_eq!(kernelx::VirtualAlloc(src as *const c_void, PAGE, mem::MEM_COMMIT, mem::PAGE_READWRITE) as usize, src);
|
||||
let console = console_desc(3, 64, 64, 1, 1, DXGI_FORMAT_R8_UNORM.0 as u32, 1);
|
||||
assert!(xg::Texture::new(&console).unwrap().size() <= PAGE as u64);
|
||||
let mut texture = None;
|
||||
dev.CreateCommittedResource(
|
||||
&D3D12_HEAP_PROPERTIES { Type: D3D12_HEAP_TYPE_DEFAULT, ..Default::default() }, D3D12_HEAP_FLAG_NONE,
|
||||
&D3D12_RESOURCE_DESC { Dimension: D3D12_RESOURCE_DIMENSION_TEXTURE2D, Width: 64, Height: 64, DepthOrArraySize: 1, MipLevels: 1, Format: DXGI_FORMAT_R8_UNORM, SampleDesc: DXGI_SAMPLE_DESC { Count: 1, Quality: 0 }, ..Default::default() },
|
||||
D3D12_RESOURCE_STATE_COMMON, None, &mut texture,
|
||||
).unwrap();
|
||||
let texture: ID3D12Resource = texture.unwrap();
|
||||
let key = texture.as_raw() as usize;
|
||||
register(raw_device, texture.as_raw(), va, &console, D3D12_RESOURCE_STATE_COMMON).unwrap();
|
||||
let _registered = RegisteredTexture(key);
|
||||
for value in [0x3D, 0x9A] {
|
||||
std::ptr::write_bytes(src as *mut u8, value, PAGE);
|
||||
refresh(queue.as_raw());
|
||||
assert_eq!(read_r8(dev, &queue, &texture), vec![value; 64 * 64], "GPU contents follow CPU writes");
|
||||
assert_eq!(address(key), Some(va), "the title address stays in the GPU view");
|
||||
}
|
||||
let (mut count, mut id) = (1usize, 0usize);
|
||||
assert_eq!(kernelx::AllocateTitlePhysicalPages(std::ptr::null_mut(), 0, &mut count, &mut id), 1);
|
||||
let mut mapped = PhysicalPage { id, at: va };
|
||||
assert_eq!(kernelx::MapTitlePhysicalPages(va as *mut c_void, 1, 0, 4, &mut mapped.id) as usize, va);
|
||||
assert!(kernelx::physical_page_at(va));
|
||||
let mut info: mem::MEMORY_BASIC_INFORMATION = std::mem::zeroed();
|
||||
assert_ne!(mem::VirtualQuery(va as *const c_void, &mut info, std::mem::size_of_val(&info)), 0);
|
||||
assert_eq!(info.State, mem::MEM_RESERVE, "exercise the lazy mapping before its first touch");
|
||||
assert_eq!(mirror_source(va), va, "a mapped lazy page overrides the CPU mirror");
|
||||
refresh(queue.as_raw());
|
||||
assert_eq!(read_r8(dev, &queue, &texture), vec![0; 64 * 64], "the untouched physical page supplies zeros");
|
||||
std::ptr::write_bytes(va as *mut u8, 0xC7, PAGE);
|
||||
refresh(queue.as_raw());
|
||||
assert_eq!(read_r8(dev, &queue, &texture), vec![0xC7; 64 * 64]);
|
||||
assert_eq!(kernelx::MapTitlePhysicalPages(va as *mut c_void, 1, 0, 4, std::ptr::null_mut()) as usize, va);
|
||||
assert!(!kernelx::physical_page_at(va));
|
||||
refresh(queue.as_raw());
|
||||
assert_eq!(read_r8(dev, &queue, &texture), vec![0x9A; 64 * 64], "unmapping restores the earlier CPU view");
|
||||
assert_eq!(address(key), Some(va));
|
||||
dev.GetDeviceRemovedReason().unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
/// A physical page mapped into a GPU view but not used yet (reserved until its first
|
||||
/// access, `kernelx::lazy`) is that address's own source, not the CPU view behind it.
|
||||
/// (Test by Kalmarv, review of PR #283.)
|
||||
|
||||
Reference in New Issue
Block a user