Skip to content

[SPIR-V] Fix nested and inherited structs in mesh shader outputs - #8982

Open
Abir Deol (apollo-2006) wants to merge 3 commits into
microsoft:mainfrom
apollo-2006:spirv-mesh-nested-struct-output
Open

Abir Deol (apollo-2006) wants to merge 3 commits into
microsoft:mainfrom
apollo-2006:spirv-mesh-nested-struct-output

Conversation

@apollo-2006

Copy link
Copy Markdown
Contributor

Fixes #7340 and #8475.

When a mesh shader's output struct contains another struct whose fields carry the semantics:

struct VertexAttributes { float3 PositionVS : POSITION0; float3 Normal : NORMAL0; };
struct VertexOut {
  float4 PositionHS : SV_Position;
  VertexAttributes attributes;
  uint MeshletIndex : COLOR0;
};

the SPIR-V backend crashed on any write to the output. The stage variables are created for the innermost fields (POSITION0, NORMAL0), but assignToMSOutAttribute looked up the semantic of the attributes field itself, which has none, and failed assert(semanticInfo.isValid()). In release builds the code goes on with the empty semantic, which matches the crash inside assignToMSOutAttribute in #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; (here collectArrayStructIndices stopped at attributes and turned Normal into a trailing index, which was then treated as a vector component)
  • whole-element and nested field writes on out primitives
  • a nested struct with its own semantic, VertexAttributes attributes : ATTR0, whose fields inherit ATTR0/ATTR1

While testing, I also found two problems with base classes in mesh outputs that happen without any nesting, on current main:

  • verts[i] = v where VertexOut has a base class never wrote the inherited fields. The loop only walked fields(), so their stage variables were never stored to.
  • verts[i].Tangent = t, with Tangent inherited, 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:

  • In 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).
  • A new assignToMSOutStruct assigns a struct value by its base classes, then its fields, recursing into nested structs. It's the same layout writeBackOutputStream uses for geometry shader outputs. The existing whole-element path now uses it too.
  • Leaf fields use the semantic their stage variable was created with (a new DeclResultIdMapper::getStageVarCreationSemantic), so fields that inherit a struct's semantic work.

Testing:

  • New meshshading.ext.nested.struct.mesh.hlsl covers whole-element, nested member, nested field, nested component, per-primitive, and inherited-semantic writes. New meshshading.ext.inherited.struct.mesh.hlsl covers 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.
  • CodeGenSPIRV and SemaHLSL lit suites: 1872 expected passes, 4 expected failures (the same as main, plus the two new tests).
  • I also checked by hand, with spirv-val (Vulkan 1.3) and by reading the stores: the issue's shader, two levels of nesting (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 a class nested in an output still gets the existing "semantic string missing" error rather than a crash.
  • git-clang-format 17.0.1 is clean.

Claude (Opus 5.5) was used to do a final pass on the PR body and code.

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
Copilot AI balanced review requested due to automatic review settings September 29, 2026 18:23
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 High severity · 1 Medium severity

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.

@apollo-2006

Copy link
Copy Markdown
Contributor Author

@microsoft-github-policy-service agree

@s-perron

Copy link
Copy Markdown
Collaborator

FYI: Diego Novillo (@dnovillo)

@github-project-automation github-project-automation Bot moved this from New to In progress in HLSL Roadmap Nov 16, 2026
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.
Copilot AI balanced review requested due to automatic review settings November 16, 2026 21:19

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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

Copilot AI balanced review requested due to automatic review settings November 17, 2026 19:58

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The supplied review reports no active issues blocking approval.

1 open issue
1 resolved since last review

Review effort: Balanced

@dnovillo Diego Novillo (dnovillo) left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks for the extra tests.

@apollo-2006

Copy link
Copy Markdown
Contributor Author

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?

@dnovillo

Copy link
Copy Markdown
Collaborator

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!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In progress

Development

Successfully merging this pull request may close these issues.

[SPIR-V] Internal compiler error for mesh shader with nested struct inside output

4 participants