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