Path: blob/master/thirdparty/basis_universal/encoder/basisu_uastc_hdr_4x4_enc.h
9903 views
// basisu_uastc_hdr_4x4_enc.h1#pragma once2#include "basisu_enc.h"3#include "basisu_gpu_texture.h"4#include "../transcoder/basisu_astc_helpers.h"5#include "../transcoder/basisu_astc_hdr_core.h"6#include "basisu_astc_hdr_common.h"78namespace basisu9{10struct uastc_hdr_4x4_codec_options : astc_hdr_codec_base_options11{12float m_bc6h_err_weight;1314bool m_use_solid;1516bool m_use_mode11_part1;17bool m_mode11_uber_mode;18uint32_t m_first_mode11_weight_ise_range;19uint32_t m_last_mode11_weight_ise_range;20bool m_mode11_direct_only;21int32_t m_first_mode11_submode;22int32_t m_last_mode11_submode;2324bool m_use_mode7_part1;25uint32_t m_first_mode7_part1_weight_ise_range;26uint32_t m_last_mode7_part1_weight_ise_range;2728bool m_use_mode7_part2;29uint32_t m_mode7_part2_part_masks;30uint32_t m_first_mode7_part2_weight_ise_range;31uint32_t m_last_mode7_part2_weight_ise_range;3233bool m_use_mode11_part2;34uint32_t m_mode11_part2_part_masks;35uint32_t m_first_mode11_part2_weight_ise_range;36uint32_t m_last_mode11_part2_weight_ise_range;3738bool m_refine_weights;3940uint32_t m_level;4142bool m_use_estimated_partitions;43uint32_t m_max_estimated_partitions;4445uastc_hdr_4x4_codec_options();4647void init();4849// TODO: set_quality_level() is preferred to configure the codec for transcoding purposes.50static const int cMinLevel = 0;51static const int cMaxLevel = 4;52static const int cDefaultLevel = 1;53void set_quality_level(int level);5455private:56void set_quality_best();57void set_quality_normal();58void set_quality_fastest();59};6061struct astc_hdr_4x4_pack_results62{63double m_best_block_error;64double m_bc6h_block_error; // note this is not used/set by the encoder, here for convienance6566// Encoder results (logical ASTC block)67astc_helpers::log_astc_block m_best_blk;6869// For statistical use70uint32_t m_best_submodes[2];71uint32_t m_best_pat_index;72bool m_constrained_weights;7374bool m_improved_via_refinement_flag;7576// Only valid if the block is solid77basist::astc_blk m_solid_blk;7879// The BC6H transcoded block80basist::bc6h_block m_bc6h_block;8182// Solid color/void extent flag83bool m_is_solid;8485void clear()86{87m_best_block_error = 1e+30f;88m_bc6h_block_error = 1e+30f;8990m_best_blk.clear();91m_best_blk.m_grid_width = 4;92m_best_blk.m_grid_height = 4;93m_best_blk.m_endpoint_ise_range = 20; // 0-2559495clear_obj(m_best_submodes);9697m_best_pat_index = 0;98m_constrained_weights = false;99100clear_obj(m_bc6h_block);101102m_is_solid = false;103m_improved_via_refinement_flag = false;104}105};106107// Encodes a 4x4 ASTC HDR block given a 4x4 array of source block pixels/texels.108// Supports solid color blocks, mode 11 (all submodes), mode 7/1 partition (all submodes),109// and mode 7/2 partitions (all submodes) - 30 patterns, only the ones also in common with the BC6H format.110// The packed ASTC weight grid dimensions are currently always 4x4 texels, but may be also 3x3 in the future.111// This function is thread safe, i.e. it may be called from multiple encoding threads simultanously with different blocks.112//113// Parameters:114// pRGBPixels - An array of 48 (16 RGB) floats: the 4x4 block to pack115// pPacked_block - A pointer to the packed ASTC HDR block116// coptions - Codec options117// pInternal_results - An optional pointer to details about how the block was packed, for statistics/debugging purposes. May be nullptr.118//119// Requirements:120// astc_hdr_enc_init() MUST have been called first to initialized the codec.121// Input pixels are checked and cannot be NaN's, Inf's, signed, or too large (greater than MAX_HALF_FLOAT, or 65504).122// Normal values and denormals are okay.123bool astc_hdr_4x4_enc_block(124const float* pRGBPixels, const basist::half_float *pRGBPixelsHalf,125const uastc_hdr_4x4_codec_options& coptions,126basisu::vector<astc_hdr_4x4_pack_results> &all_results);127128bool astc_hdr_4x4_pack_results_to_block(basist::astc_blk& dst_blk, const astc_hdr_4x4_pack_results& results);129130bool astc_hdr_4x4_refine_weights(const basist::half_float* pSource_block, astc_hdr_4x4_pack_results& cur_results, const uastc_hdr_4x4_codec_options& coptions, float bc6h_weight, bool* pImproved_flag);131132struct astc_hdr_4x4_block_stats133{134std::mutex m_mutex;135136uint32_t m_total_blocks;137uint32_t m_total_2part, m_total_solid;138uint32_t m_total_mode7_1part, m_total_mode7_2part;139uint32_t m_total_mode11_1part, m_total_mode11_2part;140uint32_t m_total_mode11_1part_constrained_weights;141142uint32_t m_weight_range_hist_7[11];143uint32_t m_weight_range_hist_7_2part[11];144uint32_t m_mode7_submode_hist[6];145146uint32_t m_weight_range_hist_11[11];147uint32_t m_weight_range_hist_11_2part[11];148uint32_t m_mode11_submode_hist[9];149150uint32_t m_part_hist[32];151152uint32_t m_total_refined;153154astc_hdr_4x4_block_stats() { clear(); }155156void clear()157{158std::lock_guard<std::mutex> lck(m_mutex);159160m_total_blocks = 0;161m_total_mode7_1part = 0, m_total_mode7_2part = 0, m_total_mode11_1part = 0, m_total_2part = 0, m_total_solid = 0, m_total_mode11_2part = 0;162m_total_mode11_1part_constrained_weights = 0;163m_total_refined = 0;164165clear_obj(m_weight_range_hist_11);166clear_obj(m_weight_range_hist_11_2part);167clear_obj(m_weight_range_hist_7);168clear_obj(m_weight_range_hist_7_2part);169clear_obj(m_mode7_submode_hist);170clear_obj(m_mode11_submode_hist);171clear_obj(m_part_hist);172}173174void update(const astc_hdr_4x4_pack_results& log_blk);175176void print();177};178179} // namespace basisu180181182183