diff --git a/basisu_backend.cpp b/basisu_backend.cpp index ab5d0dc..665320c 100644 --- a/basisu_backend.cpp +++ b/basisu_backend.cpp @@ -27,7 +27,7 @@ namespace basisu { if (!condition) { - fprintf(stderr, "basisu_backend: verify() failed at line %i!\n", line); + fprintf(stderr, "ERROR: basisu_backend: verify() failed at line %i!\n", line); abort(); } } @@ -179,7 +179,7 @@ namespace basisu { basisu_frontend &r = *m_pFront_end; - if (total_block_endpoints_remapped) + if ((total_block_endpoints_remapped) && (m_params.m_compression_level > 0)) { // We're changed the block endpoint indices, so we need to go and adjust the endpoint codebook (remove unused entries, optimize existing entries that have changed) uint_vec new_block_endpoints(get_total_blocks()); @@ -251,6 +251,10 @@ namespace basisu for (uint32_t i = 1; i < r.get_total_selector_clusters(); i++) remaining_selectors.push_back(i); + uint_vec selector_palette_bytes(m_selector_palette.size()); + for (uint32_t i = 0; i < m_selector_palette.size(); i++) + selector_palette_bytes[i] = m_selector_palette[i].get_byte(0) | (m_selector_palette[i].get_byte(1) << 8) |(m_selector_palette[i].get_byte(2) << 16) |(m_selector_palette[i].get_byte(3) << 24); + // This is the traveling salesman problem. for (uint32_t i = 1; i < r.get_total_selector_clusters(); i++) { @@ -266,12 +270,9 @@ namespace basisu { int selector_index = remaining_selectors[j]; - uint32_t hamming_dist = - g_hamming_dist[m_selector_palette[prev_selector_index].get_byte(0) ^ m_selector_palette[selector_index].get_byte(0)] + - g_hamming_dist[m_selector_palette[prev_selector_index].get_byte(1) ^ m_selector_palette[selector_index].get_byte(1)] + - g_hamming_dist[m_selector_palette[prev_selector_index].get_byte(2) ^ m_selector_palette[selector_index].get_byte(2)] + - g_hamming_dist[m_selector_palette[prev_selector_index].get_byte(3) ^ m_selector_palette[selector_index].get_byte(3)]; - + uint32_t k = selector_palette_bytes[prev_selector_index] ^ selector_palette_bytes[selector_index]; + uint32_t hamming_dist = g_hamming_dist[k & 0xFF] + g_hamming_dist[(k >> 8) & 0xFF] + g_hamming_dist[(k >> 16) & 0xFF] + g_hamming_dist[k >> 24]; + if (hamming_dist < best_hamming_dist) { best_hamming_dist = hamming_dist; @@ -923,7 +924,7 @@ namespace basisu total_selector_indices_remapped, total_selector_indices_remapped * 100.0f / get_total_blocks(), total_used_selector_history_buf, total_used_selector_history_buf * 100.0f / get_total_blocks()); - if (total_endpoint_indices_remapped) + if ((total_endpoint_indices_remapped) && (m_params.m_compression_level > 0)) { int_vec unused; r.reoptimize_remapped_endpoints(block_endpoint_indices, unused, false, &block_selector_indices); diff --git a/basisu_backend.h b/basisu_backend.h index 3aeec1a..cd1f020 100644 --- a/basisu_backend.h +++ b/basisu_backend.h @@ -75,6 +75,7 @@ namespace basisu bool m_debug, m_debug_images; float m_endpoint_rdo_quality_thresh; float m_selector_rdo_quality_thresh; + uint32_t m_compression_level; bool m_use_global_sel_codebook; uint32_t m_global_sel_codebook_pal_bits; @@ -93,6 +94,7 @@ namespace basisu m_debug_images = false; m_endpoint_rdo_quality_thresh = 0.0f; m_selector_rdo_quality_thresh = 0.0f; + m_compression_level = 0; m_use_global_sel_codebook = false; m_global_sel_codebook_pal_bits = ETC1_GLOBAL_SELECTOR_CODEBOOK_MAX_PAL_BITS; diff --git a/basisu_comp.cpp b/basisu_comp.cpp index 7e7bf91..7826a42 100644 --- a/basisu_comp.cpp +++ b/basisu_comp.cpp @@ -57,14 +57,13 @@ namespace basisu PRINT_BOOL_VALUE(m_debug_images); PRINT_BOOL_VALUE(m_global_sel_pal); PRINT_BOOL_VALUE(m_no_auto_global_sel_pal); - PRINT_BOOL_VALUE(m_no_endpoint_refinement); + PRINT_BOOL_VALUE(m_compression_level); PRINT_BOOL_VALUE(m_no_hybrid_sel_cb); PRINT_BOOL_VALUE(m_perceptual); PRINT_BOOL_VALUE(m_no_endpoint_rdo); PRINT_BOOL_VALUE(m_no_selector_rdo); PRINT_BOOL_VALUE(m_read_source_images); PRINT_BOOL_VALUE(m_write_output_basis_files); - PRINT_BOOL_VALUE(m_faster); PRINT_BOOL_VALUE(m_compute_stats); PRINT_BOOL_VALUE(m_check_for_alpha) PRINT_BOOL_VALUE(m_force_alpha) @@ -776,11 +775,10 @@ namespace basisu p.m_max_endpoint_clusters = endpoint_clusters; p.m_max_selector_clusters = selector_clusters; p.m_perceptual = m_params.m_perceptual; - p.m_endpoint_refinement = !m_params.m_no_endpoint_refinement; p.m_debug_stats = m_params.m_debug; p.m_debug_images = m_params.m_debug_images; - p.m_faster = m_params.m_faster; - + p.m_compression_level = m_params.m_compression_level; + if ((m_params.m_global_sel_pal) || (m_auto_global_sel_pal)) { p.m_pGlobal_sel_codebook = m_params.m_pSel_codebook; @@ -874,6 +872,7 @@ namespace basisu backend_params.m_debug = m_params.m_debug; backend_params.m_debug_images = m_params.m_debug_images; backend_params.m_etc1s = true; + backend_params.m_compression_level = m_params.m_compression_level; if (!m_params.m_no_endpoint_rdo) backend_params.m_endpoint_rdo_quality_thresh = m_params.m_endpoint_rdo_thresh; @@ -1077,24 +1076,7 @@ namespace basisu // TODO: We used to output SSIM (during heavy encoder development), but this slowed down compression too much. We'll be adding it back. image_metrics em; - - // ---- Nearly best possible ETC1S stats - em.calc(m_slice_images[slice_index], m_best_etc1s_images_unpacked[slice_index], 0, 0); - em.print("Unquantized ETC1S 709 Luma: "); - - s.m_best_luma_709_psnr = static_cast(em.m_psnr); - s.m_best_luma_709_ssim = static_cast(em.m_ssim); - - em.calc(m_slice_images[slice_index], m_best_etc1s_images_unpacked[slice_index], 0, 0, true, true); - em.print("Unquantized ETC1S 601 Luma: "); - - s.m_best_luma_601_psnr = static_cast(em.m_psnr); - - em.calc(m_slice_images[slice_index], m_best_etc1s_images_unpacked[slice_index], 0, 3); - em.print("Unquantized ETC1S RGB Avg: "); - - s.m_best_rgb_avg_psnr = static_cast(em.m_psnr); - + // ---- .basis ETC1S stats em.calc(m_slice_images[slice_index], m_decoded_output_textures_unpacked[slice_index], 0, 0); em.print(".basis ETC1S 709 Luma: "); @@ -1133,6 +1115,23 @@ namespace basisu em.print(".basis BC1 RGB Avg: "); s.m_basis_bc1_rgb_avg_psnr = static_cast(em.m_psnr); + + // ---- Nearly best possible ETC1S stats + em.calc(m_slice_images[slice_index], m_best_etc1s_images_unpacked[slice_index], 0, 0); + em.print("Unquantized ETC1S 709 Luma: "); + + s.m_best_luma_709_psnr = static_cast(em.m_psnr); + s.m_best_luma_709_ssim = static_cast(em.m_ssim); + + em.calc(m_slice_images[slice_index], m_best_etc1s_images_unpacked[slice_index], 0, 0, true, true); + em.print("Unquantized ETC1S 601 Luma: "); + + s.m_best_luma_601_psnr = static_cast(em.m_psnr); + + em.calc(m_slice_images[slice_index], m_best_etc1s_images_unpacked[slice_index], 0, 3); + em.print("Unquantized ETC1S RGB Avg: "); + + s.m_best_rgb_avg_psnr = static_cast(em.m_psnr); } if (m_frontend.get_params().m_debug_images) diff --git a/basisu_comp.h b/basisu_comp.h index 839e5c7..415335e 100644 --- a/basisu_comp.h +++ b/basisu_comp.h @@ -185,7 +185,8 @@ namespace basisu m_max_selector_clusters(512), m_quality_level(-1), m_mip_scale(1.0f, .000125f, 4.0f), - m_mip_smallest_dimension(1, 1, 16384) + m_mip_smallest_dimension(1, 1, 16384), + m_compression_level((int)BASISU_DEFAULT_COMPRESSION_LEVEL, 0, (int)BASISU_MAX_COMPRESSION_LEVEL) { clear(); } @@ -206,14 +207,13 @@ namespace basisu m_debug_images.clear(); m_global_sel_pal.clear(); m_no_auto_global_sel_pal.clear(); - m_no_endpoint_refinement.clear(); m_no_hybrid_sel_cb.clear(); m_perceptual.clear(); m_no_selector_rdo.clear(); m_selector_rdo_thresh.clear(); m_read_source_images.clear(); m_write_output_basis_files.clear(); - m_faster.clear(); + m_compression_level.clear(); m_compute_stats.clear(); m_check_for_alpha.clear(); m_force_alpha.clear(); @@ -270,11 +270,13 @@ namespace basisu // m_debug_images is pretty slow bool_param m_debug_images; + // Compression level, from 0 to BASISU_MAX_COMPRESSION_LEVEL (higher is slower) + param m_compression_level; + bool_param m_global_sel_pal; bool_param m_no_auto_global_sel_pal; // Frontend/backend codec parameters - bool_param m_no_endpoint_refinement; bool_param m_no_hybrid_sel_cb; // Use perceptual sRGB colorspace metrics (for normal maps, etc.) @@ -292,10 +294,7 @@ namespace basisu // Write the output basis file to disk using m_out_filename bool_param m_write_output_basis_files; - - // If true, the compressor disables some optional but slower refinement stages - bool_param m_faster; - + // Compute and display image metrics bool_param m_compute_stats; diff --git a/basisu_enc.h b/basisu_enc.h index b723c5a..0a9e721 100644 --- a/basisu_enc.h +++ b/basisu_enc.h @@ -870,7 +870,8 @@ namespace basisu typedef std::pair training_vec_with_weight; typedef std::vector< training_vec_with_weight > array_of_weighted_training_vecs; - tree_vector_quant() + tree_vector_quant() : + m_next_codebook_index(0) { } @@ -878,6 +879,7 @@ namespace basisu { clear_vector(m_training_vecs); clear_vector(m_nodes); + m_next_codebook_index = 0; } void add_training_vec(const TrainingVectorType &v, uint32_t weight) { m_training_vecs.push_back(std::make_pair(v, weight)); } @@ -908,11 +910,45 @@ namespace basisu } } + void retrieve(uint32_t max_clusters, std::vector &codebook) const + { + uint_vec node_stack; + node_stack.reserve(512); + + codebook.resize(0); + codebook.reserve(max_clusters); + + uint32_t node_index = 0; + + while (true) + { + const tsvq_node& cur = m_nodes[node_index]; + + if (cur.is_leaf() || ((2 + cur.m_codebook_index) > (int)max_clusters)) + { + codebook.resize(codebook.size() + 1); + codebook.back() = cur.m_training_vecs; + + if (node_stack.empty()) + break; + + node_index = node_stack.back(); + node_stack.pop_back(); + continue; + } + + node_stack.push_back(cur.m_right_index); + node_index = cur.m_left_index; + } + } + bool generate(uint32_t max_size) { if (!m_training_vecs.size()) return false; + m_next_codebook_index = 0; + clear_vector(m_nodes); m_nodes.reserve(max_size * 2 + 1); @@ -956,7 +992,7 @@ namespace basisu class tsvq_node { public: - inline tsvq_node() : m_weight(0), m_origin(cZero), m_left_index(-1), m_right_index(-1) { } + inline tsvq_node() : m_weight(0), m_origin(cZero), m_left_index(-1), m_right_index(-1), m_codebook_index(-1) { } // vecs is erased inline void set(const TrainingVectorType &org, uint64_t weight, float var, std::vector &vecs) { m_origin = org; m_weight = weight; m_var = var; m_training_vecs.swap(vecs); } @@ -968,6 +1004,7 @@ namespace basisu TrainingVectorType m_origin; int32_t m_left_index, m_right_index; std::vector m_training_vecs; + int m_codebook_index; }; typedef std::vector tsvq_node_vec; @@ -975,6 +1012,8 @@ namespace basisu array_of_weighted_training_vecs m_training_vecs; + uint32_t m_next_codebook_index; + tsvq_node prepare_root() const { double ttsum = 0.0f; @@ -1021,6 +1060,9 @@ namespace basisu m_nodes[node_index].m_left_index = l_child_index; m_nodes[node_index].m_right_index = r_child_index; + + m_nodes[node_index].m_codebook_index = m_next_codebook_index; + m_next_codebook_index++; m_nodes.resize(m_nodes.size() + 2); diff --git a/basisu_etc.h b/basisu_etc.h index 9e4a104..398dcfa 100644 --- a/basisu_etc.h +++ b/basisu_etc.h @@ -470,6 +470,27 @@ namespace basisu return dc; } + void get_block_color(color_rgba& color, uint32_t subblock_index, uint32_t selector_index) const + { + color_rgba b; + + if (get_diff_bit()) + { + if (subblock_index) + unpack_color5(b, get_base5_color(), get_delta3_color(), true); + else + unpack_color5(b, get_base5_color(), true); + } + else + { + b = unpack_color4(get_base4_color(subblock_index), true); + } + + const int* pInten_table = g_etc1_inten_tables[get_inten_table(subblock_index)]; + + color.set(clamp255(b.r + pInten_table[selector_index]), clamp255(b.g + pInten_table[selector_index]), clamp255(b.b + pInten_table[selector_index]), 255); + } + bool get_block_low_high_colors(color_rgba* pBlock_colors, uint32_t subblock_index) const { color_rgba b; diff --git a/basisu_frontend.cpp b/basisu_frontend.cpp index 3d4f22d..168f58d 100644 --- a/basisu_frontend.cpp +++ b/basisu_frontend.cpp @@ -20,6 +20,7 @@ #include "transcoder/basisu.h" #include "basisu_frontend.h" #include +#include #define BASISU_FRONTEND_VERIFY(c) verify(c, __LINE__); @@ -28,12 +29,15 @@ namespace basisu const uint32_t BASISU_MAX_ENDPOINT_REFINEMENT_STEPS = 3; const uint32_t BASISU_MAX_SELECTOR_REFINEMENT_STEPS = 3; + const uint32_t BASISU_ENDPOINT_PARENT_CODEBOOK_SIZE = 16; + const uint32_t BASISU_SELECTOR_PARENT_CODEBOOK_SIZE = 16; + // TODO - How to handle internal verifies in the basisu lib static inline void verify(bool condition, int line) { if (!condition) { - fprintf(stderr, "basisu_frontend: verify check failed at line %i!\n", line); + fprintf(stderr, "ERROR: basisu_frontend: verify check failed at line %i!\n", line); abort(); } } @@ -49,15 +53,15 @@ namespace basisu } } - debug_printf("basisu_frontend::init: NumEndpointClusters: %u, NumSelectorClusters: %u, EndpointRefinement: %u, Perceptual: %u, Faster: %u\n", - p.m_max_endpoint_clusters, p.m_max_selector_clusters, p.m_endpoint_refinement, p.m_perceptual, p.m_faster); + debug_printf("basisu_frontend::init: NumEndpointClusters: %u, NumSelectorClusters: %u, Perceptual: %u, CompressionLevel: %u\n", + p.m_max_endpoint_clusters, p.m_max_selector_clusters, p.m_perceptual, p.m_compression_level); debug_printf("Global sel codebook pal bits: %u, Global sel codebook mod bits: %u, Use hybrid selector codebook: %u, Hybrid codebook quality thresh: %f\n", p.m_num_global_sel_codebook_pal_bits, p.m_num_global_sel_codebook_mod_bits, p.m_use_hybrid_selector_codebooks, p.m_hybrid_codebook_quality_thresh); - + if ((p.m_max_endpoint_clusters < 1) || (p.m_max_endpoint_clusters > cMaxEndpointClusters)) return false; if ((p.m_max_selector_clusters < 1) || (p.m_max_selector_clusters > cMaxSelectorClusters)) @@ -70,6 +74,65 @@ namespace basisu m_encoded_blocks.resize(m_params.m_num_source_blocks); memset(&m_encoded_blocks[0], 0, m_encoded_blocks.size() * sizeof(m_encoded_blocks[0])); + + m_num_endpoint_codebook_iterations = 1; + m_num_selector_codebook_iterations = 1; + + switch (p.m_compression_level) + { + case 0: + { + m_endpoint_refinement = false; + m_use_hierarchical_endpoint_codebooks = true; + m_use_hierarchical_selector_codebooks = true; + break; + } + case 1: + { + m_endpoint_refinement = true; + m_use_hierarchical_endpoint_codebooks = true; + m_use_hierarchical_selector_codebooks = true; + break; + } + case 2: + { + m_endpoint_refinement = true; + m_use_hierarchical_endpoint_codebooks = false; + m_use_hierarchical_selector_codebooks = false; + break; + } + case 3: + { + m_endpoint_refinement = true; + m_use_hierarchical_endpoint_codebooks = true; + m_use_hierarchical_selector_codebooks = true; + m_num_endpoint_codebook_iterations = BASISU_MAX_ENDPOINT_REFINEMENT_STEPS; + m_num_selector_codebook_iterations = BASISU_MAX_ENDPOINT_REFINEMENT_STEPS; + break; + } + case 4: + { + m_endpoint_refinement = true; + m_use_hierarchical_endpoint_codebooks = false; + m_use_hierarchical_selector_codebooks = false; + m_num_endpoint_codebook_iterations = BASISU_MAX_ENDPOINT_REFINEMENT_STEPS; + m_num_selector_codebook_iterations = BASISU_MAX_ENDPOINT_REFINEMENT_STEPS; + break; + } + case 5: + { + m_endpoint_refinement = true; + m_use_hierarchical_endpoint_codebooks = false; + m_use_hierarchical_selector_codebooks = false; + m_num_endpoint_codebook_iterations = BASISU_MAX_ENDPOINT_REFINEMENT_STEPS*2; + m_num_selector_codebook_iterations = BASISU_MAX_ENDPOINT_REFINEMENT_STEPS*2; + break; + } + + } + + debug_printf("Endpoint refinement: %u, Hierarchical endpoint codebooks: %u, Hierarchical selector codebooks: %u, Endpoint codebook iters: %u, Selector codebook iters: %u\n", + m_endpoint_refinement, m_use_hierarchical_endpoint_codebooks, m_use_hierarchical_selector_codebooks, m_num_endpoint_codebook_iterations, m_num_selector_codebook_iterations); return true; } @@ -86,10 +149,8 @@ namespace basisu init_endpoint_training_vectors(); generate_endpoint_clusters(); - - const uint32_t num_refine_endpoint_steps = m_params.m_endpoint_refinement ? (m_params.m_faster ? 1 : BASISU_MAX_ENDPOINT_REFINEMENT_STEPS) : 1; - - for (uint32_t refine_endpoint_step = 0; refine_endpoint_step < num_refine_endpoint_steps; refine_endpoint_step++) + + for (uint32_t refine_endpoint_step = 0; refine_endpoint_step < m_num_endpoint_codebook_iterations; refine_endpoint_step++) { BASISU_FRONTEND_VERIFY(check_etc1s_constraints()); @@ -113,7 +174,7 @@ namespace basisu bool early_out = false; - if (m_params.m_endpoint_refinement) + if (m_endpoint_refinement) { //dump_endpoint_clusterization_visualization("endpoint_clusters_before_refinement.png"); @@ -149,17 +210,29 @@ namespace basisu create_selector_clusters(); - const uint32_t num_refine_selector_steps = m_params.m_pGlobal_sel_codebook ? 1 : (m_params.m_faster ? 1 : BASISU_MAX_SELECTOR_REFINEMENT_STEPS); - for (uint32_t refine_selector_steps = 0; refine_selector_steps < num_refine_selector_steps; refine_selector_steps++) + if (m_use_hierarchical_selector_codebooks) + compute_selector_clusters_within_each_parent_cluster(); + + if (m_params.m_compression_level == 0) { - create_optimized_selector_codebook(refine_selector_steps); + create_optimized_selector_codebook(0); find_optimal_selector_clusters_for_each_block(); - - if (!m_params.m_faster) + } + else + { + const uint32_t num_refine_selector_steps = m_params.m_pGlobal_sel_codebook ? 1 : m_num_selector_codebook_iterations; + for (uint32_t refine_selector_steps = 0; refine_selector_steps < num_refine_selector_steps; refine_selector_steps++) { - if (!refine_block_endpoints_given_selectors()) - break; + create_optimized_selector_codebook(refine_selector_steps); + + find_optimal_selector_clusters_for_each_block(); + + if (m_params.m_compression_level >= 3) + { + if (!refine_block_endpoints_given_selectors()) + break; + } } } @@ -195,6 +268,8 @@ namespace basisu int_vec new_to_old; uint32_t total_new_entries = 0; + std::unordered_map selector_hashmap; + for (int i = 0; i < static_cast(m_optimized_cluster_selectors.size()); i++) { if (!selector_cluster_was_used[i]) @@ -203,23 +278,25 @@ namespace basisu continue; } - int j; - for (j = 0; j < i; j++) - { - if (m_optimized_cluster_selectors[i].get_raw_selector_bits() == m_optimized_cluster_selectors[j].get_raw_selector_bits()) - break; - } + const uint32_t raw_selector_bits = m_optimized_cluster_selectors[i].get_raw_selector_bits(); - if (j < i) + auto find_res = selector_hashmap.insert(std::make_pair(raw_selector_bits, total_new_entries)); + if (!find_res.second) { - old_to_new[i] = old_to_new[j]; + old_to_new[i] = (find_res.first)->second; continue; } - + old_to_new[i] = total_new_entries++; new_to_old.push_back(i); } + for (uint32_t i = 0; i < m_block_selector_cluster_index.size(); i++) + { + BASISU_FRONTEND_VERIFY((old_to_new[m_block_selector_cluster_index[i]] >= 0) && (old_to_new[m_block_selector_cluster_index[i]] < (int)total_new_entries)); + m_block_selector_cluster_index[i] = old_to_new[m_block_selector_cluster_index[i]]; + } + std::vector new_optimized_cluster_selectors(m_optimized_cluster_selectors.size() ? total_new_entries : 0); basist::etc1_global_selector_codebook_entry_id_vec new_optimized_cluster_selector_global_cb_ids(m_optimized_cluster_selector_global_cb_ids.size() ? total_new_entries : 0); std::vector new_selector_cluster_indices(m_selector_cluster_indices.size() ? total_new_entries : 0); @@ -244,10 +321,7 @@ namespace basisu m_optimized_cluster_selector_global_cb_ids.swap(new_optimized_cluster_selector_global_cb_ids); m_selector_cluster_indices.swap(new_selector_cluster_indices); m_selector_cluster_uses_global_cb.swap(new_selector_cluster_uses_global_cb); - - for (uint32_t i = 0; i < m_block_selector_cluster_index.size(); i++) - m_block_selector_cluster_index[i] = old_to_new[m_block_selector_cluster_index[i]]; - + debug_printf("optimize_selector_codebook: Before: %u After: %u\n", orig_total_selector_clusters, total_new_entries); } @@ -256,7 +330,6 @@ namespace basisu debug_printf("basisu_frontend::init_etc1_images\n"); m_etc1_blocks_etc1s.resize(m_total_blocks); - m_etc1_blocks_etc1s_unpacked.resize(m_total_blocks); #pragma omp parallel for for (int block_index = 0; block_index < static_cast(m_total_blocks); block_index++) @@ -266,6 +339,11 @@ namespace basisu etc1_optimizer optimizer; etc1_optimizer::params optimizer_params; etc1_optimizer::results optimizer_results; + + if (m_params.m_compression_level == 0) + optimizer_params.m_quality = cETCQualityFast; + else if (m_params.m_compression_level == BASISU_MAX_COMPRESSION_LEVEL) + optimizer_params.m_quality = cETCQualityUber; optimizer_params.m_num_src_pixels = 16; optimizer_params.m_pSrc_pixels = source_blk.get_ptr(); @@ -280,6 +358,7 @@ namespace basisu etc_block &blk = m_etc1_blocks_etc1s[block_index]; + memset(&blk, 0, sizeof(blk)); blk.set_block_color5_etc1s(optimizer_results.m_block_color_unscaled); blk.set_inten_tables_etc1s(optimizer_results.m_block_inten_table); blk.set_flip_bit(true); @@ -287,8 +366,6 @@ namespace basisu for (uint32_t y = 0; y < 4; y++) for (uint32_t x = 0; x < 4; x++) blk.set_selector(x, y, selectors[x + y * 4]); - - unpack_etc1(m_etc1_blocks_etc1s[block_index], m_etc1_blocks_etc1s_unpacked[block_index].get_ptr()); } } @@ -325,8 +402,58 @@ namespace basisu m_endpoint_clusterizer.retrieve(m_endpoint_clusters); + if (m_use_hierarchical_endpoint_codebooks) + { + if (m_endpoint_clusters.size() < BASISU_ENDPOINT_PARENT_CODEBOOK_SIZE) + { + m_endpoint_parent_clusters.resize(0); + m_endpoint_parent_clusters.resize(1); + for (uint32_t i = 0; i < m_total_blocks; i++) + { + m_endpoint_parent_clusters[0].push_back(i*2); + m_endpoint_parent_clusters[0].push_back(i*2+1); + } + } + else + m_endpoint_clusterizer.retrieve(BASISU_ENDPOINT_PARENT_CODEBOOK_SIZE, m_endpoint_parent_clusters); + + BASISU_ASSUME(BASISU_ENDPOINT_PARENT_CODEBOOK_SIZE <= UINT8_MAX); + + m_block_parent_endpoint_cluster.resize(0); + m_block_parent_endpoint_cluster.resize(m_total_blocks); + for (uint32_t parent_cluster_index = 0; parent_cluster_index < m_endpoint_parent_clusters.size(); parent_cluster_index++) + { + const uint_vec &cluster = m_endpoint_parent_clusters[parent_cluster_index]; + for (uint32_t j = 0; j < cluster.size(); j++) + { + const uint32_t block_index = cluster[j] >> 1; + m_block_parent_endpoint_cluster[block_index] = static_cast(parent_cluster_index); + } + } + + // Ensure that all the blocks within each cluster are all in the same parent cluster, or something is very wrong. + for (uint32_t cluster_index = 0; cluster_index < m_endpoint_clusters.size(); cluster_index++) + { + const uint_vec &cluster = m_endpoint_clusters[cluster_index]; + + uint32_t parent_cluster_index = 0; + for (uint32_t j = 0; j < cluster.size(); j++) + { + const uint32_t block_index = cluster[j] >> 1; + if (!j) + { + parent_cluster_index = m_block_parent_endpoint_cluster[block_index]; + } + else + { + BASISU_FRONTEND_VERIFY(m_block_parent_endpoint_cluster[block_index] == parent_cluster_index); + } + } + } + } + if (m_params.m_debug_stats) - debug_printf("Total endpoint clusters: %u\n", (uint32_t)m_endpoint_clusters.size()); + debug_printf("Total endpoint clusters: %u, parent clusters: %u\n", (uint32_t)m_endpoint_clusters.size(), (uint32_t)m_endpoint_parent_clusters.size()); } void basisu_frontend::generate_block_endpoint_clusters() @@ -355,6 +482,34 @@ namespace basisu } } + void basisu_frontend::compute_endpoint_clusters_within_each_parent_cluster() + { + generate_block_endpoint_clusters(); + + m_endpoint_clusters_within_each_parent_cluster.resize(0); + m_endpoint_clusters_within_each_parent_cluster.resize(m_endpoint_parent_clusters.size()); + + for (uint32_t block_index = 0; block_index < m_total_blocks; block_index++) + { + const uint32_t cluster_index = m_block_endpoint_clusters_indices[block_index][0]; + const uint32_t parent_cluster_index = m_block_parent_endpoint_cluster[block_index]; + + m_endpoint_clusters_within_each_parent_cluster[parent_cluster_index].push_back(cluster_index); + } + + for (uint32_t i = 0; i < m_endpoint_clusters_within_each_parent_cluster.size(); i++) + { + uint_vec &cluster_indices = m_endpoint_clusters_within_each_parent_cluster[i]; + + BASISU_FRONTEND_VERIFY(cluster_indices.size()); + + vector_sort(cluster_indices); + + auto last = std::unique(cluster_indices.begin(), cluster_indices.end()); + cluster_indices.erase(last, cluster_indices.end()); + } + } + void basisu_frontend::compute_endpoint_subblock_error_vec() { m_subblock_endpoint_quant_err_vec.resize(0); @@ -456,7 +611,6 @@ namespace basisu break; subblock_endpoint_quant_err subblock_to_move(m_subblock_endpoint_quant_err_vec.back()); - const bool is_etc1s_block = true; m_subblock_endpoint_quant_err_vec.pop_back(); @@ -465,25 +619,16 @@ namespace basisu uint32_t training_vector_index = subblock_to_move.m_block_index * 2 + subblock_to_move.m_subblock_index; - if (is_etc1s_block) - { - if (cluster_sizes[subblock_to_move.m_cluster_index] <= 2) - continue; - } - else - { - if (cluster_sizes[subblock_to_move.m_cluster_index] <= 1) - continue; - } + if (cluster_sizes[subblock_to_move.m_cluster_index] <= 2) + continue; if (unordered_set_contains(training_vector_was_relocated, training_vector_index)) continue; - if (is_etc1s_block) - { - if (unordered_set_contains(training_vector_was_relocated, training_vector_index ^ 1)) - continue; - } + if (unordered_set_contains(training_vector_was_relocated, training_vector_index ^ 1)) + continue; + + const uint32_t new_endpoint_cluster_index = (uint32_t)m_endpoint_clusters.size(); enlarge_vector(m_endpoint_clusters, 1)->push_back(training_vector_index); enlarge_vector(m_endpoint_cluster_etc_params, 1); @@ -492,20 +637,12 @@ namespace basisu training_vector_was_relocated.insert(training_vector_index); - if (is_etc1s_block) - { - m_endpoint_clusters.back().push_back(training_vector_index ^ 1); - training_vector_was_relocated.insert(training_vector_index ^ 1); - - BASISU_FRONTEND_VERIFY(cluster_sizes[subblock_to_move.m_cluster_index] >= 2); - cluster_sizes[subblock_to_move.m_cluster_index] -= 2; - } - else - { - BASISU_FRONTEND_VERIFY(cluster_sizes[subblock_to_move.m_cluster_index] >= 1); - cluster_sizes[subblock_to_move.m_cluster_index]--; - } + m_endpoint_clusters.back().push_back(training_vector_index ^ 1); + training_vector_was_relocated.insert(training_vector_index ^ 1); + BASISU_FRONTEND_VERIFY(cluster_sizes[subblock_to_move.m_cluster_index] >= 2); + cluster_sizes[subblock_to_move.m_cluster_index] -= 2; + ignore_cluster.insert(subblock_to_move.m_cluster_index); num_new_endpoint_clusters--; @@ -534,6 +671,8 @@ namespace basisu generate_block_endpoint_clusters(); } + // Given each endpoint cluster, gather all the block pixels which are in that cluster and compute optimized ETC1S endpoints for them. + // TODO: Don't optimize endpoint clusters which haven't changed. void basisu_frontend::generate_endpoint_codebook(uint32_t step) { debug_printf("generate_endpoint_codebook\n"); @@ -581,6 +720,11 @@ namespace basisu cluster_optimizer_params.m_use_color4 = false; cluster_optimizer_params.m_perceptual = m_params.m_perceptual; + if (m_params.m_compression_level == 0) + cluster_optimizer_params.m_quality = cETCQualityMedium; + else if (m_params.m_compression_level == BASISU_MAX_COMPRESSION_LEVEL) + cluster_optimizer_params.m_quality = cETCQualityUber; + etc1_optimizer::results cluster_optimizer_results; std::vector cluster_selectors(total_pixels); @@ -684,6 +828,9 @@ namespace basisu uint32_t basisu_frontend::refine_endpoint_clusterization() { debug_printf("refine_endpoint_clusterization\n"); + + if (m_use_hierarchical_endpoint_codebooks) + compute_endpoint_clusters_within_each_parent_cluster(); std::vector block_clusters(m_total_blocks); @@ -700,7 +847,7 @@ namespace basisu } // cluster_indices_iter } - + //---------------------------------------------------------- // Create a new endpoint clusterization @@ -720,9 +867,16 @@ namespace basisu uint64_t best_cluster_err = UINT64_MAX; uint32_t best_cluster_index = 0; - - for (uint32_t cluster_iter = 0; cluster_iter < m_endpoint_clusters.size(); cluster_iter++) + + const uint32_t block_parent_endpoint_cluster_index = m_block_parent_endpoint_cluster.size() ? m_block_parent_endpoint_cluster[block_index] : 0; + const uint_vec *pCluster_indices = m_endpoint_clusters_within_each_parent_cluster.size() ? &m_endpoint_clusters_within_each_parent_cluster[block_parent_endpoint_cluster_index] : nullptr; + + const uint32_t total_clusters = m_use_hierarchical_endpoint_codebooks ? (uint32_t)pCluster_indices->size() : (uint32_t)m_endpoint_clusters.size(); + + for (uint32_t i = 0; i < total_clusters; i++) { + const uint32_t cluster_iter = m_use_hierarchical_endpoint_codebooks ? (*pCluster_indices)[i] : i; + color_rgba cluster_etc_base_color(m_endpoint_cluster_etc_params[cluster_iter].m_color_unscaled[0]); uint32_t cluster_etc_inten = m_endpoint_cluster_etc_params[cluster_iter].m_inten_table[0]; @@ -740,7 +894,6 @@ namespace basisu etc_block::get_block_colors5(subblock_colors, cluster_etc_base_color, cluster_etc_inten); - for (uint32_t p = 0; p < num_subblock_pixels; p++) { uint64_t best_err = UINT64_MAX; @@ -804,6 +957,8 @@ namespace basisu { debug_printf("eliminate_redundant_or_empty_endpoint_clusters\n"); + // Step 1: Sort endpoint clusters by the base colors/intens + uint_vec sorted_endpoint_cluster_indices(m_endpoint_clusters.size()); for (uint32_t i = 0; i < m_endpoint_clusters.size(); i++) sorted_endpoint_cluster_indices[i] = i; @@ -812,6 +967,7 @@ namespace basisu std::vector > new_endpoint_clusters(m_endpoint_clusters.size()); std::vector new_subblock_etc_params(m_endpoint_clusters.size()); + for (uint32_t i = 0; i < m_endpoint_clusters.size(); i++) { uint32_t j = sorted_endpoint_cluster_indices[i]; @@ -822,9 +978,11 @@ namespace basisu new_endpoint_clusters.swap(m_endpoint_clusters); new_subblock_etc_params.swap(m_endpoint_cluster_etc_params); + // Step 2: Eliminate redundant endpoint clusters, or empty endpoint clusters + new_endpoint_clusters.resize(0); new_subblock_etc_params.resize(0); - + for (int i = 0; i < (int)m_endpoint_clusters.size(); ) { if (!m_endpoint_clusters[i].size()) @@ -842,13 +1000,15 @@ namespace basisu new_endpoint_clusters.push_back(m_endpoint_clusters[i]); new_subblock_etc_params.push_back(m_endpoint_cluster_etc_params[i]); - + for (int k = i + 1; k < j; k++) + { append_vector(new_endpoint_clusters.back(), m_endpoint_clusters[k]); + } i = j; } - + if (m_endpoint_clusters.size() != new_endpoint_clusters.size()) { if (m_params.m_debug_stats) @@ -891,6 +1051,48 @@ namespace basisu m_orig_encoded_blocks = m_encoded_blocks; } + void basisu_frontend::compute_selector_clusters_within_each_parent_cluster() + { + uint_vec block_selector_cluster_indices(m_total_blocks); + + for (int cluster_index = 0; cluster_index < static_cast(m_selector_cluster_indices.size()); cluster_index++) + { + const std::vector& cluster_indices = m_selector_cluster_indices[cluster_index]; + + for (uint32_t cluster_indices_iter = 0; cluster_indices_iter < cluster_indices.size(); cluster_indices_iter++) + { + const uint32_t block_index = cluster_indices[cluster_indices_iter]; + + block_selector_cluster_indices[block_index] = cluster_index; + + } // cluster_indices_iter + + } // cluster_index + + m_selector_clusters_within_each_parent_cluster.resize(0); + m_selector_clusters_within_each_parent_cluster.resize(m_selector_parent_cluster_indices.size()); + + for (uint32_t block_index = 0; block_index < m_total_blocks; block_index++) + { + const uint32_t cluster_index = block_selector_cluster_indices[block_index]; + const uint32_t parent_cluster_index = m_block_parent_selector_cluster[block_index]; + + m_selector_clusters_within_each_parent_cluster[parent_cluster_index].push_back(cluster_index); + } + + for (uint32_t i = 0; i < m_selector_clusters_within_each_parent_cluster.size(); i++) + { + uint_vec &cluster_indices = m_selector_clusters_within_each_parent_cluster[i]; + + BASISU_FRONTEND_VERIFY(cluster_indices.size()); + + vector_sort(cluster_indices); + + auto last = std::unique(cluster_indices.begin(), cluster_indices.end()); + cluster_indices.erase(last, cluster_indices.end()); + } + } + void basisu_frontend::create_selector_clusters() { debug_printf("create_selector_clusters\n"); @@ -912,10 +1114,10 @@ namespace basisu const uint32_t subblock_index = (blk.get_inten_table(0) > blk.get_inten_table(1)) ? 0 : 1; - color_rgba block_colors[4]; - blk.get_block_colors(block_colors, subblock_index); + color_rgba block_colors[2]; + blk.get_block_low_high_colors(block_colors, subblock_index); - const uint32_t dist = color_distance(m_params.m_perceptual, block_colors[0], block_colors[3], false); + const uint32_t dist = color_distance(m_params.m_perceptual, block_colors[0], block_colors[1], false); const uint32_t cColorDistToWeight = 300; const uint32_t cMaxWeight = 4096; @@ -932,6 +1134,53 @@ namespace basisu selector_clusterizer.generate(m_params.m_max_selector_clusters); selector_clusterizer.retrieve(m_selector_cluster_indices); + + if (m_use_hierarchical_selector_codebooks) + { + if (m_selector_cluster_indices.size() < BASISU_SELECTOR_PARENT_CODEBOOK_SIZE) + { + m_selector_parent_cluster_indices.resize(0); + m_selector_parent_cluster_indices.resize(1); + for (uint32_t i = 0; i < m_total_blocks; i++) + m_selector_parent_cluster_indices[0].push_back(i); + } + else + selector_clusterizer.retrieve(BASISU_SELECTOR_PARENT_CODEBOOK_SIZE, m_selector_parent_cluster_indices); + + BASISU_ASSUME(BASISU_SELECTOR_PARENT_CODEBOOK_SIZE <= UINT8_MAX); + + m_block_parent_selector_cluster.resize(0); + m_block_parent_selector_cluster.resize(m_total_blocks); + + for (uint32_t parent_cluster_index = 0; parent_cluster_index < m_selector_parent_cluster_indices.size(); parent_cluster_index++) + { + const uint_vec &cluster = m_selector_parent_cluster_indices[parent_cluster_index]; + for (uint32_t j = 0; j < cluster.size(); j++) + m_block_parent_selector_cluster[cluster[j]] = static_cast(parent_cluster_index); + } + + // Ensure that all the blocks within each cluster are all in the same parent cluster, or something is very wrong. + for (uint32_t cluster_index = 0; cluster_index < m_selector_cluster_indices.size(); cluster_index++) + { + const uint_vec &cluster = m_selector_cluster_indices[cluster_index]; + + uint32_t parent_cluster_index = 0; + for (uint32_t j = 0; j < cluster.size(); j++) + { + const uint32_t block_index = cluster[j]; + if (!j) + { + parent_cluster_index = m_block_parent_selector_cluster[block_index]; + } + else + { + BASISU_FRONTEND_VERIFY(m_block_parent_selector_cluster[block_index] == parent_cluster_index); + } + } + } + } + + debug_printf("Total selector clusters: %u, total parent selector clusters: %u\n", (uint32_t)m_selector_cluster_indices.size(), (uint32_t)m_selector_parent_cluster_indices.size()); } void basisu_frontend::create_optimized_selector_codebook(uint32_t iter) @@ -1010,7 +1259,7 @@ namespace basisu uint32_t total_clusters_processed = 0; - // For each selector codebook entry, determine which selector minimizes the error across all the blocks that use that quantized selector. + // For each selector codebook entry, and for each of the 4x4 selectors, determine which selector minimizes the error across all the blocks that use that quantized selector. #pragma omp parallel for for (int cluster_index = 0; cluster_index < static_cast(total_selector_clusters); cluster_index++) @@ -1037,14 +1286,14 @@ namespace basisu { const uint32_t block_index = cluster_block_indices[cluster_block_index]; - etc_block &blk = m_encoded_blocks[block_index]; + const etc_block &blk = m_encoded_blocks[block_index]; const color_rgba &orig_color = get_source_pixel_block(block_index)(x, y); - color_rgba block_colors[4]; - blk.get_block_colors(block_colors, blk.get_subblock_index(x, y)); + color_rgba block_color; + blk.get_block_color(block_color, blk.get_subblock_index(x, y), s); + total_err += color_distance(m_params.m_perceptual, block_color, orig_color, false); - total_err += color_distance(m_params.m_perceptual, block_colors[s], orig_color, false); if (total_err > best_err) break; @@ -1174,76 +1423,92 @@ namespace basisu void basisu_frontend::find_optimal_selector_clusters_for_each_block() { debug_printf("find_optimal_selector_clusters_for_each_block\n"); - + m_block_selector_cluster_index.resize(m_total_blocks); - - const uint32_t total_selector_clusters = (uint32_t)m_selector_cluster_indices.size(); - - std::vector< std::vector > new_cluster_indices; - - // For each block: Determine which quantized selectors best encode that block, given its quantized endpoints. - -#pragma omp parallel for - for (int block_index = 0; block_index < static_cast(m_total_blocks); block_index++) + + if (m_params.m_compression_level == 0) { - const color_rgba* pBlock_pixels = get_source_pixel_block(block_index).get_ptr(); - - etc_block& blk = m_encoded_blocks[block_index]; - - color_rgba trial_block_colors[4]; - blk.get_block_colors(trial_block_colors, 0); - - uint64_t best_cluster_err = UINT64_MAX; - uint32_t best_cluster_index = 0; - - // TODO: One faster algorithm is to sort the cluster selectors, then iterate through them in sorted order and only eval color distances of the selectors that changed. - for (uint32_t cluster_index = 0; cluster_index < total_selector_clusters; cluster_index++) + // Don't do anything, just leave the blocks in their original selector clusters. + for (uint32_t i = 0; i < m_selector_cluster_indices.size(); i++) { - const etc_block& cluster_blk = m_optimized_cluster_selectors[cluster_index]; - - uint64_t trial_err = 0; - for (int y = 0; y < 4; y++) - { - for (int x = 0; x < 4; x++) - { - const uint32_t sel = cluster_blk.get_selector(x, y); - - trial_err += color_distance(m_params.m_perceptual, trial_block_colors[sel], pBlock_pixels[x + y * 4], false); - if (trial_err > best_cluster_err) - goto early_out; - } - } - - if (trial_err < best_cluster_err) - { - best_cluster_err = trial_err; - best_cluster_index = cluster_index; - if (!best_cluster_err) - break; - } - - early_out: - ; - } - - blk.set_raw_selector_bits(m_optimized_cluster_selectors[best_cluster_index].get_raw_selector_bits()); - - m_block_selector_cluster_index[block_index] = best_cluster_index; - -#pragma omp critical - { - vector_ensure_element_is_valid(new_cluster_indices, best_cluster_index); - new_cluster_indices[best_cluster_index].push_back(block_index); + for (uint32_t j = 0; j < m_selector_cluster_indices[i].size(); j++) + m_block_selector_cluster_index[m_selector_cluster_indices[i][j]] = i; } } + else + { + std::vector< std::vector > new_cluster_indices; - m_selector_cluster_indices.swap(new_cluster_indices); + // For each block: Determine which quantized selectors best encode that block, given its quantized endpoints. + + #pragma omp parallel for + for (int block_index = 0; block_index < static_cast(m_total_blocks); block_index++) + { + const color_rgba* pBlock_pixels = get_source_pixel_block(block_index).get_ptr(); + + etc_block& blk = m_encoded_blocks[block_index]; + + color_rgba trial_block_colors[4]; + blk.get_block_colors(trial_block_colors, 0); + + uint64_t best_cluster_err = UINT64_MAX; + uint32_t best_cluster_index = 0; + + const uint32_t parent_selector_cluster = m_block_parent_selector_cluster.size() ? m_block_parent_selector_cluster[block_index] : 0; + const uint_vec *pCluster_indices = m_selector_clusters_within_each_parent_cluster.size() ? &m_selector_clusters_within_each_parent_cluster[parent_selector_cluster] : nullptr; + + const uint32_t total_clusters = m_use_hierarchical_selector_codebooks ? (uint32_t)pCluster_indices->size() : (uint32_t)m_selector_cluster_indices.size(); + + for (uint32_t cluster_iter = 0; cluster_iter < total_clusters; cluster_iter++) + { + const uint32_t cluster_index = m_use_hierarchical_selector_codebooks ? (*pCluster_indices)[cluster_iter] : cluster_iter; + + const etc_block& cluster_blk = m_optimized_cluster_selectors[cluster_index]; + + uint64_t trial_err = 0; + for (int y = 0; y < 4; y++) + { + for (int x = 0; x < 4; x++) + { + const uint32_t sel = cluster_blk.get_selector(x, y); + + trial_err += color_distance(m_params.m_perceptual, trial_block_colors[sel], pBlock_pixels[x + y * 4], false); + if (trial_err > best_cluster_err) + goto early_out; + } + } + + if (trial_err < best_cluster_err) + { + best_cluster_err = trial_err; + best_cluster_index = cluster_index; + if (!best_cluster_err) + break; + } + + early_out: + ; + } + + blk.set_raw_selector_bits(m_optimized_cluster_selectors[best_cluster_index].get_raw_selector_bits()); + + m_block_selector_cluster_index[block_index] = best_cluster_index; + + #pragma omp critical + { + vector_ensure_element_is_valid(new_cluster_indices, best_cluster_index); + new_cluster_indices[best_cluster_index].push_back(block_index); + } + } + + m_selector_cluster_indices.swap(new_cluster_indices); + } for (uint32_t i = 0; i < m_selector_cluster_indices.size(); i++) vector_sort(m_selector_cluster_indices[i]); } - // This seems to be a pretty minor optimization (like .01-.02 dB on kodak) + // TODO: Remove old ETC1 specific stuff, and thread this. uint32_t basisu_frontend::refine_block_endpoints_given_selectors() { debug_printf("refine_block_endpoints_given_selectors\n"); @@ -1576,6 +1841,9 @@ namespace basisu cluster_optimizer_params.m_perceptual = m_params.m_perceptual; cluster_optimizer_params.m_pForce_selectors = &force_selectors[0]; + if (m_params.m_compression_level == BASISU_MAX_COMPRESSION_LEVEL) + cluster_optimizer_params.m_quality = cETCQualityUber; + etc1_optimizer::results cluster_optimizer_results; std::vector cluster_selectors(total_pixels); @@ -1738,7 +2006,8 @@ namespace basisu blk.set_inten_table(1, get_endpoint_cluster_inten_table(endpoint_cluster1_index, false)); const uint32_t selector_cluster_index = get_block_selector_cluster_index(block_index); - + CHECK(selector_cluster_index < get_total_selector_clusters()); + CHECK(vector_find(get_selector_cluster_block_indices(selector_cluster_index), block_index) != -1); blk.set_raw_selector_bits(get_selector_cluster_selector_bits(selector_cluster_index).get_raw_selector_bits()); diff --git a/basisu_frontend.h b/basisu_frontend.h index 07fcac1..2a630e0 100644 --- a/basisu_frontend.h +++ b/basisu_frontend.h @@ -33,6 +33,9 @@ namespace basisu uint32_t &operator[] (uint32_t i) { assert(i < 2); return m_comps[i]; } }; + const uint32_t BASISU_DEFAULT_COMPRESSION_LEVEL = 1; + const uint32_t BASISU_MAX_COMPRESSION_LEVEL = 5; + class basisu_frontend { BASISU_NO_EQUALS_OR_COPY_CONSTRUCT(basisu_frontend); @@ -41,7 +44,12 @@ namespace basisu basisu_frontend() : m_total_blocks(0), - m_total_pixels(0) + m_total_pixels(0), + m_endpoint_refinement(false), + m_use_hierarchical_endpoint_codebooks(false), + m_use_hierarchical_selector_codebooks(false), + m_num_endpoint_codebook_iterations(0), + m_num_selector_codebook_iterations(0) { } @@ -59,12 +67,11 @@ namespace basisu m_pSource_blocks(NULL), m_max_endpoint_clusters(256), m_max_selector_clusters(256), - m_endpoint_refinement(true), + m_compression_level(BASISU_DEFAULT_COMPRESSION_LEVEL), m_perceptual(true), m_debug_stats(false), m_debug_images(false), m_dump_endpoint_clusterization(false), - m_faster(false), m_pGlobal_sel_codebook(NULL), m_num_global_sel_codebook_pal_bits(0), m_num_global_sel_codebook_mod_bits(0), @@ -80,8 +87,8 @@ namespace basisu uint32_t m_max_endpoint_clusters; uint32_t m_max_selector_clusters; - bool m_faster; - bool m_endpoint_refinement; + uint32_t m_compression_level; + bool m_perceptual; bool m_debug_stats; bool m_debug_images; @@ -144,22 +151,44 @@ namespace basisu uint32_t m_total_blocks; uint32_t m_total_pixels; + bool m_endpoint_refinement; + bool m_use_hierarchical_endpoint_codebooks; + bool m_use_hierarchical_selector_codebooks; + + uint32_t m_num_endpoint_codebook_iterations; + uint32_t m_num_selector_codebook_iterations; + + // Source pixels for each blocks pixel_block_vec m_source_blocks; + // The quantized ETC1S texture. etc_block_vec m_encoded_blocks; - etc_block_vec m_orig_encoded_blocks; // encoded blocks after endpoint quant, but before selector quant + + // Quantized blocks after endpoint quant, but before selector quant + etc_block_vec m_orig_encoded_blocks; + // Full quality ETC1S texture etc_block_vec m_etc1_blocks_etc1s; - pixel_block_vec m_etc1_blocks_etc1s_unpacked; - + typedef vec<6, float> vec6F; - + + // Endpoint clusterizer typedef tree_vector_quant vec6F_quantizer; vec6F_quantizer m_endpoint_clusterizer; // For each endpoint cluster: An array of which subblock indices (block_index*2+subblock) are located in that cluster. - std::vector m_endpoint_clusters; + // Array of block indices for each endpoint cluster + std::vector m_endpoint_clusters; + // Array of block indices for each parent endpoint cluster + std::vector m_endpoint_parent_clusters; + + // Each block's parent cluster index + uint8_vec m_block_parent_endpoint_cluster; + + // Array of endpoint cluster indices for each parent endpoint cluster + std::vector m_endpoint_clusters_within_each_parent_cluster; + struct endpoint_cluster_etc_params { endpoint_cluster_etc_params() @@ -229,17 +258,33 @@ namespace basisu }; typedef std::vector cluster_subblock_etc_params_vec; + + // Each endpoint cluster's ETC1S parameters cluster_subblock_etc_params_vec m_endpoint_cluster_etc_params; + // The endpoint cluster index used by each ETC1 subblock. std::vector m_block_endpoint_clusters_indices; + // The block(s) within each selector cluster // Note: If you add anything here that uses selector cluster indicies, be sure to update optimize_selector_codebook()! std::vector m_selector_cluster_indices; + // The selector bits for each selector cluster. std::vector m_optimized_cluster_selectors; + + // The block(s) within each parent selector cluster. + std::vector m_selector_parent_cluster_indices; + + // Each block's parent selector cluster + uint8_vec m_block_parent_selector_cluster; + + // Array of selector cluster indices for each parent selector cluster + std::vector m_selector_clusters_within_each_parent_cluster; + basist::etc1_global_selector_codebook_entry_id_vec m_optimized_cluster_selector_global_cb_ids; bool_vec m_selector_cluster_uses_global_cb; + // Each block's selector cluster index std::vector m_block_selector_cluster_index; struct subblock_endpoint_quant_err @@ -265,6 +310,7 @@ namespace basisu } }; + // The sorted subblock endpoint quant error for each endpoint cluster std::vector m_subblock_endpoint_quant_err_vec; //----------------------------------------------------------------------------- @@ -279,6 +325,8 @@ namespace basisu uint32_t refine_endpoint_clusterization(); void eliminate_redundant_or_empty_endpoint_clusters(); void generate_block_endpoint_clusters(); + void compute_endpoint_clusters_within_each_parent_cluster(); + void compute_selector_clusters_within_each_parent_cluster(); void create_initial_packed_texture(); void create_selector_clusters(); void create_optimized_selector_codebook(uint32_t iter); diff --git a/basisu_tool.cpp b/basisu_tool.cpp index dc2876c..602111f 100644 --- a/basisu_tool.cpp +++ b/basisu_tool.cpp @@ -30,7 +30,7 @@ using namespace basisu; -#define BASISU_TOOL_VERSION "1.06.00" +#define BASISU_TOOL_VERSION "1.07.00" enum tool_mode { @@ -63,14 +63,14 @@ static void print_usage() " -multifile_printf: printf() format strint to use to compose multiple filenames\n" " -multifile_first: The index of the first file to process, default is 0 (must specify -multifile_printf and -multifile_num)\n" " -multifile_num: The total number of files to process.\n" - " -linear: Use linear colorspace metrics (instead of the default sRGB), and by default linear (not sRGB) mipmap filtering.\n" + " -level X: Set encoding speed vs. quality tradeoff. Range is 0-5, default is 1. Higher values=slower, but higher quality.\n" " -q X: Set quality level, 1-255, default is 128, lower=better compression/lower quality/faster, higher=less compression/higher quality/slower, default is 128. For even higher quality, use -max_endpoints/-max_selectors.\n" + " -linear: Use linear colorspace metrics (instead of the default sRGB), and by default linear (not sRGB) mipmap filtering.\n" " -output_file filename: Output .basis/.ktx filename\n" " -output_path: Output .basis/.ktx files to specified directory.\n" " -debug: Enable codec debug print to stdout (slightly slower).\n" " -debug_images: Enable codec debug images (much slower).\n" " -stats: Compute and display image quality metrics (slightly slower).\n" - " -slower: Enable optional stages in the compressor for slower but higher quality compression using better codebooks.\n" " -tex_type <2d, 2darray, 3d, video, cubemap>: Set Basis file header's texture type field. Cubemap arrays require multiples of 6 images, in X+, X-, Y+, Y-, Z+, Z- order, each image must be the same resolutions.\n" " 2d=arbitrary 2D images, 2darray=2D array, 3D=volume texture slices, video=video frames, cubemap=array of faces. For 2darray/3d/cubemaps/video, each source image's dimensions and # of mipmap levels must be the same.\n" " -framerate X: Set framerate in header to X/frames sec.\n" @@ -87,6 +87,7 @@ static void print_usage() " -seperate_rg_to_color_alpha: Seperate input R and G channels to RGB and A (for tangent space XY normal maps)\n" " -no_multithreading: Disable OpenMP multithreading\n" " -no_ktx: Disable KTX writing when unpacking (faster)\n" + " -etc1_only: Only unpack to ETC1, skipping the other texture formats during -unpack\n" "\n" "Mipmap generation options:\n" " -mipmap: Generate mipmaps for each source image\n" @@ -111,7 +112,6 @@ static void print_usage() " -no_hybrid_sel_cb: Don't automatically use hybrid virtual selector codebooks (for higher quality, only active when -global_sel_pal is specified)\n" " -global_pal_bits X: Set virtual selector codebook palette bits, range is [0,12], default is 8, higher is slower/better quality\n" " -global_mod_bits X: Set virtual selector codebook modifier bits, range is [0,15], defualt is 8, higher is slower/better quality\n" - " -no_endpoint_refinement: Disable endpoint codebook refinement stage (slightly faster, but lower quality)\n" " -hybrid_sel_cb_quality_thresh X: Set hybrid selector codebook quality threshold, default is 2.0, try 1.5-3, higher is lower quality/smaller codebooks\n" "\n" "Set various fields in the Basis file header:\n" @@ -119,16 +119,24 @@ static void print_usage() " -userdata1 X: Set 32-bit userdata1 field in Basis file header to X (X is a signed 32-bit int)\n" "\n" "Various command line examples:\n" - "basisu x.png : Compress sRGB image x.png to x.basis using default settings (multiple filenames OK)\n" - "basisu x.basis : Unpack x.basis to PNG/KTX files (multiple filenames OK)\n" - "basisu -file x.png -mipmap -y_flip : Compress a mipmapped x.basis file from an sRGB image named x.png, Y flip each source image\n" - "basisu -validate -file x.basis : Validate x.basis (check header, check file CRC's, attempt to transcode all slices)\n" - "basisu -unpack -file x.basis : Validates, transcodes and unpacks x.basis to mipmapped .KTX and RGB/A .PNG files (transcodes to all supported GPU texture formats)\n" - "basisu -q 255 -file x.png -mipmap -debug -stats : Compress sRGB x.png to x.basis at quality level 255 with compressor debug output/statistics\n" - "basisu -linear -max_endpoints 16128 -max_selectors 16128 -file x.png : Compress non-sRGB x.png to x.basis using the largest supported manually specified codebook sizes\n" - "basisu -linear -global_sel_pal -no_hybrid_sel_cb -file x.png : Compress a non-sRGB image, use virtual selector codebooks for improved compression (but slower encoding)\n" - "basisu -linear -global_sel_pal -file x.png: Compress a non-sRGB image, use hybrid selector codebooks for slightly improved compression (but slower encoding)\n" - "basisu -tex_type video -framerate 20 -multifile_printf \"x%02u.png\" -multifile_first 1 -multifile_count 20 : Compress a 20 sRGB source image video sequence (x01.png, x02.png, x03.png, etc.) to x01.basis\n" + " basisu x.png : Compress sRGB image x.png to x.basis using default settings (multiple filenames OK)\n" + " basisu x.basis : Unpack x.basis to PNG/KTX files (multiple filenames OK)\n" + " basisu -file x.png -mipmap -y_flip : Compress a mipmapped x.basis file from an sRGB image named x.png, Y flip each source image\n" + " basisu -validate -file x.basis : Validate x.basis (check header, check file CRC's, attempt to transcode all slices)\n" + " basisu -unpack -file x.basis : Validates, transcodes and unpacks x.basis to mipmapped .KTX and RGB/A .PNG files (transcodes to all supported GPU texture formats)\n" + " basisu -q 255 -file x.png -mipmap -debug -stats : Compress sRGB x.png to x.basis at quality level 255 with compressor debug output/statistics\n" + " basisu -linear -max_endpoints 16128 -max_selectors 16128 -file x.png : Compress non-sRGB x.png to x.basis using the largest supported manually specified codebook sizes\n" + " basisu -linear -global_sel_pal -no_hybrid_sel_cb -file x.png : Compress a non-sRGB image, use virtual selector codebooks for improved compression (but slower encoding)\n" + " basisu -linear -global_sel_pal -file x.png: Compress a non-sRGB image, use hybrid selector codebooks for slightly improved compression (but slower encoding)\n" + " basisu -tex_type video -framerate 20 -multifile_printf \"x%02u.png\" -multifile_first 1 -multifile_count 20 : Compress a 20 sRGB source image video sequence (x01.png, x02.png, x03.png, etc.) to x01.basis\n" + "\n" + "Compression level details:\n" + " Level 0: Fastest, but has marginal quality and is a work in progress. Brittle on complex images. Avg. Y dB: 35.45\n" + " Level 1: Hierarchical codebook searching. 36.87 dB, ~1.4x slower vs. level 0. (This is the default setting.)\n" + " Level 2: Full codebook searching. 37.13 dB, ~1.8x slower vs. level 0. (Equivalent the the initial release's default settings.)\n" + " Level 3: Hierarchical codebook searching, codebook k-means iterations. 37.15 dB, ~4x slower vs. level 0\n" + " Level 4: Full codebook searching, codebook k-means iterations. 37.41 dB, ~5.5x slower vs. level 0. (Equivalent to the initial release's -slower setting.)\n" + " Level 5: Full codebook searching, twice as many codebook k-means iterations, best ETC1 endpoint opt. 37.43 dB, ~12x slower vs. level 0\n" ); } @@ -212,6 +220,7 @@ public: m_multifile_num(0), m_individual(false), m_no_ktx(false), + m_etc1_only(false), m_fuzz_testing(false) { } @@ -296,8 +305,17 @@ public: m_comp_params.m_debug_images = true; else if (strcasecmp(pArg, "-stats") == 0) m_comp_params.m_compute_stats = true; + else if (strcasecmp(pArg, "-level") == 0) + { + REMAINING_ARGS_CHECK(1); + m_comp_params.m_compression_level = atoi(arg_v[arg_index + 1]); + arg_count++; + } else if (strcasecmp(pArg, "-slower") == 0) - m_comp_params.m_faster = false; + { + // This option is gone, but we'll do something reasonable with it anyway. Level 4 is equivalent to the original release's -slower, but let's just go to level 2. + m_comp_params.m_compression_level = 2; + } else if (strcasecmp(pArg, "-max_endpoints") == 0) { REMAINING_ARGS_CHECK(1); @@ -335,6 +353,8 @@ public: m_comp_params.m_mip_gen = true; else if (strcasecmp(pArg, "-no_ktx") == 0) m_no_ktx = true; + else if (strcasecmp(pArg, "-etc1_only") == 0) + m_etc1_only = true; else if (strcasecmp(pArg, "-mip_scale") == 0) { REMAINING_ARGS_CHECK(1); @@ -380,8 +400,6 @@ public: } else if (strcasecmp(pArg, "-global_sel_pal") == 0) m_comp_params.m_global_sel_pal = true; - else if (strcasecmp(pArg, "-no_endpoint_refinement") == 0) - m_comp_params.m_no_endpoint_refinement = true; else if (strcasecmp(pArg, "-no_auto_global_sel_pal") == 0) m_comp_params.m_no_auto_global_sel_pal = true; else if (strcasecmp(pArg, "-global_pal_bits") == 0) @@ -549,6 +567,7 @@ public: bool m_individual; bool m_no_ktx; + bool m_etc1_only; bool m_fuzz_testing; }; @@ -615,7 +634,7 @@ static bool compress_mode(command_line_params &opts) FILE *pCSV_file = nullptr; if (opts.m_csv_file.size()) { - pCSV_file = fopen_safe(opts.m_csv_file.c_str(), "w"); + pCSV_file = fopen_safe(opts.m_csv_file.c_str(), "a"); if (!pCSV_file) { error_printf("Failed opening CVS file \"%s\"\n", opts.m_csv_file.c_str()); @@ -683,10 +702,16 @@ static bool compress_mode(command_line_params &opts) return false; } + interval_timer tm; + tm.start(); + basis_compressor::error_code ec = c.process(); + + tm.stop(); + if (ec == basis_compressor::cECSuccess) { - printf("Compression succeeded to file \"%s\"\n", params.m_out_filename.c_str()); + printf("Compression succeeded to file \"%s\" in %3.3f secs\n", params.m_out_filename.c_str(), tm.get_elapsed_secs()); } else { @@ -742,7 +767,7 @@ static bool compress_mode(command_line_params &opts) { for (size_t slice_index = 0; slice_index < c.get_stats().size(); slice_index++) { - fprintf(pCSV_file, "\"%s\", %u, %u, %u, %u, %u, %f, %f, %f, %f, %u\n", + fprintf(pCSV_file, "\"%s\", %u, %u, %u, %u, %u, %f, %f, %f, %f, %u, %u, %f\n", params.m_out_filename.c_str(), (uint32_t)slice_index, (uint32_t)c.get_stats().size(), c.get_stats()[slice_index].m_width, c.get_stats()[slice_index].m_height, (uint32_t)c.get_any_source_image_has_alpha(), @@ -750,7 +775,8 @@ static bool compress_mode(command_line_params &opts) c.get_stats()[slice_index].m_best_luma_709_psnr, c.get_stats()[slice_index].m_basis_etc1s_luma_709_psnr, c.get_stats()[slice_index].m_basis_bc1_luma_709_psnr, - params.m_quality_level); + params.m_quality_level, (int)params.m_compression_level, tm.get_elapsed_secs()); + fflush(pCSV_file); } } @@ -879,7 +905,16 @@ static bool unpack_and_validate_mode(command_line_params &opts, bool validate_fl std::vector< gpu_image_vec > gpu_images[basist::cTFTotalTextureFormats]; - for (int format_iter = 0; format_iter < basist::cTFTotalTextureFormats; format_iter++) + int first_format = 0; + int last_format = basist::cTFTotalTextureFormats; + + if (opts.m_etc1_only) + { + first_format = basist::cTFETC1; + last_format = first_format + 1; + } + + for (int format_iter = first_format; format_iter < last_format; format_iter++) { basist::transcoder_texture_format tex_fmt = static_cast(format_iter); @@ -902,7 +937,7 @@ static bool unpack_and_validate_mode(command_line_params &opts, bool validate_fl return false; } - for (int format_iter = 0; format_iter < basist::cTFTotalTextureFormats; format_iter++) + for (int format_iter = first_format; format_iter < last_format; format_iter++) { const basist::transcoder_texture_format transcoder_tex_fmt = static_cast(format_iter); @@ -973,7 +1008,7 @@ static bool unpack_and_validate_mode(command_line_params &opts, bool validate_fl { // Now write KTX files and unpack them to individual PNG's - for (int format_iter = 0; format_iter < basist::cTFTotalTextureFormats; format_iter++) + for (int format_iter = first_format; format_iter < last_format; format_iter++) { const basist::transcoder_texture_format transcoder_tex_fmt = static_cast(format_iter); @@ -1187,7 +1222,7 @@ static bool compare_mode(command_line_params &opts) static int main_internal(int argc, const char **argv) { basisu_encoder_init(); - + printf("Basis Universal GPU Texture Compressor Reference Encoder v" BASISU_TOOL_VERSION ", Copyright (C) 2017-2019 Binomial LLC, All rights reserved\n"); #if defined(DEBUG) || defined(_DEBUG)