Skip to content
Closed
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
66 changes: 64 additions & 2 deletions crates/xcl/src/hle/d3d12x/device_x.rs
Original file line number Diff line number Diff line change
Expand Up @@ -654,10 +654,41 @@ 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,
}
}

/// 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<D3D12_CLEAR_VALUE> {
if clear.is_null() || !crate::gpu::shader::readable(clear as usize, std::mem::size_of::<D3D12_CLEAR_VALUE>()) {
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`.
Expand All @@ -680,6 +711,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;
}
Expand Down Expand Up @@ -767,7 +802,16 @@ 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;
// 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 {
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;
Expand Down Expand Up @@ -811,6 +855,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
}
Expand Down Expand Up @@ -916,6 +964,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};
Expand Down
118 changes: 116 additions & 2 deletions crates/xcl/src/hle/d3d12x/real.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -1785,8 +1798,36 @@ 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;
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)
}
_ => (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 {
Expand Down Expand Up @@ -1826,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);
Expand Down Expand Up @@ -1976,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),
Expand Down Expand Up @@ -3585,6 +3633,12 @@ unsafe fn fallback_stream(desc: *const u8) -> Option<(Vec<u8>, [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 };
}
Expand Down Expand Up @@ -3806,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() {
Expand All @@ -3817,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<T: Copy>(desc: *const T) -> Option<T> {
if desc.is_null() || !crate::gpu::shader::readable(desc as usize, std::mem::size_of::<T>()) {
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);
Expand Down Expand Up @@ -4530,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);
}
}
Loading