]> git.tdb.fi Git - libs/gl.git/commitdiff
Disallow specialization constants in certain contexts
authorMikko Rasa <tdb@tdb.fi>
Sun, 7 Nov 2021 12:32:33 +0000 (14:32 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sun, 7 Nov 2021 14:00:32 +0000 (16:00 +0200)
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
source/glsl/validate.h
tests/glsl/specialized_array_in_struct.glsl

index ec322f4207751af90482893dc21dc4f4dce39e0a..f12177db4a4236109f4060ffc72c13098f66093f 100644 (file)
@@ -1,5 +1,5 @@
-#include <algorithm>
 #include <cstring>
+#include <msp/core/algorithm.h>
 #include <msp/core/raii.h>
 #include <msp/strings/format.h>
 #include <msp/strings/utils.h>
@@ -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<swizzle.count; ++i)
                {
-                       unsigned component_flavour = (find(component_names, component_names+12, swizzle.component_group[i])-component_names)/4;
+                       unsigned component_flavour = (std::find(component_names, component_names+12, swizzle.component_group[i])-component_names)/4;
                        if(flavour==-1)
                                flavour = component_flavour;
                        else if(flavour>=0 && component_flavour!=static_cast<unsigned>(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<ConstantKind> set_const(constant_expression, const_kind);
                TraversingVisitor::visit(var.init_expression);
        }
        if(var.array_size)
        {
-               SetFlag set_const(constant_expression);
+               SetForScope<ConstantKind> set_const(constant_expression, (in_struct || !var.interface.empty() ? FIXED_CONSTANT : SPEC_CONSTANT));
                TraversingVisitor::visit(var.array_size);
        }
 }
index 52ae136bb61da6053f425cd1d70cc2ad87231be9..b47a9911d95c6a147034f1937243f34bd69ebca9 100644 (file)
@@ -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 &);
index 4a6d9ab68773158540b2f1dd38162f9631611f04..f82bd74eb4a5af4f8794845a29ab26eae08348f3 100644 (file)
@@ -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:
+<test>:8: Reference to specialization constant 'max_colors' in a fixed constant expression
+<test>:8: Reference to specialization constant 'max_colors' in a fixed constant expression
 */