Skip to content

static const global variables get transpiled to const in wgsl which dont support indexing #6747

Description

@zurrutik

I created the following shader in slang:

//////////////constants
static const float2 positions[] = {
    float2( -1.0f, -1.0f ),
    float2(  1.0f, -1.0f ),
    float2(  1.0f,  1.0f ),
    float2( -1.0f,  1.0f )
};

static const float2 texCoords[] = {
    float2( 0.0f, 0.0f ),
    float2( 1.0f, 0.0f ),
    float2( 1.0f, 1.0f ),
    float2( 0.0f, 1.0f )
};


//////////////types
struct VertexOutput
{
    float4 position : SV_POSITION;
    float2 texCoord : TEXCOORD;
};

/////////////binding sets
[[vk::binding(0, 0)]] Texture2D tex : register(t0, space0);

////////////vertex shader
[shader("vertex")]
VertexOutput vertexMain( uint vertexID: SV_VertexID )
{
    VertexOutput output;

    output.position = float4( positions[vertexID], 0.0f, 1.0f );
    output.texCoord = texCoords[vertexID];
    
    return output;
}

////////////fragment shader
[shader("fragment")]
float4 fragmentMain( VertexOutput input ) : SV_Target
{
    return 1.0f - tex.Load( int3( int2( input.position.xy ), 0 ) );
}

The problem is that the "static const" global variables get translated to "const" in .wgsl:

const texCoords_0 : array<vec2<f32>, i32(4)> = array<vec2<f32>, i32(4)>( vec2<f32>(0.0f, 0.0f), vec2<f32>(1.0f, 0.0f), vec2<f32>(1.0f, 1.0f), vec2<f32>(0.0f, 1.0f) );
const positions_0 : array<vec2<f32>, i32(4)> = array<vec2<f32>, i32(4)>( vec2<f32>(-1.0f, -1.0f), vec2<f32>(1.0f, -1.0f), vec2<f32>(1.0f, 1.0f), vec2<f32>(-1.0f, 1.0f) );

But unfortunately when I compile the shader I get the following error:

Uncaptured device error: type WGPUErrorType_Validation
 (Validation Error
 
 Caused by:
     In wgpuDeviceCreateShaderModule
 
 Shader validation error:
    ÔöîÔöÇ :15:37
    Ôöé
 15 Ôöé     output_0.position_0 = vec4<f32>(positions_0[vertexID_0], 0.0f, 1.0f);
    Ôöé                                     ^^^^^^^^^^^^^^^^^^^^^^^ naga::Expression [5]
 
 
     Entry point vertexMain at Vertex is invalid
     Expression [5] is invalid
     The expression [4] may only be indexed by a constant
 )

I think its because "const" in .wgsl means more like "constexpr" in C++ rather than "const" therefore it can only be indexed by another "constexpr" variable( which vertexID is not )

If I change texCoords_0 and positions_0 from "const" to "var<private>" it works no problem( I was tempted to use "let" which is supposed to be the equivalent of C++'s "const" but then I get an error ---> Shader '' parsing error: expected global item ('struct', 'const', 'var', 'alias', ';', 'fn') or the end of the file, found 'let'

Could you please fix this issue?? ( or alternatively provide a solution for the problem )

Regards,

Metadata

Metadata

Assignees

Labels

Dev ReviewedGoodFirstBugGreat bug for people getting going in slang codebase

Type

No fields configured for Bug.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions