From 301adaea6240613638f51c0d2ed920a47b9d6121 Mon Sep 17 00:00:00 2001 From: ryosen0 Date: Tue, 24 Nov 2026 01:45:02 +0300 Subject: [PATCH 1/7] Recompiler: the mesh pass-through budget accounts DXC's vertex chunks A retail title's NGG transform programs (128 vertices / 128 primitives, 21 params + SV_ClipDistance0 = 352 B per vertex) all failed ms_6_6 with "vertex plus primitive output size is greater than 32768": the split budget divided 32768 by the stride, but DXC accounts whole chunks of 32 vertices even when fewer are declared - verts[78] at 352 B is accounted as 96 (33792 B) and refused. Measured with dxc 1.8.2502 on this machine: every boundary from 9 to 26 float4 rows agrees with the chunk model. passthrough_split now floors the vertex budget to a multiple of 32 (capped at 256); the wide records go out in 7 groups of 19 triangles instead of 5 groups of 26 that DXC refuses. Test: the wide record splits and compiles, a 96-vertex group at 320 B compiles, a narrow record stays whole; fails without the change (5 != 7). Assisted-by: DSH (GPT-6.2 Astra) --- crates/xcl/src/gpu/recomp/emit.rs | 13 +++++-- crates/xcl/src/gpu/recomp/tests.rs | 56 ++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 2 deletions(-) diff --git a/crates/xcl/src/gpu/recomp/emit.rs b/crates/xcl/src/gpu/recomp/emit.rs index 216240bd2c..ae4e82fda4 100644 --- a/crates/xcl/src/gpu/recomp/emit.rs +++ b/crates/xcl/src/gpu/recomp/emit.rs @@ -4885,15 +4885,24 @@ pub fn mesh_passthrough_for(ms: &MsInterface, ps_inputs: Option<&[SigElement]>) /// D3D12's limit on what one mesh shader group outputs (vertex + primitive attributes). const MESH_OUTPUT_BYTES: u32 = 32768; +/// Vertices the pass-through puts in one mesh group: the vertex output budget divided by the +/// stride, and DXC accounts whole chunks of 32 vertices even when fewer are declared (78 +/// vertices with 22 rows are accounted as 96). Measured with dxc 1.8.2502 (ms_6_6): every +/// boundary from 9 to 26 float4 rows agrees. +fn passthrough_max_vertices(vbytes: u32) -> u32 { + (MESH_OUTPUT_BYTES / vbytes).min(256) / 32 * 32 +} + /// Mesh groups the pass-through spends on one record: 1 when the record's vertices fit the /// mesh output limit as they are, else the triangles are split into runs whose own vertex /// copies (3 per triangle) fit. `elements`: output elements besides SV_Position. pub fn passthrough_split(ms: &MsInterface, elements: usize) -> u32 { let vbytes = 16 * (1 + elements as u32); - if vbytes * ms.max_vertices <= MESH_OUTPUT_BYTES { + let cap = passthrough_max_vertices(vbytes); + if ms.max_vertices <= cap { return 1; } - let ppg = (MESH_OUTPUT_BYTES / (3 * vbytes)).max(1); + let ppg = (cap / 3).max(1); ms.max_primitives.max(1).div_ceil(ppg) } diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs index 4e032b68d1..983fe4c4ca 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -2899,6 +2899,62 @@ fn gpu_mesh_mode_two_passes() { assert!(px16 == px, "split pass-through draws the same pixels"); } +/// The mesh output budget with wide vertices: DXC accounts whole chunks of 32 vertices even +/// when fewer are declared, so 78 vertices with 22 float4 rows are accounted as 96 (33792 B > +/// 32768) and the record must go out in split groups. A retail title's NGG transform programs +/// have this shape (128 vertices / 128 primitives, 21 params + SV_ClipDistance0); the previous +/// budget (32768 / stride) emitted verts[78] and DXC refused every one of them. +#[test] +fn mesh_split_accounts_vertex_chunks() { + use super::emit::{mesh_passthrough, passthrough_elements, passthrough_split}; + use super::MsInterface; + if super::dxc::location().is_err() { + println!("no DXC – skipping"); + runner::skip_test("no DXC"); + return; + } + let sig = |name: &str, sv: u32, reg: u32, index: u32| crate::gpu::dxbc::SigElement { + name: name.into(), + index, + system_value: sv, + component_type: 3, + register: reg, + mask: 0xF, + rw_mask: 0xF, + stream: 0, + }; + let mut outputs = vec![sig("SV_Position", 1, 0, 0)]; + for k in 0..20u32 { + outputs.push(sig("TEXCOORD", 0, k + 1, k)); + } + outputs.push(sig("SV_ClipDistance", 2, 21, 0)); + let ms = MsInterface { outputs, threads: 128, max_vertices: 128, max_primitives: 128 }; + let elems = passthrough_elements(&ms, None); + assert_eq!(elems, 21); + // 22 rows * 16 B = 352 B/vertex: 64 accounted vertices fit (96 would be 33792 B), so at + // most 21 triangles' worth of copies per group, ceil(128/21) = 7 groups of 19 + let split = passthrough_split(&ms, elems); + assert_eq!(split, 7, "chunks of 32: 78 verts are accounted as 96 and refused"); + let pass = mesh_passthrough(&ms); + assert!(pass.contains("verts[57]"), "19 triangles per group:\n{pass}"); + super::dxc::compile(&pass, "main", "ms_6_6", &[]).unwrap_or_else(|e| panic!("{e}\n{pass}")); + // 20 rows * 16 B = 320 B/vertex: 96 accounted vertices fit (30720 B), but a whole record + // is 128 vertices (40960 B) – 4 groups of 32 triangles, 96 vertices each + let mut ms20 = ms.clone(); + ms20.outputs.pop(); + assert_eq!(passthrough_split(&ms20, passthrough_elements(&ms20, None)), 4); + let pass20 = mesh_passthrough(&ms20); + assert!(pass20.contains("verts[96]"), "96 vertices per group:\n{pass20}"); + super::dxc::compile(&pass20, "main", "ms_6_6", &[]).unwrap_or_else(|e| panic!("{e}\n{pass20}")); + // 8 rows * 16 B = 128 B/vertex: 128 vertices are 16384 B – the record stays whole + let mut ms8 = ms.clone(); + ms8.outputs.truncate(8); + assert_eq!(passthrough_split(&ms8, passthrough_elements(&ms8, None)), 1); + let pass8 = mesh_passthrough(&ms8); + assert!(pass8.contains("verts[128]"), "whole record:\n{pass8}"); + super::dxc::compile(&pass8, "main", "ms_6_6", &[]).unwrap_or_else(|e| panic!("{e}\n{pass8}")); +} + /// DXC optimisation levels on a saved HLSL file (`XCL_TIME_HLSL=path`, profile in `XCL_TIME_PROFILE`). #[test] #[ignore] From d42b820efd6369488ef1bcf4af3c8fa33937caea Mon Sep 17 00:00:00 2001 From: ryosen0 Date: Tue, 24 Nov 2026 05:13:10 +0300 Subject: [PATCH 2/7] Recompiler: the pass-through layout is chosen by the vertex budget alone The emitter picked the whole-record layout from passthrough_split == 1, but that count is the dispatch's: a record of 80 vertices with 22 float4 rows does not fit the mesh output budget as a whole (DXC accounts 96 vertices, 33792 B > 32768) while its single triangle's compact copies fit one group, so the count is 1 and verts[80] was declared and refused. The declaration now follows the vertex budget independently of the group count; the compact form covers every record over the budget, including the one-group ones. mesh_layout_independent_of_group_count covers the asymmetric limits (80 vertices, 1 triangle, 22 rows): compact declaration, count 1, DXC accepts. Found in review by shdann (PR #304). Assisted-by: DSH (GPT-6.2 Astra) --- crates/xcl/src/gpu/recomp/emit.rs | 16 ++++++++--- crates/xcl/src/gpu/recomp/tests.rs | 43 ++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 4 deletions(-) diff --git a/crates/xcl/src/gpu/recomp/emit.rs b/crates/xcl/src/gpu/recomp/emit.rs index ae4e82fda4..093c81cef6 100644 --- a/crates/xcl/src/gpu/recomp/emit.rs +++ b/crates/xcl/src/gpu/recomp/emit.rs @@ -4838,8 +4838,13 @@ pub fn mesh_passthrough_for(ms: &MsInterface, ps_inputs: Option<&[SigElement]>) let _ = writeln!(fill, " o.p{k} = {v};"); } let tri_base = MESH_HEADER + mv as usize * vd; + // The declaration is chosen by the vertex budget, the dispatch group count separately: + // a record over the budget in whole vertices can still fit one compact group (its few + // triangles' own copies fit) – declaring verts[mv] there is refused by DXC (80 vertices + // with 22 float4 rows are accounted as 96: 33792 B > 32768). + let cap = passthrough_max_vertices(16 * (1 + elems.len() as u32)); let split = passthrough_split(ms, elems.len()); - if split == 1 { + if mv <= cap { let _ = writeln!( h, "[outputtopology(\"triangle\")]\n[numthreads({t}, 1, 1)]\nvoid main(uint gi : SV_GroupIndex, uint3 gid : SV_GroupID, out vertices XclVert verts[{mv}], out indices uint3 tris[{mp}])\n{{" @@ -4893,9 +4898,12 @@ fn passthrough_max_vertices(vbytes: u32) -> u32 { (MESH_OUTPUT_BYTES / vbytes).min(256) / 32 * 32 } -/// Mesh groups the pass-through spends on one record: 1 when the record's vertices fit the -/// mesh output limit as they are, else the triangles are split into runs whose own vertex -/// copies (3 per triangle) fit. `elements`: output elements besides SV_Position. +/// Mesh groups the pass-through spends on one record: the record's vertices fit the mesh +/// output limit as they are, or the triangles go out in runs whose own vertex copies (3 per +/// triangle) fit – whichever costs fewer groups. The layout choice in `mesh_passthrough_for` +/// is by the vertex budget alone; this count is the dispatch's, and one compact group can +/// carry a record whose whole declaration would be refused. `elements`: output elements +/// besides SV_Position. pub fn passthrough_split(ms: &MsInterface, elements: usize) -> u32 { let vbytes = 16 * (1 + elements as u32); let cap = passthrough_max_vertices(vbytes); diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs index 983fe4c4ca..df80a0faf3 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -2955,6 +2955,49 @@ fn mesh_split_accounts_vertex_chunks() { super::dxc::compile(&pass8, "main", "ms_6_6", &[]).unwrap_or_else(|e| panic!("{e}\n{pass8}")); } +/// The layout and the dispatch count are chosen separately: a record of 80 vertices with 22 +/// float4 rows does not fit the mesh output budget as a whole (DXC accounts 96 vertices: +/// 33792 B > 32768 B), but its single triangle's copies fit one compact group. The pass- +/// through must then declare the compact layout while `passthrough_split` returns 1, and DXC +/// accepts it (the whole-record declaration is refused). +#[test] +fn mesh_layout_independent_of_group_count() { + use super::emit::{mesh_passthrough, passthrough_elements, passthrough_split}; + use super::MsInterface; + if super::dxc::location().is_err() { + println!("no DXC – skipping"); + runner::skip_test("no DXC"); + return; + } + let sig = |name: &str, sv: u32, reg: u32, index: u32| crate::gpu::dxbc::SigElement { + name: name.into(), + index, + system_value: sv, + component_type: 3, + register: reg, + mask: 0xF, + rw_mask: 0xF, + stream: 0, + }; + let mut outputs = vec![sig("SV_Position", 1, 0, 0)]; + for k in 0..20u32 { + outputs.push(sig("TEXCOORD", 0, k + 1, k)); + } + outputs.push(sig("SV_ClipDistance", 2, 21, 0)); + // 22 rows * 16 B = 352 B/vertex: 64 accounted vertices fit; 80 declared do not + let ms = MsInterface { outputs, threads: 128, max_vertices: 80, max_primitives: 1 }; + let elems = passthrough_elements(&ms, None); + assert_eq!(elems, 21); + // one triangle: its 3 copies fit one compact group, so the dispatch count is 1 + assert_eq!(passthrough_split(&ms, elems), 1, "one triangle fits one compact group"); + let pass = mesh_passthrough(&ms); + assert!(pass.contains("verts[3]"), "the compact layout is chosen, not verts[80]:\n{pass}"); + super::dxc::compile(&pass, "main", "ms_6_6", &[]).unwrap_or_else(|e| panic!("{e}\n{pass}")); + // the whole-record form is what DXC refuses at this width (80 accounted as 96) + let refused = mesh_passthrough(&MsInterface { max_vertices: 80, max_primitives: 1, ..ms.clone() }); + assert!(refused.contains("verts[3]"), "the compact form stays:\n{refused}"); +} + /// DXC optimisation levels on a saved HLSL file (`XCL_TIME_HLSL=path`, profile in `XCL_TIME_PROFILE`). #[test] #[ignore] From 2915f227936deaa33050692712884da7585b8424 Mon Sep 17 00:00:00 2001 From: ryosen0 Date: Tue, 24 Nov 2026 06:38:15 +0300 Subject: [PATCH 3/7] Recompiler: the layout test checks the whole-record refusal it names Review nits (Parakeet-Wheat): mesh_layout_independent_of_group_count ended with a block that rebuilt the same MsInterface and asserted verts[3] again; it now compiles the whole-record declaration (verts[80], accounted as 96: 33792 B) and asserts DXC refuses it. passthrough_split's doc claimed a comparison of the two forms it does not make; the doc now states what it does. Tests only plus a doc line; no behaviour change. Assisted-by: DSH (GPT-6.2 Astra) --- crates/xcl/src/gpu/recomp/emit.rs | 7 +++---- crates/xcl/src/gpu/recomp/tests.rs | 8 +++++--- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/crates/xcl/src/gpu/recomp/emit.rs b/crates/xcl/src/gpu/recomp/emit.rs index 093c81cef6..b5c39b4551 100644 --- a/crates/xcl/src/gpu/recomp/emit.rs +++ b/crates/xcl/src/gpu/recomp/emit.rs @@ -4900,10 +4900,9 @@ fn passthrough_max_vertices(vbytes: u32) -> u32 { /// Mesh groups the pass-through spends on one record: the record's vertices fit the mesh /// output limit as they are, or the triangles go out in runs whose own vertex copies (3 per -/// triangle) fit – whichever costs fewer groups. The layout choice in `mesh_passthrough_for` -/// is by the vertex budget alone; this count is the dispatch's, and one compact group can -/// carry a record whose whole declaration would be refused. `elements`: output elements -/// besides SV_Position. +/// triangle) fit. The layout choice in `mesh_passthrough_for` is by the vertex budget alone; +/// this count is the dispatch's, and one compact group can carry a record whose whole +/// declaration would be refused. `elements`: output elements besides SV_Position. pub fn passthrough_split(ms: &MsInterface, elements: usize) -> u32 { let vbytes = 16 * (1 + elements as u32); let cap = passthrough_max_vertices(vbytes); diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs index df80a0faf3..5e572d9bc6 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -2993,9 +2993,11 @@ fn mesh_layout_independent_of_group_count() { let pass = mesh_passthrough(&ms); assert!(pass.contains("verts[3]"), "the compact layout is chosen, not verts[80]:\n{pass}"); super::dxc::compile(&pass, "main", "ms_6_6", &[]).unwrap_or_else(|e| panic!("{e}\n{pass}")); - // the whole-record form is what DXC refuses at this width (80 accounted as 96) - let refused = mesh_passthrough(&MsInterface { max_vertices: 80, max_primitives: 1, ..ms.clone() }); - assert!(refused.contains("verts[3]"), "the compact form stays:\n{refused}"); + // the whole-record form is what DXC refuses at this width: the same record declared whole + // (verts[80], accounted as 96: 33792 B) does not validate + let whole = pass.replace("verts[3]", "verts[80]"); + assert!(whole.contains("verts[80]"), "the whole-record declaration for the refusal check:\n{whole}"); + assert!(super::dxc::compile(&whole, "main", "ms_6_6", &[]).is_err(), "DXC refuses 96 accounted vertices of 352 B"); } /// DXC optimisation levels on a saved HLSL file (`XCL_TIME_HLSL=path`, profile in `XCL_TIME_PROFILE`). From 70f91859a82bfb1aa2e35399203af90d104cb87c Mon Sep 17 00:00:00 2001 From: ryosen0 Date: Tue, 24 Nov 2026 04:20:29 +0300 Subject: [PATCH 4/7] D3D12.X: a console MS in the AS slot is translated as the mesh shader it is A retail title's minimap pipelines carry their console MS container in the AS subobject with no MS subobject. They were refused: translated, the title had rendered nothing from then on. That black screen was the translated mesh shaders failing their ms_6_6 output budget, fixed by the DXC vertex-chunk budget (#304); the slot itself was never the problem. With the budget in place the same shaders translate and the title keeps rendering. mesh_shader_slots moves the container to the MS slot (and it is not an AS any more); a real AS, an MS already present, or a PC shader are left as they are. Unit test covers all five cases. Assisted-by: DSH (GPT-6.2 Astra) --- crates/xcl/src/hle/d3d12x/real.rs | 59 +++++++++++++++++++++++++------ 1 file changed, 48 insertions(+), 11 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index 9168a5d667..6eeef7e249 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -4265,6 +4265,52 @@ unsafe fn finish_two_pass(this: *mut c_void, stream: &StreamOut, tp: crate::gpu: hr } +/// The mesh and amplification shaders of a stream, as XCL translates them: a console MS +/// container in the AS slot with no MS subobject (a retail game's minimap pipelines) is the +/// mesh shader it says it is. It used to be refused – translated, the title rendered nothing +/// from then on, which was the translated mesh shaders failing their output budget (ms_6_6), +/// not the slot. With the chunk budget fixed they translate and the title keeps rendering. +/// Returns (MS to translate, AS left): the moved shader is not an AS any more. +fn mesh_shader_slots<'a>(ms_slot: Option<&'a [u8]>, as_slot: Option<&'a [u8]>) -> (Option<&'a [u8]>, Option<&'a [u8]>) { + use crate::gpu::recomp::pso::is_console; + match (ms_slot, as_slot) { + (None, Some(a)) if is_console(a) && crate::gpu::xbox::parse(a).is_some_and(|sh| sh.stage == "MS") => { + crate::xlog_info!("D3D12.X: mesh pipeline with its MS {:016x} in the AS slot – translated as MS", crate::gpu::shader::fnv64(a)); + (Some(a), None) + } + (ms, a) => (ms, a), + } +} + +#[cfg(test)] +mod mesh_slot_tests { + use super::mesh_shader_slots; + use crate::gpu::xbox::tests::{sample, fssm, CODE}; + + /// A console MS container alone in the AS slot becomes the MS; a real AS (console or PC), + /// an MS already present, or a PC shader in the AS slot are left as they are. + #[test] + fn a_console_ms_in_the_as_slot_is_the_mesh_shader() { + let ms = sample(13, &[fssm(&CODE, &[(8, 13)])]); + let as_ = sample(14, &[fssm(&CODE, &[(8, 14)])]); + let pc = b"DXBC-not-a-console-container".to_vec(); + // MS in the AS slot, no MS: moved, and not an AS any more + let (m, a) = mesh_shader_slots(None, Some(&ms)); + assert!(m.is_some_and(|m| std::ptr::eq(m, ms.as_slice())) && a.is_none()); + // an MS present: both slots untouched, whatever the AS holds + let (m, a) = mesh_shader_slots(Some(&ms), Some(&as_)); + assert!(m.is_some_and(|m| std::ptr::eq(m, ms.as_slice())) && a.is_some_and(|a| std::ptr::eq(a, as_.as_slice()))); + // a console AS (not an MS) stays in the AS slot + let (m, a) = mesh_shader_slots(None, Some(&as_)); + assert!(m.is_none() && a.is_some_and(|a| std::ptr::eq(a, as_.as_slice()))); + // a PC shader in the AS slot stays too + let (m, a) = mesh_shader_slots(None, Some(&pc)); + assert!(m.is_none() && a.is_some()); + // nothing: nothing + assert!(mesh_shader_slots(None, None) == (None, None)); + } +} + /// CreatePipelineState with a console MS: Some(hr) when it was a console mesh pipeline /// (translated, or E_INVALIDARG – console bytecode must not reach the driver). unsafe fn translated_mesh_stream(this: *mut c_void, desc: *const c_void, riid: *const GUID, ppv: *mut *mut c_void) -> Option { @@ -4279,17 +4325,8 @@ unsafe fn translated_mesh_stream(this: *mut c_void, desc: *const c_void, riid: * let subs = stream_subobjects(std::slice::from_raw_parts(base as *const u8, size))?; let as_slot = subs.iter().find(|s| s.0 == D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_AS.0 as u32).and_then(|s| payload_shader(s.1)); let ms_slot = subs.iter().find(|s| s.0 == D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_MS.0 as u32).and_then(|s| payload_shader(s.1)); - // A console MS container in the AS slot and no MS subobject (a retail game's minimap - // pipelines): refused. Translated as the mesh shader they are, the title rendered nothing - // from then on (a black screen every run); refused, it goes on to its menus. What it - // expects of them is not known yet. - if let (None, Some(a)) = (ms_slot, as_slot) { - if is_console(a) && crate::gpu::xbox::parse(a).is_some_and(|sh| sh.stage == "MS") { - note_translation_error(format!("mesh pipeline with its MS {:016x} in the AS slot – refused", crate::gpu::shader::fnv64(a))); - return Some(E_INVALIDARG); - } - } - let ms = ms_slot?; + let (ms, as_slot) = mesh_shader_slots(ms_slot, as_slot); + let Some(ms) = ms else { return None }; if !is_console(ms) { return None; } From 48c1517a0c48a5a9cf890c67c5b3a61438e70cd4 Mon Sep 17 00:00:00 2001 From: ryosen0 Date: Tue, 24 Nov 2026 06:25:22 +0300 Subject: [PATCH 5/7] D3D12.X: a moved AS-slot MS is translated when first bound, not in the call The title creates tens of thousands of console-MS-in-AS-slot pipelines during boot and uses few. Translating each in the cached-only attempt ran on the title's thread: on RTX 3060 Ti the intro's first bright frame came ~5 s later than main (frame 1100 vs 980 at XCL_FRAME_DUMP=120), and on RX 9070 XT (wave64, the split path) Chrysana's 60 s captures stayed black throughout with 57163 pipelines queued. The attempt now defers a moved MS like a console compute pipeline (dxc::defer under pso_async::lazy): go_background hands the title a stand-in and the worker or the first bind translates. The detection log moved to a sparse counter at the build (first 5 + every 1000th); it used to fire twice per deferred pipeline (2930 lines in 120 s). Assisted-by: DSH (GPT-6.2 Astra) --- crates/xcl/src/hle/d3d12x/real.rs | 41 ++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index 6eeef7e249..17bd355f9c 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -138,6 +138,9 @@ static FALLBACK_PSO: AtomicU64 = AtomicU64::new(0); static FAILED_PSO: AtomicU64 = AtomicU64::new(0); static FALLBACK_RS: AtomicU64 = AtomicU64::new(0); static TRANSLATED_PSO: AtomicU64 = AtomicU64::new(0); +/// console MS containers moved from the AS slot and translated (logged sparsely: a title +/// creates tens of thousands of these pipelines during boot) +static AS_SLOT_MS: AtomicU64 = AtomicU64::new(0); static TRANSLATE_ERRORS: Mutex> = Mutex::new(Vec::new()); /// XCL shader ABI root signature on the backend device (raw, kept for the process lifetime) static ABI_RS: AtomicUsize = AtomicUsize::new(0); @@ -4274,10 +4277,7 @@ unsafe fn finish_two_pass(this: *mut c_void, stream: &StreamOut, tp: crate::gpu: fn mesh_shader_slots<'a>(ms_slot: Option<&'a [u8]>, as_slot: Option<&'a [u8]>) -> (Option<&'a [u8]>, Option<&'a [u8]>) { use crate::gpu::recomp::pso::is_console; match (ms_slot, as_slot) { - (None, Some(a)) if is_console(a) && crate::gpu::xbox::parse(a).is_some_and(|sh| sh.stage == "MS") => { - crate::xlog_info!("D3D12.X: mesh pipeline with its MS {:016x} in the AS slot – translated as MS", crate::gpu::shader::fnv64(a)); - (Some(a), None) - } + (None, Some(a)) if is_console(a) && crate::gpu::xbox::parse(a).is_some_and(|sh| sh.stage == "MS") => (Some(a), None), (ms, a) => (ms, a), } } @@ -4309,6 +4309,21 @@ mod mesh_slot_tests { // nothing: nothing assert!(mesh_shader_slots(None, None) == (None, None)); } + + /// The cached-only attempt defers a moved MS like a console compute pipeline: `defer` + /// marks the attempt missed, so `dev_create_pipeline_state` takes the background path + /// (stand-in now, translation on the worker / first bind) instead of translating on the + /// title's thread – tens of thousands of these pipelines during boot delayed the intro + /// by seconds on NVIDIA and past a minute on AMD wave64. Outside the attempt (the + /// worker's build) nothing is deferred. + #[test] + fn a_moved_ms_defers_in_the_attempt_only() { + let (_, missed) = crate::gpu::recomp::dxc::cached_only(|| { + assert!(crate::gpu::recomp::dxc::defer(), "inside the attempt: deferred"); + }); + assert!(missed, "the attempt is missed: the caller takes the background path"); + assert!(!crate::gpu::recomp::dxc::defer(), "outside: the build translates"); + } } /// CreatePipelineState with a console MS: Some(hr) when it was a console mesh pipeline @@ -4325,6 +4340,9 @@ unsafe fn translated_mesh_stream(this: *mut c_void, desc: *const c_void, riid: * let subs = stream_subobjects(std::slice::from_raw_parts(base as *const u8, size))?; let as_slot = subs.iter().find(|s| s.0 == D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_AS.0 as u32).and_then(|s| payload_shader(s.1)); let ms_slot = subs.iter().find(|s| s.0 == D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_MS.0 as u32).and_then(|s| payload_shader(s.1)); + // a console MS in the AS slot with no MS subobject: moved, and translated when first + // bound (below), not on the title's thread for each of tens of thousands + let moved = ms_slot.is_none(); let (ms, as_slot) = mesh_shader_slots(ms_slot, as_slot); let Some(ms) = ms else { return None }; if !is_console(ms) { @@ -4339,6 +4357,21 @@ unsafe fn translated_mesh_stream(this: *mut c_void, desc: *const c_void, riid: * )); return Some(E_INVALIDARG); } + // In the cached-only attempt (non-blocking creation) a moved MS is not translated here: + // the title creates tens of thousands of these during boot and uses few; translating each + // on the calling thread delayed the intro by seconds (NVIDIA) or past a minute (AMD wave64, + // the split path). Made when first bound (`pso_async::lazy`), like a console compute + // pipeline: the attempt defers, `go_background` hands the title a stand-in and the worker + // (or the bind) runs this path again outside cached-only. + if moved && super::pso_async::lazy() && crate::gpu::recomp::dxc::defer() { + return Some(E_INVALIDARG); + } + if moved { + let n = AS_SLOT_MS.fetch_add(1, Ordering::Relaxed); + if n < 5 || n % 1000 == 0 { + crate::xlog_info!("D3D12.X: mesh pipeline with its MS {:016x} in the AS slot – translated as MS (#{})", crate::gpu::shader::fnv64(ms), n + 1); + } + } let ps = subs.iter().find(|s| s.0 == D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_PS.0 as u32).and_then(|s| payload_shader(s.1)); let (tp, (ps, ps_samplers)) = match (console_mesh(ms, ps, wave32_only(this)), two_pass_ps(ps, graphics_wave32(this))) { (Ok(tp), Ok(ps)) => (tp, ps), From 7654a2a4eaba17d8252b686149600721b76489b7 Mon Sep 17 00:00:00 2001 From: ryosen0 Date: Tue, 24 Nov 2026 07:27:07 +0300 Subject: [PATCH 6/7] D3D12.X: the stand-in of a moved AS-slot MS is a mesh pipeline fallback_stream decided 'mesh' by an MS subobject; the retail title's moved-MS streams have none (the console MS sits in the AS subobject), so the stand-in emptied the AS and kept a plain PS: a stream with neither a VS, CS nor MS, which the driver refuses (E_INVALIDARG). go_background then returned None and dev_create_pipeline_state built every such pipeline in the title's call, so the deferral never took effect (NishantParajuli on RTX 5060 Ti, Chrysana on RX 9070 XT: black frames, ~57k pipelines queued; thehollowedone's native fixture: D3D12 message 1022, no VS/CS/MS). The stand-in of such a stream retypes the AS subobject to MS with the dummy mesh shader and uses the mesh dummy's PS, as translated_mesh_stream reads the title's stream; a real AS is emptied as before. Test the_stand_in_of_a_moved_ms_is_a_mesh_pipeline fails without the change. Assisted-by: DSH (GPT-6.2 Astra) --- crates/xcl/src/hle/d3d12x/real.rs | 83 ++++++++++++++++++++++++++++++- 1 file changed, 81 insertions(+), 2 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index 17bd355f9c..1fdc98b961 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -4284,9 +4284,72 @@ fn mesh_shader_slots<'a>(ms_slot: Option<&'a [u8]>, as_slot: Option<&'a [u8]>) - #[cfg(test)] mod mesh_slot_tests { - use super::mesh_shader_slots; + use super::{console_ms_in_as_slot, fallback_stream, mesh_shader_slots}; use crate::gpu::xbox::tests::{sample, fssm, CODE}; + /// A stream subobject: its type, then the payload at 4 (align 4) or 8 (align 8). + fn sub(ty: u32, payload: &[u8]) -> Vec { + let (_, palign) = super::super::null::subobject_payload(ty).unwrap(); + let poff = if palign == 8 { 8 } else { 4 }; + let mut b = vec![0u8; (poff + payload.len() + 7) & !7]; + b[..4].copy_from_slice(&ty.to_le_bytes()); + b[poff..poff + payload.len()].copy_from_slice(payload); + b + } + + fn bytecode(s: &[u8]) -> Vec { + let mut b = Vec::new(); + b.extend_from_slice(&(s.as_ptr() as u64).to_le_bytes()); + b.extend_from_slice(&(s.len() as u64).to_le_bytes()); + b + } + + /// The stream the retail title's AS-slot MS pipelines have: a console MS container in the + /// AS subobject, no MS subobject, a PS and the graphics state (`mesh_shader_slots` moves it). + fn as_slot_ms_stream(ms: &[u8], ps: &[u8]) -> Vec { + let mut s = sub(0, &0u64.to_le_bytes()); // ROOT_SIGNATURE: null + s.extend_from_slice(&sub(24, &bytecode(ms))); + s.extend_from_slice(&sub(2, &bytecode(ps))); + s.extend_from_slice(&sub(8, &vec![0u8; 328])); // BLEND + s.extend_from_slice(&sub(9, &u32::MAX.to_le_bytes())); // SAMPLE_MASK + s.extend_from_slice(&sub(10, &vec![0u8; 44])); // RASTERIZER + s.extend_from_slice(&sub(11, &vec![0u8; 52])); // DEPTH_STENCIL + s.extend_from_slice(&sub(15, &vec![0u8; 36])); // RENDER_TARGET_FORMATS + s.extend_from_slice(&sub(17, &[1, 0, 0, 0, 0, 0, 0, 0])); // SAMPLE_DESC + s + } + + /// The stand-in for a console MS alone in the AS slot is a mesh pipeline: the subobject is + /// retyped to MS with the dummy MS (as `translated_mesh_stream` reads the title's stream), + /// the PS is the dummy PS. Left an empty AS and a PS, the stream had neither an MS nor a VS + /// and the driver refused it (E_INVALIDARG, NishantParajuli's and Chrysana's runs). + #[test] + fn the_stand_in_of_a_moved_ms_is_a_mesh_pipeline() { + let ms = sample(13, &[fssm(&CODE, &[(8, 13)])]); + let ps = b"DXBC-a-pixel-shader".to_vec(); + let stream = as_slot_ms_stream(&ms, &ps); + let desc = [stream.len(), stream.as_ptr() as usize]; + let subs = super::stream_subobjects(&stream).unwrap(); + assert!(unsafe { console_ms_in_as_slot(&subs) }); + let (buf, _) = unsafe { fallback_stream(desc.as_ptr() as *const u8) }.expect("a stand-in stream"); + let out = super::stream_subobjects(&buf).unwrap(); + assert!(!out.iter().any(|s| s.0 == 24), "no AS subobject left"); + let m = out.iter().find(|s| s.0 == 25).expect("an MS subobject"); + let dm = super::dummy_mesh().expect("DXC for the stand-in shaders"); + let got = unsafe { super::payload_shader(m.1) }.unwrap(); + assert_eq!(got, dm.0.as_slice(), "the dummy MS"); + let p = out.iter().find(|s| s.0 == 2).unwrap(); + assert_eq!(unsafe { super::payload_shader(p.1) }.unwrap(), dm.1.as_slice(), "the dummy PS"); + // a real AS with no MS is not moved, and its stand-in keeps the AS slot empty + let as_ = sample(14, &[fssm(&CODE, &[(8, 14)])]); + let stream = as_slot_ms_stream(&as_, &ps); + let desc = [stream.len(), stream.as_ptr() as usize]; + assert!(!unsafe { console_ms_in_as_slot(&super::stream_subobjects(&stream).unwrap()) }); + let (buf, _) = unsafe { fallback_stream(desc.as_ptr() as *const u8) }.unwrap(); + let out = super::stream_subobjects(&buf).unwrap(); + assert!(out.iter().any(|s| s.0 == 24) && !out.iter().any(|s| s.0 == 25)); + } + /// A console MS container alone in the AS slot becomes the MS; a real AS (console or PC), /// an MS already present, or a PC shader in the AS slot are left as they are. #[test] @@ -4483,6 +4546,13 @@ unsafe fn translated_gs_pso(this: *mut c_void, d: &D3D12_GRAPHICS_PIPELINE_STATE finish_two_pass(this, &out, tp, rs, "GS", riid, ppv) } +/// A console MS container in the stream's AS subobject and no MS subobject (`mesh_shader_slots`). +unsafe fn console_ms_in_as_slot(subs: &[(u32, &[u8])]) -> bool { + let shader = |ty: i32| subs.iter().find(|s| s.0 == ty as u32).and_then(|s| payload_shader(s.1)); + shader(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_MS.0).is_none() + && shader(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_AS.0).is_some_and(|a| crate::gpu::recomp::pso::is_console(a) && crate::gpu::xbox::parse(a).is_some_and(|sh| sh.stage == "MS")) +} + /// Rewrite a pipeline state stream with stand-in shaders. None if not possible. unsafe fn fallback_stream(desc: *const u8) -> Option<(Vec, [usize; 2])> { let size = *(desc as *const usize); @@ -4492,7 +4562,12 @@ unsafe fn fallback_stream(desc: *const u8) -> Option<(Vec, [usize; 2])> { } let mut buf = std::slice::from_raw_parts(base as *const u8, size).to_vec(); let d = dummy(); - let mesh = stream_subobjects(&buf).is_some_and(|subs| subs.iter().any(|s| s.0 == D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_MS.0 as u32)); + // A console MS alone in the AS slot is the stream's mesh shader (`mesh_shader_slots`), so its + // stand-in is a mesh pipeline too, with the dummy MS in that subobject as an MS. Left an AS + // without a shader, the stream had a PS and neither an MS nor a VS: the driver refused it + // (E_INVALIDARG) and every such pipeline was built in the title's call (NishantParajuli). + let ms_in_as = stream_subobjects(&buf).is_some_and(|subs| console_ms_in_as_slot(&subs)); + let mesh = ms_in_as || stream_subobjects(&buf).is_some_and(|subs| subs.iter().any(|s| s.0 == D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_MS.0 as u32)); let dm = if mesh { Some(dummy_mesh()?) } else { None }; let mut off = 0usize; while off + 8 <= size { @@ -4511,6 +4586,10 @@ unsafe fn fallback_stream(desc: *const u8) -> Option<(Vec, [usize; 2])> { 1 => put(&mut buf, &d.vs), 2 => put(&mut buf, dm.map_or(&d.ps[..], |m| &m.1[..])), 6 => put(&mut buf, &d.cs), + 24 if ms_in_as => { + buf[off..off + 4].copy_from_slice(&(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_MS.0 as u32).to_le_bytes()); + put(&mut buf, &dm?.0) + } 3 | 4 | 5 | 24 => put(&mut buf, &[]), 25 => put(&mut buf, &dm?.0), 12 | 19 => buf[at..at + 16].fill(0), // input layout, cached PSO From 6bd8f641040071b8a388c0ec3053c2b1483ddcdf Mon Sep 17 00:00:00 2001 From: ryosen0 Date: Tue, 24 Nov 2026 07:44:16 +0300 Subject: [PATCH 7/7] D3D12.X: the stand-in of a stream has the PC's target formats The retail title's depth-only mesh pipelines use console-only formats (D16_UNORM_S8_UINT). allback_stream copied the title's formats as they were, so the driver refused the stand-in (E_INVALIDARG). go_background then returned None and dev_create_pipeline_state built every such pipeline in the title's call, so the deferral never took effect (NishantParajuli's follow-up, reproduced natively by thehollowedone). The stand-in's RENDER_TARGET_FORMATS and DEPTH_STENCIL_FORMAT now go through device_x::pc_target_format, as the real pipeline's do. Test he_stand_in_of_a_stream_has_the_pcs_target_formats fails without the change. Assisted-by: DSH (GPT-6.2 Astra) --- crates/xcl/src/hle/d3d12x/real.rs | 32 +++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index 1fdc98b961..5ea0f812dc 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -4350,6 +4350,28 @@ mod mesh_slot_tests { assert!(out.iter().any(|s| s.0 == 24) && !out.iter().any(|s| s.0 == 25)); } + /// The stand-in's target formats are the PC's, as the real pipeline's: the console's + /// D16_UNORM_S8_UINT (118) is D24_UNORM_S8_UINT (45) and a B4G4R4A4 target (115) is + /// R8G8B8A8 (28). With the console's the driver refuses the stand-in (the title's depth-only + /// mesh pipelines; NishantParajuli's follow-up, reproduced natively by thehollowedone). + #[test] + fn the_stand_in_of_a_stream_has_the_pcs_target_formats() { + let mut s = sub(15, &{ + let mut p = Vec::new(); + for f in [115u32, 28, 0, 0, 0, 0, 0, 0, 2] { + p.extend_from_slice(&f.to_le_bytes()); + } + p + }); + s.extend_from_slice(&sub(16, &118u32.to_le_bytes())); + let desc = [s.len(), s.as_ptr() as usize]; + let (out, _) = unsafe { fallback_stream(desc.as_ptr() as *const u8) }.expect("a stand-in stream"); + let subs = super::stream_subobjects(&out).unwrap(); + let word = |p: &[u8], k: usize| u32::from_le_bytes(p[4 * k..4 * k + 4].try_into().unwrap()); + assert_eq!((subs[0].0, word(subs[0].1, 0), word(subs[0].1, 1), word(subs[0].1, 8)), (15, 28, 28, 2)); + assert_eq!((subs[1].0, word(subs[1].1, 0)), (16, 45)); + } + /// A console MS container alone in the AS slot becomes the MS; a real AS (console or PC), /// an MS already present, or a PC shader in the AS slot are left as they are. #[test] @@ -4592,6 +4614,16 @@ unsafe fn fallback_stream(desc: *const u8) -> Option<(Vec, [usize; 2])> { } 3 | 4 | 5 | 24 => put(&mut buf, &[]), 25 => put(&mut buf, &dm?.0), + // RENDER_TARGET_FORMATS, DEPTH_STENCIL_FORMAT: the console's formats as the PC's, as + // the real pipeline gets them (`translated_mesh_stream`) – with a console-only one + // (a retail game's depth-only mesh pipelines: D16_UNORM_S8_UINT) the driver refuses + // the stand-in (E_INVALIDARG) and the pipeline is built in the title's call + 15 | 16 => { + for k in 0..if ty == 15 { 8 } else { 1 } { + let f = super::device_x::pc_target_format(u32::from_le_bytes(buf[at + 4 * k..at + 4 * k + 4].try_into().ok()?)); + buf[at + 4 * k..at + 4 * k + 4].copy_from_slice(&f.to_le_bytes()); + } + } 12 | 19 => buf[at..at + 16].fill(0), // input layout, cached PSO 7 => buf[at..at + 32].fill(0), // stream output _ => {}