diff options
author | Eric Anholt <[email protected]> | 2012-03-19 14:04:23 -0700 |
---|---|---|
committer | Eric Anholt <[email protected]> | 2012-04-13 17:01:03 -0700 |
commit | d6e6566206029ace72ba037a3ef7950876eeb88b (patch) | |
tree | 1b95dbeb4487588918af6773a6a4f9a435126e12 /src/glsl | |
parent | 599aac95ff2149d881177ed75a48d97d3dcf47bd (diff) |
glsl: Let ir_builder expressions take un-dereferenced variables.
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 <[email protected]>
Diffstat (limited to 'src/glsl')
-rw-r--r-- | src/glsl/ir_builder.cpp | 19 | ||||
-rw-r--r-- | src/glsl/ir_builder.h | 37 |
2 files changed, 40 insertions, 16 deletions
diff --git a/src/glsl/ir_builder.cpp b/src/glsl/ir_builder.cpp index a9d87145735..a867eb49e79 100644 --- a/src/glsl/ir_builder.cpp +++ b/src/glsl/ir_builder.cpp @@ -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)), diff --git a/src/glsl/ir_builder.h b/src/glsl/ir_builder.h index 5d6f476a27c..78b119dafee 100644 --- a/src/glsl/ir_builder.h +++ b/src/glsl/ir_builder.h @@ -25,11 +25,36 @@ 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 */ |