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