i965/fs: Fix fs_inst::regs_read() for sources in the ATTR file.
[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 #include <sys/types.h>
32
33 #include "util/hash_table.h"
34 #include "main/macros.h"
35 #include "main/shaderobj.h"
36 #include "main/fbobject.h"
37 #include "program/prog_parameter.h"
38 #include "program/prog_print.h"
39 #include "util/register_allocate.h"
40 #include "program/hash_table.h"
41 #include "brw_context.h"
42 #include "brw_eu.h"
43 #include "brw_wm.h"
44 #include "brw_fs.h"
45 #include "brw_cfg.h"
46 #include "brw_dead_control_flow.h"
47 #include "main/uniforms.h"
48 #include "brw_fs_live_variables.h"
49 #include "glsl/glsl_types.h"
50 #include "program/sampler.h"
51
52 using namespace brw;
53
54 void
55 fs_inst::init(enum opcode opcode, uint8_t exec_size, const fs_reg &dst,
56 const fs_reg *src, unsigned sources)
57 {
58 memset(this, 0, sizeof(*this));
59
60 this->src = new fs_reg[MAX2(sources, 3)];
61 for (unsigned i = 0; i < sources; i++)
62 this->src[i] = src[i];
63
64 this->opcode = opcode;
65 this->dst = dst;
66 this->sources = sources;
67 this->exec_size = exec_size;
68
69 assert(dst.file != IMM && dst.file != UNIFORM);
70
71 assert(this->exec_size != 0);
72
73 this->conditional_mod = BRW_CONDITIONAL_NONE;
74
75 /* This will be the case for almost all instructions. */
76 switch (dst.file) {
77 case GRF:
78 case HW_REG:
79 case MRF:
80 case ATTR:
81 this->regs_written = DIV_ROUND_UP(dst.component_size(exec_size),
82 REG_SIZE);
83 break;
84 case BAD_FILE:
85 this->regs_written = 0;
86 break;
87 case IMM:
88 case UNIFORM:
89 unreachable("Invalid destination register file");
90 default:
91 unreachable("Invalid register file");
92 }
93
94 this->writes_accumulator = false;
95 }
96
97 fs_inst::fs_inst()
98 {
99 init(BRW_OPCODE_NOP, 8, dst, NULL, 0);
100 }
101
102 fs_inst::fs_inst(enum opcode opcode, uint8_t exec_size)
103 {
104 init(opcode, exec_size, reg_undef, NULL, 0);
105 }
106
107 fs_inst::fs_inst(enum opcode opcode, uint8_t exec_size, const fs_reg &dst)
108 {
109 init(opcode, exec_size, dst, NULL, 0);
110 }
111
112 fs_inst::fs_inst(enum opcode opcode, uint8_t exec_size, const fs_reg &dst,
113 const fs_reg &src0)
114 {
115 const fs_reg src[1] = { src0 };
116 init(opcode, exec_size, dst, src, 1);
117 }
118
119 fs_inst::fs_inst(enum opcode opcode, uint8_t exec_size, const fs_reg &dst,
120 const fs_reg &src0, const fs_reg &src1)
121 {
122 const fs_reg src[2] = { src0, src1 };
123 init(opcode, exec_size, dst, src, 2);
124 }
125
126 fs_inst::fs_inst(enum opcode opcode, uint8_t exec_size, const fs_reg &dst,
127 const fs_reg &src0, const fs_reg &src1, const fs_reg &src2)
128 {
129 const fs_reg src[3] = { src0, src1, src2 };
130 init(opcode, exec_size, dst, src, 3);
131 }
132
133 fs_inst::fs_inst(enum opcode opcode, uint8_t exec_width, const fs_reg &dst,
134 const fs_reg src[], unsigned sources)
135 {
136 init(opcode, exec_width, dst, src, sources);
137 }
138
139 fs_inst::fs_inst(const fs_inst &that)
140 {
141 memcpy(this, &that, sizeof(that));
142
143 this->src = new fs_reg[MAX2(that.sources, 3)];
144
145 for (unsigned i = 0; i < that.sources; i++)
146 this->src[i] = that.src[i];
147 }
148
149 fs_inst::~fs_inst()
150 {
151 delete[] this->src;
152 }
153
154 void
155 fs_inst::resize_sources(uint8_t num_sources)
156 {
157 if (this->sources != num_sources) {
158 fs_reg *src = new fs_reg[MAX2(num_sources, 3)];
159
160 for (unsigned i = 0; i < MIN2(this->sources, num_sources); ++i)
161 src[i] = this->src[i];
162
163 delete[] this->src;
164 this->src = src;
165 this->sources = num_sources;
166 }
167 }
168
169 void
170 fs_visitor::VARYING_PULL_CONSTANT_LOAD(const fs_builder &bld,
171 const fs_reg &dst,
172 const fs_reg &surf_index,
173 const fs_reg &varying_offset,
174 uint32_t const_offset)
175 {
176 /* We have our constant surface use a pitch of 4 bytes, so our index can
177 * be any component of a vector, and then we load 4 contiguous
178 * components starting from that.
179 *
180 * We break down the const_offset to a portion added to the variable
181 * offset and a portion done using reg_offset, which means that if you
182 * have GLSL using something like "uniform vec4 a[20]; gl_FragColor =
183 * a[i]", we'll temporarily generate 4 vec4 loads from offset i * 4, and
184 * CSE can later notice that those loads are all the same and eliminate
185 * the redundant ones.
186 */
187 fs_reg vec4_offset = vgrf(glsl_type::int_type);
188 bld.ADD(vec4_offset, varying_offset, fs_reg(const_offset & ~3));
189
190 int scale = 1;
191 if (devinfo->gen == 4 && bld.dispatch_width() == 8) {
192 /* Pre-gen5, we can either use a SIMD8 message that requires (header,
193 * u, v, r) as parameters, or we can just use the SIMD16 message
194 * consisting of (header, u). We choose the second, at the cost of a
195 * longer return length.
196 */
197 scale = 2;
198 }
199
200 enum opcode op;
201 if (devinfo->gen >= 7)
202 op = FS_OPCODE_VARYING_PULL_CONSTANT_LOAD_GEN7;
203 else
204 op = FS_OPCODE_VARYING_PULL_CONSTANT_LOAD;
205
206 int regs_written = 4 * (bld.dispatch_width() / 8) * scale;
207 fs_reg vec4_result = fs_reg(GRF, alloc.allocate(regs_written), dst.type);
208 fs_inst *inst = bld.emit(op, vec4_result, surf_index, vec4_offset);
209 inst->regs_written = regs_written;
210
211 if (devinfo->gen < 7) {
212 inst->base_mrf = 13;
213 inst->header_size = 1;
214 if (devinfo->gen == 4)
215 inst->mlen = 3;
216 else
217 inst->mlen = 1 + bld.dispatch_width() / 8;
218 }
219
220 bld.MOV(dst, offset(vec4_result, bld, (const_offset & 3) * scale));
221 }
222
223 /**
224 * A helper for MOV generation for fixing up broken hardware SEND dependency
225 * handling.
226 */
227 void
228 fs_visitor::DEP_RESOLVE_MOV(const fs_builder &bld, int grf)
229 {
230 /* The caller always wants uncompressed to emit the minimal extra
231 * dependencies, and to avoid having to deal with aligning its regs to 2.
232 */
233 const fs_builder ubld = bld.annotate("send dependency resolve")
234 .half(0);
235
236 ubld.MOV(ubld.null_reg_f(), fs_reg(GRF, grf, BRW_REGISTER_TYPE_F));
237 }
238
239 bool
240 fs_inst::equals(fs_inst *inst) const
241 {
242 return (opcode == inst->opcode &&
243 dst.equals(inst->dst) &&
244 src[0].equals(inst->src[0]) &&
245 src[1].equals(inst->src[1]) &&
246 src[2].equals(inst->src[2]) &&
247 saturate == inst->saturate &&
248 predicate == inst->predicate &&
249 conditional_mod == inst->conditional_mod &&
250 mlen == inst->mlen &&
251 base_mrf == inst->base_mrf &&
252 target == inst->target &&
253 eot == inst->eot &&
254 header_size == inst->header_size &&
255 shadow_compare == inst->shadow_compare &&
256 exec_size == inst->exec_size &&
257 offset == inst->offset);
258 }
259
260 bool
261 fs_inst::overwrites_reg(const fs_reg &reg) const
262 {
263 return reg.in_range(dst, regs_written);
264 }
265
266 bool
267 fs_inst::is_send_from_grf() const
268 {
269 switch (opcode) {
270 case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD_GEN7:
271 case SHADER_OPCODE_SHADER_TIME_ADD:
272 case FS_OPCODE_INTERPOLATE_AT_CENTROID:
273 case FS_OPCODE_INTERPOLATE_AT_SAMPLE:
274 case FS_OPCODE_INTERPOLATE_AT_SHARED_OFFSET:
275 case FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET:
276 case SHADER_OPCODE_UNTYPED_ATOMIC:
277 case SHADER_OPCODE_UNTYPED_SURFACE_READ:
278 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE:
279 case SHADER_OPCODE_TYPED_ATOMIC:
280 case SHADER_OPCODE_TYPED_SURFACE_READ:
281 case SHADER_OPCODE_TYPED_SURFACE_WRITE:
282 case SHADER_OPCODE_URB_WRITE_SIMD8:
283 return true;
284 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD:
285 return src[1].file == GRF;
286 case FS_OPCODE_FB_WRITE:
287 return src[0].file == GRF;
288 default:
289 if (is_tex())
290 return src[0].file == GRF;
291
292 return false;
293 }
294 }
295
296 bool
297 fs_inst::is_copy_payload(const brw::simple_allocator &grf_alloc) const
298 {
299 if (this->opcode != SHADER_OPCODE_LOAD_PAYLOAD)
300 return false;
301
302 fs_reg reg = this->src[0];
303 if (reg.file != GRF || reg.reg_offset != 0 || reg.stride == 0)
304 return false;
305
306 if (grf_alloc.sizes[reg.reg] != this->regs_written)
307 return false;
308
309 for (int i = 0; i < this->sources; i++) {
310 reg.type = this->src[i].type;
311 if (!this->src[i].equals(reg))
312 return false;
313
314 if (i < this->header_size) {
315 reg.reg_offset += 1;
316 } else {
317 reg.reg_offset += this->exec_size / 8;
318 }
319 }
320
321 return true;
322 }
323
324 bool
325 fs_inst::can_do_source_mods(const struct brw_device_info *devinfo)
326 {
327 if (devinfo->gen == 6 && is_math())
328 return false;
329
330 if (is_send_from_grf())
331 return false;
332
333 if (!backend_instruction::can_do_source_mods())
334 return false;
335
336 return true;
337 }
338
339 bool
340 fs_inst::has_side_effects() const
341 {
342 return this->eot || backend_instruction::has_side_effects();
343 }
344
345 void
346 fs_reg::init()
347 {
348 memset(this, 0, sizeof(*this));
349 stride = 1;
350 }
351
352 /** Generic unset register constructor. */
353 fs_reg::fs_reg()
354 {
355 init();
356 this->file = BAD_FILE;
357 }
358
359 /** Immediate value constructor. */
360 fs_reg::fs_reg(float f)
361 {
362 init();
363 this->file = IMM;
364 this->type = BRW_REGISTER_TYPE_F;
365 this->stride = 0;
366 this->fixed_hw_reg.dw1.f = f;
367 }
368
369 /** Immediate value constructor. */
370 fs_reg::fs_reg(int32_t i)
371 {
372 init();
373 this->file = IMM;
374 this->type = BRW_REGISTER_TYPE_D;
375 this->stride = 0;
376 this->fixed_hw_reg.dw1.d = i;
377 }
378
379 /** Immediate value constructor. */
380 fs_reg::fs_reg(uint32_t u)
381 {
382 init();
383 this->file = IMM;
384 this->type = BRW_REGISTER_TYPE_UD;
385 this->stride = 0;
386 this->fixed_hw_reg.dw1.ud = u;
387 }
388
389 /** Vector float immediate value constructor. */
390 fs_reg::fs_reg(uint8_t vf[4])
391 {
392 init();
393 this->file = IMM;
394 this->type = BRW_REGISTER_TYPE_VF;
395 memcpy(&this->fixed_hw_reg.dw1.ud, vf, sizeof(unsigned));
396 }
397
398 /** Vector float immediate value constructor. */
399 fs_reg::fs_reg(uint8_t vf0, uint8_t vf1, uint8_t vf2, uint8_t vf3)
400 {
401 init();
402 this->file = IMM;
403 this->type = BRW_REGISTER_TYPE_VF;
404 this->fixed_hw_reg.dw1.ud = (vf0 << 0) |
405 (vf1 << 8) |
406 (vf2 << 16) |
407 (vf3 << 24);
408 }
409
410 /** Fixed brw_reg. */
411 fs_reg::fs_reg(struct brw_reg fixed_hw_reg)
412 {
413 init();
414 this->file = HW_REG;
415 this->fixed_hw_reg = fixed_hw_reg;
416 this->type = fixed_hw_reg.type;
417 }
418
419 bool
420 fs_reg::equals(const fs_reg &r) const
421 {
422 return (file == r.file &&
423 reg == r.reg &&
424 reg_offset == r.reg_offset &&
425 subreg_offset == r.subreg_offset &&
426 type == r.type &&
427 negate == r.negate &&
428 abs == r.abs &&
429 !reladdr && !r.reladdr &&
430 memcmp(&fixed_hw_reg, &r.fixed_hw_reg, sizeof(fixed_hw_reg)) == 0 &&
431 stride == r.stride);
432 }
433
434 fs_reg &
435 fs_reg::set_smear(unsigned subreg)
436 {
437 assert(file != HW_REG && file != IMM);
438 subreg_offset = subreg * type_sz(type);
439 stride = 0;
440 return *this;
441 }
442
443 bool
444 fs_reg::is_contiguous() const
445 {
446 return stride == 1;
447 }
448
449 unsigned
450 fs_reg::component_size(unsigned width) const
451 {
452 const unsigned stride = (file != HW_REG ? this->stride :
453 fixed_hw_reg.hstride == 0 ? 0 :
454 1 << (fixed_hw_reg.hstride - 1));
455 return MAX2(width * stride, 1) * type_sz(type);
456 }
457
458 int
459 fs_visitor::type_size(const struct glsl_type *type)
460 {
461 unsigned int size, i;
462
463 switch (type->base_type) {
464 case GLSL_TYPE_UINT:
465 case GLSL_TYPE_INT:
466 case GLSL_TYPE_FLOAT:
467 case GLSL_TYPE_BOOL:
468 return type->components();
469 case GLSL_TYPE_ARRAY:
470 return type_size(type->fields.array) * type->length;
471 case GLSL_TYPE_STRUCT:
472 size = 0;
473 for (i = 0; i < type->length; i++) {
474 size += type_size(type->fields.structure[i].type);
475 }
476 return size;
477 case GLSL_TYPE_SAMPLER:
478 /* Samplers take up no register space, since they're baked in at
479 * link time.
480 */
481 return 0;
482 case GLSL_TYPE_ATOMIC_UINT:
483 return 0;
484 case GLSL_TYPE_SUBROUTINE:
485 return 1;
486 case GLSL_TYPE_IMAGE:
487 case GLSL_TYPE_VOID:
488 case GLSL_TYPE_ERROR:
489 case GLSL_TYPE_INTERFACE:
490 case GLSL_TYPE_DOUBLE:
491 unreachable("not reached");
492 }
493
494 return 0;
495 }
496
497 /**
498 * Create a MOV to read the timestamp register.
499 *
500 * The caller is responsible for emitting the MOV. The return value is
501 * the destination of the MOV, with extra parameters set.
502 */
503 fs_reg
504 fs_visitor::get_timestamp(const fs_builder &bld)
505 {
506 assert(devinfo->gen >= 7);
507
508 fs_reg ts = fs_reg(retype(brw_vec4_reg(BRW_ARCHITECTURE_REGISTER_FILE,
509 BRW_ARF_TIMESTAMP,
510 0),
511 BRW_REGISTER_TYPE_UD));
512
513 fs_reg dst = fs_reg(GRF, alloc.allocate(1), BRW_REGISTER_TYPE_UD);
514
515 /* We want to read the 3 fields we care about even if it's not enabled in
516 * the dispatch.
517 */
518 bld.group(4, 0).exec_all().MOV(dst, ts);
519
520 /* The caller wants the low 32 bits of the timestamp. Since it's running
521 * at the GPU clock rate of ~1.2ghz, it will roll over every ~3 seconds,
522 * which is plenty of time for our purposes. It is identical across the
523 * EUs, but since it's tracking GPU core speed it will increment at a
524 * varying rate as render P-states change.
525 *
526 * The caller could also check if render P-states have changed (or anything
527 * else that might disrupt timing) by setting smear to 2 and checking if
528 * that field is != 0.
529 */
530 dst.set_smear(0);
531
532 return dst;
533 }
534
535 void
536 fs_visitor::emit_shader_time_begin()
537 {
538 shader_start_time = get_timestamp(bld.annotate("shader time start"));
539 }
540
541 void
542 fs_visitor::emit_shader_time_end()
543 {
544 /* Insert our code just before the final SEND with EOT. */
545 exec_node *end = this->instructions.get_tail();
546 assert(end && ((fs_inst *) end)->eot);
547 const fs_builder ibld = bld.annotate("shader time end")
548 .exec_all().at(NULL, end);
549
550 fs_reg shader_end_time = get_timestamp(ibld);
551
552 /* Check that there weren't any timestamp reset events (assuming these
553 * were the only two timestamp reads that happened).
554 */
555 fs_reg reset = shader_end_time;
556 reset.set_smear(2);
557 set_condmod(BRW_CONDITIONAL_Z,
558 ibld.AND(ibld.null_reg_ud(), reset, fs_reg(1u)));
559 ibld.IF(BRW_PREDICATE_NORMAL);
560
561 fs_reg start = shader_start_time;
562 start.negate = true;
563 fs_reg diff = fs_reg(GRF, alloc.allocate(1), BRW_REGISTER_TYPE_UD);
564 diff.set_smear(0);
565
566 const fs_builder cbld = ibld.group(1, 0);
567 cbld.group(1, 0).ADD(diff, start, shader_end_time);
568
569 /* If there were no instructions between the two timestamp gets, the diff
570 * is 2 cycles. Remove that overhead, so I can forget about that when
571 * trying to determine the time taken for single instructions.
572 */
573 cbld.ADD(diff, diff, fs_reg(-2u));
574 SHADER_TIME_ADD(cbld, 0, diff);
575 SHADER_TIME_ADD(cbld, 1, fs_reg(1u));
576 ibld.emit(BRW_OPCODE_ELSE);
577 SHADER_TIME_ADD(cbld, 2, fs_reg(1u));
578 ibld.emit(BRW_OPCODE_ENDIF);
579 }
580
581 void
582 fs_visitor::SHADER_TIME_ADD(const fs_builder &bld,
583 int shader_time_subindex,
584 fs_reg value)
585 {
586 int index = shader_time_index * 3 + shader_time_subindex;
587 fs_reg offset = fs_reg(index * SHADER_TIME_STRIDE);
588
589 fs_reg payload;
590 if (dispatch_width == 8)
591 payload = vgrf(glsl_type::uvec2_type);
592 else
593 payload = vgrf(glsl_type::uint_type);
594
595 bld.emit(SHADER_OPCODE_SHADER_TIME_ADD, fs_reg(), payload, offset, value);
596 }
597
598 void
599 fs_visitor::vfail(const char *format, va_list va)
600 {
601 char *msg;
602
603 if (failed)
604 return;
605
606 failed = true;
607
608 msg = ralloc_vasprintf(mem_ctx, format, va);
609 msg = ralloc_asprintf(mem_ctx, "%s compile failed: %s\n", stage_abbrev, msg);
610
611 this->fail_msg = msg;
612
613 if (debug_enabled) {
614 fprintf(stderr, "%s", msg);
615 }
616 }
617
618 void
619 fs_visitor::fail(const char *format, ...)
620 {
621 va_list va;
622
623 va_start(va, format);
624 vfail(format, va);
625 va_end(va);
626 }
627
628 /**
629 * Mark this program as impossible to compile in SIMD16 mode.
630 *
631 * During the SIMD8 compile (which happens first), we can detect and flag
632 * things that are unsupported in SIMD16 mode, so the compiler can skip
633 * the SIMD16 compile altogether.
634 *
635 * During a SIMD16 compile (if one happens anyway), this just calls fail().
636 */
637 void
638 fs_visitor::no16(const char *msg)
639 {
640 if (dispatch_width == 16) {
641 fail("%s", msg);
642 } else {
643 simd16_unsupported = true;
644
645 compiler->shader_perf_log(log_data,
646 "SIMD16 shader failed to compile: %s", msg);
647 }
648 }
649
650 /**
651 * Returns true if the instruction has a flag that means it won't
652 * update an entire destination register.
653 *
654 * For example, dead code elimination and live variable analysis want to know
655 * when a write to a variable screens off any preceding values that were in
656 * it.
657 */
658 bool
659 fs_inst::is_partial_write() const
660 {
661 return ((this->predicate && this->opcode != BRW_OPCODE_SEL) ||
662 (this->exec_size * type_sz(this->dst.type)) < 32 ||
663 !this->dst.is_contiguous());
664 }
665
666 unsigned
667 fs_inst::components_read(unsigned i) const
668 {
669 switch (opcode) {
670 case FS_OPCODE_LINTERP:
671 if (i == 0)
672 return 2;
673 else
674 return 1;
675
676 case FS_OPCODE_PIXEL_X:
677 case FS_OPCODE_PIXEL_Y:
678 assert(i == 0);
679 return 2;
680
681 case FS_OPCODE_FB_WRITE_LOGICAL:
682 assert(src[6].file == IMM);
683 /* First/second FB write color. */
684 if (i < 2)
685 return src[6].fixed_hw_reg.dw1.ud;
686 else
687 return 1;
688
689 case SHADER_OPCODE_TEX_LOGICAL:
690 case SHADER_OPCODE_TXD_LOGICAL:
691 case SHADER_OPCODE_TXF_LOGICAL:
692 case SHADER_OPCODE_TXL_LOGICAL:
693 case SHADER_OPCODE_TXS_LOGICAL:
694 case FS_OPCODE_TXB_LOGICAL:
695 case SHADER_OPCODE_TXF_CMS_LOGICAL:
696 case SHADER_OPCODE_TXF_UMS_LOGICAL:
697 case SHADER_OPCODE_TXF_MCS_LOGICAL:
698 case SHADER_OPCODE_LOD_LOGICAL:
699 case SHADER_OPCODE_TG4_LOGICAL:
700 case SHADER_OPCODE_TG4_OFFSET_LOGICAL:
701 assert(src[8].file == IMM && src[9].file == IMM);
702 /* Texture coordinates. */
703 if (i == 0)
704 return src[8].fixed_hw_reg.dw1.ud;
705 /* Texture derivatives. */
706 else if ((i == 2 || i == 3) && opcode == SHADER_OPCODE_TXD_LOGICAL)
707 return src[9].fixed_hw_reg.dw1.ud;
708 /* Texture offset. */
709 else if (i == 7)
710 return 2;
711 else
712 return 1;
713
714 case SHADER_OPCODE_UNTYPED_SURFACE_READ_LOGICAL:
715 case SHADER_OPCODE_TYPED_SURFACE_READ_LOGICAL:
716 assert(src[3].file == IMM);
717 /* Surface coordinates. */
718 if (i == 0)
719 return src[3].fixed_hw_reg.dw1.ud;
720 /* Surface operation source (ignored for reads). */
721 else if (i == 1)
722 return 0;
723 else
724 return 1;
725
726 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE_LOGICAL:
727 case SHADER_OPCODE_TYPED_SURFACE_WRITE_LOGICAL:
728 assert(src[3].file == IMM &&
729 src[4].file == IMM);
730 /* Surface coordinates. */
731 if (i == 0)
732 return src[3].fixed_hw_reg.dw1.ud;
733 /* Surface operation source. */
734 else if (i == 1)
735 return src[4].fixed_hw_reg.dw1.ud;
736 else
737 return 1;
738
739 case SHADER_OPCODE_UNTYPED_ATOMIC_LOGICAL:
740 case SHADER_OPCODE_TYPED_ATOMIC_LOGICAL: {
741 assert(src[3].file == IMM &&
742 src[4].file == IMM);
743 const unsigned op = src[4].fixed_hw_reg.dw1.ud;
744 /* Surface coordinates. */
745 if (i == 0)
746 return src[3].fixed_hw_reg.dw1.ud;
747 /* Surface operation source. */
748 else if (i == 1 && op == BRW_AOP_CMPWR)
749 return 2;
750 else if (i == 1 && (op == BRW_AOP_INC || op == BRW_AOP_DEC ||
751 op == BRW_AOP_PREDEC))
752 return 0;
753 else
754 return 1;
755 }
756
757 default:
758 return 1;
759 }
760 }
761
762 int
763 fs_inst::regs_read(int arg) const
764 {
765 switch (opcode) {
766 case FS_OPCODE_FB_WRITE:
767 case SHADER_OPCODE_URB_WRITE_SIMD8:
768 case SHADER_OPCODE_UNTYPED_ATOMIC:
769 case SHADER_OPCODE_UNTYPED_SURFACE_READ:
770 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE:
771 case SHADER_OPCODE_TYPED_ATOMIC:
772 case SHADER_OPCODE_TYPED_SURFACE_READ:
773 case SHADER_OPCODE_TYPED_SURFACE_WRITE:
774 case FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET:
775 if (arg == 0)
776 return mlen;
777 break;
778
779 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD_GEN7:
780 /* The payload is actually stored in src1 */
781 if (arg == 1)
782 return mlen;
783 break;
784
785 case FS_OPCODE_LINTERP:
786 if (arg == 1)
787 return 1;
788 break;
789
790 case SHADER_OPCODE_LOAD_PAYLOAD:
791 if (arg < this->header_size)
792 return 1;
793 break;
794
795 case CS_OPCODE_CS_TERMINATE:
796 return 1;
797
798 default:
799 if (is_tex() && arg == 0 && src[0].file == GRF)
800 return mlen;
801 break;
802 }
803
804 switch (src[arg].file) {
805 case BAD_FILE:
806 return 0;
807 case UNIFORM:
808 case IMM:
809 return 1;
810 case GRF:
811 case ATTR:
812 case HW_REG:
813 return DIV_ROUND_UP(components_read(arg) *
814 src[arg].component_size(exec_size),
815 REG_SIZE);
816 case MRF:
817 unreachable("MRF registers are not allowed as sources");
818 default:
819 unreachable("Invalid register file");
820 }
821 }
822
823 bool
824 fs_inst::reads_flag() const
825 {
826 return predicate;
827 }
828
829 bool
830 fs_inst::writes_flag() const
831 {
832 return (conditional_mod && (opcode != BRW_OPCODE_SEL &&
833 opcode != BRW_OPCODE_IF &&
834 opcode != BRW_OPCODE_WHILE)) ||
835 opcode == FS_OPCODE_MOV_DISPATCH_TO_FLAGS;
836 }
837
838 /**
839 * Returns how many MRFs an FS opcode will write over.
840 *
841 * Note that this is not the 0 or 1 implied writes in an actual gen
842 * instruction -- the FS opcodes often generate MOVs in addition.
843 */
844 int
845 fs_visitor::implied_mrf_writes(fs_inst *inst)
846 {
847 if (inst->mlen == 0)
848 return 0;
849
850 if (inst->base_mrf == -1)
851 return 0;
852
853 switch (inst->opcode) {
854 case SHADER_OPCODE_RCP:
855 case SHADER_OPCODE_RSQ:
856 case SHADER_OPCODE_SQRT:
857 case SHADER_OPCODE_EXP2:
858 case SHADER_OPCODE_LOG2:
859 case SHADER_OPCODE_SIN:
860 case SHADER_OPCODE_COS:
861 return 1 * dispatch_width / 8;
862 case SHADER_OPCODE_POW:
863 case SHADER_OPCODE_INT_QUOTIENT:
864 case SHADER_OPCODE_INT_REMAINDER:
865 return 2 * dispatch_width / 8;
866 case SHADER_OPCODE_TEX:
867 case FS_OPCODE_TXB:
868 case SHADER_OPCODE_TXD:
869 case SHADER_OPCODE_TXF:
870 case SHADER_OPCODE_TXF_CMS:
871 case SHADER_OPCODE_TXF_MCS:
872 case SHADER_OPCODE_TG4:
873 case SHADER_OPCODE_TG4_OFFSET:
874 case SHADER_OPCODE_TXL:
875 case SHADER_OPCODE_TXS:
876 case SHADER_OPCODE_LOD:
877 return 1;
878 case FS_OPCODE_FB_WRITE:
879 return 2;
880 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD:
881 case SHADER_OPCODE_GEN4_SCRATCH_READ:
882 return 1;
883 case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD:
884 return inst->mlen;
885 case SHADER_OPCODE_GEN4_SCRATCH_WRITE:
886 return inst->mlen;
887 case SHADER_OPCODE_UNTYPED_ATOMIC:
888 case SHADER_OPCODE_UNTYPED_SURFACE_READ:
889 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE:
890 case SHADER_OPCODE_TYPED_ATOMIC:
891 case SHADER_OPCODE_TYPED_SURFACE_READ:
892 case SHADER_OPCODE_TYPED_SURFACE_WRITE:
893 case SHADER_OPCODE_URB_WRITE_SIMD8:
894 case FS_OPCODE_INTERPOLATE_AT_CENTROID:
895 case FS_OPCODE_INTERPOLATE_AT_SAMPLE:
896 case FS_OPCODE_INTERPOLATE_AT_SHARED_OFFSET:
897 case FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET:
898 return 0;
899 default:
900 unreachable("not reached");
901 }
902 }
903
904 fs_reg
905 fs_visitor::vgrf(const glsl_type *const type)
906 {
907 int reg_width = dispatch_width / 8;
908 return fs_reg(GRF, alloc.allocate(type_size(type) * reg_width),
909 brw_type_for_base_type(type));
910 }
911
912 /** Fixed HW reg constructor. */
913 fs_reg::fs_reg(enum register_file file, int reg)
914 {
915 init();
916 this->file = file;
917 this->reg = reg;
918 this->type = BRW_REGISTER_TYPE_F;
919 this->stride = (file == UNIFORM ? 0 : 1);
920 }
921
922 /** Fixed HW reg constructor. */
923 fs_reg::fs_reg(enum register_file file, int reg, enum brw_reg_type type)
924 {
925 init();
926 this->file = file;
927 this->reg = reg;
928 this->type = type;
929 this->stride = (file == UNIFORM ? 0 : 1);
930 }
931
932 /* For SIMD16, we need to follow from the uniform setup of SIMD8 dispatch.
933 * This brings in those uniform definitions
934 */
935 void
936 fs_visitor::import_uniforms(fs_visitor *v)
937 {
938 this->push_constant_loc = v->push_constant_loc;
939 this->pull_constant_loc = v->pull_constant_loc;
940 this->uniforms = v->uniforms;
941 this->param_size = v->param_size;
942 }
943
944 void
945 fs_visitor::setup_vector_uniform_values(const gl_constant_value *values, unsigned n)
946 {
947 static const gl_constant_value zero = { 0 };
948
949 for (unsigned i = 0; i < n; ++i)
950 stage_prog_data->param[uniforms++] = &values[i];
951
952 for (unsigned i = n; i < 4; ++i)
953 stage_prog_data->param[uniforms++] = &zero;
954 }
955
956 fs_reg *
957 fs_visitor::emit_fragcoord_interpolation(bool pixel_center_integer,
958 bool origin_upper_left)
959 {
960 assert(stage == MESA_SHADER_FRAGMENT);
961 brw_wm_prog_key *key = (brw_wm_prog_key*) this->key;
962 fs_reg *reg = new(this->mem_ctx) fs_reg(vgrf(glsl_type::vec4_type));
963 fs_reg wpos = *reg;
964 bool flip = !origin_upper_left ^ key->render_to_fbo;
965
966 /* gl_FragCoord.x */
967 if (pixel_center_integer) {
968 bld.MOV(wpos, this->pixel_x);
969 } else {
970 bld.ADD(wpos, this->pixel_x, fs_reg(0.5f));
971 }
972 wpos = offset(wpos, bld, 1);
973
974 /* gl_FragCoord.y */
975 if (!flip && pixel_center_integer) {
976 bld.MOV(wpos, this->pixel_y);
977 } else {
978 fs_reg pixel_y = this->pixel_y;
979 float offset = (pixel_center_integer ? 0.0f : 0.5f);
980
981 if (flip) {
982 pixel_y.negate = true;
983 offset += key->drawable_height - 1.0f;
984 }
985
986 bld.ADD(wpos, pixel_y, fs_reg(offset));
987 }
988 wpos = offset(wpos, bld, 1);
989
990 /* gl_FragCoord.z */
991 if (devinfo->gen >= 6) {
992 bld.MOV(wpos, fs_reg(brw_vec8_grf(payload.source_depth_reg, 0)));
993 } else {
994 bld.emit(FS_OPCODE_LINTERP, wpos,
995 this->delta_xy[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC],
996 interp_reg(VARYING_SLOT_POS, 2));
997 }
998 wpos = offset(wpos, bld, 1);
999
1000 /* gl_FragCoord.w: Already set up in emit_interpolation */
1001 bld.MOV(wpos, this->wpos_w);
1002
1003 return reg;
1004 }
1005
1006 fs_inst *
1007 fs_visitor::emit_linterp(const fs_reg &attr, const fs_reg &interp,
1008 glsl_interp_qualifier interpolation_mode,
1009 bool is_centroid, bool is_sample)
1010 {
1011 brw_wm_barycentric_interp_mode barycoord_mode;
1012 if (devinfo->gen >= 6) {
1013 if (is_centroid) {
1014 if (interpolation_mode == INTERP_QUALIFIER_SMOOTH)
1015 barycoord_mode = BRW_WM_PERSPECTIVE_CENTROID_BARYCENTRIC;
1016 else
1017 barycoord_mode = BRW_WM_NONPERSPECTIVE_CENTROID_BARYCENTRIC;
1018 } else if (is_sample) {
1019 if (interpolation_mode == INTERP_QUALIFIER_SMOOTH)
1020 barycoord_mode = BRW_WM_PERSPECTIVE_SAMPLE_BARYCENTRIC;
1021 else
1022 barycoord_mode = BRW_WM_NONPERSPECTIVE_SAMPLE_BARYCENTRIC;
1023 } else {
1024 if (interpolation_mode == INTERP_QUALIFIER_SMOOTH)
1025 barycoord_mode = BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC;
1026 else
1027 barycoord_mode = BRW_WM_NONPERSPECTIVE_PIXEL_BARYCENTRIC;
1028 }
1029 } else {
1030 /* On Ironlake and below, there is only one interpolation mode.
1031 * Centroid interpolation doesn't mean anything on this hardware --
1032 * there is no multisampling.
1033 */
1034 barycoord_mode = BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC;
1035 }
1036 return bld.emit(FS_OPCODE_LINTERP, attr,
1037 this->delta_xy[barycoord_mode], interp);
1038 }
1039
1040 void
1041 fs_visitor::emit_general_interpolation(fs_reg attr, const char *name,
1042 const glsl_type *type,
1043 glsl_interp_qualifier interpolation_mode,
1044 int location, bool mod_centroid,
1045 bool mod_sample)
1046 {
1047 attr.type = brw_type_for_base_type(type->get_scalar_type());
1048
1049 assert(stage == MESA_SHADER_FRAGMENT);
1050 brw_wm_prog_data *prog_data = (brw_wm_prog_data*) this->prog_data;
1051 brw_wm_prog_key *key = (brw_wm_prog_key*) this->key;
1052
1053 unsigned int array_elements;
1054
1055 if (type->is_array()) {
1056 array_elements = type->length;
1057 if (array_elements == 0) {
1058 fail("dereferenced array '%s' has length 0\n", name);
1059 }
1060 type = type->fields.array;
1061 } else {
1062 array_elements = 1;
1063 }
1064
1065 if (interpolation_mode == INTERP_QUALIFIER_NONE) {
1066 bool is_gl_Color =
1067 location == VARYING_SLOT_COL0 || location == VARYING_SLOT_COL1;
1068 if (key->flat_shade && is_gl_Color) {
1069 interpolation_mode = INTERP_QUALIFIER_FLAT;
1070 } else {
1071 interpolation_mode = INTERP_QUALIFIER_SMOOTH;
1072 }
1073 }
1074
1075 for (unsigned int i = 0; i < array_elements; i++) {
1076 for (unsigned int j = 0; j < type->matrix_columns; j++) {
1077 if (prog_data->urb_setup[location] == -1) {
1078 /* If there's no incoming setup data for this slot, don't
1079 * emit interpolation for it.
1080 */
1081 attr = offset(attr, bld, type->vector_elements);
1082 location++;
1083 continue;
1084 }
1085
1086 if (interpolation_mode == INTERP_QUALIFIER_FLAT) {
1087 /* Constant interpolation (flat shading) case. The SF has
1088 * handed us defined values in only the constant offset
1089 * field of the setup reg.
1090 */
1091 for (unsigned int k = 0; k < type->vector_elements; k++) {
1092 struct brw_reg interp = interp_reg(location, k);
1093 interp = suboffset(interp, 3);
1094 interp.type = attr.type;
1095 bld.emit(FS_OPCODE_CINTERP, attr, fs_reg(interp));
1096 attr = offset(attr, bld, 1);
1097 }
1098 } else {
1099 /* Smooth/noperspective interpolation case. */
1100 for (unsigned int k = 0; k < type->vector_elements; k++) {
1101 struct brw_reg interp = interp_reg(location, k);
1102 if (devinfo->needs_unlit_centroid_workaround && mod_centroid) {
1103 /* Get the pixel/sample mask into f0 so that we know
1104 * which pixels are lit. Then, for each channel that is
1105 * unlit, replace the centroid data with non-centroid
1106 * data.
1107 */
1108 bld.emit(FS_OPCODE_MOV_DISPATCH_TO_FLAGS);
1109
1110 fs_inst *inst;
1111 inst = emit_linterp(attr, fs_reg(interp), interpolation_mode,
1112 false, false);
1113 inst->predicate = BRW_PREDICATE_NORMAL;
1114 inst->predicate_inverse = true;
1115 if (devinfo->has_pln)
1116 inst->no_dd_clear = true;
1117
1118 inst = emit_linterp(attr, fs_reg(interp), interpolation_mode,
1119 mod_centroid && !key->persample_shading,
1120 mod_sample || key->persample_shading);
1121 inst->predicate = BRW_PREDICATE_NORMAL;
1122 inst->predicate_inverse = false;
1123 if (devinfo->has_pln)
1124 inst->no_dd_check = true;
1125
1126 } else {
1127 emit_linterp(attr, fs_reg(interp), interpolation_mode,
1128 mod_centroid && !key->persample_shading,
1129 mod_sample || key->persample_shading);
1130 }
1131 if (devinfo->gen < 6 && interpolation_mode == INTERP_QUALIFIER_SMOOTH) {
1132 bld.MUL(attr, attr, this->pixel_w);
1133 }
1134 attr = offset(attr, bld, 1);
1135 }
1136
1137 }
1138 location++;
1139 }
1140 }
1141 }
1142
1143 fs_reg *
1144 fs_visitor::emit_frontfacing_interpolation()
1145 {
1146 fs_reg *reg = new(this->mem_ctx) fs_reg(vgrf(glsl_type::bool_type));
1147
1148 if (devinfo->gen >= 6) {
1149 /* Bit 15 of g0.0 is 0 if the polygon is front facing. We want to create
1150 * a boolean result from this (~0/true or 0/false).
1151 *
1152 * We can use the fact that bit 15 is the MSB of g0.0:W to accomplish
1153 * this task in only one instruction:
1154 * - a negation source modifier will flip the bit; and
1155 * - a W -> D type conversion will sign extend the bit into the high
1156 * word of the destination.
1157 *
1158 * An ASR 15 fills the low word of the destination.
1159 */
1160 fs_reg g0 = fs_reg(retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_W));
1161 g0.negate = true;
1162
1163 bld.ASR(*reg, g0, fs_reg(15));
1164 } else {
1165 /* Bit 31 of g1.6 is 0 if the polygon is front facing. We want to create
1166 * a boolean result from this (1/true or 0/false).
1167 *
1168 * Like in the above case, since the bit is the MSB of g1.6:UD we can use
1169 * the negation source modifier to flip it. Unfortunately the SHR
1170 * instruction only operates on UD (or D with an abs source modifier)
1171 * sources without negation.
1172 *
1173 * Instead, use ASR (which will give ~0/true or 0/false).
1174 */
1175 fs_reg g1_6 = fs_reg(retype(brw_vec1_grf(1, 6), BRW_REGISTER_TYPE_D));
1176 g1_6.negate = true;
1177
1178 bld.ASR(*reg, g1_6, fs_reg(31));
1179 }
1180
1181 return reg;
1182 }
1183
1184 void
1185 fs_visitor::compute_sample_position(fs_reg dst, fs_reg int_sample_pos)
1186 {
1187 assert(stage == MESA_SHADER_FRAGMENT);
1188 brw_wm_prog_key *key = (brw_wm_prog_key*) this->key;
1189 assert(dst.type == BRW_REGISTER_TYPE_F);
1190
1191 if (key->compute_pos_offset) {
1192 /* Convert int_sample_pos to floating point */
1193 bld.MOV(dst, int_sample_pos);
1194 /* Scale to the range [0, 1] */
1195 bld.MUL(dst, dst, fs_reg(1 / 16.0f));
1196 }
1197 else {
1198 /* From ARB_sample_shading specification:
1199 * "When rendering to a non-multisample buffer, or if multisample
1200 * rasterization is disabled, gl_SamplePosition will always be
1201 * (0.5, 0.5).
1202 */
1203 bld.MOV(dst, fs_reg(0.5f));
1204 }
1205 }
1206
1207 fs_reg *
1208 fs_visitor::emit_samplepos_setup()
1209 {
1210 assert(devinfo->gen >= 6);
1211
1212 const fs_builder abld = bld.annotate("compute sample position");
1213 fs_reg *reg = new(this->mem_ctx) fs_reg(vgrf(glsl_type::vec2_type));
1214 fs_reg pos = *reg;
1215 fs_reg int_sample_x = vgrf(glsl_type::int_type);
1216 fs_reg int_sample_y = vgrf(glsl_type::int_type);
1217
1218 /* WM will be run in MSDISPMODE_PERSAMPLE. So, only one of SIMD8 or SIMD16
1219 * mode will be enabled.
1220 *
1221 * From the Ivy Bridge PRM, volume 2 part 1, page 344:
1222 * R31.1:0 Position Offset X/Y for Slot[3:0]
1223 * R31.3:2 Position Offset X/Y for Slot[7:4]
1224 * .....
1225 *
1226 * The X, Y sample positions come in as bytes in thread payload. So, read
1227 * the positions using vstride=16, width=8, hstride=2.
1228 */
1229 struct brw_reg sample_pos_reg =
1230 stride(retype(brw_vec1_grf(payload.sample_pos_reg, 0),
1231 BRW_REGISTER_TYPE_B), 16, 8, 2);
1232
1233 if (dispatch_width == 8) {
1234 abld.MOV(int_sample_x, fs_reg(sample_pos_reg));
1235 } else {
1236 abld.half(0).MOV(half(int_sample_x, 0), fs_reg(sample_pos_reg));
1237 abld.half(1).MOV(half(int_sample_x, 1),
1238 fs_reg(suboffset(sample_pos_reg, 16)));
1239 }
1240 /* Compute gl_SamplePosition.x */
1241 compute_sample_position(pos, int_sample_x);
1242 pos = offset(pos, abld, 1);
1243 if (dispatch_width == 8) {
1244 abld.MOV(int_sample_y, fs_reg(suboffset(sample_pos_reg, 1)));
1245 } else {
1246 abld.half(0).MOV(half(int_sample_y, 0),
1247 fs_reg(suboffset(sample_pos_reg, 1)));
1248 abld.half(1).MOV(half(int_sample_y, 1),
1249 fs_reg(suboffset(sample_pos_reg, 17)));
1250 }
1251 /* Compute gl_SamplePosition.y */
1252 compute_sample_position(pos, int_sample_y);
1253 return reg;
1254 }
1255
1256 fs_reg *
1257 fs_visitor::emit_sampleid_setup()
1258 {
1259 assert(stage == MESA_SHADER_FRAGMENT);
1260 brw_wm_prog_key *key = (brw_wm_prog_key*) this->key;
1261 assert(devinfo->gen >= 6);
1262
1263 const fs_builder abld = bld.annotate("compute sample id");
1264 fs_reg *reg = new(this->mem_ctx) fs_reg(vgrf(glsl_type::int_type));
1265
1266 if (key->compute_sample_id) {
1267 fs_reg t1 = vgrf(glsl_type::int_type);
1268 fs_reg t2 = vgrf(glsl_type::int_type);
1269 t2.type = BRW_REGISTER_TYPE_UW;
1270
1271 /* The PS will be run in MSDISPMODE_PERSAMPLE. For example with
1272 * 8x multisampling, subspan 0 will represent sample N (where N
1273 * is 0, 2, 4 or 6), subspan 1 will represent sample 1, 3, 5 or
1274 * 7. We can find the value of N by looking at R0.0 bits 7:6
1275 * ("Starting Sample Pair Index (SSPI)") and multiplying by two
1276 * (since samples are always delivered in pairs). That is, we
1277 * compute 2*((R0.0 & 0xc0) >> 6) == (R0.0 & 0xc0) >> 5. Then
1278 * we need to add N to the sequence (0, 0, 0, 0, 1, 1, 1, 1) in
1279 * case of SIMD8 and sequence (0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2,
1280 * 2, 3, 3, 3, 3) in case of SIMD16. We compute this sequence by
1281 * populating a temporary variable with the sequence (0, 1, 2, 3),
1282 * and then reading from it using vstride=1, width=4, hstride=0.
1283 * These computations hold good for 4x multisampling as well.
1284 *
1285 * For 2x MSAA and SIMD16, we want to use the sequence (0, 1, 0, 1):
1286 * the first four slots are sample 0 of subspan 0; the next four
1287 * are sample 1 of subspan 0; the third group is sample 0 of
1288 * subspan 1, and finally sample 1 of subspan 1.
1289 */
1290 abld.exec_all()
1291 .AND(t1, fs_reg(retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UD)),
1292 fs_reg(0xc0));
1293 abld.exec_all().SHR(t1, t1, fs_reg(5));
1294
1295 /* This works for both SIMD8 and SIMD16 */
1296 abld.exec_all()
1297 .MOV(t2, brw_imm_v(key->persample_2x ? 0x1010 : 0x3210));
1298
1299 /* This special instruction takes care of setting vstride=1,
1300 * width=4, hstride=0 of t2 during an ADD instruction.
1301 */
1302 abld.emit(FS_OPCODE_SET_SAMPLE_ID, *reg, t1, t2);
1303 } else {
1304 /* As per GL_ARB_sample_shading specification:
1305 * "When rendering to a non-multisample buffer, or if multisample
1306 * rasterization is disabled, gl_SampleID will always be zero."
1307 */
1308 abld.MOV(*reg, fs_reg(0));
1309 }
1310
1311 return reg;
1312 }
1313
1314 void
1315 fs_visitor::resolve_source_modifiers(fs_reg *src)
1316 {
1317 if (!src->abs && !src->negate)
1318 return;
1319
1320 fs_reg temp = bld.vgrf(src->type);
1321 bld.MOV(temp, *src);
1322 *src = temp;
1323 }
1324
1325 void
1326 fs_visitor::emit_discard_jump()
1327 {
1328 assert(((brw_wm_prog_data*) this->prog_data)->uses_kill);
1329
1330 /* For performance, after a discard, jump to the end of the
1331 * shader if all relevant channels have been discarded.
1332 */
1333 fs_inst *discard_jump = bld.emit(FS_OPCODE_DISCARD_JUMP);
1334 discard_jump->flag_subreg = 1;
1335
1336 discard_jump->predicate = (dispatch_width == 8)
1337 ? BRW_PREDICATE_ALIGN1_ANY8H
1338 : BRW_PREDICATE_ALIGN1_ANY16H;
1339 discard_jump->predicate_inverse = true;
1340 }
1341
1342 void
1343 fs_visitor::assign_curb_setup()
1344 {
1345 if (dispatch_width == 8) {
1346 prog_data->dispatch_grf_start_reg = payload.num_regs;
1347 } else {
1348 if (stage == MESA_SHADER_FRAGMENT) {
1349 brw_wm_prog_data *prog_data = (brw_wm_prog_data*) this->prog_data;
1350 prog_data->dispatch_grf_start_reg_16 = payload.num_regs;
1351 } else if (stage == MESA_SHADER_COMPUTE) {
1352 brw_cs_prog_data *prog_data = (brw_cs_prog_data*) this->prog_data;
1353 prog_data->dispatch_grf_start_reg_16 = payload.num_regs;
1354 } else {
1355 unreachable("Unsupported shader type!");
1356 }
1357 }
1358
1359 prog_data->curb_read_length = ALIGN(stage_prog_data->nr_params, 8) / 8;
1360
1361 /* Map the offsets in the UNIFORM file to fixed HW regs. */
1362 foreach_block_and_inst(block, fs_inst, inst, cfg) {
1363 for (unsigned int i = 0; i < inst->sources; i++) {
1364 if (inst->src[i].file == UNIFORM) {
1365 int uniform_nr = inst->src[i].reg + inst->src[i].reg_offset;
1366 int constant_nr;
1367 if (uniform_nr >= 0 && uniform_nr < (int) uniforms) {
1368 constant_nr = push_constant_loc[uniform_nr];
1369 } else {
1370 /* Section 5.11 of the OpenGL 4.1 spec says:
1371 * "Out-of-bounds reads return undefined values, which include
1372 * values from other variables of the active program or zero."
1373 * Just return the first push constant.
1374 */
1375 constant_nr = 0;
1376 }
1377
1378 struct brw_reg brw_reg = brw_vec1_grf(payload.num_regs +
1379 constant_nr / 8,
1380 constant_nr % 8);
1381
1382 assert(inst->src[i].stride == 0);
1383 inst->src[i].file = HW_REG;
1384 inst->src[i].fixed_hw_reg = byte_offset(
1385 retype(brw_reg, inst->src[i].type),
1386 inst->src[i].subreg_offset);
1387 }
1388 }
1389 }
1390 }
1391
1392 void
1393 fs_visitor::calculate_urb_setup()
1394 {
1395 assert(stage == MESA_SHADER_FRAGMENT);
1396 brw_wm_prog_data *prog_data = (brw_wm_prog_data*) this->prog_data;
1397 brw_wm_prog_key *key = (brw_wm_prog_key*) this->key;
1398
1399 memset(prog_data->urb_setup, -1,
1400 sizeof(prog_data->urb_setup[0]) * VARYING_SLOT_MAX);
1401
1402 int urb_next = 0;
1403 /* Figure out where each of the incoming setup attributes lands. */
1404 if (devinfo->gen >= 6) {
1405 if (_mesa_bitcount_64(prog->InputsRead &
1406 BRW_FS_VARYING_INPUT_MASK) <= 16) {
1407 /* The SF/SBE pipeline stage can do arbitrary rearrangement of the
1408 * first 16 varying inputs, so we can put them wherever we want.
1409 * Just put them in order.
1410 *
1411 * This is useful because it means that (a) inputs not used by the
1412 * fragment shader won't take up valuable register space, and (b) we
1413 * won't have to recompile the fragment shader if it gets paired with
1414 * a different vertex (or geometry) shader.
1415 */
1416 for (unsigned int i = 0; i < VARYING_SLOT_MAX; i++) {
1417 if (prog->InputsRead & BRW_FS_VARYING_INPUT_MASK &
1418 BITFIELD64_BIT(i)) {
1419 prog_data->urb_setup[i] = urb_next++;
1420 }
1421 }
1422 } else {
1423 /* We have enough input varyings that the SF/SBE pipeline stage can't
1424 * arbitrarily rearrange them to suit our whim; we have to put them
1425 * in an order that matches the output of the previous pipeline stage
1426 * (geometry or vertex shader).
1427 */
1428 struct brw_vue_map prev_stage_vue_map;
1429 brw_compute_vue_map(devinfo, &prev_stage_vue_map,
1430 key->input_slots_valid);
1431 int first_slot = 2 * BRW_SF_URB_ENTRY_READ_OFFSET;
1432 assert(prev_stage_vue_map.num_slots <= first_slot + 32);
1433 for (int slot = first_slot; slot < prev_stage_vue_map.num_slots;
1434 slot++) {
1435 int varying = prev_stage_vue_map.slot_to_varying[slot];
1436 /* Note that varying == BRW_VARYING_SLOT_COUNT when a slot is
1437 * unused.
1438 */
1439 if (varying != BRW_VARYING_SLOT_COUNT &&
1440 (prog->InputsRead & BRW_FS_VARYING_INPUT_MASK &
1441 BITFIELD64_BIT(varying))) {
1442 prog_data->urb_setup[varying] = slot - first_slot;
1443 }
1444 }
1445 urb_next = prev_stage_vue_map.num_slots - first_slot;
1446 }
1447 } else {
1448 /* FINISHME: The sf doesn't map VS->FS inputs for us very well. */
1449 for (unsigned int i = 0; i < VARYING_SLOT_MAX; i++) {
1450 /* Point size is packed into the header, not as a general attribute */
1451 if (i == VARYING_SLOT_PSIZ)
1452 continue;
1453
1454 if (key->input_slots_valid & BITFIELD64_BIT(i)) {
1455 /* The back color slot is skipped when the front color is
1456 * also written to. In addition, some slots can be
1457 * written in the vertex shader and not read in the
1458 * fragment shader. So the register number must always be
1459 * incremented, mapped or not.
1460 */
1461 if (_mesa_varying_slot_in_fs((gl_varying_slot) i))
1462 prog_data->urb_setup[i] = urb_next;
1463 urb_next++;
1464 }
1465 }
1466
1467 /*
1468 * It's a FS only attribute, and we did interpolation for this attribute
1469 * in SF thread. So, count it here, too.
1470 *
1471 * See compile_sf_prog() for more info.
1472 */
1473 if (prog->InputsRead & BITFIELD64_BIT(VARYING_SLOT_PNTC))
1474 prog_data->urb_setup[VARYING_SLOT_PNTC] = urb_next++;
1475 }
1476
1477 prog_data->num_varying_inputs = urb_next;
1478 }
1479
1480 void
1481 fs_visitor::assign_urb_setup()
1482 {
1483 assert(stage == MESA_SHADER_FRAGMENT);
1484 brw_wm_prog_data *prog_data = (brw_wm_prog_data*) this->prog_data;
1485
1486 int urb_start = payload.num_regs + prog_data->base.curb_read_length;
1487
1488 /* Offset all the urb_setup[] index by the actual position of the
1489 * setup regs, now that the location of the constants has been chosen.
1490 */
1491 foreach_block_and_inst(block, fs_inst, inst, cfg) {
1492 if (inst->opcode == FS_OPCODE_LINTERP) {
1493 assert(inst->src[1].file == HW_REG);
1494 inst->src[1].fixed_hw_reg.nr += urb_start;
1495 }
1496
1497 if (inst->opcode == FS_OPCODE_CINTERP) {
1498 assert(inst->src[0].file == HW_REG);
1499 inst->src[0].fixed_hw_reg.nr += urb_start;
1500 }
1501 }
1502
1503 /* Each attribute is 4 setup channels, each of which is half a reg. */
1504 this->first_non_payload_grf =
1505 urb_start + prog_data->num_varying_inputs * 2;
1506 }
1507
1508 void
1509 fs_visitor::assign_vs_urb_setup()
1510 {
1511 brw_vs_prog_data *vs_prog_data = (brw_vs_prog_data *) prog_data;
1512 int grf, count, slot, channel, attr;
1513
1514 assert(stage == MESA_SHADER_VERTEX);
1515 count = _mesa_bitcount_64(vs_prog_data->inputs_read);
1516 if (vs_prog_data->uses_vertexid || vs_prog_data->uses_instanceid)
1517 count++;
1518
1519 /* Each attribute is 4 regs. */
1520 this->first_non_payload_grf =
1521 payload.num_regs + prog_data->curb_read_length + count * 4;
1522
1523 unsigned vue_entries =
1524 MAX2(count, vs_prog_data->base.vue_map.num_slots);
1525
1526 vs_prog_data->base.urb_entry_size = ALIGN(vue_entries, 4) / 4;
1527 vs_prog_data->base.urb_read_length = (count + 1) / 2;
1528
1529 assert(vs_prog_data->base.urb_read_length <= 15);
1530
1531 /* Rewrite all ATTR file references to the hw grf that they land in. */
1532 foreach_block_and_inst(block, fs_inst, inst, cfg) {
1533 for (int i = 0; i < inst->sources; i++) {
1534 if (inst->src[i].file == ATTR) {
1535
1536 if (inst->src[i].reg == VERT_ATTRIB_MAX) {
1537 slot = count - 1;
1538 } else {
1539 /* Attributes come in in a contiguous block, ordered by their
1540 * gl_vert_attrib value. That means we can compute the slot
1541 * number for an attribute by masking out the enabled
1542 * attributes before it and counting the bits.
1543 */
1544 attr = inst->src[i].reg + inst->src[i].reg_offset / 4;
1545 slot = _mesa_bitcount_64(vs_prog_data->inputs_read &
1546 BITFIELD64_MASK(attr));
1547 }
1548
1549 channel = inst->src[i].reg_offset & 3;
1550
1551 grf = payload.num_regs +
1552 prog_data->curb_read_length +
1553 slot * 4 + channel;
1554
1555 inst->src[i].file = HW_REG;
1556 inst->src[i].fixed_hw_reg =
1557 retype(brw_vec8_grf(grf, 0), inst->src[i].type);
1558 }
1559 }
1560 }
1561 }
1562
1563 /**
1564 * Split large virtual GRFs into separate components if we can.
1565 *
1566 * This is mostly duplicated with what brw_fs_vector_splitting does,
1567 * but that's really conservative because it's afraid of doing
1568 * splitting that doesn't result in real progress after the rest of
1569 * the optimization phases, which would cause infinite looping in
1570 * optimization. We can do it once here, safely. This also has the
1571 * opportunity to split interpolated values, or maybe even uniforms,
1572 * which we don't have at the IR level.
1573 *
1574 * We want to split, because virtual GRFs are what we register
1575 * allocate and spill (due to contiguousness requirements for some
1576 * instructions), and they're what we naturally generate in the
1577 * codegen process, but most virtual GRFs don't actually need to be
1578 * contiguous sets of GRFs. If we split, we'll end up with reduced
1579 * live intervals and better dead code elimination and coalescing.
1580 */
1581 void
1582 fs_visitor::split_virtual_grfs()
1583 {
1584 int num_vars = this->alloc.count;
1585
1586 /* Count the total number of registers */
1587 int reg_count = 0;
1588 int vgrf_to_reg[num_vars];
1589 for (int i = 0; i < num_vars; i++) {
1590 vgrf_to_reg[i] = reg_count;
1591 reg_count += alloc.sizes[i];
1592 }
1593
1594 /* An array of "split points". For each register slot, this indicates
1595 * if this slot can be separated from the previous slot. Every time an
1596 * instruction uses multiple elements of a register (as a source or
1597 * destination), we mark the used slots as inseparable. Then we go
1598 * through and split the registers into the smallest pieces we can.
1599 */
1600 bool split_points[reg_count];
1601 memset(split_points, 0, sizeof(split_points));
1602
1603 /* Mark all used registers as fully splittable */
1604 foreach_block_and_inst(block, fs_inst, inst, cfg) {
1605 if (inst->dst.file == GRF) {
1606 int reg = vgrf_to_reg[inst->dst.reg];
1607 for (unsigned j = 1; j < this->alloc.sizes[inst->dst.reg]; j++)
1608 split_points[reg + j] = true;
1609 }
1610
1611 for (int i = 0; i < inst->sources; i++) {
1612 if (inst->src[i].file == GRF) {
1613 int reg = vgrf_to_reg[inst->src[i].reg];
1614 for (unsigned j = 1; j < this->alloc.sizes[inst->src[i].reg]; j++)
1615 split_points[reg + j] = true;
1616 }
1617 }
1618 }
1619
1620 foreach_block_and_inst(block, fs_inst, inst, cfg) {
1621 if (inst->dst.file == GRF) {
1622 int reg = vgrf_to_reg[inst->dst.reg] + inst->dst.reg_offset;
1623 for (int j = 1; j < inst->regs_written; j++)
1624 split_points[reg + j] = false;
1625 }
1626 for (int i = 0; i < inst->sources; i++) {
1627 if (inst->src[i].file == GRF) {
1628 int reg = vgrf_to_reg[inst->src[i].reg] + inst->src[i].reg_offset;
1629 for (int j = 1; j < inst->regs_read(i); j++)
1630 split_points[reg + j] = false;
1631 }
1632 }
1633 }
1634
1635 int new_virtual_grf[reg_count];
1636 int new_reg_offset[reg_count];
1637
1638 int reg = 0;
1639 for (int i = 0; i < num_vars; i++) {
1640 /* The first one should always be 0 as a quick sanity check. */
1641 assert(split_points[reg] == false);
1642
1643 /* j = 0 case */
1644 new_reg_offset[reg] = 0;
1645 reg++;
1646 int offset = 1;
1647
1648 /* j > 0 case */
1649 for (unsigned j = 1; j < alloc.sizes[i]; j++) {
1650 /* If this is a split point, reset the offset to 0 and allocate a
1651 * new virtual GRF for the previous offset many registers
1652 */
1653 if (split_points[reg]) {
1654 assert(offset <= MAX_VGRF_SIZE);
1655 int grf = alloc.allocate(offset);
1656 for (int k = reg - offset; k < reg; k++)
1657 new_virtual_grf[k] = grf;
1658 offset = 0;
1659 }
1660 new_reg_offset[reg] = offset;
1661 offset++;
1662 reg++;
1663 }
1664
1665 /* The last one gets the original register number */
1666 assert(offset <= MAX_VGRF_SIZE);
1667 alloc.sizes[i] = offset;
1668 for (int k = reg - offset; k < reg; k++)
1669 new_virtual_grf[k] = i;
1670 }
1671 assert(reg == reg_count);
1672
1673 foreach_block_and_inst(block, fs_inst, inst, cfg) {
1674 if (inst->dst.file == GRF) {
1675 reg = vgrf_to_reg[inst->dst.reg] + inst->dst.reg_offset;
1676 inst->dst.reg = new_virtual_grf[reg];
1677 inst->dst.reg_offset = new_reg_offset[reg];
1678 assert((unsigned)new_reg_offset[reg] < alloc.sizes[new_virtual_grf[reg]]);
1679 }
1680 for (int i = 0; i < inst->sources; i++) {
1681 if (inst->src[i].file == GRF) {
1682 reg = vgrf_to_reg[inst->src[i].reg] + inst->src[i].reg_offset;
1683 inst->src[i].reg = new_virtual_grf[reg];
1684 inst->src[i].reg_offset = new_reg_offset[reg];
1685 assert((unsigned)new_reg_offset[reg] < alloc.sizes[new_virtual_grf[reg]]);
1686 }
1687 }
1688 }
1689 invalidate_live_intervals();
1690 }
1691
1692 /**
1693 * Remove unused virtual GRFs and compact the virtual_grf_* arrays.
1694 *
1695 * During code generation, we create tons of temporary variables, many of
1696 * which get immediately killed and are never used again. Yet, in later
1697 * optimization and analysis passes, such as compute_live_intervals, we need
1698 * to loop over all the virtual GRFs. Compacting them can save a lot of
1699 * overhead.
1700 */
1701 bool
1702 fs_visitor::compact_virtual_grfs()
1703 {
1704 bool progress = false;
1705 int remap_table[this->alloc.count];
1706 memset(remap_table, -1, sizeof(remap_table));
1707
1708 /* Mark which virtual GRFs are used. */
1709 foreach_block_and_inst(block, const fs_inst, inst, cfg) {
1710 if (inst->dst.file == GRF)
1711 remap_table[inst->dst.reg] = 0;
1712
1713 for (int i = 0; i < inst->sources; i++) {
1714 if (inst->src[i].file == GRF)
1715 remap_table[inst->src[i].reg] = 0;
1716 }
1717 }
1718
1719 /* Compact the GRF arrays. */
1720 int new_index = 0;
1721 for (unsigned i = 0; i < this->alloc.count; i++) {
1722 if (remap_table[i] == -1) {
1723 /* We just found an unused register. This means that we are
1724 * actually going to compact something.
1725 */
1726 progress = true;
1727 } else {
1728 remap_table[i] = new_index;
1729 alloc.sizes[new_index] = alloc.sizes[i];
1730 invalidate_live_intervals();
1731 ++new_index;
1732 }
1733 }
1734
1735 this->alloc.count = new_index;
1736
1737 /* Patch all the instructions to use the newly renumbered registers */
1738 foreach_block_and_inst(block, fs_inst, inst, cfg) {
1739 if (inst->dst.file == GRF)
1740 inst->dst.reg = remap_table[inst->dst.reg];
1741
1742 for (int i = 0; i < inst->sources; i++) {
1743 if (inst->src[i].file == GRF)
1744 inst->src[i].reg = remap_table[inst->src[i].reg];
1745 }
1746 }
1747
1748 /* Patch all the references to delta_xy, since they're used in register
1749 * allocation. If they're unused, switch them to BAD_FILE so we don't
1750 * think some random VGRF is delta_xy.
1751 */
1752 for (unsigned i = 0; i < ARRAY_SIZE(delta_xy); i++) {
1753 if (delta_xy[i].file == GRF) {
1754 if (remap_table[delta_xy[i].reg] != -1) {
1755 delta_xy[i].reg = remap_table[delta_xy[i].reg];
1756 } else {
1757 delta_xy[i].file = BAD_FILE;
1758 }
1759 }
1760 }
1761
1762 return progress;
1763 }
1764
1765 /*
1766 * Implements array access of uniforms by inserting a
1767 * PULL_CONSTANT_LOAD instruction.
1768 *
1769 * Unlike temporary GRF array access (where we don't support it due to
1770 * the difficulty of doing relative addressing on instruction
1771 * destinations), we could potentially do array access of uniforms
1772 * that were loaded in GRF space as push constants. In real-world
1773 * usage we've seen, though, the arrays being used are always larger
1774 * than we could load as push constants, so just always move all
1775 * uniform array access out to a pull constant buffer.
1776 */
1777 void
1778 fs_visitor::move_uniform_array_access_to_pull_constants()
1779 {
1780 if (dispatch_width != 8)
1781 return;
1782
1783 pull_constant_loc = ralloc_array(mem_ctx, int, uniforms);
1784 memset(pull_constant_loc, -1, sizeof(pull_constant_loc[0]) * uniforms);
1785
1786 /* Walk through and find array access of uniforms. Put a copy of that
1787 * uniform in the pull constant buffer.
1788 *
1789 * Note that we don't move constant-indexed accesses to arrays. No
1790 * testing has been done of the performance impact of this choice.
1791 */
1792 foreach_block_and_inst_safe(block, fs_inst, inst, cfg) {
1793 for (int i = 0 ; i < inst->sources; i++) {
1794 if (inst->src[i].file != UNIFORM || !inst->src[i].reladdr)
1795 continue;
1796
1797 int uniform = inst->src[i].reg;
1798
1799 /* If this array isn't already present in the pull constant buffer,
1800 * add it.
1801 */
1802 if (pull_constant_loc[uniform] == -1) {
1803 const gl_constant_value **values = &stage_prog_data->param[uniform];
1804
1805 assert(param_size[uniform]);
1806
1807 for (int j = 0; j < param_size[uniform]; j++) {
1808 pull_constant_loc[uniform + j] = stage_prog_data->nr_pull_params;
1809
1810 stage_prog_data->pull_param[stage_prog_data->nr_pull_params++] =
1811 values[j];
1812 }
1813 }
1814 }
1815 }
1816 }
1817
1818 /**
1819 * Assign UNIFORM file registers to either push constants or pull constants.
1820 *
1821 * We allow a fragment shader to have more than the specified minimum
1822 * maximum number of fragment shader uniform components (64). If
1823 * there are too many of these, they'd fill up all of register space.
1824 * So, this will push some of them out to the pull constant buffer and
1825 * update the program to load them.
1826 */
1827 void
1828 fs_visitor::assign_constant_locations()
1829 {
1830 /* Only the first compile (SIMD8 mode) gets to decide on locations. */
1831 if (dispatch_width != 8)
1832 return;
1833
1834 /* Find which UNIFORM registers are still in use. */
1835 bool is_live[uniforms];
1836 for (unsigned int i = 0; i < uniforms; i++) {
1837 is_live[i] = false;
1838 }
1839
1840 foreach_block_and_inst(block, fs_inst, inst, cfg) {
1841 for (int i = 0; i < inst->sources; i++) {
1842 if (inst->src[i].file != UNIFORM)
1843 continue;
1844
1845 int constant_nr = inst->src[i].reg + inst->src[i].reg_offset;
1846 if (constant_nr >= 0 && constant_nr < (int) uniforms)
1847 is_live[constant_nr] = true;
1848 }
1849 }
1850
1851 /* Only allow 16 registers (128 uniform components) as push constants.
1852 *
1853 * Just demote the end of the list. We could probably do better
1854 * here, demoting things that are rarely used in the program first.
1855 *
1856 * If changing this value, note the limitation about total_regs in
1857 * brw_curbe.c.
1858 */
1859 unsigned int max_push_components = 16 * 8;
1860 unsigned int num_push_constants = 0;
1861
1862 push_constant_loc = ralloc_array(mem_ctx, int, uniforms);
1863
1864 for (unsigned int i = 0; i < uniforms; i++) {
1865 if (!is_live[i] || pull_constant_loc[i] != -1) {
1866 /* This UNIFORM register is either dead, or has already been demoted
1867 * to a pull const. Mark it as no longer living in the param[] array.
1868 */
1869 push_constant_loc[i] = -1;
1870 continue;
1871 }
1872
1873 if (num_push_constants < max_push_components) {
1874 /* Retain as a push constant. Record the location in the params[]
1875 * array.
1876 */
1877 push_constant_loc[i] = num_push_constants++;
1878 } else {
1879 /* Demote to a pull constant. */
1880 push_constant_loc[i] = -1;
1881
1882 int pull_index = stage_prog_data->nr_pull_params++;
1883 stage_prog_data->pull_param[pull_index] = stage_prog_data->param[i];
1884 pull_constant_loc[i] = pull_index;
1885 }
1886 }
1887
1888 stage_prog_data->nr_params = num_push_constants;
1889
1890 /* Up until now, the param[] array has been indexed by reg + reg_offset
1891 * of UNIFORM registers. Condense it to only contain the uniforms we
1892 * chose to upload as push constants.
1893 */
1894 for (unsigned int i = 0; i < uniforms; i++) {
1895 int remapped = push_constant_loc[i];
1896
1897 if (remapped == -1)
1898 continue;
1899
1900 assert(remapped <= (int)i);
1901 stage_prog_data->param[remapped] = stage_prog_data->param[i];
1902 }
1903 }
1904
1905 /**
1906 * Replace UNIFORM register file access with either UNIFORM_PULL_CONSTANT_LOAD
1907 * or VARYING_PULL_CONSTANT_LOAD instructions which load values into VGRFs.
1908 */
1909 void
1910 fs_visitor::demote_pull_constants()
1911 {
1912 foreach_block_and_inst (block, fs_inst, inst, cfg) {
1913 for (int i = 0; i < inst->sources; i++) {
1914 if (inst->src[i].file != UNIFORM)
1915 continue;
1916
1917 int pull_index;
1918 unsigned location = inst->src[i].reg + inst->src[i].reg_offset;
1919 if (location >= uniforms) /* Out of bounds access */
1920 pull_index = -1;
1921 else
1922 pull_index = pull_constant_loc[location];
1923
1924 if (pull_index == -1)
1925 continue;
1926
1927 /* Set up the annotation tracking for new generated instructions. */
1928 const fs_builder ibld(this, block, inst);
1929 fs_reg surf_index(stage_prog_data->binding_table.pull_constants_start);
1930 fs_reg dst = vgrf(glsl_type::float_type);
1931
1932 assert(inst->src[i].stride == 0);
1933
1934 /* Generate a pull load into dst. */
1935 if (inst->src[i].reladdr) {
1936 VARYING_PULL_CONSTANT_LOAD(ibld, dst,
1937 surf_index,
1938 *inst->src[i].reladdr,
1939 pull_index);
1940 inst->src[i].reladdr = NULL;
1941 inst->src[i].stride = 1;
1942 } else {
1943 const fs_builder ubld = ibld.exec_all().group(8, 0);
1944 fs_reg offset = fs_reg((unsigned)(pull_index * 4) & ~15);
1945 ubld.emit(FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD,
1946 dst, surf_index, offset);
1947 inst->src[i].set_smear(pull_index & 3);
1948 }
1949
1950 /* Rewrite the instruction to use the temporary VGRF. */
1951 inst->src[i].file = GRF;
1952 inst->src[i].reg = dst.reg;
1953 inst->src[i].reg_offset = 0;
1954 }
1955 }
1956 invalidate_live_intervals();
1957 }
1958
1959 bool
1960 fs_visitor::opt_algebraic()
1961 {
1962 bool progress = false;
1963
1964 foreach_block_and_inst(block, fs_inst, inst, cfg) {
1965 switch (inst->opcode) {
1966 case BRW_OPCODE_MOV:
1967 if (inst->src[0].file != IMM)
1968 break;
1969
1970 if (inst->saturate) {
1971 if (inst->dst.type != inst->src[0].type)
1972 assert(!"unimplemented: saturate mixed types");
1973
1974 if (brw_saturate_immediate(inst->dst.type,
1975 &inst->src[0].fixed_hw_reg)) {
1976 inst->saturate = false;
1977 progress = true;
1978 }
1979 }
1980 break;
1981
1982 case BRW_OPCODE_MUL:
1983 if (inst->src[1].file != IMM)
1984 continue;
1985
1986 /* a * 1.0 = a */
1987 if (inst->src[1].is_one()) {
1988 inst->opcode = BRW_OPCODE_MOV;
1989 inst->src[1] = reg_undef;
1990 progress = true;
1991 break;
1992 }
1993
1994 /* a * -1.0 = -a */
1995 if (inst->src[1].is_negative_one()) {
1996 inst->opcode = BRW_OPCODE_MOV;
1997 inst->src[0].negate = !inst->src[0].negate;
1998 inst->src[1] = reg_undef;
1999 progress = true;
2000 break;
2001 }
2002
2003 /* a * 0.0 = 0.0 */
2004 if (inst->src[1].is_zero()) {
2005 inst->opcode = BRW_OPCODE_MOV;
2006 inst->src[0] = inst->src[1];
2007 inst->src[1] = reg_undef;
2008 progress = true;
2009 break;
2010 }
2011
2012 if (inst->src[0].file == IMM) {
2013 assert(inst->src[0].type == BRW_REGISTER_TYPE_F);
2014 inst->opcode = BRW_OPCODE_MOV;
2015 inst->src[0].fixed_hw_reg.dw1.f *= inst->src[1].fixed_hw_reg.dw1.f;
2016 inst->src[1] = reg_undef;
2017 progress = true;
2018 break;
2019 }
2020 break;
2021 case BRW_OPCODE_ADD:
2022 if (inst->src[1].file != IMM)
2023 continue;
2024
2025 /* a + 0.0 = a */
2026 if (inst->src[1].is_zero()) {
2027 inst->opcode = BRW_OPCODE_MOV;
2028 inst->src[1] = reg_undef;
2029 progress = true;
2030 break;
2031 }
2032
2033 if (inst->src[0].file == IMM) {
2034 assert(inst->src[0].type == BRW_REGISTER_TYPE_F);
2035 inst->opcode = BRW_OPCODE_MOV;
2036 inst->src[0].fixed_hw_reg.dw1.f += inst->src[1].fixed_hw_reg.dw1.f;
2037 inst->src[1] = reg_undef;
2038 progress = true;
2039 break;
2040 }
2041 break;
2042 case BRW_OPCODE_OR:
2043 if (inst->src[0].equals(inst->src[1])) {
2044 inst->opcode = BRW_OPCODE_MOV;
2045 inst->src[1] = reg_undef;
2046 progress = true;
2047 break;
2048 }
2049 break;
2050 case BRW_OPCODE_LRP:
2051 if (inst->src[1].equals(inst->src[2])) {
2052 inst->opcode = BRW_OPCODE_MOV;
2053 inst->src[0] = inst->src[1];
2054 inst->src[1] = reg_undef;
2055 inst->src[2] = reg_undef;
2056 progress = true;
2057 break;
2058 }
2059 break;
2060 case BRW_OPCODE_CMP:
2061 if (inst->conditional_mod == BRW_CONDITIONAL_GE &&
2062 inst->src[0].abs &&
2063 inst->src[0].negate &&
2064 inst->src[1].is_zero()) {
2065 inst->src[0].abs = false;
2066 inst->src[0].negate = false;
2067 inst->conditional_mod = BRW_CONDITIONAL_Z;
2068 progress = true;
2069 break;
2070 }
2071 break;
2072 case BRW_OPCODE_SEL:
2073 if (inst->src[0].equals(inst->src[1])) {
2074 inst->opcode = BRW_OPCODE_MOV;
2075 inst->src[1] = reg_undef;
2076 inst->predicate = BRW_PREDICATE_NONE;
2077 inst->predicate_inverse = false;
2078 progress = true;
2079 } else if (inst->saturate && inst->src[1].file == IMM) {
2080 switch (inst->conditional_mod) {
2081 case BRW_CONDITIONAL_LE:
2082 case BRW_CONDITIONAL_L:
2083 switch (inst->src[1].type) {
2084 case BRW_REGISTER_TYPE_F:
2085 if (inst->src[1].fixed_hw_reg.dw1.f >= 1.0f) {
2086 inst->opcode = BRW_OPCODE_MOV;
2087 inst->src[1] = reg_undef;
2088 inst->conditional_mod = BRW_CONDITIONAL_NONE;
2089 progress = true;
2090 }
2091 break;
2092 default:
2093 break;
2094 }
2095 break;
2096 case BRW_CONDITIONAL_GE:
2097 case BRW_CONDITIONAL_G:
2098 switch (inst->src[1].type) {
2099 case BRW_REGISTER_TYPE_F:
2100 if (inst->src[1].fixed_hw_reg.dw1.f <= 0.0f) {
2101 inst->opcode = BRW_OPCODE_MOV;
2102 inst->src[1] = reg_undef;
2103 inst->conditional_mod = BRW_CONDITIONAL_NONE;
2104 progress = true;
2105 }
2106 break;
2107 default:
2108 break;
2109 }
2110 default:
2111 break;
2112 }
2113 }
2114 break;
2115 case BRW_OPCODE_MAD:
2116 if (inst->src[1].is_zero() || inst->src[2].is_zero()) {
2117 inst->opcode = BRW_OPCODE_MOV;
2118 inst->src[1] = reg_undef;
2119 inst->src[2] = reg_undef;
2120 progress = true;
2121 } else if (inst->src[0].is_zero()) {
2122 inst->opcode = BRW_OPCODE_MUL;
2123 inst->src[0] = inst->src[2];
2124 inst->src[2] = reg_undef;
2125 progress = true;
2126 } else if (inst->src[1].is_one()) {
2127 inst->opcode = BRW_OPCODE_ADD;
2128 inst->src[1] = inst->src[2];
2129 inst->src[2] = reg_undef;
2130 progress = true;
2131 } else if (inst->src[2].is_one()) {
2132 inst->opcode = BRW_OPCODE_ADD;
2133 inst->src[2] = reg_undef;
2134 progress = true;
2135 } else if (inst->src[1].file == IMM && inst->src[2].file == IMM) {
2136 inst->opcode = BRW_OPCODE_ADD;
2137 inst->src[1].fixed_hw_reg.dw1.f *= inst->src[2].fixed_hw_reg.dw1.f;
2138 inst->src[2] = reg_undef;
2139 progress = true;
2140 }
2141 break;
2142 case SHADER_OPCODE_RCP: {
2143 fs_inst *prev = (fs_inst *)inst->prev;
2144 if (prev->opcode == SHADER_OPCODE_SQRT) {
2145 if (inst->src[0].equals(prev->dst)) {
2146 inst->opcode = SHADER_OPCODE_RSQ;
2147 inst->src[0] = prev->src[0];
2148 progress = true;
2149 }
2150 }
2151 break;
2152 }
2153 case SHADER_OPCODE_BROADCAST:
2154 if (is_uniform(inst->src[0])) {
2155 inst->opcode = BRW_OPCODE_MOV;
2156 inst->sources = 1;
2157 inst->force_writemask_all = true;
2158 progress = true;
2159 } else if (inst->src[1].file == IMM) {
2160 inst->opcode = BRW_OPCODE_MOV;
2161 inst->src[0] = component(inst->src[0],
2162 inst->src[1].fixed_hw_reg.dw1.ud);
2163 inst->sources = 1;
2164 inst->force_writemask_all = true;
2165 progress = true;
2166 }
2167 break;
2168
2169 default:
2170 break;
2171 }
2172
2173 /* Swap if src[0] is immediate. */
2174 if (progress && inst->is_commutative()) {
2175 if (inst->src[0].file == IMM) {
2176 fs_reg tmp = inst->src[1];
2177 inst->src[1] = inst->src[0];
2178 inst->src[0] = tmp;
2179 }
2180 }
2181 }
2182 return progress;
2183 }
2184
2185 /**
2186 * Optimize sample messages that have constant zero values for the trailing
2187 * texture coordinates. We can just reduce the message length for these
2188 * instructions instead of reserving a register for it. Trailing parameters
2189 * that aren't sent default to zero anyway. This will cause the dead code
2190 * eliminator to remove the MOV instruction that would otherwise be emitted to
2191 * set up the zero value.
2192 */
2193 bool
2194 fs_visitor::opt_zero_samples()
2195 {
2196 /* Gen4 infers the texturing opcode based on the message length so we can't
2197 * change it.
2198 */
2199 if (devinfo->gen < 5)
2200 return false;
2201
2202 bool progress = false;
2203
2204 foreach_block_and_inst(block, fs_inst, inst, cfg) {
2205 if (!inst->is_tex())
2206 continue;
2207
2208 fs_inst *load_payload = (fs_inst *) inst->prev;
2209
2210 if (load_payload->is_head_sentinel() ||
2211 load_payload->opcode != SHADER_OPCODE_LOAD_PAYLOAD)
2212 continue;
2213
2214 /* We don't want to remove the message header or the first parameter.
2215 * Removing the first parameter is not allowed, see the Haswell PRM
2216 * volume 7, page 149:
2217 *
2218 * "Parameter 0 is required except for the sampleinfo message, which
2219 * has no parameter 0"
2220 */
2221 while (inst->mlen > inst->header_size + inst->exec_size / 8 &&
2222 load_payload->src[(inst->mlen - inst->header_size) /
2223 (inst->exec_size / 8) +
2224 inst->header_size - 1].is_zero()) {
2225 inst->mlen -= inst->exec_size / 8;
2226 progress = true;
2227 }
2228 }
2229
2230 if (progress)
2231 invalidate_live_intervals();
2232
2233 return progress;
2234 }
2235
2236 /**
2237 * Optimize sample messages which are followed by the final RT write.
2238 *
2239 * CHV, and GEN9+ can mark a texturing SEND instruction with EOT to have its
2240 * results sent directly to the framebuffer, bypassing the EU. Recognize the
2241 * final texturing results copied to the framebuffer write payload and modify
2242 * them to write to the framebuffer directly.
2243 */
2244 bool
2245 fs_visitor::opt_sampler_eot()
2246 {
2247 brw_wm_prog_key *key = (brw_wm_prog_key*) this->key;
2248
2249 if (stage != MESA_SHADER_FRAGMENT)
2250 return false;
2251
2252 if (devinfo->gen < 9 && !devinfo->is_cherryview)
2253 return false;
2254
2255 /* FINISHME: It should be possible to implement this optimization when there
2256 * are multiple drawbuffers.
2257 */
2258 if (key->nr_color_regions != 1)
2259 return false;
2260
2261 /* Look for a texturing instruction immediately before the final FB_WRITE. */
2262 bblock_t *block = cfg->blocks[cfg->num_blocks - 1];
2263 fs_inst *fb_write = (fs_inst *)block->end();
2264 assert(fb_write->eot);
2265 assert(fb_write->opcode == FS_OPCODE_FB_WRITE);
2266
2267 fs_inst *tex_inst = (fs_inst *) fb_write->prev;
2268
2269 /* There wasn't one; nothing to do. */
2270 if (unlikely(tex_inst->is_head_sentinel()) || !tex_inst->is_tex())
2271 return false;
2272
2273 /* This optimisation doesn't seem to work for textureGather for some
2274 * reason. I can't find any documentation or known workarounds to indicate
2275 * that this is expected, but considering that it is probably pretty
2276 * unlikely that a shader would directly write out the results from
2277 * textureGather we might as well just disable it.
2278 */
2279 if (tex_inst->opcode == SHADER_OPCODE_TG4 ||
2280 tex_inst->opcode == SHADER_OPCODE_TG4_OFFSET)
2281 return false;
2282
2283 /* If there's no header present, we need to munge the LOAD_PAYLOAD as well.
2284 * It's very likely to be the previous instruction.
2285 */
2286 fs_inst *load_payload = (fs_inst *) tex_inst->prev;
2287 if (load_payload->is_head_sentinel() ||
2288 load_payload->opcode != SHADER_OPCODE_LOAD_PAYLOAD)
2289 return false;
2290
2291 assert(!tex_inst->eot); /* We can't get here twice */
2292 assert((tex_inst->offset & (0xff << 24)) == 0);
2293
2294 const fs_builder ibld(this, block, tex_inst);
2295
2296 tex_inst->offset |= fb_write->target << 24;
2297 tex_inst->eot = true;
2298 tex_inst->dst = ibld.null_reg_ud();
2299 fb_write->remove(cfg->blocks[cfg->num_blocks - 1]);
2300
2301 /* If a header is present, marking the eot is sufficient. Otherwise, we need
2302 * to create a new LOAD_PAYLOAD command with the same sources and a space
2303 * saved for the header. Using a new destination register not only makes sure
2304 * we have enough space, but it will make sure the dead code eliminator kills
2305 * the instruction that this will replace.
2306 */
2307 if (tex_inst->header_size != 0)
2308 return true;
2309
2310 fs_reg send_header = ibld.vgrf(BRW_REGISTER_TYPE_F,
2311 load_payload->sources + 1);
2312 fs_reg *new_sources =
2313 ralloc_array(mem_ctx, fs_reg, load_payload->sources + 1);
2314
2315 new_sources[0] = fs_reg();
2316 for (int i = 0; i < load_payload->sources; i++)
2317 new_sources[i+1] = load_payload->src[i];
2318
2319 /* The LOAD_PAYLOAD helper seems like the obvious choice here. However, it
2320 * requires a lot of information about the sources to appropriately figure
2321 * out the number of registers needed to be used. Given this stage in our
2322 * optimization, we may not have the appropriate GRFs required by
2323 * LOAD_PAYLOAD at this point (copy propagation). Therefore, we need to
2324 * manually emit the instruction.
2325 */
2326 fs_inst *new_load_payload = new(mem_ctx) fs_inst(SHADER_OPCODE_LOAD_PAYLOAD,
2327 load_payload->exec_size,
2328 send_header,
2329 new_sources,
2330 load_payload->sources + 1);
2331
2332 new_load_payload->regs_written = load_payload->regs_written + 1;
2333 new_load_payload->header_size = 1;
2334 tex_inst->mlen++;
2335 tex_inst->header_size = 1;
2336 tex_inst->insert_before(cfg->blocks[cfg->num_blocks - 1], new_load_payload);
2337 tex_inst->src[0] = send_header;
2338
2339 return true;
2340 }
2341
2342 bool
2343 fs_visitor::opt_register_renaming()
2344 {
2345 bool progress = false;
2346 int depth = 0;
2347
2348 int remap[alloc.count];
2349 memset(remap, -1, sizeof(int) * alloc.count);
2350
2351 foreach_block_and_inst(block, fs_inst, inst, cfg) {
2352 if (inst->opcode == BRW_OPCODE_IF || inst->opcode == BRW_OPCODE_DO) {
2353 depth++;
2354 } else if (inst->opcode == BRW_OPCODE_ENDIF ||
2355 inst->opcode == BRW_OPCODE_WHILE) {
2356 depth--;
2357 }
2358
2359 /* Rewrite instruction sources. */
2360 for (int i = 0; i < inst->sources; i++) {
2361 if (inst->src[i].file == GRF &&
2362 remap[inst->src[i].reg] != -1 &&
2363 remap[inst->src[i].reg] != inst->src[i].reg) {
2364 inst->src[i].reg = remap[inst->src[i].reg];
2365 progress = true;
2366 }
2367 }
2368
2369 const int dst = inst->dst.reg;
2370
2371 if (depth == 0 &&
2372 inst->dst.file == GRF &&
2373 alloc.sizes[inst->dst.reg] == inst->exec_size / 8 &&
2374 !inst->is_partial_write()) {
2375 if (remap[dst] == -1) {
2376 remap[dst] = dst;
2377 } else {
2378 remap[dst] = alloc.allocate(inst->exec_size / 8);
2379 inst->dst.reg = remap[dst];
2380 progress = true;
2381 }
2382 } else if (inst->dst.file == GRF &&
2383 remap[dst] != -1 &&
2384 remap[dst] != dst) {
2385 inst->dst.reg = remap[dst];
2386 progress = true;
2387 }
2388 }
2389
2390 if (progress) {
2391 invalidate_live_intervals();
2392
2393 for (unsigned i = 0; i < ARRAY_SIZE(delta_xy); i++) {
2394 if (delta_xy[i].file == GRF && remap[delta_xy[i].reg] != -1) {
2395 delta_xy[i].reg = remap[delta_xy[i].reg];
2396 }
2397 }
2398 }
2399
2400 return progress;
2401 }
2402
2403 /**
2404 * Remove redundant or useless discard jumps.
2405 *
2406 * For example, we can eliminate jumps in the following sequence:
2407 *
2408 * discard-jump (redundant with the next jump)
2409 * discard-jump (useless; jumps to the next instruction)
2410 * placeholder-halt
2411 */
2412 bool
2413 fs_visitor::opt_redundant_discard_jumps()
2414 {
2415 bool progress = false;
2416
2417 bblock_t *last_bblock = cfg->blocks[cfg->num_blocks - 1];
2418
2419 fs_inst *placeholder_halt = NULL;
2420 foreach_inst_in_block_reverse(fs_inst, inst, last_bblock) {
2421 if (inst->opcode == FS_OPCODE_PLACEHOLDER_HALT) {
2422 placeholder_halt = inst;
2423 break;
2424 }
2425 }
2426
2427 if (!placeholder_halt)
2428 return false;
2429
2430 /* Delete any HALTs immediately before the placeholder halt. */
2431 for (fs_inst *prev = (fs_inst *) placeholder_halt->prev;
2432 !prev->is_head_sentinel() && prev->opcode == FS_OPCODE_DISCARD_JUMP;
2433 prev = (fs_inst *) placeholder_halt->prev) {
2434 prev->remove(last_bblock);
2435 progress = true;
2436 }
2437
2438 if (progress)
2439 invalidate_live_intervals();
2440
2441 return progress;
2442 }
2443
2444 bool
2445 fs_visitor::compute_to_mrf()
2446 {
2447 bool progress = false;
2448 int next_ip = 0;
2449
2450 /* No MRFs on Gen >= 7. */
2451 if (devinfo->gen >= 7)
2452 return false;
2453
2454 calculate_live_intervals();
2455
2456 foreach_block_and_inst_safe(block, fs_inst, inst, cfg) {
2457 int ip = next_ip;
2458 next_ip++;
2459
2460 if (inst->opcode != BRW_OPCODE_MOV ||
2461 inst->is_partial_write() ||
2462 inst->dst.file != MRF || inst->src[0].file != GRF ||
2463 inst->dst.type != inst->src[0].type ||
2464 inst->src[0].abs || inst->src[0].negate ||
2465 !inst->src[0].is_contiguous() ||
2466 inst->src[0].subreg_offset)
2467 continue;
2468
2469 /* Work out which hardware MRF registers are written by this
2470 * instruction.
2471 */
2472 int mrf_low = inst->dst.reg & ~BRW_MRF_COMPR4;
2473 int mrf_high;
2474 if (inst->dst.reg & BRW_MRF_COMPR4) {
2475 mrf_high = mrf_low + 4;
2476 } else if (inst->exec_size == 16) {
2477 mrf_high = mrf_low + 1;
2478 } else {
2479 mrf_high = mrf_low;
2480 }
2481
2482 /* Can't compute-to-MRF this GRF if someone else was going to
2483 * read it later.
2484 */
2485 if (this->virtual_grf_end[inst->src[0].reg] > ip)
2486 continue;
2487
2488 /* Found a move of a GRF to a MRF. Let's see if we can go
2489 * rewrite the thing that made this GRF to write into the MRF.
2490 */
2491 foreach_inst_in_block_reverse_starting_from(fs_inst, scan_inst, inst, block) {
2492 if (scan_inst->dst.file == GRF &&
2493 scan_inst->dst.reg == inst->src[0].reg) {
2494 /* Found the last thing to write our reg we want to turn
2495 * into a compute-to-MRF.
2496 */
2497
2498 /* If this one instruction didn't populate all the
2499 * channels, bail. We might be able to rewrite everything
2500 * that writes that reg, but it would require smarter
2501 * tracking to delay the rewriting until complete success.
2502 */
2503 if (scan_inst->is_partial_write())
2504 break;
2505
2506 /* Things returning more than one register would need us to
2507 * understand coalescing out more than one MOV at a time.
2508 */
2509 if (scan_inst->regs_written > scan_inst->exec_size / 8)
2510 break;
2511
2512 /* SEND instructions can't have MRF as a destination. */
2513 if (scan_inst->mlen)
2514 break;
2515
2516 if (devinfo->gen == 6) {
2517 /* gen6 math instructions must have the destination be
2518 * GRF, so no compute-to-MRF for them.
2519 */
2520 if (scan_inst->is_math()) {
2521 break;
2522 }
2523 }
2524
2525 if (scan_inst->dst.reg_offset == inst->src[0].reg_offset) {
2526 /* Found the creator of our MRF's source value. */
2527 scan_inst->dst.file = MRF;
2528 scan_inst->dst.reg = inst->dst.reg;
2529 scan_inst->saturate |= inst->saturate;
2530 inst->remove(block);
2531 progress = true;
2532 }
2533 break;
2534 }
2535
2536 /* We don't handle control flow here. Most computation of
2537 * values that end up in MRFs are shortly before the MRF
2538 * write anyway.
2539 */
2540 if (block->start() == scan_inst)
2541 break;
2542
2543 /* You can't read from an MRF, so if someone else reads our
2544 * MRF's source GRF that we wanted to rewrite, that stops us.
2545 */
2546 bool interfered = false;
2547 for (int i = 0; i < scan_inst->sources; i++) {
2548 if (scan_inst->src[i].file == GRF &&
2549 scan_inst->src[i].reg == inst->src[0].reg &&
2550 scan_inst->src[i].reg_offset == inst->src[0].reg_offset) {
2551 interfered = true;
2552 }
2553 }
2554 if (interfered)
2555 break;
2556
2557 if (scan_inst->dst.file == MRF) {
2558 /* If somebody else writes our MRF here, we can't
2559 * compute-to-MRF before that.
2560 */
2561 int scan_mrf_low = scan_inst->dst.reg & ~BRW_MRF_COMPR4;
2562 int scan_mrf_high;
2563
2564 if (scan_inst->dst.reg & BRW_MRF_COMPR4) {
2565 scan_mrf_high = scan_mrf_low + 4;
2566 } else if (scan_inst->exec_size == 16) {
2567 scan_mrf_high = scan_mrf_low + 1;
2568 } else {
2569 scan_mrf_high = scan_mrf_low;
2570 }
2571
2572 if (mrf_low == scan_mrf_low ||
2573 mrf_low == scan_mrf_high ||
2574 mrf_high == scan_mrf_low ||
2575 mrf_high == scan_mrf_high) {
2576 break;
2577 }
2578 }
2579
2580 if (scan_inst->mlen > 0 && scan_inst->base_mrf != -1) {
2581 /* Found a SEND instruction, which means that there are
2582 * live values in MRFs from base_mrf to base_mrf +
2583 * scan_inst->mlen - 1. Don't go pushing our MRF write up
2584 * above it.
2585 */
2586 if (mrf_low >= scan_inst->base_mrf &&
2587 mrf_low < scan_inst->base_mrf + scan_inst->mlen) {
2588 break;
2589 }
2590 if (mrf_high >= scan_inst->base_mrf &&
2591 mrf_high < scan_inst->base_mrf + scan_inst->mlen) {
2592 break;
2593 }
2594 }
2595 }
2596 }
2597
2598 if (progress)
2599 invalidate_live_intervals();
2600
2601 return progress;
2602 }
2603
2604 /**
2605 * Eliminate FIND_LIVE_CHANNEL instructions occurring outside any control
2606 * flow. We could probably do better here with some form of divergence
2607 * analysis.
2608 */
2609 bool
2610 fs_visitor::eliminate_find_live_channel()
2611 {
2612 bool progress = false;
2613 unsigned depth = 0;
2614
2615 foreach_block_and_inst_safe(block, fs_inst, inst, cfg) {
2616 switch (inst->opcode) {
2617 case BRW_OPCODE_IF:
2618 case BRW_OPCODE_DO:
2619 depth++;
2620 break;
2621
2622 case BRW_OPCODE_ENDIF:
2623 case BRW_OPCODE_WHILE:
2624 depth--;
2625 break;
2626
2627 case FS_OPCODE_DISCARD_JUMP:
2628 /* This can potentially make control flow non-uniform until the end
2629 * of the program.
2630 */
2631 return progress;
2632
2633 case SHADER_OPCODE_FIND_LIVE_CHANNEL:
2634 if (depth == 0) {
2635 inst->opcode = BRW_OPCODE_MOV;
2636 inst->src[0] = fs_reg(0);
2637 inst->sources = 1;
2638 inst->force_writemask_all = true;
2639 progress = true;
2640 }
2641 break;
2642
2643 default:
2644 break;
2645 }
2646 }
2647
2648 return progress;
2649 }
2650
2651 /**
2652 * Once we've generated code, try to convert normal FS_OPCODE_FB_WRITE
2653 * instructions to FS_OPCODE_REP_FB_WRITE.
2654 */
2655 void
2656 fs_visitor::emit_repclear_shader()
2657 {
2658 brw_wm_prog_key *key = (brw_wm_prog_key*) this->key;
2659 int base_mrf = 1;
2660 int color_mrf = base_mrf + 2;
2661
2662 fs_inst *mov = bld.exec_all().MOV(vec4(brw_message_reg(color_mrf)),
2663 fs_reg(UNIFORM, 0, BRW_REGISTER_TYPE_F));
2664
2665 fs_inst *write;
2666 if (key->nr_color_regions == 1) {
2667 write = bld.emit(FS_OPCODE_REP_FB_WRITE);
2668 write->saturate = key->clamp_fragment_color;
2669 write->base_mrf = color_mrf;
2670 write->target = 0;
2671 write->header_size = 0;
2672 write->mlen = 1;
2673 } else {
2674 assume(key->nr_color_regions > 0);
2675 for (int i = 0; i < key->nr_color_regions; ++i) {
2676 write = bld.emit(FS_OPCODE_REP_FB_WRITE);
2677 write->saturate = key->clamp_fragment_color;
2678 write->base_mrf = base_mrf;
2679 write->target = i;
2680 write->header_size = 2;
2681 write->mlen = 3;
2682 }
2683 }
2684 write->eot = true;
2685
2686 calculate_cfg();
2687
2688 assign_constant_locations();
2689 assign_curb_setup();
2690
2691 /* Now that we have the uniform assigned, go ahead and force it to a vec4. */
2692 assert(mov->src[0].file == HW_REG);
2693 mov->src[0] = brw_vec4_grf(mov->src[0].fixed_hw_reg.nr, 0);
2694 }
2695
2696 /**
2697 * Walks through basic blocks, looking for repeated MRF writes and
2698 * removing the later ones.
2699 */
2700 bool
2701 fs_visitor::remove_duplicate_mrf_writes()
2702 {
2703 fs_inst *last_mrf_move[16];
2704 bool progress = false;
2705
2706 /* Need to update the MRF tracking for compressed instructions. */
2707 if (dispatch_width == 16)
2708 return false;
2709
2710 memset(last_mrf_move, 0, sizeof(last_mrf_move));
2711
2712 foreach_block_and_inst_safe (block, fs_inst, inst, cfg) {
2713 if (inst->is_control_flow()) {
2714 memset(last_mrf_move, 0, sizeof(last_mrf_move));
2715 }
2716
2717 if (inst->opcode == BRW_OPCODE_MOV &&
2718 inst->dst.file == MRF) {
2719 fs_inst *prev_inst = last_mrf_move[inst->dst.reg];
2720 if (prev_inst && inst->equals(prev_inst)) {
2721 inst->remove(block);
2722 progress = true;
2723 continue;
2724 }
2725 }
2726
2727 /* Clear out the last-write records for MRFs that were overwritten. */
2728 if (inst->dst.file == MRF) {
2729 last_mrf_move[inst->dst.reg] = NULL;
2730 }
2731
2732 if (inst->mlen > 0 && inst->base_mrf != -1) {
2733 /* Found a SEND instruction, which will include two or fewer
2734 * implied MRF writes. We could do better here.
2735 */
2736 for (int i = 0; i < implied_mrf_writes(inst); i++) {
2737 last_mrf_move[inst->base_mrf + i] = NULL;
2738 }
2739 }
2740
2741 /* Clear out any MRF move records whose sources got overwritten. */
2742 if (inst->dst.file == GRF) {
2743 for (unsigned int i = 0; i < ARRAY_SIZE(last_mrf_move); i++) {
2744 if (last_mrf_move[i] &&
2745 last_mrf_move[i]->src[0].reg == inst->dst.reg) {
2746 last_mrf_move[i] = NULL;
2747 }
2748 }
2749 }
2750
2751 if (inst->opcode == BRW_OPCODE_MOV &&
2752 inst->dst.file == MRF &&
2753 inst->src[0].file == GRF &&
2754 !inst->is_partial_write()) {
2755 last_mrf_move[inst->dst.reg] = inst;
2756 }
2757 }
2758
2759 if (progress)
2760 invalidate_live_intervals();
2761
2762 return progress;
2763 }
2764
2765 static void
2766 clear_deps_for_inst_src(fs_inst *inst, bool *deps, int first_grf, int grf_len)
2767 {
2768 /* Clear the flag for registers that actually got read (as expected). */
2769 for (int i = 0; i < inst->sources; i++) {
2770 int grf;
2771 if (inst->src[i].file == GRF) {
2772 grf = inst->src[i].reg;
2773 } else if (inst->src[i].file == HW_REG &&
2774 inst->src[i].fixed_hw_reg.file == BRW_GENERAL_REGISTER_FILE) {
2775 grf = inst->src[i].fixed_hw_reg.nr;
2776 } else {
2777 continue;
2778 }
2779
2780 if (grf >= first_grf &&
2781 grf < first_grf + grf_len) {
2782 deps[grf - first_grf] = false;
2783 if (inst->exec_size == 16)
2784 deps[grf - first_grf + 1] = false;
2785 }
2786 }
2787 }
2788
2789 /**
2790 * Implements this workaround for the original 965:
2791 *
2792 * "[DevBW, DevCL] Implementation Restrictions: As the hardware does not
2793 * check for post destination dependencies on this instruction, software
2794 * must ensure that there is no destination hazard for the case of ‘write
2795 * followed by a posted write’ shown in the following example.
2796 *
2797 * 1. mov r3 0
2798 * 2. send r3.xy <rest of send instruction>
2799 * 3. mov r2 r3
2800 *
2801 * Due to no post-destination dependency check on the ‘send’, the above
2802 * code sequence could have two instructions (1 and 2) in flight at the
2803 * same time that both consider ‘r3’ as the target of their final writes.
2804 */
2805 void
2806 fs_visitor::insert_gen4_pre_send_dependency_workarounds(bblock_t *block,
2807 fs_inst *inst)
2808 {
2809 int write_len = inst->regs_written;
2810 int first_write_grf = inst->dst.reg;
2811 bool needs_dep[BRW_MAX_MRF];
2812 assert(write_len < (int)sizeof(needs_dep) - 1);
2813
2814 memset(needs_dep, false, sizeof(needs_dep));
2815 memset(needs_dep, true, write_len);
2816
2817 clear_deps_for_inst_src(inst, needs_dep, first_write_grf, write_len);
2818
2819 /* Walk backwards looking for writes to registers we're writing which
2820 * aren't read since being written. If we hit the start of the program,
2821 * we assume that there are no outstanding dependencies on entry to the
2822 * program.
2823 */
2824 foreach_inst_in_block_reverse_starting_from(fs_inst, scan_inst, inst, block) {
2825 /* If we hit control flow, assume that there *are* outstanding
2826 * dependencies, and force their cleanup before our instruction.
2827 */
2828 if (block->start() == scan_inst) {
2829 for (int i = 0; i < write_len; i++) {
2830 if (needs_dep[i])
2831 DEP_RESOLVE_MOV(fs_builder(this, block, inst),
2832 first_write_grf + i);
2833 }
2834 return;
2835 }
2836
2837 /* We insert our reads as late as possible on the assumption that any
2838 * instruction but a MOV that might have left us an outstanding
2839 * dependency has more latency than a MOV.
2840 */
2841 if (scan_inst->dst.file == GRF) {
2842 for (int i = 0; i < scan_inst->regs_written; i++) {
2843 int reg = scan_inst->dst.reg + i;
2844
2845 if (reg >= first_write_grf &&
2846 reg < first_write_grf + write_len &&
2847 needs_dep[reg - first_write_grf]) {
2848 DEP_RESOLVE_MOV(fs_builder(this, block, inst), reg);
2849 needs_dep[reg - first_write_grf] = false;
2850 if (scan_inst->exec_size == 16)
2851 needs_dep[reg - first_write_grf + 1] = false;
2852 }
2853 }
2854 }
2855
2856 /* Clear the flag for registers that actually got read (as expected). */
2857 clear_deps_for_inst_src(scan_inst, needs_dep, first_write_grf, write_len);
2858
2859 /* Continue the loop only if we haven't resolved all the dependencies */
2860 int i;
2861 for (i = 0; i < write_len; i++) {
2862 if (needs_dep[i])
2863 break;
2864 }
2865 if (i == write_len)
2866 return;
2867 }
2868 }
2869
2870 /**
2871 * Implements this workaround for the original 965:
2872 *
2873 * "[DevBW, DevCL] Errata: A destination register from a send can not be
2874 * used as a destination register until after it has been sourced by an
2875 * instruction with a different destination register.
2876 */
2877 void
2878 fs_visitor::insert_gen4_post_send_dependency_workarounds(bblock_t *block, fs_inst *inst)
2879 {
2880 int write_len = inst->regs_written;
2881 int first_write_grf = inst->dst.reg;
2882 bool needs_dep[BRW_MAX_MRF];
2883 assert(write_len < (int)sizeof(needs_dep) - 1);
2884
2885 memset(needs_dep, false, sizeof(needs_dep));
2886 memset(needs_dep, true, write_len);
2887 /* Walk forwards looking for writes to registers we're writing which aren't
2888 * read before being written.
2889 */
2890 foreach_inst_in_block_starting_from(fs_inst, scan_inst, inst, block) {
2891 /* If we hit control flow, force resolve all remaining dependencies. */
2892 if (block->end() == scan_inst) {
2893 for (int i = 0; i < write_len; i++) {
2894 if (needs_dep[i])
2895 DEP_RESOLVE_MOV(fs_builder(this, block, scan_inst),
2896 first_write_grf + i);
2897 }
2898 return;
2899 }
2900
2901 /* Clear the flag for registers that actually got read (as expected). */
2902 clear_deps_for_inst_src(scan_inst, needs_dep, first_write_grf, write_len);
2903
2904 /* We insert our reads as late as possible since they're reading the
2905 * result of a SEND, which has massive latency.
2906 */
2907 if (scan_inst->dst.file == GRF &&
2908 scan_inst->dst.reg >= first_write_grf &&
2909 scan_inst->dst.reg < first_write_grf + write_len &&
2910 needs_dep[scan_inst->dst.reg - first_write_grf]) {
2911 DEP_RESOLVE_MOV(fs_builder(this, block, scan_inst),
2912 scan_inst->dst.reg);
2913 needs_dep[scan_inst->dst.reg - first_write_grf] = false;
2914 }
2915
2916 /* Continue the loop only if we haven't resolved all the dependencies */
2917 int i;
2918 for (i = 0; i < write_len; i++) {
2919 if (needs_dep[i])
2920 break;
2921 }
2922 if (i == write_len)
2923 return;
2924 }
2925 }
2926
2927 void
2928 fs_visitor::insert_gen4_send_dependency_workarounds()
2929 {
2930 if (devinfo->gen != 4 || devinfo->is_g4x)
2931 return;
2932
2933 bool progress = false;
2934
2935 /* Note that we're done with register allocation, so GRF fs_regs always
2936 * have a .reg_offset of 0.
2937 */
2938
2939 foreach_block_and_inst(block, fs_inst, inst, cfg) {
2940 if (inst->mlen != 0 && inst->dst.file == GRF) {
2941 insert_gen4_pre_send_dependency_workarounds(block, inst);
2942 insert_gen4_post_send_dependency_workarounds(block, inst);
2943 progress = true;
2944 }
2945 }
2946
2947 if (progress)
2948 invalidate_live_intervals();
2949 }
2950
2951 /**
2952 * Turns the generic expression-style uniform pull constant load instruction
2953 * into a hardware-specific series of instructions for loading a pull
2954 * constant.
2955 *
2956 * The expression style allows the CSE pass before this to optimize out
2957 * repeated loads from the same offset, and gives the pre-register-allocation
2958 * scheduling full flexibility, while the conversion to native instructions
2959 * allows the post-register-allocation scheduler the best information
2960 * possible.
2961 *
2962 * Note that execution masking for setting up pull constant loads is special:
2963 * the channels that need to be written are unrelated to the current execution
2964 * mask, since a later instruction will use one of the result channels as a
2965 * source operand for all 8 or 16 of its channels.
2966 */
2967 void
2968 fs_visitor::lower_uniform_pull_constant_loads()
2969 {
2970 foreach_block_and_inst (block, fs_inst, inst, cfg) {
2971 if (inst->opcode != FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD)
2972 continue;
2973
2974 if (devinfo->gen >= 7) {
2975 /* The offset arg before was a vec4-aligned byte offset. We need to
2976 * turn it into a dword offset.
2977 */
2978 fs_reg const_offset_reg = inst->src[1];
2979 assert(const_offset_reg.file == IMM &&
2980 const_offset_reg.type == BRW_REGISTER_TYPE_UD);
2981 const_offset_reg.fixed_hw_reg.dw1.ud /= 4;
2982
2983 fs_reg payload, offset;
2984 if (devinfo->gen >= 9) {
2985 /* We have to use a message header on Skylake to get SIMD4x2
2986 * mode. Reserve space for the register.
2987 */
2988 offset = payload = fs_reg(GRF, alloc.allocate(2));
2989 offset.reg_offset++;
2990 inst->mlen = 2;
2991 } else {
2992 offset = payload = fs_reg(GRF, alloc.allocate(1));
2993 inst->mlen = 1;
2994 }
2995
2996 /* This is actually going to be a MOV, but since only the first dword
2997 * is accessed, we have a special opcode to do just that one. Note
2998 * that this needs to be an operation that will be considered a def
2999 * by live variable analysis, or register allocation will explode.
3000 */
3001 fs_inst *setup = new(mem_ctx) fs_inst(FS_OPCODE_SET_SIMD4X2_OFFSET,
3002 8, offset, const_offset_reg);
3003 setup->force_writemask_all = true;
3004
3005 setup->ir = inst->ir;
3006 setup->annotation = inst->annotation;
3007 inst->insert_before(block, setup);
3008
3009 /* Similarly, this will only populate the first 4 channels of the
3010 * result register (since we only use smear values from 0-3), but we
3011 * don't tell the optimizer.
3012 */
3013 inst->opcode = FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD_GEN7;
3014 inst->src[1] = payload;
3015 inst->base_mrf = -1;
3016
3017 invalidate_live_intervals();
3018 } else {
3019 /* Before register allocation, we didn't tell the scheduler about the
3020 * MRF we use. We know it's safe to use this MRF because nothing
3021 * else does except for register spill/unspill, which generates and
3022 * uses its MRF within a single IR instruction.
3023 */
3024 inst->base_mrf = 14;
3025 inst->mlen = 1;
3026 }
3027 }
3028 }
3029
3030 bool
3031 fs_visitor::lower_load_payload()
3032 {
3033 bool progress = false;
3034
3035 foreach_block_and_inst_safe (block, fs_inst, inst, cfg) {
3036 if (inst->opcode != SHADER_OPCODE_LOAD_PAYLOAD)
3037 continue;
3038
3039 assert(inst->dst.file == MRF || inst->dst.file == GRF);
3040 assert(inst->saturate == false);
3041 fs_reg dst = inst->dst;
3042
3043 /* Get rid of COMPR4. We'll add it back in if we need it */
3044 if (dst.file == MRF)
3045 dst.reg = dst.reg & ~BRW_MRF_COMPR4;
3046
3047 const fs_builder ibld(this, block, inst);
3048 const fs_builder hbld = ibld.exec_all().group(8, 0);
3049
3050 for (uint8_t i = 0; i < inst->header_size; i++) {
3051 if (inst->src[i].file != BAD_FILE) {
3052 fs_reg mov_dst = retype(dst, BRW_REGISTER_TYPE_UD);
3053 fs_reg mov_src = retype(inst->src[i], BRW_REGISTER_TYPE_UD);
3054 hbld.MOV(mov_dst, mov_src);
3055 }
3056 dst = offset(dst, hbld, 1);
3057 }
3058
3059 if (inst->dst.file == MRF && (inst->dst.reg & BRW_MRF_COMPR4) &&
3060 inst->exec_size > 8) {
3061 /* In this case, the payload portion of the LOAD_PAYLOAD isn't
3062 * a straightforward copy. Instead, the result of the
3063 * LOAD_PAYLOAD is treated as interleaved and the first four
3064 * non-header sources are unpacked as:
3065 *
3066 * m + 0: r0
3067 * m + 1: g0
3068 * m + 2: b0
3069 * m + 3: a0
3070 * m + 4: r1
3071 * m + 5: g1
3072 * m + 6: b1
3073 * m + 7: a1
3074 *
3075 * This is used for gen <= 5 fb writes.
3076 */
3077 assert(inst->exec_size == 16);
3078 assert(inst->header_size + 4 <= inst->sources);
3079 for (uint8_t i = inst->header_size; i < inst->header_size + 4; i++) {
3080 if (inst->src[i].file != BAD_FILE) {
3081 if (devinfo->has_compr4) {
3082 fs_reg compr4_dst = retype(dst, inst->src[i].type);
3083 compr4_dst.reg |= BRW_MRF_COMPR4;
3084 ibld.MOV(compr4_dst, inst->src[i]);
3085 } else {
3086 /* Platform doesn't have COMPR4. We have to fake it */
3087 fs_reg mov_dst = retype(dst, inst->src[i].type);
3088 ibld.half(0).MOV(mov_dst, half(inst->src[i], 0));
3089 mov_dst.reg += 4;
3090 ibld.half(1).MOV(mov_dst, half(inst->src[i], 1));
3091 }
3092 }
3093
3094 dst.reg++;
3095 }
3096
3097 /* The loop above only ever incremented us through the first set
3098 * of 4 registers. However, thanks to the magic of COMPR4, we
3099 * actually wrote to the first 8 registers, so we need to take
3100 * that into account now.
3101 */
3102 dst.reg += 4;
3103
3104 /* The COMPR4 code took care of the first 4 sources. We'll let
3105 * the regular path handle any remaining sources. Yes, we are
3106 * modifying the instruction but we're about to delete it so
3107 * this really doesn't hurt anything.
3108 */
3109 inst->header_size += 4;
3110 }
3111
3112 for (uint8_t i = inst->header_size; i < inst->sources; i++) {
3113 if (inst->src[i].file != BAD_FILE)
3114 ibld.MOV(retype(dst, inst->src[i].type), inst->src[i]);
3115 dst = offset(dst, ibld, 1);
3116 }
3117
3118 inst->remove(block);
3119 progress = true;
3120 }
3121
3122 if (progress)
3123 invalidate_live_intervals();
3124
3125 return progress;
3126 }
3127
3128 bool
3129 fs_visitor::lower_integer_multiplication()
3130 {
3131 bool progress = false;
3132
3133 foreach_block_and_inst_safe(block, fs_inst, inst, cfg) {
3134 const fs_builder ibld(this, block, inst);
3135
3136 if (inst->opcode == BRW_OPCODE_MUL) {
3137 if (inst->dst.is_accumulator() ||
3138 (inst->dst.type != BRW_REGISTER_TYPE_D &&
3139 inst->dst.type != BRW_REGISTER_TYPE_UD))
3140 continue;
3141
3142 /* Gen8's MUL instruction can do a 32-bit x 32-bit -> 32-bit
3143 * operation directly, but CHV/BXT cannot.
3144 */
3145 if (devinfo->gen >= 8 &&
3146 !devinfo->is_cherryview && !devinfo->is_broxton)
3147 continue;
3148
3149 if (inst->src[1].file == IMM &&
3150 inst->src[1].fixed_hw_reg.dw1.ud < (1 << 16)) {
3151 /* The MUL instruction isn't commutative. On Gen <= 6, only the low
3152 * 16-bits of src0 are read, and on Gen >= 7 only the low 16-bits of
3153 * src1 are used.
3154 *
3155 * If multiplying by an immediate value that fits in 16-bits, do a
3156 * single MUL instruction with that value in the proper location.
3157 */
3158 if (devinfo->gen < 7) {
3159 fs_reg imm(GRF, alloc.allocate(dispatch_width / 8),
3160 inst->dst.type);
3161 ibld.MOV(imm, inst->src[1]);
3162 ibld.MUL(inst->dst, imm, inst->src[0]);
3163 } else {
3164 ibld.MUL(inst->dst, inst->src[0], inst->src[1]);
3165 }
3166 } else {
3167 /* Gen < 8 (and some Gen8+ low-power parts like Cherryview) cannot
3168 * do 32-bit integer multiplication in one instruction, but instead
3169 * must do a sequence (which actually calculates a 64-bit result):
3170 *
3171 * mul(8) acc0<1>D g3<8,8,1>D g4<8,8,1>D
3172 * mach(8) null g3<8,8,1>D g4<8,8,1>D
3173 * mov(8) g2<1>D acc0<8,8,1>D
3174 *
3175 * But on Gen > 6, the ability to use second accumulator register
3176 * (acc1) for non-float data types was removed, preventing a simple
3177 * implementation in SIMD16. A 16-channel result can be calculated by
3178 * executing the three instructions twice in SIMD8, once with quarter
3179 * control of 1Q for the first eight channels and again with 2Q for
3180 * the second eight channels.
3181 *
3182 * Which accumulator register is implicitly accessed (by AccWrEnable
3183 * for instance) is determined by the quarter control. Unfortunately
3184 * Ivybridge (and presumably Baytrail) has a hardware bug in which an
3185 * implicit accumulator access by an instruction with 2Q will access
3186 * acc1 regardless of whether the data type is usable in acc1.
3187 *
3188 * Specifically, the 2Q mach(8) writes acc1 which does not exist for
3189 * integer data types.
3190 *
3191 * Since we only want the low 32-bits of the result, we can do two
3192 * 32-bit x 16-bit multiplies (like the mul and mach are doing), and
3193 * adjust the high result and add them (like the mach is doing):
3194 *
3195 * mul(8) g7<1>D g3<8,8,1>D g4.0<8,8,1>UW
3196 * mul(8) g8<1>D g3<8,8,1>D g4.1<8,8,1>UW
3197 * shl(8) g9<1>D g8<8,8,1>D 16D
3198 * add(8) g2<1>D g7<8,8,1>D g8<8,8,1>D
3199 *
3200 * We avoid the shl instruction by realizing that we only want to add
3201 * the low 16-bits of the "high" result to the high 16-bits of the
3202 * "low" result and using proper regioning on the add:
3203 *
3204 * mul(8) g7<1>D g3<8,8,1>D g4.0<16,8,2>UW
3205 * mul(8) g8<1>D g3<8,8,1>D g4.1<16,8,2>UW
3206 * add(8) g7.1<2>UW g7.1<16,8,2>UW g8<16,8,2>UW
3207 *
3208 * Since it does not use the (single) accumulator register, we can
3209 * schedule multi-component multiplications much better.
3210 */
3211
3212 if (inst->conditional_mod && inst->dst.is_null()) {
3213 inst->dst = fs_reg(GRF, alloc.allocate(dispatch_width / 8),
3214 inst->dst.type);
3215 }
3216 fs_reg low = inst->dst;
3217 fs_reg high(GRF, alloc.allocate(dispatch_width / 8),
3218 inst->dst.type);
3219
3220 if (devinfo->gen >= 7) {
3221 fs_reg src1_0_w = inst->src[1];
3222 fs_reg src1_1_w = inst->src[1];
3223
3224 if (inst->src[1].file == IMM) {
3225 src1_0_w.fixed_hw_reg.dw1.ud &= 0xffff;
3226 src1_1_w.fixed_hw_reg.dw1.ud >>= 16;
3227 } else {
3228 src1_0_w.type = BRW_REGISTER_TYPE_UW;
3229 if (src1_0_w.stride != 0) {
3230 assert(src1_0_w.stride == 1);
3231 src1_0_w.stride = 2;
3232 }
3233
3234 src1_1_w.type = BRW_REGISTER_TYPE_UW;
3235 if (src1_1_w.stride != 0) {
3236 assert(src1_1_w.stride == 1);
3237 src1_1_w.stride = 2;
3238 }
3239 src1_1_w.subreg_offset += type_sz(BRW_REGISTER_TYPE_UW);
3240 }
3241 ibld.MUL(low, inst->src[0], src1_0_w);
3242 ibld.MUL(high, inst->src[0], src1_1_w);
3243 } else {
3244 fs_reg src0_0_w = inst->src[0];
3245 fs_reg src0_1_w = inst->src[0];
3246
3247 src0_0_w.type = BRW_REGISTER_TYPE_UW;
3248 if (src0_0_w.stride != 0) {
3249 assert(src0_0_w.stride == 1);
3250 src0_0_w.stride = 2;
3251 }
3252
3253 src0_1_w.type = BRW_REGISTER_TYPE_UW;
3254 if (src0_1_w.stride != 0) {
3255 assert(src0_1_w.stride == 1);
3256 src0_1_w.stride = 2;
3257 }
3258 src0_1_w.subreg_offset += type_sz(BRW_REGISTER_TYPE_UW);
3259
3260 ibld.MUL(low, src0_0_w, inst->src[1]);
3261 ibld.MUL(high, src0_1_w, inst->src[1]);
3262 }
3263
3264 fs_reg dst = inst->dst;
3265 dst.type = BRW_REGISTER_TYPE_UW;
3266 dst.subreg_offset = 2;
3267 dst.stride = 2;
3268
3269 high.type = BRW_REGISTER_TYPE_UW;
3270 high.stride = 2;
3271
3272 low.type = BRW_REGISTER_TYPE_UW;
3273 low.subreg_offset = 2;
3274 low.stride = 2;
3275
3276 ibld.ADD(dst, low, high);
3277
3278 if (inst->conditional_mod) {
3279 fs_reg null(retype(ibld.null_reg_f(), inst->dst.type));
3280 set_condmod(inst->conditional_mod,
3281 ibld.MOV(null, inst->dst));
3282 }
3283 }
3284
3285 } else if (inst->opcode == SHADER_OPCODE_MULH) {
3286 /* Should have been lowered to 8-wide. */
3287 assert(inst->exec_size <= 8);
3288 const fs_reg acc = retype(brw_acc_reg(inst->exec_size),
3289 inst->dst.type);
3290 fs_inst *mul = ibld.MUL(acc, inst->src[0], inst->src[1]);
3291 fs_inst *mach = ibld.MACH(inst->dst, inst->src[0], inst->src[1]);
3292
3293 if (devinfo->gen >= 8) {
3294 /* Until Gen8, integer multiplies read 32-bits from one source,
3295 * and 16-bits from the other, and relying on the MACH instruction
3296 * to generate the high bits of the result.
3297 *
3298 * On Gen8, the multiply instruction does a full 32x32-bit
3299 * multiply, but in order to do a 64-bit multiply we can simulate
3300 * the previous behavior and then use a MACH instruction.
3301 *
3302 * FINISHME: Don't use source modifiers on src1.
3303 */
3304 assert(mul->src[1].type == BRW_REGISTER_TYPE_D ||
3305 mul->src[1].type == BRW_REGISTER_TYPE_UD);
3306 mul->src[1].type = (type_is_signed(mul->src[1].type) ?
3307 BRW_REGISTER_TYPE_W : BRW_REGISTER_TYPE_UW);
3308 mul->src[1].stride *= 2;
3309
3310 } else if (devinfo->gen == 7 && !devinfo->is_haswell &&
3311 inst->force_sechalf) {
3312 /* Among other things the quarter control bits influence which
3313 * accumulator register is used by the hardware for instructions
3314 * that access the accumulator implicitly (e.g. MACH). A
3315 * second-half instruction would normally map to acc1, which
3316 * doesn't exist on Gen7 and up (the hardware does emulate it for
3317 * floating-point instructions *only* by taking advantage of the
3318 * extra precision of acc0 not normally used for floating point
3319 * arithmetic).
3320 *
3321 * HSW and up are careful enough not to try to access an
3322 * accumulator register that doesn't exist, but on earlier Gen7
3323 * hardware we need to make sure that the quarter control bits are
3324 * zero to avoid non-deterministic behaviour and emit an extra MOV
3325 * to get the result masked correctly according to the current
3326 * channel enables.
3327 */
3328 mach->force_sechalf = false;
3329 mach->force_writemask_all = true;
3330 mach->dst = ibld.vgrf(inst->dst.type);
3331 ibld.MOV(inst->dst, mach->dst);
3332 }
3333 } else {
3334 continue;
3335 }
3336
3337 inst->remove(block);
3338 progress = true;
3339 }
3340
3341 if (progress)
3342 invalidate_live_intervals();
3343
3344 return progress;
3345 }
3346
3347 static void
3348 setup_color_payload(const fs_builder &bld, const brw_wm_prog_key *key,
3349 fs_reg *dst, fs_reg color, unsigned components)
3350 {
3351 if (key->clamp_fragment_color) {
3352 fs_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_F, 4);
3353 assert(color.type == BRW_REGISTER_TYPE_F);
3354
3355 for (unsigned i = 0; i < components; i++)
3356 set_saturate(true,
3357 bld.MOV(offset(tmp, bld, i), offset(color, bld, i)));
3358
3359 color = tmp;
3360 }
3361
3362 for (unsigned i = 0; i < components; i++)
3363 dst[i] = offset(color, bld, i);
3364 }
3365
3366 static void
3367 lower_fb_write_logical_send(const fs_builder &bld, fs_inst *inst,
3368 const brw_wm_prog_data *prog_data,
3369 const brw_wm_prog_key *key,
3370 const fs_visitor::thread_payload &payload)
3371 {
3372 assert(inst->src[6].file == IMM);
3373 const brw_device_info *devinfo = bld.shader->devinfo;
3374 const fs_reg &color0 = inst->src[0];
3375 const fs_reg &color1 = inst->src[1];
3376 const fs_reg &src0_alpha = inst->src[2];
3377 const fs_reg &src_depth = inst->src[3];
3378 const fs_reg &dst_depth = inst->src[4];
3379 fs_reg sample_mask = inst->src[5];
3380 const unsigned components = inst->src[6].fixed_hw_reg.dw1.ud;
3381
3382 /* We can potentially have a message length of up to 15, so we have to set
3383 * base_mrf to either 0 or 1 in order to fit in m0..m15.
3384 */
3385 fs_reg sources[15];
3386 int header_size = 2, payload_header_size;
3387 unsigned length = 0;
3388
3389 /* From the Sandy Bridge PRM, volume 4, page 198:
3390 *
3391 * "Dispatched Pixel Enables. One bit per pixel indicating
3392 * which pixels were originally enabled when the thread was
3393 * dispatched. This field is only required for the end-of-
3394 * thread message and on all dual-source messages."
3395 */
3396 if (devinfo->gen >= 6 &&
3397 (devinfo->is_haswell || devinfo->gen >= 8 || !prog_data->uses_kill) &&
3398 color1.file == BAD_FILE &&
3399 key->nr_color_regions == 1) {
3400 header_size = 0;
3401 }
3402
3403 if (header_size != 0) {
3404 assert(header_size == 2);
3405 /* Allocate 2 registers for a header */
3406 length += 2;
3407 }
3408
3409 if (payload.aa_dest_stencil_reg) {
3410 sources[length] = fs_reg(GRF, bld.shader->alloc.allocate(1));
3411 bld.group(8, 0).exec_all().annotate("FB write stencil/AA alpha")
3412 .MOV(sources[length],
3413 fs_reg(brw_vec8_grf(payload.aa_dest_stencil_reg, 0)));
3414 length++;
3415 }
3416
3417 if (prog_data->uses_omask) {
3418 sources[length] = fs_reg(GRF, bld.shader->alloc.allocate(1),
3419 BRW_REGISTER_TYPE_UD);
3420
3421 /* Hand over gl_SampleMask. Only the lower 16 bits of each channel are
3422 * relevant. Since it's unsigned single words one vgrf is always
3423 * 16-wide, but only the lower or higher 8 channels will be used by the
3424 * hardware when doing a SIMD8 write depending on whether we have
3425 * selected the subspans for the first or second half respectively.
3426 */
3427 assert(sample_mask.file != BAD_FILE && type_sz(sample_mask.type) == 4);
3428 sample_mask.type = BRW_REGISTER_TYPE_UW;
3429 sample_mask.stride *= 2;
3430
3431 bld.exec_all().annotate("FB write oMask")
3432 .MOV(half(retype(sources[length], BRW_REGISTER_TYPE_UW),
3433 inst->force_sechalf),
3434 sample_mask);
3435 length++;
3436 }
3437
3438 payload_header_size = length;
3439
3440 if (src0_alpha.file != BAD_FILE) {
3441 /* FIXME: This is being passed at the wrong location in the payload and
3442 * doesn't work when gl_SampleMask and MRTs are used simultaneously.
3443 * It's supposed to be immediately before oMask but there seems to be no
3444 * reasonable way to pass them in the correct order because LOAD_PAYLOAD
3445 * requires header sources to form a contiguous segment at the beginning
3446 * of the message and src0_alpha has per-channel semantics.
3447 */
3448 setup_color_payload(bld, key, &sources[length], src0_alpha, 1);
3449 length++;
3450 }
3451
3452 setup_color_payload(bld, key, &sources[length], color0, components);
3453 length += 4;
3454
3455 if (color1.file != BAD_FILE) {
3456 setup_color_payload(bld, key, &sources[length], color1, components);
3457 length += 4;
3458 }
3459
3460 if (src_depth.file != BAD_FILE) {
3461 sources[length] = src_depth;
3462 length++;
3463 }
3464
3465 if (dst_depth.file != BAD_FILE) {
3466 sources[length] = dst_depth;
3467 length++;
3468 }
3469
3470 fs_inst *load;
3471 if (devinfo->gen >= 7) {
3472 /* Send from the GRF */
3473 fs_reg payload = fs_reg(GRF, -1, BRW_REGISTER_TYPE_F);
3474 load = bld.LOAD_PAYLOAD(payload, sources, length, payload_header_size);
3475 payload.reg = bld.shader->alloc.allocate(load->regs_written);
3476 load->dst = payload;
3477
3478 inst->src[0] = payload;
3479 inst->resize_sources(1);
3480 inst->base_mrf = -1;
3481 } else {
3482 /* Send from the MRF */
3483 load = bld.LOAD_PAYLOAD(fs_reg(MRF, 1, BRW_REGISTER_TYPE_F),
3484 sources, length, payload_header_size);
3485
3486 /* On pre-SNB, we have to interlace the color values. LOAD_PAYLOAD
3487 * will do this for us if we just give it a COMPR4 destination.
3488 */
3489 if (devinfo->gen < 6 && bld.dispatch_width() == 16)
3490 load->dst.reg |= BRW_MRF_COMPR4;
3491
3492 inst->resize_sources(0);
3493 inst->base_mrf = 1;
3494 }
3495
3496 inst->opcode = FS_OPCODE_FB_WRITE;
3497 inst->mlen = load->regs_written;
3498 inst->header_size = header_size;
3499 }
3500
3501 static void
3502 lower_sampler_logical_send_gen4(const fs_builder &bld, fs_inst *inst, opcode op,
3503 const fs_reg &coordinate,
3504 const fs_reg &shadow_c,
3505 const fs_reg &lod, const fs_reg &lod2,
3506 const fs_reg &sampler,
3507 unsigned coord_components,
3508 unsigned grad_components)
3509 {
3510 const bool has_lod = (op == SHADER_OPCODE_TXL || op == FS_OPCODE_TXB ||
3511 op == SHADER_OPCODE_TXF || op == SHADER_OPCODE_TXS);
3512 fs_reg msg_begin(MRF, 1, BRW_REGISTER_TYPE_F);
3513 fs_reg msg_end = msg_begin;
3514
3515 /* g0 header. */
3516 msg_end = offset(msg_end, bld.group(8, 0), 1);
3517
3518 for (unsigned i = 0; i < coord_components; i++)
3519 bld.MOV(retype(offset(msg_end, bld, i), coordinate.type),
3520 offset(coordinate, bld, i));
3521
3522 msg_end = offset(msg_end, bld, coord_components);
3523
3524 /* Messages other than SAMPLE and RESINFO in SIMD16 and TXD in SIMD8
3525 * require all three components to be present and zero if they are unused.
3526 */
3527 if (coord_components > 0 &&
3528 (has_lod || shadow_c.file != BAD_FILE ||
3529 (op == SHADER_OPCODE_TEX && bld.dispatch_width() == 8))) {
3530 for (unsigned i = coord_components; i < 3; i++)
3531 bld.MOV(offset(msg_end, bld, i), fs_reg(0.0f));
3532
3533 msg_end = offset(msg_end, bld, 3 - coord_components);
3534 }
3535
3536 if (op == SHADER_OPCODE_TXD) {
3537 /* TXD unsupported in SIMD16 mode. */
3538 assert(bld.dispatch_width() == 8);
3539
3540 /* the slots for u and v are always present, but r is optional */
3541 if (coord_components < 2)
3542 msg_end = offset(msg_end, bld, 2 - coord_components);
3543
3544 /* P = u, v, r
3545 * dPdx = dudx, dvdx, drdx
3546 * dPdy = dudy, dvdy, drdy
3547 *
3548 * 1-arg: Does not exist.
3549 *
3550 * 2-arg: dudx dvdx dudy dvdy
3551 * dPdx.x dPdx.y dPdy.x dPdy.y
3552 * m4 m5 m6 m7
3553 *
3554 * 3-arg: dudx dvdx drdx dudy dvdy drdy
3555 * dPdx.x dPdx.y dPdx.z dPdy.x dPdy.y dPdy.z
3556 * m5 m6 m7 m8 m9 m10
3557 */
3558 for (unsigned i = 0; i < grad_components; i++)
3559 bld.MOV(offset(msg_end, bld, i), offset(lod, bld, i));
3560
3561 msg_end = offset(msg_end, bld, MAX2(grad_components, 2));
3562
3563 for (unsigned i = 0; i < grad_components; i++)
3564 bld.MOV(offset(msg_end, bld, i), offset(lod2, bld, i));
3565
3566 msg_end = offset(msg_end, bld, MAX2(grad_components, 2));
3567 }
3568
3569 if (has_lod) {
3570 /* Bias/LOD with shadow comparitor is unsupported in SIMD16 -- *Without*
3571 * shadow comparitor (including RESINFO) it's unsupported in SIMD8 mode.
3572 */
3573 assert(shadow_c.file != BAD_FILE ? bld.dispatch_width() == 8 :
3574 bld.dispatch_width() == 16);
3575
3576 const brw_reg_type type =
3577 (op == SHADER_OPCODE_TXF || op == SHADER_OPCODE_TXS ?
3578 BRW_REGISTER_TYPE_UD : BRW_REGISTER_TYPE_F);
3579 bld.MOV(retype(msg_end, type), lod);
3580 msg_end = offset(msg_end, bld, 1);
3581 }
3582
3583 if (shadow_c.file != BAD_FILE) {
3584 if (op == SHADER_OPCODE_TEX && bld.dispatch_width() == 8) {
3585 /* There's no plain shadow compare message, so we use shadow
3586 * compare with a bias of 0.0.
3587 */
3588 bld.MOV(msg_end, fs_reg(0.0f));
3589 msg_end = offset(msg_end, bld, 1);
3590 }
3591
3592 bld.MOV(msg_end, shadow_c);
3593 msg_end = offset(msg_end, bld, 1);
3594 }
3595
3596 inst->opcode = op;
3597 inst->src[0] = reg_undef;
3598 inst->src[1] = sampler;
3599 inst->resize_sources(2);
3600 inst->base_mrf = msg_begin.reg;
3601 inst->mlen = msg_end.reg - msg_begin.reg;
3602 inst->header_size = 1;
3603 }
3604
3605 static void
3606 lower_sampler_logical_send_gen5(const fs_builder &bld, fs_inst *inst, opcode op,
3607 fs_reg coordinate,
3608 const fs_reg &shadow_c,
3609 fs_reg lod, fs_reg lod2,
3610 const fs_reg &sample_index,
3611 const fs_reg &sampler,
3612 const fs_reg &offset_value,
3613 unsigned coord_components,
3614 unsigned grad_components)
3615 {
3616 fs_reg message(MRF, 2, BRW_REGISTER_TYPE_F);
3617 fs_reg msg_coords = message;
3618 unsigned header_size = 0;
3619
3620 if (offset_value.file != BAD_FILE) {
3621 /* The offsets set up by the visitor are in the m1 header, so we can't
3622 * go headerless.
3623 */
3624 header_size = 1;
3625 message.reg--;
3626 }
3627
3628 for (unsigned i = 0; i < coord_components; i++) {
3629 bld.MOV(retype(offset(msg_coords, bld, i), coordinate.type), coordinate);
3630 coordinate = offset(coordinate, bld, 1);
3631 }
3632 fs_reg msg_end = offset(msg_coords, bld, coord_components);
3633 fs_reg msg_lod = offset(msg_coords, bld, 4);
3634
3635 if (shadow_c.file != BAD_FILE) {
3636 fs_reg msg_shadow = msg_lod;
3637 bld.MOV(msg_shadow, shadow_c);
3638 msg_lod = offset(msg_shadow, bld, 1);
3639 msg_end = msg_lod;
3640 }
3641
3642 switch (op) {
3643 case SHADER_OPCODE_TXL:
3644 case FS_OPCODE_TXB:
3645 bld.MOV(msg_lod, lod);
3646 msg_end = offset(msg_lod, bld, 1);
3647 break;
3648 case SHADER_OPCODE_TXD:
3649 /**
3650 * P = u, v, r
3651 * dPdx = dudx, dvdx, drdx
3652 * dPdy = dudy, dvdy, drdy
3653 *
3654 * Load up these values:
3655 * - dudx dudy dvdx dvdy drdx drdy
3656 * - dPdx.x dPdy.x dPdx.y dPdy.y dPdx.z dPdy.z
3657 */
3658 msg_end = msg_lod;
3659 for (unsigned i = 0; i < grad_components; i++) {
3660 bld.MOV(msg_end, lod);
3661 lod = offset(lod, bld, 1);
3662 msg_end = offset(msg_end, bld, 1);
3663
3664 bld.MOV(msg_end, lod2);
3665 lod2 = offset(lod2, bld, 1);
3666 msg_end = offset(msg_end, bld, 1);
3667 }
3668 break;
3669 case SHADER_OPCODE_TXS:
3670 msg_lod = retype(msg_end, BRW_REGISTER_TYPE_UD);
3671 bld.MOV(msg_lod, lod);
3672 msg_end = offset(msg_lod, bld, 1);
3673 break;
3674 case SHADER_OPCODE_TXF:
3675 msg_lod = offset(msg_coords, bld, 3);
3676 bld.MOV(retype(msg_lod, BRW_REGISTER_TYPE_UD), lod);
3677 msg_end = offset(msg_lod, bld, 1);
3678 break;
3679 case SHADER_OPCODE_TXF_CMS:
3680 msg_lod = offset(msg_coords, bld, 3);
3681 /* lod */
3682 bld.MOV(retype(msg_lod, BRW_REGISTER_TYPE_UD), fs_reg(0u));
3683 /* sample index */
3684 bld.MOV(retype(offset(msg_lod, bld, 1), BRW_REGISTER_TYPE_UD), sample_index);
3685 msg_end = offset(msg_lod, bld, 2);
3686 break;
3687 default:
3688 break;
3689 }
3690
3691 inst->opcode = op;
3692 inst->src[0] = reg_undef;
3693 inst->src[1] = sampler;
3694 inst->resize_sources(2);
3695 inst->base_mrf = message.reg;
3696 inst->mlen = msg_end.reg - message.reg;
3697 inst->header_size = header_size;
3698
3699 /* Message length > MAX_SAMPLER_MESSAGE_SIZE disallowed by hardware. */
3700 assert(inst->mlen <= MAX_SAMPLER_MESSAGE_SIZE);
3701 }
3702
3703 static bool
3704 is_high_sampler(const struct brw_device_info *devinfo, const fs_reg &sampler)
3705 {
3706 if (devinfo->gen < 8 && !devinfo->is_haswell)
3707 return false;
3708
3709 return sampler.file != IMM || sampler.fixed_hw_reg.dw1.ud >= 16;
3710 }
3711
3712 static void
3713 lower_sampler_logical_send_gen7(const fs_builder &bld, fs_inst *inst, opcode op,
3714 fs_reg coordinate,
3715 const fs_reg &shadow_c,
3716 fs_reg lod, fs_reg lod2,
3717 const fs_reg &sample_index,
3718 const fs_reg &mcs, const fs_reg &sampler,
3719 fs_reg offset_value,
3720 unsigned coord_components,
3721 unsigned grad_components)
3722 {
3723 const brw_device_info *devinfo = bld.shader->devinfo;
3724 int reg_width = bld.dispatch_width() / 8;
3725 unsigned header_size = 0, length = 0;
3726 fs_reg sources[MAX_SAMPLER_MESSAGE_SIZE];
3727 for (unsigned i = 0; i < ARRAY_SIZE(sources); i++)
3728 sources[i] = bld.vgrf(BRW_REGISTER_TYPE_F);
3729
3730 if (op == SHADER_OPCODE_TG4 || op == SHADER_OPCODE_TG4_OFFSET ||
3731 offset_value.file != BAD_FILE ||
3732 is_high_sampler(devinfo, sampler)) {
3733 /* For general texture offsets (no txf workaround), we need a header to
3734 * put them in. Note that we're only reserving space for it in the
3735 * message payload as it will be initialized implicitly by the
3736 * generator.
3737 *
3738 * TG4 needs to place its channel select in the header, for interaction
3739 * with ARB_texture_swizzle. The sampler index is only 4-bits, so for
3740 * larger sampler numbers we need to offset the Sampler State Pointer in
3741 * the header.
3742 */
3743 header_size = 1;
3744 sources[0] = fs_reg();
3745 length++;
3746 }
3747
3748 if (shadow_c.file != BAD_FILE) {
3749 bld.MOV(sources[length], shadow_c);
3750 length++;
3751 }
3752
3753 bool coordinate_done = false;
3754
3755 /* The sampler can only meaningfully compute LOD for fragment shader
3756 * messages. For all other stages, we change the opcode to TXL and
3757 * hardcode the LOD to 0.
3758 */
3759 if (bld.shader->stage != MESA_SHADER_FRAGMENT &&
3760 op == SHADER_OPCODE_TEX) {
3761 op = SHADER_OPCODE_TXL;
3762 lod = fs_reg(0.0f);
3763 }
3764
3765 /* Set up the LOD info */
3766 switch (op) {
3767 case FS_OPCODE_TXB:
3768 case SHADER_OPCODE_TXL:
3769 bld.MOV(sources[length], lod);
3770 length++;
3771 break;
3772 case SHADER_OPCODE_TXD:
3773 /* TXD should have been lowered in SIMD16 mode. */
3774 assert(bld.dispatch_width() == 8);
3775
3776 /* Load dPdx and the coordinate together:
3777 * [hdr], [ref], x, dPdx.x, dPdy.x, y, dPdx.y, dPdy.y, z, dPdx.z, dPdy.z
3778 */
3779 for (unsigned i = 0; i < coord_components; i++) {
3780 bld.MOV(sources[length], coordinate);
3781 coordinate = offset(coordinate, bld, 1);
3782 length++;
3783
3784 /* For cube map array, the coordinate is (u,v,r,ai) but there are
3785 * only derivatives for (u, v, r).
3786 */
3787 if (i < grad_components) {
3788 bld.MOV(sources[length], lod);
3789 lod = offset(lod, bld, 1);
3790 length++;
3791
3792 bld.MOV(sources[length], lod2);
3793 lod2 = offset(lod2, bld, 1);
3794 length++;
3795 }
3796 }
3797
3798 coordinate_done = true;
3799 break;
3800 case SHADER_OPCODE_TXS:
3801 bld.MOV(retype(sources[length], BRW_REGISTER_TYPE_UD), lod);
3802 length++;
3803 break;
3804 case SHADER_OPCODE_TXF:
3805 /* Unfortunately, the parameters for LD are intermixed: u, lod, v, r.
3806 * On Gen9 they are u, v, lod, r
3807 */
3808 bld.MOV(retype(sources[length], BRW_REGISTER_TYPE_D), coordinate);
3809 coordinate = offset(coordinate, bld, 1);
3810 length++;
3811
3812 if (devinfo->gen >= 9) {
3813 if (coord_components >= 2) {
3814 bld.MOV(retype(sources[length], BRW_REGISTER_TYPE_D), coordinate);
3815 coordinate = offset(coordinate, bld, 1);
3816 }
3817 length++;
3818 }
3819
3820 bld.MOV(retype(sources[length], BRW_REGISTER_TYPE_D), lod);
3821 length++;
3822
3823 for (unsigned i = devinfo->gen >= 9 ? 2 : 1; i < coord_components; i++) {
3824 bld.MOV(retype(sources[length], BRW_REGISTER_TYPE_D), coordinate);
3825 coordinate = offset(coordinate, bld, 1);
3826 length++;
3827 }
3828
3829 coordinate_done = true;
3830 break;
3831 case SHADER_OPCODE_TXF_CMS:
3832 case SHADER_OPCODE_TXF_UMS:
3833 case SHADER_OPCODE_TXF_MCS:
3834 if (op == SHADER_OPCODE_TXF_UMS || op == SHADER_OPCODE_TXF_CMS) {
3835 bld.MOV(retype(sources[length], BRW_REGISTER_TYPE_UD), sample_index);
3836 length++;
3837 }
3838
3839 if (op == SHADER_OPCODE_TXF_CMS) {
3840 /* Data from the multisample control surface. */
3841 bld.MOV(retype(sources[length], BRW_REGISTER_TYPE_UD), mcs);
3842 length++;
3843 }
3844
3845 /* There is no offsetting for this message; just copy in the integer
3846 * texture coordinates.
3847 */
3848 for (unsigned i = 0; i < coord_components; i++) {
3849 bld.MOV(retype(sources[length], BRW_REGISTER_TYPE_D), coordinate);
3850 coordinate = offset(coordinate, bld, 1);
3851 length++;
3852 }
3853
3854 coordinate_done = true;
3855 break;
3856 case SHADER_OPCODE_TG4_OFFSET:
3857 /* gather4_po_c should have been lowered in SIMD16 mode. */
3858 assert(bld.dispatch_width() == 8 || shadow_c.file == BAD_FILE);
3859
3860 /* More crazy intermixing */
3861 for (unsigned i = 0; i < 2; i++) { /* u, v */
3862 bld.MOV(sources[length], coordinate);
3863 coordinate = offset(coordinate, bld, 1);
3864 length++;
3865 }
3866
3867 for (unsigned i = 0; i < 2; i++) { /* offu, offv */
3868 bld.MOV(retype(sources[length], BRW_REGISTER_TYPE_D), offset_value);
3869 offset_value = offset(offset_value, bld, 1);
3870 length++;
3871 }
3872
3873 if (coord_components == 3) { /* r if present */
3874 bld.MOV(sources[length], coordinate);
3875 coordinate = offset(coordinate, bld, 1);
3876 length++;
3877 }
3878
3879 coordinate_done = true;
3880 break;
3881 default:
3882 break;
3883 }
3884
3885 /* Set up the coordinate (except for cases where it was done above) */
3886 if (!coordinate_done) {
3887 for (unsigned i = 0; i < coord_components; i++) {
3888 bld.MOV(sources[length], coordinate);
3889 coordinate = offset(coordinate, bld, 1);
3890 length++;
3891 }
3892 }
3893
3894 int mlen;
3895 if (reg_width == 2)
3896 mlen = length * reg_width - header_size;
3897 else
3898 mlen = length * reg_width;
3899
3900 const fs_reg src_payload = fs_reg(GRF, bld.shader->alloc.allocate(mlen),
3901 BRW_REGISTER_TYPE_F);
3902 bld.LOAD_PAYLOAD(src_payload, sources, length, header_size);
3903
3904 /* Generate the SEND. */
3905 inst->opcode = op;
3906 inst->src[0] = src_payload;
3907 inst->src[1] = sampler;
3908 inst->resize_sources(2);
3909 inst->base_mrf = -1;
3910 inst->mlen = mlen;
3911 inst->header_size = header_size;
3912
3913 /* Message length > MAX_SAMPLER_MESSAGE_SIZE disallowed by hardware. */
3914 assert(inst->mlen <= MAX_SAMPLER_MESSAGE_SIZE);
3915 }
3916
3917 static void
3918 lower_sampler_logical_send(const fs_builder &bld, fs_inst *inst, opcode op)
3919 {
3920 const brw_device_info *devinfo = bld.shader->devinfo;
3921 const fs_reg &coordinate = inst->src[0];
3922 const fs_reg &shadow_c = inst->src[1];
3923 const fs_reg &lod = inst->src[2];
3924 const fs_reg &lod2 = inst->src[3];
3925 const fs_reg &sample_index = inst->src[4];
3926 const fs_reg &mcs = inst->src[5];
3927 const fs_reg &sampler = inst->src[6];
3928 const fs_reg &offset_value = inst->src[7];
3929 assert(inst->src[8].file == IMM && inst->src[9].file == IMM);
3930 const unsigned coord_components = inst->src[8].fixed_hw_reg.dw1.ud;
3931 const unsigned grad_components = inst->src[9].fixed_hw_reg.dw1.ud;
3932
3933 if (devinfo->gen >= 7) {
3934 lower_sampler_logical_send_gen7(bld, inst, op, coordinate,
3935 shadow_c, lod, lod2, sample_index,
3936 mcs, sampler, offset_value,
3937 coord_components, grad_components);
3938 } else if (devinfo->gen >= 5) {
3939 lower_sampler_logical_send_gen5(bld, inst, op, coordinate,
3940 shadow_c, lod, lod2, sample_index,
3941 sampler, offset_value,
3942 coord_components, grad_components);
3943 } else {
3944 lower_sampler_logical_send_gen4(bld, inst, op, coordinate,
3945 shadow_c, lod, lod2, sampler,
3946 coord_components, grad_components);
3947 }
3948 }
3949
3950 /**
3951 * Initialize the header present in some typed and untyped surface
3952 * messages.
3953 */
3954 static fs_reg
3955 emit_surface_header(const fs_builder &bld, const fs_reg &sample_mask)
3956 {
3957 fs_builder ubld = bld.exec_all().group(8, 0);
3958 const fs_reg dst = ubld.vgrf(BRW_REGISTER_TYPE_UD);
3959 ubld.MOV(dst, fs_reg(0));
3960 ubld.MOV(component(dst, 7), sample_mask);
3961 return dst;
3962 }
3963
3964 static void
3965 lower_surface_logical_send(const fs_builder &bld, fs_inst *inst, opcode op,
3966 const fs_reg &sample_mask)
3967 {
3968 /* Get the logical send arguments. */
3969 const fs_reg &addr = inst->src[0];
3970 const fs_reg &src = inst->src[1];
3971 const fs_reg &surface = inst->src[2];
3972 const UNUSED fs_reg &dims = inst->src[3];
3973 const fs_reg &arg = inst->src[4];
3974
3975 /* Calculate the total number of components of the payload. */
3976 const unsigned addr_sz = inst->components_read(0);
3977 const unsigned src_sz = inst->components_read(1);
3978 const unsigned header_sz = (sample_mask.file == BAD_FILE ? 0 : 1);
3979 const unsigned sz = header_sz + addr_sz + src_sz;
3980
3981 /* Allocate space for the payload. */
3982 fs_reg *const components = new fs_reg[sz];
3983 const fs_reg payload = bld.vgrf(BRW_REGISTER_TYPE_UD, sz);
3984 unsigned n = 0;
3985
3986 /* Construct the payload. */
3987 if (header_sz)
3988 components[n++] = emit_surface_header(bld, sample_mask);
3989
3990 for (unsigned i = 0; i < addr_sz; i++)
3991 components[n++] = offset(addr, bld, i);
3992
3993 for (unsigned i = 0; i < src_sz; i++)
3994 components[n++] = offset(src, bld, i);
3995
3996 bld.LOAD_PAYLOAD(payload, components, sz, header_sz);
3997
3998 /* Update the original instruction. */
3999 inst->opcode = op;
4000 inst->mlen = header_sz + (addr_sz + src_sz) * inst->exec_size / 8;
4001 inst->header_size = header_sz;
4002
4003 inst->src[0] = payload;
4004 inst->src[1] = surface;
4005 inst->src[2] = arg;
4006 inst->resize_sources(3);
4007
4008 delete[] components;
4009 }
4010
4011 bool
4012 fs_visitor::lower_logical_sends()
4013 {
4014 bool progress = false;
4015
4016 foreach_block_and_inst_safe(block, fs_inst, inst, cfg) {
4017 const fs_builder ibld(this, block, inst);
4018
4019 switch (inst->opcode) {
4020 case FS_OPCODE_FB_WRITE_LOGICAL:
4021 assert(stage == MESA_SHADER_FRAGMENT);
4022 lower_fb_write_logical_send(ibld, inst,
4023 (const brw_wm_prog_data *)prog_data,
4024 (const brw_wm_prog_key *)key,
4025 payload);
4026 break;
4027
4028 case SHADER_OPCODE_TEX_LOGICAL:
4029 lower_sampler_logical_send(ibld, inst, SHADER_OPCODE_TEX);
4030 break;
4031
4032 case SHADER_OPCODE_TXD_LOGICAL:
4033 lower_sampler_logical_send(ibld, inst, SHADER_OPCODE_TXD);
4034 break;
4035
4036 case SHADER_OPCODE_TXF_LOGICAL:
4037 lower_sampler_logical_send(ibld, inst, SHADER_OPCODE_TXF);
4038 break;
4039
4040 case SHADER_OPCODE_TXL_LOGICAL:
4041 lower_sampler_logical_send(ibld, inst, SHADER_OPCODE_TXL);
4042 break;
4043
4044 case SHADER_OPCODE_TXS_LOGICAL:
4045 lower_sampler_logical_send(ibld, inst, SHADER_OPCODE_TXS);
4046 break;
4047
4048 case FS_OPCODE_TXB_LOGICAL:
4049 lower_sampler_logical_send(ibld, inst, FS_OPCODE_TXB);
4050 break;
4051
4052 case SHADER_OPCODE_TXF_CMS_LOGICAL:
4053 lower_sampler_logical_send(ibld, inst, SHADER_OPCODE_TXF_CMS);
4054 break;
4055
4056 case SHADER_OPCODE_TXF_UMS_LOGICAL:
4057 lower_sampler_logical_send(ibld, inst, SHADER_OPCODE_TXF_UMS);
4058 break;
4059
4060 case SHADER_OPCODE_TXF_MCS_LOGICAL:
4061 lower_sampler_logical_send(ibld, inst, SHADER_OPCODE_TXF_MCS);
4062 break;
4063
4064 case SHADER_OPCODE_LOD_LOGICAL:
4065 lower_sampler_logical_send(ibld, inst, SHADER_OPCODE_LOD);
4066 break;
4067
4068 case SHADER_OPCODE_TG4_LOGICAL:
4069 lower_sampler_logical_send(ibld, inst, SHADER_OPCODE_TG4);
4070 break;
4071
4072 case SHADER_OPCODE_TG4_OFFSET_LOGICAL:
4073 lower_sampler_logical_send(ibld, inst, SHADER_OPCODE_TG4_OFFSET);
4074 break;
4075
4076 case SHADER_OPCODE_UNTYPED_SURFACE_READ_LOGICAL:
4077 lower_surface_logical_send(ibld, inst,
4078 SHADER_OPCODE_UNTYPED_SURFACE_READ,
4079 fs_reg(0xffff));
4080 break;
4081
4082 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE_LOGICAL:
4083 lower_surface_logical_send(ibld, inst,
4084 SHADER_OPCODE_UNTYPED_SURFACE_WRITE,
4085 ibld.sample_mask_reg());
4086 break;
4087
4088 case SHADER_OPCODE_UNTYPED_ATOMIC_LOGICAL:
4089 lower_surface_logical_send(ibld, inst,
4090 SHADER_OPCODE_UNTYPED_ATOMIC,
4091 ibld.sample_mask_reg());
4092 break;
4093
4094 case SHADER_OPCODE_TYPED_SURFACE_READ_LOGICAL:
4095 lower_surface_logical_send(ibld, inst,
4096 SHADER_OPCODE_TYPED_SURFACE_READ,
4097 fs_reg(0xffff));
4098 break;
4099
4100 case SHADER_OPCODE_TYPED_SURFACE_WRITE_LOGICAL:
4101 lower_surface_logical_send(ibld, inst,
4102 SHADER_OPCODE_TYPED_SURFACE_WRITE,
4103 ibld.sample_mask_reg());
4104 break;
4105
4106 case SHADER_OPCODE_TYPED_ATOMIC_LOGICAL:
4107 lower_surface_logical_send(ibld, inst,
4108 SHADER_OPCODE_TYPED_ATOMIC,
4109 ibld.sample_mask_reg());
4110 break;
4111
4112 default:
4113 continue;
4114 }
4115
4116 progress = true;
4117 }
4118
4119 if (progress)
4120 invalidate_live_intervals();
4121
4122 return progress;
4123 }
4124
4125 /**
4126 * Get the closest native SIMD width supported by the hardware for instruction
4127 * \p inst. The instruction will be left untouched by
4128 * fs_visitor::lower_simd_width() if the returned value is equal to the
4129 * original execution size.
4130 */
4131 static unsigned
4132 get_lowered_simd_width(const struct brw_device_info *devinfo,
4133 const fs_inst *inst)
4134 {
4135 switch (inst->opcode) {
4136 case SHADER_OPCODE_MULH:
4137 /* MULH is lowered to the MUL/MACH sequence using the accumulator, which
4138 * is 8-wide on Gen7+.
4139 */
4140 return (devinfo->gen >= 7 ? 8 : inst->exec_size);
4141
4142 case FS_OPCODE_FB_WRITE_LOGICAL:
4143 /* Gen6 doesn't support SIMD16 depth writes but we cannot handle them
4144 * here.
4145 */
4146 assert(devinfo->gen != 6 || inst->src[3].file == BAD_FILE ||
4147 inst->exec_size == 8);
4148 /* Dual-source FB writes are unsupported in SIMD16 mode. */
4149 return (inst->src[1].file != BAD_FILE ? 8 : inst->exec_size);
4150
4151 case SHADER_OPCODE_TXD_LOGICAL:
4152 /* TXD is unsupported in SIMD16 mode. */
4153 return 8;
4154
4155 case SHADER_OPCODE_TG4_OFFSET_LOGICAL: {
4156 /* gather4_po_c is unsupported in SIMD16 mode. */
4157 const fs_reg &shadow_c = inst->src[1];
4158 return (shadow_c.file != BAD_FILE ? 8 : inst->exec_size);
4159 }
4160 case SHADER_OPCODE_TXL_LOGICAL:
4161 case FS_OPCODE_TXB_LOGICAL: {
4162 /* Gen4 doesn't have SIMD8 non-shadow-compare bias/LOD instructions, and
4163 * Gen4-6 can't support TXL and TXB with shadow comparison in SIMD16
4164 * mode because the message exceeds the maximum length of 11.
4165 */
4166 const fs_reg &shadow_c = inst->src[1];
4167 if (devinfo->gen == 4 && shadow_c.file == BAD_FILE)
4168 return 16;
4169 else if (devinfo->gen < 7 && shadow_c.file != BAD_FILE)
4170 return 8;
4171 else
4172 return inst->exec_size;
4173 }
4174 case SHADER_OPCODE_TXF_LOGICAL:
4175 case SHADER_OPCODE_TXS_LOGICAL:
4176 /* Gen4 doesn't have SIMD8 variants for the RESINFO and LD-with-LOD
4177 * messages. Use SIMD16 instead.
4178 */
4179 if (devinfo->gen == 4)
4180 return 16;
4181 else
4182 return inst->exec_size;
4183
4184 case SHADER_OPCODE_TYPED_ATOMIC_LOGICAL:
4185 case SHADER_OPCODE_TYPED_SURFACE_READ_LOGICAL:
4186 case SHADER_OPCODE_TYPED_SURFACE_WRITE_LOGICAL:
4187 return 8;
4188
4189 default:
4190 return inst->exec_size;
4191 }
4192 }
4193
4194 /**
4195 * The \p rows array of registers represents a \p num_rows by \p num_columns
4196 * matrix in row-major order, write it in column-major order into the register
4197 * passed as destination. \p stride gives the separation between matrix
4198 * elements in the input in fs_builder::dispatch_width() units.
4199 */
4200 static void
4201 emit_transpose(const fs_builder &bld,
4202 const fs_reg &dst, const fs_reg *rows,
4203 unsigned num_rows, unsigned num_columns, unsigned stride)
4204 {
4205 fs_reg *const components = new fs_reg[num_rows * num_columns];
4206
4207 for (unsigned i = 0; i < num_columns; ++i) {
4208 for (unsigned j = 0; j < num_rows; ++j)
4209 components[num_rows * i + j] = offset(rows[j], bld, stride * i);
4210 }
4211
4212 bld.LOAD_PAYLOAD(dst, components, num_rows * num_columns, 0);
4213
4214 delete[] components;
4215 }
4216
4217 bool
4218 fs_visitor::lower_simd_width()
4219 {
4220 bool progress = false;
4221
4222 foreach_block_and_inst_safe(block, fs_inst, inst, cfg) {
4223 const unsigned lower_width = get_lowered_simd_width(devinfo, inst);
4224
4225 if (lower_width != inst->exec_size) {
4226 /* Builder matching the original instruction. We may also need to
4227 * emit an instruction of width larger than the original, set the
4228 * execution size of the builder to the highest of both for now so
4229 * we're sure that both cases can be handled.
4230 */
4231 const fs_builder ibld = bld.at(block, inst)
4232 .exec_all(inst->force_writemask_all)
4233 .group(MAX2(inst->exec_size, lower_width),
4234 inst->force_sechalf);
4235
4236 /* Split the copies in chunks of the execution width of either the
4237 * original or the lowered instruction, whichever is lower.
4238 */
4239 const unsigned copy_width = MIN2(lower_width, inst->exec_size);
4240 const unsigned n = inst->exec_size / copy_width;
4241 const unsigned dst_size = inst->regs_written * REG_SIZE /
4242 inst->dst.component_size(inst->exec_size);
4243 fs_reg dsts[4];
4244
4245 assert(n > 0 && n <= ARRAY_SIZE(dsts) &&
4246 !inst->writes_accumulator && !inst->mlen);
4247
4248 for (unsigned i = 0; i < n; i++) {
4249 /* Emit a copy of the original instruction with the lowered width.
4250 * If the EOT flag was set throw it away except for the last
4251 * instruction to avoid killing the thread prematurely.
4252 */
4253 fs_inst split_inst = *inst;
4254 split_inst.exec_size = lower_width;
4255 split_inst.eot = inst->eot && i == n - 1;
4256
4257 /* Select the correct channel enables for the i-th group, then
4258 * transform the sources and destination and emit the lowered
4259 * instruction.
4260 */
4261 const fs_builder lbld = ibld.group(lower_width, i);
4262
4263 for (unsigned j = 0; j < inst->sources; j++) {
4264 if (inst->src[j].file != BAD_FILE &&
4265 !is_uniform(inst->src[j])) {
4266 /* Get the i-th copy_width-wide chunk of the source. */
4267 const fs_reg src = horiz_offset(inst->src[j], copy_width * i);
4268 const unsigned src_size = inst->components_read(j);
4269
4270 /* Use a trivial transposition to copy one every n
4271 * copy_width-wide components of the register into a
4272 * temporary passed as source to the lowered instruction.
4273 */
4274 split_inst.src[j] = lbld.vgrf(inst->src[j].type, src_size);
4275 emit_transpose(lbld.group(copy_width, 0),
4276 split_inst.src[j], &src, 1, src_size, n);
4277 }
4278 }
4279
4280 if (inst->regs_written) {
4281 /* Allocate enough space to hold the result of the lowered
4282 * instruction and fix up the number of registers written.
4283 */
4284 split_inst.dst = dsts[i] =
4285 lbld.vgrf(inst->dst.type, dst_size);
4286 split_inst.regs_written =
4287 DIV_ROUND_UP(inst->regs_written * lower_width,
4288 inst->exec_size);
4289 }
4290
4291 lbld.emit(split_inst);
4292 }
4293
4294 if (inst->regs_written) {
4295 /* Distance between useful channels in the temporaries, skipping
4296 * garbage if the lowered instruction is wider than the original.
4297 */
4298 const unsigned m = lower_width / copy_width;
4299
4300 /* Interleave the components of the result from the lowered
4301 * instructions. We need to set exec_all() when copying more than
4302 * one half per component, because LOAD_PAYLOAD (in terms of which
4303 * emit_transpose is implemented) can only use the same channel
4304 * enable signals for all of its non-header sources.
4305 */
4306 emit_transpose(ibld.exec_all(inst->exec_size > copy_width)
4307 .group(copy_width, 0),
4308 inst->dst, dsts, n, dst_size, m);
4309 }
4310
4311 inst->remove(block);
4312 progress = true;
4313 }
4314 }
4315
4316 if (progress)
4317 invalidate_live_intervals();
4318
4319 return progress;
4320 }
4321
4322 void
4323 fs_visitor::dump_instructions()
4324 {
4325 dump_instructions(NULL);
4326 }
4327
4328 void
4329 fs_visitor::dump_instructions(const char *name)
4330 {
4331 FILE *file = stderr;
4332 if (name && geteuid() != 0) {
4333 file = fopen(name, "w");
4334 if (!file)
4335 file = stderr;
4336 }
4337
4338 if (cfg) {
4339 calculate_register_pressure();
4340 int ip = 0, max_pressure = 0;
4341 foreach_block_and_inst(block, backend_instruction, inst, cfg) {
4342 max_pressure = MAX2(max_pressure, regs_live_at_ip[ip]);
4343 fprintf(file, "{%3d} %4d: ", regs_live_at_ip[ip], ip);
4344 dump_instruction(inst, file);
4345 ip++;
4346 }
4347 fprintf(file, "Maximum %3d registers live at once.\n", max_pressure);
4348 } else {
4349 int ip = 0;
4350 foreach_in_list(backend_instruction, inst, &instructions) {
4351 fprintf(file, "%4d: ", ip++);
4352 dump_instruction(inst, file);
4353 }
4354 }
4355
4356 if (file != stderr) {
4357 fclose(file);
4358 }
4359 }
4360
4361 void
4362 fs_visitor::dump_instruction(backend_instruction *be_inst)
4363 {
4364 dump_instruction(be_inst, stderr);
4365 }
4366
4367 void
4368 fs_visitor::dump_instruction(backend_instruction *be_inst, FILE *file)
4369 {
4370 fs_inst *inst = (fs_inst *)be_inst;
4371
4372 if (inst->predicate) {
4373 fprintf(file, "(%cf0.%d) ",
4374 inst->predicate_inverse ? '-' : '+',
4375 inst->flag_subreg);
4376 }
4377
4378 fprintf(file, "%s", brw_instruction_name(inst->opcode));
4379 if (inst->saturate)
4380 fprintf(file, ".sat");
4381 if (inst->conditional_mod) {
4382 fprintf(file, "%s", conditional_modifier[inst->conditional_mod]);
4383 if (!inst->predicate &&
4384 (devinfo->gen < 5 || (inst->opcode != BRW_OPCODE_SEL &&
4385 inst->opcode != BRW_OPCODE_IF &&
4386 inst->opcode != BRW_OPCODE_WHILE))) {
4387 fprintf(file, ".f0.%d", inst->flag_subreg);
4388 }
4389 }
4390 fprintf(file, "(%d) ", inst->exec_size);
4391
4392 if (inst->mlen) {
4393 fprintf(file, "(mlen: %d) ", inst->mlen);
4394 }
4395
4396 switch (inst->dst.file) {
4397 case GRF:
4398 fprintf(file, "vgrf%d", inst->dst.reg);
4399 if (alloc.sizes[inst->dst.reg] != inst->regs_written ||
4400 inst->dst.subreg_offset)
4401 fprintf(file, "+%d.%d",
4402 inst->dst.reg_offset, inst->dst.subreg_offset);
4403 break;
4404 case MRF:
4405 fprintf(file, "m%d", inst->dst.reg);
4406 break;
4407 case BAD_FILE:
4408 fprintf(file, "(null)");
4409 break;
4410 case UNIFORM:
4411 fprintf(file, "***u%d***", inst->dst.reg + inst->dst.reg_offset);
4412 break;
4413 case ATTR:
4414 fprintf(file, "***attr%d***", inst->dst.reg + inst->dst.reg_offset);
4415 break;
4416 case HW_REG:
4417 if (inst->dst.fixed_hw_reg.file == BRW_ARCHITECTURE_REGISTER_FILE) {
4418 switch (inst->dst.fixed_hw_reg.nr) {
4419 case BRW_ARF_NULL:
4420 fprintf(file, "null");
4421 break;
4422 case BRW_ARF_ADDRESS:
4423 fprintf(file, "a0.%d", inst->dst.fixed_hw_reg.subnr);
4424 break;
4425 case BRW_ARF_ACCUMULATOR:
4426 fprintf(file, "acc%d", inst->dst.fixed_hw_reg.subnr);
4427 break;
4428 case BRW_ARF_FLAG:
4429 fprintf(file, "f%d.%d", inst->dst.fixed_hw_reg.nr & 0xf,
4430 inst->dst.fixed_hw_reg.subnr);
4431 break;
4432 default:
4433 fprintf(file, "arf%d.%d", inst->dst.fixed_hw_reg.nr & 0xf,
4434 inst->dst.fixed_hw_reg.subnr);
4435 break;
4436 }
4437 } else {
4438 fprintf(file, "hw_reg%d", inst->dst.fixed_hw_reg.nr);
4439 }
4440 if (inst->dst.fixed_hw_reg.subnr)
4441 fprintf(file, "+%d", inst->dst.fixed_hw_reg.subnr);
4442 break;
4443 default:
4444 fprintf(file, "???");
4445 break;
4446 }
4447 fprintf(file, ":%s, ", brw_reg_type_letters(inst->dst.type));
4448
4449 for (int i = 0; i < inst->sources; i++) {
4450 if (inst->src[i].negate)
4451 fprintf(file, "-");
4452 if (inst->src[i].abs)
4453 fprintf(file, "|");
4454 switch (inst->src[i].file) {
4455 case GRF:
4456 fprintf(file, "vgrf%d", inst->src[i].reg);
4457 if (alloc.sizes[inst->src[i].reg] != (unsigned)inst->regs_read(i) ||
4458 inst->src[i].subreg_offset)
4459 fprintf(file, "+%d.%d", inst->src[i].reg_offset,
4460 inst->src[i].subreg_offset);
4461 break;
4462 case MRF:
4463 fprintf(file, "***m%d***", inst->src[i].reg);
4464 break;
4465 case ATTR:
4466 fprintf(file, "attr%d", inst->src[i].reg + inst->src[i].reg_offset);
4467 break;
4468 case UNIFORM:
4469 fprintf(file, "u%d", inst->src[i].reg + inst->src[i].reg_offset);
4470 if (inst->src[i].reladdr) {
4471 fprintf(file, "+reladdr");
4472 } else if (inst->src[i].subreg_offset) {
4473 fprintf(file, "+%d.%d", inst->src[i].reg_offset,
4474 inst->src[i].subreg_offset);
4475 }
4476 break;
4477 case BAD_FILE:
4478 fprintf(file, "(null)");
4479 break;
4480 case IMM:
4481 switch (inst->src[i].type) {
4482 case BRW_REGISTER_TYPE_F:
4483 fprintf(file, "%ff", inst->src[i].fixed_hw_reg.dw1.f);
4484 break;
4485 case BRW_REGISTER_TYPE_W:
4486 case BRW_REGISTER_TYPE_D:
4487 fprintf(file, "%dd", inst->src[i].fixed_hw_reg.dw1.d);
4488 break;
4489 case BRW_REGISTER_TYPE_UW:
4490 case BRW_REGISTER_TYPE_UD:
4491 fprintf(file, "%uu", inst->src[i].fixed_hw_reg.dw1.ud);
4492 break;
4493 case BRW_REGISTER_TYPE_VF:
4494 fprintf(file, "[%-gF, %-gF, %-gF, %-gF]",
4495 brw_vf_to_float((inst->src[i].fixed_hw_reg.dw1.ud >> 0) & 0xff),
4496 brw_vf_to_float((inst->src[i].fixed_hw_reg.dw1.ud >> 8) & 0xff),
4497 brw_vf_to_float((inst->src[i].fixed_hw_reg.dw1.ud >> 16) & 0xff),
4498 brw_vf_to_float((inst->src[i].fixed_hw_reg.dw1.ud >> 24) & 0xff));
4499 break;
4500 default:
4501 fprintf(file, "???");
4502 break;
4503 }
4504 break;
4505 case HW_REG:
4506 if (inst->src[i].fixed_hw_reg.negate)
4507 fprintf(file, "-");
4508 if (inst->src[i].fixed_hw_reg.abs)
4509 fprintf(file, "|");
4510 if (inst->src[i].fixed_hw_reg.file == BRW_ARCHITECTURE_REGISTER_FILE) {
4511 switch (inst->src[i].fixed_hw_reg.nr) {
4512 case BRW_ARF_NULL:
4513 fprintf(file, "null");
4514 break;
4515 case BRW_ARF_ADDRESS:
4516 fprintf(file, "a0.%d", inst->src[i].fixed_hw_reg.subnr);
4517 break;
4518 case BRW_ARF_ACCUMULATOR:
4519 fprintf(file, "acc%d", inst->src[i].fixed_hw_reg.subnr);
4520 break;
4521 case BRW_ARF_FLAG:
4522 fprintf(file, "f%d.%d", inst->src[i].fixed_hw_reg.nr & 0xf,
4523 inst->src[i].fixed_hw_reg.subnr);
4524 break;
4525 default:
4526 fprintf(file, "arf%d.%d", inst->src[i].fixed_hw_reg.nr & 0xf,
4527 inst->src[i].fixed_hw_reg.subnr);
4528 break;
4529 }
4530 } else {
4531 fprintf(file, "hw_reg%d", inst->src[i].fixed_hw_reg.nr);
4532 }
4533 if (inst->src[i].fixed_hw_reg.subnr)
4534 fprintf(file, "+%d", inst->src[i].fixed_hw_reg.subnr);
4535 if (inst->src[i].fixed_hw_reg.abs)
4536 fprintf(file, "|");
4537 break;
4538 default:
4539 fprintf(file, "???");
4540 break;
4541 }
4542 if (inst->src[i].abs)
4543 fprintf(file, "|");
4544
4545 if (inst->src[i].file != IMM) {
4546 fprintf(file, ":%s", brw_reg_type_letters(inst->src[i].type));
4547 }
4548
4549 if (i < inst->sources - 1 && inst->src[i + 1].file != BAD_FILE)
4550 fprintf(file, ", ");
4551 }
4552
4553 fprintf(file, " ");
4554
4555 if (dispatch_width == 16 && inst->exec_size == 8) {
4556 if (inst->force_sechalf)
4557 fprintf(file, "2ndhalf ");
4558 else
4559 fprintf(file, "1sthalf ");
4560 }
4561
4562 fprintf(file, "\n");
4563 }
4564
4565 /**
4566 * Possibly returns an instruction that set up @param reg.
4567 *
4568 * Sometimes we want to take the result of some expression/variable
4569 * dereference tree and rewrite the instruction generating the result
4570 * of the tree. When processing the tree, we know that the
4571 * instructions generated are all writing temporaries that are dead
4572 * outside of this tree. So, if we have some instructions that write
4573 * a temporary, we're free to point that temp write somewhere else.
4574 *
4575 * Note that this doesn't guarantee that the instruction generated
4576 * only reg -- it might be the size=4 destination of a texture instruction.
4577 */
4578 fs_inst *
4579 fs_visitor::get_instruction_generating_reg(fs_inst *start,
4580 fs_inst *end,
4581 const fs_reg &reg)
4582 {
4583 if (end == start ||
4584 end->is_partial_write() ||
4585 reg.reladdr ||
4586 !reg.equals(end->dst)) {
4587 return NULL;
4588 } else {
4589 return end;
4590 }
4591 }
4592
4593 void
4594 fs_visitor::setup_payload_gen6()
4595 {
4596 bool uses_depth =
4597 (prog->InputsRead & (1 << VARYING_SLOT_POS)) != 0;
4598 unsigned barycentric_interp_modes =
4599 (stage == MESA_SHADER_FRAGMENT) ?
4600 ((brw_wm_prog_data*) this->prog_data)->barycentric_interp_modes : 0;
4601
4602 assert(devinfo->gen >= 6);
4603
4604 /* R0-1: masks, pixel X/Y coordinates. */
4605 payload.num_regs = 2;
4606 /* R2: only for 32-pixel dispatch.*/
4607
4608 /* R3-26: barycentric interpolation coordinates. These appear in the
4609 * same order that they appear in the brw_wm_barycentric_interp_mode
4610 * enum. Each set of coordinates occupies 2 registers if dispatch width
4611 * == 8 and 4 registers if dispatch width == 16. Coordinates only
4612 * appear if they were enabled using the "Barycentric Interpolation
4613 * Mode" bits in WM_STATE.
4614 */
4615 for (int i = 0; i < BRW_WM_BARYCENTRIC_INTERP_MODE_COUNT; ++i) {
4616 if (barycentric_interp_modes & (1 << i)) {
4617 payload.barycentric_coord_reg[i] = payload.num_regs;
4618 payload.num_regs += 2;
4619 if (dispatch_width == 16) {
4620 payload.num_regs += 2;
4621 }
4622 }
4623 }
4624
4625 /* R27: interpolated depth if uses source depth */
4626 if (uses_depth) {
4627 payload.source_depth_reg = payload.num_regs;
4628 payload.num_regs++;
4629 if (dispatch_width == 16) {
4630 /* R28: interpolated depth if not SIMD8. */
4631 payload.num_regs++;
4632 }
4633 }
4634 /* R29: interpolated W set if GEN6_WM_USES_SOURCE_W. */
4635 if (uses_depth) {
4636 payload.source_w_reg = payload.num_regs;
4637 payload.num_regs++;
4638 if (dispatch_width == 16) {
4639 /* R30: interpolated W if not SIMD8. */
4640 payload.num_regs++;
4641 }
4642 }
4643
4644 if (stage == MESA_SHADER_FRAGMENT) {
4645 brw_wm_prog_data *prog_data = (brw_wm_prog_data*) this->prog_data;
4646 brw_wm_prog_key *key = (brw_wm_prog_key*) this->key;
4647 prog_data->uses_pos_offset = key->compute_pos_offset;
4648 /* R31: MSAA position offsets. */
4649 if (prog_data->uses_pos_offset) {
4650 payload.sample_pos_reg = payload.num_regs;
4651 payload.num_regs++;
4652 }
4653 }
4654
4655 /* R32: MSAA input coverage mask */
4656 if (prog->SystemValuesRead & SYSTEM_BIT_SAMPLE_MASK_IN) {
4657 assert(devinfo->gen >= 7);
4658 payload.sample_mask_in_reg = payload.num_regs;
4659 payload.num_regs++;
4660 if (dispatch_width == 16) {
4661 /* R33: input coverage mask if not SIMD8. */
4662 payload.num_regs++;
4663 }
4664 }
4665
4666 /* R34-: bary for 32-pixel. */
4667 /* R58-59: interp W for 32-pixel. */
4668
4669 if (prog->OutputsWritten & BITFIELD64_BIT(FRAG_RESULT_DEPTH)) {
4670 source_depth_to_render_target = true;
4671 }
4672 }
4673
4674 void
4675 fs_visitor::setup_vs_payload()
4676 {
4677 /* R0: thread header, R1: urb handles */
4678 payload.num_regs = 2;
4679 }
4680
4681 void
4682 fs_visitor::setup_cs_payload()
4683 {
4684 assert(devinfo->gen >= 7);
4685
4686 payload.num_regs = 1;
4687 }
4688
4689 void
4690 fs_visitor::assign_binding_table_offsets()
4691 {
4692 assert(stage == MESA_SHADER_FRAGMENT);
4693 brw_wm_prog_data *prog_data = (brw_wm_prog_data*) this->prog_data;
4694 brw_wm_prog_key *key = (brw_wm_prog_key*) this->key;
4695 uint32_t next_binding_table_offset = 0;
4696
4697 /* If there are no color regions, we still perform an FB write to a null
4698 * renderbuffer, which we place at surface index 0.
4699 */
4700 prog_data->binding_table.render_target_start = next_binding_table_offset;
4701 next_binding_table_offset += MAX2(key->nr_color_regions, 1);
4702
4703 assign_common_binding_table_offsets(next_binding_table_offset);
4704 }
4705
4706 void
4707 fs_visitor::calculate_register_pressure()
4708 {
4709 invalidate_live_intervals();
4710 calculate_live_intervals();
4711
4712 unsigned num_instructions = 0;
4713 foreach_block(block, cfg)
4714 num_instructions += block->instructions.length();
4715
4716 regs_live_at_ip = rzalloc_array(mem_ctx, int, num_instructions);
4717
4718 for (unsigned reg = 0; reg < alloc.count; reg++) {
4719 for (int ip = virtual_grf_start[reg]; ip <= virtual_grf_end[reg]; ip++)
4720 regs_live_at_ip[ip] += alloc.sizes[reg];
4721 }
4722 }
4723
4724 void
4725 fs_visitor::optimize()
4726 {
4727 /* bld is the common builder object pointing at the end of the program we
4728 * used to translate it into i965 IR. For the optimization and lowering
4729 * passes coming next, any code added after the end of the program without
4730 * having explicitly called fs_builder::at() clearly points at a mistake.
4731 * Ideally optimization passes wouldn't be part of the visitor so they
4732 * wouldn't have access to bld at all, but they do, so just in case some
4733 * pass forgets to ask for a location explicitly set it to NULL here to
4734 * make it trip. The dispatch width is initialized to a bogus value to
4735 * make sure that optimizations set the execution controls explicitly to
4736 * match the code they are manipulating instead of relying on the defaults.
4737 */
4738 bld = fs_builder(this, 64);
4739
4740 split_virtual_grfs();
4741
4742 move_uniform_array_access_to_pull_constants();
4743 assign_constant_locations();
4744 demote_pull_constants();
4745
4746 #define OPT(pass, args...) ({ \
4747 pass_num++; \
4748 bool this_progress = pass(args); \
4749 \
4750 if (unlikely(INTEL_DEBUG & DEBUG_OPTIMIZER) && this_progress) { \
4751 char filename[64]; \
4752 snprintf(filename, 64, "%s%d-%04d-%02d-%02d-" #pass, \
4753 stage_abbrev, dispatch_width, shader_prog ? shader_prog->Name : 0, iteration, pass_num); \
4754 \
4755 backend_shader::dump_instructions(filename); \
4756 } \
4757 \
4758 progress = progress || this_progress; \
4759 this_progress; \
4760 })
4761
4762 if (unlikely(INTEL_DEBUG & DEBUG_OPTIMIZER)) {
4763 char filename[64];
4764 snprintf(filename, 64, "%s%d-%04d-00-start",
4765 stage_abbrev, dispatch_width,
4766 shader_prog ? shader_prog->Name : 0);
4767
4768 backend_shader::dump_instructions(filename);
4769 }
4770
4771 bool progress = false;
4772 int iteration = 0;
4773 int pass_num = 0;
4774
4775 OPT(lower_simd_width);
4776 OPT(lower_logical_sends);
4777
4778 do {
4779 progress = false;
4780 pass_num = 0;
4781 iteration++;
4782
4783 OPT(remove_duplicate_mrf_writes);
4784
4785 OPT(opt_algebraic);
4786 OPT(opt_cse);
4787 OPT(opt_copy_propagate);
4788 OPT(opt_peephole_predicated_break);
4789 OPT(opt_cmod_propagation);
4790 OPT(dead_code_eliminate);
4791 OPT(opt_peephole_sel);
4792 OPT(dead_control_flow_eliminate, this);
4793 OPT(opt_register_renaming);
4794 OPT(opt_redundant_discard_jumps);
4795 OPT(opt_saturate_propagation);
4796 OPT(opt_zero_samples);
4797 OPT(register_coalesce);
4798 OPT(compute_to_mrf);
4799 OPT(eliminate_find_live_channel);
4800
4801 OPT(compact_virtual_grfs);
4802 } while (progress);
4803
4804 pass_num = 0;
4805
4806 OPT(opt_sampler_eot);
4807
4808 if (OPT(lower_load_payload)) {
4809 split_virtual_grfs();
4810 OPT(register_coalesce);
4811 OPT(compute_to_mrf);
4812 OPT(dead_code_eliminate);
4813 }
4814
4815 OPT(opt_combine_constants);
4816 OPT(lower_integer_multiplication);
4817
4818 lower_uniform_pull_constant_loads();
4819 }
4820
4821 /**
4822 * Three source instruction must have a GRF/MRF destination register.
4823 * ARF NULL is not allowed. Fix that up by allocating a temporary GRF.
4824 */
4825 void
4826 fs_visitor::fixup_3src_null_dest()
4827 {
4828 foreach_block_and_inst_safe (block, fs_inst, inst, cfg) {
4829 if (inst->is_3src() && inst->dst.is_null()) {
4830 inst->dst = fs_reg(GRF, alloc.allocate(dispatch_width / 8),
4831 inst->dst.type);
4832 }
4833 }
4834 }
4835
4836 void
4837 fs_visitor::allocate_registers()
4838 {
4839 bool allocated_without_spills;
4840
4841 static const enum instruction_scheduler_mode pre_modes[] = {
4842 SCHEDULE_PRE,
4843 SCHEDULE_PRE_NON_LIFO,
4844 SCHEDULE_PRE_LIFO,
4845 };
4846
4847 /* Try each scheduling heuristic to see if it can successfully register
4848 * allocate without spilling. They should be ordered by decreasing
4849 * performance but increasing likelihood of allocating.
4850 */
4851 for (unsigned i = 0; i < ARRAY_SIZE(pre_modes); i++) {
4852 schedule_instructions(pre_modes[i]);
4853
4854 if (0) {
4855 assign_regs_trivial();
4856 allocated_without_spills = true;
4857 } else {
4858 allocated_without_spills = assign_regs(false);
4859 }
4860 if (allocated_without_spills)
4861 break;
4862 }
4863
4864 if (!allocated_without_spills) {
4865 /* We assume that any spilling is worse than just dropping back to
4866 * SIMD8. There's probably actually some intermediate point where
4867 * SIMD16 with a couple of spills is still better.
4868 */
4869 if (dispatch_width == 16) {
4870 fail("Failure to register allocate. Reduce number of "
4871 "live scalar values to avoid this.");
4872 } else {
4873 compiler->shader_perf_log(log_data,
4874 "%s shader triggered register spilling. "
4875 "Try reducing the number of live scalar "
4876 "values to improve performance.\n",
4877 stage_name);
4878 }
4879
4880 /* Since we're out of heuristics, just go spill registers until we
4881 * get an allocation.
4882 */
4883 while (!assign_regs(true)) {
4884 if (failed)
4885 break;
4886 }
4887 }
4888
4889 /* This must come after all optimization and register allocation, since
4890 * it inserts dead code that happens to have side effects, and it does
4891 * so based on the actual physical registers in use.
4892 */
4893 insert_gen4_send_dependency_workarounds();
4894
4895 if (failed)
4896 return;
4897
4898 if (!allocated_without_spills)
4899 schedule_instructions(SCHEDULE_POST);
4900
4901 if (last_scratch > 0)
4902 prog_data->total_scratch = brw_get_scratch_size(last_scratch);
4903 }
4904
4905 bool
4906 fs_visitor::run_vs(gl_clip_plane *clip_planes)
4907 {
4908 assert(stage == MESA_SHADER_VERTEX);
4909
4910 assign_common_binding_table_offsets(0);
4911 setup_vs_payload();
4912
4913 if (shader_time_index >= 0)
4914 emit_shader_time_begin();
4915
4916 emit_nir_code();
4917
4918 if (failed)
4919 return false;
4920
4921 compute_clip_distance(clip_planes);
4922
4923 emit_urb_writes();
4924
4925 if (shader_time_index >= 0)
4926 emit_shader_time_end();
4927
4928 calculate_cfg();
4929
4930 optimize();
4931
4932 assign_curb_setup();
4933 assign_vs_urb_setup();
4934
4935 fixup_3src_null_dest();
4936 allocate_registers();
4937
4938 return !failed;
4939 }
4940
4941 bool
4942 fs_visitor::run_fs(bool do_rep_send)
4943 {
4944 brw_wm_prog_data *wm_prog_data = (brw_wm_prog_data *) this->prog_data;
4945 brw_wm_prog_key *wm_key = (brw_wm_prog_key *) this->key;
4946
4947 assert(stage == MESA_SHADER_FRAGMENT);
4948
4949 sanity_param_count = prog->Parameters->NumParameters;
4950
4951 assign_binding_table_offsets();
4952
4953 if (devinfo->gen >= 6)
4954 setup_payload_gen6();
4955 else
4956 setup_payload_gen4();
4957
4958 if (0) {
4959 emit_dummy_fs();
4960 } else if (do_rep_send) {
4961 assert(dispatch_width == 16);
4962 emit_repclear_shader();
4963 } else {
4964 if (shader_time_index >= 0)
4965 emit_shader_time_begin();
4966
4967 calculate_urb_setup();
4968 if (prog->InputsRead > 0) {
4969 if (devinfo->gen < 6)
4970 emit_interpolation_setup_gen4();
4971 else
4972 emit_interpolation_setup_gen6();
4973 }
4974
4975 /* We handle discards by keeping track of the still-live pixels in f0.1.
4976 * Initialize it with the dispatched pixels.
4977 */
4978 if (wm_prog_data->uses_kill) {
4979 fs_inst *discard_init = bld.emit(FS_OPCODE_MOV_DISPATCH_TO_FLAGS);
4980 discard_init->flag_subreg = 1;
4981 }
4982
4983 /* Generate FS IR for main(). (the visitor only descends into
4984 * functions called "main").
4985 */
4986 emit_nir_code();
4987
4988 if (failed)
4989 return false;
4990
4991 if (wm_prog_data->uses_kill)
4992 bld.emit(FS_OPCODE_PLACEHOLDER_HALT);
4993
4994 if (wm_key->alpha_test_func)
4995 emit_alpha_test();
4996
4997 emit_fb_writes();
4998
4999 if (shader_time_index >= 0)
5000 emit_shader_time_end();
5001
5002 calculate_cfg();
5003
5004 optimize();
5005
5006 assign_curb_setup();
5007 assign_urb_setup();
5008
5009 fixup_3src_null_dest();
5010 allocate_registers();
5011
5012 if (failed)
5013 return false;
5014 }
5015
5016 if (dispatch_width == 8)
5017 wm_prog_data->reg_blocks = brw_register_blocks(grf_used);
5018 else
5019 wm_prog_data->reg_blocks_16 = brw_register_blocks(grf_used);
5020
5021 /* If any state parameters were appended, then ParameterValues could have
5022 * been realloced, in which case the driver uniform storage set up by
5023 * _mesa_associate_uniform_storage() would point to freed memory. Make
5024 * sure that didn't happen.
5025 */
5026 assert(sanity_param_count == prog->Parameters->NumParameters);
5027
5028 return !failed;
5029 }
5030
5031 bool
5032 fs_visitor::run_cs()
5033 {
5034 assert(stage == MESA_SHADER_COMPUTE);
5035 assert(shader);
5036
5037 sanity_param_count = prog->Parameters->NumParameters;
5038
5039 assign_common_binding_table_offsets(0);
5040
5041 setup_cs_payload();
5042
5043 if (shader_time_index >= 0)
5044 emit_shader_time_begin();
5045
5046 emit_nir_code();
5047
5048 if (failed)
5049 return false;
5050
5051 emit_cs_terminate();
5052
5053 if (shader_time_index >= 0)
5054 emit_shader_time_end();
5055
5056 calculate_cfg();
5057
5058 optimize();
5059
5060 assign_curb_setup();
5061
5062 fixup_3src_null_dest();
5063 allocate_registers();
5064
5065 if (failed)
5066 return false;
5067
5068 /* If any state parameters were appended, then ParameterValues could have
5069 * been realloced, in which case the driver uniform storage set up by
5070 * _mesa_associate_uniform_storage() would point to freed memory. Make
5071 * sure that didn't happen.
5072 */
5073 assert(sanity_param_count == prog->Parameters->NumParameters);
5074
5075 return !failed;
5076 }
5077
5078 const unsigned *
5079 brw_wm_fs_emit(struct brw_context *brw,
5080 void *mem_ctx,
5081 const struct brw_wm_prog_key *key,
5082 struct brw_wm_prog_data *prog_data,
5083 struct gl_fragment_program *fp,
5084 struct gl_shader_program *prog,
5085 unsigned *final_assembly_size)
5086 {
5087 bool start_busy = false;
5088 double start_time = 0;
5089
5090 if (unlikely(brw->perf_debug)) {
5091 start_busy = (brw->batch.last_bo &&
5092 drm_intel_bo_busy(brw->batch.last_bo));
5093 start_time = get_time();
5094 }
5095
5096 struct brw_shader *shader = NULL;
5097 if (prog)
5098 shader = (brw_shader *) prog->_LinkedShaders[MESA_SHADER_FRAGMENT];
5099
5100 if (unlikely(INTEL_DEBUG & DEBUG_WM))
5101 brw_dump_ir("fragment", prog, &shader->base, &fp->Base);
5102
5103 int st_index8 = -1, st_index16 = -1;
5104 if (INTEL_DEBUG & DEBUG_SHADER_TIME) {
5105 st_index8 = brw_get_shader_time_index(brw, prog, &fp->Base, ST_FS8);
5106 st_index16 = brw_get_shader_time_index(brw, prog, &fp->Base, ST_FS16);
5107 }
5108
5109 /* Now the main event: Visit the shader IR and generate our FS IR for it.
5110 */
5111 fs_visitor v(brw->intelScreen->compiler, brw,
5112 mem_ctx, MESA_SHADER_FRAGMENT, key, &prog_data->base,
5113 prog, &fp->Base, 8, st_index8);
5114 if (!v.run_fs(false /* do_rep_send */)) {
5115 if (prog) {
5116 prog->LinkStatus = false;
5117 ralloc_strcat(&prog->InfoLog, v.fail_msg);
5118 }
5119
5120 _mesa_problem(NULL, "Failed to compile fragment shader: %s\n",
5121 v.fail_msg);
5122
5123 return NULL;
5124 }
5125
5126 cfg_t *simd16_cfg = NULL;
5127 fs_visitor v2(brw->intelScreen->compiler, brw,
5128 mem_ctx, MESA_SHADER_FRAGMENT, key, &prog_data->base,
5129 prog, &fp->Base, 16, st_index16);
5130 if (likely(!(INTEL_DEBUG & DEBUG_NO16) || brw->use_rep_send)) {
5131 if (!v.simd16_unsupported) {
5132 /* Try a SIMD16 compile */
5133 v2.import_uniforms(&v);
5134 if (!v2.run_fs(brw->use_rep_send)) {
5135 perf_debug("SIMD16 shader failed to compile: %s", v2.fail_msg);
5136 } else {
5137 simd16_cfg = v2.cfg;
5138 }
5139 }
5140 }
5141
5142 cfg_t *simd8_cfg;
5143 int no_simd8 = (INTEL_DEBUG & DEBUG_NO8) || brw->no_simd8;
5144 if ((no_simd8 || brw->gen < 5) && simd16_cfg) {
5145 simd8_cfg = NULL;
5146 prog_data->no_8 = true;
5147 } else {
5148 simd8_cfg = v.cfg;
5149 prog_data->no_8 = false;
5150 }
5151
5152 fs_generator g(brw->intelScreen->compiler, brw,
5153 mem_ctx, (void *) key, &prog_data->base,
5154 &fp->Base, v.promoted_constants, v.runtime_check_aads_emit, "FS");
5155
5156 if (unlikely(INTEL_DEBUG & DEBUG_WM)) {
5157 char *name;
5158 if (prog)
5159 name = ralloc_asprintf(mem_ctx, "%s fragment shader %d",
5160 prog->Label ? prog->Label : "unnamed",
5161 prog->Name);
5162 else
5163 name = ralloc_asprintf(mem_ctx, "fragment program %d", fp->Base.Id);
5164
5165 g.enable_debug(name);
5166 }
5167
5168 if (simd8_cfg)
5169 g.generate_code(simd8_cfg, 8);
5170 if (simd16_cfg)
5171 prog_data->prog_offset_16 = g.generate_code(simd16_cfg, 16);
5172
5173 if (unlikely(brw->perf_debug) && shader) {
5174 if (shader->compiled_once)
5175 brw_wm_debug_recompile(brw, prog, key);
5176 shader->compiled_once = true;
5177
5178 if (start_busy && !drm_intel_bo_busy(brw->batch.last_bo)) {
5179 perf_debug("FS compile took %.03f ms and stalled the GPU\n",
5180 (get_time() - start_time) * 1000);
5181 }
5182 }
5183
5184 return g.get_assembly(final_assembly_size);
5185 }
5186
5187 extern "C" bool
5188 brw_fs_precompile(struct gl_context *ctx,
5189 struct gl_shader_program *shader_prog,
5190 struct gl_program *prog)
5191 {
5192 struct brw_context *brw = brw_context(ctx);
5193 struct brw_wm_prog_key key;
5194
5195 struct gl_fragment_program *fp = (struct gl_fragment_program *) prog;
5196 struct brw_fragment_program *bfp = brw_fragment_program(fp);
5197 bool program_uses_dfdy = fp->UsesDFdy;
5198
5199 memset(&key, 0, sizeof(key));
5200
5201 if (brw->gen < 6) {
5202 if (fp->UsesKill)
5203 key.iz_lookup |= IZ_PS_KILL_ALPHATEST_BIT;
5204
5205 if (fp->Base.OutputsWritten & BITFIELD64_BIT(FRAG_RESULT_DEPTH))
5206 key.iz_lookup |= IZ_PS_COMPUTES_DEPTH_BIT;
5207
5208 /* Just assume depth testing. */
5209 key.iz_lookup |= IZ_DEPTH_TEST_ENABLE_BIT;
5210 key.iz_lookup |= IZ_DEPTH_WRITE_ENABLE_BIT;
5211 }
5212
5213 if (brw->gen < 6 || _mesa_bitcount_64(fp->Base.InputsRead &
5214 BRW_FS_VARYING_INPUT_MASK) > 16)
5215 key.input_slots_valid = fp->Base.InputsRead | VARYING_BIT_POS;
5216
5217 brw_setup_tex_for_precompile(brw, &key.tex, &fp->Base);
5218
5219 if (fp->Base.InputsRead & VARYING_BIT_POS) {
5220 key.drawable_height = ctx->DrawBuffer->Height;
5221 }
5222
5223 key.nr_color_regions = _mesa_bitcount_64(fp->Base.OutputsWritten &
5224 ~(BITFIELD64_BIT(FRAG_RESULT_DEPTH) |
5225 BITFIELD64_BIT(FRAG_RESULT_SAMPLE_MASK)));
5226
5227 if ((fp->Base.InputsRead & VARYING_BIT_POS) || program_uses_dfdy) {
5228 key.render_to_fbo = _mesa_is_user_fbo(ctx->DrawBuffer) ||
5229 key.nr_color_regions > 1;
5230 }
5231
5232 key.program_string_id = bfp->id;
5233
5234 uint32_t old_prog_offset = brw->wm.base.prog_offset;
5235 struct brw_wm_prog_data *old_prog_data = brw->wm.prog_data;
5236
5237 bool success = brw_codegen_wm_prog(brw, shader_prog, bfp, &key);
5238
5239 brw->wm.base.prog_offset = old_prog_offset;
5240 brw->wm.prog_data = old_prog_data;
5241
5242 return success;
5243 }
5244
5245 void
5246 brw_setup_tex_for_precompile(struct brw_context *brw,
5247 struct brw_sampler_prog_key_data *tex,
5248 struct gl_program *prog)
5249 {
5250 const bool has_shader_channel_select = brw->is_haswell || brw->gen >= 8;
5251 unsigned sampler_count = _mesa_fls(prog->SamplersUsed);
5252 for (unsigned i = 0; i < sampler_count; i++) {
5253 if (!has_shader_channel_select && (prog->ShadowSamplers & (1 << i))) {
5254 /* Assume DEPTH_TEXTURE_MODE is the default: X, X, X, 1 */
5255 tex->swizzles[i] =
5256 MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_X, SWIZZLE_X, SWIZZLE_ONE);
5257 } else {
5258 /* Color sampler: assume no swizzling. */
5259 tex->swizzles[i] = SWIZZLE_XYZW;
5260 }
5261 }
5262 }