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