Skip to content

Commit a925f3f

Browse files
Remove tabs, replacing with 4 spaces each, fix bug with vkformat in astc decoding
1 parent 24d0fee commit a925f3f

File tree

4 files changed

+108
-106
lines changed

4 files changed

+108
-106
lines changed

lib/astc_encode.cpp

Lines changed: 89 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ astcSwizzle(const ktxAstcParams &params) {
360360

361361
static void
362362
astcBlockDimensions(ktx_uint32_t block_size,
363-
uint32_t& block_x, uint32_t& block_y, uint32_t& block_z) {
363+
uint32_t& block_x, uint32_t& block_y, uint32_t& block_z) {
364364
switch (block_size) {
365365
case KTX_PACK_ASTC_BLOCK_DIMENSION_4x4 : block_x = 4; block_y = 4; block_z = 1; break;
366366
case KTX_PACK_ASTC_BLOCK_DIMENSION_5x4 : block_x = 5; block_y = 4; block_z = 1; break;
@@ -393,7 +393,7 @@ astcBlockDimensions(ktx_uint32_t block_size,
393393

394394
static void
395395
astcBlockDimensions(VkFormat format,
396-
uint32_t &x, uint32_t &y, uint32_t &z) noexcept {
396+
uint32_t &x, uint32_t &y, uint32_t &z) noexcept {
397397
switch (format) {
398398
case VK_FORMAT_ASTC_4x4_UNORM_BLOCK: x = 4; y = 4; z = 0; break;
399399
case VK_FORMAT_ASTC_4x4_SRGB_BLOCK: x = 4; y = 4; z = 0; break;
@@ -887,12 +887,12 @@ ktxTexture2_CompressAstc(ktxTexture2* This, ktx_uint32_t quality) {
887887

888888
struct decompression_workload
889889
{
890-
astcenc_context* context;
891-
uint8_t* data;
892-
size_t data_len;
893-
astcenc_image* image_out;
894-
astcenc_swizzle swizzle;
895-
astcenc_error error;
890+
astcenc_context* context;
891+
uint8_t* data;
892+
size_t data_len;
893+
astcenc_image* image_out;
894+
astcenc_swizzle swizzle;
895+
astcenc_error error;
896896
};
897897

898898
/**
@@ -903,17 +903,17 @@ struct decompression_workload
903903
* @param payload The parameters for this thread.
904904
*/
905905
static void decompression_workload_runner(int thread_count, int thread_id, void* payload) {
906-
(void)thread_count;
907-
908-
decompression_workload* work = static_cast<decompression_workload*>(payload);
909-
astcenc_error error = astcenc_decompress_image(work->context, work->data, work->data_len,
910-
work->image_out, &work->swizzle, thread_id);
911-
// This is a racy update, so which error gets returned is a random, but it
912-
// will reliably report an error if an error occurs
913-
if (error != ASTCENC_SUCCESS)
914-
{
915-
work->error = error;
916-
}
906+
(void)thread_count;
907+
908+
decompression_workload* work = static_cast<decompression_workload*>(payload);
909+
astcenc_error error = astcenc_decompress_image(work->context, work->data, work->data_len,
910+
work->image_out, &work->swizzle, thread_id);
911+
// This is a racy update, so which error gets returned is a random, but it
912+
// will reliably report an error if an error occurs
913+
if (error != ASTCENC_SUCCESS)
914+
{
915+
work->error = error;
916+
}
917917
}
918918

919919
/**
@@ -925,7 +925,7 @@ static void decompression_workload_runner(int thread_count, int thread_id, void*
925925
// will update "This" with the uncompressed copy
926926
KTX_API KTX_error_code KTX_APIENTRY
927927
ktxTexture2_DecodeAstc(ktxTexture2 *This, ktx_uint32_t vkformat) {
928-
// Decompress This using astc-encoder
928+
// Decompress This using astc-encoder
929929
uint32_t* BDB = This->pDfd + 1;
930930
khr_df_model_e colorModel = (khr_df_model_e)KHR_DFDVAL(BDB, MODEL);
931931
if (colorModel != KHR_DF_MODEL_ASTC && This->supercompressionScheme != KTX_SS_NONE) // No supercompression supported yet
@@ -935,12 +935,12 @@ ktxTexture2_DecodeAstc(ktxTexture2 *This, ktx_uint32_t vkformat) {
935935

936936
DECLARE_PRIVATE(priv, This);
937937

938-
uint32_t channelId = KHR_DFDSVAL(BDB, 0, CHANNELID);
939-
if (channelId == KHR_DF_CHANNEL_ASTC_DATA) {
940-
// Found astc data
941-
}
942-
else
943-
return KTX_FILE_DATA_ERROR;
938+
uint32_t channelId = KHR_DFDSVAL(BDB, 0, CHANNELID);
939+
if (channelId == KHR_DF_CHANNEL_ASTC_DATA) {
940+
// Found astc data
941+
}
942+
else
943+
return KTX_FILE_DATA_ERROR;
944944

945945
// Create a prototype texture to use for calculating sizes in the target
946946
// format and, as useful side effects, provide us with a properly sized
@@ -984,7 +984,7 @@ ktxTexture2_DecodeAstc(ktxTexture2 *This, ktx_uint32_t vkformat) {
984984
}
985985
}
986986

987-
// This is where I do the decompression from "This" to prototype target
987+
// This is where I do the decompression from "This" to prototype target
988988
astcenc_profile profile{ASTCENC_PRF_LDR_SRGB};
989989
astcenc_swizzle swizzle{ASTCENC_SWZ_R, ASTCENC_SWZ_G, ASTCENC_SWZ_B, ASTCENC_SWZ_A};
990990

@@ -994,15 +994,15 @@ ktxTexture2_DecodeAstc(ktxTexture2 *This, ktx_uint32_t vkformat) {
994994
float quality{ASTCENC_PRE_MEDIUM};
995995
uint32_t flags{0}; // TODO: Use normals mode to reconstruct normals params->normalMap ? ASTCENC_FLG_MAP_NORMAL : 0};
996996

997-
astcBlockDimensions((VkFormat)vkformat, block_size_x, block_size_y, block_size_z);
997+
astcBlockDimensions((VkFormat)This->vkFormat, block_size_x, block_size_y, block_size_z);
998998

999999
// quality = astcQuality(params->qualityLevel);
10001000
// profile = astcEncoderAction(*params, BDB);
10011001
// swizzle = astcSwizzle(*params);
10021002

10031003
// if(params->perceptual) flags |= ASTCENC_FLG_USE_PERCEPTUAL;
10041004

1005-
uint32_t threadCount{1}; // Decompression isn't the bottlneck and only used when checking for psnr and ssim
1005+
uint32_t threadCount{1}; // Decompression isn't the bottlneck and only used when checking for psnr and ssim
10061006
astcenc_config astc_config;
10071007
astcenc_context *astc_context;
10081008
astcenc_error astc_error = astcenc_config_init(profile,
@@ -1018,69 +1018,69 @@ ktxTexture2_DecodeAstc(ktxTexture2 *This, ktx_uint32_t vkformat) {
10181018
if (astc_error != ASTCENC_SUCCESS)
10191019
return KTX_INVALID_OPERATION;
10201020

1021-
decompression_workload work;
1022-
work.context = astc_context;
1023-
work.swizzle = swizzle;
1024-
work.error = ASTCENC_SUCCESS;
1025-
1026-
for (uint32_t levelIndex = 0; levelIndex < This->numLevels; ++levelIndex) {
1027-
const uint32_t imageWidth = std::max(This->baseWidth >> levelIndex, 1u);
1028-
const uint32_t imageHeight = std::max(This->baseHeight >> levelIndex, 1u);
1029-
const uint32_t imageDepths = std::max(This->baseDepth >> levelIndex, 1u);
1030-
1031-
for (uint32_t layerIndex = 0; layerIndex < This->numLayers; ++layerIndex) {
1032-
for (uint32_t faceIndex = 0; faceIndex < This->numFaces; ++faceIndex) {
1033-
for (uint32_t depthSliceIndex = 0; depthSliceIndex < imageDepths; ++depthSliceIndex) {
1034-
1035-
ktx_size_t levelImageSizeIn = ktxTexture_calcImageSize(ktxTexture(This), levelIndex, KTX_FORMAT_VERSION_TWO);
1036-
1037-
ktx_size_t imageOffsetIn;
1038-
ktx_size_t imageOffsetOut;
1039-
1040-
ktxTexture2_GetImageOffset(This, levelIndex, layerIndex, faceIndex + depthSliceIndex, &imageOffsetIn);
1041-
ktxTexture2_GetImageOffset(prototype, levelIndex, layerIndex, faceIndex + depthSliceIndex, &imageOffsetOut);
1042-
1043-
auto* imageDataIn = This->pData + imageOffsetIn;
1044-
auto* imageDataOut = prototype->pData + imageOffsetOut;
1045-
1046-
astcenc_image imageOut;
1047-
imageOut.dim_x = imageWidth;
1048-
imageOut.dim_y = imageHeight;
1049-
imageOut.dim_z = imageDepths;
1050-
imageOut.data_type = ASTCENC_TYPE_U8; // TODO: Fix for HDR types
1051-
imageOut.data = (void**)&imageDataOut; // TODO: Fix for HDR types
1052-
1053-
work.data = imageDataIn;
1054-
work.data_len = levelImageSizeIn;
1055-
work.image_out = &imageOut;
1056-
1057-
// Only launch worker threads for multi-threaded use - it makes basic
1058-
// single-threaded profiling and debugging a little less convoluted
1059-
if (threadCount > 1) {
1060-
launchThreads(threadCount, decompression_workload_runner, &work);
1061-
} else {
1062-
work.error = astcenc_decompress_image(work.context, work.data, work.data_len,
1063-
work.image_out, &work.swizzle, 0);
1064-
}
1065-
1066-
// Reset ASTC context for next image
1067-
astcenc_decompress_reset(astc_context);
1068-
1069-
if (work.error != ASTCENC_SUCCESS) {
1070-
std::cout << "ASTC decompressor failed\n" << astcenc_get_error_string(work.error) << std::endl;
1071-
1072-
astcenc_context_free(astc_context);
1073-
return KTX_INVALID_OPERATION;
1074-
}
1075-
}
1076-
}
1077-
}
1078-
}
1021+
decompression_workload work;
1022+
work.context = astc_context;
1023+
work.swizzle = swizzle;
1024+
work.error = ASTCENC_SUCCESS;
1025+
1026+
for (uint32_t levelIndex = 0; levelIndex < This->numLevels; ++levelIndex) {
1027+
const uint32_t imageWidth = std::max(This->baseWidth >> levelIndex, 1u);
1028+
const uint32_t imageHeight = std::max(This->baseHeight >> levelIndex, 1u);
1029+
const uint32_t imageDepths = std::max(This->baseDepth >> levelIndex, 1u);
1030+
1031+
for (uint32_t layerIndex = 0; layerIndex < This->numLayers; ++layerIndex) {
1032+
for (uint32_t faceIndex = 0; faceIndex < This->numFaces; ++faceIndex) {
1033+
for (uint32_t depthSliceIndex = 0; depthSliceIndex < imageDepths; ++depthSliceIndex) {
1034+
1035+
ktx_size_t levelImageSizeIn = ktxTexture_calcImageSize(ktxTexture(This), levelIndex, KTX_FORMAT_VERSION_TWO);
1036+
1037+
ktx_size_t imageOffsetIn;
1038+
ktx_size_t imageOffsetOut;
1039+
1040+
ktxTexture2_GetImageOffset(This, levelIndex, layerIndex, faceIndex + depthSliceIndex, &imageOffsetIn);
1041+
ktxTexture2_GetImageOffset(prototype, levelIndex, layerIndex, faceIndex + depthSliceIndex, &imageOffsetOut);
1042+
1043+
auto* imageDataIn = This->pData + imageOffsetIn;
1044+
auto* imageDataOut = prototype->pData + imageOffsetOut;
1045+
1046+
astcenc_image imageOut;
1047+
imageOut.dim_x = imageWidth;
1048+
imageOut.dim_y = imageHeight;
1049+
imageOut.dim_z = imageDepths;
1050+
imageOut.data_type = ASTCENC_TYPE_U8; // TODO: Fix for HDR types
1051+
imageOut.data = (void**)&imageDataOut; // TODO: Fix for HDR types
1052+
1053+
work.data = imageDataIn;
1054+
work.data_len = levelImageSizeIn;
1055+
work.image_out = &imageOut;
1056+
1057+
// Only launch worker threads for multi-threaded use - it makes basic
1058+
// single-threaded profiling and debugging a little less convoluted
1059+
if (threadCount > 1) {
1060+
launchThreads(threadCount, decompression_workload_runner, &work);
1061+
} else {
1062+
work.error = astcenc_decompress_image(work.context, work.data, work.data_len,
1063+
work.image_out, &work.swizzle, 0);
1064+
}
1065+
1066+
// Reset ASTC context for next image
1067+
astcenc_decompress_reset(astc_context);
1068+
1069+
if (work.error != ASTCENC_SUCCESS) {
1070+
std::cout << "ASTC decompressor failed\n" << astcenc_get_error_string(work.error) << std::endl;
1071+
1072+
astcenc_context_free(astc_context);
1073+
return KTX_INVALID_OPERATION;
1074+
}
1075+
}
1076+
}
1077+
}
1078+
}
10791079

1080-
// We are done with astcencoder
1080+
// We are done with astcdecoder
10811081
astcenc_context_free(astc_context);
10821082

1083-
if (result == KTX_SUCCESS) {
1083+
if (result == KTX_SUCCESS) {
10841084
// Fix up the current texture
10851085
DECLARE_PROTECTED(thisPrtctd, This);
10861086
DECLARE_PRIVATE(protoPriv, prototype);

tools/ktx/command_create.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,11 @@ struct OptionsCreate {
9595

9696
void init(cxxopts::Options& opts) {
9797
opts.add_options()
98-
(kFormat, "The data format of the images in the output KTX file."
98+
(kFormat, "VkFormat enum that specifies the data format of the images in the output KTX file."
9999
" The enum names are matching the VkFormats without the VK_FORMAT_ prefix."
100100
" The VK_FORMAT_ prefix is ignored if present."
101-
"\nWhen used with --encode it specifies the format of the input files before the encoding step."
101+
"\nWhen used with --encode it specifies the format of the texture object created as input to "
102+
" the encoding step before the encoding step."
102103
" In this case it must be one of:"
103104
"\n R8_UNORM"
104105
"\n R8_SRGB"
@@ -582,10 +583,11 @@ Create a KTX2 file from various input files.
582583
The following options are available:
583584
<dl>
584585
<dt>\--format &lt;enum&gt;</dt>
585-
<dd>The data format of the images in the output KTX file.
586+
<dd>VkFormat enum that specifies the data format of the images in the output KTX file.
586587
The enum names are matching the VkFormats without the VK_FORMAT_ prefix.
587588
The VK_FORMAT_ prefix is ignored if present.<br />
588-
When used with --encode it specifies the format of the input files before the encoding step.
589+
When used with --encode it specifies the format of the texture object created as input
590+
to the encoding step before the encoding step.
589591
In this case it must be one of:
590592
<ul>
591593
<li>R8_UNORM</li>

tools/ktx/command_encode_astc.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ void CommandEncodeAstc::executeEncodeAstc() {
158158
MetricsCalculator metrics;
159159
metrics.saveReferenceImages(texture, options, *this);
160160

161-
options.mode = KTX_PACK_ASTC_ENCODER_MODE_LDR; // TODO: Fix me for HDR textures
161+
options.mode = KTX_PACK_ASTC_ENCODER_MODE_LDR; // TODO: Fix me for HDR textures
162162
ret = ktxTexture2_CompressAstcEx(texture, &options);
163163
if (ret != KTX_SUCCESS)
164164
fatal(rc::IO_FAILURE, "Failed to encode KTX2 file with codec \"{}\". KTX Error: {}", ktxErrorString(ret));

tools/ktx/metrics_utils.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -94,22 +94,22 @@ class MetricsCalculator {
9494
KTXTexture2 texture{static_cast<ktxTexture2*>(malloc(sizeof(ktxTexture2)))};
9595
ktxTexture2_constructCopy(texture, encodedTexture);
9696

97-
// Start with a default swizzle
97+
// Start with a default swizzle
9898
TranscodeSwizzleInfo tSwizzleInfo{};
99-
tSwizzleInfo.defaultNumComponents = 4;
100-
tSwizzleInfo.swizzle = "rgba";
99+
tSwizzleInfo.defaultNumComponents = 4;
100+
tSwizzleInfo.swizzle = "rgba";
101101

102102
ktx_error_code_e ec = KTX_SUCCESS;
103103
// Decode the encoded texture to observe the compression losses
104-
if (isFormatAstc((VkFormat)texture->vkFormat))
105-
{
106-
ec = ktxTexture2_DecodeAstc(texture, VK_FORMAT_R8G8B8A8_UNORM);
107-
}
108-
else
109-
{
110-
ec = ktxTexture2_TranscodeBasis(texture, KTX_TTF_RGBA32, 0);
111-
tSwizzleInfo = determineTranscodeSwizzle(texture, report);
112-
}
104+
if (isFormatAstc((VkFormat)texture->vkFormat))
105+
{
106+
ec = ktxTexture2_DecodeAstc(texture, VK_FORMAT_R8G8B8A8_UNORM);
107+
}
108+
else
109+
{
110+
ec = ktxTexture2_TranscodeBasis(texture, KTX_TTF_RGBA32, 0);
111+
tSwizzleInfo = determineTranscodeSwizzle(texture, report);
112+
}
113113
if (ec != KTX_SUCCESS)
114114
report.fatal(rc::KTX_FAILURE, "Failed to transcode KTX2 texture to calculate error metrics: {}", ktxErrorString(ec));
115115

0 commit comments

Comments
 (0)