From b9d6a9831bb415904f991e1a461e42356966cbcb Mon Sep 17 00:00:00 2001 From: Robin Snyders Date: Mon, 23 Nov 2026 19:02:58 +0100 Subject: [PATCH 1/5] D3D12.X: Xbox D16S8 depth formats and depth buffers that are also UAVs The console's DXGI_FORMAT_D16_UNORM_S8_UINT family (118, 119, 120) does not exist on the PC: it becomes the D24_UNORM_S8_UINT family (45, 46, 47) in resource descs and in pipelines' DSV / RTV formats (pc_format). The console allows a depth-stencil resource that is also a UAV; the PC refuses that pair, so pc_resource_desc keeps the depth flag and drops unordered access, and a placed depth buffer's UNORDERED_ACCESS initial state becomes DEPTH_WRITE. GTA VI stopped on assertions at +0x344196 (its 2560x1440 D16S8 depth buffer got no size from GetResourceAllocationInfo) and +0x14a6390 (its 768x768x6 shadow maps, placed with the UAV state, were refused). Resources that still get no size or are refused are logged with their PC desc. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01XTYmP6vKUQKatSiA2PC95B --- crates/xcl/src/hle/d3d12x/device_x.rs | 49 +++++++++++++++++++++++++-- crates/xcl/src/hle/d3d12x/real.rs | 6 ++++ 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/device_x.rs b/crates/xcl/src/hle/d3d12x/device_x.rs index 403b7af29..e119cbf14 100644 --- a/crates/xcl/src/hle/d3d12x/device_x.rs +++ b/crates/xcl/src/hle/d3d12x/device_x.rs @@ -654,10 +654,28 @@ pub fn pc_resource_desc(x: &[u8; 0x38]) -> [u8; 0x38] { d[8..16].fill(0); // alignment: the driver's let layout = if dim == D3D12_RESOURCE_DIMENSION_BUFFER.0 as u32 { D3D12_TEXTURE_LAYOUT_ROW_MAJOR.0 } else { D3D12_TEXTURE_LAYOUT_UNKNOWN.0 }; d[0x2C..0x30].copy_from_slice(&(layout as u32).to_le_bytes()); - d[0x30..0x34].copy_from_slice(&(flags & 0xFF).to_le_bytes()); + // a depth buffer the console also writes as a UAV (GTA VI's 2560x1440 D16S8): the PC refuses + // depth + unordered access together, and the resource got no size + let pc_flags = if flags & 0x2 != 0 { flags & 0xFF & !0x4 } else { flags & 0xFF }; + d[0x30..0x34].copy_from_slice(&pc_flags.to_le_bytes()); + let format = u32::from_le_bytes(d[0x20..0x24].try_into().unwrap()); + d[0x20..0x24].copy_from_slice(&pc_format(format).to_le_bytes()); d } +/// A DXGI format of the console only as the PC's nearest: the 16-bit depth + 8-bit stencil +/// family (118 D16_UNORM_S8_UINT, 119 R16_UNORM_X8_TYPELESS, 120 X16_TYPELESS_G8_UINT) as the +/// 24-bit one (45, 46, 47). The PC driver refuses them: GTA VI's depth buffer got no size +/// (GetResourceAllocationInfo) and the title stopped on its assertion. +pub fn pc_format(f: u32) -> u32 { + match f { + 118 => 45, + 119 => 46, + 120 => 47, + f => f, + } +} + /// device #25 GetResourceAllocationInfo(visible mask, count, descs) → {size, alignment}, /// returned through a hidden pointer. Called the C way – (return buffer, device, …), XDefiant – /// or the C++ way – (device, return buffer, …): whichever argument is the device is `this`. @@ -680,6 +698,10 @@ pub(super) unsafe extern "system" fn get_resource_allocation_info_x(a: *mut c_vo } }; if s == u64::MAX || a == 0 { + static N: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0); + if N.fetch_add(1, std::sync::atomic::Ordering::Relaxed) < 8 { + crate::xlog_warn!("D3D12.X: GetResourceAllocationInfo: no size for {:x?} (as the PC's: {:x?})", &x[..0x34], &pc_resource_desc(&x)[..0x34]); + } size = u64::MAX; break; } @@ -767,7 +789,12 @@ pub(super) unsafe extern "system" fn create_placed_resource_x(this: *mut c_void, note_alias(va as usize, &x); // console-only state bits (Unicorn Overlord's 4K display target: 0x100000) are not the // PC's: the PC refused the resource (E_INVALIDARG) and the title threw - let state = state & real::PC_RESOURCE_STATES; + let mut state = state & real::PC_RESOURCE_STATES; + // a depth buffer is no UAV on the PC (`pc_resource_desc`): its unordered-access initial state + // (GTA VI's 768x768x6 shadow maps: 0x8) is refused – it starts as a depth target + if u32::from_le_bytes(d[0x30..0x34].try_into().unwrap()) & 0x2 != 0 && state & 0x8 != 0 { + state = (state & !0x8) | 0x10; + } if buffer { if let Some(hr) = real::place_at_title_address(this, va as usize, &d, state, clear, riid, ppv) { return hr; @@ -811,6 +838,10 @@ pub(super) unsafe extern "system" fn create_placed_resource_x(this: *mut c_void, w(0x20), w(0x30) ); + if hr < 0 { + let cf = if clear.is_null() { -1 } else { *(clear as *const i32) }; + crate::xlog_info!("D3D12.X: … its state {state:#x}, clear value format {cf}, as the PC's {:x?}", &d[..0x34]); + } } hr } @@ -916,6 +947,20 @@ mod tests { assert_eq!(u32::from_le_bytes(pc_resource_desc(&x)[0x2C..0x30].try_into().unwrap()), 0); } + #[test] + fn console_depth_buffers_become_pc_ones() { + let mut x = [0u8; 0x38]; + x[0] = 3; // texture 2D + x[0x20] = 118; // D16_UNORM_S8_UINT + x[0x30..0x34].copy_from_slice(&0x8030_0006u32.to_le_bytes()); // depth + UAV + console bits + let d = pc_resource_desc(&x); + let u = |o: usize| u32::from_le_bytes(d[o..o + 4].try_into().unwrap()); + assert_eq!((u(0x20), u(0x30)), (45, 0x2)); + x[0x30..0x34].copy_from_slice(&0x4u32.to_le_bytes()); // a UAV texture keeps it + assert_eq!(u32::from_le_bytes(pc_resource_desc(&x)[0x30..0x34].try_into().unwrap()), 0x4); + assert_eq!((pc_format(119), pc_format(120), pc_format(28)), (46, 47, 28)); + } + #[test] fn derived_pipeline_patches_land_on_the_desc_parts() { use std::mem::{offset_of, size_of}; diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index 3e7a44f19..30c2214e7 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -3585,6 +3585,12 @@ unsafe fn fallback_stream(desc: *const u8) -> Option<(Vec, [usize; 2])> { /// `list_x::RECT_LIST`) is TRIANGLE, as those draws become triangle lists; UNDEFINED (a stream /// without one) is TRIANGLE, or PATCH with a hull shader – the PC refuses both with a VS. fn pc_topology_type(g: &mut D3D12_GRAPHICS_PIPELINE_STATE_DESC) { + // the console's own depth format DXGI_FORMAT_D16_UNORM_S8_UINT (118) is not the PC's: the + // driver refuses the pipeline (GTA VI's); D24_UNORM_S8_UINT has the depth and the stencil + g.DSVFormat.0 = super::device_x::pc_format(g.DSVFormat.0 as u32) as i32; + for f in g.RTVFormats.iter_mut() { + f.0 = super::device_x::pc_format(f.0 as u32) as i32; + } if !(1..=4).contains(&g.PrimitiveTopologyType.0) { g.PrimitiveTopologyType = if g.HS.pShaderBytecode.is_null() { D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE } else { D3D12_PRIMITIVE_TOPOLOGY_TYPE_PATCH }; } From e0b44f913de5bd5e35628d848d01e177c358b523 Mon Sep 17 00:00:00 2001 From: Robin Snyders Date: Mon, 23 Nov 2026 19:39:01 +0100 Subject: [PATCH 2/5] D3D12.X: console mesh pipeline streams (MS as subobject 24), null stand-in shaders GTA VI's pipeline streams carry their mesh shader (DXIL program kind 13) as subobject type 24, the PC's amplification shader. translated_mesh_stream found no MS, did not translate them and the driver refused the stream ("CreateAmplificationShader: ms_6_6", then "neither VS, CS nor MS"). pc_stream_types renumbers AS / MS subobjects by the program kind in their bytecode and makes the stream's console formats (D16S8 DSV / RTV) and rect-list topology type the PC's, before any other path reads the stream. fallback_stream wrote an empty slice's dangling pointer (0x1) for the shaders it drops: the driver refused the mesh stand-ins (0x80070057) and the debug layer crashed reading 0x19. It now writes a null pointer. A stream refused even with stand-ins logs its subobject types and shader kinds. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01XTYmP6vKUQKatSiA2PC95B --- crates/xcl/src/hle/d3d12x/real.rs | 157 +++++++++++++++++++++++++++++- 1 file changed, 156 insertions(+), 1 deletion(-) diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index 30c2214e7..379a6579a 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -3545,6 +3545,106 @@ unsafe fn translated_gs_pso(this: *mut c_void, d: &D3D12_GRAPHICS_PIPELINE_STATE finish_two_pass(this, &out, tp, rs, "GS", riid, ppv) } +/// A copy of the stream with its amplification / mesh shader subobjects numbered as the PC's +/// (AS 24, MS 25) by the program kind in their bytecode, when the title's differ. GTA VI's +/// mesh pipelines carry their mesh shader (kind 13) as type 24: XCL found no MS, did not +/// translate them and the driver refused them ("CreateAmplificationShader: ms_6_6"). +unsafe fn pc_stream_types(desc: *const c_void) -> Option> { + if !crate::gpu::shader::readable(desc as usize, 16) { + return None; + } + let (size, base) = (*(desc as *const usize), *((desc as usize + 8) as *const usize)); + if !crate::gpu::shader::readable(base, size) { + return None; + } + let mut buf = std::slice::from_raw_parts(base as *const u8, size).to_vec(); + let mut changed = false; + let mut off = 0usize; + while off + 8 <= size { + let ty = u32::from_le_bytes(buf[off..off + 4].try_into().ok()?); + let Some((psize, palign)) = super::null::subobject_payload(ty) else { break }; + let poff = if palign == 8 { 8 } else { 4 }; + if ty == 24 || ty == 25 { + let want = buf.get(off + poff..off + poff + psize).and_then(|p| payload_shader(p)).and_then(program_kind).and_then(|k| match k { + 13 => Some(25u32), + 14 => Some(24u32), + _ => None, + }); + if let Some(w) = want.filter(|&w| w != ty) { + buf[off..off + 4].copy_from_slice(&w.to_le_bytes()); + changed = true; + } + } + // the console's formats (D16S8: 118) and rect-list topology type (19) as the PC's + let at = off + poff; + let words = match ty { + 15 => 8, // RENDER_TARGET_FORMATS: RTFormats[8], NumRenderTargets + 16 => 1, // DEPTH_STENCIL_FORMAT + _ => 0, + }; + for k in 0..words { + let o = at + 4 * k; + let f = u32::from_le_bytes(buf.get(o..o + 4)?.try_into().ok()?); + let pc = super::device_x::pc_format(f); + if pc != f { + buf[o..o + 4].copy_from_slice(&pc.to_le_bytes()); + changed = true; + } + } + if ty == 14 && buf.get(at..at + 4)? == 19u32.to_le_bytes() { + buf[at..at + 4].copy_from_slice(&3u32.to_le_bytes()); // TRIANGLE + changed = true; + } + off += (poff + psize + 7) & !7; + } + if changed { + static N: AtomicU64 = AtomicU64::new(0); + if N.fetch_add(1, Ordering::Relaxed) == 0 { + crate::xlog_info!("D3D12.X: pipeline streams with the console's numbering (mesh shader as subobject 24), formats or topology type – made the PC's"); + } + Some(buf) + } else { + None + } +} + +/// A refused stream's subobject types and, for its shaders, the program kind in their bytecode +/// (DXIL / DXBC version word: 0 PS, 1 VS, 2 GS, 3 HS, 4 DS, 5 CS, 13 MS, 14 AS). +unsafe fn log_stream_shapes(desc: *const c_void) { + static N: AtomicU64 = AtomicU64::new(0); + if N.fetch_add(1, Ordering::Relaxed) >= 6 || !crate::gpu::shader::readable(desc as usize, 16) { + return; + } + let (size, base) = (*(desc as *const usize), *((desc as usize + 8) as *const usize)); + if !crate::gpu::shader::readable(base, size) { + return; + } + let subs = known_subobjects(std::slice::from_raw_parts(base as *const u8, size)); + let shapes: Vec = subs + .iter() + .map(|(ty, p)| match [1, 2, 3, 4, 5, 6, 24, 25].contains(ty).then(|| payload_shader(p)).flatten() { + Some(code) => format!("{ty}:kind {}", program_kind(code).map_or("?".to_string(), |k| k.to_string())), + None => ty.to_string(), + }) + .collect(); + crate::xlog_warn!("D3D12.X: refused PSO stream ({size:#x} bytes): subobjects {shapes:?}"); +} + +/// The program kind of a DXIL / DXBC container (its first program part's version word >> 16). +fn program_kind(code: &[u8]) -> Option { + let u = |o: usize| code.get(o..o + 4).map(|b| u32::from_le_bytes(b.try_into().unwrap())); + if code.get(0..4)? != b"DXBC" { + return None; + } + for k in 0..u(28)? as usize { + let off = u(32 + 4 * k)? as usize; + if matches!(code.get(off..off + 4)?, b"DXIL" | b"SHDR" | b"SHEX") { + return Some(u(off + 8)? >> 16); + } + } + None +} + /// Rewrite a pipeline state stream with stand-in shaders. None if not possible. unsafe fn fallback_stream(desc: *const u8) -> Option<(Vec, [usize; 2])> { let size = *(desc as *const usize); @@ -3563,7 +3663,10 @@ unsafe fn fallback_stream(desc: *const u8) -> Option<(Vec, [usize; 2])> { let poff = if palign == 8 { 8 } else { 4 }; let at = off + poff; let put = |buf: &mut Vec, b: &[u8]| { - buf[at..at + 8].copy_from_slice(&(b.as_ptr() as u64).to_le_bytes()); + // no shader: a null pointer – an empty slice's is dangling (0x1), which the driver + // refused (GTA VI's mesh stand-ins, 0x80070057) and the debug layer read + let ptr = if b.is_empty() { 0 } else { b.as_ptr() as u64 }; + buf[at..at + 8].copy_from_slice(&ptr.to_le_bytes()); buf[at + 8..at + 16].copy_from_slice(&(b.len() as u64).to_le_bytes()); }; match ty { @@ -3734,6 +3837,10 @@ unsafe fn put(to: &mut T, from: &[u8]) { pub(super) unsafe extern "system" fn dev_create_pipeline_state(this: *mut c_void, desc: *const c_void, riid: *const GUID, ppv: *mut *mut c_void) -> HRESULT { super::null::capture_stream_desc(desc as *const u8); + // the console's stream numbers its mesh shader 24 (the PC's AS): as the PC's from here on + let pc_types = pc_stream_types(desc); + let pc_desc: [usize; 2] = pc_types.as_ref().map_or([0, 0], |b| [b.len(), b.as_ptr() as usize]); + let desc = if pc_types.is_some() { pc_desc.as_ptr() as *const c_void } else { desc }; let f: PsoFn = std::mem::transmute(orig_fn(this, slot_of(this, "CreatePipelineState"))); let (translated, missed) = if super::pso_async::enabled() { attempt(|| translated_mesh_stream(this, desc, riid, ppv)) } else { (translated_mesh_stream(this, desc, riid, ppv), false) }; if missed { @@ -3762,6 +3869,9 @@ pub(super) unsafe extern "system" fn dev_create_pipeline_state(this: *mut c_void let new_desc: [usize; 2] = [buf.len(), buf.as_ptr() as usize]; let hr2 = finish_pso(this, f, new_desc.as_ptr() as *const c_void, riid, ppv); note_fallback("PSO stream", hr, hr2 >= 0); + if hr2 < 0 { + log_stream_shapes(desc); + } if hr2 >= 0 { hr2 } else { @@ -4536,3 +4646,48 @@ mod queue_tests { QUEUE_OLDER.store(false, Ordering::Release); } } + +#[cfg(test)] +mod stream_tests { + use super::*; + + /// a DXBC container with one DXIL part of program kind `kind` + fn container(kind: u32) -> Vec { + let mut c = Vec::new(); + c.extend_from_slice(b"DXBC"); + c.extend_from_slice(&[0; 16]); // hash + c.extend_from_slice(&1u32.to_le_bytes()); // version + c.extend_from_slice(&64u32.to_le_bytes()); // size + c.extend_from_slice(&1u32.to_le_bytes()); // parts + c.extend_from_slice(&36u32.to_le_bytes()); // part 0 at 36 + c.extend_from_slice(b"DXIL"); + c.extend_from_slice(&20u32.to_le_bytes()); + c.extend_from_slice(&(kind << 16 | 0x66).to_le_bytes()); + c.resize(64, 0); + c + } + + #[test] + fn console_mesh_streams_become_pc_ones() { + let ms = container(13); + assert_eq!(program_kind(&ms), Some(13)); + assert_eq!(program_kind(b"not a shader"), None); + // MS as type 24, DSV format 118, topology type 19 + let mut out = StreamOut::default(); + out.shader(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_AS, &ms); + out.raw(16, &118u32.to_le_bytes(), false); + out.raw(14, &19u32.to_le_bytes(), false); + let desc: [usize; 2] = [out.0.len(), out.0.as_ptr() as usize]; + let buf = unsafe { pc_stream_types(desc.as_ptr() as *const c_void) }.expect("renumbered"); + let subs = stream_subobjects(&buf).unwrap(); + let types: Vec = subs.iter().map(|s| s.0).collect(); + assert_eq!(types, [25, 16, 14]); + assert_eq!(u32::from_le_bytes(subs[1].1.try_into().unwrap()), 45); + assert_eq!(u32::from_le_bytes(subs[2].1.try_into().unwrap()), 3); + // a PC stream is left alone + let mut pc = StreamOut::default(); + pc.shader(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_MS, &ms); + let desc: [usize; 2] = [pc.0.len(), pc.0.as_ptr() as usize]; + assert!(unsafe { pc_stream_types(desc.as_ptr() as *const c_void) }.is_none()); + } +} From 634c4238b3b4a640ad137674aa53db2f954d517b Mon Sep 17 00:00:00 2001 From: Robin Snyders Date: Mon, 23 Nov 2026 20:18:42 +0100 Subject: [PATCH 3/5] D3D12.X: a null UAV for a resource the PC has without unordered access MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit pc_resource_desc drops ALLOW_UNORDERED_ACCESS from a console depth buffer that is also a UAV. GTA VI still creates UAVs of its 768x768 shadow maps: the driver was handed an invalid view (debug layer: "CreateUnorderedAccessView … did not specify ALLOW_UNORDERED_ACCESS") and the device was removed once a dispatch used it. The PC descriptor is now a null view of the same dimension; the console descriptor XCL writes for translated shaders is unchanged. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01XTYmP6vKUQKatSiA2PC95B --- crates/xcl/src/hle/d3d12x/real.rs | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index 30c2214e7..66b2d9be4 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -1785,8 +1785,31 @@ unsafe extern "system" fn dev_create_uav_x(this: *mut c_void, res: *mut c_void, crate::xlog_info!("frame trace: UAV of render target {res:p} {:?} at {cpu:#x}: console slot {:?}, view {:?}", d.map(|d| (d.Width, d.Height, d.Format.0)), s.as_ref().map(|s| (s.index, s.pc_uav, s.at)), desc.as_ref().map(|v| (v.Format.0, v.ViewDimension.0))); } } - let Some(slot) = super::console_heap::slot(cpu) else { return f(this, res, counter, desc, cpu) }; - f(this, res, counter, desc, slot.pc_uav); + // a resource the PC has without unordered access (a console depth buffer that is also a UAV, + // `device_x::pc_resource_desc`): a null view of the same dimension – the view of it the + // driver was handed removed the device once a dispatch used it (GTA VI, ~25 s in) + let null_desc; + let (res_pc, counter, desc_pc) = match ID3D12Resource::from_raw_borrowed(&res).map(|r| r.GetDesc()) { + Some(rd) if rd.Flags.0 & D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS.0 == 0 => { + static N: AtomicU64 = AtomicU64::new(0); + if N.fetch_add(1, Ordering::Relaxed) < 4 { + crate::xlog_info!( + "D3D12.X: UAV of a resource without unordered access ({}x{}, format {}, flags {:#x}) – a null view", + rd.Width, + rd.Height, + rd.Format.0, + rd.Flags.0 + ); + } + let mut d = desc.as_ref().copied().unwrap_or(D3D12_UNORDERED_ACCESS_VIEW_DESC { ViewDimension: D3D12_UAV_DIMENSION(resource_dim_to_view(&rd) as i32), ..Default::default() }); + d.Format = windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT_R32_UINT; + null_desc = d; + (std::ptr::null_mut(), std::ptr::null_mut(), &null_desc as *const D3D12_UNORDERED_ACCESS_VIEW_DESC) + } + _ => (res, counter, desc), + }; + let Some(slot) = super::console_heap::slot(cpu) else { return f(this, res_pc, counter, desc_pc, cpu) }; + f(this, res_pc, counter, desc_pc, slot.pc_uav); super::console_heap::publish(&slot, true); super::console_heap::note_resource(&slot, res as usize); let Some(r) = ID3D12Resource::from_raw_borrowed(&res) else { From 595b2b7a8446d9bf912ae602547476adcb21ef50 Mon Sep 17 00:00:00 2001 From: Robin Snyders Date: Mon, 23 Nov 2026 20:59:14 +0100 Subject: [PATCH 4/5] D3D12.X: the PC's formats for clear values and views of console depth buffers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #163 makes the console's D16S8 depth buffers D24S8 on the PC; what refers to them kept the console's formats and the PC refused them (GTA VI, with the D3D12 debug layer): - a clear value of format 118 on a D24S8 resource: E_INVALIDARG, then the title's assertion; - CreateDepthStencilView with format 118 (now hooked): the device was removed (~55 s in); - SRVs of depth / stencil planes as R16_UNORM, R8_UINT, and of a D32S8 (R32G8X24_TYPELESS) as R32_FLOAT – also the SRV XCL makes next to a texture UAV: now R24_UNORM_X8_TYPELESS / X24_TYPELESS_G8_UINT / R32_FLOAT_X8X24_TYPELESS / X32_TYPELESS_G8X24_UINT with the matching plane slice (`depth_srv_format`); - the null UAV of a depth buffer kept the console view's plane slice 1. RTVs and SRVs with 118–120 go through `pc_format` too. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01SKtJseucqXR6iuAE6HBtiA --- crates/xcl/src/hle/d3d12x/device_x.rs | 17 ++++++ crates/xcl/src/hle/d3d12x/real.rs | 85 +++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) diff --git a/crates/xcl/src/hle/d3d12x/device_x.rs b/crates/xcl/src/hle/d3d12x/device_x.rs index e119cbf14..5abc46051 100644 --- a/crates/xcl/src/hle/d3d12x/device_x.rs +++ b/crates/xcl/src/hle/d3d12x/device_x.rs @@ -676,6 +676,19 @@ pub fn pc_format(f: u32) -> u32 { } } +/// A copy of the clear value with its format as the PC's (`pc_format`), when that differs. +unsafe fn pc_clear_value(clear: *const c_void) -> Option { + if clear.is_null() || !crate::gpu::shader::readable(clear as usize, std::mem::size_of::()) { + return None; + } + let mut c = (clear as *const D3D12_CLEAR_VALUE).read_unaligned(); + let f = pc_format(c.Format.0 as u32); + (f != c.Format.0 as u32).then(|| { + c.Format.0 = f as i32; + c + }) +} + /// device #25 GetResourceAllocationInfo(visible mask, count, descs) → {size, alignment}, /// returned through a hidden pointer. Called the C way – (return buffer, device, …), XDefiant – /// or the C++ way – (device, return buffer, …): whichever argument is the device is `this`. @@ -790,6 +803,10 @@ pub(super) unsafe extern "system" fn create_placed_resource_x(this: *mut c_void, // console-only state bits (Unicorn Overlord's 4K display target: 0x100000) are not the // PC's: the PC refused the resource (E_INVALIDARG) and the title threw let mut state = state & real::PC_RESOURCE_STATES; + // its clear value in the PC's format too (`pc_format`): D16S8 (118) against the resource's + // D24S8 was refused (GTA VI's 1024x1024 depth buffers, E_INVALIDARG, then its assertion) + let clear_pc = pc_clear_value(clear); + let clear = clear_pc.as_ref().map_or(clear, |c| c as *const D3D12_CLEAR_VALUE as *const c_void); // a depth buffer is no UAV on the PC (`pc_resource_desc`): its unordered-access initial state // (GTA VI's 768x768x6 shadow maps: 0x8) is refused – it starts as a depth target if u32::from_le_bytes(d[0x30..0x34].try_into().unwrap()) & 0x2 != 0 && state & 0x8 != 0 { diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index 66b2d9be4..45120f541 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -1627,6 +1627,19 @@ unsafe extern "system" fn heap_release(this: *mut c_void) -> u32 { } unsafe extern "system" fn dev_create_srv_x(this: *mut c_void, res: *mut c_void, desc: *const D3D12_SHADER_RESOURCE_VIEW_DESC, cpu: u64) { + let mut pc_view = pc_view_desc(desc); + if let (Some(rd), Some(mut v)) = (ID3D12Resource::from_raw_borrowed(&res).map(|r| r.GetDesc()), pc_view.or_else(|| desc.as_ref().copied())) { + if let Some((format, plane)) = depth_srv_format(rd.Format.0 as u32, v.Format.0 as u32) { + v.Format.0 = format as i32; + match v.ViewDimension { + D3D12_SRV_DIMENSION_TEXTURE2D => v.Anonymous.Texture2D.PlaneSlice = plane, + D3D12_SRV_DIMENSION_TEXTURE2DARRAY => v.Anonymous.Texture2DArray.PlaneSlice = plane, + _ => {} + } + pc_view = Some(v); + } + } + let desc = pc_view.as_ref().map_or(desc, |v| v as *const D3D12_SHADER_RESOURCE_VIEW_DESC); let f: unsafe extern "system" fn(*mut c_void, *mut c_void, *const D3D12_SHADER_RESOURCE_VIEW_DESC, u64) = std::mem::transmute(orig_fn(this, 18)); let Some(slot) = super::console_heap::slot(cpu) else { return f(this, res, desc, cpu) }; // Raytracing acceleration structure SRV (pResource == NULL, Location == console GPU VA): @@ -1803,6 +1816,11 @@ unsafe extern "system" fn dev_create_uav_x(this: *mut c_void, res: *mut c_void, } let mut d = desc.as_ref().copied().unwrap_or(D3D12_UNORDERED_ACCESS_VIEW_DESC { ViewDimension: D3D12_UAV_DIMENSION(resource_dim_to_view(&rd) as i32), ..Default::default() }); d.Format = windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT_R32_UINT; + match d.ViewDimension { + D3D12_UAV_DIMENSION_TEXTURE2D => d.Anonymous.Texture2D.PlaneSlice = 0, + D3D12_UAV_DIMENSION_TEXTURE2DARRAY => d.Anonymous.Texture2DArray.PlaneSlice = 0, + _ => {} + } null_desc = d; (std::ptr::null_mut(), std::ptr::null_mut(), &null_desc as *const D3D12_UNORDERED_ACCESS_VIEW_DESC) } @@ -1849,6 +1867,12 @@ unsafe extern "system" fn dev_create_uav_x(this: *mut c_void, res: *mut c_void, Shader4ComponentMapping: D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING, Anonymous: D3D12_SHADER_RESOURCE_VIEW_DESC_0 { Texture2D: D3D12_TEX2D_SRV { MostDetailedMip: mip, MipLevels: 1, PlaneSlice: 0, ResourceMinLODClamp: 0.0 } }, }; + // (of a depth buffer: the PC format of its depth or stencil plane, `depth_srv_format`) + let mut srv = srv; + if let Some((format, plane)) = depth_srv_format(rd.Format.0 as u32, srv.Format.0 as u32) { + srv.Format.0 = format as i32; + srv.Anonymous.Texture2D.PlaneSlice = plane; + } let g: unsafe extern "system" fn(*mut c_void, *mut c_void, *const D3D12_SHADER_RESOURCE_VIEW_DESC, u64) = std::mem::transmute(orig_fn(this, 18)); g(this, res, &srv, slot.pc_srv); super::console_heap::publish(&slot, false); @@ -1999,6 +2023,7 @@ fn overrides(kind: Kind) -> Vec<(&'static str, usize)> { ("CreateDescriptorHeap", dev_create_descriptor_heap as usize), ("CreateRootSignature", dev_create_root_signature as usize), ("CreateRenderTargetView", dev_create_rtv as usize), + ("CreateDepthStencilView", dev_create_dsv as usize), ("CreateCommittedResource", dev_create_committed_resource as usize), ("CreateHeap", dev_create_heap as usize), ("CreatePlacedResource", dev_create_placed_resource as usize), @@ -3835,6 +3860,8 @@ unsafe extern "system" fn dev_create_root_signature(this: *mut c_void, mask: u32 unsafe extern "system" fn dev_create_rtv(this: *mut c_void, res: *mut c_void, desc: *const c_void, handle: usize) { type F = unsafe extern "system" fn(*mut c_void, *mut c_void, *const c_void, usize); let f: F = std::mem::transmute(orig_fn(this, slot_of(this, "CreateRenderTargetView"))); + let pc_view = pc_view_desc(desc as *const D3D12_RENDER_TARGET_VIEW_DESC); + let desc = pc_view.as_ref().map_or(desc, |v| v as *const D3D12_RENDER_TARGET_VIEW_DESC as *const c_void); f(this, res, desc, handle); super::present::note_rtv(handle, res); if super::present::trace_frame() { @@ -3846,6 +3873,45 @@ unsafe extern "system" fn dev_create_rtv(this: *mut c_void, res: *mut c_void, de } } +/// CreateDepthStencilView with the PC's format (`device_x::pc_format`): GTA VI's D16S8 views +/// (118) of its depth buffers, which are D24S8 on the PC, were invalid and the device was +/// removed (~55 s in). +unsafe extern "system" fn dev_create_dsv(this: *mut c_void, res: *mut c_void, desc: *const D3D12_DEPTH_STENCIL_VIEW_DESC, handle: usize) { + type F = unsafe extern "system" fn(*mut c_void, *mut c_void, *const D3D12_DEPTH_STENCIL_VIEW_DESC, usize); + let f: F = std::mem::transmute(orig_fn(this, slot_of(this, "CreateDepthStencilView"))); + let pc_view = pc_view_desc(desc); + f(this, res, pc_view.as_ref().map_or(desc, |v| v as *const _), handle) +} + +/// The SRV format (and plane) the PC takes for a view of a depth + stencil resource: the console +/// reads the depth of a D32S8 (R32G8X24) as R32_FLOAT, of a D24S8 (D16S8 on the console) as +/// R16_UNORM, its stencil as R8_UINT – each invalid on the PC (GTA VI: the device was removed). +fn depth_srv_format(resource: u32, view: u32) -> Option<(u32, u32)> { + let stencil = matches!(view, 62 | 22 | 47 | 120); + let r = match (resource, stencil) { + (19 | 20, false) => (21, 0), + (19 | 20, true) => (22, 1), + (44 | 45, false) => (46, 0), + (44 | 45, true) => (47, 1), + _ => return None, + }; + (r != (view, if stencil { 1 } else { 0 }) || r.0 != view).then_some(r) +} + +/// A copy of a view desc (its DXGI_FORMAT first) with the PC's format, when that differs. +unsafe fn pc_view_desc(desc: *const T) -> Option { + if desc.is_null() || !crate::gpu::shader::readable(desc as usize, std::mem::size_of::()) { + return None; + } + let mut v = desc.read_unaligned(); + let at = &mut v as *mut T as *mut u32; + let f = super::device_x::pc_format(*at); + (f != *at).then(|| { + *at = f; + v + }) +} + /// ClearRenderTargetView: as on the PC (`XCL_TRACE_FRAME`: the colours logged) unsafe extern "system" fn cl_clear_rtv(this: *mut c_void, rtv: usize, color: *const [f32; 4], n: u32, rects: *const c_void) { type F = unsafe extern "system" fn(*mut c_void, usize, *const [f32; 4], u32, *const c_void); @@ -4559,3 +4625,22 @@ mod queue_tests { QUEUE_OLDER.store(false, Ordering::Release); } } + +#[cfg(test)] +mod depth_view_tests { + use super::depth_srv_format; + + #[test] + fn console_depth_views_become_pc_ones() { + // D32S8 (R32G8X24_TYPELESS): depth as R32_FLOAT_X8X24_TYPELESS, stencil plane 1 + assert_eq!(depth_srv_format(19, 41), Some((21, 0))); + assert_eq!(depth_srv_format(20, 62), Some((22, 1))); + // D24S8 (the console's D16S8): depth R24_UNORM_X8_TYPELESS, stencil X24_TYPELESS_G8_UINT + assert_eq!(depth_srv_format(45, 56), Some((46, 0))); + assert_eq!(depth_srv_format(45, 62), Some((47, 1))); + assert_eq!(depth_srv_format(45, 119), Some((46, 0))); + // already the PC's, or not a depth resource: unchanged + assert_eq!(depth_srv_format(45, 46), None); + assert_eq!(depth_srv_format(28, 28), None); + } +} From aeadb23f0a4c29c0297392266e3de1cd9453251b Mon Sep 17 00:00:00 2001 From: Robin Snyders Date: Mon, 23 Nov 2026 21:00:26 +0100 Subject: [PATCH 5/5] Fix the merge: close stream_tests before depth_view_tests Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01SKtJseucqXR6iuAE6HBtiA --- crates/xcl/src/hle/d3d12x/real.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index 5dcfdf666..1a4a631d4 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -4778,7 +4778,10 @@ mod stream_tests { pc.shader(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_MS, &ms); let desc: [usize; 2] = [pc.0.len(), pc.0.as_ptr() as usize]; assert!(unsafe { pc_stream_types(desc.as_ptr() as *const c_void) }.is_none()); + } +} +#[cfg(test)] mod depth_view_tests { use super::depth_srv_format;