]> git.tdb.fi Git - libs/gl.git/blobdiff - source/glsl/validate.cpp
Refactor a common part in LocationAllocator into a function
[libs/gl.git] / source / glsl / validate.cpp
index ce30c2abdf44fe9156476e9eba145063c90fde43..e11f181e0b42d1140b67b313cfe449ff8bad52d7 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>
@@ -12,11 +12,6 @@ namespace Msp {
 namespace GL {
 namespace SL {
 
-Validator::Validator():
-       stage(0),
-       last_provoker(0)
-{ }
-
 void Validator::diagnose(Node &node, Node &provoking_node, Diagnostic::Severity severity, const string &message)
 {
        Diagnostic diag;
@@ -39,12 +34,12 @@ void Validator::add_info(Node &node, const string &message)
 }
 
 
-DeclarationValidator::DeclarationValidator():
-       scope(GLOBAL),
-       iface_layout(0),
-       iface_block(0),
-       variable(0)
-}
+void DeclarationValidator::apply(Stage &s, const Features &f)
+{
+       stage = &s;
+       features = f;
+       s.content.visit(*this);
+}
 
 const char *DeclarationValidator::describe_variable(ScopeType scope)
 {
@@ -61,6 +56,8 @@ const char *DeclarationValidator::describe_variable(ScopeType scope)
 
 void DeclarationValidator::visit(Layout &layout)
 {
+       bool push_constant = false;
+       bool binding = false;
        for(const Layout::Qualifier &q: layout.qualifiers)
        {
                bool allowed = false;
@@ -70,7 +67,9 @@ void DeclarationValidator::visit(Layout &layout)
                        allowed = (variable && scope==GLOBAL);
                else if(q.name=="binding" || q.name=="set")
                {
-                       if(q.name=="set")
+                       binding = true;
+
+                       if(q.name=="set" && features.target_api!=VULKAN)
                        {
                                error(layout, "Layout qualifier 'set' not allowed when targeting OpenGL");
                                continue;
@@ -153,6 +152,12 @@ void DeclarationValidator::visit(Layout &layout)
                                err_descr = "non-matrix variable";
                        }
                }
+               else if(q.name=="push_constant")
+               {
+                       push_constant = true;
+                       allowed = (iface_block && !variable && iface_block->interface=="uniform");
+                       value = false;
+               }
 
                if(!allowed)
                {
@@ -174,6 +179,9 @@ void DeclarationValidator::visit(Layout &layout)
                else if(!value && q.has_value)
                        error(layout, format("Layout qualifier '%s' does not allow a value", q.name));
        }
+
+       if(push_constant && binding)
+               error(layout, "Layout qualifier 'push_constant' not allowed together with 'binding' or 'set'");
 }
 
 void DeclarationValidator::visit(InterfaceLayout &layout)
@@ -248,6 +256,14 @@ void DeclarationValidator::visit(VariableDeclaration &var)
                        error(var, format("Mismatched interface qualifier '%s' inside '%s' block", var.interface, iface_block->interface));
                else if(scope==STRUCT || scope==FUNCTION)
                        error(var, format("Interface qualifier not allowed on %s", descr));
+               else if(scope==GLOBAL && variable->interface=="uniform" && features.target_api==VULKAN)
+               {
+                       TypeDeclaration *type = variable->type_declaration;
+                       while(BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(type))
+                               type = basic->base_type;
+                       if(!dynamic_cast<ImageTypeDeclaration *>(type))
+                               error(var, "Interface qualifier 'uniform' not allowed on non-opaque variable in global scope");
+               }
        }
 
        TypeDeclaration *type = var.type_declaration;
@@ -266,8 +282,13 @@ void DeclarationValidator::visit(VariableDeclaration &var)
        }
        else if(kind==BasicTypeDeclaration::VOID)
                error(var, "Type 'void' not allowed on variable");
-       else if(kind==BasicTypeDeclaration::BOOL && !var.interface.empty() && var.source!=BUILTIN_SOURCE)
-               error(var, "Type 'bool' not allowed on interface variable");
+       else if(kind==BasicTypeDeclaration::BOOL && var.source!=BUILTIN_SOURCE)
+       {
+               if(scope==INTERFACE_BLOCK)
+                       error(var, "Type 'bool' not allowed in an interface block");
+               else if(!var.interface.empty())
+                       error(var, "Type 'bool' not allowed on interface variable");
+       }
 
        if(var.init_expression)
        {
@@ -305,10 +326,6 @@ void DeclarationValidator::visit(FunctionDeclaration &func)
 }
 
 
-IdentifierValidator::IdentifierValidator():
-       anonymous_block(false)
-{ }
-
 void IdentifierValidator::multiple_definition(const string &name, Statement &statement, Statement &previous)
 {
        error(statement, format("Multiple definition of %s", name));
@@ -506,15 +523,19 @@ void ReferenceValidator::visit(FunctionDeclaration &func)
 }
 
 
-ExpressionValidator::ExpressionValidator():
-       current_function(0),
-       constant_expression(false)
-{ }
-
 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)
@@ -540,7 +561,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))
@@ -630,6 +651,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)
@@ -640,12 +667,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);
        }
 }
@@ -698,10 +733,6 @@ void ExpressionValidator::visit(Return &ret)
 }
 
 
-FlowControlValidator::FlowControlValidator():
-       reachable(true)
-{ }
-
 void FlowControlValidator::visit(Block &block)
 {
        for(const RefPtr<Statement> &s: block.body)