2 * Copyright © 2013 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 opt_flip_matrices.cpp
27 * Convert (matrix * vector) operations to (vector * matrixTranspose),
28 * which can be done using dot products rather than multiplies and adds.
29 * On some hardware, this is more efficient.
31 * This currently only does the conversion for built-in matrices which
32 * already have transposed equivalents. Namely, gl_ModelViewProjectionMatrix
33 * and gl_TextureMatrix.
36 #include "ir_optimization.h"
37 #include "main/macros.h"
40 class matrix_flipper
: public ir_hierarchical_visitor
{
42 matrix_flipper(exec_list
*instructions
)
46 texmat_transpose
= NULL
;
48 foreach_in_list(ir_instruction
, ir
, instructions
) {
49 ir_variable
*var
= ir
->as_variable();
52 if (strcmp(var
->name
, "gl_ModelViewProjectionMatrixTranspose") == 0)
54 if (strcmp(var
->name
, "gl_TextureMatrixTranspose") == 0)
55 texmat_transpose
= var
;
59 ir_visitor_status
visit_enter(ir_expression
*ir
);
64 ir_variable
*mvp_transpose
;
65 ir_variable
*texmat_transpose
;
70 matrix_flipper::visit_enter(ir_expression
*ir
)
72 if (ir
->operation
!= ir_binop_mul
||
73 !ir
->operands
[0]->type
->is_matrix() ||
74 !ir
->operands
[1]->type
->is_vector())
75 return visit_continue
;
77 ir_variable
*mat_var
= ir
->operands
[0]->variable_referenced();
79 return visit_continue
;
82 strcmp(mat_var
->name
, "gl_ModelViewProjectionMatrix") == 0) {
84 ir_dereference_variable
*deref
= ir
->operands
[0]->as_dereference_variable();
85 assert(deref
&& deref
->var
== mat_var
);
88 void *mem_ctx
= ralloc_parent(ir
);
90 ir
->operands
[0] = ir
->operands
[1];
91 ir
->operands
[1] = new(mem_ctx
) ir_dereference_variable(mvp_transpose
);
94 } else if (texmat_transpose
&&
95 strcmp(mat_var
->name
, "gl_TextureMatrix") == 0) {
96 ir_dereference_array
*array_ref
= ir
->operands
[0]->as_dereference_array();
97 assert(array_ref
!= NULL
);
98 ir_dereference_variable
*var_ref
= array_ref
->array
->as_dereference_variable();
99 assert(var_ref
&& var_ref
->var
== mat_var
);
101 ir
->operands
[0] = ir
->operands
[1];
102 ir
->operands
[1] = array_ref
;
104 var_ref
->var
= texmat_transpose
;
106 texmat_transpose
->data
.max_array_access
=
107 MAX2(texmat_transpose
->data
.max_array_access
, mat_var
->data
.max_array_access
);
112 return visit_continue
;
116 opt_flip_matrices(struct exec_list
*instructions
)
118 matrix_flipper
v(instructions
);
120 visit_list_elements(&v
, instructions
);