Repository navigation
[SPIR-V] Fix nested and inherited structs in mesh shader outputs - #8982
Abir Deol (apollo-2006) wants to merge 3 commits into
Conversation
A mesh shader output struct can contain another struct whose fields
carry the semantics:
struct VertexAttributes { float3 Normal : NORMAL0; };
struct VertexOut {
float4 Pos : SV_Position;
VertexAttributes attributes;
};
The stage variables are created for the innermost fields, but writes
to the output looked up the semantic of the nested struct field
itself, which has none, and crashed in assignToMSOutAttribute on
`assert(semanticInfo.isValid())`. This happened for whole-element
writes (`verts[i] = v`), writes to the nested struct
(`verts[i].attributes = a`) and writes to its fields
(`verts[i].attributes.Normal = n`). A struct field with a semantic of
its own (`VertexAttributes attributes : ATTR0`) crashed the same way,
since its fields inherit that semantic.
Base classes had related problems, even without nesting: a
whole-element write skipped the inherited fields, so they were never
written, and a write to an inherited field (`verts[i].Tangent = t`)
kept the base class index in the access chain, which made the SPIR-V
invalid.
Treat any struct-typed member of a mesh output as a container:
descend through it when collecting indices, skip base class indices,
and when a struct is assigned, assign its base classes and fields one
by one. Leaf fields use the semantic their stage variable was created
with, which may be inherited from the enclosing struct.
Fixes microsoft#7340
Fixes microsoft#8475
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
Stage-variable lookup remains incorrect for repeated nested types and inherited clip/cull-distance semantics.
Review effort: Balanced
Findings: 1
Open (2)
What changed in this PR
Fixes SPIR-V mesh shader output handling for nested and inherited structures.
Changes:
- Recursively assigns nested and inherited output fields.
- Resolves semantics used during stage-variable creation.
- Adds regression tests and release notes.
| File | Description |
|---|---|
SpirvEmitter.cpp |
Implements recursive mesh-output assignment and index handling. |
SpirvEmitter.h |
Declares the recursive assignment helper. |
DeclResultIdMapper.h |
Adds effective-semantic lookup. |
meshshading.ext.nested.struct.mesh.hlsl |
Tests nested output structures. |
meshshading.ext.inherited.struct.mesh.hlsl |
Tests inherited output fields. |
docs/ReleaseNotes.md |
Documents the user-visible fixes. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
|
@microsoft-github-policy-service agree |
The stage variables were looked up by the field decl alone, so a struct type used twice in a mesh shader output (Pair p0 : A0; Pair p1 : B0;) sent the writes to p0 to the variables of p1. The same happened when the vertices and the primitives shared a struct type. Record the path of fields each mesh output variable was created for, and whether it is per-primitive, and use that for the lookup.
There was a problem hiding this comment.
🟡 Changes recommended
Mesh-output mappings persist across entry points, causing functions that share an output struct to reference stage variables absent from their interfaces and produce invalid SPIR-V.
2 open issues
1 resolved since last review
Review effort: Balanced
Diego Novillo (dnovillo)
left a comment
There was a problem hiding this comment.
LGTM. Thanks for the extra tests.
|
hey Diego Novillo (@dnovillo), thanks again for the review! is there anything else this needs before it can land, or should i ping someone for a second approval? |
You'll need to have the approval of a maintainer (I don't yet have write access). Gregory Roth (@pow2clk) could you please help with review? Thanks! |


Fixes #7340 and #8475.
When a mesh shader's output struct contains another struct whose fields carry the semantics:
the SPIR-V backend crashed on any write to the output. The stage variables are created for the innermost fields (
POSITION0,NORMAL0), butassignToMSOutAttributelooked up the semantic of theattributesfield itself, which has none, and failedassert(semanticInfo.isValid()). In release builds the code goes on with the empty semantic, which matches the crash insideassignToMSOutAttributein #8475's stack.It wasn't only the whole-element write from the issues. All of these crashed:
verts[i] = v;verts[i].attributes = a;verts[i].attributes.Normal = n;(herecollectArrayStructIndicesstopped atattributesand turnedNormalinto a trailing index, which was then treated as a vector component)out primitivesVertexAttributes attributes : ATTR0, whose fields inheritATTR0/ATTR1While testing, I also found two problems with base classes in mesh outputs that happen without any nesting, on current main:
verts[i] = vwhereVertexOuthas a base class never wrote the inherited fields. The loop only walkedfields(), so their stage variables were never stored to.verts[i].Tangent = t, withTangentinherited, kept the base class index in the access chain, and the SPIR-V failed validation.I fixed these here as well, since the nested fix would have been incomplete for nested structs with bases. If you'd prefer them as a separate PR, I'm happy to split them out.
The fix:
collectArrayStructIndices, keep descending through struct-typed mesh output members, and don't push base class indices for mesh outputs (they're split into one stage variable per field, so a base class has no index of its own).assignToMSOutStructassigns a struct value by its base classes, then its fields, recursing into nested structs. It's the same layoutwriteBackOutputStreamuses for geometry shader outputs. The existing whole-element path now uses it too.DeclResultIdMapper::getStageVarCreationSemantic), so fields that inherit a struct's semantic work.Testing:
meshshading.ext.nested.struct.mesh.hlslcovers whole-element, nested member, nested field, nested component, per-primitive, and inherited-semantic writes. Newmeshshading.ext.inherited.struct.mesh.hlslcovers base classes at the top level and inside a nested struct, both whole-element and direct field writes. Both fail on main and pass with this change.CodeGenSPIRVandSemaHLSLlit suites: 1872 expected passes, 4 expected failures (the same as main, plus the two new tests).verts[i].mid.inner.Uv.x), swizzled nested writes, an enclosing semantic overriding an inner one, and the NV path (-fspv-extension=SPV_NV_mesh_shader). An array of structs or aclassnested in an output still gets the existing "semantic string missing" error rather than a crash.Claude (Opus 5.5) was used to do a final pass on the PR body and code.