Mesa (master): glsl: Implement ast-to-hir for binary shifts in GLSL 1.30

Kenneth Graunke kwg at kemper.freedesktop.org
Fri Oct 15 07:20:50 UTC 2010


Module: Mesa
Branch: master
Commit: 5c4c36f7f3842e287b303b1eca8d260c37e3580b
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=5c4c36f7f3842e287b303b1eca8d260c37e3580b

Author: Chad Versace <chad at chad-versace.us>
Date:   Fri Oct  8 16:22:28 2010 -0700

glsl: Implement ast-to-hir for binary shifts in GLSL 1.30

Implement by adding the following cases to ast_expression::hir():
    - ast_lshift
    - ast_rshift
Also, implement ir validation for the new operators by adding the following
cases to ir_validate::visit_leave():
    - ir_binop_lshift
    - ir_binop_rshift

---

 src/glsl/ast_to_hir.cpp  |   61 +++++++++++++++++++++++++++++++++++++++++++--
 src/glsl/ir_validate.cpp |   13 ++++++++++
 2 files changed, 71 insertions(+), 3 deletions(-)

diff --git a/src/glsl/ast_to_hir.cpp b/src/glsl/ast_to_hir.cpp
index 6e8a1fd..b37fcbd 100644
--- a/src/glsl/ast_to_hir.cpp
+++ b/src/glsl/ast_to_hir.cpp
@@ -748,9 +748,64 @@ ast_expression::hir(exec_list *instructions,
 
    case ast_lshift:
    case ast_rshift:
-      _mesa_glsl_error(& loc, state, "FINISHME: implement bit-shift operators");
-      error_emitted = true;
-      break;
+       if (state->language_version < 130) {
+          _mesa_glsl_error(&loc, state, "operator %s requires GLSL 1.30",
+              operator_string(this->oper));
+          error_emitted = true;
+          break;
+       }
+
+       /* From page 50 (page 56 of the PDF) of the GLSL 1.30 spec:
+        *
+        *     The shift operators (<<) and (>>). For both operators, the operands
+        *     must be signed or unsigned integers or integer vectors. One operand
+        *     can be signed while the other is unsigned. In all cases, the
+        *     resulting type will be the same type as the left operand. If the
+        *     first operand is a scalar, the second operand has to be a scalar as
+        *     well. If the first operand is a vector, the second operand must be
+        *     a scalar or a vector, [...]
+        */
+
+       op[0] = this->subexpressions[0]->hir(instructions, state);
+       op[1] = this->subexpressions[1]->hir(instructions, state);
+
+       if (!op[0]->type->is_integer()) {
+           _mesa_glsl_error(& loc, state,
+               "LHS of operator %s must be an integer or integer vector",
+               operator_string(this->oper));
+           error_emitted = true;
+           break;
+       }
+       if (!op[1]->type->is_integer()) {
+           _mesa_glsl_error(& loc, state,
+               "RHS of operator %s must be an integer or integer vector",
+               operator_string(this->oper));
+           error_emitted = true;
+           break;
+       }
+       if (op[0]->type->is_scalar() && !op[1]->type->is_scalar()) {
+           _mesa_glsl_error(& loc, state,
+               "If the first operand of %s is scalar, the second must be"
+               "scalar as well", operator_string(this->oper));
+           error_emitted = true;
+           break;
+       }
+       if (op[0]->type->is_vector() &&
+           op[1]->type->is_vector() &&
+           op[0]->type->components() != op[1]->type->components()) {
+
+           _mesa_glsl_error(& loc, state,
+               "Vector operands of %s must have same number of components",
+               operator_string(this->oper));
+           error_emitted = true;
+           break;
+       }
+
+       type = op[0]->type;
+       result = new(ctx) ir_expression(operations[this->oper], type,
+                                       op[0], op[1]);
+       error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
+       break;
 
    case ast_less:
    case ast_greater:
diff --git a/src/glsl/ir_validate.cpp b/src/glsl/ir_validate.cpp
index 70fb939..db1ffb2 100644
--- a/src/glsl/ir_validate.cpp
+++ b/src/glsl/ir_validate.cpp
@@ -331,6 +331,19 @@ ir_validate::visit_leave(ir_expression *ir)
 
    case ir_binop_lshift:
    case ir_binop_rshift:
+      assert(ir->operands[0]->type->is_integer() &&
+             ir->operands[1]->type->is_integer());
+      if (ir->operands[0]->type->is_scalar()) {
+          assert(ir->operands[1]->type->is_scalar());
+      }
+      if (ir->operands[0]->type->is_vector() &&
+          ir->operands[1]->type->is_vector()) {
+          assert(ir->operands[0]->type->components() ==
+                 ir->operands[1]->type->components());
+      }
+      assert(ir->type == ir->operands[0]->type);
+      break;
+
    case ir_binop_bit_and:
    case ir_binop_bit_xor:
    case ir_binop_bit_or:




More information about the mesa-commit mailing list