Skip to content

HLSL: avoid name collisions for flattened varying array elements - #17

Merged
bkaradzic-microsoft merged 1 commit into
BabylonJS:mainfrom
bkaradzic-microsoft:fix/hlsl-varying-name-collisions
Oct 13, 2026
Merged

bkaradzic-microsoft merged 1 commit into
BabylonJS:mainfrom
bkaradzic-microsoft:fix/hlsl-varying-name-collisions

Conversation

@bkaradzic-microsoft

Copy link
Copy Markdown
Member

#16 flattens narrow inter-stage varying arrays into name_i members, which collides with a user varying of the same name (e.g. vec2 bridge[2] plus vec2 bridge_0 produced two bridge_0 members). Raised in review of BabylonJS/BabylonNative#1926.

Flattened elements are now named name__i. SPIRV-Cross sanitizes source identifiers so they never contain __ (ParsedIR::is_valid_identifier / sanitize_underscores), so the generated names cannot collide.

Testing: the spirv-cross-hlsl-narrow-varying-test fixture adds a pairs_0 varying next to pairs[3]; the generated vertex and pixel shaders compile with fxc (vs_5_0/ps_5_0).

Name flattened narrow varying array elements name__i instead of name_i.
SPIRV-Cross sanitizes source identifiers so they never contain "__",
so the generated members cannot collide with a user varying such as
pairs_0. Extend the fixture with a colliding pairs_0 varying.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 60c2ec68-6de1-445d-9fc9-b699db737eae

@bghgary bghgary left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[Reviewed by Copilot on behalf of @bghgary]

Concerns inline.

Comment thread spirv_hlsl.cpp
// cannot collide with a user varying such as `name_0`.
string CompilerHLSL::flattened_varying_element_name(const string &name, uint32_t index)
{
return join(name, "__", index);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Please also avoid collisions with generated interface-block member names. out B { vec2 x[2]; } first; plus out B_x { vec2 _0; } second; emits two B_x__0 members with this change, and FXC rejects the shader. Both identifiers are valid after sanitization; the double underscore in the second name comes from joining the block and member names.

@bkaradzic-microsoft
bkaradzic-microsoft merged commit 2b078ef into BabylonJS:main Oct 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants