Skip to content

Commit 15ab4da

Browse files
committed
Document the new functions.
1 parent 662e42d commit 15ab4da

File tree

1 file changed

+159
-33
lines changed

1 file changed

+159
-33
lines changed

interface/js_binding/ktx_wrapper.cpp

Lines changed: 159 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,10 @@ namespace ktx
548548

549549
/** @page libktx_js libktx Binding
550550
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.
552555
553556
@code{.idl}
554557
interface ktxOrientation {
@@ -563,7 +566,7 @@ interface ktxUploadResult {
563566
readonly attribute GLenum error;
564567
};
565568
566-
interface ktxTextureCreateInfo {
569+
interface ktxTextureCreateInfo { // **
567570
attribute long vkFormat;
568571
attribute long baseWidth;
569572
attribute long baseHeight;
@@ -576,7 +579,7 @@ interface ktxTextureCreateInfo {
576579
attribute boolean generateMipmaps;
577580
};
578581
579-
interface ktxAstcParams { // ktx only.
582+
interface ktxAstcParams { // **
580583
void ktxAstcParams();
581584
582585
attribute boolean verbose;
@@ -588,7 +591,7 @@ interface ktxAstcParams { // ktx only.
588591
attribute DOMString inputSwizzle;
589592
};
590593
591-
interface ktxBasisParams {
594+
interface ktxBasisParams { // **
592595
void ktxBasisParams();
593596
594597
attribute boolean uastc,
@@ -624,23 +627,24 @@ interface ktxBasisParams {
624627
625628
interface ktxTexture {
626629
void constructor(ArrayBufferView fileData);
627-
void constructor(ktxTextureCreateInfo createInfo, // ktx only
630+
void constructor(ktxTextureCreateInfo createInfo, // **
628631
CreateStorageEnum? storage);
629632
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(); // **
635638
ArrayBufferView getImage(long level, long layer, long faceSlice);
636639
UploadResult glUpload();
637640
ErrorCode? setImageFromMemory(long level, long layer, long faceSlice,
638-
ArrayBufferView imageData); // ktx only.
641+
ArrayBufferView imageData); // **
639642
ErrorCode? transcodeBasis(TranscodeTarget? target, TranscodeFlagBits
640643
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); // **
644648
DOMString findKeyValue(DOMString key);
645649
646650
readonly attribute long baseWidth;
@@ -653,8 +657,8 @@ interface ktxTexture {
653657
readonly attribute SupercmpScheme supercompressionScheme;
654658
readonly attribute ktxOrientation orientation;
655659
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.
658662
};
659663
660664
enum ErrorCode = {
@@ -729,7 +733,7 @@ enum SupercmpScheme {
729733
};
730734
731735
enum dfPrimaries = {
732-
// These are the values needed with HTML5/WebGL.
736+
// These are the values needed for KTX with HTML5/WebGL.
733737
"UNSPECIFIED",
734738
"BT709",
735739
"SRGB"
@@ -750,17 +754,16 @@ enum VkFormat = {
750754
// Full list omitted as its length will distract from the documentation
751755
// purpose of this IDL. Any VkFormat valid for KTX can be used. As shown
752756
// here, omit the VK_FORMAT_ prefix and enclose in quotes.
753-
}
754757
755-
enum AstcQualityLevel = {
758+
enum AstcQualityLevel = { // **
756759
"FASTEST",
757760
"FAST",
758761
"MEDIUM",
759762
"THOROUGH",
760763
"EXHAUSTIVE",
761764
};
762765
763-
enum AstcBlockDimension = {
766+
enum AstcBlockDimension = { // **
764767
// 2D formats
765768
"d4x4",
766769
"d5x4",
@@ -789,13 +792,13 @@ enum AstcBlockDimension = {
789792
"d6x6x6"
790793
};
791794
792-
enum AstcMode = {
795+
enum AstcMode = { // **
793796
"DEFAULT",
794797
"LDR",
795798
"HDR"
796799
};
797800
798-
enum UastcFlags = {
801+
enum UastcFlags = { // **
799802
"LEVEL_FASTEST",
800803
"LEVEL_FASTER",
801804
"LEVEL_DEFAULT",
@@ -804,7 +807,7 @@ enum UastcFlags = {
804807
};
805808
@endcode
806809
807-
## How to use
810+
# How to use
808811
809812
Put libktx.js and libktx.wasm in a directory on your server. Create a script
810813
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.
817820
@note For the read-only version of the library, use libktx_read.js and
818821
libktx_read.wasm instead.
819822
820-
### Create an instance of the ktx module
823+
## Create an instance of the ktx module
821824
822825
To avoid polluting the global @c window name space all methods, variables and
823826
tokens related to libktx are wrapped in a function that returns a promise.
824827
The promise is resolved with a module instance when it is safe to run the
825828
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.
830833
831834
The function is called @e createKtxModule. In previous releases it was called
832835
@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.
861864
}
862865
@endcode
863866
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.
866869
867-
### Download a KTX file and create a texture
870+
# Downloading and using an existing KTX texture.
868871
869872
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:
871874
872875
@code{.js}
873876
var myTexture;
@@ -894,7 +897,8 @@ function like @c loadTexture the following:
894897
gl.bindTexture(tex.target, tex.object);
895898
gl.deleteTexture(texture.object);
896899
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;
898902
ktexture.delete();
899903
};
900904
@@ -1015,6 +1019,128 @@ be necessary to expose the ktxTexture_IterateLevelFaces or
10151019
ktxTexture_IterateLoadLevelFaces API to JS with those calling a
10161020
callback in JS to upload each image to WebGL.
10171021
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+
10181144
*/
10191145

10201146
EMSCRIPTEN_BINDINGS(ktx)

0 commit comments

Comments
 (0)