2 * Copyright © 2010 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
25 * \file lower_mat_op_to_vec.cpp
27 * Breaks matrix operation expressions down to a series of vector operations.
29 * Generally this is how we have to codegen matrix operations for a
30 * GPU, so this gives us the chance to constant fold operations on a
35 #include "ir_expression_flattening.h"
36 #include "glsl_types.h"
38 class ir_mat_op_to_vec_visitor
: public ir_hierarchical_visitor
{
40 ir_mat_op_to_vec_visitor()
42 this->made_progress
= false;
46 ir_visitor_status
visit_leave(ir_assignment
*);
48 ir_dereference
*get_column(ir_dereference
*val
, int col
);
49 ir_rvalue
*get_element(ir_dereference
*val
, int col
, int row
);
51 void do_mul_mat_mat(ir_dereference
*result
,
52 ir_dereference
*a
, ir_dereference
*b
);
53 void do_mul_mat_vec(ir_dereference
*result
,
54 ir_dereference
*a
, ir_dereference
*b
);
55 void do_mul_vec_mat(ir_dereference
*result
,
56 ir_dereference
*a
, ir_dereference
*b
);
57 void do_mul_mat_scalar(ir_dereference
*result
,
58 ir_dereference
*a
, ir_dereference
*b
);
59 void do_equal_mat_mat(ir_dereference
*result
, ir_dereference
*a
,
60 ir_dereference
*b
, bool test_equal
);
67 mat_op_to_vec_predicate(ir_instruction
*ir
)
69 ir_expression
*expr
= ir
->as_expression();
75 for (i
= 0; i
< expr
->get_num_operands(); i
++) {
76 if (expr
->operands
[i
]->type
->is_matrix())
84 do_mat_op_to_vec(exec_list
*instructions
)
86 ir_mat_op_to_vec_visitor v
;
88 /* Pull out any matrix expression to a separate assignment to a
89 * temp. This will make our handling of the breakdown to
90 * operations on the matrix's vector components much easier.
92 do_expression_flattening(instructions
, mat_op_to_vec_predicate
);
94 visit_list_elements(&v
, instructions
);
96 return v
.made_progress
;
100 ir_mat_op_to_vec_visitor::get_element(ir_dereference
*val
, int col
, int row
)
102 val
= get_column(val
, col
);
104 return new(mem_ctx
) ir_swizzle(val
, row
, 0, 0, 0, 1);
108 ir_mat_op_to_vec_visitor::get_column(ir_dereference
*val
, int row
)
110 val
= val
->clone(mem_ctx
, NULL
);
112 if (val
->type
->is_matrix()) {
113 val
= new(mem_ctx
) ir_dereference_array(val
,
114 new(mem_ctx
) ir_constant(row
));
121 ir_mat_op_to_vec_visitor::do_mul_mat_mat(ir_dereference
*result
,
126 ir_assignment
*assign
;
129 for (b_col
= 0; b_col
< b
->type
->matrix_columns
; b_col
++) {
131 expr
= new(mem_ctx
) ir_expression(ir_binop_mul
,
133 get_element(b
, b_col
, 0));
135 /* following columns */
136 for (i
= 1; i
< a
->type
->matrix_columns
; i
++) {
137 ir_expression
*mul_expr
;
139 mul_expr
= new(mem_ctx
) ir_expression(ir_binop_mul
,
141 get_element(b
, b_col
, i
));
142 expr
= new(mem_ctx
) ir_expression(ir_binop_add
,
147 assign
= new(mem_ctx
) ir_assignment(get_column(result
, b_col
), expr
);
148 base_ir
->insert_before(assign
);
153 ir_mat_op_to_vec_visitor::do_mul_mat_vec(ir_dereference
*result
,
158 ir_assignment
*assign
;
162 expr
= new(mem_ctx
) ir_expression(ir_binop_mul
,
164 get_element(b
, 0, 0));
166 /* following columns */
167 for (i
= 1; i
< a
->type
->matrix_columns
; i
++) {
168 ir_expression
*mul_expr
;
170 mul_expr
= new(mem_ctx
) ir_expression(ir_binop_mul
,
172 get_element(b
, 0, i
));
173 expr
= new(mem_ctx
) ir_expression(ir_binop_add
, expr
, mul_expr
);
176 result
= result
->clone(mem_ctx
, NULL
);
177 assign
= new(mem_ctx
) ir_assignment(result
, expr
);
178 base_ir
->insert_before(assign
);
182 ir_mat_op_to_vec_visitor::do_mul_vec_mat(ir_dereference
*result
,
188 for (i
= 0; i
< b
->type
->matrix_columns
; i
++) {
189 ir_rvalue
*column_result
;
190 ir_expression
*column_expr
;
191 ir_assignment
*column_assign
;
193 column_result
= result
->clone(mem_ctx
, NULL
);
194 column_result
= new(mem_ctx
) ir_swizzle(column_result
, i
, 0, 0, 0, 1);
196 column_expr
= new(mem_ctx
) ir_expression(ir_binop_dot
,
197 a
->clone(mem_ctx
, NULL
),
200 column_assign
= new(mem_ctx
) ir_assignment(column_result
,
202 base_ir
->insert_before(column_assign
);
207 ir_mat_op_to_vec_visitor::do_mul_mat_scalar(ir_dereference
*result
,
213 for (i
= 0; i
< a
->type
->matrix_columns
; i
++) {
214 ir_expression
*column_expr
;
215 ir_assignment
*column_assign
;
217 column_expr
= new(mem_ctx
) ir_expression(ir_binop_mul
,
219 b
->clone(mem_ctx
, NULL
));
221 column_assign
= new(mem_ctx
) ir_assignment(get_column(result
, i
),
223 base_ir
->insert_before(column_assign
);
228 ir_mat_op_to_vec_visitor::do_equal_mat_mat(ir_dereference
*result
,
233 /* This essentially implements the following GLSL:
235 * bool equal(mat4 a, mat4 b)
237 * return !any(bvec4(a[0] != b[0],
243 * bool nequal(mat4 a, mat4 b)
245 * return any(bvec4(a[0] != b[0],
251 const unsigned columns
= a
->type
->matrix_columns
;
252 const glsl_type
*const bvec_type
=
253 glsl_type::get_instance(GLSL_TYPE_BOOL
, columns
, 1);
255 ir_variable
*const tmp_bvec
=
256 new(this->mem_ctx
) ir_variable(bvec_type
, "mat_cmp_bvec",
258 this->base_ir
->insert_before(tmp_bvec
);
260 for (unsigned i
= 0; i
< columns
; i
++) {
261 ir_expression
*const cmp
=
262 new(this->mem_ctx
) ir_expression(ir_binop_any_nequal
,
266 ir_dereference
*const lhs
=
267 new(this->mem_ctx
) ir_dereference_variable(tmp_bvec
);
269 ir_assignment
*const assign
=
270 new(this->mem_ctx
) ir_assignment(lhs
, cmp
, NULL
, (1U << i
));
272 this->base_ir
->insert_before(assign
);
275 ir_rvalue
*const val
= new(this->mem_ctx
) ir_dereference_variable(tmp_bvec
);
276 ir_expression
*any
= new(this->mem_ctx
) ir_expression(ir_unop_any
, val
);
279 any
= new(this->mem_ctx
) ir_expression(ir_unop_logic_not
, any
);
281 ir_assignment
*const assign
=
282 new(mem_ctx
) ir_assignment(result
->clone(mem_ctx
, NULL
), any
);
283 base_ir
->insert_before(assign
);
287 has_matrix_operand(const ir_expression
*expr
, unsigned &columns
)
289 for (unsigned i
= 0; i
< expr
->get_num_operands(); i
++) {
290 if (expr
->operands
[i
]->type
->is_matrix()) {
291 columns
= expr
->operands
[i
]->type
->matrix_columns
;
301 ir_mat_op_to_vec_visitor::visit_leave(ir_assignment
*orig_assign
)
303 ir_expression
*orig_expr
= orig_assign
->rhs
->as_expression();
304 unsigned int i
, matrix_columns
= 1;
305 ir_dereference
*op
[2];
308 return visit_continue
;
310 if (!has_matrix_operand(orig_expr
, matrix_columns
))
311 return visit_continue
;
313 assert(orig_expr
->get_num_operands() <= 2);
315 mem_ctx
= ralloc_parent(orig_assign
);
317 ir_dereference_variable
*result
=
318 orig_assign
->lhs
->as_dereference_variable();
321 /* Store the expression operands in temps so we can use them
324 for (i
= 0; i
< orig_expr
->get_num_operands(); i
++) {
325 ir_assignment
*assign
;
326 ir_dereference
*deref
= orig_expr
->operands
[i
]->as_dereference();
328 /* Avoid making a temporary if we don't need to to avoid aliasing. */
330 deref
->variable_referenced() != result
->variable_referenced()) {
335 /* Otherwise, store the operand in a temporary generally if it's
338 ir_variable
*var
= new(mem_ctx
) ir_variable(orig_expr
->operands
[i
]->type
,
341 base_ir
->insert_before(var
);
343 /* Note that we use this dereference for the assignment. That means
344 * that others that want to use op[i] have to clone the deref.
346 op
[i
] = new(mem_ctx
) ir_dereference_variable(var
);
347 assign
= new(mem_ctx
) ir_assignment(op
[i
], orig_expr
->operands
[i
]);
348 base_ir
->insert_before(assign
);
351 /* OK, time to break down this matrix operation. */
352 switch (orig_expr
->operation
) {
354 /* Apply the operation to each column.*/
355 for (i
= 0; i
< matrix_columns
; i
++) {
356 ir_expression
*column_expr
;
357 ir_assignment
*column_assign
;
359 column_expr
= new(mem_ctx
) ir_expression(orig_expr
->operation
,
360 get_column(op
[0], i
));
362 column_assign
= new(mem_ctx
) ir_assignment(get_column(result
, i
),
364 assert(column_assign
->write_mask
!= 0);
365 base_ir
->insert_before(column_assign
);
373 /* For most operations, the matrix version is just going
374 * column-wise through and applying the operation to each column
377 for (i
= 0; i
< matrix_columns
; i
++) {
378 ir_expression
*column_expr
;
379 ir_assignment
*column_assign
;
381 column_expr
= new(mem_ctx
) ir_expression(orig_expr
->operation
,
382 get_column(op
[0], i
),
383 get_column(op
[1], i
));
385 column_assign
= new(mem_ctx
) ir_assignment(get_column(result
, i
),
387 assert(column_assign
->write_mask
!= 0);
388 base_ir
->insert_before(column_assign
);
393 if (op
[0]->type
->is_matrix()) {
394 if (op
[1]->type
->is_matrix()) {
395 do_mul_mat_mat(result
, op
[0], op
[1]);
396 } else if (op
[1]->type
->is_vector()) {
397 do_mul_mat_vec(result
, op
[0], op
[1]);
399 assert(op
[1]->type
->is_scalar());
400 do_mul_mat_scalar(result
, op
[0], op
[1]);
403 assert(op
[1]->type
->is_matrix());
404 if (op
[0]->type
->is_vector()) {
405 do_mul_vec_mat(result
, op
[0], op
[1]);
407 assert(op
[0]->type
->is_scalar());
408 do_mul_mat_scalar(result
, op
[1], op
[0]);
413 case ir_binop_all_equal
:
414 case ir_binop_any_nequal
:
415 do_equal_mat_mat(result
, op
[1], op
[0],
416 (orig_expr
->operation
== ir_binop_all_equal
));
420 printf("FINISHME: Handle matrix operation for %s\n",
421 orig_expr
->operator_string());
424 orig_assign
->remove();
425 this->made_progress
= true;
427 return visit_continue
;