12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391 |
- // SPDX-License-Identifier: Apache-2.0
- // ----------------------------------------------------------------------------
- // Copyright 2011-2024 Arm Limited
- //
- // Licensed under the Apache License, Version 2.0 (the "License"); you may not
- // use this file except in compliance with the License. You may obtain a copy
- // of the License at:
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
- // License for the specific language governing permissions and limitations
- // under the License.
- // ----------------------------------------------------------------------------
- /**
- * @brief Functions for the library entrypoint.
- */
- #include <array>
- #include <cstring>
- #include <new>
- #include "astcenc.h"
- #include "astcenc_internal_entry.h"
- #include "astcenc_diagnostic_trace.h"
- /**
- * @brief Record of the quality tuning parameter values.
- *
- * See the @c astcenc_config structure for detailed parameter documentation.
- *
- * Note that the mse_overshoot entries are scaling factors relative to the base MSE to hit db_limit.
- * A 20% overshoot is harder to hit for a higher base db_limit, so we may actually use lower ratios
- * for the more through search presets because the underlying db_limit is so much higher.
- */
- struct astcenc_preset_config
- {
- float quality;
- unsigned int tune_partition_count_limit;
- unsigned int tune_2partition_index_limit;
- unsigned int tune_3partition_index_limit;
- unsigned int tune_4partition_index_limit;
- unsigned int tune_block_mode_limit;
- unsigned int tune_refinement_limit;
- unsigned int tune_candidate_limit;
- unsigned int tune_2partitioning_candidate_limit;
- unsigned int tune_3partitioning_candidate_limit;
- unsigned int tune_4partitioning_candidate_limit;
- float tune_db_limit_a_base;
- float tune_db_limit_b_base;
- float tune_mse_overshoot;
- float tune_2partition_early_out_limit_factor;
- float tune_3partition_early_out_limit_factor;
- float tune_2plane_early_out_limit_correlation;
- float tune_search_mode0_enable;
- };
- /**
- * @brief The static presets for high bandwidth encodings (x < 25 texels per block).
- */
- static const std::array<astcenc_preset_config, 6> preset_configs_high {{
- {
- ASTCENC_PRE_FASTEST,
- 2, 10, 6, 4, 43, 2, 2, 2, 2, 2, 85.2f, 63.2f, 3.5f, 1.0f, 1.0f, 0.85f, 0.0f
- }, {
- ASTCENC_PRE_FAST,
- 3, 18, 10, 8, 55, 3, 3, 2, 2, 2, 85.2f, 63.2f, 3.5f, 1.0f, 1.0f, 0.90f, 0.0f
- }, {
- ASTCENC_PRE_MEDIUM,
- 4, 34, 28, 16, 77, 3, 3, 2, 2, 2, 95.0f, 70.0f, 2.5f, 1.1f, 1.05f, 0.95f, 0.0f
- }, {
- ASTCENC_PRE_THOROUGH,
- 4, 82, 60, 30, 94, 4, 4, 3, 2, 2, 105.0f, 77.0f, 10.0f, 1.35f, 1.15f, 0.97f, 0.0f
- }, {
- ASTCENC_PRE_VERYTHOROUGH,
- 4, 256, 128, 64, 98, 4, 6, 8, 6, 4, 200.0f, 200.0f, 10.0f, 1.6f, 1.4f, 0.98f, 0.0f
- }, {
- ASTCENC_PRE_EXHAUSTIVE,
- 4, 512, 512, 512, 100, 4, 8, 8, 8, 8, 200.0f, 200.0f, 10.0f, 2.0f, 2.0f, 0.99f, 0.0f
- }
- }};
- /**
- * @brief The static presets for medium bandwidth encodings (25 <= x < 64 texels per block).
- */
- static const std::array<astcenc_preset_config, 6> preset_configs_mid {{
- {
- ASTCENC_PRE_FASTEST,
- 2, 10, 6, 4, 43, 2, 2, 2, 2, 2, 85.2f, 63.2f, 3.5f, 1.0f, 1.0f, 0.80f, 1.0f
- }, {
- ASTCENC_PRE_FAST,
- 3, 18, 12, 10, 55, 3, 3, 2, 2, 2, 85.2f, 63.2f, 3.5f, 1.0f, 1.0f, 0.85f, 1.0f
- }, {
- ASTCENC_PRE_MEDIUM,
- 3, 34, 28, 16, 77, 3, 3, 2, 2, 2, 95.0f, 70.0f, 3.0f, 1.1f, 1.05f, 0.90f, 1.0f
- }, {
- ASTCENC_PRE_THOROUGH,
- 4, 82, 60, 30, 94, 4, 4, 3, 2, 2, 105.0f, 77.0f, 10.0f, 1.4f, 1.2f, 0.95f, 0.0f
- }, {
- ASTCENC_PRE_VERYTHOROUGH,
- 4, 256, 128, 64, 98, 4, 6, 8, 6, 3, 200.0f, 200.0f, 10.0f, 1.6f, 1.4f, 0.98f, 0.0f
- }, {
- ASTCENC_PRE_EXHAUSTIVE,
- 4, 256, 256, 256, 100, 4, 8, 8, 8, 8, 200.0f, 200.0f, 10.0f, 2.0f, 2.0f, 0.99f, 0.0f
- }
- }};
- /**
- * @brief The static presets for low bandwidth encodings (64 <= x texels per block).
- */
- static const std::array<astcenc_preset_config, 6> preset_configs_low {{
- {
- ASTCENC_PRE_FASTEST,
- 2, 10, 6, 4, 40, 2, 2, 2, 2, 2, 85.0f, 63.0f, 3.5f, 1.0f, 1.0f, 0.80f, 1.0f
- }, {
- ASTCENC_PRE_FAST,
- 2, 18, 12, 10, 55, 3, 3, 2, 2, 2, 85.0f, 63.0f, 3.5f, 1.0f, 1.0f, 0.85f, 1.0f
- }, {
- ASTCENC_PRE_MEDIUM,
- 3, 34, 28, 16, 77, 3, 3, 2, 2, 2, 95.0f, 70.0f, 3.5f, 1.1f, 1.05f, 0.90f, 1.0f
- }, {
- ASTCENC_PRE_THOROUGH,
- 4, 82, 60, 30, 93, 4, 4, 3, 2, 2, 105.0f, 77.0f, 10.0f, 1.3f, 1.2f, 0.97f, 1.0f
- }, {
- ASTCENC_PRE_VERYTHOROUGH,
- 4, 256, 128, 64, 98, 4, 6, 8, 5, 2, 200.0f, 200.0f, 10.0f, 1.6f, 1.4f, 0.98f, 1.0f
- }, {
- ASTCENC_PRE_EXHAUSTIVE,
- 4, 256, 256, 256, 100, 4, 8, 8, 8, 8, 200.0f, 200.0f, 10.0f, 2.0f, 2.0f, 0.99f, 1.0f
- }
- }};
- /**
- * @brief Validate CPU floating point meets assumptions made in the codec.
- *
- * The codec is written with the assumption that a float threaded through the @c if32 union will be
- * stored and reloaded as a 32-bit IEEE-754 float with round-to-nearest rounding. This is always the
- * case in an IEEE-754 compliant system, however not every system or compilation mode is actually
- * IEEE-754 compliant. This normally fails if the code is compiled with fast math enabled.
- *
- * @return Return @c ASTCENC_SUCCESS if validated, otherwise an error on failure.
- */
- static astcenc_error validate_cpu_float()
- {
- if32 p;
- volatile float xprec_testval = 2.51f;
- p.f = xprec_testval + 12582912.0f;
- float q = p.f - 12582912.0f;
- if (q != 3.0f)
- {
- return ASTCENC_ERR_BAD_CPU_FLOAT;
- }
- return ASTCENC_SUCCESS;
- }
- /**
- * @brief Validate config profile.
- *
- * @param profile The profile to check.
- *
- * @return Return @c ASTCENC_SUCCESS if validated, otherwise an error on failure.
- */
- static astcenc_error validate_profile(
- astcenc_profile profile
- ) {
- // Values in this enum are from an external user, so not guaranteed to be
- // bounded to the enum values
- switch (static_cast<int>(profile))
- {
- case ASTCENC_PRF_LDR_SRGB:
- case ASTCENC_PRF_LDR:
- case ASTCENC_PRF_HDR_RGB_LDR_A:
- case ASTCENC_PRF_HDR:
- return ASTCENC_SUCCESS;
- default:
- return ASTCENC_ERR_BAD_PROFILE;
- }
- }
- /**
- * @brief Validate block size.
- *
- * @param block_x The block x dimensions.
- * @param block_y The block y dimensions.
- * @param block_z The block z dimensions.
- *
- * @return Return @c ASTCENC_SUCCESS if validated, otherwise an error on failure.
- */
- static astcenc_error validate_block_size(
- unsigned int block_x,
- unsigned int block_y,
- unsigned int block_z
- ) {
- // Test if this is a legal block size at all
- bool is_legal = (((block_z <= 1) && is_legal_2d_block_size(block_x, block_y)) ||
- ((block_z >= 2) && is_legal_3d_block_size(block_x, block_y, block_z)));
- if (!is_legal)
- {
- return ASTCENC_ERR_BAD_BLOCK_SIZE;
- }
- // Test if this build has sufficient capacity for this block size
- bool have_capacity = (block_x * block_y * block_z) <= BLOCK_MAX_TEXELS;
- if (!have_capacity)
- {
- return ASTCENC_ERR_NOT_IMPLEMENTED;
- }
- return ASTCENC_SUCCESS;
- }
- /**
- * @brief Validate flags.
- *
- * @param profile The profile to check.
- * @param flags The flags to check.
- *
- * @return Return @c ASTCENC_SUCCESS if validated, otherwise an error on failure.
- */
- static astcenc_error validate_flags(
- astcenc_profile profile,
- unsigned int flags
- ) {
- // Flags field must not contain any unknown flag bits
- unsigned int exMask = ~ASTCENC_ALL_FLAGS;
- if (popcount(flags & exMask) != 0)
- {
- return ASTCENC_ERR_BAD_FLAGS;
- }
- // Flags field must only contain at most a single map type
- exMask = ASTCENC_FLG_MAP_NORMAL
- | ASTCENC_FLG_MAP_RGBM;
- if (popcount(flags & exMask) > 1)
- {
- return ASTCENC_ERR_BAD_FLAGS;
- }
- // Decode_unorm8 must only be used with an LDR profile
- bool is_unorm8 = flags & ASTCENC_FLG_USE_DECODE_UNORM8;
- bool is_hdr = (profile == ASTCENC_PRF_HDR) || (profile == ASTCENC_PRF_HDR_RGB_LDR_A);
- if (is_unorm8 && is_hdr)
- {
- return ASTCENC_ERR_BAD_DECODE_MODE;
- }
- return ASTCENC_SUCCESS;
- }
- #if !defined(ASTCENC_DECOMPRESS_ONLY)
- /**
- * @brief Validate single channel compression swizzle.
- *
- * @param swizzle The swizzle to check.
- *
- * @return Return @c ASTCENC_SUCCESS if validated, otherwise an error on failure.
- */
- static astcenc_error validate_compression_swz(
- astcenc_swz swizzle
- ) {
- // Not all enum values are handled; SWZ_Z is invalid for compression
- switch (static_cast<int>(swizzle))
- {
- case ASTCENC_SWZ_R:
- case ASTCENC_SWZ_G:
- case ASTCENC_SWZ_B:
- case ASTCENC_SWZ_A:
- case ASTCENC_SWZ_0:
- case ASTCENC_SWZ_1:
- return ASTCENC_SUCCESS;
- default:
- return ASTCENC_ERR_BAD_SWIZZLE;
- }
- }
- /**
- * @brief Validate overall compression swizzle.
- *
- * @param swizzle The swizzle to check.
- *
- * @return Return @c ASTCENC_SUCCESS if validated, otherwise an error on failure.
- */
- static astcenc_error validate_compression_swizzle(
- const astcenc_swizzle& swizzle
- ) {
- if (validate_compression_swz(swizzle.r) ||
- validate_compression_swz(swizzle.g) ||
- validate_compression_swz(swizzle.b) ||
- validate_compression_swz(swizzle.a))
- {
- return ASTCENC_ERR_BAD_SWIZZLE;
- }
- return ASTCENC_SUCCESS;
- }
- #endif
- /**
- * @brief Validate single channel decompression swizzle.
- *
- * @param swizzle The swizzle to check.
- *
- * @return Return @c ASTCENC_SUCCESS if validated, otherwise an error on failure.
- */
- static astcenc_error validate_decompression_swz(
- astcenc_swz swizzle
- ) {
- // Values in this enum are from an external user, so not guaranteed to be
- // bounded to the enum values
- switch (static_cast<int>(swizzle))
- {
- case ASTCENC_SWZ_R:
- case ASTCENC_SWZ_G:
- case ASTCENC_SWZ_B:
- case ASTCENC_SWZ_A:
- case ASTCENC_SWZ_0:
- case ASTCENC_SWZ_1:
- case ASTCENC_SWZ_Z:
- return ASTCENC_SUCCESS;
- default:
- return ASTCENC_ERR_BAD_SWIZZLE;
- }
- }
- /**
- * @brief Validate overall decompression swizzle.
- *
- * @param swizzle The swizzle to check.
- *
- * @return Return @c ASTCENC_SUCCESS if validated, otherwise an error on failure.
- */
- static astcenc_error validate_decompression_swizzle(
- const astcenc_swizzle& swizzle
- ) {
- if (validate_decompression_swz(swizzle.r) ||
- validate_decompression_swz(swizzle.g) ||
- validate_decompression_swz(swizzle.b) ||
- validate_decompression_swz(swizzle.a))
- {
- return ASTCENC_ERR_BAD_SWIZZLE;
- }
- return ASTCENC_SUCCESS;
- }
- /**
- * Validate that an incoming configuration is in-spec.
- *
- * This function can respond in two ways:
- *
- * * Numerical inputs that have valid ranges are clamped to those valid ranges. No error is thrown
- * for out-of-range inputs in this case.
- * * Numerical inputs and logic inputs are are logically invalid and which make no sense
- * algorithmically will return an error.
- *
- * @param[in,out] config The input compressor configuration.
- *
- * @return Return @c ASTCENC_SUCCESS if validated, otherwise an error on failure.
- */
- static astcenc_error validate_config(
- astcenc_config &config
- ) {
- astcenc_error status;
- status = validate_profile(config.profile);
- if (status != ASTCENC_SUCCESS)
- {
- return status;
- }
- status = validate_flags(config.profile, config.flags);
- if (status != ASTCENC_SUCCESS)
- {
- return status;
- }
- status = validate_block_size(config.block_x, config.block_y, config.block_z);
- if (status != ASTCENC_SUCCESS)
- {
- return status;
- }
- #if defined(ASTCENC_DECOMPRESS_ONLY)
- // Decompress-only builds only support decompress-only contexts
- if (!(config.flags & ASTCENC_FLG_DECOMPRESS_ONLY))
- {
- return ASTCENC_ERR_BAD_PARAM;
- }
- #endif
- config.rgbm_m_scale = astc::max(config.rgbm_m_scale, 1.0f);
- config.tune_partition_count_limit = astc::clamp(config.tune_partition_count_limit, 1u, 4u);
- config.tune_2partition_index_limit = astc::clamp(config.tune_2partition_index_limit, 1u, BLOCK_MAX_PARTITIONINGS);
- config.tune_3partition_index_limit = astc::clamp(config.tune_3partition_index_limit, 1u, BLOCK_MAX_PARTITIONINGS);
- config.tune_4partition_index_limit = astc::clamp(config.tune_4partition_index_limit, 1u, BLOCK_MAX_PARTITIONINGS);
- config.tune_block_mode_limit = astc::clamp(config.tune_block_mode_limit, 1u, 100u);
- config.tune_refinement_limit = astc::max(config.tune_refinement_limit, 1u);
- config.tune_candidate_limit = astc::clamp(config.tune_candidate_limit, 1u, TUNE_MAX_TRIAL_CANDIDATES);
- config.tune_2partitioning_candidate_limit = astc::clamp(config.tune_2partitioning_candidate_limit, 1u, TUNE_MAX_PARTITIONING_CANDIDATES);
- config.tune_3partitioning_candidate_limit = astc::clamp(config.tune_3partitioning_candidate_limit, 1u, TUNE_MAX_PARTITIONING_CANDIDATES);
- config.tune_4partitioning_candidate_limit = astc::clamp(config.tune_4partitioning_candidate_limit, 1u, TUNE_MAX_PARTITIONING_CANDIDATES);
- config.tune_db_limit = astc::max(config.tune_db_limit, 0.0f);
- config.tune_mse_overshoot = astc::max(config.tune_mse_overshoot, 1.0f);
- config.tune_2partition_early_out_limit_factor = astc::max(config.tune_2partition_early_out_limit_factor, 0.0f);
- config.tune_3partition_early_out_limit_factor = astc::max(config.tune_3partition_early_out_limit_factor, 0.0f);
- config.tune_2plane_early_out_limit_correlation = astc::max(config.tune_2plane_early_out_limit_correlation, 0.0f);
- // Specifying a zero weight color component is not allowed; force to small value
- float max_weight = astc::max(astc::max(config.cw_r_weight, config.cw_g_weight),
- astc::max(config.cw_b_weight, config.cw_a_weight));
- if (max_weight > 0.0f)
- {
- max_weight /= 1000.0f;
- config.cw_r_weight = astc::max(config.cw_r_weight, max_weight);
- config.cw_g_weight = astc::max(config.cw_g_weight, max_weight);
- config.cw_b_weight = astc::max(config.cw_b_weight, max_weight);
- config.cw_a_weight = astc::max(config.cw_a_weight, max_weight);
- }
- // If all color components error weights are zero then return an error
- else
- {
- return ASTCENC_ERR_BAD_PARAM;
- }
- return ASTCENC_SUCCESS;
- }
- /* See header for documentation. */
- astcenc_error astcenc_config_init(
- astcenc_profile profile,
- unsigned int block_x,
- unsigned int block_y,
- unsigned int block_z,
- float quality,
- unsigned int flags,
- astcenc_config* configp
- ) {
- astcenc_error status;
- status = validate_cpu_float();
- if (status != ASTCENC_SUCCESS)
- {
- return status;
- }
- // Zero init all config fields; although most of will be over written
- astcenc_config& config = *configp;
- std::memset(&config, 0, sizeof(config));
- // Process the block size
- block_z = astc::max(block_z, 1u); // For 2D blocks Z==0 is accepted, but convert to 1
- status = validate_block_size(block_x, block_y, block_z);
- if (status != ASTCENC_SUCCESS)
- {
- return status;
- }
- config.block_x = block_x;
- config.block_y = block_y;
- config.block_z = block_z;
- float texels = static_cast<float>(block_x * block_y * block_z);
- float ltexels = logf(texels) / logf(10.0f);
- // Process the performance quality level or preset; note that this must be done before we
- // process any additional settings, such as color profile and flags, which may replace some of
- // these settings with more use case tuned values
- if (quality < ASTCENC_PRE_FASTEST ||
- quality > ASTCENC_PRE_EXHAUSTIVE)
- {
- return ASTCENC_ERR_BAD_QUALITY;
- }
- static const std::array<astcenc_preset_config, 6>* preset_configs;
- int texels_int = block_x * block_y * block_z;
- if (texels_int < 25)
- {
- preset_configs = &preset_configs_high;
- }
- else if (texels_int < 64)
- {
- preset_configs = &preset_configs_mid;
- }
- else
- {
- preset_configs = &preset_configs_low;
- }
- // Determine which preset to use, or which pair to interpolate
- size_t start;
- size_t end;
- for (end = 0; end < preset_configs->size(); end++)
- {
- if ((*preset_configs)[end].quality >= quality)
- {
- break;
- }
- }
- start = end == 0 ? 0 : end - 1;
- // Start and end node are the same - so just transfer the values.
- if (start == end)
- {
- config.tune_partition_count_limit = (*preset_configs)[start].tune_partition_count_limit;
- config.tune_2partition_index_limit = (*preset_configs)[start].tune_2partition_index_limit;
- config.tune_3partition_index_limit = (*preset_configs)[start].tune_3partition_index_limit;
- config.tune_4partition_index_limit = (*preset_configs)[start].tune_4partition_index_limit;
- config.tune_block_mode_limit = (*preset_configs)[start].tune_block_mode_limit;
- config.tune_refinement_limit = (*preset_configs)[start].tune_refinement_limit;
- config.tune_candidate_limit = (*preset_configs)[start].tune_candidate_limit;
- config.tune_2partitioning_candidate_limit = (*preset_configs)[start].tune_2partitioning_candidate_limit;
- config.tune_3partitioning_candidate_limit = (*preset_configs)[start].tune_3partitioning_candidate_limit;
- config.tune_4partitioning_candidate_limit = (*preset_configs)[start].tune_4partitioning_candidate_limit;
- config.tune_db_limit = astc::max((*preset_configs)[start].tune_db_limit_a_base - 35 * ltexels,
- (*preset_configs)[start].tune_db_limit_b_base - 19 * ltexels);
- config.tune_mse_overshoot = (*preset_configs)[start].tune_mse_overshoot;
- config.tune_2partition_early_out_limit_factor = (*preset_configs)[start].tune_2partition_early_out_limit_factor;
- config.tune_3partition_early_out_limit_factor = (*preset_configs)[start].tune_3partition_early_out_limit_factor;
- config.tune_2plane_early_out_limit_correlation = (*preset_configs)[start].tune_2plane_early_out_limit_correlation;
- config.tune_search_mode0_enable = (*preset_configs)[start].tune_search_mode0_enable;
- }
- // Start and end node are not the same - so interpolate between them
- else
- {
- auto& node_a = (*preset_configs)[start];
- auto& node_b = (*preset_configs)[end];
- float wt_range = node_b.quality - node_a.quality;
- assert(wt_range > 0);
- // Compute interpolation factors
- float wt_node_a = (node_b.quality - quality) / wt_range;
- float wt_node_b = (quality - node_a.quality) / wt_range;
- #define LERP(param) ((node_a.param * wt_node_a) + (node_b.param * wt_node_b))
- #define LERPI(param) astc::flt2int_rtn(\
- (static_cast<float>(node_a.param) * wt_node_a) + \
- (static_cast<float>(node_b.param) * wt_node_b))
- #define LERPUI(param) static_cast<unsigned int>(LERPI(param))
- config.tune_partition_count_limit = LERPI(tune_partition_count_limit);
- config.tune_2partition_index_limit = LERPI(tune_2partition_index_limit);
- config.tune_3partition_index_limit = LERPI(tune_3partition_index_limit);
- config.tune_4partition_index_limit = LERPI(tune_4partition_index_limit);
- config.tune_block_mode_limit = LERPI(tune_block_mode_limit);
- config.tune_refinement_limit = LERPI(tune_refinement_limit);
- config.tune_candidate_limit = LERPUI(tune_candidate_limit);
- config.tune_2partitioning_candidate_limit = LERPUI(tune_2partitioning_candidate_limit);
- config.tune_3partitioning_candidate_limit = LERPUI(tune_3partitioning_candidate_limit);
- config.tune_4partitioning_candidate_limit = LERPUI(tune_4partitioning_candidate_limit);
- config.tune_db_limit = astc::max(LERP(tune_db_limit_a_base) - 35 * ltexels,
- LERP(tune_db_limit_b_base) - 19 * ltexels);
- config.tune_mse_overshoot = LERP(tune_mse_overshoot);
- config.tune_2partition_early_out_limit_factor = LERP(tune_2partition_early_out_limit_factor);
- config.tune_3partition_early_out_limit_factor = LERP(tune_3partition_early_out_limit_factor);
- config.tune_2plane_early_out_limit_correlation = LERP(tune_2plane_early_out_limit_correlation);
- config.tune_search_mode0_enable = LERP(tune_search_mode0_enable);
- #undef LERP
- #undef LERPI
- #undef LERPUI
- }
- // Set heuristics to the defaults for each color profile
- config.cw_r_weight = 1.0f;
- config.cw_g_weight = 1.0f;
- config.cw_b_weight = 1.0f;
- config.cw_a_weight = 1.0f;
- config.a_scale_radius = 0;
- config.rgbm_m_scale = 0.0f;
- config.profile = profile;
- // Values in this enum are from an external user, so not guaranteed to be
- // bounded to the enum values
- switch (static_cast<int>(profile))
- {
- case ASTCENC_PRF_LDR:
- case ASTCENC_PRF_LDR_SRGB:
- break;
- case ASTCENC_PRF_HDR_RGB_LDR_A:
- case ASTCENC_PRF_HDR:
- config.tune_db_limit = 999.0f;
- config.tune_search_mode0_enable = 0.0f;
- break;
- default:
- return ASTCENC_ERR_BAD_PROFILE;
- }
- // Flags field must not contain any unknown flag bits
- status = validate_flags(profile, flags);
- if (status != ASTCENC_SUCCESS)
- {
- return status;
- }
- if (flags & ASTCENC_FLG_MAP_NORMAL)
- {
- // Normal map encoding uses L+A blocks, so allow one more partitioning
- // than normal. We need need fewer bits for endpoints, so more likely
- // to be able to use more partitions than an RGB/RGBA block
- config.tune_partition_count_limit = astc::min(config.tune_partition_count_limit + 1u, 4u);
- config.cw_g_weight = 0.0f;
- config.cw_b_weight = 0.0f;
- config.tune_2partition_early_out_limit_factor *= 1.5f;
- config.tune_3partition_early_out_limit_factor *= 1.5f;
- config.tune_2plane_early_out_limit_correlation = 0.99f;
- // Normals are prone to blocking artifacts on smooth curves
- // so force compressor to try harder here ...
- config.tune_db_limit *= 1.03f;
- }
- else if (flags & ASTCENC_FLG_MAP_RGBM)
- {
- config.rgbm_m_scale = 5.0f;
- config.cw_a_weight = 2.0f * config.rgbm_m_scale;
- }
- else // (This is color data)
- {
- // This is a very basic perceptual metric for RGB color data, which weights error
- // significance by the perceptual luminance contribution of each color channel. For
- // luminance the usual weights to compute luminance from a linear RGB value are as
- // follows:
- //
- // l = r * 0.3 + g * 0.59 + b * 0.11
- //
- // ... but we scale these up to keep a better balance between color and alpha. Note
- // that if the content is using alpha we'd recommend using the -a option to weight
- // the color contribution by the alpha transparency.
- if (flags & ASTCENC_FLG_USE_PERCEPTUAL)
- {
- config.cw_r_weight = 0.30f * 2.25f;
- config.cw_g_weight = 0.59f * 2.25f;
- config.cw_b_weight = 0.11f * 2.25f;
- }
- }
- config.flags = flags;
- return ASTCENC_SUCCESS;
- }
- /* See header for documentation. */
- astcenc_error astcenc_context_alloc(
- const astcenc_config* configp,
- unsigned int thread_count,
- astcenc_context** context
- ) {
- astcenc_error status;
- const astcenc_config& config = *configp;
- status = validate_cpu_float();
- if (status != ASTCENC_SUCCESS)
- {
- return status;
- }
- if (thread_count == 0)
- {
- return ASTCENC_ERR_BAD_PARAM;
- }
- #if defined(ASTCENC_DIAGNOSTICS)
- // Force single threaded compressor use in diagnostic mode.
- if (thread_count != 1)
- {
- return ASTCENC_ERR_BAD_PARAM;
- }
- #endif
- astcenc_context* ctxo = new astcenc_context;
- astcenc_contexti* ctx = &ctxo->context;
- ctx->thread_count = thread_count;
- ctx->config = config;
- ctx->working_buffers = nullptr;
- // These are allocated per-compress, as they depend on image size
- ctx->input_alpha_averages = nullptr;
- // Copy the config first and validate the copy (we may modify it)
- status = validate_config(ctx->config);
- if (status != ASTCENC_SUCCESS)
- {
- delete ctxo;
- return status;
- }
- ctx->bsd = aligned_malloc<block_size_descriptor>(sizeof(block_size_descriptor), ASTCENC_VECALIGN);
- if (!ctx->bsd)
- {
- delete ctxo;
- return ASTCENC_ERR_OUT_OF_MEM;
- }
- bool can_omit_modes = static_cast<bool>(config.flags & ASTCENC_FLG_SELF_DECOMPRESS_ONLY);
- init_block_size_descriptor(config.block_x, config.block_y, config.block_z,
- can_omit_modes,
- config.tune_partition_count_limit,
- static_cast<float>(config.tune_block_mode_limit) / 100.0f,
- *ctx->bsd);
- #if !defined(ASTCENC_DECOMPRESS_ONLY)
- // Do setup only needed by compression
- if (!(ctx->config.flags & ASTCENC_FLG_DECOMPRESS_ONLY))
- {
- // Turn a dB limit into a per-texel error for faster use later
- if ((ctx->config.profile == ASTCENC_PRF_LDR) || (ctx->config.profile == ASTCENC_PRF_LDR_SRGB))
- {
- ctx->config.tune_db_limit = astc::pow(0.1f, ctx->config.tune_db_limit * 0.1f) * 65535.0f * 65535.0f;
- }
- else
- {
- ctx->config.tune_db_limit = 0.0f;
- }
- size_t worksize = sizeof(compression_working_buffers) * thread_count;
- ctx->working_buffers = aligned_malloc<compression_working_buffers>(worksize, ASTCENC_VECALIGN);
- static_assert((ASTCENC_VECALIGN == 0) || ((sizeof(compression_working_buffers) % ASTCENC_VECALIGN) == 0),
- "compression_working_buffers size must be multiple of vector alignment");
- if (!ctx->working_buffers)
- {
- aligned_free<block_size_descriptor>(ctx->bsd);
- delete ctxo;
- *context = nullptr;
- return ASTCENC_ERR_OUT_OF_MEM;
- }
- }
- #endif
- #if defined(ASTCENC_DIAGNOSTICS)
- ctx->trace_log = new TraceLog(ctx->config.trace_file_path);
- if (!ctx->trace_log->m_file)
- {
- return ASTCENC_ERR_DTRACE_FAILURE;
- }
- trace_add_data("block_x", config.block_x);
- trace_add_data("block_y", config.block_y);
- trace_add_data("block_z", config.block_z);
- #endif
- *context = ctxo;
- #if !defined(ASTCENC_DECOMPRESS_ONLY)
- prepare_angular_tables();
- #endif
- return ASTCENC_SUCCESS;
- }
- /* See header dor documentation. */
- void astcenc_context_free(
- astcenc_context* ctxo
- ) {
- if (ctxo)
- {
- astcenc_contexti* ctx = &ctxo->context;
- aligned_free<compression_working_buffers>(ctx->working_buffers);
- aligned_free<block_size_descriptor>(ctx->bsd);
- #if defined(ASTCENC_DIAGNOSTICS)
- delete ctx->trace_log;
- #endif
- delete ctxo;
- }
- }
- #if !defined(ASTCENC_DECOMPRESS_ONLY)
- /**
- * @brief Compress an image, after any preflight has completed.
- *
- * @param[out] ctxo The compressor context.
- * @param thread_index The thread index.
- * @param image The intput image.
- * @param swizzle The input swizzle.
- * @param[out] buffer The output array for the compressed data.
- */
- static void compress_image(
- astcenc_context& ctxo,
- unsigned int thread_index,
- const astcenc_image& image,
- const astcenc_swizzle& swizzle,
- uint8_t* buffer
- ) {
- astcenc_contexti& ctx = ctxo.context;
- const block_size_descriptor& bsd = *ctx.bsd;
- astcenc_profile decode_mode = ctx.config.profile;
- image_block blk;
- int block_x = bsd.xdim;
- int block_y = bsd.ydim;
- int block_z = bsd.zdim;
- blk.texel_count = static_cast<uint8_t>(block_x * block_y * block_z);
- int dim_x = image.dim_x;
- int dim_y = image.dim_y;
- int dim_z = image.dim_z;
- int xblocks = (dim_x + block_x - 1) / block_x;
- int yblocks = (dim_y + block_y - 1) / block_y;
- int zblocks = (dim_z + block_z - 1) / block_z;
- int block_count = zblocks * yblocks * xblocks;
- int row_blocks = xblocks;
- int plane_blocks = xblocks * yblocks;
- blk.decode_unorm8 = ctxo.context.config.flags & ASTCENC_FLG_USE_DECODE_UNORM8;
- // Populate the block channel weights
- blk.channel_weight = vfloat4(ctx.config.cw_r_weight,
- ctx.config.cw_g_weight,
- ctx.config.cw_b_weight,
- ctx.config.cw_a_weight);
- // Use preallocated scratch buffer
- auto& temp_buffers = ctx.working_buffers[thread_index];
- // Only the first thread actually runs the initializer
- ctxo.manage_compress.init(block_count, ctx.config.progress_callback);
- // Determine if we can use an optimized load function
- bool needs_swz = (swizzle.r != ASTCENC_SWZ_R) || (swizzle.g != ASTCENC_SWZ_G) ||
- (swizzle.b != ASTCENC_SWZ_B) || (swizzle.a != ASTCENC_SWZ_A);
- bool needs_hdr = (decode_mode == ASTCENC_PRF_HDR) ||
- (decode_mode == ASTCENC_PRF_HDR_RGB_LDR_A);
- bool use_fast_load = !needs_swz && !needs_hdr &&
- block_z == 1 && image.data_type == ASTCENC_TYPE_U8;
- auto load_func = load_image_block;
- if (use_fast_load)
- {
- load_func = load_image_block_fast_ldr;
- }
- // All threads run this processing loop until there is no work remaining
- while (true)
- {
- unsigned int count;
- unsigned int base = ctxo.manage_compress.get_task_assignment(16, count);
- if (!count)
- {
- break;
- }
- for (unsigned int i = base; i < base + count; i++)
- {
- // Decode i into x, y, z block indices
- int z = i / plane_blocks;
- unsigned int rem = i - (z * plane_blocks);
- int y = rem / row_blocks;
- int x = rem - (y * row_blocks);
- // Test if we can apply some basic alpha-scale RDO
- bool use_full_block = true;
- if (ctx.config.a_scale_radius != 0 && block_z == 1)
- {
- int start_x = x * block_x;
- int end_x = astc::min(dim_x, start_x + block_x);
- int start_y = y * block_y;
- int end_y = astc::min(dim_y, start_y + block_y);
- // SATs accumulate error, so don't test exactly zero. Test for
- // less than 1 alpha in the expanded block footprint that
- // includes the alpha radius.
- int x_footprint = block_x + 2 * (ctx.config.a_scale_radius - 1);
- int y_footprint = block_y + 2 * (ctx.config.a_scale_radius - 1);
- float footprint = static_cast<float>(x_footprint * y_footprint);
- float threshold = 0.9f / (255.0f * footprint);
- // Do we have any alpha values?
- use_full_block = false;
- for (int ay = start_y; ay < end_y; ay++)
- {
- for (int ax = start_x; ax < end_x; ax++)
- {
- float a_avg = ctx.input_alpha_averages[ay * dim_x + ax];
- if (a_avg > threshold)
- {
- use_full_block = true;
- ax = end_x;
- ay = end_y;
- }
- }
- }
- }
- // Fetch the full block for compression
- if (use_full_block)
- {
- load_func(decode_mode, image, blk, bsd, x * block_x, y * block_y, z * block_z, swizzle);
- // Scale RGB error contribution by the maximum alpha in the block
- // This encourages preserving alpha accuracy in regions with high
- // transparency, and can buy up to 0.5 dB PSNR.
- if (ctx.config.flags & ASTCENC_FLG_USE_ALPHA_WEIGHT)
- {
- float alpha_scale = blk.data_max.lane<3>() * (1.0f / 65535.0f);
- blk.channel_weight = vfloat4(ctx.config.cw_r_weight * alpha_scale,
- ctx.config.cw_g_weight * alpha_scale,
- ctx.config.cw_b_weight * alpha_scale,
- ctx.config.cw_a_weight);
- }
- }
- // Apply alpha scale RDO - substitute constant color block
- else
- {
- blk.origin_texel = vfloat4::zero();
- blk.data_min = vfloat4::zero();
- blk.data_mean = vfloat4::zero();
- blk.data_max = vfloat4::zero();
- blk.grayscale = true;
- }
- int offset = ((z * yblocks + y) * xblocks + x) * 16;
- uint8_t *bp = buffer + offset;
- compress_block(ctx, blk, bp, temp_buffers);
- }
- ctxo.manage_compress.complete_task_assignment(count);
- }
- }
- /**
- * @brief Compute regional averages in an image.
- *
- * This function can be called by multiple threads, but only after a single
- * thread calls the setup function @c init_compute_averages().
- *
- * Results are written back into @c img->input_alpha_averages.
- *
- * @param[out] ctx The context.
- * @param ag The average and variance arguments created during setup.
- */
- static void compute_averages(
- astcenc_context& ctx,
- const avg_args &ag
- ) {
- pixel_region_args arg = ag.arg;
- arg.work_memory = new vfloat4[ag.work_memory_size];
- int size_x = ag.img_size_x;
- int size_y = ag.img_size_y;
- int size_z = ag.img_size_z;
- int step_xy = ag.blk_size_xy;
- int step_z = ag.blk_size_z;
- int y_tasks = (size_y + step_xy - 1) / step_xy;
- // All threads run this processing loop until there is no work remaining
- while (true)
- {
- unsigned int count;
- unsigned int base = ctx.manage_avg.get_task_assignment(16, count);
- if (!count)
- {
- break;
- }
- for (unsigned int i = base; i < base + count; i++)
- {
- int z = (i / (y_tasks)) * step_z;
- int y = (i - (z * y_tasks)) * step_xy;
- arg.size_z = astc::min(step_z, size_z - z);
- arg.offset_z = z;
- arg.size_y = astc::min(step_xy, size_y - y);
- arg.offset_y = y;
- for (int x = 0; x < size_x; x += step_xy)
- {
- arg.size_x = astc::min(step_xy, size_x - x);
- arg.offset_x = x;
- compute_pixel_region_variance(ctx.context, arg);
- }
- }
- ctx.manage_avg.complete_task_assignment(count);
- }
- delete[] arg.work_memory;
- }
- #endif
- /* See header for documentation. */
- astcenc_error astcenc_compress_image(
- astcenc_context* ctxo,
- astcenc_image* imagep,
- const astcenc_swizzle* swizzle,
- uint8_t* data_out,
- size_t data_len,
- unsigned int thread_index
- ) {
- #if defined(ASTCENC_DECOMPRESS_ONLY)
- (void)ctxo;
- (void)imagep;
- (void)swizzle;
- (void)data_out;
- (void)data_len;
- (void)thread_index;
- return ASTCENC_ERR_BAD_CONTEXT;
- #else
- astcenc_contexti* ctx = &ctxo->context;
- astcenc_error status;
- astcenc_image& image = *imagep;
- if (ctx->config.flags & ASTCENC_FLG_DECOMPRESS_ONLY)
- {
- return ASTCENC_ERR_BAD_CONTEXT;
- }
- status = validate_compression_swizzle(*swizzle);
- if (status != ASTCENC_SUCCESS)
- {
- return status;
- }
- if (thread_index >= ctx->thread_count)
- {
- return ASTCENC_ERR_BAD_PARAM;
- }
- unsigned int block_x = ctx->config.block_x;
- unsigned int block_y = ctx->config.block_y;
- unsigned int block_z = ctx->config.block_z;
- unsigned int xblocks = (image.dim_x + block_x - 1) / block_x;
- unsigned int yblocks = (image.dim_y + block_y - 1) / block_y;
- unsigned int zblocks = (image.dim_z + block_z - 1) / block_z;
- // Check we have enough output space (16 bytes per block)
- size_t size_needed = xblocks * yblocks * zblocks * 16;
- if (data_len < size_needed)
- {
- return ASTCENC_ERR_OUT_OF_MEM;
- }
- // If context thread count is one then implicitly reset
- if (ctx->thread_count == 1)
- {
- astcenc_compress_reset(ctxo);
- }
- if (ctx->config.a_scale_radius != 0)
- {
- // First thread to enter will do setup, other threads will subsequently
- // enter the critical section but simply skip over the initialization
- auto init_avg = [ctx, &image, swizzle]() {
- // Perform memory allocations for the destination buffers
- size_t texel_count = image.dim_x * image.dim_y * image.dim_z;
- ctx->input_alpha_averages = new float[texel_count];
- return init_compute_averages(
- image, ctx->config.a_scale_radius, *swizzle,
- ctx->avg_preprocess_args);
- };
- // Only the first thread actually runs the initializer
- ctxo->manage_avg.init(init_avg);
- // All threads will enter this function and dynamically grab work
- compute_averages(*ctxo, ctx->avg_preprocess_args);
- }
- // Wait for compute_averages to complete before compressing
- ctxo->manage_avg.wait();
- compress_image(*ctxo, thread_index, image, *swizzle, data_out);
- // Wait for compress to complete before freeing memory
- ctxo->manage_compress.wait();
- auto term_compress = [ctx]() {
- delete[] ctx->input_alpha_averages;
- ctx->input_alpha_averages = nullptr;
- };
- // Only the first thread to arrive actually runs the term
- ctxo->manage_compress.term(term_compress);
- return ASTCENC_SUCCESS;
- #endif
- }
- /* See header for documentation. */
- astcenc_error astcenc_compress_reset(
- astcenc_context* ctxo
- ) {
- #if defined(ASTCENC_DECOMPRESS_ONLY)
- (void)ctxo;
- return ASTCENC_ERR_BAD_CONTEXT;
- #else
- astcenc_contexti* ctx = &ctxo->context;
- if (ctx->config.flags & ASTCENC_FLG_DECOMPRESS_ONLY)
- {
- return ASTCENC_ERR_BAD_CONTEXT;
- }
- ctxo->manage_avg.reset();
- ctxo->manage_compress.reset();
- return ASTCENC_SUCCESS;
- #endif
- }
- /* See header for documentation. */
- astcenc_error astcenc_decompress_image(
- astcenc_context* ctxo,
- const uint8_t* data,
- size_t data_len,
- astcenc_image* image_outp,
- const astcenc_swizzle* swizzle,
- unsigned int thread_index
- ) {
- astcenc_error status;
- astcenc_image& image_out = *image_outp;
- astcenc_contexti* ctx = &ctxo->context;
- // Today this doesn't matter (working set on stack) but might in future ...
- if (thread_index >= ctx->thread_count)
- {
- return ASTCENC_ERR_BAD_PARAM;
- }
- status = validate_decompression_swizzle(*swizzle);
- if (status != ASTCENC_SUCCESS)
- {
- return status;
- }
- unsigned int block_x = ctx->config.block_x;
- unsigned int block_y = ctx->config.block_y;
- unsigned int block_z = ctx->config.block_z;
- unsigned int xblocks = (image_out.dim_x + block_x - 1) / block_x;
- unsigned int yblocks = (image_out.dim_y + block_y - 1) / block_y;
- unsigned int zblocks = (image_out.dim_z + block_z - 1) / block_z;
- unsigned int block_count = zblocks * yblocks * xblocks;
- int row_blocks = xblocks;
- int plane_blocks = xblocks * yblocks;
- // Check we have enough output space (16 bytes per block)
- size_t size_needed = xblocks * yblocks * zblocks * 16;
- if (data_len < size_needed)
- {
- return ASTCENC_ERR_OUT_OF_MEM;
- }
- image_block blk {};
- blk.texel_count = static_cast<uint8_t>(block_x * block_y * block_z);
- // Decode mode inferred from the output data type
- blk.decode_unorm8 = image_out.data_type == ASTCENC_TYPE_U8;
- // If context thread count is one then implicitly reset
- if (ctx->thread_count == 1)
- {
- astcenc_decompress_reset(ctxo);
- }
- // Only the first thread actually runs the initializer
- ctxo->manage_decompress.init(block_count, nullptr);
- // All threads run this processing loop until there is no work remaining
- while (true)
- {
- unsigned int count;
- unsigned int base = ctxo->manage_decompress.get_task_assignment(128, count);
- if (!count)
- {
- break;
- }
- for (unsigned int i = base; i < base + count; i++)
- {
- // Decode i into x, y, z block indices
- int z = i / plane_blocks;
- unsigned int rem = i - (z * plane_blocks);
- int y = rem / row_blocks;
- int x = rem - (y * row_blocks);
- unsigned int offset = (((z * yblocks + y) * xblocks) + x) * 16;
- const uint8_t* bp = data + offset;
- symbolic_compressed_block scb;
- physical_to_symbolic(*ctx->bsd, bp, scb);
- decompress_symbolic_block(ctx->config.profile, *ctx->bsd,
- x * block_x, y * block_y, z * block_z,
- scb, blk);
- store_image_block(image_out, blk, *ctx->bsd,
- x * block_x, y * block_y, z * block_z, *swizzle);
- }
- ctxo->manage_decompress.complete_task_assignment(count);
- }
- return ASTCENC_SUCCESS;
- }
- /* See header for documentation. */
- astcenc_error astcenc_decompress_reset(
- astcenc_context* ctxo
- ) {
- ctxo->manage_decompress.reset();
- return ASTCENC_SUCCESS;
- }
- /* See header for documentation. */
- astcenc_error astcenc_get_block_info(
- astcenc_context* ctxo,
- const uint8_t data[16],
- astcenc_block_info* info
- ) {
- #if defined(ASTCENC_DECOMPRESS_ONLY)
- (void)ctxo;
- (void)data;
- (void)info;
- return ASTCENC_ERR_BAD_CONTEXT;
- #else
- astcenc_contexti* ctx = &ctxo->context;
- // Decode the compressed data into a symbolic form
- symbolic_compressed_block scb;
- physical_to_symbolic(*ctx->bsd, data, scb);
- // Fetch the appropriate partition and decimation tables
- block_size_descriptor& bsd = *ctx->bsd;
- // Start from a clean slate
- memset(info, 0, sizeof(*info));
- // Basic info we can always populate
- info->profile = ctx->config.profile;
- info->block_x = ctx->config.block_x;
- info->block_y = ctx->config.block_y;
- info->block_z = ctx->config.block_z;
- info->texel_count = bsd.texel_count;
- // Check for error blocks first
- info->is_error_block = scb.block_type == SYM_BTYPE_ERROR;
- if (info->is_error_block)
- {
- return ASTCENC_SUCCESS;
- }
- // Check for constant color blocks second
- info->is_constant_block = scb.block_type == SYM_BTYPE_CONST_F16 ||
- scb.block_type == SYM_BTYPE_CONST_U16;
- if (info->is_constant_block)
- {
- return ASTCENC_SUCCESS;
- }
- // Otherwise handle a full block ; known to be valid after conditions above have been checked
- int partition_count = scb.partition_count;
- const auto& pi = bsd.get_partition_info(partition_count, scb.partition_index);
- const block_mode& bm = bsd.get_block_mode(scb.block_mode);
- const decimation_info& di = bsd.get_decimation_info(bm.decimation_mode);
- info->weight_x = di.weight_x;
- info->weight_y = di.weight_y;
- info->weight_z = di.weight_z;
- info->is_dual_plane_block = bm.is_dual_plane != 0;
- info->partition_count = scb.partition_count;
- info->partition_index = scb.partition_index;
- info->dual_plane_component = scb.plane2_component;
- info->color_level_count = get_quant_level(scb.get_color_quant_mode());
- info->weight_level_count = get_quant_level(bm.get_weight_quant_mode());
- // Unpack color endpoints for each active partition
- for (unsigned int i = 0; i < scb.partition_count; i++)
- {
- bool rgb_hdr;
- bool a_hdr;
- vint4 endpnt[2];
- unpack_color_endpoints(ctx->config.profile,
- scb.color_formats[i],
- scb.color_values[i],
- rgb_hdr, a_hdr,
- endpnt[0], endpnt[1]);
- // Store the color endpoint mode info
- info->color_endpoint_modes[i] = scb.color_formats[i];
- info->is_hdr_block = info->is_hdr_block || rgb_hdr || a_hdr;
- // Store the unpacked and decoded color endpoint
- vmask4 hdr_mask(rgb_hdr, rgb_hdr, rgb_hdr, a_hdr);
- for (int j = 0; j < 2; j++)
- {
- vint4 color_lns = lns_to_sf16(endpnt[j]);
- vint4 color_unorm = unorm16_to_sf16(endpnt[j]);
- vint4 datai = select(color_unorm, color_lns, hdr_mask);
- store(float16_to_float(datai), info->color_endpoints[i][j]);
- }
- }
- // Unpack weights for each texel
- int weight_plane1[BLOCK_MAX_TEXELS];
- int weight_plane2[BLOCK_MAX_TEXELS];
- unpack_weights(bsd, scb, di, bm.is_dual_plane, weight_plane1, weight_plane2);
- for (unsigned int i = 0; i < bsd.texel_count; i++)
- {
- info->weight_values_plane1[i] = static_cast<float>(weight_plane1[i]) * (1.0f / WEIGHTS_TEXEL_SUM);
- if (info->is_dual_plane_block)
- {
- info->weight_values_plane2[i] = static_cast<float>(weight_plane2[i]) * (1.0f / WEIGHTS_TEXEL_SUM);
- }
- }
- // Unpack partition assignments for each texel
- for (unsigned int i = 0; i < bsd.texel_count; i++)
- {
- info->partition_assignment[i] = pi.partition_of_texel[i];
- }
- return ASTCENC_SUCCESS;
- #endif
- }
- /* See header for documentation. */
- const char* astcenc_get_error_string(
- astcenc_error status
- ) {
- // Values in this enum are from an external user, so not guaranteed to be
- // bounded to the enum values
- switch (static_cast<int>(status))
- {
- case ASTCENC_SUCCESS:
- return "ASTCENC_SUCCESS";
- case ASTCENC_ERR_OUT_OF_MEM:
- return "ASTCENC_ERR_OUT_OF_MEM";
- case ASTCENC_ERR_BAD_CPU_FLOAT:
- return "ASTCENC_ERR_BAD_CPU_FLOAT";
- case ASTCENC_ERR_BAD_PARAM:
- return "ASTCENC_ERR_BAD_PARAM";
- case ASTCENC_ERR_BAD_BLOCK_SIZE:
- return "ASTCENC_ERR_BAD_BLOCK_SIZE";
- case ASTCENC_ERR_BAD_PROFILE:
- return "ASTCENC_ERR_BAD_PROFILE";
- case ASTCENC_ERR_BAD_QUALITY:
- return "ASTCENC_ERR_BAD_QUALITY";
- case ASTCENC_ERR_BAD_FLAGS:
- return "ASTCENC_ERR_BAD_FLAGS";
- case ASTCENC_ERR_BAD_SWIZZLE:
- return "ASTCENC_ERR_BAD_SWIZZLE";
- case ASTCENC_ERR_BAD_CONTEXT:
- return "ASTCENC_ERR_BAD_CONTEXT";
- case ASTCENC_ERR_NOT_IMPLEMENTED:
- return "ASTCENC_ERR_NOT_IMPLEMENTED";
- case ASTCENC_ERR_BAD_DECODE_MODE:
- return "ASTCENC_ERR_BAD_DECODE_MODE";
- #if defined(ASTCENC_DIAGNOSTICS)
- case ASTCENC_ERR_DTRACE_FAILURE:
- return "ASTCENC_ERR_DTRACE_FAILURE";
- #endif
- default:
- return nullptr;
- }
- }
|