From 890f581b8b85ed3024b205f4591f654c89b6cbd7 Mon Sep 17 00:00:00 2001 From: Mark Callow Date: Thu, 12 Dec 2024 13:15:39 +0900 Subject: [PATCH] Map Astc encoder errors to KTX errors. --- lib/astc_codec.cpp | 74 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 64 insertions(+), 10 deletions(-) diff --git a/lib/astc_codec.cpp b/lib/astc_codec.cpp index e07c1455e4..6ce46a4530 100644 --- a/lib/astc_codec.cpp +++ b/lib/astc_codec.cpp @@ -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 @@ -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 @@ -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) { @@ -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"); @@ -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); } } } @@ -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) { @@ -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; @@ -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); } } }