KhronosGroup / KhronosGroup/glslang
HLSL compilation produces scalar-requiring cbuffer offsets by default
- Dominant language
- C++
- Stars
- 3.6k
- Forks
- 989
- Avg merge
- 1d 2h
- Merged PRs (30d)
- 31
Description
Given this HLSL shader:
```
cbuffer consts
{
float o[4];
float p;
};
float4 main() : SV_Target0 { return p.xxxx; }
```
glslang used to emit a cbuffer with `p` at offset 64, which complies with the normal Vulkan packing rules. It looks like in #3575 this was changed to emit an offset of 52. Doing so requires scalar block layout on the vulkan side and to me is an unexpected behaviour change:
```
$ glslang -D -V -S frag -e main test.hlsl
$ spirv-val --target-env vulkan1.1spv1.4 frag.spv
error: line 27: Structure id 14 decorated as Block for variable in Uniform storage class must
follow relaxed uniform buffer layout rules: member1 at offset 52 overlaps previous member
ending at offset 63
%consts = OpTypeStruct %_arr_float_uint_4 %float
$ spirv-val --target-env vulkan1.1spv1.4 --scalar-block-layout frag.spv
```
Compare to dxc which by default puts `p` at an offset of 64, but has options `-fvk-use-dx-layout` and `-fvk-use-scalar-layout` to emit D3D-packed layouts or scalar packed layouts respectively, though both require scalar block layout to remove this rule.
This effectively breaks backwards compatibility - was that deliberate? If so is there any way to at least opt-out to this behaviour if you don't want to have it opt-in?
Contributor guide
Research direction
Reproduce the HLSL case with glslang and validate the generated SPIR-V using the commands in the issue, then compare the default cbuffer offsets with dxc. Trace the HLSL compilation and layout handling changed in #3575. Done means the default behavior is resolved or an explicit opt-out is provided without requiring scalar block layout unexpectedly.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 28/100