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