ir_constant_expression: Add support for the "distance" builtin.
authorKenneth Graunke <kenneth@whitecape.org>
Thu, 22 Jul 2010 01:01:22 +0000 (18:01 -0700)
committerKenneth Graunke <kenneth@whitecape.org>
Wed, 28 Jul 2010 22:46:27 +0000 (15:46 -0700)
src/glsl/ir_constant_expression.cpp

index e68a7b1a2a206743f1ed15fcccf66da267d7901c..52f53d9ff486ffb62ebda40126b3effc101e44d3 100644 (file)
@@ -854,7 +854,13 @@ ir_call::constant_expression_value()
       for (unsigned c = 0; c < op[0]->type->components(); c++)
         data.f[c] = 180.0/M_PI * op[0]->value.f[c];
    } else if (strcmp(callee, "distance") == 0) {
-      return NULL; /* FINISHME: implement this */
+      assert(op[0]->type->is_float() && op[1]->type->is_float());
+      float length_squared = 0.0;
+      for (unsigned c = 0; c < op[0]->type->components(); c++) {
+        float t = op[0]->value.f[c] - op[1]->value.f[c];
+        length_squared += t * t;
+      }
+      return new(mem_ctx) ir_constant(sqrtf(length_squared));
    } else if (strcmp(callee, "dot") == 0) {
       expr = new(mem_ctx) ir_expression(ir_binop_dot, type, op[0], op[1]);
    } else if (strcmp(callee, "equal") == 0) {