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