]> git.tdb.fi Git - libs/gl.git/blobdiff - source/glsl/reflect.cpp
Add support for uint types in GLSL
[libs/gl.git] / source / glsl / reflect.cpp
index 151735ab18f3790d51b94bbf61da68726ce56a39..8c10acccd697c265063d481a8f1a8b055ac492bb 100644 (file)
@@ -6,6 +6,190 @@ namespace Msp {
 namespace GL {
 namespace SL {
 
+bool is_scalar(const BasicTypeDeclaration &type)
+{
+       return (type.kind==BasicTypeDeclaration::INT || type.kind==BasicTypeDeclaration::FLOAT);
+}
+
+bool is_vector_or_matrix(const BasicTypeDeclaration &type)
+{
+       return (type.kind==BasicTypeDeclaration::VECTOR || type.kind==BasicTypeDeclaration::MATRIX);
+}
+
+BasicTypeDeclaration *get_element_type(BasicTypeDeclaration &type)
+{
+       if(is_vector_or_matrix(type) || type.kind==BasicTypeDeclaration::ARRAY)
+       {
+               BasicTypeDeclaration *basic_base = dynamic_cast<BasicTypeDeclaration *>(type.base_type);
+               return (basic_base ? get_element_type(*basic_base) : 0);
+       }
+       else
+               return &type;
+}
+
+bool can_convert(const BasicTypeDeclaration &from, const BasicTypeDeclaration &to)
+{
+       if(from.kind==BasicTypeDeclaration::INT && to.kind==BasicTypeDeclaration::FLOAT)
+               return from.size<=to.size;
+       else if(from.kind!=to.kind)
+               return false;
+       else if(from.kind==BasicTypeDeclaration::INT && from.sign!=to.sign)
+               return from.sign && from.size<=to.size;
+       else if(is_vector_or_matrix(from) && from.size==to.size)
+       {
+               BasicTypeDeclaration *from_base = dynamic_cast<BasicTypeDeclaration *>(from.base_type);
+               BasicTypeDeclaration *to_base = dynamic_cast<BasicTypeDeclaration *>(to.base_type);
+               return (from_base && to_base && can_convert(*from_base, *to_base));
+       }
+       else
+               return false;
+}
+
+
+unsigned TypeComparer::next_tag = 1;
+
+TypeComparer::TypeComparer():
+       first(0),
+       second(0),
+       first_tag(0),
+       r_result(false)
+{ }
+
+void TypeComparer::compare(Node &node1, Node &node2)
+{
+       if(&node1==&node2)
+               r_result = true;
+       else
+       {
+               second = &node2;
+               node1.visit(*this);
+       }
+}
+
+template<typename T>
+T *TypeComparer::multi_visit(T &node)
+{
+       static unsigned tag = next_tag++;
+
+       if(second)
+       {
+               Node *s = second;
+               first = &node;
+               first_tag = tag;
+               second = 0;
+               s->visit(*this);
+       }
+       else if(!first || tag!=first_tag)
+               r_result = false;
+       else
+       {
+               T *f = static_cast<T *>(first);
+               first = 0;
+               return f;
+       }
+
+       return 0;
+}
+
+void TypeComparer::visit(Literal &literal)
+{
+       if(Literal *lit1 = multi_visit(literal))
+       {
+               if(!lit1->type || !literal.type)
+                       r_result = false;
+               else
+               {
+                       compare(*lit1->type, *literal.type);
+                       if(r_result)
+                               r_result = (literal.value.check_type<int>() && lit1->value.value<int>()==literal.value.value<int>());
+               }
+       }
+}
+
+void TypeComparer::visit(VariableReference &var)
+{
+       if(VariableReference *var1 = multi_visit(var))
+       {
+               if(!var1->declaration || !var.declaration)
+                       r_result = false;
+               else if(!var1->declaration->constant || !var.declaration->constant)
+                       r_result = false;
+               else if(!var1->declaration->init_expression || !var.declaration->init_expression)
+                       r_result = false;
+               else
+                       compare(*var1->declaration->init_expression, *var.declaration->init_expression);
+       }
+}
+
+void TypeComparer::visit(BasicTypeDeclaration &basic)
+{
+       if(BasicTypeDeclaration *basic1 = multi_visit(basic))
+       {
+               if(basic1->kind!=basic.kind || basic1->size!=basic.size || basic1->sign!=basic.sign)
+                       r_result = false;
+               else if(basic1->base_type && basic.base_type)
+                       compare(*basic1->base_type, *basic.base_type);
+               else
+                       r_result = (!basic1->base_type && !basic.base_type);
+       }
+}
+
+void TypeComparer::visit(ImageTypeDeclaration &image)
+{
+       if(ImageTypeDeclaration *image1 = multi_visit(image))
+       {
+               if(image1->dimensions!=image.dimensions || image1->array!=image.array)
+                       r_result = false;
+               else if(image1->sampled!=image.sampled || image1->shadow!=image.shadow)
+                       r_result = false;
+               else if(image1->base_type && image.base_type)
+                       compare(*image1->base_type, *image.base_type);
+               else
+                       r_result = (!image1->base_type && !image.base_type);
+       }
+}
+
+void TypeComparer::visit(StructDeclaration &strct)
+{
+       if(StructDeclaration *strct1 = multi_visit(strct))
+       {
+               if(strct1->members.body.size()!=strct.members.body.size())
+                       r_result = false;
+               else
+               {
+                       r_result = true;
+                       NodeList<Statement>::const_iterator i = strct1->members.body.begin();
+                       NodeList<Statement>::const_iterator j = strct.members.body.begin();
+                       for(; (r_result && i!=strct1->members.body.end()); ++i, ++j)
+                               compare(**i, **j);
+               }
+       }
+}
+
+void TypeComparer::visit(VariableDeclaration &var)
+{
+       if(VariableDeclaration *var1 = multi_visit(var))
+       {
+               if(var1->name!=var.name || var1->array!=var.array)
+                       r_result = false;
+               else if(!var1->type_declaration || !var.type_declaration)
+                       r_result = false;
+               else
+               {
+                       if(var1->array)
+                       {
+                               r_result = false;
+                               if(var1->array_size && var.array_size)
+                                       compare(*var1->array_size, *var.array_size);
+                       }
+                       if(r_result && var1->type_declaration!=var.type_declaration)
+                               compare(*var1->type_declaration, *var.type_declaration);
+                       // TODO Compare layout qualifiers for interface block members
+               }
+       }
+}
+
+
 LocationCounter::LocationCounter():
        r_count(0)
 { }
@@ -135,7 +319,10 @@ void DependencyCollector::visit(InterfaceBlockReference &iface)
 void DependencyCollector::visit(FunctionCall &call)
 {
        if(call.declaration)
+       {
                dependencies.insert(call.declaration);
+               call.declaration->visit(*this);
+       }
        TraversingVisitor::visit(call);
 }
 
@@ -151,6 +338,15 @@ void DependencyCollector::visit(VariableDeclaration &var)
        TraversingVisitor::visit(var);
 }
 
+void DependencyCollector::visit(FunctionDeclaration &func)
+{
+       if(!visited_functions.count(&func))
+       {
+               visited_functions.insert(&func);
+               TraversingVisitor::visit(func);
+       }
+}
+
 } // namespace SL
 } // namespace GL
 } // namespace Msp