Welcome to mirror list, hosted at ThFree Co, Russian Federation.

draw_manager.c « intern « draw « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 38a767b7e348239c3641e79fd66e1b3a3414f538 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
/*
 * Copyright 2016, Blender Foundation.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * Contributor(s): Blender Institute
 *
 */

/** \file blender/draw/intern/draw_manager.c
 *  \ingroup draw
 */

#include <stdio.h>

#include "BLI_dynstr.h"
#include "BLI_listbase.h"
#include "BLI_rect.h"
#include "BLI_string.h"

#include "BIF_glutil.h"

#include "BKE_global.h"
#include "BKE_mesh.h"
#include "BKE_object.h"
#include "BKE_pbvh.h"
#include "BKE_paint.h"

#include "BLT_translation.h"
#include "BLF_api.h"

#include "DRW_engine.h"
#include "DRW_render.h"

#include "DNA_camera_types.h"
#include "DNA_view3d_types.h"
#include "DNA_screen_types.h"
#include "DNA_mesh_types.h"
#include "DNA_meshdata_types.h"

#include "ED_space_api.h"
#include "ED_screen.h"

#include "intern/gpu_codegen.h"
#include "GPU_batch.h"
#include "GPU_draw.h"
#include "GPU_extensions.h"
#include "GPU_framebuffer.h"
#include "GPU_immediate.h"
#include "GPU_lamp.h"
#include "GPU_material.h"
#include "GPU_shader.h"
#include "GPU_texture.h"
#include "GPU_uniformbuffer.h"
#include "GPU_viewport.h"
#include "GPU_matrix.h"

#include "IMB_colormanagement.h"

#include "RE_engine.h"

#include "UI_interface.h"
#include "UI_resources.h"

#include "WM_api.h"
#include "WM_types.h"

#include "draw_manager_text.h"
#include "draw_manager_profiling.h"

/* only for callbacks */
#include "draw_cache_impl.h"

#include "draw_mode_engines.h"
#include "engines/clay/clay_engine.h"
#include "engines/eevee/eevee_engine.h"
#include "engines/basic/basic_engine.h"
#include "engines/external/external_engine.h"

#include "DEG_depsgraph.h"
#include "DEG_depsgraph_query.h"

/* -------------------------------------------------------------------- */
/** \name Local Features
 * \{ */

#define USE_PROFILE

#ifdef USE_PROFILE
#include "PIL_time.h"

#define PROFILE_TIMER_FALLOFF 0.1

#define PROFILE_START(time_start) \
	double time_start = PIL_check_seconds_timer();

#define PROFILE_END_ACCUM(time_accum, time_start) { \
	time_accum += (PIL_check_seconds_timer() - time_start) * 1e3; \
} ((void)0)

/* exp average */
#define PROFILE_END_UPDATE(time_update, time_start) { \
	double _time_delta = (PIL_check_seconds_timer() - time_start) * 1e3; \
	time_update = (time_update * (1.0 - PROFILE_TIMER_FALLOFF)) + \
	              (_time_delta * PROFILE_TIMER_FALLOFF); \
} ((void)0)

#else

#define PROFILE_START(time_start) ((void)0)
#define PROFILE_END_ACCUM(time_accum, time_start) ((void)0)
#define PROFILE_END_UPDATE(time_update, time_start) ((void)0)

#endif  /* USE_PROFILE */


/* Use draw manager to call GPU_select, see: DRW_draw_select_loop */
#define USE_GPU_SELECT

/* Use BLI_memiter */
#define USE_MEM_ITER

#ifdef USE_MEM_ITER
#include "BLI_memiter.h"
#endif

#ifdef USE_GPU_SELECT
#  include "ED_view3d.h"
#  include "ED_armature.h"
#  include "GPU_select.h"
#endif

/** \} */


#define MAX_ATTRIB_NAME 32
#define MAX_PASS_NAME 32
#define MAX_CLIP_PLANES 6 /* GL_MAX_CLIP_PLANES is at least 6 */

extern char datatoc_gpu_shader_2D_vert_glsl[];
extern char datatoc_gpu_shader_3D_vert_glsl[];
extern char datatoc_gpu_shader_fullscreen_vert_glsl[];

/* Prototypes. */
static void DRW_engines_enable_external(void);

/* Structures */
typedef enum {
	DRW_UNIFORM_BOOL,
	DRW_UNIFORM_SHORT_TO_INT,
	DRW_UNIFORM_SHORT_TO_FLOAT,
	DRW_UNIFORM_INT,
	DRW_UNIFORM_FLOAT,
	DRW_UNIFORM_TEXTURE,
	DRW_UNIFORM_BUFFER,
	DRW_UNIFORM_MAT3,
	DRW_UNIFORM_MAT4,
	DRW_UNIFORM_BLOCK
} DRWUniformType;

typedef enum {
	DRW_ATTRIB_INT,
	DRW_ATTRIB_FLOAT,
} DRWAttribType;

struct DRWUniform {
	struct DRWUniform *next, *prev;
	DRWUniformType type;
	int location;
	int length;
	int arraysize;
	const void *value;
};

typedef struct DRWAttrib {
	struct DRWAttrib *next, *prev;
	char name[MAX_ATTRIB_NAME];
	int location;
	int format_id;
	int size; /* number of component */
	int type;
} DRWAttrib;

struct DRWInterface {
	ListBase uniforms;   /* DRWUniform */
	ListBase attribs;    /* DRWAttrib */
	int attribs_count;
	int attribs_stride;
	int attribs_size[16];
	int attribs_loc[16];
	/* matrices locations */
	int model;
	int modelinverse;
	int modelview;
	int modelviewinverse;
	int projection;
	int projectioninverse;
	int view;
	int viewinverse;
	int modelviewprojection;
	int viewprojection;
	int viewprojectioninverse;
	int normal;
	int worldnormal;
	int camtexfac;
	int orcotexfac;
	int eye;
	int clipplanes;
	/* Dynamic batch */
	Gwn_Batch *instance_batch; /* contains instances attributes */
	GLuint instance_vbo; /* same as instance_batch but generated from DRWCalls */
	int instance_count;
	Gwn_VertFormat vbo_format;
};

struct DRWPass {
	ListBase shgroups; /* DRWShadingGroup */
	DRWState state;
	char name[MAX_PASS_NAME];
};

typedef struct DRWCallHeader {
#ifndef USE_MEM_ITER
	void *next, *prev;
#endif

#ifdef USE_GPU_SELECT
	int select_id;
#endif
	uchar type;
} DRWCallHeader;

typedef struct DRWCall {
	DRWCallHeader head;

	float obmat[4][4];
	Gwn_Batch *geometry;

	Object *ob; /* Optional */
	ID *ob_data; /* Optional. */
} DRWCall;

typedef struct DRWCallGenerate {
	DRWCallHeader head;

	float obmat[4][4];

	DRWCallGenerateFn *geometry_fn;
	void *user_data;
} DRWCallGenerate;

typedef struct DRWCallDynamic {
	DRWCallHeader head;

	const void *data[];
} DRWCallDynamic;

struct DRWShadingGroup {
	struct DRWShadingGroup *next, *prev;

	GPUShader *shader;               /* Shader to bind */
	DRWInterface *interface;         /* Uniforms pointers */

	/* DRWCall or DRWCallDynamic depending of type */
#ifdef USE_MEM_ITER
	BLI_memiter *calls;
#else
	ListBase calls;
#endif

	DRWState state_extra;            /* State changes for this batch only (or'd with the pass's state) */
	DRWState state_extra_disable;    /* State changes for this batch only (and'd with the pass's state) */
	int type;

	ID *instance_data;         /* Object->data to instance */
	Gwn_Batch *instance_geom;  /* Geometry to instance */
	Gwn_Batch *batch_geom;     /* Result of call batching */

#ifdef USE_GPU_SELECT
	/* backlink to pass we're in */
	DRWPass *pass_parent;
#endif
};

/* Used by DRWShadingGroup.type */
enum {
	DRW_SHG_NORMAL,
	DRW_SHG_POINT_BATCH,
	DRW_SHG_LINE_BATCH,
	DRW_SHG_TRIANGLE_BATCH,
	DRW_SHG_INSTANCE,
};

/* Used by DRWCall.type */
enum {
	/* A single batch */
	DRW_CALL_SINGLE,
	/* Uses a callback to draw with any number of batches. */
	DRW_CALL_GENERATE,
	/* Arbitrary number of multiple args. */
	DRW_CALL_DYNAMIC,
};

/* only 16 bits long */
enum {
	STENCIL_SELECT          = (1 << 0),
	STENCIL_ACTIVE          = (1 << 1),
};

/** Render State: No persistent data between draw calls. */
static struct DRWGlobalState {
	/* Rendering state */
	GPUShader *shader;

	/* Managed by `DRW_state_set`, `DRW_state_reset` */
	DRWState state;

	/* Per viewport */
	GPUViewport *viewport;
	struct GPUFrameBuffer *default_framebuffer;
	float size[2];
	float screenvecs[2][3];
	float pixsize;

	GLenum backface, frontface;

	/* Clip planes */
	int num_clip_planes;
	float clip_planes_eq[MAX_CLIP_PLANES][4];

	struct {
		unsigned int is_select : 1;
		unsigned int is_depth : 1;
		unsigned int is_image_render : 1;
		unsigned int is_scene_render : 1;
	} options;

	/* Current rendering context */
	DRWContextState draw_ctx;

	/* Convenience pointer to text_store owned by the viewport */
	struct DRWTextStore **text_store_p;

	ListBase enabled_engines; /* RenderEngineType */

	/* Profiling */
	double cache_time;
} DST = {NULL};

/** GPU Resource State: Memory storage between drawing. */
static struct DRWResourceState {
	GPUTexture **bound_texs;

	bool *bound_tex_slots;

	int bind_tex_inc;
	int bind_ubo_inc;
} RST = {NULL};

static struct DRWMatrixOveride {
	float mat[6][4][4];
	bool override[6];
} viewport_matrix_override = {{{{0}}}};

ListBase DRW_engines = {NULL, NULL};

#ifdef USE_GPU_SELECT
static unsigned int g_DRW_select_id = (unsigned int)-1;

void DRW_select_load_id(unsigned int id)
{
	BLI_assert(G.f & G_PICKSEL);
	g_DRW_select_id = id;
}
#endif


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

/** \name Textures (DRW_texture)
 * \{ */

static void drw_texture_get_format(
        DRWTextureFormat format,
        GPUTextureFormat *r_data_type, int *r_channels)
{
	switch (format) {
		case DRW_TEX_RGBA_8: *r_data_type = GPU_RGBA8; break;
		case DRW_TEX_RGBA_16: *r_data_type = GPU_RGBA16F; break;
		case DRW_TEX_RGB_16: *r_data_type = GPU_RGB16F; break;
		case DRW_TEX_RGB_11_11_10: *r_data_type = GPU_R11F_G11F_B10F; break;
		case DRW_TEX_RG_8: *r_data_type = GPU_RG8; break;
		case DRW_TEX_RG_16: *r_data_type = GPU_RG16F; break;
		case DRW_TEX_RG_32: *r_data_type = GPU_RG32F; break;
		case DRW_TEX_R_8: *r_data_type = GPU_R8; break;
		case DRW_TEX_R_16: *r_data_type = GPU_R16F; break;
		case DRW_TEX_R_32: *r_data_type = GPU_R32F; break;
#if 0
		case DRW_TEX_RGBA_32: *r_data_type = GPU_RGBA32F; break;
		case DRW_TEX_RGB_8: *r_data_type = GPU_RGB8; break;
		case DRW_TEX_RGB_32: *r_data_type = GPU_RGB32F; break;
#endif
		case DRW_TEX_DEPTH_16: *r_data_type = GPU_DEPTH_COMPONENT16; break;
		case DRW_TEX_DEPTH_24: *r_data_type = GPU_DEPTH_COMPONENT24; break;
		case DRW_TEX_DEPTH_32: *r_data_type = GPU_DEPTH_COMPONENT32F; break;
		default :
			/* file type not supported you must uncomment it from above */
			BLI_assert(false);
			break;
	}

	switch (format) {
		case DRW_TEX_RGBA_8:
		case DRW_TEX_RGBA_16:
		case DRW_TEX_RGBA_32:
			*r_channels = 4;
			break;
		case DRW_TEX_RGB_8:
		case DRW_TEX_RGB_16:
		case DRW_TEX_RGB_32:
		case DRW_TEX_RGB_11_11_10:
			*r_channels = 3;
			break;
		case DRW_TEX_RG_8:
		case DRW_TEX_RG_16:
		case DRW_TEX_RG_32:
			*r_channels = 2;
			break;
		default:
			*r_channels = 1;
			break;
	}
}

static void drw_texture_set_parameters(GPUTexture *tex, DRWTextureFlag flags)
{
	GPU_texture_bind(tex, 0);
	if (flags & DRW_TEX_MIPMAP) {
		GPU_texture_mipmap_mode(tex, true, flags & DRW_TEX_FILTER);
		DRW_texture_generate_mipmaps(tex);
	}
	else {
		GPU_texture_filter_mode(tex, flags & DRW_TEX_FILTER);
	}
	GPU_texture_wrap_mode(tex, flags & DRW_TEX_WRAP);
	GPU_texture_compare_mode(tex, flags & DRW_TEX_COMPARE);
	GPU_texture_unbind(tex);
}

GPUTexture *DRW_texture_create_1D(int w, DRWTextureFormat format, DRWTextureFlag flags, const float *fpixels)
{
	GPUTexture *tex;
	GPUTextureFormat data_type;
	int channels;

	drw_texture_get_format(format, &data_type, &channels);
	tex = GPU_texture_create_1D_custom(w, channels, data_type, fpixels, NULL);
	drw_texture_set_parameters(tex, flags);

	return tex;
}

GPUTexture *DRW_texture_create_2D(int w, int h, DRWTextureFormat format, DRWTextureFlag flags, const float *fpixels)
{
	GPUTexture *tex;
	GPUTextureFormat data_type;
	int channels;

	drw_texture_get_format(format, &data_type, &channels);
	tex = GPU_texture_create_2D_custom(w, h, channels, data_type, fpixels, NULL);
	drw_texture_set_parameters(tex, flags);

	return tex;
}

GPUTexture *DRW_texture_create_2D_array(
        int w, int h, int d, DRWTextureFormat format, DRWTextureFlag flags, const float *fpixels)
{
	GPUTexture *tex;
	GPUTextureFormat data_type;
	int channels;

	drw_texture_get_format(format, &data_type, &channels);
	tex = GPU_texture_create_2D_array_custom(w, h, d, channels, data_type, fpixels, NULL);
	drw_texture_set_parameters(tex, flags);

	return tex;
}

GPUTexture *DRW_texture_create_cube(int w, DRWTextureFormat format, DRWTextureFlag flags, const float *fpixels)
{
	GPUTexture *tex;
	GPUTextureFormat data_type;
	int channels;

	drw_texture_get_format(format, &data_type, &channels);
	tex = GPU_texture_create_cube_custom(w, channels, data_type, fpixels, NULL);
	drw_texture_set_parameters(tex, flags);

	return tex;
}

void DRW_texture_generate_mipmaps(GPUTexture *tex)
{
	GPU_texture_bind(tex, 0);
	GPU_texture_generate_mipmap(tex);
	GPU_texture_unbind(tex);
}

void DRW_texture_update(GPUTexture *tex, const float *pixels)
{
	GPU_texture_update(tex, pixels);
}

void DRW_texture_free(GPUTexture *tex)
{
	GPU_texture_free(tex);
}

/** \} */


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

/** \name Uniform Buffer Object (DRW_uniformbuffer)
 * \{ */

GPUUniformBuffer *DRW_uniformbuffer_create(int size, const void *data)
{
	return GPU_uniformbuffer_create(size, data, NULL);
}

void DRW_uniformbuffer_update(GPUUniformBuffer *ubo, const void *data)
{
	GPU_uniformbuffer_update(ubo, data);
}

void DRW_uniformbuffer_free(GPUUniformBuffer *ubo)
{
	GPU_uniformbuffer_free(ubo);
}

/** \} */


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

/** \name Shaders (DRW_shader)
 * \{ */

GPUShader *DRW_shader_create(const char *vert, const char *geom, const char *frag, const char *defines)
{
	return GPU_shader_create(vert, frag, geom, NULL, defines);
}

GPUShader *DRW_shader_create_with_lib(
        const char *vert, const char *geom, const char *frag, const char *lib, const char *defines)
{
	GPUShader *sh;
	char *vert_with_lib = NULL;
	char *frag_with_lib = NULL;
	char *geom_with_lib = NULL;

	DynStr *ds_vert = BLI_dynstr_new();
	BLI_dynstr_append(ds_vert, lib);
	BLI_dynstr_append(ds_vert, vert);
	vert_with_lib = BLI_dynstr_get_cstring(ds_vert);
	BLI_dynstr_free(ds_vert);

	DynStr *ds_frag = BLI_dynstr_new();
	BLI_dynstr_append(ds_frag, lib);
	BLI_dynstr_append(ds_frag, frag);
	frag_with_lib = BLI_dynstr_get_cstring(ds_frag);
	BLI_dynstr_free(ds_frag);

	if (geom) {
		DynStr *ds_geom = BLI_dynstr_new();
		BLI_dynstr_append(ds_geom, lib);
		BLI_dynstr_append(ds_geom, geom);
		geom_with_lib = BLI_dynstr_get_cstring(ds_geom);
		BLI_dynstr_free(ds_geom);
	}

	sh = GPU_shader_create(vert_with_lib, frag_with_lib, geom_with_lib, NULL, defines);

	MEM_freeN(vert_with_lib);
	MEM_freeN(frag_with_lib);
	if (geom) {
		MEM_freeN(geom_with_lib);
	}

	return sh;
}

GPUShader *DRW_shader_create_2D(const char *frag, const char *defines)
{
	return GPU_shader_create(datatoc_gpu_shader_2D_vert_glsl, frag, NULL, NULL, defines);
}

GPUShader *DRW_shader_create_3D(const char *frag, const char *defines)
{
	return GPU_shader_create(datatoc_gpu_shader_3D_vert_glsl, frag, NULL, NULL, defines);
}

GPUShader *DRW_shader_create_fullscreen(const char *frag, const char *defines)
{
	return GPU_shader_create(datatoc_gpu_shader_fullscreen_vert_glsl, frag, NULL, NULL, defines);
}

GPUShader *DRW_shader_create_3D_depth_only(void)
{
	return GPU_shader_get_builtin_shader(GPU_SHADER_3D_DEPTH_ONLY);
}

void DRW_shader_free(GPUShader *shader)
{
	GPU_shader_free(shader);
}

/** \} */


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

/** \name Interface (DRW_interface)
 * \{ */

static DRWInterface *DRW_interface_create(GPUShader *shader)
{
	DRWInterface *interface = MEM_mallocN(sizeof(DRWInterface), "DRWInterface");

	interface->model = GPU_shader_get_builtin_uniform(shader, GWN_UNIFORM_MODEL);
	interface->modelinverse = GPU_shader_get_builtin_uniform(shader, GWN_UNIFORM_MODEL_INV);
	interface->modelview = GPU_shader_get_builtin_uniform(shader, GWN_UNIFORM_MODELVIEW);
	interface->modelviewinverse = GPU_shader_get_builtin_uniform(shader, GWN_UNIFORM_MODELVIEW_INV);
	interface->projection = GPU_shader_get_builtin_uniform(shader, GWN_UNIFORM_PROJECTION);
	interface->projectioninverse = GPU_shader_get_builtin_uniform(shader, GWN_UNIFORM_PROJECTION_INV);
	interface->view = GPU_shader_get_builtin_uniform(shader, GWN_UNIFORM_VIEW);
	interface->viewinverse = GPU_shader_get_builtin_uniform(shader, GWN_UNIFORM_VIEW_INV);
	interface->viewprojection = GPU_shader_get_builtin_uniform(shader, GWN_UNIFORM_VIEWPROJECTION);
	interface->viewprojectioninverse = GPU_shader_get_builtin_uniform(shader, GWN_UNIFORM_VIEWPROJECTION_INV);
	interface->modelviewprojection = GPU_shader_get_builtin_uniform(shader, GWN_UNIFORM_MVP);
	interface->normal = GPU_shader_get_builtin_uniform(shader, GWN_UNIFORM_NORMAL);
	interface->worldnormal = GPU_shader_get_builtin_uniform(shader, GWN_UNIFORM_WORLDNORMAL);
	interface->camtexfac = GPU_shader_get_builtin_uniform(shader, GWN_UNIFORM_CAMERATEXCO);
	interface->orcotexfac = GPU_shader_get_builtin_uniform(shader, GWN_UNIFORM_ORCO);
	interface->clipplanes = GPU_shader_get_builtin_uniform(shader, GWN_UNIFORM_CLIPPLANES);
	interface->eye = GPU_shader_get_builtin_uniform(shader, GWN_UNIFORM_EYE);
	interface->instance_count = 0;
	interface->attribs_count = 0;
	interface->attribs_stride = 0;
	interface->instance_vbo = 0;
	interface->instance_batch = NULL;

	memset(&interface->vbo_format, 0, sizeof(Gwn_VertFormat));

	BLI_listbase_clear(&interface->uniforms);
	BLI_listbase_clear(&interface->attribs);

	return interface;
}

#ifdef USE_GPU_SELECT
static DRWInterface *DRW_interface_duplicate(DRWInterface *interface_src)
{
	DRWInterface *interface_dst = MEM_dupallocN(interface_src);
	BLI_duplicatelist(&interface_dst->uniforms, &interface_src->uniforms);
	BLI_duplicatelist(&interface_dst->attribs, &interface_src->attribs);
	return interface_dst;
}
#endif

static void DRW_interface_uniform(DRWShadingGroup *shgroup, const char *name,
                                  DRWUniformType type, const void *value, int length, int arraysize)
{
	int location;
	if (type == DRW_UNIFORM_BLOCK) {
		location = GPU_shader_get_uniform_block(shgroup->shader, name);
	}
	else {
		location = GPU_shader_get_uniform(shgroup->shader, name);
	}

	if (location == -1) {
		if (G.debug & G_DEBUG)
			fprintf(stderr, "Uniform '%s' not found!\n", name);
		/* Nice to enable eventually, for now eevee uses uniforms that might not exist. */
		// BLI_assert(0);
		return;
	}

	DRWUniform *uni = MEM_mallocN(sizeof(DRWUniform), "DRWUniform");

	BLI_assert(arraysize > 0);

	uni->location = location;
	uni->type = type;
	uni->value = value;
	uni->length = length;
	uni->arraysize = arraysize;

	BLI_addtail(&shgroup->interface->uniforms, uni);
}

static void DRW_interface_attrib(DRWShadingGroup *shgroup, const char *name, DRWAttribType type, int size, bool dummy)
{
	DRWAttrib *attrib = MEM_mallocN(sizeof(DRWAttrib), "DRWAttrib");
	GLuint program = GPU_shader_get_program(shgroup->shader);

	attrib->location = glGetAttribLocation(program, name);
	attrib->type = type;
	attrib->size = size;

/* Adding attribute even if not found for now (to keep memory alignment).
 * Should ideally take vertex format automatically from batch eventually */
#if 0
	if (attrib->location == -1 && !dummy) {
		if (G.debug & G_DEBUG)
			fprintf(stderr, "Attribute '%s' not found!\n", name);
		BLI_assert(0);
		MEM_freeN(attrib);
		return;
	}
#else
	UNUSED_VARS(dummy);
#endif

	BLI_assert(BLI_strnlen(name, 32) < 32);
	BLI_strncpy(attrib->name, name, 32);

	shgroup->interface->attribs_count += 1;

	BLI_addtail(&shgroup->interface->attribs, attrib);
}

/** \} */


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

/** \name Shading Group (DRW_shgroup)
 * \{ */

DRWShadingGroup *DRW_shgroup_create(struct GPUShader *shader, DRWPass *pass)
{
	DRWShadingGroup *shgroup = MEM_mallocN(sizeof(DRWShadingGroup), "DRWShadingGroup");
	BLI_addtail(&pass->shgroups, shgroup);

	shgroup->type = DRW_SHG_NORMAL;
	shgroup->shader = shader;
	shgroup->interface = DRW_interface_create(shader);
	shgroup->state_extra = 0;
	shgroup->state_extra_disable = ~0x0;
	shgroup->batch_geom = NULL;
	shgroup->instance_geom = NULL;
	shgroup->instance_data = NULL;

#ifdef USE_MEM_ITER
	shgroup->calls = BLI_memiter_create(BLI_MEMITER_DEFAULT_SIZE);
#else
	BLI_listbase_clear(&shgroup->calls);
#endif

#ifdef USE_GPU_SELECT
	shgroup->pass_parent = pass;
#endif

	return shgroup;
}

DRWShadingGroup *DRW_shgroup_material_create(struct GPUMaterial *material, DRWPass *pass)
{
	double time = 0.0; /* TODO make time variable */

	/* TODO : Ideally we should not convert. But since the whole codegen
	 * is relying on GPUPass we keep it as is for now. */
	GPUPass *gpupass = GPU_material_get_pass(material);

	if (!gpupass) {
		/* Shader compilation error */
		return NULL;
	}

	struct GPUShader *shader = GPU_pass_shader(gpupass);

	DRWShadingGroup *grp = DRW_shgroup_create(shader, pass);

	/* Converting dynamic GPUInput to DRWUniform */
	ListBase *inputs = &gpupass->inputs;

	for (GPUInput *input = inputs->first; input; input = input->next) {
		/* Textures */
		if (input->ima) {
			GPUTexture *tex = GPU_texture_from_blender(
			        input->ima, input->iuser, input->textarget, input->image_isdata, time, 1);

			if (input->bindtex) {
				DRW_shgroup_uniform_texture(grp, input->shadername, tex);
			}
		}
		/* Color Ramps */
		else if (input->tex) {
			DRW_shgroup_uniform_texture(grp, input->shadername, input->tex);
		}
		/* Floats */
		else {
			switch (input->type) {
				case GPU_FLOAT:
					DRW_shgroup_uniform_float(grp, input->shadername, (float *)input->dynamicvec, 1);
					break;
				case GPU_VEC2:
					DRW_shgroup_uniform_vec2(grp, input->shadername, (float *)input->dynamicvec, 1);
					break;
				case GPU_VEC3:
					DRW_shgroup_uniform_vec3(grp, input->shadername, (float *)input->dynamicvec, 1);
					break;
				case GPU_VEC4:
					DRW_shgroup_uniform_vec4(grp, input->shadername, (float *)input->dynamicvec, 1);
					break;
				case GPU_MAT3:
					DRW_shgroup_uniform_mat3(grp, input->shadername, (float *)input->dynamicvec);
					break;
				case GPU_MAT4:
					DRW_shgroup_uniform_mat4(grp, input->shadername, (float *)input->dynamicvec);
					break;
				default:
					break;
			}
		}
	}

	GPUUniformBuffer *ubo = GPU_material_get_uniform_buffer(material);
	if (ubo != NULL) {
		DRW_shgroup_uniform_block(grp, GPU_UBO_BLOCK_NAME, ubo);
	}

	return grp;
}

DRWShadingGroup *DRW_shgroup_material_instance_create(
        struct GPUMaterial *material, DRWPass *pass, Gwn_Batch *geom, Object *ob)
{
	DRWShadingGroup *shgroup = DRW_shgroup_material_create(material, pass);

	if (shgroup) {
		shgroup->type = DRW_SHG_INSTANCE;
		shgroup->instance_geom = geom;
		shgroup->instance_data = ob->data;
	}

	return shgroup;
}

DRWShadingGroup *DRW_shgroup_instance_create(struct GPUShader *shader, DRWPass *pass, Gwn_Batch *geom)
{
	DRWShadingGroup *shgroup = DRW_shgroup_create(shader, pass);

	shgroup->type = DRW_SHG_INSTANCE;
	shgroup->instance_geom = geom;

	return shgroup;
}

DRWShadingGroup *DRW_shgroup_point_batch_create(struct GPUShader *shader, DRWPass *pass)
{
	DRWShadingGroup *shgroup = DRW_shgroup_create(shader, pass);

	shgroup->type = DRW_SHG_POINT_BATCH;
	DRW_shgroup_attrib_float(shgroup, "pos", 3);

	return shgroup;
}

DRWShadingGroup *DRW_shgroup_line_batch_create(struct GPUShader *shader, DRWPass *pass)
{
	DRWShadingGroup *shgroup = DRW_shgroup_create(shader, pass);

	shgroup->type = DRW_SHG_LINE_BATCH;
	DRW_shgroup_attrib_float(shgroup, "pos", 3);

	return shgroup;
}

/* Very special batch. Use this if you position
 * your vertices with the vertex shader
 * and dont need any VBO attrib */
DRWShadingGroup *DRW_shgroup_empty_tri_batch_create(struct GPUShader *shader, DRWPass *pass, int size)
{
	DRWShadingGroup *shgroup = DRW_shgroup_create(shader, pass);

	shgroup->type = DRW_SHG_TRIANGLE_BATCH;
	shgroup->interface->instance_count = size * 3;
	DRW_interface_attrib(shgroup, "dummy", DRW_ATTRIB_FLOAT, 1, true);

	return shgroup;
}

void DRW_shgroup_free(struct DRWShadingGroup *shgroup)
{
#ifdef USE_MEM_ITER
	BLI_memiter_destroy(shgroup->calls);
#else
	BLI_freelistN(&shgroup->calls);
#endif

	BLI_freelistN(&shgroup->interface->uniforms);
	BLI_freelistN(&shgroup->interface->attribs);

	if (shgroup->interface->instance_vbo &&
	    (shgroup->interface->instance_batch == 0))
	{
		glDeleteBuffers(1, &shgroup->interface->instance_vbo);
	}

	MEM_freeN(shgroup->interface);

	GWN_BATCH_DISCARD_SAFE(shgroup->batch_geom);
}

void DRW_shgroup_instance_batch(DRWShadingGroup *shgroup, struct Gwn_Batch *instances)
{
	BLI_assert(shgroup->type == DRW_SHG_INSTANCE);
	BLI_assert(shgroup->interface->instance_batch == NULL);

	shgroup->interface->instance_batch = instances;
}

void DRW_shgroup_call_add(DRWShadingGroup *shgroup, Gwn_Batch *geom, float (*obmat)[4])
{
	BLI_assert(geom != NULL);

	DRWCall *call;

#ifdef USE_MEM_ITER
	call = BLI_memiter_calloc(shgroup->calls, sizeof(DRWCall));
#else
	call = MEM_callocN(sizeof(DRWCall), "DRWCall");
	BLI_addtail(&shgroup->calls, call);
#endif

	call->head.type = DRW_CALL_SINGLE;
#ifdef USE_GPU_SELECT
	call->head.select_id = g_DRW_select_id;
#endif

	if (obmat != NULL) {
		copy_m4_m4(call->obmat, obmat);
	}

	call->geometry = geom;
}

void DRW_shgroup_call_object_add(DRWShadingGroup *shgroup, Gwn_Batch *geom, Object *ob)
{
	BLI_assert(geom != NULL);

	DRWCall *call;

#ifdef USE_MEM_ITER
	call = BLI_memiter_calloc(shgroup->calls, sizeof(DRWCall));
#else
	call = MEM_callocN(sizeof(DRWCall), "DRWCall");
	BLI_addtail(&shgroup->calls, call);
#endif

	call->head.type = DRW_CALL_SINGLE;
#ifdef USE_GPU_SELECT
	call->head.select_id = g_DRW_select_id;
#endif

	copy_m4_m4(call->obmat, ob->obmat);
	call->geometry = geom;
	call->ob_data = ob->data;
}

void DRW_shgroup_call_generate_add(
        DRWShadingGroup *shgroup,
        DRWCallGenerateFn *geometry_fn, void *user_data,
        float (*obmat)[4])
{
	BLI_assert(geometry_fn != NULL);

	DRWCallGenerate *call;

#ifdef USE_MEM_ITER
	call = BLI_memiter_calloc(shgroup->calls, sizeof(DRWCallGenerate));
#else
	call = MEM_callocN(sizeof(DRWCallGenerate), "DRWCallGenerate");
	BLI_addtail(&shgroup->calls, call);
#endif

	call->head.type = DRW_CALL_GENERATE;
#ifdef USE_GPU_SELECT
	call->head.select_id = g_DRW_select_id;
#endif

	if (obmat != NULL) {
		copy_m4_m4(call->obmat, obmat);
	}

	call->geometry_fn = geometry_fn;
	call->user_data = user_data;
}

static void sculpt_draw_cb(
        DRWShadingGroup *shgroup,
        void (*draw_fn)(DRWShadingGroup *shgroup, Gwn_Batch *geom),
        void *user_data)
{
	Object *ob = user_data;
	PBVH *pbvh = ob->sculpt->pbvh;

	if (pbvh) {
		BKE_pbvh_draw_cb(
		        pbvh, NULL, NULL, false,
		        (void (*)(void *, Gwn_Batch *))draw_fn, shgroup);
	}
}

void DRW_shgroup_call_sculpt_add(DRWShadingGroup *shgroup, Object *ob, float (*obmat)[4])
{
	DRW_shgroup_call_generate_add(shgroup, sculpt_draw_cb, ob, obmat);
}

void DRW_shgroup_call_dynamic_add_array(DRWShadingGroup *shgroup, const void *attr[], unsigned int attr_len)
{
	DRWInterface *interface = shgroup->interface;

#ifdef USE_GPU_SELECT
	if ((G.f & G_PICKSEL) && (interface->instance_count > 0)) {
		shgroup = MEM_dupallocN(shgroup);

#ifdef USE_MEM_ITER
		shgroup->calls = BLI_memiter_create(BLI_MEMITER_DEFAULT_SIZE);
#else
		BLI_listbase_clear(&shgroup->calls);
#endif

		shgroup->interface = interface = DRW_interface_duplicate(interface);
		interface->instance_count = 0;

		BLI_addtail(&shgroup->pass_parent->shgroups, shgroup);
	}
#endif

	unsigned int data_size = sizeof(void *) * interface->attribs_count;
	int size = sizeof(DRWCallDynamic) + data_size;

	DRWCallDynamic *call;

#ifdef USE_MEM_ITER
	call = BLI_memiter_alloc(shgroup->calls, size);
#else
	call = MEM_mallocN(size, "DRWCallDynamic");
	BLI_addtail(&shgroup->calls, call);
#endif

	memset(call, 0x0, sizeof(DRWCallDynamic));

	BLI_assert(attr_len == interface->attribs_count);
	UNUSED_VARS_NDEBUG(attr_len);

	call->head.type = DRW_CALL_DYNAMIC;
#ifdef USE_GPU_SELECT
	call->head.select_id = g_DRW_select_id;
#endif

	if (data_size != 0) {
		memcpy((void *)call->data, attr, data_size);
	}

	interface->instance_count += 1;
}

/* Used for instancing with no attributes */
void DRW_shgroup_set_instance_count(DRWShadingGroup *shgroup, int count)
{
	DRWInterface *interface = shgroup->interface;

	BLI_assert(interface->attribs_count == 0);

	interface->instance_count = count;
}

/**
 * State is added to #Pass.state while drawing.
 * Use to temporarily enable draw options.
 */
void DRW_shgroup_state_enable(DRWShadingGroup *shgroup, DRWState state)
{
	shgroup->state_extra |= state;
}

void DRW_shgroup_state_disable(DRWShadingGroup *shgroup, DRWState state)
{
	shgroup->state_extra_disable &= ~state;
}

void DRW_shgroup_attrib_float(DRWShadingGroup *shgroup, const char *name, int size)
{
	DRW_interface_attrib(shgroup, name, DRW_ATTRIB_FLOAT, size, false);
}

void DRW_shgroup_uniform_texture(DRWShadingGroup *shgroup, const char *name, const GPUTexture *tex)
{
	DRW_interface_uniform(shgroup, name, DRW_UNIFORM_TEXTURE, tex, 0, 1);
}

void DRW_shgroup_uniform_block(DRWShadingGroup *shgroup, const char *name, const GPUUniformBuffer *ubo)
{
	DRW_interface_uniform(shgroup, name, DRW_UNIFORM_BLOCK, ubo, 0, 1);
}

void DRW_shgroup_uniform_buffer(DRWShadingGroup *shgroup, const char *name, GPUTexture **tex)
{
	DRW_interface_uniform(shgroup, name, DRW_UNIFORM_BUFFER, tex, 0, 1);
}

void DRW_shgroup_uniform_bool(DRWShadingGroup *shgroup, const char *name, const bool *value, int arraysize)
{
	DRW_interface_uniform(shgroup, name, DRW_UNIFORM_BOOL, value, 1, arraysize);
}

void DRW_shgroup_uniform_float(DRWShadingGroup *shgroup, const char *name, const float *value, int arraysize)
{
	DRW_interface_uniform(shgroup, name, DRW_UNIFORM_FLOAT, value, 1, arraysize);
}

void DRW_shgroup_uniform_vec2(DRWShadingGroup *shgroup, const char *name, const float *value, int arraysize)
{
	DRW_interface_uniform(shgroup, name, DRW_UNIFORM_FLOAT, value, 2, arraysize);
}

void DRW_shgroup_uniform_vec3(DRWShadingGroup *shgroup, const char *name, const float *value, int arraysize)
{
	DRW_interface_uniform(shgroup, name, DRW_UNIFORM_FLOAT, value, 3, arraysize);
}

void DRW_shgroup_uniform_vec4(DRWShadingGroup *shgroup, const char *name, const float *value, int arraysize)
{
	DRW_interface_uniform(shgroup, name, DRW_UNIFORM_FLOAT, value, 4, arraysize);
}

void DRW_shgroup_uniform_short_to_int(DRWShadingGroup *shgroup, const char *name, const short *value, int arraysize)
{
	DRW_interface_uniform(shgroup, name, DRW_UNIFORM_SHORT_TO_INT, value, 1, arraysize);
}

void DRW_shgroup_uniform_short_to_float(DRWShadingGroup *shgroup, const char *name, const short *value, int arraysize)
{
	DRW_interface_uniform(shgroup, name, DRW_UNIFORM_SHORT_TO_FLOAT, value, 1, arraysize);
}

void DRW_shgroup_uniform_int(DRWShadingGroup *shgroup, const char *name, const int *value, int arraysize)
{
	DRW_interface_uniform(shgroup, name, DRW_UNIFORM_INT, value, 1, arraysize);
}

void DRW_shgroup_uniform_ivec2(DRWShadingGroup *shgroup, const char *name, const int *value, int arraysize)
{
	DRW_interface_uniform(shgroup, name, DRW_UNIFORM_INT, value, 2, arraysize);
}

void DRW_shgroup_uniform_ivec3(DRWShadingGroup *shgroup, const char *name, const int *value, int arraysize)
{
	DRW_interface_uniform(shgroup, name, DRW_UNIFORM_INT, value, 3, arraysize);
}

void DRW_shgroup_uniform_mat3(DRWShadingGroup *shgroup, const char *name, const float *value)
{
	DRW_interface_uniform(shgroup, name, DRW_UNIFORM_MAT3, value, 9, 1);
}

void DRW_shgroup_uniform_mat4(DRWShadingGroup *shgroup, const char *name, const float *value)
{
	DRW_interface_uniform(shgroup, name, DRW_UNIFORM_MAT4, value, 16, 1);
}

/* Creates a VBO containing OGL primitives for all DRWCallDynamic */
static void shgroup_dynamic_batch(DRWShadingGroup *shgroup)
{
	DRWInterface *interface = shgroup->interface;
	int nbr = interface->instance_count;

	Gwn_PrimType type = (shgroup->type == DRW_SHG_POINT_BATCH) ? GWN_PRIM_POINTS :
	                     (shgroup->type == DRW_SHG_TRIANGLE_BATCH) ? GWN_PRIM_TRIS : GWN_PRIM_LINES;

	if (nbr == 0)
		return;

	/* Upload Data */
	if (interface->vbo_format.attrib_ct == 0) {
		for (DRWAttrib *attrib = interface->attribs.first; attrib; attrib = attrib->next) {
			BLI_assert(attrib->size <= 4); /* matrices have no place here for now */
			if (attrib->type == DRW_ATTRIB_FLOAT) {
				attrib->format_id = GWN_vertformat_attr_add(
				        &interface->vbo_format, attrib->name, GWN_COMP_F32, attrib->size, GWN_FETCH_FLOAT);
			}
			else if (attrib->type == DRW_ATTRIB_INT) {
				attrib->format_id = GWN_vertformat_attr_add(
				        &interface->vbo_format, attrib->name, GWN_COMP_I8, attrib->size, GWN_FETCH_INT);
			}
			else {
				BLI_assert(false);
			}
		}
	}

	Gwn_VertBuf *vbo = GWN_vertbuf_create_with_format(&interface->vbo_format);
	GWN_vertbuf_data_alloc(vbo, nbr);

	int j = 0;
#ifdef USE_MEM_ITER
	BLI_memiter_handle calls_iter;
	BLI_memiter_iter_init(shgroup->calls, &calls_iter);
	for (DRWCallDynamic *call; (call = BLI_memiter_iter_step(&calls_iter)); j++)
#else
	for (DRWCallDynamic *call = shgroup->calls.first; call; call = call->head.next, j++)
#endif
	{
		int i = 0;
		for (DRWAttrib *attrib = interface->attribs.first; attrib; attrib = attrib->next, i++) {
			GWN_vertbuf_attr_set(vbo, attrib->format_id, j, call->data[i]);
		}
	}

	/* TODO make the batch dynamic instead of freeing it every times */
	if (shgroup->batch_geom)
		GWN_batch_discard(shgroup->batch_geom);

	shgroup->batch_geom = GWN_batch_create_ex(type, vbo, NULL, GWN_BATCH_OWNS_VBO);
}

static void shgroup_dynamic_instance(DRWShadingGroup *shgroup)
{
	int i = 0;
	int offset = 0;
	DRWInterface *interface = shgroup->interface;
	int buffer_size = 0;

	if (interface->instance_batch != NULL) {
		return;
	}

	/* TODO We still need this because gawain does not support Matrix attribs. */
	if (interface->instance_count == 0) {
		if (interface->instance_vbo) {
			glDeleteBuffers(1, &interface->instance_vbo);
			interface->instance_vbo = 0;
		}
		return;
	}

	/* only once */
	if (interface->attribs_stride == 0) {
		for (DRWAttrib *attrib = interface->attribs.first; attrib; attrib = attrib->next, i++) {
			BLI_assert(attrib->type == DRW_ATTRIB_FLOAT); /* Only float for now */
			interface->attribs_stride += attrib->size;
			interface->attribs_size[i] = attrib->size;
			interface->attribs_loc[i] = attrib->location;
		}
	}

	/* Gather Data */
	buffer_size = sizeof(float) * interface->attribs_stride * interface->instance_count;
	float *data = MEM_mallocN(buffer_size, "Instance VBO data");

#ifdef USE_MEM_ITER
	BLI_memiter_handle calls_iter;
	BLI_memiter_iter_init(shgroup->calls, &calls_iter);
	for (DRWCallDynamic *call; (call = BLI_memiter_iter_step(&calls_iter)); )
#else
	for (DRWCallDynamic *call = shgroup->calls.first; call; call = call->head.next)
#endif
	{
		for (int j = 0; j < interface->attribs_count; ++j) {
			memcpy(data + offset, call->data[j], sizeof(float) * interface->attribs_size[j]);
			offset += interface->attribs_size[j];
		}
	}

	/* TODO poke mike to add this to gawain */
	if (interface->instance_vbo) {
		glDeleteBuffers(1, &interface->instance_vbo);
		interface->instance_vbo = 0;
	}

	glGenBuffers(1, &interface->instance_vbo);
	glBindBuffer(GL_ARRAY_BUFFER, interface->instance_vbo);
	glBufferData(GL_ARRAY_BUFFER, buffer_size, data, GL_STATIC_DRAW);

	MEM_freeN(data);
}

static void shgroup_dynamic_batch_from_calls(DRWShadingGroup *shgroup)
{
	if ((shgroup->interface->instance_vbo || shgroup->batch_geom) &&
	    (G.debug_value == 667))
	{
		return;
	}

	if (shgroup->type == DRW_SHG_INSTANCE) {
		shgroup_dynamic_instance(shgroup);
	}
	else {
		shgroup_dynamic_batch(shgroup);
	}
}

/** \} */


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

/** \name Passes (DRW_pass)
 * \{ */

DRWPass *DRW_pass_create(const char *name, DRWState state)
{
	DRWPass *pass = MEM_callocN(sizeof(DRWPass), name);
	pass->state = state;
	BLI_strncpy(pass->name, name, MAX_PASS_NAME);

	BLI_listbase_clear(&pass->shgroups);

	return pass;
}

void DRW_pass_free(DRWPass *pass)
{
	for (DRWShadingGroup *shgroup = pass->shgroups.first; shgroup; shgroup = shgroup->next) {
		DRW_shgroup_free(shgroup);
	}

	BLI_freelistN(&pass->shgroups);
}

void DRW_pass_foreach_shgroup(DRWPass *pass, void (*callback)(void *userData, DRWShadingGroup *shgrp), void *userData)
{
	for (DRWShadingGroup *shgroup = pass->shgroups.first; shgroup; shgroup = shgroup->next) {
		callback(userData, shgroup);
	}
}

typedef struct ZSortData {
	float *axis;
	float *origin;
} ZSortData;

static int pass_shgroup_dist_sort(void *thunk, const void *a, const void *b)
{
	const ZSortData *zsortdata = (ZSortData *)thunk;
	const DRWShadingGroup *shgrp_a = (const DRWShadingGroup *)a;
	const DRWShadingGroup *shgrp_b = (const DRWShadingGroup *)b;

	const DRWCall *call_a;
	const DRWCall *call_b;

#ifdef USE_MEM_ITER
	call_a = BLI_memiter_elem_first(shgrp_a->calls);
	call_b = BLI_memiter_elem_first(shgrp_b->calls);
#else
	call_a = shgrp_a->calls.first;
	call_b = shgrp_b->calls.first;
#endif


	float tmp[3];
	sub_v3_v3v3(tmp, zsortdata->origin, call_a->obmat[3]);
	const float a_sq = dot_v3v3(zsortdata->axis, tmp);
	sub_v3_v3v3(tmp, zsortdata->origin, call_b->obmat[3]);
	const float b_sq = dot_v3v3(zsortdata->axis, tmp);

	if      (a_sq < b_sq) return  1;
	else if (a_sq > b_sq) return -1;
	else {
		/* If there is a depth prepass put it before */
		if ((shgrp_a->state_extra & DRW_STATE_WRITE_DEPTH) != 0) {
			return -1;
		}
		else if ((shgrp_b->state_extra & DRW_STATE_WRITE_DEPTH) != 0) {
			return  1;
		}
		else return  0;
	}
}

/**
 * Sort Shading groups by decreasing Z of their first draw call.
 * This is usefull for order dependant effect such as transparency.
 **/
void DRW_pass_sort_shgroup_z(DRWPass *pass)
{
	RegionView3D *rv3d = DST.draw_ctx.rv3d;

	float (*viewinv)[4];
	viewinv = (viewport_matrix_override.override[DRW_MAT_VIEWINV])
	          ? viewport_matrix_override.mat[DRW_MAT_VIEWINV] : rv3d->viewinv;

	ZSortData zsortdata = {viewinv[2], viewinv[3]};
	BLI_listbase_sort_r(&pass->shgroups, pass_shgroup_dist_sort, &zsortdata);
}

/** \} */


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

/** \name Draw (DRW_draw)
 * \{ */

static void DRW_state_set(DRWState state)
{
	if (DST.state == state) {
		return;
	}


#define CHANGED_TO(f) \
	((DST.state & (f)) ? \
		((state & (f)) ?  0 : -1) : \
		((state & (f)) ?  1 :  0))

#define CHANGED_ANY(f) \
	((DST.state & (f)) != (state & (f)))

#define CHANGED_ANY_STORE_VAR(f, enabled) \
	((DST.state & (f)) != (enabled = (state & (f))))

	/* Depth Write */
	{
		int test;
		if ((test = CHANGED_TO(DRW_STATE_WRITE_DEPTH))) {
			if (test == 1) {
				glDepthMask(GL_TRUE);
			}
			else {
				glDepthMask(GL_FALSE);
			}
		}
	}

	/* Color Write */
	{
		int test;
		if ((test = CHANGED_TO(DRW_STATE_WRITE_COLOR))) {
			if (test == 1) {
				glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
			}
			else {
				glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
			}
		}
	}

	/* Cull */
	{
		DRWState test;
		if (CHANGED_ANY_STORE_VAR(
		        DRW_STATE_CULL_BACK | DRW_STATE_CULL_FRONT,
		        test))
		{
			if (test) {
				glEnable(GL_CULL_FACE);

				if ((state & DRW_STATE_CULL_BACK) != 0) {
					glCullFace(GL_BACK);
				}
				else if ((state & DRW_STATE_CULL_FRONT) != 0) {
					glCullFace(GL_FRONT);
				}
				else {
					BLI_assert(0);
				}
			}
			else {
				glDisable(GL_CULL_FACE);
			}
		}
	}

	/* Depth Test */
	{
		DRWState test;
		if (CHANGED_ANY_STORE_VAR(
		        DRW_STATE_DEPTH_LESS | DRW_STATE_DEPTH_EQUAL | DRW_STATE_DEPTH_GREATER | DRW_STATE_DEPTH_ALWAYS,
		        test))
		{
			if (test) {
				glEnable(GL_DEPTH_TEST);

				if (state & DRW_STATE_DEPTH_LESS) {
					glDepthFunc(GL_LEQUAL);
				}
				else if (state & DRW_STATE_DEPTH_EQUAL) {
					glDepthFunc(GL_EQUAL);
				}
				else if (state & DRW_STATE_DEPTH_GREATER) {
					glDepthFunc(GL_GREATER);
				}
				else if (state & DRW_STATE_DEPTH_ALWAYS) {
					glDepthFunc(GL_ALWAYS);
				}
				else {
					BLI_assert(0);
				}
			}
			else {
				glDisable(GL_DEPTH_TEST);
			}
		}
	}

	/* Wire Width */
	{
		if (CHANGED_ANY(DRW_STATE_WIRE | DRW_STATE_WIRE_LARGE)) {
			if ((state & DRW_STATE_WIRE) != 0) {
				glLineWidth(1.0f);
			}
			else if ((state & DRW_STATE_WIRE_LARGE) != 0) {
				glLineWidth(UI_GetThemeValuef(TH_OUTLINE_WIDTH) * 2.0f);
			}
			else {
				/* do nothing */
			}
		}
	}

	/* Points Size */
	{
		int test;
		if ((test = CHANGED_TO(DRW_STATE_POINT))) {
			if (test == 1) {
				GPU_enable_program_point_size();
				glPointSize(5.0f);
			}
			else {
				GPU_disable_program_point_size();
			}
		}
	}

	/* Blending (all buffer) */
	{
		int test;
		if (CHANGED_ANY_STORE_VAR(
		        DRW_STATE_BLEND | DRW_STATE_ADDITIVE | DRW_STATE_MULTIPLY | DRW_STATE_TRANSMISSION,
		        test))
		{
			if (test) {
				glEnable(GL_BLEND);

				if ((state & DRW_STATE_BLEND) != 0) {
					glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
				}
				else if ((state & DRW_STATE_MULTIPLY) != 0) {
					glBlendFunc(GL_DST_COLOR, GL_ZERO);
				}
				else if ((state & DRW_STATE_TRANSMISSION) != 0) {
					glBlendFunc(GL_ONE, GL_SRC_ALPHA);
				}
				else if ((state & DRW_STATE_ADDITIVE) != 0) {
					glBlendFunc(GL_SRC_ALPHA, GL_ONE);
				}
				else {
					BLI_assert(0);
				}
			}
			else {
				glDisable(GL_BLEND);
			}
		}
	}

	/* Clip Planes */
	{
		int test;
		if ((test = CHANGED_TO(DRW_STATE_CLIP_PLANES))) {
			if (test == 1) {
				for (int i = 0; i < DST.num_clip_planes; ++i) {
					glEnable(GL_CLIP_DISTANCE0 + i);
				}
			}
			else {
				for (int i = 0; i < MAX_CLIP_PLANES; ++i) {
					glDisable(GL_CLIP_DISTANCE0 + i);
				}
			}
		}
	}

	/* Line Stipple */
	{
		int test;
		if (CHANGED_ANY_STORE_VAR(
		        DRW_STATE_STIPPLE_2 | DRW_STATE_STIPPLE_3 | DRW_STATE_STIPPLE_4,
		        test))
		{
			if (test) {
				if ((state & DRW_STATE_STIPPLE_2) != 0) {
					setlinestyle(2);
				}
				else if ((state & DRW_STATE_STIPPLE_3) != 0) {
					setlinestyle(3);
				}
				else if ((state & DRW_STATE_STIPPLE_4) != 0) {
					setlinestyle(4);
				}
				else {
					BLI_assert(0);
				}
			}
			else {
				setlinestyle(0);
			}
		}
	}

	/* Stencil */
	{
		DRWState test;
		if (CHANGED_ANY_STORE_VAR(
		        DRW_STATE_WRITE_STENCIL_SELECT |
		        DRW_STATE_WRITE_STENCIL_ACTIVE |
		        DRW_STATE_TEST_STENCIL_SELECT |
		        DRW_STATE_TEST_STENCIL_ACTIVE,
		        test))
		{
			if (test) {
				glEnable(GL_STENCIL_TEST);

				/* Stencil Write */
				if ((state & DRW_STATE_WRITE_STENCIL_SELECT) != 0) {
					glStencilMask(STENCIL_SELECT);
					glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
					glStencilFunc(GL_ALWAYS, 0xFF, STENCIL_SELECT);
				}
				else if ((state & DRW_STATE_WRITE_STENCIL_ACTIVE) != 0) {
					glStencilMask(STENCIL_ACTIVE);
					glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
					glStencilFunc(GL_ALWAYS, 0xFF, STENCIL_ACTIVE);
				}
				/* Stencil Test */
				else if ((state & DRW_STATE_TEST_STENCIL_SELECT) != 0) {
					glStencilMask(0x00); /* disable write */
					glStencilFunc(GL_NOTEQUAL, 0xFF, STENCIL_SELECT);
				}
				else if ((state & DRW_STATE_TEST_STENCIL_ACTIVE) != 0) {
					glStencilMask(0x00); /* disable write */
					glStencilFunc(GL_NOTEQUAL, 0xFF, STENCIL_ACTIVE);
				}
				else {
					BLI_assert(0);
				}
			}
			else {
				/* disable write & test */
				glStencilMask(0x00);
				glStencilFunc(GL_ALWAYS, 1, 0xFF);
				glDisable(GL_STENCIL_TEST);
			}
		}
	}

#undef CHANGED_TO
#undef CHANGED_ANY
#undef CHANGED_ANY_STORE_VAR

	DST.state = state;
}

typedef struct DRWBoundTexture {
	struct DRWBoundTexture *next, *prev;
	GPUTexture *tex;
} DRWBoundTexture;

static void draw_geometry_prepare(
        DRWShadingGroup *shgroup, const float (*obmat)[4], const float *texcoloc, const float *texcosize)
{
	RegionView3D *rv3d = DST.draw_ctx.rv3d;
	DRWInterface *interface = shgroup->interface;

	float mvp[4][4], mv[4][4], mi[4][4], mvi[4][4], pi[4][4], n[3][3], wn[3][3];
	float orcofacs[2][3] = {{0.0f, 0.0f, 0.0f}, {1.0f, 1.0f, 1.0f}};
	float eye[3] = { 0.0f, 0.0f, 1.0f }; /* looking into the screen */

	bool do_pi = (interface->projectioninverse != -1);
	bool do_mvp = (interface->modelviewprojection != -1);
	bool do_mi = (interface->modelinverse != -1);
	bool do_mv = (interface->modelview != -1);
	bool do_mvi = (interface->modelviewinverse != -1);
	bool do_n = (interface->normal != -1);
	bool do_wn = (interface->worldnormal != -1);
	bool do_eye = (interface->eye != -1);
	bool do_orco = (interface->orcotexfac != -1) && (texcoloc != NULL) && (texcosize != NULL);

	/* Matrix override */
	float (*persmat)[4];
	float (*persinv)[4];
	float (*viewmat)[4];
	float (*viewinv)[4];
	float (*winmat)[4];
	float (*wininv)[4];

	persmat = (viewport_matrix_override.override[DRW_MAT_PERS])
	          ? viewport_matrix_override.mat[DRW_MAT_PERS] : rv3d->persmat;
	persinv = (viewport_matrix_override.override[DRW_MAT_PERSINV])
	          ? viewport_matrix_override.mat[DRW_MAT_PERSINV] : rv3d->persinv;
	viewmat = (viewport_matrix_override.override[DRW_MAT_VIEW])
	          ? viewport_matrix_override.mat[DRW_MAT_VIEW] : rv3d->viewmat;
	viewinv = (viewport_matrix_override.override[DRW_MAT_VIEWINV])
	          ? viewport_matrix_override.mat[DRW_MAT_VIEWINV] : rv3d->viewinv;
	winmat = (viewport_matrix_override.override[DRW_MAT_WIN])
	          ? viewport_matrix_override.mat[DRW_MAT_WIN] : rv3d->winmat;
	wininv = viewport_matrix_override.mat[DRW_MAT_WININV];

	if (do_pi) {
		if (!viewport_matrix_override.override[DRW_MAT_WININV]) {
			invert_m4_m4(pi, winmat);
			wininv = pi;
		}
	}
	if (do_mi) {
		invert_m4_m4(mi, obmat);
	}
	if (do_mvp) {
		mul_m4_m4m4(mvp, persmat, obmat);
	}
	if (do_mv || do_mvi || do_n || do_eye) {
		mul_m4_m4m4(mv, viewmat, obmat);
	}
	if (do_mvi) {
		invert_m4_m4(mvi, mv);
	}
	if (do_n || do_eye) {
		copy_m3_m4(n, mv);
		invert_m3(n);
		transpose_m3(n);
	}
	if (do_wn) {
		copy_m3_m4(wn, obmat);
		invert_m3(wn);
		transpose_m3(wn);
	}
	if (do_eye) {
		/* Used by orthographic wires */
		float tmp[3][3];
		invert_m3_m3(tmp, n);
		/* set eye vector, transformed to object coords */
		mul_m3_v3(tmp, eye);
	}
	if (do_orco) {
		mul_v3_v3fl(orcofacs[1], texcosize, 2.0f);
		invert_v3(orcofacs[1]);
		sub_v3_v3v3(orcofacs[0], texcoloc, texcosize);
		negate_v3(orcofacs[0]);
		mul_v3_v3(orcofacs[0], orcofacs[1]); /* result in a nice MADD in the shader */
	}

	/* Should be really simple */
	/* step 1 : bind object dependent matrices */
	/* TODO : Some of these are not object dependant.
	 * They should be grouped inside a UBO updated once per redraw.
	 * The rest can also go into a UBO to reduce API calls. */
	GPU_shader_uniform_vector(shgroup->shader, interface->model, 16, 1, (float *)obmat);
	GPU_shader_uniform_vector(shgroup->shader, interface->modelinverse, 16, 1, (float *)mi);
	GPU_shader_uniform_vector(shgroup->shader, interface->modelviewprojection, 16, 1, (float *)mvp);
	GPU_shader_uniform_vector(shgroup->shader, interface->viewinverse, 16, 1, (float *)viewinv);
	GPU_shader_uniform_vector(shgroup->shader, interface->viewprojection, 16, 1, (float *)persmat);
	GPU_shader_uniform_vector(shgroup->shader, interface->viewprojectioninverse, 16, 1, (float *)persinv);
	GPU_shader_uniform_vector(shgroup->shader, interface->projection, 16, 1, (float *)winmat);
	GPU_shader_uniform_vector(shgroup->shader, interface->projectioninverse, 16, 1, (float *)wininv);
	GPU_shader_uniform_vector(shgroup->shader, interface->view, 16, 1, (float *)viewmat);
	GPU_shader_uniform_vector(shgroup->shader, interface->modelview, 16, 1, (float *)mv);
	GPU_shader_uniform_vector(shgroup->shader, interface->modelviewinverse, 16, 1, (float *)mvi);
	GPU_shader_uniform_vector(shgroup->shader, interface->normal, 9, 1, (float *)n);
	GPU_shader_uniform_vector(shgroup->shader, interface->worldnormal, 9, 1, (float *)wn);
	GPU_shader_uniform_vector(shgroup->shader, interface->camtexfac, 4, 1, (float *)rv3d->viewcamtexcofac);
	GPU_shader_uniform_vector(shgroup->shader, interface->orcotexfac, 3, 2, (float *)orcofacs);
	GPU_shader_uniform_vector(shgroup->shader, interface->eye, 3, 1, (float *)eye);
	GPU_shader_uniform_vector(shgroup->shader, interface->clipplanes, 4, DST.num_clip_planes, (float *)DST.clip_planes_eq);
}

static void draw_geometry_execute(DRWShadingGroup *shgroup, Gwn_Batch *geom)
{
	DRWInterface *interface = shgroup->interface;
	/* step 2 : bind vertex array & draw */
	GWN_batch_program_set(geom, GPU_shader_get_program(shgroup->shader), GPU_shader_get_interface(shgroup->shader));
	if (interface->instance_batch) {
		GWN_batch_draw_stupid_instanced_with_batch(geom, interface->instance_batch);
	}
	else if (interface->instance_vbo) {
		GWN_batch_draw_stupid_instanced(
		        geom, interface->instance_vbo, interface->instance_count, interface->attribs_count,
		        interface->attribs_stride, interface->attribs_size, interface->attribs_loc);
	}
	else {
		GWN_batch_draw_stupid(geom);
	}
	/* XXX this just tells gawain we are done with the shader.
	 * This does not unbind the shader. */
	GWN_batch_program_unset(geom);
}

static void draw_geometry(DRWShadingGroup *shgroup, Gwn_Batch *geom, const float (*obmat)[4], ID *ob_data)
{
	float *texcoloc = NULL;
	float *texcosize = NULL;

	if (ob_data != NULL) {
		switch (GS(ob_data->name)) {
			case ID_ME:
				BKE_mesh_texspace_get_reference((Mesh *)ob_data, NULL, &texcoloc, NULL, &texcosize);
				break;
			default:
				/* TODO, curve, metaball? */
				break;
		}
	}

	draw_geometry_prepare(shgroup, obmat, texcoloc, texcosize);

	draw_geometry_execute(shgroup, geom);
}

static void bind_texture(GPUTexture *tex)
{
	int bind_num = GPU_texture_bound_number(tex);
	if (bind_num == -1) {
		for (int i = 0; i < GPU_max_textures(); ++i) {
			RST.bind_tex_inc = (RST.bind_tex_inc + 1) % GPU_max_textures();
			if (RST.bound_tex_slots[RST.bind_tex_inc] == false) {
				if (RST.bound_texs[RST.bind_tex_inc] != NULL) {
					GPU_texture_unbind(RST.bound_texs[RST.bind_tex_inc]);
				}
				GPU_texture_bind(tex, RST.bind_tex_inc);
				RST.bound_texs[RST.bind_tex_inc] = tex;
				RST.bound_tex_slots[RST.bind_tex_inc] = true;
				return;
			}
		}

		printf("Not enough texture slots! Reduce number of textures used by your shader.\n");
	}
	RST.bound_tex_slots[bind_num] = true;
}

static void bind_ubo(GPUUniformBuffer *ubo)
{
	if (RST.bind_ubo_inc < GPU_max_ubo_binds()) {
		GPU_uniformbuffer_bind(ubo, RST.bind_ubo_inc);
		RST.bind_ubo_inc++;
	}
	else {
		/* This is not depending on user input.
		 * It is our responsability to make sure there enough slots. */
		BLI_assert(0 && "Not enough ubo slots! This should not happen!\n");

		/* printf so user can report bad behaviour */
		printf("Not enough ubo slots! This should not happen!\n");
	}
}

static void release_texture_slots(void)
{
	memset(RST.bound_tex_slots, 0x0, sizeof(bool) * GPU_max_textures());
}

static void release_ubo_slots(void)
{
	RST.bind_ubo_inc = 0;
}

static void draw_shgroup(DRWShadingGroup *shgroup, DRWState pass_state)
{
	BLI_assert(shgroup->shader);
	BLI_assert(shgroup->interface);

	DRWInterface *interface = shgroup->interface;
	GPUTexture *tex;
	GPUUniformBuffer *ubo;
	int val;
	float fval;

	if (DST.shader != shgroup->shader) {
		if (DST.shader) GPU_shader_unbind();
		GPU_shader_bind(shgroup->shader);
		DST.shader = shgroup->shader;
	}

	const bool is_normal = ELEM(shgroup->type, DRW_SHG_NORMAL);

	if (!is_normal) {
		shgroup_dynamic_batch_from_calls(shgroup);
	}

	release_texture_slots();
	release_ubo_slots();

	DRW_state_set((pass_state & shgroup->state_extra_disable) | shgroup->state_extra);


	/* Binding Uniform */
	/* Don't check anything, Interface should already contain the least uniform as possible */
	for (DRWUniform *uni = interface->uniforms.first; uni; uni = uni->next) {
		switch (uni->type) {
			case DRW_UNIFORM_SHORT_TO_INT:
				val = (int)*((short *)uni->value);
				GPU_shader_uniform_vector_int(
				        shgroup->shader, uni->location, uni->length, uni->arraysize, (int *)&val);
				break;
			case DRW_UNIFORM_SHORT_TO_FLOAT:
				fval = (float)*((short *)uni->value);
				GPU_shader_uniform_vector(
				        shgroup->shader, uni->location, uni->length, uni->arraysize, (float *)&fval);
				break;
			case DRW_UNIFORM_BOOL:
			case DRW_UNIFORM_INT:
				GPU_shader_uniform_vector_int(
				        shgroup->shader, uni->location, uni->length, uni->arraysize, (int *)uni->value);
				break;
			case DRW_UNIFORM_FLOAT:
			case DRW_UNIFORM_MAT3:
			case DRW_UNIFORM_MAT4:
				GPU_shader_uniform_vector(
				        shgroup->shader, uni->location, uni->length, uni->arraysize, (float *)uni->value);
				break;
			case DRW_UNIFORM_TEXTURE:
				tex = (GPUTexture *)uni->value;
				BLI_assert(tex);
				bind_texture(tex);
				GPU_shader_uniform_texture(shgroup->shader, uni->location, tex);
				break;
			case DRW_UNIFORM_BUFFER:
				if (!DRW_state_is_fbo()) {
					break;
				}
				tex = *((GPUTexture **)uni->value);
				BLI_assert(tex);
				bind_texture(tex);
				GPU_shader_uniform_texture(shgroup->shader, uni->location, tex);
				break;
			case DRW_UNIFORM_BLOCK:
				ubo = (GPUUniformBuffer *)uni->value;
				bind_ubo(ubo);
				GPU_shader_uniform_buffer(shgroup->shader, uni->location, ubo);
				break;
		}
	}

#ifdef USE_GPU_SELECT
	/* use the first item because of selection we only ever add one */
#  define GPU_SELECT_LOAD_IF_PICKSEL(_call) \
	if ((G.f & G_PICKSEL) && (_call)) { \
		GPU_select_load_id((_call)->head.select_id); \
	} ((void)0)

#ifdef USE_MEM_ITER
#  define GPU_SELECT_LOAD_IF_PICKSEL_LIST(_call_ls) \
	if (G.f & G_PICKSEL) { \
		DRWCall *call_test = BLI_memiter_elem_first(*(_call_ls)); \
		if (call_test != NULL) { \
			BLI_assert(BLI_memiter_count(*(_call_ls)) == 1); \
			GPU_select_load_id(call_test->head.select_id); \
		} \
	} ((void)0)
#else
#  define GPU_SELECT_LOAD_IF_PICKSEL_LIST(_call_ls) \
	if ((G.f & G_PICKSEL) && (_call_ls)->first) { \
		BLI_assert(BLI_listbase_is_single(_call_ls)); \
		GPU_select_load_id(((DRWCall *)(_call_ls)->first)->head.select_id); \
	} ((void)0)
#endif

#else
#  define GPU_SELECT_LOAD_IF_PICKSEL(call)
#  define GPU_SELECT_LOAD_IF_PICKSEL_LIST(call)
#endif

	/* Rendering Calls */
	if (!is_normal) {
		/* Replacing multiple calls with only one */
		float obmat[4][4];
		unit_m4(obmat);

		if (shgroup->type == DRW_SHG_INSTANCE &&
		    (interface->instance_count > 0 || interface->instance_batch != NULL))
		{
			GPU_SELECT_LOAD_IF_PICKSEL_LIST(&shgroup->calls);
			draw_geometry(shgroup, shgroup->instance_geom, obmat, shgroup->instance_data);
		}
		else {
			/* Some dynamic batch can have no geom (no call to aggregate) */
			if (shgroup->batch_geom) {
				GPU_SELECT_LOAD_IF_PICKSEL_LIST(&shgroup->calls);
				draw_geometry(shgroup, shgroup->batch_geom, obmat, NULL);
			}
		}
	}
	else {
#ifdef USE_MEM_ITER
		BLI_memiter_handle calls_iter;
		BLI_memiter_iter_init(shgroup->calls, &calls_iter);
		for (DRWCall *call; (call = BLI_memiter_iter_step(&calls_iter)); )
#else
		for (DRWCall *call = shgroup->calls.first; call; call = call->head.next)
#endif
		{
			bool neg_scale = is_negative_m4(call->obmat);

			/* Negative scale objects */
			if (neg_scale) {
				glFrontFace(DST.backface);
			}

			GPU_SELECT_LOAD_IF_PICKSEL(call);

			if (call->head.type == DRW_CALL_SINGLE) {
				draw_geometry(shgroup, call->geometry, call->obmat, call->ob_data);
			}
			else {
				BLI_assert(call->head.type == DRW_CALL_GENERATE);
				DRWCallGenerate *callgen = ((DRWCallGenerate *)call);
				draw_geometry_prepare(shgroup, callgen->obmat, NULL, NULL);
				callgen->geometry_fn(shgroup, draw_geometry_execute, callgen->user_data);
			}

			/* Reset state */
			if (neg_scale) {
				glFrontFace(DST.frontface);
			}
		}
	}

	/* TODO: remove, (currently causes alpha issue with sculpt, need to investigate) */
	DRW_state_reset();
}

static void DRW_draw_pass_ex(DRWPass *pass, DRWShadingGroup *start_group, DRWShadingGroup *end_group)
{
	/* Start fresh */
	DST.shader = NULL;

	DRW_state_set(pass->state);

	DRW_stats_query_start(pass->name);

	for (DRWShadingGroup *shgroup = start_group; shgroup; shgroup = shgroup->next) {
		draw_shgroup(shgroup, pass->state);
		/* break if upper limit */
		if (shgroup == end_group) {
			break;
		}
	}

	/* Clear Bound textures */
	for (int i = 0; i < GPU_max_textures(); i++) {
		if (RST.bound_texs[i] != NULL) {
			GPU_texture_unbind(RST.bound_texs[i]);
			RST.bound_texs[i] = NULL;
		}
	}

	if (DST.shader) {
		GPU_shader_unbind();
		DST.shader = NULL;
	}

	DRW_stats_query_end();
}

void DRW_draw_pass(DRWPass *pass)
{
	DRW_draw_pass_ex(pass, pass->shgroups.first, pass->shgroups.last);
}

/* Draw only a subset of shgroups. Used in special situations as grease pencil strokes */
void DRW_draw_pass_subset(DRWPass *pass, DRWShadingGroup *start_group, DRWShadingGroup *end_group)
{
	DRW_draw_pass_ex(pass, start_group, end_group);
}

void DRW_draw_callbacks_pre_scene(void)
{
	RegionView3D *rv3d = DST.draw_ctx.rv3d;

	gpuLoadProjectionMatrix(rv3d->winmat);
	gpuLoadMatrix(rv3d->viewmat);
}

void DRW_draw_callbacks_post_scene(void)
{
	RegionView3D *rv3d = DST.draw_ctx.rv3d;

	gpuLoadProjectionMatrix(rv3d->winmat);
	gpuLoadMatrix(rv3d->viewmat);
}

/* Reset state to not interfer with other UI drawcall */
void DRW_state_reset_ex(DRWState state)
{
	DST.state = ~state;
	DRW_state_set(state);
}

void DRW_state_reset(void)
{
	/* Reset blending function */
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

	DRW_state_reset_ex(DRW_STATE_DEFAULT);
}

/* NOTE : Make sure to reset after use! */
void DRW_state_invert_facing(void)
{
	SWAP(GLenum, DST.backface, DST.frontface);
	glFrontFace(DST.frontface);
}

/**
 * This only works if DRWPasses have been tagged with DRW_STATE_CLIP_PLANES,
 * and if the shaders have support for it (see usage of gl_ClipDistance).
 * Be sure to call DRW_state_clip_planes_reset() after you finish drawing.
 **/
void DRW_state_clip_planes_add(float plane_eq[4])
{
	copy_v4_v4(DST.clip_planes_eq[DST.num_clip_planes++], plane_eq);
}

void DRW_state_clip_planes_reset(void)
{
	DST.num_clip_planes = 0;
}

/** \} */


struct DRWTextStore *DRW_text_cache_ensure(void)
{
	BLI_assert(DST.text_store_p);
	if (*DST.text_store_p == NULL) {
		*DST.text_store_p = DRW_text_cache_create();
	}
	return *DST.text_store_p;
}


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

/** \name Settings
 * \{ */

bool DRW_object_is_renderable(Object *ob)
{
	Scene *scene = DST.draw_ctx.scene;
	Object *obedit = scene->obedit;

	if (!BKE_object_is_visible(ob)) {
		return false;
	}

	if (ob->type == OB_MESH) {
		if (ob == obedit) {
			IDProperty *props = BKE_layer_collection_engine_evaluated_get(ob, COLLECTION_MODE_EDIT, "");
			bool do_show_occlude_wire = BKE_collection_engine_property_value_get_bool(props, "show_occlude_wire");
			if (do_show_occlude_wire) {
				return false;
			}
			bool do_show_weight = BKE_collection_engine_property_value_get_bool(props, "show_weight");
			if (do_show_weight) {
				return false;
			}
		}
	}

	return true;
}

bool DRW_object_is_flat_normal(const Object *ob)
{
	if (ob->type == OB_MESH) {
		const Mesh *me = ob->data;
		if (me->mpoly && me->mpoly[0].flag & ME_SMOOTH) {
			return false;
		}
	}
	return true;
}


/**
 * Return true if the object has its own draw mode.
 * Caller must check this is active */
int DRW_object_is_mode_shade(const Object *ob)
{
	BLI_assert(ob == DST.draw_ctx.obact);
	if ((ob->mode & OB_MODE_EDIT) == 0) {
		if (ob->mode & (OB_MODE_VERTEX_PAINT | OB_MODE_WEIGHT_PAINT | OB_MODE_TEXTURE_PAINT)) {
			if ((DST.draw_ctx.v3d->flag2 & V3D_SHOW_MODE_SHADE_OVERRIDE) == 0) {
				return true;
			}
			else {
				return false;
			}
		}
	}
	return -1;
}

/** \} */


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

/** \name Framebuffers (DRW_framebuffer)
 * \{ */

static GPUTextureFormat convert_tex_format(
        int fbo_format,
        int *r_channels, bool *r_is_depth)
{
	*r_is_depth = ELEM(fbo_format, DRW_TEX_DEPTH_16, DRW_TEX_DEPTH_24);

	switch (fbo_format) {
		case DRW_TEX_R_16:     *r_channels = 1; return GPU_R16F;
		case DRW_TEX_R_32:     *r_channels = 1; return GPU_R32F;
		case DRW_TEX_RG_8:     *r_channels = 2; return GPU_RG8;
		case DRW_TEX_RG_16:    *r_channels = 2; return GPU_RG16F;
		case DRW_TEX_RG_32:    *r_channels = 2; return GPU_RG32F;
		case DRW_TEX_RGBA_8:   *r_channels = 4; return GPU_RGBA8;
		case DRW_TEX_RGBA_16:  *r_channels = 4; return GPU_RGBA16F;
		case DRW_TEX_RGBA_32:  *r_channels = 4; return GPU_RGBA32F;
		case DRW_TEX_DEPTH_16: *r_channels = 1; return GPU_DEPTH_COMPONENT16;
		case DRW_TEX_DEPTH_24: *r_channels = 1; return GPU_DEPTH_COMPONENT24;
		case DRW_TEX_DEPTH_32: *r_channels = 1; return GPU_DEPTH_COMPONENT32F;
		case DRW_TEX_RGB_11_11_10: *r_channels = 3; return GPU_R11F_G11F_B10F;
		default:
			BLI_assert(false && "Texture format unsupported as render target!");
			*r_channels = 4; return GPU_RGBA8;
	}
}

void DRW_framebuffer_init(
        struct GPUFrameBuffer **fb, void *engine_type, int width, int height,
        DRWFboTexture textures[MAX_FBO_TEX], int textures_len)
{
	BLI_assert(textures_len <= MAX_FBO_TEX);

	bool create_fb = false;
	int color_attachment = -1;

	if (!*fb) {
		*fb = GPU_framebuffer_create();
		create_fb = true;
	}

	for (int i = 0; i < textures_len; ++i) {
		int channels;
		bool is_depth;

		DRWFboTexture fbotex = textures[i];
		bool is_temp = (fbotex.flag & DRW_TEX_TEMP) != 0;

		GPUTextureFormat gpu_format = convert_tex_format(fbotex.format, &channels, &is_depth);

		if (!*fbotex.tex || is_temp) {
			/* Temp textures need to be queried each frame, others not. */
			if (is_temp) {
				*fbotex.tex = GPU_viewport_texture_pool_query(
				        DST.viewport, engine_type, width, height, channels, gpu_format);
			}
			else if (create_fb) {
				*fbotex.tex = GPU_texture_create_2D_custom(
				        width, height, channels, gpu_format, NULL, NULL);
			}
		}

		if (create_fb) {
			if (!is_depth) {
				++color_attachment;
			}
			drw_texture_set_parameters(*fbotex.tex, fbotex.flag);
			GPU_framebuffer_texture_attach(*fb, *fbotex.tex, color_attachment, 0);
		}
	}

	if (create_fb) {
		if (!GPU_framebuffer_check_valid(*fb, NULL)) {
			printf("Error invalid framebuffer\n");
		}

		/* Detach temp textures */
		for (int i = 0; i < textures_len; ++i) {
			DRWFboTexture fbotex = textures[i];

			if ((fbotex.flag & DRW_TEX_TEMP) != 0) {
				GPU_framebuffer_texture_detach(*fbotex.tex);
			}
		}

		GPU_framebuffer_bind(DST.default_framebuffer);
	}
}

void DRW_framebuffer_free(struct GPUFrameBuffer *fb)
{
	GPU_framebuffer_free(fb);
}

void DRW_framebuffer_bind(struct GPUFrameBuffer *fb)
{
	GPU_framebuffer_bind(fb);
}

void DRW_framebuffer_clear(bool color, bool depth, bool stencil, float clear_col[4], float clear_depth)
{
	if (color) {
		glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
		glClearColor(clear_col[0], clear_col[1], clear_col[2], clear_col[3]);
	}
	if (depth) {
		glDepthMask(GL_TRUE);
		glClearDepth(clear_depth);
	}
	if (stencil) {
		glStencilMask(0xFF);
	}
	glClear(((color) ? GL_COLOR_BUFFER_BIT : 0) |
	        ((depth) ? GL_DEPTH_BUFFER_BIT : 0) |
	        ((stencil) ? GL_STENCIL_BUFFER_BIT : 0));
}

void DRW_framebuffer_read_data(int x, int y, int w, int h, int channels, int slot, float *data)
{
	GLenum type;
	switch (channels) {
		case 1: type = GL_RED; break;
		case 2: type = GL_RG; break;
		case 3: type = GL_RGB; break;
		case 4: type = GL_RGBA;	break;
		default:
			BLI_assert(false && "wrong number of read channels");
			return;
	}
	glReadBuffer(GL_COLOR_ATTACHMENT0 + slot);
	glReadPixels(x, y, w, h, type, GL_FLOAT, data);
}

void DRW_framebuffer_texture_attach(struct GPUFrameBuffer *fb, GPUTexture *tex, int slot, int mip)
{
	GPU_framebuffer_texture_attach(fb, tex, slot, mip);
}

void DRW_framebuffer_texture_layer_attach(struct GPUFrameBuffer *fb, struct GPUTexture *tex, int slot, int layer, int mip)
{
	GPU_framebuffer_texture_layer_attach(fb, tex, slot, layer, mip);
}

void DRW_framebuffer_cubeface_attach(struct GPUFrameBuffer *fb, GPUTexture *tex, int slot, int face, int mip)
{
	GPU_framebuffer_texture_cubeface_attach(fb, tex, slot, face, mip);
}

void DRW_framebuffer_texture_detach(GPUTexture *tex)
{
	GPU_framebuffer_texture_detach(tex);
}

void DRW_framebuffer_blit(struct GPUFrameBuffer *fb_read, struct GPUFrameBuffer *fb_write, bool depth)
{
	GPU_framebuffer_blit(fb_read, 0, fb_write, 0, depth);
}

void DRW_framebuffer_recursive_downsample(
        struct GPUFrameBuffer *fb, struct GPUTexture *tex, int num_iter,
        void (*callback)(void *userData, int level), void *userData)
{
	GPU_framebuffer_recursive_downsample(fb, tex, num_iter, callback, userData);
}

void DRW_framebuffer_viewport_size(struct GPUFrameBuffer *UNUSED(fb_read), int x, int y, int w, int h)
{
	glViewport(x, y, w, h);
}

/* Use color management profile to draw texture to framebuffer */
void DRW_transform_to_display(GPUTexture *tex)
{
	DRW_state_set(DRW_STATE_WRITE_COLOR);

	Gwn_VertFormat *vert_format = immVertexFormat();
	unsigned int pos = GWN_vertformat_attr_add(vert_format, "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
	unsigned int texco = GWN_vertformat_attr_add(vert_format, "texCoord", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);

	const float dither = 1.0f;

	bool use_ocio = false;

	{
		Scene *scene = DST.draw_ctx.scene;
		/* View transform is already applied for offscreen, don't apply again, see: T52046 */
		ColorManagedViewSettings *view_settings =
		        (DST.options.is_image_render && !DST.options.is_scene_render) ?
		        NULL : &scene->view_settings;
		use_ocio = IMB_colormanagement_setup_glsl_draw_from_space(
		        view_settings, &scene->display_settings, NULL, dither, false);
	}

	if (!use_ocio) {
		immBindBuiltinProgram(GPU_SHADER_2D_IMAGE_LINEAR_TO_SRGB);
		immUniform1i("image", 0);
	}

	GPU_texture_bind(tex, 0); /* OCIO texture bind point is 0 */

	float mat[4][4];
	unit_m4(mat);
	immUniformMatrix4fv("ModelViewProjectionMatrix", mat);

	/* Full screen triangle */
	immBegin(GWN_PRIM_TRIS, 3);
	immAttrib2f(texco, 0.0f, 0.0f);
	immVertex2f(pos, -1.0f, -1.0f);

	immAttrib2f(texco, 2.0f, 0.0f);
	immVertex2f(pos, 3.0f, -1.0f);

	immAttrib2f(texco, 0.0f, 2.0f);
	immVertex2f(pos, -1.0f, 3.0f);
	immEnd();

	GPU_texture_unbind(tex);

	if (use_ocio) {
		IMB_colormanagement_finish_glsl_draw();
	}
	else {
		immUnbindProgram();
	}
}

/** \} */


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

/** \name Viewport (DRW_viewport)
 * \{ */

static void *DRW_viewport_engine_data_get(void *engine_type)
{
	void *data = GPU_viewport_engine_data_get(DST.viewport, engine_type);

	if (data == NULL) {
		data = GPU_viewport_engine_data_create(DST.viewport, engine_type);
	}
	return data;
}

void DRW_engine_viewport_data_size_get(
        const void *engine_type_v,
        int *r_fbl_len, int *r_txl_len, int *r_psl_len, int *r_stl_len)
{
	const DrawEngineType *engine_type = engine_type_v;

	if (r_fbl_len) {
		*r_fbl_len = engine_type->vedata_size->fbl_len;
	}
	if (r_txl_len) {
		*r_txl_len = engine_type->vedata_size->txl_len;
	}
	if (r_psl_len) {
		*r_psl_len = engine_type->vedata_size->psl_len;
	}
	if (r_stl_len) {
		*r_stl_len = engine_type->vedata_size->stl_len;
	}
}

const float *DRW_viewport_size_get(void)
{
	return &DST.size[0];
}

const float *DRW_viewport_screenvecs_get(void)
{
	return &DST.screenvecs[0][0];
}

const float *DRW_viewport_pixelsize_get(void)
{
	return &DST.pixsize;
}

/* It also stores viewport variable to an immutable place: DST
 * This is because a cache uniform only store reference
 * to its value. And we don't want to invalidate the cache
 * if this value change per viewport */
static void DRW_viewport_var_init(void)
{
	RegionView3D *rv3d = DST.draw_ctx.rv3d;

	/* Refresh DST.size */
	if (DST.viewport) {
		int size[2];
		GPU_viewport_size_get(DST.viewport, size);
		DST.size[0] = size[0];
		DST.size[1] = size[1];

		DefaultFramebufferList *fbl = (DefaultFramebufferList *)GPU_viewport_framebuffer_list_get(DST.viewport);
		DST.default_framebuffer = fbl->default_fb;
	}
	else {
		DST.size[0] = 0;
		DST.size[1] = 0;

		DST.default_framebuffer = NULL;
	}
	/* Refresh DST.screenvecs */
	copy_v3_v3(DST.screenvecs[0], rv3d->viewinv[0]);
	copy_v3_v3(DST.screenvecs[1], rv3d->viewinv[1]);
	normalize_v3(DST.screenvecs[0]);
	normalize_v3(DST.screenvecs[1]);

	/* Refresh DST.pixelsize */
	DST.pixsize = rv3d->pixsize;

	/* Reset facing */
	DST.frontface = GL_CCW;
	DST.backface = GL_CW;
	glFrontFace(DST.frontface);

	/* Alloc array of texture reference. */
	if (RST.bound_texs == NULL) {
		RST.bound_texs = MEM_callocN(sizeof(GPUTexture *) * GPU_max_textures(), "Bound GPUTexture refs");
	}
	if (RST.bound_tex_slots == NULL) {
		RST.bound_tex_slots = MEM_callocN(sizeof(bool) * GPU_max_textures(), "Bound Texture Slots");
	}

	memset(viewport_matrix_override.override, 0x0, sizeof(viewport_matrix_override.override));
}

void DRW_viewport_matrix_get(float mat[4][4], DRWViewportMatrixType type)
{
	RegionView3D *rv3d = DST.draw_ctx.rv3d;
	BLI_assert(type >= DRW_MAT_PERS && type <= DRW_MAT_WININV);

	if (viewport_matrix_override.override[type]) {
		copy_m4_m4(mat, viewport_matrix_override.mat[type]);
	}
	else {
		switch (type) {
			case DRW_MAT_PERS:
				copy_m4_m4(mat, rv3d->persmat);
				break;
			case DRW_MAT_PERSINV:
				copy_m4_m4(mat, rv3d->persinv);
				break;
			case DRW_MAT_VIEW:
				copy_m4_m4(mat, rv3d->viewmat);
				break;
			case DRW_MAT_VIEWINV:
				copy_m4_m4(mat, rv3d->viewinv);
				break;
			case DRW_MAT_WIN:
				copy_m4_m4(mat, rv3d->winmat);
				break;
			case DRW_MAT_WININV:
				invert_m4_m4(mat, rv3d->winmat);
				break;
			default:
				BLI_assert(!"Matrix type invalid");
				break;
		}
	}
}

void DRW_viewport_matrix_override_set(float mat[4][4], DRWViewportMatrixType type)
{
	copy_m4_m4(viewport_matrix_override.mat[type], mat);
	viewport_matrix_override.override[type] = true;
}

void DRW_viewport_matrix_override_unset(DRWViewportMatrixType type)
{
	viewport_matrix_override.override[type] = false;
}

bool DRW_viewport_is_persp_get(void)
{
	RegionView3D *rv3d = DST.draw_ctx.rv3d;
	return rv3d->is_persp;
}

DefaultFramebufferList *DRW_viewport_framebuffer_list_get(void)
{
	return GPU_viewport_framebuffer_list_get(DST.viewport);
}

DefaultTextureList *DRW_viewport_texture_list_get(void)
{
	return GPU_viewport_texture_list_get(DST.viewport);
}

void DRW_viewport_request_redraw(void)
{
	GPU_viewport_tag_update(DST.viewport);
}

/** \} */


/* -------------------------------------------------------------------- */
/** \name SceneLayers (DRW_scenelayer)
 * \{ */

void **DRW_scene_layer_engine_data_get(DrawEngineType *engine_type, void (*callback)(void *storage))
{
	SceneLayerEngineData *sled;

	for (sled = DST.draw_ctx.scene_layer->drawdata.first; sled; sled = sled->next) {
		if (sled->engine_type == engine_type) {
			return &sled->storage;
		}
	}

	sled = MEM_callocN(sizeof(SceneLayerEngineData), "SceneLayerEngineData");
	sled->engine_type = engine_type;
	sled->free = callback;
	BLI_addtail(&DST.draw_ctx.scene_layer->drawdata, sled);

	return &sled->storage;
}

/** \} */


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

/** \name Objects (DRW_object)
 * \{ */

void **DRW_object_engine_data_get(
        Object *ob, DrawEngineType *engine_type, void (*callback)(void *storage))
{
	ObjectEngineData *oed;

	for (oed = ob->drawdata.first; oed; oed = oed->next) {
		if (oed->engine_type == engine_type) {
			return &oed->storage;
		}
	}

	oed = MEM_callocN(sizeof(ObjectEngineData), "ObjectEngineData");
	oed->engine_type = engine_type;
	oed->free = callback;
	BLI_addtail(&ob->drawdata, oed);

	return &oed->storage;
}

/* XXX There is definitly some overlap between this and DRW_object_engine_data_get.
 * We should get rid of one of the two. */
LampEngineData *DRW_lamp_engine_data_get(Object *ob, RenderEngineType *engine_type)
{
	BLI_assert(ob->type == OB_LAMP);

	Scene *scene = DST.draw_ctx.scene;

	/* TODO Dupliobjects */
	/* TODO Should be per scenelayer */
	return GPU_lamp_engine_data_get(scene, ob, NULL, engine_type);
}

void DRW_lamp_engine_data_free(LampEngineData *led)
{
	GPU_lamp_engine_data_free(led);
}

/** \} */


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

/** \name Rendering (DRW_engines)
 * \{ */

static void DRW_engines_init(void)
{
	for (LinkData *link = DST.enabled_engines.first; link; link = link->next) {
		DrawEngineType *engine = link->data;
		ViewportEngineData *data = DRW_viewport_engine_data_get(engine);
		PROFILE_START(stime);

		if (engine->engine_init) {
			engine->engine_init(data);
		}

		PROFILE_END_UPDATE(data->init_time, stime);
	}
}

static void DRW_engines_cache_init(void)
{
	for (LinkData *link = DST.enabled_engines.first; link; link = link->next) {
		DrawEngineType *engine = link->data;
		ViewportEngineData *data = DRW_viewport_engine_data_get(engine);

		if (data->text_draw_cache) {
			DRW_text_cache_destroy(data->text_draw_cache);
			data->text_draw_cache = NULL;
		}
		if (DST.text_store_p == NULL) {
			DST.text_store_p = &data->text_draw_cache;
		}

		if (engine->cache_init) {
			engine->cache_init(data);
		}
	}
}

static void DRW_engines_cache_populate(Object *ob)
{
	for (LinkData *link = DST.enabled_engines.first; link; link = link->next) {
		DrawEngineType *engine = link->data;
		ViewportEngineData *data = DRW_viewport_engine_data_get(engine);

		if (engine->cache_populate) {
			engine->cache_populate(data, ob);
		}
	}
}

static void DRW_engines_cache_finish(void)
{
	for (LinkData *link = DST.enabled_engines.first; link; link = link->next) {
		DrawEngineType *engine = link->data;
		ViewportEngineData *data = DRW_viewport_engine_data_get(engine);

		if (engine->cache_finish) {
			engine->cache_finish(data);
		}
	}
}

static void DRW_engines_draw_background(void)
{
	for (LinkData *link = DST.enabled_engines.first; link; link = link->next) {
		DrawEngineType *engine = link->data;
		ViewportEngineData *data = DRW_viewport_engine_data_get(engine);

		if (engine->draw_background) {
			PROFILE_START(stime);

			DRW_stats_group_start(engine->idname);
			engine->draw_background(data);
			DRW_stats_group_end();

			PROFILE_END_UPDATE(data->background_time, stime);
			return;
		}
	}

	/* No draw_background found, doing default background */
	DRW_draw_background();
}

static void DRW_engines_draw_scene(void)
{
	for (LinkData *link = DST.enabled_engines.first; link; link = link->next) {
		DrawEngineType *engine = link->data;
		ViewportEngineData *data = DRW_viewport_engine_data_get(engine);
		PROFILE_START(stime);

		if (engine->draw_scene) {
			DRW_stats_group_start(engine->idname);
			engine->draw_scene(data);
			DRW_stats_group_end();
		}

		PROFILE_END_UPDATE(data->render_time, stime);
	}
}

static void DRW_engines_draw_text(void)
{
	for (LinkData *link = DST.enabled_engines.first; link; link = link->next) {
		DrawEngineType *engine = link->data;
		ViewportEngineData *data = DRW_viewport_engine_data_get(engine);
		PROFILE_START(stime);

		if (data->text_draw_cache) {
			DRW_text_cache_draw(data->text_draw_cache, DST.draw_ctx.v3d, DST.draw_ctx.ar, false);
		}

		PROFILE_END_UPDATE(data->render_time, stime);
	}
}

#define MAX_INFO_LINES 10

/**
 * Returns the offset required for the drawing of engines info.
 */
int DRW_draw_region_engine_info_offset(void)
{
	int lines = 0;
	for (LinkData *link = DST.enabled_engines.first; link; link = link->next) {
		DrawEngineType *engine = link->data;
		ViewportEngineData *data = DRW_viewport_engine_data_get(engine);

		/* Count the number of lines. */
		if (data->info[0] != '\0') {
			lines++;
			char *c = data->info;
			while (*c++ != '\0') {
				if (*c == '\n') {
					lines++;
				}
			}
		}
	}
	return MIN2(MAX_INFO_LINES, lines) * UI_UNIT_Y;
}

/**
 * Actual drawing;
 */
void DRW_draw_region_engine_info(void)
{
	const char *info_array_final[MAX_INFO_LINES + 1];
	/* This should be maxium number of engines running at the same time. */
	char info_array[MAX_INFO_LINES][GPU_INFO_SIZE];
	int i = 0;

	const DRWContextState *draw_ctx = DRW_context_state_get();
	ARegion *ar = draw_ctx->ar;
	float fill_color[4] = {0.0f, 0.0f, 0.0f, 0.25f};

	UI_GetThemeColor3fv(TH_HIGH_GRAD, fill_color);
	mul_v3_fl(fill_color, fill_color[3]);

	for (LinkData *link = DST.enabled_engines.first; link; link = link->next) {
		DrawEngineType *engine = link->data;
		ViewportEngineData *data = DRW_viewport_engine_data_get(engine);

		if (data->info[0] != '\0') {
			char *chr_current = data->info;
			char *chr_start = chr_current;
			int line_len = 0;

			while (*chr_current++ != '\0') {
				line_len++;
				if (*chr_current == '\n') {
					BLI_strncpy(info_array[i++], chr_start, line_len + 1);
					/* Re-start counting. */
					chr_start = chr_current + 1;
					line_len = -1;
				}
			}

			BLI_strncpy(info_array[i++], chr_start, line_len + 1);

			if (i >= MAX_INFO_LINES) {
				break;
			}
		}
	}

	for (int j = 0; j < i; j++) {
		info_array_final[j] = info_array[j];
	}
	info_array_final[i] = NULL;

	if (info_array[0] != NULL) {
		ED_region_info_draw_multiline(ar, info_array_final, fill_color, true);
	}
}

#undef MAX_INFO_LINES

static void use_drw_engine(DrawEngineType *engine)
{
	LinkData *ld = MEM_callocN(sizeof(LinkData), "enabled engine link data");
	ld->data = engine;
	BLI_addtail(&DST.enabled_engines, ld);
}

/* TODO revisit this when proper layering is implemented */
/* Gather all draw engines needed and store them in DST.enabled_engines
 * That also define the rendering order of engines */
static void DRW_engines_enable_from_engine(const Scene *scene)
{
	/* TODO layers */
	RenderEngineType *type = RE_engines_find(scene->r.engine);
	if (type->draw_engine != NULL) {
		use_drw_engine(type->draw_engine);
	}

	if ((type->flag & RE_INTERNAL) == 0) {
		DRW_engines_enable_external();
	}
}

static void DRW_engines_enable_from_object_mode(void)
{
	use_drw_engine(&draw_engine_object_type);
}

static void DRW_engines_enable_from_mode(int mode)
{
	switch (mode) {
		case CTX_MODE_EDIT_MESH:
			use_drw_engine(&draw_engine_edit_mesh_type);
			break;
		case CTX_MODE_EDIT_CURVE:
			use_drw_engine(&draw_engine_edit_curve_type);
			break;
		case CTX_MODE_EDIT_SURFACE:
			use_drw_engine(&draw_engine_edit_surface_type);
			break;
		case CTX_MODE_EDIT_TEXT:
			use_drw_engine(&draw_engine_edit_text_type);
			break;
		case CTX_MODE_EDIT_ARMATURE:
			use_drw_engine(&draw_engine_edit_armature_type);
			break;
		case CTX_MODE_EDIT_METABALL:
			use_drw_engine(&draw_engine_edit_metaball_type);
			break;
		case CTX_MODE_EDIT_LATTICE:
			use_drw_engine(&draw_engine_edit_lattice_type);
			break;
		case CTX_MODE_POSE:
			use_drw_engine(&draw_engine_pose_type);
			break;
		case CTX_MODE_SCULPT:
			use_drw_engine(&draw_engine_sculpt_type);
			break;
		case CTX_MODE_PAINT_WEIGHT:
			use_drw_engine(&draw_engine_pose_type);
			use_drw_engine(&draw_engine_paint_weight_type);
			break;
		case CTX_MODE_PAINT_VERTEX:
			use_drw_engine(&draw_engine_paint_vertex_type);
			break;
		case CTX_MODE_PAINT_TEXTURE:
			use_drw_engine(&draw_engine_paint_texture_type);
			break;
		case CTX_MODE_PARTICLE:
			use_drw_engine(&draw_engine_particle_type);
			break;
		case CTX_MODE_OBJECT:
			break;
		default:
			BLI_assert(!"Draw mode invalid");
			break;
	}
}

/**
 * Use for select and depth-drawing.
 */
static void DRW_engines_enable_basic(void)
{
	use_drw_engine(DRW_engine_viewport_basic_type.draw_engine);
}

/**
 * Use for external render engines.
 */
static void DRW_engines_enable_external(void)
{
	use_drw_engine(DRW_engine_viewport_external_type.draw_engine);
}

static void DRW_engines_enable(const Scene *scene, SceneLayer *sl)
{
	Object *obact = OBACT_NEW(sl);
	const int mode = CTX_data_mode_enum_ex(scene->obedit, obact);
	DRW_engines_enable_from_engine(scene);

	if (DRW_state_draw_support()) {
		DRW_engines_enable_from_object_mode();
		DRW_engines_enable_from_mode(mode);
	}
}

static void DRW_engines_disable(void)
{
	BLI_freelistN(&DST.enabled_engines);
}

static unsigned int DRW_engines_get_hash(void)
{
	unsigned int hash = 0;
	/* The cache depends on enabled engines */
	/* FIXME : if collision occurs ... segfault */
	for (LinkData *link = DST.enabled_engines.first; link; link = link->next) {
		DrawEngineType *engine = link->data;
		hash += BLI_ghashutil_strhash_p(engine->idname);
	}

	return hash;
}

static void draw_stat(rcti *rect, int u, int v, const char *txt, const int size)
{
	BLF_draw_default_ascii(rect->xmin + (1 + u * 5) * U.widget_unit,
	                       rect->ymax - (3 + v) * U.widget_unit, 0.0f,
	                       txt, size);
}

/* CPU stats */
static void DRW_debug_cpu_stats(void)
{
	int u, v;
	double init_tot_time = 0.0, background_tot_time = 0.0, render_tot_time = 0.0, tot_time = 0.0;
	/* local coordinate visible rect inside region, to accomodate overlapping ui */
	rcti rect;
	struct ARegion *ar = DST.draw_ctx.ar;
	ED_region_visible_rect(ar, &rect);

	UI_FontThemeColor(BLF_default(), TH_TEXT_HI);

	/* row by row */
	v = 0; u = 0;
	/* Label row */
	char col_label[32];
	sprintf(col_label, "Engine");
	draw_stat(&rect, u++, v, col_label, sizeof(col_label));
	sprintf(col_label, "Init");
	draw_stat(&rect, u++, v, col_label, sizeof(col_label));
	sprintf(col_label, "Background");
	draw_stat(&rect, u++, v, col_label, sizeof(col_label));
	sprintf(col_label, "Render");
	draw_stat(&rect, u++, v, col_label, sizeof(col_label));
	sprintf(col_label, "Total (w/o cache)");
	draw_stat(&rect, u++, v, col_label, sizeof(col_label));
	v++;

	/* Engines rows */
	char time_to_txt[16];
	for (LinkData *link = DST.enabled_engines.first; link; link = link->next) {
		u = 0;
		DrawEngineType *engine = link->data;
		ViewportEngineData *data = DRW_viewport_engine_data_get(engine);

		draw_stat(&rect, u++, v, engine->idname, sizeof(engine->idname));

		init_tot_time += data->init_time;
		sprintf(time_to_txt, "%.2fms", data->init_time);
		draw_stat(&rect, u++, v, time_to_txt, sizeof(time_to_txt));

		background_tot_time += data->background_time;
		sprintf(time_to_txt, "%.2fms", data->background_time);
		draw_stat(&rect, u++, v, time_to_txt, sizeof(time_to_txt));

		render_tot_time += data->render_time;
		sprintf(time_to_txt, "%.2fms", data->render_time);
		draw_stat(&rect, u++, v, time_to_txt, sizeof(time_to_txt));

		tot_time += data->init_time + data->background_time + data->render_time;
		sprintf(time_to_txt, "%.2fms", data->init_time + data->background_time + data->render_time);
		draw_stat(&rect, u++, v, time_to_txt, sizeof(time_to_txt));
		v++;
	}

	/* Totals row */
	u = 0;
	sprintf(col_label, "Sub Total");
	draw_stat(&rect, u++, v, col_label, sizeof(col_label));
	sprintf(time_to_txt, "%.2fms", init_tot_time);
	draw_stat(&rect, u++, v, time_to_txt, sizeof(time_to_txt));
	sprintf(time_to_txt, "%.2fms", background_tot_time);
	draw_stat(&rect, u++, v, time_to_txt, sizeof(time_to_txt));
	sprintf(time_to_txt, "%.2fms", render_tot_time);
	draw_stat(&rect, u++, v, time_to_txt, sizeof(time_to_txt));
	sprintf(time_to_txt, "%.2fms", tot_time);
	draw_stat(&rect, u++, v, time_to_txt, sizeof(time_to_txt));
	v += 2;

	u = 0;
	sprintf(col_label, "Cache Time");
	draw_stat(&rect, u++, v, col_label, sizeof(col_label));
	sprintf(time_to_txt, "%.2fms", DST.cache_time);
	draw_stat(&rect, u++, v, time_to_txt, sizeof(time_to_txt));
}

/* Display GPU time for each passes */
static void DRW_debug_gpu_stats(void)
{
	/* local coordinate visible rect inside region, to accomodate overlapping ui */
	rcti rect;
	struct ARegion *ar = DST.draw_ctx.ar;
	ED_region_visible_rect(ar, &rect);

	UI_FontThemeColor(BLF_default(), TH_TEXT_HI);

	int v = BLI_listbase_count(&DST.enabled_engines) + 5;

	char stat_string[32];

	/* Memory Stats */
	unsigned int tex_mem = GPU_texture_memory_usage_get();
	unsigned int vbo_mem = GWN_vertbuf_get_memory_usage();

	sprintf(stat_string, "GPU Memory");
	draw_stat(&rect, 0, v, stat_string, sizeof(stat_string));
	sprintf(stat_string, "%.2fMB", (double)(tex_mem + vbo_mem) / 1000000.0);
	draw_stat(&rect, 1, v++, stat_string, sizeof(stat_string));
	sprintf(stat_string, "   |--> Textures");
	draw_stat(&rect, 0, v, stat_string, sizeof(stat_string));
	sprintf(stat_string, "%.2fMB", (double)tex_mem / 1000000.0);
	draw_stat(&rect, 1, v++, stat_string, sizeof(stat_string));
	sprintf(stat_string, "   |--> Meshes");
	draw_stat(&rect, 0, v, stat_string, sizeof(stat_string));
	sprintf(stat_string, "%.2fMB", (double)vbo_mem / 1000000.0);
	draw_stat(&rect, 1, v++, stat_string, sizeof(stat_string));

	/* Pre offset for stats_draw */
	rect.ymax -= (3 + ++v) * U.widget_unit;

	/* Rendering Stats */
	DRW_stats_draw(&rect);
}

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

/** \name View Update
 * \{ */

void DRW_notify_view_update(const bContext *C)
{
	struct Depsgraph *graph = CTX_data_depsgraph(C);
	ARegion *ar = CTX_wm_region(C);
	View3D *v3d = CTX_wm_view3d(C);
	RegionView3D *rv3d = ar->regiondata;
	Scene *scene = DEG_get_evaluated_scene(graph);
	SceneLayer *sl = DEG_get_evaluated_scene_layer(graph);

	if (rv3d->viewport == NULL) {
		return;
	}


	/* Reset before using it. */
	memset(&DST, 0x0, sizeof(DST));

	DST.viewport = rv3d->viewport;
	DST.draw_ctx = (DRWContextState){
		ar, rv3d, v3d, scene, sl, OBACT_NEW(sl), C,
	};

	DRW_engines_enable(scene, sl);

	for (LinkData *link = DST.enabled_engines.first; link; link = link->next) {
		DrawEngineType *engine = link->data;
		ViewportEngineData *data = DRW_viewport_engine_data_get(engine);

		if (engine->view_update) {
			engine->view_update(data);
		}
	}

	DST.viewport = NULL;

	DRW_engines_disable();
}

/** \} */

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

/** \name Main Draw Loops (DRW_draw)
 * \{ */

/* Everything starts here.
 * This function takes care of calling all cache and rendering functions
 * for each relevant engine / mode engine. */
void DRW_draw_view(const bContext *C)
{
	struct Depsgraph *graph = CTX_data_depsgraph(C);
	ARegion *ar = CTX_wm_region(C);
	View3D *v3d = CTX_wm_view3d(C);

	/* Reset before using it. */
	memset(&DST, 0x0, sizeof(DST));
	DRW_draw_render_loop_ex(graph, ar, v3d, C);
}

/**
 * Used for both regular and off-screen drawing.
 * Need to reset DST before calling this function
 */
void DRW_draw_render_loop_ex(
        struct Depsgraph *graph,
        ARegion *ar, View3D *v3d,
        const bContext *evil_C)
{
	Scene *scene = DEG_get_evaluated_scene(graph);
	SceneLayer *sl = DEG_get_evaluated_scene_layer(graph);
	RegionView3D *rv3d = ar->regiondata;

	DST.draw_ctx.evil_C = evil_C;

	bool cache_is_dirty;
	DST.viewport = rv3d->viewport;
	v3d->zbuf = true;

	/* Setup viewport */
	cache_is_dirty = GPU_viewport_cache_validate(DST.viewport, DRW_engines_get_hash());

	DST.draw_ctx = (DRWContextState){
		ar, rv3d, v3d, scene, sl, OBACT_NEW(sl),
		/* reuse if caller sets */
		DST.draw_ctx.evil_C,
	};

	DRW_viewport_var_init();

	/* Get list of enabled engines */
	DRW_engines_enable(scene, sl);

	/* Update ubos */
	DRW_globals_update();

	/* Init engines */
	DRW_engines_init();

	/* TODO : tag to refresh by the deps graph */
	/* ideally only refresh when objects are added/removed */
	/* or render properties / materials change */
	if (cache_is_dirty) {
		PROFILE_START(stime);
		DRW_engines_cache_init();

		DEG_OBJECT_ITER(graph, ob, DEG_OBJECT_ITER_FLAG_ALL);
		{
			DRW_engines_cache_populate(ob);
			/* XXX find a better place for this. maybe Depsgraph? */
			ob->deg_update_flag = 0;
		}
		DEG_OBJECT_ITER_END

		DRW_engines_cache_finish();
		PROFILE_END_ACCUM(DST.cache_time, stime);
	}

	DRW_stats_begin();

	/* Start Drawing */
	DRW_state_reset();
	DRW_engines_draw_background();

	DRW_draw_callbacks_pre_scene();
	if (DST.draw_ctx.evil_C) {
		ED_region_draw_cb_draw(DST.draw_ctx.evil_C, DST.draw_ctx.ar, REGION_DRAW_PRE_VIEW);
	}

	DRW_engines_draw_scene();

	DRW_draw_callbacks_post_scene();
	if (DST.draw_ctx.evil_C) {
		ED_region_draw_cb_draw(DST.draw_ctx.evil_C, DST.draw_ctx.ar, REGION_DRAW_POST_VIEW);
	}

	DRW_state_reset();

	DRW_engines_draw_text();

	if (DST.draw_ctx.evil_C) {
		/* needed so manipulator isn't obscured */
		glDisable(GL_DEPTH_TEST);
		DRW_draw_manipulator();
		glEnable(GL_DEPTH_TEST);

		DRW_draw_region_info();
	}

	DRW_stats_reset();

	if (G.debug_value > 20) {
		DRW_debug_cpu_stats();
		DRW_debug_gpu_stats();
	}

	DRW_state_reset();
	DRW_engines_disable();

#ifdef DEBUG
	/* Avoid accidental reuse. */
	memset(&DST, 0xFF, sizeof(DST));
#endif
}

void DRW_draw_render_loop(
        struct Depsgraph *graph,
        ARegion *ar, View3D *v3d)
{
	/* Reset before using it. */
	memset(&DST, 0x0, sizeof(DST));
	DRW_draw_render_loop_ex(graph, ar, v3d, NULL);
}

void DRW_draw_render_loop_offscreen(
        struct Depsgraph *graph,
        ARegion *ar, View3D *v3d, GPUOffScreen *ofs)
{
	RegionView3D *rv3d = ar->regiondata;

	/* backup */
	void *backup_viewport = rv3d->viewport;
	{
		/* backup (_never_ use rv3d->viewport) */
		rv3d->viewport = GPU_viewport_create_from_offscreen(ofs);
	}

	/* Reset before using it. */
	memset(&DST, 0x0, sizeof(DST));
	DST.options.is_image_render = true;
	DRW_draw_render_loop_ex(graph, ar, v3d, NULL);

	/* restore */
	{
		/* don't free data owned by 'ofs' */
		GPU_viewport_clear_from_offscreen(rv3d->viewport);
		GPU_viewport_free(rv3d->viewport);
		MEM_freeN(rv3d->viewport);

		rv3d->viewport = backup_viewport;
	}

	/* we need to re-bind (annoying!) */
	GPU_offscreen_bind(ofs, false);
}

/**
 * object mode select-loop, see: ED_view3d_draw_select_loop (legacy drawing).
 */
void DRW_draw_select_loop(
        struct Depsgraph *graph,
        ARegion *ar, View3D *v3d,
        bool UNUSED(use_obedit_skip), bool UNUSED(use_nearest), const rcti *rect)
{
	Scene *scene = DEG_get_evaluated_scene(graph);
	SceneLayer *sl = DEG_get_evaluated_scene_layer(graph);
#ifndef USE_GPU_SELECT
	UNUSED_VARS(vc, scene, sl, v3d, ar, rect);
#else
	RegionView3D *rv3d = ar->regiondata;

	/* Reset before using it. */
	memset(&DST, 0x0, sizeof(DST));

	/* backup (_never_ use rv3d->viewport) */
	void *backup_viewport = rv3d->viewport;
	rv3d->viewport = NULL;

	bool use_obedit = false;
	int obedit_mode = 0;
	if (scene->obedit && scene->obedit->type == OB_MBALL) {
		use_obedit = true;
		DRW_engines_cache_populate(scene->obedit);
		obedit_mode = CTX_MODE_EDIT_METABALL;
	}
	else if ((scene->obedit && scene->obedit->type == OB_ARMATURE)) {
		/* if not drawing sketch, draw bones */
		// if (!BDR_drawSketchNames(vc))
		{
			use_obedit = true;
			obedit_mode = CTX_MODE_EDIT_ARMATURE;
		}
	}

	struct GPUViewport *viewport = GPU_viewport_create();
	GPU_viewport_size_set(viewport, (const int[2]){BLI_rcti_size_x(rect), BLI_rcti_size_y(rect)});

	bool cache_is_dirty;
	DST.viewport = viewport;
	v3d->zbuf = true;

	DST.options.is_select = true;

	/* Get list of enabled engines */
	if (use_obedit) {
		DRW_engines_enable_from_mode(obedit_mode);
	}
	else {
		DRW_engines_enable_basic();
		DRW_engines_enable_from_object_mode();
	}

	/* Setup viewport */
	cache_is_dirty = true;

	/* Instead of 'DRW_context_state_init(C, &DST.draw_ctx)', assign from args */
	DST.draw_ctx = (DRWContextState){
		ar, rv3d, v3d, scene, sl, OBACT_NEW(sl), (bContext *)NULL,
	};

	DRW_viewport_var_init();

	/* Update ubos */
	DRW_globals_update();

	/* Init engines */
	DRW_engines_init();

	/* TODO : tag to refresh by the deps graph */
	/* ideally only refresh when objects are added/removed */
	/* or render properties / materials change */
	if (cache_is_dirty) {

		DRW_engines_cache_init();

		if (use_obedit) {
			DRW_engines_cache_populate(scene->obedit);
		}
		else {
			DEG_OBJECT_ITER(graph, ob, DEG_OBJECT_ITER_FLAG_DUPLI)
			{
				if ((ob->base_flag & BASE_SELECTABLED) != 0) {
					DRW_select_load_id(ob->select_color);
					DRW_engines_cache_populate(ob);
				}
			}
			DEG_OBJECT_ITER_END
		}

		DRW_engines_cache_finish();
	}

	/* Start Drawing */
	DRW_state_reset();
	DRW_draw_callbacks_pre_scene();
	DRW_engines_draw_scene();
	DRW_draw_callbacks_post_scene();

	DRW_state_reset();
	DRW_engines_disable();

#ifdef DEBUG
	/* Avoid accidental reuse. */
	memset(&DST, 0xFF, sizeof(DST));
#endif

	/* Cleanup for selection state */
	GPU_viewport_free(viewport);
	MEM_freeN(viewport);

	/* restore */
	rv3d->viewport = backup_viewport;
#endif  /* USE_GPU_SELECT */
}

/**
 * object mode select-loop, see: ED_view3d_draw_depth_loop (legacy drawing).
 */
void DRW_draw_depth_loop(
        Depsgraph *graph,
        ARegion *ar, View3D *v3d)
{
	Scene *scene = DEG_get_evaluated_scene(graph);
	SceneLayer *sl = DEG_get_evaluated_scene_layer(graph);
	RegionView3D *rv3d = ar->regiondata;

	/* backup (_never_ use rv3d->viewport) */
	void *backup_viewport = rv3d->viewport;
	rv3d->viewport = NULL;

	/* Reset before using it. */
	memset(&DST, 0x0, sizeof(DST));

	struct GPUViewport *viewport = GPU_viewport_create();
	GPU_viewport_size_set(viewport, (const int[2]){ar->winx, ar->winy});

	bool cache_is_dirty;
	DST.viewport = viewport;
	v3d->zbuf = true;

	DST.options.is_depth = true;

	/* Get list of enabled engines */
	{
		DRW_engines_enable_basic();
		DRW_engines_enable_from_object_mode();
	}

	/* Setup viewport */
	cache_is_dirty = true;

	/* Instead of 'DRW_context_state_init(C, &DST.draw_ctx)', assign from args */
	DST.draw_ctx = (DRWContextState){
		ar, rv3d, v3d, scene, sl, OBACT_NEW(sl), (bContext *)NULL,
	};

	DRW_viewport_var_init();

	/* Update ubos */
	DRW_globals_update();

	/* Init engines */
	DRW_engines_init();

	/* TODO : tag to refresh by the deps graph */
	/* ideally only refresh when objects are added/removed */
	/* or render properties / materials change */
	if (cache_is_dirty) {

		DRW_engines_cache_init();

		DEG_OBJECT_ITER(graph, ob, DEG_OBJECT_ITER_FLAG_ALL)
		{
			DRW_engines_cache_populate(ob);
		}
		DEG_OBJECT_ITER_END

		DRW_engines_cache_finish();
	}

	/* Start Drawing */
	DRW_state_reset();
	DRW_draw_callbacks_pre_scene();
	DRW_engines_draw_scene();
	DRW_draw_callbacks_post_scene();

	DRW_state_reset();
	DRW_engines_disable();

#ifdef DEBUG
	/* Avoid accidental reuse. */
	memset(&DST, 0xFF, sizeof(DST));
#endif

	/* Cleanup for selection state */
	GPU_viewport_free(viewport);
	MEM_freeN(viewport);

	/* restore */
	rv3d->viewport = backup_viewport;
}

/** \} */


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

/** \name Draw Manager State (DRW_state)
 * \{ */

void DRW_state_dfdy_factors_get(float dfdyfac[2])
{
	GPU_get_dfdy_factors(dfdyfac);
}

/**
 * When false, drawing doesn't output to a pixel buffer
 * eg: Occlusion queries, or when we have setup a context to draw in already.
 */
bool DRW_state_is_fbo(void)
{
	return (DST.default_framebuffer != NULL);
}

/**
 * For when engines need to know if this is drawing for selection or not.
 */
bool DRW_state_is_select(void)
{
	return DST.options.is_select;
}

bool DRW_state_is_depth(void)
{
	return DST.options.is_depth;
}

/**
 * Whether we are rendering for an image
 */
bool DRW_state_is_image_render(void)
{
	return DST.options.is_image_render;
}

/**
 * Whether we are rendering only the render engine,
 * or if we should also render the mode engines.
 */
bool DRW_state_is_scene_render(void)
{
	BLI_assert(DST.options.is_scene_render ?
	           DST.options.is_image_render : true);
	return DST.options.is_scene_render;
}

/**
 * Should text draw in this mode?
 */
bool DRW_state_show_text(void)
{
	return (DST.options.is_select) == 0 &&
	       (DST.options.is_depth) == 0 &&
	       (DST.options.is_scene_render) == 0;
}

/**
 * Should draw support elements
 * Objects center, selection outline, probe data, ...
 */
bool DRW_state_draw_support(void)
{
	View3D *v3d = DST.draw_ctx.v3d;
	return (DRW_state_is_scene_render() == false) &&
	        (v3d != NULL) &&
	        ((v3d->flag2 & V3D_RENDER_OVERRIDE) == 0);
}

/** \} */


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

/** \name Context State (DRW_context_state)
 * \{ */

const DRWContextState *DRW_context_state_get(void)
{
	return &DST.draw_ctx;
}

/** \} */


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

/** \name Init/Exit (DRW_engines)
 * \{ */

void DRW_engine_register(DrawEngineType *draw_engine_type)
{
	BLI_addtail(&DRW_engines, draw_engine_type);
}

void DRW_engines_register(void)
{
#ifdef WITH_CLAY_ENGINE
	RE_engines_register(NULL, &DRW_engine_viewport_clay_type);
#endif
	RE_engines_register(NULL, &DRW_engine_viewport_eevee_type);

	DRW_engine_register(&draw_engine_object_type);
	DRW_engine_register(&draw_engine_edit_armature_type);
	DRW_engine_register(&draw_engine_edit_curve_type);
	DRW_engine_register(&draw_engine_edit_lattice_type);
	DRW_engine_register(&draw_engine_edit_mesh_type);
	DRW_engine_register(&draw_engine_edit_metaball_type);
	DRW_engine_register(&draw_engine_edit_surface_type);
	DRW_engine_register(&draw_engine_edit_text_type);
	DRW_engine_register(&draw_engine_paint_texture_type);
	DRW_engine_register(&draw_engine_paint_vertex_type);
	DRW_engine_register(&draw_engine_paint_weight_type);
	DRW_engine_register(&draw_engine_particle_type);
	DRW_engine_register(&draw_engine_pose_type);
	DRW_engine_register(&draw_engine_sculpt_type);

	/* setup callbacks */
	{
		/* BKE: curve.c */
		extern void *BKE_curve_batch_cache_dirty_cb;
		extern void *BKE_curve_batch_cache_free_cb;
		/* BKE: mesh.c */
		extern void *BKE_mesh_batch_cache_dirty_cb;
		extern void *BKE_mesh_batch_cache_free_cb;
		/* BKE: lattice.c */
		extern void *BKE_lattice_batch_cache_dirty_cb;
		extern void *BKE_lattice_batch_cache_free_cb;
		/* BKE: particle.c */
		extern void *BKE_particle_batch_cache_dirty_cb;
		extern void *BKE_particle_batch_cache_free_cb;

		BKE_curve_batch_cache_dirty_cb = DRW_curve_batch_cache_dirty;
		BKE_curve_batch_cache_free_cb = DRW_curve_batch_cache_free;

		BKE_mesh_batch_cache_dirty_cb = DRW_mesh_batch_cache_dirty;
		BKE_mesh_batch_cache_free_cb = DRW_mesh_batch_cache_free;

		BKE_lattice_batch_cache_dirty_cb = DRW_lattice_batch_cache_dirty;
		BKE_lattice_batch_cache_free_cb = DRW_lattice_batch_cache_free;

		BKE_particle_batch_cache_dirty_cb = DRW_particle_batch_cache_dirty;
		BKE_particle_batch_cache_free_cb = DRW_particle_batch_cache_free;
	}
}

extern struct GPUUniformBuffer *globals_ubo; /* draw_common.c */
extern struct GPUTexture *globals_ramp; /* draw_common.c */
void DRW_engines_free(void)
{
	DRW_shape_cache_free();
	DRW_stats_free();

	DrawEngineType *next;
	for (DrawEngineType *type = DRW_engines.first; type; type = next) {
		next = type->next;
		BLI_remlink(&R_engines, type);

		if (type->engine_free) {
			type->engine_free();
		}
	}

	if (globals_ubo)
		GPU_uniformbuffer_free(globals_ubo);

	if (globals_ramp)
		GPU_texture_free(globals_ramp);

	MEM_SAFE_FREE(RST.bound_texs);
	MEM_SAFE_FREE(RST.bound_tex_slots);

#ifdef WITH_CLAY_ENGINE
	BLI_remlink(&R_engines, &DRW_engine_viewport_clay_type);
#endif
}

/** \} */