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