Path: blob/master/thirdparty/astcenc/astcenc_ideal_endpoints_and_weights.cpp
9896 views
// SPDX-License-Identifier: Apache-2.01// ----------------------------------------------------------------------------2// Copyright 2011-2024 Arm Limited3//4// Licensed under the Apache License, Version 2.0 (the "License"); you may not5// use this file except in compliance with the License. You may obtain a copy6// of the License at:7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing, software11// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT12// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the13// License for the specific language governing permissions and limitations14// under the License.15// ----------------------------------------------------------------------------1617#if !defined(ASTCENC_DECOMPRESS_ONLY)1819/**20* @brief Functions for computing color endpoints and texel weights.21*/2223#include <cassert>2425#include "astcenc_internal.h"26#include "astcenc_vecmathlib.h"2728/**29* @brief Compute the infilled weight for N texel indices in a decimated grid.30*31* @param di The weight grid decimation to use.32* @param weights The decimated weight values to use.33* @param index The first texel index to interpolate.34*35* @return The interpolated weight for the given set of SIMD_WIDTH texels.36*/37static vfloat bilinear_infill_vla(38const decimation_info& di,39const float* weights,40unsigned int index41) {42// Load the bilinear filter texel weight indexes in the decimated grid43const uint8_t* weight_idx0 = di.texel_weights_tr[0] + index;44const uint8_t* weight_idx1 = di.texel_weights_tr[1] + index;45const uint8_t* weight_idx2 = di.texel_weights_tr[2] + index;46const uint8_t* weight_idx3 = di.texel_weights_tr[3] + index;4748// Load the bilinear filter weights from the decimated grid49vfloat weight_val0 = gatherf_byte_inds<vfloat>(weights, weight_idx0);50vfloat weight_val1 = gatherf_byte_inds<vfloat>(weights, weight_idx1);51vfloat weight_val2 = gatherf_byte_inds<vfloat>(weights, weight_idx2);52vfloat weight_val3 = gatherf_byte_inds<vfloat>(weights, weight_idx3);5354// Load the weight contribution factors for each decimated weight55vfloat tex_weight_float0 = loada(di.texel_weight_contribs_float_tr[0] + index);56vfloat tex_weight_float1 = loada(di.texel_weight_contribs_float_tr[1] + index);57vfloat tex_weight_float2 = loada(di.texel_weight_contribs_float_tr[2] + index);58vfloat tex_weight_float3 = loada(di.texel_weight_contribs_float_tr[3] + index);5960// Compute the bilinear interpolation to generate the per-texel weight61return (weight_val0 * tex_weight_float0 + weight_val1 * tex_weight_float1) +62(weight_val2 * tex_weight_float2 + weight_val3 * tex_weight_float3);63}6465/**66* @brief Compute the infilled weight for N texel indices in a decimated grid.67*68* This is specialized version which computes only two weights per texel for69* encodings that are only decimated in a single axis.70*71* @param di The weight grid decimation to use.72* @param weights The decimated weight values to use.73* @param index The first texel index to interpolate.74*75* @return The interpolated weight for the given set of SIMD_WIDTH texels.76*/77static vfloat bilinear_infill_vla_2(78const decimation_info& di,79const float* weights,80unsigned int index81) {82// Load the bilinear filter texel weight indexes in the decimated grid83const uint8_t* weight_idx0 = di.texel_weights_tr[0] + index;84const uint8_t* weight_idx1 = di.texel_weights_tr[1] + index;8586// Load the bilinear filter weights from the decimated grid87vfloat weight_val0 = gatherf_byte_inds<vfloat>(weights, weight_idx0);88vfloat weight_val1 = gatherf_byte_inds<vfloat>(weights, weight_idx1);8990// Load the weight contribution factors for each decimated weight91vfloat tex_weight_float0 = loada(di.texel_weight_contribs_float_tr[0] + index);92vfloat tex_weight_float1 = loada(di.texel_weight_contribs_float_tr[1] + index);9394// Compute the bilinear interpolation to generate the per-texel weight95return (weight_val0 * tex_weight_float0 + weight_val1 * tex_weight_float1);96}9798/**99* @brief Compute the ideal endpoints and weights for 1 color component.100*101* @param blk The image block color data to compress.102* @param pi The partition info for the current trial.103* @param[out] ei The computed ideal endpoints and weights.104* @param component The color component to compute.105*/106static void compute_ideal_colors_and_weights_1_comp(107const image_block& blk,108const partition_info& pi,109endpoints_and_weights& ei,110unsigned int component111) {112unsigned int partition_count = pi.partition_count;113ei.ep.partition_count = partition_count;114promise(partition_count > 0);115116unsigned int texel_count = blk.texel_count;117promise(texel_count > 0);118119float error_weight;120const float* data_vr = nullptr;121122assert(component < BLOCK_MAX_COMPONENTS);123switch (component)124{125case 0:126error_weight = blk.channel_weight.lane<0>();127data_vr = blk.data_r;128break;129case 1:130error_weight = blk.channel_weight.lane<1>();131data_vr = blk.data_g;132break;133case 2:134error_weight = blk.channel_weight.lane<2>();135data_vr = blk.data_b;136break;137default:138assert(component == 3);139error_weight = blk.channel_weight.lane<3>();140data_vr = blk.data_a;141break;142}143144vmask4 sep_mask = vint4::lane_id() == vint4(component);145bool is_constant_wes { true };146float partition0_len_sq { 0.0f };147148for (unsigned int i = 0; i < partition_count; i++)149{150float lowvalue { 1e10f };151float highvalue { -1e10f };152153unsigned int partition_texel_count = pi.partition_texel_count[i];154for (unsigned int j = 0; j < partition_texel_count; j++)155{156unsigned int tix = pi.texels_of_partition[i][j];157float value = data_vr[tix];158lowvalue = astc::min(value, lowvalue);159highvalue = astc::max(value, highvalue);160}161162if (highvalue <= lowvalue)163{164lowvalue = 0.0f;165highvalue = 1e-7f;166}167168float length = highvalue - lowvalue;169float length_squared = length * length;170float scale = 1.0f / length;171172if (i == 0)173{174partition0_len_sq = length_squared;175}176else177{178is_constant_wes = is_constant_wes && length_squared == partition0_len_sq;179}180181for (unsigned int j = 0; j < partition_texel_count; j++)182{183unsigned int tix = pi.texels_of_partition[i][j];184float value = (data_vr[tix] - lowvalue) * scale;185value = astc::clamp1f(value);186187ei.weights[tix] = value;188ei.weight_error_scale[tix] = length_squared * error_weight;189assert(!astc::isnan(ei.weight_error_scale[tix]));190}191192ei.ep.endpt0[i] = select(blk.data_min, vfloat4(lowvalue), sep_mask);193ei.ep.endpt1[i] = select(blk.data_max, vfloat4(highvalue), sep_mask);194}195196// Zero initialize any SIMD over-fetch197size_t texel_count_simd = round_up_to_simd_multiple_vla(texel_count);198for (size_t i = texel_count; i < texel_count_simd; i++)199{200ei.weights[i] = 0.0f;201ei.weight_error_scale[i] = 0.0f;202}203204ei.is_constant_weight_error_scale = is_constant_wes;205}206207/**208* @brief Compute the ideal endpoints and weights for 2 color components.209*210* @param blk The image block color data to compress.211* @param pi The partition info for the current trial.212* @param[out] ei The computed ideal endpoints and weights.213* @param component1 The first color component to compute.214* @param component2 The second color component to compute.215*/216static void compute_ideal_colors_and_weights_2_comp(217const image_block& blk,218const partition_info& pi,219endpoints_and_weights& ei,220int component1,221int component2222) {223unsigned int partition_count = pi.partition_count;224ei.ep.partition_count = partition_count;225promise(partition_count > 0);226227unsigned int texel_count = blk.texel_count;228promise(texel_count > 0);229230partition_metrics pms[BLOCK_MAX_PARTITIONS];231232float error_weight;233const float* data_vr = nullptr;234const float* data_vg = nullptr;235236if (component1 == 0 && component2 == 1)237{238error_weight = hadd_s(blk.channel_weight.swz<0, 1>()) / 2.0f;239240data_vr = blk.data_r;241data_vg = blk.data_g;242}243else if (component1 == 0 && component2 == 2)244{245error_weight = hadd_s(blk.channel_weight.swz<0, 2>()) / 2.0f;246247data_vr = blk.data_r;248data_vg = blk.data_b;249}250else // (component1 == 1 && component2 == 2)251{252assert(component1 == 1 && component2 == 2);253254error_weight = hadd_s(blk.channel_weight.swz<1, 2>()) / 2.0f;255256data_vr = blk.data_g;257data_vg = blk.data_b;258}259260compute_avgs_and_dirs_2_comp(pi, blk, component1, component2, pms);261262bool is_constant_wes { true };263float partition0_len_sq { 0.0f };264265vmask4 comp1_mask = vint4::lane_id() == vint4(component1);266vmask4 comp2_mask = vint4::lane_id() == vint4(component2);267268for (unsigned int i = 0; i < partition_count; i++)269{270vfloat4 dir = pms[i].dir;271if (hadd_s(dir) < 0.0f)272{273dir = vfloat4::zero() - dir;274}275276line2 line { pms[i].avg, normalize_safe(dir, unit2()) };277float lowparam { 1e10f };278float highparam { -1e10f };279280unsigned int partition_texel_count = pi.partition_texel_count[i];281for (unsigned int j = 0; j < partition_texel_count; j++)282{283unsigned int tix = pi.texels_of_partition[i][j];284vfloat4 point = vfloat2(data_vr[tix], data_vg[tix]);285float param = dot_s(point - line.a, line.b);286ei.weights[tix] = param;287288lowparam = astc::min(param, lowparam);289highparam = astc::max(param, highparam);290}291292// It is possible for a uniform-color partition to produce length=0;293// this causes NaN issues so set to small value to avoid this problem294if (highparam <= lowparam)295{296lowparam = 0.0f;297highparam = 1e-7f;298}299300float length = highparam - lowparam;301float length_squared = length * length;302float scale = 1.0f / length;303304if (i == 0)305{306partition0_len_sq = length_squared;307}308else309{310is_constant_wes = is_constant_wes && length_squared == partition0_len_sq;311}312313for (unsigned int j = 0; j < partition_texel_count; j++)314{315unsigned int tix = pi.texels_of_partition[i][j];316float idx = (ei.weights[tix] - lowparam) * scale;317idx = astc::clamp1f(idx);318319ei.weights[tix] = idx;320ei.weight_error_scale[tix] = length_squared * error_weight;321assert(!astc::isnan(ei.weight_error_scale[tix]));322}323324vfloat4 lowvalue = line.a + line.b * lowparam;325vfloat4 highvalue = line.a + line.b * highparam;326327vfloat4 ep0 = select(blk.data_min, vfloat4(lowvalue.lane<0>()), comp1_mask);328vfloat4 ep1 = select(blk.data_max, vfloat4(highvalue.lane<0>()), comp1_mask);329330ei.ep.endpt0[i] = select(ep0, vfloat4(lowvalue.lane<1>()), comp2_mask);331ei.ep.endpt1[i] = select(ep1, vfloat4(highvalue.lane<1>()), comp2_mask);332}333334// Zero initialize any SIMD over-fetch335size_t texel_count_simd = round_up_to_simd_multiple_vla(texel_count);336for (size_t i = texel_count; i < texel_count_simd; i++)337{338ei.weights[i] = 0.0f;339ei.weight_error_scale[i] = 0.0f;340}341342ei.is_constant_weight_error_scale = is_constant_wes;343}344345/**346* @brief Compute the ideal endpoints and weights for 3 color components.347*348* @param blk The image block color data to compress.349* @param pi The partition info for the current trial.350* @param[out] ei The computed ideal endpoints and weights.351* @param omitted_component The color component excluded from the calculation.352*/353static void compute_ideal_colors_and_weights_3_comp(354const image_block& blk,355const partition_info& pi,356endpoints_and_weights& ei,357unsigned int omitted_component358) {359unsigned int partition_count = pi.partition_count;360ei.ep.partition_count = partition_count;361promise(partition_count > 0);362363unsigned int texel_count = blk.texel_count;364promise(texel_count > 0);365366partition_metrics pms[BLOCK_MAX_PARTITIONS];367368float error_weight;369const float* data_vr = nullptr;370const float* data_vg = nullptr;371const float* data_vb = nullptr;372if (omitted_component == 0)373{374error_weight = hadd_s(blk.channel_weight.swz<0, 1, 2>());375data_vr = blk.data_g;376data_vg = blk.data_b;377data_vb = blk.data_a;378}379else if (omitted_component == 1)380{381error_weight = hadd_s(blk.channel_weight.swz<0, 2, 3>());382data_vr = blk.data_r;383data_vg = blk.data_b;384data_vb = blk.data_a;385}386else if (omitted_component == 2)387{388error_weight = hadd_s(blk.channel_weight.swz<0, 1, 3>());389data_vr = blk.data_r;390data_vg = blk.data_g;391data_vb = blk.data_a;392}393else394{395assert(omitted_component == 3);396397error_weight = hadd_s(blk.channel_weight.swz<0, 1, 2>());398data_vr = blk.data_r;399data_vg = blk.data_g;400data_vb = blk.data_b;401}402403error_weight = error_weight * (1.0f / 3.0f);404405if (omitted_component == 3)406{407compute_avgs_and_dirs_3_comp_rgb(pi, blk, pms);408}409else410{411compute_avgs_and_dirs_3_comp(pi, blk, omitted_component, pms);412}413414bool is_constant_wes { true };415float partition0_len_sq { 0.0f };416417for (unsigned int i = 0; i < partition_count; i++)418{419vfloat4 dir = pms[i].dir;420if (hadd_rgb_s(dir) < 0.0f)421{422dir = vfloat4::zero() - dir;423}424425line3 line { pms[i].avg, normalize_safe(dir, unit3()) };426float lowparam { 1e10f };427float highparam { -1e10f };428429unsigned int partition_texel_count = pi.partition_texel_count[i];430for (unsigned int j = 0; j < partition_texel_count; j++)431{432unsigned int tix = pi.texels_of_partition[i][j];433vfloat4 point = vfloat3(data_vr[tix], data_vg[tix], data_vb[tix]);434float param = dot3_s(point - line.a, line.b);435ei.weights[tix] = param;436437lowparam = astc::min(param, lowparam);438highparam = astc::max(param, highparam);439}440441// It is possible for a uniform-color partition to produce length=0;442// this causes NaN issues so set to small value to avoid this problem443if (highparam <= lowparam)444{445lowparam = 0.0f;446highparam = 1e-7f;447}448449float length = highparam - lowparam;450float length_squared = length * length;451float scale = 1.0f / length;452453if (i == 0)454{455partition0_len_sq = length_squared;456}457else458{459is_constant_wes = is_constant_wes && length_squared == partition0_len_sq;460}461462for (unsigned int j = 0; j < partition_texel_count; j++)463{464unsigned int tix = pi.texels_of_partition[i][j];465float idx = (ei.weights[tix] - lowparam) * scale;466idx = astc::clamp1f(idx);467468ei.weights[tix] = idx;469ei.weight_error_scale[tix] = length_squared * error_weight;470assert(!astc::isnan(ei.weight_error_scale[tix]));471}472473vfloat4 ep0 = line.a + line.b * lowparam;474vfloat4 ep1 = line.a + line.b * highparam;475476vfloat4 bmin = blk.data_min;477vfloat4 bmax = blk.data_max;478479assert(omitted_component < BLOCK_MAX_COMPONENTS);480switch (omitted_component)481{482case 0:483ei.ep.endpt0[i] = vfloat4(bmin.lane<0>(), ep0.lane<0>(), ep0.lane<1>(), ep0.lane<2>());484ei.ep.endpt1[i] = vfloat4(bmax.lane<0>(), ep1.lane<0>(), ep1.lane<1>(), ep1.lane<2>());485break;486case 1:487ei.ep.endpt0[i] = vfloat4(ep0.lane<0>(), bmin.lane<1>(), ep0.lane<1>(), ep0.lane<2>());488ei.ep.endpt1[i] = vfloat4(ep1.lane<0>(), bmax.lane<1>(), ep1.lane<1>(), ep1.lane<2>());489break;490case 2:491ei.ep.endpt0[i] = vfloat4(ep0.lane<0>(), ep0.lane<1>(), bmin.lane<2>(), ep0.lane<2>());492ei.ep.endpt1[i] = vfloat4(ep1.lane<0>(), ep1.lane<1>(), bmax.lane<2>(), ep1.lane<2>());493break;494default:495ei.ep.endpt0[i] = vfloat4(ep0.lane<0>(), ep0.lane<1>(), ep0.lane<2>(), bmin.lane<3>());496ei.ep.endpt1[i] = vfloat4(ep1.lane<0>(), ep1.lane<1>(), ep1.lane<2>(), bmax.lane<3>());497break;498}499}500501// Zero initialize any SIMD over-fetch502size_t texel_count_simd = round_up_to_simd_multiple_vla(texel_count);503for (size_t i = texel_count; i < texel_count_simd; i++)504{505ei.weights[i] = 0.0f;506ei.weight_error_scale[i] = 0.0f;507}508509ei.is_constant_weight_error_scale = is_constant_wes;510}511512/**513* @brief Compute the ideal endpoints and weights for 4 color components.514*515* @param blk The image block color data to compress.516* @param pi The partition info for the current trial.517* @param[out] ei The computed ideal endpoints and weights.518*/519static void compute_ideal_colors_and_weights_4_comp(520const image_block& blk,521const partition_info& pi,522endpoints_and_weights& ei523) {524const float error_weight = hadd_s(blk.channel_weight) / 4.0f;525526unsigned int partition_count = pi.partition_count;527528unsigned int texel_count = blk.texel_count;529promise(texel_count > 0);530promise(partition_count > 0);531532partition_metrics pms[BLOCK_MAX_PARTITIONS];533534compute_avgs_and_dirs_4_comp(pi, blk, pms);535536bool is_constant_wes { true };537float partition0_len_sq { 0.0f };538539for (unsigned int i = 0; i < partition_count; i++)540{541vfloat4 dir = pms[i].dir;542if (hadd_rgb_s(dir) < 0.0f)543{544dir = vfloat4::zero() - dir;545}546547line4 line { pms[i].avg, normalize_safe(dir, unit4()) };548float lowparam { 1e10f };549float highparam { -1e10f };550551unsigned int partition_texel_count = pi.partition_texel_count[i];552for (unsigned int j = 0; j < partition_texel_count; j++)553{554unsigned int tix = pi.texels_of_partition[i][j];555vfloat4 point = blk.texel(tix);556float param = dot_s(point - line.a, line.b);557ei.weights[tix] = param;558559lowparam = astc::min(param, lowparam);560highparam = astc::max(param, highparam);561}562563// It is possible for a uniform-color partition to produce length=0;564// this causes NaN issues so set to small value to avoid this problem565if (highparam <= lowparam)566{567lowparam = 0.0f;568highparam = 1e-7f;569}570571float length = highparam - lowparam;572float length_squared = length * length;573float scale = 1.0f / length;574575if (i == 0)576{577partition0_len_sq = length_squared;578}579else580{581is_constant_wes = is_constant_wes && length_squared == partition0_len_sq;582}583584ei.ep.endpt0[i] = line.a + line.b * lowparam;585ei.ep.endpt1[i] = line.a + line.b * highparam;586587for (unsigned int j = 0; j < partition_texel_count; j++)588{589unsigned int tix = pi.texels_of_partition[i][j];590float idx = (ei.weights[tix] - lowparam) * scale;591idx = astc::clamp1f(idx);592593ei.weights[tix] = idx;594ei.weight_error_scale[tix] = length_squared * error_weight;595assert(!astc::isnan(ei.weight_error_scale[tix]));596}597}598599// Zero initialize any SIMD over-fetch600size_t texel_count_simd = round_up_to_simd_multiple_vla(texel_count);601for (size_t i = texel_count; i < texel_count_simd; i++)602{603ei.weights[i] = 0.0f;604ei.weight_error_scale[i] = 0.0f;605}606607ei.is_constant_weight_error_scale = is_constant_wes;608}609610/* See header for documentation. */611void compute_ideal_colors_and_weights_1plane(612const image_block& blk,613const partition_info& pi,614endpoints_and_weights& ei615) {616bool uses_alpha = !blk.is_constant_channel(3);617618if (uses_alpha)619{620compute_ideal_colors_and_weights_4_comp(blk, pi, ei);621}622else623{624compute_ideal_colors_and_weights_3_comp(blk, pi, ei, 3);625}626}627628/* See header for documentation. */629void compute_ideal_colors_and_weights_2planes(630const block_size_descriptor& bsd,631const image_block& blk,632unsigned int plane2_component,633endpoints_and_weights& ei1,634endpoints_and_weights& ei2635) {636const auto& pi = bsd.get_partition_info(1, 0);637bool uses_alpha = !blk.is_constant_channel(3);638639assert(plane2_component < BLOCK_MAX_COMPONENTS);640switch (plane2_component)641{642case 0: // Separate weights for red643if (uses_alpha)644{645compute_ideal_colors_and_weights_3_comp(blk, pi, ei1, 0);646}647else648{649compute_ideal_colors_and_weights_2_comp(blk, pi, ei1, 1, 2);650}651compute_ideal_colors_and_weights_1_comp(blk, pi, ei2, 0);652break;653654case 1: // Separate weights for green655if (uses_alpha)656{657compute_ideal_colors_and_weights_3_comp(blk, pi, ei1, 1);658}659else660{661compute_ideal_colors_and_weights_2_comp(blk, pi, ei1, 0, 2);662}663compute_ideal_colors_and_weights_1_comp(blk, pi, ei2, 1);664break;665666case 2: // Separate weights for blue667if (uses_alpha)668{669compute_ideal_colors_and_weights_3_comp(blk, pi, ei1, 2);670}671else672{673compute_ideal_colors_and_weights_2_comp(blk, pi, ei1, 0, 1);674}675compute_ideal_colors_and_weights_1_comp(blk, pi, ei2, 2);676break;677678default: // Separate weights for alpha679assert(uses_alpha);680compute_ideal_colors_and_weights_3_comp(blk, pi, ei1, 3);681compute_ideal_colors_and_weights_1_comp(blk, pi, ei2, 3);682break;683}684}685686/* See header for documentation. */687float compute_error_of_weight_set_1plane(688const endpoints_and_weights& eai,689const decimation_info& di,690const float* dec_weight_quant_uvalue691) {692vfloatacc error_summav = vfloatacc::zero();693unsigned int texel_count = di.texel_count;694promise(texel_count > 0);695696// Process SIMD-width chunks, safe to over-fetch - the extra space is zero initialized697if (di.max_texel_weight_count > 2)698{699for (unsigned int i = 0; i < texel_count; i += ASTCENC_SIMD_WIDTH)700{701// Compute the bilinear interpolation of the decimated weight grid702vfloat current_values = bilinear_infill_vla(di, dec_weight_quant_uvalue, i);703704// Compute the error between the computed value and the ideal weight705vfloat actual_values = loada(eai.weights + i);706vfloat diff = current_values - actual_values;707vfloat significance = loada(eai.weight_error_scale + i);708vfloat error = diff * diff * significance;709710haccumulate(error_summav, error);711}712}713else if (di.max_texel_weight_count > 1)714{715for (unsigned int i = 0; i < texel_count; i += ASTCENC_SIMD_WIDTH)716{717// Compute the bilinear interpolation of the decimated weight grid718vfloat current_values = bilinear_infill_vla_2(di, dec_weight_quant_uvalue, i);719720// Compute the error between the computed value and the ideal weight721vfloat actual_values = loada(eai.weights + i);722vfloat diff = current_values - actual_values;723vfloat significance = loada(eai.weight_error_scale + i);724vfloat error = diff * diff * significance;725726haccumulate(error_summav, error);727}728}729else730{731for (unsigned int i = 0; i < texel_count; i += ASTCENC_SIMD_WIDTH)732{733// Load the weight set directly, without interpolation734vfloat current_values = loada(dec_weight_quant_uvalue + i);735736// Compute the error between the computed value and the ideal weight737vfloat actual_values = loada(eai.weights + i);738vfloat diff = current_values - actual_values;739vfloat significance = loada(eai.weight_error_scale + i);740vfloat error = diff * diff * significance;741742haccumulate(error_summav, error);743}744}745746// Resolve the final scalar accumulator sum747return hadd_s(error_summav);748}749750/* See header for documentation. */751float compute_error_of_weight_set_2planes(752const endpoints_and_weights& eai1,753const endpoints_and_weights& eai2,754const decimation_info& di,755const float* dec_weight_quant_uvalue_plane1,756const float* dec_weight_quant_uvalue_plane2757) {758vfloatacc error_summav = vfloatacc::zero();759unsigned int texel_count = di.texel_count;760promise(texel_count > 0);761762// Process SIMD-width chunks, safe to over-fetch - the extra space is zero initialized763if (di.max_texel_weight_count > 2)764{765for (unsigned int i = 0; i < texel_count; i += ASTCENC_SIMD_WIDTH)766{767// Plane 1768// Compute the bilinear interpolation of the decimated weight grid769vfloat current_values1 = bilinear_infill_vla(di, dec_weight_quant_uvalue_plane1, i);770771// Compute the error between the computed value and the ideal weight772vfloat actual_values1 = loada(eai1.weights + i);773vfloat diff = current_values1 - actual_values1;774vfloat error1 = diff * diff * loada(eai1.weight_error_scale + i);775776// Plane 2777// Compute the bilinear interpolation of the decimated weight grid778vfloat current_values2 = bilinear_infill_vla(di, dec_weight_quant_uvalue_plane2, i);779780// Compute the error between the computed value and the ideal weight781vfloat actual_values2 = loada(eai2.weights + i);782diff = current_values2 - actual_values2;783vfloat error2 = diff * diff * loada(eai2.weight_error_scale + i);784785haccumulate(error_summav, error1 + error2);786}787}788else if (di.max_texel_weight_count > 1)789{790for (unsigned int i = 0; i < texel_count; i += ASTCENC_SIMD_WIDTH)791{792// Plane 1793// Compute the bilinear interpolation of the decimated weight grid794vfloat current_values1 = bilinear_infill_vla_2(di, dec_weight_quant_uvalue_plane1, i);795796// Compute the error between the computed value and the ideal weight797vfloat actual_values1 = loada(eai1.weights + i);798vfloat diff = current_values1 - actual_values1;799vfloat error1 = diff * diff * loada(eai1.weight_error_scale + i);800801// Plane 2802// Compute the bilinear interpolation of the decimated weight grid803vfloat current_values2 = bilinear_infill_vla_2(di, dec_weight_quant_uvalue_plane2, i);804805// Compute the error between the computed value and the ideal weight806vfloat actual_values2 = loada(eai2.weights + i);807diff = current_values2 - actual_values2;808vfloat error2 = diff * diff * loada(eai2.weight_error_scale + i);809810haccumulate(error_summav, error1 + error2);811}812}813else814{815for (unsigned int i = 0; i < texel_count; i += ASTCENC_SIMD_WIDTH)816{817// Plane 1818// Load the weight set directly, without interpolation819vfloat current_values1 = loada(dec_weight_quant_uvalue_plane1 + i);820821// Compute the error between the computed value and the ideal weight822vfloat actual_values1 = loada(eai1.weights + i);823vfloat diff = current_values1 - actual_values1;824vfloat error1 = diff * diff * loada(eai1.weight_error_scale + i);825826// Plane 2827// Load the weight set directly, without interpolation828vfloat current_values2 = loada(dec_weight_quant_uvalue_plane2 + i);829830// Compute the error between the computed value and the ideal weight831vfloat actual_values2 = loada(eai2.weights + i);832diff = current_values2 - actual_values2;833vfloat error2 = diff * diff * loada(eai2.weight_error_scale + i);834835haccumulate(error_summav, error1 + error2);836}837}838839// Resolve the final scalar accumulator sum840return hadd_s(error_summav);841}842843/* See header for documentation. */844void compute_ideal_weights_for_decimation(845const endpoints_and_weights& ei,846const decimation_info& di,847float* dec_weight_ideal_value848) {849unsigned int texel_count = di.texel_count;850unsigned int weight_count = di.weight_count;851bool is_direct = texel_count == weight_count;852promise(texel_count > 0);853promise(weight_count > 0);854855// If we have a 1:1 mapping just shortcut the computation. Transfer enough to also copy the856// zero-initialized SIMD over-fetch region857if (is_direct)858{859for (unsigned int i = 0; i < texel_count; i += ASTCENC_SIMD_WIDTH)860{861vfloat weight(ei.weights + i);862storea(weight, dec_weight_ideal_value + i);863}864865return;866}867868// Otherwise compute an estimate and perform single refinement iteration869870// Compute an initial average for each decimated weight871bool constant_wes = ei.is_constant_weight_error_scale;872vfloat weight_error_scale(ei.weight_error_scale[0]);873874// This overshoots - this is OK as we initialize the array tails in the875// decimation table structures to safe values ...876for (unsigned int i = 0; i < weight_count; i += ASTCENC_SIMD_WIDTH)877{878// Start with a small value to avoid div-by-zero later879vfloat weight_weight(1e-10f);880vfloat initial_weight = vfloat::zero();881882// Accumulate error weighting of all the texels using this weight883vint weight_texel_count(di.weight_texel_count + i);884unsigned int max_texel_count = hmax_s(weight_texel_count);885promise(max_texel_count > 0);886887for (unsigned int j = 0; j < max_texel_count; j++)888{889const uint8_t* texel = di.weight_texels_tr[j] + i;890vfloat weight = loada(di.weights_texel_contribs_tr[j] + i);891892if (!constant_wes)893{894weight_error_scale = gatherf_byte_inds<vfloat>(ei.weight_error_scale, texel);895}896897vfloat contrib_weight = weight * weight_error_scale;898899weight_weight += contrib_weight;900initial_weight += gatherf_byte_inds<vfloat>(ei.weights, texel) * contrib_weight;901}902903storea(initial_weight / weight_weight, dec_weight_ideal_value + i);904}905906// Populate the interpolated weight grid based on the initial average907// Process SIMD-width texel coordinates at at time while we can. Safe to908// over-process full SIMD vectors - the tail is zeroed.909ASTCENC_ALIGNAS float infilled_weights[BLOCK_MAX_TEXELS];910if (di.max_texel_weight_count <= 2)911{912for (unsigned int i = 0; i < texel_count; i += ASTCENC_SIMD_WIDTH)913{914vfloat weight = bilinear_infill_vla_2(di, dec_weight_ideal_value, i);915storea(weight, infilled_weights + i);916}917}918else919{920for (unsigned int i = 0; i < texel_count; i += ASTCENC_SIMD_WIDTH)921{922vfloat weight = bilinear_infill_vla(di, dec_weight_ideal_value, i);923storea(weight, infilled_weights + i);924}925}926927// Perform a single iteration of refinement928// Empirically determined step size; larger values don't help but smaller drops image quality929constexpr float stepsize = 0.25f;930constexpr float chd_scale = -WEIGHTS_TEXEL_SUM;931932for (unsigned int i = 0; i < weight_count; i += ASTCENC_SIMD_WIDTH)933{934vfloat weight_val = loada(dec_weight_ideal_value + i);935936// Accumulate error weighting of all the texels using this weight937// Start with a small value to avoid div-by-zero later938vfloat error_change0(1e-10f);939vfloat error_change1(0.0f);940941// Accumulate error weighting of all the texels using this weight942vint weight_texel_count(di.weight_texel_count + i);943unsigned int max_texel_count = hmax_s(weight_texel_count);944promise(max_texel_count > 0);945946for (unsigned int j = 0; j < max_texel_count; j++)947{948const uint8_t* texel = di.weight_texels_tr[j] + i;949vfloat contrib_weight = loada(di.weights_texel_contribs_tr[j] + i);950951if (!constant_wes)952{953weight_error_scale = gatherf_byte_inds<vfloat>(ei.weight_error_scale, texel);954}955956vfloat scale = weight_error_scale * contrib_weight;957vfloat old_weight = gatherf_byte_inds<vfloat>(infilled_weights, texel);958vfloat ideal_weight = gatherf_byte_inds<vfloat>(ei.weights, texel);959960error_change0 += contrib_weight * scale;961error_change1 += (old_weight - ideal_weight) * scale;962}963964vfloat step = (error_change1 * chd_scale) / error_change0;965step = clamp(-stepsize, stepsize, step);966967// Update the weight; note this can store negative values968storea(weight_val + step, dec_weight_ideal_value + i);969}970}971972/* See header for documentation. */973void compute_quantized_weights_for_decimation(974const decimation_info& di,975float low_bound,976float high_bound,977const float* dec_weight_ideal_value,978float* weight_set_out,979uint8_t* quantized_weight_set,980quant_method quant_level981) {982int weight_count = di.weight_count;983promise(weight_count > 0);984const quant_and_transfer_table& qat = quant_and_xfer_tables[quant_level];985986// The available quant levels, stored with a minus 1 bias987static const float quant_levels_m1[12] {9881.0f, 2.0f, 3.0f, 4.0f, 5.0f, 7.0f, 9.0f, 11.0f, 15.0f, 19.0f, 23.0f, 31.0f989};990991vint steps_m1(get_quant_level(quant_level) - 1);992float quant_level_m1 = quant_levels_m1[quant_level];993994// Quantize the weight set using both the specified low/high bounds and standard 0..1 bounds995996// TODO: Oddity to investigate; triggered by test in issue #265.997if (high_bound <= low_bound)998{999low_bound = 0.0f;1000high_bound = 1.0f;1001}10021003float rscale = high_bound - low_bound;1004float scale = 1.0f / rscale;10051006float scaled_low_bound = low_bound * scale;1007rscale *= 1.0f / 64.0f;10081009vfloat scalev(scale);1010vfloat scaled_low_boundv(scaled_low_bound);1011vfloat quant_level_m1v(quant_level_m1);1012vfloat rscalev(rscale);1013vfloat low_boundv(low_bound);10141015// This runs to the rounded-up SIMD size, which is safe as the loop tail is filled with known1016// safe data in compute_ideal_weights_for_decimation and arrays are always 64 elements1017if (get_quant_level(quant_level) <= 16)1018{1019vtable_16x8 table;1020vtable_prepare(table, qat.quant_to_unquant);10211022for (int i = 0; i < weight_count; i += ASTCENC_SIMD_WIDTH)1023{1024vfloat ix = loada(dec_weight_ideal_value + i) * scalev - scaled_low_boundv;1025ix = clampzo(ix);10261027// Look up the two closest indexes and return the one that was closest1028vfloat ix1 = ix * quant_level_m1v;10291030vint weightl = float_to_int(ix1);1031vint weighth = min(weightl + vint(1), steps_m1);10321033vint ixli = vtable_lookup_32bit(table, weightl);1034vint ixhi = vtable_lookup_32bit(table, weighth);10351036vfloat ixl = int_to_float(ixli);1037vfloat ixh = int_to_float(ixhi);10381039vmask mask = (ixl + ixh) < (vfloat(128.0f) * ix);1040vint weight = select(ixli, ixhi, mask);1041ixl = select(ixl, ixh, mask);10421043// Invert the weight-scaling that was done initially1044storea(ixl * rscalev + low_boundv, weight_set_out + i);1045pack_and_store_low_bytes(weight, quantized_weight_set + i);1046}1047}1048else1049{1050vtable_32x8 table;1051vtable_prepare(table, qat.quant_to_unquant);10521053for (int i = 0; i < weight_count; i += ASTCENC_SIMD_WIDTH)1054{1055vfloat ix = loada(dec_weight_ideal_value + i) * scalev - scaled_low_boundv;1056ix = clampzo(ix);10571058// Look up the two closest indexes and return the one that was closest1059vfloat ix1 = ix * quant_level_m1v;10601061vint weightl = float_to_int(ix1);1062vint weighth = min(weightl + vint(1), steps_m1);10631064vint ixli = vtable_lookup_32bit(table, weightl);1065vint ixhi = vtable_lookup_32bit(table, weighth);10661067vfloat ixl = int_to_float(ixli);1068vfloat ixh = int_to_float(ixhi);10691070vmask mask = (ixl + ixh) < (vfloat(128.0f) * ix);1071vint weight = select(ixli, ixhi, mask);1072ixl = select(ixl, ixh, mask);10731074// Invert the weight-scaling that was done initially1075storea(ixl * rscalev + low_boundv, weight_set_out + i);1076pack_and_store_low_bytes(weight, quantized_weight_set + i);1077}1078}1079}10801081/**1082* @brief Compute the RGB + offset for a HDR endpoint mode #7.1083*1084* Since the matrix needed has a regular structure we can simplify the inverse calculation. This1085* gives us ~24 multiplications vs. 96 for a generic inverse.1086*1087* mat[0] = vfloat4(rgba_ws.x, 0.0f, 0.0f, wght_ws.x);1088* mat[1] = vfloat4( 0.0f, rgba_ws.y, 0.0f, wght_ws.y);1089* mat[2] = vfloat4( 0.0f, 0.0f, rgba_ws.z, wght_ws.z);1090* mat[3] = vfloat4(wght_ws.x, wght_ws.y, wght_ws.z, psum);1091* mat = invert(mat);1092*1093* @param rgba_weight_sum Sum of partition component error weights.1094* @param weight_weight_sum Sum of partition component error weights * texel weight.1095* @param rgbq_sum Sum of partition component error weights * texel weight * color data.1096* @param psum Sum of RGB color weights * texel weight^2.1097*/1098static inline vfloat4 compute_rgbo_vector(1099vfloat4 rgba_weight_sum,1100vfloat4 weight_weight_sum,1101vfloat4 rgbq_sum,1102float psum1103) {1104float X = rgba_weight_sum.lane<0>();1105float Y = rgba_weight_sum.lane<1>();1106float Z = rgba_weight_sum.lane<2>();1107float P = weight_weight_sum.lane<0>();1108float Q = weight_weight_sum.lane<1>();1109float R = weight_weight_sum.lane<2>();1110float S = psum;11111112float PP = P * P;1113float QQ = Q * Q;1114float RR = R * R;11151116float SZmRR = S * Z - RR;1117float DT = SZmRR * Y - Z * QQ;1118float YP = Y * P;1119float QX = Q * X;1120float YX = Y * X;1121float mZYP = -Z * YP;1122float mZQX = -Z * QX;1123float mRYX = -R * YX;1124float ZQP = Z * Q * P;1125float RYP = R * YP;1126float RQX = R * QX;11271128// Compute the reciprocal of matrix determinant1129float rdet = 1.0f / (DT * X + mZYP * P);11301131// Actually compute the adjugate, and then apply 1/det separately1132vfloat4 mat0(DT, ZQP, RYP, mZYP);1133vfloat4 mat1(ZQP, SZmRR * X - Z * PP, RQX, mZQX);1134vfloat4 mat2(RYP, RQX, (S * Y - QQ) * X - Y * PP, mRYX);1135vfloat4 mat3(mZYP, mZQX, mRYX, Z * YX);1136vfloat4 vect = rgbq_sum * rdet;11371138return vfloat4(dot_s(mat0, vect),1139dot_s(mat1, vect),1140dot_s(mat2, vect),1141dot_s(mat3, vect));1142}11431144/* See header for documentation. */1145void recompute_ideal_colors_1plane(1146const image_block& blk,1147const partition_info& pi,1148const decimation_info& di,1149const uint8_t* dec_weights_uquant,1150endpoints& ep,1151vfloat4 rgbs_vectors[BLOCK_MAX_PARTITIONS],1152vfloat4 rgbo_vectors[BLOCK_MAX_PARTITIONS]1153) {1154unsigned int weight_count = di.weight_count;1155unsigned int total_texel_count = blk.texel_count;1156unsigned int partition_count = pi.partition_count;11571158promise(weight_count > 0);1159promise(total_texel_count > 0);1160promise(partition_count > 0);11611162ASTCENC_ALIGNAS float dec_weight[BLOCK_MAX_WEIGHTS];1163for (unsigned int i = 0; i < weight_count; i += ASTCENC_SIMD_WIDTH)1164{1165vint unquant_value(dec_weights_uquant + i);1166vfloat unquant_valuef = int_to_float(unquant_value) * vfloat(1.0f / 64.0f);1167storea(unquant_valuef, dec_weight + i);1168}11691170ASTCENC_ALIGNAS float undec_weight[BLOCK_MAX_TEXELS];1171float* undec_weight_ref;1172if (di.max_texel_weight_count == 1)1173{1174undec_weight_ref = dec_weight;1175}1176else if (di.max_texel_weight_count <= 2)1177{1178for (unsigned int i = 0; i < total_texel_count; i += ASTCENC_SIMD_WIDTH)1179{1180vfloat weight = bilinear_infill_vla_2(di, dec_weight, i);1181storea(weight, undec_weight + i);1182}11831184undec_weight_ref = undec_weight;1185}1186else1187{1188for (unsigned int i = 0; i < total_texel_count; i += ASTCENC_SIMD_WIDTH)1189{1190vfloat weight = bilinear_infill_vla(di, dec_weight, i);1191storea(weight, undec_weight + i);1192}11931194undec_weight_ref = undec_weight;1195}11961197vfloat4 rgba_sum(blk.data_mean * static_cast<float>(blk.texel_count));11981199for (unsigned int i = 0; i < partition_count; i++)1200{1201unsigned int texel_count = pi.partition_texel_count[i];1202const uint8_t *texel_indexes = pi.texels_of_partition[i];12031204// Only compute a partition mean if more than one partition1205if (partition_count > 1)1206{1207rgba_sum = vfloat4::zero();1208promise(texel_count > 0);1209for (unsigned int j = 0; j < texel_count; j++)1210{1211unsigned int tix = texel_indexes[j];1212rgba_sum += blk.texel(tix);1213}1214}12151216rgba_sum = rgba_sum * blk.channel_weight;1217vfloat4 rgba_weight_sum = max(blk.channel_weight * static_cast<float>(texel_count), 1e-17f);1218vfloat4 scale_dir = normalize((rgba_sum / rgba_weight_sum).swz<0, 1, 2>());12191220float scale_max = 0.0f;1221float scale_min = 1e10f;12221223float wmin1 = 1.0f;1224float wmax1 = 0.0f;12251226float left_sum_s = 0.0f;1227float middle_sum_s = 0.0f;1228float right_sum_s = 0.0f;12291230vfloat4 color_vec_x = vfloat4::zero();1231vfloat4 color_vec_y = vfloat4::zero();12321233vfloat4 scale_vec = vfloat4::zero();12341235float weight_weight_sum_s = 1e-17f;12361237vfloat4 color_weight = blk.channel_weight;1238float ls_weight = hadd_rgb_s(color_weight);12391240for (unsigned int j = 0; j < texel_count; j++)1241{1242unsigned int tix = texel_indexes[j];1243vfloat4 rgba = blk.texel(tix);12441245float idx0 = undec_weight_ref[tix];12461247float om_idx0 = 1.0f - idx0;1248wmin1 = astc::min(idx0, wmin1);1249wmax1 = astc::max(idx0, wmax1);12501251float scale = dot3_s(scale_dir, rgba);1252scale_min = astc::min(scale, scale_min);1253scale_max = astc::max(scale, scale_max);12541255left_sum_s += om_idx0 * om_idx0;1256middle_sum_s += om_idx0 * idx0;1257right_sum_s += idx0 * idx0;1258weight_weight_sum_s += idx0;12591260vfloat4 color_idx(idx0);1261vfloat4 cwprod = rgba;1262vfloat4 cwiprod = cwprod * color_idx;12631264color_vec_y += cwiprod;1265color_vec_x += cwprod - cwiprod;12661267scale_vec += vfloat2(om_idx0, idx0) * (scale * ls_weight);1268}12691270vfloat4 left_sum = vfloat4(left_sum_s) * color_weight;1271vfloat4 middle_sum = vfloat4(middle_sum_s) * color_weight;1272vfloat4 right_sum = vfloat4(right_sum_s) * color_weight;1273vfloat4 lmrs_sum = vfloat3(left_sum_s, middle_sum_s, right_sum_s) * ls_weight;12741275color_vec_x = color_vec_x * color_weight;1276color_vec_y = color_vec_y * color_weight;12771278// Initialize the luminance and scale vectors with a reasonable default1279float scalediv = scale_min / astc::max(scale_max, 1e-10f);1280scalediv = astc::clamp1f(scalediv);12811282vfloat4 sds = scale_dir * scale_max;12831284rgbs_vectors[i] = vfloat4(sds.lane<0>(), sds.lane<1>(), sds.lane<2>(), scalediv);12851286if (wmin1 >= wmax1 * 0.999f)1287{1288// If all weights in the partition were equal, then just take average of all colors in1289// the partition and use that as both endpoint colors1290vfloat4 avg = (color_vec_x + color_vec_y) / rgba_weight_sum;12911292vmask4 notnan_mask = avg == avg;1293ep.endpt0[i] = select(ep.endpt0[i], avg, notnan_mask);1294ep.endpt1[i] = select(ep.endpt1[i], avg, notnan_mask);12951296rgbs_vectors[i] = vfloat4(sds.lane<0>(), sds.lane<1>(), sds.lane<2>(), 1.0f);1297}1298else1299{1300// Otherwise, complete the analytic calculation of ideal-endpoint-values for the given1301// set of texel weights and pixel colors1302vfloat4 color_det1 = (left_sum * right_sum) - (middle_sum * middle_sum);1303vfloat4 color_rdet1 = 1.0f / color_det1;13041305float ls_det1 = (lmrs_sum.lane<0>() * lmrs_sum.lane<2>()) - (lmrs_sum.lane<1>() * lmrs_sum.lane<1>());1306float ls_rdet1 = 1.0f / ls_det1;13071308vfloat4 color_mss1 = (left_sum * left_sum)1309+ (2.0f * middle_sum * middle_sum)1310+ (right_sum * right_sum);13111312float ls_mss1 = (lmrs_sum.lane<0>() * lmrs_sum.lane<0>())1313+ (2.0f * lmrs_sum.lane<1>() * lmrs_sum.lane<1>())1314+ (lmrs_sum.lane<2>() * lmrs_sum.lane<2>());13151316vfloat4 ep0 = (right_sum * color_vec_x - middle_sum * color_vec_y) * color_rdet1;1317vfloat4 ep1 = (left_sum * color_vec_y - middle_sum * color_vec_x) * color_rdet1;13181319vmask4 det_mask = abs(color_det1) > (color_mss1 * 1e-4f);1320vmask4 notnan_mask = (ep0 == ep0) & (ep1 == ep1);1321vmask4 full_mask = det_mask & notnan_mask;13221323ep.endpt0[i] = select(ep.endpt0[i], ep0, full_mask);1324ep.endpt1[i] = select(ep.endpt1[i], ep1, full_mask);13251326float scale_ep0 = (lmrs_sum.lane<2>() * scale_vec.lane<0>() - lmrs_sum.lane<1>() * scale_vec.lane<1>()) * ls_rdet1;1327float scale_ep1 = (lmrs_sum.lane<0>() * scale_vec.lane<1>() - lmrs_sum.lane<1>() * scale_vec.lane<0>()) * ls_rdet1;13281329if (fabsf(ls_det1) > (ls_mss1 * 1e-4f) && scale_ep0 == scale_ep0 && scale_ep1 == scale_ep1 && scale_ep0 < scale_ep1)1330{1331float scalediv2 = scale_ep0 / scale_ep1;1332vfloat4 sdsm = scale_dir * scale_ep1;1333rgbs_vectors[i] = vfloat4(sdsm.lane<0>(), sdsm.lane<1>(), sdsm.lane<2>(), scalediv2);1334}1335}13361337// Calculations specific to mode #7, the HDR RGB-scale mode - skip if known LDR1338if (blk.rgb_lns[0] || blk.alpha_lns[0])1339{1340vfloat4 weight_weight_sum = vfloat4(weight_weight_sum_s) * color_weight;1341float psum = right_sum_s * hadd_rgb_s(color_weight);13421343vfloat4 rgbq_sum = color_vec_x + color_vec_y;1344rgbq_sum.set_lane<3>(hadd_rgb_s(color_vec_y));13451346vfloat4 rgbovec = compute_rgbo_vector(rgba_weight_sum, weight_weight_sum, rgbq_sum, psum);1347rgbo_vectors[i] = rgbovec;13481349// We can get a failure due to the use of a singular (non-invertible) matrix1350// If it failed, compute rgbo_vectors[] with a different method ...1351if (astc::isnan(dot_s(rgbovec, rgbovec)))1352{1353vfloat4 v0 = ep.endpt0[i];1354vfloat4 v1 = ep.endpt1[i];13551356float avgdif = hadd_rgb_s(v1 - v0) * (1.0f / 3.0f);1357avgdif = astc::max(avgdif, 0.0f);13581359vfloat4 avg = (v0 + v1) * 0.5f;1360vfloat4 ep0 = avg - vfloat4(avgdif) * 0.5f;1361rgbo_vectors[i] = vfloat4(ep0.lane<0>(), ep0.lane<1>(), ep0.lane<2>(), avgdif);1362}1363}1364}1365}13661367/* See header for documentation. */1368void recompute_ideal_colors_2planes(1369const image_block& blk,1370const block_size_descriptor& bsd,1371const decimation_info& di,1372const uint8_t* dec_weights_uquant_plane1,1373const uint8_t* dec_weights_uquant_plane2,1374endpoints& ep,1375vfloat4& rgbs_vector,1376vfloat4& rgbo_vector,1377int plane2_component1378) {1379unsigned int weight_count = di.weight_count;1380unsigned int total_texel_count = blk.texel_count;13811382promise(total_texel_count > 0);1383promise(weight_count > 0);13841385ASTCENC_ALIGNAS float dec_weight_plane1[BLOCK_MAX_WEIGHTS_2PLANE];1386ASTCENC_ALIGNAS float dec_weight_plane2[BLOCK_MAX_WEIGHTS_2PLANE];13871388assert(weight_count <= BLOCK_MAX_WEIGHTS_2PLANE);13891390for (unsigned int i = 0; i < weight_count; i += ASTCENC_SIMD_WIDTH)1391{1392vint unquant_value1(dec_weights_uquant_plane1 + i);1393vfloat unquant_value1f = int_to_float(unquant_value1) * vfloat(1.0f / 64.0f);1394storea(unquant_value1f, dec_weight_plane1 + i);13951396vint unquant_value2(dec_weights_uquant_plane2 + i);1397vfloat unquant_value2f = int_to_float(unquant_value2) * vfloat(1.0f / 64.0f);1398storea(unquant_value2f, dec_weight_plane2 + i);1399}14001401ASTCENC_ALIGNAS float undec_weight_plane1[BLOCK_MAX_TEXELS];1402ASTCENC_ALIGNAS float undec_weight_plane2[BLOCK_MAX_TEXELS];14031404float* undec_weight_plane1_ref;1405float* undec_weight_plane2_ref;14061407if (di.max_texel_weight_count == 1)1408{1409undec_weight_plane1_ref = dec_weight_plane1;1410undec_weight_plane2_ref = dec_weight_plane2;1411}1412else if (di.max_texel_weight_count <= 2)1413{1414for (unsigned int i = 0; i < total_texel_count; i += ASTCENC_SIMD_WIDTH)1415{1416vfloat weight = bilinear_infill_vla_2(di, dec_weight_plane1, i);1417storea(weight, undec_weight_plane1 + i);14181419weight = bilinear_infill_vla_2(di, dec_weight_plane2, i);1420storea(weight, undec_weight_plane2 + i);1421}14221423undec_weight_plane1_ref = undec_weight_plane1;1424undec_weight_plane2_ref = undec_weight_plane2;1425}1426else1427{1428for (unsigned int i = 0; i < total_texel_count; i += ASTCENC_SIMD_WIDTH)1429{1430vfloat weight = bilinear_infill_vla(di, dec_weight_plane1, i);1431storea(weight, undec_weight_plane1 + i);14321433weight = bilinear_infill_vla(di, dec_weight_plane2, i);1434storea(weight, undec_weight_plane2 + i);1435}14361437undec_weight_plane1_ref = undec_weight_plane1;1438undec_weight_plane2_ref = undec_weight_plane2;1439}14401441unsigned int texel_count = bsd.texel_count;1442vfloat4 rgba_weight_sum = max(blk.channel_weight * static_cast<float>(texel_count), 1e-17f);1443vfloat4 scale_dir = normalize(blk.data_mean.swz<0, 1, 2>());14441445float scale_max = 0.0f;1446float scale_min = 1e10f;14471448float wmin1 = 1.0f;1449float wmax1 = 0.0f;14501451float wmin2 = 1.0f;1452float wmax2 = 0.0f;14531454float left1_sum_s = 0.0f;1455float middle1_sum_s = 0.0f;1456float right1_sum_s = 0.0f;14571458float left2_sum_s = 0.0f;1459float middle2_sum_s = 0.0f;1460float right2_sum_s = 0.0f;14611462vfloat4 color_vec_x = vfloat4::zero();1463vfloat4 color_vec_y = vfloat4::zero();14641465vfloat4 scale_vec = vfloat4::zero();14661467vfloat4 weight_weight_sum = vfloat4(1e-17f);14681469vmask4 p2_mask = vint4::lane_id() == vint4(plane2_component);1470vfloat4 color_weight = blk.channel_weight;1471float ls_weight = hadd_rgb_s(color_weight);14721473for (unsigned int j = 0; j < texel_count; j++)1474{1475vfloat4 rgba = blk.texel(j);14761477float idx0 = undec_weight_plane1_ref[j];14781479float om_idx0 = 1.0f - idx0;1480wmin1 = astc::min(idx0, wmin1);1481wmax1 = astc::max(idx0, wmax1);14821483float scale = dot3_s(scale_dir, rgba);1484scale_min = astc::min(scale, scale_min);1485scale_max = astc::max(scale, scale_max);14861487left1_sum_s += om_idx0 * om_idx0;1488middle1_sum_s += om_idx0 * idx0;1489right1_sum_s += idx0 * idx0;14901491float idx1 = undec_weight_plane2_ref[j];14921493float om_idx1 = 1.0f - idx1;1494wmin2 = astc::min(idx1, wmin2);1495wmax2 = astc::max(idx1, wmax2);14961497left2_sum_s += om_idx1 * om_idx1;1498middle2_sum_s += om_idx1 * idx1;1499right2_sum_s += idx1 * idx1;15001501vfloat4 color_idx = select(vfloat4(idx0), vfloat4(idx1), p2_mask);15021503vfloat4 cwprod = rgba;1504vfloat4 cwiprod = cwprod * color_idx;15051506color_vec_y += cwiprod;1507color_vec_x += cwprod - cwiprod;15081509scale_vec += vfloat2(om_idx0, idx0) * (ls_weight * scale);1510weight_weight_sum += color_idx;1511}15121513vfloat4 left1_sum = vfloat4(left1_sum_s) * color_weight;1514vfloat4 middle1_sum = vfloat4(middle1_sum_s) * color_weight;1515vfloat4 right1_sum = vfloat4(right1_sum_s) * color_weight;1516vfloat4 lmrs_sum = vfloat3(left1_sum_s, middle1_sum_s, right1_sum_s) * ls_weight;15171518vfloat4 left2_sum = vfloat4(left2_sum_s) * color_weight;1519vfloat4 middle2_sum = vfloat4(middle2_sum_s) * color_weight;1520vfloat4 right2_sum = vfloat4(right2_sum_s) * color_weight;15211522color_vec_x = color_vec_x * color_weight;1523color_vec_y = color_vec_y * color_weight;15241525// Initialize the luminance and scale vectors with a reasonable default1526float scalediv = scale_min / astc::max(scale_max, 1e-10f);1527scalediv = astc::clamp1f(scalediv);15281529vfloat4 sds = scale_dir * scale_max;15301531rgbs_vector = vfloat4(sds.lane<0>(), sds.lane<1>(), sds.lane<2>(), scalediv);15321533if (wmin1 >= wmax1 * 0.999f)1534{1535// If all weights in the partition were equal, then just take average of all colors in1536// the partition and use that as both endpoint colors1537vfloat4 avg = (color_vec_x + color_vec_y) / rgba_weight_sum;15381539vmask4 p1_mask = vint4::lane_id() != vint4(plane2_component);1540vmask4 notnan_mask = avg == avg;1541vmask4 full_mask = p1_mask & notnan_mask;15421543ep.endpt0[0] = select(ep.endpt0[0], avg, full_mask);1544ep.endpt1[0] = select(ep.endpt1[0], avg, full_mask);15451546rgbs_vector = vfloat4(sds.lane<0>(), sds.lane<1>(), sds.lane<2>(), 1.0f);1547}1548else1549{1550// Otherwise, complete the analytic calculation of ideal-endpoint-values for the given1551// set of texel weights and pixel colors1552vfloat4 color_det1 = (left1_sum * right1_sum) - (middle1_sum * middle1_sum);1553vfloat4 color_rdet1 = 1.0f / color_det1;15541555float ls_det1 = (lmrs_sum.lane<0>() * lmrs_sum.lane<2>()) - (lmrs_sum.lane<1>() * lmrs_sum.lane<1>());1556float ls_rdet1 = 1.0f / ls_det1;15571558vfloat4 color_mss1 = (left1_sum * left1_sum)1559+ (2.0f * middle1_sum * middle1_sum)1560+ (right1_sum * right1_sum);15611562float ls_mss1 = (lmrs_sum.lane<0>() * lmrs_sum.lane<0>())1563+ (2.0f * lmrs_sum.lane<1>() * lmrs_sum.lane<1>())1564+ (lmrs_sum.lane<2>() * lmrs_sum.lane<2>());15651566vfloat4 ep0 = (right1_sum * color_vec_x - middle1_sum * color_vec_y) * color_rdet1;1567vfloat4 ep1 = (left1_sum * color_vec_y - middle1_sum * color_vec_x) * color_rdet1;15681569float scale_ep0 = (lmrs_sum.lane<2>() * scale_vec.lane<0>() - lmrs_sum.lane<1>() * scale_vec.lane<1>()) * ls_rdet1;1570float scale_ep1 = (lmrs_sum.lane<0>() * scale_vec.lane<1>() - lmrs_sum.lane<1>() * scale_vec.lane<0>()) * ls_rdet1;15711572vmask4 p1_mask = vint4::lane_id() != vint4(plane2_component);1573vmask4 det_mask = abs(color_det1) > (color_mss1 * 1e-4f);1574vmask4 notnan_mask = (ep0 == ep0) & (ep1 == ep1);1575vmask4 full_mask = p1_mask & det_mask & notnan_mask;15761577ep.endpt0[0] = select(ep.endpt0[0], ep0, full_mask);1578ep.endpt1[0] = select(ep.endpt1[0], ep1, full_mask);15791580if (fabsf(ls_det1) > (ls_mss1 * 1e-4f) && scale_ep0 == scale_ep0 && scale_ep1 == scale_ep1 && scale_ep0 < scale_ep1)1581{1582float scalediv2 = scale_ep0 / scale_ep1;1583vfloat4 sdsm = scale_dir * scale_ep1;1584rgbs_vector = vfloat4(sdsm.lane<0>(), sdsm.lane<1>(), sdsm.lane<2>(), scalediv2);1585}1586}15871588if (wmin2 >= wmax2 * 0.999f)1589{1590// If all weights in the partition were equal, then just take average of all colors in1591// the partition and use that as both endpoint colors1592vfloat4 avg = (color_vec_x + color_vec_y) / rgba_weight_sum;15931594vmask4 notnan_mask = avg == avg;1595vmask4 full_mask = p2_mask & notnan_mask;15961597ep.endpt0[0] = select(ep.endpt0[0], avg, full_mask);1598ep.endpt1[0] = select(ep.endpt1[0], avg, full_mask);1599}1600else1601{1602// Otherwise, complete the analytic calculation of ideal-endpoint-values for the given1603// set of texel weights and pixel colors1604vfloat4 color_det2 = (left2_sum * right2_sum) - (middle2_sum * middle2_sum);1605vfloat4 color_rdet2 = 1.0f / color_det2;16061607vfloat4 color_mss2 = (left2_sum * left2_sum)1608+ (2.0f * middle2_sum * middle2_sum)1609+ (right2_sum * right2_sum);16101611vfloat4 ep0 = (right2_sum * color_vec_x - middle2_sum * color_vec_y) * color_rdet2;1612vfloat4 ep1 = (left2_sum * color_vec_y - middle2_sum * color_vec_x) * color_rdet2;16131614vmask4 det_mask = abs(color_det2) > (color_mss2 * 1e-4f);1615vmask4 notnan_mask = (ep0 == ep0) & (ep1 == ep1);1616vmask4 full_mask = p2_mask & det_mask & notnan_mask;16171618ep.endpt0[0] = select(ep.endpt0[0], ep0, full_mask);1619ep.endpt1[0] = select(ep.endpt1[0], ep1, full_mask);1620}16211622// Calculations specific to mode #7, the HDR RGB-scale mode - skip if known LDR1623if (blk.rgb_lns[0] || blk.alpha_lns[0])1624{1625weight_weight_sum = weight_weight_sum * color_weight;1626float psum = dot3_s(select(right1_sum, right2_sum, p2_mask), color_weight);16271628vfloat4 rgbq_sum = color_vec_x + color_vec_y;1629rgbq_sum.set_lane<3>(hadd_rgb_s(color_vec_y));16301631rgbo_vector = compute_rgbo_vector(rgba_weight_sum, weight_weight_sum, rgbq_sum, psum);16321633// We can get a failure due to the use of a singular (non-invertible) matrix1634// If it failed, compute rgbo_vectors[] with a different method ...1635if (astc::isnan(dot_s(rgbo_vector, rgbo_vector)))1636{1637vfloat4 v0 = ep.endpt0[0];1638vfloat4 v1 = ep.endpt1[0];16391640float avgdif = hadd_rgb_s(v1 - v0) * (1.0f / 3.0f);1641avgdif = astc::max(avgdif, 0.0f);16421643vfloat4 avg = (v0 + v1) * 0.5f;1644vfloat4 ep0 = avg - vfloat4(avgdif) * 0.5f;16451646rgbo_vector = vfloat4(ep0.lane<0>(), ep0.lane<1>(), ep0.lane<2>(), avgdif);1647}1648}1649}16501651#endif165216531654