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