1d2da8cf1b67fc6b7876a2550182dcd77d3ddcdc
[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 "program/prog_parameter.h"
37 #include "program/prog_print.h"
38 #include "program/prog_optimize.h"
39 #include "program/register_allocate.h"
40 #include "program/sampler.h"
41 #include "program/hash_table.h"
42 #include "brw_context.h"
43 #include "brw_eu.h"
44 #include "brw_wm.h"
45 }
46 #include "brw_fs.h"
47 #include "main/uniforms.h"
48 #include "glsl/glsl_types.h"
49 #include "glsl/ir_optimization.h"
50
51 void
52 fs_visitor::visit(ir_variable *ir)
53 {
54 fs_reg *reg = NULL;
55
56 if (variable_storage(ir))
57 return;
58
59 if (ir->mode == ir_var_shader_in) {
60 if (!strcmp(ir->name, "gl_FragCoord")) {
61 reg = emit_fragcoord_interpolation(ir);
62 } else if (!strcmp(ir->name, "gl_FrontFacing")) {
63 reg = emit_frontfacing_interpolation(ir);
64 } else {
65 reg = emit_general_interpolation(ir);
66 }
67 assert(reg);
68 hash_table_insert(this->variable_ht, reg, ir);
69 return;
70 } else if (ir->mode == ir_var_shader_out) {
71 reg = new(this->mem_ctx) fs_reg(this, ir->type);
72
73 if (ir->index > 0) {
74 assert(ir->location == FRAG_RESULT_DATA0);
75 assert(ir->index == 1);
76 this->dual_src_output = *reg;
77 } else if (ir->location == FRAG_RESULT_COLOR) {
78 /* Writing gl_FragColor outputs to all color regions. */
79 for (unsigned int i = 0; i < MAX2(c->key.nr_color_regions, 1); i++) {
80 this->outputs[i] = *reg;
81 this->output_components[i] = 4;
82 }
83 } else if (ir->location == FRAG_RESULT_DEPTH) {
84 this->frag_depth = *reg;
85 } else {
86 /* gl_FragData or a user-defined FS output */
87 assert(ir->location >= FRAG_RESULT_DATA0 &&
88 ir->location < FRAG_RESULT_DATA0 + BRW_MAX_DRAW_BUFFERS);
89
90 int vector_elements =
91 ir->type->is_array() ? ir->type->fields.array->vector_elements
92 : ir->type->vector_elements;
93
94 /* General color output. */
95 for (unsigned int i = 0; i < MAX2(1, ir->type->length); i++) {
96 int output = ir->location - FRAG_RESULT_DATA0 + i;
97 this->outputs[output] = *reg;
98 this->outputs[output].reg_offset += vector_elements * i;
99 this->output_components[output] = vector_elements;
100 }
101 }
102 } else if (ir->mode == ir_var_uniform) {
103 int param_index = c->prog_data.nr_params;
104
105 /* Thanks to the lower_ubo_reference pass, we will see only
106 * ir_binop_ubo_load expressions and not ir_dereference_variable for UBO
107 * variables, so no need for them to be in variable_ht.
108 */
109 if (ir->is_in_uniform_block())
110 return;
111
112 if (dispatch_width == 16) {
113 if (!variable_storage(ir)) {
114 fail("Failed to find uniform '%s' in 16-wide\n", ir->name);
115 }
116 return;
117 }
118
119 param_size[param_index] = type_size(ir->type);
120 if (!strncmp(ir->name, "gl_", 3)) {
121 setup_builtin_uniform_values(ir);
122 } else {
123 setup_uniform_values(ir);
124 }
125
126 reg = new(this->mem_ctx) fs_reg(UNIFORM, param_index);
127 reg->type = brw_type_for_base_type(ir->type);
128 }
129
130 if (!reg)
131 reg = new(this->mem_ctx) fs_reg(this, ir->type);
132
133 hash_table_insert(this->variable_ht, reg, ir);
134 }
135
136 void
137 fs_visitor::visit(ir_dereference_variable *ir)
138 {
139 fs_reg *reg = variable_storage(ir->var);
140 this->result = *reg;
141 }
142
143 void
144 fs_visitor::visit(ir_dereference_record *ir)
145 {
146 const glsl_type *struct_type = ir->record->type;
147
148 ir->record->accept(this);
149
150 unsigned int offset = 0;
151 for (unsigned int i = 0; i < struct_type->length; i++) {
152 if (strcmp(struct_type->fields.structure[i].name, ir->field) == 0)
153 break;
154 offset += type_size(struct_type->fields.structure[i].type);
155 }
156 this->result.reg_offset += offset;
157 this->result.type = brw_type_for_base_type(ir->type);
158 }
159
160 void
161 fs_visitor::visit(ir_dereference_array *ir)
162 {
163 ir_constant *constant_index;
164 fs_reg src;
165 int element_size = type_size(ir->type);
166
167 constant_index = ir->array_index->as_constant();
168
169 ir->array->accept(this);
170 src = this->result;
171 src.type = brw_type_for_base_type(ir->type);
172
173 if (constant_index) {
174 assert(src.file == UNIFORM || src.file == GRF);
175 src.reg_offset += constant_index->value.i[0] * element_size;
176 } else {
177 /* Variable index array dereference. We attach the variable index
178 * component to the reg as a pointer to a register containing the
179 * offset. Currently only uniform arrays are supported in this patch,
180 * and that reladdr pointer is resolved by
181 * move_uniform_array_access_to_pull_constants(). All other array types
182 * are lowered by lower_variable_index_to_cond_assign().
183 */
184 ir->array_index->accept(this);
185
186 fs_reg index_reg;
187 index_reg = fs_reg(this, glsl_type::int_type);
188 emit(BRW_OPCODE_MUL, index_reg, this->result, fs_reg(element_size));
189
190 if (src.reladdr) {
191 emit(BRW_OPCODE_ADD, index_reg, *src.reladdr, index_reg);
192 }
193
194 src.reladdr = ralloc(mem_ctx, fs_reg);
195 memcpy(src.reladdr, &index_reg, sizeof(index_reg));
196 }
197 this->result = src;
198 }
199
200 void
201 fs_visitor::emit_lrp(fs_reg dst, fs_reg x, fs_reg y, fs_reg a)
202 {
203 if (brw->gen < 6 ||
204 !x.is_valid_3src() ||
205 !y.is_valid_3src() ||
206 !a.is_valid_3src()) {
207 /* We can't use the LRP instruction. Emit x*(1-a) + y*a. */
208 fs_reg y_times_a = fs_reg(this, glsl_type::float_type);
209 fs_reg one_minus_a = fs_reg(this, glsl_type::float_type);
210 fs_reg x_times_one_minus_a = fs_reg(this, glsl_type::float_type);
211
212 emit(MUL(y_times_a, y, a));
213
214 a.negate = !a.negate;
215 emit(ADD(one_minus_a, a, fs_reg(1.0f)));
216 emit(MUL(x_times_one_minus_a, x, one_minus_a));
217
218 emit(ADD(dst, x_times_one_minus_a, y_times_a));
219 } else {
220 /* The LRP instruction actually does op1 * op0 + op2 * (1 - op0), so
221 * we need to reorder the operands.
222 */
223 emit(LRP(dst, a, y, x));
224 }
225 }
226
227 void
228 fs_visitor::emit_minmax(uint32_t conditionalmod, fs_reg dst,
229 fs_reg src0, fs_reg src1)
230 {
231 fs_inst *inst;
232
233 if (brw->gen >= 6) {
234 inst = emit(BRW_OPCODE_SEL, dst, src0, src1);
235 inst->conditional_mod = conditionalmod;
236 } else {
237 emit(CMP(reg_null_d, src0, src1, conditionalmod));
238
239 inst = emit(BRW_OPCODE_SEL, dst, src0, src1);
240 inst->predicate = BRW_PREDICATE_NORMAL;
241 }
242 }
243
244 /* Instruction selection: Produce a MOV.sat instead of
245 * MIN(MAX(val, 0), 1) when possible.
246 */
247 bool
248 fs_visitor::try_emit_saturate(ir_expression *ir)
249 {
250 ir_rvalue *sat_val = ir->as_rvalue_to_saturate();
251
252 if (!sat_val)
253 return false;
254
255 fs_inst *pre_inst = (fs_inst *) this->instructions.get_tail();
256
257 sat_val->accept(this);
258 fs_reg src = this->result;
259
260 fs_inst *last_inst = (fs_inst *) this->instructions.get_tail();
261
262 /* If the last instruction from our accept() didn't generate our
263 * src, generate a saturated MOV
264 */
265 fs_inst *modify = get_instruction_generating_reg(pre_inst, last_inst, src);
266 if (!modify || modify->regs_written != 1) {
267 this->result = fs_reg(this, ir->type);
268 fs_inst *inst = emit(MOV(this->result, src));
269 inst->saturate = true;
270 } else {
271 modify->saturate = true;
272 this->result = src;
273 }
274
275
276 return true;
277 }
278
279 bool
280 fs_visitor::try_emit_mad(ir_expression *ir, int mul_arg)
281 {
282 /* 3-src instructions were introduced in gen6. */
283 if (brw->gen < 6)
284 return false;
285
286 /* MAD can only handle floating-point data. */
287 if (ir->type != glsl_type::float_type)
288 return false;
289
290 ir_rvalue *nonmul = ir->operands[1 - mul_arg];
291 ir_expression *mul = ir->operands[mul_arg]->as_expression();
292
293 if (!mul || mul->operation != ir_binop_mul)
294 return false;
295
296 if (nonmul->as_constant() ||
297 mul->operands[0]->as_constant() ||
298 mul->operands[1]->as_constant())
299 return false;
300
301 nonmul->accept(this);
302 fs_reg src0 = this->result;
303
304 mul->operands[0]->accept(this);
305 fs_reg src1 = this->result;
306
307 mul->operands[1]->accept(this);
308 fs_reg src2 = this->result;
309
310 this->result = fs_reg(this, ir->type);
311 emit(BRW_OPCODE_MAD, this->result, src0, src1, src2);
312
313 return true;
314 }
315
316 void
317 fs_visitor::visit(ir_expression *ir)
318 {
319 unsigned int operand;
320 fs_reg op[3], temp;
321 fs_inst *inst;
322
323 assert(ir->get_num_operands() <= 3);
324
325 if (try_emit_saturate(ir))
326 return;
327 if (ir->operation == ir_binop_add) {
328 if (try_emit_mad(ir, 0) || try_emit_mad(ir, 1))
329 return;
330 }
331
332 for (operand = 0; operand < ir->get_num_operands(); operand++) {
333 ir->operands[operand]->accept(this);
334 if (this->result.file == BAD_FILE) {
335 fail("Failed to get tree for expression operand:\n");
336 ir->operands[operand]->print();
337 printf("\n");
338 }
339 assert(this->result.is_valid_3src());
340 op[operand] = this->result;
341
342 /* Matrix expression operands should have been broken down to vector
343 * operations already.
344 */
345 assert(!ir->operands[operand]->type->is_matrix());
346 /* And then those vector operands should have been broken down to scalar.
347 */
348 assert(!ir->operands[operand]->type->is_vector());
349 }
350
351 /* Storage for our result. If our result goes into an assignment, it will
352 * just get copy-propagated out, so no worries.
353 */
354 this->result = fs_reg(this, ir->type);
355
356 switch (ir->operation) {
357 case ir_unop_logic_not:
358 /* Note that BRW_OPCODE_NOT is not appropriate here, since it is
359 * ones complement of the whole register, not just bit 0.
360 */
361 emit(XOR(this->result, op[0], fs_reg(1)));
362 break;
363 case ir_unop_neg:
364 op[0].negate = !op[0].negate;
365 emit(MOV(this->result, op[0]));
366 break;
367 case ir_unop_abs:
368 op[0].abs = true;
369 op[0].negate = false;
370 emit(MOV(this->result, op[0]));
371 break;
372 case ir_unop_sign:
373 temp = fs_reg(this, ir->type);
374
375 emit(MOV(this->result, fs_reg(0.0f)));
376
377 emit(CMP(reg_null_f, op[0], fs_reg(0.0f), BRW_CONDITIONAL_G));
378 inst = emit(MOV(this->result, fs_reg(1.0f)));
379 inst->predicate = BRW_PREDICATE_NORMAL;
380
381 emit(CMP(reg_null_f, op[0], fs_reg(0.0f), BRW_CONDITIONAL_L));
382 inst = emit(MOV(this->result, fs_reg(-1.0f)));
383 inst->predicate = BRW_PREDICATE_NORMAL;
384
385 break;
386 case ir_unop_rcp:
387 emit_math(SHADER_OPCODE_RCP, this->result, op[0]);
388 break;
389
390 case ir_unop_exp2:
391 emit_math(SHADER_OPCODE_EXP2, this->result, op[0]);
392 break;
393 case ir_unop_log2:
394 emit_math(SHADER_OPCODE_LOG2, this->result, op[0]);
395 break;
396 case ir_unop_exp:
397 case ir_unop_log:
398 assert(!"not reached: should be handled by ir_explog_to_explog2");
399 break;
400 case ir_unop_sin:
401 case ir_unop_sin_reduced:
402 emit_math(SHADER_OPCODE_SIN, this->result, op[0]);
403 break;
404 case ir_unop_cos:
405 case ir_unop_cos_reduced:
406 emit_math(SHADER_OPCODE_COS, this->result, op[0]);
407 break;
408
409 case ir_unop_dFdx:
410 emit(FS_OPCODE_DDX, this->result, op[0]);
411 break;
412 case ir_unop_dFdy:
413 emit(FS_OPCODE_DDY, this->result, op[0]);
414 break;
415
416 case ir_binop_add:
417 emit(ADD(this->result, op[0], op[1]));
418 break;
419 case ir_binop_sub:
420 assert(!"not reached: should be handled by ir_sub_to_add_neg");
421 break;
422
423 case ir_binop_mul:
424 if (ir->type->is_integer()) {
425 /* For integer multiplication, the MUL uses the low 16 bits
426 * of one of the operands (src0 on gen6, src1 on gen7). The
427 * MACH accumulates in the contribution of the upper 16 bits
428 * of that operand.
429 *
430 * FINISHME: Emit just the MUL if we know an operand is small
431 * enough.
432 */
433 if (brw->gen >= 7 && dispatch_width == 16)
434 fail("16-wide explicit accumulator operands unsupported\n");
435
436 struct brw_reg acc = retype(brw_acc_reg(), this->result.type);
437
438 emit(MUL(acc, op[0], op[1]));
439 emit(MACH(reg_null_d, op[0], op[1]));
440 emit(MOV(this->result, fs_reg(acc)));
441 } else {
442 emit(MUL(this->result, op[0], op[1]));
443 }
444 break;
445 case ir_binop_imul_high: {
446 if (brw->gen >= 7 && dispatch_width == 16)
447 fail("16-wide explicit accumulator operands unsupported\n");
448
449 struct brw_reg acc = retype(brw_acc_reg(), this->result.type);
450
451 emit(MUL(acc, op[0], op[1]));
452 emit(MACH(this->result, op[0], op[1]));
453 break;
454 }
455 case ir_binop_div:
456 /* Floating point should be lowered by DIV_TO_MUL_RCP in the compiler. */
457 assert(ir->type->is_integer());
458 emit_math(SHADER_OPCODE_INT_QUOTIENT, this->result, op[0], op[1]);
459 break;
460 case ir_binop_carry: {
461 if (brw->gen >= 7 && dispatch_width == 16)
462 fail("16-wide explicit accumulator operands unsupported\n");
463
464 struct brw_reg acc = retype(brw_acc_reg(), BRW_REGISTER_TYPE_UD);
465
466 emit(ADDC(reg_null_ud, op[0], op[1]));
467 emit(MOV(this->result, fs_reg(acc)));
468 break;
469 }
470 case ir_binop_borrow: {
471 if (brw->gen >= 7 && dispatch_width == 16)
472 fail("16-wide explicit accumulator operands unsupported\n");
473
474 struct brw_reg acc = retype(brw_acc_reg(), BRW_REGISTER_TYPE_UD);
475
476 emit(SUBB(reg_null_ud, op[0], op[1]));
477 emit(MOV(this->result, fs_reg(acc)));
478 break;
479 }
480 case ir_binop_mod:
481 /* Floating point should be lowered by MOD_TO_FRACT in the compiler. */
482 assert(ir->type->is_integer());
483 emit_math(SHADER_OPCODE_INT_REMAINDER, this->result, op[0], op[1]);
484 break;
485
486 case ir_binop_less:
487 case ir_binop_greater:
488 case ir_binop_lequal:
489 case ir_binop_gequal:
490 case ir_binop_equal:
491 case ir_binop_all_equal:
492 case ir_binop_nequal:
493 case ir_binop_any_nequal:
494 resolve_bool_comparison(ir->operands[0], &op[0]);
495 resolve_bool_comparison(ir->operands[1], &op[1]);
496
497 emit(CMP(this->result, op[0], op[1],
498 brw_conditional_for_comparison(ir->operation)));
499 break;
500
501 case ir_binop_logic_xor:
502 emit(XOR(this->result, op[0], op[1]));
503 break;
504
505 case ir_binop_logic_or:
506 emit(OR(this->result, op[0], op[1]));
507 break;
508
509 case ir_binop_logic_and:
510 emit(AND(this->result, op[0], op[1]));
511 break;
512
513 case ir_binop_dot:
514 case ir_unop_any:
515 assert(!"not reached: should be handled by brw_fs_channel_expressions");
516 break;
517
518 case ir_unop_noise:
519 assert(!"not reached: should be handled by lower_noise");
520 break;
521
522 case ir_quadop_vector:
523 assert(!"not reached: should be handled by lower_quadop_vector");
524 break;
525
526 case ir_binop_vector_extract:
527 assert(!"not reached: should be handled by lower_vec_index_to_cond_assign()");
528 break;
529
530 case ir_triop_vector_insert:
531 assert(!"not reached: should be handled by lower_vector_insert()");
532 break;
533
534 case ir_binop_ldexp:
535 assert(!"not reached: should be handled by ldexp_to_arith()");
536 break;
537
538 case ir_unop_sqrt:
539 emit_math(SHADER_OPCODE_SQRT, this->result, op[0]);
540 break;
541
542 case ir_unop_rsq:
543 emit_math(SHADER_OPCODE_RSQ, this->result, op[0]);
544 break;
545
546 case ir_unop_bitcast_i2f:
547 case ir_unop_bitcast_u2f:
548 op[0].type = BRW_REGISTER_TYPE_F;
549 this->result = op[0];
550 break;
551 case ir_unop_i2u:
552 case ir_unop_bitcast_f2u:
553 op[0].type = BRW_REGISTER_TYPE_UD;
554 this->result = op[0];
555 break;
556 case ir_unop_u2i:
557 case ir_unop_bitcast_f2i:
558 op[0].type = BRW_REGISTER_TYPE_D;
559 this->result = op[0];
560 break;
561 case ir_unop_i2f:
562 case ir_unop_u2f:
563 case ir_unop_f2i:
564 case ir_unop_f2u:
565 emit(MOV(this->result, op[0]));
566 break;
567
568 case ir_unop_b2i:
569 emit(AND(this->result, op[0], fs_reg(1)));
570 break;
571 case ir_unop_b2f:
572 temp = fs_reg(this, glsl_type::int_type);
573 emit(AND(temp, op[0], fs_reg(1)));
574 emit(MOV(this->result, temp));
575 break;
576
577 case ir_unop_f2b:
578 emit(CMP(this->result, op[0], fs_reg(0.0f), BRW_CONDITIONAL_NZ));
579 break;
580 case ir_unop_i2b:
581 emit(CMP(this->result, op[0], fs_reg(0), BRW_CONDITIONAL_NZ));
582 break;
583
584 case ir_unop_trunc:
585 emit(RNDZ(this->result, op[0]));
586 break;
587 case ir_unop_ceil:
588 op[0].negate = !op[0].negate;
589 emit(RNDD(this->result, op[0]));
590 this->result.negate = true;
591 break;
592 case ir_unop_floor:
593 emit(RNDD(this->result, op[0]));
594 break;
595 case ir_unop_fract:
596 emit(FRC(this->result, op[0]));
597 break;
598 case ir_unop_round_even:
599 emit(RNDE(this->result, op[0]));
600 break;
601
602 case ir_binop_min:
603 case ir_binop_max:
604 resolve_ud_negate(&op[0]);
605 resolve_ud_negate(&op[1]);
606 emit_minmax(ir->operation == ir_binop_min ?
607 BRW_CONDITIONAL_L : BRW_CONDITIONAL_GE,
608 this->result, op[0], op[1]);
609 break;
610 case ir_unop_pack_snorm_2x16:
611 case ir_unop_pack_snorm_4x8:
612 case ir_unop_pack_unorm_2x16:
613 case ir_unop_pack_unorm_4x8:
614 case ir_unop_unpack_snorm_2x16:
615 case ir_unop_unpack_snorm_4x8:
616 case ir_unop_unpack_unorm_2x16:
617 case ir_unop_unpack_unorm_4x8:
618 case ir_unop_unpack_half_2x16:
619 case ir_unop_pack_half_2x16:
620 assert(!"not reached: should be handled by lower_packing_builtins");
621 break;
622 case ir_unop_unpack_half_2x16_split_x:
623 emit(FS_OPCODE_UNPACK_HALF_2x16_SPLIT_X, this->result, op[0]);
624 break;
625 case ir_unop_unpack_half_2x16_split_y:
626 emit(FS_OPCODE_UNPACK_HALF_2x16_SPLIT_Y, this->result, op[0]);
627 break;
628 case ir_binop_pow:
629 emit_math(SHADER_OPCODE_POW, this->result, op[0], op[1]);
630 break;
631
632 case ir_unop_bitfield_reverse:
633 emit(BFREV(this->result, op[0]));
634 break;
635 case ir_unop_bit_count:
636 emit(CBIT(this->result, op[0]));
637 break;
638 case ir_unop_find_msb:
639 temp = fs_reg(this, glsl_type::uint_type);
640 emit(FBH(temp, op[0]));
641
642 /* FBH counts from the MSB side, while GLSL's findMSB() wants the count
643 * from the LSB side. If FBH didn't return an error (0xFFFFFFFF), then
644 * subtract the result from 31 to convert the MSB count into an LSB count.
645 */
646
647 /* FBH only supports UD type for dst, so use a MOV to convert UD to D. */
648 emit(MOV(this->result, temp));
649 emit(CMP(reg_null_d, this->result, fs_reg(-1), BRW_CONDITIONAL_NZ));
650
651 temp.negate = true;
652 inst = emit(ADD(this->result, temp, fs_reg(31)));
653 inst->predicate = BRW_PREDICATE_NORMAL;
654 break;
655 case ir_unop_find_lsb:
656 emit(FBL(this->result, op[0]));
657 break;
658 case ir_triop_bitfield_extract:
659 /* Note that the instruction's argument order is reversed from GLSL
660 * and the IR.
661 */
662 emit(BFE(this->result, op[2], op[1], op[0]));
663 break;
664 case ir_binop_bfm:
665 emit(BFI1(this->result, op[0], op[1]));
666 break;
667 case ir_triop_bfi:
668 emit(BFI2(this->result, op[0], op[1], op[2]));
669 break;
670 case ir_quadop_bitfield_insert:
671 assert(!"not reached: should be handled by "
672 "lower_instructions::bitfield_insert_to_bfm_bfi");
673 break;
674
675 case ir_unop_bit_not:
676 emit(NOT(this->result, op[0]));
677 break;
678 case ir_binop_bit_and:
679 emit(AND(this->result, op[0], op[1]));
680 break;
681 case ir_binop_bit_xor:
682 emit(XOR(this->result, op[0], op[1]));
683 break;
684 case ir_binop_bit_or:
685 emit(OR(this->result, op[0], op[1]));
686 break;
687
688 case ir_binop_lshift:
689 emit(SHL(this->result, op[0], op[1]));
690 break;
691
692 case ir_binop_rshift:
693 if (ir->type->base_type == GLSL_TYPE_INT)
694 emit(ASR(this->result, op[0], op[1]));
695 else
696 emit(SHR(this->result, op[0], op[1]));
697 break;
698 case ir_binop_pack_half_2x16_split:
699 emit(FS_OPCODE_PACK_HALF_2x16_SPLIT, this->result, op[0], op[1]);
700 break;
701 case ir_binop_ubo_load: {
702 /* This IR node takes a constant uniform block and a constant or
703 * variable byte offset within the block and loads a vector from that.
704 */
705 ir_constant *uniform_block = ir->operands[0]->as_constant();
706 ir_constant *const_offset = ir->operands[1]->as_constant();
707 fs_reg surf_index = fs_reg(c->prog_data.base.binding_table.ubo_start +
708 uniform_block->value.u[0]);
709 if (const_offset) {
710 fs_reg packed_consts = fs_reg(this, glsl_type::float_type);
711 packed_consts.type = result.type;
712
713 fs_reg const_offset_reg = fs_reg(const_offset->value.u[0] & ~15);
714 emit(fs_inst(FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD,
715 packed_consts, surf_index, const_offset_reg));
716
717 packed_consts.smear = const_offset->value.u[0] % 16 / 4;
718 for (int i = 0; i < ir->type->vector_elements; i++) {
719 /* UBO bools are any nonzero value. We consider bools to be
720 * values with the low bit set to 1. Convert them using CMP.
721 */
722 if (ir->type->base_type == GLSL_TYPE_BOOL) {
723 emit(CMP(result, packed_consts, fs_reg(0u), BRW_CONDITIONAL_NZ));
724 } else {
725 emit(MOV(result, packed_consts));
726 }
727
728 packed_consts.smear++;
729 result.reg_offset++;
730
731 /* The std140 packing rules don't allow vectors to cross 16-byte
732 * boundaries, and a reg is 32 bytes.
733 */
734 assert(packed_consts.smear < 8);
735 }
736 } else {
737 /* Turn the byte offset into a dword offset. */
738 fs_reg base_offset = fs_reg(this, glsl_type::int_type);
739 emit(SHR(base_offset, op[1], fs_reg(2)));
740
741 for (int i = 0; i < ir->type->vector_elements; i++) {
742 emit(VARYING_PULL_CONSTANT_LOAD(result, surf_index,
743 base_offset, i));
744
745 if (ir->type->base_type == GLSL_TYPE_BOOL)
746 emit(CMP(result, result, fs_reg(0), BRW_CONDITIONAL_NZ));
747
748 result.reg_offset++;
749 }
750 }
751
752 result.reg_offset = 0;
753 break;
754 }
755
756 case ir_triop_fma:
757 /* Note that the instruction's argument order is reversed from GLSL
758 * and the IR.
759 */
760 emit(MAD(this->result, op[2], op[1], op[0]));
761 break;
762
763 case ir_triop_lrp:
764 emit_lrp(this->result, op[0], op[1], op[2]);
765 break;
766
767 case ir_triop_csel:
768 emit(CMP(reg_null_d, op[0], fs_reg(0), BRW_CONDITIONAL_NZ));
769 inst = emit(BRW_OPCODE_SEL, this->result, op[1], op[2]);
770 inst->predicate = BRW_PREDICATE_NORMAL;
771 break;
772 }
773 }
774
775 void
776 fs_visitor::emit_assignment_writes(fs_reg &l, fs_reg &r,
777 const glsl_type *type, bool predicated)
778 {
779 switch (type->base_type) {
780 case GLSL_TYPE_FLOAT:
781 case GLSL_TYPE_UINT:
782 case GLSL_TYPE_INT:
783 case GLSL_TYPE_BOOL:
784 for (unsigned int i = 0; i < type->components(); i++) {
785 l.type = brw_type_for_base_type(type);
786 r.type = brw_type_for_base_type(type);
787
788 if (predicated || !l.equals(r)) {
789 fs_inst *inst = emit(MOV(l, r));
790 inst->predicate = predicated ? BRW_PREDICATE_NORMAL : BRW_PREDICATE_NONE;
791 }
792
793 l.reg_offset++;
794 r.reg_offset++;
795 }
796 break;
797 case GLSL_TYPE_ARRAY:
798 for (unsigned int i = 0; i < type->length; i++) {
799 emit_assignment_writes(l, r, type->fields.array, predicated);
800 }
801 break;
802
803 case GLSL_TYPE_STRUCT:
804 for (unsigned int i = 0; i < type->length; i++) {
805 emit_assignment_writes(l, r, type->fields.structure[i].type,
806 predicated);
807 }
808 break;
809
810 case GLSL_TYPE_SAMPLER:
811 break;
812
813 case GLSL_TYPE_VOID:
814 case GLSL_TYPE_ERROR:
815 case GLSL_TYPE_INTERFACE:
816 assert(!"not reached");
817 break;
818 }
819 }
820
821 /* If the RHS processing resulted in an instruction generating a
822 * temporary value, and it would be easy to rewrite the instruction to
823 * generate its result right into the LHS instead, do so. This ends
824 * up reliably removing instructions where it can be tricky to do so
825 * later without real UD chain information.
826 */
827 bool
828 fs_visitor::try_rewrite_rhs_to_dst(ir_assignment *ir,
829 fs_reg dst,
830 fs_reg src,
831 fs_inst *pre_rhs_inst,
832 fs_inst *last_rhs_inst)
833 {
834 /* Only attempt if we're doing a direct assignment. */
835 if (ir->condition ||
836 !(ir->lhs->type->is_scalar() ||
837 (ir->lhs->type->is_vector() &&
838 ir->write_mask == (1 << ir->lhs->type->vector_elements) - 1)))
839 return false;
840
841 /* Make sure the last instruction generated our source reg. */
842 fs_inst *modify = get_instruction_generating_reg(pre_rhs_inst,
843 last_rhs_inst,
844 src);
845 if (!modify)
846 return false;
847
848 /* If last_rhs_inst wrote a different number of components than our LHS,
849 * we can't safely rewrite it.
850 */
851 if (virtual_grf_sizes[dst.reg] != modify->regs_written)
852 return false;
853
854 /* Success! Rewrite the instruction. */
855 modify->dst = dst;
856
857 return true;
858 }
859
860 void
861 fs_visitor::visit(ir_assignment *ir)
862 {
863 fs_reg l, r;
864 fs_inst *inst;
865
866 /* FINISHME: arrays on the lhs */
867 ir->lhs->accept(this);
868 l = this->result;
869
870 fs_inst *pre_rhs_inst = (fs_inst *) this->instructions.get_tail();
871
872 ir->rhs->accept(this);
873 r = this->result;
874
875 fs_inst *last_rhs_inst = (fs_inst *) this->instructions.get_tail();
876
877 assert(l.file != BAD_FILE);
878 assert(r.file != BAD_FILE);
879
880 if (try_rewrite_rhs_to_dst(ir, l, r, pre_rhs_inst, last_rhs_inst))
881 return;
882
883 if (ir->condition) {
884 emit_bool_to_cond_code(ir->condition);
885 }
886
887 if (ir->lhs->type->is_scalar() ||
888 ir->lhs->type->is_vector()) {
889 for (int i = 0; i < ir->lhs->type->vector_elements; i++) {
890 if (ir->write_mask & (1 << i)) {
891 inst = emit(MOV(l, r));
892 if (ir->condition)
893 inst->predicate = BRW_PREDICATE_NORMAL;
894 r.reg_offset++;
895 }
896 l.reg_offset++;
897 }
898 } else {
899 emit_assignment_writes(l, r, ir->lhs->type, ir->condition != NULL);
900 }
901 }
902
903 fs_inst *
904 fs_visitor::emit_texture_gen4(ir_texture *ir, fs_reg dst, fs_reg coordinate,
905 fs_reg shadow_c, fs_reg lod, fs_reg dPdy)
906 {
907 int mlen;
908 int base_mrf = 1;
909 bool simd16 = false;
910 fs_reg orig_dst;
911
912 /* g0 header. */
913 mlen = 1;
914
915 if (ir->shadow_comparitor) {
916 for (int i = 0; i < ir->coordinate->type->vector_elements; i++) {
917 emit(MOV(fs_reg(MRF, base_mrf + mlen + i), coordinate));
918 coordinate.reg_offset++;
919 }
920
921 /* gen4's SIMD8 sampler always has the slots for u,v,r present.
922 * the unused slots must be zeroed.
923 */
924 for (int i = ir->coordinate->type->vector_elements; i < 3; i++) {
925 emit(MOV(fs_reg(MRF, base_mrf + mlen + i), fs_reg(0.0f)));
926 }
927 mlen += 3;
928
929 if (ir->op == ir_tex) {
930 /* There's no plain shadow compare message, so we use shadow
931 * compare with a bias of 0.0.
932 */
933 emit(MOV(fs_reg(MRF, base_mrf + mlen), fs_reg(0.0f)));
934 mlen++;
935 } else if (ir->op == ir_txb || ir->op == ir_txl) {
936 emit(MOV(fs_reg(MRF, base_mrf + mlen), lod));
937 mlen++;
938 } else {
939 assert(!"Should not get here.");
940 }
941
942 emit(MOV(fs_reg(MRF, base_mrf + mlen), shadow_c));
943 mlen++;
944 } else if (ir->op == ir_tex) {
945 for (int i = 0; i < ir->coordinate->type->vector_elements; i++) {
946 emit(MOV(fs_reg(MRF, base_mrf + mlen + i), coordinate));
947 coordinate.reg_offset++;
948 }
949 /* zero the others. */
950 for (int i = ir->coordinate->type->vector_elements; i<3; i++) {
951 emit(MOV(fs_reg(MRF, base_mrf + mlen + i), fs_reg(0.0f)));
952 }
953 /* gen4's SIMD8 sampler always has the slots for u,v,r present. */
954 mlen += 3;
955 } else if (ir->op == ir_txd) {
956 fs_reg &dPdx = lod;
957
958 for (int i = 0; i < ir->coordinate->type->vector_elements; i++) {
959 emit(MOV(fs_reg(MRF, base_mrf + mlen + i), coordinate));
960 coordinate.reg_offset++;
961 }
962 /* the slots for u and v are always present, but r is optional */
963 mlen += MAX2(ir->coordinate->type->vector_elements, 2);
964
965 /* P = u, v, r
966 * dPdx = dudx, dvdx, drdx
967 * dPdy = dudy, dvdy, drdy
968 *
969 * 1-arg: Does not exist.
970 *
971 * 2-arg: dudx dvdx dudy dvdy
972 * dPdx.x dPdx.y dPdy.x dPdy.y
973 * m4 m5 m6 m7
974 *
975 * 3-arg: dudx dvdx drdx dudy dvdy drdy
976 * dPdx.x dPdx.y dPdx.z dPdy.x dPdy.y dPdy.z
977 * m5 m6 m7 m8 m9 m10
978 */
979 for (int i = 0; i < ir->lod_info.grad.dPdx->type->vector_elements; i++) {
980 emit(MOV(fs_reg(MRF, base_mrf + mlen), dPdx));
981 dPdx.reg_offset++;
982 }
983 mlen += MAX2(ir->lod_info.grad.dPdx->type->vector_elements, 2);
984
985 for (int i = 0; i < ir->lod_info.grad.dPdy->type->vector_elements; i++) {
986 emit(MOV(fs_reg(MRF, base_mrf + mlen), dPdy));
987 dPdy.reg_offset++;
988 }
989 mlen += MAX2(ir->lod_info.grad.dPdy->type->vector_elements, 2);
990 } else if (ir->op == ir_txs) {
991 /* There's no SIMD8 resinfo message on Gen4. Use SIMD16 instead. */
992 simd16 = true;
993 emit(MOV(fs_reg(MRF, base_mrf + mlen, BRW_REGISTER_TYPE_UD), lod));
994 mlen += 2;
995 } else {
996 /* Oh joy. gen4 doesn't have SIMD8 non-shadow-compare bias/lod
997 * instructions. We'll need to do SIMD16 here.
998 */
999 simd16 = true;
1000 assert(ir->op == ir_txb || ir->op == ir_txl || ir->op == ir_txf);
1001
1002 for (int i = 0; i < ir->coordinate->type->vector_elements; i++) {
1003 emit(MOV(fs_reg(MRF, base_mrf + mlen + i * 2, coordinate.type),
1004 coordinate));
1005 coordinate.reg_offset++;
1006 }
1007
1008 /* Initialize the rest of u/v/r with 0.0. Empirically, this seems to
1009 * be necessary for TXF (ld), but seems wise to do for all messages.
1010 */
1011 for (int i = ir->coordinate->type->vector_elements; i < 3; i++) {
1012 emit(MOV(fs_reg(MRF, base_mrf + mlen + i * 2), fs_reg(0.0f)));
1013 }
1014
1015 /* lod/bias appears after u/v/r. */
1016 mlen += 6;
1017
1018 emit(MOV(fs_reg(MRF, base_mrf + mlen, lod.type), lod));
1019 mlen++;
1020
1021 /* The unused upper half. */
1022 mlen++;
1023 }
1024
1025 if (simd16) {
1026 /* Now, since we're doing simd16, the return is 2 interleaved
1027 * vec4s where the odd-indexed ones are junk. We'll need to move
1028 * this weirdness around to the expected layout.
1029 */
1030 orig_dst = dst;
1031 dst = fs_reg(GRF, virtual_grf_alloc(8),
1032 (brw->is_g4x ?
1033 brw_type_for_base_type(ir->type) :
1034 BRW_REGISTER_TYPE_F));
1035 }
1036
1037 fs_inst *inst = NULL;
1038 switch (ir->op) {
1039 case ir_tex:
1040 inst = emit(SHADER_OPCODE_TEX, dst);
1041 break;
1042 case ir_txb:
1043 inst = emit(FS_OPCODE_TXB, dst);
1044 break;
1045 case ir_txl:
1046 inst = emit(SHADER_OPCODE_TXL, dst);
1047 break;
1048 case ir_txd:
1049 inst = emit(SHADER_OPCODE_TXD, dst);
1050 break;
1051 case ir_txs:
1052 inst = emit(SHADER_OPCODE_TXS, dst);
1053 break;
1054 case ir_txf:
1055 inst = emit(SHADER_OPCODE_TXF, dst);
1056 break;
1057 default:
1058 fail("unrecognized texture opcode");
1059 }
1060 inst->base_mrf = base_mrf;
1061 inst->mlen = mlen;
1062 inst->header_present = true;
1063 inst->regs_written = simd16 ? 8 : 4;
1064
1065 if (simd16) {
1066 for (int i = 0; i < 4; i++) {
1067 emit(MOV(orig_dst, dst));
1068 orig_dst.reg_offset++;
1069 dst.reg_offset += 2;
1070 }
1071 }
1072
1073 return inst;
1074 }
1075
1076 /* gen5's sampler has slots for u, v, r, array index, then optional
1077 * parameters like shadow comparitor or LOD bias. If optional
1078 * parameters aren't present, those base slots are optional and don't
1079 * need to be included in the message.
1080 *
1081 * We don't fill in the unnecessary slots regardless, which may look
1082 * surprising in the disassembly.
1083 */
1084 fs_inst *
1085 fs_visitor::emit_texture_gen5(ir_texture *ir, fs_reg dst, fs_reg coordinate,
1086 fs_reg shadow_c, fs_reg lod, fs_reg lod2,
1087 fs_reg sample_index)
1088 {
1089 int mlen = 0;
1090 int base_mrf = 2;
1091 int reg_width = dispatch_width / 8;
1092 bool header_present = false;
1093 const int vector_elements =
1094 ir->coordinate ? ir->coordinate->type->vector_elements : 0;
1095
1096 if (ir->offset != NULL && ir->op == ir_txf) {
1097 /* It appears that the ld instruction used for txf does its
1098 * address bounds check before adding in the offset. To work
1099 * around this, just add the integer offset to the integer texel
1100 * coordinate, and don't put the offset in the header.
1101 */
1102 ir_constant *offset = ir->offset->as_constant();
1103 for (int i = 0; i < vector_elements; i++) {
1104 emit(ADD(fs_reg(MRF, base_mrf + mlen + i * reg_width, coordinate.type),
1105 coordinate,
1106 offset->value.i[i]));
1107 coordinate.reg_offset++;
1108 }
1109 } else {
1110 if (ir->offset) {
1111 /* The offsets set up by the ir_texture visitor are in the
1112 * m1 header, so we can't go headerless.
1113 */
1114 header_present = true;
1115 mlen++;
1116 base_mrf--;
1117 }
1118
1119 for (int i = 0; i < vector_elements; i++) {
1120 emit(MOV(fs_reg(MRF, base_mrf + mlen + i * reg_width, coordinate.type),
1121 coordinate));
1122 coordinate.reg_offset++;
1123 }
1124 }
1125 mlen += vector_elements * reg_width;
1126
1127 if (ir->shadow_comparitor) {
1128 mlen = MAX2(mlen, header_present + 4 * reg_width);
1129
1130 emit(MOV(fs_reg(MRF, base_mrf + mlen), shadow_c));
1131 mlen += reg_width;
1132 }
1133
1134 fs_inst *inst = NULL;
1135 switch (ir->op) {
1136 case ir_tex:
1137 inst = emit(SHADER_OPCODE_TEX, dst);
1138 break;
1139 case ir_txb:
1140 mlen = MAX2(mlen, header_present + 4 * reg_width);
1141 emit(MOV(fs_reg(MRF, base_mrf + mlen), lod));
1142 mlen += reg_width;
1143
1144 inst = emit(FS_OPCODE_TXB, dst);
1145 break;
1146 case ir_txl:
1147 mlen = MAX2(mlen, header_present + 4 * reg_width);
1148 emit(MOV(fs_reg(MRF, base_mrf + mlen), lod));
1149 mlen += reg_width;
1150
1151 inst = emit(SHADER_OPCODE_TXL, dst);
1152 break;
1153 case ir_txd: {
1154 mlen = MAX2(mlen, header_present + 4 * reg_width); /* skip over 'ai' */
1155
1156 /**
1157 * P = u, v, r
1158 * dPdx = dudx, dvdx, drdx
1159 * dPdy = dudy, dvdy, drdy
1160 *
1161 * Load up these values:
1162 * - dudx dudy dvdx dvdy drdx drdy
1163 * - dPdx.x dPdy.x dPdx.y dPdy.y dPdx.z dPdy.z
1164 */
1165 for (int i = 0; i < ir->lod_info.grad.dPdx->type->vector_elements; i++) {
1166 emit(MOV(fs_reg(MRF, base_mrf + mlen), lod));
1167 lod.reg_offset++;
1168 mlen += reg_width;
1169
1170 emit(MOV(fs_reg(MRF, base_mrf + mlen), lod2));
1171 lod2.reg_offset++;
1172 mlen += reg_width;
1173 }
1174
1175 inst = emit(SHADER_OPCODE_TXD, dst);
1176 break;
1177 }
1178 case ir_txs:
1179 emit(MOV(fs_reg(MRF, base_mrf + mlen, BRW_REGISTER_TYPE_UD), lod));
1180 mlen += reg_width;
1181 inst = emit(SHADER_OPCODE_TXS, dst);
1182 break;
1183 case ir_query_levels:
1184 emit(MOV(fs_reg(MRF, base_mrf + mlen, BRW_REGISTER_TYPE_UD), fs_reg(0u)));
1185 mlen += reg_width;
1186 inst = emit(SHADER_OPCODE_TXS, dst);
1187 break;
1188 case ir_txf:
1189 mlen = header_present + 4 * reg_width;
1190 emit(MOV(fs_reg(MRF, base_mrf + mlen - reg_width, BRW_REGISTER_TYPE_UD), lod));
1191 inst = emit(SHADER_OPCODE_TXF, dst);
1192 break;
1193 case ir_txf_ms:
1194 mlen = header_present + 4 * reg_width;
1195
1196 /* lod */
1197 emit(MOV(fs_reg(MRF, base_mrf + mlen - reg_width, BRW_REGISTER_TYPE_UD), fs_reg(0)));
1198 /* sample index */
1199 emit(MOV(fs_reg(MRF, base_mrf + mlen, BRW_REGISTER_TYPE_UD), sample_index));
1200 mlen += reg_width;
1201 inst = emit(SHADER_OPCODE_TXF_MS, dst);
1202 break;
1203 case ir_lod:
1204 inst = emit(SHADER_OPCODE_LOD, dst);
1205 break;
1206 case ir_tg4:
1207 inst = emit(SHADER_OPCODE_TG4, dst);
1208 break;
1209 default:
1210 fail("unrecognized texture opcode");
1211 break;
1212 }
1213 inst->base_mrf = base_mrf;
1214 inst->mlen = mlen;
1215 inst->header_present = header_present;
1216 inst->regs_written = 4;
1217
1218 if (mlen > 11) {
1219 fail("Message length >11 disallowed by hardware\n");
1220 }
1221
1222 return inst;
1223 }
1224
1225 fs_inst *
1226 fs_visitor::emit_texture_gen7(ir_texture *ir, fs_reg dst, fs_reg coordinate,
1227 fs_reg shadow_c, fs_reg lod, fs_reg lod2,
1228 fs_reg sample_index)
1229 {
1230 int reg_width = dispatch_width / 8;
1231 bool header_present = false;
1232 int offsets[3];
1233
1234 fs_reg payload = fs_reg(this, glsl_type::float_type);
1235 fs_reg next = payload;
1236
1237 if (ir->op == ir_tg4 || (ir->offset && ir->op != ir_txf)) {
1238 /* For general texture offsets (no txf workaround), we need a header to
1239 * put them in. Note that for 16-wide we're making space for two actual
1240 * hardware registers here, so the emit will have to fix up for this.
1241 *
1242 * * ir4_tg4 needs to place its channel select in the header,
1243 * for interaction with ARB_texture_swizzle
1244 */
1245 header_present = true;
1246 next.reg_offset++;
1247 }
1248
1249 if (ir->shadow_comparitor) {
1250 emit(MOV(next, shadow_c));
1251 next.reg_offset++;
1252 }
1253
1254 bool has_nonconstant_offset = ir->offset && !ir->offset->as_constant();
1255 bool coordinate_done = false;
1256
1257 /* Set up the LOD info */
1258 switch (ir->op) {
1259 case ir_tex:
1260 case ir_lod:
1261 break;
1262 case ir_txb:
1263 emit(MOV(next, lod));
1264 next.reg_offset++;
1265 break;
1266 case ir_txl:
1267 emit(MOV(next, lod));
1268 next.reg_offset++;
1269 break;
1270 case ir_txd: {
1271 if (dispatch_width == 16)
1272 fail("Gen7 does not support sample_d/sample_d_c in SIMD16 mode.");
1273
1274 /* Load dPdx and the coordinate together:
1275 * [hdr], [ref], x, dPdx.x, dPdy.x, y, dPdx.y, dPdy.y, z, dPdx.z, dPdy.z
1276 */
1277 for (int i = 0; i < ir->coordinate->type->vector_elements; i++) {
1278 emit(MOV(next, coordinate));
1279 coordinate.reg_offset++;
1280 next.reg_offset++;
1281
1282 /* For cube map array, the coordinate is (u,v,r,ai) but there are
1283 * only derivatives for (u, v, r).
1284 */
1285 if (i < ir->lod_info.grad.dPdx->type->vector_elements) {
1286 emit(MOV(next, lod));
1287 lod.reg_offset++;
1288 next.reg_offset++;
1289
1290 emit(MOV(next, lod2));
1291 lod2.reg_offset++;
1292 next.reg_offset++;
1293 }
1294 }
1295
1296 coordinate_done = true;
1297 break;
1298 }
1299 case ir_txs:
1300 emit(MOV(next.retype(BRW_REGISTER_TYPE_UD), lod));
1301 next.reg_offset++;
1302 break;
1303 case ir_query_levels:
1304 emit(MOV(next.retype(BRW_REGISTER_TYPE_UD), fs_reg(0u)));
1305 next.reg_offset++;
1306 break;
1307 case ir_txf:
1308 /* It appears that the ld instruction used for txf does its
1309 * address bounds check before adding in the offset. To work
1310 * around this, just add the integer offset to the integer texel
1311 * coordinate, and don't put the offset in the header.
1312 */
1313 if (ir->offset) {
1314 ir_constant *offset = ir->offset->as_constant();
1315 offsets[0] = offset->value.i[0];
1316 offsets[1] = offset->value.i[1];
1317 offsets[2] = offset->value.i[2];
1318 } else {
1319 memset(offsets, 0, sizeof(offsets));
1320 }
1321
1322 /* Unfortunately, the parameters for LD are intermixed: u, lod, v, r. */
1323 emit(ADD(next.retype(BRW_REGISTER_TYPE_D), coordinate, offsets[0]));
1324 coordinate.reg_offset++;
1325 next.reg_offset++;
1326
1327 emit(MOV(next.retype(BRW_REGISTER_TYPE_D), lod));
1328 next.reg_offset++;
1329
1330 for (int i = 1; i < ir->coordinate->type->vector_elements; i++) {
1331 emit(ADD(next.retype(BRW_REGISTER_TYPE_D), coordinate, offsets[i]));
1332 coordinate.reg_offset++;
1333 next.reg_offset++;
1334 }
1335
1336 coordinate_done = true;
1337 break;
1338 case ir_txf_ms:
1339 emit(MOV(next.retype(BRW_REGISTER_TYPE_UD), sample_index));
1340 next.reg_offset++;
1341
1342 /* constant zero MCS; we arrange to never actually have a compressed
1343 * multisample surface here for now. TODO: issue ld_mcs to get this first,
1344 * if we ever support texturing from compressed multisample surfaces
1345 */
1346 emit(MOV(next.retype(BRW_REGISTER_TYPE_UD), fs_reg(0u)));
1347 next.reg_offset++;
1348
1349 /* there is no offsetting for this message; just copy in the integer
1350 * texture coordinates
1351 */
1352 for (int i = 0; i < ir->coordinate->type->vector_elements; i++) {
1353 emit(MOV(next.retype(BRW_REGISTER_TYPE_D), coordinate));
1354 coordinate.reg_offset++;
1355 next.reg_offset++;
1356 }
1357
1358 coordinate_done = true;
1359 break;
1360 case ir_tg4:
1361 if (has_nonconstant_offset) {
1362 if (ir->shadow_comparitor && dispatch_width == 16)
1363 fail("Gen7 does not support gather4_po_c in SIMD16 mode.");
1364
1365 /* More crazy intermixing */
1366 ir->offset->accept(this);
1367 fs_reg offset_value = this->result;
1368
1369 for (int i = 0; i < 2; i++) { /* u, v */
1370 emit(MOV(next, coordinate));
1371 coordinate.reg_offset++;
1372 next.reg_offset++;
1373 }
1374
1375 for (int i = 0; i < 2; i++) { /* offu, offv */
1376 emit(MOV(next.retype(BRW_REGISTER_TYPE_D), offset_value));
1377 offset_value.reg_offset++;
1378 next.reg_offset++;
1379 }
1380
1381 if (ir->coordinate->type->vector_elements == 3) { /* r if present */
1382 emit(MOV(next, coordinate));
1383 coordinate.reg_offset++;
1384 next.reg_offset++;
1385 }
1386
1387 coordinate_done = true;
1388 }
1389 break;
1390 }
1391
1392 /* Set up the coordinate (except for cases where it was done above) */
1393 if (ir->coordinate && !coordinate_done) {
1394 for (int i = 0; i < ir->coordinate->type->vector_elements; i++) {
1395 emit(MOV(next, coordinate));
1396 coordinate.reg_offset++;
1397 next.reg_offset++;
1398 }
1399 }
1400
1401 /* Generate the SEND */
1402 fs_inst *inst = NULL;
1403 switch (ir->op) {
1404 case ir_tex: inst = emit(SHADER_OPCODE_TEX, dst, payload); break;
1405 case ir_txb: inst = emit(FS_OPCODE_TXB, dst, payload); break;
1406 case ir_txl: inst = emit(SHADER_OPCODE_TXL, dst, payload); break;
1407 case ir_txd: inst = emit(SHADER_OPCODE_TXD, dst, payload); break;
1408 case ir_txf: inst = emit(SHADER_OPCODE_TXF, dst, payload); break;
1409 case ir_txf_ms: inst = emit(SHADER_OPCODE_TXF_MS, dst, payload); break;
1410 case ir_txs: inst = emit(SHADER_OPCODE_TXS, dst, payload); break;
1411 case ir_query_levels: inst = emit(SHADER_OPCODE_TXS, dst, payload); break;
1412 case ir_lod: inst = emit(SHADER_OPCODE_LOD, dst, payload); break;
1413 case ir_tg4:
1414 if (has_nonconstant_offset)
1415 inst = emit(SHADER_OPCODE_TG4_OFFSET, dst, payload);
1416 else
1417 inst = emit(SHADER_OPCODE_TG4, dst, payload);
1418 break;
1419 }
1420 inst->base_mrf = -1;
1421 if (reg_width == 2)
1422 inst->mlen = next.reg_offset * reg_width - header_present;
1423 else
1424 inst->mlen = next.reg_offset * reg_width;
1425 inst->header_present = header_present;
1426 inst->regs_written = 4;
1427
1428 virtual_grf_sizes[payload.reg] = next.reg_offset;
1429 if (inst->mlen > 11) {
1430 fail("Message length >11 disallowed by hardware\n");
1431 }
1432
1433 return inst;
1434 }
1435
1436 fs_reg
1437 fs_visitor::rescale_texcoord(ir_texture *ir, fs_reg coordinate,
1438 bool is_rect, int sampler, int texunit)
1439 {
1440 fs_inst *inst = NULL;
1441 bool needs_gl_clamp = true;
1442 fs_reg scale_x, scale_y;
1443
1444 /* The 965 requires the EU to do the normalization of GL rectangle
1445 * texture coordinates. We use the program parameter state
1446 * tracking to get the scaling factor.
1447 */
1448 if (is_rect &&
1449 (brw->gen < 6 ||
1450 (brw->gen >= 6 && (c->key.tex.gl_clamp_mask[0] & (1 << sampler) ||
1451 c->key.tex.gl_clamp_mask[1] & (1 << sampler))))) {
1452 struct gl_program_parameter_list *params = prog->Parameters;
1453 int tokens[STATE_LENGTH] = {
1454 STATE_INTERNAL,
1455 STATE_TEXRECT_SCALE,
1456 texunit,
1457 0,
1458 0
1459 };
1460
1461 if (dispatch_width == 16) {
1462 fail("rectangle scale uniform setup not supported on 16-wide\n");
1463 return coordinate;
1464 }
1465
1466 scale_x = fs_reg(UNIFORM, c->prog_data.nr_params);
1467 scale_y = fs_reg(UNIFORM, c->prog_data.nr_params + 1);
1468
1469 GLuint index = _mesa_add_state_reference(params,
1470 (gl_state_index *)tokens);
1471 c->prog_data.param[c->prog_data.nr_params++] =
1472 &prog->Parameters->ParameterValues[index][0].f;
1473 c->prog_data.param[c->prog_data.nr_params++] =
1474 &prog->Parameters->ParameterValues[index][1].f;
1475 }
1476
1477 /* The 965 requires the EU to do the normalization of GL rectangle
1478 * texture coordinates. We use the program parameter state
1479 * tracking to get the scaling factor.
1480 */
1481 if (brw->gen < 6 && is_rect) {
1482 fs_reg dst = fs_reg(this, ir->coordinate->type);
1483 fs_reg src = coordinate;
1484 coordinate = dst;
1485
1486 emit(MUL(dst, src, scale_x));
1487 dst.reg_offset++;
1488 src.reg_offset++;
1489 emit(MUL(dst, src, scale_y));
1490 } else if (is_rect) {
1491 /* On gen6+, the sampler handles the rectangle coordinates
1492 * natively, without needing rescaling. But that means we have
1493 * to do GL_CLAMP clamping at the [0, width], [0, height] scale,
1494 * not [0, 1] like the default case below.
1495 */
1496 needs_gl_clamp = false;
1497
1498 for (int i = 0; i < 2; i++) {
1499 if (c->key.tex.gl_clamp_mask[i] & (1 << sampler)) {
1500 fs_reg chan = coordinate;
1501 chan.reg_offset += i;
1502
1503 inst = emit(BRW_OPCODE_SEL, chan, chan, brw_imm_f(0.0));
1504 inst->conditional_mod = BRW_CONDITIONAL_G;
1505
1506 /* Our parameter comes in as 1.0/width or 1.0/height,
1507 * because that's what people normally want for doing
1508 * texture rectangle handling. We need width or height
1509 * for clamping, but we don't care enough to make a new
1510 * parameter type, so just invert back.
1511 */
1512 fs_reg limit = fs_reg(this, glsl_type::float_type);
1513 emit(MOV(limit, i == 0 ? scale_x : scale_y));
1514 emit(SHADER_OPCODE_RCP, limit, limit);
1515
1516 inst = emit(BRW_OPCODE_SEL, chan, chan, limit);
1517 inst->conditional_mod = BRW_CONDITIONAL_L;
1518 }
1519 }
1520 }
1521
1522 if (ir->coordinate && needs_gl_clamp) {
1523 for (unsigned int i = 0;
1524 i < MIN2(ir->coordinate->type->vector_elements, 3); i++) {
1525 if (c->key.tex.gl_clamp_mask[i] & (1 << sampler)) {
1526 fs_reg chan = coordinate;
1527 chan.reg_offset += i;
1528
1529 fs_inst *inst = emit(MOV(chan, chan));
1530 inst->saturate = true;
1531 }
1532 }
1533 }
1534 return coordinate;
1535 }
1536
1537 void
1538 fs_visitor::visit(ir_texture *ir)
1539 {
1540 fs_inst *inst = NULL;
1541
1542 int sampler =
1543 _mesa_get_sampler_uniform_value(ir->sampler, shader_prog, prog);
1544 /* FINISHME: We're failing to recompile our programs when the sampler is
1545 * updated. This only matters for the texture rectangle scale parameters
1546 * (pre-gen6, or gen6+ with GL_CLAMP).
1547 */
1548 int texunit = prog->SamplerUnits[sampler];
1549
1550 if (ir->op == ir_tg4) {
1551 /* When tg4 is used with the degenerate ZERO/ONE swizzles, don't bother
1552 * emitting anything other than setting up the constant result.
1553 */
1554 ir_constant *chan = ir->lod_info.component->as_constant();
1555 int swiz = GET_SWZ(c->key.tex.swizzles[sampler], chan->value.i[0]);
1556 if (swiz == SWIZZLE_ZERO || swiz == SWIZZLE_ONE) {
1557
1558 fs_reg res = fs_reg(this, glsl_type::vec4_type);
1559 this->result = res;
1560
1561 for (int i=0; i<4; i++) {
1562 emit(MOV(res, fs_reg(swiz == SWIZZLE_ZERO ? 0.0f : 1.0f)));
1563 res.reg_offset++;
1564 }
1565 return;
1566 }
1567 }
1568
1569 /* Should be lowered by do_lower_texture_projection */
1570 assert(!ir->projector);
1571
1572 /* Generate code to compute all the subexpression trees. This has to be
1573 * done before loading any values into MRFs for the sampler message since
1574 * generating these values may involve SEND messages that need the MRFs.
1575 */
1576 fs_reg coordinate;
1577 if (ir->coordinate) {
1578 ir->coordinate->accept(this);
1579
1580 coordinate = rescale_texcoord(ir, this->result,
1581 ir->sampler->type->sampler_dimensionality ==
1582 GLSL_SAMPLER_DIM_RECT,
1583 sampler, texunit);
1584 }
1585
1586 fs_reg shadow_comparitor;
1587 if (ir->shadow_comparitor) {
1588 ir->shadow_comparitor->accept(this);
1589 shadow_comparitor = this->result;
1590 }
1591
1592 fs_reg lod, lod2, sample_index;
1593 switch (ir->op) {
1594 case ir_tex:
1595 case ir_lod:
1596 case ir_tg4:
1597 case ir_query_levels:
1598 break;
1599 case ir_txb:
1600 ir->lod_info.bias->accept(this);
1601 lod = this->result;
1602 break;
1603 case ir_txd:
1604 ir->lod_info.grad.dPdx->accept(this);
1605 lod = this->result;
1606
1607 ir->lod_info.grad.dPdy->accept(this);
1608 lod2 = this->result;
1609 break;
1610 case ir_txf:
1611 case ir_txl:
1612 case ir_txs:
1613 ir->lod_info.lod->accept(this);
1614 lod = this->result;
1615 break;
1616 case ir_txf_ms:
1617 ir->lod_info.sample_index->accept(this);
1618 sample_index = this->result;
1619 break;
1620 default:
1621 assert(!"Unrecognized texture opcode");
1622 };
1623
1624 /* Writemasking doesn't eliminate channels on SIMD8 texture
1625 * samples, so don't worry about them.
1626 */
1627 fs_reg dst = fs_reg(this, glsl_type::get_instance(ir->type->base_type, 4, 1));
1628
1629 if (brw->gen >= 7) {
1630 inst = emit_texture_gen7(ir, dst, coordinate, shadow_comparitor,
1631 lod, lod2, sample_index);
1632 } else if (brw->gen >= 5) {
1633 inst = emit_texture_gen5(ir, dst, coordinate, shadow_comparitor,
1634 lod, lod2, sample_index);
1635 } else {
1636 inst = emit_texture_gen4(ir, dst, coordinate, shadow_comparitor,
1637 lod, lod2);
1638 }
1639
1640 if (ir->offset != NULL && ir->op != ir_txf)
1641 inst->texture_offset = brw_texture_offset(ctx, ir->offset->as_constant());
1642
1643 if (ir->op == ir_tg4)
1644 inst->texture_offset |= gather_channel(ir, sampler) << 16; // M0.2:16-17
1645
1646 inst->sampler = sampler;
1647
1648 if (ir->shadow_comparitor)
1649 inst->shadow_compare = true;
1650
1651 /* fixup #layers for cube map arrays */
1652 if (ir->op == ir_txs) {
1653 glsl_type const *type = ir->sampler->type;
1654 if (type->sampler_dimensionality == GLSL_SAMPLER_DIM_CUBE &&
1655 type->sampler_array) {
1656 fs_reg depth = dst;
1657 depth.reg_offset = 2;
1658 emit_math(SHADER_OPCODE_INT_QUOTIENT, depth, depth, fs_reg(6));
1659 }
1660 }
1661
1662 swizzle_result(ir, dst, sampler);
1663 }
1664
1665 /**
1666 * Set up the gather channel based on the swizzle, for gather4.
1667 */
1668 uint32_t
1669 fs_visitor::gather_channel(ir_texture *ir, int sampler)
1670 {
1671 ir_constant *chan = ir->lod_info.component->as_constant();
1672 int swiz = GET_SWZ(c->key.tex.swizzles[sampler], chan->value.i[0]);
1673 switch (swiz) {
1674 case SWIZZLE_X: return 0;
1675 case SWIZZLE_Y:
1676 /* gather4 sampler is broken for green channel on RG32F --
1677 * we must ask for blue instead.
1678 */
1679 if (c->key.tex.gather_channel_quirk_mask & (1<<sampler))
1680 return 2;
1681 return 1;
1682 case SWIZZLE_Z: return 2;
1683 case SWIZZLE_W: return 3;
1684 default:
1685 assert(!"Not reached"); /* zero, one swizzles handled already */
1686 return 0;
1687 }
1688 }
1689
1690 /**
1691 * Swizzle the result of a texture result. This is necessary for
1692 * EXT_texture_swizzle as well as DEPTH_TEXTURE_MODE for shadow comparisons.
1693 */
1694 void
1695 fs_visitor::swizzle_result(ir_texture *ir, fs_reg orig_val, int sampler)
1696 {
1697 if (ir->op == ir_query_levels) {
1698 /* # levels is in .w */
1699 orig_val.reg_offset += 3;
1700 this->result = orig_val;
1701 return;
1702 }
1703
1704 this->result = orig_val;
1705
1706 /* txs,lod don't actually sample the texture, so swizzling the result
1707 * makes no sense.
1708 */
1709 if (ir->op == ir_txs || ir->op == ir_lod || ir->op == ir_tg4)
1710 return;
1711
1712 if (ir->type == glsl_type::float_type) {
1713 /* Ignore DEPTH_TEXTURE_MODE swizzling. */
1714 assert(ir->sampler->type->sampler_shadow);
1715 } else if (c->key.tex.swizzles[sampler] != SWIZZLE_NOOP) {
1716 fs_reg swizzled_result = fs_reg(this, glsl_type::vec4_type);
1717
1718 for (int i = 0; i < 4; i++) {
1719 int swiz = GET_SWZ(c->key.tex.swizzles[sampler], i);
1720 fs_reg l = swizzled_result;
1721 l.reg_offset += i;
1722
1723 if (swiz == SWIZZLE_ZERO) {
1724 emit(MOV(l, fs_reg(0.0f)));
1725 } else if (swiz == SWIZZLE_ONE) {
1726 emit(MOV(l, fs_reg(1.0f)));
1727 } else {
1728 fs_reg r = orig_val;
1729 r.reg_offset += GET_SWZ(c->key.tex.swizzles[sampler], i);
1730 emit(MOV(l, r));
1731 }
1732 }
1733 this->result = swizzled_result;
1734 }
1735 }
1736
1737 void
1738 fs_visitor::visit(ir_swizzle *ir)
1739 {
1740 ir->val->accept(this);
1741 fs_reg val = this->result;
1742
1743 if (ir->type->vector_elements == 1) {
1744 this->result.reg_offset += ir->mask.x;
1745 return;
1746 }
1747
1748 fs_reg result = fs_reg(this, ir->type);
1749 this->result = result;
1750
1751 for (unsigned int i = 0; i < ir->type->vector_elements; i++) {
1752 fs_reg channel = val;
1753 int swiz = 0;
1754
1755 switch (i) {
1756 case 0:
1757 swiz = ir->mask.x;
1758 break;
1759 case 1:
1760 swiz = ir->mask.y;
1761 break;
1762 case 2:
1763 swiz = ir->mask.z;
1764 break;
1765 case 3:
1766 swiz = ir->mask.w;
1767 break;
1768 }
1769
1770 channel.reg_offset += swiz;
1771 emit(MOV(result, channel));
1772 result.reg_offset++;
1773 }
1774 }
1775
1776 void
1777 fs_visitor::visit(ir_discard *ir)
1778 {
1779 assert(ir->condition == NULL); /* FINISHME */
1780
1781 /* We track our discarded pixels in f0.1. By predicating on it, we can
1782 * update just the flag bits that aren't yet discarded. By emitting a
1783 * CMP of g0 != g0, all our currently executing channels will get turned
1784 * off.
1785 */
1786 fs_reg some_reg = fs_reg(retype(brw_vec8_grf(0, 0),
1787 BRW_REGISTER_TYPE_UW));
1788 fs_inst *cmp = emit(CMP(reg_null_f, some_reg, some_reg,
1789 BRW_CONDITIONAL_NZ));
1790 cmp->predicate = BRW_PREDICATE_NORMAL;
1791 cmp->flag_subreg = 1;
1792
1793 if (brw->gen >= 6) {
1794 /* For performance, after a discard, jump to the end of the shader.
1795 * However, many people will do foliage by discarding based on a
1796 * texture's alpha mask, and then continue on to texture with the
1797 * remaining pixels. To avoid trashing the derivatives for those
1798 * texture samples, we'll only jump if all of the pixels in the subspan
1799 * have been discarded.
1800 */
1801 fs_inst *discard_jump = emit(FS_OPCODE_DISCARD_JUMP);
1802 discard_jump->flag_subreg = 1;
1803 discard_jump->predicate = BRW_PREDICATE_ALIGN1_ANY4H;
1804 discard_jump->predicate_inverse = true;
1805 }
1806 }
1807
1808 void
1809 fs_visitor::visit(ir_constant *ir)
1810 {
1811 /* Set this->result to reg at the bottom of the function because some code
1812 * paths will cause this visitor to be applied to other fields. This will
1813 * cause the value stored in this->result to be modified.
1814 *
1815 * Make reg constant so that it doesn't get accidentally modified along the
1816 * way. Yes, I actually had this problem. :(
1817 */
1818 const fs_reg reg(this, ir->type);
1819 fs_reg dst_reg = reg;
1820
1821 if (ir->type->is_array()) {
1822 const unsigned size = type_size(ir->type->fields.array);
1823
1824 for (unsigned i = 0; i < ir->type->length; i++) {
1825 ir->array_elements[i]->accept(this);
1826 fs_reg src_reg = this->result;
1827
1828 dst_reg.type = src_reg.type;
1829 for (unsigned j = 0; j < size; j++) {
1830 emit(MOV(dst_reg, src_reg));
1831 src_reg.reg_offset++;
1832 dst_reg.reg_offset++;
1833 }
1834 }
1835 } else if (ir->type->is_record()) {
1836 foreach_list(node, &ir->components) {
1837 ir_constant *const field = (ir_constant *) node;
1838 const unsigned size = type_size(field->type);
1839
1840 field->accept(this);
1841 fs_reg src_reg = this->result;
1842
1843 dst_reg.type = src_reg.type;
1844 for (unsigned j = 0; j < size; j++) {
1845 emit(MOV(dst_reg, src_reg));
1846 src_reg.reg_offset++;
1847 dst_reg.reg_offset++;
1848 }
1849 }
1850 } else {
1851 const unsigned size = type_size(ir->type);
1852
1853 for (unsigned i = 0; i < size; i++) {
1854 switch (ir->type->base_type) {
1855 case GLSL_TYPE_FLOAT:
1856 emit(MOV(dst_reg, fs_reg(ir->value.f[i])));
1857 break;
1858 case GLSL_TYPE_UINT:
1859 emit(MOV(dst_reg, fs_reg(ir->value.u[i])));
1860 break;
1861 case GLSL_TYPE_INT:
1862 emit(MOV(dst_reg, fs_reg(ir->value.i[i])));
1863 break;
1864 case GLSL_TYPE_BOOL:
1865 emit(MOV(dst_reg, fs_reg((int)ir->value.b[i])));
1866 break;
1867 default:
1868 assert(!"Non-float/uint/int/bool constant");
1869 }
1870 dst_reg.reg_offset++;
1871 }
1872 }
1873
1874 this->result = reg;
1875 }
1876
1877 void
1878 fs_visitor::emit_bool_to_cond_code(ir_rvalue *ir)
1879 {
1880 ir_expression *expr = ir->as_expression();
1881
1882 if (expr &&
1883 expr->operation != ir_binop_logic_and &&
1884 expr->operation != ir_binop_logic_or &&
1885 expr->operation != ir_binop_logic_xor) {
1886 fs_reg op[2];
1887 fs_inst *inst;
1888
1889 assert(expr->get_num_operands() <= 2);
1890 for (unsigned int i = 0; i < expr->get_num_operands(); i++) {
1891 assert(expr->operands[i]->type->is_scalar());
1892
1893 expr->operands[i]->accept(this);
1894 op[i] = this->result;
1895
1896 resolve_ud_negate(&op[i]);
1897 }
1898
1899 switch (expr->operation) {
1900 case ir_unop_logic_not:
1901 inst = emit(AND(reg_null_d, op[0], fs_reg(1)));
1902 inst->conditional_mod = BRW_CONDITIONAL_Z;
1903 break;
1904
1905 case ir_unop_f2b:
1906 if (brw->gen >= 6) {
1907 emit(CMP(reg_null_d, op[0], fs_reg(0.0f), BRW_CONDITIONAL_NZ));
1908 } else {
1909 inst = emit(MOV(reg_null_f, op[0]));
1910 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1911 }
1912 break;
1913
1914 case ir_unop_i2b:
1915 if (brw->gen >= 6) {
1916 emit(CMP(reg_null_d, op[0], fs_reg(0), BRW_CONDITIONAL_NZ));
1917 } else {
1918 inst = emit(MOV(reg_null_d, op[0]));
1919 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1920 }
1921 break;
1922
1923 case ir_binop_greater:
1924 case ir_binop_gequal:
1925 case ir_binop_less:
1926 case ir_binop_lequal:
1927 case ir_binop_equal:
1928 case ir_binop_all_equal:
1929 case ir_binop_nequal:
1930 case ir_binop_any_nequal:
1931 resolve_bool_comparison(expr->operands[0], &op[0]);
1932 resolve_bool_comparison(expr->operands[1], &op[1]);
1933
1934 emit(CMP(reg_null_d, op[0], op[1],
1935 brw_conditional_for_comparison(expr->operation)));
1936 break;
1937
1938 default:
1939 assert(!"not reached");
1940 fail("bad cond code\n");
1941 break;
1942 }
1943 return;
1944 }
1945
1946 ir->accept(this);
1947
1948 fs_inst *inst = emit(AND(reg_null_d, this->result, fs_reg(1)));
1949 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1950 }
1951
1952 /**
1953 * Emit a gen6 IF statement with the comparison folded into the IF
1954 * instruction.
1955 */
1956 void
1957 fs_visitor::emit_if_gen6(ir_if *ir)
1958 {
1959 ir_expression *expr = ir->condition->as_expression();
1960
1961 if (expr) {
1962 fs_reg op[2];
1963 fs_inst *inst;
1964 fs_reg temp;
1965
1966 assert(expr->get_num_operands() <= 2);
1967 for (unsigned int i = 0; i < expr->get_num_operands(); i++) {
1968 assert(expr->operands[i]->type->is_scalar());
1969
1970 expr->operands[i]->accept(this);
1971 op[i] = this->result;
1972 }
1973
1974 switch (expr->operation) {
1975 case ir_unop_logic_not:
1976 case ir_binop_logic_xor:
1977 case ir_binop_logic_or:
1978 case ir_binop_logic_and:
1979 /* For operations on bool arguments, only the low bit of the bool is
1980 * valid, and the others are undefined. Fall back to the condition
1981 * code path.
1982 */
1983 break;
1984
1985 case ir_unop_f2b:
1986 inst = emit(BRW_OPCODE_IF, reg_null_f, op[0], fs_reg(0));
1987 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1988 return;
1989
1990 case ir_unop_i2b:
1991 emit(IF(op[0], fs_reg(0), BRW_CONDITIONAL_NZ));
1992 return;
1993
1994 case ir_binop_greater:
1995 case ir_binop_gequal:
1996 case ir_binop_less:
1997 case ir_binop_lequal:
1998 case ir_binop_equal:
1999 case ir_binop_all_equal:
2000 case ir_binop_nequal:
2001 case ir_binop_any_nequal:
2002 resolve_bool_comparison(expr->operands[0], &op[0]);
2003 resolve_bool_comparison(expr->operands[1], &op[1]);
2004
2005 emit(IF(op[0], op[1],
2006 brw_conditional_for_comparison(expr->operation)));
2007 return;
2008 default:
2009 assert(!"not reached");
2010 emit(IF(op[0], fs_reg(0), BRW_CONDITIONAL_NZ));
2011 fail("bad condition\n");
2012 return;
2013 }
2014 }
2015
2016 emit_bool_to_cond_code(ir->condition);
2017 fs_inst *inst = emit(BRW_OPCODE_IF);
2018 inst->predicate = BRW_PREDICATE_NORMAL;
2019 }
2020
2021 /**
2022 * Try to replace IF/MOV/ELSE/MOV/ENDIF with SEL.
2023 *
2024 * Many GLSL shaders contain the following pattern:
2025 *
2026 * x = condition ? foo : bar
2027 *
2028 * The compiler emits an ir_if tree for this, since each subexpression might be
2029 * a complex tree that could have side-effects or short-circuit logic.
2030 *
2031 * However, the common case is to simply select one of two constants or
2032 * variable values---which is exactly what SEL is for. In this case, the
2033 * assembly looks like:
2034 *
2035 * (+f0) IF
2036 * MOV dst src0
2037 * ELSE
2038 * MOV dst src1
2039 * ENDIF
2040 *
2041 * which can be easily translated into:
2042 *
2043 * (+f0) SEL dst src0 src1
2044 *
2045 * If src0 is an immediate value, we promote it to a temporary GRF.
2046 */
2047 void
2048 fs_visitor::try_replace_with_sel()
2049 {
2050 fs_inst *endif_inst = (fs_inst *) instructions.get_tail();
2051 assert(endif_inst->opcode == BRW_OPCODE_ENDIF);
2052
2053 /* Pattern match in reverse: IF, MOV, ELSE, MOV, ENDIF. */
2054 int opcodes[] = {
2055 BRW_OPCODE_IF, BRW_OPCODE_MOV, BRW_OPCODE_ELSE, BRW_OPCODE_MOV,
2056 };
2057
2058 fs_inst *match = (fs_inst *) endif_inst->prev;
2059 for (int i = 0; i < 4; i++) {
2060 if (match->is_head_sentinel() || match->opcode != opcodes[4-i-1])
2061 return;
2062 match = (fs_inst *) match->prev;
2063 }
2064
2065 /* The opcodes match; it looks like the right sequence of instructions. */
2066 fs_inst *else_mov = (fs_inst *) endif_inst->prev;
2067 fs_inst *then_mov = (fs_inst *) else_mov->prev->prev;
2068 fs_inst *if_inst = (fs_inst *) then_mov->prev;
2069
2070 /* Check that the MOVs are the right form. */
2071 if (then_mov->dst.equals(else_mov->dst) &&
2072 !then_mov->is_partial_write() &&
2073 !else_mov->is_partial_write()) {
2074
2075 /* Remove the matched instructions; we'll emit a SEL to replace them. */
2076 while (!if_inst->next->is_tail_sentinel())
2077 if_inst->next->remove();
2078 if_inst->remove();
2079
2080 /* Only the last source register can be a constant, so if the MOV in
2081 * the "then" clause uses a constant, we need to put it in a temporary.
2082 */
2083 fs_reg src0(then_mov->src[0]);
2084 if (src0.file == IMM) {
2085 src0 = fs_reg(this, glsl_type::float_type);
2086 src0.type = then_mov->src[0].type;
2087 emit(MOV(src0, then_mov->src[0]));
2088 }
2089
2090 fs_inst *sel;
2091 if (if_inst->conditional_mod) {
2092 /* Sandybridge-specific IF with embedded comparison */
2093 emit(CMP(reg_null_d, if_inst->src[0], if_inst->src[1],
2094 if_inst->conditional_mod));
2095 sel = emit(BRW_OPCODE_SEL, then_mov->dst, src0, else_mov->src[0]);
2096 sel->predicate = BRW_PREDICATE_NORMAL;
2097 } else {
2098 /* Separate CMP and IF instructions */
2099 sel = emit(BRW_OPCODE_SEL, then_mov->dst, src0, else_mov->src[0]);
2100 sel->predicate = if_inst->predicate;
2101 sel->predicate_inverse = if_inst->predicate_inverse;
2102 }
2103 }
2104 }
2105
2106 void
2107 fs_visitor::visit(ir_if *ir)
2108 {
2109 if (brw->gen < 6 && dispatch_width == 16) {
2110 fail("Can't support (non-uniform) control flow on 16-wide\n");
2111 }
2112
2113 /* Don't point the annotation at the if statement, because then it plus
2114 * the then and else blocks get printed.
2115 */
2116 this->base_ir = ir->condition;
2117
2118 if (brw->gen == 6) {
2119 emit_if_gen6(ir);
2120 } else {
2121 emit_bool_to_cond_code(ir->condition);
2122
2123 emit(IF(BRW_PREDICATE_NORMAL));
2124 }
2125
2126 foreach_list(node, &ir->then_instructions) {
2127 ir_instruction *ir = (ir_instruction *)node;
2128 this->base_ir = ir;
2129
2130 ir->accept(this);
2131 }
2132
2133 if (!ir->else_instructions.is_empty()) {
2134 emit(BRW_OPCODE_ELSE);
2135
2136 foreach_list(node, &ir->else_instructions) {
2137 ir_instruction *ir = (ir_instruction *)node;
2138 this->base_ir = ir;
2139
2140 ir->accept(this);
2141 }
2142 }
2143
2144 emit(BRW_OPCODE_ENDIF);
2145
2146 try_replace_with_sel();
2147 }
2148
2149 void
2150 fs_visitor::visit(ir_loop *ir)
2151 {
2152 fs_reg counter = reg_undef;
2153
2154 if (brw->gen < 6 && dispatch_width == 16) {
2155 fail("Can't support (non-uniform) control flow on 16-wide\n");
2156 }
2157
2158 if (ir->counter) {
2159 this->base_ir = ir->counter;
2160 ir->counter->accept(this);
2161 counter = *(variable_storage(ir->counter));
2162
2163 if (ir->from) {
2164 this->base_ir = ir->from;
2165 ir->from->accept(this);
2166
2167 emit(MOV(counter, this->result));
2168 }
2169 }
2170
2171 this->base_ir = NULL;
2172 emit(BRW_OPCODE_DO);
2173
2174 if (ir->to) {
2175 this->base_ir = ir->to;
2176 ir->to->accept(this);
2177
2178 emit(CMP(reg_null_d, counter, this->result,
2179 brw_conditional_for_comparison(ir->cmp)));
2180
2181 fs_inst *inst = emit(BRW_OPCODE_BREAK);
2182 inst->predicate = BRW_PREDICATE_NORMAL;
2183 }
2184
2185 foreach_list(node, &ir->body_instructions) {
2186 ir_instruction *ir = (ir_instruction *)node;
2187
2188 this->base_ir = ir;
2189 ir->accept(this);
2190 }
2191
2192 if (ir->increment) {
2193 this->base_ir = ir->increment;
2194 ir->increment->accept(this);
2195 emit(ADD(counter, counter, this->result));
2196 }
2197
2198 this->base_ir = NULL;
2199 emit(BRW_OPCODE_WHILE);
2200 }
2201
2202 void
2203 fs_visitor::visit(ir_loop_jump *ir)
2204 {
2205 switch (ir->mode) {
2206 case ir_loop_jump::jump_break:
2207 emit(BRW_OPCODE_BREAK);
2208 break;
2209 case ir_loop_jump::jump_continue:
2210 emit(BRW_OPCODE_CONTINUE);
2211 break;
2212 }
2213 }
2214
2215 void
2216 fs_visitor::visit(ir_call *ir)
2217 {
2218 assert(!"FINISHME");
2219 }
2220
2221 void
2222 fs_visitor::visit(ir_return *ir)
2223 {
2224 assert(!"FINISHME");
2225 }
2226
2227 void
2228 fs_visitor::visit(ir_function *ir)
2229 {
2230 /* Ignore function bodies other than main() -- we shouldn't see calls to
2231 * them since they should all be inlined before we get to ir_to_mesa.
2232 */
2233 if (strcmp(ir->name, "main") == 0) {
2234 const ir_function_signature *sig;
2235 exec_list empty;
2236
2237 sig = ir->matching_signature(NULL, &empty);
2238
2239 assert(sig);
2240
2241 foreach_list(node, &sig->body) {
2242 ir_instruction *ir = (ir_instruction *)node;
2243 this->base_ir = ir;
2244
2245 ir->accept(this);
2246 }
2247 }
2248 }
2249
2250 void
2251 fs_visitor::visit(ir_function_signature *ir)
2252 {
2253 assert(!"not reached");
2254 (void)ir;
2255 }
2256
2257 void
2258 fs_visitor::visit(ir_emit_vertex *)
2259 {
2260 assert(!"not reached");
2261 }
2262
2263 void
2264 fs_visitor::visit(ir_end_primitive *)
2265 {
2266 assert(!"not reached");
2267 }
2268
2269 fs_inst *
2270 fs_visitor::emit(fs_inst inst)
2271 {
2272 fs_inst *list_inst = new(mem_ctx) fs_inst;
2273 *list_inst = inst;
2274 emit(list_inst);
2275 return list_inst;
2276 }
2277
2278 fs_inst *
2279 fs_visitor::emit(fs_inst *inst)
2280 {
2281 if (force_uncompressed_stack > 0)
2282 inst->force_uncompressed = true;
2283 else if (force_sechalf_stack > 0)
2284 inst->force_sechalf = true;
2285
2286 inst->annotation = this->current_annotation;
2287 inst->ir = this->base_ir;
2288
2289 this->instructions.push_tail(inst);
2290
2291 return inst;
2292 }
2293
2294 void
2295 fs_visitor::emit(exec_list list)
2296 {
2297 foreach_list_safe(node, &list) {
2298 fs_inst *inst = (fs_inst *)node;
2299 inst->remove();
2300 emit(inst);
2301 }
2302 }
2303
2304 /** Emits a dummy fragment shader consisting of magenta for bringup purposes. */
2305 void
2306 fs_visitor::emit_dummy_fs()
2307 {
2308 int reg_width = dispatch_width / 8;
2309
2310 /* Everyone's favorite color. */
2311 emit(MOV(fs_reg(MRF, 2 + 0 * reg_width), fs_reg(1.0f)));
2312 emit(MOV(fs_reg(MRF, 2 + 1 * reg_width), fs_reg(0.0f)));
2313 emit(MOV(fs_reg(MRF, 2 + 2 * reg_width), fs_reg(1.0f)));
2314 emit(MOV(fs_reg(MRF, 2 + 3 * reg_width), fs_reg(0.0f)));
2315
2316 fs_inst *write;
2317 write = emit(FS_OPCODE_FB_WRITE, fs_reg(0), fs_reg(0));
2318 write->base_mrf = 2;
2319 write->mlen = 4 * reg_width;
2320 write->eot = true;
2321 }
2322
2323 /* The register location here is relative to the start of the URB
2324 * data. It will get adjusted to be a real location before
2325 * generate_code() time.
2326 */
2327 struct brw_reg
2328 fs_visitor::interp_reg(int location, int channel)
2329 {
2330 int regnr = c->prog_data.urb_setup[location] * 2 + channel / 2;
2331 int stride = (channel & 1) * 4;
2332
2333 assert(c->prog_data.urb_setup[location] != -1);
2334
2335 return brw_vec1_grf(regnr, stride);
2336 }
2337
2338 /** Emits the interpolation for the varying inputs. */
2339 void
2340 fs_visitor::emit_interpolation_setup_gen4()
2341 {
2342 this->current_annotation = "compute pixel centers";
2343 this->pixel_x = fs_reg(this, glsl_type::uint_type);
2344 this->pixel_y = fs_reg(this, glsl_type::uint_type);
2345 this->pixel_x.type = BRW_REGISTER_TYPE_UW;
2346 this->pixel_y.type = BRW_REGISTER_TYPE_UW;
2347
2348 emit(FS_OPCODE_PIXEL_X, this->pixel_x);
2349 emit(FS_OPCODE_PIXEL_Y, this->pixel_y);
2350
2351 this->current_annotation = "compute pixel deltas from v0";
2352 if (brw->has_pln) {
2353 this->delta_x[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC] =
2354 fs_reg(this, glsl_type::vec2_type);
2355 this->delta_y[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC] =
2356 this->delta_x[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC];
2357 this->delta_y[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC].reg_offset++;
2358 } else {
2359 this->delta_x[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC] =
2360 fs_reg(this, glsl_type::float_type);
2361 this->delta_y[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC] =
2362 fs_reg(this, glsl_type::float_type);
2363 }
2364 emit(ADD(this->delta_x[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC],
2365 this->pixel_x, fs_reg(negate(brw_vec1_grf(1, 0)))));
2366 emit(ADD(this->delta_y[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC],
2367 this->pixel_y, fs_reg(negate(brw_vec1_grf(1, 1)))));
2368
2369 this->current_annotation = "compute pos.w and 1/pos.w";
2370 /* Compute wpos.w. It's always in our setup, since it's needed to
2371 * interpolate the other attributes.
2372 */
2373 this->wpos_w = fs_reg(this, glsl_type::float_type);
2374 emit(FS_OPCODE_LINTERP, wpos_w,
2375 this->delta_x[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC],
2376 this->delta_y[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC],
2377 interp_reg(VARYING_SLOT_POS, 3));
2378 /* Compute the pixel 1/W value from wpos.w. */
2379 this->pixel_w = fs_reg(this, glsl_type::float_type);
2380 emit_math(SHADER_OPCODE_RCP, this->pixel_w, wpos_w);
2381 this->current_annotation = NULL;
2382 }
2383
2384 /** Emits the interpolation for the varying inputs. */
2385 void
2386 fs_visitor::emit_interpolation_setup_gen6()
2387 {
2388 struct brw_reg g1_uw = retype(brw_vec1_grf(1, 0), BRW_REGISTER_TYPE_UW);
2389
2390 /* If the pixel centers end up used, the setup is the same as for gen4. */
2391 this->current_annotation = "compute pixel centers";
2392 fs_reg int_pixel_x = fs_reg(this, glsl_type::uint_type);
2393 fs_reg int_pixel_y = fs_reg(this, glsl_type::uint_type);
2394 int_pixel_x.type = BRW_REGISTER_TYPE_UW;
2395 int_pixel_y.type = BRW_REGISTER_TYPE_UW;
2396 emit(ADD(int_pixel_x,
2397 fs_reg(stride(suboffset(g1_uw, 4), 2, 4, 0)),
2398 fs_reg(brw_imm_v(0x10101010))));
2399 emit(ADD(int_pixel_y,
2400 fs_reg(stride(suboffset(g1_uw, 5), 2, 4, 0)),
2401 fs_reg(brw_imm_v(0x11001100))));
2402
2403 /* As of gen6, we can no longer mix float and int sources. We have
2404 * to turn the integer pixel centers into floats for their actual
2405 * use.
2406 */
2407 this->pixel_x = fs_reg(this, glsl_type::float_type);
2408 this->pixel_y = fs_reg(this, glsl_type::float_type);
2409 emit(MOV(this->pixel_x, int_pixel_x));
2410 emit(MOV(this->pixel_y, int_pixel_y));
2411
2412 this->current_annotation = "compute pos.w";
2413 this->pixel_w = fs_reg(brw_vec8_grf(c->source_w_reg, 0));
2414 this->wpos_w = fs_reg(this, glsl_type::float_type);
2415 emit_math(SHADER_OPCODE_RCP, this->wpos_w, this->pixel_w);
2416
2417 for (int i = 0; i < BRW_WM_BARYCENTRIC_INTERP_MODE_COUNT; ++i) {
2418 uint8_t reg = c->barycentric_coord_reg[i];
2419 this->delta_x[i] = fs_reg(brw_vec8_grf(reg, 0));
2420 this->delta_y[i] = fs_reg(brw_vec8_grf(reg + 1, 0));
2421 }
2422
2423 this->current_annotation = NULL;
2424 }
2425
2426 void
2427 fs_visitor::emit_color_write(int target, int index, int first_color_mrf)
2428 {
2429 int reg_width = dispatch_width / 8;
2430 fs_inst *inst;
2431 fs_reg color = outputs[target];
2432 fs_reg mrf;
2433
2434 /* If there's no color data to be written, skip it. */
2435 if (color.file == BAD_FILE)
2436 return;
2437
2438 color.reg_offset += index;
2439
2440 if (dispatch_width == 8 || brw->gen >= 6) {
2441 /* SIMD8 write looks like:
2442 * m + 0: r0
2443 * m + 1: r1
2444 * m + 2: g0
2445 * m + 3: g1
2446 *
2447 * gen6 SIMD16 DP write looks like:
2448 * m + 0: r0
2449 * m + 1: r1
2450 * m + 2: g0
2451 * m + 3: g1
2452 * m + 4: b0
2453 * m + 5: b1
2454 * m + 6: a0
2455 * m + 7: a1
2456 */
2457 inst = emit(MOV(fs_reg(MRF, first_color_mrf + index * reg_width,
2458 color.type),
2459 color));
2460 inst->saturate = c->key.clamp_fragment_color;
2461 } else {
2462 /* pre-gen6 SIMD16 single source DP write looks like:
2463 * m + 0: r0
2464 * m + 1: g0
2465 * m + 2: b0
2466 * m + 3: a0
2467 * m + 4: r1
2468 * m + 5: g1
2469 * m + 6: b1
2470 * m + 7: a1
2471 */
2472 if (brw->has_compr4) {
2473 /* By setting the high bit of the MRF register number, we
2474 * indicate that we want COMPR4 mode - instead of doing the
2475 * usual destination + 1 for the second half we get
2476 * destination + 4.
2477 */
2478 inst = emit(MOV(fs_reg(MRF, BRW_MRF_COMPR4 + first_color_mrf + index,
2479 color.type),
2480 color));
2481 inst->saturate = c->key.clamp_fragment_color;
2482 } else {
2483 push_force_uncompressed();
2484 inst = emit(MOV(fs_reg(MRF, first_color_mrf + index, color.type),
2485 color));
2486 inst->saturate = c->key.clamp_fragment_color;
2487 pop_force_uncompressed();
2488
2489 push_force_sechalf();
2490 color.sechalf = true;
2491 inst = emit(MOV(fs_reg(MRF, first_color_mrf + index + 4, color.type),
2492 color));
2493 inst->saturate = c->key.clamp_fragment_color;
2494 pop_force_sechalf();
2495 color.sechalf = false;
2496 }
2497 }
2498 }
2499
2500 void
2501 fs_visitor::emit_fb_writes()
2502 {
2503 this->current_annotation = "FB write header";
2504 bool header_present = true;
2505 /* We can potentially have a message length of up to 15, so we have to set
2506 * base_mrf to either 0 or 1 in order to fit in m0..m15.
2507 */
2508 int base_mrf = 1;
2509 int nr = base_mrf;
2510 int reg_width = dispatch_width / 8;
2511 bool do_dual_src = this->dual_src_output.file != BAD_FILE;
2512 bool src0_alpha_to_render_target = false;
2513
2514 if (dispatch_width == 16 && do_dual_src) {
2515 fail("GL_ARB_blend_func_extended not yet supported in 16-wide.");
2516 do_dual_src = false;
2517 }
2518
2519 /* From the Sandy Bridge PRM, volume 4, page 198:
2520 *
2521 * "Dispatched Pixel Enables. One bit per pixel indicating
2522 * which pixels were originally enabled when the thread was
2523 * dispatched. This field is only required for the end-of-
2524 * thread message and on all dual-source messages."
2525 */
2526 if (brw->gen >= 6 &&
2527 !this->fp->UsesKill &&
2528 !do_dual_src &&
2529 c->key.nr_color_regions == 1) {
2530 header_present = false;
2531 }
2532
2533 if (header_present) {
2534 src0_alpha_to_render_target = brw->gen >= 6 &&
2535 !do_dual_src &&
2536 c->key.replicate_alpha;
2537 /* m2, m3 header */
2538 nr += 2;
2539 }
2540
2541 if (c->aa_dest_stencil_reg) {
2542 push_force_uncompressed();
2543 emit(MOV(fs_reg(MRF, nr++),
2544 fs_reg(brw_vec8_grf(c->aa_dest_stencil_reg, 0))));
2545 pop_force_uncompressed();
2546 }
2547
2548 /* Reserve space for color. It'll be filled in per MRT below. */
2549 int color_mrf = nr;
2550 nr += 4 * reg_width;
2551 if (do_dual_src)
2552 nr += 4;
2553 if (src0_alpha_to_render_target)
2554 nr += reg_width;
2555
2556 if (c->source_depth_to_render_target) {
2557 if (brw->gen == 6 && dispatch_width == 16) {
2558 /* For outputting oDepth on gen6, SIMD8 writes have to be
2559 * used. This would require 8-wide moves of each half to
2560 * message regs, kind of like pre-gen5 SIMD16 FB writes.
2561 * Just bail on doing so for now.
2562 */
2563 fail("Missing support for simd16 depth writes on gen6\n");
2564 }
2565
2566 if (prog->OutputsWritten & BITFIELD64_BIT(FRAG_RESULT_DEPTH)) {
2567 /* Hand over gl_FragDepth. */
2568 assert(this->frag_depth.file != BAD_FILE);
2569 emit(MOV(fs_reg(MRF, nr), this->frag_depth));
2570 } else {
2571 /* Pass through the payload depth. */
2572 emit(MOV(fs_reg(MRF, nr),
2573 fs_reg(brw_vec8_grf(c->source_depth_reg, 0))));
2574 }
2575 nr += reg_width;
2576 }
2577
2578 if (c->dest_depth_reg) {
2579 emit(MOV(fs_reg(MRF, nr),
2580 fs_reg(brw_vec8_grf(c->dest_depth_reg, 0))));
2581 nr += reg_width;
2582 }
2583
2584 if (do_dual_src) {
2585 fs_reg src0 = this->outputs[0];
2586 fs_reg src1 = this->dual_src_output;
2587
2588 this->current_annotation = ralloc_asprintf(this->mem_ctx,
2589 "FB write src0");
2590 for (int i = 0; i < 4; i++) {
2591 fs_inst *inst = emit(MOV(fs_reg(MRF, color_mrf + i, src0.type), src0));
2592 src0.reg_offset++;
2593 inst->saturate = c->key.clamp_fragment_color;
2594 }
2595
2596 this->current_annotation = ralloc_asprintf(this->mem_ctx,
2597 "FB write src1");
2598 for (int i = 0; i < 4; i++) {
2599 fs_inst *inst = emit(MOV(fs_reg(MRF, color_mrf + 4 + i, src1.type),
2600 src1));
2601 src1.reg_offset++;
2602 inst->saturate = c->key.clamp_fragment_color;
2603 }
2604
2605 if (INTEL_DEBUG & DEBUG_SHADER_TIME)
2606 emit_shader_time_end();
2607
2608 fs_inst *inst = emit(FS_OPCODE_FB_WRITE);
2609 inst->target = 0;
2610 inst->base_mrf = base_mrf;
2611 inst->mlen = nr - base_mrf;
2612 inst->eot = true;
2613 inst->header_present = header_present;
2614
2615 c->prog_data.dual_src_blend = true;
2616 this->current_annotation = NULL;
2617 return;
2618 }
2619
2620 for (int target = 0; target < c->key.nr_color_regions; target++) {
2621 this->current_annotation = ralloc_asprintf(this->mem_ctx,
2622 "FB write target %d",
2623 target);
2624 /* If src0_alpha_to_render_target is true, include source zero alpha
2625 * data in RenderTargetWrite message for targets > 0.
2626 */
2627 int write_color_mrf = color_mrf;
2628 if (src0_alpha_to_render_target && target != 0) {
2629 fs_inst *inst;
2630 fs_reg color = outputs[0];
2631 color.reg_offset += 3;
2632
2633 inst = emit(MOV(fs_reg(MRF, write_color_mrf, color.type),
2634 color));
2635 inst->saturate = c->key.clamp_fragment_color;
2636 write_color_mrf = color_mrf + reg_width;
2637 }
2638
2639 for (unsigned i = 0; i < this->output_components[target]; i++)
2640 emit_color_write(target, i, write_color_mrf);
2641
2642 bool eot = false;
2643 if (target == c->key.nr_color_regions - 1) {
2644 eot = true;
2645
2646 if (INTEL_DEBUG & DEBUG_SHADER_TIME)
2647 emit_shader_time_end();
2648 }
2649
2650 fs_inst *inst = emit(FS_OPCODE_FB_WRITE);
2651 inst->target = target;
2652 inst->base_mrf = base_mrf;
2653 if (src0_alpha_to_render_target && target == 0)
2654 inst->mlen = nr - base_mrf - reg_width;
2655 else
2656 inst->mlen = nr - base_mrf;
2657 inst->eot = eot;
2658 inst->header_present = header_present;
2659 }
2660
2661 if (c->key.nr_color_regions == 0) {
2662 /* Even if there's no color buffers enabled, we still need to send
2663 * alpha out the pipeline to our null renderbuffer to support
2664 * alpha-testing, alpha-to-coverage, and so on.
2665 */
2666 emit_color_write(0, 3, color_mrf);
2667
2668 if (INTEL_DEBUG & DEBUG_SHADER_TIME)
2669 emit_shader_time_end();
2670
2671 fs_inst *inst = emit(FS_OPCODE_FB_WRITE);
2672 inst->base_mrf = base_mrf;
2673 inst->mlen = nr - base_mrf;
2674 inst->eot = true;
2675 inst->header_present = header_present;
2676 }
2677
2678 this->current_annotation = NULL;
2679 }
2680
2681 void
2682 fs_visitor::resolve_ud_negate(fs_reg *reg)
2683 {
2684 if (reg->type != BRW_REGISTER_TYPE_UD ||
2685 !reg->negate)
2686 return;
2687
2688 fs_reg temp = fs_reg(this, glsl_type::uint_type);
2689 emit(MOV(temp, *reg));
2690 *reg = temp;
2691 }
2692
2693 void
2694 fs_visitor::resolve_bool_comparison(ir_rvalue *rvalue, fs_reg *reg)
2695 {
2696 if (rvalue->type != glsl_type::bool_type)
2697 return;
2698
2699 fs_reg temp = fs_reg(this, glsl_type::bool_type);
2700 emit(AND(temp, *reg, fs_reg(1)));
2701 *reg = temp;
2702 }
2703
2704 fs_visitor::fs_visitor(struct brw_context *brw,
2705 struct brw_wm_compile *c,
2706 struct gl_shader_program *shader_prog,
2707 struct gl_fragment_program *fp,
2708 unsigned dispatch_width)
2709 : dispatch_width(dispatch_width)
2710 {
2711 this->c = c;
2712 this->brw = brw;
2713 this->fp = fp;
2714 this->prog = &fp->Base;
2715 this->shader_prog = shader_prog;
2716 this->prog = &fp->Base;
2717 this->stage_prog_data = &c->prog_data.base;
2718 this->ctx = &brw->ctx;
2719 this->mem_ctx = ralloc_context(NULL);
2720 if (shader_prog)
2721 shader = (struct brw_shader *)
2722 shader_prog->_LinkedShaders[MESA_SHADER_FRAGMENT];
2723 else
2724 shader = NULL;
2725 this->failed = false;
2726 this->variable_ht = hash_table_ctor(0,
2727 hash_table_pointer_hash,
2728 hash_table_pointer_compare);
2729
2730 memset(this->outputs, 0, sizeof(this->outputs));
2731 memset(this->output_components, 0, sizeof(this->output_components));
2732 this->first_non_payload_grf = 0;
2733 this->max_grf = brw->gen >= 7 ? GEN7_MRF_HACK_START : BRW_MAX_GRF;
2734
2735 this->current_annotation = NULL;
2736 this->base_ir = NULL;
2737
2738 this->virtual_grf_sizes = NULL;
2739 this->virtual_grf_count = 0;
2740 this->virtual_grf_array_size = 0;
2741 this->virtual_grf_start = NULL;
2742 this->virtual_grf_end = NULL;
2743 this->live_intervals = NULL;
2744
2745 this->params_remap = NULL;
2746 this->nr_params_remap = 0;
2747
2748 this->force_uncompressed_stack = 0;
2749 this->force_sechalf_stack = 0;
2750
2751 memset(&this->param_size, 0, sizeof(this->param_size));
2752 }
2753
2754 fs_visitor::~fs_visitor()
2755 {
2756 ralloc_free(this->mem_ctx);
2757 hash_table_dtor(this->variable_ht);
2758 }