KhronosGroup / KTX-Software

KTX (Khronos Texture) Library and Tools
Other
854 stars 226 forks source link

Add JS bindings for full libktx #874

Closed aqnuep closed 2 months ago

aqnuep commented 5 months ago

This PR has been repurposed from adding the Phasmatic bindings for partial write functionality into adding bindings for nearly the full libktx functionality and a test for all the new functionality.

The PR makes the following minor API changes to libktx:

It adds the following functions to the JS binding while modernizing and greatly improving the binding code:

It makes available two bindings. First is a read-only binding, libktx_read.js which has the previously available functions plus findKeyValue, getDataSize, getOETF, getPrimaries and getImage. The second, using the original name libktx.js, has functions for creating new KTX texture objects and writing them to files.

The JS ktxTexture class has been renamed texture as typical usage is with an Emscripten module instance name of ktx and new ktx.ktxTexture() looks weird. Newly added classes do not have ktx prefixes.

Compared to the Phasmatic bindings it:

aqnuep commented 3 months ago

Will it help to bracket these includes and any other write-related code in ktx_wrapper with#if KTX_FEATURE_WRITE?

For some reason I cannot reply on this comment, so replying here: what do you mean by that?

MarkCallow commented 2 months ago

@abasilak, @ViNeek please review this PR. Please pay particular attention to the following:

  1. Will there be any difficulties using these bindings for the glTF-Compressor?
  2. I have changed the name of the factory function for creating the ktx module from LIBKTX to createKtxModule (and createKtxReadModule). I am not planning to provide the old name as an alias because it is a simple change for users to make. I have provided an alias. What is your opinion?
  3. In the test sample I am now setting window.ktx = <module returned from createKtxModule> so now I have code like var texture = new ktx.ktxTexture which is feels daft. I'd like to remove the ktx prefix from all the times that currently have it. What do you think? Note that the all the c++ code for the binding is in a ktx namespace. The extra prefixes really are superfluous in my opinion. I would, if I can figure out how, provide the old names as aliases. I have removed the ktx prefixes from the only existing class that had it, ktxTexture, and from the new classes and have provided an alias from ktxTexture to texture.
  4. Point out any errors or places for improvement in the code.
  5. Comment on the naming style of the classes, enums and values in the JS API.

I would like the glTF-Compressor to be updated to use this binding. Is there any room in your existing Khronos contract for that work? Better reply to me privately on this question.

MarkCallow commented 2 months ago

Here is a screenshot of the test for the new features.

Screenshot 2024-05-31 at 18 40 29
ViNeek commented 2 months ago

Hi @MarkCallow thank you for the great work.

Regarding you comments:

  1. I can' t see any difficulties in integrating these changes to the gltf Compressor.
  2. This looks nice.
  3. Removing the prefix seems like a good, clean choice.
  4. The code seems very well structured and documented.
  5. I really like the ktx.SYMBOL style.

Test page also looks very good.

MarkCallow commented 2 months ago

@ViNeek thank you for your review and comments. I have made one final (fingers crossed) change. I have changed the enumerator names to be the same as the libktx names minus the ktx{,_} prefixes and _e suffixes for consistency and to make it easier for people to recall what the JS name will be. Please take a look at 6966f6c.