2 * Copyright © 2014 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
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_tess_level.cpp
27 * This pass accounts for the difference between the way gl_TessLevelOuter
28 * and gl_TessLevelInner is declared in standard GLSL (as an array of
29 * floats), and the way it is frequently implemented in hardware (as a vec4
32 * The declaration of gl_TessLevel* is replaced with a declaration
33 * of gl_TessLevel*MESA, and any references to gl_TessLevel* are
34 * translated to refer to gl_TessLevel*MESA with the appropriate
35 * swizzling of array indices. For instance:
37 * gl_TessLevelOuter[i]
41 * gl_TessLevelOuterMESA[i]
43 * Since some hardware may not internally represent gl_TessLevel* as a pair
44 * of vec4's, this lowering pass is optional. To enable it, set the
45 * LowerTessLevel flag in gl_shader_compiler_options to true.
48 #include "glsl_symbol_table.h"
49 #include "ir_rvalue_visitor.h"
51 #include "program/prog_instruction.h" /* For WRITEMASK_* */
52 #include "main/mtypes.h"
56 class lower_tess_level_visitor
: public ir_rvalue_visitor
{
58 explicit lower_tess_level_visitor(gl_shader_stage shader_stage
)
59 : progress(false), old_tess_level_outer_var(NULL
),
60 old_tess_level_inner_var(NULL
), new_tess_level_outer_var(NULL
),
61 new_tess_level_inner_var(NULL
), shader_stage(shader_stage
)
65 virtual ir_visitor_status
visit(ir_variable
*);
66 bool is_tess_level_array(ir_rvalue
*ir
);
67 ir_rvalue
*lower_tess_level_array(ir_rvalue
*ir
);
68 virtual ir_visitor_status
visit_leave(ir_assignment
*);
69 void visit_new_assignment(ir_assignment
*ir
);
70 virtual ir_visitor_status
visit_leave(ir_call
*);
72 virtual void handle_rvalue(ir_rvalue
**rvalue
);
74 void fix_lhs(ir_assignment
*);
79 * Pointer to the declaration of gl_TessLevel*, if found.
81 ir_variable
*old_tess_level_outer_var
;
82 ir_variable
*old_tess_level_inner_var
;
85 * Pointer to the newly-created gl_TessLevel*MESA variables.
87 ir_variable
*new_tess_level_outer_var
;
88 ir_variable
*new_tess_level_inner_var
;
91 * Type of shader we are compiling (e.g. MESA_SHADER_TESS_CTRL)
93 const gl_shader_stage shader_stage
;
96 } /* anonymous namespace */
99 * Replace any declaration of gl_TessLevel* as an array of floats with a
100 * declaration of gl_TessLevel*MESA as a vec4.
103 lower_tess_level_visitor::visit(ir_variable
*ir
)
106 ((strcmp(ir
->name
, "gl_TessLevelInner") != 0) &&
107 (strcmp(ir
->name
, "gl_TessLevelOuter") != 0)))
108 return visit_continue
;
110 assert (ir
->type
->is_array());
112 if (strcmp(ir
->name
, "gl_TessLevelOuter") == 0) {
113 if (this->old_tess_level_outer_var
)
114 return visit_continue
;
116 old_tess_level_outer_var
= ir
;
117 assert(ir
->type
->fields
.array
== glsl_type::float_type
);
119 /* Clone the old var so that we inherit all of its properties */
120 new_tess_level_outer_var
= ir
->clone(ralloc_parent(ir
), NULL
);
122 /* And change the properties that we need to change */
123 new_tess_level_outer_var
->name
= ralloc_strdup(new_tess_level_outer_var
,
124 "gl_TessLevelOuterMESA");
125 new_tess_level_outer_var
->type
= glsl_type::vec4_type
;
126 new_tess_level_outer_var
->data
.max_array_access
= 0;
128 ir
->replace_with(new_tess_level_outer_var
);
129 } else if (strcmp(ir
->name
, "gl_TessLevelInner") == 0) {
130 if (this->old_tess_level_inner_var
)
131 return visit_continue
;
133 old_tess_level_inner_var
= ir
;
134 assert(ir
->type
->fields
.array
== glsl_type::float_type
);
136 /* Clone the old var so that we inherit all of its properties */
137 new_tess_level_inner_var
= ir
->clone(ralloc_parent(ir
), NULL
);
139 /* And change the properties that we need to change */
140 new_tess_level_inner_var
->name
= ralloc_strdup(new_tess_level_inner_var
,
141 "gl_TessLevelInnerMESA");
142 new_tess_level_inner_var
->type
= glsl_type::vec2_type
;
143 new_tess_level_inner_var
->data
.max_array_access
= 0;
145 ir
->replace_with(new_tess_level_inner_var
);
150 this->progress
= true;
152 return visit_continue
;
157 * Determine whether the given rvalue describes an array of floats that
158 * needs to be lowered to a vec4; that is, determine whether it
159 * matches one of the following patterns:
161 * - gl_TessLevelOuter
162 * - gl_TessLevelInner
165 lower_tess_level_visitor::is_tess_level_array(ir_rvalue
*ir
)
167 if (!ir
->type
->is_array())
169 if (ir
->type
->fields
.array
!= glsl_type::float_type
)
172 if (this->old_tess_level_outer_var
) {
173 if (ir
->variable_referenced() == this->old_tess_level_outer_var
)
176 if (this->old_tess_level_inner_var
) {
177 if (ir
->variable_referenced() == this->old_tess_level_inner_var
)
185 * If the given ir satisfies is_tess_level_array(), return new ir
186 * representing its lowered equivalent. That is, map:
188 * - gl_TessLevelOuter => gl_TessLevelOuterMESA
189 * - gl_TessLevelInner => gl_TessLevelInnerMESA
191 * Otherwise return NULL.
194 lower_tess_level_visitor::lower_tess_level_array(ir_rvalue
*ir
)
196 if (!ir
->type
->is_array())
198 if (ir
->type
->fields
.array
!= glsl_type::float_type
)
201 ir_variable
**new_var
= NULL
;
203 if (this->old_tess_level_outer_var
) {
204 if (ir
->variable_referenced() == this->old_tess_level_outer_var
)
205 new_var
= &this->new_tess_level_outer_var
;
207 if (this->old_tess_level_inner_var
) {
208 if (ir
->variable_referenced() == this->old_tess_level_inner_var
)
209 new_var
= &this->new_tess_level_inner_var
;
215 assert(ir
->as_dereference_variable());
216 return new(ralloc_parent(ir
)) ir_dereference_variable(*new_var
);
221 lower_tess_level_visitor::handle_rvalue(ir_rvalue
**rv
)
226 ir_dereference_array
*const array_deref
= (*rv
)->as_dereference_array();
227 if (array_deref
== NULL
)
230 /* Replace any expression that indexes one of the floats in gl_TessLevel*
231 * with an expression that indexes into one of the vec4's
232 * gl_TessLevel*MESA and accesses the appropriate component.
234 ir_rvalue
*lowered_vec4
=
235 this->lower_tess_level_array(array_deref
->array
);
236 if (lowered_vec4
!= NULL
) {
237 this->progress
= true;
238 void *mem_ctx
= ralloc_parent(array_deref
);
240 ir_expression
*const expr
=
241 new(mem_ctx
) ir_expression(ir_binop_vector_extract
,
243 array_deref
->array_index
);
250 lower_tess_level_visitor::fix_lhs(ir_assignment
*ir
)
252 if (ir
->lhs
->ir_type
!= ir_type_expression
)
254 void *mem_ctx
= ralloc_parent(ir
);
255 ir_expression
*const expr
= (ir_expression
*) ir
->lhs
;
257 /* The expression must be of the form:
259 * (vector_extract gl_TessLevel*MESA, j).
261 assert(expr
->operation
== ir_binop_vector_extract
);
262 assert(expr
->operands
[0]->ir_type
== ir_type_dereference_variable
);
263 assert((expr
->operands
[0]->type
== glsl_type::vec4_type
) ||
264 (expr
->operands
[0]->type
== glsl_type::vec2_type
));
266 ir_dereference
*const new_lhs
= (ir_dereference
*) expr
->operands
[0];
268 ir_constant
*old_index_constant
=
269 expr
->operands
[1]->constant_expression_value(mem_ctx
);
270 if (!old_index_constant
) {
271 ir
->rhs
= new(mem_ctx
) ir_expression(ir_triop_vector_insert
,
272 expr
->operands
[0]->type
,
273 new_lhs
->clone(mem_ctx
, NULL
),
277 ir
->set_lhs(new_lhs
);
279 if (old_index_constant
) {
280 /* gl_TessLevel* is being accessed via a constant index. Don't bother
281 * creating a vector insert op. Just use a write mask.
283 ir
->write_mask
= 1 << old_index_constant
->get_int_component(0);
285 ir
->write_mask
= (1 << expr
->operands
[0]->type
->vector_elements
) - 1;
290 * Replace any assignment having a gl_TessLevel* (undereferenced) as
291 * its LHS or RHS with a sequence of assignments, one for each component of
292 * the array. Each of these assignments is lowered to refer to
293 * gl_TessLevel*MESA as appropriate.
296 lower_tess_level_visitor::visit_leave(ir_assignment
*ir
)
298 /* First invoke the base class visitor. This causes handle_rvalue() to be
299 * called on ir->rhs and ir->condition.
301 ir_rvalue_visitor::visit_leave(ir
);
303 if (this->is_tess_level_array(ir
->lhs
) ||
304 this->is_tess_level_array(ir
->rhs
)) {
305 /* LHS or RHS of the assignment is the entire gl_TessLevel* array.
307 * reshaping gl_TessLevel* from an array of floats to a
308 * vec4, this isn't going to work as a bulk assignment anymore, so
309 * unroll it to element-by-element assignments and lower each of them.
311 * Note: to unroll into element-by-element assignments, we need to make
312 * clones of the LHS and RHS. This is safe because expressions and
313 * l-values are side-effect free.
315 void *ctx
= ralloc_parent(ir
);
316 int array_size
= ir
->lhs
->type
->array_size();
317 for (int i
= 0; i
< array_size
; ++i
) {
318 ir_dereference_array
*new_lhs
= new(ctx
) ir_dereference_array(
319 ir
->lhs
->clone(ctx
, NULL
), new(ctx
) ir_constant(i
));
320 ir_dereference_array
*new_rhs
= new(ctx
) ir_dereference_array(
321 ir
->rhs
->clone(ctx
, NULL
), new(ctx
) ir_constant(i
));
322 this->handle_rvalue((ir_rvalue
**) &new_rhs
);
324 /* Handle the LHS after creating the new assignment. This must
325 * happen in this order because handle_rvalue may replace the old LHS
326 * with an ir_expression of ir_binop_vector_extract. Since this is
327 * not a valide l-value, this will cause an assertion in the
328 * ir_assignment constructor to fail.
330 * If this occurs, replace the mangled LHS with a dereference of the
331 * vector, and replace the RHS with an ir_triop_vector_insert.
333 ir_assignment
*const assign
= new(ctx
) ir_assignment(new_lhs
, new_rhs
);
334 this->handle_rvalue((ir_rvalue
**) &assign
->lhs
);
335 this->fix_lhs(assign
);
337 this->base_ir
->insert_before(assign
);
341 return visit_continue
;
344 /* Handle the LHS as if it were an r-value. Normally
345 * rvalue_visit(ir_assignment *) only visits the RHS, but we need to lower
346 * expressions in the LHS as well.
348 * This may cause the LHS to get replaced with an ir_expression of
349 * ir_binop_vector_extract. If this occurs, replace it with a dereference
350 * of the vector, and replace the RHS with an ir_triop_vector_insert.
352 handle_rvalue((ir_rvalue
**)&ir
->lhs
);
355 return rvalue_visit(ir
);
360 * Set up base_ir properly and call visit_leave() on a newly created
361 * ir_assignment node. This is used in cases where we have to insert an
362 * ir_assignment in a place where we know the hierarchical visitor won't see
366 lower_tess_level_visitor::visit_new_assignment(ir_assignment
*ir
)
368 ir_instruction
*old_base_ir
= this->base_ir
;
371 this->base_ir
= old_base_ir
;
376 * If a gl_TessLevel* variable appears as an argument in an ir_call
377 * expression, replace it with a temporary variable, and make sure the ir_call
378 * is preceded and/or followed by assignments that copy the contents of the
379 * temporary variable to and/or from gl_TessLevel*. Each of these
380 * assignments is then lowered to refer to gl_TessLevel*MESA.
383 lower_tess_level_visitor::visit_leave(ir_call
*ir
)
385 void *ctx
= ralloc_parent(ir
);
387 const exec_node
*formal_param_node
= ir
->callee
->parameters
.get_head_raw();
388 const exec_node
*actual_param_node
= ir
->actual_parameters
.get_head_raw();
389 while (!actual_param_node
->is_tail_sentinel()) {
390 ir_variable
*formal_param
= (ir_variable
*) formal_param_node
;
391 ir_rvalue
*actual_param
= (ir_rvalue
*) actual_param_node
;
393 /* Advance formal_param_node and actual_param_node now so that we can
394 * safely replace actual_param with another node, if necessary, below.
396 formal_param_node
= formal_param_node
->next
;
397 actual_param_node
= actual_param_node
->next
;
399 if (!this->is_tess_level_array(actual_param
))
402 /* User is trying to pass a whole gl_TessLevel* array to a function
403 * call. Since we are reshaping gl_TessLevel* from an array of floats
404 * to a vec4, this isn't going to work anymore, so use a temporary
407 ir_variable
*temp
= new(ctx
) ir_variable(
408 actual_param
->type
, "temp_tess_level", ir_var_temporary
);
409 this->base_ir
->insert_before(temp
);
410 actual_param
->replace_with(
411 new(ctx
) ir_dereference_variable(temp
));
412 if (formal_param
->data
.mode
== ir_var_function_in
413 || formal_param
->data
.mode
== ir_var_function_inout
) {
414 /* Copy from gl_TessLevel* to the temporary before the call.
415 * Since we are going to insert this copy before the current
416 * instruction, we need to visit it afterwards to make sure it
419 ir_assignment
*new_assignment
= new(ctx
) ir_assignment(
420 new(ctx
) ir_dereference_variable(temp
),
421 actual_param
->clone(ctx
, NULL
));
422 this->base_ir
->insert_before(new_assignment
);
423 this->visit_new_assignment(new_assignment
);
425 if (formal_param
->data
.mode
== ir_var_function_out
426 || formal_param
->data
.mode
== ir_var_function_inout
) {
427 /* Copy from the temporary to gl_TessLevel* after the call.
428 * Since visit_list_elements() has already decided which
429 * instruction it's going to visit next, we need to visit
430 * afterwards to make sure it gets lowered.
432 ir_assignment
*new_assignment
= new(ctx
) ir_assignment(
433 actual_param
->clone(ctx
, NULL
),
434 new(ctx
) ir_dereference_variable(temp
));
435 this->base_ir
->insert_after(new_assignment
);
436 this->visit_new_assignment(new_assignment
);
440 return rvalue_visit(ir
);
445 lower_tess_level(gl_linked_shader
*shader
)
447 if ((shader
->Stage
!= MESA_SHADER_TESS_CTRL
) &&
448 (shader
->Stage
!= MESA_SHADER_TESS_EVAL
))
451 lower_tess_level_visitor
v(shader
->Stage
);
453 visit_list_elements(&v
, shader
->ir
);
455 if (v
.new_tess_level_outer_var
)
456 shader
->symbols
->add_variable(v
.new_tess_level_outer_var
);
457 if (v
.new_tess_level_inner_var
)
458 shader
->symbols
->add_variable(v
.new_tess_level_inner_var
);