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