i965/fs: Add support for non-shadow textureGrad (TXD) on gen5/6.
[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_shader.h"
48 #include "brw_fs.h"
49 #include "../glsl/glsl_types.h"
50 #include "../glsl/ir_optimization.h"
51 #include "../glsl/ir_print_visitor.h"
52
53 void
54 fs_visitor::visit(ir_variable *ir)
55 {
56 fs_reg *reg = NULL;
57
58 if (variable_storage(ir))
59 return;
60
61 if (strcmp(ir->name, "gl_FragColor") == 0) {
62 this->frag_color = ir;
63 } else if (strcmp(ir->name, "gl_FragData") == 0) {
64 this->frag_data = ir;
65 } else if (strcmp(ir->name, "gl_FragDepth") == 0) {
66 this->frag_depth = ir;
67 }
68
69 if (ir->mode == ir_var_in) {
70 if (!strcmp(ir->name, "gl_FragCoord")) {
71 reg = emit_fragcoord_interpolation(ir);
72 } else if (!strcmp(ir->name, "gl_FrontFacing")) {
73 reg = emit_frontfacing_interpolation(ir);
74 } else {
75 reg = emit_general_interpolation(ir);
76 }
77 assert(reg);
78 hash_table_insert(this->variable_ht, reg, ir);
79 return;
80 }
81
82 if (ir->mode == ir_var_uniform) {
83 int param_index = c->prog_data.nr_params;
84
85 if (c->dispatch_width == 16) {
86 if (!variable_storage(ir)) {
87 fail("Failed to find uniform '%s' in 16-wide\n", ir->name);
88 }
89 return;
90 }
91
92 if (!strncmp(ir->name, "gl_", 3)) {
93 setup_builtin_uniform_values(ir);
94 } else {
95 setup_uniform_values(ir->location, ir->type);
96 }
97
98 reg = new(this->mem_ctx) fs_reg(UNIFORM, param_index);
99 reg->type = brw_type_for_base_type(ir->type);
100 }
101
102 if (!reg)
103 reg = new(this->mem_ctx) fs_reg(this, ir->type);
104
105 hash_table_insert(this->variable_ht, reg, ir);
106 }
107
108 void
109 fs_visitor::visit(ir_dereference_variable *ir)
110 {
111 fs_reg *reg = variable_storage(ir->var);
112 this->result = *reg;
113 }
114
115 void
116 fs_visitor::visit(ir_dereference_record *ir)
117 {
118 const glsl_type *struct_type = ir->record->type;
119
120 ir->record->accept(this);
121
122 unsigned int offset = 0;
123 for (unsigned int i = 0; i < struct_type->length; i++) {
124 if (strcmp(struct_type->fields.structure[i].name, ir->field) == 0)
125 break;
126 offset += type_size(struct_type->fields.structure[i].type);
127 }
128 this->result.reg_offset += offset;
129 this->result.type = brw_type_for_base_type(ir->type);
130 }
131
132 void
133 fs_visitor::visit(ir_dereference_array *ir)
134 {
135 ir_constant *index;
136 int element_size;
137
138 ir->array->accept(this);
139 index = ir->array_index->as_constant();
140
141 element_size = type_size(ir->type);
142 this->result.type = brw_type_for_base_type(ir->type);
143
144 if (index) {
145 assert(this->result.file == UNIFORM ||
146 (this->result.file == GRF &&
147 this->result.reg != 0));
148 this->result.reg_offset += index->value.i[0] * element_size;
149 } else {
150 assert(!"FINISHME: non-constant array element");
151 }
152 }
153
154 /* Instruction selection: Produce a MOV.sat instead of
155 * MIN(MAX(val, 0), 1) when possible.
156 */
157 bool
158 fs_visitor::try_emit_saturate(ir_expression *ir)
159 {
160 ir_rvalue *sat_val = ir->as_rvalue_to_saturate();
161
162 if (!sat_val)
163 return false;
164
165 this->result = reg_undef;
166 sat_val->accept(this);
167 fs_reg src = this->result;
168
169 this->result = fs_reg(this, ir->type);
170 fs_inst *inst = emit(BRW_OPCODE_MOV, this->result, src);
171 inst->saturate = true;
172
173 return true;
174 }
175
176 void
177 fs_visitor::visit(ir_expression *ir)
178 {
179 unsigned int operand;
180 fs_reg op[2], temp;
181 fs_inst *inst;
182
183 assert(ir->get_num_operands() <= 2);
184
185 if (try_emit_saturate(ir))
186 return;
187
188 /* This is where our caller would like us to put the result, if possible. */
189 fs_reg saved_result_storage = this->result;
190
191 for (operand = 0; operand < ir->get_num_operands(); operand++) {
192 this->result = reg_undef;
193 ir->operands[operand]->accept(this);
194 if (this->result.file == BAD_FILE) {
195 ir_print_visitor v;
196 fail("Failed to get tree for expression operand:\n");
197 ir->operands[operand]->accept(&v);
198 }
199 op[operand] = this->result;
200
201 /* Matrix expression operands should have been broken down to vector
202 * operations already.
203 */
204 assert(!ir->operands[operand]->type->is_matrix());
205 /* And then those vector operands should have been broken down to scalar.
206 */
207 assert(!ir->operands[operand]->type->is_vector());
208 }
209
210 /* Inherit storage from our parent if possible, and otherwise we
211 * alloc a temporary.
212 */
213 if (saved_result_storage.file == BAD_FILE) {
214 this->result = fs_reg(this, ir->type);
215 } else {
216 this->result = saved_result_storage;
217 }
218
219 switch (ir->operation) {
220 case ir_unop_logic_not:
221 /* Note that BRW_OPCODE_NOT is not appropriate here, since it is
222 * ones complement of the whole register, not just bit 0.
223 */
224 emit(BRW_OPCODE_XOR, this->result, op[0], fs_reg(1));
225 break;
226 case ir_unop_neg:
227 op[0].negate = !op[0].negate;
228 this->result = op[0];
229 break;
230 case ir_unop_abs:
231 op[0].abs = true;
232 op[0].negate = false;
233 this->result = op[0];
234 break;
235 case ir_unop_sign:
236 temp = fs_reg(this, ir->type);
237
238 /* Unalias the destination. (imagine a = sign(a)) */
239 this->result = fs_reg(this, ir->type);
240
241 emit(BRW_OPCODE_MOV, this->result, fs_reg(0.0f));
242
243 inst = emit(BRW_OPCODE_CMP, reg_null_f, op[0], fs_reg(0.0f));
244 inst->conditional_mod = BRW_CONDITIONAL_G;
245 inst = emit(BRW_OPCODE_MOV, this->result, fs_reg(1.0f));
246 inst->predicated = true;
247
248 inst = emit(BRW_OPCODE_CMP, reg_null_f, op[0], fs_reg(0.0f));
249 inst->conditional_mod = BRW_CONDITIONAL_L;
250 inst = emit(BRW_OPCODE_MOV, this->result, fs_reg(-1.0f));
251 inst->predicated = true;
252
253 break;
254 case ir_unop_rcp:
255 emit_math(FS_OPCODE_RCP, this->result, op[0]);
256 break;
257
258 case ir_unop_exp2:
259 emit_math(FS_OPCODE_EXP2, this->result, op[0]);
260 break;
261 case ir_unop_log2:
262 emit_math(FS_OPCODE_LOG2, this->result, op[0]);
263 break;
264 case ir_unop_exp:
265 case ir_unop_log:
266 assert(!"not reached: should be handled by ir_explog_to_explog2");
267 break;
268 case ir_unop_sin:
269 case ir_unop_sin_reduced:
270 emit_math(FS_OPCODE_SIN, this->result, op[0]);
271 break;
272 case ir_unop_cos:
273 case ir_unop_cos_reduced:
274 emit_math(FS_OPCODE_COS, this->result, op[0]);
275 break;
276
277 case ir_unop_dFdx:
278 emit(FS_OPCODE_DDX, this->result, op[0]);
279 break;
280 case ir_unop_dFdy:
281 emit(FS_OPCODE_DDY, this->result, op[0]);
282 break;
283
284 case ir_binop_add:
285 emit(BRW_OPCODE_ADD, this->result, op[0], op[1]);
286 break;
287 case ir_binop_sub:
288 assert(!"not reached: should be handled by ir_sub_to_add_neg");
289 break;
290
291 case ir_binop_mul:
292 emit(BRW_OPCODE_MUL, this->result, op[0], op[1]);
293 break;
294 case ir_binop_div:
295 assert(!"not reached: should be handled by ir_div_to_mul_rcp");
296 break;
297 case ir_binop_mod:
298 assert(!"ir_binop_mod should have been converted to b * fract(a/b)");
299 break;
300
301 case ir_binop_less:
302 case ir_binop_greater:
303 case ir_binop_lequal:
304 case ir_binop_gequal:
305 case ir_binop_equal:
306 case ir_binop_all_equal:
307 case ir_binop_nequal:
308 case ir_binop_any_nequal:
309 temp = this->result;
310 /* original gen4 does implicit conversion before comparison. */
311 if (intel->gen < 5)
312 temp.type = op[0].type;
313
314 inst = emit(BRW_OPCODE_CMP, temp, op[0], op[1]);
315 inst->conditional_mod = brw_conditional_for_comparison(ir->operation);
316 emit(BRW_OPCODE_AND, this->result, this->result, fs_reg(0x1));
317 break;
318
319 case ir_binop_logic_xor:
320 emit(BRW_OPCODE_XOR, this->result, op[0], op[1]);
321 break;
322
323 case ir_binop_logic_or:
324 emit(BRW_OPCODE_OR, this->result, op[0], op[1]);
325 break;
326
327 case ir_binop_logic_and:
328 emit(BRW_OPCODE_AND, this->result, op[0], op[1]);
329 break;
330
331 case ir_binop_dot:
332 case ir_unop_any:
333 assert(!"not reached: should be handled by brw_fs_channel_expressions");
334 break;
335
336 case ir_unop_noise:
337 assert(!"not reached: should be handled by lower_noise");
338 break;
339
340 case ir_quadop_vector:
341 assert(!"not reached: should be handled by lower_quadop_vector");
342 break;
343
344 case ir_unop_sqrt:
345 emit_math(FS_OPCODE_SQRT, this->result, op[0]);
346 break;
347
348 case ir_unop_rsq:
349 emit_math(FS_OPCODE_RSQ, this->result, op[0]);
350 break;
351
352 case ir_unop_i2f:
353 case ir_unop_b2f:
354 case ir_unop_b2i:
355 case ir_unop_f2i:
356 emit(BRW_OPCODE_MOV, this->result, op[0]);
357 break;
358 case ir_unop_f2b:
359 case ir_unop_i2b:
360 temp = this->result;
361 /* original gen4 does implicit conversion before comparison. */
362 if (intel->gen < 5)
363 temp.type = op[0].type;
364
365 inst = emit(BRW_OPCODE_CMP, temp, op[0], fs_reg(0.0f));
366 inst->conditional_mod = BRW_CONDITIONAL_NZ;
367 inst = emit(BRW_OPCODE_AND, this->result, this->result, fs_reg(1));
368 break;
369
370 case ir_unop_trunc:
371 emit(BRW_OPCODE_RNDZ, this->result, op[0]);
372 break;
373 case ir_unop_ceil:
374 op[0].negate = !op[0].negate;
375 inst = emit(BRW_OPCODE_RNDD, this->result, op[0]);
376 this->result.negate = true;
377 break;
378 case ir_unop_floor:
379 inst = emit(BRW_OPCODE_RNDD, this->result, op[0]);
380 break;
381 case ir_unop_fract:
382 inst = emit(BRW_OPCODE_FRC, this->result, op[0]);
383 break;
384 case ir_unop_round_even:
385 emit(BRW_OPCODE_RNDE, this->result, op[0]);
386 break;
387
388 case ir_binop_min:
389 if (intel->gen >= 6) {
390 inst = emit(BRW_OPCODE_SEL, this->result, op[0], op[1]);
391 inst->conditional_mod = BRW_CONDITIONAL_L;
392 } else {
393 /* Unalias the destination */
394 this->result = fs_reg(this, ir->type);
395
396 inst = emit(BRW_OPCODE_CMP, this->result, op[0], op[1]);
397 inst->conditional_mod = BRW_CONDITIONAL_L;
398
399 inst = emit(BRW_OPCODE_SEL, this->result, op[0], op[1]);
400 inst->predicated = true;
401 }
402 break;
403 case ir_binop_max:
404 if (intel->gen >= 6) {
405 inst = emit(BRW_OPCODE_SEL, this->result, op[0], op[1]);
406 inst->conditional_mod = BRW_CONDITIONAL_GE;
407 } else {
408 /* Unalias the destination */
409 this->result = fs_reg(this, ir->type);
410
411 inst = emit(BRW_OPCODE_CMP, this->result, op[0], op[1]);
412 inst->conditional_mod = BRW_CONDITIONAL_G;
413
414 inst = emit(BRW_OPCODE_SEL, this->result, op[0], op[1]);
415 inst->predicated = true;
416 }
417 break;
418
419 case ir_binop_pow:
420 emit_math(FS_OPCODE_POW, this->result, op[0], op[1]);
421 break;
422
423 case ir_unop_bit_not:
424 inst = emit(BRW_OPCODE_NOT, this->result, op[0]);
425 break;
426 case ir_binop_bit_and:
427 inst = emit(BRW_OPCODE_AND, this->result, op[0], op[1]);
428 break;
429 case ir_binop_bit_xor:
430 inst = emit(BRW_OPCODE_XOR, this->result, op[0], op[1]);
431 break;
432 case ir_binop_bit_or:
433 inst = emit(BRW_OPCODE_OR, this->result, op[0], op[1]);
434 break;
435
436 case ir_unop_u2f:
437 case ir_binop_lshift:
438 case ir_binop_rshift:
439 assert(!"GLSL 1.30 features unsupported");
440 break;
441 }
442 }
443
444 void
445 fs_visitor::emit_assignment_writes(fs_reg &l, fs_reg &r,
446 const glsl_type *type, bool predicated)
447 {
448 switch (type->base_type) {
449 case GLSL_TYPE_FLOAT:
450 case GLSL_TYPE_UINT:
451 case GLSL_TYPE_INT:
452 case GLSL_TYPE_BOOL:
453 for (unsigned int i = 0; i < type->components(); i++) {
454 l.type = brw_type_for_base_type(type);
455 r.type = brw_type_for_base_type(type);
456
457 if (predicated || !l.equals(&r)) {
458 fs_inst *inst = emit(BRW_OPCODE_MOV, l, r);
459 inst->predicated = predicated;
460 }
461
462 l.reg_offset++;
463 r.reg_offset++;
464 }
465 break;
466 case GLSL_TYPE_ARRAY:
467 for (unsigned int i = 0; i < type->length; i++) {
468 emit_assignment_writes(l, r, type->fields.array, predicated);
469 }
470 break;
471
472 case GLSL_TYPE_STRUCT:
473 for (unsigned int i = 0; i < type->length; i++) {
474 emit_assignment_writes(l, r, type->fields.structure[i].type,
475 predicated);
476 }
477 break;
478
479 case GLSL_TYPE_SAMPLER:
480 break;
481
482 default:
483 assert(!"not reached");
484 break;
485 }
486 }
487
488 void
489 fs_visitor::visit(ir_assignment *ir)
490 {
491 struct fs_reg l, r;
492 fs_inst *inst;
493
494 /* FINISHME: arrays on the lhs */
495 this->result = reg_undef;
496 ir->lhs->accept(this);
497 l = this->result;
498
499 /* If we're doing a direct assignment, an RHS expression could
500 * drop its result right into our destination. Otherwise, tell it
501 * not to.
502 */
503 if (ir->condition ||
504 !(ir->lhs->type->is_scalar() ||
505 (ir->lhs->type->is_vector() &&
506 ir->write_mask == (1 << ir->lhs->type->vector_elements) - 1))) {
507 this->result = reg_undef;
508 }
509
510 ir->rhs->accept(this);
511 r = this->result;
512
513 assert(l.file != BAD_FILE);
514 assert(r.file != BAD_FILE);
515
516 if (ir->condition) {
517 emit_bool_to_cond_code(ir->condition);
518 }
519
520 if (ir->lhs->type->is_scalar() ||
521 ir->lhs->type->is_vector()) {
522 for (int i = 0; i < ir->lhs->type->vector_elements; i++) {
523 if (ir->write_mask & (1 << i)) {
524 if (ir->condition) {
525 inst = emit(BRW_OPCODE_MOV, l, r);
526 inst->predicated = true;
527 } else if (!l.equals(&r)) {
528 inst = emit(BRW_OPCODE_MOV, l, r);
529 }
530
531 r.reg_offset++;
532 }
533 l.reg_offset++;
534 }
535 } else {
536 emit_assignment_writes(l, r, ir->lhs->type, ir->condition != NULL);
537 }
538 }
539
540 fs_inst *
541 fs_visitor::emit_texture_gen4(ir_texture *ir, fs_reg dst, fs_reg coordinate,
542 int sampler)
543 {
544 int mlen;
545 int base_mrf = 1;
546 bool simd16 = false;
547 fs_reg orig_dst;
548
549 /* g0 header. */
550 mlen = 1;
551
552 if (ir->shadow_comparitor) {
553 for (int i = 0; i < ir->coordinate->type->vector_elements; i++) {
554 fs_inst *inst = emit(BRW_OPCODE_MOV,
555 fs_reg(MRF, base_mrf + mlen + i), coordinate);
556 if (i < 3 && c->key.gl_clamp_mask[i] & (1 << sampler))
557 inst->saturate = true;
558
559 coordinate.reg_offset++;
560 }
561 /* gen4's SIMD8 sampler always has the slots for u,v,r present. */
562 mlen += 3;
563
564 if (ir->op == ir_tex) {
565 /* There's no plain shadow compare message, so we use shadow
566 * compare with a bias of 0.0.
567 */
568 emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen), fs_reg(0.0f));
569 mlen++;
570 } else if (ir->op == ir_txb) {
571 this->result = reg_undef;
572 ir->lod_info.bias->accept(this);
573 emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen), this->result);
574 mlen++;
575 } else {
576 assert(ir->op == ir_txl);
577 this->result = reg_undef;
578 ir->lod_info.lod->accept(this);
579 emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen), this->result);
580 mlen++;
581 }
582
583 this->result = reg_undef;
584 ir->shadow_comparitor->accept(this);
585 emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen), this->result);
586 mlen++;
587 } else if (ir->op == ir_tex) {
588 for (int i = 0; i < ir->coordinate->type->vector_elements; i++) {
589 fs_inst *inst = emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen + i),
590 coordinate);
591 if (i < 3 && c->key.gl_clamp_mask[i] & (1 << sampler))
592 inst->saturate = true;
593 coordinate.reg_offset++;
594 }
595 /* gen4's SIMD8 sampler always has the slots for u,v,r present. */
596 mlen += 3;
597 } else if (ir->op == ir_txd) {
598 assert(!"TXD isn't supported on gen4 yet.");
599 } else {
600 /* Oh joy. gen4 doesn't have SIMD8 non-shadow-compare bias/lod
601 * instructions. We'll need to do SIMD16 here.
602 */
603 assert(ir->op == ir_txb || ir->op == ir_txl);
604
605 for (int i = 0; i < ir->coordinate->type->vector_elements; i++) {
606 fs_inst *inst = emit(BRW_OPCODE_MOV, fs_reg(MRF,
607 base_mrf + mlen + i * 2),
608 coordinate);
609 if (i < 3 && c->key.gl_clamp_mask[i] & (1 << sampler))
610 inst->saturate = true;
611 coordinate.reg_offset++;
612 }
613
614 /* lod/bias appears after u/v/r. */
615 mlen += 6;
616
617 if (ir->op == ir_txb) {
618 this->result = reg_undef;
619 ir->lod_info.bias->accept(this);
620 emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen), this->result);
621 mlen++;
622 } else {
623 this->result = reg_undef;
624 ir->lod_info.lod->accept(this);
625 emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen), this->result);
626 mlen++;
627 }
628
629 /* The unused upper half. */
630 mlen++;
631
632 /* Now, since we're doing simd16, the return is 2 interleaved
633 * vec4s where the odd-indexed ones are junk. We'll need to move
634 * this weirdness around to the expected layout.
635 */
636 simd16 = true;
637 orig_dst = dst;
638 dst = fs_reg(this, glsl_type::get_array_instance(glsl_type::vec4_type,
639 2));
640 dst.type = BRW_REGISTER_TYPE_F;
641 }
642
643 fs_inst *inst = NULL;
644 switch (ir->op) {
645 case ir_tex:
646 inst = emit(FS_OPCODE_TEX, dst);
647 break;
648 case ir_txb:
649 inst = emit(FS_OPCODE_TXB, dst);
650 break;
651 case ir_txl:
652 inst = emit(FS_OPCODE_TXL, dst);
653 break;
654 case ir_txd:
655 inst = emit(FS_OPCODE_TXD, dst);
656 break;
657 case ir_txf:
658 assert(!"GLSL 1.30 features unsupported");
659 break;
660 }
661 inst->base_mrf = base_mrf;
662 inst->mlen = mlen;
663 inst->header_present = true;
664
665 if (simd16) {
666 for (int i = 0; i < 4; i++) {
667 emit(BRW_OPCODE_MOV, orig_dst, dst);
668 orig_dst.reg_offset++;
669 dst.reg_offset += 2;
670 }
671 }
672
673 return inst;
674 }
675
676 /* gen5's sampler has slots for u, v, r, array index, then optional
677 * parameters like shadow comparitor or LOD bias. If optional
678 * parameters aren't present, those base slots are optional and don't
679 * need to be included in the message.
680 *
681 * We don't fill in the unnecessary slots regardless, which may look
682 * surprising in the disassembly.
683 */
684 fs_inst *
685 fs_visitor::emit_texture_gen5(ir_texture *ir, fs_reg dst, fs_reg coordinate,
686 int sampler)
687 {
688 int mlen = 0;
689 int base_mrf = 2;
690 int reg_width = c->dispatch_width / 8;
691 bool header_present = false;
692
693 if (ir->offset) {
694 /* The offsets set up by the ir_texture visitor are in the
695 * m1 header, so we can't go headerless.
696 */
697 header_present = true;
698 mlen++;
699 base_mrf--;
700 }
701
702 for (int i = 0; i < ir->coordinate->type->vector_elements; i++) {
703 fs_inst *inst = emit(BRW_OPCODE_MOV,
704 fs_reg(MRF, base_mrf + mlen + i * reg_width),
705 coordinate);
706 if (i < 3 && c->key.gl_clamp_mask[i] & (1 << sampler))
707 inst->saturate = true;
708 coordinate.reg_offset++;
709 }
710 mlen += ir->coordinate->type->vector_elements * reg_width;
711
712 if (ir->shadow_comparitor) {
713 mlen = MAX2(mlen, header_present + 4 * reg_width);
714
715 this->result = reg_undef;
716 ir->shadow_comparitor->accept(this);
717 emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen), this->result);
718 mlen += reg_width;
719 }
720
721 fs_inst *inst = NULL;
722 switch (ir->op) {
723 case ir_tex:
724 inst = emit(FS_OPCODE_TEX, dst);
725 break;
726 case ir_txb:
727 this->result = reg_undef;
728 ir->lod_info.bias->accept(this);
729 mlen = MAX2(mlen, header_present + 4 * reg_width);
730 emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen), this->result);
731 mlen += reg_width;
732
733 inst = emit(FS_OPCODE_TXB, dst);
734
735 break;
736 case ir_txl:
737 this->result = reg_undef;
738 ir->lod_info.lod->accept(this);
739 mlen = MAX2(mlen, header_present + 4 * reg_width);
740 emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen), this->result);
741 mlen += reg_width;
742
743 inst = emit(FS_OPCODE_TXL, dst);
744 break;
745 case ir_txd: {
746 ir->lod_info.grad.dPdx->accept(this);
747 fs_reg dPdx = this->result;
748
749 ir->lod_info.grad.dPdy->accept(this);
750 fs_reg dPdy = this->result;
751
752 mlen = MAX2(mlen, header_present + 4 * reg_width); /* skip over 'ai' */
753
754 /**
755 * P = u, v, r
756 * dPdx = dudx, dvdx, drdx
757 * dPdy = dudy, dvdy, drdy
758 *
759 * Load up these values:
760 * - dudx dudy dvdx dvdy drdx drdy
761 * - dPdx.x dPdy.x dPdx.y dPdy.y dPdx.z dPdy.z
762 */
763 for (int i = 0; i < ir->lod_info.grad.dPdx->type->vector_elements; i++) {
764 emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen), dPdx);
765 dPdx.reg_offset++;
766 mlen += reg_width;
767
768 emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen), dPdy);
769 dPdy.reg_offset++;
770 mlen += reg_width;
771 }
772
773 inst = emit(FS_OPCODE_TXD, dst);
774 break;
775 }
776 case ir_txf:
777 assert(!"GLSL 1.30 features unsupported");
778 break;
779 }
780 inst->base_mrf = base_mrf;
781 inst->mlen = mlen;
782 inst->header_present = header_present;
783
784 if (mlen > 11) {
785 fail("Message length >11 disallowed by hardware\n");
786 }
787
788 return inst;
789 }
790
791 fs_inst *
792 fs_visitor::emit_texture_gen7(ir_texture *ir, fs_reg dst, fs_reg coordinate,
793 int sampler)
794 {
795 int mlen = 0;
796 int base_mrf = 2;
797 int reg_width = c->dispatch_width / 8;
798 bool header_present = false;
799
800 if (ir->offset) {
801 /* The offsets set up by the ir_texture visitor are in the
802 * m1 header, so we can't go headerless.
803 */
804 header_present = true;
805 mlen++;
806 base_mrf--;
807 }
808
809 if (ir->shadow_comparitor) {
810 ir->shadow_comparitor->accept(this);
811 emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen), this->result);
812 mlen += reg_width;
813 }
814
815 /* Set up the LOD info */
816 switch (ir->op) {
817 case ir_tex:
818 break;
819 case ir_txb:
820 ir->lod_info.bias->accept(this);
821 emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen), this->result);
822 mlen += reg_width;
823 break;
824 case ir_txl:
825 ir->lod_info.lod->accept(this);
826 emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen), this->result);
827 mlen += reg_width;
828 break;
829 case ir_txd: {
830 if (c->dispatch_width == 16)
831 fail("Gen7 does not support sample_d/sample_d_c in SIMD16 mode.");
832
833 ir->lod_info.grad.dPdx->accept(this);
834 fs_reg dPdx = this->result;
835
836 ir->lod_info.grad.dPdy->accept(this);
837 fs_reg dPdy = this->result;
838
839 /* Load dPdx and the coordinate together:
840 * [hdr], [ref], x, dPdx.x, dPdy.x, y, dPdx.y, dPdy.y, z, dPdx.z, dPdy.z
841 */
842 for (int i = 0; i < ir->coordinate->type->vector_elements; i++) {
843 fs_inst *inst = emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen),
844 coordinate);
845 if (i < 3 && c->key.gl_clamp_mask[i] & (1 << sampler))
846 inst->saturate = true;
847 coordinate.reg_offset++;
848 mlen += reg_width;
849
850 emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen), dPdx);
851 dPdx.reg_offset++;
852 mlen += reg_width;
853
854 emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen), dPdy);
855 dPdy.reg_offset++;
856 mlen += reg_width;
857 }
858 break;
859 }
860 case ir_txf:
861 assert(!"GLSL 1.30 features unsupported");
862 break;
863 }
864
865 /* Set up the coordinate (except for TXD where it was done earlier) */
866 if (ir->op != ir_txd) {
867 for (int i = 0; i < ir->coordinate->type->vector_elements; i++) {
868 fs_inst *inst = emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen),
869 coordinate);
870 if (i < 3 && c->key.gl_clamp_mask[i] & (1 << sampler))
871 inst->saturate = true;
872 coordinate.reg_offset++;
873 mlen += reg_width;
874 }
875 }
876
877 /* Generate the SEND */
878 fs_inst *inst = NULL;
879 switch (ir->op) {
880 case ir_tex: inst = emit(FS_OPCODE_TEX, dst); break;
881 case ir_txb: inst = emit(FS_OPCODE_TXB, dst); break;
882 case ir_txl: inst = emit(FS_OPCODE_TXL, dst); break;
883 case ir_txd: inst = emit(FS_OPCODE_TXD, dst); break;
884 case ir_txf: assert(!"TXF unsupported.");
885 }
886 inst->base_mrf = base_mrf;
887 inst->mlen = mlen;
888 inst->header_present = header_present;
889
890 if (mlen > 11) {
891 fail("Message length >11 disallowed by hardware\n");
892 }
893
894 return inst;
895 }
896
897 void
898 fs_visitor::visit(ir_texture *ir)
899 {
900 int sampler;
901 fs_inst *inst = NULL;
902
903 this->result = reg_undef;
904 ir->coordinate->accept(this);
905 fs_reg coordinate = this->result;
906
907 if (ir->offset != NULL) {
908 ir_constant *offset = ir->offset->as_constant();
909 assert(offset != NULL);
910
911 signed char offsets[3];
912 for (unsigned i = 0; i < ir->offset->type->vector_elements; i++)
913 offsets[i] = (signed char) offset->value.i[i];
914
915 /* Combine all three offsets into a single unsigned dword:
916 *
917 * bits 11:8 - U Offset (X component)
918 * bits 7:4 - V Offset (Y component)
919 * bits 3:0 - R Offset (Z component)
920 */
921 unsigned offset_bits = 0;
922 for (unsigned i = 0; i < ir->offset->type->vector_elements; i++) {
923 const unsigned shift = 4 * (2 - i);
924 offset_bits |= (offsets[i] << shift) & (0xF << shift);
925 }
926
927 /* Explicitly set up the message header by copying g0 to msg reg m1. */
928 emit(BRW_OPCODE_MOV, fs_reg(MRF, 1, BRW_REGISTER_TYPE_UD),
929 fs_reg(GRF, 0, BRW_REGISTER_TYPE_UD));
930
931 /* Then set the offset bits in DWord 2 of the message header. */
932 emit(BRW_OPCODE_MOV,
933 fs_reg(retype(brw_vec1_reg(BRW_MESSAGE_REGISTER_FILE, 1, 2),
934 BRW_REGISTER_TYPE_UD)),
935 fs_reg(brw_imm_uw(offset_bits)));
936 }
937
938 /* Should be lowered by do_lower_texture_projection */
939 assert(!ir->projector);
940
941 sampler = _mesa_get_sampler_uniform_value(ir->sampler,
942 prog,
943 &fp->Base);
944 sampler = fp->Base.SamplerUnits[sampler];
945
946 /* The 965 requires the EU to do the normalization of GL rectangle
947 * texture coordinates. We use the program parameter state
948 * tracking to get the scaling factor.
949 */
950 if (ir->sampler->type->sampler_dimensionality == GLSL_SAMPLER_DIM_RECT) {
951 struct gl_program_parameter_list *params = c->fp->program.Base.Parameters;
952 int tokens[STATE_LENGTH] = {
953 STATE_INTERNAL,
954 STATE_TEXRECT_SCALE,
955 sampler,
956 0,
957 0
958 };
959
960 if (c->dispatch_width == 16) {
961 fail("rectangle scale uniform setup not supported on 16-wide\n");
962 this->result = fs_reg(this, ir->type);
963 return;
964 }
965
966 c->prog_data.param_convert[c->prog_data.nr_params] =
967 PARAM_NO_CONVERT;
968 c->prog_data.param_convert[c->prog_data.nr_params + 1] =
969 PARAM_NO_CONVERT;
970
971 fs_reg scale_x = fs_reg(UNIFORM, c->prog_data.nr_params);
972 fs_reg scale_y = fs_reg(UNIFORM, c->prog_data.nr_params + 1);
973 GLuint index = _mesa_add_state_reference(params,
974 (gl_state_index *)tokens);
975
976 this->param_index[c->prog_data.nr_params] = index;
977 this->param_offset[c->prog_data.nr_params] = 0;
978 c->prog_data.nr_params++;
979 this->param_index[c->prog_data.nr_params] = index;
980 this->param_offset[c->prog_data.nr_params] = 1;
981 c->prog_data.nr_params++;
982
983 fs_reg dst = fs_reg(this, ir->coordinate->type);
984 fs_reg src = coordinate;
985 coordinate = dst;
986
987 emit(BRW_OPCODE_MUL, dst, src, scale_x);
988 dst.reg_offset++;
989 src.reg_offset++;
990 emit(BRW_OPCODE_MUL, dst, src, scale_y);
991 }
992
993 /* Writemasking doesn't eliminate channels on SIMD8 texture
994 * samples, so don't worry about them.
995 */
996 fs_reg dst = fs_reg(this, glsl_type::vec4_type);
997
998 if (intel->gen >= 7) {
999 inst = emit_texture_gen7(ir, dst, coordinate, sampler);
1000 } else if (intel->gen >= 5) {
1001 inst = emit_texture_gen5(ir, dst, coordinate, sampler);
1002 } else {
1003 inst = emit_texture_gen4(ir, dst, coordinate, sampler);
1004 }
1005
1006 /* If there's an offset, we already set up m1. To avoid the implied move,
1007 * use the null register. Otherwise, we want an implied move from g0.
1008 */
1009 if (ir->offset != NULL || !inst->header_present)
1010 inst->src[0] = reg_undef;
1011 else
1012 inst->src[0] = fs_reg(retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UW));
1013
1014 inst->sampler = sampler;
1015
1016 this->result = dst;
1017
1018 if (ir->shadow_comparitor)
1019 inst->shadow_compare = true;
1020
1021 if (ir->type == glsl_type::float_type) {
1022 /* Ignore DEPTH_TEXTURE_MODE swizzling. */
1023 assert(ir->sampler->type->sampler_shadow);
1024 } else if (c->key.tex_swizzles[inst->sampler] != SWIZZLE_NOOP) {
1025 fs_reg swizzle_dst = fs_reg(this, glsl_type::vec4_type);
1026
1027 for (int i = 0; i < 4; i++) {
1028 int swiz = GET_SWZ(c->key.tex_swizzles[inst->sampler], i);
1029 fs_reg l = swizzle_dst;
1030 l.reg_offset += i;
1031
1032 if (swiz == SWIZZLE_ZERO) {
1033 emit(BRW_OPCODE_MOV, l, fs_reg(0.0f));
1034 } else if (swiz == SWIZZLE_ONE) {
1035 emit(BRW_OPCODE_MOV, l, fs_reg(1.0f));
1036 } else {
1037 fs_reg r = dst;
1038 r.reg_offset += GET_SWZ(c->key.tex_swizzles[inst->sampler], i);
1039 emit(BRW_OPCODE_MOV, l, r);
1040 }
1041 }
1042 this->result = swizzle_dst;
1043 }
1044 }
1045
1046 void
1047 fs_visitor::visit(ir_swizzle *ir)
1048 {
1049 this->result = reg_undef;
1050 ir->val->accept(this);
1051 fs_reg val = this->result;
1052
1053 if (ir->type->vector_elements == 1) {
1054 this->result.reg_offset += ir->mask.x;
1055 return;
1056 }
1057
1058 fs_reg result = fs_reg(this, ir->type);
1059 this->result = result;
1060
1061 for (unsigned int i = 0; i < ir->type->vector_elements; i++) {
1062 fs_reg channel = val;
1063 int swiz = 0;
1064
1065 switch (i) {
1066 case 0:
1067 swiz = ir->mask.x;
1068 break;
1069 case 1:
1070 swiz = ir->mask.y;
1071 break;
1072 case 2:
1073 swiz = ir->mask.z;
1074 break;
1075 case 3:
1076 swiz = ir->mask.w;
1077 break;
1078 }
1079
1080 channel.reg_offset += swiz;
1081 emit(BRW_OPCODE_MOV, result, channel);
1082 result.reg_offset++;
1083 }
1084 }
1085
1086 void
1087 fs_visitor::visit(ir_discard *ir)
1088 {
1089 assert(ir->condition == NULL); /* FINISHME */
1090
1091 emit(FS_OPCODE_DISCARD);
1092 kill_emitted = true;
1093 }
1094
1095 void
1096 fs_visitor::visit(ir_constant *ir)
1097 {
1098 /* Set this->result to reg at the bottom of the function because some code
1099 * paths will cause this visitor to be applied to other fields. This will
1100 * cause the value stored in this->result to be modified.
1101 *
1102 * Make reg constant so that it doesn't get accidentally modified along the
1103 * way. Yes, I actually had this problem. :(
1104 */
1105 const fs_reg reg(this, ir->type);
1106 fs_reg dst_reg = reg;
1107
1108 if (ir->type->is_array()) {
1109 const unsigned size = type_size(ir->type->fields.array);
1110
1111 for (unsigned i = 0; i < ir->type->length; i++) {
1112 this->result = reg_undef;
1113 ir->array_elements[i]->accept(this);
1114 fs_reg src_reg = this->result;
1115
1116 dst_reg.type = src_reg.type;
1117 for (unsigned j = 0; j < size; j++) {
1118 emit(BRW_OPCODE_MOV, dst_reg, src_reg);
1119 src_reg.reg_offset++;
1120 dst_reg.reg_offset++;
1121 }
1122 }
1123 } else if (ir->type->is_record()) {
1124 foreach_list(node, &ir->components) {
1125 ir_instruction *const field = (ir_instruction *) node;
1126 const unsigned size = type_size(field->type);
1127
1128 this->result = reg_undef;
1129 field->accept(this);
1130 fs_reg src_reg = this->result;
1131
1132 dst_reg.type = src_reg.type;
1133 for (unsigned j = 0; j < size; j++) {
1134 emit(BRW_OPCODE_MOV, dst_reg, src_reg);
1135 src_reg.reg_offset++;
1136 dst_reg.reg_offset++;
1137 }
1138 }
1139 } else {
1140 const unsigned size = type_size(ir->type);
1141
1142 for (unsigned i = 0; i < size; i++) {
1143 switch (ir->type->base_type) {
1144 case GLSL_TYPE_FLOAT:
1145 emit(BRW_OPCODE_MOV, dst_reg, fs_reg(ir->value.f[i]));
1146 break;
1147 case GLSL_TYPE_UINT:
1148 emit(BRW_OPCODE_MOV, dst_reg, fs_reg(ir->value.u[i]));
1149 break;
1150 case GLSL_TYPE_INT:
1151 emit(BRW_OPCODE_MOV, dst_reg, fs_reg(ir->value.i[i]));
1152 break;
1153 case GLSL_TYPE_BOOL:
1154 emit(BRW_OPCODE_MOV, dst_reg, fs_reg((int)ir->value.b[i]));
1155 break;
1156 default:
1157 assert(!"Non-float/uint/int/bool constant");
1158 }
1159 dst_reg.reg_offset++;
1160 }
1161 }
1162
1163 this->result = reg;
1164 }
1165
1166 void
1167 fs_visitor::emit_bool_to_cond_code(ir_rvalue *ir)
1168 {
1169 ir_expression *expr = ir->as_expression();
1170
1171 if (expr) {
1172 fs_reg op[2];
1173 fs_inst *inst;
1174
1175 assert(expr->get_num_operands() <= 2);
1176 for (unsigned int i = 0; i < expr->get_num_operands(); i++) {
1177 assert(expr->operands[i]->type->is_scalar());
1178
1179 this->result = reg_undef;
1180 expr->operands[i]->accept(this);
1181 op[i] = this->result;
1182 }
1183
1184 switch (expr->operation) {
1185 case ir_unop_logic_not:
1186 inst = emit(BRW_OPCODE_AND, reg_null_d, op[0], fs_reg(1));
1187 inst->conditional_mod = BRW_CONDITIONAL_Z;
1188 break;
1189
1190 case ir_binop_logic_xor:
1191 inst = emit(BRW_OPCODE_XOR, reg_null_d, op[0], op[1]);
1192 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1193 break;
1194
1195 case ir_binop_logic_or:
1196 inst = emit(BRW_OPCODE_OR, reg_null_d, op[0], op[1]);
1197 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1198 break;
1199
1200 case ir_binop_logic_and:
1201 inst = emit(BRW_OPCODE_AND, reg_null_d, op[0], op[1]);
1202 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1203 break;
1204
1205 case ir_unop_f2b:
1206 if (intel->gen >= 6) {
1207 inst = emit(BRW_OPCODE_CMP, reg_null_d, op[0], fs_reg(0.0f));
1208 } else {
1209 inst = emit(BRW_OPCODE_MOV, reg_null_f, op[0]);
1210 }
1211 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1212 break;
1213
1214 case ir_unop_i2b:
1215 if (intel->gen >= 6) {
1216 inst = emit(BRW_OPCODE_CMP, reg_null_d, op[0], fs_reg(0));
1217 } else {
1218 inst = emit(BRW_OPCODE_MOV, reg_null_d, op[0]);
1219 }
1220 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1221 break;
1222
1223 case ir_binop_greater:
1224 case ir_binop_gequal:
1225 case ir_binop_less:
1226 case ir_binop_lequal:
1227 case ir_binop_equal:
1228 case ir_binop_all_equal:
1229 case ir_binop_nequal:
1230 case ir_binop_any_nequal:
1231 inst = emit(BRW_OPCODE_CMP, reg_null_cmp, op[0], op[1]);
1232 inst->conditional_mod =
1233 brw_conditional_for_comparison(expr->operation);
1234 break;
1235
1236 default:
1237 assert(!"not reached");
1238 fail("bad cond code\n");
1239 break;
1240 }
1241 return;
1242 }
1243
1244 this->result = reg_undef;
1245 ir->accept(this);
1246
1247 if (intel->gen >= 6) {
1248 fs_inst *inst = emit(BRW_OPCODE_AND, reg_null_d, this->result, fs_reg(1));
1249 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1250 } else {
1251 fs_inst *inst = emit(BRW_OPCODE_MOV, reg_null_d, this->result);
1252 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1253 }
1254 }
1255
1256 /**
1257 * Emit a gen6 IF statement with the comparison folded into the IF
1258 * instruction.
1259 */
1260 void
1261 fs_visitor::emit_if_gen6(ir_if *ir)
1262 {
1263 ir_expression *expr = ir->condition->as_expression();
1264
1265 if (expr) {
1266 fs_reg op[2];
1267 fs_inst *inst;
1268 fs_reg temp;
1269
1270 assert(expr->get_num_operands() <= 2);
1271 for (unsigned int i = 0; i < expr->get_num_operands(); i++) {
1272 assert(expr->operands[i]->type->is_scalar());
1273
1274 this->result = reg_undef;
1275 expr->operands[i]->accept(this);
1276 op[i] = this->result;
1277 }
1278
1279 switch (expr->operation) {
1280 case ir_unop_logic_not:
1281 inst = emit(BRW_OPCODE_IF, temp, op[0], fs_reg(0));
1282 inst->conditional_mod = BRW_CONDITIONAL_Z;
1283 return;
1284
1285 case ir_binop_logic_xor:
1286 inst = emit(BRW_OPCODE_IF, reg_null_d, op[0], op[1]);
1287 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1288 return;
1289
1290 case ir_binop_logic_or:
1291 temp = fs_reg(this, glsl_type::bool_type);
1292 emit(BRW_OPCODE_OR, temp, op[0], op[1]);
1293 inst = emit(BRW_OPCODE_IF, reg_null_d, temp, fs_reg(0));
1294 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1295 return;
1296
1297 case ir_binop_logic_and:
1298 temp = fs_reg(this, glsl_type::bool_type);
1299 emit(BRW_OPCODE_AND, temp, op[0], op[1]);
1300 inst = emit(BRW_OPCODE_IF, reg_null_d, temp, fs_reg(0));
1301 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1302 return;
1303
1304 case ir_unop_f2b:
1305 inst = emit(BRW_OPCODE_IF, reg_null_f, op[0], fs_reg(0));
1306 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1307 return;
1308
1309 case ir_unop_i2b:
1310 inst = emit(BRW_OPCODE_IF, reg_null_d, op[0], fs_reg(0));
1311 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1312 return;
1313
1314 case ir_binop_greater:
1315 case ir_binop_gequal:
1316 case ir_binop_less:
1317 case ir_binop_lequal:
1318 case ir_binop_equal:
1319 case ir_binop_all_equal:
1320 case ir_binop_nequal:
1321 case ir_binop_any_nequal:
1322 inst = emit(BRW_OPCODE_IF, reg_null_d, op[0], op[1]);
1323 inst->conditional_mod =
1324 brw_conditional_for_comparison(expr->operation);
1325 return;
1326 default:
1327 assert(!"not reached");
1328 inst = emit(BRW_OPCODE_IF, reg_null_d, op[0], fs_reg(0));
1329 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1330 fail("bad condition\n");
1331 return;
1332 }
1333 return;
1334 }
1335
1336 this->result = reg_undef;
1337 ir->condition->accept(this);
1338
1339 fs_inst *inst = emit(BRW_OPCODE_IF, reg_null_d, this->result, fs_reg(0));
1340 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1341 }
1342
1343 void
1344 fs_visitor::visit(ir_if *ir)
1345 {
1346 fs_inst *inst;
1347
1348 if (intel->gen != 6 && c->dispatch_width == 16) {
1349 fail("Can't support (non-uniform) control flow on 16-wide\n");
1350 }
1351
1352 /* Don't point the annotation at the if statement, because then it plus
1353 * the then and else blocks get printed.
1354 */
1355 this->base_ir = ir->condition;
1356
1357 if (intel->gen == 6) {
1358 emit_if_gen6(ir);
1359 } else {
1360 emit_bool_to_cond_code(ir->condition);
1361
1362 inst = emit(BRW_OPCODE_IF);
1363 inst->predicated = true;
1364 }
1365
1366 foreach_iter(exec_list_iterator, iter, ir->then_instructions) {
1367 ir_instruction *ir = (ir_instruction *)iter.get();
1368 this->base_ir = ir;
1369 this->result = reg_undef;
1370 ir->accept(this);
1371 }
1372
1373 if (!ir->else_instructions.is_empty()) {
1374 emit(BRW_OPCODE_ELSE);
1375
1376 foreach_iter(exec_list_iterator, iter, ir->else_instructions) {
1377 ir_instruction *ir = (ir_instruction *)iter.get();
1378 this->base_ir = ir;
1379 this->result = reg_undef;
1380 ir->accept(this);
1381 }
1382 }
1383
1384 emit(BRW_OPCODE_ENDIF);
1385 }
1386
1387 void
1388 fs_visitor::visit(ir_loop *ir)
1389 {
1390 fs_reg counter = reg_undef;
1391
1392 if (c->dispatch_width == 16) {
1393 fail("Can't support (non-uniform) control flow on 16-wide\n");
1394 }
1395
1396 if (ir->counter) {
1397 this->base_ir = ir->counter;
1398 ir->counter->accept(this);
1399 counter = *(variable_storage(ir->counter));
1400
1401 if (ir->from) {
1402 this->result = counter;
1403
1404 this->base_ir = ir->from;
1405 this->result = counter;
1406 ir->from->accept(this);
1407
1408 if (!this->result.equals(&counter))
1409 emit(BRW_OPCODE_MOV, counter, this->result);
1410 }
1411 }
1412
1413 emit(BRW_OPCODE_DO);
1414
1415 if (ir->to) {
1416 this->base_ir = ir->to;
1417 this->result = reg_undef;
1418 ir->to->accept(this);
1419
1420 fs_inst *inst = emit(BRW_OPCODE_CMP, reg_null_cmp, counter, this->result);
1421 inst->conditional_mod = brw_conditional_for_comparison(ir->cmp);
1422
1423 inst = emit(BRW_OPCODE_BREAK);
1424 inst->predicated = true;
1425 }
1426
1427 foreach_iter(exec_list_iterator, iter, ir->body_instructions) {
1428 ir_instruction *ir = (ir_instruction *)iter.get();
1429
1430 this->base_ir = ir;
1431 this->result = reg_undef;
1432 ir->accept(this);
1433 }
1434
1435 if (ir->increment) {
1436 this->base_ir = ir->increment;
1437 this->result = reg_undef;
1438 ir->increment->accept(this);
1439 emit(BRW_OPCODE_ADD, counter, counter, this->result);
1440 }
1441
1442 emit(BRW_OPCODE_WHILE);
1443 }
1444
1445 void
1446 fs_visitor::visit(ir_loop_jump *ir)
1447 {
1448 switch (ir->mode) {
1449 case ir_loop_jump::jump_break:
1450 emit(BRW_OPCODE_BREAK);
1451 break;
1452 case ir_loop_jump::jump_continue:
1453 emit(BRW_OPCODE_CONTINUE);
1454 break;
1455 }
1456 }
1457
1458 void
1459 fs_visitor::visit(ir_call *ir)
1460 {
1461 assert(!"FINISHME");
1462 }
1463
1464 void
1465 fs_visitor::visit(ir_return *ir)
1466 {
1467 assert(!"FINISHME");
1468 }
1469
1470 void
1471 fs_visitor::visit(ir_function *ir)
1472 {
1473 /* Ignore function bodies other than main() -- we shouldn't see calls to
1474 * them since they should all be inlined before we get to ir_to_mesa.
1475 */
1476 if (strcmp(ir->name, "main") == 0) {
1477 const ir_function_signature *sig;
1478 exec_list empty;
1479
1480 sig = ir->matching_signature(&empty);
1481
1482 assert(sig);
1483
1484 foreach_iter(exec_list_iterator, iter, sig->body) {
1485 ir_instruction *ir = (ir_instruction *)iter.get();
1486 this->base_ir = ir;
1487 this->result = reg_undef;
1488 ir->accept(this);
1489 }
1490 }
1491 }
1492
1493 void
1494 fs_visitor::visit(ir_function_signature *ir)
1495 {
1496 assert(!"not reached");
1497 (void)ir;
1498 }
1499
1500 fs_inst *
1501 fs_visitor::emit(fs_inst inst)
1502 {
1503 fs_inst *list_inst = new(mem_ctx) fs_inst;
1504 *list_inst = inst;
1505
1506 if (force_uncompressed_stack > 0)
1507 list_inst->force_uncompressed = true;
1508 else if (force_sechalf_stack > 0)
1509 list_inst->force_sechalf = true;
1510
1511 list_inst->annotation = this->current_annotation;
1512 list_inst->ir = this->base_ir;
1513
1514 this->instructions.push_tail(list_inst);
1515
1516 return list_inst;
1517 }
1518
1519 /** Emits a dummy fragment shader consisting of magenta for bringup purposes. */
1520 void
1521 fs_visitor::emit_dummy_fs()
1522 {
1523 /* Everyone's favorite color. */
1524 emit(BRW_OPCODE_MOV, fs_reg(MRF, 2), fs_reg(1.0f));
1525 emit(BRW_OPCODE_MOV, fs_reg(MRF, 3), fs_reg(0.0f));
1526 emit(BRW_OPCODE_MOV, fs_reg(MRF, 4), fs_reg(1.0f));
1527 emit(BRW_OPCODE_MOV, fs_reg(MRF, 5), fs_reg(0.0f));
1528
1529 fs_inst *write;
1530 write = emit(FS_OPCODE_FB_WRITE, fs_reg(0), fs_reg(0));
1531 write->base_mrf = 0;
1532 }
1533
1534 /* The register location here is relative to the start of the URB
1535 * data. It will get adjusted to be a real location before
1536 * generate_code() time.
1537 */
1538 struct brw_reg
1539 fs_visitor::interp_reg(int location, int channel)
1540 {
1541 int regnr = urb_setup[location] * 2 + channel / 2;
1542 int stride = (channel & 1) * 4;
1543
1544 assert(urb_setup[location] != -1);
1545
1546 return brw_vec1_grf(regnr, stride);
1547 }
1548
1549 /** Emits the interpolation for the varying inputs. */
1550 void
1551 fs_visitor::emit_interpolation_setup_gen4()
1552 {
1553 this->current_annotation = "compute pixel centers";
1554 this->pixel_x = fs_reg(this, glsl_type::uint_type);
1555 this->pixel_y = fs_reg(this, glsl_type::uint_type);
1556 this->pixel_x.type = BRW_REGISTER_TYPE_UW;
1557 this->pixel_y.type = BRW_REGISTER_TYPE_UW;
1558
1559 emit(FS_OPCODE_PIXEL_X, this->pixel_x);
1560 emit(FS_OPCODE_PIXEL_Y, this->pixel_y);
1561
1562 this->current_annotation = "compute pixel deltas from v0";
1563 if (brw->has_pln) {
1564 this->delta_x = fs_reg(this, glsl_type::vec2_type);
1565 this->delta_y = this->delta_x;
1566 this->delta_y.reg_offset++;
1567 } else {
1568 this->delta_x = fs_reg(this, glsl_type::float_type);
1569 this->delta_y = fs_reg(this, glsl_type::float_type);
1570 }
1571 emit(BRW_OPCODE_ADD, this->delta_x,
1572 this->pixel_x, fs_reg(negate(brw_vec1_grf(1, 0))));
1573 emit(BRW_OPCODE_ADD, this->delta_y,
1574 this->pixel_y, fs_reg(negate(brw_vec1_grf(1, 1))));
1575
1576 this->current_annotation = "compute pos.w and 1/pos.w";
1577 /* Compute wpos.w. It's always in our setup, since it's needed to
1578 * interpolate the other attributes.
1579 */
1580 this->wpos_w = fs_reg(this, glsl_type::float_type);
1581 emit(FS_OPCODE_LINTERP, wpos_w, this->delta_x, this->delta_y,
1582 interp_reg(FRAG_ATTRIB_WPOS, 3));
1583 /* Compute the pixel 1/W value from wpos.w. */
1584 this->pixel_w = fs_reg(this, glsl_type::float_type);
1585 emit_math(FS_OPCODE_RCP, this->pixel_w, wpos_w);
1586 this->current_annotation = NULL;
1587 }
1588
1589 /** Emits the interpolation for the varying inputs. */
1590 void
1591 fs_visitor::emit_interpolation_setup_gen6()
1592 {
1593 struct brw_reg g1_uw = retype(brw_vec1_grf(1, 0), BRW_REGISTER_TYPE_UW);
1594
1595 /* If the pixel centers end up used, the setup is the same as for gen4. */
1596 this->current_annotation = "compute pixel centers";
1597 fs_reg int_pixel_x = fs_reg(this, glsl_type::uint_type);
1598 fs_reg int_pixel_y = fs_reg(this, glsl_type::uint_type);
1599 int_pixel_x.type = BRW_REGISTER_TYPE_UW;
1600 int_pixel_y.type = BRW_REGISTER_TYPE_UW;
1601 emit(BRW_OPCODE_ADD,
1602 int_pixel_x,
1603 fs_reg(stride(suboffset(g1_uw, 4), 2, 4, 0)),
1604 fs_reg(brw_imm_v(0x10101010)));
1605 emit(BRW_OPCODE_ADD,
1606 int_pixel_y,
1607 fs_reg(stride(suboffset(g1_uw, 5), 2, 4, 0)),
1608 fs_reg(brw_imm_v(0x11001100)));
1609
1610 /* As of gen6, we can no longer mix float and int sources. We have
1611 * to turn the integer pixel centers into floats for their actual
1612 * use.
1613 */
1614 this->pixel_x = fs_reg(this, glsl_type::float_type);
1615 this->pixel_y = fs_reg(this, glsl_type::float_type);
1616 emit(BRW_OPCODE_MOV, this->pixel_x, int_pixel_x);
1617 emit(BRW_OPCODE_MOV, this->pixel_y, int_pixel_y);
1618
1619 this->current_annotation = "compute pos.w";
1620 this->pixel_w = fs_reg(brw_vec8_grf(c->source_w_reg, 0));
1621 this->wpos_w = fs_reg(this, glsl_type::float_type);
1622 emit_math(FS_OPCODE_RCP, this->wpos_w, this->pixel_w);
1623
1624 this->delta_x = fs_reg(brw_vec8_grf(2, 0));
1625 this->delta_y = fs_reg(brw_vec8_grf(3, 0));
1626
1627 this->current_annotation = NULL;
1628 }
1629
1630 void
1631 fs_visitor::emit_color_write(int index, int first_color_mrf, fs_reg color)
1632 {
1633 int reg_width = c->dispatch_width / 8;
1634
1635 if (c->dispatch_width == 8 || intel->gen == 6) {
1636 /* SIMD8 write looks like:
1637 * m + 0: r0
1638 * m + 1: r1
1639 * m + 2: g0
1640 * m + 3: g1
1641 *
1642 * gen6 SIMD16 DP write looks like:
1643 * m + 0: r0
1644 * m + 1: r1
1645 * m + 2: g0
1646 * m + 3: g1
1647 * m + 4: b0
1648 * m + 5: b1
1649 * m + 6: a0
1650 * m + 7: a1
1651 */
1652 emit(BRW_OPCODE_MOV, fs_reg(MRF, first_color_mrf + index * reg_width),
1653 color);
1654 } else {
1655 /* pre-gen6 SIMD16 single source DP write looks like:
1656 * m + 0: r0
1657 * m + 1: g0
1658 * m + 2: b0
1659 * m + 3: a0
1660 * m + 4: r1
1661 * m + 5: g1
1662 * m + 6: b1
1663 * m + 7: a1
1664 */
1665 if (brw->has_compr4) {
1666 /* By setting the high bit of the MRF register number, we
1667 * indicate that we want COMPR4 mode - instead of doing the
1668 * usual destination + 1 for the second half we get
1669 * destination + 4.
1670 */
1671 emit(BRW_OPCODE_MOV,
1672 fs_reg(MRF, BRW_MRF_COMPR4 + first_color_mrf + index), color);
1673 } else {
1674 push_force_uncompressed();
1675 emit(BRW_OPCODE_MOV, fs_reg(MRF, first_color_mrf + index), color);
1676 pop_force_uncompressed();
1677
1678 push_force_sechalf();
1679 color.sechalf = true;
1680 emit(BRW_OPCODE_MOV, fs_reg(MRF, first_color_mrf + index + 4), color);
1681 pop_force_sechalf();
1682 color.sechalf = false;
1683 }
1684 }
1685 }
1686
1687 void
1688 fs_visitor::emit_fb_writes()
1689 {
1690 this->current_annotation = "FB write header";
1691 GLboolean header_present = GL_TRUE;
1692 int nr = 0;
1693 int reg_width = c->dispatch_width / 8;
1694
1695 if (intel->gen >= 6 &&
1696 !this->kill_emitted &&
1697 c->key.nr_color_regions == 1) {
1698 header_present = false;
1699 }
1700
1701 if (header_present) {
1702 /* m0, m1 header */
1703 nr += 2;
1704 }
1705
1706 if (c->aa_dest_stencil_reg) {
1707 push_force_uncompressed();
1708 emit(BRW_OPCODE_MOV, fs_reg(MRF, nr++),
1709 fs_reg(brw_vec8_grf(c->aa_dest_stencil_reg, 0)));
1710 pop_force_uncompressed();
1711 }
1712
1713 /* Reserve space for color. It'll be filled in per MRT below. */
1714 int color_mrf = nr;
1715 nr += 4 * reg_width;
1716
1717 if (c->source_depth_to_render_target) {
1718 if (intel->gen == 6 && c->dispatch_width == 16) {
1719 /* For outputting oDepth on gen6, SIMD8 writes have to be
1720 * used. This would require 8-wide moves of each half to
1721 * message regs, kind of like pre-gen5 SIMD16 FB writes.
1722 * Just bail on doing so for now.
1723 */
1724 fail("Missing support for simd16 depth writes on gen6\n");
1725 }
1726
1727 if (c->computes_depth) {
1728 /* Hand over gl_FragDepth. */
1729 assert(this->frag_depth);
1730 fs_reg depth = *(variable_storage(this->frag_depth));
1731
1732 emit(BRW_OPCODE_MOV, fs_reg(MRF, nr), depth);
1733 } else {
1734 /* Pass through the payload depth. */
1735 emit(BRW_OPCODE_MOV, fs_reg(MRF, nr),
1736 fs_reg(brw_vec8_grf(c->source_depth_reg, 0)));
1737 }
1738 nr += reg_width;
1739 }
1740
1741 if (c->dest_depth_reg) {
1742 emit(BRW_OPCODE_MOV, fs_reg(MRF, nr),
1743 fs_reg(brw_vec8_grf(c->dest_depth_reg, 0)));
1744 nr += reg_width;
1745 }
1746
1747 fs_reg color = reg_undef;
1748 if (this->frag_color)
1749 color = *(variable_storage(this->frag_color));
1750 else if (this->frag_data) {
1751 color = *(variable_storage(this->frag_data));
1752 color.type = BRW_REGISTER_TYPE_F;
1753 }
1754
1755 for (int target = 0; target < c->key.nr_color_regions; target++) {
1756 this->current_annotation = ralloc_asprintf(this->mem_ctx,
1757 "FB write target %d",
1758 target);
1759 if (this->frag_color || this->frag_data) {
1760 for (int i = 0; i < 4; i++) {
1761 emit_color_write(i, color_mrf, color);
1762 color.reg_offset++;
1763 }
1764 }
1765
1766 if (this->frag_color)
1767 color.reg_offset -= 4;
1768
1769 fs_inst *inst = emit(FS_OPCODE_FB_WRITE);
1770 inst->target = target;
1771 inst->base_mrf = 0;
1772 inst->mlen = nr;
1773 if (target == c->key.nr_color_regions - 1)
1774 inst->eot = true;
1775 inst->header_present = header_present;
1776 }
1777
1778 if (c->key.nr_color_regions == 0) {
1779 if (c->key.alpha_test && (this->frag_color || this->frag_data)) {
1780 /* If the alpha test is enabled but there's no color buffer,
1781 * we still need to send alpha out the pipeline to our null
1782 * renderbuffer.
1783 */
1784 color.reg_offset += 3;
1785 emit_color_write(3, color_mrf, color);
1786 }
1787
1788 fs_inst *inst = emit(FS_OPCODE_FB_WRITE);
1789 inst->base_mrf = 0;
1790 inst->mlen = nr;
1791 inst->eot = true;
1792 inst->header_present = header_present;
1793 }
1794
1795 this->current_annotation = NULL;
1796 }