@@ -548,7 +548,10 @@ namespace ktx
548
548
549
549
/* * @page libktx_js libktx Binding
550
550
551
- ## WebIDL for the binding
551
+ # WebIDL for the binding
552
+
553
+ Items marked with ** are only available in the full @e libktx.js wrapper. Unmarked
554
+ items are available in both @e libktx.js and @e libktx_read.js.
552
555
553
556
@code{.idl}
554
557
interface ktxOrientation {
@@ -563,7 +566,7 @@ interface ktxUploadResult {
563
566
readonly attribute GLenum error;
564
567
};
565
568
566
- interface ktxTextureCreateInfo {
569
+ interface ktxTextureCreateInfo { // **
567
570
attribute long vkFormat;
568
571
attribute long baseWidth;
569
572
attribute long baseHeight;
@@ -576,7 +579,7 @@ interface ktxTextureCreateInfo {
576
579
attribute boolean generateMipmaps;
577
580
};
578
581
579
- interface ktxAstcParams { // ktx only.
582
+ interface ktxAstcParams { // **
580
583
void ktxAstcParams();
581
584
582
585
attribute boolean verbose;
@@ -588,7 +591,7 @@ interface ktxAstcParams { // ktx only.
588
591
attribute DOMString inputSwizzle;
589
592
};
590
593
591
- interface ktxBasisParams {
594
+ interface ktxBasisParams { // **
592
595
void ktxBasisParams();
593
596
594
597
attribute boolean uastc,
@@ -624,23 +627,24 @@ interface ktxBasisParams {
624
627
625
628
interface ktxTexture {
626
629
void constructor(ArrayBufferView fileData);
627
- void constructor(ktxTextureCreateInfo createInfo, // ktx only
630
+ void constructor(ktxTextureCreateInfo createInfo, // **
628
631
CreateStorageEnum? storage);
629
632
630
- ErrorCode? compressAstc(ktxAstcParams params); // ktx only.
631
- ErrorCode? compressBasis(ktxBasisParams params); // ktx only .
632
- ktxTexture createCopy();
633
- ErrorCode defateZLIB(); // ktx only.
634
- ErrorCode deflateZstd(); // ktx only.
633
+ ErrorCode? compressAstc(ktxAstcParams params); // **
634
+ ErrorCode? compressBasis(ktxBasisParams params); // **
635
+ ktxTexture createCopy(); // **
636
+ ErrorCode defateZLIB(); // **
637
+ ErrorCode deflateZstd(); // **
635
638
ArrayBufferView getImage(long level, long layer, long faceSlice);
636
639
UploadResult glUpload();
637
640
ErrorCode? setImageFromMemory(long level, long layer, long faceSlice,
638
- ArrayBufferView imageData); // ktx only.
641
+ ArrayBufferView imageData); // **
639
642
ErrorCode? transcodeBasis(TranscodeTarget? target, TranscodeFlagBits
640
643
decodeFlags);
641
- ArrayBufferView writeToMemory(); // ktx only.
642
- ErrorCode? addKVPair(DOMString key, DOMString value);
643
- deleteKVPair(DOMString key);
644
+ ArrayBufferView writeToMemory(); // **
645
+ ErrorCode? addKVPairString(DOMString key, DOMString value); // **
646
+ ErrorCode? addKVPairByte(DOMString key, ArrayBuffewView value); // **
647
+ deleteKVPair(DOMString key); // **
644
648
DOMString findKeyValue(DOMString key);
645
649
646
650
readonly attribute long baseWidth;
@@ -653,8 +657,8 @@ interface ktxTexture {
653
657
readonly attribute SupercmpScheme supercompressionScheme;
654
658
readonly attribute ktxOrientation orientation;
655
659
656
- attribute dfTransfer OETF;
657
- attribute dfPrimaries primaries;
660
+ attribute dfTransfer OETF; // Setting available only in libktx.js.
661
+ attribute dfPrimaries primaries; // Setting available only in libktx.js.
658
662
};
659
663
660
664
enum ErrorCode = {
@@ -729,7 +733,7 @@ enum SupercmpScheme {
729
733
};
730
734
731
735
enum dfPrimaries = {
732
- // These are the values needed with HTML5/WebGL.
736
+ // These are the values needed for KTX with HTML5/WebGL.
733
737
"UNSPECIFIED",
734
738
"BT709",
735
739
"SRGB"
@@ -750,17 +754,16 @@ enum VkFormat = {
750
754
// Full list omitted as its length will distract from the documentation
751
755
// purpose of this IDL. Any VkFormat valid for KTX can be used. As shown
752
756
// here, omit the VK_FORMAT_ prefix and enclose in quotes.
753
- }
754
757
755
- enum AstcQualityLevel = {
758
+ enum AstcQualityLevel = { // **
756
759
"FASTEST",
757
760
"FAST",
758
761
"MEDIUM",
759
762
"THOROUGH",
760
763
"EXHAUSTIVE",
761
764
};
762
765
763
- enum AstcBlockDimension = {
766
+ enum AstcBlockDimension = { // **
764
767
// 2D formats
765
768
"d4x4",
766
769
"d5x4",
@@ -789,13 +792,13 @@ enum AstcBlockDimension = {
789
792
"d6x6x6"
790
793
};
791
794
792
- enum AstcMode = {
795
+ enum AstcMode = { // **
793
796
"DEFAULT",
794
797
"LDR",
795
798
"HDR"
796
799
};
797
800
798
- enum UastcFlags = {
801
+ enum UastcFlags = { // **
799
802
"LEVEL_FASTEST",
800
803
"LEVEL_FASTER",
801
804
"LEVEL_DEFAULT",
@@ -804,7 +807,7 @@ enum UastcFlags = {
804
807
};
805
808
@endcode
806
809
807
- ## How to use
810
+ # How to use
808
811
809
812
Put libktx.js and libktx.wasm in a directory on your server. Create a script
810
813
tag with libktx.js as the @c src in your .html as shown below, changing the
@@ -817,16 +820,16 @@ script source. libktx.js will automatically load libktx.wasm.
817
820
@note For the read-only version of the library, use libktx_read.js and
818
821
libktx_read.wasm instead.
819
822
820
- ### Create an instance of the ktx module
823
+ ## Create an instance of the ktx module
821
824
822
825
To avoid polluting the global @c window name space all methods, variables and
823
826
tokens related to libktx are wrapped in a function that returns a promise.
824
827
The promise is resolved with a module instance when it is safe to run the
825
828
compiled code. To use any of the features your code must call the function,
826
- wait for the promise resolution and use the returned instance. Before doing so,
827
- your code must create your WebGL context. The context is needed during module
828
- initialization so that the @c glUpload function can provide WebGLTexture object
829
- handles on the same context.
829
+ wait for the promise resolution and use the returned instance. Before calling
830
+ the function your code must create your WebGL context. The context is needed
831
+ during module initialization so that the @c glUpload function can provide
832
+ WebGLTexture object handles on the same context.
830
833
831
834
The function is called @e createKtxModule. In previous releases it was called
832
835
@e LIBKTX. It has been renamed to clarify what it is actually doing. Old scripts
@@ -861,13 +864,13 @@ This snippet shows WebGL context creation as well.
861
864
}
862
865
@endcode
863
866
864
- @e Call @c main() after the module instance has been created. Start the rest of your
865
- code there.
867
+ This calls @c main() after the module instance has been created. Start the rest
868
+ of your code there.
866
869
867
- ### Download a KTX file and create a texture
870
+ # Downloading and using an existing KTX texture.
868
871
869
872
To download an existing texture and create a WebGL texture from it, execute a
870
- function like @c loadTexture the following:
873
+ function like @c loadTexture in the following:
871
874
872
875
@code{.js}
873
876
var myTexture;
@@ -894,7 +897,8 @@ function like @c loadTexture the following:
894
897
gl.bindTexture(tex.target, tex.object);
895
898
gl.deleteTexture(texture.object);
896
899
texture = tex;
897
- elem('format').innerText = tex.format;
900
+ // Use code like this to display the transcode target format.
901
+ // elem('format').innerText = tex.format;
898
902
ktexture.delete();
899
903
};
900
904
@@ -1015,6 +1019,128 @@ be necessary to expose the ktxTexture_IterateLevelFaces or
1015
1019
ktxTexture_IterateLoadLevelFaces API to JS with those calling a
1016
1020
callback in JS to upload each image to WebGL.
1017
1021
1022
+ # Creating a new KTX texture
1023
+
1024
+ This function shows the main steps:
1025
+
1026
+ @code{.js}
1027
+ async function runTests(filename) {
1028
+ const img = await loadImage(filename);
1029
+ const imageData = await loadImageData(img);
1030
+ const ktexture = await createTexture(imageData);
1031
+ }
1032
+ @endcode
1033
+
1034
+ Step 1 is to fetch the image via code such as this:
1035
+
1036
+ @code{.js}
1037
+ async function loadImage(src){
1038
+ return new Promise((resolve, reject) => {
1039
+ let img = new Image();
1040
+ div = items[origImageItem].element;
1041
+ img.onload = () => { div.appendChild(img); resolve(img); }
1042
+ img.onerror = reject;
1043
+ img.src = src;
1044
+ })
1045
+ }
1046
+ }
1047
+ @endcode
1048
+
1049
+ Step 2 is to get the image data via code such as the following. Note
1050
+ that to get data at the original image size you must use @c img.naturalWidth
1051
+ and @c img.naturalHeight as shown here. If you use @c img.width and
1052
+ @c img.height the image data will be rendered at whatever size your CSS
1053
+ is displaying the canvas.
1054
+
1055
+ @code{.js}
1056
+ async function loadImageData (img, flip = false) {
1057
+ const canvas = document.createElement("canvas");
1058
+ const context = canvas.getContext("2d");
1059
+ const width = img.naturalWidth;
1060
+ const height = img.naturalHeight;
1061
+ canvas.width = width;
1062
+ canvas.height = height;
1063
+
1064
+ if (flip) {
1065
+ context.translate(0, height);
1066
+ context.scale(1, -1);
1067
+ }
1068
+ context.drawImage(img, 0, 0, width, height);
1069
+
1070
+ const imageData = context.getImageData(0, 0, width, height);
1071
+ return imageData;
1072
+ };
1073
+ @endcode
1074
+
1075
+ Step 3 is to create the KTX texture object as shonw here:
1076
+
1077
+ @code{.js}
1078
+ async function createTexture(imageData) {
1079
+ const createInfo = new ktx.ktxTextureCreateInfo();
1080
+ const colorSpace = imageData.colorSpace;
1081
+
1082
+ createInfo.baseWidth = imageData.width;
1083
+ createInfo.baseHeight = imageData.height;
1084
+ createInfo.baseDepth = 1;
1085
+ createInfo.numDimensions = 2;
1086
+ createInfo.numLevels = 1;
1087
+ createInfo.numLayers = 1;
1088
+ createInfo.numFaces = 1;
1089
+ createInfo.isArray = false;
1090
+ createInfo.generateMipmaps = false;
1091
+
1092
+ var displayP3;
1093
+ // Image data from 2d canvases is always 8-bit RGBA.
1094
+ // The only possible ImageData colorSpace choices are undefined, "srgb"
1095
+ // and "displayp3." All use the sRGB transfer function.
1096
+ createInfo.vkFormat = VkFormat.R8G8B8A8_SRGB;
1097
+ if ( imageData.colorSpace == "display-p3") {
1098
+ displayP3 = true;
1099
+ }
1100
+
1101
+ const ktexture = new ktx.ktxTexture(createInfo, CreateStorageEnum.ALLOC_STORAGE);
1102
+ if (ktexture != null) {
1103
+ if (displayP3) {
1104
+ ktexture.primaries = ktx.dfPrimaries.DISPLAYP3;
1105
+ }
1106
+ result = ktexture.setImageFromMemory(0, 0, 0, imageData.data);
1107
+ }
1108
+ return ktexture;
1109
+ }
1110
+ @endcode
1111
+
1112
+ The texture can now be uploaded to WebGL with `uploadTextureToGl`, that was
1113
+ listed earlier, and then displayed.
1114
+
1115
+ The texture can be compressed to one of the Basis universal formats with code
1116
+ like the following.
1117
+
1118
+ @code{.js}
1119
+ async function testEncodeBasis(ktexture) {
1120
+ const { ktxBasisParams, ErrorCode } = ktx;
1121
+ const basisu_options = new ktxBasisParams();
1122
+
1123
+ basisu_options.uastc = false;
1124
+ basisu_options.noSSE = true;
1125
+ basisu_options.verbose = false;
1126
+ basisu_options.qualityLevel = 200;
1127
+ basisu_options.compressionLevel = ktx.Etc1SDefaultCompressionLevel;
1128
+
1129
+ var result = ktexture.compressBasis(basisu_options);
1130
+ // Check for ErrorCode == SUCCESS.
1131
+ }
1132
+ @endcode
1133
+
1134
+ Finally the texture can be written back to Javascript with this single line
1135
+ of code:
1136
+
1137
+ @code{.js}
1138
+ const serializedTexture = ktexture.writeToMemory();
1139
+ @endcode
1140
+
1141
+ @c serializedTexture is a TypedArray. The web client can write the data to
1142
+ a local file or upload it to a server.
1143
+
1018
1144
*/
1019
1145
1020
1146
EMSCRIPTEN_BINDINGS (ktx)
0 commit comments