astcenc_decompress_symbolic.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. // SPDX-License-Identifier: Apache-2.0
  2. // ----------------------------------------------------------------------------
  3. // Copyright 2011-2024 Arm Limited
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License"); you may not
  6. // use this file except in compliance with the License. You may obtain a copy
  7. // of the License at:
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  14. // License for the specific language governing permissions and limitations
  15. // under the License.
  16. // ----------------------------------------------------------------------------
  17. /**
  18. * @brief Functions to decompress a symbolic block.
  19. */
  20. #include "astcenc_internal.h"
  21. #include <stdio.h>
  22. #include <assert.h>
  23. /**
  24. * @brief Compute the integer linear interpolation of two color endpoints.
  25. *
  26. * @param u8_mask The mask for lanes using decode_unorm8 rather than decode_f16.
  27. * @param color0 The endpoint0 color.
  28. * @param color1 The endpoint1 color.
  29. * @param weights The interpolation weight (between 0 and 64).
  30. *
  31. * @return The interpolated color.
  32. */
  33. static vint4 lerp_color_int(
  34. vmask4 u8_mask,
  35. vint4 color0,
  36. vint4 color1,
  37. vint4 weights
  38. ) {
  39. vint4 weight1 = weights;
  40. vint4 weight0 = vint4(64) - weight1;
  41. vint4 color = (color0 * weight0) + (color1 * weight1) + vint4(32);
  42. color = asr<6>(color);
  43. // For decode_unorm8 values force the codec to bit replicate. This allows the
  44. // rest of the codec to assume the full 0xFFFF range for everything and ignore
  45. // the decode_mode setting
  46. vint4 color_u8 = asr<8>(color) * vint4(257);
  47. color = select(color, color_u8, u8_mask);
  48. return color;
  49. }
  50. /**
  51. * @brief Convert integer color value into a float value for the decoder.
  52. *
  53. * @param data The integer color value post-interpolation.
  54. * @param lns_mask If set treat lane as HDR (LNS) else LDR (unorm16).
  55. *
  56. * @return The float color value.
  57. */
  58. static inline vfloat4 decode_texel(
  59. vint4 data,
  60. vmask4 lns_mask
  61. ) {
  62. vint4 color_lns = vint4::zero();
  63. vint4 color_unorm = vint4::zero();
  64. if (any(lns_mask))
  65. {
  66. color_lns = lns_to_sf16(data);
  67. }
  68. if (!all(lns_mask))
  69. {
  70. color_unorm = unorm16_to_sf16(data);
  71. }
  72. // Pick components and then convert to FP16
  73. vint4 datai = select(color_unorm, color_lns, lns_mask);
  74. return float16_to_float(datai);
  75. }
  76. /* See header for documentation. */
  77. void unpack_weights(
  78. const block_size_descriptor& bsd,
  79. const symbolic_compressed_block& scb,
  80. const decimation_info& di,
  81. bool is_dual_plane,
  82. int weights_plane1[BLOCK_MAX_TEXELS],
  83. int weights_plane2[BLOCK_MAX_TEXELS]
  84. ) {
  85. // Safe to overshoot as all arrays are allocated to full size
  86. if (!is_dual_plane)
  87. {
  88. // Build full 64-entry weight lookup table
  89. vint4 tab0 = vint4::load(scb.weights + 0);
  90. vint4 tab1 = vint4::load(scb.weights + 16);
  91. vint4 tab2 = vint4::load(scb.weights + 32);
  92. vint4 tab3 = vint4::load(scb.weights + 48);
  93. vint tab0p, tab1p, tab2p, tab3p;
  94. vtable_prepare(tab0, tab1, tab2, tab3, tab0p, tab1p, tab2p, tab3p);
  95. for (unsigned int i = 0; i < bsd.texel_count; i += ASTCENC_SIMD_WIDTH)
  96. {
  97. vint summed_value(8);
  98. vint weight_count(di.texel_weight_count + i);
  99. int max_weight_count = hmax(weight_count).lane<0>();
  100. promise(max_weight_count > 0);
  101. for (int j = 0; j < max_weight_count; j++)
  102. {
  103. vint texel_weights(di.texel_weights_tr[j] + i);
  104. vint texel_weights_int(di.texel_weight_contribs_int_tr[j] + i);
  105. summed_value += vtable_8bt_32bi(tab0p, tab1p, tab2p, tab3p, texel_weights) * texel_weights_int;
  106. }
  107. store(lsr<4>(summed_value), weights_plane1 + i);
  108. }
  109. }
  110. else
  111. {
  112. // Build a 32-entry weight lookup table per plane
  113. // Plane 1
  114. vint4 tab0_plane1 = vint4::load(scb.weights + 0);
  115. vint4 tab1_plane1 = vint4::load(scb.weights + 16);
  116. vint tab0_plane1p, tab1_plane1p;
  117. vtable_prepare(tab0_plane1, tab1_plane1, tab0_plane1p, tab1_plane1p);
  118. // Plane 2
  119. vint4 tab0_plane2 = vint4::load(scb.weights + 32);
  120. vint4 tab1_plane2 = vint4::load(scb.weights + 48);
  121. vint tab0_plane2p, tab1_plane2p;
  122. vtable_prepare(tab0_plane2, tab1_plane2, tab0_plane2p, tab1_plane2p);
  123. for (unsigned int i = 0; i < bsd.texel_count; i += ASTCENC_SIMD_WIDTH)
  124. {
  125. vint sum_plane1(8);
  126. vint sum_plane2(8);
  127. vint weight_count(di.texel_weight_count + i);
  128. int max_weight_count = hmax(weight_count).lane<0>();
  129. promise(max_weight_count > 0);
  130. for (int j = 0; j < max_weight_count; j++)
  131. {
  132. vint texel_weights(di.texel_weights_tr[j] + i);
  133. vint texel_weights_int(di.texel_weight_contribs_int_tr[j] + i);
  134. sum_plane1 += vtable_8bt_32bi(tab0_plane1p, tab1_plane1p, texel_weights) * texel_weights_int;
  135. sum_plane2 += vtable_8bt_32bi(tab0_plane2p, tab1_plane2p, texel_weights) * texel_weights_int;
  136. }
  137. store(lsr<4>(sum_plane1), weights_plane1 + i);
  138. store(lsr<4>(sum_plane2), weights_plane2 + i);
  139. }
  140. }
  141. }
  142. /**
  143. * @brief Return an FP32 NaN value for use in error colors.
  144. *
  145. * This NaN encoding will turn into 0xFFFF when converted to an FP16 NaN.
  146. *
  147. * @return The float color value.
  148. */
  149. static float error_color_nan()
  150. {
  151. if32 v;
  152. v.u = 0xFFFFE000U;
  153. return v.f;
  154. }
  155. /* See header for documentation. */
  156. void decompress_symbolic_block(
  157. astcenc_profile decode_mode,
  158. const block_size_descriptor& bsd,
  159. int xpos,
  160. int ypos,
  161. int zpos,
  162. const symbolic_compressed_block& scb,
  163. image_block& blk
  164. ) {
  165. blk.xpos = xpos;
  166. blk.ypos = ypos;
  167. blk.zpos = zpos;
  168. blk.data_min = vfloat4::zero();
  169. blk.data_mean = vfloat4::zero();
  170. blk.data_max = vfloat4::zero();
  171. blk.grayscale = false;
  172. // If we detected an error-block, blow up immediately.
  173. if (scb.block_type == SYM_BTYPE_ERROR)
  174. {
  175. for (unsigned int i = 0; i < bsd.texel_count; i++)
  176. {
  177. blk.data_r[i] = error_color_nan();
  178. blk.data_g[i] = error_color_nan();
  179. blk.data_b[i] = error_color_nan();
  180. blk.data_a[i] = error_color_nan();
  181. blk.rgb_lns[i] = 0;
  182. blk.alpha_lns[i] = 0;
  183. }
  184. return;
  185. }
  186. if ((scb.block_type == SYM_BTYPE_CONST_F16) ||
  187. (scb.block_type == SYM_BTYPE_CONST_U16))
  188. {
  189. vfloat4 color;
  190. uint8_t use_lns = 0;
  191. // UNORM16 constant color block
  192. if (scb.block_type == SYM_BTYPE_CONST_U16)
  193. {
  194. vint4 colori(scb.constant_color);
  195. // Determine the UNORM8 rounding on the decode
  196. vmask4 u8_mask = get_u8_component_mask(decode_mode, blk);
  197. // The real decoder would just use the top 8 bits, but we rescale
  198. // in to a 16-bit value that rounds correctly.
  199. vint4 colori_u8 = asr<8>(colori) * 257;
  200. colori = select(colori, colori_u8, u8_mask);
  201. vint4 colorf16 = unorm16_to_sf16(colori);
  202. color = float16_to_float(colorf16);
  203. }
  204. // FLOAT16 constant color block
  205. else
  206. {
  207. switch (decode_mode)
  208. {
  209. case ASTCENC_PRF_LDR_SRGB:
  210. case ASTCENC_PRF_LDR:
  211. color = vfloat4(error_color_nan());
  212. break;
  213. case ASTCENC_PRF_HDR_RGB_LDR_A:
  214. case ASTCENC_PRF_HDR:
  215. // Constant-color block; unpack from FP16 to FP32.
  216. color = float16_to_float(vint4(scb.constant_color));
  217. use_lns = 1;
  218. break;
  219. }
  220. }
  221. for (unsigned int i = 0; i < bsd.texel_count; i++)
  222. {
  223. blk.data_r[i] = color.lane<0>();
  224. blk.data_g[i] = color.lane<1>();
  225. blk.data_b[i] = color.lane<2>();
  226. blk.data_a[i] = color.lane<3>();
  227. blk.rgb_lns[i] = use_lns;
  228. blk.alpha_lns[i] = use_lns;
  229. }
  230. return;
  231. }
  232. // Get the appropriate partition-table entry
  233. int partition_count = scb.partition_count;
  234. const auto& pi = bsd.get_partition_info(partition_count, scb.partition_index);
  235. // Get the appropriate block descriptors
  236. const auto& bm = bsd.get_block_mode(scb.block_mode);
  237. const auto& di = bsd.get_decimation_info(bm.decimation_mode);
  238. bool is_dual_plane = static_cast<bool>(bm.is_dual_plane);
  239. // Unquantize and undecimate the weights
  240. int plane1_weights[BLOCK_MAX_TEXELS];
  241. int plane2_weights[BLOCK_MAX_TEXELS];
  242. unpack_weights(bsd, scb, di, is_dual_plane, plane1_weights, plane2_weights);
  243. // Now that we have endpoint colors and weights, we can unpack texel colors
  244. int plane2_component = scb.plane2_component;
  245. vmask4 plane2_mask = vint4::lane_id() == vint4(plane2_component);
  246. vmask4 u8_mask = get_u8_component_mask(decode_mode, blk);
  247. for (int i = 0; i < partition_count; i++)
  248. {
  249. // Decode the color endpoints for this partition
  250. vint4 ep0;
  251. vint4 ep1;
  252. bool rgb_lns;
  253. bool a_lns;
  254. unpack_color_endpoints(decode_mode,
  255. scb.color_formats[i],
  256. scb.color_values[i],
  257. rgb_lns, a_lns,
  258. ep0, ep1);
  259. vmask4 lns_mask(rgb_lns, rgb_lns, rgb_lns, a_lns);
  260. int texel_count = pi.partition_texel_count[i];
  261. for (int j = 0; j < texel_count; j++)
  262. {
  263. int tix = pi.texels_of_partition[i][j];
  264. vint4 weight = select(vint4(plane1_weights[tix]), vint4(plane2_weights[tix]), plane2_mask);
  265. vint4 color = lerp_color_int(u8_mask, ep0, ep1, weight);
  266. vfloat4 colorf = decode_texel(color, lns_mask);
  267. blk.data_r[tix] = colorf.lane<0>();
  268. blk.data_g[tix] = colorf.lane<1>();
  269. blk.data_b[tix] = colorf.lane<2>();
  270. blk.data_a[tix] = colorf.lane<3>();
  271. }
  272. }
  273. }
  274. #if !defined(ASTCENC_DECOMPRESS_ONLY)
  275. /* See header for documentation. */
  276. float compute_symbolic_block_difference_2plane(
  277. const astcenc_config& config,
  278. const block_size_descriptor& bsd,
  279. const symbolic_compressed_block& scb,
  280. const image_block& blk
  281. ) {
  282. // If we detected an error-block, blow up immediately.
  283. if (scb.block_type == SYM_BTYPE_ERROR)
  284. {
  285. return ERROR_CALC_DEFAULT;
  286. }
  287. assert(scb.block_mode >= 0);
  288. assert(scb.partition_count == 1);
  289. assert(bsd.get_block_mode(scb.block_mode).is_dual_plane == 1);
  290. // Get the appropriate block descriptor
  291. const block_mode& bm = bsd.get_block_mode(scb.block_mode);
  292. const decimation_info& di = bsd.get_decimation_info(bm.decimation_mode);
  293. // Unquantize and undecimate the weights
  294. int plane1_weights[BLOCK_MAX_TEXELS];
  295. int plane2_weights[BLOCK_MAX_TEXELS];
  296. unpack_weights(bsd, scb, di, true, plane1_weights, plane2_weights);
  297. vmask4 plane2_mask = vint4::lane_id() == vint4(scb.plane2_component);
  298. vfloat4 summa = vfloat4::zero();
  299. // Decode the color endpoints for this partition
  300. vint4 ep0;
  301. vint4 ep1;
  302. bool rgb_lns;
  303. bool a_lns;
  304. unpack_color_endpoints(config.profile,
  305. scb.color_formats[0],
  306. scb.color_values[0],
  307. rgb_lns, a_lns,
  308. ep0, ep1);
  309. vmask4 u8_mask = get_u8_component_mask(config.profile, blk);
  310. // Unpack and compute error for each texel in the partition
  311. unsigned int texel_count = bsd.texel_count;
  312. for (unsigned int i = 0; i < texel_count; i++)
  313. {
  314. vint4 weight = select(vint4(plane1_weights[i]), vint4(plane2_weights[i]), plane2_mask);
  315. vint4 colori = lerp_color_int(u8_mask, ep0, ep1, weight);
  316. vfloat4 color = int_to_float(colori);
  317. vfloat4 oldColor = blk.texel(i);
  318. // Compare error using a perceptual decode metric for RGBM textures
  319. if (config.flags & ASTCENC_FLG_MAP_RGBM)
  320. {
  321. // Fail encodings that result in zero weight M pixels. Note that this can cause
  322. // "interesting" artifacts if we reject all useful encodings - we typically get max
  323. // brightness encodings instead which look just as bad. We recommend users apply a
  324. // bias to their stored M value, limiting the lower value to 16 or 32 to avoid
  325. // getting small M values post-quantization, but we can't prove it would never
  326. // happen, especially at low bit rates ...
  327. if (color.lane<3>() == 0.0f)
  328. {
  329. return -ERROR_CALC_DEFAULT;
  330. }
  331. // Compute error based on decoded RGBM color
  332. color = vfloat4(
  333. color.lane<0>() * color.lane<3>() * config.rgbm_m_scale,
  334. color.lane<1>() * color.lane<3>() * config.rgbm_m_scale,
  335. color.lane<2>() * color.lane<3>() * config.rgbm_m_scale,
  336. 1.0f
  337. );
  338. oldColor = vfloat4(
  339. oldColor.lane<0>() * oldColor.lane<3>() * config.rgbm_m_scale,
  340. oldColor.lane<1>() * oldColor.lane<3>() * config.rgbm_m_scale,
  341. oldColor.lane<2>() * oldColor.lane<3>() * config.rgbm_m_scale,
  342. 1.0f
  343. );
  344. }
  345. vfloat4 error = oldColor - color;
  346. error = min(abs(error), 1e15f);
  347. error = error * error;
  348. summa += min(dot(error, blk.channel_weight), ERROR_CALC_DEFAULT);
  349. }
  350. return summa.lane<0>();
  351. }
  352. /* See header for documentation. */
  353. float compute_symbolic_block_difference_1plane(
  354. const astcenc_config& config,
  355. const block_size_descriptor& bsd,
  356. const symbolic_compressed_block& scb,
  357. const image_block& blk
  358. ) {
  359. assert(bsd.get_block_mode(scb.block_mode).is_dual_plane == 0);
  360. // If we detected an error-block, blow up immediately.
  361. if (scb.block_type == SYM_BTYPE_ERROR)
  362. {
  363. return ERROR_CALC_DEFAULT;
  364. }
  365. assert(scb.block_mode >= 0);
  366. // Get the appropriate partition-table entry
  367. unsigned int partition_count = scb.partition_count;
  368. const auto& pi = bsd.get_partition_info(partition_count, scb.partition_index);
  369. // Get the appropriate block descriptor
  370. const block_mode& bm = bsd.get_block_mode(scb.block_mode);
  371. const decimation_info& di = bsd.get_decimation_info(bm.decimation_mode);
  372. // Unquantize and undecimate the weights
  373. int plane1_weights[BLOCK_MAX_TEXELS];
  374. unpack_weights(bsd, scb, di, false, plane1_weights, nullptr);
  375. vmask4 u8_mask = get_u8_component_mask(config.profile, blk);
  376. vfloat4 summa = vfloat4::zero();
  377. for (unsigned int i = 0; i < partition_count; i++)
  378. {
  379. // Decode the color endpoints for this partition
  380. vint4 ep0;
  381. vint4 ep1;
  382. bool rgb_lns;
  383. bool a_lns;
  384. unpack_color_endpoints(config.profile,
  385. scb.color_formats[i],
  386. scb.color_values[i],
  387. rgb_lns, a_lns,
  388. ep0, ep1);
  389. // Unpack and compute error for each texel in the partition
  390. unsigned int texel_count = pi.partition_texel_count[i];
  391. for (unsigned int j = 0; j < texel_count; j++)
  392. {
  393. unsigned int tix = pi.texels_of_partition[i][j];
  394. vint4 colori = lerp_color_int(u8_mask, ep0, ep1,
  395. vint4(plane1_weights[tix]));
  396. vfloat4 color = int_to_float(colori);
  397. vfloat4 oldColor = blk.texel(tix);
  398. // Compare error using a perceptual decode metric for RGBM textures
  399. if (config.flags & ASTCENC_FLG_MAP_RGBM)
  400. {
  401. // Fail encodings that result in zero weight M pixels. Note that this can cause
  402. // "interesting" artifacts if we reject all useful encodings - we typically get max
  403. // brightness encodings instead which look just as bad. We recommend users apply a
  404. // bias to their stored M value, limiting the lower value to 16 or 32 to avoid
  405. // getting small M values post-quantization, but we can't prove it would never
  406. // happen, especially at low bit rates ...
  407. if (color.lane<3>() == 0.0f)
  408. {
  409. return -ERROR_CALC_DEFAULT;
  410. }
  411. // Compute error based on decoded RGBM color
  412. color = vfloat4(
  413. color.lane<0>() * color.lane<3>() * config.rgbm_m_scale,
  414. color.lane<1>() * color.lane<3>() * config.rgbm_m_scale,
  415. color.lane<2>() * color.lane<3>() * config.rgbm_m_scale,
  416. 1.0f
  417. );
  418. oldColor = vfloat4(
  419. oldColor.lane<0>() * oldColor.lane<3>() * config.rgbm_m_scale,
  420. oldColor.lane<1>() * oldColor.lane<3>() * config.rgbm_m_scale,
  421. oldColor.lane<2>() * oldColor.lane<3>() * config.rgbm_m_scale,
  422. 1.0f
  423. );
  424. }
  425. vfloat4 error = oldColor - color;
  426. error = min(abs(error), 1e15f);
  427. error = error * error;
  428. summa += min(dot(error, blk.channel_weight), ERROR_CALC_DEFAULT);
  429. }
  430. }
  431. return summa.lane<0>();
  432. }
  433. /* See header for documentation. */
  434. float compute_symbolic_block_difference_1plane_1partition(
  435. const astcenc_config& config,
  436. const block_size_descriptor& bsd,
  437. const symbolic_compressed_block& scb,
  438. const image_block& blk
  439. ) {
  440. // If we detected an error-block, blow up immediately.
  441. if (scb.block_type == SYM_BTYPE_ERROR)
  442. {
  443. return ERROR_CALC_DEFAULT;
  444. }
  445. assert(scb.block_mode >= 0);
  446. assert(bsd.get_partition_info(scb.partition_count, scb.partition_index).partition_count == 1);
  447. // Get the appropriate block descriptor
  448. const block_mode& bm = bsd.get_block_mode(scb.block_mode);
  449. const decimation_info& di = bsd.get_decimation_info(bm.decimation_mode);
  450. // Unquantize and undecimate the weights
  451. ASTCENC_ALIGNAS int plane1_weights[BLOCK_MAX_TEXELS];
  452. unpack_weights(bsd, scb, di, false, plane1_weights, nullptr);
  453. // Decode the color endpoints for this partition
  454. vint4 ep0;
  455. vint4 ep1;
  456. bool rgb_lns;
  457. bool a_lns;
  458. unpack_color_endpoints(config.profile,
  459. scb.color_formats[0],
  460. scb.color_values[0],
  461. rgb_lns, a_lns,
  462. ep0, ep1);
  463. vmask4 u8_mask = get_u8_component_mask(config.profile, blk);
  464. // Unpack and compute error for each texel in the partition
  465. vfloatacc summav = vfloatacc::zero();
  466. vint lane_id = vint::lane_id();
  467. unsigned int texel_count = bsd.texel_count;
  468. for (unsigned int i = 0; i < texel_count; i += ASTCENC_SIMD_WIDTH)
  469. {
  470. // Compute EP1 contribution
  471. vint weight1 = vint::loada(plane1_weights + i);
  472. vint ep1_r = vint(ep1.lane<0>()) * weight1;
  473. vint ep1_g = vint(ep1.lane<1>()) * weight1;
  474. vint ep1_b = vint(ep1.lane<2>()) * weight1;
  475. vint ep1_a = vint(ep1.lane<3>()) * weight1;
  476. // Compute EP0 contribution
  477. vint weight0 = vint(64) - weight1;
  478. vint ep0_r = vint(ep0.lane<0>()) * weight0;
  479. vint ep0_g = vint(ep0.lane<1>()) * weight0;
  480. vint ep0_b = vint(ep0.lane<2>()) * weight0;
  481. vint ep0_a = vint(ep0.lane<3>()) * weight0;
  482. // Combine contributions
  483. vint colori_r = asr<6>(ep0_r + ep1_r + vint(32));
  484. vint colori_g = asr<6>(ep0_g + ep1_g + vint(32));
  485. vint colori_b = asr<6>(ep0_b + ep1_b + vint(32));
  486. vint colori_a = asr<6>(ep0_a + ep1_a + vint(32));
  487. // If using a U8 decode mode bit replicate top 8 bits
  488. // so rest of codec can assume 0xFFFF max range everywhere
  489. vint colori_r8 = asr<8>(colori_r) * vint(257);
  490. colori_r = select(colori_r, colori_r8, vmask(u8_mask.lane<0>()));
  491. vint colori_g8 = asr<8>(colori_g) * vint(257);
  492. colori_g = select(colori_g, colori_g8, vmask(u8_mask.lane<1>()));
  493. vint colori_b8 = asr<8>(colori_b) * vint(257);
  494. colori_b = select(colori_b, colori_b8, vmask(u8_mask.lane<2>()));
  495. vint colori_a8 = asr<8>(colori_a) * vint(257);
  496. colori_a = select(colori_a, colori_a8, vmask(u8_mask.lane<3>()));
  497. // Compute color diff
  498. vfloat color_r = int_to_float(colori_r);
  499. vfloat color_g = int_to_float(colori_g);
  500. vfloat color_b = int_to_float(colori_b);
  501. vfloat color_a = int_to_float(colori_a);
  502. vfloat color_orig_r = loada(blk.data_r + i);
  503. vfloat color_orig_g = loada(blk.data_g + i);
  504. vfloat color_orig_b = loada(blk.data_b + i);
  505. vfloat color_orig_a = loada(blk.data_a + i);
  506. vfloat color_error_r = min(abs(color_orig_r - color_r), vfloat(1e15f));
  507. vfloat color_error_g = min(abs(color_orig_g - color_g), vfloat(1e15f));
  508. vfloat color_error_b = min(abs(color_orig_b - color_b), vfloat(1e15f));
  509. vfloat color_error_a = min(abs(color_orig_a - color_a), vfloat(1e15f));
  510. // Compute squared error metric
  511. color_error_r = color_error_r * color_error_r;
  512. color_error_g = color_error_g * color_error_g;
  513. color_error_b = color_error_b * color_error_b;
  514. color_error_a = color_error_a * color_error_a;
  515. vfloat metric = color_error_r * blk.channel_weight.lane<0>()
  516. + color_error_g * blk.channel_weight.lane<1>()
  517. + color_error_b * blk.channel_weight.lane<2>()
  518. + color_error_a * blk.channel_weight.lane<3>();
  519. // Mask off bad lanes
  520. vmask mask = lane_id < vint(texel_count);
  521. lane_id += vint(ASTCENC_SIMD_WIDTH);
  522. haccumulate(summav, metric, mask);
  523. }
  524. return hadd_s(summav);
  525. }
  526. #endif