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
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
|
/*
* Mesa 3-D graphics library
* Version: 6.3
*
* Copyright (C) 1999-2004 Brian Paul 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, sublicense,
* 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 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 NONINFRINGEMENT. IN NO EVENT SHALL
* BRIAN PAUL 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.
*/
/**
* Translate tgsi vertex programs to x86/x87/SSE/SSE2 machine code
* using the rtasm runtime assembler. Based on the old
* t_vb_arb_program_sse.c
*/
#include "util/u_memory.h"
#include "util/u_math.h"
#include "pipe/p_shader_tokens.h"
#include "pipe/p_debug.h"
#include "tgsi/tgsi_parse.h"
#include "tgsi/tgsi_util.h"
#include "tgsi/tgsi_exec.h"
#include "tgsi/tgsi_dump.h"
#include "draw_vs.h"
#include "draw_vs_aos.h"
#include "rtasm/rtasm_x86sse.h"
#ifdef PIPE_ARCH_X86
#define DISASSEM 0
#define FAST_MATH 1
static const char *files[] =
{
"NULL",
"CONST",
"IN",
"OUT",
"TEMP",
"SAMP",
"ADDR",
"IMM",
"INTERNAL",
};
static INLINE boolean eq( struct x86_reg a,
struct x86_reg b )
{
return (a.file == b.file &&
a.idx == b.idx &&
a.mod == b.mod &&
a.disp == b.disp);
}
struct x86_reg aos_get_x86( struct aos_compilation *cp,
unsigned which_reg, /* quick hack */
unsigned value )
{
struct x86_reg reg;
if (which_reg == 0)
reg = cp->temp_EBP;
else
reg = cp->tmp_EAX;
if (cp->x86_reg[which_reg] != value) {
unsigned offset;
switch (value) {
case X86_IMMEDIATES:
assert(which_reg == 0);
offset = Offset(struct aos_machine, immediates);
break;
case X86_CONSTANTS:
assert(which_reg == 1);
offset = Offset(struct aos_machine, constants);
break;
case X86_ATTRIBS:
assert(which_reg == 0);
offset = Offset(struct aos_machine, attrib);
break;
default:
assert(0);
offset = 0;
}
x86_mov(cp->func, reg,
x86_make_disp(cp->machine_EDX, offset));
cp->x86_reg[which_reg] = value;
}
return reg;
}
static struct x86_reg get_reg_ptr(struct aos_compilation *cp,
unsigned file,
unsigned idx )
{
struct x86_reg ptr = cp->machine_EDX;
switch (file) {
case TGSI_FILE_INPUT:
assert(idx < MAX_INPUTS);
return x86_make_disp(ptr, Offset(struct aos_machine, input[idx]));
case TGSI_FILE_OUTPUT:
return x86_make_disp(ptr, Offset(struct aos_machine, output[idx]));
case TGSI_FILE_TEMPORARY:
assert(idx < MAX_TEMPS);
return x86_make_disp(ptr, Offset(struct aos_machine, temp[idx]));
case AOS_FILE_INTERNAL:
assert(idx < MAX_INTERNALS);
return x86_make_disp(ptr, Offset(struct aos_machine, internal[idx]));
case TGSI_FILE_IMMEDIATE:
assert(idx < MAX_IMMEDIATES); /* just a sanity check */
return x86_make_disp(aos_get_x86(cp, 0, X86_IMMEDIATES), idx * 4 * sizeof(float));
case TGSI_FILE_CONSTANT:
assert(idx < MAX_CONSTANTS); /* just a sanity check */
return x86_make_disp(aos_get_x86(cp, 1, X86_CONSTANTS), idx * 4 * sizeof(float));
default:
ERROR(cp, "unknown reg file");
return x86_make_reg(0,0);
}
}
#define X87_CW_EXCEPTION_INV_OP (1<<0)
#define X87_CW_EXCEPTION_DENORM_OP (1<<1)
#define X87_CW_EXCEPTION_ZERO_DIVIDE (1<<2)
#define X87_CW_EXCEPTION_OVERFLOW (1<<3)
#define X87_CW_EXCEPTION_UNDERFLOW (1<<4)
#define X87_CW_EXCEPTION_PRECISION (1<<5)
#define X87_CW_PRECISION_SINGLE (0<<8)
#define X87_CW_PRECISION_RESERVED (1<<8)
#define X87_CW_PRECISION_DOUBLE (2<<8)
#define X87_CW_PRECISION_DOUBLE_EXT (3<<8)
#define X87_CW_PRECISION_MASK (3<<8)
#define X87_CW_ROUND_NEAREST (0<<10)
#define X87_CW_ROUND_DOWN (1<<10)
#define X87_CW_ROUND_UP (2<<10)
#define X87_CW_ROUND_ZERO (3<<10)
#define X87_CW_ROUND_MASK (3<<10)
#define X87_CW_INFINITY (1<<12)
static void spill( struct aos_compilation *cp, unsigned idx )
{
if (!cp->xmm[idx].dirty ||
(cp->xmm[idx].file != TGSI_FILE_INPUT && /* inputs are fetched into xmm & set dirty */
cp->xmm[idx].file != TGSI_FILE_OUTPUT &&
cp->xmm[idx].file != TGSI_FILE_TEMPORARY)) {
ERROR(cp, "invalid spill");
return;
}
else {
struct x86_reg oldval = get_reg_ptr(cp,
cp->xmm[idx].file,
cp->xmm[idx].idx);
if (0) debug_printf("\nspill %s[%d]",
files[cp->xmm[idx].file],
cp->xmm[idx].idx);
assert(cp->xmm[idx].dirty);
sse_movaps(cp->func, oldval, x86_make_reg(file_XMM, idx));
cp->xmm[idx].dirty = 0;
}
}
static struct x86_reg get_xmm_writable( struct aos_compilation *cp,
struct x86_reg reg )
{
if (reg.file != file_XMM ||
cp->xmm[reg.idx].file != TGSI_FILE_NULL)
{
struct x86_reg tmp = aos_get_xmm_reg(cp);
sse_movaps(cp->func, tmp, reg);
reg = tmp;
}
cp->xmm[reg.idx].last_used = cp->insn_counter;
return reg;
}
static struct x86_reg get_xmm( struct aos_compilation *cp,
struct x86_reg reg )
{
if (reg.file != file_XMM)
{
struct x86_reg tmp = aos_get_xmm_reg(cp);
sse_movaps(cp->func, tmp, reg);
reg = tmp;
}
cp->xmm[reg.idx].last_used = cp->insn_counter;
return reg;
}
/* Allocate an empty xmm register, either as a temporary or later to
* "adopt" as a shader reg.
*/
struct x86_reg aos_get_xmm_reg( struct aos_compilation *cp )
{
unsigned i;
unsigned oldest = 0;
boolean found = FALSE;
for (i = 0; i < 8; i++)
if (cp->xmm[i].last_used != cp->insn_counter &&
cp->xmm[i].file == TGSI_FILE_NULL) {
oldest = i;
found = TRUE;
}
if (!found) {
for (i = 0; i < 8; i++)
if (cp->xmm[i].last_used < cp->xmm[oldest].last_used)
oldest = i;
}
/* Need to write out the old value?
*/
if (cp->xmm[oldest].dirty)
spill(cp, oldest);
assert(cp->xmm[oldest].last_used != cp->insn_counter);
cp->xmm[oldest].file = TGSI_FILE_NULL;
cp->xmm[oldest].idx = 0;
cp->xmm[oldest].dirty = 0;
cp->xmm[oldest].last_used = cp->insn_counter;
return x86_make_reg(file_XMM, oldest);
}
void aos_release_xmm_reg( struct aos_compilation *cp,
unsigned idx )
{
cp->xmm[idx].file = TGSI_FILE_NULL;
cp->xmm[idx].idx = 0;
cp->xmm[idx].dirty = 0;
cp->xmm[idx].last_used = 0;
}
/* Mark an xmm reg as holding the current copy of a shader reg.
*/
void aos_adopt_xmm_reg( struct aos_compilation *cp,
struct x86_reg reg,
unsigned file,
unsigned idx,
unsigned dirty )
{
unsigned i;
if (reg.file != file_XMM) {
assert(0);
return;
}
/* If any xmm reg thinks it holds this shader reg, break the
* illusion.
*/
for (i = 0; i < 8; i++) {
if (cp->xmm[i].file == file &&
cp->xmm[i].idx == idx)
{
/* If an xmm reg is already holding this shader reg, take into account its
* dirty flag...
*/
dirty |= cp->xmm[i].dirty;
aos_release_xmm_reg(cp, i);
}
}
cp->xmm[reg.idx].file = file;
cp->xmm[reg.idx].idx = idx;
cp->xmm[reg.idx].dirty = dirty;
cp->xmm[reg.idx].last_used = cp->insn_counter;
}
/* Return a pointer to the in-memory copy of the reg, making sure it is uptodate.
*/
static struct x86_reg aos_get_shader_reg_ptr( struct aos_compilation *cp,
unsigned file,
unsigned idx )
{
unsigned i;
/* Ensure the in-memory copy of this reg is up-to-date
*/
for (i = 0; i < 8; i++) {
if (cp->xmm[i].file == file &&
cp->xmm[i].idx == idx &&
cp->xmm[i].dirty) {
spill(cp, i);
}
}
return get_reg_ptr( cp, file, idx );
}
/* As above, but return a pointer. Note - this pointer may alias
* those returned by get_arg_ptr().
*/
static struct x86_reg get_dst_ptr( struct aos_compilation *cp,
const struct tgsi_full_dst_register *dst )
{
unsigned file = dst->DstRegister.File;
unsigned idx = dst->DstRegister.Index;
unsigned i;
/* Ensure in-memory copy of this reg is up-to-date and invalidate
* any xmm copies.
*/
for (i = 0; i < 8; i++) {
if (cp->xmm[i].file == file &&
cp->xmm[i].idx == idx)
{
if (cp->xmm[i].dirty)
spill(cp, i);
aos_release_xmm_reg(cp, i);
}
}
return get_reg_ptr( cp, file, idx );
}
/* Return an XMM reg if the argument is resident, otherwise return a
* base+offset pointer to the saved value.
*/
struct x86_reg aos_get_shader_reg( struct aos_compilation *cp,
unsigned file,
unsigned idx )
{
unsigned i;
for (i = 0; i < 8; i++) {
if (cp->xmm[i].file == file &&
cp->xmm[i].idx == idx)
{
cp->xmm[i].last_used = cp->insn_counter;
return x86_make_reg(file_XMM, i);
}
}
/* If not found in the XMM register file, return an indirect
* reference to the in-memory copy:
*/
return get_reg_ptr( cp, file, idx );
}
static struct x86_reg aos_get_shader_reg_xmm( struct aos_compilation *cp,
unsigned file,
unsigned idx )
{
struct x86_reg reg = get_xmm( cp,
aos_get_shader_reg( cp, file, idx ) );
aos_adopt_xmm_reg( cp,
reg,
file,
idx,
FALSE );
return reg;
}
struct x86_reg aos_get_internal_xmm( struct aos_compilation *cp,
unsigned imm )
{
return aos_get_shader_reg_xmm( cp, AOS_FILE_INTERNAL, imm );
}
struct x86_reg aos_get_internal( struct aos_compilation *cp,
unsigned imm )
{
return aos_get_shader_reg( cp, AOS_FILE_INTERNAL, imm );
}
/* Emulate pshufd insn in regular SSE, if necessary:
*/
static void emit_pshufd( struct aos_compilation *cp,
struct x86_reg dst,
struct x86_reg arg0,
ubyte shuf )
{
if (cp->have_sse2) {
sse2_pshufd(cp->func, dst, arg0, shuf);
}
else {
if (!eq(dst, arg0))
sse_movaps(cp->func, dst, arg0);
sse_shufps(cp->func, dst, dst, shuf);
}
}
/* load masks (pack into negs??)
* pshufd - shuffle according to writemask
* and - result, mask
* nand - dest, mask
* or - dest, result
*/
static boolean mask_write( struct aos_compilation *cp,
struct x86_reg dst,
struct x86_reg result,
unsigned mask )
{
struct x86_reg imm_swz = aos_get_internal_xmm(cp, IMM_SWZ);
struct x86_reg tmp = aos_get_xmm_reg(cp);
emit_pshufd(cp, tmp, imm_swz,
SHUF((mask & 1) ? 2 : 3,
(mask & 2) ? 2 : 3,
(mask & 4) ? 2 : 3,
(mask & 8) ? 2 : 3));
sse_andps(cp->func, dst, tmp);
sse_andnps(cp->func, tmp, result);
sse_orps(cp->func, dst, tmp);
aos_release_xmm_reg(cp, tmp.idx);
return TRUE;
}
/* Helper for writemask:
*/
static boolean emit_shuf_copy2( struct aos_compilation *cp,
struct x86_reg dst,
struct x86_reg arg0,
struct x86_reg arg1,
ubyte shuf )
{
struct x86_reg tmp = aos_get_xmm_reg(cp);
emit_pshufd(cp, dst, arg1, shuf);
emit_pshufd(cp, tmp, arg0, shuf);
sse_shufps(cp->func, dst, tmp, SHUF(X, Y, Z, W));
emit_pshufd(cp, dst, dst, shuf);
aos_release_xmm_reg(cp, tmp.idx);
return TRUE;
}
#define SSE_SWIZZLE_NOOP ((0<<0) | (1<<2) | (2<<4) | (3<<6))
/* Locate a source register and perform any required (simple) swizzle.
*
* Just fail on complex swizzles at this point.
*/
static struct x86_reg fetch_src( struct aos_compilation *cp,
const struct tgsi_full_src_register *src )
{
struct x86_reg arg0 = aos_get_shader_reg(cp,
src->SrcRegister.File,
src->SrcRegister.Index);
unsigned i;
ubyte swz = 0;
unsigned negs = 0;
unsigned abs = 0;
for (i = 0; i < 4; i++) {
unsigned swizzle = tgsi_util_get_full_src_register_extswizzle( src, i );
unsigned neg = tgsi_util_get_full_src_register_sign_mode( src, i );
switch (swizzle) {
case TGSI_EXTSWIZZLE_ZERO:
case TGSI_EXTSWIZZLE_ONE:
ERROR(cp, "not supporting full swizzles yet in tgsi_aos_sse2");
break;
default:
swz |= (swizzle & 0x3) << (i * 2);
break;
}
switch (neg) {
case TGSI_UTIL_SIGN_TOGGLE:
negs |= (1<<i);
break;
case TGSI_UTIL_SIGN_KEEP:
break;
case TGSI_UTIL_SIGN_CLEAR:
abs |= (1<<i);
break;
default:
ERROR(cp, "unsupported sign-mode");
break;
}
}
if (swz != SSE_SWIZZLE_NOOP || negs != 0 || abs != 0) {
struct x86_reg dst = aos_get_xmm_reg(cp);
if (swz != SSE_SWIZZLE_NOOP)
emit_pshufd(cp, dst, arg0, swz);
else
sse_movaps(cp->func, dst, arg0);
if (negs && negs != 0xf) {
struct x86_reg imm_swz = aos_get_internal_xmm(cp, IMM_SWZ);
struct x86_reg tmp = aos_get_xmm_reg(cp);
/* Load 1,-1,0,0
* Use neg as arg to pshufd
* Multiply
*/
emit_pshufd(cp, tmp, imm_swz,
SHUF((negs & 1) ? 1 : 0,
(negs & 2) ? 1 : 0,
(negs & 4) ? 1 : 0,
(negs & 8) ? 1 : 0));
sse_mulps(cp->func, dst, tmp);
aos_release_xmm_reg(cp, tmp.idx);
}
else if (negs) {
struct x86_reg imm_negs = aos_get_internal_xmm(cp, IMM_NEGS);
sse_mulps(cp->func, dst, imm_negs);
}
if (abs && abs != 0xf) {
ERROR(cp, "unsupported partial abs");
}
else if (abs) {
struct x86_reg neg = aos_get_internal(cp, IMM_NEGS);
struct x86_reg tmp = aos_get_xmm_reg(cp);
sse_movaps(cp->func, tmp, dst);
sse_mulps(cp->func, tmp, neg);
sse_maxps(cp->func, dst, tmp);
aos_release_xmm_reg(cp, tmp.idx);
}
return dst;
}
return arg0;
}
static void x87_fld_src( struct aos_compilation *cp,
const struct tgsi_full_src_register *src,
unsigned channel )
{
struct x86_reg arg0 = aos_get_shader_reg_ptr(cp,
src->SrcRegister.File,
src->SrcRegister.Index);
unsigned swizzle = tgsi_util_get_full_src_register_extswizzle( src, channel );
unsigned neg = tgsi_util_get_full_src_register_sign_mode( src, channel );
switch (swizzle) {
case TGSI_EXTSWIZZLE_ZERO:
x87_fldz( cp->func );
break;
case TGSI_EXTSWIZZLE_ONE:
x87_fld1( cp->func );
break;
default:
x87_fld( cp->func, x86_make_disp(arg0, (swizzle & 3) * sizeof(float)) );
break;
}
switch (neg) {
case TGSI_UTIL_SIGN_TOGGLE:
/* Flip the sign:
*/
x87_fchs( cp->func );
break;
case TGSI_UTIL_SIGN_KEEP:
break;
case TGSI_UTIL_SIGN_CLEAR:
x87_fabs( cp->func );
break;
case TGSI_UTIL_SIGN_SET:
x87_fabs( cp->func );
x87_fchs( cp->func );
break;
default:
ERROR(cp, "unsupported sign-mode");
break;
}
}
/* Used to implement write masking. This and most of the other instructions
* here would be easier to implement if there had been a translation
* to a 2 argument format (dst/arg0, arg1) at the shader level before
* attempting to translate to x86/sse code.
*/
static void store_dest( struct aos_compilation *cp,
const struct tgsi_full_dst_register *reg,
struct x86_reg result )
{
struct x86_reg dst;
switch (reg->DstRegister.WriteMask) {
case 0:
return;
case TGSI_WRITEMASK_XYZW:
aos_adopt_xmm_reg(cp,
get_xmm_writable(cp, result),
reg->DstRegister.File,
reg->DstRegister.Index,
TRUE);
return;
default:
break;
}
dst = aos_get_shader_reg_xmm(cp,
reg->DstRegister.File,
reg->DstRegister.Index);
switch (reg->DstRegister.WriteMask) {
case TGSI_WRITEMASK_X:
sse_movss(cp->func, dst, get_xmm(cp, result));
break;
case TGSI_WRITEMASK_ZW:
sse_shufps(cp->func, dst, get_xmm(cp, result), SHUF(X, Y, Z, W));
break;
case TGSI_WRITEMASK_XY:
result = get_xmm_writable(cp, result);
sse_shufps(cp->func, result, dst, SHUF(X, Y, Z, W));
dst = result;
break;
case TGSI_WRITEMASK_YZW:
result = get_xmm_writable(cp, result);
sse_movss(cp->func, result, dst);
dst = result;
break;
default:
mask_write(cp, dst, result, reg->DstRegister.WriteMask);
break;
}
aos_adopt_xmm_reg(cp,
dst,
reg->DstRegister.File,
reg->DstRegister.Index,
TRUE);
}
static void inject_scalar( struct aos_compilation *cp,
struct x86_reg dst,
struct x86_reg result,
ubyte swizzle )
{
sse_shufps(cp->func, dst, dst, swizzle);
sse_movss(cp->func, dst, result);
sse_shufps(cp->func, dst, dst, swizzle);
}
static void store_scalar_dest( struct aos_compilation *cp,
const struct tgsi_full_dst_register *reg,
struct x86_reg result )
{
unsigned writemask = reg->DstRegister.WriteMask;
struct x86_reg dst;
if (writemask != TGSI_WRITEMASK_X &&
writemask != TGSI_WRITEMASK_Y &&
writemask != TGSI_WRITEMASK_Z &&
writemask != TGSI_WRITEMASK_W &&
writemask != 0)
{
result = get_xmm_writable(cp, result); /* already true, right? */
sse_shufps(cp->func, result, result, SHUF(X,X,X,X));
store_dest(cp, reg, result);
return;
}
result = get_xmm(cp, result);
dst = aos_get_shader_reg_xmm(cp,
reg->DstRegister.File,
reg->DstRegister.Index);
switch (reg->DstRegister.WriteMask) {
case TGSI_WRITEMASK_X:
sse_movss(cp->func, dst, result);
break;
case TGSI_WRITEMASK_Y:
inject_scalar(cp, dst, result, SHUF(Y, X, Z, W));
break;
case TGSI_WRITEMASK_Z:
inject_scalar(cp, dst, result, SHUF(Z, Y, X, W));
break;
case TGSI_WRITEMASK_W:
inject_scalar(cp, dst, result, SHUF(W, Y, Z, X));
break;
default:
break;
}
aos_adopt_xmm_reg(cp,
dst,
reg->DstRegister.File,
reg->DstRegister.Index,
TRUE);
}
static void x87_fst_or_nop( struct x86_function *func,
unsigned writemask,
unsigned channel,
struct x86_reg ptr )
{
assert(ptr.file == file_REG32);
if (writemask & (1<<channel))
x87_fst( func, x86_make_disp(ptr, channel * sizeof(float)) );
}
static void x87_fstp_or_pop( struct x86_function *func,
unsigned writemask,
unsigned channel,
struct x86_reg ptr )
{
assert(ptr.file == file_REG32);
if (writemask & (1<<channel))
x87_fstp( func, x86_make_disp(ptr, channel * sizeof(float)) );
else
x87_fstp( func, x86_make_reg( file_x87, 0 ));
}
/*
*/
static void x87_fstp_dest4( struct aos_compilation *cp,
const struct tgsi_full_dst_register *dst )
{
struct x86_reg ptr = get_dst_ptr(cp, dst);
unsigned writemask = dst->DstRegister.WriteMask;
x87_fst_or_nop(cp->func, writemask, 0, ptr);
x87_fst_or_nop(cp->func, writemask, 1, ptr);
x87_fst_or_nop(cp->func, writemask, 2, ptr);
x87_fstp_or_pop(cp->func, writemask, 3, ptr);
}
/* Save current x87 state and put it into single precision mode.
*/
static void save_fpu_state( struct aos_compilation *cp )
{
x87_fnstcw( cp->func, x86_make_disp(cp->machine_EDX,
Offset(struct aos_machine, fpu_restore)));
}
static void restore_fpu_state( struct aos_compilation *cp )
{
x87_fnclex(cp->func);
x87_fldcw( cp->func, x86_make_disp(cp->machine_EDX,
Offset(struct aos_machine, fpu_restore)));
}
static void set_fpu_round_neg_inf( struct aos_compilation *cp )
{
if (cp->fpucntl != FPU_RND_NEG) {
cp->fpucntl = FPU_RND_NEG;
x87_fnclex(cp->func);
x87_fldcw( cp->func, x86_make_disp(cp->machine_EDX,
Offset(struct aos_machine, fpu_rnd_neg_inf)));
}
}
static void set_fpu_round_nearest( struct aos_compilation *cp )
{
if (cp->fpucntl != FPU_RND_NEAREST) {
cp->fpucntl = FPU_RND_NEAREST;
x87_fnclex(cp->func);
x87_fldcw( cp->func, x86_make_disp(cp->machine_EDX,
Offset(struct aos_machine, fpu_rnd_nearest)));
}
}
static void x87_emit_ex2( struct aos_compilation *cp )
{
struct x86_reg st0 = x86_make_reg(file_x87, 0);
struct x86_reg st1 = x86_make_reg(file_x87, 1);
int stack = cp->func->x87_stack;
// set_fpu_round_neg_inf( cp );
x87_fld(cp->func, st0); /* a a */
x87_fprndint( cp->func ); /* int(a) a*/
x87_fsubr(cp->func, st1, st0); /* int(a) frc(a) */
x87_fxch(cp->func, st1); /* frc(a) int(a) */
x87_f2xm1(cp->func); /* (2^frc(a))-1 int(a) */
x87_fld1(cp->func); /* 1 (2^frc(a))-1 int(a) */
x87_faddp(cp->func, st1); /* 2^frac(a) int(a) */
x87_fscale(cp->func); /* (2^frac(a)*2^int(int(a))) int(a) */
/* 2^a int(a) */
x87_fstp(cp->func, st1); /* 2^a */
assert( stack == cp->func->x87_stack);
}
static void PIPE_CDECL print_reg( const char *msg,
const float *reg )
{
debug_printf("%s: %f %f %f %f\n", msg, reg[0], reg[1], reg[2], reg[3]);
}
static void emit_print( struct aos_compilation *cp,
const char *message, /* must point to a static string! */
unsigned file,
unsigned idx )
{
struct x86_reg ecx = x86_make_reg( file_REG32, reg_CX );
struct x86_reg arg = aos_get_shader_reg_ptr( cp, file, idx );
unsigned i;
/* There shouldn't be anything on the x87 stack. Can add this
* capacity later if need be.
*/
assert(cp->func->x87_stack == 0);
/* For absolute correctness, need to spill/invalidate all XMM regs
* too. We're obviously not concerned about performance on this
* debug path, so here goes:
*/
for (i = 0; i < 8; i++) {
if (cp->xmm[i].dirty)
spill(cp, i);
aos_release_xmm_reg(cp, i);
}
/* Push caller-save (ie scratch) regs.
*/
x86_cdecl_caller_push_regs( cp->func );
/* Push the arguments:
*/
x86_lea( cp->func, ecx, arg );
x86_push( cp->func, ecx );
x86_push_imm32( cp->func, (int)message );
/* Call the helper. Could call debug_printf directly, but
* print_reg is a nice place to put a breakpoint if need be.
*/
x86_mov_reg_imm( cp->func, ecx, (int)print_reg );
x86_call( cp->func, ecx );
x86_pop( cp->func, ecx );
x86_pop( cp->func, ecx );
/* Pop caller-save regs
*/
x86_cdecl_caller_pop_regs( cp->func );
/* Done...
*/
}
/**
* The traditional instructions. All operate on internal registers
* and ignore write masks and swizzling issues.
*/
static boolean emit_ABS( struct aos_compilation *cp, const struct tgsi_full_instruction *op )
{
struct x86_reg arg0 = fetch_src(cp, &op->FullSrcRegisters[0]);
struct x86_reg neg = aos_get_internal(cp, IMM_NEGS);
struct x86_reg tmp = aos_get_xmm_reg(cp);
sse_movaps(cp->func, tmp, arg0);
sse_mulps(cp->func, tmp, neg);
sse_maxps(cp->func, tmp, arg0);
store_dest(cp, &op->FullDstRegisters[0], tmp);
return TRUE;
}
static boolean emit_ADD( struct aos_compilation *cp, const struct tgsi_full_instruction *op )
{
struct x86_reg arg0 = fetch_src(cp, &op->FullSrcRegisters[0]);
struct x86_reg arg1 = fetch_src(cp, &op->FullSrcRegisters[1]);
struct x86_reg dst = get_xmm_writable(cp, arg0);
sse_addps(cp->func, dst, arg1);
store_dest(cp, &op->FullDstRegisters[0], dst);
return TRUE;
}
static boolean emit_COS( struct aos_compilation *cp, const struct tgsi_full_instruction *op )
{
x87_fld_src(cp, &op->FullSrcRegisters[0], 0);
x87_fcos(cp->func);
x87_fstp_dest4(cp, &op->FullDstRegisters[0]);
return TRUE;
}
/* The dotproduct instructions don't really do that well in sse:
* XXX: produces wrong results -- disabled.
*/
static boolean emit_DP3( struct aos_compilation *cp, const struct tgsi_full_instruction *op )
{
struct x86_reg arg0 = fetch_src(cp, &op->FullSrcRegisters[0]);
struct x86_reg arg1 = fetch_src(cp, &op->FullSrcRegisters[1]);
struct x86_reg tmp = aos_get_xmm_reg(cp);
struct x86_reg dst = get_xmm_writable(cp, arg0);
sse_mulps(cp->func, dst, arg1);
/* Now the hard bit: sum the first 3 values:
*/
sse_movhlps(cp->func, tmp, dst);
sse_addss(cp->func, dst, tmp); /* a*x+c*z, b*y, ?, ? */
emit_pshufd(cp, tmp, dst, SHUF(Y,X,W,Z));
sse_addss(cp->func, dst, tmp);
aos_release_xmm_reg(cp, tmp.idx);
store_scalar_dest(cp, &op->FullDstRegisters[0], dst);
return TRUE;
}
static boolean emit_DP4( struct aos_compilation *cp, const struct tgsi_full_instruction *op )
{
struct x86_reg arg0 = fetch_src(cp, &op->FullSrcRegisters[0]);
struct x86_reg arg1 = fetch_src(cp, &op->FullSrcRegisters[1]);
struct x86_reg tmp = aos_get_xmm_reg(cp);
struct x86_reg dst = get_xmm_writable(cp, arg0);
sse_mulps(cp->func, dst, arg1);
/* Now the hard bit: sum the values:
*/
sse_movhlps(cp->func, tmp, dst);
sse_addps(cp->func, dst, tmp); /* a*x+c*z, b*y+d*w, a*x+c*z, b*y+d*w */
emit_pshufd(cp, tmp, dst, SHUF(Y,X,W,Z));
sse_addss(cp->func, dst, tmp);
aos_release_xmm_reg(cp, tmp.idx);
store_scalar_dest(cp, &op->FullDstRegisters[0], dst);
return TRUE;
}
static boolean emit_DPH( struct aos_compilation *cp, const struct tgsi_full_instruction *op )
{
struct x86_reg arg0 = fetch_src(cp, &op->FullSrcRegisters[0]);
struct x86_reg arg1 = fetch_src(cp, &op->FullSrcRegisters[1]);
struct x86_reg tmp = aos_get_xmm_reg(cp);
struct x86_reg dst = get_xmm_writable(cp, arg0);
sse_mulps(cp->func, dst, arg1);
/* Now the hard bit: sum the values (from DP3):
*/
sse_movhlps(cp->func, tmp, dst);
sse_addss(cp->func, dst, tmp); /* a*x+c*z, b*y, ?, ? */
emit_pshufd(cp, tmp, dst, SHUF(Y,X,W,Z));
sse_addss(cp->func, dst, tmp);
emit_pshufd(cp, tmp, arg1, SHUF(W,W,W,W));
sse_addss(cp->func, dst, tmp);
aos_release_xmm_reg(cp, tmp.idx);
store_scalar_dest(cp, &op->FullDstRegisters[0], dst);
return TRUE;
}
static boolean emit_DST( struct aos_compilation *cp, const struct tgsi_full_instruction *op )
{
struct x86_reg arg0 = fetch_src(cp, &op->FullSrcRegisters[0]);
struct x86_reg arg1 = fetch_src(cp, &op->FullSrcRegisters[1]);
struct x86_reg dst = aos_get_xmm_reg(cp);
struct x86_reg tmp = aos_get_xmm_reg(cp);
struct x86_reg ones = aos_get_internal(cp, IMM_ONES);
/* dst[0] = 1.0 * 1.0F; */
/* dst[1] = arg0[1] * arg1[1]; */
/* dst[2] = arg0[2] * 1.0; */
/* dst[3] = 1.0 * arg1[3]; */
emit_shuf_copy2(cp, dst, arg0, ones, SHUF(X,W,Z,Y));
emit_shuf_copy2(cp, tmp, arg1, ones, SHUF(X,Z,Y,W));
sse_mulps(cp->func, dst, tmp);
aos_release_xmm_reg(cp, tmp.idx);
store_dest(cp, &op->FullDstRegisters[0], dst);
return TRUE;
}
static boolean emit_LG2( struct aos_compilation *cp, const struct tgsi_full_instruction *op )
{
x87_fld1(cp->func); /* 1 */
x87_fld_src(cp, &op->FullSrcRegisters[0], 0); /* a0 1 */
x87_fyl2x(cp->func); /* log2(a0) */
x87_fstp_dest4(cp, &op->FullDstRegisters[0]);
return TRUE;
}
static boolean emit_EX2( struct aos_compilation *cp, const struct tgsi_full_instruction *op )
{
x87_fld_src(cp, &op->FullSrcRegisters[0], 0);
x87_emit_ex2(cp);
x87_fstp_dest4(cp, &op->FullDstRegisters[0]);
return TRUE;
}
static boolean emit_FLR( struct aos_compilation *cp, const struct tgsi_full_instruction *op )
{
struct x86_reg dst = get_dst_ptr(cp, &op->FullDstRegisters[0]);
unsigned writemask = op->FullDstRegisters[0].DstRegister.WriteMask;
int i;
set_fpu_round_neg_inf( cp );
/* Load all sources first to avoid aliasing
*/
for (i = 3; i >= 0; i--) {
if (writemask & (1<<i)) {
x87_fld_src(cp, &op->FullSrcRegisters[0], i);
}
}
for (i = 0; i < 4; i++) {
if (writemask & (1<<i)) {
x87_fprndint( cp->func );
x87_fstp(cp->func, x86_make_disp(dst, i*4));
}
}
return TRUE;
}
static boolean emit_RND( struct aos_compilation *cp, const struct tgsi_full_instruction *op )
{
struct x86_reg dst = get_dst_ptr(cp, &op->FullDstRegisters[0]);
unsigned writemask = op->FullDstRegisters[0].DstRegister.WriteMask;
int i;
set_fpu_round_nearest( cp );
/* Load all sources first to avoid aliasing
*/
for (i = 3; i >= 0; i--) {
if (writemask & (1<<i)) {
x87_fld_src(cp, &op->FullSrcRegisters[0], i);
}
}
for (i = 0; i < 4; i++) {
if (writemask & (1<<i)) {
x87_fprndint( cp->func );
x87_fstp(cp->func, x86_make_disp(dst, i*4));
}
}
return TRUE;
}
static boolean emit_FRC( struct aos_compilation *cp, const struct tgsi_full_instruction *op )
{
struct x86_reg dst = get_dst_ptr(cp, &op->FullDstRegisters[0]);
struct x86_reg st0 = x86_make_reg(file_x87, 0);
struct x86_reg st1 = x86_make_reg(file_x87, 1);
unsigned writemask = op->FullDstRegisters[0].DstRegister.WriteMask;
int i;
set_fpu_round_neg_inf( cp );
/* suck all the source values onto the stack before writing out any
* dst, which may alias...
*/
for (i = 3; i >= 0; i--) {
if (writemask & (1<<i)) {
x87_fld_src(cp, &op->FullSrcRegisters[0], i);
}
}
for (i = 0; i < 4; i++) {
if (writemask & (1<<i)) {
x87_fld(cp->func, st0); /* a a */
x87_fprndint( cp->func ); /* flr(a) a */
x87_fsubp(cp->func, st1); /* frc(a) */
x87_fstp(cp->func, x86_make_disp(dst, i*4));
}
}
return TRUE;
}
static boolean emit_LIT( struct aos_compilation *cp, const struct tgsi_full_instruction *op )
{
struct x86_reg ecx = x86_make_reg( file_REG32, reg_CX );
unsigned writemask = op->FullDstRegisters[0].DstRegister.WriteMask;
unsigned lit_count = cp->lit_count++;
struct x86_reg result, arg0;
unsigned i;
#if 1
/* For absolute correctness, need to spill/invalidate all XMM regs
* too.
*/
for (i = 0; i < 8; i++) {
if (cp->xmm[i].dirty)
spill(cp, i);
aos_release_xmm_reg(cp, i);
}
#endif
if (writemask != TGSI_WRITEMASK_XYZW)
result = x86_make_disp(cp->machine_EDX, Offset(struct aos_machine, tmp[0]));
else
result = get_dst_ptr(cp, &op->FullDstRegisters[0]);
arg0 = fetch_src( cp, &op->FullSrcRegisters[0] );
if (arg0.file == file_XMM) {
struct x86_reg tmp = x86_make_disp(cp->machine_EDX,
Offset(struct aos_machine, tmp[1]));
sse_movaps( cp->func, tmp, arg0 );
arg0 = tmp;
}
/* Push caller-save (ie scratch) regs.
*/
x86_cdecl_caller_push_regs( cp->func );
/* Push the arguments:
*/
x86_push_imm32( cp->func, lit_count );
x86_lea( cp->func, ecx, arg0 );
x86_push( cp->func, ecx );
x86_lea( cp->func, ecx, result );
x86_push( cp->func, ecx );
x86_push( cp->func, cp->machine_EDX );
if (lit_count < MAX_LIT_INFO) {
x86_mov( cp->func, ecx, x86_make_disp( cp->machine_EDX,
Offset(struct aos_machine, lit_info) +
lit_count * sizeof(struct lit_info) +
Offset(struct lit_info, func)));
}
else {
x86_mov_reg_imm( cp->func, ecx, (int)aos_do_lit );
}
x86_call( cp->func, ecx );
x86_pop( cp->func, ecx ); /* fixme... */
x86_pop( cp->func, ecx );
x86_pop( cp->func, ecx );
x86_pop( cp->func, ecx );
x86_cdecl_caller_pop_regs( cp->func );
if (writemask != TGSI_WRITEMASK_XYZW) {
store_dest( cp,
&op->FullDstRegisters[0],
get_xmm_writable( cp, result ) );
}
return TRUE;
}
#if 0
static boolean emit_inline_LIT( struct aos_compilation *cp, const struct tgsi_full_instruction *op )
{
struct x86_reg dst = get_dst_ptr(cp, &op->FullDstRegisters[0]);
unsigned writemask = op->FullDstRegisters[0].DstRegister.WriteMask;
if (writemask & TGSI_WRITEMASK_YZ) {
struct x86_reg st1 = x86_make_reg(file_x87, 1);
struct x86_reg st2 = x86_make_reg(file_x87, 2);
/* a1' = a1 <= 0 ? 1 : a1;
*/
x87_fldz(cp->func); /* 1 0 */
#if 1
x87_fld1(cp->func); /* 1 0 */
#else
/* Correct but slow due to fp exceptions generated in fyl2x - fix me.
*/
x87_fldz(cp->func); /* 1 0 */
#endif
x87_fld_src(cp, &op->FullSrcRegisters[0], 1); /* a1 1 0 */
x87_fcomi(cp->func, st2); /* a1 1 0 */
x87_fcmovb(cp->func, st1); /* a1' 1 0 */
x87_fstp(cp->func, st1); /* a1' 0 */
x87_fstp(cp->func, st1); /* a1' */
x87_fld_src(cp, &op->FullSrcRegisters[0], 3); /* a3 a1' */
x87_fxch(cp->func, st1); /* a1' a3 */
/* Compute pow(a1, a3)
*/
x87_fyl2x(cp->func); /* a3*log2(a1) */
x87_emit_ex2( cp ); /* 2^(a3*log2(a1)) */
/* a0' = max2(a0, 0):
*/
x87_fldz(cp->func); /* 0 r2 */
x87_fld_src(cp, &op->FullSrcRegisters[0], 0); /* a0 0 r2 */
x87_fcomi(cp->func, st1);
x87_fcmovb(cp->func, st1); /* a0' 0 r2 */
x87_fst_or_nop(cp->func, writemask, 1, dst); /* result[1] = a0' */
x87_fcomi(cp->func, st1); /* a0' 0 r2 */
x87_fcmovnbe(cp->func, st2); /* r2' 0' r2 */
x87_fstp_or_pop(cp->func, writemask, 2, dst); /* 0 r2 */
x87_fpop(cp->func); /* r2 */
x87_fpop(cp->func);
}
if (writemask & TGSI_WRITEMASK_XW) {
x87_fld1(cp->func);
x87_fst_or_nop(cp->func, writemask, 0, dst);
x87_fstp_or_pop(cp->func, writemask, 3, dst);
}
return TRUE;
}
#endif
static boolean emit_MAX( struct aos_compilation *cp, const struct tgsi_full_instruction *op )
{
struct x86_reg arg0 = fetch_src(cp, &op->FullSrcRegisters[0]);
struct x86_reg arg1 = fetch_src(cp, &op->FullSrcRegisters[1]);
struct x86_reg dst = get_xmm_writable(cp, arg0);
sse_maxps(cp->func, dst, arg1);
store_dest(cp, &op->FullDstRegisters[0], dst);
return TRUE;
}
static boolean emit_MIN( struct aos_compilation *cp, const struct tgsi_full_instruction *op )
{
struct x86_reg arg0 = fetch_src(cp, &op->FullSrcRegisters[0]);
struct x86_reg arg1 = fetch_src(cp, &op->FullSrcRegisters[1]);
struct x86_reg dst = get_xmm_writable(cp, arg0);
sse_minps(cp->func, dst, arg1);
store_dest(cp, &op->FullDstRegisters[0], dst);
return TRUE;
}
static boolean emit_MOV( struct aos_compilation *cp, const struct tgsi_full_instruction *op )
{
struct x86_reg arg0 = fetch_src(cp, &op->FullSrcRegisters[0]);
struct x86_reg dst = get_xmm_writable(cp, arg0);
/* potentially nothing to do */
store_dest(cp, &op->FullDstRegisters[0], dst);
return TRUE;
}
static boolean emit_MUL( struct aos_compilation *cp, const struct tgsi_full_instruction *op )
{
struct x86_reg arg0 = fetch_src(cp, &op->FullSrcRegisters[0]);
struct x86_reg arg1 = fetch_src(cp, &op->FullSrcRegisters[1]);
struct x86_reg dst = get_xmm_writable(cp, arg0);
sse_mulps(cp->func, dst, arg1);
store_dest(cp, &op->FullDstRegisters[0], dst);
return TRUE;
}
static boolean emit_MAD( struct aos_compilation *cp, const struct tgsi_full_instruction *op )
{
struct x86_reg arg0 = fetch_src(cp, &op->FullSrcRegisters[0]);
struct x86_reg arg1 = fetch_src(cp, &op->FullSrcRegisters[1]);
struct x86_reg arg2 = fetch_src(cp, &op->FullSrcRegisters[2]);
/* If we can't clobber old contents of arg0, get a temporary & copy
* it there, then clobber it...
*/
arg0 = get_xmm_writable(cp, arg0);
sse_mulps(cp->func, arg0, arg1);
sse_addps(cp->func, arg0, arg2);
store_dest(cp, &op->FullDstRegisters[0], arg0);
return TRUE;
}
/* A wrapper for powf().
* Makes sure it is cdecl and operates on floats.
*/
static float PIPE_CDECL _powerf( float x, float y )
{
#if FAST_MATH
return util_fast_pow(x, y);
#else
return powf( x, y );
#endif
}
#if FAST_MATH
static float PIPE_CDECL _exp2(float x)
{
return util_fast_exp2(x);
}
#endif
/* Really not sufficient -- need to check for conditions that could
* generate inf/nan values, which will slow things down hugely.
*/
static boolean emit_POW( struct aos_compilation *cp, const struct tgsi_full_instruction *op )
{
#if 0
x87_fld_src(cp, &op->FullSrcRegisters[1], 0); /* a1.x */
x87_fld_src(cp, &op->FullSrcRegisters[0], 0); /* a0.x a1.x */
x87_fyl2x(cp->func); /* a1*log2(a0) */
x87_emit_ex2( cp ); /* 2^(a1*log2(a0)) */
x87_fstp_dest4(cp, &op->FullDstRegisters[0]);
#else
uint i;
/* For absolute correctness, need to spill/invalidate all XMM regs
* too.
*/
for (i = 0; i < 8; i++) {
if (cp->xmm[i].dirty)
spill(cp, i);
aos_release_xmm_reg(cp, i);
}
/* Push caller-save (ie scratch) regs.
*/
x86_cdecl_caller_push_regs( cp->func );
x86_lea( cp->func, cp->stack_ESP, x86_make_disp(cp->stack_ESP, -8) );
x87_fld_src( cp, &op->FullSrcRegisters[1], 0 );
x87_fstp( cp->func, x86_make_disp( cp->stack_ESP, 4 ) );
x87_fld_src( cp, &op->FullSrcRegisters[0], 0 );
x87_fstp( cp->func, x86_make_disp( cp->stack_ESP, 0 ) );
/* tmp_EAX has been pushed & will be restored below */
x86_mov_reg_imm( cp->func, cp->tmp_EAX, (unsigned long) _powerf );
x86_call( cp->func, cp->tmp_EAX );
x86_lea( cp->func, cp->stack_ESP, x86_make_disp(cp->stack_ESP, 8) );
x86_cdecl_caller_pop_regs( cp->func );
/* Note retval on x87 stack:
*/
cp->func->x87_stack++;
x87_fstp_dest4( cp, &op->FullDstRegisters[0] );
#endif
return TRUE;
}
#if FAST_MATH
static boolean emit_EXPBASE2( struct aos_compilation *cp, const struct tgsi_full_instruction *op )
{
uint i;
/* For absolute correctness, need to spill/invalidate all XMM regs
* too.
*/
for (i = 0; i < 8; i++) {
if (cp->xmm[i].dirty)
spill(cp, i);
aos_release_xmm_reg(cp, i);
}
/* Push caller-save (ie scratch) regs.
*/
x86_cdecl_caller_push_regs( cp->func );
x86_lea( cp->func, cp->stack_ESP, x86_make_disp(cp->stack_ESP, -4) );
x87_fld_src( cp, &op->FullSrcRegisters[0], 0 );
x87_fstp( cp->func, x86_make_disp( cp->stack_ESP, 0 ) );
/* tmp_EAX has been pushed & will be restored below */
x86_mov_reg_imm( cp->func, cp->tmp_EAX, (unsigned long) _exp2 );
x86_call( cp->func, cp->tmp_EAX );
x86_lea( cp->func, cp->stack_ESP, x86_make_disp(cp->stack_ESP, 4) );
x86_cdecl_caller_pop_regs( cp->func );
/* Note retval on x87 stack:
*/
cp->func->x87_stack++;
x87_fstp_dest4( cp, &op->FullDstRegisters[0] );
return TRUE;
}
#endif
static boolean emit_RCP( struct aos_compilation *cp, const struct tgsi_full_instruction *op )
{
struct x86_reg arg0 = fetch_src(cp, &op->FullSrcRegisters[0]);
struct x86_reg dst = aos_get_xmm_reg(cp);
if (cp->have_sse2) {
sse2_rcpss(cp->func, dst, arg0);
/* extend precision here...
*/
}
else {
struct x86_reg ones = aos_get_internal(cp, IMM_ONES);
sse_movss(cp->func, dst, ones);
sse_divss(cp->func, dst, arg0);
}
store_scalar_dest(cp, &op->FullDstRegisters[0], dst);
return TRUE;
}
/* Although rsqrtps() and rcpps() are low precision on some/all SSE
* implementations, it is possible to improve its precision at
* fairly low cost, using a newton/raphson step, as below:
*
* x1 = 2 * rcpps(a) - a * rcpps(a) * rcpps(a)
* x1 = 0.5 * rsqrtps(a) * [3.0 - (a * rsqrtps(a))* rsqrtps(a)]
* or:
* x1 = rsqrtps(a) * [1.5 - .5 * a * rsqrtps(a) * rsqrtps(a)]
*
*
* See: http://softwarecommunity.intel.com/articles/eng/1818.htm
*/
static boolean emit_RSQ( struct aos_compilation *cp, const struct tgsi_full_instruction *op )
{
if (0) {
struct x86_reg arg0 = fetch_src(cp, &op->FullSrcRegisters[0]);
struct x86_reg r = aos_get_xmm_reg(cp);
sse_rsqrtss(cp->func, r, arg0);
store_scalar_dest(cp, &op->FullDstRegisters[0], r);
return TRUE;
}
else {
struct x86_reg arg0 = fetch_src(cp, &op->FullSrcRegisters[0]);
struct x86_reg r = aos_get_xmm_reg(cp);
struct x86_reg neg_half = get_reg_ptr( cp, AOS_FILE_INTERNAL, IMM_RSQ );
struct x86_reg one_point_five = x86_make_disp( neg_half, 4 );
struct x86_reg src = get_xmm_writable( cp, arg0 );
sse_rsqrtss( cp->func, r, src ); /* rsqrtss(a) */
sse_mulss( cp->func, src, neg_half ); /* -.5 * a */
sse_mulss( cp->func, src, r ); /* -.5 * a * r */
sse_mulss( cp->func, src, r ); /* -.5 * a * r * r */
sse_addss( cp->func, src, one_point_five ); /* 1.5 - .5 * a * r * r */
sse_mulss( cp->func, r, src ); /* r * (1.5 - .5 * a * r * r) */
store_scalar_dest(cp, &op->FullDstRegisters[0], r);
return TRUE;
}
}
static boolean emit_SGE( struct aos_compilation *cp, const struct tgsi_full_instruction *op )
{
struct x86_reg arg0 = fetch_src(cp, &op->FullSrcRegisters[0]);
struct x86_reg arg1 = fetch_src(cp, &op->FullSrcRegisters[1]);
struct x86_reg ones = aos_get_internal(cp, IMM_ONES);
struct x86_reg dst = get_xmm_writable(cp, arg0);
sse_cmpps(cp->func, dst, arg1, cc_NotLessThan);
sse_andps(cp->func, dst, ones);
store_dest(cp, &op->FullDstRegisters[0], dst);
return TRUE;
}
static boolean emit_SIN( struct aos_compilation *cp, const struct tgsi_full_instruction *op )
{
x87_fld_src(cp, &op->FullSrcRegisters[0], 0);
x87_fsin(cp->func);
x87_fstp_dest4(cp, &op->FullDstRegisters[0]);
return TRUE;
}
static boolean emit_SLT( struct aos_compilation *cp, const struct tgsi_full_instruction *op )
{
struct x86_reg arg0 = fetch_src(cp, &op->FullSrcRegisters[0]);
struct x86_reg arg1 = fetch_src(cp, &op->FullSrcRegisters[1]);
struct x86_reg ones = aos_get_internal(cp, IMM_ONES);
struct x86_reg dst = get_xmm_writable(cp, arg0);
sse_cmpps(cp->func, dst, arg1, cc_LessThan);
sse_andps(cp->func, dst, ones);
store_dest(cp, &op->FullDstRegisters[0], dst);
return TRUE;
}
static boolean emit_SUB( struct aos_compilation *cp, const struct tgsi_full_instruction *op )
{
struct x86_reg arg0 = fetch_src(cp, &op->FullSrcRegisters[0]);
struct x86_reg arg1 = fetch_src(cp, &op->FullSrcRegisters[1]);
struct x86_reg dst = get_xmm_writable(cp, arg0);
sse_subps(cp->func, dst, arg1);
store_dest(cp, &op->FullDstRegisters[0], dst);
return TRUE;
}
static boolean emit_XPD( struct aos_compilation *cp, const struct tgsi_full_instruction *op )
{
struct x86_reg arg0 = fetch_src(cp, &op->FullSrcRegisters[0]);
struct x86_reg arg1 = fetch_src(cp, &op->FullSrcRegisters[1]);
struct x86_reg tmp0 = aos_get_xmm_reg(cp);
struct x86_reg tmp1 = aos_get_xmm_reg(cp);
emit_pshufd(cp, tmp1, arg1, SHUF(Y, Z, X, W));
sse_mulps(cp->func, tmp1, arg0);
emit_pshufd(cp, tmp0, arg0, SHUF(Y, Z, X, W));
sse_mulps(cp->func, tmp0, arg1);
sse_subps(cp->func, tmp1, tmp0);
sse_shufps(cp->func, tmp1, tmp1, SHUF(Y, Z, X, W));
/* dst[2] = arg0[0] * arg1[1] - arg0[1] * arg1[0]; */
/* dst[0] = arg0[1] * arg1[2] - arg0[2] * arg1[1]; */
/* dst[1] = arg0[2] * arg1[0] - arg0[0] * arg1[2]; */
/* dst[3] is undef */
aos_release_xmm_reg(cp, tmp0.idx);
store_dest(cp, &op->FullDstRegisters[0], tmp1);
return TRUE;
}
static boolean
emit_instruction( struct aos_compilation *cp,
struct tgsi_full_instruction *inst )
{
x87_assert_stack_empty(cp->func);
switch( inst->Instruction.Opcode ) {
case TGSI_OPCODE_MOV:
return emit_MOV( cp, inst );
case TGSI_OPCODE_LIT:
return emit_LIT(cp, inst);
case TGSI_OPCODE_RCP:
return emit_RCP(cp, inst);
case TGSI_OPCODE_RSQ:
return emit_RSQ(cp, inst);
case TGSI_OPCODE_EXP:
/*return emit_EXP(cp, inst);*/
return FALSE;
case TGSI_OPCODE_LOG:
/*return emit_LOG(cp, inst);*/
return FALSE;
case TGSI_OPCODE_MUL:
return emit_MUL(cp, inst);
case TGSI_OPCODE_ADD:
return emit_ADD(cp, inst);
case TGSI_OPCODE_DP3:
return emit_DP3(cp, inst);
case TGSI_OPCODE_DP4:
return emit_DP4(cp, inst);
case TGSI_OPCODE_DST:
return emit_DST(cp, inst);
case TGSI_OPCODE_MIN:
return emit_MIN(cp, inst);
case TGSI_OPCODE_MAX:
return emit_MAX(cp, inst);
case TGSI_OPCODE_SLT:
return emit_SLT(cp, inst);
case TGSI_OPCODE_SGE:
return emit_SGE(cp, inst);
case TGSI_OPCODE_MAD:
return emit_MAD(cp, inst);
case TGSI_OPCODE_SUB:
return emit_SUB(cp, inst);
case TGSI_OPCODE_LERP:
// return emit_LERP(cp, inst);
return FALSE;
case TGSI_OPCODE_FRAC:
return emit_FRC(cp, inst);
case TGSI_OPCODE_CLAMP:
// return emit_CLAMP(cp, inst);
return FALSE;
case TGSI_OPCODE_FLOOR:
return emit_FLR(cp, inst);
case TGSI_OPCODE_ROUND:
return emit_RND(cp, inst);
case TGSI_OPCODE_EXPBASE2:
#if FAST_MATH
return emit_EXPBASE2(cp, inst);
#elif 0
/* this seems to fail for "larger" exponents.
* See glean tvertProg1's EX2 test.
*/
return emit_EX2(cp, inst);
#else
return FALSE;
#endif
case TGSI_OPCODE_LOGBASE2:
return emit_LG2(cp, inst);
case TGSI_OPCODE_POWER:
return emit_POW(cp, inst);
case TGSI_OPCODE_CROSSPRODUCT:
return emit_XPD(cp, inst);
case TGSI_OPCODE_ABS:
return emit_ABS(cp, inst);
case TGSI_OPCODE_DPH:
return emit_DPH(cp, inst);
case TGSI_OPCODE_COS:
return emit_COS(cp, inst);
case TGSI_OPCODE_SIN:
return emit_SIN(cp, inst);
case TGSI_OPCODE_END:
return TRUE;
default:
return FALSE;
}
}
static boolean emit_viewport( struct aos_compilation *cp )
{
struct x86_reg pos = aos_get_shader_reg_xmm(cp,
TGSI_FILE_OUTPUT,
cp->vaos->draw->vs.position_output );
struct x86_reg scale = x86_make_disp(cp->machine_EDX,
Offset(struct aos_machine, scale));
struct x86_reg translate = x86_make_disp(cp->machine_EDX,
Offset(struct aos_machine, translate));
sse_mulps(cp->func, pos, scale);
sse_addps(cp->func, pos, translate);
aos_adopt_xmm_reg( cp,
pos,
TGSI_FILE_OUTPUT,
cp->vaos->draw->vs.position_output,
TRUE );
return TRUE;
}
/* This is useful to be able to see the results on softpipe. Doesn't
* do proper clipping, just assumes the backend can do it during
* rasterization -- for debug only...
*/
static boolean emit_rhw_viewport( struct aos_compilation *cp )
{
struct x86_reg tmp = aos_get_xmm_reg(cp);
struct x86_reg pos = aos_get_shader_reg_xmm(cp,
TGSI_FILE_OUTPUT,
cp->vaos->draw->vs.position_output);
struct x86_reg scale = x86_make_disp(cp->machine_EDX,
Offset(struct aos_machine, scale));
struct x86_reg translate = x86_make_disp(cp->machine_EDX,
Offset(struct aos_machine, translate));
emit_pshufd(cp, tmp, pos, SHUF(W, W, W, W));
sse2_rcpss(cp->func, tmp, tmp);
sse_shufps(cp->func, tmp, tmp, SHUF(X, X, X, X));
sse_mulps(cp->func, pos, scale);
sse_mulps(cp->func, pos, tmp);
sse_addps(cp->func, pos, translate);
/* Set pos[3] = w
*/
mask_write(cp, pos, tmp, TGSI_WRITEMASK_W);
aos_adopt_xmm_reg( cp,
pos,
TGSI_FILE_OUTPUT,
cp->vaos->draw->vs.position_output,
TRUE );
return TRUE;
}
#if 0
static boolean note_immediate( struct aos_compilation *cp,
struct tgsi_full_immediate *imm )
{
unsigned pos = cp->num_immediates++;
unsigned j;
for (j = 0; j < imm->Immediate.Size; j++) {
cp->vaos->machine->immediate[pos][j] = imm->u.ImmediateFloat32[j].Float;
}
return TRUE;
}
#endif
static void find_last_write_outputs( struct aos_compilation *cp )
{
struct tgsi_parse_context parse;
unsigned this_instruction = 0;
unsigned i;
tgsi_parse_init( &parse, cp->vaos->base.vs->state.tokens );
while (!tgsi_parse_end_of_tokens( &parse )) {
tgsi_parse_token( &parse );
if (parse.FullToken.Token.Type != TGSI_TOKEN_TYPE_INSTRUCTION)
continue;
for (i = 0; i < TGSI_FULL_MAX_DST_REGISTERS; i++) {
if (parse.FullToken.FullInstruction.FullDstRegisters[i].DstRegister.File ==
TGSI_FILE_OUTPUT)
{
unsigned idx = parse.FullToken.FullInstruction.FullDstRegisters[i].DstRegister.Index;
cp->output_last_write[idx] = this_instruction;
}
}
this_instruction++;
}
tgsi_parse_free( &parse );
}
#define ARG_MACHINE 1
#define ARG_START_ELTS 2
#define ARG_COUNT 3
#define ARG_OUTBUF 4
static boolean build_vertex_program( struct draw_vs_varient_aos_sse *varient,
boolean linear )
{
struct tgsi_parse_context parse;
struct aos_compilation cp;
unsigned fixup, label;
util_init_math();
tgsi_parse_init( &parse, varient->base.vs->state.tokens );
memset(&cp, 0, sizeof(cp));
cp.insn_counter = 1;
cp.vaos = varient;
cp.have_sse2 = 1;
cp.func = &varient->func[ linear ? 0 : 1 ];
cp.tmp_EAX = x86_make_reg(file_REG32, reg_AX);
cp.idx_EBX = x86_make_reg(file_REG32, reg_BX);
cp.outbuf_ECX = x86_make_reg(file_REG32, reg_CX);
cp.machine_EDX = x86_make_reg(file_REG32, reg_DX);
cp.count_ESI = x86_make_reg(file_REG32, reg_SI);
cp.temp_EBP = x86_make_reg(file_REG32, reg_BP);
cp.stack_ESP = x86_make_reg( file_REG32, reg_SP );
x86_init_func(cp.func);
find_last_write_outputs(&cp);
x86_push(cp.func, cp.idx_EBX);
x86_push(cp.func, cp.count_ESI);
x86_push(cp.func, cp.temp_EBP);
/* Load arguments into regs:
*/
x86_mov(cp.func, cp.machine_EDX, x86_fn_arg(cp.func, ARG_MACHINE));
x86_mov(cp.func, cp.idx_EBX, x86_fn_arg(cp.func, ARG_START_ELTS));
x86_mov(cp.func, cp.count_ESI, x86_fn_arg(cp.func, ARG_COUNT));
x86_mov(cp.func, cp.outbuf_ECX, x86_fn_arg(cp.func, ARG_OUTBUF));
/* Compare count to zero and possibly bail.
*/
x86_xor(cp.func, cp.tmp_EAX, cp.tmp_EAX);
x86_cmp(cp.func, cp.count_ESI, cp.tmp_EAX);
fixup = x86_jcc_forward(cp.func, cc_E);
save_fpu_state( &cp );
set_fpu_round_nearest( &cp );
/* Note address for loop jump
*/
label = x86_get_label(cp.func);
{
/* Fetch inputs... TODO: fetch lazily...
*/
if (!aos_fetch_inputs( &cp, linear ))
goto fail;
/* Emit the shader:
*/
while( !tgsi_parse_end_of_tokens( &parse ) && !cp.error )
{
tgsi_parse_token( &parse );
switch (parse.FullToken.Token.Type) {
case TGSI_TOKEN_TYPE_IMMEDIATE:
#if 0
if (!note_immediate( &cp, &parse.FullToken.FullImmediate ))
goto fail;
#endif
break;
case TGSI_TOKEN_TYPE_INSTRUCTION:
if (DISASSEM)
tgsi_dump_instruction( &parse.FullToken.FullInstruction, cp.insn_counter );
if (!emit_instruction( &cp, &parse.FullToken.FullInstruction ))
goto fail;
break;
}
x87_assert_stack_empty(cp.func);
cp.insn_counter++;
if (DISASSEM)
debug_printf("\n");
}
{
unsigned i;
for (i = 0; i < 8; i++) {
if (cp.xmm[i].file != TGSI_FILE_OUTPUT) {
cp.xmm[i].file = TGSI_FILE_NULL;
cp.xmm[i].dirty = 0;
}
}
}
if (cp.error)
goto fail;
if (cp.vaos->base.key.clip) {
/* not really handling clipping, just do the rhw so we can
* see the results...
*/
emit_rhw_viewport(&cp);
}
else if (cp.vaos->base.key.viewport) {
emit_viewport(&cp);
}
/* Emit output... TODO: do this eagerly after the last write to a
* given output.
*/
if (!aos_emit_outputs( &cp ))
goto fail;
/* Next vertex:
*/
x86_lea(cp.func,
cp.outbuf_ECX,
x86_make_disp(cp.outbuf_ECX,
cp.vaos->base.key.output_stride));
/* Incr index
*/
if (linear) {
x86_inc(cp.func, cp.idx_EBX);
}
else {
x86_lea(cp.func, cp.idx_EBX, x86_make_disp(cp.idx_EBX, 4));
}
}
/* decr count, loop if not zero
*/
x86_dec(cp.func, cp.count_ESI);
x86_jcc(cp.func, cc_NZ, label);
restore_fpu_state(&cp);
/* Land forward jump here:
*/
x86_fixup_fwd_jump(cp.func, fixup);
/* Exit mmx state?
*/
if (cp.func->need_emms)
mmx_emms(cp.func);
x86_pop(cp.func, cp.temp_EBP);
x86_pop(cp.func, cp.count_ESI);
x86_pop(cp.func, cp.idx_EBX);
x87_assert_stack_empty(cp.func);
x86_ret(cp.func);
tgsi_parse_free( &parse );
return !cp.error;
fail:
tgsi_parse_free( &parse );
return FALSE;
}
static void vaos_set_buffer( struct draw_vs_varient *varient,
unsigned buf,
const void *ptr,
unsigned stride )
{
struct draw_vs_varient_aos_sse *vaos = (struct draw_vs_varient_aos_sse *)varient;
unsigned i;
for (i = 0; i < vaos->base.key.nr_inputs; i++) {
if (vaos->base.key.element[i].in.buffer == buf) {
vaos->attrib[i].input_ptr = ((char *)ptr +
vaos->base.key.element[i].in.offset);
vaos->attrib[i].input_stride = stride;
}
}
}
static void PIPE_CDECL vaos_run_elts( struct draw_vs_varient *varient,
const unsigned *elts,
unsigned count,
void *output_buffer )
{
struct draw_vs_varient_aos_sse *vaos = (struct draw_vs_varient_aos_sse *)varient;
struct aos_machine *machine = vaos->draw->vs.aos_machine;
machine->internal[IMM_PSIZE][0] = vaos->draw->rasterizer->point_size;
machine->constants = vaos->draw->vs.aligned_constants;
machine->immediates = vaos->base.vs->immediates;
machine->attrib = vaos->attrib;
vaos->gen_run_elts( machine,
elts,
count,
output_buffer );
}
static void PIPE_CDECL vaos_run_linear( struct draw_vs_varient *varient,
unsigned start,
unsigned count,
void *output_buffer )
{
struct draw_vs_varient_aos_sse *vaos = (struct draw_vs_varient_aos_sse *)varient;
struct aos_machine *machine = vaos->draw->vs.aos_machine;
machine->internal[IMM_PSIZE][0] = vaos->draw->rasterizer->point_size;
machine->constants = vaos->draw->vs.aligned_constants;
machine->immediates = vaos->base.vs->immediates;
machine->attrib = vaos->attrib;
vaos->gen_run_linear( machine,
start,
count,
output_buffer );
/* Sanity spot checks to make sure we didn't trash our constants */
assert(machine->internal[IMM_ONES][0] == 1.0f);
assert(machine->internal[IMM_IDENTITY][0] == 0.0f);
assert(machine->internal[IMM_NEGS][0] == -1.0f);
}
static void vaos_destroy( struct draw_vs_varient *varient )
{
struct draw_vs_varient_aos_sse *vaos = (struct draw_vs_varient_aos_sse *)varient;
FREE( vaos->attrib );
x86_release_func( &vaos->func[0] );
x86_release_func( &vaos->func[1] );
FREE(vaos);
}
static struct draw_vs_varient *varient_aos_sse( struct draw_vertex_shader *vs,
const struct draw_vs_varient_key *key )
{
struct draw_vs_varient_aos_sse *vaos = CALLOC_STRUCT(draw_vs_varient_aos_sse);
if (!vaos)
goto fail;
vaos->base.key = *key;
vaos->base.vs = vs;
vaos->base.set_input = vaos_set_buffer;
vaos->base.destroy = vaos_destroy;
vaos->base.run_linear = vaos_run_linear;
vaos->base.run_elts = vaos_run_elts;
vaos->draw = vs->draw;
vaos->attrib = MALLOC( key->nr_inputs * sizeof(vaos->attrib[0]) );
if (!vaos->attrib)
goto fail;
#if 0
tgsi_dump(vs->state.tokens, 0);
#endif
if (!build_vertex_program( vaos, TRUE ))
goto fail;
if (!build_vertex_program( vaos, FALSE ))
goto fail;
vaos->gen_run_linear = (vaos_run_linear_func)x86_get_func(&vaos->func[0]);
if (!vaos->gen_run_linear)
goto fail;
vaos->gen_run_elts = (vaos_run_elts_func)x86_get_func(&vaos->func[1]);
if (!vaos->gen_run_elts)
goto fail;
return &vaos->base;
fail:
if (vaos && vaos->attrib)
FREE(vaos->attrib);
if (vaos)
x86_release_func( &vaos->func[0] );
if (vaos)
x86_release_func( &vaos->func[1] );
FREE(vaos);
return NULL;
}
struct draw_vs_varient *draw_vs_varient_aos_sse( struct draw_vertex_shader *vs,
const struct draw_vs_varient_key *key )
{
struct draw_vs_varient *varient = varient_aos_sse( vs, key );
if (varient == NULL) {
varient = draw_vs_varient_generic( vs, key );
}
return varient;
}
#endif /* PIPE_ARCH_X86 */
|