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