astcenc_pick_best_endpoint_format.cpp 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353
  1. // SPDX-License-Identifier: Apache-2.0
  2. // ----------------------------------------------------------------------------
  3. // Copyright 2011-2025 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. #if !defined(ASTCENC_DECOMPRESS_ONLY)
  18. /**
  19. * @brief Functions for finding best endpoint format.
  20. *
  21. * We assume there are two independent sources of error in any given partition:
  22. *
  23. * - Encoding choice errors
  24. * - Quantization errors
  25. *
  26. * Encoding choice errors are caused by encoder decisions. For example:
  27. *
  28. * - Using luminance instead of separate RGB components.
  29. * - Using a constant 1.0 alpha instead of storing an alpha component.
  30. * - Using RGB+scale instead of storing two full RGB endpoints.
  31. *
  32. * Quantization errors occur due to the limited precision we use for storage. These errors generally
  33. * scale with quantization level, but are not actually independent of color encoding. In particular:
  34. *
  35. * - If we can use offset encoding then quantization error is halved.
  36. * - If we can use blue-contraction then quantization error for RG is halved.
  37. * - If we use HDR endpoints the quantization error is higher.
  38. *
  39. * Apart from these effects, we assume the error is proportional to the quantization step size.
  40. */
  41. #include "astcenc_internal.h"
  42. #include "astcenc_vecmathlib.h"
  43. #include <assert.h>
  44. /**
  45. * @brief Compute the errors of the endpoint line options for one partition.
  46. *
  47. * Uncorrelated data assumes storing completely independent RGBA channels for each endpoint. Same
  48. * chroma data assumes storing RGBA endpoints which pass though the origin (LDR only). RGBL data
  49. * assumes storing RGB + lumashift (HDR only). Luminance error assumes storing RGB channels as a
  50. * single value.
  51. *
  52. *
  53. * @param pi The partition info data.
  54. * @param partition_index The partition index to compule the error for.
  55. * @param blk The image block.
  56. * @param uncor_pline The endpoint line assuming uncorrelated endpoints.
  57. * @param[out] uncor_err The computed error for the uncorrelated endpoint line.
  58. * @param samec_pline The endpoint line assuming the same chroma for both endpoints.
  59. * @param[out] samec_err The computed error for the uncorrelated endpoint line.
  60. * @param rgbl_pline The endpoint line assuming RGB + lumashift data.
  61. * @param[out] rgbl_err The computed error for the RGB + lumashift endpoint line.
  62. * @param l_pline The endpoint line assuming luminance data.
  63. * @param[out] l_err The computed error for the luminance endpoint line.
  64. * @param[out] a_drop_err The computed error for dropping the alpha component.
  65. */
  66. static void compute_error_squared_rgb_single_partition(
  67. const partition_info& pi,
  68. int partition_index,
  69. const image_block& blk,
  70. const processed_line3& uncor_pline,
  71. float& uncor_err,
  72. const processed_line3& samec_pline,
  73. float& samec_err,
  74. const processed_line3& rgbl_pline,
  75. float& rgbl_err,
  76. const processed_line3& l_pline,
  77. float& l_err,
  78. float& a_drop_err
  79. ) {
  80. vfloat4 ews = blk.channel_weight;
  81. unsigned int texel_count = pi.partition_texel_count[partition_index];
  82. const uint8_t* texel_indexes = pi.texels_of_partition[partition_index];
  83. promise(texel_count > 0);
  84. vfloatacc a_drop_errv = vfloatacc::zero();
  85. vfloat default_a(blk.get_default_alpha());
  86. vfloatacc uncor_errv = vfloatacc::zero();
  87. vfloat uncor_bs0(uncor_pline.bs.lane<0>());
  88. vfloat uncor_bs1(uncor_pline.bs.lane<1>());
  89. vfloat uncor_bs2(uncor_pline.bs.lane<2>());
  90. vfloat uncor_amod0(uncor_pline.amod.lane<0>());
  91. vfloat uncor_amod1(uncor_pline.amod.lane<1>());
  92. vfloat uncor_amod2(uncor_pline.amod.lane<2>());
  93. vfloatacc samec_errv = vfloatacc::zero();
  94. vfloat samec_bs0(samec_pline.bs.lane<0>());
  95. vfloat samec_bs1(samec_pline.bs.lane<1>());
  96. vfloat samec_bs2(samec_pline.bs.lane<2>());
  97. vfloatacc rgbl_errv = vfloatacc::zero();
  98. vfloat rgbl_bs0(rgbl_pline.bs.lane<0>());
  99. vfloat rgbl_bs1(rgbl_pline.bs.lane<1>());
  100. vfloat rgbl_bs2(rgbl_pline.bs.lane<2>());
  101. vfloat rgbl_amod0(rgbl_pline.amod.lane<0>());
  102. vfloat rgbl_amod1(rgbl_pline.amod.lane<1>());
  103. vfloat rgbl_amod2(rgbl_pline.amod.lane<2>());
  104. vfloatacc l_errv = vfloatacc::zero();
  105. vfloat l_bs0(l_pline.bs.lane<0>());
  106. vfloat l_bs1(l_pline.bs.lane<1>());
  107. vfloat l_bs2(l_pline.bs.lane<2>());
  108. vint lane_ids = vint::lane_id();
  109. for (unsigned int i = 0; i < texel_count; i += ASTCENC_SIMD_WIDTH)
  110. {
  111. const uint8_t* tix = texel_indexes + i;
  112. vmask mask = lane_ids < vint(texel_count);
  113. lane_ids += vint(ASTCENC_SIMD_WIDTH);
  114. // Compute the error that arises from just ditching alpha
  115. vfloat data_a = gatherf_byte_inds<vfloat>(blk.data_a, tix);
  116. vfloat alpha_diff = data_a - default_a;
  117. alpha_diff = alpha_diff * alpha_diff;
  118. haccumulate(a_drop_errv, alpha_diff, mask);
  119. vfloat data_r = gatherf_byte_inds<vfloat>(blk.data_r, tix);
  120. vfloat data_g = gatherf_byte_inds<vfloat>(blk.data_g, tix);
  121. vfloat data_b = gatherf_byte_inds<vfloat>(blk.data_b, tix);
  122. // Compute uncorrelated error
  123. vfloat param = data_r * uncor_bs0
  124. + data_g * uncor_bs1
  125. + data_b * uncor_bs2;
  126. vfloat dist0 = (uncor_amod0 + param * uncor_bs0) - data_r;
  127. vfloat dist1 = (uncor_amod1 + param * uncor_bs1) - data_g;
  128. vfloat dist2 = (uncor_amod2 + param * uncor_bs2) - data_b;
  129. vfloat error = dist0 * dist0 * ews.lane<0>()
  130. + dist1 * dist1 * ews.lane<1>()
  131. + dist2 * dist2 * ews.lane<2>();
  132. haccumulate(uncor_errv, error, mask);
  133. // Compute same chroma error - no "amod", its always zero
  134. param = data_r * samec_bs0
  135. + data_g * samec_bs1
  136. + data_b * samec_bs2;
  137. dist0 = (param * samec_bs0) - data_r;
  138. dist1 = (param * samec_bs1) - data_g;
  139. dist2 = (param * samec_bs2) - data_b;
  140. error = dist0 * dist0 * ews.lane<0>()
  141. + dist1 * dist1 * ews.lane<1>()
  142. + dist2 * dist2 * ews.lane<2>();
  143. haccumulate(samec_errv, error, mask);
  144. // Compute rgbl error
  145. param = data_r * rgbl_bs0
  146. + data_g * rgbl_bs1
  147. + data_b * rgbl_bs2;
  148. dist0 = (rgbl_amod0 + param * rgbl_bs0) - data_r;
  149. dist1 = (rgbl_amod1 + param * rgbl_bs1) - data_g;
  150. dist2 = (rgbl_amod2 + param * rgbl_bs2) - data_b;
  151. error = dist0 * dist0 * ews.lane<0>()
  152. + dist1 * dist1 * ews.lane<1>()
  153. + dist2 * dist2 * ews.lane<2>();
  154. haccumulate(rgbl_errv, error, mask);
  155. // Compute luma error - no "amod", its always zero
  156. param = data_r * l_bs0
  157. + data_g * l_bs1
  158. + data_b * l_bs2;
  159. dist0 = (param * l_bs0) - data_r;
  160. dist1 = (param * l_bs1) - data_g;
  161. dist2 = (param * l_bs2) - data_b;
  162. error = dist0 * dist0 * ews.lane<0>()
  163. + dist1 * dist1 * ews.lane<1>()
  164. + dist2 * dist2 * ews.lane<2>();
  165. haccumulate(l_errv, error, mask);
  166. }
  167. a_drop_err = hadd_s(a_drop_errv) * ews.lane<3>();
  168. uncor_err = hadd_s(uncor_errv);
  169. samec_err = hadd_s(samec_errv);
  170. rgbl_err = hadd_s(rgbl_errv);
  171. l_err = hadd_s(l_errv);
  172. }
  173. /**
  174. * @brief For a given set of input colors and partitioning determine endpoint encode errors.
  175. *
  176. * This function determines the color error that results from RGB-scale encoding (LDR only),
  177. * RGB-lumashift encoding (HDR only), luminance-encoding, and alpha drop. Also determines whether
  178. * the endpoints are eligible for offset encoding or blue-contraction
  179. *
  180. * @param blk The image block.
  181. * @param pi The partition info data.
  182. * @param ep The idealized endpoints.
  183. * @param[out] eci The resulting encoding choice error metrics.
  184. */
  185. static void compute_encoding_choice_errors(
  186. const image_block& blk,
  187. const partition_info& pi,
  188. const endpoints& ep,
  189. encoding_choice_errors eci[BLOCK_MAX_PARTITIONS])
  190. {
  191. int partition_count = pi.partition_count;
  192. promise(partition_count > 0);
  193. partition_metrics pms[BLOCK_MAX_PARTITIONS];
  194. compute_avgs_and_dirs_3_comp_rgb(pi, blk, pms);
  195. for (int i = 0; i < partition_count; i++)
  196. {
  197. partition_metrics& pm = pms[i];
  198. line3 uncor_rgb_lines;
  199. line3 samec_rgb_lines; // for LDR-RGB-scale
  200. line3 rgb_luma_lines; // for HDR-RGB-scale
  201. processed_line3 uncor_rgb_plines;
  202. processed_line3 samec_rgb_plines;
  203. processed_line3 rgb_luma_plines;
  204. processed_line3 luminance_plines;
  205. float uncorr_rgb_error;
  206. float samechroma_rgb_error;
  207. float rgb_luma_error;
  208. float luminance_rgb_error;
  209. float alpha_drop_error;
  210. uncor_rgb_lines.a = pm.avg;
  211. uncor_rgb_lines.b = normalize_safe(pm.dir, unit3());
  212. samec_rgb_lines.a = vfloat4::zero();
  213. samec_rgb_lines.b = normalize_safe(pm.avg, unit3());
  214. rgb_luma_lines.a = pm.avg;
  215. rgb_luma_lines.b = unit3();
  216. uncor_rgb_plines.amod = uncor_rgb_lines.a - uncor_rgb_lines.b * dot3(uncor_rgb_lines.a, uncor_rgb_lines.b);
  217. uncor_rgb_plines.bs = uncor_rgb_lines.b;
  218. // Same chroma always goes though zero, so this is simpler than the others
  219. samec_rgb_plines.amod = vfloat4::zero();
  220. samec_rgb_plines.bs = samec_rgb_lines.b;
  221. rgb_luma_plines.amod = rgb_luma_lines.a - rgb_luma_lines.b * dot3(rgb_luma_lines.a, rgb_luma_lines.b);
  222. rgb_luma_plines.bs = rgb_luma_lines.b;
  223. // Luminance always goes though zero, so this is simpler than the others
  224. luminance_plines.amod = vfloat4::zero();
  225. luminance_plines.bs = unit3();
  226. compute_error_squared_rgb_single_partition(
  227. pi, i, blk,
  228. uncor_rgb_plines, uncorr_rgb_error,
  229. samec_rgb_plines, samechroma_rgb_error,
  230. rgb_luma_plines, rgb_luma_error,
  231. luminance_plines, luminance_rgb_error,
  232. alpha_drop_error);
  233. // Determine if we can offset encode RGB lanes
  234. vfloat4 endpt0 = ep.endpt0[i];
  235. vfloat4 endpt1 = ep.endpt1[i];
  236. vfloat4 endpt_diff = abs(endpt1 - endpt0);
  237. vmask4 endpt_can_offset = endpt_diff < vfloat4(0.12f * 65535.0f);
  238. bool can_offset_encode = (mask(endpt_can_offset) & 0x7) == 0x7;
  239. // Store out the settings
  240. eci[i].rgb_scale_error = (samechroma_rgb_error - uncorr_rgb_error) * 0.7f; // empirical
  241. eci[i].rgb_luma_error = (rgb_luma_error - uncorr_rgb_error) * 1.5f; // wild guess
  242. eci[i].luminance_error = (luminance_rgb_error - uncorr_rgb_error) * 3.0f; // empirical
  243. eci[i].alpha_drop_error = alpha_drop_error * 3.0f;
  244. eci[i].can_offset_encode = can_offset_encode;
  245. eci[i].can_blue_contract = !blk.is_luminance();
  246. }
  247. }
  248. /**
  249. * @brief For a given partition compute the error for every endpoint integer count and quant level.
  250. *
  251. * @param encode_hdr_rgb @c true if using HDR for RGB, @c false for LDR.
  252. * @param encode_hdr_alpha @c true if using HDR for alpha, @c false for LDR.
  253. * @param partition_index The partition index.
  254. * @param pi The partition info.
  255. * @param eci The encoding choice error metrics.
  256. * @param ep The idealized endpoints.
  257. * @param error_weight The resulting encoding choice error metrics.
  258. * @param[out] best_error The best error for each integer count and quant level.
  259. * @param[out] format_of_choice The preferred endpoint format for each integer count and quant level.
  260. */
  261. static void compute_color_error_for_every_integer_count_and_quant_level(
  262. bool encode_hdr_rgb,
  263. bool encode_hdr_alpha,
  264. int partition_index,
  265. const partition_info& pi,
  266. const encoding_choice_errors& eci,
  267. const endpoints& ep,
  268. vfloat4 error_weight,
  269. float best_error[21][4],
  270. uint8_t format_of_choice[21][4]
  271. ) {
  272. int partition_size = pi.partition_texel_count[partition_index];
  273. static const float baseline_quant_error[21 - QUANT_6] {
  274. (65536.0f * 65536.0f / 18.0f) / (5 * 5),
  275. (65536.0f * 65536.0f / 18.0f) / (7 * 7),
  276. (65536.0f * 65536.0f / 18.0f) / (9 * 9),
  277. (65536.0f * 65536.0f / 18.0f) / (11 * 11),
  278. (65536.0f * 65536.0f / 18.0f) / (15 * 15),
  279. (65536.0f * 65536.0f / 18.0f) / (19 * 19),
  280. (65536.0f * 65536.0f / 18.0f) / (23 * 23),
  281. (65536.0f * 65536.0f / 18.0f) / (31 * 31),
  282. (65536.0f * 65536.0f / 18.0f) / (39 * 39),
  283. (65536.0f * 65536.0f / 18.0f) / (47 * 47),
  284. (65536.0f * 65536.0f / 18.0f) / (63 * 63),
  285. (65536.0f * 65536.0f / 18.0f) / (79 * 79),
  286. (65536.0f * 65536.0f / 18.0f) / (95 * 95),
  287. (65536.0f * 65536.0f / 18.0f) / (127 * 127),
  288. (65536.0f * 65536.0f / 18.0f) / (159 * 159),
  289. (65536.0f * 65536.0f / 18.0f) / (191 * 191),
  290. (65536.0f * 65536.0f / 18.0f) / (255 * 255)
  291. };
  292. vfloat4 ep0 = ep.endpt0[partition_index];
  293. vfloat4 ep1 = ep.endpt1[partition_index];
  294. float ep1_min = hmin_rgb_s(ep1);
  295. ep1_min = astc::max(ep1_min, 0.0f);
  296. float error_weight_rgbsum = hadd_rgb_s(error_weight);
  297. float range_upper_limit_rgb = encode_hdr_rgb ? 61440.0f : 65535.0f;
  298. float range_upper_limit_alpha = encode_hdr_alpha ? 61440.0f : 65535.0f;
  299. // It is possible to get endpoint colors significantly outside [0,upper-limit] even if the
  300. // input data are safely contained in [0,upper-limit]; we need to add an error term for this
  301. vfloat4 offset(range_upper_limit_rgb, range_upper_limit_rgb, range_upper_limit_rgb, range_upper_limit_alpha);
  302. vfloat4 ep0_range_error_high = max(ep0 - offset, 0.0f);
  303. vfloat4 ep1_range_error_high = max(ep1 - offset, 0.0f);
  304. vfloat4 ep0_range_error_low = min(ep0, 0.0f);
  305. vfloat4 ep1_range_error_low = min(ep1, 0.0f);
  306. vfloat4 sum_range_error =
  307. (ep0_range_error_low * ep0_range_error_low) +
  308. (ep1_range_error_low * ep1_range_error_low) +
  309. (ep0_range_error_high * ep0_range_error_high) +
  310. (ep1_range_error_high * ep1_range_error_high);
  311. float rgb_range_error = dot3_s(sum_range_error, error_weight)
  312. * 0.5f * static_cast<float>(partition_size);
  313. float alpha_range_error = sum_range_error.lane<3>() * error_weight.lane<3>()
  314. * 0.5f * static_cast<float>(partition_size);
  315. if (encode_hdr_rgb)
  316. {
  317. // Collect some statistics
  318. float af, cf;
  319. if (ep1.lane<0>() > ep1.lane<1>() && ep1.lane<0>() > ep1.lane<2>())
  320. {
  321. af = ep1.lane<0>();
  322. cf = ep1.lane<0>() - ep0.lane<0>();
  323. }
  324. else if (ep1.lane<1>() > ep1.lane<2>())
  325. {
  326. af = ep1.lane<1>();
  327. cf = ep1.lane<1>() - ep0.lane<1>();
  328. }
  329. else
  330. {
  331. af = ep1.lane<2>();
  332. cf = ep1.lane<2>() - ep0.lane<2>();
  333. }
  334. // Estimate of color-component spread in high endpoint color
  335. float bf = af - ep1_min;
  336. vfloat4 prd = (ep1 - vfloat4(cf)).swz<0, 1, 2>();
  337. vfloat4 pdif = prd - ep0.swz<0, 1, 2>();
  338. // Estimate of color-component spread in low endpoint color
  339. float df = hmax_s(abs(pdif));
  340. int b = static_cast<int>(bf);
  341. int c = static_cast<int>(cf);
  342. int d = static_cast<int>(df);
  343. // Determine which one of the 6 submodes is likely to be used in case of an RGBO-mode
  344. int rgbo_mode = 5; // 7 bits per component
  345. // mode 4: 8 7 6
  346. if (b < 32768 && c < 16384)
  347. {
  348. rgbo_mode = 4;
  349. }
  350. // mode 3: 9 6 7
  351. if (b < 8192 && c < 16384)
  352. {
  353. rgbo_mode = 3;
  354. }
  355. // mode 2: 10 5 8
  356. if (b < 2048 && c < 16384)
  357. {
  358. rgbo_mode = 2;
  359. }
  360. // mode 1: 11 6 5
  361. if (b < 2048 && c < 1024)
  362. {
  363. rgbo_mode = 1;
  364. }
  365. // mode 0: 11 5 7
  366. if (b < 1024 && c < 4096)
  367. {
  368. rgbo_mode = 0;
  369. }
  370. // Determine which one of the 9 submodes is likely to be used in case of an RGB-mode.
  371. int rgb_mode = 8; // 8 bits per component, except 7 bits for blue
  372. // mode 0: 9 7 6 7
  373. if (b < 16384 && c < 8192 && d < 8192)
  374. {
  375. rgb_mode = 0;
  376. }
  377. // mode 1: 9 8 6 6
  378. if (b < 32768 && c < 8192 && d < 4096)
  379. {
  380. rgb_mode = 1;
  381. }
  382. // mode 2: 10 6 7 7
  383. if (b < 4096 && c < 8192 && d < 4096)
  384. {
  385. rgb_mode = 2;
  386. }
  387. // mode 3: 10 7 7 6
  388. if (b < 8192 && c < 8192 && d < 2048)
  389. {
  390. rgb_mode = 3;
  391. }
  392. // mode 4: 11 8 6 5
  393. if (b < 8192 && c < 2048 && d < 512)
  394. {
  395. rgb_mode = 4;
  396. }
  397. // mode 5: 11 6 8 6
  398. if (b < 2048 && c < 8192 && d < 1024)
  399. {
  400. rgb_mode = 5;
  401. }
  402. // mode 6: 12 7 7 5
  403. if (b < 2048 && c < 2048 && d < 256)
  404. {
  405. rgb_mode = 6;
  406. }
  407. // mode 7: 12 6 7 6
  408. if (b < 1024 && c < 2048 && d < 512)
  409. {
  410. rgb_mode = 7;
  411. }
  412. static const float rgbo_error_scales[6] { 4.0f, 4.0f, 16.0f, 64.0f, 256.0f, 1024.0f };
  413. static const float rgb_error_scales[9] { 64.0f, 64.0f, 16.0f, 16.0f, 4.0f, 4.0f, 1.0f, 1.0f, 384.0f };
  414. float mode7mult = rgbo_error_scales[rgbo_mode] * 0.0015f; // Empirically determined ....
  415. float mode11mult = rgb_error_scales[rgb_mode] * 0.010f; // Empirically determined ....
  416. float lum_high = hadd_rgb_s(ep1) * (1.0f / 3.0f);
  417. float lum_low = hadd_rgb_s(ep0) * (1.0f / 3.0f);
  418. float lumdif = lum_high - lum_low;
  419. float mode23mult = lumdif < 960 ? 4.0f : lumdif < 3968 ? 16.0f : 128.0f;
  420. mode23mult *= 0.0005f; // Empirically determined ....
  421. // Pick among the available HDR endpoint modes
  422. for (int i = QUANT_2; i < QUANT_16; i++)
  423. {
  424. best_error[i][3] = ERROR_CALC_DEFAULT;
  425. best_error[i][2] = ERROR_CALC_DEFAULT;
  426. best_error[i][1] = ERROR_CALC_DEFAULT;
  427. best_error[i][0] = ERROR_CALC_DEFAULT;
  428. format_of_choice[i][3] = static_cast<uint8_t>(encode_hdr_alpha ? FMT_HDR_RGBA : FMT_HDR_RGB_LDR_ALPHA);
  429. format_of_choice[i][2] = FMT_HDR_RGB;
  430. format_of_choice[i][1] = FMT_HDR_RGB_SCALE;
  431. format_of_choice[i][0] = FMT_HDR_LUMINANCE_LARGE_RANGE;
  432. }
  433. for (int i = QUANT_16; i <= QUANT_256; i++)
  434. {
  435. // The base_quant_error should depend on the scale-factor that would be used during
  436. // actual encode of the color value
  437. float base_quant_error = baseline_quant_error[i - QUANT_6] * static_cast<float>(partition_size);
  438. float rgb_quantization_error = error_weight_rgbsum * base_quant_error * 2.0f;
  439. float alpha_quantization_error = error_weight.lane<3>() * base_quant_error * 2.0f;
  440. float rgba_quantization_error = rgb_quantization_error + alpha_quantization_error;
  441. // For 8 integers, we have two encodings: one with HDR A and another one with LDR A
  442. float full_hdr_rgba_error = rgba_quantization_error + rgb_range_error + alpha_range_error;
  443. best_error[i][3] = full_hdr_rgba_error;
  444. format_of_choice[i][3] = static_cast<uint8_t>(encode_hdr_alpha ? FMT_HDR_RGBA : FMT_HDR_RGB_LDR_ALPHA);
  445. // For 6 integers, we have one HDR-RGB encoding
  446. float full_hdr_rgb_error = (rgb_quantization_error * mode11mult) + rgb_range_error + eci.alpha_drop_error;
  447. best_error[i][2] = full_hdr_rgb_error;
  448. format_of_choice[i][2] = FMT_HDR_RGB;
  449. // For 4 integers, we have one HDR-RGB-Scale encoding
  450. float hdr_rgb_scale_error = (rgb_quantization_error * mode7mult) + rgb_range_error + eci.alpha_drop_error + eci.rgb_luma_error;
  451. best_error[i][1] = hdr_rgb_scale_error;
  452. format_of_choice[i][1] = FMT_HDR_RGB_SCALE;
  453. // For 2 integers, we assume luminance-with-large-range
  454. float hdr_luminance_error = (rgb_quantization_error * mode23mult) + rgb_range_error + eci.alpha_drop_error + eci.luminance_error;
  455. best_error[i][0] = hdr_luminance_error;
  456. format_of_choice[i][0] = FMT_HDR_LUMINANCE_LARGE_RANGE;
  457. }
  458. }
  459. else
  460. {
  461. for (int i = QUANT_2; i < QUANT_6; i++)
  462. {
  463. best_error[i][3] = ERROR_CALC_DEFAULT;
  464. best_error[i][2] = ERROR_CALC_DEFAULT;
  465. best_error[i][1] = ERROR_CALC_DEFAULT;
  466. best_error[i][0] = ERROR_CALC_DEFAULT;
  467. format_of_choice[i][3] = FMT_RGBA;
  468. format_of_choice[i][2] = FMT_RGB;
  469. format_of_choice[i][1] = FMT_RGB_SCALE;
  470. format_of_choice[i][0] = FMT_LUMINANCE;
  471. }
  472. float base_quant_error_rgb = error_weight_rgbsum * static_cast<float>(partition_size);
  473. float base_quant_error_a = error_weight.lane<3>() * static_cast<float>(partition_size);
  474. float base_quant_error_rgba = base_quant_error_rgb + base_quant_error_a;
  475. float error_scale_bc_rgba = eci.can_blue_contract ? 0.625f : 1.0f;
  476. float error_scale_oe_rgba = eci.can_offset_encode ? 0.5f : 1.0f;
  477. float error_scale_bc_rgb = eci.can_blue_contract ? 0.5f : 1.0f;
  478. float error_scale_oe_rgb = eci.can_offset_encode ? 0.25f : 1.0f;
  479. // Pick among the available LDR endpoint modes
  480. for (int i = QUANT_6; i <= QUANT_256; i++)
  481. {
  482. // Offset encoding not possible at higher quant levels
  483. if (i >= QUANT_192)
  484. {
  485. error_scale_oe_rgba = 1.0f;
  486. error_scale_oe_rgb = 1.0f;
  487. }
  488. float base_quant_error = baseline_quant_error[i - QUANT_6];
  489. float quant_error_rgb = base_quant_error_rgb * base_quant_error;
  490. float quant_error_rgba = base_quant_error_rgba * base_quant_error;
  491. // 8 integers can encode as RGBA+RGBA
  492. float full_ldr_rgba_error = quant_error_rgba
  493. * error_scale_bc_rgba
  494. * error_scale_oe_rgba
  495. + rgb_range_error
  496. + alpha_range_error;
  497. best_error[i][3] = full_ldr_rgba_error;
  498. format_of_choice[i][3] = FMT_RGBA;
  499. // 6 integers can encode as RGB+RGB or RGBS+AA
  500. float full_ldr_rgb_error = quant_error_rgb
  501. * error_scale_bc_rgb
  502. * error_scale_oe_rgb
  503. + rgb_range_error
  504. + eci.alpha_drop_error;
  505. float rgbs_alpha_error = quant_error_rgba
  506. + eci.rgb_scale_error
  507. + rgb_range_error
  508. + alpha_range_error;
  509. if (rgbs_alpha_error < full_ldr_rgb_error)
  510. {
  511. best_error[i][2] = rgbs_alpha_error;
  512. format_of_choice[i][2] = FMT_RGB_SCALE_ALPHA;
  513. }
  514. else
  515. {
  516. best_error[i][2] = full_ldr_rgb_error;
  517. format_of_choice[i][2] = FMT_RGB;
  518. }
  519. // 4 integers can encode as RGBS or LA+LA
  520. float ldr_rgbs_error = quant_error_rgb
  521. + rgb_range_error
  522. + eci.alpha_drop_error
  523. + eci.rgb_scale_error;
  524. float lum_alpha_error = quant_error_rgba
  525. + rgb_range_error
  526. + alpha_range_error
  527. + eci.luminance_error;
  528. if (ldr_rgbs_error < lum_alpha_error)
  529. {
  530. best_error[i][1] = ldr_rgbs_error;
  531. format_of_choice[i][1] = FMT_RGB_SCALE;
  532. }
  533. else
  534. {
  535. best_error[i][1] = lum_alpha_error;
  536. format_of_choice[i][1] = FMT_LUMINANCE_ALPHA;
  537. }
  538. // 2 integers can encode as L+L
  539. float luminance_error = quant_error_rgb
  540. + rgb_range_error
  541. + eci.alpha_drop_error
  542. + eci.luminance_error;
  543. best_error[i][0] = luminance_error;
  544. format_of_choice[i][0] = FMT_LUMINANCE;
  545. }
  546. }
  547. }
  548. /**
  549. * @brief For one partition compute the best format and quantization for a given bit count.
  550. *
  551. * @param best_combined_error The best error for each quant level and integer count.
  552. * @param best_combined_format The best format for each quant level and integer count.
  553. * @param bits_available The number of bits available for encoding.
  554. * @param[out] best_quant_level The output best color quant level.
  555. * @param[out] best_format The output best color format.
  556. *
  557. * @return The output error for the best pairing.
  558. */
  559. static float one_partition_find_best_combination_for_bitcount(
  560. const float best_combined_error[21][4],
  561. const uint8_t best_combined_format[21][4],
  562. int bits_available,
  563. uint8_t& best_quant_level,
  564. uint8_t& best_format
  565. ) {
  566. int best_integer_count = 0;
  567. float best_integer_count_error = ERROR_CALC_DEFAULT;
  568. for (int integer_count = 1; integer_count <= 4; integer_count++)
  569. {
  570. // Compute the quantization level for a given number of integers and a given number of bits
  571. int quant_level = quant_mode_table[integer_count][bits_available];
  572. // Don't have enough bits to represent a given endpoint format at all!
  573. if (quant_level < QUANT_6)
  574. {
  575. continue;
  576. }
  577. float integer_count_error = best_combined_error[quant_level][integer_count - 1];
  578. if (integer_count_error < best_integer_count_error)
  579. {
  580. best_integer_count_error = integer_count_error;
  581. best_integer_count = integer_count - 1;
  582. }
  583. }
  584. int ql = quant_mode_table[best_integer_count + 1][bits_available];
  585. best_quant_level = static_cast<uint8_t>(ql);
  586. best_format = FMT_LUMINANCE;
  587. if (ql >= QUANT_6)
  588. {
  589. best_format = best_combined_format[ql][best_integer_count];
  590. }
  591. return best_integer_count_error;
  592. }
  593. /**
  594. * @brief For 2 partitions compute the best format combinations for every pair of quant mode and integer count.
  595. *
  596. * @param best_error The best error for a single endpoint quant level and integer count.
  597. * @param best_format The best format for a single endpoint quant level and integer count.
  598. * @param[out] best_combined_error The best combined error pairings for the 2 partitions.
  599. * @param[out] best_combined_format The best combined format pairings for the 2 partitions.
  600. */
  601. static void two_partitions_find_best_combination_for_every_quantization_and_integer_count(
  602. const float best_error[2][21][4], // indexed by (partition, quant-level, integer-pair-count-minus-1)
  603. const uint8_t best_format[2][21][4],
  604. float best_combined_error[21][7], // indexed by (quant-level, integer-pair-count-minus-2)
  605. uint8_t best_combined_format[21][7][2]
  606. ) {
  607. for (int i = QUANT_2; i <= QUANT_256; i++)
  608. {
  609. for (int j = 0; j < 7; j++)
  610. {
  611. best_combined_error[i][j] = ERROR_CALC_DEFAULT;
  612. }
  613. }
  614. for (int quant = QUANT_6; quant <= QUANT_256; quant++)
  615. {
  616. for (int i = 0; i < 4; i++) // integer-count for first endpoint-pair
  617. {
  618. for (int j = 0; j < 4; j++) // integer-count for second endpoint-pair
  619. {
  620. int low2 = astc::min(i, j);
  621. int high2 = astc::max(i, j);
  622. if ((high2 - low2) > 1)
  623. {
  624. continue;
  625. }
  626. int intcnt = i + j;
  627. float errorterm = astc::min(best_error[0][quant][i] + best_error[1][quant][j], 1e10f);
  628. if (errorterm <= best_combined_error[quant][intcnt])
  629. {
  630. best_combined_error[quant][intcnt] = errorterm;
  631. best_combined_format[quant][intcnt][0] = best_format[0][quant][i];
  632. best_combined_format[quant][intcnt][1] = best_format[1][quant][j];
  633. }
  634. }
  635. }
  636. }
  637. }
  638. /**
  639. * @brief For 2 partitions compute the best format and quantization for a given bit count.
  640. *
  641. * @param best_combined_error The best error for each quant level and integer count.
  642. * @param best_combined_format The best format for each quant level and integer count.
  643. * @param bits_available The number of bits available for encoding.
  644. * @param[out] best_quant_level The output best color quant level.
  645. * @param[out] best_quant_level_mod The output best color quant level assuming two more bits are available.
  646. * @param[out] best_formats The output best color formats.
  647. *
  648. * @return The output error for the best pairing.
  649. */
  650. static float two_partitions_find_best_combination_for_bitcount(
  651. float best_combined_error[21][7],
  652. uint8_t best_combined_format[21][7][2],
  653. int bits_available,
  654. uint8_t& best_quant_level,
  655. uint8_t& best_quant_level_mod,
  656. uint8_t* best_formats
  657. ) {
  658. int best_integer_count = 0;
  659. float best_integer_count_error = ERROR_CALC_DEFAULT;
  660. for (int integer_count = 2; integer_count <= 8; integer_count++)
  661. {
  662. // Compute the quantization level for a given number of integers and a given number of bits
  663. int quant_level = quant_mode_table[integer_count][bits_available];
  664. // Don't have enough bits to represent a given endpoint format at all!
  665. if (quant_level < QUANT_6)
  666. {
  667. break;
  668. }
  669. float integer_count_error = best_combined_error[quant_level][integer_count - 2];
  670. if (integer_count_error < best_integer_count_error)
  671. {
  672. best_integer_count_error = integer_count_error;
  673. best_integer_count = integer_count;
  674. }
  675. }
  676. int ql = quant_mode_table[best_integer_count][bits_available];
  677. int ql_mod = quant_mode_table[best_integer_count][bits_available + 2];
  678. best_quant_level = static_cast<uint8_t>(ql);
  679. best_quant_level_mod = static_cast<uint8_t>(ql_mod);
  680. if (ql >= QUANT_6)
  681. {
  682. for (int i = 0; i < 2; i++)
  683. {
  684. best_formats[i] = best_combined_format[ql][best_integer_count - 2][i];
  685. }
  686. }
  687. else
  688. {
  689. for (int i = 0; i < 2; i++)
  690. {
  691. best_formats[i] = FMT_LUMINANCE;
  692. }
  693. }
  694. return best_integer_count_error;
  695. }
  696. /**
  697. * @brief For 3 partitions compute the best format combinations for every pair of quant mode and integer count.
  698. *
  699. * @param best_error The best error for a single endpoint quant level and integer count.
  700. * @param best_format The best format for a single endpoint quant level and integer count.
  701. * @param[out] best_combined_error The best combined error pairings for the 3 partitions.
  702. * @param[out] best_combined_format The best combined format pairings for the 3 partitions.
  703. */
  704. static void three_partitions_find_best_combination_for_every_quantization_and_integer_count(
  705. const float best_error[3][21][4], // indexed by (partition, quant-level, integer-count)
  706. const uint8_t best_format[3][21][4],
  707. float best_combined_error[21][10],
  708. uint8_t best_combined_format[21][10][3]
  709. ) {
  710. for (int i = QUANT_2; i <= QUANT_256; i++)
  711. {
  712. for (int j = 0; j < 10; j++)
  713. {
  714. best_combined_error[i][j] = ERROR_CALC_DEFAULT;
  715. }
  716. }
  717. for (int quant = QUANT_6; quant <= QUANT_256; quant++)
  718. {
  719. for (int i = 0; i < 4; i++) // integer-count for first endpoint-pair
  720. {
  721. for (int j = 0; j < 4; j++) // integer-count for second endpoint-pair
  722. {
  723. int low2 = astc::min(i, j);
  724. int high2 = astc::max(i, j);
  725. if ((high2 - low2) > 1)
  726. {
  727. continue;
  728. }
  729. for (int k = 0; k < 4; k++) // integer-count for third endpoint-pair
  730. {
  731. int low3 = astc::min(k, low2);
  732. int high3 = astc::max(k, high2);
  733. if ((high3 - low3) > 1)
  734. {
  735. continue;
  736. }
  737. int intcnt = i + j + k;
  738. float errorterm = astc::min(best_error[0][quant][i] + best_error[1][quant][j] + best_error[2][quant][k], 1e10f);
  739. if (errorterm <= best_combined_error[quant][intcnt])
  740. {
  741. best_combined_error[quant][intcnt] = errorterm;
  742. best_combined_format[quant][intcnt][0] = best_format[0][quant][i];
  743. best_combined_format[quant][intcnt][1] = best_format[1][quant][j];
  744. best_combined_format[quant][intcnt][2] = best_format[2][quant][k];
  745. }
  746. }
  747. }
  748. }
  749. }
  750. }
  751. /**
  752. * @brief For 3 partitions compute the best format and quantization for a given bit count.
  753. *
  754. * @param best_combined_error The best error for each quant level and integer count.
  755. * @param best_combined_format The best format for each quant level and integer count.
  756. * @param bits_available The number of bits available for encoding.
  757. * @param[out] best_quant_level The output best color quant level.
  758. * @param[out] best_quant_level_mod The output best color quant level assuming two more bits are available.
  759. * @param[out] best_formats The output best color formats.
  760. *
  761. * @return The output error for the best pairing.
  762. */
  763. static float three_partitions_find_best_combination_for_bitcount(
  764. const float best_combined_error[21][10],
  765. const uint8_t best_combined_format[21][10][3],
  766. int bits_available,
  767. uint8_t& best_quant_level,
  768. uint8_t& best_quant_level_mod,
  769. uint8_t* best_formats
  770. ) {
  771. int best_integer_count = 0;
  772. float best_integer_count_error = ERROR_CALC_DEFAULT;
  773. for (int integer_count = 3; integer_count <= 9; integer_count++)
  774. {
  775. // Compute the quantization level for a given number of integers and a given number of bits
  776. int quant_level = quant_mode_table[integer_count][bits_available];
  777. // Don't have enough bits to represent a given endpoint format at all!
  778. if (quant_level < QUANT_6)
  779. {
  780. break;
  781. }
  782. float integer_count_error = best_combined_error[quant_level][integer_count - 3];
  783. if (integer_count_error < best_integer_count_error)
  784. {
  785. best_integer_count_error = integer_count_error;
  786. best_integer_count = integer_count;
  787. }
  788. }
  789. int ql = quant_mode_table[best_integer_count][bits_available];
  790. int ql_mod = quant_mode_table[best_integer_count][bits_available + 5];
  791. best_quant_level = static_cast<uint8_t>(ql);
  792. best_quant_level_mod = static_cast<uint8_t>(ql_mod);
  793. if (ql >= QUANT_6)
  794. {
  795. for (int i = 0; i < 3; i++)
  796. {
  797. best_formats[i] = best_combined_format[ql][best_integer_count - 3][i];
  798. }
  799. }
  800. else
  801. {
  802. for (int i = 0; i < 3; i++)
  803. {
  804. best_formats[i] = FMT_LUMINANCE;
  805. }
  806. }
  807. return best_integer_count_error;
  808. }
  809. /**
  810. * @brief For 4 partitions compute the best format combinations for every pair of quant mode and integer count.
  811. *
  812. * @param best_error The best error for a single endpoint quant level and integer count.
  813. * @param best_format The best format for a single endpoint quant level and integer count.
  814. * @param[out] best_combined_error The best combined error pairings for the 4 partitions.
  815. * @param[out] best_combined_format The best combined format pairings for the 4 partitions.
  816. */
  817. static void four_partitions_find_best_combination_for_every_quantization_and_integer_count(
  818. const float best_error[4][21][4], // indexed by (partition, quant-level, integer-count)
  819. const uint8_t best_format[4][21][4],
  820. float best_combined_error[21][13],
  821. uint8_t best_combined_format[21][13][4]
  822. ) {
  823. for (int i = QUANT_2; i <= QUANT_256; i++)
  824. {
  825. for (int j = 0; j < 13; j++)
  826. {
  827. best_combined_error[i][j] = ERROR_CALC_DEFAULT;
  828. }
  829. }
  830. for (int quant = QUANT_6; quant <= QUANT_256; quant++)
  831. {
  832. for (int i = 0; i < 4; i++) // integer-count for first endpoint-pair
  833. {
  834. for (int j = 0; j < 4; j++) // integer-count for second endpoint-pair
  835. {
  836. int low2 = astc::min(i, j);
  837. int high2 = astc::max(i, j);
  838. if ((high2 - low2) > 1)
  839. {
  840. continue;
  841. }
  842. for (int k = 0; k < 4; k++) // integer-count for third endpoint-pair
  843. {
  844. int low3 = astc::min(k, low2);
  845. int high3 = astc::max(k, high2);
  846. if ((high3 - low3) > 1)
  847. {
  848. continue;
  849. }
  850. for (int l = 0; l < 4; l++) // integer-count for fourth endpoint-pair
  851. {
  852. int low4 = astc::min(l, low3);
  853. int high4 = astc::max(l, high3);
  854. if ((high4 - low4) > 1)
  855. {
  856. continue;
  857. }
  858. int intcnt = i + j + k + l;
  859. float errorterm = astc::min(best_error[0][quant][i] + best_error[1][quant][j] + best_error[2][quant][k] + best_error[3][quant][l], 1e10f);
  860. if (errorterm <= best_combined_error[quant][intcnt])
  861. {
  862. best_combined_error[quant][intcnt] = errorterm;
  863. best_combined_format[quant][intcnt][0] = best_format[0][quant][i];
  864. best_combined_format[quant][intcnt][1] = best_format[1][quant][j];
  865. best_combined_format[quant][intcnt][2] = best_format[2][quant][k];
  866. best_combined_format[quant][intcnt][3] = best_format[3][quant][l];
  867. }
  868. }
  869. }
  870. }
  871. }
  872. }
  873. }
  874. /**
  875. * @brief For 4 partitions compute the best format and quantization for a given bit count.
  876. *
  877. * @param best_combined_error The best error for each quant level and integer count.
  878. * @param best_combined_format The best format for each quant level and integer count.
  879. * @param bits_available The number of bits available for encoding.
  880. * @param[out] best_quant_level The output best color quant level.
  881. * @param[out] best_quant_level_mod The output best color quant level assuming two more bits are available.
  882. * @param[out] best_formats The output best color formats.
  883. *
  884. * @return best_error The output error for the best pairing.
  885. */
  886. static float four_partitions_find_best_combination_for_bitcount(
  887. const float best_combined_error[21][13],
  888. const uint8_t best_combined_format[21][13][4],
  889. int bits_available,
  890. uint8_t& best_quant_level,
  891. uint8_t& best_quant_level_mod,
  892. uint8_t* best_formats
  893. ) {
  894. int best_integer_count = 0;
  895. float best_integer_count_error = ERROR_CALC_DEFAULT;
  896. for (int integer_count = 4; integer_count <= 9; integer_count++)
  897. {
  898. // Compute the quantization level for a given number of integers and a given number of bits
  899. int quant_level = quant_mode_table[integer_count][bits_available];
  900. // Don't have enough bits to represent a given endpoint format at all!
  901. if (quant_level < QUANT_6)
  902. {
  903. break;
  904. }
  905. float integer_count_error = best_combined_error[quant_level][integer_count - 4];
  906. if (integer_count_error < best_integer_count_error)
  907. {
  908. best_integer_count_error = integer_count_error;
  909. best_integer_count = integer_count;
  910. }
  911. }
  912. int ql = quant_mode_table[best_integer_count][bits_available];
  913. int ql_mod = quant_mode_table[best_integer_count][bits_available + 8];
  914. best_quant_level = static_cast<uint8_t>(ql);
  915. best_quant_level_mod = static_cast<uint8_t>(ql_mod);
  916. if (ql >= QUANT_6)
  917. {
  918. for (int i = 0; i < 4; i++)
  919. {
  920. best_formats[i] = best_combined_format[ql][best_integer_count - 4][i];
  921. }
  922. }
  923. else
  924. {
  925. for (int i = 0; i < 4; i++)
  926. {
  927. best_formats[i] = FMT_LUMINANCE;
  928. }
  929. }
  930. return best_integer_count_error;
  931. }
  932. /* See header for documentation. */
  933. unsigned int compute_ideal_endpoint_formats(
  934. const partition_info& pi,
  935. const image_block& blk,
  936. const endpoints& ep,
  937. // bitcounts and errors computed for the various quantization methods
  938. const int8_t* qwt_bitcounts,
  939. const float* qwt_errors,
  940. unsigned int tune_candidate_limit,
  941. unsigned int start_block_mode,
  942. unsigned int end_block_mode,
  943. // output data
  944. uint8_t partition_format_specifiers[TUNE_MAX_TRIAL_CANDIDATES][BLOCK_MAX_PARTITIONS],
  945. int block_mode[TUNE_MAX_TRIAL_CANDIDATES],
  946. quant_method quant_level[TUNE_MAX_TRIAL_CANDIDATES],
  947. quant_method quant_level_mod[TUNE_MAX_TRIAL_CANDIDATES],
  948. compression_working_buffers& tmpbuf
  949. ) {
  950. int partition_count = pi.partition_count;
  951. promise(partition_count > 0);
  952. bool encode_hdr_rgb = static_cast<bool>(blk.rgb_lns[0]);
  953. bool encode_hdr_alpha = static_cast<bool>(blk.alpha_lns[0]);
  954. // Compute the errors that result from various encoding choices (such as using luminance instead
  955. // of RGB, discarding Alpha, using RGB-scale in place of two separate RGB endpoints and so on)
  956. encoding_choice_errors eci[BLOCK_MAX_PARTITIONS];
  957. compute_encoding_choice_errors(blk, pi, ep, eci);
  958. float best_error[BLOCK_MAX_PARTITIONS][21][4];
  959. uint8_t format_of_choice[BLOCK_MAX_PARTITIONS][21][4];
  960. for (int i = 0; i < partition_count; i++)
  961. {
  962. compute_color_error_for_every_integer_count_and_quant_level(
  963. encode_hdr_rgb, encode_hdr_alpha, i,
  964. pi, eci[i], ep, blk.channel_weight, best_error[i],
  965. format_of_choice[i]);
  966. }
  967. float* errors_of_best_combination = tmpbuf.errors_of_best_combination;
  968. uint8_t* best_quant_levels = tmpbuf.best_quant_levels;
  969. uint8_t* best_quant_levels_mod = tmpbuf.best_quant_levels_mod;
  970. uint8_t (&best_ep_formats)[WEIGHTS_MAX_BLOCK_MODES][BLOCK_MAX_PARTITIONS] = tmpbuf.best_ep_formats;
  971. // Ensure that the first iteration understep contains data that will never be picked
  972. vfloat clear_error(ERROR_CALC_DEFAULT);
  973. vint clear_quant(0);
  974. size_t packed_start_block_mode = round_down_to_simd_multiple_vla(start_block_mode);
  975. storea(clear_error, errors_of_best_combination + packed_start_block_mode);
  976. store_nbytes(clear_quant, best_quant_levels + packed_start_block_mode);
  977. store_nbytes(clear_quant, best_quant_levels_mod + packed_start_block_mode);
  978. // Ensure that last iteration overstep contains data that will never be picked
  979. size_t packed_end_block_mode = round_down_to_simd_multiple_vla(end_block_mode - 1);
  980. storea(clear_error, errors_of_best_combination + packed_end_block_mode);
  981. store_nbytes(clear_quant, best_quant_levels + packed_end_block_mode);
  982. store_nbytes(clear_quant, best_quant_levels_mod + packed_end_block_mode);
  983. // Track a scalar best to avoid expensive search at least once ...
  984. float error_of_best_combination = ERROR_CALC_DEFAULT;
  985. int index_of_best_combination = -1;
  986. // The block contains 1 partition
  987. if (partition_count == 1)
  988. {
  989. for (unsigned int i = start_block_mode; i < end_block_mode; i++)
  990. {
  991. if (qwt_errors[i] >= ERROR_CALC_DEFAULT)
  992. {
  993. errors_of_best_combination[i] = ERROR_CALC_DEFAULT;
  994. continue;
  995. }
  996. float error_of_best = one_partition_find_best_combination_for_bitcount(
  997. best_error[0], format_of_choice[0], qwt_bitcounts[i],
  998. best_quant_levels[i], best_ep_formats[i][0]);
  999. float total_error = error_of_best + qwt_errors[i];
  1000. errors_of_best_combination[i] = total_error;
  1001. best_quant_levels_mod[i] = best_quant_levels[i];
  1002. if (total_error < error_of_best_combination)
  1003. {
  1004. error_of_best_combination = total_error;
  1005. index_of_best_combination = i;
  1006. }
  1007. }
  1008. }
  1009. // The block contains 2 partitions
  1010. else if (partition_count == 2)
  1011. {
  1012. float combined_best_error[21][7];
  1013. uint8_t formats_of_choice[21][7][2];
  1014. two_partitions_find_best_combination_for_every_quantization_and_integer_count(
  1015. best_error, format_of_choice, combined_best_error, formats_of_choice);
  1016. assert(start_block_mode == 0);
  1017. for (unsigned int i = 0; i < end_block_mode; i++)
  1018. {
  1019. if (qwt_errors[i] >= ERROR_CALC_DEFAULT)
  1020. {
  1021. errors_of_best_combination[i] = ERROR_CALC_DEFAULT;
  1022. continue;
  1023. }
  1024. float error_of_best = two_partitions_find_best_combination_for_bitcount(
  1025. combined_best_error, formats_of_choice, qwt_bitcounts[i],
  1026. best_quant_levels[i], best_quant_levels_mod[i],
  1027. best_ep_formats[i]);
  1028. float total_error = error_of_best + qwt_errors[i];
  1029. errors_of_best_combination[i] = total_error;
  1030. if (total_error < error_of_best_combination)
  1031. {
  1032. error_of_best_combination = total_error;
  1033. index_of_best_combination = i;
  1034. }
  1035. }
  1036. }
  1037. // The block contains 3 partitions
  1038. else if (partition_count == 3)
  1039. {
  1040. float combined_best_error[21][10];
  1041. uint8_t formats_of_choice[21][10][3];
  1042. three_partitions_find_best_combination_for_every_quantization_and_integer_count(
  1043. best_error, format_of_choice, combined_best_error, formats_of_choice);
  1044. assert(start_block_mode == 0);
  1045. for (unsigned int i = 0; i < end_block_mode; i++)
  1046. {
  1047. if (qwt_errors[i] >= ERROR_CALC_DEFAULT)
  1048. {
  1049. errors_of_best_combination[i] = ERROR_CALC_DEFAULT;
  1050. continue;
  1051. }
  1052. float error_of_best = three_partitions_find_best_combination_for_bitcount(
  1053. combined_best_error, formats_of_choice, qwt_bitcounts[i],
  1054. best_quant_levels[i], best_quant_levels_mod[i],
  1055. best_ep_formats[i]);
  1056. float total_error = error_of_best + qwt_errors[i];
  1057. errors_of_best_combination[i] = total_error;
  1058. if (total_error < error_of_best_combination)
  1059. {
  1060. error_of_best_combination = total_error;
  1061. index_of_best_combination = i;
  1062. }
  1063. }
  1064. }
  1065. // The block contains 4 partitions
  1066. else // if (partition_count == 4)
  1067. {
  1068. assert(partition_count == 4);
  1069. float combined_best_error[21][13];
  1070. uint8_t formats_of_choice[21][13][4];
  1071. four_partitions_find_best_combination_for_every_quantization_and_integer_count(
  1072. best_error, format_of_choice, combined_best_error, formats_of_choice);
  1073. assert(start_block_mode == 0);
  1074. for (unsigned int i = 0; i < end_block_mode; i++)
  1075. {
  1076. if (qwt_errors[i] >= ERROR_CALC_DEFAULT)
  1077. {
  1078. errors_of_best_combination[i] = ERROR_CALC_DEFAULT;
  1079. continue;
  1080. }
  1081. float error_of_best = four_partitions_find_best_combination_for_bitcount(
  1082. combined_best_error, formats_of_choice, qwt_bitcounts[i],
  1083. best_quant_levels[i], best_quant_levels_mod[i],
  1084. best_ep_formats[i]);
  1085. float total_error = error_of_best + qwt_errors[i];
  1086. errors_of_best_combination[i] = total_error;
  1087. if (total_error < error_of_best_combination)
  1088. {
  1089. error_of_best_combination = total_error;
  1090. index_of_best_combination = i;
  1091. }
  1092. }
  1093. }
  1094. int best_error_weights[TUNE_MAX_TRIAL_CANDIDATES];
  1095. // Fast path the first result and avoid the list search for trial 0
  1096. best_error_weights[0] = index_of_best_combination;
  1097. if (index_of_best_combination >= 0)
  1098. {
  1099. errors_of_best_combination[index_of_best_combination] = ERROR_CALC_DEFAULT;
  1100. }
  1101. // Search the remaining results and pick the best candidate modes for trial 1+
  1102. for (unsigned int i = 1; i < tune_candidate_limit; i++)
  1103. {
  1104. vint vbest_error_index(-1);
  1105. vfloat vbest_ep_error(ERROR_CALC_DEFAULT);
  1106. // TODO: This should use size_t for the inputs of start/end_block_mode
  1107. // to avoid some of this type conversion, but that propagates and will
  1108. // need a bigger PR to fix
  1109. size_t start_mode = round_down_to_simd_multiple_vla(start_block_mode);
  1110. vint lane_ids = vint::lane_id() + vint_from_size(start_mode);
  1111. for (size_t j = start_mode; j < end_block_mode; j += ASTCENC_SIMD_WIDTH)
  1112. {
  1113. vfloat err = vfloat(errors_of_best_combination + j);
  1114. vmask mask = err < vbest_ep_error;
  1115. vbest_ep_error = select(vbest_ep_error, err, mask);
  1116. vbest_error_index = select(vbest_error_index, lane_ids, mask);
  1117. lane_ids += vint(ASTCENC_SIMD_WIDTH);
  1118. }
  1119. // Pick best mode from the SIMD result, using lowest matching index to ensure invariance
  1120. vmask lanes_min_error = vbest_ep_error == hmin(vbest_ep_error);
  1121. vbest_error_index = select(vint(0x7FFFFFFF), vbest_error_index, lanes_min_error);
  1122. int best_error_index = hmin_s(vbest_error_index);
  1123. best_error_weights[i] = best_error_index;
  1124. // Max the error for this candidate so we don't pick it again
  1125. if (best_error_index >= 0)
  1126. {
  1127. errors_of_best_combination[best_error_index] = ERROR_CALC_DEFAULT;
  1128. }
  1129. // Early-out if no more candidates are valid
  1130. else
  1131. {
  1132. break;
  1133. }
  1134. }
  1135. for (unsigned int i = 0; i < tune_candidate_limit; i++)
  1136. {
  1137. if (best_error_weights[i] < 0)
  1138. {
  1139. return i;
  1140. }
  1141. block_mode[i] = best_error_weights[i];
  1142. quant_level[i] = static_cast<quant_method>(best_quant_levels[best_error_weights[i]]);
  1143. quant_level_mod[i] = static_cast<quant_method>(best_quant_levels_mod[best_error_weights[i]]);
  1144. assert(quant_level[i] >= QUANT_6 && quant_level[i] <= QUANT_256);
  1145. assert(quant_level_mod[i] >= QUANT_6 && quant_level_mod[i] <= QUANT_256);
  1146. for (int j = 0; j < partition_count; j++)
  1147. {
  1148. partition_format_specifiers[i][j] = best_ep_formats[best_error_weights[i]][j];
  1149. }
  1150. }
  1151. return tune_candidate_limit;
  1152. }
  1153. #endif