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