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
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
|
/*
* Copyright 2008 Ben Skeggs
*
* 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
* THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "pipe/p_context.h"
#include "pipe/p_defines.h"
#include "pipe/p_state.h"
#include "pipe/p_inlines.h"
#include "pipe/p_shader_tokens.h"
#include "tgsi/tgsi_parse.h"
#include "tgsi/tgsi_util.h"
#include "nv50_context.h"
#define NV50_SU_MAX_TEMP 64
//#define NV50_PROGRAM_DUMP
/* ARL - gallium craps itself on progs/vp/arl.txt
*
* MSB - Like MAD, but MUL+SUB
* - Fuck it off, introduce a way to negate args for ops that
* support it.
*
* Look into inlining IMMD for ops other than MOV (make it general?)
* - Maybe even relax restrictions a bit, can't do P_RESULT + P_IMMD,
* but can emit to P_TEMP first - then MOV later. NVIDIA does this
*
* In ops such as ADD it's possible to construct a bad opcode in the !is_long()
* case, if the emit_src() causes the inst to suddenly become long.
*
* Verify half-insns work where expected - and force disable them where they
* don't work - MUL has it forcibly disabled atm as it fixes POW..
*
* FUCK! watch dst==src vectors, can overwrite components that are needed.
* ie. SUB R0, R0.yzxw, R0
*
* Things to check with renouveau:
* FP attr/result assignment - how?
* attrib
* - 0x16bc maps vp output onto fp hpos
* - 0x16c0 maps vp output onto fp col0
* result
* - colr always 0-3
* - depr always 4
* 0x16bc->0x16e8 --> some binding between vp/fp regs
* 0x16b8 --> VP output count
*
* 0x1298 --> "MOV rcol.x, fcol.y" "MOV depr, fcol.y" = 0x00000005
* "MOV rcol.x, fcol.y" = 0x00000004
* 0x19a8 --> as above but 0x00000100 and 0x00000000
* - 0x00100000 used when KIL used
* 0x196c --> as above but 0x00000011 and 0x00000000
*
* 0x1988 --> 0xXXNNNNNN
* - XX == FP high something
*/
struct nv50_reg {
enum {
P_TEMP,
P_ATTR,
P_RESULT,
P_CONST,
P_IMMD
} type;
int index;
int hw;
int neg;
int rhw; /* result hw for FP outputs, or interpolant index */
int acc; /* instruction where this reg is last read (first insn == 1) */
};
/* arbitrary limits */
#define MAX_IF_DEPTH 4
#define MAX_LOOP_DEPTH 4
struct nv50_pc {
struct nv50_program *p;
/* hw resources */
struct nv50_reg *r_temp[NV50_SU_MAX_TEMP];
/* tgsi resources */
struct nv50_reg *temp;
int temp_nr;
struct nv50_reg *attr;
int attr_nr;
struct nv50_reg *result;
int result_nr;
struct nv50_reg *param;
int param_nr;
struct nv50_reg *immd;
float *immd_buf;
int immd_nr;
struct nv50_reg *temp_temp[16];
unsigned temp_temp_nr;
/* broadcast and destination replacement regs */
struct nv50_reg *r_brdc;
struct nv50_reg *r_dst[4];
unsigned interp_mode[32];
/* perspective interpolation registers */
struct nv50_reg *iv_p;
struct nv50_reg *iv_c;
struct nv50_program_exec *if_cond;
struct nv50_program_exec *if_insn[MAX_IF_DEPTH];
struct nv50_program_exec *br_join[MAX_IF_DEPTH];
struct nv50_program_exec *br_loop[MAX_LOOP_DEPTH]; /* for BRK branch */
int if_lvl, loop_lvl;
unsigned loop_pos[MAX_LOOP_DEPTH];
/* current instruction and total number of insns */
unsigned insn_cur;
unsigned insn_nr;
boolean allow32;
};
static INLINE void
ctor_reg(struct nv50_reg *reg, unsigned type, int index, int hw)
{
reg->type = type;
reg->index = index;
reg->hw = hw;
reg->neg = 0;
reg->rhw = -1;
reg->acc = 0;
}
static INLINE unsigned
popcnt4(uint32_t val)
{
static const unsigned cnt[16]
= { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 };
return cnt[val & 0xf];
}
static void
alloc_reg(struct nv50_pc *pc, struct nv50_reg *reg)
{
int i = 0;
if (reg->type == P_RESULT) {
if (pc->p->cfg.high_result < (reg->hw + 1))
pc->p->cfg.high_result = reg->hw + 1;
}
if (reg->type != P_TEMP)
return;
if (reg->hw >= 0) {
/*XXX: do this here too to catch FP temp-as-attr usage..
* not clean, but works */
if (pc->p->cfg.high_temp < (reg->hw + 1))
pc->p->cfg.high_temp = reg->hw + 1;
return;
}
if (reg->rhw != -1) {
/* try to allocate temporary with index rhw first */
if (!(pc->r_temp[reg->rhw])) {
pc->r_temp[reg->rhw] = reg;
reg->hw = reg->rhw;
if (pc->p->cfg.high_temp < (reg->rhw + 1))
pc->p->cfg.high_temp = reg->rhw + 1;
return;
}
/* make sure we don't get things like $r0 needs to go
* in $r1 and $r1 in $r0
*/
i = pc->result_nr * 4;
}
for (; i < NV50_SU_MAX_TEMP; i++) {
if (!(pc->r_temp[i])) {
pc->r_temp[i] = reg;
reg->hw = i;
if (pc->p->cfg.high_temp < (i + 1))
pc->p->cfg.high_temp = i + 1;
return;
}
}
assert(0);
}
/* XXX: For shaders that aren't executed linearly (e.g. shaders that
* contain loops), we need to assign all hw regs to TGSI TEMPs early,
* lest we risk temp_temps overwriting regs alloc'd "later".
*/
static struct nv50_reg *
alloc_temp(struct nv50_pc *pc, struct nv50_reg *dst)
{
struct nv50_reg *r;
int i;
if (dst && dst->type == P_TEMP && dst->hw == -1)
return dst;
for (i = 0; i < NV50_SU_MAX_TEMP; i++) {
if (!pc->r_temp[i]) {
r = MALLOC_STRUCT(nv50_reg);
ctor_reg(r, P_TEMP, -1, i);
pc->r_temp[i] = r;
return r;
}
}
assert(0);
return NULL;
}
/* Assign the hw of the discarded temporary register src
* to the tgsi register dst and free src.
*/
static void
assimilate_temp(struct nv50_pc *pc, struct nv50_reg *dst, struct nv50_reg *src)
{
assert(src->index == -1 && src->hw != -1);
if (dst->hw != -1)
pc->r_temp[dst->hw] = NULL;
pc->r_temp[src->hw] = dst;
dst->hw = src->hw;
FREE(src);
}
/* release the hardware resource held by r */
static void
release_hw(struct nv50_pc *pc, struct nv50_reg *r)
{
assert(r->type == P_TEMP);
if (r->hw == -1)
return;
assert(pc->r_temp[r->hw] == r);
pc->r_temp[r->hw] = NULL;
r->acc = 0;
if (r->index == -1)
FREE(r);
}
static void
free_temp(struct nv50_pc *pc, struct nv50_reg *r)
{
if (r->index == -1) {
unsigned hw = r->hw;
FREE(pc->r_temp[hw]);
pc->r_temp[hw] = NULL;
}
}
static int
alloc_temp4(struct nv50_pc *pc, struct nv50_reg *dst[4], int idx)
{
int i;
if ((idx + 4) >= NV50_SU_MAX_TEMP)
return 1;
if (pc->r_temp[idx] || pc->r_temp[idx + 1] ||
pc->r_temp[idx + 2] || pc->r_temp[idx + 3])
return alloc_temp4(pc, dst, idx + 4);
for (i = 0; i < 4; i++) {
dst[i] = MALLOC_STRUCT(nv50_reg);
ctor_reg(dst[i], P_TEMP, -1, idx + i);
pc->r_temp[idx + i] = dst[i];
}
return 0;
}
static void
free_temp4(struct nv50_pc *pc, struct nv50_reg *reg[4])
{
int i;
for (i = 0; i < 4; i++)
free_temp(pc, reg[i]);
}
static struct nv50_reg *
temp_temp(struct nv50_pc *pc)
{
if (pc->temp_temp_nr >= 16)
assert(0);
pc->temp_temp[pc->temp_temp_nr] = alloc_temp(pc, NULL);
return pc->temp_temp[pc->temp_temp_nr++];
}
static void
kill_temp_temp(struct nv50_pc *pc)
{
int i;
for (i = 0; i < pc->temp_temp_nr; i++)
free_temp(pc, pc->temp_temp[i]);
pc->temp_temp_nr = 0;
}
static int
ctor_immd(struct nv50_pc *pc, float x, float y, float z, float w)
{
pc->immd_buf = REALLOC(pc->immd_buf, (pc->immd_nr * 4 * sizeof(float)),
(pc->immd_nr + 1) * 4 * sizeof(float));
pc->immd_buf[(pc->immd_nr * 4) + 0] = x;
pc->immd_buf[(pc->immd_nr * 4) + 1] = y;
pc->immd_buf[(pc->immd_nr * 4) + 2] = z;
pc->immd_buf[(pc->immd_nr * 4) + 3] = w;
return pc->immd_nr++;
}
static struct nv50_reg *
alloc_immd(struct nv50_pc *pc, float f)
{
struct nv50_reg *r = MALLOC_STRUCT(nv50_reg);
unsigned hw;
for (hw = 0; hw < pc->immd_nr * 4; hw++)
if (pc->immd_buf[hw] == f)
break;
if (hw == pc->immd_nr * 4)
hw = ctor_immd(pc, f, -f, 0.5 * f, 0) * 4;
ctor_reg(r, P_IMMD, -1, hw);
return r;
}
static struct nv50_program_exec *
exec(struct nv50_pc *pc)
{
struct nv50_program_exec *e = CALLOC_STRUCT(nv50_program_exec);
e->param.index = -1;
return e;
}
static void
emit(struct nv50_pc *pc, struct nv50_program_exec *e)
{
struct nv50_program *p = pc->p;
if (p->exec_tail)
p->exec_tail->next = e;
if (!p->exec_head)
p->exec_head = e;
p->exec_tail = e;
p->exec_size += (e->inst[0] & 1) ? 2 : 1;
}
static INLINE void set_long(struct nv50_pc *, struct nv50_program_exec *);
static boolean
is_long(struct nv50_program_exec *e)
{
if (e->inst[0] & 1)
return TRUE;
return FALSE;
}
static boolean
is_immd(struct nv50_program_exec *e)
{
if (is_long(e) && (e->inst[1] & 3) == 3)
return TRUE;
return FALSE;
}
static INLINE void
set_pred(struct nv50_pc *pc, unsigned pred, unsigned idx,
struct nv50_program_exec *e)
{
set_long(pc, e);
e->inst[1] &= ~((0x1f << 7) | (0x3 << 12));
e->inst[1] |= (pred << 7) | (idx << 12);
}
static INLINE void
set_pred_wr(struct nv50_pc *pc, unsigned on, unsigned idx,
struct nv50_program_exec *e)
{
set_long(pc, e);
e->inst[1] &= ~((0x3 << 4) | (1 << 6));
e->inst[1] |= (idx << 4) | (on << 6);
}
static INLINE void
set_long(struct nv50_pc *pc, struct nv50_program_exec *e)
{
if (is_long(e))
return;
e->inst[0] |= 1;
set_pred(pc, 0xf, 0, e);
set_pred_wr(pc, 0, 0, e);
}
static INLINE void
set_dst(struct nv50_pc *pc, struct nv50_reg *dst, struct nv50_program_exec *e)
{
if (dst->type == P_RESULT) {
set_long(pc, e);
e->inst[1] |= 0x00000008;
}
alloc_reg(pc, dst);
e->inst[0] |= (dst->hw << 2);
}
static INLINE void
set_immd(struct nv50_pc *pc, struct nv50_reg *imm, struct nv50_program_exec *e)
{
float f = pc->immd_buf[imm->hw];
unsigned val = fui(imm->neg ? -f : f);
set_long(pc, e);
/*XXX: can't be predicated - bits overlap.. catch cases where both
* are required and avoid them. */
set_pred(pc, 0, 0, e);
set_pred_wr(pc, 0, 0, e);
e->inst[1] |= 0x00000002 | 0x00000001;
e->inst[0] |= (val & 0x3f) << 16;
e->inst[1] |= (val >> 6) << 2;
}
#define INTERP_LINEAR 0
#define INTERP_FLAT 1
#define INTERP_PERSPECTIVE 2
#define INTERP_CENTROID 4
/* interpolant index has been stored in dst->rhw */
static void
emit_interp(struct nv50_pc *pc, struct nv50_reg *dst, struct nv50_reg *iv,
unsigned mode)
{
assert(dst->rhw != -1);
struct nv50_program_exec *e = exec(pc);
e->inst[0] |= 0x80000000;
set_dst(pc, dst, e);
e->inst[0] |= (dst->rhw << 16);
if (mode & INTERP_FLAT) {
e->inst[0] |= (1 << 8);
} else {
if (mode & INTERP_PERSPECTIVE) {
e->inst[0] |= (1 << 25);
alloc_reg(pc, iv);
e->inst[0] |= (iv->hw << 9);
}
if (mode & INTERP_CENTROID)
e->inst[0] |= (1 << 24);
}
emit(pc, e);
}
static void
set_data(struct nv50_pc *pc, struct nv50_reg *src, unsigned m, unsigned s,
struct nv50_program_exec *e)
{
set_long(pc, e);
e->param.index = src->hw;
e->param.shift = s;
e->param.mask = m << (s % 32);
e->inst[1] |= (((src->type == P_IMMD) ? 0 : 1) << 22);
}
static void
emit_mov(struct nv50_pc *pc, struct nv50_reg *dst, struct nv50_reg *src)
{
struct nv50_program_exec *e = exec(pc);
e->inst[0] |= 0x10000000;
set_dst(pc, dst, e);
if (pc->allow32 && dst->type != P_RESULT && src->type == P_IMMD) {
set_immd(pc, src, e);
/*XXX: 32-bit, but steals part of "half" reg space - need to
* catch and handle this case if/when we do half-regs
*/
} else
if (src->type == P_IMMD || src->type == P_CONST) {
set_long(pc, e);
set_data(pc, src, 0x7f, 9, e);
e->inst[1] |= 0x20000000; /* src0 const? */
} else {
if (src->type == P_ATTR) {
set_long(pc, e);
e->inst[1] |= 0x00200000;
}
alloc_reg(pc, src);
e->inst[0] |= (src->hw << 9);
}
if (is_long(e) && !is_immd(e)) {
e->inst[1] |= 0x04000000; /* 32-bit */
e->inst[1] |= 0x0000c000; /* "subsubop" 0x3 */
if (!(e->inst[1] & 0x20000000))
e->inst[1] |= 0x00030000; /* "subsubop" 0xf */
} else
e->inst[0] |= 0x00008000;
emit(pc, e);
}
static INLINE void
emit_mov_immdval(struct nv50_pc *pc, struct nv50_reg *dst, float f)
{
struct nv50_reg *imm = alloc_immd(pc, f);
emit_mov(pc, dst, imm);
FREE(imm);
}
static boolean
check_swap_src_0_1(struct nv50_pc *pc,
struct nv50_reg **s0, struct nv50_reg **s1)
{
struct nv50_reg *src0 = *s0, *src1 = *s1;
if (src0->type == P_CONST) {
if (src1->type != P_CONST) {
*s0 = src1;
*s1 = src0;
return TRUE;
}
} else
if (src1->type == P_ATTR) {
if (src0->type != P_ATTR) {
*s0 = src1;
*s1 = src0;
return TRUE;
}
}
return FALSE;
}
static void
set_src_0_restricted(struct nv50_pc *pc, struct nv50_reg *src,
struct nv50_program_exec *e)
{
struct nv50_reg *temp;
if (src->type != P_TEMP) {
temp = temp_temp(pc);
emit_mov(pc, temp, src);
src = temp;
}
alloc_reg(pc, src);
e->inst[0] |= (src->hw << 9);
}
static void
set_src_0(struct nv50_pc *pc, struct nv50_reg *src, struct nv50_program_exec *e)
{
if (src->type == P_ATTR) {
set_long(pc, e);
e->inst[1] |= 0x00200000;
} else
if (src->type == P_CONST || src->type == P_IMMD) {
struct nv50_reg *temp = temp_temp(pc);
emit_mov(pc, temp, src);
src = temp;
}
alloc_reg(pc, src);
e->inst[0] |= (src->hw << 9);
}
static void
set_src_1(struct nv50_pc *pc, struct nv50_reg *src, struct nv50_program_exec *e)
{
if (src->type == P_ATTR) {
struct nv50_reg *temp = temp_temp(pc);
emit_mov(pc, temp, src);
src = temp;
} else
if (src->type == P_CONST || src->type == P_IMMD) {
assert(!(e->inst[0] & 0x00800000));
if (e->inst[0] & 0x01000000) {
struct nv50_reg *temp = temp_temp(pc);
emit_mov(pc, temp, src);
src = temp;
} else {
set_data(pc, src, 0x7f, 16, e);
e->inst[0] |= 0x00800000;
}
}
alloc_reg(pc, src);
e->inst[0] |= (src->hw << 16);
}
static void
set_src_2(struct nv50_pc *pc, struct nv50_reg *src, struct nv50_program_exec *e)
{
set_long(pc, e);
if (src->type == P_ATTR) {
struct nv50_reg *temp = temp_temp(pc);
emit_mov(pc, temp, src);
src = temp;
} else
if (src->type == P_CONST || src->type == P_IMMD) {
assert(!(e->inst[0] & 0x01000000));
if (e->inst[0] & 0x00800000) {
struct nv50_reg *temp = temp_temp(pc);
emit_mov(pc, temp, src);
src = temp;
} else {
set_data(pc, src, 0x7f, 32+14, e);
e->inst[0] |= 0x01000000;
}
}
alloc_reg(pc, src);
e->inst[1] |= (src->hw << 14);
}
static void
emit_mul(struct nv50_pc *pc, struct nv50_reg *dst, struct nv50_reg *src0,
struct nv50_reg *src1)
{
struct nv50_program_exec *e = exec(pc);
e->inst[0] |= 0xc0000000;
if (!pc->allow32)
set_long(pc, e);
check_swap_src_0_1(pc, &src0, &src1);
set_dst(pc, dst, e);
set_src_0(pc, src0, e);
if (src1->type == P_IMMD && !is_long(e)) {
if (src0->neg)
e->inst[0] |= 0x00008000;
set_immd(pc, src1, e);
} else {
set_src_1(pc, src1, e);
if (src0->neg ^ src1->neg) {
if (is_long(e))
e->inst[1] |= 0x08000000;
else
e->inst[0] |= 0x00008000;
}
}
emit(pc, e);
}
static void
emit_add(struct nv50_pc *pc, struct nv50_reg *dst,
struct nv50_reg *src0, struct nv50_reg *src1)
{
struct nv50_program_exec *e = exec(pc);
e->inst[0] |= 0xb0000000;
check_swap_src_0_1(pc, &src0, &src1);
if (!pc->allow32 || src0->neg || src1->neg) {
set_long(pc, e);
e->inst[1] |= (src0->neg << 26) | (src1->neg << 27);
}
set_dst(pc, dst, e);
set_src_0(pc, src0, e);
if (src1->type == P_CONST || src1->type == P_ATTR || is_long(e))
set_src_2(pc, src1, e);
else
if (src1->type == P_IMMD)
set_immd(pc, src1, e);
else
set_src_1(pc, src1, e);
emit(pc, e);
}
static void
emit_minmax(struct nv50_pc *pc, unsigned sub, struct nv50_reg *dst,
struct nv50_reg *src0, struct nv50_reg *src1)
{
struct nv50_program_exec *e = exec(pc);
set_long(pc, e);
e->inst[0] |= 0xb0000000;
e->inst[1] |= (sub << 29);
check_swap_src_0_1(pc, &src0, &src1);
set_dst(pc, dst, e);
set_src_0(pc, src0, e);
set_src_1(pc, src1, e);
emit(pc, e);
}
static INLINE void
emit_sub(struct nv50_pc *pc, struct nv50_reg *dst, struct nv50_reg *src0,
struct nv50_reg *src1)
{
src1->neg ^= 1;
emit_add(pc, dst, src0, src1);
src1->neg ^= 1;
}
static void
emit_mad(struct nv50_pc *pc, struct nv50_reg *dst, struct nv50_reg *src0,
struct nv50_reg *src1, struct nv50_reg *src2)
{
struct nv50_program_exec *e = exec(pc);
e->inst[0] |= 0xe0000000;
check_swap_src_0_1(pc, &src0, &src1);
set_dst(pc, dst, e);
set_src_0(pc, src0, e);
set_src_1(pc, src1, e);
set_src_2(pc, src2, e);
if (src0->neg ^ src1->neg)
e->inst[1] |= 0x04000000;
if (src2->neg)
e->inst[1] |= 0x08000000;
emit(pc, e);
}
static INLINE void
emit_msb(struct nv50_pc *pc, struct nv50_reg *dst, struct nv50_reg *src0,
struct nv50_reg *src1, struct nv50_reg *src2)
{
src2->neg ^= 1;
emit_mad(pc, dst, src0, src1, src2);
src2->neg ^= 1;
}
static void
emit_flop(struct nv50_pc *pc, unsigned sub,
struct nv50_reg *dst, struct nv50_reg *src)
{
struct nv50_program_exec *e = exec(pc);
e->inst[0] |= 0x90000000;
if (sub) {
set_long(pc, e);
e->inst[1] |= (sub << 29);
}
set_dst(pc, dst, e);
if (sub == 0 || sub == 2)
set_src_0_restricted(pc, src, e);
else
set_src_0(pc, src, e);
emit(pc, e);
}
static void
emit_preex2(struct nv50_pc *pc, struct nv50_reg *dst, struct nv50_reg *src)
{
struct nv50_program_exec *e = exec(pc);
e->inst[0] |= 0xb0000000;
set_dst(pc, dst, e);
set_src_0(pc, src, e);
set_long(pc, e);
e->inst[1] |= (6 << 29) | 0x00004000;
emit(pc, e);
}
static void
emit_precossin(struct nv50_pc *pc, struct nv50_reg *dst, struct nv50_reg *src)
{
struct nv50_program_exec *e = exec(pc);
e->inst[0] |= 0xb0000000;
set_dst(pc, dst, e);
set_src_0(pc, src, e);
set_long(pc, e);
e->inst[1] |= (6 << 29);
emit(pc, e);
}
#define CVTOP_RN 0x01
#define CVTOP_FLOOR 0x03
#define CVTOP_CEIL 0x05
#define CVTOP_TRUNC 0x07
#define CVTOP_SAT 0x08
#define CVTOP_ABS 0x10
/* 0x04 == 32 bit */
/* 0x40 == dst is float */
/* 0x80 == src is float */
#define CVT_F32_F32 0xc4
#define CVT_F32_S32 0x44
#define CVT_F32_U32 0x64
#define CVT_S32_F32 0x8c
#define CVT_S32_S32 0x0c
#define CVT_NEG 0x20
#define CVT_RI 0x08
static void
emit_cvt(struct nv50_pc *pc, struct nv50_reg *dst, struct nv50_reg *src,
int wp, unsigned cvn, unsigned fmt)
{
struct nv50_program_exec *e;
e = exec(pc);
set_long(pc, e);
e->inst[0] |= 0xa0000000;
e->inst[1] |= 0x00004000;
e->inst[1] |= (cvn << 16);
e->inst[1] |= (fmt << 24);
set_src_0(pc, src, e);
if (wp >= 0)
set_pred_wr(pc, 1, wp, e);
if (dst)
set_dst(pc, dst, e);
else {
e->inst[0] |= 0x000001fc;
e->inst[1] |= 0x00000008;
}
emit(pc, e);
}
/* nv50 Condition codes:
* 0x1 = LT
* 0x2 = EQ
* 0x3 = LE
* 0x4 = GT
* 0x5 = NE
* 0x6 = GE
* 0x7 = set condition code ? (used before bra.lt/le/gt/ge)
* 0x8 = unordered bit (allows NaN)
*/
static void
emit_set(struct nv50_pc *pc, unsigned ccode, struct nv50_reg *dst, int wp,
struct nv50_reg *src0, struct nv50_reg *src1)
{
static const unsigned cc_swapped[8] = { 0, 4, 2, 6, 1, 5, 3, 7 };
struct nv50_program_exec *e = exec(pc);
struct nv50_reg *rdst;
assert(ccode < 16);
if (check_swap_src_0_1(pc, &src0, &src1))
ccode = cc_swapped[ccode & 7] | (ccode & 8);
rdst = dst;
if (dst && dst->type != P_TEMP)
dst = alloc_temp(pc, NULL);
/* set.u32 */
set_long(pc, e);
e->inst[0] |= 0xb0000000;
e->inst[1] |= 0x60000000 | (ccode << 14);
/* XXX: decuda will disasm as .u16 and use .lo/.hi regs, but
* that doesn't seem to match what the hw actually does
e->inst[1] |= 0x04000000; << breaks things, u32 by default ?
*/
if (wp >= 0)
set_pred_wr(pc, 1, wp, e);
if (dst)
set_dst(pc, dst, e);
else {
e->inst[0] |= 0x000001fc;
e->inst[1] |= 0x00000008;
}
set_src_0(pc, src0, e);
set_src_1(pc, src1, e);
emit(pc, e);
pc->if_cond = pc->p->exec_tail; /* record for OPCODE_IF */
/* cvt.f32.u32/s32 (?) if we didn't only write the predicate */
if (rdst)
emit_cvt(pc, rdst, dst, -1, CVTOP_ABS | CVTOP_RN, CVT_F32_S32);
if (rdst && rdst != dst)
free_temp(pc, dst);
}
static INLINE unsigned
map_tgsi_setop_cc(unsigned op)
{
switch (op) {
case TGSI_OPCODE_SLT: return 0x1;
case TGSI_OPCODE_SGE: return 0x6;
case TGSI_OPCODE_SEQ: return 0x2;
case TGSI_OPCODE_SGT: return 0x4;
case TGSI_OPCODE_SLE: return 0x3;
case TGSI_OPCODE_SNE: return 0xd;
default:
assert(0);
return 0;
}
}
static INLINE void
emit_flr(struct nv50_pc *pc, struct nv50_reg *dst, struct nv50_reg *src)
{
emit_cvt(pc, dst, src, -1, CVTOP_FLOOR, CVT_F32_F32 | CVT_RI);
}
static void
emit_pow(struct nv50_pc *pc, struct nv50_reg *dst,
struct nv50_reg *v, struct nv50_reg *e)
{
struct nv50_reg *temp = alloc_temp(pc, NULL);
emit_flop(pc, 3, temp, v);
emit_mul(pc, temp, temp, e);
emit_preex2(pc, temp, temp);
emit_flop(pc, 6, dst, temp);
free_temp(pc, temp);
}
static INLINE void
emit_abs(struct nv50_pc *pc, struct nv50_reg *dst, struct nv50_reg *src)
{
emit_cvt(pc, dst, src, -1, CVTOP_ABS, CVT_F32_F32);
}
static INLINE void
emit_sat(struct nv50_pc *pc, struct nv50_reg *dst, struct nv50_reg *src)
{
emit_cvt(pc, dst, src, -1, CVTOP_SAT, CVT_F32_F32);
}
static void
emit_lit(struct nv50_pc *pc, struct nv50_reg **dst, unsigned mask,
struct nv50_reg **src)
{
struct nv50_reg *one = alloc_immd(pc, 1.0);
struct nv50_reg *zero = alloc_immd(pc, 0.0);
struct nv50_reg *neg128 = alloc_immd(pc, -127.999999);
struct nv50_reg *pos128 = alloc_immd(pc, 127.999999);
struct nv50_reg *tmp[4];
boolean allow32 = pc->allow32;
pc->allow32 = FALSE;
if (mask & (3 << 1)) {
tmp[0] = alloc_temp(pc, NULL);
emit_minmax(pc, 4, tmp[0], src[0], zero);
}
if (mask & (1 << 2)) {
set_pred_wr(pc, 1, 0, pc->p->exec_tail);
tmp[1] = temp_temp(pc);
emit_minmax(pc, 4, tmp[1], src[1], zero);
tmp[3] = temp_temp(pc);
emit_minmax(pc, 4, tmp[3], src[3], neg128);
emit_minmax(pc, 5, tmp[3], tmp[3], pos128);
emit_pow(pc, dst[2], tmp[1], tmp[3]);
emit_mov(pc, dst[2], zero);
set_pred(pc, 3, 0, pc->p->exec_tail);
}
if (mask & (1 << 1))
assimilate_temp(pc, dst[1], tmp[0]);
else
if (mask & (1 << 2))
free_temp(pc, tmp[0]);
pc->allow32 = allow32;
/* do this last, in case src[i,j] == dst[0,3] */
if (mask & (1 << 0))
emit_mov(pc, dst[0], one);
if (mask & (1 << 3))
emit_mov(pc, dst[3], one);
FREE(pos128);
FREE(neg128);
FREE(zero);
FREE(one);
}
static void
emit_neg(struct nv50_pc *pc, struct nv50_reg *dst, struct nv50_reg *src)
{
struct nv50_program_exec *e = exec(pc);
set_long(pc, e);
e->inst[0] |= 0xa0000000; /* delta */
e->inst[1] |= (7 << 29); /* delta */
e->inst[1] |= 0x04000000; /* negate arg0? probably not */
e->inst[1] |= (1 << 14); /* src .f32 */
set_dst(pc, dst, e);
set_src_0(pc, src, e);
emit(pc, e);
}
static void
emit_kil(struct nv50_pc *pc, struct nv50_reg *src)
{
struct nv50_program_exec *e;
const int r_pred = 1;
/* Sets predicate reg ? */
e = exec(pc);
e->inst[0] = 0xa00001fd;
e->inst[1] = 0xc4014788;
set_src_0(pc, src, e);
set_pred_wr(pc, 1, r_pred, e);
if (src->neg)
e->inst[1] |= 0x20000000;
emit(pc, e);
/* This is probably KILP */
e = exec(pc);
e->inst[0] = 0x000001fe;
set_long(pc, e);
set_pred(pc, 1 /* LT? */, r_pred, e);
emit(pc, e);
}
static void
emit_tex(struct nv50_pc *pc, struct nv50_reg **dst, unsigned mask,
struct nv50_reg **src, unsigned unit, unsigned type, boolean proj)
{
struct nv50_reg *temp, *t[4];
struct nv50_program_exec *e;
unsigned c, mode, dim;
switch (type) {
case TGSI_TEXTURE_1D:
dim = 1;
break;
case TGSI_TEXTURE_UNKNOWN:
case TGSI_TEXTURE_2D:
case TGSI_TEXTURE_SHADOW1D: /* XXX: x, z */
case TGSI_TEXTURE_RECT:
dim = 2;
break;
case TGSI_TEXTURE_3D:
case TGSI_TEXTURE_CUBE:
case TGSI_TEXTURE_SHADOW2D:
case TGSI_TEXTURE_SHADOWRECT: /* XXX */
dim = 3;
break;
default:
assert(0);
break;
}
/* some cards need t[0]'s hw index to be a multiple of 4 */
alloc_temp4(pc, t, 0);
if (proj) {
if (src[0]->type == P_TEMP && src[0]->rhw != -1) {
mode = pc->interp_mode[src[0]->index];
t[3]->rhw = src[3]->rhw;
emit_interp(pc, t[3], NULL, (mode & INTERP_CENTROID));
emit_flop(pc, 0, t[3], t[3]);
for (c = 0; c < dim; c++) {
t[c]->rhw = src[c]->rhw;
emit_interp(pc, t[c], t[3],
(mode | INTERP_PERSPECTIVE));
}
} else {
emit_flop(pc, 0, t[3], src[3]);
for (c = 0; c < dim; c++)
emit_mul(pc, t[c], src[c], t[3]);
/* XXX: for some reason the blob sometimes uses MAD:
* emit_mad(pc, t[c], src[0][c], t[3], t[3])
* pc->p->exec_tail->inst[1] |= 0x080fc000;
*/
}
} else {
if (type == TGSI_TEXTURE_CUBE) {
temp = temp_temp(pc);
emit_minmax(pc, 4, temp, src[0], src[1]);
emit_minmax(pc, 4, temp, temp, src[2]);
emit_flop(pc, 0, temp, temp);
for (c = 0; c < 3; c++)
emit_mul(pc, t[c], src[c], temp);
} else {
for (c = 0; c < dim; c++)
emit_mov(pc, t[c], src[c]);
}
}
e = exec(pc);
set_long(pc, e);
e->inst[0] |= 0xf0000000;
e->inst[1] |= 0x00000004;
set_dst(pc, t[0], e);
e->inst[0] |= (unit << 9);
if (dim == 2)
e->inst[0] |= 0x00400000;
else
if (dim == 3)
e->inst[0] |= 0x00800000;
e->inst[0] |= (mask & 0x3) << 25;
e->inst[1] |= (mask & 0xc) << 12;
emit(pc, e);
#if 1
c = 0;
if (mask & 1) emit_mov(pc, dst[0], t[c++]);
if (mask & 2) emit_mov(pc, dst[1], t[c++]);
if (mask & 4) emit_mov(pc, dst[2], t[c++]);
if (mask & 8) emit_mov(pc, dst[3], t[c]);
free_temp4(pc, t);
#else
/* XXX: if p.e. MUL is used directly after TEX, it would still use
* the texture coordinates, not the fetched values: latency ? */
for (c = 0; c < 4; c++) {
if (mask & (1 << c))
assimilate_temp(pc, dst[c], t[c]);
else
free_temp(pc, t[c]);
}
#endif
}
static void
emit_branch(struct nv50_pc *pc, int pred, unsigned cc,
struct nv50_program_exec **join)
{
struct nv50_program_exec *e = exec(pc);
if (join) {
set_long(pc, e);
e->inst[0] |= 0xa0000002;
emit(pc, e);
*join = e;
e = exec(pc);
}
set_long(pc, e);
e->inst[0] |= 0x10000002;
if (pred >= 0)
set_pred(pc, cc, pred, e);
emit(pc, e);
}
static void
emit_nop(struct nv50_pc *pc)
{
struct nv50_program_exec *e = exec(pc);
e->inst[0] = 0xf0000000;
set_long(pc, e);
e->inst[1] = 0xe0000000;
emit(pc, e);
}
static void
convert_to_long(struct nv50_pc *pc, struct nv50_program_exec *e)
{
unsigned q = 0, m = ~0;
assert(!is_long(e));
switch (e->inst[0] >> 28) {
case 0x1:
/* MOV */
q = 0x0403c000;
m = 0xffff7fff;
break;
case 0x8:
/* INTERP (move centroid, perspective and flat bits) */
m = ~0x03000100;
q = (e->inst[0] & (3 << 24)) >> (24 - 16);
q |= (e->inst[0] & (1 << 8)) << (18 - 8);
break;
case 0x9:
/* RCP */
break;
case 0xB:
/* ADD */
m = ~(127 << 16);
q = ((e->inst[0] & (~m)) >> 2);
break;
case 0xC:
/* MUL */
m = ~0x00008000;
q = ((e->inst[0] & (~m)) << 12);
break;
case 0xE:
/* MAD (if src2 == dst) */
q = ((e->inst[0] & 0x1fc) << 12);
break;
default:
assert(0);
break;
}
set_long(pc, e);
pc->p->exec_size++;
e->inst[0] &= m;
e->inst[1] |= q;
}
static boolean
negate_supported(const struct tgsi_full_instruction *insn, int i)
{
switch (insn->Instruction.Opcode) {
case TGSI_OPCODE_DP3:
case TGSI_OPCODE_DP4:
case TGSI_OPCODE_MUL:
case TGSI_OPCODE_KIL:
case TGSI_OPCODE_ADD:
case TGSI_OPCODE_SUB:
case TGSI_OPCODE_MAD:
return TRUE;
case TGSI_OPCODE_POW:
return (i == 1) ? TRUE : FALSE;
default:
return FALSE;
}
}
/* Return a read mask for source registers deduced from opcode & write mask. */
static unsigned
nv50_tgsi_src_mask(const struct tgsi_full_instruction *insn, int c)
{
unsigned x, mask = insn->FullDstRegisters[0].DstRegister.WriteMask;
switch (insn->Instruction.Opcode) {
case TGSI_OPCODE_COS:
case TGSI_OPCODE_SIN:
return (mask & 0x8) | ((mask & 0x7) ? 0x1 : 0x0);
case TGSI_OPCODE_DP3:
return 0x7;
case TGSI_OPCODE_DP4:
case TGSI_OPCODE_DPH:
case TGSI_OPCODE_KIL: /* WriteMask ignored */
return 0xf;
case TGSI_OPCODE_DST:
return mask & (c ? 0xa : 0x6);
case TGSI_OPCODE_EX2:
case TGSI_OPCODE_LG2:
case TGSI_OPCODE_POW:
case TGSI_OPCODE_RCP:
case TGSI_OPCODE_RSQ:
case TGSI_OPCODE_SCS:
return 0x1;
case TGSI_OPCODE_LIT:
return 0xb;
case TGSI_OPCODE_TEX:
case TGSI_OPCODE_TXP:
{
const struct tgsi_instruction_ext_texture *tex;
assert(insn->Instruction.Extended);
tex = &insn->InstructionExtTexture;
mask = 0x7;
if (insn->Instruction.Opcode == TGSI_OPCODE_TXP)
mask |= 0x8;
switch (tex->Texture) {
case TGSI_TEXTURE_1D:
mask &= 0x9;
break;
case TGSI_TEXTURE_2D:
mask &= 0xb;
break;
default:
break;
}
}
return mask;
case TGSI_OPCODE_XPD:
x = 0;
if (mask & 1) x |= 0x6;
if (mask & 2) x |= 0x5;
if (mask & 4) x |= 0x3;
return x;
default:
break;
}
return mask;
}
static struct nv50_reg *
tgsi_dst(struct nv50_pc *pc, int c, const struct tgsi_full_dst_register *dst)
{
switch (dst->DstRegister.File) {
case TGSI_FILE_TEMPORARY:
return &pc->temp[dst->DstRegister.Index * 4 + c];
case TGSI_FILE_OUTPUT:
return &pc->result[dst->DstRegister.Index * 4 + c];
case TGSI_FILE_NULL:
return NULL;
default:
break;
}
return NULL;
}
static struct nv50_reg *
tgsi_src(struct nv50_pc *pc, int chan, const struct tgsi_full_src_register *src,
boolean neg)
{
struct nv50_reg *r = NULL;
struct nv50_reg *temp;
unsigned sgn, c;
sgn = tgsi_util_get_full_src_register_sign_mode(src, chan);
c = tgsi_util_get_full_src_register_extswizzle(src, chan);
switch (c) {
case TGSI_EXTSWIZZLE_X:
case TGSI_EXTSWIZZLE_Y:
case TGSI_EXTSWIZZLE_Z:
case TGSI_EXTSWIZZLE_W:
switch (src->SrcRegister.File) {
case TGSI_FILE_INPUT:
r = &pc->attr[src->SrcRegister.Index * 4 + c];
break;
case TGSI_FILE_TEMPORARY:
r = &pc->temp[src->SrcRegister.Index * 4 + c];
break;
case TGSI_FILE_CONSTANT:
r = &pc->param[src->SrcRegister.Index * 4 + c];
break;
case TGSI_FILE_IMMEDIATE:
r = &pc->immd[src->SrcRegister.Index * 4 + c];
break;
case TGSI_FILE_SAMPLER:
break;
default:
assert(0);
break;
}
break;
case TGSI_EXTSWIZZLE_ZERO:
r = alloc_immd(pc, 0.0);
return r;
case TGSI_EXTSWIZZLE_ONE:
if (sgn == TGSI_UTIL_SIGN_TOGGLE || sgn == TGSI_UTIL_SIGN_SET)
return alloc_immd(pc, -1.0);
return alloc_immd(pc, 1.0);
default:
assert(0);
break;
}
switch (sgn) {
case TGSI_UTIL_SIGN_KEEP:
break;
case TGSI_UTIL_SIGN_CLEAR:
temp = temp_temp(pc);
emit_abs(pc, temp, r);
r = temp;
break;
case TGSI_UTIL_SIGN_TOGGLE:
if (neg)
r->neg = 1;
else {
temp = temp_temp(pc);
emit_neg(pc, temp, r);
r = temp;
}
break;
case TGSI_UTIL_SIGN_SET:
temp = temp_temp(pc);
emit_abs(pc, temp, r);
if (neg)
temp->neg = 1;
else
emit_neg(pc, temp, temp);
r = temp;
break;
default:
assert(0);
break;
}
return r;
}
/* return TRUE for ops that produce only a single result */
static boolean
is_scalar_op(unsigned op)
{
switch (op) {
case TGSI_OPCODE_COS:
case TGSI_OPCODE_DP2:
case TGSI_OPCODE_DP3:
case TGSI_OPCODE_DP4:
case TGSI_OPCODE_DPH:
case TGSI_OPCODE_EX2:
case TGSI_OPCODE_LG2:
case TGSI_OPCODE_POW:
case TGSI_OPCODE_RCP:
case TGSI_OPCODE_RSQ:
case TGSI_OPCODE_SIN:
/*
case TGSI_OPCODE_KIL:
case TGSI_OPCODE_LIT:
case TGSI_OPCODE_SCS:
*/
return TRUE;
default:
return FALSE;
}
}
/* Returns a bitmask indicating which dst components depend
* on source s, component c (reverse of nv50_tgsi_src_mask).
*/
static unsigned
nv50_tgsi_dst_revdep(unsigned op, int s, int c)
{
if (is_scalar_op(op))
return 0x1;
switch (op) {
case TGSI_OPCODE_DST:
return (1 << c) & (s ? 0xa : 0x6);
case TGSI_OPCODE_XPD:
switch (c) {
case 0: return 0x6;
case 1: return 0x5;
case 2: return 0x3;
case 3: return 0x0;
default:
assert(0);
return 0x0;
}
case TGSI_OPCODE_LIT:
case TGSI_OPCODE_SCS:
case TGSI_OPCODE_TEX:
case TGSI_OPCODE_TXP:
/* these take care of dangerous swizzles themselves */
return 0x0;
case TGSI_OPCODE_IF:
case TGSI_OPCODE_KIL:
/* don't call this function for these ops */
assert(0);
return 0;
default:
/* linear vector instruction */
return (1 << c);
}
}
static INLINE boolean
has_pred(struct nv50_program_exec *e, unsigned cc)
{
if (!is_long(e) || is_immd(e))
return FALSE;
return ((e->inst[1] & 0x780) == (cc << 7));
}
/* on ENDIF see if we can do "@p0.neu single_op" instead of:
* join_at ENDIF
* @p0.eq bra ENDIF
* single_op
* ENDIF: nop.join
*/
static boolean
nv50_kill_branch(struct nv50_pc *pc)
{
int lvl = pc->if_lvl;
if (pc->if_insn[lvl]->next != pc->p->exec_tail)
return FALSE;
/* if ccode == 'true', the BRA is from an ELSE and the predicate
* reg may no longer be valid, since we currently always use $p0
*/
if (has_pred(pc->if_insn[lvl], 0xf))
return FALSE;
assert(pc->if_insn[lvl] && pc->br_join[lvl]);
/* We'll use the exec allocated for JOIN_AT (as we can't easily
* update prev's next); if exec_tail is BRK, update the pointer.
*/
if (pc->loop_lvl && pc->br_loop[pc->loop_lvl - 1] == pc->p->exec_tail)
pc->br_loop[pc->loop_lvl - 1] = pc->br_join[lvl];
pc->p->exec_size -= 4; /* remove JOIN_AT and BRA */
*pc->br_join[lvl] = *pc->p->exec_tail;
FREE(pc->if_insn[lvl]);
FREE(pc->p->exec_tail);
pc->p->exec_tail = pc->br_join[lvl];
pc->p->exec_tail->next = NULL;
set_pred(pc, 0xd, 0, pc->p->exec_tail);
return TRUE;
}
static boolean
nv50_program_tx_insn(struct nv50_pc *pc,
const struct tgsi_full_instruction *inst)
{
struct nv50_reg *rdst[4], *dst[4], *brdc, *src[3][4], *temp;
unsigned mask, sat, unit;
int i, c;
mask = inst->FullDstRegisters[0].DstRegister.WriteMask;
sat = inst->Instruction.Saturate == TGSI_SAT_ZERO_ONE;
memset(src, 0, sizeof(src));
for (c = 0; c < 4; c++) {
if ((mask & (1 << c)) && !pc->r_dst[c])
dst[c] = tgsi_dst(pc, c, &inst->FullDstRegisters[0]);
else
dst[c] = pc->r_dst[c];
rdst[c] = dst[c];
}
for (i = 0; i < inst->Instruction.NumSrcRegs; i++) {
const struct tgsi_full_src_register *fs = &inst->FullSrcRegisters[i];
unsigned src_mask;
boolean neg_supp;
src_mask = nv50_tgsi_src_mask(inst, i);
neg_supp = negate_supported(inst, i);
if (fs->SrcRegister.File == TGSI_FILE_SAMPLER)
unit = fs->SrcRegister.Index;
for (c = 0; c < 4; c++)
if (src_mask & (1 << c))
src[i][c] = tgsi_src(pc, c, fs, neg_supp);
}
brdc = temp = pc->r_brdc;
if (brdc && brdc->type != P_TEMP) {
temp = temp_temp(pc);
if (sat)
brdc = temp;
} else
if (sat) {
for (c = 0; c < 4; c++) {
if (!(mask & (1 << c)) || dst[c]->type == P_TEMP)
continue;
rdst[c] = dst[c];
dst[c] = temp_temp(pc);
}
}
assert(brdc || !is_scalar_op(inst->Instruction.Opcode));
switch (inst->Instruction.Opcode) {
case TGSI_OPCODE_ABS:
for (c = 0; c < 4; c++) {
if (!(mask & (1 << c)))
continue;
emit_abs(pc, dst[c], src[0][c]);
}
break;
case TGSI_OPCODE_ADD:
for (c = 0; c < 4; c++) {
if (!(mask & (1 << c)))
continue;
emit_add(pc, dst[c], src[0][c], src[1][c]);
}
break;
case TGSI_OPCODE_BGNLOOP:
pc->loop_pos[pc->loop_lvl++] = pc->p->exec_size;
break;
case TGSI_OPCODE_BRK:
emit_branch(pc, -1, 0, NULL);
assert(pc->loop_lvl > 0);
pc->br_loop[pc->loop_lvl - 1] = pc->p->exec_tail;
break;
case TGSI_OPCODE_CEIL:
for (c = 0; c < 4; c++) {
if (!(mask & (1 << c)))
continue;
emit_cvt(pc, dst[c], src[0][c], -1,
CVTOP_CEIL, CVT_F32_F32 | CVT_RI);
}
break;
case TGSI_OPCODE_COS:
if (mask & 8) {
emit_precossin(pc, temp, src[0][3]);
emit_flop(pc, 5, dst[3], temp);
if (!(mask &= 7))
break;
if (temp == dst[3])
temp = brdc = temp_temp(pc);
}
emit_precossin(pc, temp, src[0][0]);
emit_flop(pc, 5, brdc, temp);
break;
case TGSI_OPCODE_DP3:
emit_mul(pc, temp, src[0][0], src[1][0]);
emit_mad(pc, temp, src[0][1], src[1][1], temp);
emit_mad(pc, brdc, src[0][2], src[1][2], temp);
break;
case TGSI_OPCODE_DP4:
emit_mul(pc, temp, src[0][0], src[1][0]);
emit_mad(pc, temp, src[0][1], src[1][1], temp);
emit_mad(pc, temp, src[0][2], src[1][2], temp);
emit_mad(pc, brdc, src[0][3], src[1][3], temp);
break;
case TGSI_OPCODE_DPH:
emit_mul(pc, temp, src[0][0], src[1][0]);
emit_mad(pc, temp, src[0][1], src[1][1], temp);
emit_mad(pc, temp, src[0][2], src[1][2], temp);
emit_add(pc, brdc, src[1][3], temp);
break;
case TGSI_OPCODE_DST:
if (mask & (1 << 1))
emit_mul(pc, dst[1], src[0][1], src[1][1]);
if (mask & (1 << 2))
emit_mov(pc, dst[2], src[0][2]);
if (mask & (1 << 3))
emit_mov(pc, dst[3], src[1][3]);
if (mask & (1 << 0))
emit_mov_immdval(pc, dst[0], 1.0f);
break;
case TGSI_OPCODE_ELSE:
emit_branch(pc, -1, 0, NULL);
pc->if_insn[--pc->if_lvl]->param.index = pc->p->exec_size;
pc->if_insn[pc->if_lvl++] = pc->p->exec_tail;
break;
case TGSI_OPCODE_ENDIF:
pc->if_insn[--pc->if_lvl]->param.index = pc->p->exec_size;
/* try to replace branch over 1 insn with a predicated insn */
if (nv50_kill_branch(pc) == TRUE)
break;
if (pc->br_join[pc->if_lvl]) {
pc->br_join[pc->if_lvl]->param.index = pc->p->exec_size;
pc->br_join[pc->if_lvl] = NULL;
}
/* emit a NOP as join point, we could set it on the next
* one, but would have to make sure it is long and !immd
*/
emit_nop(pc);
pc->p->exec_tail->inst[1] |= 2;
break;
case TGSI_OPCODE_ENDLOOP:
emit_branch(pc, -1, 0, NULL);
pc->p->exec_tail->param.index = pc->loop_pos[--pc->loop_lvl];
pc->br_loop[pc->loop_lvl]->param.index = pc->p->exec_size;
break;
case TGSI_OPCODE_EX2:
emit_preex2(pc, temp, src[0][0]);
emit_flop(pc, 6, brdc, temp);
break;
case TGSI_OPCODE_FLR:
for (c = 0; c < 4; c++) {
if (!(mask & (1 << c)))
continue;
emit_flr(pc, dst[c], src[0][c]);
}
break;
case TGSI_OPCODE_FRC:
temp = temp_temp(pc);
for (c = 0; c < 4; c++) {
if (!(mask & (1 << c)))
continue;
emit_flr(pc, temp, src[0][c]);
emit_sub(pc, dst[c], src[0][c], temp);
}
break;
case TGSI_OPCODE_IF:
/* emitting a join_at may not be necessary */
assert(pc->if_lvl < MAX_IF_DEPTH);
set_pred_wr(pc, 1, 0, pc->if_cond);
emit_branch(pc, 0, 2, &pc->br_join[pc->if_lvl]);
pc->if_insn[pc->if_lvl++] = pc->p->exec_tail;
break;
case TGSI_OPCODE_KIL:
emit_kil(pc, src[0][0]);
emit_kil(pc, src[0][1]);
emit_kil(pc, src[0][2]);
emit_kil(pc, src[0][3]);
break;
case TGSI_OPCODE_LIT:
emit_lit(pc, &dst[0], mask, &src[0][0]);
break;
case TGSI_OPCODE_LG2:
emit_flop(pc, 3, brdc, src[0][0]);
break;
case TGSI_OPCODE_LRP:
temp = temp_temp(pc);
for (c = 0; c < 4; c++) {
if (!(mask & (1 << c)))
continue;
emit_sub(pc, temp, src[1][c], src[2][c]);
emit_mad(pc, dst[c], temp, src[0][c], src[2][c]);
}
break;
case TGSI_OPCODE_MAD:
for (c = 0; c < 4; c++) {
if (!(mask & (1 << c)))
continue;
emit_mad(pc, dst[c], src[0][c], src[1][c], src[2][c]);
}
break;
case TGSI_OPCODE_MAX:
for (c = 0; c < 4; c++) {
if (!(mask & (1 << c)))
continue;
emit_minmax(pc, 4, dst[c], src[0][c], src[1][c]);
}
break;
case TGSI_OPCODE_MIN:
for (c = 0; c < 4; c++) {
if (!(mask & (1 << c)))
continue;
emit_minmax(pc, 5, dst[c], src[0][c], src[1][c]);
}
break;
case TGSI_OPCODE_MOV:
case TGSI_OPCODE_SWZ:
for (c = 0; c < 4; c++) {
if (!(mask & (1 << c)))
continue;
emit_mov(pc, dst[c], src[0][c]);
}
break;
case TGSI_OPCODE_MUL:
for (c = 0; c < 4; c++) {
if (!(mask & (1 << c)))
continue;
emit_mul(pc, dst[c], src[0][c], src[1][c]);
}
break;
case TGSI_OPCODE_POW:
emit_pow(pc, brdc, src[0][0], src[1][0]);
break;
case TGSI_OPCODE_RCP:
emit_flop(pc, 0, brdc, src[0][0]);
break;
case TGSI_OPCODE_RSQ:
emit_flop(pc, 2, brdc, src[0][0]);
break;
case TGSI_OPCODE_SCS:
temp = temp_temp(pc);
if (mask & 3)
emit_precossin(pc, temp, src[0][0]);
if (mask & (1 << 0))
emit_flop(pc, 5, dst[0], temp);
if (mask & (1 << 1))
emit_flop(pc, 4, dst[1], temp);
if (mask & (1 << 2))
emit_mov_immdval(pc, dst[2], 0.0);
if (mask & (1 << 3))
emit_mov_immdval(pc, dst[3], 1.0);
break;
case TGSI_OPCODE_SIN:
if (mask & 8) {
emit_precossin(pc, temp, src[0][3]);
emit_flop(pc, 4, dst[3], temp);
if (!(mask &= 7))
break;
if (temp == dst[3])
temp = brdc = temp_temp(pc);
}
emit_precossin(pc, temp, src[0][0]);
emit_flop(pc, 4, brdc, temp);
break;
case TGSI_OPCODE_SLT:
case TGSI_OPCODE_SGE:
case TGSI_OPCODE_SEQ:
case TGSI_OPCODE_SGT:
case TGSI_OPCODE_SLE:
case TGSI_OPCODE_SNE:
i = map_tgsi_setop_cc(inst->Instruction.Opcode);
for (c = 0; c < 4; c++) {
if (!(mask & (1 << c)))
continue;
emit_set(pc, i, dst[c], -1, src[0][c], src[1][c]);
}
break;
case TGSI_OPCODE_SUB:
for (c = 0; c < 4; c++) {
if (!(mask & (1 << c)))
continue;
emit_sub(pc, dst[c], src[0][c], src[1][c]);
}
break;
case TGSI_OPCODE_TEX:
emit_tex(pc, dst, mask, src[0], unit,
inst->InstructionExtTexture.Texture, FALSE);
break;
case TGSI_OPCODE_TXP:
emit_tex(pc, dst, mask, src[0], unit,
inst->InstructionExtTexture.Texture, TRUE);
break;
case TGSI_OPCODE_TRUNC:
for (c = 0; c < 4; c++) {
if (!(mask & (1 << c)))
continue;
emit_cvt(pc, dst[c], src[0][c], -1,
CVTOP_TRUNC, CVT_F32_F32 | CVT_RI);
}
break;
case TGSI_OPCODE_XPD:
temp = temp_temp(pc);
if (mask & (1 << 0)) {
emit_mul(pc, temp, src[0][2], src[1][1]);
emit_msb(pc, dst[0], src[0][1], src[1][2], temp);
}
if (mask & (1 << 1)) {
emit_mul(pc, temp, src[0][0], src[1][2]);
emit_msb(pc, dst[1], src[0][2], src[1][0], temp);
}
if (mask & (1 << 2)) {
emit_mul(pc, temp, src[0][1], src[1][0]);
emit_msb(pc, dst[2], src[0][0], src[1][1], temp);
}
if (mask & (1 << 3))
emit_mov_immdval(pc, dst[3], 1.0);
break;
case TGSI_OPCODE_END:
break;
default:
NOUVEAU_ERR("invalid opcode %d\n", inst->Instruction.Opcode);
return FALSE;
}
if (brdc) {
if (sat)
emit_sat(pc, brdc, brdc);
for (c = 0; c < 4; c++)
if ((mask & (1 << c)) && dst[c] != brdc)
emit_mov(pc, dst[c], brdc);
} else
if (sat) {
for (c = 0; c < 4; c++) {
if (!(mask & (1 << c)))
continue;
/* in this case we saturate later */
if (dst[c]->type == P_TEMP && dst[c]->index < 0)
continue;
emit_sat(pc, rdst[c], dst[c]);
}
}
for (i = 0; i < inst->Instruction.NumSrcRegs; i++) {
for (c = 0; c < 4; c++) {
if (!src[i][c])
continue;
if (src[i][c]->index == -1 && src[i][c]->type == P_IMMD)
FREE(src[i][c]);
}
}
kill_temp_temp(pc);
return TRUE;
}
static void
prep_inspect_insn(struct nv50_pc *pc, const struct tgsi_full_instruction *insn)
{
struct nv50_reg *reg = NULL;
const struct tgsi_full_src_register *src;
const struct tgsi_dst_register *dst;
unsigned i, c, k, mask;
dst = &insn->FullDstRegisters[0].DstRegister;
mask = dst->WriteMask;
if (dst->File == TGSI_FILE_TEMPORARY)
reg = pc->temp;
else
if (dst->File == TGSI_FILE_OUTPUT)
reg = pc->result;
if (reg) {
for (c = 0; c < 4; c++) {
if (!(mask & (1 << c)))
continue;
reg[dst->Index * 4 + c].acc = pc->insn_nr;
}
}
for (i = 0; i < insn->Instruction.NumSrcRegs; i++) {
src = &insn->FullSrcRegisters[i];
if (src->SrcRegister.File == TGSI_FILE_TEMPORARY)
reg = pc->temp;
else
if (src->SrcRegister.File == TGSI_FILE_INPUT)
reg = pc->attr;
else
continue;
mask = nv50_tgsi_src_mask(insn, i);
for (c = 0; c < 4; c++) {
if (!(mask & (1 << c)))
continue;
k = tgsi_util_get_full_src_register_extswizzle(src, c);
if (k > TGSI_EXTSWIZZLE_W)
continue;
reg[src->SrcRegister.Index * 4 + k].acc = pc->insn_nr;
}
}
}
/* Returns a bitmask indicating which dst components need to be
* written to temporaries first to avoid 'corrupting' sources.
*
* m[i] (out) indicate component to write in the i-th position
* rdep[c] (in) bitmasks of dst[i] that require dst[c] as source
*/
static unsigned
nv50_revdep_reorder(unsigned m[4], unsigned rdep[4])
{
unsigned i, c, x, unsafe;
for (c = 0; c < 4; c++)
m[c] = c;
/* Swap as long as a dst component written earlier is depended on
* by one written later, but the next one isn't depended on by it.
*/
for (c = 0; c < 3; c++) {
if (rdep[m[c + 1]] & (1 << m[c]))
continue; /* if next one is depended on by us */
for (i = c + 1; i < 4; i++)
/* if we are depended on by a later one */
if (rdep[m[c]] & (1 << m[i]))
break;
if (i == 4)
continue;
/* now, swap */
x = m[c];
m[c] = m[c + 1];
m[c + 1] = x;
/* restart */
c = 0;
}
/* mark dependencies that could not be resolved by reordering */
for (i = 0; i < 3; ++i)
for (c = i + 1; c < 4; ++c)
if (rdep[m[i]] & (1 << m[c]))
unsafe |= (1 << i);
/* NOTE: $unsafe is with respect to order, not component */
return unsafe;
}
/* Select a suitable dst register for broadcasting scalar results,
* or return NULL if we have to allocate an extra TEMP.
*
* If e.g. only 1 component is written, we may also emit the final
* result to a write-only register.
*/
static struct nv50_reg *
tgsi_broadcast_dst(struct nv50_pc *pc,
const struct tgsi_full_dst_register *fd, unsigned mask)
{
if (fd->DstRegister.File == TGSI_FILE_TEMPORARY) {
int c = ffs(~mask & fd->DstRegister.WriteMask);
if (c)
return tgsi_dst(pc, c - 1, fd);
} else {
int c = ffs(fd->DstRegister.WriteMask) - 1;
if ((1 << c) == fd->DstRegister.WriteMask)
return tgsi_dst(pc, c, fd);
}
return NULL;
}
/* Scan source swizzles and return a bitmask indicating dst regs that
* also occur among the src regs, and fill rdep for nv50_revdep_reoder.
*/
static unsigned
nv50_tgsi_scan_swizzle(const struct tgsi_full_instruction *insn,
unsigned rdep[4])
{
const struct tgsi_full_dst_register *fd = &insn->FullDstRegisters[0];
const struct tgsi_full_src_register *fs;
unsigned i, deqs = 0;
for (i = 0; i < 4; ++i)
rdep[i] = 0;
for (i = 0; i < insn->Instruction.NumSrcRegs; i++) {
unsigned chn, mask = nv50_tgsi_src_mask(insn, i);
boolean neg_supp = negate_supported(insn, i);
fs = &insn->FullSrcRegisters[i];
if (fs->SrcRegister.File != fd->DstRegister.File ||
fs->SrcRegister.Index != fd->DstRegister.Index)
continue;
for (chn = 0; chn < 4; ++chn) {
unsigned s, c;
if (!(mask & (1 << chn))) /* src is not read */
continue;
c = tgsi_util_get_full_src_register_extswizzle(fs, chn);
s = tgsi_util_get_full_src_register_sign_mode(fs, chn);
if (c > TGSI_EXTSWIZZLE_W ||
!(fd->DstRegister.WriteMask & (1 << c)))
continue;
/* no danger if src is copied to TEMP first */
if ((s != TGSI_UTIL_SIGN_KEEP) &&
(s != TGSI_UTIL_SIGN_TOGGLE || !neg_supp))
continue;
rdep[c] |= nv50_tgsi_dst_revdep(
insn->Instruction.Opcode, i, chn);
deqs |= (1 << c);
}
}
return deqs;
}
static boolean
nv50_tgsi_insn(struct nv50_pc *pc, const union tgsi_full_token *tok)
{
struct tgsi_full_instruction insn = tok->FullInstruction;
const struct tgsi_full_dst_register *fd;
unsigned i, deqs, rdep[4], m[4];
fd = &tok->FullInstruction.FullDstRegisters[0];
deqs = nv50_tgsi_scan_swizzle(&insn, rdep);
if (is_scalar_op(insn.Instruction.Opcode)) {
pc->r_brdc = tgsi_broadcast_dst(pc, fd, deqs);
if (!pc->r_brdc)
pc->r_brdc = temp_temp(pc);
return nv50_program_tx_insn(pc, &insn);
}
pc->r_brdc = NULL;
if (!deqs)
return nv50_program_tx_insn(pc, &insn);
deqs = nv50_revdep_reorder(m, rdep);
for (i = 0; i < 4; ++i) {
assert(pc->r_dst[m[i]] == NULL);
insn.FullDstRegisters[0].DstRegister.WriteMask =
fd->DstRegister.WriteMask & (1 << m[i]);
if (!insn.FullDstRegisters[0].DstRegister.WriteMask)
continue;
if (deqs & (1 << i))
pc->r_dst[m[i]] = alloc_temp(pc, NULL);
if (!nv50_program_tx_insn(pc, &insn))
return FALSE;
}
for (i = 0; i < 4; i++) {
struct nv50_reg *reg = pc->r_dst[i];
if (!reg)
continue;
pc->r_dst[i] = NULL;
if (insn.Instruction.Saturate == TGSI_SAT_ZERO_ONE)
emit_sat(pc, tgsi_dst(pc, i, fd), reg);
else
emit_mov(pc, tgsi_dst(pc, i, fd), reg);
free_temp(pc, reg);
}
return TRUE;
}
static void
load_interpolant(struct nv50_pc *pc, struct nv50_reg *reg)
{
struct nv50_reg *iv, **ppiv;
unsigned mode = pc->interp_mode[reg->index];
ppiv = (mode & INTERP_CENTROID) ? &pc->iv_c : &pc->iv_p;
iv = *ppiv;
if ((mode & INTERP_PERSPECTIVE) && !iv) {
iv = *ppiv = alloc_temp(pc, NULL);
iv->rhw = popcnt4(pc->p->cfg.regs[1] >> 24) - 1;
emit_interp(pc, iv, NULL, mode & INTERP_CENTROID);
emit_flop(pc, 0, iv, iv);
/* XXX: when loading interpolants dynamically, move these
* to the program head, or make sure it can't be skipped.
*/
}
emit_interp(pc, reg, iv, mode);
}
static boolean
nv50_program_tx_prep(struct nv50_pc *pc)
{
struct tgsi_parse_context tp;
struct nv50_program *p = pc->p;
boolean ret = FALSE;
unsigned i, c, flat_nr = 0;
tgsi_parse_init(&tp, pc->p->pipe.tokens);
while (!tgsi_parse_end_of_tokens(&tp)) {
const union tgsi_full_token *tok = &tp.FullToken;
tgsi_parse_token(&tp);
switch (tok->Token.Type) {
case TGSI_TOKEN_TYPE_IMMEDIATE:
{
const struct tgsi_full_immediate *imm =
&tp.FullToken.FullImmediate;
ctor_immd(pc, imm->u[0].Float,
imm->u[1].Float,
imm->u[2].Float,
imm->u[3].Float);
}
break;
case TGSI_TOKEN_TYPE_DECLARATION:
{
const struct tgsi_full_declaration *d;
unsigned si, last, first, mode;
d = &tp.FullToken.FullDeclaration;
first = d->DeclarationRange.First;
last = d->DeclarationRange.Last;
switch (d->Declaration.File) {
case TGSI_FILE_TEMPORARY:
break;
case TGSI_FILE_OUTPUT:
if (!d->Declaration.Semantic ||
p->type == PIPE_SHADER_FRAGMENT)
break;
si = d->Semantic.SemanticIndex;
switch (d->Semantic.SemanticName) {
case TGSI_SEMANTIC_BCOLOR:
p->cfg.two_side[si].hw = first;
if (p->cfg.io_nr > first)
p->cfg.io_nr = first;
break;
case TGSI_SEMANTIC_PSIZE:
p->cfg.psiz = first;
if (p->cfg.io_nr > first)
p->cfg.io_nr = first;
break;
/*
case TGSI_SEMANTIC_CLIP_DISTANCE:
p->cfg.clpd = MIN2(p->cfg.clpd, first);
break;
*/
default:
break;
}
break;
case TGSI_FILE_INPUT:
{
if (p->type != PIPE_SHADER_FRAGMENT)
break;
switch (d->Declaration.Interpolate) {
case TGSI_INTERPOLATE_CONSTANT:
mode = INTERP_FLAT;
flat_nr++;
break;
case TGSI_INTERPOLATE_PERSPECTIVE:
mode = INTERP_PERSPECTIVE;
p->cfg.regs[1] |= 0x08 << 24;
break;
default:
mode = INTERP_LINEAR;
break;
}
if (d->Declaration.Centroid)
mode |= INTERP_CENTROID;
assert(last < 32);
for (i = first; i <= last; i++)
pc->interp_mode[i] = mode;
}
break;
case TGSI_FILE_CONSTANT:
break;
case TGSI_FILE_SAMPLER:
break;
default:
NOUVEAU_ERR("bad decl file %d\n",
d->Declaration.File);
goto out_err;
}
}
break;
case TGSI_TOKEN_TYPE_INSTRUCTION:
pc->insn_nr++;
prep_inspect_insn(pc, &tok->FullInstruction);
break;
default:
break;
}
}
if (p->type == PIPE_SHADER_VERTEX) {
int rid = 0;
for (i = 0; i < pc->attr_nr * 4; ++i) {
if (pc->attr[i].acc) {
pc->attr[i].hw = rid++;
p->cfg.attr[i / 32] |= 1 << (i % 32);
}
}
for (i = 0, rid = 0; i < pc->result_nr; ++i) {
p->cfg.io[i].hw = rid;
p->cfg.io[i].id_vp = i;
for (c = 0; c < 4; ++c) {
int n = i * 4 + c;
if (!pc->result[n].acc)
continue;
pc->result[n].hw = rid++;
p->cfg.io[i].mask |= 1 << c;
}
}
for (c = 0; c < 2; ++c)
if (p->cfg.two_side[c].hw < 0x40)
p->cfg.two_side[c] = p->cfg.io[
p->cfg.two_side[c].hw];
if (p->cfg.psiz < 0x40)
p->cfg.psiz = p->cfg.io[p->cfg.psiz].hw;
} else
if (p->type == PIPE_SHADER_FRAGMENT) {
int rid, aid;
unsigned n = 0, m = pc->attr_nr - flat_nr;
int base = (TGSI_SEMANTIC_POSITION ==
p->info.input_semantic_name[0]) ? 0 : 1;
/* non-flat interpolants have to be mapped to
* the lower hardware IDs, so sort them:
*/
for (i = 0; i < pc->attr_nr; i++) {
if (pc->interp_mode[i] == INTERP_FLAT) {
p->cfg.io[m].id_vp = i + base;
p->cfg.io[m++].id_fp = i;
} else {
if (!(pc->interp_mode[i] & INTERP_PERSPECTIVE))
p->cfg.io[n].linear = TRUE;
p->cfg.io[n].id_vp = i + base;
p->cfg.io[n++].id_fp = i;
}
}
if (!base) /* set w-coordinate mask from perspective interp */
p->cfg.io[0].mask |= p->cfg.regs[1] >> 24;
aid = popcnt4( /* if fcrd isn't contained in cfg.io */
base ? (p->cfg.regs[1] >> 24) : p->cfg.io[0].mask);
for (n = 0; n < pc->attr_nr; ++n) {
p->cfg.io[n].hw = rid = aid;
i = p->cfg.io[n].id_fp;
for (c = 0; c < 4; ++c) {
if (!pc->attr[i * 4 + c].acc)
continue;
pc->attr[i * 4 + c].rhw = rid++;
p->cfg.io[n].mask |= 1 << c;
load_interpolant(pc, &pc->attr[i * 4 + c]);
}
aid += popcnt4(p->cfg.io[n].mask);
}
if (!base)
p->cfg.regs[1] |= p->cfg.io[0].mask << 24;
m = popcnt4(p->cfg.regs[1] >> 24);
/* set count of non-position inputs and of non-flat
* non-position inputs for FP_INTERPOLANT_CTRL
*/
p->cfg.regs[1] |= aid - m;
if (flat_nr) {
i = p->cfg.io[pc->attr_nr - flat_nr].hw;
p->cfg.regs[1] |= (i - m) << 16;
} else
p->cfg.regs[1] |= p->cfg.regs[1] << 16;
/* mark color semantic for light-twoside */
n = 0x40;
for (i = 0; i < pc->attr_nr; i++) {
ubyte si, sn;
sn = p->info.input_semantic_name[p->cfg.io[i].id_fp];
si = p->info.input_semantic_index[p->cfg.io[i].id_fp];
if (sn == TGSI_SEMANTIC_COLOR) {
p->cfg.two_side[si] = p->cfg.io[i];
/* increase colour count */
p->cfg.regs[0] += popcnt4(
p->cfg.two_side[si].mask) << 16;
n = MIN2(n, p->cfg.io[i].hw - m);
}
}
if (n < 0x40)
p->cfg.regs[0] += n;
/* Initialize FP results:
* FragDepth is always first TGSI and last hw output
*/
i = p->info.writes_z ? 4 : 0;
for (rid = 0; i < pc->result_nr * 4; i++)
pc->result[i].rhw = rid++;
if (p->info.writes_z)
pc->result[2].rhw = rid;
p->cfg.high_result = rid;
}
if (pc->immd_nr) {
int rid = 0;
pc->immd = MALLOC(pc->immd_nr * 4 * sizeof(struct nv50_reg));
if (!pc->immd)
goto out_err;
for (i = 0; i < pc->immd_nr; i++) {
for (c = 0; c < 4; c++, rid++)
ctor_reg(&pc->immd[rid], P_IMMD, i, rid);
}
}
ret = TRUE;
out_err:
if (pc->iv_p)
free_temp(pc, pc->iv_p);
if (pc->iv_c)
free_temp(pc, pc->iv_c);
tgsi_parse_free(&tp);
return ret;
}
static void
free_nv50_pc(struct nv50_pc *pc)
{
if (pc->immd)
FREE(pc->immd);
if (pc->param)
FREE(pc->param);
if (pc->result)
FREE(pc->result);
if (pc->attr)
FREE(pc->attr);
if (pc->temp)
FREE(pc->temp);
FREE(pc);
}
static boolean
ctor_nv50_pc(struct nv50_pc *pc, struct nv50_program *p)
{
int i, c;
unsigned rtype[2] = { P_ATTR, P_RESULT };
pc->p = p;
pc->temp_nr = p->info.file_max[TGSI_FILE_TEMPORARY] + 1;
pc->attr_nr = p->info.file_max[TGSI_FILE_INPUT] + 1;
pc->result_nr = p->info.file_max[TGSI_FILE_OUTPUT] + 1;
pc->param_nr = p->info.file_max[TGSI_FILE_CONSTANT] + 1;
p->cfg.high_temp = 4;
p->cfg.two_side[0].hw = 0x40;
p->cfg.two_side[1].hw = 0x40;
switch (p->type) {
case PIPE_SHADER_VERTEX:
p->cfg.psiz = 0x40;
p->cfg.clpd = 0x40;
p->cfg.io_nr = pc->result_nr;
break;
case PIPE_SHADER_FRAGMENT:
rtype[0] = rtype[1] = P_TEMP;
p->cfg.regs[0] = 0x01000004;
p->cfg.io_nr = pc->attr_nr;
if (p->info.writes_z) {
p->cfg.regs[2] |= 0x00000100;
p->cfg.regs[3] |= 0x00000011;
}
if (p->info.uses_kill)
p->cfg.regs[2] |= 0x00100000;
break;
}
if (pc->temp_nr) {
pc->temp = MALLOC(pc->temp_nr * 4 * sizeof(struct nv50_reg));
if (!pc->temp)
return FALSE;
for (i = 0; i < pc->temp_nr * 4; ++i)
ctor_reg(&pc->temp[i], P_TEMP, i / 4, -1);
}
if (pc->attr_nr) {
pc->attr = MALLOC(pc->attr_nr * 4 * sizeof(struct nv50_reg));
if (!pc->attr)
return FALSE;
for (i = 0; i < pc->attr_nr * 4; ++i)
ctor_reg(&pc->attr[i], rtype[0], i / 4, -1);
}
if (pc->result_nr) {
unsigned nr = pc->result_nr * 4;
pc->result = MALLOC(nr * sizeof(struct nv50_reg));
if (!pc->result)
return FALSE;
for (i = 0; i < nr; ++i)
ctor_reg(&pc->result[i], rtype[1], i / 4, -1);
}
if (pc->param_nr) {
int rid = 0;
pc->param = MALLOC(pc->param_nr * 4 * sizeof(struct nv50_reg));
if (!pc->param)
return FALSE;
for (i = 0; i < pc->param_nr; ++i)
for (c = 0; c < 4; ++c, ++rid)
ctor_reg(&pc->param[rid], P_CONST, i, rid);
}
return TRUE;
}
static void
nv50_fp_move_results(struct nv50_pc *pc)
{
struct nv50_reg reg;
unsigned i;
ctor_reg(®, P_TEMP, -1, -1);
for (i = 0; i < pc->result_nr * 4; ++i) {
if (pc->result[i].rhw < 0 || pc->result[i].hw < 0)
continue;
if (pc->result[i].rhw != pc->result[i].hw) {
reg.hw = pc->result[i].rhw;
emit_mov(pc, ®, &pc->result[i]);
}
}
}
static void
nv50_program_fixup_insns(struct nv50_pc *pc)
{
struct nv50_program_exec *e, *prev = NULL, **bra_list;
unsigned i, n, pos;
bra_list = CALLOC(pc->p->exec_size, sizeof(struct nv50_program_exec *));
/* Collect branch instructions, we need to adjust their offsets
* when converting 32 bit instructions to 64 bit ones
*/
for (n = 0, e = pc->p->exec_head; e; e = e->next)
if (e->param.index >= 0 && !e->param.mask)
bra_list[n++] = e;
/* Make sure we don't have any single 32 bit instructions. */
for (e = pc->p->exec_head, pos = 0; e; e = e->next) {
pos += is_long(e) ? 2 : 1;
if ((pos & 1) && (!e->next || is_long(e->next))) {
for (i = 0; i < n; ++i)
if (bra_list[i]->param.index >= pos)
bra_list[i]->param.index += 1;
convert_to_long(pc, e);
++pos;
}
if (e->next)
prev = e;
}
assert(!is_immd(pc->p->exec_head));
assert(!is_immd(pc->p->exec_tail));
/* last instruction must be long so it can have the end bit set */
if (!is_long(pc->p->exec_tail)) {
convert_to_long(pc, pc->p->exec_tail);
if (prev)
convert_to_long(pc, prev);
}
assert(!(pc->p->exec_tail->inst[1] & 2));
/* set the end-bit */
pc->p->exec_tail->inst[1] |= 1;
FREE(bra_list);
}
static boolean
nv50_program_tx(struct nv50_program *p)
{
struct tgsi_parse_context parse;
struct nv50_pc *pc;
boolean ret;
pc = CALLOC_STRUCT(nv50_pc);
if (!pc)
return FALSE;
ret = ctor_nv50_pc(pc, p);
if (ret == FALSE)
goto out_cleanup;
ret = nv50_program_tx_prep(pc);
if (ret == FALSE)
goto out_cleanup;
tgsi_parse_init(&parse, pc->p->pipe.tokens);
while (!tgsi_parse_end_of_tokens(&parse)) {
const union tgsi_full_token *tok = &parse.FullToken;
/* don't allow half insn/immd on first and last instruction */
pc->allow32 = TRUE;
if (pc->insn_cur == 0 || pc->insn_cur + 2 == pc->insn_nr)
pc->allow32 = FALSE;
tgsi_parse_token(&parse);
switch (tok->Token.Type) {
case TGSI_TOKEN_TYPE_INSTRUCTION:
++pc->insn_cur;
ret = nv50_tgsi_insn(pc, tok);
if (ret == FALSE)
goto out_err;
break;
default:
break;
}
}
if (pc->p->type == PIPE_SHADER_FRAGMENT)
nv50_fp_move_results(pc);
nv50_program_fixup_insns(pc);
p->param_nr = pc->param_nr * 4;
p->immd_nr = pc->immd_nr * 4;
p->immd = pc->immd_buf;
out_err:
tgsi_parse_free(&parse);
out_cleanup:
free_nv50_pc(pc);
return ret;
}
static void
nv50_program_validate(struct nv50_context *nv50, struct nv50_program *p)
{
if (nv50_program_tx(p) == FALSE)
assert(0);
p->translated = TRUE;
}
static void
nv50_program_upload_data(struct nv50_context *nv50, float *map,
unsigned start, unsigned count, unsigned cbuf)
{
struct nouveau_channel *chan = nv50->screen->base.channel;
struct nouveau_grobj *tesla = nv50->screen->tesla;
while (count) {
unsigned nr = count > 2047 ? 2047 : count;
BEGIN_RING(chan, tesla, NV50TCL_CB_ADDR, 1);
OUT_RING (chan, (cbuf << 0) | (start << 8));
BEGIN_RING(chan, tesla, NV50TCL_CB_DATA(0) | 0x40000000, nr);
OUT_RINGp (chan, map, nr);
map += nr;
start += nr;
count -= nr;
}
}
static void
nv50_program_validate_data(struct nv50_context *nv50, struct nv50_program *p)
{
struct pipe_screen *pscreen = nv50->pipe.screen;
if (!p->data[0] && p->immd_nr) {
struct nouveau_resource *heap = nv50->screen->immd_heap[0];
if (nouveau_resource_alloc(heap, p->immd_nr, p, &p->data[0])) {
while (heap->next && heap->size < p->immd_nr) {
struct nv50_program *evict = heap->next->priv;
nouveau_resource_free(&evict->data[0]);
}
if (nouveau_resource_alloc(heap, p->immd_nr, p,
&p->data[0]))
assert(0);
}
/* immediates only need to be uploaded again when freed */
nv50_program_upload_data(nv50, p->immd, p->data[0]->start,
p->immd_nr, NV50_CB_PMISC);
}
assert(p->param_nr <= 128);
if (p->param_nr) {
unsigned cb;
float *map = pipe_buffer_map(pscreen, nv50->constbuf[p->type],
PIPE_BUFFER_USAGE_CPU_READ);
if (p->type == PIPE_SHADER_VERTEX)
cb = NV50_CB_PVP;
else
cb = NV50_CB_PFP;
nv50_program_upload_data(nv50, map, 0, p->param_nr, cb);
pipe_buffer_unmap(pscreen, nv50->constbuf[p->type]);
}
}
static void
nv50_program_validate_code(struct nv50_context *nv50, struct nv50_program *p)
{
struct nouveau_channel *chan = nv50->screen->base.channel;
struct nouveau_grobj *tesla = nv50->screen->tesla;
struct nv50_program_exec *e;
struct nouveau_stateobj *so;
const unsigned flags = NOUVEAU_BO_VRAM | NOUVEAU_BO_WR;
unsigned start, count, *up, *ptr;
boolean upload = FALSE;
if (!p->bo) {
nouveau_bo_new(chan->device, NOUVEAU_BO_VRAM, 0x100,
p->exec_size * 4, &p->bo);
upload = TRUE;
}
if (p->data[0] && p->data[0]->start != p->data_start[0])
upload = TRUE;
if (!upload)
return;
for (e = p->exec_head; e; e = e->next) {
unsigned ei, ci, bs;
if (e->param.index < 0)
continue;
if (e->param.mask == 0) {
assert(!(e->param.index & 1));
/* seem to be 8 byte steps */
ei = (e->param.index >> 1) + 0 /* START_ID */;
e->inst[0] &= 0xf0000fff;
e->inst[0] |= ei << 12;
continue;
}
bs = (e->inst[1] >> 22) & 0x07;
assert(bs < 2);
ei = e->param.shift >> 5;
ci = e->param.index;
if (bs == 0)
ci += p->data[bs]->start;
e->inst[ei] &= ~e->param.mask;
e->inst[ei] |= (ci << e->param.shift);
}
if (p->data[0])
p->data_start[0] = p->data[0]->start;
#ifdef NV50_PROGRAM_DUMP
NOUVEAU_ERR("-------\n");
for (e = p->exec_head; e; e = e->next) {
NOUVEAU_ERR("0x%08x\n", e->inst[0]);
if (is_long(e))
NOUVEAU_ERR("0x%08x\n", e->inst[1]);
}
#endif
up = ptr = MALLOC(p->exec_size * 4);
for (e = p->exec_head; e; e = e->next) {
*(ptr++) = e->inst[0];
if (is_long(e))
*(ptr++) = e->inst[1];
}
so = so_new(4,2);
so_method(so, nv50->screen->tesla, NV50TCL_CB_DEF_ADDRESS_HIGH, 3);
so_reloc (so, p->bo, 0, flags | NOUVEAU_BO_HIGH, 0, 0);
so_reloc (so, p->bo, 0, flags | NOUVEAU_BO_LOW, 0, 0);
so_data (so, (NV50_CB_PUPLOAD << 16) | 0x0800); //(p->exec_size * 4));
start = 0; count = p->exec_size;
while (count) {
struct nouveau_channel *chan = nv50->screen->base.channel;
unsigned nr;
so_emit(chan, so);
nr = MIN2(count, 2047);
nr = MIN2(chan->pushbuf->remaining, nr);
if (chan->pushbuf->remaining < (nr + 3)) {
FIRE_RING(chan);
continue;
}
BEGIN_RING(chan, tesla, NV50TCL_CB_ADDR, 1);
OUT_RING (chan, (start << 8) | NV50_CB_PUPLOAD);
BEGIN_RING(chan, tesla, NV50TCL_CB_DATA(0) | 0x40000000, nr);
OUT_RINGp (chan, up + start, nr);
start += nr;
count -= nr;
}
FREE(up);
so_ref(NULL, &so);
}
void
nv50_vertprog_validate(struct nv50_context *nv50)
{
struct nouveau_grobj *tesla = nv50->screen->tesla;
struct nv50_program *p = nv50->vertprog;
struct nouveau_stateobj *so;
if (!p->translated) {
nv50_program_validate(nv50, p);
if (!p->translated)
assert(0);
}
nv50_program_validate_data(nv50, p);
nv50_program_validate_code(nv50, p);
so = so_new(13, 2);
so_method(so, tesla, NV50TCL_VP_ADDRESS_HIGH, 2);
so_reloc (so, p->bo, 0, NOUVEAU_BO_VRAM | NOUVEAU_BO_RD |
NOUVEAU_BO_HIGH, 0, 0);
so_reloc (so, p->bo, 0, NOUVEAU_BO_VRAM | NOUVEAU_BO_RD |
NOUVEAU_BO_LOW, 0, 0);
so_method(so, tesla, NV50TCL_VP_ATTR_EN_0, 2);
so_data (so, p->cfg.attr[0]);
so_data (so, p->cfg.attr[1]);
so_method(so, tesla, NV50TCL_VP_REG_ALLOC_RESULT, 1);
so_data (so, p->cfg.high_result);
so_method(so, tesla, NV50TCL_VP_RESULT_MAP_SIZE, 2);
so_data (so, p->cfg.high_result); //8);
so_data (so, p->cfg.high_temp);
so_method(so, tesla, NV50TCL_VP_START_ID, 1);
so_data (so, 0); /* program start offset */
so_ref(so, &nv50->state.vertprog);
so_ref(NULL, &so);
}
void
nv50_fragprog_validate(struct nv50_context *nv50)
{
struct nouveau_grobj *tesla = nv50->screen->tesla;
struct nv50_program *p = nv50->fragprog;
struct nouveau_stateobj *so;
if (!p->translated) {
nv50_program_validate(nv50, p);
if (!p->translated)
assert(0);
}
nv50_program_validate_data(nv50, p);
nv50_program_validate_code(nv50, p);
so = so_new(64, 2);
so_method(so, tesla, NV50TCL_FP_ADDRESS_HIGH, 2);
so_reloc (so, p->bo, 0, NOUVEAU_BO_VRAM | NOUVEAU_BO_RD |
NOUVEAU_BO_HIGH, 0, 0);
so_reloc (so, p->bo, 0, NOUVEAU_BO_VRAM | NOUVEAU_BO_RD |
NOUVEAU_BO_LOW, 0, 0);
so_method(so, tesla, NV50TCL_FP_REG_ALLOC_TEMP, 1);
so_data (so, p->cfg.high_temp);
so_method(so, tesla, NV50TCL_FP_RESULT_COUNT, 1);
so_data (so, p->cfg.high_result);
so_method(so, tesla, NV50TCL_FP_CTRL_UNK19A8, 1);
so_data (so, p->cfg.regs[2]);
so_method(so, tesla, NV50TCL_FP_CTRL_UNK196C, 1);
so_data (so, p->cfg.regs[3]);
so_method(so, tesla, NV50TCL_FP_START_ID, 1);
so_data (so, 0); /* program start offset */
so_ref(so, &nv50->state.fragprog);
so_ref(NULL, &so);
}
static void
nv50_pntc_replace(struct nv50_context *nv50, uint32_t pntc[8], unsigned base)
{
struct nv50_program *fp = nv50->fragprog;
struct nv50_program *vp = nv50->vertprog;
unsigned i, c, m = base;
/* XXX: This can't work correctly in all cases yet, we either
* have to create TGSI_SEMANTIC_PNTC or sprite_coord_mode has
* to be per FP input instead of per VP output
*/
memset(pntc, 0, 8 * sizeof(uint32_t));
for (i = 0; i < fp->cfg.io_nr; i++) {
uint8_t sn, si;
uint8_t j = fp->cfg.io[i].id_vp, k = fp->cfg.io[i].id_fp;
unsigned n = popcnt4(fp->cfg.io[i].mask);
if (fp->info.input_semantic_name[k] != TGSI_SEMANTIC_GENERIC) {
m += n;
continue;
}
sn = vp->info.input_semantic_name[j];
si = vp->info.input_semantic_index[j];
if (j < fp->cfg.io_nr && sn == TGSI_SEMANTIC_GENERIC) {
ubyte mode =
nv50->rasterizer->pipe.sprite_coord_mode[si];
if (mode == PIPE_SPRITE_COORD_NONE) {
m += n;
continue;
}
}
/* this is either PointCoord or replaced by sprite coords */
for (c = 0; c < 4; c++) {
if (!(fp->cfg.io[i].mask & (1 << c)))
continue;
pntc[m / 8] |= (c + 1) << ((m % 8) * 4);
++m;
}
}
}
static int
nv50_sreg4_map(uint32_t *p_map, int mid, uint32_t lin[4],
struct nv50_sreg4 *fpi, struct nv50_sreg4 *vpo)
{
int c;
uint8_t mv = vpo->mask, mf = fpi->mask, oid = vpo->hw;
uint8_t *map = (uint8_t *)p_map;
for (c = 0; c < 4; ++c) {
if (mf & 1) {
if (fpi->linear == TRUE)
lin[mid / 32] |= 1 << (mid % 32);
map[mid++] = (mv & 1) ? oid : ((c == 3) ? 0x41 : 0x40);
}
oid += mv & 1;
mf >>= 1;
mv >>= 1;
}
return mid;
}
void
nv50_linkage_validate(struct nv50_context *nv50)
{
struct nouveau_grobj *tesla = nv50->screen->tesla;
struct nv50_program *vp = nv50->vertprog;
struct nv50_program *fp = nv50->fragprog;
struct nouveau_stateobj *so;
struct nv50_sreg4 dummy, *vpo;
int i, n, c, m = 0;
uint32_t map[16], lin[4], reg[5], pcrd[8];
memset(map, 0, sizeof(map));
memset(lin, 0, sizeof(lin));
reg[1] = 0x00000004; /* low and high clip distance map ids */
reg[2] = 0x00000000; /* layer index map id (disabled, GP only) */
reg[3] = 0x00000000; /* point size map id & enable */
reg[0] = fp->cfg.regs[0]; /* colour semantic reg */
reg[4] = fp->cfg.regs[1]; /* interpolant info */
dummy.linear = FALSE;
dummy.mask = 0xf; /* map all components of HPOS */
m = nv50_sreg4_map(map, m, lin, &dummy, &vp->cfg.io[0]);
dummy.mask = 0x0;
if (vp->cfg.clpd < 0x40) {
for (c = 0; c < vp->cfg.clpd_nr; ++c)
map[m++] = vp->cfg.clpd + c;
reg[1] = (m << 8);
}
reg[0] |= m << 8; /* adjust BFC0 id */
/* if light_twoside is active, it seems FFC0_ID == BFC0_ID is bad */
if (nv50->rasterizer->pipe.light_twoside) {
vpo = &vp->cfg.two_side[0];
m = nv50_sreg4_map(map, m, lin, &fp->cfg.two_side[0], &vpo[0]);
m = nv50_sreg4_map(map, m, lin, &fp->cfg.two_side[1], &vpo[1]);
}
reg[0] += m - 4; /* adjust FFC0 id */
reg[4] |= m << 8; /* set mid where 'normal' FP inputs start */
i = 0;
if (fp->info.input_semantic_name[0] == TGSI_SEMANTIC_POSITION)
i = 1;
for (; i < fp->cfg.io_nr; i++) {
ubyte sn = fp->info.input_semantic_name[fp->cfg.io[i].id_fp];
ubyte si = fp->info.input_semantic_index[fp->cfg.io[i].id_fp];
n = fp->cfg.io[i].id_vp;
if (n >= vp->cfg.io_nr ||
vp->info.output_semantic_name[n] != sn ||
vp->info.output_semantic_index[n] != si)
vpo = &dummy;
else
vpo = &vp->cfg.io[n];
m = nv50_sreg4_map(map, m, lin, &fp->cfg.io[i], vpo);
}
if (nv50->rasterizer->pipe.point_size_per_vertex) {
map[m / 4] |= vp->cfg.psiz << ((m % 4) * 8);
reg[3] = (m++ << 4) | 1;
}
/* now fill the stateobj */
so = so_new(64, 0);
n = (m + 3) / 4;
so_method(so, tesla, NV50TCL_VP_RESULT_MAP_SIZE, 1);
so_data (so, m);
so_method(so, tesla, NV50TCL_VP_RESULT_MAP(0), n);
so_datap (so, map, n);
so_method(so, tesla, NV50TCL_MAP_SEMANTIC_0, 4);
so_datap (so, reg, 4);
so_method(so, tesla, NV50TCL_FP_INTERPOLANT_CTRL, 1);
so_data (so, reg[4]);
so_method(so, tesla, 0x1540, 4);
so_datap (so, lin, 4);
if (nv50->rasterizer->pipe.point_sprite) {
nv50_pntc_replace(nv50, pcrd, (reg[4] >> 8) & 0xff);
so_method(so, tesla, NV50TCL_POINT_COORD_REPLACE_MAP(0), 8);
so_datap (so, pcrd, 8);
}
so_ref(so, &nv50->state.programs);
so_ref(NULL, &so);
}
void
nv50_program_destroy(struct nv50_context *nv50, struct nv50_program *p)
{
while (p->exec_head) {
struct nv50_program_exec *e = p->exec_head;
p->exec_head = e->next;
FREE(e);
}
p->exec_tail = NULL;
p->exec_size = 0;
nouveau_bo_ref(NULL, &p->bo);
nouveau_resource_free(&p->data[0]);
p->translated = 0;
}
|