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