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