i965/vs: Make some vec4_visitor functions virtual.
[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_cfg.h"
26 #include "glsl/ir_print_visitor.h"
27
28 extern "C" {
29 #include "main/macros.h"
30 #include "main/shaderobj.h"
31 #include "program/prog_print.h"
32 #include "program/prog_parameter.h"
33 }
34
35 #define MAX_INSTRUCTION (1 << 30)
36
37 using namespace brw;
38
39 namespace brw {
40
41 /**
42 * Common helper for constructing swizzles. When only a subset of
43 * channels of a vec4 are used, we don't want to reference the other
44 * channels, as that will tell optimization passes that those other
45 * channels are used.
46 */
47 unsigned
48 swizzle_for_size(int size)
49 {
50 static const unsigned size_swizzles[4] = {
51 BRW_SWIZZLE4(SWIZZLE_X, SWIZZLE_X, SWIZZLE_X, SWIZZLE_X),
52 BRW_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Y, SWIZZLE_Y),
53 BRW_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_Z),
54 BRW_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_W),
55 };
56
57 assert((size >= 1) && (size <= 4));
58 return size_swizzles[size - 1];
59 }
60
61 void
62 src_reg::init()
63 {
64 memset(this, 0, sizeof(*this));
65
66 this->file = BAD_FILE;
67 }
68
69 src_reg::src_reg(register_file file, int reg, const glsl_type *type)
70 {
71 init();
72
73 this->file = file;
74 this->reg = reg;
75 if (type && (type->is_scalar() || type->is_vector() || type->is_matrix()))
76 this->swizzle = swizzle_for_size(type->vector_elements);
77 else
78 this->swizzle = SWIZZLE_XYZW;
79 }
80
81 /** Generic unset register constructor. */
82 src_reg::src_reg()
83 {
84 init();
85 }
86
87 src_reg::src_reg(float f)
88 {
89 init();
90
91 this->file = IMM;
92 this->type = BRW_REGISTER_TYPE_F;
93 this->imm.f = f;
94 }
95
96 src_reg::src_reg(uint32_t u)
97 {
98 init();
99
100 this->file = IMM;
101 this->type = BRW_REGISTER_TYPE_UD;
102 this->imm.u = u;
103 }
104
105 src_reg::src_reg(int32_t i)
106 {
107 init();
108
109 this->file = IMM;
110 this->type = BRW_REGISTER_TYPE_D;
111 this->imm.i = i;
112 }
113
114 src_reg::src_reg(dst_reg reg)
115 {
116 init();
117
118 this->file = reg.file;
119 this->reg = reg.reg;
120 this->reg_offset = reg.reg_offset;
121 this->type = reg.type;
122 this->reladdr = reg.reladdr;
123 this->fixed_hw_reg = reg.fixed_hw_reg;
124
125 int swizzles[4];
126 int next_chan = 0;
127 int last = 0;
128
129 for (int i = 0; i < 4; i++) {
130 if (!(reg.writemask & (1 << i)))
131 continue;
132
133 swizzles[next_chan++] = last = i;
134 }
135
136 for (; next_chan < 4; next_chan++) {
137 swizzles[next_chan] = last;
138 }
139
140 this->swizzle = BRW_SWIZZLE4(swizzles[0], swizzles[1],
141 swizzles[2], swizzles[3]);
142 }
143
144 bool
145 vec4_instruction::is_tex()
146 {
147 return (opcode == SHADER_OPCODE_TEX ||
148 opcode == SHADER_OPCODE_TXD ||
149 opcode == SHADER_OPCODE_TXF ||
150 opcode == SHADER_OPCODE_TXF_MS ||
151 opcode == SHADER_OPCODE_TXL ||
152 opcode == SHADER_OPCODE_TXS);
153 }
154
155 void
156 dst_reg::init()
157 {
158 memset(this, 0, sizeof(*this));
159 this->file = BAD_FILE;
160 this->writemask = WRITEMASK_XYZW;
161 }
162
163 dst_reg::dst_reg()
164 {
165 init();
166 }
167
168 dst_reg::dst_reg(register_file file, int reg)
169 {
170 init();
171
172 this->file = file;
173 this->reg = reg;
174 }
175
176 dst_reg::dst_reg(register_file file, int reg, const glsl_type *type,
177 int writemask)
178 {
179 init();
180
181 this->file = file;
182 this->reg = reg;
183 this->type = brw_type_for_base_type(type);
184 this->writemask = writemask;
185 }
186
187 dst_reg::dst_reg(struct brw_reg reg)
188 {
189 init();
190
191 this->file = HW_REG;
192 this->fixed_hw_reg = reg;
193 }
194
195 dst_reg::dst_reg(src_reg reg)
196 {
197 init();
198
199 this->file = reg.file;
200 this->reg = reg.reg;
201 this->reg_offset = reg.reg_offset;
202 this->type = reg.type;
203 /* How should we do writemasking when converting from a src_reg? It seems
204 * pretty obvious that for src.xxxx the caller wants to write to src.x, but
205 * what about for src.wx? Just special-case src.xxxx for now.
206 */
207 if (reg.swizzle == BRW_SWIZZLE_XXXX)
208 this->writemask = WRITEMASK_X;
209 else
210 this->writemask = WRITEMASK_XYZW;
211 this->reladdr = reg.reladdr;
212 this->fixed_hw_reg = reg.fixed_hw_reg;
213 }
214
215 bool
216 vec4_instruction::is_math()
217 {
218 return (opcode == SHADER_OPCODE_RCP ||
219 opcode == SHADER_OPCODE_RSQ ||
220 opcode == SHADER_OPCODE_SQRT ||
221 opcode == SHADER_OPCODE_EXP2 ||
222 opcode == SHADER_OPCODE_LOG2 ||
223 opcode == SHADER_OPCODE_SIN ||
224 opcode == SHADER_OPCODE_COS ||
225 opcode == SHADER_OPCODE_INT_QUOTIENT ||
226 opcode == SHADER_OPCODE_INT_REMAINDER ||
227 opcode == SHADER_OPCODE_POW);
228 }
229
230 bool
231 vec4_instruction::is_send_from_grf()
232 {
233 switch (opcode) {
234 case SHADER_OPCODE_SHADER_TIME_ADD:
235 case VS_OPCODE_PULL_CONSTANT_LOAD_GEN7:
236 return true;
237 default:
238 return false;
239 }
240 }
241
242 bool
243 vec4_visitor::can_do_source_mods(vec4_instruction *inst)
244 {
245 if (intel->gen == 6 && inst->is_math())
246 return false;
247
248 if (inst->is_send_from_grf())
249 return false;
250
251 return true;
252 }
253
254 /**
255 * Returns how many MRFs an opcode will write over.
256 *
257 * Note that this is not the 0 or 1 implied writes in an actual gen
258 * instruction -- the generate_* functions generate additional MOVs
259 * for setup.
260 */
261 int
262 vec4_visitor::implied_mrf_writes(vec4_instruction *inst)
263 {
264 if (inst->mlen == 0)
265 return 0;
266
267 switch (inst->opcode) {
268 case SHADER_OPCODE_RCP:
269 case SHADER_OPCODE_RSQ:
270 case SHADER_OPCODE_SQRT:
271 case SHADER_OPCODE_EXP2:
272 case SHADER_OPCODE_LOG2:
273 case SHADER_OPCODE_SIN:
274 case SHADER_OPCODE_COS:
275 return 1;
276 case SHADER_OPCODE_POW:
277 return 2;
278 case VS_OPCODE_URB_WRITE:
279 return 1;
280 case VS_OPCODE_PULL_CONSTANT_LOAD:
281 return 2;
282 case VS_OPCODE_SCRATCH_READ:
283 return 2;
284 case VS_OPCODE_SCRATCH_WRITE:
285 return 3;
286 case SHADER_OPCODE_SHADER_TIME_ADD:
287 return 0;
288 default:
289 assert(!"not reached");
290 return inst->mlen;
291 }
292 }
293
294 bool
295 src_reg::equals(src_reg *r)
296 {
297 return (file == r->file &&
298 reg == r->reg &&
299 reg_offset == r->reg_offset &&
300 type == r->type &&
301 negate == r->negate &&
302 abs == r->abs &&
303 swizzle == r->swizzle &&
304 !reladdr && !r->reladdr &&
305 memcmp(&fixed_hw_reg, &r->fixed_hw_reg,
306 sizeof(fixed_hw_reg)) == 0 &&
307 imm.u == r->imm.u);
308 }
309
310 /**
311 * Must be called after calculate_live_intervales() to remove unused
312 * writes to registers -- register allocation will fail otherwise
313 * because something deffed but not used won't be considered to
314 * interfere with other regs.
315 */
316 bool
317 vec4_visitor::dead_code_eliminate()
318 {
319 bool progress = false;
320 int pc = 0;
321
322 calculate_live_intervals();
323
324 foreach_list_safe(node, &this->instructions) {
325 vec4_instruction *inst = (vec4_instruction *)node;
326
327 if (inst->dst.file == GRF && this->virtual_grf_use[inst->dst.reg] <= pc) {
328 inst->remove();
329 progress = true;
330 }
331
332 pc++;
333 }
334
335 if (progress)
336 live_intervals_valid = false;
337
338 return progress;
339 }
340
341 void
342 vec4_visitor::split_uniform_registers()
343 {
344 /* Prior to this, uniforms have been in an array sized according to
345 * the number of vector uniforms present, sparsely filled (so an
346 * aggregate results in reg indices being skipped over). Now we're
347 * going to cut those aggregates up so each .reg index is one
348 * vector. The goal is to make elimination of unused uniform
349 * components easier later.
350 */
351 foreach_list(node, &this->instructions) {
352 vec4_instruction *inst = (vec4_instruction *)node;
353
354 for (int i = 0 ; i < 3; i++) {
355 if (inst->src[i].file != UNIFORM)
356 continue;
357
358 assert(!inst->src[i].reladdr);
359
360 inst->src[i].reg += inst->src[i].reg_offset;
361 inst->src[i].reg_offset = 0;
362 }
363 }
364
365 /* Update that everything is now vector-sized. */
366 for (int i = 0; i < this->uniforms; i++) {
367 this->uniform_size[i] = 1;
368 }
369 }
370
371 void
372 vec4_visitor::pack_uniform_registers()
373 {
374 bool uniform_used[this->uniforms];
375 int new_loc[this->uniforms];
376 int new_chan[this->uniforms];
377
378 memset(uniform_used, 0, sizeof(uniform_used));
379 memset(new_loc, 0, sizeof(new_loc));
380 memset(new_chan, 0, sizeof(new_chan));
381
382 /* Find which uniform vectors are actually used by the program. We
383 * expect unused vector elements when we've moved array access out
384 * to pull constants, and from some GLSL code generators like wine.
385 */
386 foreach_list(node, &this->instructions) {
387 vec4_instruction *inst = (vec4_instruction *)node;
388
389 for (int i = 0 ; i < 3; i++) {
390 if (inst->src[i].file != UNIFORM)
391 continue;
392
393 uniform_used[inst->src[i].reg] = true;
394 }
395 }
396
397 int new_uniform_count = 0;
398
399 /* Now, figure out a packing of the live uniform vectors into our
400 * push constants.
401 */
402 for (int src = 0; src < uniforms; src++) {
403 int size = this->uniform_vector_size[src];
404
405 if (!uniform_used[src]) {
406 this->uniform_vector_size[src] = 0;
407 continue;
408 }
409
410 int dst;
411 /* Find the lowest place we can slot this uniform in. */
412 for (dst = 0; dst < src; dst++) {
413 if (this->uniform_vector_size[dst] + size <= 4)
414 break;
415 }
416
417 if (src == dst) {
418 new_loc[src] = dst;
419 new_chan[src] = 0;
420 } else {
421 new_loc[src] = dst;
422 new_chan[src] = this->uniform_vector_size[dst];
423
424 /* Move the references to the data */
425 for (int j = 0; j < size; j++) {
426 prog_data->base.param[dst * 4 + new_chan[src] + j] =
427 prog_data->base.param[src * 4 + j];
428 }
429
430 this->uniform_vector_size[dst] += size;
431 this->uniform_vector_size[src] = 0;
432 }
433
434 new_uniform_count = MAX2(new_uniform_count, dst + 1);
435 }
436
437 this->uniforms = new_uniform_count;
438
439 /* Now, update the instructions for our repacked uniforms. */
440 foreach_list(node, &this->instructions) {
441 vec4_instruction *inst = (vec4_instruction *)node;
442
443 for (int i = 0 ; i < 3; i++) {
444 int src = inst->src[i].reg;
445
446 if (inst->src[i].file != UNIFORM)
447 continue;
448
449 inst->src[i].reg = new_loc[src];
450
451 int sx = BRW_GET_SWZ(inst->src[i].swizzle, 0) + new_chan[src];
452 int sy = BRW_GET_SWZ(inst->src[i].swizzle, 1) + new_chan[src];
453 int sz = BRW_GET_SWZ(inst->src[i].swizzle, 2) + new_chan[src];
454 int sw = BRW_GET_SWZ(inst->src[i].swizzle, 3) + new_chan[src];
455 inst->src[i].swizzle = BRW_SWIZZLE4(sx, sy, sz, sw);
456 }
457 }
458 }
459
460 bool
461 src_reg::is_zero() const
462 {
463 if (file != IMM)
464 return false;
465
466 if (type == BRW_REGISTER_TYPE_F) {
467 return imm.f == 0.0;
468 } else {
469 return imm.i == 0;
470 }
471 }
472
473 bool
474 src_reg::is_one() const
475 {
476 if (file != IMM)
477 return false;
478
479 if (type == BRW_REGISTER_TYPE_F) {
480 return imm.f == 1.0;
481 } else {
482 return imm.i == 1;
483 }
484 }
485
486 /**
487 * Does algebraic optimizations (0 * a = 0, 1 * a = a, a + 0 = a).
488 *
489 * While GLSL IR also performs this optimization, we end up with it in
490 * our instruction stream for a couple of reasons. One is that we
491 * sometimes generate silly instructions, for example in array access
492 * where we'll generate "ADD offset, index, base" even if base is 0.
493 * The other is that GLSL IR's constant propagation doesn't track the
494 * components of aggregates, so some VS patterns (initialize matrix to
495 * 0, accumulate in vertex blending factors) end up breaking down to
496 * instructions involving 0.
497 */
498 bool
499 vec4_visitor::opt_algebraic()
500 {
501 bool progress = false;
502
503 foreach_list(node, &this->instructions) {
504 vec4_instruction *inst = (vec4_instruction *)node;
505
506 switch (inst->opcode) {
507 case BRW_OPCODE_ADD:
508 if (inst->src[1].is_zero()) {
509 inst->opcode = BRW_OPCODE_MOV;
510 inst->src[1] = src_reg();
511 progress = true;
512 }
513 break;
514
515 case BRW_OPCODE_MUL:
516 if (inst->src[1].is_zero()) {
517 inst->opcode = BRW_OPCODE_MOV;
518 switch (inst->src[0].type) {
519 case BRW_REGISTER_TYPE_F:
520 inst->src[0] = src_reg(0.0f);
521 break;
522 case BRW_REGISTER_TYPE_D:
523 inst->src[0] = src_reg(0);
524 break;
525 case BRW_REGISTER_TYPE_UD:
526 inst->src[0] = src_reg(0u);
527 break;
528 default:
529 assert(!"not reached");
530 inst->src[0] = src_reg(0.0f);
531 break;
532 }
533 inst->src[1] = src_reg();
534 progress = true;
535 } else if (inst->src[1].is_one()) {
536 inst->opcode = BRW_OPCODE_MOV;
537 inst->src[1] = src_reg();
538 progress = true;
539 }
540 break;
541 default:
542 break;
543 }
544 }
545
546 if (progress)
547 this->live_intervals_valid = false;
548
549 return progress;
550 }
551
552 /**
553 * Only a limited number of hardware registers may be used for push
554 * constants, so this turns access to the overflowed constants into
555 * pull constants.
556 */
557 void
558 vec4_visitor::move_push_constants_to_pull_constants()
559 {
560 int pull_constant_loc[this->uniforms];
561
562 /* Only allow 32 registers (256 uniform components) as push constants,
563 * which is the limit on gen6.
564 */
565 int max_uniform_components = 32 * 8;
566 if (this->uniforms * 4 <= max_uniform_components)
567 return;
568
569 /* Make some sort of choice as to which uniforms get sent to pull
570 * constants. We could potentially do something clever here like
571 * look for the most infrequently used uniform vec4s, but leave
572 * that for later.
573 */
574 for (int i = 0; i < this->uniforms * 4; i += 4) {
575 pull_constant_loc[i / 4] = -1;
576
577 if (i >= max_uniform_components) {
578 const float **values = &prog_data->base.param[i];
579
580 /* Try to find an existing copy of this uniform in the pull
581 * constants if it was part of an array access already.
582 */
583 for (unsigned int j = 0; j < prog_data->base.nr_pull_params; j += 4) {
584 int matches;
585
586 for (matches = 0; matches < 4; matches++) {
587 if (prog_data->base.pull_param[j + matches] != values[matches])
588 break;
589 }
590
591 if (matches == 4) {
592 pull_constant_loc[i / 4] = j / 4;
593 break;
594 }
595 }
596
597 if (pull_constant_loc[i / 4] == -1) {
598 assert(prog_data->base.nr_pull_params % 4 == 0);
599 pull_constant_loc[i / 4] = prog_data->base.nr_pull_params / 4;
600
601 for (int j = 0; j < 4; j++) {
602 prog_data->base.pull_param[prog_data->base.nr_pull_params++] = values[j];
603 }
604 }
605 }
606 }
607
608 /* Now actually rewrite usage of the things we've moved to pull
609 * constants.
610 */
611 foreach_list_safe(node, &this->instructions) {
612 vec4_instruction *inst = (vec4_instruction *)node;
613
614 for (int i = 0 ; i < 3; i++) {
615 if (inst->src[i].file != UNIFORM ||
616 pull_constant_loc[inst->src[i].reg] == -1)
617 continue;
618
619 int uniform = inst->src[i].reg;
620
621 dst_reg temp = dst_reg(this, glsl_type::vec4_type);
622
623 emit_pull_constant_load(inst, temp, inst->src[i],
624 pull_constant_loc[uniform]);
625
626 inst->src[i].file = temp.file;
627 inst->src[i].reg = temp.reg;
628 inst->src[i].reg_offset = temp.reg_offset;
629 inst->src[i].reladdr = NULL;
630 }
631 }
632
633 /* Repack push constants to remove the now-unused ones. */
634 pack_uniform_registers();
635 }
636
637 /**
638 * Sets the dependency control fields on instructions after register
639 * allocation and before the generator is run.
640 *
641 * When you have a sequence of instructions like:
642 *
643 * DP4 temp.x vertex uniform[0]
644 * DP4 temp.y vertex uniform[0]
645 * DP4 temp.z vertex uniform[0]
646 * DP4 temp.w vertex uniform[0]
647 *
648 * The hardware doesn't know that it can actually run the later instructions
649 * while the previous ones are in flight, producing stalls. However, we have
650 * manual fields we can set in the instructions that let it do so.
651 */
652 void
653 vec4_visitor::opt_set_dependency_control()
654 {
655 vec4_instruction *last_grf_write[BRW_MAX_GRF];
656 uint8_t grf_channels_written[BRW_MAX_GRF];
657 vec4_instruction *last_mrf_write[BRW_MAX_GRF];
658 uint8_t mrf_channels_written[BRW_MAX_GRF];
659
660 cfg_t cfg(this);
661
662 assert(prog_data->base.total_grf ||
663 !"Must be called after register allocation");
664
665 for (int i = 0; i < cfg.num_blocks; i++) {
666 bblock_t *bblock = cfg.blocks[i];
667 vec4_instruction *inst;
668
669 memset(last_grf_write, 0, sizeof(last_grf_write));
670 memset(last_mrf_write, 0, sizeof(last_mrf_write));
671
672 for (inst = (vec4_instruction *)bblock->start;
673 inst != (vec4_instruction *)bblock->end->next;
674 inst = (vec4_instruction *)inst->next) {
675 /* If we read from a register that we were doing dependency control
676 * on, don't do dependency control across the read.
677 */
678 for (int i = 0; i < 3; i++) {
679 int reg = inst->src[i].reg + inst->src[i].reg_offset;
680 if (inst->src[i].file == GRF) {
681 last_grf_write[reg] = NULL;
682 } else if (inst->src[i].file == HW_REG) {
683 memset(last_grf_write, 0, sizeof(last_grf_write));
684 break;
685 }
686 assert(inst->src[i].file != MRF);
687 }
688
689 /* In the presence of send messages, totally interrupt dependency
690 * control. They're long enough that the chance of dependency
691 * control around them just doesn't matter.
692 */
693 if (inst->mlen) {
694 memset(last_grf_write, 0, sizeof(last_grf_write));
695 memset(last_mrf_write, 0, sizeof(last_mrf_write));
696 continue;
697 }
698
699 /* It looks like setting dependency control on a predicated
700 * instruction hangs the GPU.
701 */
702 if (inst->predicate) {
703 memset(last_grf_write, 0, sizeof(last_grf_write));
704 memset(last_mrf_write, 0, sizeof(last_mrf_write));
705 continue;
706 }
707
708 /* Now, see if we can do dependency control for this instruction
709 * against a previous one writing to its destination.
710 */
711 int reg = inst->dst.reg + inst->dst.reg_offset;
712 if (inst->dst.file == GRF) {
713 if (last_grf_write[reg] &&
714 !(inst->dst.writemask & grf_channels_written[reg])) {
715 last_grf_write[reg]->no_dd_clear = true;
716 inst->no_dd_check = true;
717 } else {
718 grf_channels_written[reg] = 0;
719 }
720
721 last_grf_write[reg] = inst;
722 grf_channels_written[reg] |= inst->dst.writemask;
723 } else if (inst->dst.file == MRF) {
724 if (last_mrf_write[reg] &&
725 !(inst->dst.writemask & mrf_channels_written[reg])) {
726 last_mrf_write[reg]->no_dd_clear = true;
727 inst->no_dd_check = true;
728 } else {
729 mrf_channels_written[reg] = 0;
730 }
731
732 last_mrf_write[reg] = inst;
733 mrf_channels_written[reg] |= inst->dst.writemask;
734 } else if (inst->dst.reg == HW_REG) {
735 if (inst->dst.fixed_hw_reg.file == BRW_GENERAL_REGISTER_FILE)
736 memset(last_grf_write, 0, sizeof(last_grf_write));
737 if (inst->dst.fixed_hw_reg.file == BRW_MESSAGE_REGISTER_FILE)
738 memset(last_mrf_write, 0, sizeof(last_mrf_write));
739 }
740 }
741 }
742 }
743
744 bool
745 vec4_instruction::can_reswizzle_dst(int dst_writemask,
746 int swizzle,
747 int swizzle_mask)
748 {
749 /* If this instruction sets anything not referenced by swizzle, then we'd
750 * totally break it when we reswizzle.
751 */
752 if (dst.writemask & ~swizzle_mask)
753 return false;
754
755 switch (opcode) {
756 case BRW_OPCODE_DP4:
757 case BRW_OPCODE_DP3:
758 case BRW_OPCODE_DP2:
759 return true;
760 default:
761 /* Check if there happens to be no reswizzling required. */
762 for (int c = 0; c < 4; c++) {
763 int bit = 1 << BRW_GET_SWZ(swizzle, c);
764 /* Skip components of the swizzle not used by the dst. */
765 if (!(dst_writemask & (1 << c)))
766 continue;
767
768 /* We don't do the reswizzling yet, so just sanity check that we
769 * don't have to.
770 */
771 if (bit != (1 << c))
772 return false;
773 }
774 return true;
775 }
776 }
777
778 /**
779 * For any channels in the swizzle's source that were populated by this
780 * instruction, rewrite the instruction to put the appropriate result directly
781 * in those channels.
782 *
783 * e.g. for swizzle=yywx, MUL a.xy b c -> MUL a.yy_x b.yy z.yy_x
784 */
785 void
786 vec4_instruction::reswizzle_dst(int dst_writemask, int swizzle)
787 {
788 int new_writemask = 0;
789
790 switch (opcode) {
791 case BRW_OPCODE_DP4:
792 case BRW_OPCODE_DP3:
793 case BRW_OPCODE_DP2:
794 for (int c = 0; c < 4; c++) {
795 int bit = 1 << BRW_GET_SWZ(swizzle, c);
796 /* Skip components of the swizzle not used by the dst. */
797 if (!(dst_writemask & (1 << c)))
798 continue;
799 /* If we were populating this component, then populate the
800 * corresponding channel of the new dst.
801 */
802 if (dst.writemask & bit)
803 new_writemask |= (1 << c);
804 }
805 dst.writemask = new_writemask;
806 break;
807 default:
808 for (int c = 0; c < 4; c++) {
809 int bit = 1 << BRW_GET_SWZ(swizzle, c);
810 /* Skip components of the swizzle not used by the dst. */
811 if (!(dst_writemask & (1 << c)))
812 continue;
813
814 /* We don't do the reswizzling yet, so just sanity check that we
815 * don't have to.
816 */
817 assert(bit == (1 << c));
818 }
819 break;
820 }
821 }
822
823 /*
824 * Tries to reduce extra MOV instructions by taking temporary GRFs that get
825 * just written and then MOVed into another reg and making the original write
826 * of the GRF write directly to the final destination instead.
827 */
828 bool
829 vec4_visitor::opt_register_coalesce()
830 {
831 bool progress = false;
832 int next_ip = 0;
833
834 calculate_live_intervals();
835
836 foreach_list_safe(node, &this->instructions) {
837 vec4_instruction *inst = (vec4_instruction *)node;
838
839 int ip = next_ip;
840 next_ip++;
841
842 if (inst->opcode != BRW_OPCODE_MOV ||
843 (inst->dst.file != GRF && inst->dst.file != MRF) ||
844 inst->predicate ||
845 inst->src[0].file != GRF ||
846 inst->dst.type != inst->src[0].type ||
847 inst->src[0].abs || inst->src[0].negate || inst->src[0].reladdr)
848 continue;
849
850 bool to_mrf = (inst->dst.file == MRF);
851
852 /* Can't coalesce this GRF if someone else was going to
853 * read it later.
854 */
855 if (this->virtual_grf_use[inst->src[0].reg] > ip)
856 continue;
857
858 /* We need to check interference with the final destination between this
859 * instruction and the earliest instruction involved in writing the GRF
860 * we're eliminating. To do that, keep track of which of our source
861 * channels we've seen initialized.
862 */
863 bool chans_needed[4] = {false, false, false, false};
864 int chans_remaining = 0;
865 int swizzle_mask = 0;
866 for (int i = 0; i < 4; i++) {
867 int chan = BRW_GET_SWZ(inst->src[0].swizzle, i);
868
869 if (!(inst->dst.writemask & (1 << i)))
870 continue;
871
872 swizzle_mask |= (1 << chan);
873
874 if (!chans_needed[chan]) {
875 chans_needed[chan] = true;
876 chans_remaining++;
877 }
878 }
879
880 /* Now walk up the instruction stream trying to see if we can rewrite
881 * everything writing to the temporary to write into the destination
882 * instead.
883 */
884 vec4_instruction *scan_inst;
885 for (scan_inst = (vec4_instruction *)inst->prev;
886 scan_inst->prev != NULL;
887 scan_inst = (vec4_instruction *)scan_inst->prev) {
888 if (scan_inst->dst.file == GRF &&
889 scan_inst->dst.reg == inst->src[0].reg &&
890 scan_inst->dst.reg_offset == inst->src[0].reg_offset) {
891 /* Found something writing to the reg we want to coalesce away. */
892 if (to_mrf) {
893 /* SEND instructions can't have MRF as a destination. */
894 if (scan_inst->mlen)
895 break;
896
897 if (intel->gen == 6) {
898 /* gen6 math instructions must have the destination be
899 * GRF, so no compute-to-MRF for them.
900 */
901 if (scan_inst->is_math()) {
902 break;
903 }
904 }
905 }
906
907 /* If we can't handle the swizzle, bail. */
908 if (!scan_inst->can_reswizzle_dst(inst->dst.writemask,
909 inst->src[0].swizzle,
910 swizzle_mask)) {
911 break;
912 }
913
914 /* Mark which channels we found unconditional writes for. */
915 if (!scan_inst->predicate) {
916 for (int i = 0; i < 4; i++) {
917 if (scan_inst->dst.writemask & (1 << i) &&
918 chans_needed[i]) {
919 chans_needed[i] = false;
920 chans_remaining--;
921 }
922 }
923 }
924
925 if (chans_remaining == 0)
926 break;
927 }
928
929 /* We don't handle flow control here. Most computation of values
930 * that could be coalesced happens just before their use.
931 */
932 if (scan_inst->opcode == BRW_OPCODE_DO ||
933 scan_inst->opcode == BRW_OPCODE_WHILE ||
934 scan_inst->opcode == BRW_OPCODE_ELSE ||
935 scan_inst->opcode == BRW_OPCODE_ENDIF) {
936 break;
937 }
938
939 /* You can't read from an MRF, so if someone else reads our MRF's
940 * source GRF that we wanted to rewrite, that stops us. If it's a
941 * GRF we're trying to coalesce to, we don't actually handle
942 * rewriting sources so bail in that case as well.
943 */
944 bool interfered = false;
945 for (int i = 0; i < 3; i++) {
946 if (scan_inst->src[i].file == GRF &&
947 scan_inst->src[i].reg == inst->src[0].reg &&
948 scan_inst->src[i].reg_offset == inst->src[0].reg_offset) {
949 interfered = true;
950 }
951 }
952 if (interfered)
953 break;
954
955 /* If somebody else writes our destination here, we can't coalesce
956 * before that.
957 */
958 if (scan_inst->dst.file == inst->dst.file &&
959 scan_inst->dst.reg == inst->dst.reg) {
960 break;
961 }
962
963 /* Check for reads of the register we're trying to coalesce into. We
964 * can't go rewriting instructions above that to put some other value
965 * in the register instead.
966 */
967 if (to_mrf && scan_inst->mlen > 0) {
968 if (inst->dst.reg >= scan_inst->base_mrf &&
969 inst->dst.reg < scan_inst->base_mrf + scan_inst->mlen) {
970 break;
971 }
972 } else {
973 for (int i = 0; i < 3; i++) {
974 if (scan_inst->src[i].file == inst->dst.file &&
975 scan_inst->src[i].reg == inst->dst.reg &&
976 scan_inst->src[i].reg_offset == inst->src[0].reg_offset) {
977 interfered = true;
978 }
979 }
980 if (interfered)
981 break;
982 }
983 }
984
985 if (chans_remaining == 0) {
986 /* If we've made it here, we have an MOV we want to coalesce out, and
987 * a scan_inst pointing to the earliest instruction involved in
988 * computing the value. Now go rewrite the instruction stream
989 * between the two.
990 */
991
992 while (scan_inst != inst) {
993 if (scan_inst->dst.file == GRF &&
994 scan_inst->dst.reg == inst->src[0].reg &&
995 scan_inst->dst.reg_offset == inst->src[0].reg_offset) {
996 scan_inst->reswizzle_dst(inst->dst.writemask,
997 inst->src[0].swizzle);
998 scan_inst->dst.file = inst->dst.file;
999 scan_inst->dst.reg = inst->dst.reg;
1000 scan_inst->dst.reg_offset = inst->dst.reg_offset;
1001 scan_inst->saturate |= inst->saturate;
1002 }
1003 scan_inst = (vec4_instruction *)scan_inst->next;
1004 }
1005 inst->remove();
1006 progress = true;
1007 }
1008 }
1009
1010 if (progress)
1011 live_intervals_valid = false;
1012
1013 return progress;
1014 }
1015
1016 /**
1017 * Splits virtual GRFs requesting more than one contiguous physical register.
1018 *
1019 * We initially create large virtual GRFs for temporary structures, arrays,
1020 * and matrices, so that the dereference visitor functions can add reg_offsets
1021 * to work their way down to the actual member being accessed. But when it
1022 * comes to optimization, we'd like to treat each register as individual
1023 * storage if possible.
1024 *
1025 * So far, the only thing that might prevent splitting is a send message from
1026 * a GRF on IVB.
1027 */
1028 void
1029 vec4_visitor::split_virtual_grfs()
1030 {
1031 int num_vars = this->virtual_grf_count;
1032 int new_virtual_grf[num_vars];
1033 bool split_grf[num_vars];
1034
1035 memset(new_virtual_grf, 0, sizeof(new_virtual_grf));
1036
1037 /* Try to split anything > 0 sized. */
1038 for (int i = 0; i < num_vars; i++) {
1039 split_grf[i] = this->virtual_grf_sizes[i] != 1;
1040 }
1041
1042 /* Check that the instructions are compatible with the registers we're trying
1043 * to split.
1044 */
1045 foreach_list(node, &this->instructions) {
1046 vec4_instruction *inst = (vec4_instruction *)node;
1047
1048 /* If there's a SEND message loading from a GRF on gen7+, it needs to be
1049 * contiguous. Assume that the GRF for the SEND is always in src[0].
1050 */
1051 if (inst->is_send_from_grf()) {
1052 split_grf[inst->src[0].reg] = false;
1053 }
1054 }
1055
1056 /* Allocate new space for split regs. Note that the virtual
1057 * numbers will be contiguous.
1058 */
1059 for (int i = 0; i < num_vars; i++) {
1060 if (!split_grf[i])
1061 continue;
1062
1063 new_virtual_grf[i] = virtual_grf_alloc(1);
1064 for (int j = 2; j < this->virtual_grf_sizes[i]; j++) {
1065 int reg = virtual_grf_alloc(1);
1066 assert(reg == new_virtual_grf[i] + j - 1);
1067 (void) reg;
1068 }
1069 this->virtual_grf_sizes[i] = 1;
1070 }
1071
1072 foreach_list(node, &this->instructions) {
1073 vec4_instruction *inst = (vec4_instruction *)node;
1074
1075 if (inst->dst.file == GRF && split_grf[inst->dst.reg] &&
1076 inst->dst.reg_offset != 0) {
1077 inst->dst.reg = (new_virtual_grf[inst->dst.reg] +
1078 inst->dst.reg_offset - 1);
1079 inst->dst.reg_offset = 0;
1080 }
1081 for (int i = 0; i < 3; i++) {
1082 if (inst->src[i].file == GRF && split_grf[inst->src[i].reg] &&
1083 inst->src[i].reg_offset != 0) {
1084 inst->src[i].reg = (new_virtual_grf[inst->src[i].reg] +
1085 inst->src[i].reg_offset - 1);
1086 inst->src[i].reg_offset = 0;
1087 }
1088 }
1089 }
1090 this->live_intervals_valid = false;
1091 }
1092
1093 void
1094 vec4_visitor::dump_instruction(vec4_instruction *inst)
1095 {
1096 printf("%s ", brw_instruction_name(inst->opcode));
1097
1098 switch (inst->dst.file) {
1099 case GRF:
1100 printf("vgrf%d.%d", inst->dst.reg, inst->dst.reg_offset);
1101 break;
1102 case MRF:
1103 printf("m%d", inst->dst.reg);
1104 break;
1105 case BAD_FILE:
1106 printf("(null)");
1107 break;
1108 default:
1109 printf("???");
1110 break;
1111 }
1112 if (inst->dst.writemask != WRITEMASK_XYZW) {
1113 printf(".");
1114 if (inst->dst.writemask & 1)
1115 printf("x");
1116 if (inst->dst.writemask & 2)
1117 printf("y");
1118 if (inst->dst.writemask & 4)
1119 printf("z");
1120 if (inst->dst.writemask & 8)
1121 printf("w");
1122 }
1123 printf(", ");
1124
1125 for (int i = 0; i < 3; i++) {
1126 switch (inst->src[i].file) {
1127 case GRF:
1128 printf("vgrf%d", inst->src[i].reg);
1129 break;
1130 case ATTR:
1131 printf("attr%d", inst->src[i].reg);
1132 break;
1133 case UNIFORM:
1134 printf("u%d", inst->src[i].reg);
1135 break;
1136 case IMM:
1137 switch (inst->src[i].type) {
1138 case BRW_REGISTER_TYPE_F:
1139 printf("%fF", inst->src[i].imm.f);
1140 break;
1141 case BRW_REGISTER_TYPE_D:
1142 printf("%dD", inst->src[i].imm.i);
1143 break;
1144 case BRW_REGISTER_TYPE_UD:
1145 printf("%uU", inst->src[i].imm.u);
1146 break;
1147 default:
1148 printf("???");
1149 break;
1150 }
1151 break;
1152 case BAD_FILE:
1153 printf("(null)");
1154 break;
1155 default:
1156 printf("???");
1157 break;
1158 }
1159
1160 if (inst->src[i].reg_offset)
1161 printf(".%d", inst->src[i].reg_offset);
1162
1163 static const char *chans[4] = {"x", "y", "z", "w"};
1164 printf(".");
1165 for (int c = 0; c < 4; c++) {
1166 printf("%s", chans[BRW_GET_SWZ(inst->src[i].swizzle, c)]);
1167 }
1168
1169 if (i < 3)
1170 printf(", ");
1171 }
1172
1173 printf("\n");
1174 }
1175
1176 void
1177 vec4_visitor::dump_instructions()
1178 {
1179 int ip = 0;
1180 foreach_list_safe(node, &this->instructions) {
1181 vec4_instruction *inst = (vec4_instruction *)node;
1182 printf("%d: ", ip++);
1183 dump_instruction(inst);
1184 }
1185 }
1186
1187 int
1188 vec4_vs_visitor::setup_attributes(int payload_reg)
1189 {
1190 int nr_attributes;
1191 int attribute_map[VERT_ATTRIB_MAX + 1];
1192
1193 nr_attributes = 0;
1194 for (int i = 0; i < VERT_ATTRIB_MAX; i++) {
1195 if (prog_data->inputs_read & BITFIELD64_BIT(i)) {
1196 attribute_map[i] = payload_reg + nr_attributes;
1197 nr_attributes++;
1198 }
1199 }
1200
1201 /* VertexID is stored by the VF as the last vertex element, but we
1202 * don't represent it with a flag in inputs_read, so we call it
1203 * VERT_ATTRIB_MAX.
1204 */
1205 if (prog_data->uses_vertexid) {
1206 attribute_map[VERT_ATTRIB_MAX] = payload_reg + nr_attributes;
1207 nr_attributes++;
1208 }
1209
1210 foreach_list(node, &this->instructions) {
1211 vec4_instruction *inst = (vec4_instruction *)node;
1212
1213 /* We have to support ATTR as a destination for GL_FIXED fixup. */
1214 if (inst->dst.file == ATTR) {
1215 int grf = attribute_map[inst->dst.reg + inst->dst.reg_offset];
1216
1217 struct brw_reg reg = brw_vec8_grf(grf, 0);
1218 reg.type = inst->dst.type;
1219 reg.dw1.bits.writemask = inst->dst.writemask;
1220
1221 inst->dst.file = HW_REG;
1222 inst->dst.fixed_hw_reg = reg;
1223 }
1224
1225 for (int i = 0; i < 3; i++) {
1226 if (inst->src[i].file != ATTR)
1227 continue;
1228
1229 int grf = attribute_map[inst->src[i].reg + inst->src[i].reg_offset];
1230
1231 struct brw_reg reg = brw_vec8_grf(grf, 0);
1232 reg.dw1.bits.swizzle = inst->src[i].swizzle;
1233 reg.type = inst->src[i].type;
1234 if (inst->src[i].abs)
1235 reg = brw_abs(reg);
1236 if (inst->src[i].negate)
1237 reg = negate(reg);
1238
1239 inst->src[i].file = HW_REG;
1240 inst->src[i].fixed_hw_reg = reg;
1241 }
1242 }
1243
1244 /* The BSpec says we always have to read at least one thing from
1245 * the VF, and it appears that the hardware wedges otherwise.
1246 */
1247 if (nr_attributes == 0)
1248 nr_attributes = 1;
1249
1250 prog_data->base.urb_read_length = (nr_attributes + 1) / 2;
1251
1252 unsigned vue_entries =
1253 MAX2(nr_attributes, prog_data->base.vue_map.num_slots);
1254
1255 if (intel->gen == 6)
1256 prog_data->base.urb_entry_size = ALIGN(vue_entries, 8) / 8;
1257 else
1258 prog_data->base.urb_entry_size = ALIGN(vue_entries, 4) / 4;
1259
1260 return payload_reg + nr_attributes;
1261 }
1262
1263 int
1264 vec4_visitor::setup_uniforms(int reg)
1265 {
1266 /* The pre-gen6 VS requires that some push constants get loaded no
1267 * matter what, or the GPU would hang.
1268 */
1269 if (intel->gen < 6 && this->uniforms == 0) {
1270 this->uniform_vector_size[this->uniforms] = 1;
1271
1272 for (unsigned int i = 0; i < 4; i++) {
1273 unsigned int slot = this->uniforms * 4 + i;
1274 static float zero = 0.0;
1275 prog_data->base.param[slot] = &zero;
1276 }
1277
1278 this->uniforms++;
1279 reg++;
1280 } else {
1281 reg += ALIGN(uniforms, 2) / 2;
1282 }
1283
1284 prog_data->base.nr_params = this->uniforms * 4;
1285
1286 prog_data->base.curb_read_length = reg - 1;
1287
1288 return reg;
1289 }
1290
1291 void
1292 vec4_visitor::setup_payload(void)
1293 {
1294 int reg = 0;
1295
1296 /* The payload always contains important data in g0, which contains
1297 * the URB handles that are passed on to the URB write at the end
1298 * of the thread. So, we always start push constants at g1.
1299 */
1300 reg++;
1301
1302 reg = setup_uniforms(reg);
1303
1304 reg = setup_attributes(reg);
1305
1306 this->first_non_payload_grf = reg;
1307 }
1308
1309 src_reg
1310 vec4_visitor::get_timestamp()
1311 {
1312 assert(intel->gen >= 7);
1313
1314 src_reg ts = src_reg(brw_reg(BRW_ARCHITECTURE_REGISTER_FILE,
1315 BRW_ARF_TIMESTAMP,
1316 0,
1317 BRW_REGISTER_TYPE_UD,
1318 BRW_VERTICAL_STRIDE_0,
1319 BRW_WIDTH_4,
1320 BRW_HORIZONTAL_STRIDE_4,
1321 BRW_SWIZZLE_XYZW,
1322 WRITEMASK_XYZW));
1323
1324 dst_reg dst = dst_reg(this, glsl_type::uvec4_type);
1325
1326 vec4_instruction *mov = emit(MOV(dst, ts));
1327 /* We want to read the 3 fields we care about (mostly field 0, but also 2)
1328 * even if it's not enabled in the dispatch.
1329 */
1330 mov->force_writemask_all = true;
1331
1332 return src_reg(dst);
1333 }
1334
1335 void
1336 vec4_visitor::emit_shader_time_begin()
1337 {
1338 current_annotation = "shader time start";
1339 shader_start_time = get_timestamp();
1340 }
1341
1342 void
1343 vec4_visitor::emit_shader_time_end()
1344 {
1345 current_annotation = "shader time end";
1346 src_reg shader_end_time = get_timestamp();
1347
1348
1349 /* Check that there weren't any timestamp reset events (assuming these
1350 * were the only two timestamp reads that happened).
1351 */
1352 src_reg reset_end = shader_end_time;
1353 reset_end.swizzle = BRW_SWIZZLE_ZZZZ;
1354 vec4_instruction *test = emit(AND(dst_null_d(), reset_end, src_reg(1u)));
1355 test->conditional_mod = BRW_CONDITIONAL_Z;
1356
1357 emit(IF(BRW_PREDICATE_NORMAL));
1358
1359 /* Take the current timestamp and get the delta. */
1360 shader_start_time.negate = true;
1361 dst_reg diff = dst_reg(this, glsl_type::uint_type);
1362 emit(ADD(diff, shader_start_time, shader_end_time));
1363
1364 /* If there were no instructions between the two timestamp gets, the diff
1365 * is 2 cycles. Remove that overhead, so I can forget about that when
1366 * trying to determine the time taken for single instructions.
1367 */
1368 emit(ADD(diff, src_reg(diff), src_reg(-2u)));
1369
1370 emit_shader_time_write(ST_VS, src_reg(diff));
1371 emit_shader_time_write(ST_VS_WRITTEN, src_reg(1u));
1372 emit(BRW_OPCODE_ELSE);
1373 emit_shader_time_write(ST_VS_RESET, src_reg(1u));
1374 emit(BRW_OPCODE_ENDIF);
1375 }
1376
1377 void
1378 vec4_visitor::emit_shader_time_write(enum shader_time_shader_type type,
1379 src_reg value)
1380 {
1381 int shader_time_index =
1382 brw_get_shader_time_index(brw, shader_prog, prog, type);
1383
1384 dst_reg dst =
1385 dst_reg(this, glsl_type::get_array_instance(glsl_type::vec4_type, 2));
1386
1387 dst_reg offset = dst;
1388 dst_reg time = dst;
1389 time.reg_offset++;
1390
1391 offset.type = BRW_REGISTER_TYPE_UD;
1392 emit(MOV(offset, src_reg(shader_time_index * SHADER_TIME_STRIDE)));
1393
1394 time.type = BRW_REGISTER_TYPE_UD;
1395 emit(MOV(time, src_reg(value)));
1396
1397 emit(SHADER_OPCODE_SHADER_TIME_ADD, dst_reg(), src_reg(dst));
1398 }
1399
1400 bool
1401 vec4_visitor::run()
1402 {
1403 sanity_param_count = prog->Parameters->NumParameters;
1404
1405 if (INTEL_DEBUG & DEBUG_SHADER_TIME)
1406 emit_shader_time_begin();
1407
1408 emit_prolog();
1409
1410 /* Generate VS IR for main(). (the visitor only descends into
1411 * functions called "main").
1412 */
1413 if (shader) {
1414 visit_instructions(shader->ir);
1415 } else {
1416 emit_program_code();
1417 }
1418 base_ir = NULL;
1419
1420 if (c->key.base.userclip_active && !c->key.base.uses_clip_distance)
1421 setup_uniform_clipplane_values();
1422
1423 emit_thread_end();
1424
1425 /* Before any optimization, push array accesses out to scratch
1426 * space where we need them to be. This pass may allocate new
1427 * virtual GRFs, so we want to do it early. It also makes sure
1428 * that we have reladdr computations available for CSE, since we'll
1429 * often do repeated subexpressions for those.
1430 */
1431 if (shader) {
1432 move_grf_array_access_to_scratch();
1433 move_uniform_array_access_to_pull_constants();
1434 } else {
1435 /* The ARB_vertex_program frontend emits pull constant loads directly
1436 * rather than using reladdr, so we don't need to walk through all the
1437 * instructions looking for things to move. There isn't anything.
1438 *
1439 * We do still need to split things to vec4 size.
1440 */
1441 split_uniform_registers();
1442 }
1443 pack_uniform_registers();
1444 move_push_constants_to_pull_constants();
1445 split_virtual_grfs();
1446
1447 bool progress;
1448 do {
1449 progress = false;
1450 progress = dead_code_eliminate() || progress;
1451 progress = opt_copy_propagation() || progress;
1452 progress = opt_algebraic() || progress;
1453 progress = opt_register_coalesce() || progress;
1454 } while (progress);
1455
1456
1457 if (failed)
1458 return false;
1459
1460 setup_payload();
1461
1462 if (false) {
1463 /* Debug of register spilling: Go spill everything. */
1464 const int grf_count = virtual_grf_count;
1465 float spill_costs[virtual_grf_count];
1466 bool no_spill[virtual_grf_count];
1467 evaluate_spill_costs(spill_costs, no_spill);
1468 for (int i = 0; i < grf_count; i++) {
1469 if (no_spill[i])
1470 continue;
1471 spill_reg(i);
1472 }
1473 }
1474
1475 while (!reg_allocate()) {
1476 if (failed)
1477 break;
1478 }
1479
1480 opt_set_dependency_control();
1481
1482 /* If any state parameters were appended, then ParameterValues could have
1483 * been realloced, in which case the driver uniform storage set up by
1484 * _mesa_associate_uniform_storage() would point to freed memory. Make
1485 * sure that didn't happen.
1486 */
1487 assert(sanity_param_count == prog->Parameters->NumParameters);
1488
1489 return !failed;
1490 }
1491
1492 } /* namespace brw */
1493
1494 extern "C" {
1495
1496 /**
1497 * Compile a vertex shader.
1498 *
1499 * Returns the final assembly and the program's size.
1500 */
1501 const unsigned *
1502 brw_vs_emit(struct brw_context *brw,
1503 struct gl_shader_program *prog,
1504 struct brw_vs_compile *c,
1505 struct brw_vs_prog_data *prog_data,
1506 void *mem_ctx,
1507 unsigned *final_assembly_size)
1508 {
1509 struct intel_context *intel = &brw->intel;
1510 bool start_busy = false;
1511 float start_time = 0;
1512
1513 if (unlikely(intel->perf_debug)) {
1514 start_busy = (intel->batch.last_bo &&
1515 drm_intel_bo_busy(intel->batch.last_bo));
1516 start_time = get_time();
1517 }
1518
1519 struct brw_shader *shader = NULL;
1520 if (prog)
1521 shader = (brw_shader *) prog->_LinkedShaders[MESA_SHADER_VERTEX];
1522
1523 if (unlikely(INTEL_DEBUG & DEBUG_VS)) {
1524 if (shader) {
1525 printf("GLSL IR for native vertex shader %d:\n", prog->Name);
1526 _mesa_print_ir(shader->ir, NULL);
1527 printf("\n\n");
1528 } else {
1529 printf("ARB_vertex_program %d for native vertex shader\n",
1530 c->vp->program.Base.Id);
1531 _mesa_print_program(&c->vp->program.Base);
1532 }
1533 }
1534
1535 vec4_vs_visitor v(brw, c, prog_data, prog, shader, mem_ctx);
1536 if (!v.run()) {
1537 prog->LinkStatus = false;
1538 ralloc_strcat(&prog->InfoLog, v.fail_msg);
1539 return NULL;
1540 }
1541
1542 vec4_generator g(brw, c, prog, mem_ctx);
1543 const unsigned *generated =g.generate_assembly(&v.instructions,
1544 final_assembly_size);
1545
1546 if (unlikely(intel->perf_debug) && shader) {
1547 if (shader->compiled_once) {
1548 brw_vs_debug_recompile(brw, prog, &c->key);
1549 }
1550 if (start_busy && !drm_intel_bo_busy(intel->batch.last_bo)) {
1551 perf_debug("VS compile took %.03f ms and stalled the GPU\n",
1552 (get_time() - start_time) * 1000);
1553 }
1554 shader->compiled_once = true;
1555 }
1556
1557 return generated;
1558 }
1559
1560 } /* extern "C" */