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