brw_fs_visitor.cpp revision 1e188f2daef1ae31224d2429bcc1fab75c81fb36
1/* 2 * Copyright © 2010 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 * Software is furnished to do so, subject to the following conditions: 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 DEALINGS 21 * IN THE SOFTWARE. 22 */ 23 24/** @file brw_fs_visitor.cpp 25 * 26 * This file supports generating the FS LIR from the GLSL IR. The LIR 27 * makes it easier to do backend-specific optimizations than doing so 28 * in the GLSL IR or in the native code. 29 */ 30extern "C" { 31 32#include <sys/types.h> 33 34#include "main/macros.h" 35#include "main/shaderobj.h" 36#include "main/uniforms.h" 37#include "program/prog_parameter.h" 38#include "program/prog_print.h" 39#include "program/prog_optimize.h" 40#include "program/register_allocate.h" 41#include "program/sampler.h" 42#include "program/hash_table.h" 43#include "brw_context.h" 44#include "brw_eu.h" 45#include "brw_wm.h" 46} 47#include "brw_shader.h" 48#include "brw_fs.h" 49#include "glsl/glsl_types.h" 50#include "glsl/ir_optimization.h" 51#include "glsl/ir_print_visitor.h" 52 53void 54fs_visitor::visit(ir_variable *ir) 55{ 56 fs_reg *reg = NULL; 57 58 if (variable_storage(ir)) 59 return; 60 61 if (ir->mode == ir_var_in) { 62 if (!strcmp(ir->name, "gl_FragCoord")) { 63 reg = emit_fragcoord_interpolation(ir); 64 } else if (!strcmp(ir->name, "gl_FrontFacing")) { 65 reg = emit_frontfacing_interpolation(ir); 66 } else { 67 reg = emit_general_interpolation(ir); 68 } 69 assert(reg); 70 hash_table_insert(this->variable_ht, reg, ir); 71 return; 72 } else if (ir->mode == ir_var_out) { 73 reg = new(this->mem_ctx) fs_reg(this, ir->type); 74 75 if (ir->location == FRAG_RESULT_COLOR) { 76 /* Writing gl_FragColor outputs to all color regions. */ 77 for (unsigned int i = 0; i < MAX2(c->key.nr_color_regions, 1); i++) { 78 this->outputs[i] = *reg; 79 } 80 } else if (ir->location == FRAG_RESULT_DEPTH) { 81 this->frag_depth = ir; 82 } else { 83 /* gl_FragData or a user-defined FS output */ 84 assert(ir->location >= FRAG_RESULT_DATA0 && 85 ir->location < FRAG_RESULT_DATA0 + BRW_MAX_DRAW_BUFFERS); 86 87 /* General color output. */ 88 for (unsigned int i = 0; i < MAX2(1, ir->type->length); i++) { 89 int output = ir->location - FRAG_RESULT_DATA0 + i; 90 this->outputs[output] = *reg; 91 this->outputs[output].reg_offset += 4 * i; 92 } 93 } 94 } else if (ir->mode == ir_var_uniform) { 95 int param_index = c->prog_data.nr_params; 96 97 if (c->dispatch_width == 16) { 98 if (!variable_storage(ir)) { 99 fail("Failed to find uniform '%s' in 16-wide\n", ir->name); 100 } 101 return; 102 } 103 104 if (!strncmp(ir->name, "gl_", 3)) { 105 setup_builtin_uniform_values(ir); 106 } else { 107 setup_uniform_values(ir->location, ir->type); 108 } 109 110 reg = new(this->mem_ctx) fs_reg(UNIFORM, param_index); 111 reg->type = brw_type_for_base_type(ir->type); 112 } 113 114 if (!reg) 115 reg = new(this->mem_ctx) fs_reg(this, ir->type); 116 117 hash_table_insert(this->variable_ht, reg, ir); 118} 119 120void 121fs_visitor::visit(ir_dereference_variable *ir) 122{ 123 fs_reg *reg = variable_storage(ir->var); 124 this->result = *reg; 125} 126 127void 128fs_visitor::visit(ir_dereference_record *ir) 129{ 130 const glsl_type *struct_type = ir->record->type; 131 132 ir->record->accept(this); 133 134 unsigned int offset = 0; 135 for (unsigned int i = 0; i < struct_type->length; i++) { 136 if (strcmp(struct_type->fields.structure[i].name, ir->field) == 0) 137 break; 138 offset += type_size(struct_type->fields.structure[i].type); 139 } 140 this->result.reg_offset += offset; 141 this->result.type = brw_type_for_base_type(ir->type); 142} 143 144void 145fs_visitor::visit(ir_dereference_array *ir) 146{ 147 ir_constant *index; 148 int element_size; 149 150 ir->array->accept(this); 151 index = ir->array_index->as_constant(); 152 153 element_size = type_size(ir->type); 154 this->result.type = brw_type_for_base_type(ir->type); 155 156 if (index) { 157 assert(this->result.file == UNIFORM || this->result.file == GRF); 158 this->result.reg_offset += index->value.i[0] * element_size; 159 } else { 160 assert(!"FINISHME: non-constant array element"); 161 } 162} 163 164/* Instruction selection: Produce a MOV.sat instead of 165 * MIN(MAX(val, 0), 1) when possible. 166 */ 167bool 168fs_visitor::try_emit_saturate(ir_expression *ir) 169{ 170 ir_rvalue *sat_val = ir->as_rvalue_to_saturate(); 171 172 if (!sat_val) 173 return false; 174 175 fs_inst *pre_inst = (fs_inst *) this->instructions.get_tail(); 176 177 sat_val->accept(this); 178 fs_reg src = this->result; 179 180 fs_inst *last_inst = (fs_inst *) this->instructions.get_tail(); 181 182 /* If the last instruction from our accept() didn't generate our 183 * src, generate a saturated MOV 184 */ 185 fs_inst *modify = get_instruction_generating_reg(pre_inst, last_inst, src); 186 if (!modify || modify->regs_written() != 1) { 187 fs_inst *inst = emit(BRW_OPCODE_MOV, this->result, src); 188 inst->saturate = true; 189 } else { 190 modify->saturate = true; 191 this->result = src; 192 } 193 194 195 return true; 196} 197 198bool 199fs_visitor::try_emit_mad(ir_expression *ir, int mul_arg) 200{ 201 /* 3-src instructions were introduced in gen6. */ 202 if (intel->gen < 6) 203 return false; 204 205 /* MAD can only handle floating-point data. */ 206 if (ir->type != glsl_type::float_type) 207 return false; 208 209 ir_rvalue *nonmul = ir->operands[1 - mul_arg]; 210 ir_expression *mul = ir->operands[mul_arg]->as_expression(); 211 212 if (!mul || mul->operation != ir_binop_mul) 213 return false; 214 215 if (nonmul->as_constant() || 216 mul->operands[0]->as_constant() || 217 mul->operands[1]->as_constant()) 218 return false; 219 220 nonmul->accept(this); 221 fs_reg src0 = this->result; 222 223 mul->operands[0]->accept(this); 224 fs_reg src1 = this->result; 225 226 mul->operands[1]->accept(this); 227 fs_reg src2 = this->result; 228 229 this->result = fs_reg(this, ir->type); 230 emit(BRW_OPCODE_MAD, this->result, src0, src1, src2); 231 232 return true; 233} 234 235void 236fs_visitor::visit(ir_expression *ir) 237{ 238 unsigned int operand; 239 fs_reg op[2], temp; 240 fs_inst *inst; 241 242 assert(ir->get_num_operands() <= 2); 243 244 if (try_emit_saturate(ir)) 245 return; 246 if (ir->operation == ir_binop_add) { 247 if (try_emit_mad(ir, 0) || try_emit_mad(ir, 1)) 248 return; 249 } 250 251 for (operand = 0; operand < ir->get_num_operands(); operand++) { 252 ir->operands[operand]->accept(this); 253 if (this->result.file == BAD_FILE) { 254 ir_print_visitor v; 255 fail("Failed to get tree for expression operand:\n"); 256 ir->operands[operand]->accept(&v); 257 } 258 op[operand] = this->result; 259 260 /* Matrix expression operands should have been broken down to vector 261 * operations already. 262 */ 263 assert(!ir->operands[operand]->type->is_matrix()); 264 /* And then those vector operands should have been broken down to scalar. 265 */ 266 assert(!ir->operands[operand]->type->is_vector()); 267 } 268 269 /* Storage for our result. If our result goes into an assignment, it will 270 * just get copy-propagated out, so no worries. 271 */ 272 this->result = fs_reg(this, ir->type); 273 274 switch (ir->operation) { 275 case ir_unop_logic_not: 276 /* Note that BRW_OPCODE_NOT is not appropriate here, since it is 277 * ones complement of the whole register, not just bit 0. 278 */ 279 emit(BRW_OPCODE_XOR, this->result, op[0], fs_reg(1)); 280 break; 281 case ir_unop_neg: 282 op[0].negate = !op[0].negate; 283 this->result = op[0]; 284 break; 285 case ir_unop_abs: 286 op[0].abs = true; 287 op[0].negate = false; 288 this->result = op[0]; 289 break; 290 case ir_unop_sign: 291 temp = fs_reg(this, ir->type); 292 293 emit(BRW_OPCODE_MOV, this->result, fs_reg(0.0f)); 294 295 inst = emit(BRW_OPCODE_CMP, reg_null_f, op[0], fs_reg(0.0f)); 296 inst->conditional_mod = BRW_CONDITIONAL_G; 297 inst = emit(BRW_OPCODE_MOV, this->result, fs_reg(1.0f)); 298 inst->predicated = true; 299 300 inst = emit(BRW_OPCODE_CMP, reg_null_f, op[0], fs_reg(0.0f)); 301 inst->conditional_mod = BRW_CONDITIONAL_L; 302 inst = emit(BRW_OPCODE_MOV, this->result, fs_reg(-1.0f)); 303 inst->predicated = true; 304 305 break; 306 case ir_unop_rcp: 307 emit_math(SHADER_OPCODE_RCP, this->result, op[0]); 308 break; 309 310 case ir_unop_exp2: 311 emit_math(SHADER_OPCODE_EXP2, this->result, op[0]); 312 break; 313 case ir_unop_log2: 314 emit_math(SHADER_OPCODE_LOG2, this->result, op[0]); 315 break; 316 case ir_unop_exp: 317 case ir_unop_log: 318 assert(!"not reached: should be handled by ir_explog_to_explog2"); 319 break; 320 case ir_unop_sin: 321 case ir_unop_sin_reduced: 322 emit_math(SHADER_OPCODE_SIN, this->result, op[0]); 323 break; 324 case ir_unop_cos: 325 case ir_unop_cos_reduced: 326 emit_math(SHADER_OPCODE_COS, this->result, op[0]); 327 break; 328 329 case ir_unop_dFdx: 330 emit(FS_OPCODE_DDX, this->result, op[0]); 331 break; 332 case ir_unop_dFdy: 333 emit(FS_OPCODE_DDY, this->result, op[0]); 334 break; 335 336 case ir_binop_add: 337 emit(BRW_OPCODE_ADD, this->result, op[0], op[1]); 338 break; 339 case ir_binop_sub: 340 assert(!"not reached: should be handled by ir_sub_to_add_neg"); 341 break; 342 343 case ir_binop_mul: 344 if (ir->type->is_integer()) { 345 /* For integer multiplication, the MUL uses the low 16 bits 346 * of one of the operands (src0 on gen6, src1 on gen7). The 347 * MACH accumulates in the contribution of the upper 16 bits 348 * of that operand. 349 * 350 * FINISHME: Emit just the MUL if we know an operand is small 351 * enough. 352 */ 353 if (intel->gen >= 7 && c->dispatch_width == 16) 354 fail("16-wide explicit accumulator operands unsupported\n"); 355 356 struct brw_reg acc = retype(brw_acc_reg(), BRW_REGISTER_TYPE_D); 357 358 emit(BRW_OPCODE_MUL, acc, op[0], op[1]); 359 emit(BRW_OPCODE_MACH, reg_null_d, op[0], op[1]); 360 emit(BRW_OPCODE_MOV, this->result, fs_reg(acc)); 361 } else { 362 emit(BRW_OPCODE_MUL, this->result, op[0], op[1]); 363 } 364 break; 365 case ir_binop_div: 366 if (intel->gen >= 7 && c->dispatch_width == 16) 367 fail("16-wide INTDIV unsupported\n"); 368 369 /* Floating point should be lowered by DIV_TO_MUL_RCP in the compiler. */ 370 assert(ir->type->is_integer()); 371 emit_math(SHADER_OPCODE_INT_QUOTIENT, this->result, op[0], op[1]); 372 break; 373 case ir_binop_mod: 374 if (intel->gen >= 7 && c->dispatch_width == 16) 375 fail("16-wide INTDIV unsupported\n"); 376 377 /* Floating point should be lowered by MOD_TO_FRACT in the compiler. */ 378 assert(ir->type->is_integer()); 379 emit_math(SHADER_OPCODE_INT_REMAINDER, this->result, op[0], op[1]); 380 break; 381 382 case ir_binop_less: 383 case ir_binop_greater: 384 case ir_binop_lequal: 385 case ir_binop_gequal: 386 case ir_binop_equal: 387 case ir_binop_all_equal: 388 case ir_binop_nequal: 389 case ir_binop_any_nequal: 390 temp = this->result; 391 /* original gen4 does implicit conversion before comparison. */ 392 if (intel->gen < 5) 393 temp.type = op[0].type; 394 395 resolve_ud_negate(&op[0]); 396 resolve_ud_negate(&op[1]); 397 398 resolve_bool_comparison(ir->operands[0], &op[0]); 399 resolve_bool_comparison(ir->operands[1], &op[1]); 400 401 inst = emit(BRW_OPCODE_CMP, temp, op[0], op[1]); 402 inst->conditional_mod = brw_conditional_for_comparison(ir->operation); 403 break; 404 405 case ir_binop_logic_xor: 406 emit(BRW_OPCODE_XOR, this->result, op[0], op[1]); 407 break; 408 409 case ir_binop_logic_or: 410 emit(BRW_OPCODE_OR, this->result, op[0], op[1]); 411 break; 412 413 case ir_binop_logic_and: 414 emit(BRW_OPCODE_AND, this->result, op[0], op[1]); 415 break; 416 417 case ir_binop_dot: 418 case ir_unop_any: 419 assert(!"not reached: should be handled by brw_fs_channel_expressions"); 420 break; 421 422 case ir_unop_noise: 423 assert(!"not reached: should be handled by lower_noise"); 424 break; 425 426 case ir_quadop_vector: 427 assert(!"not reached: should be handled by lower_quadop_vector"); 428 break; 429 430 case ir_unop_sqrt: 431 emit_math(SHADER_OPCODE_SQRT, this->result, op[0]); 432 break; 433 434 case ir_unop_rsq: 435 emit_math(SHADER_OPCODE_RSQ, this->result, op[0]); 436 break; 437 438 case ir_unop_i2u: 439 op[0].type = BRW_REGISTER_TYPE_UD; 440 this->result = op[0]; 441 break; 442 case ir_unop_u2i: 443 op[0].type = BRW_REGISTER_TYPE_D; 444 this->result = op[0]; 445 break; 446 case ir_unop_i2f: 447 case ir_unop_u2f: 448 case ir_unop_f2i: 449 emit(BRW_OPCODE_MOV, this->result, op[0]); 450 break; 451 452 case ir_unop_b2i: 453 inst = emit(BRW_OPCODE_AND, this->result, op[0], fs_reg(1)); 454 break; 455 case ir_unop_b2f: 456 temp = fs_reg(this, glsl_type::int_type); 457 emit(BRW_OPCODE_AND, temp, op[0], fs_reg(1)); 458 emit(BRW_OPCODE_MOV, this->result, temp); 459 break; 460 461 case ir_unop_f2b: 462 case ir_unop_i2b: 463 temp = this->result; 464 /* original gen4 does implicit conversion before comparison. */ 465 if (intel->gen < 5) 466 temp.type = op[0].type; 467 468 resolve_ud_negate(&op[0]); 469 470 inst = emit(BRW_OPCODE_CMP, temp, op[0], fs_reg(0.0f)); 471 inst->conditional_mod = BRW_CONDITIONAL_NZ; 472 break; 473 474 case ir_unop_trunc: 475 emit(BRW_OPCODE_RNDZ, this->result, op[0]); 476 break; 477 case ir_unop_ceil: 478 op[0].negate = !op[0].negate; 479 inst = emit(BRW_OPCODE_RNDD, this->result, op[0]); 480 this->result.negate = true; 481 break; 482 case ir_unop_floor: 483 inst = emit(BRW_OPCODE_RNDD, this->result, op[0]); 484 break; 485 case ir_unop_fract: 486 inst = emit(BRW_OPCODE_FRC, this->result, op[0]); 487 break; 488 case ir_unop_round_even: 489 emit(BRW_OPCODE_RNDE, this->result, op[0]); 490 break; 491 492 case ir_binop_min: 493 resolve_ud_negate(&op[0]); 494 resolve_ud_negate(&op[1]); 495 496 if (intel->gen >= 6) { 497 inst = emit(BRW_OPCODE_SEL, this->result, op[0], op[1]); 498 inst->conditional_mod = BRW_CONDITIONAL_L; 499 } else { 500 /* Unalias the destination */ 501 this->result = fs_reg(this, ir->type); 502 503 inst = emit(BRW_OPCODE_CMP, this->result, op[0], op[1]); 504 inst->conditional_mod = BRW_CONDITIONAL_L; 505 506 inst = emit(BRW_OPCODE_SEL, this->result, op[0], op[1]); 507 inst->predicated = true; 508 } 509 break; 510 case ir_binop_max: 511 resolve_ud_negate(&op[0]); 512 resolve_ud_negate(&op[1]); 513 514 if (intel->gen >= 6) { 515 inst = emit(BRW_OPCODE_SEL, this->result, op[0], op[1]); 516 inst->conditional_mod = BRW_CONDITIONAL_GE; 517 } else { 518 /* Unalias the destination */ 519 this->result = fs_reg(this, ir->type); 520 521 inst = emit(BRW_OPCODE_CMP, this->result, op[0], op[1]); 522 inst->conditional_mod = BRW_CONDITIONAL_G; 523 524 inst = emit(BRW_OPCODE_SEL, this->result, op[0], op[1]); 525 inst->predicated = true; 526 } 527 break; 528 529 case ir_binop_pow: 530 emit_math(SHADER_OPCODE_POW, this->result, op[0], op[1]); 531 break; 532 533 case ir_unop_bit_not: 534 inst = emit(BRW_OPCODE_NOT, this->result, op[0]); 535 break; 536 case ir_binop_bit_and: 537 inst = emit(BRW_OPCODE_AND, this->result, op[0], op[1]); 538 break; 539 case ir_binop_bit_xor: 540 inst = emit(BRW_OPCODE_XOR, this->result, op[0], op[1]); 541 break; 542 case ir_binop_bit_or: 543 inst = emit(BRW_OPCODE_OR, this->result, op[0], op[1]); 544 break; 545 546 case ir_binop_lshift: 547 inst = emit(BRW_OPCODE_SHL, this->result, op[0], op[1]); 548 break; 549 550 case ir_binop_rshift: 551 if (ir->type->base_type == GLSL_TYPE_INT) 552 inst = emit(BRW_OPCODE_ASR, this->result, op[0], op[1]); 553 else 554 inst = emit(BRW_OPCODE_SHR, this->result, op[0], op[1]); 555 break; 556 } 557} 558 559void 560fs_visitor::emit_assignment_writes(fs_reg &l, fs_reg &r, 561 const glsl_type *type, bool predicated) 562{ 563 switch (type->base_type) { 564 case GLSL_TYPE_FLOAT: 565 case GLSL_TYPE_UINT: 566 case GLSL_TYPE_INT: 567 case GLSL_TYPE_BOOL: 568 for (unsigned int i = 0; i < type->components(); i++) { 569 l.type = brw_type_for_base_type(type); 570 r.type = brw_type_for_base_type(type); 571 572 if (predicated || !l.equals(r)) { 573 fs_inst *inst = emit(BRW_OPCODE_MOV, l, r); 574 inst->predicated = predicated; 575 } 576 577 l.reg_offset++; 578 r.reg_offset++; 579 } 580 break; 581 case GLSL_TYPE_ARRAY: 582 for (unsigned int i = 0; i < type->length; i++) { 583 emit_assignment_writes(l, r, type->fields.array, predicated); 584 } 585 break; 586 587 case GLSL_TYPE_STRUCT: 588 for (unsigned int i = 0; i < type->length; i++) { 589 emit_assignment_writes(l, r, type->fields.structure[i].type, 590 predicated); 591 } 592 break; 593 594 case GLSL_TYPE_SAMPLER: 595 break; 596 597 default: 598 assert(!"not reached"); 599 break; 600 } 601} 602 603/* If the RHS processing resulted in an instruction generating a 604 * temporary value, and it would be easy to rewrite the instruction to 605 * generate its result right into the LHS instead, do so. This ends 606 * up reliably removing instructions where it can be tricky to do so 607 * later without real UD chain information. 608 */ 609bool 610fs_visitor::try_rewrite_rhs_to_dst(ir_assignment *ir, 611 fs_reg dst, 612 fs_reg src, 613 fs_inst *pre_rhs_inst, 614 fs_inst *last_rhs_inst) 615{ 616 /* Only attempt if we're doing a direct assignment. */ 617 if (ir->condition || 618 !(ir->lhs->type->is_scalar() || 619 (ir->lhs->type->is_vector() && 620 ir->write_mask == (1 << ir->lhs->type->vector_elements) - 1))) 621 return false; 622 623 /* Make sure the last instruction generated our source reg. */ 624 fs_inst *modify = get_instruction_generating_reg(pre_rhs_inst, 625 last_rhs_inst, 626 src); 627 if (!modify) 628 return false; 629 630 /* If last_rhs_inst wrote a different number of components than our LHS, 631 * we can't safely rewrite it. 632 */ 633 if (ir->lhs->type->vector_elements != modify->regs_written()) 634 return false; 635 636 /* Success! Rewrite the instruction. */ 637 modify->dst = dst; 638 639 return true; 640} 641 642void 643fs_visitor::visit(ir_assignment *ir) 644{ 645 fs_reg l, r; 646 fs_inst *inst; 647 648 /* FINISHME: arrays on the lhs */ 649 ir->lhs->accept(this); 650 l = this->result; 651 652 fs_inst *pre_rhs_inst = (fs_inst *) this->instructions.get_tail(); 653 654 ir->rhs->accept(this); 655 r = this->result; 656 657 fs_inst *last_rhs_inst = (fs_inst *) this->instructions.get_tail(); 658 659 assert(l.file != BAD_FILE); 660 assert(r.file != BAD_FILE); 661 662 if (try_rewrite_rhs_to_dst(ir, l, r, pre_rhs_inst, last_rhs_inst)) 663 return; 664 665 if (ir->condition) { 666 emit_bool_to_cond_code(ir->condition); 667 } 668 669 if (ir->lhs->type->is_scalar() || 670 ir->lhs->type->is_vector()) { 671 for (int i = 0; i < ir->lhs->type->vector_elements; i++) { 672 if (ir->write_mask & (1 << i)) { 673 inst = emit(BRW_OPCODE_MOV, l, r); 674 if (ir->condition) 675 inst->predicated = true; 676 r.reg_offset++; 677 } 678 l.reg_offset++; 679 } 680 } else { 681 emit_assignment_writes(l, r, ir->lhs->type, ir->condition != NULL); 682 } 683} 684 685fs_inst * 686fs_visitor::emit_texture_gen4(ir_texture *ir, fs_reg dst, fs_reg coordinate, 687 int sampler) 688{ 689 int mlen; 690 int base_mrf = 1; 691 bool simd16 = false; 692 fs_reg orig_dst; 693 694 /* g0 header. */ 695 mlen = 1; 696 697 if (ir->shadow_comparitor && ir->op != ir_txd) { 698 for (int i = 0; i < ir->coordinate->type->vector_elements; i++) { 699 emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen + i), coordinate); 700 coordinate.reg_offset++; 701 } 702 /* gen4's SIMD8 sampler always has the slots for u,v,r present. */ 703 mlen += 3; 704 705 if (ir->op == ir_tex) { 706 /* There's no plain shadow compare message, so we use shadow 707 * compare with a bias of 0.0. 708 */ 709 emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen), fs_reg(0.0f)); 710 mlen++; 711 } else if (ir->op == ir_txb) { 712 ir->lod_info.bias->accept(this); 713 emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen), this->result); 714 mlen++; 715 } else { 716 assert(ir->op == ir_txl); 717 ir->lod_info.lod->accept(this); 718 emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen), this->result); 719 mlen++; 720 } 721 722 ir->shadow_comparitor->accept(this); 723 emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen), this->result); 724 mlen++; 725 } else if (ir->op == ir_tex) { 726 for (int i = 0; i < ir->coordinate->type->vector_elements; i++) { 727 emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen + i), coordinate); 728 coordinate.reg_offset++; 729 } 730 /* gen4's SIMD8 sampler always has the slots for u,v,r present. */ 731 mlen += 3; 732 } else if (ir->op == ir_txd) { 733 ir->lod_info.grad.dPdx->accept(this); 734 fs_reg dPdx = this->result; 735 736 ir->lod_info.grad.dPdy->accept(this); 737 fs_reg dPdy = this->result; 738 739 for (int i = 0; i < ir->coordinate->type->vector_elements; i++) { 740 emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen + i), coordinate); 741 coordinate.reg_offset++; 742 } 743 /* the slots for u and v are always present, but r is optional */ 744 mlen += MAX2(ir->coordinate->type->vector_elements, 2); 745 746 /* P = u, v, r 747 * dPdx = dudx, dvdx, drdx 748 * dPdy = dudy, dvdy, drdy 749 * 750 * 1-arg: Does not exist. 751 * 752 * 2-arg: dudx dvdx dudy dvdy 753 * dPdx.x dPdx.y dPdy.x dPdy.y 754 * m4 m5 m6 m7 755 * 756 * 3-arg: dudx dvdx drdx dudy dvdy drdy 757 * dPdx.x dPdx.y dPdx.z dPdy.x dPdy.y dPdy.z 758 * m5 m6 m7 m8 m9 m10 759 */ 760 for (int i = 0; i < ir->lod_info.grad.dPdx->type->vector_elements; i++) { 761 emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen), dPdx); 762 dPdx.reg_offset++; 763 } 764 mlen += MAX2(ir->lod_info.grad.dPdx->type->vector_elements, 2); 765 766 for (int i = 0; i < ir->lod_info.grad.dPdy->type->vector_elements; i++) { 767 emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen), dPdy); 768 dPdy.reg_offset++; 769 } 770 mlen += MAX2(ir->lod_info.grad.dPdy->type->vector_elements, 2); 771 } else if (ir->op == ir_txs) { 772 /* There's no SIMD8 resinfo message on Gen4. Use SIMD16 instead. */ 773 simd16 = true; 774 ir->lod_info.lod->accept(this); 775 emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen, BRW_REGISTER_TYPE_UD), this->result); 776 mlen += 2; 777 } else { 778 /* Oh joy. gen4 doesn't have SIMD8 non-shadow-compare bias/lod 779 * instructions. We'll need to do SIMD16 here. 780 */ 781 simd16 = true; 782 assert(ir->op == ir_txb || ir->op == ir_txl || ir->op == ir_txf); 783 784 for (int i = 0; i < ir->coordinate->type->vector_elements; i++) { 785 emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen + i * 2, coordinate.type), 786 coordinate); 787 coordinate.reg_offset++; 788 } 789 790 /* Initialize the rest of u/v/r with 0.0. Empirically, this seems to 791 * be necessary for TXF (ld), but seems wise to do for all messages. 792 */ 793 for (int i = ir->coordinate->type->vector_elements; i < 3; i++) { 794 emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen + i * 2), fs_reg(0.0f)); 795 } 796 797 /* lod/bias appears after u/v/r. */ 798 mlen += 6; 799 800 if (ir->op == ir_txb) { 801 ir->lod_info.bias->accept(this); 802 emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen), this->result); 803 mlen++; 804 } else { 805 ir->lod_info.lod->accept(this); 806 emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen, this->result.type), 807 this->result); 808 mlen++; 809 } 810 811 /* The unused upper half. */ 812 mlen++; 813 } 814 815 if (simd16) { 816 /* Now, since we're doing simd16, the return is 2 interleaved 817 * vec4s where the odd-indexed ones are junk. We'll need to move 818 * this weirdness around to the expected layout. 819 */ 820 orig_dst = dst; 821 const glsl_type *vec_type = 822 glsl_type::get_instance(ir->type->base_type, 4, 1); 823 dst = fs_reg(this, glsl_type::get_array_instance(vec_type, 2)); 824 dst.type = intel->is_g4x ? brw_type_for_base_type(ir->type) 825 : BRW_REGISTER_TYPE_F; 826 } 827 828 fs_inst *inst = NULL; 829 switch (ir->op) { 830 case ir_tex: 831 inst = emit(SHADER_OPCODE_TEX, dst); 832 break; 833 case ir_txb: 834 inst = emit(FS_OPCODE_TXB, dst); 835 break; 836 case ir_txl: 837 inst = emit(SHADER_OPCODE_TXL, dst); 838 break; 839 case ir_txd: 840 inst = emit(SHADER_OPCODE_TXD, dst); 841 break; 842 case ir_txs: 843 inst = emit(SHADER_OPCODE_TXS, dst); 844 break; 845 case ir_txf: 846 inst = emit(SHADER_OPCODE_TXF, dst); 847 break; 848 } 849 inst->base_mrf = base_mrf; 850 inst->mlen = mlen; 851 inst->header_present = true; 852 853 if (simd16) { 854 for (int i = 0; i < 4; i++) { 855 emit(BRW_OPCODE_MOV, orig_dst, dst); 856 orig_dst.reg_offset++; 857 dst.reg_offset += 2; 858 } 859 } 860 861 return inst; 862} 863 864/* gen5's sampler has slots for u, v, r, array index, then optional 865 * parameters like shadow comparitor or LOD bias. If optional 866 * parameters aren't present, those base slots are optional and don't 867 * need to be included in the message. 868 * 869 * We don't fill in the unnecessary slots regardless, which may look 870 * surprising in the disassembly. 871 */ 872fs_inst * 873fs_visitor::emit_texture_gen5(ir_texture *ir, fs_reg dst, fs_reg coordinate, 874 int sampler) 875{ 876 int mlen = 0; 877 int base_mrf = 2; 878 int reg_width = c->dispatch_width / 8; 879 bool header_present = false; 880 const int vector_elements = 881 ir->coordinate ? ir->coordinate->type->vector_elements : 0; 882 883 if (ir->offset) { 884 /* The offsets set up by the ir_texture visitor are in the 885 * m1 header, so we can't go headerless. 886 */ 887 header_present = true; 888 mlen++; 889 base_mrf--; 890 } 891 892 for (int i = 0; i < vector_elements; i++) { 893 emit(BRW_OPCODE_MOV, 894 fs_reg(MRF, base_mrf + mlen + i * reg_width, coordinate.type), 895 coordinate); 896 coordinate.reg_offset++; 897 } 898 mlen += vector_elements * reg_width; 899 900 if (ir->shadow_comparitor && ir->op != ir_txd) { 901 mlen = MAX2(mlen, header_present + 4 * reg_width); 902 903 ir->shadow_comparitor->accept(this); 904 emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen), this->result); 905 mlen += reg_width; 906 } 907 908 fs_inst *inst = NULL; 909 switch (ir->op) { 910 case ir_tex: 911 inst = emit(SHADER_OPCODE_TEX, dst); 912 break; 913 case ir_txb: 914 ir->lod_info.bias->accept(this); 915 mlen = MAX2(mlen, header_present + 4 * reg_width); 916 emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen), this->result); 917 mlen += reg_width; 918 919 inst = emit(FS_OPCODE_TXB, dst); 920 921 break; 922 case ir_txl: 923 ir->lod_info.lod->accept(this); 924 mlen = MAX2(mlen, header_present + 4 * reg_width); 925 emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen), this->result); 926 mlen += reg_width; 927 928 inst = emit(SHADER_OPCODE_TXL, dst); 929 break; 930 case ir_txd: { 931 ir->lod_info.grad.dPdx->accept(this); 932 fs_reg dPdx = this->result; 933 934 ir->lod_info.grad.dPdy->accept(this); 935 fs_reg dPdy = this->result; 936 937 mlen = MAX2(mlen, header_present + 4 * reg_width); /* skip over 'ai' */ 938 939 /** 940 * P = u, v, r 941 * dPdx = dudx, dvdx, drdx 942 * dPdy = dudy, dvdy, drdy 943 * 944 * Load up these values: 945 * - dudx dudy dvdx dvdy drdx drdy 946 * - dPdx.x dPdy.x dPdx.y dPdy.y dPdx.z dPdy.z 947 */ 948 for (int i = 0; i < ir->lod_info.grad.dPdx->type->vector_elements; i++) { 949 emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen), dPdx); 950 dPdx.reg_offset++; 951 mlen += reg_width; 952 953 emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen), dPdy); 954 dPdy.reg_offset++; 955 mlen += reg_width; 956 } 957 958 inst = emit(SHADER_OPCODE_TXD, dst); 959 break; 960 } 961 case ir_txs: 962 ir->lod_info.lod->accept(this); 963 emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen, BRW_REGISTER_TYPE_UD), this->result); 964 mlen += reg_width; 965 inst = emit(SHADER_OPCODE_TXS, dst); 966 break; 967 case ir_txf: 968 mlen = header_present + 4 * reg_width; 969 970 ir->lod_info.lod->accept(this); 971 emit(BRW_OPCODE_MOV, 972 fs_reg(MRF, base_mrf + mlen - reg_width, BRW_REGISTER_TYPE_UD), 973 this->result); 974 inst = emit(SHADER_OPCODE_TXF, dst); 975 break; 976 } 977 inst->base_mrf = base_mrf; 978 inst->mlen = mlen; 979 inst->header_present = header_present; 980 981 if (mlen > 11) { 982 fail("Message length >11 disallowed by hardware\n"); 983 } 984 985 return inst; 986} 987 988fs_inst * 989fs_visitor::emit_texture_gen7(ir_texture *ir, fs_reg dst, fs_reg coordinate, 990 int sampler) 991{ 992 int mlen = 0; 993 int base_mrf = 2; 994 int reg_width = c->dispatch_width / 8; 995 bool header_present = false; 996 int offsets[3]; 997 998 if (ir->offset && ir->op != ir_txf) { 999 /* The offsets set up by the ir_texture visitor are in the 1000 * m1 header, so we can't go headerless. 1001 */ 1002 header_present = true; 1003 mlen++; 1004 base_mrf--; 1005 } 1006 1007 if (ir->shadow_comparitor && ir->op != ir_txd) { 1008 ir->shadow_comparitor->accept(this); 1009 emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen), this->result); 1010 mlen += reg_width; 1011 } 1012 1013 /* Set up the LOD info */ 1014 switch (ir->op) { 1015 case ir_tex: 1016 break; 1017 case ir_txb: 1018 ir->lod_info.bias->accept(this); 1019 emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen), this->result); 1020 mlen += reg_width; 1021 break; 1022 case ir_txl: 1023 ir->lod_info.lod->accept(this); 1024 emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen), this->result); 1025 mlen += reg_width; 1026 break; 1027 case ir_txd: { 1028 if (c->dispatch_width == 16) 1029 fail("Gen7 does not support sample_d/sample_d_c in SIMD16 mode."); 1030 1031 ir->lod_info.grad.dPdx->accept(this); 1032 fs_reg dPdx = this->result; 1033 1034 ir->lod_info.grad.dPdy->accept(this); 1035 fs_reg dPdy = this->result; 1036 1037 /* Load dPdx and the coordinate together: 1038 * [hdr], [ref], x, dPdx.x, dPdy.x, y, dPdx.y, dPdy.y, z, dPdx.z, dPdy.z 1039 */ 1040 for (int i = 0; i < ir->coordinate->type->vector_elements; i++) { 1041 emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen), coordinate); 1042 coordinate.reg_offset++; 1043 mlen += reg_width; 1044 1045 emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen), dPdx); 1046 dPdx.reg_offset++; 1047 mlen += reg_width; 1048 1049 emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen), dPdy); 1050 dPdy.reg_offset++; 1051 mlen += reg_width; 1052 } 1053 break; 1054 } 1055 case ir_txs: 1056 ir->lod_info.lod->accept(this); 1057 emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen, BRW_REGISTER_TYPE_UD), this->result); 1058 mlen += reg_width; 1059 break; 1060 case ir_txf: 1061 /* It appears that the ld instruction used for txf does its 1062 * address bounds check before adding in the offset. To work 1063 * around this, just add the integer offset to the integer texel 1064 * coordinate, and don't put the offset in the header. 1065 */ 1066 if (ir->offset) { 1067 ir_constant *offset = ir->offset->as_constant(); 1068 offsets[0] = offset->value.i[0]; 1069 offsets[1] = offset->value.i[1]; 1070 offsets[2] = offset->value.i[2]; 1071 } else { 1072 memset(offsets, 0, sizeof(offsets)); 1073 } 1074 1075 /* Unfortunately, the parameters for LD are intermixed: u, lod, v, r. */ 1076 emit(BRW_OPCODE_ADD, 1077 fs_reg(MRF, base_mrf + mlen, BRW_REGISTER_TYPE_D), coordinate, offsets[0]); 1078 coordinate.reg_offset++; 1079 mlen += reg_width; 1080 1081 ir->lod_info.lod->accept(this); 1082 emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen, BRW_REGISTER_TYPE_D), this->result); 1083 mlen += reg_width; 1084 1085 for (int i = 1; i < ir->coordinate->type->vector_elements; i++) { 1086 emit(BRW_OPCODE_ADD, 1087 fs_reg(MRF, base_mrf + mlen, BRW_REGISTER_TYPE_D), coordinate, offsets[i]); 1088 coordinate.reg_offset++; 1089 mlen += reg_width; 1090 } 1091 break; 1092 } 1093 1094 /* Set up the coordinate (except for cases where it was done above) */ 1095 if (ir->op != ir_txd && ir->op != ir_txs && ir->op != ir_txf) { 1096 for (int i = 0; i < ir->coordinate->type->vector_elements; i++) { 1097 emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen), coordinate); 1098 coordinate.reg_offset++; 1099 mlen += reg_width; 1100 } 1101 } 1102 1103 /* Generate the SEND */ 1104 fs_inst *inst = NULL; 1105 switch (ir->op) { 1106 case ir_tex: inst = emit(SHADER_OPCODE_TEX, dst); break; 1107 case ir_txb: inst = emit(FS_OPCODE_TXB, dst); break; 1108 case ir_txl: inst = emit(SHADER_OPCODE_TXL, dst); break; 1109 case ir_txd: inst = emit(SHADER_OPCODE_TXD, dst); break; 1110 case ir_txf: inst = emit(SHADER_OPCODE_TXF, dst); break; 1111 case ir_txs: inst = emit(SHADER_OPCODE_TXS, dst); break; 1112 } 1113 inst->base_mrf = base_mrf; 1114 inst->mlen = mlen; 1115 inst->header_present = header_present; 1116 1117 if (mlen > 11) { 1118 fail("Message length >11 disallowed by hardware\n"); 1119 } 1120 1121 return inst; 1122} 1123 1124void 1125fs_visitor::visit(ir_texture *ir) 1126{ 1127 fs_inst *inst = NULL; 1128 1129 int sampler = _mesa_get_sampler_uniform_value(ir->sampler, prog, &fp->Base); 1130 sampler = fp->Base.SamplerUnits[sampler]; 1131 1132 /* Our hardware doesn't have a sample_d_c message, so shadow compares 1133 * for textureGrad/TXD need to be emulated with instructions. 1134 */ 1135 bool hw_compare_supported = ir->op != ir_txd; 1136 if (ir->shadow_comparitor && !hw_compare_supported) { 1137 assert(c->key.tex.compare_funcs[sampler] != GL_NONE); 1138 /* No need to even sample for GL_ALWAYS or GL_NEVER...bail early */ 1139 if (c->key.tex.compare_funcs[sampler] == GL_ALWAYS) 1140 return swizzle_result(ir, fs_reg(1.0f), sampler); 1141 else if (c->key.tex.compare_funcs[sampler] == GL_NEVER) 1142 return swizzle_result(ir, fs_reg(0.0f), sampler); 1143 } 1144 1145 if (ir->coordinate) 1146 ir->coordinate->accept(this); 1147 fs_reg coordinate = this->result; 1148 1149 if (ir->offset != NULL && !(intel->gen == 7 && ir->op == ir_txf)) { 1150 uint32_t offset_bits = brw_texture_offset(ir->offset->as_constant()); 1151 1152 /* Explicitly set up the message header by copying g0 to msg reg m1. */ 1153 emit(BRW_OPCODE_MOV, fs_reg(MRF, 1, BRW_REGISTER_TYPE_UD), 1154 fs_reg(retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD))); 1155 1156 /* Then set the offset bits in DWord 2 of the message header. */ 1157 emit(BRW_OPCODE_MOV, 1158 fs_reg(retype(brw_vec1_reg(BRW_MESSAGE_REGISTER_FILE, 1, 2), 1159 BRW_REGISTER_TYPE_UD)), 1160 fs_reg(brw_imm_uw(offset_bits))); 1161 } 1162 1163 /* Should be lowered by do_lower_texture_projection */ 1164 assert(!ir->projector); 1165 1166 bool needs_gl_clamp = true; 1167 1168 fs_reg scale_x, scale_y; 1169 1170 /* The 965 requires the EU to do the normalization of GL rectangle 1171 * texture coordinates. We use the program parameter state 1172 * tracking to get the scaling factor. 1173 */ 1174 if (ir->sampler->type->sampler_dimensionality == GLSL_SAMPLER_DIM_RECT && 1175 (intel->gen < 6 || 1176 (intel->gen >= 6 && (c->key.tex.gl_clamp_mask[0] & (1 << sampler) || 1177 c->key.tex.gl_clamp_mask[1] & (1 << sampler))))) { 1178 struct gl_program_parameter_list *params = c->fp->program.Base.Parameters; 1179 int tokens[STATE_LENGTH] = { 1180 STATE_INTERNAL, 1181 STATE_TEXRECT_SCALE, 1182 sampler, 1183 0, 1184 0 1185 }; 1186 1187 if (c->dispatch_width == 16) { 1188 fail("rectangle scale uniform setup not supported on 16-wide\n"); 1189 this->result = fs_reg(this, ir->type); 1190 return; 1191 } 1192 1193 c->prog_data.param_convert[c->prog_data.nr_params] = 1194 PARAM_NO_CONVERT; 1195 c->prog_data.param_convert[c->prog_data.nr_params + 1] = 1196 PARAM_NO_CONVERT; 1197 1198 scale_x = fs_reg(UNIFORM, c->prog_data.nr_params); 1199 scale_y = fs_reg(UNIFORM, c->prog_data.nr_params + 1); 1200 1201 GLuint index = _mesa_add_state_reference(params, 1202 (gl_state_index *)tokens); 1203 1204 this->param_index[c->prog_data.nr_params] = index; 1205 this->param_offset[c->prog_data.nr_params] = 0; 1206 c->prog_data.nr_params++; 1207 this->param_index[c->prog_data.nr_params] = index; 1208 this->param_offset[c->prog_data.nr_params] = 1; 1209 c->prog_data.nr_params++; 1210 } 1211 1212 /* The 965 requires the EU to do the normalization of GL rectangle 1213 * texture coordinates. We use the program parameter state 1214 * tracking to get the scaling factor. 1215 */ 1216 if (intel->gen < 6 && 1217 ir->sampler->type->sampler_dimensionality == GLSL_SAMPLER_DIM_RECT) { 1218 fs_reg dst = fs_reg(this, ir->coordinate->type); 1219 fs_reg src = coordinate; 1220 coordinate = dst; 1221 1222 emit(BRW_OPCODE_MUL, dst, src, scale_x); 1223 dst.reg_offset++; 1224 src.reg_offset++; 1225 emit(BRW_OPCODE_MUL, dst, src, scale_y); 1226 } else if (ir->sampler->type->sampler_dimensionality == GLSL_SAMPLER_DIM_RECT) { 1227 /* On gen6+, the sampler handles the rectangle coordinates 1228 * natively, without needing rescaling. But that means we have 1229 * to do GL_CLAMP clamping at the [0, width], [0, height] scale, 1230 * not [0, 1] like the default case below. 1231 */ 1232 needs_gl_clamp = false; 1233 1234 for (int i = 0; i < 2; i++) { 1235 if (c->key.tex.gl_clamp_mask[i] & (1 << sampler)) { 1236 fs_reg chan = coordinate; 1237 chan.reg_offset += i; 1238 1239 inst = emit(BRW_OPCODE_SEL, chan, chan, brw_imm_f(0.0)); 1240 inst->conditional_mod = BRW_CONDITIONAL_G; 1241 1242 /* Our parameter comes in as 1.0/width or 1.0/height, 1243 * because that's what people normally want for doing 1244 * texture rectangle handling. We need width or height 1245 * for clamping, but we don't care enough to make a new 1246 * parameter type, so just invert back. 1247 */ 1248 fs_reg limit = fs_reg(this, glsl_type::float_type); 1249 emit(BRW_OPCODE_MOV, limit, i == 0 ? scale_x : scale_y); 1250 emit(SHADER_OPCODE_RCP, limit, limit); 1251 1252 inst = emit(BRW_OPCODE_SEL, chan, chan, limit); 1253 inst->conditional_mod = BRW_CONDITIONAL_L; 1254 } 1255 } 1256 } 1257 1258 if (ir->coordinate && needs_gl_clamp) { 1259 for (unsigned int i = 0; 1260 i < MIN2(ir->coordinate->type->vector_elements, 3); i++) { 1261 if (c->key.tex.gl_clamp_mask[i] & (1 << sampler)) { 1262 fs_reg chan = coordinate; 1263 chan.reg_offset += i; 1264 1265 fs_inst *inst = emit(BRW_OPCODE_MOV, chan, chan); 1266 inst->saturate = true; 1267 } 1268 } 1269 } 1270 1271 /* Writemasking doesn't eliminate channels on SIMD8 texture 1272 * samples, so don't worry about them. 1273 */ 1274 fs_reg dst = fs_reg(this, glsl_type::get_instance(ir->type->base_type, 4, 1)); 1275 1276 if (intel->gen >= 7) { 1277 inst = emit_texture_gen7(ir, dst, coordinate, sampler); 1278 } else if (intel->gen >= 5) { 1279 inst = emit_texture_gen5(ir, dst, coordinate, sampler); 1280 } else { 1281 inst = emit_texture_gen4(ir, dst, coordinate, sampler); 1282 } 1283 1284 /* If there's an offset, we already set up m1. To avoid the implied move, 1285 * use the null register. Otherwise, we want an implied move from g0. 1286 */ 1287 if (ir->offset != NULL || !inst->header_present) 1288 inst->src[0] = reg_undef; 1289 else 1290 inst->src[0] = fs_reg(retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UW)); 1291 1292 inst->sampler = sampler; 1293 1294 if (ir->shadow_comparitor) { 1295 if (hw_compare_supported) { 1296 inst->shadow_compare = true; 1297 } else { 1298 ir->shadow_comparitor->accept(this); 1299 fs_reg ref = this->result; 1300 1301 fs_reg value = dst; 1302 dst = fs_reg(this, glsl_type::vec4_type); 1303 1304 /* FINISHME: This needs to be done pre-filtering. */ 1305 1306 uint32_t conditional = 0; 1307 switch (c->key.tex.compare_funcs[sampler]) { 1308 /* GL_ALWAYS and GL_NEVER were handled at the top of the function */ 1309 case GL_LESS: conditional = BRW_CONDITIONAL_L; break; 1310 case GL_GREATER: conditional = BRW_CONDITIONAL_G; break; 1311 case GL_LEQUAL: conditional = BRW_CONDITIONAL_LE; break; 1312 case GL_GEQUAL: conditional = BRW_CONDITIONAL_GE; break; 1313 case GL_EQUAL: conditional = BRW_CONDITIONAL_EQ; break; 1314 case GL_NOTEQUAL: conditional = BRW_CONDITIONAL_NEQ; break; 1315 default: assert(!"Should not get here: bad shadow compare function"); 1316 } 1317 1318 /* Use conditional moves to load 0 or 1 as the result */ 1319 this->current_annotation = "manual shadow comparison"; 1320 for (int i = 0; i < 4; i++) { 1321 inst = emit(BRW_OPCODE_MOV, dst, fs_reg(0.0f)); 1322 1323 inst = emit(BRW_OPCODE_CMP, reg_null_f, ref, value); 1324 inst->conditional_mod = conditional; 1325 1326 inst = emit(BRW_OPCODE_MOV, dst, fs_reg(1.0f)); 1327 inst->predicated = true; 1328 1329 dst.reg_offset++; 1330 value.reg_offset++; 1331 } 1332 dst.reg_offset = 0; 1333 } 1334 } 1335 1336 swizzle_result(ir, dst, sampler); 1337} 1338 1339/** 1340 * Swizzle the result of a texture result. This is necessary for 1341 * EXT_texture_swizzle as well as DEPTH_TEXTURE_MODE for shadow comparisons. 1342 */ 1343void 1344fs_visitor::swizzle_result(ir_texture *ir, fs_reg orig_val, int sampler) 1345{ 1346 this->result = orig_val; 1347 1348 if (ir->op == ir_txs) 1349 return; 1350 1351 if (ir->type == glsl_type::float_type) { 1352 /* Ignore DEPTH_TEXTURE_MODE swizzling. */ 1353 assert(ir->sampler->type->sampler_shadow); 1354 } else if (c->key.tex.swizzles[sampler] != SWIZZLE_NOOP) { 1355 fs_reg swizzled_result = fs_reg(this, glsl_type::vec4_type); 1356 1357 for (int i = 0; i < 4; i++) { 1358 int swiz = GET_SWZ(c->key.tex.swizzles[sampler], i); 1359 fs_reg l = swizzled_result; 1360 l.reg_offset += i; 1361 1362 if (swiz == SWIZZLE_ZERO) { 1363 emit(BRW_OPCODE_MOV, l, fs_reg(0.0f)); 1364 } else if (swiz == SWIZZLE_ONE) { 1365 emit(BRW_OPCODE_MOV, l, fs_reg(1.0f)); 1366 } else { 1367 fs_reg r = orig_val; 1368 r.reg_offset += GET_SWZ(c->key.tex.swizzles[sampler], i); 1369 emit(BRW_OPCODE_MOV, l, r); 1370 } 1371 } 1372 this->result = swizzled_result; 1373 } 1374} 1375 1376void 1377fs_visitor::visit(ir_swizzle *ir) 1378{ 1379 ir->val->accept(this); 1380 fs_reg val = this->result; 1381 1382 if (ir->type->vector_elements == 1) { 1383 this->result.reg_offset += ir->mask.x; 1384 return; 1385 } 1386 1387 fs_reg result = fs_reg(this, ir->type); 1388 this->result = result; 1389 1390 for (unsigned int i = 0; i < ir->type->vector_elements; i++) { 1391 fs_reg channel = val; 1392 int swiz = 0; 1393 1394 switch (i) { 1395 case 0: 1396 swiz = ir->mask.x; 1397 break; 1398 case 1: 1399 swiz = ir->mask.y; 1400 break; 1401 case 2: 1402 swiz = ir->mask.z; 1403 break; 1404 case 3: 1405 swiz = ir->mask.w; 1406 break; 1407 } 1408 1409 channel.reg_offset += swiz; 1410 emit(BRW_OPCODE_MOV, result, channel); 1411 result.reg_offset++; 1412 } 1413} 1414 1415void 1416fs_visitor::visit(ir_discard *ir) 1417{ 1418 assert(ir->condition == NULL); /* FINISHME */ 1419 1420 emit(FS_OPCODE_DISCARD); 1421 kill_emitted = true; 1422} 1423 1424void 1425fs_visitor::visit(ir_constant *ir) 1426{ 1427 /* Set this->result to reg at the bottom of the function because some code 1428 * paths will cause this visitor to be applied to other fields. This will 1429 * cause the value stored in this->result to be modified. 1430 * 1431 * Make reg constant so that it doesn't get accidentally modified along the 1432 * way. Yes, I actually had this problem. :( 1433 */ 1434 const fs_reg reg(this, ir->type); 1435 fs_reg dst_reg = reg; 1436 1437 if (ir->type->is_array()) { 1438 const unsigned size = type_size(ir->type->fields.array); 1439 1440 for (unsigned i = 0; i < ir->type->length; i++) { 1441 ir->array_elements[i]->accept(this); 1442 fs_reg src_reg = this->result; 1443 1444 dst_reg.type = src_reg.type; 1445 for (unsigned j = 0; j < size; j++) { 1446 emit(BRW_OPCODE_MOV, dst_reg, src_reg); 1447 src_reg.reg_offset++; 1448 dst_reg.reg_offset++; 1449 } 1450 } 1451 } else if (ir->type->is_record()) { 1452 foreach_list(node, &ir->components) { 1453 ir_constant *const field = (ir_constant *) node; 1454 const unsigned size = type_size(field->type); 1455 1456 field->accept(this); 1457 fs_reg src_reg = this->result; 1458 1459 dst_reg.type = src_reg.type; 1460 for (unsigned j = 0; j < size; j++) { 1461 emit(BRW_OPCODE_MOV, dst_reg, src_reg); 1462 src_reg.reg_offset++; 1463 dst_reg.reg_offset++; 1464 } 1465 } 1466 } else { 1467 const unsigned size = type_size(ir->type); 1468 1469 for (unsigned i = 0; i < size; i++) { 1470 switch (ir->type->base_type) { 1471 case GLSL_TYPE_FLOAT: 1472 emit(BRW_OPCODE_MOV, dst_reg, fs_reg(ir->value.f[i])); 1473 break; 1474 case GLSL_TYPE_UINT: 1475 emit(BRW_OPCODE_MOV, dst_reg, fs_reg(ir->value.u[i])); 1476 break; 1477 case GLSL_TYPE_INT: 1478 emit(BRW_OPCODE_MOV, dst_reg, fs_reg(ir->value.i[i])); 1479 break; 1480 case GLSL_TYPE_BOOL: 1481 emit(BRW_OPCODE_MOV, dst_reg, fs_reg((int)ir->value.b[i])); 1482 break; 1483 default: 1484 assert(!"Non-float/uint/int/bool constant"); 1485 } 1486 dst_reg.reg_offset++; 1487 } 1488 } 1489 1490 this->result = reg; 1491} 1492 1493void 1494fs_visitor::emit_bool_to_cond_code(ir_rvalue *ir) 1495{ 1496 ir_expression *expr = ir->as_expression(); 1497 1498 if (expr) { 1499 fs_reg op[2]; 1500 fs_inst *inst; 1501 1502 assert(expr->get_num_operands() <= 2); 1503 for (unsigned int i = 0; i < expr->get_num_operands(); i++) { 1504 assert(expr->operands[i]->type->is_scalar()); 1505 1506 expr->operands[i]->accept(this); 1507 op[i] = this->result; 1508 1509 resolve_ud_negate(&op[i]); 1510 } 1511 1512 switch (expr->operation) { 1513 case ir_unop_logic_not: 1514 inst = emit(BRW_OPCODE_AND, reg_null_d, op[0], fs_reg(1)); 1515 inst->conditional_mod = BRW_CONDITIONAL_Z; 1516 break; 1517 1518 case ir_binop_logic_xor: 1519 case ir_binop_logic_or: 1520 case ir_binop_logic_and: 1521 goto out; 1522 1523 case ir_unop_f2b: 1524 if (intel->gen >= 6) { 1525 inst = emit(BRW_OPCODE_CMP, reg_null_d, op[0], fs_reg(0.0f)); 1526 } else { 1527 inst = emit(BRW_OPCODE_MOV, reg_null_f, op[0]); 1528 } 1529 inst->conditional_mod = BRW_CONDITIONAL_NZ; 1530 break; 1531 1532 case ir_unop_i2b: 1533 if (intel->gen >= 6) { 1534 inst = emit(BRW_OPCODE_CMP, reg_null_d, op[0], fs_reg(0)); 1535 } else { 1536 inst = emit(BRW_OPCODE_MOV, reg_null_d, op[0]); 1537 } 1538 inst->conditional_mod = BRW_CONDITIONAL_NZ; 1539 break; 1540 1541 case ir_binop_greater: 1542 case ir_binop_gequal: 1543 case ir_binop_less: 1544 case ir_binop_lequal: 1545 case ir_binop_equal: 1546 case ir_binop_all_equal: 1547 case ir_binop_nequal: 1548 case ir_binop_any_nequal: 1549 resolve_bool_comparison(expr->operands[0], &op[0]); 1550 resolve_bool_comparison(expr->operands[1], &op[1]); 1551 1552 inst = emit(BRW_OPCODE_CMP, reg_null_cmp, op[0], op[1]); 1553 inst->conditional_mod = 1554 brw_conditional_for_comparison(expr->operation); 1555 break; 1556 1557 default: 1558 assert(!"not reached"); 1559 fail("bad cond code\n"); 1560 break; 1561 } 1562 return; 1563 } 1564 1565out: 1566 ir->accept(this); 1567 1568 fs_inst *inst = emit(BRW_OPCODE_AND, reg_null_d, this->result, fs_reg(1)); 1569 inst->conditional_mod = BRW_CONDITIONAL_NZ; 1570} 1571 1572/** 1573 * Emit a gen6 IF statement with the comparison folded into the IF 1574 * instruction. 1575 */ 1576void 1577fs_visitor::emit_if_gen6(ir_if *ir) 1578{ 1579 ir_expression *expr = ir->condition->as_expression(); 1580 1581 if (expr) { 1582 fs_reg op[2]; 1583 fs_inst *inst; 1584 fs_reg temp; 1585 1586 assert(expr->get_num_operands() <= 2); 1587 for (unsigned int i = 0; i < expr->get_num_operands(); i++) { 1588 assert(expr->operands[i]->type->is_scalar()); 1589 1590 expr->operands[i]->accept(this); 1591 op[i] = this->result; 1592 } 1593 1594 switch (expr->operation) { 1595 case ir_unop_logic_not: 1596 inst = emit(BRW_OPCODE_IF, temp, op[0], fs_reg(0)); 1597 inst->conditional_mod = BRW_CONDITIONAL_Z; 1598 return; 1599 1600 case ir_binop_logic_xor: 1601 inst = emit(BRW_OPCODE_IF, reg_null_d, op[0], op[1]); 1602 inst->conditional_mod = BRW_CONDITIONAL_NZ; 1603 return; 1604 1605 case ir_binop_logic_or: 1606 temp = fs_reg(this, glsl_type::bool_type); 1607 emit(BRW_OPCODE_OR, temp, op[0], op[1]); 1608 inst = emit(BRW_OPCODE_IF, reg_null_d, temp, fs_reg(0)); 1609 inst->conditional_mod = BRW_CONDITIONAL_NZ; 1610 return; 1611 1612 case ir_binop_logic_and: 1613 temp = fs_reg(this, glsl_type::bool_type); 1614 emit(BRW_OPCODE_AND, temp, op[0], op[1]); 1615 inst = emit(BRW_OPCODE_IF, reg_null_d, temp, fs_reg(0)); 1616 inst->conditional_mod = BRW_CONDITIONAL_NZ; 1617 return; 1618 1619 case ir_unop_f2b: 1620 inst = emit(BRW_OPCODE_IF, reg_null_f, op[0], fs_reg(0)); 1621 inst->conditional_mod = BRW_CONDITIONAL_NZ; 1622 return; 1623 1624 case ir_unop_i2b: 1625 inst = emit(BRW_OPCODE_IF, reg_null_d, op[0], fs_reg(0)); 1626 inst->conditional_mod = BRW_CONDITIONAL_NZ; 1627 return; 1628 1629 case ir_binop_greater: 1630 case ir_binop_gequal: 1631 case ir_binop_less: 1632 case ir_binop_lequal: 1633 case ir_binop_equal: 1634 case ir_binop_all_equal: 1635 case ir_binop_nequal: 1636 case ir_binop_any_nequal: 1637 inst = emit(BRW_OPCODE_IF, reg_null_d, op[0], op[1]); 1638 inst->conditional_mod = 1639 brw_conditional_for_comparison(expr->operation); 1640 return; 1641 default: 1642 assert(!"not reached"); 1643 inst = emit(BRW_OPCODE_IF, reg_null_d, op[0], fs_reg(0)); 1644 inst->conditional_mod = BRW_CONDITIONAL_NZ; 1645 fail("bad condition\n"); 1646 return; 1647 } 1648 return; 1649 } 1650 1651 ir->condition->accept(this); 1652 1653 fs_inst *inst = emit(BRW_OPCODE_IF, reg_null_d, this->result, fs_reg(0)); 1654 inst->conditional_mod = BRW_CONDITIONAL_NZ; 1655} 1656 1657void 1658fs_visitor::visit(ir_if *ir) 1659{ 1660 fs_inst *inst; 1661 1662 if (intel->gen < 6 && c->dispatch_width == 16) { 1663 fail("Can't support (non-uniform) control flow on 16-wide\n"); 1664 } 1665 1666 /* Don't point the annotation at the if statement, because then it plus 1667 * the then and else blocks get printed. 1668 */ 1669 this->base_ir = ir->condition; 1670 1671 if (intel->gen == 6) { 1672 emit_if_gen6(ir); 1673 } else { 1674 emit_bool_to_cond_code(ir->condition); 1675 1676 inst = emit(BRW_OPCODE_IF); 1677 inst->predicated = true; 1678 } 1679 1680 foreach_list(node, &ir->then_instructions) { 1681 ir_instruction *ir = (ir_instruction *)node; 1682 this->base_ir = ir; 1683 1684 ir->accept(this); 1685 } 1686 1687 if (!ir->else_instructions.is_empty()) { 1688 emit(BRW_OPCODE_ELSE); 1689 1690 foreach_list(node, &ir->else_instructions) { 1691 ir_instruction *ir = (ir_instruction *)node; 1692 this->base_ir = ir; 1693 1694 ir->accept(this); 1695 } 1696 } 1697 1698 emit(BRW_OPCODE_ENDIF); 1699} 1700 1701void 1702fs_visitor::visit(ir_loop *ir) 1703{ 1704 fs_reg counter = reg_undef; 1705 1706 if (intel->gen < 6 && c->dispatch_width == 16) { 1707 fail("Can't support (non-uniform) control flow on 16-wide\n"); 1708 } 1709 1710 if (ir->counter) { 1711 this->base_ir = ir->counter; 1712 ir->counter->accept(this); 1713 counter = *(variable_storage(ir->counter)); 1714 1715 if (ir->from) { 1716 this->base_ir = ir->from; 1717 ir->from->accept(this); 1718 1719 emit(BRW_OPCODE_MOV, counter, this->result); 1720 } 1721 } 1722 1723 this->base_ir = NULL; 1724 emit(BRW_OPCODE_DO); 1725 1726 if (ir->to) { 1727 this->base_ir = ir->to; 1728 ir->to->accept(this); 1729 1730 fs_inst *inst = emit(BRW_OPCODE_CMP, reg_null_cmp, counter, this->result); 1731 inst->conditional_mod = brw_conditional_for_comparison(ir->cmp); 1732 1733 inst = emit(BRW_OPCODE_BREAK); 1734 inst->predicated = true; 1735 } 1736 1737 foreach_list(node, &ir->body_instructions) { 1738 ir_instruction *ir = (ir_instruction *)node; 1739 1740 this->base_ir = ir; 1741 ir->accept(this); 1742 } 1743 1744 if (ir->increment) { 1745 this->base_ir = ir->increment; 1746 ir->increment->accept(this); 1747 emit(BRW_OPCODE_ADD, counter, counter, this->result); 1748 } 1749 1750 this->base_ir = NULL; 1751 emit(BRW_OPCODE_WHILE); 1752} 1753 1754void 1755fs_visitor::visit(ir_loop_jump *ir) 1756{ 1757 switch (ir->mode) { 1758 case ir_loop_jump::jump_break: 1759 emit(BRW_OPCODE_BREAK); 1760 break; 1761 case ir_loop_jump::jump_continue: 1762 emit(BRW_OPCODE_CONTINUE); 1763 break; 1764 } 1765} 1766 1767void 1768fs_visitor::visit(ir_call *ir) 1769{ 1770 assert(!"FINISHME"); 1771} 1772 1773void 1774fs_visitor::visit(ir_return *ir) 1775{ 1776 assert(!"FINISHME"); 1777} 1778 1779void 1780fs_visitor::visit(ir_function *ir) 1781{ 1782 /* Ignore function bodies other than main() -- we shouldn't see calls to 1783 * them since they should all be inlined before we get to ir_to_mesa. 1784 */ 1785 if (strcmp(ir->name, "main") == 0) { 1786 const ir_function_signature *sig; 1787 exec_list empty; 1788 1789 sig = ir->matching_signature(&empty); 1790 1791 assert(sig); 1792 1793 foreach_list(node, &sig->body) { 1794 ir_instruction *ir = (ir_instruction *)node; 1795 this->base_ir = ir; 1796 1797 ir->accept(this); 1798 } 1799 } 1800} 1801 1802void 1803fs_visitor::visit(ir_function_signature *ir) 1804{ 1805 assert(!"not reached"); 1806 (void)ir; 1807} 1808 1809fs_inst * 1810fs_visitor::emit(fs_inst inst) 1811{ 1812 fs_inst *list_inst = new(mem_ctx) fs_inst; 1813 *list_inst = inst; 1814 1815 if (force_uncompressed_stack > 0) 1816 list_inst->force_uncompressed = true; 1817 else if (force_sechalf_stack > 0) 1818 list_inst->force_sechalf = true; 1819 1820 list_inst->annotation = this->current_annotation; 1821 list_inst->ir = this->base_ir; 1822 1823 this->instructions.push_tail(list_inst); 1824 1825 return list_inst; 1826} 1827 1828/** Emits a dummy fragment shader consisting of magenta for bringup purposes. */ 1829void 1830fs_visitor::emit_dummy_fs() 1831{ 1832 int reg_width = c->dispatch_width / 8; 1833 1834 /* Everyone's favorite color. */ 1835 emit(BRW_OPCODE_MOV, fs_reg(MRF, 2 + 0 * reg_width), fs_reg(1.0f)); 1836 emit(BRW_OPCODE_MOV, fs_reg(MRF, 2 + 1 * reg_width), fs_reg(0.0f)); 1837 emit(BRW_OPCODE_MOV, fs_reg(MRF, 2 + 2 * reg_width), fs_reg(1.0f)); 1838 emit(BRW_OPCODE_MOV, fs_reg(MRF, 2 + 3 * reg_width), fs_reg(0.0f)); 1839 1840 fs_inst *write; 1841 write = emit(FS_OPCODE_FB_WRITE, fs_reg(0), fs_reg(0)); 1842 write->base_mrf = 2; 1843 write->mlen = 4 * reg_width; 1844 write->eot = true; 1845} 1846 1847/* The register location here is relative to the start of the URB 1848 * data. It will get adjusted to be a real location before 1849 * generate_code() time. 1850 */ 1851struct brw_reg 1852fs_visitor::interp_reg(int location, int channel) 1853{ 1854 int regnr = urb_setup[location] * 2 + channel / 2; 1855 int stride = (channel & 1) * 4; 1856 1857 assert(urb_setup[location] != -1); 1858 1859 return brw_vec1_grf(regnr, stride); 1860} 1861 1862/** Emits the interpolation for the varying inputs. */ 1863void 1864fs_visitor::emit_interpolation_setup_gen4() 1865{ 1866 this->current_annotation = "compute pixel centers"; 1867 this->pixel_x = fs_reg(this, glsl_type::uint_type); 1868 this->pixel_y = fs_reg(this, glsl_type::uint_type); 1869 this->pixel_x.type = BRW_REGISTER_TYPE_UW; 1870 this->pixel_y.type = BRW_REGISTER_TYPE_UW; 1871 1872 emit(FS_OPCODE_PIXEL_X, this->pixel_x); 1873 emit(FS_OPCODE_PIXEL_Y, this->pixel_y); 1874 1875 this->current_annotation = "compute pixel deltas from v0"; 1876 if (brw->has_pln) { 1877 this->delta_x[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC] = 1878 fs_reg(this, glsl_type::vec2_type); 1879 this->delta_y[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC] = 1880 this->delta_x[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC]; 1881 this->delta_y[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC].reg_offset++; 1882 } else { 1883 this->delta_x[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC] = 1884 fs_reg(this, glsl_type::float_type); 1885 this->delta_y[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC] = 1886 fs_reg(this, glsl_type::float_type); 1887 } 1888 emit(BRW_OPCODE_ADD, this->delta_x[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC], 1889 this->pixel_x, fs_reg(negate(brw_vec1_grf(1, 0)))); 1890 emit(BRW_OPCODE_ADD, this->delta_y[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC], 1891 this->pixel_y, fs_reg(negate(brw_vec1_grf(1, 1)))); 1892 1893 this->current_annotation = "compute pos.w and 1/pos.w"; 1894 /* Compute wpos.w. It's always in our setup, since it's needed to 1895 * interpolate the other attributes. 1896 */ 1897 this->wpos_w = fs_reg(this, glsl_type::float_type); 1898 emit(FS_OPCODE_LINTERP, wpos_w, 1899 this->delta_x[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC], 1900 this->delta_y[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC], 1901 interp_reg(FRAG_ATTRIB_WPOS, 3)); 1902 /* Compute the pixel 1/W value from wpos.w. */ 1903 this->pixel_w = fs_reg(this, glsl_type::float_type); 1904 emit_math(SHADER_OPCODE_RCP, this->pixel_w, wpos_w); 1905 this->current_annotation = NULL; 1906} 1907 1908/** Emits the interpolation for the varying inputs. */ 1909void 1910fs_visitor::emit_interpolation_setup_gen6() 1911{ 1912 struct brw_reg g1_uw = retype(brw_vec1_grf(1, 0), BRW_REGISTER_TYPE_UW); 1913 1914 /* If the pixel centers end up used, the setup is the same as for gen4. */ 1915 this->current_annotation = "compute pixel centers"; 1916 fs_reg int_pixel_x = fs_reg(this, glsl_type::uint_type); 1917 fs_reg int_pixel_y = fs_reg(this, glsl_type::uint_type); 1918 int_pixel_x.type = BRW_REGISTER_TYPE_UW; 1919 int_pixel_y.type = BRW_REGISTER_TYPE_UW; 1920 emit(BRW_OPCODE_ADD, 1921 int_pixel_x, 1922 fs_reg(stride(suboffset(g1_uw, 4), 2, 4, 0)), 1923 fs_reg(brw_imm_v(0x10101010))); 1924 emit(BRW_OPCODE_ADD, 1925 int_pixel_y, 1926 fs_reg(stride(suboffset(g1_uw, 5), 2, 4, 0)), 1927 fs_reg(brw_imm_v(0x11001100))); 1928 1929 /* As of gen6, we can no longer mix float and int sources. We have 1930 * to turn the integer pixel centers into floats for their actual 1931 * use. 1932 */ 1933 this->pixel_x = fs_reg(this, glsl_type::float_type); 1934 this->pixel_y = fs_reg(this, glsl_type::float_type); 1935 emit(BRW_OPCODE_MOV, this->pixel_x, int_pixel_x); 1936 emit(BRW_OPCODE_MOV, this->pixel_y, int_pixel_y); 1937 1938 this->current_annotation = "compute pos.w"; 1939 this->pixel_w = fs_reg(brw_vec8_grf(c->source_w_reg, 0)); 1940 this->wpos_w = fs_reg(this, glsl_type::float_type); 1941 emit_math(SHADER_OPCODE_RCP, this->wpos_w, this->pixel_w); 1942 1943 for (int i = 0; i < BRW_WM_BARYCENTRIC_INTERP_MODE_COUNT; ++i) { 1944 uint8_t reg = c->barycentric_coord_reg[i]; 1945 this->delta_x[i] = fs_reg(brw_vec8_grf(reg, 0)); 1946 this->delta_y[i] = fs_reg(brw_vec8_grf(reg + 1, 0)); 1947 } 1948 1949 this->current_annotation = NULL; 1950} 1951 1952void 1953fs_visitor::emit_color_write(int target, int index, int first_color_mrf) 1954{ 1955 int reg_width = c->dispatch_width / 8; 1956 fs_inst *inst; 1957 fs_reg color = outputs[target]; 1958 fs_reg mrf; 1959 1960 /* If there's no color data to be written, skip it. */ 1961 if (color.file == BAD_FILE) 1962 return; 1963 1964 color.reg_offset += index; 1965 1966 if (c->dispatch_width == 8 || intel->gen >= 6) { 1967 /* SIMD8 write looks like: 1968 * m + 0: r0 1969 * m + 1: r1 1970 * m + 2: g0 1971 * m + 3: g1 1972 * 1973 * gen6 SIMD16 DP write looks like: 1974 * m + 0: r0 1975 * m + 1: r1 1976 * m + 2: g0 1977 * m + 3: g1 1978 * m + 4: b0 1979 * m + 5: b1 1980 * m + 6: a0 1981 * m + 7: a1 1982 */ 1983 inst = emit(BRW_OPCODE_MOV, 1984 fs_reg(MRF, first_color_mrf + index * reg_width, color.type), 1985 color); 1986 inst->saturate = c->key.clamp_fragment_color; 1987 } else { 1988 /* pre-gen6 SIMD16 single source DP write looks like: 1989 * m + 0: r0 1990 * m + 1: g0 1991 * m + 2: b0 1992 * m + 3: a0 1993 * m + 4: r1 1994 * m + 5: g1 1995 * m + 6: b1 1996 * m + 7: a1 1997 */ 1998 if (brw->has_compr4) { 1999 /* By setting the high bit of the MRF register number, we 2000 * indicate that we want COMPR4 mode - instead of doing the 2001 * usual destination + 1 for the second half we get 2002 * destination + 4. 2003 */ 2004 inst = emit(BRW_OPCODE_MOV, 2005 fs_reg(MRF, BRW_MRF_COMPR4 + first_color_mrf + index, 2006 color.type), 2007 color); 2008 inst->saturate = c->key.clamp_fragment_color; 2009 } else { 2010 push_force_uncompressed(); 2011 inst = emit(BRW_OPCODE_MOV, fs_reg(MRF, first_color_mrf + index, 2012 color.type), 2013 color); 2014 inst->saturate = c->key.clamp_fragment_color; 2015 pop_force_uncompressed(); 2016 2017 push_force_sechalf(); 2018 color.sechalf = true; 2019 inst = emit(BRW_OPCODE_MOV, fs_reg(MRF, first_color_mrf + index + 4, 2020 color.type), 2021 color); 2022 inst->saturate = c->key.clamp_fragment_color; 2023 pop_force_sechalf(); 2024 color.sechalf = false; 2025 } 2026 } 2027} 2028 2029void 2030fs_visitor::emit_fb_writes() 2031{ 2032 this->current_annotation = "FB write header"; 2033 bool header_present = true; 2034 /* We can potentially have a message length of up to 15, so we have to set 2035 * base_mrf to either 0 or 1 in order to fit in m0..m15. 2036 */ 2037 int base_mrf = 1; 2038 int nr = base_mrf; 2039 int reg_width = c->dispatch_width / 8; 2040 2041 if (intel->gen >= 6 && 2042 !this->kill_emitted && 2043 c->key.nr_color_regions == 1) { 2044 header_present = false; 2045 } 2046 2047 if (header_present) { 2048 /* m2, m3 header */ 2049 nr += 2; 2050 } 2051 2052 if (c->aa_dest_stencil_reg) { 2053 push_force_uncompressed(); 2054 emit(BRW_OPCODE_MOV, fs_reg(MRF, nr++), 2055 fs_reg(brw_vec8_grf(c->aa_dest_stencil_reg, 0))); 2056 pop_force_uncompressed(); 2057 } 2058 2059 /* Reserve space for color. It'll be filled in per MRT below. */ 2060 int color_mrf = nr; 2061 nr += 4 * reg_width; 2062 2063 if (c->source_depth_to_render_target) { 2064 if (intel->gen == 6 && c->dispatch_width == 16) { 2065 /* For outputting oDepth on gen6, SIMD8 writes have to be 2066 * used. This would require 8-wide moves of each half to 2067 * message regs, kind of like pre-gen5 SIMD16 FB writes. 2068 * Just bail on doing so for now. 2069 */ 2070 fail("Missing support for simd16 depth writes on gen6\n"); 2071 } 2072 2073 if (c->computes_depth) { 2074 /* Hand over gl_FragDepth. */ 2075 assert(this->frag_depth); 2076 fs_reg depth = *(variable_storage(this->frag_depth)); 2077 2078 emit(BRW_OPCODE_MOV, fs_reg(MRF, nr), depth); 2079 } else { 2080 /* Pass through the payload depth. */ 2081 emit(BRW_OPCODE_MOV, fs_reg(MRF, nr), 2082 fs_reg(brw_vec8_grf(c->source_depth_reg, 0))); 2083 } 2084 nr += reg_width; 2085 } 2086 2087 if (c->dest_depth_reg) { 2088 emit(BRW_OPCODE_MOV, fs_reg(MRF, nr), 2089 fs_reg(brw_vec8_grf(c->dest_depth_reg, 0))); 2090 nr += reg_width; 2091 } 2092 2093 for (int target = 0; target < c->key.nr_color_regions; target++) { 2094 this->current_annotation = ralloc_asprintf(this->mem_ctx, 2095 "FB write target %d", 2096 target); 2097 for (int i = 0; i < 4; i++) 2098 emit_color_write(target, i, color_mrf); 2099 2100 fs_inst *inst = emit(FS_OPCODE_FB_WRITE); 2101 inst->target = target; 2102 inst->base_mrf = base_mrf; 2103 inst->mlen = nr - base_mrf; 2104 if (target == c->key.nr_color_regions - 1) 2105 inst->eot = true; 2106 inst->header_present = header_present; 2107 } 2108 2109 if (c->key.nr_color_regions == 0) { 2110 if (c->key.alpha_test) { 2111 /* If the alpha test is enabled but there's no color buffer, 2112 * we still need to send alpha out the pipeline to our null 2113 * renderbuffer. 2114 */ 2115 emit_color_write(0, 3, color_mrf); 2116 } 2117 2118 fs_inst *inst = emit(FS_OPCODE_FB_WRITE); 2119 inst->base_mrf = base_mrf; 2120 inst->mlen = nr - base_mrf; 2121 inst->eot = true; 2122 inst->header_present = header_present; 2123 } 2124 2125 this->current_annotation = NULL; 2126} 2127 2128void 2129fs_visitor::resolve_ud_negate(fs_reg *reg) 2130{ 2131 if (reg->type != BRW_REGISTER_TYPE_UD || 2132 !reg->negate) 2133 return; 2134 2135 fs_reg temp = fs_reg(this, glsl_type::uint_type); 2136 emit(BRW_OPCODE_MOV, temp, *reg); 2137 *reg = temp; 2138} 2139 2140void 2141fs_visitor::resolve_bool_comparison(ir_rvalue *rvalue, fs_reg *reg) 2142{ 2143 if (rvalue->type != glsl_type::bool_type) 2144 return; 2145 2146 fs_reg temp = fs_reg(this, glsl_type::bool_type); 2147 emit(BRW_OPCODE_AND, temp, *reg, fs_reg(1)); 2148 *reg = temp; 2149} 2150