Path: blob/master/thirdparty/basis_universal/encoder/basisu_uastc_enc.h
9902 views
// basisu_uastc_enc.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_etc.h"1617#include "../transcoder/basisu_transcoder_uastc.h"1819namespace basisu20{21const uint32_t TOTAL_PACK_UASTC_LEVELS = 5;2223enum24{25// Fastest is the lowest quality, although it's stil substantially higher quality vs. BC1/ETC1. It supports 5 modes.26// The output may be somewhat blocky because this setting doesn't support 2/3-subset UASTC modes, but it should be less blocky vs. BC1/ETC1.27// This setting doesn't write BC1 hints, so BC1 transcoding will be slower.28// Transcoded ETC1 quality will be lower because it only considers 2 hints out of 32.29// Avg. 43.45 dB30cPackUASTCLevelFastest = 0,3132// Faster is ~3x slower than fastest. It supports 9 modes.33// Avg. 46.49 dB34cPackUASTCLevelFaster = 1,3536// Default is ~5.5x slower than fastest. It supports 14 modes.37// Avg. 47.47 dB38cPackUASTCLevelDefault = 2,3940// Slower is ~14.5x slower than fastest. It supports all 18 modes.41// Avg. 48.01 dB42cPackUASTCLevelSlower = 3,4344// VerySlow is ~200x slower than fastest.45// The best quality the codec is capable of, but you'll need to be patient or have a lot of cores.46// Avg. 48.24 dB47cPackUASTCLevelVerySlow = 4,4849cPackUASTCLevelMask = 0xF,5051// By default the encoder tries to strike a balance between UASTC and transcoded BC7 quality.52// These flags allow you to favor only optimizing for lowest UASTC error, or lowest BC7 error.53cPackUASTCFavorUASTCError = 8,54cPackUASTCFavorBC7Error = 16,5556cPackUASTCETC1FasterHints = 64,57cPackUASTCETC1FastestHints = 128,58cPackUASTCETC1DisableFlipAndIndividual = 256,5960// Favor UASTC modes 0 and 10 more than the others (this is experimental, it's useful for RDO compression)61cPackUASTCFavorSimplerModes = 512,62};6364// pRGBAPixels: Pointer to source 4x4 block of RGBA pixels (R first in memory).65// block: Reference to destination UASTC block.66// level: Controls compression speed vs. performance tradeoff.67void encode_uastc(const uint8_t* pRGBAPixels, basist::uastc_block& output_block, uint32_t flags = cPackUASTCLevelDefault);6869struct uastc_encode_results70{71uint32_t m_uastc_mode;72uint32_t m_common_pattern;73basist::astc_block_desc m_astc;74color_rgba m_solid_color;75uint64_t m_astc_err;76};7778void pack_uastc(basist::uastc_block& blk, const uastc_encode_results& result, const etc_block& etc1_blk, uint32_t etc1_bias, const eac_a8_block& etc_eac_a8_blk, bool bc1_hint0, bool bc1_hint1);7980const uint32_t UASCT_RDO_DEFAULT_LZ_DICT_SIZE = 4096;8182const float UASTC_RDO_DEFAULT_MAX_ALLOWED_RMS_INCREASE_RATIO = 10.0f;83const float UASTC_RDO_DEFAULT_SKIP_BLOCK_RMS_THRESH = 8.0f;8485// The RDO encoder computes a smoothness factor, from [0,1], for each block. To do this it computes each block's maximum component variance, then it divides this by this factor and clamps the result.86// Larger values will result in more blocks being protected from too much distortion.87const float UASTC_RDO_DEFAULT_MAX_SMOOTH_BLOCK_STD_DEV = 18.0f;8889// The RDO encoder can artifically boost the error of smooth blocks, in order to suppress distortions on smooth areas of the texture.90// The encoder will use this value as the maximum error scale to use on smooth blocks. The larger this value, the better smooth bocks will look. Set to 1.0 to disable this completely.91const float UASTC_RDO_DEFAULT_SMOOTH_BLOCK_MAX_ERROR_SCALE = 10.0f;9293struct uastc_rdo_params94{95uastc_rdo_params()96{97clear();98}99100void clear()101{102m_lz_dict_size = UASCT_RDO_DEFAULT_LZ_DICT_SIZE;103m_lambda = 0.5f;104m_max_allowed_rms_increase_ratio = UASTC_RDO_DEFAULT_MAX_ALLOWED_RMS_INCREASE_RATIO;105m_skip_block_rms_thresh = UASTC_RDO_DEFAULT_SKIP_BLOCK_RMS_THRESH;106m_endpoint_refinement = true;107m_lz_literal_cost = 100;108109m_max_smooth_block_std_dev = UASTC_RDO_DEFAULT_MAX_SMOOTH_BLOCK_STD_DEV;110m_smooth_block_max_error_scale = UASTC_RDO_DEFAULT_SMOOTH_BLOCK_MAX_ERROR_SCALE;111}112113// m_lz_dict_size: Size of LZ dictionary to simulate in bytes. The larger this value, the slower the encoder but the higher the quality per LZ compressed bit.114uint32_t m_lz_dict_size;115116// m_lambda: The post-processor tries to reduce distortion+rate*lambda (rate is approximate LZ bits and distortion is scaled MS error).117// Larger values push the postprocessor towards optimizing more for lower rate, and smaller values more for distortion. 0=minimal distortion.118float m_lambda;119120// m_max_allowed_rms_increase_ratio: How much the RMS error of a block is allowed to increase before a trial is rejected. 1.0=no increase allowed, 1.05=5% increase allowed, etc.121float m_max_allowed_rms_increase_ratio;122123// m_skip_block_rms_thresh: Blocks with this much RMS error or more are completely skipped by the RDO encoder.124float m_skip_block_rms_thresh;125126// m_endpoint_refinement: If true, the post-process will attempt to refine the endpoints of blocks with modified selectors.127bool m_endpoint_refinement;128129float m_max_smooth_block_std_dev;130float m_smooth_block_max_error_scale;131132uint32_t m_lz_literal_cost;133};134135// num_blocks, pBlocks: Number of blocks and pointer to UASTC blocks to process.136// pBlock_pixels: Pointer to an array of 4x4 blocks containing the original texture pixels. This is NOT a raster image, but a pointer to individual 4x4 blocks.137// flags: Pass in the same flags used to encode the UASTC blocks. The flags are used to reencode the transcode hints in the same way.138bool uastc_rdo(uint32_t num_blocks, basist::uastc_block* pBlocks, const color_rgba* pBlock_pixels, const uastc_rdo_params ¶ms, uint32_t flags = cPackUASTCLevelDefault, job_pool* pJob_pool = nullptr, uint32_t total_jobs = 0);139} // namespace basisu140141142