mesa: add a UniformBooleanTrue option
[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
24 /** @file brw_fs.cpp
25 *
26 * This file drives the GLSL IR -> LIR translation, contains the
27 * optimizations on the LIR, and drives the generation of native code
28 * from the LIR.
29 */
30
31 extern "C" {
32
33 #include <sys/types.h>
34
35 #include "main/macros.h"
36 #include "main/shaderobj.h"
37 #include "main/uniforms.h"
38 #include "program/prog_parameter.h"
39 #include "program/prog_print.h"
40 #include "program/register_allocate.h"
41 #include "program/sampler.h"
42 #include "program/hash_table.h"
43 #include "brw_context.h"
44 #include "brw_eu.h"
45 #include "brw_wm.h"
46 }
47 #include "brw_shader.h"
48 #include "brw_fs.h"
49 #include "glsl/glsl_types.h"
50 #include "glsl/ir_print_visitor.h"
51
52 #define MAX_INSTRUCTION (1 << 30)
53
54 int
55 fs_visitor::type_size(const struct glsl_type *type)
56 {
57 unsigned int size, i;
58
59 switch (type->base_type) {
60 case GLSL_TYPE_UINT:
61 case GLSL_TYPE_INT:
62 case GLSL_TYPE_FLOAT:
63 case GLSL_TYPE_BOOL:
64 return type->components();
65 case GLSL_TYPE_ARRAY:
66 return type_size(type->fields.array) * type->length;
67 case GLSL_TYPE_STRUCT:
68 size = 0;
69 for (i = 0; i < type->length; i++) {
70 size += type_size(type->fields.structure[i].type);
71 }
72 return size;
73 case GLSL_TYPE_SAMPLER:
74 /* Samplers take up no register space, since they're baked in at
75 * link time.
76 */
77 return 0;
78 default:
79 assert(!"not reached");
80 return 0;
81 }
82 }
83
84 void
85 fs_visitor::fail(const char *format, ...)
86 {
87 va_list va;
88 char *msg;
89
90 if (failed)
91 return;
92
93 failed = true;
94
95 va_start(va, format);
96 msg = ralloc_vasprintf(mem_ctx, format, va);
97 va_end(va);
98 msg = ralloc_asprintf(mem_ctx, "FS compile failed: %s\n", msg);
99
100 this->fail_msg = msg;
101
102 if (INTEL_DEBUG & DEBUG_WM) {
103 fprintf(stderr, "%s", msg);
104 }
105 }
106
107 void
108 fs_visitor::push_force_uncompressed()
109 {
110 force_uncompressed_stack++;
111 }
112
113 void
114 fs_visitor::pop_force_uncompressed()
115 {
116 force_uncompressed_stack--;
117 assert(force_uncompressed_stack >= 0);
118 }
119
120 void
121 fs_visitor::push_force_sechalf()
122 {
123 force_sechalf_stack++;
124 }
125
126 void
127 fs_visitor::pop_force_sechalf()
128 {
129 force_sechalf_stack--;
130 assert(force_sechalf_stack >= 0);
131 }
132
133 /**
134 * Returns how many MRFs an FS opcode will write over.
135 *
136 * Note that this is not the 0 or 1 implied writes in an actual gen
137 * instruction -- the FS opcodes often generate MOVs in addition.
138 */
139 int
140 fs_visitor::implied_mrf_writes(fs_inst *inst)
141 {
142 if (inst->mlen == 0)
143 return 0;
144
145 switch (inst->opcode) {
146 case SHADER_OPCODE_RCP:
147 case SHADER_OPCODE_RSQ:
148 case SHADER_OPCODE_SQRT:
149 case SHADER_OPCODE_EXP2:
150 case SHADER_OPCODE_LOG2:
151 case SHADER_OPCODE_SIN:
152 case SHADER_OPCODE_COS:
153 return 1 * c->dispatch_width / 8;
154 case SHADER_OPCODE_POW:
155 return 2 * c->dispatch_width / 8;
156 case FS_OPCODE_TEX:
157 case FS_OPCODE_TXB:
158 case FS_OPCODE_TXD:
159 case FS_OPCODE_TXL:
160 case FS_OPCODE_TXS:
161 return 1;
162 case FS_OPCODE_FB_WRITE:
163 return 2;
164 case FS_OPCODE_PULL_CONSTANT_LOAD:
165 case FS_OPCODE_UNSPILL:
166 return 1;
167 case FS_OPCODE_SPILL:
168 return 2;
169 default:
170 assert(!"not reached");
171 return inst->mlen;
172 }
173 }
174
175 int
176 fs_visitor::virtual_grf_alloc(int size)
177 {
178 if (virtual_grf_array_size <= virtual_grf_next) {
179 if (virtual_grf_array_size == 0)
180 virtual_grf_array_size = 16;
181 else
182 virtual_grf_array_size *= 2;
183 virtual_grf_sizes = reralloc(mem_ctx, virtual_grf_sizes, int,
184 virtual_grf_array_size);
185 }
186 virtual_grf_sizes[virtual_grf_next] = size;
187 return virtual_grf_next++;
188 }
189
190 /** Fixed HW reg constructor. */
191 fs_reg::fs_reg(enum register_file file, int reg)
192 {
193 init();
194 this->file = file;
195 this->reg = reg;
196 this->type = BRW_REGISTER_TYPE_F;
197 }
198
199 /** Fixed HW reg constructor. */
200 fs_reg::fs_reg(enum register_file file, int reg, uint32_t type)
201 {
202 init();
203 this->file = file;
204 this->reg = reg;
205 this->type = type;
206 }
207
208 /** Automatic reg constructor. */
209 fs_reg::fs_reg(class fs_visitor *v, const struct glsl_type *type)
210 {
211 init();
212
213 this->file = GRF;
214 this->reg = v->virtual_grf_alloc(v->type_size(type));
215 this->reg_offset = 0;
216 this->type = brw_type_for_base_type(type);
217 }
218
219 fs_reg *
220 fs_visitor::variable_storage(ir_variable *var)
221 {
222 return (fs_reg *)hash_table_find(this->variable_ht, var);
223 }
224
225 void
226 import_uniforms_callback(const void *key,
227 void *data,
228 void *closure)
229 {
230 struct hash_table *dst_ht = (struct hash_table *)closure;
231 const fs_reg *reg = (const fs_reg *)data;
232
233 if (reg->file != UNIFORM)
234 return;
235
236 hash_table_insert(dst_ht, data, key);
237 }
238
239 /* For 16-wide, we need to follow from the uniform setup of 8-wide dispatch.
240 * This brings in those uniform definitions
241 */
242 void
243 fs_visitor::import_uniforms(fs_visitor *v)
244 {
245 hash_table_call_foreach(v->variable_ht,
246 import_uniforms_callback,
247 variable_ht);
248 this->params_remap = v->params_remap;
249 }
250
251 /* Our support for uniforms is piggy-backed on the struct
252 * gl_fragment_program, because that's where the values actually
253 * get stored, rather than in some global gl_shader_program uniform
254 * store.
255 */
256 int
257 fs_visitor::setup_uniform_values(int loc, const glsl_type *type)
258 {
259 unsigned int offset = 0;
260
261 if (type->is_matrix()) {
262 const glsl_type *column = glsl_type::get_instance(GLSL_TYPE_FLOAT,
263 type->vector_elements,
264 1);
265
266 for (unsigned int i = 0; i < type->matrix_columns; i++) {
267 offset += setup_uniform_values(loc + offset, column);
268 }
269
270 return offset;
271 }
272
273 switch (type->base_type) {
274 case GLSL_TYPE_FLOAT:
275 case GLSL_TYPE_UINT:
276 case GLSL_TYPE_INT:
277 case GLSL_TYPE_BOOL:
278 for (unsigned int i = 0; i < type->vector_elements; i++) {
279 unsigned int param = c->prog_data.nr_params++;
280
281 assert(param < ARRAY_SIZE(c->prog_data.param));
282
283 if (ctx->Const.NativeIntegers) {
284 c->prog_data.param_convert[param] = PARAM_NO_CONVERT;
285 } else {
286 switch (type->base_type) {
287 case GLSL_TYPE_FLOAT:
288 c->prog_data.param_convert[param] = PARAM_NO_CONVERT;
289 break;
290 case GLSL_TYPE_UINT:
291 c->prog_data.param_convert[param] = PARAM_CONVERT_F2U;
292 break;
293 case GLSL_TYPE_INT:
294 c->prog_data.param_convert[param] = PARAM_CONVERT_F2I;
295 break;
296 case GLSL_TYPE_BOOL:
297 c->prog_data.param_convert[param] = PARAM_CONVERT_F2B;
298 break;
299 default:
300 assert(!"not reached");
301 c->prog_data.param_convert[param] = PARAM_NO_CONVERT;
302 break;
303 }
304 }
305 this->param_index[param] = loc;
306 this->param_offset[param] = i;
307 }
308 return 1;
309
310 case GLSL_TYPE_STRUCT:
311 for (unsigned int i = 0; i < type->length; i++) {
312 offset += setup_uniform_values(loc + offset,
313 type->fields.structure[i].type);
314 }
315 return offset;
316
317 case GLSL_TYPE_ARRAY:
318 for (unsigned int i = 0; i < type->length; i++) {
319 offset += setup_uniform_values(loc + offset, type->fields.array);
320 }
321 return offset;
322
323 case GLSL_TYPE_SAMPLER:
324 /* The sampler takes up a slot, but we don't use any values from it. */
325 return 1;
326
327 default:
328 assert(!"not reached");
329 return 0;
330 }
331 }
332
333
334 /* Our support for builtin uniforms is even scarier than non-builtin.
335 * It sits on top of the PROG_STATE_VAR parameters that are
336 * automatically updated from GL context state.
337 */
338 void
339 fs_visitor::setup_builtin_uniform_values(ir_variable *ir)
340 {
341 const ir_state_slot *const slots = ir->state_slots;
342 assert(ir->state_slots != NULL);
343
344 for (unsigned int i = 0; i < ir->num_state_slots; i++) {
345 /* This state reference has already been setup by ir_to_mesa, but we'll
346 * get the same index back here.
347 */
348 int index = _mesa_add_state_reference(this->fp->Base.Parameters,
349 (gl_state_index *)slots[i].tokens);
350
351 /* Add each of the unique swizzles of the element as a parameter.
352 * This'll end up matching the expected layout of the
353 * array/matrix/structure we're trying to fill in.
354 */
355 int last_swiz = -1;
356 for (unsigned int j = 0; j < 4; j++) {
357 int swiz = GET_SWZ(slots[i].swizzle, j);
358 if (swiz == last_swiz)
359 break;
360 last_swiz = swiz;
361
362 c->prog_data.param_convert[c->prog_data.nr_params] =
363 PARAM_NO_CONVERT;
364 this->param_index[c->prog_data.nr_params] = index;
365 this->param_offset[c->prog_data.nr_params] = swiz;
366 c->prog_data.nr_params++;
367 }
368 }
369 }
370
371 fs_reg *
372 fs_visitor::emit_fragcoord_interpolation(ir_variable *ir)
373 {
374 fs_reg *reg = new(this->mem_ctx) fs_reg(this, ir->type);
375 fs_reg wpos = *reg;
376 bool flip = !ir->origin_upper_left ^ c->key.render_to_fbo;
377
378 /* gl_FragCoord.x */
379 if (ir->pixel_center_integer) {
380 emit(BRW_OPCODE_MOV, wpos, this->pixel_x);
381 } else {
382 emit(BRW_OPCODE_ADD, wpos, this->pixel_x, fs_reg(0.5f));
383 }
384 wpos.reg_offset++;
385
386 /* gl_FragCoord.y */
387 if (!flip && ir->pixel_center_integer) {
388 emit(BRW_OPCODE_MOV, wpos, this->pixel_y);
389 } else {
390 fs_reg pixel_y = this->pixel_y;
391 float offset = (ir->pixel_center_integer ? 0.0 : 0.5);
392
393 if (flip) {
394 pixel_y.negate = true;
395 offset += c->key.drawable_height - 1.0;
396 }
397
398 emit(BRW_OPCODE_ADD, wpos, pixel_y, fs_reg(offset));
399 }
400 wpos.reg_offset++;
401
402 /* gl_FragCoord.z */
403 if (intel->gen >= 6) {
404 emit(BRW_OPCODE_MOV, wpos,
405 fs_reg(brw_vec8_grf(c->source_depth_reg, 0)));
406 } else {
407 emit(FS_OPCODE_LINTERP, wpos, this->delta_x, this->delta_y,
408 interp_reg(FRAG_ATTRIB_WPOS, 2));
409 }
410 wpos.reg_offset++;
411
412 /* gl_FragCoord.w: Already set up in emit_interpolation */
413 emit(BRW_OPCODE_MOV, wpos, this->wpos_w);
414
415 return reg;
416 }
417
418 fs_reg *
419 fs_visitor::emit_general_interpolation(ir_variable *ir)
420 {
421 fs_reg *reg = new(this->mem_ctx) fs_reg(this, ir->type);
422 /* Interpolation is always in floating point regs. */
423 reg->type = BRW_REGISTER_TYPE_F;
424 fs_reg attr = *reg;
425
426 unsigned int array_elements;
427 const glsl_type *type;
428
429 if (ir->type->is_array()) {
430 array_elements = ir->type->length;
431 if (array_elements == 0) {
432 fail("dereferenced array '%s' has length 0\n", ir->name);
433 }
434 type = ir->type->fields.array;
435 } else {
436 array_elements = 1;
437 type = ir->type;
438 }
439
440 int location = ir->location;
441 for (unsigned int i = 0; i < array_elements; i++) {
442 for (unsigned int j = 0; j < type->matrix_columns; j++) {
443 if (urb_setup[location] == -1) {
444 /* If there's no incoming setup data for this slot, don't
445 * emit interpolation for it.
446 */
447 attr.reg_offset += type->vector_elements;
448 location++;
449 continue;
450 }
451
452 bool is_gl_Color =
453 location == FRAG_ATTRIB_COL0 || location == FRAG_ATTRIB_COL1;
454
455 if (c->key.flat_shade && is_gl_Color) {
456 /* Constant interpolation (flat shading) case. The SF has
457 * handed us defined values in only the constant offset
458 * field of the setup reg.
459 */
460 for (unsigned int k = 0; k < type->vector_elements; k++) {
461 struct brw_reg interp = interp_reg(location, k);
462 interp = suboffset(interp, 3);
463 emit(FS_OPCODE_CINTERP, attr, fs_reg(interp));
464 attr.reg_offset++;
465 }
466 } else {
467 /* Perspective interpolation case. */
468 for (unsigned int k = 0; k < type->vector_elements; k++) {
469 /* FINISHME: At some point we probably want to push
470 * this farther by giving similar treatment to the
471 * other potentially constant components of the
472 * attribute, as well as making brw_vs_constval.c
473 * handle varyings other than gl_TexCoord.
474 */
475 if (location >= FRAG_ATTRIB_TEX0 &&
476 location <= FRAG_ATTRIB_TEX7 &&
477 k == 3 && !(c->key.proj_attrib_mask & (1 << location))) {
478 emit(BRW_OPCODE_MOV, attr, fs_reg(1.0f));
479 } else {
480 struct brw_reg interp = interp_reg(location, k);
481 emit(FS_OPCODE_LINTERP, attr,
482 this->delta_x, this->delta_y, fs_reg(interp));
483 }
484 attr.reg_offset++;
485 }
486
487 if (intel->gen < 6) {
488 attr.reg_offset -= type->vector_elements;
489 for (unsigned int k = 0; k < type->vector_elements; k++) {
490 emit(BRW_OPCODE_MUL, attr, attr, this->pixel_w);
491 attr.reg_offset++;
492 }
493 }
494 }
495 location++;
496 }
497 }
498
499 return reg;
500 }
501
502 fs_reg *
503 fs_visitor::emit_frontfacing_interpolation(ir_variable *ir)
504 {
505 fs_reg *reg = new(this->mem_ctx) fs_reg(this, ir->type);
506
507 /* The frontfacing comes in as a bit in the thread payload. */
508 if (intel->gen >= 6) {
509 emit(BRW_OPCODE_ASR, *reg,
510 fs_reg(retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_D)),
511 fs_reg(15));
512 emit(BRW_OPCODE_NOT, *reg, *reg);
513 emit(BRW_OPCODE_AND, *reg, *reg, fs_reg(1));
514 } else {
515 struct brw_reg r1_6ud = retype(brw_vec1_grf(1, 6), BRW_REGISTER_TYPE_UD);
516 /* bit 31 is "primitive is back face", so checking < (1 << 31) gives
517 * us front face
518 */
519 fs_inst *inst = emit(BRW_OPCODE_CMP, *reg,
520 fs_reg(r1_6ud),
521 fs_reg(1u << 31));
522 inst->conditional_mod = BRW_CONDITIONAL_L;
523 emit(BRW_OPCODE_AND, *reg, *reg, fs_reg(1u));
524 }
525
526 return reg;
527 }
528
529 fs_inst *
530 fs_visitor::emit_math(enum opcode opcode, fs_reg dst, fs_reg src)
531 {
532 switch (opcode) {
533 case SHADER_OPCODE_RCP:
534 case SHADER_OPCODE_RSQ:
535 case SHADER_OPCODE_SQRT:
536 case SHADER_OPCODE_EXP2:
537 case SHADER_OPCODE_LOG2:
538 case SHADER_OPCODE_SIN:
539 case SHADER_OPCODE_COS:
540 break;
541 default:
542 assert(!"not reached: bad math opcode");
543 return NULL;
544 }
545
546 /* Can't do hstride == 0 args to gen6 math, so expand it out. We
547 * might be able to do better by doing execsize = 1 math and then
548 * expanding that result out, but we would need to be careful with
549 * masking.
550 *
551 * The hardware ignores source modifiers (negate and abs) on math
552 * instructions, so we also move to a temp to set those up.
553 */
554 if (intel->gen >= 6 && (src.file == UNIFORM ||
555 src.abs ||
556 src.negate)) {
557 fs_reg expanded = fs_reg(this, glsl_type::float_type);
558 emit(BRW_OPCODE_MOV, expanded, src);
559 src = expanded;
560 }
561
562 fs_inst *inst = emit(opcode, dst, src);
563
564 if (intel->gen < 6) {
565 inst->base_mrf = 2;
566 inst->mlen = c->dispatch_width / 8;
567 }
568
569 return inst;
570 }
571
572 fs_inst *
573 fs_visitor::emit_math(enum opcode opcode, fs_reg dst, fs_reg src0, fs_reg src1)
574 {
575 int base_mrf = 2;
576 fs_inst *inst;
577
578 assert(opcode == SHADER_OPCODE_POW);
579
580 if (intel->gen >= 6) {
581 /* Can't do hstride == 0 args to gen6 math, so expand it out.
582 *
583 * The hardware ignores source modifiers (negate and abs) on math
584 * instructions, so we also move to a temp to set those up.
585 */
586 if (src0.file == UNIFORM || src0.abs || src0.negate) {
587 fs_reg expanded = fs_reg(this, glsl_type::float_type);
588 emit(BRW_OPCODE_MOV, expanded, src0);
589 src0 = expanded;
590 }
591
592 if (src1.file == UNIFORM || src1.abs || src1.negate) {
593 fs_reg expanded = fs_reg(this, glsl_type::float_type);
594 emit(BRW_OPCODE_MOV, expanded, src1);
595 src1 = expanded;
596 }
597
598 inst = emit(opcode, dst, src0, src1);
599 } else {
600 emit(BRW_OPCODE_MOV, fs_reg(MRF, base_mrf + 1), src1);
601 inst = emit(opcode, dst, src0, reg_null_f);
602
603 inst->base_mrf = base_mrf;
604 inst->mlen = 2 * c->dispatch_width / 8;
605 }
606 return inst;
607 }
608
609 /**
610 * To be called after the last _mesa_add_state_reference() call, to
611 * set up prog_data.param[] for assign_curb_setup() and
612 * setup_pull_constants().
613 */
614 void
615 fs_visitor::setup_paramvalues_refs()
616 {
617 if (c->dispatch_width != 8)
618 return;
619
620 /* Set up the pointers to ParamValues now that that array is finalized. */
621 for (unsigned int i = 0; i < c->prog_data.nr_params; i++) {
622 c->prog_data.param[i] =
623 (const float *)fp->Base.Parameters->ParameterValues[this->param_index[i]] +
624 this->param_offset[i];
625 }
626 }
627
628 void
629 fs_visitor::assign_curb_setup()
630 {
631 c->prog_data.curb_read_length = ALIGN(c->prog_data.nr_params, 8) / 8;
632 if (c->dispatch_width == 8) {
633 c->prog_data.first_curbe_grf = c->nr_payload_regs;
634 } else {
635 c->prog_data.first_curbe_grf_16 = c->nr_payload_regs;
636 }
637
638 /* Map the offsets in the UNIFORM file to fixed HW regs. */
639 foreach_list(node, &this->instructions) {
640 fs_inst *inst = (fs_inst *)node;
641
642 for (unsigned int i = 0; i < 3; i++) {
643 if (inst->src[i].file == UNIFORM) {
644 int constant_nr = inst->src[i].reg + inst->src[i].reg_offset;
645 struct brw_reg brw_reg = brw_vec1_grf(c->nr_payload_regs +
646 constant_nr / 8,
647 constant_nr % 8);
648
649 inst->src[i].file = FIXED_HW_REG;
650 inst->src[i].fixed_hw_reg = retype(brw_reg, inst->src[i].type);
651 }
652 }
653 }
654 }
655
656 void
657 fs_visitor::calculate_urb_setup()
658 {
659 for (unsigned int i = 0; i < FRAG_ATTRIB_MAX; i++) {
660 urb_setup[i] = -1;
661 }
662
663 int urb_next = 0;
664 /* Figure out where each of the incoming setup attributes lands. */
665 if (intel->gen >= 6) {
666 for (unsigned int i = 0; i < FRAG_ATTRIB_MAX; i++) {
667 if (fp->Base.InputsRead & BITFIELD64_BIT(i)) {
668 urb_setup[i] = urb_next++;
669 }
670 }
671 } else {
672 /* FINISHME: The sf doesn't map VS->FS inputs for us very well. */
673 for (unsigned int i = 0; i < VERT_RESULT_MAX; i++) {
674 if (c->key.vp_outputs_written & BITFIELD64_BIT(i)) {
675 int fp_index;
676
677 if (i >= VERT_RESULT_VAR0)
678 fp_index = i - (VERT_RESULT_VAR0 - FRAG_ATTRIB_VAR0);
679 else if (i <= VERT_RESULT_TEX7)
680 fp_index = i;
681 else
682 fp_index = -1;
683
684 if (fp_index >= 0)
685 urb_setup[fp_index] = urb_next++;
686 }
687 }
688 }
689
690 /* Each attribute is 4 setup channels, each of which is half a reg. */
691 c->prog_data.urb_read_length = urb_next * 2;
692 }
693
694 void
695 fs_visitor::assign_urb_setup()
696 {
697 int urb_start = c->nr_payload_regs + c->prog_data.curb_read_length;
698
699 /* Offset all the urb_setup[] index by the actual position of the
700 * setup regs, now that the location of the constants has been chosen.
701 */
702 foreach_list(node, &this->instructions) {
703 fs_inst *inst = (fs_inst *)node;
704
705 if (inst->opcode == FS_OPCODE_LINTERP) {
706 assert(inst->src[2].file == FIXED_HW_REG);
707 inst->src[2].fixed_hw_reg.nr += urb_start;
708 }
709
710 if (inst->opcode == FS_OPCODE_CINTERP) {
711 assert(inst->src[0].file == FIXED_HW_REG);
712 inst->src[0].fixed_hw_reg.nr += urb_start;
713 }
714 }
715
716 this->first_non_payload_grf = urb_start + c->prog_data.urb_read_length;
717 }
718
719 /**
720 * Split large virtual GRFs into separate components if we can.
721 *
722 * This is mostly duplicated with what brw_fs_vector_splitting does,
723 * but that's really conservative because it's afraid of doing
724 * splitting that doesn't result in real progress after the rest of
725 * the optimization phases, which would cause infinite looping in
726 * optimization. We can do it once here, safely. This also has the
727 * opportunity to split interpolated values, or maybe even uniforms,
728 * which we don't have at the IR level.
729 *
730 * We want to split, because virtual GRFs are what we register
731 * allocate and spill (due to contiguousness requirements for some
732 * instructions), and they're what we naturally generate in the
733 * codegen process, but most virtual GRFs don't actually need to be
734 * contiguous sets of GRFs. If we split, we'll end up with reduced
735 * live intervals and better dead code elimination and coalescing.
736 */
737 void
738 fs_visitor::split_virtual_grfs()
739 {
740 int num_vars = this->virtual_grf_next;
741 bool split_grf[num_vars];
742 int new_virtual_grf[num_vars];
743
744 /* Try to split anything > 0 sized. */
745 for (int i = 0; i < num_vars; i++) {
746 if (this->virtual_grf_sizes[i] != 1)
747 split_grf[i] = true;
748 else
749 split_grf[i] = false;
750 }
751
752 if (brw->has_pln) {
753 /* PLN opcodes rely on the delta_xy being contiguous. */
754 split_grf[this->delta_x.reg] = false;
755 }
756
757 foreach_list(node, &this->instructions) {
758 fs_inst *inst = (fs_inst *)node;
759
760 /* Texturing produces 4 contiguous registers, so no splitting. */
761 if (inst->is_tex()) {
762 split_grf[inst->dst.reg] = false;
763 }
764 }
765
766 /* Allocate new space for split regs. Note that the virtual
767 * numbers will be contiguous.
768 */
769 for (int i = 0; i < num_vars; i++) {
770 if (split_grf[i]) {
771 new_virtual_grf[i] = virtual_grf_alloc(1);
772 for (int j = 2; j < this->virtual_grf_sizes[i]; j++) {
773 int reg = virtual_grf_alloc(1);
774 assert(reg == new_virtual_grf[i] + j - 1);
775 (void) reg;
776 }
777 this->virtual_grf_sizes[i] = 1;
778 }
779 }
780
781 foreach_list(node, &this->instructions) {
782 fs_inst *inst = (fs_inst *)node;
783
784 if (inst->dst.file == GRF &&
785 split_grf[inst->dst.reg] &&
786 inst->dst.reg_offset != 0) {
787 inst->dst.reg = (new_virtual_grf[inst->dst.reg] +
788 inst->dst.reg_offset - 1);
789 inst->dst.reg_offset = 0;
790 }
791 for (int i = 0; i < 3; i++) {
792 if (inst->src[i].file == GRF &&
793 split_grf[inst->src[i].reg] &&
794 inst->src[i].reg_offset != 0) {
795 inst->src[i].reg = (new_virtual_grf[inst->src[i].reg] +
796 inst->src[i].reg_offset - 1);
797 inst->src[i].reg_offset = 0;
798 }
799 }
800 }
801 this->live_intervals_valid = false;
802 }
803
804 bool
805 fs_visitor::remove_dead_constants()
806 {
807 if (c->dispatch_width == 8) {
808 this->params_remap = ralloc_array(mem_ctx, int, c->prog_data.nr_params);
809
810 for (unsigned int i = 0; i < c->prog_data.nr_params; i++)
811 this->params_remap[i] = -1;
812
813 /* Find which params are still in use. */
814 foreach_list(node, &this->instructions) {
815 fs_inst *inst = (fs_inst *)node;
816
817 for (int i = 0; i < 3; i++) {
818 int constant_nr = inst->src[i].reg + inst->src[i].reg_offset;
819
820 if (inst->src[i].file != UNIFORM)
821 continue;
822
823 assert(constant_nr < (int)c->prog_data.nr_params);
824
825 /* For now, set this to non-negative. We'll give it the
826 * actual new number in a moment, in order to keep the
827 * register numbers nicely ordered.
828 */
829 this->params_remap[constant_nr] = 0;
830 }
831 }
832
833 /* Figure out what the new numbers for the params will be. At some
834 * point when we're doing uniform array access, we're going to want
835 * to keep the distinction between .reg and .reg_offset, but for
836 * now we don't care.
837 */
838 unsigned int new_nr_params = 0;
839 for (unsigned int i = 0; i < c->prog_data.nr_params; i++) {
840 if (this->params_remap[i] != -1) {
841 this->params_remap[i] = new_nr_params++;
842 }
843 }
844
845 /* Update the list of params to be uploaded to match our new numbering. */
846 for (unsigned int i = 0; i < c->prog_data.nr_params; i++) {
847 int remapped = this->params_remap[i];
848
849 if (remapped == -1)
850 continue;
851
852 /* We've already done setup_paramvalues_refs() so no need to worry
853 * about param_index and param_offset.
854 */
855 c->prog_data.param[remapped] = c->prog_data.param[i];
856 c->prog_data.param_convert[remapped] = c->prog_data.param_convert[i];
857 }
858
859 c->prog_data.nr_params = new_nr_params;
860 } else {
861 /* This should have been generated in the 8-wide pass already. */
862 assert(this->params_remap);
863 }
864
865 /* Now do the renumbering of the shader to remove unused params. */
866 foreach_list(node, &this->instructions) {
867 fs_inst *inst = (fs_inst *)node;
868
869 for (int i = 0; i < 3; i++) {
870 int constant_nr = inst->src[i].reg + inst->src[i].reg_offset;
871
872 if (inst->src[i].file != UNIFORM)
873 continue;
874
875 assert(this->params_remap[constant_nr] != -1);
876 inst->src[i].reg = this->params_remap[constant_nr];
877 inst->src[i].reg_offset = 0;
878 }
879 }
880
881 return true;
882 }
883
884 /**
885 * Choose accesses from the UNIFORM file to demote to using the pull
886 * constant buffer.
887 *
888 * We allow a fragment shader to have more than the specified minimum
889 * maximum number of fragment shader uniform components (64). If
890 * there are too many of these, they'd fill up all of register space.
891 * So, this will push some of them out to the pull constant buffer and
892 * update the program to load them.
893 */
894 void
895 fs_visitor::setup_pull_constants()
896 {
897 /* Only allow 16 registers (128 uniform components) as push constants. */
898 unsigned int max_uniform_components = 16 * 8;
899 if (c->prog_data.nr_params <= max_uniform_components)
900 return;
901
902 if (c->dispatch_width == 16) {
903 fail("Pull constants not supported in 16-wide\n");
904 return;
905 }
906
907 /* Just demote the end of the list. We could probably do better
908 * here, demoting things that are rarely used in the program first.
909 */
910 int pull_uniform_base = max_uniform_components;
911 int pull_uniform_count = c->prog_data.nr_params - pull_uniform_base;
912
913 foreach_list(node, &this->instructions) {
914 fs_inst *inst = (fs_inst *)node;
915
916 for (int i = 0; i < 3; i++) {
917 if (inst->src[i].file != UNIFORM)
918 continue;
919
920 int uniform_nr = inst->src[i].reg + inst->src[i].reg_offset;
921 if (uniform_nr < pull_uniform_base)
922 continue;
923
924 fs_reg dst = fs_reg(this, glsl_type::float_type);
925 fs_inst *pull = new(mem_ctx) fs_inst(FS_OPCODE_PULL_CONSTANT_LOAD,
926 dst);
927 pull->offset = ((uniform_nr - pull_uniform_base) * 4) & ~15;
928 pull->ir = inst->ir;
929 pull->annotation = inst->annotation;
930 pull->base_mrf = 14;
931 pull->mlen = 1;
932
933 inst->insert_before(pull);
934
935 inst->src[i].file = GRF;
936 inst->src[i].reg = dst.reg;
937 inst->src[i].reg_offset = 0;
938 inst->src[i].smear = (uniform_nr - pull_uniform_base) & 3;
939 }
940 }
941
942 for (int i = 0; i < pull_uniform_count; i++) {
943 c->prog_data.pull_param[i] = c->prog_data.param[pull_uniform_base + i];
944 c->prog_data.pull_param_convert[i] =
945 c->prog_data.param_convert[pull_uniform_base + i];
946 }
947 c->prog_data.nr_params -= pull_uniform_count;
948 c->prog_data.nr_pull_params = pull_uniform_count;
949 }
950
951 void
952 fs_visitor::calculate_live_intervals()
953 {
954 int num_vars = this->virtual_grf_next;
955 int *def = ralloc_array(mem_ctx, int, num_vars);
956 int *use = ralloc_array(mem_ctx, int, num_vars);
957 int loop_depth = 0;
958 int loop_start = 0;
959
960 if (this->live_intervals_valid)
961 return;
962
963 for (int i = 0; i < num_vars; i++) {
964 def[i] = MAX_INSTRUCTION;
965 use[i] = -1;
966 }
967
968 int ip = 0;
969 foreach_list(node, &this->instructions) {
970 fs_inst *inst = (fs_inst *)node;
971
972 if (inst->opcode == BRW_OPCODE_DO) {
973 if (loop_depth++ == 0)
974 loop_start = ip;
975 } else if (inst->opcode == BRW_OPCODE_WHILE) {
976 loop_depth--;
977
978 if (loop_depth == 0) {
979 /* Patches up the use of vars marked for being live across
980 * the whole loop.
981 */
982 for (int i = 0; i < num_vars; i++) {
983 if (use[i] == loop_start) {
984 use[i] = ip;
985 }
986 }
987 }
988 } else {
989 for (unsigned int i = 0; i < 3; i++) {
990 if (inst->src[i].file == GRF) {
991 int reg = inst->src[i].reg;
992
993 if (!loop_depth) {
994 use[reg] = ip;
995 } else {
996 def[reg] = MIN2(loop_start, def[reg]);
997 use[reg] = loop_start;
998
999 /* Nobody else is going to go smash our start to
1000 * later in the loop now, because def[reg] now
1001 * points before the bb header.
1002 */
1003 }
1004 }
1005 }
1006 if (inst->dst.file == GRF) {
1007 int reg = inst->dst.reg;
1008
1009 if (!loop_depth) {
1010 def[reg] = MIN2(def[reg], ip);
1011 } else {
1012 def[reg] = MIN2(def[reg], loop_start);
1013 }
1014 }
1015 }
1016
1017 ip++;
1018 }
1019
1020 ralloc_free(this->virtual_grf_def);
1021 ralloc_free(this->virtual_grf_use);
1022 this->virtual_grf_def = def;
1023 this->virtual_grf_use = use;
1024
1025 this->live_intervals_valid = true;
1026 }
1027
1028 /**
1029 * Attempts to move immediate constants into the immediate
1030 * constant slot of following instructions.
1031 *
1032 * Immediate constants are a bit tricky -- they have to be in the last
1033 * operand slot, you can't do abs/negate on them,
1034 */
1035
1036 bool
1037 fs_visitor::propagate_constants()
1038 {
1039 bool progress = false;
1040
1041 calculate_live_intervals();
1042
1043 foreach_list(node, &this->instructions) {
1044 fs_inst *inst = (fs_inst *)node;
1045
1046 if (inst->opcode != BRW_OPCODE_MOV ||
1047 inst->predicated ||
1048 inst->dst.file != GRF || inst->src[0].file != IMM ||
1049 inst->dst.type != inst->src[0].type ||
1050 (c->dispatch_width == 16 &&
1051 (inst->force_uncompressed || inst->force_sechalf)))
1052 continue;
1053
1054 /* Don't bother with cases where we should have had the
1055 * operation on the constant folded in GLSL already.
1056 */
1057 if (inst->saturate)
1058 continue;
1059
1060 /* Found a move of a constant to a GRF. Find anything else using the GRF
1061 * before it's written, and replace it with the constant if we can.
1062 */
1063 for (fs_inst *scan_inst = (fs_inst *)inst->next;
1064 !scan_inst->is_tail_sentinel();
1065 scan_inst = (fs_inst *)scan_inst->next) {
1066 if (scan_inst->opcode == BRW_OPCODE_DO ||
1067 scan_inst->opcode == BRW_OPCODE_WHILE ||
1068 scan_inst->opcode == BRW_OPCODE_ELSE ||
1069 scan_inst->opcode == BRW_OPCODE_ENDIF) {
1070 break;
1071 }
1072
1073 for (int i = 2; i >= 0; i--) {
1074 if (scan_inst->src[i].file != GRF ||
1075 scan_inst->src[i].reg != inst->dst.reg ||
1076 scan_inst->src[i].reg_offset != inst->dst.reg_offset)
1077 continue;
1078
1079 /* Don't bother with cases where we should have had the
1080 * operation on the constant folded in GLSL already.
1081 */
1082 if (scan_inst->src[i].negate || scan_inst->src[i].abs)
1083 continue;
1084
1085 switch (scan_inst->opcode) {
1086 case BRW_OPCODE_MOV:
1087 scan_inst->src[i] = inst->src[0];
1088 progress = true;
1089 break;
1090
1091 case BRW_OPCODE_MUL:
1092 case BRW_OPCODE_ADD:
1093 if (i == 1) {
1094 scan_inst->src[i] = inst->src[0];
1095 progress = true;
1096 } else if (i == 0 && scan_inst->src[1].file != IMM) {
1097 /* Fit this constant in by commuting the operands */
1098 scan_inst->src[0] = scan_inst->src[1];
1099 scan_inst->src[1] = inst->src[0];
1100 progress = true;
1101 }
1102 break;
1103
1104 case BRW_OPCODE_CMP:
1105 if (i == 1) {
1106 scan_inst->src[i] = inst->src[0];
1107 progress = true;
1108 } else if (i == 0 && scan_inst->src[1].file != IMM) {
1109 uint32_t new_cmod;
1110
1111 new_cmod = brw_swap_cmod(scan_inst->conditional_mod);
1112 if (new_cmod != ~0u) {
1113 /* Fit this constant in by swapping the operands and
1114 * flipping the test
1115 */
1116 scan_inst->src[0] = scan_inst->src[1];
1117 scan_inst->src[1] = inst->src[0];
1118 scan_inst->conditional_mod = new_cmod;
1119 progress = true;
1120 }
1121 }
1122 break;
1123
1124 case BRW_OPCODE_SEL:
1125 if (i == 1) {
1126 scan_inst->src[i] = inst->src[0];
1127 progress = true;
1128 } else if (i == 0 && scan_inst->src[1].file != IMM) {
1129 scan_inst->src[0] = scan_inst->src[1];
1130 scan_inst->src[1] = inst->src[0];
1131
1132 /* If this was predicated, flipping operands means
1133 * we also need to flip the predicate.
1134 */
1135 if (scan_inst->conditional_mod == BRW_CONDITIONAL_NONE) {
1136 scan_inst->predicate_inverse =
1137 !scan_inst->predicate_inverse;
1138 }
1139 progress = true;
1140 }
1141 break;
1142
1143 case SHADER_OPCODE_RCP:
1144 /* The hardware doesn't do math on immediate values
1145 * (because why are you doing that, seriously?), but
1146 * the correct answer is to just constant fold it
1147 * anyway.
1148 */
1149 assert(i == 0);
1150 if (inst->src[0].imm.f != 0.0f) {
1151 scan_inst->opcode = BRW_OPCODE_MOV;
1152 scan_inst->src[0] = inst->src[0];
1153 scan_inst->src[0].imm.f = 1.0f / scan_inst->src[0].imm.f;
1154 progress = true;
1155 }
1156 break;
1157
1158 default:
1159 break;
1160 }
1161 }
1162
1163 if (scan_inst->dst.file == GRF &&
1164 scan_inst->dst.reg == inst->dst.reg &&
1165 (scan_inst->dst.reg_offset == inst->dst.reg_offset ||
1166 scan_inst->is_tex())) {
1167 break;
1168 }
1169 }
1170 }
1171
1172 if (progress)
1173 this->live_intervals_valid = false;
1174
1175 return progress;
1176 }
1177
1178
1179 /**
1180 * Attempts to move immediate constants into the immediate
1181 * constant slot of following instructions.
1182 *
1183 * Immediate constants are a bit tricky -- they have to be in the last
1184 * operand slot, you can't do abs/negate on them,
1185 */
1186
1187 bool
1188 fs_visitor::opt_algebraic()
1189 {
1190 bool progress = false;
1191
1192 calculate_live_intervals();
1193
1194 foreach_list(node, &this->instructions) {
1195 fs_inst *inst = (fs_inst *)node;
1196
1197 switch (inst->opcode) {
1198 case BRW_OPCODE_MUL:
1199 if (inst->src[1].file != IMM)
1200 continue;
1201
1202 /* a * 1.0 = a */
1203 if (inst->src[1].type == BRW_REGISTER_TYPE_F &&
1204 inst->src[1].imm.f == 1.0) {
1205 inst->opcode = BRW_OPCODE_MOV;
1206 inst->src[1] = reg_undef;
1207 progress = true;
1208 break;
1209 }
1210
1211 break;
1212 default:
1213 break;
1214 }
1215 }
1216
1217 return progress;
1218 }
1219
1220 /**
1221 * Must be called after calculate_live_intervales() to remove unused
1222 * writes to registers -- register allocation will fail otherwise
1223 * because something deffed but not used won't be considered to
1224 * interfere with other regs.
1225 */
1226 bool
1227 fs_visitor::dead_code_eliminate()
1228 {
1229 bool progress = false;
1230 int pc = 0;
1231
1232 calculate_live_intervals();
1233
1234 foreach_list_safe(node, &this->instructions) {
1235 fs_inst *inst = (fs_inst *)node;
1236
1237 if (inst->dst.file == GRF && this->virtual_grf_use[inst->dst.reg] <= pc) {
1238 inst->remove();
1239 progress = true;
1240 }
1241
1242 pc++;
1243 }
1244
1245 if (progress)
1246 live_intervals_valid = false;
1247
1248 return progress;
1249 }
1250
1251 bool
1252 fs_visitor::register_coalesce()
1253 {
1254 bool progress = false;
1255 int if_depth = 0;
1256 int loop_depth = 0;
1257
1258 foreach_list_safe(node, &this->instructions) {
1259 fs_inst *inst = (fs_inst *)node;
1260
1261 /* Make sure that we dominate the instructions we're going to
1262 * scan for interfering with our coalescing, or we won't have
1263 * scanned enough to see if anything interferes with our
1264 * coalescing. We don't dominate the following instructions if
1265 * we're in a loop or an if block.
1266 */
1267 switch (inst->opcode) {
1268 case BRW_OPCODE_DO:
1269 loop_depth++;
1270 break;
1271 case BRW_OPCODE_WHILE:
1272 loop_depth--;
1273 break;
1274 case BRW_OPCODE_IF:
1275 if_depth++;
1276 break;
1277 case BRW_OPCODE_ENDIF:
1278 if_depth--;
1279 break;
1280 default:
1281 break;
1282 }
1283 if (loop_depth || if_depth)
1284 continue;
1285
1286 if (inst->opcode != BRW_OPCODE_MOV ||
1287 inst->predicated ||
1288 inst->saturate ||
1289 inst->dst.file != GRF || (inst->src[0].file != GRF &&
1290 inst->src[0].file != UNIFORM)||
1291 inst->dst.type != inst->src[0].type)
1292 continue;
1293
1294 bool has_source_modifiers = inst->src[0].abs || inst->src[0].negate;
1295
1296 /* Found a move of a GRF to a GRF. Let's see if we can coalesce
1297 * them: check for no writes to either one until the exit of the
1298 * program.
1299 */
1300 bool interfered = false;
1301
1302 for (fs_inst *scan_inst = (fs_inst *)inst->next;
1303 !scan_inst->is_tail_sentinel();
1304 scan_inst = (fs_inst *)scan_inst->next) {
1305 if (scan_inst->dst.file == GRF) {
1306 if (scan_inst->dst.reg == inst->dst.reg &&
1307 (scan_inst->dst.reg_offset == inst->dst.reg_offset ||
1308 scan_inst->is_tex())) {
1309 interfered = true;
1310 break;
1311 }
1312 if (inst->src[0].file == GRF &&
1313 scan_inst->dst.reg == inst->src[0].reg &&
1314 (scan_inst->dst.reg_offset == inst->src[0].reg_offset ||
1315 scan_inst->is_tex())) {
1316 interfered = true;
1317 break;
1318 }
1319 }
1320
1321 /* The gen6 MATH instruction can't handle source modifiers or
1322 * unusual register regions, so avoid coalescing those for
1323 * now. We should do something more specific.
1324 */
1325 if (intel->gen >= 6 &&
1326 scan_inst->is_math() &&
1327 (has_source_modifiers || inst->src[0].file == UNIFORM)) {
1328 interfered = true;
1329 break;
1330 }
1331 }
1332 if (interfered) {
1333 continue;
1334 }
1335
1336 /* Rewrite the later usage to point at the source of the move to
1337 * be removed.
1338 */
1339 for (fs_inst *scan_inst = inst;
1340 !scan_inst->is_tail_sentinel();
1341 scan_inst = (fs_inst *)scan_inst->next) {
1342 for (int i = 0; i < 3; i++) {
1343 if (scan_inst->src[i].file == GRF &&
1344 scan_inst->src[i].reg == inst->dst.reg &&
1345 scan_inst->src[i].reg_offset == inst->dst.reg_offset) {
1346 fs_reg new_src = inst->src[0];
1347 new_src.negate ^= scan_inst->src[i].negate;
1348 new_src.abs |= scan_inst->src[i].abs;
1349 scan_inst->src[i] = new_src;
1350 }
1351 }
1352 }
1353
1354 inst->remove();
1355 progress = true;
1356 }
1357
1358 if (progress)
1359 live_intervals_valid = false;
1360
1361 return progress;
1362 }
1363
1364
1365 bool
1366 fs_visitor::compute_to_mrf()
1367 {
1368 bool progress = false;
1369 int next_ip = 0;
1370
1371 calculate_live_intervals();
1372
1373 foreach_list_safe(node, &this->instructions) {
1374 fs_inst *inst = (fs_inst *)node;
1375
1376 int ip = next_ip;
1377 next_ip++;
1378
1379 if (inst->opcode != BRW_OPCODE_MOV ||
1380 inst->predicated ||
1381 inst->dst.file != MRF || inst->src[0].file != GRF ||
1382 inst->dst.type != inst->src[0].type ||
1383 inst->src[0].abs || inst->src[0].negate || inst->src[0].smear != -1)
1384 continue;
1385
1386 /* Work out which hardware MRF registers are written by this
1387 * instruction.
1388 */
1389 int mrf_low = inst->dst.reg & ~BRW_MRF_COMPR4;
1390 int mrf_high;
1391 if (inst->dst.reg & BRW_MRF_COMPR4) {
1392 mrf_high = mrf_low + 4;
1393 } else if (c->dispatch_width == 16 &&
1394 (!inst->force_uncompressed && !inst->force_sechalf)) {
1395 mrf_high = mrf_low + 1;
1396 } else {
1397 mrf_high = mrf_low;
1398 }
1399
1400 /* Can't compute-to-MRF this GRF if someone else was going to
1401 * read it later.
1402 */
1403 if (this->virtual_grf_use[inst->src[0].reg] > ip)
1404 continue;
1405
1406 /* Found a move of a GRF to a MRF. Let's see if we can go
1407 * rewrite the thing that made this GRF to write into the MRF.
1408 */
1409 fs_inst *scan_inst;
1410 for (scan_inst = (fs_inst *)inst->prev;
1411 scan_inst->prev != NULL;
1412 scan_inst = (fs_inst *)scan_inst->prev) {
1413 if (scan_inst->dst.file == GRF &&
1414 scan_inst->dst.reg == inst->src[0].reg) {
1415 /* Found the last thing to write our reg we want to turn
1416 * into a compute-to-MRF.
1417 */
1418
1419 if (scan_inst->is_tex()) {
1420 /* texturing writes several continuous regs, so we can't
1421 * compute-to-mrf that.
1422 */
1423 break;
1424 }
1425
1426 /* If it's predicated, it (probably) didn't populate all
1427 * the channels. We might be able to rewrite everything
1428 * that writes that reg, but it would require smarter
1429 * tracking to delay the rewriting until complete success.
1430 */
1431 if (scan_inst->predicated)
1432 break;
1433
1434 /* If it's half of register setup and not the same half as
1435 * our MOV we're trying to remove, bail for now.
1436 */
1437 if (scan_inst->force_uncompressed != inst->force_uncompressed ||
1438 scan_inst->force_sechalf != inst->force_sechalf) {
1439 break;
1440 }
1441
1442 /* SEND instructions can't have MRF as a destination. */
1443 if (scan_inst->mlen)
1444 break;
1445
1446 if (intel->gen >= 6) {
1447 /* gen6 math instructions must have the destination be
1448 * GRF, so no compute-to-MRF for them.
1449 */
1450 if (scan_inst->is_math()) {
1451 break;
1452 }
1453 }
1454
1455 if (scan_inst->dst.reg_offset == inst->src[0].reg_offset) {
1456 /* Found the creator of our MRF's source value. */
1457 scan_inst->dst.file = MRF;
1458 scan_inst->dst.reg = inst->dst.reg;
1459 scan_inst->saturate |= inst->saturate;
1460 inst->remove();
1461 progress = true;
1462 }
1463 break;
1464 }
1465
1466 /* We don't handle flow control here. Most computation of
1467 * values that end up in MRFs are shortly before the MRF
1468 * write anyway.
1469 */
1470 if (scan_inst->opcode == BRW_OPCODE_DO ||
1471 scan_inst->opcode == BRW_OPCODE_WHILE ||
1472 scan_inst->opcode == BRW_OPCODE_ELSE ||
1473 scan_inst->opcode == BRW_OPCODE_ENDIF) {
1474 break;
1475 }
1476
1477 /* You can't read from an MRF, so if someone else reads our
1478 * MRF's source GRF that we wanted to rewrite, that stops us.
1479 */
1480 bool interfered = false;
1481 for (int i = 0; i < 3; i++) {
1482 if (scan_inst->src[i].file == GRF &&
1483 scan_inst->src[i].reg == inst->src[0].reg &&
1484 scan_inst->src[i].reg_offset == inst->src[0].reg_offset) {
1485 interfered = true;
1486 }
1487 }
1488 if (interfered)
1489 break;
1490
1491 if (scan_inst->dst.file == MRF) {
1492 /* If somebody else writes our MRF here, we can't
1493 * compute-to-MRF before that.
1494 */
1495 int scan_mrf_low = scan_inst->dst.reg & ~BRW_MRF_COMPR4;
1496 int scan_mrf_high;
1497
1498 if (scan_inst->dst.reg & BRW_MRF_COMPR4) {
1499 scan_mrf_high = scan_mrf_low + 4;
1500 } else if (c->dispatch_width == 16 &&
1501 (!scan_inst->force_uncompressed &&
1502 !scan_inst->force_sechalf)) {
1503 scan_mrf_high = scan_mrf_low + 1;
1504 } else {
1505 scan_mrf_high = scan_mrf_low;
1506 }
1507
1508 if (mrf_low == scan_mrf_low ||
1509 mrf_low == scan_mrf_high ||
1510 mrf_high == scan_mrf_low ||
1511 mrf_high == scan_mrf_high) {
1512 break;
1513 }
1514 }
1515
1516 if (scan_inst->mlen > 0) {
1517 /* Found a SEND instruction, which means that there are
1518 * live values in MRFs from base_mrf to base_mrf +
1519 * scan_inst->mlen - 1. Don't go pushing our MRF write up
1520 * above it.
1521 */
1522 if (mrf_low >= scan_inst->base_mrf &&
1523 mrf_low < scan_inst->base_mrf + scan_inst->mlen) {
1524 break;
1525 }
1526 if (mrf_high >= scan_inst->base_mrf &&
1527 mrf_high < scan_inst->base_mrf + scan_inst->mlen) {
1528 break;
1529 }
1530 }
1531 }
1532 }
1533
1534 return progress;
1535 }
1536
1537 /**
1538 * Walks through basic blocks, locking for repeated MRF writes and
1539 * removing the later ones.
1540 */
1541 bool
1542 fs_visitor::remove_duplicate_mrf_writes()
1543 {
1544 fs_inst *last_mrf_move[16];
1545 bool progress = false;
1546
1547 /* Need to update the MRF tracking for compressed instructions. */
1548 if (c->dispatch_width == 16)
1549 return false;
1550
1551 memset(last_mrf_move, 0, sizeof(last_mrf_move));
1552
1553 foreach_list_safe(node, &this->instructions) {
1554 fs_inst *inst = (fs_inst *)node;
1555
1556 switch (inst->opcode) {
1557 case BRW_OPCODE_DO:
1558 case BRW_OPCODE_WHILE:
1559 case BRW_OPCODE_IF:
1560 case BRW_OPCODE_ELSE:
1561 case BRW_OPCODE_ENDIF:
1562 memset(last_mrf_move, 0, sizeof(last_mrf_move));
1563 continue;
1564 default:
1565 break;
1566 }
1567
1568 if (inst->opcode == BRW_OPCODE_MOV &&
1569 inst->dst.file == MRF) {
1570 fs_inst *prev_inst = last_mrf_move[inst->dst.reg];
1571 if (prev_inst && inst->equals(prev_inst)) {
1572 inst->remove();
1573 progress = true;
1574 continue;
1575 }
1576 }
1577
1578 /* Clear out the last-write records for MRFs that were overwritten. */
1579 if (inst->dst.file == MRF) {
1580 last_mrf_move[inst->dst.reg] = NULL;
1581 }
1582
1583 if (inst->mlen > 0) {
1584 /* Found a SEND instruction, which will include two or fewer
1585 * implied MRF writes. We could do better here.
1586 */
1587 for (int i = 0; i < implied_mrf_writes(inst); i++) {
1588 last_mrf_move[inst->base_mrf + i] = NULL;
1589 }
1590 }
1591
1592 /* Clear out any MRF move records whose sources got overwritten. */
1593 if (inst->dst.file == GRF) {
1594 for (unsigned int i = 0; i < Elements(last_mrf_move); i++) {
1595 if (last_mrf_move[i] &&
1596 last_mrf_move[i]->src[0].reg == inst->dst.reg) {
1597 last_mrf_move[i] = NULL;
1598 }
1599 }
1600 }
1601
1602 if (inst->opcode == BRW_OPCODE_MOV &&
1603 inst->dst.file == MRF &&
1604 inst->src[0].file == GRF &&
1605 !inst->predicated) {
1606 last_mrf_move[inst->dst.reg] = inst;
1607 }
1608 }
1609
1610 return progress;
1611 }
1612
1613 bool
1614 fs_visitor::virtual_grf_interferes(int a, int b)
1615 {
1616 int start = MAX2(this->virtual_grf_def[a], this->virtual_grf_def[b]);
1617 int end = MIN2(this->virtual_grf_use[a], this->virtual_grf_use[b]);
1618
1619 /* We can't handle dead register writes here, without iterating
1620 * over the whole instruction stream to find every single dead
1621 * write to that register to compare to the live interval of the
1622 * other register. Just assert that dead_code_eliminate() has been
1623 * called.
1624 */
1625 assert((this->virtual_grf_use[a] != -1 ||
1626 this->virtual_grf_def[a] == MAX_INSTRUCTION) &&
1627 (this->virtual_grf_use[b] != -1 ||
1628 this->virtual_grf_def[b] == MAX_INSTRUCTION));
1629
1630 /* If the register is used to store 16 values of less than float
1631 * size (only the case for pixel_[xy]), then we can't allocate
1632 * another dword-sized thing to that register that would be used in
1633 * the same instruction. This is because when the GPU decodes (for
1634 * example):
1635 *
1636 * (declare (in ) vec4 gl_FragCoord@0x97766a0)
1637 * add(16) g6<1>F g6<8,8,1>UW 0.5F { align1 compr };
1638 *
1639 * it's actually processed as:
1640 * add(8) g6<1>F g6<8,8,1>UW 0.5F { align1 };
1641 * add(8) g7<1>F g6.8<8,8,1>UW 0.5F { align1 sechalf };
1642 *
1643 * so our second half values in g6 got overwritten in the first
1644 * half.
1645 */
1646 if (c->dispatch_width == 16 && (this->pixel_x.reg == a ||
1647 this->pixel_x.reg == b ||
1648 this->pixel_y.reg == a ||
1649 this->pixel_y.reg == b)) {
1650 return start <= end;
1651 }
1652
1653 return start < end;
1654 }
1655
1656 bool
1657 fs_visitor::run()
1658 {
1659 uint32_t prog_offset_16 = 0;
1660 uint32_t orig_nr_params = c->prog_data.nr_params;
1661
1662 brw_wm_payload_setup(brw, c);
1663
1664 if (c->dispatch_width == 16) {
1665 /* align to 64 byte boundary. */
1666 while ((c->func.nr_insn * sizeof(struct brw_instruction)) % 64) {
1667 brw_NOP(p);
1668 }
1669
1670 /* Save off the start of this 16-wide program in case we succeed. */
1671 prog_offset_16 = c->func.nr_insn * sizeof(struct brw_instruction);
1672
1673 brw_set_compression_control(p, BRW_COMPRESSION_COMPRESSED);
1674 }
1675
1676 if (0) {
1677 emit_dummy_fs();
1678 } else {
1679 calculate_urb_setup();
1680 if (intel->gen < 6)
1681 emit_interpolation_setup_gen4();
1682 else
1683 emit_interpolation_setup_gen6();
1684
1685 /* Generate FS IR for main(). (the visitor only descends into
1686 * functions called "main").
1687 */
1688 foreach_list(node, &*shader->ir) {
1689 ir_instruction *ir = (ir_instruction *)node;
1690 base_ir = ir;
1691 this->result = reg_undef;
1692 ir->accept(this);
1693 }
1694 if (failed)
1695 return false;
1696
1697 emit_fb_writes();
1698
1699 split_virtual_grfs();
1700
1701 setup_paramvalues_refs();
1702 setup_pull_constants();
1703
1704 bool progress;
1705 do {
1706 progress = false;
1707
1708 progress = remove_duplicate_mrf_writes() || progress;
1709
1710 progress = propagate_constants() || progress;
1711 progress = opt_algebraic() || progress;
1712 progress = register_coalesce() || progress;
1713 progress = compute_to_mrf() || progress;
1714 progress = dead_code_eliminate() || progress;
1715 } while (progress);
1716
1717 remove_dead_constants();
1718
1719 schedule_instructions();
1720
1721 assign_curb_setup();
1722 assign_urb_setup();
1723
1724 if (0) {
1725 /* Debug of register spilling: Go spill everything. */
1726 int virtual_grf_count = virtual_grf_next;
1727 for (int i = 0; i < virtual_grf_count; i++) {
1728 spill_reg(i);
1729 }
1730 }
1731
1732 if (0)
1733 assign_regs_trivial();
1734 else {
1735 while (!assign_regs()) {
1736 if (failed)
1737 break;
1738 }
1739 }
1740 }
1741 assert(force_uncompressed_stack == 0);
1742 assert(force_sechalf_stack == 0);
1743
1744 if (failed)
1745 return false;
1746
1747 generate_code();
1748
1749 if (c->dispatch_width == 8) {
1750 c->prog_data.reg_blocks = brw_register_blocks(grf_used);
1751 } else {
1752 c->prog_data.reg_blocks_16 = brw_register_blocks(grf_used);
1753 c->prog_data.prog_offset_16 = prog_offset_16;
1754
1755 /* Make sure we didn't try to sneak in an extra uniform */
1756 assert(orig_nr_params == c->prog_data.nr_params);
1757 }
1758
1759 return !failed;
1760 }
1761
1762 bool
1763 brw_wm_fs_emit(struct brw_context *brw, struct brw_wm_compile *c,
1764 struct gl_shader_program *prog)
1765 {
1766 struct intel_context *intel = &brw->intel;
1767
1768 if (!prog)
1769 return false;
1770
1771 struct brw_shader *shader =
1772 (brw_shader *) prog->_LinkedShaders[MESA_SHADER_FRAGMENT];
1773 if (!shader)
1774 return false;
1775
1776 if (unlikely(INTEL_DEBUG & DEBUG_WM)) {
1777 printf("GLSL IR for native fragment shader %d:\n", prog->Name);
1778 _mesa_print_ir(shader->ir, NULL);
1779 printf("\n\n");
1780 }
1781
1782 /* Now the main event: Visit the shader IR and generate our FS IR for it.
1783 */
1784 c->dispatch_width = 8;
1785
1786 fs_visitor v(c, prog, shader);
1787 if (!v.run()) {
1788 prog->LinkStatus = GL_FALSE;
1789 ralloc_strcat(&prog->InfoLog, v.fail_msg);
1790
1791 return false;
1792 }
1793
1794 if (intel->gen >= 5 && c->prog_data.nr_pull_params == 0) {
1795 c->dispatch_width = 16;
1796 fs_visitor v2(c, prog, shader);
1797 v2.import_uniforms(&v);
1798 v2.run();
1799 }
1800
1801 c->prog_data.dispatch_width = 8;
1802
1803 return true;
1804 }
1805
1806 bool
1807 brw_fs_precompile(struct gl_context *ctx, struct gl_shader_program *prog)
1808 {
1809 struct brw_context *brw = brw_context(ctx);
1810 struct brw_wm_prog_key key;
1811 struct gl_fragment_program *fp = prog->FragmentProgram;
1812 struct brw_fragment_program *bfp = brw_fragment_program(fp);
1813
1814 if (!fp)
1815 return true;
1816
1817 memset(&key, 0, sizeof(key));
1818
1819 if (fp->UsesKill)
1820 key.iz_lookup |= IZ_PS_KILL_ALPHATEST_BIT;
1821
1822 if (fp->Base.OutputsWritten & BITFIELD64_BIT(FRAG_RESULT_DEPTH))
1823 key.iz_lookup |= IZ_PS_COMPUTES_DEPTH_BIT;
1824
1825 /* Just assume depth testing. */
1826 key.iz_lookup |= IZ_DEPTH_TEST_ENABLE_BIT;
1827 key.iz_lookup |= IZ_DEPTH_WRITE_ENABLE_BIT;
1828
1829 key.vp_outputs_written |= BITFIELD64_BIT(FRAG_ATTRIB_WPOS);
1830 for (int i = 0; i < FRAG_ATTRIB_MAX; i++) {
1831 int vp_index = -1;
1832
1833 if (!(fp->Base.InputsRead & BITFIELD64_BIT(i)))
1834 continue;
1835
1836 key.proj_attrib_mask |= 1 << i;
1837
1838 if (i <= FRAG_ATTRIB_TEX7)
1839 vp_index = i;
1840 else if (i >= FRAG_ATTRIB_VAR0)
1841 vp_index = i - FRAG_ATTRIB_VAR0 + VERT_RESULT_VAR0;
1842
1843 if (vp_index >= 0)
1844 key.vp_outputs_written |= BITFIELD64_BIT(vp_index);
1845 }
1846
1847 key.clamp_fragment_color = true;
1848
1849 for (int i = 0; i < BRW_MAX_TEX_UNIT; i++) {
1850 if (fp->Base.ShadowSamplers & (1 << i))
1851 key.compare_funcs[i] = GL_LESS;
1852
1853 /* FINISHME: depth compares might use (0,0,0,W) for example */
1854 key.tex_swizzles[i] = SWIZZLE_XYZW;
1855 }
1856
1857 if (fp->Base.InputsRead & FRAG_BIT_WPOS) {
1858 key.drawable_height = ctx->DrawBuffer->Height;
1859 key.render_to_fbo = ctx->DrawBuffer->Name != 0;
1860 }
1861
1862 key.nr_color_regions = 1;
1863
1864 key.program_string_id = bfp->id;
1865
1866 uint32_t old_prog_offset = brw->wm.prog_offset;
1867 struct brw_wm_prog_data *old_prog_data = brw->wm.prog_data;
1868
1869 bool success = do_wm_prog(brw, prog, bfp, &key);
1870
1871 brw->wm.prog_offset = old_prog_offset;
1872 brw->wm.prog_data = old_prog_data;
1873
1874 return success;
1875 }