Path: blob/master/thirdparty/basis_universal/encoder/basisu_bc7enc.h
9902 views
// File: basisu_bc7enc.h1// Copyright (C) 2019-2024 Binomial LLC. All Rights Reserved.2//3// Licensed under the Apache License, Version 2.0 (the "License");4// you may not use this file except in compliance with the License.5// You may obtain a copy of the License at6//7// http://www.apache.org/licenses/LICENSE-2.08//9// Unless required by applicable law or agreed to in writing, software10// distributed under the License is distributed on an "AS IS" BASIS,11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12// See the License for the specific language governing permissions and13// limitations under the License.14#pragma once15#include "basisu_enc.h"16#include "../transcoder/basisu_transcoder_uastc.h"1718namespace basisu19{2021#define BC7ENC_MAX_PARTITIONS1 (64)22#define BC7ENC_MAX_UBER_LEVEL (4)2324typedef uint8_t bc7enc_bool;2526#define BC7ENC_TRUE (1)27#define BC7ENC_FALSE (0)2829typedef struct { float m_c[4]; } bc7enc_vec4F;3031extern const float g_bc7_weights1x[2 * 4];32extern const float g_bc7_weights2x[4 * 4];33extern const float g_bc7_weights3x[8 * 4];34extern const float g_bc7_weights4x[16 * 4];35extern const float g_astc_weights4x[16 * 4];36extern const float g_astc_weights5x[32 * 4];37extern const float g_astc_weights_3levelsx[3 * 4];3839extern basist::astc_quant_bin g_astc_sorted_order_unquant[basist::BC7ENC_TOTAL_ASTC_RANGES][256]; // [sorted unquantized order]4041struct color_cell_compressor_params42{43uint32_t m_num_pixels;44const basist::color_quad_u8* m_pPixels;4546uint32_t m_num_selector_weights;47const uint32_t* m_pSelector_weights;4849const bc7enc_vec4F* m_pSelector_weightsx;50uint32_t m_comp_bits;5152const uint8_t *m_pForce_selectors;5354// Non-zero m_astc_endpoint_range enables ASTC mode. m_comp_bits and m_has_pbits are always false. We only support 2, 3, or 4 bit weight encodings.55uint32_t m_astc_endpoint_range;5657uint32_t m_weights[4];58bc7enc_bool m_has_alpha;59bc7enc_bool m_has_pbits;60bc7enc_bool m_endpoints_share_pbit;61bc7enc_bool m_perceptual;62};6364struct color_cell_compressor_results65{66uint64_t m_best_overall_err;67basist::color_quad_u8 m_low_endpoint;68basist::color_quad_u8 m_high_endpoint;69uint32_t m_pbits[2];70uint8_t* m_pSelectors;71uint8_t* m_pSelectors_temp;7273// Encoded ASTC indices, if ASTC mode is enabled74basist::color_quad_u8 m_astc_low_endpoint;75basist::color_quad_u8 m_astc_high_endpoint;76};7778struct bc7enc_compress_block_params79{80// m_max_partitions_mode1 may range from 0 (disables mode 1) to BC7ENC_MAX_PARTITIONS1. The higher this value, the slower the compressor, but the higher the quality.81uint32_t m_max_partitions_mode1;8283// Relative RGBA or YCbCrA weights.84uint32_t m_weights[4];8586// m_uber_level may range from 0 to BC7ENC_MAX_UBER_LEVEL. The higher this value, the slower the compressor, but the higher the quality.87uint32_t m_uber_level;8889// If m_perceptual is true, colorspace error is computed in YCbCr space, otherwise RGB.90bc7enc_bool m_perceptual;9192uint32_t m_least_squares_passes;93};9495uint64_t color_cell_compression(uint32_t mode, const color_cell_compressor_params* pParams, color_cell_compressor_results* pResults, const bc7enc_compress_block_params* pComp_params);9697uint64_t color_cell_compression_est_astc(98uint32_t num_weights, uint32_t num_comps, const uint32_t* pWeight_table,99uint32_t num_pixels, const basist::color_quad_u8* pPixels,100uint64_t best_err_so_far, const uint32_t weights[4]);101102inline void bc7enc_compress_block_params_init_linear_weights(bc7enc_compress_block_params* p)103{104p->m_perceptual = BC7ENC_FALSE;105p->m_weights[0] = 1;106p->m_weights[1] = 1;107p->m_weights[2] = 1;108p->m_weights[3] = 1;109}110111inline void bc7enc_compress_block_params_init_perceptual_weights(bc7enc_compress_block_params* p)112{113p->m_perceptual = BC7ENC_TRUE;114p->m_weights[0] = 128;115p->m_weights[1] = 64;116p->m_weights[2] = 16;117p->m_weights[3] = 32;118}119120inline void bc7enc_compress_block_params_init(bc7enc_compress_block_params* p)121{122p->m_max_partitions_mode1 = BC7ENC_MAX_PARTITIONS1;123p->m_least_squares_passes = 1;124p->m_uber_level = 0;125bc7enc_compress_block_params_init_perceptual_weights(p);126}127128// bc7enc_compress_block_init() MUST be called before calling bc7enc_compress_block() (or you'll get artifacts).129void bc7enc_compress_block_init();130131} // namespace basisu132133134