* @ingroup reader * @brief Decodes a ktx2 texture object, if it is ASTC encoded. * The decompressed format is calculated from corresponding ASTC format. There are * only 3 possible options currently supported. RGBA8, SRGBA8 and RGBA32. * @note 3d textures are decoded to a multi-slice 3d texture. * * Updates @p This with the decoded image. * * @param This The texture to decode * * @r
| 332 | * not happen in release package. |
| 333 | */ |
| 334 | KTX_error_code |
| 335 | ktxTexture2_DecodeAstc(ktxTexture2 *This) { |
| 336 | // Decompress This using astc-decoder |
| 337 | uint32_t* BDB = This->pDfd + 1; |
| 338 | khr_df_model_e colorModel = (khr_df_model_e)KHR_DFDVAL(BDB, MODEL); |
| 339 | if (colorModel != KHR_DF_MODEL_ASTC) { |
| 340 | return KTX_INVALID_OPERATION; // Not in astc decodable format |
| 341 | } |
| 342 | if (This->supercompressionScheme == KTX_SS_BASIS_LZ) { |
| 343 | return KTX_FILE_DATA_ERROR; // Not a valid file. |
| 344 | } |
| 345 | // Safety check. |
| 346 | if (This->supercompressionScheme > KTX_SS_END_RANGE) { |
| 347 | return KTX_UNSUPPORTED_FEATURE; // Unsupported scheme. |
| 348 | } |
| 349 | // Other schemes are decoded in ktxTexture2_LoadImageData. |
| 350 | |
| 351 | DECLARE_PRIVATE_EX(priv, This); |
| 352 | |
| 353 | uint32_t channelId = KHR_DFDSVAL(BDB, 0, CHANNELID); |
| 354 | if (channelId != KHR_DF_CHANNEL_ASTC_DATA) { |
| 355 | return KTX_FILE_DATA_ERROR; |
| 356 | } |
| 357 | |
| 358 | ktx_uint32_t vkformat = (ktx_uint32_t)getUncompressedFormat(This); |
| 359 | |
| 360 | // Create a prototype texture to use for calculating sizes in the target |
| 361 | // format and, as useful side effects, provide us with a properly sized |
| 362 | // data allocation and the DFD for the target format. |
| 363 | ktxTextureCreateInfo createInfo; |
| 364 | createInfo.glInternalformat = 0; |
| 365 | createInfo.vkFormat = vkformat; |
| 366 | createInfo.baseWidth = This->baseWidth; |
| 367 | createInfo.baseHeight = This->baseHeight; |
| 368 | createInfo.baseDepth = This->baseDepth; |
| 369 | createInfo.generateMipmaps = This->generateMipmaps; |
| 370 | createInfo.isArray = This->isArray; |
| 371 | createInfo.numDimensions = This->numDimensions; |
| 372 | createInfo.numFaces = This->numFaces; |
| 373 | createInfo.numLayers = This->numLayers; |
| 374 | createInfo.numLevels = This->numLevels; |
| 375 | createInfo.pDfd = nullptr; |
| 376 | |
| 377 | KTX_error_code result; |
| 378 | ktxTexture2* prototype; |
| 379 | result = ktxTexture2_Create(&createInfo, KTX_TEXTURE_CREATE_ALLOC_STORAGE, &prototype); |
| 380 | |
| 381 | if (result != KTX_SUCCESS) { |
| 382 | assert(result == KTX_OUT_OF_MEMORY); // The only run time error |
| 383 | return result; |
| 384 | } |
| 385 | |
| 386 | if (!This->pData) { |
| 387 | if (ktxTexture_isActiveStream((ktxTexture*)This)) { |
| 388 | // Load pending. Complete it. |
| 389 | result = ktxTexture2_LoadImageData(This, NULL, 0); |
| 390 | if (result != KTX_SUCCESS) |
| 391 | { |