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
|
/*
* Copyright 2011-2016 Branimir Karadzic. All rights reserved.
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
*/
#include "bgfx_p.h"
#include "image.h"
namespace bgfx
{
static const ImageBlockInfo s_imageBlockInfo[] =
{
// +------------------------------- bits per pixel
// | +---------------------------- block width
// | | +------------------------- block height
// | | | +--------------------- block size
// | | | | +------------------ min blocks x
// | | | | | +--------------- min blocks y
// | | | | | | +----------- depth bits
// | | | | | | | +-------- stencil bits
// | | | | | | | | +----- encoding type
// | | | | | | | | |
{ 4, 4, 4, 8, 1, 1, 0, 0, uint8_t(EncodingType::Unorm) }, // BC1
{ 8, 4, 4, 16, 1, 1, 0, 0, uint8_t(EncodingType::Unorm) }, // BC2
{ 8, 4, 4, 16, 1, 1, 0, 0, uint8_t(EncodingType::Unorm) }, // BC3
{ 4, 4, 4, 8, 1, 1, 0, 0, uint8_t(EncodingType::Unorm) }, // BC4
{ 8, 4, 4, 16, 1, 1, 0, 0, uint8_t(EncodingType::Unorm) }, // BC5
{ 8, 4, 4, 16, 1, 1, 0, 0, uint8_t(EncodingType::Unorm) }, // BC6H
{ 8, 4, 4, 16, 1, 1, 0, 0, uint8_t(EncodingType::Unorm) }, // BC7
{ 4, 4, 4, 8, 1, 1, 0, 0, uint8_t(EncodingType::Unorm) }, // ETC1
{ 4, 4, 4, 8, 1, 1, 0, 0, uint8_t(EncodingType::Unorm) }, // ETC2
{ 8, 4, 4, 16, 1, 1, 0, 0, uint8_t(EncodingType::Unorm) }, // ETC2A
{ 4, 4, 4, 8, 1, 1, 0, 0, uint8_t(EncodingType::Unorm) }, // ETC2A1
{ 2, 8, 4, 8, 2, 2, 0, 0, uint8_t(EncodingType::Unorm) }, // PTC12
{ 4, 4, 4, 8, 2, 2, 0, 0, uint8_t(EncodingType::Unorm) }, // PTC14
{ 2, 8, 4, 8, 2, 2, 0, 0, uint8_t(EncodingType::Unorm) }, // PTC12A
{ 4, 4, 4, 8, 2, 2, 0, 0, uint8_t(EncodingType::Unorm) }, // PTC14A
{ 2, 8, 4, 8, 2, 2, 0, 0, uint8_t(EncodingType::Unorm) }, // PTC22
{ 4, 4, 4, 8, 2, 2, 0, 0, uint8_t(EncodingType::Unorm) }, // PTC24
{ 0, 0, 0, 0, 0, 0, 0, 0, uint8_t(EncodingType::Count) }, // Unknown
{ 1, 8, 1, 1, 1, 1, 0, 0, uint8_t(EncodingType::Unorm) }, // R1
{ 8, 1, 1, 1, 1, 1, 0, 0, uint8_t(EncodingType::Unorm) }, // A8
{ 8, 1, 1, 1, 1, 1, 0, 0, uint8_t(EncodingType::Unorm) }, // R8
{ 8, 1, 1, 1, 1, 1, 0, 0, uint8_t(EncodingType::Int ) }, // R8I
{ 8, 1, 1, 1, 1, 1, 0, 0, uint8_t(EncodingType::Uint ) }, // R8U
{ 8, 1, 1, 1, 1, 1, 0, 0, uint8_t(EncodingType::Snorm) }, // R8S
{ 16, 1, 1, 2, 1, 1, 0, 0, uint8_t(EncodingType::Unorm) }, // R16
{ 16, 1, 1, 2, 1, 1, 0, 0, uint8_t(EncodingType::Int ) }, // R16I
{ 16, 1, 1, 2, 1, 1, 0, 0, uint8_t(EncodingType::Uint ) }, // R16U
{ 16, 1, 1, 2, 1, 1, 0, 0, uint8_t(EncodingType::Float) }, // R16F
{ 16, 1, 1, 2, 1, 1, 0, 0, uint8_t(EncodingType::Snorm) }, // R16S
{ 32, 1, 1, 4, 1, 1, 0, 0, uint8_t(EncodingType::Int ) }, // R32I
{ 32, 1, 1, 4, 1, 1, 0, 0, uint8_t(EncodingType::Uint ) }, // R32U
{ 32, 1, 1, 4, 1, 1, 0, 0, uint8_t(EncodingType::Float) }, // R32F
{ 16, 1, 1, 2, 1, 1, 0, 0, uint8_t(EncodingType::Unorm) }, // RG8
{ 16, 1, 1, 2, 1, 1, 0, 0, uint8_t(EncodingType::Int ) }, // RG8I
{ 16, 1, 1, 2, 1, 1, 0, 0, uint8_t(EncodingType::Uint ) }, // RG8U
{ 16, 1, 1, 2, 1, 1, 0, 0, uint8_t(EncodingType::Snorm) }, // RG8S
{ 32, 1, 1, 4, 1, 1, 0, 0, uint8_t(EncodingType::Unorm) }, // RG16
{ 32, 1, 1, 4, 1, 1, 0, 0, uint8_t(EncodingType::Int ) }, // RG16I
{ 32, 1, 1, 4, 1, 1, 0, 0, uint8_t(EncodingType::Uint ) }, // RG16U
{ 32, 1, 1, 4, 1, 1, 0, 0, uint8_t(EncodingType::Float) }, // RG16F
{ 32, 1, 1, 4, 1, 1, 0, 0, uint8_t(EncodingType::Snorm) }, // RG16S
{ 64, 1, 1, 8, 1, 1, 0, 0, uint8_t(EncodingType::Int ) }, // RG32I
{ 64, 1, 1, 8, 1, 1, 0, 0, uint8_t(EncodingType::Uint ) }, // RG32U
{ 64, 1, 1, 8, 1, 1, 0, 0, uint8_t(EncodingType::Float) }, // RG32F
{ 32, 1, 1, 4, 1, 1, 0, 0, uint8_t(EncodingType::Float) }, // RGB9E5F
{ 32, 1, 1, 4, 1, 1, 0, 0, uint8_t(EncodingType::Unorm) }, // BGRA8
{ 32, 1, 1, 4, 1, 1, 0, 0, uint8_t(EncodingType::Unorm) }, // RGBA8
{ 32, 1, 1, 4, 1, 1, 0, 0, uint8_t(EncodingType::Int ) }, // RGBA8I
{ 32, 1, 1, 4, 1, 1, 0, 0, uint8_t(EncodingType::Uint ) }, // RGBA8U
{ 32, 1, 1, 4, 1, 1, 0, 0, uint8_t(EncodingType::Snorm) }, // RGBA8S
{ 64, 1, 1, 8, 1, 1, 0, 0, uint8_t(EncodingType::Unorm) }, // RGBA16
{ 64, 1, 1, 8, 1, 1, 0, 0, uint8_t(EncodingType::Int ) }, // RGBA16I
{ 64, 1, 1, 8, 1, 1, 0, 0, uint8_t(EncodingType::Uint ) }, // RGBA16U
{ 64, 1, 1, 8, 1, 1, 0, 0, uint8_t(EncodingType::Float) }, // RGBA16F
{ 64, 1, 1, 8, 1, 1, 0, 0, uint8_t(EncodingType::Snorm) }, // RGBA16S
{ 128, 1, 1, 16, 1, 1, 0, 0, uint8_t(EncodingType::Int ) }, // RGBA32I
{ 128, 1, 1, 16, 1, 1, 0, 0, uint8_t(EncodingType::Uint ) }, // RGBA32U
{ 128, 1, 1, 16, 1, 1, 0, 0, uint8_t(EncodingType::Float) }, // RGBA32F
{ 16, 1, 1, 2, 1, 1, 0, 0, uint8_t(EncodingType::Unorm) }, // R5G6B5
{ 16, 1, 1, 2, 1, 1, 0, 0, uint8_t(EncodingType::Unorm) }, // RGBA4
{ 16, 1, 1, 2, 1, 1, 0, 0, uint8_t(EncodingType::Unorm) }, // RGB5A1
{ 32, 1, 1, 4, 1, 1, 0, 0, uint8_t(EncodingType::Unorm) }, // RGB10A2
{ 32, 1, 1, 4, 1, 1, 0, 0, uint8_t(EncodingType::Unorm) }, // R11G11B10F
{ 0, 0, 0, 0, 0, 0, 0, 0, uint8_t(EncodingType::Count) }, // UnknownDepth
{ 16, 1, 1, 2, 1, 1, 16, 0, uint8_t(EncodingType::Unorm) }, // D16
{ 24, 1, 1, 3, 1, 1, 24, 0, uint8_t(EncodingType::Unorm) }, // D24
{ 32, 1, 1, 4, 1, 1, 24, 8, uint8_t(EncodingType::Unorm) }, // D24S8
{ 32, 1, 1, 4, 1, 1, 32, 0, uint8_t(EncodingType::Unorm) }, // D32
{ 16, 1, 1, 2, 1, 1, 16, 0, uint8_t(EncodingType::Unorm) }, // D16F
{ 24, 1, 1, 3, 1, 1, 24, 0, uint8_t(EncodingType::Unorm) }, // D24F
{ 32, 1, 1, 4, 1, 1, 32, 0, uint8_t(EncodingType::Unorm) }, // D32F
{ 8, 1, 1, 1, 1, 1, 0, 8, uint8_t(EncodingType::Unorm) }, // D0S8
};
BX_STATIC_ASSERT(TextureFormat::Count == BX_COUNTOF(s_imageBlockInfo) );
static const char* s_textureFormatName[] =
{
"BC1", // BC1
"BC2", // BC2
"BC3", // BC3
"BC4", // BC4
"BC5", // BC5
"BC6H", // BC6H
"BC7", // BC7
"ETC1", // ETC1
"ETC2", // ETC2
"ETC2A", // ETC2A
"ETC2A1", // ETC2A1
"PTC12", // PTC12
"PTC14", // PTC14
"PTC12A", // PTC12A
"PTC14A", // PTC14A
"PTC22", // PTC22
"PTC24", // PTC24
"<unknown>", // Unknown
"R1", // R1
"A8", // A8
"R8", // R8
"R8I", // R8I
"R8U", // R8U
"R8S", // R8S
"R16", // R16
"R16I", // R16I
"R16U", // R16U
"R16F", // R16F
"R16S", // R16S
"R32I", // R32I
"R32U", // R32U
"R32F", // R32F
"RG8", // RG8
"RG8I", // RG8I
"RG8U", // RG8U
"RG8S", // RG8S
"RG16", // RG16
"RG16I", // RG16I
"RG16U", // RG16U
"RG16F", // RG16F
"RG16S", // RG16S
"RG32I", // RG32I
"RG32U", // RG32U
"RG32F", // RG32F
"RGB9E5", // RGB9E5F
"BGRA8", // BGRA8
"RGBA8", // RGBA8
"RGBA8I", // RGBA8I
"RGBA8U", // RGBA8U
"RGBA8S", // RGBA8S
"RGBA16", // RGBA16
"RGBA16I", // RGBA16I
"RGBA16U", // RGBA16U
"RGBA16F", // RGBA16F
"RGBA16S", // RGBA16S
"RGBA32I", // RGBA32I
"RGBA32U", // RGBA32U
"RGBA32F", // RGBA32F
"R5G6B5", // R5G6B5
"RGBA4", // RGBA4
"RGB5A1", // RGB5A1
"RGB10A2", // RGB10A2
"R11G11B10F", // R11G11B10F
"<unknown>", // UnknownDepth
"D16", // D16
"D24", // D24
"D24S8", // D24S8
"D32", // D32
"D16F", // D16F
"D24F", // D24F
"D32F", // D32F
"D0S8", // D0S8
};
BX_STATIC_ASSERT(TextureFormat::Count == BX_COUNTOF(s_textureFormatName) );
bool isCompressed(TextureFormat::Enum _format)
{
return _format < TextureFormat::Unknown;
}
bool isColor(TextureFormat::Enum _format)
{
return _format > TextureFormat::Unknown
&& _format < TextureFormat::UnknownDepth
;
}
bool isDepth(TextureFormat::Enum _format)
{
return _format > TextureFormat::UnknownDepth
&& _format < TextureFormat::Count
;
}
bool isValid(TextureFormat::Enum _format)
{
return _format != TextureFormat::Unknown
&& _format != TextureFormat::UnknownDepth
&& _format != TextureFormat::Count
;
}
uint8_t getBitsPerPixel(TextureFormat::Enum _format)
{
return s_imageBlockInfo[_format].bitsPerPixel;
}
const ImageBlockInfo& getBlockInfo(TextureFormat::Enum _format)
{
return s_imageBlockInfo[_format];
}
uint8_t getBlockSize(TextureFormat::Enum _format)
{
return s_imageBlockInfo[_format].blockSize;
}
const char* getName(TextureFormat::Enum _format)
{
return s_textureFormatName[_format];
}
TextureFormat::Enum getFormat(const char* _name)
{
for (uint32_t ii = 0; ii < TextureFormat::Count; ++ii)
{
const TextureFormat::Enum fmt = TextureFormat::Enum(ii);
if (isValid(fmt) )
{
if (0 == bx::stricmp(s_textureFormatName[ii], _name) )
{
return fmt;
}
}
}
return TextureFormat::Unknown;
}
uint8_t imageGetNumMips(TextureFormat::Enum _format, uint16_t _width, uint16_t _height, uint16_t _depth)
{
const ImageBlockInfo& blockInfo = getBlockInfo(_format);
const uint16_t blockWidth = blockInfo.blockWidth;
const uint16_t blockHeight = blockInfo.blockHeight;
const uint16_t minBlockX = blockInfo.minBlockX;
const uint16_t minBlockY = blockInfo.minBlockY;
_width = bx::uint16_max(blockWidth * minBlockX, ( (_width + blockWidth - 1) / blockWidth)*blockWidth);
_height = bx::uint16_max(blockHeight * minBlockY, ( (_height + blockHeight - 1) / blockHeight)*blockHeight);
_depth = bx::uint16_max(1, _depth);
uint8_t numMips = 0;
for (uint32_t width = _width, height = _height, depth = _depth
; blockWidth < width || blockHeight < height || 1 < depth
; ++numMips)
{
width = bx::uint32_max(blockWidth * minBlockX, ( (width + blockWidth - 1) / blockWidth )*blockWidth);
height = bx::uint32_max(blockHeight * minBlockY, ( (height + blockHeight - 1) / blockHeight)*blockHeight);
depth = bx::uint32_max(1, depth);
width >>= 1;
height >>= 1;
depth >>= 1;
}
return numMips;
}
uint32_t imageGetSize(TextureFormat::Enum _format, uint16_t _width, uint16_t _height, uint16_t _depth, bool _cubeMap, uint8_t _numMips)
{
const ImageBlockInfo& blockInfo = getBlockInfo(_format);
const uint8_t bpp = blockInfo.bitsPerPixel;
const uint16_t blockWidth = blockInfo.blockWidth;
const uint16_t blockHeight = blockInfo.blockHeight;
const uint16_t minBlockX = blockInfo.minBlockX;
const uint16_t minBlockY = blockInfo.minBlockY;
_width = bx::uint16_max(blockWidth * minBlockX, ( (_width + blockWidth - 1) / blockWidth)*blockWidth);
_height = bx::uint16_max(blockHeight * minBlockY, ( (_height + blockHeight - 1) / blockHeight)*blockHeight);
_depth = bx::uint16_max(1, _depth);
_numMips = uint8_t(bx::uint16_max(1, _numMips) );
uint32_t width = _width;
uint32_t height = _height;
uint32_t depth = _depth;
uint32_t sides = _cubeMap ? 6 : 1;
uint32_t size = 0;
for (uint32_t lod = 0; lod < _numMips; ++lod)
{
width = bx::uint32_max(blockWidth * minBlockX, ( (width + blockWidth - 1) / blockWidth )*blockWidth);
height = bx::uint32_max(blockHeight * minBlockY, ( (height + blockHeight - 1) / blockHeight)*blockHeight);
depth = bx::uint32_max(1, depth);
size += width*height*depth*bpp/8 * sides;
width >>= 1;
height >>= 1;
depth >>= 1;
}
return size;
}
void imageSolid(uint32_t _width, uint32_t _height, uint32_t _solid, void* _dst)
{
uint32_t* dst = (uint32_t*)_dst;
for (uint32_t ii = 0, num = _width*_height; ii < num; ++ii)
{
*dst++ = _solid;
}
}
void imageCheckerboard(uint32_t _width, uint32_t _height, uint32_t _step, uint32_t _0, uint32_t _1, void* _dst)
{
uint32_t* dst = (uint32_t*)_dst;
for (uint32_t yy = 0; yy < _height; ++yy)
{
for (uint32_t xx = 0; xx < _width; ++xx)
{
uint32_t abgr = ( (xx/_step)&1) ^ ( (yy/_step)&1) ? _1 : _0;
*dst++ = abgr;
}
}
}
void imageRgba8Downsample2x2Ref(uint32_t _width, uint32_t _height, uint32_t _pitch, const void* _src, void* _dst)
{
const uint32_t dstwidth = _width/2;
const uint32_t dstheight = _height/2;
if (0 == dstwidth
|| 0 == dstheight)
{
return;
}
uint8_t* dst = (uint8_t*)_dst;
const uint8_t* src = (const uint8_t*)_src;
for (uint32_t yy = 0, ystep = _pitch*2; yy < dstheight; ++yy, src += ystep)
{
const uint8_t* rgba = src;
for (uint32_t xx = 0; xx < dstwidth; ++xx, rgba += 8, dst += 4)
{
float rr = bx::fpow(rgba[ 0], 2.2f);
float gg = bx::fpow(rgba[ 1], 2.2f);
float bb = bx::fpow(rgba[ 2], 2.2f);
float aa = rgba[ 3];
rr += bx::fpow(rgba[ 4], 2.2f);
gg += bx::fpow(rgba[ 5], 2.2f);
bb += bx::fpow(rgba[ 6], 2.2f);
aa += rgba[ 7];
rr += bx::fpow(rgba[_pitch+0], 2.2f);
gg += bx::fpow(rgba[_pitch+1], 2.2f);
bb += bx::fpow(rgba[_pitch+2], 2.2f);
aa += rgba[_pitch+3];
rr += bx::fpow(rgba[_pitch+4], 2.2f);
gg += bx::fpow(rgba[_pitch+5], 2.2f);
bb += bx::fpow(rgba[_pitch+6], 2.2f);
aa += rgba[_pitch+7];
rr *= 0.25f;
gg *= 0.25f;
bb *= 0.25f;
aa *= 0.25f;
rr = bx::fpow(rr, 1.0f/2.2f);
gg = bx::fpow(gg, 1.0f/2.2f);
bb = bx::fpow(bb, 1.0f/2.2f);
dst[0] = (uint8_t)rr;
dst[1] = (uint8_t)gg;
dst[2] = (uint8_t)bb;
dst[3] = (uint8_t)aa;
}
}
}
void imageRgba8Downsample2x2(uint32_t _width, uint32_t _height, uint32_t _pitch, const void* _src, void* _dst)
{
const uint32_t dstwidth = _width/2;
const uint32_t dstheight = _height/2;
if (0 == dstwidth
|| 0 == dstheight)
{
return;
}
uint8_t* dst = (uint8_t*)_dst;
const uint8_t* src = (const uint8_t*)_src;
using namespace bx;
const float4_t unpack = float4_ld(1.0f, 1.0f/256.0f, 1.0f/65536.0f, 1.0f/16777216.0f);
const float4_t pack = float4_ld(1.0f, 256.0f*0.5f, 65536.0f, 16777216.0f*0.5f);
const float4_t umask = float4_ild(0xff, 0xff00, 0xff0000, 0xff000000);
const float4_t pmask = float4_ild(0xff, 0x7f80, 0xff0000, 0x7f800000);
const float4_t wflip = float4_ild(0, 0, 0, 0x80000000);
const float4_t wadd = float4_ld(0.0f, 0.0f, 0.0f, 32768.0f*65536.0f);
const float4_t gamma = float4_ld(1.0f/2.2f, 1.0f/2.2f, 1.0f/2.2f, 1.0f);
const float4_t linear = float4_ld(2.2f, 2.2f, 2.2f, 1.0f);
const float4_t quater = float4_splat(0.25f);
for (uint32_t yy = 0, ystep = _pitch*2; yy < dstheight; ++yy, src += ystep)
{
const uint8_t* rgba = src;
for (uint32_t xx = 0; xx < dstwidth; ++xx, rgba += 8, dst += 4)
{
const float4_t abgr0 = float4_splat(rgba);
const float4_t abgr1 = float4_splat(rgba+4);
const float4_t abgr2 = float4_splat(rgba+_pitch);
const float4_t abgr3 = float4_splat(rgba+_pitch+4);
const float4_t abgr0m = float4_and(abgr0, umask);
const float4_t abgr1m = float4_and(abgr1, umask);
const float4_t abgr2m = float4_and(abgr2, umask);
const float4_t abgr3m = float4_and(abgr3, umask);
const float4_t abgr0x = float4_xor(abgr0m, wflip);
const float4_t abgr1x = float4_xor(abgr1m, wflip);
const float4_t abgr2x = float4_xor(abgr2m, wflip);
const float4_t abgr3x = float4_xor(abgr3m, wflip);
const float4_t abgr0f = float4_itof(abgr0x);
const float4_t abgr1f = float4_itof(abgr1x);
const float4_t abgr2f = float4_itof(abgr2x);
const float4_t abgr3f = float4_itof(abgr3x);
const float4_t abgr0c = float4_add(abgr0f, wadd);
const float4_t abgr1c = float4_add(abgr1f, wadd);
const float4_t abgr2c = float4_add(abgr2f, wadd);
const float4_t abgr3c = float4_add(abgr3f, wadd);
const float4_t abgr0n = float4_mul(abgr0c, unpack);
const float4_t abgr1n = float4_mul(abgr1c, unpack);
const float4_t abgr2n = float4_mul(abgr2c, unpack);
const float4_t abgr3n = float4_mul(abgr3c, unpack);
const float4_t abgr0l = float4_pow(abgr0n, linear);
const float4_t abgr1l = float4_pow(abgr1n, linear);
const float4_t abgr2l = float4_pow(abgr2n, linear);
const float4_t abgr3l = float4_pow(abgr3n, linear);
const float4_t sum0 = float4_add(abgr0l, abgr1l);
const float4_t sum1 = float4_add(abgr2l, abgr3l);
const float4_t sum2 = float4_add(sum0, sum1);
const float4_t avg0 = float4_mul(sum2, quater);
const float4_t avg1 = float4_pow(avg0, gamma);
const float4_t avg2 = float4_mul(avg1, pack);
const float4_t ftoi0 = float4_ftoi(avg2);
const float4_t ftoi1 = float4_and(ftoi0, pmask);
const float4_t zwxy = float4_swiz_zwxy(ftoi1);
const float4_t tmp0 = float4_or(ftoi1, zwxy);
const float4_t yyyy = float4_swiz_yyyy(tmp0);
const float4_t tmp1 = float4_iadd(yyyy, yyyy);
const float4_t result = float4_or(tmp0, tmp1);
float4_stx(dst, result);
}
}
}
void imageRgba32fDownsample2x2NormalMapRef(uint32_t _width, uint32_t _height, uint32_t _pitch, const void* _src, void* _dst)
{
const uint32_t dstwidth = _width/2;
const uint32_t dstheight = _height/2;
if (0 == dstwidth
|| 0 == dstheight)
{
return;
}
const uint8_t* src = (const uint8_t*)_src;
uint8_t* dst = (uint8_t*)_dst;
for (uint32_t yy = 0, ystep = _pitch*2; yy < dstheight; ++yy, src += ystep)
{
const float* rgba0 = (const float*)&src[0];
const float* rgba1 = (const float*)&src[_pitch];
for (uint32_t xx = 0; xx < dstwidth; ++xx, rgba0 += 8, rgba1 += 8, dst += 16)
{
float xyz[3];
xyz[0] = rgba0[0];
xyz[1] = rgba0[1];
xyz[2] = rgba0[2];
xyz[0] += rgba0[4];
xyz[1] += rgba0[5];
xyz[2] += rgba0[6];
xyz[0] += rgba1[0];
xyz[1] += rgba1[1];
xyz[2] += rgba1[2];
xyz[0] += rgba1[4];
xyz[1] += rgba1[5];
xyz[2] += rgba1[6];
bx::vec3Norm( (float*)dst, xyz);
}
}
}
void imageRgba32fDownsample2x2NormalMap(uint32_t _width, uint32_t _height, uint32_t _pitch, const void* _src, void* _dst)
{
imageRgba32fDownsample2x2NormalMapRef(_width, _height, _pitch, _src, _dst);
}
void imageSwizzleBgra8Ref(uint32_t _width, uint32_t _height, uint32_t _pitch, const void* _src, void* _dst)
{
const uint8_t* src = (uint8_t*) _src;
const uint8_t* next = src + _pitch;
uint8_t* dst = (uint8_t*)_dst;
for (uint32_t yy = 0; yy < _height; ++yy, src = next, next += _pitch)
{
for (uint32_t xx = 0; xx < _width; ++xx, src += 4, dst += 4)
{
uint8_t rr = src[0];
uint8_t gg = src[1];
uint8_t bb = src[2];
uint8_t aa = src[3];
dst[0] = bb;
dst[1] = gg;
dst[2] = rr;
dst[3] = aa;
}
}
}
void imageSwizzleBgra8(uint32_t _width, uint32_t _height, uint32_t _pitch, const void* _src, void* _dst)
{
// Test can we do four 4-byte pixels at the time.
if (0 != (_width&0x3)
|| _width < 4
|| !bx::isPtrAligned(_src, 16)
|| !bx::isPtrAligned(_dst, 16) )
{
BX_WARN(false, "Image swizzle is taking slow path.");
BX_WARN(bx::isPtrAligned(_src, 16), "Source %p is not 16-byte aligned.", _src);
BX_WARN(bx::isPtrAligned(_dst, 16), "Destination %p is not 16-byte aligned.", _dst);
BX_WARN(_width < 4, "Image width must be multiple of 4 (width %d).", _width);
imageSwizzleBgra8Ref(_width, _height, _pitch, _src, _dst);
return;
}
using namespace bx;
const float4_t mf0f0 = float4_isplat(0xff00ff00);
const float4_t m0f0f = float4_isplat(0x00ff00ff);
const uint8_t* src = (uint8_t*) _src;
const uint8_t* next = src + _pitch;
uint8_t* dst = (uint8_t*)_dst;
const uint32_t width = _width/4;
for (uint32_t yy = 0; yy < _height; ++yy, src = next, next += _pitch)
{
for (uint32_t xx = 0; xx < width; ++xx, src += 16, dst += 16)
{
const float4_t tabgr = float4_ld(src);
const float4_t t00ab = float4_srl(tabgr, 16);
const float4_t tgr00 = float4_sll(tabgr, 16);
const float4_t tgrab = float4_or(t00ab, tgr00);
const float4_t ta0g0 = float4_and(tabgr, mf0f0);
const float4_t t0r0b = float4_and(tgrab, m0f0f);
const float4_t targb = float4_or(ta0g0, t0r0b);
float4_st(dst, targb);
}
}
}
void imageCopy(uint32_t _height, uint32_t _srcPitch, const void* _src, uint32_t _dstPitch, void* _dst)
{
const uint32_t pitch = bx::uint32_min(_srcPitch, _dstPitch);
const uint8_t* src = (uint8_t*)_src;
uint8_t* dst = (uint8_t*)_dst;
for (uint32_t yy = 0; yy < _height; ++yy, src += _srcPitch, dst += _dstPitch)
{
memcpy(dst, src, pitch);
}
}
void imageCopy(uint32_t _width, uint32_t _height, uint32_t _bpp, uint32_t _pitch, const void* _src, void* _dst)
{
const uint32_t dstPitch = _width*_bpp/8;
imageCopy(_height, _pitch, _src, dstPitch, _dst);
}
uint32_t toUnorm(float _value, float _scale)
{
return uint32_t(bx::fround(
bx::fsaturate(_value) * _scale)
);
}
float fromUnorm(uint32_t _value, float _scale)
{
return float(_value) / _scale;
}
int32_t toSnorm(float _value, float _scale)
{
return int32_t(bx::fround(
bx::fclamp(_value, -1.0f, 1.0f) * _scale)
);
}
float fromSnorm(int32_t _value, float _scale)
{
return bx::fmax(-1.0f, float(_value) / _scale);
}
// R8
void packR8(void* _dst, const float* _src)
{
uint8_t* dst = (uint8_t*)_dst;
dst[0] = uint8_t(toUnorm(_src[0], 255.0f) );
}
void unpackR8(float* _dst, const void* _src)
{
const uint8_t* src = (const uint8_t*)_src;
_dst[0] = fromUnorm(src[0], 255.0f);
}
// R8S
void packR8S(void* _dst, const float* _src)
{
int8_t* dst = (int8_t*)_dst;
dst[0] = int8_t(toSnorm(_src[0], 127.0f) );
}
void unpackR8S(float* _dst, const void* _src)
{
const int8_t* src = (const int8_t*)_src;
_dst[0] = fromSnorm(src[0], 127.0f);
}
// R8I
void packR8I(void* _dst, const float* _src)
{
int8_t* dst = (int8_t*)_dst;
dst[0] = int8_t(_src[0]);
}
void unpackR8I(float* _dst, const void* _src)
{
const int8_t* src = (const int8_t*)_src;
_dst[0] = float(src[0]);
}
// R8U
void packR8U(void* _dst, const float* _src)
{
uint8_t* dst = (uint8_t*)_dst;
dst[0] = uint8_t(_src[0]);
}
void unpackR8U(float* _dst, const void* _src)
{
const uint8_t* src = (const uint8_t*)_src;
_dst[0] = float(src[0]);
}
// RG8
void packRg8(void* _dst, const float* _src)
{
uint8_t* dst = (uint8_t*)_dst;
dst[0] = uint8_t(toUnorm(_src[0], 255.0f) );
dst[1] = uint8_t(toUnorm(_src[1], 255.0f) );
}
void unpackRg8(float* _dst, const void* _src)
{
const uint8_t* src = (const uint8_t*)_src;
_dst[0] = fromUnorm(src[0], 255.0f);
_dst[1] = fromUnorm(src[1], 255.0f);
}
// RG8S
void packRg8S(void* _dst, const float* _src)
{
int8_t* dst = (int8_t*)_dst;
dst[0] = int8_t(toSnorm(_src[0], 127.0f) );
dst[1] = int8_t(toSnorm(_src[1], 127.0f) );
}
void unpackRg8S(float* _dst, const void* _src)
{
const int8_t* src = (const int8_t*)_src;
_dst[0] = fromSnorm(src[0], 127.0f);
_dst[1] = fromSnorm(src[1], 127.0f);
}
// RG8I
void packRg8I(void* _dst, const float* _src)
{
int8_t* dst = (int8_t*)_dst;
dst[0] = int8_t(_src[0]);
dst[1] = int8_t(_src[1]);
}
void unpackRg8I(float* _dst, const void* _src)
{
const int8_t* src = (const int8_t*)_src;
_dst[0] = float(src[0]);
_dst[1] = float(src[1]);
}
// RG8U
void packRg8U(void* _dst, const float* _src)
{
uint8_t* dst = (uint8_t*)_dst;
dst[0] = uint8_t(_src[0]);
dst[1] = uint8_t(_src[1]);
}
void unpackRg8U(float* _dst, const void* _src)
{
const uint8_t* src = (const uint8_t*)_src;
_dst[0] = float(src[0]);
_dst[1] = float(src[1]);
}
// RGBA8
void packRgba8(void* _dst, const float* _src)
{
uint8_t* dst = (uint8_t*)_dst;
dst[0] = uint8_t(toUnorm(_src[0], 255.0f) );
dst[1] = uint8_t(toUnorm(_src[1], 255.0f) );
dst[2] = uint8_t(toUnorm(_src[2], 255.0f) );
dst[3] = uint8_t(toUnorm(_src[3], 255.0f) );
}
void unpackRgba8(float* _dst, const void* _src)
{
const uint8_t* src = (const uint8_t*)_src;
_dst[0] = fromUnorm(src[0], 255.0f);
_dst[1] = fromUnorm(src[1], 255.0f);
_dst[2] = fromUnorm(src[2], 255.0f);
_dst[3] = fromUnorm(src[3], 255.0f);
}
// BGRA8
void packBgra8(void* _dst, const float* _src)
{
uint8_t* dst = (uint8_t*)_dst;
dst[2] = uint8_t(toUnorm(_src[0], 255.0f) );
dst[1] = uint8_t(toUnorm(_src[1], 255.0f) );
dst[0] = uint8_t(toUnorm(_src[2], 255.0f) );
dst[3] = uint8_t(toUnorm(_src[3], 255.0f) );
}
void unpackBgra8(float* _dst, const void* _src)
{
const uint8_t* src = (const uint8_t*)_src;
_dst[0] = fromUnorm(src[2], 255.0f);
_dst[1] = fromUnorm(src[1], 255.0f);
_dst[2] = fromUnorm(src[0], 255.0f);
_dst[3] = fromUnorm(src[3], 255.0f);
}
// RGBA8S
void packRgba8S(void* _dst, const float* _src)
{
int8_t* dst = (int8_t*)_dst;
dst[0] = int8_t(toSnorm(_src[0], 127.0f) );
dst[1] = int8_t(toSnorm(_src[1], 127.0f) );
dst[2] = int8_t(toSnorm(_src[2], 127.0f) );
dst[3] = int8_t(toSnorm(_src[3], 127.0f) );
}
void unpackRgba8S(float* _dst, const void* _src)
{
const int8_t* src = (const int8_t*)_src;
_dst[0] = fromSnorm(src[0], 127.0f);
_dst[1] = fromSnorm(src[1], 127.0f);
_dst[2] = fromSnorm(src[2], 127.0f);
_dst[3] = fromSnorm(src[3], 127.0f);
}
// RGBA8I
void packRgba8I(void* _dst, const float* _src)
{
int8_t* dst = (int8_t*)_dst;
dst[0] = int8_t(_src[0]);
dst[1] = int8_t(_src[1]);
dst[2] = int8_t(_src[2]);
dst[3] = int8_t(_src[3]);
}
void unpackRgba8I(float* _dst, const void* _src)
{
const int8_t* src = (const int8_t*)_src;
_dst[0] = float(src[0]);
_dst[1] = float(src[1]);
_dst[2] = float(src[2]);
_dst[3] = float(src[3]);
}
// RGBA8U
void packRgba8U(void* _dst, const float* _src)
{
uint8_t* dst = (uint8_t*)_dst;
dst[0] = uint8_t(_src[0]);
dst[1] = uint8_t(_src[1]);
dst[2] = uint8_t(_src[2]);
dst[3] = uint8_t(_src[3]);
}
void unpackRgba8U(float* _dst, const void* _src)
{
const uint8_t* src = (const uint8_t*)_src;
_dst[0] = float(src[0]);
_dst[1] = float(src[1]);
_dst[2] = float(src[2]);
_dst[3] = float(src[3]);
}
// R16
void packR16(void* _dst, const float* _src)
{
uint16_t* dst = (uint16_t*)_dst;
dst[0] = uint16_t(toUnorm(_src[0], 65535.0f) );
}
void unpackR16(float* _dst, const void* _src)
{
const uint16_t* src = (const uint16_t*)_src;
_dst[0] = fromUnorm(src[0], 65535.0f);
}
// R16S
void packR16S(void* _dst, const float* _src)
{
int16_t* dst = (int16_t*)_dst;
dst[0] = int16_t(toSnorm(_src[0], 32767.0f) );
}
void unpackR16S(float* _dst, const void* _src)
{
const int16_t* src = (const int16_t*)_src;
_dst[0] = fromSnorm(src[0], 32767.0f);
}
// R16I
void packR16I(void* _dst, const float* _src)
{
int16_t* dst = (int16_t*)_dst;
dst[0] = int16_t(_src[0]);
}
void unpackR16I(float* _dst, const void* _src)
{
const int16_t* src = (const int16_t*)_src;
_dst[0] = float(src[0]);
}
// R16U
void packR16U(void* _dst, const float* _src)
{
uint16_t* dst = (uint16_t*)_dst;
dst[0] = uint16_t(_src[0]);
}
void unpackR16U(float* _dst, const void* _src)
{
const uint16_t* src = (const uint16_t*)_src;
_dst[0] = float(src[0]);
}
// R16F
void packR16F(void* _dst, const float* _src)
{
uint16_t* dst = (uint16_t*)_dst;
dst[0] = bx::halfFromFloat(_src[0]);
}
void unpackR16F(float* _dst, const void* _src)
{
const uint16_t* src = (const uint16_t*)_src;
_dst[0] = bx::halfToFloat(src[0]);
}
// RG16
void packRg16(void* _dst, const float* _src)
{
uint16_t* dst = (uint16_t*)_dst;
dst[0] = uint16_t(toUnorm(_src[0], 65535.0f) );
dst[1] = uint16_t(toUnorm(_src[1], 65535.0f) );
}
void unpackRg16(float* _dst, const void* _src)
{
const uint16_t* src = (const uint16_t*)_src;
_dst[0] = fromUnorm(src[0], 65535.0f);
_dst[1] = fromUnorm(src[1], 65535.0f);
}
// RG16S
void packRg16S(void* _dst, const float* _src)
{
int16_t* dst = (int16_t*)_dst;
dst[0] = int16_t(toSnorm(_src[0], 32767.0f) );
dst[1] = int16_t(toSnorm(_src[1], 32767.0f) );
}
void unpackRg16S(float* _dst, const void* _src)
{
const int16_t* src = (const int16_t*)_src;
_dst[0] = fromSnorm(src[0], 32767.0f);
_dst[1] = fromSnorm(src[1], 32767.0f);
}
// RG16I
void packRg16I(void* _dst, const float* _src)
{
int16_t* dst = (int16_t*)_dst;
dst[0] = int16_t(_src[0]);
dst[1] = int16_t(_src[1]);
}
void unpackRg16I(float* _dst, const void* _src)
{
const int16_t* src = (const int16_t*)_src;
_dst[0] = float(src[0]);
_dst[1] = float(src[1]);
}
// RG16U
void packRg16U(void* _dst, const float* _src)
{
uint16_t* dst = (uint16_t*)_dst;
dst[0] = uint16_t(_src[0]);
dst[1] = uint16_t(_src[1]);
}
void unpackRg16U(float* _dst, const void* _src)
{
const uint16_t* src = (const uint16_t*)_src;
_dst[0] = float(src[0]);
_dst[1] = float(src[1]);
}
// RG16F
void packRg16F(void* _dst, const float* _src)
{
uint16_t* dst = (uint16_t*)_dst;
dst[0] = bx::halfFromFloat(_src[0]);
dst[1] = bx::halfFromFloat(_src[1]);
}
void unpackRg16F(float* _dst, const void* _src)
{
const uint16_t* src = (const uint16_t*)_src;
_dst[0] = bx::halfToFloat(src[0]);
_dst[1] = bx::halfToFloat(src[1]);
}
// RGBA16
void packRgba16(void* _dst, const float* _src)
{
uint16_t* dst = (uint16_t*)_dst;
dst[0] = uint16_t(toUnorm(_src[0], 65535.0f) );
dst[1] = uint16_t(toUnorm(_src[1], 65535.0f) );
dst[2] = uint16_t(toUnorm(_src[2], 65535.0f) );
dst[3] = uint16_t(toUnorm(_src[3], 65535.0f) );
}
void unpackRgba16(float* _dst, const void* _src)
{
const uint16_t* src = (const uint16_t*)_src;
_dst[0] = fromUnorm(src[0], 65535.0f);
_dst[1] = fromUnorm(src[1], 65535.0f);
_dst[2] = fromUnorm(src[2], 65535.0f);
_dst[3] = fromUnorm(src[3], 65535.0f);
}
// RGBA16S
void packRgba16S(void* _dst, const float* _src)
{
int16_t* dst = (int16_t*)_dst;
dst[0] = int16_t(toSnorm(_src[0], 32767.0f) );
dst[1] = int16_t(toSnorm(_src[1], 32767.0f) );
dst[2] = int16_t(toSnorm(_src[2], 32767.0f) );
dst[3] = int16_t(toSnorm(_src[3], 32767.0f) );
}
void unpackRgba16S(float* _dst, const void* _src)
{
const int16_t* src = (const int16_t*)_src;
_dst[0] = fromSnorm(src[0], 32767.0f);
_dst[1] = fromSnorm(src[1], 32767.0f);
_dst[2] = fromSnorm(src[2], 32767.0f);
_dst[3] = fromSnorm(src[3], 32767.0f);
}
// RGBA16I
void packRgba16I(void* _dst, const float* _src)
{
int16_t* dst = (int16_t*)_dst;
dst[0] = int16_t(_src[0]);
dst[1] = int16_t(_src[1]);
dst[2] = int16_t(_src[2]);
dst[3] = int16_t(_src[3]);
}
void unpackRgba16I(float* _dst, const void* _src)
{
const int16_t* src = (const int16_t*)_src;
_dst[0] = float(src[0]);
_dst[1] = float(src[1]);
_dst[2] = float(src[2]);
_dst[3] = float(src[3]);
}
// RGBA16U
void packRgba16U(void* _dst, const float* _src)
{
uint16_t* dst = (uint16_t*)_dst;
dst[0] = uint16_t(_src[0]);
dst[1] = uint16_t(_src[1]);
dst[2] = uint16_t(_src[2]);
dst[3] = uint16_t(_src[3]);
}
void unpackRgba16U(float* _dst, const void* _src)
{
const uint16_t* src = (const uint16_t*)_src;
_dst[0] = float(src[0]);
_dst[1] = float(src[1]);
_dst[2] = float(src[2]);
_dst[3] = float(src[3]);
}
// RGBA16F
void packRgba16F(void* _dst, const float* _src)
{
uint16_t* dst = (uint16_t*)_dst;
dst[0] = bx::halfFromFloat(_src[0]);
dst[1] = bx::halfFromFloat(_src[1]);
dst[2] = bx::halfFromFloat(_src[2]);
dst[3] = bx::halfFromFloat(_src[3]);
}
void unpackRgba16F(float* _dst, const void* _src)
{
const uint16_t* src = (const uint16_t*)_src;
_dst[0] = bx::halfToFloat(src[0]);
_dst[1] = bx::halfToFloat(src[1]);
_dst[2] = bx::halfToFloat(src[2]);
_dst[3] = bx::halfToFloat(src[3]);
}
// R32I
void packR32I(void* _dst, const float* _src)
{
memcpy(_dst, _src, 4);
}
void unpackR32I(float* _dst, const void* _src)
{
memcpy(_dst, _src, 4);
}
// R32U
void packR32U(void* _dst, const float* _src)
{
memcpy(_dst, _src, 4);
}
void unpackR32U(float* _dst, const void* _src)
{
memcpy(_dst, _src, 4);
}
// R32F
void packR32F(void* _dst, const float* _src)
{
memcpy(_dst, _src, 4);
}
void unpackR32F(float* _dst, const void* _src)
{
memcpy(_dst, _src, 4);
}
// RG32I
void packRg32I(void* _dst, const float* _src)
{
memcpy(_dst, _src, 8);
}
void unpackRg32I(float* _dst, const void* _src)
{
memcpy(_dst, _src, 8);
}
// RG32U
void packRg32U(void* _dst, const float* _src)
{
memcpy(_dst, _src, 8);
}
void unpackRg32U(float* _dst, const void* _src)
{
memcpy(_dst, _src, 8);
}
// RG32F
void packRg32F(void* _dst, const float* _src)
{
memcpy(_dst, _src, 8);
}
void unpackRg32F(float* _dst, const void* _src)
{
memcpy(_dst, _src, 8);
}
template<int32_t MantissaBits, int32_t ExpBits>
void encodeRgbE(float* _dst, const float* _src)
{
// Reference:
// https://www.opengl.org/registry/specs/EXT/texture_shared_exponent.txt
const int32_t expMax = (1<<ExpBits) - 1;
const int32_t expBias = (1<<(ExpBits - 1) ) - 1;
const float sharedExpMax = float(expMax) / float(expMax + 1) * float(1 << (expMax - expBias) );
const float rr = bx::fclamp(_src[0], 0.0f, sharedExpMax);
const float gg = bx::fclamp(_src[1], 0.0f, sharedExpMax);
const float bb = bx::fclamp(_src[2], 0.0f, sharedExpMax);
const float max = bx::fmax3(rr, gg, bb);
union { float ff; uint32_t ui; } cast = { max };
int32_t expShared = int32_t(bx::uint32_imax(uint32_t(-expBias-1), ( ( (cast.ui>>23) & 0xff) - 127) ) ) + 1 + expBias;
float denom = bx::fpow(2.0f, float(expShared - expBias - MantissaBits) );
if ( (1<<MantissaBits) == int32_t(bx::fround(max/denom) ) )
{
denom *= 2.0f;
++expShared;
}
const float invDenom = 1.0f/denom;
_dst[0] = bx::fround(rr * invDenom);
_dst[1] = bx::fround(gg * invDenom);
_dst[2] = bx::fround(bb * invDenom);
_dst[3] = float(expShared);
}
template<int32_t MantissaBits, int32_t ExpBits>
void decodeRgbE(float* _dst, const float* _src)
{
const int32_t expBias = (1<<(ExpBits - 1) ) - 1;
const float exponent = _src[3]-float(expBias-MantissaBits);
const float scale = bx::fpow(2.0f, exponent);
_dst[0] = _src[0] * scale;
_dst[1] = _src[1] * scale;
_dst[2] = _src[2] * scale;
}
// RGB9E5F
void packRgb9E5F(void* _dst, const float* _src)
{
float tmp[4];
encodeRgbE<9, 5>(tmp, _src);
*( (uint32_t*)_dst) = 0
| (uint32_t(tmp[0]) )
| (uint32_t(tmp[1]) << 9)
| (uint32_t(tmp[2]) <<18)
| (uint32_t(tmp[3]) <<27)
;
}
void unpackRgb9E5F(float* _dst, const void* _src)
{
uint32_t packed = *( (const uint32_t*)_src);
float tmp[4];
tmp[0] = float( ( (packed ) & 0x1ff) ) / 511.0f;
tmp[1] = float( ( (packed>> 9) & 0x1ff) ) / 511.0f;
tmp[2] = float( ( (packed>>18) & 0x1ff) ) / 511.0f;
tmp[3] = float( ( (packed>>27) & 0x1f) );
decodeRgbE<9, 5>(_dst, tmp);
}
// RGBA32I
void packRgba32I(void* _dst, const float* _src)
{
memcpy(_dst, _src, 16);
}
void unpackRgba32I(float* _dst, const void* _src)
{
memcpy(_dst, _src, 16);
}
// RGBA32U
void packRgba32U(void* _dst, const float* _src)
{
memcpy(_dst, _src, 16);
}
void unpackRgba32U(float* _dst, const void* _src)
{
memcpy(_dst, _src, 16);
}
// RGBA32F
void packRgba32F(void* _dst, const float* _src)
{
memcpy(_dst, _src, 16);
}
void unpackRgba32F(float* _dst, const void* _src)
{
memcpy(_dst, _src, 16);
}
// R5G6B5
void packR5G6B5(void* _dst, const float* _src)
{
*( (uint16_t*)_dst) = 0
| uint16_t(toUnorm(_src[0], 31.0f) )
| uint16_t(toUnorm(_src[1], 63.0f)<< 5)
| uint16_t(toUnorm(_src[2], 31.0f)<<11)
;
}
void unpackR5G6B5(float* _dst, const void* _src)
{
uint16_t packed = *( (const uint16_t*)_src);
_dst[0] = float( ( (packed ) & 0x1f) ) / 31.0f;
_dst[1] = float( ( (packed>> 5) & 0x3f) ) / 63.0f;
_dst[2] = float( ( (packed>>11) & 0x1f) ) / 31.0f;
}
// RGBA4
void packRgba4(void* _dst, const float* _src)
{
*( (uint16_t*)_dst) = 0
| uint16_t(toUnorm(_src[0], 15.0f) )
| uint16_t(toUnorm(_src[1], 15.0f)<< 4)
| uint16_t(toUnorm(_src[2], 15.0f)<< 8)
| uint16_t(toUnorm(_src[3], 15.0f)<<12)
;
}
void unpackRgba4(float* _dst, const void* _src)
{
uint16_t packed = *( (const uint16_t*)_src);
_dst[0] = float( ( (packed ) & 0xf) ) / 15.0f;
_dst[1] = float( ( (packed>> 4) & 0xf) ) / 15.0f;
_dst[2] = float( ( (packed>> 8) & 0xf) ) / 15.0f;
_dst[3] = float( ( (packed>>12) & 0xf) ) / 15.0f;
}
// RGB5A1
void packRgb5a1(void* _dst, const float* _src)
{
*( (uint16_t*)_dst) = 0
| uint16_t(toUnorm(_src[0], 31.0f) )
| uint16_t(toUnorm(_src[1], 31.0f)<< 5)
| uint16_t(toUnorm(_src[2], 31.0f)<<10)
| uint16_t(toUnorm(_src[3], 1.0f)<<15)
;
}
void unpackRgb5a1(float* _dst, const void* _src)
{
uint16_t packed = *( (const uint16_t*)_src);
_dst[0] = float( ( (packed ) & 0x1f) ) / 31.0f;
_dst[1] = float( ( (packed>> 5) & 0x1f) ) / 31.0f;
_dst[2] = float( ( (packed>>10) & 0x1f) ) / 31.0f;
_dst[3] = float( ( (packed>>14) & 0x1) );
}
// RGB10A2
void packRgb10A2(void* _dst, const float* _src)
{
*( (uint32_t*)_dst) = 0
| (toUnorm(_src[0], 1023.0f) )
| (toUnorm(_src[1], 1023.0f)<<10)
| (toUnorm(_src[2], 1023.0f)<<20)
| (toUnorm(_src[3], 3.0f)<<30)
;
}
void unpackRgb10A2(float* _dst, const void* _src)
{
uint32_t packed = *( (const uint32_t*)_src);
_dst[0] = float( ( (packed ) & 0x3ff) ) / 1023.0f;
_dst[1] = float( ( (packed>>10) & 0x3ff) ) / 1023.0f;
_dst[2] = float( ( (packed>>20) & 0x3ff) ) / 1023.0f;
_dst[3] = float( ( (packed>>30) & 0x3) ) / 3.0f;
}
// R11G11B10F
void packR11G11B10F(void* _dst, const float* _src)
{
*( (uint32_t*)_dst) = 0
| ( (bx::halfFromFloat(_src[0])>> 4) & 0x7ff)
| ( (bx::halfFromFloat(_src[0])<< 7) & 0x3ff800)
| ( (bx::halfFromFloat(_src[0])<<17) & 0xffc00000)
;
}
void unpackR11G11B10F(float* _dst, const void* _src)
{
uint32_t packed = *( (const uint32_t*)_src);
_dst[0] = bx::halfToFloat( (packed<< 4) & 0x7ff0);
_dst[1] = bx::halfToFloat( (packed>> 7) & 0x7ff0);
_dst[2] = bx::halfToFloat( (packed>>17) & 0x7fe0);
}
typedef void (*PackFn)(void*, const float*);
typedef void (*UnpackFn)(float*, const void*);
struct PackUnpack
{
PackFn pack;
UnpackFn unpack;
};
static PackUnpack s_packUnpack[] =
{
{ NULL, NULL }, // BC1
{ NULL, NULL }, // BC2
{ NULL, NULL }, // BC3
{ NULL, NULL }, // BC4
{ NULL, NULL }, // BC5
{ NULL, NULL }, // BC6H
{ NULL, NULL }, // BC7
{ NULL, NULL }, // ETC1
{ NULL, NULL }, // ETC2
{ NULL, NULL }, // ETC2A
{ NULL, NULL }, // ETC2A1
{ NULL, NULL }, // PTC12
{ NULL, NULL }, // PTC14
{ NULL, NULL }, // PTC12A
{ NULL, NULL }, // PTC14A
{ NULL, NULL }, // PTC22
{ NULL, NULL }, // PTC24
{ NULL, NULL }, // Unknown
{ NULL, NULL }, // R1
{ packR8, unpackR8 }, // A8
{ packR8, unpackR8 }, // R8
{ packR8I, unpackR8I }, // R8I
{ packR8U, unpackR8U }, // R8U
{ packR8S, unpackR8S }, // R8S
{ packR16, unpackR16 }, // R16
{ packR16I, unpackR16I }, // R16I
{ packR16U, unpackR16U }, // R16U
{ packR16F, unpackR16F }, // R16F
{ packR16S, unpackR16S }, // R16S
{ packR32I, unpackR32I }, // R32I
{ packR32U, unpackR32U }, // R32U
{ packR32F, unpackR32F }, // R32F
{ packRg8, unpackRg8 }, // RG8
{ packRg8I, unpackRg8I }, // RG8I
{ packRg8U, unpackRg8U }, // RG8U
{ packRg8S, unpackRg8S }, // RG8S
{ packRg16, unpackRg16 }, // RG16
{ packRg16I, unpackRg16I }, // RG16I
{ packRg16U, unpackRg16U }, // RG16U
{ packRg16F, unpackRg16F }, // RG16F
{ packRg16S, unpackRg16S }, // RG16S
{ packRg32I, unpackRg32I }, // RG32I
{ packRg32U, unpackRg32U }, // RG32U
{ packRg32F, unpackRg32F }, // RG32F
{ packRgb9E5F, unpackRgb9E5F }, // RGB9E5F
{ packBgra8, unpackBgra8 }, // BGRA8
{ packRgba8, unpackRgba8 }, // RGBA8
{ packRgba8I, unpackRgba8I }, // RGBA8I
{ packRgba8U, unpackRgba8U }, // RGBA8U
{ packRgba8S, unpackRgba8S }, // RGBA8S
{ packRgba16, unpackRgba16 }, // RGBA16
{ packRgba16I, unpackRgba16I }, // RGBA16I
{ packRgba16U, unpackRgba16U }, // RGBA16U
{ packRgba16F, unpackRgba16F }, // RGBA16F
{ packRgba16S, unpackRgba16S }, // RGBA16S
{ packRgba32I, unpackRgba32I }, // RGBA32I
{ packRgba32U, unpackRgba32U }, // RGBA32U
{ packRgba32F, unpackRgba32F }, // RGBA32F
{ packR5G6B5, unpackR5G6B5 }, // R5G6B5
{ packRgba4, unpackRgba4 }, // RGBA4
{ packRgb5a1, unpackRgb5a1 }, // RGB5A1
{ packRgb10A2, unpackRgb10A2 }, // RGB10A2
{ packR11G11B10F, unpackR11G11B10F }, // R11G11B10F
{ NULL, NULL }, // UnknownDepth
{ NULL, NULL }, // D16
{ NULL, NULL }, // D24
{ NULL, NULL }, // D24S8
{ NULL, NULL }, // D32
{ NULL, NULL }, // D16F
{ NULL, NULL }, // D24F
{ NULL, NULL }, // D32F
{ NULL, NULL }, // D0S8
};
BX_STATIC_ASSERT(TextureFormat::Count == BX_COUNTOF(s_packUnpack) );
bool imageConvert(void* _dst, TextureFormat::Enum _dstFormat, const void* _src, TextureFormat::Enum _srcFormat, uint32_t _width, uint32_t _height)
{
UnpackFn unpack = s_packUnpack[_srcFormat].unpack;
PackFn pack = s_packUnpack[_dstFormat].pack;
if (NULL == pack
|| NULL == unpack)
{
return false;
}
const uint8_t* src = (uint8_t*)_src;
uint8_t* dst = (uint8_t*)_dst;
const uint32_t srcBpp = s_imageBlockInfo[_srcFormat].bitsPerPixel;
const uint32_t dstBpp = s_imageBlockInfo[_dstFormat].bitsPerPixel;
const uint32_t srcPitch = _width * srcBpp / 8;
const uint32_t dstPitch = _width * dstBpp / 8;
for (uint32_t yy = 0; yy < _height; ++yy, src += srcPitch, dst += dstPitch)
{
for (uint32_t xx = 0; xx < _width; ++xx)
{
float rgba[4];
unpack(rgba, &src[xx*srcBpp/8]);
pack(&dst[xx*dstBpp/8], rgba);
}
}
return true;
}
uint8_t bitRangeConvert(uint32_t _in, uint32_t _from, uint32_t _to)
{
using namespace bx;
uint32_t tmp0 = uint32_sll(1, _to);
uint32_t tmp1 = uint32_sll(1, _from);
uint32_t tmp2 = uint32_dec(tmp0);
uint32_t tmp3 = uint32_dec(tmp1);
uint32_t tmp4 = uint32_mul(_in, tmp2);
uint32_t tmp5 = uint32_add(tmp3, tmp4);
uint32_t tmp6 = uint32_srl(tmp5, _from);
uint32_t tmp7 = uint32_add(tmp5, tmp6);
uint32_t result = uint32_srl(tmp7, _from);
return uint8_t(result);
}
void decodeBlockDxt(uint8_t _dst[16*4], const uint8_t _src[8])
{
uint8_t colors[4*3];
uint32_t c0 = _src[0] | (_src[1] << 8);
colors[0] = bitRangeConvert( (c0>> 0)&0x1f, 5, 8);
colors[1] = bitRangeConvert( (c0>> 5)&0x3f, 6, 8);
colors[2] = bitRangeConvert( (c0>>11)&0x1f, 5, 8);
uint32_t c1 = _src[2] | (_src[3] << 8);
colors[3] = bitRangeConvert( (c1>> 0)&0x1f, 5, 8);
colors[4] = bitRangeConvert( (c1>> 5)&0x3f, 6, 8);
colors[5] = bitRangeConvert( (c1>>11)&0x1f, 5, 8);
colors[6] = (2*colors[0] + colors[3]) / 3;
colors[7] = (2*colors[1] + colors[4]) / 3;
colors[8] = (2*colors[2] + colors[5]) / 3;
colors[ 9] = (colors[0] + 2*colors[3]) / 3;
colors[10] = (colors[1] + 2*colors[4]) / 3;
colors[11] = (colors[2] + 2*colors[5]) / 3;
for (uint32_t ii = 0, next = 8*4; ii < 16*4; ii += 4, next += 2)
{
int idx = ( (_src[next>>3] >> (next & 7) ) & 3) * 3;
_dst[ii+0] = colors[idx+0];
_dst[ii+1] = colors[idx+1];
_dst[ii+2] = colors[idx+2];
}
}
void decodeBlockDxt1(uint8_t _dst[16*4], const uint8_t _src[8])
{
uint8_t colors[4*4];
uint32_t c0 = _src[0] | (_src[1] << 8);
colors[0] = bitRangeConvert( (c0>> 0)&0x1f, 5, 8);
colors[1] = bitRangeConvert( (c0>> 5)&0x3f, 6, 8);
colors[2] = bitRangeConvert( (c0>>11)&0x1f, 5, 8);
colors[3] = 255;
uint32_t c1 = _src[2] | (_src[3] << 8);
colors[4] = bitRangeConvert( (c1>> 0)&0x1f, 5, 8);
colors[5] = bitRangeConvert( (c1>> 5)&0x3f, 6, 8);
colors[6] = bitRangeConvert( (c1>>11)&0x1f, 5, 8);
colors[7] = 255;
if (c0 > c1)
{
colors[ 8] = (2*colors[0] + colors[4]) / 3;
colors[ 9] = (2*colors[1] + colors[5]) / 3;
colors[10] = (2*colors[2] + colors[6]) / 3;
colors[11] = 255;
colors[12] = (colors[0] + 2*colors[4]) / 3;
colors[13] = (colors[1] + 2*colors[5]) / 3;
colors[14] = (colors[2] + 2*colors[6]) / 3;
colors[15] = 255;
}
else
{
colors[ 8] = (colors[0] + colors[4]) / 2;
colors[ 9] = (colors[1] + colors[5]) / 2;
colors[10] = (colors[2] + colors[6]) / 2;
colors[11] = 255;
colors[12] = 0;
colors[13] = 0;
colors[14] = 0;
colors[15] = 0;
}
for (uint32_t ii = 0, next = 8*4; ii < 16*4; ii += 4, next += 2)
{
int idx = ( (_src[next>>3] >> (next & 7) ) & 3) * 4;
_dst[ii+0] = colors[idx+0];
_dst[ii+1] = colors[idx+1];
_dst[ii+2] = colors[idx+2];
_dst[ii+3] = colors[idx+3];
}
}
void decodeBlockDxt23A(uint8_t _dst[16*4], const uint8_t _src[8])
{
for (uint32_t ii = 0, next = 0; ii < 16*4; ii += 4, next += 4)
{
uint32_t c0 = (_src[next>>3] >> (next&7) ) & 0xf;
_dst[ii] = bitRangeConvert(c0, 4, 8);
}
}
void decodeBlockDxt45A(uint8_t _dst[16*4], const uint8_t _src[8])
{
uint8_t alpha[8];
alpha[0] = _src[0];
alpha[1] = _src[1];
if (alpha[0] > alpha[1])
{
alpha[2] = (6*alpha[0] + 1*alpha[1]) / 7;
alpha[3] = (5*alpha[0] + 2*alpha[1]) / 7;
alpha[4] = (4*alpha[0] + 3*alpha[1]) / 7;
alpha[5] = (3*alpha[0] + 4*alpha[1]) / 7;
alpha[6] = (2*alpha[0] + 5*alpha[1]) / 7;
alpha[7] = (1*alpha[0] + 6*alpha[1]) / 7;
}
else
{
alpha[2] = (4*alpha[0] + 1*alpha[1]) / 5;
alpha[3] = (3*alpha[0] + 2*alpha[1]) / 5;
alpha[4] = (2*alpha[0] + 3*alpha[1]) / 5;
alpha[5] = (1*alpha[0] + 4*alpha[1]) / 5;
alpha[6] = 0;
alpha[7] = 255;
}
uint32_t idx0 = _src[2];
uint32_t idx1 = _src[5];
idx0 |= uint32_t(_src[3])<<8;
idx1 |= uint32_t(_src[6])<<8;
idx0 |= uint32_t(_src[4])<<16;
idx1 |= uint32_t(_src[7])<<16;
for (uint32_t ii = 0; ii < 8*4; ii += 4)
{
_dst[ii] = alpha[idx0&7];
_dst[ii+32] = alpha[idx1&7];
idx0 >>= 3;
idx1 >>= 3;
}
}
static const int32_t s_etc1Mod[8][4] =
{
{ 2, 8, -2, -8},
{ 5, 17, -5, -17},
{ 9, 29, -9, -29},
{ 13, 42, -13, -42},
{ 18, 60, -18, -60},
{ 24, 80, -24, -80},
{ 33, 106, -33, -106},
{ 47, 183, -47, -183},
};
static const uint8_t s_etc2Mod[8] = { 3, 6, 11, 16, 23, 32, 41, 64 };
uint8_t uint8_sat(int32_t _a)
{
using namespace bx;
const uint32_t min = uint32_imin(_a, 255);
const uint32_t result = uint32_imax(min, 0);
return (uint8_t)result;
}
uint8_t uint8_satadd(int32_t _a, int32_t _b)
{
const int32_t add = _a + _b;
return uint8_sat(add);
}
void decodeBlockEtc2ModeT(uint8_t _dst[16*4], const uint8_t _src[8])
{
uint8_t rgb[16];
// 0 1 2 3 4 5 6 7
// 7654321076543210765432107654321076543210765432107654321076543210
// ...rr.rrggggbbbbrrrrggggbbbbDDD.mmmmmmmmmmmmmmmmllllllllllllllll
// ^ ^ ^ ^ ^
// +-- c0 +-- c1 | +-- msb +-- lsb
// +-- dist
rgb[ 0] = ( (_src[0] >> 1) & 0xc)
| (_src[0] & 0x3)
;
rgb[ 1] = _src[1] >> 4;
rgb[ 2] = _src[1] & 0xf;
rgb[ 8] = _src[2] >> 4;
rgb[ 9] = _src[2] & 0xf;
rgb[10] = _src[3] >> 4;
rgb[ 0] = bitRangeConvert(rgb[ 0], 4, 8);
rgb[ 1] = bitRangeConvert(rgb[ 1], 4, 8);
rgb[ 2] = bitRangeConvert(rgb[ 2], 4, 8);
rgb[ 8] = bitRangeConvert(rgb[ 8], 4, 8);
rgb[ 9] = bitRangeConvert(rgb[ 9], 4, 8);
rgb[10] = bitRangeConvert(rgb[10], 4, 8);
uint8_t dist = (_src[3] >> 1) & 0x7;
int32_t mod = s_etc2Mod[dist];
rgb[ 4] = uint8_satadd(rgb[ 8], mod);
rgb[ 5] = uint8_satadd(rgb[ 9], mod);
rgb[ 6] = uint8_satadd(rgb[10], mod);
rgb[12] = uint8_satadd(rgb[ 8], -mod);
rgb[13] = uint8_satadd(rgb[ 9], -mod);
rgb[14] = uint8_satadd(rgb[10], -mod);
uint32_t indexMsb = (_src[4]<<8) | _src[5];
uint32_t indexLsb = (_src[6]<<8) | _src[7];
for (uint32_t ii = 0; ii < 16; ++ii)
{
const uint32_t idx = (ii&0xc) | ( (ii & 0x3)<<4);
const uint32_t lsbi = indexLsb & 1;
const uint32_t msbi = (indexMsb & 1)<<1;
const uint32_t pal = (lsbi | msbi)<<2;
_dst[idx + 0] = rgb[pal+2];
_dst[idx + 1] = rgb[pal+1];
_dst[idx + 2] = rgb[pal+0];
_dst[idx + 3] = 255;
indexLsb >>= 1;
indexMsb >>= 1;
}
}
void decodeBlockEtc2ModeH(uint8_t _dst[16*4], const uint8_t _src[8])
{
uint8_t rgb[16];
// 0 1 2 3 4 5 6 7
// 7654321076543210765432107654321076543210765432107654321076543210
// .rrrrggg...gb.bbbrrrrggggbbbbDD.mmmmmmmmmmmmmmmmllllllllllllllll
// ^ ^ ^ ^ ^
// +-- c0 +-- c1 | +-- msb +-- lsb
// +-- dist
rgb[ 0] = (_src[0] >> 3) & 0xf;
rgb[ 1] = ( (_src[0] << 1) & 0xe)
| ( (_src[1] >> 4) & 0x1)
;
rgb[ 2] = (_src[1] & 0x8)
| ( (_src[1] << 1) & 0x6)
| (_src[2] >> 7)
;
rgb[ 8] = (_src[2] >> 3) & 0xf;
rgb[ 9] = ( (_src[2] << 1) & 0xe)
| (_src[3] >> 7)
;
rgb[10] = (_src[2] >> 3) & 0xf;
rgb[ 0] = bitRangeConvert(rgb[ 0], 4, 8);
rgb[ 1] = bitRangeConvert(rgb[ 1], 4, 8);
rgb[ 2] = bitRangeConvert(rgb[ 2], 4, 8);
rgb[ 8] = bitRangeConvert(rgb[ 8], 4, 8);
rgb[ 9] = bitRangeConvert(rgb[ 9], 4, 8);
rgb[10] = bitRangeConvert(rgb[10], 4, 8);
uint32_t col0 = uint32_t(rgb[0]<<16) | uint32_t(rgb[1]<<8) | uint32_t(rgb[ 2]);
uint32_t col1 = uint32_t(rgb[8]<<16) | uint32_t(rgb[9]<<8) | uint32_t(rgb[10]);
uint8_t dist = (_src[3] & 0x6) | (col0 >= col1);
int32_t mod = s_etc2Mod[dist];
rgb[ 4] = uint8_satadd(rgb[ 0], -mod);
rgb[ 5] = uint8_satadd(rgb[ 1], -mod);
rgb[ 6] = uint8_satadd(rgb[ 2], -mod);
rgb[ 0] = uint8_satadd(rgb[ 0], mod);
rgb[ 1] = uint8_satadd(rgb[ 1], mod);
rgb[ 2] = uint8_satadd(rgb[ 2], mod);
rgb[12] = uint8_satadd(rgb[ 8], -mod);
rgb[13] = uint8_satadd(rgb[ 9], -mod);
rgb[14] = uint8_satadd(rgb[10], -mod);
rgb[ 8] = uint8_satadd(rgb[ 8], mod);
rgb[ 9] = uint8_satadd(rgb[ 9], mod);
rgb[10] = uint8_satadd(rgb[10], mod);
uint32_t indexMsb = (_src[4]<<8) | _src[5];
uint32_t indexLsb = (_src[6]<<8) | _src[7];
for (uint32_t ii = 0; ii < 16; ++ii)
{
const uint32_t idx = (ii&0xc) | ( (ii & 0x3)<<4);
const uint32_t lsbi = indexLsb & 1;
const uint32_t msbi = (indexMsb & 1)<<1;
const uint32_t pal = (lsbi | msbi)<<2;
_dst[idx + 0] = rgb[pal+2];
_dst[idx + 1] = rgb[pal+1];
_dst[idx + 2] = rgb[pal+0];
_dst[idx + 3] = 255;
indexLsb >>= 1;
indexMsb >>= 1;
}
}
void decodeBlockEtc2ModePlanar(uint8_t _dst[16*4], const uint8_t _src[8])
{
// 0 1 2 3 4 5 6 7
// 7654321076543210765432107654321076543210765432107654321076543210
// .rrrrrrg.ggggggb...bb.bbbrrrrr.rgggggggbbbbbbrrrrrrgggggggbbbbbb
// ^ ^ ^
// +-- c0 +-- cH +-- cV
uint8_t c0[3];
uint8_t cH[3];
uint8_t cV[3];
c0[0] = (_src[0] >> 1) & 0x3f;
c0[1] = ( (_src[0] & 1) << 6)
| ( (_src[1] >> 1) & 0x3f)
;
c0[2] = ( (_src[1] & 1) << 5)
| ( (_src[2] & 0x18) )
| ( (_src[2] << 1) & 6)
| ( (_src[3] >> 7) )
;
cH[0] = ( (_src[3] >> 1) & 0x3e)
| (_src[3] & 1)
;
cH[1] = _src[4] >> 1;
cH[2] = ( (_src[4] & 1) << 5)
| (_src[5] >> 3)
;
cV[0] = ( (_src[5] & 0x7) << 3)
| (_src[6] >> 5)
;
cV[1] = ( (_src[6] & 0x1f) << 2)
| (_src[7] >> 5)
;
cV[2] = _src[7] & 0x3f;
c0[0] = bitRangeConvert(c0[0], 6, 8);
c0[1] = bitRangeConvert(c0[1], 7, 8);
c0[2] = bitRangeConvert(c0[2], 6, 8);
cH[0] = bitRangeConvert(cH[0], 6, 8);
cH[1] = bitRangeConvert(cH[1], 7, 8);
cH[2] = bitRangeConvert(cH[2], 6, 8);
cV[0] = bitRangeConvert(cV[0], 6, 8);
cV[1] = bitRangeConvert(cV[1], 7, 8);
cV[2] = bitRangeConvert(cV[2], 6, 8);
int16_t dy[3];
dy[0] = cV[0] - c0[0];
dy[1] = cV[1] - c0[1];
dy[2] = cV[2] - c0[2];
int16_t sx[3];
sx[0] = int16_t(c0[0])<<2;
sx[1] = int16_t(c0[1])<<2;
sx[2] = int16_t(c0[2])<<2;
int16_t ex[3];
ex[0] = int16_t(cH[0])<<2;
ex[1] = int16_t(cH[1])<<2;
ex[2] = int16_t(cH[2])<<2;
for (int32_t vv = 0; vv < 4; ++vv)
{
int16_t dx[3];
dx[0] = (ex[0] - sx[0])>>2;
dx[1] = (ex[1] - sx[1])>>2;
dx[2] = (ex[2] - sx[2])>>2;
for (int32_t hh = 0; hh < 4; ++hh)
{
const uint32_t idx = (vv<<4) + (hh<<2);
_dst[idx + 0] = uint8_sat( (sx[2] + dx[2]*hh)>>2);
_dst[idx + 1] = uint8_sat( (sx[1] + dx[1]*hh)>>2);
_dst[idx + 2] = uint8_sat( (sx[0] + dx[0]*hh)>>2);
_dst[idx + 3] = 255;
}
sx[0] += dy[0];
sx[1] += dy[1];
sx[2] += dy[2];
ex[0] += dy[0];
ex[1] += dy[1];
ex[2] += dy[2];
}
}
void decodeBlockEtc12(uint8_t _dst[16*4], const uint8_t _src[8])
{
bool flipBit = 0 != (_src[3] & 0x1);
bool diffBit = 0 != (_src[3] & 0x2);
uint8_t rgb[8];
if (diffBit)
{
rgb[0] = _src[0] >> 3;
rgb[1] = _src[1] >> 3;
rgb[2] = _src[2] >> 3;
int8_t diff[3];
diff[0] = int8_t( (_src[0] & 0x7)<<5)>>5;
diff[1] = int8_t( (_src[1] & 0x7)<<5)>>5;
diff[2] = int8_t( (_src[2] & 0x7)<<5)>>5;
int8_t rr = rgb[0] + diff[0];
int8_t gg = rgb[1] + diff[1];
int8_t bb = rgb[2] + diff[2];
// Etc2 3-modes
if (rr < 0 || rr > 31)
{
decodeBlockEtc2ModeT(_dst, _src);
return;
}
if (gg < 0 || gg > 31)
{
decodeBlockEtc2ModeH(_dst, _src);
return;
}
if (bb < 0 || bb > 31)
{
decodeBlockEtc2ModePlanar(_dst, _src);
return;
}
// Etc1
rgb[0] = bitRangeConvert(rgb[0], 5, 8);
rgb[1] = bitRangeConvert(rgb[1], 5, 8);
rgb[2] = bitRangeConvert(rgb[2], 5, 8);
rgb[4] = bitRangeConvert(rr, 5, 8);
rgb[5] = bitRangeConvert(gg, 5, 8);
rgb[6] = bitRangeConvert(bb, 5, 8);
}
else
{
rgb[0] = _src[0] >> 4;
rgb[1] = _src[1] >> 4;
rgb[2] = _src[2] >> 4;
rgb[4] = _src[0] & 0xf;
rgb[5] = _src[1] & 0xf;
rgb[6] = _src[2] & 0xf;
rgb[0] = bitRangeConvert(rgb[0], 4, 8);
rgb[1] = bitRangeConvert(rgb[1], 4, 8);
rgb[2] = bitRangeConvert(rgb[2], 4, 8);
rgb[4] = bitRangeConvert(rgb[4], 4, 8);
rgb[5] = bitRangeConvert(rgb[5], 4, 8);
rgb[6] = bitRangeConvert(rgb[6], 4, 8);
}
uint32_t table[2];
table[0] = (_src[3] >> 5) & 0x7;
table[1] = (_src[3] >> 2) & 0x7;
uint32_t indexMsb = (_src[4]<<8) | _src[5];
uint32_t indexLsb = (_src[6]<<8) | _src[7];
if (flipBit)
{
for (uint32_t ii = 0; ii < 16; ++ii)
{
const uint32_t block = (ii>>1)&1;
const uint32_t color = block<<2;
const uint32_t idx = (ii&0xc) | ( (ii & 0x3)<<4);
const uint32_t lsbi = indexLsb & 1;
const uint32_t msbi = (indexMsb & 1)<<1;
const int32_t mod = s_etc1Mod[table[block] ][lsbi | msbi];
_dst[idx + 0] = uint8_satadd(rgb[color+2], mod);
_dst[idx + 1] = uint8_satadd(rgb[color+1], mod);
_dst[idx + 2] = uint8_satadd(rgb[color+0], mod);
_dst[idx + 3] = 255;
indexLsb >>= 1;
indexMsb >>= 1;
}
}
else
{
for (uint32_t ii = 0; ii < 16; ++ii)
{
const uint32_t block = ii>>3;
const uint32_t color = block<<2;
const uint32_t idx = (ii&0xc) | ( (ii & 0x3)<<4);
const uint32_t lsbi = indexLsb & 1;
const uint32_t msbi = (indexMsb & 1)<<1;
const int32_t mod = s_etc1Mod[table[block] ][lsbi | msbi];
_dst[idx + 0] = uint8_satadd(rgb[color+2], mod);
_dst[idx + 1] = uint8_satadd(rgb[color+1], mod);
_dst[idx + 2] = uint8_satadd(rgb[color+0], mod);
_dst[idx + 3] = 255;
indexLsb >>= 1;
indexMsb >>= 1;
}
}
}
static const uint8_t s_pvrtcFactors[16][4] =
{
{ 4, 4, 4, 4 },
{ 2, 6, 2, 6 },
{ 8, 0, 8, 0 },
{ 6, 2, 6, 2 },
{ 2, 2, 6, 6 },
{ 1, 3, 3, 9 },
{ 4, 0, 12, 0 },
{ 3, 1, 9, 3 },
{ 8, 8, 0, 0 },
{ 4, 12, 0, 0 },
{ 16, 0, 0, 0 },
{ 12, 4, 0, 0 },
{ 6, 6, 2, 2 },
{ 3, 9, 1, 3 },
{ 12, 0, 4, 0 },
{ 9, 3, 3, 1 },
};
static const uint8_t s_pvrtcWeights[8][4] =
{
{ 8, 0, 8, 0 },
{ 5, 3, 5, 3 },
{ 3, 5, 3, 5 },
{ 0, 8, 0, 8 },
{ 8, 0, 8, 0 },
{ 4, 4, 4, 4 },
{ 4, 4, 0, 0 },
{ 0, 8, 0, 8 },
};
uint32_t morton2d(uint32_t _x, uint32_t _y)
{
using namespace bx;
const uint32_t tmpx = uint32_part1by1(_x);
const uint32_t xbits = uint32_sll(tmpx, 1);
const uint32_t ybits = uint32_part1by1(_y);
const uint32_t result = uint32_or(xbits, ybits);
return result;
}
uint32_t getColor(const uint8_t _src[8])
{
return 0
| _src[7]<<24
| _src[6]<<16
| _src[5]<<8
| _src[4]
;
}
void decodeBlockPtc14RgbAddA(uint32_t _block, uint32_t* _r, uint32_t* _g, uint32_t* _b, uint8_t _factor)
{
if (0 != (_block & (1<<15) ) )
{
*_r += bitRangeConvert( (_block >> 10) & 0x1f, 5, 8) * _factor;
*_g += bitRangeConvert( (_block >> 5) & 0x1f, 5, 8) * _factor;
*_b += bitRangeConvert( (_block >> 1) & 0x0f, 4, 8) * _factor;
}
else
{
*_r += bitRangeConvert( (_block >> 8) & 0xf, 4, 8) * _factor;
*_g += bitRangeConvert( (_block >> 4) & 0xf, 4, 8) * _factor;
*_b += bitRangeConvert( (_block >> 1) & 0x7, 3, 8) * _factor;
}
}
void decodeBlockPtc14RgbAddB(uint32_t _block, uint32_t* _r, uint32_t* _g, uint32_t* _b, uint8_t _factor)
{
if (0 != (_block & (1<<31) ) )
{
*_r += bitRangeConvert( (_block >> 26) & 0x1f, 5, 8) * _factor;
*_g += bitRangeConvert( (_block >> 21) & 0x1f, 5, 8) * _factor;
*_b += bitRangeConvert( (_block >> 16) & 0x1f, 5, 8) * _factor;
}
else
{
*_r += bitRangeConvert( (_block >> 24) & 0xf, 4, 8) * _factor;
*_g += bitRangeConvert( (_block >> 20) & 0xf, 4, 8) * _factor;
*_b += bitRangeConvert( (_block >> 16) & 0xf, 4, 8) * _factor;
}
}
void decodeBlockPtc14(uint8_t _dst[16*4], const uint8_t* _src, uint32_t _x, uint32_t _y, uint32_t _width, uint32_t _height)
{
// 0 1 2 3 4 5 6 7
// 7654321076543210765432107654321076543210765432107654321076543210
// mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmyrrrrrgggggbbbbbxrrrrrgggggbbbbp
// ^ ^^ ^^ ^
// +-- modulation data |+- B color |+- A color |
// +-- B opaque +-- A opaque |
// alpha punchthrough --+
const uint8_t* bc = &_src[morton2d(_x, _y) * 8];
uint32_t mod = 0
| bc[3]<<24
| bc[2]<<16
| bc[1]<<8
| bc[0]
;
const bool punchthrough = !!(bc[7] & 1);
const uint8_t* weightTable = s_pvrtcWeights[4 * punchthrough];
const uint8_t* factorTable = s_pvrtcFactors[0];
for (int yy = 0; yy < 4; ++yy)
{
const uint32_t yOffset = (yy < 2) ? -1 : 0;
const uint32_t y0 = (_y + yOffset) % _height;
const uint32_t y1 = (y0 + 1) % _height;
for (int xx = 0; xx < 4; ++xx)
{
const uint32_t xOffset = (xx < 2) ? -1 : 0;
const uint32_t x0 = (_x + xOffset) % _width;
const uint32_t x1 = (x0 + 1) % _width;
const uint32_t bc0 = getColor(&_src[morton2d(x0, y0) * 8]);
const uint32_t bc1 = getColor(&_src[morton2d(x1, y0) * 8]);
const uint32_t bc2 = getColor(&_src[morton2d(x0, y1) * 8]);
const uint32_t bc3 = getColor(&_src[morton2d(x1, y1) * 8]);
const uint8_t f0 = factorTable[0];
const uint8_t f1 = factorTable[1];
const uint8_t f2 = factorTable[2];
const uint8_t f3 = factorTable[3];
uint32_t ar = 0, ag = 0, ab = 0;
decodeBlockPtc14RgbAddA(bc0, &ar, &ag, &ab, f0);
decodeBlockPtc14RgbAddA(bc1, &ar, &ag, &ab, f1);
decodeBlockPtc14RgbAddA(bc2, &ar, &ag, &ab, f2);
decodeBlockPtc14RgbAddA(bc3, &ar, &ag, &ab, f3);
uint32_t br = 0, bg = 0, bb = 0;
decodeBlockPtc14RgbAddB(bc0, &br, &bg, &bb, f0);
decodeBlockPtc14RgbAddB(bc1, &br, &bg, &bb, f1);
decodeBlockPtc14RgbAddB(bc2, &br, &bg, &bb, f2);
decodeBlockPtc14RgbAddB(bc3, &br, &bg, &bb, f3);
const uint8_t* weight = &weightTable[(mod & 3)*4];
const uint8_t wa = weight[0];
const uint8_t wb = weight[1];
_dst[(yy*4 + xx)*4+0] = uint8_t( (ab * wa + bb * wb) >> 7);
_dst[(yy*4 + xx)*4+1] = uint8_t( (ag * wa + bg * wb) >> 7);
_dst[(yy*4 + xx)*4+2] = uint8_t( (ar * wa + br * wb) >> 7);
_dst[(yy*4 + xx)*4+3] = 255;
mod >>= 2;
factorTable += 4;
}
}
}
void decodeBlockPtc14ARgbaAddA(uint32_t _block, uint32_t* _r, uint32_t* _g, uint32_t* _b, uint32_t* _a, uint8_t _factor)
{
if (0 != (_block & (1<<15) ) )
{
*_r += bitRangeConvert( (_block >> 10) & 0x1f, 5, 8) * _factor;
*_g += bitRangeConvert( (_block >> 5) & 0x1f, 5, 8) * _factor;
*_b += bitRangeConvert( (_block >> 1) & 0x0f, 4, 8) * _factor;
*_a += 255;
}
else
{
*_r += bitRangeConvert( (_block >> 8) & 0xf, 4, 8) * _factor;
*_g += bitRangeConvert( (_block >> 4) & 0xf, 4, 8) * _factor;
*_b += bitRangeConvert( (_block >> 1) & 0x7, 3, 8) * _factor;
*_a += bitRangeConvert( (_block >> 12) & 0x7, 3, 8) * _factor;
}
}
void decodeBlockPtc14ARgbaAddB(uint32_t _block, uint32_t* _r, uint32_t* _g, uint32_t* _b, uint32_t* _a, uint8_t _factor)
{
if (0 != (_block & (1<<31) ) )
{
*_r += bitRangeConvert( (_block >> 26) & 0x1f, 5, 8) * _factor;
*_g += bitRangeConvert( (_block >> 21) & 0x1f, 5, 8) * _factor;
*_b += bitRangeConvert( (_block >> 16) & 0x1f, 5, 8) * _factor;
*_a += 255;
}
else
{
*_r += bitRangeConvert( (_block >> 24) & 0xf, 4, 8) * _factor;
*_g += bitRangeConvert( (_block >> 20) & 0xf, 4, 8) * _factor;
*_b += bitRangeConvert( (_block >> 16) & 0xf, 4, 8) * _factor;
*_a += bitRangeConvert( (_block >> 28) & 0x7, 3, 8) * _factor;
}
}
void decodeBlockPtc14A(uint8_t _dst[16*4], const uint8_t* _src, uint32_t _x, uint32_t _y, uint32_t _width, uint32_t _height)
{
// 0 1 2 3 4 5 6 7
// 7654321076543210765432107654321076543210765432107654321076543210
// mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmyrrrrrgggggbbbbbxrrrrrgggggbbbbp
// ^ ^^ ^^ ^
// +-- modulation data |+- B color |+- A color |
// +-- B opaque +-- A opaque |
// alpha punchthrough --+
const uint8_t* bc = &_src[morton2d(_x, _y) * 8];
uint32_t mod = 0
| bc[3]<<24
| bc[2]<<16
| bc[1]<<8
| bc[0]
;
const bool punchthrough = !!(bc[7] & 1);
const uint8_t* weightTable = s_pvrtcWeights[4 * punchthrough];
const uint8_t* factorTable = s_pvrtcFactors[0];
for (int yy = 0; yy < 4; ++yy)
{
const uint32_t yOffset = (yy < 2) ? -1 : 0;
const uint32_t y0 = (_y + yOffset) % _height;
const uint32_t y1 = (y0 + 1) % _height;
for (int xx = 0; xx < 4; ++xx)
{
const uint32_t xOffset = (xx < 2) ? -1 : 0;
const uint32_t x0 = (_x + xOffset) % _width;
const uint32_t x1 = (x0 + 1) % _width;
const uint32_t bc0 = getColor(&_src[morton2d(x0, y0) * 8]);
const uint32_t bc1 = getColor(&_src[morton2d(x1, y0) * 8]);
const uint32_t bc2 = getColor(&_src[morton2d(x0, y1) * 8]);
const uint32_t bc3 = getColor(&_src[morton2d(x1, y1) * 8]);
const uint8_t f0 = factorTable[0];
const uint8_t f1 = factorTable[1];
const uint8_t f2 = factorTable[2];
const uint8_t f3 = factorTable[3];
uint32_t ar = 0, ag = 0, ab = 0, aa = 0;
decodeBlockPtc14ARgbaAddA(bc0, &ar, &ag, &ab, &aa, f0);
decodeBlockPtc14ARgbaAddA(bc1, &ar, &ag, &ab, &aa, f1);
decodeBlockPtc14ARgbaAddA(bc2, &ar, &ag, &ab, &aa, f2);
decodeBlockPtc14ARgbaAddA(bc3, &ar, &ag, &ab, &aa, f3);
uint32_t br = 0, bg = 0, bb = 0, ba = 0;
decodeBlockPtc14ARgbaAddB(bc0, &br, &bg, &bb, &ba, f0);
decodeBlockPtc14ARgbaAddB(bc1, &br, &bg, &bb, &ba, f1);
decodeBlockPtc14ARgbaAddB(bc2, &br, &bg, &bb, &ba, f2);
decodeBlockPtc14ARgbaAddB(bc3, &br, &bg, &bb, &ba, f3);
const uint8_t* weight = &weightTable[(mod & 3)*4];
const uint8_t wa = weight[0];
const uint8_t wb = weight[1];
const uint8_t wc = weight[2];
const uint8_t wd = weight[3];
_dst[(yy*4 + xx)*4+0] = uint8_t( (ab * wa + bb * wb) >> 7);
_dst[(yy*4 + xx)*4+1] = uint8_t( (ag * wa + bg * wb) >> 7);
_dst[(yy*4 + xx)*4+2] = uint8_t( (ar * wa + br * wb) >> 7);
_dst[(yy*4 + xx)*4+3] = uint8_t( (aa * wc + ba * wd) >> 7);
mod >>= 2;
factorTable += 4;
}
}
}
const Memory* imageAlloc(ImageContainer& _imageContainer, TextureFormat::Enum _format, uint16_t _width, uint16_t _height, uint16_t _depth, bool _cubeMap, bool _mipsActive)
{
const uint8_t numMips = _mipsActive ? imageGetNumMips(_format, _width, _height) : 1;
uint32_t size = imageGetSize(_format, _width, _height, 0, false, numMips);
const Memory* image = alloc(size);
_imageContainer.m_data = image->data;
_imageContainer.m_format = _format;
_imageContainer.m_size = image->size;
_imageContainer.m_offset = 0;
_imageContainer.m_width = _width;
_imageContainer.m_height = _height;
_imageContainer.m_depth = _depth;
_imageContainer.m_numMips = numMips;
_imageContainer.m_hasAlpha = false;
_imageContainer.m_cubeMap = _cubeMap;
_imageContainer.m_ktx = false;
_imageContainer.m_ktxLE = false;
_imageContainer.m_srgb = false;
return image;
}
void imageFree(const Memory* _memory)
{
release(_memory);
}
// DDS
#define DDS_MAGIC BX_MAKEFOURCC('D', 'D', 'S', ' ')
#define DDS_HEADER_SIZE 124
#define DDS_DXT1 BX_MAKEFOURCC('D', 'X', 'T', '1')
#define DDS_DXT2 BX_MAKEFOURCC('D', 'X', 'T', '2')
#define DDS_DXT3 BX_MAKEFOURCC('D', 'X', 'T', '3')
#define DDS_DXT4 BX_MAKEFOURCC('D', 'X', 'T', '4')
#define DDS_DXT5 BX_MAKEFOURCC('D', 'X', 'T', '5')
#define DDS_ATI1 BX_MAKEFOURCC('A', 'T', 'I', '1')
#define DDS_BC4U BX_MAKEFOURCC('B', 'C', '4', 'U')
#define DDS_ATI2 BX_MAKEFOURCC('A', 'T', 'I', '2')
#define DDS_BC5U BX_MAKEFOURCC('B', 'C', '5', 'U')
#define DDS_DX10 BX_MAKEFOURCC('D', 'X', '1', '0')
#define DDS_A8R8G8B8 21
#define DDS_R5G6B5 23
#define DDS_A1R5G5B5 25
#define DDS_A4R4G4B4 26
#define DDS_A2B10G10R10 31
#define DDS_G16R16 34
#define DDS_A2R10G10B10 35
#define DDS_A16B16G16R16 36
#define DDS_A8L8 51
#define DDS_R16F 111
#define DDS_G16R16F 112
#define DDS_A16B16G16R16F 113
#define DDS_R32F 114
#define DDS_G32R32F 115
#define DDS_A32B32G32R32F 116
#define DDS_FORMAT_R32G32B32A32_FLOAT 2
#define DDS_FORMAT_R32G32B32A32_UINT 3
#define DDS_FORMAT_R16G16B16A16_FLOAT 10
#define DDS_FORMAT_R16G16B16A16_UNORM 11
#define DDS_FORMAT_R16G16B16A16_UINT 12
#define DDS_FORMAT_R32G32_FLOAT 16
#define DDS_FORMAT_R32G32_UINT 17
#define DDS_FORMAT_R10G10B10A2_UNORM 24
#define DDS_FORMAT_R11G11B10_FLOAT 26
#define DDS_FORMAT_R8G8B8A8_UNORM 28
#define DDS_FORMAT_R8G8B8A8_UNORM_SRGB 29
#define DDS_FORMAT_R16G16_FLOAT 34
#define DDS_FORMAT_R16G16_UNORM 35
#define DDS_FORMAT_R32_FLOAT 41
#define DDS_FORMAT_R32_UINT 42
#define DDS_FORMAT_R8G8_UNORM 49
#define DDS_FORMAT_R16_FLOAT 54
#define DDS_FORMAT_R16_UNORM 56
#define DDS_FORMAT_R8_UNORM 61
#define DDS_FORMAT_R1_UNORM 66
#define DDS_FORMAT_BC1_UNORM 71
#define DDS_FORMAT_BC1_UNORM_SRGB 72
#define DDS_FORMAT_BC2_UNORM 74
#define DDS_FORMAT_BC2_UNORM_SRGB 75
#define DDS_FORMAT_BC3_UNORM 77
#define DDS_FORMAT_BC3_UNORM_SRGB 78
#define DDS_FORMAT_BC4_UNORM 80
#define DDS_FORMAT_BC5_UNORM 83
#define DDS_FORMAT_B5G6R5_UNORM 85
#define DDS_FORMAT_B5G5R5A1_UNORM 86
#define DDS_FORMAT_B8G8R8A8_UNORM 87
#define DDS_FORMAT_B8G8R8A8_UNORM_SRGB 91
#define DDS_FORMAT_BC6H_SF16 96
#define DDS_FORMAT_BC7_UNORM 98
#define DDS_FORMAT_BC7_UNORM_SRGB 99
#define DDS_FORMAT_B4G4R4A4_UNORM 115
#define DDSD_CAPS 0x00000001
#define DDSD_HEIGHT 0x00000002
#define DDSD_WIDTH 0x00000004
#define DDSD_PITCH 0x00000008
#define DDSD_PIXELFORMAT 0x00001000
#define DDSD_MIPMAPCOUNT 0x00020000
#define DDSD_LINEARSIZE 0x00080000
#define DDSD_DEPTH 0x00800000
#define DDPF_ALPHAPIXELS 0x00000001
#define DDPF_ALPHA 0x00000002
#define DDPF_FOURCC 0x00000004
#define DDPF_INDEXED 0x00000020
#define DDPF_RGB 0x00000040
#define DDPF_YUV 0x00000200
#define DDPF_LUMINANCE 0x00020000
#define DDSCAPS_COMPLEX 0x00000008
#define DDSCAPS_TEXTURE 0x00001000
#define DDSCAPS_MIPMAP 0x00400000
#define DDSCAPS2_CUBEMAP 0x00000200
#define DDSCAPS2_CUBEMAP_POSITIVEX 0x00000400
#define DDSCAPS2_CUBEMAP_NEGATIVEX 0x00000800
#define DDSCAPS2_CUBEMAP_POSITIVEY 0x00001000
#define DDSCAPS2_CUBEMAP_NEGATIVEY 0x00002000
#define DDSCAPS2_CUBEMAP_POSITIVEZ 0x00004000
#define DDSCAPS2_CUBEMAP_NEGATIVEZ 0x00008000
#define DDS_CUBEMAP_ALLFACES (DDSCAPS2_CUBEMAP_POSITIVEX|DDSCAPS2_CUBEMAP_NEGATIVEX \
|DDSCAPS2_CUBEMAP_POSITIVEY|DDSCAPS2_CUBEMAP_NEGATIVEY \
|DDSCAPS2_CUBEMAP_POSITIVEZ|DDSCAPS2_CUBEMAP_NEGATIVEZ)
#define DDSCAPS2_VOLUME 0x00200000
struct TranslateDdsFormat
{
uint32_t m_format;
TextureFormat::Enum m_textureFormat;
bool m_srgb;
};
static TranslateDdsFormat s_translateDdsFourccFormat[] =
{
{ DDS_DXT1, TextureFormat::BC1, false },
{ DDS_DXT2, TextureFormat::BC2, false },
{ DDS_DXT3, TextureFormat::BC2, false },
{ DDS_DXT4, TextureFormat::BC3, false },
{ DDS_DXT5, TextureFormat::BC3, false },
{ DDS_ATI1, TextureFormat::BC4, false },
{ DDS_BC4U, TextureFormat::BC4, false },
{ DDS_ATI2, TextureFormat::BC5, false },
{ DDS_BC5U, TextureFormat::BC5, false },
{ DDS_A16B16G16R16, TextureFormat::RGBA16, false },
{ DDS_A16B16G16R16F, TextureFormat::RGBA16F, false },
{ DDPF_RGB|DDPF_ALPHAPIXELS, TextureFormat::BGRA8, false },
{ DDPF_INDEXED, TextureFormat::R8, false },
{ DDPF_LUMINANCE, TextureFormat::R8, false },
{ DDPF_ALPHA, TextureFormat::R8, false },
{ DDS_R16F, TextureFormat::R16F, false },
{ DDS_R32F, TextureFormat::R32F, false },
{ DDS_A8L8, TextureFormat::RG8, false },
{ DDS_G16R16, TextureFormat::RG16, false },
{ DDS_G16R16F, TextureFormat::RG16F, false },
{ DDS_G32R32F, TextureFormat::RG32F, false },
{ DDS_A8R8G8B8, TextureFormat::BGRA8, false },
{ DDS_A16B16G16R16, TextureFormat::RGBA16, false },
{ DDS_A16B16G16R16F, TextureFormat::RGBA16F, false },
{ DDS_A32B32G32R32F, TextureFormat::RGBA32F, false },
{ DDS_R5G6B5, TextureFormat::R5G6B5, false },
{ DDS_A4R4G4B4, TextureFormat::RGBA4, false },
{ DDS_A1R5G5B5, TextureFormat::RGB5A1, false },
{ DDS_A2B10G10R10, TextureFormat::RGB10A2, false },
};
static TranslateDdsFormat s_translateDxgiFormat[] =
{
{ DDS_FORMAT_BC1_UNORM, TextureFormat::BC1, false },
{ DDS_FORMAT_BC1_UNORM_SRGB, TextureFormat::BC1, true },
{ DDS_FORMAT_BC2_UNORM, TextureFormat::BC2, false },
{ DDS_FORMAT_BC2_UNORM_SRGB, TextureFormat::BC2, true },
{ DDS_FORMAT_BC3_UNORM, TextureFormat::BC3, false },
{ DDS_FORMAT_BC3_UNORM_SRGB, TextureFormat::BC3, true },
{ DDS_FORMAT_BC4_UNORM, TextureFormat::BC4, false },
{ DDS_FORMAT_BC5_UNORM, TextureFormat::BC5, false },
{ DDS_FORMAT_BC6H_SF16, TextureFormat::BC6H, false },
{ DDS_FORMAT_BC7_UNORM, TextureFormat::BC7, false },
{ DDS_FORMAT_BC7_UNORM_SRGB, TextureFormat::BC7, true },
{ DDS_FORMAT_R1_UNORM, TextureFormat::R1, false },
{ DDS_FORMAT_R8_UNORM, TextureFormat::R8, false },
{ DDS_FORMAT_R16_UNORM, TextureFormat::R16, false },
{ DDS_FORMAT_R16_FLOAT, TextureFormat::R16F, false },
{ DDS_FORMAT_R32_UINT, TextureFormat::R32U, false },
{ DDS_FORMAT_R32_FLOAT, TextureFormat::R32F, false },
{ DDS_FORMAT_R8G8_UNORM, TextureFormat::RG8, false },
{ DDS_FORMAT_R16G16_UNORM, TextureFormat::RG16, false },
{ DDS_FORMAT_R16G16_FLOAT, TextureFormat::RG16F, false },
{ DDS_FORMAT_R32G32_UINT, TextureFormat::RG32U, false },
{ DDS_FORMAT_R32G32_FLOAT, TextureFormat::RG32F, false },
{ DDS_FORMAT_B8G8R8A8_UNORM, TextureFormat::BGRA8, false },
{ DDS_FORMAT_B8G8R8A8_UNORM_SRGB, TextureFormat::BGRA8, true },
{ DDS_FORMAT_R8G8B8A8_UNORM, TextureFormat::RGBA8, false },
{ DDS_FORMAT_R8G8B8A8_UNORM_SRGB, TextureFormat::RGBA8, true },
{ DDS_FORMAT_R16G16B16A16_UNORM, TextureFormat::RGBA16, false },
{ DDS_FORMAT_R16G16B16A16_FLOAT, TextureFormat::RGBA16F, false },
{ DDS_FORMAT_R32G32B32A32_UINT, TextureFormat::RGBA32U, false },
{ DDS_FORMAT_R32G32B32A32_FLOAT, TextureFormat::RGBA32F, false },
{ DDS_FORMAT_B5G6R5_UNORM, TextureFormat::R5G6B5, false },
{ DDS_FORMAT_B4G4R4A4_UNORM, TextureFormat::RGBA4, false },
{ DDS_FORMAT_B5G5R5A1_UNORM, TextureFormat::RGB5A1, false },
{ DDS_FORMAT_R10G10B10A2_UNORM, TextureFormat::RGB10A2, false },
{ DDS_FORMAT_R11G11B10_FLOAT, TextureFormat::R11G11B10F, false },
};
struct TranslateDdsPixelFormat
{
uint32_t m_bitCount;
uint32_t m_bitmask[4];
TextureFormat::Enum m_textureFormat;
};
static TranslateDdsPixelFormat s_translateDdsPixelFormat[] =
{
{ 8, { 0x000000ff, 0x00000000, 0x00000000, 0x00000000 }, TextureFormat::R8 },
{ 16, { 0x0000ffff, 0x00000000, 0x00000000, 0x00000000 }, TextureFormat::R16U },
{ 16, { 0x00000f00, 0x000000f0, 0x0000000f, 0x0000f000 }, TextureFormat::RGBA4 },
{ 16, { 0x0000f800, 0x000007e0, 0x0000001f, 0x00000000 }, TextureFormat::R5G6B5 },
{ 16, { 0x00007c00, 0x000003e0, 0x0000001f, 0x00008000 }, TextureFormat::RGB5A1 },
{ 32, { 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000 }, TextureFormat::BGRA8 },
{ 32, { 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000 }, TextureFormat::BGRA8 },
{ 32, { 0x000003ff, 0x000ffc00, 0x3ff00000, 0xc0000000 }, TextureFormat::RGB10A2 },
{ 32, { 0x0000ffff, 0xffff0000, 0x00000000, 0x00000000 }, TextureFormat::RG16 },
{ 32, { 0xffffffff, 0x00000000, 0x00000000, 0x00000000 }, TextureFormat::R32U },
};
bool imageParseDds(ImageContainer& _imageContainer, bx::ReaderSeekerI* _reader)
{
uint32_t headerSize;
bx::read(_reader, headerSize);
if (headerSize < DDS_HEADER_SIZE)
{
return false;
}
uint32_t flags;
bx::read(_reader, flags);
if ( (flags & (DDSD_CAPS|DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT) ) != (DDSD_CAPS|DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT) )
{
return false;
}
uint32_t height;
bx::read(_reader, height);
uint32_t width;
bx::read(_reader, width);
uint32_t pitch;
bx::read(_reader, pitch);
uint32_t depth;
bx::read(_reader, depth);
uint32_t mips;
bx::read(_reader, mips);
bx::skip(_reader, 44); // reserved
uint32_t pixelFormatSize;
bx::read(_reader, pixelFormatSize);
uint32_t pixelFlags;
bx::read(_reader, pixelFlags);
uint32_t fourcc;
bx::read(_reader, fourcc);
uint32_t bitCount;
bx::read(_reader, bitCount);
uint32_t bitmask[4];
bx::read(_reader, bitmask, sizeof(bitmask) );
uint32_t caps[4];
bx::read(_reader, caps);
bx::skip(_reader, 4); // reserved
uint32_t dxgiFormat = 0;
if (DDPF_FOURCC == pixelFlags
&& DDS_DX10 == fourcc)
{
bx::read(_reader, dxgiFormat);
uint32_t dims;
bx::read(_reader, dims);
uint32_t miscFlags;
bx::read(_reader, miscFlags);
uint32_t arraySize;
bx::read(_reader, arraySize);
uint32_t miscFlags2;
bx::read(_reader, miscFlags2);
}
if ( (caps[0] & DDSCAPS_TEXTURE) == 0)
{
return false;
}
bool cubeMap = 0 != (caps[1] & DDSCAPS2_CUBEMAP);
if (cubeMap)
{
if ( (caps[1] & DDS_CUBEMAP_ALLFACES) != DDS_CUBEMAP_ALLFACES)
{
// partial cube map is not supported.
return false;
}
}
TextureFormat::Enum format = TextureFormat::Unknown;
bool hasAlpha = pixelFlags & DDPF_ALPHAPIXELS;
bool srgb = false;
if (dxgiFormat == 0)
{
if (DDPF_FOURCC == (pixelFlags & DDPF_FOURCC) )
{
for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateDdsFourccFormat); ++ii)
{
if (s_translateDdsFourccFormat[ii].m_format == fourcc)
{
format = s_translateDdsFourccFormat[ii].m_textureFormat;
break;
}
}
}
else
{
for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateDdsPixelFormat); ++ii)
{
const TranslateDdsPixelFormat& pf = s_translateDdsPixelFormat[ii];
if (pf.m_bitCount == bitCount
&& pf.m_bitmask[0] == bitmask[0]
&& pf.m_bitmask[1] == bitmask[1]
&& pf.m_bitmask[2] == bitmask[2]
&& pf.m_bitmask[3] == bitmask[3])
{
format = pf.m_textureFormat;
break;
}
}
}
}
else
{
for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateDxgiFormat); ++ii)
{
if (s_translateDxgiFormat[ii].m_format == dxgiFormat)
{
format = s_translateDxgiFormat[ii].m_textureFormat;
srgb = s_translateDxgiFormat[ii].m_srgb;
break;
}
}
}
_imageContainer.m_data = NULL;
_imageContainer.m_size = 0;
_imageContainer.m_offset = (uint32_t)bx::seek(_reader);
_imageContainer.m_width = width;
_imageContainer.m_height = height;
_imageContainer.m_depth = depth;
_imageContainer.m_format = format;
_imageContainer.m_numMips = uint8_t( (caps[0] & DDSCAPS_MIPMAP) ? mips : 1);
_imageContainer.m_hasAlpha = hasAlpha;
_imageContainer.m_cubeMap = cubeMap;
_imageContainer.m_ktx = false;
_imageContainer.m_ktxLE = false;
_imageContainer.m_srgb = srgb;
return TextureFormat::Unknown != format;
}
// KTX
#define KTX_MAGIC BX_MAKEFOURCC(0xAB, 'K', 'T', 'X')
#define KTX_HEADER_SIZE 64
#define KTX_ETC1_RGB8_OES 0x8D64
#define KTX_COMPRESSED_R11_EAC 0x9270
#define KTX_COMPRESSED_SIGNED_R11_EAC 0x9271
#define KTX_COMPRESSED_RG11_EAC 0x9272
#define KTX_COMPRESSED_SIGNED_RG11_EAC 0x9273
#define KTX_COMPRESSED_RGB8_ETC2 0x9274
#define KTX_COMPRESSED_SRGB8_ETC2 0x9275
#define KTX_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2 0x9276
#define KTX_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2 0x9277
#define KTX_COMPRESSED_RGBA8_ETC2_EAC 0x9278
#define KTX_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC 0x9279
#define KTX_COMPRESSED_RGB_PVRTC_4BPPV1_IMG 0x8C00
#define KTX_COMPRESSED_RGB_PVRTC_2BPPV1_IMG 0x8C01
#define KTX_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG 0x8C02
#define KTX_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG 0x8C03
#define KTX_COMPRESSED_RGBA_PVRTC_2BPPV2_IMG 0x9137
#define KTX_COMPRESSED_RGBA_PVRTC_4BPPV2_IMG 0x9138
#define KTX_COMPRESSED_RGBA_S3TC_DXT1_EXT 0x83F1
#define KTX_COMPRESSED_RGBA_S3TC_DXT3_EXT 0x83F2
#define KTX_COMPRESSED_RGBA_S3TC_DXT5_EXT 0x83F3
#define KTX_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT 0x8C4D
#define KTX_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT 0x8C4E
#define KTX_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT 0x8C4F
#define KTX_COMPRESSED_LUMINANCE_LATC1_EXT 0x8C70
#define KTX_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT 0x8C72
#define KTX_COMPRESSED_RGBA_BPTC_UNORM_ARB 0x8E8C
#define KTX_COMPRESSED_SRGB_ALPHA_BPTC_UNORM_ARB 0x8E8D
#define KTX_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB 0x8E8E
#define KTX_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT_ARB 0x8E8F
#define KTX_COMPRESSED_SRGB_PVRTC_2BPPV1_EXT 0x8A54
#define KTX_COMPRESSED_SRGB_PVRTC_4BPPV1_EXT 0x8A55
#define KTX_COMPRESSED_SRGB_ALPHA_PVRTC_2BPPV1_EXT 0x8A56
#define KTX_COMPRESSED_SRGB_ALPHA_PVRTC_4BPPV1_EXT 0x8A57
#define KTX_R8 0x8229
#define KTX_R16 0x822A
#define KTX_RG8 0x822B
#define KTX_RG16 0x822C
#define KTX_R16F 0x822D
#define KTX_R32F 0x822E
#define KTX_RG16F 0x822F
#define KTX_RG32F 0x8230
#define KTX_RGBA8 0x8058
#define KTX_RGBA16 0x805B
#define KTX_RGBA16F 0x881A
#define KTX_R32UI 0x8236
#define KTX_RG32UI 0x823C
#define KTX_RGBA32UI 0x8D70
#define KTX_RGBA32F 0x8814
#define KTX_RGB565 0x8D62
#define KTX_RGBA4 0x8056
#define KTX_RGB5_A1 0x8057
#define KTX_RGB10_A2 0x8059
#define KTX_R8I 0x8231
#define KTX_R8UI 0x8232
#define KTX_R16I 0x8233
#define KTX_R16UI 0x8234
#define KTX_R32I 0x8235
#define KTX_R32UI 0x8236
#define KTX_RG8I 0x8237
#define KTX_RG8UI 0x8238
#define KTX_RG16I 0x8239
#define KTX_RG16UI 0x823A
#define KTX_RG32I 0x823B
#define KTX_RG32UI 0x823C
#define KTX_R8_SNORM 0x8F94
#define KTX_RG8_SNORM 0x8F95
#define KTX_RGB8_SNORM 0x8F96
#define KTX_RGBA8_SNORM 0x8F97
#define KTX_R16_SNORM 0x8F98
#define KTX_RG16_SNORM 0x8F99
#define KTX_RGB16_SNORM 0x8F9A
#define KTX_RGBA16_SNORM 0x8F9B
#define KTX_SRGB8_ALPHA8 0x8C43
#define KTX_RGBA32UI 0x8D70
#define KTX_RGB32UI 0x8D71
#define KTX_RGBA16UI 0x8D76
#define KTX_RGB16UI 0x8D77
#define KTX_RGBA8UI 0x8D7C
#define KTX_RGB8UI 0x8D7D
#define KTX_RGBA32I 0x8D82
#define KTX_RGB32I 0x8D83
#define KTX_RGBA16I 0x8D88
#define KTX_RGB16I 0x8D89
#define KTX_RGBA8I 0x8D8E
#define KTX_RGB8I 0x8D8F
#define KTX_RGB9_E5 0x8C3D
#define KTX_R11F_G11F_B10F 0x8C3A
#define KTX_ZERO 0
#define KTX_RED 0x1903
#define KTX_ALPHA 0x1906
#define KTX_RGB 0x1907
#define KTX_RGBA 0x1908
#define KTX_BGRA 0x80E1
#define KTX_RG 0x8227
#define KTX_BYTE 0x1400
#define KTX_UNSIGNED_BYTE 0x1401
#define KTX_SHORT 0x1402
#define KTX_UNSIGNED_SHORT 0x1403
#define KTX_INT 0x1404
#define KTX_UNSIGNED_INT 0x1405
#define KTX_FLOAT 0x1406
#define KTX_HALF_FLOAT 0x140B
#define KTX_UNSIGNED_INT_5_9_9_9_REV 0x8C3E
#define KTX_UNSIGNED_SHORT_5_6_5 0x8363
#define KTX_UNSIGNED_SHORT_4_4_4_4 0x8033
#define KTX_UNSIGNED_SHORT_5_5_5_1 0x8034
#define KTX_UNSIGNED_INT_2_10_10_10_REV 0x8368
#define KTX_UNSIGNED_INT_10F_11F_11F_REV 0x8C3B
struct KtxFormatInfo
{
uint32_t m_internalFmt;
uint32_t m_internalFmtSrgb;
uint32_t m_fmt;
uint32_t m_type;
};
static KtxFormatInfo s_translateKtxFormat[] =
{
{ KTX_COMPRESSED_RGBA_S3TC_DXT1_EXT, KTX_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT, KTX_COMPRESSED_RGBA_S3TC_DXT1_EXT, KTX_ZERO, }, // BC1
{ KTX_COMPRESSED_RGBA_S3TC_DXT3_EXT, KTX_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT, KTX_COMPRESSED_RGBA_S3TC_DXT3_EXT, KTX_ZERO, }, // BC2
{ KTX_COMPRESSED_RGBA_S3TC_DXT5_EXT, KTX_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT, KTX_COMPRESSED_RGBA_S3TC_DXT5_EXT, KTX_ZERO, }, // BC3
{ KTX_COMPRESSED_LUMINANCE_LATC1_EXT, KTX_ZERO, KTX_COMPRESSED_LUMINANCE_LATC1_EXT, KTX_ZERO, }, // BC4
{ KTX_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT, KTX_ZERO, KTX_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT, KTX_ZERO, }, // BC5
{ KTX_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB, KTX_ZERO, KTX_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB, KTX_ZERO, }, // BC6H
{ KTX_COMPRESSED_RGBA_BPTC_UNORM_ARB, KTX_ZERO, KTX_COMPRESSED_RGBA_BPTC_UNORM_ARB, KTX_ZERO, }, // BC7
{ KTX_ETC1_RGB8_OES, KTX_ZERO, KTX_ETC1_RGB8_OES, KTX_ZERO, }, // ETC1
{ KTX_COMPRESSED_RGB8_ETC2, KTX_ZERO, KTX_COMPRESSED_RGB8_ETC2, KTX_ZERO, }, // ETC2
{ KTX_COMPRESSED_RGBA8_ETC2_EAC, KTX_COMPRESSED_SRGB8_ETC2, KTX_COMPRESSED_RGBA8_ETC2_EAC, KTX_ZERO, }, // ETC2A
{ KTX_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2, KTX_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2, KTX_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2, KTX_ZERO, }, // ETC2A1
{ KTX_COMPRESSED_RGB_PVRTC_2BPPV1_IMG, KTX_COMPRESSED_SRGB_PVRTC_2BPPV1_EXT, KTX_COMPRESSED_RGB_PVRTC_2BPPV1_IMG, KTX_ZERO, }, // PTC12
{ KTX_COMPRESSED_RGB_PVRTC_4BPPV1_IMG, KTX_COMPRESSED_SRGB_PVRTC_4BPPV1_EXT, KTX_COMPRESSED_RGB_PVRTC_4BPPV1_IMG, KTX_ZERO, }, // PTC14
{ KTX_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG, KTX_COMPRESSED_SRGB_ALPHA_PVRTC_2BPPV1_EXT, KTX_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG, KTX_ZERO, }, // PTC12A
{ KTX_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG, KTX_COMPRESSED_SRGB_ALPHA_PVRTC_4BPPV1_EXT, KTX_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG, KTX_ZERO, }, // PTC14A
{ KTX_COMPRESSED_RGBA_PVRTC_2BPPV2_IMG, KTX_ZERO, KTX_COMPRESSED_RGBA_PVRTC_2BPPV2_IMG, KTX_ZERO, }, // PTC22
{ KTX_COMPRESSED_RGBA_PVRTC_4BPPV2_IMG, KTX_ZERO, KTX_COMPRESSED_RGBA_PVRTC_4BPPV2_IMG, KTX_ZERO, }, // PTC24
{ KTX_ZERO, KTX_ZERO, KTX_ZERO, KTX_ZERO, }, // Unknown
{ KTX_ZERO, KTX_ZERO, KTX_ZERO, KTX_ZERO, }, // R1
{ KTX_ALPHA, KTX_ZERO, KTX_ALPHA, KTX_UNSIGNED_BYTE, }, // A8
{ KTX_R8, KTX_ZERO, KTX_RED, KTX_UNSIGNED_BYTE, }, // R8
{ KTX_R8I, KTX_ZERO, KTX_RED, KTX_BYTE, }, // R8S
{ KTX_R8UI, KTX_ZERO, KTX_RED, KTX_UNSIGNED_BYTE, }, // R8S
{ KTX_R8_SNORM, KTX_ZERO, KTX_RED, KTX_BYTE, }, // R8S
{ KTX_R16, KTX_ZERO, KTX_RED, KTX_UNSIGNED_SHORT, }, // R16
{ KTX_R16I, KTX_ZERO, KTX_RED, KTX_SHORT, }, // R16I
{ KTX_R16UI, KTX_ZERO, KTX_RED, KTX_UNSIGNED_SHORT, }, // R16U
{ KTX_R16F, KTX_ZERO, KTX_RED, KTX_HALF_FLOAT, }, // R16F
{ KTX_R16_SNORM, KTX_ZERO, KTX_RED, KTX_SHORT, }, // R16S
{ KTX_R32I, KTX_ZERO, KTX_RED, KTX_INT, }, // R32I
{ KTX_R32UI, KTX_ZERO, KTX_RED, KTX_UNSIGNED_INT, }, // R32U
{ KTX_R32F, KTX_ZERO, KTX_RED, KTX_FLOAT, }, // R32F
{ KTX_RG8, KTX_ZERO, KTX_RG, KTX_UNSIGNED_BYTE, }, // RG8
{ KTX_RG8I, KTX_ZERO, KTX_RG, KTX_BYTE, }, // RG8I
{ KTX_RG8UI, KTX_ZERO, KTX_RG, KTX_UNSIGNED_BYTE, }, // RG8U
{ KTX_RG8_SNORM, KTX_ZERO, KTX_RG, KTX_BYTE, }, // RG8S
{ KTX_RG16, KTX_ZERO, KTX_RG, KTX_UNSIGNED_SHORT, }, // RG16
{ KTX_RG16I, KTX_ZERO, KTX_RG, KTX_SHORT, }, // RG16
{ KTX_RG16UI, KTX_ZERO, KTX_RG, KTX_UNSIGNED_SHORT, }, // RG16
{ KTX_RG16F, KTX_ZERO, KTX_RG, KTX_FLOAT, }, // RG16F
{ KTX_RG16_SNORM, KTX_ZERO, KTX_RG, KTX_SHORT, }, // RG16S
{ KTX_RG32I, KTX_ZERO, KTX_RG, KTX_INT, }, // RG32I
{ KTX_RG32UI, KTX_ZERO, KTX_RG, KTX_UNSIGNED_INT, }, // RG32U
{ KTX_RG32F, KTX_ZERO, KTX_RG, KTX_FLOAT, }, // RG32F
{ KTX_RGB9_E5, KTX_ZERO, KTX_RGB, KTX_UNSIGNED_INT_5_9_9_9_REV, }, // RGB9E5F
{ KTX_BGRA, KTX_SRGB8_ALPHA8, KTX_BGRA, KTX_UNSIGNED_BYTE, }, // BGRA8
{ KTX_RGBA8, KTX_SRGB8_ALPHA8, KTX_RGBA, KTX_UNSIGNED_BYTE, }, // RGBA8
{ KTX_RGBA8I, KTX_ZERO, KTX_RGBA, KTX_BYTE, }, // RGBA8I
{ KTX_RGBA8UI, KTX_ZERO, KTX_RGBA, KTX_UNSIGNED_BYTE, }, // RGBA8U
{ KTX_RGBA8_SNORM, KTX_ZERO, KTX_RGBA, KTX_BYTE, }, // RGBA8S
{ KTX_RGBA16, KTX_ZERO, KTX_RGBA, KTX_UNSIGNED_SHORT, }, // RGBA16
{ KTX_RGBA16I, KTX_ZERO, KTX_RGBA, KTX_SHORT, }, // RGBA16I
{ KTX_RGBA16UI, KTX_ZERO, KTX_RGBA, KTX_UNSIGNED_SHORT, }, // RGBA16U
{ KTX_RGBA16F, KTX_ZERO, KTX_RGBA, KTX_HALF_FLOAT, }, // RGBA16F
{ KTX_RGBA16_SNORM, KTX_ZERO, KTX_RGBA, KTX_SHORT, }, // RGBA16S
{ KTX_RGBA32I, KTX_ZERO, KTX_RGBA, KTX_INT, }, // RGBA32I
{ KTX_RGBA32UI, KTX_ZERO, KTX_RGBA, KTX_UNSIGNED_INT, }, // RGBA32U
{ KTX_RGBA32F, KTX_ZERO, KTX_RGBA, KTX_FLOAT, }, // RGBA32F
{ KTX_RGB565, KTX_ZERO, KTX_RGB, KTX_UNSIGNED_SHORT_5_6_5, }, // R5G6B5
{ KTX_RGBA4, KTX_ZERO, KTX_RGBA, KTX_UNSIGNED_SHORT_4_4_4_4, }, // RGBA4
{ KTX_RGB5_A1, KTX_ZERO, KTX_RGBA, KTX_UNSIGNED_SHORT_5_5_5_1, }, // RGB5A1
{ KTX_RGB10_A2, KTX_ZERO, KTX_RGBA, KTX_UNSIGNED_INT_2_10_10_10_REV, }, // RGB10A2
{ KTX_R11F_G11F_B10F, KTX_ZERO, KTX_RGB, KTX_UNSIGNED_INT_10F_11F_11F_REV, }, // R11G11B10F
};
BX_STATIC_ASSERT(TextureFormat::UnknownDepth == BX_COUNTOF(s_translateKtxFormat) );
bool imageParseKtx(ImageContainer& _imageContainer, bx::ReaderSeekerI* _reader)
{
uint8_t identifier[8];
bx::read(_reader, identifier);
if (identifier[1] != '1'
&& identifier[2] != '1')
{
return false;
}
uint32_t endianness;
bx::read(_reader, endianness);
bool fromLittleEndian = 0x04030201 == endianness;
uint32_t glType;
bx::readHE(_reader, glType, fromLittleEndian);
uint32_t glTypeSize;
bx::readHE(_reader, glTypeSize, fromLittleEndian);
uint32_t glFormat;
bx::readHE(_reader, glFormat, fromLittleEndian);
uint32_t glInternalFormat;
bx::readHE(_reader, glInternalFormat, fromLittleEndian);
uint32_t glBaseInternalFormat;
bx::readHE(_reader, glBaseInternalFormat, fromLittleEndian);
uint32_t width;
bx::readHE(_reader, width, fromLittleEndian);
uint32_t height;
bx::readHE(_reader, height, fromLittleEndian);
uint32_t depth;
bx::readHE(_reader, depth, fromLittleEndian);
uint32_t numberOfArrayElements;
bx::readHE(_reader, numberOfArrayElements, fromLittleEndian);
uint32_t numFaces;
bx::readHE(_reader, numFaces, fromLittleEndian);
uint32_t numMips;
bx::readHE(_reader, numMips, fromLittleEndian);
uint32_t metaDataSize;
bx::readHE(_reader, metaDataSize, fromLittleEndian);
// skip meta garbage...
int64_t offset = bx::skip(_reader, metaDataSize);
TextureFormat::Enum format = TextureFormat::Unknown;
bool hasAlpha = false;
for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateKtxFormat); ++ii)
{
if (s_translateKtxFormat[ii].m_internalFmt == glInternalFormat)
{
format = TextureFormat::Enum(ii);
break;
}
}
_imageContainer.m_data = NULL;
_imageContainer.m_size = 0;
_imageContainer.m_offset = (uint32_t)offset;
_imageContainer.m_width = width;
_imageContainer.m_height = height;
_imageContainer.m_depth = depth;
_imageContainer.m_format = format;
_imageContainer.m_numMips = uint8_t(numMips);
_imageContainer.m_hasAlpha = hasAlpha;
_imageContainer.m_cubeMap = numFaces > 1;
_imageContainer.m_ktx = true;
_imageContainer.m_ktxLE = fromLittleEndian;
_imageContainer.m_srgb = false;
return TextureFormat::Unknown != format;
}
// PVR3
#define PVR3_MAKE8CC(_a, _b, _c, _d, _e, _f, _g, _h) (uint64_t(BX_MAKEFOURCC(_a, _b, _c, _d) ) | (uint64_t(BX_MAKEFOURCC(_e, _f, _g, _h) )<<32) )
#define PVR3_MAGIC BX_MAKEFOURCC('P', 'V', 'R', 3)
#define PVR3_HEADER_SIZE 52
#define PVR3_PVRTC1_2BPP_RGB 0
#define PVR3_PVRTC1_2BPP_RGBA 1
#define PVR3_PVRTC1_4BPP_RGB 2
#define PVR3_PVRTC1_4BPP_RGBA 3
#define PVR3_PVRTC2_2BPP_RGBA 4
#define PVR3_PVRTC2_4BPP_RGBA 5
#define PVR3_ETC1 6
#define PVR3_DXT1 7
#define PVR3_DXT2 8
#define PVR3_DXT3 9
#define PVR3_DXT4 10
#define PVR3_DXT5 11
#define PVR3_BC4 12
#define PVR3_BC5 13
#define PVR3_R8 PVR3_MAKE8CC('r', 0, 0, 0, 8, 0, 0, 0)
#define PVR3_R16 PVR3_MAKE8CC('r', 0, 0, 0, 16, 0, 0, 0)
#define PVR3_R32 PVR3_MAKE8CC('r', 0, 0, 0, 32, 0, 0, 0)
#define PVR3_RG8 PVR3_MAKE8CC('r', 'g', 0, 0, 8, 8, 0, 0)
#define PVR3_RG16 PVR3_MAKE8CC('r', 'g', 0, 0, 16, 16, 0, 0)
#define PVR3_RG32 PVR3_MAKE8CC('r', 'g', 0, 0, 32, 32, 0, 0)
#define PVR3_BGRA8 PVR3_MAKE8CC('b', 'g', 'r', 'a', 8, 8, 8, 8)
#define PVR3_RGBA16 PVR3_MAKE8CC('r', 'g', 'b', 'a', 16, 16, 16, 16)
#define PVR3_RGBA32 PVR3_MAKE8CC('r', 'g', 'b', 'a', 32, 32, 32, 32)
#define PVR3_RGB565 PVR3_MAKE8CC('r', 'g', 'b', 0, 5, 6, 5, 0)
#define PVR3_RGBA4 PVR3_MAKE8CC('r', 'g', 'b', 'a', 4, 4, 4, 4)
#define PVR3_RGBA51 PVR3_MAKE8CC('r', 'g', 'b', 'a', 5, 5, 5, 1)
#define PVR3_RGB10A2 PVR3_MAKE8CC('r', 'g', 'b', 'a', 10, 10, 10, 2)
#define PVR3_CHANNEL_TYPE_ANY UINT32_MAX
#define PVR3_CHANNEL_TYPE_FLOAT UINT32_C(12)
static struct TranslatePvr3Format
{
uint64_t m_format;
uint32_t m_channelTypeMask;
TextureFormat::Enum m_textureFormat;
} s_translatePvr3Format[] =
{
{ PVR3_PVRTC1_2BPP_RGB, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC12 },
{ PVR3_PVRTC1_2BPP_RGBA, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC12A },
{ PVR3_PVRTC1_4BPP_RGB, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC14 },
{ PVR3_PVRTC1_4BPP_RGBA, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC14A },
{ PVR3_PVRTC2_2BPP_RGBA, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC22 },
{ PVR3_PVRTC2_4BPP_RGBA, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC24 },
{ PVR3_ETC1, PVR3_CHANNEL_TYPE_ANY, TextureFormat::ETC1 },
{ PVR3_DXT1, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC1 },
{ PVR3_DXT2, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC2 },
{ PVR3_DXT3, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC2 },
{ PVR3_DXT4, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC3 },
{ PVR3_DXT5, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC3 },
{ PVR3_BC4, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC4 },
{ PVR3_BC5, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC5 },
{ PVR3_R8, PVR3_CHANNEL_TYPE_ANY, TextureFormat::R8 },
{ PVR3_R16, PVR3_CHANNEL_TYPE_ANY, TextureFormat::R16U },
{ PVR3_R16, PVR3_CHANNEL_TYPE_FLOAT, TextureFormat::R16F },
{ PVR3_R32, PVR3_CHANNEL_TYPE_ANY, TextureFormat::R32U },
{ PVR3_R32, PVR3_CHANNEL_TYPE_FLOAT, TextureFormat::R32F },
{ PVR3_RG8, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RG8 },
{ PVR3_RG16, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RG16 },
{ PVR3_RG16, PVR3_CHANNEL_TYPE_FLOAT, TextureFormat::RG16F },
{ PVR3_RG32, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RG16 },
{ PVR3_RG32, PVR3_CHANNEL_TYPE_FLOAT, TextureFormat::RG32F },
{ PVR3_BGRA8, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BGRA8 },
{ PVR3_RGBA16, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RGBA16 },
{ PVR3_RGBA16, PVR3_CHANNEL_TYPE_FLOAT, TextureFormat::RGBA16F },
{ PVR3_RGBA32, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RGBA32U },
{ PVR3_RGBA32, PVR3_CHANNEL_TYPE_FLOAT, TextureFormat::RGBA32F },
{ PVR3_RGB565, PVR3_CHANNEL_TYPE_ANY, TextureFormat::R5G6B5 },
{ PVR3_RGBA4, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RGBA4 },
{ PVR3_RGBA51, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RGB5A1 },
{ PVR3_RGB10A2, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RGB10A2 },
};
bool imageParsePvr3(ImageContainer& _imageContainer, bx::ReaderSeekerI* _reader)
{
uint32_t flags;
bx::read(_reader, flags);
uint64_t pixelFormat;
bx::read(_reader, pixelFormat);
uint32_t colorSpace;
bx::read(_reader, colorSpace); // 0 - linearRGB, 1 - sRGB
uint32_t channelType;
bx::read(_reader, channelType);
uint32_t height;
bx::read(_reader, height);
uint32_t width;
bx::read(_reader, width);
uint32_t depth;
bx::read(_reader, depth);
uint32_t numSurfaces;
bx::read(_reader, numSurfaces);
uint32_t numFaces;
bx::read(_reader, numFaces);
uint32_t numMips;
bx::read(_reader, numMips);
uint32_t metaDataSize;
bx::read(_reader, metaDataSize);
// skip meta garbage...
int64_t offset = bx::skip(_reader, metaDataSize);
TextureFormat::Enum format = TextureFormat::Unknown;
bool hasAlpha = false;
for (uint32_t ii = 0; ii < BX_COUNTOF(s_translatePvr3Format); ++ii)
{
if (s_translatePvr3Format[ii].m_format == pixelFormat
&& channelType == (s_translatePvr3Format[ii].m_channelTypeMask & channelType) )
{
format = s_translatePvr3Format[ii].m_textureFormat;
break;
}
}
_imageContainer.m_data = NULL;
_imageContainer.m_size = 0;
_imageContainer.m_offset = (uint32_t)offset;
_imageContainer.m_width = width;
_imageContainer.m_height = height;
_imageContainer.m_depth = depth;
_imageContainer.m_format = format;
_imageContainer.m_numMips = uint8_t(numMips);
_imageContainer.m_hasAlpha = hasAlpha;
_imageContainer.m_cubeMap = numFaces > 1;
_imageContainer.m_ktx = false;
_imageContainer.m_ktxLE = false;
_imageContainer.m_srgb = colorSpace > 0;
return TextureFormat::Unknown != format;
}
bool imageParse(ImageContainer& _imageContainer, bx::ReaderSeekerI* _reader)
{
uint32_t magic;
bx::read(_reader, magic);
if (DDS_MAGIC == magic)
{
return imageParseDds(_imageContainer, _reader);
}
else if (KTX_MAGIC == magic)
{
return imageParseKtx(_imageContainer, _reader);
}
else if (PVR3_MAGIC == magic)
{
return imageParsePvr3(_imageContainer, _reader);
}
else if (BGFX_CHUNK_MAGIC_TEX == magic)
{
TextureCreate tc;
bx::read(_reader, tc);
_imageContainer.m_format = tc.m_format;
_imageContainer.m_offset = UINT32_MAX;
if (NULL == tc.m_mem)
{
_imageContainer.m_data = NULL;
_imageContainer.m_size = 0;
}
else
{
_imageContainer.m_data = tc.m_mem->data;
_imageContainer.m_size = tc.m_mem->size;
}
_imageContainer.m_width = tc.m_width;
_imageContainer.m_height = tc.m_height;
_imageContainer.m_depth = tc.m_depth;
_imageContainer.m_numMips = tc.m_numMips;
_imageContainer.m_hasAlpha = false;
_imageContainer.m_cubeMap = tc.m_cubeMap;
_imageContainer.m_ktx = false;
_imageContainer.m_ktxLE = false;
_imageContainer.m_srgb = false;
return true;
}
return false;
}
bool imageParse(ImageContainer& _imageContainer, const void* _data, uint32_t _size)
{
bx::MemoryReader reader(_data, _size);
return imageParse(_imageContainer, &reader);
}
void imageDecodeToBgra8(void* _dst, const void* _src, uint32_t _width, uint32_t _height, uint32_t _pitch, TextureFormat::Enum _format)
{
const uint8_t* src = (const uint8_t*)_src;
uint8_t* dst = (uint8_t*)_dst;
uint32_t width = _width/4;
uint32_t height = _height/4;
uint8_t temp[16*4];
switch (_format)
{
case TextureFormat::BC1:
for (uint32_t yy = 0; yy < height; ++yy)
{
for (uint32_t xx = 0; xx < width; ++xx)
{
decodeBlockDxt1(temp, src);
src += 8;
uint8_t* block = &dst[(yy*_pitch+xx*4)*4];
memcpy(&block[0*_pitch], &temp[ 0], 16);
memcpy(&block[1*_pitch], &temp[16], 16);
memcpy(&block[2*_pitch], &temp[32], 16);
memcpy(&block[3*_pitch], &temp[48], 16);
}
}
break;
case TextureFormat::BC2:
for (uint32_t yy = 0; yy < height; ++yy)
{
for (uint32_t xx = 0; xx < width; ++xx)
{
decodeBlockDxt23A(temp+3, src);
src += 8;
decodeBlockDxt(temp, src);
src += 8;
uint8_t* block = &dst[(yy*_pitch+xx*4)*4];
memcpy(&block[0*_pitch], &temp[ 0], 16);
memcpy(&block[1*_pitch], &temp[16], 16);
memcpy(&block[2*_pitch], &temp[32], 16);
memcpy(&block[3*_pitch], &temp[48], 16);
}
}
break;
case TextureFormat::BC3:
for (uint32_t yy = 0; yy < height; ++yy)
{
for (uint32_t xx = 0; xx < width; ++xx)
{
decodeBlockDxt45A(temp+3, src);
src += 8;
decodeBlockDxt(temp, src);
src += 8;
uint8_t* block = &dst[(yy*_pitch+xx*4)*4];
memcpy(&block[0*_pitch], &temp[ 0], 16);
memcpy(&block[1*_pitch], &temp[16], 16);
memcpy(&block[2*_pitch], &temp[32], 16);
memcpy(&block[3*_pitch], &temp[48], 16);
}
}
break;
case TextureFormat::BC4:
for (uint32_t yy = 0; yy < height; ++yy)
{
for (uint32_t xx = 0; xx < width; ++xx)
{
decodeBlockDxt45A(temp, src);
src += 8;
uint8_t* block = &dst[(yy*_pitch+xx*4)*4];
memcpy(&block[0*_pitch], &temp[ 0], 16);
memcpy(&block[1*_pitch], &temp[16], 16);
memcpy(&block[2*_pitch], &temp[32], 16);
memcpy(&block[3*_pitch], &temp[48], 16);
}
}
break;
case TextureFormat::BC5:
for (uint32_t yy = 0; yy < height; ++yy)
{
for (uint32_t xx = 0; xx < width; ++xx)
{
decodeBlockDxt45A(temp+2, src);
src += 8;
decodeBlockDxt45A(temp+1, src);
src += 8;
for (uint32_t ii = 0; ii < 16; ++ii)
{
float nx = temp[ii*4+2]*2.0f/255.0f - 1.0f;
float ny = temp[ii*4+1]*2.0f/255.0f - 1.0f;
float nz = bx::fsqrt(1.0f - nx*nx - ny*ny);
temp[ii*4+0] = uint8_t( (nz + 1.0f)*255.0f/2.0f);
temp[ii*4+3] = 0;
}
uint8_t* block = &dst[(yy*_pitch+xx*4)*4];
memcpy(&block[0*_pitch], &temp[ 0], 16);
memcpy(&block[1*_pitch], &temp[16], 16);
memcpy(&block[2*_pitch], &temp[32], 16);
memcpy(&block[3*_pitch], &temp[48], 16);
}
}
break;
case TextureFormat::ETC1:
case TextureFormat::ETC2:
for (uint32_t yy = 0; yy < height; ++yy)
{
for (uint32_t xx = 0; xx < width; ++xx)
{
decodeBlockEtc12(temp, src);
src += 8;
uint8_t* block = &dst[(yy*_pitch+xx*4)*4];
memcpy(&block[0*_pitch], &temp[ 0], 16);
memcpy(&block[1*_pitch], &temp[16], 16);
memcpy(&block[2*_pitch], &temp[32], 16);
memcpy(&block[3*_pitch], &temp[48], 16);
}
}
break;
case TextureFormat::ETC2A:
BX_WARN(false, "ETC2A decoder is not implemented.");
imageCheckerboard(_width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00), _dst);
break;
case TextureFormat::ETC2A1:
BX_WARN(false, "ETC2A1 decoder is not implemented.");
imageCheckerboard(_width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xffff0000), _dst);
break;
case TextureFormat::PTC12:
BX_WARN(false, "PTC12 decoder is not implemented.");
imageCheckerboard(_width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xffff00ff), _dst);
break;
case TextureFormat::PTC12A:
BX_WARN(false, "PTC12A decoder is not implemented.");
imageCheckerboard(_width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xffffff00), _dst);
break;
case TextureFormat::PTC14:
for (uint32_t yy = 0; yy < height; ++yy)
{
for (uint32_t xx = 0; xx < width; ++xx)
{
decodeBlockPtc14(temp, src, xx, yy, width, height);
uint8_t* block = &dst[(yy*_pitch+xx*4)*4];
memcpy(&block[0*_pitch], &temp[ 0], 16);
memcpy(&block[1*_pitch], &temp[16], 16);
memcpy(&block[2*_pitch], &temp[32], 16);
memcpy(&block[3*_pitch], &temp[48], 16);
}
}
break;
case TextureFormat::PTC14A:
for (uint32_t yy = 0; yy < height; ++yy)
{
for (uint32_t xx = 0; xx < width; ++xx)
{
decodeBlockPtc14A(temp, src, xx, yy, width, height);
uint8_t* block = &dst[(yy*_pitch+xx*4)*4];
memcpy(&block[0*_pitch], &temp[ 0], 16);
memcpy(&block[1*_pitch], &temp[16], 16);
memcpy(&block[2*_pitch], &temp[32], 16);
memcpy(&block[3*_pitch], &temp[48], 16);
}
}
break;
case TextureFormat::PTC22:
BX_WARN(false, "PTC22 decoder is not implemented.");
imageCheckerboard(_width, _height, 16, UINT32_C(0xff00ff00), UINT32_C(0xff0000ff), _dst);
break;
case TextureFormat::PTC24:
BX_WARN(false, "PTC24 decoder is not implemented.");
imageCheckerboard(_width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xffffffff), _dst);
break;
case TextureFormat::RGBA8:
imageSwizzleBgra8(_width, _height, _pitch, _src, _dst);
break;
case TextureFormat::BGRA8:
memcpy(_dst, _src, _pitch*_height);
break;
default:
// Decompression not implemented... Make ugly red-yellow checkerboard texture.
imageCheckerboard(_width, _height, 16, UINT32_C(0xffff0000), UINT32_C(0xffffff00), _dst);
break;
}
}
void imageDecodeToRgba8(void* _dst, const void* _src, uint32_t _width, uint32_t _height, uint32_t _pitch, TextureFormat::Enum _format)
{
switch (_format)
{
case TextureFormat::RGBA8:
memcpy(_dst, _src, _pitch*_height);
break;
case TextureFormat::BGRA8:
imageSwizzleBgra8(_width, _height, _pitch, _src, _dst);
break;
default:
imageDecodeToBgra8(_dst, _src, _width, _height, _pitch, _format);
imageSwizzleBgra8(_width, _height, _pitch, _dst, _dst);
break;
}
}
void imageRgba8ToRgba32fRef(void* _dst, uint32_t _width, uint32_t _height, uint32_t _pitch, const void* _src)
{
const uint32_t dstwidth = _width;
const uint32_t dstheight = _height;
if (0 == dstwidth
|| 0 == dstheight)
{
return;
}
float* dst = (float*)_dst;
const uint8_t* src = (const uint8_t*)_src;
for (uint32_t yy = 0, ystep = _pitch; yy < dstheight; ++yy, src += ystep)
{
const uint8_t* rgba = src;
for (uint32_t xx = 0; xx < dstwidth; ++xx, rgba += 4, dst += 4)
{
dst[0] = bx::fpow(rgba[0], 2.2f);
dst[1] = bx::fpow(rgba[1], 2.2f);
dst[2] = bx::fpow(rgba[2], 2.2f);
dst[3] = rgba[3];
}
}
}
void imageRgba8ToRgba32f(void* _dst, uint32_t _width, uint32_t _height, uint32_t _pitch, const void* _src)
{
const uint32_t dstwidth = _width;
const uint32_t dstheight = _height;
if (0 == dstwidth
|| 0 == dstheight)
{
return;
}
float* dst = (float*)_dst;
const uint8_t* src = (const uint8_t*)_src;
using namespace bx;
const float4_t unpack = float4_ld(1.0f, 1.0f/256.0f, 1.0f/65536.0f, 1.0f/16777216.0f);
const float4_t umask = float4_ild(0xff, 0xff00, 0xff0000, 0xff000000);
const float4_t wflip = float4_ild(0, 0, 0, 0x80000000);
const float4_t wadd = float4_ld(0.0f, 0.0f, 0.0f, 32768.0f*65536.0f);
for (uint32_t yy = 0, ystep = _pitch; yy < dstheight; ++yy, src += ystep)
{
const uint8_t* rgba = src;
for (uint32_t xx = 0; xx < dstwidth; ++xx, rgba += 4, dst += 4)
{
const float4_t abgr0 = float4_splat(rgba);
const float4_t abgr0m = float4_and(abgr0, umask);
const float4_t abgr0x = float4_xor(abgr0m, wflip);
const float4_t abgr0f = float4_itof(abgr0x);
const float4_t abgr0c = float4_add(abgr0f, wadd);
const float4_t abgr0n = float4_mul(abgr0c, unpack);
float4_st(dst, abgr0n);
}
}
}
void imageDecodeToRgba32f(bx::AllocatorI* _allocator, void* _dst, const void* _src, uint32_t _width, uint32_t _height, uint32_t _pitch, TextureFormat::Enum _format)
{
const uint8_t* src = (const uint8_t*)_src;
uint8_t* dst = (uint8_t*)_dst;
switch (_format)
{
case TextureFormat::BC5:
{
uint32_t width = _width/4;
uint32_t height = _height/4;
for (uint32_t yy = 0; yy < height; ++yy)
{
for (uint32_t xx = 0; xx < width; ++xx)
{
uint8_t temp[16*4];
decodeBlockDxt45A(temp+2, src);
src += 8;
decodeBlockDxt45A(temp+1, src);
src += 8;
for (uint32_t ii = 0; ii < 16; ++ii)
{
float nx = temp[ii*4+2]*2.0f/255.0f - 1.0f;
float ny = temp[ii*4+1]*2.0f/255.0f - 1.0f;
float nz = bx::fsqrt(1.0f - nx*nx - ny*ny);
const uint32_t offset = (yy*4 + ii/4)*_width*16 + (xx*4 + ii%4)*16;
float* block = (float*)&dst[offset];
block[0] = nx;
block[1] = ny;
block[2] = nz;
block[3] = 0.0f;
}
}
}
}
break;
case TextureFormat::RGBA32F:
memcpy(_dst, _src, _pitch*_height);
break;
case TextureFormat::RGBA8:
imageRgba8ToRgba32f(_dst, _width, _height, _pitch, _src);
break;
default:
{
void* temp = BX_ALLOC(_allocator, imageGetSize(_format, uint16_t(_pitch/4), uint16_t(_height) ) );
imageDecodeToRgba8(temp, _src, _width, _height, _pitch, _format);
imageRgba8ToRgba32f(_dst, _width, _height, _pitch, temp);
BX_FREE(_allocator, temp);
}
break;
}
}
bool imageGetRawData(const ImageContainer& _imageContainer, uint8_t _side, uint8_t _lod, const void* _data, uint32_t _size, ImageMip& _mip)
{
uint32_t offset = _imageContainer.m_offset;
TextureFormat::Enum format = TextureFormat::Enum(_imageContainer.m_format);
bool hasAlpha = _imageContainer.m_hasAlpha;
const ImageBlockInfo& blockInfo = s_imageBlockInfo[format];
const uint8_t bpp = blockInfo.bitsPerPixel;
const uint32_t blockSize = blockInfo.blockSize;
const uint32_t blockWidth = blockInfo.blockWidth;
const uint32_t blockHeight = blockInfo.blockHeight;
const uint32_t minBlockX = blockInfo.minBlockX;
const uint32_t minBlockY = blockInfo.minBlockY;
if (UINT32_MAX == _imageContainer.m_offset)
{
if (NULL == _imageContainer.m_data)
{
return false;
}
offset = 0;
_data = _imageContainer.m_data;
_size = _imageContainer.m_size;
}
const uint8_t* data = (const uint8_t*)_data;
if (_imageContainer.m_ktx)
{
uint32_t width = _imageContainer.m_width;
uint32_t height = _imageContainer.m_height;
uint32_t depth = _imageContainer.m_depth;
for (uint8_t lod = 0, num = _imageContainer.m_numMips; lod < num; ++lod)
{
uint32_t imageSize = bx::toHostEndian(*(const uint32_t*)&data[offset], _imageContainer.m_ktxLE);
offset += sizeof(uint32_t);
width = bx::uint32_max(blockWidth * minBlockX, ( (width + blockWidth - 1) / blockWidth )*blockWidth);
height = bx::uint32_max(blockHeight * minBlockY, ( (height + blockHeight - 1) / blockHeight)*blockHeight);
depth = bx::uint32_max(1, depth);
uint32_t size = width*height*depth*bpp/8;
BX_CHECK(size == imageSize, "KTX: Image size mismatch %d (expected %d).", size, imageSize);
for (uint8_t side = 0, numSides = _imageContainer.m_cubeMap ? 6 : 1; side < numSides; ++side)
{
if (side == _side
&& lod == _lod)
{
_mip.m_width = width;
_mip.m_height = height;
_mip.m_blockSize = blockSize;
_mip.m_size = size;
_mip.m_data = &data[offset];
_mip.m_bpp = bpp;
_mip.m_format = format;
_mip.m_hasAlpha = hasAlpha;
return true;
}
offset += imageSize;
BX_CHECK(offset <= _size, "Reading past size of data buffer! (offset %d, size %d)", offset, _size);
BX_UNUSED(_size);
}
width >>= 1;
height >>= 1;
depth >>= 1;
}
}
else
{
for (uint8_t side = 0, numSides = _imageContainer.m_cubeMap ? 6 : 1; side < numSides; ++side)
{
uint32_t width = _imageContainer.m_width;
uint32_t height = _imageContainer.m_height;
uint32_t depth = _imageContainer.m_depth;
for (uint8_t lod = 0, num = _imageContainer.m_numMips; lod < num; ++lod)
{
width = bx::uint32_max(blockWidth * minBlockX, ( (width + blockWidth - 1) / blockWidth )*blockWidth);
height = bx::uint32_max(blockHeight * minBlockY, ( (height + blockHeight - 1) / blockHeight)*blockHeight);
depth = bx::uint32_max(1, depth);
uint32_t size = width*height*depth*bpp/8;
if (side == _side
&& lod == _lod)
{
_mip.m_width = width;
_mip.m_height = height;
_mip.m_blockSize = blockSize;
_mip.m_size = size;
_mip.m_data = &data[offset];
_mip.m_bpp = bpp;
_mip.m_format = format;
_mip.m_hasAlpha = hasAlpha;
return true;
}
offset += size;
BX_CHECK(offset <= _size, "Reading past size of data buffer! (offset %d, size %d)", offset, _size);
BX_UNUSED(_size);
width >>= 1;
height >>= 1;
depth >>= 1;
}
}
}
return false;
}
void imageWriteTga(bx::WriterI* _writer, uint32_t _width, uint32_t _height, uint32_t _pitch, const void* _src, bool _grayscale, bool _yflip)
{
uint8_t type = _grayscale ? 3 : 2;
uint8_t bpp = _grayscale ? 8 : 32;
uint8_t header[18] = {};
header[ 2] = type;
header[12] = _width &0xff;
header[13] = (_width >>8)&0xff;
header[14] = _height &0xff;
header[15] = (_height>>8)&0xff;
header[16] = bpp;
header[17] = 32;
bx::write(_writer, header, sizeof(header) );
uint32_t dstPitch = _width*bpp/8;
if (_yflip)
{
uint8_t* data = (uint8_t*)_src + _pitch*_height - _pitch;
for (uint32_t yy = 0; yy < _height; ++yy)
{
bx::write(_writer, data, dstPitch);
data -= _pitch;
}
}
else if (_pitch == dstPitch)
{
bx::write(_writer, _src, _height*_pitch);
}
else
{
uint8_t* data = (uint8_t*)_src;
for (uint32_t yy = 0; yy < _height; ++yy)
{
bx::write(_writer, data, dstPitch);
data += _pitch;
}
}
}
static int32_t imageWriteKtxHeader(bx::WriterI* _writer, TextureFormat::Enum _format, bool _cubeMap, uint32_t _width, uint32_t _height, uint32_t _depth, uint8_t _numMips)
{
const KtxFormatInfo& tfi = s_translateKtxFormat[_format];
int32_t size = 0;
size += bx::write(_writer, "\xabKTX 11\xbb\r\n\x1a\n", 12);
size += bx::write(_writer, UINT32_C(0x04030201) );
size += bx::write(_writer, UINT32_C(0) ); // glType
size += bx::write(_writer, UINT32_C(1) ); // glTypeSize
size += bx::write(_writer, UINT32_C(0) ); // glFormat
size += bx::write(_writer, tfi.m_internalFmt); // glInternalFormat
size += bx::write(_writer, tfi.m_fmt); // glBaseInternalFormat
size += bx::write(_writer, _width);
size += bx::write(_writer, _height);
size += bx::write(_writer, _depth);
size += bx::write(_writer, UINT32_C(0) ); // numberOfArrayElements
size += bx::write(_writer, _cubeMap ? UINT32_C(6) : UINT32_C(0) );
size += bx::write(_writer, uint32_t(_numMips) );
size += bx::write(_writer, UINT32_C(0) ); // Meta-data size.
BX_CHECK(size == 64, "KTX: Failed to write header size %d (expected: %d).", size, 64);
return size;
}
void imageWriteKtx(bx::WriterI* _writer, TextureFormat::Enum _format, bool _cubeMap, uint32_t _width, uint32_t _height, uint32_t _depth, uint8_t _numMips, const void* _src)
{
imageWriteKtxHeader(_writer, _format, _cubeMap, _width, _height, _depth, _numMips);
const ImageBlockInfo& blockInfo = s_imageBlockInfo[_format];
const uint8_t bpp = blockInfo.bitsPerPixel;
const uint32_t blockWidth = blockInfo.blockWidth;
const uint32_t blockHeight = blockInfo.blockHeight;
const uint32_t minBlockX = blockInfo.minBlockX;
const uint32_t minBlockY = blockInfo.minBlockY;
const uint8_t* src = (const uint8_t*)_src;
uint32_t width = _width;
uint32_t height = _height;
uint32_t depth = _depth;
for (uint8_t lod = 0, num = _numMips; lod < num; ++lod)
{
width = bx::uint32_max(blockWidth * minBlockX, ( (width + blockWidth - 1) / blockWidth )*blockWidth);
height = bx::uint32_max(blockHeight * minBlockY, ( (height + blockHeight - 1) / blockHeight)*blockHeight);
depth = bx::uint32_max(1, depth);
uint32_t size = width*height*depth*bpp/8;
bx::write(_writer, size);
for (uint8_t side = 0, numSides = _cubeMap ? 6 : 1; side < numSides; ++side)
{
bx::write(_writer, src, size);
src += size;
}
width >>= 1;
height >>= 1;
depth >>= 1;
}
}
void imageWriteKtx(bx::WriterI* _writer, ImageContainer& _imageContainer, const void* _data, uint32_t _size)
{
imageWriteKtxHeader(_writer
, TextureFormat::Enum(_imageContainer.m_format)
, _imageContainer.m_cubeMap
, _imageContainer.m_width
, _imageContainer.m_height
, _imageContainer.m_depth
, _imageContainer.m_numMips
);
for (uint8_t lod = 0, num = _imageContainer.m_numMips; lod < num; ++lod)
{
ImageMip mip;
imageGetRawData(_imageContainer, 0, lod, _data, _size, mip);
bx::write(_writer, mip.m_size);
for (uint8_t side = 0, numSides = _imageContainer.m_cubeMap ? 6 : 1; side < numSides; ++side)
{
if (imageGetRawData(_imageContainer, side, lod, _data, _size, mip) )
{
bx::write(_writer, mip.m_data, mip.m_size);
}
}
}
}
} // namespace bgfx
|