From e6ca9de427cb641c451b752c90fb22e4eb6c1250 Mon Sep 17 00:00:00 2001 From: Ahmet Dedeler <105894172+Ahmet-Dedeler@users.noreply.github.com> Date: Tue, 24 Nov 2026 18:20:31 +0100 Subject: [PATCH 1/4] D3D12.X: note a native D3D12 host when its first object is adopted Apple's D3DMetal (macOS under CrossOver) is native code in no PE module: `class_for` already asks which module the host's first method is in, and gets none there. That is now kept in `gpu::shader::NATIVE_HOST_MEMORY` and logged once. Nothing reads it yet; the depth formats of such a host are the first use (next commit). The flag and its place are those of `mac-on-main-wip` (e97bc3a, "Mac host basics"), taken out on their own; the rest of that commit is not here. Co-Authored-By: Claude Fable 5.2 --- crates/xcl/src/gpu/shader.rs | 4 ++++ crates/xcl/src/hle/d3d12x/real.rs | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/crates/xcl/src/gpu/shader.rs b/crates/xcl/src/gpu/shader.rs index 2eb90fb85..24328e939 100644 --- a/crates/xcl/src/gpu/shader.rs +++ b/crates/xcl/src/gpu/shader.rs @@ -201,6 +201,10 @@ fn dump_dir() -> PathBuf { crate::state::data_dir().join("shaders") } +/// Set when the host's D3D12 is native code outside any PE module (Apple's D3DMetal under +/// Wine), when its first object is adopted (`hle::d3d12x::real`). +pub static NATIVE_HOST_MEMORY: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false); + /// Is `[p, p+len)` committed, readable memory? (Game-provided pointers may be garbage /// when our guess of a console struct layout is wrong.) pub fn readable(p: usize, len: usize) -> bool { diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index 6cf818107..1f137b66f 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -746,6 +746,14 @@ unsafe fn class_for(obj: *mut c_void, orig: usize, kind: Kind) -> usize { } let public = iface.methods.len(); let m0 = module_of(*ot); + // a host D3D12 whose code is in no PE module (Apple's D3DMetal under Wine) + if m0 == 0 { + static ONCE: AtomicBool = AtomicBool::new(false); + if !ONCE.swap(true, Ordering::Relaxed) { + crate::gpu::shader::NATIVE_HOST_MEMORY.store(true, Ordering::Relaxed); + crate::xlog_warn!("D3D12.X: the host's D3D12 is not a PE module (a native host: D3DMetal)"); + } + } let mut scanned = 0; while scanned < public + MAX_SCAN && module_of(*ot.add(scanned)) == m0 { scanned += 1; From d496953892c9be10caa187a7aad7a031d47e268d Mon Sep 17 00:00:00 2001 From: Ahmet Dedeler <105894172+Ahmet-Dedeler@users.noreply.github.com> Date: Tue, 24 Nov 2026 00:57:14 +0300 Subject: [PATCH 2/4] Depth formats on D3DMetal: every depth + stencil family is depth only, on top of upstream's PC formats MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Metal makes no depth-only view of a depth + stencil texture and no view in another depth family than the texture's: its assertion ("failed assertion `Texture Creation`") ends the process where a Windows driver takes or refuses the view. Upstream now makes depth buffers typeless and reads them through R24_UNORM_X8 / R32_FLOAT_X8X24 views with stencil in plane 1 – that combination. On a native host (`device_x::depth_only`, XCL_DEPTH_STENCIL=1 restores upstream's mapping) the override sits on upstream's functions: - pc_resource_desc: R32G8X24 / R24G8 families (and the console's D16S8, which upstream maps to R24G8) → R32_TYPELESS / D32_FLOAT / R32_FLOAT (`depth_only_format`). - pc_view_format (pipelines' DSV formats, clear values, format queries) the same. - pc_shader_view_format: any depth-family or stencil view of an R32-family resource → R32_FLOAT (stencil views read the depth: the stencil plane is lost); of a 16-bit depth buffer → R16_UNORM. - pc_depth_view_format: a DSV takes the depth format of the resource's family. - XCL_TRACE_DEPTH_VIEWS=1 logs every view made of a depth-family resource, flushed. Upstream's mappings of B5G5R5A1 / B4G4R4A4 targets and of the console's 116 / 117 are as upstream has them (the local pc_format and view_format_for are gone). Co-Authored-By: Claude Mythos 5.2 (cherry picked from commit 8fc0600c0b92d70195272c1c15557e128dd06cbc) --- crates/xcl/src/hle/d3d12x/device_x.rs | 64 ++++++++++++++++++++++++++- crates/xcl/src/hle/d3d12x/real.rs | 38 +++++++++++++++- 2 files changed, 98 insertions(+), 4 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/device_x.rs b/crates/xcl/src/hle/d3d12x/device_x.rs index ddb9ef2a8..8c7edb2ba 100644 --- a/crates/xcl/src/hle/d3d12x/device_x.rs +++ b/crates/xcl/src/hle/d3d12x/device_x.rs @@ -800,6 +800,11 @@ pub fn pc_resource_desc(x: &[u8; 0x38]) -> [u8; 0x38] { if dim != D3D12_RESOURCE_DIMENSION_BUFFER.0 as u32 && (flags & 4) != 0 && (samples > 1 || !texture_format_allows_uav(pc_format)) { flags &= !4; } + // a native host: no depth + stencil formats (`depth_only`) + if depth_only() { + let f = u32::from_le_bytes(d[0x20..0x24].try_into().unwrap()); + d[0x20..0x24].copy_from_slice(&depth_only_format(f).to_le_bytes()); + } 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()); @@ -830,17 +835,48 @@ pub fn pc_initial_state_for_desc(orig_flags: u32, pc_flags: u32, state: u32) -> } } +/// Whether the depth + stencil families become depth-only ones: on a native D3D12 host (Apple's +/// D3DMetal under Wine), unless `XCL_DEPTH_STENCIL=1`. Metal makes no depth-only view of a +/// depth + stencil texture and no view in another depth family than the texture's – its +/// assertion ("failed assertion `Texture Creation`") ends the process, where a Windows driver +/// takes the view or refuses it – and the console reads its depth buffers through exactly such +/// views (R24_UNORM_X8 / R32_FLOAT_X8X24 of the typeless resource, R16_UNORM of its D16S8). So +/// there every depth + stencil family is R32_TYPELESS / D32_FLOAT / R32_FLOAT (`depth_only_format`, +/// on top of the PC mapping above and in `pc_view_format` / `pc_shader_view_format`), and the +/// stencil plane is lost: stencil views read the depth. +pub fn depth_only() -> bool { + static ON: std::sync::OnceLock = std::sync::OnceLock::new(); + // (not kept before there is a device: `NATIVE_HOST_MEMORY` is set when it is adopted) + if !crate::gpu::shader::NATIVE_HOST_MEMORY.load(Ordering::Relaxed) { + return false; + } + *ON.get_or_init(|| !std::env::var("XCL_DEPTH_STENCIL").is_ok_and(|v| v.trim() == "1")) +} + +/// A depth + stencil format (R32G8X24 and R24G8 families) as the depth-only one of the same +/// role: typeless → R32_TYPELESS, the depth format → D32_FLOAT, the planes' read formats → +/// R32_FLOAT. Others as they are. +pub fn depth_only_format(f: u32) -> u32 { + match f { + 19 | 44 => 39, + 20 | 45 => 40, + 21 | 22 | 46 | 47 => 41, + f => f, + } +} + /// The PC format of a view, pipeline or clear value in the console's D16_UNORM_S8_UINT family: /// D24_UNORM_S8_UINT, R24_UNORM_X8_TYPELESS, X24_TYPELESS_G8_UINT (`pc_resource_desc` makes such /// resources R24G8_TYPELESS). pub fn pc_view_format(f: u32) -> u32 { - match f { + let pc = match f { 118 => 45, 119 => 46, 120 => 47, 116 | 117 => 10, f => f, - } + }; + if depth_only() { depth_only_format(pc) } else { pc } } /// The typeless format of a depth format's family (None: not a depth format). @@ -870,6 +906,16 @@ pub fn pc_target_format(f: u32) -> u32 { /// readable formats and removes the device for anything else (0x887A0001). pub fn pc_shader_view_format(resource: u32, view: u32) -> u32 { let view = pc_view_format(view); + // a native host: the resource is of the R32 family whatever the console named (`depth_only`), + // and every view the console makes of a depth buffer – in its own family, the 16-bit one of + // its D16S8 buffer, a stencil plane, R8 of the stencil – reads its one plane + if depth_only() && matches!(resource, 39..=41) && matches!(view, 0 | 19..=22 | 39..=41 | 44..=47 | 53..=56 | 61 | 62) { + return 41; + } + // (and the other way round: a 16-bit depth buffer read through another family's format) + if depth_only() && matches!(resource, 53..=56) && matches!(view, 0 | 39..=41 | 53..=56) { + return 56; + } match (resource, view) { // R32G8X24_TYPELESS: depth 21, stencil 22 (19..=22, 0 | 19 | 20 | 21 | 39 | 40 | 41) => 21, @@ -934,6 +980,20 @@ pub fn stencil_view(resource: u32, view: u32) -> bool { matches!((resource, view), (19..=22, 22) | (44..=47, 47)) } +/// The format of a depth view of a resource whose PC format is `resource`, asked for as `view` +/// (already `pc_target_format`): on a native host the depth format of the resource's family +/// whatever was named (`depth_only`); else `view`. +pub fn pc_depth_view_format(resource: u32, view: u32) -> u32 { + if !depth_only() { + return view; + } + match resource { + 39..=41 => 40, + 53..=56 => 55, + _ => view, + } +} + /// A clear value (D3D12_CLEAR_VALUE: format first) with `pc_view_format`, copied into `buf`. pub(super) unsafe fn pc_clear(clear: *const c_void, buf: &mut [u8; 24]) -> *const c_void { if clear.is_null() || !crate::gpu::shader::readable(clear as usize, 24) { diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index 1f137b66f..259940b8e 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -2140,9 +2140,38 @@ unsafe extern "system" fn dev_create_dsv_x(this: *mut c_void, res: *mut c_void, if copy.is_none() { copy = ID3D12Resource::from_raw_borrowed(&res).and_then(|r| default_dsv(&r.GetDesc())); } + // a native host: the resource's own depth family whatever the view names (Metal ends the + // process on a view of another family, `device_x::depth_only`) + if let (Some(c), Some(r)) = (copy.as_mut(), ID3D12Resource::from_raw_borrowed(&res)) { + let rd = r.GetDesc(); + let made = super::device_x::pc_depth_view_format(rd.Format.0 as u32, c.Format.0 as u32); + trace_depth_view("depth", res, &rd, desc.as_ref().map_or(0, |d| d.Format.0), made as i32); + c.Format = windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT(made as i32); + } f(this, res, copy.as_ref().map_or(desc, |c| c as *const _), cpu) } +/// `XCL_TRACE_DEPTH_VIEWS=1`: every view made of a resource in a depth format family (the +/// format asked for, the one made, the resource) – flushed line by line, since a view the host +/// cannot make ends the process there (D3DMetal: Metal's texture assertion). +unsafe fn trace_depth_view(kind: &str, res: *mut c_void, rd: &D3D12_RESOURCE_DESC, asked: i32, made: i32) { + static ON: OnceLock = OnceLock::new(); + if !matches!(rd.Format.0, 19..=22 | 39..=41 | 44..=47 | 53..=56) || !*ON.get_or_init(|| std::env::var_os("XCL_TRACE_DEPTH_VIEWS").is_some()) { + return; + } + crate::xlog_info!( + "depth views: {kind} view format {asked} → {made} of {res:p} (format {}, {}x{}x{}, {} mips, {} samples, flags {:#x})", + rd.Format.0, + rd.Width, + rd.Height, + rd.DepthOrArraySize, + rd.MipLevels, + rd.SampleDesc.Count, + rd.Flags.0 + ); + crate::log::flush(); +} + /// The view a DSV without a description has, for a depth buffer `pc_resource_desc` made /// typeless (None: the resource's own format is a depth one). fn default_dsv(d: &D3D12_RESOURCE_DESC) -> Option { @@ -2237,6 +2266,9 @@ unsafe extern "system" fn dev_create_srv_x(this: *mut c_void, res: *mut c_void, if let Some(v) = ID3D12Resource::from_raw_borrowed(&res).and_then(|r| pc_srv(&r.GetDesc(), copy.as_ref())) { copy = Some(v); } + if let Some(r) = ID3D12Resource::from_raw_borrowed(&res) { + trace_depth_view("shader", res, &r.GetDesc(), desc.as_ref().map_or(0, |d| d.Format.0), copy.as_ref().map_or(0, |c| c.Format.0)); + } let desc = copy.as_ref().map_or(desc, |c| c as *const _); 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): @@ -2608,8 +2640,10 @@ unsafe extern "system" fn dev_create_uav_x(this: *mut c_void, res: *mut c_void, let mut copy = desc.as_ref().copied(); if let Some(c) = copy.as_mut() { if let Some(r) = ID3D12Resource::from_raw_borrowed(&res) { - let rf = r.GetDesc().Format.0 as u32; - c.Format = windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT(super::device_x::pc_shader_view_format(rf, c.Format.0 as u32) as i32); + let rd = r.GetDesc(); + let asked = c.Format.0; + c.Format = windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT(super::device_x::pc_shader_view_format(rd.Format.0 as u32, c.Format.0 as u32) as i32); + trace_depth_view("unordered access", res, &rd, asked, c.Format.0); } else { c.Format = windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT(super::device_x::pc_view_format(c.Format.0 as u32) as i32); if c.Format == windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT_UNKNOWN && view_dim_to_image_type(c.ViewDimension.0).is_some() { From 986d3c8a6db14c53093154cbaaa4c25a46cfe1f2 Mon Sep 17 00:00:00 2001 From: Ahmet Dedeler <105894172+Ahmet-Dedeler@users.noreply.github.com> Date: Tue, 24 Nov 2026 18:21:24 +0100 Subject: [PATCH 3/4] Tests: a native host's depth-only formats `depth_only_format` takes both depth + stencil families to the R32 one, role for role, and leaves 16-bit depth and colour formats alone; off a native host the PC mapping is unchanged. The `device_x` part of `mac-on-main-wip` aaf191b; its `isa` tests are not here. Co-Authored-By: Claude Mythos 5.2 --- crates/xcl/src/hle/d3d12x/device_x.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/crates/xcl/src/hle/d3d12x/device_x.rs b/crates/xcl/src/hle/d3d12x/device_x.rs index 8c7edb2ba..3d9ed32d7 100644 --- a/crates/xcl/src/hle/d3d12x/device_x.rs +++ b/crates/xcl/src/hle/d3d12x/device_x.rs @@ -1475,6 +1475,23 @@ mod tests { assert_eq!(pc_initial_state_for_desc(0x6, 0x2, 0x8), 0x10); } + /// A native host's depth-only formats: both depth + stencil families land in the R32 one, + /// role for role; everything else – 16-bit depth, colour formats – stays. + #[test] + fn depth_stencil_families_become_the_r32_family() { + for (typeless, depth, planes) in [(19, 20, [21, 22]), (44, 45, [46, 47])] { + assert_eq!((depth_only_format(typeless), depth_only_format(depth)), (39, 40)); + assert_eq!(planes.map(depth_only_format), [41, 41]); + } + for same in [0, 10, 28, 39, 40, 41, 53, 55, 56, 61, 86, 116] { + assert_eq!(depth_only_format(same), same); + } + // (off a native host nothing changes: these tests run without a device) + if !depth_only() { + assert_eq!((pc_view_format(118), pc_shader_view_format(44, 56), pc_depth_view_format(39, 45)), (45, 46, 45)); + } + } + #[test] fn derived_pipeline_patches_land_on_the_desc_parts() { use std::mem::{offset_of, size_of}; From 579e0be524aab2d2ab662024acb2352223fa0778 Mon Sep 17 00:00:00 2001 From: ShortSide <83917259+shortside@users.noreply.github.com> Date: Tue, 24 Nov 2026 19:01:49 +0100 Subject: [PATCH 4/4] Tests: the depth format tests name their host; a native host's mapping runs everywhere `depth_only` is the process's: once a test has adopted a device of a native host, the tests of the PC's depth formats that run after it in the same process saw the depth-only mapping and failed (`console_depth_buffers_and_their_views_are_the_pcs`: (39, 2) for (44, 2); `console_formats_are_asked_as_the_pcs`: 40 for 45; CrossOver 26.3, D3DMetal, `hle::d3d12x:: --test-threads=1`). Alone they passed. A test now says which host it describes (`depth_only_for_test`, a thread-local in test builds only). The two tests of the PC's formats say "not a native host"; `depth_stencil_families_become_the_r32_family` checks both answers through the real functions (resource descriptions, pipeline and clear-value formats, shader views, depth views), so the native mapping is tested on Windows too, where no host turns it on. Co-Authored-By: Claude Fable 5.2 --- crates/xcl/src/hle/d3d12x/device_x.rs | 46 +++++++++++++++++++++++++-- crates/xcl/src/hle/d3d12x/real.rs | 1 + 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/device_x.rs b/crates/xcl/src/hle/d3d12x/device_x.rs index 3d9ed32d7..81509fe9b 100644 --- a/crates/xcl/src/hle/d3d12x/device_x.rs +++ b/crates/xcl/src/hle/d3d12x/device_x.rs @@ -845,6 +845,10 @@ pub fn pc_initial_state_for_desc(orig_flags: u32, pc_flags: u32, state: u32) -> /// on top of the PC mapping above and in `pc_view_format` / `pc_shader_view_format`), and the /// stencil plane is lost: stencil views read the depth. pub fn depth_only() -> bool { + #[cfg(test)] + if let Some(on) = DEPTH_ONLY_FOR_TEST.with(|c| c.get()) { + return on; + } static ON: std::sync::OnceLock = std::sync::OnceLock::new(); // (not kept before there is a device: `NATIVE_HOST_MEMORY` is set when it is adopted) if !crate::gpu::shader::NATIVE_HOST_MEMORY.load(Ordering::Relaxed) { @@ -853,6 +857,25 @@ pub fn depth_only() -> bool { *ON.get_or_init(|| !std::env::var("XCL_DEPTH_STENCIL").is_ok_and(|v| v.trim() == "1")) } +#[cfg(test)] +thread_local! { + /// A test's own answer for `depth_only` on its thread. The host's answer is the process's, + /// set by whichever test adopts a device first, so a test of the formats names its host. + static DEPTH_ONLY_FOR_TEST: std::cell::Cell> = const { std::cell::Cell::new(None) }; +} + +/// `depth_only` is `on` for this thread until the guard is dropped. +#[cfg(test)] +pub(super) fn depth_only_for_test(on: bool) -> impl Drop { + struct Back(Option); + impl Drop for Back { + fn drop(&mut self) { + DEPTH_ONLY_FOR_TEST.with(|c| c.set(self.0)); + } + } + Back(DEPTH_ONLY_FOR_TEST.with(|c| c.replace(Some(on)))) +} + /// A depth + stencil format (R32G8X24 and R24G8 families) as the depth-only one of the same /// role: typeless → R32_TYPELESS, the depth format → D32_FLOAT, the planes' read formats → /// R32_FLOAT. Others as they are. @@ -1373,6 +1396,7 @@ mod tests { /// D24S8, no UAV flag; views get the family's readable formats (stencil from plane 1). #[test] fn console_depth_buffers_and_their_views_are_the_pcs() { + let _pc = depth_only_for_test(false); let mut x = [0u8; 0x38]; x[0] = 3; // texture 2D let desc = |x: &[u8; 0x38]| { @@ -1486,10 +1510,28 @@ mod tests { for same in [0, 10, 28, 39, 40, 41, 53, 55, 56, 61, 86, 116] { assert_eq!(depth_only_format(same), same); } - // (off a native host nothing changes: these tests run without a device) - if !depth_only() { + // off a native host nothing changes + { + let _pc = depth_only_for_test(false); assert_eq!((pc_view_format(118), pc_shader_view_format(44, 56), pc_depth_view_format(39, 45)), (45, 46, 45)); } + // on one: resources, views, pipelines' and clear values' formats, role for role + let _native = depth_only_for_test(true); + let mut x = [0u8; 0x38]; + x[0] = 3; // texture 2D + x[0x30..0x34].copy_from_slice(&0x2u32.to_le_bytes()); // a depth buffer + for console in [118u8, 45, 20, 40] { + x[0x20] = console; // D16S8, D24S8, D32S8 and D32 buffers: all R32_TYPELESS + assert_eq!(u32::from_le_bytes(pc_resource_desc(&x)[0x20..0x24].try_into().unwrap()), 39, "depth buffer {console}"); + } + assert_eq!((pc_view_format(118), pc_view_format(45), pc_view_format(20), pc_view_format(86)), (40, 40, 40, 86)); + // every view the console makes of such a buffer reads its one plane, stencil views too + for view in [0, 21, 22, 41, 46, 47, 56, 118, 119, 120] { + assert_eq!(pc_shader_view_format(39, view), 41, "view {view} of the R32 buffer"); + } + assert_eq!((pc_shader_view_format(53, 39), pc_shader_view_format(28, 86)), (56, 28)); + // a depth view: the depth format of the resource's family, whatever was named + assert_eq!((pc_depth_view_format(39, 45), pc_depth_view_format(53, 40), pc_depth_view_format(28, 28)), (40, 55, 28)); } #[test] diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index 259940b8e..f418169ee 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -7753,6 +7753,7 @@ mod console_format_query_tests { #[test] fn console_formats_are_asked_as_the_pcs() { + let _pc = crate::hle::d3d12x::device_x::depth_only_for_test(false); for (console, pc) in [(116u32, 10u32), (117, 10), (118, 45), (119, 46), (120, 47)] { let mut d = [0u32; 3]; d[0] = console;