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