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