-
Notifications
You must be signed in to change notification settings - Fork 284
/
Copy pathbasis_wrappers.cpp
2666 lines (2236 loc) · 102 KB
/
basis_wrappers.cpp
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
// basis_wrappers.cpp - Wrappers to the C++ compressor and transcoder for WebAssembly/WebGL use.
// This file exposes wrappers for transcoding and encoding .basis and .KTX2 files.
//
// **Important**:
// Compile with -fno-strict-aliasing (same as the Linux kernel).
// This code HAS NOT been tested with strict aliasing enabled.
// The "initializeBasis()" function MUST be called at least once before using either the compressor or transcoder.
//
// There are four main categories of wrappers in this module:
// 1. Transcoding, low-level .basis file information: See class basis_file. Only depends on transcoder/basisu_transcoder.cpp.
// getFileDesc(), getImageDesc(), getImageLevelDesc(): These functions return low-level information about where compressed data is located for each image in a .basis file.
// This is useful for when you want to extract the compressed data and embed it into your own file formats, for container independent transcoding.
//
// 2. Encoding (optional): See class basis_encoder. Encodes LDR .PNG or 32bpp images, or HDR half-float/float or .EXR/.HDR images to .basis/.ktx2 files in memory.
// Must compile with BASISU_SUPPORT_ENCODING=1.
// Requires basisu_transcoder.cpp as well as all the .cpp files in the "encoder" directory. Results in a larger WebAssembly executable.
//
// 3. Low level transcoding/container independent transcoding: See class lowlevel_etc1s_image_transcoder or function transcodeUASTCImage().
// For transcoding raw compressed ETC1S/UASTC LDR/UASTC HDR texture data from non-.basis files (say from KTX2) to GPU texture data.
//
// 4. Helpers, transcoder texture format information: See functions getBytesPerBlockOrPixel(), formatHasAlpha(), etc.
// If BASISU_SUPPORT_ENCODING is 1, wrappers for the compressor will be included. Otherwise, only the wrappers for the transcoder will be compiled in.
#ifndef BASISU_SUPPORT_ENCODING
#define BASISU_SUPPORT_ENCODING 0
#endif
// Enable debug printf()'s in this module.
#ifndef BASISU_DEBUG_PRINTF
#define BASISU_DEBUG_PRINTF 0
#endif
#define BASISU_ENCODER_MAX_SOURCE_IMAGE_PIXELS (6291456)
#include "basisu_transcoder.h"
#include <algorithm>
#if BASISU_SUPPORT_ENCODING
#include "../../encoder/basisu_comp.h"
#include "../../encoder/basisu_astc_hdr_6x6_enc.h"
#include "../../encoder/basisu_resampler_filters.h"
#endif
#include <emscripten/bind.h>
#if BASISU_SUPPORT_ENCODING
#include <emscripten/threading.h>
#endif
#include <emscripten.h>
using namespace emscripten;
using namespace basist;
using namespace basisu;
static bool g_basis_initialized_flag;
// Global one-time initialization. MUST be called before using any other functionality.
void basis_init()
{
static std::mutex s_init_mutex;
std::lock_guard<std::mutex> lock(s_init_mutex);
#if BASISU_DEBUG_PRINTF
printf("basis_init()\n");
#endif
if (g_basis_initialized_flag)
return;
#if BASISU_SUPPORT_ENCODING
basisu_encoder_init();
#endif
basisu_transcoder_init();
g_basis_initialized_flag = true;
}
static void copy_from_jsbuffer(const emscripten::val& srcBuffer, basisu::vector<uint8_t>& dstVec)
{
unsigned int length = srcBuffer["length"].as<unsigned int>();
dstVec.resize(length);
emscripten::val memory = emscripten::val::module_property("HEAP8")["buffer"];
emscripten::val memoryView = srcBuffer["constructor"].new_(memory, reinterpret_cast<uintptr_t>(dstVec.data()), length);
// Copy the bytes from the Javascript buffer.
memoryView.call<void>("set", srcBuffer);
}
static bool copy_to_jsbuffer(const emscripten::val& dstBuffer, const basisu::vector<uint8_t>& srcVec)
{
if (srcVec.empty())
{
#if BASISU_DEBUG_PRINTF
printf("copy_to_jsbuffer: Provided source buffer is empty\n");
#endif
return false;
}
// Make sure the provided buffer from Javascript is big enough. If not, bail.
int dstBufferLen = dstBuffer["byteLength"].as<int>();
if (srcVec.size() > dstBufferLen)
{
#if BASISU_DEBUG_PRINTF
printf("copy_to_jsbuffer: Provided destination buffer is too small (wanted %u bytes, got %u bytes)!\n", (uint32_t)srcVec.size(), dstBufferLen);
#endif
assert(0);
return false;
}
emscripten::val memory = emscripten::val::module_property("HEAP8")["buffer"];
emscripten::val memoryView = emscripten::val::global("Uint8Array").new_(memory, reinterpret_cast<uintptr_t>(srcVec.data()), srcVec.size());
// Copy the bytes into the Javascript buffer.
dstBuffer.call<void>("set", memoryView);
return true;
}
const uint32_t BASIS_MAGIC = 0xD4ADBEA1;
const uint32_t KTX2_MAGIC = 0xD4ADBEF2;
struct basis_file_desc
{
uint32_t m_version;
uint32_t m_us_per_frame;
uint32_t m_total_images;
uint32_t m_userdata0;
uint32_t m_userdata1;
// Type of texture (cETC1S, cUASTC4x4, cUASTC_HDR_4x4, etc.)
uint32_t m_tex_format; // basis_tex_format
bool m_y_flipped;
bool m_has_alpha_slices;
// ETC1S endpoint codebook
uint32_t m_num_endpoints;
uint32_t m_endpoint_palette_ofs;
uint32_t m_endpoint_palette_len;
// ETC1S selector codebook
uint32_t m_num_selectors;
uint32_t m_selector_palette_ofs;
uint32_t m_selector_palette_len;
// Huffman codelength tables
uint32_t m_tables_ofs;
uint32_t m_tables_len;
uint32_t m_block_width;
uint32_t m_block_height;
};
struct basis_image_desc
{
uint32_t m_orig_width;
uint32_t m_orig_height;
uint32_t m_num_blocks_x;
uint32_t m_num_blocks_y;
uint32_t m_num_levels;
uint32_t m_block_width;
uint32_t m_block_height;
// Will be true if the image has alpha (for UASTC this may vary per-image)
bool m_alpha_flag;
bool m_iframe_flag;
};
struct basis_image_level_desc
{
// File offset/length of the compressed ETC1S or UASTC LDR/HDR texture data.
uint32_t m_rgb_file_ofs;
uint32_t m_rgb_file_len;
// Optional alpha data file offset/length - will be 0's for UASTC LDR/HDR or opaque ETC1S files.
uint32_t m_alpha_file_ofs;
uint32_t m_alpha_file_len;
};
struct basis_file
{
int m_magic = 0;
basisu_transcoder m_transcoder;
basisu::vector<uint8_t> m_file;
basis_file(const emscripten::val& jsBuffer)
: m_file([&]() {
size_t byteLength = jsBuffer["byteLength"].as<size_t>();
return basisu::vector<uint8_t>(byteLength);
}())
{
if (!g_basis_initialized_flag)
{
#if BASISU_DEBUG_PRINTF
printf("basis_file::basis_file: Must call basis_init() first!\n");
#endif
assert(0);
return;
}
unsigned int length = jsBuffer["length"].as<unsigned int>();
emscripten::val memory = emscripten::val::module_property("HEAP8")["buffer"];
emscripten::val memoryView = jsBuffer["constructor"].new_(memory, reinterpret_cast<uintptr_t>(m_file.data()), length);
memoryView.call<void>("set", jsBuffer);
if (!m_transcoder.validate_header(m_file.data(), m_file.size()))
{
#if BASISU_DEBUG_PRINTF
printf("basis_file::basis_file: m_transcoder.validate_header() failed!\n");
#endif
m_file.clear();
}
// Initialized after validation
m_magic = BASIS_MAGIC;
}
void close()
{
assert(m_magic == BASIS_MAGIC);
if (m_magic != BASIS_MAGIC)
return;
m_file.clear();
}
uint32_t getHasAlpha()
{
assert(m_magic == BASIS_MAGIC);
if (m_magic != BASIS_MAGIC)
return 0;
basisu_image_level_info li;
if (!m_transcoder.get_image_level_info(m_file.data(), m_file.size(), li, 0, 0))
return 0;
return li.m_alpha_flag;
}
uint32_t getNumImages()
{
assert(m_magic == BASIS_MAGIC);
if (m_magic != BASIS_MAGIC)
return 0;
return m_transcoder.get_total_images(m_file.data(), m_file.size());
}
uint32_t getNumLevels(uint32_t image_index)
{
assert(m_magic == BASIS_MAGIC);
if (m_magic != BASIS_MAGIC)
return 0;
basisu_image_info ii;
if (!m_transcoder.get_image_info(m_file.data(), m_file.size(), ii, image_index))
return 0;
return ii.m_total_levels;
}
uint32_t getImageWidth(uint32_t image_index, uint32_t level_index)
{
assert(m_magic == BASIS_MAGIC);
if (m_magic != BASIS_MAGIC)
return 0;
uint32_t orig_width, orig_height, total_blocks;
if (!m_transcoder.get_image_level_desc(m_file.data(), m_file.size(), image_index, level_index, orig_width, orig_height, total_blocks))
return 0;
return orig_width;
}
uint32_t getImageHeight(uint32_t image_index, uint32_t level_index)
{
assert(m_magic == BASIS_MAGIC);
if (m_magic != BASIS_MAGIC)
return 0;
uint32_t orig_width, orig_height, total_blocks;
if (!m_transcoder.get_image_level_desc(m_file.data(), m_file.size(), image_index, level_index, orig_width, orig_height, total_blocks))
return 0;
return orig_height;
}
// Returns a basis_tex_format (cETC1S, cUASTC, cUASTC_HDR_4x4, etc.)
uint32_t getBasisTexFormat()
{
assert(m_magic == BASIS_MAGIC);
if (m_magic != BASIS_MAGIC)
return false;
basis_tex_format fmt = m_transcoder.get_basis_tex_format(m_file.data(), m_file.size());
return (uint32_t)fmt;
}
// Currently 4 or 6
uint32_t getBlockWidth() const
{
assert(m_magic == BASIS_MAGIC);
if (m_magic != BASIS_MAGIC)
return false;
basis_tex_format fmt = m_transcoder.get_basis_tex_format(m_file.data(), m_file.size());
return basis_tex_format_get_block_width(fmt);
}
// Currently 4 or 6
uint32_t getBlockHeight()
{
assert(m_magic == BASIS_MAGIC);
if (m_magic != BASIS_MAGIC)
return false;
basis_tex_format fmt = m_transcoder.get_basis_tex_format(m_file.data(), m_file.size());
return basis_tex_format_get_block_height(fmt);
}
basis_file_desc getFileDesc()
{
basis_file_desc result;
memset(&result, 0, sizeof(result));
assert(m_magic == BASIS_MAGIC);
if (m_magic != BASIS_MAGIC)
return result;
basisu_file_info file_info;
if (!m_transcoder.get_file_info(m_file.data(), m_file.size(), file_info))
{
assert(0);
return result;
}
result.m_version = file_info.m_version;
result.m_us_per_frame = file_info.m_us_per_frame;
result.m_total_images = file_info.m_total_images;
result.m_userdata0 = file_info.m_userdata0;
result.m_userdata1 = file_info.m_userdata1;
result.m_tex_format = static_cast<uint32_t>(file_info.m_tex_format);
result.m_y_flipped = file_info.m_y_flipped;
result.m_has_alpha_slices = file_info.m_has_alpha_slices;
result.m_num_endpoints = file_info.m_total_endpoints;
result.m_endpoint_palette_ofs = file_info.m_endpoint_codebook_ofs;
result.m_endpoint_palette_len = file_info.m_endpoint_codebook_size;
result.m_num_selectors = file_info.m_total_selectors;
result.m_selector_palette_ofs = file_info.m_selector_codebook_ofs;
result.m_selector_palette_len = file_info.m_selector_codebook_size;
result.m_tables_ofs = file_info.m_tables_ofs;
result.m_tables_len = file_info.m_tables_size;
result.m_block_width = file_info.m_block_width;
result.m_block_height = file_info.m_block_height;
return result;
}
basis_image_desc getImageDesc(uint32_t image_index)
{
basis_image_desc result;
memset(&result, 0, sizeof(result));
assert(m_magic == BASIS_MAGIC);
if (m_magic != BASIS_MAGIC)
return result;
basisu_image_info image_info;
// bool get_image_info(const void *pData, uint32_t data_size, basisu_image_info &image_info, uint32_t image_index) const;
if (!m_transcoder.get_image_info(m_file.data(), m_file.size(), image_info, image_index))
{
assert(0);
return result;
}
result.m_orig_width = image_info.m_orig_width;
result.m_orig_height = image_info.m_orig_height;
result.m_num_blocks_x = image_info.m_num_blocks_x;
result.m_num_blocks_y = image_info.m_num_blocks_y;
result.m_num_levels = image_info.m_total_levels;
result.m_alpha_flag = image_info.m_alpha_flag;
result.m_iframe_flag = image_info.m_iframe_flag;
result.m_block_width = image_info.m_block_width;
result.m_block_height = image_info.m_block_height;
return result;
}
basis_image_level_desc getImageLevelDesc(uint32_t image_index, uint32_t level_index)
{
basis_image_level_desc result;
memset(&result, 0, sizeof(result));
assert(m_magic == BASIS_MAGIC);
if (m_magic != BASIS_MAGIC)
return result;
basisu_image_level_info image_info;
if (!m_transcoder.get_image_level_info(m_file.data(), m_file.size(), image_info, image_index, level_index))
{
assert(0);
return result;
}
result.m_rgb_file_ofs = image_info.m_rgb_file_ofs;
result.m_rgb_file_len = image_info.m_rgb_file_len;
result.m_alpha_file_ofs = image_info.m_alpha_file_ofs;
result.m_alpha_file_len = image_info.m_alpha_file_len;
return result;
}
uint32_t getImageTranscodedSizeInBytes(uint32_t image_index, uint32_t level_index, uint32_t format)
{
assert(m_magic == BASIS_MAGIC);
if (m_magic != BASIS_MAGIC)
return 0;
if (format >= (int)transcoder_texture_format::cTFTotalTextureFormats)
{
assert(0);
return 0;
}
const transcoder_texture_format tex_format = static_cast<transcoder_texture_format>(format);
uint32_t orig_width, orig_height, total_src_blocks;
if (!m_transcoder.get_image_level_desc(m_file.data(), m_file.size(), image_index, level_index, orig_width, orig_height, total_src_blocks))
{
assert(0);
return 0;
}
return basis_compute_transcoded_image_size_in_bytes(tex_format, orig_width, orig_height);
}
// Only true for UASTC LDR 4x4.
bool isUASTC_LDR_4x4()
{
assert(m_magic == BASIS_MAGIC);
if (m_magic != BASIS_MAGIC)
return false;
return m_transcoder.get_basis_tex_format(m_file.data(), m_file.size()) == basis_tex_format::cUASTC4x4;
}
bool isETC1S()
{
assert(m_magic == BASIS_MAGIC);
if (m_magic != BASIS_MAGIC)
return false;
return m_transcoder.get_basis_tex_format(m_file.data(), m_file.size()) == basis_tex_format::cETC1S;
}
// True for any LDR texture types
bool isLDR()
{
assert(m_magic == BASIS_MAGIC);
if (m_magic != BASIS_MAGIC)
return false;
basis_tex_format fmt = m_transcoder.get_basis_tex_format(m_file.data(), m_file.size());
return (fmt == basis_tex_format::cETC1S) || (fmt == basis_tex_format::cUASTC4x4);
}
// True if the texture is UASTC HDR 4x4 or ASTC HDR 6x6.
// In this case, it can only be transcoded to BC6H, ASTC HDR (of the same block dimensions, currently 4x4 or 6x6), RGB9E5 or half-float RGB/RGBA images.
bool isHDR()
{
assert(m_magic == BASIS_MAGIC);
if (m_magic != BASIS_MAGIC)
return false;
basis_tex_format fmt = m_transcoder.get_basis_tex_format(m_file.data(), m_file.size());
return basis_tex_format_is_hdr(fmt);
}
bool isHDR4x4()
{
assert(m_magic == BASIS_MAGIC);
if (m_magic != BASIS_MAGIC)
return false;
basis_tex_format fmt = m_transcoder.get_basis_tex_format(m_file.data(), m_file.size());
return (fmt == basis_tex_format::cUASTC_HDR_4x4);
}
bool isHDR6x6()
{
assert(m_magic == BASIS_MAGIC);
if (m_magic != BASIS_MAGIC)
return false;
basis_tex_format fmt = m_transcoder.get_basis_tex_format(m_file.data(), m_file.size());
return (fmt == basis_tex_format::cASTC_HDR_6x6) || (fmt == basis_tex_format::cASTC_HDR_6x6_INTERMEDIATE);
}
uint32_t startTranscoding()
{
assert(m_magic == BASIS_MAGIC);
if (m_magic != BASIS_MAGIC)
return 0;
return m_transcoder.start_transcoding(m_file.data(), m_file.size());
}
// Here for backwards compat, prefer transcodeImageWithFlags().
uint32_t transcodeImage(const emscripten::val& dst, uint32_t image_index, uint32_t level_index, uint32_t format, uint32_t unused, uint32_t get_alpha_for_opaque_formats)
{
(void)unused;
assert(m_magic == BASIS_MAGIC);
if (m_magic != BASIS_MAGIC)
return 0;
if (format >= (int)transcoder_texture_format::cTFTotalTextureFormats)
return 0;
const transcoder_texture_format transcoder_format = static_cast<transcoder_texture_format>(format);
uint32_t orig_width, orig_height, total_src_blocks;
if (!m_transcoder.get_image_level_desc(m_file.data(), m_file.size(), image_index, level_index, orig_width, orig_height, total_src_blocks))
return 0;
basisu::vector<uint8_t> dst_data;
uint32_t flags = get_alpha_for_opaque_formats ? cDecodeFlagsTranscodeAlphaDataToOpaqueFormats : 0;
const uint32_t transcoded_size_in_bytes = getImageTranscodedSizeInBytes(image_index, level_index, format);
dst_data.resize(transcoded_size_in_bytes);
uint32_t status;
if (basis_transcoder_format_is_uncompressed(transcoder_format))
{
status = m_transcoder.transcode_image_level(
m_file.data(), m_file.size(), image_index, level_index,
dst_data.data(), orig_width * orig_height,
transcoder_format,
flags,
orig_width,
nullptr,
orig_height);
}
else
{
const uint32_t bytes_per_block = basis_get_bytes_per_block_or_pixel(transcoder_format);
status = m_transcoder.transcode_image_level(
m_file.data(), m_file.size(), image_index, level_index,
dst_data.data(), dst_data.size() / bytes_per_block,
static_cast<basist::transcoder_texture_format>(format),
flags);
}
emscripten::val memory = emscripten::val::module_property("HEAP8")["buffer"];
emscripten::val memoryView = emscripten::val::global("Uint8Array").new_(memory, reinterpret_cast<uintptr_t>(dst_data.data()), dst_data.size());
dst.call<void>("set", memoryView);
return status;
}
// Like transcodeImage(), but with fixed parameters.
// For flags, see cDecodeFlagsPVRTCDecodeToNextPow2 etc.
uint32_t transcodeImageWithFlags(const emscripten::val& dst, uint32_t image_index, uint32_t level_index, uint32_t format, uint32_t flags)
{
assert(m_magic == BASIS_MAGIC);
if (m_magic != BASIS_MAGIC)
return 0;
if (format >= (int)transcoder_texture_format::cTFTotalTextureFormats)
return 0;
const transcoder_texture_format transcoder_format = static_cast<transcoder_texture_format>(format);
uint32_t orig_width, orig_height, total_src_blocks;
if (!m_transcoder.get_image_level_desc(m_file.data(), m_file.size(), image_index, level_index, orig_width, orig_height, total_src_blocks))
return 0;
basisu::vector<uint8_t> dst_data;
const uint32_t transcoded_size_in_bytes = getImageTranscodedSizeInBytes(image_index, level_index, format);
dst_data.resize(transcoded_size_in_bytes);
uint32_t status;
if (basis_transcoder_format_is_uncompressed(transcoder_format))
{
status = m_transcoder.transcode_image_level(
m_file.data(), m_file.size(), image_index, level_index,
dst_data.data(), orig_width * orig_height,
transcoder_format,
flags,
orig_width,
nullptr,
orig_height);
}
else
{
const uint32_t bytes_per_block = basis_get_bytes_per_block_or_pixel(transcoder_format);
status = m_transcoder.transcode_image_level(
m_file.data(), m_file.size(), image_index, level_index,
dst_data.data(), dst_data.size() / bytes_per_block,
static_cast<basist::transcoder_texture_format>(format),
flags);
}
emscripten::val memory = emscripten::val::module_property("HEAP8")["buffer"];
emscripten::val memoryView = emscripten::val::global("Uint8Array").new_(memory, reinterpret_cast<uintptr_t>(dst_data.data()), dst_data.size());
dst.call<void>("set", memoryView);
return status;
}
};
#if BASISD_SUPPORT_KTX2
struct ktx2_header_js
{
uint32_t m_vk_format;
uint32_t m_type_size;
uint32_t m_pixel_width;
uint32_t m_pixel_height;
uint32_t m_pixel_depth;
uint32_t m_layer_count;
uint32_t m_face_count;
uint32_t m_level_count;
uint32_t m_supercompression_scheme;
uint32_t m_dfd_byte_offset;
uint32_t m_dfd_byte_length;
uint32_t m_kvd_byte_offset;
uint32_t m_kvd_byte_length;
uint32_t m_sgd_byte_offset;
uint32_t m_sgd_byte_length;
};
struct ktx2_file
{
int m_magic = 0;
basist::ktx2_transcoder m_transcoder;
basisu::vector<uint8_t> m_file;
bool m_is_valid = false;
ktx2_file(const emscripten::val& jsBuffer)
: m_file([&]() {
size_t byteLength = jsBuffer["byteLength"].as<size_t>();
return basisu::vector<uint8_t>(byteLength);
}())
{
if (!g_basis_initialized_flag)
{
#if BASISU_DEBUG_PRINTF
printf("basis_file::basis_file: Must call basis_init() first!\n");
#endif
assert(0);
return;
}
unsigned int length = jsBuffer["length"].as<unsigned int>();
emscripten::val memory = emscripten::val::module_property("HEAP8")["buffer"];
emscripten::val memoryView = jsBuffer["constructor"].new_(memory, reinterpret_cast<uintptr_t>(m_file.data()), length);
memoryView.call<void>("set", jsBuffer);
if (!m_transcoder.init(m_file.data(), m_file.size()))
{
#if BASISU_DEBUG_PRINTF
printf("m_transcoder.init() failed!\n");
#endif
assert(0);
m_file.clear();
}
m_is_valid = true;
// Initialized after validation
m_magic = KTX2_MAGIC;
}
bool isValid()
{
assert(m_magic == KTX2_MAGIC);
if (m_magic != KTX2_MAGIC)
return false;
return m_is_valid;
}
void close()
{
assert(m_magic == KTX2_MAGIC);
if (m_magic != KTX2_MAGIC)
return;
m_file.clear();
m_transcoder.clear();
}
uint32_t getDFDSize()
{
return m_transcoder.get_dfd().size();
}
uint32_t getDFD(const emscripten::val& dst)
{
assert(m_magic == KTX2_MAGIC);
if (m_magic != KTX2_MAGIC)
return 0;
const uint8_vec &dst_data = m_transcoder.get_dfd();
if (dst_data.size())
return copy_to_jsbuffer(dst, dst_data);
return 1;
}
ktx2_header_js getHeader()
{
ktx2_header_js hdr;
memset(&hdr, 0, sizeof(hdr));
assert(m_magic == KTX2_MAGIC);
if (m_magic != KTX2_MAGIC)
return hdr;
const basist::ktx2_header& h = m_transcoder.get_header();
hdr.m_vk_format = h.m_vk_format;
hdr.m_type_size = h.m_type_size;
hdr.m_pixel_width = h.m_pixel_width;
hdr.m_pixel_height = h.m_pixel_height;
hdr.m_pixel_depth = h.m_pixel_depth;
hdr.m_layer_count = h.m_layer_count;
hdr.m_face_count = h.m_face_count;
hdr.m_level_count = h.m_level_count;
hdr.m_supercompression_scheme = h.m_supercompression_scheme;
hdr.m_dfd_byte_offset = h.m_dfd_byte_offset;
hdr.m_dfd_byte_length = h.m_dfd_byte_length;
hdr.m_kvd_byte_offset = h.m_kvd_byte_offset;
hdr.m_kvd_byte_length = h.m_kvd_byte_length;
// emscripten doesn't support binding uint64_t for some reason
assert(h.m_sgd_byte_offset <= UINT32_MAX);
assert(h.m_sgd_byte_length <= UINT32_MAX);
hdr.m_sgd_byte_offset = (uint32_t)h.m_sgd_byte_offset;
hdr.m_sgd_byte_length = (uint32_t)h.m_sgd_byte_length;
return hdr;
}
bool hasKey(std::string key_name)
{
assert(m_magic == KTX2_MAGIC);
if (m_magic != KTX2_MAGIC)
return false;
return m_transcoder.find_key(key_name) != nullptr;
}
uint32_t getTotalKeys()
{
assert(m_magic == KTX2_MAGIC);
if (m_magic != KTX2_MAGIC)
return 0;
return m_transcoder.get_key_values().size();
}
std::string getKey(uint32_t index)
{
assert(m_magic == KTX2_MAGIC);
if (m_magic != KTX2_MAGIC)
return std::string("");
return std::string((const char*)m_transcoder.get_key_values()[index].m_key.data());
}
uint32_t getKeyValueSize(std::string key_name)
{
assert(m_magic == KTX2_MAGIC);
if (m_magic != KTX2_MAGIC)
return 0;
const uint8_vec* p = m_transcoder.find_key(key_name);
return p ? p->size() : 0;
}
uint32_t getKeyValue(std::string key_name, const emscripten::val& dst)
{
assert(m_magic == KTX2_MAGIC);
if (m_magic != KTX2_MAGIC)
return 0;
const uint8_vec* p = m_transcoder.find_key(key_name);
if (!p)
return 0;
if (p->size())
return copy_to_jsbuffer(dst, *p);
return 1;
}
uint32_t getWidth()
{
assert(m_magic == KTX2_MAGIC);
if (m_magic != KTX2_MAGIC)
return 0;
return m_transcoder.get_width();
}
uint32_t getHeight()
{
assert(m_magic == KTX2_MAGIC);
if (m_magic != KTX2_MAGIC)
return 0;
return m_transcoder.get_height();
}
uint32_t getBlockWidth()
{
assert(m_magic == KTX2_MAGIC);
if (m_magic != KTX2_MAGIC)
return 0;
return m_transcoder.get_block_width();
}
uint32_t getBlockHeight()
{
assert(m_magic == KTX2_MAGIC);
if (m_magic != KTX2_MAGIC)
return 0;
return m_transcoder.get_block_height();
}
uint32_t getFaces()
{
assert(m_magic == KTX2_MAGIC);
if (m_magic != KTX2_MAGIC)
return 0;
return m_transcoder.get_faces();
}
uint32_t getLayers()
{
assert(m_magic == KTX2_MAGIC);
if (m_magic != KTX2_MAGIC)
return 0;
return m_transcoder.get_layers();
}
uint32_t getLevels()
{
assert(m_magic == KTX2_MAGIC);
if (m_magic != KTX2_MAGIC)
return 0;
return m_transcoder.get_levels();
}
// Returns a basis_tex_format: cETC1S, cUASTC4x4, or cUASTC_HDR_4x4, etc.
uint32_t getBasisTexFormat()
{
assert(m_magic == KTX2_MAGIC);
if (m_magic != KTX2_MAGIC)
return 0;
return (uint32_t)m_transcoder.get_basis_tex_format();
}
// Returns true if the texture is UASTC LDR 4x4 (and not HDR - see isHDR()).
bool isUASTC_LDR_4x4()
{
assert(m_magic == KTX2_MAGIC);
if (m_magic != KTX2_MAGIC)
return false;
return m_transcoder.is_uastc();
}
// Returns true if the texture is UASTC LDR 4x4 (and not HDR - see isHDR()).
bool isUASTC()
{
assert(m_magic == KTX2_MAGIC);
if (m_magic != KTX2_MAGIC)
return false;
return m_transcoder.is_uastc();
}
bool isLDR()
{
assert(m_magic == KTX2_MAGIC);
if (m_magic != KTX2_MAGIC)
return false;
return !m_transcoder.is_hdr();
}
bool isETC1S()
{
assert(m_magic == KTX2_MAGIC);
if (m_magic != KTX2_MAGIC)
return false;
return m_transcoder.is_etc1s();
}
// Returns true if the texture is UASTC HDR or ASTC HDR. In this case, it can only be transcoded to BC6H, ASTC HDR (of the same block dimensions), RGB9E5 or half-float RGB/RGBA images.
bool isHDR()
{
assert(m_magic == KTX2_MAGIC);
if (m_magic != KTX2_MAGIC)
return false;
return m_transcoder.is_hdr();
}
bool isHDR4x4()
{
assert(m_magic == KTX2_MAGIC);
if (m_magic != KTX2_MAGIC)
return false;
return m_transcoder.is_hdr_4x4();
}
bool isHDR6x6()
{
assert(m_magic == KTX2_MAGIC);
if (m_magic != KTX2_MAGIC)
return false;
return m_transcoder.is_hdr_6x6();
}
bool getHasAlpha()
{
assert(m_magic == KTX2_MAGIC);
if (m_magic != KTX2_MAGIC)
return false;
return m_transcoder.get_has_alpha();
}
uint32_t getDFDColorModel()
{
assert(m_magic == KTX2_MAGIC);
if (m_magic != KTX2_MAGIC)
return 0;
return m_transcoder.get_dfd_color_model();
}
uint32_t getDFDColorPrimaries()
{
assert(m_magic == KTX2_MAGIC);
if (m_magic != KTX2_MAGIC)
return 0;
return m_transcoder.get_dfd_color_primaries();
}
uint32_t getDFDTransferFunc()
{
assert(m_magic == KTX2_MAGIC);
if (m_magic != KTX2_MAGIC)
return 0;
return m_transcoder.get_dfd_transfer_func();
}
uint32_t getDFDFlags()
{
assert(m_magic == KTX2_MAGIC);
if (m_magic != KTX2_MAGIC)
return 0;
return m_transcoder.get_dfd_flags();
}
uint32_t getDFDTotalSamples()
{
assert(m_magic == KTX2_MAGIC);
if (m_magic != KTX2_MAGIC)
return 0;
return m_transcoder.get_dfd_total_samples();
}
uint32_t getDFDChannelID0()
{
assert(m_magic == KTX2_MAGIC);
if (m_magic != KTX2_MAGIC)
return 0;
return m_transcoder.get_dfd_channel_id0();
}