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