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