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
|
/*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* 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.
*
* The Original Code is Copyright (C) 2005 Blender Foundation.
* All rights reserved.
*
* The Original Code is: all of this file.
*
* Contributor(s): Brecht Van Lommel.
*
* ***** END GPL LICENSE BLOCK *****
*/
/** \file blender/gpu/intern/gpu_draw.c
* \ingroup gpu
*
* Utility functions for dealing with OpenGL texture & material context,
* mipmap generation and light objects.
*
* These are some obscure rendering functions shared between the
* game engine and the blender, in this module to avoid duplication
* and abstract them away from the rest a bit.
*/
#include <string.h>
#include "BLI_blenlib.h"
#include "BLI_linklist.h"
#include "BLI_math.h"
#include "BLI_threads.h"
#include "BLI_utildefines.h"
#include "BLI_hash.h"
#include "DNA_lamp_types.h"
#include "DNA_material_types.h"
#include "DNA_mesh_types.h"
#include "DNA_meshdata_types.h"
#include "DNA_modifier_types.h"
#include "DNA_node_types.h"
#include "DNA_object_types.h"
#include "DNA_scene_types.h"
#include "DNA_smoke_types.h"
#include "DNA_view3d_types.h"
#include "DNA_particle_types.h"
#include "MEM_guardedalloc.h"
#include "IMB_imbuf.h"
#include "IMB_imbuf_types.h"
#include "BKE_bmfont.h"
#include "BKE_global.h"
#include "BKE_image.h"
#include "BKE_main.h"
#include "BKE_material.h"
#include "BKE_node.h"
#include "BKE_scene.h"
#include "BKE_DerivedMesh.h"
#ifdef WITH_GAMEENGINE
# include "BKE_object.h"
#endif
#include "GPU_basic_shader.h"
#include "GPU_buffers.h"
#include "GPU_draw.h"
#include "GPU_extensions.h"
#include "GPU_material.h"
#include "GPU_matrix.h"
#include "GPU_shader.h"
#include "GPU_texture.h"
#include "PIL_time.h"
#ifdef WITH_SMOKE
# include "smoke_API.h"
#endif
#ifdef WITH_OPENSUBDIV
# include "BKE_subsurf.h"
# include "BKE_editmesh.h"
# include "gpu_codegen.h"
#endif
extern Material defmaterial; /* from material.c */
/* Text Rendering */
static void gpu_mcol(unsigned int ucol)
{
/* mcol order is swapped */
const char *cp = (char *)&ucol;
glColor3ub(cp[3], cp[2], cp[1]);
}
void GPU_render_text(
int mode, const char *textstr, int textlen, unsigned int *col,
const float *v_quad[4], const float *uv_quad[4],
int glattrib)
{
/* XXX, 2.8 removes texface */
#if 0
Image *ima = mtexpoly->tpage;
#else
Image *ima = NULL;
#endif
if ((mode & GEMAT_TEXT) && (textlen > 0) && ima) {
const float *v1 = v_quad[0];
const float *v2 = v_quad[1];
const float *v3 = v_quad[2];
const float *v4 = v_quad[3];
const size_t textlen_st = textlen;
float centerx, centery, sizex, sizey, transx, transy, movex, movey, advance;
/* multiline */
float line_start = 0.0f, line_height;
if (v4)
line_height = max_ffff(v1[1], v2[1], v3[1], v4[2]) - min_ffff(v1[1], v2[1], v3[1], v4[2]);
else
line_height = max_fff(v1[1], v2[1], v3[1]) - min_fff(v1[1], v2[1], v3[1]);
line_height *= 1.2f; /* could be an option? */
/* end multiline */
/* color has been set */
if (!col)
glColor3f(1.0f, 1.0f, 1.0f);
gpuPushMatrix();
/* get the tab width */
ImBuf *first_ibuf = BKE_image_get_first_ibuf(ima);
matrixGlyph(first_ibuf, ' ', ¢erx, ¢ery,
&sizex, &sizey, &transx, &transy, &movex, &movey, &advance);
float advance_tab = advance * 4; /* tab width could also be an option */
for (size_t index = 0; index < textlen_st; ) {
unsigned int character;
float uv[4][2];
/* lets calculate offset stuff */
character = BLI_str_utf8_as_unicode_and_size_safe(textstr + index, &index);
if (character == '\n') {
gpuTranslate2f(line_start, -line_height);
line_start = 0.0f;
continue;
}
else if (character == '\t') {
gpuTranslate2f(advance_tab, 0.0f);
line_start -= advance_tab; /* so we can go back to the start of the line */
continue;
}
else if (character > USHRT_MAX) {
/* not much we can do here bmfonts take ushort */
character = '?';
}
/* space starts at offset 1 */
/* character = character - ' ' + 1; */
matrixGlyph(first_ibuf, character, & centerx, ¢ery,
&sizex, &sizey, &transx, &transy, &movex, &movey, &advance);
uv[0][0] = (uv_quad[0][0] - centerx) * sizex + transx;
uv[0][1] = (uv_quad[0][1] - centery) * sizey + transy;
uv[1][0] = (uv_quad[1][0] - centerx) * sizex + transx;
uv[1][1] = (uv_quad[1][1] - centery) * sizey + transy;
uv[2][0] = (uv_quad[2][0] - centerx) * sizex + transx;
uv[2][1] = (uv_quad[2][1] - centery) * sizey + transy;
glBegin(GL_POLYGON);
if (glattrib >= 0) glVertexAttrib2fv(glattrib, uv[0]);
else glTexCoord2fv(uv[0]);
if (col) gpu_mcol(col[0]);
glVertex3f(sizex * v1[0] + movex, sizey * v1[1] + movey, v1[2]);
if (glattrib >= 0) glVertexAttrib2fv(glattrib, uv[1]);
else glTexCoord2fv(uv[1]);
if (col) gpu_mcol(col[1]);
glVertex3f(sizex * v2[0] + movex, sizey * v2[1] + movey, v2[2]);
if (glattrib >= 0) glVertexAttrib2fv(glattrib, uv[2]);
else glTexCoord2fv(uv[2]);
if (col) gpu_mcol(col[2]);
glVertex3f(sizex * v3[0] + movex, sizey * v3[1] + movey, v3[2]);
if (v4) {
uv[3][0] = (uv_quad[3][0] - centerx) * sizex + transx;
uv[3][1] = (uv_quad[3][1] - centery) * sizey + transy;
if (glattrib >= 0) glVertexAttrib2fv(glattrib, uv[3]);
else glTexCoord2fv(uv[3]);
if (col) gpu_mcol(col[3]);
glVertex3f(sizex * v4[0] + movex, sizey * v4[1] + movey, v4[2]);
}
glEnd();
gpuTranslate2f(advance, 0.0f);
line_start -= advance; /* so we can go back to the start of the line */
}
gpuPopMatrix();
BKE_image_release_ibuf(ima, first_ibuf, NULL);
}
}
/* Checking powers of two for images since OpenGL ES requires it */
#ifdef WITH_DDS
static bool is_power_of_2_resolution(int w, int h)
{
return is_power_of_2_i(w) && is_power_of_2_i(h);
}
#endif
static bool is_over_resolution_limit(GLenum textarget, int w, int h)
{
int size = (textarget == GL_TEXTURE_2D) ?
GPU_max_texture_size() : GPU_max_cube_map_size();
int reslimit = (U.glreslimit != 0) ?
min_ii(U.glreslimit, size) : size;
return (w > reslimit || h > reslimit);
}
static int smaller_power_of_2_limit(int num)
{
int reslimit = (U.glreslimit != 0) ?
min_ii(U.glreslimit, GPU_max_texture_size()) :
GPU_max_texture_size();
/* take texture clamping into account */
if (num > reslimit)
return reslimit;
return power_of_2_min_i(num);
}
/* Current OpenGL state caching for GPU_set_tpage */
static struct GPUTextureState {
int curtile, tile;
int curtilemode, tilemode;
int curtileXRep, tileXRep;
int curtileYRep, tileYRep;
Image *ima, *curima;
/* also controls min/mag filtering */
bool domipmap;
/* only use when 'domipmap' is set */
bool linearmipmap;
/* store this so that new images created while texture painting won't be set to mipmapped */
bool texpaint;
int alphablend;
float anisotropic;
int gpu_mipmap;
} GTS = {0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL, 1, 0, 0, -1, 1.0f, 0};
/* Mipmap settings */
void GPU_set_gpu_mipmapping(int gpu_mipmap)
{
int old_value = GTS.gpu_mipmap;
/* only actually enable if it's supported */
GTS.gpu_mipmap = gpu_mipmap;
if (old_value != GTS.gpu_mipmap) {
GPU_free_images();
}
}
void GPU_set_mipmap(bool mipmap)
{
if (GTS.domipmap != mipmap) {
GPU_free_images();
GTS.domipmap = mipmap;
}
}
void GPU_set_linear_mipmap(bool linear)
{
if (GTS.linearmipmap != linear) {
GTS.linearmipmap = linear;
}
}
bool GPU_get_mipmap(void)
{
return GTS.domipmap && !GTS.texpaint;
}
bool GPU_get_linear_mipmap(void)
{
return GTS.linearmipmap;
}
static GLenum gpu_get_mipmap_filter(bool mag)
{
/* linearmipmap is off by default *when mipmapping is off,
* use unfiltered display */
if (mag) {
if (GTS.domipmap)
return GL_LINEAR;
else
return GL_NEAREST;
}
else {
if (GTS.domipmap) {
if (GTS.linearmipmap) {
return GL_LINEAR_MIPMAP_LINEAR;
}
else {
return GL_LINEAR_MIPMAP_NEAREST;
}
}
else {
return GL_NEAREST;
}
}
}
/* Anisotropic filtering settings */
void GPU_set_anisotropic(float value)
{
if (GTS.anisotropic != value) {
GPU_free_images();
/* Clamp value to the maximum value the graphics card supports */
const float max = GPU_max_texture_anisotropy();
if (value > max)
value = max;
GTS.anisotropic = value;
}
}
float GPU_get_anisotropic(void)
{
return GTS.anisotropic;
}
/* Set OpenGL state for an MTFace */
static void gpu_make_repbind(Image *ima)
{
ImBuf *ibuf = BKE_image_acquire_ibuf(ima, NULL, NULL);
if (ibuf == NULL)
return;
if (ima->repbind) {
glDeleteTextures(ima->totbind, (GLuint *)ima->repbind);
MEM_freeN(ima->repbind);
ima->repbind = NULL;
ima->tpageflag &= ~IMA_MIPMAP_COMPLETE;
}
ima->totbind = ima->xrep * ima->yrep;
if (ima->totbind > 1) {
ima->repbind = MEM_callocN(sizeof(int) * ima->totbind, "repbind");
}
BKE_image_release_ibuf(ima, ibuf, NULL);
}
static unsigned int *gpu_get_image_bindcode(Image *ima, GLenum textarget)
{
unsigned int *bind = 0;
if (textarget == GL_TEXTURE_2D)
bind = &ima->bindcode[TEXTARGET_TEXTURE_2D];
else if (textarget == GL_TEXTURE_CUBE_MAP)
bind = &ima->bindcode[TEXTARGET_TEXTURE_CUBE_MAP];
return bind;
}
static void gpu_set_alpha_blend(GPUBlendMode alphablend)
{
if (alphablend == GPU_BLEND_SOLID) {
glDisable(GL_BLEND);
glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
else if (alphablend == GPU_BLEND_ADD) {
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE);
glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE);
}
else if (ELEM(alphablend, GPU_BLEND_ALPHA, GPU_BLEND_ALPHA_SORT)) {
glEnable(GL_BLEND);
glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE);
/* for OpenGL render we use the alpha channel, this makes alpha blend correct */
glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
/* if U.glalphaclip == 1.0, some cards go bonkers...
* turn off alpha test in this case */
}
else if (alphablend == GPU_BLEND_CLIP) {
glDisable(GL_BLEND);
glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE);
}
else if (alphablend == GPU_BLEND_ALPHA_TO_COVERAGE) {
glEnable(GL_SAMPLE_ALPHA_TO_COVERAGE);
}
}
typedef struct VerifyThreadData {
ImBuf *ibuf;
float *srgb_frect;
} VerifyThreadData;
static void gpu_verify_high_bit_srgb_buffer_slice(float *srgb_frect,
ImBuf *ibuf,
const int start_line,
const int height)
{
size_t offset = ibuf->channels * start_line * ibuf->x;
float *current_srgb_frect = srgb_frect + offset;
float *current_rect_float = ibuf->rect_float + offset;
IMB_buffer_float_from_float(current_srgb_frect,
current_rect_float,
ibuf->channels,
IB_PROFILE_SRGB,
IB_PROFILE_LINEAR_RGB, true,
ibuf->x, height,
ibuf->x, ibuf->x);
IMB_buffer_float_unpremultiply(current_srgb_frect, ibuf->x, height);
/* Clamp buffer colors to 1.0 to avoid artifacts due to glu for hdr images. */
IMB_buffer_float_clamp(current_srgb_frect, ibuf->x, height);
}
static void verify_thread_do(void *data_v,
int start_scanline,
int num_scanlines)
{
VerifyThreadData *data = (VerifyThreadData *)data_v;
gpu_verify_high_bit_srgb_buffer_slice(data->srgb_frect,
data->ibuf,
start_scanline,
num_scanlines);
}
static void gpu_verify_high_bit_srgb_buffer(float *srgb_frect,
ImBuf *ibuf)
{
if (ibuf->y < 64) {
gpu_verify_high_bit_srgb_buffer_slice(srgb_frect,
ibuf,
0, ibuf->y);
}
else {
VerifyThreadData data;
data.ibuf = ibuf;
data.srgb_frect = srgb_frect;
IMB_processor_apply_threaded_scanlines(ibuf->y, verify_thread_do, &data);
}
}
int GPU_verify_image(
Image *ima, ImageUser *iuser,
int textarget, int tftile, bool compare, bool mipmap, bool is_data)
{
unsigned int *bind = NULL;
int tpx = 0, tpy = 0;
unsigned int *rect = NULL;
float *frect = NULL;
float *srgb_frect = NULL;
/* flag to determine whether deep format is used */
bool use_high_bit_depth = false, do_color_management = false;
/* initialize tile mode and number of repeats */
GTS.ima = ima;
GTS.tilemode = (ima && (ima->tpageflag & (IMA_TILES | IMA_TWINANIM)));
GTS.tileXRep = 0;
GTS.tileYRep = 0;
/* setting current tile according to frame */
if (ima && (ima->tpageflag & IMA_TWINANIM))
GTS.tile = ima->lastframe;
else
GTS.tile = tftile;
GTS.tile = MAX2(0, GTS.tile);
if (ima) {
GTS.tileXRep = ima->xrep;
GTS.tileYRep = ima->yrep;
}
/* if same image & tile, we're done */
if (compare && ima == GTS.curima && GTS.curtile == GTS.tile &&
GTS.tilemode == GTS.curtilemode && GTS.curtileXRep == GTS.tileXRep &&
GTS.curtileYRep == GTS.tileYRep)
{
return (ima != NULL);
}
/* check if we have a valid image */
if (ima == NULL || ima->ok == 0)
return 0;
/* check if we have a valid image buffer */
ImBuf *ibuf = BKE_image_acquire_ibuf(ima, iuser, NULL);
if (ibuf == NULL)
return 0;
if (ibuf->rect_float) {
if (U.use_16bit_textures) {
/* use high precision textures. This is relatively harmless because OpenGL gives us
* a high precision format only if it is available */
use_high_bit_depth = true;
}
else if (ibuf->rect == NULL) {
IMB_rect_from_float(ibuf);
}
/* we may skip this in high precision, but if not, we need to have a valid buffer here */
else if (ibuf->userflags & IB_RECT_INVALID) {
IMB_rect_from_float(ibuf);
}
/* TODO unneeded when float images are correctly treated as linear always */
if (!is_data) {
do_color_management = true;
}
}
/* currently, tpage refresh is used by ima sequences */
if (ima->tpageflag & IMA_TPAGE_REFRESH) {
GPU_free_image(ima);
ima->tpageflag &= ~IMA_TPAGE_REFRESH;
}
if (GTS.tilemode) {
/* tiled mode */
if (ima->repbind == NULL) gpu_make_repbind(ima);
if (GTS.tile >= ima->totbind) GTS.tile = 0;
/* this happens when you change repeat buttons */
if (ima->repbind && textarget == GL_TEXTURE_2D) bind = &ima->repbind[GTS.tile];
else bind = gpu_get_image_bindcode(ima, textarget);
if (*bind == 0) {
short texwindx = ibuf->x / ima->xrep;
short texwindy = ibuf->y / ima->yrep;
if (GTS.tile >= ima->xrep * ima->yrep)
GTS.tile = ima->xrep * ima->yrep - 1;
short texwinsy = GTS.tile / ima->xrep;
short texwinsx = GTS.tile - texwinsy * ima->xrep;
texwinsx *= texwindx;
texwinsy *= texwindy;
tpx = texwindx;
tpy = texwindy;
if (use_high_bit_depth) {
if (do_color_management) {
srgb_frect = MEM_mallocN(ibuf->x * ibuf->y * sizeof(float) * 4, "floar_buf_col_cor");
gpu_verify_high_bit_srgb_buffer(srgb_frect, ibuf);
frect = srgb_frect + texwinsy * ibuf->x + texwinsx;
}
else {
frect = ibuf->rect_float + texwinsy * ibuf->x + texwinsx;
}
}
else {
rect = ibuf->rect + texwinsy * ibuf->x + texwinsx;
}
}
}
else {
/* regular image mode */
bind = gpu_get_image_bindcode(ima, textarget);
if (*bind == 0) {
tpx = ibuf->x;
tpy = ibuf->y;
rect = ibuf->rect;
if (use_high_bit_depth) {
if (do_color_management) {
frect = srgb_frect = MEM_mallocN(ibuf->x * ibuf->y * sizeof(*srgb_frect) * 4, "floar_buf_col_cor");
gpu_verify_high_bit_srgb_buffer(srgb_frect, ibuf);
}
else
frect = ibuf->rect_float;
}
}
}
if (*bind != 0) {
/* enable opengl drawing with textures */
glBindTexture(textarget, *bind);
BKE_image_release_ibuf(ima, ibuf, NULL);
return *bind;
}
const int rectw = tpx;
const int recth = tpy;
unsigned *tilerect = NULL;
float *ftilerect = NULL;
/* for tiles, copy only part of image into buffer */
if (GTS.tilemode) {
if (use_high_bit_depth) {
ftilerect = MEM_mallocN(rectw * recth * sizeof(*ftilerect), "tilerect");
for (int y = 0; y < recth; y++) {
const float *frectrow = &frect[y * ibuf->x];
float *ftilerectrow = &ftilerect[y * rectw];
memcpy(ftilerectrow, frectrow, tpx * sizeof(*frectrow));
}
frect = ftilerect;
}
else {
tilerect = MEM_mallocN(rectw * recth * sizeof(*tilerect), "tilerect");
for (int y = 0; y < recth; y++) {
const unsigned *rectrow = &rect[y * ibuf->x];
unsigned *tilerectrow = &tilerect[y * rectw];
memcpy(tilerectrow, rectrow, tpx * sizeof(*rectrow));
}
rect = tilerect;
}
}
#ifdef WITH_DDS
if (ibuf->ftype == IMB_FTYPE_DDS)
GPU_create_gl_tex_compressed(bind, rect, rectw, recth, textarget, mipmap, ima, ibuf);
else
#endif
GPU_create_gl_tex(bind, rect, frect, rectw, recth, textarget, mipmap, use_high_bit_depth, ima);
/* mark as non-color data texture */
if (*bind) {
if (is_data)
ima->tpageflag |= IMA_GLBIND_IS_DATA;
else
ima->tpageflag &= ~IMA_GLBIND_IS_DATA;
}
/* clean up */
if (tilerect)
MEM_freeN(tilerect);
if (ftilerect)
MEM_freeN(ftilerect);
if (srgb_frect)
MEM_freeN(srgb_frect);
BKE_image_release_ibuf(ima, ibuf, NULL);
return *bind;
}
static void **gpu_gen_cube_map(unsigned int *rect, float *frect, int rectw, int recth, bool use_high_bit_depth)
{
size_t block_size = use_high_bit_depth ? sizeof(float) * 4 : sizeof(unsigned char) * 4;
void **sides = NULL;
int h = recth / 2;
int w = rectw / 3;
if ((use_high_bit_depth && frect == NULL) || (!use_high_bit_depth && rect == NULL) || w != h)
return sides;
/* PosX, NegX, PosY, NegY, PosZ, NegZ */
sides = MEM_mallocN(sizeof(void *) * 6, "");
for (int i = 0; i < 6; i++)
sides[i] = MEM_mallocN(block_size * w * h, "");
/* divide image into six parts */
/* ______________________
* | | | |
* | NegX | NegY | PosX |
* |______|______|______|
* | | | |
* | NegZ | PosZ | PosY |
* |______|______|______|
*/
if (use_high_bit_depth) {
float (*frectb)[4] = (float(*)[4])frect;
float (**fsides)[4] = (float(**)[4])sides;
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
memcpy(&fsides[0][x * h + y], &frectb[(recth - y - 1) * rectw + 2 * w + x], block_size);
memcpy(&fsides[1][x * h + y], &frectb[(y + h) * rectw + w - 1 - x], block_size);
memcpy(&fsides[3][y * w + x], &frectb[(recth - y - 1) * rectw + 2 * w - 1 - x], block_size);
memcpy(&fsides[5][y * w + x], &frectb[(h - y - 1) * rectw + w - 1 - x], block_size);
}
memcpy(&fsides[2][y * w], frectb[y * rectw + 2 * w], block_size * w);
memcpy(&fsides[4][y * w], frectb[y * rectw + w], block_size * w);
}
}
else {
unsigned int **isides = (unsigned int **)sides;
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
isides[0][x * h + y] = rect[(recth - y - 1) * rectw + 2 * w + x];
isides[1][x * h + y] = rect[(y + h) * rectw + w - 1 - x];
isides[3][y * w + x] = rect[(recth - y - 1) * rectw + 2 * w - 1 - x];
isides[5][y * w + x] = rect[(h - y - 1) * rectw + w - 1 - x];
}
memcpy(&isides[2][y * w], &rect[y * rectw + 2 * w], block_size * w);
memcpy(&isides[4][y * w], &rect[y * rectw + w], block_size * w);
}
}
return sides;
}
static void gpu_del_cube_map(void **cube_map)
{
int i;
if (cube_map == NULL)
return;
for (i = 0; i < 6; i++)
MEM_freeN(cube_map[i]);
MEM_freeN(cube_map);
}
/* Image *ima can be NULL */
void GPU_create_gl_tex(
unsigned int *bind, unsigned int *rect, float *frect, int rectw, int recth,
int textarget, bool mipmap, bool use_high_bit_depth, Image *ima)
{
ImBuf *ibuf = NULL;
int tpx = rectw;
int tpy = recth;
/* create image */
glGenTextures(1, (GLuint *)bind);
glBindTexture(textarget, *bind);
if (textarget == GL_TEXTURE_2D) {
if (use_high_bit_depth) {
if (GLEW_ARB_texture_float)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, rectw, recth, 0, GL_RGBA, GL_FLOAT, frect);
else
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16, rectw, recth, 0, GL_RGBA, GL_FLOAT, frect);
}
else
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, rectw, recth, 0, GL_RGBA, GL_UNSIGNED_BYTE, rect);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gpu_get_mipmap_filter(1));
if (GPU_get_mipmap() && mipmap) {
if (GTS.gpu_mipmap) {
glGenerateMipmap(GL_TEXTURE_2D);
}
else {
int i;
if (!ibuf) {
if (use_high_bit_depth) {
ibuf = IMB_allocFromBuffer(NULL, frect, tpx, tpy);
}
else {
ibuf = IMB_allocFromBuffer(rect, NULL, tpx, tpy);
}
}
IMB_makemipmap(ibuf, true);
for (i = 1; i < ibuf->miptot; i++) {
ImBuf *mip = ibuf->mipmap[i - 1];
if (use_high_bit_depth) {
if (GLEW_ARB_texture_float)
glTexImage2D(GL_TEXTURE_2D, i, GL_RGBA16F_ARB, mip->x, mip->y, 0, GL_RGBA, GL_FLOAT, mip->rect_float);
else
glTexImage2D(GL_TEXTURE_2D, i, GL_RGBA16, mip->x, mip->y, 0, GL_RGBA, GL_FLOAT, mip->rect_float);
}
else {
glTexImage2D(GL_TEXTURE_2D, i, GL_RGBA8, mip->x, mip->y, 0, GL_RGBA, GL_UNSIGNED_BYTE, mip->rect);
}
}
}
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gpu_get_mipmap_filter(0));
if (ima)
ima->tpageflag |= IMA_MIPMAP_COMPLETE;
}
else {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
}
}
else if (textarget == GL_TEXTURE_CUBE_MAP) {
int w = rectw / 3, h = recth / 2;
if (h == w && is_power_of_2_i(h) && !is_over_resolution_limit(textarget, h, w)) {
void **cube_map = gpu_gen_cube_map(rect, frect, rectw, recth, use_high_bit_depth);
GLenum informat = use_high_bit_depth ? (GLEW_ARB_texture_float ? GL_RGBA16F_ARB : GL_RGBA16) : GL_RGBA8;
GLenum type = use_high_bit_depth ? GL_FLOAT : GL_UNSIGNED_BYTE;
if (cube_map)
for (int i = 0; i < 6; i++)
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, informat, w, h, 0, GL_RGBA, type, cube_map[i]);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, gpu_get_mipmap_filter(1));
if (GPU_get_mipmap() && mipmap) {
if (GTS.gpu_mipmap) {
glGenerateMipmap(GL_TEXTURE_CUBE_MAP);
}
else {
if (!ibuf) {
if (use_high_bit_depth) {
ibuf = IMB_allocFromBuffer(NULL, frect, tpx, tpy);
}
else {
ibuf = IMB_allocFromBuffer(rect, NULL, tpx, tpy);
}
}
IMB_makemipmap(ibuf, true);
for (int i = 1; i < ibuf->miptot; i++) {
ImBuf *mip = ibuf->mipmap[i - 1];
void **mip_cube_map = gpu_gen_cube_map(
mip->rect, mip->rect_float,
mip->x, mip->y, use_high_bit_depth);
int mipw = mip->x / 3, miph = mip->y / 2;
if (mip_cube_map) {
for (int j = 0; j < 6; j++) {
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + j, i,
informat, mipw, miph, 0, GL_RGBA, type, mip_cube_map[j]);
}
}
gpu_del_cube_map(mip_cube_map);
}
}
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, gpu_get_mipmap_filter(0));
if (ima)
ima->tpageflag |= IMA_MIPMAP_COMPLETE;
}
else {
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
}
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
gpu_del_cube_map(cube_map);
}
else {
printf("Incorrect envmap size\n");
}
}
if (GLEW_EXT_texture_filter_anisotropic)
glTexParameterf(textarget, GL_TEXTURE_MAX_ANISOTROPY_EXT, GPU_get_anisotropic());
if (ibuf)
IMB_freeImBuf(ibuf);
}
/**
* GPU_upload_dxt_texture() assumes that the texture is already bound and ready to go.
* This is so the viewport and the BGE can share some code.
* Returns false if the provided ImBuf doesn't have a supported DXT compression format
*/
bool GPU_upload_dxt_texture(ImBuf *ibuf)
{
#ifdef WITH_DDS
GLint format = 0;
int blocksize, height, width, i, size, offset = 0;
width = ibuf->x;
height = ibuf->y;
if (GLEW_EXT_texture_compression_s3tc) {
if (ibuf->dds_data.fourcc == FOURCC_DXT1)
format = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
else if (ibuf->dds_data.fourcc == FOURCC_DXT3)
format = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
else if (ibuf->dds_data.fourcc == FOURCC_DXT5)
format = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
}
if (format == 0) {
fprintf(stderr, "Unable to find a suitable DXT compression, falling back to uncompressed\n");
return false;
}
if (!is_power_of_2_resolution(width, height)) {
fprintf(stderr, "Unable to load non-power-of-two DXT image resolution, falling back to uncompressed\n");
return false;
}
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gpu_get_mipmap_filter(0));
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gpu_get_mipmap_filter(1));
if (GLEW_EXT_texture_filter_anisotropic)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, GPU_get_anisotropic());
blocksize = (ibuf->dds_data.fourcc == FOURCC_DXT1) ? 8 : 16;
for (i = 0; i < ibuf->dds_data.nummipmaps && (width || height); ++i) {
if (width == 0)
width = 1;
if (height == 0)
height = 1;
size = ((width + 3) / 4) * ((height + 3) / 4) * blocksize;
glCompressedTexImage2D(GL_TEXTURE_2D, i, format, width, height,
0, size, ibuf->dds_data.data + offset);
offset += size;
width >>= 1;
height >>= 1;
}
/* set number of mipmap levels we have, needed in case they don't go down to 1x1 */
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, i - 1);
return true;
#else
(void)ibuf;
return false;
#endif
}
void GPU_create_gl_tex_compressed(
unsigned int *bind, unsigned int *pix, int x, int y,
int textarget, int mipmap, Image *ima, ImBuf *ibuf)
{
#ifndef WITH_DDS
(void)ibuf;
/* Fall back to uncompressed if DDS isn't enabled */
GPU_create_gl_tex(bind, pix, NULL, x, y, textarget, mipmap, 0, ima);
#else
glGenTextures(1, (GLuint *)bind);
glBindTexture(textarget, *bind);
if (textarget == GL_TEXTURE_2D && GPU_upload_dxt_texture(ibuf) == 0) {
glDeleteTextures(1, (GLuint *)bind);
GPU_create_gl_tex(bind, pix, NULL, x, y, textarget, mipmap, 0, ima);
}
#endif
}
/* these two functions are called on entering and exiting texture paint mode,
* temporary disabling/enabling mipmapping on all images for quick texture
* updates with glTexSubImage2D. images that didn't change don't have to be
* re-uploaded to OpenGL */
void GPU_paint_set_mipmap(bool mipmap)
{
if (!GTS.domipmap)
return;
GTS.texpaint = !mipmap;
if (mipmap) {
for (Image *ima = G.main->image.first; ima; ima = ima->id.next) {
if (BKE_image_has_bindcode(ima)) {
if (ima->tpageflag & IMA_MIPMAP_COMPLETE) {
if (ima->bindcode[TEXTARGET_TEXTURE_2D]) {
glBindTexture(GL_TEXTURE_2D, ima->bindcode[TEXTARGET_TEXTURE_2D]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gpu_get_mipmap_filter(0));
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gpu_get_mipmap_filter(1));
}
if (ima->bindcode[TEXTARGET_TEXTURE_CUBE_MAP]) {
glBindTexture(GL_TEXTURE_CUBE_MAP, ima->bindcode[TEXTARGET_TEXTURE_CUBE_MAP]);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, gpu_get_mipmap_filter(0));
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, gpu_get_mipmap_filter(1));
}
}
else
GPU_free_image(ima);
}
else
ima->tpageflag &= ~IMA_MIPMAP_COMPLETE;
}
}
else {
for (Image *ima = G.main->image.first; ima; ima = ima->id.next) {
if (BKE_image_has_bindcode(ima)) {
if (ima->bindcode[TEXTARGET_TEXTURE_2D]) {
glBindTexture(GL_TEXTURE_2D, ima->bindcode[TEXTARGET_TEXTURE_2D]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gpu_get_mipmap_filter(1));
}
if (ima->bindcode[TEXTARGET_TEXTURE_CUBE_MAP]) {
glBindTexture(GL_TEXTURE_CUBE_MAP, ima->bindcode[TEXTARGET_TEXTURE_CUBE_MAP]);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, gpu_get_mipmap_filter(1));
}
}
else
ima->tpageflag &= ~IMA_MIPMAP_COMPLETE;
}
}
}
/* check if image has been downscaled and do scaled partial update */
static bool gpu_check_scaled_image(ImBuf *ibuf, Image *ima, float *frect, int x, int y, int w, int h)
{
if (is_over_resolution_limit(GL_TEXTURE_2D, ibuf->x, ibuf->y)) {
int x_limit = smaller_power_of_2_limit(ibuf->x);
int y_limit = smaller_power_of_2_limit(ibuf->y);
float xratio = x_limit / (float)ibuf->x;
float yratio = y_limit / (float)ibuf->y;
/* find new width, height and x,y gpu texture coordinates */
/* take ceiling because we will be losing 1 pixel due to rounding errors in x,y... */
int rectw = (int)ceil(xratio * w);
int recth = (int)ceil(yratio * h);
x *= xratio;
y *= yratio;
/* ...but take back if we are over the limit! */
if (rectw + x > x_limit) rectw--;
if (recth + y > y_limit) recth--;
/* float rectangles are already continuous in memory so we can use IMB_scaleImBuf */
if (frect) {
ImBuf *ibuf_scale = IMB_allocFromBuffer(NULL, frect, w, h);
IMB_scaleImBuf(ibuf_scale, rectw, recth);
glBindTexture(GL_TEXTURE_2D, ima->bindcode[TEXTARGET_TEXTURE_2D]);
glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, rectw, recth, GL_RGBA,
GL_FLOAT, ibuf_scale->rect_float);
IMB_freeImBuf(ibuf_scale);
}
/* byte images are not continuous in memory so do manual interpolation */
else {
unsigned char *scalerect = MEM_mallocN(rectw * recth * sizeof(*scalerect) * 4, "scalerect");
unsigned int *p = (unsigned int *)scalerect;
int i, j;
float inv_xratio = 1.0f / xratio;
float inv_yratio = 1.0f / yratio;
for (i = 0; i < rectw; i++) {
float u = (x + i) * inv_xratio;
for (j = 0; j < recth; j++) {
float v = (y + j) * inv_yratio;
bilinear_interpolation_color_wrap(ibuf, (unsigned char *)(p + i + j * (rectw)), NULL, u, v);
}
}
glBindTexture(GL_TEXTURE_2D, ima->bindcode[TEXTARGET_TEXTURE_2D]);
glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, rectw, recth, GL_RGBA,
GL_UNSIGNED_BYTE, scalerect);
MEM_freeN(scalerect);
}
if (GPU_get_mipmap()) {
glGenerateMipmap(GL_TEXTURE_2D);
}
else {
ima->tpageflag &= ~IMA_MIPMAP_COMPLETE;
}
return true;
}
return false;
}
void GPU_paint_update_image(Image *ima, ImageUser *iuser, int x, int y, int w, int h)
{
ImBuf *ibuf = BKE_image_acquire_ibuf(ima, iuser, NULL);
if (ima->repbind ||
(!GTS.gpu_mipmap && GPU_get_mipmap()) ||
(ima->bindcode[TEXTARGET_TEXTURE_2D] == 0) ||
(ibuf == NULL) ||
(w == 0) || (h == 0))
{
/* these cases require full reload still */
GPU_free_image(ima);
}
else {
/* for the special case, we can do a partial update
* which is much quicker for painting */
GLint row_length, skip_pixels, skip_rows;
/* if color correction is needed, we must update the part that needs updating. */
if (ibuf->rect_float) {
float *buffer = MEM_mallocN(w * h * sizeof(float) * 4, "temp_texpaint_float_buf");
bool is_data = (ima->tpageflag & IMA_GLBIND_IS_DATA) != 0;
IMB_partial_rect_from_float(ibuf, buffer, x, y, w, h, is_data);
if (gpu_check_scaled_image(ibuf, ima, buffer, x, y, w, h)) {
MEM_freeN(buffer);
BKE_image_release_ibuf(ima, ibuf, NULL);
return;
}
glBindTexture(GL_TEXTURE_2D, ima->bindcode[TEXTARGET_TEXTURE_2D]);
glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, w, h, GL_RGBA, GL_FLOAT, buffer);
MEM_freeN(buffer);
/* we have already accounted for the case where GTS.gpu_mipmap is false
* so we will be using GPU mipmap generation here */
if (GPU_get_mipmap()) {
glGenerateMipmap(GL_TEXTURE_2D);
}
else {
ima->tpageflag &= ~IMA_MIPMAP_COMPLETE;
}
BKE_image_release_ibuf(ima, ibuf, NULL);
return;
}
if (gpu_check_scaled_image(ibuf, ima, NULL, x, y, w, h)) {
BKE_image_release_ibuf(ima, ibuf, NULL);
return;
}
glBindTexture(GL_TEXTURE_2D, ima->bindcode[TEXTARGET_TEXTURE_2D]);
glGetIntegerv(GL_UNPACK_ROW_LENGTH, &row_length);
glGetIntegerv(GL_UNPACK_SKIP_PIXELS, &skip_pixels);
glGetIntegerv(GL_UNPACK_SKIP_ROWS, &skip_rows);
glPixelStorei(GL_UNPACK_ROW_LENGTH, ibuf->x);
glPixelStorei(GL_UNPACK_SKIP_PIXELS, x);
glPixelStorei(GL_UNPACK_SKIP_ROWS, y);
glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, w, h, GL_RGBA,
GL_UNSIGNED_BYTE, ibuf->rect);
glPixelStorei(GL_UNPACK_ROW_LENGTH, row_length);
glPixelStorei(GL_UNPACK_SKIP_PIXELS, skip_pixels);
glPixelStorei(GL_UNPACK_SKIP_ROWS, skip_rows);
/* see comment above as to why we are using gpu mipmap generation here */
if (GPU_get_mipmap()) {
glGenerateMipmap(GL_TEXTURE_2D);
}
else {
ima->tpageflag &= ~IMA_MIPMAP_COMPLETE;
}
}
BKE_image_release_ibuf(ima, ibuf, NULL);
}
void GPU_update_images_framechange(void)
{
for (Image *ima = G.main->image.first; ima; ima = ima->id.next) {
if (ima->tpageflag & IMA_TWINANIM) {
if (ima->twend >= ima->xrep * ima->yrep)
ima->twend = ima->xrep * ima->yrep - 1;
/* check: is bindcode not in the array? free. (to do) */
ima->lastframe++;
if (ima->lastframe > ima->twend)
ima->lastframe = ima->twsta;
}
}
}
int GPU_update_image_time(Image *ima, double time)
{
if (!ima)
return 0;
if (ima->lastupdate < 0)
ima->lastupdate = 0;
if (ima->lastupdate > (float)time)
ima->lastupdate = (float)time;
int inc = 0;
if (ima->tpageflag & IMA_TWINANIM) {
if (ima->twend >= ima->xrep * ima->yrep) ima->twend = ima->xrep * ima->yrep - 1;
/* check: is the bindcode not in the array? Then free. (still to do) */
float diff = (float)((float)time - ima->lastupdate);
inc = (int)(diff * (float)ima->animspeed);
ima->lastupdate += ((float)inc / (float)ima->animspeed);
int newframe = ima->lastframe + inc;
if (newframe > (int)ima->twend) {
if (ima->twend - ima->twsta != 0)
newframe = (int)ima->twsta - 1 + (newframe - ima->twend) % (ima->twend - ima->twsta);
else
newframe = ima->twsta;
}
ima->lastframe = newframe;
}
return inc;
}
void GPU_free_smoke(SmokeModifierData *smd)
{
if (smd->type & MOD_SMOKE_TYPE_DOMAIN && smd->domain) {
if (smd->domain->tex)
GPU_texture_free(smd->domain->tex);
smd->domain->tex = NULL;
if (smd->domain->tex_shadow)
GPU_texture_free(smd->domain->tex_shadow);
smd->domain->tex_shadow = NULL;
if (smd->domain->tex_flame)
GPU_texture_free(smd->domain->tex_flame);
smd->domain->tex_flame = NULL;
}
}
void GPU_create_smoke(SmokeModifierData *smd, int highres)
{
#ifdef WITH_SMOKE
if (smd->type & MOD_SMOKE_TYPE_DOMAIN) {
SmokeDomainSettings *sds = smd->domain;
if (!sds->tex && !highres) {
/* rgba texture for color + density */
if (smoke_has_colors(sds->fluid)) {
float *data = MEM_callocN(sizeof(float) * sds->total_cells * 4, "smokeColorTexture");
smoke_get_rgba(sds->fluid, data, 0);
sds->tex = GPU_texture_create_3D(sds->res[0], sds->res[1], sds->res[2], data, NULL);
MEM_freeN(data);
}
/* density only */
else {
sds->tex = GPU_texture_create_3D_custom(sds->res[0], sds->res[1], sds->res[2], 1,
GPU_R8, smoke_get_density(sds->fluid), NULL);
}
sds->tex_flame = (smoke_has_fuel(sds->fluid)) ?
GPU_texture_create_3D_custom(sds->res[0], sds->res[1], sds->res[2], 1,
GPU_R8, smoke_get_flame(sds->fluid), NULL) :
NULL;
}
else if (!sds->tex && highres) {
/* rgba texture for color + density */
if (smoke_turbulence_has_colors(sds->wt)) {
float *data = MEM_callocN(sizeof(float) * smoke_turbulence_get_cells(sds->wt) * 4, "smokeColorTexture");
smoke_turbulence_get_rgba(sds->wt, data, 0);
sds->tex = GPU_texture_create_3D(sds->res_wt[0], sds->res_wt[1], sds->res_wt[2], data, NULL);
MEM_freeN(data);
}
/* density only */
else {
sds->tex = GPU_texture_create_3D_custom(sds->res_wt[0], sds->res_wt[1], sds->res_wt[2], 1,
GPU_R8, smoke_turbulence_get_density(sds->wt), NULL);
}
sds->tex_flame = (smoke_turbulence_has_fuel(sds->wt)) ?
GPU_texture_create_3D_custom(sds->res_wt[0], sds->res_wt[1], sds->res_wt[2], 1,
GPU_R8, smoke_turbulence_get_flame(sds->wt), NULL):
NULL;
}
sds->tex_shadow = GPU_texture_create_3D_custom(sds->res[0], sds->res[1], sds->res[2], 1,
GPU_R8, sds->shadow, NULL);
}
#else // WITH_SMOKE
(void)highres;
smd->domain->tex = NULL;
smd->domain->tex_flame = NULL;
smd->domain->tex_shadow = NULL;
#endif // WITH_SMOKE
}
static LinkNode *image_free_queue = NULL;
static void gpu_queue_image_for_free(Image *ima)
{
BLI_lock_thread(LOCK_OPENGL);
BLI_linklist_prepend(&image_free_queue, ima);
BLI_unlock_thread(LOCK_OPENGL);
}
void GPU_free_unused_buffers(void)
{
if (!BLI_thread_is_main())
return;
BLI_lock_thread(LOCK_OPENGL);
/* images */
for (LinkNode *node = image_free_queue; node; node = node->next) {
Image *ima = node->link;
/* check in case it was freed in the meantime */
if (G.main && BLI_findindex(&G.main->image, ima) != -1)
GPU_free_image(ima);
}
BLI_linklist_free(image_free_queue, NULL);
image_free_queue = NULL;
/* vbo buffers */
GPU_global_buffer_pool_free_unused();
BLI_unlock_thread(LOCK_OPENGL);
}
void GPU_free_image(Image *ima)
{
if (!BLI_thread_is_main()) {
gpu_queue_image_for_free(ima);
return;
}
for (int i = 0; i < TEXTARGET_COUNT; i++) {
/* free regular image binding */
if (ima->bindcode[i]) {
glDeleteTextures(1, (GLuint *)&ima->bindcode[i]);
ima->bindcode[i] = 0;
}
/* free glsl image binding */
if (ima->gputexture[i]) {
GPU_texture_free(ima->gputexture[i]);
ima->gputexture[i] = NULL;
}
}
/* free repeated image binding */
if (ima->repbind) {
glDeleteTextures(ima->totbind, (GLuint *)ima->repbind);
MEM_freeN(ima->repbind);
ima->repbind = NULL;
}
ima->tpageflag &= ~(IMA_MIPMAP_COMPLETE | IMA_GLBIND_IS_DATA);
}
void GPU_free_images(void)
{
if (G.main)
for (Image *ima = G.main->image.first; ima; ima = ima->id.next)
GPU_free_image(ima);
}
/* same as above but only free animated images */
void GPU_free_images_anim(void)
{
if (G.main)
for (Image *ima = G.main->image.first; ima; ima = ima->id.next)
if (BKE_image_is_animated(ima))
GPU_free_image(ima);
}
void GPU_free_images_old(void)
{
static int lasttime = 0;
int ctime = (int)PIL_check_seconds_timer();
/*
* Run garbage collector once for every collecting period of time
* if textimeout is 0, that's the option to NOT run the collector
*/
if (U.textimeout == 0 || ctime % U.texcollectrate || ctime == lasttime)
return;
/* of course not! */
if (G.is_rendering)
return;
lasttime = ctime;
Image *ima = G.main->image.first;
while (ima) {
if ((ima->flag & IMA_NOCOLLECT) == 0 && ctime - ima->lastused > U.textimeout) {
/* If it's in GL memory, deallocate and set time tag to current time
* This gives textures a "second chance" to be used before dying. */
if (BKE_image_has_bindcode(ima) || ima->repbind) {
GPU_free_image(ima);
ima->lastused = ctime;
}
/* Otherwise, just kill the buffers */
else {
BKE_image_free_buffers(ima);
}
}
ima = ima->id.next;
}
}
/* OpenGL Materials */
#define FIXEDMAT 8
/* OpenGL state caching for materials */
typedef struct GPUMaterialFixed {
float diff[3];
float spec[3];
int hard;
float alpha;
} GPUMaterialFixed;
static struct GPUMaterialState {
GPUMaterialFixed (*matbuf);
GPUMaterialFixed matbuf_fixed[FIXEDMAT];
int totmat;
/* set when called inside GPU_begin_object_materials / GPU_end_object_materials
* otherwise calling GPU_object_material_bind returns zero */
bool is_enabled;
Material **gmatbuf;
Material *gmatbuf_fixed[FIXEDMAT];
Material *gboundmat;
Object *gob;
DupliObject *dob;
Scene *gscene;
int glay;
bool gscenelock;
float (*gviewmat)[4];
float (*gviewinv)[4];
float (*gviewcamtexcofac);
bool backface_culling;
bool two_sided_lighting;
GPUBlendMode *alphablend;
GPUBlendMode alphablend_fixed[FIXEDMAT];
bool use_alpha_pass, is_alpha_pass;
bool use_matcaps;
int lastmatnr, lastretval;
GPUBlendMode lastalphablend;
bool is_opensubdiv;
} GMS = {NULL};
/* fixed function material, alpha handed by caller */
static void gpu_material_to_fixed(
GPUMaterialFixed *smat, const Material *bmat, const int gamma, const Object *ob,
const int new_shading_nodes, const bool dimdown)
{
if (bmat->mode & MA_SHLESS) {
copy_v3_v3(smat->diff, &bmat->r);
if (gamma)
linearrgb_to_srgb_v3_v3(smat->diff, smat->diff);
zero_v3(smat->spec);
smat->alpha = 1.0f;
smat->hard = 0;
}
else if (new_shading_nodes) {
copy_v3_v3(smat->diff, &bmat->r);
copy_v3_v3(smat->spec, &bmat->specr);
smat->alpha = 1.0f;
smat->hard = CLAMPIS(bmat->har, 0, 128);
if (dimdown) {
mul_v3_fl(smat->diff, 0.8f);
mul_v3_fl(smat->spec, 0.5f);
}
if (gamma) {
linearrgb_to_srgb_v3_v3(smat->diff, smat->diff);
linearrgb_to_srgb_v3_v3(smat->spec, smat->spec);
}
}
else {
mul_v3_v3fl(smat->diff, &bmat->r, bmat->ref + bmat->emit);
if (bmat->shade_flag & MA_OBCOLOR)
mul_v3_v3(smat->diff, ob->col);
mul_v3_v3fl(smat->spec, &bmat->specr, bmat->spec);
smat->hard = CLAMPIS(bmat->har, 1, 128);
smat->alpha = 1.0f;
if (gamma) {
linearrgb_to_srgb_v3_v3(smat->diff, smat->diff);
linearrgb_to_srgb_v3_v3(smat->spec, smat->spec);
}
}
}
static Material *gpu_active_node_material(Material *ma)
{
if (ma && ma->use_nodes && ma->nodetree) {
bNode *node = nodeGetActiveID(ma->nodetree, ID_MA);
if (node)
return (Material *)node->id;
else
return NULL;
}
return ma;
}
void GPU_begin_dupli_object(DupliObject *dob)
{
GMS.dob = dob;
}
void GPU_end_dupli_object(void)
{
GMS.dob = NULL;
}
void GPU_begin_object_materials(
View3D *v3d, RegionView3D *rv3d, Scene *scene, SceneLayer *sl, Object *ob,
bool glsl, bool *do_alpha_after)
{
Material *ma;
GPUMaterial *gpumat;
GPUBlendMode alphablend;
DupliObject *dob;
int a;
const bool gamma = BKE_scene_check_color_management_enabled(scene);
const bool new_shading_nodes = BKE_scene_use_new_shading_nodes(scene);
const bool use_matcap = (v3d->flag2 & V3D_SHOW_SOLID_MATCAP) != 0; /* assumes v3d->defmaterial->preview is set */
bool use_opensubdiv = false;
#ifdef WITH_OPENSUBDIV
{
DerivedMesh *derivedFinal = NULL;
if (ob->type == OB_MESH) {
Mesh *me = ob->data;
BMEditMesh *em = me->edit_btmesh;
if (em != NULL) {
derivedFinal = em->derivedFinal;
}
else {
derivedFinal = ob->derivedFinal;
}
}
else {
derivedFinal = ob->derivedFinal;
}
if (derivedFinal != NULL && derivedFinal->type == DM_TYPE_CCGDM) {
CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) derivedFinal;
use_opensubdiv = ccgdm->useGpuBackend;
}
}
#endif
#ifdef WITH_GAMEENGINE
if (rv3d->rflag & RV3D_IS_GAME_ENGINE) {
ob = BKE_object_lod_matob_get(ob, sl);
}
#else
UNUSED_VARS(sl);
#endif
/* initialize state */
/* DupliObject must be restored */
dob = GMS.dob;
memset(&GMS, 0, sizeof(GMS));
GMS.is_enabled = true;
GMS.dob = dob;
GMS.lastmatnr = -1;
GMS.lastretval = -1;
GMS.lastalphablend = GPU_BLEND_SOLID;
GMS.use_matcaps = use_matcap;
GMS.backface_culling = (v3d->flag2 & V3D_BACKFACE_CULLING) != 0;
GMS.two_sided_lighting = false;
if (ob && ob->type == OB_MESH)
GMS.two_sided_lighting = (((Mesh *)ob->data)->flag & ME_TWOSIDED) != 0;
GMS.gob = ob;
GMS.gscene = scene;
GMS.is_opensubdiv = use_opensubdiv;
GMS.totmat = use_matcap ? 1 : ob->totcol + 1; /* materials start from 1, default material is 0 */
GMS.glay = (v3d->localvd) ? v3d->localvd->lay : v3d->lay; /* keep lamps visible in local view */
GMS.gscenelock = (v3d->scenelock != 0);
GMS.gviewmat = rv3d->viewmat;
GMS.gviewinv = rv3d->viewinv;
GMS.gviewcamtexcofac = rv3d->viewcamtexcofac;
/* alpha pass setup. there's various cases to handle here:
* - object transparency on: only solid materials draw in the first pass,
* and only transparent in the second 'alpha' pass.
* - object transparency off: for glsl we draw both in a single pass, and
* for solid we don't use transparency at all. */
GMS.use_alpha_pass = (do_alpha_after != NULL);
GMS.is_alpha_pass = (v3d->transp != false);
if (GMS.use_alpha_pass)
*do_alpha_after = false;
if (GMS.totmat > FIXEDMAT) {
GMS.matbuf = MEM_callocN(sizeof(GPUMaterialFixed) * GMS.totmat, "GMS.matbuf");
GMS.gmatbuf = MEM_callocN(sizeof(*GMS.gmatbuf) * GMS.totmat, "GMS.matbuf");
GMS.alphablend = MEM_callocN(sizeof(*GMS.alphablend) * GMS.totmat, "GMS.matbuf");
}
else {
GMS.matbuf = GMS.matbuf_fixed;
GMS.gmatbuf = GMS.gmatbuf_fixed;
GMS.alphablend = GMS.alphablend_fixed;
}
/* viewport material, setup in space_view3d, defaults to matcap using ma->preview now */
if (use_matcap) {
GMS.gmatbuf[0] = v3d->defmaterial;
GPU_material_matcap(scene, v3d->defmaterial, use_opensubdiv);
/* do material 1 too, for displists! */
memcpy(&GMS.matbuf[1], &GMS.matbuf[0], sizeof(GPUMaterialFixed));
GMS.alphablend[0] = GPU_BLEND_SOLID;
}
else {
/* no materials assigned? */
if (ob->totcol == 0) {
gpu_material_to_fixed(&GMS.matbuf[0], &defmaterial, 0, ob, new_shading_nodes, true);
/* do material 1 too, for displists! */
memcpy(&GMS.matbuf[1], &GMS.matbuf[0], sizeof(GPUMaterialFixed));
if (glsl) {
GMS.gmatbuf[0] = &defmaterial;
GPU_material_from_blender(GMS.gscene, &defmaterial, GMS.is_opensubdiv);
}
GMS.alphablend[0] = GPU_BLEND_SOLID;
}
/* setup materials */
for (a = 1; a <= ob->totcol; a++) {
/* find a suitable material */
ma = give_current_material(ob, a);
if (!glsl && !new_shading_nodes) ma = gpu_active_node_material(ma);
if (ma == NULL) ma = &defmaterial;
/* create glsl material if requested */
gpumat = glsl ? GPU_material_from_blender(GMS.gscene, ma, GMS.is_opensubdiv) : NULL;
if (gpumat) {
/* do glsl only if creating it succeed, else fallback */
GMS.gmatbuf[a] = ma;
alphablend = GPU_material_alpha_blend(gpumat, ob->col);
}
else {
/* fixed function opengl materials */
gpu_material_to_fixed(&GMS.matbuf[a], ma, gamma, ob, new_shading_nodes, false);
if (GMS.use_alpha_pass && ((ma->mode & MA_TRANSP) || (new_shading_nodes && ma->alpha != 1.0f))) {
GMS.matbuf[a].alpha = ma->alpha;
alphablend = (ma->alpha == 1.0f) ? GPU_BLEND_SOLID: GPU_BLEND_ALPHA;
}
else {
GMS.matbuf[a].alpha = 1.0f;
alphablend = GPU_BLEND_SOLID;
}
}
/* setting 'do_alpha_after = true' indicates this object needs to be
* drawn in a second alpha pass for improved blending */
if (do_alpha_after && !GMS.is_alpha_pass)
if (ELEM(alphablend, GPU_BLEND_ALPHA, GPU_BLEND_ADD, GPU_BLEND_ALPHA_SORT))
*do_alpha_after = true;
GMS.alphablend[a] = alphablend;
}
}
/* let's start with a clean state */
GPU_object_material_unbind();
}
static int gpu_get_particle_info(GPUParticleInfo *pi)
{
DupliObject *dob = GMS.dob;
if (dob->particle_system) {
int ind;
if (dob->persistent_id[0] < dob->particle_system->totpart)
ind = dob->persistent_id[0];
else {
ind = dob->particle_system->child[dob->persistent_id[0] - dob->particle_system->totpart].parent;
}
if (ind >= 0) {
ParticleData *p = &dob->particle_system->particles[ind];
pi->scalprops[0] = ind;
pi->scalprops[1] = GMS.gscene->r.cfra - p->time;
pi->scalprops[2] = p->lifetime;
pi->scalprops[3] = p->size;
copy_v3_v3(pi->location, p->state.co);
copy_v3_v3(pi->velocity, p->state.vel);
copy_v3_v3(pi->angular_velocity, p->state.ave);
return 1;
}
else return 0;
}
else
return 0;
}
static void GPU_get_object_info(float oi[3], Material *mat)
{
Object *ob = GMS.gob;
oi[0] = ob->index;
oi[1] = mat->index;
unsigned int random;
if (GMS.dob) {
random = GMS.dob->random_id;
}
else {
random = BLI_hash_int_2d(BLI_hash_string(GMS.gob->id.name + 2), 0);
}
oi[2] = random * (1.0f / (float)0xFFFFFFFF);
}
int GPU_object_material_bind(int nr, void *attribs)
{
GPUVertexAttribs *gattribs = attribs;
/* no GPU_begin_object_materials, use default material */
if (!GMS.matbuf) {
memset(&GMS, 0, sizeof(GMS));
float diffuse[3], specular[3];
mul_v3_v3fl(diffuse, &defmaterial.r, defmaterial.ref + defmaterial.emit);
mul_v3_v3fl(specular, &defmaterial.specr, defmaterial.spec);
GPU_basic_shader_colors(diffuse, specular, 35, 1.0f);
if (GMS.two_sided_lighting)
GPU_basic_shader_bind(GPU_SHADER_LIGHTING | GPU_SHADER_TWO_SIDED);
else
GPU_basic_shader_bind(GPU_SHADER_LIGHTING);
return 0;
}
/* prevent index to use un-initialized array items */
if (nr >= GMS.totmat)
nr = 0;
if (gattribs)
memset(gattribs, 0, sizeof(*gattribs));
/* keep current material */
if (nr == GMS.lastmatnr)
return GMS.lastretval;
/* unbind glsl material */
if (GMS.gboundmat) {
if (GMS.is_alpha_pass) glDepthMask(0);
GPU_material_unbind(GPU_material_from_blender(GMS.gscene, GMS.gboundmat, GMS.is_opensubdiv));
GMS.gboundmat = NULL;
}
/* draw materials with alpha in alpha pass */
GMS.lastmatnr = nr;
GMS.lastretval = 1;
if (GMS.use_alpha_pass) {
GMS.lastretval = ELEM(GMS.alphablend[nr], GPU_BLEND_SOLID, GPU_BLEND_CLIP);
if (GMS.is_alpha_pass)
GMS.lastretval = !GMS.lastretval;
}
else
GMS.lastretval = !GMS.is_alpha_pass;
if (GMS.lastretval) {
/* for alpha pass, use alpha blend */
GPUBlendMode alphablend = GMS.alphablend[nr];
if (gattribs && GMS.gmatbuf[nr]) {
/* bind glsl material and get attributes */
Material *mat = GMS.gmatbuf[nr];
GPUParticleInfo partile_info;
float object_info[3] = {0};
float auto_bump_scale;
GPUMaterial *gpumat = GPU_material_from_blender(GMS.gscene, mat, GMS.is_opensubdiv);
GPU_material_vertex_attributes(gpumat, gattribs);
if (GMS.dob) {
gpu_get_particle_info(&partile_info);
}
if ((GPU_get_material_builtins(gpumat) & GPU_OBJECT_INFO) != 0) {
GPU_get_object_info(object_info, mat);
}
GPU_material_bind(
gpumat, GMS.gob->lay, GMS.glay, 1.0, !(GMS.gob->mode & OB_MODE_TEXTURE_PAINT),
GMS.gviewmat, GMS.gviewinv, GMS.gviewcamtexcofac, GMS.gscenelock);
auto_bump_scale = GMS.gob->derivedFinal != NULL ? GMS.gob->derivedFinal->auto_bump_scale : 1.0f;
GPU_material_bind_uniforms(gpumat, GMS.gob->obmat, GMS.gviewmat, GMS.gob->col, auto_bump_scale, &partile_info, object_info);
GMS.gboundmat = mat;
/* for glsl use alpha blend mode, unless it's set to solid and
* we are already drawing in an alpha pass */
if (mat->game.alpha_blend != GPU_BLEND_SOLID)
alphablend = mat->game.alpha_blend;
if (GMS.is_alpha_pass) glDepthMask(1);
if (GMS.backface_culling) {
if (mat->game.flag)
glEnable(GL_CULL_FACE);
else
glDisable(GL_CULL_FACE);
}
if (GMS.use_matcaps)
glColor3f(1.0f, 1.0f, 1.0f);
}
else {
/* or do fixed function opengl material */
GPU_basic_shader_colors(
GMS.matbuf[nr].diff,
GMS.matbuf[nr].spec, GMS.matbuf[nr].hard, GMS.matbuf[nr].alpha);
if (GMS.two_sided_lighting)
GPU_basic_shader_bind(GPU_SHADER_LIGHTING | GPU_SHADER_TWO_SIDED);
else
GPU_basic_shader_bind(GPU_SHADER_LIGHTING);
}
/* set (alpha) blending mode */
GPU_set_material_alpha_blend(alphablend);
}
return GMS.lastretval;
}
int GPU_object_material_visible(int nr, void *attribs)
{
GPUVertexAttribs *gattribs = attribs;
int visible;
if (!GMS.matbuf)
return 0;
if (gattribs)
memset(gattribs, 0, sizeof(*gattribs));
if (nr >= GMS.totmat)
nr = 0;
if (GMS.use_alpha_pass) {
visible = ELEM(GMS.alphablend[nr], GPU_BLEND_SOLID, GPU_BLEND_CLIP);
if (GMS.is_alpha_pass)
visible = !visible;
}
else
visible = !GMS.is_alpha_pass;
return visible;
}
void GPU_set_material_alpha_blend(int alphablend)
{
if (GMS.lastalphablend == alphablend)
return;
gpu_set_alpha_blend(alphablend);
GMS.lastalphablend = alphablend;
}
int GPU_get_material_alpha_blend(void)
{
return GMS.lastalphablend;
}
void GPU_object_material_unbind(void)
{
GMS.lastmatnr = -1;
GMS.lastretval = 1;
if (GMS.gboundmat) {
if (GMS.backface_culling)
glDisable(GL_CULL_FACE);
if (GMS.is_alpha_pass) glDepthMask(0);
GPU_material_unbind(GPU_material_from_blender(GMS.gscene, GMS.gboundmat, GMS.is_opensubdiv));
GMS.gboundmat = NULL;
}
else
GPU_basic_shader_bind(GPU_SHADER_USE_COLOR);
GPU_set_material_alpha_blend(GPU_BLEND_SOLID);
}
void GPU_material_diffuse_get(int nr, float diff[4])
{
/* prevent index to use un-initialized array items */
if (nr >= GMS.totmat)
nr = 0;
/* no GPU_begin_object_materials, use default material */
if (!GMS.matbuf) {
mul_v3_v3fl(diff, &defmaterial.r, defmaterial.ref + defmaterial.emit);
}
else {
copy_v3_v3(diff, GMS.matbuf[nr].diff);
diff[3] = GMS.matbuf[nr].alpha;
}
}
bool GPU_material_use_matcaps_get(void)
{
return GMS.use_matcaps;
}
bool GPU_object_materials_check(void)
{
return GMS.is_enabled;
}
void GPU_end_object_materials(void)
{
GPU_object_material_unbind();
GMS.is_enabled = false;
if (GMS.matbuf && GMS.matbuf != GMS.matbuf_fixed) {
MEM_freeN(GMS.matbuf);
MEM_freeN(GMS.gmatbuf);
MEM_freeN(GMS.alphablend);
}
GMS.matbuf = NULL;
GMS.gmatbuf = NULL;
GMS.alphablend = NULL;
GMS.two_sided_lighting = false;
}
/* Lights */
int GPU_default_lights(void)
{
/* initialize */
if (U.light[0].flag == 0 && U.light[1].flag == 0 && U.light[2].flag == 0) {
U.light[0].flag = 1;
U.light[0].vec[0] = -0.3; U.light[0].vec[1] = 0.3; U.light[0].vec[2] = 0.9;
U.light[0].col[0] = 0.8; U.light[0].col[1] = 0.8; U.light[0].col[2] = 0.8;
U.light[0].spec[0] = 0.5; U.light[0].spec[1] = 0.5; U.light[0].spec[2] = 0.5;
U.light[0].spec[3] = 1.0;
U.light[1].flag = 0;
U.light[1].vec[0] = 0.5; U.light[1].vec[1] = 0.5; U.light[1].vec[2] = 0.1;
U.light[1].col[0] = 0.4; U.light[1].col[1] = 0.4; U.light[1].col[2] = 0.8;
U.light[1].spec[0] = 0.3; U.light[1].spec[1] = 0.3; U.light[1].spec[2] = 0.5;
U.light[1].spec[3] = 1.0;
U.light[2].flag = 0;
U.light[2].vec[0] = 0.3; U.light[2].vec[1] = -0.3; U.light[2].vec[2] = -0.2;
U.light[2].col[0] = 0.8; U.light[2].col[1] = 0.5; U.light[2].col[2] = 0.4;
U.light[2].spec[0] = 0.5; U.light[2].spec[1] = 0.4; U.light[2].spec[2] = 0.3;
U.light[2].spec[3] = 1.0;
}
GPU_basic_shader_light_set_viewer(false);
int count = 0;
for (int a = 0; a < 8; a++) {
if (a < 3 && U.light[a].flag) {
GPULightData light = {0};
light.type = GPU_LIGHT_SUN;
normalize_v3_v3(light.direction, U.light[a].vec);
copy_v3_v3(light.diffuse, U.light[a].col);
copy_v3_v3(light.specular, U.light[a].spec);
GPU_basic_shader_light_set(a, &light);
count++;
}
else
GPU_basic_shader_light_set(a, NULL);
}
return count;
}
int GPU_scene_object_lights(SceneLayer *sl, float viewmat[4][4], int ortho)
{
/* disable all lights */
for (int count = 0; count < 8; count++)
GPU_basic_shader_light_set(count, NULL);
/* view direction for specular is not computed correct by default in
* opengl, so we set the settings ourselfs */
GPU_basic_shader_light_set_viewer(!ortho);
int count = 0;
for (Base *base = FIRSTBASE_NEW; base; base = base->next) {
if (base->object->type != OB_LAMP)
continue;
Lamp *la = base->object->data;
/* setup lamp transform */
gpuPushMatrix();
gpuLoadMatrix(viewmat);
/* setup light */
GPULightData light = {0};
mul_v3_v3fl(light.diffuse, &la->r, la->energy);
mul_v3_v3fl(light.specular, &la->r, la->energy);
if (la->type == LA_SUN) {
/* directional sun light */
light.type = GPU_LIGHT_SUN;
normalize_v3_v3(light.direction, base->object->obmat[2]);
}
else {
/* other lamps with position attenuation */
copy_v3_v3(light.position, base->object->obmat[3]);
light.constant_attenuation = 1.0f;
light.linear_attenuation = la->att1 / la->dist;
light.quadratic_attenuation = la->att2 / (la->dist * la->dist);
if (la->type == LA_SPOT) {
light.type = GPU_LIGHT_SPOT;
negate_v3_v3(light.direction, base->object->obmat[2]);
normalize_v3(light.direction);
light.spot_cutoff = RAD2DEGF(la->spotsize * 0.5f);
light.spot_exponent = 128.0f * la->spotblend;
}
else
light.type = GPU_LIGHT_POINT;
}
GPU_basic_shader_light_set(count, &light);
gpuPopMatrix();
count++;
if (count == 8)
break;
}
return count;
}
static void gpu_multisample(bool enable)
{
#ifdef __linux__
/* changing multisample from the default (enabled) causes problems on some
* systems (NVIDIA/Linux) when the pixel format doesn't have a multisample buffer */
bool toggle_ok = true;
if (GPU_type_matches(GPU_DEVICE_NVIDIA, GPU_OS_UNIX, GPU_DRIVER_ANY)) {
int samples = 0;
glGetIntegerv(GL_SAMPLES, &samples);
if (samples == 0)
toggle_ok = false;
}
if (toggle_ok) {
if (enable)
glEnable(GL_MULTISAMPLE);
else
glDisable(GL_MULTISAMPLE);
}
#else
if (enable)
glEnable(GL_MULTISAMPLE);
else
glDisable(GL_MULTISAMPLE);
#endif
}
/* Default OpenGL State
*
* This is called on startup, for opengl offscreen render and to restore state
* for the game engine. Generally we should always return to this state when
* temporarily modifying the state for drawing, though that are (undocumented)
* exceptions that we should try to get rid of. */
void GPU_state_init(void)
{
GPU_default_lights();
GPU_disable_program_point_size();
glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
glDepthFunc(GL_LEQUAL);
glDisable(GL_BLEND);
glDisable(GL_DEPTH_TEST);
glDisable(GL_COLOR_LOGIC_OP);
glDisable(GL_STENCIL_TEST);
glDepthRange(0.0, 1.0);
glFrontFace(GL_CCW);
glCullFace(GL_BACK);
glDisable(GL_CULL_FACE);
gpu_multisample(false);
}
void GPU_enable_program_point_size(void)
{
glEnable(GL_PROGRAM_POINT_SIZE);
}
void GPU_disable_program_point_size(void)
{
glDisable(GL_PROGRAM_POINT_SIZE);
}
#ifdef WITH_OPENSUBDIV
/* Update face-varying variables offset which might be
* different from mesh to mesh sharing the same material.
*/
void GPU_draw_update_fvar_offset(DerivedMesh *dm)
{
/* Sanity check to be sure we only do this for OpenSubdiv draw. */
BLI_assert(dm->type == DM_TYPE_CCGDM);
BLI_assert(GMS.is_opensubdiv);
for (int i = 0; i < GMS.totmat; ++i) {
Material *material = GMS.gmatbuf[i];
GPUMaterial *gpu_material;
if (material == NULL) {
continue;
}
gpu_material = GPU_material_from_blender(GMS.gscene,
material,
GMS.is_opensubdiv);
GPU_material_update_fvar_offset(gpu_material, dm);
}
}
#endif
/** \name Framebuffer color depth, for selection codes
* \{ */
#ifdef __APPLE__
/* apple seems to round colors to below and up on some configs */
static unsigned int index_to_framebuffer(int index)
{
unsigned int i = index;
switch (GPU_color_depth()) {
case 12:
i = ((i & 0xF00) << 12) + ((i & 0xF0) << 8) + ((i & 0xF) << 4);
/* sometimes dithering subtracts! */
i |= 0x070707;
break;
case 15:
case 16:
i = ((i & 0x7C00) << 9) + ((i & 0x3E0) << 6) + ((i & 0x1F) << 3);
i |= 0x030303;
break;
case 24:
break;
default: /* 18 bits... */
i = ((i & 0x3F000) << 6) + ((i & 0xFC0) << 4) + ((i & 0x3F) << 2);
i |= 0x010101;
break;
}
return i;
}
#else
/* this is the old method as being in use for ages.... seems to work? colors are rounded to lower values */
static unsigned int index_to_framebuffer(int index)
{
unsigned int i = index;
switch (GPU_color_depth()) {
case 8:
i = ((i & 48) << 18) + ((i & 12) << 12) + ((i & 3) << 6);
i |= 0x3F3F3F;
break;
case 12:
i = ((i & 0xF00) << 12) + ((i & 0xF0) << 8) + ((i & 0xF) << 4);
/* sometimes dithering subtracts! */
i |= 0x0F0F0F;
break;
case 15:
case 16:
i = ((i & 0x7C00) << 9) + ((i & 0x3E0) << 6) + ((i & 0x1F) << 3);
i |= 0x070707;
break;
case 24:
break;
default: /* 18 bits... */
i = ((i & 0x3F000) << 6) + ((i & 0xFC0) << 4) + ((i & 0x3F) << 2);
i |= 0x030303;
break;
}
return i;
}
#endif
void GPU_select_index_set(int index)
{
const int col = index_to_framebuffer(index);
glColor3ub(( (col) & 0xFF),
(((col) >> 8) & 0xFF),
(((col) >> 16) & 0xFF));
}
void GPU_select_index_get(int index, int *r_col)
{
const int col = index_to_framebuffer(index);
char *c_col = (char *)r_col;
c_col[0] = (col & 0xFF); /* red */
c_col[1] = ((col >> 8) & 0xFF); /* green */
c_col[2] = ((col >> 16) & 0xFF); /* blue */
c_col[3] = 0xFF; /* alpha */
}
#define INDEX_FROM_BUF_8(col) ((((col) & 0xC00000) >> 18) + (((col) & 0xC000) >> 12) + (((col) & 0xC0) >> 6))
#define INDEX_FROM_BUF_12(col) ((((col) & 0xF00000) >> 12) + (((col) & 0xF000) >> 8) + (((col) & 0xF0) >> 4))
#define INDEX_FROM_BUF_15_16(col) ((((col) & 0xF80000) >> 9) + (((col) & 0xF800) >> 6) + (((col) & 0xF8) >> 3))
#define INDEX_FROM_BUF_18(col) ((((col) & 0xFC0000) >> 6) + (((col) & 0xFC00) >> 4) + (((col) & 0xFC) >> 2))
#define INDEX_FROM_BUF_24(col) ((col) & 0xFFFFFF)
int GPU_select_to_index(unsigned int col)
{
if (col == 0) {
return 0;
}
switch (GPU_color_depth()) {
case 8: return INDEX_FROM_BUF_8(col);
case 12: return INDEX_FROM_BUF_12(col);
case 15:
case 16: return INDEX_FROM_BUF_15_16(col);
case 24: return INDEX_FROM_BUF_24(col);
default: return INDEX_FROM_BUF_18(col);
}
}
void GPU_select_to_index_array(unsigned int *col, const unsigned int size)
{
#define INDEX_BUF_ARRAY(INDEX_FROM_BUF_BITS) \
for (i = size; i--; col++) { \
if ((c = *col)) { \
*col = INDEX_FROM_BUF_BITS(c); \
} \
} ((void)0)
if (size > 0) {
unsigned int i, c;
switch (GPU_color_depth()) {
case 8:
INDEX_BUF_ARRAY(INDEX_FROM_BUF_8);
break;
case 12:
INDEX_BUF_ARRAY(INDEX_FROM_BUF_12);
break;
case 15:
case 16:
INDEX_BUF_ARRAY(INDEX_FROM_BUF_15_16);
break;
case 24:
INDEX_BUF_ARRAY(INDEX_FROM_BUF_24);
break;
default:
INDEX_BUF_ARRAY(INDEX_FROM_BUF_18);
break;
}
}
#undef INDEX_BUF_ARRAY
}
#define STATE_STACK_DEPTH 16
typedef struct {
eGPUAttribMask mask;
/* GL_ENABLE_BIT */
unsigned int is_blend : 1;
unsigned int is_cull_face : 1;
unsigned int is_depth_test : 1;
unsigned int is_dither : 1;
unsigned int is_lighting : 1;
unsigned int is_line_smooth : 1;
unsigned int is_color_logic_op : 1;
unsigned int is_multisample : 1;
unsigned int is_polygon_offset_line : 1;
unsigned int is_polygon_offset_fill : 1;
unsigned int is_polygon_smooth : 1;
unsigned int is_sample_alpha_to_coverage : 1;
unsigned int is_scissor_test : 1;
unsigned int is_stencil_test : 1;
bool is_clip_plane[6];
/* GL_DEPTH_BUFFER_BIT */
/* unsigned int is_depth_test : 1; */
int depth_func;
double depth_clear_value;
bool depth_write_mask;
/* GL_SCISSOR_BIT */
int scissor_box[4];
/* unsigned int is_scissor_test : 1; */
/* GL_VIEWPORT_BIT */
int viewport[4];
double near_far[2];
} GPUAttribValues;
typedef struct {
GPUAttribValues attrib_stack[STATE_STACK_DEPTH];
unsigned int top;
} GPUAttribStack;
static GPUAttribStack state = {
.top = 0
};
#define AttribStack state
#define Attrib state.attrib_stack[state.top]
/**
* Replacement for glPush/PopAttributes
*
* We don't need to cover all the options of legacy OpenGL
* but simply the ones used by Blender.
*/
void gpuPushAttrib(eGPUAttribMask mask)
{
Attrib.mask = mask;
if ((mask & GPU_DEPTH_BUFFER_BIT) != 0) {
Attrib.is_depth_test = glIsEnabled(GL_DEPTH_TEST);
glGetIntegerv(GL_DEPTH_FUNC, &Attrib.depth_func);
glGetDoublev(GL_DEPTH_CLEAR_VALUE, &Attrib.depth_clear_value);
glGetBooleanv(GL_DEPTH_WRITEMASK, (GLboolean *)&Attrib.depth_write_mask);
}
if ((mask & GPU_ENABLE_BIT) != 0) {
Attrib.is_blend = glIsEnabled(GL_BLEND);
for (int i = 0; i < 6; i++) {
Attrib.is_clip_plane[i] = glIsEnabled(GL_CLIP_PLANE0 + i);
}
Attrib.is_cull_face = glIsEnabled(GL_CULL_FACE);
Attrib.is_depth_test = glIsEnabled(GL_DEPTH_TEST);
Attrib.is_dither = glIsEnabled(GL_DITHER);
Attrib.is_line_smooth = glIsEnabled(GL_LINE_SMOOTH);
Attrib.is_color_logic_op = glIsEnabled(GL_COLOR_LOGIC_OP);
Attrib.is_multisample = glIsEnabled(GL_MULTISAMPLE);
Attrib.is_polygon_offset_line = glIsEnabled(GL_POLYGON_OFFSET_LINE);
Attrib.is_polygon_offset_fill = glIsEnabled(GL_POLYGON_OFFSET_FILL);
Attrib.is_polygon_smooth = glIsEnabled(GL_POLYGON_SMOOTH);
Attrib.is_sample_alpha_to_coverage = glIsEnabled(GL_SAMPLE_ALPHA_TO_COVERAGE);
Attrib.is_scissor_test = glIsEnabled(GL_SCISSOR_TEST);
Attrib.is_stencil_test = glIsEnabled(GL_STENCIL_TEST);
}
if ((mask & GPU_SCISSOR_BIT) != 0) {
Attrib.is_scissor_test = glIsEnabled(GL_SCISSOR_TEST);
glGetIntegerv(GL_SCISSOR_BOX, (GLint *)&Attrib.scissor_box);
}
if ((mask & GPU_VIEWPORT_BIT) != 0) {
glGetDoublev(GL_DEPTH_RANGE, (GLdouble *)&Attrib.near_far);
glGetIntegerv(GL_VIEWPORT, (GLint *)&Attrib.viewport);
}
if ((mask & GPU_BLEND_BIT) != 0) {
Attrib.is_blend = glIsEnabled(GL_BLEND);
}
BLI_assert(AttribStack.top < STATE_STACK_DEPTH);
AttribStack.top++;
}
static void restore_mask(GLenum cap, const bool value) {
if (value) {
glEnable(cap);
}
else {
glDisable(cap);
}
}
void gpuPopAttrib(void)
{
BLI_assert(AttribStack.top > 0);
AttribStack.top--;
GLint mask = Attrib.mask;
if ((mask & GPU_DEPTH_BUFFER_BIT) != 0) {
restore_mask(GL_DEPTH_TEST, Attrib.is_depth_test);
glDepthFunc(Attrib.depth_func);
glClearDepth(Attrib.depth_clear_value);
glDepthMask(Attrib.depth_write_mask);
}
if ((mask & GPU_ENABLE_BIT) != 0) {
restore_mask(GL_BLEND, Attrib.is_blend);
for (int i = 0; i < 6; i++) {
restore_mask(GL_CLIP_PLANE0 + i, Attrib.is_clip_plane[i]);
}
restore_mask(GL_CULL_FACE, Attrib.is_cull_face);
restore_mask(GL_DEPTH_TEST, Attrib.is_depth_test);
restore_mask(GL_DITHER, Attrib.is_dither);
restore_mask(GL_LINE_SMOOTH, Attrib.is_line_smooth);
restore_mask(GL_COLOR_LOGIC_OP, Attrib.is_color_logic_op);
restore_mask(GL_MULTISAMPLE, Attrib.is_multisample);
restore_mask(GL_POLYGON_OFFSET_LINE, Attrib.is_polygon_offset_line);
restore_mask(GL_POLYGON_OFFSET_FILL, Attrib.is_polygon_offset_fill);
restore_mask(GL_POLYGON_SMOOTH, Attrib.is_polygon_smooth);
restore_mask(GL_SAMPLE_ALPHA_TO_COVERAGE, Attrib.is_sample_alpha_to_coverage);
restore_mask(GL_SCISSOR_TEST, Attrib.is_scissor_test);
restore_mask(GL_STENCIL_TEST, Attrib.is_stencil_test);
}
if ((mask & GPU_VIEWPORT_BIT) != 0) {
glViewport(Attrib.viewport[0], Attrib.viewport[1], Attrib.viewport[2], Attrib.viewport[3]);
glDepthRange(Attrib.near_far[0], Attrib.near_far[1]);
}
if ((mask & GPU_SCISSOR_BIT) != 0) {
restore_mask(GL_SCISSOR_TEST, Attrib.is_scissor_test);
glScissor(Attrib.scissor_box[0], Attrib.scissor_box[1], Attrib.scissor_box[2], Attrib.scissor_box[3]);
}
if ((mask & GPU_BLEND_BIT) != 0) {
restore_mask(GL_BLEND, Attrib.is_blend);
}
}
#undef Attrib
#undef AttribStack
/** \} */
|