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