162b481573d6a7d20f55879fc88dd6a77ba06d54
[mesa.git] / src / mesa / drivers / dri / i965 / brw_vec4.cpp
1 /*
2 * Copyright © 2011 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 #include "brw_vec4.h"
25 #include "brw_fs.h"
26 #include "brw_cfg.h"
27 #include "brw_vs.h"
28 #include "brw_nir.h"
29 #include "brw_vec4_builder.h"
30 #include "brw_vec4_live_variables.h"
31 #include "brw_dead_control_flow.h"
32 #include "program/prog_parameter.h"
33
34 #define MAX_INSTRUCTION (1 << 30)
35
36 using namespace brw;
37
38 namespace brw {
39
40 void
41 src_reg::init()
42 {
43 memset(this, 0, sizeof(*this));
44
45 this->file = BAD_FILE;
46 }
47
48 src_reg::src_reg(enum brw_reg_file file, int nr, const glsl_type *type)
49 {
50 init();
51
52 this->file = file;
53 this->nr = nr;
54 if (type && (type->is_scalar() || type->is_vector() || type->is_matrix()))
55 this->swizzle = brw_swizzle_for_size(type->vector_elements);
56 else
57 this->swizzle = BRW_SWIZZLE_XYZW;
58 if (type)
59 this->type = brw_type_for_base_type(type);
60 }
61
62 /** Generic unset register constructor. */
63 src_reg::src_reg()
64 {
65 init();
66 }
67
68 src_reg::src_reg(struct ::brw_reg reg) :
69 backend_reg(reg)
70 {
71 this->reg_offset = 0;
72 this->reladdr = NULL;
73 }
74
75 src_reg::src_reg(const dst_reg &reg) :
76 backend_reg(reg)
77 {
78 this->reladdr = reg.reladdr;
79 this->swizzle = brw_swizzle_for_mask(reg.writemask);
80 }
81
82 void
83 dst_reg::init()
84 {
85 memset(this, 0, sizeof(*this));
86 this->file = BAD_FILE;
87 this->writemask = WRITEMASK_XYZW;
88 }
89
90 dst_reg::dst_reg()
91 {
92 init();
93 }
94
95 dst_reg::dst_reg(enum brw_reg_file file, int nr)
96 {
97 init();
98
99 this->file = file;
100 this->nr = nr;
101 }
102
103 dst_reg::dst_reg(enum brw_reg_file file, int nr, const glsl_type *type,
104 unsigned writemask)
105 {
106 init();
107
108 this->file = file;
109 this->nr = nr;
110 this->type = brw_type_for_base_type(type);
111 this->writemask = writemask;
112 }
113
114 dst_reg::dst_reg(enum brw_reg_file file, int nr, brw_reg_type type,
115 unsigned writemask)
116 {
117 init();
118
119 this->file = file;
120 this->nr = nr;
121 this->type = type;
122 this->writemask = writemask;
123 }
124
125 dst_reg::dst_reg(struct ::brw_reg reg) :
126 backend_reg(reg)
127 {
128 this->reg_offset = 0;
129 this->reladdr = NULL;
130 }
131
132 dst_reg::dst_reg(const src_reg &reg) :
133 backend_reg(reg)
134 {
135 this->writemask = brw_mask_for_swizzle(reg.swizzle);
136 this->reladdr = reg.reladdr;
137 }
138
139 bool
140 dst_reg::equals(const dst_reg &r) const
141 {
142 return (this->backend_reg::equals(r) &&
143 (reladdr == r.reladdr ||
144 (reladdr && r.reladdr && reladdr->equals(*r.reladdr))));
145 }
146
147 bool
148 vec4_instruction::is_send_from_grf()
149 {
150 switch (opcode) {
151 case SHADER_OPCODE_SHADER_TIME_ADD:
152 case VS_OPCODE_PULL_CONSTANT_LOAD_GEN7:
153 case SHADER_OPCODE_UNTYPED_ATOMIC:
154 case SHADER_OPCODE_UNTYPED_SURFACE_READ:
155 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE:
156 case SHADER_OPCODE_TYPED_ATOMIC:
157 case SHADER_OPCODE_TYPED_SURFACE_READ:
158 case SHADER_OPCODE_TYPED_SURFACE_WRITE:
159 case VEC4_OPCODE_URB_READ:
160 case TCS_OPCODE_URB_WRITE:
161 case TCS_OPCODE_RELEASE_INPUT:
162 case SHADER_OPCODE_BARRIER:
163 return true;
164 default:
165 return false;
166 }
167 }
168
169 /**
170 * Returns true if this instruction's sources and destinations cannot
171 * safely be the same register.
172 *
173 * In most cases, a register can be written over safely by the same
174 * instruction that is its last use. For a single instruction, the
175 * sources are dereferenced before writing of the destination starts
176 * (naturally).
177 *
178 * However, there are a few cases where this can be problematic:
179 *
180 * - Virtual opcodes that translate to multiple instructions in the
181 * code generator: if src == dst and one instruction writes the
182 * destination before a later instruction reads the source, then
183 * src will have been clobbered.
184 *
185 * The register allocator uses this information to set up conflicts between
186 * GRF sources and the destination.
187 */
188 bool
189 vec4_instruction::has_source_and_destination_hazard() const
190 {
191 switch (opcode) {
192 case TCS_OPCODE_SET_INPUT_URB_OFFSETS:
193 case TCS_OPCODE_SET_OUTPUT_URB_OFFSETS:
194 case TES_OPCODE_ADD_INDIRECT_URB_OFFSET:
195 return true;
196 default:
197 return false;
198 }
199 }
200
201 unsigned
202 vec4_instruction::regs_read(unsigned arg) const
203 {
204 if (src[arg].file == BAD_FILE)
205 return 0;
206
207 switch (opcode) {
208 case SHADER_OPCODE_SHADER_TIME_ADD:
209 case SHADER_OPCODE_UNTYPED_ATOMIC:
210 case SHADER_OPCODE_UNTYPED_SURFACE_READ:
211 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE:
212 case SHADER_OPCODE_TYPED_ATOMIC:
213 case SHADER_OPCODE_TYPED_SURFACE_READ:
214 case SHADER_OPCODE_TYPED_SURFACE_WRITE:
215 case TCS_OPCODE_URB_WRITE:
216 return arg == 0 ? mlen : 1;
217
218 case VS_OPCODE_PULL_CONSTANT_LOAD_GEN7:
219 return arg == 1 ? mlen : 1;
220
221 default:
222 return 1;
223 }
224 }
225
226 bool
227 vec4_instruction::can_do_source_mods(const struct brw_device_info *devinfo)
228 {
229 if (devinfo->gen == 6 && is_math())
230 return false;
231
232 if (is_send_from_grf())
233 return false;
234
235 if (!backend_instruction::can_do_source_mods())
236 return false;
237
238 return true;
239 }
240
241 bool
242 vec4_instruction::can_do_writemask(const struct brw_device_info *devinfo)
243 {
244 switch (opcode) {
245 case SHADER_OPCODE_GEN4_SCRATCH_READ:
246 case VS_OPCODE_PULL_CONSTANT_LOAD:
247 case VS_OPCODE_PULL_CONSTANT_LOAD_GEN7:
248 case VS_OPCODE_SET_SIMD4X2_HEADER_GEN9:
249 case TCS_OPCODE_SET_INPUT_URB_OFFSETS:
250 case TCS_OPCODE_SET_OUTPUT_URB_OFFSETS:
251 case TES_OPCODE_CREATE_INPUT_READ_HEADER:
252 case TES_OPCODE_ADD_INDIRECT_URB_OFFSET:
253 case VEC4_OPCODE_URB_READ:
254 case SHADER_OPCODE_MOV_INDIRECT:
255 return false;
256 default:
257 /* The MATH instruction on Gen6 only executes in align1 mode, which does
258 * not support writemasking.
259 */
260 if (devinfo->gen == 6 && is_math())
261 return false;
262
263 if (is_tex())
264 return false;
265
266 return true;
267 }
268 }
269
270 bool
271 vec4_instruction::can_change_types() const
272 {
273 return dst.type == src[0].type &&
274 !src[0].abs && !src[0].negate && !saturate &&
275 (opcode == BRW_OPCODE_MOV ||
276 (opcode == BRW_OPCODE_SEL &&
277 dst.type == src[1].type &&
278 predicate != BRW_PREDICATE_NONE &&
279 !src[1].abs && !src[1].negate));
280 }
281
282 /**
283 * Returns how many MRFs an opcode will write over.
284 *
285 * Note that this is not the 0 or 1 implied writes in an actual gen
286 * instruction -- the generate_* functions generate additional MOVs
287 * for setup.
288 */
289 int
290 vec4_visitor::implied_mrf_writes(vec4_instruction *inst)
291 {
292 if (inst->mlen == 0 || inst->is_send_from_grf())
293 return 0;
294
295 switch (inst->opcode) {
296 case SHADER_OPCODE_RCP:
297 case SHADER_OPCODE_RSQ:
298 case SHADER_OPCODE_SQRT:
299 case SHADER_OPCODE_EXP2:
300 case SHADER_OPCODE_LOG2:
301 case SHADER_OPCODE_SIN:
302 case SHADER_OPCODE_COS:
303 return 1;
304 case SHADER_OPCODE_INT_QUOTIENT:
305 case SHADER_OPCODE_INT_REMAINDER:
306 case SHADER_OPCODE_POW:
307 case TCS_OPCODE_THREAD_END:
308 return 2;
309 case VS_OPCODE_URB_WRITE:
310 return 1;
311 case VS_OPCODE_PULL_CONSTANT_LOAD:
312 return 2;
313 case SHADER_OPCODE_GEN4_SCRATCH_READ:
314 return 2;
315 case SHADER_OPCODE_GEN4_SCRATCH_WRITE:
316 return 3;
317 case GS_OPCODE_URB_WRITE:
318 case GS_OPCODE_URB_WRITE_ALLOCATE:
319 case GS_OPCODE_THREAD_END:
320 return 0;
321 case GS_OPCODE_FF_SYNC:
322 return 1;
323 case TCS_OPCODE_URB_WRITE:
324 return 0;
325 case SHADER_OPCODE_SHADER_TIME_ADD:
326 return 0;
327 case SHADER_OPCODE_TEX:
328 case SHADER_OPCODE_TXL:
329 case SHADER_OPCODE_TXD:
330 case SHADER_OPCODE_TXF:
331 case SHADER_OPCODE_TXF_CMS:
332 case SHADER_OPCODE_TXF_CMS_W:
333 case SHADER_OPCODE_TXF_MCS:
334 case SHADER_OPCODE_TXS:
335 case SHADER_OPCODE_TG4:
336 case SHADER_OPCODE_TG4_OFFSET:
337 case SHADER_OPCODE_SAMPLEINFO:
338 case VS_OPCODE_GET_BUFFER_SIZE:
339 return inst->header_size;
340 default:
341 unreachable("not reached");
342 }
343 }
344
345 bool
346 src_reg::equals(const src_reg &r) const
347 {
348 return (this->backend_reg::equals(r) &&
349 !reladdr && !r.reladdr);
350 }
351
352 bool
353 vec4_visitor::opt_vector_float()
354 {
355 bool progress = false;
356
357 int last_reg = -1, last_reg_offset = -1;
358 enum brw_reg_file last_reg_file = BAD_FILE;
359
360 uint8_t imm[4] = { 0 };
361 int inst_count = 0;
362 vec4_instruction *imm_inst[4];
363 unsigned writemask = 0;
364 enum brw_reg_type dest_type = BRW_REGISTER_TYPE_F;
365
366 foreach_block_and_inst_safe(block, vec4_instruction, inst, cfg) {
367 int vf = -1;
368 enum brw_reg_type need_type;
369
370 /* Look for unconditional MOVs from an immediate with a partial
371 * writemask. Skip type-conversion MOVs other than integer 0,
372 * where the type doesn't matter. See if the immediate can be
373 * represented as a VF.
374 */
375 if (inst->opcode == BRW_OPCODE_MOV &&
376 inst->src[0].file == IMM &&
377 inst->predicate == BRW_PREDICATE_NONE &&
378 inst->dst.writemask != WRITEMASK_XYZW &&
379 (inst->src[0].type == inst->dst.type || inst->src[0].d == 0)) {
380
381 vf = brw_float_to_vf(inst->src[0].d);
382 need_type = BRW_REGISTER_TYPE_D;
383
384 if (vf == -1) {
385 vf = brw_float_to_vf(inst->src[0].f);
386 need_type = BRW_REGISTER_TYPE_F;
387 }
388 } else {
389 last_reg = -1;
390 }
391
392 /* If this wasn't a MOV, or the destination register doesn't match,
393 * or we have to switch destination types, then this breaks our
394 * sequence. Combine anything we've accumulated so far.
395 */
396 if (last_reg != inst->dst.nr ||
397 last_reg_offset != inst->dst.reg_offset ||
398 last_reg_file != inst->dst.file ||
399 (vf > 0 && dest_type != need_type)) {
400
401 if (inst_count > 1) {
402 unsigned vf;
403 memcpy(&vf, imm, sizeof(vf));
404 vec4_instruction *mov = MOV(imm_inst[0]->dst, brw_imm_vf(vf));
405 mov->dst.type = dest_type;
406 mov->dst.writemask = writemask;
407 inst->insert_before(block, mov);
408
409 for (int i = 0; i < inst_count; i++) {
410 imm_inst[i]->remove(block);
411 }
412
413 progress = true;
414 }
415
416 inst_count = 0;
417 last_reg = -1;
418 writemask = 0;
419 dest_type = BRW_REGISTER_TYPE_F;
420
421 for (int i = 0; i < 4; i++) {
422 imm[i] = 0;
423 }
424 }
425
426 /* Record this instruction's value (if it was representable). */
427 if (vf != -1) {
428 if ((inst->dst.writemask & WRITEMASK_X) != 0)
429 imm[0] = vf;
430 if ((inst->dst.writemask & WRITEMASK_Y) != 0)
431 imm[1] = vf;
432 if ((inst->dst.writemask & WRITEMASK_Z) != 0)
433 imm[2] = vf;
434 if ((inst->dst.writemask & WRITEMASK_W) != 0)
435 imm[3] = vf;
436
437 writemask |= inst->dst.writemask;
438 imm_inst[inst_count++] = inst;
439
440 last_reg = inst->dst.nr;
441 last_reg_offset = inst->dst.reg_offset;
442 last_reg_file = inst->dst.file;
443 if (vf > 0)
444 dest_type = need_type;
445 }
446 }
447
448 if (progress)
449 invalidate_live_intervals();
450
451 return progress;
452 }
453
454 /* Replaces unused channels of a swizzle with channels that are used.
455 *
456 * For instance, this pass transforms
457 *
458 * mov vgrf4.yz, vgrf5.wxzy
459 *
460 * into
461 *
462 * mov vgrf4.yz, vgrf5.xxzx
463 *
464 * This eliminates false uses of some channels, letting dead code elimination
465 * remove the instructions that wrote them.
466 */
467 bool
468 vec4_visitor::opt_reduce_swizzle()
469 {
470 bool progress = false;
471
472 foreach_block_and_inst_safe(block, vec4_instruction, inst, cfg) {
473 if (inst->dst.file == BAD_FILE ||
474 inst->dst.file == ARF ||
475 inst->dst.file == FIXED_GRF ||
476 inst->is_send_from_grf())
477 continue;
478
479 unsigned swizzle;
480
481 /* Determine which channels of the sources are read. */
482 switch (inst->opcode) {
483 case VEC4_OPCODE_PACK_BYTES:
484 case BRW_OPCODE_DP4:
485 case BRW_OPCODE_DPH: /* FINISHME: DPH reads only three channels of src0,
486 * but all four of src1.
487 */
488 swizzle = brw_swizzle_for_size(4);
489 break;
490 case BRW_OPCODE_DP3:
491 swizzle = brw_swizzle_for_size(3);
492 break;
493 case BRW_OPCODE_DP2:
494 swizzle = brw_swizzle_for_size(2);
495 break;
496 default:
497 swizzle = brw_swizzle_for_mask(inst->dst.writemask);
498 break;
499 }
500
501 /* Update sources' swizzles. */
502 for (int i = 0; i < 3; i++) {
503 if (inst->src[i].file != VGRF &&
504 inst->src[i].file != ATTR &&
505 inst->src[i].file != UNIFORM)
506 continue;
507
508 const unsigned new_swizzle =
509 brw_compose_swizzle(swizzle, inst->src[i].swizzle);
510 if (inst->src[i].swizzle != new_swizzle) {
511 inst->src[i].swizzle = new_swizzle;
512 progress = true;
513 }
514 }
515 }
516
517 if (progress)
518 invalidate_live_intervals();
519
520 return progress;
521 }
522
523 void
524 vec4_visitor::split_uniform_registers()
525 {
526 /* Prior to this, uniforms have been in an array sized according to
527 * the number of vector uniforms present, sparsely filled (so an
528 * aggregate results in reg indices being skipped over). Now we're
529 * going to cut those aggregates up so each .nr index is one
530 * vector. The goal is to make elimination of unused uniform
531 * components easier later.
532 */
533 foreach_block_and_inst(block, vec4_instruction, inst, cfg) {
534 for (int i = 0 ; i < 3; i++) {
535 if (inst->src[i].file != UNIFORM)
536 continue;
537
538 assert(!inst->src[i].reladdr);
539
540 inst->src[i].nr += inst->src[i].reg_offset;
541 inst->src[i].reg_offset = 0;
542 }
543 }
544 }
545
546 void
547 vec4_visitor::pack_uniform_registers()
548 {
549 uint8_t chans_used[this->uniforms];
550 int new_loc[this->uniforms];
551 int new_chan[this->uniforms];
552
553 memset(chans_used, 0, sizeof(chans_used));
554 memset(new_loc, 0, sizeof(new_loc));
555 memset(new_chan, 0, sizeof(new_chan));
556
557 /* Find which uniform vectors are actually used by the program. We
558 * expect unused vector elements when we've moved array access out
559 * to pull constants, and from some GLSL code generators like wine.
560 */
561 foreach_block_and_inst(block, vec4_instruction, inst, cfg) {
562 unsigned readmask;
563 switch (inst->opcode) {
564 case VEC4_OPCODE_PACK_BYTES:
565 case BRW_OPCODE_DP4:
566 case BRW_OPCODE_DPH:
567 readmask = 0xf;
568 break;
569 case BRW_OPCODE_DP3:
570 readmask = 0x7;
571 break;
572 case BRW_OPCODE_DP2:
573 readmask = 0x3;
574 break;
575 default:
576 readmask = inst->dst.writemask;
577 break;
578 }
579
580 for (int i = 0 ; i < 3; i++) {
581 if (inst->src[i].file != UNIFORM)
582 continue;
583
584 int reg = inst->src[i].nr;
585 for (int c = 0; c < 4; c++) {
586 if (!(readmask & (1 << c)))
587 continue;
588
589 chans_used[reg] = MAX2(chans_used[reg],
590 BRW_GET_SWZ(inst->src[i].swizzle, c) + 1);
591 }
592 }
593
594 if (inst->opcode == SHADER_OPCODE_MOV_INDIRECT &&
595 inst->src[0].file == UNIFORM) {
596 assert(inst->src[2].file == BRW_IMMEDIATE_VALUE);
597 assert(inst->src[0].subnr == 0);
598
599 unsigned bytes_read = inst->src[2].ud;
600 assert(bytes_read % 4 == 0);
601 unsigned vec4s_read = DIV_ROUND_UP(bytes_read, 16);
602
603 /* We just mark every register touched by a MOV_INDIRECT as being
604 * fully used. This ensures that it doesn't broken up piecewise by
605 * the next part of our packing algorithm.
606 */
607 int reg = inst->src[0].nr;
608 for (unsigned i = 0; i < vec4s_read; i++)
609 chans_used[reg + i] = 4;
610 }
611 }
612
613 int new_uniform_count = 0;
614
615 /* Now, figure out a packing of the live uniform vectors into our
616 * push constants.
617 */
618 for (int src = 0; src < uniforms; src++) {
619 int size = chans_used[src];
620
621 if (size == 0)
622 continue;
623
624 int dst;
625 /* Find the lowest place we can slot this uniform in. */
626 for (dst = 0; dst < src; dst++) {
627 if (chans_used[dst] + size <= 4)
628 break;
629 }
630
631 if (src == dst) {
632 new_loc[src] = dst;
633 new_chan[src] = 0;
634 } else {
635 new_loc[src] = dst;
636 new_chan[src] = chans_used[dst];
637
638 /* Move the references to the data */
639 for (int j = 0; j < size; j++) {
640 stage_prog_data->param[dst * 4 + new_chan[src] + j] =
641 stage_prog_data->param[src * 4 + j];
642 }
643
644 chans_used[dst] += size;
645 chans_used[src] = 0;
646 }
647
648 new_uniform_count = MAX2(new_uniform_count, dst + 1);
649 }
650
651 this->uniforms = new_uniform_count;
652
653 /* Now, update the instructions for our repacked uniforms. */
654 foreach_block_and_inst(block, vec4_instruction, inst, cfg) {
655 for (int i = 0 ; i < 3; i++) {
656 int src = inst->src[i].nr;
657
658 if (inst->src[i].file != UNIFORM)
659 continue;
660
661 inst->src[i].nr = new_loc[src];
662 inst->src[i].swizzle += BRW_SWIZZLE4(new_chan[src], new_chan[src],
663 new_chan[src], new_chan[src]);
664 }
665 }
666 }
667
668 /**
669 * Does algebraic optimizations (0 * a = 0, 1 * a = a, a + 0 = a).
670 *
671 * While GLSL IR also performs this optimization, we end up with it in
672 * our instruction stream for a couple of reasons. One is that we
673 * sometimes generate silly instructions, for example in array access
674 * where we'll generate "ADD offset, index, base" even if base is 0.
675 * The other is that GLSL IR's constant propagation doesn't track the
676 * components of aggregates, so some VS patterns (initialize matrix to
677 * 0, accumulate in vertex blending factors) end up breaking down to
678 * instructions involving 0.
679 */
680 bool
681 vec4_visitor::opt_algebraic()
682 {
683 bool progress = false;
684
685 foreach_block_and_inst(block, vec4_instruction, inst, cfg) {
686 switch (inst->opcode) {
687 case BRW_OPCODE_MOV:
688 if (inst->src[0].file != IMM)
689 break;
690
691 if (inst->saturate) {
692 if (inst->dst.type != inst->src[0].type)
693 assert(!"unimplemented: saturate mixed types");
694
695 if (brw_saturate_immediate(inst->dst.type,
696 &inst->src[0].as_brw_reg())) {
697 inst->saturate = false;
698 progress = true;
699 }
700 }
701 break;
702
703 case VEC4_OPCODE_UNPACK_UNIFORM:
704 if (inst->src[0].file != UNIFORM) {
705 inst->opcode = BRW_OPCODE_MOV;
706 progress = true;
707 }
708 break;
709
710 case BRW_OPCODE_ADD:
711 if (inst->src[1].is_zero()) {
712 inst->opcode = BRW_OPCODE_MOV;
713 inst->src[1] = src_reg();
714 progress = true;
715 }
716 break;
717
718 case BRW_OPCODE_MUL:
719 if (inst->src[1].is_zero()) {
720 inst->opcode = BRW_OPCODE_MOV;
721 switch (inst->src[0].type) {
722 case BRW_REGISTER_TYPE_F:
723 inst->src[0] = brw_imm_f(0.0f);
724 break;
725 case BRW_REGISTER_TYPE_D:
726 inst->src[0] = brw_imm_d(0);
727 break;
728 case BRW_REGISTER_TYPE_UD:
729 inst->src[0] = brw_imm_ud(0u);
730 break;
731 default:
732 unreachable("not reached");
733 }
734 inst->src[1] = src_reg();
735 progress = true;
736 } else if (inst->src[1].is_one()) {
737 inst->opcode = BRW_OPCODE_MOV;
738 inst->src[1] = src_reg();
739 progress = true;
740 } else if (inst->src[1].is_negative_one()) {
741 inst->opcode = BRW_OPCODE_MOV;
742 inst->src[0].negate = !inst->src[0].negate;
743 inst->src[1] = src_reg();
744 progress = true;
745 }
746 break;
747 case BRW_OPCODE_CMP:
748 if (inst->conditional_mod == BRW_CONDITIONAL_GE &&
749 inst->src[0].abs &&
750 inst->src[0].negate &&
751 inst->src[1].is_zero()) {
752 inst->src[0].abs = false;
753 inst->src[0].negate = false;
754 inst->conditional_mod = BRW_CONDITIONAL_Z;
755 progress = true;
756 break;
757 }
758 break;
759 case SHADER_OPCODE_BROADCAST:
760 if (is_uniform(inst->src[0]) ||
761 inst->src[1].is_zero()) {
762 inst->opcode = BRW_OPCODE_MOV;
763 inst->src[1] = src_reg();
764 inst->force_writemask_all = true;
765 progress = true;
766 }
767 break;
768
769 default:
770 break;
771 }
772 }
773
774 if (progress)
775 invalidate_live_intervals();
776
777 return progress;
778 }
779
780 /**
781 * Only a limited number of hardware registers may be used for push
782 * constants, so this turns access to the overflowed constants into
783 * pull constants.
784 */
785 void
786 vec4_visitor::move_push_constants_to_pull_constants()
787 {
788 int pull_constant_loc[this->uniforms];
789
790 /* Only allow 32 registers (256 uniform components) as push constants,
791 * which is the limit on gen6.
792 *
793 * If changing this value, note the limitation about total_regs in
794 * brw_curbe.c.
795 */
796 int max_uniform_components = 32 * 8;
797 if (this->uniforms * 4 <= max_uniform_components)
798 return;
799
800 /* Make some sort of choice as to which uniforms get sent to pull
801 * constants. We could potentially do something clever here like
802 * look for the most infrequently used uniform vec4s, but leave
803 * that for later.
804 */
805 for (int i = 0; i < this->uniforms * 4; i += 4) {
806 pull_constant_loc[i / 4] = -1;
807
808 if (i >= max_uniform_components) {
809 const gl_constant_value **values = &stage_prog_data->param[i];
810
811 /* Try to find an existing copy of this uniform in the pull
812 * constants if it was part of an array access already.
813 */
814 for (unsigned int j = 0; j < stage_prog_data->nr_pull_params; j += 4) {
815 int matches;
816
817 for (matches = 0; matches < 4; matches++) {
818 if (stage_prog_data->pull_param[j + matches] != values[matches])
819 break;
820 }
821
822 if (matches == 4) {
823 pull_constant_loc[i / 4] = j / 4;
824 break;
825 }
826 }
827
828 if (pull_constant_loc[i / 4] == -1) {
829 assert(stage_prog_data->nr_pull_params % 4 == 0);
830 pull_constant_loc[i / 4] = stage_prog_data->nr_pull_params / 4;
831
832 for (int j = 0; j < 4; j++) {
833 stage_prog_data->pull_param[stage_prog_data->nr_pull_params++] =
834 values[j];
835 }
836 }
837 }
838 }
839
840 /* Now actually rewrite usage of the things we've moved to pull
841 * constants.
842 */
843 foreach_block_and_inst_safe(block, vec4_instruction, inst, cfg) {
844 for (int i = 0 ; i < 3; i++) {
845 if (inst->src[i].file != UNIFORM ||
846 pull_constant_loc[inst->src[i].nr] == -1)
847 continue;
848
849 int uniform = inst->src[i].nr;
850
851 dst_reg temp = dst_reg(this, glsl_type::vec4_type);
852
853 emit_pull_constant_load(block, inst, temp, inst->src[i],
854 pull_constant_loc[uniform], src_reg());
855
856 inst->src[i].file = temp.file;
857 inst->src[i].nr = temp.nr;
858 inst->src[i].reg_offset = temp.reg_offset;
859 inst->src[i].reladdr = NULL;
860 }
861 }
862
863 /* Repack push constants to remove the now-unused ones. */
864 pack_uniform_registers();
865 }
866
867 /* Conditions for which we want to avoid setting the dependency control bits */
868 bool
869 vec4_visitor::is_dep_ctrl_unsafe(const vec4_instruction *inst)
870 {
871 #define IS_DWORD(reg) \
872 (reg.type == BRW_REGISTER_TYPE_UD || \
873 reg.type == BRW_REGISTER_TYPE_D)
874
875 /* "When source or destination datatype is 64b or operation is integer DWord
876 * multiply, DepCtrl must not be used."
877 * May apply to future SoCs as well.
878 */
879 if (devinfo->is_cherryview) {
880 if (inst->opcode == BRW_OPCODE_MUL &&
881 IS_DWORD(inst->src[0]) &&
882 IS_DWORD(inst->src[1]))
883 return true;
884 }
885 #undef IS_DWORD
886
887 if (devinfo->gen >= 8) {
888 if (inst->opcode == BRW_OPCODE_F32TO16)
889 return true;
890 }
891
892 /*
893 * mlen:
894 * In the presence of send messages, totally interrupt dependency
895 * control. They're long enough that the chance of dependency
896 * control around them just doesn't matter.
897 *
898 * predicate:
899 * From the Ivy Bridge PRM, volume 4 part 3.7, page 80:
900 * When a sequence of NoDDChk and NoDDClr are used, the last instruction that
901 * completes the scoreboard clear must have a non-zero execution mask. This
902 * means, if any kind of predication can change the execution mask or channel
903 * enable of the last instruction, the optimization must be avoided. This is
904 * to avoid instructions being shot down the pipeline when no writes are
905 * required.
906 *
907 * math:
908 * Dependency control does not work well over math instructions.
909 * NB: Discovered empirically
910 */
911 return (inst->mlen || inst->predicate || inst->is_math());
912 }
913
914 /**
915 * Sets the dependency control fields on instructions after register
916 * allocation and before the generator is run.
917 *
918 * When you have a sequence of instructions like:
919 *
920 * DP4 temp.x vertex uniform[0]
921 * DP4 temp.y vertex uniform[0]
922 * DP4 temp.z vertex uniform[0]
923 * DP4 temp.w vertex uniform[0]
924 *
925 * The hardware doesn't know that it can actually run the later instructions
926 * while the previous ones are in flight, producing stalls. However, we have
927 * manual fields we can set in the instructions that let it do so.
928 */
929 void
930 vec4_visitor::opt_set_dependency_control()
931 {
932 vec4_instruction *last_grf_write[BRW_MAX_GRF];
933 uint8_t grf_channels_written[BRW_MAX_GRF];
934 vec4_instruction *last_mrf_write[BRW_MAX_GRF];
935 uint8_t mrf_channels_written[BRW_MAX_GRF];
936
937 assert(prog_data->total_grf ||
938 !"Must be called after register allocation");
939
940 foreach_block (block, cfg) {
941 memset(last_grf_write, 0, sizeof(last_grf_write));
942 memset(last_mrf_write, 0, sizeof(last_mrf_write));
943
944 foreach_inst_in_block (vec4_instruction, inst, block) {
945 /* If we read from a register that we were doing dependency control
946 * on, don't do dependency control across the read.
947 */
948 for (int i = 0; i < 3; i++) {
949 int reg = inst->src[i].nr + inst->src[i].reg_offset;
950 if (inst->src[i].file == VGRF) {
951 last_grf_write[reg] = NULL;
952 } else if (inst->src[i].file == FIXED_GRF) {
953 memset(last_grf_write, 0, sizeof(last_grf_write));
954 break;
955 }
956 assert(inst->src[i].file != MRF);
957 }
958
959 if (is_dep_ctrl_unsafe(inst)) {
960 memset(last_grf_write, 0, sizeof(last_grf_write));
961 memset(last_mrf_write, 0, sizeof(last_mrf_write));
962 continue;
963 }
964
965 /* Now, see if we can do dependency control for this instruction
966 * against a previous one writing to its destination.
967 */
968 int reg = inst->dst.nr + inst->dst.reg_offset;
969 if (inst->dst.file == VGRF || inst->dst.file == FIXED_GRF) {
970 if (last_grf_write[reg] &&
971 !(inst->dst.writemask & grf_channels_written[reg])) {
972 last_grf_write[reg]->no_dd_clear = true;
973 inst->no_dd_check = true;
974 } else {
975 grf_channels_written[reg] = 0;
976 }
977
978 last_grf_write[reg] = inst;
979 grf_channels_written[reg] |= inst->dst.writemask;
980 } else if (inst->dst.file == MRF) {
981 if (last_mrf_write[reg] &&
982 !(inst->dst.writemask & mrf_channels_written[reg])) {
983 last_mrf_write[reg]->no_dd_clear = true;
984 inst->no_dd_check = true;
985 } else {
986 mrf_channels_written[reg] = 0;
987 }
988
989 last_mrf_write[reg] = inst;
990 mrf_channels_written[reg] |= inst->dst.writemask;
991 }
992 }
993 }
994 }
995
996 bool
997 vec4_instruction::can_reswizzle(const struct brw_device_info *devinfo,
998 int dst_writemask,
999 int swizzle,
1000 int swizzle_mask)
1001 {
1002 /* Gen6 MATH instructions can not execute in align16 mode, so swizzles
1003 * are not allowed.
1004 */
1005 if (devinfo->gen == 6 && is_math() && swizzle != BRW_SWIZZLE_XYZW)
1006 return false;
1007
1008 if (!can_do_writemask(devinfo) && dst_writemask != WRITEMASK_XYZW)
1009 return false;
1010
1011 /* If this instruction sets anything not referenced by swizzle, then we'd
1012 * totally break it when we reswizzle.
1013 */
1014 if (dst.writemask & ~swizzle_mask)
1015 return false;
1016
1017 if (mlen > 0)
1018 return false;
1019
1020 for (int i = 0; i < 3; i++) {
1021 if (src[i].is_accumulator())
1022 return false;
1023 }
1024
1025 return true;
1026 }
1027
1028 /**
1029 * For any channels in the swizzle's source that were populated by this
1030 * instruction, rewrite the instruction to put the appropriate result directly
1031 * in those channels.
1032 *
1033 * e.g. for swizzle=yywx, MUL a.xy b c -> MUL a.yy_x b.yy z.yy_x
1034 */
1035 void
1036 vec4_instruction::reswizzle(int dst_writemask, int swizzle)
1037 {
1038 /* Destination write mask doesn't correspond to source swizzle for the dot
1039 * product and pack_bytes instructions.
1040 */
1041 if (opcode != BRW_OPCODE_DP4 && opcode != BRW_OPCODE_DPH &&
1042 opcode != BRW_OPCODE_DP3 && opcode != BRW_OPCODE_DP2 &&
1043 opcode != VEC4_OPCODE_PACK_BYTES) {
1044 for (int i = 0; i < 3; i++) {
1045 if (src[i].file == BAD_FILE || src[i].file == IMM)
1046 continue;
1047
1048 src[i].swizzle = brw_compose_swizzle(swizzle, src[i].swizzle);
1049 }
1050 }
1051
1052 /* Apply the specified swizzle and writemask to the original mask of
1053 * written components.
1054 */
1055 dst.writemask = dst_writemask &
1056 brw_apply_swizzle_to_mask(swizzle, dst.writemask);
1057 }
1058
1059 /*
1060 * Tries to reduce extra MOV instructions by taking temporary GRFs that get
1061 * just written and then MOVed into another reg and making the original write
1062 * of the GRF write directly to the final destination instead.
1063 */
1064 bool
1065 vec4_visitor::opt_register_coalesce()
1066 {
1067 bool progress = false;
1068 int next_ip = 0;
1069
1070 calculate_live_intervals();
1071
1072 foreach_block_and_inst_safe (block, vec4_instruction, inst, cfg) {
1073 int ip = next_ip;
1074 next_ip++;
1075
1076 if (inst->opcode != BRW_OPCODE_MOV ||
1077 (inst->dst.file != VGRF && inst->dst.file != MRF) ||
1078 inst->predicate ||
1079 inst->src[0].file != VGRF ||
1080 inst->dst.type != inst->src[0].type ||
1081 inst->src[0].abs || inst->src[0].negate || inst->src[0].reladdr)
1082 continue;
1083
1084 /* Remove no-op MOVs */
1085 if (inst->dst.file == inst->src[0].file &&
1086 inst->dst.nr == inst->src[0].nr &&
1087 inst->dst.reg_offset == inst->src[0].reg_offset) {
1088 bool is_nop_mov = true;
1089
1090 for (unsigned c = 0; c < 4; c++) {
1091 if ((inst->dst.writemask & (1 << c)) == 0)
1092 continue;
1093
1094 if (BRW_GET_SWZ(inst->src[0].swizzle, c) != c) {
1095 is_nop_mov = false;
1096 break;
1097 }
1098 }
1099
1100 if (is_nop_mov) {
1101 inst->remove(block);
1102 progress = true;
1103 continue;
1104 }
1105 }
1106
1107 bool to_mrf = (inst->dst.file == MRF);
1108
1109 /* Can't coalesce this GRF if someone else was going to
1110 * read it later.
1111 */
1112 if (var_range_end(var_from_reg(alloc, inst->src[0]), 4) > ip)
1113 continue;
1114
1115 /* We need to check interference with the final destination between this
1116 * instruction and the earliest instruction involved in writing the GRF
1117 * we're eliminating. To do that, keep track of which of our source
1118 * channels we've seen initialized.
1119 */
1120 const unsigned chans_needed =
1121 brw_apply_inv_swizzle_to_mask(inst->src[0].swizzle,
1122 inst->dst.writemask);
1123 unsigned chans_remaining = chans_needed;
1124
1125 /* Now walk up the instruction stream trying to see if we can rewrite
1126 * everything writing to the temporary to write into the destination
1127 * instead.
1128 */
1129 vec4_instruction *_scan_inst = (vec4_instruction *)inst->prev;
1130 foreach_inst_in_block_reverse_starting_from(vec4_instruction, scan_inst,
1131 inst) {
1132 _scan_inst = scan_inst;
1133
1134 if (inst->src[0].in_range(scan_inst->dst, scan_inst->regs_written)) {
1135 /* Found something writing to the reg we want to coalesce away. */
1136 if (to_mrf) {
1137 /* SEND instructions can't have MRF as a destination. */
1138 if (scan_inst->mlen)
1139 break;
1140
1141 if (devinfo->gen == 6) {
1142 /* gen6 math instructions must have the destination be
1143 * VGRF, so no compute-to-MRF for them.
1144 */
1145 if (scan_inst->is_math()) {
1146 break;
1147 }
1148 }
1149 }
1150
1151 /* This doesn't handle saturation on the instruction we
1152 * want to coalesce away if the register types do not match.
1153 * But if scan_inst is a non type-converting 'mov', we can fix
1154 * the types later.
1155 */
1156 if (inst->saturate &&
1157 inst->dst.type != scan_inst->dst.type &&
1158 !(scan_inst->opcode == BRW_OPCODE_MOV &&
1159 scan_inst->dst.type == scan_inst->src[0].type))
1160 break;
1161
1162 /* If we can't handle the swizzle, bail. */
1163 if (!scan_inst->can_reswizzle(devinfo, inst->dst.writemask,
1164 inst->src[0].swizzle,
1165 chans_needed)) {
1166 break;
1167 }
1168
1169 /* This doesn't handle coalescing of multiple registers. */
1170 if (scan_inst->regs_written > 1)
1171 break;
1172
1173 /* Mark which channels we found unconditional writes for. */
1174 if (!scan_inst->predicate)
1175 chans_remaining &= ~scan_inst->dst.writemask;
1176
1177 if (chans_remaining == 0)
1178 break;
1179 }
1180
1181 /* You can't read from an MRF, so if someone else reads our MRF's
1182 * source GRF that we wanted to rewrite, that stops us. If it's a
1183 * GRF we're trying to coalesce to, we don't actually handle
1184 * rewriting sources so bail in that case as well.
1185 */
1186 bool interfered = false;
1187 for (int i = 0; i < 3; i++) {
1188 if (inst->src[0].in_range(scan_inst->src[i],
1189 scan_inst->regs_read(i)))
1190 interfered = true;
1191 }
1192 if (interfered)
1193 break;
1194
1195 /* If somebody else writes the same channels of our destination here,
1196 * we can't coalesce before that.
1197 */
1198 if (inst->dst.in_range(scan_inst->dst, scan_inst->regs_written) &&
1199 (inst->dst.writemask & scan_inst->dst.writemask) != 0) {
1200 break;
1201 }
1202
1203 /* Check for reads of the register we're trying to coalesce into. We
1204 * can't go rewriting instructions above that to put some other value
1205 * in the register instead.
1206 */
1207 if (to_mrf && scan_inst->mlen > 0) {
1208 if (inst->dst.nr >= scan_inst->base_mrf &&
1209 inst->dst.nr < scan_inst->base_mrf + scan_inst->mlen) {
1210 break;
1211 }
1212 } else {
1213 for (int i = 0; i < 3; i++) {
1214 if (inst->dst.in_range(scan_inst->src[i],
1215 scan_inst->regs_read(i)))
1216 interfered = true;
1217 }
1218 if (interfered)
1219 break;
1220 }
1221 }
1222
1223 if (chans_remaining == 0) {
1224 /* If we've made it here, we have an MOV we want to coalesce out, and
1225 * a scan_inst pointing to the earliest instruction involved in
1226 * computing the value. Now go rewrite the instruction stream
1227 * between the two.
1228 */
1229 vec4_instruction *scan_inst = _scan_inst;
1230 while (scan_inst != inst) {
1231 if (scan_inst->dst.file == VGRF &&
1232 scan_inst->dst.nr == inst->src[0].nr &&
1233 scan_inst->dst.reg_offset == inst->src[0].reg_offset) {
1234 scan_inst->reswizzle(inst->dst.writemask,
1235 inst->src[0].swizzle);
1236 scan_inst->dst.file = inst->dst.file;
1237 scan_inst->dst.nr = inst->dst.nr;
1238 scan_inst->dst.reg_offset = inst->dst.reg_offset;
1239 if (inst->saturate &&
1240 inst->dst.type != scan_inst->dst.type) {
1241 /* If we have reached this point, scan_inst is a non
1242 * type-converting 'mov' and we can modify its register types
1243 * to match the ones in inst. Otherwise, we could have an
1244 * incorrect saturation result.
1245 */
1246 scan_inst->dst.type = inst->dst.type;
1247 scan_inst->src[0].type = inst->src[0].type;
1248 }
1249 scan_inst->saturate |= inst->saturate;
1250 }
1251 scan_inst = (vec4_instruction *)scan_inst->next;
1252 }
1253 inst->remove(block);
1254 progress = true;
1255 }
1256 }
1257
1258 if (progress)
1259 invalidate_live_intervals();
1260
1261 return progress;
1262 }
1263
1264 /**
1265 * Eliminate FIND_LIVE_CHANNEL instructions occurring outside any control
1266 * flow. We could probably do better here with some form of divergence
1267 * analysis.
1268 */
1269 bool
1270 vec4_visitor::eliminate_find_live_channel()
1271 {
1272 bool progress = false;
1273 unsigned depth = 0;
1274
1275 foreach_block_and_inst_safe(block, vec4_instruction, inst, cfg) {
1276 switch (inst->opcode) {
1277 case BRW_OPCODE_IF:
1278 case BRW_OPCODE_DO:
1279 depth++;
1280 break;
1281
1282 case BRW_OPCODE_ENDIF:
1283 case BRW_OPCODE_WHILE:
1284 depth--;
1285 break;
1286
1287 case SHADER_OPCODE_FIND_LIVE_CHANNEL:
1288 if (depth == 0) {
1289 inst->opcode = BRW_OPCODE_MOV;
1290 inst->src[0] = brw_imm_d(0);
1291 inst->force_writemask_all = true;
1292 progress = true;
1293 }
1294 break;
1295
1296 default:
1297 break;
1298 }
1299 }
1300
1301 return progress;
1302 }
1303
1304 /**
1305 * Splits virtual GRFs requesting more than one contiguous physical register.
1306 *
1307 * We initially create large virtual GRFs for temporary structures, arrays,
1308 * and matrices, so that the dereference visitor functions can add reg_offsets
1309 * to work their way down to the actual member being accessed. But when it
1310 * comes to optimization, we'd like to treat each register as individual
1311 * storage if possible.
1312 *
1313 * So far, the only thing that might prevent splitting is a send message from
1314 * a GRF on IVB.
1315 */
1316 void
1317 vec4_visitor::split_virtual_grfs()
1318 {
1319 int num_vars = this->alloc.count;
1320 int new_virtual_grf[num_vars];
1321 bool split_grf[num_vars];
1322
1323 memset(new_virtual_grf, 0, sizeof(new_virtual_grf));
1324
1325 /* Try to split anything > 0 sized. */
1326 for (int i = 0; i < num_vars; i++) {
1327 split_grf[i] = this->alloc.sizes[i] != 1;
1328 }
1329
1330 /* Check that the instructions are compatible with the registers we're trying
1331 * to split.
1332 */
1333 foreach_block_and_inst(block, vec4_instruction, inst, cfg) {
1334 if (inst->dst.file == VGRF && inst->regs_written > 1)
1335 split_grf[inst->dst.nr] = false;
1336
1337 for (int i = 0; i < 3; i++) {
1338 if (inst->src[i].file == VGRF && inst->regs_read(i) > 1)
1339 split_grf[inst->src[i].nr] = false;
1340 }
1341 }
1342
1343 /* Allocate new space for split regs. Note that the virtual
1344 * numbers will be contiguous.
1345 */
1346 for (int i = 0; i < num_vars; i++) {
1347 if (!split_grf[i])
1348 continue;
1349
1350 new_virtual_grf[i] = alloc.allocate(1);
1351 for (unsigned j = 2; j < this->alloc.sizes[i]; j++) {
1352 unsigned reg = alloc.allocate(1);
1353 assert(reg == new_virtual_grf[i] + j - 1);
1354 (void) reg;
1355 }
1356 this->alloc.sizes[i] = 1;
1357 }
1358
1359 foreach_block_and_inst(block, vec4_instruction, inst, cfg) {
1360 if (inst->dst.file == VGRF && split_grf[inst->dst.nr] &&
1361 inst->dst.reg_offset != 0) {
1362 inst->dst.nr = (new_virtual_grf[inst->dst.nr] +
1363 inst->dst.reg_offset - 1);
1364 inst->dst.reg_offset = 0;
1365 }
1366 for (int i = 0; i < 3; i++) {
1367 if (inst->src[i].file == VGRF && split_grf[inst->src[i].nr] &&
1368 inst->src[i].reg_offset != 0) {
1369 inst->src[i].nr = (new_virtual_grf[inst->src[i].nr] +
1370 inst->src[i].reg_offset - 1);
1371 inst->src[i].reg_offset = 0;
1372 }
1373 }
1374 }
1375 invalidate_live_intervals();
1376 }
1377
1378 void
1379 vec4_visitor::dump_instruction(backend_instruction *be_inst)
1380 {
1381 dump_instruction(be_inst, stderr);
1382 }
1383
1384 void
1385 vec4_visitor::dump_instruction(backend_instruction *be_inst, FILE *file)
1386 {
1387 vec4_instruction *inst = (vec4_instruction *)be_inst;
1388
1389 if (inst->predicate) {
1390 fprintf(file, "(%cf0.%d%s) ",
1391 inst->predicate_inverse ? '-' : '+',
1392 inst->flag_subreg,
1393 pred_ctrl_align16[inst->predicate]);
1394 }
1395
1396 fprintf(file, "%s", brw_instruction_name(devinfo, inst->opcode));
1397 if (inst->saturate)
1398 fprintf(file, ".sat");
1399 if (inst->conditional_mod) {
1400 fprintf(file, "%s", conditional_modifier[inst->conditional_mod]);
1401 if (!inst->predicate &&
1402 (devinfo->gen < 5 || (inst->opcode != BRW_OPCODE_SEL &&
1403 inst->opcode != BRW_OPCODE_IF &&
1404 inst->opcode != BRW_OPCODE_WHILE))) {
1405 fprintf(file, ".f0.%d", inst->flag_subreg);
1406 }
1407 }
1408 fprintf(file, " ");
1409
1410 switch (inst->dst.file) {
1411 case VGRF:
1412 fprintf(file, "vgrf%d.%d", inst->dst.nr, inst->dst.reg_offset);
1413 break;
1414 case FIXED_GRF:
1415 fprintf(file, "g%d", inst->dst.nr);
1416 break;
1417 case MRF:
1418 fprintf(file, "m%d", inst->dst.nr);
1419 break;
1420 case ARF:
1421 switch (inst->dst.nr) {
1422 case BRW_ARF_NULL:
1423 fprintf(file, "null");
1424 break;
1425 case BRW_ARF_ADDRESS:
1426 fprintf(file, "a0.%d", inst->dst.subnr);
1427 break;
1428 case BRW_ARF_ACCUMULATOR:
1429 fprintf(file, "acc%d", inst->dst.subnr);
1430 break;
1431 case BRW_ARF_FLAG:
1432 fprintf(file, "f%d.%d", inst->dst.nr & 0xf, inst->dst.subnr);
1433 break;
1434 default:
1435 fprintf(file, "arf%d.%d", inst->dst.nr & 0xf, inst->dst.subnr);
1436 break;
1437 }
1438 if (inst->dst.subnr)
1439 fprintf(file, "+%d", inst->dst.subnr);
1440 break;
1441 case BAD_FILE:
1442 fprintf(file, "(null)");
1443 break;
1444 case IMM:
1445 case ATTR:
1446 case UNIFORM:
1447 unreachable("not reached");
1448 }
1449 if (inst->dst.writemask != WRITEMASK_XYZW) {
1450 fprintf(file, ".");
1451 if (inst->dst.writemask & 1)
1452 fprintf(file, "x");
1453 if (inst->dst.writemask & 2)
1454 fprintf(file, "y");
1455 if (inst->dst.writemask & 4)
1456 fprintf(file, "z");
1457 if (inst->dst.writemask & 8)
1458 fprintf(file, "w");
1459 }
1460 fprintf(file, ":%s", brw_reg_type_letters(inst->dst.type));
1461
1462 if (inst->src[0].file != BAD_FILE)
1463 fprintf(file, ", ");
1464
1465 for (int i = 0; i < 3 && inst->src[i].file != BAD_FILE; i++) {
1466 if (inst->src[i].negate)
1467 fprintf(file, "-");
1468 if (inst->src[i].abs)
1469 fprintf(file, "|");
1470 switch (inst->src[i].file) {
1471 case VGRF:
1472 fprintf(file, "vgrf%d", inst->src[i].nr);
1473 break;
1474 case FIXED_GRF:
1475 fprintf(file, "g%d", inst->src[i].nr);
1476 break;
1477 case ATTR:
1478 fprintf(file, "attr%d", inst->src[i].nr);
1479 break;
1480 case UNIFORM:
1481 fprintf(file, "u%d", inst->src[i].nr);
1482 break;
1483 case IMM:
1484 switch (inst->src[i].type) {
1485 case BRW_REGISTER_TYPE_F:
1486 fprintf(file, "%fF", inst->src[i].f);
1487 break;
1488 case BRW_REGISTER_TYPE_D:
1489 fprintf(file, "%dD", inst->src[i].d);
1490 break;
1491 case BRW_REGISTER_TYPE_UD:
1492 fprintf(file, "%uU", inst->src[i].ud);
1493 break;
1494 case BRW_REGISTER_TYPE_VF:
1495 fprintf(file, "[%-gF, %-gF, %-gF, %-gF]",
1496 brw_vf_to_float((inst->src[i].ud >> 0) & 0xff),
1497 brw_vf_to_float((inst->src[i].ud >> 8) & 0xff),
1498 brw_vf_to_float((inst->src[i].ud >> 16) & 0xff),
1499 brw_vf_to_float((inst->src[i].ud >> 24) & 0xff));
1500 break;
1501 default:
1502 fprintf(file, "???");
1503 break;
1504 }
1505 break;
1506 case ARF:
1507 switch (inst->src[i].nr) {
1508 case BRW_ARF_NULL:
1509 fprintf(file, "null");
1510 break;
1511 case BRW_ARF_ADDRESS:
1512 fprintf(file, "a0.%d", inst->src[i].subnr);
1513 break;
1514 case BRW_ARF_ACCUMULATOR:
1515 fprintf(file, "acc%d", inst->src[i].subnr);
1516 break;
1517 case BRW_ARF_FLAG:
1518 fprintf(file, "f%d.%d", inst->src[i].nr & 0xf, inst->src[i].subnr);
1519 break;
1520 default:
1521 fprintf(file, "arf%d.%d", inst->src[i].nr & 0xf, inst->src[i].subnr);
1522 break;
1523 }
1524 if (inst->src[i].subnr)
1525 fprintf(file, "+%d", inst->src[i].subnr);
1526 break;
1527 case BAD_FILE:
1528 fprintf(file, "(null)");
1529 break;
1530 case MRF:
1531 unreachable("not reached");
1532 }
1533
1534 /* Don't print .0; and only VGRFs have reg_offsets and sizes */
1535 if (inst->src[i].reg_offset != 0 &&
1536 inst->src[i].file == VGRF &&
1537 alloc.sizes[inst->src[i].nr] != 1)
1538 fprintf(file, ".%d", inst->src[i].reg_offset);
1539
1540 if (inst->src[i].file != IMM) {
1541 static const char *chans[4] = {"x", "y", "z", "w"};
1542 fprintf(file, ".");
1543 for (int c = 0; c < 4; c++) {
1544 fprintf(file, "%s", chans[BRW_GET_SWZ(inst->src[i].swizzle, c)]);
1545 }
1546 }
1547
1548 if (inst->src[i].abs)
1549 fprintf(file, "|");
1550
1551 if (inst->src[i].file != IMM) {
1552 fprintf(file, ":%s", brw_reg_type_letters(inst->src[i].type));
1553 }
1554
1555 if (i < 2 && inst->src[i + 1].file != BAD_FILE)
1556 fprintf(file, ", ");
1557 }
1558
1559 if (inst->force_writemask_all)
1560 fprintf(file, " NoMask");
1561
1562 fprintf(file, "\n");
1563 }
1564
1565
1566 static inline struct brw_reg
1567 attribute_to_hw_reg(int attr, bool interleaved)
1568 {
1569 if (interleaved)
1570 return stride(brw_vec4_grf(attr / 2, (attr % 2) * 4), 0, 4, 1);
1571 else
1572 return brw_vec8_grf(attr, 0);
1573 }
1574
1575
1576 /**
1577 * Replace each register of type ATTR in this->instructions with a reference
1578 * to a fixed HW register.
1579 *
1580 * If interleaved is true, then each attribute takes up half a register, with
1581 * register N containing attribute 2*N in its first half and attribute 2*N+1
1582 * in its second half (this corresponds to the payload setup used by geometry
1583 * shaders in "single" or "dual instanced" dispatch mode). If interleaved is
1584 * false, then each attribute takes up a whole register, with register N
1585 * containing attribute N (this corresponds to the payload setup used by
1586 * vertex shaders, and by geometry shaders in "dual object" dispatch mode).
1587 */
1588 void
1589 vec4_visitor::lower_attributes_to_hw_regs(const int *attribute_map,
1590 bool interleaved)
1591 {
1592 foreach_block_and_inst(block, vec4_instruction, inst, cfg) {
1593 for (int i = 0; i < 3; i++) {
1594 if (inst->src[i].file != ATTR)
1595 continue;
1596
1597 int grf = attribute_map[inst->src[i].nr + inst->src[i].reg_offset];
1598
1599 /* All attributes used in the shader need to have been assigned a
1600 * hardware register by the caller
1601 */
1602 assert(grf != 0);
1603
1604 struct brw_reg reg = attribute_to_hw_reg(grf, interleaved);
1605 reg.swizzle = inst->src[i].swizzle;
1606 reg.type = inst->src[i].type;
1607 if (inst->src[i].abs)
1608 reg = brw_abs(reg);
1609 if (inst->src[i].negate)
1610 reg = negate(reg);
1611
1612 inst->src[i] = reg;
1613 }
1614 }
1615 }
1616
1617 int
1618 vec4_vs_visitor::setup_attributes(int payload_reg)
1619 {
1620 int nr_attributes;
1621 int attribute_map[VERT_ATTRIB_MAX + 2];
1622 memset(attribute_map, 0, sizeof(attribute_map));
1623
1624 nr_attributes = 0;
1625 for (int i = 0; i < VERT_ATTRIB_MAX; i++) {
1626 if (vs_prog_data->inputs_read & BITFIELD64_BIT(i)) {
1627 attribute_map[i] = payload_reg + nr_attributes;
1628 nr_attributes++;
1629 }
1630 }
1631
1632 /* VertexID is stored by the VF as the last vertex element, but we
1633 * don't represent it with a flag in inputs_read, so we call it
1634 * VERT_ATTRIB_MAX.
1635 */
1636 if (vs_prog_data->uses_vertexid || vs_prog_data->uses_instanceid ||
1637 vs_prog_data->uses_basevertex || vs_prog_data->uses_baseinstance) {
1638 attribute_map[VERT_ATTRIB_MAX] = payload_reg + nr_attributes;
1639 nr_attributes++;
1640 }
1641
1642 if (vs_prog_data->uses_drawid) {
1643 attribute_map[VERT_ATTRIB_MAX + 1] = payload_reg + nr_attributes;
1644 nr_attributes++;
1645 }
1646
1647 lower_attributes_to_hw_regs(attribute_map, false /* interleaved */);
1648
1649 return payload_reg + vs_prog_data->nr_attributes;
1650 }
1651
1652 int
1653 vec4_visitor::setup_uniforms(int reg)
1654 {
1655 prog_data->base.dispatch_grf_start_reg = reg;
1656
1657 /* The pre-gen6 VS requires that some push constants get loaded no
1658 * matter what, or the GPU would hang.
1659 */
1660 if (devinfo->gen < 6 && this->uniforms == 0) {
1661 stage_prog_data->param =
1662 reralloc(NULL, stage_prog_data->param, const gl_constant_value *, 4);
1663 for (unsigned int i = 0; i < 4; i++) {
1664 unsigned int slot = this->uniforms * 4 + i;
1665 static gl_constant_value zero = { 0.0 };
1666 stage_prog_data->param[slot] = &zero;
1667 }
1668
1669 this->uniforms++;
1670 reg++;
1671 } else {
1672 reg += ALIGN(uniforms, 2) / 2;
1673 }
1674
1675 stage_prog_data->nr_params = this->uniforms * 4;
1676
1677 prog_data->base.curb_read_length =
1678 reg - prog_data->base.dispatch_grf_start_reg;
1679
1680 return reg;
1681 }
1682
1683 void
1684 vec4_vs_visitor::setup_payload(void)
1685 {
1686 int reg = 0;
1687
1688 /* The payload always contains important data in g0, which contains
1689 * the URB handles that are passed on to the URB write at the end
1690 * of the thread. So, we always start push constants at g1.
1691 */
1692 reg++;
1693
1694 reg = setup_uniforms(reg);
1695
1696 reg = setup_attributes(reg);
1697
1698 this->first_non_payload_grf = reg;
1699 }
1700
1701 bool
1702 vec4_visitor::lower_minmax()
1703 {
1704 assert(devinfo->gen < 6);
1705
1706 bool progress = false;
1707
1708 foreach_block_and_inst_safe(block, vec4_instruction, inst, cfg) {
1709 const vec4_builder ibld(this, block, inst);
1710
1711 if (inst->opcode == BRW_OPCODE_SEL &&
1712 inst->predicate == BRW_PREDICATE_NONE) {
1713 /* FIXME: Using CMP doesn't preserve the NaN propagation semantics of
1714 * the original SEL.L/GE instruction
1715 */
1716 ibld.CMP(ibld.null_reg_d(), inst->src[0], inst->src[1],
1717 inst->conditional_mod);
1718 inst->predicate = BRW_PREDICATE_NORMAL;
1719 inst->conditional_mod = BRW_CONDITIONAL_NONE;
1720
1721 progress = true;
1722 }
1723 }
1724
1725 if (progress)
1726 invalidate_live_intervals();
1727
1728 return progress;
1729 }
1730
1731 src_reg
1732 vec4_visitor::get_timestamp()
1733 {
1734 assert(devinfo->gen >= 7);
1735
1736 src_reg ts = src_reg(brw_reg(BRW_ARCHITECTURE_REGISTER_FILE,
1737 BRW_ARF_TIMESTAMP,
1738 0,
1739 0,
1740 0,
1741 BRW_REGISTER_TYPE_UD,
1742 BRW_VERTICAL_STRIDE_0,
1743 BRW_WIDTH_4,
1744 BRW_HORIZONTAL_STRIDE_4,
1745 BRW_SWIZZLE_XYZW,
1746 WRITEMASK_XYZW));
1747
1748 dst_reg dst = dst_reg(this, glsl_type::uvec4_type);
1749
1750 vec4_instruction *mov = emit(MOV(dst, ts));
1751 /* We want to read the 3 fields we care about (mostly field 0, but also 2)
1752 * even if it's not enabled in the dispatch.
1753 */
1754 mov->force_writemask_all = true;
1755
1756 return src_reg(dst);
1757 }
1758
1759 void
1760 vec4_visitor::emit_shader_time_begin()
1761 {
1762 current_annotation = "shader time start";
1763 shader_start_time = get_timestamp();
1764 }
1765
1766 void
1767 vec4_visitor::emit_shader_time_end()
1768 {
1769 current_annotation = "shader time end";
1770 src_reg shader_end_time = get_timestamp();
1771
1772
1773 /* Check that there weren't any timestamp reset events (assuming these
1774 * were the only two timestamp reads that happened).
1775 */
1776 src_reg reset_end = shader_end_time;
1777 reset_end.swizzle = BRW_SWIZZLE_ZZZZ;
1778 vec4_instruction *test = emit(AND(dst_null_ud(), reset_end, brw_imm_ud(1u)));
1779 test->conditional_mod = BRW_CONDITIONAL_Z;
1780
1781 emit(IF(BRW_PREDICATE_NORMAL));
1782
1783 /* Take the current timestamp and get the delta. */
1784 shader_start_time.negate = true;
1785 dst_reg diff = dst_reg(this, glsl_type::uint_type);
1786 emit(ADD(diff, shader_start_time, shader_end_time));
1787
1788 /* If there were no instructions between the two timestamp gets, the diff
1789 * is 2 cycles. Remove that overhead, so I can forget about that when
1790 * trying to determine the time taken for single instructions.
1791 */
1792 emit(ADD(diff, src_reg(diff), brw_imm_ud(-2u)));
1793
1794 emit_shader_time_write(0, src_reg(diff));
1795 emit_shader_time_write(1, brw_imm_ud(1u));
1796 emit(BRW_OPCODE_ELSE);
1797 emit_shader_time_write(2, brw_imm_ud(1u));
1798 emit(BRW_OPCODE_ENDIF);
1799 }
1800
1801 void
1802 vec4_visitor::emit_shader_time_write(int shader_time_subindex, src_reg value)
1803 {
1804 dst_reg dst =
1805 dst_reg(this, glsl_type::get_array_instance(glsl_type::vec4_type, 2));
1806
1807 dst_reg offset = dst;
1808 dst_reg time = dst;
1809 time.reg_offset++;
1810
1811 offset.type = BRW_REGISTER_TYPE_UD;
1812 int index = shader_time_index * 3 + shader_time_subindex;
1813 emit(MOV(offset, brw_imm_d(index * SHADER_TIME_STRIDE)));
1814
1815 time.type = BRW_REGISTER_TYPE_UD;
1816 emit(MOV(time, value));
1817
1818 vec4_instruction *inst =
1819 emit(SHADER_OPCODE_SHADER_TIME_ADD, dst_reg(), src_reg(dst));
1820 inst->mlen = 2;
1821 }
1822
1823 void
1824 vec4_visitor::convert_to_hw_regs()
1825 {
1826 foreach_block_and_inst(block, vec4_instruction, inst, cfg) {
1827 for (int i = 0; i < 3; i++) {
1828 struct src_reg &src = inst->src[i];
1829 struct brw_reg reg;
1830 switch (src.file) {
1831 case VGRF:
1832 reg = brw_vec8_grf(src.nr + src.reg_offset, 0);
1833 reg.type = src.type;
1834 reg.swizzle = src.swizzle;
1835 reg.abs = src.abs;
1836 reg.negate = src.negate;
1837 break;
1838
1839 case UNIFORM:
1840 reg = stride(brw_vec4_grf(prog_data->base.dispatch_grf_start_reg +
1841 (src.nr + src.reg_offset) / 2,
1842 ((src.nr + src.reg_offset) % 2) * 4),
1843 0, 4, 1);
1844 reg.type = src.type;
1845 reg.swizzle = src.swizzle;
1846 reg.abs = src.abs;
1847 reg.negate = src.negate;
1848
1849 /* This should have been moved to pull constants. */
1850 assert(!src.reladdr);
1851 break;
1852
1853 case ARF:
1854 case FIXED_GRF:
1855 case IMM:
1856 continue;
1857
1858 case BAD_FILE:
1859 /* Probably unused. */
1860 reg = brw_null_reg();
1861 break;
1862
1863 case MRF:
1864 case ATTR:
1865 unreachable("not reached");
1866 }
1867
1868 src = reg;
1869 }
1870
1871 if (inst->is_3src(devinfo)) {
1872 /* 3-src instructions with scalar sources support arbitrary subnr,
1873 * but don't actually use swizzles. Convert swizzle into subnr.
1874 */
1875 for (int i = 0; i < 3; i++) {
1876 if (inst->src[i].vstride == BRW_VERTICAL_STRIDE_0) {
1877 assert(brw_is_single_value_swizzle(inst->src[i].swizzle));
1878 inst->src[i].subnr += 4 * BRW_GET_SWZ(inst->src[i].swizzle, 0);
1879 }
1880 }
1881 }
1882
1883 dst_reg &dst = inst->dst;
1884 struct brw_reg reg;
1885
1886 switch (inst->dst.file) {
1887 case VGRF:
1888 reg = brw_vec8_grf(dst.nr + dst.reg_offset, 0);
1889 reg.type = dst.type;
1890 reg.writemask = dst.writemask;
1891 break;
1892
1893 case MRF:
1894 assert(((dst.nr + dst.reg_offset) & ~BRW_MRF_COMPR4) < BRW_MAX_MRF(devinfo->gen));
1895 reg = brw_message_reg(dst.nr + dst.reg_offset);
1896 reg.type = dst.type;
1897 reg.writemask = dst.writemask;
1898 break;
1899
1900 case ARF:
1901 case FIXED_GRF:
1902 reg = dst.as_brw_reg();
1903 break;
1904
1905 case BAD_FILE:
1906 reg = brw_null_reg();
1907 break;
1908
1909 case IMM:
1910 case ATTR:
1911 case UNIFORM:
1912 unreachable("not reached");
1913 }
1914
1915 dst = reg;
1916 }
1917 }
1918
1919 bool
1920 vec4_visitor::run()
1921 {
1922 if (shader_time_index >= 0)
1923 emit_shader_time_begin();
1924
1925 emit_prolog();
1926
1927 emit_nir_code();
1928 if (failed)
1929 return false;
1930 base_ir = NULL;
1931
1932 emit_thread_end();
1933
1934 calculate_cfg();
1935
1936 /* Before any optimization, push array accesses out to scratch
1937 * space where we need them to be. This pass may allocate new
1938 * virtual GRFs, so we want to do it early. It also makes sure
1939 * that we have reladdr computations available for CSE, since we'll
1940 * often do repeated subexpressions for those.
1941 */
1942 move_grf_array_access_to_scratch();
1943 move_uniform_array_access_to_pull_constants();
1944
1945 pack_uniform_registers();
1946 move_push_constants_to_pull_constants();
1947 split_virtual_grfs();
1948
1949 #define OPT(pass, args...) ({ \
1950 pass_num++; \
1951 bool this_progress = pass(args); \
1952 \
1953 if (unlikely(INTEL_DEBUG & DEBUG_OPTIMIZER) && this_progress) { \
1954 char filename[64]; \
1955 snprintf(filename, 64, "%s-%s-%02d-%02d-" #pass, \
1956 stage_abbrev, nir->info.name, iteration, pass_num); \
1957 \
1958 backend_shader::dump_instructions(filename); \
1959 } \
1960 \
1961 progress = progress || this_progress; \
1962 this_progress; \
1963 })
1964
1965
1966 if (unlikely(INTEL_DEBUG & DEBUG_OPTIMIZER)) {
1967 char filename[64];
1968 snprintf(filename, 64, "%s-%s-00-00-start",
1969 stage_abbrev, nir->info.name);
1970
1971 backend_shader::dump_instructions(filename);
1972 }
1973
1974 bool progress;
1975 int iteration = 0;
1976 int pass_num = 0;
1977 do {
1978 progress = false;
1979 pass_num = 0;
1980 iteration++;
1981
1982 OPT(opt_predicated_break, this);
1983 OPT(opt_reduce_swizzle);
1984 OPT(dead_code_eliminate);
1985 OPT(dead_control_flow_eliminate, this);
1986 OPT(opt_copy_propagation);
1987 OPT(opt_cmod_propagation);
1988 OPT(opt_cse);
1989 OPT(opt_algebraic);
1990 OPT(opt_register_coalesce);
1991 OPT(eliminate_find_live_channel);
1992 } while (progress);
1993
1994 pass_num = 0;
1995
1996 if (OPT(opt_vector_float)) {
1997 OPT(opt_cse);
1998 OPT(opt_copy_propagation, false);
1999 OPT(opt_copy_propagation, true);
2000 OPT(dead_code_eliminate);
2001 }
2002
2003 if (devinfo->gen <= 5 && OPT(lower_minmax)) {
2004 OPT(opt_cmod_propagation);
2005 OPT(opt_cse);
2006 OPT(opt_copy_propagation);
2007 OPT(dead_code_eliminate);
2008 }
2009
2010 if (failed)
2011 return false;
2012
2013 setup_payload();
2014
2015 if (unlikely(INTEL_DEBUG & DEBUG_SPILL_VEC4)) {
2016 /* Debug of register spilling: Go spill everything. */
2017 const int grf_count = alloc.count;
2018 float spill_costs[alloc.count];
2019 bool no_spill[alloc.count];
2020 evaluate_spill_costs(spill_costs, no_spill);
2021 for (int i = 0; i < grf_count; i++) {
2022 if (no_spill[i])
2023 continue;
2024 spill_reg(i);
2025 }
2026 }
2027
2028 bool allocated_without_spills = reg_allocate();
2029
2030 if (!allocated_without_spills) {
2031 compiler->shader_perf_log(log_data,
2032 "%s shader triggered register spilling. "
2033 "Try reducing the number of live vec4 values "
2034 "to improve performance.\n",
2035 stage_name);
2036
2037 while (!reg_allocate()) {
2038 if (failed)
2039 return false;
2040 }
2041 }
2042
2043 opt_schedule_instructions();
2044
2045 opt_set_dependency_control();
2046
2047 convert_to_hw_regs();
2048
2049 if (last_scratch > 0) {
2050 prog_data->base.total_scratch =
2051 brw_get_scratch_size(last_scratch * REG_SIZE);
2052 }
2053
2054 return !failed;
2055 }
2056
2057 } /* namespace brw */
2058
2059 extern "C" {
2060
2061 /**
2062 * Compile a vertex shader.
2063 *
2064 * Returns the final assembly and the program's size.
2065 */
2066 const unsigned *
2067 brw_compile_vs(const struct brw_compiler *compiler, void *log_data,
2068 void *mem_ctx,
2069 const struct brw_vs_prog_key *key,
2070 struct brw_vs_prog_data *prog_data,
2071 const nir_shader *src_shader,
2072 gl_clip_plane *clip_planes,
2073 bool use_legacy_snorm_formula,
2074 int shader_time_index,
2075 unsigned *final_assembly_size,
2076 char **error_str)
2077 {
2078 const bool is_scalar = compiler->scalar_stage[MESA_SHADER_VERTEX];
2079 nir_shader *shader = nir_shader_clone(mem_ctx, src_shader);
2080 shader = brw_nir_apply_sampler_key(shader, compiler->devinfo, &key->tex,
2081 is_scalar);
2082 brw_nir_lower_vs_inputs(shader, compiler->devinfo, is_scalar,
2083 use_legacy_snorm_formula, key->gl_attrib_wa_flags);
2084 brw_nir_lower_vue_outputs(shader, is_scalar);
2085 shader = brw_postprocess_nir(shader, compiler->devinfo, is_scalar);
2086
2087 const unsigned *assembly = NULL;
2088
2089 unsigned nr_attributes = _mesa_bitcount_64(prog_data->inputs_read);
2090
2091 /* gl_VertexID and gl_InstanceID are system values, but arrive via an
2092 * incoming vertex attribute. So, add an extra slot.
2093 */
2094 if (shader->info.system_values_read &
2095 (BITFIELD64_BIT(SYSTEM_VALUE_BASE_VERTEX) |
2096 BITFIELD64_BIT(SYSTEM_VALUE_BASE_INSTANCE) |
2097 BITFIELD64_BIT(SYSTEM_VALUE_VERTEX_ID_ZERO_BASE) |
2098 BITFIELD64_BIT(SYSTEM_VALUE_INSTANCE_ID))) {
2099 nr_attributes++;
2100 }
2101
2102 /* gl_DrawID has its very own vec4 */
2103 if (shader->info.system_values_read & BITFIELD64_BIT(SYSTEM_VALUE_DRAW_ID)) {
2104 nr_attributes++;
2105 }
2106
2107 unsigned nr_attribute_slots =
2108 nr_attributes +
2109 _mesa_bitcount_64(shader->info.double_inputs_read);
2110
2111 /* The 3DSTATE_VS documentation lists the lower bound on "Vertex URB Entry
2112 * Read Length" as 1 in vec4 mode, and 0 in SIMD8 mode. Empirically, in
2113 * vec4 mode, the hardware appears to wedge unless we read something.
2114 */
2115 if (is_scalar)
2116 prog_data->base.urb_read_length =
2117 DIV_ROUND_UP(nr_attribute_slots, 2);
2118 else
2119 prog_data->base.urb_read_length =
2120 DIV_ROUND_UP(MAX2(nr_attribute_slots, 1), 2);
2121
2122 prog_data->nr_attributes = nr_attributes;
2123 prog_data->nr_attribute_slots = nr_attribute_slots;
2124
2125 /* Since vertex shaders reuse the same VUE entry for inputs and outputs
2126 * (overwriting the original contents), we need to make sure the size is
2127 * the larger of the two.
2128 */
2129 const unsigned vue_entries =
2130 MAX2(nr_attribute_slots, (unsigned)prog_data->base.vue_map.num_slots);
2131
2132 if (compiler->devinfo->gen == 6)
2133 prog_data->base.urb_entry_size = DIV_ROUND_UP(vue_entries, 8);
2134 else
2135 prog_data->base.urb_entry_size = DIV_ROUND_UP(vue_entries, 4);
2136
2137 if (is_scalar) {
2138 prog_data->base.dispatch_mode = DISPATCH_MODE_SIMD8;
2139
2140 fs_visitor v(compiler, log_data, mem_ctx, key, &prog_data->base.base,
2141 NULL, /* prog; Only used for TEXTURE_RECTANGLE on gen < 8 */
2142 shader, 8, shader_time_index);
2143 if (!v.run_vs(clip_planes)) {
2144 if (error_str)
2145 *error_str = ralloc_strdup(mem_ctx, v.fail_msg);
2146
2147 return NULL;
2148 }
2149
2150 prog_data->base.base.dispatch_grf_start_reg = v.payload.num_regs;
2151
2152 fs_generator g(compiler, log_data, mem_ctx, (void *) key,
2153 &prog_data->base.base, v.promoted_constants,
2154 v.runtime_check_aads_emit, MESA_SHADER_VERTEX);
2155 if (INTEL_DEBUG & DEBUG_VS) {
2156 const char *debug_name =
2157 ralloc_asprintf(mem_ctx, "%s vertex shader %s",
2158 shader->info.label ? shader->info.label : "unnamed",
2159 shader->info.name);
2160
2161 g.enable_debug(debug_name);
2162 }
2163 g.generate_code(v.cfg, 8);
2164 assembly = g.get_assembly(final_assembly_size);
2165 }
2166
2167 if (!assembly) {
2168 prog_data->base.dispatch_mode = DISPATCH_MODE_4X2_DUAL_OBJECT;
2169
2170 vec4_vs_visitor v(compiler, log_data, key, prog_data,
2171 shader, clip_planes, mem_ctx,
2172 shader_time_index, use_legacy_snorm_formula);
2173 if (!v.run()) {
2174 if (error_str)
2175 *error_str = ralloc_strdup(mem_ctx, v.fail_msg);
2176
2177 return NULL;
2178 }
2179
2180 assembly = brw_vec4_generate_assembly(compiler, log_data, mem_ctx,
2181 shader, &prog_data->base, v.cfg,
2182 final_assembly_size);
2183 }
2184
2185 return assembly;
2186 }
2187
2188 } /* extern "C" */