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
64 changes: 64 additions & 0 deletions crates/xcl/src/hle/d3d12x/bind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,73 @@ pub(super) unsafe extern "system" fn execute_indirect(this: *mut c_void, sig: us
}
}

/// A DrawInstanced of the console's rect list (three corners of each rectangle, the fourth
/// implied) as two triangles a rectangle; false: not a rect list, or one this cannot expand
/// (the caller draws the three vertices as a triangle – half of each rectangle).
///
/// The PC has no rect lists. The fourth corner is made here, on the CPU, from the title's
/// vertex data: v3 = v0 + v2 − v1 (seen in a retail title's full-screen passes: (−1,1) (−1,−1)
/// (1,−1) with texture coordinates (0,0) (0,1) (1,1) – the missing corner is (1,1), (1,0)).
/// The vertex layout is not looked at: each 4-byte word that is the same in all three vertices
/// is kept (a packed colour), any other is taken as a float. Vertices a shader makes from the
/// vertex index alone (no vertex buffer) are not expanded.
unsafe fn draw_rects(this: *mut c_void, list: &ID3D12GraphicsCommandList, vertices: u32, instances: u32, first_vertex: u32, first_instance: u32) -> bool {
let Some(vbs) = super::list_x::rect_list_state(this) else { return false };
let rects = (vertices / 3) as usize;
if vbs.is_empty() || rects == 0 || rects > 4096 {
return false;
}
let mut views = Vec::new();
for &(slot, (va, bytes, stride), _) in &vbs {
let (s, at) = (stride as usize, va as usize + first_vertex as usize * stride as usize);
let len = rects * 3 * s;
if va == 0 || s == 0 || s % 4 != 0 || first_vertex as usize * s + len > bytes as usize || !crate::gpu::shader::readable(at, len) {
return false;
}
let p = allocate_upload_memory(this, rects * 6 * s, 16);
if p.is_null() {
return false;
}
let word = |v: usize, w: usize| (at as *const u8).add(v * s + w * 4).cast::<u32>().read_unaligned();
for r in 0..rects {
let out = p.add(r * 6 * s);
let src = (at as *const u8).add(r * 3 * s);
// v0 v1 v2, then v0 v2 v3
std::ptr::copy_nonoverlapping(src, out, 3 * s);
std::ptr::copy_nonoverlapping(src, out.add(3 * s), s);
std::ptr::copy_nonoverlapping(src.add(2 * s), out.add(4 * s), s);
for w in 0..s / 4 {
let (a, b, c) = (word(r * 3, w), word(r * 3 + 1, w), word(r * 3 + 2, w));
let d = if a == b && b == c { a } else { (f32::from_bits(a) + f32::from_bits(c) - f32::from_bits(b)).to_bits() };
out.add(5 * s + w * 4).cast::<u32>().write_unaligned(d);
}
}
views.push((slot, D3D12_VERTEX_BUFFER_VIEW { BufferLocation: super::list_x::pc_va_of(p as u64), SizeInBytes: (rects * 6 * s) as u32, StrideInBytes: stride }));
}
for (slot, v) in &views {
list.IASetVertexBuffers(*slot, Some(&[*v]));
}
list.DrawInstanced(rects as u32 * 6, instances, 0, first_instance);
for &(slot, _, pc) in &vbs {
if let Some(v) = pc {
list.IASetVertexBuffers(slot, Some(&[v]));
}
}
static N: AtomicU64 = AtomicU64::new(0);
if N.fetch_add(1, Ordering::Relaxed) == 0 {
crate::xlog_info!("D3D12.X: rect lists are drawn as two triangles a rectangle, the fourth corner made on the CPU");
}
true
}

/// #12 DrawInstanced
pub(super) unsafe extern "system" fn draw_instanced(this: *mut c_void, vertices: u32, instances: u32, first_vertex: u32, first_instance: u32) {
DRAW_ARGS.with(|d| d.set((false, vertices, instances, first_vertex, 0)));
if let Some(list) = graphics_bound(this) {
if draw_rects(this, &list, vertices, instances, first_vertex, first_instance) {
return;
}
super::list_x::sync_title_buffers(this, None, first_vertex, vertices, if first_instance != 0 { instances.max(2) } else { instances });
list.DrawInstanced(vertices, instances, first_vertex, first_instance);
}
}
Expand All @@ -253,6 +316,7 @@ thread_local! {
/// #13 DrawIndexedInstanced
pub(super) unsafe extern "system" fn draw_indexed_instanced(this: *mut c_void, indices: u32, instances: u32, first_index: u32, base_vertex: i32, first_instance: u32) {
if let Some(list) = prepare_indexed_draw(this, indices, instances, first_index, base_vertex) {
super::list_x::sync_title_buffers(this, Some((first_index, indices, base_vertex)), 0, 0, if first_instance != 0 { instances.max(2) } else { instances });
emit_prepared_indexed_draw(&list, indices, instances, first_index, base_vertex, first_instance);
}
}
Expand Down
78 changes: 76 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,37 @@ 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.
///
/// 86 (B5G5R5A1_UNORM) is a PC format, but not one a PC driver takes for a render target that
/// is also a UAV (a retail title's 2560x1440 target, flags 0x5: CreatePlacedResource refused it,
/// E_INVALIDARG, and the title stopped). It becomes R8G8B8A8_UNORM (28): what shaders write to
/// it and read from it is the same, with more bits; its memory is twice the console's.
pub fn pc_format(f: u32) -> u32 {
match f {
// B5G5R5A1 (86) and B4G4R4A4 (115) render targets (the same title's 2560x1440 ones)
86 | 115 => 28,
// the console's R10G10B10_7E3_A2_FLOAT / _6E4_A2_FLOAT: half floats hold them
116 | 117 => 10,
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`.
Expand All @@ -680,6 +707,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 @@ -752,6 +783,22 @@ fn note_alias(va: usize, x: &[u8; 0x38]) {
}
}

/// A D3D12_CLEAR_VALUE (format, then 16 bytes of colour or depth + stencil) with a console-only
/// format as the PC's (`pc_format`); None: nothing to change (or no clear value).
pub(super) unsafe fn pc_clear_value(clear: *const c_void) -> Option<[u8; 20]> {
if clear.is_null() || !crate::gpu::shader::readable(clear as usize, 20) {
return None;
}
let mut c = (clear as *const [u8; 20]).read_unaligned();
let f = u32::from_le_bytes(c[0..4].try_into().unwrap());
let pc = pc_format(f);
if pc == f {
return None;
}
c[0..4].copy_from_slice(&pc.to_le_bytes());
Some(c)
}

/// device #44 CreatePlacedResourceX(address, desc, initial state, clear value, riid, resource):
/// a resource in title memory. A buffer at a 64 KiB-aligned address in unified memory is that
/// memory (placed in its UMA heap); a texture gets a PC texture kept in step with the memory
Expand All @@ -764,10 +811,19 @@ pub(super) unsafe extern "system" fn create_placed_resource_x(this: *mut c_void,
let x: [u8; 0x38] = (desc as *const [u8; 0x38]).read_unaligned();
let d = pc_resource_desc(&x);
let buffer = u32::from_le_bytes(d[0..4].try_into().unwrap()) == D3D12_RESOURCE_DIMENSION_BUFFER.0 as u32;
// the clear value names the console's format too (a 1024x1024 D16S8 depth buffer: 118 with
// the desc already 45 – the PC refused the pair, E_INVALIDARG, and the title stopped)
let pc_clear = pc_clear_value(clear);
let clear = pc_clear.as_ref().map_or(clear, |c| c.as_ptr() as *const 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;
Expand Down Expand Up @@ -811,6 +867,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 +976,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