From 2e9d0beaafa8220d8a917749c8a12beaf4718729 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sun, 7 Nov 2021 14:32:33 +0200 Subject: [PATCH] Disallow specialization constants in certain contexts The initialization of a specialization constant must not use other specialization constants. Arrays sized by a specialization constant inside structures are broken by design because member offsets in SPIR-V are required to be literals. Changing the size through specialization would at best leave empty space in the struct and at worst overlap other members. --- source/glsl/validate.cpp | 35 +++++++++++++++++---- source/glsl/validate.h | 11 ++++++- tests/glsl/specialized_array_in_struct.glsl | 26 ++------------- 3 files changed, 42 insertions(+), 30 deletions(-) diff --git a/source/glsl/validate.cpp b/source/glsl/validate.cpp index ec322f42..f12177db 100644 --- a/source/glsl/validate.cpp +++ b/source/glsl/validate.cpp @@ -1,5 +1,5 @@ -#include #include +#include #include #include #include @@ -497,8 +497,17 @@ void ReferenceValidator::visit(FunctionDeclaration &func) void ExpressionValidator::visit(VariableReference &var) { - if(var.declaration && constant_expression && !var.declaration->constant) - error(var, format("Reference to non-constant variable '%s' in a constant expression", var.name)); + if(var.declaration && constant_expression) + { + if(!var.declaration->constant) + error(var, format("Reference to non-constant variable '%s' in a constant expression", var.name)); + else if(var.declaration->layout && constant_expression==FIXED_CONSTANT) + { + auto i = find_member(var.declaration->layout->qualifiers, string("constant_id"), &Layout::Qualifier::name); + if(i!=var.declaration->layout->qualifiers.end()) + error(var, format("Reference to specialization constant '%s' in a fixed constant expression", var.name)); + } + } } void ExpressionValidator::visit(InterfaceBlockReference &iface) @@ -524,7 +533,7 @@ void ExpressionValidator::visit(Swizzle &swizzle) int flavour = -1; for(unsigned i=0; i=0 && component_flavour!=static_cast(flavour)) @@ -614,6 +623,12 @@ void ExpressionValidator::visit(TernaryExpression &ternary) TraversingVisitor::visit(ternary); } +void ExpressionValidator::visit(StructDeclaration &strct) +{ + SetFlag set_struct(in_struct); + TraversingVisitor::visit(strct); +} + void ExpressionValidator::visit(VariableDeclaration &var) { if(var.init_expression && var.init_expression->type && var.type_declaration && var.init_expression->type!=var.type_declaration) @@ -624,12 +639,20 @@ void ExpressionValidator::visit(VariableDeclaration &var) var.layout->visit(*this); if(var.init_expression) { - SetFlag set_const(constant_expression, var.constant); + ConstantKind const_kind = (var.constant ? SPEC_CONSTANT : NOT_CONSTANT); + if(var.layout) + { + auto i = find_member(var.layout->qualifiers, string("constant_id"), &Layout::Qualifier::name); + if(i!=var.layout->qualifiers.end()) + const_kind = FIXED_CONSTANT; + } + + SetForScope set_const(constant_expression, const_kind); TraversingVisitor::visit(var.init_expression); } if(var.array_size) { - SetFlag set_const(constant_expression); + SetForScope set_const(constant_expression, (in_struct || !var.interface.empty() ? FIXED_CONSTANT : SPEC_CONSTANT)); TraversingVisitor::visit(var.array_size); } } diff --git a/source/glsl/validate.h b/source/glsl/validate.h index 52ae136b..b47a9911 100644 --- a/source/glsl/validate.h +++ b/source/glsl/validate.h @@ -113,8 +113,16 @@ is indicated by a null result type. */ class ExpressionValidator: private Validator { private: + enum ConstantKind + { + NOT_CONSTANT, + FIXED_CONSTANT, + SPEC_CONSTANT + }; + FunctionDeclaration *current_function = 0; - bool constant_expression = false; + bool in_struct = false; + ConstantKind constant_expression = NOT_CONSTANT; public: void apply(Stage &s) { stage = &s; s.content.visit(*this); } @@ -127,6 +135,7 @@ private: virtual void visit(BinaryExpression &); virtual void visit(Assignment &); virtual void visit(TernaryExpression &); + virtual void visit(StructDeclaration &); virtual void visit(VariableDeclaration &); virtual void visit(FunctionDeclaration &); virtual void visit(Conditional &); diff --git a/tests/glsl/specialized_array_in_struct.glsl b/tests/glsl/specialized_array_in_struct.glsl index 4a6d9ab6..f82bd74e 100644 --- a/tests/glsl/specialized_array_in_struct.glsl +++ b/tests/glsl/specialized_array_in_struct.glsl @@ -24,27 +24,7 @@ void main() // Compile mode: module -/* Expected output: vertex -layout(location=0) in vec4 position; -void main() -{ - gl_Position = position; -} -*/ - -/* Expected output: fragment -layout(constant_id=953017667) const int max_colors = 2; -struct ColorInfo -{ - vec4 color; -}; -layout(binding=23) uniform Colors -{ - ColorInfo colors[max_colors]; -}; -layout(location=0) out vec4 frag_color; -void main() -{ - frag_color = colors[0].color; -} +/* Expected error: +:8: Reference to specialization constant 'max_colors' in a fixed constant expression +:8: Reference to specialization constant 'max_colors' in a fixed constant expression */ -- 2.45.2