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