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