aboutsummaryrefslogtreecommitdiffstats
path: root/src/gallium/drivers/r600/sfn/sfn_emittexinstruction.cpp
blob: 1059db8e4f13528703d1bfb5b47a973828ee6e13 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
/* -*- mesa-c++  -*-
 *
 * Copyright (c) 2018 Collabora LTD
 *
 * Author: Gert Wollny <gert.wollny@collabora.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * on the rights to use, copy, modify, merge, publish, distribute, sub
 * license, and/or sell copies of the Software, and to permit persons to whom
 * the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include "sfn_emittexinstruction.h"
#include "sfn_shader_base.h"
#include "sfn_instruction_fetch.h"

namespace r600 {

EmitTexInstruction::EmitTexInstruction(ShaderFromNirProcessor &processor):
   EmitInstruction (processor)
{
}

bool EmitTexInstruction::do_emit(nir_instr* instr)
{
   nir_tex_instr* ir = nir_instr_as_tex(instr);

   TexInputs src;
   if (!get_inputs(*ir, src))
      return false;

   if (ir->sampler_dim == GLSL_SAMPLER_DIM_CUBE) {
      switch (ir->op) {
      case nir_texop_tex:
         return emit_cube_tex(ir, src);
      case nir_texop_txf:
         return emit_cube_txf(ir, src);
      case nir_texop_txb:
         return emit_cube_txb(ir, src);
      case nir_texop_txl:
         return emit_cube_txl(ir, src);
      case nir_texop_txs:
         return emit_tex_txs(ir, src, {0,1,2,3});
      case nir_texop_txd:
         return emit_cube_txd(ir, src);
      case nir_texop_lod:
         return emit_cube_lod(ir, src);
      case nir_texop_tg4:
         return emit_cube_tg4(ir, src);
      case nir_texop_query_levels:
         return emit_tex_txs(ir, src, {3,7,7,7});
      default:
         return false;
      }
   } else if (ir->sampler_dim == GLSL_SAMPLER_DIM_BUF) {
      switch (ir->op) {
      case nir_texop_txf:
         return emit_buf_txf(ir, src);
      case nir_texop_txs:
         return emit_tex_txs(ir, src, {0,1,2,3});
      default:
         return false;
      }
   } else {
      switch (ir->op) {
      case nir_texop_tex:
         return emit_tex_tex(ir, src);
      case nir_texop_txf:
         return emit_tex_txf(ir, src);
      case nir_texop_txb:
         return emit_tex_txb(ir, src);
      case nir_texop_txl:
         return emit_tex_txl(ir, src);
      case nir_texop_txd:
         return emit_tex_txd(ir, src);
      case nir_texop_txs:
         return emit_tex_txs(ir, src, {0,1,2,3});
      case nir_texop_lod:
         return emit_tex_lod(ir, src);
      case nir_texop_tg4:
         return emit_tex_tg4(ir, src);
      case nir_texop_txf_ms:
         return emit_tex_txf_ms(ir, src);
      case nir_texop_query_levels:
         return emit_tex_txs(ir, src, {3,7,7,7});
      default:
         return false;
      }
   }
}

bool EmitTexInstruction::emit_cube_txf(UNUSED nir_tex_instr* instr, UNUSED TexInputs &src)
{
   return false;
}

bool EmitTexInstruction::emit_cube_txd(nir_tex_instr* instr, TexInputs& tex_src)
{

   assert(instr->src[0].src.is_ssa);

   r600::sfn_log << SfnLog::instr << "emit '"
                 << *reinterpret_cast<nir_instr*>(instr)
                 << "' (" << __func__ << ")\n";

   auto tex_op = TexInstruction::sample_g;

   std::array<PValue, 4> v;
   for (int i = 0; i < 4; ++i)
      v[i] = from_nir(instr->dest, i);

   GPRVector cubed(v);
   emit_cube_prep(tex_src.coord, cubed, instr->is_array);

   std::array<PValue,4> dst_elms;
   std::array<PValue,4> src_elms;

   const uint16_t lookup[4] = {1, 0, 3, 2};
   for (uint16_t i = 0; i < 4; ++i) {
      dst_elms[i] = v[i];
      src_elms[i] = cubed.reg_i(lookup[i]);
   }

   GPRVector empty_dst(0, {7,7,7,7});

   if (instr->is_shadow)  {
      emit_instruction(new AluInstruction(op1_mov, src_elms[3], tex_src.comperator,
                       {alu_last_instr, alu_write}));
      tex_op = TexInstruction::sample_c_g;
   }


   PValue half(new LiteralValue(0.5f));
   for (int i = 0; i < 3; ++i) {
      emit_instruction(new AluInstruction(op2_mul_ieee, tex_src.ddx.reg_i(i), {tex_src.ddx.reg_i(i), half},
      {alu_last_instr, alu_write}));
   }
   for (int i = 0; i < 3; ++i) {
      emit_instruction(new AluInstruction(op2_mul_ieee, tex_src.ddy.reg_i(i), {tex_src.ddy.reg_i(i), half},
      {alu_last_instr, alu_write}));
   }

   auto sampler = get_samplerr_id(instr->sampler_index, tex_src.sampler_deref);
   assert(!sampler.indirect);

   TexInstruction *irgh = new TexInstruction(TexInstruction::set_gradient_h, empty_dst, tex_src.ddx,
                                             sampler.id, sampler.id + R600_MAX_CONST_BUFFERS, tex_src.sampler_offset);
   irgh->set_dest_swizzle({7,7,7,7});

   TexInstruction *irgv = new TexInstruction(TexInstruction::set_gradient_v, empty_dst, tex_src.ddy,
                           sampler.id, sampler.id + R600_MAX_CONST_BUFFERS, tex_src.sampler_offset);
   irgv->set_dest_swizzle({7,7,7,7});

   GPRVector dst(dst_elms);
   GPRVector src(src_elms);
   TexInstruction *ir = new TexInstruction(tex_op, dst, src, instr->sampler_index,
                                 sampler.id + R600_MAX_CONST_BUFFERS, tex_src.sampler_offset);

   set_rect_coordinate_flags(instr, ir);
   //set_offsets(ir, tex_src.offset);

   emit_instruction(irgh);
   emit_instruction(irgv);
   emit_instruction(ir);
   return true;
}


bool EmitTexInstruction::emit_cube_txl(nir_tex_instr* instr, TexInputs& tex_src)
{
   assert(instr->src[0].src.is_ssa);

   if (instr->is_shadow)
      return false;

   r600::sfn_log << SfnLog::instr << "emit '"
                 << *reinterpret_cast<nir_instr*>(instr)
                 << "' (" << __func__ << ")\n";

   std::array<PValue, 4> v;
   for (int i = 0; i < 4; ++i)
      v[i] = from_nir(instr->dest, i);

   GPRVector cubed(v);
   emit_cube_prep(tex_src.coord, cubed, instr->is_array);

   std::array<PValue,4> dst_elms;
   std::array<PValue,4> src_elms;

   const uint16_t lookup[4] = {1, 0, 3, 2};
   for (uint16_t i = 0; i < 4; ++i) {
      dst_elms[i] = v[i];
      src_elms[i] = cubed.reg_i(lookup[i]);
   }

   auto *ir = new AluInstruction(op1_mov, src_elms[3], tex_src.lod,
                                 {alu_last_instr, alu_write});
   emit_instruction(ir);

   GPRVector src(src_elms);
   GPRVector dst(dst_elms);

   auto sampler = get_samplerr_id(instr->sampler_index, tex_src.sampler_deref);
   assert(!sampler.indirect);

   auto tir = new TexInstruction(TexInstruction::sample_l, dst, src,
                                 sampler.id,sampler.id + R600_MAX_CONST_BUFFERS,
                                 tex_src.sampler_offset);

   if (instr->is_array)
      tir->set_flag(TexInstruction::z_unnormalized);

   emit_instruction(tir);
   return true;
}

bool EmitTexInstruction::emit_cube_lod(nir_tex_instr* instr, TexInputs& src)
{
   auto tex_op = TexInstruction::get_tex_lod;

   std::array<PValue, 4> v;
   for (int i = 0; i < 4; ++i)
      v[i] = from_nir(instr->dest, i);

   GPRVector cubed(v);
   emit_cube_prep(src.coord, cubed, instr->is_array);

   auto sampler = get_samplerr_id(instr->sampler_index, src.sampler_deref);
   assert(!sampler.indirect);

   auto dst = make_dest(*instr);
   auto irt = new TexInstruction(tex_op, dst, cubed, sampler.id,
                                 sampler.id + R600_MAX_CONST_BUFFERS,
                                 src.sampler_offset);

   emit_instruction(irt);
   return true;

}


bool EmitTexInstruction::emit_cube_txb(nir_tex_instr* instr, TexInputs& tex_src)
{
   assert(instr->src[0].src.is_ssa);

   r600::sfn_log << SfnLog::instr << "emit '"
                 << *reinterpret_cast<nir_instr*>(instr)
                 << "' (" << __func__ << ")\n";

   std::array<PValue, 4> v;
   for (int i = 0; i < 4; ++i)
      v[i] = from_nir(instr->dest, i);

   GPRVector cubed(v);
   emit_cube_prep(tex_src.coord, cubed, instr->is_array);

   std::array<PValue,4> dst_elms;
   std::array<PValue,4> src_elms;

   const uint16_t lookup[4] = {1, 0, 3, 2};
   for (uint16_t i = 0; i < 4; ++i) {
      dst_elms[i] = v[i];
      src_elms[i] = v[lookup[i]];
   }

   GPRVector src(src_elms);
   GPRVector dst(dst_elms);

   auto tex_op = TexInstruction::sample_lb;
   if (!instr->is_shadow)  {
      emit_instruction(new AluInstruction(op1_mov, src_elms[3], tex_src.bias,
                                          {alu_last_instr, alu_write}));
   } else {
      emit_instruction(new AluInstruction(op1_mov, src_elms[3], tex_src.comperator,
                       {alu_last_instr, alu_write}));
      tex_op = TexInstruction::sample_c_lb;
   }

   auto sampler = get_samplerr_id(instr->sampler_index, tex_src.sampler_deref);
   assert(!sampler.indirect && "Indirect sampler selection not yet supported");

   auto tir = new TexInstruction(tex_op, dst, src,
                                 sampler.id,
                                 sampler.id + R600_MAX_CONST_BUFFERS, tex_src.sampler_offset);
   emit_instruction(tir);
   return true;

}

bool EmitTexInstruction::emit_cube_tex(nir_tex_instr* instr, TexInputs& tex_src)
{
   std::array<PValue, 4> v;
   for (int i = 0; i < 4; ++i)
      v[i] = from_nir(instr->dest, i);

   auto tex_op = TexInstruction::sample;
   GPRVector cubed(v);
   emit_cube_prep(tex_src.coord, cubed, instr->is_array);

   std::array<PValue,4> dst_elms;
   std::array<PValue,4> src_elms;

   const uint16_t lookup[4] = {1, 0, 3, 2};
   for (uint16_t i = 0; i < 4; ++i) {
      dst_elms[i] = v[i];
      src_elms[i] = v[lookup[i]];
   }

   if (instr->is_shadow)  {
      emit_instruction(new AluInstruction(op1_mov, src_elms[3], tex_src.comperator,
                       {alu_last_instr, alu_write}));
      tex_op = TexInstruction::sample_c;
   }

   GPRVector dst(dst_elms);
   GPRVector src(src_elms);

   auto sampler = get_samplerr_id(instr->sampler_index, tex_src.sampler_deref);
   assert(!sampler.indirect && "Indirect sampler selection not yet supported");

   auto tir = new TexInstruction(tex_op, dst, src,
                                 sampler.id,
                                 sampler.id + R600_MAX_CONST_BUFFERS, tex_src.sampler_offset);
   if (instr->is_array)
      tir->set_flag(TexInstruction::z_unnormalized);

   emit_instruction(tir);
   return true;

}

bool EmitTexInstruction::emit_cube_prep(const GPRVector& coord, GPRVector& cubed, bool is_array)
{
   AluInstruction *ir = nullptr;
   const uint16_t src0_chan[4] = {2, 2, 0, 1};
   const uint16_t src1_chan[4] = {1, 0, 2, 2};

   for (int i = 0; i < 4; ++i)  {
      ir = new AluInstruction(op2_cube, cubed.reg_i(i), coord.reg_i(src0_chan[i]),
                              coord.reg_i(src1_chan[i]), {alu_write});

      emit_instruction(ir);
   }
   ir->set_flag(alu_last_instr);

   ir = new AluInstruction(op1_recip_ieee, cubed.reg_i(2), cubed.reg_i(2), {alu_write, alu_last_instr});
   ir->set_flag(alu_src0_abs);
   emit_instruction(ir);

   PValue one_p_5(new LiteralValue(1.5f));
   for (int i = 0; i < 2; ++i)  {
      ir = new AluInstruction(op3_muladd, cubed.reg_i(i), cubed.reg_i(i), cubed.reg_i(2),
                              one_p_5, {alu_write});
      emit_instruction(ir);
   }
   ir->set_flag(alu_last_instr);

   if (is_array) {
      auto face = cubed.reg_i(3);
      PValue array_index = get_temp_register();

      ir = new AluInstruction(op1_rndne, array_index, coord.reg_i(3), {alu_write, alu_last_instr});
      emit_instruction(ir);

      ir = new AluInstruction(op2_max, array_index, {array_index, Value::zero}, {alu_write, alu_last_instr});
      emit_instruction(ir);

      ir = new AluInstruction(op3_muladd, face, {array_index, PValue (new LiteralValue(8.0f)), face},
                              {alu_write, alu_last_instr});
      emit_instruction(ir);
   }

   return true;
}

bool EmitTexInstruction::emit_buf_txf(nir_tex_instr* instr, TexInputs &src)
{
   auto dst = make_dest(*instr);

   auto ir = new FetchInstruction(vc_fetch, no_index_offset, dst, src.coord.reg_i(0), 0,
                                  instr->texture_index +  R600_MAX_CONST_BUFFERS,
                                  PValue(), bim_none);
   ir->set_flag(vtx_use_const_field);
   emit_instruction(ir);
   return true;
}

bool EmitTexInstruction::emit_tex_tex(nir_tex_instr* instr, TexInputs& src)
{

   r600::sfn_log << SfnLog::instr << "emit '"
                 << *reinterpret_cast<nir_instr*>(instr)
                 << "' (" << __func__ << ")\n";

   auto tex_op = TexInstruction::sample;

   auto sampler = get_samplerr_id(instr->sampler_index, src.sampler_deref);
   assert(!sampler.indirect);

   if (instr->is_shadow)  {
      emit_instruction(new AluInstruction(op1_mov, src.coord.reg_i(3), src.comperator,
                       {alu_last_instr, alu_write}));
      tex_op = TexInstruction::sample_c;
   }

   auto dst = make_dest(*instr);
   auto irt = new TexInstruction(tex_op, dst, src.coord, sampler.id,
                                 sampler.id + R600_MAX_CONST_BUFFERS, src.sampler_offset);
   if (instr->is_array)
      handle_array_index(*instr, src.coord, irt);

   set_rect_coordinate_flags(instr, irt);
   set_offsets(irt, src.offset);

   emit_instruction(irt);
   return true;
}

bool EmitTexInstruction::emit_tex_txd(nir_tex_instr* instr, TexInputs& src)
{
   r600::sfn_log << SfnLog::instr << "emit '"
                 << *reinterpret_cast<nir_instr*>(instr)
                 << "' (" << __func__ << ")\n";

   auto tex_op = TexInstruction::sample_g;
   auto dst = make_dest(*instr);

   GPRVector empty_dst(0,{7,7,7,7});

   if (instr->is_shadow)  {
      emit_instruction(new AluInstruction(op1_mov, src.coord.reg_i(3), src.comperator,
                       {alu_last_instr, alu_write}));
      tex_op = TexInstruction::sample_c_g;
   }

   auto sampler = get_samplerr_id(instr->sampler_index, src.sampler_deref);
   assert(!sampler.indirect && "Indirect sampler selection not yet supported");

   TexInstruction *irgh = new TexInstruction(TexInstruction::set_gradient_h, empty_dst, src.ddx,
                                             sampler.id,
                                             sampler.id + R600_MAX_CONST_BUFFERS, src.sampler_offset);
   irgh->set_dest_swizzle({7,7,7,7});

   TexInstruction *irgv = new TexInstruction(TexInstruction::set_gradient_v, empty_dst, src.ddy,
                           sampler.id, sampler.id + R600_MAX_CONST_BUFFERS, src.sampler_offset);
   irgv->set_dest_swizzle({7,7,7,7});

   TexInstruction *ir = new TexInstruction(tex_op, dst, src.coord, sampler.id,
                                           sampler.id + R600_MAX_CONST_BUFFERS, src.sampler_offset);
   if (instr->is_array)
      handle_array_index(*instr, src.coord, ir);

   set_rect_coordinate_flags(instr, ir);
   set_offsets(ir, src.offset);

   emit_instruction(irgh);
   emit_instruction(irgv);
   emit_instruction(ir);
   return true;
}

bool EmitTexInstruction::emit_tex_txf(nir_tex_instr* instr, TexInputs& src)
{
   r600::sfn_log << SfnLog::instr << "emit '"
                 << *reinterpret_cast<nir_instr*>(instr)
                 << "' (" << __func__ << ")\n";

   auto dst = make_dest(*instr);

   if (*src.coord.reg_i(3) != *src.lod)
      emit_instruction(new AluInstruction(op1_mov, src.coord.reg_i(3), src.lod, {alu_write, alu_last_instr}));

   auto sampler = get_samplerr_id(instr->sampler_index, src.sampler_deref);
   assert(!sampler.indirect);

   /* txf doesn't need rounding for the array index, but 1D has the array index
    * in the z component */
   if (instr->is_array && instr->sampler_dim == GLSL_SAMPLER_DIM_1D)
      src.coord.set_reg_i(2, src.coord.reg_i(1));

   auto tex_ir = new TexInstruction(TexInstruction::ld, dst, src.coord,
                                    sampler.id,
                                    sampler.id + R600_MAX_CONST_BUFFERS, src.sampler_offset);


   if (src.offset) {
      assert(src.offset->is_ssa);
      AluInstruction *ir = nullptr;
      for (unsigned i = 0; i < src.offset->ssa->num_components; ++i) {
         ir = new AluInstruction(op2_add_int, src.coord.reg_i(i),
                  {src.coord.reg_i(i), from_nir(*src.offset, i, i)}, {alu_write});
         emit_instruction(ir);
      }
      if (ir)
         ir->set_flag(alu_last_instr);
   }

   emit_instruction(tex_ir);
   return true;
}

bool EmitTexInstruction::emit_tex_lod(nir_tex_instr* instr, TexInputs& src)
{
   auto tex_op = TexInstruction::get_tex_lod;

   auto sampler = get_samplerr_id(instr->sampler_index, src.sampler_deref);
   assert(!sampler.indirect && "Indirect sampler selection not yet supported");

   auto dst = make_dest(*instr);
   auto irt = new TexInstruction(tex_op, dst, src.coord, sampler.id,
                                 sampler.id + R600_MAX_CONST_BUFFERS, src.sampler_offset);
   irt->set_dest_swizzle({1,0,7,7});
   emit_instruction(irt);
   return true;

}

bool EmitTexInstruction::emit_tex_txl(nir_tex_instr* instr, TexInputs& src)
{
   r600::sfn_log << SfnLog::instr << "emit '"
                 << *reinterpret_cast<nir_instr*>(instr)
                 << "' (" << __func__ << ")\n";

   auto tex_op = TexInstruction::sample_l;
   emit_instruction(new AluInstruction(op1_mov, src.coord.reg_i(3), src.lod,
                                       {alu_last_instr, alu_write}));

   if (instr->is_shadow)  {
      emit_instruction(new AluInstruction(op1_mov, src.coord.reg_i(2), src.comperator,
                       {alu_last_instr, alu_write}));
      tex_op = TexInstruction::sample_c_l;
   }

   auto sampler = get_samplerr_id(instr->sampler_index, src.sampler_deref);
   assert(!sampler.indirect && "Indirect sampler selection not yet supported");

   auto dst = make_dest(*instr);
   auto irt = new TexInstruction(tex_op, dst, src.coord, sampler.id,
                                 sampler.id + R600_MAX_CONST_BUFFERS, src.sampler_offset);

   if (instr->is_array)
      handle_array_index(*instr, src.coord, irt);

   set_rect_coordinate_flags(instr, irt);
   set_offsets(irt, src.offset);

   emit_instruction(irt);
   return true;
}

bool EmitTexInstruction::emit_tex_txb(nir_tex_instr* instr, TexInputs& src)
{
   auto tex_op = TexInstruction::sample_lb;

   std::array<uint8_t, 4> in_swizzle = {0,1,2,3};

   emit_instruction(new AluInstruction(op1_mov, src.coord.reg_i(3), src.bias,
                                       {alu_last_instr, alu_write}));

   if (instr->is_shadow) {
      emit_instruction(new AluInstruction(op1_mov, src.coord.reg_i(2), src.comperator,
                                          {alu_last_instr, alu_write}));
      tex_op = TexInstruction::sample_c_lb;
   }

   GPRVector tex_src(src.coord, in_swizzle);

   auto sampler = get_samplerr_id(instr->sampler_index, src.sampler_deref);
   assert(!sampler.indirect && "Indirect sampler selection not yet supported");

   auto dst = make_dest(*instr);
   auto irt = new TexInstruction(tex_op, dst, tex_src, sampler.id,
                                 sampler.id + R600_MAX_CONST_BUFFERS, src.sampler_offset);
   if (instr->is_array)
      handle_array_index(*instr, tex_src, irt);

   set_rect_coordinate_flags(instr, irt);
   set_offsets(irt, src.offset);

   emit_instruction(irt);
   return true;
}

bool EmitTexInstruction::emit_tex_txs(nir_tex_instr* instr, TexInputs& tex_src,
                                      const std::array<int,4>& dest_swz)
{
   std::array<PValue,4> dst_elms;
   std::array<PValue,4> src_elms;

   for (uint16_t i = 0; i < 4; ++i) {
      dst_elms[i] = from_nir(instr->dest, (i < instr->dest.ssa.num_components) ? i : 7);
   }

   GPRVector dst(dst_elms);

   if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF) {
      emit_instruction(new FetchInstruction(dst, PValue(new GPRValue(0, 7)),
                       instr->sampler_index + R600_MAX_CONST_BUFFERS,
                       bim_none));
   } else {
      for (uint16_t i = 0; i < 4; ++i)
         src_elms[i] =  tex_src.lod;
      GPRVector src(src_elms);

      auto sampler = get_samplerr_id(instr->sampler_index, tex_src.sampler_deref);
      assert(!sampler.indirect && "Indirect sampler selection not yet supported");

      auto ir = new TexInstruction(TexInstruction::get_resinfo, dst, src,
                                   sampler.id,
                                   sampler.id + R600_MAX_CONST_BUFFERS, tex_src.sampler_offset);
      ir->set_dest_swizzle(dest_swz);
      emit_instruction(ir);
   }

   return true;

}

bool EmitTexInstruction::emit_tex_tg4(nir_tex_instr* instr, TexInputs& src)
{
   r600::sfn_log << SfnLog::instr << "emit '"
                 << *reinterpret_cast<nir_instr*>(instr)
                 << "' (" << __func__ << ")\n";

   auto tex_op = TexInstruction::gather4;

   if (instr->is_shadow)  {
      emit_instruction(new AluInstruction(op1_mov, src.coord.reg_i(3), src.comperator,
                       {alu_last_instr, alu_write}));
      tex_op = TexInstruction::gather4_c;
   }

   auto sampler = get_samplerr_id(instr->sampler_index, src.sampler_deref);
   assert(!sampler.indirect && "Indirect sampler selection not yet supported");

   bool literal_offset = false;
   if (src.offset) {
      literal_offset =  src.offset->is_ssa && get_literal_register(*src.offset);
      r600::sfn_log << SfnLog::tex << " really have offsets and they are " <<
                       (literal_offset ? "literal" : "varying") <<
                       "\n";

      if (!literal_offset) {
         GPRVector::Swizzle swizzle = {4,4,4,4};
         for (unsigned i = 0; i < instr->coord_components; ++i)
            swizzle[i] = i;

         int noffsets = instr->coord_components;
         if (instr->is_array)
            --noffsets;

         auto ofs = vec_from_nir_with_fetch_constant(*src.offset,
                                                     ( 1 << noffsets) - 1,
                                                     swizzle);
         GPRVector dummy(0, {7,7,7,7});
         tex_op = (tex_op == TexInstruction::gather4_c) ?
                     TexInstruction::gather4_c_o : TexInstruction::gather4_o;

         auto set_ofs = new TexInstruction(TexInstruction::set_offsets, dummy,
                                           ofs, sampler.id,
                                           sampler.id + R600_MAX_CONST_BUFFERS, src.sampler_offset);
         set_ofs->set_dest_swizzle({7,7,7,7});
         emit_instruction(set_ofs);
      }
   }


   /* pre CAYMAN needs swizzle */
   auto dst = make_dest(*instr);
   auto irt = new TexInstruction(tex_op, dst, src.coord, sampler.id,
                                 sampler.id + R600_MAX_CONST_BUFFERS, src.sampler_offset);

   irt->set_dest_swizzle({1,2,0,3});
   irt->set_gather_comp(instr->component);

   if (instr->is_array)
      handle_array_index(*instr, src.coord, irt);

   if (literal_offset) {
      r600::sfn_log << SfnLog::tex << "emit literal offsets\n";
      set_offsets(irt, src.offset);
   }

   set_rect_coordinate_flags(instr, irt);

   emit_instruction(irt);
   return true;
}

bool EmitTexInstruction::emit_cube_tg4(nir_tex_instr* instr, TexInputs& tex_src)
{
   std::array<PValue, 4> v;
   for (int i = 0; i < 4; ++i)
      v[i] = from_nir(instr->dest, i);

   auto tex_op = TexInstruction::gather4;
   GPRVector cubed(v);
   emit_cube_prep(tex_src.coord, cubed, instr->is_array);

   std::array<PValue,4> dst_elms;
   std::array<PValue,4> src_elms;

   const uint16_t lookup[4] = {1, 0, 3, 2};
   for (uint16_t i = 0; i < 4; ++i) {
      dst_elms[i] = v[i];
      src_elms[i] = v[lookup[i]];
   }

   if (instr->is_shadow)  {
      emit_instruction(new AluInstruction(op1_mov, src_elms[3], tex_src.comperator,
                       {alu_last_instr, alu_write}));
      tex_op = TexInstruction::gather4_c;
   }

   GPRVector dst(dst_elms);
   GPRVector src(src_elms);

   auto sampler = get_samplerr_id(instr->sampler_index, tex_src.sampler_deref);
   assert(!sampler.indirect && "Indirect sampler selection not yet supported");

   auto tir = new TexInstruction(tex_op, dst, src, sampler.id,
                                 sampler.id + R600_MAX_CONST_BUFFERS, tex_src.sampler_offset);

   tir->set_gather_comp(instr->component);

   tir->set_dest_swizzle({1, 2, 0, 3});

   if (instr->is_array)
      tir->set_flag(TexInstruction::z_unnormalized);

   emit_instruction(tir);
   return true;
}

bool EmitTexInstruction::emit_tex_txf_ms(nir_tex_instr* instr, TexInputs& src)
{
   assert(instr->src[0].src.is_ssa);

   r600::sfn_log << SfnLog::instr << "emit '"
                 << *reinterpret_cast<nir_instr*>(instr)
                 << "' (" << __func__ << ")\n";

   auto sampler = get_samplerr_id(instr->sampler_index, src.sampler_deref);
   assert(!sampler.indirect && "Indirect sampler selection not yet supported");

   int sample_id = allocate_temp_register();

   GPRVector sample_id_dest(sample_id, {0,7,7,7});
   PValue help(new GPRValue(sample_id, 1));

   /* FIXME: Texture destination registers must be handled differently,
    * because the swizzle identfies which source componnet has to be written
    * at a certain position, and the target register is actually different.
    * At this point we just add a helper register, but for later work (scheduling
    * and optimization on the r600 IR level, this needs to be implemented
    * differently */


   emit_instruction(new AluInstruction(op1_mov, src.coord.reg_i(3),
                                       src.ms_index,
                                       {alu_write, alu_last_instr}));

   auto tex_sample_id_ir = new TexInstruction(TexInstruction::ld, sample_id_dest, src.coord,
                                              sampler.id,
                                              sampler.id + R600_MAX_CONST_BUFFERS, src.sampler_offset);
   tex_sample_id_ir->set_flag(TexInstruction::x_unnormalized);
   tex_sample_id_ir->set_flag(TexInstruction::y_unnormalized);
   tex_sample_id_ir->set_flag(TexInstruction::z_unnormalized);
   tex_sample_id_ir->set_flag(TexInstruction::w_unnormalized);
   tex_sample_id_ir->set_inst_mode(1);

   emit_instruction(tex_sample_id_ir);

   emit_instruction(new AluInstruction(op2_mullo_int, help,
                                       {src.ms_index, PValue(new LiteralValue(4))},
                                       {alu_write, alu_last_instr}));

   emit_instruction(new AluInstruction(op2_lshr_int, src.coord.reg_i(3),
                                       {sample_id_dest.reg_i(0), help},
                                       {alu_write, alu_last_instr}));

   emit_instruction(new AluInstruction(op2_and_int, src.coord.reg_i(3),
                                       {src.coord.reg_i(3), PValue(new LiteralValue(15))},
                                       {alu_write, alu_last_instr}));

   auto dst = make_dest(*instr);

   /* txf doesn't need rounding for the array index, but 1D has the array index
    * in the z component */
   if (instr->is_array && instr->sampler_dim == GLSL_SAMPLER_DIM_1D)
      src.coord.set_reg_i(2, src.coord.reg_i(1));

   auto tex_ir = new TexInstruction(TexInstruction::ld, dst, src.coord,
                                    sampler.id,
                                    sampler.id + R600_MAX_CONST_BUFFERS, src.sampler_offset);


   if (src.offset) {
      assert(src.offset->is_ssa);
      AluInstruction *ir = nullptr;
      for (unsigned i = 0; i < src.offset->ssa->num_components; ++i) {
         ir = new AluInstruction(op2_add_int, src.coord.reg_i(i),
                  {src.coord.reg_i(i), from_nir(*src.offset, i, i)}, {alu_write});
         emit_instruction(ir);
      }
      if (ir)
         ir->set_flag(alu_last_instr);
   }

   emit_instruction(tex_ir);
   return true;
}

bool EmitTexInstruction::get_inputs(const nir_tex_instr& instr, TexInputs &src)
{
   sfn_log << SfnLog::tex << "Get Inputs with " << instr.coord_components << " components\n";

   unsigned grad_components = instr.coord_components;
   if (instr.is_array)
      --grad_components;


   src.offset = nullptr;
   bool retval = true;
   for (unsigned i = 0; i < instr.num_srcs; ++i) {
      switch (instr.src[i].src_type) {
      case nir_tex_src_bias:
         src.bias = from_nir(instr.src[i], 0);
         break;

      case nir_tex_src_coord: {
         src.coord = vec_from_nir_with_fetch_constant(instr.src[i].src,
                                                      (1 << instr.coord_components) - 1,
         {0,1,2,3});
      } break;
      case nir_tex_src_comparator:
         src.comperator = from_nir(instr.src[i], 0);
         break;
      case nir_tex_src_ddx: {
         sfn_log << SfnLog::tex << "Get DDX ";
         src.ddx = vec_from_nir_with_fetch_constant(instr.src[i].src,
                                                    (1 << grad_components) - 1,
                                                    swizzle_from_comps(grad_components));
         sfn_log << SfnLog::tex << src.ddx << "\n";
      } break;
      case nir_tex_src_ddy:{
         sfn_log << SfnLog::tex << "Get DDY ";
         src.ddy = vec_from_nir_with_fetch_constant(instr.src[i].src,
                                                    (1 << grad_components) - 1,
                                                    swizzle_from_comps(grad_components));
         sfn_log << SfnLog::tex << src.ddy << "\n";
      }  break;
      case nir_tex_src_lod:
         src.lod = from_nir_with_fetch_constant(instr.src[i].src, 0);
         break;
      case nir_tex_src_offset:
         sfn_log << SfnLog::tex << "  -- Find offset\n";
         src.offset = &instr.src[i].src;
         break;
      case nir_tex_src_sampler_deref:
         src.sampler_deref = get_deref_location(instr.src[i].src);
         break;
      case nir_tex_src_texture_deref:
         src.texture_deref = get_deref_location(instr.src[i].src);
         break;
      case nir_tex_src_ms_index:
         src.ms_index = from_nir(instr.src[i], 0);
         break;
      case nir_tex_src_texture_offset:
         src.texture_offset = from_nir(instr.src[i], 0);
         break;
      case nir_tex_src_sampler_offset:
         src.sampler_offset = from_nir(instr.src[i], 0);
         break;
      case nir_tex_src_plane:
      case nir_tex_src_projector:
      case nir_tex_src_min_lod:
      case nir_tex_src_ms_mcs:
      default:
         sfn_log << SfnLog::tex << "Texture source type " <<  instr.src[i].src_type << " not supported\n";
         retval = false;
      }
   }
   return retval;
}

GPRVector EmitTexInstruction::make_dest(nir_tex_instr& instr)
{
   int num_dest_components = instr.dest.is_ssa ? instr.dest.ssa.num_components :
                                                 instr.dest.reg.reg->num_components;
   std::array<PValue,4> dst_elms;
   for (uint16_t i = 0; i < 4; ++i)
      dst_elms[i] = from_nir(instr.dest, (i < num_dest_components) ? i : 7);
   return GPRVector(dst_elms);
}


GPRVector EmitTexInstruction::make_dest(nir_tex_instr& instr,
                                        const std::array<int, 4>& swizzle)
{
   int num_dest_components = instr.dest.is_ssa ? instr.dest.ssa.num_components :
                                                 instr.dest.reg.reg->num_components;
   std::array<PValue,4> dst_elms;
   for (uint16_t i = 0; i < 4; ++i) {
      int k = swizzle[i];
      dst_elms[i] = from_nir(instr.dest, (k < num_dest_components) ? k : 7);
   }
   return GPRVector(dst_elms);
}

void EmitTexInstruction::set_rect_coordinate_flags(nir_tex_instr* instr,
                                                   TexInstruction* ir) const
{
   if (instr->sampler_dim == GLSL_SAMPLER_DIM_RECT) {
      ir->set_flag(TexInstruction::x_unnormalized);
      ir->set_flag(TexInstruction::y_unnormalized);
   }
}

void EmitTexInstruction::set_offsets(TexInstruction* ir, nir_src *offset)
{
   if (!offset)
      return;

   assert(offset->is_ssa);
   auto literal = get_literal_register(*offset);
   assert(literal);

   for (int i = 0; i < offset->ssa->num_components; ++i) {
      ir->set_offset(i, literal->value[i].i32);
   }
}

void EmitTexInstruction::handle_array_index(const nir_tex_instr& instr, const GPRVector& src, TexInstruction *ir)
{
   int src_idx = instr.sampler_dim == GLSL_SAMPLER_DIM_1D ? 1 : 2;
   emit_instruction(new AluInstruction(op1_rndne, src.reg_i(2), src.reg_i(src_idx),
                                       {alu_last_instr, alu_write}));
   ir->set_flag(TexInstruction::z_unnormalized);
}

EmitTexInstruction::SamplerId
EmitTexInstruction::get_samplerr_id(int sampler_id, const nir_variable *deref)
{
   EmitTexInstruction::SamplerId result = {sampler_id, false};

   if (deref) {
      assert(glsl_type_is_sampler(deref->type));
      result.id = deref->data.binding;
   }
   return result;
}

EmitTexInstruction::TexInputs::TexInputs():
   sampler_deref(nullptr),
   texture_deref(nullptr),
   offset(nullptr)
{
}

}