ir_constant_visitor: Handle constant swizzles
authorIan Romanick <ian.d.romanick@intel.com>
Fri, 11 Jun 2010 19:30:28 +0000 (12:30 -0700)
committerIan Romanick <ian.d.romanick@intel.com>
Fri, 11 Jun 2010 22:43:59 +0000 (15:43 -0700)
ir_constant_expression.cpp

index a7c4fe6382e78b1697ab5c94d4cb7ae64b266ffb..781166a8a262d4e8c8c9356a50b54672af88880d 100644 (file)
@@ -538,8 +538,33 @@ ir_constant_visitor::visit(ir_texture *ir)
 void
 ir_constant_visitor::visit(ir_swizzle *ir)
 {
-   (void) ir;
-   value = NULL;
+   ir_constant *v = ir->val->constant_expression_value();
+
+   this->value = NULL;
+
+   if (v != NULL) {
+      union {
+        float f[4];
+        unsigned u[4];
+        bool b[4];
+      } data;
+
+      const unsigned swiz_idx[4] = {
+        ir->mask.x, ir->mask.y, ir->mask.z, ir->mask.w
+      };
+
+      for (unsigned i = 0; i < ir->mask.num_components; i++) {
+        switch (v->type->base_type) {
+        case GLSL_TYPE_UINT:
+        case GLSL_TYPE_INT:   data.u[i] = v->value.u[swiz_idx[i]]; break;
+        case GLSL_TYPE_FLOAT: data.f[i] = v->value.f[swiz_idx[i]]; break;
+        case GLSL_TYPE_BOOL:  data.b[i] = v->value.b[swiz_idx[i]]; break;
+        default:              assert(!"Should not get here."); break;
+        }
+      }
+
+      this->value = new ir_constant(ir->type, &data);
+   }
 }