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