KhronosGroup / KTX-Software

KTX (Khronos Texture) Library and Tools
Other
834 stars 222 forks source link

How to call `CreateFromMemory`? #888

Closed javagl closed 3 months ago

javagl commented 3 months ago

The ktxTexture2_CreateFromMemory function has the following signature:

ktxTexture2_CreateFromMemory(const ktx_uint8_t* bytes, ktx_size_t size,
                             ktxTextureCreateFlags createFlags,
                             ktxTexture2** newTex)

It receives ktxTextureCreateFlags. These are typedef'ed to ktx_uint32_t, and there is an enum

enum ktxTextureCreateFlagBits {
    KTX_TEXTURE_CREATE_NO_FLAGS = 0x00,
    KTX_TEXTURE_CREATE_LOAD_IMAGE_DATA_BIT = 0x01,
                                   /*!< Load the images from the KTX source. */
    KTX_TEXTURE_CREATE_RAW_KVDATA_BIT = 0x02,
                                   /*!< Load the raw key-value data instead of
                                        creating a @c ktxHashList from it. */
    KTX_TEXTURE_CREATE_SKIP_KVDATA_BIT = 0x04,
                                   /*!< Skip any key-value data. This overrides
                                        the RAW_KVDATA_BIT. */
    KTX_TEXTURE_CREATE_CHECK_GLTF_BASISU_BIT = 0x08
                                   /*!< Load texture compatible with the rules
                                        of KHR_texture_basisu glTF extension */
};

And this seems to be the one that is intended to be used there (because the documentation explicitly mentions KTX_TEXTURE_CREATE_LOAD_IMAGE_DATA_BIT)

But still, there are several places in the texturetests.cc where this function is called as

        result = ktxTexture2_CreateFromMemory(ktxMemFile, ktxMemFileLen,
                                              KTX_TEXTURE_CREATE_ALLOC_STORAGE,
                                              &texture);

The KTX_TEXTURE_CREATE_ALLOC_STORAGE is not part of the ktxTextureCreateFlagBits. It is part of the ktxTextureCreateStorageEnum.

I assume that the fact that this works is... only a coincidence, because KTX_TEXTURE_CREATE_ALLOC_STORAGE happens to have the same value as KTX_TEXTURE_CREATE_LOAD_IMAGE_DATA_BIT. (And if that's the case: Hooray for C++'s helpless attempt to disguise arbitrary values as "types"...).

But this is either a bug in the tests, or a case of a function being distressingly under-documented.


Some context: A build for the JNI improvements PR is failing in the test for createFromMemory, saying

org.khronos.ktx.test.KtxTexture2Test
[ERROR] testCreateFromMemoryBasic  Time elapsed: 0.023 s  <<< ERROR!
org.khronos.ktx.KtxException: Operation not allowed in the current state.
    at org.khronos.ktx.test.KtxTexture2Test.testCreateFromMemoryBasic(KtxTexture2Test.java:87)

So I wanted to look up what I might have done wrong there. This failure happens only on Linux (and not on Windows), so maybe it's not even my fault, but I'll dig into it...

MarkCallow commented 3 months ago

These are bugs in texturetests, likely copy and paste errors. I'll fix them. They should be using KTX_TEXTURE_CREATE_LOAD_IMAGE_DATA_BIT.

As for invalid operations I don't see any KTX_INVALID_OPERATION error returned by ktxTexture_CreateFromMemory and its calllees.

javagl commented 3 months ago

As for invalid operations I don't see any KTX_INVALID_OPERATION error returned by ktxTexture_CreateFromMemory and its calllees.

I still have to investigate why this is happening, but I added a comment about that in the PR at https://github.com/KhronosGroup/KTX-Software/pull/886#issuecomment-2050035880