astcenc_find_best_partitioning.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. // SPDX-License-Identifier: Apache-2.0
  2. // ----------------------------------------------------------------------------
  3. // Copyright 2011-2023 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 partition for a block.
  20. *
  21. * The partition search operates in two stages. The first pass uses kmeans clustering to group
  22. * texels into an ideal partitioning for the requested partition count, and then compares that
  23. * against the 1024 partitionings generated by the ASTC partition hash function. The generated
  24. * partitions are then ranked by the number of texels in the wrong partition, compared to the ideal
  25. * clustering. All 1024 partitions are tested for similarity and ranked, apart from duplicates and
  26. * partitionings that actually generate fewer than the requested partition count, but only the top
  27. * N candidates are actually put through a more detailed search. N is determined by the compressor
  28. * quality preset.
  29. *
  30. * For the detailed search, each candidate is checked against two possible encoding methods:
  31. *
  32. * - The best partitioning assuming different chroma colors (RGB + RGB or RGB + delta endpoints).
  33. * - The best partitioning assuming same chroma colors (RGB + scale endpoints).
  34. *
  35. * This is implemented by computing the compute mean color and dominant direction for each
  36. * partition. This defines two lines, both of which go through the mean color value.
  37. *
  38. * - One line has a direction defined by the dominant direction; this is used to assess the error
  39. * from using an uncorrelated color representation.
  40. * - The other line goes through (0,0,0,1) and is used to assess the error from using a same chroma
  41. * (RGB + scale) color representation.
  42. *
  43. * The best candidate is selected by computing the squared-errors that result from using these
  44. * lines for endpoint selection.
  45. */
  46. #include <limits>
  47. #include "astcenc_internal.h"
  48. /**
  49. * @brief Pick some initial kmeans cluster centers.
  50. *
  51. * @param blk The image block color data to compress.
  52. * @param texel_count The number of texels in the block.
  53. * @param partition_count The number of partitions in the block.
  54. * @param[out] cluster_centers The initial partition cluster center colors.
  55. */
  56. static void kmeans_init(
  57. const image_block& blk,
  58. unsigned int texel_count,
  59. unsigned int partition_count,
  60. vfloat4 cluster_centers[BLOCK_MAX_PARTITIONS]
  61. ) {
  62. promise(texel_count > 0);
  63. promise(partition_count > 0);
  64. unsigned int clusters_selected = 0;
  65. float distances[BLOCK_MAX_TEXELS];
  66. // Pick a random sample as first cluster center; 145897 from random.org
  67. unsigned int sample = 145897 % texel_count;
  68. vfloat4 center_color = blk.texel(sample);
  69. cluster_centers[clusters_selected] = center_color;
  70. clusters_selected++;
  71. // Compute the distance to the first cluster center
  72. float distance_sum = 0.0f;
  73. for (unsigned int i = 0; i < texel_count; i++)
  74. {
  75. vfloat4 color = blk.texel(i);
  76. vfloat4 diff = color - center_color;
  77. float distance = dot_s(diff * diff, blk.channel_weight);
  78. distance_sum += distance;
  79. distances[i] = distance;
  80. }
  81. // More numbers from random.org for weighted-random center selection
  82. const float cluster_cutoffs[9] {
  83. 0.626220f, 0.932770f, 0.275454f,
  84. 0.318558f, 0.240113f, 0.009190f,
  85. 0.347661f, 0.731960f, 0.156391f
  86. };
  87. unsigned int cutoff = (clusters_selected - 1) + 3 * (partition_count - 2);
  88. // Pick the remaining samples as needed
  89. while (true)
  90. {
  91. // Pick the next center in a weighted-random fashion.
  92. float summa = 0.0f;
  93. float distance_cutoff = distance_sum * cluster_cutoffs[cutoff++];
  94. for (sample = 0; sample < texel_count; sample++)
  95. {
  96. summa += distances[sample];
  97. if (summa >= distance_cutoff)
  98. {
  99. break;
  100. }
  101. }
  102. // Clamp to a valid range and store the selected cluster center
  103. sample = astc::min(sample, texel_count - 1);
  104. center_color = blk.texel(sample);
  105. cluster_centers[clusters_selected++] = center_color;
  106. if (clusters_selected >= partition_count)
  107. {
  108. break;
  109. }
  110. // Compute the distance to the new cluster center, keep the min dist
  111. distance_sum = 0.0f;
  112. for (unsigned int i = 0; i < texel_count; i++)
  113. {
  114. vfloat4 color = blk.texel(i);
  115. vfloat4 diff = color - center_color;
  116. float distance = dot_s(diff * diff, blk.channel_weight);
  117. distance = astc::min(distance, distances[i]);
  118. distance_sum += distance;
  119. distances[i] = distance;
  120. }
  121. }
  122. }
  123. /**
  124. * @brief Assign texels to clusters, based on a set of chosen center points.
  125. *
  126. * @param blk The image block color data to compress.
  127. * @param texel_count The number of texels in the block.
  128. * @param partition_count The number of partitions in the block.
  129. * @param cluster_centers The partition cluster center colors.
  130. * @param[out] partition_of_texel The partition assigned for each texel.
  131. */
  132. static void kmeans_assign(
  133. const image_block& blk,
  134. unsigned int texel_count,
  135. unsigned int partition_count,
  136. const vfloat4 cluster_centers[BLOCK_MAX_PARTITIONS],
  137. uint8_t partition_of_texel[BLOCK_MAX_TEXELS]
  138. ) {
  139. promise(texel_count > 0);
  140. promise(partition_count > 0);
  141. uint8_t partition_texel_count[BLOCK_MAX_PARTITIONS] { 0 };
  142. // Find the best partition for every texel
  143. for (unsigned int i = 0; i < texel_count; i++)
  144. {
  145. float best_distance = std::numeric_limits<float>::max();
  146. unsigned int best_partition = 0;
  147. vfloat4 color = blk.texel(i);
  148. for (unsigned int j = 0; j < partition_count; j++)
  149. {
  150. vfloat4 diff = color - cluster_centers[j];
  151. float distance = dot_s(diff * diff, blk.channel_weight);
  152. if (distance < best_distance)
  153. {
  154. best_distance = distance;
  155. best_partition = j;
  156. }
  157. }
  158. partition_of_texel[i] = static_cast<uint8_t>(best_partition);
  159. partition_texel_count[best_partition]++;
  160. }
  161. // It is possible to get a situation where a partition ends up without any texels. In this case,
  162. // assign texel N to partition N. This is silly, but ensures that every partition retains at
  163. // least one texel. Reassigning a texel in this manner may cause another partition to go empty,
  164. // so if we actually did a reassignment, run the whole loop over again.
  165. bool problem_case;
  166. do
  167. {
  168. problem_case = false;
  169. for (unsigned int i = 0; i < partition_count; i++)
  170. {
  171. if (partition_texel_count[i] == 0)
  172. {
  173. partition_texel_count[partition_of_texel[i]]--;
  174. partition_texel_count[i]++;
  175. partition_of_texel[i] = static_cast<uint8_t>(i);
  176. problem_case = true;
  177. }
  178. }
  179. } while (problem_case);
  180. }
  181. /**
  182. * @brief Compute new cluster centers based on their center of gravity.
  183. *
  184. * @param blk The image block color data to compress.
  185. * @param texel_count The number of texels in the block.
  186. * @param partition_count The number of partitions in the block.
  187. * @param[out] cluster_centers The new cluster center colors.
  188. * @param partition_of_texel The partition assigned for each texel.
  189. */
  190. static void kmeans_update(
  191. const image_block& blk,
  192. unsigned int texel_count,
  193. unsigned int partition_count,
  194. vfloat4 cluster_centers[BLOCK_MAX_PARTITIONS],
  195. const uint8_t partition_of_texel[BLOCK_MAX_TEXELS]
  196. ) {
  197. promise(texel_count > 0);
  198. promise(partition_count > 0);
  199. vfloat4 color_sum[BLOCK_MAX_PARTITIONS] {
  200. vfloat4::zero(),
  201. vfloat4::zero(),
  202. vfloat4::zero(),
  203. vfloat4::zero()
  204. };
  205. uint8_t partition_texel_count[BLOCK_MAX_PARTITIONS] { 0 };
  206. // Find the center-of-gravity in each cluster
  207. for (unsigned int i = 0; i < texel_count; i++)
  208. {
  209. uint8_t partition = partition_of_texel[i];
  210. color_sum[partition] += blk.texel(i);
  211. partition_texel_count[partition]++;
  212. }
  213. // Set the center of gravity to be the new cluster center
  214. for (unsigned int i = 0; i < partition_count; i++)
  215. {
  216. float scale = 1.0f / static_cast<float>(partition_texel_count[i]);
  217. cluster_centers[i] = color_sum[i] * scale;
  218. }
  219. }
  220. /**
  221. * @brief Compute bit-mismatch for partitioning in 2-partition mode.
  222. *
  223. * @param a The texel assignment bitvector for the block.
  224. * @param b The texel assignment bitvector for the partition table.
  225. *
  226. * @return The number of bit mismatches.
  227. */
  228. static inline unsigned int partition_mismatch2(
  229. const uint64_t a[2],
  230. const uint64_t b[2]
  231. ) {
  232. int v1 = popcount(a[0] ^ b[0]) + popcount(a[1] ^ b[1]);
  233. int v2 = popcount(a[0] ^ b[1]) + popcount(a[1] ^ b[0]);
  234. return astc::min(v1, v2);
  235. }
  236. /**
  237. * @brief Compute bit-mismatch for partitioning in 3-partition mode.
  238. *
  239. * @param a The texel assignment bitvector for the block.
  240. * @param b The texel assignment bitvector for the partition table.
  241. *
  242. * @return The number of bit mismatches.
  243. */
  244. static inline unsigned int partition_mismatch3(
  245. const uint64_t a[3],
  246. const uint64_t b[3]
  247. ) {
  248. int p00 = popcount(a[0] ^ b[0]);
  249. int p01 = popcount(a[0] ^ b[1]);
  250. int p02 = popcount(a[0] ^ b[2]);
  251. int p10 = popcount(a[1] ^ b[0]);
  252. int p11 = popcount(a[1] ^ b[1]);
  253. int p12 = popcount(a[1] ^ b[2]);
  254. int p20 = popcount(a[2] ^ b[0]);
  255. int p21 = popcount(a[2] ^ b[1]);
  256. int p22 = popcount(a[2] ^ b[2]);
  257. int s0 = p11 + p22;
  258. int s1 = p12 + p21;
  259. int v0 = astc::min(s0, s1) + p00;
  260. int s2 = p10 + p22;
  261. int s3 = p12 + p20;
  262. int v1 = astc::min(s2, s3) + p01;
  263. int s4 = p10 + p21;
  264. int s5 = p11 + p20;
  265. int v2 = astc::min(s4, s5) + p02;
  266. return astc::min(v0, v1, v2);
  267. }
  268. /**
  269. * @brief Compute bit-mismatch for partitioning in 4-partition mode.
  270. *
  271. * @param a The texel assignment bitvector for the block.
  272. * @param b The texel assignment bitvector for the partition table.
  273. *
  274. * @return The number of bit mismatches.
  275. */
  276. static inline unsigned int partition_mismatch4(
  277. const uint64_t a[4],
  278. const uint64_t b[4]
  279. ) {
  280. int p00 = popcount(a[0] ^ b[0]);
  281. int p01 = popcount(a[0] ^ b[1]);
  282. int p02 = popcount(a[0] ^ b[2]);
  283. int p03 = popcount(a[0] ^ b[3]);
  284. int p10 = popcount(a[1] ^ b[0]);
  285. int p11 = popcount(a[1] ^ b[1]);
  286. int p12 = popcount(a[1] ^ b[2]);
  287. int p13 = popcount(a[1] ^ b[3]);
  288. int p20 = popcount(a[2] ^ b[0]);
  289. int p21 = popcount(a[2] ^ b[1]);
  290. int p22 = popcount(a[2] ^ b[2]);
  291. int p23 = popcount(a[2] ^ b[3]);
  292. int p30 = popcount(a[3] ^ b[0]);
  293. int p31 = popcount(a[3] ^ b[1]);
  294. int p32 = popcount(a[3] ^ b[2]);
  295. int p33 = popcount(a[3] ^ b[3]);
  296. int mx23 = astc::min(p22 + p33, p23 + p32);
  297. int mx13 = astc::min(p21 + p33, p23 + p31);
  298. int mx12 = astc::min(p21 + p32, p22 + p31);
  299. int mx03 = astc::min(p20 + p33, p23 + p30);
  300. int mx02 = astc::min(p20 + p32, p22 + p30);
  301. int mx01 = astc::min(p21 + p30, p20 + p31);
  302. int v0 = p00 + astc::min(p11 + mx23, p12 + mx13, p13 + mx12);
  303. int v1 = p01 + astc::min(p10 + mx23, p12 + mx03, p13 + mx02);
  304. int v2 = p02 + astc::min(p11 + mx03, p10 + mx13, p13 + mx01);
  305. int v3 = p03 + astc::min(p11 + mx02, p12 + mx01, p10 + mx12);
  306. return astc::min(v0, v1, v2, v3);
  307. }
  308. using mismatch_dispatch = unsigned int (*)(const uint64_t*, const uint64_t*);
  309. /**
  310. * @brief Count the partition table mismatches vs the data clustering.
  311. *
  312. * @param bsd The block size information.
  313. * @param partition_count The number of partitions in the block.
  314. * @param bitmaps The block texel partition assignment patterns.
  315. * @param[out] mismatch_counts The array storing per partitioning mismatch counts.
  316. */
  317. static void count_partition_mismatch_bits(
  318. const block_size_descriptor& bsd,
  319. unsigned int partition_count,
  320. const uint64_t bitmaps[BLOCK_MAX_PARTITIONS],
  321. unsigned int mismatch_counts[BLOCK_MAX_PARTITIONINGS]
  322. ) {
  323. unsigned int active_count = bsd.partitioning_count_selected[partition_count - 1];
  324. promise(active_count > 0);
  325. if (partition_count == 2)
  326. {
  327. for (unsigned int i = 0; i < active_count; i++)
  328. {
  329. mismatch_counts[i] = partition_mismatch2(bitmaps, bsd.coverage_bitmaps_2[i]);
  330. }
  331. }
  332. else if (partition_count == 3)
  333. {
  334. for (unsigned int i = 0; i < active_count; i++)
  335. {
  336. mismatch_counts[i] = partition_mismatch3(bitmaps, bsd.coverage_bitmaps_3[i]);
  337. }
  338. }
  339. else
  340. {
  341. for (unsigned int i = 0; i < active_count; i++)
  342. {
  343. mismatch_counts[i] = partition_mismatch4(bitmaps, bsd.coverage_bitmaps_4[i]);
  344. }
  345. }
  346. }
  347. /**
  348. * @brief Use counting sort on the mismatch array to sort partition candidates.
  349. *
  350. * @param partitioning_count The number of packed partitionings.
  351. * @param mismatch_count Partitioning mismatch counts, in index order.
  352. * @param[out] partition_ordering Partition index values, in mismatch order.
  353. *
  354. * @return The number of active partitions in this selection.
  355. */
  356. static unsigned int get_partition_ordering_by_mismatch_bits(
  357. unsigned int partitioning_count,
  358. const unsigned int mismatch_count[BLOCK_MAX_PARTITIONINGS],
  359. unsigned int partition_ordering[BLOCK_MAX_PARTITIONINGS]
  360. ) {
  361. promise(partitioning_count > 0);
  362. unsigned int mscount[256] { 0 };
  363. // Create the histogram of mismatch counts
  364. for (unsigned int i = 0; i < partitioning_count; i++)
  365. {
  366. mscount[mismatch_count[i]]++;
  367. }
  368. unsigned int active_count = partitioning_count - mscount[255];
  369. // Create a running sum from the histogram array
  370. // Cells store previous values only; i.e. exclude self after sum
  371. unsigned int summa = 0;
  372. for (unsigned int i = 0; i < 256; i++)
  373. {
  374. unsigned int cnt = mscount[i];
  375. mscount[i] = summa;
  376. summa += cnt;
  377. }
  378. // Use the running sum as the index, incrementing after read to allow
  379. // sequential entries with the same count
  380. for (unsigned int i = 0; i < partitioning_count; i++)
  381. {
  382. unsigned int idx = mscount[mismatch_count[i]]++;
  383. partition_ordering[idx] = i;
  384. }
  385. return active_count;
  386. }
  387. /**
  388. * @brief Use k-means clustering to compute a partition ordering for a block..
  389. *
  390. * @param bsd The block size information.
  391. * @param blk The image block color data to compress.
  392. * @param partition_count The desired number of partitions in the block.
  393. * @param[out] partition_ordering The list of recommended partition indices, in priority order.
  394. *
  395. * @return The number of active partitionings in this selection.
  396. */
  397. static unsigned int compute_kmeans_partition_ordering(
  398. const block_size_descriptor& bsd,
  399. const image_block& blk,
  400. unsigned int partition_count,
  401. unsigned int partition_ordering[BLOCK_MAX_PARTITIONINGS]
  402. ) {
  403. vfloat4 cluster_centers[BLOCK_MAX_PARTITIONS];
  404. uint8_t texel_partitions[BLOCK_MAX_TEXELS];
  405. // Use three passes of k-means clustering to partition the block data
  406. for (unsigned int i = 0; i < 3; i++)
  407. {
  408. if (i == 0)
  409. {
  410. kmeans_init(blk, bsd.texel_count, partition_count, cluster_centers);
  411. }
  412. else
  413. {
  414. kmeans_update(blk, bsd.texel_count, partition_count, cluster_centers, texel_partitions);
  415. }
  416. kmeans_assign(blk, bsd.texel_count, partition_count, cluster_centers, texel_partitions);
  417. }
  418. // Construct the block bitmaps of texel assignments to each partition
  419. uint64_t bitmaps[BLOCK_MAX_PARTITIONS] { 0 };
  420. unsigned int texels_to_process = astc::min(bsd.texel_count, BLOCK_MAX_KMEANS_TEXELS);
  421. promise(texels_to_process > 0);
  422. for (unsigned int i = 0; i < texels_to_process; i++)
  423. {
  424. unsigned int idx = bsd.kmeans_texels[i];
  425. bitmaps[texel_partitions[idx]] |= 1ULL << i;
  426. }
  427. // Count the mismatch between the block and the format's partition tables
  428. unsigned int mismatch_counts[BLOCK_MAX_PARTITIONINGS];
  429. count_partition_mismatch_bits(bsd, partition_count, bitmaps, mismatch_counts);
  430. // Sort the partitions based on the number of mismatched bits
  431. return get_partition_ordering_by_mismatch_bits(
  432. bsd.partitioning_count_selected[partition_count - 1],
  433. mismatch_counts, partition_ordering);
  434. }
  435. /**
  436. * @brief Insert a partitioning into an order list of results, sorted by error.
  437. *
  438. * @param max_values The max number of entries in the best result arrays.
  439. * @param this_error The error of the new entry.
  440. * @param this_partition The partition ID of the new entry.
  441. * @param[out] best_errors The array of best error values.
  442. * @param[out] best_partitions The array of best partition values.
  443. */
  444. static void insert_result(
  445. unsigned int max_values,
  446. float this_error,
  447. unsigned int this_partition,
  448. float* best_errors,
  449. unsigned int* best_partitions)
  450. {
  451. promise(max_values > 0);
  452. // Don't bother searching if the current worst error beats the new error
  453. if (this_error >= best_errors[max_values - 1])
  454. {
  455. return;
  456. }
  457. // Else insert into the list in error-order
  458. for (unsigned int i = 0; i < max_values; i++)
  459. {
  460. // Existing result is better - move on ...
  461. if (this_error > best_errors[i])
  462. {
  463. continue;
  464. }
  465. // Move existing results down one
  466. for (unsigned int j = max_values - 1; j > i; j--)
  467. {
  468. best_errors[j] = best_errors[j - 1];
  469. best_partitions[j] = best_partitions[j - 1];
  470. }
  471. // Insert new result
  472. best_errors[i] = this_error;
  473. best_partitions[i] = this_partition;
  474. break;
  475. }
  476. }
  477. /* See header for documentation. */
  478. unsigned int find_best_partition_candidates(
  479. const block_size_descriptor& bsd,
  480. const image_block& blk,
  481. unsigned int partition_count,
  482. unsigned int partition_search_limit,
  483. unsigned int best_partitions[TUNE_MAX_PARTITIONING_CANDIDATES],
  484. unsigned int requested_candidates
  485. ) {
  486. // Constant used to estimate quantization error for a given partitioning; the optimal value for
  487. // this depends on bitrate. These values have been determined empirically.
  488. unsigned int texels_per_block = bsd.texel_count;
  489. float weight_imprecision_estim = 0.055f;
  490. if (texels_per_block <= 20)
  491. {
  492. weight_imprecision_estim = 0.03f;
  493. }
  494. else if (texels_per_block <= 31)
  495. {
  496. weight_imprecision_estim = 0.04f;
  497. }
  498. else if (texels_per_block <= 41)
  499. {
  500. weight_imprecision_estim = 0.05f;
  501. }
  502. promise(partition_count > 0);
  503. promise(partition_search_limit > 0);
  504. weight_imprecision_estim = weight_imprecision_estim * weight_imprecision_estim;
  505. unsigned int partition_sequence[BLOCK_MAX_PARTITIONINGS];
  506. unsigned int sequence_len = compute_kmeans_partition_ordering(bsd, blk, partition_count, partition_sequence);
  507. partition_search_limit = astc::min(partition_search_limit, sequence_len);
  508. requested_candidates = astc::min(partition_search_limit, requested_candidates);
  509. bool uses_alpha = !blk.is_constant_channel(3);
  510. // Partitioning errors assuming uncorrelated-chrominance endpoints
  511. float uncor_best_errors[TUNE_MAX_PARTITIONING_CANDIDATES];
  512. unsigned int uncor_best_partitions[TUNE_MAX_PARTITIONING_CANDIDATES];
  513. // Partitioning errors assuming same-chrominance endpoints
  514. float samec_best_errors[TUNE_MAX_PARTITIONING_CANDIDATES];
  515. unsigned int samec_best_partitions[TUNE_MAX_PARTITIONING_CANDIDATES];
  516. for (unsigned int i = 0; i < requested_candidates; i++)
  517. {
  518. uncor_best_errors[i] = ERROR_CALC_DEFAULT;
  519. samec_best_errors[i] = ERROR_CALC_DEFAULT;
  520. }
  521. if (uses_alpha)
  522. {
  523. for (unsigned int i = 0; i < partition_search_limit; i++)
  524. {
  525. unsigned int partition = partition_sequence[i];
  526. const auto& pi = bsd.get_raw_partition_info(partition_count, partition);
  527. // Compute weighting to give to each component in each partition
  528. partition_metrics pms[BLOCK_MAX_PARTITIONS];
  529. compute_avgs_and_dirs_4_comp(pi, blk, pms);
  530. line4 uncor_lines[BLOCK_MAX_PARTITIONS];
  531. line4 samec_lines[BLOCK_MAX_PARTITIONS];
  532. processed_line4 uncor_plines[BLOCK_MAX_PARTITIONS];
  533. processed_line4 samec_plines[BLOCK_MAX_PARTITIONS];
  534. float line_lengths[BLOCK_MAX_PARTITIONS];
  535. for (unsigned int j = 0; j < partition_count; j++)
  536. {
  537. partition_metrics& pm = pms[j];
  538. uncor_lines[j].a = pm.avg;
  539. uncor_lines[j].b = normalize_safe(pm.dir, unit4());
  540. uncor_plines[j].amod = uncor_lines[j].a - uncor_lines[j].b * dot(uncor_lines[j].a, uncor_lines[j].b);
  541. uncor_plines[j].bs = uncor_lines[j].b;
  542. samec_lines[j].a = vfloat4::zero();
  543. samec_lines[j].b = normalize_safe(pm.avg, unit4());
  544. samec_plines[j].amod = vfloat4::zero();
  545. samec_plines[j].bs = samec_lines[j].b;
  546. }
  547. float uncor_error = 0.0f;
  548. float samec_error = 0.0f;
  549. compute_error_squared_rgba(pi,
  550. blk,
  551. uncor_plines,
  552. samec_plines,
  553. line_lengths,
  554. uncor_error,
  555. samec_error);
  556. // Compute an estimate of error introduced by weight quantization imprecision.
  557. // This error is computed as follows, for each partition
  558. // 1: compute the principal-axis vector (full length) in error-space
  559. // 2: convert the principal-axis vector to regular RGB-space
  560. // 3: scale the vector by a constant that estimates average quantization error
  561. // 4: for each texel, square the vector, then do a dot-product with the texel's
  562. // error weight; sum up the results across all texels.
  563. // 4(optimized): square the vector once, then do a dot-product with the average
  564. // texel error, then multiply by the number of texels.
  565. for (unsigned int j = 0; j < partition_count; j++)
  566. {
  567. float tpp = static_cast<float>(pi.partition_texel_count[j]);
  568. vfloat4 error_weights(tpp * weight_imprecision_estim);
  569. vfloat4 uncor_vector = uncor_lines[j].b * line_lengths[j];
  570. vfloat4 samec_vector = samec_lines[j].b * line_lengths[j];
  571. uncor_error += dot_s(uncor_vector * uncor_vector, error_weights);
  572. samec_error += dot_s(samec_vector * samec_vector, error_weights);
  573. }
  574. insert_result(requested_candidates, uncor_error, partition, uncor_best_errors, uncor_best_partitions);
  575. insert_result(requested_candidates, samec_error, partition, samec_best_errors, samec_best_partitions);
  576. }
  577. }
  578. else
  579. {
  580. for (unsigned int i = 0; i < partition_search_limit; i++)
  581. {
  582. unsigned int partition = partition_sequence[i];
  583. const auto& pi = bsd.get_raw_partition_info(partition_count, partition);
  584. // Compute weighting to give to each component in each partition
  585. partition_metrics pms[BLOCK_MAX_PARTITIONS];
  586. compute_avgs_and_dirs_3_comp_rgb(pi, blk, pms);
  587. partition_lines3 plines[BLOCK_MAX_PARTITIONS];
  588. for (unsigned int j = 0; j < partition_count; j++)
  589. {
  590. partition_metrics& pm = pms[j];
  591. partition_lines3& pl = plines[j];
  592. pl.uncor_line.a = pm.avg;
  593. pl.uncor_line.b = normalize_safe(pm.dir, unit3());
  594. pl.samec_line.a = vfloat4::zero();
  595. pl.samec_line.b = normalize_safe(pm.avg, unit3());
  596. pl.uncor_pline.amod = pl.uncor_line.a - pl.uncor_line.b * dot3(pl.uncor_line.a, pl.uncor_line.b);
  597. pl.uncor_pline.bs = pl.uncor_line.b;
  598. pl.samec_pline.amod = vfloat4::zero();
  599. pl.samec_pline.bs = pl.samec_line.b;
  600. }
  601. float uncor_error = 0.0f;
  602. float samec_error = 0.0f;
  603. compute_error_squared_rgb(pi,
  604. blk,
  605. plines,
  606. uncor_error,
  607. samec_error);
  608. // Compute an estimate of error introduced by weight quantization imprecision.
  609. // This error is computed as follows, for each partition
  610. // 1: compute the principal-axis vector (full length) in error-space
  611. // 2: convert the principal-axis vector to regular RGB-space
  612. // 3: scale the vector by a constant that estimates average quantization error
  613. // 4: for each texel, square the vector, then do a dot-product with the texel's
  614. // error weight; sum up the results across all texels.
  615. // 4(optimized): square the vector once, then do a dot-product with the average
  616. // texel error, then multiply by the number of texels.
  617. for (unsigned int j = 0; j < partition_count; j++)
  618. {
  619. partition_lines3& pl = plines[j];
  620. float tpp = static_cast<float>(pi.partition_texel_count[j]);
  621. vfloat4 error_weights(tpp * weight_imprecision_estim);
  622. vfloat4 uncor_vector = pl.uncor_line.b * pl.line_length;
  623. vfloat4 samec_vector = pl.samec_line.b * pl.line_length;
  624. uncor_error += dot3_s(uncor_vector * uncor_vector, error_weights);
  625. samec_error += dot3_s(samec_vector * samec_vector, error_weights);
  626. }
  627. insert_result(requested_candidates, uncor_error, partition, uncor_best_errors, uncor_best_partitions);
  628. insert_result(requested_candidates, samec_error, partition, samec_best_errors, samec_best_partitions);
  629. }
  630. }
  631. unsigned int interleave[2 * TUNE_MAX_PARTITIONING_CANDIDATES];
  632. for (unsigned int i = 0; i < requested_candidates; i++)
  633. {
  634. interleave[2 * i] = bsd.get_raw_partition_info(partition_count, uncor_best_partitions[i]).partition_index;
  635. interleave[2 * i + 1] = bsd.get_raw_partition_info(partition_count, samec_best_partitions[i]).partition_index;
  636. }
  637. uint64_t bitmasks[1024/64] { 0 };
  638. unsigned int emitted = 0;
  639. // Deduplicate the first "requested" entries
  640. for (unsigned int i = 0; i < requested_candidates * 2; i++)
  641. {
  642. unsigned int partition = interleave[i];
  643. unsigned int word = partition / 64;
  644. unsigned int bit = partition % 64;
  645. bool written = bitmasks[word] & (1ull << bit);
  646. if (!written)
  647. {
  648. best_partitions[emitted] = partition;
  649. bitmasks[word] |= 1ull << bit;
  650. emitted++;
  651. if (emitted == requested_candidates)
  652. {
  653. break;
  654. }
  655. }
  656. }
  657. return emitted;
  658. }
  659. #endif