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.
24 #include "ir_builder.h"
25 #include "ir_rvalue_visitor.h"
26 #include "ir_optimization.h"
27 #include "main/mtypes.h"
29 using namespace ir_builder
;
33 class vector_deref_visitor
: public ir_rvalue_enter_visitor
{
35 vector_deref_visitor(void *mem_ctx
, gl_shader_stage shader_stage
)
36 : progress(false), shader_stage(shader_stage
),
37 factory(&factory_instructions
, mem_ctx
)
41 virtual ~vector_deref_visitor()
45 virtual void handle_rvalue(ir_rvalue
**rv
);
46 virtual ir_visitor_status
visit_enter(ir_assignment
*ir
);
49 gl_shader_stage shader_stage
;
50 exec_list factory_instructions
;
54 } /* anonymous namespace */
57 vector_deref_visitor::visit_enter(ir_assignment
*ir
)
59 if (!ir
->lhs
|| ir
->lhs
->ir_type
!= ir_type_dereference_array
)
60 return ir_rvalue_enter_visitor::visit_enter(ir
);
62 ir_dereference_array
*const deref
= (ir_dereference_array
*) ir
->lhs
;
63 if (!deref
->array
->type
->is_vector())
64 return ir_rvalue_enter_visitor::visit_enter(ir
);
66 ir_rvalue
*const new_lhs
= deref
->array
;
68 void *mem_ctx
= ralloc_parent(ir
);
69 ir_constant
*old_index_constant
=
70 deref
->array_index
->constant_expression_value(mem_ctx
);
71 if (!old_index_constant
) {
72 if (shader_stage
== MESA_SHADER_TESS_CTRL
&&
73 deref
->variable_referenced()->data
.mode
== ir_var_shader_out
) {
74 /* Tessellation control shader outputs act as if they have memory
75 * backing them and if we have writes from multiple threads
76 * targeting the same vec4 (this can happen for patch outputs), the
77 * load-vec-store pattern of ir_triop_vector_insert doesn't work.
78 * Instead, we have to lower to a series of conditional write-masked
81 ir_variable
*const src_temp
=
82 factory
.make_temp(ir
->rhs
->type
, "scalar_tmp");
84 /* The newly created variable declaration goes before the assignment
85 * because we're going to set it as the new LHS.
87 ir
->insert_before(factory
.instructions
);
88 ir
->set_lhs(new(mem_ctx
) ir_dereference_variable(src_temp
));
90 ir_variable
*const arr_index
=
91 factory
.make_temp(deref
->array_index
->type
, "index_tmp");
92 factory
.emit(assign(arr_index
, deref
->array_index
));
94 for (unsigned i
= 0; i
< new_lhs
->type
->vector_elements
; i
++) {
95 ir_constant
*const cmp_index
=
96 ir_constant::zero(factory
.mem_ctx
, deref
->array_index
->type
);
97 cmp_index
->value
.u
[0] = i
;
99 ir_rvalue
*const lhs_clone
= new_lhs
->clone(factory
.mem_ctx
, NULL
);
100 ir_dereference_variable
*const src_temp_deref
=
101 new(mem_ctx
) ir_dereference_variable(src_temp
);
103 if (new_lhs
->ir_type
!= ir_type_swizzle
) {
104 assert(lhs_clone
->as_dereference());
105 ir_assignment
*cond_assign
=
106 new(mem_ctx
) ir_assignment(lhs_clone
->as_dereference(),
108 equal(arr_index
, cmp_index
),
110 factory
.emit(cond_assign
);
112 ir_assignment
*cond_assign
=
113 new(mem_ctx
) ir_assignment(swizzle(lhs_clone
, i
, 1),
115 equal(arr_index
, cmp_index
));
116 factory
.emit(cond_assign
);
119 ir
->insert_after(factory
.instructions
);
121 ir
->rhs
= new(mem_ctx
) ir_expression(ir_triop_vector_insert
,
123 new_lhs
->clone(mem_ctx
, NULL
),
126 ir
->write_mask
= (1 << new_lhs
->type
->vector_elements
) - 1;
127 ir
->set_lhs(new_lhs
);
129 } else if (new_lhs
->ir_type
!= ir_type_swizzle
) {
130 ir
->set_lhs(new_lhs
);
131 ir
->write_mask
= 1 << old_index_constant
->get_uint_component(0);
133 /* If the "new" LHS is a swizzle, use the set_lhs helper to instead
136 unsigned component
[1] = { old_index_constant
->get_uint_component(0) };
137 ir
->set_lhs(new(mem_ctx
) ir_swizzle(new_lhs
, component
, 1));
140 return ir_rvalue_enter_visitor::visit_enter(ir
);
144 vector_deref_visitor::handle_rvalue(ir_rvalue
**rv
)
146 if (*rv
== NULL
|| (*rv
)->ir_type
!= ir_type_dereference_array
)
149 ir_dereference_array
*const deref
= (ir_dereference_array
*) *rv
;
150 if (!deref
->array
->type
->is_vector())
153 void *mem_ctx
= ralloc_parent(deref
);
154 *rv
= new(mem_ctx
) ir_expression(ir_binop_vector_extract
,
160 lower_vector_derefs(gl_linked_shader
*shader
)
162 vector_deref_visitor
v(shader
->ir
, shader
->Stage
);
164 visit_list_elements(&v
, shader
->ir
);