From 299eb0c889322a960d1907b6722ee1caf88475a4 Mon Sep 17 00:00:00 2001 From: pdeayton-nv <205388607+pdeayton-nv@users.noreply.github.com> Date: Tue, 15 Jul 2025 17:44:33 -0700 Subject: Fix GLSL memory layout qualifiers not applied to uniform buffers or cbuffers (#7740) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix GLSL memory layout qualifiers not applied to uniform buffers or cbuffers The `layout(scalar)` qualifier was being ignored for GLSL `uniform` blocks and HLSL `cbuffer` declarations, causing them to use std140 layout instead of the requested scalar layout. **Root Cause**: The parsing logic in `slang-parser.cpp` handled layout qualifiers inconsistently: - GLSL `buffer` blocks correctly used `getLayoutArg()` to map layout modifiers - GLSL `uniform` blocks and HLSL `cbuffer` skipped layout arguments entirely **Solution**: - Enhanced `parseHLSLCBufferDecl()` to check for GLSL layout qualifiers - Added same `getLayoutArg()` logic used by buffer blocks - Modified uniform block parsing to pass layout arguments through **Testing**: - Added comprehensive test case: `tests/glsl/layout-scalar-qualifier.slang` - Verified fix works for both `uniform` blocks and `cbuffer` declarations - Confirmed no regressions in existing test suite **Before**: `layout(scalar) uniform {...}` → std140 layout (32 bytes, offset 0,16) **After**: `layout(scalar) uniform {...}` → scalar layout (16 bytes, offset 0,4) Fixes #7735 🤖 Generated with [Claude Code](https://claude.ai/code) Co-authored-by: pdeayton-nv * Apply code formatting to slang-parser.cpp Fixed line wrapping, trailing whitespace, and parameter formatting according to repository style guidelines. Co-authored-by: Harsh Aggarwal (NVIDIA) * format code (#7742) Co-authored-by: slangbot <186143334+slangbot@users.noreply.github.com> --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: pdeayton-nv Co-authored-by: Harsh Aggarwal (NVIDIA) Co-authored-by: slangbot Co-authored-by: slangbot <186143334+slangbot@users.noreply.github.com> Co-authored-by: Yong He --- tests/glsl/layout-scalar-qualifier.slang | 39 ++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 tests/glsl/layout-scalar-qualifier.slang (limited to 'tests') diff --git a/tests/glsl/layout-scalar-qualifier.slang b/tests/glsl/layout-scalar-qualifier.slang new file mode 100644 index 000000000..ac7b34d8b --- /dev/null +++ b/tests/glsl/layout-scalar-qualifier.slang @@ -0,0 +1,39 @@ +//TEST:SIMPLE(filecheck=CHECK): -allow-glsl -target spirv-asm + +RWStructuredBuffer result; + +// Test GLSL uniform block with scalar layout qualifier +// CHECK: SLANG_ParameterGroup_Data_uniform_natural +// CHECK: OpMemberDecorate %SLANG_ParameterGroup_Data_uniform_natural 0 Offset 0 +// CHECK: OpMemberDecorate %SLANG_ParameterGroup_Data_uniform_natural 1 Offset 4 +layout(scalar) uniform Data_uniform { + uint a; + uint3 b; +}; + +// Test HLSL cbuffer with scalar layout qualifier +// CHECK: SLANG_ParameterGroup_Data_cbuffer_natural +// CHECK: OpMemberDecorate %SLANG_ParameterGroup_Data_cbuffer_natural 0 Offset 0 +// CHECK: OpMemberDecorate %SLANG_ParameterGroup_Data_cbuffer_natural 1 Offset 4 +layout(scalar) cbuffer Data_cbuffer { + uint c; + uint3 d; +}; + +// Reference: ScalarDataLayout should work correctly +struct Data { + uint e; + uint3 f; +}; +ConstantBuffer Data_ConstantBufferScalarDataLayout; + +[shader("compute")] +[numthreads(1,1,1)] +void computeMain(uint3 threadId : SV_DispatchThreadID) +{ + uint index = threadId.x; + result[index] = + a + b.x + + c + d.y + + Data_ConstantBufferScalarDataLayout.e + Data_ConstantBufferScalarDataLayout.f.z; +} \ No newline at end of file -- cgit v1.2.3