OSDN Git Service

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