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