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