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