]> git.tdb.fi Git - libs/gl.git/blobdiff - source/glsl/generate.cpp
Implement the ternary operator in GLSL
[libs/gl.git] / source / glsl / generate.cpp
index 09a776afb7f60bb61d2bdc47d0a8f3fb9e470b3e..e5855e94cacb701c4ab96bec16324335fcccf910 100644 (file)
@@ -191,6 +191,9 @@ void TypeResolver::visit(BasicTypeDeclaration &type)
                        if(basic_base->kind==BasicTypeDeclaration::VECTOR)
                        {
                                type.kind = BasicTypeDeclaration::MATRIX;
+                               /* A matrix's base type is its column vector type.  This will put
+                               the column vector's size, i.e. the matrix's row count, in the high
+                               half of the size. */
                                type.size |= basic_base->size<<16;
                        }
 
@@ -869,6 +872,36 @@ void ExpressionResolver::visit(Assignment &assign)
        resolve(assign, assign.left->type, true);
 }
 
+void ExpressionResolver::visit(TernaryExpression &ternary)
+{
+       TraversingVisitor::visit(ternary);
+
+       BasicTypeDeclaration *basic_cond = dynamic_cast<BasicTypeDeclaration *>(ternary.condition->type);
+       if(!basic_cond || basic_cond->kind!=BasicTypeDeclaration::BOOL)
+               return;
+
+       TypeDeclaration *type = 0;
+       if(ternary.true_expr->type==ternary.false_expr->type)
+               type = ternary.true_expr->type;
+       else
+       {
+               BasicTypeDeclaration *basic_true = dynamic_cast<BasicTypeDeclaration *>(ternary.true_expr->type);
+               BasicTypeDeclaration *basic_false = dynamic_cast<BasicTypeDeclaration *>(ternary.false_expr->type);
+               Compatibility compat = get_compatibility(*basic_true, *basic_false);
+               if(compat==NOT_COMPATIBLE)
+                       return;
+
+               type = (compat==LEFT_CONVERTIBLE ? basic_true : basic_false);
+
+               if(compat==LEFT_CONVERTIBLE)
+                       convert_to(ternary.true_expr, *basic_false);
+               else if(compat==RIGHT_CONVERTIBLE)
+                       convert_to(ternary.false_expr, *basic_true);
+       }
+
+       resolve(ternary, type, false);
+}
+
 void ExpressionResolver::visit(FunctionCall &call)
 {
        TraversingVisitor::visit(call);
@@ -1037,6 +1070,9 @@ VariableDeclaration *InterfaceGenerator::generate_interface(VariableDeclaration
        if(stage->content.variables.count(name))
                return 0;
 
+       if(stage->type==Stage::GEOMETRY && !copy_block && var.interface=="out" && var.array)
+               return 0;
+
        VariableDeclaration* iface_var = new VariableDeclaration;
        iface_var->sampling = var.sampling;
        iface_var->interface = iface;
@@ -1059,6 +1095,8 @@ VariableDeclaration *InterfaceGenerator::generate_interface(VariableDeclaration
 
        iface_target_block->body.insert(iface_insert_point, iface_var);
        iface_target_block->variables.insert(make_pair(name, iface_var));
+       if(iface_target_block==&stage->content && iface=="in")
+               declared_inputs.push_back(iface_var);
 
        return iface_var;
 }
@@ -1134,8 +1172,14 @@ void InterfaceGenerator::visit(VariableReference &var)
                i = prev_vars.find(in_prefix+var.name);
        if(i!=prev_vars.end() && i->second->interface=="out")
        {
-               generate_interface(*i->second, "in", i->second->name);
-               var.name = i->second->name;
+               if(stage->type==Stage::GEOMETRY && i->second->array)
+                       stage->diagnostics.push_back(Diagnostic(Diagnostic::WARN, var.source, var.line,
+                               format("Can't access '%s' through automatic interface because it's an array", var.name)));
+               else
+               {
+                       generate_interface(*i->second, "in", i->second->name);
+                       var.name = i->second->name;
+               }
                return;
        }
 
@@ -1185,11 +1229,13 @@ void InterfaceGenerator::visit(VariableDeclaration &var)
                        }
                }
        }
-       else if(var.interface=="in")
+       else if(var.interface=="in" && current_block==&stage->content)
        {
+               declared_inputs.push_back(&var);
+
                /* Try to link input variables in global scope with output variables from
                previous stage. */
-               if(current_block==&stage->content && !var.linked_declaration && stage->previous)
+               if(!var.linked_declaration && stage->previous)
                {
                        const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
                        map<string, VariableDeclaration *>::const_iterator i = prev_vars.find(var.name);
@@ -1234,12 +1280,8 @@ void InterfaceGenerator::visit(FunctionDeclaration &func)
 
 void InterfaceGenerator::visit(Passthrough &pass)
 {
-       vector<VariableDeclaration *> pass_vars;
-
-       // Pass through all input variables of this stage.
-       for(map<string, VariableDeclaration *>::const_iterator i=stage->content.variables.begin(); i!=stage->content.variables.end(); ++i)
-               if(i->second->interface=="in")
-                       pass_vars.push_back(i->second);
+       // Pass through all input variables declared so far.
+       vector<VariableDeclaration *> pass_vars = declared_inputs;
 
        if(stage->previous)
        {