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
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
11429
11430
11431
11432
11433
11434
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463
11464
11465
11466
11467
11468
11469
11470
11471
11472
11473
11474
11475
11476
11477
11478
11479
11480
11481
11482
11483
11484
11485
11486
11487
11488
11489
11490
11491
11492
11493
11494
11495
11496
11497
11498
11499
11500
11501
11502
11503
11504
11505
11506
11507
11508
11509
11510
11511
11512
11513
11514
11515
11516
11517
11518
11519
11520
11521
11522
11523
11524
11525
11526
11527
11528
11529
11530
11531
11532
11533
11534
11535
11536
11537
11538
11539
11540
11541
11542
11543
11544
11545
11546
11547
11548
11549
11550
11551
11552
11553
11554
11555
11556
11557
11558
11559
11560
11561
11562
11563
11564
11565
11566
11567
11568
11569
11570
11571
11572
11573
11574
11575
11576
11577
11578
11579
11580
11581
11582
11583
11584
11585
11586
11587
11588
11589
11590
11591
11592
11593
11594
11595
11596
11597
11598
11599
11600
11601
11602
11603
11604
11605
11606
11607
11608
11609
11610
11611
11612
11613
11614
11615
11616
11617
11618
11619
11620
11621
11622
11623
11624
11625
11626
11627
11628
11629
11630
11631
11632
11633
11634
11635
11636
11637
11638
11639
11640
11641
11642
11643
11644
11645
11646
11647
11648
11649
11650
11651
11652
11653
11654
11655
11656
11657
11658
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
11669
11670
11671
11672
11673
11674
11675
11676
11677
11678
11679
11680
11681
11682
11683
11684
11685
11686
11687
11688
11689
11690
11691
11692
11693
11694
11695
11696
11697
11698
11699
11700
11701
11702
11703
11704
11705
11706
11707
11708
11709
11710
11711
11712
11713
11714
11715
11716
11717
11718
11719
11720
11721
11722
11723
11724
11725
11726
11727
11728
11729
11730
11731
11732
11733
11734
11735
11736
11737
11738
11739
11740
11741
11742
11743
11744
11745
11746
11747
11748
11749
11750
11751
11752
11753
11754
11755
11756
11757
11758
11759
11760
11761
11762
11763
11764
11765
11766
11767
11768
11769
11770
11771
11772
11773
11774
11775
11776
11777
11778
11779
11780
11781
11782
11783
11784
11785
11786
11787
11788
11789
11790
11791
11792
11793
11794
11795
11796
11797
11798
11799
11800
11801
11802
11803
11804
11805
11806
11807
11808
11809
11810
11811
11812
11813
11814
11815
11816
11817
11818
11819
11820
11821
11822
11823
11824
11825
11826
11827
11828
11829
11830
11831
11832
11833
11834
11835
11836
11837
11838
11839
11840
11841
11842
11843
11844
11845
11846
11847
11848
11849
11850
11851
11852
11853
11854
11855
11856
11857
11858
11859
11860
11861
11862
11863
11864
11865
11866
11867
11868
11869
11870
11871
11872
11873
11874
11875
11876
11877
11878
11879
11880
11881
11882
11883
11884
11885
11886
11887
11888
11889
11890
11891
11892
11893
11894
11895
11896
11897
11898
11899
11900
11901
11902
11903
11904
11905
11906
11907
11908
11909
11910
11911
11912
11913
11914
11915
11916
11917
11918
11919
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931
11932
11933
11934
11935
11936
11937
11938
11939
11940
11941
11942
11943
11944
11945
11946
11947
11948
11949
11950
11951
11952
11953
11954
11955
11956
11957
11958
11959
11960
11961
11962
11963
11964
11965
11966
11967
11968
11969
11970
11971
11972
11973
11974
11975
11976
11977
11978
11979
11980
11981
11982
11983
11984
11985
11986
11987
11988
11989
11990
11991
11992
11993
11994
11995
11996
11997
11998
11999
12000
12001
12002
12003
12004
12005
12006
12007
12008
12009
12010
12011
12012
12013
12014
12015
12016
12017
12018
12019
12020
12021
12022
12023
12024
12025
12026
12027
12028
12029
12030
12031
12032
12033
12034
12035
12036
12037
12038
12039
12040
12041
12042
12043
12044
12045
12046
12047
12048
12049
12050
12051
12052
12053
12054
12055
12056
12057
12058
12059
12060
12061
12062
12063
12064
12065
12066
12067
12068
12069
12070
12071
12072
12073
12074
12075
12076
12077
12078
12079
12080
12081
12082
12083
12084
12085
12086
12087
12088
12089
12090
12091
12092
12093
12094
12095
12096
12097
12098
12099
12100
12101
12102
12103
12104
12105
12106
12107
12108
12109
12110
12111
12112
12113
12114
12115
12116
12117
12118
12119
12120
12121
12122
12123
12124
12125
12126
12127
12128
12129
12130
12131
12132
12133
12134
12135
12136
12137
12138
12139
12140
12141
12142
12143
12144
12145
12146
12147
12148
12149
12150
12151
12152
12153
12154
12155
12156
12157
12158
12159
12160
12161
12162
12163
12164
12165
12166
12167
12168
12169
12170
12171
12172
12173
12174
12175
12176
12177
12178
12179
12180
12181
12182
12183
12184
12185
12186
12187
12188
12189
12190
12191
12192
12193
12194
12195
12196
12197
12198
12199
12200
12201
12202
12203
12204
12205
12206
12207
12208
12209
12210
12211
12212
12213
12214
12215
12216
12217
12218
12219
12220
12221
12222
12223
12224
12225
12226
12227
12228
12229
12230
12231
12232
12233
12234
12235
12236
12237
12238
12239
12240
12241
12242
12243
12244
12245
12246
12247
12248
12249
12250
12251
12252
12253
12254
12255
12256
12257
12258
12259
12260
12261
12262
12263
12264
12265
12266
12267
12268
12269
12270
12271
12272
12273
12274
12275
12276
12277
12278
12279
12280
12281
12282
12283
12284
12285
12286
12287
12288
12289
12290
12291
12292
12293
12294
12295
12296
12297
12298
12299
12300
12301
12302
12303
12304
12305
12306
12307
12308
12309
12310
12311
12312
12313
12314
12315
12316
12317
12318
12319
12320
12321
12322
12323
12324
12325
12326
12327
12328
12329
12330
12331
12332
12333
12334
12335
12336
12337
12338
12339
12340
12341
12342
12343
12344
12345
12346
12347
12348
12349
12350
12351
12352
12353
12354
12355
12356
12357
12358
12359
12360
12361
12362
12363
12364
12365
12366
12367
12368
12369
12370
12371
12372
12373
12374
12375
12376
12377
12378
12379
12380
12381
12382
12383
12384
12385
12386
12387
12388
12389
12390
12391
12392
12393
12394
12395
12396
12397
12398
12399
12400
12401
12402
12403
12404
12405
12406
12407
12408
12409
12410
12411
12412
12413
12414
12415
12416
12417
12418
12419
12420
12421
12422
12423
12424
12425
12426
12427
12428
12429
12430
12431
12432
12433
12434
12435
12436
12437
12438
12439
12440
12441
12442
12443
12444
12445
12446
12447
12448
12449
12450
12451
12452
12453
12454
12455
12456
12457
12458
12459
12460
12461
12462
12463
12464
12465
12466
12467
12468
12469
12470
12471
12472
12473
12474
12475
12476
12477
12478
12479
12480
12481
12482
12483
12484
12485
12486
12487
12488
12489
12490
12491
12492
12493
12494
12495
12496
12497
12498
12499
12500
12501
12502
12503
12504
12505
12506
12507
12508
12509
12510
12511
12512
12513
12514
12515
12516
12517
12518
12519
12520
12521
12522
12523
12524
12525
12526
12527
12528
12529
12530
12531
12532
12533
12534
12535
12536
12537
12538
12539
12540
12541
12542
12543
12544
12545
12546
12547
12548
12549
12550
12551
12552
12553
12554
12555
12556
12557
12558
12559
12560
12561
12562
12563
12564
12565
12566
12567
12568
12569
12570
12571
12572
12573
12574
12575
12576
12577
12578
12579
12580
12581
12582
12583
12584
12585
12586
12587
12588
12589
12590
12591
12592
12593
12594
12595
12596
12597
12598
12599
12600
12601
12602
12603
12604
12605
12606
12607
12608
12609
12610
12611
12612
12613
12614
12615
12616
12617
12618
12619
12620
12621
12622
12623
12624
12625
12626
12627
12628
12629
12630
12631
12632
12633
12634
12635
12636
12637
12638
12639
12640
12641
12642
12643
12644
12645
12646
12647
12648
12649
12650
12651
12652
12653
12654
12655
12656
12657
12658
12659
12660
12661
12662
12663
12664
12665
12666
12667
12668
12669
12670
12671
12672
12673
12674
12675
12676
12677
12678
12679
12680
12681
12682
12683
12684
12685
12686
12687
12688
12689
12690
12691
12692
12693
12694
12695
12696
12697
12698
12699
12700
12701
12702
12703
12704
12705
12706
12707
12708
12709
12710
12711
12712
12713
12714
12715
12716
12717
12718
12719
12720
12721
12722
12723
12724
12725
12726
12727
12728
12729
12730
12731
12732
12733
12734
12735
12736
12737
12738
12739
12740
12741
12742
12743
12744
12745
12746
12747
12748
12749
12750
12751
12752
12753
12754
12755
12756
12757
12758
12759
12760
12761
12762
12763
12764
12765
12766
12767
12768
12769
12770
12771
12772
12773
12774
12775
12776
12777
12778
12779
12780
12781
12782
12783
12784
12785
12786
12787
12788
12789
12790
12791
12792
12793
12794
12795
12796
12797
12798
12799
12800
12801
12802
12803
12804
12805
12806
12807
12808
12809
12810
12811
12812
12813
12814
12815
12816
12817
12818
12819
12820
12821
12822
12823
12824
12825
12826
12827
12828
12829
12830
12831
12832
12833
12834
12835
12836
12837
12838
12839
12840
12841
12842
12843
12844
12845
12846
12847
12848
12849
12850
12851
12852
12853
12854
12855
12856
12857
12858
12859
12860
12861
12862
12863
12864
12865
12866
12867
12868
12869
12870
12871
12872
12873
12874
12875
12876
12877
12878
12879
12880
12881
12882
12883
12884
12885
12886
12887
12888
12889
12890
12891
12892
12893
12894
12895
12896
12897
12898
12899
12900
12901
12902
12903
12904
12905
12906
12907
12908
12909
12910
12911
12912
12913
12914
12915
12916
12917
12918
12919
12920
12921
12922
12923
12924
12925
12926
12927
12928
12929
12930
12931
12932
12933
12934
12935
12936
12937
12938
12939
12940
12941
12942
12943
12944
12945
12946
12947
12948
12949
12950
12951
12952
12953
12954
12955
12956
12957
12958
12959
12960
12961
12962
12963
12964
12965
12966
12967
12968
12969
12970
12971
12972
12973
12974
12975
12976
12977
12978
12979
12980
12981
12982
12983
12984
12985
12986
12987
12988
12989
12990
12991
12992
12993
12994
12995
12996
12997
12998
12999
13000
13001
13002
13003
13004
13005
13006
13007
13008
13009
13010
13011
13012
13013
13014
13015
13016
13017
13018
13019
13020
13021
13022
13023
13024
13025
13026
13027
13028
13029
13030
13031
13032
13033
13034
13035
13036
13037
13038
13039
13040
13041
13042
13043
13044
13045
13046
13047
13048
13049
13050
13051
13052
13053
13054
13055
13056
13057
13058
13059
13060
13061
13062
13063
13064
13065
13066
13067
13068
13069
13070
13071
13072
13073
13074
13075
13076
13077
13078
13079
13080
13081
13082
13083
13084
13085
13086
13087
13088
13089
13090
13091
13092
13093
13094
13095
13096
13097
13098
13099
13100
13101
13102
13103
13104
13105
13106
13107
13108
13109
13110
13111
13112
13113
13114
13115
13116
13117
13118
13119
13120
13121
13122
13123
13124
13125
13126
13127
13128
13129
13130
13131
13132
13133
13134
13135
13136
13137
13138
13139
13140
13141
13142
13143
13144
13145
13146
13147
13148
13149
13150
13151
13152
13153
13154
13155
13156
13157
13158
13159
13160
13161
13162
13163
13164
13165
13166
13167
13168
13169
13170
13171
13172
13173
13174
13175
13176
13177
13178
13179
13180
13181
13182
13183
13184
13185
13186
13187
13188
13189
13190
13191
13192
13193
13194
13195
13196
13197
13198
13199
13200
13201
13202
13203
13204
13205
13206
13207
13208
13209
13210
13211
13212
13213
13214
13215
13216
13217
13218
13219
13220
13221
13222
13223
13224
13225
13226
13227
13228
13229
13230
13231
13232
13233
13234
13235
13236
13237
13238
13239
13240
13241
13242
13243
13244
13245
13246
13247
13248
13249
13250
13251
13252
13253
13254
13255
13256
13257
13258
13259
13260
13261
13262
13263
13264
13265
13266
13267
13268
13269
13270
13271
13272
13273
13274
13275
13276
13277
13278
13279
13280
13281
13282
13283
13284
13285
13286
13287
13288
13289
13290
13291
13292
13293
13294
13295
13296
13297
13298
13299
13300
13301
13302
13303
13304
13305
13306
13307
13308
13309
13310
13311
13312
13313
13314
13315
13316
13317
13318
13319
13320
13321
13322
13323
13324
13325
13326
13327
13328
13329
13330
13331
13332
13333
13334
13335
13336
13337
13338
13339
13340
13341
13342
13343
13344
13345
13346
13347
13348
13349
13350
13351
13352
13353
13354
13355
13356
13357
13358
13359
13360
13361
13362
13363
13364
13365
13366
13367
13368
13369
13370
13371
13372
13373
13374
13375
13376
13377
13378
13379
13380
13381
13382
13383
13384
13385
13386
13387
13388
13389
13390
13391
13392
13393
13394
13395
13396
13397
13398
13399
13400
13401
13402
13403
13404
13405
13406
13407
13408
13409
13410
13411
13412
13413
13414
13415
13416
13417
13418
13419
13420
13421
13422
13423
13424
13425
13426
13427
13428
13429
13430
13431
13432
13433
13434
13435
13436
13437
13438
13439
13440
13441
13442
13443
13444
13445
13446
13447
13448
13449
13450
13451
13452
13453
13454
13455
13456
13457
13458
13459
13460
13461
13462
13463
13464
13465
13466
13467
13468
13469
13470
13471
13472
13473
13474
13475
13476
13477
13478
13479
13480
13481
13482
13483
13484
13485
13486
13487
13488
13489
13490
13491
13492
13493
13494
13495
13496
13497
13498
13499
13500
13501
13502
13503
13504
13505
13506
13507
13508
13509
13510
13511
13512
13513
13514
13515
13516
13517
13518
13519
13520
13521
13522
13523
13524
13525
13526
13527
13528
13529
13530
13531
13532
13533
13534
13535
13536
13537
13538
13539
13540
13541
13542
13543
13544
13545
13546
13547
13548
13549
13550
13551
13552
13553
13554
13555
13556
13557
13558
13559
13560
13561
13562
13563
13564
13565
13566
13567
13568
13569
13570
13571
13572
13573
13574
13575
13576
13577
13578
13579
13580
13581
13582
13583
13584
13585
13586
13587
13588
13589
13590
13591
13592
13593
13594
13595
13596
13597
13598
13599
13600
13601
13602
13603
13604
13605
13606
13607
13608
13609
13610
13611
13612
13613
13614
13615
13616
13617
13618
13619
13620
13621
13622
13623
13624
13625
13626
13627
13628
13629
13630
13631
13632
13633
13634
13635
13636
13637
13638
13639
13640
13641
13642
13643
13644
13645
13646
13647
13648
13649
13650
13651
13652
13653
13654
13655
13656
13657
13658
13659
13660
13661
13662
13663
13664
13665
13666
13667
13668
13669
13670
13671
13672
13673
13674
13675
13676
13677
13678
13679
13680
13681
13682
13683
13684
13685
13686
13687
13688
13689
13690
13691
13692
13693
13694
13695
13696
13697
13698
13699
13700
13701
13702
13703
13704
13705
13706
13707
13708
13709
13710
13711
13712
13713
13714
13715
13716
13717
13718
13719
13720
13721
13722
13723
13724
13725
13726
13727
13728
13729
13730
13731
13732
13733
13734
13735
13736
13737
13738
13739
13740
13741
13742
13743
13744
13745
13746
13747
13748
13749
13750
13751
13752
13753
13754
13755
13756
13757
13758
13759
13760
13761
13762
13763
13764
13765
13766
13767
13768
13769
13770
13771
13772
13773
13774
13775
13776
13777
13778
13779
13780
13781
13782
13783
13784
13785
13786
13787
13788
13789
13790
13791
13792
13793
13794
13795
13796
13797
13798
13799
13800
13801
13802
13803
13804
13805
13806
13807
13808
13809
13810
13811
13812
13813
13814
13815
13816
13817
13818
13819
13820
13821
13822
13823
13824
13825
13826
13827
13828
13829
13830
13831
13832
13833
13834
13835
13836
13837
13838
13839
13840
13841
13842
13843
13844
13845
13846
13847
13848
13849
13850
13851
13852
13853
13854
13855
13856
13857
13858
13859
13860
13861
13862
13863
13864
13865
13866
13867
13868
13869
13870
13871
13872
13873
13874
13875
13876
13877
13878
13879
13880
13881
13882
13883
13884
13885
13886
13887
13888
13889
13890
13891
13892
13893
13894
13895
13896
13897
13898
13899
13900
13901
13902
13903
13904
13905
13906
13907
13908
13909
13910
13911
13912
13913
13914
13915
13916
13917
13918
13919
13920
13921
13922
13923
13924
13925
13926
13927
13928
13929
13930
13931
13932
13933
13934
13935
13936
13937
13938
13939
13940
13941
13942
13943
13944
13945
13946
13947
13948
13949
13950
13951
13952
13953
13954
13955
13956
13957
13958
13959
13960
13961
13962
13963
13964
13965
13966
13967
13968
13969
13970
13971
13972
13973
13974
13975
13976
13977
13978
13979
13980
13981
13982
13983
13984
13985
13986
13987
13988
13989
13990
13991
13992
13993
13994
13995
13996
13997
13998
13999
14000
14001
14002
14003
14004
14005
14006
14007
14008
14009
14010
14011
14012
14013
14014
14015
14016
14017
14018
14019
14020
14021
14022
14023
14024
14025
14026
14027
14028
14029
14030
14031
14032
14033
14034
14035
14036
14037
14038
14039
14040
14041
14042
14043
14044
14045
14046
14047
14048
14049
14050
14051
14052
14053
14054
14055
14056
14057
14058
14059
14060
14061
14062
14063
14064
14065
14066
14067
14068
14069
14070
14071
14072
14073
14074
14075
14076
14077
14078
14079
14080
14081
14082
14083
14084
14085
14086
14087
14088
14089
14090
14091
14092
14093
14094
14095
14096
14097
14098
14099
14100
14101
14102
14103
14104
14105
14106
14107
14108
14109
14110
14111
14112
14113
14114
14115
14116
14117
14118
14119
14120
14121
14122
14123
14124
14125
14126
14127
14128
14129
14130
14131
14132
14133
14134
14135
14136
14137
14138
14139
14140
14141
14142
14143
14144
14145
14146
14147
14148
14149
14150
14151
14152
14153
14154
14155
14156
14157
14158
14159
14160
14161
14162
14163
14164
14165
14166
14167
14168
14169
14170
14171
14172
14173
14174
14175
14176
14177
14178
14179
14180
14181
14182
14183
14184
14185
14186
14187
14188
14189
14190
14191
14192
14193
14194
14195
14196
14197
14198
14199
14200
14201
14202
14203
14204
14205
14206
14207
14208
14209
14210
14211
14212
14213
14214
14215
14216
14217
14218
14219
14220
14221
14222
14223
14224
14225
14226
14227
14228
14229
14230
14231
14232
14233
14234
14235
14236
14237
14238
14239
14240
14241
14242
14243
14244
14245
14246
14247
14248
14249
14250
14251
14252
14253
14254
14255
14256
14257
14258
14259
14260
14261
14262
14263
14264
14265
14266
14267
14268
14269
14270
14271
14272
14273
14274
14275
14276
14277
14278
14279
14280
14281
14282
14283
14284
14285
14286
14287
14288
14289
14290
14291
14292
14293
14294
14295
14296
14297
14298
14299
14300
14301
14302
14303
14304
14305
14306
14307
14308
14309
14310
14311
14312
14313
14314
14315
14316
14317
14318
14319
14320
14321
14322
14323
14324
14325
14326
14327
14328
14329
14330
14331
14332
14333
14334
14335
14336
14337
14338
14339
14340
14341
14342
14343
14344
14345
14346
14347
14348
14349
14350
14351
14352
14353
14354
14355
14356
14357
14358
14359
14360
14361
14362
14363
14364
14365
14366
14367
14368
14369
14370
14371
14372
14373
14374
14375
14376
14377
14378
14379
14380
14381
14382
14383
14384
14385
14386
14387
14388
14389
14390
14391
14392
14393
14394
14395
14396
14397
14398
14399
14400
14401
14402
14403
14404
14405
14406
14407
14408
14409
14410
14411
14412
14413
14414
14415
14416
14417
14418
14419
14420
14421
14422
14423
14424
14425
14426
14427
14428
14429
14430
14431
14432
14433
14434
14435
14436
14437
14438
14439
14440
14441
14442
14443
14444
14445
14446
14447
14448
14449
14450
14451
14452
14453
14454
14455
14456
14457
14458
14459
14460
14461
14462
14463
14464
14465
14466
14467
14468
14469
14470
14471
14472
14473
14474
14475
14476
14477
14478
14479
14480
14481
14482
14483
14484
14485
14486
14487
14488
14489
14490
14491
14492
14493
14494
14495
14496
14497
14498
14499
14500
14501
14502
14503
14504
14505
14506
14507
14508
14509
14510
14511
14512
14513
14514
14515
14516
14517
14518
14519
14520
14521
14522
14523
14524
14525
14526
14527
14528
14529
14530
14531
14532
14533
14534
14535
14536
14537
14538
14539
14540
14541
14542
14543
14544
14545
14546
14547
14548
14549
14550
14551
14552
14553
14554
14555
14556
14557
14558
14559
14560
14561
14562
14563
14564
14565
14566
14567
14568
14569
14570
14571
14572
14573
14574
14575
14576
14577
14578
14579
14580
14581
14582
14583
14584
14585
14586
14587
14588
14589
14590
14591
14592
14593
14594
14595
14596
14597
14598
14599
14600
14601
14602
14603
14604
14605
14606
14607
14608
14609
14610
14611
14612
14613
14614
14615
14616
14617
14618
14619
14620
14621
14622
14623
14624
14625
14626
14627
14628
14629
14630
14631
14632
14633
14634
14635
14636
14637
14638
14639
14640
14641
14642
14643
14644
14645
14646
14647
14648
14649
14650
14651
14652
14653
14654
14655
14656
14657
14658
14659
14660
14661
14662
14663
14664
14665
14666
14667
14668
14669
14670
14671
14672
14673
14674
14675
14676
14677
14678
14679
14680
14681
14682
14683
14684
14685
14686
14687
14688
14689
14690
14691
14692
14693
14694
14695
14696
14697
14698
14699
14700
14701
14702
14703
14704
14705
14706
14707
14708
14709
14710
14711
14712
14713
14714
14715
14716
14717
14718
14719
14720
14721
14722
14723
14724
14725
14726
14727
14728
14729
14730
14731
14732
14733
14734
14735
14736
14737
14738
14739
14740
14741
14742
14743
14744
14745
14746
14747
14748
14749
14750
14751
14752
14753
14754
14755
14756
14757
14758
14759
14760
14761
14762
14763
14764
14765
14766
14767
14768
14769
14770
14771
14772
14773
14774
14775
14776
14777
14778
14779
14780
14781
14782
14783
14784
14785
14786
14787
14788
14789
14790
14791
14792
14793
14794
14795
14796
14797
14798
14799
14800
14801
14802
14803
14804
14805
14806
14807
14808
14809
14810
14811
14812
14813
14814
14815
14816
14817
14818
14819
14820
14821
14822
14823
14824
14825
14826
14827
14828
14829
14830
14831
14832
14833
14834
14835
14836
14837
14838
14839
14840
14841
14842
14843
14844
14845
14846
14847
14848
14849
14850
14851
14852
14853
14854
14855
14856
14857
14858
14859
14860
14861
14862
14863
14864
14865
14866
14867
14868
14869
14870
14871
14872
14873
14874
14875
14876
14877
14878
14879
14880
14881
14882
14883
14884
14885
14886
14887
14888
14889
14890
14891
14892
14893
14894
14895
14896
14897
14898
14899
14900
14901
14902
14903
14904
14905
14906
14907
14908
14909
14910
14911
14912
14913
14914
14915
14916
14917
14918
14919
14920
14921
14922
14923
14924
14925
14926
14927
14928
14929
14930
14931
14932
14933
14934
14935
14936
14937
14938
14939
14940
14941
14942
14943
14944
14945
14946
14947
14948
14949
14950
14951
14952
14953
14954
14955
14956
14957
14958
14959
14960
14961
14962
14963
14964
14965
14966
14967
14968
14969
14970
14971
14972
14973
14974
14975
14976
14977
14978
14979
14980
14981
14982
14983
14984
14985
14986
14987
14988
14989
14990
14991
14992
14993
14994
14995
14996
14997
14998
14999
15000
15001
15002
15003
15004
15005
15006
15007
15008
15009
15010
15011
15012
15013
15014
15015
15016
15017
15018
15019
15020
15021
15022
15023
15024
15025
15026
15027
15028
15029
15030
15031
15032
15033
15034
15035
15036
15037
15038
15039
15040
15041
15042
15043
15044
15045
15046
15047
15048
15049
15050
15051
15052
15053
15054
15055
15056
15057
15058
15059
15060
15061
15062
15063
15064
15065
15066
15067
15068
15069
15070
15071
15072
15073
15074
15075
15076
15077
15078
15079
15080
15081
15082
15083
15084
15085
15086
15087
15088
15089
15090
15091
15092
15093
15094
15095
15096
15097
15098
15099
15100
15101
15102
15103
15104
15105
15106
15107
15108
15109
15110
15111
15112
15113
15114
15115
15116
15117
15118
15119
15120
15121
15122
15123
15124
15125
15126
15127
15128
15129
15130
15131
15132
15133
15134
15135
15136
15137
15138
15139
15140
15141
15142
15143
15144
15145
15146
15147
15148
15149
15150
15151
15152
15153
15154
15155
15156
15157
15158
15159
15160
15161
15162
15163
15164
15165
15166
15167
15168
15169
15170
15171
15172
15173
15174
15175
15176
15177
15178
15179
15180
15181
15182
15183
15184
15185
15186
15187
15188
15189
15190
15191
15192
15193
15194
15195
15196
15197
15198
15199
15200
15201
15202
15203
15204
15205
15206
15207
15208
15209
15210
15211
15212
15213
15214
15215
15216
15217
15218
15219
15220
15221
15222
15223
15224
15225
15226
15227
15228
15229
15230
15231
15232
15233
15234
15235
15236
15237
15238
15239
15240
15241
15242
15243
15244
15245
15246
15247
15248
15249
15250
15251
15252
15253
15254
15255
15256
15257
15258
15259
15260
15261
15262
15263
15264
15265
15266
15267
15268
15269
15270
15271
15272
15273
15274
15275
15276
15277
15278
15279
15280
15281
15282
15283
15284
15285
15286
15287
15288
15289
15290
15291
15292
15293
15294
15295
15296
15297
15298
15299
15300
15301
15302
15303
15304
15305
15306
15307
15308
15309
15310
15311
15312
15313
15314
15315
15316
15317
15318
15319
15320
15321
15322
15323
15324
15325
15326
15327
15328
15329
15330
15331
15332
15333
15334
15335
15336
15337
15338
15339
15340
15341
15342
15343
15344
15345
15346
15347
15348
15349
15350
15351
15352
15353
15354
15355
15356
15357
15358
15359
15360
15361
15362
15363
15364
15365
15366
15367
15368
15369
15370
15371
15372
15373
15374
15375
15376
15377
15378
15379
15380
15381
15382
15383
15384
15385
15386
15387
15388
15389
15390
15391
15392
15393
15394
15395
15396
15397
15398
15399
15400
15401
15402
15403
15404
15405
15406
15407
15408
15409
15410
15411
15412
15413
15414
15415
15416
15417
15418
15419
15420
15421
15422
15423
15424
15425
15426
15427
15428
15429
15430
15431
15432
15433
15434
15435
15436
15437
15438
15439
15440
15441
15442
15443
15444
15445
15446
15447
15448
15449
15450
15451
15452
15453
15454
15455
15456
15457
15458
15459
15460
15461
15462
15463
15464
15465
15466
15467
15468
15469
15470
15471
15472
15473
15474
15475
15476
15477
15478
15479
15480
15481
15482
15483
15484
15485
15486
15487
15488
15489
15490
15491
15492
15493
15494
15495
15496
15497
15498
15499
15500
15501
15502
15503
15504
15505
15506
15507
15508
15509
15510
15511
15512
15513
15514
15515
15516
15517
15518
15519
15520
15521
15522
15523
15524
15525
15526
15527
15528
15529
15530
15531
15532
15533
15534
15535
15536
15537
15538
15539
15540
15541
15542
15543
15544
15545
15546
15547
15548
15549
15550
15551
15552
15553
15554
15555
15556
15557
15558
15559
15560
15561
15562
15563
15564
15565
15566
15567
15568
15569
15570
15571
15572
15573
15574
15575
15576
15577
15578
15579
15580
15581
15582
15583
15584
15585
15586
15587
15588
15589
15590
15591
15592
15593
15594
15595
15596
15597
15598
15599
15600
15601
15602
15603
15604
15605
15606
15607
15608
15609
15610
15611
15612
15613
15614
15615
15616
15617
15618
15619
15620
15621
15622
15623
15624
15625
15626
15627
15628
15629
15630
15631
15632
15633
15634
15635
15636
15637
15638
15639
15640
15641
15642
15643
15644
15645
15646
15647
15648
15649
15650
15651
15652
15653
15654
15655
15656
15657
15658
15659
15660
15661
15662
15663
15664
15665
15666
15667
15668
15669
15670
15671
15672
|
#if defined(SOKOL_IMPL) && !defined(SOKOL_GFX_IMPL)
#define SOKOL_GFX_IMPL
#endif
#ifndef SOKOL_GFX_INCLUDED
/*
sokol_gfx.h -- simple 3D API wrapper
Project URL: https://github.com/floooh/sokol
Example code: https://github.com/floooh/sokol-samples
Do this:
#define SOKOL_IMPL or
#define SOKOL_GFX_IMPL
before you include this file in *one* C or C++ file to create the
implementation.
In the same place define one of the following to select the rendering
backend:
#define SOKOL_GLCORE33
#define SOKOL_GLES2
#define SOKOL_GLES3
#define SOKOL_D3D11
#define SOKOL_METAL
#define SOKOL_WGPU
#define SOKOL_DUMMY_BACKEND
I.e. for the GL 3.3 Core Profile it should look like this:
#include ...
#include ...
#define SOKOL_IMPL
#define SOKOL_GLCORE33
#include "sokol_gfx.h"
The dummy backend replaces the platform-specific backend code with empty
stub functions. This is useful for writing tests that need to run on the
command line.
Optionally provide the following defines with your own implementations:
SOKOL_ASSERT(c) - your own assert macro (default: assert(c))
SOKOL_MALLOC(s) - your own malloc function (default: malloc(s))
SOKOL_FREE(p) - your own free function (default: free(p))
SOKOL_LOG(msg) - your own logging function (default: puts(msg))
SOKOL_UNREACHABLE() - a guard macro for unreachable code (default: assert(false))
SOKOL_GFX_API_DECL - public function declaration prefix (default: extern)
SOKOL_API_DECL - same as SOKOL_GFX_API_DECL
SOKOL_API_IMPL - public function implementation prefix (default: -)
SOKOL_TRACE_HOOKS - enable trace hook callbacks (search below for TRACE HOOKS)
SOKOL_EXTERNAL_GL_LOADER - indicates that you're using your own GL loader, in this case
sokol_gfx.h will not include any platform GL headers and disable
the integrated Win32 GL loader
If sokol_gfx.h is compiled as a DLL, define the following before
including the declaration or implementation:
SOKOL_DLL
On Windows, SOKOL_DLL will define SOKOL_GFX_API_DECL as __declspec(dllexport)
or __declspec(dllimport) as needed.
If you want to compile without deprecated structs and functions,
define:
SOKOL_NO_DEPRECATED
Optionally define the following to force debug checks and validations
even in release mode:
SOKOL_DEBUG - by default this is defined if _DEBUG is defined
sokol_gfx DOES NOT:
===================
- create a window or the 3D-API context/device, you must do this
before sokol_gfx is initialized, and pass any required information
(like 3D device pointers) to the sokol_gfx initialization call
- present the rendered frame, how this is done exactly usually depends
on how the window and 3D-API context/device was created
- provide a unified shader language, instead 3D-API-specific shader
source-code or shader-bytecode must be provided (for the "official"
offline shader cross-compiler, see here:
https://github.com/floooh/sokol-tools/blob/master/docs/sokol-shdc.md)
STEP BY STEP
============
--- to initialize sokol_gfx, after creating a window and a 3D-API
context/device, call:
sg_setup(const sg_desc*)
--- create resource objects (at least buffers, shaders and pipelines,
and optionally images and passes):
sg_buffer sg_make_buffer(const sg_buffer_desc*)
sg_image sg_make_image(const sg_image_desc*)
sg_shader sg_make_shader(const sg_shader_desc*)
sg_pipeline sg_make_pipeline(const sg_pipeline_desc*)
sg_pass sg_make_pass(const sg_pass_desc*)
--- start rendering to the default frame buffer with:
sg_begin_default_pass(const sg_pass_action* action, int width, int height)
...or alternatively with:
sg_begin_default_passf(const sg_pass_action* action, float width, float height)
...which takes the framebuffer width and height as float values.
--- or start rendering to an offscreen framebuffer with:
sg_begin_pass(sg_pass pass, const sg_pass_action* action)
--- set the pipeline state for the next draw call with:
sg_apply_pipeline(sg_pipeline pip)
--- fill an sg_bindings struct with the resource bindings for the next
draw call (1..N vertex buffers, 0 or 1 index buffer, 0..N image objects
to use as textures each on the vertex-shader- and fragment-shader-stage
and then call
sg_apply_bindings(const sg_bindings* bindings)
to update the resource bindings
--- optionally update shader uniform data with:
sg_apply_uniforms(sg_shader_stage stage, int ub_index, const void* data, int num_bytes)
--- kick off a draw call with:
sg_draw(int base_element, int num_elements, int num_instances)
The sg_draw() function unifies all the different ways to render primitives
in a single call (indexed vs non-indexed rendering, and instanced vs non-instanced
rendering). In case of indexed rendering, base_element and num_element specify
indices in the currently bound index buffer. In case of non-indexed rendering
base_element and num_elements specify vertices in the currently bound
vertex-buffer(s). To perform instanced rendering, the rendering pipeline
must be setup for instancing (see sg_pipeline_desc below), a separate vertex buffer
containing per-instance data must be bound, and the num_instances parameter
must be > 1.
--- finish the current rendering pass with:
sg_end_pass()
--- when done with the current frame, call
sg_commit()
--- at the end of your program, shutdown sokol_gfx with:
sg_shutdown()
--- if you need to destroy resources before sg_shutdown(), call:
sg_destroy_buffer(sg_buffer buf)
sg_destroy_image(sg_image img)
sg_destroy_shader(sg_shader shd)
sg_destroy_pipeline(sg_pipeline pip)
sg_destroy_pass(sg_pass pass)
--- to set a new viewport rectangle, call
sg_apply_viewport(int x, int y, int width, int height, bool origin_top_left)
...or if you want to specifiy the viewport rectangle with float values:
sg_apply_viewportf(float x, float y, float width, float height, bool origin_top_left)
--- to set a new scissor rect, call:
sg_apply_scissor_rect(int x, int y, int width, int height, bool origin_top_left)
...or with float values:
sg_apply_scissor_rectf(float x, float y, float width, float height, bool origin_top_left)
Both sg_apply_viewport() and sg_apply_scissor_rect() must be called
inside a rendering pass
Note that sg_begin_default_pass() and sg_begin_pass() will reset both the
viewport and scissor rectangles to cover the entire framebuffer.
--- to update (overwrite) the content of buffer and image resources, call:
sg_update_buffer(sg_buffer buf, const sg_range* data)
sg_update_image(sg_image img, const sg_image_data* data)
Buffers and images to be updated must have been created with
SG_USAGE_DYNAMIC or SG_USAGE_STREAM
Only one update per frame is allowed for buffer and image resources when
using the sg_update_*() functions. The rationale is to have a simple
countermeasure to avoid the CPU scribbling over data the GPU is currently
using, or the CPU having to wait for the GPU
Buffer and image updates can be partial, as long as a rendering
operation only references the valid (updated) data in the
buffer or image.
--- to append a chunk of data to a buffer resource, call:
int sg_append_buffer(sg_buffer buf, const sg_range* data)
The difference to sg_update_buffer() is that sg_append_buffer()
can be called multiple times per frame to append new data to the
buffer piece by piece, optionally interleaved with draw calls referencing
the previously written data.
sg_append_buffer() returns a byte offset to the start of the
written data, this offset can be assigned to
sg_bindings.vertex_buffer_offsets[n] or
sg_bindings.index_buffer_offset
Code example:
for (...) {
const void* data = ...;
const int num_bytes = ...;
int offset = sg_append_buffer(buf, &(sg_range) { .ptr=data, .size=num_bytes });
bindings.vertex_buffer_offsets[0] = offset;
sg_apply_pipeline(pip);
sg_apply_bindings(&bindings);
sg_apply_uniforms(...);
sg_draw(...);
}
A buffer to be used with sg_append_buffer() must have been created
with SG_USAGE_DYNAMIC or SG_USAGE_STREAM.
If the application appends more data to the buffer then fits into
the buffer, the buffer will go into the "overflow" state for the
rest of the frame.
Any draw calls attempting to render an overflown buffer will be
silently dropped (in debug mode this will also result in a
validation error).
You can also check manually if a buffer is in overflow-state by calling
bool sg_query_buffer_overflow(sg_buffer buf)
NOTE: Due to restrictions in underlying 3D-APIs, appended chunks of
data will be 4-byte aligned in the destination buffer. This means
that there will be gaps in index buffers containing 16-bit indices
when the number of indices in a call to sg_append_buffer() is
odd. This isn't a problem when each call to sg_append_buffer()
is associated with one draw call, but will be problematic when
a single indexed draw call spans several appended chunks of indices.
--- to check at runtime for optional features, limits and pixelformat support,
call:
sg_features sg_query_features()
sg_limits sg_query_limits()
sg_pixelformat_info sg_query_pixelformat(sg_pixel_format fmt)
--- if you need to call into the underlying 3D-API directly, you must call:
sg_reset_state_cache()
...before calling sokol_gfx functions again
--- you can inspect the original sg_desc structure handed to sg_setup()
by calling sg_query_desc(). This will return an sg_desc struct with
the default values patched in instead of any zero-initialized values
--- you can inspect various internal resource attributes via:
sg_buffer_info sg_query_buffer_info(sg_buffer buf)
sg_image_info sg_query_image_info(sg_image img)
sg_shader_info sg_query_shader_info(sg_shader shd)
sg_pipeline_info sg_query_pipeline_info(sg_pipeline pip)
sg_pass_info sg_query_pass_info(sg_pass pass)
...please note that the returned info-structs are tied quite closely
to sokol_gfx.h internals, and may change more often than other
public API functions and structs.
--- you can ask at runtime what backend sokol_gfx.h has been compiled
for, or whether the GLES3 backend had to fall back to GLES2 with:
sg_backend sg_query_backend(void)
--- you can query the default resource creation parameters through the functions
sg_buffer_desc sg_query_buffer_defaults(const sg_buffer_desc* desc)
sg_image_desc sg_query_image_defaults(const sg_image_desc* desc)
sg_shader_desc sg_query_shader_defaults(const sg_shader_desc* desc)
sg_pipeline_desc sg_query_pipeline_defaults(const sg_pipeline_desc* desc)
sg_pass_desc sg_query_pass_defaults(const sg_pass_desc* desc)
These functions take a pointer to a desc structure which may contain
zero-initialized items for default values. These zero-init values
will be replaced with their concrete values in the returned desc
struct.
ON INITIALIZATION:
==================
When calling sg_setup(), a pointer to an sg_desc struct must be provided
which contains initialization options. These options provide two types
of information to sokol-gfx:
(1) upper bounds and limits needed to allocate various internal
data structures:
- the max number of resources of each type that can
be alive at the same time, this is used for allocating
internal pools
- the max overall size of uniform data that can be
updated per frame, including a worst-case alignment
per uniform update (this worst-case alignment is 256 bytes)
- the max size of all dynamic resource updates (sg_update_buffer,
sg_append_buffer and sg_update_image) per frame
- the max number of entries in the texture sampler cache
(how many unique texture sampler can exist at the same time)
Not all of those limit values are used by all backends, but it is
good practice to provide them none-the-less.
(2) 3D-API "context information" (sometimes also called "bindings"):
sokol_gfx.h doesn't create or initialize 3D API objects which are
closely related to the presentation layer (this includes the "rendering
device", the swapchain, and any objects which depend on the
swapchain). These API objects (or callback functions to obtain
them, if those objects might change between frames), must
be provided in a nested sg_context_desc struct inside the
sg_desc struct. If sokol_gfx.h is used together with
sokol_app.h, have a look at the sokol_glue.h header which provides
a convenience function to get a sg_context_desc struct filled out
with context information provided by sokol_app.h
See the documention block of the sg_desc struct below for more information.
BACKEND-SPECIFIC TOPICS:
========================
--- the GL backends need to know about the internal structure of uniform
blocks, and the texture sampler-name and -type:
typedef struct {
float mvp[16]; // model-view-projection matrix
float offset0[2]; // some 2D vectors
float offset1[2];
float offset2[2];
} params_t;
// uniform block structure and texture image definition in sg_shader_desc:
sg_shader_desc desc = {
// uniform block description (size and internal structure)
.vs.uniform_blocks[0] = {
.size = sizeof(params_t),
.uniforms = {
[0] = { .name="mvp", .type=SG_UNIFORMTYPE_MAT4 },
[1] = { .name="offset0", .type=SG_UNIFORMTYPE_VEC2 },
...
}
},
// one texture on the fragment-shader-stage, GLES2/WebGL needs name and image type
.fs.images[0] = { .name="tex", .type=SG_IMAGETYPE_ARRAY }
...
};
--- the Metal and D3D11 backends only need to know the size of uniform blocks,
not their internal member structure, and they only need to know
the type of a texture sampler, not its name:
sg_shader_desc desc = {
.vs.uniform_blocks[0].size = sizeof(params_t),
.fs.images[0].type = SG_IMAGETYPE_ARRAY,
...
};
--- when creating a shader object, GLES2/WebGL need to know the vertex
attribute names as used in the vertex shader:
sg_shader_desc desc = {
.attrs = {
[0] = { .name="position" },
[1] = { .name="color1" }
}
};
The vertex attribute names provided when creating a shader will be
used later in sg_create_pipeline() for matching the vertex layout
to vertex shader inputs.
--- on D3D11 you need to provide a semantic name and semantic index in the
shader description struct instead (see the D3D11 documentation on
D3D11_INPUT_ELEMENT_DESC for details):
sg_shader_desc desc = {
.attrs = {
[0] = { .sem_name="POSITION", .sem_index=0 }
[1] = { .sem_name="COLOR", .sem_index=1 }
}
};
The provided semantic information will be used later in sg_create_pipeline()
to match the vertex layout to vertex shader inputs.
--- on D3D11, and when passing HLSL source code (instead of byte code) to shader
creation, you can optionally define the shader model targets on the vertex
stage:
sg_shader_Desc desc = {
.vs = {
...
.d3d11_target = "vs_5_0"
},
.fs = {
...
.d3d11_target = "ps_5_0"
}
};
The default targets are "ps_4_0" and "fs_4_0". Note that those target names
are only used when compiling shaders from source. They are ignored when
creating a shader from bytecode.
--- on Metal, GL 3.3 or GLES3/WebGL2, you don't need to provide an attribute
name or semantic name, since vertex attributes can be bound by their slot index
(this is mandatory in Metal, and optional in GL):
sg_pipeline_desc desc = {
.layout = {
.attrs = {
[0] = { .format=SG_VERTEXFORMAT_FLOAT3 },
[1] = { .format=SG_VERTEXFORMAT_FLOAT4 }
}
}
};
WORKING WITH CONTEXTS
=====================
sokol-gfx allows to switch between different rendering contexts and
associate resource objects with contexts. This is useful to
create GL applications that render into multiple windows.
A rendering context keeps track of all resources created while
the context is active. When the context is destroyed, all resources
"belonging to the context" are destroyed as well.
A default context will be created and activated implicitly in
sg_setup(), and destroyed in sg_shutdown(). So for a typical application
which *doesn't* use multiple contexts, nothing changes, and calling
the context functions isn't necessary.
Three functions have been added to work with contexts:
--- sg_context sg_setup_context():
This must be called once after a GL context has been created and
made active.
--- void sg_activate_context(sg_context ctx)
This must be called after making a different GL context active.
Apart from 3D-API-specific actions, the call to sg_activate_context()
will internally call sg_reset_state_cache().
--- void sg_discard_context(sg_context ctx)
This must be called right before a GL context is destroyed and
will destroy all resources associated with the context (that
have been created while the context was active) The GL context must be
active at the time sg_discard_context(sg_context ctx) is called.
Also note that resources (buffers, images, shaders and pipelines) must
only be used or destroyed while the same GL context is active that
was also active while the resource was created (an exception is
resource sharing on GL, such resources can be used while
another context is active, but must still be destroyed under
the same context that was active during creation).
For more information, check out the multiwindow-glfw sample:
https://github.com/floooh/sokol-samples/blob/master/glfw/multiwindow-glfw.c
TRACE HOOKS:
============
sokol_gfx.h optionally allows to install "trace hook" callbacks for
each public API functions. When a public API function is called, and
a trace hook callback has been installed for this function, the
callback will be invoked with the parameters and result of the function.
This is useful for things like debugging- and profiling-tools, or
keeping track of resource creation and destruction.
To use the trace hook feature:
--- Define SOKOL_TRACE_HOOKS before including the implementation.
--- Setup an sg_trace_hooks structure with your callback function
pointers (keep all function pointers you're not interested
in zero-initialized), optionally set the user_data member
in the sg_trace_hooks struct.
--- Install the trace hooks by calling sg_install_trace_hooks(),
the return value of this function is another sg_trace_hooks
struct which contains the previously set of trace hooks.
You should keep this struct around, and call those previous
functions pointers from your own trace callbacks for proper
chaining.
As an example of how trace hooks are used, have a look at the
imgui/sokol_gfx_imgui.h header which implements a realtime
debugging UI for sokol_gfx.h on top of Dear ImGui.
A NOTE ON PORTABLE PACKED VERTEX FORMATS:
=========================================
There are two things to consider when using packed
vertex formats like UBYTE4, SHORT2, etc which need to work
across all backends:
- D3D11 can only convert *normalized* vertex formats to
floating point during vertex fetch, normalized formats
have a trailing 'N', and are "normalized" to a range
-1.0..+1.0 (for the signed formats) or 0.0..1.0 (for the
unsigned formats):
- SG_VERTEXFORMAT_BYTE4N
- SG_VERTEXFORMAT_UBYTE4N
- SG_VERTEXFORMAT_SHORT2N
- SG_VERTEXFORMAT_USHORT2N
- SG_VERTEXFORMAT_SHORT4N
- SG_VERTEXFORMAT_USHORT4N
D3D11 will not convert *non-normalized* vertex formats to floating point
vertex shader inputs, those can only be uses with the *ivecn* vertex shader
input types when D3D11 is used as backend (GL and Metal can use both formats)
- SG_VERTEXFORMAT_BYTE4,
- SG_VERTEXFORMAT_UBYTE4
- SG_VERTEXFORMAT_SHORT2
- SG_VERTEXFORMAT_SHORT4
- WebGL/GLES2 cannot use integer vertex shader inputs (int or ivecn)
- SG_VERTEXFORMAT_UINT10_N2 is not supported on WebGL/GLES2
So for a vertex input layout which works on all platforms, only use the following
vertex formats, and if needed "expand" the normalized vertex shader
inputs in the vertex shader by multiplying with 127.0, 255.0, 32767.0 or
65535.0:
- SG_VERTEXFORMAT_FLOAT,
- SG_VERTEXFORMAT_FLOAT2,
- SG_VERTEXFORMAT_FLOAT3,
- SG_VERTEXFORMAT_FLOAT4,
- SG_VERTEXFORMAT_BYTE4N,
- SG_VERTEXFORMAT_UBYTE4N,
- SG_VERTEXFORMAT_SHORT2N,
- SG_VERTEXFORMAT_USHORT2N
- SG_VERTEXFORMAT_SHORT4N,
- SG_VERTEXFORMAT_USHORT4N
TODO:
====
- talk about asynchronous resource creation
zlib/libpng license
Copyright (c) 2018 Andre Weissflog
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the
use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software in a
product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not
be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#define SOKOL_GFX_INCLUDED (1)
#include <stddef.h> // size_t
#include <stdint.h>
#include <stdbool.h>
#if defined(SOKOL_API_DECL) && !defined(SOKOL_GFX_API_DECL)
#define SOKOL_GFX_API_DECL SOKOL_API_DECL
#endif
#ifndef SOKOL_GFX_API_DECL
#if defined(_WIN32) && defined(SOKOL_DLL) && defined(SOKOL_GFX_IMPL)
#define SOKOL_GFX_API_DECL __declspec(dllexport)
#elif defined(_WIN32) && defined(SOKOL_DLL)
#define SOKOL_GFX_API_DECL __declspec(dllimport)
#else
#define SOKOL_GFX_API_DECL extern
#endif
#endif
#ifdef __cplusplus
extern "C" {
#endif
/*
Resource id typedefs:
sg_buffer: vertex- and index-buffers
sg_image: textures and render targets
sg_shader: vertex- and fragment-shaders, uniform blocks
sg_pipeline: associated shader and vertex-layouts, and render states
sg_pass: a bundle of render targets and actions on them
sg_context: a 'context handle' for switching between 3D-API contexts
Instead of pointers, resource creation functions return a 32-bit
number which uniquely identifies the resource object.
The 32-bit resource id is split into a 16-bit pool index in the lower bits,
and a 16-bit 'unique counter' in the upper bits. The index allows fast
pool lookups, and combined with the unique-mask it allows to detect
'dangling accesses' (trying to use an object which no longer exists, and
its pool slot has been reused for a new object)
The resource ids are wrapped into a struct so that the compiler
can complain when the wrong resource type is used.
*/
typedef struct sg_buffer { uint32_t id; } sg_buffer;
typedef struct sg_image { uint32_t id; } sg_image;
typedef struct sg_shader { uint32_t id; } sg_shader;
typedef struct sg_pipeline { uint32_t id; } sg_pipeline;
typedef struct sg_pass { uint32_t id; } sg_pass;
typedef struct sg_context { uint32_t id; } sg_context;
/*
sg_range is a pointer-size-pair struct used to pass memory blobs into
sokol-gfx. When initialized from a value type (array or struct), you can
use the SG_RANGE() macro to build an sg_range struct. For functions which
take either a sg_range pointer, or a (C++) sg_range reference, use the
SG_RANGE_REF macro as a solution which compiles both in C and C++.
*/
typedef struct sg_range {
const void* ptr;
size_t size;
} sg_range;
// disabling this for every includer isn't great, but the warnings are also quite pointless
#if defined(_MSC_VER)
#pragma warning(disable:4221) /* /W4 only: nonstandard extension used: 'x': cannot be initialized using address of automatic variable 'y' */
#pragma warning(disable:4204) /* VS2015: nonstandard extension used: non-constant aggregate initializer */
#endif
#if defined(__cplusplus)
#define SG_RANGE(x) sg_range{ &x, sizeof(x) }
#define SG_RANGE_REF(x) sg_range{ &x, sizeof(x) }
#else
#define SG_RANGE(x) (sg_range){ &x, sizeof(x) }
#define SG_RANGE_REF(x) &(sg_range){ &x, sizeof(x) }
#endif
// various compile-time constants
enum {
SG_INVALID_ID = 0,
SG_NUM_SHADER_STAGES = 2,
SG_NUM_INFLIGHT_FRAMES = 2,
SG_MAX_COLOR_ATTACHMENTS = 4,
SG_MAX_SHADERSTAGE_BUFFERS = 8,
SG_MAX_SHADERSTAGE_IMAGES = 12,
SG_MAX_SHADERSTAGE_UBS = 4,
SG_MAX_UB_MEMBERS = 16,
SG_MAX_VERTEX_ATTRIBUTES = 16, /* NOTE: actual max vertex attrs can be less on GLES2, see sg_limits! */
SG_MAX_MIPMAPS = 16,
SG_MAX_TEXTUREARRAY_LAYERS = 128
};
/*
sg_color
An RGBA color value.
*/
typedef struct sg_color { float r, g, b, a; } sg_color;
/*
sg_backend
The active 3D-API backend, use the function sg_query_backend()
to get the currently active backend.
NOTE that SG_BACKEND_GLES2 will be returned if sokol-gfx was
compiled with SOKOL_GLES3, but the runtime platform doesn't support
GLES3/WebGL2 and sokol-gfx had to fallback to GLES2/WebGL.
*/
typedef enum sg_backend {
SG_BACKEND_GLCORE33,
SG_BACKEND_GLES2,
SG_BACKEND_GLES3,
SG_BACKEND_D3D11,
SG_BACKEND_METAL_IOS,
SG_BACKEND_METAL_MACOS,
SG_BACKEND_METAL_SIMULATOR,
SG_BACKEND_WGPU,
SG_BACKEND_DUMMY,
} sg_backend;
/*
sg_pixel_format
sokol_gfx.h basically uses the same pixel formats as WebGPU, since these
are supported on most newer GPUs. GLES2 and WebGL only supports a much
smaller subset of actually available pixel formats. Call
sg_query_pixelformat() to check at runtime if a pixel format supports the
desired features.
A pixelformat name consist of three parts:
- components (R, RG, RGB or RGBA)
- bit width per component (8, 16 or 32)
- component data type:
- unsigned normalized (no postfix)
- signed normalized (SN postfix)
- unsigned integer (UI postfix)
- signed integer (SI postfix)
- float (F postfix)
Not all pixel formats can be used for everything, call sg_query_pixelformat()
to inspect the capabilities of a given pixelformat. The function returns
an sg_pixelformat_info struct with the following bool members:
- sample: the pixelformat can be sampled as texture at least with
nearest filtering
- filter: the pixelformat can be samples as texture with linear
filtering
- render: the pixelformat can be used for render targets
- blend: blending is supported when using the pixelformat for
render targets
- msaa: multisample-antialiasing is supported when using the
pixelformat for render targets
- depth: the pixelformat can be used for depth-stencil attachments
When targeting GLES2/WebGL, the only safe formats to use
as texture are SG_PIXELFORMAT_R8 and SG_PIXELFORMAT_RGBA8. For rendering
in GLES2/WebGL, only SG_PIXELFORMAT_RGBA8 is safe. All other formats
must be checked via sg_query_pixelformats().
The default pixel format for texture images is SG_PIXELFORMAT_RGBA8.
The default pixel format for render target images is platform-dependent:
- for Metal and D3D11 it is SG_PIXELFORMAT_BGRA8
- for GL backends it is SG_PIXELFORMAT_RGBA8
This is mainly because of the default framebuffer which is setup outside
of sokol_gfx.h. On some backends, using BGRA for the default frame buffer
allows more efficient frame flips. For your own offscreen-render-targets,
use whatever renderable pixel format is convenient for you.
*/
typedef enum sg_pixel_format {
_SG_PIXELFORMAT_DEFAULT, /* value 0 reserved for default-init */
SG_PIXELFORMAT_NONE,
SG_PIXELFORMAT_R8,
SG_PIXELFORMAT_R8SN,
SG_PIXELFORMAT_R8UI,
SG_PIXELFORMAT_R8SI,
SG_PIXELFORMAT_R16,
SG_PIXELFORMAT_R16SN,
SG_PIXELFORMAT_R16UI,
SG_PIXELFORMAT_R16SI,
SG_PIXELFORMAT_R16F,
SG_PIXELFORMAT_RG8,
SG_PIXELFORMAT_RG8SN,
SG_PIXELFORMAT_RG8UI,
SG_PIXELFORMAT_RG8SI,
SG_PIXELFORMAT_R32UI,
SG_PIXELFORMAT_R32SI,
SG_PIXELFORMAT_R32F,
SG_PIXELFORMAT_RG16,
SG_PIXELFORMAT_RG16SN,
SG_PIXELFORMAT_RG16UI,
SG_PIXELFORMAT_RG16SI,
SG_PIXELFORMAT_RG16F,
SG_PIXELFORMAT_RGBA8,
SG_PIXELFORMAT_RGBA8SN,
SG_PIXELFORMAT_RGBA8UI,
SG_PIXELFORMAT_RGBA8SI,
SG_PIXELFORMAT_BGRA8,
SG_PIXELFORMAT_RGB10A2,
SG_PIXELFORMAT_RG11B10F,
SG_PIXELFORMAT_RG32UI,
SG_PIXELFORMAT_RG32SI,
SG_PIXELFORMAT_RG32F,
SG_PIXELFORMAT_RGBA16,
SG_PIXELFORMAT_RGBA16SN,
SG_PIXELFORMAT_RGBA16UI,
SG_PIXELFORMAT_RGBA16SI,
SG_PIXELFORMAT_RGBA16F,
SG_PIXELFORMAT_RGBA32UI,
SG_PIXELFORMAT_RGBA32SI,
SG_PIXELFORMAT_RGBA32F,
SG_PIXELFORMAT_DEPTH,
SG_PIXELFORMAT_DEPTH_STENCIL,
SG_PIXELFORMAT_BC1_RGBA,
SG_PIXELFORMAT_BC2_RGBA,
SG_PIXELFORMAT_BC3_RGBA,
SG_PIXELFORMAT_BC4_R,
SG_PIXELFORMAT_BC4_RSN,
SG_PIXELFORMAT_BC5_RG,
SG_PIXELFORMAT_BC5_RGSN,
SG_PIXELFORMAT_BC6H_RGBF,
SG_PIXELFORMAT_BC6H_RGBUF,
SG_PIXELFORMAT_BC7_RGBA,
SG_PIXELFORMAT_PVRTC_RGB_2BPP,
SG_PIXELFORMAT_PVRTC_RGB_4BPP,
SG_PIXELFORMAT_PVRTC_RGBA_2BPP,
SG_PIXELFORMAT_PVRTC_RGBA_4BPP,
SG_PIXELFORMAT_ETC2_RGB8,
SG_PIXELFORMAT_ETC2_RGB8A1,
SG_PIXELFORMAT_ETC2_RGBA8,
SG_PIXELFORMAT_ETC2_RG11,
SG_PIXELFORMAT_ETC2_RG11SN,
_SG_PIXELFORMAT_NUM,
_SG_PIXELFORMAT_FORCE_U32 = 0x7FFFFFFF
} sg_pixel_format;
/*
Runtime information about a pixel format, returned
by sg_query_pixelformat().
*/
typedef struct sg_pixelformat_info {
bool sample; // pixel format can be sampled in shaders
bool filter; // pixel format can be sampled with filtering
bool render; // pixel format can be used as render target
bool blend; // alpha-blending is supported
bool msaa; // pixel format can be used as MSAA render target
bool depth; // pixel format is a depth format
#if defined(SOKOL_ZIG_BINDINGS)
uint32_t __pad[3];
#endif
} sg_pixelformat_info;
/*
Runtime information about available optional features,
returned by sg_query_features()
*/
typedef struct sg_features {
bool instancing; // hardware instancing supported
bool origin_top_left; // framebuffer and texture origin is in top left corner
bool multiple_render_targets; // offscreen render passes can have multiple render targets attached
bool msaa_render_targets; // offscreen render passes support MSAA antialiasing
bool imagetype_3d; // creation of SG_IMAGETYPE_3D images is supported
bool imagetype_array; // creation of SG_IMAGETYPE_ARRAY images is supported
bool image_clamp_to_border; // border color and clamp-to-border UV-wrap mode is supported
bool mrt_independent_blend_state; // multiple-render-target rendering can use per-render-target blend state
bool mrt_independent_write_mask; // multiple-render-target rendering can use per-render-target color write masks
#if defined(SOKOL_ZIG_BINDINGS)
uint32_t __pad[3];
#endif
} sg_features;
/*
Runtime information about resource limits, returned by sg_query_limit()
*/
typedef struct sg_limits {
int max_image_size_2d; // max width/height of SG_IMAGETYPE_2D images
int max_image_size_cube; // max width/height of SG_IMAGETYPE_CUBE images
int max_image_size_3d; // max width/height/depth of SG_IMAGETYPE_3D images
int max_image_size_array; // max width/height of SG_IMAGETYPE_ARRAY images
int max_image_array_layers; // max number of layers in SG_IMAGETYPE_ARRAY images
int max_vertex_attrs; // <= SG_MAX_VERTEX_ATTRIBUTES (only on some GLES2 impls)
} sg_limits;
/*
sg_resource_state
The current state of a resource in its resource pool.
Resources start in the INITIAL state, which means the
pool slot is unoccupied and can be allocated. When a resource is
created, first an id is allocated, and the resource pool slot
is set to state ALLOC. After allocation, the resource is
initialized, which may result in the VALID or FAILED state. The
reason why allocation and initialization are separate is because
some resource types (e.g. buffers and images) might be asynchronously
initialized by the user application. If a resource which is not
in the VALID state is attempted to be used for rendering, rendering
operations will silently be dropped.
The special INVALID state is returned in sg_query_xxx_state() if no
resource object exists for the provided resource id.
*/
typedef enum sg_resource_state {
SG_RESOURCESTATE_INITIAL,
SG_RESOURCESTATE_ALLOC,
SG_RESOURCESTATE_VALID,
SG_RESOURCESTATE_FAILED,
SG_RESOURCESTATE_INVALID,
_SG_RESOURCESTATE_FORCE_U32 = 0x7FFFFFFF
} sg_resource_state;
/*
sg_usage
A resource usage hint describing the update strategy of
buffers and images. This is used in the sg_buffer_desc.usage
and sg_image_desc.usage members when creating buffers
and images:
SG_USAGE_IMMUTABLE: the resource will never be updated with
new data, instead the content of the
resource must be provided on creation
SG_USAGE_DYNAMIC: the resource will be updated infrequently
with new data (this could range from "once
after creation", to "quite often but not
every frame")
SG_USAGE_STREAM: the resource will be updated each frame
with new content
The rendering backends use this hint to prevent that the
CPU needs to wait for the GPU when attempting to update
a resource that might be currently accessed by the GPU.
Resource content is updated with the functions sg_update_buffer() or
sg_append_buffer() for buffer objects, and sg_update_image() for image
objects. For the sg_update_*() functions, only one update is allowed per
frame and resource object, while sg_append_buffer() can be called
multiple times per frame on the same buffer. The application must update
all data required for rendering (this means that the update data can be
smaller than the resource size, if only a part of the overall resource
size is used for rendering, you only need to make sure that the data that
*is* used is valid).
The default usage is SG_USAGE_IMMUTABLE.
*/
typedef enum sg_usage {
_SG_USAGE_DEFAULT, /* value 0 reserved for default-init */
SG_USAGE_IMMUTABLE,
SG_USAGE_DYNAMIC,
SG_USAGE_STREAM,
_SG_USAGE_NUM,
_SG_USAGE_FORCE_U32 = 0x7FFFFFFF
} sg_usage;
/*
sg_buffer_type
This indicates whether a buffer contains vertex- or index-data,
used in the sg_buffer_desc.type member when creating a buffer.
The default value is SG_BUFFERTYPE_VERTEXBUFFER.
*/
typedef enum sg_buffer_type {
_SG_BUFFERTYPE_DEFAULT, /* value 0 reserved for default-init */
SG_BUFFERTYPE_VERTEXBUFFER,
SG_BUFFERTYPE_INDEXBUFFER,
_SG_BUFFERTYPE_NUM,
_SG_BUFFERTYPE_FORCE_U32 = 0x7FFFFFFF
} sg_buffer_type;
/*
sg_index_type
Indicates whether indexed rendering (fetching vertex-indices from an
index buffer) is used, and if yes, the index data type (16- or 32-bits).
This is used in the sg_pipeline_desc.index_type member when creating a
pipeline object.
The default index type is SG_INDEXTYPE_NONE.
*/
typedef enum sg_index_type {
_SG_INDEXTYPE_DEFAULT, /* value 0 reserved for default-init */
SG_INDEXTYPE_NONE,
SG_INDEXTYPE_UINT16,
SG_INDEXTYPE_UINT32,
_SG_INDEXTYPE_NUM,
_SG_INDEXTYPE_FORCE_U32 = 0x7FFFFFFF
} sg_index_type;
/*
sg_image_type
Indicates the basic type of an image object (2D-texture, cubemap,
3D-texture or 2D-array-texture). 3D- and array-textures are not supported
on the GLES2/WebGL backend (use sg_query_features().imagetype_3d and
sg_query_features().imagetype_array to check for support). The image type
is used in the sg_image_desc.type member when creating an image, and
in sg_shader_image_desc when describing a shader's texture sampler binding.
The default image type when creating an image is SG_IMAGETYPE_2D.
*/
typedef enum sg_image_type {
_SG_IMAGETYPE_DEFAULT, /* value 0 reserved for default-init */
SG_IMAGETYPE_2D,
SG_IMAGETYPE_CUBE,
SG_IMAGETYPE_3D,
SG_IMAGETYPE_ARRAY,
_SG_IMAGETYPE_NUM,
_SG_IMAGETYPE_FORCE_U32 = 0x7FFFFFFF
} sg_image_type;
/*
sg_sampler_type
Indicates the basic data type of a shader's texture sampler which
can be float , unsigned integer or signed integer. The sampler
type is used in the sg_shader_image_desc to describe the
sampler type of a shader's texture sampler binding.
The default sampler type is SG_SAMPLERTYPE_FLOAT.
*/
typedef enum sg_sampler_type {
_SG_SAMPLERTYPE_DEFAULT, /* value 0 reserved for default-init */
SG_SAMPLERTYPE_FLOAT,
SG_SAMPLERTYPE_SINT,
SG_SAMPLERTYPE_UINT,
} sg_sampler_type;
/*
sg_cube_face
The cubemap faces. Use these as indices in the sg_image_desc.content
array.
*/
typedef enum sg_cube_face {
SG_CUBEFACE_POS_X,
SG_CUBEFACE_NEG_X,
SG_CUBEFACE_POS_Y,
SG_CUBEFACE_NEG_Y,
SG_CUBEFACE_POS_Z,
SG_CUBEFACE_NEG_Z,
SG_CUBEFACE_NUM,
_SG_CUBEFACE_FORCE_U32 = 0x7FFFFFFF
} sg_cube_face;
/*
sg_shader_stage
There are 2 shader stages: vertex- and fragment-shader-stage.
Each shader stage consists of:
- one slot for a shader function (provided as source- or byte-code)
- SG_MAX_SHADERSTAGE_UBS slots for uniform blocks
- SG_MAX_SHADERSTAGE_IMAGES slots for images used as textures by
the shader function
*/
typedef enum sg_shader_stage {
SG_SHADERSTAGE_VS,
SG_SHADERSTAGE_FS,
_SG_SHADERSTAGE_FORCE_U32 = 0x7FFFFFFF
} sg_shader_stage;
/*
sg_primitive_type
This is the common subset of 3D primitive types supported across all 3D
APIs. This is used in the sg_pipeline_desc.primitive_type member when
creating a pipeline object.
The default primitive type is SG_PRIMITIVETYPE_TRIANGLES.
*/
typedef enum sg_primitive_type {
_SG_PRIMITIVETYPE_DEFAULT, /* value 0 reserved for default-init */
SG_PRIMITIVETYPE_POINTS,
SG_PRIMITIVETYPE_LINES,
SG_PRIMITIVETYPE_LINE_STRIP,
SG_PRIMITIVETYPE_TRIANGLES,
SG_PRIMITIVETYPE_TRIANGLE_STRIP,
_SG_PRIMITIVETYPE_NUM,
_SG_PRIMITIVETYPE_FORCE_U32 = 0x7FFFFFFF
} sg_primitive_type;
/*
sg_filter
The filtering mode when sampling a texture image. This is
used in the sg_image_desc.min_filter and sg_image_desc.mag_filter
members when creating an image object.
The default filter mode is SG_FILTER_NEAREST.
*/
typedef enum sg_filter {
_SG_FILTER_DEFAULT, /* value 0 reserved for default-init */
SG_FILTER_NEAREST,
SG_FILTER_LINEAR,
SG_FILTER_NEAREST_MIPMAP_NEAREST,
SG_FILTER_NEAREST_MIPMAP_LINEAR,
SG_FILTER_LINEAR_MIPMAP_NEAREST,
SG_FILTER_LINEAR_MIPMAP_LINEAR,
_SG_FILTER_NUM,
_SG_FILTER_FORCE_U32 = 0x7FFFFFFF
} sg_filter;
/*
sg_wrap
The texture coordinates wrapping mode when sampling a texture
image. This is used in the sg_image_desc.wrap_u, .wrap_v
and .wrap_w members when creating an image.
The default wrap mode is SG_WRAP_REPEAT.
NOTE: SG_WRAP_CLAMP_TO_BORDER is not supported on all backends
and platforms. To check for support, call sg_query_features()
and check the "clamp_to_border" boolean in the returned
sg_features struct.
Platforms which don't support SG_WRAP_CLAMP_TO_BORDER will silently fall back
to SG_WRAP_CLAMP_TO_EDGE without a validation error.
Platforms which support clamp-to-border are:
- all desktop GL platforms
- Metal on macOS
- D3D11
Platforms which do not support clamp-to-border:
- GLES2/3 and WebGL/WebGL2
- Metal on iOS
*/
typedef enum sg_wrap {
_SG_WRAP_DEFAULT, /* value 0 reserved for default-init */
SG_WRAP_REPEAT,
SG_WRAP_CLAMP_TO_EDGE,
SG_WRAP_CLAMP_TO_BORDER,
SG_WRAP_MIRRORED_REPEAT,
_SG_WRAP_NUM,
_SG_WRAP_FORCE_U32 = 0x7FFFFFFF
} sg_wrap;
/*
sg_border_color
The border color to use when sampling a texture, and the UV wrap
mode is SG_WRAP_CLAMP_TO_BORDER.
The default border color is SG_BORDERCOLOR_OPAQUE_BLACK
*/
typedef enum sg_border_color {
_SG_BORDERCOLOR_DEFAULT, /* value 0 reserved for default-init */
SG_BORDERCOLOR_TRANSPARENT_BLACK,
SG_BORDERCOLOR_OPAQUE_BLACK,
SG_BORDERCOLOR_OPAQUE_WHITE,
_SG_BORDERCOLOR_NUM,
_SG_BORDERCOLOR_FORCE_U32 = 0x7FFFFFFF
} sg_border_color;
/*
sg_vertex_format
The data type of a vertex component. This is used to describe
the layout of vertex data when creating a pipeline object.
*/
typedef enum sg_vertex_format {
SG_VERTEXFORMAT_INVALID,
SG_VERTEXFORMAT_FLOAT,
SG_VERTEXFORMAT_FLOAT2,
SG_VERTEXFORMAT_FLOAT3,
SG_VERTEXFORMAT_FLOAT4,
SG_VERTEXFORMAT_BYTE4,
SG_VERTEXFORMAT_BYTE4N,
SG_VERTEXFORMAT_UBYTE4,
SG_VERTEXFORMAT_UBYTE4N,
SG_VERTEXFORMAT_SHORT2,
SG_VERTEXFORMAT_SHORT2N,
SG_VERTEXFORMAT_USHORT2N,
SG_VERTEXFORMAT_SHORT4,
SG_VERTEXFORMAT_SHORT4N,
SG_VERTEXFORMAT_USHORT4N,
SG_VERTEXFORMAT_UINT10_N2,
_SG_VERTEXFORMAT_NUM,
_SG_VERTEXFORMAT_FORCE_U32 = 0x7FFFFFFF
} sg_vertex_format;
/*
sg_vertex_step
Defines whether the input pointer of a vertex input stream is advanced
'per vertex' or 'per instance'. The default step-func is
SG_VERTEXSTEP_PER_VERTEX. SG_VERTEXSTEP_PER_INSTANCE is used with
instanced-rendering.
The vertex-step is part of the vertex-layout definition
when creating pipeline objects.
*/
typedef enum sg_vertex_step {
_SG_VERTEXSTEP_DEFAULT, /* value 0 reserved for default-init */
SG_VERTEXSTEP_PER_VERTEX,
SG_VERTEXSTEP_PER_INSTANCE,
_SG_VERTEXSTEP_NUM,
_SG_VERTEXSTEP_FORCE_U32 = 0x7FFFFFFF
} sg_vertex_step;
/*
sg_uniform_type
The data type of a uniform block member. This is used to
describe the internal layout of uniform blocks when creating
a shader object.
*/
typedef enum sg_uniform_type {
SG_UNIFORMTYPE_INVALID,
SG_UNIFORMTYPE_FLOAT,
SG_UNIFORMTYPE_FLOAT2,
SG_UNIFORMTYPE_FLOAT3,
SG_UNIFORMTYPE_FLOAT4,
SG_UNIFORMTYPE_MAT4,
_SG_UNIFORMTYPE_NUM,
_SG_UNIFORMTYPE_FORCE_U32 = 0x7FFFFFFF
} sg_uniform_type;
/*
sg_cull_mode
The face-culling mode, this is used in the
sg_pipeline_desc.cull_mode member when creating a
pipeline object.
The default cull mode is SG_CULLMODE_NONE
*/
typedef enum sg_cull_mode {
_SG_CULLMODE_DEFAULT, /* value 0 reserved for default-init */
SG_CULLMODE_NONE,
SG_CULLMODE_FRONT,
SG_CULLMODE_BACK,
_SG_CULLMODE_NUM,
_SG_CULLMODE_FORCE_U32 = 0x7FFFFFFF
} sg_cull_mode;
/*
sg_face_winding
The vertex-winding rule that determines a front-facing primitive. This
is used in the member sg_pipeline_desc.face_winding
when creating a pipeline object.
The default winding is SG_FACEWINDING_CW (clockwise)
*/
typedef enum sg_face_winding {
_SG_FACEWINDING_DEFAULT, /* value 0 reserved for default-init */
SG_FACEWINDING_CCW,
SG_FACEWINDING_CW,
_SG_FACEWINDING_NUM,
_SG_FACEWINDING_FORCE_U32 = 0x7FFFFFFF
} sg_face_winding;
/*
sg_compare_func
The compare-function for depth- and stencil-ref tests.
This is used when creating pipeline objects in the members:
sg_pipeline_desc
.depth
.compare
.stencil
.front.compare
.back.compar
The default compare func for depth- and stencil-tests is
SG_COMPAREFUNC_ALWAYS.
*/
typedef enum sg_compare_func {
_SG_COMPAREFUNC_DEFAULT, /* value 0 reserved for default-init */
SG_COMPAREFUNC_NEVER,
SG_COMPAREFUNC_LESS,
SG_COMPAREFUNC_EQUAL,
SG_COMPAREFUNC_LESS_EQUAL,
SG_COMPAREFUNC_GREATER,
SG_COMPAREFUNC_NOT_EQUAL,
SG_COMPAREFUNC_GREATER_EQUAL,
SG_COMPAREFUNC_ALWAYS,
_SG_COMPAREFUNC_NUM,
_SG_COMPAREFUNC_FORCE_U32 = 0x7FFFFFFF
} sg_compare_func;
/*
sg_stencil_op
The operation performed on a currently stored stencil-value when a
comparison test passes or fails. This is used when creating a pipeline
object in the members:
sg_pipeline_desc
.stencil
.front
.fail_op
.depth_fail_op
.pass_op
.back
.fail_op
.depth_fail_op
.pass_op
The default value is SG_STENCILOP_KEEP.
*/
typedef enum sg_stencil_op {
_SG_STENCILOP_DEFAULT, /* value 0 reserved for default-init */
SG_STENCILOP_KEEP,
SG_STENCILOP_ZERO,
SG_STENCILOP_REPLACE,
SG_STENCILOP_INCR_CLAMP,
SG_STENCILOP_DECR_CLAMP,
SG_STENCILOP_INVERT,
SG_STENCILOP_INCR_WRAP,
SG_STENCILOP_DECR_WRAP,
_SG_STENCILOP_NUM,
_SG_STENCILOP_FORCE_U32 = 0x7FFFFFFF
} sg_stencil_op;
/*
sg_blend_factor
The source and destination factors in blending operations.
This is used in the following members when creating a pipeline object:
sg_pipeline_desc
.colors[i]
.blend
.src_factor_rgb
.dst_factor_rgb
.src_factor_alpha
.dst_factor_alpha
The default value is SG_BLENDFACTOR_ONE for source
factors, and SG_BLENDFACTOR_ZERO for destination factors.
*/
typedef enum sg_blend_factor {
_SG_BLENDFACTOR_DEFAULT, /* value 0 reserved for default-init */
SG_BLENDFACTOR_ZERO,
SG_BLENDFACTOR_ONE,
SG_BLENDFACTOR_SRC_COLOR,
SG_BLENDFACTOR_ONE_MINUS_SRC_COLOR,
SG_BLENDFACTOR_SRC_ALPHA,
SG_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
SG_BLENDFACTOR_DST_COLOR,
SG_BLENDFACTOR_ONE_MINUS_DST_COLOR,
SG_BLENDFACTOR_DST_ALPHA,
SG_BLENDFACTOR_ONE_MINUS_DST_ALPHA,
SG_BLENDFACTOR_SRC_ALPHA_SATURATED,
SG_BLENDFACTOR_BLEND_COLOR,
SG_BLENDFACTOR_ONE_MINUS_BLEND_COLOR,
SG_BLENDFACTOR_BLEND_ALPHA,
SG_BLENDFACTOR_ONE_MINUS_BLEND_ALPHA,
_SG_BLENDFACTOR_NUM,
_SG_BLENDFACTOR_FORCE_U32 = 0x7FFFFFFF
} sg_blend_factor;
/*
sg_blend_op
Describes how the source and destination values are combined in the
fragment blending operation. It is used in the following members when
creating a pipeline object:
sg_pipeline_desc
.colors[i]
.blend
.op_rgb
.op_alpha
The default value is SG_BLENDOP_ADD.
*/
typedef enum sg_blend_op {
_SG_BLENDOP_DEFAULT, /* value 0 reserved for default-init */
SG_BLENDOP_ADD,
SG_BLENDOP_SUBTRACT,
SG_BLENDOP_REVERSE_SUBTRACT,
_SG_BLENDOP_NUM,
_SG_BLENDOP_FORCE_U32 = 0x7FFFFFFF
} sg_blend_op;
/*
sg_color_mask
Selects the active color channels when writing a fragment color to the
framebuffer. This is used in the members
sg_pipeline_desc.colors[i].write_mask when creating a pipeline object.
The default colormask is SG_COLORMASK_RGBA (write all colors channels)
NOTE: since the color mask value 0 is reserved for the default value
(SG_COLORMASK_RGBA), use SG_COLORMASK_NONE if all color channels
should be disabled.
*/
typedef enum sg_color_mask {
_SG_COLORMASK_DEFAULT = 0, /* value 0 reserved for default-init */
SG_COLORMASK_NONE = 0x10, /* special value for 'all channels disabled */
SG_COLORMASK_R = 0x1,
SG_COLORMASK_G = 0x2,
SG_COLORMASK_RG = 0x3,
SG_COLORMASK_B = 0x4,
SG_COLORMASK_RB = 0x5,
SG_COLORMASK_GB = 0x6,
SG_COLORMASK_RGB = 0x7,
SG_COLORMASK_A = 0x8,
SG_COLORMASK_RA = 0x9,
SG_COLORMASK_GA = 0xA,
SG_COLORMASK_RGA = 0xB,
SG_COLORMASK_BA = 0xC,
SG_COLORMASK_RBA = 0xD,
SG_COLORMASK_GBA = 0xE,
SG_COLORMASK_RGBA = 0xF,
_SG_COLORMASK_FORCE_U32 = 0x7FFFFFFF
} sg_color_mask;
/*
sg_action
Defines what action should be performed at the start of a render pass:
SG_ACTION_CLEAR: clear the render target image
SG_ACTION_LOAD: load the previous content of the render target image
SG_ACTION_DONTCARE: leave the render target image content undefined
This is used in the sg_pass_action structure.
The default action for all pass attachments is SG_ACTION_CLEAR, with the
clear color rgba = {0.5f, 0.5f, 0.5f, 1.0f], depth=1.0 and stencil=0.
If you want to override the default behaviour, it is important to not
only set the clear color, but the 'action' field as well (as long as this
is in its _SG_ACTION_DEFAULT, the value fields will be ignored).
*/
typedef enum sg_action {
_SG_ACTION_DEFAULT,
SG_ACTION_CLEAR,
SG_ACTION_LOAD,
SG_ACTION_DONTCARE,
_SG_ACTION_NUM,
_SG_ACTION_FORCE_U32 = 0x7FFFFFFF
} sg_action;
/*
sg_pass_action
The sg_pass_action struct defines the actions to be performed
at the start of a rendering pass in the functions sg_begin_pass()
and sg_begin_default_pass().
A separate action and clear values can be defined for each
color attachment, and for the depth-stencil attachment.
The default clear values are defined by the macros:
- SG_DEFAULT_CLEAR_RED: 0.5f
- SG_DEFAULT_CLEAR_GREEN: 0.5f
- SG_DEFAULT_CLEAR_BLUE: 0.5f
- SG_DEFAULT_CLEAR_ALPHA: 1.0f
- SG_DEFAULT_CLEAR_DEPTH: 1.0f
- SG_DEFAULT_CLEAR_STENCIL: 0
*/
typedef struct sg_color_attachment_action {
sg_action action;
sg_color value;
} sg_color_attachment_action;
typedef struct sg_depth_attachment_action {
sg_action action;
float value;
} sg_depth_attachment_action;
typedef struct sg_stencil_attachment_action {
sg_action action;
uint8_t value;
} sg_stencil_attachment_action;
typedef struct sg_pass_action {
uint32_t _start_canary;
sg_color_attachment_action colors[SG_MAX_COLOR_ATTACHMENTS];
sg_depth_attachment_action depth;
sg_stencil_attachment_action stencil;
uint32_t _end_canary;
} sg_pass_action;
/*
sg_bindings
The sg_bindings structure defines the resource binding slots
of the sokol_gfx render pipeline, used as argument to the
sg_apply_bindings() function.
A resource binding struct contains:
- 1..N vertex buffers
- 0..N vertex buffer offsets
- 0..1 index buffers
- 0..1 index buffer offsets
- 0..N vertex shader stage images
- 0..N fragment shader stage images
The max number of vertex buffer and shader stage images
are defined by the SG_MAX_SHADERSTAGE_BUFFERS and
SG_MAX_SHADERSTAGE_IMAGES configuration constants.
The optional buffer offsets can be used to put different unrelated
chunks of vertex- and/or index-data into the same buffer objects.
*/
typedef struct sg_bindings {
uint32_t _start_canary;
sg_buffer vertex_buffers[SG_MAX_SHADERSTAGE_BUFFERS];
int vertex_buffer_offsets[SG_MAX_SHADERSTAGE_BUFFERS];
sg_buffer index_buffer;
int index_buffer_offset;
sg_image vs_images[SG_MAX_SHADERSTAGE_IMAGES];
sg_image fs_images[SG_MAX_SHADERSTAGE_IMAGES];
uint32_t _end_canary;
} sg_bindings;
/*
sg_buffer_desc
Creation parameters for sg_buffer objects, used in the
sg_make_buffer() call.
The default configuration is:
.size: 0 (*must* be >0 for buffers without data)
.type: SG_BUFFERTYPE_VERTEXBUFFER
.usage: SG_USAGE_IMMUTABLE
.data.ptr 0 (*must* be valid for immutable buffers)
.data.size 0 (*must* be > 0 for immutable buffers)
.label 0 (optional string label for trace hooks)
The label will be ignored by sokol_gfx.h, it is only useful
when hooking into sg_make_buffer() or sg_init_buffer() via
the sg_install_trace_hooks() function.
For immutable buffers which are initialized with initial data,
keep the .size item zero-initialized, and set the size together with the
pointer to the initial data in the .data item.
For mutable buffers without initial data, keep the .data item
zero-initialized, and set the buffer size in the .size item instead.
You can also set both size values, but currently both size values must
be identical (this may change in the future when the dynamic resource
management may become more flexible).
ADVANCED TOPIC: Injecting native 3D-API buffers:
The following struct members allow to inject your own GL, Metal
or D3D11 buffers into sokol_gfx:
.gl_buffers[SG_NUM_INFLIGHT_FRAMES]
.mtl_buffers[SG_NUM_INFLIGHT_FRAMES]
.d3d11_buffer
You must still provide all other struct items except the .data item, and
these must match the creation parameters of the native buffers you
provide. For SG_USAGE_IMMUTABLE, only provide a single native 3D-API
buffer, otherwise you need to provide SG_NUM_INFLIGHT_FRAMES buffers
(only for GL and Metal, not D3D11). Providing multiple buffers for GL and
Metal is necessary because sokol_gfx will rotate through them when
calling sg_update_buffer() to prevent lock-stalls.
Note that it is expected that immutable injected buffer have already been
initialized with content, and the .content member must be 0!
Also you need to call sg_reset_state_cache() after calling native 3D-API
functions, and before calling any sokol_gfx function.
*/
typedef struct sg_buffer_desc {
uint32_t _start_canary;
size_t size;
sg_buffer_type type;
sg_usage usage;
sg_range data;
const char* label;
/* GL specific */
uint32_t gl_buffers[SG_NUM_INFLIGHT_FRAMES];
/* Metal specific */
const void* mtl_buffers[SG_NUM_INFLIGHT_FRAMES];
/* D3D11 specific */
const void* d3d11_buffer;
/* WebGPU specific */
const void* wgpu_buffer;
uint32_t _end_canary;
} sg_buffer_desc;
/*
sg_image_data
Defines the content of an image through a 2D array of sg_range structs.
The first array dimension is the cubemap face, and the second array
dimension the mipmap level.
*/
typedef struct sg_image_data {
sg_range subimage[SG_CUBEFACE_NUM][SG_MAX_MIPMAPS];
} sg_image_data;
/*
sg_image_desc
Creation parameters for sg_image objects, used in the sg_make_image()
call.
The default configuration is:
.type: SG_IMAGETYPE_2D
.render_target: false
.width 0 (must be set to >0)
.height 0 (must be set to >0)
.num_slices 1 (3D textures: depth; array textures: number of layers)
.num_mipmaps: 1
.usage: SG_USAGE_IMMUTABLE
.pixel_format: SG_PIXELFORMAT_RGBA8 for textures, or sg_desc.context.color_format for render targets
.sample_count: 1 for textures, or sg_desc.context.sample_count for render targets
.min_filter: SG_FILTER_NEAREST
.mag_filter: SG_FILTER_NEAREST
.wrap_u: SG_WRAP_REPEAT
.wrap_v: SG_WRAP_REPEAT
.wrap_w: SG_WRAP_REPEAT (only SG_IMAGETYPE_3D)
.border_color SG_BORDERCOLOR_OPAQUE_BLACK
.max_anisotropy 1 (must be 1..16)
.min_lod 0.0f
.max_lod FLT_MAX
.data an sg_image_data struct to define the initial content
.label 0 (optional string label for trace hooks)
Q: Why is the default sample_count for render targets identical with the
"default sample count" from sg_desc.context.sample_count?
A: So that it matches the default sample count in pipeline objects. Even
though it is a bit strange/confusing that offscreen render targets by default
get the same sample count as the default framebuffer, but it's better that
an offscreen render target created with default parameters matches
a pipeline object created with default parameters.
NOTE:
SG_IMAGETYPE_ARRAY and SG_IMAGETYPE_3D are not supported on WebGL/GLES2,
use sg_query_features().imagetype_array and
sg_query_features().imagetype_3d at runtime to check if array- and
3D-textures are supported.
Images with usage SG_USAGE_IMMUTABLE must be fully initialized by
providing a valid .data member which points to initialization data.
ADVANCED TOPIC: Injecting native 3D-API textures:
The following struct members allow to inject your own GL, Metal or D3D11
textures into sokol_gfx:
.gl_textures[SG_NUM_INFLIGHT_FRAMES]
.mtl_textures[SG_NUM_INFLIGHT_FRAMES]
.d3d11_texture
.d3d11_shader_resource_view
For GL, you can also specify the texture target or leave it empty to use
the default texture target for the image type (GL_TEXTURE_2D for
SG_IMAGETYPE_2D etc)
For D3D11, you can provide either a D3D11 texture, or a
shader-resource-view, or both. If only a texture is provided, a matching
shader-resource-view will be created. If only a shader-resource-view is
provided, the texture will be looked up from the shader-resource-view.
The same rules apply as for injecting native buffers (see sg_buffer_desc
documentation for more details).
*/
typedef struct sg_image_desc {
uint32_t _start_canary;
sg_image_type type;
bool render_target;
int width;
int height;
int num_slices;
int num_mipmaps;
sg_usage usage;
sg_pixel_format pixel_format;
int sample_count;
sg_filter min_filter;
sg_filter mag_filter;
sg_wrap wrap_u;
sg_wrap wrap_v;
sg_wrap wrap_w;
sg_border_color border_color;
uint32_t max_anisotropy;
float min_lod;
float max_lod;
sg_image_data data;
const char* label;
/* GL specific */
uint32_t gl_textures[SG_NUM_INFLIGHT_FRAMES];
uint32_t gl_texture_target;
/* Metal specific */
const void* mtl_textures[SG_NUM_INFLIGHT_FRAMES];
/* D3D11 specific */
const void* d3d11_texture;
const void* d3d11_shader_resource_view;
/* WebGPU specific */
const void* wgpu_texture;
uint32_t _end_canary;
} sg_image_desc;
/*
sg_shader_desc
The structure sg_shader_desc defines all creation parameters for shader
programs, used as input to the sg_make_shader() function:
- reflection information for vertex attributes (vertex shader inputs):
- vertex attribute name (required for GLES2, optional for GLES3 and GL)
- a semantic name and index (required for D3D11)
- for each shader-stage (vertex and fragment):
- the shader source or bytecode
- an optional entry function name
- an optional compile target (only for D3D11 when source is provided,
defaults are "vs_4_0" and "ps_4_0")
- reflection info for each uniform block used by the shader stage:
- the size of the uniform block in bytes
- reflection info for each uniform block member (only required for GL backends):
- member name
- member type (SG_UNIFORMTYPE_xxx)
- if the member is an array, the number of array items
- reflection info for the texture images used by the shader stage:
- the image type (SG_IMAGETYPE_xxx)
- the sampler type (SG_SAMPLERTYPE_xxx, default is SG_SAMPLERTYPE_FLOAT)
- the name of the texture sampler (required for GLES2, optional everywhere else)
For all GL backends, shader source-code must be provided. For D3D11 and Metal,
either shader source-code or byte-code can be provided.
For D3D11, if source code is provided, the d3dcompiler_47.dll will be loaded
on demand. If this fails, shader creation will fail. When compiling HLSL
source code, you can provide an optional target string via
sg_shader_stage_desc.d3d11_target, the default target is "vs_4_0" for the
vertex shader stage and "ps_4_0" for the pixel shader stage.
*/
typedef struct sg_shader_attr_desc {
const char* name; // GLSL vertex attribute name (only strictly required for GLES2)
const char* sem_name; // HLSL semantic name
int sem_index; // HLSL semantic index
} sg_shader_attr_desc;
typedef struct sg_shader_uniform_desc {
const char* name;
sg_uniform_type type;
int array_count;
} sg_shader_uniform_desc;
typedef struct sg_shader_uniform_block_desc {
size_t size;
sg_shader_uniform_desc uniforms[SG_MAX_UB_MEMBERS];
} sg_shader_uniform_block_desc;
typedef struct sg_shader_image_desc {
const char* name;
sg_image_type image_type;
sg_sampler_type sampler_type;
} sg_shader_image_desc;
typedef struct sg_shader_stage_desc {
const char* source;
sg_range bytecode;
const char* entry;
const char* d3d11_target;
sg_shader_uniform_block_desc uniform_blocks[SG_MAX_SHADERSTAGE_UBS];
sg_shader_image_desc images[SG_MAX_SHADERSTAGE_IMAGES];
} sg_shader_stage_desc;
typedef struct sg_shader_desc {
uint32_t _start_canary;
sg_shader_attr_desc attrs[SG_MAX_VERTEX_ATTRIBUTES];
sg_shader_stage_desc vs;
sg_shader_stage_desc fs;
const char* label;
uint32_t _end_canary;
} sg_shader_desc;
/*
sg_pipeline_desc
The sg_pipeline_desc struct defines all creation parameters for an
sg_pipeline object, used as argument to the sg_make_pipeline() function:
- the vertex layout for all input vertex buffers
- a shader object
- the 3D primitive type (points, lines, triangles, ...)
- the index type (none, 16- or 32-bit)
- all the fixed-function-pipeline state (depth-, stencil-, blend-state, etc...)
If the vertex data has no gaps between vertex components, you can omit
the .layout.buffers[].stride and layout.attrs[].offset items (leave them
default-initialized to 0), sokol-gfx will then compute the offsets and
strides from the vertex component formats (.layout.attrs[].format).
Please note that ALL vertex attribute offsets must be 0 in order for the
automatic offset computation to kick in.
The default configuration is as follows:
.shader: 0 (must be initialized with a valid sg_shader id!)
.layout:
.buffers[]: vertex buffer layouts
.stride: 0 (if no stride is given it will be computed)
.step_func SG_VERTEXSTEP_PER_VERTEX
.step_rate 1
.attrs[]: vertex attribute declarations
.buffer_index 0 the vertex buffer bind slot
.offset 0 (offsets can be omitted if the vertex layout has no gaps)
.format SG_VERTEXFORMAT_INVALID (must be initialized!)
.depth:
.pixel_format: sg_desc.context.depth_format
.compare: SG_COMPAREFUNC_ALWAYS
.write_enabled: false
.bias: 0.0f
.bias_slope_scale: 0.0f
.bias_clamp: 0.0f
.stencil:
.enabled: false
.front/back:
.compare: SG_COMPAREFUNC_ALWAYS
.depth_fail_op: SG_STENCILOP_KEEP
.pass_op: SG_STENCILOP_KEEP
.compare: SG_COMPAREFUNC_ALWAYS
.read_mask: 0
.write_mask: 0
.ref: 0
.color_count 1
.colors[0..color_count]
.pixel_format sg_desc.context.color_format
.write_mask: SG_COLORMASK_RGBA
.blend:
.enabled: false
.src_factor_rgb: SG_BLENDFACTOR_ONE
.dst_factor_rgb: SG_BLENDFACTOR_ZERO
.op_rgb: SG_BLENDOP_ADD
.src_factor_alpha: SG_BLENDFACTOR_ONE
.dst_factor_alpha: SG_BLENDFACTOR_ZERO
.op_alpha: SG_BLENDOP_ADD
.primitive_type: SG_PRIMITIVETYPE_TRIANGLES
.index_type: SG_INDEXTYPE_NONE
.cull_mode: SG_CULLMODE_NONE
.face_winding: SG_FACEWINDING_CW
.sample_count: sg_desc.context.sample_count
.blend_color: (sg_color) { 0.0f, 0.0f, 0.0f, 0.0f }
.alpha_to_coverage_enabled: false
.label 0 (optional string label for trace hooks)
*/
typedef struct sg_buffer_layout_desc {
int stride;
sg_vertex_step step_func;
int step_rate;
#if defined(SOKOL_ZIG_BINDINGS)
uint32_t __pad[2];
#endif
} sg_buffer_layout_desc;
typedef struct sg_vertex_attr_desc {
int buffer_index;
int offset;
sg_vertex_format format;
#if defined(SOKOL_ZIG_BINDINGS)
uint32_t __pad[2];
#endif
} sg_vertex_attr_desc;
typedef struct sg_layout_desc {
sg_buffer_layout_desc buffers[SG_MAX_SHADERSTAGE_BUFFERS];
sg_vertex_attr_desc attrs[SG_MAX_VERTEX_ATTRIBUTES];
} sg_layout_desc;
typedef struct sg_stencil_face_state {
sg_compare_func compare;
sg_stencil_op fail_op;
sg_stencil_op depth_fail_op;
sg_stencil_op pass_op;
} sg_stencil_face_state;
typedef struct sg_stencil_state {
bool enabled;
sg_stencil_face_state front;
sg_stencil_face_state back;
uint8_t read_mask;
uint8_t write_mask;
uint8_t ref;
} sg_stencil_state;
typedef struct sg_depth_state {
sg_pixel_format pixel_format;
sg_compare_func compare;
bool write_enabled;
float bias;
float bias_slope_scale;
float bias_clamp;
} sg_depth_state;
typedef struct sg_blend_state {
bool enabled;
sg_blend_factor src_factor_rgb;
sg_blend_factor dst_factor_rgb;
sg_blend_op op_rgb;
sg_blend_factor src_factor_alpha;
sg_blend_factor dst_factor_alpha;
sg_blend_op op_alpha;
} sg_blend_state;
typedef struct sg_color_state {
sg_pixel_format pixel_format;
sg_color_mask write_mask;
sg_blend_state blend;
} sg_color_state;
typedef struct sg_pipeline_desc {
uint32_t _start_canary;
sg_shader shader;
sg_layout_desc layout;
sg_depth_state depth;
sg_stencil_state stencil;
int color_count;
sg_color_state colors[SG_MAX_COLOR_ATTACHMENTS];
sg_primitive_type primitive_type;
sg_index_type index_type;
sg_cull_mode cull_mode;
sg_face_winding face_winding;
int sample_count;
sg_color blend_color;
bool alpha_to_coverage_enabled;
const char* label;
uint32_t _end_canary;
} sg_pipeline_desc;
/*
sg_pass_desc
Creation parameters for an sg_pass object, used as argument
to the sg_make_pass() function.
A pass object contains 1..4 color-attachments and none, or one,
depth-stencil-attachment. Each attachment consists of
an image, and two additional indices describing
which subimage the pass will render to: one mipmap index, and
if the image is a cubemap, array-texture or 3D-texture, the
face-index, array-layer or depth-slice.
Pass images must fulfill the following requirements:
All images must have:
- been created as render target (sg_image_desc.render_target = true)
- the same size
- the same sample count
In addition, all color-attachment images must have the same pixel format.
*/
typedef struct sg_pass_attachment_desc {
sg_image image;
int mip_level;
int slice; /* cube texture: face; array texture: layer; 3D texture: slice */
} sg_pass_attachment_desc;
typedef struct sg_pass_desc {
uint32_t _start_canary;
sg_pass_attachment_desc color_attachments[SG_MAX_COLOR_ATTACHMENTS];
sg_pass_attachment_desc depth_stencil_attachment;
const char* label;
uint32_t _end_canary;
} sg_pass_desc;
/*
sg_trace_hooks
Installable callback functions to keep track of the sokol-gfx calls,
this is useful for debugging, or keeping track of resource creation
and destruction.
Trace hooks are installed with sg_install_trace_hooks(), this returns
another sg_trace_hooks struct with the previous set of
trace hook function pointers. These should be invoked by the
new trace hooks to form a proper call chain.
*/
typedef struct sg_trace_hooks {
void* user_data;
void (*reset_state_cache)(void* user_data);
void (*make_buffer)(const sg_buffer_desc* desc, sg_buffer result, void* user_data);
void (*make_image)(const sg_image_desc* desc, sg_image result, void* user_data);
void (*make_shader)(const sg_shader_desc* desc, sg_shader result, void* user_data);
void (*make_pipeline)(const sg_pipeline_desc* desc, sg_pipeline result, void* user_data);
void (*make_pass)(const sg_pass_desc* desc, sg_pass result, void* user_data);
void (*destroy_buffer)(sg_buffer buf, void* user_data);
void (*destroy_image)(sg_image img, void* user_data);
void (*destroy_shader)(sg_shader shd, void* user_data);
void (*destroy_pipeline)(sg_pipeline pip, void* user_data);
void (*destroy_pass)(sg_pass pass, void* user_data);
void (*update_buffer)(sg_buffer buf, const sg_range* data, void* user_data);
void (*update_image)(sg_image img, const sg_image_data* data, void* user_data);
void (*append_buffer)(sg_buffer buf, const sg_range* data, int result, void* user_data);
void (*begin_default_pass)(const sg_pass_action* pass_action, int width, int height, void* user_data);
void (*begin_pass)(sg_pass pass, const sg_pass_action* pass_action, void* user_data);
void (*apply_viewport)(int x, int y, int width, int height, bool origin_top_left, void* user_data);
void (*apply_scissor_rect)(int x, int y, int width, int height, bool origin_top_left, void* user_data);
void (*apply_pipeline)(sg_pipeline pip, void* user_data);
void (*apply_bindings)(const sg_bindings* bindings, void* user_data);
void (*apply_uniforms)(sg_shader_stage stage, int ub_index, const sg_range* data, void* user_data);
void (*draw)(int base_element, int num_elements, int num_instances, void* user_data);
void (*end_pass)(void* user_data);
void (*commit)(void* user_data);
void (*alloc_buffer)(sg_buffer result, void* user_data);
void (*alloc_image)(sg_image result, void* user_data);
void (*alloc_shader)(sg_shader result, void* user_data);
void (*alloc_pipeline)(sg_pipeline result, void* user_data);
void (*alloc_pass)(sg_pass result, void* user_data);
void (*dealloc_buffer)(sg_buffer buf_id, void* user_data);
void (*dealloc_image)(sg_image img_id, void* user_data);
void (*dealloc_shader)(sg_shader shd_id, void* user_data);
void (*dealloc_pipeline)(sg_pipeline pip_id, void* user_data);
void (*dealloc_pass)(sg_pass pass_id, void* user_data);
void (*init_buffer)(sg_buffer buf_id, const sg_buffer_desc* desc, void* user_data);
void (*init_image)(sg_image img_id, const sg_image_desc* desc, void* user_data);
void (*init_shader)(sg_shader shd_id, const sg_shader_desc* desc, void* user_data);
void (*init_pipeline)(sg_pipeline pip_id, const sg_pipeline_desc* desc, void* user_data);
void (*init_pass)(sg_pass pass_id, const sg_pass_desc* desc, void* user_data);
void (*uninit_buffer)(sg_buffer buf_id, void* user_data);
void (*uninit_image)(sg_image img_id, void* user_data);
void (*uninit_shader)(sg_shader shd_id, void* user_data);
void (*uninit_pipeline)(sg_pipeline pip_id, void* user_data);
void (*uninit_pass)(sg_pass pass_id, void* user_data);
void (*fail_buffer)(sg_buffer buf_id, void* user_data);
void (*fail_image)(sg_image img_id, void* user_data);
void (*fail_shader)(sg_shader shd_id, void* user_data);
void (*fail_pipeline)(sg_pipeline pip_id, void* user_data);
void (*fail_pass)(sg_pass pass_id, void* user_data);
void (*push_debug_group)(const char* name, void* user_data);
void (*pop_debug_group)(void* user_data);
void (*err_buffer_pool_exhausted)(void* user_data);
void (*err_image_pool_exhausted)(void* user_data);
void (*err_shader_pool_exhausted)(void* user_data);
void (*err_pipeline_pool_exhausted)(void* user_data);
void (*err_pass_pool_exhausted)(void* user_data);
void (*err_context_mismatch)(void* user_data);
void (*err_pass_invalid)(void* user_data);
void (*err_draw_invalid)(void* user_data);
void (*err_bindings_invalid)(void* user_data);
} sg_trace_hooks;
/*
sg_buffer_info
sg_image_info
sg_shader_info
sg_pipeline_info
sg_pass_info
These structs contain various internal resource attributes which
might be useful for debug-inspection. Please don't rely on the
actual content of those structs too much, as they are quite closely
tied to sokol_gfx.h internals and may change more frequently than
the other public API elements.
The *_info structs are used as the return values of the following functions:
sg_query_buffer_info()
sg_query_image_info()
sg_query_shader_info()
sg_query_pipeline_info()
sg_query_pass_info()
*/
typedef struct sg_slot_info {
sg_resource_state state; /* the current state of this resource slot */
uint32_t res_id; /* type-neutral resource if (e.g. sg_buffer.id) */
uint32_t ctx_id; /* the context this resource belongs to */
} sg_slot_info;
typedef struct sg_buffer_info {
sg_slot_info slot; /* resource pool slot info */
uint32_t update_frame_index; /* frame index of last sg_update_buffer() */
uint32_t append_frame_index; /* frame index of last sg_append_buffer() */
int append_pos; /* current position in buffer for sg_append_buffer() */
bool append_overflow; /* is buffer in overflow state (due to sg_append_buffer) */
int num_slots; /* number of renaming-slots for dynamically updated buffers */
int active_slot; /* currently active write-slot for dynamically updated buffers */
} sg_buffer_info;
typedef struct sg_image_info {
sg_slot_info slot; /* resource pool slot info */
uint32_t upd_frame_index; /* frame index of last sg_update_image() */
int num_slots; /* number of renaming-slots for dynamically updated images */
int active_slot; /* currently active write-slot for dynamically updated images */
int width; /* image width */
int height; /* image height */
} sg_image_info;
typedef struct sg_shader_info {
sg_slot_info slot; /* resoure pool slot info */
} sg_shader_info;
typedef struct sg_pipeline_info {
sg_slot_info slot; /* resource pool slot info */
} sg_pipeline_info;
typedef struct sg_pass_info {
sg_slot_info slot; /* resource pool slot info */
} sg_pass_info;
/*
sg_desc
The sg_desc struct contains configuration values for sokol_gfx,
it is used as parameter to the sg_setup() call.
NOTE that all callback function pointers come in two versions, one without
a userdata pointer, and one with a userdata pointer. You would
either initialize one or the other depending on whether you pass data
to your callbacks.
FIXME: explain the various configuration options
The default configuration is:
.buffer_pool_size 128
.image_pool_size 128
.shader_pool_size 32
.pipeline_pool_size 64
.pass_pool_size 16
.context_pool_size 16
.sampler_cache_size 64
.uniform_buffer_size 4 MB (4*1024*1024)
.staging_buffer_size 8 MB (8*1024*1024)
.context.color_format: default value depends on selected backend:
all GL backends: SG_PIXELFORMAT_RGBA8
Metal and D3D11: SG_PIXELFORMAT_BGRA8
WGPU: *no default* (must be queried from WGPU swapchain)
.context.depth_format SG_PIXELFORMAT_DEPTH_STENCIL
.context.sample_count 1
GL specific:
.context.gl.force_gles2
if this is true the GL backend will act in "GLES2 fallback mode" even
when compiled with SOKOL_GLES3, this is useful to fall back
to traditional WebGL if a browser doesn't support a WebGL2 context
Metal specific:
(NOTE: All Objective-C object references are transferred through
a bridged (const void*) to sokol_gfx, which will use a unretained
bridged cast (__bridged id<xxx>) to retrieve the Objective-C
references back. Since the bridge cast is unretained, the caller
must hold a strong reference to the Objective-C object for the
duration of the sokol_gfx call!
.context.metal.device
a pointer to the MTLDevice object
.context.metal.renderpass_descriptor_cb
.context.metal_renderpass_descriptor_userdata_cb
A C callback function to obtain the MTLRenderPassDescriptor for the
current frame when rendering to the default framebuffer, will be called
in sg_begin_default_pass().
.context.metal.drawable_cb
.context.metal.drawable_userdata_cb
a C callback function to obtain a MTLDrawable for the current
frame when rendering to the default framebuffer, will be called in
sg_end_pass() of the default pass
.context.metal.user_data
optional user data pointer passed to the userdata versions of
callback functions
D3D11 specific:
.context.d3d11.device
a pointer to the ID3D11Device object, this must have been created
before sg_setup() is called
.context.d3d11.device_context
a pointer to the ID3D11DeviceContext object
.context.d3d11.render_target_view_cb
.context.d3d11.render_target_view_userdata_cb
a C callback function to obtain a pointer to the current
ID3D11RenderTargetView object of the default framebuffer,
this function will be called in sg_begin_pass() when rendering
to the default framebuffer
.context.d3d11.depth_stencil_view_cb
.context.d3d11.depth_stencil_view_userdata_cb
a C callback function to obtain a pointer to the current
ID3D11DepthStencilView object of the default framebuffer,
this function will be called in sg_begin_pass() when rendering
to the default framebuffer
.context.metal.user_data
optional user data pointer passed to the userdata versions of
callback functions
WebGPU specific:
.context.wgpu.device
a WGPUDevice handle
.context.wgpu.render_format
WGPUTextureFormat of the swap chain surface
.context.wgpu.render_view_cb
.context.wgpu.render_view_userdata_cb
callback to get the current WGPUTextureView of the swapchain's
rendering attachment (may be an MSAA surface)
.context.wgpu.resolve_view_cb
.context.wgpu.resolve_view_userdata_cb
callback to get the current WGPUTextureView of the swapchain's
MSAA-resolve-target surface, must return 0 if not MSAA rendering
.context.wgpu.depth_stencil_view_cb
.context.wgpu.depth_stencil_view_userdata_cb
callback to get current default-pass depth-stencil-surface WGPUTextureView
the pixel format of the default WGPUTextureView must be WGPUTextureFormat_Depth24Plus8
.context.metal.user_data
optional user data pointer passed to the userdata versions of
callback functions
When using sokol_gfx.h and sokol_app.h together, consider using the
helper function sapp_sgcontext() in the sokol_glue.h header to
initialize the sg_desc.context nested struct. sapp_sgcontext() returns
a completely initialized sg_context_desc struct with information
provided by sokol_app.h.
*/
typedef struct sg_gl_context_desc {
bool force_gles2;
} sg_gl_context_desc;
typedef struct sg_metal_context_desc {
const void* device;
const void* (*renderpass_descriptor_cb)(void);
const void* (*renderpass_descriptor_userdata_cb)(void*);
const void* (*drawable_cb)(void);
const void* (*drawable_userdata_cb)(void*);
void* user_data;
} sg_metal_context_desc;
typedef struct sg_d3d11_context_desc {
const void* device;
const void* device_context;
const void* (*render_target_view_cb)(void);
const void* (*render_target_view_userdata_cb)(void*);
const void* (*depth_stencil_view_cb)(void);
const void* (*depth_stencil_view_userdata_cb)(void*);
void* user_data;
} sg_d3d11_context_desc;
typedef struct sg_wgpu_context_desc {
const void* device; /* WGPUDevice */
const void* (*render_view_cb)(void); /* returns WGPUTextureView */
const void* (*render_view_userdata_cb)(void*);
const void* (*resolve_view_cb)(void); /* returns WGPUTextureView */
const void* (*resolve_view_userdata_cb)(void*);
const void* (*depth_stencil_view_cb)(void); /* returns WGPUTextureView, must be WGPUTextureFormat_Depth24Plus8 */
const void* (*depth_stencil_view_userdata_cb)(void*);
void* user_data;
} sg_wgpu_context_desc;
typedef struct sg_context_desc {
sg_pixel_format color_format;
sg_pixel_format depth_format;
int sample_count;
sg_gl_context_desc gl;
sg_metal_context_desc metal;
sg_d3d11_context_desc d3d11;
sg_wgpu_context_desc wgpu;
} sg_context_desc;
typedef struct sg_desc {
uint32_t _start_canary;
int buffer_pool_size;
int image_pool_size;
int shader_pool_size;
int pipeline_pool_size;
int pass_pool_size;
int context_pool_size;
int uniform_buffer_size;
int staging_buffer_size;
int sampler_cache_size;
sg_context_desc context;
uint32_t _end_canary;
} sg_desc;
/* setup and misc functions */
SOKOL_GFX_API_DECL void sg_setup(const sg_desc* desc);
SOKOL_GFX_API_DECL void sg_shutdown(void);
SOKOL_GFX_API_DECL bool sg_isvalid(void);
SOKOL_GFX_API_DECL void sg_reset_state_cache(void);
SOKOL_GFX_API_DECL sg_trace_hooks sg_install_trace_hooks(const sg_trace_hooks* trace_hooks);
SOKOL_GFX_API_DECL void sg_push_debug_group(const char* name);
SOKOL_GFX_API_DECL void sg_pop_debug_group(void);
/* resource creation, destruction and updating */
SOKOL_GFX_API_DECL sg_buffer sg_make_buffer(const sg_buffer_desc* desc);
SOKOL_GFX_API_DECL sg_image sg_make_image(const sg_image_desc* desc);
SOKOL_GFX_API_DECL sg_shader sg_make_shader(const sg_shader_desc* desc);
SOKOL_GFX_API_DECL sg_pipeline sg_make_pipeline(const sg_pipeline_desc* desc);
SOKOL_GFX_API_DECL sg_pass sg_make_pass(const sg_pass_desc* desc);
SOKOL_GFX_API_DECL void sg_destroy_buffer(sg_buffer buf);
SOKOL_GFX_API_DECL void sg_destroy_image(sg_image img);
SOKOL_GFX_API_DECL void sg_destroy_shader(sg_shader shd);
SOKOL_GFX_API_DECL void sg_destroy_pipeline(sg_pipeline pip);
SOKOL_GFX_API_DECL void sg_destroy_pass(sg_pass pass);
SOKOL_GFX_API_DECL void sg_update_buffer(sg_buffer buf, const sg_range* data);
SOKOL_GFX_API_DECL void sg_update_image(sg_image img, const sg_image_data* data);
SOKOL_GFX_API_DECL int sg_append_buffer(sg_buffer buf, const sg_range* data);
SOKOL_GFX_API_DECL bool sg_query_buffer_overflow(sg_buffer buf);
/* rendering functions */
SOKOL_GFX_API_DECL void sg_begin_default_pass(const sg_pass_action* pass_action, int width, int height);
SOKOL_GFX_API_DECL void sg_begin_default_passf(const sg_pass_action* pass_action, float width, float height);
SOKOL_GFX_API_DECL void sg_begin_pass(sg_pass pass, const sg_pass_action* pass_action);
SOKOL_GFX_API_DECL void sg_apply_viewport(int x, int y, int width, int height, bool origin_top_left);
SOKOL_GFX_API_DECL void sg_apply_viewportf(float x, float y, float width, float height, bool origin_top_left);
SOKOL_GFX_API_DECL void sg_apply_scissor_rect(int x, int y, int width, int height, bool origin_top_left);
SOKOL_GFX_API_DECL void sg_apply_scissor_rectf(float x, float y, float width, float height, bool origin_top_left);
SOKOL_GFX_API_DECL void sg_apply_pipeline(sg_pipeline pip);
SOKOL_GFX_API_DECL void sg_apply_bindings(const sg_bindings* bindings);
SOKOL_GFX_API_DECL void sg_apply_uniforms(sg_shader_stage stage, int ub_index, const sg_range* data);
SOKOL_GFX_API_DECL void sg_draw(int base_element, int num_elements, int num_instances);
SOKOL_GFX_API_DECL void sg_end_pass(void);
SOKOL_GFX_API_DECL void sg_commit(void);
/* getting information */
SOKOL_GFX_API_DECL sg_desc sg_query_desc(void);
SOKOL_GFX_API_DECL sg_backend sg_query_backend(void);
SOKOL_GFX_API_DECL sg_features sg_query_features(void);
SOKOL_GFX_API_DECL sg_limits sg_query_limits(void);
SOKOL_GFX_API_DECL sg_pixelformat_info sg_query_pixelformat(sg_pixel_format fmt);
/* get current state of a resource (INITIAL, ALLOC, VALID, FAILED, INVALID) */
SOKOL_GFX_API_DECL sg_resource_state sg_query_buffer_state(sg_buffer buf);
SOKOL_GFX_API_DECL sg_resource_state sg_query_image_state(sg_image img);
SOKOL_GFX_API_DECL sg_resource_state sg_query_shader_state(sg_shader shd);
SOKOL_GFX_API_DECL sg_resource_state sg_query_pipeline_state(sg_pipeline pip);
SOKOL_GFX_API_DECL sg_resource_state sg_query_pass_state(sg_pass pass);
/* get runtime information about a resource */
SOKOL_GFX_API_DECL sg_buffer_info sg_query_buffer_info(sg_buffer buf);
SOKOL_GFX_API_DECL sg_image_info sg_query_image_info(sg_image img);
SOKOL_GFX_API_DECL sg_shader_info sg_query_shader_info(sg_shader shd);
SOKOL_GFX_API_DECL sg_pipeline_info sg_query_pipeline_info(sg_pipeline pip);
SOKOL_GFX_API_DECL sg_pass_info sg_query_pass_info(sg_pass pass);
/* get resource creation desc struct with their default values replaced */
SOKOL_GFX_API_DECL sg_buffer_desc sg_query_buffer_defaults(const sg_buffer_desc* desc);
SOKOL_GFX_API_DECL sg_image_desc sg_query_image_defaults(const sg_image_desc* desc);
SOKOL_GFX_API_DECL sg_shader_desc sg_query_shader_defaults(const sg_shader_desc* desc);
SOKOL_GFX_API_DECL sg_pipeline_desc sg_query_pipeline_defaults(const sg_pipeline_desc* desc);
SOKOL_GFX_API_DECL sg_pass_desc sg_query_pass_defaults(const sg_pass_desc* desc);
/* separate resource allocation and initialization (for async setup) */
SOKOL_GFX_API_DECL sg_buffer sg_alloc_buffer(void);
SOKOL_GFX_API_DECL sg_image sg_alloc_image(void);
SOKOL_GFX_API_DECL sg_shader sg_alloc_shader(void);
SOKOL_GFX_API_DECL sg_pipeline sg_alloc_pipeline(void);
SOKOL_GFX_API_DECL sg_pass sg_alloc_pass(void);
SOKOL_GFX_API_DECL void sg_dealloc_buffer(sg_buffer buf_id);
SOKOL_GFX_API_DECL void sg_dealloc_image(sg_image img_id);
SOKOL_GFX_API_DECL void sg_dealloc_shader(sg_shader shd_id);
SOKOL_GFX_API_DECL void sg_dealloc_pipeline(sg_pipeline pip_id);
SOKOL_GFX_API_DECL void sg_dealloc_pass(sg_pass pass_id);
SOKOL_GFX_API_DECL void sg_init_buffer(sg_buffer buf_id, const sg_buffer_desc* desc);
SOKOL_GFX_API_DECL void sg_init_image(sg_image img_id, const sg_image_desc* desc);
SOKOL_GFX_API_DECL void sg_init_shader(sg_shader shd_id, const sg_shader_desc* desc);
SOKOL_GFX_API_DECL void sg_init_pipeline(sg_pipeline pip_id, const sg_pipeline_desc* desc);
SOKOL_GFX_API_DECL void sg_init_pass(sg_pass pass_id, const sg_pass_desc* desc);
SOKOL_GFX_API_DECL bool sg_uninit_buffer(sg_buffer buf_id);
SOKOL_GFX_API_DECL bool sg_uninit_image(sg_image img_id);
SOKOL_GFX_API_DECL bool sg_uninit_shader(sg_shader shd_id);
SOKOL_GFX_API_DECL bool sg_uninit_pipeline(sg_pipeline pip_id);
SOKOL_GFX_API_DECL bool sg_uninit_pass(sg_pass pass_id);
SOKOL_GFX_API_DECL void sg_fail_buffer(sg_buffer buf_id);
SOKOL_GFX_API_DECL void sg_fail_image(sg_image img_id);
SOKOL_GFX_API_DECL void sg_fail_shader(sg_shader shd_id);
SOKOL_GFX_API_DECL void sg_fail_pipeline(sg_pipeline pip_id);
SOKOL_GFX_API_DECL void sg_fail_pass(sg_pass pass_id);
/* rendering contexts (optional) */
SOKOL_GFX_API_DECL sg_context sg_setup_context(void);
SOKOL_GFX_API_DECL void sg_activate_context(sg_context ctx_id);
SOKOL_GFX_API_DECL void sg_discard_context(sg_context ctx_id);
/* Backend-specific helper functions, these may come in handy for mixing
sokol-gfx rendering with 'native backend' rendering functions.
This group of functions will be expanded as needed.
*/
/* D3D11: return ID3D11Device */
SOKOL_GFX_API_DECL const void* sg_d3d11_device(void);
/* Metal: return __bridge-casted MTLDevice */
SOKOL_GFX_API_DECL const void* sg_mtl_device(void);
/* Metal: return __bridge-casted MTLRenderCommandEncoder in current pass (or zero if outside pass) */
SOKOL_GFX_API_DECL const void* sg_mtl_render_command_encoder(void);
#ifdef __cplusplus
} /* extern "C" */
/* reference-based equivalents for c++ */
inline void sg_setup(const sg_desc& desc) { return sg_setup(&desc); }
inline sg_buffer sg_make_buffer(const sg_buffer_desc& desc) { return sg_make_buffer(&desc); }
inline sg_image sg_make_image(const sg_image_desc& desc) { return sg_make_image(&desc); }
inline sg_shader sg_make_shader(const sg_shader_desc& desc) { return sg_make_shader(&desc); }
inline sg_pipeline sg_make_pipeline(const sg_pipeline_desc& desc) { return sg_make_pipeline(&desc); }
inline sg_pass sg_make_pass(const sg_pass_desc& desc) { return sg_make_pass(&desc); }
inline void sg_update_image(sg_image img, const sg_image_data& data) { return sg_update_image(img, &data); }
inline void sg_begin_default_pass(const sg_pass_action& pass_action, int width, int height) { return sg_begin_default_pass(&pass_action, width, height); }
inline void sg_begin_default_passf(const sg_pass_action& pass_action, float width, float height) { return sg_begin_default_passf(&pass_action, width, height); }
inline void sg_begin_pass(sg_pass pass, const sg_pass_action& pass_action) { return sg_begin_pass(pass, &pass_action); }
inline void sg_apply_bindings(const sg_bindings& bindings) { return sg_apply_bindings(&bindings); }
inline void sg_apply_uniforms(sg_shader_stage stage, int ub_index, const sg_range& data) { return sg_apply_uniforms(stage, ub_index, &data); }
inline sg_buffer_desc sg_query_buffer_defaults(const sg_buffer_desc& desc) { return sg_query_buffer_defaults(&desc); }
inline sg_image_desc sg_query_image_defaults(const sg_image_desc& desc) { return sg_query_image_defaults(&desc); }
inline sg_shader_desc sg_query_shader_defaults(const sg_shader_desc& desc) { return sg_query_shader_defaults(&desc); }
inline sg_pipeline_desc sg_query_pipeline_defaults(const sg_pipeline_desc& desc) { return sg_query_pipeline_defaults(&desc); }
inline sg_pass_desc sg_query_pass_defaults(const sg_pass_desc& desc) { return sg_query_pass_defaults(&desc); }
inline void sg_init_buffer(sg_buffer buf_id, const sg_buffer_desc& desc) { return sg_init_buffer(buf_id, &desc); }
inline void sg_init_image(sg_image img_id, const sg_image_desc& desc) { return sg_init_image(img_id, &desc); }
inline void sg_init_shader(sg_shader shd_id, const sg_shader_desc& desc) { return sg_init_shader(shd_id, &desc); }
inline void sg_init_pipeline(sg_pipeline pip_id, const sg_pipeline_desc& desc) { return sg_init_pipeline(pip_id, &desc); }
inline void sg_init_pass(sg_pass pass_id, const sg_pass_desc& desc) { return sg_init_pass(pass_id, &desc); }
inline void sg_update_buffer(sg_buffer buf_id, const sg_range& data) { return sg_update_buffer(buf_id, &data); }
inline int sg_append_buffer(sg_buffer buf_id, const sg_range& data) { return sg_append_buffer(buf_id, &data); }
#endif
#endif // SOKOL_GFX_INCLUDED
/*--- IMPLEMENTATION ---------------------------------------------------------*/
#ifdef SOKOL_GFX_IMPL
#define SOKOL_GFX_IMPL_INCLUDED (1)
#if !(defined(SOKOL_GLCORE33)||defined(SOKOL_GLES2)||defined(SOKOL_GLES3)||defined(SOKOL_D3D11)||defined(SOKOL_METAL)||defined(SOKOL_WGPU)||defined(SOKOL_DUMMY_BACKEND))
#error "Please select a backend with SOKOL_GLCORE33, SOKOL_GLES2, SOKOL_GLES3, SOKOL_D3D11, SOKOL_METAL, SOKOL_WGPU or SOKOL_DUMMY_BACKEND"
#endif
#include <string.h> /* memset */
#include <float.h> /* FLT_MAX */
#ifndef SOKOL_API_IMPL
#define SOKOL_API_IMPL
#endif
#ifndef SOKOL_DEBUG
#ifndef NDEBUG
#define SOKOL_DEBUG (1)
#endif
#endif
#ifndef SOKOL_ASSERT
#include <assert.h>
#define SOKOL_ASSERT(c) assert(c)
#endif
#ifndef SOKOL_VALIDATE_BEGIN
#define SOKOL_VALIDATE_BEGIN() _sg_validate_begin()
#endif
#ifndef SOKOL_VALIDATE
#define SOKOL_VALIDATE(cond, err) _sg_validate((cond), err)
#endif
#ifndef SOKOL_VALIDATE_END
#define SOKOL_VALIDATE_END() _sg_validate_end()
#endif
#ifndef SOKOL_UNREACHABLE
#define SOKOL_UNREACHABLE SOKOL_ASSERT(false)
#endif
#ifndef SOKOL_MALLOC
#include <stdlib.h>
#define SOKOL_MALLOC(s) malloc(s)
#define SOKOL_FREE(p) free(p)
#endif
#ifndef SOKOL_LOG
#ifdef SOKOL_DEBUG
#include <stdio.h>
#define SOKOL_LOG(s) { SOKOL_ASSERT(s); puts(s); }
#else
#define SOKOL_LOG(s)
#endif
#endif
#ifndef _SOKOL_PRIVATE
#if defined(__GNUC__) || defined(__clang__)
#define _SOKOL_PRIVATE __attribute__((unused)) static
#else
#define _SOKOL_PRIVATE static
#endif
#endif
#ifndef _SOKOL_UNUSED
#define _SOKOL_UNUSED(x) (void)(x)
#endif
#if defined(SOKOL_TRACE_HOOKS)
#define _SG_TRACE_ARGS(fn, ...) if (_sg.hooks.fn) { _sg.hooks.fn(__VA_ARGS__, _sg.hooks.user_data); }
#define _SG_TRACE_NOARGS(fn) if (_sg.hooks.fn) { _sg.hooks.fn(_sg.hooks.user_data); }
#else
#define _SG_TRACE_ARGS(fn, ...)
#define _SG_TRACE_NOARGS(fn)
#endif
/* default clear values */
#ifndef SG_DEFAULT_CLEAR_RED
#define SG_DEFAULT_CLEAR_RED (0.5f)
#endif
#ifndef SG_DEFAULT_CLEAR_GREEN
#define SG_DEFAULT_CLEAR_GREEN (0.5f)
#endif
#ifndef SG_DEFAULT_CLEAR_BLUE
#define SG_DEFAULT_CLEAR_BLUE (0.5f)
#endif
#ifndef SG_DEFAULT_CLEAR_ALPHA
#define SG_DEFAULT_CLEAR_ALPHA (1.0f)
#endif
#ifndef SG_DEFAULT_CLEAR_DEPTH
#define SG_DEFAULT_CLEAR_DEPTH (1.0f)
#endif
#ifndef SG_DEFAULT_CLEAR_STENCIL
#define SG_DEFAULT_CLEAR_STENCIL (0)
#endif
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable:4115) /* named type definition in parentheses */
#pragma warning(disable:4505) /* unreferenced local function has been removed */
#pragma warning(disable:4201) /* nonstandard extension used: nameless struct/union (needed by d3d11.h) */
#pragma warning(disable:4054) /* 'type cast': from function pointer */
#pragma warning(disable:4055) /* 'type cast': from data pointer */
#endif
#if defined(SOKOL_D3D11)
#ifndef D3D11_NO_HELPERS
#define D3D11_NO_HELPERS
#endif
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <d3d11.h>
#include <d3dcompiler.h>
#ifdef _MSC_VER
#if (defined(WINAPI_FAMILY_PARTITION) && !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP))
#pragma comment (lib, "WindowsApp")
#else
#pragma comment (lib, "kernel32")
#pragma comment (lib, "user32")
#pragma comment (lib, "dxgi")
#pragma comment (lib, "d3d11")
#pragma comment (lib, "dxguid")
#endif
#endif
#elif defined(SOKOL_METAL)
// see https://clang.llvm.org/docs/LanguageExtensions.html#automatic-reference-counting
#if !defined(__cplusplus)
#if __has_feature(objc_arc) && !__has_feature(objc_arc_fields)
#error "sokol_gfx.h requires __has_feature(objc_arc_field) if ARC is enabled (use a more recent compiler version)"
#endif
#endif
#include <TargetConditionals.h>
#if defined(TARGET_OS_IPHONE) && !TARGET_OS_IPHONE
#define _SG_TARGET_MACOS (1)
#else
#define _SG_TARGET_IOS (1)
#if defined(TARGET_IPHONE_SIMULATOR) && TARGET_IPHONE_SIMULATOR
#define _SG_TARGET_IOS_SIMULATOR (1)
#endif
#endif
#import <Metal/Metal.h>
#elif defined(SOKOL_WGPU)
#if defined(__EMSCRIPTEN__)
#include <webgpu/webgpu.h>
#else
#include <dawn/webgpu.h>
#endif
#elif defined(SOKOL_GLCORE33) || defined(SOKOL_GLES2) || defined(SOKOL_GLES3)
#define _SOKOL_ANY_GL (1)
// include platform specific GL headers (or on Win32: use an embedded GL loader)
#if !defined(SOKOL_EXTERNAL_GL_LOADER)
#if defined(_WIN32)
#if defined(SOKOL_GLCORE33) && !defined(SOKOL_EXTERNAL_GL_LOADER)
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
#define _SOKOL_USE_WIN32_GL_LOADER (1)
#pragma comment (lib, "kernel32") // GetProcAddress()
#endif
#elif defined(__APPLE__)
#include <TargetConditionals.h>
#ifndef GL_SILENCE_DEPRECATION
#define GL_SILENCE_DEPRECATION
#endif
#if defined(TARGET_OS_IPHONE) && !TARGET_OS_IPHONE
#include <OpenGL/gl3.h>
#else
#include <OpenGLES/ES3/gl.h>
#include <OpenGLES/ES3/glext.h>
#endif
#elif defined(__EMSCRIPTEN__) || defined(__ANDROID__)
#if defined(SOKOL_GLES3)
#include <GLES3/gl3.h>
#elif defined(SOKOL_GLES2)
#ifndef GL_EXT_PROTOTYPES
#define GL_GLEXT_PROTOTYPES
#endif
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
#endif
#elif defined(__linux__) || defined(__unix__)
#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h>
#endif
#endif
// optional GL loader definitions (only on Win32)
#if defined(_SOKOL_USE_WIN32_GL_LOADER)
#define __gl_h_ 1
#define __gl32_h_ 1
#define __gl31_h_ 1
#define __GL_H__ 1
#define __glext_h_ 1
#define __GLEXT_H_ 1
#define __gltypes_h_ 1
#define __glcorearb_h_ 1
#define __gl_glcorearb_h_ 1
#define GL_APIENTRY APIENTRY
typedef unsigned int GLenum;
typedef unsigned int GLuint;
typedef int GLsizei;
typedef char GLchar;
typedef ptrdiff_t GLintptr;
typedef ptrdiff_t GLsizeiptr;
typedef double GLclampd;
typedef unsigned short GLushort;
typedef unsigned char GLubyte;
typedef unsigned char GLboolean;
typedef uint64_t GLuint64;
typedef double GLdouble;
typedef unsigned short GLhalf;
typedef float GLclampf;
typedef unsigned int GLbitfield;
typedef signed char GLbyte;
typedef short GLshort;
typedef void GLvoid;
typedef int64_t GLint64;
typedef float GLfloat;
typedef struct __GLsync * GLsync;
typedef int GLint;
#define GL_INT_2_10_10_10_REV 0x8D9F
#define GL_R32F 0x822E
#define GL_PROGRAM_POINT_SIZE 0x8642
#define GL_STENCIL_ATTACHMENT 0x8D20
#define GL_DEPTH_ATTACHMENT 0x8D00
#define GL_COLOR_ATTACHMENT2 0x8CE2
#define GL_COLOR_ATTACHMENT0 0x8CE0
#define GL_R16F 0x822D
#define GL_COLOR_ATTACHMENT22 0x8CF6
#define GL_DRAW_FRAMEBUFFER 0x8CA9
#define GL_FRAMEBUFFER_COMPLETE 0x8CD5
#define GL_NUM_EXTENSIONS 0x821D
#define GL_INFO_LOG_LENGTH 0x8B84
#define GL_VERTEX_SHADER 0x8B31
#define GL_INCR 0x1E02
#define GL_DYNAMIC_DRAW 0x88E8
#define GL_STATIC_DRAW 0x88E4
#define GL_TEXTURE_CUBE_MAP_POSITIVE_Z 0x8519
#define GL_TEXTURE_CUBE_MAP 0x8513
#define GL_FUNC_SUBTRACT 0x800A
#define GL_FUNC_REVERSE_SUBTRACT 0x800B
#define GL_CONSTANT_COLOR 0x8001
#define GL_DECR_WRAP 0x8508
#define GL_R8 0x8229
#define GL_LINEAR_MIPMAP_LINEAR 0x2703
#define GL_ELEMENT_ARRAY_BUFFER 0x8893
#define GL_SHORT 0x1402
#define GL_DEPTH_TEST 0x0B71
#define GL_TEXTURE_CUBE_MAP_NEGATIVE_Y 0x8518
#define GL_LINK_STATUS 0x8B82
#define GL_TEXTURE_CUBE_MAP_POSITIVE_Y 0x8517
#define GL_SAMPLE_ALPHA_TO_COVERAGE 0x809E
#define GL_RGBA16F 0x881A
#define GL_CONSTANT_ALPHA 0x8003
#define GL_READ_FRAMEBUFFER 0x8CA8
#define GL_TEXTURE0 0x84C0
#define GL_TEXTURE_MIN_LOD 0x813A
#define GL_CLAMP_TO_EDGE 0x812F
#define GL_UNSIGNED_SHORT_5_6_5 0x8363
#define GL_TEXTURE_WRAP_R 0x8072
#define GL_UNSIGNED_SHORT_5_5_5_1 0x8034
#define GL_NEAREST_MIPMAP_NEAREST 0x2700
#define GL_UNSIGNED_SHORT_4_4_4_4 0x8033
#define GL_SRC_ALPHA_SATURATE 0x0308
#define GL_STREAM_DRAW 0x88E0
#define GL_ONE 1
#define GL_NEAREST_MIPMAP_LINEAR 0x2702
#define GL_RGB10_A2 0x8059
#define GL_RGBA8 0x8058
#define GL_COLOR_ATTACHMENT1 0x8CE1
#define GL_RGBA4 0x8056
#define GL_RGB8 0x8051
#define GL_ARRAY_BUFFER 0x8892
#define GL_STENCIL 0x1802
#define GL_TEXTURE_2D 0x0DE1
#define GL_DEPTH 0x1801
#define GL_FRONT 0x0404
#define GL_STENCIL_BUFFER_BIT 0x00000400
#define GL_REPEAT 0x2901
#define GL_RGBA 0x1908
#define GL_TEXTURE_CUBE_MAP_POSITIVE_X 0x8515
#define GL_DECR 0x1E03
#define GL_FRAGMENT_SHADER 0x8B30
#define GL_FLOAT 0x1406
#define GL_TEXTURE_MAX_LOD 0x813B
#define GL_DEPTH_COMPONENT 0x1902
#define GL_ONE_MINUS_DST_ALPHA 0x0305
#define GL_COLOR 0x1800
#define GL_TEXTURE_2D_ARRAY 0x8C1A
#define GL_TRIANGLES 0x0004
#define GL_UNSIGNED_BYTE 0x1401
#define GL_TEXTURE_MAG_FILTER 0x2800
#define GL_ONE_MINUS_CONSTANT_ALPHA 0x8004
#define GL_NONE 0
#define GL_SRC_COLOR 0x0300
#define GL_BYTE 0x1400
#define GL_TEXTURE_CUBE_MAP_NEGATIVE_Z 0x851A
#define GL_LINE_STRIP 0x0003
#define GL_TEXTURE_3D 0x806F
#define GL_CW 0x0900
#define GL_LINEAR 0x2601
#define GL_RENDERBUFFER 0x8D41
#define GL_GEQUAL 0x0206
#define GL_COLOR_BUFFER_BIT 0x00004000
#define GL_RGBA32F 0x8814
#define GL_BLEND 0x0BE2
#define GL_ONE_MINUS_SRC_ALPHA 0x0303
#define GL_ONE_MINUS_CONSTANT_COLOR 0x8002
#define GL_TEXTURE_WRAP_T 0x2803
#define GL_TEXTURE_WRAP_S 0x2802
#define GL_TEXTURE_MIN_FILTER 0x2801
#define GL_LINEAR_MIPMAP_NEAREST 0x2701
#define GL_EXTENSIONS 0x1F03
#define GL_NO_ERROR 0
#define GL_REPLACE 0x1E01
#define GL_KEEP 0x1E00
#define GL_CCW 0x0901
#define GL_TEXTURE_CUBE_MAP_NEGATIVE_X 0x8516
#define GL_RGB 0x1907
#define GL_TRIANGLE_STRIP 0x0005
#define GL_FALSE 0
#define GL_ZERO 0
#define GL_CULL_FACE 0x0B44
#define GL_INVERT 0x150A
#define GL_INT 0x1404
#define GL_UNSIGNED_INT 0x1405
#define GL_UNSIGNED_SHORT 0x1403
#define GL_NEAREST 0x2600
#define GL_SCISSOR_TEST 0x0C11
#define GL_LEQUAL 0x0203
#define GL_STENCIL_TEST 0x0B90
#define GL_DITHER 0x0BD0
#define GL_DEPTH_COMPONENT16 0x81A5
#define GL_EQUAL 0x0202
#define GL_FRAMEBUFFER 0x8D40
#define GL_RGB5 0x8050
#define GL_LINES 0x0001
#define GL_DEPTH_BUFFER_BIT 0x00000100
#define GL_SRC_ALPHA 0x0302
#define GL_INCR_WRAP 0x8507
#define GL_LESS 0x0201
#define GL_MULTISAMPLE 0x809D
#define GL_FRAMEBUFFER_BINDING 0x8CA6
#define GL_BACK 0x0405
#define GL_ALWAYS 0x0207
#define GL_FUNC_ADD 0x8006
#define GL_ONE_MINUS_DST_COLOR 0x0307
#define GL_NOTEQUAL 0x0205
#define GL_DST_COLOR 0x0306
#define GL_COMPILE_STATUS 0x8B81
#define GL_RED 0x1903
#define GL_COLOR_ATTACHMENT3 0x8CE3
#define GL_DST_ALPHA 0x0304
#define GL_RGB5_A1 0x8057
#define GL_GREATER 0x0204
#define GL_POLYGON_OFFSET_FILL 0x8037
#define GL_TRUE 1
#define GL_NEVER 0x0200
#define GL_POINTS 0x0000
#define GL_ONE_MINUS_SRC_COLOR 0x0301
#define GL_MIRRORED_REPEAT 0x8370
#define GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS 0x8B4D
#define GL_R11F_G11F_B10F 0x8C3A
#define GL_UNSIGNED_INT_10F_11F_11F_REV 0x8C3B
#define GL_RGBA32UI 0x8D70
#define GL_RGB32UI 0x8D71
#define GL_RGBA16UI 0x8D76
#define GL_RGB16UI 0x8D77
#define GL_RGBA8UI 0x8D7C
#define GL_RGB8UI 0x8D7D
#define GL_RGBA32I 0x8D82
#define GL_RGB32I 0x8D83
#define GL_RGBA16I 0x8D88
#define GL_RGB16I 0x8D89
#define GL_RGBA8I 0x8D8E
#define GL_RGB8I 0x8D8F
#define GL_RED_INTEGER 0x8D94
#define GL_RG 0x8227
#define GL_RG_INTEGER 0x8228
#define GL_R8 0x8229
#define GL_R16 0x822A
#define GL_RG8 0x822B
#define GL_RG16 0x822C
#define GL_R16F 0x822D
#define GL_R32F 0x822E
#define GL_RG16F 0x822F
#define GL_RG32F 0x8230
#define GL_R8I 0x8231
#define GL_R8UI 0x8232
#define GL_R16I 0x8233
#define GL_R16UI 0x8234
#define GL_R32I 0x8235
#define GL_R32UI 0x8236
#define GL_RG8I 0x8237
#define GL_RG8UI 0x8238
#define GL_RG16I 0x8239
#define GL_RG16UI 0x823A
#define GL_RG32I 0x823B
#define GL_RG32UI 0x823C
#define GL_RGBA_INTEGER 0x8D99
#define GL_R8_SNORM 0x8F94
#define GL_RG8_SNORM 0x8F95
#define GL_RGB8_SNORM 0x8F96
#define GL_RGBA8_SNORM 0x8F97
#define GL_R16_SNORM 0x8F98
#define GL_RG16_SNORM 0x8F99
#define GL_RGB16_SNORM 0x8F9A
#define GL_RGBA16_SNORM 0x8F9B
#define GL_RGBA16 0x805B
#define GL_MAX_TEXTURE_SIZE 0x0D33
#define GL_MAX_CUBE_MAP_TEXTURE_SIZE 0x851C
#define GL_MAX_3D_TEXTURE_SIZE 0x8073
#define GL_MAX_ARRAY_TEXTURE_LAYERS 0x88FF
#define GL_MAX_VERTEX_ATTRIBS 0x8869
#define GL_CLAMP_TO_BORDER 0x812D
#define GL_TEXTURE_BORDER_COLOR 0x1004
#define GL_CURRENT_PROGRAM 0x8B8D
#endif
#ifndef GL_UNSIGNED_INT_2_10_10_10_REV
#define GL_UNSIGNED_INT_2_10_10_10_REV 0x8368
#endif
#ifndef GL_UNSIGNED_INT_24_8
#define GL_UNSIGNED_INT_24_8 0x84FA
#endif
#ifndef GL_TEXTURE_MAX_ANISOTROPY_EXT
#define GL_TEXTURE_MAX_ANISOTROPY_EXT 0x84FE
#endif
#ifndef GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT
#define GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT 0x84FF
#endif
#ifndef GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
#define GL_COMPRESSED_RGBA_S3TC_DXT1_EXT 0x83F1
#endif
#ifndef GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
#define GL_COMPRESSED_RGBA_S3TC_DXT3_EXT 0x83F2
#endif
#ifndef GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
#define GL_COMPRESSED_RGBA_S3TC_DXT5_EXT 0x83F3
#endif
#ifndef GL_COMPRESSED_RED_RGTC1
#define GL_COMPRESSED_RED_RGTC1 0x8DBB
#endif
#ifndef GL_COMPRESSED_SIGNED_RED_RGTC1
#define GL_COMPRESSED_SIGNED_RED_RGTC1 0x8DBC
#endif
#ifndef GL_COMPRESSED_RED_GREEN_RGTC2
#define GL_COMPRESSED_RED_GREEN_RGTC2 0x8DBD
#endif
#ifndef GL_COMPRESSED_SIGNED_RED_GREEN_RGTC2
#define GL_COMPRESSED_SIGNED_RED_GREEN_RGTC2 0x8DBE
#endif
#ifndef GL_COMPRESSED_RGBA_BPTC_UNORM_ARB
#define GL_COMPRESSED_RGBA_BPTC_UNORM_ARB 0x8E8C
#endif
#ifndef GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM_ARB
#define GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM_ARB 0x8E8D
#endif
#ifndef GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB
#define GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB 0x8E8E
#endif
#ifndef GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT_ARB
#define GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT_ARB 0x8E8F
#endif
#ifndef GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG
#define GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG 0x8C01
#endif
#ifndef GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG
#define GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG 0x8C00
#endif
#ifndef GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG
#define GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG 0x8C03
#endif
#ifndef GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG
#define GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG 0x8C02
#endif
#ifndef GL_COMPRESSED_RGB8_ETC2
#define GL_COMPRESSED_RGB8_ETC2 0x9274
#endif
#ifndef GL_COMPRESSED_RGBA8_ETC2_EAC
#define GL_COMPRESSED_RGBA8_ETC2_EAC 0x9278
#endif
#ifndef GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2
#define GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2 0x9276
#endif
#ifndef GL_COMPRESSED_RG11_EAC
#define GL_COMPRESSED_RG11_EAC 0x9272
#endif
#ifndef GL_COMPRESSED_SIGNED_RG11_EAC
#define GL_COMPRESSED_SIGNED_RG11_EAC 0x9273
#endif
#ifndef GL_DEPTH24_STENCIL8
#define GL_DEPTH24_STENCIL8 0x88F0
#endif
#ifndef GL_HALF_FLOAT
#define GL_HALF_FLOAT 0x140B
#endif
#ifndef GL_DEPTH_STENCIL
#define GL_DEPTH_STENCIL 0x84F9
#endif
#ifndef GL_LUMINANCE
#define GL_LUMINANCE 0x1909
#endif
#ifdef SOKOL_GLES2
#ifdef GL_ANGLE_instanced_arrays
#define _SOKOL_GL_INSTANCING_ENABLED
#define glDrawArraysInstanced(mode, first, count, instancecount) glDrawArraysInstancedANGLE(mode, first, count, instancecount)
#define glDrawElementsInstanced(mode, count, type, indices, instancecount) glDrawElementsInstancedANGLE(mode, count, type, indices, instancecount)
#define glVertexAttribDivisor(index, divisor) glVertexAttribDivisorANGLE(index, divisor)
#elif defined(GL_EXT_draw_instanced) && defined(GL_EXT_instanced_arrays)
#define _SOKOL_GL_INSTANCING_ENABLED
#define glDrawArraysInstanced(mode, first, count, instancecount) glDrawArraysInstancedEXT(mode, first, count, instancecount)
#define glDrawElementsInstanced(mode, count, type, indices, instancecount) glDrawElementsInstancedEXT(mode, count, type, indices, instancecount)
#define glVertexAttribDivisor(index, divisor) glVertexAttribDivisorEXT(index, divisor)
#else
#define _SOKOL_GLES2_INSTANCING_ERROR "Select GL_ANGLE_instanced_arrays or (GL_EXT_draw_instanced & GL_EXT_instanced_arrays) to enable instancing in GLES2"
#define glDrawArraysInstanced(mode, first, count, instancecount) SOKOL_ASSERT(0 && _SOKOL_GLES2_INSTANCING_ERROR)
#define glDrawElementsInstanced(mode, count, type, indices, instancecount) SOKOL_ASSERT(0 && _SOKOL_GLES2_INSTANCING_ERROR)
#define glVertexAttribDivisor(index, divisor) SOKOL_ASSERT(0 && _SOKOL_GLES2_INSTANCING_ERROR)
#endif
#else
#define _SOKOL_GL_INSTANCING_ENABLED
#endif
#define _SG_GL_CHECK_ERROR() { SOKOL_ASSERT(glGetError() == GL_NO_ERROR); }
#endif
/*=== COMMON BACKEND STUFF ===================================================*/
/* resource pool slots */
typedef struct {
uint32_t id;
uint32_t ctx_id;
sg_resource_state state;
} _sg_slot_t;
/* constants */
enum {
_SG_STRING_SIZE = 16,
_SG_SLOT_SHIFT = 16,
_SG_SLOT_MASK = (1<<_SG_SLOT_SHIFT)-1,
_SG_MAX_POOL_SIZE = (1<<_SG_SLOT_SHIFT),
_SG_DEFAULT_BUFFER_POOL_SIZE = 128,
_SG_DEFAULT_IMAGE_POOL_SIZE = 128,
_SG_DEFAULT_SHADER_POOL_SIZE = 32,
_SG_DEFAULT_PIPELINE_POOL_SIZE = 64,
_SG_DEFAULT_PASS_POOL_SIZE = 16,
_SG_DEFAULT_CONTEXT_POOL_SIZE = 16,
_SG_DEFAULT_SAMPLER_CACHE_CAPACITY = 64,
_SG_DEFAULT_UB_SIZE = 4 * 1024 * 1024,
_SG_DEFAULT_STAGING_SIZE = 8 * 1024 * 1024,
};
/* fixed-size string */
typedef struct {
char buf[_SG_STRING_SIZE];
} _sg_str_t;
/* helper macros */
#define _sg_def(val, def) (((val) == 0) ? (def) : (val))
#define _sg_def_flt(val, def) (((val) == 0.0f) ? (def) : (val))
#define _sg_min(a,b) ((a<b)?a:b)
#define _sg_max(a,b) ((a>b)?a:b)
#define _sg_clamp(v,v0,v1) ((v<v0)?(v0):((v>v1)?(v1):(v)))
#define _sg_fequal(val,cmp,delta) (((val-cmp)> -delta)&&((val-cmp)<delta))
typedef struct {
int size;
int append_pos;
bool append_overflow;
sg_buffer_type type;
sg_usage usage;
uint32_t update_frame_index;
uint32_t append_frame_index;
int num_slots;
int active_slot;
} _sg_buffer_common_t;
_SOKOL_PRIVATE void _sg_buffer_common_init(_sg_buffer_common_t* cmn, const sg_buffer_desc* desc) {
cmn->size = (int)desc->size;
cmn->append_pos = 0;
cmn->append_overflow = false;
cmn->type = desc->type;
cmn->usage = desc->usage;
cmn->update_frame_index = 0;
cmn->append_frame_index = 0;
cmn->num_slots = (cmn->usage == SG_USAGE_IMMUTABLE) ? 1 : SG_NUM_INFLIGHT_FRAMES;
cmn->active_slot = 0;
}
typedef struct {
sg_image_type type;
bool render_target;
int width;
int height;
int num_slices;
int num_mipmaps;
sg_usage usage;
sg_pixel_format pixel_format;
int sample_count;
sg_filter min_filter;
sg_filter mag_filter;
sg_wrap wrap_u;
sg_wrap wrap_v;
sg_wrap wrap_w;
sg_border_color border_color;
uint32_t max_anisotropy;
uint32_t upd_frame_index;
int num_slots;
int active_slot;
} _sg_image_common_t;
_SOKOL_PRIVATE void _sg_image_common_init(_sg_image_common_t* cmn, const sg_image_desc* desc) {
cmn->type = desc->type;
cmn->render_target = desc->render_target;
cmn->width = desc->width;
cmn->height = desc->height;
cmn->num_slices = desc->num_slices;
cmn->num_mipmaps = desc->num_mipmaps;
cmn->usage = desc->usage;
cmn->pixel_format = desc->pixel_format;
cmn->sample_count = desc->sample_count;
cmn->min_filter = desc->min_filter;
cmn->mag_filter = desc->mag_filter;
cmn->wrap_u = desc->wrap_u;
cmn->wrap_v = desc->wrap_v;
cmn->wrap_w = desc->wrap_w;
cmn->border_color = desc->border_color;
cmn->max_anisotropy = desc->max_anisotropy;
cmn->upd_frame_index = 0;
cmn->num_slots = (cmn->usage == SG_USAGE_IMMUTABLE) ? 1 : SG_NUM_INFLIGHT_FRAMES;
cmn->active_slot = 0;
}
typedef struct {
size_t size;
} _sg_uniform_block_t;
typedef struct {
sg_image_type image_type;
sg_sampler_type sampler_type;
} _sg_shader_image_t;
typedef struct {
int num_uniform_blocks;
int num_images;
_sg_uniform_block_t uniform_blocks[SG_MAX_SHADERSTAGE_UBS];
_sg_shader_image_t images[SG_MAX_SHADERSTAGE_IMAGES];
} _sg_shader_stage_t;
typedef struct {
_sg_shader_stage_t stage[SG_NUM_SHADER_STAGES];
} _sg_shader_common_t;
_SOKOL_PRIVATE void _sg_shader_common_init(_sg_shader_common_t* cmn, const sg_shader_desc* desc) {
for (int stage_index = 0; stage_index < SG_NUM_SHADER_STAGES; stage_index++) {
const sg_shader_stage_desc* stage_desc = (stage_index == SG_SHADERSTAGE_VS) ? &desc->vs : &desc->fs;
_sg_shader_stage_t* stage = &cmn->stage[stage_index];
SOKOL_ASSERT(stage->num_uniform_blocks == 0);
for (int ub_index = 0; ub_index < SG_MAX_SHADERSTAGE_UBS; ub_index++) {
const sg_shader_uniform_block_desc* ub_desc = &stage_desc->uniform_blocks[ub_index];
if (0 == ub_desc->size) {
break;
}
stage->uniform_blocks[ub_index].size = ub_desc->size;
stage->num_uniform_blocks++;
}
SOKOL_ASSERT(stage->num_images == 0);
for (int img_index = 0; img_index < SG_MAX_SHADERSTAGE_IMAGES; img_index++) {
const sg_shader_image_desc* img_desc = &stage_desc->images[img_index];
if (img_desc->image_type == _SG_IMAGETYPE_DEFAULT) {
break;
}
stage->images[img_index].image_type = img_desc->image_type;
stage->images[img_index].sampler_type = img_desc->sampler_type;
stage->num_images++;
}
}
}
typedef struct {
sg_shader shader_id;
sg_index_type index_type;
bool vertex_layout_valid[SG_MAX_SHADERSTAGE_BUFFERS];
int color_attachment_count;
sg_pixel_format color_formats[SG_MAX_COLOR_ATTACHMENTS];
sg_pixel_format depth_format;
int sample_count;
float depth_bias;
float depth_bias_slope_scale;
float depth_bias_clamp;
sg_color blend_color;
} _sg_pipeline_common_t;
_SOKOL_PRIVATE void _sg_pipeline_common_init(_sg_pipeline_common_t* cmn, const sg_pipeline_desc* desc) {
SOKOL_ASSERT(desc->color_count < SG_MAX_COLOR_ATTACHMENTS);
cmn->shader_id = desc->shader;
cmn->index_type = desc->index_type;
for (int i = 0; i < SG_MAX_SHADERSTAGE_BUFFERS; i++) {
cmn->vertex_layout_valid[i] = false;
}
cmn->color_attachment_count = desc->color_count;
for (int i = 0; i < cmn->color_attachment_count; i++) {
cmn->color_formats[i] = desc->colors[i].pixel_format;
}
cmn->depth_format = desc->depth.pixel_format;
cmn->sample_count = desc->sample_count;
cmn->depth_bias = desc->depth.bias;
cmn->depth_bias_slope_scale = desc->depth.bias_slope_scale;
cmn->depth_bias_clamp = desc->depth.bias_clamp;
cmn->blend_color = desc->blend_color;
}
typedef struct {
sg_image image_id;
int mip_level;
int slice;
} _sg_pass_attachment_common_t;
typedef struct {
int num_color_atts;
_sg_pass_attachment_common_t color_atts[SG_MAX_COLOR_ATTACHMENTS];
_sg_pass_attachment_common_t ds_att;
} _sg_pass_common_t;
_SOKOL_PRIVATE void _sg_pass_common_init(_sg_pass_common_t* cmn, const sg_pass_desc* desc) {
const sg_pass_attachment_desc* att_desc;
_sg_pass_attachment_common_t* att;
for (int i = 0; i < SG_MAX_COLOR_ATTACHMENTS; i++) {
att_desc = &desc->color_attachments[i];
if (att_desc->image.id != SG_INVALID_ID) {
cmn->num_color_atts++;
att = &cmn->color_atts[i];
att->image_id = att_desc->image;
att->mip_level = att_desc->mip_level;
att->slice = att_desc->slice;
}
}
att_desc = &desc->depth_stencil_attachment;
if (att_desc->image.id != SG_INVALID_ID) {
att = &cmn->ds_att;
att->image_id = att_desc->image;
att->mip_level = att_desc->mip_level;
att->slice = att_desc->slice;
}
}
/*=== GENERIC SAMPLER CACHE ==================================================*/
/*
this is used by the Metal and WGPU backends to reduce the
number of sampler state objects created through the backend API
*/
typedef struct {
sg_filter min_filter;
sg_filter mag_filter;
sg_wrap wrap_u;
sg_wrap wrap_v;
sg_wrap wrap_w;
sg_border_color border_color;
uint32_t max_anisotropy;
int min_lod; /* orig min/max_lod is float, this is int(min/max_lod*1000.0) */
int max_lod;
uintptr_t sampler_handle;
} _sg_sampler_cache_item_t;
typedef struct {
int capacity;
int num_items;
_sg_sampler_cache_item_t* items;
} _sg_sampler_cache_t;
_SOKOL_PRIVATE void _sg_smpcache_init(_sg_sampler_cache_t* cache, int capacity) {
SOKOL_ASSERT(cache && (capacity > 0));
memset(cache, 0, sizeof(_sg_sampler_cache_t));
cache->capacity = capacity;
const size_t size = (size_t)cache->capacity * sizeof(_sg_sampler_cache_item_t);
cache->items = (_sg_sampler_cache_item_t*) SOKOL_MALLOC(size);
SOKOL_ASSERT(cache->items);
memset(cache->items, 0, size);
}
_SOKOL_PRIVATE void _sg_smpcache_discard(_sg_sampler_cache_t* cache) {
SOKOL_ASSERT(cache && cache->items);
SOKOL_FREE(cache->items);
cache->items = 0;
cache->num_items = 0;
cache->capacity = 0;
}
_SOKOL_PRIVATE int _sg_smpcache_minlod_int(float min_lod) {
return (int) (min_lod * 1000.0f);
}
_SOKOL_PRIVATE int _sg_smpcache_maxlod_int(float max_lod) {
return (int) (_sg_clamp(max_lod, 0.0f, 1000.0f) * 1000.0f);
}
_SOKOL_PRIVATE int _sg_smpcache_find_item(const _sg_sampler_cache_t* cache, const sg_image_desc* img_desc) {
/* return matching sampler cache item index or -1 */
SOKOL_ASSERT(cache && cache->items);
SOKOL_ASSERT(img_desc);
const int min_lod = _sg_smpcache_minlod_int(img_desc->min_lod);
const int max_lod = _sg_smpcache_maxlod_int(img_desc->max_lod);
for (int i = 0; i < cache->num_items; i++) {
const _sg_sampler_cache_item_t* item = &cache->items[i];
if ((img_desc->min_filter == item->min_filter) &&
(img_desc->mag_filter == item->mag_filter) &&
(img_desc->wrap_u == item->wrap_u) &&
(img_desc->wrap_v == item->wrap_v) &&
(img_desc->wrap_w == item->wrap_w) &&
(img_desc->max_anisotropy == item->max_anisotropy) &&
(img_desc->border_color == item->border_color) &&
(min_lod == item->min_lod) &&
(max_lod == item->max_lod))
{
return i;
}
}
/* fallthrough: no matching cache item found */
return -1;
}
_SOKOL_PRIVATE void _sg_smpcache_add_item(_sg_sampler_cache_t* cache, const sg_image_desc* img_desc, uintptr_t sampler_handle) {
SOKOL_ASSERT(cache && cache->items);
SOKOL_ASSERT(img_desc);
SOKOL_ASSERT(cache->num_items < cache->capacity);
const int item_index = cache->num_items++;
_sg_sampler_cache_item_t* item = &cache->items[item_index];
item->min_filter = img_desc->min_filter;
item->mag_filter = img_desc->mag_filter;
item->wrap_u = img_desc->wrap_u;
item->wrap_v = img_desc->wrap_v;
item->wrap_w = img_desc->wrap_w;
item->border_color = img_desc->border_color;
item->max_anisotropy = img_desc->max_anisotropy;
item->min_lod = _sg_smpcache_minlod_int(img_desc->min_lod);
item->max_lod = _sg_smpcache_maxlod_int(img_desc->max_lod);
item->sampler_handle = sampler_handle;
}
_SOKOL_PRIVATE uintptr_t _sg_smpcache_sampler(_sg_sampler_cache_t* cache, int item_index) {
SOKOL_ASSERT(cache && cache->items);
SOKOL_ASSERT(item_index < cache->num_items);
return cache->items[item_index].sampler_handle;
}
/*=== DUMMY BACKEND DECLARATIONS =============================================*/
#if defined(SOKOL_DUMMY_BACKEND)
typedef struct {
_sg_slot_t slot;
_sg_buffer_common_t cmn;
} _sg_dummy_buffer_t;
typedef _sg_dummy_buffer_t _sg_buffer_t;
typedef struct {
_sg_slot_t slot;
_sg_image_common_t cmn;
} _sg_dummy_image_t;
typedef _sg_dummy_image_t _sg_image_t;
typedef struct {
_sg_slot_t slot;
_sg_shader_common_t cmn;
} _sg_dummy_shader_t;
typedef _sg_dummy_shader_t _sg_shader_t;
typedef struct {
_sg_slot_t slot;
_sg_shader_t* shader;
_sg_pipeline_common_t cmn;
} _sg_dummy_pipeline_t;
typedef _sg_dummy_pipeline_t _sg_pipeline_t;
typedef struct {
_sg_image_t* image;
} _sg_dummy_attachment_t;
typedef struct {
_sg_slot_t slot;
_sg_pass_common_t cmn;
struct {
_sg_dummy_attachment_t color_atts[SG_MAX_COLOR_ATTACHMENTS];
_sg_dummy_attachment_t ds_att;
} dmy;
} _sg_dummy_pass_t;
typedef _sg_dummy_pass_t _sg_pass_t;
typedef _sg_pass_attachment_common_t _sg_pass_attachment_t;
typedef struct {
_sg_slot_t slot;
} _sg_dummy_context_t;
typedef _sg_dummy_context_t _sg_context_t;
/*== GL BACKEND DECLARATIONS =================================================*/
#elif defined(_SOKOL_ANY_GL)
typedef struct {
_sg_slot_t slot;
_sg_buffer_common_t cmn;
struct {
GLuint buf[SG_NUM_INFLIGHT_FRAMES];
bool ext_buffers; /* if true, external buffers were injected with sg_buffer_desc.gl_buffers */
} gl;
} _sg_gl_buffer_t;
typedef _sg_gl_buffer_t _sg_buffer_t;
typedef struct {
_sg_slot_t slot;
_sg_image_common_t cmn;
struct {
GLenum target;
GLuint depth_render_buffer;
GLuint msaa_render_buffer;
GLuint tex[SG_NUM_INFLIGHT_FRAMES];
bool ext_textures; /* if true, external textures were injected with sg_image_desc.gl_textures */
} gl;
} _sg_gl_image_t;
typedef _sg_gl_image_t _sg_image_t;
typedef struct {
GLint gl_loc;
sg_uniform_type type;
uint8_t count;
uint16_t offset;
} _sg_gl_uniform_t;
typedef struct {
int num_uniforms;
_sg_gl_uniform_t uniforms[SG_MAX_UB_MEMBERS];
} _sg_gl_uniform_block_t;
typedef struct {
int gl_tex_slot;
} _sg_gl_shader_image_t;
typedef struct {
_sg_str_t name;
} _sg_gl_shader_attr_t;
typedef struct {
_sg_gl_uniform_block_t uniform_blocks[SG_MAX_SHADERSTAGE_UBS];
_sg_gl_shader_image_t images[SG_MAX_SHADERSTAGE_IMAGES];
} _sg_gl_shader_stage_t;
typedef struct {
_sg_slot_t slot;
_sg_shader_common_t cmn;
struct {
GLuint prog;
_sg_gl_shader_attr_t attrs[SG_MAX_VERTEX_ATTRIBUTES];
_sg_gl_shader_stage_t stage[SG_NUM_SHADER_STAGES];
} gl;
} _sg_gl_shader_t;
typedef _sg_gl_shader_t _sg_shader_t;
typedef struct {
int8_t vb_index; /* -1 if attr is not enabled */
int8_t divisor; /* -1 if not initialized */
uint8_t stride;
uint8_t size;
uint8_t normalized;
int offset;
GLenum type;
} _sg_gl_attr_t;
typedef struct {
_sg_slot_t slot;
_sg_pipeline_common_t cmn;
_sg_shader_t* shader;
struct {
_sg_gl_attr_t attrs[SG_MAX_VERTEX_ATTRIBUTES];
sg_depth_state depth;
sg_stencil_state stencil;
sg_primitive_type primitive_type;
sg_blend_state blend;
sg_color_mask color_write_mask[SG_MAX_COLOR_ATTACHMENTS];
sg_cull_mode cull_mode;
sg_face_winding face_winding;
int sample_count;
bool alpha_to_coverage_enabled;
} gl;
} _sg_gl_pipeline_t;
typedef _sg_gl_pipeline_t _sg_pipeline_t;
typedef struct {
_sg_image_t* image;
GLuint gl_msaa_resolve_buffer;
} _sg_gl_attachment_t;
typedef struct {
_sg_slot_t slot;
_sg_pass_common_t cmn;
struct {
GLuint fb;
_sg_gl_attachment_t color_atts[SG_MAX_COLOR_ATTACHMENTS];
_sg_gl_attachment_t ds_att;
} gl;
} _sg_gl_pass_t;
typedef _sg_gl_pass_t _sg_pass_t;
typedef _sg_pass_attachment_common_t _sg_pass_attachment_t;
typedef struct {
_sg_slot_t slot;
#if !defined(SOKOL_GLES2)
GLuint vao;
#endif
GLuint default_framebuffer;
} _sg_gl_context_t;
typedef _sg_gl_context_t _sg_context_t;
typedef struct {
_sg_gl_attr_t gl_attr;
GLuint gl_vbuf;
} _sg_gl_cache_attr_t;
typedef struct {
GLenum target;
GLuint texture;
} _sg_gl_texture_bind_slot;
typedef struct {
sg_depth_state depth;
sg_stencil_state stencil;
sg_blend_state blend;
sg_color_mask color_write_mask[SG_MAX_COLOR_ATTACHMENTS];
sg_cull_mode cull_mode;
sg_face_winding face_winding;
bool polygon_offset_enabled;
int sample_count;
sg_color blend_color;
bool alpha_to_coverage_enabled;
_sg_gl_cache_attr_t attrs[SG_MAX_VERTEX_ATTRIBUTES];
GLuint vertex_buffer;
GLuint index_buffer;
GLuint stored_vertex_buffer;
GLuint stored_index_buffer;
GLuint prog;
_sg_gl_texture_bind_slot textures[SG_MAX_SHADERSTAGE_IMAGES];
_sg_gl_texture_bind_slot stored_texture;
int cur_ib_offset;
GLenum cur_primitive_type;
GLenum cur_index_type;
GLenum cur_active_texture;
_sg_pipeline_t* cur_pipeline;
sg_pipeline cur_pipeline_id;
} _sg_gl_state_cache_t;
typedef struct {
bool valid;
bool gles2;
bool in_pass;
int cur_pass_width;
int cur_pass_height;
_sg_context_t* cur_context;
_sg_pass_t* cur_pass;
sg_pass cur_pass_id;
_sg_gl_state_cache_t cache;
bool ext_anisotropic;
GLint max_anisotropy;
GLint max_combined_texture_image_units;
#if _SOKOL_USE_WIN32_GL_LOADER
HINSTANCE opengl32_dll;
#endif
} _sg_gl_backend_t;
/*== D3D11 BACKEND DECLARATIONS ==============================================*/
#elif defined(SOKOL_D3D11)
typedef struct {
_sg_slot_t slot;
_sg_buffer_common_t cmn;
struct {
ID3D11Buffer* buf;
} d3d11;
} _sg_d3d11_buffer_t;
typedef _sg_d3d11_buffer_t _sg_buffer_t;
typedef struct {
_sg_slot_t slot;
_sg_image_common_t cmn;
struct {
DXGI_FORMAT format;
ID3D11Texture2D* tex2d;
ID3D11Texture3D* tex3d;
ID3D11Texture2D* texds;
ID3D11Texture2D* texmsaa;
ID3D11ShaderResourceView* srv;
ID3D11SamplerState* smp;
} d3d11;
} _sg_d3d11_image_t;
typedef _sg_d3d11_image_t _sg_image_t;
typedef struct {
_sg_str_t sem_name;
int sem_index;
} _sg_d3d11_shader_attr_t;
typedef struct {
ID3D11Buffer* cbufs[SG_MAX_SHADERSTAGE_UBS];
} _sg_d3d11_shader_stage_t;
typedef struct {
_sg_slot_t slot;
_sg_shader_common_t cmn;
struct {
_sg_d3d11_shader_attr_t attrs[SG_MAX_VERTEX_ATTRIBUTES];
_sg_d3d11_shader_stage_t stage[SG_NUM_SHADER_STAGES];
ID3D11VertexShader* vs;
ID3D11PixelShader* fs;
void* vs_blob;
size_t vs_blob_length;
} d3d11;
} _sg_d3d11_shader_t;
typedef _sg_d3d11_shader_t _sg_shader_t;
typedef struct {
_sg_slot_t slot;
_sg_pipeline_common_t cmn;
_sg_shader_t* shader;
struct {
UINT stencil_ref;
UINT vb_strides[SG_MAX_SHADERSTAGE_BUFFERS];
D3D_PRIMITIVE_TOPOLOGY topology;
DXGI_FORMAT index_format;
ID3D11InputLayout* il;
ID3D11RasterizerState* rs;
ID3D11DepthStencilState* dss;
ID3D11BlendState* bs;
} d3d11;
} _sg_d3d11_pipeline_t;
typedef _sg_d3d11_pipeline_t _sg_pipeline_t;
typedef struct {
_sg_image_t* image;
ID3D11RenderTargetView* rtv;
} _sg_d3d11_color_attachment_t;
typedef struct {
_sg_image_t* image;
ID3D11DepthStencilView* dsv;
} _sg_d3d11_ds_attachment_t;
typedef struct {
_sg_slot_t slot;
_sg_pass_common_t cmn;
struct {
_sg_d3d11_color_attachment_t color_atts[SG_MAX_COLOR_ATTACHMENTS];
_sg_d3d11_ds_attachment_t ds_att;
} d3d11;
} _sg_d3d11_pass_t;
typedef _sg_d3d11_pass_t _sg_pass_t;
typedef _sg_pass_attachment_common_t _sg_pass_attachment_t;
typedef struct {
_sg_slot_t slot;
} _sg_d3d11_context_t;
typedef _sg_d3d11_context_t _sg_context_t;
typedef struct {
bool valid;
ID3D11Device* dev;
ID3D11DeviceContext* ctx;
const void* (*rtv_cb)(void);
const void* (*rtv_userdata_cb)(void*);
const void* (*dsv_cb)(void);
const void* (*dsv_userdata_cb)(void*);
void* user_data;
bool in_pass;
bool use_indexed_draw;
int cur_width;
int cur_height;
int num_rtvs;
_sg_pass_t* cur_pass;
sg_pass cur_pass_id;
_sg_pipeline_t* cur_pipeline;
sg_pipeline cur_pipeline_id;
ID3D11RenderTargetView* cur_rtvs[SG_MAX_COLOR_ATTACHMENTS];
ID3D11DepthStencilView* cur_dsv;
/* on-demand loaded d3dcompiler_47.dll handles */
HINSTANCE d3dcompiler_dll;
bool d3dcompiler_dll_load_failed;
pD3DCompile D3DCompile_func;
/* the following arrays are used for unbinding resources, they will always contain zeroes */
ID3D11RenderTargetView* zero_rtvs[SG_MAX_COLOR_ATTACHMENTS];
ID3D11Buffer* zero_vbs[SG_MAX_SHADERSTAGE_BUFFERS];
UINT zero_vb_offsets[SG_MAX_SHADERSTAGE_BUFFERS];
UINT zero_vb_strides[SG_MAX_SHADERSTAGE_BUFFERS];
ID3D11Buffer* zero_cbs[SG_MAX_SHADERSTAGE_UBS];
ID3D11ShaderResourceView* zero_srvs[SG_MAX_SHADERSTAGE_IMAGES];
ID3D11SamplerState* zero_smps[SG_MAX_SHADERSTAGE_IMAGES];
/* global subresourcedata array for texture updates */
D3D11_SUBRESOURCE_DATA subres_data[SG_MAX_MIPMAPS * SG_MAX_TEXTUREARRAY_LAYERS];
} _sg_d3d11_backend_t;
/*=== METAL BACKEND DECLARATIONS =============================================*/
#elif defined(SOKOL_METAL)
#if defined(_SG_TARGET_MACOS) || defined(_SG_TARGET_IOS_SIMULATOR)
#define _SG_MTL_UB_ALIGN (256)
#else
#define _SG_MTL_UB_ALIGN (16)
#endif
#define _SG_MTL_INVALID_SLOT_INDEX (0)
typedef struct {
uint32_t frame_index; /* frame index at which it is safe to release this resource */
int slot_index;
} _sg_mtl_release_item_t;
typedef struct {
NSMutableArray* pool;
int num_slots;
int free_queue_top;
int* free_queue;
int release_queue_front;
int release_queue_back;
_sg_mtl_release_item_t* release_queue;
} _sg_mtl_idpool_t;
typedef struct {
_sg_slot_t slot;
_sg_buffer_common_t cmn;
struct {
int buf[SG_NUM_INFLIGHT_FRAMES]; /* index into _sg_mtl_pool */
} mtl;
} _sg_mtl_buffer_t;
typedef _sg_mtl_buffer_t _sg_buffer_t;
typedef struct {
_sg_slot_t slot;
_sg_image_common_t cmn;
struct {
int tex[SG_NUM_INFLIGHT_FRAMES];
int depth_tex;
int msaa_tex;
int sampler_state;
} mtl;
} _sg_mtl_image_t;
typedef _sg_mtl_image_t _sg_image_t;
typedef struct {
int mtl_lib;
int mtl_func;
} _sg_mtl_shader_stage_t;
typedef struct {
_sg_slot_t slot;
_sg_shader_common_t cmn;
struct {
_sg_mtl_shader_stage_t stage[SG_NUM_SHADER_STAGES];
} mtl;
} _sg_mtl_shader_t;
typedef _sg_mtl_shader_t _sg_shader_t;
typedef struct {
_sg_slot_t slot;
_sg_pipeline_common_t cmn;
_sg_shader_t* shader;
struct {
MTLPrimitiveType prim_type;
int index_size;
MTLIndexType index_type;
MTLCullMode cull_mode;
MTLWinding winding;
uint32_t stencil_ref;
int rps;
int dss;
} mtl;
} _sg_mtl_pipeline_t;
typedef _sg_mtl_pipeline_t _sg_pipeline_t;
typedef struct {
_sg_image_t* image;
} _sg_mtl_attachment_t;
typedef struct {
_sg_slot_t slot;
_sg_pass_common_t cmn;
struct {
_sg_mtl_attachment_t color_atts[SG_MAX_COLOR_ATTACHMENTS];
_sg_mtl_attachment_t ds_att;
} mtl;
} _sg_mtl_pass_t;
typedef _sg_mtl_pass_t _sg_pass_t;
typedef _sg_pass_attachment_common_t _sg_pass_attachment_t;
typedef struct {
_sg_slot_t slot;
} _sg_mtl_context_t;
typedef _sg_mtl_context_t _sg_context_t;
/* resouce binding state cache */
typedef struct {
const _sg_pipeline_t* cur_pipeline;
sg_pipeline cur_pipeline_id;
const _sg_buffer_t* cur_indexbuffer;
int cur_indexbuffer_offset;
sg_buffer cur_indexbuffer_id;
const _sg_buffer_t* cur_vertexbuffers[SG_MAX_SHADERSTAGE_BUFFERS];
int cur_vertexbuffer_offsets[SG_MAX_SHADERSTAGE_BUFFERS];
sg_buffer cur_vertexbuffer_ids[SG_MAX_SHADERSTAGE_BUFFERS];
const _sg_image_t* cur_vs_images[SG_MAX_SHADERSTAGE_IMAGES];
sg_image cur_vs_image_ids[SG_MAX_SHADERSTAGE_IMAGES];
const _sg_image_t* cur_fs_images[SG_MAX_SHADERSTAGE_IMAGES];
sg_image cur_fs_image_ids[SG_MAX_SHADERSTAGE_IMAGES];
} _sg_mtl_state_cache_t;
typedef struct {
bool valid;
const void*(*renderpass_descriptor_cb)(void);
const void*(*renderpass_descriptor_userdata_cb)(void*);
const void*(*drawable_cb)(void);
const void*(*drawable_userdata_cb)(void*);
void* user_data;
uint32_t frame_index;
uint32_t cur_frame_rotate_index;
int ub_size;
int cur_ub_offset;
uint8_t* cur_ub_base_ptr;
bool in_pass;
bool pass_valid;
int cur_width;
int cur_height;
_sg_mtl_state_cache_t state_cache;
_sg_sampler_cache_t sampler_cache;
_sg_mtl_idpool_t idpool;
dispatch_semaphore_t sem;
id<MTLDevice> device;
id<MTLCommandQueue> cmd_queue;
id<MTLCommandBuffer> cmd_buffer;
id<MTLRenderCommandEncoder> cmd_encoder;
id<MTLBuffer> uniform_buffers[SG_NUM_INFLIGHT_FRAMES];
} _sg_mtl_backend_t;
/*=== WGPU BACKEND DECLARATIONS ==============================================*/
#elif defined(SOKOL_WGPU)
#define _SG_WGPU_STAGING_ALIGN (256)
#define _SG_WGPU_STAGING_PIPELINE_SIZE (8)
#define _SG_WGPU_ROWPITCH_ALIGN (256)
#define _SG_WGPU_MAX_SHADERSTAGE_IMAGES (8)
#define _SG_WGPU_MAX_UNIFORM_UPDATE_SIZE (1<<16)
typedef struct {
_sg_slot_t slot;
_sg_buffer_common_t cmn;
struct {
WGPUBuffer buf;
} wgpu;
} _sg_wgpu_buffer_t;
typedef _sg_wgpu_buffer_t _sg_buffer_t;
typedef struct {
_sg_slot_t slot;
_sg_image_common_t cmn;
struct {
WGPUTexture tex;
WGPUTextureView tex_view;
WGPUTexture msaa_tex;
WGPUSampler sampler;
} wgpu;
} _sg_wgpu_image_t;
typedef _sg_wgpu_image_t _sg_image_t;
typedef struct {
WGPUShaderModule module;
WGPUBindGroupLayout bind_group_layout;
_sg_str_t entry;
} _sg_wgpu_shader_stage_t;
typedef struct {
_sg_slot_t slot;
_sg_shader_common_t cmn;
struct {
_sg_wgpu_shader_stage_t stage[SG_NUM_SHADER_STAGES];
} wgpu;
} _sg_wgpu_shader_t;
typedef _sg_wgpu_shader_t _sg_shader_t;
typedef struct {
_sg_slot_t slot;
_sg_pipeline_common_t cmn;
_sg_shader_t* shader;
struct {
WGPURenderPipeline pip;
uint32_t stencil_ref;
} wgpu;
} _sg_wgpu_pipeline_t;
typedef _sg_wgpu_pipeline_t _sg_pipeline_t;
typedef struct {
_sg_image_t* image;
WGPUTextureView render_tex_view;
WGPUTextureView resolve_tex_view;
} _sg_wgpu_attachment_t;
typedef struct {
_sg_slot_t slot;
_sg_pass_common_t cmn;
struct {
_sg_wgpu_attachment_t color_atts[SG_MAX_COLOR_ATTACHMENTS];
_sg_wgpu_attachment_t ds_att;
} wgpu;
} _sg_wgpu_pass_t;
typedef _sg_wgpu_pass_t _sg_pass_t;
typedef _sg_pass_attachment_common_t _sg_pass_attachment_t;
typedef struct {
_sg_slot_t slot;
} _sg_wgpu_context_t;
typedef _sg_wgpu_context_t _sg_context_t;
/* a pool of per-frame uniform buffers */
typedef struct {
WGPUBindGroupLayout bindgroup_layout;
uint32_t num_bytes;
uint32_t offset; /* current offset into current frame's mapped uniform buffer */
uint32_t bind_offsets[SG_NUM_SHADER_STAGES][SG_MAX_SHADERSTAGE_UBS];
WGPUBuffer buf; /* the GPU-side uniform buffer */
WGPUBindGroup bindgroup;
struct {
int num;
int cur;
WGPUBuffer buf[_SG_WGPU_STAGING_PIPELINE_SIZE]; /* CPU-side staging buffers */
uint8_t* ptr[_SG_WGPU_STAGING_PIPELINE_SIZE]; /* if != 0, staging buffer currently mapped */
} stage;
} _sg_wgpu_ubpool_t;
/* ...a similar pool (like uniform buffer pool) of dynamic-resource staging buffers */
typedef struct {
uint32_t num_bytes;
uint32_t offset; /* current offset into current frame's staging buffer */
int num; /* number of staging buffers */
int cur; /* this frame's staging buffer */
WGPUBuffer buf[_SG_WGPU_STAGING_PIPELINE_SIZE]; /* CPU-side staging buffers */
uint8_t* ptr[_SG_WGPU_STAGING_PIPELINE_SIZE]; /* if != 0, staging buffer currently mapped */
} _sg_wgpu_stagingpool_t;
/* the WGPU backend state */
typedef struct {
bool valid;
bool in_pass;
bool draw_indexed;
int cur_width;
int cur_height;
WGPUDevice dev;
WGPUTextureView (*render_view_cb)(void);
WGPUTextureView (*render_view_userdata_cb)(void*);
WGPUTextureView (*resolve_view_cb)(void);
WGPUTextureView (*resolve_view_userdata_cb)(void*);
WGPUTextureView (*depth_stencil_view_cb)(void);
WGPUTextureView (*depth_stencil_view_userdata_cb)(void*);
void* user_data;
WGPUQueue queue;
WGPUCommandEncoder render_cmd_enc;
WGPUCommandEncoder staging_cmd_enc;
WGPURenderPassEncoder pass_enc;
WGPUBindGroup empty_bind_group;
const _sg_pipeline_t* cur_pipeline;
sg_pipeline cur_pipeline_id;
_sg_sampler_cache_t sampler_cache;
_sg_wgpu_ubpool_t ub;
_sg_wgpu_stagingpool_t staging;
} _sg_wgpu_backend_t;
#endif
/*=== RESOURCE POOL DECLARATIONS =============================================*/
/* this *MUST* remain 0 */
#define _SG_INVALID_SLOT_INDEX (0)
typedef struct {
int size;
int queue_top;
uint32_t* gen_ctrs;
int* free_queue;
} _sg_pool_t;
typedef struct {
_sg_pool_t buffer_pool;
_sg_pool_t image_pool;
_sg_pool_t shader_pool;
_sg_pool_t pipeline_pool;
_sg_pool_t pass_pool;
_sg_pool_t context_pool;
_sg_buffer_t* buffers;
_sg_image_t* images;
_sg_shader_t* shaders;
_sg_pipeline_t* pipelines;
_sg_pass_t* passes;
_sg_context_t* contexts;
} _sg_pools_t;
/*=== VALIDATION LAYER DECLARATIONS ==========================================*/
typedef enum {
/* special case 'validation was successful' */
_SG_VALIDATE_SUCCESS,
/* buffer creation */
_SG_VALIDATE_BUFFERDESC_CANARY,
_SG_VALIDATE_BUFFERDESC_SIZE,
_SG_VALIDATE_BUFFERDESC_DATA,
_SG_VALIDATE_BUFFERDESC_DATA_SIZE,
_SG_VALIDATE_BUFFERDESC_NO_DATA,
/* image creation */
_SG_VALIDATE_IMAGEDESC_CANARY,
_SG_VALIDATE_IMAGEDESC_WIDTH,
_SG_VALIDATE_IMAGEDESC_HEIGHT,
_SG_VALIDATE_IMAGEDESC_RT_PIXELFORMAT,
_SG_VALIDATE_IMAGEDESC_NONRT_PIXELFORMAT,
_SG_VALIDATE_IMAGEDESC_MSAA_BUT_NO_RT,
_SG_VALIDATE_IMAGEDESC_NO_MSAA_RT_SUPPORT,
_SG_VALIDATE_IMAGEDESC_RT_IMMUTABLE,
_SG_VALIDATE_IMAGEDESC_RT_NO_DATA,
_SG_VALIDATE_IMAGEDESC_DATA,
_SG_VALIDATE_IMAGEDESC_NO_DATA,
/* shader creation */
_SG_VALIDATE_SHADERDESC_CANARY,
_SG_VALIDATE_SHADERDESC_SOURCE,
_SG_VALIDATE_SHADERDESC_BYTECODE,
_SG_VALIDATE_SHADERDESC_SOURCE_OR_BYTECODE,
_SG_VALIDATE_SHADERDESC_NO_BYTECODE_SIZE,
_SG_VALIDATE_SHADERDESC_NO_CONT_UBS,
_SG_VALIDATE_SHADERDESC_NO_CONT_IMGS,
_SG_VALIDATE_SHADERDESC_NO_CONT_UB_MEMBERS,
_SG_VALIDATE_SHADERDESC_NO_UB_MEMBERS,
_SG_VALIDATE_SHADERDESC_UB_MEMBER_NAME,
_SG_VALIDATE_SHADERDESC_UB_SIZE_MISMATCH,
_SG_VALIDATE_SHADERDESC_IMG_NAME,
_SG_VALIDATE_SHADERDESC_ATTR_NAMES,
_SG_VALIDATE_SHADERDESC_ATTR_SEMANTICS,
_SG_VALIDATE_SHADERDESC_ATTR_STRING_TOO_LONG,
/* pipeline creation */
_SG_VALIDATE_PIPELINEDESC_CANARY,
_SG_VALIDATE_PIPELINEDESC_SHADER,
_SG_VALIDATE_PIPELINEDESC_NO_ATTRS,
_SG_VALIDATE_PIPELINEDESC_LAYOUT_STRIDE4,
_SG_VALIDATE_PIPELINEDESC_ATTR_NAME,
_SG_VALIDATE_PIPELINEDESC_ATTR_SEMANTICS,
/* pass creation */
_SG_VALIDATE_PASSDESC_CANARY,
_SG_VALIDATE_PASSDESC_NO_COLOR_ATTS,
_SG_VALIDATE_PASSDESC_NO_CONT_COLOR_ATTS,
_SG_VALIDATE_PASSDESC_IMAGE,
_SG_VALIDATE_PASSDESC_MIPLEVEL,
_SG_VALIDATE_PASSDESC_FACE,
_SG_VALIDATE_PASSDESC_LAYER,
_SG_VALIDATE_PASSDESC_SLICE,
_SG_VALIDATE_PASSDESC_IMAGE_NO_RT,
_SG_VALIDATE_PASSDESC_COLOR_INV_PIXELFORMAT,
_SG_VALIDATE_PASSDESC_DEPTH_INV_PIXELFORMAT,
_SG_VALIDATE_PASSDESC_IMAGE_SIZES,
_SG_VALIDATE_PASSDESC_IMAGE_SAMPLE_COUNTS,
/* sg_begin_pass validation */
_SG_VALIDATE_BEGINPASS_PASS,
_SG_VALIDATE_BEGINPASS_IMAGE,
/* sg_apply_pipeline validation */
_SG_VALIDATE_APIP_PIPELINE_VALID_ID,
_SG_VALIDATE_APIP_PIPELINE_EXISTS,
_SG_VALIDATE_APIP_PIPELINE_VALID,
_SG_VALIDATE_APIP_SHADER_EXISTS,
_SG_VALIDATE_APIP_SHADER_VALID,
_SG_VALIDATE_APIP_ATT_COUNT,
_SG_VALIDATE_APIP_COLOR_FORMAT,
_SG_VALIDATE_APIP_DEPTH_FORMAT,
_SG_VALIDATE_APIP_SAMPLE_COUNT,
/* sg_apply_bindings validation */
_SG_VALIDATE_ABND_PIPELINE,
_SG_VALIDATE_ABND_PIPELINE_EXISTS,
_SG_VALIDATE_ABND_PIPELINE_VALID,
_SG_VALIDATE_ABND_VBS,
_SG_VALIDATE_ABND_VB_EXISTS,
_SG_VALIDATE_ABND_VB_TYPE,
_SG_VALIDATE_ABND_VB_OVERFLOW,
_SG_VALIDATE_ABND_NO_IB,
_SG_VALIDATE_ABND_IB,
_SG_VALIDATE_ABND_IB_EXISTS,
_SG_VALIDATE_ABND_IB_TYPE,
_SG_VALIDATE_ABND_IB_OVERFLOW,
_SG_VALIDATE_ABND_VS_IMGS,
_SG_VALIDATE_ABND_VS_IMG_EXISTS,
_SG_VALIDATE_ABND_VS_IMG_TYPES,
_SG_VALIDATE_ABND_FS_IMGS,
_SG_VALIDATE_ABND_FS_IMG_EXISTS,
_SG_VALIDATE_ABND_FS_IMG_TYPES,
/* sg_apply_uniforms validation */
_SG_VALIDATE_AUB_NO_PIPELINE,
_SG_VALIDATE_AUB_NO_UB_AT_SLOT,
_SG_VALIDATE_AUB_SIZE,
/* sg_update_buffer validation */
_SG_VALIDATE_UPDATEBUF_USAGE,
_SG_VALIDATE_UPDATEBUF_SIZE,
_SG_VALIDATE_UPDATEBUF_ONCE,
_SG_VALIDATE_UPDATEBUF_APPEND,
/* sg_append_buffer validation */
_SG_VALIDATE_APPENDBUF_USAGE,
_SG_VALIDATE_APPENDBUF_SIZE,
_SG_VALIDATE_APPENDBUF_UPDATE,
/* sg_update_image validation */
_SG_VALIDATE_UPDIMG_USAGE,
_SG_VALIDATE_UPDIMG_NOTENOUGHDATA,
_SG_VALIDATE_UPDIMG_SIZE,
_SG_VALIDATE_UPDIMG_COMPRESSED,
_SG_VALIDATE_UPDIMG_ONCE
} _sg_validate_error_t;
/*=== GENERIC BACKEND STATE ==================================================*/
typedef struct {
bool valid;
sg_desc desc; /* original desc with default values patched in */
uint32_t frame_index;
sg_context active_context;
sg_pass cur_pass;
sg_pipeline cur_pipeline;
bool pass_valid;
bool bindings_valid;
bool next_draw_valid;
#if defined(SOKOL_DEBUG)
_sg_validate_error_t validate_error;
#endif
_sg_pools_t pools;
sg_backend backend;
sg_features features;
sg_limits limits;
sg_pixelformat_info formats[_SG_PIXELFORMAT_NUM];
#if defined(_SOKOL_ANY_GL)
_sg_gl_backend_t gl;
#elif defined(SOKOL_METAL)
_sg_mtl_backend_t mtl;
#elif defined(SOKOL_D3D11)
_sg_d3d11_backend_t d3d11;
#elif defined(SOKOL_WGPU)
_sg_wgpu_backend_t wgpu;
#endif
#if defined(SOKOL_TRACE_HOOKS)
sg_trace_hooks hooks;
#endif
} _sg_state_t;
static _sg_state_t _sg;
/*-- helper functions --------------------------------------------------------*/
_SOKOL_PRIVATE bool _sg_strempty(const _sg_str_t* str) {
return 0 == str->buf[0];
}
_SOKOL_PRIVATE const char* _sg_strptr(const _sg_str_t* str) {
return &str->buf[0];
}
_SOKOL_PRIVATE void _sg_strcpy(_sg_str_t* dst, const char* src) {
SOKOL_ASSERT(dst);
if (src) {
#if defined(_MSC_VER)
strncpy_s(dst->buf, _SG_STRING_SIZE, src, (_SG_STRING_SIZE-1));
#else
strncpy(dst->buf, src, _SG_STRING_SIZE);
#endif
dst->buf[_SG_STRING_SIZE-1] = 0;
}
else {
memset(dst->buf, 0, _SG_STRING_SIZE);
}
}
/* return byte size of a vertex format */
_SOKOL_PRIVATE int _sg_vertexformat_bytesize(sg_vertex_format fmt) {
switch (fmt) {
case SG_VERTEXFORMAT_FLOAT: return 4;
case SG_VERTEXFORMAT_FLOAT2: return 8;
case SG_VERTEXFORMAT_FLOAT3: return 12;
case SG_VERTEXFORMAT_FLOAT4: return 16;
case SG_VERTEXFORMAT_BYTE4: return 4;
case SG_VERTEXFORMAT_BYTE4N: return 4;
case SG_VERTEXFORMAT_UBYTE4: return 4;
case SG_VERTEXFORMAT_UBYTE4N: return 4;
case SG_VERTEXFORMAT_SHORT2: return 4;
case SG_VERTEXFORMAT_SHORT2N: return 4;
case SG_VERTEXFORMAT_USHORT2N: return 4;
case SG_VERTEXFORMAT_SHORT4: return 8;
case SG_VERTEXFORMAT_SHORT4N: return 8;
case SG_VERTEXFORMAT_USHORT4N: return 8;
case SG_VERTEXFORMAT_UINT10_N2: return 4;
case SG_VERTEXFORMAT_INVALID: return 0;
default:
SOKOL_UNREACHABLE;
return -1;
}
}
/* return the byte size of a shader uniform */
_SOKOL_PRIVATE int _sg_uniform_size(sg_uniform_type type, int count) {
switch (type) {
case SG_UNIFORMTYPE_INVALID: return 0;
case SG_UNIFORMTYPE_FLOAT: return 4 * count;
case SG_UNIFORMTYPE_FLOAT2: return 8 * count;
case SG_UNIFORMTYPE_FLOAT3: return 12 * count; /* FIXME: std140??? */
case SG_UNIFORMTYPE_FLOAT4: return 16 * count;
case SG_UNIFORMTYPE_MAT4: return 64 * count;
default:
SOKOL_UNREACHABLE;
return -1;
}
}
/* return true if pixel format is a compressed format */
_SOKOL_PRIVATE bool _sg_is_compressed_pixel_format(sg_pixel_format fmt) {
switch (fmt) {
case SG_PIXELFORMAT_BC1_RGBA:
case SG_PIXELFORMAT_BC2_RGBA:
case SG_PIXELFORMAT_BC3_RGBA:
case SG_PIXELFORMAT_BC4_R:
case SG_PIXELFORMAT_BC4_RSN:
case SG_PIXELFORMAT_BC5_RG:
case SG_PIXELFORMAT_BC5_RGSN:
case SG_PIXELFORMAT_BC6H_RGBF:
case SG_PIXELFORMAT_BC6H_RGBUF:
case SG_PIXELFORMAT_BC7_RGBA:
case SG_PIXELFORMAT_PVRTC_RGB_2BPP:
case SG_PIXELFORMAT_PVRTC_RGB_4BPP:
case SG_PIXELFORMAT_PVRTC_RGBA_2BPP:
case SG_PIXELFORMAT_PVRTC_RGBA_4BPP:
case SG_PIXELFORMAT_ETC2_RGB8:
case SG_PIXELFORMAT_ETC2_RGB8A1:
case SG_PIXELFORMAT_ETC2_RGBA8:
case SG_PIXELFORMAT_ETC2_RG11:
case SG_PIXELFORMAT_ETC2_RG11SN:
return true;
default:
return false;
}
}
/* return true if pixel format is a valid render target format */
_SOKOL_PRIVATE bool _sg_is_valid_rendertarget_color_format(sg_pixel_format fmt) {
const int fmt_index = (int) fmt;
SOKOL_ASSERT((fmt_index >= 0) && (fmt_index < _SG_PIXELFORMAT_NUM));
return _sg.formats[fmt_index].render && !_sg.formats[fmt_index].depth;
}
/* return true if pixel format is a valid depth format */
_SOKOL_PRIVATE bool _sg_is_valid_rendertarget_depth_format(sg_pixel_format fmt) {
const int fmt_index = (int) fmt;
SOKOL_ASSERT((fmt_index >= 0) && (fmt_index < _SG_PIXELFORMAT_NUM));
return _sg.formats[fmt_index].render && _sg.formats[fmt_index].depth;
}
/* return true if pixel format is a depth-stencil format */
_SOKOL_PRIVATE bool _sg_is_depth_stencil_format(sg_pixel_format fmt) {
return (SG_PIXELFORMAT_DEPTH_STENCIL == fmt);
}
/* return the bytes-per-pixel for a pixel format */
_SOKOL_PRIVATE int _sg_pixelformat_bytesize(sg_pixel_format fmt) {
switch (fmt) {
case SG_PIXELFORMAT_R8:
case SG_PIXELFORMAT_R8SN:
case SG_PIXELFORMAT_R8UI:
case SG_PIXELFORMAT_R8SI:
return 1;
case SG_PIXELFORMAT_R16:
case SG_PIXELFORMAT_R16SN:
case SG_PIXELFORMAT_R16UI:
case SG_PIXELFORMAT_R16SI:
case SG_PIXELFORMAT_R16F:
case SG_PIXELFORMAT_RG8:
case SG_PIXELFORMAT_RG8SN:
case SG_PIXELFORMAT_RG8UI:
case SG_PIXELFORMAT_RG8SI:
return 2;
case SG_PIXELFORMAT_R32UI:
case SG_PIXELFORMAT_R32SI:
case SG_PIXELFORMAT_R32F:
case SG_PIXELFORMAT_RG16:
case SG_PIXELFORMAT_RG16SN:
case SG_PIXELFORMAT_RG16UI:
case SG_PIXELFORMAT_RG16SI:
case SG_PIXELFORMAT_RG16F:
case SG_PIXELFORMAT_RGBA8:
case SG_PIXELFORMAT_RGBA8SN:
case SG_PIXELFORMAT_RGBA8UI:
case SG_PIXELFORMAT_RGBA8SI:
case SG_PIXELFORMAT_BGRA8:
case SG_PIXELFORMAT_RGB10A2:
case SG_PIXELFORMAT_RG11B10F:
return 4;
case SG_PIXELFORMAT_RG32UI:
case SG_PIXELFORMAT_RG32SI:
case SG_PIXELFORMAT_RG32F:
case SG_PIXELFORMAT_RGBA16:
case SG_PIXELFORMAT_RGBA16SN:
case SG_PIXELFORMAT_RGBA16UI:
case SG_PIXELFORMAT_RGBA16SI:
case SG_PIXELFORMAT_RGBA16F:
return 8;
case SG_PIXELFORMAT_RGBA32UI:
case SG_PIXELFORMAT_RGBA32SI:
case SG_PIXELFORMAT_RGBA32F:
return 16;
default:
SOKOL_UNREACHABLE;
return 0;
}
}
_SOKOL_PRIVATE int _sg_roundup(int val, int round_to) {
return (val+(round_to-1)) & ~(round_to-1);
}
/* return row pitch for an image
see ComputePitch in https://github.com/microsoft/DirectXTex/blob/master/DirectXTex/DirectXTexUtil.cpp
*/
_SOKOL_PRIVATE int _sg_row_pitch(sg_pixel_format fmt, int width, int row_align) {
int pitch;
switch (fmt) {
case SG_PIXELFORMAT_BC1_RGBA:
case SG_PIXELFORMAT_BC4_R:
case SG_PIXELFORMAT_BC4_RSN:
case SG_PIXELFORMAT_ETC2_RGB8:
case SG_PIXELFORMAT_ETC2_RGB8A1:
pitch = ((width + 3) / 4) * 8;
pitch = pitch < 8 ? 8 : pitch;
break;
case SG_PIXELFORMAT_BC2_RGBA:
case SG_PIXELFORMAT_BC3_RGBA:
case SG_PIXELFORMAT_BC5_RG:
case SG_PIXELFORMAT_BC5_RGSN:
case SG_PIXELFORMAT_BC6H_RGBF:
case SG_PIXELFORMAT_BC6H_RGBUF:
case SG_PIXELFORMAT_BC7_RGBA:
case SG_PIXELFORMAT_ETC2_RGBA8:
case SG_PIXELFORMAT_ETC2_RG11:
case SG_PIXELFORMAT_ETC2_RG11SN:
pitch = ((width + 3) / 4) * 16;
pitch = pitch < 16 ? 16 : pitch;
break;
case SG_PIXELFORMAT_PVRTC_RGB_4BPP:
case SG_PIXELFORMAT_PVRTC_RGBA_4BPP:
{
const int block_size = 4*4;
const int bpp = 4;
int width_blocks = width / 4;
width_blocks = width_blocks < 2 ? 2 : width_blocks;
pitch = width_blocks * ((block_size * bpp) / 8);
}
break;
case SG_PIXELFORMAT_PVRTC_RGB_2BPP:
case SG_PIXELFORMAT_PVRTC_RGBA_2BPP:
{
const int block_size = 8*4;
const int bpp = 2;
int width_blocks = width / 4;
width_blocks = width_blocks < 2 ? 2 : width_blocks;
pitch = width_blocks * ((block_size * bpp) / 8);
}
break;
default:
pitch = width * _sg_pixelformat_bytesize(fmt);
break;
}
pitch = _sg_roundup(pitch, row_align);
return pitch;
}
/* compute the number of rows in a surface depending on pixel format */
_SOKOL_PRIVATE int _sg_num_rows(sg_pixel_format fmt, int height) {
int num_rows;
switch (fmt) {
case SG_PIXELFORMAT_BC1_RGBA:
case SG_PIXELFORMAT_BC4_R:
case SG_PIXELFORMAT_BC4_RSN:
case SG_PIXELFORMAT_ETC2_RGB8:
case SG_PIXELFORMAT_ETC2_RGB8A1:
case SG_PIXELFORMAT_ETC2_RGBA8:
case SG_PIXELFORMAT_ETC2_RG11:
case SG_PIXELFORMAT_ETC2_RG11SN:
case SG_PIXELFORMAT_BC2_RGBA:
case SG_PIXELFORMAT_BC3_RGBA:
case SG_PIXELFORMAT_BC5_RG:
case SG_PIXELFORMAT_BC5_RGSN:
case SG_PIXELFORMAT_BC6H_RGBF:
case SG_PIXELFORMAT_BC6H_RGBUF:
case SG_PIXELFORMAT_BC7_RGBA:
case SG_PIXELFORMAT_PVRTC_RGB_4BPP:
case SG_PIXELFORMAT_PVRTC_RGBA_4BPP:
case SG_PIXELFORMAT_PVRTC_RGB_2BPP:
case SG_PIXELFORMAT_PVRTC_RGBA_2BPP:
num_rows = ((height + 3) / 4);
break;
default:
num_rows = height;
break;
}
if (num_rows < 1) {
num_rows = 1;
}
return num_rows;
}
/* return pitch of a 2D subimage / texture slice
see ComputePitch in https://github.com/microsoft/DirectXTex/blob/master/DirectXTex/DirectXTexUtil.cpp
*/
_SOKOL_PRIVATE int _sg_surface_pitch(sg_pixel_format fmt, int width, int height, int row_align) {
int num_rows = _sg_num_rows(fmt, height);
return num_rows * _sg_row_pitch(fmt, width, row_align);
}
/* capability table pixel format helper functions */
_SOKOL_PRIVATE void _sg_pixelformat_all(sg_pixelformat_info* pfi) {
pfi->sample = true;
pfi->filter = true;
pfi->blend = true;
pfi->render = true;
pfi->msaa = true;
}
_SOKOL_PRIVATE void _sg_pixelformat_s(sg_pixelformat_info* pfi) {
pfi->sample = true;
}
_SOKOL_PRIVATE void _sg_pixelformat_sf(sg_pixelformat_info* pfi) {
pfi->sample = true;
pfi->filter = true;
}
_SOKOL_PRIVATE void _sg_pixelformat_sr(sg_pixelformat_info* pfi) {
pfi->sample = true;
pfi->render = true;
}
_SOKOL_PRIVATE void _sg_pixelformat_srmd(sg_pixelformat_info* pfi) {
pfi->sample = true;
pfi->render = true;
pfi->msaa = true;
pfi->depth = true;
}
_SOKOL_PRIVATE void _sg_pixelformat_srm(sg_pixelformat_info* pfi) {
pfi->sample = true;
pfi->render = true;
pfi->msaa = true;
}
_SOKOL_PRIVATE void _sg_pixelformat_sfrm(sg_pixelformat_info* pfi) {
pfi->sample = true;
pfi->filter = true;
pfi->render = true;
pfi->msaa = true;
}
_SOKOL_PRIVATE void _sg_pixelformat_sbrm(sg_pixelformat_info* pfi) {
pfi->sample = true;
pfi->blend = true;
pfi->render = true;
pfi->msaa = true;
}
_SOKOL_PRIVATE void _sg_pixelformat_sbr(sg_pixelformat_info* pfi) {
pfi->sample = true;
pfi->blend = true;
pfi->render = true;
}
_SOKOL_PRIVATE void _sg_pixelformat_sfbr(sg_pixelformat_info* pfi) {
pfi->sample = true;
pfi->filter = true;
pfi->blend = true;
pfi->render = true;
}
/* resolve pass action defaults into a new pass action struct */
_SOKOL_PRIVATE void _sg_resolve_default_pass_action(const sg_pass_action* from, sg_pass_action* to) {
SOKOL_ASSERT(from && to);
*to = *from;
for (int i = 0; i < SG_MAX_COLOR_ATTACHMENTS; i++) {
if (to->colors[i].action == _SG_ACTION_DEFAULT) {
to->colors[i].action = SG_ACTION_CLEAR;
to->colors[i].value.r = SG_DEFAULT_CLEAR_RED;
to->colors[i].value.g = SG_DEFAULT_CLEAR_GREEN;
to->colors[i].value.b = SG_DEFAULT_CLEAR_BLUE;
to->colors[i].value.a = SG_DEFAULT_CLEAR_ALPHA;
}
}
if (to->depth.action == _SG_ACTION_DEFAULT) {
to->depth.action = SG_ACTION_CLEAR;
to->depth.value = SG_DEFAULT_CLEAR_DEPTH;
}
if (to->stencil.action == _SG_ACTION_DEFAULT) {
to->stencil.action = SG_ACTION_CLEAR;
to->stencil.value = SG_DEFAULT_CLEAR_STENCIL;
}
}
/*== DUMMY BACKEND IMPL ======================================================*/
#if defined(SOKOL_DUMMY_BACKEND)
_SOKOL_PRIVATE void _sg_dummy_setup_backend(const sg_desc* desc) {
SOKOL_ASSERT(desc);
_SOKOL_UNUSED(desc);
_sg.backend = SG_BACKEND_DUMMY;
for (int i = SG_PIXELFORMAT_R8; i < SG_PIXELFORMAT_BC1_RGBA; i++) {
_sg.formats[i].sample = true;
_sg.formats[i].filter = true;
_sg.formats[i].render = true;
_sg.formats[i].blend = true;
_sg.formats[i].msaa = true;
}
_sg.formats[SG_PIXELFORMAT_DEPTH].depth = true;
_sg.formats[SG_PIXELFORMAT_DEPTH_STENCIL].depth = true;
}
_SOKOL_PRIVATE void _sg_dummy_discard_backend(void) {
/* empty */
}
_SOKOL_PRIVATE void _sg_dummy_reset_state_cache(void) {
/* empty*/
}
_SOKOL_PRIVATE sg_resource_state _sg_dummy_create_context(_sg_context_t* ctx) {
SOKOL_ASSERT(ctx);
_SOKOL_UNUSED(ctx);
return SG_RESOURCESTATE_VALID;
}
_SOKOL_PRIVATE void _sg_dummy_destroy_context(_sg_context_t* ctx) {
SOKOL_ASSERT(ctx);
_SOKOL_UNUSED(ctx);
}
_SOKOL_PRIVATE void _sg_dummy_activate_context(_sg_context_t* ctx) {
SOKOL_ASSERT(ctx);
_SOKOL_UNUSED(ctx);
}
_SOKOL_PRIVATE sg_resource_state _sg_dummy_create_buffer(_sg_buffer_t* buf, const sg_buffer_desc* desc) {
SOKOL_ASSERT(buf && desc);
_sg_buffer_common_init(&buf->cmn, desc);
return SG_RESOURCESTATE_VALID;
}
_SOKOL_PRIVATE void _sg_dummy_destroy_buffer(_sg_buffer_t* buf) {
SOKOL_ASSERT(buf);
_SOKOL_UNUSED(buf);
}
_SOKOL_PRIVATE sg_resource_state _sg_dummy_create_image(_sg_image_t* img, const sg_image_desc* desc) {
SOKOL_ASSERT(img && desc);
_sg_image_common_init(&img->cmn, desc);
return SG_RESOURCESTATE_VALID;
}
_SOKOL_PRIVATE void _sg_dummy_destroy_image(_sg_image_t* img) {
SOKOL_ASSERT(img);
_SOKOL_UNUSED(img);
}
_SOKOL_PRIVATE sg_resource_state _sg_dummy_create_shader(_sg_shader_t* shd, const sg_shader_desc* desc) {
SOKOL_ASSERT(shd && desc);
_sg_shader_common_init(&shd->cmn, desc);
return SG_RESOURCESTATE_VALID;
}
_SOKOL_PRIVATE void _sg_dummy_destroy_shader(_sg_shader_t* shd) {
SOKOL_ASSERT(shd);
_SOKOL_UNUSED(shd);
}
_SOKOL_PRIVATE sg_resource_state _sg_dummy_create_pipeline(_sg_pipeline_t* pip, _sg_shader_t* shd, const sg_pipeline_desc* desc) {
SOKOL_ASSERT(pip && desc);
pip->shader = shd;
_sg_pipeline_common_init(&pip->cmn, desc);
for (int attr_index = 0; attr_index < SG_MAX_VERTEX_ATTRIBUTES; attr_index++) {
const sg_vertex_attr_desc* a_desc = &desc->layout.attrs[attr_index];
if (a_desc->format == SG_VERTEXFORMAT_INVALID) {
break;
}
SOKOL_ASSERT(a_desc->buffer_index < SG_MAX_SHADERSTAGE_BUFFERS);
pip->cmn.vertex_layout_valid[a_desc->buffer_index] = true;
}
return SG_RESOURCESTATE_VALID;
}
_SOKOL_PRIVATE void _sg_dummy_destroy_pipeline(_sg_pipeline_t* pip) {
SOKOL_ASSERT(pip);
_SOKOL_UNUSED(pip);
}
_SOKOL_PRIVATE sg_resource_state _sg_dummy_create_pass(_sg_pass_t* pass, _sg_image_t** att_images, const sg_pass_desc* desc) {
SOKOL_ASSERT(pass && desc);
SOKOL_ASSERT(att_images && att_images[0]);
_sg_pass_common_init(&pass->cmn, desc);
const sg_pass_attachment_desc* att_desc;
for (int i = 0; i < pass->cmn.num_color_atts; i++) {
att_desc = &desc->color_attachments[i];
SOKOL_ASSERT(att_desc->image.id != SG_INVALID_ID);
SOKOL_ASSERT(0 == pass->dmy.color_atts[i].image);
SOKOL_ASSERT(att_images[i] && (att_images[i]->slot.id == att_desc->image.id));
SOKOL_ASSERT(_sg_is_valid_rendertarget_color_format(att_images[i]->cmn.pixel_format));
pass->dmy.color_atts[i].image = att_images[i];
}
SOKOL_ASSERT(0 == pass->dmy.ds_att.image);
att_desc = &desc->depth_stencil_attachment;
if (att_desc->image.id != SG_INVALID_ID) {
const int ds_img_index = SG_MAX_COLOR_ATTACHMENTS;
SOKOL_ASSERT(att_images[ds_img_index] && (att_images[ds_img_index]->slot.id == att_desc->image.id));
SOKOL_ASSERT(_sg_is_valid_rendertarget_depth_format(att_images[ds_img_index]->cmn.pixel_format));
pass->dmy.ds_att.image = att_images[ds_img_index];
}
return SG_RESOURCESTATE_VALID;
}
_SOKOL_PRIVATE void _sg_dummy_destroy_pass(_sg_pass_t* pass) {
SOKOL_ASSERT(pass);
_SOKOL_UNUSED(pass);
}
_SOKOL_PRIVATE _sg_image_t* _sg_dummy_pass_color_image(const _sg_pass_t* pass, int index) {
SOKOL_ASSERT(pass && (index >= 0) && (index < SG_MAX_COLOR_ATTACHMENTS));
/* NOTE: may return null */
return pass->dmy.color_atts[index].image;
}
_SOKOL_PRIVATE _sg_image_t* _sg_dummy_pass_ds_image(const _sg_pass_t* pass) {
/* NOTE: may return null */
SOKOL_ASSERT(pass);
return pass->dmy.ds_att.image;
}
_SOKOL_PRIVATE void _sg_dummy_begin_pass(_sg_pass_t* pass, const sg_pass_action* action, int w, int h) {
SOKOL_ASSERT(action);
_SOKOL_UNUSED(pass);
_SOKOL_UNUSED(action);
_SOKOL_UNUSED(w);
_SOKOL_UNUSED(h);
}
_SOKOL_PRIVATE void _sg_dummy_end_pass(void) {
/* empty */
}
_SOKOL_PRIVATE void _sg_dummy_commit(void) {
/* empty */
}
_SOKOL_PRIVATE void _sg_dummy_apply_viewport(int x, int y, int w, int h, bool origin_top_left) {
_SOKOL_UNUSED(x);
_SOKOL_UNUSED(y);
_SOKOL_UNUSED(w);
_SOKOL_UNUSED(h);
_SOKOL_UNUSED(origin_top_left);
}
_SOKOL_PRIVATE void _sg_dummy_apply_scissor_rect(int x, int y, int w, int h, bool origin_top_left) {
_SOKOL_UNUSED(x);
_SOKOL_UNUSED(y);
_SOKOL_UNUSED(w);
_SOKOL_UNUSED(h);
_SOKOL_UNUSED(origin_top_left);
}
_SOKOL_PRIVATE void _sg_dummy_apply_pipeline(_sg_pipeline_t* pip) {
SOKOL_ASSERT(pip);
_SOKOL_UNUSED(pip);
}
_SOKOL_PRIVATE void _sg_dummy_apply_bindings(
_sg_pipeline_t* pip,
_sg_buffer_t** vbs, const int* vb_offsets, int num_vbs,
_sg_buffer_t* ib, int ib_offset,
_sg_image_t** vs_imgs, int num_vs_imgs,
_sg_image_t** fs_imgs, int num_fs_imgs)
{
SOKOL_ASSERT(pip);
SOKOL_ASSERT(vbs && vb_offsets);
SOKOL_ASSERT(vs_imgs);
SOKOL_ASSERT(fs_imgs);
_SOKOL_UNUSED(pip);
_SOKOL_UNUSED(vbs); _SOKOL_UNUSED(vb_offsets); _SOKOL_UNUSED(num_vbs);
_SOKOL_UNUSED(ib); _SOKOL_UNUSED(ib_offset);
_SOKOL_UNUSED(vs_imgs); _SOKOL_UNUSED(num_vs_imgs);
_SOKOL_UNUSED(fs_imgs); _SOKOL_UNUSED(num_fs_imgs);
}
_SOKOL_PRIVATE void _sg_dummy_apply_uniforms(sg_shader_stage stage_index, int ub_index, const sg_range* data) {
_SOKOL_UNUSED(stage_index);
_SOKOL_UNUSED(ub_index);
_SOKOL_UNUSED(data);
}
_SOKOL_PRIVATE void _sg_dummy_draw(int base_element, int num_elements, int num_instances) {
_SOKOL_UNUSED(base_element);
_SOKOL_UNUSED(num_elements);
_SOKOL_UNUSED(num_instances);
}
_SOKOL_PRIVATE void _sg_dummy_update_buffer(_sg_buffer_t* buf, const sg_range* data) {
SOKOL_ASSERT(buf && data && data->ptr && (data->size > 0));
_SOKOL_UNUSED(data);
if (++buf->cmn.active_slot >= buf->cmn.num_slots) {
buf->cmn.active_slot = 0;
}
}
_SOKOL_PRIVATE int _sg_dummy_append_buffer(_sg_buffer_t* buf, const sg_range* data, bool new_frame) {
SOKOL_ASSERT(buf && data && data->ptr && (data->size > 0));
_SOKOL_UNUSED(data);
if (new_frame) {
if (++buf->cmn.active_slot >= buf->cmn.num_slots) {
buf->cmn.active_slot = 0;
}
}
/* NOTE: this is a requirement from WebGPU, but we want identical behaviour across all backend */
return _sg_roundup((int)data->size, 4);
}
_SOKOL_PRIVATE void _sg_dummy_update_image(_sg_image_t* img, const sg_image_data* data) {
SOKOL_ASSERT(img && data);
_SOKOL_UNUSED(data);
if (++img->cmn.active_slot >= img->cmn.num_slots) {
img->cmn.active_slot = 0;
}
}
/*== GL BACKEND ==============================================================*/
#elif defined(_SOKOL_ANY_GL)
/*=== OPTIONAL GL LOADER FOR WIN32 ===========================================*/
#if defined(_SOKOL_USE_WIN32_GL_LOADER)
// X Macro list of GL function names and signatures
#define _SG_GL_FUNCS \
_SG_XMACRO(glBindVertexArray, void, (GLuint array)) \
_SG_XMACRO(glFramebufferTextureLayer, void, (GLenum target, GLenum attachment, GLuint texture, GLint level, GLint layer)) \
_SG_XMACRO(glGenFramebuffers, void, (GLsizei n, GLuint * framebuffers)) \
_SG_XMACRO(glBindFramebuffer, void, (GLenum target, GLuint framebuffer)) \
_SG_XMACRO(glBindRenderbuffer, void, (GLenum target, GLuint renderbuffer)) \
_SG_XMACRO(glGetStringi, const GLubyte *, (GLenum name, GLuint index)) \
_SG_XMACRO(glClearBufferfi, void, (GLenum buffer, GLint drawbuffer, GLfloat depth, GLint stencil)) \
_SG_XMACRO(glClearBufferfv, void, (GLenum buffer, GLint drawbuffer, const GLfloat * value)) \
_SG_XMACRO(glClearBufferuiv, void, (GLenum buffer, GLint drawbuffer, const GLuint * value)) \
_SG_XMACRO(glClearBufferiv, void, (GLenum buffer, GLint drawbuffer, const GLint * value)) \
_SG_XMACRO(glDeleteRenderbuffers, void, (GLsizei n, const GLuint * renderbuffers)) \
_SG_XMACRO(glUniform4fv, void, (GLint location, GLsizei count, const GLfloat * value)) \
_SG_XMACRO(glUniform2fv, void, (GLint location, GLsizei count, const GLfloat * value)) \
_SG_XMACRO(glUseProgram, void, (GLuint program)) \
_SG_XMACRO(glShaderSource, void, (GLuint shader, GLsizei count, const GLchar *const* string, const GLint * length)) \
_SG_XMACRO(glLinkProgram, void, (GLuint program)) \
_SG_XMACRO(glGetUniformLocation, GLint, (GLuint program, const GLchar * name)) \
_SG_XMACRO(glGetShaderiv, void, (GLuint shader, GLenum pname, GLint * params)) \
_SG_XMACRO(glGetProgramInfoLog, void, (GLuint program, GLsizei bufSize, GLsizei * length, GLchar * infoLog)) \
_SG_XMACRO(glGetAttribLocation, GLint, (GLuint program, const GLchar * name)) \
_SG_XMACRO(glDisableVertexAttribArray, void, (GLuint index)) \
_SG_XMACRO(glDeleteShader, void, (GLuint shader)) \
_SG_XMACRO(glDeleteProgram, void, (GLuint program)) \
_SG_XMACRO(glCompileShader, void, (GLuint shader)) \
_SG_XMACRO(glStencilFuncSeparate, void, (GLenum face, GLenum func, GLint ref, GLuint mask)) \
_SG_XMACRO(glStencilOpSeparate, void, (GLenum face, GLenum sfail, GLenum dpfail, GLenum dppass)) \
_SG_XMACRO(glRenderbufferStorageMultisample, void, (GLenum target, GLsizei samples, GLenum internalformat, GLsizei width, GLsizei height)) \
_SG_XMACRO(glDrawBuffers, void, (GLsizei n, const GLenum * bufs)) \
_SG_XMACRO(glVertexAttribDivisor, void, (GLuint index, GLuint divisor)) \
_SG_XMACRO(glBufferSubData, void, (GLenum target, GLintptr offset, GLsizeiptr size, const void * data)) \
_SG_XMACRO(glGenBuffers, void, (GLsizei n, GLuint * buffers)) \
_SG_XMACRO(glCheckFramebufferStatus, GLenum, (GLenum target)) \
_SG_XMACRO(glFramebufferRenderbuffer, void, (GLenum target, GLenum attachment, GLenum renderbuffertarget, GLuint renderbuffer)) \
_SG_XMACRO(glCompressedTexImage2D, void, (GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLint border, GLsizei imageSize, const void * data)) \
_SG_XMACRO(glCompressedTexImage3D, void, (GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLsizei imageSize, const void * data)) \
_SG_XMACRO(glActiveTexture, void, (GLenum texture)) \
_SG_XMACRO(glTexSubImage3D, void, (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, const void * pixels)) \
_SG_XMACRO(glUniformMatrix4fv, void, (GLint location, GLsizei count, GLboolean transpose, const GLfloat * value)) \
_SG_XMACRO(glRenderbufferStorage, void, (GLenum target, GLenum internalformat, GLsizei width, GLsizei height)) \
_SG_XMACRO(glGenTextures, void, (GLsizei n, GLuint * textures)) \
_SG_XMACRO(glPolygonOffset, void, (GLfloat factor, GLfloat units)) \
_SG_XMACRO(glDrawElements, void, (GLenum mode, GLsizei count, GLenum type, const void * indices)) \
_SG_XMACRO(glDeleteFramebuffers, void, (GLsizei n, const GLuint * framebuffers)) \
_SG_XMACRO(glBlendEquationSeparate, void, (GLenum modeRGB, GLenum modeAlpha)) \
_SG_XMACRO(glDeleteTextures, void, (GLsizei n, const GLuint * textures)) \
_SG_XMACRO(glGetProgramiv, void, (GLuint program, GLenum pname, GLint * params)) \
_SG_XMACRO(glBindTexture, void, (GLenum target, GLuint texture)) \
_SG_XMACRO(glTexImage3D, void, (GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLenum format, GLenum type, const void * pixels)) \
_SG_XMACRO(glCreateShader, GLuint, (GLenum type)) \
_SG_XMACRO(glTexSubImage2D, void, (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const void * pixels)) \
_SG_XMACRO(glClearDepth, void, (GLdouble depth)) \
_SG_XMACRO(glFramebufferTexture2D, void, (GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level)) \
_SG_XMACRO(glCreateProgram, GLuint, (void)) \
_SG_XMACRO(glViewport, void, (GLint x, GLint y, GLsizei width, GLsizei height)) \
_SG_XMACRO(glDeleteBuffers, void, (GLsizei n, const GLuint * buffers)) \
_SG_XMACRO(glDrawArrays, void, (GLenum mode, GLint first, GLsizei count)) \
_SG_XMACRO(glDrawElementsInstanced, void, (GLenum mode, GLsizei count, GLenum type, const void * indices, GLsizei instancecount)) \
_SG_XMACRO(glVertexAttribPointer, void, (GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const void * pointer)) \
_SG_XMACRO(glUniform1i, void, (GLint location, GLint v0)) \
_SG_XMACRO(glDisable, void, (GLenum cap)) \
_SG_XMACRO(glColorMask, void, (GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha)) \
_SG_XMACRO(glColorMaski, void, (GLuint buf, GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha)) \
_SG_XMACRO(glBindBuffer, void, (GLenum target, GLuint buffer)) \
_SG_XMACRO(glDeleteVertexArrays, void, (GLsizei n, const GLuint * arrays)) \
_SG_XMACRO(glDepthMask, void, (GLboolean flag)) \
_SG_XMACRO(glDrawArraysInstanced, void, (GLenum mode, GLint first, GLsizei count, GLsizei instancecount)) \
_SG_XMACRO(glClearStencil, void, (GLint s)) \
_SG_XMACRO(glScissor, void, (GLint x, GLint y, GLsizei width, GLsizei height)) \
_SG_XMACRO(glUniform3fv, void, (GLint location, GLsizei count, const GLfloat * value)) \
_SG_XMACRO(glGenRenderbuffers, void, (GLsizei n, GLuint * renderbuffers)) \
_SG_XMACRO(glBufferData, void, (GLenum target, GLsizeiptr size, const void * data, GLenum usage)) \
_SG_XMACRO(glBlendFuncSeparate, void, (GLenum sfactorRGB, GLenum dfactorRGB, GLenum sfactorAlpha, GLenum dfactorAlpha)) \
_SG_XMACRO(glTexParameteri, void, (GLenum target, GLenum pname, GLint param)) \
_SG_XMACRO(glGetIntegerv, void, (GLenum pname, GLint * data)) \
_SG_XMACRO(glEnable, void, (GLenum cap)) \
_SG_XMACRO(glBlitFramebuffer, void, (GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1, GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1, GLbitfield mask, GLenum filter)) \
_SG_XMACRO(glStencilMask, void, (GLuint mask)) \
_SG_XMACRO(glAttachShader, void, (GLuint program, GLuint shader)) \
_SG_XMACRO(glGetError, GLenum, (void)) \
_SG_XMACRO(glClearColor, void, (GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha)) \
_SG_XMACRO(glBlendColor, void, (GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha)) \
_SG_XMACRO(glTexParameterf, void, (GLenum target, GLenum pname, GLfloat param)) \
_SG_XMACRO(glTexParameterfv, void, (GLenum target, GLenum pname, GLfloat* params)) \
_SG_XMACRO(glGetShaderInfoLog, void, (GLuint shader, GLsizei bufSize, GLsizei * length, GLchar * infoLog)) \
_SG_XMACRO(glDepthFunc, void, (GLenum func)) \
_SG_XMACRO(glStencilOp , void, (GLenum fail, GLenum zfail, GLenum zpass)) \
_SG_XMACRO(glStencilFunc, void, (GLenum func, GLint ref, GLuint mask)) \
_SG_XMACRO(glEnableVertexAttribArray, void, (GLuint index)) \
_SG_XMACRO(glBlendFunc, void, (GLenum sfactor, GLenum dfactor)) \
_SG_XMACRO(glUniform1fv, void, (GLint location, GLsizei count, const GLfloat * value)) \
_SG_XMACRO(glReadBuffer, void, (GLenum src)) \
_SG_XMACRO(glClear, void, (GLbitfield mask)) \
_SG_XMACRO(glTexImage2D, void, (GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const void * pixels)) \
_SG_XMACRO(glGenVertexArrays, void, (GLsizei n, GLuint * arrays)) \
_SG_XMACRO(glFrontFace, void, (GLenum mode)) \
_SG_XMACRO(glCullFace, void, (GLenum mode))
// generate GL function pointer typedefs
#define _SG_XMACRO(name, ret, args) typedef ret (GL_APIENTRY* PFN_ ## name) args;
_SG_GL_FUNCS
#undef _SG_XMACRO
// generate GL function pointers
#define _SG_XMACRO(name, ret, args) static PFN_ ## name name;
_SG_GL_FUNCS
#undef _SG_XMACRO
// helper function to lookup GL functions in GL DLL
typedef PROC (WINAPI * _sg_wglGetProcAddress)(LPCSTR);
_SOKOL_PRIVATE void* _sg_gl_getprocaddr(const char* name, _sg_wglGetProcAddress wgl_getprocaddress) {
void* proc_addr = (void*) wgl_getprocaddress(name);
if (0 == proc_addr) {
proc_addr = (void*) GetProcAddress(_sg.gl.opengl32_dll, name);
}
SOKOL_ASSERT(proc_addr);
return proc_addr;
}
// populate GL function pointers
_SOKOL_PRIVATE void _sg_gl_load_opengl(void) {
SOKOL_ASSERT(0 == _sg.gl.opengl32_dll);
_sg.gl.opengl32_dll = LoadLibraryA("opengl32.dll");
SOKOL_ASSERT(_sg.gl.opengl32_dll);
_sg_wglGetProcAddress wgl_getprocaddress = (_sg_wglGetProcAddress) GetProcAddress(_sg.gl.opengl32_dll, "wglGetProcAddress");
SOKOL_ASSERT(wgl_getprocaddress);
#define _SG_XMACRO(name, ret, args) name = (PFN_ ## name) _sg_gl_getprocaddr(#name, wgl_getprocaddress);
_SG_GL_FUNCS
#undef _SG_XMACRO
}
_SOKOL_PRIVATE void _sg_gl_unload_opengl(void) {
SOKOL_ASSERT(_sg.gl.opengl32_dll);
FreeLibrary(_sg.gl.opengl32_dll);
_sg.gl.opengl32_dll = 0;
}
#endif // _SOKOL_USE_WIN32_GL_LOADER
/*-- type translation --------------------------------------------------------*/
_SOKOL_PRIVATE GLenum _sg_gl_buffer_target(sg_buffer_type t) {
switch (t) {
case SG_BUFFERTYPE_VERTEXBUFFER: return GL_ARRAY_BUFFER;
case SG_BUFFERTYPE_INDEXBUFFER: return GL_ELEMENT_ARRAY_BUFFER;
default: SOKOL_UNREACHABLE; return 0;
}
}
_SOKOL_PRIVATE GLenum _sg_gl_texture_target(sg_image_type t) {
switch (t) {
case SG_IMAGETYPE_2D: return GL_TEXTURE_2D;
case SG_IMAGETYPE_CUBE: return GL_TEXTURE_CUBE_MAP;
#if !defined(SOKOL_GLES2)
case SG_IMAGETYPE_3D: return GL_TEXTURE_3D;
case SG_IMAGETYPE_ARRAY: return GL_TEXTURE_2D_ARRAY;
#endif
default: SOKOL_UNREACHABLE; return 0;
}
}
_SOKOL_PRIVATE GLenum _sg_gl_usage(sg_usage u) {
switch (u) {
case SG_USAGE_IMMUTABLE: return GL_STATIC_DRAW;
case SG_USAGE_DYNAMIC: return GL_DYNAMIC_DRAW;
case SG_USAGE_STREAM: return GL_STREAM_DRAW;
default: SOKOL_UNREACHABLE; return 0;
}
}
_SOKOL_PRIVATE GLenum _sg_gl_shader_stage(sg_shader_stage stage) {
switch (stage) {
case SG_SHADERSTAGE_VS: return GL_VERTEX_SHADER;
case SG_SHADERSTAGE_FS: return GL_FRAGMENT_SHADER;
default: SOKOL_UNREACHABLE; return 0;
}
}
_SOKOL_PRIVATE GLint _sg_gl_vertexformat_size(sg_vertex_format fmt) {
switch (fmt) {
case SG_VERTEXFORMAT_FLOAT: return 1;
case SG_VERTEXFORMAT_FLOAT2: return 2;
case SG_VERTEXFORMAT_FLOAT3: return 3;
case SG_VERTEXFORMAT_FLOAT4: return 4;
case SG_VERTEXFORMAT_BYTE4: return 4;
case SG_VERTEXFORMAT_BYTE4N: return 4;
case SG_VERTEXFORMAT_UBYTE4: return 4;
case SG_VERTEXFORMAT_UBYTE4N: return 4;
case SG_VERTEXFORMAT_SHORT2: return 2;
case SG_VERTEXFORMAT_SHORT2N: return 2;
case SG_VERTEXFORMAT_USHORT2N: return 2;
case SG_VERTEXFORMAT_SHORT4: return 4;
case SG_VERTEXFORMAT_SHORT4N: return 4;
case SG_VERTEXFORMAT_USHORT4N: return 4;
case SG_VERTEXFORMAT_UINT10_N2: return 4;
default: SOKOL_UNREACHABLE; return 0;
}
}
_SOKOL_PRIVATE GLenum _sg_gl_vertexformat_type(sg_vertex_format fmt) {
switch (fmt) {
case SG_VERTEXFORMAT_FLOAT:
case SG_VERTEXFORMAT_FLOAT2:
case SG_VERTEXFORMAT_FLOAT3:
case SG_VERTEXFORMAT_FLOAT4:
return GL_FLOAT;
case SG_VERTEXFORMAT_BYTE4:
case SG_VERTEXFORMAT_BYTE4N:
return GL_BYTE;
case SG_VERTEXFORMAT_UBYTE4:
case SG_VERTEXFORMAT_UBYTE4N:
return GL_UNSIGNED_BYTE;
case SG_VERTEXFORMAT_SHORT2:
case SG_VERTEXFORMAT_SHORT2N:
case SG_VERTEXFORMAT_SHORT4:
case SG_VERTEXFORMAT_SHORT4N:
return GL_SHORT;
case SG_VERTEXFORMAT_USHORT2N:
case SG_VERTEXFORMAT_USHORT4N:
return GL_UNSIGNED_SHORT;
case SG_VERTEXFORMAT_UINT10_N2:
return GL_UNSIGNED_INT_2_10_10_10_REV;
default:
SOKOL_UNREACHABLE; return 0;
}
}
_SOKOL_PRIVATE GLboolean _sg_gl_vertexformat_normalized(sg_vertex_format fmt) {
switch (fmt) {
case SG_VERTEXFORMAT_BYTE4N:
case SG_VERTEXFORMAT_UBYTE4N:
case SG_VERTEXFORMAT_SHORT2N:
case SG_VERTEXFORMAT_USHORT2N:
case SG_VERTEXFORMAT_SHORT4N:
case SG_VERTEXFORMAT_USHORT4N:
case SG_VERTEXFORMAT_UINT10_N2:
return GL_TRUE;
default:
return GL_FALSE;
}
}
_SOKOL_PRIVATE GLenum _sg_gl_primitive_type(sg_primitive_type t) {
switch (t) {
case SG_PRIMITIVETYPE_POINTS: return GL_POINTS;
case SG_PRIMITIVETYPE_LINES: return GL_LINES;
case SG_PRIMITIVETYPE_LINE_STRIP: return GL_LINE_STRIP;
case SG_PRIMITIVETYPE_TRIANGLES: return GL_TRIANGLES;
case SG_PRIMITIVETYPE_TRIANGLE_STRIP: return GL_TRIANGLE_STRIP;
default: SOKOL_UNREACHABLE; return 0;
}
}
_SOKOL_PRIVATE GLenum _sg_gl_index_type(sg_index_type t) {
switch (t) {
case SG_INDEXTYPE_NONE: return 0;
case SG_INDEXTYPE_UINT16: return GL_UNSIGNED_SHORT;
case SG_INDEXTYPE_UINT32: return GL_UNSIGNED_INT;
default: SOKOL_UNREACHABLE; return 0;
}
}
_SOKOL_PRIVATE GLenum _sg_gl_compare_func(sg_compare_func cmp) {
switch (cmp) {
case SG_COMPAREFUNC_NEVER: return GL_NEVER;
case SG_COMPAREFUNC_LESS: return GL_LESS;
case SG_COMPAREFUNC_EQUAL: return GL_EQUAL;
case SG_COMPAREFUNC_LESS_EQUAL: return GL_LEQUAL;
case SG_COMPAREFUNC_GREATER: return GL_GREATER;
case SG_COMPAREFUNC_NOT_EQUAL: return GL_NOTEQUAL;
case SG_COMPAREFUNC_GREATER_EQUAL: return GL_GEQUAL;
case SG_COMPAREFUNC_ALWAYS: return GL_ALWAYS;
default: SOKOL_UNREACHABLE; return 0;
}
}
_SOKOL_PRIVATE GLenum _sg_gl_stencil_op(sg_stencil_op op) {
switch (op) {
case SG_STENCILOP_KEEP: return GL_KEEP;
case SG_STENCILOP_ZERO: return GL_ZERO;
case SG_STENCILOP_REPLACE: return GL_REPLACE;
case SG_STENCILOP_INCR_CLAMP: return GL_INCR;
case SG_STENCILOP_DECR_CLAMP: return GL_DECR;
case SG_STENCILOP_INVERT: return GL_INVERT;
case SG_STENCILOP_INCR_WRAP: return GL_INCR_WRAP;
case SG_STENCILOP_DECR_WRAP: return GL_DECR_WRAP;
default: SOKOL_UNREACHABLE; return 0;
}
}
_SOKOL_PRIVATE GLenum _sg_gl_blend_factor(sg_blend_factor f) {
switch (f) {
case SG_BLENDFACTOR_ZERO: return GL_ZERO;
case SG_BLENDFACTOR_ONE: return GL_ONE;
case SG_BLENDFACTOR_SRC_COLOR: return GL_SRC_COLOR;
case SG_BLENDFACTOR_ONE_MINUS_SRC_COLOR: return GL_ONE_MINUS_SRC_COLOR;
case SG_BLENDFACTOR_SRC_ALPHA: return GL_SRC_ALPHA;
case SG_BLENDFACTOR_ONE_MINUS_SRC_ALPHA: return GL_ONE_MINUS_SRC_ALPHA;
case SG_BLENDFACTOR_DST_COLOR: return GL_DST_COLOR;
case SG_BLENDFACTOR_ONE_MINUS_DST_COLOR: return GL_ONE_MINUS_DST_COLOR;
case SG_BLENDFACTOR_DST_ALPHA: return GL_DST_ALPHA;
case SG_BLENDFACTOR_ONE_MINUS_DST_ALPHA: return GL_ONE_MINUS_DST_ALPHA;
case SG_BLENDFACTOR_SRC_ALPHA_SATURATED: return GL_SRC_ALPHA_SATURATE;
case SG_BLENDFACTOR_BLEND_COLOR: return GL_CONSTANT_COLOR;
case SG_BLENDFACTOR_ONE_MINUS_BLEND_COLOR: return GL_ONE_MINUS_CONSTANT_COLOR;
case SG_BLENDFACTOR_BLEND_ALPHA: return GL_CONSTANT_ALPHA;
case SG_BLENDFACTOR_ONE_MINUS_BLEND_ALPHA: return GL_ONE_MINUS_CONSTANT_ALPHA;
default: SOKOL_UNREACHABLE; return 0;
}
}
_SOKOL_PRIVATE GLenum _sg_gl_blend_op(sg_blend_op op) {
switch (op) {
case SG_BLENDOP_ADD: return GL_FUNC_ADD;
case SG_BLENDOP_SUBTRACT: return GL_FUNC_SUBTRACT;
case SG_BLENDOP_REVERSE_SUBTRACT: return GL_FUNC_REVERSE_SUBTRACT;
default: SOKOL_UNREACHABLE; return 0;
}
}
_SOKOL_PRIVATE GLenum _sg_gl_filter(sg_filter f) {
switch (f) {
case SG_FILTER_NEAREST: return GL_NEAREST;
case SG_FILTER_LINEAR: return GL_LINEAR;
case SG_FILTER_NEAREST_MIPMAP_NEAREST: return GL_NEAREST_MIPMAP_NEAREST;
case SG_FILTER_NEAREST_MIPMAP_LINEAR: return GL_NEAREST_MIPMAP_LINEAR;
case SG_FILTER_LINEAR_MIPMAP_NEAREST: return GL_LINEAR_MIPMAP_NEAREST;
case SG_FILTER_LINEAR_MIPMAP_LINEAR: return GL_LINEAR_MIPMAP_LINEAR;
default: SOKOL_UNREACHABLE; return 0;
}
}
_SOKOL_PRIVATE GLenum _sg_gl_wrap(sg_wrap w) {
switch (w) {
case SG_WRAP_CLAMP_TO_EDGE: return GL_CLAMP_TO_EDGE;
#if defined(SOKOL_GLCORE33)
case SG_WRAP_CLAMP_TO_BORDER: return GL_CLAMP_TO_BORDER;
#else
case SG_WRAP_CLAMP_TO_BORDER: return GL_CLAMP_TO_EDGE;
#endif
case SG_WRAP_REPEAT: return GL_REPEAT;
case SG_WRAP_MIRRORED_REPEAT: return GL_MIRRORED_REPEAT;
default: SOKOL_UNREACHABLE; return 0;
}
}
_SOKOL_PRIVATE GLenum _sg_gl_teximage_type(sg_pixel_format fmt) {
switch (fmt) {
case SG_PIXELFORMAT_R8:
case SG_PIXELFORMAT_R8UI:
case SG_PIXELFORMAT_RG8:
case SG_PIXELFORMAT_RG8UI:
case SG_PIXELFORMAT_RGBA8:
case SG_PIXELFORMAT_RGBA8UI:
case SG_PIXELFORMAT_BGRA8:
return GL_UNSIGNED_BYTE;
case SG_PIXELFORMAT_R8SN:
case SG_PIXELFORMAT_R8SI:
case SG_PIXELFORMAT_RG8SN:
case SG_PIXELFORMAT_RG8SI:
case SG_PIXELFORMAT_RGBA8SN:
case SG_PIXELFORMAT_RGBA8SI:
return GL_BYTE;
case SG_PIXELFORMAT_R16:
case SG_PIXELFORMAT_R16UI:
case SG_PIXELFORMAT_RG16:
case SG_PIXELFORMAT_RG16UI:
case SG_PIXELFORMAT_RGBA16:
case SG_PIXELFORMAT_RGBA16UI:
return GL_UNSIGNED_SHORT;
case SG_PIXELFORMAT_R16SN:
case SG_PIXELFORMAT_R16SI:
case SG_PIXELFORMAT_RG16SN:
case SG_PIXELFORMAT_RG16SI:
case SG_PIXELFORMAT_RGBA16SN:
case SG_PIXELFORMAT_RGBA16SI:
return GL_SHORT;
case SG_PIXELFORMAT_R16F:
case SG_PIXELFORMAT_RG16F:
case SG_PIXELFORMAT_RGBA16F:
return GL_HALF_FLOAT;
case SG_PIXELFORMAT_R32UI:
case SG_PIXELFORMAT_RG32UI:
case SG_PIXELFORMAT_RGBA32UI:
return GL_UNSIGNED_INT;
case SG_PIXELFORMAT_R32SI:
case SG_PIXELFORMAT_RG32SI:
case SG_PIXELFORMAT_RGBA32SI:
return GL_INT;
case SG_PIXELFORMAT_R32F:
case SG_PIXELFORMAT_RG32F:
case SG_PIXELFORMAT_RGBA32F:
return GL_FLOAT;
#if !defined(SOKOL_GLES2)
case SG_PIXELFORMAT_RGB10A2:
return GL_UNSIGNED_INT_2_10_10_10_REV;
case SG_PIXELFORMAT_RG11B10F:
return GL_UNSIGNED_INT_10F_11F_11F_REV;
#endif
case SG_PIXELFORMAT_DEPTH:
return GL_UNSIGNED_SHORT;
case SG_PIXELFORMAT_DEPTH_STENCIL:
return GL_UNSIGNED_INT_24_8;
default:
SOKOL_UNREACHABLE; return 0;
}
}
_SOKOL_PRIVATE GLenum _sg_gl_teximage_format(sg_pixel_format fmt) {
switch (fmt) {
case SG_PIXELFORMAT_R8:
case SG_PIXELFORMAT_R8SN:
case SG_PIXELFORMAT_R16:
case SG_PIXELFORMAT_R16SN:
case SG_PIXELFORMAT_R16F:
case SG_PIXELFORMAT_R32F:
#if defined(SOKOL_GLES2)
return GL_LUMINANCE;
#else
if (_sg.gl.gles2) {
return GL_LUMINANCE;
}
else {
return GL_RED;
}
#endif
#if !defined(SOKOL_GLES2)
case SG_PIXELFORMAT_R8UI:
case SG_PIXELFORMAT_R8SI:
case SG_PIXELFORMAT_R16UI:
case SG_PIXELFORMAT_R16SI:
case SG_PIXELFORMAT_R32UI:
case SG_PIXELFORMAT_R32SI:
return GL_RED_INTEGER;
case SG_PIXELFORMAT_RG8:
case SG_PIXELFORMAT_RG8SN:
case SG_PIXELFORMAT_RG16:
case SG_PIXELFORMAT_RG16SN:
case SG_PIXELFORMAT_RG16F:
case SG_PIXELFORMAT_RG32F:
return GL_RG;
case SG_PIXELFORMAT_RG8UI:
case SG_PIXELFORMAT_RG8SI:
case SG_PIXELFORMAT_RG16UI:
case SG_PIXELFORMAT_RG16SI:
case SG_PIXELFORMAT_RG32UI:
case SG_PIXELFORMAT_RG32SI:
return GL_RG_INTEGER;
#endif
case SG_PIXELFORMAT_RGBA8:
case SG_PIXELFORMAT_RGBA8SN:
case SG_PIXELFORMAT_RGBA16:
case SG_PIXELFORMAT_RGBA16SN:
case SG_PIXELFORMAT_RGBA16F:
case SG_PIXELFORMAT_RGBA32F:
case SG_PIXELFORMAT_RGB10A2:
return GL_RGBA;
#if !defined(SOKOL_GLES2)
case SG_PIXELFORMAT_RGBA8UI:
case SG_PIXELFORMAT_RGBA8SI:
case SG_PIXELFORMAT_RGBA16UI:
case SG_PIXELFORMAT_RGBA16SI:
case SG_PIXELFORMAT_RGBA32UI:
case SG_PIXELFORMAT_RGBA32SI:
return GL_RGBA_INTEGER;
#endif
case SG_PIXELFORMAT_RG11B10F:
return GL_RGB;
case SG_PIXELFORMAT_DEPTH:
return GL_DEPTH_COMPONENT;
case SG_PIXELFORMAT_DEPTH_STENCIL:
return GL_DEPTH_STENCIL;
case SG_PIXELFORMAT_BC1_RGBA:
return GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
case SG_PIXELFORMAT_BC2_RGBA:
return GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
case SG_PIXELFORMAT_BC3_RGBA:
return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
case SG_PIXELFORMAT_BC4_R:
return GL_COMPRESSED_RED_RGTC1;
case SG_PIXELFORMAT_BC4_RSN:
return GL_COMPRESSED_SIGNED_RED_RGTC1;
case SG_PIXELFORMAT_BC5_RG:
return GL_COMPRESSED_RED_GREEN_RGTC2;
case SG_PIXELFORMAT_BC5_RGSN:
return GL_COMPRESSED_SIGNED_RED_GREEN_RGTC2;
case SG_PIXELFORMAT_BC6H_RGBF:
return GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB;
case SG_PIXELFORMAT_BC6H_RGBUF:
return GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT_ARB;
case SG_PIXELFORMAT_BC7_RGBA:
return GL_COMPRESSED_RGBA_BPTC_UNORM_ARB;
case SG_PIXELFORMAT_PVRTC_RGB_2BPP:
return GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG;
case SG_PIXELFORMAT_PVRTC_RGB_4BPP:
return GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG;
case SG_PIXELFORMAT_PVRTC_RGBA_2BPP:
return GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG;
case SG_PIXELFORMAT_PVRTC_RGBA_4BPP:
return GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG;
case SG_PIXELFORMAT_ETC2_RGB8:
return GL_COMPRESSED_RGB8_ETC2;
case SG_PIXELFORMAT_ETC2_RGB8A1:
return GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2;
case SG_PIXELFORMAT_ETC2_RGBA8:
return GL_COMPRESSED_RGBA8_ETC2_EAC;
case SG_PIXELFORMAT_ETC2_RG11:
return GL_COMPRESSED_RG11_EAC;
case SG_PIXELFORMAT_ETC2_RG11SN:
return GL_COMPRESSED_SIGNED_RG11_EAC;
default:
SOKOL_UNREACHABLE; return 0;
}
}
_SOKOL_PRIVATE GLenum _sg_gl_teximage_internal_format(sg_pixel_format fmt) {
#if defined(SOKOL_GLES2)
return _sg_gl_teximage_format(fmt);
#else
if (_sg.gl.gles2) {
return _sg_gl_teximage_format(fmt);
}
else {
switch (fmt) {
case SG_PIXELFORMAT_R8: return GL_R8;
case SG_PIXELFORMAT_R8SN: return GL_R8_SNORM;
case SG_PIXELFORMAT_R8UI: return GL_R8UI;
case SG_PIXELFORMAT_R8SI: return GL_R8I;
#if !defined(SOKOL_GLES3)
case SG_PIXELFORMAT_R16: return GL_R16;
case SG_PIXELFORMAT_R16SN: return GL_R16_SNORM;
#endif
case SG_PIXELFORMAT_R16UI: return GL_R16UI;
case SG_PIXELFORMAT_R16SI: return GL_R16I;
case SG_PIXELFORMAT_R16F: return GL_R16F;
case SG_PIXELFORMAT_RG8: return GL_RG8;
case SG_PIXELFORMAT_RG8SN: return GL_RG8_SNORM;
case SG_PIXELFORMAT_RG8UI: return GL_RG8UI;
case SG_PIXELFORMAT_RG8SI: return GL_RG8I;
case SG_PIXELFORMAT_R32UI: return GL_R32UI;
case SG_PIXELFORMAT_R32SI: return GL_R32I;
case SG_PIXELFORMAT_R32F: return GL_R32F;
#if !defined(SOKOL_GLES3)
case SG_PIXELFORMAT_RG16: return GL_RG16;
case SG_PIXELFORMAT_RG16SN: return GL_RG16_SNORM;
#endif
case SG_PIXELFORMAT_RG16UI: return GL_RG16UI;
case SG_PIXELFORMAT_RG16SI: return GL_RG16I;
case SG_PIXELFORMAT_RG16F: return GL_RG16F;
case SG_PIXELFORMAT_RGBA8: return GL_RGBA8;
case SG_PIXELFORMAT_RGBA8SN: return GL_RGBA8_SNORM;
case SG_PIXELFORMAT_RGBA8UI: return GL_RGBA8UI;
case SG_PIXELFORMAT_RGBA8SI: return GL_RGBA8I;
case SG_PIXELFORMAT_RGB10A2: return GL_RGB10_A2;
case SG_PIXELFORMAT_RG11B10F: return GL_R11F_G11F_B10F;
case SG_PIXELFORMAT_RG32UI: return GL_RG32UI;
case SG_PIXELFORMAT_RG32SI: return GL_RG32I;
case SG_PIXELFORMAT_RG32F: return GL_RG32F;
#if !defined(SOKOL_GLES3)
case SG_PIXELFORMAT_RGBA16: return GL_RGBA16;
case SG_PIXELFORMAT_RGBA16SN: return GL_RGBA16_SNORM;
#endif
case SG_PIXELFORMAT_RGBA16UI: return GL_RGBA16UI;
case SG_PIXELFORMAT_RGBA16SI: return GL_RGBA16I;
case SG_PIXELFORMAT_RGBA16F: return GL_RGBA16F;
case SG_PIXELFORMAT_RGBA32UI: return GL_RGBA32UI;
case SG_PIXELFORMAT_RGBA32SI: return GL_RGBA32I;
case SG_PIXELFORMAT_RGBA32F: return GL_RGBA32F;
case SG_PIXELFORMAT_DEPTH: return GL_DEPTH_COMPONENT16;
case SG_PIXELFORMAT_DEPTH_STENCIL: return GL_DEPTH24_STENCIL8;
case SG_PIXELFORMAT_BC1_RGBA: return GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
case SG_PIXELFORMAT_BC2_RGBA: return GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
case SG_PIXELFORMAT_BC3_RGBA: return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
case SG_PIXELFORMAT_BC4_R: return GL_COMPRESSED_RED_RGTC1;
case SG_PIXELFORMAT_BC4_RSN: return GL_COMPRESSED_SIGNED_RED_RGTC1;
case SG_PIXELFORMAT_BC5_RG: return GL_COMPRESSED_RED_GREEN_RGTC2;
case SG_PIXELFORMAT_BC5_RGSN: return GL_COMPRESSED_SIGNED_RED_GREEN_RGTC2;
case SG_PIXELFORMAT_BC6H_RGBF: return GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB;
case SG_PIXELFORMAT_BC6H_RGBUF: return GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT_ARB;
case SG_PIXELFORMAT_BC7_RGBA: return GL_COMPRESSED_RGBA_BPTC_UNORM_ARB;
case SG_PIXELFORMAT_PVRTC_RGB_2BPP: return GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG;
case SG_PIXELFORMAT_PVRTC_RGB_4BPP: return GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG;
case SG_PIXELFORMAT_PVRTC_RGBA_2BPP: return GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG;
case SG_PIXELFORMAT_PVRTC_RGBA_4BPP: return GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG;
case SG_PIXELFORMAT_ETC2_RGB8: return GL_COMPRESSED_RGB8_ETC2;
case SG_PIXELFORMAT_ETC2_RGB8A1: return GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2;
case SG_PIXELFORMAT_ETC2_RGBA8: return GL_COMPRESSED_RGBA8_ETC2_EAC;
case SG_PIXELFORMAT_ETC2_RG11: return GL_COMPRESSED_RG11_EAC;
case SG_PIXELFORMAT_ETC2_RG11SN: return GL_COMPRESSED_SIGNED_RG11_EAC;
default: SOKOL_UNREACHABLE; return 0;
}
}
#endif
}
_SOKOL_PRIVATE GLenum _sg_gl_cubeface_target(int face_index) {
switch (face_index) {
case 0: return GL_TEXTURE_CUBE_MAP_POSITIVE_X;
case 1: return GL_TEXTURE_CUBE_MAP_NEGATIVE_X;
case 2: return GL_TEXTURE_CUBE_MAP_POSITIVE_Y;
case 3: return GL_TEXTURE_CUBE_MAP_NEGATIVE_Y;
case 4: return GL_TEXTURE_CUBE_MAP_POSITIVE_Z;
case 5: return GL_TEXTURE_CUBE_MAP_NEGATIVE_Z;
default: SOKOL_UNREACHABLE; return 0;
}
}
_SOKOL_PRIVATE GLenum _sg_gl_depth_attachment_format(sg_pixel_format fmt) {
switch (fmt) {
case SG_PIXELFORMAT_DEPTH: return GL_DEPTH_COMPONENT16;
case SG_PIXELFORMAT_DEPTH_STENCIL: return GL_DEPTH24_STENCIL8;
default: SOKOL_UNREACHABLE; return 0;
}
}
/* see: https://www.khronos.org/registry/OpenGL-Refpages/es3.0/html/glTexImage2D.xhtml */
_SOKOL_PRIVATE void _sg_gl_init_pixelformats(bool has_bgra) {
#if !defined(SOKOL_GLES2)
if (!_sg.gl.gles2) {
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_R8]);
}
else {
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_R8]);
}
#else
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_R8]);
#endif
#if !defined(SOKOL_GLES2)
if (!_sg.gl.gles2) {
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_R8SN]);
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_R8UI]);
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_R8SI]);
#if !defined(SOKOL_GLES3)
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_R16]);
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_R16SN]);
#endif
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_R16UI]);
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_R16SI]);
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RG8]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_RG8SN]);
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RG8UI]);
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RG8SI]);
_sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_R32UI]);
_sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_R32SI]);
#if !defined(SOKOL_GLES3)
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RG16]);
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RG16SN]);
#endif
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RG16UI]);
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RG16SI]);
}
#endif
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RGBA8]);
#if !defined(SOKOL_GLES2)
if (!_sg.gl.gles2) {
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_RGBA8SN]);
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RGBA8UI]);
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RGBA8SI]);
}
#endif
if (has_bgra) {
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_BGRA8]);
}
#if !defined(SOKOL_GLES2)
if (!_sg.gl.gles2) {
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RGB10A2]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_RG11B10F]);
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RG32UI]);
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RG32SI]);
#if !defined(SOKOL_GLES3)
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RGBA16]);
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RGBA16SN]);
#endif
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RGBA16UI]);
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RGBA16SI]);
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RGBA32UI]);
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RGBA32SI]);
}
#endif
// FIXME: WEBGL_depth_texture extension?
_sg_pixelformat_srmd(&_sg.formats[SG_PIXELFORMAT_DEPTH]);
_sg_pixelformat_srmd(&_sg.formats[SG_PIXELFORMAT_DEPTH_STENCIL]);
}
/* FIXME: OES_half_float_blend */
_SOKOL_PRIVATE void _sg_gl_init_pixelformats_half_float(bool has_colorbuffer_half_float, bool has_texture_half_float_linear) {
#if !defined(SOKOL_GLES2)
if (!_sg.gl.gles2) {
if (has_texture_half_float_linear) {
if (has_colorbuffer_half_float) {
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_R16F]);
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RG16F]);
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RGBA16F]);
}
else {
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_R16F]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_RG16F]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_RGBA16F]);
}
}
else {
if (has_colorbuffer_half_float) {
_sg_pixelformat_sbrm(&_sg.formats[SG_PIXELFORMAT_R16F]);
_sg_pixelformat_sbrm(&_sg.formats[SG_PIXELFORMAT_RG16F]);
_sg_pixelformat_sbrm(&_sg.formats[SG_PIXELFORMAT_RGBA16F]);
}
else {
_sg_pixelformat_s(&_sg.formats[SG_PIXELFORMAT_R16F]);
_sg_pixelformat_s(&_sg.formats[SG_PIXELFORMAT_RG16F]);
_sg_pixelformat_s(&_sg.formats[SG_PIXELFORMAT_RGBA16F]);
}
}
}
else {
#endif
/* GLES2 can only render to RGBA, and there's no RG format */
if (has_texture_half_float_linear) {
if (has_colorbuffer_half_float) {
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RGBA16F]);
}
else {
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_RGBA16F]);
}
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_R16F]);
}
else {
if (has_colorbuffer_half_float) {
_sg_pixelformat_sbrm(&_sg.formats[SG_PIXELFORMAT_RGBA16F]);
}
else {
_sg_pixelformat_s(&_sg.formats[SG_PIXELFORMAT_RGBA16F]);
}
_sg_pixelformat_s(&_sg.formats[SG_PIXELFORMAT_R16F]);
}
#if !defined(SOKOL_GLES2)
}
#endif
}
_SOKOL_PRIVATE void _sg_gl_init_pixelformats_float(bool has_colorbuffer_float, bool has_texture_float_linear, bool has_float_blend) {
#if !defined(SOKOL_GLES2)
if (!_sg.gl.gles2) {
if (has_texture_float_linear) {
if (has_colorbuffer_float) {
if (has_float_blend) {
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_R32F]);
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RG32F]);
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RGBA32F]);
}
else {
_sg_pixelformat_sfrm(&_sg.formats[SG_PIXELFORMAT_R32F]);
_sg_pixelformat_sfrm(&_sg.formats[SG_PIXELFORMAT_RG32F]);
_sg_pixelformat_sfrm(&_sg.formats[SG_PIXELFORMAT_RGBA32F]);
}
}
else {
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_R32F]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_RG32F]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_RGBA32F]);
}
}
else {
if (has_colorbuffer_float) {
_sg_pixelformat_sbrm(&_sg.formats[SG_PIXELFORMAT_R32F]);
_sg_pixelformat_sbrm(&_sg.formats[SG_PIXELFORMAT_RG32F]);
_sg_pixelformat_sbrm(&_sg.formats[SG_PIXELFORMAT_RGBA32F]);
}
else {
_sg_pixelformat_s(&_sg.formats[SG_PIXELFORMAT_R32F]);
_sg_pixelformat_s(&_sg.formats[SG_PIXELFORMAT_RG32F]);
_sg_pixelformat_s(&_sg.formats[SG_PIXELFORMAT_RGBA32F]);
}
}
}
else {
#endif
/* GLES2 can only render to RGBA, and there's no RG format */
if (has_texture_float_linear) {
if (has_colorbuffer_float) {
if (has_float_blend) {
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RGBA32F]);
}
else {
_sg_pixelformat_sfrm(&_sg.formats[SG_PIXELFORMAT_RGBA32F]);
}
}
else {
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_RGBA32F]);
}
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_R32F]);
}
else {
if (has_colorbuffer_float) {
_sg_pixelformat_sbrm(&_sg.formats[SG_PIXELFORMAT_RGBA32F]);
}
else {
_sg_pixelformat_s(&_sg.formats[SG_PIXELFORMAT_RGBA32F]);
}
_sg_pixelformat_s(&_sg.formats[SG_PIXELFORMAT_R32F]);
}
#if !defined(SOKOL_GLES2)
}
#endif
}
_SOKOL_PRIVATE void _sg_gl_init_pixelformats_s3tc(void) {
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC1_RGBA]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC2_RGBA]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC3_RGBA]);
}
_SOKOL_PRIVATE void _sg_gl_init_pixelformats_rgtc(void) {
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC4_R]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC4_RSN]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC5_RG]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC5_RGSN]);
}
_SOKOL_PRIVATE void _sg_gl_init_pixelformats_bptc(void) {
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC6H_RGBF]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC6H_RGBUF]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC7_RGBA]);
}
_SOKOL_PRIVATE void _sg_gl_init_pixelformats_pvrtc(void) {
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_PVRTC_RGB_2BPP]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_PVRTC_RGB_4BPP]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_PVRTC_RGBA_2BPP]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_PVRTC_RGBA_4BPP]);
}
_SOKOL_PRIVATE void _sg_gl_init_pixelformats_etc2(void) {
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_ETC2_RGB8]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_ETC2_RGB8A1]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_ETC2_RGBA8]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_ETC2_RG11]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_ETC2_RG11SN]);
}
_SOKOL_PRIVATE void _sg_gl_init_limits(void) {
_SG_GL_CHECK_ERROR();
GLint gl_int;
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &gl_int);
_SG_GL_CHECK_ERROR();
_sg.limits.max_image_size_2d = gl_int;
_sg.limits.max_image_size_array = gl_int;
glGetIntegerv(GL_MAX_CUBE_MAP_TEXTURE_SIZE, &gl_int);
_SG_GL_CHECK_ERROR();
_sg.limits.max_image_size_cube = gl_int;
glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &gl_int);
_SG_GL_CHECK_ERROR();
if (gl_int > SG_MAX_VERTEX_ATTRIBUTES) {
gl_int = SG_MAX_VERTEX_ATTRIBUTES;
}
_sg.limits.max_vertex_attrs = gl_int;
#if !defined(SOKOL_GLES2)
if (!_sg.gl.gles2) {
glGetIntegerv(GL_MAX_3D_TEXTURE_SIZE, &gl_int);
_SG_GL_CHECK_ERROR();
_sg.limits.max_image_size_3d = gl_int;
glGetIntegerv(GL_MAX_ARRAY_TEXTURE_LAYERS, &gl_int);
_SG_GL_CHECK_ERROR();
_sg.limits.max_image_array_layers = gl_int;
}
#endif
if (_sg.gl.ext_anisotropic) {
glGetIntegerv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &gl_int);
_SG_GL_CHECK_ERROR();
_sg.gl.max_anisotropy = gl_int;
}
else {
_sg.gl.max_anisotropy = 1;
}
glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &gl_int);
_SG_GL_CHECK_ERROR();
_sg.gl.max_combined_texture_image_units = gl_int;
}
#if defined(SOKOL_GLCORE33)
_SOKOL_PRIVATE void _sg_gl_init_caps_glcore33(void) {
_sg.backend = SG_BACKEND_GLCORE33;
_sg.features.origin_top_left = false;
_sg.features.instancing = true;
_sg.features.multiple_render_targets = true;
_sg.features.msaa_render_targets = true;
_sg.features.imagetype_3d = true;
_sg.features.imagetype_array = true;
_sg.features.image_clamp_to_border = true;
_sg.features.mrt_independent_blend_state = false;
_sg.features.mrt_independent_write_mask = true;
/* scan extensions */
bool has_s3tc = false; /* BC1..BC3 */
bool has_rgtc = false; /* BC4 and BC5 */
bool has_bptc = false; /* BC6H and BC7 */
bool has_pvrtc = false;
bool has_etc2 = false;
GLint num_ext = 0;
glGetIntegerv(GL_NUM_EXTENSIONS, &num_ext);
for (int i = 0; i < num_ext; i++) {
const char* ext = (const char*) glGetStringi(GL_EXTENSIONS, (GLuint)i);
if (ext) {
if (strstr(ext, "_texture_compression_s3tc")) {
has_s3tc = true;
}
else if (strstr(ext, "_texture_compression_rgtc")) {
has_rgtc = true;
}
else if (strstr(ext, "_texture_compression_bptc")) {
has_bptc = true;
}
else if (strstr(ext, "_texture_compression_pvrtc")) {
has_pvrtc = true;
}
else if (strstr(ext, "_ES3_compatibility")) {
has_etc2 = true;
}
else if (strstr(ext, "_texture_filter_anisotropic")) {
_sg.gl.ext_anisotropic = true;
}
}
}
/* limits */
_sg_gl_init_limits();
/* pixel formats */
const bool has_bgra = false; /* not a bug */
const bool has_colorbuffer_float = true;
const bool has_colorbuffer_half_float = true;
const bool has_texture_float_linear = true; /* FIXME??? */
const bool has_texture_half_float_linear = true;
const bool has_float_blend = true;
_sg_gl_init_pixelformats(has_bgra);
_sg_gl_init_pixelformats_float(has_colorbuffer_float, has_texture_float_linear, has_float_blend);
_sg_gl_init_pixelformats_half_float(has_colorbuffer_half_float, has_texture_half_float_linear);
if (has_s3tc) {
_sg_gl_init_pixelformats_s3tc();
}
if (has_rgtc) {
_sg_gl_init_pixelformats_rgtc();
}
if (has_bptc) {
_sg_gl_init_pixelformats_bptc();
}
if (has_pvrtc) {
_sg_gl_init_pixelformats_pvrtc();
}
if (has_etc2) {
_sg_gl_init_pixelformats_etc2();
}
}
#endif
#if defined(SOKOL_GLES3)
_SOKOL_PRIVATE void _sg_gl_init_caps_gles3(void) {
_sg.backend = SG_BACKEND_GLES3;
_sg.features.origin_top_left = false;
_sg.features.instancing = true;
_sg.features.multiple_render_targets = true;
_sg.features.msaa_render_targets = true;
_sg.features.imagetype_3d = true;
_sg.features.imagetype_array = true;
_sg.features.image_clamp_to_border = false;
_sg.features.mrt_independent_blend_state = false;
_sg.features.mrt_independent_write_mask = false;
bool has_s3tc = false; /* BC1..BC3 */
bool has_rgtc = false; /* BC4 and BC5 */
bool has_bptc = false; /* BC6H and BC7 */
bool has_pvrtc = false;
#if defined(__EMSCRIPTEN__)
bool has_etc2 = false;
#else
bool has_etc2 = true;
#endif
bool has_colorbuffer_float = false;
bool has_colorbuffer_half_float = false;
bool has_texture_float_linear = false;
bool has_float_blend = false;
GLint num_ext = 0;
glGetIntegerv(GL_NUM_EXTENSIONS, &num_ext);
for (int i = 0; i < num_ext; i++) {
const char* ext = (const char*) glGetStringi(GL_EXTENSIONS, (GLuint)i);
if (ext) {
if (strstr(ext, "_texture_compression_s3tc")) {
has_s3tc = true;
}
else if (strstr(ext, "_compressed_texture_s3tc")) {
has_s3tc = true;
}
else if (strstr(ext, "_texture_compression_rgtc")) {
has_rgtc = true;
}
else if (strstr(ext, "_texture_compression_bptc")) {
has_bptc = true;
}
else if (strstr(ext, "_texture_compression_pvrtc")) {
has_pvrtc = true;
}
else if (strstr(ext, "_compressed_texture_pvrtc")) {
has_pvrtc = true;
}
else if (strstr(ext, "_compressed_texture_etc")) {
has_etc2 = true;
}
else if (strstr(ext, "_color_buffer_float")) {
has_colorbuffer_float = true;
}
else if (strstr(ext, "_color_buffer_half_float")) {
has_colorbuffer_half_float = true;
}
else if (strstr(ext, "_texture_float_linear")) {
has_texture_float_linear = true;
}
else if (strstr(ext, "_float_blend")) {
has_float_blend = true;
}
else if (strstr(ext, "_texture_filter_anisotropic")) {
_sg.gl.ext_anisotropic = true;
}
}
}
/* on WebGL2, color_buffer_float also includes 16-bit formats
see: https://developer.mozilla.org/en-US/docs/Web/API/EXT_color_buffer_float
*/
#if defined(__EMSCRIPTEN__)
has_colorbuffer_half_float = has_colorbuffer_float;
#endif
/* limits */
_sg_gl_init_limits();
/* pixel formats */
const bool has_texture_half_float_linear = true;
const bool has_bgra = false; /* not a bug */
_sg_gl_init_pixelformats(has_bgra);
_sg_gl_init_pixelformats_float(has_colorbuffer_float, has_texture_float_linear, has_float_blend);
_sg_gl_init_pixelformats_half_float(has_colorbuffer_half_float, has_texture_half_float_linear);
if (has_s3tc) {
_sg_gl_init_pixelformats_s3tc();
}
if (has_rgtc) {
_sg_gl_init_pixelformats_rgtc();
}
if (has_bptc) {
_sg_gl_init_pixelformats_bptc();
}
if (has_pvrtc) {
_sg_gl_init_pixelformats_pvrtc();
}
if (has_etc2) {
_sg_gl_init_pixelformats_etc2();
}
}
#endif
#if defined(SOKOL_GLES3) || defined(SOKOL_GLES2)
_SOKOL_PRIVATE void _sg_gl_init_caps_gles2(void) {
_sg.backend = SG_BACKEND_GLES2;
bool has_s3tc = false; /* BC1..BC3 */
bool has_rgtc = false; /* BC4 and BC5 */
bool has_bptc = false; /* BC6H and BC7 */
bool has_pvrtc = false;
bool has_etc2 = false;
bool has_texture_float = false;
bool has_texture_float_linear = false;
bool has_colorbuffer_float = false;
bool has_float_blend = false;
bool has_instancing = false;
const char* ext = (const char*) glGetString(GL_EXTENSIONS);
if (ext) {
has_s3tc = strstr(ext, "_texture_compression_s3tc") || strstr(ext, "_compressed_texture_s3tc");
has_rgtc = strstr(ext, "_texture_compression_rgtc");
has_bptc = strstr(ext, "_texture_compression_bptc");
has_pvrtc = strstr(ext, "_texture_compression_pvrtc") || strstr(ext, "_compressed_texture_pvrtc");
has_etc2 = strstr(ext, "_compressed_texture_etc");
has_texture_float = strstr(ext, "_texture_float");
has_texture_float_linear = strstr(ext, "_texture_float_linear");
has_colorbuffer_float = strstr(ext, "_color_buffer_float");
has_float_blend = strstr(ext, "_float_blend");
/* don't bother with half_float support on WebGL1
has_texture_half_float = strstr(ext, "_texture_half_float");
has_texture_half_float_linear = strstr(ext, "_texture_half_float_linear");
has_colorbuffer_half_float = strstr(ext, "_color_buffer_half_float");
*/
has_instancing = strstr(ext, "_instanced_arrays");
_sg.gl.ext_anisotropic = strstr(ext, "ext_anisotropic");
}
_sg.features.origin_top_left = false;
#if defined(_SOKOL_GL_INSTANCING_ENABLED)
_sg.features.instancing = has_instancing;
#endif
_sg.features.multiple_render_targets = false;
_sg.features.msaa_render_targets = false;
_sg.features.imagetype_3d = false;
_sg.features.imagetype_array = false;
_sg.features.image_clamp_to_border = false;
_sg.features.mrt_independent_blend_state = false;
_sg.features.mrt_independent_write_mask = false;
/* limits */
_sg_gl_init_limits();
/* pixel formats */
const bool has_bgra = false; /* not a bug */
const bool has_texture_half_float = false;
const bool has_texture_half_float_linear = false;
const bool has_colorbuffer_half_float = false;
_sg_gl_init_pixelformats(has_bgra);
if (has_texture_float) {
_sg_gl_init_pixelformats_float(has_colorbuffer_float, has_texture_float_linear, has_float_blend);
}
if (has_texture_half_float) {
_sg_gl_init_pixelformats_half_float(has_colorbuffer_half_float, has_texture_half_float_linear);
}
if (has_s3tc) {
_sg_gl_init_pixelformats_s3tc();
}
if (has_rgtc) {
_sg_gl_init_pixelformats_rgtc();
}
if (has_bptc) {
_sg_gl_init_pixelformats_bptc();
}
if (has_pvrtc) {
_sg_gl_init_pixelformats_pvrtc();
}
if (has_etc2) {
_sg_gl_init_pixelformats_etc2();
}
/* GLES2 doesn't allow multi-sampled render targets at all */
for (int i = 0; i < _SG_PIXELFORMAT_NUM; i++) {
_sg.formats[i].msaa = false;
}
}
#endif
/*-- state cache implementation ----------------------------------------------*/
_SOKOL_PRIVATE void _sg_gl_cache_clear_buffer_bindings(bool force) {
if (force || (_sg.gl.cache.vertex_buffer != 0)) {
glBindBuffer(GL_ARRAY_BUFFER, 0);
_sg.gl.cache.vertex_buffer = 0;
}
if (force || (_sg.gl.cache.index_buffer != 0)) {
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
_sg.gl.cache.index_buffer = 0;
}
}
_SOKOL_PRIVATE void _sg_gl_cache_bind_buffer(GLenum target, GLuint buffer) {
SOKOL_ASSERT((GL_ARRAY_BUFFER == target) || (GL_ELEMENT_ARRAY_BUFFER == target));
if (target == GL_ARRAY_BUFFER) {
if (_sg.gl.cache.vertex_buffer != buffer) {
_sg.gl.cache.vertex_buffer = buffer;
glBindBuffer(target, buffer);
}
}
else {
if (_sg.gl.cache.index_buffer != buffer) {
_sg.gl.cache.index_buffer = buffer;
glBindBuffer(target, buffer);
}
}
}
_SOKOL_PRIVATE void _sg_gl_cache_store_buffer_binding(GLenum target) {
if (target == GL_ARRAY_BUFFER) {
_sg.gl.cache.stored_vertex_buffer = _sg.gl.cache.vertex_buffer;
}
else {
_sg.gl.cache.stored_index_buffer = _sg.gl.cache.index_buffer;
}
}
_SOKOL_PRIVATE void _sg_gl_cache_restore_buffer_binding(GLenum target) {
if (target == GL_ARRAY_BUFFER) {
if (_sg.gl.cache.stored_vertex_buffer != 0) {
/* we only care restoring valid ids */
_sg_gl_cache_bind_buffer(target, _sg.gl.cache.stored_vertex_buffer);
_sg.gl.cache.stored_vertex_buffer = 0;
}
}
else {
if (_sg.gl.cache.stored_index_buffer != 0) {
/* we only care restoring valid ids */
_sg_gl_cache_bind_buffer(target, _sg.gl.cache.stored_index_buffer);
_sg.gl.cache.stored_index_buffer = 0;
}
}
}
/* called when from _sg_gl_destroy_buffer() */
_SOKOL_PRIVATE void _sg_gl_cache_invalidate_buffer(GLuint buf) {
if (buf == _sg.gl.cache.vertex_buffer) {
_sg.gl.cache.vertex_buffer = 0;
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
if (buf == _sg.gl.cache.index_buffer) {
_sg.gl.cache.index_buffer = 0;
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
if (buf == _sg.gl.cache.stored_vertex_buffer) {
_sg.gl.cache.stored_vertex_buffer = 0;
}
if (buf == _sg.gl.cache.stored_index_buffer) {
_sg.gl.cache.stored_index_buffer = 0;
}
for (int i = 0; i < SG_MAX_VERTEX_ATTRIBUTES; i++) {
if (buf == _sg.gl.cache.attrs[i].gl_vbuf) {
_sg.gl.cache.attrs[i].gl_vbuf = 0;
}
}
}
_SOKOL_PRIVATE void _sg_gl_cache_active_texture(GLenum texture) {
if (_sg.gl.cache.cur_active_texture != texture) {
_sg.gl.cache.cur_active_texture = texture;
glActiveTexture(texture);
}
}
_SOKOL_PRIVATE void _sg_gl_cache_clear_texture_bindings(bool force) {
for (int i = 0; (i < SG_MAX_SHADERSTAGE_IMAGES) && (i < _sg.gl.max_combined_texture_image_units); i++) {
if (force || (_sg.gl.cache.textures[i].texture != 0)) {
GLenum gl_texture_slot = (GLenum) (GL_TEXTURE0 + i);
glActiveTexture(gl_texture_slot);
glBindTexture(GL_TEXTURE_2D, 0);
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
#if !defined(SOKOL_GLES2)
if (!_sg.gl.gles2) {
glBindTexture(GL_TEXTURE_3D, 0);
glBindTexture(GL_TEXTURE_2D_ARRAY, 0);
}
#endif
_sg.gl.cache.textures[i].target = 0;
_sg.gl.cache.textures[i].texture = 0;
_sg.gl.cache.cur_active_texture = gl_texture_slot;
}
}
}
_SOKOL_PRIVATE void _sg_gl_cache_bind_texture(int slot_index, GLenum target, GLuint texture) {
/* it's valid to call this function with target=0 and/or texture=0
target=0 will unbind the previous binding, texture=0 will clear
the new binding
*/
SOKOL_ASSERT(slot_index < SG_MAX_SHADERSTAGE_IMAGES);
if (slot_index >= _sg.gl.max_combined_texture_image_units) {
return;
}
_sg_gl_texture_bind_slot* slot = &_sg.gl.cache.textures[slot_index];
if ((slot->target != target) || (slot->texture != texture)) {
_sg_gl_cache_active_texture((GLenum)(GL_TEXTURE0 + slot_index));
/* if the target has changed, clear the previous binding on that target */
if ((target != slot->target) && (slot->target != 0)) {
glBindTexture(slot->target, 0);
}
/* apply new binding (texture can be 0 to unbind) */
if (target != 0) {
glBindTexture(target, texture);
}
slot->target = target;
slot->texture = texture;
}
}
_SOKOL_PRIVATE void _sg_gl_cache_store_texture_binding(int slot_index) {
SOKOL_ASSERT(slot_index < SG_MAX_SHADERSTAGE_IMAGES);
_sg.gl.cache.stored_texture = _sg.gl.cache.textures[slot_index];
}
_SOKOL_PRIVATE void _sg_gl_cache_restore_texture_binding(int slot_index) {
SOKOL_ASSERT(slot_index < SG_MAX_SHADERSTAGE_IMAGES);
_sg_gl_texture_bind_slot* slot = &_sg.gl.cache.stored_texture;
if (slot->texture != 0) {
/* we only care restoring valid ids */
SOKOL_ASSERT(slot->target != 0);
_sg_gl_cache_bind_texture(slot_index, slot->target, slot->texture);
slot->target = 0;
slot->texture = 0;
}
}
/* called from _sg_gl_destroy_texture() */
_SOKOL_PRIVATE void _sg_gl_cache_invalidate_texture(GLuint tex) {
for (int i = 0; i < SG_MAX_SHADERSTAGE_IMAGES; i++) {
_sg_gl_texture_bind_slot* slot = &_sg.gl.cache.textures[i];
if (tex == slot->texture) {
_sg_gl_cache_active_texture((GLenum)(GL_TEXTURE0 + i));
glBindTexture(slot->target, 0);
slot->target = 0;
slot->texture = 0;
}
}
if (tex == _sg.gl.cache.stored_texture.texture) {
_sg.gl.cache.stored_texture.target = 0;
_sg.gl.cache.stored_texture.texture = 0;
}
}
/* called from _sg_gl_destroy_shader() */
_SOKOL_PRIVATE void _sg_gl_cache_invalidate_program(GLuint prog) {
if (prog == _sg.gl.cache.prog) {
_sg.gl.cache.prog = 0;
glUseProgram(0);
}
}
_SOKOL_PRIVATE void _sg_gl_reset_state_cache(void) {
if (_sg.gl.cur_context) {
_SG_GL_CHECK_ERROR();
#if !defined(SOKOL_GLES2)
if (!_sg.gl.gles2) {
glBindVertexArray(_sg.gl.cur_context->vao);
_SG_GL_CHECK_ERROR();
}
#endif
memset(&_sg.gl.cache, 0, sizeof(_sg.gl.cache));
_sg_gl_cache_clear_buffer_bindings(true);
_SG_GL_CHECK_ERROR();
_sg_gl_cache_clear_texture_bindings(true);
_SG_GL_CHECK_ERROR();
for (int i = 0; i < _sg.limits.max_vertex_attrs; i++) {
_sg_gl_attr_t* attr = &_sg.gl.cache.attrs[i].gl_attr;
attr->vb_index = -1;
attr->divisor = -1;
glDisableVertexAttribArray((GLuint)i);
_SG_GL_CHECK_ERROR();
}
_sg.gl.cache.cur_primitive_type = GL_TRIANGLES;
/* shader program */
glGetIntegerv(GL_CURRENT_PROGRAM, (GLint*)&_sg.gl.cache.prog);
_SG_GL_CHECK_ERROR();
/* depth and stencil state */
_sg.gl.cache.depth.compare = SG_COMPAREFUNC_ALWAYS;
_sg.gl.cache.stencil.front.compare = SG_COMPAREFUNC_ALWAYS;
_sg.gl.cache.stencil.front.fail_op = SG_STENCILOP_KEEP;
_sg.gl.cache.stencil.front.depth_fail_op = SG_STENCILOP_KEEP;
_sg.gl.cache.stencil.front.pass_op = SG_STENCILOP_KEEP;
_sg.gl.cache.stencil.back.compare = SG_COMPAREFUNC_ALWAYS;
_sg.gl.cache.stencil.back.fail_op = SG_STENCILOP_KEEP;
_sg.gl.cache.stencil.back.depth_fail_op = SG_STENCILOP_KEEP;
_sg.gl.cache.stencil.back.pass_op = SG_STENCILOP_KEEP;
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_ALWAYS);
glDepthMask(GL_FALSE);
glDisable(GL_STENCIL_TEST);
glStencilFunc(GL_ALWAYS, 0, 0);
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
glStencilMask(0);
/* blend state */
_sg.gl.cache.blend.src_factor_rgb = SG_BLENDFACTOR_ONE;
_sg.gl.cache.blend.dst_factor_rgb = SG_BLENDFACTOR_ZERO;
_sg.gl.cache.blend.op_rgb = SG_BLENDOP_ADD;
_sg.gl.cache.blend.src_factor_alpha = SG_BLENDFACTOR_ONE;
_sg.gl.cache.blend.dst_factor_alpha = SG_BLENDFACTOR_ZERO;
_sg.gl.cache.blend.op_alpha = SG_BLENDOP_ADD;
glDisable(GL_BLEND);
glBlendFuncSeparate(GL_ONE, GL_ZERO, GL_ONE, GL_ZERO);
glBlendEquationSeparate(GL_FUNC_ADD, GL_FUNC_ADD);
glBlendColor(0.0f, 0.0f, 0.0f, 0.0f);
/* standalone state */
for (int i = 0; i < SG_MAX_COLOR_ATTACHMENTS; i++) {
_sg.gl.cache.color_write_mask[i] = SG_COLORMASK_RGBA;
}
_sg.gl.cache.cull_mode = SG_CULLMODE_NONE;
_sg.gl.cache.face_winding = SG_FACEWINDING_CW;
_sg.gl.cache.sample_count = 1;
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glPolygonOffset(0.0f, 0.0f);
glDisable(GL_POLYGON_OFFSET_FILL);
glDisable(GL_CULL_FACE);
glFrontFace(GL_CW);
glCullFace(GL_BACK);
glEnable(GL_SCISSOR_TEST);
glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE);
glEnable(GL_DITHER);
glDisable(GL_POLYGON_OFFSET_FILL);
#if defined(SOKOL_GLCORE33)
glEnable(GL_MULTISAMPLE);
glEnable(GL_PROGRAM_POINT_SIZE);
#endif
}
}
_SOKOL_PRIVATE void _sg_gl_setup_backend(const sg_desc* desc) {
/* assumes that _sg.gl is already zero-initialized */
_sg.gl.valid = true;
#if defined(SOKOL_GLES2) || defined(SOKOL_GLES3)
_sg.gl.gles2 = desc->context.gl.force_gles2;
#else
_SOKOL_UNUSED(desc);
_sg.gl.gles2 = false;
#endif
#if defined(_SOKOL_USE_WIN32_GL_LOADER)
_sg_gl_load_opengl();
#endif
/* clear initial GL error state */
#if defined(SOKOL_DEBUG)
while (glGetError() != GL_NO_ERROR);
#endif
#if defined(SOKOL_GLCORE33)
_sg_gl_init_caps_glcore33();
#elif defined(SOKOL_GLES3)
if (_sg.gl.gles2) {
_sg_gl_init_caps_gles2();
}
else {
_sg_gl_init_caps_gles3();
}
#else
_sg_gl_init_caps_gles2();
#endif
}
_SOKOL_PRIVATE void _sg_gl_discard_backend(void) {
SOKOL_ASSERT(_sg.gl.valid);
_sg.gl.valid = false;
#if defined(_SOKOL_USE_WIN32_GL_LOADER)
_sg_gl_unload_opengl();
#endif
}
_SOKOL_PRIVATE void _sg_gl_activate_context(_sg_context_t* ctx) {
SOKOL_ASSERT(_sg.gl.valid);
/* NOTE: ctx can be 0 to unset the current context */
_sg.gl.cur_context = ctx;
_sg_gl_reset_state_cache();
}
/*-- GL backend resource creation and destruction ----------------------------*/
_SOKOL_PRIVATE sg_resource_state _sg_gl_create_context(_sg_context_t* ctx) {
SOKOL_ASSERT(ctx);
SOKOL_ASSERT(0 == ctx->default_framebuffer);
_SG_GL_CHECK_ERROR();
glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*)&ctx->default_framebuffer);
_SG_GL_CHECK_ERROR();
#if !defined(SOKOL_GLES2)
if (!_sg.gl.gles2) {
SOKOL_ASSERT(0 == ctx->vao);
glGenVertexArrays(1, &ctx->vao);
glBindVertexArray(ctx->vao);
_SG_GL_CHECK_ERROR();
}
#endif
return SG_RESOURCESTATE_VALID;
}
_SOKOL_PRIVATE void _sg_gl_destroy_context(_sg_context_t* ctx) {
SOKOL_ASSERT(ctx);
#if !defined(SOKOL_GLES2)
if (!_sg.gl.gles2) {
if (ctx->vao) {
glDeleteVertexArrays(1, &ctx->vao);
}
_SG_GL_CHECK_ERROR();
}
#else
_SOKOL_UNUSED(ctx);
#endif
}
_SOKOL_PRIVATE sg_resource_state _sg_gl_create_buffer(_sg_buffer_t* buf, const sg_buffer_desc* desc) {
SOKOL_ASSERT(buf && desc);
_SG_GL_CHECK_ERROR();
_sg_buffer_common_init(&buf->cmn, desc);
buf->gl.ext_buffers = (0 != desc->gl_buffers[0]);
GLenum gl_target = _sg_gl_buffer_target(buf->cmn.type);
GLenum gl_usage = _sg_gl_usage(buf->cmn.usage);
for (int slot = 0; slot < buf->cmn.num_slots; slot++) {
GLuint gl_buf = 0;
if (buf->gl.ext_buffers) {
SOKOL_ASSERT(desc->gl_buffers[slot]);
gl_buf = desc->gl_buffers[slot];
}
else {
glGenBuffers(1, &gl_buf);
_sg_gl_cache_store_buffer_binding(gl_target);
_sg_gl_cache_bind_buffer(gl_target, gl_buf);
glBufferData(gl_target, buf->cmn.size, 0, gl_usage);
if (buf->cmn.usage == SG_USAGE_IMMUTABLE) {
SOKOL_ASSERT(desc->data.ptr);
glBufferSubData(gl_target, 0, buf->cmn.size, desc->data.ptr);
}
_sg_gl_cache_restore_buffer_binding(gl_target);
}
buf->gl.buf[slot] = gl_buf;
}
_SG_GL_CHECK_ERROR();
return SG_RESOURCESTATE_VALID;
}
_SOKOL_PRIVATE void _sg_gl_destroy_buffer(_sg_buffer_t* buf) {
SOKOL_ASSERT(buf);
_SG_GL_CHECK_ERROR();
for (int slot = 0; slot < buf->cmn.num_slots; slot++) {
if (buf->gl.buf[slot]) {
_sg_gl_cache_invalidate_buffer(buf->gl.buf[slot]);
if (!buf->gl.ext_buffers) {
glDeleteBuffers(1, &buf->gl.buf[slot]);
}
}
}
_SG_GL_CHECK_ERROR();
}
_SOKOL_PRIVATE bool _sg_gl_supported_texture_format(sg_pixel_format fmt) {
const int fmt_index = (int) fmt;
SOKOL_ASSERT((fmt_index > SG_PIXELFORMAT_NONE) && (fmt_index < _SG_PIXELFORMAT_NUM));
return _sg.formats[fmt_index].sample;
}
_SOKOL_PRIVATE sg_resource_state _sg_gl_create_image(_sg_image_t* img, const sg_image_desc* desc) {
SOKOL_ASSERT(img && desc);
_SG_GL_CHECK_ERROR();
_sg_image_common_init(&img->cmn, desc);
img->gl.ext_textures = (0 != desc->gl_textures[0]);
/* check if texture format is support */
if (!_sg_gl_supported_texture_format(img->cmn.pixel_format)) {
SOKOL_LOG("texture format not supported by GL context\n");
return SG_RESOURCESTATE_FAILED;
}
/* check for optional texture types */
if ((img->cmn.type == SG_IMAGETYPE_3D) && !_sg.features.imagetype_3d) {
SOKOL_LOG("3D textures not supported by GL context\n");
return SG_RESOURCESTATE_FAILED;
}
if ((img->cmn.type == SG_IMAGETYPE_ARRAY) && !_sg.features.imagetype_array) {
SOKOL_LOG("array textures not supported by GL context\n");
return SG_RESOURCESTATE_FAILED;
}
#if !defined(SOKOL_GLES2)
bool msaa = false;
if (!_sg.gl.gles2) {
msaa = (img->cmn.sample_count > 1) && (_sg.features.msaa_render_targets);
}
#endif
if (_sg_is_valid_rendertarget_depth_format(img->cmn.pixel_format)) {
/* special case depth-stencil-buffer? */
SOKOL_ASSERT((img->cmn.usage == SG_USAGE_IMMUTABLE) && (img->cmn.num_slots == 1));
SOKOL_ASSERT(!img->gl.ext_textures); /* cannot provide external texture for depth images */
glGenRenderbuffers(1, &img->gl.depth_render_buffer);
glBindRenderbuffer(GL_RENDERBUFFER, img->gl.depth_render_buffer);
GLenum gl_depth_format = _sg_gl_depth_attachment_format(img->cmn.pixel_format);
#if !defined(SOKOL_GLES2)
if (!_sg.gl.gles2 && msaa) {
glRenderbufferStorageMultisample(GL_RENDERBUFFER, img->cmn.sample_count, gl_depth_format, img->cmn.width, img->cmn.height);
}
else
#endif
{
glRenderbufferStorage(GL_RENDERBUFFER, gl_depth_format, img->cmn.width, img->cmn.height);
}
}
else {
/* regular color texture */
img->gl.target = _sg_gl_texture_target(img->cmn.type);
const GLenum gl_internal_format = _sg_gl_teximage_internal_format(img->cmn.pixel_format);
/* if this is a MSAA render target, need to create a separate render buffer */
#if !defined(SOKOL_GLES2)
if (!_sg.gl.gles2 && img->cmn.render_target && msaa) {
glGenRenderbuffers(1, &img->gl.msaa_render_buffer);
glBindRenderbuffer(GL_RENDERBUFFER, img->gl.msaa_render_buffer);
glRenderbufferStorageMultisample(GL_RENDERBUFFER, img->cmn.sample_count, gl_internal_format, img->cmn.width, img->cmn.height);
}
#endif
if (img->gl.ext_textures) {
/* inject externally GL textures */
for (int slot = 0; slot < img->cmn.num_slots; slot++) {
SOKOL_ASSERT(desc->gl_textures[slot]);
img->gl.tex[slot] = desc->gl_textures[slot];
}
if (desc->gl_texture_target) {
img->gl.target = (GLenum)desc->gl_texture_target;
}
}
else {
/* create our own GL texture(s) */
const GLenum gl_format = _sg_gl_teximage_format(img->cmn.pixel_format);
const bool is_compressed = _sg_is_compressed_pixel_format(img->cmn.pixel_format);
for (int slot = 0; slot < img->cmn.num_slots; slot++) {
glGenTextures(1, &img->gl.tex[slot]);
SOKOL_ASSERT(img->gl.tex[slot]);
_sg_gl_cache_store_texture_binding(0);
_sg_gl_cache_bind_texture(0, img->gl.target, img->gl.tex[slot]);
GLenum gl_min_filter = _sg_gl_filter(img->cmn.min_filter);
GLenum gl_mag_filter = _sg_gl_filter(img->cmn.mag_filter);
glTexParameteri(img->gl.target, GL_TEXTURE_MIN_FILTER, (GLint)gl_min_filter);
glTexParameteri(img->gl.target, GL_TEXTURE_MAG_FILTER, (GLint)gl_mag_filter);
if (_sg.gl.ext_anisotropic && (img->cmn.max_anisotropy > 1)) {
GLint max_aniso = (GLint) img->cmn.max_anisotropy;
if (max_aniso > _sg.gl.max_anisotropy) {
max_aniso = _sg.gl.max_anisotropy;
}
glTexParameteri(img->gl.target, GL_TEXTURE_MAX_ANISOTROPY_EXT, max_aniso);
}
if (img->cmn.type == SG_IMAGETYPE_CUBE) {
glTexParameteri(img->gl.target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(img->gl.target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
}
else {
glTexParameteri(img->gl.target, GL_TEXTURE_WRAP_S, (GLint)_sg_gl_wrap(img->cmn.wrap_u));
glTexParameteri(img->gl.target, GL_TEXTURE_WRAP_T, (GLint)_sg_gl_wrap(img->cmn.wrap_v));
#if !defined(SOKOL_GLES2)
if (!_sg.gl.gles2 && (img->cmn.type == SG_IMAGETYPE_3D)) {
glTexParameteri(img->gl.target, GL_TEXTURE_WRAP_R, (GLint)_sg_gl_wrap(img->cmn.wrap_w));
}
#endif
#if defined(SOKOL_GLCORE33)
float border[4];
switch (img->cmn.border_color) {
case SG_BORDERCOLOR_TRANSPARENT_BLACK:
border[0] = 0.0f; border[1] = 0.0f; border[2] = 0.0f; border[3] = 0.0f;
break;
case SG_BORDERCOLOR_OPAQUE_WHITE:
border[0] = 1.0f; border[1] = 1.0f; border[2] = 1.0f; border[3] = 1.0f;
break;
default:
border[0] = 0.0f; border[1] = 0.0f; border[2] = 0.0f; border[3] = 1.0f;
break;
}
glTexParameterfv(img->gl.target, GL_TEXTURE_BORDER_COLOR, border);
#endif
}
#if !defined(SOKOL_GLES2)
if (!_sg.gl.gles2) {
/* GL spec has strange defaults for mipmap min/max lod: -1000 to +1000 */
const float min_lod = _sg_clamp(desc->min_lod, 0.0f, 1000.0f);
const float max_lod = _sg_clamp(desc->max_lod, 0.0f, 1000.0f);
glTexParameterf(img->gl.target, GL_TEXTURE_MIN_LOD, min_lod);
glTexParameterf(img->gl.target, GL_TEXTURE_MAX_LOD, max_lod);
}
#endif
const int num_faces = img->cmn.type == SG_IMAGETYPE_CUBE ? 6 : 1;
int data_index = 0;
for (int face_index = 0; face_index < num_faces; face_index++) {
for (int mip_index = 0; mip_index < img->cmn.num_mipmaps; mip_index++, data_index++) {
GLenum gl_img_target = img->gl.target;
if (SG_IMAGETYPE_CUBE == img->cmn.type) {
gl_img_target = _sg_gl_cubeface_target(face_index);
}
const GLvoid* data_ptr = desc->data.subimage[face_index][mip_index].ptr;
const GLsizei data_size = (GLsizei) desc->data.subimage[face_index][mip_index].size;
int mip_width = img->cmn.width >> mip_index;
if (mip_width == 0) {
mip_width = 1;
}
int mip_height = img->cmn.height >> mip_index;
if (mip_height == 0) {
mip_height = 1;
}
if ((SG_IMAGETYPE_2D == img->cmn.type) || (SG_IMAGETYPE_CUBE == img->cmn.type)) {
if (is_compressed) {
glCompressedTexImage2D(gl_img_target, mip_index, gl_internal_format,
mip_width, mip_height, 0, data_size, data_ptr);
}
else {
const GLenum gl_type = _sg_gl_teximage_type(img->cmn.pixel_format);
glTexImage2D(gl_img_target, mip_index, (GLint)gl_internal_format,
mip_width, mip_height, 0, gl_format, gl_type, data_ptr);
}
}
#if !defined(SOKOL_GLES2)
else if (!_sg.gl.gles2 && ((SG_IMAGETYPE_3D == img->cmn.type) || (SG_IMAGETYPE_ARRAY == img->cmn.type))) {
int mip_depth = img->cmn.num_slices;
if (SG_IMAGETYPE_3D == img->cmn.type) {
mip_depth >>= mip_index;
}
if (mip_depth == 0) {
mip_depth = 1;
}
if (is_compressed) {
glCompressedTexImage3D(gl_img_target, mip_index, gl_internal_format,
mip_width, mip_height, mip_depth, 0, data_size, data_ptr);
}
else {
const GLenum gl_type = _sg_gl_teximage_type(img->cmn.pixel_format);
glTexImage3D(gl_img_target, mip_index, (GLint)gl_internal_format,
mip_width, mip_height, mip_depth, 0, gl_format, gl_type, data_ptr);
}
}
#endif
}
}
_sg_gl_cache_restore_texture_binding(0);
}
}
}
_SG_GL_CHECK_ERROR();
return SG_RESOURCESTATE_VALID;
}
_SOKOL_PRIVATE void _sg_gl_destroy_image(_sg_image_t* img) {
SOKOL_ASSERT(img);
_SG_GL_CHECK_ERROR();
for (int slot = 0; slot < img->cmn.num_slots; slot++) {
if (img->gl.tex[slot]) {
_sg_gl_cache_invalidate_texture(img->gl.tex[slot]);
if (!img->gl.ext_textures) {
glDeleteTextures(1, &img->gl.tex[slot]);
}
}
}
if (img->gl.depth_render_buffer) {
glDeleteRenderbuffers(1, &img->gl.depth_render_buffer);
}
if (img->gl.msaa_render_buffer) {
glDeleteRenderbuffers(1, &img->gl.msaa_render_buffer);
}
_SG_GL_CHECK_ERROR();
}
_SOKOL_PRIVATE GLuint _sg_gl_compile_shader(sg_shader_stage stage, const char* src) {
SOKOL_ASSERT(src);
_SG_GL_CHECK_ERROR();
GLuint gl_shd = glCreateShader(_sg_gl_shader_stage(stage));
glShaderSource(gl_shd, 1, &src, 0);
glCompileShader(gl_shd);
GLint compile_status = 0;
glGetShaderiv(gl_shd, GL_COMPILE_STATUS, &compile_status);
if (!compile_status) {
/* compilation failed, log error and delete shader */
GLint log_len = 0;
glGetShaderiv(gl_shd, GL_INFO_LOG_LENGTH, &log_len);
if (log_len > 0) {
GLchar* log_buf = (GLchar*) SOKOL_MALLOC((size_t)log_len);
glGetShaderInfoLog(gl_shd, log_len, &log_len, log_buf);
SOKOL_LOG(log_buf);
SOKOL_FREE(log_buf);
}
glDeleteShader(gl_shd);
gl_shd = 0;
}
_SG_GL_CHECK_ERROR();
return gl_shd;
}
_SOKOL_PRIVATE sg_resource_state _sg_gl_create_shader(_sg_shader_t* shd, const sg_shader_desc* desc) {
SOKOL_ASSERT(shd && desc);
SOKOL_ASSERT(!shd->gl.prog);
_SG_GL_CHECK_ERROR();
_sg_shader_common_init(&shd->cmn, desc);
/* copy vertex attribute names over, these are required for GLES2, and optional for GLES3 and GL3.x */
for (int i = 0; i < SG_MAX_VERTEX_ATTRIBUTES; i++) {
_sg_strcpy(&shd->gl.attrs[i].name, desc->attrs[i].name);
}
GLuint gl_vs = _sg_gl_compile_shader(SG_SHADERSTAGE_VS, desc->vs.source);
GLuint gl_fs = _sg_gl_compile_shader(SG_SHADERSTAGE_FS, desc->fs.source);
if (!(gl_vs && gl_fs)) {
return SG_RESOURCESTATE_FAILED;
}
GLuint gl_prog = glCreateProgram();
glAttachShader(gl_prog, gl_vs);
glAttachShader(gl_prog, gl_fs);
glLinkProgram(gl_prog);
glDeleteShader(gl_vs);
glDeleteShader(gl_fs);
_SG_GL_CHECK_ERROR();
GLint link_status;
glGetProgramiv(gl_prog, GL_LINK_STATUS, &link_status);
if (!link_status) {
GLint log_len = 0;
glGetProgramiv(gl_prog, GL_INFO_LOG_LENGTH, &log_len);
if (log_len > 0) {
GLchar* log_buf = (GLchar*) SOKOL_MALLOC((size_t)log_len);
glGetProgramInfoLog(gl_prog, log_len, &log_len, log_buf);
SOKOL_LOG(log_buf);
SOKOL_FREE(log_buf);
}
glDeleteProgram(gl_prog);
return SG_RESOURCESTATE_FAILED;
}
shd->gl.prog = gl_prog;
/* resolve uniforms */
_SG_GL_CHECK_ERROR();
for (int stage_index = 0; stage_index < SG_NUM_SHADER_STAGES; stage_index++) {
const sg_shader_stage_desc* stage_desc = (stage_index == SG_SHADERSTAGE_VS)? &desc->vs : &desc->fs;
_sg_gl_shader_stage_t* gl_stage = &shd->gl.stage[stage_index];
for (int ub_index = 0; ub_index < shd->cmn.stage[stage_index].num_uniform_blocks; ub_index++) {
const sg_shader_uniform_block_desc* ub_desc = &stage_desc->uniform_blocks[ub_index];
SOKOL_ASSERT(ub_desc->size > 0);
_sg_gl_uniform_block_t* ub = &gl_stage->uniform_blocks[ub_index];
SOKOL_ASSERT(ub->num_uniforms == 0);
int cur_uniform_offset = 0;
for (int u_index = 0; u_index < SG_MAX_UB_MEMBERS; u_index++) {
const sg_shader_uniform_desc* u_desc = &ub_desc->uniforms[u_index];
if (u_desc->type == SG_UNIFORMTYPE_INVALID) {
break;
}
_sg_gl_uniform_t* u = &ub->uniforms[u_index];
u->type = u_desc->type;
u->count = (uint8_t) u_desc->array_count;
u->offset = (uint16_t) cur_uniform_offset;
cur_uniform_offset += _sg_uniform_size(u->type, u->count);
if (u_desc->name) {
u->gl_loc = glGetUniformLocation(gl_prog, u_desc->name);
}
else {
u->gl_loc = u_index;
}
ub->num_uniforms++;
}
SOKOL_ASSERT(ub_desc->size == (size_t)cur_uniform_offset);
}
}
/* resolve image locations */
_SG_GL_CHECK_ERROR();
GLuint cur_prog = 0;
glGetIntegerv(GL_CURRENT_PROGRAM, (GLint*)&cur_prog);
glUseProgram(gl_prog);
int gl_tex_slot = 0;
for (int stage_index = 0; stage_index < SG_NUM_SHADER_STAGES; stage_index++) {
const sg_shader_stage_desc* stage_desc = (stage_index == SG_SHADERSTAGE_VS)? &desc->vs : &desc->fs;
_sg_gl_shader_stage_t* gl_stage = &shd->gl.stage[stage_index];
for (int img_index = 0; img_index < shd->cmn.stage[stage_index].num_images; img_index++) {
const sg_shader_image_desc* img_desc = &stage_desc->images[img_index];
SOKOL_ASSERT(img_desc->image_type != _SG_IMAGETYPE_DEFAULT);
_sg_gl_shader_image_t* gl_img = &gl_stage->images[img_index];
GLint gl_loc = img_index;
if (img_desc->name) {
gl_loc = glGetUniformLocation(gl_prog, img_desc->name);
}
if (gl_loc != -1) {
gl_img->gl_tex_slot = gl_tex_slot++;
glUniform1i(gl_loc, gl_img->gl_tex_slot);
}
else {
gl_img->gl_tex_slot = -1;
}
}
}
/* it's legal to call glUseProgram with 0 */
glUseProgram(cur_prog);
_SG_GL_CHECK_ERROR();
return SG_RESOURCESTATE_VALID;
}
_SOKOL_PRIVATE void _sg_gl_destroy_shader(_sg_shader_t* shd) {
SOKOL_ASSERT(shd);
_SG_GL_CHECK_ERROR();
if (shd->gl.prog) {
_sg_gl_cache_invalidate_program(shd->gl.prog);
glDeleteProgram(shd->gl.prog);
}
_SG_GL_CHECK_ERROR();
}
_SOKOL_PRIVATE sg_resource_state _sg_gl_create_pipeline(_sg_pipeline_t* pip, _sg_shader_t* shd, const sg_pipeline_desc* desc) {
SOKOL_ASSERT(pip && shd && desc);
SOKOL_ASSERT(!pip->shader && pip->cmn.shader_id.id == SG_INVALID_ID);
SOKOL_ASSERT(desc->shader.id == shd->slot.id);
SOKOL_ASSERT(shd->gl.prog);
pip->shader = shd;
_sg_pipeline_common_init(&pip->cmn, desc);
pip->gl.primitive_type = desc->primitive_type;
pip->gl.depth = desc->depth;
pip->gl.stencil = desc->stencil;
// FIXME: blend color and write mask per draw-buffer-attachment (requires GL4)
pip->gl.blend = desc->colors[0].blend;
for (int i = 0; i < SG_MAX_COLOR_ATTACHMENTS; i++) {
pip->gl.color_write_mask[i] = desc->colors[i].write_mask;
}
pip->gl.cull_mode = desc->cull_mode;
pip->gl.face_winding = desc->face_winding;
pip->gl.sample_count = desc->sample_count;
pip->gl.alpha_to_coverage_enabled = desc->alpha_to_coverage_enabled;
/* resolve vertex attributes */
for (int attr_index = 0; attr_index < SG_MAX_VERTEX_ATTRIBUTES; attr_index++) {
pip->gl.attrs[attr_index].vb_index = -1;
}
for (int attr_index = 0; attr_index < _sg.limits.max_vertex_attrs; attr_index++) {
const sg_vertex_attr_desc* a_desc = &desc->layout.attrs[attr_index];
if (a_desc->format == SG_VERTEXFORMAT_INVALID) {
break;
}
SOKOL_ASSERT(a_desc->buffer_index < SG_MAX_SHADERSTAGE_BUFFERS);
const sg_buffer_layout_desc* l_desc = &desc->layout.buffers[a_desc->buffer_index];
const sg_vertex_step step_func = l_desc->step_func;
const int step_rate = l_desc->step_rate;
GLint attr_loc = attr_index;
if (!_sg_strempty(&shd->gl.attrs[attr_index].name)) {
attr_loc = glGetAttribLocation(pip->shader->gl.prog, _sg_strptr(&shd->gl.attrs[attr_index].name));
}
SOKOL_ASSERT(attr_loc < (GLint)_sg.limits.max_vertex_attrs);
if (attr_loc != -1) {
_sg_gl_attr_t* gl_attr = &pip->gl.attrs[attr_loc];
SOKOL_ASSERT(gl_attr->vb_index == -1);
gl_attr->vb_index = (int8_t) a_desc->buffer_index;
if (step_func == SG_VERTEXSTEP_PER_VERTEX) {
gl_attr->divisor = 0;
}
else {
gl_attr->divisor = (int8_t) step_rate;
}
SOKOL_ASSERT(l_desc->stride > 0);
gl_attr->stride = (uint8_t) l_desc->stride;
gl_attr->offset = a_desc->offset;
gl_attr->size = (uint8_t) _sg_gl_vertexformat_size(a_desc->format);
gl_attr->type = _sg_gl_vertexformat_type(a_desc->format);
gl_attr->normalized = _sg_gl_vertexformat_normalized(a_desc->format);
pip->cmn.vertex_layout_valid[a_desc->buffer_index] = true;
}
else {
SOKOL_LOG("Vertex attribute not found in shader: ");
SOKOL_LOG(_sg_strptr(&shd->gl.attrs[attr_index].name));
}
}
return SG_RESOURCESTATE_VALID;
}
_SOKOL_PRIVATE void _sg_gl_destroy_pipeline(_sg_pipeline_t* pip) {
SOKOL_ASSERT(pip);
_SOKOL_UNUSED(pip);
/* empty */
}
/*
_sg_create_pass
att_imgs must point to a _sg_image* att_imgs[SG_MAX_COLOR_ATTACHMENTS+1] array,
first entries are the color attachment images (or nullptr), last entry
is the depth-stencil image (or nullptr).
*/
_SOKOL_PRIVATE sg_resource_state _sg_gl_create_pass(_sg_pass_t* pass, _sg_image_t** att_images, const sg_pass_desc* desc) {
SOKOL_ASSERT(pass && att_images && desc);
SOKOL_ASSERT(att_images && att_images[0]);
_SG_GL_CHECK_ERROR();
_sg_pass_common_init(&pass->cmn, desc);
/* copy image pointers */
const sg_pass_attachment_desc* att_desc;
for (int i = 0; i < pass->cmn.num_color_atts; i++) {
att_desc = &desc->color_attachments[i];
SOKOL_ASSERT(att_desc->image.id != SG_INVALID_ID);
SOKOL_ASSERT(0 == pass->gl.color_atts[i].image);
SOKOL_ASSERT(att_images[i] && (att_images[i]->slot.id == att_desc->image.id));
SOKOL_ASSERT(_sg_is_valid_rendertarget_color_format(att_images[i]->cmn.pixel_format));
pass->gl.color_atts[i].image = att_images[i];
}
SOKOL_ASSERT(0 == pass->gl.ds_att.image);
att_desc = &desc->depth_stencil_attachment;
if (att_desc->image.id != SG_INVALID_ID) {
const int ds_img_index = SG_MAX_COLOR_ATTACHMENTS;
SOKOL_ASSERT(att_images[ds_img_index] && (att_images[ds_img_index]->slot.id == att_desc->image.id));
SOKOL_ASSERT(_sg_is_valid_rendertarget_depth_format(att_images[ds_img_index]->cmn.pixel_format));
pass->gl.ds_att.image = att_images[ds_img_index];
}
/* store current framebuffer binding (restored at end of function) */
GLuint gl_orig_fb;
glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*)&gl_orig_fb);
/* create a framebuffer object */
glGenFramebuffers(1, &pass->gl.fb);
glBindFramebuffer(GL_FRAMEBUFFER, pass->gl.fb);
/* attach msaa render buffer or textures */
const bool is_msaa = (0 != att_images[0]->gl.msaa_render_buffer);
if (is_msaa) {
for (int i = 0; i < SG_MAX_COLOR_ATTACHMENTS; i++) {
const _sg_image_t* att_img = pass->gl.color_atts[i].image;
if (att_img) {
const GLuint gl_render_buffer = att_img->gl.msaa_render_buffer;
SOKOL_ASSERT(gl_render_buffer);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, (GLenum)(GL_COLOR_ATTACHMENT0+i), GL_RENDERBUFFER, gl_render_buffer);
}
}
}
else {
for (int i = 0; i < SG_MAX_COLOR_ATTACHMENTS; i++) {
const _sg_image_t* att_img = pass->gl.color_atts[i].image;
const int mip_level = pass->cmn.color_atts[i].mip_level;
const int slice = pass->cmn.color_atts[i].slice;
if (att_img) {
const GLuint gl_tex = att_img->gl.tex[0];
SOKOL_ASSERT(gl_tex);
const GLenum gl_att = (GLenum)(GL_COLOR_ATTACHMENT0 + i);
switch (att_img->cmn.type) {
case SG_IMAGETYPE_2D:
glFramebufferTexture2D(GL_FRAMEBUFFER, gl_att, GL_TEXTURE_2D, gl_tex, mip_level);
break;
case SG_IMAGETYPE_CUBE:
glFramebufferTexture2D(GL_FRAMEBUFFER, gl_att, _sg_gl_cubeface_target(slice), gl_tex, mip_level);
break;
default:
/* 3D- or array-texture */
#if !defined(SOKOL_GLES2)
if (!_sg.gl.gles2) {
glFramebufferTextureLayer(GL_FRAMEBUFFER, gl_att, gl_tex, mip_level, slice);
}
#endif
break;
}
}
}
}
/* attach depth-stencil buffer to framebuffer */
if (pass->gl.ds_att.image) {
const GLuint gl_render_buffer = pass->gl.ds_att.image->gl.depth_render_buffer;
SOKOL_ASSERT(gl_render_buffer);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, gl_render_buffer);
if (_sg_is_depth_stencil_format(pass->gl.ds_att.image->cmn.pixel_format)) {
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, gl_render_buffer);
}
}
/* check if framebuffer is complete */
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
SOKOL_LOG("Framebuffer completeness check failed!\n");
return SG_RESOURCESTATE_FAILED;
}
/* setup color attachments for the framebuffer */
#if !defined(SOKOL_GLES2)
if (!_sg.gl.gles2) {
GLenum att[SG_MAX_COLOR_ATTACHMENTS] = {
GL_COLOR_ATTACHMENT0,
GL_COLOR_ATTACHMENT1,
GL_COLOR_ATTACHMENT2,
GL_COLOR_ATTACHMENT3
};
glDrawBuffers(pass->cmn.num_color_atts, att);
}
#endif
/* create MSAA resolve framebuffers if necessary */
if (is_msaa) {
for (int i = 0; i < SG_MAX_COLOR_ATTACHMENTS; i++) {
_sg_gl_attachment_t* gl_att = &pass->gl.color_atts[i];
_sg_pass_attachment_t* cmn_att = &pass->cmn.color_atts[i];
if (gl_att->image) {
SOKOL_ASSERT(0 == gl_att->gl_msaa_resolve_buffer);
glGenFramebuffers(1, &gl_att->gl_msaa_resolve_buffer);
glBindFramebuffer(GL_FRAMEBUFFER, gl_att->gl_msaa_resolve_buffer);
const GLuint gl_tex = gl_att->image->gl.tex[0];
SOKOL_ASSERT(gl_tex);
switch (gl_att->image->cmn.type) {
case SG_IMAGETYPE_2D:
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
GL_TEXTURE_2D, gl_tex, cmn_att->mip_level);
break;
case SG_IMAGETYPE_CUBE:
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
_sg_gl_cubeface_target(cmn_att->slice), gl_tex, cmn_att->mip_level);
break;
default:
#if !defined(SOKOL_GLES2)
if (!_sg.gl.gles2) {
glFramebufferTextureLayer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, gl_tex, cmn_att->mip_level, cmn_att->slice);
}
#endif
break;
}
/* check if framebuffer is complete */
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
SOKOL_LOG("Framebuffer completeness check failed (msaa resolve buffer)!\n");
return SG_RESOURCESTATE_FAILED;
}
/* setup color attachments for the framebuffer */
#if !defined(SOKOL_GLES2)
if (!_sg.gl.gles2) {
const GLenum gl_draw_bufs = GL_COLOR_ATTACHMENT0;
glDrawBuffers(1, &gl_draw_bufs);
}
#endif
}
}
}
/* restore original framebuffer binding */
glBindFramebuffer(GL_FRAMEBUFFER, gl_orig_fb);
_SG_GL_CHECK_ERROR();
return SG_RESOURCESTATE_VALID;
}
_SOKOL_PRIVATE void _sg_gl_destroy_pass(_sg_pass_t* pass) {
SOKOL_ASSERT(pass);
_SG_GL_CHECK_ERROR();
if (0 != pass->gl.fb) {
glDeleteFramebuffers(1, &pass->gl.fb);
}
for (int i = 0; i < SG_MAX_COLOR_ATTACHMENTS; i++) {
if (pass->gl.color_atts[i].gl_msaa_resolve_buffer) {
glDeleteFramebuffers(1, &pass->gl.color_atts[i].gl_msaa_resolve_buffer);
}
}
if (pass->gl.ds_att.gl_msaa_resolve_buffer) {
glDeleteFramebuffers(1, &pass->gl.ds_att.gl_msaa_resolve_buffer);
}
_SG_GL_CHECK_ERROR();
}
_SOKOL_PRIVATE _sg_image_t* _sg_gl_pass_color_image(const _sg_pass_t* pass, int index) {
SOKOL_ASSERT(pass && (index >= 0) && (index < SG_MAX_COLOR_ATTACHMENTS));
/* NOTE: may return null */
return pass->gl.color_atts[index].image;
}
_SOKOL_PRIVATE _sg_image_t* _sg_gl_pass_ds_image(const _sg_pass_t* pass) {
/* NOTE: may return null */
SOKOL_ASSERT(pass);
return pass->gl.ds_att.image;
}
_SOKOL_PRIVATE void _sg_gl_begin_pass(_sg_pass_t* pass, const sg_pass_action* action, int w, int h) {
/* FIXME: what if a texture used as render target is still bound, should we
unbind all currently bound textures in begin pass? */
SOKOL_ASSERT(action);
SOKOL_ASSERT(!_sg.gl.in_pass);
_SG_GL_CHECK_ERROR();
_sg.gl.in_pass = true;
_sg.gl.cur_pass = pass; /* can be 0 */
if (pass) {
_sg.gl.cur_pass_id.id = pass->slot.id;
}
else {
_sg.gl.cur_pass_id.id = SG_INVALID_ID;
}
_sg.gl.cur_pass_width = w;
_sg.gl.cur_pass_height = h;
/* number of color attachments */
const int num_color_atts = pass ? pass->cmn.num_color_atts : 1;
/* bind the render pass framebuffer */
if (pass) {
/* offscreen pass */
SOKOL_ASSERT(pass->gl.fb);
glBindFramebuffer(GL_FRAMEBUFFER, pass->gl.fb);
}
else {
/* default pass */
SOKOL_ASSERT(_sg.gl.cur_context);
glBindFramebuffer(GL_FRAMEBUFFER, _sg.gl.cur_context->default_framebuffer);
}
glViewport(0, 0, w, h);
glScissor(0, 0, w, h);
/* clear color and depth-stencil attachments if needed */
bool clear_color = false;
for (int i = 0; i < num_color_atts; i++) {
if (SG_ACTION_CLEAR == action->colors[i].action) {
clear_color = true;
break;
}
}
const bool clear_depth = (action->depth.action == SG_ACTION_CLEAR);
const bool clear_stencil = (action->stencil.action == SG_ACTION_CLEAR);
bool need_pip_cache_flush = false;
if (clear_color) {
bool need_color_mask_flush = false;
// NOTE: not a bug to iterate over all possible color attachments
for (int i = 0; i < SG_MAX_COLOR_ATTACHMENTS; i++) {
if (SG_COLORMASK_RGBA != _sg.gl.cache.color_write_mask[i]) {
need_pip_cache_flush = true;
need_color_mask_flush = true;
_sg.gl.cache.color_write_mask[i] = SG_COLORMASK_RGBA;
}
}
if (need_color_mask_flush) {
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
}
}
if (clear_depth) {
if (!_sg.gl.cache.depth.write_enabled) {
need_pip_cache_flush = true;
_sg.gl.cache.depth.write_enabled = true;
glDepthMask(GL_TRUE);
}
if (_sg.gl.cache.depth.compare != SG_COMPAREFUNC_ALWAYS) {
need_pip_cache_flush = true;
_sg.gl.cache.depth.compare = SG_COMPAREFUNC_ALWAYS;
glDepthFunc(GL_ALWAYS);
}
}
if (clear_stencil) {
if (_sg.gl.cache.stencil.write_mask != 0xFF) {
need_pip_cache_flush = true;
_sg.gl.cache.stencil.write_mask = 0xFF;
glStencilMask(0xFF);
}
}
if (need_pip_cache_flush) {
/* we messed with the state cache directly, need to clear cached
pipeline to force re-evaluation in next sg_apply_pipeline() */
_sg.gl.cache.cur_pipeline = 0;
_sg.gl.cache.cur_pipeline_id.id = SG_INVALID_ID;
}
bool use_mrt_clear = (0 != pass);
#if defined(SOKOL_GLES2)
use_mrt_clear = false;
#else
if (_sg.gl.gles2) {
use_mrt_clear = false;
}
#endif
if (!use_mrt_clear) {
GLbitfield clear_mask = 0;
if (clear_color) {
clear_mask |= GL_COLOR_BUFFER_BIT;
const sg_color c = action->colors[0].value;
glClearColor(c.r, c.g, c.b, c.a);
}
if (clear_depth) {
clear_mask |= GL_DEPTH_BUFFER_BIT;
#ifdef SOKOL_GLCORE33
glClearDepth(action->depth.value);
#else
glClearDepthf(action->depth.value);
#endif
}
if (clear_stencil) {
clear_mask |= GL_STENCIL_BUFFER_BIT;
glClearStencil(action->stencil.value);
}
if (0 != clear_mask) {
glClear(clear_mask);
}
}
#if !defined SOKOL_GLES2
else {
SOKOL_ASSERT(pass);
for (int i = 0; i < num_color_atts; i++) {
if (action->colors[i].action == SG_ACTION_CLEAR) {
glClearBufferfv(GL_COLOR, i, &action->colors[i].value.r);
}
}
if (pass->gl.ds_att.image) {
if (clear_depth && clear_stencil) {
glClearBufferfi(GL_DEPTH_STENCIL, 0, action->depth.value, action->stencil.value);
}
else if (clear_depth) {
glClearBufferfv(GL_DEPTH, 0, &action->depth.value);
}
else if (clear_stencil) {
GLint val = (GLint) action->stencil.value;
glClearBufferiv(GL_STENCIL, 0, &val);
}
}
}
#endif
_SG_GL_CHECK_ERROR();
}
_SOKOL_PRIVATE void _sg_gl_end_pass(void) {
SOKOL_ASSERT(_sg.gl.in_pass);
_SG_GL_CHECK_ERROR();
/* if this was an offscreen pass, and MSAA rendering was used, need
to resolve into the pass images */
#if !defined(SOKOL_GLES2)
if (!_sg.gl.gles2 && _sg.gl.cur_pass) {
/* check if the pass object is still valid */
const _sg_pass_t* pass = _sg.gl.cur_pass;
SOKOL_ASSERT(pass->slot.id == _sg.gl.cur_pass_id.id);
bool is_msaa = (0 != _sg.gl.cur_pass->gl.color_atts[0].gl_msaa_resolve_buffer);
if (is_msaa) {
SOKOL_ASSERT(pass->gl.fb);
glBindFramebuffer(GL_READ_FRAMEBUFFER, pass->gl.fb);
SOKOL_ASSERT(pass->gl.color_atts[0].image);
const int w = pass->gl.color_atts[0].image->cmn.width;
const int h = pass->gl.color_atts[0].image->cmn.height;
for (int att_index = 0; att_index < SG_MAX_COLOR_ATTACHMENTS; att_index++) {
const _sg_gl_attachment_t* gl_att = &pass->gl.color_atts[att_index];
if (gl_att->image) {
SOKOL_ASSERT(gl_att->gl_msaa_resolve_buffer);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, gl_att->gl_msaa_resolve_buffer);
glReadBuffer((GLenum)(GL_COLOR_ATTACHMENT0 + att_index));
glBlitFramebuffer(0, 0, w, h, 0, 0, w, h, GL_COLOR_BUFFER_BIT, GL_NEAREST);
}
else {
break;
}
}
}
}
#endif
_sg.gl.cur_pass = 0;
_sg.gl.cur_pass_id.id = SG_INVALID_ID;
_sg.gl.cur_pass_width = 0;
_sg.gl.cur_pass_height = 0;
SOKOL_ASSERT(_sg.gl.cur_context);
glBindFramebuffer(GL_FRAMEBUFFER, _sg.gl.cur_context->default_framebuffer);
_sg.gl.in_pass = false;
_SG_GL_CHECK_ERROR();
}
_SOKOL_PRIVATE void _sg_gl_apply_viewport(int x, int y, int w, int h, bool origin_top_left) {
SOKOL_ASSERT(_sg.gl.in_pass);
y = origin_top_left ? (_sg.gl.cur_pass_height - (y+h)) : y;
glViewport(x, y, w, h);
}
_SOKOL_PRIVATE void _sg_gl_apply_scissor_rect(int x, int y, int w, int h, bool origin_top_left) {
SOKOL_ASSERT(_sg.gl.in_pass);
y = origin_top_left ? (_sg.gl.cur_pass_height - (y+h)) : y;
glScissor(x, y, w, h);
}
_SOKOL_PRIVATE void _sg_gl_apply_pipeline(_sg_pipeline_t* pip) {
SOKOL_ASSERT(pip);
SOKOL_ASSERT(pip->shader && (pip->cmn.shader_id.id == pip->shader->slot.id));
_SG_GL_CHECK_ERROR();
if ((_sg.gl.cache.cur_pipeline != pip) || (_sg.gl.cache.cur_pipeline_id.id != pip->slot.id)) {
_sg.gl.cache.cur_pipeline = pip;
_sg.gl.cache.cur_pipeline_id.id = pip->slot.id;
_sg.gl.cache.cur_primitive_type = _sg_gl_primitive_type(pip->gl.primitive_type);
_sg.gl.cache.cur_index_type = _sg_gl_index_type(pip->cmn.index_type);
/* update depth state */
{
const sg_depth_state* state_ds = &pip->gl.depth;
sg_depth_state* cache_ds = &_sg.gl.cache.depth;
if (state_ds->compare != cache_ds->compare) {
cache_ds->compare = state_ds->compare;
glDepthFunc(_sg_gl_compare_func(state_ds->compare));
}
if (state_ds->write_enabled != cache_ds->write_enabled) {
cache_ds->write_enabled = state_ds->write_enabled;
glDepthMask(state_ds->write_enabled);
}
if (!_sg_fequal(state_ds->bias, cache_ds->bias, 0.000001f) ||
!_sg_fequal(state_ds->bias_slope_scale, cache_ds->bias_slope_scale, 0.000001f))
{
/* according to ANGLE's D3D11 backend:
D3D11 SlopeScaledDepthBias ==> GL polygonOffsetFactor
D3D11 DepthBias ==> GL polygonOffsetUnits
DepthBiasClamp has no meaning on GL
*/
cache_ds->bias = state_ds->bias;
cache_ds->bias_slope_scale = state_ds->bias_slope_scale;
glPolygonOffset(state_ds->bias_slope_scale, state_ds->bias);
bool po_enabled = true;
if (_sg_fequal(state_ds->bias, 0.0f, 0.000001f) &&
_sg_fequal(state_ds->bias_slope_scale, 0.0f, 0.000001f))
{
po_enabled = false;
}
if (po_enabled != _sg.gl.cache.polygon_offset_enabled) {
_sg.gl.cache.polygon_offset_enabled = po_enabled;
if (po_enabled) {
glEnable(GL_POLYGON_OFFSET_FILL);
}
else {
glDisable(GL_POLYGON_OFFSET_FILL);
}
}
}
}
/* update stencil state */
{
const sg_stencil_state* state_ss = &pip->gl.stencil;
sg_stencil_state* cache_ss = &_sg.gl.cache.stencil;
if (state_ss->enabled != cache_ss->enabled) {
cache_ss->enabled = state_ss->enabled;
if (state_ss->enabled) {
glEnable(GL_STENCIL_TEST);
}
else {
glDisable(GL_STENCIL_TEST);
}
}
if (state_ss->write_mask != cache_ss->write_mask) {
cache_ss->write_mask = state_ss->write_mask;
glStencilMask(state_ss->write_mask);
}
for (int i = 0; i < 2; i++) {
const sg_stencil_face_state* state_sfs = (i==0)? &state_ss->front : &state_ss->back;
sg_stencil_face_state* cache_sfs = (i==0)? &cache_ss->front : &cache_ss->back;
GLenum gl_face = (i==0)? GL_FRONT : GL_BACK;
if ((state_sfs->compare != cache_sfs->compare) ||
(state_ss->read_mask != cache_ss->read_mask) ||
(state_ss->ref != cache_ss->ref))
{
cache_sfs->compare = state_sfs->compare;
glStencilFuncSeparate(gl_face,
_sg_gl_compare_func(state_sfs->compare),
state_ss->ref,
state_ss->read_mask);
}
if ((state_sfs->fail_op != cache_sfs->fail_op) ||
(state_sfs->depth_fail_op != cache_sfs->depth_fail_op) ||
(state_sfs->pass_op != cache_sfs->pass_op))
{
cache_sfs->fail_op = state_sfs->fail_op;
cache_sfs->depth_fail_op = state_sfs->depth_fail_op;
cache_sfs->pass_op = state_sfs->pass_op;
glStencilOpSeparate(gl_face,
_sg_gl_stencil_op(state_sfs->fail_op),
_sg_gl_stencil_op(state_sfs->depth_fail_op),
_sg_gl_stencil_op(state_sfs->pass_op));
}
}
cache_ss->read_mask = state_ss->read_mask;
cache_ss->ref = state_ss->ref;
}
/* update blend state
FIXME: separate blend state per color attachment not support, needs GL4
*/
{
const sg_blend_state* state_bs = &pip->gl.blend;
sg_blend_state* cache_bs = &_sg.gl.cache.blend;
if (state_bs->enabled != cache_bs->enabled) {
cache_bs->enabled = state_bs->enabled;
if (state_bs->enabled) {
glEnable(GL_BLEND);
}
else {
glDisable(GL_BLEND);
}
}
if ((state_bs->src_factor_rgb != cache_bs->src_factor_rgb) ||
(state_bs->dst_factor_rgb != cache_bs->dst_factor_rgb) ||
(state_bs->src_factor_alpha != cache_bs->src_factor_alpha) ||
(state_bs->dst_factor_alpha != cache_bs->dst_factor_alpha))
{
cache_bs->src_factor_rgb = state_bs->src_factor_rgb;
cache_bs->dst_factor_rgb = state_bs->dst_factor_rgb;
cache_bs->src_factor_alpha = state_bs->src_factor_alpha;
cache_bs->dst_factor_alpha = state_bs->dst_factor_alpha;
glBlendFuncSeparate(_sg_gl_blend_factor(state_bs->src_factor_rgb),
_sg_gl_blend_factor(state_bs->dst_factor_rgb),
_sg_gl_blend_factor(state_bs->src_factor_alpha),
_sg_gl_blend_factor(state_bs->dst_factor_alpha));
}
if ((state_bs->op_rgb != cache_bs->op_rgb) || (state_bs->op_alpha != cache_bs->op_alpha)) {
cache_bs->op_rgb = state_bs->op_rgb;
cache_bs->op_alpha = state_bs->op_alpha;
glBlendEquationSeparate(_sg_gl_blend_op(state_bs->op_rgb), _sg_gl_blend_op(state_bs->op_alpha));
}
}
/* standalone state */
for (GLuint i = 0; i < (GLuint)pip->cmn.color_attachment_count; i++) {
if (pip->gl.color_write_mask[i] != _sg.gl.cache.color_write_mask[i]) {
const sg_color_mask cm = pip->gl.color_write_mask[i];
_sg.gl.cache.color_write_mask[i] = cm;
#ifdef SOKOL_GLCORE33
glColorMaski(i,
(cm & SG_COLORMASK_R) != 0,
(cm & SG_COLORMASK_G) != 0,
(cm & SG_COLORMASK_B) != 0,
(cm & SG_COLORMASK_A) != 0);
#else
if (0 == i) {
glColorMask((cm & SG_COLORMASK_R) != 0,
(cm & SG_COLORMASK_G) != 0,
(cm & SG_COLORMASK_B) != 0,
(cm & SG_COLORMASK_A) != 0);
}
#endif
}
}
if (!_sg_fequal(pip->cmn.blend_color.r, _sg.gl.cache.blend_color.r, 0.0001f) ||
!_sg_fequal(pip->cmn.blend_color.g, _sg.gl.cache.blend_color.g, 0.0001f) ||
!_sg_fequal(pip->cmn.blend_color.b, _sg.gl.cache.blend_color.b, 0.0001f) ||
!_sg_fequal(pip->cmn.blend_color.a, _sg.gl.cache.blend_color.a, 0.0001f))
{
sg_color c = pip->cmn.blend_color;
_sg.gl.cache.blend_color = c;
glBlendColor(c.r, c.g, c.b, c.a);
}
if (pip->gl.cull_mode != _sg.gl.cache.cull_mode) {
_sg.gl.cache.cull_mode = pip->gl.cull_mode;
if (SG_CULLMODE_NONE == pip->gl.cull_mode) {
glDisable(GL_CULL_FACE);
}
else {
glEnable(GL_CULL_FACE);
GLenum gl_mode = (SG_CULLMODE_FRONT == pip->gl.cull_mode) ? GL_FRONT : GL_BACK;
glCullFace(gl_mode);
}
}
if (pip->gl.face_winding != _sg.gl.cache.face_winding) {
_sg.gl.cache.face_winding = pip->gl.face_winding;
GLenum gl_winding = (SG_FACEWINDING_CW == pip->gl.face_winding) ? GL_CW : GL_CCW;
glFrontFace(gl_winding);
}
if (pip->gl.alpha_to_coverage_enabled != _sg.gl.cache.alpha_to_coverage_enabled) {
_sg.gl.cache.alpha_to_coverage_enabled = pip->gl.alpha_to_coverage_enabled;
if (pip->gl.alpha_to_coverage_enabled) {
glEnable(GL_SAMPLE_ALPHA_TO_COVERAGE);
}
else {
glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE);
}
}
#ifdef SOKOL_GLCORE33
if (pip->gl.sample_count != _sg.gl.cache.sample_count) {
_sg.gl.cache.sample_count = pip->gl.sample_count;
if (pip->gl.sample_count > 1) {
glEnable(GL_MULTISAMPLE);
}
else {
glDisable(GL_MULTISAMPLE);
}
}
#endif
/* bind shader program */
if (pip->shader->gl.prog != _sg.gl.cache.prog) {
_sg.gl.cache.prog = pip->shader->gl.prog;
glUseProgram(pip->shader->gl.prog);
}
}
_SG_GL_CHECK_ERROR();
}
_SOKOL_PRIVATE void _sg_gl_apply_bindings(
_sg_pipeline_t* pip,
_sg_buffer_t** vbs, const int* vb_offsets, int num_vbs,
_sg_buffer_t* ib, int ib_offset,
_sg_image_t** vs_imgs, int num_vs_imgs,
_sg_image_t** fs_imgs, int num_fs_imgs)
{
SOKOL_ASSERT(pip);
_SOKOL_UNUSED(num_fs_imgs);
_SOKOL_UNUSED(num_vs_imgs);
_SOKOL_UNUSED(num_vbs);
_SG_GL_CHECK_ERROR();
/* bind textures */
_SG_GL_CHECK_ERROR();
for (int stage_index = 0; stage_index < SG_NUM_SHADER_STAGES; stage_index++) {
const _sg_shader_stage_t* stage = &pip->shader->cmn.stage[stage_index];
const _sg_gl_shader_stage_t* gl_stage = &pip->shader->gl.stage[stage_index];
_sg_image_t** imgs = (stage_index == SG_SHADERSTAGE_VS)? vs_imgs : fs_imgs;
SOKOL_ASSERT(((stage_index == SG_SHADERSTAGE_VS)? num_vs_imgs : num_fs_imgs) == stage->num_images);
for (int img_index = 0; img_index < stage->num_images; img_index++) {
const _sg_gl_shader_image_t* gl_shd_img = &gl_stage->images[img_index];
if (gl_shd_img->gl_tex_slot != -1) {
_sg_image_t* img = imgs[img_index];
const GLuint gl_tex = img->gl.tex[img->cmn.active_slot];
SOKOL_ASSERT(img && img->gl.target);
SOKOL_ASSERT((gl_shd_img->gl_tex_slot != -1) && gl_tex);
_sg_gl_cache_bind_texture(gl_shd_img->gl_tex_slot, img->gl.target, gl_tex);
}
}
}
_SG_GL_CHECK_ERROR();
/* index buffer (can be 0) */
const GLuint gl_ib = ib ? ib->gl.buf[ib->cmn.active_slot] : 0;
_sg_gl_cache_bind_buffer(GL_ELEMENT_ARRAY_BUFFER, gl_ib);
_sg.gl.cache.cur_ib_offset = ib_offset;
/* vertex attributes */
for (GLuint attr_index = 0; attr_index < (GLuint)_sg.limits.max_vertex_attrs; attr_index++) {
_sg_gl_attr_t* attr = &pip->gl.attrs[attr_index];
_sg_gl_cache_attr_t* cache_attr = &_sg.gl.cache.attrs[attr_index];
bool cache_attr_dirty = false;
int vb_offset = 0;
GLuint gl_vb = 0;
if (attr->vb_index >= 0) {
/* attribute is enabled */
SOKOL_ASSERT(attr->vb_index < num_vbs);
_sg_buffer_t* vb = vbs[attr->vb_index];
SOKOL_ASSERT(vb);
gl_vb = vb->gl.buf[vb->cmn.active_slot];
vb_offset = vb_offsets[attr->vb_index] + attr->offset;
if ((gl_vb != cache_attr->gl_vbuf) ||
(attr->size != cache_attr->gl_attr.size) ||
(attr->type != cache_attr->gl_attr.type) ||
(attr->normalized != cache_attr->gl_attr.normalized) ||
(attr->stride != cache_attr->gl_attr.stride) ||
(vb_offset != cache_attr->gl_attr.offset) ||
(cache_attr->gl_attr.divisor != attr->divisor))
{
_sg_gl_cache_bind_buffer(GL_ARRAY_BUFFER, gl_vb);
glVertexAttribPointer(attr_index, attr->size, attr->type,
attr->normalized, attr->stride,
(const GLvoid*)(GLintptr)vb_offset);
#if defined(_SOKOL_GL_INSTANCING_ENABLED)
if (_sg.features.instancing) {
glVertexAttribDivisor(attr_index, (GLuint)attr->divisor);
}
#endif
cache_attr_dirty = true;
}
if (cache_attr->gl_attr.vb_index == -1) {
glEnableVertexAttribArray(attr_index);
cache_attr_dirty = true;
}
}
else {
/* attribute is disabled */
if (cache_attr->gl_attr.vb_index != -1) {
glDisableVertexAttribArray(attr_index);
cache_attr_dirty = true;
}
}
if (cache_attr_dirty) {
cache_attr->gl_attr = *attr;
cache_attr->gl_attr.offset = vb_offset;
cache_attr->gl_vbuf = gl_vb;
}
}
_SG_GL_CHECK_ERROR();
}
_SOKOL_PRIVATE void _sg_gl_apply_uniforms(sg_shader_stage stage_index, int ub_index, const sg_range* data) {
SOKOL_ASSERT(_sg.gl.cache.cur_pipeline);
SOKOL_ASSERT(_sg.gl.cache.cur_pipeline->slot.id == _sg.gl.cache.cur_pipeline_id.id);
SOKOL_ASSERT(_sg.gl.cache.cur_pipeline->shader->slot.id == _sg.gl.cache.cur_pipeline->cmn.shader_id.id);
SOKOL_ASSERT(_sg.gl.cache.cur_pipeline->shader->cmn.stage[stage_index].num_uniform_blocks > ub_index);
SOKOL_ASSERT(_sg.gl.cache.cur_pipeline->shader->cmn.stage[stage_index].uniform_blocks[ub_index].size == data->size);
const _sg_gl_shader_stage_t* gl_stage = &_sg.gl.cache.cur_pipeline->shader->gl.stage[stage_index];
const _sg_gl_uniform_block_t* gl_ub = &gl_stage->uniform_blocks[ub_index];
for (int u_index = 0; u_index < gl_ub->num_uniforms; u_index++) {
const _sg_gl_uniform_t* u = &gl_ub->uniforms[u_index];
SOKOL_ASSERT(u->type != SG_UNIFORMTYPE_INVALID);
if (u->gl_loc == -1) {
continue;
}
GLfloat* ptr = (GLfloat*) (((uint8_t*)data->ptr) + u->offset);
switch (u->type) {
case SG_UNIFORMTYPE_INVALID:
break;
case SG_UNIFORMTYPE_FLOAT:
glUniform1fv(u->gl_loc, u->count, ptr);
break;
case SG_UNIFORMTYPE_FLOAT2:
glUniform2fv(u->gl_loc, u->count, ptr);
break;
case SG_UNIFORMTYPE_FLOAT3:
glUniform3fv(u->gl_loc, u->count, ptr);
break;
case SG_UNIFORMTYPE_FLOAT4:
glUniform4fv(u->gl_loc, u->count, ptr);
break;
case SG_UNIFORMTYPE_MAT4:
glUniformMatrix4fv(u->gl_loc, u->count, GL_FALSE, ptr);
break;
default:
SOKOL_UNREACHABLE;
break;
}
}
}
_SOKOL_PRIVATE void _sg_gl_draw(int base_element, int num_elements, int num_instances) {
const GLenum i_type = _sg.gl.cache.cur_index_type;
const GLenum p_type = _sg.gl.cache.cur_primitive_type;
if (0 != i_type) {
/* indexed rendering */
const int i_size = (i_type == GL_UNSIGNED_SHORT) ? 2 : 4;
const int ib_offset = _sg.gl.cache.cur_ib_offset;
const GLvoid* indices = (const GLvoid*)(GLintptr)(base_element*i_size+ib_offset);
if (num_instances == 1) {
glDrawElements(p_type, num_elements, i_type, indices);
}
else {
if (_sg.features.instancing) {
glDrawElementsInstanced(p_type, num_elements, i_type, indices, num_instances);
}
}
}
else {
/* non-indexed rendering */
if (num_instances == 1) {
glDrawArrays(p_type, base_element, num_elements);
}
else {
if (_sg.features.instancing) {
glDrawArraysInstanced(p_type, base_element, num_elements, num_instances);
}
}
}
}
_SOKOL_PRIVATE void _sg_gl_commit(void) {
SOKOL_ASSERT(!_sg.gl.in_pass);
/* "soft" clear bindings (only those that are actually bound) */
_sg_gl_cache_clear_buffer_bindings(false);
_sg_gl_cache_clear_texture_bindings(false);
}
_SOKOL_PRIVATE void _sg_gl_update_buffer(_sg_buffer_t* buf, const sg_range* data) {
SOKOL_ASSERT(buf && data && data->ptr && (data->size > 0));
/* only one update per buffer per frame allowed */
if (++buf->cmn.active_slot >= buf->cmn.num_slots) {
buf->cmn.active_slot = 0;
}
GLenum gl_tgt = _sg_gl_buffer_target(buf->cmn.type);
SOKOL_ASSERT(buf->cmn.active_slot < SG_NUM_INFLIGHT_FRAMES);
GLuint gl_buf = buf->gl.buf[buf->cmn.active_slot];
SOKOL_ASSERT(gl_buf);
_SG_GL_CHECK_ERROR();
_sg_gl_cache_store_buffer_binding(gl_tgt);
_sg_gl_cache_bind_buffer(gl_tgt, gl_buf);
glBufferSubData(gl_tgt, 0, (GLsizeiptr)data->size, data->ptr);
_sg_gl_cache_restore_buffer_binding(gl_tgt);
_SG_GL_CHECK_ERROR();
}
_SOKOL_PRIVATE int _sg_gl_append_buffer(_sg_buffer_t* buf, const sg_range* data, bool new_frame) {
SOKOL_ASSERT(buf && data && data->ptr && (data->size > 0));
if (new_frame) {
if (++buf->cmn.active_slot >= buf->cmn.num_slots) {
buf->cmn.active_slot = 0;
}
}
GLenum gl_tgt = _sg_gl_buffer_target(buf->cmn.type);
SOKOL_ASSERT(buf->cmn.active_slot < SG_NUM_INFLIGHT_FRAMES);
GLuint gl_buf = buf->gl.buf[buf->cmn.active_slot];
SOKOL_ASSERT(gl_buf);
_SG_GL_CHECK_ERROR();
_sg_gl_cache_store_buffer_binding(gl_tgt);
_sg_gl_cache_bind_buffer(gl_tgt, gl_buf);
glBufferSubData(gl_tgt, buf->cmn.append_pos, (GLsizeiptr)data->size, data->ptr);
_sg_gl_cache_restore_buffer_binding(gl_tgt);
_SG_GL_CHECK_ERROR();
/* NOTE: this is a requirement from WebGPU, but we want identical behaviour across all backend */
return _sg_roundup((int)data->size, 4);
}
_SOKOL_PRIVATE void _sg_gl_update_image(_sg_image_t* img, const sg_image_data* data) {
SOKOL_ASSERT(img && data);
/* only one update per image per frame allowed */
if (++img->cmn.active_slot >= img->cmn.num_slots) {
img->cmn.active_slot = 0;
}
SOKOL_ASSERT(img->cmn.active_slot < SG_NUM_INFLIGHT_FRAMES);
SOKOL_ASSERT(0 != img->gl.tex[img->cmn.active_slot]);
_sg_gl_cache_store_texture_binding(0);
_sg_gl_cache_bind_texture(0, img->gl.target, img->gl.tex[img->cmn.active_slot]);
const GLenum gl_img_format = _sg_gl_teximage_format(img->cmn.pixel_format);
const GLenum gl_img_type = _sg_gl_teximage_type(img->cmn.pixel_format);
const int num_faces = img->cmn.type == SG_IMAGETYPE_CUBE ? 6 : 1;
const int num_mips = img->cmn.num_mipmaps;
for (int face_index = 0; face_index < num_faces; face_index++) {
for (int mip_index = 0; mip_index < num_mips; mip_index++) {
GLenum gl_img_target = img->gl.target;
if (SG_IMAGETYPE_CUBE == img->cmn.type) {
gl_img_target = _sg_gl_cubeface_target(face_index);
}
const GLvoid* data_ptr = data->subimage[face_index][mip_index].ptr;
int mip_width = img->cmn.width >> mip_index;
if (mip_width == 0) {
mip_width = 1;
}
int mip_height = img->cmn.height >> mip_index;
if (mip_height == 0) {
mip_height = 1;
}
if ((SG_IMAGETYPE_2D == img->cmn.type) || (SG_IMAGETYPE_CUBE == img->cmn.type)) {
glTexSubImage2D(gl_img_target, mip_index,
0, 0,
mip_width, mip_height,
gl_img_format, gl_img_type,
data_ptr);
}
#if !defined(SOKOL_GLES2)
else if (!_sg.gl.gles2 && ((SG_IMAGETYPE_3D == img->cmn.type) || (SG_IMAGETYPE_ARRAY == img->cmn.type))) {
int mip_depth = img->cmn.num_slices >> mip_index;
if (mip_depth == 0) {
mip_depth = 1;
}
glTexSubImage3D(gl_img_target, mip_index,
0, 0, 0,
mip_width, mip_height, mip_depth,
gl_img_format, gl_img_type,
data_ptr);
}
#endif
}
}
_sg_gl_cache_restore_texture_binding(0);
}
/*== D3D11 BACKEND IMPLEMENTATION ============================================*/
#elif defined(SOKOL_D3D11)
#if defined(__cplusplus)
#define _sg_d3d11_AddRef(self) (self)->AddRef()
#else
#define _sg_d3d11_AddRef(self) (self)->lpVtbl->AddRef(self)
#endif
#if defined(__cplusplus)
#define _sg_d3d11_Release(self) (self)->Release()
#else
#define _sg_d3d11_Release(self) (self)->lpVtbl->Release(self)
#endif
/*-- D3D11 C/C++ wrappers ----------------------------------------------------*/
static inline HRESULT _sg_d3d11_CheckFormatSupport(ID3D11Device* self, DXGI_FORMAT Format, UINT* pFormatSupport) {
#if defined(__cplusplus)
return self->CheckFormatSupport(Format, pFormatSupport);
#else
return self->lpVtbl->CheckFormatSupport(self, Format, pFormatSupport);
#endif
}
static inline void _sg_d3d11_OMSetRenderTargets(ID3D11DeviceContext* self, UINT NumViews, ID3D11RenderTargetView* const* ppRenderTargetViews, ID3D11DepthStencilView *pDepthStencilView) {
#if defined(__cplusplus)
self->OMSetRenderTargets(NumViews, ppRenderTargetViews, pDepthStencilView);
#else
self->lpVtbl->OMSetRenderTargets(self, NumViews, ppRenderTargetViews, pDepthStencilView);
#endif
}
static inline void _sg_d3d11_RSSetState(ID3D11DeviceContext* self, ID3D11RasterizerState* pRasterizerState) {
#if defined(__cplusplus)
self->RSSetState(pRasterizerState);
#else
self->lpVtbl->RSSetState(self, pRasterizerState);
#endif
}
static inline void _sg_d3d11_OMSetDepthStencilState(ID3D11DeviceContext* self, ID3D11DepthStencilState* pDepthStencilState, UINT StencilRef) {
#if defined(__cplusplus)
self->OMSetDepthStencilState(pDepthStencilState, StencilRef);
#else
self->lpVtbl->OMSetDepthStencilState(self, pDepthStencilState, StencilRef);
#endif
}
static inline void _sg_d3d11_OMSetBlendState(ID3D11DeviceContext* self, ID3D11BlendState* pBlendState, const FLOAT BlendFactor[4], UINT SampleMask) {
#if defined(__cplusplus)
self->OMSetBlendState(pBlendState, BlendFactor, SampleMask);
#else
self->lpVtbl->OMSetBlendState(self, pBlendState, BlendFactor, SampleMask);
#endif
}
static inline void _sg_d3d11_IASetVertexBuffers(ID3D11DeviceContext* self, UINT StartSlot, UINT NumBuffers, ID3D11Buffer* const* ppVertexBuffers, const UINT* pStrides, const UINT* pOffsets) {
#if defined(__cplusplus)
self->IASetVertexBuffers(StartSlot, NumBuffers, ppVertexBuffers, pStrides, pOffsets);
#else
self->lpVtbl->IASetVertexBuffers(self, StartSlot, NumBuffers, ppVertexBuffers, pStrides, pOffsets);
#endif
}
static inline void _sg_d3d11_IASetIndexBuffer(ID3D11DeviceContext* self, ID3D11Buffer* pIndexBuffer, DXGI_FORMAT Format, UINT Offset) {
#if defined(__cplusplus)
self->IASetIndexBuffer(pIndexBuffer, Format, Offset);
#else
self->lpVtbl->IASetIndexBuffer(self, pIndexBuffer, Format, Offset);
#endif
}
static inline void _sg_d3d11_IASetInputLayout(ID3D11DeviceContext* self, ID3D11InputLayout* pInputLayout) {
#if defined(__cplusplus)
self->IASetInputLayout(pInputLayout);
#else
self->lpVtbl->IASetInputLayout(self, pInputLayout);
#endif
}
static inline void _sg_d3d11_VSSetShader(ID3D11DeviceContext* self, ID3D11VertexShader* pVertexShader, ID3D11ClassInstance* const* ppClassInstances, UINT NumClassInstances) {
#if defined(__cplusplus)
self->VSSetShader(pVertexShader, ppClassInstances, NumClassInstances);
#else
self->lpVtbl->VSSetShader(self, pVertexShader, ppClassInstances, NumClassInstances);
#endif
}
static inline void _sg_d3d11_PSSetShader(ID3D11DeviceContext* self, ID3D11PixelShader* pPixelShader, ID3D11ClassInstance* const* ppClassInstances, UINT NumClassInstances) {
#if defined(__cplusplus)
self->PSSetShader(pPixelShader, ppClassInstances, NumClassInstances);
#else
self->lpVtbl->PSSetShader(self, pPixelShader, ppClassInstances, NumClassInstances);
#endif
}
static inline void _sg_d3d11_VSSetConstantBuffers(ID3D11DeviceContext* self, UINT StartSlot, UINT NumBuffers, ID3D11Buffer* const* ppConstantBuffers) {
#if defined(__cplusplus)
self->VSSetConstantBuffers(StartSlot, NumBuffers, ppConstantBuffers);
#else
self->lpVtbl->VSSetConstantBuffers(self, StartSlot, NumBuffers, ppConstantBuffers);
#endif
}
static inline void _sg_d3d11_PSSetConstantBuffers(ID3D11DeviceContext* self, UINT StartSlot, UINT NumBuffers, ID3D11Buffer* const* ppConstantBuffers) {
#if defined(__cplusplus)
self->PSSetConstantBuffers(StartSlot, NumBuffers, ppConstantBuffers);
#else
self->lpVtbl->PSSetConstantBuffers(self, StartSlot, NumBuffers, ppConstantBuffers);
#endif
}
static inline void _sg_d3d11_VSSetShaderResources(ID3D11DeviceContext* self, UINT StartSlot, UINT NumViews, ID3D11ShaderResourceView* const* ppShaderResourceViews) {
#if defined(__cplusplus)
self->VSSetShaderResources(StartSlot, NumViews, ppShaderResourceViews);
#else
self->lpVtbl->VSSetShaderResources(self, StartSlot, NumViews, ppShaderResourceViews);
#endif
}
static inline void _sg_d3d11_PSSetShaderResources(ID3D11DeviceContext* self, UINT StartSlot, UINT NumViews, ID3D11ShaderResourceView* const* ppShaderResourceViews) {
#if defined(__cplusplus)
self->PSSetShaderResources(StartSlot, NumViews, ppShaderResourceViews);
#else
self->lpVtbl->PSSetShaderResources(self, StartSlot, NumViews, ppShaderResourceViews);
#endif
}
static inline void _sg_d3d11_VSSetSamplers(ID3D11DeviceContext* self, UINT StartSlot, UINT NumSamplers, ID3D11SamplerState* const* ppSamplers) {
#if defined(__cplusplus)
self->VSSetSamplers(StartSlot, NumSamplers, ppSamplers);
#else
self->lpVtbl->VSSetSamplers(self, StartSlot, NumSamplers, ppSamplers);
#endif
}
static inline void _sg_d3d11_PSSetSamplers(ID3D11DeviceContext* self, UINT StartSlot, UINT NumSamplers, ID3D11SamplerState* const* ppSamplers) {
#if defined(__cplusplus)
self->PSSetSamplers(StartSlot, NumSamplers, ppSamplers);
#else
self->lpVtbl->PSSetSamplers(self, StartSlot, NumSamplers, ppSamplers);
#endif
}
static inline HRESULT _sg_d3d11_CreateBuffer(ID3D11Device* self, const D3D11_BUFFER_DESC* pDesc, const D3D11_SUBRESOURCE_DATA* pInitialData, ID3D11Buffer** ppBuffer) {
#if defined(__cplusplus)
return self->CreateBuffer(pDesc, pInitialData, ppBuffer);
#else
return self->lpVtbl->CreateBuffer(self, pDesc, pInitialData, ppBuffer);
#endif
}
static inline HRESULT _sg_d3d11_CreateTexture2D(ID3D11Device* self, const D3D11_TEXTURE2D_DESC* pDesc, const D3D11_SUBRESOURCE_DATA* pInitialData, ID3D11Texture2D** ppTexture2D) {
#if defined(__cplusplus)
return self->CreateTexture2D(pDesc, pInitialData, ppTexture2D);
#else
return self->lpVtbl->CreateTexture2D(self, pDesc, pInitialData, ppTexture2D);
#endif
}
static inline HRESULT _sg_d3d11_CreateShaderResourceView(ID3D11Device* self, ID3D11Resource* pResource, const D3D11_SHADER_RESOURCE_VIEW_DESC* pDesc, ID3D11ShaderResourceView** ppSRView) {
#if defined(__cplusplus)
return self->CreateShaderResourceView(pResource, pDesc, ppSRView);
#else
return self->lpVtbl->CreateShaderResourceView(self, pResource, pDesc, ppSRView);
#endif
}
static inline void _sg_d3d11_GetResource(ID3D11View* self, ID3D11Resource** ppResource) {
#if defined(__cplusplus)
self->GetResource(ppResource);
#else
self->lpVtbl->GetResource(self, ppResource);
#endif
}
static inline HRESULT _sg_d3d11_CreateTexture3D(ID3D11Device* self, const D3D11_TEXTURE3D_DESC* pDesc, const D3D11_SUBRESOURCE_DATA* pInitialData, ID3D11Texture3D** ppTexture3D) {
#if defined(__cplusplus)
return self->CreateTexture3D(pDesc, pInitialData, ppTexture3D);
#else
return self->lpVtbl->CreateTexture3D(self, pDesc, pInitialData, ppTexture3D);
#endif
}
static inline HRESULT _sg_d3d11_CreateSamplerState(ID3D11Device* self, const D3D11_SAMPLER_DESC* pSamplerDesc, ID3D11SamplerState** ppSamplerState) {
#if defined(__cplusplus)
return self->CreateSamplerState(pSamplerDesc, ppSamplerState);
#else
return self->lpVtbl->CreateSamplerState(self, pSamplerDesc, ppSamplerState);
#endif
}
static inline LPVOID _sg_d3d11_GetBufferPointer(ID3D10Blob* self) {
#if defined(__cplusplus)
return self->GetBufferPointer();
#else
return self->lpVtbl->GetBufferPointer(self);
#endif
}
static inline SIZE_T _sg_d3d11_GetBufferSize(ID3D10Blob* self) {
#if defined(__cplusplus)
return self->GetBufferSize();
#else
return self->lpVtbl->GetBufferSize(self);
#endif
}
static inline HRESULT _sg_d3d11_CreateVertexShader(ID3D11Device* self, const void* pShaderBytecode, SIZE_T BytecodeLength, ID3D11ClassLinkage* pClassLinkage, ID3D11VertexShader** ppVertexShader) {
#if defined(__cplusplus)
return self->CreateVertexShader(pShaderBytecode, BytecodeLength, pClassLinkage, ppVertexShader);
#else
return self->lpVtbl->CreateVertexShader(self, pShaderBytecode, BytecodeLength, pClassLinkage, ppVertexShader);
#endif
}
static inline HRESULT _sg_d3d11_CreatePixelShader(ID3D11Device* self, const void* pShaderBytecode, SIZE_T BytecodeLength, ID3D11ClassLinkage* pClassLinkage, ID3D11PixelShader** ppPixelShader) {
#if defined(__cplusplus)
return self->CreatePixelShader(pShaderBytecode, BytecodeLength, pClassLinkage, ppPixelShader);
#else
return self->lpVtbl->CreatePixelShader(self, pShaderBytecode, BytecodeLength, pClassLinkage, ppPixelShader);
#endif
}
static inline HRESULT _sg_d3d11_CreateInputLayout(ID3D11Device* self, const D3D11_INPUT_ELEMENT_DESC* pInputElementDescs, UINT NumElements, const void* pShaderBytecodeWithInputSignature, SIZE_T BytecodeLength, ID3D11InputLayout **ppInputLayout) {
#if defined(__cplusplus)
return self->CreateInputLayout(pInputElementDescs, NumElements, pShaderBytecodeWithInputSignature, BytecodeLength, ppInputLayout);
#else
return self->lpVtbl->CreateInputLayout(self, pInputElementDescs, NumElements, pShaderBytecodeWithInputSignature, BytecodeLength, ppInputLayout);
#endif
}
static inline HRESULT _sg_d3d11_CreateRasterizerState(ID3D11Device* self, const D3D11_RASTERIZER_DESC* pRasterizerDesc, ID3D11RasterizerState** ppRasterizerState) {
#if defined(__cplusplus)
return self->CreateRasterizerState(pRasterizerDesc, ppRasterizerState);
#else
return self->lpVtbl->CreateRasterizerState(self, pRasterizerDesc, ppRasterizerState);
#endif
}
static inline HRESULT _sg_d3d11_CreateDepthStencilState(ID3D11Device* self, const D3D11_DEPTH_STENCIL_DESC* pDepthStencilDesc, ID3D11DepthStencilState** ppDepthStencilState) {
#if defined(__cplusplus)
return self->CreateDepthStencilState(pDepthStencilDesc, ppDepthStencilState);
#else
return self->lpVtbl->CreateDepthStencilState(self, pDepthStencilDesc, ppDepthStencilState);
#endif
}
static inline HRESULT _sg_d3d11_CreateBlendState(ID3D11Device* self, const D3D11_BLEND_DESC* pBlendStateDesc, ID3D11BlendState** ppBlendState) {
#if defined(__cplusplus)
return self->CreateBlendState(pBlendStateDesc, ppBlendState);
#else
return self->lpVtbl->CreateBlendState(self, pBlendStateDesc, ppBlendState);
#endif
}
static inline HRESULT _sg_d3d11_CreateRenderTargetView(ID3D11Device* self, ID3D11Resource *pResource, const D3D11_RENDER_TARGET_VIEW_DESC* pDesc, ID3D11RenderTargetView** ppRTView) {
#if defined(__cplusplus)
return self->CreateRenderTargetView(pResource, pDesc, ppRTView);
#else
return self->lpVtbl->CreateRenderTargetView(self, pResource, pDesc, ppRTView);
#endif
}
static inline HRESULT _sg_d3d11_CreateDepthStencilView(ID3D11Device* self, ID3D11Resource* pResource, const D3D11_DEPTH_STENCIL_VIEW_DESC* pDesc, ID3D11DepthStencilView** ppDepthStencilView) {
#if defined(__cplusplus)
return self->CreateDepthStencilView(pResource, pDesc, ppDepthStencilView);
#else
return self->lpVtbl->CreateDepthStencilView(self, pResource, pDesc, ppDepthStencilView);
#endif
}
static inline void _sg_d3d11_RSSetViewports(ID3D11DeviceContext* self, UINT NumViewports, const D3D11_VIEWPORT* pViewports) {
#if defined(__cplusplus)
self->RSSetViewports(NumViewports, pViewports);
#else
self->lpVtbl->RSSetViewports(self, NumViewports, pViewports);
#endif
}
static inline void _sg_d3d11_RSSetScissorRects(ID3D11DeviceContext* self, UINT NumRects, const D3D11_RECT* pRects) {
#if defined(__cplusplus)
self->RSSetScissorRects(NumRects, pRects);
#else
self->lpVtbl->RSSetScissorRects(self, NumRects, pRects);
#endif
}
static inline void _sg_d3d11_ClearRenderTargetView(ID3D11DeviceContext* self, ID3D11RenderTargetView* pRenderTargetView, const FLOAT ColorRGBA[4]) {
#if defined(__cplusplus)
self->ClearRenderTargetView(pRenderTargetView, ColorRGBA);
#else
self->lpVtbl->ClearRenderTargetView(self, pRenderTargetView, ColorRGBA);
#endif
}
static inline void _sg_d3d11_ClearDepthStencilView(ID3D11DeviceContext* self, ID3D11DepthStencilView* pDepthStencilView, UINT ClearFlags, FLOAT Depth, UINT8 Stencil) {
#if defined(__cplusplus)
self->ClearDepthStencilView(pDepthStencilView, ClearFlags, Depth, Stencil);
#else
self->lpVtbl->ClearDepthStencilView(self, pDepthStencilView, ClearFlags, Depth, Stencil);
#endif
}
static inline void _sg_d3d11_ResolveSubresource(ID3D11DeviceContext* self, ID3D11Resource* pDstResource, UINT DstSubresource, ID3D11Resource* pSrcResource, UINT SrcSubresource, DXGI_FORMAT Format) {
#if defined(__cplusplus)
self->ResolveSubresource(pDstResource, DstSubresource, pSrcResource, SrcSubresource, Format);
#else
self->lpVtbl->ResolveSubresource(self, pDstResource, DstSubresource, pSrcResource, SrcSubresource, Format);
#endif
}
static inline void _sg_d3d11_IASetPrimitiveTopology(ID3D11DeviceContext* self, D3D11_PRIMITIVE_TOPOLOGY Topology) {
#if defined(__cplusplus)
self->IASetPrimitiveTopology(Topology);
#else
self->lpVtbl->IASetPrimitiveTopology(self, Topology);
#endif
}
static inline void _sg_d3d11_UpdateSubresource(ID3D11DeviceContext* self, ID3D11Resource* pDstResource, UINT DstSubresource, const D3D11_BOX* pDstBox, const void* pSrcData, UINT SrcRowPitch, UINT SrcDepthPitch) {
#if defined(__cplusplus)
self->UpdateSubresource(pDstResource, DstSubresource, pDstBox, pSrcData, SrcRowPitch, SrcDepthPitch);
#else
self->lpVtbl->UpdateSubresource(self, pDstResource, DstSubresource, pDstBox, pSrcData, SrcRowPitch, SrcDepthPitch);
#endif
}
static inline void _sg_d3d11_DrawIndexed(ID3D11DeviceContext* self, UINT IndexCount, UINT StartIndexLocation, INT BaseVertexLocation) {
#if defined(__cplusplus)
self->DrawIndexed(IndexCount, StartIndexLocation, BaseVertexLocation);
#else
self->lpVtbl->DrawIndexed(self, IndexCount, StartIndexLocation, BaseVertexLocation);
#endif
}
static inline void _sg_d3d11_DrawIndexedInstanced(ID3D11DeviceContext* self, UINT IndexCountPerInstance, UINT InstanceCount, UINT StartIndexLocation, INT BaseVertexLocation, UINT StartInstanceLocation) {
#if defined(__cplusplus)
self->DrawIndexedInstanced(IndexCountPerInstance, InstanceCount, StartIndexLocation, BaseVertexLocation, StartInstanceLocation);
#else
self->lpVtbl->DrawIndexedInstanced(self, IndexCountPerInstance, InstanceCount, StartIndexLocation, BaseVertexLocation, StartInstanceLocation);
#endif
}
static inline void _sg_d3d11_Draw(ID3D11DeviceContext* self, UINT VertexCount, UINT StartVertexLocation) {
#if defined(__cplusplus)
self->Draw(VertexCount, StartVertexLocation);
#else
self->lpVtbl->Draw(self, VertexCount, StartVertexLocation);
#endif
}
static inline void _sg_d3d11_DrawInstanced(ID3D11DeviceContext* self, UINT VertexCountPerInstance, UINT InstanceCount, UINT StartVertexLocation, UINT StartInstanceLocation) {
#if defined(__cplusplus)
self->DrawInstanced(VertexCountPerInstance, InstanceCount, StartVertexLocation, StartInstanceLocation);
#else
self->lpVtbl->DrawInstanced(self, VertexCountPerInstance, InstanceCount, StartVertexLocation, StartInstanceLocation);
#endif
}
static inline HRESULT _sg_d3d11_Map(ID3D11DeviceContext* self, ID3D11Resource* pResource, UINT Subresource, D3D11_MAP MapType, UINT MapFlags, D3D11_MAPPED_SUBRESOURCE* pMappedResource) {
#if defined(__cplusplus)
return self->Map(pResource, Subresource, MapType, MapFlags, pMappedResource);
#else
return self->lpVtbl->Map(self, pResource, Subresource, MapType, MapFlags, pMappedResource);
#endif
}
static inline void _sg_d3d11_Unmap(ID3D11DeviceContext* self, ID3D11Resource* pResource, UINT Subresource) {
#if defined(__cplusplus)
self->Unmap(pResource, Subresource);
#else
self->lpVtbl->Unmap(self, pResource, Subresource);
#endif
}
/*-- enum translation functions ----------------------------------------------*/
_SOKOL_PRIVATE D3D11_USAGE _sg_d3d11_usage(sg_usage usg) {
switch (usg) {
case SG_USAGE_IMMUTABLE:
return D3D11_USAGE_IMMUTABLE;
case SG_USAGE_DYNAMIC:
case SG_USAGE_STREAM:
return D3D11_USAGE_DYNAMIC;
default:
SOKOL_UNREACHABLE;
return (D3D11_USAGE) 0;
}
}
_SOKOL_PRIVATE UINT _sg_d3d11_cpu_access_flags(sg_usage usg) {
switch (usg) {
case SG_USAGE_IMMUTABLE:
return 0;
case SG_USAGE_DYNAMIC:
case SG_USAGE_STREAM:
return D3D11_CPU_ACCESS_WRITE;
default:
SOKOL_UNREACHABLE;
return 0;
}
}
_SOKOL_PRIVATE DXGI_FORMAT _sg_d3d11_pixel_format(sg_pixel_format fmt) {
switch (fmt) {
case SG_PIXELFORMAT_R8: return DXGI_FORMAT_R8_UNORM;
case SG_PIXELFORMAT_R8SN: return DXGI_FORMAT_R8_SNORM;
case SG_PIXELFORMAT_R8UI: return DXGI_FORMAT_R8_UINT;
case SG_PIXELFORMAT_R8SI: return DXGI_FORMAT_R8_SINT;
case SG_PIXELFORMAT_R16: return DXGI_FORMAT_R16_UNORM;
case SG_PIXELFORMAT_R16SN: return DXGI_FORMAT_R16_SNORM;
case SG_PIXELFORMAT_R16UI: return DXGI_FORMAT_R16_UINT;
case SG_PIXELFORMAT_R16SI: return DXGI_FORMAT_R16_SINT;
case SG_PIXELFORMAT_R16F: return DXGI_FORMAT_R16_FLOAT;
case SG_PIXELFORMAT_RG8: return DXGI_FORMAT_R8G8_UNORM;
case SG_PIXELFORMAT_RG8SN: return DXGI_FORMAT_R8G8_SNORM;
case SG_PIXELFORMAT_RG8UI: return DXGI_FORMAT_R8G8_UINT;
case SG_PIXELFORMAT_RG8SI: return DXGI_FORMAT_R8G8_SINT;
case SG_PIXELFORMAT_R32UI: return DXGI_FORMAT_R32_UINT;
case SG_PIXELFORMAT_R32SI: return DXGI_FORMAT_R32_SINT;
case SG_PIXELFORMAT_R32F: return DXGI_FORMAT_R32_FLOAT;
case SG_PIXELFORMAT_RG16: return DXGI_FORMAT_R16G16_UNORM;
case SG_PIXELFORMAT_RG16SN: return DXGI_FORMAT_R16G16_SNORM;
case SG_PIXELFORMAT_RG16UI: return DXGI_FORMAT_R16G16_UINT;
case SG_PIXELFORMAT_RG16SI: return DXGI_FORMAT_R16G16_SINT;
case SG_PIXELFORMAT_RG16F: return DXGI_FORMAT_R16G16_FLOAT;
case SG_PIXELFORMAT_RGBA8: return DXGI_FORMAT_R8G8B8A8_UNORM;
case SG_PIXELFORMAT_RGBA8SN: return DXGI_FORMAT_R8G8B8A8_SNORM;
case SG_PIXELFORMAT_RGBA8UI: return DXGI_FORMAT_R8G8B8A8_UINT;
case SG_PIXELFORMAT_RGBA8SI: return DXGI_FORMAT_R8G8B8A8_SINT;
case SG_PIXELFORMAT_BGRA8: return DXGI_FORMAT_B8G8R8A8_UNORM;
case SG_PIXELFORMAT_RGB10A2: return DXGI_FORMAT_R10G10B10A2_UNORM;
case SG_PIXELFORMAT_RG11B10F: return DXGI_FORMAT_R11G11B10_FLOAT;
case SG_PIXELFORMAT_RG32UI: return DXGI_FORMAT_R32G32_UINT;
case SG_PIXELFORMAT_RG32SI: return DXGI_FORMAT_R32G32_SINT;
case SG_PIXELFORMAT_RG32F: return DXGI_FORMAT_R32G32_FLOAT;
case SG_PIXELFORMAT_RGBA16: return DXGI_FORMAT_R16G16B16A16_UNORM;
case SG_PIXELFORMAT_RGBA16SN: return DXGI_FORMAT_R16G16B16A16_SNORM;
case SG_PIXELFORMAT_RGBA16UI: return DXGI_FORMAT_R16G16B16A16_UINT;
case SG_PIXELFORMAT_RGBA16SI: return DXGI_FORMAT_R16G16B16A16_SINT;
case SG_PIXELFORMAT_RGBA16F: return DXGI_FORMAT_R16G16B16A16_FLOAT;
case SG_PIXELFORMAT_RGBA32UI: return DXGI_FORMAT_R32G32B32A32_UINT;
case SG_PIXELFORMAT_RGBA32SI: return DXGI_FORMAT_R32G32B32A32_SINT;
case SG_PIXELFORMAT_RGBA32F: return DXGI_FORMAT_R32G32B32A32_FLOAT;
case SG_PIXELFORMAT_DEPTH: return DXGI_FORMAT_D32_FLOAT;
case SG_PIXELFORMAT_DEPTH_STENCIL: return DXGI_FORMAT_D24_UNORM_S8_UINT;
case SG_PIXELFORMAT_BC1_RGBA: return DXGI_FORMAT_BC1_UNORM;
case SG_PIXELFORMAT_BC2_RGBA: return DXGI_FORMAT_BC2_UNORM;
case SG_PIXELFORMAT_BC3_RGBA: return DXGI_FORMAT_BC3_UNORM;
case SG_PIXELFORMAT_BC4_R: return DXGI_FORMAT_BC4_UNORM;
case SG_PIXELFORMAT_BC4_RSN: return DXGI_FORMAT_BC4_SNORM;
case SG_PIXELFORMAT_BC5_RG: return DXGI_FORMAT_BC5_UNORM;
case SG_PIXELFORMAT_BC5_RGSN: return DXGI_FORMAT_BC5_SNORM;
case SG_PIXELFORMAT_BC6H_RGBF: return DXGI_FORMAT_BC6H_SF16;
case SG_PIXELFORMAT_BC6H_RGBUF: return DXGI_FORMAT_BC6H_UF16;
case SG_PIXELFORMAT_BC7_RGBA: return DXGI_FORMAT_BC7_UNORM;
default: return DXGI_FORMAT_UNKNOWN;
};
}
_SOKOL_PRIVATE D3D11_PRIMITIVE_TOPOLOGY _sg_d3d11_primitive_topology(sg_primitive_type prim_type) {
switch (prim_type) {
case SG_PRIMITIVETYPE_POINTS: return D3D11_PRIMITIVE_TOPOLOGY_POINTLIST;
case SG_PRIMITIVETYPE_LINES: return D3D11_PRIMITIVE_TOPOLOGY_LINELIST;
case SG_PRIMITIVETYPE_LINE_STRIP: return D3D11_PRIMITIVE_TOPOLOGY_LINESTRIP;
case SG_PRIMITIVETYPE_TRIANGLES: return D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST;
case SG_PRIMITIVETYPE_TRIANGLE_STRIP: return D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP;
default: SOKOL_UNREACHABLE; return (D3D11_PRIMITIVE_TOPOLOGY) 0;
}
}
_SOKOL_PRIVATE DXGI_FORMAT _sg_d3d11_index_format(sg_index_type index_type) {
switch (index_type) {
case SG_INDEXTYPE_NONE: return DXGI_FORMAT_UNKNOWN;
case SG_INDEXTYPE_UINT16: return DXGI_FORMAT_R16_UINT;
case SG_INDEXTYPE_UINT32: return DXGI_FORMAT_R32_UINT;
default: SOKOL_UNREACHABLE; return (DXGI_FORMAT) 0;
}
}
_SOKOL_PRIVATE D3D11_FILTER _sg_d3d11_filter(sg_filter min_f, sg_filter mag_f, uint32_t max_anisotropy) {
if (max_anisotropy > 1) {
return D3D11_FILTER_ANISOTROPIC;
}
else if (mag_f == SG_FILTER_NEAREST) {
switch (min_f) {
case SG_FILTER_NEAREST:
case SG_FILTER_NEAREST_MIPMAP_NEAREST:
return D3D11_FILTER_MIN_MAG_MIP_POINT;
case SG_FILTER_LINEAR:
case SG_FILTER_LINEAR_MIPMAP_NEAREST:
return D3D11_FILTER_MIN_LINEAR_MAG_MIP_POINT;
case SG_FILTER_NEAREST_MIPMAP_LINEAR:
return D3D11_FILTER_MIN_MAG_POINT_MIP_LINEAR;
case SG_FILTER_LINEAR_MIPMAP_LINEAR:
return D3D11_FILTER_MIN_LINEAR_MAG_POINT_MIP_LINEAR;
default:
SOKOL_UNREACHABLE; break;
}
}
else if (mag_f == SG_FILTER_LINEAR) {
switch (min_f) {
case SG_FILTER_NEAREST:
case SG_FILTER_NEAREST_MIPMAP_NEAREST:
return D3D11_FILTER_MIN_POINT_MAG_LINEAR_MIP_POINT;
case SG_FILTER_LINEAR:
case SG_FILTER_LINEAR_MIPMAP_NEAREST:
return D3D11_FILTER_MIN_MAG_LINEAR_MIP_POINT;
case SG_FILTER_NEAREST_MIPMAP_LINEAR:
return D3D11_FILTER_MIN_POINT_MAG_MIP_LINEAR;
case SG_FILTER_LINEAR_MIPMAP_LINEAR:
return D3D11_FILTER_MIN_MAG_MIP_LINEAR;
default:
SOKOL_UNREACHABLE; break;
}
}
/* invalid value for mag filter */
SOKOL_UNREACHABLE;
return D3D11_FILTER_MIN_MAG_MIP_POINT;
}
_SOKOL_PRIVATE D3D11_TEXTURE_ADDRESS_MODE _sg_d3d11_address_mode(sg_wrap m) {
switch (m) {
case SG_WRAP_REPEAT: return D3D11_TEXTURE_ADDRESS_WRAP;
case SG_WRAP_CLAMP_TO_EDGE: return D3D11_TEXTURE_ADDRESS_CLAMP;
case SG_WRAP_CLAMP_TO_BORDER: return D3D11_TEXTURE_ADDRESS_BORDER;
case SG_WRAP_MIRRORED_REPEAT: return D3D11_TEXTURE_ADDRESS_MIRROR;
default: SOKOL_UNREACHABLE; return (D3D11_TEXTURE_ADDRESS_MODE) 0;
}
}
_SOKOL_PRIVATE DXGI_FORMAT _sg_d3d11_vertex_format(sg_vertex_format fmt) {
switch (fmt) {
case SG_VERTEXFORMAT_FLOAT: return DXGI_FORMAT_R32_FLOAT;
case SG_VERTEXFORMAT_FLOAT2: return DXGI_FORMAT_R32G32_FLOAT;
case SG_VERTEXFORMAT_FLOAT3: return DXGI_FORMAT_R32G32B32_FLOAT;
case SG_VERTEXFORMAT_FLOAT4: return DXGI_FORMAT_R32G32B32A32_FLOAT;
case SG_VERTEXFORMAT_BYTE4: return DXGI_FORMAT_R8G8B8A8_SINT;
case SG_VERTEXFORMAT_BYTE4N: return DXGI_FORMAT_R8G8B8A8_SNORM;
case SG_VERTEXFORMAT_UBYTE4: return DXGI_FORMAT_R8G8B8A8_UINT;
case SG_VERTEXFORMAT_UBYTE4N: return DXGI_FORMAT_R8G8B8A8_UNORM;
case SG_VERTEXFORMAT_SHORT2: return DXGI_FORMAT_R16G16_SINT;
case SG_VERTEXFORMAT_SHORT2N: return DXGI_FORMAT_R16G16_SNORM;
case SG_VERTEXFORMAT_USHORT2N: return DXGI_FORMAT_R16G16_UNORM;
case SG_VERTEXFORMAT_SHORT4: return DXGI_FORMAT_R16G16B16A16_SINT;
case SG_VERTEXFORMAT_SHORT4N: return DXGI_FORMAT_R16G16B16A16_SNORM;
case SG_VERTEXFORMAT_USHORT4N: return DXGI_FORMAT_R16G16B16A16_UNORM;
case SG_VERTEXFORMAT_UINT10_N2: return DXGI_FORMAT_R10G10B10A2_UNORM;
default: SOKOL_UNREACHABLE; return (DXGI_FORMAT) 0;
}
}
_SOKOL_PRIVATE D3D11_INPUT_CLASSIFICATION _sg_d3d11_input_classification(sg_vertex_step step) {
switch (step) {
case SG_VERTEXSTEP_PER_VERTEX: return D3D11_INPUT_PER_VERTEX_DATA;
case SG_VERTEXSTEP_PER_INSTANCE: return D3D11_INPUT_PER_INSTANCE_DATA;
default: SOKOL_UNREACHABLE; return (D3D11_INPUT_CLASSIFICATION) 0;
}
}
_SOKOL_PRIVATE D3D11_CULL_MODE _sg_d3d11_cull_mode(sg_cull_mode m) {
switch (m) {
case SG_CULLMODE_NONE: return D3D11_CULL_NONE;
case SG_CULLMODE_FRONT: return D3D11_CULL_FRONT;
case SG_CULLMODE_BACK: return D3D11_CULL_BACK;
default: SOKOL_UNREACHABLE; return (D3D11_CULL_MODE) 0;
}
}
_SOKOL_PRIVATE D3D11_COMPARISON_FUNC _sg_d3d11_compare_func(sg_compare_func f) {
switch (f) {
case SG_COMPAREFUNC_NEVER: return D3D11_COMPARISON_NEVER;
case SG_COMPAREFUNC_LESS: return D3D11_COMPARISON_LESS;
case SG_COMPAREFUNC_EQUAL: return D3D11_COMPARISON_EQUAL;
case SG_COMPAREFUNC_LESS_EQUAL: return D3D11_COMPARISON_LESS_EQUAL;
case SG_COMPAREFUNC_GREATER: return D3D11_COMPARISON_GREATER;
case SG_COMPAREFUNC_NOT_EQUAL: return D3D11_COMPARISON_NOT_EQUAL;
case SG_COMPAREFUNC_GREATER_EQUAL: return D3D11_COMPARISON_GREATER_EQUAL;
case SG_COMPAREFUNC_ALWAYS: return D3D11_COMPARISON_ALWAYS;
default: SOKOL_UNREACHABLE; return (D3D11_COMPARISON_FUNC) 0;
}
}
_SOKOL_PRIVATE D3D11_STENCIL_OP _sg_d3d11_stencil_op(sg_stencil_op op) {
switch (op) {
case SG_STENCILOP_KEEP: return D3D11_STENCIL_OP_KEEP;
case SG_STENCILOP_ZERO: return D3D11_STENCIL_OP_ZERO;
case SG_STENCILOP_REPLACE: return D3D11_STENCIL_OP_REPLACE;
case SG_STENCILOP_INCR_CLAMP: return D3D11_STENCIL_OP_INCR_SAT;
case SG_STENCILOP_DECR_CLAMP: return D3D11_STENCIL_OP_DECR_SAT;
case SG_STENCILOP_INVERT: return D3D11_STENCIL_OP_INVERT;
case SG_STENCILOP_INCR_WRAP: return D3D11_STENCIL_OP_INCR;
case SG_STENCILOP_DECR_WRAP: return D3D11_STENCIL_OP_DECR;
default: SOKOL_UNREACHABLE; return (D3D11_STENCIL_OP) 0;
}
}
_SOKOL_PRIVATE D3D11_BLEND _sg_d3d11_blend_factor(sg_blend_factor f) {
switch (f) {
case SG_BLENDFACTOR_ZERO: return D3D11_BLEND_ZERO;
case SG_BLENDFACTOR_ONE: return D3D11_BLEND_ONE;
case SG_BLENDFACTOR_SRC_COLOR: return D3D11_BLEND_SRC_COLOR;
case SG_BLENDFACTOR_ONE_MINUS_SRC_COLOR: return D3D11_BLEND_INV_SRC_COLOR;
case SG_BLENDFACTOR_SRC_ALPHA: return D3D11_BLEND_SRC_ALPHA;
case SG_BLENDFACTOR_ONE_MINUS_SRC_ALPHA: return D3D11_BLEND_INV_SRC_ALPHA;
case SG_BLENDFACTOR_DST_COLOR: return D3D11_BLEND_DEST_COLOR;
case SG_BLENDFACTOR_ONE_MINUS_DST_COLOR: return D3D11_BLEND_INV_DEST_COLOR;
case SG_BLENDFACTOR_DST_ALPHA: return D3D11_BLEND_DEST_ALPHA;
case SG_BLENDFACTOR_ONE_MINUS_DST_ALPHA: return D3D11_BLEND_INV_DEST_ALPHA;
case SG_BLENDFACTOR_SRC_ALPHA_SATURATED: return D3D11_BLEND_SRC_ALPHA_SAT;
case SG_BLENDFACTOR_BLEND_COLOR: return D3D11_BLEND_BLEND_FACTOR;
case SG_BLENDFACTOR_ONE_MINUS_BLEND_COLOR: return D3D11_BLEND_INV_BLEND_FACTOR;
case SG_BLENDFACTOR_BLEND_ALPHA: return D3D11_BLEND_BLEND_FACTOR;
case SG_BLENDFACTOR_ONE_MINUS_BLEND_ALPHA: return D3D11_BLEND_INV_BLEND_FACTOR;
default: SOKOL_UNREACHABLE; return (D3D11_BLEND) 0;
}
}
_SOKOL_PRIVATE D3D11_BLEND_OP _sg_d3d11_blend_op(sg_blend_op op) {
switch (op) {
case SG_BLENDOP_ADD: return D3D11_BLEND_OP_ADD;
case SG_BLENDOP_SUBTRACT: return D3D11_BLEND_OP_SUBTRACT;
case SG_BLENDOP_REVERSE_SUBTRACT: return D3D11_BLEND_OP_REV_SUBTRACT;
default: SOKOL_UNREACHABLE; return (D3D11_BLEND_OP) 0;
}
}
_SOKOL_PRIVATE UINT8 _sg_d3d11_color_write_mask(sg_color_mask m) {
UINT8 res = 0;
if (m & SG_COLORMASK_R) {
res |= D3D11_COLOR_WRITE_ENABLE_RED;
}
if (m & SG_COLORMASK_G) {
res |= D3D11_COLOR_WRITE_ENABLE_GREEN;
}
if (m & SG_COLORMASK_B) {
res |= D3D11_COLOR_WRITE_ENABLE_BLUE;
}
if (m & SG_COLORMASK_A) {
res |= D3D11_COLOR_WRITE_ENABLE_ALPHA;
}
return res;
}
/* see: https://docs.microsoft.com/en-us/windows/win32/direct3d11/overviews-direct3d-11-resources-limits#resource-limits-for-feature-level-11-hardware */
_SOKOL_PRIVATE void _sg_d3d11_init_caps(void) {
_sg.backend = SG_BACKEND_D3D11;
_sg.features.instancing = true;
_sg.features.origin_top_left = true;
_sg.features.multiple_render_targets = true;
_sg.features.msaa_render_targets = true;
_sg.features.imagetype_3d = true;
_sg.features.imagetype_array = true;
_sg.features.image_clamp_to_border = true;
_sg.features.mrt_independent_blend_state = true;
_sg.features.mrt_independent_write_mask = true;
_sg.limits.max_image_size_2d = 16 * 1024;
_sg.limits.max_image_size_cube = 16 * 1024;
_sg.limits.max_image_size_3d = 2 * 1024;
_sg.limits.max_image_size_array = 16 * 1024;
_sg.limits.max_image_array_layers = 2 * 1024;
_sg.limits.max_vertex_attrs = SG_MAX_VERTEX_ATTRIBUTES;
/* see: https://docs.microsoft.com/en-us/windows/win32/api/d3d11/ne-d3d11-d3d11_format_support */
for (int fmt = (SG_PIXELFORMAT_NONE+1); fmt < _SG_PIXELFORMAT_NUM; fmt++) {
UINT dxgi_fmt_caps = 0;
const DXGI_FORMAT dxgi_fmt = _sg_d3d11_pixel_format((sg_pixel_format)fmt);
if (dxgi_fmt != DXGI_FORMAT_UNKNOWN) {
HRESULT hr = _sg_d3d11_CheckFormatSupport(_sg.d3d11.dev, dxgi_fmt, &dxgi_fmt_caps);
SOKOL_ASSERT(SUCCEEDED(hr) || (E_FAIL == hr));
if (!SUCCEEDED(hr)) {
dxgi_fmt_caps = 0;
}
}
sg_pixelformat_info* info = &_sg.formats[fmt];
info->sample = 0 != (dxgi_fmt_caps & D3D11_FORMAT_SUPPORT_TEXTURE2D);
info->filter = 0 != (dxgi_fmt_caps & D3D11_FORMAT_SUPPORT_SHADER_SAMPLE);
info->render = 0 != (dxgi_fmt_caps & D3D11_FORMAT_SUPPORT_RENDER_TARGET);
info->blend = 0 != (dxgi_fmt_caps & D3D11_FORMAT_SUPPORT_BLENDABLE);
info->msaa = 0 != (dxgi_fmt_caps & D3D11_FORMAT_SUPPORT_MULTISAMPLE_RENDERTARGET);
info->depth = 0 != (dxgi_fmt_caps & D3D11_FORMAT_SUPPORT_DEPTH_STENCIL);
if (info->depth) {
info->render = true;
}
}
}
_SOKOL_PRIVATE void _sg_d3d11_setup_backend(const sg_desc* desc) {
/* assume _sg.d3d11 already is zero-initialized */
SOKOL_ASSERT(desc);
SOKOL_ASSERT(desc->context.d3d11.device);
SOKOL_ASSERT(desc->context.d3d11.device_context);
SOKOL_ASSERT(desc->context.d3d11.render_target_view_cb || desc->context.d3d11.render_target_view_userdata_cb);
SOKOL_ASSERT(desc->context.d3d11.depth_stencil_view_cb || desc->context.d3d11.depth_stencil_view_userdata_cb);
_sg.d3d11.valid = true;
_sg.d3d11.dev = (ID3D11Device*) desc->context.d3d11.device;
_sg.d3d11.ctx = (ID3D11DeviceContext*) desc->context.d3d11.device_context;
_sg.d3d11.rtv_cb = desc->context.d3d11.render_target_view_cb;
_sg.d3d11.rtv_userdata_cb = desc->context.d3d11.render_target_view_userdata_cb;
_sg.d3d11.dsv_cb = desc->context.d3d11.depth_stencil_view_cb;
_sg.d3d11.dsv_userdata_cb = desc->context.d3d11.depth_stencil_view_userdata_cb;
_sg.d3d11.user_data = desc->context.d3d11.user_data;
_sg_d3d11_init_caps();
}
_SOKOL_PRIVATE void _sg_d3d11_discard_backend(void) {
SOKOL_ASSERT(_sg.d3d11.valid);
_sg.d3d11.valid = false;
}
_SOKOL_PRIVATE void _sg_d3d11_clear_state(void) {
/* clear all the device context state, so that resource refs don't keep stuck in the d3d device context */
_sg_d3d11_OMSetRenderTargets(_sg.d3d11.ctx, SG_MAX_COLOR_ATTACHMENTS, _sg.d3d11.zero_rtvs, NULL);
_sg_d3d11_RSSetState(_sg.d3d11.ctx, NULL);
_sg_d3d11_OMSetDepthStencilState(_sg.d3d11.ctx, NULL, 0);
_sg_d3d11_OMSetBlendState(_sg.d3d11.ctx, NULL, NULL, 0xFFFFFFFF);
_sg_d3d11_IASetVertexBuffers(_sg.d3d11.ctx, 0, SG_MAX_SHADERSTAGE_BUFFERS, _sg.d3d11.zero_vbs, _sg.d3d11.zero_vb_strides, _sg.d3d11.zero_vb_offsets);
_sg_d3d11_IASetIndexBuffer(_sg.d3d11.ctx, NULL, DXGI_FORMAT_UNKNOWN, 0);
_sg_d3d11_IASetInputLayout(_sg.d3d11.ctx, NULL);
_sg_d3d11_VSSetShader(_sg.d3d11.ctx, NULL, NULL, 0);
_sg_d3d11_PSSetShader(_sg.d3d11.ctx, NULL, NULL, 0);
_sg_d3d11_VSSetConstantBuffers(_sg.d3d11.ctx, 0, SG_MAX_SHADERSTAGE_UBS, _sg.d3d11.zero_cbs);
_sg_d3d11_PSSetConstantBuffers(_sg.d3d11.ctx, 0, SG_MAX_SHADERSTAGE_UBS, _sg.d3d11.zero_cbs);
_sg_d3d11_VSSetShaderResources(_sg.d3d11.ctx, 0, SG_MAX_SHADERSTAGE_IMAGES, _sg.d3d11.zero_srvs);
_sg_d3d11_PSSetShaderResources(_sg.d3d11.ctx, 0, SG_MAX_SHADERSTAGE_IMAGES, _sg.d3d11.zero_srvs);
_sg_d3d11_VSSetSamplers(_sg.d3d11.ctx, 0, SG_MAX_SHADERSTAGE_IMAGES, _sg.d3d11.zero_smps);
_sg_d3d11_PSSetSamplers(_sg.d3d11.ctx, 0, SG_MAX_SHADERSTAGE_IMAGES, _sg.d3d11.zero_smps);
}
_SOKOL_PRIVATE void _sg_d3d11_reset_state_cache(void) {
/* just clear the d3d11 device context state */
_sg_d3d11_clear_state();
}
_SOKOL_PRIVATE void _sg_d3d11_activate_context(_sg_context_t* ctx) {
_SOKOL_UNUSED(ctx);
_sg_d3d11_clear_state();
}
_SOKOL_PRIVATE sg_resource_state _sg_d3d11_create_context(_sg_context_t* ctx) {
SOKOL_ASSERT(ctx);
_SOKOL_UNUSED(ctx);
return SG_RESOURCESTATE_VALID;
}
_SOKOL_PRIVATE void _sg_d3d11_destroy_context(_sg_context_t* ctx) {
SOKOL_ASSERT(ctx);
_SOKOL_UNUSED(ctx);
/* empty */
}
_SOKOL_PRIVATE sg_resource_state _sg_d3d11_create_buffer(_sg_buffer_t* buf, const sg_buffer_desc* desc) {
SOKOL_ASSERT(buf && desc);
SOKOL_ASSERT(!buf->d3d11.buf);
_sg_buffer_common_init(&buf->cmn, desc);
const bool injected = (0 != desc->d3d11_buffer);
if (injected) {
buf->d3d11.buf = (ID3D11Buffer*) desc->d3d11_buffer;
_sg_d3d11_AddRef(buf->d3d11.buf);
}
else {
D3D11_BUFFER_DESC d3d11_desc;
memset(&d3d11_desc, 0, sizeof(d3d11_desc));
d3d11_desc.ByteWidth = (UINT)buf->cmn.size;
d3d11_desc.Usage = _sg_d3d11_usage(buf->cmn.usage);
d3d11_desc.BindFlags = buf->cmn.type == SG_BUFFERTYPE_VERTEXBUFFER ? D3D11_BIND_VERTEX_BUFFER : D3D11_BIND_INDEX_BUFFER;
d3d11_desc.CPUAccessFlags = _sg_d3d11_cpu_access_flags(buf->cmn.usage);
D3D11_SUBRESOURCE_DATA* init_data_ptr = 0;
D3D11_SUBRESOURCE_DATA init_data;
memset(&init_data, 0, sizeof(init_data));
if (buf->cmn.usage == SG_USAGE_IMMUTABLE) {
SOKOL_ASSERT(desc->data.ptr);
init_data.pSysMem = desc->data.ptr;
init_data_ptr = &init_data;
}
HRESULT hr = _sg_d3d11_CreateBuffer(_sg.d3d11.dev, &d3d11_desc, init_data_ptr, &buf->d3d11.buf);
_SOKOL_UNUSED(hr);
SOKOL_ASSERT(SUCCEEDED(hr) && buf->d3d11.buf);
}
return SG_RESOURCESTATE_VALID;
}
_SOKOL_PRIVATE void _sg_d3d11_destroy_buffer(_sg_buffer_t* buf) {
SOKOL_ASSERT(buf);
if (buf->d3d11.buf) {
_sg_d3d11_Release(buf->d3d11.buf);
}
}
_SOKOL_PRIVATE void _sg_d3d11_fill_subres_data(const _sg_image_t* img, const sg_image_data* data) {
const int num_faces = (img->cmn.type == SG_IMAGETYPE_CUBE) ? 6:1;
const int num_slices = (img->cmn.type == SG_IMAGETYPE_ARRAY) ? img->cmn.num_slices:1;
int subres_index = 0;
for (int face_index = 0; face_index < num_faces; face_index++) {
for (int slice_index = 0; slice_index < num_slices; slice_index++) {
for (int mip_index = 0; mip_index < img->cmn.num_mipmaps; mip_index++, subres_index++) {
SOKOL_ASSERT(subres_index < (SG_MAX_MIPMAPS * SG_MAX_TEXTUREARRAY_LAYERS));
D3D11_SUBRESOURCE_DATA* subres_data = &_sg.d3d11.subres_data[subres_index];
const int mip_width = ((img->cmn.width>>mip_index)>0) ? img->cmn.width>>mip_index : 1;
const int mip_height = ((img->cmn.height>>mip_index)>0) ? img->cmn.height>>mip_index : 1;
const sg_range* subimg_data = &(data->subimage[face_index][mip_index]);
const size_t slice_size = subimg_data->size / (size_t)num_slices;
const size_t slice_offset = slice_size * (size_t)slice_index;
const uint8_t* ptr = (const uint8_t*) subimg_data->ptr;
subres_data->pSysMem = ptr + slice_offset;
subres_data->SysMemPitch = (UINT)_sg_row_pitch(img->cmn.pixel_format, mip_width, 1);
if (img->cmn.type == SG_IMAGETYPE_3D) {
/* FIXME? const int mip_depth = ((img->depth>>mip_index)>0) ? img->depth>>mip_index : 1; */
subres_data->SysMemSlicePitch = (UINT)_sg_surface_pitch(img->cmn.pixel_format, mip_width, mip_height, 1);
}
else {
subres_data->SysMemSlicePitch = 0;
}
}
}
}
}
_SOKOL_PRIVATE sg_resource_state _sg_d3d11_create_image(_sg_image_t* img, const sg_image_desc* desc) {
SOKOL_ASSERT(img && desc);
SOKOL_ASSERT(!img->d3d11.tex2d && !img->d3d11.tex3d && !img->d3d11.texds && !img->d3d11.texmsaa);
SOKOL_ASSERT(!img->d3d11.srv && !img->d3d11.smp);
HRESULT hr;
_SOKOL_UNUSED(hr);
_sg_image_common_init(&img->cmn, desc);
const bool injected = (0 != desc->d3d11_texture) || (0 != desc->d3d11_shader_resource_view);
const bool msaa = (img->cmn.sample_count > 1);
img->d3d11.format = _sg_d3d11_pixel_format(img->cmn.pixel_format);
/* special case depth-stencil buffer? */
if (_sg_is_valid_rendertarget_depth_format(img->cmn.pixel_format)) {
/* create only a depth-texture */
SOKOL_ASSERT(!injected);
if (img->d3d11.format == DXGI_FORMAT_UNKNOWN) {
SOKOL_LOG("trying to create a D3D11 depth-texture with unsupported pixel format\n");
return SG_RESOURCESTATE_FAILED;
}
D3D11_TEXTURE2D_DESC d3d11_desc;
memset(&d3d11_desc, 0, sizeof(d3d11_desc));
d3d11_desc.Width = (UINT)img->cmn.width;
d3d11_desc.Height = (UINT)img->cmn.height;
d3d11_desc.MipLevels = 1;
d3d11_desc.ArraySize = 1;
d3d11_desc.Format = img->d3d11.format;
d3d11_desc.Usage = D3D11_USAGE_DEFAULT;
d3d11_desc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
d3d11_desc.SampleDesc.Count = (UINT)img->cmn.sample_count;
d3d11_desc.SampleDesc.Quality = (UINT) (msaa ? D3D11_STANDARD_MULTISAMPLE_PATTERN : 0);
hr = _sg_d3d11_CreateTexture2D(_sg.d3d11.dev, &d3d11_desc, NULL, &img->d3d11.texds);
SOKOL_ASSERT(SUCCEEDED(hr) && img->d3d11.texds);
}
else {
/* create (or inject) color texture and shader-resource-view */
/* prepare initial content pointers */
D3D11_SUBRESOURCE_DATA* init_data = 0;
if (!injected && (img->cmn.usage == SG_USAGE_IMMUTABLE) && !img->cmn.render_target) {
_sg_d3d11_fill_subres_data(img, &desc->data);
init_data = _sg.d3d11.subres_data;
}
if (img->cmn.type != SG_IMAGETYPE_3D) {
/* 2D-, cube- or array-texture */
/* if this is an MSAA render target, the following texture will be the 'resolve-texture' */
/* first check for injected texture and/or resource view */
if (injected) {
img->d3d11.tex2d = (ID3D11Texture2D*) desc->d3d11_texture;
img->d3d11.srv = (ID3D11ShaderResourceView*) desc->d3d11_shader_resource_view;
if (img->d3d11.tex2d) {
_sg_d3d11_AddRef(img->d3d11.tex2d);
}
else {
/* if only a shader-resource-view was provided, but no texture, lookup
the texture from the shader-resource-view, this also bumps the refcount
*/
SOKOL_ASSERT(img->d3d11.srv);
_sg_d3d11_GetResource((ID3D11View*)img->d3d11.srv, (ID3D11Resource**)&img->d3d11.tex2d);
SOKOL_ASSERT(img->d3d11.tex2d);
}
if (img->d3d11.srv) {
_sg_d3d11_AddRef(img->d3d11.srv);
}
}
/* if not injected, create texture */
if (0 == img->d3d11.tex2d) {
D3D11_TEXTURE2D_DESC d3d11_tex_desc;
memset(&d3d11_tex_desc, 0, sizeof(d3d11_tex_desc));
d3d11_tex_desc.Width = (UINT)img->cmn.width;
d3d11_tex_desc.Height = (UINT)img->cmn.height;
d3d11_tex_desc.MipLevels = (UINT)img->cmn.num_mipmaps;
switch (img->cmn.type) {
case SG_IMAGETYPE_ARRAY: d3d11_tex_desc.ArraySize = (UINT)img->cmn.num_slices; break;
case SG_IMAGETYPE_CUBE: d3d11_tex_desc.ArraySize = 6; break;
default: d3d11_tex_desc.ArraySize = 1; break;
}
d3d11_tex_desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
d3d11_tex_desc.Format = img->d3d11.format;
if (img->cmn.render_target) {
d3d11_tex_desc.Usage = D3D11_USAGE_DEFAULT;
if (!msaa) {
d3d11_tex_desc.BindFlags |= D3D11_BIND_RENDER_TARGET;
}
d3d11_tex_desc.CPUAccessFlags = 0;
}
else {
d3d11_tex_desc.Usage = _sg_d3d11_usage(img->cmn.usage);
d3d11_tex_desc.CPUAccessFlags = _sg_d3d11_cpu_access_flags(img->cmn.usage);
}
if (img->d3d11.format == DXGI_FORMAT_UNKNOWN) {
/* trying to create a texture format that's not supported by D3D */
SOKOL_LOG("trying to create a D3D11 texture with unsupported pixel format\n");
return SG_RESOURCESTATE_FAILED;
}
d3d11_tex_desc.SampleDesc.Count = 1;
d3d11_tex_desc.SampleDesc.Quality = 0;
d3d11_tex_desc.MiscFlags = (img->cmn.type == SG_IMAGETYPE_CUBE) ? D3D11_RESOURCE_MISC_TEXTURECUBE : 0;
hr = _sg_d3d11_CreateTexture2D(_sg.d3d11.dev, &d3d11_tex_desc, init_data, &img->d3d11.tex2d);
SOKOL_ASSERT(SUCCEEDED(hr) && img->d3d11.tex2d);
}
/* ...and similar, if not injected, create shader-resource-view */
if (0 == img->d3d11.srv) {
D3D11_SHADER_RESOURCE_VIEW_DESC d3d11_srv_desc;
memset(&d3d11_srv_desc, 0, sizeof(d3d11_srv_desc));
d3d11_srv_desc.Format = img->d3d11.format;
switch (img->cmn.type) {
case SG_IMAGETYPE_2D:
d3d11_srv_desc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
d3d11_srv_desc.Texture2D.MipLevels = (UINT)img->cmn.num_mipmaps;
break;
case SG_IMAGETYPE_CUBE:
d3d11_srv_desc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBE;
d3d11_srv_desc.TextureCube.MipLevels = (UINT)img->cmn.num_mipmaps;
break;
case SG_IMAGETYPE_ARRAY:
d3d11_srv_desc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2DARRAY;
d3d11_srv_desc.Texture2DArray.MipLevels = (UINT)img->cmn.num_mipmaps;
d3d11_srv_desc.Texture2DArray.ArraySize = (UINT)img->cmn.num_slices;
break;
default:
SOKOL_UNREACHABLE; break;
}
hr = _sg_d3d11_CreateShaderResourceView(_sg.d3d11.dev, (ID3D11Resource*)img->d3d11.tex2d, &d3d11_srv_desc, &img->d3d11.srv);
SOKOL_ASSERT(SUCCEEDED(hr) && img->d3d11.srv);
}
}
else {
/* 3D texture - same procedure, first check if injected, than create non-injected */
if (injected) {
img->d3d11.tex3d = (ID3D11Texture3D*) desc->d3d11_texture;
img->d3d11.srv = (ID3D11ShaderResourceView*) desc->d3d11_shader_resource_view;
if (img->d3d11.tex3d) {
_sg_d3d11_AddRef(img->d3d11.tex3d);
}
else {
SOKOL_ASSERT(img->d3d11.srv);
_sg_d3d11_GetResource((ID3D11View*)img->d3d11.srv, (ID3D11Resource**)&img->d3d11.tex3d);
SOKOL_ASSERT(img->d3d11.tex3d);
}
if (img->d3d11.srv) {
_sg_d3d11_AddRef(img->d3d11.srv);
}
}
if (0 == img->d3d11.tex3d) {
D3D11_TEXTURE3D_DESC d3d11_tex_desc;
memset(&d3d11_tex_desc, 0, sizeof(d3d11_tex_desc));
d3d11_tex_desc.Width = (UINT)img->cmn.width;
d3d11_tex_desc.Height = (UINT)img->cmn.height;
d3d11_tex_desc.Depth = (UINT)img->cmn.num_slices;
d3d11_tex_desc.MipLevels = (UINT)img->cmn.num_mipmaps;
d3d11_tex_desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
d3d11_tex_desc.Format = img->d3d11.format;
if (img->cmn.render_target) {
d3d11_tex_desc.Usage = D3D11_USAGE_DEFAULT;
if (!msaa) {
d3d11_tex_desc.BindFlags |= D3D11_BIND_RENDER_TARGET;
}
d3d11_tex_desc.CPUAccessFlags = 0;
}
else {
d3d11_tex_desc.Usage = _sg_d3d11_usage(img->cmn.usage);
d3d11_tex_desc.CPUAccessFlags = _sg_d3d11_cpu_access_flags(img->cmn.usage);
}
if (img->d3d11.format == DXGI_FORMAT_UNKNOWN) {
/* trying to create a texture format that's not supported by D3D */
SOKOL_LOG("trying to create a D3D11 texture with unsupported pixel format\n");
return SG_RESOURCESTATE_FAILED;
}
hr = _sg_d3d11_CreateTexture3D(_sg.d3d11.dev, &d3d11_tex_desc, init_data, &img->d3d11.tex3d);
SOKOL_ASSERT(SUCCEEDED(hr) && img->d3d11.tex3d);
}
if (0 == img->d3d11.srv) {
D3D11_SHADER_RESOURCE_VIEW_DESC d3d11_srv_desc;
memset(&d3d11_srv_desc, 0, sizeof(d3d11_srv_desc));
d3d11_srv_desc.Format = img->d3d11.format;
d3d11_srv_desc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE3D;
d3d11_srv_desc.Texture3D.MipLevels = (UINT)img->cmn.num_mipmaps;
hr = _sg_d3d11_CreateShaderResourceView(_sg.d3d11.dev, (ID3D11Resource*)img->d3d11.tex3d, &d3d11_srv_desc, &img->d3d11.srv);
SOKOL_ASSERT(SUCCEEDED(hr) && img->d3d11.srv);
}
}
/* also need to create a separate MSAA render target texture? */
if (msaa) {
D3D11_TEXTURE2D_DESC d3d11_tex_desc;
memset(&d3d11_tex_desc, 0, sizeof(d3d11_tex_desc));
d3d11_tex_desc.Width = (UINT)img->cmn.width;
d3d11_tex_desc.Height = (UINT)img->cmn.height;
d3d11_tex_desc.MipLevels = 1;
d3d11_tex_desc.ArraySize = 1;
d3d11_tex_desc.Format = img->d3d11.format;
d3d11_tex_desc.Usage = D3D11_USAGE_DEFAULT;
d3d11_tex_desc.BindFlags = D3D11_BIND_RENDER_TARGET;
d3d11_tex_desc.CPUAccessFlags = 0;
d3d11_tex_desc.SampleDesc.Count = (UINT)img->cmn.sample_count;
d3d11_tex_desc.SampleDesc.Quality = (UINT)D3D11_STANDARD_MULTISAMPLE_PATTERN;
hr = _sg_d3d11_CreateTexture2D(_sg.d3d11.dev, &d3d11_tex_desc, NULL, &img->d3d11.texmsaa);
SOKOL_ASSERT(SUCCEEDED(hr) && img->d3d11.texmsaa);
}
/* sampler state object, note D3D11 implements an internal shared-pool for sampler objects */
D3D11_SAMPLER_DESC d3d11_smp_desc;
memset(&d3d11_smp_desc, 0, sizeof(d3d11_smp_desc));
d3d11_smp_desc.Filter = _sg_d3d11_filter(img->cmn.min_filter, img->cmn.mag_filter, img->cmn.max_anisotropy);
d3d11_smp_desc.AddressU = _sg_d3d11_address_mode(img->cmn.wrap_u);
d3d11_smp_desc.AddressV = _sg_d3d11_address_mode(img->cmn.wrap_v);
d3d11_smp_desc.AddressW = _sg_d3d11_address_mode(img->cmn.wrap_w);
switch (img->cmn.border_color) {
case SG_BORDERCOLOR_TRANSPARENT_BLACK:
/* all 0.0f */
break;
case SG_BORDERCOLOR_OPAQUE_WHITE:
for (int i = 0; i < 4; i++) {
d3d11_smp_desc.BorderColor[i] = 1.0f;
}
break;
default:
/* opaque black */
d3d11_smp_desc.BorderColor[3] = 1.0f;
break;
}
d3d11_smp_desc.MaxAnisotropy = img->cmn.max_anisotropy;
d3d11_smp_desc.ComparisonFunc = D3D11_COMPARISON_NEVER;
d3d11_smp_desc.MinLOD = desc->min_lod;
d3d11_smp_desc.MaxLOD = desc->max_lod;
hr = _sg_d3d11_CreateSamplerState(_sg.d3d11.dev, &d3d11_smp_desc, &img->d3d11.smp);
SOKOL_ASSERT(SUCCEEDED(hr) && img->d3d11.smp);
}
return SG_RESOURCESTATE_VALID;
}
_SOKOL_PRIVATE void _sg_d3d11_destroy_image(_sg_image_t* img) {
SOKOL_ASSERT(img);
if (img->d3d11.tex2d) {
_sg_d3d11_Release(img->d3d11.tex2d);
}
if (img->d3d11.tex3d) {
_sg_d3d11_Release(img->d3d11.tex3d);
}
if (img->d3d11.texds) {
_sg_d3d11_Release(img->d3d11.texds);
}
if (img->d3d11.texmsaa) {
_sg_d3d11_Release(img->d3d11.texmsaa);
}
if (img->d3d11.srv) {
_sg_d3d11_Release(img->d3d11.srv);
}
if (img->d3d11.smp) {
_sg_d3d11_Release(img->d3d11.smp);
}
}
_SOKOL_PRIVATE bool _sg_d3d11_load_d3dcompiler_dll(void) {
/* on UWP, don't do anything (not tested) */
#if (defined(WINAPI_FAMILY_PARTITION) && !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP))
return true;
#else
/* load DLL on demand */
if ((0 == _sg.d3d11.d3dcompiler_dll) && !_sg.d3d11.d3dcompiler_dll_load_failed) {
_sg.d3d11.d3dcompiler_dll = LoadLibraryA("d3dcompiler_47.dll");
if (0 == _sg.d3d11.d3dcompiler_dll) {
/* don't attempt to load missing DLL in the future */
SOKOL_LOG("failed to load d3dcompiler_47.dll!\n");
_sg.d3d11.d3dcompiler_dll_load_failed = true;
return false;
}
/* look up function pointers */
_sg.d3d11.D3DCompile_func = (pD3DCompile)(void*) GetProcAddress(_sg.d3d11.d3dcompiler_dll, "D3DCompile");
SOKOL_ASSERT(_sg.d3d11.D3DCompile_func);
}
return 0 != _sg.d3d11.d3dcompiler_dll;
#endif
}
#if (defined(WINAPI_FAMILY_PARTITION) && !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP))
#define _sg_d3d11_D3DCompile D3DCompile
#else
#define _sg_d3d11_D3DCompile _sg.d3d11.D3DCompile_func
#endif
_SOKOL_PRIVATE ID3DBlob* _sg_d3d11_compile_shader(const sg_shader_stage_desc* stage_desc) {
if (!_sg_d3d11_load_d3dcompiler_dll()) {
return NULL;
}
SOKOL_ASSERT(stage_desc->d3d11_target);
ID3DBlob* output = NULL;
ID3DBlob* errors_or_warnings = NULL;
HRESULT hr = _sg_d3d11_D3DCompile(
stage_desc->source, /* pSrcData */
strlen(stage_desc->source), /* SrcDataSize */
NULL, /* pSourceName */
NULL, /* pDefines */
NULL, /* pInclude */
stage_desc->entry ? stage_desc->entry : "main", /* pEntryPoint */
stage_desc->d3d11_target, /* pTarget (vs_5_0 or ps_5_0) */
D3DCOMPILE_PACK_MATRIX_COLUMN_MAJOR | D3DCOMPILE_OPTIMIZATION_LEVEL3, /* Flags1 */
0, /* Flags2 */
&output, /* ppCode */
&errors_or_warnings); /* ppErrorMsgs */
if (errors_or_warnings) {
SOKOL_LOG((LPCSTR)_sg_d3d11_GetBufferPointer(errors_or_warnings));
_sg_d3d11_Release(errors_or_warnings); errors_or_warnings = NULL;
}
if (FAILED(hr)) {
/* just in case, usually output is NULL here */
if (output) {
_sg_d3d11_Release(output);
output = NULL;
}
}
return output;
}
_SOKOL_PRIVATE sg_resource_state _sg_d3d11_create_shader(_sg_shader_t* shd, const sg_shader_desc* desc) {
SOKOL_ASSERT(shd && desc);
SOKOL_ASSERT(!shd->d3d11.vs && !shd->d3d11.fs && !shd->d3d11.vs_blob);
HRESULT hr;
_SOKOL_UNUSED(hr);
_sg_shader_common_init(&shd->cmn, desc);
/* copy vertex attribute semantic names and indices */
for (int i = 0; i < SG_MAX_VERTEX_ATTRIBUTES; i++) {
_sg_strcpy(&shd->d3d11.attrs[i].sem_name, desc->attrs[i].sem_name);
shd->d3d11.attrs[i].sem_index = desc->attrs[i].sem_index;
}
/* shader stage uniform blocks and image slots */
for (int stage_index = 0; stage_index < SG_NUM_SHADER_STAGES; stage_index++) {
_sg_shader_stage_t* cmn_stage = &shd->cmn.stage[stage_index];
_sg_d3d11_shader_stage_t* d3d11_stage = &shd->d3d11.stage[stage_index];
for (int ub_index = 0; ub_index < cmn_stage->num_uniform_blocks; ub_index++) {
const _sg_uniform_block_t* ub = &cmn_stage->uniform_blocks[ub_index];
/* create a D3D constant buffer for each uniform block */
SOKOL_ASSERT(0 == d3d11_stage->cbufs[ub_index]);
D3D11_BUFFER_DESC cb_desc;
memset(&cb_desc, 0, sizeof(cb_desc));
cb_desc.ByteWidth = (UINT)_sg_roundup((int)ub->size, 16);
cb_desc.Usage = D3D11_USAGE_DEFAULT;
cb_desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
hr = _sg_d3d11_CreateBuffer(_sg.d3d11.dev, &cb_desc, NULL, &d3d11_stage->cbufs[ub_index]);
SOKOL_ASSERT(SUCCEEDED(hr) && d3d11_stage->cbufs[ub_index]);
}
}
const void* vs_ptr = 0, *fs_ptr = 0;
SIZE_T vs_length = 0, fs_length = 0;
ID3DBlob* vs_blob = 0, *fs_blob = 0;
if (desc->vs.bytecode.ptr && desc->fs.bytecode.ptr) {
/* create from shader byte code */
vs_ptr = desc->vs.bytecode.ptr;
fs_ptr = desc->fs.bytecode.ptr;
vs_length = desc->vs.bytecode.size;
fs_length = desc->fs.bytecode.size;
}
else {
/* compile from shader source code */
vs_blob = _sg_d3d11_compile_shader(&desc->vs);
fs_blob = _sg_d3d11_compile_shader(&desc->fs);
if (vs_blob && fs_blob) {
vs_ptr = _sg_d3d11_GetBufferPointer(vs_blob);
vs_length = _sg_d3d11_GetBufferSize(vs_blob);
fs_ptr = _sg_d3d11_GetBufferPointer(fs_blob);
fs_length = _sg_d3d11_GetBufferSize(fs_blob);
}
}
sg_resource_state result = SG_RESOURCESTATE_FAILED;
if (vs_ptr && fs_ptr && (vs_length > 0) && (fs_length > 0)) {
/* create the D3D vertex- and pixel-shader objects */
hr = _sg_d3d11_CreateVertexShader(_sg.d3d11.dev, vs_ptr, vs_length, NULL, &shd->d3d11.vs);
bool vs_succeeded = SUCCEEDED(hr) && shd->d3d11.vs;
hr = _sg_d3d11_CreatePixelShader(_sg.d3d11.dev, fs_ptr, fs_length, NULL, &shd->d3d11.fs);
bool fs_succeeded = SUCCEEDED(hr) && shd->d3d11.fs;
/* need to store the vertex shader byte code, this is needed later in sg_create_pipeline */
if (vs_succeeded && fs_succeeded) {
shd->d3d11.vs_blob_length = vs_length;
shd->d3d11.vs_blob = SOKOL_MALLOC((size_t)vs_length);
SOKOL_ASSERT(shd->d3d11.vs_blob);
memcpy(shd->d3d11.vs_blob, vs_ptr, vs_length);
result = SG_RESOURCESTATE_VALID;
}
}
if (vs_blob) {
_sg_d3d11_Release(vs_blob); vs_blob = 0;
}
if (fs_blob) {
_sg_d3d11_Release(fs_blob); fs_blob = 0;
}
return result;
}
_SOKOL_PRIVATE void _sg_d3d11_destroy_shader(_sg_shader_t* shd) {
SOKOL_ASSERT(shd);
if (shd->d3d11.vs) {
_sg_d3d11_Release(shd->d3d11.vs);
}
if (shd->d3d11.fs) {
_sg_d3d11_Release(shd->d3d11.fs);
}
if (shd->d3d11.vs_blob) {
SOKOL_FREE(shd->d3d11.vs_blob);
}
for (int stage_index = 0; stage_index < SG_NUM_SHADER_STAGES; stage_index++) {
_sg_shader_stage_t* cmn_stage = &shd->cmn.stage[stage_index];
_sg_d3d11_shader_stage_t* d3d11_stage = &shd->d3d11.stage[stage_index];
for (int ub_index = 0; ub_index < cmn_stage->num_uniform_blocks; ub_index++) {
if (d3d11_stage->cbufs[ub_index]) {
_sg_d3d11_Release(d3d11_stage->cbufs[ub_index]);
}
}
}
}
_SOKOL_PRIVATE sg_resource_state _sg_d3d11_create_pipeline(_sg_pipeline_t* pip, _sg_shader_t* shd, const sg_pipeline_desc* desc) {
SOKOL_ASSERT(pip && shd && desc);
SOKOL_ASSERT(desc->shader.id == shd->slot.id);
SOKOL_ASSERT(shd->slot.state == SG_RESOURCESTATE_VALID);
SOKOL_ASSERT(shd->d3d11.vs_blob && shd->d3d11.vs_blob_length > 0);
SOKOL_ASSERT(!pip->d3d11.il && !pip->d3d11.rs && !pip->d3d11.dss && !pip->d3d11.bs);
pip->shader = shd;
_sg_pipeline_common_init(&pip->cmn, desc);
pip->d3d11.index_format = _sg_d3d11_index_format(pip->cmn.index_type);
pip->d3d11.topology = _sg_d3d11_primitive_topology(desc->primitive_type);
pip->d3d11.stencil_ref = desc->stencil.ref;
/* create input layout object */
HRESULT hr;
_SOKOL_UNUSED(hr);
D3D11_INPUT_ELEMENT_DESC d3d11_comps[SG_MAX_VERTEX_ATTRIBUTES];
memset(d3d11_comps, 0, sizeof(d3d11_comps));
int attr_index = 0;
for (; attr_index < SG_MAX_VERTEX_ATTRIBUTES; attr_index++) {
const sg_vertex_attr_desc* a_desc = &desc->layout.attrs[attr_index];
if (a_desc->format == SG_VERTEXFORMAT_INVALID) {
break;
}
SOKOL_ASSERT(a_desc->buffer_index < SG_MAX_SHADERSTAGE_BUFFERS);
const sg_buffer_layout_desc* l_desc = &desc->layout.buffers[a_desc->buffer_index];
const sg_vertex_step step_func = l_desc->step_func;
const int step_rate = l_desc->step_rate;
D3D11_INPUT_ELEMENT_DESC* d3d11_comp = &d3d11_comps[attr_index];
d3d11_comp->SemanticName = _sg_strptr(&shd->d3d11.attrs[attr_index].sem_name);
d3d11_comp->SemanticIndex = (UINT)shd->d3d11.attrs[attr_index].sem_index;
d3d11_comp->Format = _sg_d3d11_vertex_format(a_desc->format);
d3d11_comp->InputSlot = (UINT)a_desc->buffer_index;
d3d11_comp->AlignedByteOffset = (UINT)a_desc->offset;
d3d11_comp->InputSlotClass = _sg_d3d11_input_classification(step_func);
if (SG_VERTEXSTEP_PER_INSTANCE == step_func) {
d3d11_comp->InstanceDataStepRate = (UINT)step_rate;
}
pip->cmn.vertex_layout_valid[a_desc->buffer_index] = true;
}
for (int layout_index = 0; layout_index < SG_MAX_SHADERSTAGE_BUFFERS; layout_index++) {
if (pip->cmn.vertex_layout_valid[layout_index]) {
const sg_buffer_layout_desc* l_desc = &desc->layout.buffers[layout_index];
SOKOL_ASSERT(l_desc->stride > 0);
pip->d3d11.vb_strides[layout_index] = (UINT)l_desc->stride;
}
else {
pip->d3d11.vb_strides[layout_index] = 0;
}
}
hr = _sg_d3d11_CreateInputLayout(_sg.d3d11.dev,
d3d11_comps, /* pInputElementDesc */
(UINT)attr_index, /* NumElements */
shd->d3d11.vs_blob, /* pShaderByteCodeWithInputSignature */
shd->d3d11.vs_blob_length, /* BytecodeLength */
&pip->d3d11.il);
SOKOL_ASSERT(SUCCEEDED(hr) && pip->d3d11.il);
/* create rasterizer state */
D3D11_RASTERIZER_DESC rs_desc;
memset(&rs_desc, 0, sizeof(rs_desc));
rs_desc.FillMode = D3D11_FILL_SOLID;
rs_desc.CullMode = _sg_d3d11_cull_mode(desc->cull_mode);
rs_desc.FrontCounterClockwise = desc->face_winding == SG_FACEWINDING_CCW;
rs_desc.DepthBias = (INT) pip->cmn.depth_bias;
rs_desc.DepthBiasClamp = pip->cmn.depth_bias_clamp;
rs_desc.SlopeScaledDepthBias = pip->cmn.depth_bias_slope_scale;
rs_desc.DepthClipEnable = TRUE;
rs_desc.ScissorEnable = TRUE;
rs_desc.MultisampleEnable = desc->sample_count > 1;
rs_desc.AntialiasedLineEnable = FALSE;
hr = _sg_d3d11_CreateRasterizerState(_sg.d3d11.dev, &rs_desc, &pip->d3d11.rs);
SOKOL_ASSERT(SUCCEEDED(hr) && pip->d3d11.rs);
/* create depth-stencil state */
D3D11_DEPTH_STENCIL_DESC dss_desc;
memset(&dss_desc, 0, sizeof(dss_desc));
dss_desc.DepthEnable = TRUE;
dss_desc.DepthWriteMask = desc->depth.write_enabled ? D3D11_DEPTH_WRITE_MASK_ALL : D3D11_DEPTH_WRITE_MASK_ZERO;
dss_desc.DepthFunc = _sg_d3d11_compare_func(desc->depth.compare);
dss_desc.StencilEnable = desc->stencil.enabled;
dss_desc.StencilReadMask = desc->stencil.read_mask;
dss_desc.StencilWriteMask = desc->stencil.write_mask;
const sg_stencil_face_state* sf = &desc->stencil.front;
dss_desc.FrontFace.StencilFailOp = _sg_d3d11_stencil_op(sf->fail_op);
dss_desc.FrontFace.StencilDepthFailOp = _sg_d3d11_stencil_op(sf->depth_fail_op);
dss_desc.FrontFace.StencilPassOp = _sg_d3d11_stencil_op(sf->pass_op);
dss_desc.FrontFace.StencilFunc = _sg_d3d11_compare_func(sf->compare);
const sg_stencil_face_state* sb = &desc->stencil.back;
dss_desc.BackFace.StencilFailOp = _sg_d3d11_stencil_op(sb->fail_op);
dss_desc.BackFace.StencilDepthFailOp = _sg_d3d11_stencil_op(sb->depth_fail_op);
dss_desc.BackFace.StencilPassOp = _sg_d3d11_stencil_op(sb->pass_op);
dss_desc.BackFace.StencilFunc = _sg_d3d11_compare_func(sb->compare);
hr = _sg_d3d11_CreateDepthStencilState(_sg.d3d11.dev, &dss_desc, &pip->d3d11.dss);
SOKOL_ASSERT(SUCCEEDED(hr) && pip->d3d11.dss);
/* create blend state */
D3D11_BLEND_DESC bs_desc;
memset(&bs_desc, 0, sizeof(bs_desc));
bs_desc.AlphaToCoverageEnable = desc->alpha_to_coverage_enabled;
bs_desc.IndependentBlendEnable = TRUE;
{
int i = 0;
for (i = 0; i < desc->color_count; i++) {
const sg_blend_state* src = &desc->colors[i].blend;
D3D11_RENDER_TARGET_BLEND_DESC* dst = &bs_desc.RenderTarget[i];
dst->BlendEnable = src->enabled;
dst->SrcBlend = _sg_d3d11_blend_factor(src->src_factor_rgb);
dst->DestBlend = _sg_d3d11_blend_factor(src->dst_factor_rgb);
dst->BlendOp = _sg_d3d11_blend_op(src->op_rgb);
dst->SrcBlendAlpha = _sg_d3d11_blend_factor(src->src_factor_alpha);
dst->DestBlendAlpha = _sg_d3d11_blend_factor(src->dst_factor_alpha);
dst->BlendOpAlpha = _sg_d3d11_blend_op(src->op_alpha);
dst->RenderTargetWriteMask = _sg_d3d11_color_write_mask(desc->colors[i].write_mask);
}
for (; i < 8; i++) {
D3D11_RENDER_TARGET_BLEND_DESC* dst = &bs_desc.RenderTarget[i];
dst->BlendEnable = FALSE;
dst->SrcBlend = dst->SrcBlendAlpha = D3D11_BLEND_ONE;
dst->DestBlend = dst->DestBlendAlpha = D3D11_BLEND_ZERO;
dst->BlendOp = dst->BlendOpAlpha = D3D11_BLEND_OP_ADD;
dst->RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
}
}
hr = _sg_d3d11_CreateBlendState(_sg.d3d11.dev, &bs_desc, &pip->d3d11.bs);
SOKOL_ASSERT(SUCCEEDED(hr) && pip->d3d11.bs);
return SG_RESOURCESTATE_VALID;
}
_SOKOL_PRIVATE void _sg_d3d11_destroy_pipeline(_sg_pipeline_t* pip) {
SOKOL_ASSERT(pip);
if (pip->d3d11.il) {
_sg_d3d11_Release(pip->d3d11.il);
}
if (pip->d3d11.rs) {
_sg_d3d11_Release(pip->d3d11.rs);
}
if (pip->d3d11.dss) {
_sg_d3d11_Release(pip->d3d11.dss);
}
if (pip->d3d11.bs) {
_sg_d3d11_Release(pip->d3d11.bs);
}
}
_SOKOL_PRIVATE sg_resource_state _sg_d3d11_create_pass(_sg_pass_t* pass, _sg_image_t** att_images, const sg_pass_desc* desc) {
SOKOL_ASSERT(pass && desc);
SOKOL_ASSERT(att_images && att_images[0]);
SOKOL_ASSERT(_sg.d3d11.dev);
_sg_pass_common_init(&pass->cmn, desc);
for (int i = 0; i < pass->cmn.num_color_atts; i++) {
const sg_pass_attachment_desc* att_desc = &desc->color_attachments[i];
_SOKOL_UNUSED(att_desc);
SOKOL_ASSERT(att_desc->image.id != SG_INVALID_ID);
_sg_image_t* att_img = att_images[i];
SOKOL_ASSERT(att_img && (att_img->slot.id == att_desc->image.id));
SOKOL_ASSERT(_sg_is_valid_rendertarget_color_format(att_img->cmn.pixel_format));
SOKOL_ASSERT(0 == pass->d3d11.color_atts[i].image);
pass->d3d11.color_atts[i].image = att_img;
/* create D3D11 render-target-view */
const _sg_pass_attachment_t* cmn_att = &pass->cmn.color_atts[i];
SOKOL_ASSERT(0 == pass->d3d11.color_atts[i].rtv);
ID3D11Resource* d3d11_res = 0;
const bool is_msaa = att_img->cmn.sample_count > 1;
D3D11_RENDER_TARGET_VIEW_DESC d3d11_rtv_desc;
memset(&d3d11_rtv_desc, 0, sizeof(d3d11_rtv_desc));
d3d11_rtv_desc.Format = att_img->d3d11.format;
if ((att_img->cmn.type == SG_IMAGETYPE_2D) || is_msaa) {
if (is_msaa) {
d3d11_res = (ID3D11Resource*) att_img->d3d11.texmsaa;
d3d11_rtv_desc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2DMS;
}
else {
d3d11_res = (ID3D11Resource*) att_img->d3d11.tex2d;
d3d11_rtv_desc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
d3d11_rtv_desc.Texture2D.MipSlice = (UINT)cmn_att->mip_level;
}
}
else if ((att_img->cmn.type == SG_IMAGETYPE_CUBE) || (att_img->cmn.type == SG_IMAGETYPE_ARRAY)) {
d3d11_res = (ID3D11Resource*) att_img->d3d11.tex2d;
d3d11_rtv_desc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2DARRAY;
d3d11_rtv_desc.Texture2DArray.MipSlice = (UINT)cmn_att->mip_level;
d3d11_rtv_desc.Texture2DArray.FirstArraySlice = (UINT)cmn_att->slice;
d3d11_rtv_desc.Texture2DArray.ArraySize = 1;
}
else {
SOKOL_ASSERT(att_img->cmn.type == SG_IMAGETYPE_3D);
d3d11_res = (ID3D11Resource*) att_img->d3d11.tex3d;
d3d11_rtv_desc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE3D;
d3d11_rtv_desc.Texture3D.MipSlice = (UINT)cmn_att->mip_level;
d3d11_rtv_desc.Texture3D.FirstWSlice = (UINT)cmn_att->slice;
d3d11_rtv_desc.Texture3D.WSize = 1;
}
SOKOL_ASSERT(d3d11_res);
HRESULT hr = _sg_d3d11_CreateRenderTargetView(_sg.d3d11.dev, d3d11_res, &d3d11_rtv_desc, &pass->d3d11.color_atts[i].rtv);
_SOKOL_UNUSED(hr);
SOKOL_ASSERT(SUCCEEDED(hr) && pass->d3d11.color_atts[i].rtv);
}
/* optional depth-stencil image */
SOKOL_ASSERT(0 == pass->d3d11.ds_att.image);
SOKOL_ASSERT(0 == pass->d3d11.ds_att.dsv);
if (desc->depth_stencil_attachment.image.id != SG_INVALID_ID) {
const int ds_img_index = SG_MAX_COLOR_ATTACHMENTS;
const sg_pass_attachment_desc* att_desc = &desc->depth_stencil_attachment;
_SOKOL_UNUSED(att_desc);
_sg_image_t* att_img = att_images[ds_img_index];
SOKOL_ASSERT(att_img && (att_img->slot.id == att_desc->image.id));
SOKOL_ASSERT(_sg_is_valid_rendertarget_depth_format(att_img->cmn.pixel_format));
SOKOL_ASSERT(0 == pass->d3d11.ds_att.image);
pass->d3d11.ds_att.image = att_img;
/* create D3D11 depth-stencil-view */
D3D11_DEPTH_STENCIL_VIEW_DESC d3d11_dsv_desc;
memset(&d3d11_dsv_desc, 0, sizeof(d3d11_dsv_desc));
d3d11_dsv_desc.Format = att_img->d3d11.format;
const bool is_msaa = att_img->cmn.sample_count > 1;
if (is_msaa) {
d3d11_dsv_desc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2DMS;
}
else {
d3d11_dsv_desc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
}
ID3D11Resource* d3d11_res = (ID3D11Resource*) att_img->d3d11.texds;
SOKOL_ASSERT(d3d11_res);
HRESULT hr = _sg_d3d11_CreateDepthStencilView(_sg.d3d11.dev, d3d11_res, &d3d11_dsv_desc, &pass->d3d11.ds_att.dsv);
_SOKOL_UNUSED(hr);
SOKOL_ASSERT(SUCCEEDED(hr) && pass->d3d11.ds_att.dsv);
}
return SG_RESOURCESTATE_VALID;
}
_SOKOL_PRIVATE void _sg_d3d11_destroy_pass(_sg_pass_t* pass) {
SOKOL_ASSERT(pass);
for (int i = 0; i < SG_MAX_COLOR_ATTACHMENTS; i++) {
if (pass->d3d11.color_atts[i].rtv) {
_sg_d3d11_Release(pass->d3d11.color_atts[i].rtv);
}
}
if (pass->d3d11.ds_att.dsv) {
_sg_d3d11_Release(pass->d3d11.ds_att.dsv);
}
}
_SOKOL_PRIVATE _sg_image_t* _sg_d3d11_pass_color_image(const _sg_pass_t* pass, int index) {
SOKOL_ASSERT(pass && (index >= 0) && (index < SG_MAX_COLOR_ATTACHMENTS));
/* NOTE: may return null */
return pass->d3d11.color_atts[index].image;
}
_SOKOL_PRIVATE _sg_image_t* _sg_d3d11_pass_ds_image(const _sg_pass_t* pass) {
/* NOTE: may return null */
SOKOL_ASSERT(pass);
return pass->d3d11.ds_att.image;
}
_SOKOL_PRIVATE void _sg_d3d11_begin_pass(_sg_pass_t* pass, const sg_pass_action* action, int w, int h) {
SOKOL_ASSERT(action);
SOKOL_ASSERT(!_sg.d3d11.in_pass);
SOKOL_ASSERT(_sg.d3d11.rtv_cb || _sg.d3d11.rtv_userdata_cb);
SOKOL_ASSERT(_sg.d3d11.dsv_cb || _sg.d3d11.dsv_userdata_cb);
_sg.d3d11.in_pass = true;
_sg.d3d11.cur_width = w;
_sg.d3d11.cur_height = h;
if (pass) {
_sg.d3d11.cur_pass = pass;
_sg.d3d11.cur_pass_id.id = pass->slot.id;
_sg.d3d11.num_rtvs = 0;
for (int i = 0; i < SG_MAX_COLOR_ATTACHMENTS; i++) {
_sg.d3d11.cur_rtvs[i] = pass->d3d11.color_atts[i].rtv;
if (_sg.d3d11.cur_rtvs[i]) {
_sg.d3d11.num_rtvs++;
}
}
_sg.d3d11.cur_dsv = pass->d3d11.ds_att.dsv;
}
else {
/* render to default frame buffer */
_sg.d3d11.cur_pass = 0;
_sg.d3d11.cur_pass_id.id = SG_INVALID_ID;
_sg.d3d11.num_rtvs = 1;
if (_sg.d3d11.rtv_cb) {
_sg.d3d11.cur_rtvs[0] = (ID3D11RenderTargetView*) _sg.d3d11.rtv_cb();
}
else {
_sg.d3d11.cur_rtvs[0] = (ID3D11RenderTargetView*) _sg.d3d11.rtv_userdata_cb(_sg.d3d11.user_data);
}
for (int i = 1; i < SG_MAX_COLOR_ATTACHMENTS; i++) {
_sg.d3d11.cur_rtvs[i] = 0;
}
if (_sg.d3d11.dsv_cb) {
_sg.d3d11.cur_dsv = (ID3D11DepthStencilView*) _sg.d3d11.dsv_cb();
}
else {
_sg.d3d11.cur_dsv = (ID3D11DepthStencilView*) _sg.d3d11.dsv_userdata_cb(_sg.d3d11.user_data);
}
SOKOL_ASSERT(_sg.d3d11.cur_rtvs[0] && _sg.d3d11.cur_dsv);
}
/* apply the render-target- and depth-stencil-views */
_sg_d3d11_OMSetRenderTargets(_sg.d3d11.ctx, SG_MAX_COLOR_ATTACHMENTS, _sg.d3d11.cur_rtvs, _sg.d3d11.cur_dsv);
/* set viewport and scissor rect to cover whole screen */
D3D11_VIEWPORT vp;
memset(&vp, 0, sizeof(vp));
vp.Width = (FLOAT) w;
vp.Height = (FLOAT) h;
vp.MaxDepth = 1.0f;
_sg_d3d11_RSSetViewports(_sg.d3d11.ctx, 1, &vp);
D3D11_RECT rect;
rect.left = 0;
rect.top = 0;
rect.right = w;
rect.bottom = h;
_sg_d3d11_RSSetScissorRects(_sg.d3d11.ctx, 1, &rect);
/* perform clear action */
for (int i = 0; i < _sg.d3d11.num_rtvs; i++) {
if (action->colors[i].action == SG_ACTION_CLEAR) {
_sg_d3d11_ClearRenderTargetView(_sg.d3d11.ctx, _sg.d3d11.cur_rtvs[i], &action->colors[i].value.r);
}
}
UINT ds_flags = 0;
if (action->depth.action == SG_ACTION_CLEAR) {
ds_flags |= D3D11_CLEAR_DEPTH;
}
if (action->stencil.action == SG_ACTION_CLEAR) {
ds_flags |= D3D11_CLEAR_STENCIL;
}
if ((0 != ds_flags) && _sg.d3d11.cur_dsv) {
_sg_d3d11_ClearDepthStencilView(_sg.d3d11.ctx, _sg.d3d11.cur_dsv, ds_flags, action->depth.value, action->stencil.value);
}
}
/* D3D11CalcSubresource only exists for C++ */
_SOKOL_PRIVATE UINT _sg_d3d11_calcsubresource(UINT mip_slice, UINT array_slice, UINT mip_levels) {
return mip_slice + array_slice * mip_levels;
}
_SOKOL_PRIVATE void _sg_d3d11_end_pass(void) {
SOKOL_ASSERT(_sg.d3d11.in_pass && _sg.d3d11.ctx);
_sg.d3d11.in_pass = false;
/* need to resolve MSAA render target into texture? */
if (_sg.d3d11.cur_pass) {
SOKOL_ASSERT(_sg.d3d11.cur_pass->slot.id == _sg.d3d11.cur_pass_id.id);
for (int i = 0; i < _sg.d3d11.num_rtvs; i++) {
_sg_pass_attachment_t* cmn_att = &_sg.d3d11.cur_pass->cmn.color_atts[i];
_sg_image_t* att_img = _sg.d3d11.cur_pass->d3d11.color_atts[i].image;
SOKOL_ASSERT(att_img && (att_img->slot.id == cmn_att->image_id.id));
if (att_img->cmn.sample_count > 1) {
/* FIXME: support MSAA resolve into 3D texture */
SOKOL_ASSERT(att_img->d3d11.tex2d && att_img->d3d11.texmsaa && !att_img->d3d11.tex3d);
SOKOL_ASSERT(DXGI_FORMAT_UNKNOWN != att_img->d3d11.format);
UINT dst_subres = _sg_d3d11_calcsubresource((UINT)cmn_att->mip_level, (UINT)cmn_att->slice, (UINT)att_img->cmn.num_mipmaps);
_sg_d3d11_ResolveSubresource(_sg.d3d11.ctx,
(ID3D11Resource*) att_img->d3d11.tex2d, /* pDstResource */
dst_subres, /* DstSubresource */
(ID3D11Resource*) att_img->d3d11.texmsaa, /* pSrcResource */
0, /* SrcSubresource */
att_img->d3d11.format);
}
}
}
_sg.d3d11.cur_pass = 0;
_sg.d3d11.cur_pass_id.id = SG_INVALID_ID;
_sg.d3d11.cur_pipeline = 0;
_sg.d3d11.cur_pipeline_id.id = SG_INVALID_ID;
for (int i = 0; i < SG_MAX_COLOR_ATTACHMENTS; i++) {
_sg.d3d11.cur_rtvs[i] = 0;
}
_sg.d3d11.cur_dsv = 0;
_sg_d3d11_clear_state();
}
_SOKOL_PRIVATE void _sg_d3d11_apply_viewport(int x, int y, int w, int h, bool origin_top_left) {
SOKOL_ASSERT(_sg.d3d11.ctx);
SOKOL_ASSERT(_sg.d3d11.in_pass);
D3D11_VIEWPORT vp;
vp.TopLeftX = (FLOAT) x;
vp.TopLeftY = (FLOAT) (origin_top_left ? y : (_sg.d3d11.cur_height - (y + h)));
vp.Width = (FLOAT) w;
vp.Height = (FLOAT) h;
vp.MinDepth = 0.0f;
vp.MaxDepth = 1.0f;
_sg_d3d11_RSSetViewports(_sg.d3d11.ctx, 1, &vp);
}
_SOKOL_PRIVATE void _sg_d3d11_apply_scissor_rect(int x, int y, int w, int h, bool origin_top_left) {
SOKOL_ASSERT(_sg.d3d11.ctx);
SOKOL_ASSERT(_sg.d3d11.in_pass);
D3D11_RECT rect;
rect.left = x;
rect.top = (origin_top_left ? y : (_sg.d3d11.cur_height - (y + h)));
rect.right = x + w;
rect.bottom = origin_top_left ? (y + h) : (_sg.d3d11.cur_height - y);
_sg_d3d11_RSSetScissorRects(_sg.d3d11.ctx, 1, &rect);
}
_SOKOL_PRIVATE void _sg_d3d11_apply_pipeline(_sg_pipeline_t* pip) {
SOKOL_ASSERT(pip);
SOKOL_ASSERT(pip->shader && (pip->cmn.shader_id.id == pip->shader->slot.id));
SOKOL_ASSERT(_sg.d3d11.ctx);
SOKOL_ASSERT(_sg.d3d11.in_pass);
SOKOL_ASSERT(pip->d3d11.rs && pip->d3d11.bs && pip->d3d11.dss && pip->d3d11.il);
_sg.d3d11.cur_pipeline = pip;
_sg.d3d11.cur_pipeline_id.id = pip->slot.id;
_sg.d3d11.use_indexed_draw = (pip->d3d11.index_format != DXGI_FORMAT_UNKNOWN);
_sg_d3d11_RSSetState(_sg.d3d11.ctx, pip->d3d11.rs);
_sg_d3d11_OMSetDepthStencilState(_sg.d3d11.ctx, pip->d3d11.dss, pip->d3d11.stencil_ref);
_sg_d3d11_OMSetBlendState(_sg.d3d11.ctx, pip->d3d11.bs, &pip->cmn.blend_color.r, 0xFFFFFFFF);
_sg_d3d11_IASetPrimitiveTopology(_sg.d3d11.ctx, pip->d3d11.topology);
_sg_d3d11_IASetInputLayout(_sg.d3d11.ctx, pip->d3d11.il);
_sg_d3d11_VSSetShader(_sg.d3d11.ctx, pip->shader->d3d11.vs, NULL, 0);
_sg_d3d11_VSSetConstantBuffers(_sg.d3d11.ctx, 0, SG_MAX_SHADERSTAGE_UBS, pip->shader->d3d11.stage[SG_SHADERSTAGE_VS].cbufs);
_sg_d3d11_PSSetShader(_sg.d3d11.ctx, pip->shader->d3d11.fs, NULL, 0);
_sg_d3d11_PSSetConstantBuffers(_sg.d3d11.ctx, 0, SG_MAX_SHADERSTAGE_UBS, pip->shader->d3d11.stage[SG_SHADERSTAGE_FS].cbufs);
}
_SOKOL_PRIVATE void _sg_d3d11_apply_bindings(
_sg_pipeline_t* pip,
_sg_buffer_t** vbs, const int* vb_offsets, int num_vbs,
_sg_buffer_t* ib, int ib_offset,
_sg_image_t** vs_imgs, int num_vs_imgs,
_sg_image_t** fs_imgs, int num_fs_imgs)
{
SOKOL_ASSERT(pip);
SOKOL_ASSERT(_sg.d3d11.ctx);
SOKOL_ASSERT(_sg.d3d11.in_pass);
/* gather all the D3D11 resources into arrays */
ID3D11Buffer* d3d11_ib = ib ? ib->d3d11.buf : 0;
ID3D11Buffer* d3d11_vbs[SG_MAX_SHADERSTAGE_BUFFERS];
UINT d3d11_vb_offsets[SG_MAX_SHADERSTAGE_BUFFERS];
ID3D11ShaderResourceView* d3d11_vs_srvs[SG_MAX_SHADERSTAGE_IMAGES];
ID3D11SamplerState* d3d11_vs_smps[SG_MAX_SHADERSTAGE_IMAGES];
ID3D11ShaderResourceView* d3d11_fs_srvs[SG_MAX_SHADERSTAGE_IMAGES];
ID3D11SamplerState* d3d11_fs_smps[SG_MAX_SHADERSTAGE_IMAGES];
int i;
for (i = 0; i < num_vbs; i++) {
SOKOL_ASSERT(vbs[i]->d3d11.buf);
d3d11_vbs[i] = vbs[i]->d3d11.buf;
d3d11_vb_offsets[i] = (UINT)vb_offsets[i];
}
for (; i < SG_MAX_SHADERSTAGE_BUFFERS; i++) {
d3d11_vbs[i] = 0;
d3d11_vb_offsets[i] = 0;
}
for (i = 0; i < num_vs_imgs; i++) {
SOKOL_ASSERT(vs_imgs[i]->d3d11.srv);
SOKOL_ASSERT(vs_imgs[i]->d3d11.smp);
d3d11_vs_srvs[i] = vs_imgs[i]->d3d11.srv;
d3d11_vs_smps[i] = vs_imgs[i]->d3d11.smp;
}
for (; i < SG_MAX_SHADERSTAGE_IMAGES; i++) {
d3d11_vs_srvs[i] = 0;
d3d11_vs_smps[i] = 0;
}
for (i = 0; i < num_fs_imgs; i++) {
SOKOL_ASSERT(fs_imgs[i]->d3d11.srv);
SOKOL_ASSERT(fs_imgs[i]->d3d11.smp);
d3d11_fs_srvs[i] = fs_imgs[i]->d3d11.srv;
d3d11_fs_smps[i] = fs_imgs[i]->d3d11.smp;
}
for (; i < SG_MAX_SHADERSTAGE_IMAGES; i++) {
d3d11_fs_srvs[i] = 0;
d3d11_fs_smps[i] = 0;
}
_sg_d3d11_IASetVertexBuffers(_sg.d3d11.ctx, 0, SG_MAX_SHADERSTAGE_BUFFERS, d3d11_vbs, pip->d3d11.vb_strides, d3d11_vb_offsets);
_sg_d3d11_IASetIndexBuffer(_sg.d3d11.ctx, d3d11_ib, pip->d3d11.index_format, (UINT)ib_offset);
_sg_d3d11_VSSetShaderResources(_sg.d3d11.ctx, 0, SG_MAX_SHADERSTAGE_IMAGES, d3d11_vs_srvs);
_sg_d3d11_VSSetSamplers(_sg.d3d11.ctx, 0, SG_MAX_SHADERSTAGE_IMAGES, d3d11_vs_smps);
_sg_d3d11_PSSetShaderResources(_sg.d3d11.ctx, 0, SG_MAX_SHADERSTAGE_IMAGES, d3d11_fs_srvs);
_sg_d3d11_PSSetSamplers(_sg.d3d11.ctx, 0, SG_MAX_SHADERSTAGE_IMAGES, d3d11_fs_smps);
}
_SOKOL_PRIVATE void _sg_d3d11_apply_uniforms(sg_shader_stage stage_index, int ub_index, const sg_range* data) {
SOKOL_ASSERT(_sg.d3d11.ctx && _sg.d3d11.in_pass);
SOKOL_ASSERT(_sg.d3d11.cur_pipeline && _sg.d3d11.cur_pipeline->slot.id == _sg.d3d11.cur_pipeline_id.id);
SOKOL_ASSERT(_sg.d3d11.cur_pipeline->shader && _sg.d3d11.cur_pipeline->shader->slot.id == _sg.d3d11.cur_pipeline->cmn.shader_id.id);
SOKOL_ASSERT(ub_index < _sg.d3d11.cur_pipeline->shader->cmn.stage[stage_index].num_uniform_blocks);
SOKOL_ASSERT(data->size == _sg.d3d11.cur_pipeline->shader->cmn.stage[stage_index].uniform_blocks[ub_index].size);
ID3D11Buffer* cb = _sg.d3d11.cur_pipeline->shader->d3d11.stage[stage_index].cbufs[ub_index];
SOKOL_ASSERT(cb);
_sg_d3d11_UpdateSubresource(_sg.d3d11.ctx, (ID3D11Resource*)cb, 0, NULL, data->ptr, 0, 0);
}
_SOKOL_PRIVATE void _sg_d3d11_draw(int base_element, int num_elements, int num_instances) {
SOKOL_ASSERT(_sg.d3d11.in_pass);
if (_sg.d3d11.use_indexed_draw) {
if (1 == num_instances) {
_sg_d3d11_DrawIndexed(_sg.d3d11.ctx, (UINT)num_elements, (UINT)base_element, 0);
}
else {
_sg_d3d11_DrawIndexedInstanced(_sg.d3d11.ctx, (UINT)num_elements, (UINT)num_instances, (UINT)base_element, 0, 0);
}
}
else {
if (1 == num_instances) {
_sg_d3d11_Draw(_sg.d3d11.ctx, (UINT)num_elements, (UINT)base_element);
}
else {
_sg_d3d11_DrawInstanced(_sg.d3d11.ctx, (UINT)num_elements, (UINT)num_instances, (UINT)base_element, 0);
}
}
}
_SOKOL_PRIVATE void _sg_d3d11_commit(void) {
SOKOL_ASSERT(!_sg.d3d11.in_pass);
}
_SOKOL_PRIVATE void _sg_d3d11_update_buffer(_sg_buffer_t* buf, const sg_range* data) {
SOKOL_ASSERT(buf && data && data->ptr && (data->size > 0));
SOKOL_ASSERT(_sg.d3d11.ctx);
SOKOL_ASSERT(buf->d3d11.buf);
D3D11_MAPPED_SUBRESOURCE d3d11_msr;
HRESULT hr = _sg_d3d11_Map(_sg.d3d11.ctx, (ID3D11Resource*)buf->d3d11.buf, 0, D3D11_MAP_WRITE_DISCARD, 0, &d3d11_msr);
_SOKOL_UNUSED(hr);
SOKOL_ASSERT(SUCCEEDED(hr));
memcpy(d3d11_msr.pData, data->ptr, data->size);
_sg_d3d11_Unmap(_sg.d3d11.ctx, (ID3D11Resource*)buf->d3d11.buf, 0);
}
_SOKOL_PRIVATE int _sg_d3d11_append_buffer(_sg_buffer_t* buf, const sg_range* data, bool new_frame) {
SOKOL_ASSERT(buf && data && data->ptr && (data->size > 0));
SOKOL_ASSERT(_sg.d3d11.ctx);
SOKOL_ASSERT(buf->d3d11.buf);
D3D11_MAP map_type = new_frame ? D3D11_MAP_WRITE_DISCARD : D3D11_MAP_WRITE_NO_OVERWRITE;
D3D11_MAPPED_SUBRESOURCE d3d11_msr;
HRESULT hr = _sg_d3d11_Map(_sg.d3d11.ctx, (ID3D11Resource*)buf->d3d11.buf, 0, map_type, 0, &d3d11_msr);
_SOKOL_UNUSED(hr);
SOKOL_ASSERT(SUCCEEDED(hr));
uint8_t* dst_ptr = (uint8_t*)d3d11_msr.pData + buf->cmn.append_pos;
memcpy(dst_ptr, data->ptr, data->size);
_sg_d3d11_Unmap(_sg.d3d11.ctx, (ID3D11Resource*)buf->d3d11.buf, 0);
/* NOTE: this is a requirement from WebGPU, but we want identical behaviour across all backend */
return _sg_roundup((int)data->size, 4);
}
_SOKOL_PRIVATE void _sg_d3d11_update_image(_sg_image_t* img, const sg_image_data* data) {
SOKOL_ASSERT(img && data);
SOKOL_ASSERT(_sg.d3d11.ctx);
SOKOL_ASSERT(img->d3d11.tex2d || img->d3d11.tex3d);
ID3D11Resource* d3d11_res = 0;
if (img->d3d11.tex3d) {
d3d11_res = (ID3D11Resource*) img->d3d11.tex3d;
}
else {
d3d11_res = (ID3D11Resource*) img->d3d11.tex2d;
}
SOKOL_ASSERT(d3d11_res);
const int num_faces = (img->cmn.type == SG_IMAGETYPE_CUBE) ? 6:1;
const int num_slices = (img->cmn.type == SG_IMAGETYPE_ARRAY) ? img->cmn.num_slices:1;
UINT subres_index = 0;
HRESULT hr;
_SOKOL_UNUSED(hr);
D3D11_MAPPED_SUBRESOURCE d3d11_msr;
for (int face_index = 0; face_index < num_faces; face_index++) {
for (int slice_index = 0; slice_index < num_slices; slice_index++) {
for (int mip_index = 0; mip_index < img->cmn.num_mipmaps; mip_index++, subres_index++) {
SOKOL_ASSERT(subres_index < (SG_MAX_MIPMAPS * SG_MAX_TEXTUREARRAY_LAYERS));
const int mip_width = ((img->cmn.width>>mip_index)>0) ? img->cmn.width>>mip_index : 1;
const int mip_height = ((img->cmn.height>>mip_index)>0) ? img->cmn.height>>mip_index : 1;
const int src_pitch = _sg_row_pitch(img->cmn.pixel_format, mip_width, 1);
const sg_range* subimg_data = &(data->subimage[face_index][mip_index]);
const size_t slice_size = subimg_data->size / (size_t)num_slices;
const size_t slice_offset = slice_size * (size_t)slice_index;
const uint8_t* slice_ptr = ((const uint8_t*)subimg_data->ptr) + slice_offset;
hr = _sg_d3d11_Map(_sg.d3d11.ctx, d3d11_res, subres_index, D3D11_MAP_WRITE_DISCARD, 0, &d3d11_msr);
SOKOL_ASSERT(SUCCEEDED(hr));
/* FIXME: need to handle difference in depth-pitch for 3D textures as well! */
if (src_pitch == (int)d3d11_msr.RowPitch) {
memcpy(d3d11_msr.pData, slice_ptr, slice_size);
}
else {
SOKOL_ASSERT(src_pitch < (int)d3d11_msr.RowPitch);
const uint8_t* src_ptr = slice_ptr;
uint8_t* dst_ptr = (uint8_t*) d3d11_msr.pData;
for (int row_index = 0; row_index < mip_height; row_index++) {
memcpy(dst_ptr, src_ptr, (size_t)src_pitch);
src_ptr += src_pitch;
dst_ptr += d3d11_msr.RowPitch;
}
}
_sg_d3d11_Unmap(_sg.d3d11.ctx, d3d11_res, subres_index);
}
}
}
}
/*== METAL BACKEND IMPLEMENTATION ============================================*/
#elif defined(SOKOL_METAL)
#if __has_feature(objc_arc)
#define _SG_OBJC_RETAIN(obj) { }
#define _SG_OBJC_RELEASE(obj) { obj = nil; }
#define _SG_OBJC_RELEASE_WITH_NULL(obj) { obj = [NSNull null]; }
#else
#define _SG_OBJC_RETAIN(obj) { [obj retain]; }
#define _SG_OBJC_RELEASE(obj) { [obj release]; obj = nil; }
#define _SG_OBJC_RELEASE_WITH_NULL(obj) { [obj release]; obj = [NSNull null]; }
#endif
/*-- enum translation functions ----------------------------------------------*/
_SOKOL_PRIVATE MTLLoadAction _sg_mtl_load_action(sg_action a) {
switch (a) {
case SG_ACTION_CLEAR: return MTLLoadActionClear;
case SG_ACTION_LOAD: return MTLLoadActionLoad;
case SG_ACTION_DONTCARE: return MTLLoadActionDontCare;
default: SOKOL_UNREACHABLE; return (MTLLoadAction)0;
}
}
_SOKOL_PRIVATE MTLResourceOptions _sg_mtl_buffer_resource_options(sg_usage usg) {
switch (usg) {
case SG_USAGE_IMMUTABLE:
return MTLResourceStorageModeShared;
case SG_USAGE_DYNAMIC:
case SG_USAGE_STREAM:
#if defined(_SG_TARGET_MACOS)
return MTLCPUCacheModeWriteCombined|MTLResourceStorageModeManaged;
#else
return MTLCPUCacheModeWriteCombined;
#endif
default:
SOKOL_UNREACHABLE;
return 0;
}
}
_SOKOL_PRIVATE MTLVertexStepFunction _sg_mtl_step_function(sg_vertex_step step) {
switch (step) {
case SG_VERTEXSTEP_PER_VERTEX: return MTLVertexStepFunctionPerVertex;
case SG_VERTEXSTEP_PER_INSTANCE: return MTLVertexStepFunctionPerInstance;
default: SOKOL_UNREACHABLE; return (MTLVertexStepFunction)0;
}
}
_SOKOL_PRIVATE MTLVertexFormat _sg_mtl_vertex_format(sg_vertex_format fmt) {
switch (fmt) {
case SG_VERTEXFORMAT_FLOAT: return MTLVertexFormatFloat;
case SG_VERTEXFORMAT_FLOAT2: return MTLVertexFormatFloat2;
case SG_VERTEXFORMAT_FLOAT3: return MTLVertexFormatFloat3;
case SG_VERTEXFORMAT_FLOAT4: return MTLVertexFormatFloat4;
case SG_VERTEXFORMAT_BYTE4: return MTLVertexFormatChar4;
case SG_VERTEXFORMAT_BYTE4N: return MTLVertexFormatChar4Normalized;
case SG_VERTEXFORMAT_UBYTE4: return MTLVertexFormatUChar4;
case SG_VERTEXFORMAT_UBYTE4N: return MTLVertexFormatUChar4Normalized;
case SG_VERTEXFORMAT_SHORT2: return MTLVertexFormatShort2;
case SG_VERTEXFORMAT_SHORT2N: return MTLVertexFormatShort2Normalized;
case SG_VERTEXFORMAT_USHORT2N: return MTLVertexFormatUShort2Normalized;
case SG_VERTEXFORMAT_SHORT4: return MTLVertexFormatShort4;
case SG_VERTEXFORMAT_SHORT4N: return MTLVertexFormatShort4Normalized;
case SG_VERTEXFORMAT_USHORT4N: return MTLVertexFormatUShort4Normalized;
case SG_VERTEXFORMAT_UINT10_N2: return MTLVertexFormatUInt1010102Normalized;
default: SOKOL_UNREACHABLE; return (MTLVertexFormat)0;
}
}
_SOKOL_PRIVATE MTLPrimitiveType _sg_mtl_primitive_type(sg_primitive_type t) {
switch (t) {
case SG_PRIMITIVETYPE_POINTS: return MTLPrimitiveTypePoint;
case SG_PRIMITIVETYPE_LINES: return MTLPrimitiveTypeLine;
case SG_PRIMITIVETYPE_LINE_STRIP: return MTLPrimitiveTypeLineStrip;
case SG_PRIMITIVETYPE_TRIANGLES: return MTLPrimitiveTypeTriangle;
case SG_PRIMITIVETYPE_TRIANGLE_STRIP: return MTLPrimitiveTypeTriangleStrip;
default: SOKOL_UNREACHABLE; return (MTLPrimitiveType)0;
}
}
_SOKOL_PRIVATE MTLPixelFormat _sg_mtl_pixel_format(sg_pixel_format fmt) {
switch (fmt) {
case SG_PIXELFORMAT_R8: return MTLPixelFormatR8Unorm;
case SG_PIXELFORMAT_R8SN: return MTLPixelFormatR8Snorm;
case SG_PIXELFORMAT_R8UI: return MTLPixelFormatR8Uint;
case SG_PIXELFORMAT_R8SI: return MTLPixelFormatR8Sint;
case SG_PIXELFORMAT_R16: return MTLPixelFormatR16Unorm;
case SG_PIXELFORMAT_R16SN: return MTLPixelFormatR16Snorm;
case SG_PIXELFORMAT_R16UI: return MTLPixelFormatR16Uint;
case SG_PIXELFORMAT_R16SI: return MTLPixelFormatR16Sint;
case SG_PIXELFORMAT_R16F: return MTLPixelFormatR16Float;
case SG_PIXELFORMAT_RG8: return MTLPixelFormatRG8Unorm;
case SG_PIXELFORMAT_RG8SN: return MTLPixelFormatRG8Snorm;
case SG_PIXELFORMAT_RG8UI: return MTLPixelFormatRG8Uint;
case SG_PIXELFORMAT_RG8SI: return MTLPixelFormatRG8Sint;
case SG_PIXELFORMAT_R32UI: return MTLPixelFormatR32Uint;
case SG_PIXELFORMAT_R32SI: return MTLPixelFormatR32Sint;
case SG_PIXELFORMAT_R32F: return MTLPixelFormatR32Float;
case SG_PIXELFORMAT_RG16: return MTLPixelFormatRG16Unorm;
case SG_PIXELFORMAT_RG16SN: return MTLPixelFormatRG16Snorm;
case SG_PIXELFORMAT_RG16UI: return MTLPixelFormatRG16Uint;
case SG_PIXELFORMAT_RG16SI: return MTLPixelFormatRG16Sint;
case SG_PIXELFORMAT_RG16F: return MTLPixelFormatRG16Float;
case SG_PIXELFORMAT_RGBA8: return MTLPixelFormatRGBA8Unorm;
case SG_PIXELFORMAT_RGBA8SN: return MTLPixelFormatRGBA8Snorm;
case SG_PIXELFORMAT_RGBA8UI: return MTLPixelFormatRGBA8Uint;
case SG_PIXELFORMAT_RGBA8SI: return MTLPixelFormatRGBA8Sint;
case SG_PIXELFORMAT_BGRA8: return MTLPixelFormatBGRA8Unorm;
case SG_PIXELFORMAT_RGB10A2: return MTLPixelFormatRGB10A2Unorm;
case SG_PIXELFORMAT_RG11B10F: return MTLPixelFormatRG11B10Float;
case SG_PIXELFORMAT_RG32UI: return MTLPixelFormatRG32Uint;
case SG_PIXELFORMAT_RG32SI: return MTLPixelFormatRG32Sint;
case SG_PIXELFORMAT_RG32F: return MTLPixelFormatRG32Float;
case SG_PIXELFORMAT_RGBA16: return MTLPixelFormatRGBA16Unorm;
case SG_PIXELFORMAT_RGBA16SN: return MTLPixelFormatRGBA16Snorm;
case SG_PIXELFORMAT_RGBA16UI: return MTLPixelFormatRGBA16Uint;
case SG_PIXELFORMAT_RGBA16SI: return MTLPixelFormatRGBA16Sint;
case SG_PIXELFORMAT_RGBA16F: return MTLPixelFormatRGBA16Float;
case SG_PIXELFORMAT_RGBA32UI: return MTLPixelFormatRGBA32Uint;
case SG_PIXELFORMAT_RGBA32SI: return MTLPixelFormatRGBA32Sint;
case SG_PIXELFORMAT_RGBA32F: return MTLPixelFormatRGBA32Float;
case SG_PIXELFORMAT_DEPTH: return MTLPixelFormatDepth32Float;
case SG_PIXELFORMAT_DEPTH_STENCIL: return MTLPixelFormatDepth32Float_Stencil8;
#if defined(_SG_TARGET_MACOS)
case SG_PIXELFORMAT_BC1_RGBA: return MTLPixelFormatBC1_RGBA;
case SG_PIXELFORMAT_BC2_RGBA: return MTLPixelFormatBC2_RGBA;
case SG_PIXELFORMAT_BC3_RGBA: return MTLPixelFormatBC3_RGBA;
case SG_PIXELFORMAT_BC4_R: return MTLPixelFormatBC4_RUnorm;
case SG_PIXELFORMAT_BC4_RSN: return MTLPixelFormatBC4_RSnorm;
case SG_PIXELFORMAT_BC5_RG: return MTLPixelFormatBC5_RGUnorm;
case SG_PIXELFORMAT_BC5_RGSN: return MTLPixelFormatBC5_RGSnorm;
case SG_PIXELFORMAT_BC6H_RGBF: return MTLPixelFormatBC6H_RGBFloat;
case SG_PIXELFORMAT_BC6H_RGBUF: return MTLPixelFormatBC6H_RGBUfloat;
case SG_PIXELFORMAT_BC7_RGBA: return MTLPixelFormatBC7_RGBAUnorm;
#else
case SG_PIXELFORMAT_PVRTC_RGB_2BPP: return MTLPixelFormatPVRTC_RGB_2BPP;
case SG_PIXELFORMAT_PVRTC_RGB_4BPP: return MTLPixelFormatPVRTC_RGB_4BPP;
case SG_PIXELFORMAT_PVRTC_RGBA_2BPP: return MTLPixelFormatPVRTC_RGBA_2BPP;
case SG_PIXELFORMAT_PVRTC_RGBA_4BPP: return MTLPixelFormatPVRTC_RGBA_4BPP;
case SG_PIXELFORMAT_ETC2_RGB8: return MTLPixelFormatETC2_RGB8;
case SG_PIXELFORMAT_ETC2_RGB8A1: return MTLPixelFormatETC2_RGB8A1;
case SG_PIXELFORMAT_ETC2_RGBA8: return MTLPixelFormatEAC_RGBA8;
case SG_PIXELFORMAT_ETC2_RG11: return MTLPixelFormatEAC_RG11Unorm;
case SG_PIXELFORMAT_ETC2_RG11SN: return MTLPixelFormatEAC_RG11Snorm;
#endif
default: return MTLPixelFormatInvalid;
}
}
_SOKOL_PRIVATE MTLColorWriteMask _sg_mtl_color_write_mask(sg_color_mask m) {
MTLColorWriteMask mtl_mask = MTLColorWriteMaskNone;
if (m & SG_COLORMASK_R) {
mtl_mask |= MTLColorWriteMaskRed;
}
if (m & SG_COLORMASK_G) {
mtl_mask |= MTLColorWriteMaskGreen;
}
if (m & SG_COLORMASK_B) {
mtl_mask |= MTLColorWriteMaskBlue;
}
if (m & SG_COLORMASK_A) {
mtl_mask |= MTLColorWriteMaskAlpha;
}
return mtl_mask;
}
_SOKOL_PRIVATE MTLBlendOperation _sg_mtl_blend_op(sg_blend_op op) {
switch (op) {
case SG_BLENDOP_ADD: return MTLBlendOperationAdd;
case SG_BLENDOP_SUBTRACT: return MTLBlendOperationSubtract;
case SG_BLENDOP_REVERSE_SUBTRACT: return MTLBlendOperationReverseSubtract;
default: SOKOL_UNREACHABLE; return (MTLBlendOperation)0;
}
}
_SOKOL_PRIVATE MTLBlendFactor _sg_mtl_blend_factor(sg_blend_factor f) {
switch (f) {
case SG_BLENDFACTOR_ZERO: return MTLBlendFactorZero;
case SG_BLENDFACTOR_ONE: return MTLBlendFactorOne;
case SG_BLENDFACTOR_SRC_COLOR: return MTLBlendFactorSourceColor;
case SG_BLENDFACTOR_ONE_MINUS_SRC_COLOR: return MTLBlendFactorOneMinusSourceColor;
case SG_BLENDFACTOR_SRC_ALPHA: return MTLBlendFactorSourceAlpha;
case SG_BLENDFACTOR_ONE_MINUS_SRC_ALPHA: return MTLBlendFactorOneMinusSourceAlpha;
case SG_BLENDFACTOR_DST_COLOR: return MTLBlendFactorDestinationColor;
case SG_BLENDFACTOR_ONE_MINUS_DST_COLOR: return MTLBlendFactorOneMinusDestinationColor;
case SG_BLENDFACTOR_DST_ALPHA: return MTLBlendFactorDestinationAlpha;
case SG_BLENDFACTOR_ONE_MINUS_DST_ALPHA: return MTLBlendFactorOneMinusDestinationAlpha;
case SG_BLENDFACTOR_SRC_ALPHA_SATURATED: return MTLBlendFactorSourceAlphaSaturated;
case SG_BLENDFACTOR_BLEND_COLOR: return MTLBlendFactorBlendColor;
case SG_BLENDFACTOR_ONE_MINUS_BLEND_COLOR: return MTLBlendFactorOneMinusBlendColor;
case SG_BLENDFACTOR_BLEND_ALPHA: return MTLBlendFactorBlendAlpha;
case SG_BLENDFACTOR_ONE_MINUS_BLEND_ALPHA: return MTLBlendFactorOneMinusBlendAlpha;
default: SOKOL_UNREACHABLE; return (MTLBlendFactor)0;
}
}
_SOKOL_PRIVATE MTLCompareFunction _sg_mtl_compare_func(sg_compare_func f) {
switch (f) {
case SG_COMPAREFUNC_NEVER: return MTLCompareFunctionNever;
case SG_COMPAREFUNC_LESS: return MTLCompareFunctionLess;
case SG_COMPAREFUNC_EQUAL: return MTLCompareFunctionEqual;
case SG_COMPAREFUNC_LESS_EQUAL: return MTLCompareFunctionLessEqual;
case SG_COMPAREFUNC_GREATER: return MTLCompareFunctionGreater;
case SG_COMPAREFUNC_NOT_EQUAL: return MTLCompareFunctionNotEqual;
case SG_COMPAREFUNC_GREATER_EQUAL: return MTLCompareFunctionGreaterEqual;
case SG_COMPAREFUNC_ALWAYS: return MTLCompareFunctionAlways;
default: SOKOL_UNREACHABLE; return (MTLCompareFunction)0;
}
}
_SOKOL_PRIVATE MTLStencilOperation _sg_mtl_stencil_op(sg_stencil_op op) {
switch (op) {
case SG_STENCILOP_KEEP: return MTLStencilOperationKeep;
case SG_STENCILOP_ZERO: return MTLStencilOperationZero;
case SG_STENCILOP_REPLACE: return MTLStencilOperationReplace;
case SG_STENCILOP_INCR_CLAMP: return MTLStencilOperationIncrementClamp;
case SG_STENCILOP_DECR_CLAMP: return MTLStencilOperationDecrementClamp;
case SG_STENCILOP_INVERT: return MTLStencilOperationInvert;
case SG_STENCILOP_INCR_WRAP: return MTLStencilOperationIncrementWrap;
case SG_STENCILOP_DECR_WRAP: return MTLStencilOperationDecrementWrap;
default: SOKOL_UNREACHABLE; return (MTLStencilOperation)0;
}
}
_SOKOL_PRIVATE MTLCullMode _sg_mtl_cull_mode(sg_cull_mode m) {
switch (m) {
case SG_CULLMODE_NONE: return MTLCullModeNone;
case SG_CULLMODE_FRONT: return MTLCullModeFront;
case SG_CULLMODE_BACK: return MTLCullModeBack;
default: SOKOL_UNREACHABLE; return (MTLCullMode)0;
}
}
_SOKOL_PRIVATE MTLWinding _sg_mtl_winding(sg_face_winding w) {
switch (w) {
case SG_FACEWINDING_CW: return MTLWindingClockwise;
case SG_FACEWINDING_CCW: return MTLWindingCounterClockwise;
default: SOKOL_UNREACHABLE; return (MTLWinding)0;
}
}
_SOKOL_PRIVATE MTLIndexType _sg_mtl_index_type(sg_index_type t) {
switch (t) {
case SG_INDEXTYPE_UINT16: return MTLIndexTypeUInt16;
case SG_INDEXTYPE_UINT32: return MTLIndexTypeUInt32;
default: SOKOL_UNREACHABLE; return (MTLIndexType)0;
}
}
_SOKOL_PRIVATE int _sg_mtl_index_size(sg_index_type t) {
switch (t) {
case SG_INDEXTYPE_NONE: return 0;
case SG_INDEXTYPE_UINT16: return 2;
case SG_INDEXTYPE_UINT32: return 4;
default: SOKOL_UNREACHABLE; return 0;
}
}
_SOKOL_PRIVATE MTLTextureType _sg_mtl_texture_type(sg_image_type t) {
switch (t) {
case SG_IMAGETYPE_2D: return MTLTextureType2D;
case SG_IMAGETYPE_CUBE: return MTLTextureTypeCube;
case SG_IMAGETYPE_3D: return MTLTextureType3D;
case SG_IMAGETYPE_ARRAY: return MTLTextureType2DArray;
default: SOKOL_UNREACHABLE; return (MTLTextureType)0;
}
}
_SOKOL_PRIVATE bool _sg_mtl_is_pvrtc(sg_pixel_format fmt) {
switch (fmt) {
case SG_PIXELFORMAT_PVRTC_RGB_2BPP:
case SG_PIXELFORMAT_PVRTC_RGB_4BPP:
case SG_PIXELFORMAT_PVRTC_RGBA_2BPP:
case SG_PIXELFORMAT_PVRTC_RGBA_4BPP:
return true;
default:
return false;
}
}
_SOKOL_PRIVATE MTLSamplerAddressMode _sg_mtl_address_mode(sg_wrap w) {
switch (w) {
case SG_WRAP_REPEAT: return MTLSamplerAddressModeRepeat;
case SG_WRAP_CLAMP_TO_EDGE: return MTLSamplerAddressModeClampToEdge;
#if defined(_SG_TARGET_MACOS)
case SG_WRAP_CLAMP_TO_BORDER: return MTLSamplerAddressModeClampToBorderColor;
#else
/* clamp-to-border not supported on iOS, fall back to clamp-to-edge */
case SG_WRAP_CLAMP_TO_BORDER: return MTLSamplerAddressModeClampToEdge;
#endif
case SG_WRAP_MIRRORED_REPEAT: return MTLSamplerAddressModeMirrorRepeat;
default: SOKOL_UNREACHABLE; return (MTLSamplerAddressMode)0;
}
}
#if defined(_SG_TARGET_MACOS)
_SOKOL_PRIVATE MTLSamplerBorderColor _sg_mtl_border_color(sg_border_color c) {
switch (c) {
case SG_BORDERCOLOR_TRANSPARENT_BLACK: return MTLSamplerBorderColorTransparentBlack;
case SG_BORDERCOLOR_OPAQUE_BLACK: return MTLSamplerBorderColorOpaqueBlack;
case SG_BORDERCOLOR_OPAQUE_WHITE: return MTLSamplerBorderColorOpaqueWhite;
default: SOKOL_UNREACHABLE; return (MTLSamplerBorderColor)0;
}
}
#endif
_SOKOL_PRIVATE MTLSamplerMinMagFilter _sg_mtl_minmag_filter(sg_filter f) {
switch (f) {
case SG_FILTER_NEAREST:
case SG_FILTER_NEAREST_MIPMAP_NEAREST:
case SG_FILTER_NEAREST_MIPMAP_LINEAR:
return MTLSamplerMinMagFilterNearest;
case SG_FILTER_LINEAR:
case SG_FILTER_LINEAR_MIPMAP_NEAREST:
case SG_FILTER_LINEAR_MIPMAP_LINEAR:
return MTLSamplerMinMagFilterLinear;
default:
SOKOL_UNREACHABLE; return (MTLSamplerMinMagFilter)0;
}
}
_SOKOL_PRIVATE MTLSamplerMipFilter _sg_mtl_mip_filter(sg_filter f) {
switch (f) {
case SG_FILTER_NEAREST:
case SG_FILTER_LINEAR:
return MTLSamplerMipFilterNotMipmapped;
case SG_FILTER_NEAREST_MIPMAP_NEAREST:
case SG_FILTER_LINEAR_MIPMAP_NEAREST:
return MTLSamplerMipFilterNearest;
case SG_FILTER_NEAREST_MIPMAP_LINEAR:
case SG_FILTER_LINEAR_MIPMAP_LINEAR:
return MTLSamplerMipFilterLinear;
default:
SOKOL_UNREACHABLE; return (MTLSamplerMipFilter)0;
}
}
/*-- a pool for all Metal resource objects, with deferred release queue -------*/
_SOKOL_PRIVATE void _sg_mtl_init_pool(const sg_desc* desc) {
_sg.mtl.idpool.num_slots = 2 *
(
2 * desc->buffer_pool_size +
5 * desc->image_pool_size +
4 * desc->shader_pool_size +
2 * desc->pipeline_pool_size +
desc->pass_pool_size
);
_sg.mtl.idpool.pool = [NSMutableArray arrayWithCapacity:(NSUInteger)_sg.mtl.idpool.num_slots];
_SG_OBJC_RETAIN(_sg.mtl.idpool.pool);
NSNull* null = [NSNull null];
for (int i = 0; i < _sg.mtl.idpool.num_slots; i++) {
[_sg.mtl.idpool.pool addObject:null];
}
SOKOL_ASSERT([_sg.mtl.idpool.pool count] == (NSUInteger)_sg.mtl.idpool.num_slots);
/* a queue of currently free slot indices */
_sg.mtl.idpool.free_queue_top = 0;
_sg.mtl.idpool.free_queue = (int*)SOKOL_MALLOC((size_t)_sg.mtl.idpool.num_slots * sizeof(int));
/* pool slot 0 is reserved! */
for (int i = _sg.mtl.idpool.num_slots-1; i >= 1; i--) {
_sg.mtl.idpool.free_queue[_sg.mtl.idpool.free_queue_top++] = i;
}
/* a circular queue which holds release items (frame index
when a resource is to be released, and the resource's
pool index
*/
_sg.mtl.idpool.release_queue_front = 0;
_sg.mtl.idpool.release_queue_back = 0;
_sg.mtl.idpool.release_queue = (_sg_mtl_release_item_t*)SOKOL_MALLOC((size_t)_sg.mtl.idpool.num_slots * sizeof(_sg_mtl_release_item_t));
for (int i = 0; i < _sg.mtl.idpool.num_slots; i++) {
_sg.mtl.idpool.release_queue[i].frame_index = 0;
_sg.mtl.idpool.release_queue[i].slot_index = _SG_MTL_INVALID_SLOT_INDEX;
}
}
_SOKOL_PRIVATE void _sg_mtl_destroy_pool(void) {
SOKOL_FREE(_sg.mtl.idpool.release_queue); _sg.mtl.idpool.release_queue = 0;
SOKOL_FREE(_sg.mtl.idpool.free_queue); _sg.mtl.idpool.free_queue = 0;
_SG_OBJC_RELEASE(_sg.mtl.idpool.pool);
}
/* get a new free resource pool slot */
_SOKOL_PRIVATE int _sg_mtl_alloc_pool_slot(void) {
SOKOL_ASSERT(_sg.mtl.idpool.free_queue_top > 0);
const int slot_index = _sg.mtl.idpool.free_queue[--_sg.mtl.idpool.free_queue_top];
SOKOL_ASSERT((slot_index > 0) && (slot_index < _sg.mtl.idpool.num_slots));
return slot_index;
}
/* put a free resource pool slot back into the free-queue */
_SOKOL_PRIVATE void _sg_mtl_free_pool_slot(int slot_index) {
SOKOL_ASSERT(_sg.mtl.idpool.free_queue_top < _sg.mtl.idpool.num_slots);
SOKOL_ASSERT((slot_index > 0) && (slot_index < _sg.mtl.idpool.num_slots));
_sg.mtl.idpool.free_queue[_sg.mtl.idpool.free_queue_top++] = slot_index;
}
/* add an MTLResource to the pool, return pool index or 0 if input was 'nil' */
_SOKOL_PRIVATE int _sg_mtl_add_resource(id res) {
if (nil == res) {
return _SG_MTL_INVALID_SLOT_INDEX;
}
const int slot_index = _sg_mtl_alloc_pool_slot();
SOKOL_ASSERT([NSNull null] == _sg.mtl.idpool.pool[(NSUInteger)slot_index]);
_sg.mtl.idpool.pool[(NSUInteger)slot_index] = res;
return slot_index;
}
/* mark an MTLResource for release, this will put the resource into the
deferred-release queue, and the resource will then be released N frames later,
the special pool index 0 will be ignored (this means that a nil
value was provided to _sg_mtl_add_resource()
*/
_SOKOL_PRIVATE void _sg_mtl_release_resource(uint32_t frame_index, int slot_index) {
if (slot_index == _SG_MTL_INVALID_SLOT_INDEX) {
return;
}
SOKOL_ASSERT((slot_index > 0) && (slot_index < _sg.mtl.idpool.num_slots));
SOKOL_ASSERT([NSNull null] != _sg.mtl.idpool.pool[(NSUInteger)slot_index]);
int release_index = _sg.mtl.idpool.release_queue_front++;
if (_sg.mtl.idpool.release_queue_front >= _sg.mtl.idpool.num_slots) {
/* wrap-around */
_sg.mtl.idpool.release_queue_front = 0;
}
/* release queue full? */
SOKOL_ASSERT(_sg.mtl.idpool.release_queue_front != _sg.mtl.idpool.release_queue_back);
SOKOL_ASSERT(0 == _sg.mtl.idpool.release_queue[release_index].frame_index);
const uint32_t safe_to_release_frame_index = frame_index + SG_NUM_INFLIGHT_FRAMES + 1;
_sg.mtl.idpool.release_queue[release_index].frame_index = safe_to_release_frame_index;
_sg.mtl.idpool.release_queue[release_index].slot_index = slot_index;
}
/* run garbage-collection pass on all resources in the release-queue */
_SOKOL_PRIVATE void _sg_mtl_garbage_collect(uint32_t frame_index) {
while (_sg.mtl.idpool.release_queue_back != _sg.mtl.idpool.release_queue_front) {
if (frame_index < _sg.mtl.idpool.release_queue[_sg.mtl.idpool.release_queue_back].frame_index) {
/* don't need to check further, release-items past this are too young */
break;
}
/* safe to release this resource */
const int slot_index = _sg.mtl.idpool.release_queue[_sg.mtl.idpool.release_queue_back].slot_index;
SOKOL_ASSERT((slot_index > 0) && (slot_index < _sg.mtl.idpool.num_slots));
SOKOL_ASSERT(_sg.mtl.idpool.pool[(NSUInteger)slot_index] != [NSNull null]);
_SG_OBJC_RELEASE_WITH_NULL(_sg.mtl.idpool.pool[(NSUInteger)slot_index]);
/* put the now free pool index back on the free queue */
_sg_mtl_free_pool_slot(slot_index);
/* reset the release queue slot and advance the back index */
_sg.mtl.idpool.release_queue[_sg.mtl.idpool.release_queue_back].frame_index = 0;
_sg.mtl.idpool.release_queue[_sg.mtl.idpool.release_queue_back].slot_index = _SG_MTL_INVALID_SLOT_INDEX;
_sg.mtl.idpool.release_queue_back++;
if (_sg.mtl.idpool.release_queue_back >= _sg.mtl.idpool.num_slots) {
/* wrap-around */
_sg.mtl.idpool.release_queue_back = 0;
}
}
}
_SOKOL_PRIVATE id _sg_mtl_id(int slot_index) {
return _sg.mtl.idpool.pool[(NSUInteger)slot_index];
}
_SOKOL_PRIVATE void _sg_mtl_init_sampler_cache(const sg_desc* desc) {
SOKOL_ASSERT(desc->sampler_cache_size > 0);
_sg_smpcache_init(&_sg.mtl.sampler_cache, desc->sampler_cache_size);
}
/* destroy the sampler cache, and release all sampler objects */
_SOKOL_PRIVATE void _sg_mtl_destroy_sampler_cache(uint32_t frame_index) {
SOKOL_ASSERT(_sg.mtl.sampler_cache.items);
SOKOL_ASSERT(_sg.mtl.sampler_cache.num_items <= _sg.mtl.sampler_cache.capacity);
for (int i = 0; i < _sg.mtl.sampler_cache.num_items; i++) {
_sg_mtl_release_resource(frame_index, (int)_sg_smpcache_sampler(&_sg.mtl.sampler_cache, i));
}
_sg_smpcache_discard(&_sg.mtl.sampler_cache);
}
/*
create and add an MTLSamplerStateObject and return its resource pool index,
reuse identical sampler state if one exists
*/
_SOKOL_PRIVATE int _sg_mtl_create_sampler(id<MTLDevice> mtl_device, const sg_image_desc* img_desc) {
SOKOL_ASSERT(img_desc);
int index = _sg_smpcache_find_item(&_sg.mtl.sampler_cache, img_desc);
if (index >= 0) {
/* reuse existing sampler */
return (int)_sg_smpcache_sampler(&_sg.mtl.sampler_cache, index);
}
else {
/* create a new Metal sampler state object and add to sampler cache */
MTLSamplerDescriptor* mtl_desc = [[MTLSamplerDescriptor alloc] init];
mtl_desc.sAddressMode = _sg_mtl_address_mode(img_desc->wrap_u);
mtl_desc.tAddressMode = _sg_mtl_address_mode(img_desc->wrap_v);
if (SG_IMAGETYPE_3D == img_desc->type) {
mtl_desc.rAddressMode = _sg_mtl_address_mode(img_desc->wrap_w);
}
#if defined(_SG_TARGET_MACOS)
mtl_desc.borderColor = _sg_mtl_border_color(img_desc->border_color);
#endif
mtl_desc.minFilter = _sg_mtl_minmag_filter(img_desc->min_filter);
mtl_desc.magFilter = _sg_mtl_minmag_filter(img_desc->mag_filter);
mtl_desc.mipFilter = _sg_mtl_mip_filter(img_desc->min_filter);
mtl_desc.lodMinClamp = img_desc->min_lod;
mtl_desc.lodMaxClamp = img_desc->max_lod;
mtl_desc.maxAnisotropy = img_desc->max_anisotropy;
mtl_desc.normalizedCoordinates = YES;
id<MTLSamplerState> mtl_sampler = [mtl_device newSamplerStateWithDescriptor:mtl_desc];
_SG_OBJC_RELEASE(mtl_desc);
int sampler_handle = _sg_mtl_add_resource(mtl_sampler);
_sg_smpcache_add_item(&_sg.mtl.sampler_cache, img_desc, (uintptr_t)sampler_handle);
return sampler_handle;
}
}
_SOKOL_PRIVATE void _sg_mtl_clear_state_cache(void) {
memset(&_sg.mtl.state_cache, 0, sizeof(_sg.mtl.state_cache));
}
/* https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf */
_SOKOL_PRIVATE void _sg_mtl_init_caps(void) {
#if defined(_SG_TARGET_MACOS)
_sg.backend = SG_BACKEND_METAL_MACOS;
#elif defined(_SG_TARGET_IOS)
#if defined(_SG_TARGET_IOS_SIMULATOR)
_sg.backend = SG_BACKEND_METAL_SIMULATOR;
#else
_sg.backend = SG_BACKEND_METAL_IOS;
#endif
#endif
_sg.features.instancing = true;
_sg.features.origin_top_left = true;
_sg.features.multiple_render_targets = true;
_sg.features.msaa_render_targets = true;
_sg.features.imagetype_3d = true;
_sg.features.imagetype_array = true;
#if defined(_SG_TARGET_MACOS)
_sg.features.image_clamp_to_border = true;
#else
_sg.features.image_clamp_to_border = false;
#endif
_sg.features.mrt_independent_blend_state = true;
_sg.features.mrt_independent_write_mask = true;
#if defined(_SG_TARGET_MACOS)
_sg.limits.max_image_size_2d = 16 * 1024;
_sg.limits.max_image_size_cube = 16 * 1024;
_sg.limits.max_image_size_3d = 2 * 1024;
_sg.limits.max_image_size_array = 16 * 1024;
_sg.limits.max_image_array_layers = 2 * 1024;
#else
/* newer iOS devices support 16k textures */
_sg.limits.max_image_size_2d = 8 * 1024;
_sg.limits.max_image_size_cube = 8 * 1024;
_sg.limits.max_image_size_3d = 2 * 1024;
_sg.limits.max_image_size_array = 8 * 1024;
_sg.limits.max_image_array_layers = 2 * 1024;
#endif
_sg.limits.max_vertex_attrs = SG_MAX_VERTEX_ATTRIBUTES;
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_R8]);
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_R8SN]);
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_R8UI]);
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_R8SI]);
#if defined(_SG_TARGET_MACOS)
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_R16]);
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_R16SN]);
#else
_sg_pixelformat_sfbr(&_sg.formats[SG_PIXELFORMAT_R16]);
_sg_pixelformat_sfbr(&_sg.formats[SG_PIXELFORMAT_R16SN]);
#endif
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_R16UI]);
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_R16SI]);
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_R16F]);
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RG8]);
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RG8SN]);
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RG8UI]);
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RG8SI]);
_sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_R32UI]);
_sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_R32SI]);
#if defined(_SG_TARGET_MACOS)
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_R32F]);
#else
_sg_pixelformat_sbr(&_sg.formats[SG_PIXELFORMAT_R32F]);
#endif
#if defined(_SG_TARGET_MACOS)
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RG16]);
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RG16SN]);
#else
_sg_pixelformat_sfbr(&_sg.formats[SG_PIXELFORMAT_RG16]);
_sg_pixelformat_sfbr(&_sg.formats[SG_PIXELFORMAT_RG16SN]);
#endif
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RG16UI]);
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RG16SI]);
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RG16F]);
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RGBA8]);
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RGBA8SN]);
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RGBA8UI]);
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RGBA8SI]);
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_BGRA8]);
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RGB10A2]);
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RG11B10F]);
#if defined(_SG_TARGET_MACOS)
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RG32UI]);
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RG32SI]);
#else
_sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_RG32UI]);
_sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_RG32SI]);
#endif
#if defined(_SG_TARGET_MACOS)
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RG32F]);
#else
_sg_pixelformat_sbr(&_sg.formats[SG_PIXELFORMAT_RG32F]);
#endif
#if defined(_SG_TARGET_MACOS)
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RGBA16]);
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RGBA16SN]);
#else
_sg_pixelformat_sfbr(&_sg.formats[SG_PIXELFORMAT_RGBA16]);
_sg_pixelformat_sfbr(&_sg.formats[SG_PIXELFORMAT_RGBA16SN]);
#endif
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RGBA16UI]);
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RGBA16SI]);
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RGBA16F]);
#if defined(_SG_TARGET_MACOS)
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RGBA32UI]);
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RGBA32SI]);
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RGBA32F]);
#else
_sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_RGBA32UI]);
_sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_RGBA32SI]);
_sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_RGBA32F]);
#endif
_sg_pixelformat_srmd(&_sg.formats[SG_PIXELFORMAT_DEPTH]);
_sg_pixelformat_srmd(&_sg.formats[SG_PIXELFORMAT_DEPTH_STENCIL]);
#if defined(_SG_TARGET_MACOS)
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC1_RGBA]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC2_RGBA]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC3_RGBA]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC4_R]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC4_RSN]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC5_RG]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC5_RGSN]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC6H_RGBF]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC6H_RGBUF]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC7_RGBA]);
#else
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_PVRTC_RGB_2BPP]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_PVRTC_RGB_4BPP]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_PVRTC_RGBA_2BPP]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_PVRTC_RGBA_4BPP]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_ETC2_RGB8]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_ETC2_RGB8A1]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_ETC2_RGBA8]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_ETC2_RG11]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_ETC2_RG11SN]);
#endif
}
/*-- main Metal backend state and functions ----------------------------------*/
_SOKOL_PRIVATE void _sg_mtl_setup_backend(const sg_desc* desc) {
/* assume already zero-initialized */
SOKOL_ASSERT(desc);
SOKOL_ASSERT(desc->context.metal.device);
SOKOL_ASSERT(desc->context.metal.renderpass_descriptor_cb || desc->context.metal.renderpass_descriptor_userdata_cb);
SOKOL_ASSERT(desc->context.metal.drawable_cb || desc->context.metal.drawable_userdata_cb);
SOKOL_ASSERT(desc->uniform_buffer_size > 0);
_sg_mtl_init_pool(desc);
_sg_mtl_init_sampler_cache(desc);
_sg_mtl_clear_state_cache();
_sg.mtl.valid = true;
_sg.mtl.renderpass_descriptor_cb = desc->context.metal.renderpass_descriptor_cb;
_sg.mtl.renderpass_descriptor_userdata_cb = desc->context.metal.renderpass_descriptor_userdata_cb;
_sg.mtl.drawable_cb = desc->context.metal.drawable_cb;
_sg.mtl.drawable_userdata_cb = desc->context.metal.drawable_userdata_cb;
_sg.mtl.user_data = desc->context.metal.user_data;
_sg.mtl.frame_index = 1;
_sg.mtl.ub_size = desc->uniform_buffer_size;
_sg.mtl.sem = dispatch_semaphore_create(SG_NUM_INFLIGHT_FRAMES);
_sg.mtl.device = (__bridge id<MTLDevice>) desc->context.metal.device;
_sg.mtl.cmd_queue = [_sg.mtl.device newCommandQueue];
MTLResourceOptions res_opts = MTLResourceCPUCacheModeWriteCombined;
#if defined(_SG_TARGET_MACOS)
res_opts |= MTLResourceStorageModeManaged;
#endif
for (int i = 0; i < SG_NUM_INFLIGHT_FRAMES; i++) {
_sg.mtl.uniform_buffers[i] = [_sg.mtl.device
newBufferWithLength:(NSUInteger)_sg.mtl.ub_size
options:res_opts
];
}
_sg_mtl_init_caps();
}
_SOKOL_PRIVATE void _sg_mtl_discard_backend(void) {
SOKOL_ASSERT(_sg.mtl.valid);
/* wait for the last frame to finish */
for (int i = 0; i < SG_NUM_INFLIGHT_FRAMES; i++) {
dispatch_semaphore_wait(_sg.mtl.sem, DISPATCH_TIME_FOREVER);
}
/* semaphore must be "relinquished" before destruction */
for (int i = 0; i < SG_NUM_INFLIGHT_FRAMES; i++) {
dispatch_semaphore_signal(_sg.mtl.sem);
}
_sg_mtl_destroy_sampler_cache(_sg.mtl.frame_index);
_sg_mtl_garbage_collect(_sg.mtl.frame_index + SG_NUM_INFLIGHT_FRAMES + 2);
_sg_mtl_destroy_pool();
_sg.mtl.valid = false;
_SG_OBJC_RELEASE(_sg.mtl.sem);
_SG_OBJC_RELEASE(_sg.mtl.device);
_SG_OBJC_RELEASE(_sg.mtl.cmd_queue);
for (int i = 0; i < SG_NUM_INFLIGHT_FRAMES; i++) {
_SG_OBJC_RELEASE(_sg.mtl.uniform_buffers[i]);
}
/* NOTE: MTLCommandBuffer and MTLRenderCommandEncoder are auto-released */
_sg.mtl.cmd_buffer = nil;
_sg.mtl.cmd_encoder = nil;
}
_SOKOL_PRIVATE void _sg_mtl_bind_uniform_buffers(void) {
SOKOL_ASSERT(nil != _sg.mtl.cmd_encoder);
for (int slot = 0; slot < SG_MAX_SHADERSTAGE_UBS; slot++) {
[_sg.mtl.cmd_encoder
setVertexBuffer:_sg.mtl.uniform_buffers[_sg.mtl.cur_frame_rotate_index]
offset:0
atIndex:(NSUInteger)slot];
[_sg.mtl.cmd_encoder
setFragmentBuffer:_sg.mtl.uniform_buffers[_sg.mtl.cur_frame_rotate_index]
offset:0
atIndex:(NSUInteger)slot];
}
}
_SOKOL_PRIVATE void _sg_mtl_reset_state_cache(void) {
_sg_mtl_clear_state_cache();
/* need to restore the uniform buffer binding (normally happens in
_sg_mtl_begin_pass()
*/
if (nil != _sg.mtl.cmd_encoder) {
_sg_mtl_bind_uniform_buffers();
}
}
_SOKOL_PRIVATE sg_resource_state _sg_mtl_create_context(_sg_context_t* ctx) {
SOKOL_ASSERT(ctx);
_SOKOL_UNUSED(ctx);
return SG_RESOURCESTATE_VALID;
}
_SOKOL_PRIVATE void _sg_mtl_destroy_context(_sg_context_t* ctx) {
SOKOL_ASSERT(ctx);
_SOKOL_UNUSED(ctx);
/* empty */
}
_SOKOL_PRIVATE void _sg_mtl_activate_context(_sg_context_t* ctx) {
_SOKOL_UNUSED(ctx);
_sg_mtl_clear_state_cache();
}
_SOKOL_PRIVATE sg_resource_state _sg_mtl_create_buffer(_sg_buffer_t* buf, const sg_buffer_desc* desc) {
SOKOL_ASSERT(buf && desc);
_sg_buffer_common_init(&buf->cmn, desc);
const bool injected = (0 != desc->mtl_buffers[0]);
MTLResourceOptions mtl_options = _sg_mtl_buffer_resource_options(buf->cmn.usage);
for (int slot = 0; slot < buf->cmn.num_slots; slot++) {
id<MTLBuffer> mtl_buf;
if (injected) {
SOKOL_ASSERT(desc->mtl_buffers[slot]);
mtl_buf = (__bridge id<MTLBuffer>) desc->mtl_buffers[slot];
}
else {
if (buf->cmn.usage == SG_USAGE_IMMUTABLE) {
SOKOL_ASSERT(desc->data.ptr);
mtl_buf = [_sg.mtl.device newBufferWithBytes:desc->data.ptr length:(NSUInteger)buf->cmn.size options:mtl_options];
}
else {
mtl_buf = [_sg.mtl.device newBufferWithLength:(NSUInteger)buf->cmn.size options:mtl_options];
}
}
buf->mtl.buf[slot] = _sg_mtl_add_resource(mtl_buf);
}
return SG_RESOURCESTATE_VALID;
}
_SOKOL_PRIVATE void _sg_mtl_destroy_buffer(_sg_buffer_t* buf) {
SOKOL_ASSERT(buf);
for (int slot = 0; slot < buf->cmn.num_slots; slot++) {
/* it's valid to call release resource with '0' */
_sg_mtl_release_resource(_sg.mtl.frame_index, buf->mtl.buf[slot]);
}
}
_SOKOL_PRIVATE void _sg_mtl_copy_image_data(const _sg_image_t* img, __unsafe_unretained id<MTLTexture> mtl_tex, const sg_image_data* data) {
const int num_faces = (img->cmn.type == SG_IMAGETYPE_CUBE) ? 6:1;
const int num_slices = (img->cmn.type == SG_IMAGETYPE_ARRAY) ? img->cmn.num_slices : 1;
for (int face_index = 0; face_index < num_faces; face_index++) {
for (int mip_index = 0; mip_index < img->cmn.num_mipmaps; mip_index++) {
SOKOL_ASSERT(data->subimage[face_index][mip_index].ptr);
SOKOL_ASSERT(data->subimage[face_index][mip_index].size > 0);
const uint8_t* data_ptr = (const uint8_t*)data->subimage[face_index][mip_index].ptr;
const int mip_width = _sg_max(img->cmn.width >> mip_index, 1);
const int mip_height = _sg_max(img->cmn.height >> mip_index, 1);
/* special case PVRTC formats: bytePerRow must be 0 */
int bytes_per_row = 0;
int bytes_per_slice = _sg_surface_pitch(img->cmn.pixel_format, mip_width, mip_height, 1);
if (!_sg_mtl_is_pvrtc(img->cmn.pixel_format)) {
bytes_per_row = _sg_row_pitch(img->cmn.pixel_format, mip_width, 1);
}
MTLRegion region;
if (img->cmn.type == SG_IMAGETYPE_3D) {
const int mip_depth = _sg_max(img->cmn.num_slices >> mip_index, 1);
region = MTLRegionMake3D(0, 0, 0, (NSUInteger)mip_width, (NSUInteger)mip_height, (NSUInteger)mip_depth);
/* FIXME: apparently the minimal bytes_per_image size for 3D texture
is 4 KByte... somehow need to handle this */
}
else {
region = MTLRegionMake2D(0, 0, (NSUInteger)mip_width, (NSUInteger)mip_height);
}
for (int slice_index = 0; slice_index < num_slices; slice_index++) {
const int mtl_slice_index = (img->cmn.type == SG_IMAGETYPE_CUBE) ? face_index : slice_index;
const int slice_offset = slice_index * bytes_per_slice;
SOKOL_ASSERT((slice_offset + bytes_per_slice) <= (int)data->subimage[face_index][mip_index].size);
[mtl_tex replaceRegion:region
mipmapLevel:(NSUInteger)mip_index
slice:(NSUInteger)mtl_slice_index
withBytes:data_ptr + slice_offset
bytesPerRow:(NSUInteger)bytes_per_row
bytesPerImage:(NSUInteger)bytes_per_slice];
}
}
}
}
/*
FIXME: METAL RESOURCE STORAGE MODE FOR macOS AND iOS
For immutable textures on macOS, the recommended procedure is to create
a MTLStorageModeManaged texture with the immutable content first,
and then use the GPU to blit the content into a MTLStorageModePrivate
texture before the first use.
On iOS use the same one-time-blit procedure, but from a
MTLStorageModeShared to a MTLStorageModePrivate texture.
It probably makes sense to handle this in a separate 'resource manager'
with a recycable pool of blit-source-textures?
*/
/* initialize MTLTextureDescritor with common attributes */
_SOKOL_PRIVATE bool _sg_mtl_init_texdesc_common(MTLTextureDescriptor* mtl_desc, _sg_image_t* img) {
mtl_desc.textureType = _sg_mtl_texture_type(img->cmn.type);
mtl_desc.pixelFormat = _sg_mtl_pixel_format(img->cmn.pixel_format);
if (MTLPixelFormatInvalid == mtl_desc.pixelFormat) {
SOKOL_LOG("Unsupported texture pixel format!\n");
return false;
}
mtl_desc.width = (NSUInteger)img->cmn.width;
mtl_desc.height = (NSUInteger)img->cmn.height;
if (SG_IMAGETYPE_3D == img->cmn.type) {
mtl_desc.depth = (NSUInteger)img->cmn.num_slices;
}
else {
mtl_desc.depth = 1;
}
mtl_desc.mipmapLevelCount = (NSUInteger)img->cmn.num_mipmaps;
if (SG_IMAGETYPE_ARRAY == img->cmn.type) {
mtl_desc.arrayLength = (NSUInteger)img->cmn.num_slices;
}
else {
mtl_desc.arrayLength = 1;
}
mtl_desc.usage = MTLTextureUsageShaderRead;
if (img->cmn.usage != SG_USAGE_IMMUTABLE) {
mtl_desc.cpuCacheMode = MTLCPUCacheModeWriteCombined;
}
#if defined(_SG_TARGET_MACOS)
/* macOS: use managed textures */
mtl_desc.resourceOptions = MTLResourceStorageModeManaged;
mtl_desc.storageMode = MTLStorageModeManaged;
#else
/* iOS: use CPU/GPU shared memory */
mtl_desc.resourceOptions = MTLResourceStorageModeShared;
mtl_desc.storageMode = MTLStorageModeShared;
#endif
return true;
}
/* initialize MTLTextureDescritor with rendertarget attributes */
_SOKOL_PRIVATE void _sg_mtl_init_texdesc_rt(MTLTextureDescriptor* mtl_desc, _sg_image_t* img) {
SOKOL_ASSERT(img->cmn.render_target);
_SOKOL_UNUSED(img);
/* reset the cpuCacheMode to 'default' */
mtl_desc.cpuCacheMode = MTLCPUCacheModeDefaultCache;
/* render targets are only visible to the GPU */
mtl_desc.resourceOptions = MTLResourceStorageModePrivate;
mtl_desc.storageMode = MTLStorageModePrivate;
/* non-MSAA render targets are shader-readable */
mtl_desc.usage = MTLTextureUsageShaderRead | MTLTextureUsageRenderTarget;
}
/* initialize MTLTextureDescritor with MSAA attributes */
_SOKOL_PRIVATE void _sg_mtl_init_texdesc_rt_msaa(MTLTextureDescriptor* mtl_desc, _sg_image_t* img) {
SOKOL_ASSERT(img->cmn.sample_count > 1);
/* reset the cpuCacheMode to 'default' */
mtl_desc.cpuCacheMode = MTLCPUCacheModeDefaultCache;
/* render targets are only visible to the GPU */
mtl_desc.resourceOptions = MTLResourceStorageModePrivate;
mtl_desc.storageMode = MTLStorageModePrivate;
/* MSAA render targets are not shader-readable (instead they are resolved) */
mtl_desc.usage = MTLTextureUsageRenderTarget;
mtl_desc.textureType = MTLTextureType2DMultisample;
mtl_desc.depth = 1;
mtl_desc.arrayLength = 1;
mtl_desc.mipmapLevelCount = 1;
mtl_desc.sampleCount = (NSUInteger)img->cmn.sample_count;
}
_SOKOL_PRIVATE sg_resource_state _sg_mtl_create_image(_sg_image_t* img, const sg_image_desc* desc) {
SOKOL_ASSERT(img && desc);
_sg_image_common_init(&img->cmn, desc);
const bool injected = (0 != desc->mtl_textures[0]);
const bool msaa = (img->cmn.sample_count > 1);
/* first initialize all Metal resource pool slots to 'empty' */
for (int i = 0; i < SG_NUM_INFLIGHT_FRAMES; i++) {
img->mtl.tex[i] = _sg_mtl_add_resource(nil);
}
img->mtl.sampler_state = _sg_mtl_add_resource(nil);
img->mtl.depth_tex = _sg_mtl_add_resource(nil);
img->mtl.msaa_tex = _sg_mtl_add_resource(nil);
/* initialize a Metal texture descriptor with common attributes */
MTLTextureDescriptor* mtl_desc = [[MTLTextureDescriptor alloc] init];
if (!_sg_mtl_init_texdesc_common(mtl_desc, img)) {
_SG_OBJC_RELEASE(mtl_desc);
return SG_RESOURCESTATE_FAILED;
}
/* special case depth-stencil-buffer? */
if (_sg_is_valid_rendertarget_depth_format(img->cmn.pixel_format)) {
/* depth-stencil buffer texture must always be a render target */
SOKOL_ASSERT(img->cmn.render_target);
SOKOL_ASSERT(img->cmn.type == SG_IMAGETYPE_2D);
SOKOL_ASSERT(img->cmn.num_mipmaps == 1);
SOKOL_ASSERT(!injected);
if (msaa) {
_sg_mtl_init_texdesc_rt_msaa(mtl_desc, img);
}
else {
_sg_mtl_init_texdesc_rt(mtl_desc, img);
}
id<MTLTexture> tex = [_sg.mtl.device newTextureWithDescriptor:mtl_desc];
SOKOL_ASSERT(nil != tex);
img->mtl.depth_tex = _sg_mtl_add_resource(tex);
}
else {
/* create the color texture
In case this is a render target without MSAA, add the relevant
render-target descriptor attributes.
In case this is a render target *with* MSAA, the color texture
will serve as MSAA-resolve target (not as render target), and rendering
will go into a separate render target texture of type
MTLTextureType2DMultisample.
*/
if (img->cmn.render_target && !msaa) {
_sg_mtl_init_texdesc_rt(mtl_desc, img);
}
for (int slot = 0; slot < img->cmn.num_slots; slot++) {
id<MTLTexture> tex;
if (injected) {
SOKOL_ASSERT(desc->mtl_textures[slot]);
tex = (__bridge id<MTLTexture>) desc->mtl_textures[slot];
}
else {
tex = [_sg.mtl.device newTextureWithDescriptor:mtl_desc];
if ((img->cmn.usage == SG_USAGE_IMMUTABLE) && !img->cmn.render_target) {
_sg_mtl_copy_image_data(img, tex, &desc->data);
}
}
img->mtl.tex[slot] = _sg_mtl_add_resource(tex);
}
/* if MSAA color render target, create an additional MSAA render-surface texture */
if (img->cmn.render_target && msaa) {
_sg_mtl_init_texdesc_rt_msaa(mtl_desc, img);
id<MTLTexture> tex = [_sg.mtl.device newTextureWithDescriptor:mtl_desc];
img->mtl.msaa_tex = _sg_mtl_add_resource(tex);
}
/* create (possibly shared) sampler state */
img->mtl.sampler_state = _sg_mtl_create_sampler(_sg.mtl.device, desc);
}
_SG_OBJC_RELEASE(mtl_desc);
return SG_RESOURCESTATE_VALID;
}
_SOKOL_PRIVATE void _sg_mtl_destroy_image(_sg_image_t* img) {
SOKOL_ASSERT(img);
/* it's valid to call release resource with a 'null resource' */
for (int slot = 0; slot < img->cmn.num_slots; slot++) {
_sg_mtl_release_resource(_sg.mtl.frame_index, img->mtl.tex[slot]);
}
_sg_mtl_release_resource(_sg.mtl.frame_index, img->mtl.depth_tex);
_sg_mtl_release_resource(_sg.mtl.frame_index, img->mtl.msaa_tex);
/* NOTE: sampler state objects are shared and not released until shutdown */
}
_SOKOL_PRIVATE id<MTLLibrary> _sg_mtl_compile_library(const char* src) {
NSError* err = NULL;
id<MTLLibrary> lib = [_sg.mtl.device
newLibraryWithSource:[NSString stringWithUTF8String:src]
options:nil
error:&err
];
if (err) {
SOKOL_LOG([err.localizedDescription UTF8String]);
}
return lib;
}
_SOKOL_PRIVATE id<MTLLibrary> _sg_mtl_library_from_bytecode(const void* ptr, size_t num_bytes) {
NSError* err = NULL;
dispatch_data_t lib_data = dispatch_data_create(ptr, num_bytes, NULL, DISPATCH_DATA_DESTRUCTOR_DEFAULT);
id<MTLLibrary> lib = [_sg.mtl.device newLibraryWithData:lib_data error:&err];
if (err) {
SOKOL_LOG([err.localizedDescription UTF8String]);
}
_SG_OBJC_RELEASE(lib_data);
return lib;
}
_SOKOL_PRIVATE sg_resource_state _sg_mtl_create_shader(_sg_shader_t* shd, const sg_shader_desc* desc) {
SOKOL_ASSERT(shd && desc);
_sg_shader_common_init(&shd->cmn, desc);
/* create metal libray objects and lookup entry functions */
id<MTLLibrary> vs_lib;
id<MTLLibrary> fs_lib;
id<MTLFunction> vs_func;
id<MTLFunction> fs_func;
const char* vs_entry = desc->vs.entry;
const char* fs_entry = desc->fs.entry;
if (desc->vs.bytecode.ptr && desc->fs.bytecode.ptr) {
/* separate byte code provided */
vs_lib = _sg_mtl_library_from_bytecode(desc->vs.bytecode.ptr, desc->vs.bytecode.size);
fs_lib = _sg_mtl_library_from_bytecode(desc->fs.bytecode.ptr, desc->fs.bytecode.size);
if (nil == vs_lib || nil == fs_lib) {
return SG_RESOURCESTATE_FAILED;
}
vs_func = [vs_lib newFunctionWithName:[NSString stringWithUTF8String:vs_entry]];
fs_func = [fs_lib newFunctionWithName:[NSString stringWithUTF8String:fs_entry]];
}
else if (desc->vs.source && desc->fs.source) {
/* separate sources provided */
vs_lib = _sg_mtl_compile_library(desc->vs.source);
fs_lib = _sg_mtl_compile_library(desc->fs.source);
if (nil == vs_lib || nil == fs_lib) {
return SG_RESOURCESTATE_FAILED;
}
vs_func = [vs_lib newFunctionWithName:[NSString stringWithUTF8String:vs_entry]];
fs_func = [fs_lib newFunctionWithName:[NSString stringWithUTF8String:fs_entry]];
}
else {
return SG_RESOURCESTATE_FAILED;
}
if (nil == vs_func) {
SOKOL_LOG("vertex shader entry function not found\n");
return SG_RESOURCESTATE_FAILED;
}
if (nil == fs_func) {
SOKOL_LOG("fragment shader entry function not found\n");
return SG_RESOURCESTATE_FAILED;
}
/* it is legal to call _sg_mtl_add_resource with a nil value, this will return a special 0xFFFFFFFF index */
shd->mtl.stage[SG_SHADERSTAGE_VS].mtl_lib = _sg_mtl_add_resource(vs_lib);
shd->mtl.stage[SG_SHADERSTAGE_FS].mtl_lib = _sg_mtl_add_resource(fs_lib);
shd->mtl.stage[SG_SHADERSTAGE_VS].mtl_func = _sg_mtl_add_resource(vs_func);
shd->mtl.stage[SG_SHADERSTAGE_FS].mtl_func = _sg_mtl_add_resource(fs_func);
return SG_RESOURCESTATE_VALID;
}
_SOKOL_PRIVATE void _sg_mtl_destroy_shader(_sg_shader_t* shd) {
SOKOL_ASSERT(shd);
/* it is valid to call _sg_mtl_release_resource with a 'null resource' */
_sg_mtl_release_resource(_sg.mtl.frame_index, shd->mtl.stage[SG_SHADERSTAGE_VS].mtl_func);
_sg_mtl_release_resource(_sg.mtl.frame_index, shd->mtl.stage[SG_SHADERSTAGE_VS].mtl_lib);
_sg_mtl_release_resource(_sg.mtl.frame_index, shd->mtl.stage[SG_SHADERSTAGE_FS].mtl_func);
_sg_mtl_release_resource(_sg.mtl.frame_index, shd->mtl.stage[SG_SHADERSTAGE_FS].mtl_lib);
}
_SOKOL_PRIVATE sg_resource_state _sg_mtl_create_pipeline(_sg_pipeline_t* pip, _sg_shader_t* shd, const sg_pipeline_desc* desc) {
SOKOL_ASSERT(pip && shd && desc);
SOKOL_ASSERT(desc->shader.id == shd->slot.id);
pip->shader = shd;
_sg_pipeline_common_init(&pip->cmn, desc);
sg_primitive_type prim_type = desc->primitive_type;
pip->mtl.prim_type = _sg_mtl_primitive_type(prim_type);
pip->mtl.index_size = _sg_mtl_index_size(pip->cmn.index_type);
if (SG_INDEXTYPE_NONE != pip->cmn.index_type) {
pip->mtl.index_type = _sg_mtl_index_type(pip->cmn.index_type);
}
pip->mtl.cull_mode = _sg_mtl_cull_mode(desc->cull_mode);
pip->mtl.winding = _sg_mtl_winding(desc->face_winding);
pip->mtl.stencil_ref = desc->stencil.ref;
/* create vertex-descriptor */
MTLVertexDescriptor* vtx_desc = [MTLVertexDescriptor vertexDescriptor];
for (NSUInteger attr_index = 0; attr_index < SG_MAX_VERTEX_ATTRIBUTES; attr_index++) {
const sg_vertex_attr_desc* a_desc = &desc->layout.attrs[attr_index];
if (a_desc->format == SG_VERTEXFORMAT_INVALID) {
break;
}
SOKOL_ASSERT(a_desc->buffer_index < SG_MAX_SHADERSTAGE_BUFFERS);
vtx_desc.attributes[attr_index].format = _sg_mtl_vertex_format(a_desc->format);
vtx_desc.attributes[attr_index].offset = (NSUInteger)a_desc->offset;
vtx_desc.attributes[attr_index].bufferIndex = (NSUInteger)(a_desc->buffer_index + SG_MAX_SHADERSTAGE_UBS);
pip->cmn.vertex_layout_valid[a_desc->buffer_index] = true;
}
for (NSUInteger layout_index = 0; layout_index < SG_MAX_SHADERSTAGE_BUFFERS; layout_index++) {
if (pip->cmn.vertex_layout_valid[layout_index]) {
const sg_buffer_layout_desc* l_desc = &desc->layout.buffers[layout_index];
const NSUInteger mtl_vb_slot = layout_index + SG_MAX_SHADERSTAGE_UBS;
SOKOL_ASSERT(l_desc->stride > 0);
vtx_desc.layouts[mtl_vb_slot].stride = (NSUInteger)l_desc->stride;
vtx_desc.layouts[mtl_vb_slot].stepFunction = _sg_mtl_step_function(l_desc->step_func);
vtx_desc.layouts[mtl_vb_slot].stepRate = (NSUInteger)l_desc->step_rate;
}
}
/* render-pipeline descriptor */
MTLRenderPipelineDescriptor* rp_desc = [[MTLRenderPipelineDescriptor alloc] init];
rp_desc.vertexDescriptor = vtx_desc;
SOKOL_ASSERT(shd->mtl.stage[SG_SHADERSTAGE_VS].mtl_func != _SG_MTL_INVALID_SLOT_INDEX);
rp_desc.vertexFunction = _sg_mtl_id(shd->mtl.stage[SG_SHADERSTAGE_VS].mtl_func);
SOKOL_ASSERT(shd->mtl.stage[SG_SHADERSTAGE_FS].mtl_func != _SG_MTL_INVALID_SLOT_INDEX);
rp_desc.fragmentFunction = _sg_mtl_id(shd->mtl.stage[SG_SHADERSTAGE_FS].mtl_func);
rp_desc.sampleCount = (NSUInteger)desc->sample_count;
rp_desc.alphaToCoverageEnabled = desc->alpha_to_coverage_enabled;
rp_desc.alphaToOneEnabled = NO;
rp_desc.rasterizationEnabled = YES;
rp_desc.depthAttachmentPixelFormat = _sg_mtl_pixel_format(desc->depth.pixel_format);
if (desc->depth.pixel_format == SG_PIXELFORMAT_DEPTH_STENCIL) {
rp_desc.stencilAttachmentPixelFormat = _sg_mtl_pixel_format(desc->depth.pixel_format);
}
/* FIXME: this only works on macOS 10.13!
for (int i = 0; i < (SG_MAX_SHADERSTAGE_UBS+SG_MAX_SHADERSTAGE_BUFFERS); i++) {
rp_desc.vertexBuffers[i].mutability = MTLMutabilityImmutable;
}
for (int i = 0; i < SG_MAX_SHADERSTAGE_UBS; i++) {
rp_desc.fragmentBuffers[i].mutability = MTLMutabilityImmutable;
}
*/
for (NSUInteger i = 0; i < (NSUInteger)desc->color_count; i++) {
SOKOL_ASSERT(i < SG_MAX_COLOR_ATTACHMENTS);
const sg_color_state* cs = &desc->colors[i];
rp_desc.colorAttachments[i].pixelFormat = _sg_mtl_pixel_format(cs->pixel_format);
rp_desc.colorAttachments[i].writeMask = _sg_mtl_color_write_mask(cs->write_mask);
rp_desc.colorAttachments[i].blendingEnabled = cs->blend.enabled;
rp_desc.colorAttachments[i].alphaBlendOperation = _sg_mtl_blend_op(cs->blend.op_alpha);
rp_desc.colorAttachments[i].rgbBlendOperation = _sg_mtl_blend_op(cs->blend.op_rgb);
rp_desc.colorAttachments[i].destinationAlphaBlendFactor = _sg_mtl_blend_factor(cs->blend.dst_factor_alpha);
rp_desc.colorAttachments[i].destinationRGBBlendFactor = _sg_mtl_blend_factor(cs->blend.dst_factor_rgb);
rp_desc.colorAttachments[i].sourceAlphaBlendFactor = _sg_mtl_blend_factor(cs->blend.src_factor_alpha);
rp_desc.colorAttachments[i].sourceRGBBlendFactor = _sg_mtl_blend_factor(cs->blend.src_factor_rgb);
}
NSError* err = NULL;
id<MTLRenderPipelineState> mtl_rps = [_sg.mtl.device newRenderPipelineStateWithDescriptor:rp_desc error:&err];
_SG_OBJC_RELEASE(rp_desc);
if (nil == mtl_rps) {
SOKOL_ASSERT(err);
SOKOL_LOG([err.localizedDescription UTF8String]);
return SG_RESOURCESTATE_FAILED;
}
/* depth-stencil-state */
MTLDepthStencilDescriptor* ds_desc = [[MTLDepthStencilDescriptor alloc] init];
ds_desc.depthCompareFunction = _sg_mtl_compare_func(desc->depth.compare);
ds_desc.depthWriteEnabled = desc->depth.write_enabled;
if (desc->stencil.enabled) {
const sg_stencil_face_state* sb = &desc->stencil.back;
ds_desc.backFaceStencil = [[MTLStencilDescriptor alloc] init];
ds_desc.backFaceStencil.stencilFailureOperation = _sg_mtl_stencil_op(sb->fail_op);
ds_desc.backFaceStencil.depthFailureOperation = _sg_mtl_stencil_op(sb->depth_fail_op);
ds_desc.backFaceStencil.depthStencilPassOperation = _sg_mtl_stencil_op(sb->pass_op);
ds_desc.backFaceStencil.stencilCompareFunction = _sg_mtl_compare_func(sb->compare);
ds_desc.backFaceStencil.readMask = desc->stencil.read_mask;
ds_desc.backFaceStencil.writeMask = desc->stencil.write_mask;
const sg_stencil_face_state* sf = &desc->stencil.front;
ds_desc.frontFaceStencil = [[MTLStencilDescriptor alloc] init];
ds_desc.frontFaceStencil.stencilFailureOperation = _sg_mtl_stencil_op(sf->fail_op);
ds_desc.frontFaceStencil.depthFailureOperation = _sg_mtl_stencil_op(sf->depth_fail_op);
ds_desc.frontFaceStencil.depthStencilPassOperation = _sg_mtl_stencil_op(sf->pass_op);
ds_desc.frontFaceStencil.stencilCompareFunction = _sg_mtl_compare_func(sf->compare);
ds_desc.frontFaceStencil.readMask = desc->stencil.read_mask;
ds_desc.frontFaceStencil.writeMask = desc->stencil.write_mask;
}
id<MTLDepthStencilState> mtl_dss = [_sg.mtl.device newDepthStencilStateWithDescriptor:ds_desc];
_SG_OBJC_RELEASE(ds_desc);
pip->mtl.rps = _sg_mtl_add_resource(mtl_rps);
pip->mtl.dss = _sg_mtl_add_resource(mtl_dss);
return SG_RESOURCESTATE_VALID;
}
_SOKOL_PRIVATE void _sg_mtl_destroy_pipeline(_sg_pipeline_t* pip) {
SOKOL_ASSERT(pip);
/* it's valid to call release resource with a 'null resource' */
_sg_mtl_release_resource(_sg.mtl.frame_index, pip->mtl.rps);
_sg_mtl_release_resource(_sg.mtl.frame_index, pip->mtl.dss);
}
_SOKOL_PRIVATE sg_resource_state _sg_mtl_create_pass(_sg_pass_t* pass, _sg_image_t** att_images, const sg_pass_desc* desc) {
SOKOL_ASSERT(pass && desc);
SOKOL_ASSERT(att_images && att_images[0]);
_sg_pass_common_init(&pass->cmn, desc);
/* copy image pointers */
const sg_pass_attachment_desc* att_desc;
for (int i = 0; i < pass->cmn.num_color_atts; i++) {
att_desc = &desc->color_attachments[i];
if (att_desc->image.id != SG_INVALID_ID) {
SOKOL_ASSERT(att_desc->image.id != SG_INVALID_ID);
SOKOL_ASSERT(0 == pass->mtl.color_atts[i].image);
SOKOL_ASSERT(att_images[i] && (att_images[i]->slot.id == att_desc->image.id));
SOKOL_ASSERT(_sg_is_valid_rendertarget_color_format(att_images[i]->cmn.pixel_format));
pass->mtl.color_atts[i].image = att_images[i];
}
}
SOKOL_ASSERT(0 == pass->mtl.ds_att.image);
att_desc = &desc->depth_stencil_attachment;
if (att_desc->image.id != SG_INVALID_ID) {
const int ds_img_index = SG_MAX_COLOR_ATTACHMENTS;
SOKOL_ASSERT(att_images[ds_img_index] && (att_images[ds_img_index]->slot.id == att_desc->image.id));
SOKOL_ASSERT(_sg_is_valid_rendertarget_depth_format(att_images[ds_img_index]->cmn.pixel_format));
pass->mtl.ds_att.image = att_images[ds_img_index];
}
return SG_RESOURCESTATE_VALID;
}
_SOKOL_PRIVATE void _sg_mtl_destroy_pass(_sg_pass_t* pass) {
SOKOL_ASSERT(pass);
_SOKOL_UNUSED(pass);
}
_SOKOL_PRIVATE _sg_image_t* _sg_mtl_pass_color_image(const _sg_pass_t* pass, int index) {
SOKOL_ASSERT(pass && (index >= 0) && (index < SG_MAX_COLOR_ATTACHMENTS));
/* NOTE: may return null */
return pass->mtl.color_atts[index].image;
}
_SOKOL_PRIVATE _sg_image_t* _sg_mtl_pass_ds_image(const _sg_pass_t* pass) {
/* NOTE: may return null */
SOKOL_ASSERT(pass);
return pass->mtl.ds_att.image;
}
_SOKOL_PRIVATE void _sg_mtl_begin_pass(_sg_pass_t* pass, const sg_pass_action* action, int w, int h) {
SOKOL_ASSERT(action);
SOKOL_ASSERT(!_sg.mtl.in_pass);
SOKOL_ASSERT(_sg.mtl.cmd_queue);
SOKOL_ASSERT(nil == _sg.mtl.cmd_encoder);
SOKOL_ASSERT(_sg.mtl.renderpass_descriptor_cb || _sg.mtl.renderpass_descriptor_userdata_cb);
_sg.mtl.in_pass = true;
_sg.mtl.cur_width = w;
_sg.mtl.cur_height = h;
_sg_mtl_clear_state_cache();
/* if this is the first pass in the frame, create a command buffer */
if (nil == _sg.mtl.cmd_buffer) {
/* block until the oldest frame in flight has finished */
dispatch_semaphore_wait(_sg.mtl.sem, DISPATCH_TIME_FOREVER);
_sg.mtl.cmd_buffer = [_sg.mtl.cmd_queue commandBufferWithUnretainedReferences];
}
/* if this is first pass in frame, get uniform buffer base pointer */
if (0 == _sg.mtl.cur_ub_base_ptr) {
_sg.mtl.cur_ub_base_ptr = (uint8_t*)[_sg.mtl.uniform_buffers[_sg.mtl.cur_frame_rotate_index] contents];
}
/* initialize a render pass descriptor */
MTLRenderPassDescriptor* pass_desc = nil;
if (pass) {
/* offscreen render pass */
pass_desc = [MTLRenderPassDescriptor renderPassDescriptor];
}
else {
/* default render pass, call user-provided callback to provide render pass descriptor */
if (_sg.mtl.renderpass_descriptor_cb) {
pass_desc = (__bridge MTLRenderPassDescriptor*) _sg.mtl.renderpass_descriptor_cb();
}
else {
pass_desc = (__bridge MTLRenderPassDescriptor*) _sg.mtl.renderpass_descriptor_userdata_cb(_sg.mtl.user_data);
}
}
if (pass_desc) {
_sg.mtl.pass_valid = true;
}
else {
/* default pass descriptor will not be valid if window is minimized,
don't do any rendering in this case */
_sg.mtl.pass_valid = false;
return;
}
if (pass) {
/* setup pass descriptor for offscreen rendering */
SOKOL_ASSERT(pass->slot.state == SG_RESOURCESTATE_VALID);
for (NSUInteger i = 0; i < (NSUInteger)pass->cmn.num_color_atts; i++) {
const _sg_pass_attachment_t* cmn_att = &pass->cmn.color_atts[i];
const _sg_mtl_attachment_t* mtl_att = &pass->mtl.color_atts[i];
const _sg_image_t* att_img = mtl_att->image;
SOKOL_ASSERT(att_img->slot.state == SG_RESOURCESTATE_VALID);
SOKOL_ASSERT(att_img->slot.id == cmn_att->image_id.id);
const bool is_msaa = (att_img->cmn.sample_count > 1);
pass_desc.colorAttachments[i].loadAction = _sg_mtl_load_action(action->colors[i].action);
pass_desc.colorAttachments[i].storeAction = is_msaa ? MTLStoreActionMultisampleResolve : MTLStoreActionStore;
sg_color c = action->colors[i].value;
pass_desc.colorAttachments[i].clearColor = MTLClearColorMake(c.r, c.g, c.b, c.a);
if (is_msaa) {
SOKOL_ASSERT(att_img->mtl.msaa_tex != _SG_MTL_INVALID_SLOT_INDEX);
SOKOL_ASSERT(att_img->mtl.tex[mtl_att->image->cmn.active_slot] != _SG_MTL_INVALID_SLOT_INDEX);
pass_desc.colorAttachments[i].texture = _sg_mtl_id(att_img->mtl.msaa_tex);
pass_desc.colorAttachments[i].resolveTexture = _sg_mtl_id(att_img->mtl.tex[att_img->cmn.active_slot]);
pass_desc.colorAttachments[i].resolveLevel = (NSUInteger)cmn_att->mip_level;
switch (att_img->cmn.type) {
case SG_IMAGETYPE_CUBE:
case SG_IMAGETYPE_ARRAY:
pass_desc.colorAttachments[i].resolveSlice = (NSUInteger)cmn_att->slice;
break;
case SG_IMAGETYPE_3D:
pass_desc.colorAttachments[i].resolveDepthPlane = (NSUInteger)cmn_att->slice;
break;
default: break;
}
}
else {
SOKOL_ASSERT(att_img->mtl.tex[att_img->cmn.active_slot] != _SG_MTL_INVALID_SLOT_INDEX);
pass_desc.colorAttachments[i].texture = _sg_mtl_id(att_img->mtl.tex[att_img->cmn.active_slot]);
pass_desc.colorAttachments[i].level = (NSUInteger)cmn_att->mip_level;
switch (att_img->cmn.type) {
case SG_IMAGETYPE_CUBE:
case SG_IMAGETYPE_ARRAY:
pass_desc.colorAttachments[i].slice = (NSUInteger)cmn_att->slice;
break;
case SG_IMAGETYPE_3D:
pass_desc.colorAttachments[i].depthPlane = (NSUInteger)cmn_att->slice;
break;
default: break;
}
}
}
const _sg_image_t* ds_att_img = pass->mtl.ds_att.image;
if (0 != ds_att_img) {
SOKOL_ASSERT(ds_att_img->slot.state == SG_RESOURCESTATE_VALID);
SOKOL_ASSERT(ds_att_img->slot.id == pass->cmn.ds_att.image_id.id);
SOKOL_ASSERT(ds_att_img->mtl.depth_tex != _SG_MTL_INVALID_SLOT_INDEX);
pass_desc.depthAttachment.texture = _sg_mtl_id(ds_att_img->mtl.depth_tex);
pass_desc.depthAttachment.loadAction = _sg_mtl_load_action(action->depth.action);
pass_desc.depthAttachment.clearDepth = action->depth.value;
if (_sg_is_depth_stencil_format(ds_att_img->cmn.pixel_format)) {
pass_desc.stencilAttachment.texture = _sg_mtl_id(ds_att_img->mtl.depth_tex);
pass_desc.stencilAttachment.loadAction = _sg_mtl_load_action(action->stencil.action);
pass_desc.stencilAttachment.clearStencil = action->stencil.value;
}
}
}
else {
/* setup pass descriptor for default rendering */
pass_desc.colorAttachments[0].loadAction = _sg_mtl_load_action(action->colors[0].action);
sg_color c = action->colors[0].value;
pass_desc.colorAttachments[0].clearColor = MTLClearColorMake(c.r, c.g, c.b, c.a);
pass_desc.depthAttachment.loadAction = _sg_mtl_load_action(action->depth.action);
pass_desc.depthAttachment.clearDepth = action->depth.value;
pass_desc.stencilAttachment.loadAction = _sg_mtl_load_action(action->stencil.action);
pass_desc.stencilAttachment.clearStencil = action->stencil.value;
}
/* create a render command encoder, this might return nil if window is minimized */
_sg.mtl.cmd_encoder = [_sg.mtl.cmd_buffer renderCommandEncoderWithDescriptor:pass_desc];
if (nil == _sg.mtl.cmd_encoder) {
_sg.mtl.pass_valid = false;
return;
}
/* bind the global uniform buffer, this only happens once per pass */
_sg_mtl_bind_uniform_buffers();
}
_SOKOL_PRIVATE void _sg_mtl_end_pass(void) {
SOKOL_ASSERT(_sg.mtl.in_pass);
_sg.mtl.in_pass = false;
_sg.mtl.pass_valid = false;
if (nil != _sg.mtl.cmd_encoder) {
[_sg.mtl.cmd_encoder endEncoding];
/* NOTE: MTLRenderCommandEncoder is autoreleased */
_sg.mtl.cmd_encoder = nil;
}
}
_SOKOL_PRIVATE void _sg_mtl_commit(void) {
SOKOL_ASSERT(!_sg.mtl.in_pass);
SOKOL_ASSERT(!_sg.mtl.pass_valid);
SOKOL_ASSERT(_sg.mtl.drawable_cb || _sg.mtl.drawable_userdata_cb);
SOKOL_ASSERT(nil == _sg.mtl.cmd_encoder);
SOKOL_ASSERT(nil != _sg.mtl.cmd_buffer);
#if defined(_SG_TARGET_MACOS)
[_sg.mtl.uniform_buffers[_sg.mtl.cur_frame_rotate_index] didModifyRange:NSMakeRange(0, (NSUInteger)_sg.mtl.cur_ub_offset)];
#endif
/* present, commit and signal semaphore when done */
id<MTLDrawable> cur_drawable = nil;
if (_sg.mtl.drawable_cb) {
cur_drawable = (__bridge id<MTLDrawable>) _sg.mtl.drawable_cb();
}
else {
cur_drawable = (__bridge id<MTLDrawable>) _sg.mtl.drawable_userdata_cb(_sg.mtl.user_data);
}
[_sg.mtl.cmd_buffer presentDrawable:cur_drawable];
[_sg.mtl.cmd_buffer addCompletedHandler:^(id<MTLCommandBuffer> cmd_buffer) {
_SOKOL_UNUSED(cmd_buffer);
dispatch_semaphore_signal(_sg.mtl.sem);
}];
[_sg.mtl.cmd_buffer commit];
/* garbage-collect resources pending for release */
_sg_mtl_garbage_collect(_sg.mtl.frame_index);
/* rotate uniform buffer slot */
if (++_sg.mtl.cur_frame_rotate_index >= SG_NUM_INFLIGHT_FRAMES) {
_sg.mtl.cur_frame_rotate_index = 0;
}
_sg.mtl.frame_index++;
_sg.mtl.cur_ub_offset = 0;
_sg.mtl.cur_ub_base_ptr = 0;
/* NOTE: MTLCommandBuffer is autoreleased */
_sg.mtl.cmd_buffer = nil;
}
_SOKOL_PRIVATE void _sg_mtl_apply_viewport(int x, int y, int w, int h, bool origin_top_left) {
SOKOL_ASSERT(_sg.mtl.in_pass);
if (!_sg.mtl.pass_valid) {
return;
}
SOKOL_ASSERT(nil != _sg.mtl.cmd_encoder);
MTLViewport vp;
vp.originX = (double) x;
vp.originY = (double) (origin_top_left ? y : (_sg.mtl.cur_height - (y + h)));
vp.width = (double) w;
vp.height = (double) h;
vp.znear = 0.0;
vp.zfar = 1.0;
[_sg.mtl.cmd_encoder setViewport:vp];
}
_SOKOL_PRIVATE void _sg_mtl_apply_scissor_rect(int x, int y, int w, int h, bool origin_top_left) {
SOKOL_ASSERT(_sg.mtl.in_pass);
if (!_sg.mtl.pass_valid) {
return;
}
SOKOL_ASSERT(nil != _sg.mtl.cmd_encoder);
/* clip against framebuffer rect */
x = _sg_min(_sg_max(0, x), _sg.mtl.cur_width-1);
y = _sg_min(_sg_max(0, y), _sg.mtl.cur_height-1);
if ((x + w) > _sg.mtl.cur_width) {
w = _sg.mtl.cur_width - x;
}
if ((y + h) > _sg.mtl.cur_height) {
h = _sg.mtl.cur_height - y;
}
w = _sg_max(w, 1);
h = _sg_max(h, 1);
MTLScissorRect r;
r.x = (NSUInteger)x;
r.y = (NSUInteger) (origin_top_left ? y : (_sg.mtl.cur_height - (y + h)));
r.width = (NSUInteger)w;
r.height = (NSUInteger)h;
[_sg.mtl.cmd_encoder setScissorRect:r];
}
_SOKOL_PRIVATE void _sg_mtl_apply_pipeline(_sg_pipeline_t* pip) {
SOKOL_ASSERT(pip);
SOKOL_ASSERT(pip->shader && (pip->cmn.shader_id.id == pip->shader->slot.id));
SOKOL_ASSERT(_sg.mtl.in_pass);
if (!_sg.mtl.pass_valid) {
return;
}
SOKOL_ASSERT(nil != _sg.mtl.cmd_encoder);
if ((_sg.mtl.state_cache.cur_pipeline != pip) || (_sg.mtl.state_cache.cur_pipeline_id.id != pip->slot.id)) {
_sg.mtl.state_cache.cur_pipeline = pip;
_sg.mtl.state_cache.cur_pipeline_id.id = pip->slot.id;
sg_color c = pip->cmn.blend_color;
[_sg.mtl.cmd_encoder setBlendColorRed:c.r green:c.g blue:c.b alpha:c.a];
[_sg.mtl.cmd_encoder setCullMode:pip->mtl.cull_mode];
[_sg.mtl.cmd_encoder setFrontFacingWinding:pip->mtl.winding];
[_sg.mtl.cmd_encoder setStencilReferenceValue:pip->mtl.stencil_ref];
[_sg.mtl.cmd_encoder setDepthBias:pip->cmn.depth_bias slopeScale:pip->cmn.depth_bias_slope_scale clamp:pip->cmn.depth_bias_clamp];
SOKOL_ASSERT(pip->mtl.rps != _SG_MTL_INVALID_SLOT_INDEX);
[_sg.mtl.cmd_encoder setRenderPipelineState:_sg_mtl_id(pip->mtl.rps)];
SOKOL_ASSERT(pip->mtl.dss != _SG_MTL_INVALID_SLOT_INDEX);
[_sg.mtl.cmd_encoder setDepthStencilState:_sg_mtl_id(pip->mtl.dss)];
}
}
_SOKOL_PRIVATE void _sg_mtl_apply_bindings(
_sg_pipeline_t* pip,
_sg_buffer_t** vbs, const int* vb_offsets, int num_vbs,
_sg_buffer_t* ib, int ib_offset,
_sg_image_t** vs_imgs, int num_vs_imgs,
_sg_image_t** fs_imgs, int num_fs_imgs)
{
_SOKOL_UNUSED(pip);
SOKOL_ASSERT(_sg.mtl.in_pass);
if (!_sg.mtl.pass_valid) {
return;
}
SOKOL_ASSERT(nil != _sg.mtl.cmd_encoder);
/* store index buffer binding, this will be needed later in sg_draw() */
_sg.mtl.state_cache.cur_indexbuffer = ib;
_sg.mtl.state_cache.cur_indexbuffer_offset = ib_offset;
if (ib) {
SOKOL_ASSERT(pip->cmn.index_type != SG_INDEXTYPE_NONE);
_sg.mtl.state_cache.cur_indexbuffer_id.id = ib->slot.id;
}
else {
SOKOL_ASSERT(pip->cmn.index_type == SG_INDEXTYPE_NONE);
_sg.mtl.state_cache.cur_indexbuffer_id.id = SG_INVALID_ID;
}
/* apply vertex buffers */
NSUInteger slot;
for (slot = 0; slot < (NSUInteger)num_vbs; slot++) {
const _sg_buffer_t* vb = vbs[slot];
if ((_sg.mtl.state_cache.cur_vertexbuffers[slot] != vb) ||
(_sg.mtl.state_cache.cur_vertexbuffer_offsets[slot] != vb_offsets[slot]) ||
(_sg.mtl.state_cache.cur_vertexbuffer_ids[slot].id != vb->slot.id))
{
_sg.mtl.state_cache.cur_vertexbuffers[slot] = vb;
_sg.mtl.state_cache.cur_vertexbuffer_offsets[slot] = vb_offsets[slot];
_sg.mtl.state_cache.cur_vertexbuffer_ids[slot].id = vb->slot.id;
const NSUInteger mtl_slot = SG_MAX_SHADERSTAGE_UBS + slot;
SOKOL_ASSERT(vb->mtl.buf[vb->cmn.active_slot] != _SG_MTL_INVALID_SLOT_INDEX);
[_sg.mtl.cmd_encoder setVertexBuffer:_sg_mtl_id(vb->mtl.buf[vb->cmn.active_slot])
offset:(NSUInteger)vb_offsets[slot]
atIndex:mtl_slot];
}
}
/* apply vertex shader images */
for (slot = 0; slot < (NSUInteger)num_vs_imgs; slot++) {
const _sg_image_t* img = vs_imgs[slot];
if ((_sg.mtl.state_cache.cur_vs_images[slot] != img) || (_sg.mtl.state_cache.cur_vs_image_ids[slot].id != img->slot.id)) {
_sg.mtl.state_cache.cur_vs_images[slot] = img;
_sg.mtl.state_cache.cur_vs_image_ids[slot].id = img->slot.id;
SOKOL_ASSERT(img->mtl.tex[img->cmn.active_slot] != _SG_MTL_INVALID_SLOT_INDEX);
[_sg.mtl.cmd_encoder setVertexTexture:_sg_mtl_id(img->mtl.tex[img->cmn.active_slot]) atIndex:slot];
SOKOL_ASSERT(img->mtl.sampler_state != _SG_MTL_INVALID_SLOT_INDEX);
[_sg.mtl.cmd_encoder setVertexSamplerState:_sg_mtl_id(img->mtl.sampler_state) atIndex:slot];
}
}
/* apply fragment shader images */
for (slot = 0; slot < (NSUInteger)num_fs_imgs; slot++) {
const _sg_image_t* img = fs_imgs[slot];
if ((_sg.mtl.state_cache.cur_fs_images[slot] != img) || (_sg.mtl.state_cache.cur_fs_image_ids[slot].id != img->slot.id)) {
_sg.mtl.state_cache.cur_fs_images[slot] = img;
_sg.mtl.state_cache.cur_fs_image_ids[slot].id = img->slot.id;
SOKOL_ASSERT(img->mtl.tex[img->cmn.active_slot] != _SG_MTL_INVALID_SLOT_INDEX);
[_sg.mtl.cmd_encoder setFragmentTexture:_sg_mtl_id(img->mtl.tex[img->cmn.active_slot]) atIndex:slot];
SOKOL_ASSERT(img->mtl.sampler_state != _SG_MTL_INVALID_SLOT_INDEX);
[_sg.mtl.cmd_encoder setFragmentSamplerState:_sg_mtl_id(img->mtl.sampler_state) atIndex:slot];
}
}
}
_SOKOL_PRIVATE void _sg_mtl_apply_uniforms(sg_shader_stage stage_index, int ub_index, const sg_range* data) {
SOKOL_ASSERT(_sg.mtl.in_pass);
if (!_sg.mtl.pass_valid) {
return;
}
SOKOL_ASSERT(nil != _sg.mtl.cmd_encoder);
SOKOL_ASSERT(((size_t)_sg.mtl.cur_ub_offset + data->size) <= (size_t)_sg.mtl.ub_size);
SOKOL_ASSERT((_sg.mtl.cur_ub_offset & (_SG_MTL_UB_ALIGN-1)) == 0);
SOKOL_ASSERT(_sg.mtl.state_cache.cur_pipeline && _sg.mtl.state_cache.cur_pipeline->shader);
SOKOL_ASSERT(_sg.mtl.state_cache.cur_pipeline->slot.id == _sg.mtl.state_cache.cur_pipeline_id.id);
SOKOL_ASSERT(_sg.mtl.state_cache.cur_pipeline->shader->slot.id == _sg.mtl.state_cache.cur_pipeline->cmn.shader_id.id);
SOKOL_ASSERT(ub_index < _sg.mtl.state_cache.cur_pipeline->shader->cmn.stage[stage_index].num_uniform_blocks);
SOKOL_ASSERT(data->size <= _sg.mtl.state_cache.cur_pipeline->shader->cmn.stage[stage_index].uniform_blocks[ub_index].size);
/* copy to global uniform buffer, record offset into cmd encoder, and advance offset */
uint8_t* dst = &_sg.mtl.cur_ub_base_ptr[_sg.mtl.cur_ub_offset];
memcpy(dst, data->ptr, data->size);
if (stage_index == SG_SHADERSTAGE_VS) {
[_sg.mtl.cmd_encoder setVertexBufferOffset:(NSUInteger)_sg.mtl.cur_ub_offset atIndex:(NSUInteger)ub_index];
}
else {
[_sg.mtl.cmd_encoder setFragmentBufferOffset:(NSUInteger)_sg.mtl.cur_ub_offset atIndex:(NSUInteger)ub_index];
}
_sg.mtl.cur_ub_offset = _sg_roundup(_sg.mtl.cur_ub_offset + (int)data->size, _SG_MTL_UB_ALIGN);
}
_SOKOL_PRIVATE void _sg_mtl_draw(int base_element, int num_elements, int num_instances) {
SOKOL_ASSERT(_sg.mtl.in_pass);
if (!_sg.mtl.pass_valid) {
return;
}
SOKOL_ASSERT(nil != _sg.mtl.cmd_encoder);
SOKOL_ASSERT(_sg.mtl.state_cache.cur_pipeline && (_sg.mtl.state_cache.cur_pipeline->slot.id == _sg.mtl.state_cache.cur_pipeline_id.id));
if (SG_INDEXTYPE_NONE != _sg.mtl.state_cache.cur_pipeline->cmn.index_type) {
/* indexed rendering */
SOKOL_ASSERT(_sg.mtl.state_cache.cur_indexbuffer && (_sg.mtl.state_cache.cur_indexbuffer->slot.id == _sg.mtl.state_cache.cur_indexbuffer_id.id));
const _sg_buffer_t* ib = _sg.mtl.state_cache.cur_indexbuffer;
SOKOL_ASSERT(ib->mtl.buf[ib->cmn.active_slot] != _SG_MTL_INVALID_SLOT_INDEX);
const NSUInteger index_buffer_offset = (NSUInteger) (_sg.mtl.state_cache.cur_indexbuffer_offset + base_element * _sg.mtl.state_cache.cur_pipeline->mtl.index_size);
[_sg.mtl.cmd_encoder drawIndexedPrimitives:_sg.mtl.state_cache.cur_pipeline->mtl.prim_type
indexCount:(NSUInteger)num_elements
indexType:_sg.mtl.state_cache.cur_pipeline->mtl.index_type
indexBuffer:_sg_mtl_id(ib->mtl.buf[ib->cmn.active_slot])
indexBufferOffset:index_buffer_offset
instanceCount:(NSUInteger)num_instances];
}
else {
/* non-indexed rendering */
[_sg.mtl.cmd_encoder drawPrimitives:_sg.mtl.state_cache.cur_pipeline->mtl.prim_type
vertexStart:(NSUInteger)base_element
vertexCount:(NSUInteger)num_elements
instanceCount:(NSUInteger)num_instances];
}
}
_SOKOL_PRIVATE void _sg_mtl_update_buffer(_sg_buffer_t* buf, const sg_range* data) {
SOKOL_ASSERT(buf && data && data->ptr && (data->size > 0));
if (++buf->cmn.active_slot >= buf->cmn.num_slots) {
buf->cmn.active_slot = 0;
}
__unsafe_unretained id<MTLBuffer> mtl_buf = _sg_mtl_id(buf->mtl.buf[buf->cmn.active_slot]);
void* dst_ptr = [mtl_buf contents];
memcpy(dst_ptr, data->ptr, data->size);
#if defined(_SG_TARGET_MACOS)
[mtl_buf didModifyRange:NSMakeRange(0, data->size)];
#endif
}
_SOKOL_PRIVATE int _sg_mtl_append_buffer(_sg_buffer_t* buf, const sg_range* data, bool new_frame) {
SOKOL_ASSERT(buf && data && data->ptr && (data->size > 0));
if (new_frame) {
if (++buf->cmn.active_slot >= buf->cmn.num_slots) {
buf->cmn.active_slot = 0;
}
}
__unsafe_unretained id<MTLBuffer> mtl_buf = _sg_mtl_id(buf->mtl.buf[buf->cmn.active_slot]);
uint8_t* dst_ptr = (uint8_t*) [mtl_buf contents];
dst_ptr += buf->cmn.append_pos;
memcpy(dst_ptr, data->ptr, data->size);
#if defined(_SG_TARGET_MACOS)
[mtl_buf didModifyRange:NSMakeRange((NSUInteger)buf->cmn.append_pos, (NSUInteger)data->size)];
#endif
/* NOTE: this is a requirement from WebGPU, but we want identical behaviour across all backends */
return _sg_roundup((int)data->size, 4);
}
_SOKOL_PRIVATE void _sg_mtl_update_image(_sg_image_t* img, const sg_image_data* data) {
SOKOL_ASSERT(img && data);
if (++img->cmn.active_slot >= img->cmn.num_slots) {
img->cmn.active_slot = 0;
}
__unsafe_unretained id<MTLTexture> mtl_tex = _sg_mtl_id(img->mtl.tex[img->cmn.active_slot]);
_sg_mtl_copy_image_data(img, mtl_tex, data);
}
/*== WEBGPU BACKEND IMPLEMENTATION ===========================================*/
#elif defined(SOKOL_WGPU)
_SOKOL_PRIVATE WGPUBufferUsageFlags _sg_wgpu_buffer_usage(sg_buffer_type t, sg_usage u) {
WGPUBufferUsageFlags res = 0;
if (SG_BUFFERTYPE_VERTEXBUFFER == t) {
res |= WGPUBufferUsage_Vertex;
}
else {
res |= WGPUBufferUsage_Index;
}
if (SG_USAGE_IMMUTABLE != u) {
res |= WGPUBufferUsage_CopyDst;
}
return res;
}
_SOKOL_PRIVATE WGPULoadOp _sg_wgpu_load_op(sg_action a) {
switch (a) {
case SG_ACTION_CLEAR:
case SG_ACTION_DONTCARE:
return WGPULoadOp_Clear;
case SG_ACTION_LOAD:
return WGPULoadOp_Load;
default:
SOKOL_UNREACHABLE;
return (WGPULoadOp)0;
}
}
_SOKOL_PRIVATE WGPUTextureViewDimension _sg_wgpu_tex_viewdim(sg_image_type t) {
switch (t) {
case SG_IMAGETYPE_2D: return WGPUTextureViewDimension_2D;
case SG_IMAGETYPE_CUBE: return WGPUTextureViewDimension_Cube;
case SG_IMAGETYPE_3D: return WGPUTextureViewDimension_3D;
case SG_IMAGETYPE_ARRAY: return WGPUTextureViewDimension_2DArray;
default: SOKOL_UNREACHABLE; return WGPUTextureViewDimension_Force32;
}
}
_SOKOL_PRIVATE WGPUTextureComponentType _sg_wgpu_tex_comptype(sg_sampler_type t) {
switch (t) {
case SG_SAMPLERTYPE_FLOAT: return WGPUTextureComponentType_Float;
case SG_SAMPLERTYPE_SINT: return WGPUTextureComponentType_Sint;
case SG_SAMPLERTYPE_UINT: return WGPUTextureComponentType_Uint;
default: SOKOL_UNREACHABLE; return WGPUTextureComponentType_Force32;
}
}
_SOKOL_PRIVATE WGPUTextureDimension _sg_wgpu_tex_dim(sg_image_type t) {
if (SG_IMAGETYPE_3D == t) {
return WGPUTextureDimension_3D;
}
else {
return WGPUTextureDimension_2D;
}
}
_SOKOL_PRIVATE WGPUAddressMode _sg_wgpu_sampler_addrmode(sg_wrap m) {
switch (m) {
case SG_WRAP_REPEAT:
return WGPUAddressMode_Repeat;
case SG_WRAP_CLAMP_TO_EDGE:
case SG_WRAP_CLAMP_TO_BORDER:
return WGPUAddressMode_ClampToEdge;
case SG_WRAP_MIRRORED_REPEAT:
return WGPUAddressMode_MirrorRepeat;
default:
SOKOL_UNREACHABLE;
return WGPUAddressMode_Force32;
}
}
_SOKOL_PRIVATE WGPUFilterMode _sg_wgpu_sampler_minmagfilter(sg_filter f) {
switch (f) {
case SG_FILTER_NEAREST:
case SG_FILTER_NEAREST_MIPMAP_NEAREST:
case SG_FILTER_NEAREST_MIPMAP_LINEAR:
return WGPUFilterMode_Nearest;
case SG_FILTER_LINEAR:
case SG_FILTER_LINEAR_MIPMAP_NEAREST:
case SG_FILTER_LINEAR_MIPMAP_LINEAR:
return WGPUFilterMode_Linear;
default:
SOKOL_UNREACHABLE;
return WGPUFilterMode_Force32;
}
}
_SOKOL_PRIVATE WGPUFilterMode _sg_wgpu_sampler_mipfilter(sg_filter f) {
switch (f) {
case SG_FILTER_NEAREST:
case SG_FILTER_LINEAR:
case SG_FILTER_NEAREST_MIPMAP_NEAREST:
case SG_FILTER_LINEAR_MIPMAP_NEAREST:
return WGPUFilterMode_Nearest;
case SG_FILTER_NEAREST_MIPMAP_LINEAR:
case SG_FILTER_LINEAR_MIPMAP_LINEAR:
return WGPUFilterMode_Linear;
default:
SOKOL_UNREACHABLE;
return WGPUFilterMode_Force32;
}
}
_SOKOL_PRIVATE WGPUIndexFormat _sg_wgpu_indexformat(sg_index_type t) {
/* NOTE: there's no WGPUIndexFormat_None */
return (t == SG_INDEXTYPE_UINT16) ? WGPUIndexFormat_Uint16 : WGPUIndexFormat_Uint32;
}
_SOKOL_PRIVATE WGPUInputStepMode _sg_wgpu_stepmode(sg_vertex_step s) {
return (s == SG_VERTEXSTEP_PER_VERTEX) ? WGPUInputStepMode_Vertex : WGPUInputStepMode_Instance;
}
_SOKOL_PRIVATE WGPUVertexFormat _sg_wgpu_vertexformat(sg_vertex_format f) {
switch (f) {
case SG_VERTEXFORMAT_FLOAT: return WGPUVertexFormat_Float;
case SG_VERTEXFORMAT_FLOAT2: return WGPUVertexFormat_Float2;
case SG_VERTEXFORMAT_FLOAT3: return WGPUVertexFormat_Float3;
case SG_VERTEXFORMAT_FLOAT4: return WGPUVertexFormat_Float4;
case SG_VERTEXFORMAT_BYTE4: return WGPUVertexFormat_Char4;
case SG_VERTEXFORMAT_BYTE4N: return WGPUVertexFormat_Char4Norm;
case SG_VERTEXFORMAT_UBYTE4: return WGPUVertexFormat_UChar4;
case SG_VERTEXFORMAT_UBYTE4N: return WGPUVertexFormat_UChar4Norm;
case SG_VERTEXFORMAT_SHORT2: return WGPUVertexFormat_Short2;
case SG_VERTEXFORMAT_SHORT2N: return WGPUVertexFormat_Short2Norm;
case SG_VERTEXFORMAT_USHORT2N: return WGPUVertexFormat_UShort2Norm;
case SG_VERTEXFORMAT_SHORT4: return WGPUVertexFormat_Short4;
case SG_VERTEXFORMAT_SHORT4N: return WGPUVertexFormat_Short4Norm;
case SG_VERTEXFORMAT_USHORT4N: return WGPUVertexFormat_UShort4Norm;
/* FIXME! UINT10_N2 */
case SG_VERTEXFORMAT_UINT10_N2:
default:
SOKOL_UNREACHABLE;
return WGPUVertexFormat_Force32;
}
}
_SOKOL_PRIVATE WGPUPrimitiveTopology _sg_wgpu_topology(sg_primitive_type t) {
switch (t) {
case SG_PRIMITIVETYPE_POINTS: return WGPUPrimitiveTopology_PointList;
case SG_PRIMITIVETYPE_LINES: return WGPUPrimitiveTopology_LineList;
case SG_PRIMITIVETYPE_LINE_STRIP: return WGPUPrimitiveTopology_LineStrip;
case SG_PRIMITIVETYPE_TRIANGLES: return WGPUPrimitiveTopology_TriangleList;
case SG_PRIMITIVETYPE_TRIANGLE_STRIP: return WGPUPrimitiveTopology_TriangleStrip;
default: SOKOL_UNREACHABLE; return WGPUPrimitiveTopology_Force32;
}
}
_SOKOL_PRIVATE WGPUFrontFace _sg_wgpu_frontface(sg_face_winding fw) {
return (fw == SG_FACEWINDING_CCW) ? WGPUFrontFace_CCW : WGPUFrontFace_CW;
}
_SOKOL_PRIVATE WGPUCullMode _sg_wgpu_cullmode(sg_cull_mode cm) {
switch (cm) {
case SG_CULLMODE_NONE: return WGPUCullMode_None;
case SG_CULLMODE_FRONT: return WGPUCullMode_Front;
case SG_CULLMODE_BACK: return WGPUCullMode_Back;
default: SOKOL_UNREACHABLE; return WGPUCullMode_Force32;
}
}
_SOKOL_PRIVATE WGPUTextureFormat _sg_wgpu_textureformat(sg_pixel_format p) {
switch (p) {
case SG_PIXELFORMAT_NONE: return WGPUTextureFormat_Undefined;
case SG_PIXELFORMAT_R8: return WGPUTextureFormat_R8Unorm;
case SG_PIXELFORMAT_R8SN: return WGPUTextureFormat_R8Snorm;
case SG_PIXELFORMAT_R8UI: return WGPUTextureFormat_R8Uint;
case SG_PIXELFORMAT_R8SI: return WGPUTextureFormat_R8Sint;
case SG_PIXELFORMAT_R16UI: return WGPUTextureFormat_R16Uint;
case SG_PIXELFORMAT_R16SI: return WGPUTextureFormat_R16Sint;
case SG_PIXELFORMAT_R16F: return WGPUTextureFormat_R16Float;
case SG_PIXELFORMAT_RG8: return WGPUTextureFormat_RG8Unorm;
case SG_PIXELFORMAT_RG8SN: return WGPUTextureFormat_RG8Snorm;
case SG_PIXELFORMAT_RG8UI: return WGPUTextureFormat_RG8Uint;
case SG_PIXELFORMAT_RG8SI: return WGPUTextureFormat_RG8Sint;
case SG_PIXELFORMAT_R32UI: return WGPUTextureFormat_R32Uint;
case SG_PIXELFORMAT_R32SI: return WGPUTextureFormat_R32Sint;
case SG_PIXELFORMAT_R32F: return WGPUTextureFormat_R32Float;
case SG_PIXELFORMAT_RG16UI: return WGPUTextureFormat_RG16Uint;
case SG_PIXELFORMAT_RG16SI: return WGPUTextureFormat_RG16Sint;
case SG_PIXELFORMAT_RG16F: return WGPUTextureFormat_RG16Float;
case SG_PIXELFORMAT_RGBA8: return WGPUTextureFormat_RGBA8Unorm;
case SG_PIXELFORMAT_RGBA8SN: return WGPUTextureFormat_RGBA8Snorm;
case SG_PIXELFORMAT_RGBA8UI: return WGPUTextureFormat_RGBA8Uint;
case SG_PIXELFORMAT_RGBA8SI: return WGPUTextureFormat_RGBA8Sint;
case SG_PIXELFORMAT_BGRA8: return WGPUTextureFormat_BGRA8Unorm;
case SG_PIXELFORMAT_RGB10A2: return WGPUTextureFormat_RGB10A2Unorm;
case SG_PIXELFORMAT_RG11B10F: return WGPUTextureFormat_RG11B10Float;
case SG_PIXELFORMAT_RG32UI: return WGPUTextureFormat_RG32Uint;
case SG_PIXELFORMAT_RG32SI: return WGPUTextureFormat_RG32Sint;
case SG_PIXELFORMAT_RG32F: return WGPUTextureFormat_RG32Float;
case SG_PIXELFORMAT_RGBA16UI: return WGPUTextureFormat_RGBA16Uint;
case SG_PIXELFORMAT_RGBA16SI: return WGPUTextureFormat_RGBA16Sint;
case SG_PIXELFORMAT_RGBA16F: return WGPUTextureFormat_RGBA16Float;
case SG_PIXELFORMAT_RGBA32UI: return WGPUTextureFormat_RGBA32Uint;
case SG_PIXELFORMAT_RGBA32SI: return WGPUTextureFormat_RGBA32Sint;
case SG_PIXELFORMAT_RGBA32F: return WGPUTextureFormat_RGBA32Float;
case SG_PIXELFORMAT_DEPTH: return WGPUTextureFormat_Depth24Plus;
case SG_PIXELFORMAT_DEPTH_STENCIL: return WGPUTextureFormat_Depth24PlusStencil8;
case SG_PIXELFORMAT_BC1_RGBA: return WGPUTextureFormat_BC1RGBAUnorm;
case SG_PIXELFORMAT_BC2_RGBA: return WGPUTextureFormat_BC2RGBAUnorm;
case SG_PIXELFORMAT_BC3_RGBA: return WGPUTextureFormat_BC3RGBAUnorm;
case SG_PIXELFORMAT_BC4_R: return WGPUTextureFormat_BC4RUnorm;
case SG_PIXELFORMAT_BC4_RSN: return WGPUTextureFormat_BC4RSnorm;
case SG_PIXELFORMAT_BC5_RG: return WGPUTextureFormat_BC5RGUnorm;
case SG_PIXELFORMAT_BC5_RGSN: return WGPUTextureFormat_BC5RGSnorm;
case SG_PIXELFORMAT_BC6H_RGBF: return WGPUTextureFormat_BC6HRGBSfloat;
case SG_PIXELFORMAT_BC6H_RGBUF: return WGPUTextureFormat_BC6HRGBUfloat;
case SG_PIXELFORMAT_BC7_RGBA: return WGPUTextureFormat_BC7RGBAUnorm;
/* NOT SUPPORTED */
case SG_PIXELFORMAT_R16:
case SG_PIXELFORMAT_R16SN:
case SG_PIXELFORMAT_RG16:
case SG_PIXELFORMAT_RG16SN:
case SG_PIXELFORMAT_RGBA16:
case SG_PIXELFORMAT_RGBA16SN:
case SG_PIXELFORMAT_PVRTC_RGB_2BPP:
case SG_PIXELFORMAT_PVRTC_RGB_4BPP:
case SG_PIXELFORMAT_PVRTC_RGBA_2BPP:
case SG_PIXELFORMAT_PVRTC_RGBA_4BPP:
case SG_PIXELFORMAT_ETC2_RGB8:
case SG_PIXELFORMAT_ETC2_RGB8A1:
case SG_PIXELFORMAT_ETC2_RGBA8:
case SG_PIXELFORMAT_ETC2_RG11:
case SG_PIXELFORMAT_ETC2_RG11SN:
default:
SOKOL_UNREACHABLE;
return WGPUTextureFormat_Force32;
}
}
/*
FIXME ??? this isn't needed anywhere?
_SOKOL_PRIVATE WGPUTextureAspect _sg_wgpu_texture_aspect(sg_pixel_format fmt) {
if (_sg_is_valid_rendertarget_depth_format(fmt)) {
if (!_sg_is_depth_stencil_format(fmt)) {
return WGPUTextureAspect_DepthOnly;
}
}
return WGPUTextureAspect_All;
}
*/
_SOKOL_PRIVATE WGPUCompareFunction _sg_wgpu_comparefunc(sg_compare_func f) {
switch (f) {
case SG_COMPAREFUNC_NEVER: return WGPUCompareFunction_Never;
case SG_COMPAREFUNC_LESS: return WGPUCompareFunction_Less;
case SG_COMPAREFUNC_EQUAL: return WGPUCompareFunction_Equal;
case SG_COMPAREFUNC_LESS_EQUAL: return WGPUCompareFunction_LessEqual;
case SG_COMPAREFUNC_GREATER: return WGPUCompareFunction_Greater;
case SG_COMPAREFUNC_NOT_EQUAL: return WGPUCompareFunction_NotEqual;
case SG_COMPAREFUNC_GREATER_EQUAL: return WGPUCompareFunction_GreaterEqual;
case SG_COMPAREFUNC_ALWAYS: return WGPUCompareFunction_Always;
default: SOKOL_UNREACHABLE; return WGPUCompareFunction_Force32;
}
}
_SOKOL_PRIVATE WGPUStencilOperation _sg_wgpu_stencilop(sg_stencil_op op) {
switch (op) {
case SG_STENCILOP_KEEP: return WGPUStencilOperation_Keep;
case SG_STENCILOP_ZERO: return WGPUStencilOperation_Zero;
case SG_STENCILOP_REPLACE: return WGPUStencilOperation_Replace;
case SG_STENCILOP_INCR_CLAMP: return WGPUStencilOperation_IncrementClamp;
case SG_STENCILOP_DECR_CLAMP: return WGPUStencilOperation_DecrementClamp;
case SG_STENCILOP_INVERT: return WGPUStencilOperation_Invert;
case SG_STENCILOP_INCR_WRAP: return WGPUStencilOperation_IncrementWrap;
case SG_STENCILOP_DECR_WRAP: return WGPUStencilOperation_DecrementWrap;
default: SOKOL_UNREACHABLE; return WGPUStencilOperation_Force32;
}
}
_SOKOL_PRIVATE WGPUBlendOperation _sg_wgpu_blendop(sg_blend_op op) {
switch (op) {
case SG_BLENDOP_ADD: return WGPUBlendOperation_Add;
case SG_BLENDOP_SUBTRACT: return WGPUBlendOperation_Subtract;
case SG_BLENDOP_REVERSE_SUBTRACT: return WGPUBlendOperation_ReverseSubtract;
default: SOKOL_UNREACHABLE; return WGPUBlendOperation_Force32;
}
}
_SOKOL_PRIVATE WGPUBlendFactor _sg_wgpu_blendfactor(sg_blend_factor f) {
switch (f) {
case SG_BLENDFACTOR_ZERO: return WGPUBlendFactor_Zero;
case SG_BLENDFACTOR_ONE: return WGPUBlendFactor_One;
case SG_BLENDFACTOR_SRC_COLOR: return WGPUBlendFactor_SrcColor;
case SG_BLENDFACTOR_ONE_MINUS_SRC_COLOR: return WGPUBlendFactor_OneMinusSrcColor;
case SG_BLENDFACTOR_SRC_ALPHA: return WGPUBlendFactor_SrcAlpha;
case SG_BLENDFACTOR_ONE_MINUS_SRC_ALPHA: return WGPUBlendFactor_OneMinusSrcAlpha;
case SG_BLENDFACTOR_DST_COLOR: return WGPUBlendFactor_DstColor;
case SG_BLENDFACTOR_ONE_MINUS_DST_COLOR: return WGPUBlendFactor_OneMinusDstColor;
case SG_BLENDFACTOR_DST_ALPHA: return WGPUBlendFactor_DstAlpha;
case SG_BLENDFACTOR_ONE_MINUS_DST_ALPHA: return WGPUBlendFactor_OneMinusDstAlpha;
case SG_BLENDFACTOR_SRC_ALPHA_SATURATED: return WGPUBlendFactor_SrcAlphaSaturated;
case SG_BLENDFACTOR_BLEND_COLOR: return WGPUBlendFactor_BlendColor;
case SG_BLENDFACTOR_ONE_MINUS_BLEND_COLOR: return WGPUBlendFactor_OneMinusBlendColor;
/* FIXME: separate blend alpha value not supported? */
case SG_BLENDFACTOR_BLEND_ALPHA: return WGPUBlendFactor_BlendColor;
case SG_BLENDFACTOR_ONE_MINUS_BLEND_ALPHA: return WGPUBlendFactor_OneMinusBlendColor;
default:
SOKOL_UNREACHABLE; return WGPUBlendFactor_Force32;
}
}
_SOKOL_PRIVATE WGPUColorWriteMaskFlags _sg_wgpu_colorwritemask(uint8_t m) {
WGPUColorWriteMaskFlags res = 0;
if (0 != (m & SG_COLORMASK_R)) {
res |= WGPUColorWriteMask_Red;
}
if (0 != (m & SG_COLORMASK_G)) {
res |= WGPUColorWriteMask_Green;
}
if (0 != (m & SG_COLORMASK_B)) {
res |= WGPUColorWriteMask_Blue;
}
if (0 != (m & SG_COLORMASK_A)) {
res |= WGPUColorWriteMask_Alpha;
}
return res;
}
_SOKOL_PRIVATE void _sg_wgpu_init_caps(void) {
_sg.backend = SG_BACKEND_WGPU;
_sg.features.instancing = true;
_sg.features.origin_top_left = true;
_sg.features.multiple_render_targets = true;
_sg.features.msaa_render_targets = true;
_sg.features.imagetype_3d = true;
_sg.features.imagetype_array = true;
_sg.features.image_clamp_to_border = false;
_sg.features.mrt_independent_blend_state = true;
_sg.features.mrt_independent_write_mask = true;
/* FIXME: max images size??? */
_sg.limits.max_image_size_2d = 8 * 1024;
_sg.limits.max_image_size_cube = 8 * 1024;
_sg.limits.max_image_size_3d = 2 * 1024;
_sg.limits.max_image_size_array = 8 * 1024;
_sg.limits.max_image_array_layers = 2 * 1024;
_sg.limits.max_vertex_attrs = SG_MAX_VERTEX_ATTRIBUTES;
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_R8]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_R8SN]);
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_R8UI]);
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_R8SI]);
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_R16UI]);
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_R16SI]);
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_R16F]);
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RG8]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_RG8SN]);
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RG8UI]);
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RG8SI]);
_sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_R32UI]);
_sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_R32SI]);
_sg_pixelformat_sbr(&_sg.formats[SG_PIXELFORMAT_R32F]);
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RG16UI]);
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RG16SI]);
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RG16F]);
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RGBA8]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_RGBA8SN]);
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RGBA8UI]);
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RGBA8SI]);
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_BGRA8]);
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RGB10A2]);
_sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_RG32UI]);
_sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_RG32SI]);
_sg_pixelformat_sbr(&_sg.formats[SG_PIXELFORMAT_RG32F]);
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RGBA16UI]);
_sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RGBA16SI]);
_sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RGBA16F]);
_sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_RGBA32UI]);
_sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_RGBA32SI]);
_sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_RGBA32F]);
_sg_pixelformat_srmd(&_sg.formats[SG_PIXELFORMAT_DEPTH]);
_sg_pixelformat_srmd(&_sg.formats[SG_PIXELFORMAT_DEPTH_STENCIL]);
/* FIXME FIXME FIXME: need to check if BC texture compression is
actually supported, currently the WebGPU C-API doesn't allow this
*/
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC1_RGBA]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC2_RGBA]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC3_RGBA]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC4_R]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC4_RSN]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC5_RG]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC5_RGSN]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC6H_RGBF]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC6H_RGBUF]);
_sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC7_RGBA]);
}
/*
WGPU uniform buffer pool implementation:
At start of frame, a mapped buffer is grabbed from the pool,
or a new buffer is created if there is no mapped buffer available.
At end of frame, the current buffer is unmapped before queue submit,
and async-mapped immediately again.
UNIFORM BUFFER FIXME:
- As per WebGPU spec, it should be possible to create a Uniform|MapWrite
buffer, but this isn't currently allowed in Dawn.
*/
_SOKOL_PRIVATE void _sg_wgpu_ubpool_init(const sg_desc* desc) {
/* Add the max-uniform-update size (64 KB) to the requested buffer size,
this is to prevent validation errors in the WebGPU implementation
if the entire buffer size is used per frame. 64 KB is the allowed
max uniform update size on NVIDIA
*/
_sg.wgpu.ub.num_bytes = desc->uniform_buffer_size + _SG_WGPU_MAX_UNIFORM_UPDATE_SIZE;
WGPUBufferDescriptor ub_desc;
memset(&ub_desc, 0, sizeof(ub_desc));
ub_desc.size = _sg.wgpu.ub.num_bytes;
ub_desc.usage = WGPUBufferUsage_Uniform|WGPUBufferUsage_CopyDst;
_sg.wgpu.ub.buf = wgpuDeviceCreateBuffer(_sg.wgpu.dev, &ub_desc);
SOKOL_ASSERT(_sg.wgpu.ub.buf);
WGPUBindGroupLayoutBinding ub_bglb_desc[SG_NUM_SHADER_STAGES][SG_MAX_SHADERSTAGE_UBS];
memset(ub_bglb_desc, 0, sizeof(ub_bglb_desc));
for (int stage_index = 0; stage_index < SG_NUM_SHADER_STAGES; stage_index++) {
WGPUShaderStage vis = (stage_index == SG_SHADERSTAGE_VS) ? WGPUShaderStage_Vertex : WGPUShaderStage_Fragment;
for (int ub_index = 0; ub_index < SG_MAX_SHADERSTAGE_UBS; ub_index++) {
int bind_index = stage_index * SG_MAX_SHADERSTAGE_UBS + ub_index;
ub_bglb_desc[stage_index][ub_index].binding = bind_index;
ub_bglb_desc[stage_index][ub_index].visibility = vis;
ub_bglb_desc[stage_index][ub_index].type = WGPUBindingType_UniformBuffer;
ub_bglb_desc[stage_index][ub_index].hasDynamicOffset = true;
}
}
WGPUBindGroupLayoutDescriptor ub_bgl_desc;
memset(&ub_bgl_desc, 0, sizeof(ub_bgl_desc));
ub_bgl_desc.bindingCount = SG_NUM_SHADER_STAGES * SG_MAX_SHADERSTAGE_UBS;
ub_bgl_desc.bindings = &ub_bglb_desc[0][0];
_sg.wgpu.ub.bindgroup_layout = wgpuDeviceCreateBindGroupLayout(_sg.wgpu.dev, &ub_bgl_desc);
SOKOL_ASSERT(_sg.wgpu.ub.bindgroup_layout);
WGPUBindGroupBinding ub_bgb[SG_NUM_SHADER_STAGES][SG_MAX_SHADERSTAGE_UBS];
memset(ub_bgb, 0, sizeof(ub_bgb));
for (int stage_index = 0; stage_index < SG_NUM_SHADER_STAGES; stage_index++) {
for (int ub_index = 0; ub_index < SG_MAX_SHADERSTAGE_UBS; ub_index++) {
int bind_index = stage_index * SG_MAX_SHADERSTAGE_UBS + ub_index;
ub_bgb[stage_index][ub_index].binding = bind_index;
ub_bgb[stage_index][ub_index].buffer = _sg.wgpu.ub.buf;
// FIXME FIXME FIXME FIXME: HACK FOR VALIDATION BUG IN DAWN
ub_bgb[stage_index][ub_index].size = (1<<16);
}
}
WGPUBindGroupDescriptor bg_desc;
memset(&bg_desc, 0, sizeof(bg_desc));
bg_desc.layout = _sg.wgpu.ub.bindgroup_layout;
bg_desc.bindingCount = SG_NUM_SHADER_STAGES * SG_MAX_SHADERSTAGE_UBS;
bg_desc.bindings = &ub_bgb[0][0];
_sg.wgpu.ub.bindgroup = wgpuDeviceCreateBindGroup(_sg.wgpu.dev, &bg_desc);
SOKOL_ASSERT(_sg.wgpu.ub.bindgroup);
}
_SOKOL_PRIVATE void _sg_wgpu_ubpool_discard(void) {
if (_sg.wgpu.ub.buf) {
wgpuBufferRelease(_sg.wgpu.ub.buf);
_sg.wgpu.ub.buf = 0;
}
if (_sg.wgpu.ub.bindgroup) {
wgpuBindGroupRelease(_sg.wgpu.ub.bindgroup);
_sg.wgpu.ub.bindgroup = 0;
}
if (_sg.wgpu.ub.bindgroup_layout) {
wgpuBindGroupLayoutRelease(_sg.wgpu.ub.bindgroup_layout);
_sg.wgpu.ub.bindgroup_layout = 0;
}
for (int i = 0; i < _sg.wgpu.ub.stage.num; i++) {
if (_sg.wgpu.ub.stage.buf[i]) {
wgpuBufferRelease(_sg.wgpu.ub.stage.buf[i]);
_sg.wgpu.ub.stage.buf[i] = 0;
_sg.wgpu.ub.stage.ptr[i] = 0;
}
}
}
_SOKOL_PRIVATE void _sg_wgpu_ubpool_mapped_callback(WGPUBufferMapAsyncStatus status, void* data, uint64_t data_len, void* user_data) {
if (!_sg.wgpu.valid) {
return;
}
/* FIXME: better handling for this */
if (WGPUBufferMapAsyncStatus_Success != status) {
SOKOL_LOG("Mapping uniform buffer failed!\n");
SOKOL_ASSERT(false);
}
SOKOL_ASSERT(data && (data_len == _sg.wgpu.ub.num_bytes));
int index = (int)(intptr_t) user_data;
SOKOL_ASSERT(index < _sg.wgpu.ub.stage.num);
SOKOL_ASSERT(0 == _sg.wgpu.ub.stage.ptr[index]);
_sg.wgpu.ub.stage.ptr[index] = (uint8_t*) data;
}
_SOKOL_PRIVATE void _sg_wgpu_ubpool_next_frame(bool first_frame) {
/* immediately request a new mapping for the last frame's current staging buffer */
if (!first_frame) {
WGPUBuffer ub_src = _sg.wgpu.ub.stage.buf[_sg.wgpu.ub.stage.cur];
wgpuBufferMapWriteAsync(ub_src, _sg_wgpu_ubpool_mapped_callback, (void*)(intptr_t)_sg.wgpu.ub.stage.cur);
}
/* rewind per-frame offsets */
_sg.wgpu.ub.offset = 0;
memset(&_sg.wgpu.ub.bind_offsets, 0, sizeof(_sg.wgpu.ub.bind_offsets));
/* check if a mapped staging buffer is available, otherwise create one */
for (int i = 0; i < _sg.wgpu.ub.stage.num; i++) {
if (_sg.wgpu.ub.stage.ptr[i]) {
_sg.wgpu.ub.stage.cur = i;
return;
}
}
/* no mapped uniform buffer available, create one */
SOKOL_ASSERT(_sg.wgpu.ub.stage.num < _SG_WGPU_STAGING_PIPELINE_SIZE);
_sg.wgpu.ub.stage.cur = _sg.wgpu.ub.stage.num++;
const int cur = _sg.wgpu.ub.stage.cur;
WGPUBufferDescriptor desc;
memset(&desc, 0, sizeof(desc));
desc.size = _sg.wgpu.ub.num_bytes;
desc.usage = WGPUBufferUsage_CopySrc|WGPUBufferUsage_MapWrite;
WGPUCreateBufferMappedResult res = wgpuDeviceCreateBufferMapped(_sg.wgpu.dev, &desc);
_sg.wgpu.ub.stage.buf[cur] = res.buffer;
_sg.wgpu.ub.stage.ptr[cur] = (uint8_t*) res.data;
SOKOL_ASSERT(_sg.wgpu.ub.stage.buf[cur]);
SOKOL_ASSERT(_sg.wgpu.ub.stage.ptr[cur]);
SOKOL_ASSERT(res.dataLength == _sg.wgpu.ub.num_bytes);
}
_SOKOL_PRIVATE void _sg_wgpu_ubpool_flush(void) {
/* unmap staging buffer and copy to uniform buffer */
const int cur = _sg.wgpu.ub.stage.cur;
SOKOL_ASSERT(_sg.wgpu.ub.stage.ptr[cur]);
_sg.wgpu.ub.stage.ptr[cur] = 0;
WGPUBuffer src_buf = _sg.wgpu.ub.stage.buf[cur];
wgpuBufferUnmap(src_buf);
if (_sg.wgpu.ub.offset > 0) {
WGPUBuffer dst_buf = _sg.wgpu.ub.buf;
wgpuCommandEncoderCopyBufferToBuffer(_sg.wgpu.render_cmd_enc, src_buf, 0, dst_buf, 0, _sg.wgpu.ub.offset);
}
}
/* helper function to compute number of bytes needed in staging buffer to copy image data */
_SOKOL_PRIVATE uint32_t _sg_wgpu_image_data_buffer_size(const _sg_image_t* img) {
uint32_t num_bytes = 0;
const uint32_t num_faces = (img->cmn.type == SG_IMAGETYPE_CUBE) ? 6:1;
const uint32_t num_slices = (img->cmn.type == SG_IMAGETYPE_ARRAY) ? img->cmn.num_slices : 1;
for (int mip_index = 0; mip_index < img->cmn.num_mipmaps; mip_index++) {
const uint32_t mip_width = _sg_max(img->cmn.width >> mip_index, 1);
const uint32_t mip_height = _sg_max(img->cmn.height >> mip_index, 1);
/* row-pitch must be 256-aligend */
const uint32_t bytes_per_slice = _sg_surface_pitch(img->cmn.pixel_format, mip_width, mip_height, _SG_WGPU_ROWPITCH_ALIGN);
num_bytes += bytes_per_slice * num_slices * num_faces;
}
return num_bytes;
}
/* helper function to copy image data into a texture via a staging buffer, returns number of
bytes copied
*/
_SOKOL_PRIVATE uint32_t _sg_wgpu_copy_image_data(WGPUBuffer stg_buf, uint8_t* stg_base_ptr, uint32_t stg_base_offset, _sg_image_t* img, const sg_image_data* data) {
SOKOL_ASSERT(_sg.wgpu.staging_cmd_enc);
SOKOL_ASSERT(stg_buf && stg_base_ptr);
SOKOL_ASSERT(img);
SOKOL_ASSERT(data);
uint32_t stg_offset = stg_base_offset;
const uint32_t num_faces = (img->cmn.type == SG_IMAGETYPE_CUBE) ? 6:1;
const uint32_t num_slices = (img->cmn.type == SG_IMAGETYPE_ARRAY) ? img->cmn.num_slices : 1;
const sg_pixel_format fmt = img->cmn.pixel_format;
WGPUBufferCopyView src_view;
memset(&src_view, 0, sizeof(src_view));
src_view.buffer = stg_buf;
WGPUTextureCopyView dst_view;
memset(&dst_view, 0, sizeof(dst_view));
dst_view.texture = img->wgpu.tex;
WGPUExtent3D extent;
memset(&extent, 0, sizeof(extent));
for (uint32_t face_index = 0; face_index < num_faces; face_index++) {
for (uint32_t mip_index = 0; mip_index < (uint32_t)img->cmn.num_mipmaps; mip_index++) {
SOKOL_ASSERT(data->subimage[face_index][mip_index].ptr);
SOKOL_ASSERT(data->subimage[face_index][mip_index].size > 0);
const uint8_t* src_base_ptr = (const uint8_t*)data->subimage[face_index][mip_index].ptr;
SOKOL_ASSERT(src_base_ptr);
uint8_t* dst_base_ptr = stg_base_ptr + stg_offset;
const uint32_t mip_width = _sg_max(img->cmn.width >> mip_index, 1);
const uint32_t mip_height = _sg_max(img->cmn.height >> mip_index, 1);
const uint32_t mip_depth = (img->cmn.type == SG_IMAGETYPE_3D) ? _sg_max(img->cmn.num_slices >> mip_index, 1) : 1;
const uint32_t num_rows = _sg_num_rows(fmt, mip_height);
const uint32_t src_bytes_per_row = _sg_row_pitch(fmt, mip_width, 1);
const uint32_t dst_bytes_per_row = _sg_row_pitch(fmt, mip_width, _SG_WGPU_ROWPITCH_ALIGN);
const uint32_t src_bytes_per_slice = _sg_surface_pitch(fmt, mip_width, mip_height, 1);
const uint32_t dst_bytes_per_slice = _sg_surface_pitch(fmt, mip_width, mip_height, _SG_WGPU_ROWPITCH_ALIGN);
SOKOL_ASSERT((uint32_t)data->subimage[face_index][mip_index].size == (src_bytes_per_slice * num_slices));
SOKOL_ASSERT(src_bytes_per_row <= dst_bytes_per_row);
SOKOL_ASSERT(src_bytes_per_slice == (src_bytes_per_row * num_rows));
SOKOL_ASSERT(dst_bytes_per_slice == (dst_bytes_per_row * num_rows));
_SOKOL_UNUSED(src_bytes_per_slice);
/* copy data into mapped staging buffer */
if (src_bytes_per_row == dst_bytes_per_row) {
/* can do a single memcpy */
uint32_t num_bytes = data->subimage[face_index][mip_index].size;
memcpy(dst_base_ptr, src_base_ptr, num_bytes);
}
else {
/* src/dst pitch doesn't match, need to copy row by row */
uint8_t* dst_ptr = dst_base_ptr;
const uint8_t* src_ptr = src_base_ptr;
for (uint32_t slice_index = 0; slice_index < num_slices; slice_index++) {
SOKOL_ASSERT(dst_ptr == dst_base_ptr + slice_index * dst_bytes_per_slice);
for (uint32_t row_index = 0; row_index < num_rows; row_index++) {
memcpy(dst_ptr, src_ptr, src_bytes_per_row);
src_ptr += src_bytes_per_row;
dst_ptr += dst_bytes_per_row;
}
}
}
/* record the staging copy operation into command encoder */
src_view.imageHeight = mip_height;
src_view.rowPitch = dst_bytes_per_row;
dst_view.mipLevel = mip_index;
extent.width = mip_width;
extent.height = mip_height;
extent.depth = mip_depth;
SOKOL_ASSERT((img->cmn.type != SG_IMAGETYPE_CUBE) || (num_slices == 1));
for (uint32_t slice_index = 0; slice_index < num_slices; slice_index++) {
const uint32_t layer_index = (img->cmn.type == SG_IMAGETYPE_ARRAY) ? slice_index : face_index;
src_view.offset = stg_offset;
dst_view.arrayLayer = layer_index;
wgpuCommandEncoderCopyBufferToTexture(_sg.wgpu.staging_cmd_enc, &src_view, &dst_view, &extent);
stg_offset += dst_bytes_per_slice;
SOKOL_ASSERT(stg_offset <= _sg.wgpu.staging.num_bytes);
}
}
}
SOKOL_ASSERT(stg_offset >= stg_base_offset);
return (stg_offset - stg_base_offset);
}
/*
The WGPU staging buffer implementation:
Very similar to the uniform buffer pool, there's a pool of big
per-frame staging buffers, each must be big enough to hold
all data uploaded to dynamic resources for one frame.
Staging buffers are created on demand and reused, because the
'frame pipeline depth' of WGPU isn't predictable.
The difference to the uniform buffer system is that there isn't
a 1:1 relationship for source- and destination for the
data-copy operation. There's always one staging buffer as copy-source
per frame, but many copy-destinations (regular vertex/index buffers
or images). Instead of one big copy-operation at the end of the frame,
multiple copy-operations will be written throughout the frame.
*/
_SOKOL_PRIVATE void _sg_wgpu_staging_init(const sg_desc* desc) {
SOKOL_ASSERT(desc && (desc->staging_buffer_size > 0));
_sg.wgpu.staging.num_bytes = desc->staging_buffer_size;
/* there's actually nothing more to do here */
}
_SOKOL_PRIVATE void _sg_wgpu_staging_discard(void) {
for (int i = 0; i < _sg.wgpu.staging.num; i++) {
if (_sg.wgpu.staging.buf[i]) {
wgpuBufferRelease(_sg.wgpu.staging.buf[i]);
_sg.wgpu.staging.buf[i] = 0;
_sg.wgpu.staging.ptr[i] = 0;
}
}
}
_SOKOL_PRIVATE void _sg_wgpu_staging_mapped_callback(WGPUBufferMapAsyncStatus status, void* data, uint64_t data_len, void* user_data) {
if (!_sg.wgpu.valid) {
return;
}
/* FIXME: better handling for this */
if (WGPUBufferMapAsyncStatus_Success != status) {
SOKOL_ASSERT("Mapping staging buffer failed!\n");
SOKOL_ASSERT(false);
}
SOKOL_ASSERT(data && (data_len == _sg.wgpu.staging.num_bytes));
int index = (int)(intptr_t) user_data;
SOKOL_ASSERT(index < _sg.wgpu.staging.num);
SOKOL_ASSERT(0 == _sg.wgpu.staging.ptr[index]);
_sg.wgpu.staging.ptr[index] = (uint8_t*) data;
}
_SOKOL_PRIVATE void _sg_wgpu_staging_next_frame(bool first_frame) {
/* immediately request a new mapping for the last frame's current staging buffer */
if (!first_frame) {
WGPUBuffer cur_buf = _sg.wgpu.staging.buf[_sg.wgpu.staging.cur];
wgpuBufferMapWriteAsync(cur_buf, _sg_wgpu_staging_mapped_callback, (void*)(intptr_t)_sg.wgpu.staging.cur);
}
/* rewind staging-buffer offset */
_sg.wgpu.staging.offset = 0;
/* check if mapped staging buffer is available, otherwise create one */
for (int i = 0; i < _sg.wgpu.staging.num; i++) {
if (_sg.wgpu.staging.ptr[i]) {
_sg.wgpu.staging.cur = i;
return;
}
}
/* no mapped buffer available, create one */
SOKOL_ASSERT(_sg.wgpu.staging.num < _SG_WGPU_STAGING_PIPELINE_SIZE);
_sg.wgpu.staging.cur = _sg.wgpu.staging.num++;
const int cur = _sg.wgpu.staging.cur;
WGPUBufferDescriptor desc;
memset(&desc, 0, sizeof(desc));
desc.size = _sg.wgpu.staging.num_bytes;
desc.usage = WGPUBufferUsage_CopySrc|WGPUBufferUsage_MapWrite;
WGPUCreateBufferMappedResult res = wgpuDeviceCreateBufferMapped(_sg.wgpu.dev, &desc);
_sg.wgpu.staging.buf[cur] = res.buffer;
_sg.wgpu.staging.ptr[cur] = (uint8_t*) res.data;
SOKOL_ASSERT(_sg.wgpu.staging.buf[cur]);
SOKOL_ASSERT(_sg.wgpu.staging.ptr[cur]);
SOKOL_ASSERT(res.dataLength == _sg.wgpu.staging.num_bytes);
}
_SOKOL_PRIVATE uint32_t _sg_wgpu_staging_copy_to_buffer(WGPUBuffer dst_buf, uint32_t dst_buf_offset, const void* data, uint32_t data_num_bytes) {
/* Copy a chunk of data into the staging buffer, and record a blit-operation into
the command encoder, bump the offset for the next data chunk, return 0 if there
was not enough room in the staging buffer, return the number of actually
copied bytes on success.
NOTE: that the number of staging bytes to be copied must be a multiple of 4.
*/
SOKOL_ASSERT(_sg.wgpu.staging_cmd_enc);
SOKOL_ASSERT((dst_buf_offset & 3) == 0);
SOKOL_ASSERT(data_num_bytes > 0);
uint32_t copy_num_bytes = _sg_roundup(data_num_bytes, 4);
if ((_sg.wgpu.staging.offset + copy_num_bytes) >= _sg.wgpu.staging.num_bytes) {
SOKOL_LOG("WGPU: Per frame staging buffer full (in _sg_wgpu_staging_copy_to_buffer())!\n");
return false;
}
const int cur = _sg.wgpu.staging.cur;
SOKOL_ASSERT(_sg.wgpu.staging.ptr[cur]);
uint32_t stg_buf_offset = _sg.wgpu.staging.offset;
uint8_t* stg_ptr = _sg.wgpu.staging.ptr[cur] + stg_buf_offset;
memcpy(stg_ptr, data, data_num_bytes);
WGPUBuffer stg_buf = _sg.wgpu.staging.buf[cur];
wgpuCommandEncoderCopyBufferToBuffer(_sg.wgpu.staging_cmd_enc, stg_buf, stg_buf_offset, dst_buf, dst_buf_offset, copy_num_bytes);
_sg.wgpu.staging.offset = stg_buf_offset + copy_num_bytes;
return copy_num_bytes;
}
_SOKOL_PRIVATE bool _sg_wgpu_staging_copy_to_texture(_sg_image_t* img, const sg_image_data* data) {
/* similar to _sg_wgpu_staging_copy_to_buffer(), but with image data instead */
SOKOL_ASSERT(_sg.wgpu.staging_cmd_enc);
uint32_t num_bytes = _sg_wgpu_image_data_buffer_size(img);
if ((_sg.wgpu.staging.offset + num_bytes) >= _sg.wgpu.staging.num_bytes) {
SOKOL_LOG("WGPU: Per frame staging buffer full (in _sg_wgpu_staging_copy_to_texture)!\n");
return false;
}
const int cur = _sg.wgpu.staging.cur;
SOKOL_ASSERT(_sg.wgpu.staging.ptr[cur]);
uint32_t stg_offset = _sg.wgpu.staging.offset;
uint8_t* stg_ptr = _sg.wgpu.staging.ptr[cur];
WGPUBuffer stg_buf = _sg.wgpu.staging.buf[cur];
uint32_t bytes_copied = _sg_wgpu_copy_image_data(stg_buf, stg_ptr, stg_offset, img, data);
_SOKOL_UNUSED(bytes_copied);
SOKOL_ASSERT(bytes_copied == num_bytes);
_sg.wgpu.staging.offset = _sg_roundup(stg_offset + num_bytes, _SG_WGPU_STAGING_ALIGN);
return true;
}
_SOKOL_PRIVATE void _sg_wgpu_staging_unmap(void) {
/* called at end of frame before queue-submit */
const int cur = _sg.wgpu.staging.cur;
SOKOL_ASSERT(_sg.wgpu.staging.ptr[cur]);
_sg.wgpu.staging.ptr[cur] = 0;
wgpuBufferUnmap(_sg.wgpu.staging.buf[cur]);
}
/*--- WGPU sampler cache functions ---*/
_SOKOL_PRIVATE void _sg_wgpu_init_sampler_cache(const sg_desc* desc) {
SOKOL_ASSERT(desc->sampler_cache_size > 0);
_sg_smpcache_init(&_sg.wgpu.sampler_cache, desc->sampler_cache_size);
}
_SOKOL_PRIVATE void _sg_wgpu_destroy_sampler_cache(void) {
SOKOL_ASSERT(_sg.wgpu.sampler_cache.items);
SOKOL_ASSERT(_sg.wgpu.sampler_cache.num_items <= _sg.wgpu.sampler_cache.capacity);
for (int i = 0; i < _sg.wgpu.sampler_cache.num_items; i++) {
wgpuSamplerRelease((WGPUSampler)_sg_smpcache_sampler(&_sg.wgpu.sampler_cache, i));
}
_sg_smpcache_discard(&_sg.wgpu.sampler_cache);
}
_SOKOL_PRIVATE WGPUSampler _sg_wgpu_create_sampler(const sg_image_desc* img_desc) {
SOKOL_ASSERT(img_desc);
int index = _sg_smpcache_find_item(&_sg.wgpu.sampler_cache, img_desc);
if (index >= 0) {
/* reuse existing sampler */
return (WGPUSampler) _sg_smpcache_sampler(&_sg.wgpu.sampler_cache, index);
}
else {
/* create a new WGPU sampler and add to sampler cache */
/* FIXME: anisotropic filtering not supported? */
WGPUSamplerDescriptor smp_desc;
memset(&smp_desc, 0, sizeof(smp_desc));
smp_desc.addressModeU = _sg_wgpu_sampler_addrmode(img_desc->wrap_u);
smp_desc.addressModeV = _sg_wgpu_sampler_addrmode(img_desc->wrap_v);
smp_desc.addressModeW = _sg_wgpu_sampler_addrmode(img_desc->wrap_w);
smp_desc.magFilter = _sg_wgpu_sampler_minmagfilter(img_desc->mag_filter);
smp_desc.minFilter = _sg_wgpu_sampler_minmagfilter(img_desc->min_filter);
smp_desc.mipmapFilter = _sg_wgpu_sampler_mipfilter(img_desc->min_filter);
smp_desc.lodMinClamp = img_desc->min_lod;
smp_desc.lodMaxClamp = img_desc->max_lod;
WGPUSampler smp = wgpuDeviceCreateSampler(_sg.wgpu.dev, &smp_desc);
SOKOL_ASSERT(smp);
_sg_smpcache_add_item(&_sg.wgpu.sampler_cache, img_desc, (uintptr_t)smp);
return smp;
}
}
/*--- WGPU backend API functions ---*/
_SOKOL_PRIVATE void _sg_wgpu_setup_backend(const sg_desc* desc) {
SOKOL_ASSERT(desc);
SOKOL_ASSERT(desc->context.wgpu.device);
SOKOL_ASSERT(desc->context.wgpu.render_view_cb || desc->context.wgpu.render_view_userdata_cb);
SOKOL_ASSERT(desc->context.wgpu.resolve_view_cb || desc->context.wgpu.resolve_view_userdata_cb);
SOKOL_ASSERT(desc->context.wgpu.depth_stencil_view_cb || desc->context.wgpu.depth_stencil_view_userdata_cb);
SOKOL_ASSERT(desc->uniform_buffer_size > 0);
SOKOL_ASSERT(desc->staging_buffer_size > 0);
_sg.backend = SG_BACKEND_WGPU;
_sg.wgpu.valid = true;
_sg.wgpu.dev = (WGPUDevice) desc->context.wgpu.device;
_sg.wgpu.render_view_cb = (WGPUTextureView(*)(void)) desc->context.wgpu.render_view_cb;
_sg.wgpu.render_view_userdata_cb = (WGPUTextureView(*)(void*)) desc->context.wgpu.render_view_userdata_cb;
_sg.wgpu.resolve_view_cb = (WGPUTextureView(*)(void)) desc->context.wgpu.resolve_view_cb;
_sg.wgpu.resolve_view_userdata_cb = (WGPUTextureView(*)(void*)) desc->context.wgpu.resolve_view_userdata_cb;
_sg.wgpu.depth_stencil_view_cb = (WGPUTextureView(*)(void)) desc->context.wgpu.depth_stencil_view_cb;
_sg.wgpu.depth_stencil_view_userdata_cb = (WGPUTextureView(*)(void*)) desc->context.wgpu.depth_stencil_view_userdata_cb;
_sg.wgpu.user_data = desc->context.wgpu.user_data;
_sg.wgpu.queue = wgpuDeviceCreateQueue(_sg.wgpu.dev);
SOKOL_ASSERT(_sg.wgpu.queue);
/* setup WebGPU features and limits */
_sg_wgpu_init_caps();
/* setup the sampler cache, uniform and staging buffer pools */
_sg_wgpu_init_sampler_cache(&_sg.desc);
_sg_wgpu_ubpool_init(desc);
_sg_wgpu_ubpool_next_frame(true);
_sg_wgpu_staging_init(desc);
_sg_wgpu_staging_next_frame(true);
/* create an empty bind group for shader stages without bound images */
WGPUBindGroupLayoutDescriptor bgl_desc;
memset(&bgl_desc, 0, sizeof(bgl_desc));
WGPUBindGroupLayout empty_bgl = wgpuDeviceCreateBindGroupLayout(_sg.wgpu.dev, &bgl_desc);
SOKOL_ASSERT(empty_bgl);
WGPUBindGroupDescriptor bg_desc;
memset(&bg_desc, 0, sizeof(bg_desc));
bg_desc.layout = empty_bgl;
_sg.wgpu.empty_bind_group = wgpuDeviceCreateBindGroup(_sg.wgpu.dev, &bg_desc);
SOKOL_ASSERT(_sg.wgpu.empty_bind_group);
wgpuBindGroupLayoutRelease(empty_bgl);
/* create initial per-frame command encoders */
WGPUCommandEncoderDescriptor cmd_enc_desc;
memset(&cmd_enc_desc, 0, sizeof(cmd_enc_desc));
_sg.wgpu.render_cmd_enc = wgpuDeviceCreateCommandEncoder(_sg.wgpu.dev, &cmd_enc_desc);
SOKOL_ASSERT(_sg.wgpu.render_cmd_enc);
_sg.wgpu.staging_cmd_enc = wgpuDeviceCreateCommandEncoder(_sg.wgpu.dev, &cmd_enc_desc);
SOKOL_ASSERT(_sg.wgpu.staging_cmd_enc);
}
_SOKOL_PRIVATE void _sg_wgpu_discard_backend(void) {
SOKOL_ASSERT(_sg.wgpu.valid);
SOKOL_ASSERT(_sg.wgpu.render_cmd_enc);
SOKOL_ASSERT(_sg.wgpu.staging_cmd_enc);
_sg.wgpu.valid = false;
_sg_wgpu_ubpool_discard();
_sg_wgpu_staging_discard();
_sg_wgpu_destroy_sampler_cache();
wgpuBindGroupRelease(_sg.wgpu.empty_bind_group);
wgpuCommandEncoderRelease(_sg.wgpu.render_cmd_enc);
_sg.wgpu.render_cmd_enc = 0;
wgpuCommandEncoderRelease(_sg.wgpu.staging_cmd_enc);
_sg.wgpu.staging_cmd_enc = 0;
if (_sg.wgpu.queue) {
wgpuQueueRelease(_sg.wgpu.queue);
_sg.wgpu.queue = 0;
}
}
_SOKOL_PRIVATE void _sg_wgpu_reset_state_cache(void) {
SOKOL_LOG("_sg_wgpu_reset_state_cache: FIXME\n");
}
_SOKOL_PRIVATE sg_resource_state _sg_wgpu_create_context(_sg_context_t* ctx) {
SOKOL_ASSERT(ctx);
_SOKOL_UNUSED(ctx);
return SG_RESOURCESTATE_VALID;
}
_SOKOL_PRIVATE void _sg_wgpu_destroy_context(_sg_context_t* ctx) {
SOKOL_ASSERT(ctx);
_SOKOL_UNUSED(ctx);
}
_SOKOL_PRIVATE void _sg_wgpu_activate_context(_sg_context_t* ctx) {
(void)ctx;
SOKOL_LOG("_sg_wgpu_activate_context: FIXME\n");
}
_SOKOL_PRIVATE sg_resource_state _sg_wgpu_create_buffer(_sg_buffer_t* buf, const sg_buffer_desc* desc) {
SOKOL_ASSERT(buf && desc);
const bool injected = (0 != desc->wgpu_buffer);
_sg_buffer_common_init(&buf->cmn, desc);
if (injected) {
buf->wgpu.buf = (WGPUBuffer) desc->wgpu_buffer;
wgpuBufferReference(buf->wgpu.buf);
}
else {
WGPUBufferDescriptor wgpu_buf_desc;
memset(&wgpu_buf_desc, 0, sizeof(wgpu_buf_desc));
wgpu_buf_desc.usage = _sg_wgpu_buffer_usage(buf->cmn.type, buf->cmn.usage);
wgpu_buf_desc.size = buf->cmn.size;
if (SG_USAGE_IMMUTABLE == buf->cmn.usage) {
SOKOL_ASSERT(desc->data.ptr);
WGPUCreateBufferMappedResult res = wgpuDeviceCreateBufferMapped(_sg.wgpu.dev, &wgpu_buf_desc);
buf->wgpu.buf = res.buffer;
SOKOL_ASSERT(res.data && (res.dataLength == buf->cmn.size));
memcpy(res.data, desc->data.ptr, buf->cmn.size);
wgpuBufferUnmap(res.buffer);
}
else {
buf->wgpu.buf = wgpuDeviceCreateBuffer(_sg.wgpu.dev, &wgpu_buf_desc);
}
}
return SG_RESOURCESTATE_VALID;
}
_SOKOL_PRIVATE void _sg_wgpu_destroy_buffer(_sg_buffer_t* buf) {
SOKOL_ASSERT(buf);
WGPUBuffer wgpu_buf = buf->wgpu.buf;
if (0 != wgpu_buf) {
wgpuBufferRelease(wgpu_buf);
}
}
_SOKOL_PRIVATE void _sg_wgpu_init_texdesc_common(WGPUTextureDescriptor* wgpu_tex_desc, const sg_image_desc* desc) {
wgpu_tex_desc->usage = WGPUTextureUsage_Sampled|WGPUTextureUsage_CopyDst;
wgpu_tex_desc->dimension = _sg_wgpu_tex_dim(desc->type);
wgpu_tex_desc->size.width = desc->width;
wgpu_tex_desc->size.height = desc->height;
if (desc->type == SG_IMAGETYPE_3D) {
wgpu_tex_desc->size.depth = desc->num_slices;
wgpu_tex_desc->arrayLayerCount = 1;
}
else if (desc->type == SG_IMAGETYPE_CUBE) {
wgpu_tex_desc->size.depth = 1;
wgpu_tex_desc->arrayLayerCount = 6;
}
else {
wgpu_tex_desc->size.depth = 1;
wgpu_tex_desc->arrayLayerCount = desc->num_slices;
}
wgpu_tex_desc->format = _sg_wgpu_textureformat(desc->pixel_format);
wgpu_tex_desc->mipLevelCount = desc->num_mipmaps;
wgpu_tex_desc->sampleCount = 1;
}
_SOKOL_PRIVATE sg_resource_state _sg_wgpu_create_image(_sg_image_t* img, const sg_image_desc* desc) {
SOKOL_ASSERT(img && desc);
SOKOL_ASSERT(_sg.wgpu.dev);
SOKOL_ASSERT(_sg.wgpu.staging_cmd_enc);
_sg_image_common_init(&img->cmn, desc);
const bool injected = (0 != desc->wgpu_texture);
const bool is_msaa = desc->sample_count > 1;
WGPUTextureDescriptor wgpu_tex_desc;
memset(&wgpu_tex_desc, 0, sizeof(wgpu_tex_desc));
_sg_wgpu_init_texdesc_common(&wgpu_tex_desc, desc);
if (_sg_is_valid_rendertarget_depth_format(img->cmn.pixel_format)) {
SOKOL_ASSERT(img->cmn.render_target);
SOKOL_ASSERT(img->cmn.type == SG_IMAGETYPE_2D);
SOKOL_ASSERT(img->cmn.num_mipmaps == 1);
SOKOL_ASSERT(!injected);
/* NOTE: a depth-stencil texture will never be MSAA-resolved, so there
won't be a separate MSAA- and resolve-texture
*/
wgpu_tex_desc.usage = WGPUTextureUsage_OutputAttachment;
wgpu_tex_desc.sampleCount = desc->sample_count;
img->wgpu.tex = wgpuDeviceCreateTexture(_sg.wgpu.dev, &wgpu_tex_desc);
SOKOL_ASSERT(img->wgpu.tex);
}
else {
if (injected) {
img->wgpu.tex = (WGPUTexture) desc->wgpu_texture;
wgpuTextureReference(img->wgpu.tex);
}
else {
/* NOTE: in the MSAA-rendertarget case, both the MSAA texture *and*
the resolve texture need OutputAttachment usage
*/
if (img->cmn.render_target) {
wgpu_tex_desc.usage = WGPUTextureUsage_Sampled|WGPUTextureUsage_OutputAttachment;
}
img->wgpu.tex = wgpuDeviceCreateTexture(_sg.wgpu.dev, &wgpu_tex_desc);
SOKOL_ASSERT(img->wgpu.tex);
/* copy content into texture via a throw-away staging buffer */
if (desc->usage == SG_USAGE_IMMUTABLE && !desc->render_target) {
WGPUBufferDescriptor wgpu_buf_desc;
memset(&wgpu_buf_desc, 0, sizeof(wgpu_buf_desc));
wgpu_buf_desc.size = _sg_wgpu_image_data_buffer_size(img);
wgpu_buf_desc.usage = WGPUBufferUsage_CopySrc|WGPUBufferUsage_CopyDst;
WGPUCreateBufferMappedResult map = wgpuDeviceCreateBufferMapped(_sg.wgpu.dev, &wgpu_buf_desc);
SOKOL_ASSERT(map.buffer && map.data);
uint32_t num_bytes = _sg_wgpu_copy_image_data(map.buffer, (uint8_t*)map.data, 0, img, &desc->data);
_SOKOL_UNUSED(num_bytes);
SOKOL_ASSERT(num_bytes == wgpu_buf_desc.size);
wgpuBufferUnmap(map.buffer);
wgpuBufferRelease(map.buffer);
}
}
/* create texture view object */
WGPUTextureViewDescriptor wgpu_view_desc;
memset(&wgpu_view_desc, 0, sizeof(wgpu_view_desc));
wgpu_view_desc.dimension = _sg_wgpu_tex_viewdim(desc->type);
img->wgpu.tex_view = wgpuTextureCreateView(img->wgpu.tex, &wgpu_view_desc);
/* if render target and MSAA, then a separate texture in MSAA format is needed
which will be resolved into the regular texture at the end of the
offscreen-render pass
*/
if (desc->render_target && is_msaa) {
wgpu_tex_desc.dimension = WGPUTextureDimension_2D;
wgpu_tex_desc.size.depth = 1;
wgpu_tex_desc.arrayLayerCount = 1;
wgpu_tex_desc.mipLevelCount = 1;
wgpu_tex_desc.usage = WGPUTextureUsage_OutputAttachment;
wgpu_tex_desc.sampleCount = desc->sample_count;
img->wgpu.msaa_tex = wgpuDeviceCreateTexture(_sg.wgpu.dev, &wgpu_tex_desc);
SOKOL_ASSERT(img->wgpu.msaa_tex);
}
/* create sampler via shared-sampler-cache */
img->wgpu.sampler = _sg_wgpu_create_sampler(desc);
SOKOL_ASSERT(img->wgpu.sampler);
}
return SG_RESOURCESTATE_VALID;
}
_SOKOL_PRIVATE void _sg_wgpu_destroy_image(_sg_image_t* img) {
SOKOL_ASSERT(img);
if (img->wgpu.tex) {
wgpuTextureRelease(img->wgpu.tex);
img->wgpu.tex = 0;
}
if (img->wgpu.tex_view) {
wgpuTextureViewRelease(img->wgpu.tex_view);
img->wgpu.tex_view = 0;
}
if (img->wgpu.msaa_tex) {
wgpuTextureRelease(img->wgpu.msaa_tex);
img->wgpu.msaa_tex = 0;
}
/* NOTE: do *not* destroy the sampler from the shared-sampler-cache */
img->wgpu.sampler = 0;
}
/*
How BindGroups work in WebGPU:
- up to 4 bind groups can be bound simultaneously
- up to 16 bindings per bind group
- 'binding' slots are local per bind group
- in the shader:
layout(set=0, binding=1) corresponds to bind group 0, binding 1
Now how to map this to sokol-gfx's bind model:
Reduce SG_MAX_SHADERSTAGE_IMAGES to 8, then:
1 bind group for all 8 uniform buffers
1 bind group for vertex shader textures + samplers
1 bind group for fragment shader textures + samples
Alternatively:
1 bind group for 8 uniform buffer slots
1 bind group for 8 vs images + 8 vs samplers
1 bind group for 12 fs images
1 bind group for 12 fs samplers
I guess this means that we need to create BindGroups on the
fly during sg_apply_bindings() :/
*/
_SOKOL_PRIVATE sg_resource_state _sg_wgpu_create_shader(_sg_shader_t* shd, const sg_shader_desc* desc) {
SOKOL_ASSERT(shd && desc);
SOKOL_ASSERT(desc->vs.bytecode.ptr && desc->fs.bytecode.ptr);
_sg_shader_common_init(&shd->cmn, desc);
bool success = true;
for (int stage_index = 0; stage_index < SG_NUM_SHADER_STAGES; stage_index++) {
const sg_shader_stage_desc* stage_desc = (stage_index == SG_SHADERSTAGE_VS) ? &desc->vs : &desc->fs;
SOKOL_ASSERT((stage_desc->bytecode.size & 3) == 0);
_sg_shader_stage_t* cmn_stage = &shd->cmn.stage[stage_index];
_sg_wgpu_shader_stage_t* wgpu_stage = &shd->wgpu.stage[stage_index];
_sg_strcpy(&wgpu_stage->entry, stage_desc->entry);
WGPUShaderModuleDescriptor wgpu_shdmod_desc;
memset(&wgpu_shdmod_desc, 0, sizeof(wgpu_shdmod_desc));
wgpu_shdmod_desc.codeSize = stage_desc->bytecode.size >> 2;
wgpu_shdmod_desc.code = (const uint32_t*) stage_desc->bytecode.ptr;
wgpu_stage->module = wgpuDeviceCreateShaderModule(_sg.wgpu.dev, &wgpu_shdmod_desc);
if (0 == wgpu_stage->module) {
success = false;
}
/* create image/sampler bind group for the shader stage */
WGPUShaderStage vis = (stage_index == SG_SHADERSTAGE_VS) ? WGPUShaderStage_Vertex : WGPUShaderStage_Fragment;
int num_imgs = cmn_stage->num_images;
if (num_imgs > _SG_WGPU_MAX_SHADERSTAGE_IMAGES) {
num_imgs = _SG_WGPU_MAX_SHADERSTAGE_IMAGES;
}
WGPUBindGroupLayoutBinding bglb_desc[_SG_WGPU_MAX_SHADERSTAGE_IMAGES * 2];
memset(bglb_desc, 0, sizeof(bglb_desc));
for (int img_index = 0; img_index < num_imgs; img_index++) {
/* texture- and sampler-bindings */
WGPUBindGroupLayoutBinding* tex_desc = &bglb_desc[img_index*2 + 0];
WGPUBindGroupLayoutBinding* smp_desc = &bglb_desc[img_index*2 + 1];
tex_desc->binding = img_index;
tex_desc->visibility = vis;
tex_desc->type = WGPUBindingType_SampledTexture;
tex_desc->textureDimension = _sg_wgpu_tex_viewdim(cmn_stage->images[img_index].image_type);
tex_desc->textureComponentType = _sg_wgpu_tex_comptype(cmn_stage->images[img_index].sampler_type);
smp_desc->binding = img_index + _SG_WGPU_MAX_SHADERSTAGE_IMAGES;
smp_desc->visibility = vis;
smp_desc->type = WGPUBindingType_Sampler;
}
WGPUBindGroupLayoutDescriptor img_bgl_desc;
memset(&img_bgl_desc, 0, sizeof(img_bgl_desc));
img_bgl_desc.bindingCount = num_imgs * 2;
img_bgl_desc.bindings = &bglb_desc[0];
wgpu_stage->bind_group_layout = wgpuDeviceCreateBindGroupLayout(_sg.wgpu.dev, &img_bgl_desc);
SOKOL_ASSERT(wgpu_stage->bind_group_layout);
}
return success ? SG_RESOURCESTATE_VALID : SG_RESOURCESTATE_FAILED;
}
_SOKOL_PRIVATE void _sg_wgpu_destroy_shader(_sg_shader_t* shd) {
SOKOL_ASSERT(shd);
for (int stage_index = 0; stage_index < SG_NUM_SHADER_STAGES; stage_index++) {
_sg_wgpu_shader_stage_t* wgpu_stage = &shd->wgpu.stage[stage_index];
if (wgpu_stage->module) {
wgpuShaderModuleRelease(wgpu_stage->module);
wgpu_stage->module = 0;
}
if (wgpu_stage->bind_group_layout) {
wgpuBindGroupLayoutRelease(wgpu_stage->bind_group_layout);
wgpu_stage->bind_group_layout = 0;
}
}
}
_SOKOL_PRIVATE sg_resource_state _sg_wgpu_create_pipeline(_sg_pipeline_t* pip, _sg_shader_t* shd, const sg_pipeline_desc* desc) {
SOKOL_ASSERT(pip && shd && desc);
SOKOL_ASSERT(desc->shader.id == shd->slot.id);
SOKOL_ASSERT(shd->wgpu.stage[SG_SHADERSTAGE_VS].bind_group_layout);
SOKOL_ASSERT(shd->wgpu.stage[SG_SHADERSTAGE_FS].bind_group_layout);
pip->shader = shd;
_sg_pipeline_common_init(&pip->cmn, desc);
pip->wgpu.stencil_ref = (uint32_t) desc->stencil.ref;
WGPUBindGroupLayout pip_bgl[3] = {
_sg.wgpu.ub.bindgroup_layout,
shd->wgpu.stage[SG_SHADERSTAGE_VS].bind_group_layout,
shd->wgpu.stage[SG_SHADERSTAGE_FS].bind_group_layout
};
WGPUPipelineLayoutDescriptor pl_desc;
memset(&pl_desc, 0, sizeof(pl_desc));
pl_desc.bindGroupLayoutCount = 3;
pl_desc.bindGroupLayouts = &pip_bgl[0];
WGPUPipelineLayout pip_layout = wgpuDeviceCreatePipelineLayout(_sg.wgpu.dev, &pl_desc);
WGPUVertexBufferLayoutDescriptor vb_desc[SG_MAX_SHADERSTAGE_BUFFERS];
memset(&vb_desc, 0, sizeof(vb_desc));
WGPUVertexAttributeDescriptor va_desc[SG_MAX_SHADERSTAGE_BUFFERS][SG_MAX_VERTEX_ATTRIBUTES];
memset(&va_desc, 0, sizeof(va_desc));
int vb_idx = 0;
for (; vb_idx < SG_MAX_SHADERSTAGE_BUFFERS; vb_idx++) {
const sg_buffer_layout_desc* src_vb_desc = &desc->layout.buffers[vb_idx];
if (0 == src_vb_desc->stride) {
break;
}
vb_desc[vb_idx].arrayStride = src_vb_desc->stride;
vb_desc[vb_idx].stepMode = _sg_wgpu_stepmode(src_vb_desc->step_func);
/* NOTE: WebGPU has no support for vertex step rate (because that's
not supported by Core Vulkan
*/
int va_idx = 0;
for (int va_loc = 0; va_loc < SG_MAX_VERTEX_ATTRIBUTES; va_loc++) {
const sg_vertex_attr_desc* src_va_desc = &desc->layout.attrs[va_loc];
if (SG_VERTEXFORMAT_INVALID == src_va_desc->format) {
break;
}
pip->cmn.vertex_layout_valid[src_va_desc->buffer_index] = true;
if (vb_idx == src_va_desc->buffer_index) {
va_desc[vb_idx][va_idx].format = _sg_wgpu_vertexformat(src_va_desc->format);
va_desc[vb_idx][va_idx].offset = src_va_desc->offset;
va_desc[vb_idx][va_idx].shaderLocation = va_loc;
va_idx++;
}
}
vb_desc[vb_idx].attributeCount = va_idx;
vb_desc[vb_idx].attributes = &va_desc[vb_idx][0];
}
WGPUVertexStateDescriptor vx_state_desc;
memset(&vx_state_desc, 0, sizeof(vx_state_desc));
vx_state_desc.indexFormat = _sg_wgpu_indexformat(desc->index_type);
vx_state_desc.vertexBufferCount = vb_idx;
vx_state_desc.vertexBuffers = vb_desc;
WGPURasterizationStateDescriptor rs_desc;
memset(&rs_desc, 0, sizeof(rs_desc));
rs_desc.frontFace = _sg_wgpu_frontface(desc->face_winding);
rs_desc.cullMode = _sg_wgpu_cullmode(desc->cull_mode);
rs_desc.depthBias = (int32_t) desc->depth.bias;
rs_desc.depthBiasClamp = desc->depth.bias_clamp;
rs_desc.depthBiasSlopeScale = desc->depth.bias_slope_scale;
WGPUDepthStencilStateDescriptor ds_desc;
memset(&ds_desc, 0, sizeof(ds_desc));
ds_desc.format = _sg_wgpu_textureformat(desc->depth.pixel_format);
ds_desc.depthWriteEnabled = desc->depth.write_enabled;
ds_desc.depthCompare = _sg_wgpu_comparefunc(desc->depth.compare);
ds_desc.stencilReadMask = desc->stencil.read_mask;
ds_desc.stencilWriteMask = desc->stencil.write_mask;
ds_desc.stencilFront.compare = _sg_wgpu_comparefunc(desc->stencil.front.compare);
ds_desc.stencilFront.failOp = _sg_wgpu_stencilop(desc->stencil.front.fail_op);
ds_desc.stencilFront.depthFailOp = _sg_wgpu_stencilop(desc->stencil.front.depth_fail_op);
ds_desc.stencilFront.passOp = _sg_wgpu_stencilop(desc->stencil.front.pass_op);
ds_desc.stencilBack.compare = _sg_wgpu_comparefunc(desc->stencil.back.compare);
ds_desc.stencilBack.failOp = _sg_wgpu_stencilop(desc->stencil.back.fail_op);
ds_desc.stencilBack.depthFailOp = _sg_wgpu_stencilop(desc->stencil.back.depth_fail_op);
ds_desc.stencilBack.passOp = _sg_wgpu_stencilop(desc->stencil.back.pass_op);
WGPUProgrammableStageDescriptor fs_desc;
memset(&fs_desc, 0, sizeof(fs_desc));
fs_desc.module = shd->wgpu.stage[SG_SHADERSTAGE_FS].module;
fs_desc.entryPoint = shd->wgpu.stage[SG_SHADERSTAGE_VS].entry.buf;
WGPUColorStateDescriptor cs_desc[SG_MAX_COLOR_ATTACHMENTS];
memset(cs_desc, 0, sizeof(cs_desc));
for (uint32_t i = 0; i < desc->color_count; i++) {
SOKOL_ASSERT(i < SG_MAX_COLOR_ATTACHMENTS);
cs_desc[i].format = _sg_wgpu_textureformat(desc->colors[i].pixel_format);
cs_desc[i].colorBlend.operation = _sg_wgpu_blendop(desc->colors[i].blend.op_rgb);
cs_desc[i].colorBlend.srcFactor = _sg_wgpu_blendfactor(desc->colors[i].blend.src_factor_rgb);
cs_desc[i].colorBlend.dstFactor = _sg_wgpu_blendfactor(desc->colors[i].blend.dst_factor_rgb);
cs_desc[i].alphaBlend.operation = _sg_wgpu_blendop(desc->colors[i].blend.op_alpha);
cs_desc[i].alphaBlend.srcFactor = _sg_wgpu_blendfactor(desc->colors[i].blend.src_factor_alpha);
cs_desc[i].alphaBlend.dstFactor = _sg_wgpu_blendfactor(desc->colors[i].blend.dst_factor_alpha);
cs_desc[i].writeMask = _sg_wgpu_colorwritemask(desc->colors[i].write_mask);
}
WGPURenderPipelineDescriptor pip_desc;
memset(&pip_desc, 0, sizeof(pip_desc));
pip_desc.layout = pip_layout;
pip_desc.vertexStage.module = shd->wgpu.stage[SG_SHADERSTAGE_VS].module;
pip_desc.vertexStage.entryPoint = shd->wgpu.stage[SG_SHADERSTAGE_VS].entry.buf;
pip_desc.fragmentStage = &fs_desc;
pip_desc.vertexState = &vx_state_desc;
pip_desc.primitiveTopology = _sg_wgpu_topology(desc->primitive_type);
pip_desc.rasterizationState = &rs_desc;
pip_desc.sampleCount = desc->sample_count;
if (SG_PIXELFORMAT_NONE != desc->depth.pixel_format) {
pip_desc.depthStencilState = &ds_desc;
}
pip_desc.colorStateCount = desc->color_count;
pip_desc.colorStates = cs_desc;
pip_desc.sampleMask = 0xFFFFFFFF; /* FIXME: ??? */
pip->wgpu.pip = wgpuDeviceCreateRenderPipeline(_sg.wgpu.dev, &pip_desc);
SOKOL_ASSERT(0 != pip->wgpu.pip);
wgpuPipelineLayoutRelease(pip_layout);
return SG_RESOURCESTATE_VALID;
}
_SOKOL_PRIVATE void _sg_wgpu_destroy_pipeline(_sg_pipeline_t* pip) {
SOKOL_ASSERT(pip);
if (pip->wgpu.pip) {
wgpuRenderPipelineRelease(pip->wgpu.pip);
pip->wgpu.pip = 0;
}
}
_SOKOL_PRIVATE sg_resource_state _sg_wgpu_create_pass(_sg_pass_t* pass, _sg_image_t** att_images, const sg_pass_desc* desc) {
SOKOL_ASSERT(pass && desc);
SOKOL_ASSERT(att_images && att_images[0]);
_sg_pass_common_init(&pass->cmn, desc);
/* copy image pointers and create render-texture views */
const sg_pass_attachment_desc* att_desc;
for (uint32_t i = 0; i < pass->cmn.num_color_atts; i++) {
att_desc = &desc->color_attachments[i];
if (att_desc->image.id != SG_INVALID_ID) {
SOKOL_ASSERT(att_desc->image.id != SG_INVALID_ID);
SOKOL_ASSERT(0 == pass->wgpu.color_atts[i].image);
_sg_image_t* img = att_images[i];
SOKOL_ASSERT(img && (img->slot.id == att_desc->image.id));
SOKOL_ASSERT(_sg_is_valid_rendertarget_color_format(img->cmn.pixel_format));
pass->wgpu.color_atts[i].image = img;
/* create a render-texture-view to render into the right sub-surface */
const bool is_msaa = img->cmn.sample_count > 1;
WGPUTextureViewDescriptor view_desc;
memset(&view_desc, 0, sizeof(view_desc));
view_desc.baseMipLevel = is_msaa ? 0 : att_desc->mip_level;
view_desc.mipLevelCount = 1;
view_desc.baseArrayLayer = is_msaa ? 0 : att_desc->slice;
view_desc.arrayLayerCount = 1;
WGPUTexture wgpu_tex = is_msaa ? img->wgpu.msaa_tex : img->wgpu.tex;
SOKOL_ASSERT(wgpu_tex);
pass->wgpu.color_atts[i].render_tex_view = wgpuTextureCreateView(wgpu_tex, &view_desc);
SOKOL_ASSERT(pass->wgpu.color_atts[i].render_tex_view);
/* ... and if needed a separate resolve texture view */
if (is_msaa) {
view_desc.baseMipLevel = att_desc->mip_level;
view_desc.baseArrayLayer = att_desc->slice;
WGPUTexture wgpu_tex = img->wgpu.tex;
pass->wgpu.color_atts[i].resolve_tex_view = wgpuTextureCreateView(wgpu_tex, &view_desc);
SOKOL_ASSERT(pass->wgpu.color_atts[i].resolve_tex_view);
}
}
}
SOKOL_ASSERT(0 == pass->wgpu.ds_att.image);
att_desc = &desc->depth_stencil_attachment;
if (att_desc->image.id != SG_INVALID_ID) {
const int ds_img_index = SG_MAX_COLOR_ATTACHMENTS;
SOKOL_ASSERT(att_images[ds_img_index] && (att_images[ds_img_index]->slot.id == att_desc->image.id));
SOKOL_ASSERT(_sg_is_valid_rendertarget_depth_format(att_images[ds_img_index]->cmn.pixel_format));
_sg_image_t* ds_img = att_images[ds_img_index];
pass->wgpu.ds_att.image = ds_img;
/* create a render-texture view */
SOKOL_ASSERT(0 == att_desc->mip_level);
SOKOL_ASSERT(0 == att_desc->slice);
WGPUTextureViewDescriptor view_desc;
memset(&view_desc, 0, sizeof(view_desc));
WGPUTexture wgpu_tex = ds_img->wgpu.tex;
SOKOL_ASSERT(wgpu_tex);
pass->wgpu.ds_att.render_tex_view = wgpuTextureCreateView(wgpu_tex, &view_desc);
SOKOL_ASSERT(pass->wgpu.ds_att.render_tex_view);
}
return SG_RESOURCESTATE_VALID;
}
_SOKOL_PRIVATE void _sg_wgpu_destroy_pass(_sg_pass_t* pass) {
SOKOL_ASSERT(pass);
for (uint32_t i = 0; i < pass->cmn.num_color_atts; i++) {
if (pass->wgpu.color_atts[i].render_tex_view) {
wgpuTextureViewRelease(pass->wgpu.color_atts[i].render_tex_view);
pass->wgpu.color_atts[i].render_tex_view = 0;
}
if (pass->wgpu.color_atts[i].resolve_tex_view) {
wgpuTextureViewRelease(pass->wgpu.color_atts[i].resolve_tex_view);
pass->wgpu.color_atts[i].resolve_tex_view = 0;
}
}
if (pass->wgpu.ds_att.render_tex_view) {
wgpuTextureViewRelease(pass->wgpu.ds_att.render_tex_view);
pass->wgpu.ds_att.render_tex_view = 0;
}
}
_SOKOL_PRIVATE _sg_image_t* _sg_wgpu_pass_color_image(const _sg_pass_t* pass, int index) {
SOKOL_ASSERT(pass && (index >= 0) && (index < SG_MAX_COLOR_ATTACHMENTS));
/* NOTE: may return null */
return pass->wgpu.color_atts[index].image;
}
_SOKOL_PRIVATE _sg_image_t* _sg_wgpu_pass_ds_image(const _sg_pass_t* pass) {
/* NOTE: may return null */
SOKOL_ASSERT(pass);
return pass->wgpu.ds_att.image;
}
_SOKOL_PRIVATE void _sg_wgpu_begin_pass(_sg_pass_t* pass, const sg_pass_action* action, int w, int h) {
SOKOL_ASSERT(action);
SOKOL_ASSERT(!_sg.wgpu.in_pass);
SOKOL_ASSERT(_sg.wgpu.render_cmd_enc);
SOKOL_ASSERT(_sg.wgpu.dev);
SOKOL_ASSERT(_sg.wgpu.render_view_cb || _sg.wgpu.render_view_userdata_cb);
SOKOL_ASSERT(_sg.wgpu.resolve_view_cb || _sg.wgpu.resolve_view_userdata_cb);
SOKOL_ASSERT(_sg.wgpu.depth_stencil_view_cb || _sg.wgpu.depth_stencil_view_userdata_cb);
_sg.wgpu.in_pass = true;
_sg.wgpu.cur_width = w;
_sg.wgpu.cur_height = h;
_sg.wgpu.cur_pipeline = 0;
_sg.wgpu.cur_pipeline_id.id = SG_INVALID_ID;
SOKOL_ASSERT(_sg.wgpu.render_cmd_enc);
if (pass) {
WGPURenderPassDescriptor wgpu_pass_desc;
memset(&wgpu_pass_desc, 0, sizeof(wgpu_pass_desc));
WGPURenderPassColorAttachmentDescriptor wgpu_color_att_desc[SG_MAX_COLOR_ATTACHMENTS];
memset(&wgpu_color_att_desc, 0, sizeof(wgpu_color_att_desc));
SOKOL_ASSERT(pass->slot.state == SG_RESOURCESTATE_VALID);
for (uint32_t i = 0; i < pass->cmn.num_color_atts; i++) {
const _sg_wgpu_attachment_t* wgpu_att = &pass->wgpu.color_atts[i];
wgpu_color_att_desc[i].loadOp = _sg_wgpu_load_op(action->colors[i].action);
wgpu_color_att_desc[i].storeOp = WGPUStoreOp_Store;
wgpu_color_att_desc[i].clearColor.r = action->colors[i].value.r;
wgpu_color_att_desc[i].clearColor.g = action->colors[i].value.g;
wgpu_color_att_desc[i].clearColor.b = action->colors[i].value.b;
wgpu_color_att_desc[i].clearColor.a = action->colors[i].value.a;
wgpu_color_att_desc[i].attachment = wgpu_att->render_tex_view;
if (wgpu_att->image->cmn.sample_count > 1) {
wgpu_color_att_desc[i].resolveTarget = wgpu_att->resolve_tex_view;
}
}
wgpu_pass_desc.colorAttachmentCount = pass->cmn.num_color_atts;
wgpu_pass_desc.colorAttachments = &wgpu_color_att_desc[0];
if (pass->wgpu.ds_att.image) {
WGPURenderPassDepthStencilAttachmentDescriptor wgpu_ds_att_desc;
memset(&wgpu_ds_att_desc, 0, sizeof(wgpu_ds_att_desc));
wgpu_ds_att_desc.depthLoadOp = _sg_wgpu_load_op(action->depth.action);
wgpu_ds_att_desc.clearDepth = action->depth.value;
wgpu_ds_att_desc.stencilLoadOp = _sg_wgpu_load_op(action->stencil.action);
wgpu_ds_att_desc.clearStencil = action->stencil.value;
wgpu_ds_att_desc.attachment = pass->wgpu.ds_att.render_tex_view;
wgpu_pass_desc.depthStencilAttachment = &wgpu_ds_att_desc;
_sg.wgpu.pass_enc = wgpuCommandEncoderBeginRenderPass(_sg.wgpu.render_cmd_enc, &wgpu_pass_desc);
}
}
else {
/* default render pass */
WGPUTextureView wgpu_render_view = _sg.wgpu.render_view_cb ? _sg.wgpu.render_view_cb() : _sg.wgpu.render_view_userdata_cb(_sg.wgpu.user_data);
WGPUTextureView wgpu_resolve_view = _sg.wgpu.resolve_view_cb ? _sg.wgpu.resolve_view_cb() : _sg.wgpu.resolve_view_userdata_cb(_sg.wgpu.user_data);
WGPUTextureView wgpu_depth_stencil_view = _sg.wgpu.depth_stencil_view_cb ? _sg.wgpu.depth_stencil_view_cb() : _sg.wgpu.depth_stencil_view_userdata_cb(_sg.wgpu.user_data);
WGPURenderPassDescriptor pass_desc;
memset(&pass_desc, 0, sizeof(pass_desc));
WGPURenderPassColorAttachmentDescriptor color_att_desc;
memset(&color_att_desc, 0, sizeof(color_att_desc));
color_att_desc.loadOp = _sg_wgpu_load_op(action->colors[0].action);
color_att_desc.clearColor.r = action->colors[0].value.r;
color_att_desc.clearColor.g = action->colors[0].value.g;
color_att_desc.clearColor.b = action->colors[0].value.b;
color_att_desc.clearColor.a = action->colors[0].value.a;
color_att_desc.attachment = wgpu_render_view;
color_att_desc.resolveTarget = wgpu_resolve_view; /* null if no MSAA rendering */
pass_desc.colorAttachmentCount = 1;
pass_desc.colorAttachments = &color_att_desc;
WGPURenderPassDepthStencilAttachmentDescriptor ds_att_desc;
memset(&ds_att_desc, 0, sizeof(ds_att_desc));
ds_att_desc.attachment = wgpu_depth_stencil_view;
SOKOL_ASSERT(0 != ds_att_desc.attachment);
ds_att_desc.depthLoadOp = _sg_wgpu_load_op(action->depth.action);
ds_att_desc.clearDepth = action->depth.value;
ds_att_desc.stencilLoadOp = _sg_wgpu_load_op(action->stencil.action);
ds_att_desc.clearStencil = action->stencil.value;
pass_desc.depthStencilAttachment = &ds_att_desc;
_sg.wgpu.pass_enc = wgpuCommandEncoderBeginRenderPass(_sg.wgpu.render_cmd_enc, &pass_desc);
}
SOKOL_ASSERT(_sg.wgpu.pass_enc);
/* initial uniform buffer binding (required even if no uniforms are set in the frame) */
wgpuRenderPassEncoderSetBindGroup(_sg.wgpu.pass_enc,
0, /* groupIndex 0 is reserved for uniform buffers */
_sg.wgpu.ub.bindgroup,
SG_NUM_SHADER_STAGES * SG_MAX_SHADERSTAGE_UBS,
&_sg.wgpu.ub.bind_offsets[0][0]);
}
_SOKOL_PRIVATE void _sg_wgpu_end_pass(void) {
SOKOL_ASSERT(_sg.wgpu.in_pass);
SOKOL_ASSERT(_sg.wgpu.pass_enc);
_sg.wgpu.in_pass = false;
wgpuRenderPassEncoderEndPass(_sg.wgpu.pass_enc);
wgpuRenderPassEncoderRelease(_sg.wgpu.pass_enc);
_sg.wgpu.pass_enc = 0;
}
_SOKOL_PRIVATE void _sg_wgpu_commit(void) {
SOKOL_ASSERT(!_sg.wgpu.in_pass);
SOKOL_ASSERT(_sg.wgpu.queue);
SOKOL_ASSERT(_sg.wgpu.render_cmd_enc);
SOKOL_ASSERT(_sg.wgpu.staging_cmd_enc);
/* finish and submit this frame's work */
_sg_wgpu_ubpool_flush();
_sg_wgpu_staging_unmap();
WGPUCommandBuffer cmd_bufs[2];
WGPUCommandBufferDescriptor cmd_buf_desc;
memset(&cmd_buf_desc, 0, sizeof(cmd_buf_desc));
cmd_bufs[0] = wgpuCommandEncoderFinish(_sg.wgpu.staging_cmd_enc, &cmd_buf_desc);
SOKOL_ASSERT(cmd_bufs[0]);
wgpuCommandEncoderRelease(_sg.wgpu.staging_cmd_enc);
_sg.wgpu.staging_cmd_enc = 0;
cmd_bufs[1] = wgpuCommandEncoderFinish(_sg.wgpu.render_cmd_enc, &cmd_buf_desc);
SOKOL_ASSERT(cmd_bufs[1]);
wgpuCommandEncoderRelease(_sg.wgpu.render_cmd_enc);
_sg.wgpu.render_cmd_enc = 0;
wgpuQueueSubmit(_sg.wgpu.queue, 2, &cmd_bufs[0]);
wgpuCommandBufferRelease(cmd_bufs[0]);
wgpuCommandBufferRelease(cmd_bufs[1]);
/* create a new render- and staging-command-encoders for next frame */
WGPUCommandEncoderDescriptor cmd_enc_desc;
memset(&cmd_enc_desc, 0, sizeof(cmd_enc_desc));
_sg.wgpu.staging_cmd_enc = wgpuDeviceCreateCommandEncoder(_sg.wgpu.dev, &cmd_enc_desc);
_sg.wgpu.render_cmd_enc = wgpuDeviceCreateCommandEncoder(_sg.wgpu.dev, &cmd_enc_desc);
/* grab new staging buffers for uniform- and vertex/image-updates */
_sg_wgpu_ubpool_next_frame(false);
_sg_wgpu_staging_next_frame(false);
}
_SOKOL_PRIVATE void _sg_wgpu_apply_viewport(int x, int y, int w, int h, bool origin_top_left) {
SOKOL_ASSERT(_sg.wgpu.in_pass);
SOKOL_ASSERT(_sg.wgpu.pass_enc);
float xf = (float) x;
float yf = (float) (origin_top_left ? y : (_sg.wgpu.cur_height - (y + h)));
float wf = (float) w;
float hf = (float) h;
wgpuRenderPassEncoderSetViewport(_sg.wgpu.pass_enc, xf, yf, wf, hf, 0.0f, 1.0f);
}
_SOKOL_PRIVATE void _sg_wgpu_apply_scissor_rect(int x, int y, int w, int h, bool origin_top_left) {
SOKOL_ASSERT(_sg.wgpu.in_pass);
SOKOL_ASSERT(_sg.wgpu.pass_enc);
SOKOL_ASSERT(_sg.wgpu.in_pass);
SOKOL_ASSERT(_sg.wgpu.pass_enc);
/* clip against framebuffer rect */
x = _sg_min(_sg_max(0, x), _sg.wgpu.cur_width-1);
y = _sg_min(_sg_max(0, y), _sg.wgpu.cur_height-1);
if ((x + w) > _sg.wgpu.cur_width) {
w = _sg.wgpu.cur_width - x;
}
if ((y + h) > _sg.wgpu.cur_height) {
h = _sg.wgpu.cur_height - y;
}
w = _sg_max(w, 1);
h = _sg_max(h, 1);
uint32_t sx = (uint32_t) x;
uint32_t sy = origin_top_left ? y : (_sg.wgpu.cur_height - (y + h));
uint32_t sw = w;
uint32_t sh = h;
wgpuRenderPassEncoderSetScissorRect(_sg.wgpu.pass_enc, sx, sy, sw, sh);
}
_SOKOL_PRIVATE void _sg_wgpu_apply_pipeline(_sg_pipeline_t* pip) {
SOKOL_ASSERT(pip);
SOKOL_ASSERT(pip->wgpu.pip);
SOKOL_ASSERT(_sg.wgpu.in_pass);
SOKOL_ASSERT(_sg.wgpu.pass_enc);
_sg.wgpu.draw_indexed = (pip->cmn.index_type != SG_INDEXTYPE_NONE);
_sg.wgpu.cur_pipeline = pip;
_sg.wgpu.cur_pipeline_id.id = pip->slot.id;
wgpuRenderPassEncoderSetPipeline(_sg.wgpu.pass_enc, pip->wgpu.pip);
wgpuRenderPassEncoderSetBlendColor(_sg.wgpu.pass_enc, (WGPUColor*)&pip->cmn.blend_color);
wgpuRenderPassEncoderSetStencilReference(_sg.wgpu.pass_enc, pip->wgpu.stencil_ref);
}
_SOKOL_PRIVATE WGPUBindGroup _sg_wgpu_create_images_bindgroup(WGPUBindGroupLayout bgl, _sg_image_t** imgs, int num_imgs) {
SOKOL_ASSERT(_sg.wgpu.dev);
SOKOL_ASSERT(num_imgs <= _SG_WGPU_MAX_SHADERSTAGE_IMAGES);
WGPUBindGroupBinding img_bgb[_SG_WGPU_MAX_SHADERSTAGE_IMAGES * 2];
memset(&img_bgb, 0, sizeof(img_bgb));
for (int img_index = 0; img_index < num_imgs; img_index++) {
WGPUBindGroupBinding* tex_bdg = &img_bgb[img_index*2 + 0];
WGPUBindGroupBinding* smp_bdg = &img_bgb[img_index*2 + 1];
tex_bdg->binding = img_index;
tex_bdg->textureView = imgs[img_index]->wgpu.tex_view;
smp_bdg->binding = img_index + _SG_WGPU_MAX_SHADERSTAGE_IMAGES;
smp_bdg->sampler = imgs[img_index]->wgpu.sampler;
}
WGPUBindGroupDescriptor bg_desc;
memset(&bg_desc, 0, sizeof(bg_desc));
bg_desc.layout = bgl;
bg_desc.bindingCount = 2 * num_imgs;
bg_desc.bindings = &img_bgb[0];
WGPUBindGroup bg = wgpuDeviceCreateBindGroup(_sg.wgpu.dev, &bg_desc);
SOKOL_ASSERT(bg);
return bg;
}
_SOKOL_PRIVATE void _sg_wgpu_apply_bindings(
_sg_pipeline_t* pip,
_sg_buffer_t** vbs, const int* vb_offsets, int num_vbs,
_sg_buffer_t* ib, int ib_offset,
_sg_image_t** vs_imgs, int num_vs_imgs,
_sg_image_t** fs_imgs, int num_fs_imgs)
{
SOKOL_ASSERT(_sg.wgpu.in_pass);
SOKOL_ASSERT(_sg.wgpu.pass_enc);
SOKOL_ASSERT(pip->shader && (pip->cmn.shader_id.id == pip->shader->slot.id));
/* index buffer */
if (ib) {
wgpuRenderPassEncoderSetIndexBuffer(_sg.wgpu.pass_enc, ib->wgpu.buf, ib_offset);
}
/* vertex buffers */
for (uint32_t slot = 0; slot < (uint32_t)num_vbs; slot++) {
wgpuRenderPassEncoderSetVertexBuffer(_sg.wgpu.pass_enc, slot, vbs[slot]->wgpu.buf, (uint64_t)vb_offsets[slot]);
}
/* need to create throw-away bind groups for images */
if (num_vs_imgs > 0) {
if (num_vs_imgs > _SG_WGPU_MAX_SHADERSTAGE_IMAGES) {
num_vs_imgs = _SG_WGPU_MAX_SHADERSTAGE_IMAGES;
}
WGPUBindGroupLayout vs_bgl = pip->shader->wgpu.stage[SG_SHADERSTAGE_VS].bind_group_layout;
SOKOL_ASSERT(vs_bgl);
WGPUBindGroup vs_img_bg = _sg_wgpu_create_images_bindgroup(vs_bgl, vs_imgs, num_vs_imgs);
wgpuRenderPassEncoderSetBindGroup(_sg.wgpu.pass_enc, 1, vs_img_bg, 0, 0);
wgpuBindGroupRelease(vs_img_bg);
}
else {
wgpuRenderPassEncoderSetBindGroup(_sg.wgpu.pass_enc, 1, _sg.wgpu.empty_bind_group, 0, 0);
}
if (num_fs_imgs > 0) {
if (num_fs_imgs > _SG_WGPU_MAX_SHADERSTAGE_IMAGES) {
num_fs_imgs = _SG_WGPU_MAX_SHADERSTAGE_IMAGES;
}
WGPUBindGroupLayout fs_bgl = pip->shader->wgpu.stage[SG_SHADERSTAGE_FS].bind_group_layout;
SOKOL_ASSERT(fs_bgl);
WGPUBindGroup fs_img_bg = _sg_wgpu_create_images_bindgroup(fs_bgl, fs_imgs, num_fs_imgs);
wgpuRenderPassEncoderSetBindGroup(_sg.wgpu.pass_enc, 2, fs_img_bg, 0, 0);
wgpuBindGroupRelease(fs_img_bg);
}
else {
wgpuRenderPassEncoderSetBindGroup(_sg.wgpu.pass_enc, 2, _sg.wgpu.empty_bind_group, 0, 0);
}
}
_SOKOL_PRIVATE void _sg_wgpu_apply_uniforms(sg_shader_stage stage_index, int ub_index, const sg_range* data) {
SOKOL_ASSERT(_sg.wgpu.in_pass);
SOKOL_ASSERT(_sg.wgpu.pass_enc);
SOKOL_ASSERT((_sg.wgpu.ub.offset + data->size) <= _sg.wgpu.ub.num_bytes);
SOKOL_ASSERT((_sg.wgpu.ub.offset & (_SG_WGPU_STAGING_ALIGN-1)) == 0);
SOKOL_ASSERT(_sg.wgpu.cur_pipeline && _sg.wgpu.cur_pipeline->shader);
SOKOL_ASSERT(_sg.wgpu.cur_pipeline->slot.id == _sg.wgpu.cur_pipeline_id.id);
SOKOL_ASSERT(_sg.wgpu.cur_pipeline->shader->slot.id == _sg.wgpu.cur_pipeline->cmn.shader_id.id);
SOKOL_ASSERT(ub_index < _sg.wgpu.cur_pipeline->shader->cmn.stage[stage_index].num_uniform_blocks);
SOKOL_ASSERT(data->size <= _sg.wgpu.cur_pipeline->shader->cmn.stage[stage_index].uniform_blocks[ub_index].size);
SOKOL_ASSERT(data->size <= _SG_WGPU_MAX_UNIFORM_UPDATE_SIZE);
SOKOL_ASSERT(0 != _sg.wgpu.ub.stage.ptr[_sg.wgpu.ub.stage.cur]);
uint8_t* dst_ptr = _sg.wgpu.ub.stage.ptr[_sg.wgpu.ub.stage.cur] + _sg.wgpu.ub.offset;
memcpy(dst_ptr, data->ptr, data->size);
_sg.wgpu.ub.bind_offsets[stage_index][ub_index] = _sg.wgpu.ub.offset;
wgpuRenderPassEncoderSetBindGroup(_sg.wgpu.pass_enc,
0, /* groupIndex 0 is reserved for uniform buffers */
_sg.wgpu.ub.bindgroup,
SG_NUM_SHADER_STAGES * SG_MAX_SHADERSTAGE_UBS,
&_sg.wgpu.ub.bind_offsets[0][0]);
_sg.wgpu.ub.offset = _sg_roundup(_sg.wgpu.ub.offset + data->size, _SG_WGPU_STAGING_ALIGN);
}
_SOKOL_PRIVATE void _sg_wgpu_draw(int base_element, int num_elements, int num_instances) {
SOKOL_ASSERT(_sg.wgpu.in_pass);
SOKOL_ASSERT(_sg.wgpu.pass_enc);
if (_sg.wgpu.draw_indexed) {
wgpuRenderPassEncoderDrawIndexed(_sg.wgpu.pass_enc, num_elements, num_instances, base_element, 0, 0);
}
else {
wgpuRenderPassEncoderDraw(_sg.wgpu.pass_enc, num_elements, num_instances, base_element, 0);
}
}
_SOKOL_PRIVATE void _sg_wgpu_update_buffer(_sg_buffer_t* buf, const sg_range* data) {
SOKOL_ASSERT(buf && data && data->ptr && (data->size > 0));
uint32_t copied_num_bytes = _sg_wgpu_staging_copy_to_buffer(buf->wgpu.buf, 0, data->ptr, data->size);
SOKOL_ASSERT(copied_num_bytes > 0); _SOKOL_UNUSED(copied_num_bytes);
}
_SOKOL_PRIVATE int _sg_wgpu_append_buffer(_sg_buffer_t* buf, const sg_range* data, bool new_frame) {
SOKOL_ASSERT(buf && data && data->ptr && (data->size > 0));
_SOKOL_UNUSED(new_frame);
uint32_t copied_num_bytes = _sg_wgpu_staging_copy_to_buffer(buf->wgpu.buf, buf->cmn.append_pos, data->ptr, data->size);
SOKOL_ASSERT(copied_num_bytes > 0); _SOKOL_UNUSED(copied_num_bytes);
return (int)copied_num_bytes;
}
_SOKOL_PRIVATE void _sg_wgpu_update_image(_sg_image_t* img, const sg_image_data* data) {
SOKOL_ASSERT(img && data);
bool success = _sg_wgpu_staging_copy_to_texture(img, data);
SOKOL_ASSERT(success);
_SOKOL_UNUSED(success);
}
#endif
/*== BACKEND API WRAPPERS ====================================================*/
static inline void _sg_setup_backend(const sg_desc* desc) {
#if defined(_SOKOL_ANY_GL)
_sg_gl_setup_backend(desc);
#elif defined(SOKOL_METAL)
_sg_mtl_setup_backend(desc);
#elif defined(SOKOL_D3D11)
_sg_d3d11_setup_backend(desc);
#elif defined(SOKOL_WGPU)
_sg_wgpu_setup_backend(desc);
#elif defined(SOKOL_DUMMY_BACKEND)
_sg_dummy_setup_backend(desc);
#else
#error("INVALID BACKEND");
#endif
}
static inline void _sg_discard_backend(void) {
#if defined(_SOKOL_ANY_GL)
_sg_gl_discard_backend();
#elif defined(SOKOL_METAL)
_sg_mtl_discard_backend();
#elif defined(SOKOL_D3D11)
_sg_d3d11_discard_backend();
#elif defined(SOKOL_WGPU)
_sg_wgpu_discard_backend();
#elif defined(SOKOL_DUMMY_BACKEND)
_sg_dummy_discard_backend();
#else
#error("INVALID BACKEND");
#endif
}
static inline void _sg_reset_state_cache(void) {
#if defined(_SOKOL_ANY_GL)
_sg_gl_reset_state_cache();
#elif defined(SOKOL_METAL)
_sg_mtl_reset_state_cache();
#elif defined(SOKOL_D3D11)
_sg_d3d11_reset_state_cache();
#elif defined(SOKOL_WGPU)
_sg_wgpu_reset_state_cache();
#elif defined(SOKOL_DUMMY_BACKEND)
_sg_dummy_reset_state_cache();
#else
#error("INVALID BACKEND");
#endif
}
static inline void _sg_activate_context(_sg_context_t* ctx) {
#if defined(_SOKOL_ANY_GL)
_sg_gl_activate_context(ctx);
#elif defined(SOKOL_METAL)
_sg_mtl_activate_context(ctx);
#elif defined(SOKOL_D3D11)
_sg_d3d11_activate_context(ctx);
#elif defined(SOKOL_WGPU)
_sg_wgpu_activate_context(ctx);
#elif defined(SOKOL_DUMMY_BACKEND)
_sg_dummy_activate_context(ctx);
#else
#error("INVALID BACKEND");
#endif
}
static inline sg_resource_state _sg_create_context(_sg_context_t* ctx) {
#if defined(_SOKOL_ANY_GL)
return _sg_gl_create_context(ctx);
#elif defined(SOKOL_METAL)
return _sg_mtl_create_context(ctx);
#elif defined(SOKOL_D3D11)
return _sg_d3d11_create_context(ctx);
#elif defined(SOKOL_WGPU)
return _sg_wgpu_create_context(ctx);
#elif defined(SOKOL_DUMMY_BACKEND)
return _sg_dummy_create_context(ctx);
#else
#error("INVALID BACKEND");
#endif
}
static inline void _sg_destroy_context(_sg_context_t* ctx) {
#if defined(_SOKOL_ANY_GL)
_sg_gl_destroy_context(ctx);
#elif defined(SOKOL_METAL)
_sg_mtl_destroy_context(ctx);
#elif defined(SOKOL_D3D11)
_sg_d3d11_destroy_context(ctx);
#elif defined(SOKOL_WGPU)
_sg_wgpu_destroy_context(ctx);
#elif defined(SOKOL_DUMMY_BACKEND)
_sg_dummy_destroy_context(ctx);
#else
#error("INVALID BACKEND");
#endif
}
static inline sg_resource_state _sg_create_buffer(_sg_buffer_t* buf, const sg_buffer_desc* desc) {
#if defined(_SOKOL_ANY_GL)
return _sg_gl_create_buffer(buf, desc);
#elif defined(SOKOL_METAL)
return _sg_mtl_create_buffer(buf, desc);
#elif defined(SOKOL_D3D11)
return _sg_d3d11_create_buffer(buf, desc);
#elif defined(SOKOL_WGPU)
return _sg_wgpu_create_buffer(buf, desc);
#elif defined(SOKOL_DUMMY_BACKEND)
return _sg_dummy_create_buffer(buf, desc);
#else
#error("INVALID BACKEND");
#endif
}
static inline void _sg_destroy_buffer(_sg_buffer_t* buf) {
#if defined(_SOKOL_ANY_GL)
_sg_gl_destroy_buffer(buf);
#elif defined(SOKOL_METAL)
_sg_mtl_destroy_buffer(buf);
#elif defined(SOKOL_D3D11)
_sg_d3d11_destroy_buffer(buf);
#elif defined(SOKOL_WGPU)
_sg_wgpu_destroy_buffer(buf);
#elif defined(SOKOL_DUMMY_BACKEND)
_sg_dummy_destroy_buffer(buf);
#else
#error("INVALID BACKEND");
#endif
}
static inline sg_resource_state _sg_create_image(_sg_image_t* img, const sg_image_desc* desc) {
#if defined(_SOKOL_ANY_GL)
return _sg_gl_create_image(img, desc);
#elif defined(SOKOL_METAL)
return _sg_mtl_create_image(img, desc);
#elif defined(SOKOL_D3D11)
return _sg_d3d11_create_image(img, desc);
#elif defined(SOKOL_WGPU)
return _sg_wgpu_create_image(img, desc);
#elif defined(SOKOL_DUMMY_BACKEND)
return _sg_dummy_create_image(img, desc);
#else
#error("INVALID BACKEND");
#endif
}
static inline void _sg_destroy_image(_sg_image_t* img) {
#if defined(_SOKOL_ANY_GL)
_sg_gl_destroy_image(img);
#elif defined(SOKOL_METAL)
_sg_mtl_destroy_image(img);
#elif defined(SOKOL_D3D11)
_sg_d3d11_destroy_image(img);
#elif defined(SOKOL_WGPU)
_sg_wgpu_destroy_image(img);
#elif defined(SOKOL_DUMMY_BACKEND)
_sg_dummy_destroy_image(img);
#else
#error("INVALID BACKEND");
#endif
}
static inline sg_resource_state _sg_create_shader(_sg_shader_t* shd, const sg_shader_desc* desc) {
#if defined(_SOKOL_ANY_GL)
return _sg_gl_create_shader(shd, desc);
#elif defined(SOKOL_METAL)
return _sg_mtl_create_shader(shd, desc);
#elif defined(SOKOL_D3D11)
return _sg_d3d11_create_shader(shd, desc);
#elif defined(SOKOL_WGPU)
return _sg_wgpu_create_shader(shd, desc);
#elif defined(SOKOL_DUMMY_BACKEND)
return _sg_dummy_create_shader(shd, desc);
#else
#error("INVALID BACKEND");
#endif
}
static inline void _sg_destroy_shader(_sg_shader_t* shd) {
#if defined(_SOKOL_ANY_GL)
_sg_gl_destroy_shader(shd);
#elif defined(SOKOL_METAL)
_sg_mtl_destroy_shader(shd);
#elif defined(SOKOL_D3D11)
_sg_d3d11_destroy_shader(shd);
#elif defined(SOKOL_WGPU)
_sg_wgpu_destroy_shader(shd);
#elif defined(SOKOL_DUMMY_BACKEND)
_sg_dummy_destroy_shader(shd);
#else
#error("INVALID BACKEND");
#endif
}
static inline sg_resource_state _sg_create_pipeline(_sg_pipeline_t* pip, _sg_shader_t* shd, const sg_pipeline_desc* desc) {
#if defined(_SOKOL_ANY_GL)
return _sg_gl_create_pipeline(pip, shd, desc);
#elif defined(SOKOL_METAL)
return _sg_mtl_create_pipeline(pip, shd, desc);
#elif defined(SOKOL_D3D11)
return _sg_d3d11_create_pipeline(pip, shd, desc);
#elif defined(SOKOL_WGPU)
return _sg_wgpu_create_pipeline(pip, shd, desc);
#elif defined(SOKOL_DUMMY_BACKEND)
return _sg_dummy_create_pipeline(pip, shd, desc);
#else
#error("INVALID BACKEND");
#endif
}
static inline void _sg_destroy_pipeline(_sg_pipeline_t* pip) {
#if defined(_SOKOL_ANY_GL)
_sg_gl_destroy_pipeline(pip);
#elif defined(SOKOL_METAL)
_sg_mtl_destroy_pipeline(pip);
#elif defined(SOKOL_D3D11)
_sg_d3d11_destroy_pipeline(pip);
#elif defined(SOKOL_WGPU)
_sg_wgpu_destroy_pipeline(pip);
#elif defined(SOKOL_DUMMY_BACKEND)
_sg_dummy_destroy_pipeline(pip);
#else
#error("INVALID BACKEND");
#endif
}
static inline sg_resource_state _sg_create_pass(_sg_pass_t* pass, _sg_image_t** att_images, const sg_pass_desc* desc) {
#if defined(_SOKOL_ANY_GL)
return _sg_gl_create_pass(pass, att_images, desc);
#elif defined(SOKOL_METAL)
return _sg_mtl_create_pass(pass, att_images, desc);
#elif defined(SOKOL_D3D11)
return _sg_d3d11_create_pass(pass, att_images, desc);
#elif defined(SOKOL_WGPU)
return _sg_wgpu_create_pass(pass, att_images, desc);
#elif defined(SOKOL_DUMMY_BACKEND)
return _sg_dummy_create_pass(pass, att_images, desc);
#else
#error("INVALID BACKEND");
#endif
}
static inline void _sg_destroy_pass(_sg_pass_t* pass) {
#if defined(_SOKOL_ANY_GL)
_sg_gl_destroy_pass(pass);
#elif defined(SOKOL_METAL)
_sg_mtl_destroy_pass(pass);
#elif defined(SOKOL_D3D11)
_sg_d3d11_destroy_pass(pass);
#elif defined(SOKOL_WGPU)
return _sg_wgpu_destroy_pass(pass);
#elif defined(SOKOL_DUMMY_BACKEND)
_sg_dummy_destroy_pass(pass);
#else
#error("INVALID BACKEND");
#endif
}
static inline _sg_image_t* _sg_pass_color_image(const _sg_pass_t* pass, int index) {
#if defined(_SOKOL_ANY_GL)
return _sg_gl_pass_color_image(pass, index);
#elif defined(SOKOL_METAL)
return _sg_mtl_pass_color_image(pass, index);
#elif defined(SOKOL_D3D11)
return _sg_d3d11_pass_color_image(pass, index);
#elif defined(SOKOL_WGPU)
return _sg_wgpu_pass_color_image(pass, index);
#elif defined(SOKOL_DUMMY_BACKEND)
return _sg_dummy_pass_color_image(pass, index);
#else
#error("INVALID BACKEND");
#endif
}
static inline _sg_image_t* _sg_pass_ds_image(const _sg_pass_t* pass) {
#if defined(_SOKOL_ANY_GL)
return _sg_gl_pass_ds_image(pass);
#elif defined(SOKOL_METAL)
return _sg_mtl_pass_ds_image(pass);
#elif defined(SOKOL_D3D11)
return _sg_d3d11_pass_ds_image(pass);
#elif defined(SOKOL_WGPU)
return _sg_wgpu_pass_ds_image(pass);
#elif defined(SOKOL_DUMMY_BACKEND)
return _sg_dummy_pass_ds_image(pass);
#else
#error("INVALID BACKEND");
#endif
}
static inline void _sg_begin_pass(_sg_pass_t* pass, const sg_pass_action* action, int w, int h) {
#if defined(_SOKOL_ANY_GL)
_sg_gl_begin_pass(pass, action, w, h);
#elif defined(SOKOL_METAL)
_sg_mtl_begin_pass(pass, action, w, h);
#elif defined(SOKOL_D3D11)
_sg_d3d11_begin_pass(pass, action, w, h);
#elif defined(SOKOL_WGPU)
_sg_wgpu_begin_pass(pass, action, w, h);
#elif defined(SOKOL_DUMMY_BACKEND)
_sg_dummy_begin_pass(pass, action, w, h);
#else
#error("INVALID BACKEND");
#endif
}
static inline void _sg_end_pass(void) {
#if defined(_SOKOL_ANY_GL)
_sg_gl_end_pass();
#elif defined(SOKOL_METAL)
_sg_mtl_end_pass();
#elif defined(SOKOL_D3D11)
_sg_d3d11_end_pass();
#elif defined(SOKOL_WGPU)
_sg_wgpu_end_pass();
#elif defined(SOKOL_DUMMY_BACKEND)
_sg_dummy_end_pass();
#else
#error("INVALID BACKEND");
#endif
}
static inline void _sg_apply_viewport(int x, int y, int w, int h, bool origin_top_left) {
#if defined(_SOKOL_ANY_GL)
_sg_gl_apply_viewport(x, y, w, h, origin_top_left);
#elif defined(SOKOL_METAL)
_sg_mtl_apply_viewport(x, y, w, h, origin_top_left);
#elif defined(SOKOL_D3D11)
_sg_d3d11_apply_viewport(x, y, w, h, origin_top_left);
#elif defined(SOKOL_WGPU)
_sg_wgpu_apply_viewport(x, y, w, h, origin_top_left);
#elif defined(SOKOL_DUMMY_BACKEND)
_sg_dummy_apply_viewport(x, y, w, h, origin_top_left);
#else
#error("INVALID BACKEND");
#endif
}
static inline void _sg_apply_scissor_rect(int x, int y, int w, int h, bool origin_top_left) {
#if defined(_SOKOL_ANY_GL)
_sg_gl_apply_scissor_rect(x, y, w, h, origin_top_left);
#elif defined(SOKOL_METAL)
_sg_mtl_apply_scissor_rect(x, y, w, h, origin_top_left);
#elif defined(SOKOL_D3D11)
_sg_d3d11_apply_scissor_rect(x, y, w, h, origin_top_left);
#elif defined(SOKOL_WGPU)
_sg_wgpu_apply_scissor_rect(x, y, w, h, origin_top_left);
#elif defined(SOKOL_DUMMY_BACKEND)
_sg_dummy_apply_scissor_rect(x, y, w, h, origin_top_left);
#else
#error("INVALID BACKEND");
#endif
}
static inline void _sg_apply_pipeline(_sg_pipeline_t* pip) {
#if defined(_SOKOL_ANY_GL)
_sg_gl_apply_pipeline(pip);
#elif defined(SOKOL_METAL)
_sg_mtl_apply_pipeline(pip);
#elif defined(SOKOL_D3D11)
_sg_d3d11_apply_pipeline(pip);
#elif defined(SOKOL_WGPU)
_sg_wgpu_apply_pipeline(pip);
#elif defined(SOKOL_DUMMY_BACKEND)
_sg_dummy_apply_pipeline(pip);
#else
#error("INVALID BACKEND");
#endif
}
static inline void _sg_apply_bindings(
_sg_pipeline_t* pip,
_sg_buffer_t** vbs, const int* vb_offsets, int num_vbs,
_sg_buffer_t* ib, int ib_offset,
_sg_image_t** vs_imgs, int num_vs_imgs,
_sg_image_t** fs_imgs, int num_fs_imgs)
{
#if defined(_SOKOL_ANY_GL)
_sg_gl_apply_bindings(pip, vbs, vb_offsets, num_vbs, ib, ib_offset, vs_imgs, num_vs_imgs, fs_imgs, num_fs_imgs);
#elif defined(SOKOL_METAL)
_sg_mtl_apply_bindings(pip, vbs, vb_offsets, num_vbs, ib, ib_offset, vs_imgs, num_vs_imgs, fs_imgs, num_fs_imgs);
#elif defined(SOKOL_D3D11)
_sg_d3d11_apply_bindings(pip, vbs, vb_offsets, num_vbs, ib, ib_offset, vs_imgs, num_vs_imgs, fs_imgs, num_fs_imgs);
#elif defined(SOKOL_WGPU)
_sg_wgpu_apply_bindings(pip, vbs, vb_offsets, num_vbs, ib, ib_offset, vs_imgs, num_vs_imgs, fs_imgs, num_fs_imgs);
#elif defined(SOKOL_DUMMY_BACKEND)
_sg_dummy_apply_bindings(pip, vbs, vb_offsets, num_vbs, ib, ib_offset, vs_imgs, num_vs_imgs, fs_imgs, num_fs_imgs);
#else
#error("INVALID BACKEND");
#endif
}
static inline void _sg_apply_uniforms(sg_shader_stage stage_index, int ub_index, const sg_range* data) {
#if defined(_SOKOL_ANY_GL)
_sg_gl_apply_uniforms(stage_index, ub_index, data);
#elif defined(SOKOL_METAL)
_sg_mtl_apply_uniforms(stage_index, ub_index, data);
#elif defined(SOKOL_D3D11)
_sg_d3d11_apply_uniforms(stage_index, ub_index, data);
#elif defined(SOKOL_WGPU)
_sg_wgpu_apply_uniforms(stage_index, ub_index, data);
#elif defined(SOKOL_DUMMY_BACKEND)
_sg_dummy_apply_uniforms(stage_index, ub_index, data);
#else
#error("INVALID BACKEND");
#endif
}
static inline void _sg_draw(int base_element, int num_elements, int num_instances) {
#if defined(_SOKOL_ANY_GL)
_sg_gl_draw(base_element, num_elements, num_instances);
#elif defined(SOKOL_METAL)
_sg_mtl_draw(base_element, num_elements, num_instances);
#elif defined(SOKOL_D3D11)
_sg_d3d11_draw(base_element, num_elements, num_instances);
#elif defined(SOKOL_WGPU)
_sg_wgpu_draw(base_element, num_elements, num_instances);
#elif defined(SOKOL_DUMMY_BACKEND)
_sg_dummy_draw(base_element, num_elements, num_instances);
#else
#error("INVALID BACKEND");
#endif
}
static inline void _sg_commit(void) {
#if defined(_SOKOL_ANY_GL)
_sg_gl_commit();
#elif defined(SOKOL_METAL)
_sg_mtl_commit();
#elif defined(SOKOL_D3D11)
_sg_d3d11_commit();
#elif defined(SOKOL_WGPU)
_sg_wgpu_commit();
#elif defined(SOKOL_DUMMY_BACKEND)
_sg_dummy_commit();
#else
#error("INVALID BACKEND");
#endif
}
static inline void _sg_update_buffer(_sg_buffer_t* buf, const sg_range* data) {
#if defined(_SOKOL_ANY_GL)
_sg_gl_update_buffer(buf, data);
#elif defined(SOKOL_METAL)
_sg_mtl_update_buffer(buf, data);
#elif defined(SOKOL_D3D11)
_sg_d3d11_update_buffer(buf, data);
#elif defined(SOKOL_WGPU)
_sg_wgpu_update_buffer(buf, data);
#elif defined(SOKOL_DUMMY_BACKEND)
_sg_dummy_update_buffer(buf, data);
#else
#error("INVALID BACKEND");
#endif
}
static inline int _sg_append_buffer(_sg_buffer_t* buf, const sg_range* data, bool new_frame) {
#if defined(_SOKOL_ANY_GL)
return _sg_gl_append_buffer(buf, data, new_frame);
#elif defined(SOKOL_METAL)
return _sg_mtl_append_buffer(buf, data, new_frame);
#elif defined(SOKOL_D3D11)
return _sg_d3d11_append_buffer(buf, data, new_frame);
#elif defined(SOKOL_WGPU)
return _sg_wgpu_append_buffer(buf, data, new_frame);
#elif defined(SOKOL_DUMMY_BACKEND)
return _sg_dummy_append_buffer(buf, data, new_frame);
#else
#error("INVALID BACKEND");
#endif
}
static inline void _sg_update_image(_sg_image_t* img, const sg_image_data* data) {
#if defined(_SOKOL_ANY_GL)
_sg_gl_update_image(img, data);
#elif defined(SOKOL_METAL)
_sg_mtl_update_image(img, data);
#elif defined(SOKOL_D3D11)
_sg_d3d11_update_image(img, data);
#elif defined(SOKOL_WGPU)
_sg_wgpu_update_image(img, data);
#elif defined(SOKOL_DUMMY_BACKEND)
_sg_dummy_update_image(img, data);
#else
#error("INVALID BACKEND");
#endif
}
/*== RESOURCE POOLS ==========================================================*/
_SOKOL_PRIVATE void _sg_init_pool(_sg_pool_t* pool, int num) {
SOKOL_ASSERT(pool && (num >= 1));
/* slot 0 is reserved for the 'invalid id', so bump the pool size by 1 */
pool->size = num + 1;
pool->queue_top = 0;
/* generation counters indexable by pool slot index, slot 0 is reserved */
size_t gen_ctrs_size = sizeof(uint32_t) * (size_t)pool->size;
pool->gen_ctrs = (uint32_t*) SOKOL_MALLOC(gen_ctrs_size);
SOKOL_ASSERT(pool->gen_ctrs);
memset(pool->gen_ctrs, 0, gen_ctrs_size);
/* it's not a bug to only reserve 'num' here */
pool->free_queue = (int*) SOKOL_MALLOC(sizeof(int) * (size_t)num);
SOKOL_ASSERT(pool->free_queue);
/* never allocate the zero-th pool item since the invalid id is 0 */
for (int i = pool->size-1; i >= 1; i--) {
pool->free_queue[pool->queue_top++] = i;
}
}
_SOKOL_PRIVATE void _sg_discard_pool(_sg_pool_t* pool) {
SOKOL_ASSERT(pool);
SOKOL_ASSERT(pool->free_queue);
SOKOL_FREE(pool->free_queue);
pool->free_queue = 0;
SOKOL_ASSERT(pool->gen_ctrs);
SOKOL_FREE(pool->gen_ctrs);
pool->gen_ctrs = 0;
pool->size = 0;
pool->queue_top = 0;
}
_SOKOL_PRIVATE int _sg_pool_alloc_index(_sg_pool_t* pool) {
SOKOL_ASSERT(pool);
SOKOL_ASSERT(pool->free_queue);
if (pool->queue_top > 0) {
int slot_index = pool->free_queue[--pool->queue_top];
SOKOL_ASSERT((slot_index > 0) && (slot_index < pool->size));
return slot_index;
}
else {
/* pool exhausted */
return _SG_INVALID_SLOT_INDEX;
}
}
_SOKOL_PRIVATE void _sg_pool_free_index(_sg_pool_t* pool, int slot_index) {
SOKOL_ASSERT((slot_index > _SG_INVALID_SLOT_INDEX) && (slot_index < pool->size));
SOKOL_ASSERT(pool);
SOKOL_ASSERT(pool->free_queue);
SOKOL_ASSERT(pool->queue_top < pool->size);
#ifdef SOKOL_DEBUG
/* debug check against double-free */
for (int i = 0; i < pool->queue_top; i++) {
SOKOL_ASSERT(pool->free_queue[i] != slot_index);
}
#endif
pool->free_queue[pool->queue_top++] = slot_index;
SOKOL_ASSERT(pool->queue_top <= (pool->size-1));
}
_SOKOL_PRIVATE void _sg_reset_slot(_sg_slot_t* slot) {
SOKOL_ASSERT(slot);
memset(slot, 0, sizeof(_sg_slot_t));
}
_SOKOL_PRIVATE void _sg_reset_buffer(_sg_buffer_t* buf) {
SOKOL_ASSERT(buf);
_sg_slot_t slot = buf->slot;
memset(buf, 0, sizeof(_sg_buffer_t));
buf->slot = slot;
buf->slot.state = SG_RESOURCESTATE_ALLOC;
}
_SOKOL_PRIVATE void _sg_reset_image(_sg_image_t* img) {
SOKOL_ASSERT(img);
_sg_slot_t slot = img->slot;
memset(img, 0, sizeof(_sg_image_t));
img->slot = slot;
img->slot.state = SG_RESOURCESTATE_ALLOC;
}
_SOKOL_PRIVATE void _sg_reset_shader(_sg_shader_t* shd) {
SOKOL_ASSERT(shd);
_sg_slot_t slot = shd->slot;
memset(shd, 0, sizeof(_sg_shader_t));
shd->slot = slot;
shd->slot.state = SG_RESOURCESTATE_ALLOC;
}
_SOKOL_PRIVATE void _sg_reset_pipeline(_sg_pipeline_t* pip) {
SOKOL_ASSERT(pip);
_sg_slot_t slot = pip->slot;
memset(pip, 0, sizeof(_sg_pipeline_t));
pip->slot = slot;
pip->slot.state = SG_RESOURCESTATE_ALLOC;
}
_SOKOL_PRIVATE void _sg_reset_pass(_sg_pass_t* pass) {
SOKOL_ASSERT(pass);
_sg_slot_t slot = pass->slot;
memset(pass, 0, sizeof(_sg_pass_t));
pass->slot = slot;
pass->slot.state = SG_RESOURCESTATE_ALLOC;
}
_SOKOL_PRIVATE void _sg_reset_context(_sg_context_t* ctx) {
SOKOL_ASSERT(ctx);
_sg_slot_t slot = ctx->slot;
memset(ctx, 0, sizeof(_sg_context_t));
ctx->slot = slot;
ctx->slot.state = SG_RESOURCESTATE_ALLOC;
}
_SOKOL_PRIVATE void _sg_setup_pools(_sg_pools_t* p, const sg_desc* desc) {
SOKOL_ASSERT(p);
SOKOL_ASSERT(desc);
/* note: the pools here will have an additional item, since slot 0 is reserved */
SOKOL_ASSERT((desc->buffer_pool_size > 0) && (desc->buffer_pool_size < _SG_MAX_POOL_SIZE));
_sg_init_pool(&p->buffer_pool, desc->buffer_pool_size);
size_t buffer_pool_byte_size = sizeof(_sg_buffer_t) * (size_t)p->buffer_pool.size;
p->buffers = (_sg_buffer_t*) SOKOL_MALLOC(buffer_pool_byte_size);
SOKOL_ASSERT(p->buffers);
memset(p->buffers, 0, buffer_pool_byte_size);
SOKOL_ASSERT((desc->image_pool_size > 0) && (desc->image_pool_size < _SG_MAX_POOL_SIZE));
_sg_init_pool(&p->image_pool, desc->image_pool_size);
size_t image_pool_byte_size = sizeof(_sg_image_t) * (size_t)p->image_pool.size;
p->images = (_sg_image_t*) SOKOL_MALLOC(image_pool_byte_size);
SOKOL_ASSERT(p->images);
memset(p->images, 0, image_pool_byte_size);
SOKOL_ASSERT((desc->shader_pool_size > 0) && (desc->shader_pool_size < _SG_MAX_POOL_SIZE));
_sg_init_pool(&p->shader_pool, desc->shader_pool_size);
size_t shader_pool_byte_size = sizeof(_sg_shader_t) * (size_t)p->shader_pool.size;
p->shaders = (_sg_shader_t*) SOKOL_MALLOC(shader_pool_byte_size);
SOKOL_ASSERT(p->shaders);
memset(p->shaders, 0, shader_pool_byte_size);
SOKOL_ASSERT((desc->pipeline_pool_size > 0) && (desc->pipeline_pool_size < _SG_MAX_POOL_SIZE));
_sg_init_pool(&p->pipeline_pool, desc->pipeline_pool_size);
size_t pipeline_pool_byte_size = sizeof(_sg_pipeline_t) * (size_t)p->pipeline_pool.size;
p->pipelines = (_sg_pipeline_t*) SOKOL_MALLOC(pipeline_pool_byte_size);
SOKOL_ASSERT(p->pipelines);
memset(p->pipelines, 0, pipeline_pool_byte_size);
SOKOL_ASSERT((desc->pass_pool_size > 0) && (desc->pass_pool_size < _SG_MAX_POOL_SIZE));
_sg_init_pool(&p->pass_pool, desc->pass_pool_size);
size_t pass_pool_byte_size = sizeof(_sg_pass_t) * (size_t)p->pass_pool.size;
p->passes = (_sg_pass_t*) SOKOL_MALLOC(pass_pool_byte_size);
SOKOL_ASSERT(p->passes);
memset(p->passes, 0, pass_pool_byte_size);
SOKOL_ASSERT((desc->context_pool_size > 0) && (desc->context_pool_size < _SG_MAX_POOL_SIZE));
_sg_init_pool(&p->context_pool, desc->context_pool_size);
size_t context_pool_byte_size = sizeof(_sg_context_t) * (size_t)p->context_pool.size;
p->contexts = (_sg_context_t*) SOKOL_MALLOC(context_pool_byte_size);
SOKOL_ASSERT(p->contexts);
memset(p->contexts, 0, context_pool_byte_size);
}
_SOKOL_PRIVATE void _sg_discard_pools(_sg_pools_t* p) {
SOKOL_ASSERT(p);
SOKOL_FREE(p->contexts); p->contexts = 0;
SOKOL_FREE(p->passes); p->passes = 0;
SOKOL_FREE(p->pipelines); p->pipelines = 0;
SOKOL_FREE(p->shaders); p->shaders = 0;
SOKOL_FREE(p->images); p->images = 0;
SOKOL_FREE(p->buffers); p->buffers = 0;
_sg_discard_pool(&p->context_pool);
_sg_discard_pool(&p->pass_pool);
_sg_discard_pool(&p->pipeline_pool);
_sg_discard_pool(&p->shader_pool);
_sg_discard_pool(&p->image_pool);
_sg_discard_pool(&p->buffer_pool);
}
/* allocate the slot at slot_index:
- bump the slot's generation counter
- create a resource id from the generation counter and slot index
- set the slot's id to this id
- set the slot's state to ALLOC
- return the resource id
*/
_SOKOL_PRIVATE uint32_t _sg_slot_alloc(_sg_pool_t* pool, _sg_slot_t* slot, int slot_index) {
/* FIXME: add handling for an overflowing generation counter,
for now, just overflow (another option is to disable
the slot)
*/
SOKOL_ASSERT(pool && pool->gen_ctrs);
SOKOL_ASSERT((slot_index > _SG_INVALID_SLOT_INDEX) && (slot_index < pool->size));
SOKOL_ASSERT((slot->state == SG_RESOURCESTATE_INITIAL) && (slot->id == SG_INVALID_ID));
uint32_t ctr = ++pool->gen_ctrs[slot_index];
slot->id = (ctr<<_SG_SLOT_SHIFT)|(slot_index & _SG_SLOT_MASK);
slot->state = SG_RESOURCESTATE_ALLOC;
return slot->id;
}
/* extract slot index from id */
_SOKOL_PRIVATE int _sg_slot_index(uint32_t id) {
int slot_index = (int) (id & _SG_SLOT_MASK);
SOKOL_ASSERT(_SG_INVALID_SLOT_INDEX != slot_index);
return slot_index;
}
/* returns pointer to resource by id without matching id check */
_SOKOL_PRIVATE _sg_buffer_t* _sg_buffer_at(const _sg_pools_t* p, uint32_t buf_id) {
SOKOL_ASSERT(p && (SG_INVALID_ID != buf_id));
int slot_index = _sg_slot_index(buf_id);
SOKOL_ASSERT((slot_index > _SG_INVALID_SLOT_INDEX) && (slot_index < p->buffer_pool.size));
return &p->buffers[slot_index];
}
_SOKOL_PRIVATE _sg_image_t* _sg_image_at(const _sg_pools_t* p, uint32_t img_id) {
SOKOL_ASSERT(p && (SG_INVALID_ID != img_id));
int slot_index = _sg_slot_index(img_id);
SOKOL_ASSERT((slot_index > _SG_INVALID_SLOT_INDEX) && (slot_index < p->image_pool.size));
return &p->images[slot_index];
}
_SOKOL_PRIVATE _sg_shader_t* _sg_shader_at(const _sg_pools_t* p, uint32_t shd_id) {
SOKOL_ASSERT(p && (SG_INVALID_ID != shd_id));
int slot_index = _sg_slot_index(shd_id);
SOKOL_ASSERT((slot_index > _SG_INVALID_SLOT_INDEX) && (slot_index < p->shader_pool.size));
return &p->shaders[slot_index];
}
_SOKOL_PRIVATE _sg_pipeline_t* _sg_pipeline_at(const _sg_pools_t* p, uint32_t pip_id) {
SOKOL_ASSERT(p && (SG_INVALID_ID != pip_id));
int slot_index = _sg_slot_index(pip_id);
SOKOL_ASSERT((slot_index > _SG_INVALID_SLOT_INDEX) && (slot_index < p->pipeline_pool.size));
return &p->pipelines[slot_index];
}
_SOKOL_PRIVATE _sg_pass_t* _sg_pass_at(const _sg_pools_t* p, uint32_t pass_id) {
SOKOL_ASSERT(p && (SG_INVALID_ID != pass_id));
int slot_index = _sg_slot_index(pass_id);
SOKOL_ASSERT((slot_index > _SG_INVALID_SLOT_INDEX) && (slot_index < p->pass_pool.size));
return &p->passes[slot_index];
}
_SOKOL_PRIVATE _sg_context_t* _sg_context_at(const _sg_pools_t* p, uint32_t context_id) {
SOKOL_ASSERT(p && (SG_INVALID_ID != context_id));
int slot_index = _sg_slot_index(context_id);
SOKOL_ASSERT((slot_index > _SG_INVALID_SLOT_INDEX) && (slot_index < p->context_pool.size));
return &p->contexts[slot_index];
}
/* returns pointer to resource with matching id check, may return 0 */
_SOKOL_PRIVATE _sg_buffer_t* _sg_lookup_buffer(const _sg_pools_t* p, uint32_t buf_id) {
if (SG_INVALID_ID != buf_id) {
_sg_buffer_t* buf = _sg_buffer_at(p, buf_id);
if (buf->slot.id == buf_id) {
return buf;
}
}
return 0;
}
_SOKOL_PRIVATE _sg_image_t* _sg_lookup_image(const _sg_pools_t* p, uint32_t img_id) {
if (SG_INVALID_ID != img_id) {
_sg_image_t* img = _sg_image_at(p, img_id);
if (img->slot.id == img_id) {
return img;
}
}
return 0;
}
_SOKOL_PRIVATE _sg_shader_t* _sg_lookup_shader(const _sg_pools_t* p, uint32_t shd_id) {
SOKOL_ASSERT(p);
if (SG_INVALID_ID != shd_id) {
_sg_shader_t* shd = _sg_shader_at(p, shd_id);
if (shd->slot.id == shd_id) {
return shd;
}
}
return 0;
}
_SOKOL_PRIVATE _sg_pipeline_t* _sg_lookup_pipeline(const _sg_pools_t* p, uint32_t pip_id) {
SOKOL_ASSERT(p);
if (SG_INVALID_ID != pip_id) {
_sg_pipeline_t* pip = _sg_pipeline_at(p, pip_id);
if (pip->slot.id == pip_id) {
return pip;
}
}
return 0;
}
_SOKOL_PRIVATE _sg_pass_t* _sg_lookup_pass(const _sg_pools_t* p, uint32_t pass_id) {
SOKOL_ASSERT(p);
if (SG_INVALID_ID != pass_id) {
_sg_pass_t* pass = _sg_pass_at(p, pass_id);
if (pass->slot.id == pass_id) {
return pass;
}
}
return 0;
}
_SOKOL_PRIVATE _sg_context_t* _sg_lookup_context(const _sg_pools_t* p, uint32_t ctx_id) {
SOKOL_ASSERT(p);
if (SG_INVALID_ID != ctx_id) {
_sg_context_t* ctx = _sg_context_at(p, ctx_id);
if (ctx->slot.id == ctx_id) {
return ctx;
}
}
return 0;
}
_SOKOL_PRIVATE void _sg_destroy_all_resources(_sg_pools_t* p, uint32_t ctx_id) {
/* this is a bit dumb since it loops over all pool slots to
find the occupied slots, on the other hand it is only ever
executed at shutdown
NOTE: ONLY EXECUTE THIS AT SHUTDOWN
...because the free queues will not be reset
and the resource slots not be cleared!
*/
for (int i = 1; i < p->buffer_pool.size; i++) {
if (p->buffers[i].slot.ctx_id == ctx_id) {
sg_resource_state state = p->buffers[i].slot.state;
if ((state == SG_RESOURCESTATE_VALID) || (state == SG_RESOURCESTATE_FAILED)) {
_sg_destroy_buffer(&p->buffers[i]);
}
}
}
for (int i = 1; i < p->image_pool.size; i++) {
if (p->images[i].slot.ctx_id == ctx_id) {
sg_resource_state state = p->images[i].slot.state;
if ((state == SG_RESOURCESTATE_VALID) || (state == SG_RESOURCESTATE_FAILED)) {
_sg_destroy_image(&p->images[i]);
}
}
}
for (int i = 1; i < p->shader_pool.size; i++) {
if (p->shaders[i].slot.ctx_id == ctx_id) {
sg_resource_state state = p->shaders[i].slot.state;
if ((state == SG_RESOURCESTATE_VALID) || (state == SG_RESOURCESTATE_FAILED)) {
_sg_destroy_shader(&p->shaders[i]);
}
}
}
for (int i = 1; i < p->pipeline_pool.size; i++) {
if (p->pipelines[i].slot.ctx_id == ctx_id) {
sg_resource_state state = p->pipelines[i].slot.state;
if ((state == SG_RESOURCESTATE_VALID) || (state == SG_RESOURCESTATE_FAILED)) {
_sg_destroy_pipeline(&p->pipelines[i]);
}
}
}
for (int i = 1; i < p->pass_pool.size; i++) {
if (p->passes[i].slot.ctx_id == ctx_id) {
sg_resource_state state = p->passes[i].slot.state;
if ((state == SG_RESOURCESTATE_VALID) || (state == SG_RESOURCESTATE_FAILED)) {
_sg_destroy_pass(&p->passes[i]);
}
}
}
}
/*== VALIDATION LAYER ========================================================*/
#if defined(SOKOL_DEBUG)
/* return a human readable string for an _sg_validate_error */
_SOKOL_PRIVATE const char* _sg_validate_string(_sg_validate_error_t err) {
switch (err) {
/* buffer creation validation errors */
case _SG_VALIDATE_BUFFERDESC_CANARY: return "sg_buffer_desc not initialized";
case _SG_VALIDATE_BUFFERDESC_SIZE: return "sg_buffer_desc.size cannot be 0";
case _SG_VALIDATE_BUFFERDESC_DATA: return "immutable buffers must be initialized with data (sg_buffer_desc.data.ptr and sg_buffer_desc.data.size)";
case _SG_VALIDATE_BUFFERDESC_DATA_SIZE: return "immutable buffer data size differs from buffer size";
case _SG_VALIDATE_BUFFERDESC_NO_DATA: return "dynamic/stream usage buffers cannot be initialized with data";
/* image creation validation errros */
case _SG_VALIDATE_IMAGEDESC_CANARY: return "sg_image_desc not initialized";
case _SG_VALIDATE_IMAGEDESC_WIDTH: return "sg_image_desc.width must be > 0";
case _SG_VALIDATE_IMAGEDESC_HEIGHT: return "sg_image_desc.height must be > 0";
case _SG_VALIDATE_IMAGEDESC_RT_PIXELFORMAT: return "invalid pixel format for render-target image";
case _SG_VALIDATE_IMAGEDESC_NONRT_PIXELFORMAT: return "invalid pixel format for non-render-target image";
case _SG_VALIDATE_IMAGEDESC_MSAA_BUT_NO_RT: return "non-render-target images cannot be multisampled";
case _SG_VALIDATE_IMAGEDESC_NO_MSAA_RT_SUPPORT: return "MSAA not supported for this pixel format";
case _SG_VALIDATE_IMAGEDESC_RT_IMMUTABLE: return "render target images must be SG_USAGE_IMMUTABLE";
case _SG_VALIDATE_IMAGEDESC_RT_NO_DATA: return "render target images cannot be initialized with data";
case _SG_VALIDATE_IMAGEDESC_DATA: return "missing or invalid data for immutable image";
case _SG_VALIDATE_IMAGEDESC_NO_DATA: return "dynamic/stream usage images cannot be initialized with data";
/* shader creation */
case _SG_VALIDATE_SHADERDESC_CANARY: return "sg_shader_desc not initialized";
case _SG_VALIDATE_SHADERDESC_SOURCE: return "shader source code required";
case _SG_VALIDATE_SHADERDESC_BYTECODE: return "shader byte code required";
case _SG_VALIDATE_SHADERDESC_SOURCE_OR_BYTECODE: return "shader source or byte code required";
case _SG_VALIDATE_SHADERDESC_NO_BYTECODE_SIZE: return "shader byte code length (in bytes) required";
case _SG_VALIDATE_SHADERDESC_NO_CONT_UBS: return "shader uniform blocks must occupy continuous slots";
case _SG_VALIDATE_SHADERDESC_NO_CONT_UB_MEMBERS: return "uniform block members must occupy continuous slots";
case _SG_VALIDATE_SHADERDESC_NO_UB_MEMBERS: return "GL backend requires uniform block member declarations";
case _SG_VALIDATE_SHADERDESC_UB_MEMBER_NAME: return "uniform block member name missing";
case _SG_VALIDATE_SHADERDESC_UB_SIZE_MISMATCH: return "size of uniform block members doesn't match uniform block size";
case _SG_VALIDATE_SHADERDESC_NO_CONT_IMGS: return "shader images must occupy continuous slots";
case _SG_VALIDATE_SHADERDESC_IMG_NAME: return "GL backend requires uniform block member names";
case _SG_VALIDATE_SHADERDESC_ATTR_NAMES: return "GLES2 backend requires vertex attribute names";
case _SG_VALIDATE_SHADERDESC_ATTR_SEMANTICS: return "D3D11 backend requires vertex attribute semantics";
case _SG_VALIDATE_SHADERDESC_ATTR_STRING_TOO_LONG: return "vertex attribute name/semantic string too long (max len 16)";
/* pipeline creation */
case _SG_VALIDATE_PIPELINEDESC_CANARY: return "sg_pipeline_desc not initialized";
case _SG_VALIDATE_PIPELINEDESC_SHADER: return "sg_pipeline_desc.shader missing or invalid";
case _SG_VALIDATE_PIPELINEDESC_NO_ATTRS: return "sg_pipeline_desc.layout.attrs is empty or not continuous";
case _SG_VALIDATE_PIPELINEDESC_LAYOUT_STRIDE4: return "sg_pipeline_desc.layout.buffers[].stride must be multiple of 4";
case _SG_VALIDATE_PIPELINEDESC_ATTR_NAME: return "GLES2/WebGL missing vertex attribute name in shader";
case _SG_VALIDATE_PIPELINEDESC_ATTR_SEMANTICS: return "D3D11 missing vertex attribute semantics in shader";
/* pass creation */
case _SG_VALIDATE_PASSDESC_CANARY: return "sg_pass_desc not initialized";
case _SG_VALIDATE_PASSDESC_NO_COLOR_ATTS: return "sg_pass_desc.color_attachments[0] must be valid";
case _SG_VALIDATE_PASSDESC_NO_CONT_COLOR_ATTS: return "color attachments must occupy continuous slots";
case _SG_VALIDATE_PASSDESC_IMAGE: return "pass attachment image is not valid";
case _SG_VALIDATE_PASSDESC_MIPLEVEL: return "pass attachment mip level is bigger than image has mipmaps";
case _SG_VALIDATE_PASSDESC_FACE: return "pass attachment image is cubemap, but face index is too big";
case _SG_VALIDATE_PASSDESC_LAYER: return "pass attachment image is array texture, but layer index is too big";
case _SG_VALIDATE_PASSDESC_SLICE: return "pass attachment image is 3d texture, but slice value is too big";
case _SG_VALIDATE_PASSDESC_IMAGE_NO_RT: return "pass attachment image must be render targets";
case _SG_VALIDATE_PASSDESC_COLOR_INV_PIXELFORMAT: return "pass color-attachment images must have a renderable pixel format";
case _SG_VALIDATE_PASSDESC_DEPTH_INV_PIXELFORMAT: return "pass depth-attachment image must have depth pixel format";
case _SG_VALIDATE_PASSDESC_IMAGE_SIZES: return "all pass attachments must have the same size";
case _SG_VALIDATE_PASSDESC_IMAGE_SAMPLE_COUNTS: return "all pass attachments must have the same sample count";
/* sg_begin_pass */
case _SG_VALIDATE_BEGINPASS_PASS: return "sg_begin_pass: pass must be valid";
case _SG_VALIDATE_BEGINPASS_IMAGE: return "sg_begin_pass: one or more attachment images are not valid";
/* sg_apply_pipeline */
case _SG_VALIDATE_APIP_PIPELINE_VALID_ID: return "sg_apply_pipeline: invalid pipeline id provided";
case _SG_VALIDATE_APIP_PIPELINE_EXISTS: return "sg_apply_pipeline: pipeline object no longer alive";
case _SG_VALIDATE_APIP_PIPELINE_VALID: return "sg_apply_pipeline: pipeline object not in valid state";
case _SG_VALIDATE_APIP_SHADER_EXISTS: return "sg_apply_pipeline: shader object no longer alive";
case _SG_VALIDATE_APIP_SHADER_VALID: return "sg_apply_pipeline: shader object not in valid state";
case _SG_VALIDATE_APIP_ATT_COUNT: return "sg_apply_pipeline: number of pipeline color attachments doesn't match number of pass color attachments";
case _SG_VALIDATE_APIP_COLOR_FORMAT: return "sg_apply_pipeline: pipeline color attachment pixel format doesn't match pass color attachment pixel format";
case _SG_VALIDATE_APIP_DEPTH_FORMAT: return "sg_apply_pipeline: pipeline depth pixel_format doesn't match pass depth attachment pixel format";
case _SG_VALIDATE_APIP_SAMPLE_COUNT: return "sg_apply_pipeline: pipeline MSAA sample count doesn't match render pass attachment sample count";
/* sg_apply_bindings */
case _SG_VALIDATE_ABND_PIPELINE: return "sg_apply_bindings: must be called after sg_apply_pipeline";
case _SG_VALIDATE_ABND_PIPELINE_EXISTS: return "sg_apply_bindings: currently applied pipeline object no longer alive";
case _SG_VALIDATE_ABND_PIPELINE_VALID: return "sg_apply_bindings: currently applied pipeline object not in valid state";
case _SG_VALIDATE_ABND_VBS: return "sg_apply_bindings: number of vertex buffers doesn't match number of pipeline vertex layouts";
case _SG_VALIDATE_ABND_VB_EXISTS: return "sg_apply_bindings: vertex buffer no longer alive";
case _SG_VALIDATE_ABND_VB_TYPE: return "sg_apply_bindings: buffer in vertex buffer slot is not a SG_BUFFERTYPE_VERTEXBUFFER";
case _SG_VALIDATE_ABND_VB_OVERFLOW: return "sg_apply_bindings: buffer in vertex buffer slot is overflown";
case _SG_VALIDATE_ABND_NO_IB: return "sg_apply_bindings: pipeline object defines indexed rendering, but no index buffer provided";
case _SG_VALIDATE_ABND_IB: return "sg_apply_bindings: pipeline object defines non-indexed rendering, but index buffer provided";
case _SG_VALIDATE_ABND_IB_EXISTS: return "sg_apply_bindings: index buffer no longer alive";
case _SG_VALIDATE_ABND_IB_TYPE: return "sg_apply_bindings: buffer in index buffer slot is not a SG_BUFFERTYPE_INDEXBUFFER";
case _SG_VALIDATE_ABND_IB_OVERFLOW: return "sg_apply_bindings: buffer in index buffer slot is overflown";
case _SG_VALIDATE_ABND_VS_IMGS: return "sg_apply_bindings: vertex shader image count doesn't match sg_shader_desc";
case _SG_VALIDATE_ABND_VS_IMG_EXISTS: return "sg_apply_bindings: vertex shader image no longer alive";
case _SG_VALIDATE_ABND_VS_IMG_TYPES: return "sg_apply_bindings: one or more vertex shader image types don't match sg_shader_desc";
case _SG_VALIDATE_ABND_FS_IMGS: return "sg_apply_bindings: fragment shader image count doesn't match sg_shader_desc";
case _SG_VALIDATE_ABND_FS_IMG_EXISTS: return "sg_apply_bindings: fragment shader image no longer alive";
case _SG_VALIDATE_ABND_FS_IMG_TYPES: return "sg_apply_bindings: one or more fragment shader image types don't match sg_shader_desc";
/* sg_apply_uniforms */
case _SG_VALIDATE_AUB_NO_PIPELINE: return "sg_apply_uniforms: must be called after sg_apply_pipeline()";
case _SG_VALIDATE_AUB_NO_UB_AT_SLOT: return "sg_apply_uniforms: no uniform block declaration at this shader stage UB slot";
case _SG_VALIDATE_AUB_SIZE: return "sg_apply_uniforms: data size exceeds declared uniform block size";
/* sg_update_buffer */
case _SG_VALIDATE_UPDATEBUF_USAGE: return "sg_update_buffer: cannot update immutable buffer";
case _SG_VALIDATE_UPDATEBUF_SIZE: return "sg_update_buffer: update size is bigger than buffer size";
case _SG_VALIDATE_UPDATEBUF_ONCE: return "sg_update_buffer: only one update allowed per buffer and frame";
case _SG_VALIDATE_UPDATEBUF_APPEND: return "sg_update_buffer: cannot call sg_update_buffer and sg_append_buffer in same frame";
/* sg_append_buffer */
case _SG_VALIDATE_APPENDBUF_USAGE: return "sg_append_buffer: cannot append to immutable buffer";
case _SG_VALIDATE_APPENDBUF_SIZE: return "sg_append_buffer: overall appended size is bigger than buffer size";
case _SG_VALIDATE_APPENDBUF_UPDATE: return "sg_append_buffer: cannot call sg_append_buffer and sg_update_buffer in same frame";
/* sg_update_image */
case _SG_VALIDATE_UPDIMG_USAGE: return "sg_update_image: cannot update immutable image";
case _SG_VALIDATE_UPDIMG_NOTENOUGHDATA: return "sg_update_image: not enough subimage data provided";
case _SG_VALIDATE_UPDIMG_SIZE: return "sg_update_image: provided subimage data size too big";
case _SG_VALIDATE_UPDIMG_COMPRESSED: return "sg_update_image: cannot update images with compressed format";
case _SG_VALIDATE_UPDIMG_ONCE: return "sg_update_image: only one update allowed per image and frame";
default: return "unknown validation error";
}
}
#endif /* defined(SOKOL_DEBUG) */
/*-- validation checks -------------------------------------------------------*/
#if defined(SOKOL_DEBUG)
_SOKOL_PRIVATE void _sg_validate_begin(void) {
_sg.validate_error = _SG_VALIDATE_SUCCESS;
}
_SOKOL_PRIVATE void _sg_validate(bool cond, _sg_validate_error_t err) {
if (!cond) {
_sg.validate_error = err;
SOKOL_LOG(_sg_validate_string(err));
}
}
_SOKOL_PRIVATE bool _sg_validate_end(void) {
if (_sg.validate_error != _SG_VALIDATE_SUCCESS) {
#if !defined(SOKOL_VALIDATE_NON_FATAL)
SOKOL_LOG("^^^^ VALIDATION FAILED, TERMINATING ^^^^");
SOKOL_ASSERT(false);
#endif
return false;
}
else {
return true;
}
}
#endif
_SOKOL_PRIVATE bool _sg_validate_buffer_desc(const sg_buffer_desc* desc) {
#if !defined(SOKOL_DEBUG)
_SOKOL_UNUSED(desc);
return true;
#else
SOKOL_ASSERT(desc);
SOKOL_VALIDATE_BEGIN();
SOKOL_VALIDATE(desc->_start_canary == 0, _SG_VALIDATE_BUFFERDESC_CANARY);
SOKOL_VALIDATE(desc->_end_canary == 0, _SG_VALIDATE_BUFFERDESC_CANARY);
SOKOL_VALIDATE(desc->size > 0, _SG_VALIDATE_BUFFERDESC_SIZE);
bool injected = (0 != desc->gl_buffers[0]) ||
(0 != desc->mtl_buffers[0]) ||
(0 != desc->d3d11_buffer) ||
(0 != desc->wgpu_buffer);
if (!injected && (desc->usage == SG_USAGE_IMMUTABLE)) {
SOKOL_VALIDATE((0 != desc->data.ptr) && (desc->data.size > 0), _SG_VALIDATE_BUFFERDESC_DATA);
SOKOL_VALIDATE(desc->size == desc->data.size, _SG_VALIDATE_BUFFERDESC_DATA_SIZE);
}
else {
SOKOL_VALIDATE(0 == desc->data.ptr, _SG_VALIDATE_BUFFERDESC_NO_DATA);
}
return SOKOL_VALIDATE_END();
#endif
}
_SOKOL_PRIVATE bool _sg_validate_image_desc(const sg_image_desc* desc) {
#if !defined(SOKOL_DEBUG)
_SOKOL_UNUSED(desc);
return true;
#else
SOKOL_ASSERT(desc);
SOKOL_VALIDATE_BEGIN();
SOKOL_VALIDATE(desc->_start_canary == 0, _SG_VALIDATE_IMAGEDESC_CANARY);
SOKOL_VALIDATE(desc->_end_canary == 0, _SG_VALIDATE_IMAGEDESC_CANARY);
SOKOL_VALIDATE(desc->width > 0, _SG_VALIDATE_IMAGEDESC_WIDTH);
SOKOL_VALIDATE(desc->height > 0, _SG_VALIDATE_IMAGEDESC_HEIGHT);
const sg_pixel_format fmt = desc->pixel_format;
const sg_usage usage = desc->usage;
const bool injected = (0 != desc->gl_textures[0]) ||
(0 != desc->mtl_textures[0]) ||
(0 != desc->d3d11_texture) ||
(0 != desc->wgpu_texture);
if (desc->render_target) {
SOKOL_ASSERT(((int)fmt >= 0) && ((int)fmt < _SG_PIXELFORMAT_NUM));
SOKOL_VALIDATE(_sg.formats[fmt].render, _SG_VALIDATE_IMAGEDESC_RT_PIXELFORMAT);
/* on GLES2, sample count for render targets is completely ignored */
#if defined(SOKOL_GLES2) || defined(SOKOL_GLES3)
if (!_sg.gl.gles2) {
#endif
if (desc->sample_count > 1) {
SOKOL_VALIDATE(_sg.features.msaa_render_targets && _sg.formats[fmt].msaa, _SG_VALIDATE_IMAGEDESC_NO_MSAA_RT_SUPPORT);
}
#if defined(SOKOL_GLES2) || defined(SOKOL_GLES3)
}
#endif
SOKOL_VALIDATE(usage == SG_USAGE_IMMUTABLE, _SG_VALIDATE_IMAGEDESC_RT_IMMUTABLE);
SOKOL_VALIDATE(desc->data.subimage[0][0].ptr==0, _SG_VALIDATE_IMAGEDESC_RT_NO_DATA);
}
else {
SOKOL_VALIDATE(desc->sample_count <= 1, _SG_VALIDATE_IMAGEDESC_MSAA_BUT_NO_RT);
const bool valid_nonrt_fmt = !_sg_is_valid_rendertarget_depth_format(fmt);
SOKOL_VALIDATE(valid_nonrt_fmt, _SG_VALIDATE_IMAGEDESC_NONRT_PIXELFORMAT);
/* FIXME: should use the same "expected size" computation as in _sg_validate_update_image() here */
if (!injected && (usage == SG_USAGE_IMMUTABLE)) {
const int num_faces = desc->type == SG_IMAGETYPE_CUBE ? 6:1;
const int num_mips = desc->num_mipmaps;
for (int face_index = 0; face_index < num_faces; face_index++) {
for (int mip_index = 0; mip_index < num_mips; mip_index++) {
const bool has_data = desc->data.subimage[face_index][mip_index].ptr != 0;
const bool has_size = desc->data.subimage[face_index][mip_index].size > 0;
SOKOL_VALIDATE(has_data && has_size, _SG_VALIDATE_IMAGEDESC_DATA);
}
}
}
else {
for (int face_index = 0; face_index < SG_CUBEFACE_NUM; face_index++) {
for (int mip_index = 0; mip_index < SG_MAX_MIPMAPS; mip_index++) {
const bool no_data = 0 == desc->data.subimage[face_index][mip_index].ptr;
const bool no_size = 0 == desc->data.subimage[face_index][mip_index].size;
SOKOL_VALIDATE(no_data && no_size, _SG_VALIDATE_IMAGEDESC_NO_DATA);
}
}
}
}
return SOKOL_VALIDATE_END();
#endif
}
_SOKOL_PRIVATE bool _sg_validate_shader_desc(const sg_shader_desc* desc) {
#if !defined(SOKOL_DEBUG)
_SOKOL_UNUSED(desc);
return true;
#else
SOKOL_ASSERT(desc);
SOKOL_VALIDATE_BEGIN();
SOKOL_VALIDATE(desc->_start_canary == 0, _SG_VALIDATE_SHADERDESC_CANARY);
SOKOL_VALIDATE(desc->_end_canary == 0, _SG_VALIDATE_SHADERDESC_CANARY);
#if defined(SOKOL_GLES2)
SOKOL_VALIDATE(0 != desc->attrs[0].name, _SG_VALIDATE_SHADERDESC_ATTR_NAMES);
#elif defined(SOKOL_D3D11)
SOKOL_VALIDATE(0 != desc->attrs[0].sem_name, _SG_VALIDATE_SHADERDESC_ATTR_SEMANTICS);
#endif
#if defined(SOKOL_GLCORE33) || defined(SOKOL_GLES2) || defined(SOKOL_GLES3)
/* on GL, must provide shader source code */
SOKOL_VALIDATE(0 != desc->vs.source, _SG_VALIDATE_SHADERDESC_SOURCE);
SOKOL_VALIDATE(0 != desc->fs.source, _SG_VALIDATE_SHADERDESC_SOURCE);
#elif defined(SOKOL_METAL) || defined(SOKOL_D3D11)
/* on Metal or D3D11, must provide shader source code or byte code */
SOKOL_VALIDATE((0 != desc->vs.source)||(0 != desc->vs.bytecode.ptr), _SG_VALIDATE_SHADERDESC_SOURCE_OR_BYTECODE);
SOKOL_VALIDATE((0 != desc->fs.source)||(0 != desc->fs.bytecode.ptr), _SG_VALIDATE_SHADERDESC_SOURCE_OR_BYTECODE);
#elif defined(SOKOL_WGPU)
/* on WGPU byte code must be provided */
SOKOL_VALIDATE((0 != desc->vs.bytecode.ptr), _SG_VALIDATE_SHADERDESC_BYTECODE);
SOKOL_VALIDATE((0 != desc->fs.bytecode.ptr), _SG_VALIDATE_SHADERDESC_BYTECODE);
#else
/* Dummy Backend, don't require source or bytecode */
#endif
for (int i = 0; i < SG_MAX_VERTEX_ATTRIBUTES; i++) {
if (desc->attrs[i].name) {
SOKOL_VALIDATE(strlen(desc->attrs[i].name) < _SG_STRING_SIZE, _SG_VALIDATE_SHADERDESC_ATTR_STRING_TOO_LONG);
}
if (desc->attrs[i].sem_name) {
SOKOL_VALIDATE(strlen(desc->attrs[i].sem_name) < _SG_STRING_SIZE, _SG_VALIDATE_SHADERDESC_ATTR_STRING_TOO_LONG);
}
}
/* if shader byte code, the size must also be provided */
if (0 != desc->vs.bytecode.ptr) {
SOKOL_VALIDATE(desc->vs.bytecode.size > 0, _SG_VALIDATE_SHADERDESC_NO_BYTECODE_SIZE);
}
if (0 != desc->fs.bytecode.ptr) {
SOKOL_VALIDATE(desc->fs.bytecode.size > 0, _SG_VALIDATE_SHADERDESC_NO_BYTECODE_SIZE);
}
for (int stage_index = 0; stage_index < SG_NUM_SHADER_STAGES; stage_index++) {
const sg_shader_stage_desc* stage_desc = (stage_index == 0)? &desc->vs : &desc->fs;
bool uniform_blocks_continuous = true;
for (int ub_index = 0; ub_index < SG_MAX_SHADERSTAGE_UBS; ub_index++) {
const sg_shader_uniform_block_desc* ub_desc = &stage_desc->uniform_blocks[ub_index];
if (ub_desc->size > 0) {
SOKOL_VALIDATE(uniform_blocks_continuous, _SG_VALIDATE_SHADERDESC_NO_CONT_UBS);
bool uniforms_continuous = true;
int uniform_offset = 0;
int num_uniforms = 0;
for (int u_index = 0; u_index < SG_MAX_UB_MEMBERS; u_index++) {
const sg_shader_uniform_desc* u_desc = &ub_desc->uniforms[u_index];
if (u_desc->type != SG_UNIFORMTYPE_INVALID) {
SOKOL_VALIDATE(uniforms_continuous, _SG_VALIDATE_SHADERDESC_NO_CONT_UB_MEMBERS);
#if defined(SOKOL_GLES2) || defined(SOKOL_GLES3)
SOKOL_VALIDATE(0 != u_desc->name, _SG_VALIDATE_SHADERDESC_UB_MEMBER_NAME);
#endif
const int array_count = u_desc->array_count;
uniform_offset += _sg_uniform_size(u_desc->type, array_count);
num_uniforms++;
}
else {
uniforms_continuous = false;
}
}
#if defined(SOKOL_GLCORE33) || defined(SOKOL_GLES2) || defined(SOKOL_GLES3)
SOKOL_VALIDATE((size_t)uniform_offset == ub_desc->size, _SG_VALIDATE_SHADERDESC_UB_SIZE_MISMATCH);
SOKOL_VALIDATE(num_uniforms > 0, _SG_VALIDATE_SHADERDESC_NO_UB_MEMBERS);
#endif
}
else {
uniform_blocks_continuous = false;
}
}
bool images_continuous = true;
for (int img_index = 0; img_index < SG_MAX_SHADERSTAGE_IMAGES; img_index++) {
const sg_shader_image_desc* img_desc = &stage_desc->images[img_index];
if (img_desc->image_type != _SG_IMAGETYPE_DEFAULT) {
SOKOL_VALIDATE(images_continuous, _SG_VALIDATE_SHADERDESC_NO_CONT_IMGS);
#if defined(SOKOL_GLES2)
SOKOL_VALIDATE(0 != img_desc->name, _SG_VALIDATE_SHADERDESC_IMG_NAME);
#endif
}
else {
images_continuous = false;
}
}
}
return SOKOL_VALIDATE_END();
#endif
}
_SOKOL_PRIVATE bool _sg_validate_pipeline_desc(const sg_pipeline_desc* desc) {
#if !defined(SOKOL_DEBUG)
_SOKOL_UNUSED(desc);
return true;
#else
SOKOL_ASSERT(desc);
SOKOL_VALIDATE_BEGIN();
SOKOL_VALIDATE(desc->_start_canary == 0, _SG_VALIDATE_PIPELINEDESC_CANARY);
SOKOL_VALIDATE(desc->_end_canary == 0, _SG_VALIDATE_PIPELINEDESC_CANARY);
SOKOL_VALIDATE(desc->shader.id != SG_INVALID_ID, _SG_VALIDATE_PIPELINEDESC_SHADER);
for (int buf_index = 0; buf_index < SG_MAX_SHADERSTAGE_BUFFERS; buf_index++) {
const sg_buffer_layout_desc* l_desc = &desc->layout.buffers[buf_index];
if (l_desc->stride == 0) {
continue;
}
SOKOL_VALIDATE((l_desc->stride & 3) == 0, _SG_VALIDATE_PIPELINEDESC_LAYOUT_STRIDE4);
}
SOKOL_VALIDATE(desc->layout.attrs[0].format != SG_VERTEXFORMAT_INVALID, _SG_VALIDATE_PIPELINEDESC_NO_ATTRS);
const _sg_shader_t* shd = _sg_lookup_shader(&_sg.pools, desc->shader.id);
SOKOL_VALIDATE(0 != shd, _SG_VALIDATE_PIPELINEDESC_SHADER);
if (shd) {
SOKOL_VALIDATE(shd->slot.state == SG_RESOURCESTATE_VALID, _SG_VALIDATE_PIPELINEDESC_SHADER);
bool attrs_cont = true;
for (int attr_index = 0; attr_index < SG_MAX_VERTEX_ATTRIBUTES; attr_index++) {
const sg_vertex_attr_desc* a_desc = &desc->layout.attrs[attr_index];
if (a_desc->format == SG_VERTEXFORMAT_INVALID) {
attrs_cont = false;
continue;
}
SOKOL_VALIDATE(attrs_cont, _SG_VALIDATE_PIPELINEDESC_NO_ATTRS);
SOKOL_ASSERT(a_desc->buffer_index < SG_MAX_SHADERSTAGE_BUFFERS);
#if defined(SOKOL_GLES2)
/* on GLES2, vertex attribute names must be provided */
SOKOL_VALIDATE(!_sg_strempty(&shd->gl.attrs[attr_index].name), _SG_VALIDATE_PIPELINEDESC_ATTR_NAME);
#elif defined(SOKOL_D3D11)
/* on D3D11, semantic names (and semantic indices) must be provided */
SOKOL_VALIDATE(!_sg_strempty(&shd->d3d11.attrs[attr_index].sem_name), _SG_VALIDATE_PIPELINEDESC_ATTR_SEMANTICS);
#endif
}
}
return SOKOL_VALIDATE_END();
#endif
}
_SOKOL_PRIVATE bool _sg_validate_pass_desc(const sg_pass_desc* desc) {
#if !defined(SOKOL_DEBUG)
_SOKOL_UNUSED(desc);
return true;
#else
SOKOL_ASSERT(desc);
SOKOL_VALIDATE_BEGIN();
SOKOL_VALIDATE(desc->_start_canary == 0, _SG_VALIDATE_PASSDESC_CANARY);
SOKOL_VALIDATE(desc->_end_canary == 0, _SG_VALIDATE_PASSDESC_CANARY);
bool atts_cont = true;
int width = -1, height = -1, sample_count = -1;
for (int att_index = 0; att_index < SG_MAX_COLOR_ATTACHMENTS; att_index++) {
const sg_pass_attachment_desc* att = &desc->color_attachments[att_index];
if (att->image.id == SG_INVALID_ID) {
SOKOL_VALIDATE(att_index > 0, _SG_VALIDATE_PASSDESC_NO_COLOR_ATTS);
atts_cont = false;
continue;
}
SOKOL_VALIDATE(atts_cont, _SG_VALIDATE_PASSDESC_NO_CONT_COLOR_ATTS);
const _sg_image_t* img = _sg_lookup_image(&_sg.pools, att->image.id);
SOKOL_ASSERT(img);
SOKOL_VALIDATE(img->slot.state == SG_RESOURCESTATE_VALID, _SG_VALIDATE_PASSDESC_IMAGE);
SOKOL_VALIDATE(att->mip_level < img->cmn.num_mipmaps, _SG_VALIDATE_PASSDESC_MIPLEVEL);
if (img->cmn.type == SG_IMAGETYPE_CUBE) {
SOKOL_VALIDATE(att->slice < 6, _SG_VALIDATE_PASSDESC_FACE);
}
else if (img->cmn.type == SG_IMAGETYPE_ARRAY) {
SOKOL_VALIDATE(att->slice < img->cmn.num_slices, _SG_VALIDATE_PASSDESC_LAYER);
}
else if (img->cmn.type == SG_IMAGETYPE_3D) {
SOKOL_VALIDATE(att->slice < img->cmn.num_slices, _SG_VALIDATE_PASSDESC_SLICE);
}
SOKOL_VALIDATE(img->cmn.render_target, _SG_VALIDATE_PASSDESC_IMAGE_NO_RT);
if (att_index == 0) {
width = img->cmn.width >> att->mip_level;
height = img->cmn.height >> att->mip_level;
sample_count = img->cmn.sample_count;
}
else {
SOKOL_VALIDATE(width == img->cmn.width >> att->mip_level, _SG_VALIDATE_PASSDESC_IMAGE_SIZES);
SOKOL_VALIDATE(height == img->cmn.height >> att->mip_level, _SG_VALIDATE_PASSDESC_IMAGE_SIZES);
SOKOL_VALIDATE(sample_count == img->cmn.sample_count, _SG_VALIDATE_PASSDESC_IMAGE_SAMPLE_COUNTS);
}
SOKOL_VALIDATE(_sg_is_valid_rendertarget_color_format(img->cmn.pixel_format), _SG_VALIDATE_PASSDESC_COLOR_INV_PIXELFORMAT);
}
if (desc->depth_stencil_attachment.image.id != SG_INVALID_ID) {
const sg_pass_attachment_desc* att = &desc->depth_stencil_attachment;
const _sg_image_t* img = _sg_lookup_image(&_sg.pools, att->image.id);
SOKOL_ASSERT(img);
SOKOL_VALIDATE(img->slot.state == SG_RESOURCESTATE_VALID, _SG_VALIDATE_PASSDESC_IMAGE);
SOKOL_VALIDATE(att->mip_level < img->cmn.num_mipmaps, _SG_VALIDATE_PASSDESC_MIPLEVEL);
if (img->cmn.type == SG_IMAGETYPE_CUBE) {
SOKOL_VALIDATE(att->slice < 6, _SG_VALIDATE_PASSDESC_FACE);
}
else if (img->cmn.type == SG_IMAGETYPE_ARRAY) {
SOKOL_VALIDATE(att->slice < img->cmn.num_slices, _SG_VALIDATE_PASSDESC_LAYER);
}
else if (img->cmn.type == SG_IMAGETYPE_3D) {
SOKOL_VALIDATE(att->slice < img->cmn.num_slices, _SG_VALIDATE_PASSDESC_SLICE);
}
SOKOL_VALIDATE(img->cmn.render_target, _SG_VALIDATE_PASSDESC_IMAGE_NO_RT);
SOKOL_VALIDATE(width == img->cmn.width >> att->mip_level, _SG_VALIDATE_PASSDESC_IMAGE_SIZES);
SOKOL_VALIDATE(height == img->cmn.height >> att->mip_level, _SG_VALIDATE_PASSDESC_IMAGE_SIZES);
SOKOL_VALIDATE(sample_count == img->cmn.sample_count, _SG_VALIDATE_PASSDESC_IMAGE_SAMPLE_COUNTS);
SOKOL_VALIDATE(_sg_is_valid_rendertarget_depth_format(img->cmn.pixel_format), _SG_VALIDATE_PASSDESC_DEPTH_INV_PIXELFORMAT);
}
return SOKOL_VALIDATE_END();
#endif
}
_SOKOL_PRIVATE bool _sg_validate_begin_pass(_sg_pass_t* pass) {
#if !defined(SOKOL_DEBUG)
_SOKOL_UNUSED(pass);
return true;
#else
SOKOL_VALIDATE_BEGIN();
SOKOL_VALIDATE(pass->slot.state == SG_RESOURCESTATE_VALID, _SG_VALIDATE_BEGINPASS_PASS);
for (int i = 0; i < SG_MAX_COLOR_ATTACHMENTS; i++) {
const _sg_pass_attachment_t* att = &pass->cmn.color_atts[i];
const _sg_image_t* img = _sg_pass_color_image(pass, i);
if (img) {
SOKOL_VALIDATE(img->slot.state == SG_RESOURCESTATE_VALID, _SG_VALIDATE_BEGINPASS_IMAGE);
SOKOL_VALIDATE(img->slot.id == att->image_id.id, _SG_VALIDATE_BEGINPASS_IMAGE);
}
}
const _sg_image_t* ds_img = _sg_pass_ds_image(pass);
if (ds_img) {
const _sg_pass_attachment_t* att = &pass->cmn.ds_att;
SOKOL_VALIDATE(ds_img->slot.state == SG_RESOURCESTATE_VALID, _SG_VALIDATE_BEGINPASS_IMAGE);
SOKOL_VALIDATE(ds_img->slot.id == att->image_id.id, _SG_VALIDATE_BEGINPASS_IMAGE);
}
return SOKOL_VALIDATE_END();
#endif
}
_SOKOL_PRIVATE bool _sg_validate_apply_pipeline(sg_pipeline pip_id) {
#if !defined(SOKOL_DEBUG)
_SOKOL_UNUSED(pip_id);
return true;
#else
SOKOL_VALIDATE_BEGIN();
/* the pipeline object must be alive and valid */
SOKOL_VALIDATE(pip_id.id != SG_INVALID_ID, _SG_VALIDATE_APIP_PIPELINE_VALID_ID);
const _sg_pipeline_t* pip = _sg_lookup_pipeline(&_sg.pools, pip_id.id);
SOKOL_VALIDATE(pip != 0, _SG_VALIDATE_APIP_PIPELINE_EXISTS);
if (!pip) {
return SOKOL_VALIDATE_END();
}
SOKOL_VALIDATE(pip->slot.state == SG_RESOURCESTATE_VALID, _SG_VALIDATE_APIP_PIPELINE_VALID);
/* the pipeline's shader must be alive and valid */
SOKOL_ASSERT(pip->shader);
SOKOL_VALIDATE(pip->shader->slot.id == pip->cmn.shader_id.id, _SG_VALIDATE_APIP_SHADER_EXISTS);
SOKOL_VALIDATE(pip->shader->slot.state == SG_RESOURCESTATE_VALID, _SG_VALIDATE_APIP_SHADER_VALID);
/* check that pipeline attributes match current pass attributes */
const _sg_pass_t* pass = _sg_lookup_pass(&_sg.pools, _sg.cur_pass.id);
if (pass) {
/* an offscreen pass */
SOKOL_VALIDATE(pip->cmn.color_attachment_count == pass->cmn.num_color_atts, _SG_VALIDATE_APIP_ATT_COUNT);
for (int i = 0; i < pip->cmn.color_attachment_count; i++) {
const _sg_image_t* att_img = _sg_pass_color_image(pass, i);
SOKOL_VALIDATE(pip->cmn.color_formats[i] == att_img->cmn.pixel_format, _SG_VALIDATE_APIP_COLOR_FORMAT);
SOKOL_VALIDATE(pip->cmn.sample_count == att_img->cmn.sample_count, _SG_VALIDATE_APIP_SAMPLE_COUNT);
}
const _sg_image_t* att_dsimg = _sg_pass_ds_image(pass);
if (att_dsimg) {
SOKOL_VALIDATE(pip->cmn.depth_format == att_dsimg->cmn.pixel_format, _SG_VALIDATE_APIP_DEPTH_FORMAT);
}
else {
SOKOL_VALIDATE(pip->cmn.depth_format == SG_PIXELFORMAT_NONE, _SG_VALIDATE_APIP_DEPTH_FORMAT);
}
}
else {
/* default pass */
SOKOL_VALIDATE(pip->cmn.color_attachment_count == 1, _SG_VALIDATE_APIP_ATT_COUNT);
SOKOL_VALIDATE(pip->cmn.color_formats[0] == _sg.desc.context.color_format, _SG_VALIDATE_APIP_COLOR_FORMAT);
SOKOL_VALIDATE(pip->cmn.depth_format == _sg.desc.context.depth_format, _SG_VALIDATE_APIP_DEPTH_FORMAT);
SOKOL_VALIDATE(pip->cmn.sample_count == _sg.desc.context.sample_count, _SG_VALIDATE_APIP_SAMPLE_COUNT);
}
return SOKOL_VALIDATE_END();
#endif
}
_SOKOL_PRIVATE bool _sg_validate_apply_bindings(const sg_bindings* bindings) {
#if !defined(SOKOL_DEBUG)
_SOKOL_UNUSED(bindings);
return true;
#else
SOKOL_VALIDATE_BEGIN();
/* a pipeline object must have been applied */
SOKOL_VALIDATE(_sg.cur_pipeline.id != SG_INVALID_ID, _SG_VALIDATE_ABND_PIPELINE);
const _sg_pipeline_t* pip = _sg_lookup_pipeline(&_sg.pools, _sg.cur_pipeline.id);
SOKOL_VALIDATE(pip != 0, _SG_VALIDATE_ABND_PIPELINE_EXISTS);
if (!pip) {
return SOKOL_VALIDATE_END();
}
SOKOL_VALIDATE(pip->slot.state == SG_RESOURCESTATE_VALID, _SG_VALIDATE_ABND_PIPELINE_VALID);
SOKOL_ASSERT(pip->shader && (pip->cmn.shader_id.id == pip->shader->slot.id));
/* has expected vertex buffers, and vertex buffers still exist */
for (int i = 0; i < SG_MAX_SHADERSTAGE_BUFFERS; i++) {
if (bindings->vertex_buffers[i].id != SG_INVALID_ID) {
SOKOL_VALIDATE(pip->cmn.vertex_layout_valid[i], _SG_VALIDATE_ABND_VBS);
/* buffers in vertex-buffer-slots must be of type SG_BUFFERTYPE_VERTEXBUFFER */
const _sg_buffer_t* buf = _sg_lookup_buffer(&_sg.pools, bindings->vertex_buffers[i].id);
SOKOL_VALIDATE(buf != 0, _SG_VALIDATE_ABND_VB_EXISTS);
if (buf && buf->slot.state == SG_RESOURCESTATE_VALID) {
SOKOL_VALIDATE(SG_BUFFERTYPE_VERTEXBUFFER == buf->cmn.type, _SG_VALIDATE_ABND_VB_TYPE);
SOKOL_VALIDATE(!buf->cmn.append_overflow, _SG_VALIDATE_ABND_VB_OVERFLOW);
}
}
else {
/* vertex buffer provided in a slot which has no vertex layout in pipeline */
SOKOL_VALIDATE(!pip->cmn.vertex_layout_valid[i], _SG_VALIDATE_ABND_VBS);
}
}
/* index buffer expected or not, and index buffer still exists */
if (pip->cmn.index_type == SG_INDEXTYPE_NONE) {
/* pipeline defines non-indexed rendering, but index buffer provided */
SOKOL_VALIDATE(bindings->index_buffer.id == SG_INVALID_ID, _SG_VALIDATE_ABND_IB);
}
else {
/* pipeline defines indexed rendering, but no index buffer provided */
SOKOL_VALIDATE(bindings->index_buffer.id != SG_INVALID_ID, _SG_VALIDATE_ABND_NO_IB);
}
if (bindings->index_buffer.id != SG_INVALID_ID) {
/* buffer in index-buffer-slot must be of type SG_BUFFERTYPE_INDEXBUFFER */
const _sg_buffer_t* buf = _sg_lookup_buffer(&_sg.pools, bindings->index_buffer.id);
SOKOL_VALIDATE(buf != 0, _SG_VALIDATE_ABND_IB_EXISTS);
if (buf && buf->slot.state == SG_RESOURCESTATE_VALID) {
SOKOL_VALIDATE(SG_BUFFERTYPE_INDEXBUFFER == buf->cmn.type, _SG_VALIDATE_ABND_IB_TYPE);
SOKOL_VALIDATE(!buf->cmn.append_overflow, _SG_VALIDATE_ABND_IB_OVERFLOW);
}
}
/* has expected vertex shader images */
for (int i = 0; i < SG_MAX_SHADERSTAGE_IMAGES; i++) {
_sg_shader_stage_t* stage = &pip->shader->cmn.stage[SG_SHADERSTAGE_VS];
if (bindings->vs_images[i].id != SG_INVALID_ID) {
SOKOL_VALIDATE(i < stage->num_images, _SG_VALIDATE_ABND_VS_IMGS);
const _sg_image_t* img = _sg_lookup_image(&_sg.pools, bindings->vs_images[i].id);
SOKOL_VALIDATE(img != 0, _SG_VALIDATE_ABND_VS_IMG_EXISTS);
if (img && img->slot.state == SG_RESOURCESTATE_VALID) {
SOKOL_VALIDATE(img->cmn.type == stage->images[i].image_type, _SG_VALIDATE_ABND_VS_IMG_TYPES);
}
}
else {
SOKOL_VALIDATE(i >= stage->num_images, _SG_VALIDATE_ABND_VS_IMGS);
}
}
/* has expected fragment shader images */
for (int i = 0; i < SG_MAX_SHADERSTAGE_IMAGES; i++) {
_sg_shader_stage_t* stage = &pip->shader->cmn.stage[SG_SHADERSTAGE_FS];
if (bindings->fs_images[i].id != SG_INVALID_ID) {
SOKOL_VALIDATE(i < stage->num_images, _SG_VALIDATE_ABND_FS_IMGS);
const _sg_image_t* img = _sg_lookup_image(&_sg.pools, bindings->fs_images[i].id);
SOKOL_VALIDATE(img != 0, _SG_VALIDATE_ABND_FS_IMG_EXISTS);
if (img && img->slot.state == SG_RESOURCESTATE_VALID) {
SOKOL_VALIDATE(img->cmn.type == stage->images[i].image_type, _SG_VALIDATE_ABND_FS_IMG_TYPES);
}
}
else {
SOKOL_VALIDATE(i >= stage->num_images, _SG_VALIDATE_ABND_FS_IMGS);
}
}
return SOKOL_VALIDATE_END();
#endif
}
_SOKOL_PRIVATE bool _sg_validate_apply_uniforms(sg_shader_stage stage_index, int ub_index, const sg_range* data) {
#if !defined(SOKOL_DEBUG)
_SOKOL_UNUSED(stage_index);
_SOKOL_UNUSED(ub_index);
_SOKOL_UNUSED(data);
return true;
#else
SOKOL_ASSERT((stage_index == SG_SHADERSTAGE_VS) || (stage_index == SG_SHADERSTAGE_FS));
SOKOL_ASSERT((ub_index >= 0) && (ub_index < SG_MAX_SHADERSTAGE_UBS));
SOKOL_VALIDATE_BEGIN();
SOKOL_VALIDATE(_sg.cur_pipeline.id != SG_INVALID_ID, _SG_VALIDATE_AUB_NO_PIPELINE);
const _sg_pipeline_t* pip = _sg_lookup_pipeline(&_sg.pools, _sg.cur_pipeline.id);
SOKOL_ASSERT(pip && (pip->slot.id == _sg.cur_pipeline.id));
SOKOL_ASSERT(pip->shader && (pip->shader->slot.id == pip->cmn.shader_id.id));
/* check that there is a uniform block at 'stage' and 'ub_index' */
const _sg_shader_stage_t* stage = &pip->shader->cmn.stage[stage_index];
SOKOL_VALIDATE(ub_index < stage->num_uniform_blocks, _SG_VALIDATE_AUB_NO_UB_AT_SLOT);
/* check that the provided data size doesn't exceed the uniform block size */
SOKOL_VALIDATE(data->size <= stage->uniform_blocks[ub_index].size, _SG_VALIDATE_AUB_SIZE);
return SOKOL_VALIDATE_END();
#endif
}
_SOKOL_PRIVATE bool _sg_validate_update_buffer(const _sg_buffer_t* buf, const sg_range* data) {
#if !defined(SOKOL_DEBUG)
_SOKOL_UNUSED(buf);
_SOKOL_UNUSED(data);
return true;
#else
SOKOL_ASSERT(buf && data && data->ptr);
SOKOL_VALIDATE_BEGIN();
SOKOL_VALIDATE(buf->cmn.usage != SG_USAGE_IMMUTABLE, _SG_VALIDATE_UPDATEBUF_USAGE);
SOKOL_VALIDATE(buf->cmn.size >= (int)data->size, _SG_VALIDATE_UPDATEBUF_SIZE);
SOKOL_VALIDATE(buf->cmn.update_frame_index != _sg.frame_index, _SG_VALIDATE_UPDATEBUF_ONCE);
SOKOL_VALIDATE(buf->cmn.append_frame_index != _sg.frame_index, _SG_VALIDATE_UPDATEBUF_APPEND);
return SOKOL_VALIDATE_END();
#endif
}
_SOKOL_PRIVATE bool _sg_validate_append_buffer(const _sg_buffer_t* buf, const sg_range* data) {
#if !defined(SOKOL_DEBUG)
_SOKOL_UNUSED(buf);
_SOKOL_UNUSED(data);
return true;
#else
SOKOL_ASSERT(buf && data && data->ptr);
SOKOL_VALIDATE_BEGIN();
SOKOL_VALIDATE(buf->cmn.usage != SG_USAGE_IMMUTABLE, _SG_VALIDATE_APPENDBUF_USAGE);
SOKOL_VALIDATE(buf->cmn.size >= (buf->cmn.append_pos + (int)data->size), _SG_VALIDATE_APPENDBUF_SIZE);
SOKOL_VALIDATE(buf->cmn.update_frame_index != _sg.frame_index, _SG_VALIDATE_APPENDBUF_UPDATE);
return SOKOL_VALIDATE_END();
#endif
}
_SOKOL_PRIVATE bool _sg_validate_update_image(const _sg_image_t* img, const sg_image_data* data) {
#if !defined(SOKOL_DEBUG)
_SOKOL_UNUSED(img);
_SOKOL_UNUSED(data);
return true;
#else
SOKOL_ASSERT(img && data);
SOKOL_VALIDATE_BEGIN();
SOKOL_VALIDATE(img->cmn.usage != SG_USAGE_IMMUTABLE, _SG_VALIDATE_UPDIMG_USAGE);
SOKOL_VALIDATE(img->cmn.upd_frame_index != _sg.frame_index, _SG_VALIDATE_UPDIMG_ONCE);
SOKOL_VALIDATE(!_sg_is_compressed_pixel_format(img->cmn.pixel_format), _SG_VALIDATE_UPDIMG_COMPRESSED);
const int num_faces = (img->cmn.type == SG_IMAGETYPE_CUBE) ? 6 : 1;
const int num_mips = img->cmn.num_mipmaps;
for (int face_index = 0; face_index < num_faces; face_index++) {
for (int mip_index = 0; mip_index < num_mips; mip_index++) {
SOKOL_VALIDATE(0 != data->subimage[face_index][mip_index].ptr, _SG_VALIDATE_UPDIMG_NOTENOUGHDATA);
const int mip_width = _sg_max(img->cmn.width >> mip_index, 1);
const int mip_height = _sg_max(img->cmn.height >> mip_index, 1);
const int bytes_per_slice = _sg_surface_pitch(img->cmn.pixel_format, mip_width, mip_height, 1);
const int expected_size = bytes_per_slice * img->cmn.num_slices;
SOKOL_VALIDATE(data->subimage[face_index][mip_index].size <= (size_t)expected_size, _SG_VALIDATE_UPDIMG_SIZE);
}
}
return SOKOL_VALIDATE_END();
#endif
}
/*== fill in desc default values =============================================*/
_SOKOL_PRIVATE sg_buffer_desc _sg_buffer_desc_defaults(const sg_buffer_desc* desc) {
sg_buffer_desc def = *desc;
def.type = _sg_def(def.type, SG_BUFFERTYPE_VERTEXBUFFER);
def.usage = _sg_def(def.usage, SG_USAGE_IMMUTABLE);
if (def.size == 0) {
def.size = def.data.size;
}
else if (def.data.size == 0) {
def.data.size = def.size;
}
return def;
}
_SOKOL_PRIVATE sg_image_desc _sg_image_desc_defaults(const sg_image_desc* desc) {
sg_image_desc def = *desc;
def.type = _sg_def(def.type, SG_IMAGETYPE_2D);
def.num_slices = _sg_def(def.num_slices, 1);
def.num_mipmaps = _sg_def(def.num_mipmaps, 1);
def.usage = _sg_def(def.usage, SG_USAGE_IMMUTABLE);
if (desc->render_target) {
def.pixel_format = _sg_def(def.pixel_format, _sg.desc.context.color_format);
def.sample_count = _sg_def(def.sample_count, _sg.desc.context.sample_count);
}
else {
def.pixel_format = _sg_def(def.pixel_format, SG_PIXELFORMAT_RGBA8);
def.sample_count = _sg_def(def.sample_count, 1);
}
def.min_filter = _sg_def(def.min_filter, SG_FILTER_NEAREST);
def.mag_filter = _sg_def(def.mag_filter, SG_FILTER_NEAREST);
def.wrap_u = _sg_def(def.wrap_u, SG_WRAP_REPEAT);
def.wrap_v = _sg_def(def.wrap_v, SG_WRAP_REPEAT);
def.wrap_w = _sg_def(def.wrap_w, SG_WRAP_REPEAT);
def.border_color = _sg_def(def.border_color, SG_BORDERCOLOR_OPAQUE_BLACK);
def.max_anisotropy = _sg_def(def.max_anisotropy, 1);
def.max_lod = _sg_def_flt(def.max_lod, FLT_MAX);
return def;
}
_SOKOL_PRIVATE sg_shader_desc _sg_shader_desc_defaults(const sg_shader_desc* desc) {
sg_shader_desc def = *desc;
#if defined(SOKOL_METAL)
def.vs.entry = _sg_def(def.vs.entry, "_main");
def.fs.entry = _sg_def(def.fs.entry, "_main");
#else
def.vs.entry = _sg_def(def.vs.entry, "main");
def.fs.entry = _sg_def(def.fs.entry, "main");
#endif
#if defined(SOKOL_D3D11)
if (def.vs.source) {
def.vs.d3d11_target = _sg_def(def.vs.d3d11_target, "vs_4_0");
}
if (def.fs.source) {
def.fs.d3d11_target = _sg_def(def.fs.d3d11_target, "ps_4_0");
}
#endif
for (int stage_index = 0; stage_index < SG_NUM_SHADER_STAGES; stage_index++) {
sg_shader_stage_desc* stage_desc = (stage_index == SG_SHADERSTAGE_VS)? &def.vs : &def.fs;
for (int ub_index = 0; ub_index < SG_MAX_SHADERSTAGE_UBS; ub_index++) {
sg_shader_uniform_block_desc* ub_desc = &stage_desc->uniform_blocks[ub_index];
if (0 == ub_desc->size) {
break;
}
for (int u_index = 0; u_index < SG_MAX_UB_MEMBERS; u_index++) {
sg_shader_uniform_desc* u_desc = &ub_desc->uniforms[u_index];
if (u_desc->type == SG_UNIFORMTYPE_INVALID) {
break;
}
u_desc->array_count = _sg_def(u_desc->array_count, 1);
}
}
for (int img_index = 0; img_index < SG_MAX_SHADERSTAGE_IMAGES; img_index++) {
sg_shader_image_desc* img_desc = &stage_desc->images[img_index];
if (img_desc->image_type == _SG_IMAGETYPE_DEFAULT) {
break;
}
img_desc->sampler_type = _sg_def(img_desc->sampler_type, SG_SAMPLERTYPE_FLOAT);
}
}
return def;
}
_SOKOL_PRIVATE sg_pipeline_desc _sg_pipeline_desc_defaults(const sg_pipeline_desc* desc) {
sg_pipeline_desc def = *desc;
def.primitive_type = _sg_def(def.primitive_type, SG_PRIMITIVETYPE_TRIANGLES);
def.index_type = _sg_def(def.index_type, SG_INDEXTYPE_NONE);
def.cull_mode = _sg_def(def.cull_mode, SG_CULLMODE_NONE);
def.face_winding = _sg_def(def.face_winding, SG_FACEWINDING_CW);
def.sample_count = _sg_def(def.sample_count, _sg.desc.context.sample_count);
def.stencil.front.compare = _sg_def(def.stencil.front.compare, SG_COMPAREFUNC_ALWAYS);
def.stencil.front.fail_op = _sg_def(def.stencil.front.fail_op, SG_STENCILOP_KEEP);
def.stencil.front.depth_fail_op = _sg_def(def.stencil.front.depth_fail_op, SG_STENCILOP_KEEP);
def.stencil.front.pass_op = _sg_def(def.stencil.front.pass_op, SG_STENCILOP_KEEP);
def.stencil.back.compare = _sg_def(def.stencil.back.compare, SG_COMPAREFUNC_ALWAYS);
def.stencil.back.fail_op = _sg_def(def.stencil.back.fail_op, SG_STENCILOP_KEEP);
def.stencil.back.depth_fail_op = _sg_def(def.stencil.back.depth_fail_op, SG_STENCILOP_KEEP);
def.stencil.back.pass_op = _sg_def(def.stencil.back.pass_op, SG_STENCILOP_KEEP);
def.depth.compare = _sg_def(def.depth.compare, SG_COMPAREFUNC_ALWAYS);
def.depth.pixel_format = _sg_def(def.depth.pixel_format, _sg.desc.context.depth_format);
def.color_count = _sg_def(def.color_count, 1);
if (def.color_count > SG_MAX_COLOR_ATTACHMENTS) {
def.color_count = SG_MAX_COLOR_ATTACHMENTS;
}
for (int i = 0; i < def.color_count; i++) {
sg_color_state* cs = &def.colors[i];
cs->pixel_format = _sg_def(cs->pixel_format, _sg.desc.context.color_format);
cs->write_mask = _sg_def(cs->write_mask, SG_COLORMASK_RGBA);
sg_blend_state* bs = &def.colors[i].blend;
bs->src_factor_rgb = _sg_def(bs->src_factor_rgb, SG_BLENDFACTOR_ONE);
bs->dst_factor_rgb = _sg_def(bs->dst_factor_rgb, SG_BLENDFACTOR_ZERO);
bs->op_rgb = _sg_def(bs->op_rgb, SG_BLENDOP_ADD);
bs->src_factor_alpha = _sg_def(bs->src_factor_alpha, SG_BLENDFACTOR_ONE);
bs->dst_factor_alpha = _sg_def(bs->dst_factor_alpha, SG_BLENDFACTOR_ZERO);
bs->op_alpha = _sg_def(bs->op_alpha, SG_BLENDOP_ADD);
}
for (int attr_index = 0; attr_index < SG_MAX_VERTEX_ATTRIBUTES; attr_index++) {
sg_vertex_attr_desc* a_desc = &def.layout.attrs[attr_index];
if (a_desc->format == SG_VERTEXFORMAT_INVALID) {
break;
}
SOKOL_ASSERT(a_desc->buffer_index < SG_MAX_SHADERSTAGE_BUFFERS);
sg_buffer_layout_desc* b_desc = &def.layout.buffers[a_desc->buffer_index];
b_desc->step_func = _sg_def(b_desc->step_func, SG_VERTEXSTEP_PER_VERTEX);
b_desc->step_rate = _sg_def(b_desc->step_rate, 1);
}
/* resolve vertex layout strides and offsets */
int auto_offset[SG_MAX_SHADERSTAGE_BUFFERS];
memset(auto_offset, 0, sizeof(auto_offset));
bool use_auto_offset = true;
for (int attr_index = 0; attr_index < SG_MAX_VERTEX_ATTRIBUTES; attr_index++) {
/* to use computed offsets, *all* attr offsets must be 0 */
if (def.layout.attrs[attr_index].offset != 0) {
use_auto_offset = false;
}
}
for (int attr_index = 0; attr_index < SG_MAX_VERTEX_ATTRIBUTES; attr_index++) {
sg_vertex_attr_desc* a_desc = &def.layout.attrs[attr_index];
if (a_desc->format == SG_VERTEXFORMAT_INVALID) {
break;
}
SOKOL_ASSERT(a_desc->buffer_index < SG_MAX_SHADERSTAGE_BUFFERS);
if (use_auto_offset) {
a_desc->offset = auto_offset[a_desc->buffer_index];
}
auto_offset[a_desc->buffer_index] += _sg_vertexformat_bytesize(a_desc->format);
}
/* compute vertex strides if needed */
for (int buf_index = 0; buf_index < SG_MAX_SHADERSTAGE_BUFFERS; buf_index++) {
sg_buffer_layout_desc* l_desc = &def.layout.buffers[buf_index];
if (l_desc->stride == 0) {
l_desc->stride = auto_offset[buf_index];
}
}
return def;
}
_SOKOL_PRIVATE sg_pass_desc _sg_pass_desc_defaults(const sg_pass_desc* desc) {
/* FIXME: no values to replace in sg_pass_desc? */
sg_pass_desc def = *desc;
return def;
}
/*== allocate/initialize resource private functions ==========================*/
_SOKOL_PRIVATE sg_buffer _sg_alloc_buffer(void) {
sg_buffer res;
int slot_index = _sg_pool_alloc_index(&_sg.pools.buffer_pool);
if (_SG_INVALID_SLOT_INDEX != slot_index) {
res.id = _sg_slot_alloc(&_sg.pools.buffer_pool, &_sg.pools.buffers[slot_index].slot, slot_index);
}
else {
/* pool is exhausted */
res.id = SG_INVALID_ID;
}
return res;
}
_SOKOL_PRIVATE sg_image _sg_alloc_image(void) {
sg_image res;
int slot_index = _sg_pool_alloc_index(&_sg.pools.image_pool);
if (_SG_INVALID_SLOT_INDEX != slot_index) {
res.id = _sg_slot_alloc(&_sg.pools.image_pool, &_sg.pools.images[slot_index].slot, slot_index);
}
else {
/* pool is exhausted */
res.id = SG_INVALID_ID;
}
return res;
}
_SOKOL_PRIVATE sg_shader _sg_alloc_shader(void) {
sg_shader res;
int slot_index = _sg_pool_alloc_index(&_sg.pools.shader_pool);
if (_SG_INVALID_SLOT_INDEX != slot_index) {
res.id = _sg_slot_alloc(&_sg.pools.shader_pool, &_sg.pools.shaders[slot_index].slot, slot_index);
}
else {
/* pool is exhausted */
res.id = SG_INVALID_ID;
}
return res;
}
_SOKOL_PRIVATE sg_pipeline _sg_alloc_pipeline(void) {
sg_pipeline res;
int slot_index = _sg_pool_alloc_index(&_sg.pools.pipeline_pool);
if (_SG_INVALID_SLOT_INDEX != slot_index) {
res.id =_sg_slot_alloc(&_sg.pools.pipeline_pool, &_sg.pools.pipelines[slot_index].slot, slot_index);
}
else {
/* pool is exhausted */
res.id = SG_INVALID_ID;
}
return res;
}
_SOKOL_PRIVATE sg_pass _sg_alloc_pass(void) {
sg_pass res;
int slot_index = _sg_pool_alloc_index(&_sg.pools.pass_pool);
if (_SG_INVALID_SLOT_INDEX != slot_index) {
res.id = _sg_slot_alloc(&_sg.pools.pass_pool, &_sg.pools.passes[slot_index].slot, slot_index);
}
else {
/* pool is exhausted */
res.id = SG_INVALID_ID;
}
return res;
}
_SOKOL_PRIVATE void _sg_dealloc_buffer(sg_buffer buf_id) {
SOKOL_ASSERT(buf_id.id != SG_INVALID_ID);
_sg_buffer_t* buf = _sg_lookup_buffer(&_sg.pools, buf_id.id);
SOKOL_ASSERT(buf && buf->slot.state == SG_RESOURCESTATE_ALLOC);
_sg_reset_slot(&buf->slot);
_sg_pool_free_index(&_sg.pools.buffer_pool, _sg_slot_index(buf_id.id));
}
_SOKOL_PRIVATE void _sg_dealloc_image(sg_image img_id) {
SOKOL_ASSERT(img_id.id != SG_INVALID_ID);
_sg_image_t* img = _sg_lookup_image(&_sg.pools, img_id.id);
SOKOL_ASSERT(img && img->slot.state == SG_RESOURCESTATE_ALLOC);
_sg_reset_slot(&img->slot);
_sg_pool_free_index(&_sg.pools.image_pool, _sg_slot_index(img_id.id));
}
_SOKOL_PRIVATE void _sg_dealloc_shader(sg_shader shd_id) {
SOKOL_ASSERT(shd_id.id != SG_INVALID_ID);
_sg_shader_t* shd = _sg_lookup_shader(&_sg.pools, shd_id.id);
SOKOL_ASSERT(shd && shd->slot.state == SG_RESOURCESTATE_ALLOC);
_sg_reset_slot(&shd->slot);
_sg_pool_free_index(&_sg.pools.shader_pool, _sg_slot_index(shd_id.id));
}
_SOKOL_PRIVATE void _sg_dealloc_pipeline(sg_pipeline pip_id) {
SOKOL_ASSERT(pip_id.id != SG_INVALID_ID);
_sg_pipeline_t* pip = _sg_lookup_pipeline(&_sg.pools, pip_id.id);
SOKOL_ASSERT(pip && pip->slot.state == SG_RESOURCESTATE_ALLOC);
_sg_reset_slot(&pip->slot);
_sg_pool_free_index(&_sg.pools.pipeline_pool, _sg_slot_index(pip_id.id));
}
_SOKOL_PRIVATE void _sg_dealloc_pass(sg_pass pass_id) {
SOKOL_ASSERT(pass_id.id != SG_INVALID_ID);
_sg_pass_t* pass = _sg_lookup_pass(&_sg.pools, pass_id.id);
SOKOL_ASSERT(pass && pass->slot.state == SG_RESOURCESTATE_ALLOC);
_sg_reset_slot(&pass->slot);
_sg_pool_free_index(&_sg.pools.pass_pool, _sg_slot_index(pass_id.id));
}
_SOKOL_PRIVATE void _sg_init_buffer(sg_buffer buf_id, const sg_buffer_desc* desc) {
SOKOL_ASSERT(buf_id.id != SG_INVALID_ID && desc);
_sg_buffer_t* buf = _sg_lookup_buffer(&_sg.pools, buf_id.id);
SOKOL_ASSERT(buf && buf->slot.state == SG_RESOURCESTATE_ALLOC);
buf->slot.ctx_id = _sg.active_context.id;
if (_sg_validate_buffer_desc(desc)) {
buf->slot.state = _sg_create_buffer(buf, desc);
}
else {
buf->slot.state = SG_RESOURCESTATE_FAILED;
}
SOKOL_ASSERT((buf->slot.state == SG_RESOURCESTATE_VALID)||(buf->slot.state == SG_RESOURCESTATE_FAILED));
}
_SOKOL_PRIVATE void _sg_init_image(sg_image img_id, const sg_image_desc* desc) {
SOKOL_ASSERT(img_id.id != SG_INVALID_ID && desc);
_sg_image_t* img = _sg_lookup_image(&_sg.pools, img_id.id);
SOKOL_ASSERT(img && img->slot.state == SG_RESOURCESTATE_ALLOC);
img->slot.ctx_id = _sg.active_context.id;
if (_sg_validate_image_desc(desc)) {
img->slot.state = _sg_create_image(img, desc);
}
else {
img->slot.state = SG_RESOURCESTATE_FAILED;
}
SOKOL_ASSERT((img->slot.state == SG_RESOURCESTATE_VALID)||(img->slot.state == SG_RESOURCESTATE_FAILED));
}
_SOKOL_PRIVATE void _sg_init_shader(sg_shader shd_id, const sg_shader_desc* desc) {
SOKOL_ASSERT(shd_id.id != SG_INVALID_ID && desc);
_sg_shader_t* shd = _sg_lookup_shader(&_sg.pools, shd_id.id);
SOKOL_ASSERT(shd && shd->slot.state == SG_RESOURCESTATE_ALLOC);
shd->slot.ctx_id = _sg.active_context.id;
if (_sg_validate_shader_desc(desc)) {
shd->slot.state = _sg_create_shader(shd, desc);
}
else {
shd->slot.state = SG_RESOURCESTATE_FAILED;
}
SOKOL_ASSERT((shd->slot.state == SG_RESOURCESTATE_VALID)||(shd->slot.state == SG_RESOURCESTATE_FAILED));
}
_SOKOL_PRIVATE void _sg_init_pipeline(sg_pipeline pip_id, const sg_pipeline_desc* desc) {
SOKOL_ASSERT(pip_id.id != SG_INVALID_ID && desc);
_sg_pipeline_t* pip = _sg_lookup_pipeline(&_sg.pools, pip_id.id);
SOKOL_ASSERT(pip && pip->slot.state == SG_RESOURCESTATE_ALLOC);
pip->slot.ctx_id = _sg.active_context.id;
if (_sg_validate_pipeline_desc(desc)) {
_sg_shader_t* shd = _sg_lookup_shader(&_sg.pools, desc->shader.id);
if (shd && (shd->slot.state == SG_RESOURCESTATE_VALID)) {
pip->slot.state = _sg_create_pipeline(pip, shd, desc);
}
else {
pip->slot.state = SG_RESOURCESTATE_FAILED;
}
}
else {
pip->slot.state = SG_RESOURCESTATE_FAILED;
}
SOKOL_ASSERT((pip->slot.state == SG_RESOURCESTATE_VALID)||(pip->slot.state == SG_RESOURCESTATE_FAILED));
}
_SOKOL_PRIVATE void _sg_init_pass(sg_pass pass_id, const sg_pass_desc* desc) {
SOKOL_ASSERT(pass_id.id != SG_INVALID_ID && desc);
_sg_pass_t* pass = _sg_lookup_pass(&_sg.pools, pass_id.id);
SOKOL_ASSERT(pass && pass->slot.state == SG_RESOURCESTATE_ALLOC);
pass->slot.ctx_id = _sg.active_context.id;
if (_sg_validate_pass_desc(desc)) {
/* lookup pass attachment image pointers */
_sg_image_t* att_imgs[SG_MAX_COLOR_ATTACHMENTS + 1];
for (int i = 0; i < SG_MAX_COLOR_ATTACHMENTS; i++) {
if (desc->color_attachments[i].image.id) {
att_imgs[i] = _sg_lookup_image(&_sg.pools, desc->color_attachments[i].image.id);
/* FIXME: this shouldn't be an assertion, but result in a SG_RESOURCESTATE_FAILED pass */
SOKOL_ASSERT(att_imgs[i] && att_imgs[i]->slot.state == SG_RESOURCESTATE_VALID);
}
else {
att_imgs[i] = 0;
}
}
const int ds_att_index = SG_MAX_COLOR_ATTACHMENTS;
if (desc->depth_stencil_attachment.image.id) {
att_imgs[ds_att_index] = _sg_lookup_image(&_sg.pools, desc->depth_stencil_attachment.image.id);
/* FIXME: this shouldn't be an assertion, but result in a SG_RESOURCESTATE_FAILED pass */
SOKOL_ASSERT(att_imgs[ds_att_index] && att_imgs[ds_att_index]->slot.state == SG_RESOURCESTATE_VALID);
}
else {
att_imgs[ds_att_index] = 0;
}
pass->slot.state = _sg_create_pass(pass, att_imgs, desc);
}
else {
pass->slot.state = SG_RESOURCESTATE_FAILED;
}
SOKOL_ASSERT((pass->slot.state == SG_RESOURCESTATE_VALID)||(pass->slot.state == SG_RESOURCESTATE_FAILED));
}
_SOKOL_PRIVATE bool _sg_uninit_buffer(sg_buffer buf_id) {
_sg_buffer_t* buf = _sg_lookup_buffer(&_sg.pools, buf_id.id);
if (buf) {
if (buf->slot.ctx_id == _sg.active_context.id) {
_sg_destroy_buffer(buf);
_sg_reset_buffer(buf);
return true;
}
else {
SOKOL_LOG("_sg_uninit_buffer: active context mismatch (must be same as for creation)");
_SG_TRACE_NOARGS(err_context_mismatch);
}
}
return false;
}
_SOKOL_PRIVATE bool _sg_uninit_image(sg_image img_id) {
_sg_image_t* img = _sg_lookup_image(&_sg.pools, img_id.id);
if (img) {
if (img->slot.ctx_id == _sg.active_context.id) {
_sg_destroy_image(img);
_sg_reset_image(img);
return true;
}
else {
SOKOL_LOG("_sg_uninit_image: active context mismatch (must be same as for creation)");
_SG_TRACE_NOARGS(err_context_mismatch);
}
}
return false;
}
_SOKOL_PRIVATE bool _sg_uninit_shader(sg_shader shd_id) {
_sg_shader_t* shd = _sg_lookup_shader(&_sg.pools, shd_id.id);
if (shd) {
if (shd->slot.ctx_id == _sg.active_context.id) {
_sg_destroy_shader(shd);
_sg_reset_shader(shd);
return true;
}
else {
SOKOL_LOG("_sg_uninit_shader: active context mismatch (must be same as for creation)");
_SG_TRACE_NOARGS(err_context_mismatch);
}
}
return false;
}
_SOKOL_PRIVATE bool _sg_uninit_pipeline(sg_pipeline pip_id) {
_sg_pipeline_t* pip = _sg_lookup_pipeline(&_sg.pools, pip_id.id);
if (pip) {
if (pip->slot.ctx_id == _sg.active_context.id) {
_sg_destroy_pipeline(pip);
_sg_reset_pipeline(pip);
return true;
}
else {
SOKOL_LOG("_sg_uninit_pipeline: active context mismatch (must be same as for creation)");
_SG_TRACE_NOARGS(err_context_mismatch);
}
}
return false;
}
_SOKOL_PRIVATE bool _sg_uninit_pass(sg_pass pass_id) {
_sg_pass_t* pass = _sg_lookup_pass(&_sg.pools, pass_id.id);
if (pass) {
if (pass->slot.ctx_id == _sg.active_context.id) {
_sg_destroy_pass(pass);
_sg_reset_pass(pass);
return true;
}
else {
SOKOL_LOG("_sg_uninit_pass: active context mismatch (must be same as for creation)");
_SG_TRACE_NOARGS(err_context_mismatch);
}
}
return false;
}
/*== PUBLIC API FUNCTIONS ====================================================*/
#if defined(SOKOL_METAL)
// this is ARC compatible
#if defined(__cplusplus)
#define _SG_CLEAR(type, item) { item = (type) { }; }
#else
#define _SG_CLEAR(type, item) { item = (type) { 0 }; }
#endif
#else
#define _SG_CLEAR(type, item) { memset(&item, 0, sizeof(item)); }
#endif
SOKOL_API_IMPL void sg_setup(const sg_desc* desc) {
SOKOL_ASSERT(desc);
SOKOL_ASSERT((desc->_start_canary == 0) && (desc->_end_canary == 0));
_SG_CLEAR(_sg_state_t, _sg);
_sg.desc = *desc;
/* replace zero-init items with their default values
NOTE: on WebGPU, the default color pixel format MUST be provided,
cannot be a default compile-time constant.
*/
#if defined(SOKOL_WGPU)
SOKOL_ASSERT(SG_PIXELFORMAT_NONE != _sg.desc.context.color_format);
#elif defined(SOKOL_METAL) || defined(SOKOL_D3D11)
_sg.desc.context.color_format = _sg_def(_sg.desc.context.color_format, SG_PIXELFORMAT_BGRA8);
#else
_sg.desc.context.color_format = _sg_def(_sg.desc.context.color_format, SG_PIXELFORMAT_RGBA8);
#endif
_sg.desc.context.depth_format = _sg_def(_sg.desc.context.depth_format, SG_PIXELFORMAT_DEPTH_STENCIL);
_sg.desc.context.sample_count = _sg_def(_sg.desc.context.sample_count, 1);
_sg.desc.buffer_pool_size = _sg_def(_sg.desc.buffer_pool_size, _SG_DEFAULT_BUFFER_POOL_SIZE);
_sg.desc.image_pool_size = _sg_def(_sg.desc.image_pool_size, _SG_DEFAULT_IMAGE_POOL_SIZE);
_sg.desc.shader_pool_size = _sg_def(_sg.desc.shader_pool_size, _SG_DEFAULT_SHADER_POOL_SIZE);
_sg.desc.pipeline_pool_size = _sg_def(_sg.desc.pipeline_pool_size, _SG_DEFAULT_PIPELINE_POOL_SIZE);
_sg.desc.pass_pool_size = _sg_def(_sg.desc.pass_pool_size, _SG_DEFAULT_PASS_POOL_SIZE);
_sg.desc.context_pool_size = _sg_def(_sg.desc.context_pool_size, _SG_DEFAULT_CONTEXT_POOL_SIZE);
_sg.desc.uniform_buffer_size = _sg_def(_sg.desc.uniform_buffer_size, _SG_DEFAULT_UB_SIZE);
_sg.desc.staging_buffer_size = _sg_def(_sg.desc.staging_buffer_size, _SG_DEFAULT_STAGING_SIZE);
_sg.desc.sampler_cache_size = _sg_def(_sg.desc.sampler_cache_size, _SG_DEFAULT_SAMPLER_CACHE_CAPACITY);
_sg_setup_pools(&_sg.pools, &_sg.desc);
_sg.frame_index = 1;
_sg_setup_backend(&_sg.desc);
_sg.valid = true;
sg_setup_context();
}
SOKOL_API_IMPL void sg_shutdown(void) {
/* can only delete resources for the currently set context here, if multiple
contexts are used, the app code must take care of properly releasing them
(since only the app code can switch between 3D-API contexts)
*/
if (_sg.active_context.id != SG_INVALID_ID) {
_sg_context_t* ctx = _sg_lookup_context(&_sg.pools, _sg.active_context.id);
if (ctx) {
_sg_destroy_all_resources(&_sg.pools, _sg.active_context.id);
_sg_destroy_context(ctx);
}
}
_sg_discard_backend();
_sg_discard_pools(&_sg.pools);
_sg.valid = false;
}
SOKOL_API_IMPL bool sg_isvalid(void) {
return _sg.valid;
}
SOKOL_API_IMPL sg_desc sg_query_desc(void) {
SOKOL_ASSERT(_sg.valid);
return _sg.desc;
}
SOKOL_API_IMPL sg_backend sg_query_backend(void) {
SOKOL_ASSERT(_sg.valid);
return _sg.backend;
}
SOKOL_API_IMPL sg_features sg_query_features(void) {
SOKOL_ASSERT(_sg.valid);
return _sg.features;
}
SOKOL_API_IMPL sg_limits sg_query_limits(void) {
SOKOL_ASSERT(_sg.valid);
return _sg.limits;
}
SOKOL_API_IMPL sg_pixelformat_info sg_query_pixelformat(sg_pixel_format fmt) {
SOKOL_ASSERT(_sg.valid);
int fmt_index = (int) fmt;
SOKOL_ASSERT((fmt_index > SG_PIXELFORMAT_NONE) && (fmt_index < _SG_PIXELFORMAT_NUM));
return _sg.formats[fmt_index];
}
SOKOL_API_IMPL sg_context sg_setup_context(void) {
SOKOL_ASSERT(_sg.valid);
sg_context res;
int slot_index = _sg_pool_alloc_index(&_sg.pools.context_pool);
if (_SG_INVALID_SLOT_INDEX != slot_index) {
res.id = _sg_slot_alloc(&_sg.pools.context_pool, &_sg.pools.contexts[slot_index].slot, slot_index);
_sg_context_t* ctx = _sg_context_at(&_sg.pools, res.id);
ctx->slot.state = _sg_create_context(ctx);
SOKOL_ASSERT(ctx->slot.state == SG_RESOURCESTATE_VALID);
_sg_activate_context(ctx);
}
else {
/* pool is exhausted */
res.id = SG_INVALID_ID;
}
_sg.active_context = res;
return res;
}
SOKOL_API_IMPL void sg_discard_context(sg_context ctx_id) {
SOKOL_ASSERT(_sg.valid);
_sg_destroy_all_resources(&_sg.pools, ctx_id.id);
_sg_context_t* ctx = _sg_lookup_context(&_sg.pools, ctx_id.id);
if (ctx) {
_sg_destroy_context(ctx);
_sg_reset_context(ctx);
_sg_reset_slot(&ctx->slot);
_sg_pool_free_index(&_sg.pools.context_pool, _sg_slot_index(ctx_id.id));
}
_sg.active_context.id = SG_INVALID_ID;
_sg_activate_context(0);
}
SOKOL_API_IMPL void sg_activate_context(sg_context ctx_id) {
SOKOL_ASSERT(_sg.valid);
_sg.active_context = ctx_id;
_sg_context_t* ctx = _sg_lookup_context(&_sg.pools, ctx_id.id);
/* NOTE: ctx can be 0 here if the context is no longer valid */
_sg_activate_context(ctx);
}
SOKOL_API_IMPL sg_trace_hooks sg_install_trace_hooks(const sg_trace_hooks* trace_hooks) {
SOKOL_ASSERT(_sg.valid);
SOKOL_ASSERT(trace_hooks);
_SOKOL_UNUSED(trace_hooks);
#if defined(SOKOL_TRACE_HOOKS)
sg_trace_hooks old_hooks = _sg.hooks;
_sg.hooks = *trace_hooks;
#else
static sg_trace_hooks old_hooks;
SOKOL_LOG("sg_install_trace_hooks() called, but SG_TRACE_HOOKS is not defined!");
#endif
return old_hooks;
}
SOKOL_API_IMPL sg_buffer sg_alloc_buffer(void) {
SOKOL_ASSERT(_sg.valid);
sg_buffer res = _sg_alloc_buffer();
_SG_TRACE_ARGS(alloc_buffer, res);
return res;
}
SOKOL_API_IMPL sg_image sg_alloc_image(void) {
SOKOL_ASSERT(_sg.valid);
sg_image res = _sg_alloc_image();
_SG_TRACE_ARGS(alloc_image, res);
return res;
}
SOKOL_API_IMPL sg_shader sg_alloc_shader(void) {
SOKOL_ASSERT(_sg.valid);
sg_shader res = _sg_alloc_shader();
_SG_TRACE_ARGS(alloc_shader, res);
return res;
}
SOKOL_API_IMPL sg_pipeline sg_alloc_pipeline(void) {
SOKOL_ASSERT(_sg.valid);
sg_pipeline res = _sg_alloc_pipeline();
_SG_TRACE_ARGS(alloc_pipeline, res);
return res;
}
SOKOL_API_IMPL sg_pass sg_alloc_pass(void) {
SOKOL_ASSERT(_sg.valid);
sg_pass res = _sg_alloc_pass();
_SG_TRACE_ARGS(alloc_pass, res);
return res;
}
SOKOL_API_IMPL void sg_dealloc_buffer(sg_buffer buf_id) {
SOKOL_ASSERT(_sg.valid);
_sg_dealloc_buffer(buf_id);
_SG_TRACE_ARGS(dealloc_buffer, buf_id);
}
SOKOL_API_IMPL void sg_dealloc_image(sg_image img_id) {
SOKOL_ASSERT(_sg.valid);
_sg_dealloc_image(img_id);
_SG_TRACE_ARGS(dealloc_image, img_id);
}
SOKOL_API_IMPL void sg_dealloc_shader(sg_shader shd_id) {
SOKOL_ASSERT(_sg.valid);
_sg_dealloc_shader(shd_id);
_SG_TRACE_ARGS(dealloc_shader, shd_id);
}
SOKOL_API_IMPL void sg_dealloc_pipeline(sg_pipeline pip_id) {
SOKOL_ASSERT(_sg.valid);
_sg_dealloc_pipeline(pip_id);
_SG_TRACE_ARGS(dealloc_pipeline, pip_id);
}
SOKOL_API_IMPL void sg_dealloc_pass(sg_pass pass_id) {
SOKOL_ASSERT(_sg.valid);
_sg_dealloc_pass(pass_id);
_SG_TRACE_ARGS(dealloc_pass, pass_id);
}
SOKOL_API_IMPL void sg_init_buffer(sg_buffer buf_id, const sg_buffer_desc* desc) {
SOKOL_ASSERT(_sg.valid);
sg_buffer_desc desc_def = _sg_buffer_desc_defaults(desc);
_sg_init_buffer(buf_id, &desc_def);
_SG_TRACE_ARGS(init_buffer, buf_id, &desc_def);
}
SOKOL_API_IMPL void sg_init_image(sg_image img_id, const sg_image_desc* desc) {
SOKOL_ASSERT(_sg.valid);
sg_image_desc desc_def = _sg_image_desc_defaults(desc);
_sg_init_image(img_id, &desc_def);
_SG_TRACE_ARGS(init_image, img_id, &desc_def);
}
SOKOL_API_IMPL void sg_init_shader(sg_shader shd_id, const sg_shader_desc* desc) {
SOKOL_ASSERT(_sg.valid);
sg_shader_desc desc_def = _sg_shader_desc_defaults(desc);
_sg_init_shader(shd_id, &desc_def);
_SG_TRACE_ARGS(init_shader, shd_id, &desc_def);
}
SOKOL_API_IMPL void sg_init_pipeline(sg_pipeline pip_id, const sg_pipeline_desc* desc) {
SOKOL_ASSERT(_sg.valid);
sg_pipeline_desc desc_def = _sg_pipeline_desc_defaults(desc);
_sg_init_pipeline(pip_id, &desc_def);
_SG_TRACE_ARGS(init_pipeline, pip_id, &desc_def);
}
SOKOL_API_IMPL void sg_init_pass(sg_pass pass_id, const sg_pass_desc* desc) {
SOKOL_ASSERT(_sg.valid);
sg_pass_desc desc_def = _sg_pass_desc_defaults(desc);
_sg_init_pass(pass_id, &desc_def);
_SG_TRACE_ARGS(init_pass, pass_id, &desc_def);
}
SOKOL_API_IMPL bool sg_uninit_buffer(sg_buffer buf_id) {
SOKOL_ASSERT(_sg.valid);
bool res = _sg_uninit_buffer(buf_id);
_SG_TRACE_ARGS(uninit_buffer, buf_id);
return res;
}
SOKOL_API_IMPL bool sg_uninit_image(sg_image img_id) {
SOKOL_ASSERT(_sg.valid);
bool res = _sg_uninit_image(img_id);
_SG_TRACE_ARGS(uninit_image, img_id);
return res;
}
SOKOL_API_IMPL bool sg_uninit_shader(sg_shader shd_id) {
SOKOL_ASSERT(_sg.valid);
bool res = _sg_uninit_shader(shd_id);
_SG_TRACE_ARGS(uninit_shader, shd_id);
return res;
}
SOKOL_API_IMPL bool sg_uninit_pipeline(sg_pipeline pip_id) {
SOKOL_ASSERT(_sg.valid);
bool res = _sg_uninit_pipeline(pip_id);
_SG_TRACE_ARGS(uninit_pipeline, pip_id);
return res;
}
SOKOL_API_IMPL bool sg_uninit_pass(sg_pass pass_id) {
SOKOL_ASSERT(_sg.valid);
bool res = _sg_uninit_pass(pass_id);
_SG_TRACE_ARGS(uninit_pass, pass_id);
return res;
}
/*-- set allocated resource to failed state ----------------------------------*/
SOKOL_API_IMPL void sg_fail_buffer(sg_buffer buf_id) {
SOKOL_ASSERT(_sg.valid);
SOKOL_ASSERT(buf_id.id != SG_INVALID_ID);
_sg_buffer_t* buf = _sg_lookup_buffer(&_sg.pools, buf_id.id);
SOKOL_ASSERT(buf && buf->slot.state == SG_RESOURCESTATE_ALLOC);
buf->slot.ctx_id = _sg.active_context.id;
buf->slot.state = SG_RESOURCESTATE_FAILED;
_SG_TRACE_ARGS(fail_buffer, buf_id);
}
SOKOL_API_IMPL void sg_fail_image(sg_image img_id) {
SOKOL_ASSERT(_sg.valid);
SOKOL_ASSERT(img_id.id != SG_INVALID_ID);
_sg_image_t* img = _sg_lookup_image(&_sg.pools, img_id.id);
SOKOL_ASSERT(img && img->slot.state == SG_RESOURCESTATE_ALLOC);
img->slot.ctx_id = _sg.active_context.id;
img->slot.state = SG_RESOURCESTATE_FAILED;
_SG_TRACE_ARGS(fail_image, img_id);
}
SOKOL_API_IMPL void sg_fail_shader(sg_shader shd_id) {
SOKOL_ASSERT(_sg.valid);
SOKOL_ASSERT(shd_id.id != SG_INVALID_ID);
_sg_shader_t* shd = _sg_lookup_shader(&_sg.pools, shd_id.id);
SOKOL_ASSERT(shd && shd->slot.state == SG_RESOURCESTATE_ALLOC);
shd->slot.ctx_id = _sg.active_context.id;
shd->slot.state = SG_RESOURCESTATE_FAILED;
_SG_TRACE_ARGS(fail_shader, shd_id);
}
SOKOL_API_IMPL void sg_fail_pipeline(sg_pipeline pip_id) {
SOKOL_ASSERT(_sg.valid);
SOKOL_ASSERT(pip_id.id != SG_INVALID_ID);
_sg_pipeline_t* pip = _sg_lookup_pipeline(&_sg.pools, pip_id.id);
SOKOL_ASSERT(pip && pip->slot.state == SG_RESOURCESTATE_ALLOC);
pip->slot.ctx_id = _sg.active_context.id;
pip->slot.state = SG_RESOURCESTATE_FAILED;
_SG_TRACE_ARGS(fail_pipeline, pip_id);
}
SOKOL_API_IMPL void sg_fail_pass(sg_pass pass_id) {
SOKOL_ASSERT(_sg.valid);
SOKOL_ASSERT(pass_id.id != SG_INVALID_ID);
_sg_pass_t* pass = _sg_lookup_pass(&_sg.pools, pass_id.id);
SOKOL_ASSERT(pass && pass->slot.state == SG_RESOURCESTATE_ALLOC);
pass->slot.ctx_id = _sg.active_context.id;
pass->slot.state = SG_RESOURCESTATE_FAILED;
_SG_TRACE_ARGS(fail_pass, pass_id);
}
/*-- get resource state */
SOKOL_API_IMPL sg_resource_state sg_query_buffer_state(sg_buffer buf_id) {
SOKOL_ASSERT(_sg.valid);
_sg_buffer_t* buf = _sg_lookup_buffer(&_sg.pools, buf_id.id);
sg_resource_state res = buf ? buf->slot.state : SG_RESOURCESTATE_INVALID;
return res;
}
SOKOL_API_IMPL sg_resource_state sg_query_image_state(sg_image img_id) {
SOKOL_ASSERT(_sg.valid);
_sg_image_t* img = _sg_lookup_image(&_sg.pools, img_id.id);
sg_resource_state res = img ? img->slot.state : SG_RESOURCESTATE_INVALID;
return res;
}
SOKOL_API_IMPL sg_resource_state sg_query_shader_state(sg_shader shd_id) {
SOKOL_ASSERT(_sg.valid);
_sg_shader_t* shd = _sg_lookup_shader(&_sg.pools, shd_id.id);
sg_resource_state res = shd ? shd->slot.state : SG_RESOURCESTATE_INVALID;
return res;
}
SOKOL_API_IMPL sg_resource_state sg_query_pipeline_state(sg_pipeline pip_id) {
SOKOL_ASSERT(_sg.valid);
_sg_pipeline_t* pip = _sg_lookup_pipeline(&_sg.pools, pip_id.id);
sg_resource_state res = pip ? pip->slot.state : SG_RESOURCESTATE_INVALID;
return res;
}
SOKOL_API_IMPL sg_resource_state sg_query_pass_state(sg_pass pass_id) {
SOKOL_ASSERT(_sg.valid);
_sg_pass_t* pass = _sg_lookup_pass(&_sg.pools, pass_id.id);
sg_resource_state res = pass ? pass->slot.state : SG_RESOURCESTATE_INVALID;
return res;
}
/*-- allocate and initialize resource ----------------------------------------*/
SOKOL_API_IMPL sg_buffer sg_make_buffer(const sg_buffer_desc* desc) {
SOKOL_ASSERT(_sg.valid);
SOKOL_ASSERT(desc);
sg_buffer_desc desc_def = _sg_buffer_desc_defaults(desc);
sg_buffer buf_id = _sg_alloc_buffer();
if (buf_id.id != SG_INVALID_ID) {
_sg_init_buffer(buf_id, &desc_def);
}
else {
SOKOL_LOG("buffer pool exhausted!");
_SG_TRACE_NOARGS(err_buffer_pool_exhausted);
}
_SG_TRACE_ARGS(make_buffer, &desc_def, buf_id);
return buf_id;
}
SOKOL_API_IMPL sg_image sg_make_image(const sg_image_desc* desc) {
SOKOL_ASSERT(_sg.valid);
SOKOL_ASSERT(desc);
sg_image_desc desc_def = _sg_image_desc_defaults(desc);
sg_image img_id = _sg_alloc_image();
if (img_id.id != SG_INVALID_ID) {
_sg_init_image(img_id, &desc_def);
}
else {
SOKOL_LOG("image pool exhausted!");
_SG_TRACE_NOARGS(err_image_pool_exhausted);
}
_SG_TRACE_ARGS(make_image, &desc_def, img_id);
return img_id;
}
SOKOL_API_IMPL sg_shader sg_make_shader(const sg_shader_desc* desc) {
SOKOL_ASSERT(_sg.valid);
SOKOL_ASSERT(desc);
sg_shader_desc desc_def = _sg_shader_desc_defaults(desc);
sg_shader shd_id = _sg_alloc_shader();
if (shd_id.id != SG_INVALID_ID) {
_sg_init_shader(shd_id, &desc_def);
}
else {
SOKOL_LOG("shader pool exhausted!");
_SG_TRACE_NOARGS(err_shader_pool_exhausted);
}
_SG_TRACE_ARGS(make_shader, &desc_def, shd_id);
return shd_id;
}
SOKOL_API_IMPL sg_pipeline sg_make_pipeline(const sg_pipeline_desc* desc) {
SOKOL_ASSERT(_sg.valid);
SOKOL_ASSERT(desc);
sg_pipeline_desc desc_def = _sg_pipeline_desc_defaults(desc);
sg_pipeline pip_id = _sg_alloc_pipeline();
if (pip_id.id != SG_INVALID_ID) {
_sg_init_pipeline(pip_id, &desc_def);
}
else {
SOKOL_LOG("pipeline pool exhausted!");
_SG_TRACE_NOARGS(err_pipeline_pool_exhausted);
}
_SG_TRACE_ARGS(make_pipeline, &desc_def, pip_id);
return pip_id;
}
SOKOL_API_IMPL sg_pass sg_make_pass(const sg_pass_desc* desc) {
SOKOL_ASSERT(_sg.valid);
SOKOL_ASSERT(desc);
sg_pass_desc desc_def = _sg_pass_desc_defaults(desc);
sg_pass pass_id = _sg_alloc_pass();
if (pass_id.id != SG_INVALID_ID) {
_sg_init_pass(pass_id, &desc_def);
}
else {
SOKOL_LOG("pass pool exhausted!");
_SG_TRACE_NOARGS(err_pass_pool_exhausted);
}
_SG_TRACE_ARGS(make_pass, &desc_def, pass_id);
return pass_id;
}
/*-- destroy resource --------------------------------------------------------*/
SOKOL_API_IMPL void sg_destroy_buffer(sg_buffer buf_id) {
SOKOL_ASSERT(_sg.valid);
_SG_TRACE_ARGS(destroy_buffer, buf_id);
if (_sg_uninit_buffer(buf_id)) {
_sg_dealloc_buffer(buf_id);
}
}
SOKOL_API_IMPL void sg_destroy_image(sg_image img_id) {
SOKOL_ASSERT(_sg.valid);
_SG_TRACE_ARGS(destroy_image, img_id);
if (_sg_uninit_image(img_id)) {
_sg_dealloc_image(img_id);
}
}
SOKOL_API_IMPL void sg_destroy_shader(sg_shader shd_id) {
SOKOL_ASSERT(_sg.valid);
_SG_TRACE_ARGS(destroy_shader, shd_id);
if (_sg_uninit_shader(shd_id)) {
_sg_dealloc_shader(shd_id);
}
}
SOKOL_API_IMPL void sg_destroy_pipeline(sg_pipeline pip_id) {
SOKOL_ASSERT(_sg.valid);
_SG_TRACE_ARGS(destroy_pipeline, pip_id);
if (_sg_uninit_pipeline(pip_id)) {
_sg_dealloc_pipeline(pip_id);
}
}
SOKOL_API_IMPL void sg_destroy_pass(sg_pass pass_id) {
SOKOL_ASSERT(_sg.valid);
_SG_TRACE_ARGS(destroy_pass, pass_id);
if (_sg_uninit_pass(pass_id)) {
_sg_dealloc_pass(pass_id);
}
}
SOKOL_API_IMPL void sg_begin_default_pass(const sg_pass_action* pass_action, int width, int height) {
SOKOL_ASSERT(_sg.valid);
SOKOL_ASSERT(pass_action);
SOKOL_ASSERT((pass_action->_start_canary == 0) && (pass_action->_end_canary == 0));
sg_pass_action pa;
_sg_resolve_default_pass_action(pass_action, &pa);
_sg.cur_pass.id = SG_INVALID_ID;
_sg.pass_valid = true;
_sg_begin_pass(0, &pa, width, height);
_SG_TRACE_ARGS(begin_default_pass, pass_action, width, height);
}
SOKOL_API_IMPL void sg_begin_default_passf(const sg_pass_action* pass_action, float width, float height) {
sg_begin_default_pass(pass_action, (int)width, (int)height);
}
SOKOL_API_IMPL void sg_begin_pass(sg_pass pass_id, const sg_pass_action* pass_action) {
SOKOL_ASSERT(_sg.valid);
SOKOL_ASSERT(pass_action);
SOKOL_ASSERT((pass_action->_start_canary == 0) && (pass_action->_end_canary == 0));
_sg.cur_pass = pass_id;
_sg_pass_t* pass = _sg_lookup_pass(&_sg.pools, pass_id.id);
if (pass && _sg_validate_begin_pass(pass)) {
_sg.pass_valid = true;
sg_pass_action pa;
_sg_resolve_default_pass_action(pass_action, &pa);
const _sg_image_t* img = _sg_pass_color_image(pass, 0);
SOKOL_ASSERT(img);
const int w = img->cmn.width;
const int h = img->cmn.height;
_sg_begin_pass(pass, &pa, w, h);
_SG_TRACE_ARGS(begin_pass, pass_id, pass_action);
}
else {
_sg.pass_valid = false;
_SG_TRACE_NOARGS(err_pass_invalid);
}
}
SOKOL_API_IMPL void sg_apply_viewport(int x, int y, int width, int height, bool origin_top_left) {
SOKOL_ASSERT(_sg.valid);
if (!_sg.pass_valid) {
_SG_TRACE_NOARGS(err_pass_invalid);
return;
}
_sg_apply_viewport(x, y, width, height, origin_top_left);
_SG_TRACE_ARGS(apply_viewport, x, y, width, height, origin_top_left);
}
SOKOL_API_IMPL void sg_apply_viewportf(float x, float y, float width, float height, bool origin_top_left) {
sg_apply_viewport((int)x, (int)y, (int)width, (int)height, origin_top_left);
}
SOKOL_API_IMPL void sg_apply_scissor_rect(int x, int y, int width, int height, bool origin_top_left) {
SOKOL_ASSERT(_sg.valid);
if (!_sg.pass_valid) {
_SG_TRACE_NOARGS(err_pass_invalid);
return;
}
_sg_apply_scissor_rect(x, y, width, height, origin_top_left);
_SG_TRACE_ARGS(apply_scissor_rect, x, y, width, height, origin_top_left);
}
SOKOL_API_IMPL void sg_apply_scissor_rectf(float x, float y, float width, float height, bool origin_top_left) {
sg_apply_scissor_rect((int)x, (int)y, (int)width, (int)height, origin_top_left);
}
SOKOL_API_IMPL void sg_apply_pipeline(sg_pipeline pip_id) {
SOKOL_ASSERT(_sg.valid);
_sg.bindings_valid = false;
if (!_sg_validate_apply_pipeline(pip_id)) {
_sg.next_draw_valid = false;
_SG_TRACE_NOARGS(err_draw_invalid);
return;
}
if (!_sg.pass_valid) {
_SG_TRACE_NOARGS(err_pass_invalid);
return;
}
_sg.cur_pipeline = pip_id;
_sg_pipeline_t* pip = _sg_lookup_pipeline(&_sg.pools, pip_id.id);
SOKOL_ASSERT(pip);
_sg.next_draw_valid = (SG_RESOURCESTATE_VALID == pip->slot.state);
SOKOL_ASSERT(pip->shader && (pip->shader->slot.id == pip->cmn.shader_id.id));
_sg_apply_pipeline(pip);
_SG_TRACE_ARGS(apply_pipeline, pip_id);
}
SOKOL_API_IMPL void sg_apply_bindings(const sg_bindings* bindings) {
SOKOL_ASSERT(_sg.valid);
SOKOL_ASSERT(bindings);
SOKOL_ASSERT((bindings->_start_canary == 0) && (bindings->_end_canary==0));
if (!_sg_validate_apply_bindings(bindings)) {
_sg.next_draw_valid = false;
_SG_TRACE_NOARGS(err_draw_invalid);
return;
}
_sg.bindings_valid = true;
_sg_pipeline_t* pip = _sg_lookup_pipeline(&_sg.pools, _sg.cur_pipeline.id);
SOKOL_ASSERT(pip);
_sg_buffer_t* vbs[SG_MAX_SHADERSTAGE_BUFFERS] = { 0 };
int num_vbs = 0;
for (int i = 0; i < SG_MAX_SHADERSTAGE_BUFFERS; i++, num_vbs++) {
if (bindings->vertex_buffers[i].id) {
vbs[i] = _sg_lookup_buffer(&_sg.pools, bindings->vertex_buffers[i].id);
SOKOL_ASSERT(vbs[i]);
_sg.next_draw_valid &= (SG_RESOURCESTATE_VALID == vbs[i]->slot.state);
_sg.next_draw_valid &= !vbs[i]->cmn.append_overflow;
}
else {
break;
}
}
_sg_buffer_t* ib = 0;
if (bindings->index_buffer.id) {
ib = _sg_lookup_buffer(&_sg.pools, bindings->index_buffer.id);
SOKOL_ASSERT(ib);
_sg.next_draw_valid &= (SG_RESOURCESTATE_VALID == ib->slot.state);
_sg.next_draw_valid &= !ib->cmn.append_overflow;
}
_sg_image_t* vs_imgs[SG_MAX_SHADERSTAGE_IMAGES] = { 0 };
int num_vs_imgs = 0;
for (int i = 0; i < SG_MAX_SHADERSTAGE_IMAGES; i++, num_vs_imgs++) {
if (bindings->vs_images[i].id) {
vs_imgs[i] = _sg_lookup_image(&_sg.pools, bindings->vs_images[i].id);
SOKOL_ASSERT(vs_imgs[i]);
_sg.next_draw_valid &= (SG_RESOURCESTATE_VALID == vs_imgs[i]->slot.state);
}
else {
break;
}
}
_sg_image_t* fs_imgs[SG_MAX_SHADERSTAGE_IMAGES] = { 0 };
int num_fs_imgs = 0;
for (int i = 0; i < SG_MAX_SHADERSTAGE_IMAGES; i++, num_fs_imgs++) {
if (bindings->fs_images[i].id) {
fs_imgs[i] = _sg_lookup_image(&_sg.pools, bindings->fs_images[i].id);
SOKOL_ASSERT(fs_imgs[i]);
_sg.next_draw_valid &= (SG_RESOURCESTATE_VALID == fs_imgs[i]->slot.state);
}
else {
break;
}
}
if (_sg.next_draw_valid) {
const int* vb_offsets = bindings->vertex_buffer_offsets;
int ib_offset = bindings->index_buffer_offset;
_sg_apply_bindings(pip, vbs, vb_offsets, num_vbs, ib, ib_offset, vs_imgs, num_vs_imgs, fs_imgs, num_fs_imgs);
_SG_TRACE_ARGS(apply_bindings, bindings);
}
else {
_SG_TRACE_NOARGS(err_draw_invalid);
}
}
SOKOL_API_IMPL void sg_apply_uniforms(sg_shader_stage stage, int ub_index, const sg_range* data) {
SOKOL_ASSERT(_sg.valid);
SOKOL_ASSERT((stage == SG_SHADERSTAGE_VS) || (stage == SG_SHADERSTAGE_FS));
SOKOL_ASSERT((ub_index >= 0) && (ub_index < SG_MAX_SHADERSTAGE_UBS));
SOKOL_ASSERT(data && data->ptr && (data->size > 0));
if (!_sg_validate_apply_uniforms(stage, ub_index, data)) {
_sg.next_draw_valid = false;
_SG_TRACE_NOARGS(err_draw_invalid);
return;
}
if (!_sg.pass_valid) {
_SG_TRACE_NOARGS(err_pass_invalid);
return;
}
if (!_sg.next_draw_valid) {
_SG_TRACE_NOARGS(err_draw_invalid);
}
_sg_apply_uniforms(stage, ub_index, data);
_SG_TRACE_ARGS(apply_uniforms, stage, ub_index, data);
}
SOKOL_API_IMPL void sg_draw(int base_element, int num_elements, int num_instances) {
SOKOL_ASSERT(_sg.valid);
SOKOL_ASSERT(base_element >= 0);
SOKOL_ASSERT(num_elements >= 0);
SOKOL_ASSERT(num_instances >= 0);
#if defined(SOKOL_DEBUG)
if (!_sg.bindings_valid) {
SOKOL_LOG("attempting to draw without resource bindings");
}
#endif
if (!_sg.pass_valid) {
_SG_TRACE_NOARGS(err_pass_invalid);
return;
}
if (!_sg.next_draw_valid) {
_SG_TRACE_NOARGS(err_draw_invalid);
return;
}
if (!_sg.bindings_valid) {
_SG_TRACE_NOARGS(err_bindings_invalid);
return;
}
/* attempting to draw with zero elements or instances is not technically an
error, but might be handled as an error in the backend API (e.g. on Metal)
*/
if ((0 == num_elements) || (0 == num_instances)) {
_SG_TRACE_NOARGS(err_draw_invalid);
return;
}
_sg_draw(base_element, num_elements, num_instances);
_SG_TRACE_ARGS(draw, base_element, num_elements, num_instances);
}
SOKOL_API_IMPL void sg_end_pass(void) {
SOKOL_ASSERT(_sg.valid);
if (!_sg.pass_valid) {
_SG_TRACE_NOARGS(err_pass_invalid);
return;
}
_sg_end_pass();
_sg.cur_pass.id = SG_INVALID_ID;
_sg.cur_pipeline.id = SG_INVALID_ID;
_sg.pass_valid = false;
_SG_TRACE_NOARGS(end_pass);
}
SOKOL_API_IMPL void sg_commit(void) {
SOKOL_ASSERT(_sg.valid);
_sg_commit();
_SG_TRACE_NOARGS(commit);
_sg.frame_index++;
}
SOKOL_API_IMPL void sg_reset_state_cache(void) {
SOKOL_ASSERT(_sg.valid);
_sg_reset_state_cache();
_SG_TRACE_NOARGS(reset_state_cache);
}
SOKOL_API_IMPL void sg_update_buffer(sg_buffer buf_id, const sg_range* data) {
SOKOL_ASSERT(_sg.valid);
SOKOL_ASSERT(data && data->ptr && (data->size > 0));
_sg_buffer_t* buf = _sg_lookup_buffer(&_sg.pools, buf_id.id);
if ((data->size > 0) && buf && (buf->slot.state == SG_RESOURCESTATE_VALID)) {
if (_sg_validate_update_buffer(buf, data)) {
SOKOL_ASSERT(data->size <= (size_t)buf->cmn.size);
/* only one update allowed per buffer and frame */
SOKOL_ASSERT(buf->cmn.update_frame_index != _sg.frame_index);
/* update and append on same buffer in same frame not allowed */
SOKOL_ASSERT(buf->cmn.append_frame_index != _sg.frame_index);
_sg_update_buffer(buf, data);
buf->cmn.update_frame_index = _sg.frame_index;
}
}
_SG_TRACE_ARGS(update_buffer, buf_id, data);
}
SOKOL_API_IMPL int sg_append_buffer(sg_buffer buf_id, const sg_range* data) {
SOKOL_ASSERT(_sg.valid);
SOKOL_ASSERT(data && data->ptr);
_sg_buffer_t* buf = _sg_lookup_buffer(&_sg.pools, buf_id.id);
int result;
if (buf) {
/* rewind append cursor in a new frame */
if (buf->cmn.append_frame_index != _sg.frame_index) {
buf->cmn.append_pos = 0;
buf->cmn.append_overflow = false;
}
if ((buf->cmn.append_pos + _sg_roundup((int)data->size, 4)) > buf->cmn.size) {
buf->cmn.append_overflow = true;
}
const int start_pos = buf->cmn.append_pos;
if (buf->slot.state == SG_RESOURCESTATE_VALID) {
if (_sg_validate_append_buffer(buf, data)) {
if (!buf->cmn.append_overflow && (data->size > 0)) {
/* update and append on same buffer in same frame not allowed */
SOKOL_ASSERT(buf->cmn.update_frame_index != _sg.frame_index);
int copied_num_bytes = _sg_append_buffer(buf, data, buf->cmn.append_frame_index != _sg.frame_index);
buf->cmn.append_pos += copied_num_bytes;
buf->cmn.append_frame_index = _sg.frame_index;
}
}
}
result = start_pos;
}
else {
/* FIXME: should we return -1 here? */
result = 0;
}
_SG_TRACE_ARGS(append_buffer, buf_id, data, result);
return result;
}
SOKOL_API_IMPL bool sg_query_buffer_overflow(sg_buffer buf_id) {
SOKOL_ASSERT(_sg.valid);
_sg_buffer_t* buf = _sg_lookup_buffer(&_sg.pools, buf_id.id);
bool result = buf ? buf->cmn.append_overflow : false;
return result;
}
SOKOL_API_IMPL void sg_update_image(sg_image img_id, const sg_image_data* data) {
SOKOL_ASSERT(_sg.valid);
_sg_image_t* img = _sg_lookup_image(&_sg.pools, img_id.id);
if (img && img->slot.state == SG_RESOURCESTATE_VALID) {
if (_sg_validate_update_image(img, data)) {
SOKOL_ASSERT(img->cmn.upd_frame_index != _sg.frame_index);
_sg_update_image(img, data);
img->cmn.upd_frame_index = _sg.frame_index;
}
}
_SG_TRACE_ARGS(update_image, img_id, data);
}
SOKOL_API_IMPL void sg_push_debug_group(const char* name) {
SOKOL_ASSERT(_sg.valid);
SOKOL_ASSERT(name);
_SOKOL_UNUSED(name);
_SG_TRACE_ARGS(push_debug_group, name);
}
SOKOL_API_IMPL void sg_pop_debug_group(void) {
SOKOL_ASSERT(_sg.valid);
_SG_TRACE_NOARGS(pop_debug_group);
}
SOKOL_API_IMPL sg_buffer_info sg_query_buffer_info(sg_buffer buf_id) {
SOKOL_ASSERT(_sg.valid);
sg_buffer_info info;
memset(&info, 0, sizeof(info));
const _sg_buffer_t* buf = _sg_lookup_buffer(&_sg.pools, buf_id.id);
if (buf) {
info.slot.state = buf->slot.state;
info.slot.res_id = buf->slot.id;
info.slot.ctx_id = buf->slot.ctx_id;
info.update_frame_index = buf->cmn.update_frame_index;
info.append_frame_index = buf->cmn.append_frame_index;
info.append_pos = buf->cmn.append_pos;
info.append_overflow = buf->cmn.append_overflow;
#if defined(SOKOL_D3D11)
info.num_slots = 1;
info.active_slot = 0;
#else
info.num_slots = buf->cmn.num_slots;
info.active_slot = buf->cmn.active_slot;
#endif
}
return info;
}
SOKOL_API_IMPL sg_image_info sg_query_image_info(sg_image img_id) {
SOKOL_ASSERT(_sg.valid);
sg_image_info info;
memset(&info, 0, sizeof(info));
const _sg_image_t* img = _sg_lookup_image(&_sg.pools, img_id.id);
if (img) {
info.slot.state = img->slot.state;
info.slot.res_id = img->slot.id;
info.slot.ctx_id = img->slot.ctx_id;
#if defined(SOKOL_D3D11)
info.num_slots = 1;
info.active_slot = 0;
#else
info.num_slots = img->cmn.num_slots;
info.active_slot = img->cmn.active_slot;
#endif
info.width = img->cmn.width;
info.height = img->cmn.height;
}
return info;
}
SOKOL_API_IMPL sg_shader_info sg_query_shader_info(sg_shader shd_id) {
SOKOL_ASSERT(_sg.valid);
sg_shader_info info;
memset(&info, 0, sizeof(info));
const _sg_shader_t* shd = _sg_lookup_shader(&_sg.pools, shd_id.id);
if (shd) {
info.slot.state = shd->slot.state;
info.slot.res_id = shd->slot.id;
info.slot.ctx_id = shd->slot.ctx_id;
}
return info;
}
SOKOL_API_IMPL sg_pipeline_info sg_query_pipeline_info(sg_pipeline pip_id) {
SOKOL_ASSERT(_sg.valid);
sg_pipeline_info info;
memset(&info, 0, sizeof(info));
const _sg_pipeline_t* pip = _sg_lookup_pipeline(&_sg.pools, pip_id.id);
if (pip) {
info.slot.state = pip->slot.state;
info.slot.res_id = pip->slot.id;
info.slot.ctx_id = pip->slot.ctx_id;
}
return info;
}
SOKOL_API_IMPL sg_pass_info sg_query_pass_info(sg_pass pass_id) {
SOKOL_ASSERT(_sg.valid);
sg_pass_info info;
memset(&info, 0, sizeof(info));
const _sg_pass_t* pass = _sg_lookup_pass(&_sg.pools, pass_id.id);
if (pass) {
info.slot.state = pass->slot.state;
info.slot.res_id = pass->slot.id;
info.slot.ctx_id = pass->slot.ctx_id;
}
return info;
}
SOKOL_API_IMPL sg_buffer_desc sg_query_buffer_defaults(const sg_buffer_desc* desc) {
SOKOL_ASSERT(_sg.valid && desc);
return _sg_buffer_desc_defaults(desc);
}
SOKOL_API_IMPL sg_image_desc sg_query_image_defaults(const sg_image_desc* desc) {
SOKOL_ASSERT(_sg.valid && desc);
return _sg_image_desc_defaults(desc);
}
SOKOL_API_IMPL sg_shader_desc sg_query_shader_defaults(const sg_shader_desc* desc) {
SOKOL_ASSERT(_sg.valid && desc);
return _sg_shader_desc_defaults(desc);
}
SOKOL_API_IMPL sg_pipeline_desc sg_query_pipeline_defaults(const sg_pipeline_desc* desc) {
SOKOL_ASSERT(_sg.valid && desc);
return _sg_pipeline_desc_defaults(desc);
}
SOKOL_API_IMPL sg_pass_desc sg_query_pass_defaults(const sg_pass_desc* desc) {
SOKOL_ASSERT(_sg.valid && desc);
return _sg_pass_desc_defaults(desc);
}
SOKOL_API_IMPL const void* sg_d3d11_device(void) {
#if defined(SOKOL_D3D11)
return (const void*) _sg.d3d11.dev;
#else
return 0;
#endif
}
SOKOL_API_IMPL const void* sg_mtl_device(void) {
#if defined(SOKOL_METAL)
if (nil != _sg.mtl.device) {
return (__bridge const void*) _sg.mtl.device;
}
else {
return 0;
}
#else
return 0;
#endif
}
SOKOL_API_IMPL const void* sg_mtl_render_command_encoder(void) {
#if defined(SOKOL_METAL)
if (nil != _sg.mtl.cmd_encoder) {
return (__bridge const void*) _sg.mtl.cmd_encoder;
}
else {
return 0;
}
#else
return 0;
#endif
}
#ifdef _MSC_VER
#pragma warning(pop)
#endif
#endif /* SOKOL_GFX_IMPL */
|