Skip to content
Draft
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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,11 @@ Fill the reusable DXIL cache ahead of a run with the read-only package wrapper.
console shaders' hashes (`cs`, `vs` / `ps`, `ms` / `ps`, `vs` / `gs` / `ps`).
- `XCL_TEXTURE_LEVEL_PAGES=1` gives sampled title textures backing as their mip levels stream in.
Zero-filled mip uploads in a batch share one staging span and still clear any mapped tiles.
- Single-sampled sRGB textures with unordered access use typeless storage where the adapter
supports the matching UNORM UAV. SRVs and RTVs retain sRGB conversion. `XCL_SRGB_TYPELESS=0`
restores the previous behavior of dropping unordered access. The log counts the textures
that keep or lose unordered access. PC D3D12 has no BGRX typed UAV, so BGRX retains sRGB
sampling and rendering with unordered access stripped.
- `XCL_DSTORAGE_PARALLEL_DECOMPRESS=1` enables parallel CPU decompression for file-backed zlib
requests with virtual destinations. Defaults: four workers and a shared 32 MiB staging budget
(`XCL_DSTORAGE_WORKERS`, `XCL_DSTORAGE_MEMORY_BUDGET_MIB`). Requests retire in queue order.
Expand Down
2 changes: 1 addition & 1 deletion THIRD_PARTY.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ carry a note in their header about its origin and the date of changes (GPL-3.0
| [FFmpeg](https://ffmpeg.org/) | LGPL-2.1+ | XMA1/XMA2 decoder and tables (`crates/xcl/src/hle/xma2/`) – after `libavcodec/wmaprodec.c`, `wmaprodata.h`, `wma.c`, `sinewin_tablegen.h`, `libavutil/float_dsp.c`, `tx_template.c` | port to safe Rust with annotation |
| [AMD Machine-Readable GPU ISA](https://gpuopen.com/machine-readable-isa/) | MIT | `crates/xcl/src/gpu/rdna2_spec.rs` (generated by `scripts/gen_rdna2.py`) | encoding and instruction tables |
| [AMD addrlib](https://gitlab.freedesktop.org/mesa/mesa/-/tree/main/src/amd/addrlib) (Mesa) | MIT | `crates/xcl/src/gpu/tiling_tables.rs` (generated by `scripts/gen_tiling.py` from `gfx10SwizzlePattern.h`); GFX10 mip chain / mip tail layout in `gpu/tiling.rs` (after `Gfx10Lib::ComputeSurfaceInfoMacroTiled`) | swizzle tables; layout logic ported with annotation |
| [VIctory](https://github.com/Marvisak/VIctory) | GPL-3.0 | `crates/xcl/src/hle/bcpack/`: port of its `victory-formats/src/bcpack.rs`, and `tables.rs` generated by `scripts/gen_bcpack_victory_tables.py` from its `bcpack-tables.bin` (the format's constant tables; its 128 rANS models are those of Microsoft's [silicon-rans](https://github.com/microsoft/silicon-rans) sample, MIT) | port to Rust; tables |
| [VIctory](https://github.com/Marvisak/VIctory) | GPL-3.0 | `crates/xcl/src/hle/bcpack/`: port of its `victory-formats/src/bcpack.rs`, and `tables.rs` generated by `scripts/gen_bcpack_victory_tables.py` from its `bcpack-tables.bin` (the format's constant tables; its 128 rANS models are those of Microsoft's [silicon-rans](https://github.com/microsoft/silicon-rans) sample, MIT); the typeless sRGB UAV mapping in `d3d12x/device_x.rs`, after `victory-gpu/src/resource.rs` | port to Rust; tables; format mapping (code written independently) |
| [LLVM](https://github.com/llvm/llvm-project) | Apache-2.0 WITH LLVM-exception | `tests/data/llvm-amdgpu/*` (test data, not part of the program); GFX10 buffer format values (`UfmtSymbolicGFX10`) in `gpu/recomp/emit.rs` | test vectors; hardware facts |
| [WinDurango](https://github.com/WinDurango/WinDurango) | LGPL-3.0 | no code; d3d12_x.dll export numbers (facts) in `crates/xcl/data/stubdb.toml` | reference only |
| [libHttpClient](https://github.com/microsoft/libHttpClient) | MIT | no code; XTaskQueue/XAsync contract | specification |
Expand Down
5 changes: 4 additions & 1 deletion crates/xcl/src/hle/d3d12x/bind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4315,7 +4315,10 @@ unsafe fn trace_draw_kinds(this: *mut c_void, pso: usize, indirect: bool) {
}
let Some((vs, ps)) = super::real::pso_shaders(pso) else { return };
let rt = super::present::rt0_resource(this as usize).unwrap_or(0);
let desc = |r: usize| ID3D12Resource::from_raw_borrowed(&(r as *mut c_void)).map(|x| { let d = x.GetDesc(); (d.Width, d.Height, d.Format.0, d.SampleDesc.Count) });
let desc = |r: usize| ID3D12Resource::from_raw_borrowed(&(r as *mut c_void)).map(|x| {
let d = x.GetDesc();
(d.Width, d.Height, super::real::console_format_of(r, d.Format.0 as u32) as i32, d.SampleDesc.Count)
});
// `XCL_TRACE_FORMATS=26,29` (DXGI format numbers): only draws into targets of those formats –
// the first 160 kinds of a busy frame are its first passes, and the ones looked for (a
// lighting target's) never got a line
Expand Down
146 changes: 134 additions & 12 deletions crates/xcl/src/hle/d3d12x/device_x.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
//!
//! Console queue and list types add DMA (0x10, the copy engine); console queue priorities
//! are mapped onto PC ones.
//!
//! The sRGB UAV typeless mapping follows VIctory's victory-gpu/src/resource.rs
//! (https://github.com/Marvisak/VIctory, GPL-3.0).

use std::collections::HashMap;
use std::ffi::{c_char, c_void, CStr, CString};
Expand Down Expand Up @@ -812,12 +815,40 @@ pub(super) unsafe fn strip_unsupported_uav_for_device(device: *mut c_void, mut d
/// `pc_resource_desc` further validated against `device`'s `D3D12_FEATURE_FORMAT_SUPPORT` so
/// `GetResourceAllocationInfo` never sees `ALLOW_UNORDERED_ACCESS` on an unsupported format.
pub(super) unsafe fn pc_resource_desc_for_device(device: *mut c_void, x: &[u8; 0x38]) -> [u8; 0x38] {
strip_unsupported_uav_for_device(device, pc_resource_desc(x))
let srgb = srgb_typeless_enabled() && srgb_uav_format(x).is_some_and(|f| {
device_format_supports_uav(device, srgb_uav_linear_format(f))
});
strip_unsupported_uav_for_device(device, pc_resource_desc_with_srgb_typeless(x, srgb))
}

pub(super) fn srgb_typeless_enabled() -> bool {
static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
*ON.get_or_init(|| !std::env::var("XCL_SRGB_TYPELESS").is_ok_and(|v| v.trim() == "0"))
}

pub(super) fn srgb_typeless_family(format: u32) -> Option<u32> {
match format {
29 => Some(27),
91 => Some(90),
93 => Some(92),
_ => None,
}
}

pub(super) fn srgb_uav_format(x: &[u8; 0x38]) -> Option<u32> {
let u = |o: usize| u32::from_le_bytes(x[o..o + 4].try_into().unwrap());
let format = u(0x20);
srgb_typeless_family(format)?;
(matches!(u(0), 2..=4) && u(0x24) == 1 && u(0x30) & 6 == 4).then_some(format)
}

/// 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] {
pc_resource_desc_with_srgb_typeless(x, srgb_typeless_enabled())
}

fn pc_resource_desc_with_srgb_typeless(x: &[u8; 0x38], srgb_typeless: 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 Down Expand Up @@ -859,12 +890,21 @@ pub fn pc_resource_desc(x: &[u8; 0x38]) -> [u8; 0x38] {
if flags & 2 != 0 {
flags &= !4;
}
// Keep shader writes linear while the texture's SRVs and RTVs retain sRGB conversion.
// (only where the linear sibling takes typed UAVs: B8G8R8X8 has none on the PC, so a BGRX
// sRGB texture keeps its format and loses the flag as before)
let srgb_family = srgb_uav_format(x)
.filter(|&f| srgb_typeless && texture_format_allows_uav(srgb_uav_linear_format(f)))
.and_then(srgb_typeless_family);
if let Some(family) = srgb_family {
d[0x20..0x24].copy_from_slice(&family.to_le_bytes());
}
// Any non-buffer texture whose PC format cannot support typed UAVs (BC1–BC7, sRGB, depth,
// 96-bit, A8, B5G6R5, B8G8R8X8, etc.) or that is multisampled must not carry
// ALLOW_UNORDERED_ACCESS: PC D3D12 rejects even GetResourceAllocationInfo (id=599 / 0x887A0001).
let pc_format = u32::from_le_bytes(d[0x20..0x24].try_into().unwrap());
let samples = u32::from_le_bytes(d[0x24..0x28].try_into().unwrap());
if dim != D3D12_RESOURCE_DIMENSION_BUFFER.0 as u32 && (flags & 4) != 0 && (samples > 1 || !texture_format_allows_uav(pc_format)) {
if dim != D3D12_RESOURCE_DIMENSION_BUFFER.0 as u32 && (flags & 4) != 0 && srgb_family.is_none() && (samples > 1 || !texture_format_allows_uav(pc_format)) {
flags &= !4;
}
d[8..16].fill(0); // alignment: the driver's
Expand Down Expand Up @@ -969,6 +1009,12 @@ pub fn pc_uav_linear_format(fmt: u32) -> u32 {
}
}

/// A typeless BGRX resource must use its own UNORM sibling. PC D3D12 has no BGRX typed UAV
/// support, so the device path drops its UAV flag; BGRA (87) is from a different family.
pub(super) fn srgb_uav_linear_format(fmt: u32) -> u32 {
if fmt == 93 { 88 } else { pc_uav_linear_format(fmt) }
}

/// Whether the host D3D12 device supports typed UAV views (`D3D12_FORMAT_SUPPORT1_TYPED_UNORDERED_ACCESS_VIEW`)
/// for `fmt`. Queries `CheckFeatureSupport` once per `(adapter LUID, fmt)` and caches the answer.
pub(super) unsafe fn pc_typed_uav_supported(dev_raw: *mut c_void, fmt: i32) -> bool {
Expand Down Expand Up @@ -1023,15 +1069,18 @@ pub(super) fn pc_clear_format(pc_format: u32, clear_format: u32) -> u32 {
}
}

/// Like `pc_clear`, also aligning `clear.Format` with `pc_format` when `pc_resource_desc` promoted
/// a UAV-capable `B5G6R5` (`85 -> 28`) or `B8G8R8X8` (`88 -> 87`) resource.
pub(super) unsafe fn pc_clear_for_desc(clear: *const c_void, pc_format: u32, buf: &mut [u8; 24]) -> *const c_void {
/// Like `pc_clear`, also aligning the clear format for promoted UAV resources. An sRGB resource
/// made typeless keeps its original typed clear format.
pub(super) unsafe fn pc_clear_for_desc(clear: *const c_void, x: &[u8; 0x38], d: &[u8; 0x38], buf: &mut [u8; 24]) -> *const c_void {
if clear.is_null() || !crate::gpu::shader::readable(clear as usize, 24) {
return clear;
}
std::ptr::copy_nonoverlapping(clear as *const u8, buf.as_mut_ptr(), 24);
let f = u32::from_le_bytes(buf[0..4].try_into().unwrap());
buf[0..4].copy_from_slice(&pc_clear_format(pc_format, f).to_le_bytes());
let pc_format = u32::from_le_bytes(d[0x20..0x24].try_into().unwrap());
let format = srgb_uav_format(x).filter(|&f| srgb_typeless_family(f) == Some(pc_format))
.unwrap_or_else(|| pc_clear_format(pc_format, f));
buf[0..4].copy_from_slice(&format.to_le_bytes());
buf.as_ptr() as *const c_void
}

Expand Down Expand Up @@ -1257,8 +1306,7 @@ pub(super) unsafe extern "system" fn create_placed_resource_x(this: *mut c_void,
// PC's: the PC refused the resource (E_INVALIDARG) and the title threw
let state = pc_initial_state(u32::from_le_bytes(x[0x30..0x34].try_into().unwrap()), state);
let mut clear_buf = [0u8; 24];
let pc_format = u32::from_le_bytes(d[0x20..0x24].try_into().unwrap());
let clear = pc_clear_for_desc(clear, pc_format, &mut clear_buf);
let clear = pc_clear_for_desc(clear, &x, &d, &mut clear_buf);
if buffer {
if let Some(hr) = real::place_at_title_address(this, va as usize, &d, state, clear, riid, ppv) {
return hr;
Expand All @@ -1283,6 +1331,7 @@ pub(super) unsafe extern "system" fn create_placed_resource_x(this: *mut c_void,
if super::vram::gpu_owned(flags) {
if let Some(hr) = super::vram::place(this, va as usize, &pc, state, clear, console_bytes, riid, ppv) {
if hr >= 0 && !(*ppv).is_null() {
real::note_srgb_texture(*ppv, &x);
if super::title_textures::register_gpu_owned(this, *ppv, va as usize, &x, D3D12_RESOURCE_STATES(state as i32)).is_err() {
if let Some(bytes) = console_bytes {
super::title_textures::retire_overlapping(va as usize, bytes as usize);
Expand All @@ -1308,6 +1357,7 @@ pub(super) unsafe extern "system" fn create_placed_resource_x(this: *mut c_void,
}
};
if hr >= 0 && !buffer && !(*ppv).is_null() {
real::note_srgb_texture(*ppv, &x);
static M: AtomicU64 = AtomicU64::new(0);
match super::title_textures::register(this, *ppv, va as usize, &x, D3D12_RESOURCE_STATES(state as i32)) {
Ok(()) => {
Expand Down Expand Up @@ -1392,6 +1442,11 @@ pub(super) unsafe extern "system" fn get_copyable_footprints_x(
return f(this, desc, first, num, base, layouts, rows, row_sizes, total);
}
let x: [u8; 0x38] = (desc as *const [u8; 0x38]).read_unaligned();
if srgb_typeless_enabled() && srgb_uav_format(&x).is_some() {
// The layout is the same either way; keep the title's typed format in its footprints.
let d = pc_resource_desc_with_srgb_typeless(&x, false);
return f(this, d.as_ptr(), first, num, base, layouts, rows, row_sizes, total);
}
let mut probe = u64::MAX;
let t = if total.is_null() { &mut probe as *mut u64 } else { total };
*t = u64::MAX;
Expand Down Expand Up @@ -1771,7 +1826,7 @@ mod tests {
x[0x20..0x24].copy_from_slice(&fmt.to_le_bytes());
x[0x24..0x28].copy_from_slice(&samples.to_le_bytes());
x[0x30..0x34].copy_from_slice(&flags.to_le_bytes());
let d = pc_resource_desc(&x);
let d = pc_resource_desc_with_srgb_typeless(&x, false);
(
u32::from_le_bytes(d[0x20..0x24].try_into().unwrap()),
u32::from_le_bytes(d[0x30..0x34].try_into().unwrap()),
Expand Down Expand Up @@ -1958,6 +2013,67 @@ mod tests {
assert_eq!(pc_uav_linear_format(28), 28);
}

#[test]
fn srgb_uav_resource_desc_selects_typeless_only_for_eligible_textures() {
for (srgb, family, linear) in [(29u32, 27, 28), (91, 90, 87), (93, 93, 88)] {
assert_eq!(srgb_uav_linear_format(srgb), linear);
// BGRX: no typed UAV of its linear sibling on the PC, so never typeless with the flag
let uav = if srgb == 93 { 0 } else { 4 };
let mut x = [0u8; 0x38];
x[0x20..0x24].copy_from_slice(&srgb.to_le_bytes());
for (dim, samples, flags, expected_format, expected_flags) in [
(2, 1, 4, family, uav),
(3, 1, 5, family, 1 | uav),
(4, 1, 4, family, uav),
(3, 1, 1, srgb, 1),
(3, 4, 5, srgb, 1),
(3, 1, 6, srgb, 2),
(1, 1, 4, srgb, 4),
] {
x[0] = dim;
x[0x24] = samples;
x[0x30] = flags;
let d = pc_resource_desc_with_srgb_typeless(&x, true);
assert_eq!((u32::from(d[0x20]), d[0x30]), (expected_format, expected_flags));
}
x[0] = 3;
x[0x30] = 5;
let d = pc_resource_desc_with_srgb_typeless(&x, false);
assert_eq!((u32::from(d[0x20]), d[0x30]), (srgb, 1));
if srgb == 93 {
let d = unsafe { pc_resource_desc_for_device(std::ptr::null_mut(), &x) };
assert_eq!((d[0x20], d[0x30]), (93, 1), "BGRX has no PC typed UAV");
}
}
}

#[test]
fn srgb_uav_clear_value_keeps_the_title_format() {
// (BGRX sRGB, 93, is never made typeless: its clear follows the typed rule, as on main)
for (srgb, raw_format) in [(29u8, 28), (91, 0)] {
let mut x = [0u8; 0x38];
x[0] = 3;
x[0x20] = srgb;
x[0x24] = 1;
x[0x30] = 5;
let d = pc_resource_desc_with_srgb_typeless(&x, true);
let mut raw = [0u8; 24];
let mut buf = [0u8; 24];
raw[0] = raw_format;
raw[4..8].copy_from_slice(&0.5f32.to_le_bytes());
unsafe { pc_clear_for_desc(raw.as_ptr().cast(), &x, &d, &mut buf) };
assert_eq!((buf[0], &buf[4..]), (srgb, &raw[4..]));
}
let mut x = [0u8; 0x38];
(x[0], x[0x20], x[0x24], x[0x30]) = (3, 93, 1, 5);
let d = pc_resource_desc_with_srgb_typeless(&x, true);
assert_eq!((d[0x20], d[0x30]), (93, 1));
let (mut raw, mut buf) = ([0u8; 24], [0u8; 24]);
raw[0] = 92;
unsafe { pc_clear_for_desc(raw.as_ptr().cast(), &x, &d, &mut buf) };
assert_eq!(u32::from(buf[0]), pc_clear_format(93, 92));
}

#[test]
fn pc_clear_for_desc_aligns_promoted_uav_targets_only() {
let desc_fmt = |fmt: u32, flags: u32| {
Expand All @@ -1975,10 +2091,16 @@ mod tests {
let (mut raw, mut buf) = ([0u8; 24], [0u8; 24]);
raw[0..4].copy_from_slice(&85u32.to_le_bytes());
raw[4..8].copy_from_slice(&0.5f32.to_le_bytes());
let mut x = [0u8; 0x38];
x[0] = 3;
x[0x20] = 85;
x[0x24] = 1;
x[0x30] = 5;
let d = pc_resource_desc(&x);
unsafe {
assert_eq!(pc_clear_for_desc(raw.as_ptr() as *const c_void, 28, &mut buf), buf.as_ptr() as *const c_void);
assert_eq!(pc_clear_for_desc(raw.as_ptr() as *const c_void, &x, &d, &mut buf), buf.as_ptr() as *const c_void);
assert_eq!((u32::from_le_bytes(buf[0..4].try_into().unwrap()), &buf[4..24]), (28, &raw[4..24]));
assert!(pc_clear_for_desc(std::ptr::null(), 28, &mut buf).is_null());
assert!(pc_clear_for_desc(std::ptr::null(), &x, &d, &mut buf).is_null());
}
}

Expand Down Expand Up @@ -2017,7 +2139,7 @@ mod tests {
};
let (mut old_buf, mut new_buf) = ([0u8; 24], [0u8; 24]);
let old_cv: D3D12_CLEAR_VALUE = std::ptr::read_unaligned(pc_clear(&clear as *const _ as *const c_void, &mut old_buf) as *const _);
let new_cv: D3D12_CLEAR_VALUE = std::ptr::read_unaligned(pc_clear_for_desc(&clear as *const _ as *const c_void, pc_fmt, &mut new_buf) as *const _);
let new_cv: D3D12_CLEAR_VALUE = std::ptr::read_unaligned(pc_clear_for_desc(&clear as *const _ as *const c_void, &x, &d_bytes, &mut new_buf) as *const _);
let mut r: Option<ID3D12Resource> = None;
assert!(dev.CreateCommittedResource(&heap, D3D12_HEAP_FLAG_NONE, &d, D3D12_RESOURCE_STATE_COMMON, Some(&old_cv), &mut r).is_err());
assert_eq!(new_cv.Format.0 as u32, promoted_fmt);
Expand Down
Loading
Loading