yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

Yong HeDiagnose on initExpr of a global `const` decl (#7711)56e91e91e

master
2.0 KiB49 linesraw
1//TEST:SIMPLE(filecheck=CHK): -target hlsl -entry main
2
3// Test for diagnostic 31224: global const/uniform variables with initializers must be static
4// Exception: specialization constants are allowed to have initializers without static
5// Test for diagnostic 31219: specialization constants cannot be declared static
6
7// These should trigger error 31224
8//CHK-DAG: ([[# @LINE + 1]]): error 31224: global const variable with initializer must be declared static: 'globalConstWithInit'
9const float globalConstWithInit = 1.0f;
10
11//CHK-DAG: ([[# @LINE + 1]]): error 31224: global const variable with initializer must be declared static: 'uniformWithInit'
12uniform float uniformWithInit = 2.0f;
13
14// These are valid and should not produce errors
15static const float staticConstWithInit = 3.0f;        // OK - static const with init
16const float globalConstNoInit;                        // OK - const without init
17uniform float uniformNoInit;                          // OK - uniform without init
18
19// Specialization constants with initializers are allowed without static
20[SpecializationConstant]
21const int specConstInt = 42;                          // OK - specialization constant with init
22
23[vk::constant_id(5)]
24const float specConstFloat = 3.14f;                   // OK - vk constant id with init
25
26// These should trigger error 31219: static specialization constants are not allowed
27//CHK-DAG: ([[# @LINE + 2]]): error 31219: push or specialization constants cannot be 'static'.
28[SpecializationConstant]
29static const int staticSpecConstInt = 100;
30
31//CHK-DAG: ([[# @LINE + 2]]): error 31219: push or specialization constants cannot be 'static'.
32[vk::constant_id(7)]
33static const float staticSpecConstFloat = 2.71f;
34
35void functionScope()
36{
37    const float localConstWithInit = 4.0f;           // OK - local const with init
38}
39
40struct MyStruct 
41{
42    static const float memberStaticConst = 5.0f;    // OK - member static const
43};
44
45[shader("vertex")]
46float4 main() : SV_Position
47{
48    return float4(staticConstWithInit, 0, 0, 1);
49}