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