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