glsl: Let ir_builder expressions take un-dereferenced variables.
authorEric Anholt <eric@anholt.net>
Mon, 19 Mar 2012 21:04:23 +0000 (14:04 -0700)
committerEric Anholt <eric@anholt.net>
Sat, 14 Apr 2012 00:01:03 +0000 (17:01 -0700)
Having to explicitly dereference is irritating and bloats the code,
when the compiler can detect and do the right thing.

v2: Use a little shim class to produce the automatic dereference
    generation at compile time as opposed to runtime, while also
    allowing compile-time type checking.

Reviewed-by: Kenneth Graunke <kenneth@whitecape.org>
src/glsl/ir_builder.cpp
src/glsl/ir_builder.h
src/mesa/main/ff_fragment_shader.cpp

index a9d87145735aa69a3e6b393e43c48aaf81cb8511..a867eb49e79c753248db302ad5101e8a9115b71c 100644 (file)
@@ -28,38 +28,37 @@ using namespace ir_builder;
 namespace ir_builder {
 
 ir_expression *
-expr(ir_expression_operation op,
-     ir_rvalue *a, ir_rvalue *b)
+expr(ir_expression_operation op, operand a, operand b)
 {
-   void *mem_ctx = ralloc_parent(a);
+   void *mem_ctx = ralloc_parent(a.val);
 
-   return new(mem_ctx) ir_expression(op, a, b);
+   return new(mem_ctx) ir_expression(op, a.val, b.val);
 }
 
-ir_expression *add(ir_rvalue *a, ir_rvalue *b)
+ir_expression *add(operand a, operand b)
 {
    return expr(ir_binop_add, a, b);
 }
 
-ir_expression *sub(ir_rvalue *a, ir_rvalue *b)
+ir_expression *sub(operand a, operand b)
 {
    return expr(ir_binop_sub, a, b);
 }
 
-ir_expression *mul(ir_rvalue *a, ir_rvalue *b)
+ir_expression *mul(operand a, operand b)
 {
    return expr(ir_binop_mul, a, b);
 }
 
-ir_expression *dot(ir_rvalue *a, ir_rvalue *b)
+ir_expression *dot(operand a, operand b)
 {
    return expr(ir_binop_dot, a, b);
 }
 
 ir_expression *
-saturate(ir_rvalue *a)
+saturate(operand a)
 {
-   void *mem_ctx = ralloc_parent(a);
+   void *mem_ctx = ralloc_parent(a.val);
 
    return expr(ir_binop_max,
               expr(ir_binop_min, a, new(mem_ctx) ir_constant(1.0f)),
index 5d6f476a27cbfe88c9390b150b7df6a534eeaffc..78b119dafeebf9df88aaf2e86bd6689cd560aa9c 100644 (file)
 
 namespace ir_builder {
 
-ir_expression *expr(ir_expression_operation op, ir_rvalue *a, ir_rvalue *b);
-ir_expression *add(ir_rvalue *a, ir_rvalue *b);
-ir_expression *sub(ir_rvalue *a, ir_rvalue *b);
-ir_expression *mul(ir_rvalue *a, ir_rvalue *b);
-ir_expression *dot(ir_rvalue *a, ir_rvalue *b);
-ir_expression *saturate(ir_rvalue *a);
+/**
+ * This little class exists to let the helper expression generators
+ * take either an ir_rvalue * or an ir_variable * to be automatically
+ * dereferenced, while still providing compile-time type checking.
+ *
+ * You don't have to explicitly call the constructor -- C++ will see
+ * that you passed an ir_variable, and silently call the
+ * operand(ir_variable *var) constructor behind your back.
+ */
+class operand {
+public:
+   operand(ir_rvalue *val)
+      : val(val)
+   {
+   }
+
+   operand(ir_variable *var)
+   {
+      void *mem_ctx = ralloc_parent(var);
+      val = new(mem_ctx) ir_dereference_variable(var);
+   }
+
+   ir_rvalue *val;
+};
+
+ir_expression *expr(ir_expression_operation op, operand a, operand b);
+ir_expression *add(operand a, operand b);
+ir_expression *sub(operand a, operand b);
+ir_expression *mul(operand a, operand b);
+ir_expression *dot(operand a, operand b);
+ir_expression *saturate(operand a);
 
 } /* namespace ir_builder */
index cfee334472109d870dd89b5863d978c0106c3c90..2d56d3baff25be52e9f772b097c4c6d5c8820086 100644 (file)
@@ -1112,13 +1112,10 @@ load_texunit_bumpmap( struct texenv_fragment_program *p, GLuint unit )
    GLuint bumpedUnitNr = key->unit[unit].OptRGB[1].Source - SRC_TEXTURE0;
    ir_rvalue *bump;
    ir_rvalue *texcoord;
-   ir_variable *rot_mat_0_var, *rot_mat_1_var;
-   ir_dereference_variable *rot_mat_0, *rot_mat_1;
+   ir_variable *rot_mat_0, *rot_mat_1;
 
-   rot_mat_0_var = p->shader->symbols->get_variable("gl_BumpRotMatrix0MESA");
-   rot_mat_1_var = p->shader->symbols->get_variable("gl_BumpRotMatrix1MESA");
-   rot_mat_0 = new(p->mem_ctx) ir_dereference_variable(rot_mat_0_var);
-   rot_mat_1 = new(p->mem_ctx) ir_dereference_variable(rot_mat_1_var);
+   rot_mat_0 = p->shader->symbols->get_variable("gl_BumpRotMatrix0MESA");
+   rot_mat_1 = p->shader->symbols->get_variable("gl_BumpRotMatrix1MESA");
 
    ir_variable *tc_array = p->shader->symbols->get_variable("gl_TexCoord");
    assert(tc_array);
@@ -1262,9 +1259,7 @@ emit_fog_instructions(struct texenv_fragment_program *p,
       ir_assignment *assign = new(p->mem_ctx) ir_assignment(temp, f);
       p->instructions->push_tail(assign);
 
-      f = new(p->mem_ctx) ir_dereference_variable(temp_var);
-      temp = new(p->mem_ctx) ir_dereference_variable(temp_var);
-      f = mul(f, temp);
+      f = mul(temp_var, temp_var);
       f = new(p->mem_ctx) ir_expression(ir_unop_neg, f);
       f = new(p->mem_ctx) ir_expression(ir_unop_exp2, f);
       break;
@@ -1276,15 +1271,13 @@ emit_fog_instructions(struct texenv_fragment_program *p,
    assign = new(p->mem_ctx) ir_assignment(temp, f);
    p->instructions->push_tail(assign);
 
-   f = new(p->mem_ctx) ir_dereference_variable(f_var);
-   f = sub(new(p->mem_ctx) ir_constant(1.0f), f);
+   f = sub(new(p->mem_ctx) ir_constant(1.0f), f_var);
    temp = new(p->mem_ctx) ir_dereference_variable(params);
    temp = new(p->mem_ctx) ir_dereference_record(temp, "color");
    temp = new(p->mem_ctx) ir_swizzle(temp, 0, 1, 2, 3, 3);
    temp = mul(temp, f);
 
-   f = new(p->mem_ctx) ir_dereference_variable(f_var);
-   f = add(temp, mul(fragcolor, f));
+   f = add(temp, mul(fragcolor, f_var));
 
    ir_dereference *deref = new(p->mem_ctx) ir_dereference_variable(fog_result);
    assign = new(p->mem_ctx) ir_assignment(deref, f, NULL, WRITEMASK_XYZ);