From e971efc3aa4d75d05f152ce9b42d3fd45b0df6b0 Mon Sep 17 00:00:00 2001 From: NathansDomainSolutions <184714143+NathansDomainSolutions@users.noreply.github.com> Date: Tue, 24 Nov 2026 23:57:40 -0900 Subject: [PATCH] d3d12: give the pixel stage a copy of guest tables the mesh layout rewrites A mesh pipeline's reflected root rewrites some guest tables into the mesh program's FSSM layout and makes them mesh-only. When the pixel stage also reads such a table, the pipeline was refused (E_NOTIMPL, mesh_table.rs pixel-access check), so the draw was lost. Append a pixel-only copy of the original table instead and bind the guest's table to both parameters at draw time. Co-Authored-By: Claude Opus 5.5 (cherry picked from commit 1b04aed9c3132cdff160b2ebba9122b1affd349c) (cherry picked from commit 1b88a230760e0a382580a022bc56e515984988c2) --- .../d3d12/src/commands/compute_bindings.rs | 8 +++- runtime/d3d12/src/device/compute_table.rs | 5 ++ runtime/d3d12/src/device/mesh_table.rs | 46 ++++++++++++++++--- 3 files changed, 52 insertions(+), 7 deletions(-) diff --git a/runtime/d3d12/src/commands/compute_bindings.rs b/runtime/d3d12/src/commands/compute_bindings.rs index ccc525f6..3253f8f0 100644 --- a/runtime/d3d12/src/commands/compute_bindings.rs +++ b/runtime/d3d12/src/commands/compute_bindings.rs @@ -265,7 +265,7 @@ impl Bindings { let mut storage = [None; 64]; // no root signature can consume more than 64 DWORDs let bases = storage.get_mut(..table.lengths.len()).ok_or(E_INVALIDARG)?; for (index, &length) in table.lengths.iter().enumerate().filter(|(_, len)| **len != 0) { - let slot = index as u32; + let slot = table.mirrors.iter().find(|(host, _)| *host as usize == index).map_or(index as u32, |&(_, guest)| guest); let bound = if COMPUTE { let Some(Argument::Table(table)) = self.compute_args.get(&slot) else { return Err(E_INVALIDARG.into()); }; table @@ -308,6 +308,12 @@ impl Bindings { let base = bases.get(slot as usize).copied().flatten().unwrap_or(bound.native); list.SetGraphicsRootDescriptorTable(slot, base); } + for &(host, guest) in &table.mirrors { + if let Some(bound) = self.graphics_tables.get(&guest) { + let base = bases.get(host as usize).copied().flatten().unwrap_or(bound.native); + list.SetGraphicsRootDescriptorTable(host, base); + } + } if let Some((slot, address)) = index_map { list.SetGraphicsRootShaderResourceView(slot, address); static IDENTITY: std::sync::Once = std::sync::Once::new(); diff --git a/runtime/d3d12/src/device/compute_table.rs b/runtime/d3d12/src/device/compute_table.rs index 6fddfed6..2c56cbf8 100644 --- a/runtime/d3d12/src/device/compute_table.rs +++ b/runtime/d3d12/src/device/compute_table.rs @@ -13,6 +13,9 @@ pub(crate) struct ComputeTable { pub(crate) lengths: Vec, pub(crate) accessed_views: Vec<(usize, std::ops::Range)>, pub(crate) index_map: Option, + /// LOCAL: appended host parameters that bind the same guest table + /// (host index, guest slot), e.g. a pixel-only copy of a mesh-rewritten table. + pub(crate) mirrors: Vec<(u32, u32)>, } #[derive(Clone, Copy)] @@ -268,6 +271,7 @@ pub(super) fn reflected( lengths: lengths.into(), accessed_views, index_map, + mirrors: Vec::new(), }))) } } @@ -368,6 +372,7 @@ fn append_corroborated_ranges( lengths, accessed_views, index_map: None, + mirrors: Vec::new(), }))) } } diff --git a/runtime/d3d12/src/device/mesh_table.rs b/runtime/d3d12/src/device/mesh_table.rs index 3d953e8d..c0a94ccb 100644 --- a/runtime/d3d12/src/device/mesh_table.rs +++ b/runtime/d3d12/src/device/mesh_table.rs @@ -37,8 +37,11 @@ pub(super) fn reflected( accessed_views.push((binding.parameter, slot..end)); } } + // Guest tables the pixel stage reads but the mesh layout rewrote. Each gets + // an appended pixel-only copy of its original ranges (see `mirrors`). + let mut mirrored = Vec::new(); if let Some(pixel) = pixel { - collect_pixel_accesses(&root, &layout, pixel, &mut lengths, &mut accessed_views)?; + collect_pixel_accesses(&root, &layout, pixel, &mut lengths, &mut accessed_views, &mut mirrored)?; } let bytes = original.serialized(); // SAFETY: the deserializer owns the source pointers throughout serialization. @@ -58,6 +61,7 @@ pub(super) fn reflected( return Err(E_INVALIDARG.into()); } let mut params = std::slice::from_raw_parts(source.pParameters, root.params.len()).to_vec(); + let originals = params.clone(); let mut range_storage = vec![Vec::new(); params.len()]; for (index, change) in layout.replacements { let parameter = &mut params[index]; @@ -111,6 +115,20 @@ pub(super) fn reflected( } } } + let mut mirrors = Vec::new(); + for &guest in &mirrored { + let mut copy = originals[guest]; + if copy.ParameterType != D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE { + return Err(E_NOTIMPL.into()); + } + copy.ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL; + mirrors.push((params.len() as u32, guest as u32)); + params.push(copy); + } + if lengths.len() < params.len() { + lengths.resize(params.len(), 0); + } + desc.Anonymous.Desc_1_1.NumParameters = params.len() as u32; desc.Anonymous.Desc_1_1.pParameters = params.as_ptr(); let mut blob = None; D3D12SerializeVersionedRootSignature(&desc, &mut blob, None)?; @@ -124,6 +142,7 @@ pub(super) fn reflected( lengths, accessed_views, index_map: None, + mirrors, }))) } } @@ -132,8 +151,9 @@ fn collect_pixel_accesses( root: &RootSig, mesh: &MeshLayout, pixel: &[u8], - lengths: &mut [u32], + lengths: &mut Vec, views: &mut Vec<(usize, Range)>, + mirrored: &mut Vec, ) -> Result<()> { for [ty, space, low, high] in shader_resources(pixel).ok_or(E_NOTIMPL)? { let kind = match ty { @@ -170,14 +190,28 @@ fn collect_pixel_accesses( if from >= to { continue; } - if rewritten { - return Err(E_NOTIMPL.into()); + // A rewritten table is mesh-only; the pixel stage reads + // its appended original copy at `target`. + let target = if rewritten { + let position = match mirrored.iter().position(|&m| m == index) { + Some(position) => position, + None => { + mirrored.push(index); + mirrored.len() - 1 + } + }; + root.params.len() + position + } else { + index + }; + if lengths.len() <= target { + lengths.resize(target + 1, 0); } let start = u32::try_from(start + (from - base)).map_err(|_| E_NOTIMPL)?; let end = start.checked_add((to - from) as u32).ok_or(E_NOTIMPL)?; - lengths[index] = lengths[index].max(end); + lengths[target] = lengths[target].max(end); if kind != 2 { - views.push((index, start..end)); + views.push((target, start..end)); } } }