Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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