7327c3a700eee16703ae5184da31d71bd94639c6
[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 "main/uniforms.h"
35 #include "program/prog_parameter.h"
36 #include "program/prog_print.h"
37 #include "program/prog_optimize.h"
38 #include "program/register_allocate.h"
39 #include "program/sampler.h"
40 #include "program/hash_table.h"
41 #include "brw_context.h"
42 #include "brw_eu.h"
43 #include "brw_wm.h"
44 #include "talloc.h"
45 }
46 #include "../glsl/glsl_types.h"
47 #include "../glsl/ir_optimization.h"
48 #include "../glsl/ir_print_visitor.h"
49
50 enum register_file {
51 ARF = BRW_ARCHITECTURE_REGISTER_FILE,
52 GRF = BRW_GENERAL_REGISTER_FILE,
53 MRF = BRW_MESSAGE_REGISTER_FILE,
54 IMM = BRW_IMMEDIATE_VALUE,
55 FIXED_HW_REG, /* a struct brw_reg */
56 UNIFORM, /* prog_data->params[hw_reg] */
57 BAD_FILE
58 };
59
60 enum fs_opcodes {
61 FS_OPCODE_FB_WRITE = 256,
62 FS_OPCODE_RCP,
63 FS_OPCODE_RSQ,
64 FS_OPCODE_SQRT,
65 FS_OPCODE_EXP2,
66 FS_OPCODE_LOG2,
67 FS_OPCODE_POW,
68 FS_OPCODE_SIN,
69 FS_OPCODE_COS,
70 FS_OPCODE_DDX,
71 FS_OPCODE_DDY,
72 FS_OPCODE_LINTERP,
73 FS_OPCODE_TEX,
74 FS_OPCODE_TXB,
75 FS_OPCODE_TXL,
76 FS_OPCODE_DISCARD,
77 };
78
79 static int using_new_fs = -1;
80 static struct brw_reg brw_reg_from_fs_reg(class fs_reg *reg);
81
82 struct gl_shader *
83 brw_new_shader(GLcontext *ctx, GLuint name, GLuint type)
84 {
85 struct brw_shader *shader;
86
87 shader = talloc_zero(NULL, struct brw_shader);
88 if (shader) {
89 shader->base.Type = type;
90 shader->base.Name = name;
91 _mesa_init_shader(ctx, &shader->base);
92 }
93
94 return &shader->base;
95 }
96
97 struct gl_shader_program *
98 brw_new_shader_program(GLcontext *ctx, GLuint name)
99 {
100 struct brw_shader_program *prog;
101 prog = talloc_zero(NULL, struct brw_shader_program);
102 if (prog) {
103 prog->base.Name = name;
104 _mesa_init_shader_program(ctx, &prog->base);
105 }
106 return &prog->base;
107 }
108
109 GLboolean
110 brw_compile_shader(GLcontext *ctx, struct gl_shader *shader)
111 {
112 if (!_mesa_ir_compile_shader(ctx, shader))
113 return GL_FALSE;
114
115 return GL_TRUE;
116 }
117
118 GLboolean
119 brw_link_shader(GLcontext *ctx, struct gl_shader_program *prog)
120 {
121 if (using_new_fs == -1)
122 using_new_fs = getenv("INTEL_NEW_FS") != NULL;
123
124 for (unsigned i = 0; i < prog->_NumLinkedShaders; i++) {
125 struct brw_shader *shader = (struct brw_shader *)prog->_LinkedShaders[i];
126
127 if (using_new_fs && shader->base.Type == GL_FRAGMENT_SHADER) {
128 void *mem_ctx = talloc_new(NULL);
129 bool progress;
130
131 if (shader->ir)
132 talloc_free(shader->ir);
133 shader->ir = new(shader) exec_list;
134 clone_ir_list(mem_ctx, shader->ir, shader->base.ir);
135
136 do_mat_op_to_vec(shader->ir);
137 do_mod_to_fract(shader->ir);
138 do_div_to_mul_rcp(shader->ir);
139 do_sub_to_add_neg(shader->ir);
140 do_explog_to_explog2(shader->ir);
141 do_lower_texture_projection(shader->ir);
142
143 do {
144 progress = false;
145
146 brw_do_channel_expressions(shader->ir);
147 brw_do_vector_splitting(shader->ir);
148
149 progress = do_lower_jumps(shader->ir, true, true,
150 true, /* main return */
151 false, /* continue */
152 false /* loops */
153 ) || progress;
154
155 progress = do_common_optimization(shader->ir, true, 32) || progress;
156
157 progress = lower_noise(shader->ir) || progress;
158 progress =
159 lower_variable_index_to_cond_assign(shader->ir,
160 GL_TRUE, /* input */
161 GL_TRUE, /* output */
162 GL_TRUE, /* temp */
163 GL_TRUE /* uniform */
164 ) || progress;
165 } while (progress);
166
167 validate_ir_tree(shader->ir);
168
169 reparent_ir(shader->ir, shader->ir);
170 talloc_free(mem_ctx);
171 }
172 }
173
174 if (!_mesa_ir_link_shader(ctx, prog))
175 return GL_FALSE;
176
177 return GL_TRUE;
178 }
179
180 static int
181 type_size(const struct glsl_type *type)
182 {
183 unsigned int size, i;
184
185 switch (type->base_type) {
186 case GLSL_TYPE_UINT:
187 case GLSL_TYPE_INT:
188 case GLSL_TYPE_FLOAT:
189 case GLSL_TYPE_BOOL:
190 return type->components();
191 case GLSL_TYPE_ARRAY:
192 return type_size(type->fields.array) * type->length;
193 case GLSL_TYPE_STRUCT:
194 size = 0;
195 for (i = 0; i < type->length; i++) {
196 size += type_size(type->fields.structure[i].type);
197 }
198 return size;
199 case GLSL_TYPE_SAMPLER:
200 /* Samplers take up no register space, since they're baked in at
201 * link time.
202 */
203 return 0;
204 default:
205 assert(!"not reached");
206 return 0;
207 }
208 }
209
210 class fs_reg {
211 public:
212 /* Callers of this talloc-based new need not call delete. It's
213 * easier to just talloc_free 'ctx' (or any of its ancestors). */
214 static void* operator new(size_t size, void *ctx)
215 {
216 void *node;
217
218 node = talloc_size(ctx, size);
219 assert(node != NULL);
220
221 return node;
222 }
223
224 void init()
225 {
226 this->reg = 0;
227 this->reg_offset = 0;
228 this->negate = 0;
229 this->abs = 0;
230 this->hw_reg = -1;
231 }
232
233 /** Generic unset register constructor. */
234 fs_reg()
235 {
236 init();
237 this->file = BAD_FILE;
238 }
239
240 /** Immediate value constructor. */
241 fs_reg(float f)
242 {
243 init();
244 this->file = IMM;
245 this->type = BRW_REGISTER_TYPE_F;
246 this->imm.f = f;
247 }
248
249 /** Immediate value constructor. */
250 fs_reg(int32_t i)
251 {
252 init();
253 this->file = IMM;
254 this->type = BRW_REGISTER_TYPE_D;
255 this->imm.i = i;
256 }
257
258 /** Immediate value constructor. */
259 fs_reg(uint32_t u)
260 {
261 init();
262 this->file = IMM;
263 this->type = BRW_REGISTER_TYPE_UD;
264 this->imm.u = u;
265 }
266
267 /** Fixed brw_reg Immediate value constructor. */
268 fs_reg(struct brw_reg fixed_hw_reg)
269 {
270 init();
271 this->file = FIXED_HW_REG;
272 this->fixed_hw_reg = fixed_hw_reg;
273 this->type = fixed_hw_reg.type;
274 }
275
276 fs_reg(enum register_file file, int hw_reg);
277 fs_reg(class fs_visitor *v, const struct glsl_type *type);
278
279 /** Register file: ARF, GRF, MRF, IMM. */
280 enum register_file file;
281 /** virtual register number. 0 = fixed hw reg */
282 int reg;
283 /** Offset within the virtual register. */
284 int reg_offset;
285 /** HW register number. Generally unset until register allocation. */
286 int hw_reg;
287 /** Register type. BRW_REGISTER_TYPE_* */
288 int type;
289 bool negate;
290 bool abs;
291 struct brw_reg fixed_hw_reg;
292
293 /** Value for file == BRW_IMMMEDIATE_FILE */
294 union {
295 int32_t i;
296 uint32_t u;
297 float f;
298 } imm;
299 };
300
301 static const fs_reg reg_undef;
302 static const fs_reg reg_null(ARF, BRW_ARF_NULL);
303
304 class fs_inst : public exec_node {
305 public:
306 /* Callers of this talloc-based new need not call delete. It's
307 * easier to just talloc_free 'ctx' (or any of its ancestors). */
308 static void* operator new(size_t size, void *ctx)
309 {
310 void *node;
311
312 node = talloc_zero_size(ctx, size);
313 assert(node != NULL);
314
315 return node;
316 }
317
318 void init()
319 {
320 this->opcode = BRW_OPCODE_NOP;
321 this->saturate = false;
322 this->conditional_mod = BRW_CONDITIONAL_NONE;
323 this->predicated = false;
324 this->sampler = 0;
325 this->target = 0;
326 this->eot = false;
327 this->header_present = false;
328 this->shadow_compare = false;
329 }
330
331 fs_inst()
332 {
333 init();
334 }
335
336 fs_inst(int opcode)
337 {
338 init();
339 this->opcode = opcode;
340 }
341
342 fs_inst(int opcode, fs_reg dst, fs_reg src0)
343 {
344 init();
345 this->opcode = opcode;
346 this->dst = dst;
347 this->src[0] = src0;
348 }
349
350 fs_inst(int opcode, fs_reg dst, fs_reg src0, fs_reg src1)
351 {
352 init();
353 this->opcode = opcode;
354 this->dst = dst;
355 this->src[0] = src0;
356 this->src[1] = src1;
357 }
358
359 fs_inst(int opcode, fs_reg dst, fs_reg src0, fs_reg src1, fs_reg src2)
360 {
361 init();
362 this->opcode = opcode;
363 this->dst = dst;
364 this->src[0] = src0;
365 this->src[1] = src1;
366 this->src[2] = src2;
367 }
368
369 int opcode; /* BRW_OPCODE_* or FS_OPCODE_* */
370 fs_reg dst;
371 fs_reg src[3];
372 bool saturate;
373 bool predicated;
374 int conditional_mod; /**< BRW_CONDITIONAL_* */
375
376 int mlen; /**< SEND message length */
377 int sampler;
378 int target; /**< MRT target. */
379 bool eot;
380 bool header_present;
381 bool shadow_compare;
382
383 /** @{
384 * Annotation for the generated IR. One of the two can be set.
385 */
386 ir_instruction *ir;
387 const char *annotation;
388 /** @} */
389 };
390
391 class fs_visitor : public ir_visitor
392 {
393 public:
394
395 fs_visitor(struct brw_wm_compile *c, struct brw_shader *shader)
396 {
397 this->c = c;
398 this->p = &c->func;
399 this->brw = p->brw;
400 this->fp = brw->fragment_program;
401 this->intel = &brw->intel;
402 this->ctx = &intel->ctx;
403 this->mem_ctx = talloc_new(NULL);
404 this->shader = shader;
405 this->fail = false;
406 this->variable_ht = hash_table_ctor(0,
407 hash_table_pointer_hash,
408 hash_table_pointer_compare);
409
410 this->frag_color = NULL;
411 this->frag_data = NULL;
412 this->frag_depth = NULL;
413 this->first_non_payload_grf = 0;
414
415 this->current_annotation = NULL;
416 this->annotation_string = NULL;
417 this->annotation_ir = NULL;
418 this->base_ir = NULL;
419
420 this->virtual_grf_sizes = NULL;
421 this->virtual_grf_next = 1;
422 this->virtual_grf_array_size = 0;
423 this->virtual_grf_def = NULL;
424 this->virtual_grf_use = NULL;
425
426 this->kill_emitted = false;
427 }
428
429 ~fs_visitor()
430 {
431 talloc_free(this->mem_ctx);
432 hash_table_dtor(this->variable_ht);
433 }
434
435 fs_reg *variable_storage(ir_variable *var);
436 int virtual_grf_alloc(int size);
437
438 void visit(ir_variable *ir);
439 void visit(ir_assignment *ir);
440 void visit(ir_dereference_variable *ir);
441 void visit(ir_dereference_record *ir);
442 void visit(ir_dereference_array *ir);
443 void visit(ir_expression *ir);
444 void visit(ir_texture *ir);
445 void visit(ir_if *ir);
446 void visit(ir_constant *ir);
447 void visit(ir_swizzle *ir);
448 void visit(ir_return *ir);
449 void visit(ir_loop *ir);
450 void visit(ir_loop_jump *ir);
451 void visit(ir_discard *ir);
452 void visit(ir_call *ir);
453 void visit(ir_function *ir);
454 void visit(ir_function_signature *ir);
455
456 fs_inst *emit(fs_inst inst);
457 void assign_curb_setup();
458 void calculate_urb_setup();
459 void assign_urb_setup();
460 void assign_regs();
461 void assign_regs_trivial();
462 void calculate_live_intervals();
463 bool propagate_constants();
464 bool dead_code_eliminate();
465 bool virtual_grf_interferes(int a, int b);
466 void generate_code();
467 void generate_fb_write(fs_inst *inst);
468 void generate_linterp(fs_inst *inst, struct brw_reg dst,
469 struct brw_reg *src);
470 void generate_tex(fs_inst *inst, struct brw_reg dst, struct brw_reg src);
471 void generate_math(fs_inst *inst, struct brw_reg dst, struct brw_reg *src);
472 void generate_discard(fs_inst *inst, struct brw_reg temp);
473 void generate_ddx(fs_inst *inst, struct brw_reg dst, struct brw_reg src);
474 void generate_ddy(fs_inst *inst, struct brw_reg dst, struct brw_reg src);
475
476 void emit_dummy_fs();
477 void emit_fragcoord_interpolation(ir_variable *ir);
478 void emit_general_interpolation(ir_variable *ir);
479 void emit_interpolation_setup_gen4();
480 void emit_interpolation_setup_gen6();
481 fs_inst *emit_texture_gen4(ir_texture *ir, fs_reg dst, fs_reg coordinate);
482 fs_inst *emit_texture_gen5(ir_texture *ir, fs_reg dst, fs_reg coordinate);
483 void emit_fb_writes();
484 void emit_assignment_writes(fs_reg &l, fs_reg &r,
485 const glsl_type *type, bool predicated);
486
487 struct brw_reg interp_reg(int location, int channel);
488 int setup_uniform_values(int loc, const glsl_type *type);
489 void setup_builtin_uniform_values(ir_variable *ir);
490
491 struct brw_context *brw;
492 const struct gl_fragment_program *fp;
493 struct intel_context *intel;
494 GLcontext *ctx;
495 struct brw_wm_compile *c;
496 struct brw_compile *p;
497 struct brw_shader *shader;
498 void *mem_ctx;
499 exec_list instructions;
500
501 int *virtual_grf_sizes;
502 int virtual_grf_next;
503 int virtual_grf_array_size;
504 int *virtual_grf_def;
505 int *virtual_grf_use;
506
507 struct hash_table *variable_ht;
508 ir_variable *frag_color, *frag_data, *frag_depth;
509 int first_non_payload_grf;
510 int urb_setup[FRAG_ATTRIB_MAX];
511 bool kill_emitted;
512
513 /** @{ debug annotation info */
514 const char *current_annotation;
515 ir_instruction *base_ir;
516 const char **annotation_string;
517 ir_instruction **annotation_ir;
518 /** @} */
519
520 bool fail;
521
522 /* Result of last visit() method. */
523 fs_reg result;
524
525 fs_reg pixel_x;
526 fs_reg pixel_y;
527 fs_reg wpos_w;
528 fs_reg pixel_w;
529 fs_reg delta_x;
530 fs_reg delta_y;
531
532 int grf_used;
533
534 };
535
536 int
537 fs_visitor::virtual_grf_alloc(int size)
538 {
539 if (virtual_grf_array_size <= virtual_grf_next) {
540 if (virtual_grf_array_size == 0)
541 virtual_grf_array_size = 16;
542 else
543 virtual_grf_array_size *= 2;
544 virtual_grf_sizes = talloc_realloc(mem_ctx, virtual_grf_sizes,
545 int, virtual_grf_array_size);
546
547 /* This slot is always unused. */
548 virtual_grf_sizes[0] = 0;
549 }
550 virtual_grf_sizes[virtual_grf_next] = size;
551 return virtual_grf_next++;
552 }
553
554 /** Fixed HW reg constructor. */
555 fs_reg::fs_reg(enum register_file file, int hw_reg)
556 {
557 init();
558 this->file = file;
559 this->hw_reg = hw_reg;
560 this->type = BRW_REGISTER_TYPE_F;
561 }
562
563 int
564 brw_type_for_base_type(const struct glsl_type *type)
565 {
566 switch (type->base_type) {
567 case GLSL_TYPE_FLOAT:
568 return BRW_REGISTER_TYPE_F;
569 case GLSL_TYPE_INT:
570 case GLSL_TYPE_BOOL:
571 return BRW_REGISTER_TYPE_D;
572 case GLSL_TYPE_UINT:
573 return BRW_REGISTER_TYPE_UD;
574 case GLSL_TYPE_ARRAY:
575 case GLSL_TYPE_STRUCT:
576 /* These should be overridden with the type of the member when
577 * dereferenced into. BRW_REGISTER_TYPE_UD seems like a likely
578 * way to trip up if we don't.
579 */
580 return BRW_REGISTER_TYPE_UD;
581 default:
582 assert(!"not reached");
583 return BRW_REGISTER_TYPE_F;
584 }
585 }
586
587 /** Automatic reg constructor. */
588 fs_reg::fs_reg(class fs_visitor *v, const struct glsl_type *type)
589 {
590 init();
591
592 this->file = GRF;
593 this->reg = v->virtual_grf_alloc(type_size(type));
594 this->reg_offset = 0;
595 this->type = brw_type_for_base_type(type);
596 }
597
598 fs_reg *
599 fs_visitor::variable_storage(ir_variable *var)
600 {
601 return (fs_reg *)hash_table_find(this->variable_ht, var);
602 }
603
604 /* Our support for uniforms is piggy-backed on the struct
605 * gl_fragment_program, because that's where the values actually
606 * get stored, rather than in some global gl_shader_program uniform
607 * store.
608 */
609 int
610 fs_visitor::setup_uniform_values(int loc, const glsl_type *type)
611 {
612 unsigned int offset = 0;
613 float *vec_values;
614
615 if (type->is_matrix()) {
616 const glsl_type *column = glsl_type::get_instance(GLSL_TYPE_FLOAT,
617 type->vector_elements,
618 1);
619
620 for (unsigned int i = 0; i < type->matrix_columns; i++) {
621 offset += setup_uniform_values(loc + offset, column);
622 }
623
624 return offset;
625 }
626
627 switch (type->base_type) {
628 case GLSL_TYPE_FLOAT:
629 case GLSL_TYPE_UINT:
630 case GLSL_TYPE_INT:
631 case GLSL_TYPE_BOOL:
632 vec_values = fp->Base.Parameters->ParameterValues[loc];
633 for (unsigned int i = 0; i < type->vector_elements; i++) {
634 c->prog_data.param[c->prog_data.nr_params++] = &vec_values[i];
635 }
636 return 1;
637
638 case GLSL_TYPE_STRUCT:
639 for (unsigned int i = 0; i < type->length; i++) {
640 offset += setup_uniform_values(loc + offset,
641 type->fields.structure[i].type);
642 }
643 return offset;
644
645 case GLSL_TYPE_ARRAY:
646 for (unsigned int i = 0; i < type->length; i++) {
647 offset += setup_uniform_values(loc + offset, type->fields.array);
648 }
649 return offset;
650
651 case GLSL_TYPE_SAMPLER:
652 /* The sampler takes up a slot, but we don't use any values from it. */
653 return 1;
654
655 default:
656 assert(!"not reached");
657 return 0;
658 }
659 }
660
661
662 /* Our support for builtin uniforms is even scarier than non-builtin.
663 * It sits on top of the PROG_STATE_VAR parameters that are
664 * automatically updated from GL context state.
665 */
666 void
667 fs_visitor::setup_builtin_uniform_values(ir_variable *ir)
668 {
669 const struct gl_builtin_uniform_desc *statevar = NULL;
670
671 for (unsigned int i = 0; _mesa_builtin_uniform_desc[i].name; i++) {
672 statevar = &_mesa_builtin_uniform_desc[i];
673 if (strcmp(ir->name, _mesa_builtin_uniform_desc[i].name) == 0)
674 break;
675 }
676
677 if (!statevar->name) {
678 this->fail = true;
679 printf("Failed to find builtin uniform `%s'\n", ir->name);
680 return;
681 }
682
683 int array_count;
684 if (ir->type->is_array()) {
685 array_count = ir->type->length;
686 } else {
687 array_count = 1;
688 }
689
690 for (int a = 0; a < array_count; a++) {
691 for (unsigned int i = 0; i < statevar->num_elements; i++) {
692 struct gl_builtin_uniform_element *element = &statevar->elements[i];
693 int tokens[STATE_LENGTH];
694
695 memcpy(tokens, element->tokens, sizeof(element->tokens));
696 if (ir->type->is_array()) {
697 tokens[1] = a;
698 }
699
700 /* This state reference has already been setup by ir_to_mesa,
701 * but we'll get the same index back here.
702 */
703 int index = _mesa_add_state_reference(this->fp->Base.Parameters,
704 (gl_state_index *)tokens);
705 float *vec_values = this->fp->Base.Parameters->ParameterValues[index];
706
707 /* Add each of the unique swizzles of the element as a
708 * parameter. This'll end up matching the expected layout of
709 * the array/matrix/structure we're trying to fill in.
710 */
711 int last_swiz = -1;
712 for (unsigned int i = 0; i < 4; i++) {
713 int swiz = GET_SWZ(element->swizzle, i);
714 if (swiz == last_swiz)
715 break;
716 last_swiz = swiz;
717
718 c->prog_data.param[c->prog_data.nr_params++] = &vec_values[swiz];
719 }
720 }
721 }
722 }
723
724 void
725 fs_visitor::emit_fragcoord_interpolation(ir_variable *ir)
726 {
727 fs_reg *reg = new(this->mem_ctx) fs_reg(this, ir->type);
728 fs_reg wpos = *reg;
729 fs_reg neg_y = this->pixel_y;
730 neg_y.negate = true;
731
732 /* gl_FragCoord.x */
733 if (ir->pixel_center_integer) {
734 emit(fs_inst(BRW_OPCODE_MOV, wpos, this->pixel_x));
735 } else {
736 emit(fs_inst(BRW_OPCODE_ADD, wpos, this->pixel_x, fs_reg(0.5f)));
737 }
738 wpos.reg_offset++;
739
740 /* gl_FragCoord.y */
741 if (ir->origin_upper_left && ir->pixel_center_integer) {
742 emit(fs_inst(BRW_OPCODE_MOV, wpos, this->pixel_y));
743 } else {
744 fs_reg pixel_y = this->pixel_y;
745 float offset = (ir->pixel_center_integer ? 0.0 : 0.5);
746
747 if (!ir->origin_upper_left) {
748 pixel_y.negate = true;
749 offset += c->key.drawable_height - 1.0;
750 }
751
752 emit(fs_inst(BRW_OPCODE_ADD, wpos, pixel_y, fs_reg(offset)));
753 }
754 wpos.reg_offset++;
755
756 /* gl_FragCoord.z */
757 emit(fs_inst(FS_OPCODE_LINTERP, wpos, this->delta_x, this->delta_y,
758 interp_reg(FRAG_ATTRIB_WPOS, 2)));
759 wpos.reg_offset++;
760
761 /* gl_FragCoord.w: Already set up in emit_interpolation */
762 emit(fs_inst(BRW_OPCODE_MOV, wpos, this->wpos_w));
763
764 hash_table_insert(this->variable_ht, reg, ir);
765 }
766
767
768 void
769 fs_visitor::emit_general_interpolation(ir_variable *ir)
770 {
771 fs_reg *reg = new(this->mem_ctx) fs_reg(this, ir->type);
772 /* Interpolation is always in floating point regs. */
773 reg->type = BRW_REGISTER_TYPE_F;
774 fs_reg attr = *reg;
775
776 unsigned int array_elements;
777 const glsl_type *type;
778
779 if (ir->type->is_array()) {
780 array_elements = ir->type->length;
781 if (array_elements == 0) {
782 this->fail = true;
783 }
784 type = ir->type->fields.array;
785 } else {
786 array_elements = 1;
787 type = ir->type;
788 }
789
790 int location = ir->location;
791 for (unsigned int i = 0; i < array_elements; i++) {
792 for (unsigned int j = 0; j < type->matrix_columns; j++) {
793 if (urb_setup[location] == -1) {
794 /* If there's no incoming setup data for this slot, don't
795 * emit interpolation for it.
796 */
797 attr.reg_offset += type->vector_elements;
798 location++;
799 continue;
800 }
801
802 for (unsigned int c = 0; c < type->vector_elements; c++) {
803 struct brw_reg interp = interp_reg(location, c);
804 emit(fs_inst(FS_OPCODE_LINTERP,
805 attr,
806 this->delta_x,
807 this->delta_y,
808 fs_reg(interp)));
809 attr.reg_offset++;
810 }
811 attr.reg_offset -= type->vector_elements;
812
813 if (intel->gen < 6) {
814 for (unsigned int c = 0; c < type->vector_elements; c++) {
815 emit(fs_inst(BRW_OPCODE_MUL,
816 attr,
817 attr,
818 this->pixel_w));
819 attr.reg_offset++;
820 }
821 }
822 location++;
823 }
824 }
825
826 hash_table_insert(this->variable_ht, reg, ir);
827 }
828
829 void
830 fs_visitor::visit(ir_variable *ir)
831 {
832 fs_reg *reg = NULL;
833
834 if (variable_storage(ir))
835 return;
836
837 if (strcmp(ir->name, "gl_FragColor") == 0) {
838 this->frag_color = ir;
839 } else if (strcmp(ir->name, "gl_FragData") == 0) {
840 this->frag_data = ir;
841 } else if (strcmp(ir->name, "gl_FragDepth") == 0) {
842 this->frag_depth = ir;
843 }
844
845 if (ir->mode == ir_var_in) {
846 if (!strcmp(ir->name, "gl_FragCoord")) {
847 emit_fragcoord_interpolation(ir);
848 return;
849 } else if (!strcmp(ir->name, "gl_FrontFacing")) {
850 reg = new(this->mem_ctx) fs_reg(this, ir->type);
851 struct brw_reg r1_6ud = retype(brw_vec1_grf(1, 6), BRW_REGISTER_TYPE_UD);
852 /* bit 31 is "primitive is back face", so checking < (1 << 31) gives
853 * us front face
854 */
855 fs_inst *inst = emit(fs_inst(BRW_OPCODE_CMP,
856 *reg,
857 fs_reg(r1_6ud),
858 fs_reg(1u << 31)));
859 inst->conditional_mod = BRW_CONDITIONAL_L;
860 emit(fs_inst(BRW_OPCODE_AND, *reg, *reg, fs_reg(1u)));
861 } else {
862 emit_general_interpolation(ir);
863 return;
864 }
865 }
866
867 if (ir->mode == ir_var_uniform) {
868 int param_index = c->prog_data.nr_params;
869
870 if (!strncmp(ir->name, "gl_", 3)) {
871 setup_builtin_uniform_values(ir);
872 } else {
873 setup_uniform_values(ir->location, ir->type);
874 }
875
876 reg = new(this->mem_ctx) fs_reg(UNIFORM, param_index);
877 }
878
879 if (!reg)
880 reg = new(this->mem_ctx) fs_reg(this, ir->type);
881
882 hash_table_insert(this->variable_ht, reg, ir);
883 }
884
885 void
886 fs_visitor::visit(ir_dereference_variable *ir)
887 {
888 fs_reg *reg = variable_storage(ir->var);
889 this->result = *reg;
890 }
891
892 void
893 fs_visitor::visit(ir_dereference_record *ir)
894 {
895 const glsl_type *struct_type = ir->record->type;
896
897 ir->record->accept(this);
898
899 unsigned int offset = 0;
900 for (unsigned int i = 0; i < struct_type->length; i++) {
901 if (strcmp(struct_type->fields.structure[i].name, ir->field) == 0)
902 break;
903 offset += type_size(struct_type->fields.structure[i].type);
904 }
905 this->result.reg_offset += offset;
906 this->result.type = brw_type_for_base_type(ir->type);
907 }
908
909 void
910 fs_visitor::visit(ir_dereference_array *ir)
911 {
912 ir_constant *index;
913 int element_size;
914
915 ir->array->accept(this);
916 index = ir->array_index->as_constant();
917
918 element_size = type_size(ir->type);
919 this->result.type = brw_type_for_base_type(ir->type);
920
921 if (index) {
922 assert(this->result.file == UNIFORM ||
923 (this->result.file == GRF &&
924 this->result.reg != 0));
925 this->result.reg_offset += index->value.i[0] * element_size;
926 } else {
927 assert(!"FINISHME: non-constant array element");
928 }
929 }
930
931 void
932 fs_visitor::visit(ir_expression *ir)
933 {
934 unsigned int operand;
935 fs_reg op[2], temp;
936 fs_reg result;
937 fs_inst *inst;
938
939 for (operand = 0; operand < ir->get_num_operands(); operand++) {
940 ir->operands[operand]->accept(this);
941 if (this->result.file == BAD_FILE) {
942 ir_print_visitor v;
943 printf("Failed to get tree for expression operand:\n");
944 ir->operands[operand]->accept(&v);
945 this->fail = true;
946 }
947 op[operand] = this->result;
948
949 /* Matrix expression operands should have been broken down to vector
950 * operations already.
951 */
952 assert(!ir->operands[operand]->type->is_matrix());
953 /* And then those vector operands should have been broken down to scalar.
954 */
955 assert(!ir->operands[operand]->type->is_vector());
956 }
957
958 /* Storage for our result. If our result goes into an assignment, it will
959 * just get copy-propagated out, so no worries.
960 */
961 this->result = fs_reg(this, ir->type);
962
963 switch (ir->operation) {
964 case ir_unop_logic_not:
965 emit(fs_inst(BRW_OPCODE_ADD, this->result, op[0], fs_reg(-1)));
966 break;
967 case ir_unop_neg:
968 op[0].negate = !op[0].negate;
969 this->result = op[0];
970 break;
971 case ir_unop_abs:
972 op[0].abs = true;
973 this->result = op[0];
974 break;
975 case ir_unop_sign:
976 temp = fs_reg(this, ir->type);
977
978 emit(fs_inst(BRW_OPCODE_MOV, this->result, fs_reg(0.0f)));
979
980 inst = emit(fs_inst(BRW_OPCODE_CMP, reg_null, op[0], fs_reg(0.0f)));
981 inst->conditional_mod = BRW_CONDITIONAL_G;
982 inst = emit(fs_inst(BRW_OPCODE_MOV, this->result, fs_reg(1.0f)));
983 inst->predicated = true;
984
985 inst = emit(fs_inst(BRW_OPCODE_CMP, reg_null, op[0], fs_reg(0.0f)));
986 inst->conditional_mod = BRW_CONDITIONAL_L;
987 inst = emit(fs_inst(BRW_OPCODE_MOV, this->result, fs_reg(-1.0f)));
988 inst->predicated = true;
989
990 break;
991 case ir_unop_rcp:
992 emit(fs_inst(FS_OPCODE_RCP, this->result, op[0]));
993 break;
994
995 case ir_unop_exp2:
996 emit(fs_inst(FS_OPCODE_EXP2, this->result, op[0]));
997 break;
998 case ir_unop_log2:
999 emit(fs_inst(FS_OPCODE_LOG2, this->result, op[0]));
1000 break;
1001 case ir_unop_exp:
1002 case ir_unop_log:
1003 assert(!"not reached: should be handled by ir_explog_to_explog2");
1004 break;
1005 case ir_unop_sin:
1006 emit(fs_inst(FS_OPCODE_SIN, this->result, op[0]));
1007 break;
1008 case ir_unop_cos:
1009 emit(fs_inst(FS_OPCODE_COS, this->result, op[0]));
1010 break;
1011
1012 case ir_unop_dFdx:
1013 emit(fs_inst(FS_OPCODE_DDX, this->result, op[0]));
1014 break;
1015 case ir_unop_dFdy:
1016 emit(fs_inst(FS_OPCODE_DDY, this->result, op[0]));
1017 break;
1018
1019 case ir_binop_add:
1020 emit(fs_inst(BRW_OPCODE_ADD, this->result, op[0], op[1]));
1021 break;
1022 case ir_binop_sub:
1023 assert(!"not reached: should be handled by ir_sub_to_add_neg");
1024 break;
1025
1026 case ir_binop_mul:
1027 emit(fs_inst(BRW_OPCODE_MUL, this->result, op[0], op[1]));
1028 break;
1029 case ir_binop_div:
1030 assert(!"not reached: should be handled by ir_div_to_mul_rcp");
1031 break;
1032 case ir_binop_mod:
1033 assert(!"ir_binop_mod should have been converted to b * fract(a/b)");
1034 break;
1035
1036 case ir_binop_less:
1037 inst = emit(fs_inst(BRW_OPCODE_CMP, this->result, op[0], op[1]));
1038 inst->conditional_mod = BRW_CONDITIONAL_L;
1039 emit(fs_inst(BRW_OPCODE_AND, this->result, this->result, fs_reg(0x1)));
1040 break;
1041 case ir_binop_greater:
1042 inst = emit(fs_inst(BRW_OPCODE_CMP, this->result, op[0], op[1]));
1043 inst->conditional_mod = BRW_CONDITIONAL_G;
1044 emit(fs_inst(BRW_OPCODE_AND, this->result, this->result, fs_reg(0x1)));
1045 break;
1046 case ir_binop_lequal:
1047 inst = emit(fs_inst(BRW_OPCODE_CMP, this->result, op[0], op[1]));
1048 inst->conditional_mod = BRW_CONDITIONAL_LE;
1049 emit(fs_inst(BRW_OPCODE_AND, this->result, this->result, fs_reg(0x1)));
1050 break;
1051 case ir_binop_gequal:
1052 inst = emit(fs_inst(BRW_OPCODE_CMP, this->result, op[0], op[1]));
1053 inst->conditional_mod = BRW_CONDITIONAL_GE;
1054 emit(fs_inst(BRW_OPCODE_AND, this->result, this->result, fs_reg(0x1)));
1055 break;
1056 case ir_binop_equal:
1057 case ir_binop_all_equal: /* same as nequal for scalars */
1058 inst = emit(fs_inst(BRW_OPCODE_CMP, this->result, op[0], op[1]));
1059 inst->conditional_mod = BRW_CONDITIONAL_Z;
1060 emit(fs_inst(BRW_OPCODE_AND, this->result, this->result, fs_reg(0x1)));
1061 break;
1062 case ir_binop_nequal:
1063 case ir_binop_any_nequal: /* same as nequal for scalars */
1064 inst = emit(fs_inst(BRW_OPCODE_CMP, this->result, op[0], op[1]));
1065 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1066 emit(fs_inst(BRW_OPCODE_AND, this->result, this->result, fs_reg(0x1)));
1067 break;
1068
1069 case ir_binop_logic_xor:
1070 emit(fs_inst(BRW_OPCODE_XOR, this->result, op[0], op[1]));
1071 break;
1072
1073 case ir_binop_logic_or:
1074 emit(fs_inst(BRW_OPCODE_OR, this->result, op[0], op[1]));
1075 break;
1076
1077 case ir_binop_logic_and:
1078 emit(fs_inst(BRW_OPCODE_AND, this->result, op[0], op[1]));
1079 break;
1080
1081 case ir_binop_dot:
1082 case ir_binop_cross:
1083 case ir_unop_any:
1084 assert(!"not reached: should be handled by brw_fs_channel_expressions");
1085 break;
1086
1087 case ir_unop_noise:
1088 assert(!"not reached: should be handled by lower_noise");
1089 break;
1090
1091 case ir_unop_sqrt:
1092 emit(fs_inst(FS_OPCODE_SQRT, this->result, op[0]));
1093 break;
1094
1095 case ir_unop_rsq:
1096 emit(fs_inst(FS_OPCODE_RSQ, this->result, op[0]));
1097 break;
1098
1099 case ir_unop_i2f:
1100 case ir_unop_b2f:
1101 case ir_unop_b2i:
1102 emit(fs_inst(BRW_OPCODE_MOV, this->result, op[0]));
1103 break;
1104 case ir_unop_f2i:
1105 emit(fs_inst(BRW_OPCODE_MOV, this->result, op[0]));
1106 break;
1107 case ir_unop_f2b:
1108 case ir_unop_i2b:
1109 inst = emit(fs_inst(BRW_OPCODE_CMP, this->result, op[0], fs_reg(0.0f)));
1110 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1111
1112 case ir_unop_trunc:
1113 emit(fs_inst(BRW_OPCODE_RNDD, this->result, op[0]));
1114 break;
1115 case ir_unop_ceil:
1116 op[0].negate = ~op[0].negate;
1117 inst = emit(fs_inst(BRW_OPCODE_RNDD, this->result, op[0]));
1118 this->result.negate = true;
1119 break;
1120 case ir_unop_floor:
1121 inst = emit(fs_inst(BRW_OPCODE_RNDD, this->result, op[0]));
1122 break;
1123 case ir_unop_fract:
1124 inst = emit(fs_inst(BRW_OPCODE_FRC, this->result, op[0]));
1125 break;
1126
1127 case ir_binop_min:
1128 inst = emit(fs_inst(BRW_OPCODE_CMP, this->result, op[0], op[1]));
1129 inst->conditional_mod = BRW_CONDITIONAL_L;
1130
1131 inst = emit(fs_inst(BRW_OPCODE_SEL, this->result, op[0], op[1]));
1132 inst->predicated = true;
1133 break;
1134 case ir_binop_max:
1135 inst = emit(fs_inst(BRW_OPCODE_CMP, this->result, op[0], op[1]));
1136 inst->conditional_mod = BRW_CONDITIONAL_G;
1137
1138 inst = emit(fs_inst(BRW_OPCODE_SEL, this->result, op[0], op[1]));
1139 inst->predicated = true;
1140 break;
1141
1142 case ir_binop_pow:
1143 inst = emit(fs_inst(FS_OPCODE_POW, this->result, op[0], op[1]));
1144 break;
1145
1146 case ir_unop_bit_not:
1147 case ir_unop_u2f:
1148 case ir_binop_lshift:
1149 case ir_binop_rshift:
1150 case ir_binop_bit_and:
1151 case ir_binop_bit_xor:
1152 case ir_binop_bit_or:
1153 assert(!"GLSL 1.30 features unsupported");
1154 break;
1155 }
1156 }
1157
1158 void
1159 fs_visitor::emit_assignment_writes(fs_reg &l, fs_reg &r,
1160 const glsl_type *type, bool predicated)
1161 {
1162 switch (type->base_type) {
1163 case GLSL_TYPE_FLOAT:
1164 case GLSL_TYPE_UINT:
1165 case GLSL_TYPE_INT:
1166 case GLSL_TYPE_BOOL:
1167 for (unsigned int i = 0; i < type->components(); i++) {
1168 l.type = brw_type_for_base_type(type);
1169 r.type = brw_type_for_base_type(type);
1170
1171 fs_inst *inst = emit(fs_inst(BRW_OPCODE_MOV, l, r));
1172 inst->predicated = predicated;
1173
1174 l.reg_offset++;
1175 r.reg_offset++;
1176 }
1177 break;
1178 case GLSL_TYPE_ARRAY:
1179 for (unsigned int i = 0; i < type->length; i++) {
1180 emit_assignment_writes(l, r, type->fields.array, predicated);
1181 }
1182
1183 case GLSL_TYPE_STRUCT:
1184 for (unsigned int i = 0; i < type->length; i++) {
1185 emit_assignment_writes(l, r, type->fields.structure[i].type,
1186 predicated);
1187 }
1188 break;
1189
1190 case GLSL_TYPE_SAMPLER:
1191 break;
1192
1193 default:
1194 assert(!"not reached");
1195 break;
1196 }
1197 }
1198
1199 void
1200 fs_visitor::visit(ir_assignment *ir)
1201 {
1202 struct fs_reg l, r;
1203 fs_inst *inst;
1204
1205 /* FINISHME: arrays on the lhs */
1206 ir->lhs->accept(this);
1207 l = this->result;
1208
1209 ir->rhs->accept(this);
1210 r = this->result;
1211
1212 assert(l.file != BAD_FILE);
1213 assert(r.file != BAD_FILE);
1214
1215 if (ir->condition) {
1216 /* Get the condition bool into the predicate. */
1217 ir->condition->accept(this);
1218 inst = emit(fs_inst(BRW_OPCODE_CMP, reg_null, this->result, fs_reg(0)));
1219 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1220 }
1221
1222 if (ir->lhs->type->is_scalar() ||
1223 ir->lhs->type->is_vector()) {
1224 for (int i = 0; i < ir->lhs->type->vector_elements; i++) {
1225 if (ir->write_mask & (1 << i)) {
1226 inst = emit(fs_inst(BRW_OPCODE_MOV, l, r));
1227 if (ir->condition)
1228 inst->predicated = true;
1229 r.reg_offset++;
1230 }
1231 l.reg_offset++;
1232 }
1233 } else {
1234 emit_assignment_writes(l, r, ir->lhs->type, ir->condition != NULL);
1235 }
1236 }
1237
1238 fs_inst *
1239 fs_visitor::emit_texture_gen4(ir_texture *ir, fs_reg dst, fs_reg coordinate)
1240 {
1241 int mlen;
1242 int base_mrf = 2;
1243 bool simd16 = false;
1244 fs_reg orig_dst;
1245
1246 if (ir->shadow_comparitor) {
1247 for (mlen = 0; mlen < ir->coordinate->type->vector_elements; mlen++) {
1248 emit(fs_inst(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen),
1249 coordinate));
1250 coordinate.reg_offset++;
1251 }
1252 /* gen4's SIMD8 sampler always has the slots for u,v,r present. */
1253 mlen = 3;
1254
1255 if (ir->op == ir_tex) {
1256 /* There's no plain shadow compare message, so we use shadow
1257 * compare with a bias of 0.0.
1258 */
1259 emit(fs_inst(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen),
1260 fs_reg(0.0f)));
1261 mlen++;
1262 } else if (ir->op == ir_txb) {
1263 ir->lod_info.bias->accept(this);
1264 emit(fs_inst(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen),
1265 this->result));
1266 mlen++;
1267 } else {
1268 assert(ir->op == ir_txl);
1269 ir->lod_info.lod->accept(this);
1270 emit(fs_inst(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen),
1271 this->result));
1272 mlen++;
1273 }
1274
1275 ir->shadow_comparitor->accept(this);
1276 emit(fs_inst(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen), this->result));
1277 mlen++;
1278 } else if (ir->op == ir_tex) {
1279 for (mlen = 0; mlen < ir->coordinate->type->vector_elements; mlen++) {
1280 emit(fs_inst(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen),
1281 coordinate));
1282 coordinate.reg_offset++;
1283 }
1284 /* gen4's SIMD8 sampler always has the slots for u,v,r present. */
1285 mlen = 3;
1286 } else {
1287 /* Oh joy. gen4 doesn't have SIMD8 non-shadow-compare bias/lod
1288 * instructions. We'll need to do SIMD16 here.
1289 */
1290 assert(ir->op == ir_txb || ir->op == ir_txl);
1291
1292 for (mlen = 0; mlen < ir->coordinate->type->vector_elements * 2;) {
1293 emit(fs_inst(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen),
1294 coordinate));
1295 coordinate.reg_offset++;
1296 mlen++;
1297
1298 /* The unused upper half. */
1299 mlen++;
1300 }
1301
1302 /* lod/bias appears after u/v/r. */
1303 mlen = 6;
1304
1305 if (ir->op == ir_txb) {
1306 ir->lod_info.bias->accept(this);
1307 emit(fs_inst(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen),
1308 this->result));
1309 mlen++;
1310 } else {
1311 ir->lod_info.lod->accept(this);
1312 emit(fs_inst(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen),
1313 this->result));
1314 mlen++;
1315 }
1316
1317 /* The unused upper half. */
1318 mlen++;
1319
1320 /* Now, since we're doing simd16, the return is 2 interleaved
1321 * vec4s where the odd-indexed ones are junk. We'll need to move
1322 * this weirdness around to the expected layout.
1323 */
1324 simd16 = true;
1325 orig_dst = dst;
1326 dst = fs_reg(this, glsl_type::get_array_instance(glsl_type::vec4_type,
1327 2));
1328 dst.type = BRW_REGISTER_TYPE_F;
1329 }
1330
1331 fs_inst *inst = NULL;
1332 switch (ir->op) {
1333 case ir_tex:
1334 inst = emit(fs_inst(FS_OPCODE_TEX, dst, fs_reg(MRF, base_mrf)));
1335 break;
1336 case ir_txb:
1337 inst = emit(fs_inst(FS_OPCODE_TXB, dst, fs_reg(MRF, base_mrf)));
1338 break;
1339 case ir_txl:
1340 inst = emit(fs_inst(FS_OPCODE_TXL, dst, fs_reg(MRF, base_mrf)));
1341 break;
1342 case ir_txd:
1343 case ir_txf:
1344 assert(!"GLSL 1.30 features unsupported");
1345 break;
1346 }
1347 inst->mlen = mlen;
1348
1349 if (simd16) {
1350 for (int i = 0; i < 4; i++) {
1351 emit(fs_inst(BRW_OPCODE_MOV, orig_dst, dst));
1352 orig_dst.reg_offset++;
1353 dst.reg_offset += 2;
1354 }
1355 }
1356
1357 return inst;
1358 }
1359
1360 fs_inst *
1361 fs_visitor::emit_texture_gen5(ir_texture *ir, fs_reg dst, fs_reg coordinate)
1362 {
1363 /* gen5's SIMD8 sampler has slots for u, v, r, array index, then
1364 * optional parameters like shadow comparitor or LOD bias. If
1365 * optional parameters aren't present, those base slots are
1366 * optional and don't need to be included in the message.
1367 *
1368 * We don't fill in the unnecessary slots regardless, which may
1369 * look surprising in the disassembly.
1370 */
1371 int mlen;
1372 int base_mrf = 2;
1373
1374 for (mlen = 0; mlen < ir->coordinate->type->vector_elements; mlen++) {
1375 emit(fs_inst(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen), coordinate));
1376 coordinate.reg_offset++;
1377 }
1378
1379 if (ir->shadow_comparitor) {
1380 mlen = MAX2(mlen, 4);
1381
1382 ir->shadow_comparitor->accept(this);
1383 emit(fs_inst(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen), this->result));
1384 mlen++;
1385 }
1386
1387 fs_inst *inst = NULL;
1388 switch (ir->op) {
1389 case ir_tex:
1390 inst = emit(fs_inst(FS_OPCODE_TEX, dst, fs_reg(MRF, base_mrf)));
1391 break;
1392 case ir_txb:
1393 ir->lod_info.bias->accept(this);
1394 mlen = MAX2(mlen, 4);
1395 emit(fs_inst(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen), this->result));
1396 mlen++;
1397
1398 inst = emit(fs_inst(FS_OPCODE_TXB, dst, fs_reg(MRF, base_mrf)));
1399 break;
1400 case ir_txl:
1401 ir->lod_info.lod->accept(this);
1402 mlen = MAX2(mlen, 4);
1403 emit(fs_inst(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + mlen), this->result));
1404 mlen++;
1405
1406 inst = emit(fs_inst(FS_OPCODE_TXL, dst, fs_reg(MRF, base_mrf)));
1407 break;
1408 case ir_txd:
1409 case ir_txf:
1410 assert(!"GLSL 1.30 features unsupported");
1411 break;
1412 }
1413 inst->mlen = mlen;
1414
1415 return inst;
1416 }
1417
1418 void
1419 fs_visitor::visit(ir_texture *ir)
1420 {
1421 fs_inst *inst = NULL;
1422
1423 ir->coordinate->accept(this);
1424 fs_reg coordinate = this->result;
1425
1426 /* Should be lowered by do_lower_texture_projection */
1427 assert(!ir->projector);
1428
1429 /* Writemasking doesn't eliminate channels on SIMD8 texture
1430 * samples, so don't worry about them.
1431 */
1432 fs_reg dst = fs_reg(this, glsl_type::vec4_type);
1433
1434 if (intel->gen < 5) {
1435 inst = emit_texture_gen4(ir, dst, coordinate);
1436 } else {
1437 inst = emit_texture_gen5(ir, dst, coordinate);
1438 }
1439
1440 inst->sampler =
1441 _mesa_get_sampler_uniform_value(ir->sampler,
1442 ctx->Shader.CurrentProgram,
1443 &brw->fragment_program->Base);
1444 inst->sampler = c->fp->program.Base.SamplerUnits[inst->sampler];
1445
1446 this->result = dst;
1447
1448 if (ir->shadow_comparitor)
1449 inst->shadow_compare = true;
1450
1451 if (c->key.tex_swizzles[inst->sampler] != SWIZZLE_NOOP) {
1452 fs_reg swizzle_dst = fs_reg(this, glsl_type::vec4_type);
1453
1454 for (int i = 0; i < 4; i++) {
1455 int swiz = GET_SWZ(c->key.tex_swizzles[inst->sampler], i);
1456 fs_reg l = swizzle_dst;
1457 l.reg_offset += i;
1458
1459 if (swiz == SWIZZLE_ZERO) {
1460 emit(fs_inst(BRW_OPCODE_MOV, l, fs_reg(0.0f)));
1461 } else if (swiz == SWIZZLE_ONE) {
1462 emit(fs_inst(BRW_OPCODE_MOV, l, fs_reg(1.0f)));
1463 } else {
1464 fs_reg r = dst;
1465 r.reg_offset += GET_SWZ(c->key.tex_swizzles[inst->sampler], i);
1466 emit(fs_inst(BRW_OPCODE_MOV, l, r));
1467 }
1468 }
1469 this->result = swizzle_dst;
1470 }
1471 }
1472
1473 void
1474 fs_visitor::visit(ir_swizzle *ir)
1475 {
1476 ir->val->accept(this);
1477 fs_reg val = this->result;
1478
1479 if (ir->type->vector_elements == 1) {
1480 this->result.reg_offset += ir->mask.x;
1481 return;
1482 }
1483
1484 fs_reg result = fs_reg(this, ir->type);
1485 this->result = result;
1486
1487 for (unsigned int i = 0; i < ir->type->vector_elements; i++) {
1488 fs_reg channel = val;
1489 int swiz = 0;
1490
1491 switch (i) {
1492 case 0:
1493 swiz = ir->mask.x;
1494 break;
1495 case 1:
1496 swiz = ir->mask.y;
1497 break;
1498 case 2:
1499 swiz = ir->mask.z;
1500 break;
1501 case 3:
1502 swiz = ir->mask.w;
1503 break;
1504 }
1505
1506 channel.reg_offset += swiz;
1507 emit(fs_inst(BRW_OPCODE_MOV, result, channel));
1508 result.reg_offset++;
1509 }
1510 }
1511
1512 void
1513 fs_visitor::visit(ir_discard *ir)
1514 {
1515 fs_reg temp = fs_reg(this, glsl_type::uint_type);
1516
1517 assert(ir->condition == NULL); /* FINISHME */
1518
1519 emit(fs_inst(FS_OPCODE_DISCARD, temp, temp));
1520 kill_emitted = true;
1521 }
1522
1523 void
1524 fs_visitor::visit(ir_constant *ir)
1525 {
1526 fs_reg reg(this, ir->type);
1527 this->result = reg;
1528
1529 for (unsigned int i = 0; i < ir->type->vector_elements; i++) {
1530 switch (ir->type->base_type) {
1531 case GLSL_TYPE_FLOAT:
1532 emit(fs_inst(BRW_OPCODE_MOV, reg, fs_reg(ir->value.f[i])));
1533 break;
1534 case GLSL_TYPE_UINT:
1535 emit(fs_inst(BRW_OPCODE_MOV, reg, fs_reg(ir->value.u[i])));
1536 break;
1537 case GLSL_TYPE_INT:
1538 emit(fs_inst(BRW_OPCODE_MOV, reg, fs_reg(ir->value.i[i])));
1539 break;
1540 case GLSL_TYPE_BOOL:
1541 emit(fs_inst(BRW_OPCODE_MOV, reg, fs_reg((int)ir->value.b[i])));
1542 break;
1543 default:
1544 assert(!"Non-float/uint/int/bool constant");
1545 }
1546 reg.reg_offset++;
1547 }
1548 }
1549
1550 void
1551 fs_visitor::visit(ir_if *ir)
1552 {
1553 fs_inst *inst;
1554
1555 /* Don't point the annotation at the if statement, because then it plus
1556 * the then and else blocks get printed.
1557 */
1558 this->base_ir = ir->condition;
1559
1560 /* Generate the condition into the condition code. */
1561 ir->condition->accept(this);
1562 inst = emit(fs_inst(BRW_OPCODE_MOV, fs_reg(brw_null_reg()), this->result));
1563 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1564
1565 inst = emit(fs_inst(BRW_OPCODE_IF));
1566 inst->predicated = true;
1567
1568 foreach_iter(exec_list_iterator, iter, ir->then_instructions) {
1569 ir_instruction *ir = (ir_instruction *)iter.get();
1570 this->base_ir = ir;
1571
1572 ir->accept(this);
1573 }
1574
1575 if (!ir->else_instructions.is_empty()) {
1576 emit(fs_inst(BRW_OPCODE_ELSE));
1577
1578 foreach_iter(exec_list_iterator, iter, ir->else_instructions) {
1579 ir_instruction *ir = (ir_instruction *)iter.get();
1580 this->base_ir = ir;
1581
1582 ir->accept(this);
1583 }
1584 }
1585
1586 emit(fs_inst(BRW_OPCODE_ENDIF));
1587 }
1588
1589 void
1590 fs_visitor::visit(ir_loop *ir)
1591 {
1592 fs_reg counter = reg_undef;
1593
1594 if (ir->counter) {
1595 this->base_ir = ir->counter;
1596 ir->counter->accept(this);
1597 counter = *(variable_storage(ir->counter));
1598
1599 if (ir->from) {
1600 this->base_ir = ir->from;
1601 ir->from->accept(this);
1602
1603 emit(fs_inst(BRW_OPCODE_MOV, counter, this->result));
1604 }
1605 }
1606
1607 emit(fs_inst(BRW_OPCODE_DO));
1608
1609 if (ir->to) {
1610 this->base_ir = ir->to;
1611 ir->to->accept(this);
1612
1613 fs_inst *inst = emit(fs_inst(BRW_OPCODE_CMP, reg_null,
1614 counter, this->result));
1615 switch (ir->cmp) {
1616 case ir_binop_equal:
1617 inst->conditional_mod = BRW_CONDITIONAL_Z;
1618 break;
1619 case ir_binop_nequal:
1620 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1621 break;
1622 case ir_binop_gequal:
1623 inst->conditional_mod = BRW_CONDITIONAL_GE;
1624 break;
1625 case ir_binop_lequal:
1626 inst->conditional_mod = BRW_CONDITIONAL_LE;
1627 break;
1628 case ir_binop_greater:
1629 inst->conditional_mod = BRW_CONDITIONAL_G;
1630 break;
1631 case ir_binop_less:
1632 inst->conditional_mod = BRW_CONDITIONAL_L;
1633 break;
1634 default:
1635 assert(!"not reached: unknown loop condition");
1636 this->fail = true;
1637 break;
1638 }
1639
1640 inst = emit(fs_inst(BRW_OPCODE_BREAK));
1641 inst->predicated = true;
1642 }
1643
1644 foreach_iter(exec_list_iterator, iter, ir->body_instructions) {
1645 ir_instruction *ir = (ir_instruction *)iter.get();
1646
1647 this->base_ir = ir;
1648 ir->accept(this);
1649 }
1650
1651 if (ir->increment) {
1652 this->base_ir = ir->increment;
1653 ir->increment->accept(this);
1654 emit(fs_inst(BRW_OPCODE_ADD, counter, counter, this->result));
1655 }
1656
1657 emit(fs_inst(BRW_OPCODE_WHILE));
1658 }
1659
1660 void
1661 fs_visitor::visit(ir_loop_jump *ir)
1662 {
1663 switch (ir->mode) {
1664 case ir_loop_jump::jump_break:
1665 emit(fs_inst(BRW_OPCODE_BREAK));
1666 break;
1667 case ir_loop_jump::jump_continue:
1668 emit(fs_inst(BRW_OPCODE_CONTINUE));
1669 break;
1670 }
1671 }
1672
1673 void
1674 fs_visitor::visit(ir_call *ir)
1675 {
1676 assert(!"FINISHME");
1677 }
1678
1679 void
1680 fs_visitor::visit(ir_return *ir)
1681 {
1682 assert(!"FINISHME");
1683 }
1684
1685 void
1686 fs_visitor::visit(ir_function *ir)
1687 {
1688 /* Ignore function bodies other than main() -- we shouldn't see calls to
1689 * them since they should all be inlined before we get to ir_to_mesa.
1690 */
1691 if (strcmp(ir->name, "main") == 0) {
1692 const ir_function_signature *sig;
1693 exec_list empty;
1694
1695 sig = ir->matching_signature(&empty);
1696
1697 assert(sig);
1698
1699 foreach_iter(exec_list_iterator, iter, sig->body) {
1700 ir_instruction *ir = (ir_instruction *)iter.get();
1701 this->base_ir = ir;
1702
1703 ir->accept(this);
1704 }
1705 }
1706 }
1707
1708 void
1709 fs_visitor::visit(ir_function_signature *ir)
1710 {
1711 assert(!"not reached");
1712 (void)ir;
1713 }
1714
1715 fs_inst *
1716 fs_visitor::emit(fs_inst inst)
1717 {
1718 fs_inst *list_inst = new(mem_ctx) fs_inst;
1719 *list_inst = inst;
1720
1721 list_inst->annotation = this->current_annotation;
1722 list_inst->ir = this->base_ir;
1723
1724 this->instructions.push_tail(list_inst);
1725
1726 return list_inst;
1727 }
1728
1729 /** Emits a dummy fragment shader consisting of magenta for bringup purposes. */
1730 void
1731 fs_visitor::emit_dummy_fs()
1732 {
1733 /* Everyone's favorite color. */
1734 emit(fs_inst(BRW_OPCODE_MOV,
1735 fs_reg(MRF, 2),
1736 fs_reg(1.0f)));
1737 emit(fs_inst(BRW_OPCODE_MOV,
1738 fs_reg(MRF, 3),
1739 fs_reg(0.0f)));
1740 emit(fs_inst(BRW_OPCODE_MOV,
1741 fs_reg(MRF, 4),
1742 fs_reg(1.0f)));
1743 emit(fs_inst(BRW_OPCODE_MOV,
1744 fs_reg(MRF, 5),
1745 fs_reg(0.0f)));
1746
1747 fs_inst *write;
1748 write = emit(fs_inst(FS_OPCODE_FB_WRITE,
1749 fs_reg(0),
1750 fs_reg(0)));
1751 }
1752
1753 /* The register location here is relative to the start of the URB
1754 * data. It will get adjusted to be a real location before
1755 * generate_code() time.
1756 */
1757 struct brw_reg
1758 fs_visitor::interp_reg(int location, int channel)
1759 {
1760 int regnr = urb_setup[location] * 2 + channel / 2;
1761 int stride = (channel & 1) * 4;
1762
1763 assert(urb_setup[location] != -1);
1764
1765 return brw_vec1_grf(regnr, stride);
1766 }
1767
1768 /** Emits the interpolation for the varying inputs. */
1769 void
1770 fs_visitor::emit_interpolation_setup_gen4()
1771 {
1772 struct brw_reg g1_uw = retype(brw_vec1_grf(1, 0), BRW_REGISTER_TYPE_UW);
1773
1774 this->current_annotation = "compute pixel centers";
1775 this->pixel_x = fs_reg(this, glsl_type::uint_type);
1776 this->pixel_y = fs_reg(this, glsl_type::uint_type);
1777 this->pixel_x.type = BRW_REGISTER_TYPE_UW;
1778 this->pixel_y.type = BRW_REGISTER_TYPE_UW;
1779 emit(fs_inst(BRW_OPCODE_ADD,
1780 this->pixel_x,
1781 fs_reg(stride(suboffset(g1_uw, 4), 2, 4, 0)),
1782 fs_reg(brw_imm_v(0x10101010))));
1783 emit(fs_inst(BRW_OPCODE_ADD,
1784 this->pixel_y,
1785 fs_reg(stride(suboffset(g1_uw, 5), 2, 4, 0)),
1786 fs_reg(brw_imm_v(0x11001100))));
1787
1788 this->current_annotation = "compute pixel deltas from v0";
1789 if (brw->has_pln) {
1790 this->delta_x = fs_reg(this, glsl_type::vec2_type);
1791 this->delta_y = this->delta_x;
1792 this->delta_y.reg_offset++;
1793 } else {
1794 this->delta_x = fs_reg(this, glsl_type::float_type);
1795 this->delta_y = fs_reg(this, glsl_type::float_type);
1796 }
1797 emit(fs_inst(BRW_OPCODE_ADD,
1798 this->delta_x,
1799 this->pixel_x,
1800 fs_reg(negate(brw_vec1_grf(1, 0)))));
1801 emit(fs_inst(BRW_OPCODE_ADD,
1802 this->delta_y,
1803 this->pixel_y,
1804 fs_reg(negate(brw_vec1_grf(1, 1)))));
1805
1806 this->current_annotation = "compute pos.w and 1/pos.w";
1807 /* Compute wpos.w. It's always in our setup, since it's needed to
1808 * interpolate the other attributes.
1809 */
1810 this->wpos_w = fs_reg(this, glsl_type::float_type);
1811 emit(fs_inst(FS_OPCODE_LINTERP, wpos_w, this->delta_x, this->delta_y,
1812 interp_reg(FRAG_ATTRIB_WPOS, 3)));
1813 /* Compute the pixel 1/W value from wpos.w. */
1814 this->pixel_w = fs_reg(this, glsl_type::float_type);
1815 emit(fs_inst(FS_OPCODE_RCP, this->pixel_w, wpos_w));
1816 this->current_annotation = NULL;
1817 }
1818
1819 /** Emits the interpolation for the varying inputs. */
1820 void
1821 fs_visitor::emit_interpolation_setup_gen6()
1822 {
1823 struct brw_reg g1_uw = retype(brw_vec1_grf(1, 0), BRW_REGISTER_TYPE_UW);
1824
1825 /* If the pixel centers end up used, the setup is the same as for gen4. */
1826 this->current_annotation = "compute pixel centers";
1827 this->pixel_x = fs_reg(this, glsl_type::uint_type);
1828 this->pixel_y = fs_reg(this, glsl_type::uint_type);
1829 this->pixel_x.type = BRW_REGISTER_TYPE_UW;
1830 this->pixel_y.type = BRW_REGISTER_TYPE_UW;
1831 emit(fs_inst(BRW_OPCODE_ADD,
1832 this->pixel_x,
1833 fs_reg(stride(suboffset(g1_uw, 4), 2, 4, 0)),
1834 fs_reg(brw_imm_v(0x10101010))));
1835 emit(fs_inst(BRW_OPCODE_ADD,
1836 this->pixel_y,
1837 fs_reg(stride(suboffset(g1_uw, 5), 2, 4, 0)),
1838 fs_reg(brw_imm_v(0x11001100))));
1839
1840 this->current_annotation = "compute 1/pos.w";
1841 this->wpos_w = fs_reg(brw_vec8_grf(c->key.source_w_reg, 0));
1842 this->pixel_w = fs_reg(this, glsl_type::float_type);
1843 emit(fs_inst(FS_OPCODE_RCP, this->pixel_w, wpos_w));
1844
1845 this->delta_x = fs_reg(brw_vec8_grf(2, 0));
1846 this->delta_y = fs_reg(brw_vec8_grf(3, 0));
1847
1848 this->current_annotation = NULL;
1849 }
1850
1851 void
1852 fs_visitor::emit_fb_writes()
1853 {
1854 this->current_annotation = "FB write header";
1855 GLboolean header_present = GL_TRUE;
1856 int nr = 0;
1857
1858 if (intel->gen >= 6 &&
1859 !this->kill_emitted &&
1860 c->key.nr_color_regions == 1) {
1861 header_present = false;
1862 }
1863
1864 if (header_present) {
1865 /* m0, m1 header */
1866 nr += 2;
1867 }
1868
1869 if (c->key.aa_dest_stencil_reg) {
1870 emit(fs_inst(BRW_OPCODE_MOV, fs_reg(MRF, nr++),
1871 fs_reg(brw_vec8_grf(c->key.aa_dest_stencil_reg, 0))));
1872 }
1873
1874 /* Reserve space for color. It'll be filled in per MRT below. */
1875 int color_mrf = nr;
1876 nr += 4;
1877
1878 if (c->key.source_depth_to_render_target) {
1879 if (c->key.computes_depth) {
1880 /* Hand over gl_FragDepth. */
1881 assert(this->frag_depth);
1882 fs_reg depth = *(variable_storage(this->frag_depth));
1883
1884 emit(fs_inst(BRW_OPCODE_MOV, fs_reg(MRF, nr++), depth));
1885 } else {
1886 /* Pass through the payload depth. */
1887 emit(fs_inst(BRW_OPCODE_MOV, fs_reg(MRF, nr++),
1888 fs_reg(brw_vec8_grf(c->key.source_depth_reg, 0))));
1889 }
1890 }
1891
1892 if (c->key.dest_depth_reg) {
1893 emit(fs_inst(BRW_OPCODE_MOV, fs_reg(MRF, nr++),
1894 fs_reg(brw_vec8_grf(c->key.dest_depth_reg, 0))));
1895 }
1896
1897 fs_reg color = reg_undef;
1898 if (this->frag_color)
1899 color = *(variable_storage(this->frag_color));
1900 else if (this->frag_data)
1901 color = *(variable_storage(this->frag_data));
1902
1903 for (int target = 0; target < c->key.nr_color_regions; target++) {
1904 this->current_annotation = talloc_asprintf(this->mem_ctx,
1905 "FB write target %d",
1906 target);
1907 if (this->frag_color || this->frag_data) {
1908 for (int i = 0; i < 4; i++) {
1909 emit(fs_inst(BRW_OPCODE_MOV,
1910 fs_reg(MRF, color_mrf + i),
1911 color));
1912 color.reg_offset++;
1913 }
1914 }
1915
1916 if (this->frag_color)
1917 color.reg_offset -= 4;
1918
1919 fs_inst *inst = emit(fs_inst(FS_OPCODE_FB_WRITE,
1920 reg_undef, reg_undef));
1921 inst->target = target;
1922 inst->mlen = nr;
1923 if (target == c->key.nr_color_regions - 1)
1924 inst->eot = true;
1925 inst->header_present = header_present;
1926 }
1927
1928 if (c->key.nr_color_regions == 0) {
1929 fs_inst *inst = emit(fs_inst(FS_OPCODE_FB_WRITE,
1930 reg_undef, reg_undef));
1931 inst->mlen = nr;
1932 inst->eot = true;
1933 inst->header_present = header_present;
1934 }
1935
1936 this->current_annotation = NULL;
1937 }
1938
1939 void
1940 fs_visitor::generate_fb_write(fs_inst *inst)
1941 {
1942 GLboolean eot = inst->eot;
1943 struct brw_reg implied_header;
1944
1945 /* Header is 2 regs, g0 and g1 are the contents. g0 will be implied
1946 * move, here's g1.
1947 */
1948 brw_push_insn_state(p);
1949 brw_set_mask_control(p, BRW_MASK_DISABLE);
1950 brw_set_compression_control(p, BRW_COMPRESSION_NONE);
1951
1952 if (inst->header_present) {
1953 if (intel->gen >= 6) {
1954 brw_MOV(p,
1955 brw_message_reg(0),
1956 brw_vec8_grf(0, 0));
1957 implied_header = brw_null_reg();
1958 } else {
1959 implied_header = retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UW);
1960 }
1961
1962 brw_MOV(p,
1963 brw_message_reg(1),
1964 brw_vec8_grf(1, 0));
1965 } else {
1966 implied_header = brw_null_reg();
1967 }
1968
1969 brw_pop_insn_state(p);
1970
1971 brw_fb_WRITE(p,
1972 8, /* dispatch_width */
1973 retype(vec8(brw_null_reg()), BRW_REGISTER_TYPE_UW),
1974 0, /* base MRF */
1975 implied_header,
1976 inst->target,
1977 inst->mlen,
1978 0,
1979 eot);
1980 }
1981
1982 void
1983 fs_visitor::generate_linterp(fs_inst *inst,
1984 struct brw_reg dst, struct brw_reg *src)
1985 {
1986 struct brw_reg delta_x = src[0];
1987 struct brw_reg delta_y = src[1];
1988 struct brw_reg interp = src[2];
1989
1990 if (brw->has_pln &&
1991 delta_y.nr == delta_x.nr + 1 &&
1992 (intel->gen >= 6 || (delta_x.nr & 1) == 0)) {
1993 brw_PLN(p, dst, interp, delta_x);
1994 } else {
1995 brw_LINE(p, brw_null_reg(), interp, delta_x);
1996 brw_MAC(p, dst, suboffset(interp, 1), delta_y);
1997 }
1998 }
1999
2000 void
2001 fs_visitor::generate_math(fs_inst *inst,
2002 struct brw_reg dst, struct brw_reg *src)
2003 {
2004 int op;
2005
2006 switch (inst->opcode) {
2007 case FS_OPCODE_RCP:
2008 op = BRW_MATH_FUNCTION_INV;
2009 break;
2010 case FS_OPCODE_RSQ:
2011 op = BRW_MATH_FUNCTION_RSQ;
2012 break;
2013 case FS_OPCODE_SQRT:
2014 op = BRW_MATH_FUNCTION_SQRT;
2015 break;
2016 case FS_OPCODE_EXP2:
2017 op = BRW_MATH_FUNCTION_EXP;
2018 break;
2019 case FS_OPCODE_LOG2:
2020 op = BRW_MATH_FUNCTION_LOG;
2021 break;
2022 case FS_OPCODE_POW:
2023 op = BRW_MATH_FUNCTION_POW;
2024 break;
2025 case FS_OPCODE_SIN:
2026 op = BRW_MATH_FUNCTION_SIN;
2027 break;
2028 case FS_OPCODE_COS:
2029 op = BRW_MATH_FUNCTION_COS;
2030 break;
2031 default:
2032 assert(!"not reached: unknown math function");
2033 op = 0;
2034 break;
2035 }
2036
2037 if (inst->opcode == FS_OPCODE_POW) {
2038 brw_MOV(p, brw_message_reg(3), src[1]);
2039 }
2040
2041 brw_math(p, dst,
2042 op,
2043 inst->saturate ? BRW_MATH_SATURATE_SATURATE :
2044 BRW_MATH_SATURATE_NONE,
2045 2, src[0],
2046 BRW_MATH_DATA_VECTOR,
2047 BRW_MATH_PRECISION_FULL);
2048 }
2049
2050 void
2051 fs_visitor::generate_tex(fs_inst *inst, struct brw_reg dst, struct brw_reg src)
2052 {
2053 int msg_type = -1;
2054 int rlen = 4;
2055 uint32_t simd_mode = BRW_SAMPLER_SIMD_MODE_SIMD8;
2056
2057 if (intel->gen == 5) {
2058 switch (inst->opcode) {
2059 case FS_OPCODE_TEX:
2060 if (inst->shadow_compare) {
2061 msg_type = BRW_SAMPLER_MESSAGE_SAMPLE_COMPARE_GEN5;
2062 } else {
2063 msg_type = BRW_SAMPLER_MESSAGE_SAMPLE_GEN5;
2064 }
2065 break;
2066 case FS_OPCODE_TXB:
2067 if (inst->shadow_compare) {
2068 msg_type = BRW_SAMPLER_MESSAGE_SAMPLE_BIAS_COMPARE_GEN5;
2069 } else {
2070 msg_type = BRW_SAMPLER_MESSAGE_SAMPLE_BIAS_GEN5;
2071 }
2072 break;
2073 }
2074 } else {
2075 switch (inst->opcode) {
2076 case FS_OPCODE_TEX:
2077 /* Note that G45 and older determines shadow compare and dispatch width
2078 * from message length for most messages.
2079 */
2080 msg_type = BRW_SAMPLER_MESSAGE_SIMD8_SAMPLE;
2081 if (inst->shadow_compare) {
2082 assert(inst->mlen == 5);
2083 } else {
2084 assert(inst->mlen <= 6);
2085 }
2086 break;
2087 case FS_OPCODE_TXB:
2088 if (inst->shadow_compare) {
2089 assert(inst->mlen == 5);
2090 msg_type = BRW_SAMPLER_MESSAGE_SIMD8_SAMPLE;
2091 } else {
2092 assert(inst->mlen == 8);
2093 msg_type = BRW_SAMPLER_MESSAGE_SIMD16_SAMPLE_BIAS;
2094 simd_mode = BRW_SAMPLER_SIMD_MODE_SIMD16;
2095 }
2096 break;
2097 }
2098 }
2099 assert(msg_type != -1);
2100
2101 if (simd_mode == BRW_SAMPLER_SIMD_MODE_SIMD16) {
2102 rlen = 8;
2103 dst = vec16(dst);
2104 }
2105
2106 /* g0 header. */
2107 src.nr--;
2108
2109 brw_SAMPLE(p,
2110 retype(dst, BRW_REGISTER_TYPE_UW),
2111 src.nr,
2112 retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UW),
2113 SURF_INDEX_TEXTURE(inst->sampler),
2114 inst->sampler,
2115 WRITEMASK_XYZW,
2116 msg_type,
2117 rlen,
2118 inst->mlen + 1,
2119 0,
2120 1,
2121 simd_mode);
2122 }
2123
2124
2125 /* For OPCODE_DDX and OPCODE_DDY, per channel of output we've got input
2126 * looking like:
2127 *
2128 * arg0: ss0.tl ss0.tr ss0.bl ss0.br ss1.tl ss1.tr ss1.bl ss1.br
2129 *
2130 * and we're trying to produce:
2131 *
2132 * DDX DDY
2133 * dst: (ss0.tr - ss0.tl) (ss0.tl - ss0.bl)
2134 * (ss0.tr - ss0.tl) (ss0.tr - ss0.br)
2135 * (ss0.br - ss0.bl) (ss0.tl - ss0.bl)
2136 * (ss0.br - ss0.bl) (ss0.tr - ss0.br)
2137 * (ss1.tr - ss1.tl) (ss1.tl - ss1.bl)
2138 * (ss1.tr - ss1.tl) (ss1.tr - ss1.br)
2139 * (ss1.br - ss1.bl) (ss1.tl - ss1.bl)
2140 * (ss1.br - ss1.bl) (ss1.tr - ss1.br)
2141 *
2142 * and add another set of two more subspans if in 16-pixel dispatch mode.
2143 *
2144 * For DDX, it ends up being easy: width = 2, horiz=0 gets us the same result
2145 * for each pair, and vertstride = 2 jumps us 2 elements after processing a
2146 * pair. But for DDY, it's harder, as we want to produce the pairs swizzled
2147 * between each other. We could probably do it like ddx and swizzle the right
2148 * order later, but bail for now and just produce
2149 * ((ss0.tl - ss0.bl)x4 (ss1.tl - ss1.bl)x4)
2150 */
2151 void
2152 fs_visitor::generate_ddx(fs_inst *inst, struct brw_reg dst, struct brw_reg src)
2153 {
2154 struct brw_reg src0 = brw_reg(src.file, src.nr, 1,
2155 BRW_REGISTER_TYPE_F,
2156 BRW_VERTICAL_STRIDE_2,
2157 BRW_WIDTH_2,
2158 BRW_HORIZONTAL_STRIDE_0,
2159 BRW_SWIZZLE_XYZW, WRITEMASK_XYZW);
2160 struct brw_reg src1 = brw_reg(src.file, src.nr, 0,
2161 BRW_REGISTER_TYPE_F,
2162 BRW_VERTICAL_STRIDE_2,
2163 BRW_WIDTH_2,
2164 BRW_HORIZONTAL_STRIDE_0,
2165 BRW_SWIZZLE_XYZW, WRITEMASK_XYZW);
2166 brw_ADD(p, dst, src0, negate(src1));
2167 }
2168
2169 void
2170 fs_visitor::generate_ddy(fs_inst *inst, struct brw_reg dst, struct brw_reg src)
2171 {
2172 struct brw_reg src0 = brw_reg(src.file, src.nr, 0,
2173 BRW_REGISTER_TYPE_F,
2174 BRW_VERTICAL_STRIDE_4,
2175 BRW_WIDTH_4,
2176 BRW_HORIZONTAL_STRIDE_0,
2177 BRW_SWIZZLE_XYZW, WRITEMASK_XYZW);
2178 struct brw_reg src1 = brw_reg(src.file, src.nr, 2,
2179 BRW_REGISTER_TYPE_F,
2180 BRW_VERTICAL_STRIDE_4,
2181 BRW_WIDTH_4,
2182 BRW_HORIZONTAL_STRIDE_0,
2183 BRW_SWIZZLE_XYZW, WRITEMASK_XYZW);
2184 brw_ADD(p, dst, src0, negate(src1));
2185 }
2186
2187 void
2188 fs_visitor::generate_discard(fs_inst *inst, struct brw_reg temp)
2189 {
2190 struct brw_reg g0 = retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UW);
2191 temp = brw_uw1_reg(temp.file, temp.nr, 0);
2192
2193 brw_push_insn_state(p);
2194 brw_set_mask_control(p, BRW_MASK_DISABLE);
2195 brw_NOT(p, temp, brw_mask_reg(1)); /* IMASK */
2196 brw_AND(p, g0, temp, g0);
2197 brw_pop_insn_state(p);
2198 }
2199
2200 void
2201 fs_visitor::assign_curb_setup()
2202 {
2203 c->prog_data.first_curbe_grf = c->key.nr_payload_regs;
2204 c->prog_data.curb_read_length = ALIGN(c->prog_data.nr_params, 8) / 8;
2205
2206 /* Map the offsets in the UNIFORM file to fixed HW regs. */
2207 foreach_iter(exec_list_iterator, iter, this->instructions) {
2208 fs_inst *inst = (fs_inst *)iter.get();
2209
2210 for (unsigned int i = 0; i < 3; i++) {
2211 if (inst->src[i].file == UNIFORM) {
2212 int constant_nr = inst->src[i].hw_reg + inst->src[i].reg_offset;
2213 struct brw_reg brw_reg = brw_vec1_grf(c->prog_data.first_curbe_grf +
2214 constant_nr / 8,
2215 constant_nr % 8);
2216
2217 inst->src[i].file = FIXED_HW_REG;
2218 inst->src[i].fixed_hw_reg = brw_reg;
2219 }
2220 }
2221 }
2222 }
2223
2224 void
2225 fs_visitor::calculate_urb_setup()
2226 {
2227 for (unsigned int i = 0; i < FRAG_ATTRIB_MAX; i++) {
2228 urb_setup[i] = -1;
2229 }
2230
2231 int urb_next = 0;
2232 /* Figure out where each of the incoming setup attributes lands. */
2233 if (intel->gen >= 6) {
2234 for (unsigned int i = 0; i < FRAG_ATTRIB_MAX; i++) {
2235 if (i == FRAG_ATTRIB_WPOS ||
2236 (brw->fragment_program->Base.InputsRead & BITFIELD64_BIT(i))) {
2237 urb_setup[i] = urb_next++;
2238 }
2239 }
2240 } else {
2241 /* FINISHME: The sf doesn't map VS->FS inputs for us very well. */
2242 for (unsigned int i = 0; i < VERT_RESULT_MAX; i++) {
2243 if (c->key.vp_outputs_written & BITFIELD64_BIT(i)) {
2244 int fp_index;
2245
2246 if (i >= VERT_RESULT_VAR0)
2247 fp_index = i - (VERT_RESULT_VAR0 - FRAG_ATTRIB_VAR0);
2248 else if (i <= VERT_RESULT_TEX7)
2249 fp_index = i;
2250 else
2251 fp_index = -1;
2252
2253 if (fp_index >= 0)
2254 urb_setup[fp_index] = urb_next++;
2255 }
2256 }
2257 }
2258
2259 /* Each attribute is 4 setup channels, each of which is half a reg. */
2260 c->prog_data.urb_read_length = urb_next * 2;
2261 }
2262
2263 void
2264 fs_visitor::assign_urb_setup()
2265 {
2266 int urb_start = c->prog_data.first_curbe_grf + c->prog_data.curb_read_length;
2267
2268 /* Offset all the urb_setup[] index by the actual position of the
2269 * setup regs, now that the location of the constants has been chosen.
2270 */
2271 foreach_iter(exec_list_iterator, iter, this->instructions) {
2272 fs_inst *inst = (fs_inst *)iter.get();
2273
2274 if (inst->opcode != FS_OPCODE_LINTERP)
2275 continue;
2276
2277 assert(inst->src[2].file == FIXED_HW_REG);
2278
2279 inst->src[2].fixed_hw_reg.nr += urb_start;
2280 }
2281
2282 this->first_non_payload_grf = urb_start + c->prog_data.urb_read_length;
2283 }
2284
2285 static void
2286 assign_reg(int *reg_hw_locations, fs_reg *reg)
2287 {
2288 if (reg->file == GRF && reg->reg != 0) {
2289 reg->hw_reg = reg_hw_locations[reg->reg] + reg->reg_offset;
2290 reg->reg = 0;
2291 }
2292 }
2293
2294 void
2295 fs_visitor::assign_regs_trivial()
2296 {
2297 int last_grf = 0;
2298 int hw_reg_mapping[this->virtual_grf_next];
2299 int i;
2300
2301 hw_reg_mapping[0] = 0;
2302 hw_reg_mapping[1] = this->first_non_payload_grf;
2303 for (i = 2; i < this->virtual_grf_next; i++) {
2304 hw_reg_mapping[i] = (hw_reg_mapping[i - 1] +
2305 this->virtual_grf_sizes[i - 1]);
2306 }
2307 last_grf = hw_reg_mapping[i - 1] + this->virtual_grf_sizes[i - 1];
2308
2309 foreach_iter(exec_list_iterator, iter, this->instructions) {
2310 fs_inst *inst = (fs_inst *)iter.get();
2311
2312 assign_reg(hw_reg_mapping, &inst->dst);
2313 assign_reg(hw_reg_mapping, &inst->src[0]);
2314 assign_reg(hw_reg_mapping, &inst->src[1]);
2315 }
2316
2317 this->grf_used = last_grf + 1;
2318 }
2319
2320 void
2321 fs_visitor::assign_regs()
2322 {
2323 int last_grf = 0;
2324 int hw_reg_mapping[this->virtual_grf_next + 1];
2325 int base_reg_count = BRW_MAX_GRF - this->first_non_payload_grf;
2326 int class_sizes[base_reg_count];
2327 int class_count = 0;
2328 int aligned_pair_class = -1;
2329
2330 /* Set up the register classes.
2331 *
2332 * The base registers store a scalar value. For texture samples,
2333 * we get virtual GRFs composed of 4 contiguous hw register. For
2334 * structures and arrays, we store them as contiguous larger things
2335 * than that, though we should be able to do better most of the
2336 * time.
2337 */
2338 class_sizes[class_count++] = 1;
2339 if (brw->has_pln && intel->gen < 6) {
2340 /* Always set up the (unaligned) pairs for gen5, so we can find
2341 * them for making the aligned pair class.
2342 */
2343 class_sizes[class_count++] = 2;
2344 }
2345 for (int r = 1; r < this->virtual_grf_next; r++) {
2346 int i;
2347
2348 for (i = 0; i < class_count; i++) {
2349 if (class_sizes[i] == this->virtual_grf_sizes[r])
2350 break;
2351 }
2352 if (i == class_count) {
2353 if (this->virtual_grf_sizes[r] >= base_reg_count) {
2354 fprintf(stderr, "Object too large to register allocate.\n");
2355 this->fail = true;
2356 }
2357
2358 class_sizes[class_count++] = this->virtual_grf_sizes[r];
2359 }
2360 }
2361
2362 int ra_reg_count = 0;
2363 int class_base_reg[class_count];
2364 int class_reg_count[class_count];
2365 int classes[class_count + 1];
2366
2367 for (int i = 0; i < class_count; i++) {
2368 class_base_reg[i] = ra_reg_count;
2369 class_reg_count[i] = base_reg_count - (class_sizes[i] - 1);
2370 ra_reg_count += class_reg_count[i];
2371 }
2372
2373 struct ra_regs *regs = ra_alloc_reg_set(ra_reg_count);
2374 for (int i = 0; i < class_count; i++) {
2375 classes[i] = ra_alloc_reg_class(regs);
2376
2377 for (int i_r = 0; i_r < class_reg_count[i]; i_r++) {
2378 ra_class_add_reg(regs, classes[i], class_base_reg[i] + i_r);
2379 }
2380
2381 /* Add conflicts between our contiguous registers aliasing
2382 * base regs and other register classes' contiguous registers
2383 * that alias base regs, or the base regs themselves for classes[0].
2384 */
2385 for (int c = 0; c <= i; c++) {
2386 for (int i_r = 0; i_r < class_reg_count[i]; i_r++) {
2387 for (int c_r = MAX2(0, i_r - (class_sizes[c] - 1));
2388 c_r < MIN2(class_reg_count[c], i_r + class_sizes[i]);
2389 c_r++) {
2390
2391 if (0) {
2392 printf("%d/%d conflicts %d/%d\n",
2393 class_sizes[i], this->first_non_payload_grf + i_r,
2394 class_sizes[c], this->first_non_payload_grf + c_r);
2395 }
2396
2397 ra_add_reg_conflict(regs,
2398 class_base_reg[i] + i_r,
2399 class_base_reg[c] + c_r);
2400 }
2401 }
2402 }
2403 }
2404
2405 /* Add a special class for aligned pairs, which we'll put delta_x/y
2406 * in on gen5 so that we can do PLN.
2407 */
2408 if (brw->has_pln && intel->gen < 6) {
2409 int reg_count = (base_reg_count - 1) / 2;
2410 int unaligned_pair_class = 1;
2411 assert(class_sizes[unaligned_pair_class] == 2);
2412
2413 aligned_pair_class = class_count;
2414 classes[aligned_pair_class] = ra_alloc_reg_class(regs);
2415 class_base_reg[aligned_pair_class] = 0;
2416 class_reg_count[aligned_pair_class] = 0;
2417 int start = (this->first_non_payload_grf & 1) ? 1 : 0;
2418
2419 for (int i = 0; i < reg_count; i++) {
2420 ra_class_add_reg(regs, classes[aligned_pair_class],
2421 class_base_reg[unaligned_pair_class] + i * 2 + start);
2422 }
2423 class_count++;
2424 }
2425
2426 ra_set_finalize(regs);
2427
2428 struct ra_graph *g = ra_alloc_interference_graph(regs,
2429 this->virtual_grf_next);
2430 /* Node 0 is just a placeholder to keep virtual_grf[] mapping 1:1
2431 * with nodes.
2432 */
2433 ra_set_node_class(g, 0, classes[0]);
2434
2435 for (int i = 1; i < this->virtual_grf_next; i++) {
2436 for (int c = 0; c < class_count; c++) {
2437 if (class_sizes[c] == this->virtual_grf_sizes[i]) {
2438 if (aligned_pair_class >= 0 &&
2439 this->delta_x.reg == i) {
2440 ra_set_node_class(g, i, classes[aligned_pair_class]);
2441 } else {
2442 ra_set_node_class(g, i, classes[c]);
2443 }
2444 break;
2445 }
2446 }
2447
2448 for (int j = 1; j < i; j++) {
2449 if (virtual_grf_interferes(i, j)) {
2450 ra_add_node_interference(g, i, j);
2451 }
2452 }
2453 }
2454
2455 /* FINISHME: Handle spilling */
2456 if (!ra_allocate_no_spills(g)) {
2457 fprintf(stderr, "Failed to allocate registers.\n");
2458 this->fail = true;
2459 return;
2460 }
2461
2462 /* Get the chosen virtual registers for each node, and map virtual
2463 * regs in the register classes back down to real hardware reg
2464 * numbers.
2465 */
2466 hw_reg_mapping[0] = 0; /* unused */
2467 for (int i = 1; i < this->virtual_grf_next; i++) {
2468 int reg = ra_get_node_reg(g, i);
2469 int hw_reg = -1;
2470
2471 for (int c = 0; c < class_count; c++) {
2472 if (reg >= class_base_reg[c] &&
2473 reg < class_base_reg[c] + class_reg_count[c]) {
2474 hw_reg = reg - class_base_reg[c];
2475 break;
2476 }
2477 }
2478
2479 assert(hw_reg != -1);
2480 hw_reg_mapping[i] = this->first_non_payload_grf + hw_reg;
2481 last_grf = MAX2(last_grf,
2482 hw_reg_mapping[i] + this->virtual_grf_sizes[i] - 1);
2483 }
2484
2485 foreach_iter(exec_list_iterator, iter, this->instructions) {
2486 fs_inst *inst = (fs_inst *)iter.get();
2487
2488 assign_reg(hw_reg_mapping, &inst->dst);
2489 assign_reg(hw_reg_mapping, &inst->src[0]);
2490 assign_reg(hw_reg_mapping, &inst->src[1]);
2491 }
2492
2493 this->grf_used = last_grf + 1;
2494
2495 talloc_free(g);
2496 talloc_free(regs);
2497 }
2498
2499 void
2500 fs_visitor::calculate_live_intervals()
2501 {
2502 int num_vars = this->virtual_grf_next;
2503 int *def = talloc_array(mem_ctx, int, num_vars);
2504 int *use = talloc_array(mem_ctx, int, num_vars);
2505 int loop_depth = 0;
2506 int loop_start = 0;
2507
2508 for (int i = 0; i < num_vars; i++) {
2509 def[i] = 1 << 30;
2510 use[i] = -1;
2511 }
2512
2513 int ip = 0;
2514 foreach_iter(exec_list_iterator, iter, this->instructions) {
2515 fs_inst *inst = (fs_inst *)iter.get();
2516
2517 if (inst->opcode == BRW_OPCODE_DO) {
2518 if (loop_depth++ == 0)
2519 loop_start = ip;
2520 } else if (inst->opcode == BRW_OPCODE_WHILE) {
2521 loop_depth--;
2522
2523 if (loop_depth == 0) {
2524 /* FINISHME:
2525 *
2526 * Patches up any vars marked for use within the loop as
2527 * live until the end. This is conservative, as there
2528 * will often be variables defined and used inside the
2529 * loop but dead at the end of the loop body.
2530 */
2531 for (int i = 0; i < num_vars; i++) {
2532 if (use[i] == loop_start) {
2533 use[i] = ip;
2534 }
2535 }
2536 }
2537 } else {
2538 int eip = ip;
2539
2540 if (loop_depth)
2541 eip = loop_start;
2542
2543 for (unsigned int i = 0; i < 3; i++) {
2544 if (inst->src[i].file == GRF && inst->src[i].reg != 0) {
2545 use[inst->src[i].reg] = MAX2(use[inst->src[i].reg], eip);
2546 }
2547 }
2548 if (inst->dst.file == GRF && inst->dst.reg != 0) {
2549 def[inst->dst.reg] = MIN2(def[inst->dst.reg], eip);
2550 }
2551 }
2552
2553 ip++;
2554 }
2555
2556 talloc_free(this->virtual_grf_def);
2557 talloc_free(this->virtual_grf_use);
2558 this->virtual_grf_def = def;
2559 this->virtual_grf_use = use;
2560 }
2561
2562 /**
2563 * Attempts to move immediate constants into the immediate
2564 * constant slot of following instructions.
2565 *
2566 * Immediate constants are a bit tricky -- they have to be in the last
2567 * operand slot, you can't do abs/negate on them,
2568 */
2569
2570 bool
2571 fs_visitor::propagate_constants()
2572 {
2573 bool progress = false;
2574
2575 foreach_iter(exec_list_iterator, iter, this->instructions) {
2576 fs_inst *inst = (fs_inst *)iter.get();
2577
2578 if (inst->opcode != BRW_OPCODE_MOV ||
2579 inst->predicated ||
2580 inst->dst.file != GRF || inst->src[0].file != IMM ||
2581 inst->dst.type != inst->src[0].type)
2582 continue;
2583
2584 /* Don't bother with cases where we should have had the
2585 * operation on the constant folded in GLSL already.
2586 */
2587 if (inst->saturate)
2588 continue;
2589
2590 /* Found a move of a constant to a GRF. Find anything else using the GRF
2591 * before it's written, and replace it with the constant if we can.
2592 */
2593 exec_list_iterator scan_iter = iter;
2594 scan_iter.next();
2595 for (; scan_iter.has_next(); scan_iter.next()) {
2596 fs_inst *scan_inst = (fs_inst *)scan_iter.get();
2597
2598 if (scan_inst->opcode == BRW_OPCODE_DO ||
2599 scan_inst->opcode == BRW_OPCODE_WHILE ||
2600 scan_inst->opcode == BRW_OPCODE_ELSE ||
2601 scan_inst->opcode == BRW_OPCODE_ENDIF) {
2602 break;
2603 }
2604
2605 for (int i = 2; i >= 0; i--) {
2606 if (scan_inst->src[i].file != GRF ||
2607 scan_inst->src[i].reg != inst->dst.reg ||
2608 scan_inst->src[i].reg_offset != inst->dst.reg_offset)
2609 continue;
2610
2611 /* Don't bother with cases where we should have had the
2612 * operation on the constant folded in GLSL already.
2613 */
2614 if (scan_inst->src[i].negate || scan_inst->src[i].abs)
2615 continue;
2616
2617 switch (scan_inst->opcode) {
2618 case BRW_OPCODE_MOV:
2619 scan_inst->src[i] = inst->src[0];
2620 progress = true;
2621 break;
2622
2623 case BRW_OPCODE_MUL:
2624 case BRW_OPCODE_ADD:
2625 if (i == 1) {
2626 scan_inst->src[i] = inst->src[0];
2627 progress = true;
2628 } else if (i == 0 && scan_inst->src[1].file != IMM) {
2629 /* Fit this constant in by commuting the operands */
2630 scan_inst->src[0] = scan_inst->src[1];
2631 scan_inst->src[1] = inst->src[0];
2632 }
2633 break;
2634 case BRW_OPCODE_CMP:
2635 if (i == 1) {
2636 scan_inst->src[i] = inst->src[0];
2637 progress = true;
2638 }
2639 }
2640 }
2641
2642 if (scan_inst->dst.file == GRF &&
2643 scan_inst->dst.reg == inst->dst.reg &&
2644 (scan_inst->dst.reg_offset == inst->dst.reg_offset ||
2645 scan_inst->opcode == FS_OPCODE_TEX)) {
2646 break;
2647 }
2648 }
2649 }
2650
2651 return progress;
2652 }
2653 /**
2654 * Must be called after calculate_live_intervales() to remove unused
2655 * writes to registers -- register allocation will fail otherwise
2656 * because something deffed but not used won't be considered to
2657 * interfere with other regs.
2658 */
2659 bool
2660 fs_visitor::dead_code_eliminate()
2661 {
2662 bool progress = false;
2663 int num_vars = this->virtual_grf_next;
2664 bool dead[num_vars];
2665
2666 for (int i = 0; i < num_vars; i++) {
2667 /* This would be ">=", but FS_OPCODE_DISCARD has a src == dst where
2668 * it writes dst then reads it as src.
2669 */
2670 dead[i] = this->virtual_grf_def[i] > this->virtual_grf_use[i];
2671
2672 if (dead[i]) {
2673 /* Mark off its interval so it won't interfere with anything. */
2674 this->virtual_grf_def[i] = -1;
2675 this->virtual_grf_use[i] = -1;
2676 }
2677 }
2678
2679 foreach_iter(exec_list_iterator, iter, this->instructions) {
2680 fs_inst *inst = (fs_inst *)iter.get();
2681
2682 if (inst->dst.file == GRF && dead[inst->dst.reg]) {
2683 inst->remove();
2684 progress = true;
2685 }
2686 }
2687
2688 return progress;
2689 }
2690
2691 bool
2692 fs_visitor::virtual_grf_interferes(int a, int b)
2693 {
2694 int start = MAX2(this->virtual_grf_def[a], this->virtual_grf_def[b]);
2695 int end = MIN2(this->virtual_grf_use[a], this->virtual_grf_use[b]);
2696
2697 /* For dead code, just check if the def interferes with the other range. */
2698 if (this->virtual_grf_use[a] == -1) {
2699 return (this->virtual_grf_def[a] >= this->virtual_grf_def[b] &&
2700 this->virtual_grf_def[a] < this->virtual_grf_use[b]);
2701 }
2702 if (this->virtual_grf_use[b] == -1) {
2703 return (this->virtual_grf_def[b] >= this->virtual_grf_def[a] &&
2704 this->virtual_grf_def[b] < this->virtual_grf_use[a]);
2705 }
2706
2707 return start <= end;
2708 }
2709
2710 static struct brw_reg brw_reg_from_fs_reg(fs_reg *reg)
2711 {
2712 struct brw_reg brw_reg;
2713
2714 switch (reg->file) {
2715 case GRF:
2716 case ARF:
2717 case MRF:
2718 brw_reg = brw_vec8_reg(reg->file,
2719 reg->hw_reg, 0);
2720 brw_reg = retype(brw_reg, reg->type);
2721 break;
2722 case IMM:
2723 switch (reg->type) {
2724 case BRW_REGISTER_TYPE_F:
2725 brw_reg = brw_imm_f(reg->imm.f);
2726 break;
2727 case BRW_REGISTER_TYPE_D:
2728 brw_reg = brw_imm_d(reg->imm.i);
2729 break;
2730 case BRW_REGISTER_TYPE_UD:
2731 brw_reg = brw_imm_ud(reg->imm.u);
2732 break;
2733 default:
2734 assert(!"not reached");
2735 break;
2736 }
2737 break;
2738 case FIXED_HW_REG:
2739 brw_reg = reg->fixed_hw_reg;
2740 break;
2741 case BAD_FILE:
2742 /* Probably unused. */
2743 brw_reg = brw_null_reg();
2744 break;
2745 case UNIFORM:
2746 assert(!"not reached");
2747 brw_reg = brw_null_reg();
2748 break;
2749 }
2750 if (reg->abs)
2751 brw_reg = brw_abs(brw_reg);
2752 if (reg->negate)
2753 brw_reg = negate(brw_reg);
2754
2755 return brw_reg;
2756 }
2757
2758 void
2759 fs_visitor::generate_code()
2760 {
2761 unsigned int annotation_len = 0;
2762 int last_native_inst = 0;
2763 struct brw_instruction *if_stack[16], *loop_stack[16];
2764 int if_stack_depth = 0, loop_stack_depth = 0;
2765 int if_depth_in_loop[16];
2766
2767 if_depth_in_loop[loop_stack_depth] = 0;
2768
2769 memset(&if_stack, 0, sizeof(if_stack));
2770 foreach_iter(exec_list_iterator, iter, this->instructions) {
2771 fs_inst *inst = (fs_inst *)iter.get();
2772 struct brw_reg src[3], dst;
2773
2774 for (unsigned int i = 0; i < 3; i++) {
2775 src[i] = brw_reg_from_fs_reg(&inst->src[i]);
2776 }
2777 dst = brw_reg_from_fs_reg(&inst->dst);
2778
2779 brw_set_conditionalmod(p, inst->conditional_mod);
2780 brw_set_predicate_control(p, inst->predicated);
2781
2782 switch (inst->opcode) {
2783 case BRW_OPCODE_MOV:
2784 brw_MOV(p, dst, src[0]);
2785 break;
2786 case BRW_OPCODE_ADD:
2787 brw_ADD(p, dst, src[0], src[1]);
2788 break;
2789 case BRW_OPCODE_MUL:
2790 brw_MUL(p, dst, src[0], src[1]);
2791 break;
2792
2793 case BRW_OPCODE_FRC:
2794 brw_FRC(p, dst, src[0]);
2795 break;
2796 case BRW_OPCODE_RNDD:
2797 brw_RNDD(p, dst, src[0]);
2798 break;
2799 case BRW_OPCODE_RNDZ:
2800 brw_RNDZ(p, dst, src[0]);
2801 break;
2802
2803 case BRW_OPCODE_AND:
2804 brw_AND(p, dst, src[0], src[1]);
2805 break;
2806 case BRW_OPCODE_OR:
2807 brw_OR(p, dst, src[0], src[1]);
2808 break;
2809 case BRW_OPCODE_XOR:
2810 brw_XOR(p, dst, src[0], src[1]);
2811 break;
2812
2813 case BRW_OPCODE_CMP:
2814 brw_CMP(p, dst, inst->conditional_mod, src[0], src[1]);
2815 break;
2816 case BRW_OPCODE_SEL:
2817 brw_SEL(p, dst, src[0], src[1]);
2818 break;
2819
2820 case BRW_OPCODE_IF:
2821 assert(if_stack_depth < 16);
2822 if_stack[if_stack_depth] = brw_IF(p, BRW_EXECUTE_8);
2823 if_depth_in_loop[loop_stack_depth]++;
2824 if_stack_depth++;
2825 break;
2826 case BRW_OPCODE_ELSE:
2827 if_stack[if_stack_depth - 1] =
2828 brw_ELSE(p, if_stack[if_stack_depth - 1]);
2829 break;
2830 case BRW_OPCODE_ENDIF:
2831 if_stack_depth--;
2832 brw_ENDIF(p , if_stack[if_stack_depth]);
2833 if_depth_in_loop[loop_stack_depth]--;
2834 break;
2835
2836 case BRW_OPCODE_DO:
2837 loop_stack[loop_stack_depth++] = brw_DO(p, BRW_EXECUTE_8);
2838 if_depth_in_loop[loop_stack_depth] = 0;
2839 break;
2840
2841 case BRW_OPCODE_BREAK:
2842 brw_BREAK(p, if_depth_in_loop[loop_stack_depth]);
2843 brw_set_predicate_control(p, BRW_PREDICATE_NONE);
2844 break;
2845 case BRW_OPCODE_CONTINUE:
2846 brw_CONT(p, if_depth_in_loop[loop_stack_depth]);
2847 brw_set_predicate_control(p, BRW_PREDICATE_NONE);
2848 break;
2849
2850 case BRW_OPCODE_WHILE: {
2851 struct brw_instruction *inst0, *inst1;
2852 GLuint br = 1;
2853
2854 if (intel->gen >= 5)
2855 br = 2;
2856
2857 assert(loop_stack_depth > 0);
2858 loop_stack_depth--;
2859 inst0 = inst1 = brw_WHILE(p, loop_stack[loop_stack_depth]);
2860 /* patch all the BREAK/CONT instructions from last BGNLOOP */
2861 while (inst0 > loop_stack[loop_stack_depth]) {
2862 inst0--;
2863 if (inst0->header.opcode == BRW_OPCODE_BREAK &&
2864 inst0->bits3.if_else.jump_count == 0) {
2865 inst0->bits3.if_else.jump_count = br * (inst1 - inst0 + 1);
2866 }
2867 else if (inst0->header.opcode == BRW_OPCODE_CONTINUE &&
2868 inst0->bits3.if_else.jump_count == 0) {
2869 inst0->bits3.if_else.jump_count = br * (inst1 - inst0);
2870 }
2871 }
2872 }
2873 break;
2874
2875 case FS_OPCODE_RCP:
2876 case FS_OPCODE_RSQ:
2877 case FS_OPCODE_SQRT:
2878 case FS_OPCODE_EXP2:
2879 case FS_OPCODE_LOG2:
2880 case FS_OPCODE_POW:
2881 case FS_OPCODE_SIN:
2882 case FS_OPCODE_COS:
2883 generate_math(inst, dst, src);
2884 break;
2885 case FS_OPCODE_LINTERP:
2886 generate_linterp(inst, dst, src);
2887 break;
2888 case FS_OPCODE_TEX:
2889 case FS_OPCODE_TXB:
2890 case FS_OPCODE_TXL:
2891 generate_tex(inst, dst, src[0]);
2892 break;
2893 case FS_OPCODE_DISCARD:
2894 generate_discard(inst, dst /* src0 == dst */);
2895 break;
2896 case FS_OPCODE_DDX:
2897 generate_ddx(inst, dst, src[0]);
2898 break;
2899 case FS_OPCODE_DDY:
2900 generate_ddy(inst, dst, src[0]);
2901 break;
2902 case FS_OPCODE_FB_WRITE:
2903 generate_fb_write(inst);
2904 break;
2905 default:
2906 if (inst->opcode < (int)ARRAY_SIZE(brw_opcodes)) {
2907 _mesa_problem(ctx, "Unsupported opcode `%s' in FS",
2908 brw_opcodes[inst->opcode].name);
2909 } else {
2910 _mesa_problem(ctx, "Unsupported opcode %d in FS", inst->opcode);
2911 }
2912 this->fail = true;
2913 }
2914
2915 if (annotation_len < p->nr_insn) {
2916 annotation_len *= 2;
2917 if (annotation_len < 16)
2918 annotation_len = 16;
2919
2920 this->annotation_string = talloc_realloc(this->mem_ctx,
2921 annotation_string,
2922 const char *,
2923 annotation_len);
2924 this->annotation_ir = talloc_realloc(this->mem_ctx,
2925 annotation_ir,
2926 ir_instruction *,
2927 annotation_len);
2928 }
2929
2930 for (unsigned int i = last_native_inst; i < p->nr_insn; i++) {
2931 this->annotation_string[i] = inst->annotation;
2932 this->annotation_ir[i] = inst->ir;
2933 }
2934 last_native_inst = p->nr_insn;
2935 }
2936 }
2937
2938 GLboolean
2939 brw_wm_fs_emit(struct brw_context *brw, struct brw_wm_compile *c)
2940 {
2941 struct brw_compile *p = &c->func;
2942 struct intel_context *intel = &brw->intel;
2943 GLcontext *ctx = &intel->ctx;
2944 struct brw_shader *shader = NULL;
2945 struct gl_shader_program *prog = ctx->Shader.CurrentProgram;
2946
2947 if (!prog)
2948 return GL_FALSE;
2949
2950 if (!using_new_fs)
2951 return GL_FALSE;
2952
2953 for (unsigned int i = 0; i < prog->_NumLinkedShaders; i++) {
2954 if (prog->_LinkedShaders[i]->Type == GL_FRAGMENT_SHADER) {
2955 shader = (struct brw_shader *)prog->_LinkedShaders[i];
2956 break;
2957 }
2958 }
2959 if (!shader)
2960 return GL_FALSE;
2961
2962 /* We always use 8-wide mode, at least for now. For one, flow
2963 * control only works in 8-wide. Also, when we're fragment shader
2964 * bound, we're almost always under register pressure as well, so
2965 * 8-wide would save us from the performance cliff of spilling
2966 * regs.
2967 */
2968 c->dispatch_width = 8;
2969
2970 if (INTEL_DEBUG & DEBUG_WM) {
2971 printf("GLSL IR for native fragment shader %d:\n", prog->Name);
2972 _mesa_print_ir(shader->ir, NULL);
2973 printf("\n");
2974 }
2975
2976 /* Now the main event: Visit the shader IR and generate our FS IR for it.
2977 */
2978 fs_visitor v(c, shader);
2979
2980 if (0) {
2981 v.emit_dummy_fs();
2982 } else {
2983 v.calculate_urb_setup();
2984 if (intel->gen < 6)
2985 v.emit_interpolation_setup_gen4();
2986 else
2987 v.emit_interpolation_setup_gen6();
2988
2989 /* Generate FS IR for main(). (the visitor only descends into
2990 * functions called "main").
2991 */
2992 foreach_iter(exec_list_iterator, iter, *shader->ir) {
2993 ir_instruction *ir = (ir_instruction *)iter.get();
2994 v.base_ir = ir;
2995 ir->accept(&v);
2996 }
2997
2998 v.emit_fb_writes();
2999 v.assign_curb_setup();
3000 v.assign_urb_setup();
3001
3002 bool progress;
3003 do {
3004 progress = false;
3005
3006 v.calculate_live_intervals();
3007 progress = v.propagate_constants() || progress;
3008 progress = v.dead_code_eliminate() || progress;
3009 } while (progress);
3010
3011 if (0)
3012 v.assign_regs_trivial();
3013 else
3014 v.assign_regs();
3015 }
3016
3017 if (!v.fail)
3018 v.generate_code();
3019
3020 assert(!v.fail); /* FINISHME: Cleanly fail, tested at link time, etc. */
3021
3022 if (v.fail)
3023 return GL_FALSE;
3024
3025 if (INTEL_DEBUG & DEBUG_WM) {
3026 const char *last_annotation_string = NULL;
3027 ir_instruction *last_annotation_ir = NULL;
3028
3029 printf("Native code for fragment shader %d:\n", prog->Name);
3030 for (unsigned int i = 0; i < p->nr_insn; i++) {
3031 if (last_annotation_ir != v.annotation_ir[i]) {
3032 last_annotation_ir = v.annotation_ir[i];
3033 if (last_annotation_ir) {
3034 printf(" ");
3035 last_annotation_ir->print();
3036 printf("\n");
3037 }
3038 }
3039 if (last_annotation_string != v.annotation_string[i]) {
3040 last_annotation_string = v.annotation_string[i];
3041 if (last_annotation_string)
3042 printf(" %s\n", last_annotation_string);
3043 }
3044 brw_disasm(stdout, &p->store[i], intel->gen);
3045 }
3046 printf("\n");
3047 }
3048
3049 c->prog_data.total_grf = v.grf_used;
3050 c->prog_data.total_scratch = 0;
3051
3052 return GL_TRUE;
3053 }