KhronosGroup / KhronosGroup/glslang

Array with size using specialization constant passed as function parameter.

Open
#3,315 2 comments 0 reactions 0 assignees View on GitHub
GLSL/ESSL question Specification
Dominant language
C++
Stars
3.6k
Forks
989
Avg merge
1d 2h
Merged PRs (30d)
31

Description

Following code compiled with glslangValidator, the spirv will make a shadow copy of the function parameter and call the function, but glsl can't use with types containing arrays sized with a specialization constant for initializer,
```
#version 460
#extension GL_EXT_shader_explicit_arithmetic_types : enable
layout(constant_id = 0) const uint32_t RANK_OUT = 4;
layout(local_size_x = 64) in;
void outputOffsetToIndex(uint[RANK_OUT] shape, uint offset, out uint[RANK_OUT] index) {
for(uint32_t i = RANK_OUT - 1; i >= 0; i--) {
index[i] = offset % shape[i];
offset /= shape[i];
}
}
void main() {
uint offset = gl_GlobalInvocationID.x;
uint[RANK_OUT] shape;
uint[RANK_OUT] index;
outputOffsetToIndex(shape, offset, index);
}
```

if use spirv-cross to decompile the spv to glsl, code will be like this, compile again using glslang will print: 'initializer' : can't use with types containing arrays sized with a specialization constant
```
#version 460
layout(local_size_x = 64, local_size_y = 1, local_size_z = 1) in;
layout(constant_id = 0) const uint RANK_OUT = 4u;
const uint _19 = (RANK_OUT - 1u);
void outputOffsetToIndex(uint shape[RANK_OUT], uint offset, out uint index[RANK_OUT])
{
for (uint i = _19; i >= 0u; i--)
{
index[i] = offset % shape[i];
offset /= shape[i];
}
}
void main()
{
uint offset = gl_GlobalInvocationID.x;
uint shape[RANK_OUT];
uint param[RANK_OUT] = shape;
uint param_1 = offset;
uint index[RANK_OUT];
uint param_2[RANK_OUT] = index;
outputOffsetToIndex(param, param_1, param_2);
shape = param;
offset = param_1;
index = param_2;
}
```

Contributor guide

Open the contributing guide

Research direction

Start with the GLSL reproduction in the issue, compile it with glslangValidator, then use spirv-cross to decompile the generated SPIR-V and reproduce the reported initializer error. Trace the parameter shadow-copy output and verify that the round-tripped GLSL compiles successfully without the specialization-constant array initializer error.

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
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.