glcpp: Allow vertical tab and form feed characters in GLSL
[mesa.git] / src / compiler / glsl / ir_expression_operation.py
index 5c899a743ee1ee8e6bce927acdd4be319472e04f..58a585b741d23269d2fdc55a477e82c8be8c8bc9 100644 (file)
@@ -90,71 +90,12 @@ signed_numeric_types = (int_type, float_type, double_type)
 integer_types = (uint_type, int_type)
 real_types = (float_type, double_type)
 
-# This template is for unary and binary operations that can only have operands
-# of a single type or the implementation for all types is identical.
-# ir_unop_logic_not is an example of the former, and ir_quadop_bitfield_insert
-# is an example of the latter..
-constant_template0 = mako.template.Template("""\
-   case ${op.get_enum_name()}:
-    % if len(op.source_types) == 1:
-      assert(op[0]->type->base_type == ${op.source_types[0].glsl_type});
-    % endif
-      for (unsigned c = 0; c < op[0]->type->components(); c++)
-    % for dst_type, src_types in op.signatures():
-        % if loop.index == 0:
-         data.${dst_type.union_field}[c] = ${op.get_c_expression(src_types)};
-        % endif
-    % endfor
-      break;""")
-
-# This template is for unary operations that can have operands of a several
-# different types.  ir_unop_bit_not is an example.
-constant_template1 = mako.template.Template("""\
-   case ${op.get_enum_name()}:
-      switch (op[0]->type->base_type) {
-    % for dst_type, src_types in op.signatures():
-      case ${src_types[0].glsl_type}:
-         for (unsigned c = 0; c < op[0]->type->components(); c++)
-            data.${dst_type.union_field}[c] = ${op.get_c_expression(src_types)};
-         break;
-    % endfor
-      default:
-         assert(0);
-      }
-      break;""")
-
-# This template is for unary operations that can have operands of a several
-# different types, and each type has a different C expression.  ir_unop_neg is
-# an example.
-constant_template3 = mako.template.Template("""\
+# This template is for operations that can have operands of a several
+# different types, and each type may or may not has a different C expression.
+# This is used by most operations.
+constant_template_common = mako.template.Template("""\
    case ${op.get_enum_name()}:
       for (unsigned c = 0; c < op[0]->type->components(); c++) {
-         switch (this->type->base_type) {
-    % for dst_type, src_types in op.signatures():
-         case ${src_types[0].glsl_type}:
-            data.${dst_type.union_field}[c] = ${op.get_c_expression(src_types)};
-            break;
-    % endfor
-         default:
-            assert(0);
-         }
-      }
-      break;""")
-
-# This template is for unary operations that map an operand of one type to an
-# operand of another type.  ir_unop_f2b is an example.
-constant_template2 = mako.template.Template("""\
-   case ${op.get_enum_name()}:
-      assert(op[0]->type->base_type == ${op.source_types[0].glsl_type});
-      for (unsigned c = 0; c < op[0]->type->components(); c++)
-         data.${op.dest_type.union_field}[c] = ${op.get_c_expression(op.source_types)};
-      break;""")
-
-# This template is for operations with an output type that doesn't match the
-# input types.
-constant_template5 = mako.template.Template("""\
-   case ${op.get_enum_name()}:
-      for (unsigned c = 0; c < components; c++) {
          switch (op[0]->type->base_type) {
     % for dst_type, src_types in op.signatures():
          case ${src_types[0].glsl_type}:
@@ -162,7 +103,7 @@ constant_template5 = mako.template.Template("""\
             break;
     % endfor
          default:
-            assert(0);
+            unreachable("invalid type");
          }
       }
       break;""")
@@ -193,7 +134,7 @@ constant_template_vector_scalar = mako.template.Template("""\
             break;
     % endfor
          default:
-            assert(0);
+            unreachable("invalid type");
          }
       }
       break;""")
@@ -216,7 +157,7 @@ constant_template_mul = mako.template.Template("""\
                break;
     % endfor
             default:
-               assert(0);
+               unreachable("invalid type");
             }
          }
       } else {
@@ -274,7 +215,7 @@ constant_template_horizontal = mako.template.Template("""\
          break;
     % endfor
       default:
-         assert(0);
+         unreachable("invalid type");
       }
       break;""")
 
@@ -291,7 +232,7 @@ constant_template_vector_extract = mako.template.Template("""\
          break;
     % endfor
       default:
-         assert(0);
+         unreachable("invalid type");
       }
       break;
    }""")
@@ -310,8 +251,7 @@ constant_template_vector_insert = mako.template.Template("""\
          break;
     % endfor
       default:
-         assert(!"Should not get here.");
-         break;
+         unreachable("invalid type");
       }
       break;
    }""")
@@ -327,7 +267,50 @@ constant_template_vector = mako.template.Template("""\
             break;
     % endfor
          default:
-            assert(0);
+            unreachable("invalid type");
+         }
+      }
+      break;""")
+
+# This template is for ir_triop_lrp.
+constant_template_lrp = mako.template.Template("""\
+   case ${op.get_enum_name()}: {
+      assert(op[0]->type->base_type == GLSL_TYPE_FLOAT ||
+             op[0]->type->base_type == GLSL_TYPE_DOUBLE);
+      assert(op[1]->type->base_type == GLSL_TYPE_FLOAT ||
+             op[1]->type->base_type == GLSL_TYPE_DOUBLE);
+      assert(op[2]->type->base_type == GLSL_TYPE_FLOAT ||
+             op[2]->type->base_type == GLSL_TYPE_DOUBLE);
+
+      unsigned c2_inc = op[2]->type->is_scalar() ? 0 : 1;
+      for (unsigned c = 0, c2 = 0; c < components; c2 += c2_inc, c++) {
+         switch (this->type->base_type) {
+    % for dst_type, src_types in op.signatures():
+         case ${src_types[0].glsl_type}:
+            data.${dst_type.union_field}[c] = ${op.get_c_expression(src_types, ("c", "c", "c2"))};
+            break;
+    % endfor
+         default:
+            unreachable("invalid type");
+         }
+      }
+      break;
+   }""")
+
+# This template is for ir_triop_csel.  This expression is really unique
+# because not all of the operands are the same type, and the second operand
+# determines the type of the expression (instead of the first).
+constant_template_csel = mako.template.Template("""\
+   case ${op.get_enum_name()}:
+      for (unsigned c = 0; c < components; c++) {
+         switch (this->type->base_type) {
+    % for dst_type, src_types in op.signatures():
+         case ${src_types[1].glsl_type}:
+            data.${dst_type.union_field}[c] = ${op.get_c_expression(src_types)};
+            break;
+    % endfor
+         default:
+            unreachable("invalid type");
          }
       }
       break;""")
@@ -374,64 +357,47 @@ class operation(object):
 
 
    def get_enum_name(self):
-      return "ir_{}op_{}".format(("un", "bin", "tri", "quad")[self.num_operands-1], self.name)
+      return "ir_{0}op_{1}".format(("un", "bin", "tri", "quad")[self.num_operands-1], self.name)
 
 
    def get_template(self):
       if self.c_expression is None:
          return None
 
-      if self.num_operands == 1:
-         if horizontal_operation in self.flags and non_assign_operation in self.flags:
+      if horizontal_operation in self.flags:
+         if non_assign_operation in self.flags:
             return constant_template_horizontal_nonassignment.render(op=self)
-         elif horizontal_operation in self.flags:
+         elif types_identical_operation in self.flags:
             return constant_template_horizontal_single_implementation.render(op=self)
-         elif self.dest_type is not None and len(self.source_types) == 1:
-            return constant_template2.render(op=self)
-         elif self.dest_type is not None:
-            return constant_template5.render(op=self)
-         elif len(self.source_types) == 1:
-            return constant_template0.render(op=self)
-         elif len(self.c_expression) == 1 and 'default' in self.c_expression:
-            return constant_template1.render(op=self)
          else:
-            return constant_template3.render(op=self)
-      elif self.num_operands == 2:
+            return constant_template_horizontal.render(op=self)
+
+      if self.num_operands == 2:
          if self.name == "mul":
             return constant_template_mul.render(op=self)
          elif self.name == "vector_extract":
             return constant_template_vector_extract.render(op=self)
          elif vector_scalar_operation in self.flags:
             return constant_template_vector_scalar.render(op=self)
-         elif horizontal_operation in self.flags and types_identical_operation in self.flags:
-            return constant_template_horizontal_single_implementation.render(op=self)
-         elif horizontal_operation in self.flags:
-            return constant_template_horizontal.render(op=self)
-         elif len(self.source_types) == 1:
-            return constant_template0.render(op=self)
-         elif self.dest_type is not None:
-            return constant_template5.render(op=self)
-         else:
-            return constant_template3.render(op=self)
       elif self.num_operands == 3:
          if self.name == "vector_insert":
             return constant_template_vector_insert.render(op=self)
-         else:
-            return constant_template3.render(op=self)
+         elif self.name == "lrp":
+            return constant_template_lrp.render(op=self)
+         elif self.name == "csel":
+            return constant_template_csel.render(op=self)
       elif self.num_operands == 4:
          if self.name == "vector":
             return constant_template_vector.render(op=self)
-         elif types_identical_operation in self.flags:
-            return constant_template0.render(op=self)
 
-      return None
+      return constant_template_common.render(op=self)
 
 
    def get_c_expression(self, types, indices=("c", "c", "c")):
-      src0 = "op[0]->value.{}[{}]".format(types[0].union_field, indices[0])
-      src1 = "op[1]->value.{}[{}]".format(types[1].union_field, indices[1]) if len(types) >= 2 else "ERROR"
-      src2 = "op[2]->value.{}[{}]".format(types[2].union_field, indices[2]) if len(types) >= 3 else "ERROR"
-      src3 = "op[3]->value.{}[c]".format(types[3].union_field) if len(types) >= 4 else "ERROR"
+      src0 = "op[0]->value.{0}[{1}]".format(types[0].union_field, indices[0])
+      src1 = "op[1]->value.{0}[{1}]".format(types[1].union_field, indices[1]) if len(types) >= 2 else "ERROR"
+      src2 = "op[2]->value.{0}[{1}]".format(types[2].union_field, indices[2]) if len(types) >= 3 else "ERROR"
+      src3 = "op[3]->value.{0}[c]".format(types[3].union_field) if len(types) >= 4 else "ERROR"
 
       expr = self.c_expression[types[0].union_field] if types[0].union_field in self.c_expression else self.c_expression['default']
 
@@ -665,7 +631,7 @@ ir_expression_operation = [
    # Fused floating-point multiply-add, part of ARB_gpu_shader5.
    operation("fma", 3, source_types=real_types, c_expression="{src0} * {src1} + {src2}"),
 
-   operation("lrp", 3),
+   operation("lrp", 3, source_types=real_types, c_expression={'f': "{src0} * (1.0f - {src2}) + ({src1} * {src2})", 'd': "{src0} * (1.0 - {src2}) + ({src1} * {src2})"}),
 
    # Conditional Select
    #
@@ -673,7 +639,9 @@ ir_expression_operation = [
    # component on vectors).
    #
    # See also lower_instructions_visitor::ldexp_to_arith
-   operation("csel", 3),
+   operation("csel", 3,
+             all_signatures=zip(all_types, zip(len(all_types) * (bool_type,), all_types, all_types)),
+             c_expression="{src0} ? {src1} : {src2}"),
 
    operation("bitfield_extract", 3,
              all_signatures=((int_type, (uint_type, int_type, int_type)),
@@ -691,8 +659,7 @@ ir_expression_operation = [
    operation("bitfield_insert", 4,
              all_signatures=((uint_type, (uint_type, uint_type, int_type, int_type)),
                              (int_type, (int_type, int_type, int_type, int_type))),
-             c_expression="bitfield_insert({src0}, {src1}, {src2}, {src3})",
-             flags=types_identical_operation),
+             c_expression="bitfield_insert({src0}, {src1}, {src2}, {src3})"),
 
    operation("vector", 4, source_types=all_types, c_expression="anything-except-None"),
 ]
@@ -740,6 +707,12 @@ const char *const ir_expression_operation_strings[] = {
 % for item in values:
    "${item.printable_name}",
 % endfor
+};
+
+const char *const ir_expression_operation_enum_strings[] = {
+% for item in values:
+   "${item.name}",
+% endfor
 };""")
 
    constant_template = mako.template.Template("""\