From cbc1a4f53f4aa61e3f0766c43bfcb7e1d9c4ccdc Mon Sep 17 00:00:00 2001 From: b3IKK <278540256+b3IKK@users.noreply.github.com> Date: Tue, 24 Nov 2026 20:42:10 +0000 Subject: [PATCH] D3D12.X: a resolve marks its destination and names the PC's format ResolveSubresource / ResolveSubresourceRegion forwarded the console's format code as it was - its 116-120 are the console depth family's, and the PC reads those codes as video formats - so a resolve into such a view's target was refused (or resolved the wrong depth format). Neither marked the destination with title_textures::note_gpu_write, which CopyResource and the copies already do: a kept-in-step texture the GPU had resolved into was uploaded over by the next CPU write scan, losing the resolved image. Translate the format with device_x::pc_target_format (the target's view format) and mark the destination as written by the GPU. Assisted-by: OpenCode (Gemini 4 Neon) --- crates/xcl/src/hle/d3d12x/list_x.rs | 52 +++++++++++++++++++++ crates/xcl/src/hle/d3d12x/title_textures.rs | 6 +++ 2 files changed, 58 insertions(+) diff --git a/crates/xcl/src/hle/d3d12x/list_x.rs b/crates/xcl/src/hle/d3d12x/list_x.rs index ef98c131b7..80225d5511 100644 --- a/crates/xcl/src/hle/d3d12x/list_x.rs +++ b/crates/xcl/src/hle/d3d12x/list_x.rs @@ -4229,18 +4229,29 @@ unsafe fn trace_resolve(what: &str, dst: usize, src: usize, format: i32, extra: /// command list #19 ResolveSubresource(dst, dst sub, src, src sub, format) (PC #19) pub(super) unsafe extern "system" fn resolve_subresource_x(this: *mut c_void, dst: usize, dst_sub: u32, src: usize, src_sub: u32, format: i32) { super::present::note_use(dst, super::present::USE_COPY); + // the GPU wrote the destination: a CPU upload of a kept-in-step texture must not follow it + super::title_textures::note_gpu_write(dst); trace_resolve("ResolveSubresource", dst, src, format, ""); + let format = resolve_format(format); pc::(this, PC_RESOLVE_SUBRESOURCE)(this, dst, dst_sub, src, src_sub, format) } +/// The PC's form of a resolve's format, which is the target's *view* format and so may be one +/// of the console's (its 116–120 are the depth family's). +fn resolve_format(format: i32) -> i32 { + super::device_x::pc_target_format(format as u32) as i32 +} + /// command list #108 ResolveSubresourceRegion(dst, dst sub, x, y, src, src sub, rect, format, /// mode) (PC #64) #[allow(clippy::too_many_arguments)] pub(super) unsafe extern "system" fn resolve_subresource_region_x(this: *mut c_void, dst: usize, dst_sub: u32, x: u32, y: u32, src: usize, src_sub: u32, rect: *const windows::Win32::Foundation::RECT, format: i32, mode: i32) { super::present::note_use(dst, super::present::USE_COPY); + super::title_textures::note_gpu_write(dst); if super::present::trace_frame() { trace_resolve("ResolveSubresourceRegion", dst, src, format, &format!(" mode {mode} at ({x}, {y}) rect {:?}", rect.as_ref().map(|r| (r.left, r.top, r.right, r.bottom)))); } + let format = resolve_format(format); pc::(this, PC_RESOLVE_SUBRESOURCE_REGION)(this, dst, dst_sub, x, y, src, src_sub, rect, format, mode) } @@ -5069,6 +5080,47 @@ mod tests { }); } + /// A resolve writes its destination with the GPU: it is marked so (a later CPU upload must + /// not overwrite it), and the format it names is the target's console view format, the + /// PC's form of which the driver gets. + #[test] + fn resolve_subresource_marks_the_destination_and_translates_the_format() { + #[repr(C)] + struct FakeCom { + vt: *const usize, + } + thread_local! { + static FORMATS: std::cell::RefCell> = const { std::cell::RefCell::new(Vec::new()) }; + } + unsafe extern "system" fn fake_resolve(_t: *mut c_void, _dst: usize, _ds: u32, _src: usize, _ss: u32, format: i32) { + FORMATS.with(|f| f.borrow_mut().push(format)); + } + unsafe extern "system" fn fake_resolve_region(_t: *mut c_void, _dst: usize, _ds: u32, _x: u32, _y: u32, _src: usize, _ss: u32, _r: *const windows::Win32::Foundation::RECT, format: i32, _m: i32) { + FORMATS.with(|f| f.borrow_mut().push(format)); + } + let mut orig = [0usize; 70]; + orig[PC_RESOLVE_SUBRESOURCE] = fake_resolve as usize; + orig[PC_RESOLVE_SUBRESOURCE_REGION] = fake_resolve_region as usize; + // (adopted objects keep their original vtable at ext[-1]) + let ext = [orig.as_ptr() as usize, 0usize]; + let mut list = FakeCom { vt: unsafe { ext.as_ptr().add(1) } }; + let list = &mut list as *mut FakeCom as *mut c_void; + let before = crate::hle::d3d12x::title_textures::NOTE_GPU_WRITE_CALLS.load(Ordering::Relaxed); + unsafe { + resolve_subresource_x(list, 0x1111, 0, 0x2222, 0, 116); + resolve_subresource_region_x(list, 0x1111, 0, 0, 0, 0x2222, 0, std::ptr::null(), 116, 0); + } + FORMATS.with(|f| assert_eq!(*f.borrow(), vec![10, 10], "the console's D16S8 family is the PC's D24S8 on a resolve")); + assert_eq!( + crate::hle::d3d12x::title_textures::NOTE_GPU_WRITE_CALLS.load(Ordering::Relaxed), + before + 2, + "both resolves mark the destination GPU-written" + ); + // a PC format is passed as it is + unsafe { resolve_subresource_x(list, 0x1111, 0, 0x2222, 0, 28) }; + FORMATS.with(|f| assert_eq!(*f.borrow(), vec![10, 10, 28])); + } + /// SetDescriptorHeaps(0, NULL) unbinds every heap: the list must not still count the /// shared heap as bound, or a translated draw after it reads the memory windows /// (`ResourceDescriptorHeap[0..3]`) from no heap at all. diff --git a/crates/xcl/src/hle/d3d12x/title_textures.rs b/crates/xcl/src/hle/d3d12x/title_textures.rs index 683b055938..b4c6c07ceb 100644 --- a/crates/xcl/src/hle/d3d12x/title_textures.rs +++ b/crates/xcl/src/hle/d3d12x/title_textures.rs @@ -464,6 +464,8 @@ fn recheck_source(e: &mut Entry) -> bool { /// by the GPU). The first upload, if still pending, stays: it runs before the title's submit /// that copies. pub fn note_gpu_write(res: usize) { + #[cfg(test)] + NOTE_GPU_WRITE_CALLS.fetch_add(1, Ordering::Relaxed); let mut marked = false; { let mut t = TEXTURES.lock().unwrap(); @@ -482,6 +484,10 @@ pub fn note_gpu_write(res: usize) { static GPU_WRITTEN: AtomicU64 = AtomicU64::new(0); +/// `note_gpu_write` calls (a list's hooks that must mark a write count them: a resolve did). +#[cfg(test)] +pub(super) static NOTE_GPU_WRITE_CALLS: AtomicU64 = AtomicU64::new(0); + /// The textures kept in step whose memory `[va, va + len)` overlaps (PC texture, its address). pub fn overlapping(va: usize, len: usize) -> Vec<(usize, usize)> { TEXTURES.lock().unwrap().overlapping(va, len)