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

/**
 * @file crocus_program.c
 *
 * This file contains the driver interface for compiling shaders.
 *
 * See crocus_program_cache.c for the in-memory program cache where the
 * compiled shaders are stored.
 */

#include <stdio.h>
#include <errno.h>
#include "pipe/p_defines.h"
#include "pipe/p_state.h"
#include "pipe/p_context.h"
#include "pipe/p_screen.h"
#include "util/u_atomic.h"
#include "util/u_upload_mgr.h"
#include "util/debug.h"
#include "util/u_prim.h"
#include "compiler/nir/nir.h"
#include "compiler/nir/nir_builder.h"
#include "compiler/nir/nir_serialize.h"
#include "intel/compiler/brw_compiler.h"
#include "intel/compiler/brw_nir.h"
#include "crocus_context.h"
#include "nir/tgsi_to_nir.h"

#define KEY_INIT_NO_ID()                              \
   .base.subgroup_size_type = BRW_SUBGROUP_SIZE_UNIFORM, \
   .base.tex.swizzles[0 ... MAX_SAMPLERS - 1] = 0x688,   \
   .base.tex.compressed_multisample_layout_mask = ~0
#define KEY_INIT() .base.program_string_id = ish->program_id, KEY_INIT_NO_ID()

static void
crocus_sanitize_tex_key(struct brw_sampler_prog_key_data *key)
{
   key->gather_channel_quirk_mask = 0;
   for (unsigned s = 0; s < MAX_SAMPLERS; s++) {
      key->swizzles[s] = SWIZZLE_NOOP;
      key->gfx6_gather_wa[s] = 0;
   }
}

static uint32_t
crocus_get_texture_swizzle(const struct crocus_context *ice,
                           const struct crocus_sampler_view *t)
{
   uint32_t swiz = 0;

   for (int i = 0; i < 4; i++) {
      swiz |= t->swizzle[i] << (i * 3);
   }
   return swiz;
}

static inline bool can_push_ubo(const struct intel_device_info *devinfo)
{
   /* push works for everyone except SNB at the moment */
   return devinfo->ver != 6;
}

static uint8_t
gfx6_gather_workaround(enum pipe_format pformat)
{
   switch (pformat) {
   case PIPE_FORMAT_R8_SINT: return WA_SIGN | WA_8BIT;
   case PIPE_FORMAT_R8_UINT: return WA_8BIT;
   case PIPE_FORMAT_R16_SINT: return WA_SIGN | WA_16BIT;
   case PIPE_FORMAT_R16_UINT: return WA_16BIT;
   default:
      /* Note that even though PIPE_FORMAT_R32_SINT and
       * PIPE_FORMAT_R32_UINThave format overrides in
       * the surface state, there is no shader w/a required.
       */
      return 0;
   }
}

static const unsigned crocus_gfx6_swizzle_for_offset[4] = {
   BRW_SWIZZLE4(0, 1, 2, 3),
   BRW_SWIZZLE4(1, 2, 3, 3),
   BRW_SWIZZLE4(2, 3, 3, 3),
   BRW_SWIZZLE4(3, 3, 3, 3)
};

static void
gfx6_gs_xfb_setup(const struct pipe_stream_output_info *so_info,
                  struct brw_gs_prog_data *gs_prog_data)
{
   /* Make sure that the VUE slots won't overflow the unsigned chars in
    * prog_data->transform_feedback_bindings[].
    */
   STATIC_ASSERT(BRW_VARYING_SLOT_COUNT <= 256);

   /* Make sure that we don't need more binding table entries than we've
    * set aside for use in transform feedback.  (We shouldn't, since we
    * set aside enough binding table entries to have one per component).
    */
   assert(so_info->num_outputs <= BRW_MAX_SOL_BINDINGS);

   gs_prog_data->num_transform_feedback_bindings = so_info->num_outputs;
   for (unsigned i = 0; i < so_info->num_outputs; i++) {
      gs_prog_data->transform_feedback_bindings[i] =
         so_info->output[i].register_index;
      gs_prog_data->transform_feedback_swizzles[i] =
         crocus_gfx6_swizzle_for_offset[so_info->output[i].start_component];
   }
}

static void
gfx6_ff_gs_xfb_setup(const struct pipe_stream_output_info *so_info,
                     struct brw_ff_gs_prog_key *key)
{
   key->num_transform_feedback_bindings = so_info->num_outputs;
   for (unsigned i = 0; i < so_info->num_outputs; i++) {
      key->transform_feedback_bindings[i] =
         so_info->output[i].register_index;
      key->transform_feedback_swizzles[i] =
         crocus_gfx6_swizzle_for_offset[so_info->output[i].start_component];
   }
}

static void
crocus_populate_sampler_prog_key_data(struct crocus_context *ice,
                                      const struct intel_device_info *devinfo,
                                      gl_shader_stage stage,
                                      struct crocus_uncompiled_shader *ish,
                                      bool uses_texture_gather,
                                      struct brw_sampler_prog_key_data *key)
{
   uint32_t mask = ish->nir->info.textures_used[0];

   while (mask) {
      const int s = u_bit_scan(&mask);

      struct crocus_sampler_view *texture = ice->state.shaders[stage].textures[s];
      key->swizzles[s] = SWIZZLE_NOOP;
      key->scale_factors[s] = 0.0f;

      if (!texture)
         continue;
      if (texture->base.target == PIPE_BUFFER)
         continue;
      if (!devinfo->is_haswell) {
         key->swizzles[s] = crocus_get_texture_swizzle(ice, texture);
      }

      /* gather4 for RG32* is broken in multiple ways on Gen7. */
      if (devinfo->ver == 7 && uses_texture_gather) {
         switch (texture->base.format) {
         case PIPE_FORMAT_R32G32_UINT:
         case PIPE_FORMAT_R32G32_SINT: {
            /* We have to override the format to R32G32_FLOAT_LD.
             * This means that SCS_ALPHA and SCS_ONE will return 0x3f8
             * (1.0) rather than integer 1.  This needs shader hacks.
             *
             * On Ivybridge, we whack W (alpha) to ONE in our key's
             * swizzle.  On Haswell, we look at the original texture
             * swizzle, and use XYZW with channels overridden to ONE,
             * leaving normal texture swizzling to SCS.
             */
            unsigned src_swizzle = key->swizzles[s];
            for (int i = 0; i < 4; i++) {
               unsigned src_comp = GET_SWZ(src_swizzle, i);
               if (src_comp == SWIZZLE_ONE || src_comp == SWIZZLE_W) {
                  key->swizzles[i] &= ~(0x7 << (3 * i));
                  key->swizzles[i] |= SWIZZLE_ONE << (3 * i);
               }
            }
         }
         FALLTHROUGH;
         case PIPE_FORMAT_R32G32_FLOAT:
            /* The channel select for green doesn't work - we have to
             * request blue.  Haswell can use SCS for this, but Ivybridge
             * needs a shader workaround.
             */
            if (!devinfo->is_haswell)
               key->gather_channel_quirk_mask |= 1 << s;
            break;
         default:
            break;
         }
      }
      if (devinfo->ver == 6 && uses_texture_gather) {
         key->gfx6_gather_wa[s] = gfx6_gather_workaround(texture->base.format);
      }
   }
}

static void
crocus_lower_swizzles(struct nir_shader *nir,
                      const struct brw_sampler_prog_key_data *key_tex)
{
   struct nir_lower_tex_options tex_options = { 0 };
   uint32_t mask = nir->info.textures_used[0];

   while (mask) {
      const int s = u_bit_scan(&mask);

      if (key_tex->swizzles[s] == SWIZZLE_NOOP)
         continue;

      tex_options.swizzle_result |= (1 << s);
      for (unsigned c = 0; c < 4; c++)
         tex_options.swizzles[s][c] = GET_SWZ(key_tex->swizzles[s], c);
   }
   if (tex_options.swizzle_result)
      nir_lower_tex(nir, &tex_options);
}

static unsigned
get_new_program_id(struct crocus_screen *screen)
{
   return p_atomic_inc_return(&screen->program_id);
}

static nir_ssa_def *
get_aoa_deref_offset(nir_builder *b,
                     nir_deref_instr *deref,
                     unsigned elem_size)
{
   unsigned array_size = elem_size;
   nir_ssa_def *offset = nir_imm_int(b, 0);

   while (deref->deref_type != nir_deref_type_var) {
      assert(deref->deref_type == nir_deref_type_array);

      /* This level's element size is the previous level's array size */
      nir_ssa_def *index = nir_ssa_for_src(b, deref->arr.index, 1);
      assert(deref->arr.index.ssa);
      offset = nir_iadd(b, offset,
                        nir_imul(b, index, nir_imm_int(b, array_size)));

      deref = nir_deref_instr_parent(deref);
      assert(glsl_type_is_array(deref->type));
      array_size *= glsl_get_length(deref->type);
   }

   /* Accessing an invalid surface index with the dataport can result in a
    * hang.  According to the spec "if the index used to select an individual
    * element is negative or greater than or equal to the size of the array,
    * the results of the operation are undefined but may not lead to
    * termination" -- which is one of the possible outcomes of the hang.
    * Clamp the index to prevent access outside of the array bounds.
    */
   return nir_umin(b, offset, nir_imm_int(b, array_size - elem_size));
}

static void
crocus_lower_storage_image_derefs(nir_shader *nir)
{
   nir_function_impl *impl = nir_shader_get_entrypoint(nir);

   nir_builder b;
   nir_builder_init(&b, impl);

   nir_foreach_block(block, impl) {
      nir_foreach_instr_safe(instr, block) {
         if (instr->type != nir_instr_type_intrinsic)
            continue;

         nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
         switch (intrin->intrinsic) {
         case nir_intrinsic_image_deref_load:
         case nir_intrinsic_image_deref_store:
         case nir_intrinsic_image_deref_atomic_add:
         case nir_intrinsic_image_deref_atomic_imin:
         case nir_intrinsic_image_deref_atomic_umin:
         case nir_intrinsic_image_deref_atomic_imax:
         case nir_intrinsic_image_deref_atomic_umax:
         case nir_intrinsic_image_deref_atomic_and:
         case nir_intrinsic_image_deref_atomic_or:
         case nir_intrinsic_image_deref_atomic_xor:
         case nir_intrinsic_image_deref_atomic_exchange:
         case nir_intrinsic_image_deref_atomic_comp_swap:
         case nir_intrinsic_image_deref_size:
         case nir_intrinsic_image_deref_samples:
         case nir_intrinsic_image_deref_load_raw_intel:
         case nir_intrinsic_image_deref_store_raw_intel: {
            nir_deref_instr *deref = nir_src_as_deref(intrin->src[0]);
            nir_variable *var = nir_deref_instr_get_variable(deref);

            b.cursor = nir_before_instr(&intrin->instr);
            nir_ssa_def *index =
               nir_iadd(&b, nir_imm_int(&b, var->data.driver_location),
                        get_aoa_deref_offset(&b, deref, 1));
            nir_rewrite_image_intrinsic(intrin, index, false);
            break;
         }

         default:
            break;
         }
      }
   }
}

// XXX: need unify_interfaces() at link time...

/**
 * Undo nir_lower_passthrough_edgeflags but keep the inputs_read flag.
 */
static bool
crocus_fix_edge_flags(nir_shader *nir)
{
   if (nir->info.stage != MESA_SHADER_VERTEX) {
      nir_shader_preserve_all_metadata(nir);
      return false;
   }

   nir_variable *var = nir_find_variable_with_location(nir, nir_var_shader_out,
                                                       VARYING_SLOT_EDGE);
   if (!var) {
      nir_shader_preserve_all_metadata(nir);
      return false;
   }

   var->data.mode = nir_var_shader_temp;
   nir->info.outputs_written &= ~VARYING_BIT_EDGE;
   nir->info.inputs_read &= ~VERT_BIT_EDGEFLAG;
   nir_fixup_deref_modes(nir);

   nir_foreach_function(f, nir) {
      if (f->impl) {
         nir_metadata_preserve(f->impl, nir_metadata_block_index |
                               nir_metadata_dominance |
                               nir_metadata_live_ssa_defs |
                               nir_metadata_loop_analysis);
      } else {
         nir_metadata_preserve(f->impl, nir_metadata_all);
      }
   }

   return true;
}

/**
 * Fix an uncompiled shader's stream output info.
 *
 * Core Gallium stores output->register_index as a "slot" number, where
 * slots are assigned consecutively to all outputs in info->outputs_written.
 * This naive packing of outputs doesn't work for us - we too have slots,
 * but the layout is defined by the VUE map, which we won't have until we
 * compile a specific shader variant.  So, we remap these and simply store
 * VARYING_SLOT_* in our copy's output->register_index fields.
 *
 * We also fix up VARYING_SLOT_{LAYER,VIEWPORT,PSIZ} to select the Y/Z/W
 * components of our VUE header.  See brw_vue_map.c for the layout.
 */
static void
update_so_info(struct pipe_stream_output_info *so_info,
               uint64_t outputs_written)
{
   uint8_t reverse_map[64] = {};
   unsigned slot = 0;
   while (outputs_written) {
      reverse_map[slot++] = u_bit_scan64(&outputs_written);
   }

   for (unsigned i = 0; i < so_info->num_outputs; i++) {
      struct pipe_stream_output *output = &so_info->output[i];

      /* Map Gallium's condensed "slots" back to real VARYING_SLOT_* enums */
      output->register_index = reverse_map[output->register_index];

      /* The VUE header contains three scalar fields packed together:
       * - gl_PointSize is stored in VARYING_SLOT_PSIZ.w
       * - gl_Layer is stored in VARYING_SLOT_PSIZ.y
       * - gl_ViewportIndex is stored in VARYING_SLOT_PSIZ.z
       */
      switch (output->register_index) {
      case VARYING_SLOT_LAYER:
         assert(output->num_components == 1);
         output->register_index = VARYING_SLOT_PSIZ;
         output->start_component = 1;
         break;
      case VARYING_SLOT_VIEWPORT:
         assert(output->num_components == 1);
         output->register_index = VARYING_SLOT_PSIZ;
         output->start_component = 2;
         break;
      case VARYING_SLOT_PSIZ:
         assert(output->num_components == 1);
         output->start_component = 3;
         break;
      }

      //info->outputs_written |= 1ull << output->register_index;
   }
}

static void
setup_vec4_image_sysval(uint32_t *sysvals, uint32_t idx,
                        unsigned offset, unsigned n)
{
   assert(offset % sizeof(uint32_t) == 0);

   for (unsigned i = 0; i < n; ++i)
      sysvals[i] = BRW_PARAM_IMAGE(idx, offset / sizeof(uint32_t) + i);

   for (unsigned i = n; i < 4; ++i)
      sysvals[i] = BRW_PARAM_BUILTIN_ZERO;
}

/**
 * Associate NIR uniform variables with the prog_data->param[] mechanism
 * used by the backend.  Also, decide which UBOs we'd like to push in an
 * ideal situation (though the backend can reduce this).
 */
static void
crocus_setup_uniforms(const struct brw_compiler *compiler,
                      void *mem_ctx,
                      nir_shader *nir,
                      struct brw_stage_prog_data *prog_data,
                      enum brw_param_builtin **out_system_values,
                      unsigned *out_num_system_values,
                      unsigned *out_num_cbufs)
{
   UNUSED const struct intel_device_info *devinfo = compiler->devinfo;

   const unsigned CROCUS_MAX_SYSTEM_VALUES =
      PIPE_MAX_SHADER_IMAGES * BRW_IMAGE_PARAM_SIZE;
   enum brw_param_builtin *system_values =
      rzalloc_array(mem_ctx, enum brw_param_builtin, CROCUS_MAX_SYSTEM_VALUES);
   unsigned num_system_values = 0;

   unsigned patch_vert_idx = -1;
   unsigned ucp_idx[CROCUS_MAX_CLIP_PLANES];
   unsigned img_idx[PIPE_MAX_SHADER_IMAGES];
   unsigned variable_group_size_idx = -1;
   memset(ucp_idx, -1, sizeof(ucp_idx));
   memset(img_idx, -1, sizeof(img_idx));

   nir_function_impl *impl = nir_shader_get_entrypoint(nir);

   nir_builder b;
   nir_builder_init(&b, impl);

   b.cursor = nir_before_block(nir_start_block(impl));
   nir_ssa_def *temp_ubo_name = nir_ssa_undef(&b, 1, 32);
   nir_ssa_def *temp_const_ubo_name = NULL;

   /* Turn system value intrinsics into uniforms */
   nir_foreach_block(block, impl) {
      nir_foreach_instr_safe(instr, block) {
         if (instr->type != nir_instr_type_intrinsic)
            continue;

         nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
         nir_ssa_def *offset;

         switch (intrin->intrinsic) {
         case nir_intrinsic_load_constant: {
            /* This one is special because it reads from the shader constant
             * data and not cbuf0 which gallium uploads for us.
             */
            b.cursor = nir_before_instr(instr);
            nir_ssa_def *offset =
               nir_iadd_imm(&b, nir_ssa_for_src(&b, intrin->src[0], 1),
                            nir_intrinsic_base(intrin));

            if (temp_const_ubo_name == NULL)
               temp_const_ubo_name = nir_imm_int(&b, 0);

            nir_intrinsic_instr *load_ubo =
               nir_intrinsic_instr_create(b.shader, nir_intrinsic_load_ubo);
            load_ubo->num_components = intrin->num_components;
            load_ubo->src[0] = nir_src_for_ssa(temp_const_ubo_name);
            load_ubo->src[1] = nir_src_for_ssa(offset);
            nir_intrinsic_set_align(load_ubo, 4, 0);
            nir_intrinsic_set_range_base(load_ubo, 0);
            nir_intrinsic_set_range(load_ubo, ~0);
            nir_ssa_dest_init(&load_ubo->instr, &load_ubo->dest,
                              intrin->dest.ssa.num_components,
                              intrin->dest.ssa.bit_size,
                              intrin->dest.ssa.name);
            nir_builder_instr_insert(&b, &load_ubo->instr);

            nir_ssa_def_rewrite_uses(&intrin->dest.ssa,
                                     &load_ubo->dest.ssa);
            nir_instr_remove(&intrin->instr);
            continue;
         }
         case nir_intrinsic_load_user_clip_plane: {
            unsigned ucp = nir_intrinsic_ucp_id(intrin);

            if (ucp_idx[ucp] == -1) {
               ucp_idx[ucp] = num_system_values;
               num_system_values += 4;
            }

            for (int i = 0; i < 4; i++) {
               system_values[ucp_idx[ucp] + i] =
                  BRW_PARAM_BUILTIN_CLIP_PLANE(ucp, i);
            }

            b.cursor = nir_before_instr(instr);
            offset = nir_imm_int(&b, ucp_idx[ucp] * sizeof(uint32_t));
            break;
         }
         case nir_intrinsic_load_patch_vertices_in:
            if (patch_vert_idx == -1)
               patch_vert_idx = num_system_values++;

            system_values[patch_vert_idx] =
               BRW_PARAM_BUILTIN_PATCH_VERTICES_IN;

            b.cursor = nir_before_instr(instr);
            offset = nir_imm_int(&b, patch_vert_idx * sizeof(uint32_t));
            break;
         case nir_intrinsic_image_deref_load_param_intel: {
            assert(devinfo->ver < 9);
            nir_deref_instr *deref = nir_src_as_deref(intrin->src[0]);
            nir_variable *var = nir_deref_instr_get_variable(deref);

            if (img_idx[var->data.binding] == -1) {
               /* GL only allows arrays of arrays of images. */
               assert(glsl_type_is_image(glsl_without_array(var->type)));
               unsigned num_images = MAX2(1, glsl_get_aoa_size(var->type));

               for (int i = 0; i < num_images; i++) {
                  const unsigned img = var->data.binding + i;

                  img_idx[img] = num_system_values;
                  num_system_values += BRW_IMAGE_PARAM_SIZE;

                  uint32_t *img_sv = &system_values[img_idx[img]];

                  setup_vec4_image_sysval(
                     img_sv + BRW_IMAGE_PARAM_OFFSET_OFFSET, img,
                     offsetof(struct brw_image_param, offset), 2);
                  setup_vec4_image_sysval(
                     img_sv + BRW_IMAGE_PARAM_SIZE_OFFSET, img,
                     offsetof(struct brw_image_param, size), 3);
                  setup_vec4_image_sysval(
                     img_sv + BRW_IMAGE_PARAM_STRIDE_OFFSET, img,
                     offsetof(struct brw_image_param, stride), 4);
                  setup_vec4_image_sysval(
                     img_sv + BRW_IMAGE_PARAM_TILING_OFFSET, img,
                     offsetof(struct brw_image_param, tiling), 3);
                  setup_vec4_image_sysval(
                     img_sv + BRW_IMAGE_PARAM_SWIZZLING_OFFSET, img,
                     offsetof(struct brw_image_param, swizzling), 2);
               }
            }

            b.cursor = nir_before_instr(instr);
            offset = nir_iadd(&b,
                              get_aoa_deref_offset(&b, deref, BRW_IMAGE_PARAM_SIZE * 4),
                              nir_imm_int(&b, img_idx[var->data.binding] * 4 +
                                          nir_intrinsic_base(intrin) * 16));
            break;
         }
         case nir_intrinsic_load_workgroup_size: {
            assert(nir->info.workgroup_size_variable);
            if (variable_group_size_idx == -1) {
               variable_group_size_idx = num_system_values;
               num_system_values += 3;
               for (int i = 0; i < 3; i++) {
                  system_values[variable_group_size_idx + i] =
                     BRW_PARAM_BUILTIN_WORK_GROUP_SIZE_X + i;
               }
            }

            b.cursor = nir_before_instr(instr);
            offset = nir_imm_int(&b,
                                 variable_group_size_idx * sizeof(uint32_t));
            break;
         }
         default:
            continue;
         }

         unsigned comps = nir_intrinsic_dest_components(intrin);

         nir_intrinsic_instr *load =
            nir_intrinsic_instr_create(nir, nir_intrinsic_load_ubo);
         load->num_components = comps;
         load->src[0] = nir_src_for_ssa(temp_ubo_name);
         load->src[1] = nir_src_for_ssa(offset);
         nir_intrinsic_set_align(load, 4, 0);
         nir_intrinsic_set_range_base(load, 0);
         nir_intrinsic_set_range(load, ~0);
         nir_ssa_dest_init(&load->instr, &load->dest, comps, 32, NULL);
         nir_builder_instr_insert(&b, &load->instr);
         nir_ssa_def_rewrite_uses(&intrin->dest.ssa,
                                  &load->dest.ssa);
         nir_instr_remove(instr);
      }
   }

   nir_validate_shader(nir, "before remapping");

   /* Uniforms are stored in constant buffer 0, the
    * user-facing UBOs are indexed by one.  So if any constant buffer is
    * needed, the constant buffer 0 will be needed, so account for it.
    */
   unsigned num_cbufs = nir->info.num_ubos;
   if (num_cbufs || nir->num_uniforms)
      num_cbufs++;

   /* Place the new params in a new cbuf. */
   if (num_system_values > 0) {
      unsigned sysval_cbuf_index = num_cbufs;
      num_cbufs++;

      system_values = reralloc(mem_ctx, system_values, enum brw_param_builtin,
                               num_system_values);

      nir_foreach_block(block, impl) {
         nir_foreach_instr_safe(instr, block) {
            if (instr->type != nir_instr_type_intrinsic)
               continue;

            nir_intrinsic_instr *load = nir_instr_as_intrinsic(instr);

            if (load->intrinsic != nir_intrinsic_load_ubo)
               continue;

            b.cursor = nir_before_instr(instr);

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

            if (load->src[0].ssa == temp_ubo_name) {
               nir_ssa_def *imm = nir_imm_int(&b, sysval_cbuf_index);
               nir_instr_rewrite_src(instr, &load->src[0],
                                     nir_src_for_ssa(imm));
            }
         }
      }

      /* We need to fold the new iadds for brw_nir_analyze_ubo_ranges */
      nir_opt_constant_folding(nir);
   } else {
      ralloc_free(system_values);
      system_values = NULL;
   }

   assert(num_cbufs < PIPE_MAX_CONSTANT_BUFFERS);
   nir_validate_shader(nir, "after remap");

   /* We don't use params[] but gallium leaves num_uniforms set.  We use this
    * to detect when cbuf0 exists but we don't need it anymore when we get
    * here.  Instead, zero it out so that the back-end doesn't get confused
    * when nr_params * 4 != num_uniforms != nr_params * 4.
    */
   nir->num_uniforms = 0;

   /* Constant loads (if any) need to go at the end of the constant buffers so
    * we need to know num_cbufs before we can lower to them.
    */
   if (temp_const_ubo_name != NULL) {
      nir_load_const_instr *const_ubo_index =
         nir_instr_as_load_const(temp_const_ubo_name->parent_instr);
      assert(const_ubo_index->def.bit_size == 32);
      const_ubo_index->value[0].u32 = num_cbufs;
   }

   *out_system_values = system_values;
   *out_num_system_values = num_system_values;
   *out_num_cbufs = num_cbufs;
}

static const char *surface_group_names[] = {
   [CROCUS_SURFACE_GROUP_RENDER_TARGET]      = "render target",
   [CROCUS_SURFACE_GROUP_RENDER_TARGET_READ] = "non-coherent render target read",
   [CROCUS_SURFACE_GROUP_SOL]                = "streamout",
   [CROCUS_SURFACE_GROUP_CS_WORK_GROUPS]     = "CS work groups",
   [CROCUS_SURFACE_GROUP_TEXTURE]            = "texture",
   [CROCUS_SURFACE_GROUP_TEXTURE_GATHER]     = "texture gather",
   [CROCUS_SURFACE_GROUP_UBO]                = "ubo",
   [CROCUS_SURFACE_GROUP_SSBO]               = "ssbo",
   [CROCUS_SURFACE_GROUP_IMAGE]              = "image",
};

static void
crocus_print_binding_table(FILE *fp, const char *name,
                           const struct crocus_binding_table *bt)
{
   STATIC_ASSERT(ARRAY_SIZE(surface_group_names) == CROCUS_SURFACE_GROUP_COUNT);

   uint32_t total = 0;
   uint32_t compacted = 0;

   for (int i = 0; i < CROCUS_SURFACE_GROUP_COUNT; i++) {
      uint32_t size = bt->sizes[i];
      total += size;
      if (size)
         compacted += util_bitcount64(bt->used_mask[i]);
   }

   if (total == 0) {
      fprintf(fp, "Binding table for %s is empty\n\n", name);
      return;
   }

   if (total != compacted) {
      fprintf(fp, "Binding table for %s "
              "(compacted to %u entries from %u entries)\n",
              name, compacted, total);
   } else {
      fprintf(fp, "Binding table for %s (%u entries)\n", name, total);
   }

   uint32_t entry = 0;
   for (int i = 0; i < CROCUS_SURFACE_GROUP_COUNT; i++) {
      uint64_t mask = bt->used_mask[i];
      while (mask) {
         int index = u_bit_scan64(&mask);
         fprintf(fp, "  [%u] %s #%d\n", entry++, surface_group_names[i], index);
      }
   }
   fprintf(fp, "\n");
}

enum {
   /* Max elements in a surface group. */
   SURFACE_GROUP_MAX_ELEMENTS = 64,
};

/**
 * Map a <group, index> pair to a binding table index.
 *
 * For example: <UBO, 5> => binding table index 12
 */
uint32_t
crocus_group_index_to_bti(const struct crocus_binding_table *bt,
                          enum crocus_surface_group group, uint32_t index)
{
   assert(index < bt->sizes[group]);
   uint64_t mask = bt->used_mask[group];
   uint64_t bit = 1ull << index;
   if (bit & mask) {
      return bt->offsets[group] + util_bitcount64((bit - 1) & mask);
   } else {
      return CROCUS_SURFACE_NOT_USED;
   }
}

/**
 * Map a binding table index back to a <group, index> pair.
 *
 * For example: binding table index 12 => <UBO, 5>
 */
uint32_t
crocus_bti_to_group_index(const struct crocus_binding_table *bt,
                          enum crocus_surface_group group, uint32_t bti)
{
   uint64_t used_mask = bt->used_mask[group];
   assert(bti >= bt->offsets[group]);

   uint32_t c = bti - bt->offsets[group];
   while (used_mask) {
      int i = u_bit_scan64(&used_mask);
      if (c == 0)
         return i;
      c--;
   }

   return CROCUS_SURFACE_NOT_USED;
}

static void
rewrite_src_with_bti(nir_builder *b, struct crocus_binding_table *bt,
                     nir_instr *instr, nir_src *src,
                     enum crocus_surface_group group)
{
   assert(bt->sizes[group] > 0);

   b->cursor = nir_before_instr(instr);
   nir_ssa_def *bti;
   if (nir_src_is_const(*src)) {
      uint32_t index = nir_src_as_uint(*src);
      bti = nir_imm_intN_t(b, crocus_group_index_to_bti(bt, group, index),
                           src->ssa->bit_size);
   } else {
      /* Indirect usage makes all the surfaces of the group to be available,
       * so we can just add the base.
       */
      assert(bt->used_mask[group] == BITFIELD64_MASK(bt->sizes[group]));
      bti = nir_iadd_imm(b, src->ssa, bt->offsets[group]);
   }
   nir_instr_rewrite_src(instr, src, nir_src_for_ssa(bti));
}

static void
mark_used_with_src(struct crocus_binding_table *bt, nir_src *src,
                   enum crocus_surface_group group)
{
   assert(bt->sizes[group] > 0);

   if (nir_src_is_const(*src)) {
      uint64_t index = nir_src_as_uint(*src);
      assert(index < bt->sizes[group]);
      bt->used_mask[group] |= 1ull << index;
   } else {
      /* There's an indirect usage, we need all the surfaces. */
      bt->used_mask[group] = BITFIELD64_MASK(bt->sizes[group]);
   }
}

static bool
skip_compacting_binding_tables(void)
{
   static int skip = -1;
   if (skip < 0)
      skip = env_var_as_boolean("INTEL_DISABLE_COMPACT_BINDING_TABLE", false);
   return skip;
}

/**
 * Set up the binding table indices and apply to the shader.
 */
static void
crocus_setup_binding_table(const struct intel_device_info *devinfo,
                           struct nir_shader *nir,
                           struct crocus_binding_table *bt,
                           unsigned num_render_targets,
                           unsigned num_system_values,
                           unsigned num_cbufs,
                           const struct brw_sampler_prog_key_data *key)
{
   const struct shader_info *info = &nir->info;

   memset(bt, 0, sizeof(*bt));

   /* Set the sizes for each surface group.  For some groups, we already know
    * upfront how many will be used, so mark them.
    */
   if (info->stage == MESA_SHADER_FRAGMENT) {
      bt->sizes[CROCUS_SURFACE_GROUP_RENDER_TARGET] = num_render_targets;
      /* All render targets used. */
      bt->used_mask[CROCUS_SURFACE_GROUP_RENDER_TARGET] =
         BITFIELD64_MASK(num_render_targets);

      /* Setup render target read surface group in order to support non-coherent
       * framebuffer fetch on Gfx7
       */
      if (devinfo->ver >= 6 && info->outputs_read) {
         bt->sizes[CROCUS_SURFACE_GROUP_RENDER_TARGET_READ] = num_render_targets;
         bt->used_mask[CROCUS_SURFACE_GROUP_RENDER_TARGET_READ] =
            BITFIELD64_MASK(num_render_targets);
      }
   } else if (info->stage == MESA_SHADER_COMPUTE) {
      bt->sizes[CROCUS_SURFACE_GROUP_CS_WORK_GROUPS] = 1;
   } else if (info->stage == MESA_SHADER_GEOMETRY) {
      /* In gfx6 we reserve the first BRW_MAX_SOL_BINDINGS entries for transform
       * feedback surfaces.
       */
      if (devinfo->ver == 6) {
         bt->sizes[CROCUS_SURFACE_GROUP_SOL] = BRW_MAX_SOL_BINDINGS;
         bt->used_mask[CROCUS_SURFACE_GROUP_SOL] = (uint64_t)-1;
      }
   }

   bt->sizes[CROCUS_SURFACE_GROUP_TEXTURE] = BITSET_LAST_BIT(info->textures_used);
   bt->used_mask[CROCUS_SURFACE_GROUP_TEXTURE] = info->textures_used[0];

   if (info->uses_texture_gather) {
      bt->sizes[CROCUS_SURFACE_GROUP_TEXTURE_GATHER] = BITSET_LAST_BIT(info->textures_used);
      bt->used_mask[CROCUS_SURFACE_GROUP_TEXTURE_GATHER] = info->textures_used[0];
   }

   bt->sizes[CROCUS_SURFACE_GROUP_IMAGE] = info->num_images;

   /* Allocate an extra slot in the UBO section for NIR constants.
    * Binding table compaction will remove it if unnecessary.
    *
    * We don't include them in crocus_compiled_shader::num_cbufs because
    * they are uploaded separately from shs->constbufs[], but from a shader
    * point of view, they're another UBO (at the end of the section).
    */
   bt->sizes[CROCUS_SURFACE_GROUP_UBO] = num_cbufs + 1;

   bt->sizes[CROCUS_SURFACE_GROUP_SSBO] = info->num_ssbos;

   for (int i = 0; i < CROCUS_SURFACE_GROUP_COUNT; i++)
      assert(bt->sizes[i] <= SURFACE_GROUP_MAX_ELEMENTS);

   /* Mark surfaces used for the cases we don't have the information available
    * upfront.
    */
   nir_function_impl *impl = nir_shader_get_entrypoint(nir);
   nir_foreach_block (block, impl) {
      nir_foreach_instr (instr, block) {
         if (instr->type != nir_instr_type_intrinsic)
            continue;

         nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
         switch (intrin->intrinsic) {
         case nir_intrinsic_load_num_workgroups:
            bt->used_mask[CROCUS_SURFACE_GROUP_CS_WORK_GROUPS] = 1;
            break;

         case nir_intrinsic_load_output:
            if (devinfo->ver >= 6) {
               mark_used_with_src(bt, &intrin->src[0],
                                  CROCUS_SURFACE_GROUP_RENDER_TARGET_READ);
            }
            break;

         case nir_intrinsic_image_size:
         case nir_intrinsic_image_load:
         case nir_intrinsic_image_store:
         case nir_intrinsic_image_atomic_add:
         case nir_intrinsic_image_atomic_imin:
         case nir_intrinsic_image_atomic_umin:
         case nir_intrinsic_image_atomic_imax:
         case nir_intrinsic_image_atomic_umax:
         case nir_intrinsic_image_atomic_and:
         case nir_intrinsic_image_atomic_or:
         case nir_intrinsic_image_atomic_xor:
         case nir_intrinsic_image_atomic_exchange:
         case nir_intrinsic_image_atomic_comp_swap:
         case nir_intrinsic_image_load_raw_intel:
         case nir_intrinsic_image_store_raw_intel:
            mark_used_with_src(bt, &intrin->src[0], CROCUS_SURFACE_GROUP_IMAGE);
            break;

         case nir_intrinsic_load_ubo:
            mark_used_with_src(bt, &intrin->src[0], CROCUS_SURFACE_GROUP_UBO);
            break;

         case nir_intrinsic_store_ssbo:
            mark_used_with_src(bt, &intrin->src[1], CROCUS_SURFACE_GROUP_SSBO);
            break;

         case nir_intrinsic_get_ssbo_size:
         case nir_intrinsic_ssbo_atomic_add:
         case nir_intrinsic_ssbo_atomic_imin:
         case nir_intrinsic_ssbo_atomic_umin:
         case nir_intrinsic_ssbo_atomic_imax:
         case nir_intrinsic_ssbo_atomic_umax:
         case nir_intrinsic_ssbo_atomic_and:
         case nir_intrinsic_ssbo_atomic_or:
         case nir_intrinsic_ssbo_atomic_xor:
         case nir_intrinsic_ssbo_atomic_exchange:
         case nir_intrinsic_ssbo_atomic_comp_swap:
         case nir_intrinsic_ssbo_atomic_fmin:
         case nir_intrinsic_ssbo_atomic_fmax:
         case nir_intrinsic_ssbo_atomic_fcomp_swap:
         case nir_intrinsic_load_ssbo:
            mark_used_with_src(bt, &intrin->src[0], CROCUS_SURFACE_GROUP_SSBO);
            break;

         default:
            break;
         }
      }
   }

   /* When disable we just mark everything as used. */
   if (unlikely(skip_compacting_binding_tables())) {
      for (int i = 0; i < CROCUS_SURFACE_GROUP_COUNT; i++)
         bt->used_mask[i] = BITFIELD64_MASK(bt->sizes[i]);
   }

   /* Calculate the offsets and the binding table size based on the used
    * surfaces.  After this point, the functions to go between "group indices"
    * and binding table indices can be used.
    */
   uint32_t next = 0;
   for (int i = 0; i < CROCUS_SURFACE_GROUP_COUNT; i++) {
      if (bt->used_mask[i] != 0) {
         bt->offsets[i] = next;
         next += util_bitcount64(bt->used_mask[i]);
      }
   }
   bt->size_bytes = next * 4;

   if (unlikely(INTEL_DEBUG & DEBUG_BT)) {
      crocus_print_binding_table(stderr, gl_shader_stage_name(info->stage), bt);
   }

   /* Apply the binding table indices.  The backend compiler is not expected
    * to change those, as we haven't set any of the *_start entries in brw
    * binding_table.
    */
   nir_builder b;
   nir_builder_init(&b, impl);

   nir_foreach_block (block, impl) {
      nir_foreach_instr (instr, block) {
         if (instr->type == nir_instr_type_tex) {
            nir_tex_instr *tex = nir_instr_as_tex(instr);
            bool is_gather = tex->op == nir_texop_tg4;

            /* rewrite the tg4 component from green to blue before replacing the
               texture index */
            if (devinfo->ver == 7 && !devinfo->is_haswell) {
               if (tex->component == 1)
                  if (key->gather_channel_quirk_mask & (1 << tex->texture_index))
                     tex->component = 2;
            }

            if (is_gather && devinfo->ver == 6 && key->gfx6_gather_wa[tex->texture_index]) {
               b.cursor = nir_after_instr(instr);
               enum gfx6_gather_sampler_wa wa = key->gfx6_gather_wa[tex->texture_index];
               int width = (wa & WA_8BIT) ? 8 : 16;

               nir_ssa_def *val = nir_fmul_imm(&b, &tex->dest.ssa, (1 << width) - 1);
               val = nir_f2u32(&b, val);
               if (wa & WA_SIGN) {
                  val = nir_ishl(&b, val, nir_imm_int(&b, 32 - width));
                  val = nir_ishr(&b, val, nir_imm_int(&b, 32 - width));
               }
               nir_ssa_def_rewrite_uses_after(&tex->dest.ssa, val, val->parent_instr);
            }

            tex->texture_index =
               crocus_group_index_to_bti(bt, is_gather ? CROCUS_SURFACE_GROUP_TEXTURE_GATHER : CROCUS_SURFACE_GROUP_TEXTURE,
                                         tex->texture_index);
            continue;
         }

         if (instr->type != nir_instr_type_intrinsic)
            continue;

         nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
         switch (intrin->intrinsic) {
         case nir_intrinsic_image_size:
         case nir_intrinsic_image_load:
         case nir_intrinsic_image_store:
         case nir_intrinsic_image_atomic_add:
         case nir_intrinsic_image_atomic_imin:
         case nir_intrinsic_image_atomic_umin:
         case nir_intrinsic_image_atomic_imax:
         case nir_intrinsic_image_atomic_umax:
         case nir_intrinsic_image_atomic_and:
         case nir_intrinsic_image_atomic_or:
         case nir_intrinsic_image_atomic_xor:
         case nir_intrinsic_image_atomic_exchange:
         case nir_intrinsic_image_atomic_comp_swap:
         case nir_intrinsic_image_load_raw_intel:
         case nir_intrinsic_image_store_raw_intel:
            rewrite_src_with_bti(&b, bt, instr, &intrin->src[0],
                                 CROCUS_SURFACE_GROUP_IMAGE);
            break;

         case nir_intrinsic_load_ubo:
            rewrite_src_with_bti(&b, bt, instr, &intrin->src[0],
                                 CROCUS_SURFACE_GROUP_UBO);
            break;

         case nir_intrinsic_store_ssbo:
            rewrite_src_with_bti(&b, bt, instr, &intrin->src[1],
                                 CROCUS_SURFACE_GROUP_SSBO);
            break;

         case nir_intrinsic_load_output:
            if (devinfo->ver >= 6) {
               rewrite_src_with_bti(&b, bt, instr, &intrin->src[0],
                                    CROCUS_SURFACE_GROUP_RENDER_TARGET_READ);
            }
            break;

         case nir_intrinsic_get_ssbo_size:
         case nir_intrinsic_ssbo_atomic_add:
         case nir_intrinsic_ssbo_atomic_imin:
         case nir_intrinsic_ssbo_atomic_umin:
         case nir_intrinsic_ssbo_atomic_imax:
         case nir_intrinsic_ssbo_atomic_umax:
         case nir_intrinsic_ssbo_atomic_and:
         case nir_intrinsic_ssbo_atomic_or:
         case nir_intrinsic_ssbo_atomic_xor:
         case nir_intrinsic_ssbo_atomic_exchange:
         case nir_intrinsic_ssbo_atomic_comp_swap:
         case nir_intrinsic_ssbo_atomic_fmin:
         case nir_intrinsic_ssbo_atomic_fmax:
         case nir_intrinsic_ssbo_atomic_fcomp_swap:
         case nir_intrinsic_load_ssbo:
            rewrite_src_with_bti(&b, bt, instr, &intrin->src[0],
                                 CROCUS_SURFACE_GROUP_SSBO);
            break;

         default:
            break;
         }
      }
   }
}

static void
crocus_debug_recompile(struct crocus_context *ice,
                       struct shader_info *info,
                       const struct brw_base_prog_key *key)
{
   struct crocus_screen *screen = (struct crocus_screen *) ice->ctx.screen;
   const struct brw_compiler *c = screen->compiler;

   if (!info)
      return;

   c->shader_perf_log(&ice->dbg, "Recompiling %s shader for program %s: %s\n",
                      _mesa_shader_stage_to_string(info->stage),
                      info->name ? info->name : "(no identifier)",
                      info->label ? info->label : "");

   const void *old_key =
      crocus_find_previous_compile(ice, info->stage, key->program_string_id);

   brw_debug_key_recompile(c, &ice->dbg, info->stage, old_key, key);
}

/**
 * Get the shader for the last enabled geometry stage.
 *
 * This stage is the one which will feed stream output and the rasterizer.
 */
static gl_shader_stage
last_vue_stage(struct crocus_context *ice)
{
   if (ice->shaders.uncompiled[MESA_SHADER_GEOMETRY])
      return MESA_SHADER_GEOMETRY;

   if (ice->shaders.uncompiled[MESA_SHADER_TESS_EVAL])
      return MESA_SHADER_TESS_EVAL;

   return MESA_SHADER_VERTEX;
}

static GLbitfield64
crocus_vs_outputs_written(struct crocus_context *ice,
                          const struct brw_vs_prog_key *key,
                          GLbitfield64 user_varyings)
{
   struct crocus_screen *screen = (struct crocus_screen *)ice->ctx.screen;
   const struct intel_device_info *devinfo = &screen->devinfo;
   GLbitfield64 outputs_written = user_varyings;

   if (devinfo->ver < 6) {

      if (key->copy_edgeflag)
         outputs_written |= BITFIELD64_BIT(VARYING_SLOT_EDGE);

      /* Put dummy slots into the VUE for the SF to put the replaced
       * point sprite coords in.  We shouldn't need these dummy slots,
       * which take up precious URB space, but it would mean that the SF
       * doesn't get nice aligned pairs of input coords into output
       * coords, which would be a pain to handle.
       */
      for (unsigned i = 0; i < 8; i++) {
         if (key->point_coord_replace & (1 << i))
            outputs_written |= BITFIELD64_BIT(VARYING_SLOT_TEX0 + i);
      }

      /* if back colors are written, allocate slots for front colors too */
      if (outputs_written & BITFIELD64_BIT(VARYING_SLOT_BFC0))
         outputs_written |= BITFIELD64_BIT(VARYING_SLOT_COL0);
      if (outputs_written & BITFIELD64_BIT(VARYING_SLOT_BFC1))
         outputs_written |= BITFIELD64_BIT(VARYING_SLOT_COL1);
   }

   /* In order for legacy clipping to work, we need to populate the clip
    * distance varying slots whenever clipping is enabled, even if the vertex
    * shader doesn't write to gl_ClipDistance.
    */
   if (key->nr_userclip_plane_consts > 0) {
      outputs_written |= BITFIELD64_BIT(VARYING_SLOT_CLIP_DIST0);
      outputs_written |= BITFIELD64_BIT(VARYING_SLOT_CLIP_DIST1);
   }

   return outputs_written;
}

/*
 * If no edgeflags come from the user, gen4/5
 * require giving the clip shader a default edgeflag.
 *
 * This will always be 1.0.
 */
static void
crocus_lower_default_edgeflags(struct nir_shader *nir)
{
   nir_function_impl *impl = nir_shader_get_entrypoint(nir);

   nir_builder b;
   nir_builder_init(&b, impl);

   b.cursor = nir_after_cf_list(&b.impl->body);
   nir_variable *var = nir_variable_create(nir, nir_var_shader_out,
                                           glsl_float_type(),
                                           "edgeflag");
   var->data.location = VARYING_SLOT_EDGE;
   nir_store_var(&b, var, nir_imm_float(&b, 1.0), 0x1);
}

/**
 * Compile a vertex shader, and upload the assembly.
 */
static struct crocus_compiled_shader *
crocus_compile_vs(struct crocus_context *ice,
                  struct crocus_uncompiled_shader *ish,
                  const struct brw_vs_prog_key *key)
{
   struct crocus_screen *screen = (struct crocus_screen *)ice->ctx.screen;
   const struct brw_compiler *compiler = screen->compiler;
   const struct intel_device_info *devinfo = &screen->devinfo;
   void *mem_ctx = ralloc_context(NULL);
   struct brw_vs_prog_data *vs_prog_data =
      rzalloc(mem_ctx, struct brw_vs_prog_data);
   struct brw_vue_prog_data *vue_prog_data = &vs_prog_data->base;
   struct brw_stage_prog_data *prog_data = &vue_prog_data->base;
   enum brw_param_builtin *system_values;
   unsigned num_system_values;
   unsigned num_cbufs;

   nir_shader *nir = nir_shader_clone(mem_ctx, ish->nir);

   if (key->nr_userclip_plane_consts) {
      nir_function_impl *impl = nir_shader_get_entrypoint(nir);
      nir_lower_clip_vs(nir, (1 << key->nr_userclip_plane_consts) - 1, true,
                        false, NULL);
      nir_lower_io_to_temporaries(nir, impl, true, false);
      nir_lower_global_vars_to_local(nir);
      nir_lower_vars_to_ssa(nir);
      nir_shader_gather_info(nir, impl);
   }

   prog_data->use_alt_mode = ish->use_alt_mode;

   crocus_setup_uniforms(compiler, mem_ctx, nir, prog_data, &system_values,
                         &num_system_values, &num_cbufs);

   crocus_lower_swizzles(nir, &key->base.tex);

   if (devinfo->ver <= 5 &&
       !(nir->info.inputs_read & BITFIELD64_BIT(VERT_ATTRIB_EDGEFLAG)))
      crocus_lower_default_edgeflags(nir);

   struct crocus_binding_table bt;
   crocus_setup_binding_table(devinfo, nir, &bt, /* num_render_targets */ 0,
                              num_system_values, num_cbufs, &key->base.tex);

   if (can_push_ubo(devinfo))
      brw_nir_analyze_ubo_ranges(compiler, nir, NULL, prog_data->ubo_ranges);

   uint64_t outputs_written =
      crocus_vs_outputs_written(ice, key, nir->info.outputs_written);
   brw_compute_vue_map(devinfo,
                       &vue_prog_data->vue_map, outputs_written,
                       nir->info.separate_shader, /* pos slots */ 1);

   /* Don't tell the backend about our clip plane constants, we've already
    * lowered them in NIR and we don't want it doing it again.
    */
   struct brw_vs_prog_key key_no_ucp = *key;
   key_no_ucp.nr_userclip_plane_consts = 0;
   key_no_ucp.copy_edgeflag = false;
   crocus_sanitize_tex_key(&key_no_ucp.base.tex);

   struct brw_compile_vs_params params = {
      .nir = nir,
      .key = &key_no_ucp,
      .prog_data = vs_prog_data,
      .edgeflag_is_last = devinfo->ver < 6,
      .log_data = &ice->dbg,
   };
   const unsigned *program =
      brw_compile_vs(compiler, mem_ctx, &params);
   if (program == NULL) {
      dbg_printf("Failed to compile vertex shader: %s\n", params.error_str);
      ralloc_free(mem_ctx);
      return false;
   }

   if (ish->compiled_once) {
      crocus_debug_recompile(ice, &nir->info, &key->base);
   } else {
      ish->compiled_once = true;
   }

   uint32_t *so_decls = NULL;
   if (devinfo->ver > 6)
      so_decls = screen->vtbl.create_so_decl_list(&ish->stream_output,
                                                  &vue_prog_data->vue_map);

   struct crocus_compiled_shader *shader =
      crocus_upload_shader(ice, CROCUS_CACHE_VS, sizeof(*key), key, program,
                           prog_data->program_size,
                           prog_data, sizeof(*vs_prog_data), so_decls,
                           system_values, num_system_values,
                           num_cbufs, &bt);

   crocus_disk_cache_store(screen->disk_cache, ish, shader,
                           ice->shaders.cache_bo_map,
                           key, sizeof(*key));

   ralloc_free(mem_ctx);
   return shader;
}

/**
 * Update the current vertex shader variant.
 *
 * Fill out the key, look in the cache, compile and bind if needed.
 */
static void
crocus_update_compiled_vs(struct crocus_context *ice)
{
   struct crocus_shader_state *shs = &ice->state.shaders[MESA_SHADER_VERTEX];
   struct crocus_uncompiled_shader *ish =
      ice->shaders.uncompiled[MESA_SHADER_VERTEX];
   struct crocus_screen *screen = (struct crocus_screen *)ice->ctx.screen;
   const struct intel_device_info *devinfo = &screen->devinfo;
   struct brw_vs_prog_key key = { KEY_INIT() };

   if (ish->nos & (1ull << CROCUS_NOS_TEXTURES))
      crocus_populate_sampler_prog_key_data(ice, devinfo, MESA_SHADER_VERTEX, ish,
                                            ish->nir->info.uses_texture_gather, &key.base.tex);
   screen->vtbl.populate_vs_key(ice, &ish->nir->info, last_vue_stage(ice), &key);

   struct crocus_compiled_shader *old = ice->shaders.prog[CROCUS_CACHE_VS];
   struct crocus_compiled_shader *shader =
      crocus_find_cached_shader(ice, CROCUS_CACHE_VS, sizeof(key), &key);

   if (!shader)
      shader = crocus_disk_cache_retrieve(ice, ish, &key, sizeof(key));

   if (!shader)
      shader = crocus_compile_vs(ice, ish, &key);

   if (old != shader) {
      ice->shaders.prog[CROCUS_CACHE_VS] = shader;
      ice->state.stage_dirty |= CROCUS_STAGE_DIRTY_VS |
                                CROCUS_STAGE_DIRTY_BINDINGS_VS |
                                CROCUS_STAGE_DIRTY_CONSTANTS_VS;
      shs->sysvals_need_upload = true;

      const struct brw_vs_prog_data *vs_prog_data =
         (void *) shader->prog_data;
      const bool uses_draw_params = vs_prog_data->uses_firstvertex ||
                                    vs_prog_data->uses_baseinstance;
      const bool uses_derived_draw_params = vs_prog_data->uses_drawid ||
                                            vs_prog_data->uses_is_indexed_draw;
      const bool needs_sgvs_element = uses_draw_params ||
                                      vs_prog_data->uses_instanceid ||
                                      vs_prog_data->uses_vertexid;

      if (ice->state.vs_uses_draw_params != uses_draw_params ||
          ice->state.vs_uses_derived_draw_params != uses_derived_draw_params ||
          ice->state.vs_needs_edge_flag != ish->needs_edge_flag ||
          ice->state.vs_uses_vertexid != vs_prog_data->uses_vertexid ||
          ice->state.vs_uses_instanceid != vs_prog_data->uses_instanceid) {
         ice->state.dirty |= CROCUS_DIRTY_VERTEX_BUFFERS |
                             CROCUS_DIRTY_VERTEX_ELEMENTS;
      }
      ice->state.vs_uses_draw_params = uses_draw_params;
      ice->state.vs_uses_derived_draw_params = uses_derived_draw_params;
      ice->state.vs_needs_sgvs_element = needs_sgvs_element;
      ice->state.vs_needs_edge_flag = ish->needs_edge_flag;
      ice->state.vs_uses_vertexid = vs_prog_data->uses_vertexid;
      ice->state.vs_uses_instanceid = vs_prog_data->uses_instanceid;
   }
}

/**
 * Get the shader_info for a given stage, or NULL if the stage is disabled.
 */
const struct shader_info *
crocus_get_shader_info(const struct crocus_context *ice, gl_shader_stage stage)
{
   const struct crocus_uncompiled_shader *ish = ice->shaders.uncompiled[stage];

   if (!ish)
      return NULL;

   const nir_shader *nir = ish->nir;
   return &nir->info;
}

/**
 * Get the union of TCS output and TES input slots.
 *
 * TCS and TES need to agree on a common URB entry layout.  In particular,
 * the data for all patch vertices is stored in a single URB entry (unlike
 * GS which has one entry per input vertex).  This means that per-vertex
 * array indexing needs a stride.
 *
 * SSO requires locations to match, but doesn't require the number of
 * outputs/inputs to match (in fact, the TCS often has extra outputs).
 * So, we need to take the extra step of unifying these on the fly.
 */
static void
get_unified_tess_slots(const struct crocus_context *ice,
                       uint64_t *per_vertex_slots,
                       uint32_t *per_patch_slots)
{
   const struct shader_info *tcs =
      crocus_get_shader_info(ice, MESA_SHADER_TESS_CTRL);
   const struct shader_info *tes =
      crocus_get_shader_info(ice, MESA_SHADER_TESS_EVAL);

   *per_vertex_slots = tes->inputs_read;
   *per_patch_slots = tes->patch_inputs_read;

   if (tcs) {
      *per_vertex_slots |= tcs->outputs_written;
      *per_patch_slots |= tcs->patch_outputs_written;
   }
}

/**
 * Compile a tessellation control shader, and upload the assembly.
 */
static struct crocus_compiled_shader *
crocus_compile_tcs(struct crocus_context *ice,
                   struct crocus_uncompiled_shader *ish,
                   const struct brw_tcs_prog_key *key)
{
   struct crocus_screen *screen = (struct crocus_screen *)ice->ctx.screen;
   const struct brw_compiler *compiler = screen->compiler;
   const struct nir_shader_compiler_options *options =
      compiler->glsl_compiler_options[MESA_SHADER_TESS_CTRL].NirOptions;
   void *mem_ctx = ralloc_context(NULL);
   struct brw_tcs_prog_data *tcs_prog_data =
      rzalloc(mem_ctx, struct brw_tcs_prog_data);
   struct brw_vue_prog_data *vue_prog_data = &tcs_prog_data->base;
   struct brw_stage_prog_data *prog_data = &vue_prog_data->base;
   const struct intel_device_info *devinfo = &screen->devinfo;
   enum brw_param_builtin *system_values = NULL;
   unsigned num_system_values = 0;
   unsigned num_cbufs = 0;

   nir_shader *nir;

   struct crocus_binding_table bt;

   if (ish) {
      nir = nir_shader_clone(mem_ctx, ish->nir);

      crocus_setup_uniforms(compiler, mem_ctx, nir, prog_data, &system_values,
                            &num_system_values, &num_cbufs);

      crocus_lower_swizzles(nir, &key->base.tex);
      crocus_setup_binding_table(devinfo, nir, &bt, /* num_render_targets */ 0,
                                 num_system_values, num_cbufs, &key->base.tex);
      if (can_push_ubo(devinfo))
         brw_nir_analyze_ubo_ranges(compiler, nir, NULL, prog_data->ubo_ranges);
   } else {
      nir = brw_nir_create_passthrough_tcs(mem_ctx, compiler, options, key);

      /* Reserve space for passing the default tess levels as constants. */
      num_cbufs = 1;
      num_system_values = 8;
      system_values =
         rzalloc_array(mem_ctx, enum brw_param_builtin, num_system_values);
      prog_data->param = rzalloc_array(mem_ctx, uint32_t, num_system_values);
      prog_data->nr_params = num_system_values;

      if (key->tes_primitive_mode == GL_QUADS) {
         for (int i = 0; i < 4; i++)
            system_values[7 - i] = BRW_PARAM_BUILTIN_TESS_LEVEL_OUTER_X + i;

         system_values[3] = BRW_PARAM_BUILTIN_TESS_LEVEL_INNER_X;
         system_values[2] = BRW_PARAM_BUILTIN_TESS_LEVEL_INNER_Y;
      } else if (key->tes_primitive_mode == GL_TRIANGLES) {
         for (int i = 0; i < 3; i++)
            system_values[7 - i] = BRW_PARAM_BUILTIN_TESS_LEVEL_OUTER_X + i;

         system_values[4] = BRW_PARAM_BUILTIN_TESS_LEVEL_INNER_X;
      } else {
         assert(key->tes_primitive_mode == GL_ISOLINES);
         system_values[7] = BRW_PARAM_BUILTIN_TESS_LEVEL_OUTER_Y;
         system_values[6] = BRW_PARAM_BUILTIN_TESS_LEVEL_OUTER_X;
      }

      /* Manually setup the TCS binding table. */
      memset(&bt, 0, sizeof(bt));
      bt.sizes[CROCUS_SURFACE_GROUP_UBO] = 1;
      bt.used_mask[CROCUS_SURFACE_GROUP_UBO] = 1;
      bt.size_bytes = 4;

      prog_data->ubo_ranges[0].length = 1;
   }

   struct brw_tcs_prog_key key_clean = *key;
   crocus_sanitize_tex_key(&key_clean.base.tex);
   char *error_str = NULL;
   const unsigned *program =
      brw_compile_tcs(compiler, &ice->dbg, mem_ctx, &key_clean, tcs_prog_data, nir,
                      -1, NULL, &error_str);
   if (program == NULL) {
      dbg_printf("Failed to compile control shader: %s\n", error_str);
      ralloc_free(mem_ctx);
      return false;
   }

   if (ish) {
      if (ish->compiled_once) {
         crocus_debug_recompile(ice, &nir->info, &key->base);
      } else {
         ish->compiled_once = true;
      }
   }

   struct crocus_compiled_shader *shader =
      crocus_upload_shader(ice, CROCUS_CACHE_TCS, sizeof(*key), key, program,
                           prog_data->program_size,
                           prog_data, sizeof(*tcs_prog_data), NULL,
                           system_values, num_system_values,
                           num_cbufs, &bt);

   if (ish)
      crocus_disk_cache_store(screen->disk_cache, ish, shader,
                              ice->shaders.cache_bo_map,
                              key, sizeof(*key));

   ralloc_free(mem_ctx);
   return shader;
}

/**
 * Update the current tessellation control shader variant.
 *
 * Fill out the key, look in the cache, compile and bind if needed.
 */
static void
crocus_update_compiled_tcs(struct crocus_context *ice)
{
   struct crocus_shader_state *shs = &ice->state.shaders[MESA_SHADER_TESS_CTRL];
   struct crocus_uncompiled_shader *tcs =
      ice->shaders.uncompiled[MESA_SHADER_TESS_CTRL];
   struct crocus_screen *screen = (struct crocus_screen *)ice->ctx.screen;
   const struct intel_device_info *devinfo = &screen->devinfo;

   const struct shader_info *tes_info =
      crocus_get_shader_info(ice, MESA_SHADER_TESS_EVAL);
   struct brw_tcs_prog_key key = {
      KEY_INIT_NO_ID(),
      .base.program_string_id = tcs ? tcs->program_id : 0,
      .tes_primitive_mode = tes_info->tess.primitive_mode,
      .input_vertices = ice->state.vertices_per_patch,
      .quads_workaround = tes_info->tess.primitive_mode == GL_QUADS &&
                          tes_info->tess.spacing == TESS_SPACING_EQUAL,
   };

   if (tcs && tcs->nos & (1ull << CROCUS_NOS_TEXTURES))
      crocus_populate_sampler_prog_key_data(ice, devinfo, MESA_SHADER_TESS_CTRL, tcs,
                                            tcs->nir->info.uses_texture_gather, &key.base.tex);
   get_unified_tess_slots(ice, &key.outputs_written,
                          &key.patch_outputs_written);
   screen->vtbl.populate_tcs_key(ice, &key);

   struct crocus_compiled_shader *old = ice->shaders.prog[CROCUS_CACHE_TCS];
   struct crocus_compiled_shader *shader =
      crocus_find_cached_shader(ice, CROCUS_CACHE_TCS, sizeof(key), &key);

   if (tcs && !shader)
      shader = crocus_disk_cache_retrieve(ice, tcs, &key, sizeof(key));

   if (!shader)
      shader = crocus_compile_tcs(ice, tcs, &key);

   if (old != shader) {
      ice->shaders.prog[CROCUS_CACHE_TCS] = shader;
      ice->state.stage_dirty |= CROCUS_STAGE_DIRTY_TCS |
                                CROCUS_STAGE_DIRTY_BINDINGS_TCS |
                                CROCUS_STAGE_DIRTY_CONSTANTS_TCS;
      shs->sysvals_need_upload = true;
   }
}

/**
 * Compile a tessellation evaluation shader, and upload the assembly.
 */
static struct crocus_compiled_shader *
crocus_compile_tes(struct crocus_context *ice,
                   struct crocus_uncompiled_shader *ish,
                   const struct brw_tes_prog_key *key)
{
   struct crocus_screen *screen = (struct crocus_screen *)ice->ctx.screen;
   const struct brw_compiler *compiler = screen->compiler;
   void *mem_ctx = ralloc_context(NULL);
   struct brw_tes_prog_data *tes_prog_data =
      rzalloc(mem_ctx, struct brw_tes_prog_data);
   struct brw_vue_prog_data *vue_prog_data = &tes_prog_data->base;
   struct brw_stage_prog_data *prog_data = &vue_prog_data->base;
   enum brw_param_builtin *system_values;
   const struct intel_device_info *devinfo = &screen->devinfo;
   unsigned num_system_values;
   unsigned num_cbufs;

   nir_shader *nir = nir_shader_clone(mem_ctx, ish->nir);

   if (key->nr_userclip_plane_consts) {
      nir_function_impl *impl = nir_shader_get_entrypoint(nir);
      nir_lower_clip_vs(nir, (1 << key->nr_userclip_plane_consts) - 1, true,
                        false, NULL);
      nir_lower_io_to_temporaries(nir, impl, true, false);
      nir_lower_global_vars_to_local(nir);
      nir_lower_vars_to_ssa(nir);
      nir_shader_gather_info(nir, impl);
   }

   crocus_setup_uniforms(compiler, mem_ctx, nir, prog_data, &system_values,
                         &num_system_values, &num_cbufs);
   crocus_lower_swizzles(nir, &key->base.tex);
   struct crocus_binding_table bt;
   crocus_setup_binding_table(devinfo, nir, &bt, /* num_render_targets */ 0,
                              num_system_values, num_cbufs, &key->base.tex);

   if (can_push_ubo(devinfo))
      brw_nir_analyze_ubo_ranges(compiler, nir, NULL, prog_data->ubo_ranges);

   struct brw_vue_map input_vue_map;
   brw_compute_tess_vue_map(&input_vue_map, key->inputs_read,
                            key->patch_inputs_read);

   struct brw_tes_prog_key key_clean = *key;
   crocus_sanitize_tex_key(&key_clean.base.tex);
   char *error_str = NULL;
   const unsigned *program =
      brw_compile_tes(compiler, &ice->dbg, mem_ctx, &key_clean, &input_vue_map,
                      tes_prog_data, nir, -1, NULL, &error_str);
   if (program == NULL) {
      dbg_printf("Failed to compile evaluation shader: %s\n", error_str);
      ralloc_free(mem_ctx);
      return false;
   }

   if (ish->compiled_once) {
      crocus_debug_recompile(ice, &nir->info, &key->base);
   } else {
      ish->compiled_once = true;
   }

   uint32_t *so_decls = NULL;
   if (devinfo->ver > 6)
      so_decls = screen->vtbl.create_so_decl_list(&ish->stream_output,
                                                  &vue_prog_data->vue_map);

   struct crocus_compiled_shader *shader =
      crocus_upload_shader(ice, CROCUS_CACHE_TES, sizeof(*key), key, program,
                           prog_data->program_size,
                           prog_data, sizeof(*tes_prog_data), so_decls,
                           system_values, num_system_values,
                           num_cbufs, &bt);

   crocus_disk_cache_store(screen->disk_cache, ish, shader,
                           ice->shaders.cache_bo_map,
                           key, sizeof(*key));

   ralloc_free(mem_ctx);
   return shader;
}

/**
 * Update the current tessellation evaluation shader variant.
 *
 * Fill out the key, look in the cache, compile and bind if needed.
 */
static void
crocus_update_compiled_tes(struct crocus_context *ice)
{
   struct crocus_shader_state *shs = &ice->state.shaders[MESA_SHADER_TESS_EVAL];
   struct crocus_uncompiled_shader *ish =
      ice->shaders.uncompiled[MESA_SHADER_TESS_EVAL];
   struct brw_tes_prog_key key = { KEY_INIT() };
   struct crocus_screen *screen = (struct crocus_screen *)ice->ctx.screen;
   const struct intel_device_info *devinfo = &screen->devinfo;

   if (ish->nos & (1ull << CROCUS_NOS_TEXTURES))
      crocus_populate_sampler_prog_key_data(ice, devinfo, MESA_SHADER_TESS_EVAL, ish,
                                            ish->nir->info.uses_texture_gather, &key.base.tex);
   get_unified_tess_slots(ice, &key.inputs_read, &key.patch_inputs_read);
   screen->vtbl.populate_tes_key(ice, &ish->nir->info, last_vue_stage(ice), &key);

   struct crocus_compiled_shader *old = ice->shaders.prog[CROCUS_CACHE_TES];
   struct crocus_compiled_shader *shader =
      crocus_find_cached_shader(ice, CROCUS_CACHE_TES, sizeof(key), &key);

   if (!shader)
      shader = crocus_disk_cache_retrieve(ice, ish, &key, sizeof(key));

   if (!shader)
      shader = crocus_compile_tes(ice, ish, &key);

   if (old != shader) {
      ice->shaders.prog[CROCUS_CACHE_TES] = shader;
      ice->state.stage_dirty |= CROCUS_STAGE_DIRTY_TES |
                                CROCUS_STAGE_DIRTY_BINDINGS_TES |
                                CROCUS_STAGE_DIRTY_CONSTANTS_TES;
      shs->sysvals_need_upload = true;
   }

   /* TODO: Could compare and avoid flagging this. */
   const struct shader_info *tes_info = &ish->nir->info;
   if (BITSET_TEST(tes_info->system_values_read, SYSTEM_VALUE_VERTICES_IN)) {
      ice->state.stage_dirty |= CROCUS_STAGE_DIRTY_CONSTANTS_TES;
      ice->state.shaders[MESA_SHADER_TESS_EVAL].sysvals_need_upload = true;
   }
}

/**
 * Compile a geometry shader, and upload the assembly.
 */
static struct crocus_compiled_shader *
crocus_compile_gs(struct crocus_context *ice,
                  struct crocus_uncompiled_shader *ish,
                  const struct brw_gs_prog_key *key)
{
   struct crocus_screen *screen = (struct crocus_screen *)ice->ctx.screen;
   const struct brw_compiler *compiler = screen->compiler;
   const struct intel_device_info *devinfo = &screen->devinfo;
   void *mem_ctx = ralloc_context(NULL);
   struct brw_gs_prog_data *gs_prog_data =
      rzalloc(mem_ctx, struct brw_gs_prog_data);
   struct brw_vue_prog_data *vue_prog_data = &gs_prog_data->base;
   struct brw_stage_prog_data *prog_data = &vue_prog_data->base;
   enum brw_param_builtin *system_values;
   unsigned num_system_values;
   unsigned num_cbufs;

   nir_shader *nir = nir_shader_clone(mem_ctx, ish->nir);

   if (key->nr_userclip_plane_consts) {
      nir_function_impl *impl = nir_shader_get_entrypoint(nir);
      nir_lower_clip_gs(nir, (1 << key->nr_userclip_plane_consts) - 1, false,
                        NULL);
      nir_lower_io_to_temporaries(nir, impl, true, false);
      nir_lower_global_vars_to_local(nir);
      nir_lower_vars_to_ssa(nir);
      nir_shader_gather_info(nir, impl);
   }

   crocus_setup_uniforms(compiler, mem_ctx, nir, prog_data, &system_values,
                         &num_system_values, &num_cbufs);
   crocus_lower_swizzles(nir, &key->base.tex);
   struct crocus_binding_table bt;
   crocus_setup_binding_table(devinfo, nir, &bt, /* num_render_targets */ 0,
                              num_system_values, num_cbufs, &key->base.tex);

   if (can_push_ubo(devinfo))
      brw_nir_analyze_ubo_ranges(compiler, nir, NULL, prog_data->ubo_ranges);

   brw_compute_vue_map(devinfo,
                       &vue_prog_data->vue_map, nir->info.outputs_written,
                       nir->info.separate_shader, /* pos slots */ 1);

   if (devinfo->ver == 6)
      gfx6_gs_xfb_setup(&ish->stream_output, gs_prog_data);
   struct brw_gs_prog_key key_clean = *key;
   crocus_sanitize_tex_key(&key_clean.base.tex);

   char *error_str = NULL;
   const unsigned *program =
      brw_compile_gs(compiler, &ice->dbg, mem_ctx, &key_clean, gs_prog_data, nir,
                     -1, NULL, &error_str);
   if (program == NULL) {
      dbg_printf("Failed to compile geometry shader: %s\n", error_str);
      ralloc_free(mem_ctx);
      return false;
   }

   if (ish->compiled_once) {
      crocus_debug_recompile(ice, &nir->info, &key->base);
   } else {
      ish->compiled_once = true;
   }

   uint32_t *so_decls = NULL;
   if (devinfo->ver > 6)
      so_decls = screen->vtbl.create_so_decl_list(&ish->stream_output,
                                                  &vue_prog_data->vue_map);

   struct crocus_compiled_shader *shader =
      crocus_upload_shader(ice, CROCUS_CACHE_GS, sizeof(*key), key, program,
                           prog_data->program_size,
                           prog_data, sizeof(*gs_prog_data), so_decls,
                           system_values, num_system_values,
                           num_cbufs, &bt);

   crocus_disk_cache_store(screen->disk_cache, ish, shader,
                           ice->shaders.cache_bo_map,
                           key, sizeof(*key));

   ralloc_free(mem_ctx);
   return shader;
}

/**
 * Update the current geometry shader variant.
 *
 * Fill out the key, look in the cache, compile and bind if needed.
 */
static void
crocus_update_compiled_gs(struct crocus_context *ice)
{
   struct crocus_shader_state *shs = &ice->state.shaders[MESA_SHADER_GEOMETRY];
   struct crocus_uncompiled_shader *ish =
      ice->shaders.uncompiled[MESA_SHADER_GEOMETRY];
   struct crocus_compiled_shader *old = ice->shaders.prog[CROCUS_CACHE_GS];
   struct crocus_compiled_shader *shader = NULL;

   if (ish) {
      struct crocus_screen *screen = (struct crocus_screen *)ice->ctx.screen;
      const struct intel_device_info *devinfo = &screen->devinfo;
      struct brw_gs_prog_key key = { KEY_INIT() };

      if (ish->nos & (1ull << CROCUS_NOS_TEXTURES))
         crocus_populate_sampler_prog_key_data(ice, devinfo, MESA_SHADER_GEOMETRY, ish,
                                               ish->nir->info.uses_texture_gather, &key.base.tex);
      screen->vtbl.populate_gs_key(ice, &ish->nir->info, last_vue_stage(ice), &key);

      shader =
         crocus_find_cached_shader(ice, CROCUS_CACHE_GS, sizeof(key), &key);

      if (!shader)
         shader = crocus_disk_cache_retrieve(ice, ish, &key, sizeof(key));

      if (!shader)
         shader = crocus_compile_gs(ice, ish, &key);
   }

   if (old != shader) {
      ice->shaders.prog[CROCUS_CACHE_GS] = shader;
      ice->state.stage_dirty |= CROCUS_STAGE_DIRTY_GS |
                                CROCUS_STAGE_DIRTY_BINDINGS_GS |
                                CROCUS_STAGE_DIRTY_CONSTANTS_GS;
      shs->sysvals_need_upload = true;
   }
}

/**
 * Compile a fragment (pixel) shader, and upload the assembly.
 */
static struct crocus_compiled_shader *
crocus_compile_fs(struct crocus_context *ice,
                  struct crocus_uncompiled_shader *ish,
                  const struct brw_wm_prog_key *key,
                  struct brw_vue_map *vue_map)
{
   struct crocus_screen *screen = (struct crocus_screen *)ice->ctx.screen;
   const struct brw_compiler *compiler = screen->compiler;
   void *mem_ctx = ralloc_context(NULL);
   struct brw_wm_prog_data *fs_prog_data =
      rzalloc(mem_ctx, struct brw_wm_prog_data);
   struct brw_stage_prog_data *prog_data = &fs_prog_data->base;
   enum brw_param_builtin *system_values;
   const struct intel_device_info *devinfo = &screen->devinfo;
   unsigned num_system_values;
   unsigned num_cbufs;

   nir_shader *nir = nir_shader_clone(mem_ctx, ish->nir);

   prog_data->use_alt_mode = ish->use_alt_mode;

   crocus_setup_uniforms(compiler, mem_ctx, nir, prog_data, &system_values,
                         &num_system_values, &num_cbufs);

   /* Lower output variables to load_output intrinsics before setting up
    * binding tables, so crocus_setup_binding_table can map any load_output
    * intrinsics to CROCUS_SURFACE_GROUP_RENDER_TARGET_READ on Gen8 for
    * non-coherent framebuffer fetches.
    */
   brw_nir_lower_fs_outputs(nir);

   /* lower swizzles before binding table */
   crocus_lower_swizzles(nir, &key->base.tex);
   int null_rts = 1;

   struct crocus_binding_table bt;
   crocus_setup_binding_table(devinfo, nir, &bt,
                              MAX2(key->nr_color_regions, null_rts),
                              num_system_values, num_cbufs,
                              &key->base.tex);

   if (can_push_ubo(devinfo))
      brw_nir_analyze_ubo_ranges(compiler, nir, NULL, prog_data->ubo_ranges);

   struct brw_wm_prog_key key_clean = *key;
   crocus_sanitize_tex_key(&key_clean.base.tex);

   struct brw_compile_fs_params params = {
      .nir = nir,
      .key = &key_clean,
      .prog_data = fs_prog_data,

      .allow_spilling = true,
      .vue_map = vue_map,

      .log_data = &ice->dbg,
   };
   const unsigned *program =
      brw_compile_fs(compiler, mem_ctx, &params);
   if (program == NULL) {
      dbg_printf("Failed to compile fragment shader: %s\n", params.error_str);
      ralloc_free(mem_ctx);
      return false;
   }

   if (ish->compiled_once) {
      crocus_debug_recompile(ice, &nir->info, &key->base);
   } else {
      ish->compiled_once = true;
   }

   struct crocus_compiled_shader *shader =
      crocus_upload_shader(ice, CROCUS_CACHE_FS, sizeof(*key), key, program,
                           prog_data->program_size,
                           prog_data, sizeof(*fs_prog_data), NULL,
                           system_values, num_system_values,
                           num_cbufs, &bt);

   crocus_disk_cache_store(screen->disk_cache, ish, shader,
                           ice->shaders.cache_bo_map,
                           key, sizeof(*key));

   ralloc_free(mem_ctx);
   return shader;
}

/**
 * Update the current fragment shader variant.
 *
 * Fill out the key, look in the cache, compile and bind if needed.
 */
static void
crocus_update_compiled_fs(struct crocus_context *ice)
{
   struct crocus_screen *screen = (struct crocus_screen *)ice->ctx.screen;
   const struct intel_device_info *devinfo = &screen->devinfo;
   struct crocus_shader_state *shs = &ice->state.shaders[MESA_SHADER_FRAGMENT];
   struct crocus_uncompiled_shader *ish =
      ice->shaders.uncompiled[MESA_SHADER_FRAGMENT];
   struct brw_wm_prog_key key = { KEY_INIT() };

   if (ish->nos & (1ull << CROCUS_NOS_TEXTURES))
      crocus_populate_sampler_prog_key_data(ice, devinfo, MESA_SHADER_FRAGMENT, ish,
                                            ish->nir->info.uses_texture_gather, &key.base.tex);
   screen->vtbl.populate_fs_key(ice, &ish->nir->info, &key);

   if (ish->nos & (1ull << CROCUS_NOS_LAST_VUE_MAP))
      key.input_slots_valid = ice->shaders.last_vue_map->slots_valid;

   struct crocus_compiled_shader *old = ice->shaders.prog[CROCUS_CACHE_FS];
   struct crocus_compiled_shader *shader =
      crocus_find_cached_shader(ice, CROCUS_CACHE_FS, sizeof(key), &key);

   if (!shader)
      shader = crocus_disk_cache_retrieve(ice, ish, &key, sizeof(key));

   if (!shader)
      shader = crocus_compile_fs(ice, ish, &key, ice->shaders.last_vue_map);

   if (old != shader) {
      // XXX: only need to flag CLIP if barycentric has NONPERSPECTIVE
      // toggles.  might be able to avoid flagging SBE too.
      ice->shaders.prog[CROCUS_CACHE_FS] = shader;
      ice->state.dirty |= CROCUS_DIRTY_WM;
      /* gen4 clip/sf rely on fs prog_data */
      if (devinfo->ver < 6)
         ice->state.dirty |= CROCUS_DIRTY_GEN4_CLIP_PROG | CROCUS_DIRTY_GEN4_SF_PROG;
      else
         ice->state.dirty |= CROCUS_DIRTY_CLIP;
      if (devinfo->ver == 6)
         ice->state.dirty |= CROCUS_DIRTY_RASTER;
      if (devinfo->ver >= 7)
         ice->state.dirty |= CROCUS_DIRTY_GEN7_SBE;
      ice->state.stage_dirty |= CROCUS_STAGE_DIRTY_FS |
                                CROCUS_STAGE_DIRTY_BINDINGS_FS |
                                CROCUS_STAGE_DIRTY_CONSTANTS_FS;
      shs->sysvals_need_upload = true;
   }
}

/**
 * Update the last enabled stage's VUE map.
 *
 * When the shader feeding the rasterizer's output interface changes, we
 * need to re-emit various packets.
 */
static void
update_last_vue_map(struct crocus_context *ice,
                    struct brw_stage_prog_data *prog_data)
{
   struct crocus_screen *screen = (struct crocus_screen *)ice->ctx.screen;
   const struct intel_device_info *devinfo = &screen->devinfo;
   struct brw_vue_prog_data *vue_prog_data = (void *) prog_data;
   struct brw_vue_map *vue_map = &vue_prog_data->vue_map;
   struct brw_vue_map *old_map = ice->shaders.last_vue_map;
   const uint64_t changed_slots =
      (old_map ? old_map->slots_valid : 0ull) ^ vue_map->slots_valid;

   if (changed_slots & VARYING_BIT_VIEWPORT) {
      ice->state.num_viewports =
         (vue_map->slots_valid & VARYING_BIT_VIEWPORT) ? CROCUS_MAX_VIEWPORTS : 1;
      ice->state.dirty |= CROCUS_DIRTY_SF_CL_VIEWPORT |
                          CROCUS_DIRTY_CC_VIEWPORT;
      if (devinfo->ver < 6)
         ice->state.dirty |= CROCUS_DIRTY_GEN4_CLIP_PROG | CROCUS_DIRTY_GEN4_SF_PROG;

      if (devinfo->ver <= 6)
         ice->state.dirty |= CROCUS_DIRTY_GEN4_FF_GS_PROG;

      if (devinfo->ver >= 6)
         ice->state.dirty |= CROCUS_DIRTY_CLIP |
                             CROCUS_DIRTY_GEN6_SCISSOR_RECT;;
      ice->state.stage_dirty |= CROCUS_STAGE_DIRTY_UNCOMPILED_FS |
         ice->state.stage_dirty_for_nos[CROCUS_NOS_LAST_VUE_MAP];
   }

   if (changed_slots || (old_map && old_map->separate != vue_map->separate)) {
      ice->state.dirty |= CROCUS_DIRTY_GEN7_SBE;
      ice->state.stage_dirty |= CROCUS_STAGE_DIRTY_UNCOMPILED_FS;
   }

   ice->shaders.last_vue_map = &vue_prog_data->vue_map;
}

static void
crocus_update_pull_constant_descriptors(struct crocus_context *ice,
                                        gl_shader_stage stage)
{
   struct crocus_compiled_shader *shader = ice->shaders.prog[stage];

   if (!shader || !shader->prog_data->has_ubo_pull)
      return;

   struct crocus_shader_state *shs = &ice->state.shaders[stage];
   bool any_new_descriptors =
      shader->num_system_values > 0 && shs->sysvals_need_upload;

   unsigned bound_cbufs = shs->bound_cbufs;

   while (bound_cbufs) {
      const int i = u_bit_scan(&bound_cbufs);
      struct pipe_constant_buffer *cbuf = &shs->constbufs[i];
      if (cbuf->buffer) {
         any_new_descriptors = true;
      }
   }

   if (any_new_descriptors)
      ice->state.stage_dirty |= CROCUS_STAGE_DIRTY_BINDINGS_VS << stage;
}

/**
 * Get the prog_data for a given stage, or NULL if the stage is disabled.
 */
static struct brw_vue_prog_data *
get_vue_prog_data(struct crocus_context *ice, gl_shader_stage stage)
{
   if (!ice->shaders.prog[stage])
      return NULL;

   return (void *) ice->shaders.prog[stage]->prog_data;
}

static struct crocus_compiled_shader *
crocus_compile_clip(struct crocus_context *ice, struct brw_clip_prog_key *key)
{
   struct crocus_screen *screen = (struct crocus_screen *)ice->ctx.screen;
   const struct brw_compiler *compiler = screen->compiler;
   void *mem_ctx;
   unsigned program_size;
   mem_ctx = ralloc_context(NULL);

   struct brw_clip_prog_data *clip_prog_data =
      rzalloc(mem_ctx, struct brw_clip_prog_data);

   const unsigned *program = brw_compile_clip(compiler, mem_ctx, key, clip_prog_data,
                                              ice->shaders.last_vue_map, &program_size);

   if (program == NULL) {
      dbg_printf("failed to compile clip shader\n");
      ralloc_free(mem_ctx);
      return false;
   }
   struct crocus_binding_table bt;
   memset(&bt, 0, sizeof(bt));

   struct crocus_compiled_shader *shader =
      crocus_upload_shader(ice, CROCUS_CACHE_CLIP, sizeof(*key), key, program,
                           program_size,
                           (struct brw_stage_prog_data *)clip_prog_data, sizeof(*clip_prog_data),
                           NULL, NULL, 0, 0, &bt);
   ralloc_free(mem_ctx);
   return shader;
}
static void
crocus_update_compiled_clip(struct crocus_context *ice)
{
   struct crocus_screen *screen = (struct crocus_screen *)ice->ctx.screen;
   struct brw_clip_prog_key key;
   struct crocus_compiled_shader *old = ice->shaders.clip_prog;
   memset(&key, 0, sizeof(key));

   const struct brw_wm_prog_data *wm_prog_data = brw_wm_prog_data(ice->shaders.prog[MESA_SHADER_FRAGMENT]->prog_data);
   if (wm_prog_data) {
      key.contains_flat_varying = wm_prog_data->contains_flat_varying;
      key.contains_noperspective_varying =
         wm_prog_data->contains_noperspective_varying;
      memcpy(key.interp_mode, wm_prog_data->interp_mode, sizeof(key.interp_mode));
   }

   key.primitive = u_reduced_prim(ice->state.prim_mode);
   key.attrs = ice->shaders.last_vue_map->slots_valid;

   struct pipe_rasterizer_state *rs_state = crocus_get_rast_state(ice);
   key.pv_first = rs_state->flatshade_first;

   if (rs_state->clip_plane_enable)
      key.nr_userclip = util_logbase2(rs_state->clip_plane_enable) + 1;

   if (screen->devinfo.ver == 5)
      key.clip_mode = BRW_CLIP_MODE_KERNEL_CLIP;
   else
      key.clip_mode = BRW_CLIP_MODE_NORMAL;

   if (key.primitive == PIPE_PRIM_TRIANGLES) {
      if (rs_state->cull_face == PIPE_FACE_FRONT_AND_BACK)
         key.clip_mode = BRW_CLIP_MODE_REJECT_ALL;
      else {
         uint32_t fill_front = BRW_CLIP_FILL_MODE_CULL;
         uint32_t fill_back = BRW_CLIP_FILL_MODE_CULL;
         uint32_t offset_front = 0;
         uint32_t offset_back = 0;

         if (!(rs_state->cull_face & PIPE_FACE_FRONT)) {
            switch (rs_state->fill_front) {
            case PIPE_POLYGON_MODE_FILL:
               fill_front = BRW_CLIP_FILL_MODE_FILL;
               offset_front = 0;
               break;
            case PIPE_POLYGON_MODE_LINE:
               fill_front = BRW_CLIP_FILL_MODE_LINE;
               offset_front = rs_state->offset_line;
               break;
            case PIPE_POLYGON_MODE_POINT:
               fill_front = BRW_CLIP_FILL_MODE_POINT;
               offset_front = rs_state->offset_point;
               break;
            }
         }

         if (!(rs_state->cull_face & PIPE_FACE_BACK)) {
            switch (rs_state->fill_back) {
            case PIPE_POLYGON_MODE_FILL:
               fill_back = BRW_CLIP_FILL_MODE_FILL;
               offset_back = 0;
               break;
            case PIPE_POLYGON_MODE_LINE:
               fill_back = BRW_CLIP_FILL_MODE_LINE;
               offset_back = rs_state->offset_line;
               break;
            case PIPE_POLYGON_MODE_POINT:
               fill_back = BRW_CLIP_FILL_MODE_POINT;
               offset_back = rs_state->offset_point;
               break;
            }
         }

         if (rs_state->fill_back != PIPE_POLYGON_MODE_FILL ||
             rs_state->fill_front != PIPE_POLYGON_MODE_FILL) {
            key.do_unfilled = 1;

            /* Most cases the fixed function units will handle.  Cases where
             * one or more polygon faces are unfilled will require help:
             */
            key.clip_mode = BRW_CLIP_MODE_CLIP_NON_REJECTED;

            if (offset_back || offset_front) {
               double mrd = 0.0;
               if (ice->state.framebuffer.zsbuf)
                  mrd = util_get_depth_format_mrd(util_format_description(ice->state.framebuffer.zsbuf->format));
               key.offset_units = rs_state->offset_units * mrd * 2;
               key.offset_factor = rs_state->offset_scale * mrd;
               key.offset_clamp = rs_state->offset_clamp * mrd;
            }

            if (!(rs_state->front_ccw ^ rs_state->bottom_edge_rule)) {
               key.fill_ccw = fill_front;
               key.fill_cw = fill_back;
               key.offset_ccw = offset_front;
               key.offset_cw = offset_back;
               if (rs_state->light_twoside &&
                   key.fill_cw != BRW_CLIP_FILL_MODE_CULL)
                  key.copy_bfc_cw = 1;
            } else {
               key.fill_cw = fill_front;
               key.fill_ccw = fill_back;
               key.offset_cw = offset_front;
               key.offset_ccw = offset_back;
               if (rs_state->light_twoside &&
                   key.fill_ccw != BRW_CLIP_FILL_MODE_CULL)
                  key.copy_bfc_ccw = 1;
            }
         }
      }
   }
   struct crocus_compiled_shader *shader =
      crocus_find_cached_shader(ice, CROCUS_CACHE_CLIP, sizeof(key), &key);

   if (!shader)
      shader = crocus_compile_clip(ice, &key);

   if (old != shader) {
      ice->state.dirty |= CROCUS_DIRTY_CLIP;
      ice->shaders.clip_prog = shader;
   }
}

static struct crocus_compiled_shader *
crocus_compile_sf(struct crocus_context *ice, struct brw_sf_prog_key *key)
{
   struct crocus_screen *screen = (struct crocus_screen *)ice->ctx.screen;
   const struct brw_compiler *compiler = screen->compiler;
   void *mem_ctx;
   unsigned program_size;
   mem_ctx = ralloc_context(NULL);

   struct brw_sf_prog_data *sf_prog_data =
      rzalloc(mem_ctx, struct brw_sf_prog_data);

   const unsigned *program = brw_compile_sf(compiler, mem_ctx, key, sf_prog_data,
                                            ice->shaders.last_vue_map, &program_size);

   if (program == NULL) {
      dbg_printf("failed to compile sf shader\n");
      ralloc_free(mem_ctx);
      return false;
   }

   struct crocus_binding_table bt;
   memset(&bt, 0, sizeof(bt));
   struct crocus_compiled_shader *shader =
      crocus_upload_shader(ice, CROCUS_CACHE_SF, sizeof(*key), key, program,
                           program_size,
                           (struct brw_stage_prog_data *)sf_prog_data, sizeof(*sf_prog_data),
                           NULL, NULL, 0, 0, &bt);
   ralloc_free(mem_ctx);
   return shader;
}

static void
crocus_update_compiled_sf(struct crocus_context *ice)
{
   struct brw_sf_prog_key key;
   struct crocus_compiled_shader *old = ice->shaders.sf_prog;
   memset(&key, 0, sizeof(key));

   key.attrs = ice->shaders.last_vue_map->slots_valid;

   switch (u_reduced_prim(ice->state.prim_mode)) {
   case GL_TRIANGLES:
   default:
      if (key.attrs & BITFIELD64_BIT(VARYING_SLOT_EDGE))
         key.primitive = BRW_SF_PRIM_UNFILLED_TRIS;
      else
         key.primitive = BRW_SF_PRIM_TRIANGLES;
      break;
   case GL_LINES:
      key.primitive = BRW_SF_PRIM_LINES;
      break;
   case GL_POINTS:
      key.primitive = BRW_SF_PRIM_POINTS;
      break;
   }

   struct pipe_rasterizer_state *rs_state = crocus_get_rast_state(ice);
   key.userclip_active = rs_state->clip_plane_enable != 0;
   const struct brw_wm_prog_data *wm_prog_data = brw_wm_prog_data(ice->shaders.prog[MESA_SHADER_FRAGMENT]->prog_data);
   if (wm_prog_data) {
      key.contains_flat_varying = wm_prog_data->contains_flat_varying;
      memcpy(key.interp_mode, wm_prog_data->interp_mode, sizeof(key.interp_mode));
   }

   key.do_twoside_color = rs_state->light_twoside;

   key.do_point_sprite = rs_state->point_quad_rasterization;
   if (key.do_point_sprite) {
      key.point_sprite_coord_replace = rs_state->sprite_coord_enable & 0xff;
      if (rs_state->sprite_coord_enable & (1 << 8))
         key.do_point_coord = 1;
      if (wm_prog_data && wm_prog_data->urb_setup[VARYING_SLOT_PNTC] != -1)
         key.do_point_coord = 1;
   }

   key.sprite_origin_lower_left = rs_state->sprite_coord_mode == PIPE_SPRITE_COORD_LOWER_LEFT;

   if (key.do_twoside_color) {
      key.frontface_ccw = rs_state->front_ccw;
   }
   struct crocus_compiled_shader *shader =
      crocus_find_cached_shader(ice, CROCUS_CACHE_SF, sizeof(key), &key);

   if (!shader)
      shader = crocus_compile_sf(ice, &key);

   if (old != shader) {
      ice->state.dirty |= CROCUS_DIRTY_RASTER;
      ice->shaders.sf_prog = shader;
   }
}

static struct crocus_compiled_shader *
crocus_compile_ff_gs(struct crocus_context *ice, struct brw_ff_gs_prog_key *key)
{
   struct crocus_screen *screen = (struct crocus_screen *)ice->ctx.screen;
   struct brw_compiler *compiler = screen->compiler;
   void *mem_ctx;
   unsigned program_size;
   mem_ctx = ralloc_context(NULL);

   struct brw_ff_gs_prog_data *ff_gs_prog_data =
      rzalloc(mem_ctx, struct brw_ff_gs_prog_data);

   const unsigned *program = brw_compile_ff_gs_prog(compiler, mem_ctx, key, ff_gs_prog_data,
                                                    ice->shaders.last_vue_map, &program_size);

   if (program == NULL) {
      dbg_printf("failed to compile sf shader\n");
      ralloc_free(mem_ctx);
      return false;
   }

   struct crocus_binding_table bt;
   memset(&bt, 0, sizeof(bt));

   if (screen->devinfo.ver == 6) {
      bt.sizes[CROCUS_SURFACE_GROUP_SOL] = BRW_MAX_SOL_BINDINGS;
      bt.used_mask[CROCUS_SURFACE_GROUP_SOL] = (uint64_t)-1;

      bt.size_bytes = BRW_MAX_SOL_BINDINGS * 4;
   }

   struct crocus_compiled_shader *shader =
      crocus_upload_shader(ice, CROCUS_CACHE_FF_GS, sizeof(*key), key, program,
                           program_size,
                           (struct brw_stage_prog_data *)ff_gs_prog_data, sizeof(*ff_gs_prog_data),
                           NULL, NULL, 0, 0, &bt);
   ralloc_free(mem_ctx);
   return shader;
}

static void
crocus_update_compiled_ff_gs(struct crocus_context *ice)
{
   struct crocus_screen *screen = (struct crocus_screen *)ice->ctx.screen;
   const struct intel_device_info *devinfo = &screen->devinfo;
   struct brw_ff_gs_prog_key key;
   struct crocus_compiled_shader *old = ice->shaders.ff_gs_prog;
   memset(&key, 0, sizeof(key));

   assert(devinfo->ver < 7);

   key.attrs = ice->shaders.last_vue_map->slots_valid;

   key.primitive = screen->vtbl.translate_prim_type(ice->state.prim_mode, 0);

   struct pipe_rasterizer_state *rs_state = crocus_get_rast_state(ice);
   key.pv_first = rs_state->flatshade_first;

   if (key.primitive == _3DPRIM_QUADLIST && !rs_state->flatshade) {
      /* Provide consistenbbbbbt primitive order with brw_set_prim's
       * optimization of single quads to trifans.
       */
      key.pv_first = true;
   }

   if (devinfo->ver >= 6) {
      key.need_gs_prog = ice->state.streamout_active;
      if (key.need_gs_prog) {
         struct crocus_uncompiled_shader *vs =
            ice->shaders.uncompiled[MESA_SHADER_VERTEX];
         gfx6_ff_gs_xfb_setup(&vs->stream_output,
                              &key);
      }
   } else {
      key.need_gs_prog = (key.primitive == _3DPRIM_QUADLIST ||
                          key.primitive == _3DPRIM_QUADSTRIP ||
                          key.primitive == _3DPRIM_LINELOOP);
   }

   struct crocus_compiled_shader *shader = NULL;
   if (key.need_gs_prog) {
      shader = crocus_find_cached_shader(ice, CROCUS_CACHE_FF_GS,
                                         sizeof(key), &key);
      if (!shader)
         shader = crocus_compile_ff_gs(ice, &key);
   }
   if (old != shader) {
      ice->state.stage_dirty |= CROCUS_STAGE_DIRTY_GS;
      if (!!old != !!shader)
         ice->state.dirty |= CROCUS_DIRTY_GEN6_URB;
      ice->shaders.ff_gs_prog = shader;
      if (shader) {
         const struct brw_ff_gs_prog_data *gs_prog_data = (struct brw_ff_gs_prog_data *)ice->shaders.ff_gs_prog->prog_data;
         ice->state.last_xfb_verts_per_prim = gs_prog_data->svbi_postincrement_value;
      }
   }
}

// XXX: crocus_compiled_shaders are space-leaking :(
// XXX: do remember to unbind them if deleting them.

/**
 * Update the current shader variants for the given state.
 *
 * This should be called on every draw call to ensure that the correct
 * shaders are bound.  It will also flag any dirty state triggered by
 * swapping out those shaders.
 */
bool
crocus_update_compiled_shaders(struct crocus_context *ice)
{
   struct crocus_screen *screen = (void *) ice->ctx.screen;
   const uint64_t stage_dirty = ice->state.stage_dirty;

   struct brw_vue_prog_data *old_prog_datas[4];
   if (!(ice->state.dirty & CROCUS_DIRTY_GEN6_URB)) {
      for (int i = MESA_SHADER_VERTEX; i <= MESA_SHADER_GEOMETRY; i++)
         old_prog_datas[i] = get_vue_prog_data(ice, i);
   }

   if (stage_dirty & (CROCUS_STAGE_DIRTY_UNCOMPILED_TCS |
                      CROCUS_STAGE_DIRTY_UNCOMPILED_TES)) {
      struct crocus_uncompiled_shader *tes =
         ice->shaders.uncompiled[MESA_SHADER_TESS_EVAL];
      if (tes) {
         crocus_update_compiled_tcs(ice);
         crocus_update_compiled_tes(ice);
      } else {
         ice->shaders.prog[CROCUS_CACHE_TCS] = NULL;
         ice->shaders.prog[CROCUS_CACHE_TES] = NULL;
         ice->state.stage_dirty |=
            CROCUS_STAGE_DIRTY_TCS | CROCUS_STAGE_DIRTY_TES |
            CROCUS_STAGE_DIRTY_BINDINGS_TCS | CROCUS_STAGE_DIRTY_BINDINGS_TES |
            CROCUS_STAGE_DIRTY_CONSTANTS_TCS | CROCUS_STAGE_DIRTY_CONSTANTS_TES;
      }
   }

   if (stage_dirty & CROCUS_STAGE_DIRTY_UNCOMPILED_VS)
      crocus_update_compiled_vs(ice);
   if (stage_dirty & CROCUS_STAGE_DIRTY_UNCOMPILED_GS)
      crocus_update_compiled_gs(ice);

   if (stage_dirty & (CROCUS_STAGE_DIRTY_UNCOMPILED_GS |
                      CROCUS_STAGE_DIRTY_UNCOMPILED_TES)) {
      const struct crocus_compiled_shader *gs =
         ice->shaders.prog[MESA_SHADER_GEOMETRY];
      const struct crocus_compiled_shader *tes =
         ice->shaders.prog[MESA_SHADER_TESS_EVAL];

      bool points_or_lines = false;

      if (gs) {
         const struct brw_gs_prog_data *gs_prog_data = (void *) gs->prog_data;
         points_or_lines =
            gs_prog_data->output_topology == _3DPRIM_POINTLIST ||
            gs_prog_data->output_topology == _3DPRIM_LINESTRIP;
      } else if (tes) {
         const struct brw_tes_prog_data *tes_data = (void *) tes->prog_data;
         points_or_lines =
            tes_data->output_topology == BRW_TESS_OUTPUT_TOPOLOGY_LINE ||
            tes_data->output_topology == BRW_TESS_OUTPUT_TOPOLOGY_POINT;
      }

      if (ice->shaders.output_topology_is_points_or_lines != points_or_lines) {
         /* Outbound to XY Clip enables */
         ice->shaders.output_topology_is_points_or_lines = points_or_lines;
         ice->state.dirty |= CROCUS_DIRTY_CLIP;
      }
   }

   if (!ice->shaders.prog[MESA_SHADER_VERTEX])
      return false;

   gl_shader_stage last_stage = last_vue_stage(ice);
   struct crocus_compiled_shader *shader = ice->shaders.prog[last_stage];
   struct crocus_uncompiled_shader *ish = ice->shaders.uncompiled[last_stage];
   update_last_vue_map(ice, shader->prog_data);
   if (ice->state.streamout != shader->streamout) {
      ice->state.streamout = shader->streamout;
      ice->state.dirty |= CROCUS_DIRTY_SO_DECL_LIST | CROCUS_DIRTY_STREAMOUT;
   }

   if (ice->state.streamout_active) {
      screen->vtbl.update_so_strides(ice, ish->stream_output.stride);
   }

   /* use ice->state version as last_vue_map can dirty this bit */
   if (ice->state.stage_dirty & CROCUS_STAGE_DIRTY_UNCOMPILED_FS)
      crocus_update_compiled_fs(ice);

   if (screen->devinfo.ver <= 6) {
      if (ice->state.dirty & CROCUS_DIRTY_GEN4_FF_GS_PROG &&
          !ice->shaders.prog[MESA_SHADER_GEOMETRY])
         crocus_update_compiled_ff_gs(ice);
   }

   if (screen->devinfo.ver < 6) {
      if (ice->state.dirty & CROCUS_DIRTY_GEN4_CLIP_PROG)
         crocus_update_compiled_clip(ice);
      if (ice->state.dirty & CROCUS_DIRTY_GEN4_SF_PROG)
         crocus_update_compiled_sf(ice);
   }


   /* Changing shader interfaces may require a URB configuration. */
   if (!(ice->state.dirty & CROCUS_DIRTY_GEN6_URB)) {
      for (int i = MESA_SHADER_VERTEX; i <= MESA_SHADER_GEOMETRY; i++) {
         struct brw_vue_prog_data *old = old_prog_datas[i];
         struct brw_vue_prog_data *new = get_vue_prog_data(ice, i);
         if (!!old != !!new ||
             (new && new->urb_entry_size != old->urb_entry_size)) {
            ice->state.dirty |= CROCUS_DIRTY_GEN6_URB;
            break;
         }
      }
   }

   if (ice->state.stage_dirty & CROCUS_RENDER_STAGE_DIRTY_CONSTANTS) {
      for (int i = MESA_SHADER_VERTEX; i <= MESA_SHADER_FRAGMENT; i++) {
         if (ice->state.stage_dirty & (CROCUS_STAGE_DIRTY_CONSTANTS_VS << i))
            crocus_update_pull_constant_descriptors(ice, i);
      }
   }
   return true;
}

static struct crocus_compiled_shader *
crocus_compile_cs(struct crocus_context *ice,
                  struct crocus_uncompiled_shader *ish,
                  const struct brw_cs_prog_key *key)
{
   struct crocus_screen *screen = (struct crocus_screen *)ice->ctx.screen;
   const struct brw_compiler *compiler = screen->compiler;
   void *mem_ctx = ralloc_context(NULL);
   struct brw_cs_prog_data *cs_prog_data =
      rzalloc(mem_ctx, struct brw_cs_prog_data);
   struct brw_stage_prog_data *prog_data = &cs_prog_data->base;
   enum brw_param_builtin *system_values;
   const struct intel_device_info *devinfo = &screen->devinfo;
   unsigned num_system_values;
   unsigned num_cbufs;

   nir_shader *nir = nir_shader_clone(mem_ctx, ish->nir);

   NIR_PASS_V(nir, brw_nir_lower_cs_intrinsics);

   crocus_setup_uniforms(compiler, mem_ctx, nir, prog_data, &system_values,
                         &num_system_values, &num_cbufs);
   crocus_lower_swizzles(nir, &key->base.tex);
   struct crocus_binding_table bt;
   crocus_setup_binding_table(devinfo, nir, &bt, /* num_render_targets */ 0,
                              num_system_values, num_cbufs, &key->base.tex);

   struct brw_compile_cs_params params = {
      .nir = nir,
      .key = key,
      .prog_data = cs_prog_data,
      .log_data = &ice->dbg,
   };

   const unsigned *program =
      brw_compile_cs(compiler, mem_ctx, &params);
   if (program == NULL) {
      dbg_printf("Failed to compile compute shader: %s\n", params.error_str);
      ralloc_free(mem_ctx);
      return false;
   }

   if (ish->compiled_once) {
      crocus_debug_recompile(ice, &nir->info, &key->base);
   } else {
      ish->compiled_once = true;
   }

   struct crocus_compiled_shader *shader =
      crocus_upload_shader(ice, CROCUS_CACHE_CS, sizeof(*key), key, program,
                           prog_data->program_size,
                           prog_data, sizeof(*cs_prog_data), NULL,
                           system_values, num_system_values,
                           num_cbufs, &bt);

   crocus_disk_cache_store(screen->disk_cache, ish, shader,
                           ice->shaders.cache_bo_map,
                           key, sizeof(*key));

   ralloc_free(mem_ctx);
   return shader;
}

static void
crocus_update_compiled_cs(struct crocus_context *ice)
{
   struct crocus_shader_state *shs = &ice->state.shaders[MESA_SHADER_COMPUTE];
   struct crocus_uncompiled_shader *ish =
      ice->shaders.uncompiled[MESA_SHADER_COMPUTE];
   struct crocus_screen *screen = (struct crocus_screen *)ice->ctx.screen;
   const struct intel_device_info *devinfo = &screen->devinfo;
   struct brw_cs_prog_key key = { KEY_INIT() };

   if (ish->nos & (1ull << CROCUS_NOS_TEXTURES))
      crocus_populate_sampler_prog_key_data(ice, devinfo, MESA_SHADER_COMPUTE, ish,
                                            ish->nir->info.uses_texture_gather, &key.base.tex);
   screen->vtbl.populate_cs_key(ice, &key);

   struct crocus_compiled_shader *old = ice->shaders.prog[CROCUS_CACHE_CS];
   struct crocus_compiled_shader *shader =
      crocus_find_cached_shader(ice, CROCUS_CACHE_CS, sizeof(key), &key);

   if (!shader)
      shader = crocus_disk_cache_retrieve(ice, ish, &key, sizeof(key));

   if (!shader)
      shader = crocus_compile_cs(ice, ish, &key);

   if (old != shader) {
      ice->shaders.prog[CROCUS_CACHE_CS] = shader;
      ice->state.stage_dirty |= CROCUS_STAGE_DIRTY_CS |
                          CROCUS_STAGE_DIRTY_BINDINGS_CS |
                          CROCUS_STAGE_DIRTY_CONSTANTS_CS;
      shs->sysvals_need_upload = true;
   }
}

void
crocus_update_compiled_compute_shader(struct crocus_context *ice)
{
   if (ice->state.stage_dirty & CROCUS_STAGE_DIRTY_UNCOMPILED_CS)
      crocus_update_compiled_cs(ice);

   if (ice->state.stage_dirty & CROCUS_STAGE_DIRTY_CONSTANTS_CS)
      crocus_update_pull_constant_descriptors(ice, MESA_SHADER_COMPUTE);
}

void
crocus_fill_cs_push_const_buffer(struct brw_cs_prog_data *cs_prog_data,
                                 unsigned threads,
                                 uint32_t *dst)
{
   assert(brw_cs_push_const_total_size(cs_prog_data, threads) > 0);
   assert(cs_prog_data->push.cross_thread.size == 0);
   assert(cs_prog_data->push.per_thread.dwords == 1);
   assert(cs_prog_data->base.param[0] == BRW_PARAM_BUILTIN_SUBGROUP_ID);
   for (unsigned t = 0; t < threads; t++)
      dst[8 * t] = t;
}

/**
 * Allocate scratch BOs as needed for the given per-thread size and stage.
 */
struct crocus_bo *
crocus_get_scratch_space(struct crocus_context *ice,
                         unsigned per_thread_scratch,
                         gl_shader_stage stage)
{
   struct crocus_screen *screen = (struct crocus_screen *)ice->ctx.screen;
   struct crocus_bufmgr *bufmgr = screen->bufmgr;
   const struct intel_device_info *devinfo = &screen->devinfo;

   unsigned encoded_size = ffs(per_thread_scratch) - 11;
   assert(encoded_size < (1 << 16));

   struct crocus_bo **bop = &ice->shaders.scratch_bos[encoded_size][stage];

   unsigned subslice_total = screen->subslice_total;
   subslice_total = 4 * devinfo->num_slices;
   //   assert(subslice_total >= screen->subslice_total);

   if (!*bop) {
      unsigned scratch_ids_per_subslice = devinfo->max_cs_threads;

      uint32_t max_threads[] = {
         [MESA_SHADER_VERTEX]    = devinfo->max_vs_threads,
         [MESA_SHADER_TESS_CTRL] = devinfo->max_tcs_threads,
         [MESA_SHADER_TESS_EVAL] = devinfo->max_tes_threads,
         [MESA_SHADER_GEOMETRY]  = devinfo->max_gs_threads,
         [MESA_SHADER_FRAGMENT]  = devinfo->max_wm_threads,
         [MESA_SHADER_COMPUTE]   = scratch_ids_per_subslice * subslice_total,
      };

      uint32_t size = per_thread_scratch * max_threads[stage];

      *bop = crocus_bo_alloc(bufmgr, "scratch", size);
   }

   return *bop;
}

/* ------------------------------------------------------------------- */

/**
 * The pipe->create_[stage]_state() driver hooks.
 *
 * Performs basic NIR preprocessing, records any state dependencies, and
 * returns an crocus_uncompiled_shader as the Gallium CSO.
 *
 * Actual shader compilation to assembly happens later, at first use.
 */
static void *
crocus_create_uncompiled_shader(struct pipe_context *ctx,
                                nir_shader *nir,
                                const struct pipe_stream_output_info *so_info)
{
   struct crocus_screen *screen = (struct crocus_screen *)ctx->screen;
   const struct intel_device_info *devinfo = &screen->devinfo;
   struct crocus_uncompiled_shader *ish =
      calloc(1, sizeof(struct crocus_uncompiled_shader));
   if (!ish)
      return NULL;

   if (devinfo->ver >= 6)
     NIR_PASS(ish->needs_edge_flag, nir, crocus_fix_edge_flags);
   else
     ish->needs_edge_flag = false;

   brw_preprocess_nir(screen->compiler, nir, NULL);

   NIR_PASS_V(nir, brw_nir_lower_image_load_store, devinfo, false);
   NIR_PASS_V(nir, crocus_lower_storage_image_derefs);

   nir_sweep(nir);

   ish->program_id = get_new_program_id(screen);
   ish->nir = nir;
   if (so_info) {
      memcpy(&ish->stream_output, so_info, sizeof(*so_info));
      update_so_info(&ish->stream_output, nir->info.outputs_written);
   }

   /* Save this now before potentially dropping nir->info.name */
   if (nir->info.name && strncmp(nir->info.name, "ARB", 3) == 0)
      ish->use_alt_mode = true;

   if (screen->disk_cache) {
      /* Serialize the NIR to a binary blob that we can hash for the disk
       * cache.  Drop unnecessary information (like variable names)
       * so the serialized NIR is smaller, and also to let us detect more
       * isomorphic shaders when hashing, increasing cache hits.
       */
      struct blob blob;
      blob_init(&blob);
      nir_serialize(&blob, nir, true);
      _mesa_sha1_compute(blob.data, blob.size, ish->nir_sha1);
      blob_finish(&blob);
   }

   return ish;
}

static struct crocus_uncompiled_shader *
crocus_create_shader_state(struct pipe_context *ctx,
                           const struct pipe_shader_state *state)
{
   struct nir_shader *nir;

   if (state->type == PIPE_SHADER_IR_TGSI)
      nir = tgsi_to_nir(state->tokens, ctx->screen, false);
   else
      nir = state->ir.nir;

   return crocus_create_uncompiled_shader(ctx, nir, &state->stream_output);
}

static void *
crocus_create_vs_state(struct pipe_context *ctx,
                       const struct pipe_shader_state *state)
{
   struct crocus_context *ice = (void *) ctx;
   struct crocus_screen *screen = (void *) ctx->screen;
   struct crocus_uncompiled_shader *ish = crocus_create_shader_state(ctx, state);

   ish->nos |= (1ull << CROCUS_NOS_TEXTURES);
   /* User clip planes or gen5 sprite coord enable */
   if (ish->nir->info.clip_distance_array_size == 0 ||
       screen->devinfo.ver <= 5)
      ish->nos |= (1ull << CROCUS_NOS_RASTERIZER);

   if (!screen->devinfo.is_haswell)
      ish->nos |= (1ull << CROCUS_NOS_VERTEX_ELEMENTS);

   if (screen->precompile) {
      struct brw_vs_prog_key key = { KEY_INIT() };

      if (!crocus_disk_cache_retrieve(ice, ish, &key, sizeof(key)))
         crocus_compile_vs(ice, ish, &key);
   }

   return ish;
}

static void *
crocus_create_tcs_state(struct pipe_context *ctx,
                        const struct pipe_shader_state *state)
{
   struct crocus_context *ice = (void *) ctx;
   struct crocus_screen *screen = (void *) ctx->screen;
   struct crocus_uncompiled_shader *ish = crocus_create_shader_state(ctx, state);
   struct shader_info *info = &ish->nir->info;

   ish->nos |= (1ull << CROCUS_NOS_TEXTURES);
   if (screen->precompile) {
      const unsigned _GL_TRIANGLES = 0x0004;
      struct brw_tcs_prog_key key = {
         KEY_INIT(),
         // XXX: make sure the linker fills this out from the TES...
         .tes_primitive_mode =
            info->tess.primitive_mode ? info->tess.primitive_mode
                                      : _GL_TRIANGLES,
         .outputs_written = info->outputs_written,
         .patch_outputs_written = info->patch_outputs_written,
      };

      key.input_vertices = info->tess.tcs_vertices_out;

      if (!crocus_disk_cache_retrieve(ice, ish, &key, sizeof(key)))
         crocus_compile_tcs(ice, ish, &key);
   }

   return ish;
}

static void *
crocus_create_tes_state(struct pipe_context *ctx,
                        const struct pipe_shader_state *state)
{
   struct crocus_context *ice = (void *) ctx;
   struct crocus_screen *screen = (void *) ctx->screen;
   struct crocus_uncompiled_shader *ish = crocus_create_shader_state(ctx, state);
   struct shader_info *info = &ish->nir->info;

   ish->nos |= (1ull << CROCUS_NOS_TEXTURES);
   /* User clip planes */
   if (ish->nir->info.clip_distance_array_size == 0)
      ish->nos |= (1ull << CROCUS_NOS_RASTERIZER);

   if (screen->precompile) {
      struct brw_tes_prog_key key = {
         KEY_INIT(),
         // XXX: not ideal, need TCS output/TES input unification
         .inputs_read = info->inputs_read,
         .patch_inputs_read = info->patch_inputs_read,
      };

      if (!crocus_disk_cache_retrieve(ice, ish, &key, sizeof(key)))
         crocus_compile_tes(ice, ish, &key);
   }

   return ish;
}

static void *
crocus_create_gs_state(struct pipe_context *ctx,
                       const struct pipe_shader_state *state)
{
   struct crocus_context *ice = (void *) ctx;
   struct crocus_screen *screen = (void *) ctx->screen;
   struct crocus_uncompiled_shader *ish = crocus_create_shader_state(ctx, state);

   ish->nos |= (1ull << CROCUS_NOS_TEXTURES);
   /* User clip planes */
   if (ish->nir->info.clip_distance_array_size == 0)
      ish->nos |= (1ull << CROCUS_NOS_RASTERIZER);

   if (screen->precompile) {
      struct brw_gs_prog_key key = { KEY_INIT() };

      if (!crocus_disk_cache_retrieve(ice, ish, &key, sizeof(key)))
         crocus_compile_gs(ice, ish, &key);
   }

   return ish;
}

static void *
crocus_create_fs_state(struct pipe_context *ctx,
                       const struct pipe_shader_state *state)
{
   struct crocus_context *ice = (void *) ctx;
   struct crocus_screen *screen = (void *) ctx->screen;
   struct crocus_uncompiled_shader *ish = crocus_create_shader_state(ctx, state);
   struct shader_info *info = &ish->nir->info;

   ish->nos |= (1ull << CROCUS_NOS_FRAMEBUFFER) |
               (1ull << CROCUS_NOS_DEPTH_STENCIL_ALPHA) |
               (1ull << CROCUS_NOS_RASTERIZER) |
               (1ull << CROCUS_NOS_TEXTURES) |
               (1ull << CROCUS_NOS_BLEND);

   /* The program key needs the VUE map if there are > 16 inputs or gen4/5 */
   if (screen->devinfo.ver < 6 || util_bitcount64(ish->nir->info.inputs_read &
                                                  BRW_FS_VARYING_INPUT_MASK) > 16) {
      ish->nos |= (1ull << CROCUS_NOS_LAST_VUE_MAP);
   }

   if (screen->precompile) {
      const uint64_t color_outputs = info->outputs_written &
         ~(BITFIELD64_BIT(FRAG_RESULT_DEPTH) |
           BITFIELD64_BIT(FRAG_RESULT_STENCIL) |
           BITFIELD64_BIT(FRAG_RESULT_SAMPLE_MASK));

      bool can_rearrange_varyings =
         screen->devinfo.ver > 6 && util_bitcount64(info->inputs_read & BRW_FS_VARYING_INPUT_MASK) <= 16;

      const struct intel_device_info *devinfo = &screen->devinfo;
      struct brw_wm_prog_key key = {
         KEY_INIT(),
         .nr_color_regions = util_bitcount(color_outputs),
         .coherent_fb_fetch = false,
         .input_slots_valid =
         can_rearrange_varyings ? 0 : info->inputs_read | VARYING_BIT_POS,
      };

      struct brw_vue_map vue_map;
      if (devinfo->ver < 6) {
         brw_compute_vue_map(devinfo, &vue_map,
                             info->inputs_read | VARYING_BIT_POS,
                             false, /* pos slots */ 1);
      }
      if (!crocus_disk_cache_retrieve(ice, ish, &key, sizeof(key)))
         crocus_compile_fs(ice, ish, &key, &vue_map);
   }

   return ish;
}

static void *
crocus_create_compute_state(struct pipe_context *ctx,
                            const struct pipe_compute_state *state)
{
   assert(state->ir_type == PIPE_SHADER_IR_NIR);

   struct crocus_context *ice = (void *) ctx;
   struct crocus_screen *screen = (void *) ctx->screen;
   struct crocus_uncompiled_shader *ish =
      crocus_create_uncompiled_shader(ctx, (void *) state->prog, NULL);

   ish->nos |= (1ull << CROCUS_NOS_TEXTURES);
   // XXX: disallow more than 64KB of shared variables

   if (screen->precompile) {
      struct brw_cs_prog_key key = { KEY_INIT() };

      if (!crocus_disk_cache_retrieve(ice, ish, &key, sizeof(key)))
         crocus_compile_cs(ice, ish, &key);
   }

   return ish;
}

/**
 * The pipe->delete_[stage]_state() driver hooks.
 *
 * Frees the crocus_uncompiled_shader.
 */
static void
crocus_delete_shader_state(struct pipe_context *ctx, void *state, gl_shader_stage stage)
{
   struct crocus_uncompiled_shader *ish = state;
   struct crocus_context *ice = (void *) ctx;

   if (ice->shaders.uncompiled[stage] == ish) {
      ice->shaders.uncompiled[stage] = NULL;
      ice->state.stage_dirty |= CROCUS_STAGE_DIRTY_UNCOMPILED_VS << stage;
   }

   if (ish->const_data) {
      pipe_resource_reference(&ish->const_data, NULL);
      pipe_resource_reference(&ish->const_data_state.res, NULL);
   }

   ralloc_free(ish->nir);
   free(ish);
}

static void
crocus_delete_vs_state(struct pipe_context *ctx, void *state)
{
   crocus_delete_shader_state(ctx, state, MESA_SHADER_VERTEX);
}

static void
crocus_delete_tcs_state(struct pipe_context *ctx, void *state)
{
   crocus_delete_shader_state(ctx, state, MESA_SHADER_TESS_CTRL);
}

static void
crocus_delete_tes_state(struct pipe_context *ctx, void *state)
{
   crocus_delete_shader_state(ctx, state, MESA_SHADER_TESS_EVAL);
}

static void
crocus_delete_gs_state(struct pipe_context *ctx, void *state)
{
   crocus_delete_shader_state(ctx, state, MESA_SHADER_GEOMETRY);
}

static void
crocus_delete_fs_state(struct pipe_context *ctx, void *state)
{
   crocus_delete_shader_state(ctx, state, MESA_SHADER_FRAGMENT);
}

static void
crocus_delete_cs_state(struct pipe_context *ctx, void *state)
{
   crocus_delete_shader_state(ctx, state, MESA_SHADER_COMPUTE);
}

/**
 * The pipe->bind_[stage]_state() driver hook.
 *
 * Binds an uncompiled shader as the current one for a particular stage.
 * Updates dirty tracking to account for the shader's NOS.
 */
static void
bind_shader_state(struct crocus_context *ice,
                  struct crocus_uncompiled_shader *ish,
                  gl_shader_stage stage)
{
   uint64_t dirty_bit = CROCUS_STAGE_DIRTY_UNCOMPILED_VS << stage;
   const uint64_t nos = ish ? ish->nos : 0;

   const struct shader_info *old_info = crocus_get_shader_info(ice, stage);
   const struct shader_info *new_info = ish ? &ish->nir->info : NULL;

   if ((old_info ? BITSET_LAST_BIT(old_info->textures_used) : 0) !=
       (new_info ? BITSET_LAST_BIT(new_info->textures_used) : 0)) {
      ice->state.stage_dirty |= CROCUS_STAGE_DIRTY_SAMPLER_STATES_VS << stage;
   }

   ice->shaders.uncompiled[stage] = ish;
   ice->state.stage_dirty |= dirty_bit;

   /* Record that CSOs need to mark CROCUS_DIRTY_UNCOMPILED_XS when they change
    * (or that they no longer need to do so).
    */
   for (int i = 0; i < CROCUS_NOS_COUNT; i++) {
      if (nos & (1 << i))
         ice->state.stage_dirty_for_nos[i] |= dirty_bit;
      else
         ice->state.stage_dirty_for_nos[i] &= ~dirty_bit;
   }
}

static void
crocus_bind_vs_state(struct pipe_context *ctx, void *state)
{
   struct crocus_context *ice = (struct crocus_context *)ctx;
   struct crocus_uncompiled_shader *new_ish = state;
   struct crocus_screen *screen = (struct crocus_screen *)ice->ctx.screen;
   const struct intel_device_info *devinfo = &screen->devinfo;

   if (new_ish &&
       ice->state.window_space_position !=
       new_ish->nir->info.vs.window_space_position) {
      ice->state.window_space_position =
         new_ish->nir->info.vs.window_space_position;

      ice->state.dirty |= CROCUS_DIRTY_CLIP |
                          CROCUS_DIRTY_RASTER |
                          CROCUS_DIRTY_CC_VIEWPORT;
   }

   if (devinfo->ver == 6) {
      ice->state.stage_dirty |= CROCUS_DIRTY_GEN4_FF_GS_PROG;
   }

   bind_shader_state((void *) ctx, state, MESA_SHADER_VERTEX);
}

static void
crocus_bind_tcs_state(struct pipe_context *ctx, void *state)
{
   bind_shader_state((void *) ctx, state, MESA_SHADER_TESS_CTRL);
}

static void
crocus_bind_tes_state(struct pipe_context *ctx, void *state)
{
   struct crocus_context *ice = (struct crocus_context *)ctx;

   /* Enabling/disabling optional stages requires a URB reconfiguration. */
   if (!!state != !!ice->shaders.uncompiled[MESA_SHADER_TESS_EVAL])
      ice->state.dirty |= CROCUS_DIRTY_GEN6_URB;

   bind_shader_state((void *) ctx, state, MESA_SHADER_TESS_EVAL);
}

static void
crocus_bind_gs_state(struct pipe_context *ctx, void *state)
{
   struct crocus_context *ice = (struct crocus_context *)ctx;

   /* Enabling/disabling optional stages requires a URB reconfiguration. */
   if (!!state != !!ice->shaders.uncompiled[MESA_SHADER_GEOMETRY])
      ice->state.dirty |= CROCUS_DIRTY_GEN6_URB;

   bind_shader_state((void *) ctx, state, MESA_SHADER_GEOMETRY);
}

static void
crocus_bind_fs_state(struct pipe_context *ctx, void *state)
{
   struct crocus_context *ice = (struct crocus_context *) ctx;
   struct crocus_uncompiled_shader *old_ish =
      ice->shaders.uncompiled[MESA_SHADER_FRAGMENT];
   struct crocus_uncompiled_shader *new_ish = state;

   const unsigned color_bits =
      BITFIELD64_BIT(FRAG_RESULT_COLOR) |
      BITFIELD64_RANGE(FRAG_RESULT_DATA0, BRW_MAX_DRAW_BUFFERS);

   /* Fragment shader outputs influence HasWriteableRT */
   if (!old_ish || !new_ish ||
       (old_ish->nir->info.outputs_written & color_bits) !=
       (new_ish->nir->info.outputs_written & color_bits))
      ice->state.dirty |= CROCUS_DIRTY_WM;

   bind_shader_state((void *) ctx, state, MESA_SHADER_FRAGMENT);
}

static void
crocus_bind_cs_state(struct pipe_context *ctx, void *state)
{
   bind_shader_state((void *) ctx, state, MESA_SHADER_COMPUTE);
}

void
crocus_init_program_functions(struct pipe_context *ctx)
{
   ctx->create_vs_state  = crocus_create_vs_state;
   ctx->create_tcs_state = crocus_create_tcs_state;
   ctx->create_tes_state = crocus_create_tes_state;
   ctx->create_gs_state  = crocus_create_gs_state;
   ctx->create_fs_state  = crocus_create_fs_state;
   ctx->create_compute_state = crocus_create_compute_state;

   ctx->delete_vs_state  = crocus_delete_vs_state;
   ctx->delete_tcs_state = crocus_delete_tcs_state;
   ctx->delete_tes_state = crocus_delete_tes_state;
   ctx->delete_gs_state  = crocus_delete_gs_state;
   ctx->delete_fs_state  = crocus_delete_fs_state;
   ctx->delete_compute_state = crocus_delete_cs_state;

   ctx->bind_vs_state  = crocus_bind_vs_state;
   ctx->bind_tcs_state = crocus_bind_tcs_state;
   ctx->bind_tes_state = crocus_bind_tes_state;
   ctx->bind_gs_state  = crocus_bind_gs_state;
   ctx->bind_fs_state  = crocus_bind_fs_state;
   ctx->bind_compute_state = crocus_bind_cs_state;
}