X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Fglsl%2Fvalidate.cpp;h=7d3d20d1c78eb104c85ac8ef511a099b5115ae5e;hp=0c4b98cbb2c8fd9de87ec21b553d911febde227a;hb=76cc18518fc8b0b4fa11fda153e7d9b3899ed112;hpb=38712d8ecc57d043a2419ffbaeeb57f7a6586f14 diff --git a/source/glsl/validate.cpp b/source/glsl/validate.cpp index 0c4b98cb..7d3d20d1 100644 --- a/source/glsl/validate.cpp +++ b/source/glsl/validate.cpp @@ -1,5 +1,5 @@ -#include #include +#include #include #include #include @@ -34,6 +34,13 @@ void Validator::add_info(Node &node, const string &message) } +void DeclarationValidator::apply(Stage &s, const Features &f) +{ + stage = &s; + features = f; + s.content.visit(*this); +} + const char *DeclarationValidator::describe_variable(ScopeType scope) { switch(scope) @@ -49,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; @@ -58,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; @@ -66,11 +77,9 @@ void DeclarationValidator::visit(Layout &layout) if(variable) { - TypeDeclaration *type = variable->type_declaration; - while(BasicTypeDeclaration *basic = dynamic_cast(type)) - type = basic->base_type; + const TypeDeclaration *base_type = get_ultimate_base_type(variable->type_declaration); bool uniform = (variable->interface=="uniform"); - allowed = (scope==GLOBAL && uniform && dynamic_cast(type)); + allowed = (scope==GLOBAL && uniform && dynamic_cast(base_type)); err_descr = (uniform ? "variable of non-opaque type" : "non-uniform variable"); } else if(iface_block) @@ -141,6 +150,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) { @@ -162,6 +177,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) @@ -236,6 +254,11 @@ 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) + { + if(!dynamic_cast(get_ultimate_base_type(variable->type_declaration))) + error(var, "Interface qualifier 'uniform' not allowed on non-opaque variable in global scope"); + } } TypeDeclaration *type = var.type_declaration; @@ -254,8 +277,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) { @@ -492,8 +520,16 @@ 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) + { + if(has_layout_qualifier(var.declaration->layout.get(), "constant_id")) + error(var, format("Reference to specialization constant '%s' in a fixed constant expression", var.name)); + } + } } void ExpressionValidator::visit(InterfaceBlockReference &iface) @@ -519,7 +555,7 @@ void ExpressionValidator::visit(Swizzle &swizzle) int flavour = -1; for(unsigned i=0; i=0 && component_flavour!=static_cast(flavour)) @@ -609,6 +645,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) @@ -619,12 +661,15 @@ 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 ? NOT_CONSTANT : + has_layout_qualifier(var.layout.get(), "constant_id") ? FIXED_CONSTANT : SPEC_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); } } @@ -719,18 +764,13 @@ void FlowControlValidator::visit(Iteration &iter) } -int StageInterfaceValidator::get_location(const Layout &layout) -{ - return get_layout_value(layout, "location", -1); -} - void StageInterfaceValidator::visit(VariableDeclaration &var) { - int location = (var.layout ? get_location(*var.layout) : -1); + int location = get_layout_value(var.layout.get(), "location"); if(var.interface=="in" && var.linked_declaration) { const Layout *linked_layout = var.linked_declaration->layout.get(); - int linked_location = (linked_layout ? get_location(*linked_layout) : -1); + int linked_location = get_layout_value(linked_layout, "location"); if(linked_location!=location) { error(var, format("Mismatched location %d for 'in %s'", location, var.name)); @@ -794,6 +834,11 @@ void GlobalInterfaceValidator::check_uniform(const Uniform &uni) error(*uni.node, format("Mismatched location %d for uniform '%s'", uni.location, uni.name)); add_info(*i->second->node, format("Previously declared here with location %d", i->second->location)); } + if(i->second->desc_set!=uni.desc_set) + { + error(*uni.node, format("Mismatched descriptor set %d for uniform '%s'", uni.desc_set, uni.name)); + add_info(*i->second->node, format("Previously declared here with descriptor set %d", i->second->desc_set)); + } if(uni.bind_point>=0 && i->second->bind_point>=0 && i->second->bind_point!=uni.bind_point) { error(*uni.node, format("Mismatched binding %d for uniform '%s'", uni.bind_point, uni.name)); @@ -859,10 +904,10 @@ void GlobalInterfaceValidator::visit(VariableDeclaration &var) uni.name = var.name; if(var.layout) { - uni.location = get_layout_value(*var.layout, "location"); + uni.location = get_layout_value(var.layout.get(), "location"); uni.loc_count = LocationCounter().apply(var); - uni.desc_set = get_layout_value(*var.layout, "set", 0); - uni.bind_point = get_layout_value(*var.layout, "binding"); + uni.desc_set = get_layout_value(var.layout.get(), "set", 0); + uni.bind_point = get_layout_value(var.layout.get(), "binding"); } uniforms.push_back(uni); @@ -880,8 +925,8 @@ void GlobalInterfaceValidator::visit(InterfaceBlock &iface) uni.name = iface.block_name; if(iface.layout) { - uni.desc_set = get_layout_value(*iface.layout, "set", 0); - uni.bind_point = get_layout_value(*iface.layout, "binding"); + uni.desc_set = get_layout_value(iface.layout.get(), "set", 0); + uni.bind_point = get_layout_value(iface.layout.get(), "binding"); } uniforms.push_back(uni);