Skip to content

Commit

Permalink
Map Astc encoder errors to KTX errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkCallow committed Dec 12, 2024
1 parent 6e8090c commit 890f581
Showing 1 changed file with 64 additions and 10 deletions.
74 changes: 64 additions & 10 deletions lib/astc_codec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,7 @@ struct LaunchDesc {

/**
* @internal
* @~English
* @brief Helper function to translate thread entry points.
*
* Convert a (void*) thread entry to an (int, void*) thread entry, where the
Expand Down Expand Up @@ -573,6 +574,46 @@ launchThreads(int threadCount, void (*func)(int, int, void*), void *payload) {
delete[] threadDescs;
}

/**
* @internal
* @~English
* @brief Map astcenc error code to KTX error code
*
* Asserts are fired on errors reflecting bad parameters passed by libktx
* or astcenc compilation settings that do not permit correct operation.
*
* @param astc_error The error code to be mapped.
* @return An equivalent KTX error code.
*/
static ktx_error_code_e
mapAstcError(astcenc_error astc_error) {
switch (astc_error) {
case ASTCENC_SUCCESS:
return KTX_SUCCESS;
case ASTCENC_ERR_OUT_OF_MEM:
return KTX_OUT_OF_MEMORY;
case ASTCENC_ERR_BAD_BLOCK_SIZE: [[fallthrough]];
case ASTCENC_ERR_BAD_DECODE_MODE: [[fallthrough]];
case ASTCENC_ERR_BAD_FLAGS: [[fallthrough]];
case ASTCENC_ERR_BAD_PARAM: [[fallthrough]];
case ASTCENC_ERR_BAD_PROFILE: [[fallthrough]];
case ASTCENC_ERR_BAD_QUALITY: [[fallthrough]];
case ASTCENC_ERR_BAD_SWIZZLE:
assert(false && "libktx passing bad parameter to astcenc");
return KTX_INVALID_VALUE;
case ASTCENC_ERR_BAD_CONTEXT:
assert(false && "libktx has set up astcenc context incorrectly");
return KTX_INVALID_OPERATION;
case ASTCENC_ERR_BAD_CPU_FLOAT:
assert(false && "Code compiled in way that float operations do not meet codec's assumptions.");
// Most likely compiled with fast match enabled.
return KTX_INVALID_OPERATION;
case ASTCENC_ERR_NOT_IMPLEMENTED:
assert(false && "ASTCENC_BLOCK_MAX_TEXELS not enough for specified block size");
return KTX_UNSUPPORTED_FEATURE;
}
}

/**
* @memberof ktxTexture2
* @ingroup writer
Expand Down Expand Up @@ -605,10 +646,16 @@ launchThreads(int threadCount, void (*func)(int, int, void*), void *payload) {
* The texture's images are 1D. Only 2D images can
* be supercompressed.
* @exception KTX_INVALID_OPERATION
* ASTC compressor failed to compress image.
* ASTC encoder failed to compress image.
* Possibly due to incorrect floating point
* compilation settings. Should not happen
* in release package.
* @exception KTX_INVALID_OPERATION
* This->generateMipmaps is set.
* @exception KTX_OUT_OF_MEMORY Not enough memory to carry out compression.
* @exception KTX_UNSUPPORTED_FEATURE ASTC encoder not compiled with enough
* capacity for requested block size. Should
* not happen in release package.
*/
extern "C" KTX_error_code
ktxTexture2_CompressAstcEx(ktxTexture2* This, ktxAstcParams* params) {
Expand Down Expand Up @@ -717,13 +764,13 @@ ktxTexture2_CompressAstcEx(ktxTexture2* This, ktxAstcParams* params) {
&astc_config);

if (astc_error != ASTCENC_SUCCESS)
return KTX_INVALID_OPERATION;
return mapAstcError(astc_error);

astc_error = astcenc_context_alloc(&astc_config, threadCount,
&astc_context);

if (astc_error != ASTCENC_SUCCESS)
return KTX_INVALID_OPERATION;
return mapAstcError(astc_error);

// Walk in reverse on levels so we don't have to do this later
assert(prototype->dataSize && "Prototype texture size not initialized.\n");
Expand Down Expand Up @@ -785,11 +832,11 @@ ktxTexture2_CompressAstcEx(ktxTexture2* This, ktxAstcParams* params) {
imageFree(input_image);

if (work.error != ASTCENC_SUCCESS) {
std::cout << "ASTC compressor failed\n" <<
astcenc_get_error_string(work.error) << std::endl;
//std::cout << "ASTC compressor failed\n" <<
// astcenc_get_error_string(work.error) << std::endl;

astcenc_context_free(astc_context);
return KTX_INVALID_OPERATION;
return mapAstcError(work.error);
}
}
}
Expand Down Expand Up @@ -944,11 +991,18 @@ static void decompression_workload_runner(int thread_count, int thread_id, void*
* @exception KTX_INVALID_OPERATION
* The texture object does not contain any data.
* @exception KTX_INVALID_OPERATION
* ASTC decoder returned an error.
* ASTC decoder failed to decompress image.
* Possibly due to incorrect floating point
* compilation settings. Should not happen
* in release package.
* @exception KTX_OUT_OF_MEMORY Not enough memory to carry out decoding.
* @exception KTX_UNSUPPORTED_FEATURE
* The texture's images are supercompressed with an
* unsupported scheme.
* @exception KTX_UNSUPPORTED_FEATURE
* ASTC encoder not compiled with enough
* capacity for requested block size. Should
* not happen in release package.
*/
KTX_error_code
ktxTexture2_DecodeAstc(ktxTexture2 *This, ktx_uint32_t vkformat) {
Expand Down Expand Up @@ -1043,12 +1097,12 @@ ktxTexture2_DecodeAstc(ktxTexture2 *This, ktx_uint32_t vkformat) {
&astc_config);

if (astc_error != ASTCENC_SUCCESS)
return KTX_INVALID_OPERATION;
return mapAstcError(astc_error);

astc_error = astcenc_context_alloc(&astc_config, threadCount, &astc_context);

if (astc_error != ASTCENC_SUCCESS)
return KTX_INVALID_OPERATION;
return mapAstcError(astc_error);

decompression_workload work;
work.context = astc_context;
Expand Down Expand Up @@ -1102,7 +1156,7 @@ ktxTexture2_DecodeAstc(ktxTexture2 *This, ktx_uint32_t vkformat) {
//std::cout << "ASTC decompressor failed\n" << astcenc_get_error_string(work.error) << std::endl;

astcenc_context_free(astc_context);
return KTX_INVALID_OPERATION;
return mapAstcError(work.error);
}
}
}
Expand Down

0 comments on commit 890f581

Please sign in to comment.