b0965eb562dd8e6e176dae3a48f58623c20b3020
[mesa.git] / src / compiler / glsl / lower_tess_level.cpp
1 /*
2 * Copyright © 2014 Intel Corporation
3 *
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
10 *
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
13 * Software.
14 *
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.
22 */
23
24 /**
25 * \file lower_tess_level.cpp
26 *
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
30 * and vec2).
31 *
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:
36 *
37 * gl_TessLevelOuter[i]
38 *
39 * is translated into:
40 *
41 * gl_TessLevelOuterMESA[i]
42 *
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.
46 */
47
48 #include "glsl_symbol_table.h"
49 #include "ir_rvalue_visitor.h"
50 #include "ir.h"
51 #include "program/prog_instruction.h" /* For WRITEMASK_* */
52
53 namespace {
54
55 class lower_tess_level_visitor : public ir_rvalue_visitor {
56 public:
57 explicit lower_tess_level_visitor(gl_shader_stage shader_stage)
58 : progress(false), old_tess_level_outer_var(NULL),
59 old_tess_level_inner_var(NULL), new_tess_level_outer_var(NULL),
60 new_tess_level_inner_var(NULL), shader_stage(shader_stage)
61 {
62 }
63
64 virtual ir_visitor_status visit(ir_variable *);
65 bool is_tess_level_array(ir_rvalue *ir);
66 ir_rvalue *lower_tess_level_array(ir_rvalue *ir);
67 virtual ir_visitor_status visit_leave(ir_assignment *);
68 void visit_new_assignment(ir_assignment *ir);
69 virtual ir_visitor_status visit_leave(ir_call *);
70
71 virtual void handle_rvalue(ir_rvalue **rvalue);
72
73 void fix_lhs(ir_assignment *);
74
75 bool progress;
76
77 /**
78 * Pointer to the declaration of gl_TessLevel*, if found.
79 */
80 ir_variable *old_tess_level_outer_var;
81 ir_variable *old_tess_level_inner_var;
82
83 /**
84 * Pointer to the newly-created gl_TessLevel*MESA variables.
85 */
86 ir_variable *new_tess_level_outer_var;
87 ir_variable *new_tess_level_inner_var;
88
89 /**
90 * Type of shader we are compiling (e.g. MESA_SHADER_TESS_CTRL)
91 */
92 const gl_shader_stage shader_stage;
93 };
94
95 } /* anonymous namespace */
96
97 /**
98 * Replace any declaration of gl_TessLevel* as an array of floats with a
99 * declaration of gl_TessLevel*MESA as a vec4.
100 */
101 ir_visitor_status
102 lower_tess_level_visitor::visit(ir_variable *ir)
103 {
104 if ((!ir->name) ||
105 ((strcmp(ir->name, "gl_TessLevelInner") != 0) &&
106 (strcmp(ir->name, "gl_TessLevelOuter") != 0)))
107 return visit_continue;
108
109 assert (ir->type->is_array());
110
111 if (strcmp(ir->name, "gl_TessLevelOuter") == 0) {
112 if (this->old_tess_level_outer_var)
113 return visit_continue;
114
115 old_tess_level_outer_var = ir;
116 assert(ir->type->fields.array == glsl_type::float_type);
117
118 /* Clone the old var so that we inherit all of its properties */
119 new_tess_level_outer_var = ir->clone(ralloc_parent(ir), NULL);
120
121 /* And change the properties that we need to change */
122 new_tess_level_outer_var->name = ralloc_strdup(new_tess_level_outer_var,
123 "gl_TessLevelOuterMESA");
124 new_tess_level_outer_var->type = glsl_type::vec4_type;
125 new_tess_level_outer_var->data.max_array_access = 0;
126
127 ir->replace_with(new_tess_level_outer_var);
128 } else if (strcmp(ir->name, "gl_TessLevelInner") == 0) {
129 if (this->old_tess_level_inner_var)
130 return visit_continue;
131
132 old_tess_level_inner_var = ir;
133 assert(ir->type->fields.array == glsl_type::float_type);
134
135 /* Clone the old var so that we inherit all of its properties */
136 new_tess_level_inner_var = ir->clone(ralloc_parent(ir), NULL);
137
138 /* And change the properties that we need to change */
139 new_tess_level_inner_var->name = ralloc_strdup(new_tess_level_inner_var,
140 "gl_TessLevelInnerMESA");
141 new_tess_level_inner_var->type = glsl_type::vec2_type;
142 new_tess_level_inner_var->data.max_array_access = 0;
143
144 ir->replace_with(new_tess_level_inner_var);
145 } else {
146 assert(0);
147 }
148
149 this->progress = true;
150
151 return visit_continue;
152 }
153
154
155 /**
156 * Determine whether the given rvalue describes an array of floats that
157 * needs to be lowered to a vec4; that is, determine whether it
158 * matches one of the following patterns:
159 *
160 * - gl_TessLevelOuter
161 * - gl_TessLevelInner
162 */
163 bool
164 lower_tess_level_visitor::is_tess_level_array(ir_rvalue *ir)
165 {
166 if (!ir->type->is_array())
167 return false;
168 if (ir->type->fields.array != glsl_type::float_type)
169 return false;
170
171 if (this->old_tess_level_outer_var) {
172 if (ir->variable_referenced() == this->old_tess_level_outer_var)
173 return true;
174 }
175 if (this->old_tess_level_inner_var) {
176 if (ir->variable_referenced() == this->old_tess_level_inner_var)
177 return true;
178 }
179 return false;
180 }
181
182
183 /**
184 * If the given ir satisfies is_tess_level_array(), return new ir
185 * representing its lowered equivalent. That is, map:
186 *
187 * - gl_TessLevelOuter => gl_TessLevelOuterMESA
188 * - gl_TessLevelInner => gl_TessLevelInnerMESA
189 *
190 * Otherwise return NULL.
191 */
192 ir_rvalue *
193 lower_tess_level_visitor::lower_tess_level_array(ir_rvalue *ir)
194 {
195 if (!ir->type->is_array())
196 return NULL;
197 if (ir->type->fields.array != glsl_type::float_type)
198 return NULL;
199
200 ir_variable **new_var = NULL;
201
202 if (this->old_tess_level_outer_var) {
203 if (ir->variable_referenced() == this->old_tess_level_outer_var)
204 new_var = &this->new_tess_level_outer_var;
205 }
206 if (this->old_tess_level_inner_var) {
207 if (ir->variable_referenced() == this->old_tess_level_inner_var)
208 new_var = &this->new_tess_level_inner_var;
209 }
210
211 if (new_var == NULL)
212 return NULL;
213
214 assert(ir->as_dereference_variable());
215 return new(ralloc_parent(ir)) ir_dereference_variable(*new_var);
216 }
217
218
219 void
220 lower_tess_level_visitor::handle_rvalue(ir_rvalue **rv)
221 {
222 if (*rv == NULL)
223 return;
224
225 ir_dereference_array *const array_deref = (*rv)->as_dereference_array();
226 if (array_deref == NULL)
227 return;
228
229 /* Replace any expression that indexes one of the floats in gl_TessLevel*
230 * with an expression that indexes into one of the vec4's
231 * gl_TessLevel*MESA and accesses the appropriate component.
232 */
233 ir_rvalue *lowered_vec4 =
234 this->lower_tess_level_array(array_deref->array);
235 if (lowered_vec4 != NULL) {
236 this->progress = true;
237 void *mem_ctx = ralloc_parent(array_deref);
238
239 ir_expression *const expr =
240 new(mem_ctx) ir_expression(ir_binop_vector_extract,
241 lowered_vec4,
242 array_deref->array_index);
243
244 *rv = expr;
245 }
246 }
247
248 void
249 lower_tess_level_visitor::fix_lhs(ir_assignment *ir)
250 {
251 if (ir->lhs->ir_type != ir_type_expression)
252 return;
253 void *mem_ctx = ralloc_parent(ir);
254 ir_expression *const expr = (ir_expression *) ir->lhs;
255
256 /* The expression must be of the form:
257 *
258 * (vector_extract gl_TessLevel*MESA, j).
259 */
260 assert(expr->operation == ir_binop_vector_extract);
261 assert(expr->operands[0]->ir_type == ir_type_dereference_variable);
262 assert((expr->operands[0]->type == glsl_type::vec4_type) ||
263 (expr->operands[0]->type == glsl_type::vec2_type));
264
265 ir_dereference *const new_lhs = (ir_dereference *) expr->operands[0];
266
267 ir_constant *old_index_constant =
268 expr->operands[1]->constant_expression_value(mem_ctx);
269 if (!old_index_constant) {
270 ir->rhs = new(mem_ctx) ir_expression(ir_triop_vector_insert,
271 expr->operands[0]->type,
272 new_lhs->clone(mem_ctx, NULL),
273 ir->rhs,
274 expr->operands[1]);
275 }
276 ir->set_lhs(new_lhs);
277
278 if (old_index_constant) {
279 /* gl_TessLevel* is being accessed via a constant index. Don't bother
280 * creating a vector insert op. Just use a write mask.
281 */
282 ir->write_mask = 1 << old_index_constant->get_int_component(0);
283 } else {
284 ir->write_mask = (1 << expr->operands[0]->type->vector_elements) - 1;
285 }
286 }
287
288 /**
289 * Replace any assignment having a gl_TessLevel* (undereferenced) as
290 * its LHS or RHS with a sequence of assignments, one for each component of
291 * the array. Each of these assignments is lowered to refer to
292 * gl_TessLevel*MESA as appropriate.
293 */
294 ir_visitor_status
295 lower_tess_level_visitor::visit_leave(ir_assignment *ir)
296 {
297 /* First invoke the base class visitor. This causes handle_rvalue() to be
298 * called on ir->rhs and ir->condition.
299 */
300 ir_rvalue_visitor::visit_leave(ir);
301
302 if (this->is_tess_level_array(ir->lhs) ||
303 this->is_tess_level_array(ir->rhs)) {
304 /* LHS or RHS of the assignment is the entire gl_TessLevel* array.
305 * Since we are
306 * reshaping gl_TessLevel* from an array of floats to a
307 * vec4, this isn't going to work as a bulk assignment anymore, so
308 * unroll it to element-by-element assignments and lower each of them.
309 *
310 * Note: to unroll into element-by-element assignments, we need to make
311 * clones of the LHS and RHS. This is safe because expressions and
312 * l-values are side-effect free.
313 */
314 void *ctx = ralloc_parent(ir);
315 int array_size = ir->lhs->type->array_size();
316 for (int i = 0; i < array_size; ++i) {
317 ir_dereference_array *new_lhs = new(ctx) ir_dereference_array(
318 ir->lhs->clone(ctx, NULL), new(ctx) ir_constant(i));
319 ir_dereference_array *new_rhs = new(ctx) ir_dereference_array(
320 ir->rhs->clone(ctx, NULL), new(ctx) ir_constant(i));
321 this->handle_rvalue((ir_rvalue **) &new_rhs);
322
323 /* Handle the LHS after creating the new assignment. This must
324 * happen in this order because handle_rvalue may replace the old LHS
325 * with an ir_expression of ir_binop_vector_extract. Since this is
326 * not a valide l-value, this will cause an assertion in the
327 * ir_assignment constructor to fail.
328 *
329 * If this occurs, replace the mangled LHS with a dereference of the
330 * vector, and replace the RHS with an ir_triop_vector_insert.
331 */
332 ir_assignment *const assign = new(ctx) ir_assignment(new_lhs, new_rhs);
333 this->handle_rvalue((ir_rvalue **) &assign->lhs);
334 this->fix_lhs(assign);
335
336 this->base_ir->insert_before(assign);
337 }
338 ir->remove();
339
340 return visit_continue;
341 }
342
343 /* Handle the LHS as if it were an r-value. Normally
344 * rvalue_visit(ir_assignment *) only visits the RHS, but we need to lower
345 * expressions in the LHS as well.
346 *
347 * This may cause the LHS to get replaced with an ir_expression of
348 * ir_binop_vector_extract. If this occurs, replace it with a dereference
349 * of the vector, and replace the RHS with an ir_triop_vector_insert.
350 */
351 handle_rvalue((ir_rvalue **)&ir->lhs);
352 this->fix_lhs(ir);
353
354 return rvalue_visit(ir);
355 }
356
357
358 /**
359 * Set up base_ir properly and call visit_leave() on a newly created
360 * ir_assignment node. This is used in cases where we have to insert an
361 * ir_assignment in a place where we know the hierarchical visitor won't see
362 * it.
363 */
364 void
365 lower_tess_level_visitor::visit_new_assignment(ir_assignment *ir)
366 {
367 ir_instruction *old_base_ir = this->base_ir;
368 this->base_ir = ir;
369 ir->accept(this);
370 this->base_ir = old_base_ir;
371 }
372
373
374 /**
375 * If a gl_TessLevel* variable appears as an argument in an ir_call
376 * expression, replace it with a temporary variable, and make sure the ir_call
377 * is preceded and/or followed by assignments that copy the contents of the
378 * temporary variable to and/or from gl_TessLevel*. Each of these
379 * assignments is then lowered to refer to gl_TessLevel*MESA.
380 */
381 ir_visitor_status
382 lower_tess_level_visitor::visit_leave(ir_call *ir)
383 {
384 void *ctx = ralloc_parent(ir);
385
386 const exec_node *formal_param_node = ir->callee->parameters.get_head_raw();
387 const exec_node *actual_param_node = ir->actual_parameters.get_head_raw();
388 while (!actual_param_node->is_tail_sentinel()) {
389 ir_variable *formal_param = (ir_variable *) formal_param_node;
390 ir_rvalue *actual_param = (ir_rvalue *) actual_param_node;
391
392 /* Advance formal_param_node and actual_param_node now so that we can
393 * safely replace actual_param with another node, if necessary, below.
394 */
395 formal_param_node = formal_param_node->next;
396 actual_param_node = actual_param_node->next;
397
398 if (!this->is_tess_level_array(actual_param))
399 continue;
400
401 /* User is trying to pass a whole gl_TessLevel* array to a function
402 * call. Since we are reshaping gl_TessLevel* from an array of floats
403 * to a vec4, this isn't going to work anymore, so use a temporary
404 * array instead.
405 */
406 ir_variable *temp = new(ctx) ir_variable(
407 actual_param->type, "temp_tess_level", ir_var_temporary);
408 this->base_ir->insert_before(temp);
409 actual_param->replace_with(
410 new(ctx) ir_dereference_variable(temp));
411 if (formal_param->data.mode == ir_var_function_in
412 || formal_param->data.mode == ir_var_function_inout) {
413 /* Copy from gl_TessLevel* to the temporary before the call.
414 * Since we are going to insert this copy before the current
415 * instruction, we need to visit it afterwards to make sure it
416 * gets lowered.
417 */
418 ir_assignment *new_assignment = new(ctx) ir_assignment(
419 new(ctx) ir_dereference_variable(temp),
420 actual_param->clone(ctx, NULL));
421 this->base_ir->insert_before(new_assignment);
422 this->visit_new_assignment(new_assignment);
423 }
424 if (formal_param->data.mode == ir_var_function_out
425 || formal_param->data.mode == ir_var_function_inout) {
426 /* Copy from the temporary to gl_TessLevel* after the call.
427 * Since visit_list_elements() has already decided which
428 * instruction it's going to visit next, we need to visit
429 * afterwards to make sure it gets lowered.
430 */
431 ir_assignment *new_assignment = new(ctx) ir_assignment(
432 actual_param->clone(ctx, NULL),
433 new(ctx) ir_dereference_variable(temp));
434 this->base_ir->insert_after(new_assignment);
435 this->visit_new_assignment(new_assignment);
436 }
437 }
438
439 return rvalue_visit(ir);
440 }
441
442
443 bool
444 lower_tess_level(gl_linked_shader *shader)
445 {
446 if ((shader->Stage != MESA_SHADER_TESS_CTRL) &&
447 (shader->Stage != MESA_SHADER_TESS_EVAL))
448 return false;
449
450 lower_tess_level_visitor v(shader->Stage);
451
452 visit_list_elements(&v, shader->ir);
453
454 if (v.new_tess_level_outer_var)
455 shader->symbols->add_variable(v.new_tess_level_outer_var);
456 if (v.new_tess_level_inner_var)
457 shader->symbols->add_variable(v.new_tess_level_inner_var);
458
459 return v.progress;
460 }