brw_fs_visitor.cpp revision c331b3123ecda127919458e24848b7c1596525ac
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 */
30extern "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
53void
54fs_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
108void
109fs_visitor::visit(ir_dereference_variable *ir)
110{
111   fs_reg *reg = variable_storage(ir->var);
112   this->result = *reg;
113}
114
115void
116fs_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
132void
133fs_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 */
157bool
158fs_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
176void
177fs_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
444void
445fs_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
488void
489fs_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
540fs_inst *
541fs_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 */
684fs_inst *
685fs_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   case ir_txf:
747      assert(!"GLSL 1.30 features unsupported");
748      break;
749   }
750   inst->base_mrf = base_mrf;
751   inst->mlen = mlen;
752   inst->header_present = header_present;
753
754   if (mlen > 11) {
755      fail("Message length >11 disallowed by hardware\n");
756   }
757
758   return inst;
759}
760
761fs_inst *
762fs_visitor::emit_texture_gen7(ir_texture *ir, fs_reg dst, fs_reg coordinate,
763			      int sampler)
764{
765   int mlen = 0;
766   int base_mrf = 2;
767   int reg_width = c->dispatch_width / 8;
768   bool header_present = false;
769
770   if (ir->offset) {
771      /* The offsets set up by the ir_texture visitor are in the
772       * m1 header, so we can't go headerless.
773       */
774      header_present = true;
775      mlen++;
776      base_mrf--;
777   }
778
779   if (ir->shadow_comparitor) {
780      ir->shadow_comparitor->accept(this);
781      emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen), this->result);
782      mlen += reg_width;
783   }
784
785   /* Set up the LOD info */
786   switch (ir->op) {
787   case ir_tex:
788      break;
789   case ir_txb:
790      ir->lod_info.bias->accept(this);
791      emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen), this->result);
792      mlen += reg_width;
793      break;
794   case ir_txl:
795      ir->lod_info.lod->accept(this);
796      emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen), this->result);
797      mlen += reg_width;
798      break;
799   case ir_txd:
800   case ir_txf:
801      assert(!"GLSL 1.30 features unsupported");
802      break;
803   }
804
805   /* Set up the coordinate */
806   for (int i = 0; i < ir->coordinate->type->vector_elements; i++) {
807      fs_inst *inst = emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen),
808			   coordinate);
809      if (i < 3 && c->key.gl_clamp_mask[i] & (1 << sampler))
810	 inst->saturate = true;
811      coordinate.reg_offset++;
812      mlen += reg_width;
813   }
814
815   /* Generate the SEND */
816   fs_inst *inst = NULL;
817   switch (ir->op) {
818   case ir_tex: inst = emit(FS_OPCODE_TEX, dst); break;
819   case ir_txb: inst = emit(FS_OPCODE_TXB, dst); break;
820   case ir_txl: inst = emit(FS_OPCODE_TXL, dst); break;
821   case ir_txd: inst = emit(FS_OPCODE_TXD, dst); break;
822   case ir_txf: assert(!"TXF unsupported.");
823   }
824   inst->base_mrf = base_mrf;
825   inst->mlen = mlen;
826   inst->header_present = header_present;
827
828   if (mlen > 11) {
829      fail("Message length >11 disallowed by hardware\n");
830   }
831
832   return inst;
833}
834
835void
836fs_visitor::visit(ir_texture *ir)
837{
838   int sampler;
839   fs_inst *inst = NULL;
840
841   this->result = reg_undef;
842   ir->coordinate->accept(this);
843   fs_reg coordinate = this->result;
844
845   if (ir->offset != NULL) {
846      ir_constant *offset = ir->offset->as_constant();
847      assert(offset != NULL);
848
849      signed char offsets[3];
850      for (unsigned i = 0; i < ir->offset->type->vector_elements; i++)
851	 offsets[i] = (signed char) offset->value.i[i];
852
853      /* Combine all three offsets into a single unsigned dword:
854       *
855       *    bits 11:8 - U Offset (X component)
856       *    bits  7:4 - V Offset (Y component)
857       *    bits  3:0 - R Offset (Z component)
858       */
859      unsigned offset_bits = 0;
860      for (unsigned i = 0; i < ir->offset->type->vector_elements; i++) {
861	 const unsigned shift = 4 * (2 - i);
862	 offset_bits |= (offsets[i] << shift) & (0xF << shift);
863      }
864
865      /* Explicitly set up the message header by copying g0 to msg reg m1. */
866      emit(BRW_OPCODE_MOV, fs_reg(MRF, 1, BRW_REGISTER_TYPE_UD),
867	   fs_reg(GRF, 0, BRW_REGISTER_TYPE_UD));
868
869      /* Then set the offset bits in DWord 2 of the message header. */
870      emit(BRW_OPCODE_MOV,
871	   fs_reg(retype(brw_vec1_reg(BRW_MESSAGE_REGISTER_FILE, 1, 2),
872			 BRW_REGISTER_TYPE_UD)),
873	   fs_reg(brw_imm_uw(offset_bits)));
874   }
875
876   /* Should be lowered by do_lower_texture_projection */
877   assert(!ir->projector);
878
879   sampler = _mesa_get_sampler_uniform_value(ir->sampler,
880					     prog,
881					     &fp->Base);
882   sampler = fp->Base.SamplerUnits[sampler];
883
884   /* The 965 requires the EU to do the normalization of GL rectangle
885    * texture coordinates.  We use the program parameter state
886    * tracking to get the scaling factor.
887    */
888   if (ir->sampler->type->sampler_dimensionality == GLSL_SAMPLER_DIM_RECT) {
889      struct gl_program_parameter_list *params = c->fp->program.Base.Parameters;
890      int tokens[STATE_LENGTH] = {
891	 STATE_INTERNAL,
892	 STATE_TEXRECT_SCALE,
893	 sampler,
894	 0,
895	 0
896      };
897
898      if (c->dispatch_width == 16) {
899	 fail("rectangle scale uniform setup not supported on 16-wide\n");
900	 this->result = fs_reg(this, ir->type);
901	 return;
902      }
903
904      c->prog_data.param_convert[c->prog_data.nr_params] =
905	 PARAM_NO_CONVERT;
906      c->prog_data.param_convert[c->prog_data.nr_params + 1] =
907	 PARAM_NO_CONVERT;
908
909      fs_reg scale_x = fs_reg(UNIFORM, c->prog_data.nr_params);
910      fs_reg scale_y = fs_reg(UNIFORM, c->prog_data.nr_params + 1);
911      GLuint index = _mesa_add_state_reference(params,
912					       (gl_state_index *)tokens);
913
914      this->param_index[c->prog_data.nr_params] = index;
915      this->param_offset[c->prog_data.nr_params] = 0;
916      c->prog_data.nr_params++;
917      this->param_index[c->prog_data.nr_params] = index;
918      this->param_offset[c->prog_data.nr_params] = 1;
919      c->prog_data.nr_params++;
920
921      fs_reg dst = fs_reg(this, ir->coordinate->type);
922      fs_reg src = coordinate;
923      coordinate = dst;
924
925      emit(BRW_OPCODE_MUL, dst, src, scale_x);
926      dst.reg_offset++;
927      src.reg_offset++;
928      emit(BRW_OPCODE_MUL, dst, src, scale_y);
929   }
930
931   /* Writemasking doesn't eliminate channels on SIMD8 texture
932    * samples, so don't worry about them.
933    */
934   fs_reg dst = fs_reg(this, glsl_type::vec4_type);
935
936   if (intel->gen >= 7) {
937      inst = emit_texture_gen7(ir, dst, coordinate, sampler);
938   } else if (intel->gen >= 5) {
939      inst = emit_texture_gen5(ir, dst, coordinate, sampler);
940   } else {
941      inst = emit_texture_gen4(ir, dst, coordinate, sampler);
942   }
943
944   /* If there's an offset, we already set up m1.  To avoid the implied move,
945    * use the null register.  Otherwise, we want an implied move from g0.
946    */
947   if (ir->offset != NULL || !inst->header_present)
948      inst->src[0] = reg_undef;
949   else
950      inst->src[0] = fs_reg(retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UW));
951
952   inst->sampler = sampler;
953
954   this->result = dst;
955
956   if (ir->shadow_comparitor)
957      inst->shadow_compare = true;
958
959   if (ir->type == glsl_type::float_type) {
960      /* Ignore DEPTH_TEXTURE_MODE swizzling. */
961      assert(ir->sampler->type->sampler_shadow);
962   } else if (c->key.tex_swizzles[inst->sampler] != SWIZZLE_NOOP) {
963      fs_reg swizzle_dst = fs_reg(this, glsl_type::vec4_type);
964
965      for (int i = 0; i < 4; i++) {
966	 int swiz = GET_SWZ(c->key.tex_swizzles[inst->sampler], i);
967	 fs_reg l = swizzle_dst;
968	 l.reg_offset += i;
969
970	 if (swiz == SWIZZLE_ZERO) {
971	    emit(BRW_OPCODE_MOV, l, fs_reg(0.0f));
972	 } else if (swiz == SWIZZLE_ONE) {
973	    emit(BRW_OPCODE_MOV, l, fs_reg(1.0f));
974	 } else {
975	    fs_reg r = dst;
976	    r.reg_offset += GET_SWZ(c->key.tex_swizzles[inst->sampler], i);
977	    emit(BRW_OPCODE_MOV, l, r);
978	 }
979      }
980      this->result = swizzle_dst;
981   }
982}
983
984void
985fs_visitor::visit(ir_swizzle *ir)
986{
987   this->result = reg_undef;
988   ir->val->accept(this);
989   fs_reg val = this->result;
990
991   if (ir->type->vector_elements == 1) {
992      this->result.reg_offset += ir->mask.x;
993      return;
994   }
995
996   fs_reg result = fs_reg(this, ir->type);
997   this->result = result;
998
999   for (unsigned int i = 0; i < ir->type->vector_elements; i++) {
1000      fs_reg channel = val;
1001      int swiz = 0;
1002
1003      switch (i) {
1004      case 0:
1005	 swiz = ir->mask.x;
1006	 break;
1007      case 1:
1008	 swiz = ir->mask.y;
1009	 break;
1010      case 2:
1011	 swiz = ir->mask.z;
1012	 break;
1013      case 3:
1014	 swiz = ir->mask.w;
1015	 break;
1016      }
1017
1018      channel.reg_offset += swiz;
1019      emit(BRW_OPCODE_MOV, result, channel);
1020      result.reg_offset++;
1021   }
1022}
1023
1024void
1025fs_visitor::visit(ir_discard *ir)
1026{
1027   assert(ir->condition == NULL); /* FINISHME */
1028
1029   emit(FS_OPCODE_DISCARD);
1030   kill_emitted = true;
1031}
1032
1033void
1034fs_visitor::visit(ir_constant *ir)
1035{
1036   /* Set this->result to reg at the bottom of the function because some code
1037    * paths will cause this visitor to be applied to other fields.  This will
1038    * cause the value stored in this->result to be modified.
1039    *
1040    * Make reg constant so that it doesn't get accidentally modified along the
1041    * way.  Yes, I actually had this problem. :(
1042    */
1043   const fs_reg reg(this, ir->type);
1044   fs_reg dst_reg = reg;
1045
1046   if (ir->type->is_array()) {
1047      const unsigned size = type_size(ir->type->fields.array);
1048
1049      for (unsigned i = 0; i < ir->type->length; i++) {
1050	 this->result = reg_undef;
1051	 ir->array_elements[i]->accept(this);
1052	 fs_reg src_reg = this->result;
1053
1054	 dst_reg.type = src_reg.type;
1055	 for (unsigned j = 0; j < size; j++) {
1056	    emit(BRW_OPCODE_MOV, dst_reg, src_reg);
1057	    src_reg.reg_offset++;
1058	    dst_reg.reg_offset++;
1059	 }
1060      }
1061   } else if (ir->type->is_record()) {
1062      foreach_list(node, &ir->components) {
1063	 ir_instruction *const field = (ir_instruction *) node;
1064	 const unsigned size = type_size(field->type);
1065
1066	 this->result = reg_undef;
1067	 field->accept(this);
1068	 fs_reg src_reg = this->result;
1069
1070	 dst_reg.type = src_reg.type;
1071	 for (unsigned j = 0; j < size; j++) {
1072	    emit(BRW_OPCODE_MOV, dst_reg, src_reg);
1073	    src_reg.reg_offset++;
1074	    dst_reg.reg_offset++;
1075	 }
1076      }
1077   } else {
1078      const unsigned size = type_size(ir->type);
1079
1080      for (unsigned i = 0; i < size; i++) {
1081	 switch (ir->type->base_type) {
1082	 case GLSL_TYPE_FLOAT:
1083	    emit(BRW_OPCODE_MOV, dst_reg, fs_reg(ir->value.f[i]));
1084	    break;
1085	 case GLSL_TYPE_UINT:
1086	    emit(BRW_OPCODE_MOV, dst_reg, fs_reg(ir->value.u[i]));
1087	    break;
1088	 case GLSL_TYPE_INT:
1089	    emit(BRW_OPCODE_MOV, dst_reg, fs_reg(ir->value.i[i]));
1090	    break;
1091	 case GLSL_TYPE_BOOL:
1092	    emit(BRW_OPCODE_MOV, dst_reg, fs_reg((int)ir->value.b[i]));
1093	    break;
1094	 default:
1095	    assert(!"Non-float/uint/int/bool constant");
1096	 }
1097	 dst_reg.reg_offset++;
1098      }
1099   }
1100
1101   this->result = reg;
1102}
1103
1104void
1105fs_visitor::emit_bool_to_cond_code(ir_rvalue *ir)
1106{
1107   ir_expression *expr = ir->as_expression();
1108
1109   if (expr) {
1110      fs_reg op[2];
1111      fs_inst *inst;
1112
1113      assert(expr->get_num_operands() <= 2);
1114      for (unsigned int i = 0; i < expr->get_num_operands(); i++) {
1115	 assert(expr->operands[i]->type->is_scalar());
1116
1117	 this->result = reg_undef;
1118	 expr->operands[i]->accept(this);
1119	 op[i] = this->result;
1120      }
1121
1122      switch (expr->operation) {
1123      case ir_unop_logic_not:
1124	 inst = emit(BRW_OPCODE_AND, reg_null_d, op[0], fs_reg(1));
1125	 inst->conditional_mod = BRW_CONDITIONAL_Z;
1126	 break;
1127
1128      case ir_binop_logic_xor:
1129	 inst = emit(BRW_OPCODE_XOR, reg_null_d, op[0], op[1]);
1130	 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1131	 break;
1132
1133      case ir_binop_logic_or:
1134	 inst = emit(BRW_OPCODE_OR, reg_null_d, op[0], op[1]);
1135	 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1136	 break;
1137
1138      case ir_binop_logic_and:
1139	 inst = emit(BRW_OPCODE_AND, reg_null_d, op[0], op[1]);
1140	 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1141	 break;
1142
1143      case ir_unop_f2b:
1144	 if (intel->gen >= 6) {
1145	    inst = emit(BRW_OPCODE_CMP, reg_null_d, op[0], fs_reg(0.0f));
1146	 } else {
1147	    inst = emit(BRW_OPCODE_MOV, reg_null_f, op[0]);
1148	 }
1149	 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1150	 break;
1151
1152      case ir_unop_i2b:
1153	 if (intel->gen >= 6) {
1154	    inst = emit(BRW_OPCODE_CMP, reg_null_d, op[0], fs_reg(0));
1155	 } else {
1156	    inst = emit(BRW_OPCODE_MOV, reg_null_d, op[0]);
1157	 }
1158	 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1159	 break;
1160
1161      case ir_binop_greater:
1162      case ir_binop_gequal:
1163      case ir_binop_less:
1164      case ir_binop_lequal:
1165      case ir_binop_equal:
1166      case ir_binop_all_equal:
1167      case ir_binop_nequal:
1168      case ir_binop_any_nequal:
1169	 inst = emit(BRW_OPCODE_CMP, reg_null_cmp, op[0], op[1]);
1170	 inst->conditional_mod =
1171	    brw_conditional_for_comparison(expr->operation);
1172	 break;
1173
1174      default:
1175	 assert(!"not reached");
1176	 fail("bad cond code\n");
1177	 break;
1178      }
1179      return;
1180   }
1181
1182   this->result = reg_undef;
1183   ir->accept(this);
1184
1185   if (intel->gen >= 6) {
1186      fs_inst *inst = emit(BRW_OPCODE_AND, reg_null_d, this->result, fs_reg(1));
1187      inst->conditional_mod = BRW_CONDITIONAL_NZ;
1188   } else {
1189      fs_inst *inst = emit(BRW_OPCODE_MOV, reg_null_d, this->result);
1190      inst->conditional_mod = BRW_CONDITIONAL_NZ;
1191   }
1192}
1193
1194/**
1195 * Emit a gen6 IF statement with the comparison folded into the IF
1196 * instruction.
1197 */
1198void
1199fs_visitor::emit_if_gen6(ir_if *ir)
1200{
1201   ir_expression *expr = ir->condition->as_expression();
1202
1203   if (expr) {
1204      fs_reg op[2];
1205      fs_inst *inst;
1206      fs_reg temp;
1207
1208      assert(expr->get_num_operands() <= 2);
1209      for (unsigned int i = 0; i < expr->get_num_operands(); i++) {
1210	 assert(expr->operands[i]->type->is_scalar());
1211
1212	 this->result = reg_undef;
1213	 expr->operands[i]->accept(this);
1214	 op[i] = this->result;
1215      }
1216
1217      switch (expr->operation) {
1218      case ir_unop_logic_not:
1219	 inst = emit(BRW_OPCODE_IF, temp, op[0], fs_reg(0));
1220	 inst->conditional_mod = BRW_CONDITIONAL_Z;
1221	 return;
1222
1223      case ir_binop_logic_xor:
1224	 inst = emit(BRW_OPCODE_IF, reg_null_d, op[0], op[1]);
1225	 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1226	 return;
1227
1228      case ir_binop_logic_or:
1229	 temp = fs_reg(this, glsl_type::bool_type);
1230	 emit(BRW_OPCODE_OR, temp, op[0], op[1]);
1231	 inst = emit(BRW_OPCODE_IF, reg_null_d, temp, fs_reg(0));
1232	 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1233	 return;
1234
1235      case ir_binop_logic_and:
1236	 temp = fs_reg(this, glsl_type::bool_type);
1237	 emit(BRW_OPCODE_AND, temp, op[0], op[1]);
1238	 inst = emit(BRW_OPCODE_IF, reg_null_d, temp, fs_reg(0));
1239	 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1240	 return;
1241
1242      case ir_unop_f2b:
1243	 inst = emit(BRW_OPCODE_IF, reg_null_f, op[0], fs_reg(0));
1244	 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1245	 return;
1246
1247      case ir_unop_i2b:
1248	 inst = emit(BRW_OPCODE_IF, reg_null_d, op[0], fs_reg(0));
1249	 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1250	 return;
1251
1252      case ir_binop_greater:
1253      case ir_binop_gequal:
1254      case ir_binop_less:
1255      case ir_binop_lequal:
1256      case ir_binop_equal:
1257      case ir_binop_all_equal:
1258      case ir_binop_nequal:
1259      case ir_binop_any_nequal:
1260	 inst = emit(BRW_OPCODE_IF, reg_null_d, op[0], op[1]);
1261	 inst->conditional_mod =
1262	    brw_conditional_for_comparison(expr->operation);
1263	 return;
1264      default:
1265	 assert(!"not reached");
1266	 inst = emit(BRW_OPCODE_IF, reg_null_d, op[0], fs_reg(0));
1267	 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1268	 fail("bad condition\n");
1269	 return;
1270      }
1271      return;
1272   }
1273
1274   this->result = reg_undef;
1275   ir->condition->accept(this);
1276
1277   fs_inst *inst = emit(BRW_OPCODE_IF, reg_null_d, this->result, fs_reg(0));
1278   inst->conditional_mod = BRW_CONDITIONAL_NZ;
1279}
1280
1281void
1282fs_visitor::visit(ir_if *ir)
1283{
1284   fs_inst *inst;
1285
1286   if (intel->gen != 6 && c->dispatch_width == 16) {
1287      fail("Can't support (non-uniform) control flow on 16-wide\n");
1288   }
1289
1290   /* Don't point the annotation at the if statement, because then it plus
1291    * the then and else blocks get printed.
1292    */
1293   this->base_ir = ir->condition;
1294
1295   if (intel->gen == 6) {
1296      emit_if_gen6(ir);
1297   } else {
1298      emit_bool_to_cond_code(ir->condition);
1299
1300      inst = emit(BRW_OPCODE_IF);
1301      inst->predicated = true;
1302   }
1303
1304   foreach_iter(exec_list_iterator, iter, ir->then_instructions) {
1305      ir_instruction *ir = (ir_instruction *)iter.get();
1306      this->base_ir = ir;
1307      this->result = reg_undef;
1308      ir->accept(this);
1309   }
1310
1311   if (!ir->else_instructions.is_empty()) {
1312      emit(BRW_OPCODE_ELSE);
1313
1314      foreach_iter(exec_list_iterator, iter, ir->else_instructions) {
1315	 ir_instruction *ir = (ir_instruction *)iter.get();
1316	 this->base_ir = ir;
1317	 this->result = reg_undef;
1318	 ir->accept(this);
1319      }
1320   }
1321
1322   emit(BRW_OPCODE_ENDIF);
1323}
1324
1325void
1326fs_visitor::visit(ir_loop *ir)
1327{
1328   fs_reg counter = reg_undef;
1329
1330   if (c->dispatch_width == 16) {
1331      fail("Can't support (non-uniform) control flow on 16-wide\n");
1332   }
1333
1334   if (ir->counter) {
1335      this->base_ir = ir->counter;
1336      ir->counter->accept(this);
1337      counter = *(variable_storage(ir->counter));
1338
1339      if (ir->from) {
1340	 this->result = counter;
1341
1342	 this->base_ir = ir->from;
1343	 this->result = counter;
1344	 ir->from->accept(this);
1345
1346	 if (!this->result.equals(&counter))
1347	    emit(BRW_OPCODE_MOV, counter, this->result);
1348      }
1349   }
1350
1351   emit(BRW_OPCODE_DO);
1352
1353   if (ir->to) {
1354      this->base_ir = ir->to;
1355      this->result = reg_undef;
1356      ir->to->accept(this);
1357
1358      fs_inst *inst = emit(BRW_OPCODE_CMP, reg_null_cmp, counter, this->result);
1359      inst->conditional_mod = brw_conditional_for_comparison(ir->cmp);
1360
1361      inst = emit(BRW_OPCODE_BREAK);
1362      inst->predicated = true;
1363   }
1364
1365   foreach_iter(exec_list_iterator, iter, ir->body_instructions) {
1366      ir_instruction *ir = (ir_instruction *)iter.get();
1367
1368      this->base_ir = ir;
1369      this->result = reg_undef;
1370      ir->accept(this);
1371   }
1372
1373   if (ir->increment) {
1374      this->base_ir = ir->increment;
1375      this->result = reg_undef;
1376      ir->increment->accept(this);
1377      emit(BRW_OPCODE_ADD, counter, counter, this->result);
1378   }
1379
1380   emit(BRW_OPCODE_WHILE);
1381}
1382
1383void
1384fs_visitor::visit(ir_loop_jump *ir)
1385{
1386   switch (ir->mode) {
1387   case ir_loop_jump::jump_break:
1388      emit(BRW_OPCODE_BREAK);
1389      break;
1390   case ir_loop_jump::jump_continue:
1391      emit(BRW_OPCODE_CONTINUE);
1392      break;
1393   }
1394}
1395
1396void
1397fs_visitor::visit(ir_call *ir)
1398{
1399   assert(!"FINISHME");
1400}
1401
1402void
1403fs_visitor::visit(ir_return *ir)
1404{
1405   assert(!"FINISHME");
1406}
1407
1408void
1409fs_visitor::visit(ir_function *ir)
1410{
1411   /* Ignore function bodies other than main() -- we shouldn't see calls to
1412    * them since they should all be inlined before we get to ir_to_mesa.
1413    */
1414   if (strcmp(ir->name, "main") == 0) {
1415      const ir_function_signature *sig;
1416      exec_list empty;
1417
1418      sig = ir->matching_signature(&empty);
1419
1420      assert(sig);
1421
1422      foreach_iter(exec_list_iterator, iter, sig->body) {
1423	 ir_instruction *ir = (ir_instruction *)iter.get();
1424	 this->base_ir = ir;
1425	 this->result = reg_undef;
1426	 ir->accept(this);
1427      }
1428   }
1429}
1430
1431void
1432fs_visitor::visit(ir_function_signature *ir)
1433{
1434   assert(!"not reached");
1435   (void)ir;
1436}
1437
1438fs_inst *
1439fs_visitor::emit(fs_inst inst)
1440{
1441   fs_inst *list_inst = new(mem_ctx) fs_inst;
1442   *list_inst = inst;
1443
1444   if (force_uncompressed_stack > 0)
1445      list_inst->force_uncompressed = true;
1446   else if (force_sechalf_stack > 0)
1447      list_inst->force_sechalf = true;
1448
1449   list_inst->annotation = this->current_annotation;
1450   list_inst->ir = this->base_ir;
1451
1452   this->instructions.push_tail(list_inst);
1453
1454   return list_inst;
1455}
1456
1457/** Emits a dummy fragment shader consisting of magenta for bringup purposes. */
1458void
1459fs_visitor::emit_dummy_fs()
1460{
1461   /* Everyone's favorite color. */
1462   emit(BRW_OPCODE_MOV, fs_reg(MRF, 2), fs_reg(1.0f));
1463   emit(BRW_OPCODE_MOV, fs_reg(MRF, 3), fs_reg(0.0f));
1464   emit(BRW_OPCODE_MOV, fs_reg(MRF, 4), fs_reg(1.0f));
1465   emit(BRW_OPCODE_MOV, fs_reg(MRF, 5), fs_reg(0.0f));
1466
1467   fs_inst *write;
1468   write = emit(FS_OPCODE_FB_WRITE, fs_reg(0), fs_reg(0));
1469   write->base_mrf = 0;
1470}
1471
1472/* The register location here is relative to the start of the URB
1473 * data.  It will get adjusted to be a real location before
1474 * generate_code() time.
1475 */
1476struct brw_reg
1477fs_visitor::interp_reg(int location, int channel)
1478{
1479   int regnr = urb_setup[location] * 2 + channel / 2;
1480   int stride = (channel & 1) * 4;
1481
1482   assert(urb_setup[location] != -1);
1483
1484   return brw_vec1_grf(regnr, stride);
1485}
1486
1487/** Emits the interpolation for the varying inputs. */
1488void
1489fs_visitor::emit_interpolation_setup_gen4()
1490{
1491   this->current_annotation = "compute pixel centers";
1492   this->pixel_x = fs_reg(this, glsl_type::uint_type);
1493   this->pixel_y = fs_reg(this, glsl_type::uint_type);
1494   this->pixel_x.type = BRW_REGISTER_TYPE_UW;
1495   this->pixel_y.type = BRW_REGISTER_TYPE_UW;
1496
1497   emit(FS_OPCODE_PIXEL_X, this->pixel_x);
1498   emit(FS_OPCODE_PIXEL_Y, this->pixel_y);
1499
1500   this->current_annotation = "compute pixel deltas from v0";
1501   if (brw->has_pln) {
1502      this->delta_x = fs_reg(this, glsl_type::vec2_type);
1503      this->delta_y = this->delta_x;
1504      this->delta_y.reg_offset++;
1505   } else {
1506      this->delta_x = fs_reg(this, glsl_type::float_type);
1507      this->delta_y = fs_reg(this, glsl_type::float_type);
1508   }
1509   emit(BRW_OPCODE_ADD, this->delta_x,
1510	this->pixel_x, fs_reg(negate(brw_vec1_grf(1, 0))));
1511   emit(BRW_OPCODE_ADD, this->delta_y,
1512	this->pixel_y, fs_reg(negate(brw_vec1_grf(1, 1))));
1513
1514   this->current_annotation = "compute pos.w and 1/pos.w";
1515   /* Compute wpos.w.  It's always in our setup, since it's needed to
1516    * interpolate the other attributes.
1517    */
1518   this->wpos_w = fs_reg(this, glsl_type::float_type);
1519   emit(FS_OPCODE_LINTERP, wpos_w, this->delta_x, this->delta_y,
1520	interp_reg(FRAG_ATTRIB_WPOS, 3));
1521   /* Compute the pixel 1/W value from wpos.w. */
1522   this->pixel_w = fs_reg(this, glsl_type::float_type);
1523   emit_math(FS_OPCODE_RCP, this->pixel_w, wpos_w);
1524   this->current_annotation = NULL;
1525}
1526
1527/** Emits the interpolation for the varying inputs. */
1528void
1529fs_visitor::emit_interpolation_setup_gen6()
1530{
1531   struct brw_reg g1_uw = retype(brw_vec1_grf(1, 0), BRW_REGISTER_TYPE_UW);
1532
1533   /* If the pixel centers end up used, the setup is the same as for gen4. */
1534   this->current_annotation = "compute pixel centers";
1535   fs_reg int_pixel_x = fs_reg(this, glsl_type::uint_type);
1536   fs_reg int_pixel_y = fs_reg(this, glsl_type::uint_type);
1537   int_pixel_x.type = BRW_REGISTER_TYPE_UW;
1538   int_pixel_y.type = BRW_REGISTER_TYPE_UW;
1539   emit(BRW_OPCODE_ADD,
1540	int_pixel_x,
1541	fs_reg(stride(suboffset(g1_uw, 4), 2, 4, 0)),
1542	fs_reg(brw_imm_v(0x10101010)));
1543   emit(BRW_OPCODE_ADD,
1544	int_pixel_y,
1545	fs_reg(stride(suboffset(g1_uw, 5), 2, 4, 0)),
1546	fs_reg(brw_imm_v(0x11001100)));
1547
1548   /* As of gen6, we can no longer mix float and int sources.  We have
1549    * to turn the integer pixel centers into floats for their actual
1550    * use.
1551    */
1552   this->pixel_x = fs_reg(this, glsl_type::float_type);
1553   this->pixel_y = fs_reg(this, glsl_type::float_type);
1554   emit(BRW_OPCODE_MOV, this->pixel_x, int_pixel_x);
1555   emit(BRW_OPCODE_MOV, this->pixel_y, int_pixel_y);
1556
1557   this->current_annotation = "compute pos.w";
1558   this->pixel_w = fs_reg(brw_vec8_grf(c->source_w_reg, 0));
1559   this->wpos_w = fs_reg(this, glsl_type::float_type);
1560   emit_math(FS_OPCODE_RCP, this->wpos_w, this->pixel_w);
1561
1562   this->delta_x = fs_reg(brw_vec8_grf(2, 0));
1563   this->delta_y = fs_reg(brw_vec8_grf(3, 0));
1564
1565   this->current_annotation = NULL;
1566}
1567
1568void
1569fs_visitor::emit_color_write(int index, int first_color_mrf, fs_reg color)
1570{
1571   int reg_width = c->dispatch_width / 8;
1572
1573   if (c->dispatch_width == 8 || intel->gen == 6) {
1574      /* SIMD8 write looks like:
1575       * m + 0: r0
1576       * m + 1: r1
1577       * m + 2: g0
1578       * m + 3: g1
1579       *
1580       * gen6 SIMD16 DP write looks like:
1581       * m + 0: r0
1582       * m + 1: r1
1583       * m + 2: g0
1584       * m + 3: g1
1585       * m + 4: b0
1586       * m + 5: b1
1587       * m + 6: a0
1588       * m + 7: a1
1589       */
1590      emit(BRW_OPCODE_MOV, fs_reg(MRF, first_color_mrf + index * reg_width),
1591	   color);
1592   } else {
1593      /* pre-gen6 SIMD16 single source DP write looks like:
1594       * m + 0: r0
1595       * m + 1: g0
1596       * m + 2: b0
1597       * m + 3: a0
1598       * m + 4: r1
1599       * m + 5: g1
1600       * m + 6: b1
1601       * m + 7: a1
1602       */
1603      if (brw->has_compr4) {
1604	 /* By setting the high bit of the MRF register number, we
1605	  * indicate that we want COMPR4 mode - instead of doing the
1606	  * usual destination + 1 for the second half we get
1607	  * destination + 4.
1608	  */
1609	 emit(BRW_OPCODE_MOV,
1610	      fs_reg(MRF, BRW_MRF_COMPR4 + first_color_mrf + index), color);
1611      } else {
1612	 push_force_uncompressed();
1613	 emit(BRW_OPCODE_MOV, fs_reg(MRF, first_color_mrf + index), color);
1614	 pop_force_uncompressed();
1615
1616	 push_force_sechalf();
1617	 color.sechalf = true;
1618	 emit(BRW_OPCODE_MOV, fs_reg(MRF, first_color_mrf + index + 4), color);
1619	 pop_force_sechalf();
1620	 color.sechalf = false;
1621      }
1622   }
1623}
1624
1625void
1626fs_visitor::emit_fb_writes()
1627{
1628   this->current_annotation = "FB write header";
1629   GLboolean header_present = GL_TRUE;
1630   int nr = 0;
1631   int reg_width = c->dispatch_width / 8;
1632
1633   if (intel->gen >= 6 &&
1634       !this->kill_emitted &&
1635       c->key.nr_color_regions == 1) {
1636      header_present = false;
1637   }
1638
1639   if (header_present) {
1640      /* m0, m1 header */
1641      nr += 2;
1642   }
1643
1644   if (c->aa_dest_stencil_reg) {
1645      push_force_uncompressed();
1646      emit(BRW_OPCODE_MOV, fs_reg(MRF, nr++),
1647	   fs_reg(brw_vec8_grf(c->aa_dest_stencil_reg, 0)));
1648      pop_force_uncompressed();
1649   }
1650
1651   /* Reserve space for color. It'll be filled in per MRT below. */
1652   int color_mrf = nr;
1653   nr += 4 * reg_width;
1654
1655   if (c->source_depth_to_render_target) {
1656      if (intel->gen == 6 && c->dispatch_width == 16) {
1657	 /* For outputting oDepth on gen6, SIMD8 writes have to be
1658	  * used.  This would require 8-wide moves of each half to
1659	  * message regs, kind of like pre-gen5 SIMD16 FB writes.
1660	  * Just bail on doing so for now.
1661	  */
1662	 fail("Missing support for simd16 depth writes on gen6\n");
1663      }
1664
1665      if (c->computes_depth) {
1666	 /* Hand over gl_FragDepth. */
1667	 assert(this->frag_depth);
1668	 fs_reg depth = *(variable_storage(this->frag_depth));
1669
1670	 emit(BRW_OPCODE_MOV, fs_reg(MRF, nr), depth);
1671      } else {
1672	 /* Pass through the payload depth. */
1673	 emit(BRW_OPCODE_MOV, fs_reg(MRF, nr),
1674	      fs_reg(brw_vec8_grf(c->source_depth_reg, 0)));
1675      }
1676      nr += reg_width;
1677   }
1678
1679   if (c->dest_depth_reg) {
1680      emit(BRW_OPCODE_MOV, fs_reg(MRF, nr),
1681	   fs_reg(brw_vec8_grf(c->dest_depth_reg, 0)));
1682      nr += reg_width;
1683   }
1684
1685   fs_reg color = reg_undef;
1686   if (this->frag_color)
1687      color = *(variable_storage(this->frag_color));
1688   else if (this->frag_data) {
1689      color = *(variable_storage(this->frag_data));
1690      color.type = BRW_REGISTER_TYPE_F;
1691   }
1692
1693   for (int target = 0; target < c->key.nr_color_regions; target++) {
1694      this->current_annotation = ralloc_asprintf(this->mem_ctx,
1695						 "FB write target %d",
1696						 target);
1697      if (this->frag_color || this->frag_data) {
1698	 for (int i = 0; i < 4; i++) {
1699	    emit_color_write(i, color_mrf, color);
1700	    color.reg_offset++;
1701	 }
1702      }
1703
1704      if (this->frag_color)
1705	 color.reg_offset -= 4;
1706
1707      fs_inst *inst = emit(FS_OPCODE_FB_WRITE);
1708      inst->target = target;
1709      inst->base_mrf = 0;
1710      inst->mlen = nr;
1711      if (target == c->key.nr_color_regions - 1)
1712	 inst->eot = true;
1713      inst->header_present = header_present;
1714   }
1715
1716   if (c->key.nr_color_regions == 0) {
1717      if (c->key.alpha_test && (this->frag_color || this->frag_data)) {
1718	 /* If the alpha test is enabled but there's no color buffer,
1719	  * we still need to send alpha out the pipeline to our null
1720	  * renderbuffer.
1721	  */
1722	 color.reg_offset += 3;
1723	 emit_color_write(3, color_mrf, color);
1724      }
1725
1726      fs_inst *inst = emit(FS_OPCODE_FB_WRITE);
1727      inst->base_mrf = 0;
1728      inst->mlen = nr;
1729      inst->eot = true;
1730      inst->header_present = header_present;
1731   }
1732
1733   this->current_annotation = NULL;
1734}
1735