brw_fs_visitor.cpp revision 6c947cfd1973c3791d54f1406c973357b4a9621a
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) { 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) { 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) { 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 int sampler; 936 fs_inst *inst = NULL; 937 938 this->result = reg_undef; 939 ir->coordinate->accept(this); 940 fs_reg coordinate = this->result; 941 942 if (ir->offset != NULL) { 943 ir_constant *offset = ir->offset->as_constant(); 944 assert(offset != NULL); 945 946 signed char offsets[3]; 947 for (unsigned i = 0; i < ir->offset->type->vector_elements; i++) 948 offsets[i] = (signed char) offset->value.i[i]; 949 950 /* Combine all three offsets into a single unsigned dword: 951 * 952 * bits 11:8 - U Offset (X component) 953 * bits 7:4 - V Offset (Y component) 954 * bits 3:0 - R Offset (Z component) 955 */ 956 unsigned offset_bits = 0; 957 for (unsigned i = 0; i < ir->offset->type->vector_elements; i++) { 958 const unsigned shift = 4 * (2 - i); 959 offset_bits |= (offsets[i] << shift) & (0xF << shift); 960 } 961 962 /* Explicitly set up the message header by copying g0 to msg reg m1. */ 963 emit(BRW_OPCODE_MOV, fs_reg(MRF, 1, BRW_REGISTER_TYPE_UD), 964 fs_reg(GRF, 0, BRW_REGISTER_TYPE_UD)); 965 966 /* Then set the offset bits in DWord 2 of the message header. */ 967 emit(BRW_OPCODE_MOV, 968 fs_reg(retype(brw_vec1_reg(BRW_MESSAGE_REGISTER_FILE, 1, 2), 969 BRW_REGISTER_TYPE_UD)), 970 fs_reg(brw_imm_uw(offset_bits))); 971 } 972 973 /* Should be lowered by do_lower_texture_projection */ 974 assert(!ir->projector); 975 976 sampler = _mesa_get_sampler_uniform_value(ir->sampler, 977 prog, 978 &fp->Base); 979 sampler = fp->Base.SamplerUnits[sampler]; 980 981 /* The 965 requires the EU to do the normalization of GL rectangle 982 * texture coordinates. We use the program parameter state 983 * tracking to get the scaling factor. 984 */ 985 if (ir->sampler->type->sampler_dimensionality == GLSL_SAMPLER_DIM_RECT) { 986 struct gl_program_parameter_list *params = c->fp->program.Base.Parameters; 987 int tokens[STATE_LENGTH] = { 988 STATE_INTERNAL, 989 STATE_TEXRECT_SCALE, 990 sampler, 991 0, 992 0 993 }; 994 995 if (c->dispatch_width == 16) { 996 fail("rectangle scale uniform setup not supported on 16-wide\n"); 997 this->result = fs_reg(this, ir->type); 998 return; 999 } 1000 1001 c->prog_data.param_convert[c->prog_data.nr_params] = 1002 PARAM_NO_CONVERT; 1003 c->prog_data.param_convert[c->prog_data.nr_params + 1] = 1004 PARAM_NO_CONVERT; 1005 1006 fs_reg scale_x = fs_reg(UNIFORM, c->prog_data.nr_params); 1007 fs_reg scale_y = fs_reg(UNIFORM, c->prog_data.nr_params + 1); 1008 GLuint index = _mesa_add_state_reference(params, 1009 (gl_state_index *)tokens); 1010 1011 this->param_index[c->prog_data.nr_params] = index; 1012 this->param_offset[c->prog_data.nr_params] = 0; 1013 c->prog_data.nr_params++; 1014 this->param_index[c->prog_data.nr_params] = index; 1015 this->param_offset[c->prog_data.nr_params] = 1; 1016 c->prog_data.nr_params++; 1017 1018 fs_reg dst = fs_reg(this, ir->coordinate->type); 1019 fs_reg src = coordinate; 1020 coordinate = dst; 1021 1022 emit(BRW_OPCODE_MUL, dst, src, scale_x); 1023 dst.reg_offset++; 1024 src.reg_offset++; 1025 emit(BRW_OPCODE_MUL, dst, src, scale_y); 1026 } 1027 1028 /* Writemasking doesn't eliminate channels on SIMD8 texture 1029 * samples, so don't worry about them. 1030 */ 1031 fs_reg dst = fs_reg(this, glsl_type::vec4_type); 1032 1033 if (intel->gen >= 7) { 1034 inst = emit_texture_gen7(ir, dst, coordinate, sampler); 1035 } else if (intel->gen >= 5) { 1036 inst = emit_texture_gen5(ir, dst, coordinate, sampler); 1037 } else { 1038 inst = emit_texture_gen4(ir, dst, coordinate, sampler); 1039 } 1040 1041 /* If there's an offset, we already set up m1. To avoid the implied move, 1042 * use the null register. Otherwise, we want an implied move from g0. 1043 */ 1044 if (ir->offset != NULL || !inst->header_present) 1045 inst->src[0] = reg_undef; 1046 else 1047 inst->src[0] = fs_reg(retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UW)); 1048 1049 inst->sampler = sampler; 1050 1051 this->result = dst; 1052 1053 if (ir->shadow_comparitor) 1054 inst->shadow_compare = true; 1055 1056 if (ir->type == glsl_type::float_type) { 1057 /* Ignore DEPTH_TEXTURE_MODE swizzling. */ 1058 assert(ir->sampler->type->sampler_shadow); 1059 } else if (c->key.tex_swizzles[inst->sampler] != SWIZZLE_NOOP) { 1060 fs_reg swizzle_dst = fs_reg(this, glsl_type::vec4_type); 1061 1062 for (int i = 0; i < 4; i++) { 1063 int swiz = GET_SWZ(c->key.tex_swizzles[inst->sampler], i); 1064 fs_reg l = swizzle_dst; 1065 l.reg_offset += i; 1066 1067 if (swiz == SWIZZLE_ZERO) { 1068 emit(BRW_OPCODE_MOV, l, fs_reg(0.0f)); 1069 } else if (swiz == SWIZZLE_ONE) { 1070 emit(BRW_OPCODE_MOV, l, fs_reg(1.0f)); 1071 } else { 1072 fs_reg r = dst; 1073 r.reg_offset += GET_SWZ(c->key.tex_swizzles[inst->sampler], i); 1074 emit(BRW_OPCODE_MOV, l, r); 1075 } 1076 } 1077 this->result = swizzle_dst; 1078 } 1079} 1080 1081void 1082fs_visitor::visit(ir_swizzle *ir) 1083{ 1084 this->result = reg_undef; 1085 ir->val->accept(this); 1086 fs_reg val = this->result; 1087 1088 if (ir->type->vector_elements == 1) { 1089 this->result.reg_offset += ir->mask.x; 1090 return; 1091 } 1092 1093 fs_reg result = fs_reg(this, ir->type); 1094 this->result = result; 1095 1096 for (unsigned int i = 0; i < ir->type->vector_elements; i++) { 1097 fs_reg channel = val; 1098 int swiz = 0; 1099 1100 switch (i) { 1101 case 0: 1102 swiz = ir->mask.x; 1103 break; 1104 case 1: 1105 swiz = ir->mask.y; 1106 break; 1107 case 2: 1108 swiz = ir->mask.z; 1109 break; 1110 case 3: 1111 swiz = ir->mask.w; 1112 break; 1113 } 1114 1115 channel.reg_offset += swiz; 1116 emit(BRW_OPCODE_MOV, result, channel); 1117 result.reg_offset++; 1118 } 1119} 1120 1121void 1122fs_visitor::visit(ir_discard *ir) 1123{ 1124 assert(ir->condition == NULL); /* FINISHME */ 1125 1126 emit(FS_OPCODE_DISCARD); 1127 kill_emitted = true; 1128} 1129 1130void 1131fs_visitor::visit(ir_constant *ir) 1132{ 1133 /* Set this->result to reg at the bottom of the function because some code 1134 * paths will cause this visitor to be applied to other fields. This will 1135 * cause the value stored in this->result to be modified. 1136 * 1137 * Make reg constant so that it doesn't get accidentally modified along the 1138 * way. Yes, I actually had this problem. :( 1139 */ 1140 const fs_reg reg(this, ir->type); 1141 fs_reg dst_reg = reg; 1142 1143 if (ir->type->is_array()) { 1144 const unsigned size = type_size(ir->type->fields.array); 1145 1146 for (unsigned i = 0; i < ir->type->length; i++) { 1147 this->result = reg_undef; 1148 ir->array_elements[i]->accept(this); 1149 fs_reg src_reg = this->result; 1150 1151 dst_reg.type = src_reg.type; 1152 for (unsigned j = 0; j < size; j++) { 1153 emit(BRW_OPCODE_MOV, dst_reg, src_reg); 1154 src_reg.reg_offset++; 1155 dst_reg.reg_offset++; 1156 } 1157 } 1158 } else if (ir->type->is_record()) { 1159 foreach_list(node, &ir->components) { 1160 ir_instruction *const field = (ir_instruction *) node; 1161 const unsigned size = type_size(field->type); 1162 1163 this->result = reg_undef; 1164 field->accept(this); 1165 fs_reg src_reg = this->result; 1166 1167 dst_reg.type = src_reg.type; 1168 for (unsigned j = 0; j < size; j++) { 1169 emit(BRW_OPCODE_MOV, dst_reg, src_reg); 1170 src_reg.reg_offset++; 1171 dst_reg.reg_offset++; 1172 } 1173 } 1174 } else { 1175 const unsigned size = type_size(ir->type); 1176 1177 for (unsigned i = 0; i < size; i++) { 1178 switch (ir->type->base_type) { 1179 case GLSL_TYPE_FLOAT: 1180 emit(BRW_OPCODE_MOV, dst_reg, fs_reg(ir->value.f[i])); 1181 break; 1182 case GLSL_TYPE_UINT: 1183 emit(BRW_OPCODE_MOV, dst_reg, fs_reg(ir->value.u[i])); 1184 break; 1185 case GLSL_TYPE_INT: 1186 emit(BRW_OPCODE_MOV, dst_reg, fs_reg(ir->value.i[i])); 1187 break; 1188 case GLSL_TYPE_BOOL: 1189 emit(BRW_OPCODE_MOV, dst_reg, fs_reg((int)ir->value.b[i])); 1190 break; 1191 default: 1192 assert(!"Non-float/uint/int/bool constant"); 1193 } 1194 dst_reg.reg_offset++; 1195 } 1196 } 1197 1198 this->result = reg; 1199} 1200 1201void 1202fs_visitor::emit_bool_to_cond_code(ir_rvalue *ir) 1203{ 1204 ir_expression *expr = ir->as_expression(); 1205 1206 if (expr) { 1207 fs_reg op[2]; 1208 fs_inst *inst; 1209 1210 assert(expr->get_num_operands() <= 2); 1211 for (unsigned int i = 0; i < expr->get_num_operands(); i++) { 1212 assert(expr->operands[i]->type->is_scalar()); 1213 1214 this->result = reg_undef; 1215 expr->operands[i]->accept(this); 1216 op[i] = this->result; 1217 } 1218 1219 switch (expr->operation) { 1220 case ir_unop_logic_not: 1221 inst = emit(BRW_OPCODE_AND, reg_null_d, op[0], fs_reg(1)); 1222 inst->conditional_mod = BRW_CONDITIONAL_Z; 1223 break; 1224 1225 case ir_binop_logic_xor: 1226 inst = emit(BRW_OPCODE_XOR, reg_null_d, op[0], op[1]); 1227 inst->conditional_mod = BRW_CONDITIONAL_NZ; 1228 break; 1229 1230 case ir_binop_logic_or: 1231 inst = emit(BRW_OPCODE_OR, reg_null_d, op[0], op[1]); 1232 inst->conditional_mod = BRW_CONDITIONAL_NZ; 1233 break; 1234 1235 case ir_binop_logic_and: 1236 inst = emit(BRW_OPCODE_AND, reg_null_d, op[0], op[1]); 1237 inst->conditional_mod = BRW_CONDITIONAL_NZ; 1238 break; 1239 1240 case ir_unop_f2b: 1241 if (intel->gen >= 6) { 1242 inst = emit(BRW_OPCODE_CMP, reg_null_d, op[0], fs_reg(0.0f)); 1243 } else { 1244 inst = emit(BRW_OPCODE_MOV, reg_null_f, op[0]); 1245 } 1246 inst->conditional_mod = BRW_CONDITIONAL_NZ; 1247 break; 1248 1249 case ir_unop_i2b: 1250 if (intel->gen >= 6) { 1251 inst = emit(BRW_OPCODE_CMP, reg_null_d, op[0], fs_reg(0)); 1252 } else { 1253 inst = emit(BRW_OPCODE_MOV, reg_null_d, op[0]); 1254 } 1255 inst->conditional_mod = BRW_CONDITIONAL_NZ; 1256 break; 1257 1258 case ir_binop_greater: 1259 case ir_binop_gequal: 1260 case ir_binop_less: 1261 case ir_binop_lequal: 1262 case ir_binop_equal: 1263 case ir_binop_all_equal: 1264 case ir_binop_nequal: 1265 case ir_binop_any_nequal: 1266 inst = emit(BRW_OPCODE_CMP, reg_null_cmp, op[0], op[1]); 1267 inst->conditional_mod = 1268 brw_conditional_for_comparison(expr->operation); 1269 break; 1270 1271 default: 1272 assert(!"not reached"); 1273 fail("bad cond code\n"); 1274 break; 1275 } 1276 return; 1277 } 1278 1279 this->result = reg_undef; 1280 ir->accept(this); 1281 1282 if (intel->gen >= 6) { 1283 fs_inst *inst = emit(BRW_OPCODE_AND, reg_null_d, this->result, fs_reg(1)); 1284 inst->conditional_mod = BRW_CONDITIONAL_NZ; 1285 } else { 1286 fs_inst *inst = emit(BRW_OPCODE_MOV, reg_null_d, this->result); 1287 inst->conditional_mod = BRW_CONDITIONAL_NZ; 1288 } 1289} 1290 1291/** 1292 * Emit a gen6 IF statement with the comparison folded into the IF 1293 * instruction. 1294 */ 1295void 1296fs_visitor::emit_if_gen6(ir_if *ir) 1297{ 1298 ir_expression *expr = ir->condition->as_expression(); 1299 1300 if (expr) { 1301 fs_reg op[2]; 1302 fs_inst *inst; 1303 fs_reg temp; 1304 1305 assert(expr->get_num_operands() <= 2); 1306 for (unsigned int i = 0; i < expr->get_num_operands(); i++) { 1307 assert(expr->operands[i]->type->is_scalar()); 1308 1309 this->result = reg_undef; 1310 expr->operands[i]->accept(this); 1311 op[i] = this->result; 1312 } 1313 1314 switch (expr->operation) { 1315 case ir_unop_logic_not: 1316 inst = emit(BRW_OPCODE_IF, temp, op[0], fs_reg(0)); 1317 inst->conditional_mod = BRW_CONDITIONAL_Z; 1318 return; 1319 1320 case ir_binop_logic_xor: 1321 inst = emit(BRW_OPCODE_IF, reg_null_d, op[0], op[1]); 1322 inst->conditional_mod = BRW_CONDITIONAL_NZ; 1323 return; 1324 1325 case ir_binop_logic_or: 1326 temp = fs_reg(this, glsl_type::bool_type); 1327 emit(BRW_OPCODE_OR, temp, op[0], op[1]); 1328 inst = emit(BRW_OPCODE_IF, reg_null_d, temp, fs_reg(0)); 1329 inst->conditional_mod = BRW_CONDITIONAL_NZ; 1330 return; 1331 1332 case ir_binop_logic_and: 1333 temp = fs_reg(this, glsl_type::bool_type); 1334 emit(BRW_OPCODE_AND, temp, op[0], op[1]); 1335 inst = emit(BRW_OPCODE_IF, reg_null_d, temp, fs_reg(0)); 1336 inst->conditional_mod = BRW_CONDITIONAL_NZ; 1337 return; 1338 1339 case ir_unop_f2b: 1340 inst = emit(BRW_OPCODE_IF, reg_null_f, op[0], fs_reg(0)); 1341 inst->conditional_mod = BRW_CONDITIONAL_NZ; 1342 return; 1343 1344 case ir_unop_i2b: 1345 inst = emit(BRW_OPCODE_IF, reg_null_d, op[0], fs_reg(0)); 1346 inst->conditional_mod = BRW_CONDITIONAL_NZ; 1347 return; 1348 1349 case ir_binop_greater: 1350 case ir_binop_gequal: 1351 case ir_binop_less: 1352 case ir_binop_lequal: 1353 case ir_binop_equal: 1354 case ir_binop_all_equal: 1355 case ir_binop_nequal: 1356 case ir_binop_any_nequal: 1357 inst = emit(BRW_OPCODE_IF, reg_null_d, op[0], op[1]); 1358 inst->conditional_mod = 1359 brw_conditional_for_comparison(expr->operation); 1360 return; 1361 default: 1362 assert(!"not reached"); 1363 inst = emit(BRW_OPCODE_IF, reg_null_d, op[0], fs_reg(0)); 1364 inst->conditional_mod = BRW_CONDITIONAL_NZ; 1365 fail("bad condition\n"); 1366 return; 1367 } 1368 return; 1369 } 1370 1371 this->result = reg_undef; 1372 ir->condition->accept(this); 1373 1374 fs_inst *inst = emit(BRW_OPCODE_IF, reg_null_d, this->result, fs_reg(0)); 1375 inst->conditional_mod = BRW_CONDITIONAL_NZ; 1376} 1377 1378void 1379fs_visitor::visit(ir_if *ir) 1380{ 1381 fs_inst *inst; 1382 1383 if (intel->gen != 6 && c->dispatch_width == 16) { 1384 fail("Can't support (non-uniform) control flow on 16-wide\n"); 1385 } 1386 1387 /* Don't point the annotation at the if statement, because then it plus 1388 * the then and else blocks get printed. 1389 */ 1390 this->base_ir = ir->condition; 1391 1392 if (intel->gen == 6) { 1393 emit_if_gen6(ir); 1394 } else { 1395 emit_bool_to_cond_code(ir->condition); 1396 1397 inst = emit(BRW_OPCODE_IF); 1398 inst->predicated = true; 1399 } 1400 1401 foreach_iter(exec_list_iterator, iter, ir->then_instructions) { 1402 ir_instruction *ir = (ir_instruction *)iter.get(); 1403 this->base_ir = ir; 1404 this->result = reg_undef; 1405 ir->accept(this); 1406 } 1407 1408 if (!ir->else_instructions.is_empty()) { 1409 emit(BRW_OPCODE_ELSE); 1410 1411 foreach_iter(exec_list_iterator, iter, ir->else_instructions) { 1412 ir_instruction *ir = (ir_instruction *)iter.get(); 1413 this->base_ir = ir; 1414 this->result = reg_undef; 1415 ir->accept(this); 1416 } 1417 } 1418 1419 emit(BRW_OPCODE_ENDIF); 1420} 1421 1422void 1423fs_visitor::visit(ir_loop *ir) 1424{ 1425 fs_reg counter = reg_undef; 1426 1427 if (c->dispatch_width == 16) { 1428 fail("Can't support (non-uniform) control flow on 16-wide\n"); 1429 } 1430 1431 if (ir->counter) { 1432 this->base_ir = ir->counter; 1433 ir->counter->accept(this); 1434 counter = *(variable_storage(ir->counter)); 1435 1436 if (ir->from) { 1437 this->result = counter; 1438 1439 this->base_ir = ir->from; 1440 this->result = counter; 1441 ir->from->accept(this); 1442 1443 if (!this->result.equals(&counter)) 1444 emit(BRW_OPCODE_MOV, counter, this->result); 1445 } 1446 } 1447 1448 emit(BRW_OPCODE_DO); 1449 1450 if (ir->to) { 1451 this->base_ir = ir->to; 1452 this->result = reg_undef; 1453 ir->to->accept(this); 1454 1455 fs_inst *inst = emit(BRW_OPCODE_CMP, reg_null_cmp, counter, this->result); 1456 inst->conditional_mod = brw_conditional_for_comparison(ir->cmp); 1457 1458 inst = emit(BRW_OPCODE_BREAK); 1459 inst->predicated = true; 1460 } 1461 1462 foreach_iter(exec_list_iterator, iter, ir->body_instructions) { 1463 ir_instruction *ir = (ir_instruction *)iter.get(); 1464 1465 this->base_ir = ir; 1466 this->result = reg_undef; 1467 ir->accept(this); 1468 } 1469 1470 if (ir->increment) { 1471 this->base_ir = ir->increment; 1472 this->result = reg_undef; 1473 ir->increment->accept(this); 1474 emit(BRW_OPCODE_ADD, counter, counter, this->result); 1475 } 1476 1477 emit(BRW_OPCODE_WHILE); 1478} 1479 1480void 1481fs_visitor::visit(ir_loop_jump *ir) 1482{ 1483 switch (ir->mode) { 1484 case ir_loop_jump::jump_break: 1485 emit(BRW_OPCODE_BREAK); 1486 break; 1487 case ir_loop_jump::jump_continue: 1488 emit(BRW_OPCODE_CONTINUE); 1489 break; 1490 } 1491} 1492 1493void 1494fs_visitor::visit(ir_call *ir) 1495{ 1496 assert(!"FINISHME"); 1497} 1498 1499void 1500fs_visitor::visit(ir_return *ir) 1501{ 1502 assert(!"FINISHME"); 1503} 1504 1505void 1506fs_visitor::visit(ir_function *ir) 1507{ 1508 /* Ignore function bodies other than main() -- we shouldn't see calls to 1509 * them since they should all be inlined before we get to ir_to_mesa. 1510 */ 1511 if (strcmp(ir->name, "main") == 0) { 1512 const ir_function_signature *sig; 1513 exec_list empty; 1514 1515 sig = ir->matching_signature(&empty); 1516 1517 assert(sig); 1518 1519 foreach_iter(exec_list_iterator, iter, sig->body) { 1520 ir_instruction *ir = (ir_instruction *)iter.get(); 1521 this->base_ir = ir; 1522 this->result = reg_undef; 1523 ir->accept(this); 1524 } 1525 } 1526} 1527 1528void 1529fs_visitor::visit(ir_function_signature *ir) 1530{ 1531 assert(!"not reached"); 1532 (void)ir; 1533} 1534 1535fs_inst * 1536fs_visitor::emit(fs_inst inst) 1537{ 1538 fs_inst *list_inst = new(mem_ctx) fs_inst; 1539 *list_inst = inst; 1540 1541 if (force_uncompressed_stack > 0) 1542 list_inst->force_uncompressed = true; 1543 else if (force_sechalf_stack > 0) 1544 list_inst->force_sechalf = true; 1545 1546 list_inst->annotation = this->current_annotation; 1547 list_inst->ir = this->base_ir; 1548 1549 this->instructions.push_tail(list_inst); 1550 1551 return list_inst; 1552} 1553 1554/** Emits a dummy fragment shader consisting of magenta for bringup purposes. */ 1555void 1556fs_visitor::emit_dummy_fs() 1557{ 1558 /* Everyone's favorite color. */ 1559 emit(BRW_OPCODE_MOV, fs_reg(MRF, 2), fs_reg(1.0f)); 1560 emit(BRW_OPCODE_MOV, fs_reg(MRF, 3), fs_reg(0.0f)); 1561 emit(BRW_OPCODE_MOV, fs_reg(MRF, 4), fs_reg(1.0f)); 1562 emit(BRW_OPCODE_MOV, fs_reg(MRF, 5), fs_reg(0.0f)); 1563 1564 fs_inst *write; 1565 write = emit(FS_OPCODE_FB_WRITE, fs_reg(0), fs_reg(0)); 1566 write->base_mrf = 0; 1567} 1568 1569/* The register location here is relative to the start of the URB 1570 * data. It will get adjusted to be a real location before 1571 * generate_code() time. 1572 */ 1573struct brw_reg 1574fs_visitor::interp_reg(int location, int channel) 1575{ 1576 int regnr = urb_setup[location] * 2 + channel / 2; 1577 int stride = (channel & 1) * 4; 1578 1579 assert(urb_setup[location] != -1); 1580 1581 return brw_vec1_grf(regnr, stride); 1582} 1583 1584/** Emits the interpolation for the varying inputs. */ 1585void 1586fs_visitor::emit_interpolation_setup_gen4() 1587{ 1588 this->current_annotation = "compute pixel centers"; 1589 this->pixel_x = fs_reg(this, glsl_type::uint_type); 1590 this->pixel_y = fs_reg(this, glsl_type::uint_type); 1591 this->pixel_x.type = BRW_REGISTER_TYPE_UW; 1592 this->pixel_y.type = BRW_REGISTER_TYPE_UW; 1593 1594 emit(FS_OPCODE_PIXEL_X, this->pixel_x); 1595 emit(FS_OPCODE_PIXEL_Y, this->pixel_y); 1596 1597 this->current_annotation = "compute pixel deltas from v0"; 1598 if (brw->has_pln) { 1599 this->delta_x = fs_reg(this, glsl_type::vec2_type); 1600 this->delta_y = this->delta_x; 1601 this->delta_y.reg_offset++; 1602 } else { 1603 this->delta_x = fs_reg(this, glsl_type::float_type); 1604 this->delta_y = fs_reg(this, glsl_type::float_type); 1605 } 1606 emit(BRW_OPCODE_ADD, this->delta_x, 1607 this->pixel_x, fs_reg(negate(brw_vec1_grf(1, 0)))); 1608 emit(BRW_OPCODE_ADD, this->delta_y, 1609 this->pixel_y, fs_reg(negate(brw_vec1_grf(1, 1)))); 1610 1611 this->current_annotation = "compute pos.w and 1/pos.w"; 1612 /* Compute wpos.w. It's always in our setup, since it's needed to 1613 * interpolate the other attributes. 1614 */ 1615 this->wpos_w = fs_reg(this, glsl_type::float_type); 1616 emit(FS_OPCODE_LINTERP, wpos_w, this->delta_x, this->delta_y, 1617 interp_reg(FRAG_ATTRIB_WPOS, 3)); 1618 /* Compute the pixel 1/W value from wpos.w. */ 1619 this->pixel_w = fs_reg(this, glsl_type::float_type); 1620 emit_math(FS_OPCODE_RCP, this->pixel_w, wpos_w); 1621 this->current_annotation = NULL; 1622} 1623 1624/** Emits the interpolation for the varying inputs. */ 1625void 1626fs_visitor::emit_interpolation_setup_gen6() 1627{ 1628 struct brw_reg g1_uw = retype(brw_vec1_grf(1, 0), BRW_REGISTER_TYPE_UW); 1629 1630 /* If the pixel centers end up used, the setup is the same as for gen4. */ 1631 this->current_annotation = "compute pixel centers"; 1632 fs_reg int_pixel_x = fs_reg(this, glsl_type::uint_type); 1633 fs_reg int_pixel_y = fs_reg(this, glsl_type::uint_type); 1634 int_pixel_x.type = BRW_REGISTER_TYPE_UW; 1635 int_pixel_y.type = BRW_REGISTER_TYPE_UW; 1636 emit(BRW_OPCODE_ADD, 1637 int_pixel_x, 1638 fs_reg(stride(suboffset(g1_uw, 4), 2, 4, 0)), 1639 fs_reg(brw_imm_v(0x10101010))); 1640 emit(BRW_OPCODE_ADD, 1641 int_pixel_y, 1642 fs_reg(stride(suboffset(g1_uw, 5), 2, 4, 0)), 1643 fs_reg(brw_imm_v(0x11001100))); 1644 1645 /* As of gen6, we can no longer mix float and int sources. We have 1646 * to turn the integer pixel centers into floats for their actual 1647 * use. 1648 */ 1649 this->pixel_x = fs_reg(this, glsl_type::float_type); 1650 this->pixel_y = fs_reg(this, glsl_type::float_type); 1651 emit(BRW_OPCODE_MOV, this->pixel_x, int_pixel_x); 1652 emit(BRW_OPCODE_MOV, this->pixel_y, int_pixel_y); 1653 1654 this->current_annotation = "compute pos.w"; 1655 this->pixel_w = fs_reg(brw_vec8_grf(c->source_w_reg, 0)); 1656 this->wpos_w = fs_reg(this, glsl_type::float_type); 1657 emit_math(FS_OPCODE_RCP, this->wpos_w, this->pixel_w); 1658 1659 this->delta_x = fs_reg(brw_vec8_grf(2, 0)); 1660 this->delta_y = fs_reg(brw_vec8_grf(3, 0)); 1661 1662 this->current_annotation = NULL; 1663} 1664 1665void 1666fs_visitor::emit_color_write(int index, int first_color_mrf, fs_reg color) 1667{ 1668 int reg_width = c->dispatch_width / 8; 1669 1670 if (c->dispatch_width == 8 || intel->gen == 6) { 1671 /* SIMD8 write looks like: 1672 * m + 0: r0 1673 * m + 1: r1 1674 * m + 2: g0 1675 * m + 3: g1 1676 * 1677 * gen6 SIMD16 DP write looks like: 1678 * m + 0: r0 1679 * m + 1: r1 1680 * m + 2: g0 1681 * m + 3: g1 1682 * m + 4: b0 1683 * m + 5: b1 1684 * m + 6: a0 1685 * m + 7: a1 1686 */ 1687 emit(BRW_OPCODE_MOV, fs_reg(MRF, first_color_mrf + index * reg_width), 1688 color); 1689 } else { 1690 /* pre-gen6 SIMD16 single source DP write looks like: 1691 * m + 0: r0 1692 * m + 1: g0 1693 * m + 2: b0 1694 * m + 3: a0 1695 * m + 4: r1 1696 * m + 5: g1 1697 * m + 6: b1 1698 * m + 7: a1 1699 */ 1700 if (brw->has_compr4) { 1701 /* By setting the high bit of the MRF register number, we 1702 * indicate that we want COMPR4 mode - instead of doing the 1703 * usual destination + 1 for the second half we get 1704 * destination + 4. 1705 */ 1706 emit(BRW_OPCODE_MOV, 1707 fs_reg(MRF, BRW_MRF_COMPR4 + first_color_mrf + index), color); 1708 } else { 1709 push_force_uncompressed(); 1710 emit(BRW_OPCODE_MOV, fs_reg(MRF, first_color_mrf + index), color); 1711 pop_force_uncompressed(); 1712 1713 push_force_sechalf(); 1714 color.sechalf = true; 1715 emit(BRW_OPCODE_MOV, fs_reg(MRF, first_color_mrf + index + 4), color); 1716 pop_force_sechalf(); 1717 color.sechalf = false; 1718 } 1719 } 1720} 1721 1722void 1723fs_visitor::emit_fb_writes() 1724{ 1725 this->current_annotation = "FB write header"; 1726 GLboolean header_present = GL_TRUE; 1727 int nr = 0; 1728 int reg_width = c->dispatch_width / 8; 1729 1730 if (intel->gen >= 6 && 1731 !this->kill_emitted && 1732 c->key.nr_color_regions == 1) { 1733 header_present = false; 1734 } 1735 1736 if (header_present) { 1737 /* m0, m1 header */ 1738 nr += 2; 1739 } 1740 1741 if (c->aa_dest_stencil_reg) { 1742 push_force_uncompressed(); 1743 emit(BRW_OPCODE_MOV, fs_reg(MRF, nr++), 1744 fs_reg(brw_vec8_grf(c->aa_dest_stencil_reg, 0))); 1745 pop_force_uncompressed(); 1746 } 1747 1748 /* Reserve space for color. It'll be filled in per MRT below. */ 1749 int color_mrf = nr; 1750 nr += 4 * reg_width; 1751 1752 if (c->source_depth_to_render_target) { 1753 if (intel->gen == 6 && c->dispatch_width == 16) { 1754 /* For outputting oDepth on gen6, SIMD8 writes have to be 1755 * used. This would require 8-wide moves of each half to 1756 * message regs, kind of like pre-gen5 SIMD16 FB writes. 1757 * Just bail on doing so for now. 1758 */ 1759 fail("Missing support for simd16 depth writes on gen6\n"); 1760 } 1761 1762 if (c->computes_depth) { 1763 /* Hand over gl_FragDepth. */ 1764 assert(this->frag_depth); 1765 fs_reg depth = *(variable_storage(this->frag_depth)); 1766 1767 emit(BRW_OPCODE_MOV, fs_reg(MRF, nr), depth); 1768 } else { 1769 /* Pass through the payload depth. */ 1770 emit(BRW_OPCODE_MOV, fs_reg(MRF, nr), 1771 fs_reg(brw_vec8_grf(c->source_depth_reg, 0))); 1772 } 1773 nr += reg_width; 1774 } 1775 1776 if (c->dest_depth_reg) { 1777 emit(BRW_OPCODE_MOV, fs_reg(MRF, nr), 1778 fs_reg(brw_vec8_grf(c->dest_depth_reg, 0))); 1779 nr += reg_width; 1780 } 1781 1782 fs_reg color = reg_undef; 1783 if (this->frag_color) 1784 color = *(variable_storage(this->frag_color)); 1785 else if (this->frag_data) { 1786 color = *(variable_storage(this->frag_data)); 1787 color.type = BRW_REGISTER_TYPE_F; 1788 } 1789 1790 for (int target = 0; target < c->key.nr_color_regions; target++) { 1791 this->current_annotation = ralloc_asprintf(this->mem_ctx, 1792 "FB write target %d", 1793 target); 1794 if (this->frag_color || this->frag_data) { 1795 for (int i = 0; i < 4; i++) { 1796 emit_color_write(i, color_mrf, color); 1797 color.reg_offset++; 1798 } 1799 } 1800 1801 if (this->frag_color) 1802 color.reg_offset -= 4; 1803 1804 fs_inst *inst = emit(FS_OPCODE_FB_WRITE); 1805 inst->target = target; 1806 inst->base_mrf = 0; 1807 inst->mlen = nr; 1808 if (target == c->key.nr_color_regions - 1) 1809 inst->eot = true; 1810 inst->header_present = header_present; 1811 } 1812 1813 if (c->key.nr_color_regions == 0) { 1814 if (c->key.alpha_test && (this->frag_color || this->frag_data)) { 1815 /* If the alpha test is enabled but there's no color buffer, 1816 * we still need to send alpha out the pipeline to our null 1817 * renderbuffer. 1818 */ 1819 color.reg_offset += 3; 1820 emit_color_write(3, color_mrf, color); 1821 } 1822 1823 fs_inst *inst = emit(FS_OPCODE_FB_WRITE); 1824 inst->base_mrf = 0; 1825 inst->mlen = nr; 1826 inst->eot = true; 1827 inst->header_present = header_present; 1828 } 1829 1830 this->current_annotation = NULL; 1831} 1832