[Mesa-dev] [PATCH 3/7] glsl: Let ir_builder expressions take un-dereferenced variables.

Eric Anholt eric at anholt.net
Mon Mar 26 12:42:39 PDT 2012


Having to explicitly dereference is irritating and bloats the code,
when we can detect and do the right thing (at the expense of type
safety, given that we're taking an ir_instruction to manage this
instead of ir_rvalue).
---
 src/glsl/ir_builder.cpp              |    8 ++++++++
 src/mesa/main/ff_fragment_shader.cpp |   19 ++++++-------------
 2 files changed, 14 insertions(+), 13 deletions(-)

diff --git a/src/glsl/ir_builder.cpp b/src/glsl/ir_builder.cpp
index 039ee1b..03dbd8b 100644
--- a/src/glsl/ir_builder.cpp
+++ b/src/glsl/ir_builder.cpp
@@ -33,6 +33,14 @@ expr(ir_expression_operation op,
 {
    void *mem_ctx = ralloc_parent(a);
 
+   ir_variable *a_var = a->as_variable();
+   if (a_var)
+      a = new(mem_ctx) ir_dereference_variable(a_var);
+
+   ir_variable *b_var = a->as_variable();
+   if (b_var)
+      b = new(mem_ctx) ir_dereference_variable(b_var);
+
    return new(mem_ctx) ir_expression(op, a->as_rvalue(), b->as_rvalue());
 }
 
diff --git a/src/mesa/main/ff_fragment_shader.cpp b/src/mesa/main/ff_fragment_shader.cpp
index cfee334..2d56d3b 100644
--- a/src/mesa/main/ff_fragment_shader.cpp
+++ b/src/mesa/main/ff_fragment_shader.cpp
@@ -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);
-- 
1.7.9.1



More information about the mesa-dev mailing list