Skip to content
28 changes: 22 additions & 6 deletions crates/xcl/src/gpu/recomp/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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{{"
Expand Down Expand Up @@ -4885,15 +4890,26 @@ 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;

/// 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.
/// 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: 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. 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);
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)
}

Expand Down
101 changes: 101 additions & 0 deletions crates/xcl/src/gpu/recomp/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2899,6 +2899,107 @@ 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}"));
}

/// 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: 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`).
#[test]
#[ignore]
Expand Down
Loading
Loading