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