aboutsummaryrefslogtreecommitdiffstats
path: root/src/gallium/drivers/ilo/ilo_state.c
blob: e05d49f7bf453f17a344ff16f1c787aa3300ea19 (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
/*
 * Mesa 3-D graphics library
 *
 * Copyright (C) 2012-2013 LunarG, Inc.
 *
 * 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.
 *
 * Authors:
 *    Chia-I Wu <olv@lunarg.com>
 */

#include "util/u_dual_blend.h"
#include "util/u_dynarray.h"
#include "util/u_framebuffer.h"
#include "util/u_helpers.h"
#include "util/u_resource.h"
#include "util/u_upload_mgr.h"

#include "ilo_context.h"
#include "ilo_format.h"
#include "ilo_resource.h"
#include "ilo_shader.h"
#include "ilo_state.h"

/**
 * Translate a pipe primitive type to the matching hardware primitive type.
 */
static enum gen_3dprim_type
ilo_translate_draw_mode(unsigned mode)
{
   static const enum gen_3dprim_type prim_mapping[PIPE_PRIM_MAX] = {
      [PIPE_PRIM_POINTS]                     = GEN6_3DPRIM_POINTLIST,
      [PIPE_PRIM_LINES]                      = GEN6_3DPRIM_LINELIST,
      [PIPE_PRIM_LINE_LOOP]                  = GEN6_3DPRIM_LINELOOP,
      [PIPE_PRIM_LINE_STRIP]                 = GEN6_3DPRIM_LINESTRIP,
      [PIPE_PRIM_TRIANGLES]                  = GEN6_3DPRIM_TRILIST,
      [PIPE_PRIM_TRIANGLE_STRIP]             = GEN6_3DPRIM_TRISTRIP,
      [PIPE_PRIM_TRIANGLE_FAN]               = GEN6_3DPRIM_TRIFAN,
      [PIPE_PRIM_QUADS]                      = GEN6_3DPRIM_QUADLIST,
      [PIPE_PRIM_QUAD_STRIP]                 = GEN6_3DPRIM_QUADSTRIP,
      [PIPE_PRIM_POLYGON]                    = GEN6_3DPRIM_POLYGON,
      [PIPE_PRIM_LINES_ADJACENCY]            = GEN6_3DPRIM_LINELIST_ADJ,
      [PIPE_PRIM_LINE_STRIP_ADJACENCY]       = GEN6_3DPRIM_LINESTRIP_ADJ,
      [PIPE_PRIM_TRIANGLES_ADJACENCY]        = GEN6_3DPRIM_TRILIST_ADJ,
      [PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY]   = GEN6_3DPRIM_TRISTRIP_ADJ,
   };

   assert(prim_mapping[mode]);

   return prim_mapping[mode];
}

static enum gen_index_format
ilo_translate_index_size(unsigned index_size)
{
   switch (index_size) {
   case 1:                             return GEN6_INDEX_BYTE;
   case 2:                             return GEN6_INDEX_WORD;
   case 4:                             return GEN6_INDEX_DWORD;
   default:
      assert(!"unknown index size");
      return GEN6_INDEX_BYTE;
   }
}

static enum gen_mip_filter
ilo_translate_mip_filter(unsigned filter)
{
   switch (filter) {
   case PIPE_TEX_MIPFILTER_NEAREST:    return GEN6_MIPFILTER_NEAREST;
   case PIPE_TEX_MIPFILTER_LINEAR:     return GEN6_MIPFILTER_LINEAR;
   case PIPE_TEX_MIPFILTER_NONE:       return GEN6_MIPFILTER_NONE;
   default:
      assert(!"unknown mipfilter");
      return GEN6_MIPFILTER_NONE;
   }
}

static int
ilo_translate_img_filter(unsigned filter)
{
   switch (filter) {
   case PIPE_TEX_FILTER_NEAREST:       return GEN6_MAPFILTER_NEAREST;
   case PIPE_TEX_FILTER_LINEAR:        return GEN6_MAPFILTER_LINEAR;
   default:
      assert(!"unknown sampler filter");
      return GEN6_MAPFILTER_NEAREST;
   }
}

static enum gen_texcoord_mode
ilo_translate_address_wrap(unsigned wrap)
{
   switch (wrap) {
   case PIPE_TEX_WRAP_CLAMP:           return GEN8_TEXCOORDMODE_HALF_BORDER;
   case PIPE_TEX_WRAP_REPEAT:          return GEN6_TEXCOORDMODE_WRAP;
   case PIPE_TEX_WRAP_CLAMP_TO_EDGE:   return GEN6_TEXCOORDMODE_CLAMP;
   case PIPE_TEX_WRAP_CLAMP_TO_BORDER: return GEN6_TEXCOORDMODE_CLAMP_BORDER;
   case PIPE_TEX_WRAP_MIRROR_REPEAT:   return GEN6_TEXCOORDMODE_MIRROR;
   case PIPE_TEX_WRAP_MIRROR_CLAMP:
   case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
   case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
   default:
      assert(!"unknown sampler wrap mode");
      return GEN6_TEXCOORDMODE_WRAP;
   }
}

static enum gen_aniso_ratio
ilo_translate_max_anisotropy(unsigned max_anisotropy)
{
   switch (max_anisotropy) {
   case 0: case 1: case 2:             return GEN6_ANISORATIO_2;
   case 3: case 4:                     return GEN6_ANISORATIO_4;
   case 5: case 6:                     return GEN6_ANISORATIO_6;
   case 7: case 8:                     return GEN6_ANISORATIO_8;
   case 9: case 10:                    return GEN6_ANISORATIO_10;
   case 11: case 12:                   return GEN6_ANISORATIO_12;
   case 13: case 14:                   return GEN6_ANISORATIO_14;
   default:                            return GEN6_ANISORATIO_16;
   }
}

static enum gen_prefilter_op
ilo_translate_shadow_func(unsigned func)
{
   /*
    * For PIPE_FUNC_x, the reference value is on the left-hand side of the
    * comparison, and 1.0 is returned when the comparison is true.
    *
    * For GEN6_PREFILTEROP_x, the reference value is on the right-hand side of
    * the comparison, and 0.0 is returned when the comparison is true.
    */
   switch (func) {
   case PIPE_FUNC_NEVER:               return GEN6_PREFILTEROP_ALWAYS;
   case PIPE_FUNC_LESS:                return GEN6_PREFILTEROP_LEQUAL;
   case PIPE_FUNC_EQUAL:               return GEN6_PREFILTEROP_NOTEQUAL;
   case PIPE_FUNC_LEQUAL:              return GEN6_PREFILTEROP_LESS;
   case PIPE_FUNC_GREATER:             return GEN6_PREFILTEROP_GEQUAL;
   case PIPE_FUNC_NOTEQUAL:            return GEN6_PREFILTEROP_EQUAL;
   case PIPE_FUNC_GEQUAL:              return GEN6_PREFILTEROP_GREATER;
   case PIPE_FUNC_ALWAYS:              return GEN6_PREFILTEROP_NEVER;
   default:
      assert(!"unknown shadow compare function");
      return GEN6_PREFILTEROP_NEVER;
   }
}

static enum gen_front_winding
ilo_translate_front_ccw(unsigned front_ccw)
{
   return (front_ccw) ? GEN6_FRONTWINDING_CCW : GEN6_FRONTWINDING_CW;
}

static enum gen_cull_mode
ilo_translate_cull_face(unsigned cull_face)
{
   switch (cull_face) {
   case PIPE_FACE_NONE:                return GEN6_CULLMODE_NONE;
   case PIPE_FACE_FRONT:               return GEN6_CULLMODE_FRONT;
   case PIPE_FACE_BACK:                return GEN6_CULLMODE_BACK;
   case PIPE_FACE_FRONT_AND_BACK:      return GEN6_CULLMODE_BOTH;
   default:
      assert(!"unknown face culling");
      return GEN6_CULLMODE_NONE;
   }
}

static enum gen_fill_mode
ilo_translate_poly_mode(unsigned poly_mode)
{
   switch (poly_mode) {
   case PIPE_POLYGON_MODE_FILL:        return GEN6_FILLMODE_SOLID;
   case PIPE_POLYGON_MODE_LINE:        return GEN6_FILLMODE_WIREFRAME;
   case PIPE_POLYGON_MODE_POINT:       return GEN6_FILLMODE_POINT;
   default:
      assert(!"unknown polygon mode");
      return GEN6_FILLMODE_SOLID;
   }
}

static enum gen_pixel_location
ilo_translate_half_pixel_center(bool half_pixel_center)
{
   return (half_pixel_center) ? GEN6_PIXLOC_CENTER : GEN6_PIXLOC_UL_CORNER;
}

static enum gen_compare_function
ilo_translate_compare_func(unsigned func)
{
   switch (func) {
   case PIPE_FUNC_NEVER:               return GEN6_COMPAREFUNCTION_NEVER;
   case PIPE_FUNC_LESS:                return GEN6_COMPAREFUNCTION_LESS;
   case PIPE_FUNC_EQUAL:               return GEN6_COMPAREFUNCTION_EQUAL;
   case PIPE_FUNC_LEQUAL:              return GEN6_COMPAREFUNCTION_LEQUAL;
   case PIPE_FUNC_GREATER:             return GEN6_COMPAREFUNCTION_GREATER;
   case PIPE_FUNC_NOTEQUAL:            return GEN6_COMPAREFUNCTION_NOTEQUAL;
   case PIPE_FUNC_GEQUAL:              return GEN6_COMPAREFUNCTION_GEQUAL;
   case PIPE_FUNC_ALWAYS:              return GEN6_COMPAREFUNCTION_ALWAYS;
   default:
      assert(!"unknown compare function");
      return GEN6_COMPAREFUNCTION_NEVER;
   }
}

static enum gen_stencil_op
ilo_translate_stencil_op(unsigned stencil_op)
{
   switch (stencil_op) {
   case PIPE_STENCIL_OP_KEEP:          return GEN6_STENCILOP_KEEP;
   case PIPE_STENCIL_OP_ZERO:          return GEN6_STENCILOP_ZERO;
   case PIPE_STENCIL_OP_REPLACE:       return GEN6_STENCILOP_REPLACE;
   case PIPE_STENCIL_OP_INCR:          return GEN6_STENCILOP_INCRSAT;
   case PIPE_STENCIL_OP_DECR:          return GEN6_STENCILOP_DECRSAT;
   case PIPE_STENCIL_OP_INCR_WRAP:     return GEN6_STENCILOP_INCR;
   case PIPE_STENCIL_OP_DECR_WRAP:     return GEN6_STENCILOP_DECR;
   case PIPE_STENCIL_OP_INVERT:        return GEN6_STENCILOP_INVERT;
   default:
      assert(!"unknown stencil op");
      return GEN6_STENCILOP_KEEP;
   }
}

static enum gen_logic_op
ilo_translate_logicop(unsigned logicop)
{
   switch (logicop) {
   case PIPE_LOGICOP_CLEAR:            return GEN6_LOGICOP_CLEAR;
   case PIPE_LOGICOP_NOR:              return GEN6_LOGICOP_NOR;
   case PIPE_LOGICOP_AND_INVERTED:     return GEN6_LOGICOP_AND_INVERTED;
   case PIPE_LOGICOP_COPY_INVERTED:    return GEN6_LOGICOP_COPY_INVERTED;
   case PIPE_LOGICOP_AND_REVERSE:      return GEN6_LOGICOP_AND_REVERSE;
   case PIPE_LOGICOP_INVERT:           return GEN6_LOGICOP_INVERT;
   case PIPE_LOGICOP_XOR:              return GEN6_LOGICOP_XOR;
   case PIPE_LOGICOP_NAND:             return GEN6_LOGICOP_NAND;
   case PIPE_LOGICOP_AND:              return GEN6_LOGICOP_AND;
   case PIPE_LOGICOP_EQUIV:            return GEN6_LOGICOP_EQUIV;
   case PIPE_LOGICOP_NOOP:             return GEN6_LOGICOP_NOOP;
   case PIPE_LOGICOP_OR_INVERTED:      return GEN6_LOGICOP_OR_INVERTED;
   case PIPE_LOGICOP_COPY:             return GEN6_LOGICOP_COPY;
   case PIPE_LOGICOP_OR_REVERSE:       return GEN6_LOGICOP_OR_REVERSE;
   case PIPE_LOGICOP_OR:               return GEN6_LOGICOP_OR;
   case PIPE_LOGICOP_SET:              return GEN6_LOGICOP_SET;
   default:
      assert(!"unknown logicop function");
      return GEN6_LOGICOP_CLEAR;
   }
}

static int
ilo_translate_blend_func(unsigned blend)
{
   switch (blend) {
   case PIPE_BLEND_ADD:                return GEN6_BLENDFUNCTION_ADD;
   case PIPE_BLEND_SUBTRACT:           return GEN6_BLENDFUNCTION_SUBTRACT;
   case PIPE_BLEND_REVERSE_SUBTRACT:   return GEN6_BLENDFUNCTION_REVERSE_SUBTRACT;
   case PIPE_BLEND_MIN:                return GEN6_BLENDFUNCTION_MIN;
   case PIPE_BLEND_MAX:                return GEN6_BLENDFUNCTION_MAX;
   default:
      assert(!"unknown blend function");
      return GEN6_BLENDFUNCTION_ADD;
   }
}

static int
ilo_translate_blend_factor(unsigned factor)
{
   switch (factor) {
   case PIPE_BLENDFACTOR_ONE:                return GEN6_BLENDFACTOR_ONE;
   case PIPE_BLENDFACTOR_SRC_COLOR:          return GEN6_BLENDFACTOR_SRC_COLOR;
   case PIPE_BLENDFACTOR_SRC_ALPHA:          return GEN6_BLENDFACTOR_SRC_ALPHA;
   case PIPE_BLENDFACTOR_DST_ALPHA:          return GEN6_BLENDFACTOR_DST_ALPHA;
   case PIPE_BLENDFACTOR_DST_COLOR:          return GEN6_BLENDFACTOR_DST_COLOR;
   case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE: return GEN6_BLENDFACTOR_SRC_ALPHA_SATURATE;
   case PIPE_BLENDFACTOR_CONST_COLOR:        return GEN6_BLENDFACTOR_CONST_COLOR;
   case PIPE_BLENDFACTOR_CONST_ALPHA:        return GEN6_BLENDFACTOR_CONST_ALPHA;
   case PIPE_BLENDFACTOR_SRC1_COLOR:         return GEN6_BLENDFACTOR_SRC1_COLOR;
   case PIPE_BLENDFACTOR_SRC1_ALPHA:         return GEN6_BLENDFACTOR_SRC1_ALPHA;
   case PIPE_BLENDFACTOR_ZERO:               return GEN6_BLENDFACTOR_ZERO;
   case PIPE_BLENDFACTOR_INV_SRC_COLOR:      return GEN6_BLENDFACTOR_INV_SRC_COLOR;
   case PIPE_BLENDFACTOR_INV_SRC_ALPHA:      return GEN6_BLENDFACTOR_INV_SRC_ALPHA;
   case PIPE_BLENDFACTOR_INV_DST_ALPHA:      return GEN6_BLENDFACTOR_INV_DST_ALPHA;
   case PIPE_BLENDFACTOR_INV_DST_COLOR:      return GEN6_BLENDFACTOR_INV_DST_COLOR;
   case PIPE_BLENDFACTOR_INV_CONST_COLOR:    return GEN6_BLENDFACTOR_INV_CONST_COLOR;
   case PIPE_BLENDFACTOR_INV_CONST_ALPHA:    return GEN6_BLENDFACTOR_INV_CONST_ALPHA;
   case PIPE_BLENDFACTOR_INV_SRC1_COLOR:     return GEN6_BLENDFACTOR_INV_SRC1_COLOR;
   case PIPE_BLENDFACTOR_INV_SRC1_ALPHA:     return GEN6_BLENDFACTOR_INV_SRC1_ALPHA;
   default:
      assert(!"unknown blend factor");
      return GEN6_BLENDFACTOR_ONE;
   }
}

static void
finalize_shader_states(struct ilo_state_vector *vec)
{
   unsigned type;

   for (type = 0; type < PIPE_SHADER_TYPES; type++) {
      struct ilo_shader_state *shader;
      uint32_t state;

      switch (type) {
      case PIPE_SHADER_VERTEX:
         shader = vec->vs;
         state = ILO_DIRTY_VS;
         break;
      case PIPE_SHADER_GEOMETRY:
         shader = vec->gs;
         state = ILO_DIRTY_GS;
         break;
      case PIPE_SHADER_FRAGMENT:
         shader = vec->fs;
         state = ILO_DIRTY_FS;
         break;
      default:
         shader = NULL;
         state = 0;
         break;
      }

      if (!shader)
         continue;

      /* compile if the shader or the states it depends on changed */
      if (vec->dirty & state) {
         ilo_shader_select_kernel(shader, vec, ILO_DIRTY_ALL);
      }
      else if (ilo_shader_select_kernel(shader, vec, vec->dirty)) {
         /* mark the state dirty if a new kernel is selected */
         vec->dirty |= state;
      }

      /* need to setup SBE for FS */
      if (type == PIPE_SHADER_FRAGMENT && vec->dirty &
            (state | ILO_DIRTY_GS | ILO_DIRTY_VS | ILO_DIRTY_RASTERIZER)) {
         if (ilo_shader_select_kernel_sbe(shader,
               (vec->gs) ? vec->gs : vec->vs, vec->rasterizer))
            vec->dirty |= state;
      }
   }
}

static void
finalize_cbuf_state(struct ilo_context *ilo,
                    struct ilo_cbuf_state *cbuf,
                    const struct ilo_shader_state *sh)
{
   uint32_t upload_mask = cbuf->enabled_mask;

   /* skip CBUF0 if the kernel does not need it */
   upload_mask &=
      ~ilo_shader_get_kernel_param(sh, ILO_KERNEL_SKIP_CBUF0_UPLOAD);

   while (upload_mask) {
      unsigned offset, i;

      i = u_bit_scan(&upload_mask);
      /* no need to upload */
      if (cbuf->cso[i].resource)
         continue;

      u_upload_data(ilo->uploader, 0, cbuf->cso[i].info.size, 16,
            cbuf->cso[i].user_buffer, &offset, &cbuf->cso[i].resource);

      cbuf->cso[i].info.vma = ilo_resource_get_vma(cbuf->cso[i].resource);
      cbuf->cso[i].info.offset = offset;

      memset(&cbuf->cso[i].surface, 0, sizeof(cbuf->cso[i].surface));
      ilo_state_surface_init_for_buffer(&cbuf->cso[i].surface,
            ilo->dev, &cbuf->cso[i].info);

      ilo->state_vector.dirty |= ILO_DIRTY_CBUF;
   }
}

static void
finalize_constant_buffers(struct ilo_context *ilo)
{
   struct ilo_state_vector *vec = &ilo->state_vector;

   if (vec->dirty & (ILO_DIRTY_CBUF | ILO_DIRTY_VS))
      finalize_cbuf_state(ilo, &vec->cbuf[PIPE_SHADER_VERTEX], vec->vs);

   if (ilo->state_vector.dirty & (ILO_DIRTY_CBUF | ILO_DIRTY_FS))
      finalize_cbuf_state(ilo, &vec->cbuf[PIPE_SHADER_FRAGMENT], vec->fs);
}

static void
finalize_index_buffer(struct ilo_context *ilo)
{
   const struct ilo_dev *dev = ilo->dev;
   struct ilo_state_vector *vec = &ilo->state_vector;
   const bool need_upload = (vec->draw->indexed &&
         (vec->ib.state.user_buffer ||
          vec->ib.state.offset % vec->ib.state.index_size));
   struct pipe_resource *current_hw_res = NULL;
   struct ilo_state_index_buffer_info info;
   int64_t vertex_start_bias = 0;

   if (!(vec->dirty & ILO_DIRTY_IB) && !need_upload)
      return;

   /* make sure vec->ib.hw_resource changes when reallocated */
   pipe_resource_reference(&current_hw_res, vec->ib.hw_resource);

   if (need_upload) {
      const unsigned offset = vec->ib.state.index_size * vec->draw->start;
      const unsigned size = vec->ib.state.index_size * vec->draw->count;
      unsigned hw_offset;

      if (vec->ib.state.user_buffer) {
         u_upload_data(ilo->uploader, 0, size, 16,
               vec->ib.state.user_buffer + offset,
               &hw_offset, &vec->ib.hw_resource);
      } else {
         u_upload_buffer(ilo->uploader, 0,
               vec->ib.state.offset + offset, size, 16, vec->ib.state.buffer,
               &hw_offset, &vec->ib.hw_resource);
      }

      /* the HW offset should be aligned */
      assert(hw_offset % vec->ib.state.index_size == 0);
      vertex_start_bias = hw_offset / vec->ib.state.index_size;

      /*
       * INDEX[vec->draw->start] in the original buffer is INDEX[0] in the HW
       * resource
       */
      vertex_start_bias -= vec->draw->start;
   } else {
      pipe_resource_reference(&vec->ib.hw_resource, vec->ib.state.buffer);

      /* note that index size may be zero when the draw is not indexed */
      if (vec->draw->indexed)
         vertex_start_bias = vec->ib.state.offset / vec->ib.state.index_size;
   }

   vec->draw_info.vertex_start += vertex_start_bias;

   /* treat the IB as clean if the HW states do not change */
   if (vec->ib.hw_resource == current_hw_res &&
       vec->ib.hw_index_size == vec->ib.state.index_size)
      vec->dirty &= ~ILO_DIRTY_IB;
   else
      vec->ib.hw_index_size = vec->ib.state.index_size;

   pipe_resource_reference(&current_hw_res, NULL);

   memset(&info, 0, sizeof(info));
   if (vec->ib.hw_resource) {
      info.vma = ilo_resource_get_vma(vec->ib.hw_resource);
      info.size = info.vma->vm_size;
      info.format = ilo_translate_index_size(vec->ib.hw_index_size);
   }

   ilo_state_index_buffer_set_info(&vec->ib.ib, dev, &info);
}

static void
finalize_vertex_elements(struct ilo_context *ilo)
{
   const struct ilo_dev *dev = ilo->dev;
   struct ilo_state_vector *vec = &ilo->state_vector;
   struct ilo_ve_state *ve = vec->ve;
   const bool last_element_edge_flag = (vec->vs &&
         ilo_shader_get_kernel_param(vec->vs, ILO_KERNEL_VS_INPUT_EDGEFLAG));
   const bool prepend_vertexid = (vec->vs &&
         ilo_shader_get_kernel_param(vec->vs, ILO_KERNEL_VS_INPUT_VERTEXID));
   const bool prepend_instanceid = (vec->vs &&
         ilo_shader_get_kernel_param(vec->vs,
            ILO_KERNEL_VS_INPUT_INSTANCEID));
   const enum gen_index_format index_format = (vec->draw->indexed) ?
      ilo_translate_index_size(vec->ib.state.index_size) : GEN6_INDEX_DWORD;

   /* check for non-orthogonal states */
   if (ve->vf_params.cv_topology != vec->draw_info.topology ||
       ve->vf_params.prepend_vertexid != prepend_vertexid ||
       ve->vf_params.prepend_instanceid != prepend_instanceid ||
       ve->vf_params.last_element_edge_flag != last_element_edge_flag ||
       ve->vf_params.cv_index_format != index_format ||
       ve->vf_params.cut_index_enable != vec->draw->primitive_restart ||
       ve->vf_params.cut_index != vec->draw->restart_index) {
      ve->vf_params.cv_topology = vec->draw_info.topology;
      ve->vf_params.prepend_vertexid = prepend_vertexid;
      ve->vf_params.prepend_instanceid = prepend_instanceid;
      ve->vf_params.last_element_edge_flag = last_element_edge_flag;
      ve->vf_params.cv_index_format = index_format;
      ve->vf_params.cut_index_enable = vec->draw->primitive_restart;
      ve->vf_params.cut_index = vec->draw->restart_index;

      ilo_state_vf_set_params(&ve->vf, dev, &ve->vf_params);

      vec->dirty |= ILO_DIRTY_VE;
   }
}

static void
finalize_vertex_buffers(struct ilo_context *ilo)
{
   const struct ilo_dev *dev = ilo->dev;
   struct ilo_state_vector *vec = &ilo->state_vector;
   struct ilo_state_vertex_buffer_info info;
   unsigned i;

   if (!(vec->dirty & (ILO_DIRTY_VE | ILO_DIRTY_VB)))
      return;

   memset(&info, 0, sizeof(info));

   for (i = 0; i < vec->ve->vb_count; i++) {
      const unsigned pipe_idx = vec->ve->vb_mapping[i];
      const struct pipe_vertex_buffer *cso = &vec->vb.states[pipe_idx];

      if (cso->buffer) {
         info.vma = ilo_resource_get_vma(cso->buffer);
         info.offset = cso->buffer_offset;
         info.size = info.vma->vm_size - cso->buffer_offset;

         info.stride = cso->stride;
      } else {
         memset(&info, 0, sizeof(info));
      }

      ilo_state_vertex_buffer_set_info(&vec->vb.vb[i], dev, &info);
   }
}

static void
finalize_urb(struct ilo_context *ilo)
{
   const uint16_t attr_size = sizeof(uint32_t) * 4;
   const struct ilo_dev *dev = ilo->dev;
   struct ilo_state_vector *vec = &ilo->state_vector;
   struct ilo_state_urb_info info;

   if (!(vec->dirty & (ILO_DIRTY_VE | ILO_DIRTY_VS |
                       ILO_DIRTY_GS | ILO_DIRTY_FS)))
      return;

   memset(&info, 0, sizeof(info));

   info.ve_entry_size = attr_size * ilo_state_vf_get_attr_count(&vec->ve->vf);

   if (vec->vs) {
      info.vs_const_data = (bool)
         (ilo_shader_get_kernel_param(vec->vs, ILO_KERNEL_PCB_CBUF0_SIZE) +
          ilo_shader_get_kernel_param(vec->vs, ILO_KERNEL_VS_PCB_UCP_SIZE));
      info.vs_entry_size = attr_size *
         ilo_shader_get_kernel_param(vec->vs, ILO_KERNEL_OUTPUT_COUNT);
   }

   if (vec->gs) {
      info.gs_const_data = (bool)
         ilo_shader_get_kernel_param(vec->gs, ILO_KERNEL_PCB_CBUF0_SIZE);

      /*
       * From the Ivy Bridge PRM, volume 2 part 1, page 189:
       *
       *     "All outputs of a GS thread will be stored in the single GS
       *      thread output URB entry."
       *
       * TODO
       */
      info.gs_entry_size = attr_size *
         ilo_shader_get_kernel_param(vec->gs, ILO_KERNEL_OUTPUT_COUNT);
   }

   if (vec->fs) {
      info.ps_const_data = (bool)
         ilo_shader_get_kernel_param(vec->fs, ILO_KERNEL_PCB_CBUF0_SIZE);
   }

   ilo_state_urb_set_info(&vec->urb, dev, &info);
}

static void
finalize_viewport(struct ilo_context *ilo)
{
   const struct ilo_dev *dev = ilo->dev;
   struct ilo_state_vector *vec = &ilo->state_vector;

   if (vec->dirty & ILO_DIRTY_VIEWPORT) {
      ilo_state_viewport_set_params(&vec->viewport.vp,
            dev, &vec->viewport.params, false);
   } else if (vec->dirty & ILO_DIRTY_SCISSOR) {
      ilo_state_viewport_set_params(&vec->viewport.vp,
            dev, &vec->viewport.params, true);
      vec->dirty |= ILO_DIRTY_VIEWPORT;
   }
}

static bool
can_enable_gb_test(const struct ilo_rasterizer_state *rasterizer,
                   const struct ilo_viewport_state *viewport,
                   const struct ilo_fb_state *fb)
{
   unsigned i;

   /*
    * There are several reasons that guard band test should be disabled
    *
    *  - GL wide points (to avoid partially visibie object)
    *  - GL wide or AA lines (to avoid partially visibie object)
    *  - missing 2D clipping
    */
   if (rasterizer->state.point_size_per_vertex ||
       rasterizer->state.point_size > 1.0f ||
       rasterizer->state.line_width > 1.0f ||
       rasterizer->state.line_smooth)
      return false;

   for (i = 0; i < viewport->params.count; i++) {
      const struct ilo_state_viewport_matrix_info *mat =
         &viewport->matrices[i];
      float min_x, max_x, min_y, max_y;

      min_x = -1.0f * fabsf(mat->scale[0]) + mat->translate[0];
      max_x =  1.0f * fabsf(mat->scale[0]) + mat->translate[0];
      min_y = -1.0f * fabsf(mat->scale[1]) + mat->translate[1];
      max_y =  1.0f * fabsf(mat->scale[1]) + mat->translate[1];

      if (min_x > 0.0f || max_x < fb->state.width ||
          min_y > 0.0f || max_y < fb->state.height)
         return false;
   }

   return true;
}

static void
finalize_rasterizer(struct ilo_context *ilo)
{
   const struct ilo_dev *dev = ilo->dev;
   struct ilo_state_vector *vec = &ilo->state_vector;
   struct ilo_rasterizer_state *rasterizer = vec->rasterizer;
   struct ilo_state_raster_info *info = &vec->rasterizer->info;
   const bool gb_test_enable =
      can_enable_gb_test(rasterizer, &vec->viewport, &vec->fb);
   const bool multisample =
      (rasterizer->state.multisample && vec->fb.num_samples > 1);
   const uint8_t barycentric_interps = ilo_shader_get_kernel_param(vec->fs,
         ILO_KERNEL_FS_BARYCENTRIC_INTERPOLATIONS);

   /* check for non-orthogonal states */
   if (info->clip.viewport_count != vec->viewport.params.count ||
       info->clip.gb_test_enable != gb_test_enable ||
       info->setup.msaa_enable != multisample ||
       info->setup.line_msaa_enable != multisample ||
       info->tri.depth_offset_format != vec->fb.depth_offset_format ||
       info->scan.sample_count != vec->fb.num_samples ||
       info->scan.sample_mask != vec->sample_mask ||
       info->scan.barycentric_interps != barycentric_interps ||
       info->params.any_integer_rt != vec->fb.has_integer_rt ||
       info->params.hiz_enable != vec->fb.has_hiz) {
      info->clip.viewport_count = vec->viewport.params.count;
      info->clip.gb_test_enable = gb_test_enable;
      info->setup.msaa_enable = multisample;
      info->setup.line_msaa_enable = multisample;
      info->tri.depth_offset_format = vec->fb.depth_offset_format;
      info->scan.sample_count = vec->fb.num_samples;
      info->scan.sample_mask = vec->sample_mask;
      info->scan.barycentric_interps = barycentric_interps;
      info->params.any_integer_rt = vec->fb.has_integer_rt;
      info->params.hiz_enable = vec->fb.has_hiz;

      ilo_state_raster_set_info(&rasterizer->rs, dev, &rasterizer->info);

      vec->dirty |= ILO_DIRTY_RASTERIZER;
   }
}

static bool
finalize_blend_rt(struct ilo_context *ilo)
{
   struct ilo_state_vector *vec = &ilo->state_vector;
   const struct ilo_fb_state *fb = &vec->fb;
   struct ilo_blend_state *blend = vec->blend;
   struct ilo_state_cc_blend_info *info = &vec->blend->info.blend;
   bool changed = false;
   unsigned i;

   if (!(vec->dirty & (ILO_DIRTY_FB | ILO_DIRTY_BLEND)))
      return false;

   /* set up one for dummy RT writes */
   if (!fb->state.nr_cbufs) {
      if (info->rt != &blend->dummy_rt) {
         info->rt = &blend->dummy_rt;
         info->rt_count = 1;
         changed = true;
      }

      return changed;
   }

   if (info->rt != blend->effective_rt ||
       info->rt_count != fb->state.nr_cbufs) {
      info->rt = blend->effective_rt;
      info->rt_count = fb->state.nr_cbufs;
      changed = true;
   }

   for (i = 0; i < fb->state.nr_cbufs; i++) {
      const struct ilo_fb_blend_caps *caps = &fb->blend_caps[i];
      struct ilo_state_cc_blend_rt_info *rt = &blend->effective_rt[i];
      /* ignore logicop when not UNORM */
      const bool logicop_enable =
         (blend->rt[i].logicop_enable && caps->is_unorm);

      if (rt->cv_is_unorm != caps->is_unorm ||
          rt->cv_is_integer != caps->is_integer ||
          rt->logicop_enable != logicop_enable ||
          rt->force_dst_alpha_one != caps->force_dst_alpha_one) {
         rt->cv_is_unorm = caps->is_unorm;
         rt->cv_is_integer = caps->is_integer;
         rt->logicop_enable = logicop_enable;
         rt->force_dst_alpha_one = caps->force_dst_alpha_one;

         changed = true;
      }
   }

   return changed;
}

static void
finalize_blend(struct ilo_context *ilo)
{
   const struct ilo_dev *dev = ilo->dev;
   struct ilo_state_vector *vec = &ilo->state_vector;
   struct ilo_blend_state *blend = vec->blend;
   struct ilo_state_cc_info *info = &blend->info;
   const bool sample_count_one = (vec->fb.num_samples <= 1);
   const bool float_source0_alpha =
      (!vec->fb.state.nr_cbufs || !vec->fb.state.cbufs[0] ||
       !util_format_is_pure_integer(vec->fb.state.cbufs[0]->format));

   /* check for non-orthogonal states */
   if (finalize_blend_rt(ilo) ||
       info->alpha.cv_sample_count_one != sample_count_one ||
       info->alpha.cv_float_source0_alpha != float_source0_alpha ||
       info->alpha.test_enable != vec->dsa->alpha_test ||
       info->alpha.test_func != vec->dsa->alpha_func ||
       memcmp(&info->stencil, &vec->dsa->stencil, sizeof(info->stencil)) ||
       memcmp(&info->depth, &vec->dsa->depth, sizeof(info->depth)) ||
       memcmp(&info->params, &vec->cc_params, sizeof(info->params))) {
      info->alpha.cv_sample_count_one = sample_count_one;
      info->alpha.cv_float_source0_alpha = float_source0_alpha;
      info->alpha.test_enable = vec->dsa->alpha_test;
      info->alpha.test_func = vec->dsa->alpha_func;
      info->stencil = vec->dsa->stencil;
      info->depth = vec->dsa->depth;
      info->params = vec->cc_params;

      ilo_state_cc_set_info(&blend->cc, dev, info);

      blend->alpha_may_kill = (info->alpha.alpha_to_coverage ||
                               info->alpha.test_enable);

      vec->dirty |= ILO_DIRTY_BLEND;
   }
}

/**
 * Finalize states.  Some states depend on other states and are
 * incomplete/invalid until finalized.
 */
void
ilo_finalize_3d_states(struct ilo_context *ilo,
                       const struct pipe_draw_info *draw)
{
   ilo->state_vector.draw = draw;

   ilo->state_vector.draw_info.topology = ilo_translate_draw_mode(draw->mode);
   ilo->state_vector.draw_info.indexed = draw->indexed;
   ilo->state_vector.draw_info.vertex_count = draw->count;
   ilo->state_vector.draw_info.vertex_start = draw->start;
   ilo->state_vector.draw_info.instance_count = draw->instance_count;
   ilo->state_vector.draw_info.instance_start = draw->start_instance;
   ilo->state_vector.draw_info.vertex_base = draw->index_bias;

   finalize_blend(ilo);
   finalize_shader_states(&ilo->state_vector);
   finalize_constant_buffers(ilo);
   finalize_index_buffer(ilo);
   finalize_vertex_elements(ilo);
   finalize_vertex_buffers(ilo);

   finalize_urb(ilo);
   finalize_rasterizer(ilo);
   finalize_viewport(ilo);

   u_upload_unmap(ilo->uploader);
}

static void
finalize_global_binding(struct ilo_state_vector *vec)
{
   struct ilo_shader_state *cs = vec->cs;
   int base, count, shift;
   int i;

   count = ilo_shader_get_kernel_param(cs,
         ILO_KERNEL_CS_SURFACE_GLOBAL_COUNT);
   if (!count)
      return;

   base = ilo_shader_get_kernel_param(cs, ILO_KERNEL_CS_SURFACE_GLOBAL_BASE);
   shift = 32 - util_last_bit(base + count - 1);

   if (count > vec->global_binding.count)
      count = vec->global_binding.count;

   for (i = 0; i < count; i++) {
      struct ilo_global_binding_cso *cso =
         util_dynarray_element(&vec->global_binding.bindings,
               struct ilo_global_binding_cso, i);
      const uint32_t offset = *cso->handle & ((1 << shift) - 1);

      *cso->handle = ((base + i) << shift) | offset;
   }
}

void
ilo_finalize_compute_states(struct ilo_context *ilo)
{
   finalize_global_binding(&ilo->state_vector);
}

static void *
ilo_create_blend_state(struct pipe_context *pipe,
                       const struct pipe_blend_state *state)
{
   const struct ilo_dev *dev = ilo_context(pipe)->dev;
   struct ilo_state_cc_info *info;
   struct ilo_blend_state *blend;
   int i;

   blend = CALLOC_STRUCT(ilo_blend_state);
   assert(blend);

   info = &blend->info;

   info->alpha.cv_float_source0_alpha = true;
   info->alpha.cv_sample_count_one = true;
   info->alpha.alpha_to_one = state->alpha_to_one;
   info->alpha.alpha_to_coverage = state->alpha_to_coverage;
   info->alpha.test_enable = false;
   info->alpha.test_func = GEN6_COMPAREFUNCTION_ALWAYS;

   info->stencil.cv_has_buffer = true;
   info->depth.cv_has_buffer= true;

   info->blend.rt = blend->effective_rt;
   info->blend.rt_count = 1;
   info->blend.dither_enable = state->dither;

   for (i = 0; i < ARRAY_SIZE(blend->rt); i++) {
      const struct pipe_rt_blend_state *rt = &state->rt[i];
      struct ilo_state_cc_blend_rt_info *rt_info = &blend->rt[i];

      rt_info->cv_has_buffer = true;
      rt_info->cv_is_unorm = true;
      rt_info->cv_is_integer = false;

      /* logic op takes precedence over blending */
      if (state->logicop_enable) {
         rt_info->logicop_enable = true;
         rt_info->logicop_func = ilo_translate_logicop(state->logicop_func);
      } else if (rt->blend_enable) {
         rt_info->blend_enable = true;

         rt_info->rgb_src = ilo_translate_blend_factor(rt->rgb_src_factor);
         rt_info->rgb_dst = ilo_translate_blend_factor(rt->rgb_dst_factor);
         rt_info->rgb_func = ilo_translate_blend_func(rt->rgb_func);

         rt_info->a_src = ilo_translate_blend_factor(rt->alpha_src_factor);
         rt_info->a_dst = ilo_translate_blend_factor(rt->alpha_dst_factor);
         rt_info->a_func = ilo_translate_blend_func(rt->alpha_func);
      }

      if (!(rt->colormask & PIPE_MASK_A))
         rt_info->argb_write_disables |= (1 << 3);
      if (!(rt->colormask & PIPE_MASK_R))
         rt_info->argb_write_disables |= (1 << 2);
      if (!(rt->colormask & PIPE_MASK_G))
         rt_info->argb_write_disables |= (1 << 1);
      if (!(rt->colormask & PIPE_MASK_B))
         rt_info->argb_write_disables |= (1 << 0);

      if (!state->independent_blend_enable) {
         for (i = 1; i < ARRAY_SIZE(blend->rt); i++)
            blend->rt[i] = *rt_info;
         break;
      }
   }

   memcpy(blend->effective_rt, blend->rt, sizeof(blend->rt));

   blend->dummy_rt.argb_write_disables = 0xf;

   if (!ilo_state_cc_init(&blend->cc, dev, &blend->info)) {
      FREE(blend);
      return NULL;
   }

   blend->dual_blend = util_blend_state_is_dual(state, 0);

   return blend;
}

static void
ilo_bind_blend_state(struct pipe_context *pipe, void *state)
{
   struct ilo_state_vector *vec = &ilo_context(pipe)->state_vector;

   vec->blend = state;

   vec->dirty |= ILO_DIRTY_BLEND;
}

static void
ilo_delete_blend_state(struct pipe_context *pipe, void  *state)
{
   FREE(state);
}

static void *
ilo_create_sampler_state(struct pipe_context *pipe,
                         const struct pipe_sampler_state *state)
{
   const struct ilo_dev *dev = ilo_context(pipe)->dev;
   struct ilo_sampler_cso *sampler;
   struct ilo_state_sampler_info info;
   struct ilo_state_sampler_border_info border;

   sampler = CALLOC_STRUCT(ilo_sampler_cso);
   assert(sampler);

   memset(&info, 0, sizeof(info));

   info.non_normalized = !state->normalized_coords;
   if (state->normalized_coords) {
      info.lod_bias = state->lod_bias;
      info.min_lod = state->min_lod;
      info.max_lod = state->max_lod;

      info.mip_filter = ilo_translate_mip_filter(state->min_mip_filter);
   } else {
      /* work around a bug in util_blitter */
      info.mip_filter = GEN6_MIPFILTER_NONE;
   }

   if (state->max_anisotropy) {
      info.min_filter = GEN6_MAPFILTER_ANISOTROPIC;
      info.mag_filter = GEN6_MAPFILTER_ANISOTROPIC;
   } else {
      info.min_filter = ilo_translate_img_filter(state->min_img_filter);
      info.mag_filter = ilo_translate_img_filter(state->mag_img_filter);
   }

   info.max_anisotropy = ilo_translate_max_anisotropy(state->max_anisotropy);

   /* use LOD 0 when no mipmapping (see sampler_set_gen6_SAMPLER_STATE()) */
   if (info.mip_filter == GEN6_MIPFILTER_NONE && info.min_lod > 0.0f) {
      info.min_lod = 0.0f;
      info.mag_filter = info.min_filter;
   }

   if (state->seamless_cube_map) {
      if (state->min_img_filter == PIPE_TEX_FILTER_NEAREST ||
          state->mag_img_filter == PIPE_TEX_FILTER_NEAREST) {
         info.tcx_ctrl = GEN6_TEXCOORDMODE_CLAMP;
         info.tcy_ctrl = GEN6_TEXCOORDMODE_CLAMP;
         info.tcz_ctrl = GEN6_TEXCOORDMODE_CLAMP;
      } else {
         info.tcx_ctrl = GEN6_TEXCOORDMODE_CUBE;
         info.tcy_ctrl = GEN6_TEXCOORDMODE_CUBE;
         info.tcz_ctrl = GEN6_TEXCOORDMODE_CUBE;
      }
   } else {
      info.tcx_ctrl = ilo_translate_address_wrap(state->wrap_s);
      info.tcy_ctrl = ilo_translate_address_wrap(state->wrap_t);
      info.tcz_ctrl = ilo_translate_address_wrap(state->wrap_r);

      if (ilo_dev_gen(dev) < ILO_GEN(8)) {
         /*
          * For nearest filtering, PIPE_TEX_WRAP_CLAMP means
          * PIPE_TEX_WRAP_CLAMP_TO_EDGE;  for linear filtering,
          * PIPE_TEX_WRAP_CLAMP means PIPE_TEX_WRAP_CLAMP_TO_BORDER while
          * additionally clamping the texture coordinates to [0.0, 1.0].
          *
          * PIPE_TEX_WRAP_CLAMP is not supported natively until Gen8.  The
          * clamping has to be taken care of in the shaders.  There are two
          * filters here, but let the minification one has a say.
          */
         const bool clamp_is_to_edge =
            (state->min_img_filter == PIPE_TEX_FILTER_NEAREST);

         if (clamp_is_to_edge) {
            if (info.tcx_ctrl == GEN8_TEXCOORDMODE_HALF_BORDER)
               info.tcx_ctrl = GEN6_TEXCOORDMODE_CLAMP;
            if (info.tcy_ctrl == GEN8_TEXCOORDMODE_HALF_BORDER)
               info.tcy_ctrl = GEN6_TEXCOORDMODE_CLAMP;
            if (info.tcz_ctrl == GEN8_TEXCOORDMODE_HALF_BORDER)
               info.tcz_ctrl = GEN6_TEXCOORDMODE_CLAMP;
         } else {
            if (info.tcx_ctrl == GEN8_TEXCOORDMODE_HALF_BORDER) {
               info.tcx_ctrl = GEN6_TEXCOORDMODE_CLAMP_BORDER;
               sampler->saturate_s = true;
            }
            if (info.tcy_ctrl == GEN8_TEXCOORDMODE_HALF_BORDER) {
               info.tcy_ctrl = GEN6_TEXCOORDMODE_CLAMP_BORDER;
               sampler->saturate_t = true;
            }
            if (info.tcz_ctrl == GEN8_TEXCOORDMODE_HALF_BORDER) {
               info.tcz_ctrl = GEN6_TEXCOORDMODE_CLAMP_BORDER;
               sampler->saturate_r = true;
            }
         }
      }
   }

   if (state->compare_mode == PIPE_TEX_COMPARE_R_TO_TEXTURE)
      info.shadow_func = ilo_translate_shadow_func(state->compare_func);

   ilo_state_sampler_init(&sampler->sampler, dev, &info);

   memset(&border, 0, sizeof(border));
   memcpy(border.rgba.f, state->border_color.f, sizeof(border.rgba.f));

   ilo_state_sampler_border_init(&sampler->border, dev, &border);

   return sampler;
}

static void
ilo_bind_sampler_states(struct pipe_context *pipe,
                        enum pipe_shader_type shader,
                        unsigned start, unsigned count, void **samplers)
{
   struct ilo_state_vector *vec = &ilo_context(pipe)->state_vector;
   struct ilo_sampler_state *dst = &vec->sampler[shader];
   bool changed = false;
   unsigned i;

   assert(start + count <= ARRAY_SIZE(dst->cso));

   if (samplers) {
      for (i = 0; i < count; i++) {
         if (dst->cso[start + i] != samplers[i]) {
            dst->cso[start + i] = samplers[i];

            /*
             * This function is sometimes called to reduce the number of bound
             * samplers.  Do not consider that as a state change (and create a
             * new array of SAMPLER_STATE).
             */
            if (samplers[i])
               changed = true;
         }
      }
   }
   else {
      for (i = 0; i < count; i++)
         dst->cso[start + i] = NULL;
   }

   if (changed) {
      switch (shader) {
      case PIPE_SHADER_VERTEX:
         vec->dirty |= ILO_DIRTY_SAMPLER_VS;
         break;
      case PIPE_SHADER_GEOMETRY:
         vec->dirty |= ILO_DIRTY_SAMPLER_GS;
         break;
      case PIPE_SHADER_FRAGMENT:
         vec->dirty |= ILO_DIRTY_SAMPLER_FS;
         break;
      case PIPE_SHADER_COMPUTE:
         vec->dirty |= ILO_DIRTY_SAMPLER_CS;
         break;
      }
   }
}

static void
ilo_delete_sampler_state(struct pipe_context *pipe, void *state)
{
   FREE(state);
}

static void *
ilo_create_rasterizer_state(struct pipe_context *pipe,
                            const struct pipe_rasterizer_state *state)
{
   const struct ilo_dev *dev = ilo_context(pipe)->dev;
   struct ilo_rasterizer_state *rast;
   struct ilo_state_raster_info *info;

   rast = CALLOC_STRUCT(ilo_rasterizer_state);
   assert(rast);

   rast->state = *state;

   info = &rast->info;

   info->clip.clip_enable = true;
   info->clip.stats_enable = true;
   info->clip.viewport_count = 1;
   info->clip.force_rtaindex_zero = true;
   info->clip.user_clip_enables = state->clip_plane_enable;
   info->clip.gb_test_enable = true;
   info->clip.xy_test_enable = true;
   info->clip.z_far_enable = state->depth_clip;
   info->clip.z_near_enable = state->depth_clip;
   info->clip.z_near_zero = state->clip_halfz;

   info->setup.first_vertex_provoking = state->flatshade_first;
   info->setup.viewport_transform = true;
   info->setup.scissor_enable = state->scissor;
   info->setup.msaa_enable = false;
   info->setup.line_msaa_enable = false;
   info->point.aa_enable = state->point_smooth;
   info->point.programmable_width = state->point_size_per_vertex;
   info->line.aa_enable = state->line_smooth;
   info->line.stipple_enable = state->line_stipple_enable;
   info->line.giq_enable = true;
   info->line.giq_last_pixel = state->line_last_pixel;
   info->tri.front_winding = ilo_translate_front_ccw(state->front_ccw);
   info->tri.cull_mode = ilo_translate_cull_face(state->cull_face);
   info->tri.fill_mode_front = ilo_translate_poly_mode(state->fill_front);
   info->tri.fill_mode_back = ilo_translate_poly_mode(state->fill_back);
   info->tri.depth_offset_format = GEN6_ZFORMAT_D24_UNORM_X8_UINT;
   info->tri.depth_offset_solid = state->offset_tri;
   info->tri.depth_offset_wireframe = state->offset_line;
   info->tri.depth_offset_point = state->offset_point;
   info->tri.poly_stipple_enable = state->poly_stipple_enable;

   info->scan.stats_enable = true;
   info->scan.sample_count = 1;
   info->scan.pixloc =
      ilo_translate_half_pixel_center(state->half_pixel_center);
   info->scan.sample_mask = ~0u;
   info->scan.zw_interp = GEN6_ZW_INTERP_PIXEL;
   info->scan.barycentric_interps = GEN6_INTERP_PERSPECTIVE_PIXEL;
   info->scan.earlyz_control = GEN7_EDSC_NORMAL;
   info->scan.earlyz_op = ILO_STATE_RASTER_EARLYZ_NORMAL;
   info->scan.earlyz_stencil_clear = false;

   info->params.any_integer_rt = false;
   info->params.hiz_enable = true;
   info->params.point_width =
      (state->point_size == 0.0f) ? 1.0f : state->point_size;
   info->params.line_width =
      (state->line_width == 0.0f) ? 1.0f : state->line_width;

   info->params.depth_offset_scale = state->offset_scale;
   /*
    * Scale the constant term.  The minimum representable value used by the HW
    * is not large enouch to be the minimum resolvable difference.
    */
   info->params.depth_offset_const = state->offset_units * 2.0f;
   info->params.depth_offset_clamp = state->offset_clamp;

   ilo_state_raster_init(&rast->rs, dev, info);

   return rast;
}

static void
ilo_bind_rasterizer_state(struct pipe_context *pipe, void *state)
{
   const struct ilo_dev *dev = ilo_context(pipe)->dev;
   struct ilo_state_vector *vec = &ilo_context(pipe)->state_vector;

   vec->rasterizer = state;

   if (vec->rasterizer) {
      struct ilo_state_line_stipple_info info;

      info.pattern = vec->rasterizer->state.line_stipple_pattern;
      info.repeat_count = vec->rasterizer->state.line_stipple_factor + 1;

      ilo_state_line_stipple_set_info(&vec->line_stipple, dev, &info);
   }

   vec->dirty |= ILO_DIRTY_RASTERIZER;
}

static void
ilo_delete_rasterizer_state(struct pipe_context *pipe, void *state)
{
   FREE(state);
}

static void *
ilo_create_depth_stencil_alpha_state(struct pipe_context *pipe,
                                     const struct pipe_depth_stencil_alpha_state *state)
{
   struct ilo_dsa_state *dsa;
   int i;

   dsa = CALLOC_STRUCT(ilo_dsa_state);
   assert(dsa);

   dsa->depth.cv_has_buffer = true;
   dsa->depth.test_enable = state->depth.enabled;
   dsa->depth.write_enable = state->depth.writemask;
   dsa->depth.test_func = ilo_translate_compare_func(state->depth.func);

   dsa->stencil.cv_has_buffer = true;
   for (i = 0; i < ARRAY_SIZE(state->stencil); i++) {
      const struct pipe_stencil_state *stencil = &state->stencil[i];
      struct ilo_state_cc_stencil_op_info *op;

      if (!stencil->enabled)
         break;

      if (i == 0) {
         dsa->stencil.test_enable = true;
         dsa->stencil_front.test_mask = stencil->valuemask;
         dsa->stencil_front.write_mask = stencil->writemask;

         op = &dsa->stencil.front;
      } else {
         dsa->stencil.twosided_enable = true;
         dsa->stencil_back.test_mask = stencil->valuemask;
         dsa->stencil_back.write_mask = stencil->writemask;

         op = &dsa->stencil.back;
      }

      op->test_func = ilo_translate_compare_func(stencil->func);
      op->fail_op = ilo_translate_stencil_op(stencil->fail_op);
      op->zfail_op = ilo_translate_stencil_op(stencil->zfail_op);
      op->zpass_op = ilo_translate_stencil_op(stencil->zpass_op);
   }

   dsa->alpha_test = state->alpha.enabled;
   dsa->alpha_ref = state->alpha.ref_value;
   dsa->alpha_func = ilo_translate_compare_func(state->alpha.func);

   return dsa;
}

static void
ilo_bind_depth_stencil_alpha_state(struct pipe_context *pipe, void *state)
{
   struct ilo_state_vector *vec = &ilo_context(pipe)->state_vector;

   vec->dsa = state;
   if (vec->dsa) {
      vec->cc_params.alpha_ref = vec->dsa->alpha_ref;
      vec->cc_params.stencil_front.test_mask =
         vec->dsa->stencil_front.test_mask;
      vec->cc_params.stencil_front.write_mask =
         vec->dsa->stencil_front.write_mask;
      vec->cc_params.stencil_back.test_mask =
         vec->dsa->stencil_back.test_mask;
      vec->cc_params.stencil_back.write_mask =
         vec->dsa->stencil_back.write_mask;
   }

   vec->dirty |= ILO_DIRTY_DSA;
}

static void
ilo_delete_depth_stencil_alpha_state(struct pipe_context *pipe, void *state)
{
   FREE(state);
}

static void *
ilo_create_fs_state(struct pipe_context *pipe,
                    const struct pipe_shader_state *state)
{
   struct ilo_context *ilo = ilo_context(pipe);
   struct ilo_shader_state *shader;

   shader = ilo_shader_create_fs(ilo->dev, state, &ilo->state_vector);
   assert(shader);

   ilo_shader_cache_add(ilo->shader_cache, shader);

   return shader;
}

static void
ilo_bind_fs_state(struct pipe_context *pipe, void *state)
{
   struct ilo_state_vector *vec = &ilo_context(pipe)->state_vector;

   vec->fs = state;

   vec->dirty |= ILO_DIRTY_FS;
}

static void
ilo_delete_fs_state(struct pipe_context *pipe, void *state)
{
   struct ilo_context *ilo = ilo_context(pipe);
   struct ilo_shader_state *fs = (struct ilo_shader_state *) state;

   ilo_shader_cache_remove(ilo->shader_cache, fs);
   ilo_shader_destroy(fs);
}

static void *
ilo_create_vs_state(struct pipe_context *pipe,
                    const struct pipe_shader_state *state)
{
   struct ilo_context *ilo = ilo_context(pipe);
   struct ilo_shader_state *shader;

   shader = ilo_shader_create_vs(ilo->dev, state, &ilo->state_vector);
   assert(shader);

   ilo_shader_cache_add(ilo->shader_cache, shader);

   return shader;
}

static void
ilo_bind_vs_state(struct pipe_context *pipe, void *state)
{
   struct ilo_state_vector *vec = &ilo_context(pipe)->state_vector;

   vec->vs = state;

   vec->dirty |= ILO_DIRTY_VS;
}

static void
ilo_delete_vs_state(struct pipe_context *pipe, void *state)
{
   struct ilo_context *ilo = ilo_context(pipe);
   struct ilo_shader_state *vs = (struct ilo_shader_state *) state;

   ilo_shader_cache_remove(ilo->shader_cache, vs);
   ilo_shader_destroy(vs);
}

static void *
ilo_create_gs_state(struct pipe_context *pipe,
                    const struct pipe_shader_state *state)
{
   struct ilo_context *ilo = ilo_context(pipe);
   struct ilo_shader_state *shader;

   shader = ilo_shader_create_gs(ilo->dev, state, &ilo->state_vector);
   assert(shader);

   ilo_shader_cache_add(ilo->shader_cache, shader);

   return shader;
}

static void
ilo_bind_gs_state(struct pipe_context *pipe, void *state)
{
   struct ilo_state_vector *vec = &ilo_context(pipe)->state_vector;

   /* util_blitter may set this unnecessarily */
   if (vec->gs == state)
      return;

   vec->gs = state;

   vec->dirty |= ILO_DIRTY_GS;
}

static void
ilo_delete_gs_state(struct pipe_context *pipe, void *state)
{
   struct ilo_context *ilo = ilo_context(pipe);
   struct ilo_shader_state *gs = (struct ilo_shader_state *) state;

   ilo_shader_cache_remove(ilo->shader_cache, gs);
   ilo_shader_destroy(gs);
}

static void *
ilo_create_vertex_elements_state(struct pipe_context *pipe,
                                 unsigned num_elements,
                                 const struct pipe_vertex_element *elements)
{
   const struct ilo_dev *dev = ilo_context(pipe)->dev;
   struct ilo_state_vf_element_info vf_elements[PIPE_MAX_ATTRIBS];
   unsigned instance_divisors[PIPE_MAX_ATTRIBS];
   struct ilo_state_vf_info vf_info;
   struct ilo_ve_state *ve;
   unsigned i;

   ve = CALLOC_STRUCT(ilo_ve_state);
   assert(ve);

   for (i = 0; i < num_elements; i++) {
      const struct pipe_vertex_element *elem = &elements[i];
      struct ilo_state_vf_element_info *attr = &vf_elements[i];
      unsigned hw_idx;

      /*
       * map the pipe vb to the hardware vb, which has a fixed instance
       * divisor
       */
      for (hw_idx = 0; hw_idx < ve->vb_count; hw_idx++) {
         if (ve->vb_mapping[hw_idx] == elem->vertex_buffer_index &&
             instance_divisors[hw_idx] == elem->instance_divisor)
            break;
      }

      /* create one if there is no matching hardware vb */
      if (hw_idx >= ve->vb_count) {
         hw_idx = ve->vb_count++;

         ve->vb_mapping[hw_idx] = elem->vertex_buffer_index;
         instance_divisors[hw_idx] = elem->instance_divisor;
      }

      attr->buffer = hw_idx;
      attr->vertex_offset = elem->src_offset;
      attr->format = ilo_format_translate_vertex(dev, elem->src_format);
      attr->format_size = util_format_get_blocksize(elem->src_format);
      attr->component_count = util_format_get_nr_components(elem->src_format);
      attr->is_integer = util_format_is_pure_integer(elem->src_format);

      attr->instancing_enable = (elem->instance_divisor != 0);
      attr->instancing_step_rate = elem->instance_divisor;
   }

   memset(&vf_info, 0, sizeof(vf_info));
   vf_info.data = ve->vf_data;
   vf_info.data_size = sizeof(ve->vf_data);
   vf_info.elements = vf_elements;
   vf_info.element_count = num_elements;
   /* vf_info.params and ve->vf_params are both zeroed */

   if (!ilo_state_vf_init(&ve->vf, dev, &vf_info)) {
      FREE(ve);
      return NULL;
   }

   return ve;
}

static void
ilo_bind_vertex_elements_state(struct pipe_context *pipe, void *state)
{
   struct ilo_state_vector *vec = &ilo_context(pipe)->state_vector;

   vec->ve = state;

   vec->dirty |= ILO_DIRTY_VE;
}

static void
ilo_delete_vertex_elements_state(struct pipe_context *pipe, void *state)
{
   struct ilo_ve_state *ve = state;

   FREE(ve);
}

static void
ilo_set_blend_color(struct pipe_context *pipe,
                    const struct pipe_blend_color *state)
{
   struct ilo_state_vector *vec = &ilo_context(pipe)->state_vector;

   memcpy(vec->cc_params.blend_rgba, state->color, sizeof(state->color));

   vec->dirty |= ILO_DIRTY_BLEND_COLOR;
}

static void
ilo_set_stencil_ref(struct pipe_context *pipe,
                    const struct pipe_stencil_ref *state)
{
   struct ilo_state_vector *vec = &ilo_context(pipe)->state_vector;

   /* util_blitter may set this unnecessarily */
   if (!memcmp(&vec->stencil_ref, state, sizeof(*state)))
      return;

   vec->stencil_ref = *state;

   vec->cc_params.stencil_front.test_ref = state->ref_value[0];
   vec->cc_params.stencil_back.test_ref = state->ref_value[1];

   vec->dirty |= ILO_DIRTY_STENCIL_REF;
}

static void
ilo_set_sample_mask(struct pipe_context *pipe,
                    unsigned sample_mask)
{
   struct ilo_state_vector *vec = &ilo_context(pipe)->state_vector;

   /* util_blitter may set this unnecessarily */
   if (vec->sample_mask == sample_mask)
      return;

   vec->sample_mask = sample_mask;

   vec->dirty |= ILO_DIRTY_SAMPLE_MASK;
}

static void
ilo_set_clip_state(struct pipe_context *pipe,
                   const struct pipe_clip_state *state)
{
   struct ilo_state_vector *vec = &ilo_context(pipe)->state_vector;

   vec->clip = *state;

   vec->dirty |= ILO_DIRTY_CLIP;
}

static void
ilo_set_constant_buffer(struct pipe_context *pipe,
                        uint shader, uint index,
                        const struct pipe_constant_buffer *buf)
{
   const struct ilo_dev *dev = ilo_context(pipe)->dev;
   struct ilo_state_vector *vec = &ilo_context(pipe)->state_vector;
   struct ilo_cbuf_state *cbuf = &vec->cbuf[shader];
   const unsigned count = 1;
   unsigned i;

   assert(shader < ARRAY_SIZE(vec->cbuf));
   assert(index + count <= ARRAY_SIZE(vec->cbuf[shader].cso));

   if (buf) {
      for (i = 0; i < count; i++) {
         struct ilo_cbuf_cso *cso = &cbuf->cso[index + i];

         pipe_resource_reference(&cso->resource, buf[i].buffer);

         cso->info.access = ILO_STATE_SURFACE_ACCESS_DP_DATA;
         cso->info.format = GEN6_FORMAT_R32G32B32A32_FLOAT;
         cso->info.format_size = 16;
         cso->info.struct_size = 16;
         cso->info.readonly = true;
         cso->info.size = buf[i].buffer_size;

         if (buf[i].buffer) {
            cso->info.vma = ilo_resource_get_vma(buf[i].buffer);
            cso->info.offset = buf[i].buffer_offset;

            memset(&cso->surface, 0, sizeof(cso->surface));
            ilo_state_surface_init_for_buffer(&cso->surface, dev, &cso->info);

            cso->user_buffer = NULL;

            cbuf->enabled_mask |= 1 << (index + i);
         } else if (buf[i].user_buffer) {
            cso->info.vma = NULL;
            /* buffer_offset does not apply for user buffer */
            cso->user_buffer = buf[i].user_buffer;

            cbuf->enabled_mask |= 1 << (index + i);
         } else {
            cso->info.vma = NULL;
            cso->info.size = 0;
            cso->user_buffer = NULL;

            cbuf->enabled_mask &= ~(1 << (index + i));
         }
      }
   } else {
      for (i = 0; i < count; i++) {
         struct ilo_cbuf_cso *cso = &cbuf->cso[index + i];

         pipe_resource_reference(&cso->resource, NULL);

         cso->info.vma = NULL;
         cso->info.size = 0;
         cso->user_buffer = NULL;

         cbuf->enabled_mask &= ~(1 << (index + i));
      }
   }

   vec->dirty |= ILO_DIRTY_CBUF;
}

static void
fb_set_blend_caps(const struct ilo_dev *dev,
                  enum pipe_format format,
                  struct ilo_fb_blend_caps *caps)
{
   const struct util_format_description *desc =
      util_format_description(format);
   const int ch = util_format_get_first_non_void_channel(format);

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

   if (format == PIPE_FORMAT_NONE || desc->is_mixed)
      return;

   caps->is_unorm = (ch >= 0 && desc->channel[ch].normalized &&
         desc->channel[ch].type == UTIL_FORMAT_TYPE_UNSIGNED &&
         desc->colorspace == UTIL_FORMAT_COLORSPACE_RGB);
   caps->is_integer = util_format_is_pure_integer(format);

   /*
    * From the Sandy Bridge PRM, volume 2 part 1, page 365:
    *
    *     "Logic Ops are only supported on *_UNORM surfaces (excluding _SRGB
    *      variants), otherwise Logic Ops must be DISABLED."
    *
    * According to the classic driver, this is lifted on Gen8+.
    */
   caps->can_logicop = (ilo_dev_gen(dev) >= ILO_GEN(8) || caps->is_unorm);

   /* no blending for pure integer formats */
   caps->can_blend = !caps->is_integer;

   /*
    * From the Sandy Bridge PRM, volume 2 part 1, page 382:
    *
    *     "Alpha Test can only be enabled if Pixel Shader outputs a float
    *      alpha value."
    */
   caps->can_alpha_test = !caps->is_integer;

   caps->force_dst_alpha_one =
      (ilo_format_translate_render(dev, format) !=
       ilo_format_translate_color(dev, format));

   /* sanity check */
   if (caps->force_dst_alpha_one) {
      enum pipe_format render_format;

      switch (format) {
      case PIPE_FORMAT_B8G8R8X8_UNORM:
         render_format = PIPE_FORMAT_B8G8R8A8_UNORM;
         break;
      default:
         render_format = PIPE_FORMAT_NONE;
         break;
      }

      assert(ilo_format_translate_render(dev, format) ==
             ilo_format_translate_color(dev, render_format));
   }
}

static void
ilo_set_framebuffer_state(struct pipe_context *pipe,
                          const struct pipe_framebuffer_state *state)
{
   const struct ilo_dev *dev = ilo_context(pipe)->dev;
   struct ilo_state_vector *vec = &ilo_context(pipe)->state_vector;
   struct ilo_fb_state *fb = &vec->fb;
   const struct pipe_surface *first_surf = NULL;
   int i;

   util_copy_framebuffer_state(&fb->state, state);

   fb->has_integer_rt = false;
   for (i = 0; i < state->nr_cbufs; i++) {
      if (state->cbufs[i]) {
         fb_set_blend_caps(dev, state->cbufs[i]->format, &fb->blend_caps[i]);

         fb->has_integer_rt |= fb->blend_caps[i].is_integer;

         if (!first_surf)
            first_surf = state->cbufs[i];
      } else {
         fb_set_blend_caps(dev, PIPE_FORMAT_NONE, &fb->blend_caps[i]);
      }
   }

   if (!first_surf && state->zsbuf)
      first_surf = state->zsbuf;

   fb->num_samples = (first_surf) ? first_surf->texture->nr_samples : 1;
   if (!fb->num_samples)
      fb->num_samples = 1;

   if (state->zsbuf) {
      const struct ilo_surface_cso *cso =
         (const struct ilo_surface_cso *) state->zsbuf;
      const struct ilo_texture *tex = ilo_texture(cso->base.texture);

      fb->has_hiz = cso->u.zs.hiz_vma;
      fb->depth_offset_format =
         ilo_format_translate_depth(dev, tex->image_format);
   } else {
      fb->has_hiz = false;
      fb->depth_offset_format = GEN6_ZFORMAT_D32_FLOAT;
   }

   /*
    * The PRMs list several restrictions when the framebuffer has more than
    * one surface.  It seems they are actually lifted on GEN6+.
    */

   vec->dirty |= ILO_DIRTY_FB;
}

static void
ilo_set_polygon_stipple(struct pipe_context *pipe,
                        const struct pipe_poly_stipple *state)
{
   const struct ilo_dev *dev = ilo_context(pipe)->dev;
   struct ilo_state_vector *vec = &ilo_context(pipe)->state_vector;
   struct ilo_state_poly_stipple_info info;
   int i;

   for (i = 0; i < 32; i++)
      info.pattern[i] = state->stipple[i];

   ilo_state_poly_stipple_set_info(&vec->poly_stipple, dev, &info);

   vec->dirty |= ILO_DIRTY_POLY_STIPPLE;
}

static void
ilo_set_scissor_states(struct pipe_context *pipe,
                       unsigned start_slot,
                       unsigned num_scissors,
                       const struct pipe_scissor_state *scissors)
{
   struct ilo_state_vector *vec = &ilo_context(pipe)->state_vector;
   unsigned i;

   for (i = 0; i < num_scissors; i++) {
      struct ilo_state_viewport_scissor_info *info =
         &vec->viewport.scissors[start_slot + i];

      if (scissors[i].minx < scissors[i].maxx &&
          scissors[i].miny < scissors[i].maxy) {
         info->min_x = scissors[i].minx;
         info->min_y = scissors[i].miny;
         info->max_x = scissors[i].maxx - 1;
         info->max_y = scissors[i].maxy - 1;
      } else {
         info->min_x = 1;
         info->min_y = 1;
         info->max_x = 0;
         info->max_y = 0;
      }
   }

   vec->dirty |= ILO_DIRTY_SCISSOR;
}

static void
ilo_set_viewport_states(struct pipe_context *pipe,
                        unsigned start_slot,
                        unsigned num_viewports,
                        const struct pipe_viewport_state *viewports)
{
   struct ilo_state_vector *vec = &ilo_context(pipe)->state_vector;

   if (viewports) {
      unsigned i;

      for (i = 0; i < num_viewports; i++) {
         struct ilo_state_viewport_matrix_info *info =
            &vec->viewport.matrices[start_slot + i];

         memcpy(info->scale, viewports[i].scale, sizeof(info->scale));
         memcpy(info->translate, viewports[i].translate,
               sizeof(info->translate));
      }

      if (vec->viewport.params.count < start_slot + num_viewports)
         vec->viewport.params.count = start_slot + num_viewports;

      /* need to save viewport 0 for util_blitter */
      if (!start_slot && num_viewports)
         vec->viewport.viewport0 = viewports[0];
   }
   else {
      if (vec->viewport.params.count <= start_slot + num_viewports &&
          vec->viewport.params.count > start_slot)
         vec->viewport.params.count = start_slot;
   }

   vec->dirty |= ILO_DIRTY_VIEWPORT;
}

static void
ilo_set_sampler_views(struct pipe_context *pipe, unsigned shader,
                      unsigned start, unsigned count,
                      struct pipe_sampler_view **views)
{
   struct ilo_state_vector *vec = &ilo_context(pipe)->state_vector;
   struct ilo_view_state *dst = &vec->view[shader];
   unsigned i;

   assert(start + count <= ARRAY_SIZE(dst->states));

   if (views) {
      for (i = 0; i < count; i++)
         pipe_sampler_view_reference(&dst->states[start + i], views[i]);
   }
   else {
      for (i = 0; i < count; i++)
         pipe_sampler_view_reference(&dst->states[start + i], NULL);
   }

   if (dst->count <= start + count) {
      if (views)
         count += start;
      else
         count = start;

      while (count > 0 && !dst->states[count - 1])
         count--;

      dst->count = count;
   }

   switch (shader) {
   case PIPE_SHADER_VERTEX:
      vec->dirty |= ILO_DIRTY_VIEW_VS;
      break;
   case PIPE_SHADER_GEOMETRY:
      vec->dirty |= ILO_DIRTY_VIEW_GS;
      break;
   case PIPE_SHADER_FRAGMENT:
      vec->dirty |= ILO_DIRTY_VIEW_FS;
      break;
   case PIPE_SHADER_COMPUTE:
      vec->dirty |= ILO_DIRTY_VIEW_CS;
      break;
   }
}

static void
ilo_set_shader_images(struct pipe_context *pipe, unsigned shader,
                      unsigned start, unsigned count,
                      const struct pipe_image_view *views)
{
#if 0
   struct ilo_state_vector *vec = &ilo_context(pipe)->state_vector;
   struct ilo_resource_state *dst = &vec->resource;
   unsigned i;

   assert(start + count <= ARRAY_SIZE(dst->states));

   if (surfaces) {
      for (i = 0; i < count; i++)
         pipe_surface_reference(&dst->states[start + i], surfaces[i]);
   }
   else {
      for (i = 0; i < count; i++)
         pipe_surface_reference(&dst->states[start + i], NULL);
   }

   if (dst->count <= start + count) {
      if (surfaces)
         count += start;
      else
         count = start;

      while (count > 0 && !dst->states[count - 1])
         count--;

      dst->count = count;
   }

   vec->dirty |= ILO_DIRTY_RESOURCE;
#endif
}

static void
ilo_set_vertex_buffers(struct pipe_context *pipe,
                       unsigned start_slot, unsigned num_buffers,
                       const struct pipe_vertex_buffer *buffers)
{
   struct ilo_state_vector *vec = &ilo_context(pipe)->state_vector;
   unsigned i;

   /* no PIPE_CAP_USER_VERTEX_BUFFERS */
   if (buffers) {
      for (i = 0; i < num_buffers; i++)
         assert(!buffers[i].user_buffer);
   }

   util_set_vertex_buffers_mask(vec->vb.states,
         &vec->vb.enabled_mask, buffers, start_slot, num_buffers);

   vec->dirty |= ILO_DIRTY_VB;
}

static void
ilo_set_index_buffer(struct pipe_context *pipe,
                     const struct pipe_index_buffer *state)
{
   struct ilo_state_vector *vec = &ilo_context(pipe)->state_vector;

   if (state) {
      pipe_resource_reference(&vec->ib.state.buffer, state->buffer);
      vec->ib.state = *state;
   } else {
      pipe_resource_reference(&vec->ib.state.buffer, NULL);
      memset(&vec->ib.state, 0, sizeof(vec->ib.state));
   }

   vec->dirty |= ILO_DIRTY_IB;
}

static struct pipe_stream_output_target *
ilo_create_stream_output_target(struct pipe_context *pipe,
                                struct pipe_resource *res,
                                unsigned buffer_offset,
                                unsigned buffer_size)
{
   const struct ilo_dev *dev = ilo_context(pipe)->dev;
   struct ilo_stream_output_target *target;
   struct ilo_state_sol_buffer_info info;

   target = CALLOC_STRUCT(ilo_stream_output_target);
   assert(target);

   pipe_reference_init(&target->base.reference, 1);
   pipe_resource_reference(&target->base.buffer, res);
   target->base.context = pipe;
   target->base.buffer_offset = buffer_offset;
   target->base.buffer_size = buffer_size;

   memset(&info, 0, sizeof(info));
   info.vma = ilo_resource_get_vma(res);
   info.offset = buffer_offset;
   info.size = buffer_size;

   ilo_state_sol_buffer_init(&target->sb, dev, &info);

   return &target->base;
}

static void
ilo_set_stream_output_targets(struct pipe_context *pipe,
                              unsigned num_targets,
                              struct pipe_stream_output_target **targets,
                              const unsigned *offset)
{
   struct ilo_state_vector *vec = &ilo_context(pipe)->state_vector;
   unsigned i;
   unsigned append_bitmask = 0;

   if (!targets)
      num_targets = 0;

   /* util_blitter may set this unnecessarily */
   if (!vec->so.count && !num_targets)
      return;

   for (i = 0; i < num_targets; i++) {
      pipe_so_target_reference(&vec->so.states[i], targets[i]);
      if (offset[i] == (unsigned)-1)
         append_bitmask |= 1 << i;
   }

   for (; i < vec->so.count; i++)
      pipe_so_target_reference(&vec->so.states[i], NULL);

   vec->so.count = num_targets;
   vec->so.append_bitmask = append_bitmask;

   vec->so.enabled = (vec->so.count > 0);

   vec->dirty |= ILO_DIRTY_SO;
}

static void
ilo_stream_output_target_destroy(struct pipe_context *pipe,
                                 struct pipe_stream_output_target *target)
{
   pipe_resource_reference(&target->buffer, NULL);
   FREE(target);
}

static struct pipe_sampler_view *
ilo_create_sampler_view(struct pipe_context *pipe,
                        struct pipe_resource *res,
                        const struct pipe_sampler_view *templ)
{
   const struct ilo_dev *dev = ilo_context(pipe)->dev;
   struct ilo_view_cso *view;

   view = CALLOC_STRUCT(ilo_view_cso);
   assert(view);

   view->base = *templ;
   pipe_reference_init(&view->base.reference, 1);
   view->base.texture = NULL;
   pipe_resource_reference(&view->base.texture, res);
   view->base.context = pipe;

   if (res->target == PIPE_BUFFER) {
      struct ilo_state_surface_buffer_info info;

      memset(&info, 0, sizeof(info));
      info.vma = ilo_resource_get_vma(res);
      info.offset = templ->u.buf.offset;
      info.size = templ->u.buf.size;
      info.access = ILO_STATE_SURFACE_ACCESS_SAMPLER;
      info.format = ilo_format_translate_color(dev, templ->format);
      info.format_size = util_format_get_blocksize(templ->format);
      info.struct_size = info.format_size;
      info.readonly = true;

      ilo_state_surface_init_for_buffer(&view->surface, dev, &info);
   } else {
      struct ilo_texture *tex = ilo_texture(res);
      struct ilo_state_surface_image_info info;

      /* warn about degraded performance because of a missing binding flag */
      if (tex->image.tiling == GEN6_TILING_NONE &&
          !(tex->base.bind & PIPE_BIND_SAMPLER_VIEW)) {
         ilo_warn("creating sampler view for a resource "
                  "not created for sampling\n");
      }

      memset(&info, 0, sizeof(info));

      info.img = &tex->image;
      info.level_base = templ->u.tex.first_level;
      info.level_count = templ->u.tex.last_level -
         templ->u.tex.first_level + 1;
      info.slice_base = templ->u.tex.first_layer;
      info.slice_count = templ->u.tex.last_layer -
         templ->u.tex.first_layer + 1;

      info.vma = &tex->vma;
      info.access = ILO_STATE_SURFACE_ACCESS_SAMPLER;
      info.type = tex->image.type;

      if (templ->format == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT &&
          tex->separate_s8) {
         info.format = ilo_format_translate_texture(dev,
               PIPE_FORMAT_Z32_FLOAT);
      } else {
         info.format = ilo_format_translate_texture(dev, templ->format);
      }

      info.is_array = util_resource_is_array_texture(&tex->base);
      info.readonly = true;

      ilo_state_surface_init_for_image(&view->surface, dev, &info);
   }

   return &view->base;
}

static void
ilo_sampler_view_destroy(struct pipe_context *pipe,
                         struct pipe_sampler_view *view)
{
   pipe_resource_reference(&view->texture, NULL);
   FREE(view);
}

static struct pipe_surface *
ilo_create_surface(struct pipe_context *pipe,
                   struct pipe_resource *res,
                   const struct pipe_surface *templ)
{
   const struct ilo_dev *dev = ilo_context(pipe)->dev;
   struct ilo_texture *tex = ilo_texture(res);
   struct ilo_surface_cso *surf;

   surf = CALLOC_STRUCT(ilo_surface_cso);
   assert(surf);

   surf->base = *templ;
   pipe_reference_init(&surf->base.reference, 1);
   surf->base.texture = NULL;
   pipe_resource_reference(&surf->base.texture, &tex->base);

   surf->base.context = pipe;
   surf->base.width = u_minify(tex->base.width0, templ->u.tex.level);
   surf->base.height = u_minify(tex->base.height0, templ->u.tex.level);

   surf->is_rt = !util_format_is_depth_or_stencil(templ->format);

   if (surf->is_rt) {
      struct ilo_state_surface_image_info info;

      /* relax this? */
      assert(tex->base.target != PIPE_BUFFER);

      memset(&info, 0, sizeof(info));

      info.img = &tex->image;
      info.level_base = templ->u.tex.level;
      info.level_count = 1;
      info.slice_base = templ->u.tex.first_layer;
      info.slice_count = templ->u.tex.last_layer -
         templ->u.tex.first_layer + 1;

      info.vma = &tex->vma;
      if (ilo_image_can_enable_aux(&tex->image, templ->u.tex.level))
         info.aux_vma = &tex->aux_vma;

      info.access = ILO_STATE_SURFACE_ACCESS_DP_RENDER;

      info.type = (tex->image.type == GEN6_SURFTYPE_CUBE) ?
         GEN6_SURFTYPE_2D : tex->image.type;

      info.format = ilo_format_translate_render(dev, templ->format);
      info.is_array = util_resource_is_array_texture(&tex->base);

      ilo_state_surface_init_for_image(&surf->u.rt, dev, &info);
   } else {
      struct ilo_state_zs_info info;

      assert(res->target != PIPE_BUFFER);

      memset(&info, 0, sizeof(info));

      if (templ->format == PIPE_FORMAT_S8_UINT) {
         info.s_vma = &tex->vma;
         info.s_img = &tex->image;
      } else {
         info.z_vma = &tex->vma;
         info.z_img = &tex->image;

         if (tex->separate_s8) {
            info.s_vma = &tex->separate_s8->vma;
            info.s_img = &tex->separate_s8->image;
         }

         if (ilo_image_can_enable_aux(&tex->image, templ->u.tex.level))
            info.hiz_vma = &tex->aux_vma;
      }

      info.level = templ->u.tex.level;
      info.slice_base = templ->u.tex.first_layer;
      info.slice_count = templ->u.tex.last_layer -
         templ->u.tex.first_layer + 1;

      info.type = (tex->image.type == GEN6_SURFTYPE_CUBE) ?
         GEN6_SURFTYPE_2D : tex->image.type;

      info.format = ilo_format_translate_depth(dev, tex->image_format);
      if (ilo_dev_gen(dev) == ILO_GEN(6) && !info.hiz_vma &&
          tex->image_format == PIPE_FORMAT_Z24X8_UNORM)
         info.format = GEN6_ZFORMAT_D24_UNORM_S8_UINT;

      ilo_state_zs_init(&surf->u.zs, dev, &info);
   }

   return &surf->base;
}

static void
ilo_surface_destroy(struct pipe_context *pipe,
                    struct pipe_surface *surface)
{
   pipe_resource_reference(&surface->texture, NULL);
   FREE(surface);
}

static void *
ilo_create_compute_state(struct pipe_context *pipe,
                         const struct pipe_compute_state *state)
{
   struct ilo_context *ilo = ilo_context(pipe);
   struct ilo_shader_state *shader;

   shader = ilo_shader_create_cs(ilo->dev, state, &ilo->state_vector);
   assert(shader);

   ilo_shader_cache_add(ilo->shader_cache, shader);

   return shader;
}

static void
ilo_bind_compute_state(struct pipe_context *pipe, void *state)
{
   struct ilo_state_vector *vec = &ilo_context(pipe)->state_vector;

   vec->cs = state;

   vec->dirty |= ILO_DIRTY_CS;
}

static void
ilo_delete_compute_state(struct pipe_context *pipe, void *state)
{
   struct ilo_context *ilo = ilo_context(pipe);
   struct ilo_shader_state *cs = (struct ilo_shader_state *) state;

   ilo_shader_cache_remove(ilo->shader_cache, cs);
   ilo_shader_destroy(cs);
}

static void
ilo_set_compute_resources(struct pipe_context *pipe,
                          unsigned start, unsigned count,
                          struct pipe_surface **surfaces)
{
   struct ilo_state_vector *vec = &ilo_context(pipe)->state_vector;
   struct ilo_resource_state *dst = &vec->cs_resource;
   unsigned i;

   assert(start + count <= ARRAY_SIZE(dst->states));

   if (surfaces) {
      for (i = 0; i < count; i++)
         pipe_surface_reference(&dst->states[start + i], surfaces[i]);
   }
   else {
      for (i = 0; i < count; i++)
         pipe_surface_reference(&dst->states[start + i], NULL);
   }

   if (dst->count <= start + count) {
      if (surfaces)
         count += start;
      else
         count = start;

      while (count > 0 && !dst->states[count - 1])
         count--;

      dst->count = count;
   }

   vec->dirty |= ILO_DIRTY_CS_RESOURCE;
}

static void
ilo_set_global_binding(struct pipe_context *pipe,
                       unsigned start, unsigned count,
                       struct pipe_resource **resources,
                       uint32_t **handles)
{
   struct ilo_state_vector *vec = &ilo_context(pipe)->state_vector;
   struct ilo_global_binding_cso *dst;
   unsigned i;

   /* make room */
   if (vec->global_binding.count < start + count) {
      if (resources) {
         const unsigned old_size = vec->global_binding.bindings.size;
         const unsigned new_size = sizeof(*dst) * (start + count);

         if (old_size < new_size) {
            util_dynarray_resize(&vec->global_binding.bindings, new_size);
            memset(vec->global_binding.bindings.data + old_size, 0,
                  new_size - old_size);
         }
      } else {
         count = vec->global_binding.count - start;
      }
   }

   dst = util_dynarray_element(&vec->global_binding.bindings,
         struct ilo_global_binding_cso, start);

   if (resources) {
      for (i = 0; i < count; i++) {
         pipe_resource_reference(&dst[i].resource, resources[i]);
         dst[i].handle = handles[i];
      }
   } else {
      for (i = 0; i < count; i++) {
         pipe_resource_reference(&dst[i].resource, NULL);
         dst[i].handle = NULL;
      }
   }

   if (vec->global_binding.count <= start + count) {
      dst = util_dynarray_begin(&vec->global_binding.bindings);

      if (resources)
         count += start;
      else
         count = start;

      while (count > 0 && !dst[count - 1].resource)
         count--;

      vec->global_binding.count = count;
   }

   vec->dirty |= ILO_DIRTY_GLOBAL_BINDING;
}

/**
 * Initialize state-related functions.
 */
void
ilo_init_state_functions(struct ilo_context *ilo)
{
   STATIC_ASSERT(ILO_STATE_COUNT <= 32);

   ilo->base.create_blend_state = ilo_create_blend_state;
   ilo->base.bind_blend_state = ilo_bind_blend_state;
   ilo->base.delete_blend_state = ilo_delete_blend_state;
   ilo->base.create_sampler_state = ilo_create_sampler_state;
   ilo->base.bind_sampler_states = ilo_bind_sampler_states;
   ilo->base.delete_sampler_state = ilo_delete_sampler_state;
   ilo->base.create_rasterizer_state = ilo_create_rasterizer_state;
   ilo->base.bind_rasterizer_state = ilo_bind_rasterizer_state;
   ilo->base.delete_rasterizer_state = ilo_delete_rasterizer_state;
   ilo->base.create_depth_stencil_alpha_state = ilo_create_depth_stencil_alpha_state;
   ilo->base.bind_depth_stencil_alpha_state = ilo_bind_depth_stencil_alpha_state;
   ilo->base.delete_depth_stencil_alpha_state = ilo_delete_depth_stencil_alpha_state;
   ilo->base.create_fs_state = ilo_create_fs_state;
   ilo->base.bind_fs_state = ilo_bind_fs_state;
   ilo->base.delete_fs_state = ilo_delete_fs_state;
   ilo->base.create_vs_state = ilo_create_vs_state;
   ilo->base.bind_vs_state = ilo_bind_vs_state;
   ilo->base.delete_vs_state = ilo_delete_vs_state;
   ilo->base.create_gs_state = ilo_create_gs_state;
   ilo->base.bind_gs_state = ilo_bind_gs_state;
   ilo->base.delete_gs_state = ilo_delete_gs_state;
   ilo->base.create_vertex_elements_state = ilo_create_vertex_elements_state;
   ilo->base.bind_vertex_elements_state = ilo_bind_vertex_elements_state;
   ilo->base.delete_vertex_elements_state = ilo_delete_vertex_elements_state;

   ilo->base.set_blend_color = ilo_set_blend_color;
   ilo->base.set_stencil_ref = ilo_set_stencil_ref;
   ilo->base.set_sample_mask = ilo_set_sample_mask;
   ilo->base.set_clip_state = ilo_set_clip_state;
   ilo->base.set_constant_buffer = ilo_set_constant_buffer;
   ilo->base.set_framebuffer_state = ilo_set_framebuffer_state;
   ilo->base.set_polygon_stipple = ilo_set_polygon_stipple;
   ilo->base.set_scissor_states = ilo_set_scissor_states;
   ilo->base.set_viewport_states = ilo_set_viewport_states;
   ilo->base.set_sampler_views = ilo_set_sampler_views;
   ilo->base.set_shader_images = ilo_set_shader_images;
   ilo->base.set_vertex_buffers = ilo_set_vertex_buffers;
   ilo->base.set_index_buffer = ilo_set_index_buffer;

   ilo->base.create_stream_output_target = ilo_create_stream_output_target;
   ilo->base.stream_output_target_destroy = ilo_stream_output_target_destroy;
   ilo->base.set_stream_output_targets = ilo_set_stream_output_targets;

   ilo->base.create_sampler_view = ilo_create_sampler_view;
   ilo->base.sampler_view_destroy = ilo_sampler_view_destroy;

   ilo->base.create_surface = ilo_create_surface;
   ilo->base.surface_destroy = ilo_surface_destroy;

   ilo->base.create_compute_state = ilo_create_compute_state;
   ilo->base.bind_compute_state = ilo_bind_compute_state;
   ilo->base.delete_compute_state = ilo_delete_compute_state;
   ilo->base.set_compute_resources = ilo_set_compute_resources;
   ilo->base.set_global_binding = ilo_set_global_binding;
}

void
ilo_state_vector_init(const struct ilo_dev *dev,
                      struct ilo_state_vector *vec)
{
   struct ilo_state_urb_info urb_info;

   vec->sample_mask = ~0u;

   ilo_state_viewport_init_data_only(&vec->viewport.vp, dev,
         vec->viewport.vp_data, sizeof(vec->viewport.vp_data));
   assert(vec->viewport.vp.array_size >= ILO_MAX_VIEWPORTS);

   vec->viewport.params.matrices = vec->viewport.matrices;
   vec->viewport.params.scissors = vec->viewport.scissors;

   ilo_state_hs_init_disabled(&vec->disabled_hs, dev);
   ilo_state_ds_init_disabled(&vec->disabled_ds, dev);
   ilo_state_gs_init_disabled(&vec->disabled_gs, dev);

   ilo_state_sol_buffer_init_disabled(&vec->so.dummy_sb, dev);

   ilo_state_surface_init_for_null(&vec->fb.null_rt, dev);
   ilo_state_zs_init_for_null(&vec->fb.null_zs, dev);

   ilo_state_sampler_init_disabled(&vec->disabled_sampler, dev);

   memset(&urb_info, 0, sizeof(urb_info));
   ilo_state_urb_init(&vec->urb, dev, &urb_info);

   util_dynarray_init(&vec->global_binding.bindings);

   vec->dirty = ILO_DIRTY_ALL;
}

void
ilo_state_vector_cleanup(struct ilo_state_vector *vec)
{
   unsigned i, sh;

   for (i = 0; i < ARRAY_SIZE(vec->vb.states); i++) {
      if (vec->vb.enabled_mask & (1 << i))
         pipe_resource_reference(&vec->vb.states[i].buffer, NULL);
   }

   pipe_resource_reference(&vec->ib.state.buffer, NULL);
   pipe_resource_reference(&vec->ib.hw_resource, NULL);

   for (i = 0; i < vec->so.count; i++)
      pipe_so_target_reference(&vec->so.states[i], NULL);

   for (sh = 0; sh < PIPE_SHADER_TYPES; sh++) {
      for (i = 0; i < vec->view[sh].count; i++) {
         struct pipe_sampler_view *view = vec->view[sh].states[i];
         pipe_sampler_view_reference(&view, NULL);
      }

      for (i = 0; i < ARRAY_SIZE(vec->cbuf[sh].cso); i++) {
         struct ilo_cbuf_cso *cbuf = &vec->cbuf[sh].cso[i];
         pipe_resource_reference(&cbuf->resource, NULL);
      }
   }

   for (i = 0; i < vec->resource.count; i++)
      pipe_surface_reference(&vec->resource.states[i], NULL);

   for (i = 0; i < vec->fb.state.nr_cbufs; i++)
      pipe_surface_reference(&vec->fb.state.cbufs[i], NULL);

   if (vec->fb.state.zsbuf)
      pipe_surface_reference(&vec->fb.state.zsbuf, NULL);

   for (i = 0; i < vec->cs_resource.count; i++)
      pipe_surface_reference(&vec->cs_resource.states[i], NULL);

   for (i = 0; i < vec->global_binding.count; i++) {
      struct ilo_global_binding_cso *cso =
         util_dynarray_element(&vec->global_binding.bindings,
               struct ilo_global_binding_cso, i);
      pipe_resource_reference(&cso->resource, NULL);
   }

   util_dynarray_fini(&vec->global_binding.bindings);
}

/**
 * Mark all states that have the resource dirty.
 */
void
ilo_state_vector_resource_renamed(struct ilo_state_vector *vec,
                                  struct pipe_resource *res)
{
   uint32_t states = 0;
   unsigned sh, i;

   if (res->target == PIPE_BUFFER) {
      uint32_t vb_mask = vec->vb.enabled_mask;

      while (vb_mask) {
         const unsigned idx = u_bit_scan(&vb_mask);

         if (vec->vb.states[idx].buffer == res) {
            states |= ILO_DIRTY_VB;
            break;
         }
      }

      if (vec->ib.state.buffer == res) {
         states |= ILO_DIRTY_IB;

         /*
          * finalize_index_buffer() has an optimization that clears
          * ILO_DIRTY_IB when the HW states do not change.  However, it fails
          * to flush the VF cache when the HW states do not change, but the
          * contents of the IB has changed.  Here, we set the index size to an
          * invalid value to avoid the optimization.
          */
         vec->ib.hw_index_size = 0;
      }

      for (i = 0; i < vec->so.count; i++) {
         if (vec->so.states[i]->buffer == res) {
            states |= ILO_DIRTY_SO;
            break;
         }
      }
   }

   for (sh = 0; sh < PIPE_SHADER_TYPES; sh++) {
      for (i = 0; i < vec->view[sh].count; i++) {
         struct ilo_view_cso *cso = (struct ilo_view_cso *) vec->view[sh].states[i];

         if (cso->base.texture == res) {
            static const unsigned view_dirty_bits[PIPE_SHADER_TYPES] = {
               [PIPE_SHADER_VERTEX]    = ILO_DIRTY_VIEW_VS,
               [PIPE_SHADER_FRAGMENT]  = ILO_DIRTY_VIEW_FS,
               [PIPE_SHADER_GEOMETRY]  = ILO_DIRTY_VIEW_GS,
               [PIPE_SHADER_COMPUTE]   = ILO_DIRTY_VIEW_CS,
            };

            states |= view_dirty_bits[sh];
            break;
         }
      }

      if (res->target == PIPE_BUFFER) {
         for (i = 0; i < ARRAY_SIZE(vec->cbuf[sh].cso); i++) {
            struct ilo_cbuf_cso *cbuf = &vec->cbuf[sh].cso[i];

            if (cbuf->resource == res) {
               states |= ILO_DIRTY_CBUF;
               break;
            }
         }
      }
   }

   for (i = 0; i < vec->resource.count; i++) {
      struct ilo_surface_cso *cso =
         (struct ilo_surface_cso *) vec->resource.states[i];

      if (cso->base.texture == res) {
         states |= ILO_DIRTY_RESOURCE;
         break;
      }
   }

   /* for now? */
   if (res->target != PIPE_BUFFER) {
      for (i = 0; i < vec->fb.state.nr_cbufs; i++) {
         struct ilo_surface_cso *cso =
            (struct ilo_surface_cso *) vec->fb.state.cbufs[i];
         if (cso && cso->base.texture == res) {
            states |= ILO_DIRTY_FB;
            break;
         }
      }

      if (vec->fb.state.zsbuf && vec->fb.state.zsbuf->texture == res)
         states |= ILO_DIRTY_FB;
   }

   for (i = 0; i < vec->cs_resource.count; i++) {
      struct ilo_surface_cso *cso =
         (struct ilo_surface_cso *) vec->cs_resource.states[i];
      if (cso->base.texture == res) {
         states |= ILO_DIRTY_CS_RESOURCE;
         break;
      }
   }

   for (i = 0; i < vec->global_binding.count; i++) {
      struct ilo_global_binding_cso *cso =
         util_dynarray_element(&vec->global_binding.bindings,
               struct ilo_global_binding_cso, i);

      if (cso->resource == res) {
         states |= ILO_DIRTY_GLOBAL_BINDING;
         break;
      }
   }

   vec->dirty |= states;
}

void
ilo_state_vector_dump_dirty(const struct ilo_state_vector *vec)
{
   static const char *state_names[ILO_STATE_COUNT] = {
      [ILO_STATE_VB]              = "VB",
      [ILO_STATE_VE]              = "VE",
      [ILO_STATE_IB]              = "IB",
      [ILO_STATE_VS]              = "VS",
      [ILO_STATE_GS]              = "GS",
      [ILO_STATE_SO]              = "SO",
      [ILO_STATE_CLIP]            = "CLIP",
      [ILO_STATE_VIEWPORT]        = "VIEWPORT",
      [ILO_STATE_SCISSOR]         = "SCISSOR",
      [ILO_STATE_RASTERIZER]      = "RASTERIZER",
      [ILO_STATE_POLY_STIPPLE]    = "POLY_STIPPLE",
      [ILO_STATE_SAMPLE_MASK]     = "SAMPLE_MASK",
      [ILO_STATE_FS]              = "FS",
      [ILO_STATE_DSA]             = "DSA",
      [ILO_STATE_STENCIL_REF]     = "STENCIL_REF",
      [ILO_STATE_BLEND]           = "BLEND",
      [ILO_STATE_BLEND_COLOR]     = "BLEND_COLOR",
      [ILO_STATE_FB]              = "FB",
      [ILO_STATE_SAMPLER_VS]      = "SAMPLER_VS",
      [ILO_STATE_SAMPLER_GS]      = "SAMPLER_GS",
      [ILO_STATE_SAMPLER_FS]      = "SAMPLER_FS",
      [ILO_STATE_SAMPLER_CS]      = "SAMPLER_CS",
      [ILO_STATE_VIEW_VS]         = "VIEW_VS",
      [ILO_STATE_VIEW_GS]         = "VIEW_GS",
      [ILO_STATE_VIEW_FS]         = "VIEW_FS",
      [ILO_STATE_VIEW_CS]         = "VIEW_CS",
      [ILO_STATE_CBUF]            = "CBUF",
      [ILO_STATE_RESOURCE]        = "RESOURCE",
      [ILO_STATE_CS]              = "CS",
      [ILO_STATE_CS_RESOURCE]     = "CS_RESOURCE",
      [ILO_STATE_GLOBAL_BINDING]  = "GLOBAL_BINDING",
   };
   uint32_t dirty = vec->dirty;

   if (!dirty) {
      ilo_printf("no state is dirty\n");
      return;
   }

   dirty &= (1U << ILO_STATE_COUNT) - 1;

   ilo_printf("%2d states are dirty:", util_bitcount(dirty));
   while (dirty) {
      const enum ilo_state state = u_bit_scan(&dirty);
      ilo_printf(" %s", state_names[state]);
   }
   ilo_printf("\n");
}