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