Tizen 2.0 Release
[profile/ivi/osmesa.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 && ir->op != ir_txd) {
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       this->result = reg_undef;
599       ir->lod_info.grad.dPdx->accept(this);
600       fs_reg dPdx = this->result;
601
602       this->result = reg_undef;
603       ir->lod_info.grad.dPdy->accept(this);
604       fs_reg dPdy = this->result;
605
606       for (int i = 0; i < ir->coordinate->type->vector_elements; i++) {
607          emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen + i), coordinate);
608          coordinate.reg_offset++;
609       }
610       /* the slots for u and v are always present, but r is optional */
611       mlen += MAX2(ir->coordinate->type->vector_elements, 2);
612
613       /*  P   = u, v, r
614        * dPdx = dudx, dvdx, drdx
615        * dPdy = dudy, dvdy, drdy
616        *
617        * 1-arg: Does not exist.
618        *
619        * 2-arg: dudx   dvdx   dudy   dvdy
620        *        dPdx.x dPdx.y dPdy.x dPdy.y
621        *        m4     m5     m6     m7
622        *
623        * 3-arg: dudx   dvdx   drdx   dudy   dvdy   drdy
624        *        dPdx.x dPdx.y dPdx.z dPdy.x dPdy.y dPdy.z
625        *        m5     m6     m7     m8     m9     m10
626        */
627       for (int i = 0; i < ir->lod_info.grad.dPdx->type->vector_elements; i++) {
628          emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen), dPdx);
629          dPdx.reg_offset++;
630       }
631       mlen += MAX2(ir->lod_info.grad.dPdx->type->vector_elements, 2);
632
633       for (int i = 0; i < ir->lod_info.grad.dPdy->type->vector_elements; i++) {
634          emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen), dPdy);
635          dPdy.reg_offset++;
636       }
637       mlen += MAX2(ir->lod_info.grad.dPdy->type->vector_elements, 2);
638    } else {
639       /* Oh joy.  gen4 doesn't have SIMD8 non-shadow-compare bias/lod
640        * instructions.  We'll need to do SIMD16 here.
641        */
642       assert(ir->op == ir_txb || ir->op == ir_txl);
643
644       for (int i = 0; i < ir->coordinate->type->vector_elements; i++) {
645          fs_inst *inst = emit(BRW_OPCODE_MOV, fs_reg(MRF,
646                                                      base_mrf + mlen + i * 2),
647                               coordinate);
648          if (i < 3 && c->key.gl_clamp_mask[i] & (1 << sampler))
649             inst->saturate = true;
650          coordinate.reg_offset++;
651       }
652
653       /* lod/bias appears after u/v/r. */
654       mlen += 6;
655
656       if (ir->op == ir_txb) {
657          this->result = reg_undef;
658          ir->lod_info.bias->accept(this);
659          emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen), this->result);
660          mlen++;
661       } else {
662          this->result = reg_undef;
663          ir->lod_info.lod->accept(this);
664          emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen), this->result);
665          mlen++;
666       }
667
668       /* The unused upper half. */
669       mlen++;
670
671       /* Now, since we're doing simd16, the return is 2 interleaved
672        * vec4s where the odd-indexed ones are junk. We'll need to move
673        * this weirdness around to the expected layout.
674        */
675       simd16 = true;
676       orig_dst = dst;
677       dst = fs_reg(this, glsl_type::get_array_instance(glsl_type::vec4_type,
678                                                        2));
679       dst.type = BRW_REGISTER_TYPE_F;
680    }
681
682    fs_inst *inst = NULL;
683    switch (ir->op) {
684    case ir_tex:
685       inst = emit(FS_OPCODE_TEX, dst);
686       break;
687    case ir_txb:
688       inst = emit(FS_OPCODE_TXB, dst);
689       break;
690    case ir_txl:
691       inst = emit(FS_OPCODE_TXL, dst);
692       break;
693    case ir_txd:
694       inst = emit(FS_OPCODE_TXD, dst);
695       break;
696    case ir_txf:
697       assert(!"GLSL 1.30 features unsupported");
698       break;
699    }
700    inst->base_mrf = base_mrf;
701    inst->mlen = mlen;
702    inst->header_present = true;
703
704    if (simd16) {
705       for (int i = 0; i < 4; i++) {
706          emit(BRW_OPCODE_MOV, orig_dst, dst);
707          orig_dst.reg_offset++;
708          dst.reg_offset += 2;
709       }
710    }
711
712    return inst;
713 }
714
715 /* gen5's sampler has slots for u, v, r, array index, then optional
716  * parameters like shadow comparitor or LOD bias.  If optional
717  * parameters aren't present, those base slots are optional and don't
718  * need to be included in the message.
719  *
720  * We don't fill in the unnecessary slots regardless, which may look
721  * surprising in the disassembly.
722  */
723 fs_inst *
724 fs_visitor::emit_texture_gen5(ir_texture *ir, fs_reg dst, fs_reg coordinate,
725                               int sampler)
726 {
727    int mlen = 0;
728    int base_mrf = 2;
729    int reg_width = c->dispatch_width / 8;
730    bool header_present = false;
731
732    if (ir->offset) {
733       /* The offsets set up by the ir_texture visitor are in the
734        * m1 header, so we can't go headerless.
735        */
736       header_present = true;
737       mlen++;
738       base_mrf--;
739    }
740
741    for (int i = 0; i < ir->coordinate->type->vector_elements; i++) {
742       fs_inst *inst = emit(BRW_OPCODE_MOV,
743                            fs_reg(MRF, base_mrf + mlen + i * reg_width),
744                            coordinate);
745       if (i < 3 && c->key.gl_clamp_mask[i] & (1 << sampler))
746          inst->saturate = true;
747       coordinate.reg_offset++;
748    }
749    mlen += ir->coordinate->type->vector_elements * reg_width;
750
751    if (ir->shadow_comparitor && ir->op != ir_txd) {
752       mlen = MAX2(mlen, header_present + 4 * reg_width);
753
754       this->result = reg_undef;
755       ir->shadow_comparitor->accept(this);
756       emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen), this->result);
757       mlen += reg_width;
758    }
759
760    fs_inst *inst = NULL;
761    switch (ir->op) {
762    case ir_tex:
763       inst = emit(FS_OPCODE_TEX, dst);
764       break;
765    case ir_txb:
766       this->result = reg_undef;
767       ir->lod_info.bias->accept(this);
768       mlen = MAX2(mlen, header_present + 4 * reg_width);
769       emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen), this->result);
770       mlen += reg_width;
771
772       inst = emit(FS_OPCODE_TXB, dst);
773
774       break;
775    case ir_txl:
776       this->result = reg_undef;
777       ir->lod_info.lod->accept(this);
778       mlen = MAX2(mlen, header_present + 4 * reg_width);
779       emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen), this->result);
780       mlen += reg_width;
781
782       inst = emit(FS_OPCODE_TXL, dst);
783       break;
784    case ir_txd: {
785       this->result = reg_undef;
786       ir->lod_info.grad.dPdx->accept(this);
787       fs_reg dPdx = this->result;
788
789       this->result = reg_undef;
790       ir->lod_info.grad.dPdy->accept(this);
791       fs_reg dPdy = this->result;
792
793       mlen = MAX2(mlen, header_present + 4 * reg_width); /* skip over 'ai' */
794
795       /**
796        *  P   =  u,    v,    r
797        * dPdx = dudx, dvdx, drdx
798        * dPdy = dudy, dvdy, drdy
799        *
800        * Load up these values:
801        * - dudx   dudy   dvdx   dvdy   drdx   drdy
802        * - dPdx.x dPdy.x dPdx.y dPdy.y dPdx.z dPdy.z
803        */
804       for (int i = 0; i < ir->lod_info.grad.dPdx->type->vector_elements; i++) {
805          emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen), dPdx);
806          dPdx.reg_offset++;
807          mlen += reg_width;
808
809          emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen), dPdy);
810          dPdy.reg_offset++;
811          mlen += reg_width;
812       }
813
814       inst = emit(FS_OPCODE_TXD, dst);
815       break;
816    }
817    case ir_txf:
818       assert(!"GLSL 1.30 features unsupported");
819       break;
820    }
821    inst->base_mrf = base_mrf;
822    inst->mlen = mlen;
823    inst->header_present = header_present;
824
825    if (mlen > 11) {
826       fail("Message length >11 disallowed by hardware\n");
827    }
828
829    return inst;
830 }
831
832 fs_inst *
833 fs_visitor::emit_texture_gen7(ir_texture *ir, fs_reg dst, fs_reg coordinate,
834                               int sampler)
835 {
836    int mlen = 0;
837    int base_mrf = 2;
838    int reg_width = c->dispatch_width / 8;
839    bool header_present = false;
840
841    if (ir->offset) {
842       /* The offsets set up by the ir_texture visitor are in the
843        * m1 header, so we can't go headerless.
844        */
845       header_present = true;
846       mlen++;
847       base_mrf--;
848    }
849
850    if (ir->shadow_comparitor && ir->op != ir_txd) {
851       this->result = reg_undef;
852       ir->shadow_comparitor->accept(this);
853       emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen), this->result);
854       mlen += reg_width;
855    }
856
857    /* Set up the LOD info */
858    switch (ir->op) {
859    case ir_tex:
860       break;
861    case ir_txb:
862       this->result = reg_undef;
863       ir->lod_info.bias->accept(this);
864       emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen), this->result);
865       mlen += reg_width;
866       break;
867    case ir_txl:
868       this->result = reg_undef;
869       ir->lod_info.lod->accept(this);
870       emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen), this->result);
871       mlen += reg_width;
872       break;
873    case ir_txd: {
874       if (c->dispatch_width == 16)
875          fail("Gen7 does not support sample_d/sample_d_c in SIMD16 mode.");
876
877       this->result = reg_undef;
878       ir->lod_info.grad.dPdx->accept(this);
879       fs_reg dPdx = this->result;
880
881       this->result = reg_undef;
882       ir->lod_info.grad.dPdy->accept(this);
883       fs_reg dPdy = this->result;
884
885       /* Load dPdx and the coordinate together:
886        * [hdr], [ref], x, dPdx.x, dPdy.x, y, dPdx.y, dPdy.y, z, dPdx.z, dPdy.z
887        */
888       for (int i = 0; i < ir->coordinate->type->vector_elements; i++) {
889          fs_inst *inst = emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen),
890                               coordinate);
891          if (i < 3 && c->key.gl_clamp_mask[i] & (1 << sampler))
892             inst->saturate = true;
893          coordinate.reg_offset++;
894          mlen += reg_width;
895
896          emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen), dPdx);
897          dPdx.reg_offset++;
898          mlen += reg_width;
899
900          emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen), dPdy);
901          dPdy.reg_offset++;
902          mlen += reg_width;
903       }
904       break;
905    }
906    case ir_txf:
907       assert(!"GLSL 1.30 features unsupported");
908       break;
909    }
910
911    /* Set up the coordinate (except for TXD where it was done earlier) */
912    if (ir->op != ir_txd) {
913       for (int i = 0; i < ir->coordinate->type->vector_elements; i++) {
914          fs_inst *inst = emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen),
915                               coordinate);
916          if (i < 3 && c->key.gl_clamp_mask[i] & (1 << sampler))
917             inst->saturate = true;
918          coordinate.reg_offset++;
919          mlen += reg_width;
920       }
921    }
922
923    /* Generate the SEND */
924    fs_inst *inst = NULL;
925    switch (ir->op) {
926    case ir_tex: inst = emit(FS_OPCODE_TEX, dst); break;
927    case ir_txb: inst = emit(FS_OPCODE_TXB, dst); break;
928    case ir_txl: inst = emit(FS_OPCODE_TXL, dst); break;
929    case ir_txd: inst = emit(FS_OPCODE_TXD, dst); break;
930    case ir_txf: assert(!"TXF unsupported.");
931    }
932    inst->base_mrf = base_mrf;
933    inst->mlen = mlen;
934    inst->header_present = header_present;
935
936    if (mlen > 11) {
937       fail("Message length >11 disallowed by hardware\n");
938    }
939
940    return inst;
941 }
942
943 void
944 fs_visitor::visit(ir_texture *ir)
945 {
946    fs_inst *inst = NULL;
947
948    int sampler = _mesa_get_sampler_uniform_value(ir->sampler, prog, &fp->Base);
949    sampler = fp->Base.SamplerUnits[sampler];
950
951    /* Our hardware doesn't have a sample_d_c message, so shadow compares
952     * for textureGrad/TXD need to be emulated with instructions.
953     */
954    bool hw_compare_supported = ir->op != ir_txd;
955    if (ir->shadow_comparitor && !hw_compare_supported) {
956       assert(c->key.compare_funcs[sampler] != GL_NONE);
957       /* No need to even sample for GL_ALWAYS or GL_NEVER...bail early */
958       if (c->key.compare_funcs[sampler] == GL_ALWAYS)
959          return swizzle_result(ir, fs_reg(1.0f), sampler);
960       else if (c->key.compare_funcs[sampler] == GL_NEVER)
961          return swizzle_result(ir, fs_reg(0.0f), sampler);
962    }
963
964    this->result = reg_undef;
965    ir->coordinate->accept(this);
966    fs_reg coordinate = this->result;
967
968    if (ir->offset != NULL) {
969       ir_constant *offset = ir->offset->as_constant();
970       assert(offset != NULL);
971
972       signed char offsets[3];
973       for (unsigned i = 0; i < ir->offset->type->vector_elements; i++)
974          offsets[i] = (signed char) offset->value.i[i];
975
976       /* Combine all three offsets into a single unsigned dword:
977        *
978        *    bits 11:8 - U Offset (X component)
979        *    bits  7:4 - V Offset (Y component)
980        *    bits  3:0 - R Offset (Z component)
981        */
982       unsigned offset_bits = 0;
983       for (unsigned i = 0; i < ir->offset->type->vector_elements; i++) {
984          const unsigned shift = 4 * (2 - i);
985          offset_bits |= (offsets[i] << shift) & (0xF << shift);
986       }
987
988       /* Explicitly set up the message header by copying g0 to msg reg m1. */
989       emit(BRW_OPCODE_MOV, fs_reg(MRF, 1, BRW_REGISTER_TYPE_UD),
990            fs_reg(GRF, 0, BRW_REGISTER_TYPE_UD));
991
992       /* Then set the offset bits in DWord 2 of the message header. */
993       emit(BRW_OPCODE_MOV,
994            fs_reg(retype(brw_vec1_reg(BRW_MESSAGE_REGISTER_FILE, 1, 2),
995                          BRW_REGISTER_TYPE_UD)),
996            fs_reg(brw_imm_uw(offset_bits)));
997    }
998
999    /* Should be lowered by do_lower_texture_projection */
1000    assert(!ir->projector);
1001
1002    /* The 965 requires the EU to do the normalization of GL rectangle
1003     * texture coordinates.  We use the program parameter state
1004     * tracking to get the scaling factor.
1005     */
1006    if (ir->sampler->type->sampler_dimensionality == GLSL_SAMPLER_DIM_RECT) {
1007       struct gl_program_parameter_list *params = c->fp->program.Base.Parameters;
1008       int tokens[STATE_LENGTH] = {
1009          STATE_INTERNAL,
1010          STATE_TEXRECT_SCALE,
1011          sampler,
1012          0,
1013          0
1014       };
1015
1016       if (c->dispatch_width == 16) {
1017          fail("rectangle scale uniform setup not supported on 16-wide\n");
1018          this->result = fs_reg(this, ir->type);
1019          return;
1020       }
1021
1022       c->prog_data.param_convert[c->prog_data.nr_params] =
1023          PARAM_NO_CONVERT;
1024       c->prog_data.param_convert[c->prog_data.nr_params + 1] =
1025          PARAM_NO_CONVERT;
1026
1027       fs_reg scale_x = fs_reg(UNIFORM, c->prog_data.nr_params);
1028       fs_reg scale_y = fs_reg(UNIFORM, c->prog_data.nr_params + 1);
1029       GLuint index = _mesa_add_state_reference(params,
1030                                                (gl_state_index *)tokens);
1031
1032       this->param_index[c->prog_data.nr_params] = index;
1033       this->param_offset[c->prog_data.nr_params] = 0;
1034       c->prog_data.nr_params++;
1035       this->param_index[c->prog_data.nr_params] = index;
1036       this->param_offset[c->prog_data.nr_params] = 1;
1037       c->prog_data.nr_params++;
1038
1039       fs_reg dst = fs_reg(this, ir->coordinate->type);
1040       fs_reg src = coordinate;
1041       coordinate = dst;
1042
1043       emit(BRW_OPCODE_MUL, dst, src, scale_x);
1044       dst.reg_offset++;
1045       src.reg_offset++;
1046       emit(BRW_OPCODE_MUL, dst, src, scale_y);
1047    }
1048
1049    /* Writemasking doesn't eliminate channels on SIMD8 texture
1050     * samples, so don't worry about them.
1051     */
1052    fs_reg dst = fs_reg(this, glsl_type::vec4_type);
1053
1054    if (intel->gen >= 7) {
1055       inst = emit_texture_gen7(ir, dst, coordinate, sampler);
1056    } else if (intel->gen >= 5) {
1057       inst = emit_texture_gen5(ir, dst, coordinate, sampler);
1058    } else {
1059       inst = emit_texture_gen4(ir, dst, coordinate, sampler);
1060    }
1061
1062    /* If there's an offset, we already set up m1.  To avoid the implied move,
1063     * use the null register.  Otherwise, we want an implied move from g0.
1064     */
1065    if (ir->offset != NULL || !inst->header_present)
1066       inst->src[0] = reg_undef;
1067    else
1068       inst->src[0] = fs_reg(retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UW));
1069
1070    inst->sampler = sampler;
1071
1072    if (ir->shadow_comparitor) {
1073       if (hw_compare_supported) {
1074          inst->shadow_compare = true;
1075       } else {
1076          this->result = reg_undef;
1077          ir->shadow_comparitor->accept(this);
1078          fs_reg ref = this->result;
1079
1080          fs_reg value = dst;
1081          dst = fs_reg(this, glsl_type::vec4_type);
1082
1083          /* FINISHME: This needs to be done pre-filtering. */
1084
1085          uint32_t conditional = 0;
1086          switch (c->key.compare_funcs[sampler]) {
1087          /* GL_ALWAYS and GL_NEVER were handled at the top of the function */
1088          case GL_LESS:     conditional = BRW_CONDITIONAL_L;   break;
1089          case GL_GREATER:  conditional = BRW_CONDITIONAL_G;   break;
1090          case GL_LEQUAL:   conditional = BRW_CONDITIONAL_LE;  break;
1091          case GL_GEQUAL:   conditional = BRW_CONDITIONAL_GE;  break;
1092          case GL_EQUAL:    conditional = BRW_CONDITIONAL_EQ;  break;
1093          case GL_NOTEQUAL: conditional = BRW_CONDITIONAL_NEQ; break;
1094          default: assert(!"Should not get here: bad shadow compare function");
1095          }
1096
1097          /* Use conditional moves to load 0 or 1 as the result */
1098          this->current_annotation = "manual shadow comparison";
1099          for (int i = 0; i < 4; i++) {
1100             inst = emit(BRW_OPCODE_MOV, dst, fs_reg(0.0f));
1101
1102             inst = emit(BRW_OPCODE_CMP, reg_null_f, ref, value);
1103             inst->conditional_mod = conditional;
1104
1105             inst = emit(BRW_OPCODE_MOV, dst, fs_reg(1.0f));
1106             inst->predicated = true;
1107
1108             dst.reg_offset++;
1109             value.reg_offset++;
1110          }
1111          dst.reg_offset = 0;
1112       }
1113    }
1114
1115    swizzle_result(ir, dst, sampler);
1116 }
1117
1118 /**
1119  * Swizzle the result of a texture result.  This is necessary for
1120  * EXT_texture_swizzle as well as DEPTH_TEXTURE_MODE for shadow comparisons.
1121  */
1122 void
1123 fs_visitor::swizzle_result(ir_texture *ir, fs_reg orig_val, int sampler)
1124 {
1125    this->result = orig_val;
1126
1127    if (ir->type == glsl_type::float_type) {
1128       /* Ignore DEPTH_TEXTURE_MODE swizzling. */
1129       assert(ir->sampler->type->sampler_shadow);
1130    } else if (c->key.tex_swizzles[sampler] != SWIZZLE_NOOP) {
1131       fs_reg swizzled_result = fs_reg(this, glsl_type::vec4_type);
1132
1133       for (int i = 0; i < 4; i++) {
1134          int swiz = GET_SWZ(c->key.tex_swizzles[sampler], i);
1135          fs_reg l = swizzled_result;
1136          l.reg_offset += i;
1137
1138          if (swiz == SWIZZLE_ZERO) {
1139             emit(BRW_OPCODE_MOV, l, fs_reg(0.0f));
1140          } else if (swiz == SWIZZLE_ONE) {
1141             emit(BRW_OPCODE_MOV, l, fs_reg(1.0f));
1142          } else {
1143             fs_reg r = orig_val;
1144             r.reg_offset += GET_SWZ(c->key.tex_swizzles[sampler], i);
1145             emit(BRW_OPCODE_MOV, l, r);
1146          }
1147       }
1148       this->result = swizzled_result;
1149    }
1150 }
1151
1152 void
1153 fs_visitor::visit(ir_swizzle *ir)
1154 {
1155    this->result = reg_undef;
1156    ir->val->accept(this);
1157    fs_reg val = this->result;
1158
1159    if (ir->type->vector_elements == 1) {
1160       this->result.reg_offset += ir->mask.x;
1161       return;
1162    }
1163
1164    fs_reg result = fs_reg(this, ir->type);
1165    this->result = result;
1166
1167    for (unsigned int i = 0; i < ir->type->vector_elements; i++) {
1168       fs_reg channel = val;
1169       int swiz = 0;
1170
1171       switch (i) {
1172       case 0:
1173          swiz = ir->mask.x;
1174          break;
1175       case 1:
1176          swiz = ir->mask.y;
1177          break;
1178       case 2:
1179          swiz = ir->mask.z;
1180          break;
1181       case 3:
1182          swiz = ir->mask.w;
1183          break;
1184       }
1185
1186       channel.reg_offset += swiz;
1187       emit(BRW_OPCODE_MOV, result, channel);
1188       result.reg_offset++;
1189    }
1190 }
1191
1192 void
1193 fs_visitor::visit(ir_discard *ir)
1194 {
1195    assert(ir->condition == NULL); /* FINISHME */
1196
1197    emit(FS_OPCODE_DISCARD);
1198    kill_emitted = true;
1199 }
1200
1201 void
1202 fs_visitor::visit(ir_constant *ir)
1203 {
1204    /* Set this->result to reg at the bottom of the function because some code
1205     * paths will cause this visitor to be applied to other fields.  This will
1206     * cause the value stored in this->result to be modified.
1207     *
1208     * Make reg constant so that it doesn't get accidentally modified along the
1209     * way.  Yes, I actually had this problem. :(
1210     */
1211    const fs_reg reg(this, ir->type);
1212    fs_reg dst_reg = reg;
1213
1214    if (ir->type->is_array()) {
1215       const unsigned size = type_size(ir->type->fields.array);
1216
1217       for (unsigned i = 0; i < ir->type->length; i++) {
1218          this->result = reg_undef;
1219          ir->array_elements[i]->accept(this);
1220          fs_reg src_reg = this->result;
1221
1222          dst_reg.type = src_reg.type;
1223          for (unsigned j = 0; j < size; j++) {
1224             emit(BRW_OPCODE_MOV, dst_reg, src_reg);
1225             src_reg.reg_offset++;
1226             dst_reg.reg_offset++;
1227          }
1228       }
1229    } else if (ir->type->is_record()) {
1230       foreach_list(node, &ir->components) {
1231          ir_instruction *const field = (ir_instruction *) node;
1232          const unsigned size = type_size(field->type);
1233
1234          this->result = reg_undef;
1235          field->accept(this);
1236          fs_reg src_reg = this->result;
1237
1238          dst_reg.type = src_reg.type;
1239          for (unsigned j = 0; j < size; j++) {
1240             emit(BRW_OPCODE_MOV, dst_reg, src_reg);
1241             src_reg.reg_offset++;
1242             dst_reg.reg_offset++;
1243          }
1244       }
1245    } else {
1246       const unsigned size = type_size(ir->type);
1247
1248       for (unsigned i = 0; i < size; i++) {
1249          switch (ir->type->base_type) {
1250          case GLSL_TYPE_FLOAT:
1251             emit(BRW_OPCODE_MOV, dst_reg, fs_reg(ir->value.f[i]));
1252             break;
1253          case GLSL_TYPE_UINT:
1254             emit(BRW_OPCODE_MOV, dst_reg, fs_reg(ir->value.u[i]));
1255             break;
1256          case GLSL_TYPE_INT:
1257             emit(BRW_OPCODE_MOV, dst_reg, fs_reg(ir->value.i[i]));
1258             break;
1259          case GLSL_TYPE_BOOL:
1260             emit(BRW_OPCODE_MOV, dst_reg, fs_reg((int)ir->value.b[i]));
1261             break;
1262          default:
1263             assert(!"Non-float/uint/int/bool constant");
1264          }
1265          dst_reg.reg_offset++;
1266       }
1267    }
1268
1269    this->result = reg;
1270 }
1271
1272 void
1273 fs_visitor::emit_bool_to_cond_code(ir_rvalue *ir)
1274 {
1275    ir_expression *expr = ir->as_expression();
1276
1277    if (expr) {
1278       fs_reg op[2];
1279       fs_inst *inst;
1280
1281       assert(expr->get_num_operands() <= 2);
1282       for (unsigned int i = 0; i < expr->get_num_operands(); i++) {
1283          assert(expr->operands[i]->type->is_scalar());
1284
1285          this->result = reg_undef;
1286          expr->operands[i]->accept(this);
1287          op[i] = this->result;
1288       }
1289
1290       switch (expr->operation) {
1291       case ir_unop_logic_not:
1292          inst = emit(BRW_OPCODE_AND, reg_null_d, op[0], fs_reg(1));
1293          inst->conditional_mod = BRW_CONDITIONAL_Z;
1294          break;
1295
1296       case ir_binop_logic_xor:
1297          inst = emit(BRW_OPCODE_XOR, reg_null_d, op[0], op[1]);
1298          inst->conditional_mod = BRW_CONDITIONAL_NZ;
1299          break;
1300
1301       case ir_binop_logic_or:
1302          inst = emit(BRW_OPCODE_OR, reg_null_d, op[0], op[1]);
1303          inst->conditional_mod = BRW_CONDITIONAL_NZ;
1304          break;
1305
1306       case ir_binop_logic_and:
1307          inst = emit(BRW_OPCODE_AND, reg_null_d, op[0], op[1]);
1308          inst->conditional_mod = BRW_CONDITIONAL_NZ;
1309          break;
1310
1311       case ir_unop_f2b:
1312          if (intel->gen >= 6) {
1313             inst = emit(BRW_OPCODE_CMP, reg_null_d, op[0], fs_reg(0.0f));
1314          } else {
1315             inst = emit(BRW_OPCODE_MOV, reg_null_f, op[0]);
1316          }
1317          inst->conditional_mod = BRW_CONDITIONAL_NZ;
1318          break;
1319
1320       case ir_unop_i2b:
1321          if (intel->gen >= 6) {
1322             inst = emit(BRW_OPCODE_CMP, reg_null_d, op[0], fs_reg(0));
1323          } else {
1324             inst = emit(BRW_OPCODE_MOV, reg_null_d, op[0]);
1325          }
1326          inst->conditional_mod = BRW_CONDITIONAL_NZ;
1327          break;
1328
1329       case ir_binop_greater:
1330       case ir_binop_gequal:
1331       case ir_binop_less:
1332       case ir_binop_lequal:
1333       case ir_binop_equal:
1334       case ir_binop_all_equal:
1335       case ir_binop_nequal:
1336       case ir_binop_any_nequal:
1337          inst = emit(BRW_OPCODE_CMP, reg_null_cmp, op[0], op[1]);
1338          inst->conditional_mod =
1339             brw_conditional_for_comparison(expr->operation);
1340          break;
1341
1342       default:
1343          assert(!"not reached");
1344          fail("bad cond code\n");
1345          break;
1346       }
1347       return;
1348    }
1349
1350    this->result = reg_undef;
1351    ir->accept(this);
1352
1353    if (intel->gen >= 6) {
1354       fs_inst *inst = emit(BRW_OPCODE_AND, reg_null_d, this->result, fs_reg(1));
1355       inst->conditional_mod = BRW_CONDITIONAL_NZ;
1356    } else {
1357       fs_inst *inst = emit(BRW_OPCODE_MOV, reg_null_d, this->result);
1358       inst->conditional_mod = BRW_CONDITIONAL_NZ;
1359    }
1360 }
1361
1362 /**
1363  * Emit a gen6 IF statement with the comparison folded into the IF
1364  * instruction.
1365  */
1366 void
1367 fs_visitor::emit_if_gen6(ir_if *ir)
1368 {
1369    ir_expression *expr = ir->condition->as_expression();
1370
1371    if (expr) {
1372       fs_reg op[2];
1373       fs_inst *inst;
1374       fs_reg temp;
1375
1376       assert(expr->get_num_operands() <= 2);
1377       for (unsigned int i = 0; i < expr->get_num_operands(); i++) {
1378          assert(expr->operands[i]->type->is_scalar());
1379
1380          this->result = reg_undef;
1381          expr->operands[i]->accept(this);
1382          op[i] = this->result;
1383       }
1384
1385       switch (expr->operation) {
1386       case ir_unop_logic_not:
1387          inst = emit(BRW_OPCODE_IF, temp, op[0], fs_reg(0));
1388          inst->conditional_mod = BRW_CONDITIONAL_Z;
1389          return;
1390
1391       case ir_binop_logic_xor:
1392          inst = emit(BRW_OPCODE_IF, reg_null_d, op[0], op[1]);
1393          inst->conditional_mod = BRW_CONDITIONAL_NZ;
1394          return;
1395
1396       case ir_binop_logic_or:
1397          temp = fs_reg(this, glsl_type::bool_type);
1398          emit(BRW_OPCODE_OR, temp, op[0], op[1]);
1399          inst = emit(BRW_OPCODE_IF, reg_null_d, temp, fs_reg(0));
1400          inst->conditional_mod = BRW_CONDITIONAL_NZ;
1401          return;
1402
1403       case ir_binop_logic_and:
1404          temp = fs_reg(this, glsl_type::bool_type);
1405          emit(BRW_OPCODE_AND, temp, op[0], op[1]);
1406          inst = emit(BRW_OPCODE_IF, reg_null_d, temp, fs_reg(0));
1407          inst->conditional_mod = BRW_CONDITIONAL_NZ;
1408          return;
1409
1410       case ir_unop_f2b:
1411          inst = emit(BRW_OPCODE_IF, reg_null_f, op[0], fs_reg(0));
1412          inst->conditional_mod = BRW_CONDITIONAL_NZ;
1413          return;
1414
1415       case ir_unop_i2b:
1416          inst = emit(BRW_OPCODE_IF, reg_null_d, op[0], fs_reg(0));
1417          inst->conditional_mod = BRW_CONDITIONAL_NZ;
1418          return;
1419
1420       case ir_binop_greater:
1421       case ir_binop_gequal:
1422       case ir_binop_less:
1423       case ir_binop_lequal:
1424       case ir_binop_equal:
1425       case ir_binop_all_equal:
1426       case ir_binop_nequal:
1427       case ir_binop_any_nequal:
1428          inst = emit(BRW_OPCODE_IF, reg_null_d, op[0], op[1]);
1429          inst->conditional_mod =
1430             brw_conditional_for_comparison(expr->operation);
1431          return;
1432       default:
1433          assert(!"not reached");
1434          inst = emit(BRW_OPCODE_IF, reg_null_d, op[0], fs_reg(0));
1435          inst->conditional_mod = BRW_CONDITIONAL_NZ;
1436          fail("bad condition\n");
1437          return;
1438       }
1439       return;
1440    }
1441
1442    this->result = reg_undef;
1443    ir->condition->accept(this);
1444
1445    fs_inst *inst = emit(BRW_OPCODE_IF, reg_null_d, this->result, fs_reg(0));
1446    inst->conditional_mod = BRW_CONDITIONAL_NZ;
1447 }
1448
1449 void
1450 fs_visitor::visit(ir_if *ir)
1451 {
1452    fs_inst *inst;
1453
1454    if (intel->gen < 6 && c->dispatch_width == 16) {
1455       fail("Can't support (non-uniform) control flow on 16-wide\n");
1456    }
1457
1458    /* Don't point the annotation at the if statement, because then it plus
1459     * the then and else blocks get printed.
1460     */
1461    this->base_ir = ir->condition;
1462
1463    if (intel->gen == 6) {
1464       emit_if_gen6(ir);
1465    } else {
1466       emit_bool_to_cond_code(ir->condition);
1467
1468       inst = emit(BRW_OPCODE_IF);
1469       inst->predicated = true;
1470    }
1471
1472    foreach_iter(exec_list_iterator, iter, ir->then_instructions) {
1473       ir_instruction *ir = (ir_instruction *)iter.get();
1474       this->base_ir = ir;
1475       this->result = reg_undef;
1476       ir->accept(this);
1477    }
1478
1479    if (!ir->else_instructions.is_empty()) {
1480       emit(BRW_OPCODE_ELSE);
1481
1482       foreach_iter(exec_list_iterator, iter, ir->else_instructions) {
1483          ir_instruction *ir = (ir_instruction *)iter.get();
1484          this->base_ir = ir;
1485          this->result = reg_undef;
1486          ir->accept(this);
1487       }
1488    }
1489
1490    emit(BRW_OPCODE_ENDIF);
1491 }
1492
1493 void
1494 fs_visitor::visit(ir_loop *ir)
1495 {
1496    fs_reg counter = reg_undef;
1497
1498    if (c->dispatch_width == 16) {
1499       fail("Can't support (non-uniform) control flow on 16-wide\n");
1500    }
1501
1502    if (ir->counter) {
1503       this->base_ir = ir->counter;
1504       ir->counter->accept(this);
1505       counter = *(variable_storage(ir->counter));
1506
1507       if (ir->from) {
1508          this->result = counter;
1509
1510          this->base_ir = ir->from;
1511          this->result = counter;
1512          ir->from->accept(this);
1513
1514          if (!this->result.equals(&counter))
1515             emit(BRW_OPCODE_MOV, counter, this->result);
1516       }
1517    }
1518
1519    emit(BRW_OPCODE_DO);
1520
1521    if (ir->to) {
1522       this->base_ir = ir->to;
1523       this->result = reg_undef;
1524       ir->to->accept(this);
1525
1526       fs_inst *inst = emit(BRW_OPCODE_CMP, reg_null_cmp, counter, this->result);
1527       inst->conditional_mod = brw_conditional_for_comparison(ir->cmp);
1528
1529       inst = emit(BRW_OPCODE_BREAK);
1530       inst->predicated = true;
1531    }
1532
1533    foreach_iter(exec_list_iterator, iter, ir->body_instructions) {
1534       ir_instruction *ir = (ir_instruction *)iter.get();
1535
1536       this->base_ir = ir;
1537       this->result = reg_undef;
1538       ir->accept(this);
1539    }
1540
1541    if (ir->increment) {
1542       this->base_ir = ir->increment;
1543       this->result = reg_undef;
1544       ir->increment->accept(this);
1545       emit(BRW_OPCODE_ADD, counter, counter, this->result);
1546    }
1547
1548    emit(BRW_OPCODE_WHILE);
1549 }
1550
1551 void
1552 fs_visitor::visit(ir_loop_jump *ir)
1553 {
1554    switch (ir->mode) {
1555    case ir_loop_jump::jump_break:
1556       emit(BRW_OPCODE_BREAK);
1557       break;
1558    case ir_loop_jump::jump_continue:
1559       emit(BRW_OPCODE_CONTINUE);
1560       break;
1561    }
1562 }
1563
1564 void
1565 fs_visitor::visit(ir_call *ir)
1566 {
1567    assert(!"FINISHME");
1568 }
1569
1570 void
1571 fs_visitor::visit(ir_return *ir)
1572 {
1573    assert(!"FINISHME");
1574 }
1575
1576 void
1577 fs_visitor::visit(ir_function *ir)
1578 {
1579    /* Ignore function bodies other than main() -- we shouldn't see calls to
1580     * them since they should all be inlined before we get to ir_to_mesa.
1581     */
1582    if (strcmp(ir->name, "main") == 0) {
1583       const ir_function_signature *sig;
1584       exec_list empty;
1585
1586       sig = ir->matching_signature(&empty);
1587
1588       assert(sig);
1589
1590       foreach_iter(exec_list_iterator, iter, sig->body) {
1591          ir_instruction *ir = (ir_instruction *)iter.get();
1592          this->base_ir = ir;
1593          this->result = reg_undef;
1594          ir->accept(this);
1595       }
1596    }
1597 }
1598
1599 void
1600 fs_visitor::visit(ir_function_signature *ir)
1601 {
1602    assert(!"not reached");
1603    (void)ir;
1604 }
1605
1606 fs_inst *
1607 fs_visitor::emit(fs_inst inst)
1608 {
1609    fs_inst *list_inst = new(mem_ctx) fs_inst;
1610    *list_inst = inst;
1611
1612    if (force_uncompressed_stack > 0)
1613       list_inst->force_uncompressed = true;
1614    else if (force_sechalf_stack > 0)
1615       list_inst->force_sechalf = true;
1616
1617    list_inst->annotation = this->current_annotation;
1618    list_inst->ir = this->base_ir;
1619
1620    this->instructions.push_tail(list_inst);
1621
1622    return list_inst;
1623 }
1624
1625 /** Emits a dummy fragment shader consisting of magenta for bringup purposes. */
1626 void
1627 fs_visitor::emit_dummy_fs()
1628 {
1629    /* Everyone's favorite color. */
1630    emit(BRW_OPCODE_MOV, fs_reg(MRF, 2), fs_reg(1.0f));
1631    emit(BRW_OPCODE_MOV, fs_reg(MRF, 3), fs_reg(0.0f));
1632    emit(BRW_OPCODE_MOV, fs_reg(MRF, 4), fs_reg(1.0f));
1633    emit(BRW_OPCODE_MOV, fs_reg(MRF, 5), fs_reg(0.0f));
1634
1635    fs_inst *write;
1636    write = emit(FS_OPCODE_FB_WRITE, fs_reg(0), fs_reg(0));
1637    write->base_mrf = 0;
1638 }
1639
1640 /* The register location here is relative to the start of the URB
1641  * data.  It will get adjusted to be a real location before
1642  * generate_code() time.
1643  */
1644 struct brw_reg
1645 fs_visitor::interp_reg(int location, int channel)
1646 {
1647    int regnr = urb_setup[location] * 2 + channel / 2;
1648    int stride = (channel & 1) * 4;
1649
1650    assert(urb_setup[location] != -1);
1651
1652    return brw_vec1_grf(regnr, stride);
1653 }
1654
1655 /** Emits the interpolation for the varying inputs. */
1656 void
1657 fs_visitor::emit_interpolation_setup_gen4()
1658 {
1659    this->current_annotation = "compute pixel centers";
1660    this->pixel_x = fs_reg(this, glsl_type::uint_type);
1661    this->pixel_y = fs_reg(this, glsl_type::uint_type);
1662    this->pixel_x.type = BRW_REGISTER_TYPE_UW;
1663    this->pixel_y.type = BRW_REGISTER_TYPE_UW;
1664
1665    emit(FS_OPCODE_PIXEL_X, this->pixel_x);
1666    emit(FS_OPCODE_PIXEL_Y, this->pixel_y);
1667
1668    this->current_annotation = "compute pixel deltas from v0";
1669    if (brw->has_pln) {
1670       this->delta_x = fs_reg(this, glsl_type::vec2_type);
1671       this->delta_y = this->delta_x;
1672       this->delta_y.reg_offset++;
1673    } else {
1674       this->delta_x = fs_reg(this, glsl_type::float_type);
1675       this->delta_y = fs_reg(this, glsl_type::float_type);
1676    }
1677    emit(BRW_OPCODE_ADD, this->delta_x,
1678         this->pixel_x, fs_reg(negate(brw_vec1_grf(1, 0))));
1679    emit(BRW_OPCODE_ADD, this->delta_y,
1680         this->pixel_y, fs_reg(negate(brw_vec1_grf(1, 1))));
1681
1682    this->current_annotation = "compute pos.w and 1/pos.w";
1683    /* Compute wpos.w.  It's always in our setup, since it's needed to
1684     * interpolate the other attributes.
1685     */
1686    this->wpos_w = fs_reg(this, glsl_type::float_type);
1687    emit(FS_OPCODE_LINTERP, wpos_w, this->delta_x, this->delta_y,
1688         interp_reg(FRAG_ATTRIB_WPOS, 3));
1689    /* Compute the pixel 1/W value from wpos.w. */
1690    this->pixel_w = fs_reg(this, glsl_type::float_type);
1691    emit_math(FS_OPCODE_RCP, this->pixel_w, wpos_w);
1692    this->current_annotation = NULL;
1693 }
1694
1695 /** Emits the interpolation for the varying inputs. */
1696 void
1697 fs_visitor::emit_interpolation_setup_gen6()
1698 {
1699    struct brw_reg g1_uw = retype(brw_vec1_grf(1, 0), BRW_REGISTER_TYPE_UW);
1700
1701    /* If the pixel centers end up used, the setup is the same as for gen4. */
1702    this->current_annotation = "compute pixel centers";
1703    fs_reg int_pixel_x = fs_reg(this, glsl_type::uint_type);
1704    fs_reg int_pixel_y = fs_reg(this, glsl_type::uint_type);
1705    int_pixel_x.type = BRW_REGISTER_TYPE_UW;
1706    int_pixel_y.type = BRW_REGISTER_TYPE_UW;
1707    emit(BRW_OPCODE_ADD,
1708         int_pixel_x,
1709         fs_reg(stride(suboffset(g1_uw, 4), 2, 4, 0)),
1710         fs_reg(brw_imm_v(0x10101010)));
1711    emit(BRW_OPCODE_ADD,
1712         int_pixel_y,
1713         fs_reg(stride(suboffset(g1_uw, 5), 2, 4, 0)),
1714         fs_reg(brw_imm_v(0x11001100)));
1715
1716    /* As of gen6, we can no longer mix float and int sources.  We have
1717     * to turn the integer pixel centers into floats for their actual
1718     * use.
1719     */
1720    this->pixel_x = fs_reg(this, glsl_type::float_type);
1721    this->pixel_y = fs_reg(this, glsl_type::float_type);
1722    emit(BRW_OPCODE_MOV, this->pixel_x, int_pixel_x);
1723    emit(BRW_OPCODE_MOV, this->pixel_y, int_pixel_y);
1724
1725    this->current_annotation = "compute pos.w";
1726    this->pixel_w = fs_reg(brw_vec8_grf(c->source_w_reg, 0));
1727    this->wpos_w = fs_reg(this, glsl_type::float_type);
1728    emit_math(FS_OPCODE_RCP, this->wpos_w, this->pixel_w);
1729
1730    this->delta_x = fs_reg(brw_vec8_grf(2, 0));
1731    this->delta_y = fs_reg(brw_vec8_grf(3, 0));
1732
1733    this->current_annotation = NULL;
1734 }
1735
1736 void
1737 fs_visitor::emit_color_write(int index, int first_color_mrf, fs_reg color)
1738 {
1739    int reg_width = c->dispatch_width / 8;
1740    fs_inst *inst;
1741
1742    if (c->dispatch_width == 8 || intel->gen >= 6) {
1743       /* SIMD8 write looks like:
1744        * m + 0: r0
1745        * m + 1: r1
1746        * m + 2: g0
1747        * m + 3: g1
1748        *
1749        * gen6 SIMD16 DP write looks like:
1750        * m + 0: r0
1751        * m + 1: r1
1752        * m + 2: g0
1753        * m + 3: g1
1754        * m + 4: b0
1755        * m + 5: b1
1756        * m + 6: a0
1757        * m + 7: a1
1758        */
1759       inst = emit(BRW_OPCODE_MOV,
1760                   fs_reg(MRF, first_color_mrf + index * reg_width),
1761                   color);
1762       inst->saturate = c->key.clamp_fragment_color;
1763    } else {
1764       /* pre-gen6 SIMD16 single source DP write looks like:
1765        * m + 0: r0
1766        * m + 1: g0
1767        * m + 2: b0
1768        * m + 3: a0
1769        * m + 4: r1
1770        * m + 5: g1
1771        * m + 6: b1
1772        * m + 7: a1
1773        */
1774       if (brw->has_compr4) {
1775          /* By setting the high bit of the MRF register number, we
1776           * indicate that we want COMPR4 mode - instead of doing the
1777           * usual destination + 1 for the second half we get
1778           * destination + 4.
1779           */
1780          inst = emit(BRW_OPCODE_MOV,
1781                      fs_reg(MRF, BRW_MRF_COMPR4 + first_color_mrf + index),
1782                      color);
1783          inst->saturate = c->key.clamp_fragment_color;
1784       } else {
1785          push_force_uncompressed();
1786          inst = emit(BRW_OPCODE_MOV, fs_reg(MRF, first_color_mrf + index),
1787                      color);
1788          inst->saturate = c->key.clamp_fragment_color;
1789          pop_force_uncompressed();
1790
1791          push_force_sechalf();
1792          color.sechalf = true;
1793          inst = emit(BRW_OPCODE_MOV, fs_reg(MRF, first_color_mrf + index + 4),
1794                      color);
1795          inst->saturate = c->key.clamp_fragment_color;
1796          pop_force_sechalf();
1797          color.sechalf = false;
1798       }
1799    }
1800 }
1801
1802 void
1803 fs_visitor::emit_fb_writes()
1804 {
1805    this->current_annotation = "FB write header";
1806    GLboolean header_present = GL_TRUE;
1807    int nr = 0;
1808    int reg_width = c->dispatch_width / 8;
1809
1810    if (intel->gen >= 6 &&
1811        !this->kill_emitted &&
1812        c->key.nr_color_regions == 1) {
1813       header_present = false;
1814    }
1815
1816    if (header_present) {
1817       /* m0, m1 header */
1818       nr += 2;
1819    }
1820
1821    if (c->aa_dest_stencil_reg) {
1822       push_force_uncompressed();
1823       emit(BRW_OPCODE_MOV, fs_reg(MRF, nr++),
1824            fs_reg(brw_vec8_grf(c->aa_dest_stencil_reg, 0)));
1825       pop_force_uncompressed();
1826    }
1827
1828    /* Reserve space for color. It'll be filled in per MRT below. */
1829    int color_mrf = nr;
1830    nr += 4 * reg_width;
1831
1832    if (c->source_depth_to_render_target) {
1833       if (intel->gen == 6 && c->dispatch_width == 16) {
1834          /* For outputting oDepth on gen6, SIMD8 writes have to be
1835           * used.  This would require 8-wide moves of each half to
1836           * message regs, kind of like pre-gen5 SIMD16 FB writes.
1837           * Just bail on doing so for now.
1838           */
1839          fail("Missing support for simd16 depth writes on gen6\n");
1840       }
1841
1842       if (c->computes_depth) {
1843          /* Hand over gl_FragDepth. */
1844          assert(this->frag_depth);
1845          fs_reg depth = *(variable_storage(this->frag_depth));
1846
1847          emit(BRW_OPCODE_MOV, fs_reg(MRF, nr), depth);
1848       } else {
1849          /* Pass through the payload depth. */
1850          emit(BRW_OPCODE_MOV, fs_reg(MRF, nr),
1851               fs_reg(brw_vec8_grf(c->source_depth_reg, 0)));
1852       }
1853       nr += reg_width;
1854    }
1855
1856    if (c->dest_depth_reg) {
1857       emit(BRW_OPCODE_MOV, fs_reg(MRF, nr),
1858            fs_reg(brw_vec8_grf(c->dest_depth_reg, 0)));
1859       nr += reg_width;
1860    }
1861
1862    fs_reg color = reg_undef;
1863    if (this->frag_color)
1864       color = *(variable_storage(this->frag_color));
1865    else if (this->frag_data) {
1866       color = *(variable_storage(this->frag_data));
1867       color.type = BRW_REGISTER_TYPE_F;
1868    }
1869
1870    for (int target = 0; target < c->key.nr_color_regions; target++) {
1871       this->current_annotation = ralloc_asprintf(this->mem_ctx,
1872                                                  "FB write target %d",
1873                                                  target);
1874       if (this->frag_color || this->frag_data) {
1875          for (int i = 0; i < 4; i++) {
1876             emit_color_write(i, color_mrf, color);
1877             color.reg_offset++;
1878          }
1879       }
1880
1881       if (this->frag_color)
1882          color.reg_offset -= 4;
1883
1884       fs_inst *inst = emit(FS_OPCODE_FB_WRITE);
1885       inst->target = target;
1886       inst->base_mrf = 0;
1887       inst->mlen = nr;
1888       if (target == c->key.nr_color_regions - 1)
1889          inst->eot = true;
1890       inst->header_present = header_present;
1891    }
1892
1893    if (c->key.nr_color_regions == 0) {
1894       if (c->key.alpha_test && (this->frag_color || this->frag_data)) {
1895          /* If the alpha test is enabled but there's no color buffer,
1896           * we still need to send alpha out the pipeline to our null
1897           * renderbuffer.
1898           */
1899          color.reg_offset += 3;
1900          emit_color_write(3, color_mrf, color);
1901       }
1902
1903       fs_inst *inst = emit(FS_OPCODE_FB_WRITE);
1904       inst->base_mrf = 0;
1905       inst->mlen = nr;
1906       inst->eot = true;
1907       inst->header_present = header_present;
1908    }
1909
1910    this->current_annotation = NULL;
1911 }