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 = expr->operands[1]->constant_expression_value(); 268 if (!old_index_constant) { 269 ir->rhs = new(mem_ctx) ir_expression(ir_triop_vector_insert, 270 expr->operands[0]->type, 271 new_lhs->clone(mem_ctx, NULL), 272 ir->rhs, 273 expr->operands[1]); 274 } 275 ir->set_lhs(new_lhs); 276 277 if (old_index_constant) { 278 /* gl_TessLevel* is being accessed via a constant index. Don't bother 279 * creating a vector insert op. Just use a write mask. 280 */ 281 ir->write_mask = 1 << old_index_constant->get_int_component(0); 282 } else { 283 ir->write_mask = (1 << expr->operands[0]->type->vector_elements) - 1; 284 } 285 } 286 287 /** 288 * Replace any assignment having a gl_TessLevel* (undereferenced) as 289 * its LHS or RHS with a sequence of assignments, one for each component of 290 * the array. Each of these assignments is lowered to refer to 291 * gl_TessLevel*MESA as appropriate. 292 */ 293 ir_visitor_status 294 lower_tess_level_visitor::visit_leave(ir_assignment *ir) 295 { 296 /* First invoke the base class visitor. This causes handle_rvalue() to be 297 * called on ir->rhs and ir->condition. 298 */ 299 ir_rvalue_visitor::visit_leave(ir); 300 301 if (this->is_tess_level_array(ir->lhs) || 302 this->is_tess_level_array(ir->rhs)) { 303 /* LHS or RHS of the assignment is the entire gl_TessLevel* array. 304 * Since we are 305 * reshaping gl_TessLevel* from an array of floats to a 306 * vec4, this isn't going to work as a bulk assignment anymore, so 307 * unroll it to element-by-element assignments and lower each of them. 308 * 309 * Note: to unroll into element-by-element assignments, we need to make 310 * clones of the LHS and RHS. This is safe because expressions and 311 * l-values are side-effect free. 312 */ 313 void *ctx = ralloc_parent(ir); 314 int array_size = ir->lhs->type->array_size(); 315 for (int i = 0; i < array_size; ++i) { 316 ir_dereference_array *new_lhs = new(ctx) ir_dereference_array( 317 ir->lhs->clone(ctx, NULL), new(ctx) ir_constant(i)); 318 ir_dereference_array *new_rhs = new(ctx) ir_dereference_array( 319 ir->rhs->clone(ctx, NULL), new(ctx) ir_constant(i)); 320 this->handle_rvalue((ir_rvalue **) &new_rhs); 321 322 /* Handle the LHS after creating the new assignment. This must 323 * happen in this order because handle_rvalue may replace the old LHS 324 * with an ir_expression of ir_binop_vector_extract. Since this is 325 * not a valide l-value, this will cause an assertion in the 326 * ir_assignment constructor to fail. 327 * 328 * If this occurs, replace the mangled LHS with a dereference of the 329 * vector, and replace the RHS with an ir_triop_vector_insert. 330 */ 331 ir_assignment *const assign = new(ctx) ir_assignment(new_lhs, new_rhs); 332 this->handle_rvalue((ir_rvalue **) &assign->lhs); 333 this->fix_lhs(assign); 334 335 this->base_ir->insert_before(assign); 336 } 337 ir->remove(); 338 339 return visit_continue; 340 } 341 342 /* Handle the LHS as if it were an r-value. Normally 343 * rvalue_visit(ir_assignment *) only visits the RHS, but we need to lower 344 * expressions in the LHS as well. 345 * 346 * This may cause the LHS to get replaced with an ir_expression of 347 * ir_binop_vector_extract. If this occurs, replace it with a dereference 348 * of the vector, and replace the RHS with an ir_triop_vector_insert. 349 */ 350 handle_rvalue((ir_rvalue **)&ir->lhs); 351 this->fix_lhs(ir); 352 353 return rvalue_visit(ir); 354 } 355 356 357 /** 358 * Set up base_ir properly and call visit_leave() on a newly created 359 * ir_assignment node. This is used in cases where we have to insert an 360 * ir_assignment in a place where we know the hierarchical visitor won't see 361 * it. 362 */ 363 void 364 lower_tess_level_visitor::visit_new_assignment(ir_assignment *ir) 365 { 366 ir_instruction *old_base_ir = this->base_ir; 367 this->base_ir = ir; 368 ir->accept(this); 369 this->base_ir = old_base_ir; 370 } 371 372 373 /** 374 * If a gl_TessLevel* variable appears as an argument in an ir_call 375 * expression, replace it with a temporary variable, and make sure the ir_call 376 * is preceded and/or followed by assignments that copy the contents of the 377 * temporary variable to and/or from gl_TessLevel*. Each of these 378 * assignments is then lowered to refer to gl_TessLevel*MESA. 379 */ 380 ir_visitor_status 381 lower_tess_level_visitor::visit_leave(ir_call *ir) 382 { 383 void *ctx = ralloc_parent(ir); 384 385 const exec_node *formal_param_node = ir->callee->parameters.get_head_raw(); 386 const exec_node *actual_param_node = ir->actual_parameters.get_head_raw(); 387 while (!actual_param_node->is_tail_sentinel()) { 388 ir_variable *formal_param = (ir_variable *) formal_param_node; 389 ir_rvalue *actual_param = (ir_rvalue *) actual_param_node; 390 391 /* Advance formal_param_node and actual_param_node now so that we can 392 * safely replace actual_param with another node, if necessary, below. 393 */ 394 formal_param_node = formal_param_node->next; 395 actual_param_node = actual_param_node->next; 396 397 if (!this->is_tess_level_array(actual_param)) 398 continue; 399 400 /* User is trying to pass a whole gl_TessLevel* array to a function 401 * call. Since we are reshaping gl_TessLevel* from an array of floats 402 * to a vec4, this isn't going to work anymore, so use a temporary 403 * array instead. 404 */ 405 ir_variable *temp = new(ctx) ir_variable( 406 actual_param->type, "temp_tess_level", ir_var_temporary); 407 this->base_ir->insert_before(temp); 408 actual_param->replace_with( 409 new(ctx) ir_dereference_variable(temp)); 410 if (formal_param->data.mode == ir_var_function_in 411 || formal_param->data.mode == ir_var_function_inout) { 412 /* Copy from gl_TessLevel* to the temporary before the call. 413 * Since we are going to insert this copy before the current 414 * instruction, we need to visit it afterwards to make sure it 415 * gets lowered. 416 */ 417 ir_assignment *new_assignment = new(ctx) ir_assignment( 418 new(ctx) ir_dereference_variable(temp), 419 actual_param->clone(ctx, NULL)); 420 this->base_ir->insert_before(new_assignment); 421 this->visit_new_assignment(new_assignment); 422 } 423 if (formal_param->data.mode == ir_var_function_out 424 || formal_param->data.mode == ir_var_function_inout) { 425 /* Copy from the temporary to gl_TessLevel* after the call. 426 * Since visit_list_elements() has already decided which 427 * instruction it's going to visit next, we need to visit 428 * afterwards to make sure it gets lowered. 429 */ 430 ir_assignment *new_assignment = new(ctx) ir_assignment( 431 actual_param->clone(ctx, NULL), 432 new(ctx) ir_dereference_variable(temp)); 433 this->base_ir->insert_after(new_assignment); 434 this->visit_new_assignment(new_assignment); 435 } 436 } 437 438 return rvalue_visit(ir); 439 } 440 441 442 bool 443 lower_tess_level(gl_linked_shader *shader) 444 { 445 if ((shader->Stage != MESA_SHADER_TESS_CTRL) && 446 (shader->Stage != MESA_SHADER_TESS_EVAL)) 447 return false; 448 449 lower_tess_level_visitor v(shader->Stage); 450 451 visit_list_elements(&v, shader->ir); 452 453 if (v.new_tess_level_outer_var) 454 shader->symbols->add_variable(v.new_tess_level_outer_var); 455 if (v.new_tess_level_inner_var) 456 shader->symbols->add_variable(v.new_tess_level_inner_var); 457 458 return v.progress; 459 } 460