Skip to content
Open
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
55 changes: 53 additions & 2 deletions crates/xcl/src/hle/d3d12x/device_x.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

use std::collections::HashMap;
use std::ffi::{c_char, c_void, CStr, CString};
use std::sync::atomic::{AtomicI8, AtomicU64, Ordering};
use std::sync::atomic::{AtomicBool, AtomicI8, AtomicU64, Ordering};
use std::sync::{Arc, Mutex};

use windows::core::Interface;
Expand Down Expand Up @@ -147,6 +147,27 @@ pub(super) unsafe extern "system" fn create_graphics_pso_pc(this: *mut c_void, d
create_graphics_pso_tracked(this, desc, riid, ppv)
}

/// Whether R9G9B9E5_SHAREDEXP render targets and UAVs are made half floats (`pc_resource_desc`,
/// `pc_target_format`): some PC drivers take the format neither as a target nor through unordered
/// access (NVIDIA: E_INVALIDARG), others take both (an AMD RDNA4 card). Half floats until the
/// device is asked (`probe_rgb9e5_targets`).
static RGB9E5_AS_HALF: AtomicBool = AtomicBool::new(true);

/// `CheckFeatureSupport(D3D12_FEATURE_FORMAT_SUPPORT)` for R9G9B9E5_SHAREDEXP: its targets keep
/// the format when the device renders into it and stores to it through typed UAVs.
pub(super) fn probe_rgb9e5_targets(dev: &ID3D12Device) {
let mut s = D3D12_FEATURE_DATA_FORMAT_SUPPORT { Format: windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT(67), ..Default::default() };
let native = unsafe { dev.CheckFeatureSupport(D3D12_FEATURE_FORMAT_SUPPORT, &mut s as *mut _ as *mut c_void, std::mem::size_of_val(&s) as u32) }.is_ok()
&& s.Support1.0 & D3D12_FORMAT_SUPPORT1_RENDER_TARGET.0 != 0
&& s.Support1.0 & D3D12_FORMAT_SUPPORT1_TYPED_UNORDERED_ACCESS_VIEW.0 != 0
&& s.Support2.0 & D3D12_FORMAT_SUPPORT2_UAV_TYPED_STORE.0 != 0;
RGB9E5_AS_HALF.store(!native, Ordering::Relaxed);
crate::xlog_info!(
"D3D12.X: R9G9B9E5 render targets / UAVs: {}",
if native { "kept (the device renders into the format and stores to it)" } else { "half floats (the device does not render into the format or store to it)" }
);
}

/// Cached result of `CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS16).DynamicDepthBiasSupported`:
/// -1 = not yet probed, 0 = unsupported, 1 = supported.
static DYN_DEPTH_BIAS: AtomicI8 = AtomicI8::new(-1);
Expand Down Expand Up @@ -648,6 +669,11 @@ pub(super) unsafe extern "system" fn create_sampler_x(this: *mut c_void, x: *con
/// the console resource desc (0x38 bytes, D3D12_RESOURCE_DESC) as a PC one: its tile layouts
/// and console flag bits mean nothing to a PC driver
pub fn pc_resource_desc(x: &[u8; 0x38]) -> [u8; 0x38] {
resource_desc(x, RGB9E5_AS_HALF.load(Ordering::Relaxed))
}

/// `pc_resource_desc`, R9G9B9E5 targets made half floats or not
fn resource_desc(x: &[u8; 0x38], rgb9e5_as_half: bool) -> [u8; 0x38] {
let mut d = *x;
let dim = u32::from_le_bytes(d[0..4].try_into().unwrap());
let mut flags = u32::from_le_bytes(d[0x30..0x34].try_into().unwrap());
Expand All @@ -674,6 +700,13 @@ pub fn pc_resource_desc(x: &[u8; 0x38]) -> [u8; 0x38] {
if matches!(format, 86 | 115) && flags & 5 != 0 {
d[0x20..0x24].copy_from_slice(&28u32.to_le_bytes());
}
// R9G9B9E5_SHAREDEXP targets: some PC drivers render into the format neither as a target nor
// through unordered access (a retail game's 1920x1080 one on the Series S profile, flags
// 0x300005: E_INVALIDARG on NVIDIA, then its assertion) – half floats hold every
// shared-exponent value exactly (`RGB9E5_AS_HALF`)
if format == 67 && flags & 5 != 0 && rgb9e5_as_half {
d[0x20..0x24].copy_from_slice(&10u32.to_le_bytes());
}
// a depth buffer the console also lets shaders write (a retail game's: ALLOW_DEPTH_STENCIL |
// ALLOW_UNORDERED_ACCESS): the PC refuses the pair – the depth buffer stays
if flags & 2 != 0 {
Expand Down Expand Up @@ -725,8 +758,14 @@ fn depth_typeless(f: u32) -> Option<u32> {
/// The format of a render target / depth view or pipeline on the PC: `pc_view_format`, and a
/// B5G5R5A1 / B4G4R4A4 target is R8G8B8A8 (`pc_resource_desc`).
pub fn pc_target_format(f: u32) -> u32 {
target_format(f, RGB9E5_AS_HALF.load(Ordering::Relaxed))
}

/// `pc_target_format`, R9G9B9E5 targets made half floats or not
fn target_format(f: u32, rgb9e5_as_half: bool) -> u32 {
match f {
86 | 115 => 28,
67 if rgb9e5_as_half => 10,
f => pc_view_format(f),
}
}
Expand All @@ -749,6 +788,8 @@ pub fn pc_shader_view_format(resource: u32, view: u32) -> u32 {
(53 | 55, 0 | 53 | 55) => 56,
// a target made R8G8B8A8 (`pc_resource_desc`)
(28, 86 | 115) => 28,
// an R9G9B9E5 target made half floats
(10, 67) => 10,
(_, v) => v,
}
}
Expand Down Expand Up @@ -1125,7 +1166,7 @@ mod tests {
let mut x = [0u8; 0x38];
x[0] = 3; // texture 2D
let desc = |x: &[u8; 0x38]| {
let d = pc_resource_desc(x);
let d = resource_desc(x, true);
(u32::from_le_bytes(d[0x20..0x24].try_into().unwrap()), u32::from_le_bytes(d[0x30..0x34].try_into().unwrap()))
};
x[0x20] = 118; // D16_UNORM_S8_UINT, depth + UAV + console bits
Expand All @@ -1139,6 +1180,16 @@ mod tests {
x[0x20] = 86; // B5G5R5A1 target that is also a UAV
x[0x30..0x34].copy_from_slice(&0x5u32.to_le_bytes());
assert_eq!(desc(&x), (28, 0x5));
x[0x20] = 67; // R9G9B9E5 target that is also a UAV (and console bits): half floats
x[0x30..0x34].copy_from_slice(&0x30_0005u32.to_le_bytes());
assert_eq!(desc(&x), (10, 0x5));
// on a device that renders into the format and stores to it (an AMD RDNA4 card): kept
let d = resource_desc(&x, false);
assert_eq!(u32::from_le_bytes(d[0x20..0x24].try_into().unwrap()), 67);
assert_eq!((target_format(67, true), target_format(67, false)), (10, 67));
x[0x30..0x34].copy_from_slice(&0x0u32.to_le_bytes()); // a sampled one stays R9G9B9E5
assert_eq!(desc(&x), (67, 0));
assert_eq!((pc_shader_view_format(10, 67), pc_shader_view_format(67, 67)), (10, 67));
// views: a D24S8 format or R16_UNORM of the D16S8 buffer read depth; stencil formats plane 1
assert_eq!(pc_shader_view_format(44, 45), 46);
assert_eq!(pc_shader_view_format(44, 56), 46);
Expand Down
43 changes: 43 additions & 0 deletions crates/xcl/src/hle/d3d12x/real.rs
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,7 @@ pub fn init() -> Result<String, String> {
}
open_uma(&device);
super::device_x::probe_dynamic_depth_bias(&device);
super::device_x::probe_rgb9e5_targets(&device);
let raw = device.into_raw(); // one reference kept for the process lifetime
adopt(raw, Kind::Device);
DEVICE.store(raw as usize, Ordering::Release);
Expand Down Expand Up @@ -1969,6 +1970,18 @@ fn default_dsv(d: &D3D12_RESOURCE_DESC) -> Option<D3D12_DEPTH_STENCIL_VIEW_DESC>
Some(v)
}

/// A shader view's component mapping with every read of the fourth component (alpha) made the
/// constant 1 (D3D12_SHADER_COMPONENT_MAPPING_FORCE_VALUE_1); the other choices as they were.
fn alpha_read_as_one(mapping: u32) -> u32 {
let mut m = mapping;
for i in 0..4 {
if (m >> (3 * i)) & 7 == 3 {
m = (m & !(7 << (3 * i))) | (5 << (3 * i));
}
}
m
}

/// A shader view `v` (None: the resource's default one) of `rd` with the PC's format names
/// (`device_x::pc_shader_view_format`) – None: as it is.
fn pc_srv(rd: &D3D12_RESOURCE_DESC, v: Option<&D3D12_SHADER_RESOURCE_VIEW_DESC>) -> Option<D3D12_SHADER_RESOURCE_VIEW_DESC> {
Expand Down Expand Up @@ -2013,6 +2026,11 @@ fn pc_srv(rd: &D3D12_RESOURCE_DESC, v: Option<&D3D12_SHADER_RESOURCE_VIEW_DESC>)
if v.is_some() && f == out.Format.0 as u32 && !stencil {
return None;
}
// an R9G9B9E5 view of a target made half floats: that format has no alpha and reads 1 there,
// where the half floats hold whatever the title's pixel shader wrote
if (rf, out.Format.0 as u32, f) == (10, 67, 10) {
out.Shader4ComponentMapping = alpha_read_as_one(out.Shader4ComponentMapping);
}
out.Format = DXGI_FORMAT(f as i32);
// the PC reads stencil from plane 1; the console's view of it is plane 0 in a stencil format
if stencil {
Expand Down Expand Up @@ -6057,6 +6075,31 @@ mod srv_tests {
assert_eq!(v.ViewDimension.0, dim);
}
}

/// An R9G9B9E5 view of a target made half floats reads alpha 1, as that format does; the
/// title's other component choices stay.
#[test]
fn an_rgb9e5_view_of_half_floats_reads_alpha_one() {
use windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT;
assert_eq!(alpha_read_as_one(0x1688), 0x1A88); // R, G, B, 1
// B, A, R, constant 0 → B, 1, R, constant 0
assert_eq!(alpha_read_as_one(0x1000 | 2 | 3 << 3 | 4 << 9), 0x1000 | 2 | 5 << 3 | 4 << 9);
let rd = D3D12_RESOURCE_DESC { Dimension: D3D12_RESOURCE_DIMENSION_TEXTURE2D, Format: DXGI_FORMAT(10), DepthOrArraySize: 1, ..Default::default() };
let v = D3D12_SHADER_RESOURCE_VIEW_DESC {
Format: DXGI_FORMAT(67),
ViewDimension: D3D12_SRV_DIMENSION_TEXTURE2D,
Shader4ComponentMapping: 0x1688,
Anonymous: D3D12_SHADER_RESOURCE_VIEW_DESC_0 { Texture2D: D3D12_TEX2D_SRV { MipLevels: 1, ..Default::default() } },
};
let out = pc_srv(&rd, Some(&v)).expect("the view is made a half-float one");
assert_eq!((out.Format.0, out.Shader4ComponentMapping), (10, 0x1A88));
// a half-float view of it, or an R9G9B9E5 texture that stayed one: as the title made them
let mut half = v;
half.Format = DXGI_FORMAT(10);
assert!(pc_srv(&rd, Some(&half)).is_none());
let rd67 = D3D12_RESOURCE_DESC { Format: DXGI_FORMAT(67), ..rd };
assert!(pc_srv(&rd67, Some(&v)).is_none());
}
}

#[cfg(test)]
Expand Down
Loading