Merge remote branch 'origin/master' into nv50-compiler
[mesa.git] / src / mesa / drivers / dri / i965 / brw_fs.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 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 *
26 */
27
28 extern "C" {
29
30 #include <sys/types.h>
31
32 #include "main/macros.h"
33 #include "main/shaderobj.h"
34 #include "program/prog_parameter.h"
35 #include "program/prog_print.h"
36 #include "program/prog_optimize.h"
37 #include "program/hash_table.h"
38 #include "brw_context.h"
39 #include "brw_eu.h"
40 #include "brw_wm.h"
41 #include "talloc.h"
42 }
43 #include "../glsl/glsl_types.h"
44 #include "../glsl/ir_optimization.h"
45 #include "../glsl/ir_print_visitor.h"
46
47 enum register_file {
48 ARF = BRW_ARCHITECTURE_REGISTER_FILE,
49 GRF = BRW_GENERAL_REGISTER_FILE,
50 MRF = BRW_MESSAGE_REGISTER_FILE,
51 IMM = BRW_IMMEDIATE_VALUE,
52 FIXED_HW_REG, /* a struct brw_reg */
53 UNIFORM, /* prog_data->params[hw_reg] */
54 BAD_FILE
55 };
56
57 enum fs_opcodes {
58 FS_OPCODE_FB_WRITE = 256,
59 FS_OPCODE_RCP,
60 FS_OPCODE_RSQ,
61 FS_OPCODE_SQRT,
62 FS_OPCODE_EXP2,
63 FS_OPCODE_LOG2,
64 FS_OPCODE_POW,
65 FS_OPCODE_SIN,
66 FS_OPCODE_COS,
67 FS_OPCODE_DDX,
68 FS_OPCODE_DDY,
69 FS_OPCODE_LINTERP,
70 FS_OPCODE_TEX,
71 FS_OPCODE_TXB,
72 FS_OPCODE_TXL,
73 FS_OPCODE_DISCARD,
74 };
75
76 static int using_new_fs = -1;
77
78 struct gl_shader *
79 brw_new_shader(GLcontext *ctx, GLuint name, GLuint type)
80 {
81 struct brw_shader *shader;
82
83 shader = talloc_zero(NULL, struct brw_shader);
84 if (shader) {
85 shader->base.Type = type;
86 shader->base.Name = name;
87 _mesa_init_shader(ctx, &shader->base);
88 }
89
90 return &shader->base;
91 }
92
93 struct gl_shader_program *
94 brw_new_shader_program(GLcontext *ctx, GLuint name)
95 {
96 struct brw_shader_program *prog;
97 prog = talloc_zero(NULL, struct brw_shader_program);
98 if (prog) {
99 prog->base.Name = name;
100 _mesa_init_shader_program(ctx, &prog->base);
101 }
102 return &prog->base;
103 }
104
105 GLboolean
106 brw_compile_shader(GLcontext *ctx, struct gl_shader *shader)
107 {
108 if (!_mesa_ir_compile_shader(ctx, shader))
109 return GL_FALSE;
110
111 return GL_TRUE;
112 }
113
114 GLboolean
115 brw_link_shader(GLcontext *ctx, struct gl_shader_program *prog)
116 {
117 if (using_new_fs == -1)
118 using_new_fs = getenv("INTEL_NEW_FS") != NULL;
119
120 for (unsigned i = 0; i < prog->_NumLinkedShaders; i++) {
121 struct brw_shader *shader = (struct brw_shader *)prog->_LinkedShaders[i];
122
123 if (using_new_fs && shader->base.Type == GL_FRAGMENT_SHADER) {
124 void *mem_ctx = talloc_new(NULL);
125 bool progress;
126
127 if (shader->ir)
128 talloc_free(shader->ir);
129 shader->ir = new(shader) exec_list;
130 clone_ir_list(mem_ctx, shader->ir, shader->base.ir);
131
132 do_mat_op_to_vec(shader->ir);
133 do_mod_to_fract(shader->ir);
134 do_div_to_mul_rcp(shader->ir);
135 do_sub_to_add_neg(shader->ir);
136 do_explog_to_explog2(shader->ir);
137
138 brw_do_channel_expressions(shader->ir);
139 brw_do_vector_splitting(shader->ir);
140
141 do {
142 progress = false;
143
144 progress = do_common_optimization(shader->ir, true) || progress;
145 } while (progress);
146
147 validate_ir_tree(shader->ir);
148
149 reparent_ir(shader->ir, shader->ir);
150 talloc_free(mem_ctx);
151 }
152 }
153
154 if (!_mesa_ir_link_shader(ctx, prog))
155 return GL_FALSE;
156
157 return GL_TRUE;
158 }
159
160 static int
161 type_size(const struct glsl_type *type)
162 {
163 unsigned int size, i;
164
165 switch (type->base_type) {
166 case GLSL_TYPE_UINT:
167 case GLSL_TYPE_INT:
168 case GLSL_TYPE_FLOAT:
169 case GLSL_TYPE_BOOL:
170 return type->components();
171 case GLSL_TYPE_ARRAY:
172 /* FINISHME: uniform/varying arrays. */
173 return type_size(type->fields.array) * type->length;
174 case GLSL_TYPE_STRUCT:
175 size = 0;
176 for (i = 0; i < type->length; i++) {
177 size += type_size(type->fields.structure[i].type);
178 }
179 return size;
180 case GLSL_TYPE_SAMPLER:
181 /* Samplers take up no register space, since they're baked in at
182 * link time.
183 */
184 return 0;
185 default:
186 assert(!"not reached");
187 return 0;
188 }
189 }
190
191 class fs_reg {
192 public:
193 /* Callers of this talloc-based new need not call delete. It's
194 * easier to just talloc_free 'ctx' (or any of its ancestors). */
195 static void* operator new(size_t size, void *ctx)
196 {
197 void *node;
198
199 node = talloc_size(ctx, size);
200 assert(node != NULL);
201
202 return node;
203 }
204
205 /** Generic unset register constructor. */
206 fs_reg()
207 {
208 this->file = BAD_FILE;
209 this->reg = 0;
210 this->reg_offset = 0;
211 this->hw_reg = -1;
212 this->negate = 0;
213 this->abs = 0;
214 }
215
216 /** Immediate value constructor. */
217 fs_reg(float f)
218 {
219 this->file = IMM;
220 this->reg = 0;
221 this->hw_reg = 0;
222 this->type = BRW_REGISTER_TYPE_F;
223 this->imm.f = f;
224 this->negate = 0;
225 this->abs = 0;
226 }
227
228 /** Immediate value constructor. */
229 fs_reg(int32_t i)
230 {
231 this->file = IMM;
232 this->reg = 0;
233 this->hw_reg = 0;
234 this->type = BRW_REGISTER_TYPE_D;
235 this->imm.i = i;
236 this->negate = 0;
237 this->abs = 0;
238 }
239
240 /** Immediate value constructor. */
241 fs_reg(uint32_t u)
242 {
243 this->file = IMM;
244 this->reg = 0;
245 this->hw_reg = 0;
246 this->type = BRW_REGISTER_TYPE_UD;
247 this->imm.u = u;
248 this->negate = 0;
249 this->abs = 0;
250 }
251
252 /** Fixed brw_reg Immediate value constructor. */
253 fs_reg(struct brw_reg fixed_hw_reg)
254 {
255 this->file = FIXED_HW_REG;
256 this->fixed_hw_reg = fixed_hw_reg;
257 this->reg = 0;
258 this->hw_reg = 0;
259 this->type = fixed_hw_reg.type;
260 this->negate = 0;
261 this->abs = 0;
262 }
263
264 fs_reg(enum register_file file, int hw_reg);
265 fs_reg(class fs_visitor *v, const struct glsl_type *type);
266
267 /** Register file: ARF, GRF, MRF, IMM. */
268 enum register_file file;
269 /** Abstract register number. 0 = fixed hw reg */
270 int reg;
271 /** Offset within the abstract register. */
272 int reg_offset;
273 /** HW register number. Generally unset until register allocation. */
274 int hw_reg;
275 /** Register type. BRW_REGISTER_TYPE_* */
276 int type;
277 bool negate;
278 bool abs;
279 struct brw_reg fixed_hw_reg;
280
281 /** Value for file == BRW_IMMMEDIATE_FILE */
282 union {
283 int32_t i;
284 uint32_t u;
285 float f;
286 } imm;
287 };
288
289 static const fs_reg reg_undef;
290 static const fs_reg reg_null(ARF, BRW_ARF_NULL);
291
292 class fs_inst : public exec_node {
293 public:
294 /* Callers of this talloc-based new need not call delete. It's
295 * easier to just talloc_free 'ctx' (or any of its ancestors). */
296 static void* operator new(size_t size, void *ctx)
297 {
298 void *node;
299
300 node = talloc_zero_size(ctx, size);
301 assert(node != NULL);
302
303 return node;
304 }
305
306 void init()
307 {
308 this->opcode = BRW_OPCODE_NOP;
309 this->saturate = false;
310 this->conditional_mod = BRW_CONDITIONAL_NONE;
311 this->predicated = false;
312 this->sampler = 0;
313 this->shadow_compare = false;
314 }
315
316 fs_inst()
317 {
318 init();
319 }
320
321 fs_inst(int opcode)
322 {
323 init();
324 this->opcode = opcode;
325 }
326
327 fs_inst(int opcode, fs_reg dst, fs_reg src0)
328 {
329 init();
330 this->opcode = opcode;
331 this->dst = dst;
332 this->src[0] = src0;
333 }
334
335 fs_inst(int opcode, fs_reg dst, fs_reg src0, fs_reg src1)
336 {
337 init();
338 this->opcode = opcode;
339 this->dst = dst;
340 this->src[0] = src0;
341 this->src[1] = src1;
342 }
343
344 fs_inst(int opcode, fs_reg dst, fs_reg src0, fs_reg src1, fs_reg src2)
345 {
346 init();
347 this->opcode = opcode;
348 this->dst = dst;
349 this->src[0] = src0;
350 this->src[1] = src1;
351 this->src[2] = src2;
352 }
353
354 int opcode; /* BRW_OPCODE_* or FS_OPCODE_* */
355 fs_reg dst;
356 fs_reg src[3];
357 bool saturate;
358 bool predicated;
359 int conditional_mod; /**< BRW_CONDITIONAL_* */
360
361 int mlen; /** SEND message length */
362 int sampler;
363 bool shadow_compare;
364
365 /** @{
366 * Annotation for the generated IR. One of the two can be set.
367 */
368 ir_instruction *ir;
369 const char *annotation;
370 /** @} */
371 };
372
373 class fs_visitor : public ir_visitor
374 {
375 public:
376
377 fs_visitor(struct brw_wm_compile *c, struct brw_shader *shader)
378 {
379 this->c = c;
380 this->p = &c->func;
381 this->brw = p->brw;
382 this->intel = &brw->intel;
383 this->ctx = &intel->ctx;
384 this->mem_ctx = talloc_new(NULL);
385 this->shader = shader;
386 this->fail = false;
387 this->next_abstract_grf = 1;
388 this->variable_ht = hash_table_ctor(0,
389 hash_table_pointer_hash,
390 hash_table_pointer_compare);
391
392 this->frag_color = NULL;
393 this->frag_data = NULL;
394 this->frag_depth = NULL;
395 this->first_non_payload_grf = 0;
396
397 this->current_annotation = NULL;
398 this->annotation_string = NULL;
399 this->annotation_ir = NULL;
400 }
401 ~fs_visitor()
402 {
403 talloc_free(this->mem_ctx);
404 hash_table_dtor(this->variable_ht);
405 }
406
407 fs_reg *variable_storage(ir_variable *var);
408
409 void visit(ir_variable *ir);
410 void visit(ir_assignment *ir);
411 void visit(ir_dereference_variable *ir);
412 void visit(ir_dereference_record *ir);
413 void visit(ir_dereference_array *ir);
414 void visit(ir_expression *ir);
415 void visit(ir_texture *ir);
416 void visit(ir_if *ir);
417 void visit(ir_constant *ir);
418 void visit(ir_swizzle *ir);
419 void visit(ir_return *ir);
420 void visit(ir_loop *ir);
421 void visit(ir_loop_jump *ir);
422 void visit(ir_discard *ir);
423 void visit(ir_call *ir);
424 void visit(ir_function *ir);
425 void visit(ir_function_signature *ir);
426
427 fs_inst *emit(fs_inst inst);
428 void assign_curb_setup();
429 void assign_urb_setup();
430 void assign_regs();
431 void generate_code();
432 void generate_fb_write(fs_inst *inst);
433 void generate_linterp(fs_inst *inst, struct brw_reg dst,
434 struct brw_reg *src);
435 void generate_tex(fs_inst *inst, struct brw_reg dst, struct brw_reg src);
436 void generate_math(fs_inst *inst, struct brw_reg dst, struct brw_reg *src);
437 void generate_discard(fs_inst *inst);
438
439 void emit_dummy_fs();
440 void emit_interpolation();
441 void emit_pinterp(int location);
442 void emit_fb_writes();
443
444 struct brw_reg interp_reg(int location, int channel);
445
446 struct brw_context *brw;
447 struct intel_context *intel;
448 GLcontext *ctx;
449 struct brw_wm_compile *c;
450 struct brw_compile *p;
451 struct brw_shader *shader;
452 void *mem_ctx;
453 exec_list instructions;
454 int next_abstract_grf;
455 struct hash_table *variable_ht;
456 ir_variable *frag_color, *frag_data, *frag_depth;
457 int first_non_payload_grf;
458
459 /** @{ debug annotation info */
460 const char *current_annotation;
461 ir_instruction *base_ir;
462 const char **annotation_string;
463 ir_instruction **annotation_ir;
464 /** @} */
465
466 bool fail;
467
468 /* Result of last visit() method. */
469 fs_reg result;
470
471 fs_reg pixel_x;
472 fs_reg pixel_y;
473 fs_reg pixel_w;
474 fs_reg delta_x;
475 fs_reg delta_y;
476 fs_reg interp_attrs[64];
477
478 int grf_used;
479
480 };
481
482 /** Fixed HW reg constructor. */
483 fs_reg::fs_reg(enum register_file file, int hw_reg)
484 {
485 this->file = file;
486 this->reg = 0;
487 this->reg_offset = 0;
488 this->hw_reg = hw_reg;
489 this->type = BRW_REGISTER_TYPE_F;
490 this->negate = 0;
491 this->abs = 0;
492 }
493
494 /** Automatic reg constructor. */
495 fs_reg::fs_reg(class fs_visitor *v, const struct glsl_type *type)
496 {
497 this->file = GRF;
498 this->reg = v->next_abstract_grf;
499 this->reg_offset = 0;
500 v->next_abstract_grf += type_size(type);
501 this->hw_reg = -1;
502 this->negate = 0;
503 this->abs = 0;
504
505 switch (type->base_type) {
506 case GLSL_TYPE_FLOAT:
507 this->type = BRW_REGISTER_TYPE_F;
508 break;
509 case GLSL_TYPE_INT:
510 case GLSL_TYPE_BOOL:
511 this->type = BRW_REGISTER_TYPE_D;
512 break;
513 case GLSL_TYPE_UINT:
514 this->type = BRW_REGISTER_TYPE_UD;
515 break;
516 default:
517 assert(!"not reached");
518 this->type = BRW_REGISTER_TYPE_F;
519 break;
520 }
521 }
522
523 fs_reg *
524 fs_visitor::variable_storage(ir_variable *var)
525 {
526 return (fs_reg *)hash_table_find(this->variable_ht, var);
527 }
528
529 void
530 fs_visitor::visit(ir_variable *ir)
531 {
532 fs_reg *reg = NULL;
533
534 if (strcmp(ir->name, "gl_FragColor") == 0) {
535 this->frag_color = ir;
536 } else if (strcmp(ir->name, "gl_FragData") == 0) {
537 this->frag_data = ir;
538 } else if (strcmp(ir->name, "gl_FragDepth") == 0) {
539 this->frag_depth = ir;
540 assert(!"FINISHME: this hangs currently.");
541 }
542
543 if (ir->mode == ir_var_in) {
544 reg = &this->interp_attrs[ir->location];
545 }
546
547 if (ir->mode == ir_var_uniform) {
548 const float *vec_values;
549 int param_index = c->prog_data.nr_params;
550
551 /* FINISHME: This is wildly incomplete. */
552 assert(ir->type->is_scalar() || ir->type->is_vector() ||
553 ir->type->is_sampler());
554
555 const struct gl_program *fp = &this->brw->fragment_program->Base;
556 /* Our support for uniforms is piggy-backed on the struct
557 * gl_fragment_program, because that's where the values actually
558 * get stored, rather than in some global gl_shader_program uniform
559 * store.
560 */
561 vec_values = fp->Parameters->ParameterValues[ir->location];
562 for (unsigned int i = 0; i < ir->type->vector_elements; i++) {
563 c->prog_data.param[c->prog_data.nr_params++] = &vec_values[i];
564 }
565
566 reg = new(this->mem_ctx) fs_reg(UNIFORM, param_index);
567 }
568
569 if (!reg)
570 reg = new(this->mem_ctx) fs_reg(this, ir->type);
571
572 hash_table_insert(this->variable_ht, reg, ir);
573 }
574
575 void
576 fs_visitor::visit(ir_dereference_variable *ir)
577 {
578 fs_reg *reg = variable_storage(ir->var);
579 this->result = *reg;
580 }
581
582 void
583 fs_visitor::visit(ir_dereference_record *ir)
584 {
585 assert(!"FINISHME");
586 }
587
588 void
589 fs_visitor::visit(ir_dereference_array *ir)
590 {
591 ir_constant *index;
592 int element_size;
593
594 ir->array->accept(this);
595 index = ir->array_index->as_constant();
596
597 if (ir->type->is_matrix()) {
598 element_size = ir->type->vector_elements;
599 } else {
600 element_size = type_size(ir->type);
601 }
602
603 if (index) {
604 assert(this->result.file == UNIFORM ||
605 (this->result.file == GRF &&
606 this->result.reg != 0));
607 this->result.reg_offset += index->value.i[0] * element_size;
608 } else {
609 assert(!"FINISHME: non-constant matrix column");
610 }
611 }
612
613 void
614 fs_visitor::visit(ir_expression *ir)
615 {
616 unsigned int operand;
617 fs_reg op[2], temp;
618 fs_reg result;
619 fs_inst *inst;
620
621 for (operand = 0; operand < ir->get_num_operands(); operand++) {
622 ir->operands[operand]->accept(this);
623 if (this->result.file == BAD_FILE) {
624 ir_print_visitor v;
625 printf("Failed to get tree for expression operand:\n");
626 ir->operands[operand]->accept(&v);
627 this->fail = true;
628 }
629 op[operand] = this->result;
630
631 /* Matrix expression operands should have been broken down to vector
632 * operations already.
633 */
634 assert(!ir->operands[operand]->type->is_matrix());
635 /* And then those vector operands should have been broken down to scalar.
636 */
637 assert(!ir->operands[operand]->type->is_vector());
638 }
639
640 /* Storage for our result. If our result goes into an assignment, it will
641 * just get copy-propagated out, so no worries.
642 */
643 this->result = fs_reg(this, ir->type);
644
645 switch (ir->operation) {
646 case ir_unop_logic_not:
647 emit(fs_inst(BRW_OPCODE_ADD, this->result, op[0], fs_reg(-1)));
648 break;
649 case ir_unop_neg:
650 op[0].negate = ~op[0].negate;
651 this->result = op[0];
652 break;
653 case ir_unop_abs:
654 op[0].abs = true;
655 this->result = op[0];
656 break;
657 case ir_unop_sign:
658 temp = fs_reg(this, ir->type);
659
660 emit(fs_inst(BRW_OPCODE_MOV, this->result, fs_reg(0.0f)));
661
662 inst = emit(fs_inst(BRW_OPCODE_CMP, reg_null, op[0], fs_reg(0.0f)));
663 inst->conditional_mod = BRW_CONDITIONAL_G;
664 inst = emit(fs_inst(BRW_OPCODE_MOV, this->result, fs_reg(1.0f)));
665 inst->predicated = true;
666
667 inst = emit(fs_inst(BRW_OPCODE_CMP, reg_null, op[0], fs_reg(0.0f)));
668 inst->conditional_mod = BRW_CONDITIONAL_L;
669 inst = emit(fs_inst(BRW_OPCODE_MOV, this->result, fs_reg(-1.0f)));
670 inst->predicated = true;
671
672 break;
673 case ir_unop_rcp:
674 emit(fs_inst(FS_OPCODE_RCP, this->result, op[0]));
675 break;
676
677 case ir_unop_exp2:
678 emit(fs_inst(FS_OPCODE_EXP2, this->result, op[0]));
679 break;
680 case ir_unop_log2:
681 emit(fs_inst(FS_OPCODE_LOG2, this->result, op[0]));
682 break;
683 case ir_unop_exp:
684 case ir_unop_log:
685 assert(!"not reached: should be handled by ir_explog_to_explog2");
686 break;
687 case ir_unop_sin:
688 emit(fs_inst(FS_OPCODE_SIN, this->result, op[0]));
689 break;
690 case ir_unop_cos:
691 emit(fs_inst(FS_OPCODE_COS, this->result, op[0]));
692 break;
693
694 case ir_unop_dFdx:
695 emit(fs_inst(FS_OPCODE_DDX, this->result, op[0]));
696 break;
697 case ir_unop_dFdy:
698 emit(fs_inst(FS_OPCODE_DDY, this->result, op[0]));
699 break;
700
701 case ir_binop_add:
702 emit(fs_inst(BRW_OPCODE_ADD, this->result, op[0], op[1]));
703 break;
704 case ir_binop_sub:
705 assert(!"not reached: should be handled by ir_sub_to_add_neg");
706 break;
707
708 case ir_binop_mul:
709 emit(fs_inst(BRW_OPCODE_MUL, this->result, op[0], op[1]));
710 break;
711 case ir_binop_div:
712 assert(!"not reached: should be handled by ir_div_to_mul_rcp");
713 break;
714 case ir_binop_mod:
715 assert(!"ir_binop_mod should have been converted to b * fract(a/b)");
716 break;
717
718 case ir_binop_less:
719 inst = emit(fs_inst(BRW_OPCODE_CMP, this->result, op[0], op[1]));
720 inst->conditional_mod = BRW_CONDITIONAL_L;
721 emit(fs_inst(BRW_OPCODE_AND, this->result, this->result, fs_reg(0x1)));
722 break;
723 case ir_binop_greater:
724 inst = emit(fs_inst(BRW_OPCODE_CMP, this->result, op[0], op[1]));
725 inst->conditional_mod = BRW_CONDITIONAL_G;
726 emit(fs_inst(BRW_OPCODE_AND, this->result, this->result, fs_reg(0x1)));
727 break;
728 case ir_binop_lequal:
729 inst = emit(fs_inst(BRW_OPCODE_CMP, this->result, op[0], op[1]));
730 inst->conditional_mod = BRW_CONDITIONAL_LE;
731 emit(fs_inst(BRW_OPCODE_AND, this->result, this->result, fs_reg(0x1)));
732 break;
733 case ir_binop_gequal:
734 inst = emit(fs_inst(BRW_OPCODE_CMP, this->result, op[0], op[1]));
735 inst->conditional_mod = BRW_CONDITIONAL_GE;
736 emit(fs_inst(BRW_OPCODE_AND, this->result, this->result, fs_reg(0x1)));
737 break;
738 case ir_binop_equal:
739 inst = emit(fs_inst(BRW_OPCODE_CMP, this->result, op[0], op[1]));
740 inst->conditional_mod = BRW_CONDITIONAL_Z;
741 emit(fs_inst(BRW_OPCODE_AND, this->result, this->result, fs_reg(0x1)));
742 break;
743 case ir_binop_nequal:
744 inst = emit(fs_inst(BRW_OPCODE_CMP, this->result, op[0], op[1]));
745 inst->conditional_mod = BRW_CONDITIONAL_NZ;
746 emit(fs_inst(BRW_OPCODE_AND, this->result, this->result, fs_reg(0x1)));
747 break;
748
749 case ir_binop_logic_xor:
750 emit(fs_inst(BRW_OPCODE_XOR, this->result, op[0], op[1]));
751 break;
752
753 case ir_binop_logic_or:
754 emit(fs_inst(BRW_OPCODE_OR, this->result, op[0], op[1]));
755 break;
756
757 case ir_binop_logic_and:
758 emit(fs_inst(BRW_OPCODE_AND, this->result, op[0], op[1]));
759 break;
760
761 case ir_binop_dot:
762 case ir_binop_cross:
763 case ir_unop_any:
764 assert(!"not reached: should be handled by brw_channel_expressions");
765 break;
766
767 case ir_unop_sqrt:
768 emit(fs_inst(FS_OPCODE_SQRT, this->result, op[0]));
769 break;
770
771 case ir_unop_rsq:
772 emit(fs_inst(FS_OPCODE_RSQ, this->result, op[0]));
773 break;
774
775 case ir_unop_i2f:
776 case ir_unop_b2f:
777 case ir_unop_b2i:
778 emit(fs_inst(BRW_OPCODE_MOV, this->result, op[0]));
779 break;
780 case ir_unop_f2i:
781 emit(fs_inst(BRW_OPCODE_RNDZ, this->result, op[0]));
782 break;
783 case ir_unop_f2b:
784 case ir_unop_i2b:
785 inst = emit(fs_inst(BRW_OPCODE_CMP, this->result, op[0], fs_reg(0.0f)));
786 inst->conditional_mod = BRW_CONDITIONAL_NZ;
787
788 case ir_unop_trunc:
789 emit(fs_inst(BRW_OPCODE_RNDD, this->result, op[0]));
790 break;
791 case ir_unop_ceil:
792 op[0].negate = ~op[0].negate;
793 inst = emit(fs_inst(BRW_OPCODE_RNDD, this->result, op[0]));
794 this->result.negate = true;
795 break;
796 case ir_unop_floor:
797 inst = emit(fs_inst(BRW_OPCODE_RNDD, this->result, op[0]));
798 break;
799 case ir_unop_fract:
800 inst = emit(fs_inst(BRW_OPCODE_FRC, this->result, op[0]));
801 break;
802
803 case ir_binop_min:
804 inst = emit(fs_inst(BRW_OPCODE_CMP, this->result, op[0], op[1]));
805 inst->conditional_mod = BRW_CONDITIONAL_L;
806
807 inst = emit(fs_inst(BRW_OPCODE_SEL, this->result, op[0], op[1]));
808 inst->predicated = true;
809 break;
810 case ir_binop_max:
811 inst = emit(fs_inst(BRW_OPCODE_CMP, this->result, op[0], op[1]));
812 inst->conditional_mod = BRW_CONDITIONAL_G;
813
814 inst = emit(fs_inst(BRW_OPCODE_SEL, this->result, op[0], op[1]));
815 inst->predicated = true;
816 break;
817
818 case ir_binop_pow:
819 inst = emit(fs_inst(FS_OPCODE_POW, this->result, op[0], op[1]));
820 break;
821
822 case ir_unop_bit_not:
823 case ir_unop_u2f:
824 case ir_binop_lshift:
825 case ir_binop_rshift:
826 case ir_binop_bit_and:
827 case ir_binop_bit_xor:
828 case ir_binop_bit_or:
829 assert(!"GLSL 1.30 features unsupported");
830 break;
831 }
832 }
833
834 void
835 fs_visitor::visit(ir_assignment *ir)
836 {
837 struct fs_reg l, r;
838 int i;
839 int write_mask;
840 fs_inst *inst;
841
842 /* FINISHME: arrays on the lhs */
843 ir->lhs->accept(this);
844 l = this->result;
845
846 ir->rhs->accept(this);
847 r = this->result;
848
849 /* FINISHME: This should really set to the correct maximal writemask for each
850 * FINISHME: component written (in the loops below). This case can only
851 * FINISHME: occur for matrices, arrays, and structures.
852 */
853 if (ir->write_mask == 0) {
854 assert(!ir->lhs->type->is_scalar() && !ir->lhs->type->is_vector());
855 write_mask = WRITEMASK_XYZW;
856 } else {
857 assert(ir->lhs->type->is_vector() || ir->lhs->type->is_scalar());
858 write_mask = ir->write_mask;
859 }
860
861 assert(l.file != BAD_FILE);
862 assert(r.file != BAD_FILE);
863
864 if (ir->condition) {
865 /* Get the condition bool into the predicate. */
866 ir->condition->accept(this);
867 inst = emit(fs_inst(BRW_OPCODE_CMP, this->result, fs_reg(0)));
868 inst->conditional_mod = BRW_CONDITIONAL_NZ;
869 }
870
871 for (i = 0; i < type_size(ir->lhs->type); i++) {
872 if (i >= 4 || (write_mask & (1 << i))) {
873 inst = emit(fs_inst(BRW_OPCODE_MOV, l, r));
874 if (ir->condition)
875 inst->predicated = true;
876 }
877 l.reg_offset++;
878 r.reg_offset++;
879 }
880 }
881
882 void
883 fs_visitor::visit(ir_texture *ir)
884 {
885 int base_mrf = 2;
886 fs_inst *inst = NULL;
887 unsigned int mlen = 0;
888
889 ir->coordinate->accept(this);
890 fs_reg coordinate = this->result;
891
892 if (ir->projector) {
893 fs_reg inv_proj = fs_reg(this, glsl_type::float_type);
894
895 ir->projector->accept(this);
896 emit(fs_inst(FS_OPCODE_RCP, inv_proj, this->result));
897
898 fs_reg proj_coordinate = fs_reg(this, ir->coordinate->type);
899 for (unsigned int i = 0; i < ir->coordinate->type->vector_elements; i++) {
900 emit(fs_inst(BRW_OPCODE_MUL, proj_coordinate, coordinate, inv_proj));
901 coordinate.reg_offset++;
902 proj_coordinate.reg_offset++;
903 }
904 proj_coordinate.reg_offset = 0;
905
906 coordinate = proj_coordinate;
907 }
908
909 for (mlen = 0; mlen < ir->coordinate->type->vector_elements; mlen++) {
910 emit(fs_inst(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen), coordinate));
911 coordinate.reg_offset++;
912 }
913
914 /* Pre-Ironlake, the 8-wide sampler always took u,v,r. */
915 if (intel->gen < 5)
916 mlen = 3;
917
918 if (ir->shadow_comparitor) {
919 /* For shadow comparisons, we have to supply u,v,r. */
920 mlen = 3;
921
922 ir->shadow_comparitor->accept(this);
923 emit(fs_inst(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen), this->result));
924 mlen++;
925 }
926
927 /* Do we ever want to handle writemasking on texture samples? Is it
928 * performance relevant?
929 */
930 fs_reg dst = fs_reg(this, glsl_type::vec4_type);
931
932 switch (ir->op) {
933 case ir_tex:
934 inst = emit(fs_inst(FS_OPCODE_TEX, dst, fs_reg(MRF, base_mrf)));
935 break;
936 case ir_txb:
937 ir->lod_info.bias->accept(this);
938 emit(fs_inst(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen), this->result));
939 mlen++;
940
941 inst = emit(fs_inst(FS_OPCODE_TXB, dst, fs_reg(MRF, base_mrf)));
942 break;
943 case ir_txl:
944 ir->lod_info.lod->accept(this);
945 emit(fs_inst(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen), this->result));
946 mlen++;
947
948 inst = emit(fs_inst(FS_OPCODE_TXL, dst, fs_reg(MRF, base_mrf)));
949 break;
950 case ir_txd:
951 case ir_txf:
952 assert(!"GLSL 1.30 features unsupported");
953 break;
954 }
955
956 this->result = dst;
957
958 if (ir->shadow_comparitor)
959 inst->shadow_compare = true;
960 inst->mlen = mlen;
961 }
962
963 void
964 fs_visitor::visit(ir_swizzle *ir)
965 {
966 ir->val->accept(this);
967 fs_reg val = this->result;
968
969 fs_reg result = fs_reg(this, ir->type);
970 this->result = result;
971
972 for (unsigned int i = 0; i < ir->type->vector_elements; i++) {
973 fs_reg channel = val;
974 int swiz = 0;
975
976 switch (i) {
977 case 0:
978 swiz = ir->mask.x;
979 break;
980 case 1:
981 swiz = ir->mask.y;
982 break;
983 case 2:
984 swiz = ir->mask.z;
985 break;
986 case 3:
987 swiz = ir->mask.w;
988 break;
989 }
990
991 channel.reg_offset += swiz;
992 emit(fs_inst(BRW_OPCODE_MOV, result, channel));
993 result.reg_offset++;
994 }
995 }
996
997 void
998 fs_visitor::visit(ir_discard *ir)
999 {
1000 assert(ir->condition == NULL); /* FINISHME */
1001
1002 emit(fs_inst(FS_OPCODE_DISCARD));
1003 }
1004
1005 void
1006 fs_visitor::visit(ir_constant *ir)
1007 {
1008 fs_reg reg(this, ir->type);
1009 this->result = reg;
1010
1011 for (unsigned int i = 0; i < ir->type->vector_elements; i++) {
1012 switch (ir->type->base_type) {
1013 case GLSL_TYPE_FLOAT:
1014 emit(fs_inst(BRW_OPCODE_MOV, reg, fs_reg(ir->value.f[i])));
1015 break;
1016 case GLSL_TYPE_UINT:
1017 emit(fs_inst(BRW_OPCODE_MOV, reg, fs_reg(ir->value.u[i])));
1018 break;
1019 case GLSL_TYPE_INT:
1020 emit(fs_inst(BRW_OPCODE_MOV, reg, fs_reg(ir->value.i[i])));
1021 break;
1022 case GLSL_TYPE_BOOL:
1023 emit(fs_inst(BRW_OPCODE_MOV, reg, fs_reg((int)ir->value.b[i])));
1024 break;
1025 default:
1026 assert(!"Non-float/uint/int/bool constant");
1027 }
1028 reg.reg_offset++;
1029 }
1030 }
1031
1032 void
1033 fs_visitor::visit(ir_if *ir)
1034 {
1035 fs_inst *inst;
1036
1037 /* Don't point the annotation at the if statement, because then it plus
1038 * the then and else blocks get printed.
1039 */
1040 this->base_ir = ir->condition;
1041
1042 /* Generate the condition into the condition code. */
1043 ir->condition->accept(this);
1044 inst = emit(fs_inst(BRW_OPCODE_MOV, fs_reg(brw_null_reg()), this->result));
1045 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1046
1047 inst = emit(fs_inst(BRW_OPCODE_IF));
1048 inst->predicated = true;
1049
1050 foreach_iter(exec_list_iterator, iter, ir->then_instructions) {
1051 ir_instruction *ir = (ir_instruction *)iter.get();
1052 this->base_ir = ir;
1053
1054 ir->accept(this);
1055 }
1056
1057 if (!ir->else_instructions.is_empty()) {
1058 emit(fs_inst(BRW_OPCODE_ELSE));
1059
1060 foreach_iter(exec_list_iterator, iter, ir->else_instructions) {
1061 ir_instruction *ir = (ir_instruction *)iter.get();
1062 this->base_ir = ir;
1063
1064 ir->accept(this);
1065 }
1066 }
1067
1068 emit(fs_inst(BRW_OPCODE_ENDIF));
1069 }
1070
1071 void
1072 fs_visitor::visit(ir_loop *ir)
1073 {
1074 assert(!ir->from);
1075 assert(!ir->to);
1076 assert(!ir->increment);
1077 assert(!ir->counter);
1078
1079 emit(fs_inst(BRW_OPCODE_DO));
1080
1081 /* Start a safety counter. If the user messed up their loop
1082 * counting, we don't want to hang the GPU.
1083 */
1084 fs_reg max_iter = fs_reg(this, glsl_type::int_type);
1085 emit(fs_inst(BRW_OPCODE_MOV, max_iter, fs_reg(10000)));
1086
1087 foreach_iter(exec_list_iterator, iter, ir->body_instructions) {
1088 ir_instruction *ir = (ir_instruction *)iter.get();
1089 fs_inst *inst;
1090
1091 this->base_ir = ir;
1092 ir->accept(this);
1093
1094 /* Check the maximum loop iters counter. */
1095 inst = emit(fs_inst(BRW_OPCODE_ADD, max_iter, max_iter, fs_reg(-1)));
1096 inst->conditional_mod = BRW_CONDITIONAL_Z;
1097
1098 inst = emit(fs_inst(BRW_OPCODE_BREAK));
1099 inst->predicated = true;
1100 }
1101
1102 emit(fs_inst(BRW_OPCODE_WHILE));
1103 }
1104
1105 void
1106 fs_visitor::visit(ir_loop_jump *ir)
1107 {
1108 switch (ir->mode) {
1109 case ir_loop_jump::jump_break:
1110 emit(fs_inst(BRW_OPCODE_BREAK));
1111 break;
1112 case ir_loop_jump::jump_continue:
1113 emit(fs_inst(BRW_OPCODE_CONTINUE));
1114 break;
1115 }
1116 }
1117
1118 void
1119 fs_visitor::visit(ir_call *ir)
1120 {
1121 assert(!"FINISHME");
1122 }
1123
1124 void
1125 fs_visitor::visit(ir_return *ir)
1126 {
1127 assert(!"FINISHME");
1128 }
1129
1130 void
1131 fs_visitor::visit(ir_function *ir)
1132 {
1133 /* Ignore function bodies other than main() -- we shouldn't see calls to
1134 * them since they should all be inlined before we get to ir_to_mesa.
1135 */
1136 if (strcmp(ir->name, "main") == 0) {
1137 const ir_function_signature *sig;
1138 exec_list empty;
1139
1140 sig = ir->matching_signature(&empty);
1141
1142 assert(sig);
1143
1144 foreach_iter(exec_list_iterator, iter, sig->body) {
1145 ir_instruction *ir = (ir_instruction *)iter.get();
1146 this->base_ir = ir;
1147
1148 ir->accept(this);
1149 }
1150 }
1151 }
1152
1153 void
1154 fs_visitor::visit(ir_function_signature *ir)
1155 {
1156 assert(!"not reached");
1157 (void)ir;
1158 }
1159
1160 fs_inst *
1161 fs_visitor::emit(fs_inst inst)
1162 {
1163 fs_inst *list_inst = new(mem_ctx) fs_inst;
1164 *list_inst = inst;
1165
1166 list_inst->annotation = this->current_annotation;
1167 list_inst->ir = this->base_ir;
1168
1169 this->instructions.push_tail(list_inst);
1170
1171 return list_inst;
1172 }
1173
1174 /** Emits a dummy fragment shader consisting of magenta for bringup purposes. */
1175 void
1176 fs_visitor::emit_dummy_fs()
1177 {
1178 /* Everyone's favorite color. */
1179 emit(fs_inst(BRW_OPCODE_MOV,
1180 fs_reg(MRF, 2),
1181 fs_reg(1.0f)));
1182 emit(fs_inst(BRW_OPCODE_MOV,
1183 fs_reg(MRF, 3),
1184 fs_reg(0.0f)));
1185 emit(fs_inst(BRW_OPCODE_MOV,
1186 fs_reg(MRF, 4),
1187 fs_reg(1.0f)));
1188 emit(fs_inst(BRW_OPCODE_MOV,
1189 fs_reg(MRF, 5),
1190 fs_reg(0.0f)));
1191
1192 fs_inst *write;
1193 write = emit(fs_inst(FS_OPCODE_FB_WRITE,
1194 fs_reg(0),
1195 fs_reg(0)));
1196 }
1197
1198 /* The register location here is relative to the start of the URB
1199 * data. It will get adjusted to be a real location before
1200 * generate_code() time.
1201 */
1202 struct brw_reg
1203 fs_visitor::interp_reg(int location, int channel)
1204 {
1205 int regnr = location * 2 + channel / 2;
1206 int stride = (channel & 1) * 4;
1207
1208 return brw_vec1_grf(regnr, stride);
1209 }
1210
1211 /** Emits the interpolation for the varying inputs. */
1212 void
1213 fs_visitor::emit_interpolation()
1214 {
1215 struct brw_reg g1_uw = retype(brw_vec1_grf(1, 0), BRW_REGISTER_TYPE_UW);
1216 /* For now, the source regs for the setup URB data will be unset,
1217 * since we don't know until codegen how many push constants we'll
1218 * use, and therefore what the setup URB offset is.
1219 */
1220 fs_reg src_reg = reg_undef;
1221
1222 this->current_annotation = "compute pixel centers";
1223 this->pixel_x = fs_reg(this, glsl_type::uint_type);
1224 this->pixel_y = fs_reg(this, glsl_type::uint_type);
1225 emit(fs_inst(BRW_OPCODE_ADD,
1226 this->pixel_x,
1227 fs_reg(stride(suboffset(g1_uw, 4), 2, 4, 0)),
1228 fs_reg(brw_imm_v(0x10101010))));
1229 emit(fs_inst(BRW_OPCODE_ADD,
1230 this->pixel_y,
1231 fs_reg(stride(suboffset(g1_uw, 5), 2, 4, 0)),
1232 fs_reg(brw_imm_v(0x11001100))));
1233
1234 this->current_annotation = "compute pixel deltas from v0";
1235 this->delta_x = fs_reg(this, glsl_type::float_type);
1236 this->delta_y = fs_reg(this, glsl_type::float_type);
1237 emit(fs_inst(BRW_OPCODE_ADD,
1238 this->delta_x,
1239 this->pixel_x,
1240 fs_reg(negate(brw_vec1_grf(1, 0)))));
1241 emit(fs_inst(BRW_OPCODE_ADD,
1242 this->delta_y,
1243 this->pixel_y,
1244 fs_reg(brw_vec1_grf(1, 1))));
1245
1246 this->current_annotation = "compute pos.w and 1/pos.w";
1247 /* Compute wpos. Unlike many other varying inputs, we usually need it
1248 * to produce 1/w, and the varying variable wouldn't show up.
1249 */
1250 fs_reg wpos = fs_reg(this, glsl_type::vec4_type);
1251 this->interp_attrs[FRAG_ATTRIB_WPOS] = wpos;
1252 emit(fs_inst(BRW_OPCODE_MOV, wpos, this->pixel_x)); /* FINISHME: ARB_fcc */
1253 wpos.reg_offset++;
1254 emit(fs_inst(BRW_OPCODE_MOV, wpos, this->pixel_y)); /* FINISHME: ARB_fcc */
1255 wpos.reg_offset++;
1256 emit(fs_inst(FS_OPCODE_LINTERP, wpos, this->delta_x, this->delta_y,
1257 interp_reg(FRAG_ATTRIB_WPOS, 2)));
1258 wpos.reg_offset++;
1259 emit(fs_inst(FS_OPCODE_LINTERP, wpos, this->delta_x, this->delta_y,
1260 interp_reg(FRAG_ATTRIB_WPOS, 3)));
1261 /* Compute the pixel W value from wpos.w. */
1262 this->pixel_w = fs_reg(this, glsl_type::float_type);
1263 emit(fs_inst(FS_OPCODE_RCP, this->pixel_w, wpos));
1264
1265 /* FINISHME: gl_FrontFacing */
1266
1267 foreach_iter(exec_list_iterator, iter, *this->shader->ir) {
1268 ir_instruction *ir = (ir_instruction *)iter.get();
1269 ir_variable *var = ir->as_variable();
1270
1271 if (!var)
1272 continue;
1273
1274 if (var->mode != ir_var_in)
1275 continue;
1276
1277 /* If it's already set up (WPOS), skip. */
1278 if (var->location == 0)
1279 continue;
1280
1281 this->current_annotation = talloc_asprintf(this->mem_ctx,
1282 "interpolate %s "
1283 "(FRAG_ATTRIB[%d])",
1284 var->name,
1285 var->location);
1286 emit_pinterp(var->location);
1287 }
1288 this->current_annotation = NULL;
1289 }
1290
1291 void
1292 fs_visitor::emit_pinterp(int location)
1293 {
1294 fs_reg interp_attr = fs_reg(this, glsl_type::vec4_type);
1295 this->interp_attrs[location] = interp_attr;
1296
1297 for (unsigned int i = 0; i < 4; i++) {
1298 struct brw_reg interp = interp_reg(location, i);
1299 emit(fs_inst(FS_OPCODE_LINTERP,
1300 interp_attr,
1301 this->delta_x,
1302 this->delta_y,
1303 fs_reg(interp)));
1304 interp_attr.reg_offset++;
1305 }
1306 interp_attr.reg_offset -= 4;
1307
1308 for (unsigned int i = 0; i < 4; i++) {
1309 emit(fs_inst(BRW_OPCODE_MUL,
1310 interp_attr,
1311 interp_attr,
1312 this->pixel_w));
1313 interp_attr.reg_offset++;
1314 }
1315 }
1316
1317 void
1318 fs_visitor::emit_fb_writes()
1319 {
1320 this->current_annotation = "FB write";
1321
1322 assert(this->frag_color || !"FINISHME: MRT");
1323 fs_reg color = *(variable_storage(this->frag_color));
1324
1325 for (int i = 0; i < 4; i++) {
1326 emit(fs_inst(BRW_OPCODE_MOV,
1327 fs_reg(MRF, 2 + i),
1328 color));
1329 color.reg_offset++;
1330 }
1331
1332 emit(fs_inst(FS_OPCODE_FB_WRITE,
1333 fs_reg(0),
1334 fs_reg(0)));
1335
1336 this->current_annotation = NULL;
1337 }
1338
1339 void
1340 fs_visitor::generate_fb_write(fs_inst *inst)
1341 {
1342 GLboolean eot = 1; /* FINISHME: MRT */
1343 /* FINISHME: AADS */
1344
1345 /* Header is 2 regs, g0 and g1 are the contents. g0 will be implied
1346 * move, here's g1.
1347 */
1348 brw_push_insn_state(p);
1349 brw_set_mask_control(p, BRW_MASK_DISABLE);
1350 brw_set_compression_control(p, BRW_COMPRESSION_NONE);
1351 brw_MOV(p,
1352 brw_message_reg(1),
1353 brw_vec8_grf(1, 0));
1354 brw_pop_insn_state(p);
1355
1356 int nr = 2 + 4;
1357
1358 brw_fb_WRITE(p,
1359 8, /* dispatch_width */
1360 retype(vec8(brw_null_reg()), BRW_REGISTER_TYPE_UW),
1361 0, /* base MRF */
1362 retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UW),
1363 0, /* FINISHME: MRT target */
1364 nr,
1365 0,
1366 eot);
1367 }
1368
1369 void
1370 fs_visitor::generate_linterp(fs_inst *inst,
1371 struct brw_reg dst, struct brw_reg *src)
1372 {
1373 struct brw_reg delta_x = src[0];
1374 struct brw_reg delta_y = src[1];
1375 struct brw_reg interp = src[2];
1376
1377 if (brw->has_pln &&
1378 delta_y.nr == delta_x.nr + 1 &&
1379 (intel->gen >= 6 || (delta_x.nr & 1) == 0)) {
1380 brw_PLN(p, dst, interp, delta_x);
1381 } else {
1382 brw_LINE(p, brw_null_reg(), interp, delta_x);
1383 brw_MAC(p, dst, suboffset(interp, 1), delta_y);
1384 }
1385 }
1386
1387 void
1388 fs_visitor::generate_math(fs_inst *inst,
1389 struct brw_reg dst, struct brw_reg *src)
1390 {
1391 int op;
1392
1393 switch (inst->opcode) {
1394 case FS_OPCODE_RCP:
1395 op = BRW_MATH_FUNCTION_INV;
1396 break;
1397 case FS_OPCODE_RSQ:
1398 op = BRW_MATH_FUNCTION_RSQ;
1399 break;
1400 case FS_OPCODE_SQRT:
1401 op = BRW_MATH_FUNCTION_SQRT;
1402 break;
1403 case FS_OPCODE_EXP2:
1404 op = BRW_MATH_FUNCTION_EXP;
1405 break;
1406 case FS_OPCODE_LOG2:
1407 op = BRW_MATH_FUNCTION_LOG;
1408 break;
1409 case FS_OPCODE_POW:
1410 op = BRW_MATH_FUNCTION_POW;
1411 break;
1412 case FS_OPCODE_SIN:
1413 op = BRW_MATH_FUNCTION_SIN;
1414 break;
1415 case FS_OPCODE_COS:
1416 op = BRW_MATH_FUNCTION_COS;
1417 break;
1418 default:
1419 assert(!"not reached: unknown math function");
1420 op = 0;
1421 break;
1422 }
1423
1424 if (inst->opcode == FS_OPCODE_POW) {
1425 brw_MOV(p, brw_message_reg(3), src[1]);
1426 }
1427
1428 brw_math(p, dst,
1429 op,
1430 inst->saturate ? BRW_MATH_SATURATE_SATURATE :
1431 BRW_MATH_SATURATE_NONE,
1432 2, src[0],
1433 BRW_MATH_DATA_VECTOR,
1434 BRW_MATH_PRECISION_FULL);
1435 }
1436
1437 void
1438 fs_visitor::generate_tex(fs_inst *inst, struct brw_reg dst, struct brw_reg src)
1439 {
1440 int msg_type = -1;
1441 int rlen = 4;
1442
1443 if (intel->gen == 5) {
1444 switch (inst->opcode) {
1445 case FS_OPCODE_TEX:
1446 if (inst->shadow_compare) {
1447 msg_type = BRW_SAMPLER_MESSAGE_SAMPLE_COMPARE_GEN5;
1448 } else {
1449 msg_type = BRW_SAMPLER_MESSAGE_SAMPLE_GEN5;
1450 }
1451 break;
1452 case FS_OPCODE_TXB:
1453 if (inst->shadow_compare) {
1454 msg_type = BRW_SAMPLER_MESSAGE_SAMPLE_BIAS_COMPARE_GEN5;
1455 } else {
1456 msg_type = BRW_SAMPLER_MESSAGE_SAMPLE_BIAS_GEN5;
1457 }
1458 break;
1459 }
1460 } else {
1461 switch (inst->opcode) {
1462 case FS_OPCODE_TEX:
1463 /* Note that G45 and older determines shadow compare and dispatch width
1464 * from message length for most messages.
1465 */
1466 if (inst->shadow_compare) {
1467 msg_type = BRW_SAMPLER_MESSAGE_SIMD16_SAMPLE_COMPARE;
1468 } else {
1469 msg_type = BRW_SAMPLER_MESSAGE_SIMD16_SAMPLE;
1470 }
1471 case FS_OPCODE_TXB:
1472 if (inst->shadow_compare) {
1473 assert(!"FINISHME: shadow compare with bias.");
1474 msg_type = BRW_SAMPLER_MESSAGE_SIMD16_SAMPLE_BIAS;
1475 } else {
1476 msg_type = BRW_SAMPLER_MESSAGE_SIMD16_SAMPLE_BIAS;
1477 rlen = 8;
1478 }
1479 break;
1480 }
1481 }
1482 assert(msg_type != -1);
1483
1484 /* g0 header. */
1485 src.nr--;
1486
1487 brw_SAMPLE(p,
1488 retype(dst, BRW_REGISTER_TYPE_UW),
1489 src.nr,
1490 retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UW),
1491 SURF_INDEX_TEXTURE(inst->sampler),
1492 inst->sampler,
1493 WRITEMASK_XYZW,
1494 msg_type,
1495 rlen,
1496 inst->mlen + 1,
1497 0,
1498 1,
1499 BRW_SAMPLER_SIMD_MODE_SIMD8);
1500 }
1501
1502 void
1503 fs_visitor::generate_discard(fs_inst *inst)
1504 {
1505 struct brw_reg g0 = retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UW);
1506 brw_push_insn_state(p);
1507 brw_set_mask_control(p, BRW_MASK_DISABLE);
1508 brw_NOT(p, c->emit_mask_reg, brw_mask_reg(1)); /* IMASK */
1509 brw_AND(p, g0, c->emit_mask_reg, g0);
1510 brw_pop_insn_state(p);
1511 }
1512
1513 static void
1514 trivial_assign_reg(int header_size, fs_reg *reg)
1515 {
1516 if (reg->file == GRF && reg->reg != 0) {
1517 reg->hw_reg = header_size + reg->reg - 1 + reg->reg_offset;
1518 reg->reg = 0;
1519 }
1520 }
1521
1522 void
1523 fs_visitor::assign_curb_setup()
1524 {
1525 c->prog_data.first_curbe_grf = c->key.nr_payload_regs;
1526 c->prog_data.curb_read_length = ALIGN(c->prog_data.nr_params, 8) / 8;
1527
1528 /* Map the offsets in the UNIFORM file to fixed HW regs. */
1529 foreach_iter(exec_list_iterator, iter, this->instructions) {
1530 fs_inst *inst = (fs_inst *)iter.get();
1531
1532 for (unsigned int i = 0; i < 3; i++) {
1533 if (inst->src[i].file == UNIFORM) {
1534 int constant_nr = inst->src[i].hw_reg + inst->src[i].reg_offset;
1535 struct brw_reg brw_reg = brw_vec1_grf(c->prog_data.first_curbe_grf +
1536 constant_nr / 8,
1537 constant_nr % 8);
1538
1539 inst->src[i].file = FIXED_HW_REG;
1540 inst->src[i].fixed_hw_reg = brw_reg;
1541 }
1542 }
1543 }
1544 }
1545
1546 void
1547 fs_visitor::assign_urb_setup()
1548 {
1549 int urb_start = c->prog_data.first_curbe_grf + c->prog_data.curb_read_length;
1550 int interp_reg_nr[FRAG_ATTRIB_MAX];
1551
1552 c->prog_data.urb_read_length = 0;
1553
1554 /* Figure out where each of the incoming setup attributes lands. */
1555 for (unsigned int i = 0; i < FRAG_ATTRIB_MAX; i++) {
1556 interp_reg_nr[i] = -1;
1557
1558 if (i != FRAG_ATTRIB_WPOS &&
1559 !(brw->fragment_program->Base.InputsRead & BITFIELD64_BIT(i)))
1560 continue;
1561
1562 /* Each attribute is 4 setup channels, each of which is half a reg. */
1563 interp_reg_nr[i] = urb_start + c->prog_data.urb_read_length;
1564 c->prog_data.urb_read_length += 2;
1565 }
1566
1567 /* Map the register numbers for FS_OPCODE_LINTERP so that it uses
1568 * the correct setup input.
1569 */
1570 foreach_iter(exec_list_iterator, iter, this->instructions) {
1571 fs_inst *inst = (fs_inst *)iter.get();
1572
1573 if (inst->opcode != FS_OPCODE_LINTERP)
1574 continue;
1575
1576 assert(inst->src[2].file == FIXED_HW_REG);
1577
1578 int location = inst->src[2].fixed_hw_reg.nr / 2;
1579 assert(interp_reg_nr[location] != -1);
1580 inst->src[2].fixed_hw_reg.nr = (interp_reg_nr[location] +
1581 (inst->src[2].fixed_hw_reg.nr & 1));
1582 }
1583
1584 this->first_non_payload_grf = urb_start + c->prog_data.urb_read_length;
1585 }
1586
1587 void
1588 fs_visitor::assign_regs()
1589 {
1590 int header_size = this->first_non_payload_grf;
1591 int last_grf = 0;
1592
1593 /* FINISHME: trivial assignment of register numbers */
1594 foreach_iter(exec_list_iterator, iter, this->instructions) {
1595 fs_inst *inst = (fs_inst *)iter.get();
1596
1597 trivial_assign_reg(header_size, &inst->dst);
1598 trivial_assign_reg(header_size, &inst->src[0]);
1599 trivial_assign_reg(header_size, &inst->src[1]);
1600
1601 last_grf = MAX2(last_grf, inst->dst.hw_reg);
1602 last_grf = MAX2(last_grf, inst->src[0].hw_reg);
1603 last_grf = MAX2(last_grf, inst->src[1].hw_reg);
1604 }
1605
1606 this->grf_used = last_grf + 1;
1607 }
1608
1609 static struct brw_reg brw_reg_from_fs_reg(fs_reg *reg)
1610 {
1611 struct brw_reg brw_reg;
1612
1613 switch (reg->file) {
1614 case GRF:
1615 case ARF:
1616 case MRF:
1617 brw_reg = brw_vec8_reg(reg->file,
1618 reg->hw_reg, 0);
1619 brw_reg = retype(brw_reg, reg->type);
1620 break;
1621 case IMM:
1622 switch (reg->type) {
1623 case BRW_REGISTER_TYPE_F:
1624 brw_reg = brw_imm_f(reg->imm.f);
1625 break;
1626 case BRW_REGISTER_TYPE_D:
1627 brw_reg = brw_imm_d(reg->imm.i);
1628 break;
1629 case BRW_REGISTER_TYPE_UD:
1630 brw_reg = brw_imm_ud(reg->imm.u);
1631 break;
1632 default:
1633 assert(!"not reached");
1634 break;
1635 }
1636 break;
1637 case FIXED_HW_REG:
1638 brw_reg = reg->fixed_hw_reg;
1639 break;
1640 case BAD_FILE:
1641 /* Probably unused. */
1642 brw_reg = brw_null_reg();
1643 break;
1644 case UNIFORM:
1645 assert(!"not reached");
1646 brw_reg = brw_null_reg();
1647 break;
1648 }
1649 if (reg->abs)
1650 brw_reg = brw_abs(brw_reg);
1651 if (reg->negate)
1652 brw_reg = negate(brw_reg);
1653
1654 return brw_reg;
1655 }
1656
1657 void
1658 fs_visitor::generate_code()
1659 {
1660 unsigned int annotation_len = 0;
1661 int last_native_inst = 0;
1662 struct brw_instruction *if_stack[16], *loop_stack[16];
1663 int if_stack_depth = 0, loop_stack_depth = 0;
1664 int if_depth_in_loop[16];
1665
1666 if_depth_in_loop[loop_stack_depth] = 0;
1667
1668 memset(&if_stack, 0, sizeof(if_stack));
1669 foreach_iter(exec_list_iterator, iter, this->instructions) {
1670 fs_inst *inst = (fs_inst *)iter.get();
1671 struct brw_reg src[3], dst;
1672
1673 for (unsigned int i = 0; i < 3; i++) {
1674 src[i] = brw_reg_from_fs_reg(&inst->src[i]);
1675 }
1676 dst = brw_reg_from_fs_reg(&inst->dst);
1677
1678 brw_set_conditionalmod(p, inst->conditional_mod);
1679 brw_set_predicate_control(p, inst->predicated);
1680
1681 switch (inst->opcode) {
1682 case BRW_OPCODE_MOV:
1683 brw_MOV(p, dst, src[0]);
1684 break;
1685 case BRW_OPCODE_ADD:
1686 brw_ADD(p, dst, src[0], src[1]);
1687 break;
1688 case BRW_OPCODE_MUL:
1689 brw_MUL(p, dst, src[0], src[1]);
1690 break;
1691
1692 case BRW_OPCODE_FRC:
1693 brw_FRC(p, dst, src[0]);
1694 break;
1695 case BRW_OPCODE_RNDD:
1696 brw_RNDD(p, dst, src[0]);
1697 break;
1698 case BRW_OPCODE_RNDZ:
1699 brw_RNDZ(p, dst, src[0]);
1700 break;
1701
1702 case BRW_OPCODE_AND:
1703 brw_AND(p, dst, src[0], src[1]);
1704 break;
1705 case BRW_OPCODE_OR:
1706 brw_OR(p, dst, src[0], src[1]);
1707 break;
1708 case BRW_OPCODE_XOR:
1709 brw_XOR(p, dst, src[0], src[1]);
1710 break;
1711
1712 case BRW_OPCODE_CMP:
1713 brw_CMP(p, dst, inst->conditional_mod, src[0], src[1]);
1714 break;
1715 case BRW_OPCODE_SEL:
1716 brw_SEL(p, dst, src[0], src[1]);
1717 break;
1718
1719 case BRW_OPCODE_IF:
1720 assert(if_stack_depth < 16);
1721 if_stack[if_stack_depth] = brw_IF(p, BRW_EXECUTE_8);
1722 if_stack_depth++;
1723 break;
1724 case BRW_OPCODE_ELSE:
1725 if_stack[if_stack_depth - 1] =
1726 brw_ELSE(p, if_stack[if_stack_depth - 1]);
1727 break;
1728 case BRW_OPCODE_ENDIF:
1729 if_stack_depth--;
1730 brw_ENDIF(p , if_stack[if_stack_depth]);
1731 break;
1732
1733 case BRW_OPCODE_DO:
1734 loop_stack[loop_stack_depth++] = brw_DO(p, BRW_EXECUTE_8);
1735 if_depth_in_loop[loop_stack_depth] = 0;
1736 break;
1737
1738 case BRW_OPCODE_BREAK:
1739 brw_BREAK(p, if_depth_in_loop[loop_stack_depth]);
1740 brw_set_predicate_control(p, BRW_PREDICATE_NONE);
1741 break;
1742 case BRW_OPCODE_CONTINUE:
1743 brw_CONT(p, if_depth_in_loop[loop_stack_depth]);
1744 brw_set_predicate_control(p, BRW_PREDICATE_NONE);
1745 break;
1746
1747 case BRW_OPCODE_WHILE: {
1748 struct brw_instruction *inst0, *inst1;
1749 GLuint br = 1;
1750
1751 if (intel->gen == 5)
1752 br = 2;
1753
1754 assert(loop_stack_depth > 0);
1755 loop_stack_depth--;
1756 inst0 = inst1 = brw_WHILE(p, loop_stack[loop_stack_depth]);
1757 /* patch all the BREAK/CONT instructions from last BGNLOOP */
1758 while (inst0 > loop_stack[loop_stack_depth]) {
1759 inst0--;
1760 if (inst0->header.opcode == BRW_OPCODE_BREAK &&
1761 inst0->bits3.if_else.jump_count == 0) {
1762 inst0->bits3.if_else.jump_count = br * (inst1 - inst0 + 1);
1763 }
1764 else if (inst0->header.opcode == BRW_OPCODE_CONTINUE &&
1765 inst0->bits3.if_else.jump_count == 0) {
1766 inst0->bits3.if_else.jump_count = br * (inst1 - inst0);
1767 }
1768 }
1769 }
1770 break;
1771
1772 case FS_OPCODE_RCP:
1773 case FS_OPCODE_RSQ:
1774 case FS_OPCODE_SQRT:
1775 case FS_OPCODE_EXP2:
1776 case FS_OPCODE_LOG2:
1777 case FS_OPCODE_POW:
1778 case FS_OPCODE_SIN:
1779 case FS_OPCODE_COS:
1780 generate_math(inst, dst, src);
1781 break;
1782 case FS_OPCODE_LINTERP:
1783 generate_linterp(inst, dst, src);
1784 break;
1785 case FS_OPCODE_TEX:
1786 case FS_OPCODE_TXB:
1787 case FS_OPCODE_TXL:
1788 generate_tex(inst, dst, src[0]);
1789 break;
1790 case FS_OPCODE_DISCARD:
1791 generate_discard(inst);
1792 break;
1793 case FS_OPCODE_FB_WRITE:
1794 generate_fb_write(inst);
1795 break;
1796 default:
1797 if (inst->opcode < (int)ARRAY_SIZE(brw_opcodes)) {
1798 _mesa_problem(ctx, "Unsupported opcode `%s' in FS",
1799 brw_opcodes[inst->opcode].name);
1800 } else {
1801 _mesa_problem(ctx, "Unsupported opcode %d in FS", inst->opcode);
1802 }
1803 this->fail = true;
1804 }
1805
1806 if (annotation_len < p->nr_insn) {
1807 annotation_len *= 2;
1808 if (annotation_len < 16)
1809 annotation_len = 16;
1810
1811 this->annotation_string = talloc_realloc(this->mem_ctx,
1812 annotation_string,
1813 const char *,
1814 annotation_len);
1815 this->annotation_ir = talloc_realloc(this->mem_ctx,
1816 annotation_ir,
1817 ir_instruction *,
1818 annotation_len);
1819 }
1820
1821 for (unsigned int i = last_native_inst; i < p->nr_insn; i++) {
1822 this->annotation_string[i] = inst->annotation;
1823 this->annotation_ir[i] = inst->ir;
1824 }
1825 last_native_inst = p->nr_insn;
1826 }
1827 }
1828
1829 GLboolean
1830 brw_wm_fs_emit(struct brw_context *brw, struct brw_wm_compile *c)
1831 {
1832 struct brw_compile *p = &c->func;
1833 struct intel_context *intel = &brw->intel;
1834 GLcontext *ctx = &intel->ctx;
1835 struct brw_shader *shader = NULL;
1836 struct gl_shader_program *prog = ctx->Shader.CurrentProgram;
1837
1838 if (!prog)
1839 return GL_FALSE;
1840
1841 if (!using_new_fs)
1842 return GL_FALSE;
1843
1844 for (unsigned int i = 0; i < prog->_NumLinkedShaders; i++) {
1845 if (prog->_LinkedShaders[i]->Type == GL_FRAGMENT_SHADER) {
1846 shader = (struct brw_shader *)prog->_LinkedShaders[i];
1847 break;
1848 }
1849 }
1850 if (!shader)
1851 return GL_FALSE;
1852
1853 /* We always use 8-wide mode, at least for now. For one, flow
1854 * control only works in 8-wide. Also, when we're fragment shader
1855 * bound, we're almost always under register pressure as well, so
1856 * 8-wide would save us from the performance cliff of spilling
1857 * regs.
1858 */
1859 c->dispatch_width = 8;
1860
1861 if (INTEL_DEBUG & DEBUG_WM) {
1862 printf("GLSL IR for native fragment shader %d:\n", prog->Name);
1863 _mesa_print_ir(shader->ir, NULL);
1864 printf("\n");
1865 }
1866
1867 /* Now the main event: Visit the shader IR and generate our FS IR for it.
1868 */
1869 fs_visitor v(c, shader);
1870
1871 if (0) {
1872 v.emit_dummy_fs();
1873 } else {
1874 v.emit_interpolation();
1875
1876 /* Generate FS IR for main(). (the visitor only descends into
1877 * functions called "main").
1878 */
1879 foreach_iter(exec_list_iterator, iter, *shader->ir) {
1880 ir_instruction *ir = (ir_instruction *)iter.get();
1881 v.base_ir = ir;
1882 ir->accept(&v);
1883 }
1884
1885 if (v.fail)
1886 return GL_FALSE;
1887
1888 v.emit_fb_writes();
1889 v.assign_curb_setup();
1890 v.assign_urb_setup();
1891 v.assign_regs();
1892 }
1893
1894 v.generate_code();
1895
1896 if (INTEL_DEBUG & DEBUG_WM) {
1897 const char *last_annotation_string = NULL;
1898 ir_instruction *last_annotation_ir = NULL;
1899
1900 printf("Native code for fragment shader %d:\n", prog->Name);
1901 for (unsigned int i = 0; i < p->nr_insn; i++) {
1902 if (last_annotation_ir != v.annotation_ir[i]) {
1903 last_annotation_ir = v.annotation_ir[i];
1904 if (last_annotation_ir) {
1905 printf(" ");
1906 last_annotation_ir->print();
1907 printf("\n");
1908 }
1909 }
1910 if (last_annotation_string != v.annotation_string[i]) {
1911 last_annotation_string = v.annotation_string[i];
1912 if (last_annotation_string)
1913 printf(" %s\n", last_annotation_string);
1914 }
1915 brw_disasm(stdout, &p->store[i], intel->gen);
1916 }
1917 printf("\n");
1918 }
1919
1920 c->prog_data.total_grf = v.grf_used;
1921 c->prog_data.total_scratch = 0;
1922
1923 return GL_TRUE;
1924 }