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