summaryrefslogtreecommitdiff
path: root/src/mesa/pipe/tgsi/mesa/mesa_to_tgsi.c
blob: 182a7eff055b79ddf7e6a26c62f4b264dcbdfabf (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
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
#include "tgsi_platform.h"
#include "tgsi_mesa.h"
#include "pipe/tgsi/exec/tgsi_attribs.h"
#include "pipe/tgsi/mesa/mesa_to_tgsi.h"

#define TGSI_DEBUG 1


/**
 * Convert a VERT_ATTRIB_x to a TGSI_ATTRIB_y
 */
uint
tgsi_mesa_translate_vertex_input(GLuint attrib)
{
   /* XXX these could be implemented with array lookups too.... */
   switch (attrib) {
   case VERT_ATTRIB_POS:
      return TGSI_ATTRIB_POS;
   case VERT_ATTRIB_WEIGHT:
      return TGSI_ATTRIB_WEIGHT;
   case VERT_ATTRIB_NORMAL:
      return TGSI_ATTRIB_NORMAL;
   case VERT_ATTRIB_COLOR0:
      return TGSI_ATTRIB_COLOR0;
   case VERT_ATTRIB_COLOR1:
      return TGSI_ATTRIB_COLOR1;
   case VERT_ATTRIB_FOG:
      return TGSI_ATTRIB_FOG;
   case VERT_ATTRIB_COLOR_INDEX:
      return TGSI_ATTRIB_COLOR_INDEX;
   case VERT_ATTRIB_EDGEFLAG:
      return TGSI_ATTRIB_EDGEFLAG;
   case VERT_ATTRIB_TEX0:
      return TGSI_ATTRIB_TEX0;
   case VERT_ATTRIB_TEX1:
      return TGSI_ATTRIB_TEX1;
   case VERT_ATTRIB_TEX2:
      return TGSI_ATTRIB_TEX2;
   case VERT_ATTRIB_TEX3:
      return TGSI_ATTRIB_TEX3;
   case VERT_ATTRIB_TEX4:
      return TGSI_ATTRIB_TEX4;
   case VERT_ATTRIB_TEX5:
      return TGSI_ATTRIB_TEX5;
   case VERT_ATTRIB_TEX6:
      return TGSI_ATTRIB_TEX6;
   case VERT_ATTRIB_TEX7:
      return TGSI_ATTRIB_TEX7;
   case VERT_ATTRIB_GENERIC0:
      return TGSI_ATTRIB_VAR0;
   case VERT_ATTRIB_GENERIC1:
      return TGSI_ATTRIB_VAR1;
   case VERT_ATTRIB_GENERIC2:
      return TGSI_ATTRIB_VAR2;
   case VERT_ATTRIB_GENERIC3:
      return TGSI_ATTRIB_VAR3;
   case VERT_ATTRIB_GENERIC4:
      return TGSI_ATTRIB_VAR4;
   case VERT_ATTRIB_GENERIC5:
      return TGSI_ATTRIB_VAR5;
   case VERT_ATTRIB_GENERIC6:
      return TGSI_ATTRIB_VAR6;
   case VERT_ATTRIB_GENERIC7:
      return TGSI_ATTRIB_VAR7;
   default:
      assert(0);
      return 0;
   }
}


/**
 * Convert VERT_RESULT_x to TGSI_ATTRIB_y
 */
uint
tgsi_mesa_translate_vertex_output(GLuint attrib)
{
   switch (attrib) {
   case VERT_RESULT_HPOS:
      return TGSI_ATTRIB_POS;
   case VERT_RESULT_COL0:
      return TGSI_ATTRIB_COLOR0;
   case VERT_RESULT_COL1:
      return TGSI_ATTRIB_COLOR1;
   case VERT_RESULT_FOGC:
      return TGSI_ATTRIB_FOG;
   case VERT_RESULT_TEX0:
      return TGSI_ATTRIB_TEX0;
   case VERT_RESULT_TEX1:
      return TGSI_ATTRIB_TEX1;
   case VERT_RESULT_TEX2:
      return TGSI_ATTRIB_TEX2;
   case VERT_RESULT_TEX3:
      return TGSI_ATTRIB_TEX3;
   case VERT_RESULT_TEX4:
      return TGSI_ATTRIB_TEX4;
   case VERT_RESULT_TEX5:
      return TGSI_ATTRIB_TEX5;
   case VERT_RESULT_TEX6:
      return TGSI_ATTRIB_TEX6;
   case VERT_RESULT_TEX7:
      return TGSI_ATTRIB_TEX7;
   case VERT_RESULT_PSIZ:
      return TGSI_ATTRIB_POINTSIZE;
   case VERT_RESULT_BFC0:
      return TGSI_ATTRIB_BFC0;
   case VERT_RESULT_BFC1:
      return TGSI_ATTRIB_BFC1;
   case VERT_RESULT_VAR0:
      return TGSI_ATTRIB_VAR0;
   case VERT_RESULT_VAR0 + 1:
      return TGSI_ATTRIB_VAR1;
   case VERT_RESULT_VAR0 + 2:
      return TGSI_ATTRIB_VAR2;
   case VERT_RESULT_VAR0 + 3:
      return TGSI_ATTRIB_VAR3;
   case VERT_RESULT_VAR0 + 4:
      return TGSI_ATTRIB_VAR4;
   case VERT_RESULT_VAR0 + 5:
      return TGSI_ATTRIB_VAR5;
   case VERT_RESULT_VAR0 + 6:
      return TGSI_ATTRIB_VAR6;
   case VERT_RESULT_VAR0 + 7:
      return TGSI_ATTRIB_VAR7;
   default:
      assert(0);
      return 0;
   }
}


/**
 * Convert a FRAG_ATTRIB_x to a TGSI_ATTRIB_y
 */
uint
tgsi_mesa_translate_fragment_input(GLuint attrib)
{
   switch (attrib) {
   case FRAG_ATTRIB_WPOS:
      return TGSI_ATTRIB_POS;
   case FRAG_ATTRIB_COL0:
      return TGSI_ATTRIB_COLOR0;
   case FRAG_ATTRIB_COL1:
      return TGSI_ATTRIB_COLOR1;
   case FRAG_ATTRIB_FOGC:
      return TGSI_ATTRIB_FOG;
   case FRAG_ATTRIB_TEX0:
      return TGSI_ATTRIB_TEX0;
   case FRAG_ATTRIB_TEX1:
      return TGSI_ATTRIB_TEX1;
   case FRAG_ATTRIB_TEX2:
      return TGSI_ATTRIB_TEX2;
   case FRAG_ATTRIB_TEX3:
      return TGSI_ATTRIB_TEX3;
   case FRAG_ATTRIB_TEX4:
      return TGSI_ATTRIB_TEX4;
   case FRAG_ATTRIB_TEX5:
      return TGSI_ATTRIB_TEX5;
   case FRAG_ATTRIB_TEX6:
      return TGSI_ATTRIB_TEX6;
   case FRAG_ATTRIB_TEX7:
      return TGSI_ATTRIB_TEX7;
   case FRAG_ATTRIB_VAR0:
      return TGSI_ATTRIB_VAR0;
   case FRAG_ATTRIB_VAR0 + 1:
      return TGSI_ATTRIB_VAR1;
   case FRAG_ATTRIB_VAR0 + 2:
      return TGSI_ATTRIB_VAR2;
   case FRAG_ATTRIB_VAR0 + 3:
      return TGSI_ATTRIB_VAR3;
   case FRAG_ATTRIB_VAR0 + 4:
      return TGSI_ATTRIB_VAR4;
   case FRAG_ATTRIB_VAR0 + 5:
      return TGSI_ATTRIB_VAR5;
   case FRAG_ATTRIB_VAR0 + 6:
      return TGSI_ATTRIB_VAR6;
   case FRAG_ATTRIB_VAR0 + 7:
      return TGSI_ATTRIB_VAR7;
   default:
      assert(0);
      return 0;
   }
}


/**
 * Convert FRAG_RESULT_x to TGSI_ATTRIB_y
 */
uint
tgsi_mesa_translate_fragment_output(GLuint attrib)
{
   switch (attrib) {
   case FRAG_RESULT_DEPR:
      return TGSI_ATTRIB_POS;
   case FRAG_RESULT_COLR:
      /* fall-through */
   case FRAG_RESULT_COLH:
      /* fall-through */
   case FRAG_RESULT_DATA0:
      return TGSI_ATTRIB_COLOR0;
   case FRAG_RESULT_DATA0 + 1:
      return TGSI_ATTRIB_COLOR0 + 1;
   case FRAG_RESULT_DATA0 + 2:
      return TGSI_ATTRIB_COLOR0 + 2;
   case FRAG_RESULT_DATA0 + 3:
      return TGSI_ATTRIB_COLOR0 + 3;
   default:
      assert(0);
      return 0;
   }
}


uint
tgsi_mesa_translate_vertex_input_mask(GLbitfield mask)
{
   uint tgsiMask = 0x0;
   uint i;
   for (i = 0; i < VERT_ATTRIB_MAX && mask; i++) {
      if (mask & (1 << i)) {
         tgsiMask |= 1 << tgsi_mesa_translate_vertex_input(i);
      }
      mask &= ~(1 << i);
   }
   return tgsiMask;
}


uint
tgsi_mesa_translate_vertex_output_mask(GLbitfield mask)
{
   uint tgsiMask = 0x0;
   uint i;
   for (i = 0; i < VERT_RESULT_MAX && mask; i++) {
      if (mask & (1 << i)) {
         tgsiMask |= 1 << tgsi_mesa_translate_vertex_output(i);
      }
      mask &= ~(1 << i);
   }
   return tgsiMask;
}

uint
tgsi_mesa_translate_fragment_input_mask(GLbitfield mask)
{
   uint tgsiMask = 0x0;
   uint i;
   for (i = 0; i < FRAG_ATTRIB_MAX && mask; i++) {
      if (mask & (1 << i)) {
         tgsiMask |= 1 << tgsi_mesa_translate_fragment_input(i);
      }
      mask &= ~(1 << i);
   }
   return tgsiMask;
}


uint
tgsi_mesa_translate_fragment_output_mask(GLbitfield mask)
{
   uint tgsiMask = 0x0;
   uint i;
   for (i = 0; i < FRAG_RESULT_MAX && mask; i++) {
      if (mask & (1 << i)) {
         tgsiMask |= 1 << tgsi_mesa_translate_fragment_output(i);
      }
      mask &= ~(1 << i);
   }
   return tgsiMask;
}






/*
 * Map mesa register file to TGSI register file.
 */
static GLuint
map_register_file(
   enum register_file file )
{
   switch( file ) {
   case PROGRAM_UNDEFINED:
      return TGSI_FILE_NULL;
   case PROGRAM_TEMPORARY:
      return TGSI_FILE_TEMPORARY;
   //case PROGRAM_LOCAL_PARAM:
   //case PROGRAM_ENV_PARAM:
   case PROGRAM_STATE_VAR:
   case PROGRAM_NAMED_PARAM:
   case PROGRAM_CONSTANT:
   case PROGRAM_UNIFORM:
      return TGSI_FILE_CONSTANT;
   case PROGRAM_INPUT:
      return TGSI_FILE_INPUT;
   case PROGRAM_OUTPUT:
      return TGSI_FILE_OUTPUT;
   case PROGRAM_ADDRESS:
      return TGSI_FILE_ADDRESS;
   default:
      assert( 0 );
      return TGSI_FILE_NULL;
   }
}

/**
 * Map mesa register file index to TGSI index.
 * Take special care when processing input and output indices.
 * \param processor  either TGSI_PROCESSOR_FRAGMENT or  TGSI_PROCESSOR_VERTEX
 * \param file  one of TGSI_FILE_x
 * \param index  the mesa register file index
 * \param usage_bitmask  ???
 */
static GLuint
map_register_file_index(
   GLuint processor,
   GLuint file,
   GLuint index,
   GLbitfield usage_bitmask )
{
   GLuint mapped_index;
   GLuint i;

   assert(processor == TGSI_PROCESSOR_FRAGMENT
          || processor == TGSI_PROCESSOR_VERTEX);

   switch( file ) {
   case TGSI_FILE_INPUT:
      /*
       * The fragment/vertex program input indexes (FRAG/VERT_ATTRIB_x) get
       * mapped to a packed sequence of integers.
       * If a program uses one input attribute, the mapped index will be 1.
       * If a program uses two input attribs, the mapped indexes will be 1,2.
       * If a program uses 3 input attribs, the mapped indexes will be 1,2,3.
       * etc.
       */
      assert( index < 32 );
      assert( usage_bitmask & (1 << index) );
      mapped_index = 0;
      for( i = 0; i < index; i++ ) {
         if( usage_bitmask & (1 << i) ) {
            mapped_index++;
         }
      }
      printf("Map input %d to %d\n", index, mapped_index);
      break;

   case TGSI_FILE_OUTPUT:
      /*
      assert( usage_bitmask == 0x0 );
      */
      if( processor == TGSI_PROCESSOR_FRAGMENT ) {
         /* depth result  -> index 0
          * color results -> index 1, 2, ...
          */
	 if( index == FRAG_RESULT_DEPR ) {
            mapped_index = 0; /**TGSI_ATTRIB_POS;**/
         }
         else {
            assert( index == FRAG_RESULT_COLR );
            mapped_index = 1; /**TGSI_ATTRIB_COLOR0;**/
         }
      }
      else {
         /* vertex output slots are tightly packed, find mapped pos */
         /* mapped_index = VERT_RESULT_x */
         mapped_index = 0;
         for( i = 0; i < index; i++ ) {
            if( usage_bitmask & (1 << i) ) {
               mapped_index++;
            }
         }
      }
      break;

   default:
      mapped_index = index;
   }

   return mapped_index;
}

/*
 * Map mesa texture target to TGSI texture target.
 */
static GLuint
map_texture_target(
   GLuint textarget )
{
   switch( textarget ) {
   case TEXTURE_1D_INDEX:
      return TGSI_TEXTURE_1D;
   case TEXTURE_2D_INDEX:
      return TGSI_TEXTURE_2D;
   case TEXTURE_3D_INDEX:
      return TGSI_TEXTURE_3D;
   case TEXTURE_CUBE_INDEX:
      return TGSI_TEXTURE_CUBE;
   case TEXTURE_RECT_INDEX:
      return TGSI_TEXTURE_RECT;
   default:
      assert( 0 );
   }

   return TGSI_TEXTURE_1D;
}

static GLuint
convert_sat(
   GLuint sat )
{
   switch( sat ) {
   case SATURATE_OFF:
      return TGSI_SAT_NONE;
   case SATURATE_ZERO_ONE:
      return TGSI_SAT_ZERO_ONE;
   case SATURATE_PLUS_MINUS_ONE:
      return TGSI_SAT_MINUS_PLUS_ONE;
   default:
      assert( 0 );
      return TGSI_SAT_NONE;
   }
}

static GLuint
convert_writemask(
   GLuint writemask )
{
   assert( WRITEMASK_X == TGSI_WRITEMASK_X );
   assert( WRITEMASK_Y == TGSI_WRITEMASK_Y );
   assert( WRITEMASK_Z == TGSI_WRITEMASK_Z );
   assert( WRITEMASK_W == TGSI_WRITEMASK_W );
   assert( (writemask & ~TGSI_WRITEMASK_XYZW) == 0 );

   return writemask;
}

static GLboolean
compile_instruction(
   const struct prog_instruction *inst,
   struct tgsi_full_instruction *fullinst,
   GLuint inputs_read,
   GLuint outputs_written,
   GLuint preamble_size,
   GLuint processor )
{
   GLuint i;
   struct tgsi_full_dst_register *fulldst;
   struct tgsi_full_src_register *fullsrc;

   *fullinst = tgsi_default_full_instruction();

   fullinst->Instruction.Saturate = convert_sat( inst->SaturateMode );
   fullinst->Instruction.NumDstRegs = _mesa_num_inst_dst_regs( inst->Opcode );
   fullinst->Instruction.NumSrcRegs = _mesa_num_inst_src_regs( inst->Opcode );

   fulldst = &fullinst->FullDstRegisters[0];
   fulldst->DstRegister.File = map_register_file( inst->DstReg.File );
   fulldst->DstRegister.Index = map_register_file_index(
      processor,
      fulldst->DstRegister.File,
      inst->DstReg.Index,
      outputs_written
      );
   fulldst->DstRegister.WriteMask = convert_writemask( inst->DstReg.WriteMask );

   for( i = 0; i < fullinst->Instruction.NumSrcRegs; i++ ) {
      GLuint j;

      fullsrc = &fullinst->FullSrcRegisters[i];
      fullsrc->SrcRegister.File = map_register_file( inst->SrcReg[i].File );
      fullsrc->SrcRegister.Index = map_register_file_index(
         processor,
         fullsrc->SrcRegister.File,
         inst->SrcReg[i].Index,
         inputs_read );

      for( j = 0; j < 4; j++ ) {
         GLuint swz;

         swz = GET_SWZ( inst->SrcReg[i].Swizzle, j );
         if( swz > SWIZZLE_W ) {
            tgsi_util_set_src_register_extswizzle(
               &fullsrc->SrcRegisterExtSwz,
               swz,
               j );
         }
         else {
            tgsi_util_set_src_register_swizzle(
               &fullsrc->SrcRegister,
               swz,
               j );
         }
      }

      if( inst->SrcReg[i].NegateBase == NEGATE_XYZW ) {
         fullsrc->SrcRegister.Negate = 1;
      }
      else if( inst->SrcReg[i].NegateBase != NEGATE_NONE ) {
         if( inst->SrcReg[i].NegateBase & NEGATE_X ) {
            fullsrc->SrcRegisterExtSwz.NegateX = 1;
         }
         if( inst->SrcReg[i].NegateBase & NEGATE_Y ) {
            fullsrc->SrcRegisterExtSwz.NegateY = 1;
         }
         if( inst->SrcReg[i].NegateBase & NEGATE_Z ) {
            fullsrc->SrcRegisterExtSwz.NegateZ = 1;
         }
         if( inst->SrcReg[i].NegateBase & NEGATE_W ) {
            fullsrc->SrcRegisterExtSwz.NegateW = 1;
         }
      }

      if( inst->SrcReg[i].Abs ) {
         fullsrc->SrcRegisterExtMod.Absolute = 1;
      }

      if( inst->SrcReg[i].NegateAbs ) {
         fullsrc->SrcRegisterExtMod.Negate = 1;
      }

      if( inst->SrcReg[i].RelAddr ) {
         fullsrc->SrcRegister.Indirect = 1;

         fullsrc->SrcRegisterInd.File = TGSI_FILE_ADDRESS;
         fullsrc->SrcRegisterInd.Index = 0;
      }
   }

   switch( inst->Opcode ) {
   case OPCODE_ARL:
      fullinst->Instruction.Opcode = TGSI_OPCODE_ARL;
      break;
   case OPCODE_ABS:
      fullinst->Instruction.Opcode = TGSI_OPCODE_ABS;
      break;
   case OPCODE_ADD:
      fullinst->Instruction.Opcode = TGSI_OPCODE_ADD;
      break;
   case OPCODE_BGNLOOP:
      fullinst->Instruction.Opcode = TGSI_OPCODE_BGNLOOP2;
      break;
   case OPCODE_BGNSUB:
      fullinst->Instruction.Opcode = TGSI_OPCODE_BGNSUB;
      break;
   case OPCODE_BRA:
      fullinst->Instruction.Opcode = TGSI_OPCODE_BRA;
      break;
   case OPCODE_BRK:
      fullinst->Instruction.Opcode = TGSI_OPCODE_BRK;
      break;
   case OPCODE_CMP:
      fullinst->Instruction.Opcode = TGSI_OPCODE_CMP;
      break;
   case OPCODE_CONT:
      fullinst->Instruction.Opcode = TGSI_OPCODE_CONT;
      break;
   case OPCODE_COS:
      fullinst->Instruction.Opcode = TGSI_OPCODE_COS;
      break;
   case OPCODE_DDX:
      fullinst->Instruction.Opcode = TGSI_OPCODE_DDX;
      break;
   case OPCODE_DDY:
      fullinst->Instruction.Opcode = TGSI_OPCODE_DDY;
      break;
   case OPCODE_DP3:
      fullinst->Instruction.Opcode = TGSI_OPCODE_DP3;
      break;
   case OPCODE_DP4:
      fullinst->Instruction.Opcode = TGSI_OPCODE_DP4;
      break;
   case OPCODE_DPH:
      fullinst->Instruction.Opcode = TGSI_OPCODE_DPH;
      break;
   case OPCODE_DST:
      fullinst->Instruction.Opcode = TGSI_OPCODE_DST;
      break;
   case OPCODE_ELSE:
      fullinst->Instruction.Opcode = TGSI_OPCODE_ELSE;
      fullinst->InstructionExtLabel.Label = inst->BranchTarget + preamble_size;
      break;
   case OPCODE_ENDIF:
      fullinst->Instruction.Opcode = TGSI_OPCODE_ENDIF;
      break;
   case OPCODE_ENDLOOP:
      fullinst->Instruction.Opcode = TGSI_OPCODE_ENDLOOP2;
      break;
   case OPCODE_ENDSUB:
      fullinst->Instruction.Opcode = TGSI_OPCODE_ENDSUB;
      break;
   case OPCODE_EX2:
      fullinst->Instruction.Opcode = TGSI_OPCODE_EX2;
      break;
   case OPCODE_EXP:
      fullinst->Instruction.Opcode = TGSI_OPCODE_EXP;
      break;
   case OPCODE_FLR:
      fullinst->Instruction.Opcode = TGSI_OPCODE_FLR;
      break;
   case OPCODE_FRC:
      fullinst->Instruction.Opcode = TGSI_OPCODE_FRC;
      break;
   case OPCODE_IF:
      fullinst->Instruction.Opcode = TGSI_OPCODE_IF;
      fullinst->InstructionExtLabel.Label = inst->BranchTarget + preamble_size;
      break;
   case OPCODE_INT:
      fullinst->Instruction.Opcode = TGSI_OPCODE_INT;
      break;
   case OPCODE_KIL:
      fullinst->Instruction.Opcode = TGSI_OPCODE_KIL;
      break;
   case OPCODE_LG2:
      fullinst->Instruction.Opcode = TGSI_OPCODE_LG2;
      break;
   case OPCODE_LOG:
      fullinst->Instruction.Opcode = TGSI_OPCODE_LOG;
      break;
   case OPCODE_LIT:
      fullinst->Instruction.Opcode = TGSI_OPCODE_LIT;
      break;
   case OPCODE_LRP:
      fullinst->Instruction.Opcode = TGSI_OPCODE_LRP;
      break;
   case OPCODE_MAD:
      fullinst->Instruction.Opcode = TGSI_OPCODE_MAD;
      break;
   case OPCODE_MAX:
      fullinst->Instruction.Opcode = TGSI_OPCODE_MAX;
      break;
   case OPCODE_MIN:
      fullinst->Instruction.Opcode = TGSI_OPCODE_MIN;
      break;
   case OPCODE_MOV:
      fullinst->Instruction.Opcode = TGSI_OPCODE_MOV;
      break;
   case OPCODE_MUL:
      fullinst->Instruction.Opcode = TGSI_OPCODE_MUL;
      break;
   case OPCODE_NOISE1:
      fullinst->Instruction.Opcode = TGSI_OPCODE_NOISE1;
      break;
   case OPCODE_NOISE2:
      fullinst->Instruction.Opcode = TGSI_OPCODE_NOISE2;
      break;
   case OPCODE_NOISE3:
      fullinst->Instruction.Opcode = TGSI_OPCODE_NOISE3;
      break;
   case OPCODE_NOISE4:
      fullinst->Instruction.Opcode = TGSI_OPCODE_NOISE4;
      break;
   case OPCODE_NOP:
      fullinst->Instruction.Opcode = TGSI_OPCODE_NOP;
      break;
   case OPCODE_POW:
      fullinst->Instruction.Opcode = TGSI_OPCODE_POW;
      break;
   case OPCODE_RCP:
      fullinst->Instruction.Opcode = TGSI_OPCODE_RCP;
      break;
   case OPCODE_RSQ:
      fullinst->Instruction.Opcode = TGSI_OPCODE_RSQ;
      tgsi_util_set_full_src_register_sign_mode(
         &fullinst->FullSrcRegisters[0],
         TGSI_UTIL_SIGN_CLEAR );
      break;
   case OPCODE_SCS:
      fullinst->Instruction.Opcode = TGSI_OPCODE_SCS;
      fulldst->DstRegister.WriteMask &= TGSI_WRITEMASK_XY;
      break;
   case OPCODE_SEQ:
      fullinst->Instruction.Opcode = TGSI_OPCODE_SEQ;
      break;
   case OPCODE_SGE:
      fullinst->Instruction.Opcode = TGSI_OPCODE_SGE;
      break;
   case OPCODE_SGT:
      fullinst->Instruction.Opcode = TGSI_OPCODE_SGT;
      break;
   case OPCODE_SIN:
      fullinst->Instruction.Opcode = TGSI_OPCODE_SIN;
      break;
   case OPCODE_SLE:
      fullinst->Instruction.Opcode = TGSI_OPCODE_SLE;
      break;
   case OPCODE_SLT:
      fullinst->Instruction.Opcode = TGSI_OPCODE_SLT;
      break;
   case OPCODE_SNE:
      fullinst->Instruction.Opcode = TGSI_OPCODE_SNE;
      break;
   case OPCODE_SUB:
      fullinst->Instruction.Opcode = TGSI_OPCODE_SUB;
      break;
   case OPCODE_SWZ:
      fullinst->Instruction.Opcode = TGSI_OPCODE_SWZ;
      break;
   case OPCODE_TEX:
      fullinst->Instruction.Opcode = TGSI_OPCODE_TEX;
      fullinst->Instruction.NumSrcRegs = 2;
      fullinst->InstructionExtTexture.Texture = map_texture_target( inst->TexSrcTarget );
      fullinst->FullSrcRegisters[1].SrcRegister.File = TGSI_FILE_SAMPLER;
      fullinst->FullSrcRegisters[1].SrcRegister.Index = inst->TexSrcUnit;
      break;
   case OPCODE_TXB:
      fullinst->Instruction.Opcode = TGSI_OPCODE_TXB;
      fullinst->Instruction.NumSrcRegs = 2;
      fullinst->InstructionExtTexture.Texture = map_texture_target( inst->TexSrcTarget );
      fullinst->FullSrcRegisters[1].SrcRegister.File = TGSI_FILE_SAMPLER;
      fullinst->FullSrcRegisters[1].SrcRegister.Index = inst->TexSrcUnit;
      break;
   case OPCODE_TXD:
      fullinst->Instruction.Opcode = TGSI_OPCODE_TXD;
      fullinst->Instruction.NumSrcRegs = 2;
      fullinst->InstructionExtTexture.Texture = map_texture_target( inst->TexSrcTarget );
      fullinst->FullSrcRegisters[1].SrcRegister.File = TGSI_FILE_SAMPLER;
      fullinst->FullSrcRegisters[1].SrcRegister.Index = inst->TexSrcUnit;
      break;
   case OPCODE_TXL:
      fullinst->Instruction.Opcode = TGSI_OPCODE_TXL;
      fullinst->Instruction.NumSrcRegs = 2;
      fullinst->InstructionExtTexture.Texture = map_texture_target( inst->TexSrcTarget );
      fullinst->FullSrcRegisters[1].SrcRegister.File = TGSI_FILE_SAMPLER;
      fullinst->FullSrcRegisters[1].SrcRegister.Index = inst->TexSrcUnit;
      break;
   case OPCODE_TXP:
      fullinst->Instruction.Opcode = TGSI_OPCODE_TEX;
      fullinst->Instruction.NumSrcRegs = 2;
      fullinst->InstructionExtTexture.Texture = map_texture_target( inst->TexSrcTarget );
      fullinst->FullSrcRegisters[0].SrcRegisterExtSwz.ExtDivide = TGSI_EXTSWIZZLE_W;
      fullinst->FullSrcRegisters[1].SrcRegister.File = TGSI_FILE_SAMPLER;
      fullinst->FullSrcRegisters[1].SrcRegister.Index = inst->TexSrcUnit;
      break;
   case OPCODE_XPD:
      fullinst->Instruction.Opcode = TGSI_OPCODE_XPD;
      fulldst->DstRegister.WriteMask &= TGSI_WRITEMASK_XYZ;
      break;
   case OPCODE_END:
      return GL_TRUE;
   default:
      assert( 0 );
   }

   return GL_FALSE;
}

static struct tgsi_full_declaration
make_frag_input_decl(
   GLuint first,
   GLuint last,
   GLuint interpolate,
   GLuint usage_mask )
{
   struct tgsi_full_declaration decl;

   decl = tgsi_default_full_declaration();
   decl.Declaration.File = TGSI_FILE_INPUT;
   decl.Declaration.Declare = TGSI_DECLARE_RANGE;
   decl.Declaration.UsageMask = usage_mask;
   decl.Declaration.Interpolate = 1;
   decl.u.DeclarationRange.First = first;
   decl.u.DeclarationRange.Last = last;
   decl.Interpolation.Interpolate = interpolate;

   return decl;
}

static struct tgsi_full_declaration
make_frag_output_decl(
   GLuint index,
   GLuint semantic_name,
   GLuint usage_mask )
{
   struct tgsi_full_declaration decl;

   decl = tgsi_default_full_declaration();
   decl.Declaration.File = TGSI_FILE_OUTPUT;
   decl.Declaration.Declare = TGSI_DECLARE_RANGE;
   decl.Declaration.UsageMask = usage_mask;
   decl.Declaration.Semantic = 1;
   decl.u.DeclarationRange.First = index;
   decl.u.DeclarationRange.Last = index;
   decl.Semantic.SemanticName = semantic_name;
   decl.Semantic.SemanticIndex = 0;

   return decl;
}

GLboolean
tgsi_mesa_compile_fp_program(
   const struct gl_fragment_program *program,
   struct tgsi_token *tokens,
   GLuint maxTokens )
{
   GLuint i, ti, count;
   struct tgsi_header *header;
   struct tgsi_processor *processor;
   struct tgsi_full_declaration fulldecl;
   struct tgsi_full_instruction fullinst;
   struct tgsi_full_dst_register *fulldst;
   struct tgsi_full_src_register *fullsrc;
   GLuint inputs_read;
   GLboolean reads_wpos;
   GLuint preamble_size = 0;

   *(struct tgsi_version *) &tokens[0] = tgsi_build_version();

   header = (struct tgsi_header *) &tokens[1];
   *header = tgsi_build_header();

   processor = (struct tgsi_processor *) &tokens[2];
   *processor = tgsi_build_processor( TGSI_PROCESSOR_FRAGMENT, header );

   ti = 3;

   reads_wpos = program->Base.InputsRead & (1 << FRAG_ATTRIB_WPOS);
   inputs_read = program->Base.InputsRead | (1 << FRAG_ATTRIB_WPOS);

   /*
    * Declare input attributes. Note that we do not interpolate fragment position.
    */

   /* Fragment position. */
   if( reads_wpos ) {
      fulldecl = make_frag_input_decl(
         0,
         0,
         TGSI_INTERPOLATE_CONSTANT,
         TGSI_WRITEMASK_XY );
      ti += tgsi_build_full_declaration(
         &fulldecl,
         &tokens[ti],
         header,
         maxTokens - ti );
   }

   /* Fragment zw. */
   fulldecl = make_frag_input_decl(
      0,
      0,
      TGSI_INTERPOLATE_LINEAR,
      reads_wpos ? TGSI_WRITEMASK_ZW : TGSI_WRITEMASK_Z );
   ti += tgsi_build_full_declaration(
      &fulldecl,
      &tokens[ti],
      header,
      maxTokens - ti );

   count = 0;
   for( i = 1; i < 32; i++ ) {
      if( inputs_read & (1 << i) ) {
         count++;
      }
   }
   if( count > 0 ) {
      fulldecl = make_frag_input_decl(
         1,
         1 + count - 1,
         TGSI_INTERPOLATE_LINEAR,
         TGSI_WRITEMASK_XYZW );
      ti += tgsi_build_full_declaration(
         &fulldecl,
         &tokens[ti],
         header,
         maxTokens - ti );
   }

   /*
    * Declare output attributes.
    */
   assert(
      program->Base.OutputsWritten ==
      (program->Base.OutputsWritten & ((1 << FRAG_RESULT_COLR) | (1 << FRAG_RESULT_DEPR))) );

   fulldecl = make_frag_output_decl(
      0,
      TGSI_SEMANTIC_DEPTH,
      TGSI_WRITEMASK_Z );
   ti += tgsi_build_full_declaration(
      &fulldecl,
      &tokens[ti],
      header,
      maxTokens - ti );

   if( program->Base.OutputsWritten & (1 << FRAG_RESULT_COLR) ) {
      fulldecl = make_frag_output_decl(
         1,
         TGSI_SEMANTIC_COLOR,
         TGSI_WRITEMASK_XYZW );
      ti += tgsi_build_full_declaration(
         &fulldecl,
         &tokens[ti],
         header,
         maxTokens - ti );
   }

   /*
    * Copy fragment z if the shader does not write it.
    */
#if 0
   if( !(program->Base.OutputsWritten & (1 << FRAG_RESULT_DEPR)) ) {
      fullinst = tgsi_default_full_instruction();

      fullinst.Instruction.Opcode = TGSI_OPCODE_MOV;
      fullinst.Instruction.NumDstRegs = 1;
      fullinst.Instruction.NumSrcRegs = 1;

      fulldst = &fullinst.FullDstRegisters[0];
      fulldst->DstRegister.File = TGSI_FILE_OUTPUT;
      fulldst->DstRegister.Index = 0;
      fulldst->DstRegister.WriteMask = TGSI_WRITEMASK_Z;

      fullsrc = &fullinst.FullSrcRegisters[0];
      fullsrc->SrcRegister.File = TGSI_FILE_INPUT;
      fullsrc->SrcRegister.Index = 0;

      ti += tgsi_build_full_instruction(
         &fullinst,
         &tokens[ti],
         header,
         maxTokens - ti );
      preamble_size++;
   }
#endif

   for( i = 0; i < program->Base.NumInstructions; i++ ) {
      if( compile_instruction(
            &program->Base.Instructions[i],
            &fullinst,
            inputs_read,
            ~0, /*outputs_written*/
            preamble_size,
            TGSI_PROCESSOR_FRAGMENT ) ) {
         assert( i == program->Base.NumInstructions - 1 );

         if( TGSI_DEBUG ) {
            tgsi_dump( tokens, 0 );
         }
         break;
      }

      ti += tgsi_build_full_instruction(
         &fullinst,
         &tokens[ti],
         header,
         maxTokens - ti );
   }

   return GL_TRUE;
}

GLboolean
tgsi_mesa_compile_vp_program(
   const struct gl_vertex_program *program,
   struct tgsi_token *tokens,
   GLuint maxTokens )
{
   GLuint i, ti;
   struct tgsi_header *header;
   struct tgsi_processor *processor;
   struct tgsi_full_instruction fullinst;
   GLuint inputs_read = ~0;
   GLuint outputs_written;

   outputs_written = program->Base.OutputsWritten;

   *(struct tgsi_version *) &tokens[0] = tgsi_build_version();

   header = (struct tgsi_header *) &tokens[1];
   *header = tgsi_build_header();

   processor = (struct tgsi_processor *) &tokens[2];
   *processor = tgsi_build_processor( TGSI_PROCESSOR_VERTEX, header );

   ti = 3;

   for( i = 0; i < program->Base.NumInstructions; i++ ) {
      if( compile_instruction(
            &program->Base.Instructions[i],
            &fullinst,
            inputs_read,
            outputs_written,
            0,
            TGSI_PROCESSOR_VERTEX ) ) {
         assert( i == program->Base.NumInstructions - 1 );

	 if( TGSI_DEBUG ) {
            tgsi_dump( tokens, 0 );
         }
         break;
      }

      ti += tgsi_build_full_instruction(
         &fullinst,
         &tokens[ti],
         header,
         maxTokens - ti );
   }

   return GL_TRUE;
}