Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions source/slang/slang-check-decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,13 +240,19 @@ struct SemanticsDeclModifiersVisitor : public SemanticsDeclVisitorBase,
// In HLSL, const global variables without static are uniform parameters
// that cannot have default values
// Exception: specialization constants are allowed to have initializers
// Exception: In GLSL mode, global const variables are real constants, not uniform
// parameters
if (isGlobalDecl(decl) && (hasConst || hasUniform) && !hasStatic &&
!hasSpecializationConstant && decl->initExpr)
{
getSink()->diagnose(
decl,
Diagnostics::constGlobalVarWithInitRequiresStatic,
decl->getName());
auto moduleDecl = getModuleDecl(decl);
if (!moduleDecl || !moduleDecl->hasModifier<GLSLModuleModifier>())
{
getSink()->diagnose(
decl,
Diagnostics::constGlobalVarWithInitRequiresStatic,
decl->getName());
}
}
}

Expand Down
26 changes: 26 additions & 0 deletions tests/diagnostics/glsl-global-const-with-init.slang
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//TEST:SIMPLE(filecheck=CHK): -target glsl -stage fragment -entry main -allow-glsl

// Test for GLSL mode: global const variables with initializers should be allowed
// In GLSL mode, global const variables are real constants, not uniform parameters
// This should NOT produce the error 31224 that would trigger in HLSL mode

#version 450

// These should NOT trigger error 31224 in GLSL mode (they would in HLSL)
const float globalConstWithInit = 1.0; // OK in GLSL - real constant
const vec3 globalVecConst = vec3(1.0, 2.0, 3.0); // OK in GLSL - real constant
const int globalIntConst = 42; // OK in GLSL - real constant

// Regular uniforms without const should still be allowed
uniform float uniformFloat; // OK - uniform without const
uniform vec4 uniformVec; // OK - uniform without const

// CHK-NOT: error 31224
// CHK: void main()

out vec4 fragColor;

void main()
{
fragColor = vec4(globalConstWithInit, globalVecConst.x, globalIntConst, 1.0);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//TEST:SIMPLE(filecheck=CHK): -target hlsl -entry main

// Test to ensure HLSL mode still produces the diagnostic for global const with initializers
// This verifies our fix doesn't break existing HLSL behavior

// This should trigger error 31224 in HLSL mode
//CHK: ([[# @LINE + 1]]): error 31224: global const variable with initializer must be declared static: 'globalConstWithInit'
const float globalConstWithInit = 1.0f;

// This should also trigger error 31224 in HLSL mode
//CHK: ([[# @LINE + 1]]): error 31224: global const variable with initializer must be declared static: 'uniformWithInit'
uniform float uniformWithInit = 2.0f;

[shader("vertex")]
float4 main() : SV_Position
{
return float4(1, 0, 0, 1);
}
Loading