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