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

index a2748a7..b6cb2d8 100644 (file)
@@ -1064,7 +1064,21 @@ ir_call::constant_expression_value()
       for (unsigned c = 0; c < op[0]->type->components(); c++)
         data.f[c] = sinhf(op[0]->value.f[c]);
    } else if (strcmp(callee, "smoothstep") == 0) {
-      return NULL; /* FINISHME: implement this */
+      assert(num_parameters == 3);
+      assert(op[1]->type == op[0]->type);
+      unsigned edge_inc = op[0]->type->is_scalar() ? 0 : 1;
+      for (unsigned c = 0, e = 0; c < type->components(); e += edge_inc, c++) {
+        const float edge0 = op[0]->value.f[e];
+        const float edge1 = op[1]->value.f[e];
+        if (edge0 == edge1) {
+           data.f[c] = 0.0; /* Avoid a crash - results are undefined anyway */
+        } else {
+           const float numerator = op[2]->value.f[c] - edge0;
+           const float denominator = edge1 - edge0;
+           const float t = CLAMP(numerator/denominator, 0, 1);
+           data.f[c] = t * t * (3 - 2 * t);
+        }
+      }
    } else if (strcmp(callee, "sqrt") == 0) {
       expr = new(mem_ctx) ir_expression(ir_unop_sqrt, type, op[0], NULL);
    } else if (strcmp(callee, "step") == 0) {