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