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