From 19db204bf6bcdbd9d2995e309f8f268a514dd788 Mon Sep 17 00:00:00 2001 From: germanaizek Date: Fri, 27 May 2022 18:50:26 +0300 Subject: [PATCH] Code refactor, fixed bugs and add Win64 func --- basisu_tool.cpp | 64 +++++++++++++------------ encoder/basisu_basis_file.h | 4 +- encoder/basisu_comp.cpp | 17 +++---- encoder/basisu_enc.cpp | 11 +++-- encoder/basisu_enc.h | 12 ++--- encoder/basisu_frontend.cpp | 8 ++-- encoder/basisu_frontend.h | 2 +- encoder/basisu_opencl.cpp | 6 +-- encoder/basisu_uastc_enc.cpp | 2 +- encoder/pvpngreader.cpp | 3 +- transcoder/basisu.h | 4 +- transcoder/basisu_transcoder.cpp | 8 ++-- transcoder/basisu_transcoder_internal.h | 4 +- 13 files changed, 72 insertions(+), 73 deletions(-) diff --git a/basisu_tool.cpp b/basisu_tool.cpp index 13ed340f..156e9b3d 100644 --- a/basisu_tool.cpp +++ b/basisu_tool.cpp @@ -1136,7 +1136,7 @@ static bool compress_mode(command_line_params &opts) if (opts.m_output_path.size()) string_combine_path(filename, opts.m_output_path.c_str(), filename.c_str()); - params.m_out_filename = filename; + params.m_out_filename = std::move(filename); } if (opts.m_parallel_compression) @@ -1244,40 +1244,41 @@ static bool compress_mode(command_line_params &opts) } } - if ((pCSV_file) && (c.get_stats().size())) + const auto& stats = c.get_stats(); + if ((pCSV_file) && (stats.size())) { - if (c.get_stats().size()) + if (stats.size()) { float rgb_avg_psnr_min = 1e+9f, rgb_avg_psnr_avg = 0.0f; float a_avg_psnr_min = 1e+9f, a_avg_psnr_avg = 0.0f; float luma_709_psnr_min = 1e+9f, luma_709_psnr_avg = 0.0f; - for (size_t slice_index = 0; slice_index < c.get_stats().size(); slice_index++) + for (size_t slice_index = 0; slice_index < stats.size(); slice_index++) { - rgb_avg_psnr_min = basisu::minimum(rgb_avg_psnr_min, c.get_stats()[slice_index].m_basis_rgb_avg_psnr); - rgb_avg_psnr_avg += c.get_stats()[slice_index].m_basis_rgb_avg_psnr; + rgb_avg_psnr_min = basisu::minimum(rgb_avg_psnr_min, stats[slice_index].m_basis_rgb_avg_psnr); + rgb_avg_psnr_avg += stats[slice_index].m_basis_rgb_avg_psnr; - a_avg_psnr_min = basisu::minimum(a_avg_psnr_min, c.get_stats()[slice_index].m_basis_a_avg_psnr); - a_avg_psnr_avg += c.get_stats()[slice_index].m_basis_a_avg_psnr; + a_avg_psnr_min = basisu::minimum(a_avg_psnr_min, stats[slice_index].m_basis_a_avg_psnr); + a_avg_psnr_avg += stats[slice_index].m_basis_a_avg_psnr; - luma_709_psnr_min = basisu::minimum(luma_709_psnr_min, c.get_stats()[slice_index].m_basis_luma_709_psnr); - luma_709_psnr_avg += c.get_stats()[slice_index].m_basis_luma_709_psnr; + luma_709_psnr_min = basisu::minimum(luma_709_psnr_min, stats[slice_index].m_basis_luma_709_psnr); + luma_709_psnr_avg += stats[slice_index].m_basis_luma_709_psnr; } - rgb_avg_psnr_avg /= c.get_stats().size(); - a_avg_psnr_avg /= c.get_stats().size(); - luma_709_psnr_avg /= c.get_stats().size(); + rgb_avg_psnr_avg /= stats.size(); + a_avg_psnr_avg /= stats.size(); + luma_709_psnr_avg /= stats.size(); fprintf(pCSV_file, "\"%s\", %u, %u, %u, %u, %u, %f, %f, %f, %f, %f, %u, %u, %f, %f, %f, %f, %f, %f, %f\n", params.m_out_filename.c_str(), c.get_basis_file_size(), - (uint32_t)c.get_stats().size(), - c.get_stats()[0].m_width, c.get_stats()[0].m_height, (uint32_t)c.get_any_source_image_has_alpha(), + (uint32_t)stats.size(), + stats[0].m_width, stats[0].m_height, (uint32_t)c.get_any_source_image_has_alpha(), c.get_basis_bits_per_texel(), - c.get_stats()[0].m_basis_rgb_avg_psnr, - c.get_stats()[0].m_basis_rgba_avg_psnr, - c.get_stats()[0].m_basis_luma_709_psnr, - c.get_stats()[0].m_best_etc1s_luma_709_psnr, + stats[0].m_basis_rgb_avg_psnr, + stats[0].m_basis_rgba_avg_psnr, + stats[0].m_basis_luma_709_psnr, + stats[0].m_best_etc1s_luma_709_psnr, params.m_quality_level, (int)params.m_compression_level, tm.get_elapsed_secs(), rgb_avg_psnr_min, rgb_avg_psnr_avg, a_avg_psnr_min, a_avg_psnr_avg, @@ -1425,18 +1426,20 @@ static bool unpack_and_validate_ktx2_file( printf("Total key values: %u\n", dec.get_key_values().size()); for (uint32_t i = 0; i < dec.get_key_values().size(); i++) { - printf("%u. Key: \"%s\", Value length in bytes: %u", i, (const char*)dec.get_key_values()[i].m_key.data(), dec.get_key_values()[i].m_value.size()); + const auto& key = dec.get_key_values()[i].m_key; + const auto& value = dec.get_key_values()[i].m_value; + printf("%u. Key: \"%s\", Value length in bytes: %u", i, (const char*)key.data(), value.size()); - if (dec.get_key_values()[i].m_value.size() > 256) + if (value.size() > 256) continue; bool is_ascii = true; - for (uint32_t j = 0; j < dec.get_key_values()[i].m_value.size(); j++) + for (uint32_t j = 0; j < value.size(); j++) { - uint8_t c = dec.get_key_values()[i].m_value[j]; + uint8_t c = value[j]; if (!( ((c >= ' ') && (c < 0x80)) || - ((j == dec.get_key_values()[i].m_value.size() - 1) && (!c)) + ((j == value.size() - 1) && (!c)) )) { is_ascii = false; @@ -1446,18 +1449,18 @@ static bool unpack_and_validate_ktx2_file( if (is_ascii) { - uint8_vec s(dec.get_key_values()[i].m_value); + uint8_vec s(value); s.push_back(0); printf(" Value String: \"%s\"", (const char *)s.data()); } else { printf(" Value Bytes: "); - for (uint32_t j = 0; j < dec.get_key_values()[i].m_value.size(); j++) + for (uint32_t j = 0; j < value.size(); j++) { if (j) printf(","); - printf("0x%X", dec.get_key_values()[i].m_value[j]); + printf("0x%X", value[j]); } } printf("\n"); @@ -1467,10 +1470,11 @@ static bool unpack_and_validate_ktx2_file( { printf("ETC1S header:\n"); + const auto& header = dec.get_etc1s_header(); printf("Endpoint Count: %u, Selector Count: %u, Endpoint Length: %u, Selector Length: %u, Tables Length: %u, Extended Length: %u\n", - (uint32_t)dec.get_etc1s_header().m_endpoint_count, (uint32_t)dec.get_etc1s_header().m_selector_count, - (uint32_t)dec.get_etc1s_header().m_endpoints_byte_length, (uint32_t)dec.get_etc1s_header().m_selectors_byte_length, - (uint32_t)dec.get_etc1s_header().m_tables_byte_length, (uint32_t)dec.get_etc1s_header().m_extended_byte_length); + (uint32_t)header.m_endpoint_count, (uint32_t)header.m_selector_count, + (uint32_t)header.m_endpoints_byte_length, (uint32_t)header.m_selectors_byte_length, + (uint32_t)header.m_tables_byte_length, (uint32_t)header.m_extended_byte_length); printf("Total ETC1S image descs: %u\n", dec.get_etc1s_image_descs().size()); for (uint32_t i = 0; i < dec.get_etc1s_image_descs().size(); i++) diff --git a/encoder/basisu_basis_file.h b/encoder/basisu_basis_file.h index 98498a01..23ead589 100644 --- a/encoder/basisu_basis_file.h +++ b/encoder/basisu_basis_file.h @@ -23,9 +23,7 @@ namespace basisu BASISU_NO_EQUALS_OR_COPY_CONSTRUCT(basisu_file); public: - basisu_file() - { - } + basisu_file() = default; void clear() { diff --git a/encoder/basisu_comp.cpp b/encoder/basisu_comp.cpp index 166a1c4f..2ff44bab 100644 --- a/encoder/basisu_comp.cpp +++ b/encoder/basisu_comp.cpp @@ -780,7 +780,7 @@ namespace basisu enlarge_vector(m_slice_images, 1); enlarge_vector(m_slice_descs, 1); - m_stats[dest_image_index].m_filename = source_filename.c_str(); + m_stats[dest_image_index].m_filename = source_filename; m_stats[dest_image_index].m_width = orig_width; m_stats[dest_image_index].m_height = orig_height; @@ -1619,16 +1619,13 @@ namespace basisu if ((!m_params.m_uastc) && (m_frontend.get_params().m_debug_images)) { // Write "best" ETC1S debug images - if (!m_params.m_uastc) - { - gpu_image best_etc1s_gpu_image(m_best_etc1s_images[slice_index]); - best_etc1s_gpu_image.override_dimensions(slice_desc.m_orig_width, slice_desc.m_orig_height); - write_compressed_texture_file((out_basename + "_best_etc1s.ktx").c_str(), best_etc1s_gpu_image); + gpu_image best_etc1s_gpu_image(m_best_etc1s_images[slice_index]); + best_etc1s_gpu_image.override_dimensions(slice_desc.m_orig_width, slice_desc.m_orig_height); + write_compressed_texture_file((out_basename + "_best_etc1s.ktx").c_str(), best_etc1s_gpu_image); - image best_etc1s_unpacked; - best_etc1s_gpu_image.unpack(best_etc1s_unpacked); - save_png(out_basename + "_best_etc1s.png", best_etc1s_unpacked); - } + image best_etc1s_unpacked; + best_etc1s_gpu_image.unpack(best_etc1s_unpacked); + save_png(out_basename + "_best_etc1s.png", best_etc1s_unpacked); } if (m_params.m_debug_images) diff --git a/encoder/basisu_enc.cpp b/encoder/basisu_enc.cpp index b427215e..9ee909fa 100644 --- a/encoder/basisu_enc.cpp +++ b/encoder/basisu_enc.cpp @@ -514,11 +514,12 @@ namespace basisu #endif if (!pFile) return false; - - fseek(pFile, 0, SEEK_END); -#ifdef _WIN32 + +#ifdef _WIN32 + _fseeki64(pFile, 0, SEEK_END); int64_t filesize = _ftelli64(pFile); #else + fseek(pFile, 0, SEEK_END); int64_t filesize = ftello(pFile); #endif if (filesize < 0) @@ -526,7 +527,11 @@ namespace basisu fclose(pFile); return false; } +#ifdef _WIN32 + _fseeki64(pFile, 0, SEEK_SET); +#else fseek(pFile, 0, SEEK_SET); +#endif if (sizeof(size_t) == sizeof(uint32_t)) { diff --git a/encoder/basisu_enc.h b/encoder/basisu_enc.h index 0efeaa46..a94bc317 100644 --- a/encoder/basisu_enc.h +++ b/encoder/basisu_enc.h @@ -1273,7 +1273,7 @@ namespace basisu std::mt19937 m_mt; public: - rand() { } + rand() = default; rand(uint32_t s) { seed(s); } void seed(uint32_t s) { m_mt.seed(s); } @@ -2267,9 +2267,7 @@ namespace basisu class huffman_encoding_table { public: - huffman_encoding_table() - { - } + huffman_encoding_table() = default; void clear() { @@ -2464,7 +2462,7 @@ namespace basisu class huff2D { public: - huff2D() { } + huff2D() = default; huff2D(uint32_t bits_per_sym, uint32_t total_syms_per_group) { init(bits_per_sym, total_syms_per_group); } inline const histogram &get_histogram() const { return m_histogram; } @@ -2561,9 +2559,7 @@ namespace basisu class palette_index_reorderer { public: - palette_index_reorderer() - { - } + palette_index_reorderer() = default; void clear() { diff --git a/encoder/basisu_frontend.cpp b/encoder/basisu_frontend.cpp index 1f30a33c..35b78c24 100644 --- a/encoder/basisu_frontend.cpp +++ b/encoder/basisu_frontend.cpp @@ -2240,7 +2240,8 @@ namespace basisu } // block_index_iter #ifndef __EMSCRIPTEN__ - m_params.m_pJob_pool->wait_for_all(); + if (m_params.m_pJob_pool) + m_params.m_pJob_pool->wait_for_all(); #endif vec16F_clusterizer selector_clusterizer; @@ -2861,7 +2862,6 @@ namespace basisu total_subblocks_examined += total_pixels / 8; etc1_optimizer optimizer; - etc1_solution_coordinates solutions[2]; etc1_optimizer::params cluster_optimizer_params; cluster_optimizer_params.m_num_src_pixels = total_pixels; @@ -3242,8 +3242,8 @@ namespace basisu debug_printf("basisu_frontend::reoptimize_remapped_endpoints: stage 2\n"); - m_endpoint_clusters = new_endpoint_clusters; - m_endpoint_cluster_etc_params = new_endpoint_cluster_etc_params; + m_endpoint_clusters = std::move(new_endpoint_clusters); + m_endpoint_cluster_etc_params = std::move(new_endpoint_cluster_etc_params); eliminate_redundant_or_empty_endpoint_clusters(); diff --git a/encoder/basisu_frontend.h b/encoder/basisu_frontend.h index cda73f39..eaaf9f4c 100644 --- a/encoder/basisu_frontend.h +++ b/encoder/basisu_frontend.h @@ -28,7 +28,7 @@ namespace basisu { uint32_t m_comps[2]; - vec2U() { } + vec2U() = default; vec2U(uint32_t a, uint32_t b) { set(a, b); } void set(uint32_t a, uint32_t b) { m_comps[0] = a; m_comps[1] = b; } diff --git a/encoder/basisu_opencl.cpp b/encoder/basisu_opencl.cpp index 81e3090a..09a6506f 100644 --- a/encoder/basisu_opencl.cpp +++ b/encoder/basisu_opencl.cpp @@ -62,9 +62,7 @@ namespace basisu m_ocl_mutex.unlock(); } - ~ocl() - { - } + ~ocl() = default; bool is_initialized() const { return m_device_id != nullptr; } @@ -891,7 +889,7 @@ namespace basisu g_ocl.destroy_command_queue(pContext->m_command_queue); - memset(pContext, 0, sizeof(opencl_context)); + memset(pContext, 0, sizeof(opencl_context_ptr)); free(pContext); diff --git a/encoder/basisu_uastc_enc.cpp b/encoder/basisu_uastc_enc.cpp index 271bbc6f..650f8d8e 100644 --- a/encoder/basisu_uastc_enc.cpp +++ b/encoder/basisu_uastc_enc.cpp @@ -3789,7 +3789,7 @@ namespace basisu { uint64_t m_sel; uint32_t m_ofs; - selector_bitsequence() { } + selector_bitsequence() = default; selector_bitsequence(uint32_t bit_ofs, uint64_t sel) : m_sel(sel), m_ofs(bit_ofs) { } bool operator== (const selector_bitsequence& other) const { diff --git a/encoder/pvpngreader.cpp b/encoder/pvpngreader.cpp index 46639f27..a0d7decb 100644 --- a/encoder/pvpngreader.cpp +++ b/encoder/pvpngreader.cpp @@ -551,6 +551,7 @@ void* png_decoder::png_malloc(uint32_t len) break; if (j == PNG_MAX_ALLOC_BLOCKS) + delete p; return nullptr; m_pMalloc_blocks[j] = p; @@ -666,7 +667,7 @@ int64_t png_decoder::fetch_next_chunk_dword() return status; if (status != 4) - terminate(PNG_BAD_CHUNK_SIZE); + return terminate(PNG_BAD_CHUNK_SIZE); uint32_t v = buf[3] + ((uint32_t)buf[2] << 8) + ((uint32_t)buf[1] << 16) + ((uint32_t)buf[0] << 24); return (int64_t)v; diff --git a/transcoder/basisu.h b/transcoder/basisu.h index 1230b59e..4d398e85 100644 --- a/transcoder/basisu.h +++ b/transcoder/basisu.h @@ -402,13 +402,15 @@ namespace basisu cBC3, // DXT5 (BC4/DXT5A block followed by a BC1/DXT1 block) cBC4, // DXT5A cBC5, // 3DC/DXN (two BC4/DXT5A blocks) - cBC7, + cBC7_ALT, + cBC7_RGBA, cASTC4x4, // LDR only cPVRTC1_4_RGB, cPVRTC1_4_RGBA, cATC_RGB, cATC_RGBA_INTERPOLATED_ALPHA, cFXT1_RGB, + cPVRTC2_4_RGB, cPVRTC2_4_RGBA, cETC2_R11_EAC, cETC2_RG11_EAC, diff --git a/transcoder/basisu_transcoder.cpp b/transcoder/basisu_transcoder.cpp index 3aeba0ee..f3a6596d 100644 --- a/transcoder/basisu_transcoder.cpp +++ b/transcoder/basisu_transcoder.cpp @@ -10840,7 +10840,7 @@ namespace basist case transcoder_texture_format::cTFPVRTC1_4_RGB: return "PVRTC1_4_RGB"; case transcoder_texture_format::cTFPVRTC1_4_RGBA: return "PVRTC1_4_RGBA"; case transcoder_texture_format::cTFBC7_RGBA: return "BC7_RGBA"; - case transcoder_texture_format::cTFBC7_ALT: return "BC7_RGBA"; + case transcoder_texture_format::cTFBC7_ALT: return "BC7_ALT"; case transcoder_texture_format::cTFETC2_RGBA: return "ETC2_RGBA"; case transcoder_texture_format::cTFBC3_RGBA: return "BC3_RGBA"; case transcoder_texture_format::cTFBC5_RG: return "BC5_RG"; @@ -10942,8 +10942,8 @@ namespace basist case transcoder_texture_format::cTFBC4_R: return basisu::texture_format::cBC4; case transcoder_texture_format::cTFPVRTC1_4_RGB: return basisu::texture_format::cPVRTC1_4_RGB; case transcoder_texture_format::cTFPVRTC1_4_RGBA: return basisu::texture_format::cPVRTC1_4_RGBA; - case transcoder_texture_format::cTFBC7_RGBA: return basisu::texture_format::cBC7; - case transcoder_texture_format::cTFBC7_ALT: return basisu::texture_format::cBC7; + case transcoder_texture_format::cTFBC7_RGBA: return basisu::texture_format::cBC7_RGBA; + case transcoder_texture_format::cTFBC7_ALT: return basisu::texture_format::cBC7_ALT; case transcoder_texture_format::cTFETC2_RGBA: return basisu::texture_format::cETC2_RGBA; case transcoder_texture_format::cTFBC3_RGBA: return basisu::texture_format::cBC3; case transcoder_texture_format::cTFBC5_RG: return basisu::texture_format::cBC5; @@ -10955,7 +10955,7 @@ namespace basist case transcoder_texture_format::cTFBGR565: return basisu::texture_format::cBGR565; case transcoder_texture_format::cTFRGBA4444: return basisu::texture_format::cRGBA4444; case transcoder_texture_format::cTFFXT1_RGB: return basisu::texture_format::cFXT1_RGB; - case transcoder_texture_format::cTFPVRTC2_4_RGB: return basisu::texture_format::cPVRTC2_4_RGBA; + case transcoder_texture_format::cTFPVRTC2_4_RGB: return basisu::texture_format::cPVRTC2_4_RGB; case transcoder_texture_format::cTFPVRTC2_4_RGBA: return basisu::texture_format::cPVRTC2_4_RGBA; case transcoder_texture_format::cTFETC2_EAC_R11: return basisu::texture_format::cETC2_R11_EAC; case transcoder_texture_format::cTFETC2_EAC_RG11: return basisu::texture_format::cETC2_RG11_EAC; diff --git a/transcoder/basisu_transcoder_internal.h b/transcoder/basisu_transcoder_internal.h index 776a9986..54e8b2b6 100644 --- a/transcoder/basisu_transcoder_internal.h +++ b/transcoder/basisu_transcoder_internal.h @@ -113,9 +113,7 @@ namespace basist friend class bitwise_decoder; public: - huffman_decoding_table() - { - } + huffman_decoding_table() = default; void clear() {