Path: blob/master/thirdparty/basis_universal/encoder/basisu_uastc_enc.cpp
9903 views
// basisu_uastc_enc.cpp1// Copyright (C) 2019-2024 Binomial LLC. All Rights Reserved.2//3// Licensed under the Apache License, Version 2.0 (the "License");4// you may not use this file except in compliance with the License.5// You may obtain a copy of the License at6//7// http://www.apache.org/licenses/LICENSE-2.08//9// Unless required by applicable law or agreed to in writing, software10// distributed under the License is distributed on an "AS IS" BASIS,11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12// See the License for the specific language governing permissions and13// limitations under the License.14#include "basisu_uastc_enc.h"15#include "3rdparty/android_astc_decomp.h"16#include "basisu_gpu_texture.h"17#include "basisu_bc7enc.h"1819#ifdef _DEBUG20// When BASISU_VALIDATE_UASTC_ENC is 1, we pack and unpack to/from UASTC and ASTC, then validate that each codec returns the exact same results. This is slower.21#define BASISU_VALIDATE_UASTC_ENC 122#endif2324#define BASISU_SUPPORT_FORCE_MODE 02526using namespace basist;2728namespace basisu29{30const uint32_t MAX_ENCODE_RESULTS = 512;3132#if BASISU_VALIDATE_UASTC_ENC33static void validate_func(bool condition, int line)34{35if (!condition)36{37fprintf(stderr, "basisu_uastc_enc: Internal validation failed on line %u!\n", line);38}39}4041#define VALIDATE(c) validate_func(c, __LINE__);42#else43#define VALIDATE(c)44#endif4546enum dxt_constants47{48cDXT1SelectorBits = 2U, cDXT1SelectorValues = 1U << cDXT1SelectorBits, cDXT1SelectorMask = cDXT1SelectorValues - 1U,49cDXT5SelectorBits = 3U, cDXT5SelectorValues = 1U << cDXT5SelectorBits, cDXT5SelectorMask = cDXT5SelectorValues - 1U,50};5152struct dxt1_block53{54enum { cTotalEndpointBytes = 2, cTotalSelectorBytes = 4 };5556uint8_t m_low_color[cTotalEndpointBytes];57uint8_t m_high_color[cTotalEndpointBytes];58uint8_t m_selectors[cTotalSelectorBytes];5960inline void clear() { basisu::clear_obj(*this); }6162inline uint32_t get_high_color() const { return m_high_color[0] | (m_high_color[1] << 8U); }63inline uint32_t get_low_color() const { return m_low_color[0] | (m_low_color[1] << 8U); }64inline void set_low_color(uint16_t c) { m_low_color[0] = static_cast<uint8_t>(c & 0xFF); m_low_color[1] = static_cast<uint8_t>((c >> 8) & 0xFF); }65inline void set_high_color(uint16_t c) { m_high_color[0] = static_cast<uint8_t>(c & 0xFF); m_high_color[1] = static_cast<uint8_t>((c >> 8) & 0xFF); }66inline uint32_t get_selector(uint32_t x, uint32_t y) const { assert((x < 4U) && (y < 4U)); return (m_selectors[y] >> (x * cDXT1SelectorBits))& cDXT1SelectorMask; }67inline void set_selector(uint32_t x, uint32_t y, uint32_t val) { assert((x < 4U) && (y < 4U) && (val < 4U)); m_selectors[y] &= (~(cDXT1SelectorMask << (x * cDXT1SelectorBits))); m_selectors[y] |= (val << (x * cDXT1SelectorBits)); }6869static uint16_t pack_color(const color_rgba& color, bool scaled, uint32_t bias = 127U)70{71uint32_t r = color.r, g = color.g, b = color.b;72if (scaled)73{74r = (r * 31U + bias) / 255U;75g = (g * 63U + bias) / 255U;76b = (b * 31U + bias) / 255U;77}78return static_cast<uint16_t>(basisu::minimum(b, 31U) | (basisu::minimum(g, 63U) << 5U) | (basisu::minimum(r, 31U) << 11U));79}8081static uint16_t pack_unscaled_color(uint32_t r, uint32_t g, uint32_t b) { return static_cast<uint16_t>(b | (g << 5U) | (r << 11U)); }82};8384#define UASTC_WRITE_MODE_DESCS 08586static inline void uastc_write_bits(uint8_t* pBuf, uint32_t& bit_offset, uint64_t code, uint32_t codesize, const char* pDesc)87{88(void)pDesc;8990#if UASTC_WRITE_MODE_DESCS91if (pDesc)92printf("%s: %u %u\n", pDesc, bit_offset, codesize);93#endif9495assert((codesize == 64) || (code < (1ULL << codesize)));9697while (codesize)98{99uint32_t byte_bit_offset = bit_offset & 7;100uint32_t bits_to_write = basisu::minimum<int>(codesize, 8 - byte_bit_offset);101102pBuf[bit_offset >> 3] |= (code << byte_bit_offset);103104code >>= bits_to_write;105codesize -= bits_to_write;106bit_offset += bits_to_write;107}108}109110void pack_uastc(basist::uastc_block& blk, const uastc_encode_results& result, const etc_block& etc1_blk, uint32_t etc1_bias, const eac_a8_block& etc_eac_a8_blk, bool bc1_hint0, bool bc1_hint1)111{112if ((g_uastc_mode_has_alpha[result.m_uastc_mode]) && (result.m_uastc_mode != UASTC_MODE_INDEX_SOLID_COLOR))113{114assert(etc_eac_a8_blk.m_multiplier >= 1);115}116117uint8_t buf[32];118memset(buf, 0, sizeof(buf));119120uint32_t block_bit_offset = 0;121122#if UASTC_WRITE_MODE_DESCS123printf("**** Mode: %u\n", result.m_uastc_mode);124#endif125126uastc_write_bits(buf, block_bit_offset, g_uastc_mode_huff_codes[result.m_uastc_mode][0], g_uastc_mode_huff_codes[result.m_uastc_mode][1], "mode");127128if (result.m_uastc_mode == UASTC_MODE_INDEX_SOLID_COLOR)129{130uastc_write_bits(buf, block_bit_offset, result.m_solid_color.r, 8, "R");131uastc_write_bits(buf, block_bit_offset, result.m_solid_color.g, 8, "G");132uastc_write_bits(buf, block_bit_offset, result.m_solid_color.b, 8, "B");133uastc_write_bits(buf, block_bit_offset, result.m_solid_color.a, 8, "A");134135uastc_write_bits(buf, block_bit_offset, etc1_blk.get_diff_bit(), 1, "ETC1D");136uastc_write_bits(buf, block_bit_offset, etc1_blk.get_inten_table(0), 3, "ETC1I");137uastc_write_bits(buf, block_bit_offset, etc1_blk.get_selector(0, 0), 2, "ETC1S");138139uint32_t r, g, b;140if (etc1_blk.get_diff_bit())141etc_block::unpack_color5(r, g, b, etc1_blk.get_base5_color(), false);142else143etc_block::unpack_color4(r, g, b, etc1_blk.get_base4_color(0), false);144145uastc_write_bits(buf, block_bit_offset, r, 5, "ETC1R");146uastc_write_bits(buf, block_bit_offset, g, 5, "ETC1G");147uastc_write_bits(buf, block_bit_offset, b, 5, "ETC1B");148149memcpy(&blk, buf, sizeof(blk));150return;151}152153if (g_uastc_mode_has_bc1_hint0[result.m_uastc_mode])154uastc_write_bits(buf, block_bit_offset, bc1_hint0, 1, "BC1H0");155else156{157assert(bc1_hint0 == false);158}159160if (g_uastc_mode_has_bc1_hint1[result.m_uastc_mode])161uastc_write_bits(buf, block_bit_offset, bc1_hint1, 1, "BC1H1");162else163{164assert(bc1_hint1 == false);165}166167uastc_write_bits(buf, block_bit_offset, etc1_blk.get_flip_bit(), 1, "ETC1F");168uastc_write_bits(buf, block_bit_offset, etc1_blk.get_diff_bit(), 1, "ETC1D");169uastc_write_bits(buf, block_bit_offset, etc1_blk.get_inten_table(0), 3, "ETC1I0");170uastc_write_bits(buf, block_bit_offset, etc1_blk.get_inten_table(1), 3, "ETC1I1");171172if (g_uastc_mode_has_etc1_bias[result.m_uastc_mode])173uastc_write_bits(buf, block_bit_offset, etc1_bias, 5, "ETC1BIAS");174else175{176assert(etc1_bias == 0);177}178179if (g_uastc_mode_has_alpha[result.m_uastc_mode])180{181const uint32_t etc2_hints = etc_eac_a8_blk.m_table | (etc_eac_a8_blk.m_multiplier << 4);182183assert(etc2_hints > 0 && etc2_hints <= 0xFF);184uastc_write_bits(buf, block_bit_offset, etc2_hints, 8, "ETC2TM");185}186187uint32_t subsets = 1;188switch (result.m_uastc_mode)189{190case 2:191case 4:192case 7:193case 9:194case 16:195uastc_write_bits(buf, block_bit_offset, result.m_common_pattern, 5, "PAT");196subsets = 2;197break;198case 3:199uastc_write_bits(buf, block_bit_offset, result.m_common_pattern, 4, "PAT");200subsets = 3;201break;202default:203break;204}205206#ifdef _DEBUG207uint32_t part_seed = 0;208switch (result.m_uastc_mode)209{210case 2:211case 4:212case 9:213case 16:214part_seed = g_astc_bc7_common_partitions2[result.m_common_pattern].m_astc;215break;216case 3:217part_seed = g_astc_bc7_common_partitions3[result.m_common_pattern].m_astc;218break;219case 7:220part_seed = g_bc7_3_astc2_common_partitions[result.m_common_pattern].m_astc2;221break;222default:223break;224}225#endif226227uint32_t total_planes = 1;228switch (result.m_uastc_mode)229{230case 6:231case 11:232case 13:233uastc_write_bits(buf, block_bit_offset, result.m_astc.m_ccs, 2, "COMPSEL");234total_planes = 2;235break;236case 17:237// CCS field is always 3 for dual plane LA.238assert(result.m_astc.m_ccs == 3);239total_planes = 2;240break;241default:242break;243}244245uint8_t weights[32];246memcpy(weights, result.m_astc.m_weights, 16 * total_planes);247248uint8_t endpoints[18];249memcpy(endpoints, result.m_astc.m_endpoints, sizeof(endpoints));250251const uint32_t total_comps = g_uastc_mode_comps[result.m_uastc_mode];252253// LLAA254// LLAA LLAA255// LLAA LLAA LLAA256// RRGGBB257// RRGGBB RRGGBB258// RRGGBB RRGGBB RRGGBB259// RRGGBBAA260// RRGGBBAA RRGGBBAA261262const uint32_t weight_bits = g_uastc_mode_weight_bits[result.m_uastc_mode];263264const uint8_t* pPartition_pattern;265const uint8_t* pSubset_anchor_indices = basist::get_anchor_indices(subsets, result.m_uastc_mode, result.m_common_pattern, pPartition_pattern);266267for (uint32_t plane_index = 0; plane_index < total_planes; plane_index++)268{269for (uint32_t subset_index = 0; subset_index < subsets; subset_index++)270{271const uint32_t anchor_index = pSubset_anchor_indices[subset_index];272273#ifdef _DEBUG274if (subsets >= 2)275{276for (uint32_t i = 0; i < 16; i++)277{278const uint32_t part_index = astc_compute_texel_partition(part_seed, i & 3, i >> 2, 0, subsets, true);279if (part_index == subset_index)280{281assert(anchor_index == i);282break;283}284}285}286else287{288assert(!anchor_index);289}290#endif291292// Check anchor weight's MSB - if it's set then invert this subset's weights and swap the endpoints293if (weights[anchor_index * total_planes + plane_index] & (1 << (weight_bits - 1)))294{295for (uint32_t i = 0; i < 16; i++)296{297const uint32_t part_index = pPartition_pattern[i];298299#ifdef _DEBUG300if (subsets >= 2)301{302assert(part_index == (uint32_t)astc_compute_texel_partition(part_seed, i & 3, i >> 2, 0, subsets, true));303}304else305{306assert(!part_index);307}308#endif309310if (part_index == subset_index)311weights[i * total_planes + plane_index] = ((1 << weight_bits) - 1) - weights[i * total_planes + plane_index];312}313314if (total_planes == 2)315{316for (int c = 0; c < (int)total_comps; c++)317{318const uint32_t comp_plane = (total_comps == 2) ? c : ((c == result.m_astc.m_ccs) ? 1 : 0);319320if (comp_plane == plane_index)321std::swap(endpoints[c * 2 + 0], endpoints[c * 2 + 1]);322}323}324else325{326for (uint32_t c = 0; c < total_comps; c++)327std::swap(endpoints[subset_index * total_comps * 2 + c * 2 + 0], endpoints[subset_index * total_comps * 2 + c * 2 + 1]);328}329}330} // subset_index331} // plane_index332333const uint32_t total_values = total_comps * 2 * subsets;334const uint32_t endpoint_range = g_uastc_mode_endpoint_ranges[result.m_uastc_mode];335336uint32_t bit_values[18];337uint32_t tq_values[8];338uint32_t total_tq_values = 0;339uint32_t tq_accum = 0;340uint32_t tq_mul = 1;341342const uint32_t ep_bits = g_astc_bise_range_table[endpoint_range][0];343const uint32_t ep_trits = g_astc_bise_range_table[endpoint_range][1];344const uint32_t ep_quints = g_astc_bise_range_table[endpoint_range][2];345346for (uint32_t i = 0; i < total_values; i++)347{348uint32_t val = endpoints[i];349350uint32_t bits = val & ((1 << ep_bits) - 1);351uint32_t tq = val >> ep_bits;352353bit_values[i] = bits;354355if (ep_trits)356{357assert(tq < 3);358tq_accum += tq * tq_mul;359tq_mul *= 3;360if (tq_mul == 243)361{362tq_values[total_tq_values++] = tq_accum;363tq_accum = 0;364tq_mul = 1;365}366}367else if (ep_quints)368{369assert(tq < 5);370tq_accum += tq * tq_mul;371tq_mul *= 5;372if (tq_mul == 125)373{374tq_values[total_tq_values++] = tq_accum;375tq_accum = 0;376tq_mul = 1;377}378}379}380381uint32_t total_endpoint_bits = 0;382(void)total_endpoint_bits;383384for (uint32_t i = 0; i < total_tq_values; i++)385{386const uint32_t num_bits = ep_trits ? 8 : 7;387uastc_write_bits(buf, block_bit_offset, tq_values[i], num_bits, "ETQ");388total_endpoint_bits += num_bits;389}390391if (tq_mul > 1)392{393uint32_t num_bits;394if (ep_trits)395{396if (tq_mul == 3)397num_bits = 2;398else if (tq_mul == 9)399num_bits = 4;400else if (tq_mul == 27)401num_bits = 5;402else //if (tq_mul == 81)403num_bits = 7;404}405else406{407if (tq_mul == 5)408num_bits = 3;409else //if (tq_mul == 25)410num_bits = 5;411}412uastc_write_bits(buf, block_bit_offset, tq_accum, num_bits, "ETQ");413total_endpoint_bits += num_bits;414}415416for (uint32_t i = 0; i < total_values; i++)417{418uastc_write_bits(buf, block_bit_offset, bit_values[i], ep_bits, "EBITS");419total_endpoint_bits += ep_bits;420}421422#if UASTC_WRITE_MODE_DESCS423uint32_t weight_start = block_bit_offset;424#endif425426uint32_t total_weight_bits = 0;427(void)total_weight_bits;428429const uint32_t plane_shift = (total_planes == 2) ? 1 : 0;430for (uint32_t i = 0; i < 16 * total_planes; i++)431{432uint32_t numbits = weight_bits;433for (uint32_t s = 0; s < subsets; s++)434{435if (pSubset_anchor_indices[s] == (i >> plane_shift))436{437numbits--;438break;439}440}441442uastc_write_bits(buf, block_bit_offset, weights[i], numbits, nullptr);443444total_weight_bits += numbits;445}446447#if UASTC_WRITE_MODE_DESCS448printf("WEIGHTS: %u %u\n", weight_start, total_weight_bits);449#endif450451assert(block_bit_offset <= 128);452memcpy(&blk, buf, sizeof(blk));453454#if UASTC_WRITE_MODE_DESCS455printf("Total bits: %u, endpoint bits: %u, weight bits: %u\n", block_bit_offset, total_endpoint_bits, total_weight_bits);456#endif457}458459// MODE 0460// 0. DualPlane: 0, WeightRange: 8 (16), Subsets: 1, CEM: 8 (RGB Direct ), EndpointRange: 19 (192) MODE6 RGB461// 18. DualPlane: 0, WeightRange: 11 (32), Subsets: 1, CEM: 8 (RGB Direct ), EndpointRange: 11 (32) MODE6 RGB462static void astc_mode0_or_18(uint32_t mode, const color_rgba block[4][4], uastc_encode_results* pResults, uint32_t& total_results, bc7enc_compress_block_params& comp_params, const uint8_t *pForce_selectors = nullptr)463{464const uint32_t endpoint_range = (mode == 18) ? 11 : 19;465const uint32_t weight_range = (mode == 18) ? 11 : 8;466467color_cell_compressor_params ccell_params;468memset(&ccell_params, 0, sizeof(ccell_params));469470ccell_params.m_num_pixels = 16;471ccell_params.m_pPixels = (color_quad_u8*)&block[0][0];472ccell_params.m_num_selector_weights = (mode == 18) ? 32 : 16;473ccell_params.m_pSelector_weights = (mode == 18) ? g_astc_weights5 : g_astc_weights4;474ccell_params.m_pSelector_weightsx = (mode == 18) ? (const bc7enc_vec4F*)g_astc_weights5x : (const bc7enc_vec4F*)g_astc_weights4x;475ccell_params.m_astc_endpoint_range = endpoint_range;476ccell_params.m_weights[0] = 1;477ccell_params.m_weights[1] = 1;478ccell_params.m_weights[2] = 1;479ccell_params.m_weights[3] = 1;480ccell_params.m_pForce_selectors = pForce_selectors;481482color_cell_compressor_results ccell_results;483uint8_t ccell_result_selectors[16];484uint8_t ccell_result_selectors_temp[16];485memset(&ccell_results, 0, sizeof(ccell_results));486ccell_results.m_pSelectors = &ccell_result_selectors[0];487ccell_results.m_pSelectors_temp = &ccell_result_selectors_temp[0];488489uint64_t part_err = color_cell_compression(255, &ccell_params, &ccell_results, &comp_params);490491// ASTC492astc_block_desc astc_results;493memset(&astc_results, 0, sizeof(astc_results));494495astc_results.m_dual_plane = false;496astc_results.m_weight_range = weight_range;// (mode == 18) ? 11 : 8;497498astc_results.m_ccs = 0;499astc_results.m_subsets = 1;500astc_results.m_partition_seed = 0;501astc_results.m_cem = 8;502503astc_results.m_endpoints[0] = ccell_results.m_astc_low_endpoint.m_c[0];504astc_results.m_endpoints[1] = ccell_results.m_astc_high_endpoint.m_c[0];505astc_results.m_endpoints[2] = ccell_results.m_astc_low_endpoint.m_c[1];506astc_results.m_endpoints[3] = ccell_results.m_astc_high_endpoint.m_c[1];507astc_results.m_endpoints[4] = ccell_results.m_astc_low_endpoint.m_c[2];508astc_results.m_endpoints[5] = ccell_results.m_astc_high_endpoint.m_c[2];509510bool invert = false;511512if (pForce_selectors == nullptr)513{514int s0 = g_astc_unquant[endpoint_range][astc_results.m_endpoints[0]].m_unquant + g_astc_unquant[endpoint_range][astc_results.m_endpoints[2]].m_unquant + g_astc_unquant[endpoint_range][astc_results.m_endpoints[4]].m_unquant;515int s1 = g_astc_unquant[endpoint_range][astc_results.m_endpoints[1]].m_unquant + g_astc_unquant[endpoint_range][astc_results.m_endpoints[3]].m_unquant + g_astc_unquant[endpoint_range][astc_results.m_endpoints[5]].m_unquant;516if (s1 < s0)517{518std::swap(astc_results.m_endpoints[0], astc_results.m_endpoints[1]);519std::swap(astc_results.m_endpoints[2], astc_results.m_endpoints[3]);520std::swap(astc_results.m_endpoints[4], astc_results.m_endpoints[5]);521invert = true;522}523}524525for (uint32_t y = 0; y < 4; y++)526{527for (uint32_t x = 0; x < 4; x++)528{529astc_results.m_weights[x + y * 4] = ccell_result_selectors[x + y * 4];530531if (invert)532astc_results.m_weights[x + y * 4] = ((mode == 18) ? 31 : 15) - astc_results.m_weights[x + y * 4];533}534}535536assert(total_results < MAX_ENCODE_RESULTS);537if (total_results < MAX_ENCODE_RESULTS)538{539pResults[total_results].m_uastc_mode = mode;540pResults[total_results].m_common_pattern = 0;541pResults[total_results].m_astc = astc_results;542pResults[total_results].m_astc_err = part_err;543total_results++;544}545}546547// MODE 1548// 1-subset, 2-bit indices, 8-bit endpoints, BC7 mode 3549// DualPlane: 0, WeightRange: 2 (4), Subsets: 1, CEM: 8 (RGB Direct ), EndpointRange: 20 (256) MODE3 or MODE5 RGB550static void astc_mode1(const color_rgba block[4][4], uastc_encode_results* pResults, uint32_t& total_results, bc7enc_compress_block_params& comp_params)551{552color_cell_compressor_params ccell_params;553memset(&ccell_params, 0, sizeof(ccell_params));554555ccell_params.m_num_pixels = 16;556ccell_params.m_pPixels = (color_quad_u8*)&block[0][0];557ccell_params.m_num_selector_weights = 4;558ccell_params.m_pSelector_weights = g_bc7_weights2;559ccell_params.m_pSelector_weightsx = (const bc7enc_vec4F*)g_bc7_weights2x;560ccell_params.m_astc_endpoint_range = 20;561ccell_params.m_weights[0] = 1;562ccell_params.m_weights[1] = 1;563ccell_params.m_weights[2] = 1;564ccell_params.m_weights[3] = 1;565566color_cell_compressor_results ccell_results;567uint8_t ccell_result_selectors[16];568uint8_t ccell_result_selectors_temp[16];569memset(&ccell_results, 0, sizeof(ccell_results));570ccell_results.m_pSelectors = &ccell_result_selectors[0];571ccell_results.m_pSelectors_temp = &ccell_result_selectors_temp[0];572573uint64_t part_err = color_cell_compression(255, &ccell_params, &ccell_results, &comp_params);574575// ASTC576astc_block_desc astc_results;577memset(&astc_results, 0, sizeof(astc_results));578579astc_results.m_dual_plane = false;580astc_results.m_weight_range = 2;581582astc_results.m_ccs = 0;583astc_results.m_subsets = 1;584astc_results.m_partition_seed = 0;585astc_results.m_cem = 8;586587astc_results.m_endpoints[0] = ccell_results.m_astc_low_endpoint.m_c[0];588astc_results.m_endpoints[1] = ccell_results.m_astc_high_endpoint.m_c[0];589astc_results.m_endpoints[2] = ccell_results.m_astc_low_endpoint.m_c[1];590astc_results.m_endpoints[3] = ccell_results.m_astc_high_endpoint.m_c[1];591astc_results.m_endpoints[4] = ccell_results.m_astc_low_endpoint.m_c[2];592astc_results.m_endpoints[5] = ccell_results.m_astc_high_endpoint.m_c[2];593594const uint32_t range = 20;595596bool invert = false;597598int s0 = g_astc_unquant[range][astc_results.m_endpoints[0]].m_unquant + g_astc_unquant[range][astc_results.m_endpoints[2]].m_unquant + g_astc_unquant[range][astc_results.m_endpoints[4]].m_unquant;599int s1 = g_astc_unquant[range][astc_results.m_endpoints[1]].m_unquant + g_astc_unquant[range][astc_results.m_endpoints[3]].m_unquant + g_astc_unquant[range][astc_results.m_endpoints[5]].m_unquant;600if (s1 < s0)601{602std::swap(astc_results.m_endpoints[0], astc_results.m_endpoints[1]);603std::swap(astc_results.m_endpoints[2], astc_results.m_endpoints[3]);604std::swap(astc_results.m_endpoints[4], astc_results.m_endpoints[5]);605invert = true;606}607608for (uint32_t y = 0; y < 4; y++)609{610for (uint32_t x = 0; x < 4; x++)611{612astc_results.m_weights[x + y * 4] = ccell_result_selectors[x + y * 4];613614if (invert)615astc_results.m_weights[x + y * 4] = 3 - astc_results.m_weights[x + y * 4];616}617}618619assert(total_results < MAX_ENCODE_RESULTS);620if (total_results < MAX_ENCODE_RESULTS)621{622pResults[total_results].m_uastc_mode = 1;623pResults[total_results].m_common_pattern = 0;624pResults[total_results].m_astc = astc_results;625pResults[total_results].m_astc_err = part_err;626total_results++;627}628}629630static uint32_t estimate_partition2(uint32_t num_weights, uint32_t num_comps, const uint32_t* pWeights, const color_rgba block[4][4], const uint32_t weights[4])631{632assert(pWeights[0] == 0 && pWeights[num_weights - 1] == 64);633634uint64_t best_err = UINT64_MAX;635uint32_t best_common_pattern = 0;636637for (uint32_t common_pattern = 0; common_pattern < TOTAL_ASTC_BC7_COMMON_PARTITIONS2; common_pattern++)638{639const uint32_t bc7_pattern = g_astc_bc7_common_partitions2[common_pattern].m_bc7;640641const uint8_t* pPartition = &g_bc7_partition2[bc7_pattern * 16];642643color_quad_u8 subset_colors[2][16];644uint32_t subset_total_colors[2] = { 0, 0 };645for (uint32_t index = 0; index < 16; index++)646subset_colors[pPartition[index]][subset_total_colors[pPartition[index]]++] = ((const color_quad_u8*)block)[index];647648uint64_t total_subset_err = 0;649for (uint32_t subset = 0; (subset < 2) && (total_subset_err < best_err); subset++)650total_subset_err += color_cell_compression_est_astc(num_weights, num_comps, pWeights, subset_total_colors[subset], &subset_colors[subset][0], best_err, weights);651652if (total_subset_err < best_err)653{654best_err = total_subset_err;655best_common_pattern = common_pattern;656}657}658659return best_common_pattern;660}661662// MODE 2663// 2-subset, 3-bit indices, 4-bit endpoints, BC7 mode 1664// DualPlane: 0, WeightRange: 5 (8), Subsets: 2, CEM: 8 (RGB Direct ), EndpointRange: 8 (16) MODE1665static void astc_mode2(const color_rgba block[4][4], uastc_encode_results* pResults, uint32_t& total_results, bc7enc_compress_block_params& comp_params, bool estimate_partition)666{667uint32_t first_common_pattern = 0;668uint32_t last_common_pattern = TOTAL_ASTC_BC7_COMMON_PARTITIONS2;669670if (estimate_partition)671{672const uint32_t weights[4] = { 1, 1, 1, 1 };673first_common_pattern = estimate_partition2(8, 3, g_bc7_weights3, block, weights);674last_common_pattern = first_common_pattern + 1;675}676677for (uint32_t common_pattern = first_common_pattern; common_pattern < last_common_pattern; common_pattern++)678{679const uint32_t bc7_pattern = g_astc_bc7_common_partitions2[common_pattern].m_bc7;680681color_rgba part_pixels[2][16];682uint32_t part_pixel_index[4][4];683uint32_t num_part_pixels[2] = { 0, 0 };684685for (uint32_t y = 0; y < 4; y++)686{687for (uint32_t x = 0; x < 4; x++)688{689const uint32_t part = g_bc7_partition2[16 * bc7_pattern + x + y * 4];690part_pixel_index[y][x] = num_part_pixels[part];691part_pixels[part][num_part_pixels[part]++] = block[y][x];692}693}694695color_cell_compressor_params ccell_params[2];696color_cell_compressor_results ccell_results[2];697uint8_t ccell_result_selectors[2][16];698uint8_t ccell_result_selectors_temp[2][16];699700uint64_t total_part_err = 0;701for (uint32_t part = 0; part < 2; part++)702{703memset(&ccell_params[part], 0, sizeof(ccell_params[part]));704705ccell_params[part].m_num_pixels = num_part_pixels[part];706ccell_params[part].m_pPixels = (color_quad_u8*)&part_pixels[part][0];707ccell_params[part].m_num_selector_weights = 8;708ccell_params[part].m_pSelector_weights = g_bc7_weights3;709ccell_params[part].m_pSelector_weightsx = (const bc7enc_vec4F*)g_bc7_weights3x;710ccell_params[part].m_astc_endpoint_range = 8;711ccell_params[part].m_weights[0] = 1;712ccell_params[part].m_weights[1] = 1;713ccell_params[part].m_weights[2] = 1;714ccell_params[part].m_weights[3] = 1;715716memset(&ccell_results[part], 0, sizeof(ccell_results[part]));717ccell_results[part].m_pSelectors = &ccell_result_selectors[part][0];718ccell_results[part].m_pSelectors_temp = &ccell_result_selectors_temp[part][0];719720uint64_t part_err = color_cell_compression(255, &ccell_params[part], &ccell_results[part], &comp_params);721total_part_err += part_err;722} // part723724{725// ASTC726astc_block_desc astc_results;727memset(&astc_results, 0, sizeof(astc_results));728729astc_results.m_dual_plane = false;730astc_results.m_weight_range = 5;731732astc_results.m_ccs = 0;733astc_results.m_subsets = 2;734astc_results.m_partition_seed = g_astc_bc7_common_partitions2[common_pattern].m_astc;735astc_results.m_cem = 8;736737uint32_t p0 = 0;738uint32_t p1 = 1;739if (g_astc_bc7_common_partitions2[common_pattern].m_invert)740std::swap(p0, p1);741742astc_results.m_endpoints[0] = ccell_results[p0].m_astc_low_endpoint.m_c[0];743astc_results.m_endpoints[1] = ccell_results[p0].m_astc_high_endpoint.m_c[0];744astc_results.m_endpoints[2] = ccell_results[p0].m_astc_low_endpoint.m_c[1];745astc_results.m_endpoints[3] = ccell_results[p0].m_astc_high_endpoint.m_c[1];746astc_results.m_endpoints[4] = ccell_results[p0].m_astc_low_endpoint.m_c[2];747astc_results.m_endpoints[5] = ccell_results[p0].m_astc_high_endpoint.m_c[2];748749const uint32_t range = 8;750751bool invert[2] = { false, false };752753int s0 = g_astc_unquant[range][astc_results.m_endpoints[0]].m_unquant + g_astc_unquant[range][astc_results.m_endpoints[2]].m_unquant + g_astc_unquant[range][astc_results.m_endpoints[4]].m_unquant;754int s1 = g_astc_unquant[range][astc_results.m_endpoints[1]].m_unquant + g_astc_unquant[range][astc_results.m_endpoints[3]].m_unquant + g_astc_unquant[range][astc_results.m_endpoints[5]].m_unquant;755if (s1 < s0)756{757std::swap(astc_results.m_endpoints[0], astc_results.m_endpoints[1]);758std::swap(astc_results.m_endpoints[2], astc_results.m_endpoints[3]);759std::swap(astc_results.m_endpoints[4], astc_results.m_endpoints[5]);760invert[0] = true;761}762763astc_results.m_endpoints[6] = ccell_results[p1].m_astc_low_endpoint.m_c[0];764astc_results.m_endpoints[7] = ccell_results[p1].m_astc_high_endpoint.m_c[0];765astc_results.m_endpoints[8] = ccell_results[p1].m_astc_low_endpoint.m_c[1];766astc_results.m_endpoints[9] = ccell_results[p1].m_astc_high_endpoint.m_c[1];767astc_results.m_endpoints[10] = ccell_results[p1].m_astc_low_endpoint.m_c[2];768astc_results.m_endpoints[11] = ccell_results[p1].m_astc_high_endpoint.m_c[2];769770s0 = g_astc_unquant[range][astc_results.m_endpoints[0 + 6]].m_unquant + g_astc_unquant[range][astc_results.m_endpoints[2 + 6]].m_unquant + g_astc_unquant[range][astc_results.m_endpoints[4 + 6]].m_unquant;771s1 = g_astc_unquant[range][astc_results.m_endpoints[1 + 6]].m_unquant + g_astc_unquant[range][astc_results.m_endpoints[3 + 6]].m_unquant + g_astc_unquant[range][astc_results.m_endpoints[5 + 6]].m_unquant;772773if (s1 < s0)774{775std::swap(astc_results.m_endpoints[0 + 6], astc_results.m_endpoints[1 + 6]);776std::swap(astc_results.m_endpoints[2 + 6], astc_results.m_endpoints[3 + 6]);777std::swap(astc_results.m_endpoints[4 + 6], astc_results.m_endpoints[5 + 6]);778invert[1] = true;779}780781for (uint32_t y = 0; y < 4; y++)782{783for (uint32_t x = 0; x < 4; x++)784{785const uint32_t bc7_part = g_bc7_partition2[16 * bc7_pattern + x + y * 4];786787astc_results.m_weights[x + y * 4] = ccell_result_selectors[bc7_part][part_pixel_index[y][x]];788789uint32_t astc_part = bc7_part;790if (g_astc_bc7_common_partitions2[common_pattern].m_invert)791astc_part = 1 - astc_part;792793if (invert[astc_part])794astc_results.m_weights[x + y * 4] = 7 - astc_results.m_weights[x + y * 4];795}796}797798assert(total_results < MAX_ENCODE_RESULTS);799if (total_results < MAX_ENCODE_RESULTS)800{801pResults[total_results].m_uastc_mode = 2;802pResults[total_results].m_common_pattern = common_pattern;803pResults[total_results].m_astc = astc_results;804pResults[total_results].m_astc_err = total_part_err;805total_results++;806}807}808809} // common_pattern810}811812// MODE 3813// 3-subsets, 2-bit indices, [0,11] endpoints, BC7 mode 2814// DualPlane: 0, WeightRange: 2 (4), Subsets: 3, CEM: 8 (RGB Direct ), EndpointRange: 7 (12) MODE2815static void astc_mode3(const color_rgba block[4][4], uastc_encode_results* pResults, uint32_t& total_results, bc7enc_compress_block_params& comp_params, bool estimate_partition)816{817uint32_t first_common_pattern = 0;818uint32_t last_common_pattern = TOTAL_ASTC_BC7_COMMON_PARTITIONS3;819820if (estimate_partition)821{822uint64_t best_err = UINT64_MAX;823uint32_t best_common_pattern = 0;824const uint32_t weights[4] = { 1, 1, 1, 1 };825826for (uint32_t common_pattern = 0; common_pattern < TOTAL_ASTC_BC7_COMMON_PARTITIONS3; common_pattern++)827{828const uint32_t bc7_pattern = g_astc_bc7_common_partitions3[common_pattern].m_bc7;829830const uint8_t* pPartition = &g_bc7_partition3[bc7_pattern * 16];831832color_quad_u8 subset_colors[3][16];833uint32_t subset_total_colors[3] = { 0, 0 };834for (uint32_t index = 0; index < 16; index++)835subset_colors[pPartition[index]][subset_total_colors[pPartition[index]]++] = ((const color_quad_u8*)block)[index];836837uint64_t total_subset_err = 0;838for (uint32_t subset = 0; (subset < 3) && (total_subset_err < best_err); subset++)839total_subset_err += color_cell_compression_est_astc(4, 3, g_bc7_weights2, subset_total_colors[subset], &subset_colors[subset][0], best_err, weights);840841if (total_subset_err < best_err)842{843best_err = total_subset_err;844best_common_pattern = common_pattern;845}846}847848first_common_pattern = best_common_pattern;849last_common_pattern = best_common_pattern + 1;850}851852for (uint32_t common_pattern = first_common_pattern; common_pattern < last_common_pattern; common_pattern++)853{854const uint32_t endpoint_range = 7;855856const uint32_t bc7_pattern = g_astc_bc7_common_partitions3[common_pattern].m_bc7;857858color_rgba part_pixels[3][16];859uint32_t part_pixel_index[4][4];860uint32_t num_part_pixels[3] = { 0, 0, 0 };861862for (uint32_t y = 0; y < 4; y++)863{864for (uint32_t x = 0; x < 4; x++)865{866const uint32_t bc7_part = g_bc7_partition3[16 * bc7_pattern + x + y * 4];867part_pixel_index[y][x] = num_part_pixels[bc7_part];868part_pixels[bc7_part][num_part_pixels[bc7_part]++] = block[y][x];869}870}871872color_cell_compressor_params ccell_params[3];873color_cell_compressor_results ccell_results[3];874uint8_t ccell_result_selectors[3][16];875uint8_t ccell_result_selectors_temp[3][16];876877uint64_t total_part_err = 0;878for (uint32_t bc7_part = 0; bc7_part < 3; bc7_part++)879{880memset(&ccell_params[bc7_part], 0, sizeof(ccell_params[bc7_part]));881882ccell_params[bc7_part].m_num_pixels = num_part_pixels[bc7_part];883ccell_params[bc7_part].m_pPixels = (color_quad_u8*)&part_pixels[bc7_part][0];884ccell_params[bc7_part].m_num_selector_weights = 4;885ccell_params[bc7_part].m_pSelector_weights = g_bc7_weights2;886ccell_params[bc7_part].m_pSelector_weightsx = (const bc7enc_vec4F*)g_bc7_weights2x;887ccell_params[bc7_part].m_astc_endpoint_range = endpoint_range;888ccell_params[bc7_part].m_weights[0] = 1;889ccell_params[bc7_part].m_weights[1] = 1;890ccell_params[bc7_part].m_weights[2] = 1;891ccell_params[bc7_part].m_weights[3] = 1;892893memset(&ccell_results[bc7_part], 0, sizeof(ccell_results[bc7_part]));894ccell_results[bc7_part].m_pSelectors = &ccell_result_selectors[bc7_part][0];895ccell_results[bc7_part].m_pSelectors_temp = &ccell_result_selectors_temp[bc7_part][0];896897uint64_t part_err = color_cell_compression(255, &ccell_params[bc7_part], &ccell_results[bc7_part], &comp_params);898total_part_err += part_err;899} // part900901{902// ASTC903astc_block_desc astc_results;904memset(&astc_results, 0, sizeof(astc_results));905906astc_results.m_dual_plane = false;907astc_results.m_weight_range = 2;908909astc_results.m_ccs = 0;910astc_results.m_subsets = 3;911astc_results.m_partition_seed = g_astc_bc7_common_partitions3[common_pattern].m_astc;912astc_results.m_cem = 8;913914uint32_t astc_to_bc7_part[3]; // converts ASTC to BC7 partition index915const uint32_t perm = g_astc_bc7_common_partitions3[common_pattern].m_astc_to_bc7_perm;916astc_to_bc7_part[0] = g_astc_to_bc7_partition_index_perm_tables[perm][0];917astc_to_bc7_part[1] = g_astc_to_bc7_partition_index_perm_tables[perm][1];918astc_to_bc7_part[2] = g_astc_to_bc7_partition_index_perm_tables[perm][2];919920bool invert_astc_part[3] = { false, false, false };921922for (uint32_t astc_part = 0; astc_part < 3; astc_part++)923{924uint8_t* pEndpoints = &astc_results.m_endpoints[6 * astc_part];925926pEndpoints[0] = ccell_results[astc_to_bc7_part[astc_part]].m_astc_low_endpoint.m_c[0];927pEndpoints[1] = ccell_results[astc_to_bc7_part[astc_part]].m_astc_high_endpoint.m_c[0];928pEndpoints[2] = ccell_results[astc_to_bc7_part[astc_part]].m_astc_low_endpoint.m_c[1];929pEndpoints[3] = ccell_results[astc_to_bc7_part[astc_part]].m_astc_high_endpoint.m_c[1];930pEndpoints[4] = ccell_results[astc_to_bc7_part[astc_part]].m_astc_low_endpoint.m_c[2];931pEndpoints[5] = ccell_results[astc_to_bc7_part[astc_part]].m_astc_high_endpoint.m_c[2];932933int s0 = g_astc_unquant[endpoint_range][pEndpoints[0]].m_unquant + g_astc_unquant[endpoint_range][pEndpoints[2]].m_unquant + g_astc_unquant[endpoint_range][pEndpoints[4]].m_unquant;934int s1 = g_astc_unquant[endpoint_range][pEndpoints[1]].m_unquant + g_astc_unquant[endpoint_range][pEndpoints[3]].m_unquant + g_astc_unquant[endpoint_range][pEndpoints[5]].m_unquant;935if (s1 < s0)936{937std::swap(pEndpoints[0], pEndpoints[1]);938std::swap(pEndpoints[2], pEndpoints[3]);939std::swap(pEndpoints[4], pEndpoints[5]);940invert_astc_part[astc_part] = true;941}942}943944for (uint32_t y = 0; y < 4; y++)945{946for (uint32_t x = 0; x < 4; x++)947{948const uint32_t bc7_part = g_bc7_partition3[16 * bc7_pattern + x + y * 4];949950astc_results.m_weights[x + y * 4] = ccell_result_selectors[bc7_part][part_pixel_index[y][x]];951952uint32_t astc_part = 0;953for (uint32_t i = 0; i < 3; i++)954{955if (astc_to_bc7_part[i] == bc7_part)956{957astc_part = i;958break;959}960}961962if (invert_astc_part[astc_part])963astc_results.m_weights[x + y * 4] = 3 - astc_results.m_weights[x + y * 4];964}965}966967assert(total_results < MAX_ENCODE_RESULTS);968if (total_results < MAX_ENCODE_RESULTS)969{970pResults[total_results].m_uastc_mode = 3;971pResults[total_results].m_common_pattern = common_pattern;972pResults[total_results].m_astc = astc_results;973pResults[total_results].m_astc_err = total_part_err;974total_results++;975}976977}978979} // common_pattern980}981982// MODE 4983// DualPlane: 0, WeightRange: 2 (4), Subsets: 2, CEM: 8 (RGB Direct ), EndpointRange: 12 (40) MODE3984static void astc_mode4(const color_rgba block[4][4], uastc_encode_results* pResults, uint32_t& total_results, bc7enc_compress_block_params& comp_params, bool estimate_partition)985{986//const uint32_t weight_range = 2;987const uint32_t endpoint_range = 12;988989uint32_t first_common_pattern = 0;990uint32_t last_common_pattern = TOTAL_ASTC_BC7_COMMON_PARTITIONS2;991992if (estimate_partition)993{994const uint32_t weights[4] = { 1, 1, 1, 1 };995first_common_pattern = estimate_partition2(4, 3, g_bc7_weights2, block, weights);996last_common_pattern = first_common_pattern + 1;997}998999for (uint32_t common_pattern = first_common_pattern; common_pattern < last_common_pattern; common_pattern++)1000{1001const uint32_t bc7_pattern = g_astc_bc7_common_partitions2[common_pattern].m_bc7;10021003color_rgba part_pixels[2][16];1004uint32_t part_pixel_index[4][4];1005uint32_t num_part_pixels[2] = { 0, 0 };10061007for (uint32_t y = 0; y < 4; y++)1008{1009for (uint32_t x = 0; x < 4; x++)1010{1011const uint32_t part = g_bc7_partition2[16 * bc7_pattern + x + y * 4];1012part_pixel_index[y][x] = num_part_pixels[part];1013part_pixels[part][num_part_pixels[part]++] = block[y][x];1014}1015}10161017color_cell_compressor_params ccell_params[2];1018color_cell_compressor_results ccell_results[2];1019uint8_t ccell_result_selectors[2][16];1020uint8_t ccell_result_selectors_temp[2][16];10211022uint64_t total_part_err = 0;1023for (uint32_t part = 0; part < 2; part++)1024{1025memset(&ccell_params[part], 0, sizeof(ccell_params[part]));10261027ccell_params[part].m_num_pixels = num_part_pixels[part];1028ccell_params[part].m_pPixels = (color_quad_u8*)&part_pixels[part][0];1029ccell_params[part].m_num_selector_weights = 4;1030ccell_params[part].m_pSelector_weights = g_bc7_weights2;1031ccell_params[part].m_pSelector_weightsx = (const bc7enc_vec4F*)g_bc7_weights2x;1032ccell_params[part].m_astc_endpoint_range = endpoint_range;1033ccell_params[part].m_weights[0] = 1;1034ccell_params[part].m_weights[1] = 1;1035ccell_params[part].m_weights[2] = 1;1036ccell_params[part].m_weights[3] = 1;10371038memset(&ccell_results[part], 0, sizeof(ccell_results[part]));1039ccell_results[part].m_pSelectors = &ccell_result_selectors[part][0];1040ccell_results[part].m_pSelectors_temp = &ccell_result_selectors_temp[part][0];10411042uint64_t part_err = color_cell_compression(255, &ccell_params[part], &ccell_results[part], &comp_params);1043total_part_err += part_err;1044} // part10451046// ASTC1047astc_block_desc astc_results;1048memset(&astc_results, 0, sizeof(astc_results));10491050astc_results.m_dual_plane = false;1051astc_results.m_weight_range = 2;10521053astc_results.m_ccs = 0;1054astc_results.m_subsets = 2;1055astc_results.m_partition_seed = g_astc_bc7_common_partitions2[common_pattern].m_astc;1056astc_results.m_cem = 8;10571058uint32_t p0 = 0;1059uint32_t p1 = 1;1060if (g_astc_bc7_common_partitions2[common_pattern].m_invert)1061std::swap(p0, p1);10621063astc_results.m_endpoints[0] = ccell_results[p0].m_astc_low_endpoint.m_c[0];1064astc_results.m_endpoints[1] = ccell_results[p0].m_astc_high_endpoint.m_c[0];1065astc_results.m_endpoints[2] = ccell_results[p0].m_astc_low_endpoint.m_c[1];1066astc_results.m_endpoints[3] = ccell_results[p0].m_astc_high_endpoint.m_c[1];1067astc_results.m_endpoints[4] = ccell_results[p0].m_astc_low_endpoint.m_c[2];1068astc_results.m_endpoints[5] = ccell_results[p0].m_astc_high_endpoint.m_c[2];10691070bool invert[2] = { false, false };10711072int s0 = g_astc_unquant[endpoint_range][astc_results.m_endpoints[0]].m_unquant + g_astc_unquant[endpoint_range][astc_results.m_endpoints[2]].m_unquant + g_astc_unquant[endpoint_range][astc_results.m_endpoints[4]].m_unquant;1073int s1 = g_astc_unquant[endpoint_range][astc_results.m_endpoints[1]].m_unquant + g_astc_unquant[endpoint_range][astc_results.m_endpoints[3]].m_unquant + g_astc_unquant[endpoint_range][astc_results.m_endpoints[5]].m_unquant;1074if (s1 < s0)1075{1076std::swap(astc_results.m_endpoints[0], astc_results.m_endpoints[1]);1077std::swap(astc_results.m_endpoints[2], astc_results.m_endpoints[3]);1078std::swap(astc_results.m_endpoints[4], astc_results.m_endpoints[5]);1079invert[0] = true;1080}10811082astc_results.m_endpoints[6] = ccell_results[p1].m_astc_low_endpoint.m_c[0];1083astc_results.m_endpoints[7] = ccell_results[p1].m_astc_high_endpoint.m_c[0];1084astc_results.m_endpoints[8] = ccell_results[p1].m_astc_low_endpoint.m_c[1];1085astc_results.m_endpoints[9] = ccell_results[p1].m_astc_high_endpoint.m_c[1];1086astc_results.m_endpoints[10] = ccell_results[p1].m_astc_low_endpoint.m_c[2];1087astc_results.m_endpoints[11] = ccell_results[p1].m_astc_high_endpoint.m_c[2];10881089s0 = g_astc_unquant[endpoint_range][astc_results.m_endpoints[0 + 6]].m_unquant + g_astc_unquant[endpoint_range][astc_results.m_endpoints[2 + 6]].m_unquant + g_astc_unquant[endpoint_range][astc_results.m_endpoints[4 + 6]].m_unquant;1090s1 = g_astc_unquant[endpoint_range][astc_results.m_endpoints[1 + 6]].m_unquant + g_astc_unquant[endpoint_range][astc_results.m_endpoints[3 + 6]].m_unquant + g_astc_unquant[endpoint_range][astc_results.m_endpoints[5 + 6]].m_unquant;10911092if (s1 < s0)1093{1094std::swap(astc_results.m_endpoints[0 + 6], astc_results.m_endpoints[1 + 6]);1095std::swap(astc_results.m_endpoints[2 + 6], astc_results.m_endpoints[3 + 6]);1096std::swap(astc_results.m_endpoints[4 + 6], astc_results.m_endpoints[5 + 6]);1097invert[1] = true;1098}10991100for (uint32_t y = 0; y < 4; y++)1101{1102for (uint32_t x = 0; x < 4; x++)1103{1104const uint32_t bc7_part = g_bc7_partition2[16 * bc7_pattern + x + y * 4];11051106astc_results.m_weights[x + y * 4] = ccell_result_selectors[bc7_part][part_pixel_index[y][x]];11071108uint32_t astc_part = bc7_part;1109if (g_astc_bc7_common_partitions2[common_pattern].m_invert)1110astc_part = 1 - astc_part;11111112if (invert[astc_part])1113astc_results.m_weights[x + y * 4] = 3 - astc_results.m_weights[x + y * 4];1114}1115}11161117assert(total_results < MAX_ENCODE_RESULTS);1118if (total_results < MAX_ENCODE_RESULTS)1119{1120pResults[total_results].m_uastc_mode = 4;1121pResults[total_results].m_common_pattern = common_pattern;1122pResults[total_results].m_astc = astc_results;1123pResults[total_results].m_astc_err = total_part_err;1124total_results++;1125}11261127} // common_pattern1128}11291130// MODE 51131// DualPlane: 0, WeightRange: 5 (8), Subsets: 1, CEM: 8 (RGB Direct ), EndpointRange: 20 (256) BC7 MODE 6 (or MODE 1 1-subset)1132static void astc_mode5(const color_rgba block[4][4], uastc_encode_results* pResults, uint32_t& total_results, bc7enc_compress_block_params& comp_params)1133{1134const uint32_t weight_range = 5;1135const uint32_t endpoint_range = 20;11361137color_cell_compressor_params ccell_params;1138memset(&ccell_params, 0, sizeof(ccell_params));11391140ccell_params.m_num_pixels = 16;1141ccell_params.m_pPixels = (color_quad_u8*)&block[0][0];1142ccell_params.m_num_selector_weights = 8;1143ccell_params.m_pSelector_weights = g_bc7_weights3;1144ccell_params.m_pSelector_weightsx = (const bc7enc_vec4F*)g_bc7_weights3x;1145ccell_params.m_astc_endpoint_range = endpoint_range;1146ccell_params.m_weights[0] = 1;1147ccell_params.m_weights[1] = 1;1148ccell_params.m_weights[2] = 1;1149ccell_params.m_weights[3] = 1;11501151color_cell_compressor_results ccell_results;1152uint8_t ccell_result_selectors[16];1153uint8_t ccell_result_selectors_temp[16];1154memset(&ccell_results, 0, sizeof(ccell_results));1155ccell_results.m_pSelectors = &ccell_result_selectors[0];1156ccell_results.m_pSelectors_temp = &ccell_result_selectors_temp[0];11571158uint64_t part_err = color_cell_compression(255, &ccell_params, &ccell_results, &comp_params);11591160// ASTC1161astc_block_desc blk;1162memset(&blk, 0, sizeof(blk));11631164blk.m_dual_plane = false;1165blk.m_weight_range = weight_range;11661167blk.m_ccs = 0;1168blk.m_subsets = 1;1169blk.m_partition_seed = 0;1170blk.m_cem = 8;11711172blk.m_endpoints[0] = ccell_results.m_astc_low_endpoint.m_c[0];1173blk.m_endpoints[1] = ccell_results.m_astc_high_endpoint.m_c[0];1174blk.m_endpoints[2] = ccell_results.m_astc_low_endpoint.m_c[1];1175blk.m_endpoints[3] = ccell_results.m_astc_high_endpoint.m_c[1];1176blk.m_endpoints[4] = ccell_results.m_astc_low_endpoint.m_c[2];1177blk.m_endpoints[5] = ccell_results.m_astc_high_endpoint.m_c[2];11781179bool invert = false;11801181int s0 = g_astc_unquant[endpoint_range][blk.m_endpoints[0]].m_unquant + g_astc_unquant[endpoint_range][blk.m_endpoints[2]].m_unquant + g_astc_unquant[endpoint_range][blk.m_endpoints[4]].m_unquant;1182int s1 = g_astc_unquant[endpoint_range][blk.m_endpoints[1]].m_unquant + g_astc_unquant[endpoint_range][blk.m_endpoints[3]].m_unquant + g_astc_unquant[endpoint_range][blk.m_endpoints[5]].m_unquant;1183if (s1 < s0)1184{1185std::swap(blk.m_endpoints[0], blk.m_endpoints[1]);1186std::swap(blk.m_endpoints[2], blk.m_endpoints[3]);1187std::swap(blk.m_endpoints[4], blk.m_endpoints[5]);1188invert = true;1189}11901191for (uint32_t y = 0; y < 4; y++)1192{1193for (uint32_t x = 0; x < 4; x++)1194{1195blk.m_weights[x + y * 4] = ccell_result_selectors[x + y * 4];11961197if (invert)1198blk.m_weights[x + y * 4] = 7 - blk.m_weights[x + y * 4];1199}1200}12011202assert(total_results < MAX_ENCODE_RESULTS);1203if (total_results < MAX_ENCODE_RESULTS)1204{1205pResults[total_results].m_uastc_mode = 5;1206pResults[total_results].m_common_pattern = 0;1207pResults[total_results].m_astc = blk;1208pResults[total_results].m_astc_err = part_err;1209total_results++;1210}1211}12121213// MODE 61214// DualPlane: 1, WeightRange: 2 (4), Subsets: 1, CEM: 8 (RGB Direct ), EndpointRange: 18 (160) BC7 MODE51215static void astc_mode6(const color_rgba block[4][4], uastc_encode_results* pResults, uint32_t& total_results, bc7enc_compress_block_params& comp_params)1216{1217for (uint32_t rot_comp = 0; rot_comp < 3; rot_comp++)1218{1219const uint32_t weight_range = 2;1220const uint32_t endpoint_range = 18;12211222color_quad_u8 block_rgb[16];1223color_quad_u8 block_a[16];1224for (uint32_t i = 0; i < 16; i++)1225{1226block_rgb[i] = ((color_quad_u8*)&block[0][0])[i];1227block_a[i] = block_rgb[i];12281229uint8_t c = block_a[i].m_c[rot_comp];1230block_a[i].m_c[0] = c;1231block_a[i].m_c[1] = c;1232block_a[i].m_c[2] = c;1233block_a[i].m_c[3] = 255;12341235block_rgb[i].m_c[rot_comp] = 255;1236}12371238uint8_t ccell_result_selectors_temp[16];12391240color_cell_compressor_params ccell_params_rgb;1241memset(&ccell_params_rgb, 0, sizeof(ccell_params_rgb));12421243ccell_params_rgb.m_num_pixels = 16;1244ccell_params_rgb.m_pPixels = block_rgb;1245ccell_params_rgb.m_num_selector_weights = 4;1246ccell_params_rgb.m_pSelector_weights = g_bc7_weights2;1247ccell_params_rgb.m_pSelector_weightsx = (const bc7enc_vec4F*)g_bc7_weights2x;1248ccell_params_rgb.m_astc_endpoint_range = endpoint_range;1249ccell_params_rgb.m_weights[0] = 1;1250ccell_params_rgb.m_weights[1] = 1;1251ccell_params_rgb.m_weights[2] = 1;1252ccell_params_rgb.m_weights[3] = 1;12531254color_cell_compressor_results ccell_results_rgb;1255uint8_t ccell_result_selectors_rgb[16];1256memset(&ccell_results_rgb, 0, sizeof(ccell_results_rgb));1257ccell_results_rgb.m_pSelectors = &ccell_result_selectors_rgb[0];1258ccell_results_rgb.m_pSelectors_temp = &ccell_result_selectors_temp[0];12591260uint64_t part_err_rgb = color_cell_compression(255, &ccell_params_rgb, &ccell_results_rgb, &comp_params);12611262color_cell_compressor_params ccell_params_a;1263memset(&ccell_params_a, 0, sizeof(ccell_params_a));12641265ccell_params_a.m_num_pixels = 16;1266ccell_params_a.m_pPixels = block_a;1267ccell_params_a.m_num_selector_weights = 4;1268ccell_params_a.m_pSelector_weights = g_bc7_weights2;1269ccell_params_a.m_pSelector_weightsx = (const bc7enc_vec4F*)g_bc7_weights2x;1270ccell_params_a.m_astc_endpoint_range = endpoint_range;1271ccell_params_a.m_weights[0] = 1;1272ccell_params_a.m_weights[1] = 1;1273ccell_params_a.m_weights[2] = 1;1274ccell_params_a.m_weights[3] = 1;12751276color_cell_compressor_results ccell_results_a;1277uint8_t ccell_result_selectors_a[16];1278memset(&ccell_results_a, 0, sizeof(ccell_results_a));1279ccell_results_a.m_pSelectors = &ccell_result_selectors_a[0];1280ccell_results_a.m_pSelectors_temp = &ccell_result_selectors_temp[0];12811282uint64_t part_err_a = color_cell_compression(255, &ccell_params_a, &ccell_results_a, &comp_params) / 3;12831284uint64_t total_err = part_err_rgb + part_err_a;12851286// ASTC1287astc_block_desc blk;1288memset(&blk, 0, sizeof(blk));12891290blk.m_dual_plane = true;1291blk.m_weight_range = weight_range;12921293blk.m_ccs = rot_comp;1294blk.m_subsets = 1;1295blk.m_partition_seed = 0;1296blk.m_cem = 8;12971298blk.m_endpoints[0] = (rot_comp == 0 ? ccell_results_a : ccell_results_rgb).m_astc_low_endpoint.m_c[0];1299blk.m_endpoints[1] = (rot_comp == 0 ? ccell_results_a : ccell_results_rgb).m_astc_high_endpoint.m_c[0];1300blk.m_endpoints[2] = (rot_comp == 1 ? ccell_results_a : ccell_results_rgb).m_astc_low_endpoint.m_c[1];1301blk.m_endpoints[3] = (rot_comp == 1 ? ccell_results_a : ccell_results_rgb).m_astc_high_endpoint.m_c[1];1302blk.m_endpoints[4] = (rot_comp == 2 ? ccell_results_a : ccell_results_rgb).m_astc_low_endpoint.m_c[2];1303blk.m_endpoints[5] = (rot_comp == 2 ? ccell_results_a : ccell_results_rgb).m_astc_high_endpoint.m_c[2];13041305bool invert = false;13061307int s0 = g_astc_unquant[endpoint_range][blk.m_endpoints[0]].m_unquant + g_astc_unquant[endpoint_range][blk.m_endpoints[2]].m_unquant + g_astc_unquant[endpoint_range][blk.m_endpoints[4]].m_unquant;1308int s1 = g_astc_unquant[endpoint_range][blk.m_endpoints[1]].m_unquant + g_astc_unquant[endpoint_range][blk.m_endpoints[3]].m_unquant + g_astc_unquant[endpoint_range][blk.m_endpoints[5]].m_unquant;1309if (s1 < s0)1310{1311std::swap(blk.m_endpoints[0], blk.m_endpoints[1]);1312std::swap(blk.m_endpoints[2], blk.m_endpoints[3]);1313std::swap(blk.m_endpoints[4], blk.m_endpoints[5]);1314invert = true;1315}13161317for (uint32_t y = 0; y < 4; y++)1318{1319for (uint32_t x = 0; x < 4; x++)1320{1321uint32_t rgb_index = ccell_result_selectors_rgb[x + y * 4];1322uint32_t a_index = ccell_result_selectors_a[x + y * 4];13231324if (invert)1325{1326rgb_index = 3 - rgb_index;1327a_index = 3 - a_index;1328}13291330blk.m_weights[(x + y * 4) * 2 + 0] = (uint8_t)rgb_index;1331blk.m_weights[(x + y * 4) * 2 + 1] = (uint8_t)a_index;1332}1333}13341335assert(total_results < MAX_ENCODE_RESULTS);1336if (total_results < MAX_ENCODE_RESULTS)1337{1338pResults[total_results].m_uastc_mode = 6;1339pResults[total_results].m_common_pattern = 0;1340pResults[total_results].m_astc = blk;1341pResults[total_results].m_astc_err = total_err;1342total_results++;1343}1344} // rot_comp1345}13461347// MODE 7 - 2 subset ASTC, 3 subset BC71348// DualPlane: 0, WeightRange: 2 (4), Subsets: 2, CEM: 8 (RGB Direct ), EndpointRange: 12 (40) MODE21349static void astc_mode7(const color_rgba block[4][4], uastc_encode_results* pResults, uint32_t& total_results, bc7enc_compress_block_params& comp_params, bool estimate_partition)1350{1351uint32_t first_common_pattern = 0;1352uint32_t last_common_pattern = TOTAL_BC7_3_ASTC2_COMMON_PARTITIONS;13531354if (estimate_partition)1355{1356uint64_t best_err = UINT64_MAX;1357uint32_t best_common_pattern = 0;1358const uint32_t weights[4] = { 1, 1, 1, 1 };13591360for (uint32_t common_pattern = 0; common_pattern < TOTAL_BC7_3_ASTC2_COMMON_PARTITIONS; common_pattern++)1361{1362const uint8_t* pPartition = &g_bc7_3_astc2_patterns2[common_pattern][0];13631364#ifdef _DEBUG1365const uint32_t astc_pattern = g_bc7_3_astc2_common_partitions[common_pattern].m_astc2;1366const uint32_t bc7_pattern = g_bc7_3_astc2_common_partitions[common_pattern].m_bc73;1367const uint32_t common_pattern_k = g_bc7_3_astc2_common_partitions[common_pattern].k;13681369for (uint32_t y = 0; y < 4; y++)1370{1371for (uint32_t x = 0; x < 4; x++)1372{1373const uint32_t astc_part = bc7_convert_partition_index_3_to_2(g_bc7_partition3[16 * bc7_pattern + x + y * 4], common_pattern_k);1374assert((int)astc_part == astc_compute_texel_partition(astc_pattern, x, y, 0, 2, true));1375assert(astc_part == pPartition[x + y * 4]);1376}1377}1378#endif13791380color_quad_u8 subset_colors[2][16];1381uint32_t subset_total_colors[2] = { 0, 0 };1382for (uint32_t index = 0; index < 16; index++)1383subset_colors[pPartition[index]][subset_total_colors[pPartition[index]]++] = ((const color_quad_u8*)block)[index];13841385uint64_t total_subset_err = 0;1386for (uint32_t subset = 0; (subset < 2) && (total_subset_err < best_err); subset++)1387total_subset_err += color_cell_compression_est_astc(4, 3, g_bc7_weights2, subset_total_colors[subset], &subset_colors[subset][0], best_err, weights);13881389if (total_subset_err < best_err)1390{1391best_err = total_subset_err;1392best_common_pattern = common_pattern;1393}1394}13951396first_common_pattern = best_common_pattern;1397last_common_pattern = best_common_pattern + 1;1398}13991400//const uint32_t weight_range = 2;1401const uint32_t endpoint_range = 12;14021403for (uint32_t common_pattern = first_common_pattern; common_pattern < last_common_pattern; common_pattern++)1404{1405const uint32_t astc_pattern = g_bc7_3_astc2_common_partitions[common_pattern].m_astc2;1406const uint32_t bc7_pattern = g_bc7_3_astc2_common_partitions[common_pattern].m_bc73;1407const uint32_t common_pattern_k = g_bc7_3_astc2_common_partitions[common_pattern].k;14081409color_rgba part_pixels[2][16];1410uint32_t part_pixel_index[4][4];1411uint32_t num_part_pixels[2] = { 0, 0 };14121413for (uint32_t y = 0; y < 4; y++)1414{1415for (uint32_t x = 0; x < 4; x++)1416{1417const uint32_t astc_part = bc7_convert_partition_index_3_to_2(g_bc7_partition3[16 * bc7_pattern + x + y * 4], common_pattern_k);1418#ifdef _DEBUG1419assert((int)astc_part == astc_compute_texel_partition(astc_pattern, x, y, 0, 2, true));1420#endif14211422part_pixel_index[y][x] = num_part_pixels[astc_part];1423part_pixels[astc_part][num_part_pixels[astc_part]++] = block[y][x];1424}1425}14261427color_cell_compressor_params ccell_params[2];1428color_cell_compressor_results ccell_results[2];1429uint8_t ccell_result_selectors[2][16];1430uint8_t ccell_result_selectors_temp[2][16];14311432uint64_t total_part_err = 0;1433for (uint32_t part = 0; part < 2; part++)1434{1435memset(&ccell_params[part], 0, sizeof(ccell_params[part]));14361437ccell_params[part].m_num_pixels = num_part_pixels[part];1438ccell_params[part].m_pPixels = (color_quad_u8*)&part_pixels[part][0];1439ccell_params[part].m_num_selector_weights = 4;1440ccell_params[part].m_pSelector_weights = g_bc7_weights2;1441ccell_params[part].m_pSelector_weightsx = (const bc7enc_vec4F*)g_bc7_weights2x;1442ccell_params[part].m_astc_endpoint_range = endpoint_range;1443ccell_params[part].m_weights[0] = 1;1444ccell_params[part].m_weights[1] = 1;1445ccell_params[part].m_weights[2] = 1;1446ccell_params[part].m_weights[3] = 1;14471448memset(&ccell_results[part], 0, sizeof(ccell_results[part]));1449ccell_results[part].m_pSelectors = &ccell_result_selectors[part][0];1450ccell_results[part].m_pSelectors_temp = &ccell_result_selectors_temp[part][0];14511452uint64_t part_err = color_cell_compression(255, &ccell_params[part], &ccell_results[part], &comp_params);1453total_part_err += part_err;1454} // part14551456// ASTC1457astc_block_desc blk;1458memset(&blk, 0, sizeof(blk));14591460blk.m_dual_plane = false;1461blk.m_weight_range = 2;14621463blk.m_ccs = 0;1464blk.m_subsets = 2;1465blk.m_partition_seed = astc_pattern;1466blk.m_cem = 8;14671468const uint32_t p0 = 0;1469const uint32_t p1 = 1;14701471blk.m_endpoints[0] = ccell_results[p0].m_astc_low_endpoint.m_c[0];1472blk.m_endpoints[1] = ccell_results[p0].m_astc_high_endpoint.m_c[0];1473blk.m_endpoints[2] = ccell_results[p0].m_astc_low_endpoint.m_c[1];1474blk.m_endpoints[3] = ccell_results[p0].m_astc_high_endpoint.m_c[1];1475blk.m_endpoints[4] = ccell_results[p0].m_astc_low_endpoint.m_c[2];1476blk.m_endpoints[5] = ccell_results[p0].m_astc_high_endpoint.m_c[2];14771478bool invert[2] = { false, false };14791480int s0 = g_astc_unquant[endpoint_range][blk.m_endpoints[0]].m_unquant + g_astc_unquant[endpoint_range][blk.m_endpoints[2]].m_unquant + g_astc_unquant[endpoint_range][blk.m_endpoints[4]].m_unquant;1481int s1 = g_astc_unquant[endpoint_range][blk.m_endpoints[1]].m_unquant + g_astc_unquant[endpoint_range][blk.m_endpoints[3]].m_unquant + g_astc_unquant[endpoint_range][blk.m_endpoints[5]].m_unquant;1482if (s1 < s0)1483{1484std::swap(blk.m_endpoints[0], blk.m_endpoints[1]);1485std::swap(blk.m_endpoints[2], blk.m_endpoints[3]);1486std::swap(blk.m_endpoints[4], blk.m_endpoints[5]);1487invert[0] = true;1488}14891490blk.m_endpoints[6] = ccell_results[p1].m_astc_low_endpoint.m_c[0];1491blk.m_endpoints[7] = ccell_results[p1].m_astc_high_endpoint.m_c[0];1492blk.m_endpoints[8] = ccell_results[p1].m_astc_low_endpoint.m_c[1];1493blk.m_endpoints[9] = ccell_results[p1].m_astc_high_endpoint.m_c[1];1494blk.m_endpoints[10] = ccell_results[p1].m_astc_low_endpoint.m_c[2];1495blk.m_endpoints[11] = ccell_results[p1].m_astc_high_endpoint.m_c[2];14961497s0 = g_astc_unquant[endpoint_range][blk.m_endpoints[0 + 6]].m_unquant + g_astc_unquant[endpoint_range][blk.m_endpoints[2 + 6]].m_unquant + g_astc_unquant[endpoint_range][blk.m_endpoints[4 + 6]].m_unquant;1498s1 = g_astc_unquant[endpoint_range][blk.m_endpoints[1 + 6]].m_unquant + g_astc_unquant[endpoint_range][blk.m_endpoints[3 + 6]].m_unquant + g_astc_unquant[endpoint_range][blk.m_endpoints[5 + 6]].m_unquant;14991500if (s1 < s0)1501{1502std::swap(blk.m_endpoints[0 + 6], blk.m_endpoints[1 + 6]);1503std::swap(blk.m_endpoints[2 + 6], blk.m_endpoints[3 + 6]);1504std::swap(blk.m_endpoints[4 + 6], blk.m_endpoints[5 + 6]);1505invert[1] = true;1506}15071508for (uint32_t y = 0; y < 4; y++)1509{1510for (uint32_t x = 0; x < 4; x++)1511{1512const uint32_t astc_part = bc7_convert_partition_index_3_to_2(g_bc7_partition3[16 * bc7_pattern + x + y * 4], common_pattern_k);15131514blk.m_weights[x + y * 4] = ccell_result_selectors[astc_part][part_pixel_index[y][x]];15151516if (invert[astc_part])1517blk.m_weights[x + y * 4] = 3 - blk.m_weights[x + y * 4];1518}1519}15201521assert(total_results < MAX_ENCODE_RESULTS);1522if (total_results < MAX_ENCODE_RESULTS)1523{1524pResults[total_results].m_uastc_mode = 7;1525pResults[total_results].m_common_pattern = common_pattern;1526pResults[total_results].m_astc = blk;1527pResults[total_results].m_astc_err = total_part_err;1528total_results++;1529}15301531} // common_pattern1532}15331534static void estimate_partition2_list(uint32_t num_weights, uint32_t num_comps, const uint32_t* pWeights, const color_rgba block[4][4], uint32_t* pParts, uint32_t max_parts, const uint32_t weights[4])1535{1536assert(pWeights[0] == 0 && pWeights[num_weights - 1] == 64);15371538const uint32_t MAX_PARTS = 8;1539assert(max_parts <= MAX_PARTS);15401541uint64_t part_error[MAX_PARTS];1542memset(part_error, 0xFF, sizeof(part_error));1543memset(pParts, 0, sizeof(pParts[0]) * max_parts);15441545for (uint32_t common_pattern = 0; common_pattern < TOTAL_ASTC_BC7_COMMON_PARTITIONS2; common_pattern++)1546{1547const uint32_t bc7_pattern = g_astc_bc7_common_partitions2[common_pattern].m_bc7;15481549const uint8_t* pPartition = &g_bc7_partition2[bc7_pattern * 16];15501551color_quad_u8 subset_colors[2][16];1552uint32_t subset_total_colors[2] = { 0, 0 };1553for (uint32_t index = 0; index < 16; index++)1554subset_colors[pPartition[index]][subset_total_colors[pPartition[index]]++] = ((const color_quad_u8*)block)[index];15551556uint64_t total_subset_err = 0;1557for (uint32_t subset = 0; subset < 2; subset++)1558total_subset_err += color_cell_compression_est_astc(num_weights, num_comps, pWeights, subset_total_colors[subset], &subset_colors[subset][0], UINT64_MAX, weights);15591560for (int i = 0; i < (int)max_parts; i++)1561{1562if (total_subset_err < part_error[i])1563{1564for (int j = max_parts - 1; j > i; --j)1565{1566pParts[j] = pParts[j - 1];1567part_error[j] = part_error[j - 1];1568}15691570pParts[i] = common_pattern;1571part_error[i] = total_subset_err;15721573break;1574}1575}1576}15771578#ifdef _DEBUG1579for (uint32_t i = 0; i < max_parts - 1; i++)1580{1581assert(part_error[i] <= part_error[i + 1]);1582}1583#endif1584}15851586// 9. DualPlane: 0, WeightRange: 2 (4), Subsets: 2, CEM: 12 (RGBA Direct), EndpointRange: 8 (16) - BC7 MODE 71587// 16. DualPlane: 0, WeightRange : 2 (4), Subsets : 2, CEM: 4 (LA Direct), EndpointRange : 20 (256) - BC7 MODE 71588static void astc_mode9_or_16(uint32_t mode, const color_rgba source_block[4][4], uastc_encode_results* pResults, uint32_t& total_results, bc7enc_compress_block_params& comp_params, uint32_t estimate_partition_list_size)1589{1590assert(mode == 9 || mode == 16);15911592const color_rgba* pBlock = &source_block[0][0];15931594color_rgba temp_block[16];1595if (mode == 16)1596{1597for (uint32_t i = 0; i < 16; i++)1598{1599if (mode == 16)1600{1601assert(pBlock[i].r == pBlock[i].g);1602assert(pBlock[i].r == pBlock[i].b);1603}16041605const uint32_t l = pBlock[i].r;1606const uint32_t a = pBlock[i].a;16071608// Use (l,0,0,a) not (l,l,l,a) so both components are treated equally.1609temp_block[i].set_noclamp_rgba(l, 0, 0, a);1610}16111612pBlock = temp_block;1613}16141615const uint32_t weights[4] = { 1, 1, 1, 1 };16161617//const uint32_t weight_range = 2;1618const uint32_t endpoint_range = (mode == 16) ? 20 : 8;16191620uint32_t first_common_pattern = 0;1621uint32_t last_common_pattern = TOTAL_ASTC_BC7_COMMON_PARTITIONS2;1622bool use_part_list = false;16231624const uint32_t MAX_PARTS = 8;1625uint32_t parts[MAX_PARTS];16261627if (estimate_partition_list_size == 1)1628{1629first_common_pattern = estimate_partition2(4, 4, g_bc7_weights2, (const color_rgba(*)[4])pBlock, weights);1630last_common_pattern = first_common_pattern + 1;1631}1632else if (estimate_partition_list_size > 0)1633{1634assert(estimate_partition_list_size <= MAX_PARTS);1635estimate_partition_list_size = basisu::minimum(estimate_partition_list_size, MAX_PARTS);16361637estimate_partition2_list(4, 4, g_bc7_weights2, (const color_rgba(*)[4])pBlock, parts, estimate_partition_list_size, weights);16381639first_common_pattern = 0;1640last_common_pattern = estimate_partition_list_size;1641use_part_list = true;16421643#ifdef _DEBUG1644assert(parts[0] == estimate_partition2(4, 4, g_bc7_weights2, (const color_rgba(*)[4])pBlock, weights));1645#endif1646}16471648for (uint32_t common_pattern_iter = first_common_pattern; common_pattern_iter < last_common_pattern; common_pattern_iter++)1649{1650const uint32_t common_pattern = use_part_list ? parts[common_pattern_iter] : common_pattern_iter;16511652const uint32_t bc7_pattern = g_astc_bc7_common_partitions2[common_pattern].m_bc7;16531654color_rgba part_pixels[2][16];1655uint32_t part_pixel_index[4][4];1656uint32_t num_part_pixels[2] = { 0, 0 };16571658for (uint32_t y = 0; y < 4; y++)1659{1660for (uint32_t x = 0; x < 4; x++)1661{1662const uint32_t part = g_bc7_partition2[16 * bc7_pattern + x + y * 4];1663part_pixel_index[y][x] = num_part_pixels[part];1664part_pixels[part][num_part_pixels[part]++] = pBlock[y * 4 + x];1665}1666}16671668color_cell_compressor_params ccell_params[2];1669color_cell_compressor_results ccell_results[2];1670uint8_t ccell_result_selectors[2][16];1671uint8_t ccell_result_selectors_temp[2][16];16721673uint64_t total_err = 0;1674for (uint32_t subset = 0; subset < 2; subset++)1675{1676memset(&ccell_params[subset], 0, sizeof(ccell_params[subset]));16771678ccell_params[subset].m_num_pixels = num_part_pixels[subset];1679ccell_params[subset].m_pPixels = (color_quad_u8*)&part_pixels[subset][0];1680ccell_params[subset].m_num_selector_weights = 4;1681ccell_params[subset].m_pSelector_weights = g_bc7_weights2;1682ccell_params[subset].m_pSelector_weightsx = (const bc7enc_vec4F*)g_bc7_weights2x;1683ccell_params[subset].m_astc_endpoint_range = endpoint_range;1684ccell_params[subset].m_weights[0] = weights[0];1685ccell_params[subset].m_weights[1] = weights[1];1686ccell_params[subset].m_weights[2] = weights[2];1687ccell_params[subset].m_weights[3] = weights[3];1688ccell_params[subset].m_has_alpha = true;16891690memset(&ccell_results[subset], 0, sizeof(ccell_results[subset]));1691ccell_results[subset].m_pSelectors = &ccell_result_selectors[subset][0];1692ccell_results[subset].m_pSelectors_temp = &ccell_result_selectors_temp[subset][0];16931694uint64_t subset_err = color_cell_compression(255, &ccell_params[subset], &ccell_results[subset], &comp_params);16951696if (mode == 16)1697{1698color_rgba colors[4];1699for (uint32_t c = 0; c < 4; c++)1700{1701colors[0].m_comps[c] = g_astc_unquant[endpoint_range][ccell_results[subset].m_astc_low_endpoint.m_c[(c < 3) ? 0 : 3]].m_unquant;1702colors[3].m_comps[c] = g_astc_unquant[endpoint_range][ccell_results[subset].m_astc_high_endpoint.m_c[(c < 3) ? 0 : 3]].m_unquant;1703}17041705for (uint32_t i = 1; i < 4 - 1; i++)1706for (uint32_t c = 0; c < 4; c++)1707colors[i].m_comps[c] = (uint8_t)astc_interpolate(colors[0].m_comps[c], colors[3].m_comps[c], g_bc7_weights2[i], false);17081709for (uint32_t p = 0; p < ccell_params[subset].m_num_pixels; p++)1710{1711color_rgba orig_pix(part_pixels[subset][p]);1712orig_pix.g = orig_pix.r;1713orig_pix.b = orig_pix.r;1714total_err += color_distance_la(orig_pix, colors[ccell_result_selectors[subset][p]]);1715}1716}1717else1718{1719total_err += subset_err;1720}1721} // subset17221723// ASTC1724astc_block_desc astc_results;1725memset(&astc_results, 0, sizeof(astc_results));17261727astc_results.m_dual_plane = false;1728astc_results.m_weight_range = 2;17291730astc_results.m_ccs = 0;1731astc_results.m_subsets = 2;1732astc_results.m_partition_seed = g_astc_bc7_common_partitions2[common_pattern].m_astc;1733astc_results.m_cem = (mode == 16) ? 4 : 12;17341735uint32_t part[2] = { 0, 1 };1736if (g_astc_bc7_common_partitions2[common_pattern].m_invert)1737std::swap(part[0], part[1]);17381739bool invert[2] = { false, false };17401741for (uint32_t p = 0; p < 2; p++)1742{1743if (mode == 16)1744{1745astc_results.m_endpoints[p * 4 + 0] = ccell_results[part[p]].m_astc_low_endpoint.m_c[0];1746astc_results.m_endpoints[p * 4 + 1] = ccell_results[part[p]].m_astc_high_endpoint.m_c[0];17471748astc_results.m_endpoints[p * 4 + 2] = ccell_results[part[p]].m_astc_low_endpoint.m_c[3];1749astc_results.m_endpoints[p * 4 + 3] = ccell_results[part[p]].m_astc_high_endpoint.m_c[3];1750}1751else1752{1753for (uint32_t c = 0; c < 4; c++)1754{1755astc_results.m_endpoints[p * 8 + c * 2] = ccell_results[part[p]].m_astc_low_endpoint.m_c[c];1756astc_results.m_endpoints[p * 8 + c * 2 + 1] = ccell_results[part[p]].m_astc_high_endpoint.m_c[c];1757}17581759int s0 = g_astc_unquant[endpoint_range][astc_results.m_endpoints[p * 8 + 0]].m_unquant +1760g_astc_unquant[endpoint_range][astc_results.m_endpoints[p * 8 + 2]].m_unquant +1761g_astc_unquant[endpoint_range][astc_results.m_endpoints[p * 8 + 4]].m_unquant;17621763int s1 = g_astc_unquant[endpoint_range][astc_results.m_endpoints[p * 8 + 1]].m_unquant +1764g_astc_unquant[endpoint_range][astc_results.m_endpoints[p * 8 + 3]].m_unquant +1765g_astc_unquant[endpoint_range][astc_results.m_endpoints[p * 8 + 5]].m_unquant;17661767if (s1 < s0)1768{1769std::swap(astc_results.m_endpoints[p * 8 + 0], astc_results.m_endpoints[p * 8 + 1]);1770std::swap(astc_results.m_endpoints[p * 8 + 2], astc_results.m_endpoints[p * 8 + 3]);1771std::swap(astc_results.m_endpoints[p * 8 + 4], astc_results.m_endpoints[p * 8 + 5]);1772std::swap(astc_results.m_endpoints[p * 8 + 6], astc_results.m_endpoints[p * 8 + 7]);1773invert[p] = true;1774}1775}1776}17771778for (uint32_t y = 0; y < 4; y++)1779{1780for (uint32_t x = 0; x < 4; x++)1781{1782const uint32_t bc7_part = g_bc7_partition2[16 * bc7_pattern + x + y * 4];17831784astc_results.m_weights[x + y * 4] = ccell_result_selectors[bc7_part][part_pixel_index[y][x]];17851786uint32_t astc_part = bc7_part;1787if (g_astc_bc7_common_partitions2[common_pattern].m_invert)1788astc_part = 1 - astc_part;17891790if (invert[astc_part])1791astc_results.m_weights[x + y * 4] = 3 - astc_results.m_weights[x + y * 4];1792}1793}17941795assert(total_results < MAX_ENCODE_RESULTS);1796if (total_results < MAX_ENCODE_RESULTS)1797{1798pResults[total_results].m_uastc_mode = mode;1799pResults[total_results].m_common_pattern = common_pattern;1800pResults[total_results].m_astc = astc_results;1801pResults[total_results].m_astc_err = total_err;1802total_results++;1803}18041805} // common_pattern1806}18071808// MODE 101809// DualPlane: 0, WeightRange: 8 (16), Subsets: 1, CEM: 12 (RGBA Direct ), EndpointRange: 13 (48) MODE61810static void astc_mode10(const color_rgba block[4][4], uastc_encode_results* pResults, uint32_t& total_results, bc7enc_compress_block_params& comp_params)1811{1812const uint32_t weight_range = 8;1813const uint32_t endpoint_range = 13;18141815color_cell_compressor_params ccell_params;1816memset(&ccell_params, 0, sizeof(ccell_params));18171818ccell_params.m_num_pixels = 16;1819ccell_params.m_pPixels = (color_quad_u8*)&block[0][0];1820ccell_params.m_num_selector_weights = 16;1821ccell_params.m_pSelector_weights = g_astc_weights4;1822ccell_params.m_pSelector_weightsx = (const bc7enc_vec4F*)g_astc_weights4x;1823ccell_params.m_astc_endpoint_range = endpoint_range;1824ccell_params.m_weights[0] = 1;1825ccell_params.m_weights[1] = 1;1826ccell_params.m_weights[2] = 1;1827ccell_params.m_weights[3] = 1;1828ccell_params.m_has_alpha = true;18291830color_cell_compressor_results ccell_results;1831uint8_t ccell_result_selectors[16];1832uint8_t ccell_result_selectors_temp[16];1833memset(&ccell_results, 0, sizeof(ccell_results));1834ccell_results.m_pSelectors = &ccell_result_selectors[0];1835ccell_results.m_pSelectors_temp = &ccell_result_selectors_temp[0];18361837uint64_t part_err = color_cell_compression(255, &ccell_params, &ccell_results, &comp_params);18381839// ASTC1840astc_block_desc astc_results;1841memset(&astc_results, 0, sizeof(astc_results));18421843astc_results.m_dual_plane = false;1844astc_results.m_weight_range = weight_range;18451846astc_results.m_ccs = 0;1847astc_results.m_subsets = 1;1848astc_results.m_partition_seed = 0;1849astc_results.m_cem = 12;18501851astc_results.m_endpoints[0] = ccell_results.m_astc_low_endpoint.m_c[0];1852astc_results.m_endpoints[1] = ccell_results.m_astc_high_endpoint.m_c[0];1853astc_results.m_endpoints[2] = ccell_results.m_astc_low_endpoint.m_c[1];1854astc_results.m_endpoints[3] = ccell_results.m_astc_high_endpoint.m_c[1];1855astc_results.m_endpoints[4] = ccell_results.m_astc_low_endpoint.m_c[2];1856astc_results.m_endpoints[5] = ccell_results.m_astc_high_endpoint.m_c[2];1857astc_results.m_endpoints[6] = ccell_results.m_astc_low_endpoint.m_c[3];1858astc_results.m_endpoints[7] = ccell_results.m_astc_high_endpoint.m_c[3];18591860bool invert = false;18611862int s0 = g_astc_unquant[endpoint_range][astc_results.m_endpoints[0]].m_unquant + g_astc_unquant[endpoint_range][astc_results.m_endpoints[2]].m_unquant + g_astc_unquant[endpoint_range][astc_results.m_endpoints[4]].m_unquant;1863int s1 = g_astc_unquant[endpoint_range][astc_results.m_endpoints[1]].m_unquant + g_astc_unquant[endpoint_range][astc_results.m_endpoints[3]].m_unquant + g_astc_unquant[endpoint_range][astc_results.m_endpoints[5]].m_unquant;1864if (s1 < s0)1865{1866std::swap(astc_results.m_endpoints[0], astc_results.m_endpoints[1]);1867std::swap(astc_results.m_endpoints[2], astc_results.m_endpoints[3]);1868std::swap(astc_results.m_endpoints[4], astc_results.m_endpoints[5]);1869std::swap(astc_results.m_endpoints[6], astc_results.m_endpoints[7]);1870invert = true;1871}18721873for (uint32_t y = 0; y < 4; y++)1874{1875for (uint32_t x = 0; x < 4; x++)1876{1877astc_results.m_weights[x + y * 4] = ccell_result_selectors[x + y * 4];18781879if (invert)1880astc_results.m_weights[x + y * 4] = 15 - astc_results.m_weights[x + y * 4];1881}1882}18831884assert(total_results < MAX_ENCODE_RESULTS);1885if (total_results < MAX_ENCODE_RESULTS)1886{1887pResults[total_results].m_uastc_mode = 10;1888pResults[total_results].m_common_pattern = 0;1889pResults[total_results].m_astc = astc_results;1890pResults[total_results].m_astc_err = part_err;1891total_results++;1892}1893}18941895// 11. DualPlane: 1, WeightRange: 2 (4), Subsets: 1, CEM: 12 (RGBA Direct), EndpointRange: 13 (48) MODE51896// 17. DualPlane: 1, WeightRange : 2 (4), Subsets : 1, CEM : 4 (LA Direct), EndpointRange : 20 (256) BC7 MODE51897static void astc_mode11_or_17(uint32_t mode, const color_rgba block[4][4], uastc_encode_results* pResults, uint32_t& total_results, bc7enc_compress_block_params& comp_params)1898{1899assert((mode == 11) || (mode == 17));19001901const uint32_t weight_range = 2;1902const uint32_t endpoint_range = (mode == 17) ? 20 : 13;19031904bc7enc_compress_block_params local_comp_params(comp_params);1905local_comp_params.m_perceptual = false;1906local_comp_params.m_weights[0] = 1;1907local_comp_params.m_weights[1] = 1;1908local_comp_params.m_weights[2] = 1;1909local_comp_params.m_weights[3] = 1;19101911const uint32_t last_rot_comp = (mode == 17) ? 1 : 4;19121913for (uint32_t rot_comp = 0; rot_comp < last_rot_comp; rot_comp++)1914{1915color_quad_u8 block_rgb[16];1916color_quad_u8 block_a[16];1917for (uint32_t i = 0; i < 16; i++)1918{1919block_rgb[i] = ((color_quad_u8*)&block[0][0])[i];1920block_a[i] = block_rgb[i];19211922if (mode == 17)1923{1924assert(block_rgb[i].m_c[0] == block_rgb[i].m_c[1]);1925assert(block_rgb[i].m_c[0] == block_rgb[i].m_c[2]);19261927block_a[i].m_c[0] = block_rgb[i].m_c[3];1928block_a[i].m_c[1] = block_rgb[i].m_c[3];1929block_a[i].m_c[2] = block_rgb[i].m_c[3];1930block_a[i].m_c[3] = 255;19311932block_rgb[i].m_c[1] = block_rgb[i].m_c[0];1933block_rgb[i].m_c[2] = block_rgb[i].m_c[0];1934block_rgb[i].m_c[3] = 255;1935}1936else1937{1938uint8_t c = block_a[i].m_c[rot_comp];1939block_a[i].m_c[0] = c;1940block_a[i].m_c[1] = c;1941block_a[i].m_c[2] = c;1942block_a[i].m_c[3] = 255;19431944block_rgb[i].m_c[rot_comp] = block_rgb[i].m_c[3];1945block_rgb[i].m_c[3] = 255;1946}1947}19481949uint8_t ccell_result_selectors_temp[16];19501951color_cell_compressor_params ccell_params_rgb;1952memset(&ccell_params_rgb, 0, sizeof(ccell_params_rgb));19531954ccell_params_rgb.m_num_pixels = 16;1955ccell_params_rgb.m_pPixels = block_rgb;1956ccell_params_rgb.m_num_selector_weights = 4;1957ccell_params_rgb.m_pSelector_weights = g_bc7_weights2;1958ccell_params_rgb.m_pSelector_weightsx = (const bc7enc_vec4F*)g_bc7_weights2x;1959ccell_params_rgb.m_astc_endpoint_range = endpoint_range;1960ccell_params_rgb.m_weights[0] = 1;1961ccell_params_rgb.m_weights[1] = 1;1962ccell_params_rgb.m_weights[2] = 1;1963ccell_params_rgb.m_weights[3] = 1;19641965color_cell_compressor_results ccell_results_rgb;1966uint8_t ccell_result_selectors_rgb[16];1967memset(&ccell_results_rgb, 0, sizeof(ccell_results_rgb));1968ccell_results_rgb.m_pSelectors = &ccell_result_selectors_rgb[0];1969ccell_results_rgb.m_pSelectors_temp = &ccell_result_selectors_temp[0];19701971uint64_t part_err_rgb = color_cell_compression(255, &ccell_params_rgb, &ccell_results_rgb, &local_comp_params);19721973color_cell_compressor_params ccell_params_a;1974memset(&ccell_params_a, 0, sizeof(ccell_params_a));19751976ccell_params_a.m_num_pixels = 16;1977ccell_params_a.m_pPixels = block_a;1978ccell_params_a.m_num_selector_weights = 4;1979ccell_params_a.m_pSelector_weights = g_bc7_weights2;1980ccell_params_a.m_pSelector_weightsx = (const bc7enc_vec4F*)g_bc7_weights2x;1981ccell_params_a.m_astc_endpoint_range = endpoint_range;1982ccell_params_a.m_weights[0] = 1;1983ccell_params_a.m_weights[1] = 1;1984ccell_params_a.m_weights[2] = 1;1985ccell_params_a.m_weights[3] = 1;19861987color_cell_compressor_results ccell_results_a;1988uint8_t ccell_result_selectors_a[16];1989memset(&ccell_results_a, 0, sizeof(ccell_results_a));1990ccell_results_a.m_pSelectors = &ccell_result_selectors_a[0];1991ccell_results_a.m_pSelectors_temp = &ccell_result_selectors_temp[0];19921993uint64_t part_err_a = color_cell_compression(255, &ccell_params_a, &ccell_results_a, &local_comp_params) / 3;19941995uint64_t total_err = (mode == 17) ? ((part_err_rgb / 3) + part_err_a) : (part_err_rgb + part_err_a);19961997// ASTC1998astc_block_desc blk;1999memset(&blk, 0, sizeof(blk));20002001blk.m_dual_plane = true;2002blk.m_weight_range = weight_range;20032004blk.m_ccs = (mode == 17) ? 3 : rot_comp;2005blk.m_subsets = 1;2006blk.m_partition_seed = 0;2007blk.m_cem = (mode == 17) ? 4 : 12;20082009bool invert = false;20102011if (mode == 17)2012{2013assert(ccell_results_rgb.m_astc_low_endpoint.m_c[0] == ccell_results_rgb.m_astc_low_endpoint.m_c[1]);2014assert(ccell_results_rgb.m_astc_low_endpoint.m_c[0] == ccell_results_rgb.m_astc_low_endpoint.m_c[2]);20152016assert(ccell_results_rgb.m_astc_high_endpoint.m_c[0] == ccell_results_rgb.m_astc_high_endpoint.m_c[1]);2017assert(ccell_results_rgb.m_astc_high_endpoint.m_c[0] == ccell_results_rgb.m_astc_high_endpoint.m_c[2]);20182019blk.m_endpoints[0] = ccell_results_rgb.m_astc_low_endpoint.m_c[0];2020blk.m_endpoints[1] = ccell_results_rgb.m_astc_high_endpoint.m_c[0];20212022blk.m_endpoints[2] = ccell_results_a.m_astc_low_endpoint.m_c[0];2023blk.m_endpoints[3] = ccell_results_a.m_astc_high_endpoint.m_c[0];2024}2025else2026{2027blk.m_endpoints[0] = (rot_comp == 0 ? ccell_results_a : ccell_results_rgb).m_astc_low_endpoint.m_c[0];2028blk.m_endpoints[1] = (rot_comp == 0 ? ccell_results_a : ccell_results_rgb).m_astc_high_endpoint.m_c[0];2029blk.m_endpoints[2] = (rot_comp == 1 ? ccell_results_a : ccell_results_rgb).m_astc_low_endpoint.m_c[1];2030blk.m_endpoints[3] = (rot_comp == 1 ? ccell_results_a : ccell_results_rgb).m_astc_high_endpoint.m_c[1];2031blk.m_endpoints[4] = (rot_comp == 2 ? ccell_results_a : ccell_results_rgb).m_astc_low_endpoint.m_c[2];2032blk.m_endpoints[5] = (rot_comp == 2 ? ccell_results_a : ccell_results_rgb).m_astc_high_endpoint.m_c[2];2033if (rot_comp == 3)2034{2035blk.m_endpoints[6] = ccell_results_a.m_astc_low_endpoint.m_c[0];2036blk.m_endpoints[7] = ccell_results_a.m_astc_high_endpoint.m_c[0];2037}2038else2039{2040blk.m_endpoints[6] = ccell_results_rgb.m_astc_low_endpoint.m_c[rot_comp];2041blk.m_endpoints[7] = ccell_results_rgb.m_astc_high_endpoint.m_c[rot_comp];2042}20432044int s0 = g_astc_unquant[endpoint_range][blk.m_endpoints[0]].m_unquant + g_astc_unquant[endpoint_range][blk.m_endpoints[2]].m_unquant + g_astc_unquant[endpoint_range][blk.m_endpoints[4]].m_unquant;2045int s1 = g_astc_unquant[endpoint_range][blk.m_endpoints[1]].m_unquant + g_astc_unquant[endpoint_range][blk.m_endpoints[3]].m_unquant + g_astc_unquant[endpoint_range][blk.m_endpoints[5]].m_unquant;2046if (s1 < s0)2047{2048std::swap(blk.m_endpoints[0], blk.m_endpoints[1]);2049std::swap(blk.m_endpoints[2], blk.m_endpoints[3]);2050std::swap(blk.m_endpoints[4], blk.m_endpoints[5]);2051std::swap(blk.m_endpoints[6], blk.m_endpoints[7]);2052invert = true;2053}2054}20552056for (uint32_t y = 0; y < 4; y++)2057{2058for (uint32_t x = 0; x < 4; x++)2059{2060uint32_t rgb_index = ccell_result_selectors_rgb[x + y * 4];2061uint32_t a_index = ccell_result_selectors_a[x + y * 4];20622063if (invert)2064{2065rgb_index = 3 - rgb_index;2066a_index = 3 - a_index;2067}20682069blk.m_weights[(x + y * 4) * 2 + 0] = (uint8_t)rgb_index;2070blk.m_weights[(x + y * 4) * 2 + 1] = (uint8_t)a_index;2071}2072}20732074assert(total_results < MAX_ENCODE_RESULTS);2075if (total_results < MAX_ENCODE_RESULTS)2076{2077pResults[total_results].m_uastc_mode = mode;2078pResults[total_results].m_common_pattern = 0;2079pResults[total_results].m_astc = blk;2080pResults[total_results].m_astc_err = total_err;2081total_results++;2082}2083} // rot_comp2084}20852086// MODE 122087// DualPlane: 0, WeightRange: 5 (8), Subsets: 1, CEM: 12 (RGBA Direct ), EndpointRange: 19 (192) MODE62088static void astc_mode12(const color_rgba block[4][4], uastc_encode_results* pResults, uint32_t& total_results, bc7enc_compress_block_params& comp_params)2089{2090const uint32_t weight_range = 5;2091const uint32_t endpoint_range = 19;20922093color_cell_compressor_params ccell_params;2094memset(&ccell_params, 0, sizeof(ccell_params));20952096ccell_params.m_num_pixels = 16;2097ccell_params.m_pPixels = (color_quad_u8*)&block[0][0];2098ccell_params.m_num_selector_weights = 8;2099ccell_params.m_pSelector_weights = g_bc7_weights3;2100ccell_params.m_pSelector_weightsx = (const bc7enc_vec4F*)g_bc7_weights3x;2101ccell_params.m_astc_endpoint_range = endpoint_range;2102ccell_params.m_weights[0] = 1;2103ccell_params.m_weights[1] = 1;2104ccell_params.m_weights[2] = 1;2105ccell_params.m_weights[3] = 1;2106ccell_params.m_has_alpha = true;21072108color_cell_compressor_results ccell_results;2109uint8_t ccell_result_selectors[16];2110uint8_t ccell_result_selectors_temp[16];2111memset(&ccell_results, 0, sizeof(ccell_results));2112ccell_results.m_pSelectors = &ccell_result_selectors[0];2113ccell_results.m_pSelectors_temp = &ccell_result_selectors_temp[0];21142115uint64_t part_err = color_cell_compression(255, &ccell_params, &ccell_results, &comp_params);21162117// ASTC2118astc_block_desc astc_results;2119memset(&astc_results, 0, sizeof(astc_results));21202121astc_results.m_dual_plane = false;2122astc_results.m_weight_range = weight_range;21232124astc_results.m_ccs = 0;2125astc_results.m_subsets = 1;2126astc_results.m_partition_seed = 0;2127astc_results.m_cem = 12;21282129astc_results.m_endpoints[0] = ccell_results.m_astc_low_endpoint.m_c[0];2130astc_results.m_endpoints[1] = ccell_results.m_astc_high_endpoint.m_c[0];2131astc_results.m_endpoints[2] = ccell_results.m_astc_low_endpoint.m_c[1];2132astc_results.m_endpoints[3] = ccell_results.m_astc_high_endpoint.m_c[1];2133astc_results.m_endpoints[4] = ccell_results.m_astc_low_endpoint.m_c[2];2134astc_results.m_endpoints[5] = ccell_results.m_astc_high_endpoint.m_c[2];2135astc_results.m_endpoints[6] = ccell_results.m_astc_low_endpoint.m_c[3];2136astc_results.m_endpoints[7] = ccell_results.m_astc_high_endpoint.m_c[3];21372138bool invert = false;21392140int s0 = g_astc_unquant[endpoint_range][astc_results.m_endpoints[0]].m_unquant + g_astc_unquant[endpoint_range][astc_results.m_endpoints[2]].m_unquant + g_astc_unquant[endpoint_range][astc_results.m_endpoints[4]].m_unquant;2141int s1 = g_astc_unquant[endpoint_range][astc_results.m_endpoints[1]].m_unquant + g_astc_unquant[endpoint_range][astc_results.m_endpoints[3]].m_unquant + g_astc_unquant[endpoint_range][astc_results.m_endpoints[5]].m_unquant;2142if (s1 < s0)2143{2144std::swap(astc_results.m_endpoints[0], astc_results.m_endpoints[1]);2145std::swap(astc_results.m_endpoints[2], astc_results.m_endpoints[3]);2146std::swap(astc_results.m_endpoints[4], astc_results.m_endpoints[5]);2147std::swap(astc_results.m_endpoints[6], astc_results.m_endpoints[7]);2148invert = true;2149}21502151for (uint32_t y = 0; y < 4; y++)2152{2153for (uint32_t x = 0; x < 4; x++)2154{2155astc_results.m_weights[x + y * 4] = ccell_result_selectors[x + y * 4];21562157if (invert)2158astc_results.m_weights[x + y * 4] = 7 - astc_results.m_weights[x + y * 4];2159}2160}21612162assert(total_results < MAX_ENCODE_RESULTS);2163if (total_results < MAX_ENCODE_RESULTS)2164{2165pResults[total_results].m_uastc_mode = 12;2166pResults[total_results].m_common_pattern = 0;2167pResults[total_results].m_astc = astc_results;2168pResults[total_results].m_astc_err = part_err;2169total_results++;2170}2171}21722173// 13. DualPlane: 1, WeightRange: 0 (2), Subsets: 1, CEM: 12 (RGBA Direct ), EndpointRange: 20 (256) MODE52174static void astc_mode13(const color_rgba block[4][4], uastc_encode_results* pResults, uint32_t& total_results, bc7enc_compress_block_params& comp_params)2175{2176bc7enc_compress_block_params local_comp_params(comp_params);2177local_comp_params.m_perceptual = false;2178local_comp_params.m_weights[0] = 1;2179local_comp_params.m_weights[1] = 1;2180local_comp_params.m_weights[2] = 1;2181local_comp_params.m_weights[3] = 1;21822183for (uint32_t rot_comp = 0; rot_comp < 4; rot_comp++)2184{2185const uint32_t weight_range = 0;2186const uint32_t endpoint_range = 20;21872188color_quad_u8 block_rgb[16];2189color_quad_u8 block_a[16];2190for (uint32_t i = 0; i < 16; i++)2191{2192block_rgb[i] = ((color_quad_u8*)&block[0][0])[i];2193block_a[i] = block_rgb[i];21942195uint8_t c = block_a[i].m_c[rot_comp];2196block_a[i].m_c[0] = c;2197block_a[i].m_c[1] = c;2198block_a[i].m_c[2] = c;2199block_a[i].m_c[3] = 255;22002201block_rgb[i].m_c[rot_comp] = block_rgb[i].m_c[3];2202block_rgb[i].m_c[3] = 255;2203}22042205uint8_t ccell_result_selectors_temp[16];22062207color_cell_compressor_params ccell_params_rgb;2208memset(&ccell_params_rgb, 0, sizeof(ccell_params_rgb));22092210ccell_params_rgb.m_num_pixels = 16;2211ccell_params_rgb.m_pPixels = block_rgb;2212ccell_params_rgb.m_num_selector_weights = 2;2213ccell_params_rgb.m_pSelector_weights = g_bc7_weights1;2214ccell_params_rgb.m_pSelector_weightsx = (const bc7enc_vec4F*)g_bc7_weights1x;2215ccell_params_rgb.m_astc_endpoint_range = endpoint_range;2216ccell_params_rgb.m_weights[0] = 1;2217ccell_params_rgb.m_weights[1] = 1;2218ccell_params_rgb.m_weights[2] = 1;2219ccell_params_rgb.m_weights[3] = 1;22202221color_cell_compressor_results ccell_results_rgb;2222uint8_t ccell_result_selectors_rgb[16];2223memset(&ccell_results_rgb, 0, sizeof(ccell_results_rgb));2224ccell_results_rgb.m_pSelectors = &ccell_result_selectors_rgb[0];2225ccell_results_rgb.m_pSelectors_temp = &ccell_result_selectors_temp[0];22262227uint64_t part_err_rgb = color_cell_compression(255, &ccell_params_rgb, &ccell_results_rgb, &local_comp_params);22282229color_cell_compressor_params ccell_params_a;2230memset(&ccell_params_a, 0, sizeof(ccell_params_a));22312232ccell_params_a.m_num_pixels = 16;2233ccell_params_a.m_pPixels = block_a;2234ccell_params_a.m_num_selector_weights = 2;2235ccell_params_a.m_pSelector_weights = g_bc7_weights1;2236ccell_params_a.m_pSelector_weightsx = (const bc7enc_vec4F*)g_bc7_weights1x;2237ccell_params_a.m_astc_endpoint_range = endpoint_range;2238ccell_params_a.m_weights[0] = 1;2239ccell_params_a.m_weights[1] = 1;2240ccell_params_a.m_weights[2] = 1;2241ccell_params_a.m_weights[3] = 1;22422243color_cell_compressor_results ccell_results_a;2244uint8_t ccell_result_selectors_a[16];2245memset(&ccell_results_a, 0, sizeof(ccell_results_a));2246ccell_results_a.m_pSelectors = &ccell_result_selectors_a[0];2247ccell_results_a.m_pSelectors_temp = &ccell_result_selectors_temp[0];22482249uint64_t part_err_a = color_cell_compression(255, &ccell_params_a, &ccell_results_a, &local_comp_params) / 3;22502251uint64_t total_err = part_err_rgb + part_err_a;22522253// ASTC2254astc_block_desc blk;2255memset(&blk, 0, sizeof(blk));22562257blk.m_dual_plane = true;2258blk.m_weight_range = weight_range;22592260blk.m_ccs = rot_comp;2261blk.m_subsets = 1;2262blk.m_partition_seed = 0;2263blk.m_cem = 12;22642265blk.m_endpoints[0] = (rot_comp == 0 ? ccell_results_a : ccell_results_rgb).m_astc_low_endpoint.m_c[0];2266blk.m_endpoints[1] = (rot_comp == 0 ? ccell_results_a : ccell_results_rgb).m_astc_high_endpoint.m_c[0];2267blk.m_endpoints[2] = (rot_comp == 1 ? ccell_results_a : ccell_results_rgb).m_astc_low_endpoint.m_c[1];2268blk.m_endpoints[3] = (rot_comp == 1 ? ccell_results_a : ccell_results_rgb).m_astc_high_endpoint.m_c[1];2269blk.m_endpoints[4] = (rot_comp == 2 ? ccell_results_a : ccell_results_rgb).m_astc_low_endpoint.m_c[2];2270blk.m_endpoints[5] = (rot_comp == 2 ? ccell_results_a : ccell_results_rgb).m_astc_high_endpoint.m_c[2];2271if (rot_comp == 3)2272{2273blk.m_endpoints[6] = ccell_results_a.m_astc_low_endpoint.m_c[0];2274blk.m_endpoints[7] = ccell_results_a.m_astc_high_endpoint.m_c[0];2275}2276else2277{2278blk.m_endpoints[6] = ccell_results_rgb.m_astc_low_endpoint.m_c[rot_comp];2279blk.m_endpoints[7] = ccell_results_rgb.m_astc_high_endpoint.m_c[rot_comp];2280}22812282bool invert = false;22832284int s0 = g_astc_unquant[endpoint_range][blk.m_endpoints[0]].m_unquant + g_astc_unquant[endpoint_range][blk.m_endpoints[2]].m_unquant + g_astc_unquant[endpoint_range][blk.m_endpoints[4]].m_unquant;2285int s1 = g_astc_unquant[endpoint_range][blk.m_endpoints[1]].m_unquant + g_astc_unquant[endpoint_range][blk.m_endpoints[3]].m_unquant + g_astc_unquant[endpoint_range][blk.m_endpoints[5]].m_unquant;2286if (s1 < s0)2287{2288std::swap(blk.m_endpoints[0], blk.m_endpoints[1]);2289std::swap(blk.m_endpoints[2], blk.m_endpoints[3]);2290std::swap(blk.m_endpoints[4], blk.m_endpoints[5]);2291std::swap(blk.m_endpoints[6], blk.m_endpoints[7]);2292invert = true;2293}22942295for (uint32_t y = 0; y < 4; y++)2296{2297for (uint32_t x = 0; x < 4; x++)2298{2299uint32_t rgb_index = ccell_result_selectors_rgb[x + y * 4];2300uint32_t a_index = ccell_result_selectors_a[x + y * 4];23012302if (invert)2303{2304rgb_index = 1 - rgb_index;2305a_index = 1 - a_index;2306}23072308blk.m_weights[(x + y * 4) * 2 + 0] = (uint8_t)rgb_index;2309blk.m_weights[(x + y * 4) * 2 + 1] = (uint8_t)a_index;2310}2311}23122313assert(total_results < MAX_ENCODE_RESULTS);2314if (total_results < MAX_ENCODE_RESULTS)2315{2316pResults[total_results].m_uastc_mode = 13;2317pResults[total_results].m_common_pattern = 0;2318pResults[total_results].m_astc = blk;2319pResults[total_results].m_astc_err = total_err;2320total_results++;2321}2322} // rot_comp2323}23242325// MODE142326// DualPlane: 0, WeightRange: 2 (4), Subsets: 1, CEM: 12 (RGBA Direct ), EndpointRange: 20 (256) MODE62327static void astc_mode14(const color_rgba block[4][4], uastc_encode_results* pResults, uint32_t& total_results, bc7enc_compress_block_params& comp_params)2328{2329const uint32_t weight_range = 2;2330const uint32_t endpoint_range = 20;23312332color_cell_compressor_params ccell_params;2333memset(&ccell_params, 0, sizeof(ccell_params));23342335ccell_params.m_num_pixels = 16;2336ccell_params.m_pPixels = (color_quad_u8*)&block[0][0];2337ccell_params.m_num_selector_weights = 4;2338ccell_params.m_pSelector_weights = g_bc7_weights2;2339ccell_params.m_pSelector_weightsx = (const bc7enc_vec4F*)g_bc7_weights2x;2340ccell_params.m_astc_endpoint_range = endpoint_range;2341ccell_params.m_weights[0] = 1;2342ccell_params.m_weights[1] = 1;2343ccell_params.m_weights[2] = 1;2344ccell_params.m_weights[3] = 1;2345ccell_params.m_has_alpha = true;23462347color_cell_compressor_results ccell_results;2348uint8_t ccell_result_selectors[16];2349uint8_t ccell_result_selectors_temp[16];2350memset(&ccell_results, 0, sizeof(ccell_results));2351ccell_results.m_pSelectors = &ccell_result_selectors[0];2352ccell_results.m_pSelectors_temp = &ccell_result_selectors_temp[0];23532354uint64_t part_err = color_cell_compression(255, &ccell_params, &ccell_results, &comp_params);23552356// ASTC2357astc_block_desc astc_results;2358memset(&astc_results, 0, sizeof(astc_results));23592360astc_results.m_dual_plane = false;2361astc_results.m_weight_range = weight_range;23622363astc_results.m_ccs = 0;2364astc_results.m_subsets = 1;2365astc_results.m_partition_seed = 0;2366astc_results.m_cem = 12;23672368astc_results.m_endpoints[0] = ccell_results.m_astc_low_endpoint.m_c[0];2369astc_results.m_endpoints[1] = ccell_results.m_astc_high_endpoint.m_c[0];2370astc_results.m_endpoints[2] = ccell_results.m_astc_low_endpoint.m_c[1];2371astc_results.m_endpoints[3] = ccell_results.m_astc_high_endpoint.m_c[1];2372astc_results.m_endpoints[4] = ccell_results.m_astc_low_endpoint.m_c[2];2373astc_results.m_endpoints[5] = ccell_results.m_astc_high_endpoint.m_c[2];2374astc_results.m_endpoints[6] = ccell_results.m_astc_low_endpoint.m_c[3];2375astc_results.m_endpoints[7] = ccell_results.m_astc_high_endpoint.m_c[3];23762377bool invert = false;23782379int s0 = g_astc_unquant[endpoint_range][astc_results.m_endpoints[0]].m_unquant + g_astc_unquant[endpoint_range][astc_results.m_endpoints[2]].m_unquant + g_astc_unquant[endpoint_range][astc_results.m_endpoints[4]].m_unquant;2380int s1 = g_astc_unquant[endpoint_range][astc_results.m_endpoints[1]].m_unquant + g_astc_unquant[endpoint_range][astc_results.m_endpoints[3]].m_unquant + g_astc_unquant[endpoint_range][astc_results.m_endpoints[5]].m_unquant;2381if (s1 < s0)2382{2383std::swap(astc_results.m_endpoints[0], astc_results.m_endpoints[1]);2384std::swap(astc_results.m_endpoints[2], astc_results.m_endpoints[3]);2385std::swap(astc_results.m_endpoints[4], astc_results.m_endpoints[5]);2386std::swap(astc_results.m_endpoints[6], astc_results.m_endpoints[7]);2387invert = true;2388}23892390for (uint32_t y = 0; y < 4; y++)2391{2392for (uint32_t x = 0; x < 4; x++)2393{2394astc_results.m_weights[x + y * 4] = ccell_result_selectors[x + y * 4];23952396if (invert)2397astc_results.m_weights[x + y * 4] = 3 - astc_results.m_weights[x + y * 4];2398}2399}24002401assert(total_results < MAX_ENCODE_RESULTS);2402if (total_results < MAX_ENCODE_RESULTS)2403{2404pResults[total_results].m_uastc_mode = 14;2405pResults[total_results].m_common_pattern = 0;2406pResults[total_results].m_astc = astc_results;2407pResults[total_results].m_astc_err = part_err;2408total_results++;2409}2410}24112412// MODE 152413// DualPlane: 0, WeightRange : 8 (16), Subsets : 1, CEM : 4 (LA Direct), EndpointRange : 20 (256) BC7 MODE62414static void astc_mode15(const color_rgba block[4][4], uastc_encode_results* pResults, uint32_t& total_results, bc7enc_compress_block_params& comp_params)2415{2416const uint32_t weight_range = 8;2417const uint32_t endpoint_range = 20;24182419color_cell_compressor_params ccell_params;2420memset(&ccell_params, 0, sizeof(ccell_params));24212422color_rgba temp_block[16];2423for (uint32_t i = 0; i < 16; i++)2424{2425const uint32_t l = ((const color_rgba*)block)[i].r;2426const uint32_t a = ((const color_rgba*)block)[i].a;24272428// Use (l,0,0,a) not (l,l,l,a) so both components are treated equally.2429temp_block[i].set_noclamp_rgba(l, 0, 0, a);2430}24312432ccell_params.m_num_pixels = 16;2433//ccell_params.m_pPixels = (color_quad_u8*)&block[0][0];2434ccell_params.m_pPixels = (color_quad_u8*)temp_block;2435ccell_params.m_num_selector_weights = 16;2436ccell_params.m_pSelector_weights = g_astc_weights4;2437ccell_params.m_pSelector_weightsx = (const bc7enc_vec4F*)g_astc_weights4x;2438ccell_params.m_astc_endpoint_range = endpoint_range;2439ccell_params.m_weights[0] = 1;2440ccell_params.m_weights[1] = 1;2441ccell_params.m_weights[2] = 1;2442ccell_params.m_weights[3] = 1;2443ccell_params.m_has_alpha = true;24442445color_cell_compressor_results ccell_results;2446uint8_t ccell_result_selectors[16];2447uint8_t ccell_result_selectors_temp[16];2448memset(&ccell_results, 0, sizeof(ccell_results));2449ccell_results.m_pSelectors = &ccell_result_selectors[0];2450ccell_results.m_pSelectors_temp = &ccell_result_selectors_temp[0];24512452color_cell_compression(255, &ccell_params, &ccell_results, &comp_params);24532454// ASTC2455astc_block_desc astc_results;2456memset(&astc_results, 0, sizeof(astc_results));24572458astc_results.m_dual_plane = false;2459astc_results.m_weight_range = weight_range;24602461astc_results.m_ccs = 0;2462astc_results.m_subsets = 1;2463astc_results.m_partition_seed = 0;2464astc_results.m_cem = 4;24652466astc_results.m_endpoints[0] = ccell_results.m_astc_low_endpoint.m_c[0];2467astc_results.m_endpoints[1] = ccell_results.m_astc_high_endpoint.m_c[0];24682469astc_results.m_endpoints[2] = ccell_results.m_astc_low_endpoint.m_c[3];2470astc_results.m_endpoints[3] = ccell_results.m_astc_high_endpoint.m_c[3];24712472for (uint32_t y = 0; y < 4; y++)2473for (uint32_t x = 0; x < 4; x++)2474astc_results.m_weights[x + y * 4] = ccell_result_selectors[x + y * 4];24752476color_rgba colors[16];2477for (uint32_t c = 0; c < 4; c++)2478{2479colors[0].m_comps[c] = g_astc_unquant[endpoint_range][ccell_results.m_astc_low_endpoint.m_c[(c < 3) ? 0 : 3]].m_unquant;2480colors[15].m_comps[c] = g_astc_unquant[endpoint_range][ccell_results.m_astc_high_endpoint.m_c[(c < 3) ? 0 : 3]].m_unquant;2481}24822483for (uint32_t i = 1; i < 16 - 1; i++)2484for (uint32_t c = 0; c < 4; c++)2485colors[i].m_comps[c] = (uint8_t)astc_interpolate(colors[0].m_comps[c], colors[15].m_comps[c], g_astc_weights4[i], false);24862487uint64_t total_err = 0;2488for (uint32_t p = 0; p < 16; p++)2489total_err += color_distance_la(((const color_rgba*)block)[p], colors[ccell_result_selectors[p]]);24902491assert(total_results < MAX_ENCODE_RESULTS);2492if (total_results < MAX_ENCODE_RESULTS)2493{2494pResults[total_results].m_uastc_mode = 15;2495pResults[total_results].m_common_pattern = 0;2496pResults[total_results].m_astc = astc_results;2497pResults[total_results].m_astc_err = total_err;2498total_results++;2499}2500}25012502static void compute_block_error(const color_rgba block[4][4], const color_rgba decoded_block[4][4], uint64_t &total_rgb_err, uint64_t &total_rgba_err, uint64_t &total_la_err)2503{2504uint64_t total_err_r = 0, total_err_g = 0, total_err_b = 0, total_err_a = 0;25052506for (uint32_t y = 0; y < 4; y++)2507{2508for (uint32_t x = 0; x < 4; x++)2509{2510const int dr = (int)block[y][x].m_comps[0] - (int)decoded_block[y][x].m_comps[0];2511const int dg = (int)block[y][x].m_comps[1] - (int)decoded_block[y][x].m_comps[1];2512const int db = (int)block[y][x].m_comps[2] - (int)decoded_block[y][x].m_comps[2];2513const int da = (int)block[y][x].m_comps[3] - (int)decoded_block[y][x].m_comps[3];25142515total_err_r += dr * dr;2516total_err_g += dg * dg;2517total_err_b += db * db;2518total_err_a += da * da;2519}2520}25212522total_la_err = total_err_r + total_err_a;2523total_rgb_err = total_err_r + total_err_g + total_err_b;2524total_rgba_err = total_rgb_err + total_err_a;2525}25262527static void compute_bc1_hints(bool &bc1_hint0, bool &bc1_hint1, const uastc_encode_results &best_results, const color_rgba block[4][4], const color_rgba decoded_uastc_block[4][4])2528{2529const uint32_t best_mode = best_results.m_uastc_mode;2530const bool perceptual = false;25312532bc1_hint0 = false;2533bc1_hint1 = false;25342535if (best_mode == UASTC_MODE_INDEX_SOLID_COLOR)2536return;25372538if (!g_uastc_mode_has_bc1_hint0[best_mode] && !g_uastc_mode_has_bc1_hint1[best_mode])2539return;25402541color_rgba tblock_bc1[4][4];2542dxt1_block tbc1_block[8];2543basist::encode_bc1(tbc1_block, (const uint8_t*)&decoded_uastc_block[0][0], 0);2544unpack_block(texture_format::cBC1, tbc1_block, &tblock_bc1[0][0]);25452546color_rgba tblock_hint0_bc1[4][4];2547color_rgba tblock_hint1_bc1[4][4];25482549etc_block etc1_blk;2550memset(&etc1_blk, 0, sizeof(etc1_blk));25512552eac_a8_block etc2_blk;2553memset(&etc2_blk, 0, sizeof(etc2_blk));2554etc2_blk.m_multiplier = 1;25552556// Pack to UASTC, then unpack, because the endpoints may be swapped.25572558uastc_block temp_ublock;2559pack_uastc(temp_ublock, best_results, etc1_blk, 0, etc2_blk, false, false);25602561unpacked_uastc_block temp_ublock_unpacked;2562unpack_uastc(temp_ublock, temp_ublock_unpacked, false);25632564unpacked_uastc_block ublock;2565memset(&ublock, 0, sizeof(ublock));2566ublock.m_mode = best_results.m_uastc_mode;2567ublock.m_common_pattern = best_results.m_common_pattern;2568ublock.m_astc = temp_ublock_unpacked.m_astc;25692570dxt1_block b;25712572// HINT12573if (!g_uastc_mode_has_bc1_hint1[best_mode])2574{2575memset(tblock_hint1_bc1, 0, sizeof(tblock_hint1_bc1));2576}2577else2578{2579transcode_uastc_to_bc1_hint1(ublock, (color32 (*)[4]) decoded_uastc_block, &b, false);25802581unpack_block(texture_format::cBC1, &b, &tblock_hint1_bc1[0][0]);2582}25832584// HINT02585if (!g_uastc_mode_has_bc1_hint0[best_mode])2586{2587memset(tblock_hint0_bc1, 0, sizeof(tblock_hint0_bc1));2588}2589else2590{2591transcode_uastc_to_bc1_hint0(ublock, &b);25922593unpack_block(texture_format::cBC1, &b, &tblock_hint0_bc1[0][0]);2594}25952596// Compute block errors2597uint64_t total_t_err = 0, total_hint0_err = 0, total_hint1_err = 0;2598for (uint32_t y = 0; y < 4; y++)2599{2600for (uint32_t x = 0; x < 4; x++)2601{2602total_t_err += color_distance(perceptual, block[y][x], tblock_bc1[y][x], false);2603total_hint0_err += color_distance(perceptual, block[y][x], tblock_hint0_bc1[y][x], false);2604total_hint1_err += color_distance(perceptual, block[y][x], tblock_hint1_bc1[y][x], false);2605}2606}26072608const float t_err = sqrtf((float)total_t_err);2609const float t_err_hint0 = sqrtf((float)total_hint0_err);2610const float t_err_hint1 = sqrtf((float)total_hint1_err);26112612const float err_thresh0 = 1.075f;2613const float err_thresh1 = 1.075f;26142615if ((g_uastc_mode_has_bc1_hint0[best_mode]) && (t_err_hint0 <= t_err * err_thresh0))2616bc1_hint0 = true;26172618if ((g_uastc_mode_has_bc1_hint1[best_mode]) && (t_err_hint1 <= t_err * err_thresh1))2619bc1_hint1 = true;2620}26212622struct ycbcr2623{2624int32_t m_y;2625int32_t m_cb;2626int32_t m_cr;2627};26282629static inline void rgb_to_y_cb_cr(const color_rgba& c, ycbcr& dst)2630{2631const int y = c.r * 54 + c.g * 183 + c.b * 19;2632dst.m_y = y;2633dst.m_cb = (c.b << 8) - y;2634dst.m_cr = (c.r << 8) - y;2635}26362637static inline uint64_t color_diff(const ycbcr& a, const ycbcr& b)2638{2639const int y_delta = a.m_y - b.m_y;2640const int cb_delta = a.m_cb - b.m_cb;2641const int cr_delta = a.m_cr - b.m_cr;2642return ((int64_t)y_delta * y_delta * 4) + ((int64_t)cr_delta * cr_delta) + ((int64_t)cb_delta * cb_delta);2643}26442645static inline int gray_distance2(const color_rgba& c, int r, int g, int b)2646{2647int gray_dist = (((int)c[0] - r) + ((int)c[1] - g) + ((int)c[2] - b) + 1) / 3;26482649int gray_point_r = clamp255(r + gray_dist);2650int gray_point_g = clamp255(g + gray_dist);2651int gray_point_b = clamp255(b + gray_dist);26522653int dist_to_gray_point_r = c[0] - gray_point_r;2654int dist_to_gray_point_g = c[1] - gray_point_g;2655int dist_to_gray_point_b = c[2] - gray_point_b;26562657return (dist_to_gray_point_r * dist_to_gray_point_r) + (dist_to_gray_point_g * dist_to_gray_point_g) + (dist_to_gray_point_b * dist_to_gray_point_b);2658}26592660static bool pack_etc1_estimate_flipped(const color_rgba* pSrc_pixels)2661{2662int sums[3][2][2];26632664#define GET_XY(x, y, c) pSrc_pixels[(x) + ((y) * 4)][c]26652666for (uint32_t c = 0; c < 3; c++)2667{2668sums[c][0][0] = GET_XY(0, 0, c) + GET_XY(0, 1, c) + GET_XY(1, 0, c) + GET_XY(1, 1, c);2669sums[c][1][0] = GET_XY(2, 0, c) + GET_XY(2, 1, c) + GET_XY(3, 0, c) + GET_XY(3, 1, c);2670sums[c][0][1] = GET_XY(0, 2, c) + GET_XY(0, 3, c) + GET_XY(1, 2, c) + GET_XY(1, 3, c);2671sums[c][1][1] = GET_XY(2, 2, c) + GET_XY(2, 3, c) + GET_XY(3, 2, c) + GET_XY(3, 3, c);2672}26732674int upper_avg[3], lower_avg[3], left_avg[3], right_avg[3];2675for (uint32_t c = 0; c < 3; c++)2676{2677upper_avg[c] = (sums[c][0][0] + sums[c][1][0] + 4) / 8;2678lower_avg[c] = (sums[c][0][1] + sums[c][1][1] + 4) / 8;2679left_avg[c] = (sums[c][0][0] + sums[c][0][1] + 4) / 8;2680right_avg[c] = (sums[c][1][0] + sums[c][1][1] + 4) / 8;2681}26822683#undef GET_XY2684#define GET_XY(x, y, a) gray_distance2(pSrc_pixels[(x) + ((y) * 4)], a[0], a[1], a[2])26852686int upper_gray_dist = 0, lower_gray_dist = 0, left_gray_dist = 0, right_gray_dist = 0;2687for (uint32_t i = 0; i < 4; i++)2688{2689for (uint32_t j = 0; j < 2; j++)2690{2691upper_gray_dist += GET_XY(i, j, upper_avg);2692lower_gray_dist += GET_XY(i, 2 + j, lower_avg);2693left_gray_dist += GET_XY(j, i, left_avg);2694right_gray_dist += GET_XY(2 + j, i, right_avg);2695}2696}26972698#undef GET_XY26992700int upper_lower_sum = upper_gray_dist + lower_gray_dist;2701int left_right_sum = left_gray_dist + right_gray_dist;27022703return upper_lower_sum < left_right_sum;2704}27052706static void compute_etc1_hints(etc_block& best_etc1_blk, uint32_t& best_etc1_bias, const uastc_encode_results& best_results, const color_rgba block[4][4], const color_rgba decoded_uastc_block[4][4], int level, uint32_t flags)2707{2708best_etc1_bias = 0;27092710if (best_results.m_uastc_mode == UASTC_MODE_INDEX_SOLID_COLOR)2711{2712pack_etc1_block_solid_color(best_etc1_blk, &best_results.m_solid_color.m_comps[0]);2713return;2714}27152716const bool faster_etc1 = (flags & cPackUASTCETC1FasterHints) != 0;2717const bool fastest_etc1 = (flags & cPackUASTCETC1FastestHints) != 0;27182719const bool has_bias = g_uastc_mode_has_etc1_bias[best_results.m_uastc_mode];27202721// 0 should be at the top, but we need 13 first because it represents bias (0,0,0).2722const uint8_t s_sorted_bias_modes[32] = { 13, 0, 22, 29, 27, 12, 26, 9, 30, 31, 8, 10, 25, 2, 23, 5, 15, 7, 3, 11, 6, 17, 28, 18, 1, 19, 20, 21, 24, 4, 14, 16 };27232724uint32_t last_bias = 1;2725bool use_faster_bias_mode_table = false;2726const bool flip_estimate = (level <= cPackUASTCLevelFaster) || (faster_etc1) || (fastest_etc1);2727if (has_bias)2728{2729switch (level)2730{2731case cPackUASTCLevelFastest:2732{2733last_bias = fastest_etc1 ? 1 : (faster_etc1 ? 1 : 2);2734use_faster_bias_mode_table = true;2735break;2736}2737case cPackUASTCLevelFaster:2738{2739last_bias = fastest_etc1 ? 1 : (faster_etc1 ? 3 : 5);2740use_faster_bias_mode_table = true;2741break;2742}2743case cPackUASTCLevelDefault:2744{2745last_bias = fastest_etc1 ? 1 : (faster_etc1 ? 10 : 20);2746use_faster_bias_mode_table = true;2747break;2748}2749case cPackUASTCLevelSlower:2750{2751last_bias = fastest_etc1 ? 1 : (faster_etc1 ? 16 : 32);2752use_faster_bias_mode_table = true;2753break;2754}2755default:2756{2757last_bias = 32;2758break;2759}2760}2761}27622763memset(&best_etc1_blk, 0, sizeof(best_etc1_blk));2764uint64_t best_err = UINT64_MAX;27652766etc_block trial_block;2767memset(&trial_block, 0, sizeof(trial_block));27682769ycbcr block_ycbcr[4][4], decoded_uastc_block_ycbcr[4][4];2770for (uint32_t y = 0; y < 4; y++)2771{2772for (uint32_t x = 0; x < 4; x++)2773{2774rgb_to_y_cb_cr(block[y][x], block_ycbcr[y][x]);2775rgb_to_y_cb_cr(decoded_uastc_block[y][x], decoded_uastc_block_ycbcr[y][x]);2776}2777}27782779uint32_t first_flip = 0, last_flip = 2;2780uint32_t first_individ = 0, last_individ = 2;27812782if (flags & cPackUASTCETC1DisableFlipAndIndividual)2783{2784last_flip = 1;2785last_individ = 1;2786}2787else if (flip_estimate)2788{2789if (pack_etc1_estimate_flipped(&decoded_uastc_block[0][0]))2790first_flip = 1;2791last_flip = first_flip + 1;2792}27932794for (uint32_t flip = first_flip; flip < last_flip; flip++)2795{2796trial_block.set_flip_bit(flip != 0);27972798for (uint32_t individ = first_individ; individ < last_individ; individ++)2799{2800const uint32_t mul = individ ? 15 : 31;28012802trial_block.set_diff_bit(individ == 0);28032804color_rgba unbiased_block_colors[2];28052806int min_r[2] = { 255, 255 }, min_g[2] = { 255, 255 }, min_b[2] = { 255, 255 }, max_r[2] = { 0, 0 }, max_g[2] = { 0, 0 }, max_b[2] = { 0, 0 };28072808for (uint32_t subset = 0; subset < 2; subset++)2809{2810uint32_t avg_color[3];2811memset(avg_color, 0, sizeof(avg_color));28122813for (uint32_t j = 0; j < 8; j++)2814{2815const etc_coord2 &c = g_etc1_pixel_coords[flip][subset][j];2816const color_rgba& p = decoded_uastc_block[c.m_y][c.m_x];28172818avg_color[0] += p.r;2819avg_color[1] += p.g;2820avg_color[2] += p.b;28212822min_r[subset] = basisu::minimum<uint32_t>(min_r[subset], p.r);2823min_g[subset] = basisu::minimum<uint32_t>(min_g[subset], p.g);2824min_b[subset] = basisu::minimum<uint32_t>(min_b[subset], p.b);28252826max_r[subset] = basisu::maximum<uint32_t>(max_r[subset], p.r);2827max_g[subset] = basisu::maximum<uint32_t>(max_g[subset], p.g);2828max_b[subset] = basisu::maximum<uint32_t>(max_b[subset], p.b);2829} // j28302831unbiased_block_colors[subset][0] = (uint8_t)((avg_color[0] * mul + 1020) / (8 * 255));2832unbiased_block_colors[subset][1] = (uint8_t)((avg_color[1] * mul + 1020) / (8 * 255));2833unbiased_block_colors[subset][2] = (uint8_t)((avg_color[2] * mul + 1020) / (8 * 255));2834unbiased_block_colors[subset][3] = 0;28352836} // subset28372838for (uint32_t bias_iter = 0; bias_iter < last_bias; bias_iter++)2839{2840const uint32_t bias = use_faster_bias_mode_table ? s_sorted_bias_modes[bias_iter] : bias_iter;28412842color_rgba block_colors[2];2843for (uint32_t subset = 0; subset < 2; subset++)2844block_colors[subset] = has_bias ? apply_etc1_bias((color32&)unbiased_block_colors[subset], bias, mul, subset) : unbiased_block_colors[subset];28452846if (individ)2847trial_block.set_block_color4(block_colors[0], block_colors[1]);2848else2849trial_block.set_block_color5_clamp(block_colors[0], block_colors[1]);28502851uint32_t range[2];2852for (uint32_t subset = 0; subset < 2; subset++)2853{2854const color_rgba base_c(trial_block.get_block_color(subset, true));28552856const int pos_r = iabs(max_r[subset] - base_c.r);2857const int neg_r = iabs(base_c.r - min_r[subset]);28582859const int pos_g = iabs(max_g[subset] - base_c.g);2860const int neg_g = iabs(base_c.g - min_g[subset]);28612862const int pos_b = iabs(max_b[subset] - base_c.b);2863const int neg_b = iabs(base_c.b - min_b[subset]);28642865range[subset] = maximum(maximum(pos_r, neg_r, pos_g, neg_g), pos_b, neg_b);2866}28672868uint32_t best_inten_table[2] = { 0, 0 };28692870for (uint32_t subset = 0; subset < 2; subset++)2871{2872uint64_t best_subset_err = UINT64_MAX;28732874const uint32_t inten_table_limit = (level == cPackUASTCLevelVerySlow) ? 8 : ((range[subset] > 51) ? 8 : (range[subset] >= 7 ? 4 : 2));28752876for (uint32_t inten_table = 0; inten_table < inten_table_limit; inten_table++)2877{2878trial_block.set_inten_table(subset, inten_table);28792880color_rgba color_table[4];2881trial_block.get_block_colors(color_table, subset);28822883ycbcr color_table_ycbcr[4];2884for (uint32_t i = 0; i < 4; i++)2885rgb_to_y_cb_cr(color_table[i], color_table_ycbcr[i]);28862887uint64_t total_error = 0;2888if (flip)2889{2890for (uint32_t y = 0; y < 2; y++)2891{2892{2893const ycbcr& c = decoded_uastc_block_ycbcr[subset * 2 + y][0];2894total_error += minimum(color_diff(color_table_ycbcr[0], c), color_diff(color_table_ycbcr[1], c), color_diff(color_table_ycbcr[2], c), color_diff(color_table_ycbcr[3], c));2895}2896{2897const ycbcr& c = decoded_uastc_block_ycbcr[subset * 2 + y][1];2898total_error += minimum(color_diff(color_table_ycbcr[0], c), color_diff(color_table_ycbcr[1], c), color_diff(color_table_ycbcr[2], c), color_diff(color_table_ycbcr[3], c));2899}2900{2901const ycbcr& c = decoded_uastc_block_ycbcr[subset * 2 + y][2];2902total_error += minimum(color_diff(color_table_ycbcr[0], c), color_diff(color_table_ycbcr[1], c), color_diff(color_table_ycbcr[2], c), color_diff(color_table_ycbcr[3], c));2903}2904{2905const ycbcr& c = decoded_uastc_block_ycbcr[subset * 2 + y][3];2906total_error += minimum(color_diff(color_table_ycbcr[0], c), color_diff(color_table_ycbcr[1], c), color_diff(color_table_ycbcr[2], c), color_diff(color_table_ycbcr[3], c));2907}2908if (total_error >= best_subset_err)2909break;2910}2911}2912else2913{2914for (uint32_t y = 0; y < 4; y++)2915{2916{2917const ycbcr& c = decoded_uastc_block_ycbcr[y][subset * 2 + 0];2918total_error += minimum(color_diff(color_table_ycbcr[0], c), color_diff(color_table_ycbcr[1], c), color_diff(color_table_ycbcr[2], c), color_diff(color_table_ycbcr[3], c));2919}2920{2921const ycbcr& c = decoded_uastc_block_ycbcr[y][subset * 2 + 1];2922total_error += minimum(color_diff(color_table_ycbcr[0], c), color_diff(color_table_ycbcr[1], c), color_diff(color_table_ycbcr[2], c), color_diff(color_table_ycbcr[3], c));2923}2924}2925if (total_error >= best_subset_err)2926break;2927}29282929if (total_error < best_subset_err)2930{2931best_subset_err = total_error;2932best_inten_table[subset] = inten_table;2933}29342935} // inten_table29362937} // subset29382939trial_block.set_inten_table(0, best_inten_table[0]);2940trial_block.set_inten_table(1, best_inten_table[1]);29412942// Compute error against the ORIGINAL block.2943uint64_t err = 0;29442945for (uint32_t subset = 0; subset < 2; subset++)2946{2947color_rgba color_table[4];2948trial_block.get_block_colors(color_table, subset);29492950ycbcr color_table_ycbcr[4];2951for (uint32_t i = 0; i < 4; i++)2952rgb_to_y_cb_cr(color_table[i], color_table_ycbcr[i]);29532954if (flip)2955{2956for (uint32_t y = 0; y < 2; y++)2957{2958for (uint32_t x = 0; x < 4; x++)2959{2960const ycbcr& c = decoded_uastc_block_ycbcr[subset * 2 + y][x];2961const uint64_t best_index_err = minimum(color_diff(color_table_ycbcr[0], c) << 2, (color_diff(color_table_ycbcr[1], c) << 2) + 1, (color_diff(color_table_ycbcr[2], c) << 2) + 2, (color_diff(color_table_ycbcr[3], c) << 2) + 3);29622963const uint32_t best_index = (uint32_t)best_index_err & 3;2964err += color_diff(block_ycbcr[subset * 2 + y][x], color_table_ycbcr[best_index]);2965}2966if (err >= best_err)2967break;2968}2969}2970else2971{2972for (uint32_t y = 0; y < 4; y++)2973{2974for (uint32_t x = 0; x < 2; x++)2975{2976const ycbcr& c = decoded_uastc_block_ycbcr[y][subset * 2 + x];2977const uint64_t best_index_err = minimum(color_diff(color_table_ycbcr[0], c) << 2, (color_diff(color_table_ycbcr[1], c) << 2) + 1, (color_diff(color_table_ycbcr[2], c) << 2) + 2, (color_diff(color_table_ycbcr[3], c) << 2) + 3);29782979const uint32_t best_index = (uint32_t)best_index_err & 3;2980err += color_diff(block_ycbcr[y][subset * 2 + x], color_table_ycbcr[best_index]);2981}2982if (err >= best_err)2983break;2984}2985}29862987} // subset29882989if (err < best_err)2990{2991best_err = err;29922993best_etc1_blk = trial_block;2994best_etc1_bias = bias;2995}29962997} // bias_iter29982999} // individ30003001} // flip3002}30033004struct uastc_pack_eac_a8_results3005{3006uint32_t m_base;3007uint32_t m_table;3008uint32_t m_multiplier;3009};30103011static uint64_t uastc_pack_eac_a8(uastc_pack_eac_a8_results& results, const uint8_t* pPixels, uint32_t num_pixels, uint32_t base_search_rad, uint32_t mul_search_rad, uint32_t table_mask)3012{3013assert(num_pixels <= 16);30143015uint32_t min_alpha = 255, max_alpha = 0;3016for (uint32_t i = 0; i < num_pixels; i++)3017{3018const uint32_t a = pPixels[i];3019if (a < min_alpha) min_alpha = a;3020if (a > max_alpha) max_alpha = a;3021}30223023if (min_alpha == max_alpha)3024{3025results.m_base = min_alpha;3026results.m_table = 13;3027results.m_multiplier = 1;3028return 0;3029}30303031const uint32_t alpha_range = max_alpha - min_alpha;30323033uint64_t best_err = UINT64_MAX;30343035for (uint32_t table = 0; table < 16; table++)3036{3037if ((table_mask & (1U << table)) == 0)3038continue;30393040const float range = (float)(g_etc2_eac_tables[table][ETC2_EAC_MAX_VALUE_SELECTOR] - g_etc2_eac_tables[table][ETC2_EAC_MIN_VALUE_SELECTOR]);3041const int center = (int)roundf(lerp((float)min_alpha, (float)max_alpha, (float)(0 - g_etc2_eac_tables[table][ETC2_EAC_MIN_VALUE_SELECTOR]) / range));30423043const int base_min = clamp255(center - base_search_rad);3044const int base_max = clamp255(center + base_search_rad);30453046const int mul = (int)roundf(alpha_range / range);3047const int mul_low = clamp<int>(mul - mul_search_rad, 1, 15);3048const int mul_high = clamp<int>(mul + mul_search_rad, 1, 15);30493050for (int base = base_min; base <= base_max; base++)3051{3052for (int multiplier = mul_low; multiplier <= mul_high; multiplier++)3053{3054uint64_t total_err = 0;30553056for (uint32_t i = 0; i < num_pixels; i++)3057{3058const int a = pPixels[i];30593060uint32_t best_s_err = UINT32_MAX;3061//uint32_t best_s = 0;3062for (uint32_t s = 0; s < 8; s++)3063{3064const int v = clamp255((int)multiplier * g_etc2_eac_tables[table][s] + (int)base);30653066uint32_t err = iabs(a - v);3067if (err < best_s_err)3068{3069best_s_err = err;3070//best_s = s;3071}3072}30733074total_err += best_s_err * best_s_err;3075if (total_err >= best_err)3076break;3077}30783079if (total_err < best_err)3080{3081best_err = total_err;3082results.m_base = base;3083results.m_multiplier = multiplier;3084results.m_table = table;3085if (!best_err)3086return best_err;3087}30883089} // table30903091} // multiplier30923093} // base30943095return best_err;3096}30973098const int32_t DEFAULT_BC7_ERROR_WEIGHT = 50;3099const float UASTC_ERROR_THRESH = 1.3f;31003101// TODO: This is a quick hack to favor certain modes when we know we'll be followed up with an RDO postprocess.3102static inline float get_uastc_mode_weight(uint32_t mode)3103{3104const float FAVORED_MODE_WEIGHT = .8f;31053106switch (mode)3107{3108case 0:3109case 10:3110return FAVORED_MODE_WEIGHT;3111default:3112break;3113}31143115return 1.0f;3116}31173118void encode_uastc(const uint8_t* pRGBAPixels, uastc_block& output_block, uint32_t flags)3119{3120// printf("encode_uastc: \n");3121// for (int i = 0; i < 16; i++)3122// printf("[%u %u %u %u] ", pRGBAPixels[i * 4 + 0], pRGBAPixels[i * 4 + 1], pRGBAPixels[i * 4 + 2], pRGBAPixels[i * 4 + 3]);3123// printf("\n");31243125const color_rgba(*block)[4] = reinterpret_cast<const color_rgba(*)[4]>(pRGBAPixels);31263127bool solid_color = true, has_alpha = false, is_la = true;31283129const color_rgba first_color(block[0][0]);3130for (uint32_t y = 0; y < 4; y++)3131{3132for (uint32_t x = 0; x < 4; x++)3133{3134if (block[y][x].a < 255)3135has_alpha = true;31363137if (block[y][x] != first_color)3138solid_color = false;31393140if ((block[y][x].r != block[y][x].g) || (block[y][x].r != block[y][x].b))3141is_la = false;3142}3143}31443145if (solid_color)3146{3147// Solid color blocks are so common that we handle them specially and as quickly as we can.3148uastc_encode_results solid_results;3149solid_results.m_uastc_mode = UASTC_MODE_INDEX_SOLID_COLOR;3150solid_results.m_astc_err = 0;3151solid_results.m_common_pattern = 0;3152solid_results.m_solid_color = first_color;3153memset(&solid_results.m_astc, 0, sizeof(solid_results.m_astc));31543155etc_block etc1_blk;3156uint32_t etc1_bias = 0;31573158pack_etc1_block_solid_color(etc1_blk, &first_color.m_comps[0]);31593160eac_a8_block eac_a8_blk;3161eac_a8_blk.m_table = 0;3162eac_a8_blk.m_multiplier = 1;31633164pack_uastc(output_block, solid_results, etc1_blk, etc1_bias, eac_a8_blk, false, false);31653166// printf(" Solid\n");31673168return;3169}31703171int level = flags & 7;3172const bool favor_uastc_error = (flags & cPackUASTCFavorUASTCError) != 0;3173const bool favor_bc7_error = !favor_uastc_error && ((flags & cPackUASTCFavorBC7Error) != 0);3174//const bool etc1_perceptual = true;31753176// TODO: This uses 64KB of stack space!3177uastc_encode_results results[MAX_ENCODE_RESULTS];31783179level = clampi(level, cPackUASTCLevelFastest, cPackUASTCLevelVerySlow);31803181// Set all options to slowest, then configure from there depending on the selected level.3182uint32_t mode_mask = UINT32_MAX;3183uint32_t uber_level = 6;3184bool estimate_partition = false;3185bool always_try_alpha_modes = true;3186uint32_t eac_a8_mul_search_rad = 3;3187uint32_t eac_a8_table_mask = UINT32_MAX;3188uint32_t least_squares_passes = 2;3189bool bc1_hints = true;3190bool only_use_la_on_transparent_blocks = false;31913192switch (level)3193{3194case cPackUASTCLevelFastest:3195{3196mode_mask = (1 << 0) | (1 << 8) |3197(1 << 11) | (1 << 12) |3198(1 << 15);3199always_try_alpha_modes = false;3200eac_a8_mul_search_rad = 0;3201eac_a8_table_mask = (1 << 2) | (1 << 8) | (1 << 11) | (1 << 13);3202uber_level = 0;3203least_squares_passes = 1;3204bc1_hints = false;3205estimate_partition = true;3206only_use_la_on_transparent_blocks = true;3207break;3208}3209case cPackUASTCLevelFaster:3210{3211mode_mask = (1 << 0) | (1 << 4) | (1 << 6) | (1 << 8) |3212(1 << 9) | (1 << 11) | (1 << 12) |3213(1 << 15) | (1 << 17);3214always_try_alpha_modes = false;3215eac_a8_mul_search_rad = 0;3216eac_a8_table_mask = (1 << 2) | (1 << 8) | (1 << 11) | (1 << 13);3217uber_level = 0;3218least_squares_passes = 1;3219estimate_partition = true;3220break;3221}3222case cPackUASTCLevelDefault:3223{3224mode_mask = (1 << 0) | (1 << 1) | (1 << 4) | (1 << 5) | (1 << 6) | (1 << 8) |3225(1 << 9) | (1 << 10) | (1 << 11) | (1 << 12) | (1 << 13) |3226(1 << 15) | (1 << 16) | (1 << 17);3227always_try_alpha_modes = false;3228eac_a8_mul_search_rad = 1;3229eac_a8_table_mask = (1 << 0) | (1 << 2) | (1 << 6) | (1 << 7) | (1 << 8) | (1 << 10) | (1 << 11) | (1 << 13);3230uber_level = 1;3231least_squares_passes = 1;3232estimate_partition = true;3233break;3234}3235case cPackUASTCLevelSlower:3236{3237always_try_alpha_modes = false;3238eac_a8_mul_search_rad = 2;3239uber_level = 3;3240estimate_partition = true;3241break;3242}3243case cPackUASTCLevelVerySlow:3244{3245break;3246}3247}32483249#if BASISU_SUPPORT_FORCE_MODE3250static int force_mode = -1;3251force_mode = (force_mode + 1) % TOTAL_UASTC_MODES;3252mode_mask = UINT32_MAX;3253always_try_alpha_modes = true;3254only_use_la_on_transparent_blocks = false;3255#endif32563257// HACK HACK3258//mode_mask &= ~(1 << 18);3259//mode_mask = (1 << 18)| (1 << 10);32603261uint32_t total_results = 0;32623263if (only_use_la_on_transparent_blocks)3264{3265if ((is_la) && (!has_alpha))3266is_la = false;3267}32683269const bool try_alpha_modes = has_alpha || always_try_alpha_modes;32703271bc7enc_compress_block_params comp_params;3272memset(&comp_params, 0, sizeof(comp_params));3273comp_params.m_max_partitions_mode1 = 64;3274comp_params.m_least_squares_passes = least_squares_passes;3275comp_params.m_weights[0] = 1;3276comp_params.m_weights[1] = 1;3277comp_params.m_weights[2] = 1;3278comp_params.m_weights[3] = 1;3279comp_params.m_uber_level = uber_level;32803281if (is_la)3282{3283if (mode_mask & (1U << 15))3284astc_mode15(block, results, total_results, comp_params);32853286if (mode_mask & (1U << 16))3287astc_mode9_or_16(16, block, results, total_results, comp_params, estimate_partition ? 4 : 0);32883289if (mode_mask & (1U << 17))3290astc_mode11_or_17(17, block, results, total_results, comp_params);3291}32923293if (!has_alpha)3294{3295if (mode_mask & (1U << 0))3296astc_mode0_or_18(0, block, results, total_results, comp_params);32973298if (mode_mask & (1U << 1))3299astc_mode1(block, results, total_results, comp_params);33003301if (mode_mask & (1U << 2))3302astc_mode2(block, results, total_results, comp_params, estimate_partition);33033304if (mode_mask & (1U << 3))3305astc_mode3(block, results, total_results, comp_params, estimate_partition);33063307if (mode_mask & (1U << 4))3308astc_mode4(block, results, total_results, comp_params, estimate_partition);33093310if (mode_mask & (1U << 5))3311astc_mode5(block, results, total_results, comp_params);33123313if (mode_mask & (1U << 6))3314astc_mode6(block, results, total_results, comp_params);33153316if (mode_mask & (1U << 7))3317astc_mode7(block, results, total_results, comp_params, estimate_partition);33183319if (mode_mask & (1U << 18))3320astc_mode0_or_18(18, block, results, total_results, comp_params);3321}33223323if (try_alpha_modes)3324{3325if (mode_mask & (1U << 9))3326astc_mode9_or_16(9, block, results, total_results, comp_params, estimate_partition ? 4 : 0);33273328if (mode_mask & (1U << 10))3329astc_mode10(block, results, total_results, comp_params);33303331if (mode_mask & (1U << 11))3332astc_mode11_or_17(11, block, results, total_results, comp_params);33333334if (mode_mask & (1U << 12))3335astc_mode12(block, results, total_results, comp_params);33363337if (mode_mask & (1U << 13))3338astc_mode13(block, results, total_results, comp_params);33393340if (mode_mask & (1U << 14))3341astc_mode14(block, results, total_results, comp_params);3342}33433344assert(total_results);33453346// Fix up the errors so we consistently have LA, RGB, or RGBA error.3347for (uint32_t i = 0; i < total_results; i++)3348{3349uastc_encode_results& r = results[i];3350if (!is_la)3351{3352if (g_uastc_mode_is_la[r.m_uastc_mode])3353{3354color_rgba unpacked_block[16];3355unpack_uastc(r.m_uastc_mode, r.m_common_pattern, r.m_solid_color.get_color32(), r.m_astc, (basist::color32 *)unpacked_block, false);33563357uint64_t total_err = 0;3358for (uint32_t j = 0; j < 16; j++)3359total_err += color_distance(unpacked_block[j], ((const color_rgba*)block)[j], true);33603361r.m_astc_err = total_err;3362}3363}3364else3365{3366if (!g_uastc_mode_is_la[r.m_uastc_mode])3367{3368color_rgba unpacked_block[16];3369unpack_uastc(r.m_uastc_mode, r.m_common_pattern, r.m_solid_color.get_color32(), r.m_astc, (basist::color32 *)unpacked_block, false);33703371uint64_t total_err = 0;3372for (uint32_t j = 0; j < 16; j++)3373total_err += color_distance_la(unpacked_block[j], ((const color_rgba*)block)[j]);33743375r.m_astc_err = total_err;3376}3377}3378}33793380unpacked_uastc_block unpacked_ublock;3381memset(&unpacked_ublock, 0, sizeof(unpacked_ublock));33823383uint64_t total_overall_err[MAX_ENCODE_RESULTS];3384float uastc_err_f[MAX_ENCODE_RESULTS];3385double best_uastc_err_f = 1e+20f;33863387int best_index = -1;33883389if (total_results == 1)3390{3391best_index = 0;3392}3393else3394{3395const uint32_t bc7_err_weight = favor_bc7_error ? 100 : ((favor_uastc_error ? 0 : DEFAULT_BC7_ERROR_WEIGHT));3396const uint32_t uastc_err_weight = favor_bc7_error ? 0 : 100;33973398// Find best overall results, balancing UASTC and UASTC->BC7 error.3399// We purposely allow UASTC error to increase a little, if doing so lowers the BC7 error.3400for (uint32_t i = 0; i < total_results; i++)3401{3402#if BASISU_SUPPORT_FORCE_MODE3403if (results[i].m_uastc_mode == force_mode)3404{3405best_index = i;3406break;3407}3408#endif34093410unpacked_ublock.m_mode = results[i].m_uastc_mode;3411unpacked_ublock.m_astc = results[i].m_astc;3412unpacked_ublock.m_common_pattern = results[i].m_common_pattern;3413unpacked_ublock.m_solid_color = results[i].m_solid_color.get_color32();34143415color_rgba decoded_uastc_block[4][4];3416bool success = unpack_uastc(results[i].m_uastc_mode, results[i].m_common_pattern, results[i].m_solid_color.get_color32(), results[i].m_astc, (basist::color32 *)&decoded_uastc_block[0][0], false);3417(void)success;3418VALIDATE(success);34193420uint64_t total_uastc_rgb_err, total_uastc_rgba_err, total_uastc_la_err;3421compute_block_error(block, decoded_uastc_block, total_uastc_rgb_err, total_uastc_rgba_err, total_uastc_la_err);34223423// Validate the computed error, or we're go mad if it's inaccurate.3424if (results[i].m_uastc_mode == UASTC_MODE_INDEX_SOLID_COLOR)3425{3426VALIDATE(total_uastc_rgba_err == 0);3427}3428else if (is_la)3429{3430VALIDATE(total_uastc_la_err == results[i].m_astc_err);3431}3432else if (g_uastc_mode_has_alpha[results[i].m_uastc_mode])3433{3434VALIDATE(total_uastc_rgba_err == results[i].m_astc_err);3435}3436else3437{3438VALIDATE(total_uastc_rgb_err == results[i].m_astc_err);3439}34403441// Transcode to BC73442bc7_optimization_results bc7_results;3443transcode_uastc_to_bc7(unpacked_ublock, bc7_results);34443445bc7_block bc7_data;3446encode_bc7_block(&bc7_data, &bc7_results);34473448color_rgba decoded_bc7_block[4][4];3449unpack_block(texture_format::cBC7, &bc7_data, &decoded_bc7_block[0][0]);34503451// Compute BC7 error3452uint64_t total_bc7_la_err, total_bc7_rgb_err, total_bc7_rgba_err;3453compute_block_error(block, decoded_bc7_block, total_bc7_rgb_err, total_bc7_rgba_err, total_bc7_la_err);34543455if (results[i].m_uastc_mode == UASTC_MODE_INDEX_SOLID_COLOR)3456{3457VALIDATE(total_bc7_rgba_err == 0);34583459best_index = i;3460break;3461}34623463uint64_t total_uastc_err = 0, total_bc7_err = 0;3464if (is_la)3465{3466total_bc7_err = total_bc7_la_err;3467total_uastc_err = total_uastc_la_err;3468}3469else if (has_alpha)3470{3471total_bc7_err = total_bc7_rgba_err;3472total_uastc_err = total_uastc_rgba_err;3473}3474else3475{3476total_bc7_err = total_bc7_rgb_err;3477total_uastc_err = total_uastc_rgb_err;3478}34793480total_overall_err[i] = ((total_bc7_err * bc7_err_weight) / 100) + ((total_uastc_err * uastc_err_weight) / 100);3481if (!total_overall_err[i])3482{3483best_index = i;3484break;3485}34863487uastc_err_f[i] = sqrtf((float)total_uastc_err);34883489if (uastc_err_f[i] < best_uastc_err_f)3490{3491best_uastc_err_f = uastc_err_f[i];3492}34933494} // total_results34953496if (best_index < 0)3497{3498uint64_t best_err = UINT64_MAX;34993500if ((best_uastc_err_f == 0.0f) || (favor_bc7_error))3501{3502for (uint32_t i = 0; i < total_results; i++)3503{3504// TODO: This is a quick hack to favor modes 0 or 10 for better RDO compression.3505const float err_weight = (flags & cPackUASTCFavorSimplerModes) ? get_uastc_mode_weight(results[i].m_uastc_mode) : 1.0f;35063507const uint64_t w = (uint64_t)(total_overall_err[i] * err_weight);3508if (w < best_err)3509{3510best_err = w;3511best_index = i;3512if (!best_err)3513break;3514}3515} // i3516}3517else3518{3519// Scan the UASTC results, and consider all results within a window that has the best UASTC+BC7 error.3520for (uint32_t i = 0; i < total_results; i++)3521{3522double err_delta = uastc_err_f[i] / best_uastc_err_f;35233524if (err_delta <= UASTC_ERROR_THRESH)3525{3526// TODO: This is a quick hack to favor modes 0 or 10 for better RDO compression.3527const float err_weight = (flags & cPackUASTCFavorSimplerModes) ? get_uastc_mode_weight(results[i].m_uastc_mode) : 1.0f;35283529const uint64_t w = (uint64_t)(total_overall_err[i] * err_weight);3530if (w < best_err)3531{3532best_err = w;3533best_index = i;3534if (!best_err)3535break;3536}3537}3538} // i3539}3540}3541}35423543const uastc_encode_results& best_results = results[best_index];3544const uint32_t best_mode = best_results.m_uastc_mode;3545const astc_block_desc& best_astc_results = best_results.m_astc;35463547color_rgba decoded_uastc_block[4][4];3548bool success = unpack_uastc(best_mode, best_results.m_common_pattern, best_results.m_solid_color.get_color32(), best_astc_results, (basist::color32 *)&decoded_uastc_block[0][0], false);3549(void)success;3550VALIDATE(success);35513552#if BASISU_VALIDATE_UASTC_ENC3553// Make sure that the UASTC block unpacks to the same exact pixels as the ASTC block does, using two different decoders.3554{3555// Round trip to packed UASTC and back, then decode to pixels.3556etc_block etc1_blk;3557memset(&etc1_blk, 0, sizeof(etc1_blk));3558eac_a8_block etc_eac_a8_blk;3559memset(&etc_eac_a8_blk, 0, sizeof(etc_eac_a8_blk));3560etc_eac_a8_blk.m_multiplier = 1;35613562basist::uastc_block temp_block;3563pack_uastc(temp_block, best_results, etc1_blk, 0, etc_eac_a8_blk, false, false);35643565basist::color32 temp_block_unpacked[4][4];3566success = basist::unpack_uastc(temp_block, (basist::color32 *)temp_block_unpacked, false);3567VALIDATE(success);35683569// Now round trip to packed ASTC and back, then decode to pixels.3570uint32_t astc_data[4];35713572if (best_results.m_uastc_mode == UASTC_MODE_INDEX_SOLID_COLOR)3573pack_astc_solid_block(astc_data, (color32 &)best_results.m_solid_color);3574else3575{3576success = pack_astc_block(astc_data, &best_astc_results, best_results.m_uastc_mode);3577VALIDATE(success);3578}35793580color_rgba decoded_astc_block[4][4];3581success = basisu_astc::astc::decompress_ldr((uint8_t*)decoded_astc_block, (uint8_t*)&astc_data, false, 4, 4);3582VALIDATE(success);35833584for (uint32_t y = 0; y < 4; y++)3585{3586for (uint32_t x = 0; x < 4; x++)3587{3588VALIDATE(decoded_astc_block[y][x] == decoded_uastc_block[y][x]);35893590VALIDATE(temp_block_unpacked[y][x].c[0] == decoded_uastc_block[y][x].r);3591VALIDATE(temp_block_unpacked[y][x].c[1] == decoded_uastc_block[y][x].g);3592VALIDATE(temp_block_unpacked[y][x].c[2] == decoded_uastc_block[y][x].b);3593VALIDATE(temp_block_unpacked[y][x].c[3] == decoded_uastc_block[y][x].a);3594}3595}3596}3597#endif35983599// Compute BC1 hints3600bool bc1_hint0 = false, bc1_hint1 = false;3601if (bc1_hints)3602compute_bc1_hints(bc1_hint0, bc1_hint1, best_results, block, decoded_uastc_block);36033604eac_a8_block eac_a8_blk;3605if ((g_uastc_mode_has_alpha[best_mode]) && (best_mode != UASTC_MODE_INDEX_SOLID_COLOR))3606{3607// Compute ETC2 hints3608uint8_t decoded_uastc_block_alpha[16];3609for (uint32_t i = 0; i < 16; i++)3610decoded_uastc_block_alpha[i] = decoded_uastc_block[i >> 2][i & 3].a;36113612uastc_pack_eac_a8_results eac8_a8_results;3613memset(&eac8_a8_results, 0, sizeof(eac8_a8_results));3614uastc_pack_eac_a8(eac8_a8_results, decoded_uastc_block_alpha, 16, 0, eac_a8_mul_search_rad, eac_a8_table_mask);36153616// All we care about for hinting is the table and multiplier.3617eac_a8_blk.m_table = eac8_a8_results.m_table;3618eac_a8_blk.m_multiplier = eac8_a8_results.m_multiplier;3619}3620else3621{3622memset(&eac_a8_blk, 0, sizeof(eac_a8_blk));3623}36243625// Compute ETC1 hints3626etc_block etc1_blk;3627uint32_t etc1_bias = 0;3628compute_etc1_hints(etc1_blk, etc1_bias, best_results, block, decoded_uastc_block, level, flags);36293630// Finally, pack the UASTC block with its hints and we're done.3631pack_uastc(output_block, best_results, etc1_blk, etc1_bias, eac_a8_blk, bc1_hint0, bc1_hint1);36323633// printf(" Packed: ");3634// for (int i = 0; i < 16; i++)3635// printf("%X ", output_block.m_bytes[i]);3636// printf("\n");3637}36383639static bool uastc_recompute_hints(basist::uastc_block* pBlock, const color_rgba* pBlock_pixels, uint32_t flags, const unpacked_uastc_block *pUnpacked_blk)3640{3641unpacked_uastc_block unpacked_blk;36423643if (pUnpacked_blk)3644unpacked_blk = *pUnpacked_blk;3645else3646{3647if (!unpack_uastc(*pBlock, unpacked_blk, false, true))3648return false;3649}3650color_rgba decoded_uastc_block[4][4];3651if (!unpack_uastc(unpacked_blk, (basist::color32 *)decoded_uastc_block, false))3652return false;3653uastc_encode_results results;3654results.m_uastc_mode = unpacked_blk.m_mode;3655results.m_common_pattern = unpacked_blk.m_common_pattern;3656results.m_astc = unpacked_blk.m_astc;3657results.m_solid_color = unpacked_blk.m_solid_color;3658results.m_astc_err = 0;3659bool bc1_hints = true;3660uint32_t eac_a8_mul_search_rad = 3;3661uint32_t eac_a8_table_mask = UINT32_MAX;3662const uint32_t level = flags & cPackUASTCLevelMask;3663switch (level)3664{3665case cPackUASTCLevelFastest:3666{3667eac_a8_mul_search_rad = 0;3668eac_a8_table_mask = (1 << 2) | (1 << 8) | (1 << 11) | (1 << 13);3669bc1_hints = false;3670break;3671}3672case cPackUASTCLevelFaster:3673{3674eac_a8_mul_search_rad = 0;3675eac_a8_table_mask = (1 << 2) | (1 << 8) | (1 << 11) | (1 << 13);3676break;3677}3678case cPackUASTCLevelDefault:3679{3680eac_a8_mul_search_rad = 1;3681eac_a8_table_mask = (1 << 0) | (1 << 2) | (1 << 6) | (1 << 7) | (1 << 8) | (1 << 10) | (1 << 11) | (1 << 13);3682break;3683}3684case cPackUASTCLevelSlower:3685{3686eac_a8_mul_search_rad = 2;3687break;3688}3689case cPackUASTCLevelVerySlow:3690{3691break;3692}3693}3694bool bc1_hint0 = false, bc1_hint1 = false;3695if (bc1_hints)3696compute_bc1_hints(bc1_hint0, bc1_hint1, results, (color_rgba (*)[4])pBlock_pixels, decoded_uastc_block);3697const uint32_t best_mode = unpacked_blk.m_mode;3698eac_a8_block eac_a8_blk;3699if ((g_uastc_mode_has_alpha[best_mode]) && (best_mode != UASTC_MODE_INDEX_SOLID_COLOR))3700{3701uint8_t decoded_uastc_block_alpha[16];3702for (uint32_t i = 0; i < 16; i++)3703decoded_uastc_block_alpha[i] = decoded_uastc_block[i >> 2][i & 3].a;3704uastc_pack_eac_a8_results eac8_a8_results;3705memset(&eac8_a8_results, 0, sizeof(eac8_a8_results));3706uastc_pack_eac_a8(eac8_a8_results, decoded_uastc_block_alpha, 16, 0, eac_a8_mul_search_rad, eac_a8_table_mask);3707eac_a8_blk.m_table = eac8_a8_results.m_table;3708eac_a8_blk.m_multiplier = eac8_a8_results.m_multiplier;3709}3710else3711{3712memset(&eac_a8_blk, 0, sizeof(eac_a8_blk));3713}3714etc_block etc1_blk;3715uint32_t etc1_bias = 0;3716compute_etc1_hints(etc1_blk, etc1_bias, results, (color_rgba (*)[4])pBlock_pixels, decoded_uastc_block, level, flags);3717pack_uastc(*pBlock, results, etc1_blk, etc1_bias, eac_a8_blk, bc1_hint0, bc1_hint1);3718return true;3719}37203721static const uint8_t g_uastc_mode_selector_bits[TOTAL_UASTC_MODES][2] =3722{3723{ 65, 63 }, { 69, 31 }, { 73, 46 }, { 89, 29 },3724{ 89, 30 }, { 68, 47 }, { 66, 62 }, { 89, 30 },3725{ 0, 0 }, { 97, 30 }, { 65, 63 }, { 66, 62 },3726{ 81, 47 }, { 94, 30 }, { 92, 31 }, { 62, 63 },3727{ 98, 30 }, { 61, 62 }, { 49, 79 }3728};37293730static inline uint32_t set_block_bits(uint8_t* pBytes, uint64_t val, uint32_t num_bits, uint32_t cur_ofs)3731{3732assert(num_bits <= 64);3733assert((num_bits == 64) || (val < (1ULL << num_bits)));3734uint64_t mask = (num_bits == 64) ? UINT64_MAX : ((1ULL << num_bits) - 1);3735while (num_bits)3736{3737const uint32_t n = basisu::minimum<uint32_t>(8U - (cur_ofs & 7U), num_bits);3738pBytes[cur_ofs >> 3] &= ~static_cast<uint8_t>(mask << (cur_ofs & 7U));3739pBytes[cur_ofs >> 3] |= static_cast<uint8_t>(val << (cur_ofs & 7U));3740val >>= n;3741mask >>= n;3742num_bits -= n;3743cur_ofs += n;3744}3745return cur_ofs;3746}37473748static const uint8_t g_tdefl_small_dist_extra[512] =3749{37500, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5,37515, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,37526, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,37536, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,37547, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,37557, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,37567, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,37577, 7, 7, 7, 7, 7, 7, 73758};37593760static const uint8_t g_tdefl_large_dist_extra[128] =3761{37620, 0, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,376312, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,376413, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 133765};37663767static inline uint32_t compute_match_cost_estimate(uint32_t dist)3768{3769uint32_t len_cost = 7;3770uint32_t dist_cost = 5;3771if (dist < 512)3772dist_cost += g_tdefl_small_dist_extra[dist & 511];3773else3774{3775dist_cost += g_tdefl_large_dist_extra[basisu::minimum<uint32_t>(dist, 32767) >> 8];3776while (dist >= 32768)3777{3778dist_cost++;3779dist >>= 1;3780}3781}3782return len_cost + dist_cost;3783}37843785struct selector_bitsequence3786{3787uint64_t m_sel;3788uint32_t m_ofs;3789uint32_t m_pad; // avoid implicit padding for selector_bitsequence_hash3790selector_bitsequence() { }3791selector_bitsequence(uint32_t bit_ofs, uint64_t sel) : m_sel(sel), m_ofs(bit_ofs), m_pad(0) { }3792bool operator== (const selector_bitsequence& other) const3793{3794return (m_ofs == other.m_ofs) && (m_sel == other.m_sel);3795}37963797bool operator< (const selector_bitsequence& other) const3798{3799if (m_ofs < other.m_ofs)3800return true;3801else if (m_ofs == other.m_ofs)3802return m_sel < other.m_sel;38033804return false;3805}3806};38073808struct selector_bitsequence_hash3809{3810std::size_t operator()(selector_bitsequence const& s) const noexcept3811{3812return hash_hsieh((const uint8_t*)&s, sizeof(s));3813}3814};38153816static bool uastc_rdo_blocks(uint32_t first_index, uint32_t last_index, basist::uastc_block* pBlocks, const color_rgba* pBlock_pixels, const uastc_rdo_params& params, uint32_t flags,3817uint32_t &total_skipped, uint32_t &total_refined, uint32_t &total_modified, uint32_t &total_smooth)3818{3819debug_printf("uastc_rdo_blocks: Processing blocks %u to %u\n", first_index, last_index);38203821const int total_blocks_to_check = basisu::maximum<uint32_t>(1U, params.m_lz_dict_size / sizeof(basist::uastc_block));3822const bool perceptual = false;38233824std::unordered_map<selector_bitsequence, uint32_t, selector_bitsequence_hash> selector_history;38253826for (uint32_t block_index = first_index; block_index < last_index; block_index++)3827{3828const basist::uastc_block& blk = pBlocks[block_index];3829const color_rgba* pPixels = &pBlock_pixels[16 * block_index];38303831unpacked_uastc_block unpacked_blk;3832if (!unpack_uastc(blk, unpacked_blk, false, true))3833return false;38343835const uint32_t block_mode = unpacked_blk.m_mode;3836if (block_mode == UASTC_MODE_INDEX_SOLID_COLOR)3837continue;38383839tracked_stat r_stats, g_stats, b_stats, a_stats;38403841for (uint32_t i = 0; i < 16; i++)3842{3843r_stats.update(pPixels[i].r);3844g_stats.update(pPixels[i].g);3845b_stats.update(pPixels[i].b);3846a_stats.update(pPixels[i].a);3847}38483849const float max_std_dev = basisu::maximum<float>(basisu::maximum<float>(basisu::maximum(r_stats.get_std_dev(), g_stats.get_std_dev()), b_stats.get_std_dev()), a_stats.get_std_dev());38503851float yl = clamp<float>(max_std_dev / params.m_max_smooth_block_std_dev, 0.0f, 1.0f);3852yl = yl * yl;3853const float smooth_block_error_scale = lerp<float>(params.m_smooth_block_max_error_scale, 1.0f, yl);3854if (smooth_block_error_scale > 1.0f)3855total_smooth++;38563857color_rgba decoded_uastc_block[4][4];3858if (!unpack_uastc(unpacked_blk, (basist::color32*)decoded_uastc_block, false))3859return false;38603861uint64_t uastc_err = 0;3862for (uint32_t i = 0; i < 16; i++)3863uastc_err += color_distance(perceptual, pPixels[i], ((color_rgba*)decoded_uastc_block)[i], true);38643865// Transcode to BC73866bc7_optimization_results b7_results;3867if (!transcode_uastc_to_bc7(unpacked_blk, b7_results))3868return false;38693870basist::bc7_block b7_block;3871basist::encode_bc7_block(&b7_block, &b7_results);38723873color_rgba decoded_b7_blk[4][4];3874unpack_block(texture_format::cBC7, &b7_block, &decoded_b7_blk[0][0]);38753876uint64_t bc7_err = 0;3877for (uint32_t i = 0; i < 16; i++)3878bc7_err += color_distance(perceptual, pPixels[i], ((color_rgba*)decoded_b7_blk)[i], true);38793880uint64_t cur_err = (uastc_err + bc7_err) / 2;38813882// Divide by 16*4 to compute RMS error3883const float cur_ms_err = (float)cur_err * (1.0f / 64.0f);3884const float cur_rms_err = sqrt(cur_ms_err);38853886const uint32_t first_sel_bit = g_uastc_mode_selector_bits[block_mode][0];3887const uint32_t total_sel_bits = g_uastc_mode_selector_bits[block_mode][1];3888assert(first_sel_bit + total_sel_bits <= 128);3889assert(total_sel_bits > 0);38903891uint32_t cur_bit_offset = first_sel_bit;3892uint64_t cur_sel_bits = read_bits((const uint8_t*)&blk, cur_bit_offset, basisu::minimum(64U, total_sel_bits));38933894if (cur_rms_err >= params.m_skip_block_rms_thresh)3895{3896auto cur_search_res = selector_history.insert(std::make_pair(selector_bitsequence(first_sel_bit, cur_sel_bits), block_index));38973898// Block already has too much error, so don't mess with it.3899if (!cur_search_res.second)3900(*cur_search_res.first).second = block_index;39013902total_skipped++;3903continue;3904}39053906int cur_bits;3907auto cur_find_res = selector_history.find(selector_bitsequence(first_sel_bit, cur_sel_bits));3908if (cur_find_res == selector_history.end())3909{3910// Wasn't found - wildly estimate literal cost3911//cur_bits = (total_sel_bits * 5) / 4;3912cur_bits = (total_sel_bits * params.m_lz_literal_cost) / 100;3913}3914else3915{3916// Was found - wildly estimate match cost3917uint32_t match_block_index = cur_find_res->second;3918const int block_dist_in_bytes = (block_index - match_block_index) * 16;3919cur_bits = compute_match_cost_estimate(block_dist_in_bytes);3920}39213922int first_block_to_check = basisu::maximum<int>(first_index, block_index - total_blocks_to_check);3923int last_block_to_check = block_index - 1;39243925basist::uastc_block best_block(blk);3926uint32_t best_block_index = block_index;39273928float best_t = cur_ms_err * smooth_block_error_scale + cur_bits * params.m_lambda;39293930// Now scan through previous blocks, insert their selector bit patterns into the current block, and find3931// selector bit patterns which don't increase the overall block error too much.3932for (int prev_block_index = last_block_to_check; prev_block_index >= first_block_to_check; --prev_block_index)3933{3934const basist::uastc_block& prev_blk = pBlocks[prev_block_index];39353936uint32_t bit_offset = first_sel_bit;3937uint64_t sel_bits = read_bits((const uint8_t*)&prev_blk, bit_offset, basisu::minimum(64U, total_sel_bits));39383939int match_block_index = prev_block_index;3940auto res = selector_history.find(selector_bitsequence(first_sel_bit, sel_bits));3941if (res != selector_history.end())3942match_block_index = res->second;3943// Have we already checked this bit pattern? If so then skip this block.3944if (match_block_index > prev_block_index)3945continue;39463947unpacked_uastc_block unpacked_prev_blk;3948if (!unpack_uastc(prev_blk, unpacked_prev_blk, false, true))3949return false;39503951basist::uastc_block trial_blk(blk);39523953set_block_bits((uint8_t*)&trial_blk, sel_bits, basisu::minimum(64U, total_sel_bits), first_sel_bit);39543955if (total_sel_bits > 64)3956{3957sel_bits = read_bits((const uint8_t*)&prev_blk, bit_offset, total_sel_bits - 64U);39583959set_block_bits((uint8_t*)&trial_blk, sel_bits, total_sel_bits - 64U, first_sel_bit + basisu::minimum(64U, total_sel_bits));3960}39613962unpacked_uastc_block unpacked_trial_blk;3963if (!unpack_uastc(trial_blk, unpacked_trial_blk, false, true))3964continue;39653966color_rgba decoded_trial_uastc_block[4][4];3967if (!unpack_uastc(unpacked_trial_blk, (basist::color32*)decoded_trial_uastc_block, false))3968continue;39693970uint64_t trial_uastc_err = 0;3971for (uint32_t i = 0; i < 16; i++)3972trial_uastc_err += color_distance(perceptual, pPixels[i], ((color_rgba*)decoded_trial_uastc_block)[i], true);39733974// Transcode trial to BC7, compute error3975bc7_optimization_results trial_b7_results;3976if (!transcode_uastc_to_bc7(unpacked_trial_blk, trial_b7_results))3977return false;39783979basist::bc7_block trial_b7_block;3980basist::encode_bc7_block(&trial_b7_block, &trial_b7_results);39813982color_rgba decoded_trial_b7_blk[4][4];3983unpack_block(texture_format::cBC7, &trial_b7_block, &decoded_trial_b7_blk[0][0]);39843985uint64_t trial_bc7_err = 0;3986for (uint32_t i = 0; i < 16; i++)3987trial_bc7_err += color_distance(perceptual, pPixels[i], ((color_rgba*)decoded_trial_b7_blk)[i], true);39883989uint64_t trial_err = (trial_uastc_err + trial_bc7_err) / 2;39903991const float trial_ms_err = (float)trial_err * (1.0f / 64.0f);3992const float trial_rms_err = sqrtf(trial_ms_err);39933994if (trial_rms_err > cur_rms_err * params.m_max_allowed_rms_increase_ratio)3995continue;39963997const int block_dist_in_bytes = (block_index - match_block_index) * 16;3998const int match_bits = compute_match_cost_estimate(block_dist_in_bytes);39994000float t = trial_ms_err * smooth_block_error_scale + match_bits * params.m_lambda;4001if (t < best_t)4002{4003best_t = t;4004best_block_index = prev_block_index;40054006best_block = trial_blk;4007}40084009} // prev_block_index40104011if (best_block_index != block_index)4012{4013total_modified++;40144015unpacked_uastc_block unpacked_best_blk;4016if (!unpack_uastc(best_block, unpacked_best_blk, false, false))4017return false;40184019if ((params.m_endpoint_refinement) && (block_mode == 0))4020{4021// Attempt to refine mode 0 block's endpoints, using the new selectors. This doesn't help much, but it does help.4022// TODO: We could do this with the other modes too.4023color_rgba decoded_best_uastc_block[4][4];4024if (!unpack_uastc(unpacked_best_blk, (basist::color32*)decoded_best_uastc_block, false))4025return false;40264027// Compute the block's current error (with the modified selectors).4028uint64_t best_uastc_err = 0;4029for (uint32_t i = 0; i < 16; i++)4030best_uastc_err += color_distance(perceptual, pPixels[i], ((color_rgba*)decoded_best_uastc_block)[i], true);40314032bc7enc_compress_block_params comp_params;4033memset(&comp_params, 0, sizeof(comp_params));4034comp_params.m_max_partitions_mode1 = 64;4035comp_params.m_least_squares_passes = 1;4036comp_params.m_weights[0] = 1;4037comp_params.m_weights[1] = 1;4038comp_params.m_weights[2] = 1;4039comp_params.m_weights[3] = 1;4040comp_params.m_uber_level = 0;40414042uastc_encode_results results;4043uint32_t total_results = 0;4044astc_mode0_or_18(0, (color_rgba(*)[4])pPixels, &results, total_results, comp_params, unpacked_best_blk.m_astc.m_weights);4045assert(total_results == 1);40464047// See if the overall error has actually gone done.40484049color_rgba decoded_trial_uastc_block[4][4];4050bool success = unpack_uastc(results.m_uastc_mode, results.m_common_pattern, results.m_solid_color.get_color32(), results.m_astc, (basist::color32*) & decoded_trial_uastc_block[0][0], false);4051assert(success);40524053BASISU_NOTE_UNUSED(success);40544055uint64_t trial_uastc_err = 0;4056for (uint32_t i = 0; i < 16; i++)4057trial_uastc_err += color_distance(perceptual, pPixels[i], ((color_rgba*)decoded_trial_uastc_block)[i], true);40584059if (trial_uastc_err < best_uastc_err)4060{4061// The error went down, so accept the new endpoints.40624063// Ensure the selectors haven't changed, otherwise we'll invalidate the LZ matches.4064for (uint32_t i = 0; i < 16; i++)4065assert(unpacked_best_blk.m_astc.m_weights[i] == results.m_astc.m_weights[i]);40664067unpacked_best_blk.m_astc = results.m_astc;40684069total_refined++;4070}4071} // if ((params.m_endpoint_refinement) && (block_mode == 0))40724073// The selectors have changed, so go recompute the block hints.4074if (!uastc_recompute_hints(&best_block, pPixels, flags, &unpacked_best_blk))4075return false;40764077// Write the modified block4078pBlocks[block_index] = best_block;40794080} // if (best_block_index != block_index)40814082{4083uint32_t bit_offset = first_sel_bit;4084uint64_t sel_bits = read_bits((const uint8_t*)&best_block, bit_offset, basisu::minimum(64U, total_sel_bits));40854086auto res = selector_history.insert(std::make_pair(selector_bitsequence(first_sel_bit, sel_bits), block_index));4087if (!res.second)4088(*res.first).second = block_index;4089}40904091} // block_index40924093return true;4094}40954096// This function implements a basic form of rate distortion optimization (RDO) for UASTC.4097// It only changes selectors and then updates the hints. It uses very approximate LZ bitprice estimation.4098// There's A LOT that can be done better in here, but it's a start.4099// One nice advantage of the method used here is that it works for any input, no matter which or how many modes it uses.4100bool uastc_rdo(uint32_t num_blocks, basist::uastc_block* pBlocks, const color_rgba* pBlock_pixels, const uastc_rdo_params& params, uint32_t flags, job_pool* pJob_pool, uint32_t total_jobs)4101{4102assert(params.m_max_allowed_rms_increase_ratio > 1.0f);4103assert(params.m_lz_dict_size > 0);4104assert(params.m_lambda > 0.0f);41054106uint32_t total_skipped = 0, total_modified = 0, total_refined = 0, total_smooth = 0;41074108uint32_t blocks_per_job = total_jobs ? (num_blocks / total_jobs) : 0;41094110std::mutex stat_mutex;41114112bool status = false;41134114if ((!pJob_pool) || (total_jobs <= 1) || (blocks_per_job <= 8))4115{4116status = uastc_rdo_blocks(0, num_blocks, pBlocks, pBlock_pixels, params, flags, total_skipped, total_refined, total_modified, total_smooth);4117}4118else4119{4120bool all_succeeded = true;41214122for (uint32_t block_index_iter = 0; block_index_iter < num_blocks; block_index_iter += blocks_per_job)4123{4124const uint32_t first_index = block_index_iter;4125const uint32_t last_index = minimum<uint32_t>(num_blocks, block_index_iter + blocks_per_job);41264127pJob_pool->add_job([first_index, last_index, pBlocks, pBlock_pixels, ¶ms, flags, &total_skipped, &total_modified, &total_refined, &total_smooth, &all_succeeded, &stat_mutex] {41284129uint32_t job_skipped = 0, job_modified = 0, job_refined = 0, job_smooth = 0;41304131bool status = uastc_rdo_blocks(first_index, last_index, pBlocks, pBlock_pixels, params, flags, job_skipped, job_refined, job_modified, job_smooth);41324133{4134std::lock_guard<std::mutex> lck(stat_mutex);41354136all_succeeded = all_succeeded && status;4137total_skipped += job_skipped;4138total_modified += job_modified;4139total_refined += job_refined;4140total_smooth += job_smooth;4141}41424143}4144);41454146} // block_index_iter41474148pJob_pool->wait_for_all();41494150status = all_succeeded;4151}41524153debug_printf("uastc_rdo: Total modified: %3.2f%%, total skipped: %3.2f%%, total refined: %3.2f%%, total smooth: %3.2f%%\n", total_modified * 100.0f / num_blocks, total_skipped * 100.0f / num_blocks, total_refined * 100.0f / num_blocks, total_smooth * 100.0f / num_blocks);41544155return status;4156}4157} // namespace basisu41584159416041614162416341644165