Path: blob/master/thirdparty/basis_universal/transcoder/basisu_astc_helpers.h
21549 views
// basisu_astc_helpers.h1// Be sure to define ASTC_HELPERS_IMPLEMENTATION somewhere to get the implementation, otherwise you only get the header.2#ifndef BASISU_ASTC_HELPERS_HEADER3#define BASISU_ASTC_HELPERS_HEADER45#include <stdlib.h>6#include <stdint.h>7#include <math.h>8#include <fenv.h>910namespace astc_helpers11{12const uint32_t MAX_WEIGHT_VALUE = 64; // grid texel weights must range from [0,64]13const uint32_t MIN_GRID_DIM = 2; // the minimum dimension of a block's weight grid14const uint32_t MIN_BLOCK_DIM = 4, MAX_BLOCK_DIM = 12; // the valid block dimensions in texels15const uint32_t MAX_GRID_WEIGHTS = 64; // a block may have a maximum of 64 weight grid values16const uint32_t NUM_MODE11_ENDPOINTS = 6, NUM_MODE7_ENDPOINTS = 4;1718static const uint32_t NUM_ASTC_BLOCK_SIZES = 14;19extern const uint8_t g_astc_block_sizes[NUM_ASTC_BLOCK_SIZES][2];2021// The Color Endpoint Modes (CEM's)22enum cems23{24CEM_LDR_LUM_DIRECT = 0,25CEM_LDR_LUM_BASE_PLUS_OFS = 1,26CEM_HDR_LUM_LARGE_RANGE = 2,27CEM_HDR_LUM_SMALL_RANGE = 3,28CEM_LDR_LUM_ALPHA_DIRECT = 4,29CEM_LDR_LUM_ALPHA_BASE_PLUS_OFS = 5,30CEM_LDR_RGB_BASE_SCALE = 6,31CEM_HDR_RGB_BASE_SCALE = 7,32CEM_LDR_RGB_DIRECT = 8,33CEM_LDR_RGB_BASE_PLUS_OFFSET = 9,34CEM_LDR_RGB_BASE_SCALE_PLUS_TWO_A = 10,35CEM_HDR_RGB = 11,36CEM_LDR_RGBA_DIRECT = 12,37CEM_LDR_RGBA_BASE_PLUS_OFFSET = 13,38CEM_HDR_RGB_LDR_ALPHA = 14,39CEM_HDR_RGB_HDR_ALPHA = 1540};4142// All Bounded Integer Sequence Coding (BISE or ISE) ranges.43// Weights: Ranges [0,11] are valid.44// Endpoints: Ranges [4,20] are valid.45enum bise_levels46{47BISE_2_LEVELS = 0,48BISE_3_LEVELS = 1,49BISE_4_LEVELS = 2,50BISE_5_LEVELS = 3,51BISE_6_LEVELS = 4,52BISE_8_LEVELS = 5,53BISE_10_LEVELS = 6,54BISE_12_LEVELS = 7,55BISE_16_LEVELS = 8,56BISE_20_LEVELS = 9,57BISE_24_LEVELS = 10,58BISE_32_LEVELS = 11,59BISE_40_LEVELS = 12,60BISE_48_LEVELS = 13,61BISE_64_LEVELS = 14,62BISE_80_LEVELS = 15,63BISE_96_LEVELS = 16,64BISE_128_LEVELS = 17,65BISE_160_LEVELS = 18,66BISE_192_LEVELS = 19,67BISE_256_LEVELS = 2068};6970const uint32_t TOTAL_ISE_RANGES = 21;7172// Valid endpoint ISE ranges73const uint32_t FIRST_VALID_ENDPOINT_ISE_RANGE = BISE_6_LEVELS; // 474const uint32_t LAST_VALID_ENDPOINT_ISE_RANGE = BISE_256_LEVELS; // 2075const uint32_t TOTAL_ENDPOINT_ISE_RANGES = LAST_VALID_ENDPOINT_ISE_RANGE - FIRST_VALID_ENDPOINT_ISE_RANGE + 1;7677// Valid weight ISE ranges78const uint32_t FIRST_VALID_WEIGHT_ISE_RANGE = BISE_2_LEVELS; // 079const uint32_t LAST_VALID_WEIGHT_ISE_RANGE = BISE_32_LEVELS; // 1180const uint32_t TOTAL_WEIGHT_ISE_RANGES = LAST_VALID_WEIGHT_ISE_RANGE - FIRST_VALID_WEIGHT_ISE_RANGE + 1;8182// The ISE range table.83extern const int8_t g_ise_range_table[TOTAL_ISE_RANGES][3]; // 0=bits (0 to 8), 1=trits (0 or 1), 2=quints (0 or 1)8485// Possible Color Component Select values, used in dual plane mode.86// The CCS component will be interpolated using the 2nd weight plane.87enum ccs88{89CCS_GBA_R = 0,90CCS_RBA_G = 1,91CCS_RGA_B = 2,92CCS_RGB_A = 393};9495struct astc_block96{97uint32_t m_vals[4];98};99100const uint32_t MAX_PARTITIONS = 4; // Max # of partitions or subsets for single plane mode101const uint32_t MAX_DUAL_PLANE_PARTITIONS = 3; // Max # of partitions or subsets for dual plane mode102const uint32_t NUM_PARTITION_PATTERNS = 1024; // Total # of partition pattern seeds (10-bits)103const uint32_t MAX_ENDPOINTS = 18; // Maximum # of endpoint values in a block104105struct log_astc_block106{107bool m_error_flag;108109bool m_solid_color_flag_ldr, m_solid_color_flag_hdr;110111uint8_t m_user_mode; // user defined value, not used in this module112113// Rest is only valid if !m_solid_color_flag_ldr && !m_solid_color_flag_hdr114uint8_t m_grid_width, m_grid_height; // weight grid dimensions, not the dimension of the block115116bool m_dual_plane;117118uint8_t m_weight_ise_range; // 0-11119uint8_t m_endpoint_ise_range; // 4-20, this is actually inferred from the size of the other config bits+weights, but this is here for checking120121uint8_t m_color_component_selector; // 0-3, controls which channel uses the 2nd (odd) weights, only used in dual plane mode122123uint8_t m_num_partitions; // or the # of subsets, 1-4 (1-3 if dual plane mode)124uint16_t m_partition_id; // 10-bits, must be 0 if m_num_partitions==1125126uint8_t m_color_endpoint_modes[MAX_PARTITIONS]; // each subset's CEM's127128union129{130// ISE weight grid values. In dual plane mode, the order is p0,p1, p0,p1, etc.131uint8_t m_weights[MAX_GRID_WEIGHTS];132uint16_t m_solid_color[4];133};134135// ISE endpoint values136// Endpoint order examples:137// 1 subset LA : LL0 LH0 AL0 AH0138// 1 subset RGB : RL0 RH0 GL0 GH0 BL0 BH0139// 1 subset RGBA : RL0 RH0 GL0 GH0 BL0 BH0 AL0 AH0140// 2 subset LA : LL0 LH0 AL0 AH0 LL1 LH1 AL1 AH1141// 2 subset RGB : RL0 RH0 GL0 GH0 BL0 BH0 RL1 RH1 GL1 GH1 BL1 BH1142// 2 subset RGBA : RL0 RH0 GL0 GH0 BL0 BH0 AL0 AH0 RL1 RH1 GL1 GH1 BL1 BH1 AL1 AH1143uint8_t m_endpoints[MAX_ENDPOINTS];144145void clear()146{147memset(this, 0, sizeof(*this));148}149};150151// Open interval152inline int bounds_check(int v, int l, int h) { (void)v; (void)l; (void)h; assert(v >= l && v < h); return v; }153inline uint32_t bounds_check(uint32_t v, uint32_t l, uint32_t h) { (void)v; (void)l; (void)h; assert(v >= l && v < h); return v; }154155inline uint32_t get_bits(uint32_t val, int low, int high)156{157const int num_bits = (high - low) + 1;158assert((num_bits >= 1) && (num_bits <= 32));159160val >>= low;161if (num_bits != 32)162val &= ((1u << num_bits) - 1);163164return val;165}166167// Returns the number of levels in the given ISE range.168inline uint32_t get_ise_levels(uint32_t ise_range)169{170assert(ise_range < TOTAL_ISE_RANGES);171return (1 + 2 * g_ise_range_table[ise_range][1] + 4 * g_ise_range_table[ise_range][2]) << g_ise_range_table[ise_range][0];172}173174inline int get_ise_sequence_bits(int count, int range)175{176// See 18.22 Data Size Determination - note this will be <= the # of bits actually written by encode_bise(). (It's magic.)177int total_bits = g_ise_range_table[range][0] * count;178total_bits += (g_ise_range_table[range][1] * 8 * count + 4) / 5;179total_bits += (g_ise_range_table[range][2] * 7 * count + 2) / 3;180return total_bits;181}182183inline uint32_t weight_interpolate(uint32_t l, uint32_t h, uint32_t w)184{185assert(w <= MAX_WEIGHT_VALUE);186return (l * (64 - w) + h * w + 32) >> 6;187}188189void encode_bise(uint32_t* pDst, const uint8_t* pSrc_vals, uint32_t bit_pos, int num_vals, int range, uint32_t *pStats = nullptr);190191struct pack_stats192{193uint32_t m_header_bits;194uint32_t m_endpoint_bits;195uint32_t m_weight_bits;196197inline pack_stats() { clear(); }198inline void clear() { memset(this, 0, sizeof(*this)); }199};200201// Packs a logical to physical ASTC block. Note this does not validate the block's dimensions (use is_valid_block_size()), just the grid dimensions.202bool pack_astc_block(astc_block &phys_block, const log_astc_block& log_block, int* pExpected_endpoint_range = nullptr, pack_stats *pStats = nullptr);203204// Pack LDR void extent (really solid color) blocks. For LDR, pass in (val | (val << 8)) for each component.205void pack_void_extent_ldr(astc_block& blk, uint16_t r, uint16_t g, uint16_t b, uint16_t a, pack_stats *pStats = nullptr);206207// Pack HDR void extent (16-bit values are FP16/half floats - no NaN/Inf's)208void pack_void_extent_hdr(astc_block& blk, uint16_t rh, uint16_t gh, uint16_t bh, uint16_t ah, pack_stats* pStats = nullptr);209210// These helpers are all quite slow, but are useful for table preparation.211212// Dequantizes ISE encoded endpoint val to [0,255]213uint32_t dequant_bise_endpoint(uint32_t val, uint32_t ise_range); // ISE ranges 4-11214215// Dequantizes ISE encoded weight val to [0,64]216uint32_t dequant_bise_weight(uint32_t val, uint32_t ise_range); // ISE ranges 0-10217218uint32_t find_nearest_bise_endpoint(int v, uint32_t ise_range);219uint32_t find_nearest_bise_weight(int v, uint32_t ise_range);220221void create_quant_tables(222uint8_t* pVal_to_ise, // [0-255] or [0-64] value to nearest ISE symbol, array size is [256] or [65]223uint8_t* pISE_to_val, // ASTC encoded ISE symbol to [0,255] or [0,64] value, [levels]224uint8_t* pISE_to_rank, // returns the level rank index given an ISE symbol, [levels]225uint8_t* pRank_to_ISE, // returns the ISE symbol given a level rank, inverse of pISE_to_rank, [levels]226uint32_t ise_range, // ise range, [4,20] for endpoints, [0,11] for weights227bool weight_flag); // false if block endpoints, true if weights228229// True if the CEM is LDR.230bool is_cem_ldr(uint32_t mode);231inline bool is_cem_hdr(uint32_t mode) { return !is_cem_ldr(mode); }232233// True if the passed in dimensions are a valid ASTC block size. There are 14 supported configs, from 4x4 (8bpp) to 12x12 (.89bpp).234bool is_valid_block_size(uint32_t w, uint32_t h);235236bool block_has_any_hdr_cems(const log_astc_block& log_blk);237bool block_has_any_ldr_cems(const log_astc_block& log_blk);238239// Returns the # of endpoint values for the given CEM.240inline uint32_t get_num_cem_values(uint32_t cem) { assert(cem <= 15); return 2 + 2 * (cem >> 2); }241242struct dequant_table243{244basisu::vector<uint8_t> m_val_to_ise; // [0-255] or [0-64] value to nearest ISE symbol, array size is [256] or [65]245basisu::vector<uint8_t> m_ISE_to_val; // ASTC encoded ISE symbol to [0,255] or [0,64] value, [levels]246basisu::vector<uint8_t> m_ISE_to_rank; // returns the level rank index given an ISE symbol, [levels]247basisu::vector<uint8_t> m_rank_to_ISE; // returns the ISE symbol given a level rank, inverse of pISE_to_rank, [levels]248249void init(bool weight_flag, uint32_t num_levels, bool init_rank_tabs)250{251m_val_to_ise.resize(weight_flag ? (MAX_WEIGHT_VALUE + 1) : 256);252m_ISE_to_val.resize(num_levels);253if (init_rank_tabs)254{255m_ISE_to_rank.resize(num_levels);256m_rank_to_ISE.resize(num_levels);257}258}259};260261struct dequant_tables262{263dequant_table m_weights[TOTAL_WEIGHT_ISE_RANGES];264dequant_table m_endpoints[TOTAL_ENDPOINT_ISE_RANGES];265266const dequant_table& get_weight_tab(uint32_t range) const267{268assert((range >= FIRST_VALID_WEIGHT_ISE_RANGE) && (range <= LAST_VALID_WEIGHT_ISE_RANGE));269return m_weights[range - FIRST_VALID_WEIGHT_ISE_RANGE];270}271272dequant_table& get_weight_tab(uint32_t range)273{274assert((range >= FIRST_VALID_WEIGHT_ISE_RANGE) && (range <= LAST_VALID_WEIGHT_ISE_RANGE));275return m_weights[range - FIRST_VALID_WEIGHT_ISE_RANGE];276}277278const dequant_table& get_endpoint_tab(uint32_t range) const279{280assert((range >= FIRST_VALID_ENDPOINT_ISE_RANGE) && (range <= LAST_VALID_ENDPOINT_ISE_RANGE));281return m_endpoints[range - FIRST_VALID_ENDPOINT_ISE_RANGE];282}283284dequant_table& get_endpoint_tab(uint32_t range)285{286assert((range >= FIRST_VALID_ENDPOINT_ISE_RANGE) && (range <= LAST_VALID_ENDPOINT_ISE_RANGE));287return m_endpoints[range - FIRST_VALID_ENDPOINT_ISE_RANGE];288}289290void init(bool init_rank_tabs)291{292for (uint32_t range = FIRST_VALID_WEIGHT_ISE_RANGE; range <= LAST_VALID_WEIGHT_ISE_RANGE; range++)293{294const uint32_t num_levels = get_ise_levels(range);295dequant_table& tab = get_weight_tab(range);296297tab.init(true, num_levels, init_rank_tabs);298299create_quant_tables(tab.m_val_to_ise.data(), tab.m_ISE_to_val.data(), init_rank_tabs ? tab.m_ISE_to_rank.data() : nullptr, init_rank_tabs ? tab.m_rank_to_ISE.data() : nullptr, range, true);300}301302for (uint32_t range = FIRST_VALID_ENDPOINT_ISE_RANGE; range <= LAST_VALID_ENDPOINT_ISE_RANGE; range++)303{304const uint32_t num_levels = get_ise_levels(range);305dequant_table& tab = get_endpoint_tab(range);306307tab.init(false, num_levels, init_rank_tabs);308309create_quant_tables(tab.m_val_to_ise.data(), tab.m_ISE_to_val.data(), init_rank_tabs ? tab.m_ISE_to_rank.data() : nullptr, init_rank_tabs ? tab.m_rank_to_ISE.data() : nullptr, range, false);310}311}312};313314extern dequant_tables g_dequant_tables;315void init_tables(bool init_rank_tabs);316317struct weighted_sample318{319uint8_t m_src_x;320uint8_t m_src_y;321uint8_t m_weights[2][2]; // [y][x], scaled by 16, round by adding 8322};323324void compute_upsample_weights(325int block_width, int block_height,326int weight_grid_width, int weight_grid_height,327weighted_sample* pWeights); // there will be block_width * block_height bilinear samples328329void upsample_weight_grid(330uint32_t bx, uint32_t by, // destination/to dimension331uint32_t wx, uint32_t wy, // source/from dimension332const uint8_t* pSrc_weights, // these are dequantized [0,64] weights, NOT ISE symbols, [wy][wx]333uint8_t* pDst_weights); // [by][bx]334335// Procedurally returns the texel partition/subset index given the block coordinate and config.336int compute_texel_partition(uint32_t seedIn, uint32_t xIn, uint32_t yIn, uint32_t zIn, int num_partitions, bool small_block);337338void blue_contract(339int r, int g, int b, int a,340int& dr, int& dg, int& db, int& da);341342void bit_transfer_signed(int& a, int& b);343344void decode_endpoint(uint32_t cem_index, int (*pEndpoints)[2], const uint8_t* pE);345346typedef uint16_t half_float;347half_float float_to_half(float val, bool toward_zero);348float half_to_float(half_float hval);349350// Notes:351// qlog16_to_half(half_to_qlog16(half_val_as_int)) == half_val_as_int (is lossless)352// However, this is not lossless in the general sense.353inline half_float qlog16_to_half(int k)354{355assert((k >= 0) && (k <= 0xFFFF));356357int E = (k & 0xF800) >> 11;358int M = k & 0x7FF;359360int Mt;361if (M < 512)362Mt = 3 * M;363else if (M >= 1536)364Mt = 5 * M - 2048;365else366Mt = 4 * M - 512;367368return (half_float)((E << 10) + (Mt >> 3));369}370371const int MAX_RGB9E5 = 0xff80;372void unpack_rgb9e5(uint32_t packed, float& r, float& g, float& b);373uint32_t pack_rgb9e5(float r, float g, float b);374375enum decode_mode376{377cDecodeModeSRGB8 = 0, // returns uint8_t's, not valid on HDR blocks378cDecodeModeLDR8 = 1, // returns uint8_t's, not valid on HDR blocks379cDecodeModeHDR16 = 2, // returns uint16_t's (half floats), valid on all LDR/HDR blocks380cDecodeModeRGB9E5 = 3 // returns uint32_t's, packed as RGB 9E5 (shared exponent), see https://registry.khronos.org/OpenGL/extensions/EXT/EXT_texture_shared_exponent.txt381};382383// Decodes logical block to output pixels.384// pPixels must point to either 32-bit pixel values (SRGB8/LDR8/9E5) or 64-bit pixel values (HDR16)385bool decode_block(const log_astc_block& log_blk, void* pPixels, uint32_t blk_width, uint32_t blk_height, decode_mode dec_mode);386387void decode_bise(uint32_t ise_range, uint8_t* pVals, uint32_t num_vals, const uint8_t *pBits128, uint32_t bit_ofs);388389// Unpack a physical ASTC encoded GPU texture block to a logical block description.390bool unpack_block(const void* pASTC_block, log_astc_block& log_blk, uint32_t blk_width, uint32_t blk_height);391392} // namespace astc_helpers393394#endif // BASISU_ASTC_HELPERS_HEADER395396//------------------------------------------------------------------397398#ifdef BASISU_ASTC_HELPERS_IMPLEMENTATION399400namespace astc_helpers401{402template<typename T> inline T my_min(T a, T b) { return (a < b) ? a : b; }403template<typename T> inline T my_max(T a, T b) { return (a > b) ? a : b; }404405const uint8_t g_astc_block_sizes[NUM_ASTC_BLOCK_SIZES][2] = {406{ 4, 4 }, { 5, 4 }, { 5, 5 }, { 6, 5 },407{ 6, 6 }, { 8, 5 }, { 8, 6 }, { 10, 5 },408{ 10, 6 }, { 8, 8 }, { 10, 8 }, { 10, 10 },409{ 12, 10 }, { 12, 12 }410};411412const int8_t g_ise_range_table[TOTAL_ISE_RANGES][3] =413{414//b t q415//2 3 5 // rng ise_index notes416{ 1, 0, 0 }, // 0..1 0417{ 0, 1, 0 }, // 0..2 1418{ 2, 0, 0 }, // 0..3 2419{ 0, 0, 1 }, // 0..4 3420{ 1, 1, 0 }, // 0..5 4 min endpoint ISE index421{ 3, 0, 0 }, // 0..7 5422{ 1, 0, 1 }, // 0..9 6423{ 2, 1, 0 }, // 0..11 7424{ 4, 0, 0 }, // 0..15 8425{ 2, 0, 1 }, // 0..19 9426{ 3, 1, 0 }, // 0..23 10427{ 5, 0, 0 }, // 0..31 11 max weight ISE index428{ 3, 0, 1 }, // 0..39 12429{ 4, 1, 0 }, // 0..47 13430{ 6, 0, 0 }, // 0..63 14431{ 4, 0, 1 }, // 0..79 15432{ 5, 1, 0 }, // 0..95 16433{ 7, 0, 0 }, // 0..127 17434{ 5, 0, 1 }, // 0..159 18435{ 6, 1, 0 }, // 0..191 19436{ 8, 0, 0 }, // 0..255 20437};438439static inline void astc_set_bits_1_to_9(uint32_t* pDst, uint32_t& bit_offset, uint32_t code, uint32_t codesize)440{441uint8_t* pBuf = reinterpret_cast<uint8_t*>(pDst);442443assert(codesize <= 9);444if (codesize)445{446uint32_t byte_bit_offset = bit_offset & 7;447uint32_t val = code << byte_bit_offset;448449uint32_t index = bit_offset >> 3;450pBuf[index] |= (uint8_t)val;451452if (codesize > (8 - byte_bit_offset))453pBuf[index + 1] |= (uint8_t)(val >> 8);454455bit_offset += codesize;456}457}458459static inline uint32_t astc_extract_bits(uint32_t bits, int low, int high)460{461return (bits >> low) & ((1 << (high - low + 1)) - 1);462}463464// Writes bits to output in an endian safe way465static inline void astc_set_bits(uint32_t* pOutput, uint32_t& bit_pos, uint32_t value, uint32_t total_bits)466{467assert(total_bits <= 31);468assert(value < (1u << total_bits));469470uint8_t* pBytes = reinterpret_cast<uint8_t*>(pOutput);471472while (total_bits)473{474const uint32_t bits_to_write = my_min<int>(total_bits, 8 - (bit_pos & 7));475476pBytes[bit_pos >> 3] |= static_cast<uint8_t>(value << (bit_pos & 7));477478bit_pos += bits_to_write;479total_bits -= bits_to_write;480value >>= bits_to_write;481}482}483484static const uint8_t g_astc_quint_encode[125] =485{4860, 1, 2, 3, 4, 8, 9, 10, 11, 12, 16, 17, 18, 19, 20, 24, 25, 26, 27, 28, 5, 13, 21, 29, 6, 32, 33, 34, 35, 36, 40, 41, 42, 43, 44, 48, 49, 50, 51, 52, 56, 57,48758, 59, 60, 37, 45, 53, 61, 14, 64, 65, 66, 67, 68, 72, 73, 74, 75, 76, 80, 81, 82, 83, 84, 88, 89, 90, 91, 92, 69, 77, 85, 93, 22, 96, 97, 98, 99, 100, 104,488105, 106, 107, 108, 112, 113, 114, 115, 116, 120, 121, 122, 123, 124, 101, 109, 117, 125, 30, 102, 103, 70, 71, 38, 110, 111, 78, 79, 46, 118, 119, 86, 87, 54,489126, 127, 94, 95, 62, 39, 47, 55, 63, 7 /*31 - results in the same decode as 7*/490};491492// Encodes 3 values to output, usable for any range that uses quints and bits493static inline void astc_encode_quints(uint32_t* pOutput, const uint8_t* pValues, uint32_t& bit_pos, int n, uint32_t* pStats)494{495// First extract the quints and the bits from the 3 input values496int quints = 0, bits[3];497const uint32_t bit_mask = (1 << n) - 1;498for (int i = 0; i < 3; i++)499{500static const int s_muls[3] = { 1, 5, 25 };501502const int t = pValues[i] >> n;503504quints += t * s_muls[i];505bits[i] = pValues[i] & bit_mask;506}507508// Encode the quints, by inverting the bit manipulations done by the decoder, converting 3 quints into 7-bits.509// See https://www.khronos.org/registry/DataFormat/specs/1.2/dataformat.1.2.html#astc-integer-sequence-encoding510511assert(quints < 125);512const int T = g_astc_quint_encode[quints];513514// Now interleave the 7 encoded quint bits with the bits to form the encoded output. See table 95-96.515astc_set_bits(pOutput, bit_pos, bits[0] | (astc_extract_bits(T, 0, 2) << n) | (bits[1] << (3 + n)) | (astc_extract_bits(T, 3, 4) << (3 + n * 2)) |516(bits[2] << (5 + n * 2)) | (astc_extract_bits(T, 5, 6) << (5 + n * 3)), 7 + n * 3);517518if (pStats)519*pStats += n * 3 + 7;520}521522static const uint8_t g_astc_trit_encode[243] = { 0, 1, 2, 4, 5, 6, 8, 9, 10, 16, 17, 18, 20, 21, 22, 24, 25, 26, 3, 7, 11, 19, 23, 27, 12, 13, 14, 32, 33, 34, 36, 37, 38, 40, 41, 42, 48, 49, 50, 52, 53, 54, 56, 57, 58, 35, 39,52343, 51, 55, 59, 44, 45, 46, 64, 65, 66, 68, 69, 70, 72, 73, 74, 80, 81, 82, 84, 85, 86, 88, 89, 90, 67, 71, 75, 83, 87, 91, 76, 77, 78, 128, 129, 130, 132, 133, 134, 136, 137, 138, 144, 145, 146, 148, 149, 150, 152, 153, 154,524131, 135, 139, 147, 151, 155, 140, 141, 142, 160, 161, 162, 164, 165, 166, 168, 169, 170, 176, 177, 178, 180, 181, 182, 184, 185, 186, 163, 167, 171, 179, 183, 187, 172, 173, 174, 192, 193, 194, 196, 197, 198, 200, 201, 202,525208, 209, 210, 212, 213, 214, 216, 217, 218, 195, 199, 203, 211, 215, 219, 204, 205, 206, 96, 97, 98, 100, 101, 102, 104, 105, 106, 112, 113, 114, 116, 117, 118, 120, 121, 122, 99, 103, 107, 115, 119, 123, 108, 109, 110, 224,526225, 226, 228, 229, 230, 232, 233, 234, 240, 241, 242, 244, 245, 246, 248, 249, 250, 227, 231, 235, 243, 247, 251, 236, 237, 238, 28, 29, 30, 60, 61, 62, 92, 93, 94, 156, 157, 158, 188, 189, 190, 220, 221, 222, 31, 63, 95, 159,527191, 223, 124, 125, 126 };528529// Encodes 5 values to output, usable for any range that uses trits and bits530static void astc_encode_trits(uint32_t* pOutput, const uint8_t* pValues, uint32_t& bit_pos, int n, uint32_t *pStats)531{532// First extract the trits and the bits from the 5 input values533int trits = 0, bits[5];534const uint32_t bit_mask = (1 << n) - 1;535for (int i = 0; i < 5; i++)536{537static const int s_muls[5] = { 1, 3, 9, 27, 81 };538539const int t = pValues[i] >> n;540541trits += t * s_muls[i];542bits[i] = pValues[i] & bit_mask;543}544545// Encode the trits, by inverting the bit manipulations done by the decoder, converting 5 trits into 8-bits.546// See https://www.khronos.org/registry/DataFormat/specs/1.2/dataformat.1.2.html#astc-integer-sequence-encoding547548assert(trits < 243);549const int T = g_astc_trit_encode[trits];550551// Now interleave the 8 encoded trit bits with the bits to form the encoded output. See table 94.552astc_set_bits(pOutput, bit_pos, bits[0] | (astc_extract_bits(T, 0, 1) << n) | (bits[1] << (2 + n)), n * 2 + 2);553554astc_set_bits(pOutput, bit_pos, astc_extract_bits(T, 2, 3) | (bits[2] << 2) | (astc_extract_bits(T, 4, 4) << (2 + n)) | (bits[3] << (3 + n)) | (astc_extract_bits(T, 5, 6) << (3 + n * 2)) |555(bits[4] << (5 + n * 2)) | (astc_extract_bits(T, 7, 7) << (5 + n * 3)), n * 3 + 6);556557if (pStats)558*pStats += n * 5 + 8;559}560561// Packs values using ASTC's BISE to output buffer.562void encode_bise(uint32_t* pDst, const uint8_t* pSrc_vals, uint32_t bit_pos, int num_vals, int range, uint32_t *pStats)563{564uint32_t temp[5] = { 0 };565566const int num_bits = g_ise_range_table[range][0];567568int group_size = 0;569if (g_ise_range_table[range][1])570group_size = 5;571else if (g_ise_range_table[range][2])572group_size = 3;573574#ifndef NDEBUG575const uint32_t num_levels = get_ise_levels(range);576for (int i = 0; i < num_vals; i++)577{578assert(pSrc_vals[i] < num_levels);579}580#endif581582if (group_size)583{584// Range has trits or quints - pack each group of 5 or 3 values585const int total_groups = (group_size == 5) ? ((num_vals + 4) / 5) : ((num_vals + 2) / 3);586587for (int group_index = 0; group_index < total_groups; group_index++)588{589uint8_t vals[5] = { 0 };590591const int limit = my_min(group_size, num_vals - group_index * group_size);592for (int i = 0; i < limit; i++)593vals[i] = pSrc_vals[group_index * group_size + i];594595// Note this always writes a group of 3 or 5 bits values, even for incomplete groups. So it can write more than needed.596// get_ise_sequence_bits() returns the # of bits that must be written for proper decoding.597if (group_size == 5)598astc_encode_trits(temp, vals, bit_pos, num_bits, pStats);599else600astc_encode_quints(temp, vals, bit_pos, num_bits, pStats);601}602}603else604{605for (int i = 0; i < num_vals; i++)606astc_set_bits_1_to_9(temp, bit_pos, pSrc_vals[i], num_bits);607608if (pStats)609*pStats += num_vals * num_bits;610}611612pDst[0] |= temp[0]; pDst[1] |= temp[1];613pDst[2] |= temp[2]; pDst[3] |= temp[3];614}615616inline uint32_t rev_dword(uint32_t bits)617{618uint32_t v = (bits << 16) | (bits >> 16);619v = ((v & 0x00ff00ff) << 8) | ((v & 0xff00ff00) >> 8); v = ((v & 0x0f0f0f0f) << 4) | ((v & 0xf0f0f0f0) >> 4);620v = ((v & 0x33333333) << 2) | ((v & 0xcccccccc) >> 2); v = ((v & 0x55555555) << 1) | ((v & 0xaaaaaaaa) >> 1);621return v;622}623624static inline bool is_packable(int value, int num_bits) { assert((num_bits >= 1) && (num_bits < 31)); return (value >= 0) && (value < (1 << num_bits)); }625626static bool get_config_bits(const log_astc_block &log_block, uint32_t &config_bits)627{628config_bits = 0;629630const int W = log_block.m_grid_width, H = log_block.m_grid_height;631632const uint32_t P = log_block.m_weight_ise_range >= 6; // high precision633const uint32_t Dp_P = (log_block.m_dual_plane << 1) | P; // pack dual plane+high precision bits634635// See Tables 81-82636// Compute p from weight range637uint32_t p = 2 + log_block.m_weight_ise_range - (P ? 6 : 0);638639// Rearrange p's bits to p0 p2 p1640p = (p >> 1) + ((p & 1) << 2);641642// Try encoding each row of table 82.643644// W+4 H+2645if (is_packable(W - 4, 2) && is_packable(H - 2, 2))646{647config_bits = (Dp_P << 9) | ((W - 4) << 7) | ((H - 2) << 5) | ((p & 4) << 2) | (p & 3);648return true;649}650651// W+8 H+2652if (is_packable(W - 8, 2) && is_packable(H - 2, 2))653{654config_bits = (Dp_P << 9) | ((W - 8) << 7) | ((H - 2) << 5) | ((p & 4) << 2) | 4 | (p & 3);655return true;656}657658// W+2 H+8659if (is_packable(W - 2, 2) && is_packable(H - 8, 2))660{661config_bits = (Dp_P << 9) | ((H - 8) << 7) | ((W - 2) << 5) | ((p & 4) << 2) | 8 | (p & 3);662return true;663}664665// W+2 H+6666if (is_packable(W - 2, 2) && is_packable(H - 6, 1))667{668config_bits = (Dp_P << 9) | ((H - 6) << 7) | ((W - 2) << 5) | ((p & 4) << 2) | 12 | (p & 3);669return true;670}671672// W+2 H+2673if (is_packable(W - 2, 1) && is_packable(H - 2, 2))674{675config_bits = (Dp_P << 9) | ((W) << 7) | ((H - 2) << 5) | ((p & 4) << 2) | 12 | (p & 3);676return true;677}678679// 12 H+2680if ((W == 12) && is_packable(H - 2, 2))681{682config_bits = (Dp_P << 9) | ((H - 2) << 5) | (p << 2);683return true;684}685686// W+2 12687if ((H == 12) && is_packable(W - 2, 2))688{689config_bits = (Dp_P << 9) | (1 << 7) | ((W - 2) << 5) | (p << 2);690return true;691}692693// 6 10694if ((W == 6) && (H == 10))695{696config_bits = (Dp_P << 9) | (3 << 7) | (p << 2);697return true;698}699700// 10 6701if ((W == 10) && (H == 6))702{703config_bits = (Dp_P << 9) | (0b1101 << 5) | (p << 2);704return true;705}706707// W+6 H+6 (no dual plane or high prec)708if ((!Dp_P) && is_packable(W - 6, 2) && is_packable(H - 6, 2))709{710config_bits = ((H - 6) << 9) | 256 | ((W - 6) << 5) | (p << 2);711return true;712}713714// Failed: unsupported weight grid dimensions or config.715return false;716}717718bool pack_astc_block(astc_block& phys_block, const log_astc_block& log_block, int* pExpected_endpoint_range, pack_stats *pStats)719{720memset(&phys_block, 0, sizeof(phys_block));721722if (pExpected_endpoint_range)723*pExpected_endpoint_range = -1;724725assert(!log_block.m_error_flag);726if (log_block.m_error_flag)727return false;728729if (log_block.m_solid_color_flag_ldr)730{731pack_void_extent_ldr(phys_block, log_block.m_solid_color[0], log_block.m_solid_color[1], log_block.m_solid_color[2], log_block.m_solid_color[3], pStats);732return true;733}734else if (log_block.m_solid_color_flag_hdr)735{736pack_void_extent_hdr(phys_block, log_block.m_solid_color[0], log_block.m_solid_color[1], log_block.m_solid_color[2], log_block.m_solid_color[3], pStats);737return true;738}739740if ((log_block.m_num_partitions < 1) || (log_block.m_num_partitions > MAX_PARTITIONS))741return false;742743// Max usable weight range is 11744if (log_block.m_weight_ise_range > LAST_VALID_WEIGHT_ISE_RANGE)745return false;746747// See 23.24 Illegal Encodings, [0,5] is the minimum ISE encoding for endpoints748if ((log_block.m_endpoint_ise_range < FIRST_VALID_ENDPOINT_ISE_RANGE) || (log_block.m_endpoint_ise_range > LAST_VALID_ENDPOINT_ISE_RANGE))749return false;750751if (log_block.m_color_component_selector > 3)752return false;753754// TODO: sanity check grid width/height vs. block's physical width/height755756uint32_t config_bits = 0;757if (!get_config_bits(log_block, config_bits))758return false;759760uint32_t bit_pos = 0;761astc_set_bits(&phys_block.m_vals[0], bit_pos, config_bits, 11);762if (pStats)763pStats->m_header_bits += 11;764765const uint32_t total_grid_weights = (log_block.m_dual_plane ? 2 : 1) * (log_block.m_grid_width * log_block.m_grid_height);766const uint32_t total_weight_bits = get_ise_sequence_bits(total_grid_weights, log_block.m_weight_ise_range);767768// 18.24 Illegal Encodings769if ((!total_grid_weights) || (total_grid_weights > MAX_GRID_WEIGHTS) || (total_weight_bits < 24) || (total_weight_bits > 96))770return false;771772uint32_t total_extra_bits = 0;773774astc_set_bits(&phys_block.m_vals[0], bit_pos, log_block.m_num_partitions - 1, 2);775if (pStats)776pStats->m_header_bits += 2;777778if (log_block.m_num_partitions > 1)779{780if (log_block.m_partition_id >= NUM_PARTITION_PATTERNS)781return false;782783astc_set_bits(&phys_block.m_vals[0], bit_pos, log_block.m_partition_id, 10);784if (pStats)785pStats->m_header_bits += 10;786787uint32_t highest_cem = 0, lowest_cem = UINT32_MAX;788for (uint32_t j = 0; j < log_block.m_num_partitions; j++)789{790highest_cem = my_max<uint32_t>(highest_cem, log_block.m_color_endpoint_modes[j]);791lowest_cem = my_min<uint32_t>(lowest_cem, log_block.m_color_endpoint_modes[j]);792}793794if (highest_cem > 15)795return false;796797// Ensure CEM range is contiguous798if (((highest_cem >> 2) > (1 + (lowest_cem >> 2))))799return false;800801// See tables 79/80802uint32_t encoded_cem = log_block.m_color_endpoint_modes[0] << 2;803if (lowest_cem != highest_cem)804{805encoded_cem = my_min<uint32_t>(3, 1 + (lowest_cem >> 2));806807// See tables at 23.11 Color Endpoint Mode808for (uint32_t j = 0; j < log_block.m_num_partitions; j++)809{810const int M = log_block.m_color_endpoint_modes[j] & 3;811812const int C = (log_block.m_color_endpoint_modes[j] >> 2) - ((encoded_cem & 3) - 1);813if ((C & 1) != C)814return false;815816encoded_cem |= (C << (2 + j)) | (M << (2 + log_block.m_num_partitions + 2 * j));817}818819total_extra_bits = 3 * log_block.m_num_partitions - 4;820821if ((total_weight_bits + total_extra_bits) > 128)822return false;823824uint32_t cem_bit_pos = 128 - total_weight_bits - total_extra_bits;825astc_set_bits(&phys_block.m_vals[0], cem_bit_pos, encoded_cem >> 6, total_extra_bits);826if (pStats)827pStats->m_header_bits += total_extra_bits;828}829830astc_set_bits(&phys_block.m_vals[0], bit_pos, encoded_cem & 0x3f, 6);831if (pStats)832pStats->m_header_bits += 6;833}834else835{836if (log_block.m_partition_id)837return false;838if (log_block.m_color_endpoint_modes[0] > 15)839return false;840841astc_set_bits(&phys_block.m_vals[0], bit_pos, log_block.m_color_endpoint_modes[0], 4);842if (pStats)843pStats->m_header_bits += 4;844}845846if (log_block.m_dual_plane)847{848if (log_block.m_num_partitions > 3)849return false;850851total_extra_bits += 2;852853uint32_t ccs_bit_pos = 128 - (int)total_weight_bits - (int)total_extra_bits;854astc_set_bits(&phys_block.m_vals[0], ccs_bit_pos, log_block.m_color_component_selector, 2);855if (pStats)856pStats->m_header_bits += 2;857}858859const uint32_t total_config_bits = bit_pos + total_extra_bits;860const int num_remaining_bits = 128 - (int)total_config_bits - (int)total_weight_bits;861if (num_remaining_bits < 0)862return false;863864uint32_t total_cem_vals = 0;865for (uint32_t j = 0; j < log_block.m_num_partitions; j++)866total_cem_vals += 2 + 2 * (log_block.m_color_endpoint_modes[j] >> 2);867868if (total_cem_vals > MAX_ENDPOINTS)869return false;870871int endpoint_ise_range = -1;872for (int k = 20; k > 0; k--)873{874int bits = get_ise_sequence_bits(total_cem_vals, k);875if (bits <= num_remaining_bits)876{877endpoint_ise_range = k;878break;879}880}881882// See 23.24 Illegal Encodings, [0,5] is the minimum ISE encoding for endpoints883if (endpoint_ise_range < (int)FIRST_VALID_ENDPOINT_ISE_RANGE)884return false;885886// Ensure the caller utilized the right endpoint ISE range.887if ((int)log_block.m_endpoint_ise_range != endpoint_ise_range)888{889if (pExpected_endpoint_range)890*pExpected_endpoint_range = endpoint_ise_range;891return false;892}893894if (pStats)895{896pStats->m_endpoint_bits += get_ise_sequence_bits(total_cem_vals, endpoint_ise_range);897pStats->m_weight_bits += get_ise_sequence_bits(total_grid_weights, log_block.m_weight_ise_range);898}899900// Pack endpoints forwards901encode_bise(&phys_block.m_vals[0], log_block.m_endpoints, bit_pos, total_cem_vals, endpoint_ise_range);902903// Pack weights backwards904uint32_t weight_data[4] = { 0 };905encode_bise(weight_data, log_block.m_weights, 0, total_grid_weights, log_block.m_weight_ise_range);906907for (uint32_t i = 0; i < 4; i++)908phys_block.m_vals[i] |= rev_dword(weight_data[3 - i]);909910return true;911}912913static inline uint32_t bit_replication_scale(uint32_t src, int num_src_bits, int num_dst_bits)914{915assert(num_src_bits <= num_dst_bits);916assert((src & ((1 << num_src_bits) - 1)) == src);917918uint32_t dst = 0;919for (int shift = num_dst_bits - num_src_bits; shift > -num_src_bits; shift -= num_src_bits)920dst |= (shift >= 0) ? (src << shift) : (src >> -shift);921922return dst;923}924925uint32_t dequant_bise_endpoint(uint32_t val, uint32_t ise_range)926{927assert((ise_range >= FIRST_VALID_ENDPOINT_ISE_RANGE) && (ise_range <= LAST_VALID_ENDPOINT_ISE_RANGE));928assert(val < get_ise_levels(ise_range));929930uint32_t u = 0;931932switch (ise_range)933{934case 5:935{936u = bit_replication_scale(val, 3, 8);937break;938}939case 8:940{941u = bit_replication_scale(val, 4, 8);942break;943}944case 11:945{946u = bit_replication_scale(val, 5, 8);947break;948}949case 14:950{951u = bit_replication_scale(val, 6, 8);952break;953}954case 17:955{956u = bit_replication_scale(val, 7, 8);957break;958}959case 20:960{961u = val;962break;963}964case 4:965case 6:966case 7:967case 9:968case 10:969case 12:970case 13:971case 15:972case 16:973case 18:974case 19:975{976const uint32_t num_bits = g_ise_range_table[ise_range][0];977const uint32_t num_trits = g_ise_range_table[ise_range][1]; BASISU_NOTE_UNUSED(num_trits);978const uint32_t num_quints = g_ise_range_table[ise_range][2]; BASISU_NOTE_UNUSED(num_quints);979980// compute Table 103 row index981const int range_index = (num_bits * 2 + (num_quints ? 1 : 0)) - 2;982983assert(range_index >= 0 && range_index <= 10);984985uint32_t bits = val & ((1 << num_bits) - 1);986uint32_t tval = val >> num_bits;987988assert(tval < (num_trits ? 3U : 5U));989990uint32_t a = bits & 1;991uint32_t b = (bits >> 1) & 1;992uint32_t c = (bits >> 2) & 1;993uint32_t d = (bits >> 3) & 1;994uint32_t e = (bits >> 4) & 1;995uint32_t f = (bits >> 5) & 1;996997uint32_t A = a ? 511 : 0;998uint32_t B = 0;9991000switch (range_index)1001{1002case 2:1003{1004// 8765432101005// b000b0bb01006B = (b << 1) | (b << 2) | (b << 4) | (b << 8);1007break;1008}1009case 3:1010{1011// 8765432101012// b0000bb001013B = (b << 2) | (b << 3) | (b << 8);1014break;1015}1016case 4:1017{1018// 8765432101019// cb000cbcb1020B = b | (c << 1) | (b << 2) | (c << 3) | (b << 7) | (c << 8);1021break;1022}1023case 5:1024{1025// 8765432101026// cb0000cbc1027B = c | (b << 1) | (c << 2) | (b << 7) | (c << 8);1028break;1029}1030case 6:1031{1032// 8765432101033// dcb000dcb1034B = b | (c << 1) | (d << 2) | (b << 6) | (c << 7) | (d << 8);1035break;1036}1037case 7:1038{1039// 8765432101040// dcb0000dc1041B = c | (d << 1) | (b << 6) | (c << 7) | (d << 8);1042break;1043}1044case 8:1045{1046// 8765432101047// edcb000ed1048B = d | (e << 1) | (b << 5) | (c << 6) | (d << 7) | (e << 8);1049break;1050}1051case 9:1052{1053// 8765432101054// edcb0000e1055B = e | (b << 5) | (c << 6) | (d << 7) | (e << 8);1056break;1057}1058case 10:1059{1060// 8765432101061// fedcb000f1062B = f | (b << 4) | (c << 5) | (d << 6) | (e << 7) | (f << 8);1063break;1064}1065default:1066break;1067}10681069static uint8_t C_vals[11] = { 204, 113, 93, 54, 44, 26, 22, 13, 11, 6, 5 };1070uint32_t C = C_vals[range_index];1071uint32_t D = tval;10721073u = D * C + B;1074u = u ^ A;1075u = (A & 0x80) | (u >> 2);10761077break;1078}1079default:1080{1081assert(0);1082break;1083}1084}10851086return u;1087}10881089uint32_t dequant_bise_weight(uint32_t val, uint32_t ise_range)1090{1091assert(val < get_ise_levels(ise_range));10921093uint32_t u = 0;1094switch (ise_range)1095{1096case 0:1097{1098u = val ? 63 : 0;1099break;1100}1101case 1: // 0-21102{1103const uint8_t s_tab_0_2[3] = { 0, 32, 63 };1104u = s_tab_0_2[val];1105break;1106}1107case 2: // 0-31108{1109u = bit_replication_scale(val, 2, 6);1110break;1111}1112case 3: // 0-41113{1114const uint8_t s_tab_0_4[5] = { 0, 16, 32, 47, 63 };1115u = s_tab_0_4[val];1116break;1117}1118case 5: // 0-71119{1120u = bit_replication_scale(val, 3, 6);1121break;1122}1123case 8: // 0-151124{1125u = bit_replication_scale(val, 4, 6);1126break;1127}1128case 11: // 0-311129{1130u = bit_replication_scale(val, 5, 6);1131break;1132}1133case 4: // 0-51134case 6: // 0-91135case 7: // 0-111136case 9: // 0-191137case 10: // 0-231138{1139const uint32_t num_bits = g_ise_range_table[ise_range][0];1140const uint32_t num_trits = g_ise_range_table[ise_range][1]; BASISU_NOTE_UNUSED(num_trits);1141const uint32_t num_quints = g_ise_range_table[ise_range][2]; BASISU_NOTE_UNUSED(num_quints);11421143// compute Table 103 row index1144const int range_index = num_bits * 2 + (num_quints ? 1 : 0);11451146// Extract bits and tris/quints from value1147const uint32_t bits = val & ((1u << num_bits) - 1);1148const uint32_t D = val >> num_bits;11491150assert(D < (num_trits ? 3U : 5U));11511152// Now dequantize1153// See Table 103. ASTC weight unquantization parameters1154static const uint32_t C_table[5] = { 50, 28, 23, 13, 11 };11551156const uint32_t a = bits & 1, b = (bits >> 1) & 1, c = (bits >> 2) & 1;11571158const uint32_t A = (a == 0) ? 0 : 0x7F;11591160uint32_t B = 0;1161if (range_index == 4)1162B = ((b << 6) | (b << 2) | (b << 0));1163else if (range_index == 5)1164B = ((b << 6) | (b << 1));1165else if (range_index == 6)1166B = ((c << 6) | (b << 5) | (c << 1) | (b << 0));11671168const uint32_t C = C_table[range_index - 2];11691170u = D * C + B;1171u = u ^ A;1172u = (A & 0x20) | (u >> 2);1173break;1174}1175default:1176assert(0);1177break;1178}11791180if (u > 32)1181u++;11821183return u;1184}11851186// Returns the nearest ISE symbol given a [0,255] endpoint value.1187uint32_t find_nearest_bise_endpoint(int v, uint32_t ise_range)1188{1189assert(ise_range >= FIRST_VALID_ENDPOINT_ISE_RANGE && ise_range <= LAST_VALID_ENDPOINT_ISE_RANGE);11901191const uint32_t total_levels = get_ise_levels(ise_range);1192int best_e = INT_MAX, best_index = 0;1193for (uint32_t i = 0; i < total_levels; i++)1194{1195const int qv = dequant_bise_endpoint(i, ise_range);1196int e = labs(v - qv);1197if (e < best_e)1198{1199best_e = e;1200best_index = i;1201if (!best_e)1202break;1203}1204}1205return best_index;1206}12071208// Returns the nearest ISE weight given a [0,64] endpoint value.1209uint32_t find_nearest_bise_weight(int v, uint32_t ise_range)1210{1211assert(ise_range >= FIRST_VALID_WEIGHT_ISE_RANGE && ise_range <= LAST_VALID_WEIGHT_ISE_RANGE);1212assert(v <= (int)MAX_WEIGHT_VALUE);12131214const uint32_t total_levels = get_ise_levels(ise_range);1215int best_e = INT_MAX, best_index = 0;1216for (uint32_t i = 0; i < total_levels; i++)1217{1218const int qv = dequant_bise_weight(i, ise_range);1219int e = labs(v - qv);1220if (e < best_e)1221{1222best_e = e;1223best_index = i;1224if (!best_e)1225break;1226}1227}1228return best_index;1229}12301231void create_quant_tables(1232uint8_t* pVal_to_ise, // [0-255] or [0-64] value to nearest ISE symbol, array size is [256] or [65]1233uint8_t* pISE_to_val, // ASTC encoded ISE symbol to [0,255] or [0,64] value, [levels]1234uint8_t* pISE_to_rank, // returns the level rank index given an ISE symbol, [levels]1235uint8_t* pRank_to_ISE, // returns the ISE symbol given a level rank, inverse of pISE_to_rank, [levels]1236uint32_t ise_range, // ise range, [4,20] for endpoints, [0,11] for weights1237bool weight_flag) // false if block endpoints, true if weights1238{1239const uint32_t num_dequant_vals = weight_flag ? (MAX_WEIGHT_VALUE + 1) : 256;12401241for (uint32_t i = 0; i < num_dequant_vals; i++)1242{1243uint32_t bise_index = weight_flag ? astc_helpers::find_nearest_bise_weight(i, ise_range) : astc_helpers::find_nearest_bise_endpoint(i, ise_range);12441245if (pVal_to_ise)1246pVal_to_ise[i] = (uint8_t)bise_index;12471248if (pISE_to_val)1249pISE_to_val[bise_index] = weight_flag ? (uint8_t)astc_helpers::dequant_bise_weight(bise_index, ise_range) : (uint8_t)astc_helpers::dequant_bise_endpoint(bise_index, ise_range);1250}12511252if (pISE_to_rank || pRank_to_ISE)1253{1254const uint32_t num_levels = get_ise_levels(ise_range);12551256if (!g_ise_range_table[ise_range][1] && !g_ise_range_table[ise_range][2])1257{1258// Only bits1259for (uint32_t i = 0; i < num_levels; i++)1260{1261if (pISE_to_rank)1262pISE_to_rank[i] = (uint8_t)i;12631264if (pRank_to_ISE)1265pRank_to_ISE[i] = (uint8_t)i;1266}1267}1268else1269{1270// Range has trits or quints1271uint32_t vals[256];1272for (uint32_t i = 0; i < num_levels; i++)1273{1274uint32_t v = weight_flag ? astc_helpers::dequant_bise_weight(i, ise_range) : astc_helpers::dequant_bise_endpoint(i, ise_range);12751276// Low=ISE value1277// High=dequantized value1278vals[i] = (v << 16) | i;1279}12801281// Sorts by dequantized value1282std::sort(vals, vals + num_levels);12831284for (uint32_t rank = 0; rank < num_levels; rank++)1285{1286uint32_t ise_val = (uint8_t)vals[rank];12871288if (pISE_to_rank)1289pISE_to_rank[ise_val] = (uint8_t)rank;12901291if (pRank_to_ISE)1292pRank_to_ISE[rank] = (uint8_t)ise_val;1293}1294}1295}1296}12971298void pack_void_extent_ldr(astc_block &blk, uint16_t rh, uint16_t gh, uint16_t bh, uint16_t ah, pack_stats* pStats)1299{1300uint8_t* pDst = (uint8_t*)&blk.m_vals[0];1301memset(pDst, 0xFF, 16);13021303pDst[0] = 0b11111100;1304pDst[1] = 0b11111101;13051306pDst[8] = (uint8_t)rh;1307pDst[9] = (uint8_t)(rh >> 8);1308pDst[10] = (uint8_t)gh;1309pDst[11] = (uint8_t)(gh >> 8);1310pDst[12] = (uint8_t)bh;1311pDst[13] = (uint8_t)(bh >> 8);1312pDst[14] = (uint8_t)ah;1313pDst[15] = (uint8_t)(ah >> 8);13141315if (pStats)1316pStats->m_header_bits += 128;1317}13181319// rh-ah are half-floats1320void pack_void_extent_hdr(astc_block& blk, uint16_t rh, uint16_t gh, uint16_t bh, uint16_t ah, pack_stats *pStats)1321{1322uint8_t* pDst = (uint8_t*)&blk.m_vals[0];1323memset(pDst, 0xFF, 16);13241325pDst[0] = 0b11111100;13261327pDst[8] = (uint8_t)rh;1328pDst[9] = (uint8_t)(rh >> 8);1329pDst[10] = (uint8_t)gh;1330pDst[11] = (uint8_t)(gh >> 8);1331pDst[12] = (uint8_t)bh;1332pDst[13] = (uint8_t)(bh >> 8);1333pDst[14] = (uint8_t)ah;1334pDst[15] = (uint8_t)(ah >> 8);13351336if (pStats)1337pStats->m_header_bits += 128;1338}13391340bool is_cem_ldr(uint32_t mode)1341{1342switch (mode)1343{1344case CEM_LDR_LUM_DIRECT:1345case CEM_LDR_LUM_BASE_PLUS_OFS:1346case CEM_LDR_LUM_ALPHA_DIRECT:1347case CEM_LDR_LUM_ALPHA_BASE_PLUS_OFS:1348case CEM_LDR_RGB_BASE_SCALE:1349case CEM_LDR_RGB_DIRECT:1350case CEM_LDR_RGB_BASE_PLUS_OFFSET:1351case CEM_LDR_RGB_BASE_SCALE_PLUS_TWO_A:1352case CEM_LDR_RGBA_DIRECT:1353case CEM_LDR_RGBA_BASE_PLUS_OFFSET:1354return true;1355default:1356break;1357}13581359return false;1360}13611362bool is_valid_block_size(uint32_t w, uint32_t h)1363{1364assert((w >= MIN_BLOCK_DIM) && (w <= MAX_BLOCK_DIM));1365assert((h >= MIN_BLOCK_DIM) && (h <= MAX_BLOCK_DIM));13661367#define SIZECHK(x, y) if ((w == (x)) && (h == (y))) return true;1368SIZECHK(4, 4);1369SIZECHK(5, 4);13701371SIZECHK(5, 5);13721373SIZECHK(6, 5);1374SIZECHK(6, 6);13751376SIZECHK(8, 5);1377SIZECHK(8, 6);1378SIZECHK(10, 5);1379SIZECHK(10, 6);13801381SIZECHK(8, 8);1382SIZECHK(10, 8);1383SIZECHK(10, 10);13841385SIZECHK(12, 10);1386SIZECHK(12, 12);1387#undef SIZECHK13881389return false;1390}13911392bool block_has_any_hdr_cems(const log_astc_block& log_blk)1393{1394assert((log_blk.m_num_partitions >= 1) && (log_blk.m_num_partitions <= MAX_PARTITIONS));13951396for (uint32_t i = 0; i < log_blk.m_num_partitions; i++)1397if (is_cem_hdr(log_blk.m_color_endpoint_modes[i]))1398return true;13991400return false;1401}14021403bool block_has_any_ldr_cems(const log_astc_block& log_blk)1404{1405assert((log_blk.m_num_partitions >= 1) && (log_blk.m_num_partitions <= MAX_PARTITIONS));14061407for (uint32_t i = 0; i < log_blk.m_num_partitions; i++)1408if (!is_cem_hdr(log_blk.m_color_endpoint_modes[i]))1409return true;14101411return false;1412}14131414dequant_tables g_dequant_tables;14151416void precompute_texel_partitions_4x4();1417void precompute_texel_partitions_6x6();14181419void init_tables(bool init_rank_tabs)1420{1421g_dequant_tables.init(init_rank_tabs);14221423precompute_texel_partitions_4x4();1424precompute_texel_partitions_6x6();1425}14261427void compute_upsample_weights(1428int block_width, int block_height,1429int weight_grid_width, int weight_grid_height,1430weighted_sample* pWeights) // there will be block_width * block_height bilinear samples1431{1432const uint32_t scaleX = (1024 + block_width / 2) / (block_width - 1);1433const uint32_t scaleY = (1024 + block_height / 2) / (block_height - 1);14341435for (int texelY = 0; texelY < block_height; texelY++)1436{1437for (int texelX = 0; texelX < block_width; texelX++)1438{1439const uint32_t gX = (scaleX * texelX * (weight_grid_width - 1) + 32) >> 6;1440const uint32_t gY = (scaleY * texelY * (weight_grid_height - 1) + 32) >> 6;1441const uint32_t jX = gX >> 4;1442const uint32_t jY = gY >> 4;1443const uint32_t fX = gX & 0xf;1444const uint32_t fY = gY & 0xf;1445const uint32_t w11 = (fX * fY + 8) >> 4;1446const uint32_t w10 = fY - w11;1447const uint32_t w01 = fX - w11;1448const uint32_t w00 = 16 - fX - fY + w11;14491450weighted_sample& s = pWeights[texelX + texelY * block_width];1451s.m_src_x = (uint8_t)jX;1452s.m_src_y = (uint8_t)jY;1453s.m_weights[0][0] = (uint8_t)w00;1454s.m_weights[0][1] = (uint8_t)w01;1455s.m_weights[1][0] = (uint8_t)w10;1456s.m_weights[1][1] = (uint8_t)w11;1457}1458}1459}14601461// Should be dequantized [0,64] weights1462void upsample_weight_grid(1463uint32_t bx, uint32_t by, // destination/to dimension1464uint32_t wx, uint32_t wy, // source/from dimension1465const uint8_t* pSrc_weights, // these are dequantized [0,64] weights, NOT ISE symbols, [wy][wx]1466uint8_t* pDst_weights) // [by][bx]1467{1468assert((bx >= 2) && (by >= 2) && (bx <= 12) && (by <= 12));1469assert((wx >= 2) && (wy >= 2) && (wx <= bx) && (wy <= by));14701471const uint32_t total_src_weights = wx * wy;1472const uint32_t total_dst_weights = bx * by;14731474if (total_src_weights == total_dst_weights)1475{1476memcpy(pDst_weights, pSrc_weights, total_src_weights);1477return;1478}14791480weighted_sample weights[12 * 12];1481compute_upsample_weights(bx, by, wx, wy, weights);14821483const weighted_sample* pS = weights;14841485for (uint32_t y = 0; y < by; y++)1486{1487for (uint32_t x = 0; x < bx; x++, ++pS)1488{1489const uint32_t w00 = pS->m_weights[0][0];1490const uint32_t w01 = pS->m_weights[0][1];1491const uint32_t w10 = pS->m_weights[1][0];1492const uint32_t w11 = pS->m_weights[1][1];14931494assert(w00 || w01 || w10 || w11);14951496const uint32_t sx = pS->m_src_x, sy = pS->m_src_y;14971498uint32_t total = 8;1499if (w00) total += pSrc_weights[bounds_check(sx + sy * wx, 0U, total_src_weights)] * w00;1500if (w01) total += pSrc_weights[bounds_check(sx + 1 + sy * wx, 0U, total_src_weights)] * w01;1501if (w10) total += pSrc_weights[bounds_check(sx + (sy + 1) * wx, 0U, total_src_weights)] * w10;1502if (w11) total += pSrc_weights[bounds_check(sx + 1 + (sy + 1) * wx, 0U, total_src_weights)] * w11;15031504pDst_weights[x + y * bx] = (uint8_t)(total >> 4);1505}1506}1507}15081509inline uint32_t hash52(uint32_t v)1510{1511uint32_t p = v;1512p ^= p >> 15; p -= p << 17; p += p << 7; p += p << 4;1513p ^= p >> 5; p += p << 16; p ^= p >> 7; p ^= p >> 3;1514p ^= p << 6; p ^= p >> 17;1515return p;1516}15171518// small_block = num_blk_pixels < 311519int compute_texel_partition(uint32_t seedIn, uint32_t xIn, uint32_t yIn, uint32_t zIn, int num_partitions, bool small_block)1520{1521assert(zIn == 0);15221523const uint32_t x = small_block ? xIn << 1 : xIn;1524const uint32_t y = small_block ? yIn << 1 : yIn;1525const uint32_t z = small_block ? zIn << 1 : zIn;1526const uint32_t seed = seedIn + 1024 * (num_partitions - 1);1527const uint32_t rnum = hash52(seed);15281529uint8_t seed1 = (uint8_t)(rnum & 0xf);1530uint8_t seed2 = (uint8_t)((rnum >> 4) & 0xf);1531uint8_t seed3 = (uint8_t)((rnum >> 8) & 0xf);1532uint8_t seed4 = (uint8_t)((rnum >> 12) & 0xf);1533uint8_t seed5 = (uint8_t)((rnum >> 16) & 0xf);1534uint8_t seed6 = (uint8_t)((rnum >> 20) & 0xf);1535uint8_t seed7 = (uint8_t)((rnum >> 24) & 0xf);1536uint8_t seed8 = (uint8_t)((rnum >> 28) & 0xf);1537uint8_t seed9 = (uint8_t)((rnum >> 18) & 0xf);1538uint8_t seed10 = (uint8_t)((rnum >> 22) & 0xf);1539uint8_t seed11 = (uint8_t)((rnum >> 26) & 0xf);1540uint8_t seed12 = (uint8_t)(((rnum >> 30) | (rnum << 2)) & 0xf);15411542seed1 = (uint8_t)(seed1 * seed1);1543seed2 = (uint8_t)(seed2 * seed2);1544seed3 = (uint8_t)(seed3 * seed3);1545seed4 = (uint8_t)(seed4 * seed4);1546seed5 = (uint8_t)(seed5 * seed5);1547seed6 = (uint8_t)(seed6 * seed6);1548seed7 = (uint8_t)(seed7 * seed7);1549seed8 = (uint8_t)(seed8 * seed8);1550seed9 = (uint8_t)(seed9 * seed9);1551seed10 = (uint8_t)(seed10 * seed10);1552seed11 = (uint8_t)(seed11 * seed11);1553seed12 = (uint8_t)(seed12 * seed12);15541555const int shA = (seed & 2) != 0 ? 4 : 5;1556const int shB = (num_partitions == 3) ? 6 : 5;1557const int sh1 = (seed & 1) != 0 ? shA : shB;1558const int sh2 = (seed & 1) != 0 ? shB : shA;1559const int sh3 = (seed & 0x10) != 0 ? sh1 : sh2;15601561seed1 = (uint8_t)(seed1 >> sh1);1562seed2 = (uint8_t)(seed2 >> sh2);1563seed3 = (uint8_t)(seed3 >> sh1);1564seed4 = (uint8_t)(seed4 >> sh2);1565seed5 = (uint8_t)(seed5 >> sh1);1566seed6 = (uint8_t)(seed6 >> sh2);1567seed7 = (uint8_t)(seed7 >> sh1);1568seed8 = (uint8_t)(seed8 >> sh2);1569seed9 = (uint8_t)(seed9 >> sh3);1570seed10 = (uint8_t)(seed10 >> sh3);1571seed11 = (uint8_t)(seed11 >> sh3);1572seed12 = (uint8_t)(seed12 >> sh3);15731574const int a = 0x3f & (seed1 * x + seed2 * y + seed11 * z + (rnum >> 14));1575const int b = 0x3f & (seed3 * x + seed4 * y + seed12 * z + (rnum >> 10));1576const int c = (num_partitions >= 3) ? 0x3f & (seed5 * x + seed6 * y + seed9 * z + (rnum >> 6)) : 0;1577const int d = (num_partitions >= 4) ? 0x3f & (seed7 * x + seed8 * y + seed10 * z + (rnum >> 2)) : 0;15781579return (a >= b && a >= c && a >= d) ? 01580: (b >= c && b >= d) ? 11581: (c >= d) ? 21582: 3;1583}15841585// 4x4, 2 and 3 subsets1586static uint32_t g_texel_partitions_4x4[1024][2];15871588// 6x6, 2 and 3 subsets (2 subsets low 4 bits, 3 subsets high 4 bits)1589static uint8_t g_texel_partitions_6x6[1024][6 * 6];15901591void precompute_texel_partitions_4x4()1592{1593for (uint32_t p = 0; p < 1024; p++)1594{1595uint32_t v2 = 0, v3 = 0;15961597for (uint32_t y = 0; y < 4; y++)1598{1599for (uint32_t x = 0; x < 4; x++)1600{1601const uint32_t shift = x * 2 + y * 8;1602v2 |= (compute_texel_partition(p, x, y, 0, 2, true) << shift);1603v3 |= (compute_texel_partition(p, x, y, 0, 3, true) << shift);1604}1605}16061607g_texel_partitions_4x4[p][0] = v2;1608g_texel_partitions_4x4[p][1] = v3;1609}1610}16111612void precompute_texel_partitions_6x6()1613{1614for (uint32_t p = 0; p < 1024; p++)1615{1616for (uint32_t y = 0; y < 6; y++)1617{1618for (uint32_t x = 0; x < 6; x++)1619{1620const uint32_t p2 = compute_texel_partition(p, x, y, 0, 2, false);1621const uint32_t p3 = compute_texel_partition(p, x, y, 0, 3, false);16221623assert((p2 <= 1) && (p3 <= 2));1624g_texel_partitions_6x6[p][x + y * 6] = (uint8_t)((p3 << 4) | p2);1625}1626}1627}1628}16291630static inline int get_precompute_texel_partitions_4x4(uint32_t seed, uint32_t x, uint32_t y, uint32_t num_partitions)1631{1632assert(g_texel_partitions_4x4[1][0]);1633assert(seed < 1024);1634assert((x <= 3) && (y <= 3));1635assert((num_partitions >= 2) && (num_partitions <= 3));16361637const uint32_t shift = x * 2 + y * 8;1638return (g_texel_partitions_4x4[seed][num_partitions - 2] >> shift) & 3;1639}16401641static inline int get_precompute_texel_partitions_6x6(uint32_t seed, uint32_t x, uint32_t y, uint32_t num_partitions)1642{1643assert(g_texel_partitions_6x6[0][0]);1644assert(seed < 1024);1645assert((x <= 5) && (y <= 5));1646assert((num_partitions >= 2) && (num_partitions <= 3));16471648const uint32_t shift = (num_partitions == 3) ? 4 : 0;1649return (g_texel_partitions_6x6[seed][x + y * 6] >> shift) & 3;1650}16511652void blue_contract(1653int r, int g, int b, int a,1654int &dr, int &dg, int &db, int &da)1655{1656dr = (r + b) >> 1;1657dg = (g + b) >> 1;1658db = b;1659da = a;1660}16611662inline void bit_transfer_signed(int& a, int& b)1663{1664b >>= 1;1665b |= (a & 0x80);1666a >>= 1;1667a &= 0x3F;1668if ((a & 0x20) != 0)1669a -= 0x40;1670}16711672static inline int clamp(int a, int l, int h)1673{1674if (a < l)1675a = l;1676else if (a > h)1677a = h;1678return a;1679}16801681static inline float clampf(float a, float l, float h)1682{1683if (a < l)1684a = l;1685else if (a > h)1686a = h;1687return a;1688}16891690inline int sign_extend(int src, int num_src_bits)1691{1692assert((num_src_bits >= 2) && (num_src_bits <= 31));16931694const bool negative = (src & (1 << (num_src_bits - 1))) != 0;1695if (negative)1696return src | ~((1 << num_src_bits) - 1);1697else1698return src & ((1 << num_src_bits) - 1);1699}17001701// endpoints is [4][2]1702void decode_endpoint(uint32_t cem_index, int (*pEndpoints)[2], const uint8_t *pE)1703{1704assert(cem_index <= CEM_HDR_RGB_HDR_ALPHA);17051706int v0 = pE[0], v1 = pE[1];17071708int& e0_r = pEndpoints[0][0], &e0_g = pEndpoints[1][0], &e0_b = pEndpoints[2][0], &e0_a = pEndpoints[3][0];1709int& e1_r = pEndpoints[0][1], &e1_g = pEndpoints[1][1], &e1_b = pEndpoints[2][1], &e1_a = pEndpoints[3][1];17101711switch (cem_index)1712{1713case CEM_LDR_LUM_DIRECT:1714{1715e0_r = v0; e1_r = v1;1716e0_g = v0; e1_g = v1;1717e0_b = v0; e1_b = v1;1718e0_a = 0xFF; e1_a = 0xFF;1719break;1720}1721case CEM_LDR_LUM_BASE_PLUS_OFS:1722{1723int l0 = (v0 >> 2) | (v1 & 0xc0);1724int l1 = l0 + (v1 & 0x3f);17251726if (l1 > 0xFF)1727l1 = 0xFF;17281729e0_r = l0; e1_r = l1;1730e0_g = l0; e1_g = l1;1731e0_b = l0; e1_b = l1;1732e0_a = 0xFF; e1_a = 0xFF;1733break;1734}1735case CEM_LDR_LUM_ALPHA_DIRECT:1736{1737int v2 = pE[2], v3 = pE[3];17381739e0_r = v0; e1_r = v1;1740e0_g = v0; e1_g = v1;1741e0_b = v0; e1_b = v1;1742e0_a = v2; e1_a = v3;1743break;1744}1745case CEM_LDR_LUM_ALPHA_BASE_PLUS_OFS:1746{1747int v2 = pE[2], v3 = pE[3];17481749bit_transfer_signed(v1, v0);1750bit_transfer_signed(v3, v2);17511752e0_r = v0; e1_r = v0 + v1;1753e0_g = v0; e1_g = v0 + v1;1754e0_b = v0; e1_b = v0 + v1;1755e0_a = v2; e1_a = v2 + v3;17561757for (uint32_t c = 0; c < 4; c++)1758{1759pEndpoints[c][0] = clamp(pEndpoints[c][0], 0, 255);1760pEndpoints[c][1] = clamp(pEndpoints[c][1], 0, 255);1761}17621763break;1764}1765case CEM_LDR_RGB_BASE_SCALE:1766{1767int v2 = pE[2], v3 = pE[3];17681769e0_r = (v0 * v3) >> 8; e1_r = v0;1770e0_g = (v1 * v3) >> 8; e1_g = v1;1771e0_b = (v2 * v3) >> 8; e1_b = v2;1772e0_a = 0xFF; e1_a = 0xFF;17731774break;1775}1776case CEM_LDR_RGB_DIRECT:1777{1778int v2 = pE[2], v3 = pE[3], v4 = pE[4], v5 = pE[5];17791780if ((v1 + v3 + v5) >= (v0 + v2 + v4))1781{1782e0_r = v0; e1_r = v1;1783e0_g = v2; e1_g = v3;1784e0_b = v4; e1_b = v5;1785e0_a = 0xFF; e1_a = 0xFF;1786}1787else1788{1789blue_contract(v1, v3, v5, 0xFF, e0_r, e0_g, e0_b, e0_a);1790blue_contract(v0, v2, v4, 0xFF, e1_r, e1_g, e1_b, e1_a);1791}17921793break;1794}1795case CEM_LDR_RGB_BASE_PLUS_OFFSET:1796{1797int v2 = pE[2], v3 = pE[3], v4 = pE[4], v5 = pE[5];17981799bit_transfer_signed(v1, v0);1800bit_transfer_signed(v3, v2);1801bit_transfer_signed(v5, v4);18021803if ((v1 + v3 + v5) >= 0)1804{1805e0_r = v0; e1_r = v0 + v1;1806e0_g = v2; e1_g = v2 + v3;1807e0_b = v4; e1_b = v4 + v5;1808e0_a = 0xFF; e1_a = 0xFF;1809}1810else1811{1812blue_contract(v0 + v1, v2 + v3, v4 + v5, 0xFF, e0_r, e0_g, e0_b, e0_a);1813blue_contract(v0, v2, v4, 0xFF, e1_r, e1_g, e1_b, e1_a);1814}18151816for (uint32_t c = 0; c < 4; c++)1817{1818pEndpoints[c][0] = clamp(pEndpoints[c][0], 0, 255);1819pEndpoints[c][1] = clamp(pEndpoints[c][1], 0, 255);1820}18211822break;1823}1824case CEM_LDR_RGB_BASE_SCALE_PLUS_TWO_A:1825{1826int v2 = pE[2], v3 = pE[3], v4 = pE[4], v5 = pE[5];18271828e0_r = (v0 * v3) >> 8; e1_r = v0;1829e0_g = (v1 * v3) >> 8; e1_g = v1;1830e0_b = (v2 * v3) >> 8; e1_b = v2;1831e0_a = v4; e1_a = v5;18321833break;1834}1835case CEM_LDR_RGBA_DIRECT:1836{1837int v2 = pE[2], v3 = pE[3], v4 = pE[4], v5 = pE[5], v6 = pE[6], v7 = pE[7];18381839if ((v1 + v3 + v5) >= (v0 + v2 + v4))1840{1841e0_r = v0; e1_r = v1;1842e0_g = v2; e1_g = v3;1843e0_b = v4; e1_b = v5;1844e0_a = v6; e1_a = v7;1845}1846else1847{1848blue_contract(v1, v3, v5, v7, e0_r, e0_g, e0_b, e0_a);1849blue_contract(v0, v2, v4, v6, e1_r, e1_g, e1_b, e1_a);1850}18511852break;1853}1854case CEM_LDR_RGBA_BASE_PLUS_OFFSET:1855{1856int v2 = pE[2], v3 = pE[3], v4 = pE[4], v5 = pE[5], v6 = pE[6], v7 = pE[7];18571858bit_transfer_signed(v1, v0);1859bit_transfer_signed(v3, v2);1860bit_transfer_signed(v5, v4);1861bit_transfer_signed(v7, v6);18621863if ((v1 + v3 + v5) >= 0)1864{1865e0_r = v0; e1_r = v0 + v1;1866e0_g = v2; e1_g = v2 + v3;1867e0_b = v4; e1_b = v4 + v5;1868e0_a = v6; e1_a = v6 + v7;1869}1870else1871{1872blue_contract(v0 + v1, v2 + v3, v4 + v5, v6 + v7, e0_r, e0_g, e0_b, e0_a);1873blue_contract(v0, v2, v4, v6, e1_r, e1_g, e1_b, e1_a);1874}18751876for (uint32_t c = 0; c < 4; c++)1877{1878pEndpoints[c][0] = clamp(pEndpoints[c][0], 0, 255);1879pEndpoints[c][1] = clamp(pEndpoints[c][1], 0, 255);1880}18811882break;1883}1884case CEM_HDR_LUM_LARGE_RANGE:1885{1886int y0, y1;1887if (v1 >= v0)1888{1889y0 = (v0 << 4);1890y1 = (v1 << 4);1891}1892else1893{1894y0 = (v1 << 4) + 8;1895y1 = (v0 << 4) - 8;1896}18971898e0_r = y0; e1_r = y1;1899e0_g = y0; e1_g = y1;1900e0_b = y0; e1_b = y1;1901e0_a = 0x780; e1_a = 0x780;19021903break;1904}1905case CEM_HDR_LUM_SMALL_RANGE:1906{1907int y0, y1, d;19081909if ((v0 & 0x80) != 0)1910{1911y0 = ((v1 & 0xE0) << 4) | ((v0 & 0x7F) << 2);1912d = (v1 & 0x1F) << 2;1913}1914else1915{1916y0 = ((v1 & 0xF0) << 4) | ((v0 & 0x7F) << 1);1917d = (v1 & 0x0F) << 1;1918}19191920y1 = y0 + d;1921if (y1 > 0xFFF)1922y1 = 0xFFF;19231924e0_r = y0; e1_r = y1;1925e0_g = y0; e1_g = y1;1926e0_b = y0; e1_b = y1;1927e0_a = 0x780; e1_a = 0x780;19281929break;1930}1931case CEM_HDR_RGB_BASE_SCALE:1932{1933int v2 = pE[2], v3 = pE[3];19341935int modeval = ((v0 & 0xC0) >> 6) | ((v1 & 0x80) >> 5) | ((v2 & 0x80) >> 4);19361937int majcomp, mode;1938if ((modeval & 0xC) != 0xC)1939{1940majcomp = modeval >> 2;1941mode = modeval & 3;1942}1943else if (modeval != 0xF)1944{1945majcomp = modeval & 3;1946mode = 4;1947}1948else1949{1950majcomp = 0;1951mode = 5;1952}19531954int red = v0 & 0x3f;1955int green = v1 & 0x1f;1956int blue = v2 & 0x1f;1957int scale = v3 & 0x1f;19581959int x0 = (v1 >> 6) & 1;1960int x1 = (v1 >> 5) & 1;1961int x2 = (v2 >> 6) & 1;1962int x3 = (v2 >> 5) & 1;1963int x4 = (v3 >> 7) & 1;1964int x5 = (v3 >> 6) & 1;1965int x6 = (v3 >> 5) & 1;19661967int ohm = 1 << mode;1968if (ohm & 0x30) green |= x0 << 6;1969if (ohm & 0x3A) green |= x1 << 5;1970if (ohm & 0x30) blue |= x2 << 6;1971if (ohm & 0x3A) blue |= x3 << 5;1972if (ohm & 0x3D) scale |= x6 << 5;1973if (ohm & 0x2D) scale |= x5 << 6;1974if (ohm & 0x04) scale |= x4 << 7;1975if (ohm & 0x3B) red |= x4 << 6;1976if (ohm & 0x04) red |= x3 << 6;1977if (ohm & 0x10) red |= x5 << 7;1978if (ohm & 0x0F) red |= x2 << 7;1979if (ohm & 0x05) red |= x1 << 8;1980if (ohm & 0x0A) red |= x0 << 8;1981if (ohm & 0x05) red |= x0 << 9;1982if (ohm & 0x02) red |= x6 << 9;1983if (ohm & 0x01) red |= x3 << 10;1984if (ohm & 0x02) red |= x5 << 10;19851986static const int s_shamts[6] = { 1,1,2,3,4,5 };19871988const int shamt = s_shamts[mode];1989red <<= shamt;1990green <<= shamt;1991blue <<= shamt;1992scale <<= shamt;19931994if (mode != 5)1995{1996green = red - green;1997blue = red - blue;1998}19992000if (majcomp == 1)2001std::swap(red, green);20022003if (majcomp == 2)2004std::swap(red, blue);20052006e1_r = clamp(red, 0, 0xFFF);2007e1_g = clamp(green, 0, 0xFFF);2008e1_b = clamp(blue, 0, 0xFFF);2009e1_a = 0x780;20102011e0_r = clamp(red - scale, 0, 0xFFF);2012e0_g = clamp(green - scale, 0, 0xFFF);2013e0_b = clamp(blue - scale, 0, 0xFFF);2014e0_a = 0x780;20152016break;2017}2018case CEM_HDR_RGB_HDR_ALPHA:2019case CEM_HDR_RGB_LDR_ALPHA:2020case CEM_HDR_RGB:2021{2022int v2 = pE[2], v3 = pE[3], v4 = pE[4], v5 = pE[5];20232024int majcomp = ((v4 & 0x80) >> 7) | ((v5 & 0x80) >> 6);20252026e0_a = 0x780;2027e1_a = 0x780;20282029if (majcomp == 3)2030{2031e0_r = v0 << 4;2032e0_g = v2 << 4;2033e0_b = (v4 & 0x7f) << 5;20342035e1_r = v1 << 4;2036e1_g = v3 << 4;2037e1_b = (v5 & 0x7f) << 5;2038}2039else2040{2041int mode = ((v1 & 0x80) >> 7) | ((v2 & 0x80) >> 6) | ((v3 & 0x80) >> 5);2042int va = v0 | ((v1 & 0x40) << 2);2043int vb0 = v2 & 0x3f;2044int vb1 = v3 & 0x3f;2045int vc = v1 & 0x3f;2046int vd0 = v4 & 0x7f;2047int vd1 = v5 & 0x7f;20482049static const int s_dbitstab[8] = { 7,6,7,6,5,6,5,6 };2050vd0 = sign_extend(vd0, s_dbitstab[mode]);2051vd1 = sign_extend(vd1, s_dbitstab[mode]);20522053int x0 = (v2 >> 6) & 1;2054int x1 = (v3 >> 6) & 1;2055int x2 = (v4 >> 6) & 1;2056int x3 = (v5 >> 6) & 1;2057int x4 = (v4 >> 5) & 1;2058int x5 = (v5 >> 5) & 1;20592060int ohm = 1 << mode;2061if (ohm & 0xA4) va |= x0 << 9;2062if (ohm & 0x08) va |= x2 << 9;2063if (ohm & 0x50) va |= x4 << 9;2064if (ohm & 0x50) va |= x5 << 10;2065if (ohm & 0xA0) va |= x1 << 10;2066if (ohm & 0xC0) va |= x2 << 11;2067if (ohm & 0x04) vc |= x1 << 6;2068if (ohm & 0xE8) vc |= x3 << 6;2069if (ohm & 0x20) vc |= x2 << 7;2070if (ohm & 0x5B) vb0 |= x0 << 6;2071if (ohm & 0x5B) vb1 |= x1 << 6;2072if (ohm & 0x12) vb0 |= x2 << 7;2073if (ohm & 0x12) vb1 |= x3 << 7;20742075int shamt = (mode >> 1) ^ 3;2076va = (uint32_t)va << shamt;2077vb0 = (uint32_t)vb0 << shamt;2078vb1 = (uint32_t)vb1 << shamt;2079vc = (uint32_t)vc << shamt;2080vd0 = (uint32_t)vd0 << shamt;2081vd1 = (uint32_t)vd1 << shamt;20822083e1_r = clamp(va, 0, 0xFFF);2084e1_g = clamp(va - vb0, 0, 0xFFF);2085e1_b = clamp(va - vb1, 0, 0xFFF);20862087e0_r = clamp(va - vc, 0, 0xFFF);2088e0_g = clamp(va - vb0 - vc - vd0, 0, 0xFFF);2089e0_b = clamp(va - vb1 - vc - vd1, 0, 0xFFF);20902091if (majcomp == 1)2092{2093std::swap(e0_r, e0_g);2094std::swap(e1_r, e1_g);2095}2096else if (majcomp == 2)2097{2098std::swap(e0_r, e0_b);2099std::swap(e1_r, e1_b);2100}2101}21022103if (cem_index == CEM_HDR_RGB_LDR_ALPHA)2104{2105int v6 = pE[6], v7 = pE[7];21062107e0_a = v6;2108e1_a = v7;2109}2110else if (cem_index == CEM_HDR_RGB_HDR_ALPHA)2111{2112int v6 = pE[6], v7 = pE[7];21132114// Extract mode bits2115int mode = ((v6 >> 7) & 1) | ((v7 >> 6) & 2);2116v6 &= 0x7F;2117v7 &= 0x7F;21182119if (mode == 3)2120{2121e0_a = v6 << 5;2122e1_a = v7 << 5;2123}2124else2125{2126v6 |= (v7 << (mode + 1)) & 0x780;2127v7 &= (0x3F >> mode);2128v7 ^= (0x20 >> mode);2129v7 -= (0x20 >> mode);2130v6 <<= (4 - mode);2131v7 <<= (4 - mode);21322133v7 += v6;2134v7 = clamp(v7, 0, 0xFFF);2135e0_a = v6;2136e1_a = v7;2137}2138}21392140break;2141}2142default:2143{2144assert(0);2145for (uint32_t c = 0; c < 4; c++)2146{2147pEndpoints[c][0] = 0;2148pEndpoints[c][1] = 0;2149}2150break;2151}2152}2153}21542155static inline bool is_half_inf_or_nan(half_float v)2156{2157return get_bits(v, 10, 14) == 31;2158}21592160// This float->half conversion matches how "F32TO16" works on Intel GPU's.2161half_float float_to_half(float val, bool toward_zero)2162{2163union { float f; int32_t i; uint32_t u; } fi = { val };2164const int flt_m = fi.i & 0x7FFFFF, flt_e = (fi.i >> 23) & 0xFF, flt_s = (fi.i >> 31) & 0x1;2165int s = flt_s, e = 0, m = 0;21662167// inf/NaN2168if (flt_e == 0xff)2169{2170e = 31;2171if (flt_m != 0) // NaN2172m = 1;2173}2174// not zero or denormal2175else if (flt_e != 0)2176{2177int new_exp = flt_e - 127;2178if (new_exp > 15)2179e = 31;2180else if (new_exp < -14)2181{2182if (toward_zero)2183m = (int)truncf((1 << 24) * fabsf(fi.f));2184else2185m = lrintf((1 << 24) * fabsf(fi.f));2186}2187else2188{2189e = new_exp + 15;2190if (toward_zero)2191m = (int)truncf((float)flt_m * (1.0f / (float)(1 << 13)));2192else2193m = lrintf((float)flt_m * (1.0f / (float)(1 << 13)));2194}2195}21962197assert((0 <= m) && (m <= 1024));2198if (m == 1024)2199{2200e++;2201m = 0;2202}22032204assert((s >= 0) && (s <= 1));2205assert((e >= 0) && (e <= 31));2206assert((m >= 0) && (m <= 1023));22072208half_float result = (half_float)((s << 15) | (e << 10) | m);2209return result;2210}22112212float half_to_float(half_float hval)2213{2214union { float f; uint32_t u; } x = { 0 };22152216uint32_t s = ((uint32_t)hval >> 15) & 1;2217uint32_t e = ((uint32_t)hval >> 10) & 0x1F;2218uint32_t m = (uint32_t)hval & 0x3FF;22192220if (!e)2221{2222if (!m)2223{2224// +- 02225x.u = s << 31;2226return x.f;2227}2228else2229{2230// denormalized2231while (!(m & 0x00000400))2232{2233m <<= 1;2234--e;2235}22362237++e;2238m &= ~0x00000400;2239}2240}2241else if (e == 31)2242{2243if (m == 0)2244{2245// +/- INF2246x.u = (s << 31) | 0x7f800000;2247return x.f;2248}2249else2250{2251// +/- NaN2252x.u = (s << 31) | 0x7f800000 | (m << 13);2253return x.f;2254}2255}22562257e = e + (127 - 15);2258m = m << 13;22592260assert(s <= 1);2261assert(m <= 0x7FFFFF);2262assert(e <= 255);22632264x.u = m | (e << 23) | (s << 31);2265return x.f;2266}22672268// See https://registry.khronos.org/OpenGL/extensions/EXT/EXT_texture_shared_exponent.txt2269const int RGB9E5_EXPONENT_BITS = 5, RGB9E5_MANTISSA_BITS = 9, RGB9E5_EXP_BIAS = 15, RGB9E5_MAX_VALID_BIASED_EXP = 31;2270const int MAX_RGB9E5_EXP = (RGB9E5_MAX_VALID_BIASED_EXP - RGB9E5_EXP_BIAS);2271const int RGB9E5_MANTISSA_VALUES = (1 << RGB9E5_MANTISSA_BITS);2272const int MAX_RGB9E5_MANTISSA = (RGB9E5_MANTISSA_VALUES - 1);2273//const int MAX_RGB9E5 = (int)(((float)MAX_RGB9E5_MANTISSA) / RGB9E5_MANTISSA_VALUES * (1 << MAX_RGB9E5_EXP));2274const int EPSILON_RGB9E5 = (int)((1.0f / (float)RGB9E5_MANTISSA_VALUES) / (float)(1 << RGB9E5_EXP_BIAS));22752276void unpack_rgb9e5(uint32_t packed, float& r, float& g, float& b)2277{2278int x = packed & 511;2279int y = (packed >> 9) & 511;2280int z = (packed >> 18) & 511;2281int w = (packed >> 27) & 31;22822283const float scale = powf(2.0f, static_cast<float>(w - RGB9E5_EXP_BIAS - RGB9E5_MANTISSA_BITS));22842285r = x * scale;2286g = y * scale;2287b = z * scale;2288}22892290// floor_log2 is not correct for the denorm and zero values, but we are going to do a max of this value with the minimum rgb9e5 exponent that will hide these problem cases.2291static inline int floor_log2(float x)2292{2293union float7542294{2295unsigned int raw;2296float value;2297};22982299float754 f;2300f.value = x;2301// Extract float exponent2302return ((f.raw >> 23) & 0xFF) - 127;2303}23042305static inline int maximumi(int a, int b) { return (a > b) ? a : b; }2306static inline float maximumf(float a, float b) { return (a > b) ? a : b; }23072308uint32_t pack_rgb9e5(float r, float g, float b)2309{2310r = clampf(r, 0.0f, MAX_RGB9E5);2311g = clampf(g, 0.0f, MAX_RGB9E5);2312b = clampf(b, 0.0f, MAX_RGB9E5);23132314float maxrgb = maximumf(maximumf(r, g), b);2315int exp_shared = maximumi(-RGB9E5_EXP_BIAS - 1, floor_log2(maxrgb)) + 1 + RGB9E5_EXP_BIAS;2316assert((exp_shared >= 0) && (exp_shared <= RGB9E5_MAX_VALID_BIASED_EXP));23172318float denom = powf(2.0f, (float)(exp_shared - RGB9E5_EXP_BIAS - RGB9E5_MANTISSA_BITS));23192320int maxm = (int)floorf((maxrgb / denom) + 0.5f);2321if (maxm == (MAX_RGB9E5_MANTISSA + 1))2322{2323denom *= 2;2324exp_shared += 1;2325assert(exp_shared <= RGB9E5_MAX_VALID_BIASED_EXP);2326}2327else2328{2329assert(maxm <= MAX_RGB9E5_MANTISSA);2330}23312332int rm = (int)floorf((r / denom) + 0.5f);2333int gm = (int)floorf((g / denom) + 0.5f);2334int bm = (int)floorf((b / denom) + 0.5f);23352336assert((rm >= 0) && (rm <= MAX_RGB9E5_MANTISSA));2337assert((gm >= 0) && (gm <= MAX_RGB9E5_MANTISSA));2338assert((bm >= 0) && (bm <= MAX_RGB9E5_MANTISSA));23392340return rm | (gm << 9) | (bm << 18) | (exp_shared << 27);2341}23422343static inline int clz17(uint32_t x)2344{2345assert(x <= 0x1FFFF);2346x &= 0x1FFFF;23472348if (!x)2349return 17;23502351uint32_t n = 0;2352while ((x & 0x10000) == 0)2353{2354x <<= 1u;2355n++;2356}23572358return n;2359}23602361static inline uint32_t pack_rgb9e5_ldr_astc(int Cr, int Cg, int Cb)2362{2363int lz = clz17(Cr | Cg | Cb | 1);2364if (Cr == 65535) { Cr = 65536; lz = 0; }2365if (Cg == 65535) { Cg = 65536; lz = 0; }2366if (Cb == 65535) { Cb = 65536; lz = 0; }2367Cr <<= lz; Cg <<= lz; Cb <<= lz;2368Cr = (Cr >> 8) & 0x1FF;2369Cg = (Cg >> 8) & 0x1FF;2370Cb = (Cb >> 8) & 0x1FF;2371uint32_t exponent = 16 - lz;2372uint32_t texel = (exponent << 27) | (Cb << 18) | (Cg << 9) | Cr;2373return texel;2374}23752376static inline uint32_t pack_rgb9e5_hdr_astc(int Cr, int Cg, int Cb)2377{2378if (Cr > 0x7c00) Cr = 0; else if (Cr == 0x7c00) Cr = 0x7bff;2379if (Cg > 0x7c00) Cg = 0; else if (Cg == 0x7c00) Cg = 0x7bff;2380if (Cb > 0x7c00) Cb = 0; else if (Cb == 0x7c00) Cb = 0x7bff;2381int Re = (Cr >> 10) & 0x1F;2382int Ge = (Cg >> 10) & 0x1F;2383int Be = (Cb >> 10) & 0x1F;2384int Rex = (Re == 0) ? 1 : Re;2385int Gex = (Ge == 0) ? 1 : Ge;2386int Bex = (Be == 0) ? 1 : Be;2387int Xm = ((Cr | Cg | Cb) & 0x200) >> 9;2388int Xe = Re | Ge | Be;2389uint32_t rshift, gshift, bshift, expo;23902391if (Xe == 0)2392{2393expo = rshift = gshift = bshift = Xm;2394}2395else if (Re >= Ge && Re >= Be)2396{2397expo = Rex + 1;2398rshift = 2;2399gshift = Rex - Gex + 2;2400bshift = Rex - Bex + 2;2401}2402else if (Ge >= Be)2403{2404expo = Gex + 1;2405rshift = Gex - Rex + 2;2406gshift = 2;2407bshift = Gex - Bex + 2;2408}2409else2410{2411expo = Bex + 1;2412rshift = Bex - Rex + 2;2413gshift = Bex - Gex + 2;2414bshift = 2;2415}24162417int Rm = (Cr & 0x3FF) | (Re == 0 ? 0 : 0x400);2418int Gm = (Cg & 0x3FF) | (Ge == 0 ? 0 : 0x400);2419int Bm = (Cb & 0x3FF) | (Be == 0 ? 0 : 0x400);2420Rm = (Rm >> rshift) & 0x1FF;2421Gm = (Gm >> gshift) & 0x1FF;2422Bm = (Bm >> bshift) & 0x1FF;24232424uint32_t texel = (expo << 27) | (Bm << 18) | (Gm << 9) | (Rm << 0);2425return texel;2426}24272428// Important: pPixels is either 32-bit/texel or 64-bit/texel.2429bool decode_block(const log_astc_block& log_blk, void* pPixels, uint32_t blk_width, uint32_t blk_height, decode_mode dec_mode)2430{2431assert(is_valid_block_size(blk_width, blk_height));24322433assert(g_dequant_tables.m_endpoints[0].m_ISE_to_val.size());2434if (!g_dequant_tables.m_endpoints[0].m_ISE_to_val.size())2435return false;24362437const uint32_t num_blk_pixels = blk_width * blk_height;24382439// Write block error color2440if (dec_mode == cDecodeModeHDR16)2441{2442// NaN's2443memset(pPixels, 0xFF, num_blk_pixels * sizeof(half_float) * 4);2444}2445else if (dec_mode == cDecodeModeRGB9E5)2446{2447const uint32_t purple_9e5 = pack_rgb9e5(1.0f, 0.0f, 1.0f);24482449for (uint32_t i = 0; i < num_blk_pixels; i++)2450((uint32_t*)pPixels)[i] = purple_9e5;2451}2452else2453{2454for (uint32_t i = 0; i < num_blk_pixels; i++)2455((uint32_t*)pPixels)[i] = 0xFFFF00FF;2456}24572458if (log_blk.m_error_flag)2459{2460// Should this return false? It's not an invalid logical block config, though.2461return false;2462}24632464// Handle solid color blocks2465if (log_blk.m_solid_color_flag_ldr)2466{2467// LDR solid block2468if (dec_mode == cDecodeModeHDR16)2469{2470// Convert LDR pixels to half-float2471half_float h[4];2472for (uint32_t c = 0; c < 4; c++)2473h[c] = (log_blk.m_solid_color[c] == 0xFFFF) ? 0x3C00 : float_to_half((float)log_blk.m_solid_color[c] * (1.0f / 65536.0f), true);24742475for (uint32_t i = 0; i < num_blk_pixels; i++)2476memcpy((uint16_t*)pPixels + i * 4, h, sizeof(half_float) * 4);2477}2478else if (dec_mode == cDecodeModeRGB9E5)2479{2480float r = (log_blk.m_solid_color[0] == 0xFFFF) ? 1.0f : ((float)log_blk.m_solid_color[0] * (1.0f / 65536.0f));2481float g = (log_blk.m_solid_color[1] == 0xFFFF) ? 1.0f : ((float)log_blk.m_solid_color[1] * (1.0f / 65536.0f));2482float b = (log_blk.m_solid_color[2] == 0xFFFF) ? 1.0f : ((float)log_blk.m_solid_color[2] * (1.0f / 65536.0f));24832484const uint32_t packed = pack_rgb9e5(r, g, b);24852486for (uint32_t i = 0; i < num_blk_pixels; i++)2487((uint32_t*)pPixels)[i] = packed;2488}2489else2490{2491// Convert LDR pixels to 8-bits2492for (uint32_t i = 0; i < num_blk_pixels; i++)2493for (uint32_t c = 0; c < 4; c++)2494((uint8_t*)pPixels)[i * 4 + c] = (log_blk.m_solid_color[c] >> 8);2495}24962497return true;2498}2499else if (log_blk.m_solid_color_flag_hdr)2500{2501// HDR solid block, decode mode must be half-float or RGB9E52502if (dec_mode == cDecodeModeHDR16)2503{2504for (uint32_t i = 0; i < num_blk_pixels; i++)2505memcpy((uint16_t*)pPixels + i * 4, log_blk.m_solid_color, sizeof(half_float) * 4);2506}2507else if (dec_mode == cDecodeModeRGB9E5)2508{2509float r = half_to_float(log_blk.m_solid_color[0]);2510float g = half_to_float(log_blk.m_solid_color[1]);2511float b = half_to_float(log_blk.m_solid_color[2]);25122513const uint32_t packed = pack_rgb9e5(r, g, b);25142515for (uint32_t i = 0; i < num_blk_pixels; i++)2516((uint32_t*)pPixels)[i] = packed;2517}2518else2519{2520return false;2521}25222523return true;2524}25252526// Sanity check block's config2527if ((log_blk.m_grid_width < 2) || (log_blk.m_grid_height < 2))2528return false;2529if ((log_blk.m_grid_width > blk_width) || (log_blk.m_grid_height > blk_height))2530return false;25312532if ((log_blk.m_endpoint_ise_range < FIRST_VALID_ENDPOINT_ISE_RANGE) || (log_blk.m_endpoint_ise_range > LAST_VALID_ENDPOINT_ISE_RANGE))2533return false;2534if ((log_blk.m_weight_ise_range < FIRST_VALID_WEIGHT_ISE_RANGE) || (log_blk.m_weight_ise_range > LAST_VALID_WEIGHT_ISE_RANGE))2535return false;2536if ((log_blk.m_num_partitions < 1) || (log_blk.m_num_partitions > MAX_PARTITIONS))2537return false;2538if ((log_blk.m_dual_plane) && (log_blk.m_num_partitions > MAX_DUAL_PLANE_PARTITIONS))2539return false;2540if (log_blk.m_partition_id >= NUM_PARTITION_PATTERNS)2541return false;2542if ((log_blk.m_num_partitions == 1) && (log_blk.m_partition_id > 0))2543return false;2544if (log_blk.m_color_component_selector > 3)2545return false;25462547const uint32_t total_endpoint_levels = get_ise_levels(log_blk.m_endpoint_ise_range);2548const uint32_t total_weight_levels = get_ise_levels(log_blk.m_weight_ise_range);25492550bool is_ldr_endpoints[MAX_PARTITIONS];25512552// Check CEM's2553uint32_t total_cem_vals = 0;2554for (uint32_t i = 0; i < log_blk.m_num_partitions; i++)2555{2556if (log_blk.m_color_endpoint_modes[i] > 15)2557return false;25582559total_cem_vals += get_num_cem_values(log_blk.m_color_endpoint_modes[i]);25602561is_ldr_endpoints[i] = is_cem_ldr(log_blk.m_color_endpoint_modes[i]);2562}25632564if (total_cem_vals > MAX_ENDPOINTS)2565return false;25662567const dequant_table& endpoint_dequant_tab = g_dequant_tables.get_endpoint_tab(log_blk.m_endpoint_ise_range);2568const uint8_t* pEndpoint_dequant = endpoint_dequant_tab.m_ISE_to_val.data();25692570// Dequantized endpoints to [0,255]2571uint8_t dequantized_endpoints[MAX_ENDPOINTS];2572for (uint32_t i = 0; i < total_cem_vals; i++)2573{2574if (log_blk.m_endpoints[i] >= total_endpoint_levels)2575return false;2576dequantized_endpoints[i] = pEndpoint_dequant[log_blk.m_endpoints[i]];2577}25782579// Dequantize weights to [0,64]2580uint8_t dequantized_weights[2][12 * 12];25812582const dequant_table& weight_dequant_tab = g_dequant_tables.get_weight_tab(log_blk.m_weight_ise_range);2583const uint8_t* pWeight_dequant = weight_dequant_tab.m_ISE_to_val.data();25842585const uint32_t total_weight_vals = (log_blk.m_dual_plane ? 2 : 1) * log_blk.m_grid_width * log_blk.m_grid_height;2586for (uint32_t i = 0; i < total_weight_vals; i++)2587{2588if (log_blk.m_weights[i] >= total_weight_levels)2589return false;25902591const uint32_t plane_index = log_blk.m_dual_plane ? (i & 1) : 0;2592const uint32_t grid_index = log_blk.m_dual_plane ? (i >> 1) : i;25932594dequantized_weights[plane_index][grid_index] = pWeight_dequant[log_blk.m_weights[i]];2595}25962597// Upsample weight grid. [0,64] weights2598uint8_t upsampled_weights[2][12 * 12];25992600upsample_weight_grid(blk_width, blk_height, log_blk.m_grid_width, log_blk.m_grid_height, &dequantized_weights[0][0], &upsampled_weights[0][0]);2601if (log_blk.m_dual_plane)2602upsample_weight_grid(blk_width, blk_height, log_blk.m_grid_width, log_blk.m_grid_height, &dequantized_weights[1][0], &upsampled_weights[1][0]);26032604// Decode CEM's2605int endpoints[4][4][2]; // [subset][comp][l/h]26062607uint32_t endpoint_val_index = 0;2608for (uint32_t subset = 0; subset < log_blk.m_num_partitions; subset++)2609{2610const uint32_t cem_index = log_blk.m_color_endpoint_modes[subset];26112612decode_endpoint(cem_index, &endpoints[subset][0], &dequantized_endpoints[endpoint_val_index]);26132614endpoint_val_index += get_num_cem_values(cem_index);2615}26162617// Decode texels2618const bool small_block = num_blk_pixels < 31;2619const bool use_precomputed_texel_partitions_4x4 = (blk_width == 4) && (blk_height == 4) && (log_blk.m_num_partitions >= 2) && (log_blk.m_num_partitions <= 3);2620const bool use_precomputed_texel_partitions_6x6 = (blk_width == 6) && (blk_height == 6) && (log_blk.m_num_partitions >= 2) && (log_blk.m_num_partitions <= 3);2621const uint32_t ccs = log_blk.m_dual_plane ? log_blk.m_color_component_selector : UINT32_MAX;26222623bool success = true;26242625if (dec_mode == cDecodeModeRGB9E5)2626{2627// returns uint32_t's2628for (uint32_t y = 0; y < blk_height; y++)2629{2630for (uint32_t x = 0; x < blk_width; x++)2631{2632const uint32_t pixel_index = x + y * blk_width;26332634uint32_t subset = 0;2635if (log_blk.m_num_partitions > 1)2636{2637if (use_precomputed_texel_partitions_4x4)2638subset = get_precompute_texel_partitions_4x4(log_blk.m_partition_id, x, y, log_blk.m_num_partitions);2639else if (use_precomputed_texel_partitions_6x6)2640subset = get_precompute_texel_partitions_6x6(log_blk.m_partition_id, x, y, log_blk.m_num_partitions);2641else2642subset = compute_texel_partition(log_blk.m_partition_id, x, y, 0, log_blk.m_num_partitions, small_block);2643}26442645int comp[3];26462647for (uint32_t c = 0; c < 3; c++)2648{2649const uint32_t w = upsampled_weights[(c == ccs) ? 1 : 0][pixel_index];26502651if (is_ldr_endpoints[subset])2652{2653assert((endpoints[subset][c][0] >= 0) && (endpoints[subset][c][0] <= 0xFF));2654assert((endpoints[subset][c][1] >= 0) && (endpoints[subset][c][1] <= 0xFF));26552656int le = endpoints[subset][c][0];2657int he = endpoints[subset][c][1];26582659le = (le << 8) | le;2660he = (he << 8) | he;26612662int k = weight_interpolate(le, he, w);2663assert((k >= 0) && (k <= 0xFFFF));26642665comp[c] = k; // 1.02666}2667else2668{2669assert((endpoints[subset][c][0] >= 0) && (endpoints[subset][c][0] <= 0xFFF));2670assert((endpoints[subset][c][1] >= 0) && (endpoints[subset][c][1] <= 0xFFF));26712672int le = endpoints[subset][c][0] << 4;2673int he = endpoints[subset][c][1] << 4;26742675int qlog16 = weight_interpolate(le, he, w);26762677comp[c] = qlog16_to_half(qlog16);26782679if (is_half_inf_or_nan((half_float)comp[c]))2680comp[c] = 0x7BFF;2681}26822683} // c26842685uint32_t packed;2686if (is_ldr_endpoints[subset])2687packed = pack_rgb9e5_ldr_astc(comp[0], comp[1], comp[2]);2688else2689packed = pack_rgb9e5_hdr_astc(comp[0], comp[1], comp[2]);26902691((uint32_t*)pPixels)[pixel_index] = packed;26922693} // x2694} // y2695}2696else if (dec_mode == cDecodeModeHDR16)2697{2698// Note: must round towards zero when converting float to half for ASTC (18.19 Weight Application)26992700// returns half floats2701for (uint32_t y = 0; y < blk_height; y++)2702{2703for (uint32_t x = 0; x < blk_width; x++)2704{2705const uint32_t pixel_index = x + y * blk_width;27062707uint32_t subset = 0;2708if (log_blk.m_num_partitions > 1)2709{2710if (use_precomputed_texel_partitions_4x4)2711subset = get_precompute_texel_partitions_4x4(log_blk.m_partition_id, x, y, log_blk.m_num_partitions);2712else if (use_precomputed_texel_partitions_6x6)2713subset = get_precompute_texel_partitions_6x6(log_blk.m_partition_id, x, y, log_blk.m_num_partitions);2714else2715subset = compute_texel_partition(log_blk.m_partition_id, x, y, 0, log_blk.m_num_partitions, small_block);2716}27172718for (uint32_t c = 0; c < 4; c++)2719{2720const uint32_t w = upsampled_weights[(c == ccs) ? 1 : 0][pixel_index];27212722half_float o;27232724if ( (is_ldr_endpoints[subset]) ||2725((log_blk.m_color_endpoint_modes[subset] == CEM_HDR_RGB_LDR_ALPHA) && (c == 3)) )2726{2727assert((endpoints[subset][c][0] >= 0) && (endpoints[subset][c][0] <= 0xFF));2728assert((endpoints[subset][c][1] >= 0) && (endpoints[subset][c][1] <= 0xFF));27292730int le = endpoints[subset][c][0];2731int he = endpoints[subset][c][1];27322733le = (le << 8) | le;2734he = (he << 8) | he;27352736int k = weight_interpolate(le, he, w);2737assert((k >= 0) && (k <= 0xFFFF));27382739if (k == 0xFFFF)2740o = 0x3C00; // 1.02741else2742o = float_to_half((float)k * (1.0f / 65536.0f), true);2743}2744else2745{2746assert((endpoints[subset][c][0] >= 0) && (endpoints[subset][c][0] <= 0xFFF));2747assert((endpoints[subset][c][1] >= 0) && (endpoints[subset][c][1] <= 0xFFF));27482749int le = endpoints[subset][c][0] << 4;2750int he = endpoints[subset][c][1] << 4;27512752int qlog16 = weight_interpolate(le, he, w);27532754o = qlog16_to_half(qlog16);27552756if (is_half_inf_or_nan(o))2757o = 0x7BFF;2758}27592760((half_float*)pPixels)[pixel_index * 4 + c] = o;2761}27622763} // x2764} // y2765}2766else2767{2768// returns uint8_t's2769for (uint32_t y = 0; y < blk_height; y++)2770{2771for (uint32_t x = 0; x < blk_width; x++)2772{2773const uint32_t pixel_index = x + y * blk_width;27742775uint32_t subset = 0;2776if (log_blk.m_num_partitions > 1)2777{2778if (use_precomputed_texel_partitions_4x4)2779subset = get_precompute_texel_partitions_4x4(log_blk.m_partition_id, x, y, log_blk.m_num_partitions);2780else if (use_precomputed_texel_partitions_6x6)2781subset = get_precompute_texel_partitions_6x6(log_blk.m_partition_id, x, y, log_blk.m_num_partitions);2782else2783subset = compute_texel_partition(log_blk.m_partition_id, x, y, 0, log_blk.m_num_partitions, small_block);2784}27852786if (!is_ldr_endpoints[subset])2787{2788((uint32_t*)pPixels)[pixel_index * 4] = 0xFFFF00FF;2789success = false;2790}2791else2792{2793for (uint32_t c = 0; c < 4; c++)2794{2795const uint32_t w = upsampled_weights[(c == ccs) ? 1 : 0][pixel_index];27962797int le = endpoints[subset][c][0];2798int he = endpoints[subset][c][1];27992800// FIXME: the spec is apparently wrong? this matches ARM's and Google's decoder2801//if ((dec_mode == cDecodeModeSRGB8) && (c <= 2))2802// See https://github.com/ARM-software/astc-encoder/issues/4472803if (dec_mode == cDecodeModeSRGB8)2804{2805le = (le << 8) | 0x80;2806he = (he << 8) | 0x80;2807}2808else2809{2810le = (le << 8) | le;2811he = (he << 8) | he;2812}28132814uint32_t k = weight_interpolate(le, he, w);28152816// FIXME: This is what the spec says to do in LDR mode, but this is not what ARM's decoder does2817// See decompress_symbolic_block(), decode_texel() and unorm16_to_sf16.2818// It seems to effectively divide by 65535.0 and convert to FP16, then back to float, mul by 255.0, add .5 and then convert to 8-bit.2819((uint8_t*)pPixels)[pixel_index * 4 + c] = (uint8_t)(k >> 8);2820}2821}28222823} // x2824} // y2825}28262827return success;2828}28292830//------------------------------------------------2831// Physical to logical block decoding28322833// unsigned 128-bit int, with some signed helpers2834class uint1282835{2836uint64_t m_lo, m_hi;28372838public:2839uint128() = default;2840inline uint128(uint64_t lo) : m_lo(lo), m_hi(0) { }2841inline uint128(uint64_t lo, uint64_t hi) : m_lo(lo), m_hi(hi) { }2842inline uint128(const uint128& other) : m_lo(other.m_lo), m_hi(other.m_hi) { }28432844inline uint128& set_signed(int64_t lo) { m_lo = lo; m_hi = (lo < 0) ? UINT64_MAX : 0; return *this; }2845inline uint128& set(uint64_t lo) { m_lo = lo; m_hi = 0; return *this; }28462847inline explicit operator uint8_t () const { return (uint8_t)m_lo; }2848inline explicit operator uint16_t () const { return (uint16_t)m_lo; }2849inline explicit operator uint32_t () const { return (uint32_t)m_lo; }2850inline explicit operator uint64_t () const { return m_lo; }28512852inline uint128& operator= (const uint128& rhs) { m_lo = rhs.m_lo; m_hi = rhs.m_hi; return *this; }2853inline uint128& operator= (const uint64_t val) { m_lo = val; m_hi = 0; return *this; }28542855inline uint64_t get_low() const { return m_lo; }2856inline uint64_t& get_low() { return m_lo; }28572858inline uint64_t get_high() const { return m_hi; }2859inline uint64_t& get_high() { return m_hi; }28602861inline bool operator== (const uint128& rhs) const { return (m_lo == rhs.m_lo) && (m_hi == rhs.m_hi); }2862inline bool operator!= (const uint128& rhs) const { return (m_lo != rhs.m_lo) || (m_hi != rhs.m_hi); }28632864inline bool operator< (const uint128& rhs) const2865{2866if (m_hi < rhs.m_hi)2867return true;28682869if (m_hi == rhs.m_hi)2870{2871if (m_lo < rhs.m_lo)2872return true;2873}28742875return false;2876}28772878inline bool operator> (const uint128& rhs) const { return (rhs < *this); }28792880inline bool operator<= (const uint128& rhs) const { return (*this == rhs) || (*this < rhs); }2881inline bool operator>= (const uint128& rhs) const { return (*this == rhs) || (*this > rhs); }28822883inline bool is_zero() const { return (m_lo == 0) && (m_hi == 0); }2884inline bool is_all_ones() const { return (m_lo == UINT64_MAX) && (m_hi == UINT64_MAX); }2885inline bool is_non_zero() const { return (m_lo != 0) || (m_hi != 0); }2886inline explicit operator bool() const { return is_non_zero(); }2887inline bool is_signed() const { return ((int64_t)m_hi) < 0; }28882889inline bool signed_less(const uint128& rhs) const2890{2891const bool l_signed = is_signed(), r_signed = rhs.is_signed();28922893if (l_signed == r_signed)2894return *this < rhs;28952896if (l_signed && !r_signed)2897return true;28982899assert(!l_signed && r_signed);2900return false;2901}29022903inline bool signed_greater(const uint128& rhs) const { return rhs.signed_less(*this); }2904inline bool signed_less_equal(const uint128& rhs) const { return !rhs.signed_less(*this); }2905inline bool signed_greater_equal(const uint128& rhs) const { return !signed_less(rhs); }29062907double get_double() const2908{2909double res = 0;29102911if (m_hi)2912res = (double)m_hi * pow(2.0f, 64.0f);29132914res += (double)m_lo;29152916return res;2917}29182919double get_signed_double() const2920{2921if (is_signed())2922return -(uint128(*this).abs().get_double());2923else2924return get_double();2925}29262927inline uint128 abs() const2928{2929uint128 res(*this);2930if (res.is_signed())2931res = -res;2932return res;2933}29342935inline uint128& operator<<= (int shift)2936{2937assert(shift >= 0);2938if (shift < 0)2939return *this;29402941m_hi = (shift >= 64) ? ((shift >= 128) ? 0 : (m_lo << (shift - 64))) : (m_hi << shift);29422943if ((shift) && (shift < 64))2944m_hi |= (m_lo >> (64 - shift));29452946m_lo = (shift >= 64) ? 0 : (m_lo << shift);29472948return *this;2949}29502951inline uint128 operator<< (int shift) const { uint128 res(*this); res <<= shift; return res; }29522953inline uint128& operator>>= (int shift)2954{2955assert(shift >= 0);2956if (shift < 0)2957return *this;29582959m_lo = (shift >= 64) ? ((shift >= 128) ? 0 : (m_hi >> (shift - 64))) : (m_lo >> shift);29602961if ((shift) && (shift < 64))2962m_lo |= (m_hi << (64 - shift));29632964m_hi = (shift >= 64) ? 0 : (m_hi >> shift);29652966return *this;2967}29682969inline uint128 operator>> (int shift) const { uint128 res(*this); res >>= shift; return res; }29702971inline uint128 signed_shift_right(int shift) const2972{2973uint128 res(*this);2974res >>= shift;29752976if (is_signed())2977{2978uint128 x(0U);2979x = ~x;2980x >>= shift;2981res |= (~x);2982}29832984return res;2985}29862987inline uint128& operator |= (const uint128& rhs) { m_lo |= rhs.m_lo; m_hi |= rhs.m_hi; return *this; }2988inline uint128 operator | (const uint128& rhs) const { uint128 res(*this); res |= rhs; return res; }29892990inline uint128& operator &= (const uint128& rhs) { m_lo &= rhs.m_lo; m_hi &= rhs.m_hi; return *this; }2991inline uint128 operator & (const uint128& rhs) const { uint128 res(*this); res &= rhs; return res; }29922993inline uint128& operator ^= (const uint128& rhs) { m_lo ^= rhs.m_lo; m_hi ^= rhs.m_hi; return *this; }2994inline uint128 operator ^ (const uint128& rhs) const { uint128 res(*this); res ^= rhs; return res; }29952996inline uint128 operator ~() const { return uint128(~m_lo, ~m_hi); }29972998inline uint128 operator -() const { uint128 res(~*this); if (++res.m_lo == 0) ++res.m_hi; return res; }29993000// prefix3001inline uint128 operator ++()3002{3003if (++m_lo == 0)3004++m_hi;3005return *this;3006}30073008// postfix3009inline uint128 operator ++(int)3010{3011uint128 res(*this);3012if (++m_lo == 0)3013++m_hi;3014return res;3015}30163017// prefix3018inline uint128 operator --()3019{3020const uint64_t t = m_lo;3021if (--m_lo > t)3022--m_hi;3023return *this;3024}30253026// postfix3027inline uint128 operator --(int)3028{3029const uint64_t t = m_lo;3030uint128 res(*this);3031if (--m_lo > t)3032--m_hi;3033return res;3034}30353036inline uint128& operator+= (const uint128& rhs)3037{3038const uint64_t t = m_lo + rhs.m_lo;3039m_hi = m_hi + rhs.m_hi + (t < m_lo);3040m_lo = t;3041return *this;3042}30433044inline uint128 operator+ (const uint128& rhs) const { uint128 res(*this); res += rhs; return res; }30453046inline uint128& operator-= (const uint128& rhs)3047{3048const uint64_t t = m_lo - rhs.m_lo;3049m_hi = m_hi - rhs.m_hi - (t > m_lo);3050m_lo = t;3051return *this;3052}30533054inline uint128 operator- (const uint128& rhs) const { uint128 res(*this); res -= rhs; return res; }30553056// computes bit by bit, very slow3057uint128& operator*=(const uint128& rhs)3058{3059uint128 temp(*this), result(0U);30603061for (uint128 bitmask(rhs); bitmask; bitmask >>= 1, temp <<= 1)3062if (bitmask.get_low() & 1)3063result += temp;30643065*this = result;3066return *this;3067}30683069uint128 operator*(const uint128& rhs) const { uint128 res(*this); res *= rhs; return res; }30703071// computes bit by bit, very slow3072friend uint128 divide(const uint128& dividend, const uint128& divisor, uint128& remainder)3073{3074remainder = 0;30753076if (!divisor)3077{3078assert(0);3079return ~uint128(0U);3080}30813082uint128 quotient(0), one(1);30833084for (int i = 127; i >= 0; i--)3085{3086remainder = (remainder << 1) | ((dividend >> i) & one);3087if (remainder >= divisor)3088{3089remainder -= divisor;3090quotient |= (one << i);3091}3092}30933094return quotient;3095}30963097uint128 operator/(const uint128& rhs) const { uint128 remainder, res; res = divide(*this, rhs, remainder); return res; }3098uint128 operator/=(const uint128& rhs) { uint128 remainder; *this = divide(*this, rhs, remainder); return *this; }30993100uint128 operator%(const uint128& rhs) const { uint128 remainder; divide(*this, rhs, remainder); return remainder; }3101uint128 operator%=(const uint128& rhs) { uint128 remainder; divide(*this, rhs, remainder); *this = remainder; return *this; }31023103void print_hex(FILE* pFile) const3104{3105fprintf(pFile, "0x%016llx%016llx", (unsigned long long int)m_hi, (unsigned long long int)m_lo);3106}31073108void format_unsigned(std::string& res) const3109{3110basisu::vector<uint8_t> digits;3111digits.reserve(39 + 1);31123113uint128 k(*this), ten(10);3114do3115{3116uint128 r;3117k = divide(k, ten, r);3118digits.push_back((uint8_t)r);3119} while (k);31203121for (int i = (int)digits.size() - 1; i >= 0; i--)3122res += ('0' + digits[i]);3123}31243125void format_signed(std::string& res) const3126{3127uint128 val(*this);31283129if (val.is_signed())3130{3131res.push_back('-');3132val = -val;3133}31343135val.format_unsigned(res);3136}31373138void print_unsigned(FILE* pFile)3139{3140std::string str;3141format_unsigned(str);3142fprintf(pFile, "%s", str.c_str());3143}31443145void print_signed(FILE* pFile)3146{3147std::string str;3148format_signed(str);3149fprintf(pFile, "%s", str.c_str());3150}31513152uint128 get_reversed_bits() const3153{3154uint128 res;31553156const uint32_t* pSrc = (const uint32_t*)this;3157uint32_t* pDst = (uint32_t*)&res;31583159pDst[0] = rev_dword(pSrc[3]);3160pDst[1] = rev_dword(pSrc[2]);3161pDst[2] = rev_dword(pSrc[1]);3162pDst[3] = rev_dword(pSrc[0]);31633164return res;3165}31663167uint128 get_byteswapped() const3168{3169uint128 res;31703171const uint8_t* pSrc = (const uint8_t*)this;3172uint8_t* pDst = (uint8_t*)&res;31733174for (uint32_t i = 0; i < 16; i++)3175pDst[i] = pSrc[15 - i];31763177return res;3178}31793180inline uint64_t get_bits64(uint32_t bit_ofs, uint32_t bit_len) const3181{3182assert(bit_ofs < 128);3183assert(bit_len && (bit_len <= 64) && ((bit_ofs + bit_len) <= 128));31843185uint128 res(*this);3186res >>= bit_ofs;31873188const uint64_t bitmask = (bit_len == 64) ? UINT64_MAX : ((1ull << bit_len) - 1);3189return res.get_low() & bitmask;3190}31913192inline uint32_t get_bits(uint32_t bit_ofs, uint32_t bit_len) const3193{3194assert(bit_len <= 32);3195return (uint32_t)get_bits64(bit_ofs, bit_len);3196}31973198inline uint32_t next_bits(uint32_t& bit_ofs, uint32_t len) const3199{3200assert(len && (len <= 32));3201uint32_t x = get_bits(bit_ofs, len);3202bit_ofs += len;3203return x;3204}32053206inline uint128& set_bits(uint64_t val, uint32_t bit_ofs, uint32_t num_bits)3207{3208assert(bit_ofs < 128);3209assert(num_bits && (num_bits <= 64) && ((bit_ofs + num_bits) <= 128));32103211uint128 bitmask(1);3212bitmask = (bitmask << num_bits) - 1;3213assert(uint128(val) <= bitmask);32143215bitmask <<= bit_ofs;3216*this &= ~bitmask;32173218*this = *this | (uint128(val) << bit_ofs);3219return *this;3220}3221};32223223static bool decode_void_extent(const uint128& bits, log_astc_block& log_blk)3224{3225if (bits.get_bits(10, 2) != 0b11)3226return false;32273228uint32_t bit_ofs = 12;3229const uint32_t min_s = bits.next_bits(bit_ofs, 13);3230const uint32_t max_s = bits.next_bits(bit_ofs, 13);3231const uint32_t min_t = bits.next_bits(bit_ofs, 13);3232const uint32_t max_t = bits.next_bits(bit_ofs, 13);3233assert(bit_ofs == 64);32343235const bool all_extents_all_ones = (min_s == 0x1FFF) && (max_s == 0x1FFF) && (min_t == 0x1FFF) && (max_t == 0x1FFF);32363237if (!all_extents_all_ones && ((min_s >= max_s) || (min_t >= max_t)))3238return false;32393240const bool hdr_flag = bits.get_bits(9, 1) != 0;32413242if (hdr_flag)3243log_blk.m_solid_color_flag_hdr = true;3244else3245log_blk.m_solid_color_flag_ldr = true;32463247log_blk.m_solid_color[0] = (uint16_t)bits.get_bits(64, 16);3248log_blk.m_solid_color[1] = (uint16_t)bits.get_bits(80, 16);3249log_blk.m_solid_color[2] = (uint16_t)bits.get_bits(96, 16);3250log_blk.m_solid_color[3] = (uint16_t)bits.get_bits(112, 16);32513252if (log_blk.m_solid_color_flag_hdr)3253{3254for (uint32_t c = 0; c < 4; c++)3255if (is_half_inf_or_nan(log_blk.m_solid_color[c]))3256return false;3257}32583259return true;3260}32613262struct astc_dec_row3263{3264int8_t Dp_ofs, P_ofs, W_ofs, W_size, H_ofs, H_size, W_bias, H_bias, p0_ofs, p1_ofs, p2_ofs;3265};32663267static const astc_dec_row s_dec_rows[10] =3268{3269// Dp_ofs, P_ofs, W_ofs, W_size, H_ofs, H_size, W_bias, H_bias, p0_ofs, p1_ofs, p2_ofs;3270{ 10, 9, 7, 2, 5, 2, 4, 2, 4, 0, 1 }, // 4 23271{ 10, 9, 7, 2, 5, 2, 8, 2, 4, 0, 1 }, // 8 23272{ 10, 9, 5, 2, 7, 2, 2, 8, 4, 0, 1 }, // 2 83273{ 10, 9, 5, 2, 7, 1, 2, 6, 4, 0, 1 }, // 2 632743275{ 10, 9, 7, 1, 5, 2, 2, 2, 4, 0, 1 }, // 2 23276{ 10, 9, 0, 0, 5, 2, 12, 2, 4, 2, 3 }, // 12 23277{ 10, 9, 5, 2, 0, 0, 2, 12, 4, 2, 3 }, // 2 123278{ 10, 9, 0, 0, 0, 0, 6, 10, 4, 2, 3 }, // 6 1032793280{ 10, 9, 0, 0, 0, 0, 10, 6, 4, 2, 3 }, // 10 63281{ -1, -1, 5, 2, 9, 2, 6, 6, 4, 2, 3 }, // 6 63282};32833284static bool decode_config(const uint128& bits, log_astc_block& log_blk)3285{3286// Reserved3287if (bits.get_bits(0, 4) == 0)3288return false;32893290// Reserved3291if ((bits.get_bits(0, 2) == 0) && (bits.get_bits(6, 3) == 0b111))3292{3293if (bits.get_bits(2, 4) != 0b1111)3294return false;3295}32963297// Void extent3298if (bits.get_bits(0, 9) == 0b111111100)3299return decode_void_extent(bits, log_blk);33003301// Check rows3302const uint32_t x0_2 = bits.get_bits(0, 2), x2_2 = bits.get_bits(2, 2);3303const uint32_t x5_4 = bits.get_bits(5, 4), x8_1 = bits.get_bits(8, 1);3304const uint32_t x7_2 = bits.get_bits(7, 2);33053306int row_index = -1;3307if (x0_2 == 0)3308{3309if (x7_2 == 0b00)3310row_index = 5;3311else if (x7_2 == 0b01)3312row_index = 6;3313else if (x5_4 == 0b1100)3314row_index = 7;3315else if (x5_4 == 0b1101)3316row_index = 8;3317else if (x7_2 == 0b10)3318row_index = 9;3319}3320else3321{3322if (x2_2 == 0b00)3323row_index = 0;3324else if (x2_2 == 0b01)3325row_index = 1;3326else if (x2_2 == 0b10)3327row_index = 2;3328else if ((x2_2 == 0b11) && (x8_1 == 0))3329row_index = 3;3330else if ((x2_2 == 0b11) && (x8_1 == 1))3331row_index = 4;3332}3333if (row_index < 0)3334return false;33353336const astc_dec_row& r = s_dec_rows[row_index];33373338bool P = false, Dp = false;3339uint32_t W = r.W_bias, H = r.H_bias;33403341if (r.P_ofs >= 0)3342P = bits.get_bits(r.P_ofs, 1) != 0;33433344if (r.Dp_ofs >= 0)3345Dp = bits.get_bits(r.Dp_ofs, 1) != 0;33463347if (r.W_size)3348W += bits.get_bits(r.W_ofs, r.W_size);33493350if (r.H_size)3351H += bits.get_bits(r.H_ofs, r.H_size);33523353assert((W >= MIN_GRID_DIM) && (W <= MAX_BLOCK_DIM));3354assert((H >= MIN_GRID_DIM) && (H <= MAX_BLOCK_DIM));33553356int p0 = bits.get_bits(r.p0_ofs, 1);3357int p1 = bits.get_bits(r.p1_ofs, 1);3358int p2 = bits.get_bits(r.p2_ofs, 1);33593360uint32_t p = p0 | (p1 << 1) | (p2 << 2);3361if (p < 2)3362return false;33633364log_blk.m_grid_width = (uint8_t)W;3365log_blk.m_grid_height = (uint8_t)H;33663367log_blk.m_weight_ise_range = (uint8_t)((p - 2) + (P * BISE_10_LEVELS));3368assert(log_blk.m_weight_ise_range <= LAST_VALID_WEIGHT_ISE_RANGE);33693370log_blk.m_dual_plane = Dp;33713372return true;3373}33743375static inline uint32_t read_le_dword(const uint8_t* pBytes)3376{3377return (pBytes[0]) | (pBytes[1] << 8U) | (pBytes[2] << 16U) | (pBytes[3] << 24U);3378}33793380// See 18.12.Integer Sequence Encoding - tables computed by executing the decoder functions with all possible 8/7-bit inputs.3381static const uint8_t s_trit_decode[256][5] =3382{3383{0,0,0,0,0},{1,0,0,0,0},{2,0,0,0,0},{0,0,2,0,0},{0,1,0,0,0},{1,1,0,0,0},{2,1,0,0,0},{1,0,2,0,0},3384{0,2,0,0,0},{1,2,0,0,0},{2,2,0,0,0},{2,0,2,0,0},{0,2,2,0,0},{1,2,2,0,0},{2,2,2,0,0},{2,0,2,0,0},3385{0,0,1,0,0},{1,0,1,0,0},{2,0,1,0,0},{0,1,2,0,0},{0,1,1,0,0},{1,1,1,0,0},{2,1,1,0,0},{1,1,2,0,0},3386{0,2,1,0,0},{1,2,1,0,0},{2,2,1,0,0},{2,1,2,0,0},{0,0,0,2,2},{1,0,0,2,2},{2,0,0,2,2},{0,0,2,2,2},3387{0,0,0,1,0},{1,0,0,1,0},{2,0,0,1,0},{0,0,2,1,0},{0,1,0,1,0},{1,1,0,1,0},{2,1,0,1,0},{1,0,2,1,0},3388{0,2,0,1,0},{1,2,0,1,0},{2,2,0,1,0},{2,0,2,1,0},{0,2,2,1,0},{1,2,2,1,0},{2,2,2,1,0},{2,0,2,1,0},3389{0,0,1,1,0},{1,0,1,1,0},{2,0,1,1,0},{0,1,2,1,0},{0,1,1,1,0},{1,1,1,1,0},{2,1,1,1,0},{1,1,2,1,0},3390{0,2,1,1,0},{1,2,1,1,0},{2,2,1,1,0},{2,1,2,1,0},{0,1,0,2,2},{1,1,0,2,2},{2,1,0,2,2},{1,0,2,2,2},3391{0,0,0,2,0},{1,0,0,2,0},{2,0,0,2,0},{0,0,2,2,0},{0,1,0,2,0},{1,1,0,2,0},{2,1,0,2,0},{1,0,2,2,0},3392{0,2,0,2,0},{1,2,0,2,0},{2,2,0,2,0},{2,0,2,2,0},{0,2,2,2,0},{1,2,2,2,0},{2,2,2,2,0},{2,0,2,2,0},3393{0,0,1,2,0},{1,0,1,2,0},{2,0,1,2,0},{0,1,2,2,0},{0,1,1,2,0},{1,1,1,2,0},{2,1,1,2,0},{1,1,2,2,0},3394{0,2,1,2,0},{1,2,1,2,0},{2,2,1,2,0},{2,1,2,2,0},{0,2,0,2,2},{1,2,0,2,2},{2,2,0,2,2},{2,0,2,2,2},3395{0,0,0,0,2},{1,0,0,0,2},{2,0,0,0,2},{0,0,2,0,2},{0,1,0,0,2},{1,1,0,0,2},{2,1,0,0,2},{1,0,2,0,2},3396{0,2,0,0,2},{1,2,0,0,2},{2,2,0,0,2},{2,0,2,0,2},{0,2,2,0,2},{1,2,2,0,2},{2,2,2,0,2},{2,0,2,0,2},3397{0,0,1,0,2},{1,0,1,0,2},{2,0,1,0,2},{0,1,2,0,2},{0,1,1,0,2},{1,1,1,0,2},{2,1,1,0,2},{1,1,2,0,2},3398{0,2,1,0,2},{1,2,1,0,2},{2,2,1,0,2},{2,1,2,0,2},{0,2,2,2,2},{1,2,2,2,2},{2,2,2,2,2},{2,0,2,2,2},3399{0,0,0,0,1},{1,0,0,0,1},{2,0,0,0,1},{0,0,2,0,1},{0,1,0,0,1},{1,1,0,0,1},{2,1,0,0,1},{1,0,2,0,1},3400{0,2,0,0,1},{1,2,0,0,1},{2,2,0,0,1},{2,0,2,0,1},{0,2,2,0,1},{1,2,2,0,1},{2,2,2,0,1},{2,0,2,0,1},3401{0,0,1,0,1},{1,0,1,0,1},{2,0,1,0,1},{0,1,2,0,1},{0,1,1,0,1},{1,1,1,0,1},{2,1,1,0,1},{1,1,2,0,1},3402{0,2,1,0,1},{1,2,1,0,1},{2,2,1,0,1},{2,1,2,0,1},{0,0,1,2,2},{1,0,1,2,2},{2,0,1,2,2},{0,1,2,2,2},3403{0,0,0,1,1},{1,0,0,1,1},{2,0,0,1,1},{0,0,2,1,1},{0,1,0,1,1},{1,1,0,1,1},{2,1,0,1,1},{1,0,2,1,1},3404{0,2,0,1,1},{1,2,0,1,1},{2,2,0,1,1},{2,0,2,1,1},{0,2,2,1,1},{1,2,2,1,1},{2,2,2,1,1},{2,0,2,1,1},3405{0,0,1,1,1},{1,0,1,1,1},{2,0,1,1,1},{0,1,2,1,1},{0,1,1,1,1},{1,1,1,1,1},{2,1,1,1,1},{1,1,2,1,1},3406{0,2,1,1,1},{1,2,1,1,1},{2,2,1,1,1},{2,1,2,1,1},{0,1,1,2,2},{1,1,1,2,2},{2,1,1,2,2},{1,1,2,2,2},3407{0,0,0,2,1},{1,0,0,2,1},{2,0,0,2,1},{0,0,2,2,1},{0,1,0,2,1},{1,1,0,2,1},{2,1,0,2,1},{1,0,2,2,1},3408{0,2,0,2,1},{1,2,0,2,1},{2,2,0,2,1},{2,0,2,2,1},{0,2,2,2,1},{1,2,2,2,1},{2,2,2,2,1},{2,0,2,2,1},3409{0,0,1,2,1},{1,0,1,2,1},{2,0,1,2,1},{0,1,2,2,1},{0,1,1,2,1},{1,1,1,2,1},{2,1,1,2,1},{1,1,2,2,1},3410{0,2,1,2,1},{1,2,1,2,1},{2,2,1,2,1},{2,1,2,2,1},{0,2,1,2,2},{1,2,1,2,2},{2,2,1,2,2},{2,1,2,2,2},3411{0,0,0,1,2},{1,0,0,1,2},{2,0,0,1,2},{0,0,2,1,2},{0,1,0,1,2},{1,1,0,1,2},{2,1,0,1,2},{1,0,2,1,2},3412{0,2,0,1,2},{1,2,0,1,2},{2,2,0,1,2},{2,0,2,1,2},{0,2,2,1,2},{1,2,2,1,2},{2,2,2,1,2},{2,0,2,1,2},3413{0,0,1,1,2},{1,0,1,1,2},{2,0,1,1,2},{0,1,2,1,2},{0,1,1,1,2},{1,1,1,1,2},{2,1,1,1,2},{1,1,2,1,2},3414{0,2,1,1,2},{1,2,1,1,2},{2,2,1,1,2},{2,1,2,1,2},{0,2,2,2,2},{1,2,2,2,2},{2,2,2,2,2},{2,1,2,2,2}3415};34163417static const uint8_t s_quint_decode[128][3] =3418{3419{0,0,0},{1,0,0},{2,0,0},{3,0,0},{4,0,0},{0,4,0},{4,4,0},{4,4,4},3420{0,1,0},{1,1,0},{2,1,0},{3,1,0},{4,1,0},{1,4,0},{4,4,1},{4,4,4},3421{0,2,0},{1,2,0},{2,2,0},{3,2,0},{4,2,0},{2,4,0},{4,4,2},{4,4,4},3422{0,3,0},{1,3,0},{2,3,0},{3,3,0},{4,3,0},{3,4,0},{4,4,3},{4,4,4},3423{0,0,1},{1,0,1},{2,0,1},{3,0,1},{4,0,1},{0,4,1},{4,0,4},{0,4,4},3424{0,1,1},{1,1,1},{2,1,1},{3,1,1},{4,1,1},{1,4,1},{4,1,4},{1,4,4},3425{0,2,1},{1,2,1},{2,2,1},{3,2,1},{4,2,1},{2,4,1},{4,2,4},{2,4,4},3426{0,3,1},{1,3,1},{2,3,1},{3,3,1},{4,3,1},{3,4,1},{4,3,4},{3,4,4},3427{0,0,2},{1,0,2},{2,0,2},{3,0,2},{4,0,2},{0,4,2},{2,0,4},{3,0,4},3428{0,1,2},{1,1,2},{2,1,2},{3,1,2},{4,1,2},{1,4,2},{2,1,4},{3,1,4},3429{0,2,2},{1,2,2},{2,2,2},{3,2,2},{4,2,2},{2,4,2},{2,2,4},{3,2,4},3430{0,3,2},{1,3,2},{2,3,2},{3,3,2},{4,3,2},{3,4,2},{2,3,4},{3,3,4},3431{0,0,3},{1,0,3},{2,0,3},{3,0,3},{4,0,3},{0,4,3},{0,0,4},{1,0,4},3432{0,1,3},{1,1,3},{2,1,3},{3,1,3},{4,1,3},{1,4,3},{0,1,4},{1,1,4},3433{0,2,3},{1,2,3},{2,2,3},{3,2,3},{4,2,3},{2,4,3},{0,2,4},{1,2,4},3434{0,3,3},{1,3,3},{2,3,3},{3,3,3},{4,3,3},{3,4,3},{0,3,4},{1,3,4}3435};34363437static void decode_trit_block(uint8_t* pVals, uint32_t num_vals, const uint128& bits, uint32_t& bit_ofs, uint32_t bits_per_val)3438{3439assert((num_vals >= 1) && (num_vals <= 5));3440uint32_t m[5] = { 0 }, T = 0;34413442static const uint8_t s_t_bits[5] = { 2, 2, 1, 2, 1 };34433444for (uint32_t T_ofs = 0, c = 0; c < num_vals; c++)3445{3446if (bits_per_val)3447m[c] = bits.next_bits(bit_ofs, bits_per_val);3448T |= (bits.next_bits(bit_ofs, s_t_bits[c]) << T_ofs);3449T_ofs += s_t_bits[c];3450}34513452const uint8_t (&p_trits)[5] = s_trit_decode[T];34533454for (uint32_t i = 0; i < num_vals; i++)3455pVals[i] = (uint8_t)((p_trits[i] << bits_per_val) | m[i]);3456}34573458static void decode_quint_block(uint8_t* pVals, uint32_t num_vals, const uint128& bits, uint32_t& bit_ofs, uint32_t bits_per_val)3459{3460assert((num_vals >= 1) && (num_vals <= 3));3461uint32_t m[3] = { 0 }, T = 0;34623463static const uint8_t s_t_bits[3] = { 3, 2, 2 };34643465for (uint32_t T_ofs = 0, c = 0; c < num_vals; c++)3466{3467if (bits_per_val)3468m[c] = bits.next_bits(bit_ofs, bits_per_val);3469T |= (bits.next_bits(bit_ofs, s_t_bits[c]) << T_ofs);3470T_ofs += s_t_bits[c];3471}34723473const uint8_t (&p_quints)[3] = s_quint_decode[T];34743475for (uint32_t i = 0; i < num_vals; i++)3476pVals[i] = (uint8_t)((p_quints[i] << bits_per_val) | m[i]);3477}34783479static void decode_bise(uint32_t ise_range, uint8_t* pVals, uint32_t num_vals, const uint128& bits, uint32_t bit_ofs)3480{3481assert(num_vals && (ise_range < TOTAL_ISE_RANGES));34823483const uint32_t bits_per_val = g_ise_range_table[ise_range][0];34843485if (g_ise_range_table[ise_range][1])3486{3487// Trits+bits, 5 vals per block, 7 bits extra per block3488const uint32_t total_blocks = (num_vals + 4) / 5;3489for (uint32_t b = 0; b < total_blocks; b++)3490{3491const uint32_t num_vals_in_block = std::min<int>(num_vals - 5 * b, 5);3492decode_trit_block(pVals + 5 * b, num_vals_in_block, bits, bit_ofs, bits_per_val);3493}3494}3495else if (g_ise_range_table[ise_range][2])3496{3497// Quints+bits, 3 vals per block, 8 bits extra per block3498const uint32_t total_blocks = (num_vals + 2) / 3;3499for (uint32_t b = 0; b < total_blocks; b++)3500{3501const uint32_t num_vals_in_block = std::min<int>(num_vals - 3 * b, 3);3502decode_quint_block(pVals + 3 * b, num_vals_in_block, bits, bit_ofs, bits_per_val);3503}3504}3505else3506{3507assert(bits_per_val);35083509// Only bits3510for (uint32_t i = 0; i < num_vals; i++)3511pVals[i] = (uint8_t)bits.next_bits(bit_ofs, bits_per_val);3512}3513}35143515void decode_bise(uint32_t ise_range, uint8_t* pVals, uint32_t num_vals, const uint8_t* pBits128, uint32_t bit_ofs)3516{3517const uint128 bits(3518(uint64_t)read_le_dword(pBits128) | (((uint64_t)read_le_dword(pBits128 + sizeof(uint32_t))) << 32),3519(uint64_t)read_le_dword(pBits128 + sizeof(uint32_t) * 2) | (((uint64_t)read_le_dword(pBits128 + sizeof(uint32_t) * 3)) << 32));35203521return decode_bise(ise_range, pVals, num_vals, bits, bit_ofs);3522}35233524// Decodes a physical ASTC block to a logical ASTC block.3525// blk_width/blk_height are only used to validate the weight grid's dimensions.3526bool unpack_block(const void* pASTC_block, log_astc_block& log_blk, uint32_t blk_width, uint32_t blk_height)3527{3528assert(is_valid_block_size(blk_width, blk_height));35293530const uint8_t* pS = (uint8_t*)pASTC_block;35313532log_blk.clear();3533log_blk.m_error_flag = true;35343535const uint128 bits(3536(uint64_t)read_le_dword(pS) | (((uint64_t)read_le_dword(pS + sizeof(uint32_t))) << 32),3537(uint64_t)read_le_dword(pS + sizeof(uint32_t) * 2) | (((uint64_t)read_le_dword(pS + sizeof(uint32_t) * 3)) << 32));35383539const uint128 rev_bits(bits.get_reversed_bits());35403541if (!decode_config(bits, log_blk))3542return false;35433544if (log_blk.m_solid_color_flag_hdr || log_blk.m_solid_color_flag_ldr)3545{3546// Void extent3547log_blk.m_error_flag = false;3548return true;3549}35503551// Check grid dimensions3552if ((log_blk.m_grid_width > blk_width) || (log_blk.m_grid_height > blk_height))3553return false;35543555// Now we have the grid width/height, dual plane, weight ISE range35563557const uint32_t total_grid_weights = (log_blk.m_dual_plane ? 2 : 1) * (log_blk.m_grid_width * log_blk.m_grid_height);3558const uint32_t total_weight_bits = get_ise_sequence_bits(total_grid_weights, log_blk.m_weight_ise_range);35593560// 18.24 Illegal Encodings3561if ((!total_grid_weights) || (total_grid_weights > MAX_GRID_WEIGHTS) || (total_weight_bits < 24) || (total_weight_bits > 96))3562return false;35633564const uint32_t end_of_weight_bit_ofs = 128 - total_weight_bits;35653566uint32_t total_extra_bits = 0;35673568// Right before the weight bits, there may be extra CEM bits, then the 2 CCS bits if dual plane.35693570log_blk.m_num_partitions = (uint8_t)(bits.get_bits(11, 2) + 1);3571if (log_blk.m_num_partitions == 1)3572log_blk.m_color_endpoint_modes[0] = (uint8_t)(bits.get_bits(13, 4)); // read CEM bits3573else3574{3575// 2 or more partitions3576if (log_blk.m_dual_plane && (log_blk.m_num_partitions == 4))3577return false;35783579log_blk.m_partition_id = (uint16_t)bits.get_bits(13, 10);35803581uint32_t cem_bits = bits.get_bits(23, 6);35823583if ((cem_bits & 3) == 0)3584{3585// All CEM's the same3586for (uint32_t i = 0; i < log_blk.m_num_partitions; i++)3587log_blk.m_color_endpoint_modes[i] = (uint8_t)(cem_bits >> 2);3588}3589else3590{3591// CEM's different, but within up to 2 adjacent classes3592const uint32_t first_cem_index = ((cem_bits & 3) - 1) * 4;35933594total_extra_bits = 3 * log_blk.m_num_partitions - 4;35953596if ((total_weight_bits + total_extra_bits) > 128)3597return false;35983599uint32_t cem_bit_pos = end_of_weight_bit_ofs - total_extra_bits;36003601uint32_t c[4] = { 0 }, m[4] = { 0 };36023603cem_bits >>= 2;3604for (uint32_t i = 0; i < log_blk.m_num_partitions; i++, cem_bits >>= 1)3605c[i] = cem_bits & 1;36063607switch (log_blk.m_num_partitions)3608{3609case 2:3610{3611m[0] = cem_bits & 3;3612m[1] = bits.next_bits(cem_bit_pos, 2);3613break;3614}3615case 3:3616{3617m[0] = cem_bits & 1;3618m[0] |= (bits.next_bits(cem_bit_pos, 1) << 1);3619m[1] = bits.next_bits(cem_bit_pos, 2);3620m[2] = bits.next_bits(cem_bit_pos, 2);3621break;3622}3623case 4:3624{3625for (uint32_t i = 0; i < 4; i++)3626m[i] = bits.next_bits(cem_bit_pos, 2);3627break;3628}3629default:3630{3631assert(0);3632break;3633}3634}36353636assert(cem_bit_pos == end_of_weight_bit_ofs);36373638for (uint32_t i = 0; i < log_blk.m_num_partitions; i++)3639{3640log_blk.m_color_endpoint_modes[i] = (uint8_t)(first_cem_index + (c[i] * 4) + m[i]);3641assert(log_blk.m_color_endpoint_modes[i] <= 15);3642}3643}3644}36453646// Now we have all the CEM indices.36473648if (log_blk.m_dual_plane)3649{3650// Read CCS bits, beneath any CEM bits3651total_extra_bits += 2;36523653if (total_extra_bits > end_of_weight_bit_ofs)3654return false;36553656uint32_t ccs_bit_pos = end_of_weight_bit_ofs - total_extra_bits;3657log_blk.m_color_component_selector = (uint8_t)(bits.get_bits(ccs_bit_pos, 2));3658}36593660uint32_t config_bit_pos = 11 + 2; // config+num_parts3661if (log_blk.m_num_partitions == 1)3662config_bit_pos += 4; // CEM bits3663else3664config_bit_pos += 10 + 6; // part_id+CEM bits36653666// config+num_parts+total_extra_bits (CEM extra+CCS)3667uint32_t total_config_bits = config_bit_pos + total_extra_bits;36683669// Compute number of remaining bits in block3670const int num_remaining_bits = 128 - (int)total_config_bits - (int)total_weight_bits;3671if (num_remaining_bits < 0)3672return false;36733674// Compute total number of ISE encoded color endpoint mode values3675uint32_t total_cem_vals = 0;3676for (uint32_t j = 0; j < log_blk.m_num_partitions; j++)3677total_cem_vals += get_num_cem_values(log_blk.m_color_endpoint_modes[j]);36783679if (total_cem_vals > MAX_ENDPOINTS)3680return false;36813682// Infer endpoint ISE range based off the # of values we need to encode, and the # of remaining bits in the block3683int endpoint_ise_range = -1;3684for (int k = 20; k > 0; k--)3685{3686int b = get_ise_sequence_bits(total_cem_vals, k);3687if (b <= num_remaining_bits)3688{3689endpoint_ise_range = k;3690break;3691}3692}36933694// See 23.24 Illegal Encodings, [0,5] is the minimum ISE encoding for endpoints3695if (endpoint_ise_range < (int)FIRST_VALID_ENDPOINT_ISE_RANGE)3696return false;36973698log_blk.m_endpoint_ise_range = (uint8_t)endpoint_ise_range;36993700// Decode endpoints forwards in block3701decode_bise(log_blk.m_endpoint_ise_range, log_blk.m_endpoints, total_cem_vals, bits, config_bit_pos);37023703// Decode grid weights backwards in block3704decode_bise(log_blk.m_weight_ise_range, log_blk.m_weights, total_grid_weights, rev_bits, 0);37053706log_blk.m_error_flag = false;37073708return true;3709}37103711} // namespace astc_helpers37123713#endif //BASISU_ASTC_HELPERS_IMPLEMENTATION371437153716