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
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
|
/**************************************************************************
*
* Copyright 2009 VMware, Inc.
* Copyright 2007-2008 Tungsten Graphics, Inc., Cedar Park, Texas.
* All Rights Reserved.
*
* 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 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 TUNGSTEN GRAPHICS AND/OR ITS 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.
*
**************************************************************************/
/**
* @file
* TGSI to LLVM IR translation -- SoA.
*
* @author Jose Fonseca <jfonseca@vmware.com>
*
* Based on tgsi_sse2.c code written by Michal Krol, Keith Whitwell,
* Brian Paul, and others.
*/
#include "pipe/p_config.h"
#include "pipe/p_shader_tokens.h"
#include "util/u_debug.h"
#include "util/u_math.h"
#include "util/u_memory.h"
#include "tgsi/tgsi_dump.h"
#include "tgsi/tgsi_info.h"
#include "tgsi/tgsi_parse.h"
#include "tgsi/tgsi_util.h"
#include "tgsi/tgsi_exec.h"
#include "tgsi/tgsi_scan.h"
#include "lp_bld_type.h"
#include "lp_bld_const.h"
#include "lp_bld_arit.h"
#include "lp_bld_logic.h"
#include "lp_bld_swizzle.h"
#include "lp_bld_flow.h"
#include "lp_bld_tgsi.h"
#include "lp_bld_limits.h"
#include "lp_bld_debug.h"
#define FOR_EACH_CHANNEL( CHAN )\
for (CHAN = 0; CHAN < NUM_CHANNELS; CHAN++)
#define IS_DST0_CHANNEL_ENABLED( INST, CHAN )\
((INST)->Dst[0].Register.WriteMask & (1 << (CHAN)))
#define IF_IS_DST0_CHANNEL_ENABLED( INST, CHAN )\
if (IS_DST0_CHANNEL_ENABLED( INST, CHAN ))
#define FOR_EACH_DST0_ENABLED_CHANNEL( INST, CHAN )\
FOR_EACH_CHANNEL( CHAN )\
IF_IS_DST0_CHANNEL_ENABLED( INST, CHAN )
#define CHAN_X 0
#define CHAN_Y 1
#define CHAN_Z 2
#define CHAN_W 3
#define QUAD_TOP_LEFT 0
#define QUAD_TOP_RIGHT 1
#define QUAD_BOTTOM_LEFT 2
#define QUAD_BOTTOM_RIGHT 3
struct lp_exec_mask {
struct lp_build_context *bld;
boolean has_mask;
LLVMTypeRef int_vec_type;
LLVMValueRef cond_stack[LP_MAX_TGSI_NESTING];
int cond_stack_size;
LLVMValueRef cond_mask;
LLVMValueRef break_stack[LP_MAX_TGSI_NESTING];
int break_stack_size;
LLVMValueRef break_mask;
LLVMValueRef cont_stack[LP_MAX_TGSI_NESTING];
int cont_stack_size;
LLVMValueRef cont_mask;
LLVMBasicBlockRef loop_stack[LP_MAX_TGSI_NESTING];
int loop_stack_size;
LLVMBasicBlockRef loop_block;
LLVMValueRef exec_mask;
};
struct lp_build_tgsi_soa_context
{
struct lp_build_context base;
LLVMValueRef consts_ptr;
const LLVMValueRef *pos;
const LLVMValueRef (*inputs)[NUM_CHANNELS];
LLVMValueRef (*outputs)[NUM_CHANNELS];
struct lp_build_sampler_soa *sampler;
LLVMValueRef immediates[LP_MAX_TGSI_IMMEDIATES][NUM_CHANNELS];
LLVMValueRef temps[LP_MAX_TGSI_TEMPS][NUM_CHANNELS];
LLVMValueRef addr[LP_MAX_TGSI_ADDRS][NUM_CHANNELS];
/* we allocate an array of temps if we have indirect
* addressing and then the temps above is unused */
LLVMValueRef temps_array;
boolean has_indirect_addressing;
struct lp_build_mask_context *mask;
struct lp_exec_mask exec_mask;
};
static const unsigned char
swizzle_left[4] = {
QUAD_TOP_LEFT, QUAD_TOP_LEFT,
QUAD_BOTTOM_LEFT, QUAD_BOTTOM_LEFT
};
static const unsigned char
swizzle_right[4] = {
QUAD_TOP_RIGHT, QUAD_TOP_RIGHT,
QUAD_BOTTOM_RIGHT, QUAD_BOTTOM_RIGHT
};
static const unsigned char
swizzle_top[4] = {
QUAD_TOP_LEFT, QUAD_TOP_RIGHT,
QUAD_TOP_LEFT, QUAD_TOP_RIGHT
};
static const unsigned char
swizzle_bottom[4] = {
QUAD_BOTTOM_LEFT, QUAD_BOTTOM_RIGHT,
QUAD_BOTTOM_LEFT, QUAD_BOTTOM_RIGHT
};
static void lp_exec_mask_init(struct lp_exec_mask *mask, struct lp_build_context *bld)
{
mask->bld = bld;
mask->has_mask = FALSE;
mask->cond_stack_size = 0;
mask->loop_stack_size = 0;
mask->break_stack_size = 0;
mask->cont_stack_size = 0;
mask->int_vec_type = lp_build_int_vec_type(mask->bld->type);
}
static void lp_exec_mask_update(struct lp_exec_mask *mask)
{
if (mask->loop_stack_size) {
/*for loops we need to update the entire mask at runtime */
LLVMValueRef tmp;
assert(mask->break_mask);
tmp = LLVMBuildAnd(mask->bld->builder,
mask->cont_mask,
mask->break_mask,
"maskcb");
mask->exec_mask = LLVMBuildAnd(mask->bld->builder,
mask->cond_mask,
tmp,
"maskfull");
} else
mask->exec_mask = mask->cond_mask;
mask->has_mask = (mask->cond_stack_size > 0 ||
mask->loop_stack_size > 0);
}
static void lp_exec_mask_cond_push(struct lp_exec_mask *mask,
LLVMValueRef val)
{
assert(mask->cond_stack_size < LP_MAX_TGSI_NESTING);
mask->cond_stack[mask->cond_stack_size++] = mask->cond_mask;
mask->cond_mask = LLVMBuildBitCast(mask->bld->builder, val,
mask->int_vec_type, "");
lp_exec_mask_update(mask);
}
static void lp_exec_mask_cond_invert(struct lp_exec_mask *mask)
{
LLVMValueRef prev_mask = mask->cond_stack[mask->cond_stack_size - 1];
LLVMValueRef inv_mask = LLVMBuildNot(mask->bld->builder,
mask->cond_mask, "");
/* means that we didn't have any mask before and that
* we were fully enabled */
if (mask->cond_stack_size <= 1) {
prev_mask = LLVMConstAllOnes(mask->int_vec_type);
}
mask->cond_mask = LLVMBuildAnd(mask->bld->builder,
inv_mask,
prev_mask, "");
lp_exec_mask_update(mask);
}
static void lp_exec_mask_cond_pop(struct lp_exec_mask *mask)
{
mask->cond_mask = mask->cond_stack[--mask->cond_stack_size];
lp_exec_mask_update(mask);
}
static void lp_exec_bgnloop(struct lp_exec_mask *mask)
{
if (mask->cont_stack_size == 0)
mask->cont_mask = LLVMConstAllOnes(mask->int_vec_type);
if (mask->break_stack_size == 0)
mask->break_mask = LLVMConstAllOnes(mask->int_vec_type);
if (mask->cond_stack_size == 0)
mask->cond_mask = LLVMConstAllOnes(mask->int_vec_type);
assert(mask->break_stack_size < LP_MAX_TGSI_NESTING);
assert(mask->cont_stack_size < LP_MAX_TGSI_NESTING);
assert(mask->break_stack_size < LP_MAX_TGSI_NESTING);
mask->break_stack[mask->break_stack_size++] = mask->break_mask;
mask->cont_stack[mask->cont_stack_size++] = mask->cont_mask;
mask->loop_stack[mask->loop_stack_size++] = mask->loop_block;
mask->loop_block = lp_build_insert_new_block(mask->bld->builder, "bgnloop");
LLVMBuildBr(mask->bld->builder, mask->loop_block);
LLVMPositionBuilderAtEnd(mask->bld->builder, mask->loop_block);
lp_exec_mask_update(mask);
}
static void lp_exec_break(struct lp_exec_mask *mask)
{
LLVMValueRef exec_mask = LLVMBuildNot(mask->bld->builder,
mask->exec_mask,
"break");
mask->break_mask = LLVMBuildAnd(mask->bld->builder,
mask->break_mask,
exec_mask, "break_full");
lp_exec_mask_update(mask);
}
static void lp_exec_continue(struct lp_exec_mask *mask)
{
LLVMValueRef exec_mask = LLVMBuildNot(mask->bld->builder,
mask->exec_mask,
"");
mask->cont_mask = LLVMBuildAnd(mask->bld->builder,
mask->cont_mask,
exec_mask, "");
lp_exec_mask_update(mask);
}
static void lp_exec_endloop(struct lp_exec_mask *mask)
{
LLVMBasicBlockRef endloop;
LLVMTypeRef reg_type = LLVMIntType(mask->bld->type.width*
mask->bld->type.length);
LLVMValueRef i1cond;
assert(mask->break_mask);
/* i1cond = (mask == 0) */
i1cond = LLVMBuildICmp(
mask->bld->builder,
LLVMIntNE,
LLVMBuildBitCast(mask->bld->builder, mask->break_mask, reg_type, ""),
LLVMConstNull(reg_type), "");
endloop = lp_build_insert_new_block(mask->bld->builder, "endloop");
LLVMBuildCondBr(mask->bld->builder,
i1cond, mask->loop_block, endloop);
LLVMPositionBuilderAtEnd(mask->bld->builder, endloop);
mask->loop_block = mask->loop_stack[--mask->loop_stack_size];
/* pop the cont mask */
if (mask->cont_stack_size) {
mask->cont_mask = mask->cont_stack[--mask->cont_stack_size];
}
/* pop the break mask */
if (mask->break_stack_size) {
mask->break_mask = mask->break_stack[--mask->break_stack_size];
}
lp_exec_mask_update(mask);
}
/* stores val into an address pointed to by dst.
* mask->exec_mask is used to figure out which bits of val
* should be stored into the address
* (0 means don't store this bit, 1 means do store).
*/
static void lp_exec_mask_store(struct lp_exec_mask *mask,
LLVMValueRef val,
LLVMValueRef dst)
{
if (mask->has_mask) {
LLVMValueRef real_val, dst_val;
dst_val = LLVMBuildLoad(mask->bld->builder, dst, "");
real_val = lp_build_select(mask->bld,
mask->exec_mask,
val, dst_val);
LLVMBuildStore(mask->bld->builder, real_val, dst);
} else
LLVMBuildStore(mask->bld->builder, val, dst);
}
static LLVMValueRef
emit_ddx(struct lp_build_tgsi_soa_context *bld,
LLVMValueRef src)
{
LLVMValueRef src_left = lp_build_swizzle1_aos(&bld->base, src, swizzle_left);
LLVMValueRef src_right = lp_build_swizzle1_aos(&bld->base, src, swizzle_right);
return lp_build_sub(&bld->base, src_right, src_left);
}
static LLVMValueRef
emit_ddy(struct lp_build_tgsi_soa_context *bld,
LLVMValueRef src)
{
LLVMValueRef src_top = lp_build_swizzle1_aos(&bld->base, src, swizzle_top);
LLVMValueRef src_bottom = lp_build_swizzle1_aos(&bld->base, src, swizzle_bottom);
return lp_build_sub(&bld->base, src_top, src_bottom);
}
static LLVMValueRef
get_temp_ptr(struct lp_build_tgsi_soa_context *bld,
unsigned index,
unsigned swizzle,
boolean is_indirect,
LLVMValueRef addr)
{
if (!bld->has_indirect_addressing) {
return bld->temps[index][swizzle];
} else {
LLVMValueRef lindex =
LLVMConstInt(LLVMInt32Type(), index*4 + swizzle, 0);
if (is_indirect)
lindex = lp_build_add(&bld->base, lindex, addr);
return LLVMBuildGEP(bld->base.builder, bld->temps_array, &lindex, 1, "");
}
}
/**
* Register fetch.
*/
static LLVMValueRef
emit_fetch(
struct lp_build_tgsi_soa_context *bld,
const struct tgsi_full_instruction *inst,
unsigned index,
const unsigned chan_index )
{
const struct tgsi_full_src_register *reg = &inst->Src[index];
unsigned swizzle = tgsi_util_get_full_src_register_swizzle( reg, chan_index );
LLVMValueRef res;
LLVMValueRef addr;
switch (swizzle) {
case TGSI_SWIZZLE_X:
case TGSI_SWIZZLE_Y:
case TGSI_SWIZZLE_Z:
case TGSI_SWIZZLE_W:
if (reg->Register.Indirect) {
LLVMTypeRef int_vec_type = lp_build_int_vec_type(bld->base.type);
unsigned swizzle = tgsi_util_get_src_register_swizzle( ®->Indirect, chan_index );
addr = LLVMBuildLoad(bld->base.builder,
bld->addr[reg->Indirect.Index][swizzle],
"");
/* for indexing we want integers */
addr = LLVMBuildFPToSI(bld->base.builder, addr,
int_vec_type, "");
addr = LLVMBuildExtractElement(bld->base.builder,
addr, LLVMConstInt(LLVMInt32Type(), 0, 0),
"");
addr = lp_build_mul(&bld->base, addr, LLVMConstInt(LLVMInt32Type(), 4, 0));
}
switch (reg->Register.File) {
case TGSI_FILE_CONSTANT: {
LLVMValueRef index = LLVMConstInt(LLVMInt32Type(), reg->Register.Index*4 + swizzle, 0);
LLVMValueRef scalar, scalar_ptr;
if (reg->Register.Indirect) {
/*lp_build_printf(bld->base.builder,
"\taddr = %d\n", addr);*/
index = lp_build_add(&bld->base, index, addr);
}
scalar_ptr = LLVMBuildGEP(bld->base.builder, bld->consts_ptr, &index, 1, "");
scalar = LLVMBuildLoad(bld->base.builder, scalar_ptr, "");
res = lp_build_broadcast_scalar(&bld->base, scalar);
break;
}
case TGSI_FILE_IMMEDIATE:
res = bld->immediates[reg->Register.Index][swizzle];
assert(res);
break;
case TGSI_FILE_INPUT:
res = bld->inputs[reg->Register.Index][swizzle];
assert(res);
break;
case TGSI_FILE_TEMPORARY: {
LLVMValueRef temp_ptr = get_temp_ptr(bld, reg->Register.Index,
swizzle,
reg->Register.Indirect,
addr);
res = LLVMBuildLoad(bld->base.builder, temp_ptr, "");
if(!res)
return bld->base.undef;
break;
}
default:
assert( 0 );
return bld->base.undef;
}
break;
default:
assert( 0 );
return bld->base.undef;
}
switch( tgsi_util_get_full_src_register_sign_mode( reg, chan_index ) ) {
case TGSI_UTIL_SIGN_CLEAR:
res = lp_build_abs( &bld->base, res );
break;
case TGSI_UTIL_SIGN_SET:
/* TODO: Use bitwese OR for floating point */
res = lp_build_abs( &bld->base, res );
res = LLVMBuildNeg( bld->base.builder, res, "" );
break;
case TGSI_UTIL_SIGN_TOGGLE:
res = LLVMBuildNeg( bld->base.builder, res, "" );
break;
case TGSI_UTIL_SIGN_KEEP:
break;
}
return res;
}
/**
* Register fetch with derivatives.
*/
static void
emit_fetch_deriv(
struct lp_build_tgsi_soa_context *bld,
const struct tgsi_full_instruction *inst,
unsigned index,
const unsigned chan_index,
LLVMValueRef *res,
LLVMValueRef *ddx,
LLVMValueRef *ddy)
{
LLVMValueRef src;
src = emit_fetch(bld, inst, index, chan_index);
if(res)
*res = src;
/* TODO: use interpolation coeffs for inputs */
if(ddx)
*ddx = emit_ddx(bld, src);
if(ddy)
*ddy = emit_ddy(bld, src);
}
/**
* Register store.
*/
static void
emit_store(
struct lp_build_tgsi_soa_context *bld,
const struct tgsi_full_instruction *inst,
unsigned index,
unsigned chan_index,
LLVMValueRef value)
{
const struct tgsi_full_dst_register *reg = &inst->Dst[index];
LLVMValueRef addr;
switch( inst->Instruction.Saturate ) {
case TGSI_SAT_NONE:
break;
case TGSI_SAT_ZERO_ONE:
value = lp_build_max(&bld->base, value, bld->base.zero);
value = lp_build_min(&bld->base, value, bld->base.one);
break;
case TGSI_SAT_MINUS_PLUS_ONE:
value = lp_build_max(&bld->base, value, lp_build_const_vec(bld->base.type, -1.0));
value = lp_build_min(&bld->base, value, bld->base.one);
break;
default:
assert(0);
}
if (reg->Register.Indirect) {
LLVMTypeRef int_vec_type = lp_build_int_vec_type(bld->base.type);
unsigned swizzle = tgsi_util_get_src_register_swizzle( ®->Indirect, chan_index );
addr = LLVMBuildLoad(bld->base.builder,
bld->addr[reg->Indirect.Index][swizzle],
"");
/* for indexing we want integers */
addr = LLVMBuildFPToSI(bld->base.builder, addr,
int_vec_type, "");
addr = LLVMBuildExtractElement(bld->base.builder,
addr, LLVMConstInt(LLVMInt32Type(), 0, 0),
"");
addr = lp_build_mul(&bld->base, addr, LLVMConstInt(LLVMInt32Type(), 4, 0));
}
switch( reg->Register.File ) {
case TGSI_FILE_OUTPUT:
lp_exec_mask_store(&bld->exec_mask, value,
bld->outputs[reg->Register.Index][chan_index]);
break;
case TGSI_FILE_TEMPORARY: {
LLVMValueRef temp_ptr = get_temp_ptr(bld, reg->Register.Index,
chan_index,
reg->Register.Indirect,
addr);
lp_exec_mask_store(&bld->exec_mask, value, temp_ptr);
break;
}
case TGSI_FILE_ADDRESS:
lp_exec_mask_store(&bld->exec_mask, value,
bld->addr[reg->Indirect.Index][chan_index]);
break;
case TGSI_FILE_PREDICATE:
/* FIXME */
break;
default:
assert( 0 );
}
}
/**
* High-level instruction translators.
*/
static void
emit_tex( struct lp_build_tgsi_soa_context *bld,
const struct tgsi_full_instruction *inst,
boolean apply_lodbias,
boolean projected,
LLVMValueRef *texel)
{
const uint unit = inst->Src[1].Register.Index;
LLVMValueRef lodbias;
LLVMValueRef oow = NULL;
LLVMValueRef coords[3];
unsigned num_coords;
unsigned i;
if (!bld->sampler) {
_debug_printf("warning: found texture instruction but no sampler generator supplied\n");
for (i = 0; i < 4; i++) {
texel[i] = bld->base.undef;
}
return;
}
switch (inst->Texture.Texture) {
case TGSI_TEXTURE_1D:
num_coords = 1;
break;
case TGSI_TEXTURE_2D:
case TGSI_TEXTURE_RECT:
num_coords = 2;
break;
case TGSI_TEXTURE_SHADOW1D:
case TGSI_TEXTURE_SHADOW2D:
case TGSI_TEXTURE_SHADOWRECT:
case TGSI_TEXTURE_3D:
case TGSI_TEXTURE_CUBE:
num_coords = 3;
break;
default:
assert(0);
return;
}
if(apply_lodbias)
lodbias = emit_fetch( bld, inst, 0, 3 );
else
lodbias = bld->base.zero;
if (projected) {
oow = emit_fetch( bld, inst, 0, 3 );
oow = lp_build_rcp(&bld->base, oow);
}
for (i = 0; i < num_coords; i++) {
coords[i] = emit_fetch( bld, inst, 0, i );
if (projected)
coords[i] = lp_build_mul(&bld->base, coords[i], oow);
}
for (i = num_coords; i < 3; i++) {
coords[i] = bld->base.undef;
}
bld->sampler->emit_fetch_texel(bld->sampler,
bld->base.builder,
bld->base.type,
unit, num_coords, coords, lodbias,
texel);
}
/**
* Kill fragment if any of the src register values are negative.
*/
static void
emit_kil(
struct lp_build_tgsi_soa_context *bld,
const struct tgsi_full_instruction *inst )
{
const struct tgsi_full_src_register *reg = &inst->Src[0];
LLVMValueRef terms[NUM_CHANNELS];
LLVMValueRef mask;
unsigned chan_index;
memset(&terms, 0, sizeof terms);
FOR_EACH_CHANNEL( chan_index ) {
unsigned swizzle;
/* Unswizzle channel */
swizzle = tgsi_util_get_full_src_register_swizzle( reg, chan_index );
/* Check if the component has not been already tested. */
assert(swizzle < NUM_CHANNELS);
if( !terms[swizzle] )
/* TODO: change the comparison operator instead of setting the sign */
terms[swizzle] = emit_fetch(bld, inst, 0, chan_index );
}
mask = NULL;
FOR_EACH_CHANNEL( chan_index ) {
if(terms[chan_index]) {
LLVMValueRef chan_mask;
/*
* If term < 0 then mask = 0 else mask = ~0.
*/
chan_mask = lp_build_cmp(&bld->base, PIPE_FUNC_GEQUAL, terms[chan_index], bld->base.zero);
if(mask)
mask = LLVMBuildAnd(bld->base.builder, mask, chan_mask, "");
else
mask = chan_mask;
}
}
if(mask)
lp_build_mask_update(bld->mask, mask);
}
/**
* Predicated fragment kill.
* XXX Actually, we do an unconditional kill (as in tgsi_exec.c).
* The only predication is the execution mask which will apply if
* we're inside a loop or conditional.
*/
static void
emit_kilp(struct lp_build_tgsi_soa_context *bld,
const struct tgsi_full_instruction *inst)
{
LLVMValueRef mask;
/* For those channels which are "alive", disable fragment shader
* execution.
*/
if (bld->exec_mask.has_mask) {
mask = LLVMBuildNot(bld->base.builder, bld->exec_mask.exec_mask, "kilp");
}
else {
mask = bld->base.zero;
}
lp_build_mask_update(bld->mask, mask);
}
static void
emit_declaration(
struct lp_build_tgsi_soa_context *bld,
const struct tgsi_full_declaration *decl)
{
LLVMTypeRef vec_type = lp_build_vec_type(bld->base.type);
unsigned first = decl->Range.First;
unsigned last = decl->Range.Last;
unsigned idx, i;
for (idx = first; idx <= last; ++idx) {
switch (decl->Declaration.File) {
case TGSI_FILE_TEMPORARY:
assert(idx < LP_MAX_TGSI_TEMPS);
if (bld->has_indirect_addressing) {
LLVMValueRef val = LLVMConstInt(LLVMInt32Type(),
last*4 + 4, 0);
bld->temps_array = lp_build_array_alloca(bld->base.builder,
vec_type, val, "");
} else {
for (i = 0; i < NUM_CHANNELS; i++)
bld->temps[idx][i] = lp_build_alloca(bld->base.builder,
vec_type, "");
}
break;
case TGSI_FILE_OUTPUT:
for (i = 0; i < NUM_CHANNELS; i++)
bld->outputs[idx][i] = lp_build_alloca(bld->base.builder,
vec_type, "");
break;
case TGSI_FILE_ADDRESS:
assert(idx < LP_MAX_TGSI_ADDRS);
for (i = 0; i < NUM_CHANNELS; i++)
bld->addr[idx][i] = lp_build_alloca(bld->base.builder,
vec_type, "");
break;
case TGSI_FILE_PREDICATE:
_debug_printf("warning: predicate registers not yet implemented\n");
break;
default:
/* don't need to declare other vars */
break;
}
}
}
/**
* Emit LLVM for one TGSI instruction.
* \param return TRUE for success, FALSE otherwise
*/
static boolean
emit_instruction(
struct lp_build_tgsi_soa_context *bld,
const struct tgsi_full_instruction *inst,
const struct tgsi_opcode_info *info)
{
unsigned chan_index;
LLVMValueRef src0, src1, src2;
LLVMValueRef tmp0, tmp1, tmp2;
LLVMValueRef tmp3 = NULL;
LLVMValueRef tmp4 = NULL;
LLVMValueRef tmp5 = NULL;
LLVMValueRef tmp6 = NULL;
LLVMValueRef tmp7 = NULL;
LLVMValueRef res;
LLVMValueRef dst0[NUM_CHANNELS];
/*
* Stores and write masks are handled in a general fashion after the long
* instruction opcode switch statement.
*
* Although not stricitly necessary, we avoid generating instructions for
* channels which won't be stored, in cases where's that easy. For some
* complex instructions, like texture sampling, it is more convenient to
* assume a full writemask and then let LLVM optimization passes eliminate
* redundant code.
*/
assert(info->num_dst <= 1);
if(info->num_dst) {
FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
dst0[chan_index] = bld->base.undef;
}
}
switch (inst->Instruction.Opcode) {
case TGSI_OPCODE_ARL:
FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
tmp0 = emit_fetch( bld, inst, 0, chan_index );
tmp0 = lp_build_floor(&bld->base, tmp0);
dst0[chan_index] = tmp0;
}
break;
case TGSI_OPCODE_MOV:
FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
dst0[chan_index] = emit_fetch( bld, inst, 0, chan_index );
}
break;
case TGSI_OPCODE_LIT:
if( IS_DST0_CHANNEL_ENABLED( inst, CHAN_X ) ) {
dst0[CHAN_X] = bld->base.one;
}
if( IS_DST0_CHANNEL_ENABLED( inst, CHAN_Y ) ) {
src0 = emit_fetch( bld, inst, 0, CHAN_X );
dst0[CHAN_Y] = lp_build_max( &bld->base, src0, bld->base.zero);
}
if( IS_DST0_CHANNEL_ENABLED( inst, CHAN_Z ) ) {
/* XMM[1] = SrcReg[0].yyyy */
tmp1 = emit_fetch( bld, inst, 0, CHAN_Y );
/* XMM[1] = max(XMM[1], 0) */
tmp1 = lp_build_max( &bld->base, tmp1, bld->base.zero);
/* XMM[2] = SrcReg[0].wwww */
tmp2 = emit_fetch( bld, inst, 0, CHAN_W );
tmp1 = lp_build_pow( &bld->base, tmp1, tmp2);
tmp0 = emit_fetch( bld, inst, 0, CHAN_X );
tmp2 = lp_build_cmp(&bld->base, PIPE_FUNC_GREATER, tmp0, bld->base.zero);
dst0[CHAN_Z] = lp_build_select(&bld->base, tmp2, tmp1, bld->base.zero);
}
if( IS_DST0_CHANNEL_ENABLED( inst, CHAN_W ) ) {
dst0[CHAN_W] = bld->base.one;
}
break;
case TGSI_OPCODE_RCP:
/* TGSI_OPCODE_RECIP */
src0 = emit_fetch( bld, inst, 0, CHAN_X );
res = lp_build_rcp(&bld->base, src0);
FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
dst0[chan_index] = res;
}
break;
case TGSI_OPCODE_RSQ:
/* TGSI_OPCODE_RECIPSQRT */
src0 = emit_fetch( bld, inst, 0, CHAN_X );
src0 = lp_build_abs(&bld->base, src0);
res = lp_build_rsqrt(&bld->base, src0);
FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
dst0[chan_index] = res;
}
break;
case TGSI_OPCODE_EXP:
if (IS_DST0_CHANNEL_ENABLED( inst, CHAN_X ) ||
IS_DST0_CHANNEL_ENABLED( inst, CHAN_Y ) ||
IS_DST0_CHANNEL_ENABLED( inst, CHAN_Z )) {
LLVMValueRef *p_exp2_int_part = NULL;
LLVMValueRef *p_frac_part = NULL;
LLVMValueRef *p_exp2 = NULL;
src0 = emit_fetch( bld, inst, 0, CHAN_X );
if (IS_DST0_CHANNEL_ENABLED( inst, CHAN_X ))
p_exp2_int_part = &tmp0;
if (IS_DST0_CHANNEL_ENABLED( inst, CHAN_Y ))
p_frac_part = &tmp1;
if (IS_DST0_CHANNEL_ENABLED( inst, CHAN_Z ))
p_exp2 = &tmp2;
lp_build_exp2_approx(&bld->base, src0, p_exp2_int_part, p_frac_part, p_exp2);
if (IS_DST0_CHANNEL_ENABLED( inst, CHAN_X ))
dst0[CHAN_X] = tmp0;
if (IS_DST0_CHANNEL_ENABLED( inst, CHAN_Y ))
dst0[CHAN_Y] = tmp1;
if (IS_DST0_CHANNEL_ENABLED( inst, CHAN_Z ))
dst0[CHAN_Z] = tmp2;
}
/* dst.w = 1.0 */
if (IS_DST0_CHANNEL_ENABLED( inst, CHAN_W )) {
dst0[CHAN_W] = bld->base.one;
}
break;
case TGSI_OPCODE_LOG:
if (IS_DST0_CHANNEL_ENABLED( inst, CHAN_X ) ||
IS_DST0_CHANNEL_ENABLED( inst, CHAN_Y ) ||
IS_DST0_CHANNEL_ENABLED( inst, CHAN_Z )) {
LLVMValueRef *p_floor_log2 = NULL;
LLVMValueRef *p_exp = NULL;
LLVMValueRef *p_log2 = NULL;
src0 = emit_fetch( bld, inst, 0, CHAN_X );
src0 = lp_build_abs( &bld->base, src0 );
if (IS_DST0_CHANNEL_ENABLED( inst, CHAN_X ))
p_floor_log2 = &tmp0;
if (IS_DST0_CHANNEL_ENABLED( inst, CHAN_Y ))
p_exp = &tmp1;
if (IS_DST0_CHANNEL_ENABLED( inst, CHAN_Z ))
p_log2 = &tmp2;
lp_build_log2_approx(&bld->base, src0, p_exp, p_floor_log2, p_log2);
/* dst.x = floor(lg2(abs(src.x))) */
if (IS_DST0_CHANNEL_ENABLED( inst, CHAN_X ))
dst0[CHAN_X] = tmp0;
/* dst.y = abs(src)/ex2(floor(lg2(abs(src.x)))) */
if (IS_DST0_CHANNEL_ENABLED( inst, CHAN_Y )) {
dst0[CHAN_Y] = lp_build_div( &bld->base, src0, tmp1);
}
/* dst.z = lg2(abs(src.x)) */
if (IS_DST0_CHANNEL_ENABLED( inst, CHAN_Z ))
dst0[CHAN_Z] = tmp2;
}
/* dst.w = 1.0 */
if (IS_DST0_CHANNEL_ENABLED( inst, CHAN_W )) {
dst0[CHAN_W] = bld->base.one;
}
break;
case TGSI_OPCODE_MUL:
FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
src0 = emit_fetch( bld, inst, 0, chan_index );
src1 = emit_fetch( bld, inst, 1, chan_index );
dst0[chan_index] = lp_build_mul(&bld->base, src0, src1);
}
break;
case TGSI_OPCODE_ADD:
FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
src0 = emit_fetch( bld, inst, 0, chan_index );
src1 = emit_fetch( bld, inst, 1, chan_index );
dst0[chan_index] = lp_build_add(&bld->base, src0, src1);
}
break;
case TGSI_OPCODE_DP3:
/* TGSI_OPCODE_DOT3 */
tmp0 = emit_fetch( bld, inst, 0, CHAN_X );
tmp1 = emit_fetch( bld, inst, 1, CHAN_X );
tmp0 = lp_build_mul( &bld->base, tmp0, tmp1);
tmp1 = emit_fetch( bld, inst, 0, CHAN_Y );
tmp2 = emit_fetch( bld, inst, 1, CHAN_Y );
tmp1 = lp_build_mul( &bld->base, tmp1, tmp2);
tmp0 = lp_build_add( &bld->base, tmp0, tmp1);
tmp1 = emit_fetch( bld, inst, 0, CHAN_Z );
tmp2 = emit_fetch( bld, inst, 1, CHAN_Z );
tmp1 = lp_build_mul( &bld->base, tmp1, tmp2);
tmp0 = lp_build_add( &bld->base, tmp0, tmp1);
FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
dst0[chan_index] = tmp0;
}
break;
case TGSI_OPCODE_DP4:
/* TGSI_OPCODE_DOT4 */
tmp0 = emit_fetch( bld, inst, 0, CHAN_X );
tmp1 = emit_fetch( bld, inst, 1, CHAN_X );
tmp0 = lp_build_mul( &bld->base, tmp0, tmp1);
tmp1 = emit_fetch( bld, inst, 0, CHAN_Y );
tmp2 = emit_fetch( bld, inst, 1, CHAN_Y );
tmp1 = lp_build_mul( &bld->base, tmp1, tmp2);
tmp0 = lp_build_add( &bld->base, tmp0, tmp1);
tmp1 = emit_fetch( bld, inst, 0, CHAN_Z );
tmp2 = emit_fetch( bld, inst, 1, CHAN_Z );
tmp1 = lp_build_mul( &bld->base, tmp1, tmp2);
tmp0 = lp_build_add( &bld->base, tmp0, tmp1);
tmp1 = emit_fetch( bld, inst, 0, CHAN_W );
tmp2 = emit_fetch( bld, inst, 1, CHAN_W );
tmp1 = lp_build_mul( &bld->base, tmp1, tmp2);
tmp0 = lp_build_add( &bld->base, tmp0, tmp1);
FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
dst0[chan_index] = tmp0;
}
break;
case TGSI_OPCODE_DST:
IF_IS_DST0_CHANNEL_ENABLED( inst, CHAN_X ) {
dst0[CHAN_X] = bld->base.one;
}
IF_IS_DST0_CHANNEL_ENABLED( inst, CHAN_Y ) {
tmp0 = emit_fetch( bld, inst, 0, CHAN_Y );
tmp1 = emit_fetch( bld, inst, 1, CHAN_Y );
dst0[CHAN_Y] = lp_build_mul( &bld->base, tmp0, tmp1);
}
IF_IS_DST0_CHANNEL_ENABLED( inst, CHAN_Z ) {
dst0[CHAN_Z] = emit_fetch( bld, inst, 0, CHAN_Z );
}
IF_IS_DST0_CHANNEL_ENABLED( inst, CHAN_W ) {
dst0[CHAN_W] = emit_fetch( bld, inst, 1, CHAN_W );
}
break;
case TGSI_OPCODE_MIN:
FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
src0 = emit_fetch( bld, inst, 0, chan_index );
src1 = emit_fetch( bld, inst, 1, chan_index );
dst0[chan_index] = lp_build_min( &bld->base, src0, src1 );
}
break;
case TGSI_OPCODE_MAX:
FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
src0 = emit_fetch( bld, inst, 0, chan_index );
src1 = emit_fetch( bld, inst, 1, chan_index );
dst0[chan_index] = lp_build_max( &bld->base, src0, src1 );
}
break;
case TGSI_OPCODE_SLT:
/* TGSI_OPCODE_SETLT */
FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
src0 = emit_fetch( bld, inst, 0, chan_index );
src1 = emit_fetch( bld, inst, 1, chan_index );
tmp0 = lp_build_cmp( &bld->base, PIPE_FUNC_LESS, src0, src1 );
dst0[chan_index] = lp_build_select( &bld->base, tmp0, bld->base.one, bld->base.zero );
}
break;
case TGSI_OPCODE_SGE:
/* TGSI_OPCODE_SETGE */
FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
src0 = emit_fetch( bld, inst, 0, chan_index );
src1 = emit_fetch( bld, inst, 1, chan_index );
tmp0 = lp_build_cmp( &bld->base, PIPE_FUNC_GEQUAL, src0, src1 );
dst0[chan_index] = lp_build_select( &bld->base, tmp0, bld->base.one, bld->base.zero );
}
break;
case TGSI_OPCODE_MAD:
/* TGSI_OPCODE_MADD */
FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
tmp0 = emit_fetch( bld, inst, 0, chan_index );
tmp1 = emit_fetch( bld, inst, 1, chan_index );
tmp2 = emit_fetch( bld, inst, 2, chan_index );
tmp0 = lp_build_mul( &bld->base, tmp0, tmp1);
tmp0 = lp_build_add( &bld->base, tmp0, tmp2);
dst0[chan_index] = tmp0;
}
break;
case TGSI_OPCODE_SUB:
FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
tmp0 = emit_fetch( bld, inst, 0, chan_index );
tmp1 = emit_fetch( bld, inst, 1, chan_index );
dst0[chan_index] = lp_build_sub( &bld->base, tmp0, tmp1);
}
break;
case TGSI_OPCODE_LRP:
FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
src0 = emit_fetch( bld, inst, 0, chan_index );
src1 = emit_fetch( bld, inst, 1, chan_index );
src2 = emit_fetch( bld, inst, 2, chan_index );
tmp0 = lp_build_sub( &bld->base, src1, src2 );
tmp0 = lp_build_mul( &bld->base, src0, tmp0 );
dst0[chan_index] = lp_build_add( &bld->base, tmp0, src2 );
}
break;
case TGSI_OPCODE_CND:
FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
src0 = emit_fetch( bld, inst, 0, chan_index );
src1 = emit_fetch( bld, inst, 1, chan_index );
src2 = emit_fetch( bld, inst, 2, chan_index );
tmp1 = lp_build_const_vec(bld->base.type, 0.5);
tmp0 = lp_build_cmp( &bld->base, PIPE_FUNC_GREATER, src2, tmp1);
dst0[chan_index] = lp_build_select( &bld->base, tmp0, src0, src1 );
}
break;
case TGSI_OPCODE_DP2A:
tmp0 = emit_fetch( bld, inst, 0, CHAN_X ); /* xmm0 = src[0].x */
tmp1 = emit_fetch( bld, inst, 1, CHAN_X ); /* xmm1 = src[1].x */
tmp0 = lp_build_mul( &bld->base, tmp0, tmp1); /* xmm0 = xmm0 * xmm1 */
tmp1 = emit_fetch( bld, inst, 0, CHAN_Y ); /* xmm1 = src[0].y */
tmp2 = emit_fetch( bld, inst, 1, CHAN_Y ); /* xmm2 = src[1].y */
tmp1 = lp_build_mul( &bld->base, tmp1, tmp2); /* xmm1 = xmm1 * xmm2 */
tmp0 = lp_build_add( &bld->base, tmp0, tmp1); /* xmm0 = xmm0 + xmm1 */
tmp1 = emit_fetch( bld, inst, 2, CHAN_X ); /* xmm1 = src[2].x */
tmp0 = lp_build_add( &bld->base, tmp0, tmp1); /* xmm0 = xmm0 + xmm1 */
FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
dst0[chan_index] = tmp0; /* dest[ch] = xmm0 */
}
break;
case TGSI_OPCODE_FRC:
FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
src0 = emit_fetch( bld, inst, 0, chan_index );
tmp0 = lp_build_floor(&bld->base, src0);
tmp0 = lp_build_sub(&bld->base, src0, tmp0);
dst0[chan_index] = tmp0;
}
break;
case TGSI_OPCODE_CLAMP:
FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
tmp0 = emit_fetch( bld, inst, 0, chan_index );
src1 = emit_fetch( bld, inst, 1, chan_index );
src2 = emit_fetch( bld, inst, 2, chan_index );
tmp0 = lp_build_max(&bld->base, tmp0, src1);
tmp0 = lp_build_min(&bld->base, tmp0, src2);
dst0[chan_index] = tmp0;
}
break;
case TGSI_OPCODE_FLR:
FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
tmp0 = emit_fetch( bld, inst, 0, chan_index );
dst0[chan_index] = lp_build_floor(&bld->base, tmp0);
}
break;
case TGSI_OPCODE_ROUND:
FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
tmp0 = emit_fetch( bld, inst, 0, chan_index );
dst0[chan_index] = lp_build_round(&bld->base, tmp0);
}
break;
case TGSI_OPCODE_EX2: {
tmp0 = emit_fetch( bld, inst, 0, CHAN_X );
tmp0 = lp_build_exp2( &bld->base, tmp0);
FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
dst0[chan_index] = tmp0;
}
break;
}
case TGSI_OPCODE_LG2:
tmp0 = emit_fetch( bld, inst, 0, CHAN_X );
tmp0 = lp_build_log2( &bld->base, tmp0);
FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
dst0[chan_index] = tmp0;
}
break;
case TGSI_OPCODE_POW:
src0 = emit_fetch( bld, inst, 0, CHAN_X );
src1 = emit_fetch( bld, inst, 1, CHAN_X );
res = lp_build_pow( &bld->base, src0, src1 );
FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
dst0[chan_index] = res;
}
break;
case TGSI_OPCODE_XPD:
if( IS_DST0_CHANNEL_ENABLED( inst, CHAN_X ) ||
IS_DST0_CHANNEL_ENABLED( inst, CHAN_Y ) ) {
tmp1 = emit_fetch( bld, inst, 1, CHAN_Z );
tmp3 = emit_fetch( bld, inst, 0, CHAN_Z );
}
if( IS_DST0_CHANNEL_ENABLED( inst, CHAN_X ) ||
IS_DST0_CHANNEL_ENABLED( inst, CHAN_Z ) ) {
tmp0 = emit_fetch( bld, inst, 0, CHAN_Y );
tmp4 = emit_fetch( bld, inst, 1, CHAN_Y );
}
IF_IS_DST0_CHANNEL_ENABLED( inst, CHAN_X ) {
tmp2 = tmp0;
tmp2 = lp_build_mul( &bld->base, tmp2, tmp1);
tmp5 = tmp3;
tmp5 = lp_build_mul( &bld->base, tmp5, tmp4);
tmp2 = lp_build_sub( &bld->base, tmp2, tmp5);
dst0[CHAN_X] = tmp2;
}
if( IS_DST0_CHANNEL_ENABLED( inst, CHAN_Y ) ||
IS_DST0_CHANNEL_ENABLED( inst, CHAN_Z ) ) {
tmp2 = emit_fetch( bld, inst, 1, CHAN_X );
tmp5 = emit_fetch( bld, inst, 0, CHAN_X );
}
IF_IS_DST0_CHANNEL_ENABLED( inst, CHAN_Y ) {
tmp3 = lp_build_mul( &bld->base, tmp3, tmp2);
tmp1 = lp_build_mul( &bld->base, tmp1, tmp5);
tmp3 = lp_build_sub( &bld->base, tmp3, tmp1);
dst0[CHAN_Y] = tmp3;
}
IF_IS_DST0_CHANNEL_ENABLED( inst, CHAN_Z ) {
tmp5 = lp_build_mul( &bld->base, tmp5, tmp4);
tmp0 = lp_build_mul( &bld->base, tmp0, tmp2);
tmp5 = lp_build_sub( &bld->base, tmp5, tmp0);
dst0[CHAN_Z] = tmp5;
}
IF_IS_DST0_CHANNEL_ENABLED( inst, CHAN_W ) {
dst0[CHAN_W] = bld->base.one;
}
break;
case TGSI_OPCODE_ABS:
FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
tmp0 = emit_fetch( bld, inst, 0, chan_index );
dst0[chan_index] = lp_build_abs( &bld->base, tmp0 );
}
break;
case TGSI_OPCODE_RCC:
/* deprecated? */
assert(0);
return FALSE;
case TGSI_OPCODE_DPH:
tmp0 = emit_fetch( bld, inst, 0, CHAN_X );
tmp1 = emit_fetch( bld, inst, 1, CHAN_X );
tmp0 = lp_build_mul( &bld->base, tmp0, tmp1);
tmp1 = emit_fetch( bld, inst, 0, CHAN_Y );
tmp2 = emit_fetch( bld, inst, 1, CHAN_Y );
tmp1 = lp_build_mul( &bld->base, tmp1, tmp2);
tmp0 = lp_build_add( &bld->base, tmp0, tmp1);
tmp1 = emit_fetch( bld, inst, 0, CHAN_Z );
tmp2 = emit_fetch( bld, inst, 1, CHAN_Z );
tmp1 = lp_build_mul( &bld->base, tmp1, tmp2);
tmp0 = lp_build_add( &bld->base, tmp0, tmp1);
tmp1 = emit_fetch( bld, inst, 1, CHAN_W );
tmp0 = lp_build_add( &bld->base, tmp0, tmp1);
FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
dst0[chan_index] = tmp0;
}
break;
case TGSI_OPCODE_COS:
tmp0 = emit_fetch( bld, inst, 0, CHAN_X );
tmp0 = lp_build_cos( &bld->base, tmp0 );
FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
dst0[chan_index] = tmp0;
}
break;
case TGSI_OPCODE_DDX:
FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
emit_fetch_deriv( bld, inst, 0, chan_index, NULL, &dst0[chan_index], NULL);
}
break;
case TGSI_OPCODE_DDY:
FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
emit_fetch_deriv( bld, inst, 0, chan_index, NULL, NULL, &dst0[chan_index]);
}
break;
case TGSI_OPCODE_KILP:
/* predicated kill */
emit_kilp( bld, inst );
break;
case TGSI_OPCODE_KIL:
/* conditional kill */
emit_kil( bld, inst );
break;
case TGSI_OPCODE_PK2H:
return FALSE;
break;
case TGSI_OPCODE_PK2US:
return FALSE;
break;
case TGSI_OPCODE_PK4B:
return FALSE;
break;
case TGSI_OPCODE_PK4UB:
return FALSE;
break;
case TGSI_OPCODE_RFL:
return FALSE;
break;
case TGSI_OPCODE_SEQ:
FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
src0 = emit_fetch( bld, inst, 0, chan_index );
src1 = emit_fetch( bld, inst, 1, chan_index );
tmp0 = lp_build_cmp( &bld->base, PIPE_FUNC_EQUAL, src0, src1 );
dst0[chan_index] = lp_build_select( &bld->base, tmp0, bld->base.one, bld->base.zero );
}
break;
case TGSI_OPCODE_SFL:
FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
dst0[chan_index] = bld->base.zero;
}
break;
case TGSI_OPCODE_SGT:
FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
src0 = emit_fetch( bld, inst, 0, chan_index );
src1 = emit_fetch( bld, inst, 1, chan_index );
tmp0 = lp_build_cmp( &bld->base, PIPE_FUNC_GREATER, src0, src1 );
dst0[chan_index] = lp_build_select( &bld->base, tmp0, bld->base.one, bld->base.zero );
}
break;
case TGSI_OPCODE_SIN:
tmp0 = emit_fetch( bld, inst, 0, CHAN_X );
tmp0 = lp_build_sin( &bld->base, tmp0 );
FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
dst0[chan_index] = tmp0;
}
break;
case TGSI_OPCODE_SLE:
FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
src0 = emit_fetch( bld, inst, 0, chan_index );
src1 = emit_fetch( bld, inst, 1, chan_index );
tmp0 = lp_build_cmp( &bld->base, PIPE_FUNC_LEQUAL, src0, src1 );
dst0[chan_index] = lp_build_select( &bld->base, tmp0, bld->base.one, bld->base.zero );
}
break;
case TGSI_OPCODE_SNE:
FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
src0 = emit_fetch( bld, inst, 0, chan_index );
src1 = emit_fetch( bld, inst, 1, chan_index );
tmp0 = lp_build_cmp( &bld->base, PIPE_FUNC_NOTEQUAL, src0, src1 );
dst0[chan_index] = lp_build_select( &bld->base, tmp0, bld->base.one, bld->base.zero );
}
break;
case TGSI_OPCODE_STR:
FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
dst0[chan_index] = bld->base.one;
}
break;
case TGSI_OPCODE_TEX:
emit_tex( bld, inst, FALSE, FALSE, dst0 );
break;
case TGSI_OPCODE_TXD:
/* FIXME */
return FALSE;
break;
case TGSI_OPCODE_UP2H:
/* deprecated */
assert (0);
return FALSE;
break;
case TGSI_OPCODE_UP2US:
/* deprecated */
assert(0);
return FALSE;
break;
case TGSI_OPCODE_UP4B:
/* deprecated */
assert(0);
return FALSE;
break;
case TGSI_OPCODE_UP4UB:
/* deprecated */
assert(0);
return FALSE;
break;
case TGSI_OPCODE_X2D:
/* deprecated? */
assert(0);
return FALSE;
break;
case TGSI_OPCODE_ARA:
/* deprecated */
assert(0);
return FALSE;
break;
case TGSI_OPCODE_ARR:
FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
tmp0 = emit_fetch( bld, inst, 0, chan_index );
tmp0 = lp_build_round(&bld->base, tmp0);
dst0[chan_index] = tmp0;
}
break;
case TGSI_OPCODE_BRA:
/* deprecated */
assert(0);
return FALSE;
break;
case TGSI_OPCODE_CAL:
/* FIXME */
return FALSE;
break;
case TGSI_OPCODE_RET:
/* FIXME */
return FALSE;
break;
case TGSI_OPCODE_END:
break;
case TGSI_OPCODE_SSG:
/* TGSI_OPCODE_SGN */
FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
tmp0 = emit_fetch( bld, inst, 0, chan_index );
dst0[chan_index] = lp_build_sgn( &bld->base, tmp0 );
}
break;
case TGSI_OPCODE_CMP:
FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
src0 = emit_fetch( bld, inst, 0, chan_index );
src1 = emit_fetch( bld, inst, 1, chan_index );
src2 = emit_fetch( bld, inst, 2, chan_index );
tmp0 = lp_build_cmp( &bld->base, PIPE_FUNC_LESS, src0, bld->base.zero );
dst0[chan_index] = lp_build_select( &bld->base, tmp0, src1, src2);
}
break;
case TGSI_OPCODE_SCS:
IF_IS_DST0_CHANNEL_ENABLED( inst, CHAN_X ) {
tmp0 = emit_fetch( bld, inst, 0, CHAN_X );
dst0[CHAN_X] = lp_build_cos( &bld->base, tmp0 );
}
IF_IS_DST0_CHANNEL_ENABLED( inst, CHAN_Y ) {
tmp0 = emit_fetch( bld, inst, 0, CHAN_X );
dst0[CHAN_Y] = lp_build_sin( &bld->base, tmp0 );
}
IF_IS_DST0_CHANNEL_ENABLED( inst, CHAN_Z ) {
dst0[CHAN_Z] = bld->base.zero;
}
IF_IS_DST0_CHANNEL_ENABLED( inst, CHAN_W ) {
dst0[CHAN_W] = bld->base.one;
}
break;
case TGSI_OPCODE_TXB:
emit_tex( bld, inst, TRUE, FALSE, dst0 );
break;
case TGSI_OPCODE_NRM:
/* fall-through */
case TGSI_OPCODE_NRM4:
/* 3 or 4-component normalization */
{
uint dims = (inst->Instruction.Opcode == TGSI_OPCODE_NRM) ? 3 : 4;
if (IS_DST0_CHANNEL_ENABLED(inst, CHAN_X) ||
IS_DST0_CHANNEL_ENABLED(inst, CHAN_Y) ||
IS_DST0_CHANNEL_ENABLED(inst, CHAN_Z) ||
(IS_DST0_CHANNEL_ENABLED(inst, CHAN_W) && dims == 4)) {
/* NOTE: Cannot use xmm regs 2/3 here (see emit_rsqrt() above). */
/* xmm4 = src.x */
/* xmm0 = src.x * src.x */
tmp0 = emit_fetch(bld, inst, 0, CHAN_X);
if (IS_DST0_CHANNEL_ENABLED(inst, CHAN_X)) {
tmp4 = tmp0;
}
tmp0 = lp_build_mul( &bld->base, tmp0, tmp0);
/* xmm5 = src.y */
/* xmm0 = xmm0 + src.y * src.y */
tmp1 = emit_fetch(bld, inst, 0, CHAN_Y);
if (IS_DST0_CHANNEL_ENABLED(inst, CHAN_Y)) {
tmp5 = tmp1;
}
tmp1 = lp_build_mul( &bld->base, tmp1, tmp1);
tmp0 = lp_build_add( &bld->base, tmp0, tmp1);
/* xmm6 = src.z */
/* xmm0 = xmm0 + src.z * src.z */
tmp1 = emit_fetch(bld, inst, 0, CHAN_Z);
if (IS_DST0_CHANNEL_ENABLED(inst, CHAN_Z)) {
tmp6 = tmp1;
}
tmp1 = lp_build_mul( &bld->base, tmp1, tmp1);
tmp0 = lp_build_add( &bld->base, tmp0, tmp1);
if (dims == 4) {
/* xmm7 = src.w */
/* xmm0 = xmm0 + src.w * src.w */
tmp1 = emit_fetch(bld, inst, 0, CHAN_W);
if (IS_DST0_CHANNEL_ENABLED(inst, CHAN_W)) {
tmp7 = tmp1;
}
tmp1 = lp_build_mul( &bld->base, tmp1, tmp1);
tmp0 = lp_build_add( &bld->base, tmp0, tmp1);
}
/* xmm1 = 1 / sqrt(xmm0) */
tmp1 = lp_build_rsqrt( &bld->base, tmp0);
/* dst.x = xmm1 * src.x */
if (IS_DST0_CHANNEL_ENABLED(inst, CHAN_X)) {
dst0[CHAN_X] = lp_build_mul( &bld->base, tmp4, tmp1);
}
/* dst.y = xmm1 * src.y */
if (IS_DST0_CHANNEL_ENABLED(inst, CHAN_Y)) {
dst0[CHAN_Y] = lp_build_mul( &bld->base, tmp5, tmp1);
}
/* dst.z = xmm1 * src.z */
if (IS_DST0_CHANNEL_ENABLED(inst, CHAN_Z)) {
dst0[CHAN_Z] = lp_build_mul( &bld->base, tmp6, tmp1);
}
/* dst.w = xmm1 * src.w */
if (IS_DST0_CHANNEL_ENABLED(inst, CHAN_X) && dims == 4) {
dst0[CHAN_W] = lp_build_mul( &bld->base, tmp7, tmp1);
}
}
/* dst.w = 1.0 */
if (IS_DST0_CHANNEL_ENABLED(inst, CHAN_W) && dims == 3) {
dst0[CHAN_W] = bld->base.one;
}
}
break;
case TGSI_OPCODE_DIV:
/* deprecated */
assert( 0 );
return FALSE;
break;
case TGSI_OPCODE_DP2:
tmp0 = emit_fetch( bld, inst, 0, CHAN_X ); /* xmm0 = src[0].x */
tmp1 = emit_fetch( bld, inst, 1, CHAN_X ); /* xmm1 = src[1].x */
tmp0 = lp_build_mul( &bld->base, tmp0, tmp1); /* xmm0 = xmm0 * xmm1 */
tmp1 = emit_fetch( bld, inst, 0, CHAN_Y ); /* xmm1 = src[0].y */
tmp2 = emit_fetch( bld, inst, 1, CHAN_Y ); /* xmm2 = src[1].y */
tmp1 = lp_build_mul( &bld->base, tmp1, tmp2); /* xmm1 = xmm1 * xmm2 */
tmp0 = lp_build_add( &bld->base, tmp0, tmp1); /* xmm0 = xmm0 + xmm1 */
FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
dst0[chan_index] = tmp0; /* dest[ch] = xmm0 */
}
break;
case TGSI_OPCODE_TXL:
emit_tex( bld, inst, TRUE, FALSE, dst0 );
break;
case TGSI_OPCODE_TXP:
emit_tex( bld, inst, FALSE, TRUE, dst0 );
break;
case TGSI_OPCODE_BRK:
lp_exec_break(&bld->exec_mask);
break;
case TGSI_OPCODE_IF:
tmp0 = emit_fetch(bld, inst, 0, CHAN_X);
tmp0 = lp_build_cmp(&bld->base, PIPE_FUNC_NOTEQUAL,
tmp0, bld->base.zero);
lp_exec_mask_cond_push(&bld->exec_mask, tmp0);
break;
case TGSI_OPCODE_BGNLOOP:
lp_exec_bgnloop(&bld->exec_mask);
break;
case TGSI_OPCODE_ELSE:
lp_exec_mask_cond_invert(&bld->exec_mask);
break;
case TGSI_OPCODE_ENDIF:
lp_exec_mask_cond_pop(&bld->exec_mask);
break;
case TGSI_OPCODE_ENDLOOP:
lp_exec_endloop(&bld->exec_mask);
break;
case TGSI_OPCODE_PUSHA:
/* deprecated? */
assert(0);
return FALSE;
break;
case TGSI_OPCODE_POPA:
/* deprecated? */
assert(0);
return FALSE;
break;
case TGSI_OPCODE_CEIL:
FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
tmp0 = emit_fetch( bld, inst, 0, chan_index );
dst0[chan_index] = lp_build_ceil(&bld->base, tmp0);
}
break;
case TGSI_OPCODE_I2F:
/* deprecated? */
assert(0);
return FALSE;
break;
case TGSI_OPCODE_NOT:
/* deprecated? */
assert(0);
return FALSE;
break;
case TGSI_OPCODE_TRUNC:
FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
tmp0 = emit_fetch( bld, inst, 0, chan_index );
dst0[chan_index] = lp_build_trunc(&bld->base, tmp0);
}
break;
case TGSI_OPCODE_SHL:
/* deprecated? */
assert(0);
return FALSE;
break;
case TGSI_OPCODE_ISHR:
/* deprecated? */
assert(0);
return FALSE;
break;
case TGSI_OPCODE_AND:
/* deprecated? */
assert(0);
return FALSE;
break;
case TGSI_OPCODE_OR:
/* deprecated? */
assert(0);
return FALSE;
break;
case TGSI_OPCODE_MOD:
/* deprecated? */
assert(0);
return FALSE;
break;
case TGSI_OPCODE_XOR:
/* deprecated? */
assert(0);
return FALSE;
break;
case TGSI_OPCODE_SAD:
/* deprecated? */
assert(0);
return FALSE;
break;
case TGSI_OPCODE_TXF:
/* deprecated? */
assert(0);
return FALSE;
break;
case TGSI_OPCODE_TXQ:
/* deprecated? */
assert(0);
return FALSE;
break;
case TGSI_OPCODE_CONT:
lp_exec_continue(&bld->exec_mask);
break;
case TGSI_OPCODE_EMIT:
return FALSE;
break;
case TGSI_OPCODE_ENDPRIM:
return FALSE;
break;
case TGSI_OPCODE_NOP:
break;
default:
return FALSE;
}
if(info->num_dst) {
FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
emit_store( bld, inst, 0, chan_index, dst0[chan_index]);
}
}
return TRUE;
}
void
lp_build_tgsi_soa(LLVMBuilderRef builder,
const struct tgsi_token *tokens,
struct lp_type type,
struct lp_build_mask_context *mask,
LLVMValueRef consts_ptr,
const LLVMValueRef *pos,
const LLVMValueRef (*inputs)[NUM_CHANNELS],
LLVMValueRef (*outputs)[NUM_CHANNELS],
struct lp_build_sampler_soa *sampler,
struct tgsi_shader_info *info)
{
struct lp_build_tgsi_soa_context bld;
struct tgsi_parse_context parse;
uint num_immediates = 0;
unsigned i;
/* Setup build context */
memset(&bld, 0, sizeof bld);
lp_build_context_init(&bld.base, builder, type);
bld.mask = mask;
bld.pos = pos;
bld.inputs = inputs;
bld.outputs = outputs;
bld.consts_ptr = consts_ptr;
bld.sampler = sampler;
bld.has_indirect_addressing = info->opcode_count[TGSI_OPCODE_ARR] > 0 ||
info->opcode_count[TGSI_OPCODE_ARL] > 0;
lp_exec_mask_init(&bld.exec_mask, &bld.base);
tgsi_parse_init( &parse, tokens );
while( !tgsi_parse_end_of_tokens( &parse ) ) {
tgsi_parse_token( &parse );
switch( parse.FullToken.Token.Type ) {
case TGSI_TOKEN_TYPE_DECLARATION:
/* Inputs already interpolated */
emit_declaration( &bld, &parse.FullToken.FullDeclaration );
break;
case TGSI_TOKEN_TYPE_INSTRUCTION:
{
unsigned opcode = parse.FullToken.FullInstruction.Instruction.Opcode;
const struct tgsi_opcode_info *opcode_info = tgsi_get_opcode_info(opcode);
if (!emit_instruction( &bld, &parse.FullToken.FullInstruction, opcode_info ))
_debug_printf("warning: failed to translate tgsi opcode %s to LLVM\n",
opcode_info->mnemonic);
}
break;
case TGSI_TOKEN_TYPE_IMMEDIATE:
/* simply copy the immediate values into the next immediates[] slot */
{
const uint size = parse.FullToken.FullImmediate.Immediate.NrTokens - 1;
assert(size <= 4);
assert(num_immediates < LP_MAX_TGSI_IMMEDIATES);
for( i = 0; i < size; ++i )
bld.immediates[num_immediates][i] =
lp_build_const_vec(type, parse.FullToken.FullImmediate.u[i].Float);
for( i = size; i < 4; ++i )
bld.immediates[num_immediates][i] = bld.base.undef;
num_immediates++;
}
break;
case TGSI_TOKEN_TYPE_PROPERTY:
break;
default:
assert( 0 );
}
}
if (0) {
LLVMBasicBlockRef block = LLVMGetInsertBlock(builder);
LLVMValueRef function = LLVMGetBasicBlockParent(block);
debug_printf("11111111111111111111111111111 \n");
tgsi_dump(tokens, 0);
LLVMDumpValue(function);
debug_printf("2222222222222222222222222222 \n");
}
tgsi_parse_free( &parse );
}
|