Skip to content
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
Loading
Loading