astc_pick_best_endpoint_format.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  1. /*----------------------------------------------------------------------------*/
  2. /**
  3. * This confidential and proprietary software may be used only as
  4. * authorised by a licensing agreement from ARM Limited
  5. * (C) COPYRIGHT 2011-2012 ARM Limited
  6. * ALL RIGHTS RESERVED
  7. *
  8. * The entire notice above must be reproduced on all authorised
  9. * copies and copies may only be made to the extent permitted
  10. * by a licensing agreement from ARM Limited.
  11. *
  12. * @brief Functions to pick the best ASTC endpoint format for a given block.
  13. */
  14. /*----------------------------------------------------------------------------*/
  15. #include "astc_codec_internals.h"
  16. #ifdef DEBUG_PRINT_DIAGNOSTICS
  17. #include <stdio.h>
  18. #endif
  19. #include <math.h>
  20. /*
  21. functions to determine, for a given partitioning, which color endpoint formats are the best to use.
  22. */
  23. // for a given partition, compute for every (integer-component-count, quantization-level)
  24. // the color error.
  25. static void compute_color_error_for_every_integer_count_and_quantization_level(int encode_hdr_rgb, // 1 = perform HDR encoding, 0 = perform LDR encoding.
  26. int encode_hdr_alpha, int partition_index, const partition_info * pi,
  27. const encoding_choice_errors * eci, // pointer to the structure for the CURRENT partition.
  28. const endpoints * ep, float4 error_weightings[4],
  29. // arrays to return results back through.
  30. float best_error[21][4], int format_of_choice[21][4])
  31. {
  32. int i, j;
  33. int partition_size = pi->texels_per_partition[partition_index];
  34. static const float baseline_quant_error[21] = {
  35. (65536.0f * 65536.0f / 18.0f), // 2 values, 1 step
  36. (65536.0f * 65536.0f / 18.0f) / (2 * 2), // 3 values, 2 steps
  37. (65536.0f * 65536.0f / 18.0f) / (3 * 3), // 4 values, 3 steps
  38. (65536.0f * 65536.0f / 18.0f) / (4 * 4), // 5 values
  39. (65536.0f * 65536.0f / 18.0f) / (5 * 5),
  40. (65536.0f * 65536.0f / 18.0f) / (7 * 7),
  41. (65536.0f * 65536.0f / 18.0f) / (9 * 9),
  42. (65536.0f * 65536.0f / 18.0f) / (11 * 11),
  43. (65536.0f * 65536.0f / 18.0f) / (15 * 15),
  44. (65536.0f * 65536.0f / 18.0f) / (19 * 19),
  45. (65536.0f * 65536.0f / 18.0f) / (23 * 23),
  46. (65536.0f * 65536.0f / 18.0f) / (31 * 31),
  47. (65536.0f * 65536.0f / 18.0f) / (39 * 39),
  48. (65536.0f * 65536.0f / 18.0f) / (47 * 47),
  49. (65536.0f * 65536.0f / 18.0f) / (63 * 63),
  50. (65536.0f * 65536.0f / 18.0f) / (79 * 79),
  51. (65536.0f * 65536.0f / 18.0f) / (95 * 95),
  52. (65536.0f * 65536.0f / 18.0f) / (127 * 127),
  53. (65536.0f * 65536.0f / 18.0f) / (159 * 159),
  54. (65536.0f * 65536.0f / 18.0f) / (191 * 191),
  55. (65536.0f * 65536.0f / 18.0f) / (255 * 255)
  56. };
  57. float4 ep0 = ep->endpt0[partition_index];
  58. float4 ep1 = ep->endpt1[partition_index];
  59. float ep0_max = MAX(MAX(ep0.x, ep0.y), ep0.z);
  60. float ep0_min = MIN(MIN(ep0.x, ep0.y), ep0.z);
  61. float ep1_max = MAX(MAX(ep1.x, ep1.y), ep1.z);
  62. float ep1_min = MIN(MIN(ep1.x, ep1.y), ep1.z);
  63. ep0_min = MAX(ep0_min, 0.0f);
  64. ep1_min = MAX(ep1_min, 0.0f);
  65. ep0_max = MAX(ep0_max, 1e-10f);
  66. ep1_max = MAX(ep1_max, 1e-10f);
  67. float4 error_weight = error_weightings[partition_index];
  68. float error_weight_rgbsum = error_weight.x + error_weight.y + error_weight.z;
  69. float range_upper_limit_rgb = encode_hdr_rgb ? 61440.0f : 65535.0f;
  70. float range_upper_limit_alpha = encode_hdr_alpha ? 61440.0f : 65535.0f;
  71. // it is possible to get endpoint colors significantly outside [0,upper-limit]
  72. // even if the input data are safely contained in [0,upper-limit];
  73. // we need to add an error term for this situation,
  74. float4 ep0_range_error_high;
  75. float4 ep1_range_error_high;
  76. float4 ep0_range_error_low;
  77. float4 ep1_range_error_low;
  78. ep0_range_error_high.x = MAX(0.0f, ep0.x - range_upper_limit_rgb);
  79. ep0_range_error_high.y = MAX(0.0f, ep0.y - range_upper_limit_rgb);
  80. ep0_range_error_high.z = MAX(0.0f, ep0.z - range_upper_limit_rgb);
  81. ep0_range_error_high.w = MAX(0.0f, ep0.w - range_upper_limit_alpha);
  82. ep1_range_error_high.x = MAX(0.0f, ep1.x - range_upper_limit_rgb);
  83. ep1_range_error_high.y = MAX(0.0f, ep1.y - range_upper_limit_rgb);
  84. ep1_range_error_high.z = MAX(0.0f, ep1.z - range_upper_limit_rgb);
  85. ep1_range_error_high.w = MAX(0.0f, ep1.w - range_upper_limit_alpha);
  86. ep0_range_error_low.x = MIN(0.0f, ep0.x);
  87. ep0_range_error_low.y = MIN(0.0f, ep0.y);
  88. ep0_range_error_low.z = MIN(0.0f, ep0.z);
  89. ep0_range_error_low.w = MIN(0.0f, ep0.w);
  90. ep1_range_error_low.x = MIN(0.0f, ep1.x);
  91. ep1_range_error_low.y = MIN(0.0f, ep1.y);
  92. ep1_range_error_low.z = MIN(0.0f, ep1.z);
  93. ep1_range_error_low.w = MIN(0.0f, ep1.w);
  94. float4 sum_range_error =
  95. (ep0_range_error_low * ep0_range_error_low) + (ep1_range_error_low * ep1_range_error_low) + (ep0_range_error_high * ep0_range_error_high) + (ep1_range_error_high * ep1_range_error_high);
  96. float rgb_range_error = dot(sum_range_error.xyz, error_weight.xyz) * 0.5f * partition_size;
  97. float alpha_range_error = sum_range_error.w * error_weight.w * 0.5f * partition_size;
  98. #ifdef DEBUG_PRINT_DIAGNOSTICS
  99. if (print_diagnostics)
  100. {
  101. printf("%s : partition=%d\nrgb-error_wt=%f alpha_error_wt=%f\n", __func__, partition_index, error_weight_rgbsum, error_weight.w);
  102. printf("ep0 = %f %f %f %f\n", ep0.x, ep0.y, ep0.z, ep0.w);
  103. printf("ep1 = %f %f %f %f\n", ep1.x, ep1.y, ep1.z, ep1.w);
  104. printf("rgb_range_error = %f, alpha_range_error = %f\n", rgb_range_error, alpha_range_error);
  105. printf("rgb-luma-error: %f\n", eci->rgb_luma_error);
  106. }
  107. #endif
  108. if (encode_hdr_rgb)
  109. {
  110. // collect some statistics
  111. float af, cf;
  112. if (ep1.x > ep1.y && ep1.x > ep1.z)
  113. {
  114. af = ep1.x;
  115. cf = ep1.x - ep0.x;
  116. }
  117. else if (ep1.y > ep1.z)
  118. {
  119. af = ep1.y;
  120. cf = ep1.y - ep0.y;
  121. }
  122. else
  123. {
  124. af = ep1.z;
  125. cf = ep1.z - ep0.z;
  126. }
  127. float bf = af - ep1_min; // estimate of color-component spread in high endpoint color
  128. float3 prd = ep1.xyz - float3(cf, cf, cf);
  129. float3 pdif = prd - ep0.xyz;
  130. // estimate of color-component spread in low endpoint color
  131. float df = MAX(MAX(fabs(pdif.x), fabs(pdif.y)), fabs(pdif.z));
  132. int b = (int)bf;
  133. int c = (int)cf;
  134. int d = (int)df;
  135. // determine which one of the 6 submodes is likely to be used in
  136. // case of an RGBO-mode
  137. int rgbo_mode = 5; // 7 bits per component
  138. // mode 4: 8 7 6
  139. if (b < 32768 && c < 16384)
  140. rgbo_mode = 4;
  141. // mode 3: 9 6 7
  142. if (b < 8192 && c < 16384)
  143. rgbo_mode = 3;
  144. // mode 2: 10 5 8
  145. if (b < 2048 && c < 16384)
  146. rgbo_mode = 2;
  147. // mode 1: 11 6 5
  148. if (b < 2048 && c < 1024)
  149. rgbo_mode = 1;
  150. // mode 0: 11 5 7
  151. if (b < 1024 && c < 4096)
  152. rgbo_mode = 0;
  153. // determine which one of the 9 submodes is likely to be used in
  154. // case of an RGB-mode.
  155. int rgb_mode = 8; // 8 bits per component, except 7 bits for blue
  156. // mode 0: 9 7 6 7
  157. if (b < 16384 && c < 8192 && d < 8192)
  158. rgb_mode = 0;
  159. // mode 1: 9 8 6 6
  160. if (b < 32768 && c < 8192 && d < 4096)
  161. rgb_mode = 1;
  162. // mode 2: 10 6 7 7
  163. if (b < 4096 && c < 8192 && d < 4096)
  164. rgb_mode = 2;
  165. // mode 3: 10 7 7 6
  166. if (b < 8192 && c < 8192 && d < 2048)
  167. rgb_mode = 3;
  168. // mode 4: 11 8 6 5
  169. if (b < 8192 && c < 2048 && d < 512)
  170. rgb_mode = 4;
  171. // mode 5: 11 6 8 6
  172. if (b < 2048 && c < 8192 && d < 1024)
  173. rgb_mode = 5;
  174. // mode 6: 12 7 7 5
  175. if (b < 2048 && c < 2048 && d < 256)
  176. rgb_mode = 6;
  177. // mode 7: 12 6 7 6
  178. if (b < 1024 && c < 2048 && d < 512)
  179. rgb_mode = 7;
  180. static const float rgbo_error_scales[6] = { 4.0f, 4.0f, 16.0f, 64.0f, 256.0f, 1024.0f };
  181. 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 };
  182. float mode7mult = rgbo_error_scales[rgbo_mode] * 0.0015f; // empirically determined ....
  183. float mode11mult = rgb_error_scales[rgb_mode] * 0.010f; // empirically determined ....
  184. float lum_high = (ep1.x + ep1.y + ep1.z) * (1.0f / 3.0f);
  185. float lum_low = (ep0.x + ep0.y + ep0.z) * (1.0f / 3.0f);
  186. float lumdif = lum_high - lum_low;
  187. float mode23mult = lumdif < 960 ? 4.0f : lumdif < 3968 ? 16.0f : 128.0f;
  188. mode23mult *= 0.0005f; // empirically determined ....
  189. // pick among the available HDR endpoint modes
  190. for (i = 0; i < 8; i++)
  191. {
  192. best_error[i][3] = 1e30f;
  193. format_of_choice[i][3] = encode_hdr_alpha ? FMT_HDR_RGBA : FMT_HDR_RGB_LDR_ALPHA;
  194. best_error[i][2] = 1e30f;
  195. format_of_choice[i][2] = FMT_HDR_RGB;
  196. best_error[i][1] = 1e30f;
  197. format_of_choice[i][1] = FMT_HDR_RGB_SCALE;
  198. best_error[i][0] = 1e30f;
  199. format_of_choice[i][0] = FMT_HDR_LUMINANCE_LARGE_RANGE;
  200. }
  201. for (i = 8; i < 21; i++)
  202. {
  203. // base_quant_error should depend on the scale-factor that would be used
  204. // during actual encode of the color value.
  205. float base_quant_error = baseline_quant_error[i] * partition_size * 1.0f;
  206. float rgb_quantization_error = error_weight_rgbsum * base_quant_error * 2.0f;
  207. float alpha_quantization_error = error_weight.w * base_quant_error * 2.0f;
  208. float rgba_quantization_error = rgb_quantization_error + alpha_quantization_error;
  209. #ifdef DEBUG_PRINT_DIAGNOSTICS
  210. if (print_diagnostics)
  211. printf("rgba-quant = %f can_offset_encode=%d\n", rgba_quantization_error, eci->can_offset_encode);
  212. #endif
  213. // for 8 integers, we have two encodings: one with HDR alpha and another one
  214. // with LDR alpha.
  215. float full_hdr_rgba_error = rgba_quantization_error + rgb_range_error + alpha_range_error;
  216. best_error[i][3] = full_hdr_rgba_error;
  217. format_of_choice[i][3] = encode_hdr_alpha ? FMT_HDR_RGBA : FMT_HDR_RGB_LDR_ALPHA;
  218. // for 6 integers, we have one HDR-RGB encoding
  219. float full_hdr_rgb_error = (rgb_quantization_error * mode11mult) + rgb_range_error + eci->alpha_drop_error;
  220. best_error[i][2] = full_hdr_rgb_error;
  221. format_of_choice[i][2] = FMT_HDR_RGB;
  222. // for 4 integers, we have one HDR-RGB-Scale encoding
  223. float hdr_rgb_scale_error = (rgb_quantization_error * mode7mult) + rgb_range_error + eci->alpha_drop_error + eci->rgb_luma_error;
  224. best_error[i][1] = hdr_rgb_scale_error;
  225. format_of_choice[i][1] = FMT_HDR_RGB_SCALE;
  226. // for 2 integers, we assume luminance-with-large-range
  227. float hdr_luminance_error = (rgb_quantization_error * mode23mult) + rgb_range_error + eci->alpha_drop_error + eci->luminance_error;
  228. best_error[i][0] = hdr_luminance_error;
  229. format_of_choice[i][0] = FMT_HDR_LUMINANCE_LARGE_RANGE;
  230. #ifdef DEBUG_PRINT_DIAGNOSTICS
  231. if (print_diagnostics)
  232. {
  233. for (j = 0; j < 4; j++)
  234. {
  235. printf("(hdr) quant-level=%d ints=%d format=%d error=%f\n", i, j, format_of_choice[i][j], best_error[i][j]);
  236. }
  237. }
  238. #endif
  239. }
  240. }
  241. else
  242. {
  243. for (i = 0; i < 4; i++)
  244. {
  245. best_error[i][3] = 1e30f;
  246. best_error[i][2] = 1e30f;
  247. best_error[i][1] = 1e30f;
  248. best_error[i][0] = 1e30f;
  249. format_of_choice[i][3] = FMT_RGBA;
  250. format_of_choice[i][2] = FMT_RGB;
  251. format_of_choice[i][1] = FMT_RGB_SCALE;
  252. format_of_choice[i][0] = FMT_LUMINANCE;
  253. }
  254. // pick among the available LDR endpoint modes
  255. for (i = 4; i < 21; i++)
  256. {
  257. float base_quant_error = baseline_quant_error[i] * partition_size * 1.0f;
  258. float rgb_quantization_error = error_weight_rgbsum * base_quant_error;
  259. float alpha_quantization_error = error_weight.w * base_quant_error;
  260. float rgba_quantization_error = rgb_quantization_error + alpha_quantization_error;
  261. #ifdef DEBUG_PRINT_DIAGNOSTICS
  262. if (print_diagnostics)
  263. printf("rgba-quant = %f can_offset_encode=%d\n", rgba_quantization_error, eci->can_offset_encode);
  264. #endif
  265. // for 8 integers, the available encodings are:
  266. // full LDR RGB-Alpha
  267. float full_ldr_rgba_error = rgba_quantization_error;
  268. if (eci->can_blue_contract)
  269. full_ldr_rgba_error *= 0.625f;
  270. if (eci->can_offset_encode && i <= 18)
  271. full_ldr_rgba_error *= 0.5f;
  272. full_ldr_rgba_error += rgb_range_error + alpha_range_error;
  273. best_error[i][3] = full_ldr_rgba_error;
  274. format_of_choice[i][3] = FMT_RGBA;
  275. // for 6 integers, we have:
  276. // - an LDR-RGB encoding
  277. // - an RGBS + Alpha encoding (LDR)
  278. float full_ldr_rgb_error = rgb_quantization_error;
  279. if (eci->can_blue_contract)
  280. full_ldr_rgb_error *= 0.5f;
  281. if (eci->can_offset_encode && i <= 18)
  282. full_ldr_rgb_error *= 0.25f;
  283. full_ldr_rgb_error += eci->alpha_drop_error + rgb_range_error;
  284. float rgbs_alpha_error = rgba_quantization_error + eci->rgb_scale_error + rgb_range_error + alpha_range_error;
  285. if (rgbs_alpha_error < full_ldr_rgb_error)
  286. {
  287. best_error[i][2] = rgbs_alpha_error;
  288. format_of_choice[i][2] = FMT_RGB_SCALE_ALPHA;
  289. }
  290. else
  291. {
  292. best_error[i][2] = full_ldr_rgb_error;
  293. format_of_choice[i][2] = FMT_RGB;
  294. }
  295. // for 4 integers, we have a Luminance-Alpha encoding and the RGBS encoding
  296. float ldr_rgbs_error = rgb_quantization_error + eci->alpha_drop_error + eci->rgb_scale_error + rgb_range_error;
  297. float lum_alpha_error = rgba_quantization_error + eci->luminance_error + rgb_range_error + alpha_range_error;
  298. if (ldr_rgbs_error < lum_alpha_error)
  299. {
  300. best_error[i][1] = ldr_rgbs_error;
  301. format_of_choice[i][1] = FMT_RGB_SCALE;
  302. }
  303. else
  304. {
  305. best_error[i][1] = lum_alpha_error;
  306. format_of_choice[i][1] = FMT_LUMINANCE_ALPHA;
  307. }
  308. // for 2 integers, we have a Luminance-encoding and an Alpha-encoding.
  309. float luminance_error = rgb_quantization_error + eci->alpha_drop_error + eci->luminance_error + rgb_range_error;
  310. best_error[i][0] = luminance_error;
  311. format_of_choice[i][0] = FMT_LUMINANCE;
  312. #ifdef DEBUG_PRINT_DIAGNOSTICS
  313. if (print_diagnostics)
  314. {
  315. for (j = 0; j < 4; j++)
  316. {
  317. printf(" (ldr) quant-level=%d ints=%d format=%d error=%f\n", i, j, format_of_choice[i][j], best_error[i][j]);
  318. }
  319. }
  320. #endif
  321. }
  322. }
  323. }
  324. // for 1 partition, find the best combination (one format + a quantization level) for a given bitcount
  325. static void one_partition_find_best_combination_for_bitcount(float combined_best_error[21][4],
  326. int formats_of_choice[21][4], int bits_available, int *best_quantization_level, int *best_formats, float *error_of_best_combination)
  327. {
  328. int i;
  329. int best_integer_count = -1;
  330. float best_integer_count_error = 1e20f;
  331. for (i = 0; i < 4; i++)
  332. {
  333. // compute the quantization level for a given number of integers and a given number of bits.
  334. int quantization_level = quantization_mode_table[i + 1][bits_available];
  335. if (quantization_level == -1)
  336. continue; // used to indicate the case where we don't have enough bits to represent a given endpoint format at all.
  337. if (combined_best_error[quantization_level][i] < best_integer_count_error)
  338. {
  339. best_integer_count_error = combined_best_error[quantization_level][i];
  340. best_integer_count = i;
  341. }
  342. }
  343. int ql = quantization_mode_table[best_integer_count + 1][bits_available];
  344. *best_quantization_level = ql;
  345. *error_of_best_combination = best_integer_count_error;
  346. if (ql >= 0)
  347. *best_formats = formats_of_choice[ql][best_integer_count];
  348. else
  349. *best_formats = FMT_LUMINANCE;
  350. }
  351. // for 2 partitions, find the best format combinations for every (quantization-mode, integer-count) combination
  352. static void two_partitions_find_best_combination_for_every_quantization_and_integer_count(float best_error[2][21][4], // indexed by (partition, quant-level, integer-pair-count-minus-1)
  353. int format_of_choice[2][21][4],
  354. float combined_best_error[21][7], // indexed by (quant-level, integer-pair-count-minus-2)
  355. int formats_of_choice[21][7][2])
  356. {
  357. int i, j;
  358. for (i = 0; i < 21; i++)
  359. for (j = 0; j < 7; j++)
  360. combined_best_error[i][j] = 1e30f;
  361. int quant;
  362. for (quant = 5; quant < 21; quant++)
  363. {
  364. for (i = 0; i < 4; i++) // integer-count for first endpoint-pair
  365. {
  366. for (j = 0; j < 4; j++) // integer-count for second endpoint-pair
  367. {
  368. int low2 = MIN(i, j);
  369. int high2 = MAX(i, j);
  370. if ((high2 - low2) > 1)
  371. continue;
  372. int intcnt = i + j;
  373. float errorterm = MIN(best_error[0][quant][i] + best_error[1][quant][j], 1e10f);
  374. if (errorterm <= combined_best_error[quant][intcnt])
  375. {
  376. combined_best_error[quant][intcnt] = errorterm;
  377. formats_of_choice[quant][intcnt][0] = format_of_choice[0][quant][i];
  378. formats_of_choice[quant][intcnt][1] = format_of_choice[1][quant][j];
  379. }
  380. }
  381. }
  382. }
  383. }
  384. // for 2 partitions, find the best combination (two formats + a quantization level) for a given bitcount
  385. static void two_partitions_find_best_combination_for_bitcount(float combined_best_error[21][7],
  386. int formats_of_choice[21][7][2],
  387. int bits_available, int *best_quantization_level, int *best_quantization_level_mod, int *best_formats, float *error_of_best_combination)
  388. {
  389. int i;
  390. int best_integer_count = 0;
  391. float best_integer_count_error = 1e20f;
  392. int integer_count;
  393. for (integer_count = 2; integer_count <= 8; integer_count++)
  394. {
  395. // compute the quantization level for a given number of integers and a given number of bits.
  396. int quantization_level = quantization_mode_table[integer_count][bits_available];
  397. if (quantization_level == -1)
  398. break; // used to indicate the case where we don't have enough bits to represent a given endpoint format at all.
  399. float integer_count_error = combined_best_error[quantization_level][integer_count - 2];
  400. if (integer_count_error < best_integer_count_error)
  401. {
  402. best_integer_count_error = integer_count_error;
  403. best_integer_count = integer_count;
  404. }
  405. }
  406. int ql = quantization_mode_table[best_integer_count][bits_available];
  407. int ql_mod = quantization_mode_table[best_integer_count][bits_available + 2];
  408. *best_quantization_level = ql;
  409. *best_quantization_level_mod = ql_mod;
  410. *error_of_best_combination = best_integer_count_error;
  411. if (ql >= 0)
  412. {
  413. for (i = 0; i < 2; i++)
  414. best_formats[i] = formats_of_choice[ql][best_integer_count - 2][i];
  415. }
  416. else
  417. {
  418. for (i = 0; i < 2; i++)
  419. best_formats[i] = FMT_LUMINANCE;
  420. }
  421. }
  422. // for 3 partitions, find the best format combinations for every (quantization-mode, integer-count) combination
  423. static void three_partitions_find_best_combination_for_every_quantization_and_integer_count(float best_error[3][21][4], // indexed by (partition, quant-level, integer-count)
  424. int format_of_choice[3][21][4], float combined_best_error[21][10], int formats_of_choice[21][10][3])
  425. {
  426. int i, j, k;
  427. for (i = 0; i < 21; i++)
  428. for (j = 0; j < 10; j++)
  429. combined_best_error[i][j] = 1e30f;
  430. int quant;
  431. for (quant = 5; quant < 21; quant++)
  432. {
  433. for (i = 0; i < 4; i++) // integer-count for first endpoint-pair
  434. {
  435. for (j = 0; j < 4; j++) // integer-count for second endpoint-pair
  436. {
  437. int low2 = MIN(i, j);
  438. int high2 = MAX(i, j);
  439. if ((high2 - low2) > 1)
  440. continue;
  441. for (k = 0; k < 4; k++) // integer-count for third endpoint-pair
  442. {
  443. int low3 = MIN(k, low2);
  444. int high3 = MAX(k, high2);
  445. if ((high3 - low3) > 1)
  446. continue;
  447. int intcnt = i + j + k;
  448. float errorterm = MIN(best_error[0][quant][i] + best_error[1][quant][j] + best_error[2][quant][k], 1e10f);
  449. if (errorterm <= combined_best_error[quant][intcnt])
  450. {
  451. combined_best_error[quant][intcnt] = errorterm;
  452. formats_of_choice[quant][intcnt][0] = format_of_choice[0][quant][i];
  453. formats_of_choice[quant][intcnt][1] = format_of_choice[1][quant][j];
  454. formats_of_choice[quant][intcnt][2] = format_of_choice[2][quant][k];
  455. }
  456. }
  457. }
  458. }
  459. }
  460. }
  461. // for 3 partitions, find the best combination (three formats + a quantization level) for a given bitcount
  462. static void three_partitions_find_best_combination_for_bitcount(float combined_best_error[21][10],
  463. int formats_of_choice[21][10][3],
  464. int bits_available, int *best_quantization_level, int *best_quantization_level_mod, int *best_formats, float *error_of_best_combination)
  465. {
  466. int i;
  467. int best_integer_count = 0;
  468. float best_integer_count_error = 1e20f;
  469. int integer_count;
  470. for (integer_count = 3; integer_count <= 9; integer_count++)
  471. {
  472. // compute the quantization level for a given number of integers and a given number of bits.
  473. int quantization_level = quantization_mode_table[integer_count][bits_available];
  474. if (quantization_level == -1)
  475. break; // used to indicate the case where we don't have enough bits to represent a given endpoint format at all.
  476. float integer_count_error = combined_best_error[quantization_level][integer_count - 3];
  477. if (integer_count_error < best_integer_count_error)
  478. {
  479. best_integer_count_error = integer_count_error;
  480. best_integer_count = integer_count;
  481. }
  482. }
  483. int ql = quantization_mode_table[best_integer_count][bits_available];
  484. int ql_mod = quantization_mode_table[best_integer_count][bits_available + 5];
  485. *best_quantization_level = ql;
  486. *best_quantization_level_mod = ql_mod;
  487. *error_of_best_combination = best_integer_count_error;
  488. if (ql >= 0)
  489. {
  490. for (i = 0; i < 3; i++)
  491. best_formats[i] = formats_of_choice[ql][best_integer_count - 3][i];
  492. }
  493. else
  494. {
  495. for (i = 0; i < 3; i++)
  496. best_formats[i] = FMT_LUMINANCE;
  497. }
  498. }
  499. // for 4 partitions, find the best format combinations for every (quantization-mode, integer-count) combination
  500. static void four_partitions_find_best_combination_for_every_quantization_and_integer_count(float best_error[4][21][4], // indexed by (partition, quant-level, integer-count)
  501. int format_of_choice[4][21][4], float combined_best_error[21][13], int formats_of_choice[21][13][4])
  502. {
  503. int i, j, k, l;
  504. for (i = 0; i < 21; i++)
  505. for (j = 0; j < 13; j++)
  506. combined_best_error[i][j] = 1e30f;
  507. int quant;
  508. for (quant = 5; quant < 21; quant++)
  509. {
  510. for (i = 0; i < 4; i++) // integer-count for first endpoint-pair
  511. {
  512. for (j = 0; j < 4; j++) // integer-count for second endpoint-pair
  513. {
  514. int low2 = MIN(i, j);
  515. int high2 = MAX(i, j);
  516. if ((high2 - low2) > 1)
  517. continue;
  518. for (k = 0; k < 4; k++) // integer-count for third endpoint-pair
  519. {
  520. int low3 = MIN(k, low2);
  521. int high3 = MAX(k, high2);
  522. if ((high3 - low3) > 1)
  523. continue;
  524. for (l = 0; l < 4; l++) // integer-count for fourth endpoint-pair
  525. {
  526. int low4 = MIN(l, low3);
  527. int high4 = MAX(l, high3);
  528. if ((high4 - low4) > 1)
  529. continue;
  530. int intcnt = i + j + k + l;
  531. float errorterm = MIN(best_error[0][quant][i] + best_error[1][quant][j] + best_error[2][quant][k] + best_error[3][quant][l], 1e10f);
  532. if (errorterm <= combined_best_error[quant][intcnt])
  533. {
  534. combined_best_error[quant][intcnt] = errorterm;
  535. formats_of_choice[quant][intcnt][0] = format_of_choice[0][quant][i];
  536. formats_of_choice[quant][intcnt][1] = format_of_choice[1][quant][j];
  537. formats_of_choice[quant][intcnt][2] = format_of_choice[2][quant][k];
  538. formats_of_choice[quant][intcnt][3] = format_of_choice[3][quant][l];
  539. }
  540. }
  541. }
  542. }
  543. }
  544. }
  545. }
  546. // for 4 partitions, find the best combination (four formats + a quantization level) for a given bitcount
  547. static void four_partitions_find_best_combination_for_bitcount(float combined_best_error[21][13],
  548. int formats_of_choice[21][13][4],
  549. int bits_available, int *best_quantization_level, int *best_quantization_level_mod, int *best_formats, float *error_of_best_combination)
  550. {
  551. int i;
  552. int best_integer_count = 0;
  553. float best_integer_count_error = 1e20f;
  554. int integer_count;
  555. for (integer_count = 4; integer_count <= 9; integer_count++)
  556. {
  557. // compute the quantization level for a given number of integers and a given number of bits.
  558. int quantization_level = quantization_mode_table[integer_count][bits_available];
  559. if (quantization_level == -1)
  560. break; // used to indicate the case where we don't have enough bits to represent a given endpoint format at all.
  561. float integer_count_error = combined_best_error[quantization_level][integer_count - 4];
  562. if (integer_count_error < best_integer_count_error)
  563. {
  564. best_integer_count_error = integer_count_error;
  565. best_integer_count = integer_count;
  566. }
  567. }
  568. int ql = quantization_mode_table[best_integer_count][bits_available];
  569. int ql_mod = quantization_mode_table[best_integer_count][bits_available + 8];
  570. *best_quantization_level = ql;
  571. *best_quantization_level_mod = ql_mod;
  572. *error_of_best_combination = best_integer_count_error;
  573. if (ql >= 0)
  574. {
  575. for (i = 0; i < 4; i++)
  576. best_formats[i] = formats_of_choice[ql][best_integer_count - 4][i];
  577. }
  578. else
  579. {
  580. for (i = 0; i < 4; i++)
  581. best_formats[i] = FMT_LUMINANCE;
  582. }
  583. }
  584. /*
  585. The determine_optimal_set_of_endpoint_formats_to_use() function.
  586. It identifies, for each mode, which set of color endpoint encodings
  587. produces the best overall result. It then reports back which 4 modes
  588. look best, along with the ideal color encoding combination for each.
  589. It takes as input:
  590. a partitioning an imageblock,
  591. a set of color endpoints.
  592. for each mode, the number of bits available for color encoding and the error incurred by quantization.
  593. in case of 2 plane of weights, a specifier for which color component to use for the second plane of weights.
  594. It delivers as output for each of the 4 selected modes:
  595. format specifier
  596. for each partition
  597. quantization level to use
  598. modified quantization level to use
  599. (when all format specifiers are equal)
  600. */
  601. void determine_optimal_set_of_endpoint_formats_to_use(int xdim, int ydim, int zdim,
  602. const partition_info * pt, const imageblock * blk, const error_weight_block * ewb,
  603. const endpoints * ep,
  604. int separate_component, // separate color component for 2-plane mode; -1 for single-plane mode
  605. // bitcounts and errors computed for the various quantization methods
  606. const int *qwt_bitcounts, const float *qwt_errors,
  607. // output data
  608. int partition_format_specifiers[4][4], int quantized_weight[4],
  609. int quantization_level[4], int quantization_level_mod[4])
  610. {
  611. int i, j;
  612. int partition_count = pt->partition_count;
  613. int encode_hdr_rgb = blk->rgb_lns[0];
  614. int encode_hdr_alpha = blk->alpha_lns[0];
  615. // call a helper function to compute the errors that result from various
  616. // encoding choices (such as using luminance instead of RGB, discarding Alpha,
  617. // using RGB-scale in place of two separate RGB endpoints and so on)
  618. encoding_choice_errors eci[4];
  619. compute_encoding_choice_errors(xdim, ydim, zdim, blk, pt, ewb, separate_component, eci);
  620. // for each partition, compute the error weights to apply for that partition.
  621. float4 error_weightings[4];
  622. float4 dummied_color_scalefactors[4]; // only used to receive data
  623. compute_partition_error_color_weightings(xdim, ydim, zdim, ewb, pt, error_weightings, dummied_color_scalefactors);
  624. float best_error[4][21][4];
  625. int format_of_choice[4][21][4];
  626. for (i = 0; i < partition_count; i++)
  627. compute_color_error_for_every_integer_count_and_quantization_level(encode_hdr_rgb, encode_hdr_alpha, i, pt, &(eci[i]), ep, error_weightings, best_error[i], format_of_choice[i]);
  628. float errors_of_best_combination[MAX_WEIGHT_MODES];
  629. int best_quantization_levels[MAX_WEIGHT_MODES];
  630. int best_quantization_levels_mod[MAX_WEIGHT_MODES];
  631. int best_ep_formats[MAX_WEIGHT_MODES][4];
  632. // code for the case where the block contains 1 partition
  633. if (partition_count == 1)
  634. {
  635. int best_quantization_level;
  636. int best_format;
  637. float error_of_best_combination;
  638. for (i = 0; i < MAX_WEIGHT_MODES; i++)
  639. {
  640. if (qwt_errors[i] >= 1e29f)
  641. {
  642. errors_of_best_combination[i] = 1e30f;
  643. continue;
  644. }
  645. one_partition_find_best_combination_for_bitcount(best_error[0], format_of_choice[0], qwt_bitcounts[i], &best_quantization_level, &best_format, &error_of_best_combination);
  646. error_of_best_combination += qwt_errors[i];
  647. errors_of_best_combination[i] = error_of_best_combination;
  648. best_quantization_levels[i] = best_quantization_level;
  649. best_quantization_levels_mod[i] = best_quantization_level;
  650. best_ep_formats[i][0] = best_format;
  651. }
  652. }
  653. // code for the case where the block contains 2 partitions
  654. else if (partition_count == 2)
  655. {
  656. int best_quantization_level;
  657. int best_quantization_level_mod;
  658. int best_formats[2];
  659. float error_of_best_combination;
  660. float combined_best_error[21][7];
  661. int formats_of_choice[21][7][2];
  662. two_partitions_find_best_combination_for_every_quantization_and_integer_count(best_error, format_of_choice, combined_best_error, formats_of_choice);
  663. for (i = 0; i < MAX_WEIGHT_MODES; i++)
  664. {
  665. if (qwt_errors[i] >= 1e29f)
  666. {
  667. errors_of_best_combination[i] = 1e30f;
  668. continue;
  669. }
  670. two_partitions_find_best_combination_for_bitcount(combined_best_error, formats_of_choice, qwt_bitcounts[i],
  671. &best_quantization_level, &best_quantization_level_mod, best_formats, &error_of_best_combination);
  672. error_of_best_combination += qwt_errors[i];
  673. errors_of_best_combination[i] = error_of_best_combination;
  674. best_quantization_levels[i] = best_quantization_level;
  675. best_quantization_levels_mod[i] = best_quantization_level_mod;
  676. best_ep_formats[i][0] = best_formats[0];
  677. best_ep_formats[i][1] = best_formats[1];
  678. }
  679. }
  680. // code for the case where the block contains 3 partitions
  681. else if (partition_count == 3)
  682. {
  683. int best_quantization_level;
  684. int best_quantization_level_mod;
  685. int best_formats[3];
  686. float error_of_best_combination;
  687. float combined_best_error[21][10];
  688. int formats_of_choice[21][10][3];
  689. three_partitions_find_best_combination_for_every_quantization_and_integer_count(best_error, format_of_choice, combined_best_error, formats_of_choice);
  690. for (i = 0; i < MAX_WEIGHT_MODES; i++)
  691. {
  692. if (qwt_errors[i] >= 1e29f)
  693. {
  694. errors_of_best_combination[i] = 1e30f;
  695. continue;
  696. }
  697. three_partitions_find_best_combination_for_bitcount(combined_best_error,
  698. formats_of_choice, qwt_bitcounts[i], &best_quantization_level, &best_quantization_level_mod, best_formats, &error_of_best_combination);
  699. error_of_best_combination += qwt_errors[i];
  700. errors_of_best_combination[i] = error_of_best_combination;
  701. best_quantization_levels[i] = best_quantization_level;
  702. best_quantization_levels_mod[i] = best_quantization_level_mod;
  703. best_ep_formats[i][0] = best_formats[0];
  704. best_ep_formats[i][1] = best_formats[1];
  705. best_ep_formats[i][2] = best_formats[2];
  706. }
  707. }
  708. // code for the case where the block contains 4 partitions
  709. else if (partition_count == 4)
  710. {
  711. int best_quantization_level;
  712. int best_quantization_level_mod;
  713. int best_formats[4];
  714. float error_of_best_combination;
  715. float combined_best_error[21][13];
  716. int formats_of_choice[21][13][4];
  717. four_partitions_find_best_combination_for_every_quantization_and_integer_count(best_error, format_of_choice, combined_best_error, formats_of_choice);
  718. for (i = 0; i < MAX_WEIGHT_MODES; i++)
  719. {
  720. if (qwt_errors[i] >= 1e29f)
  721. {
  722. errors_of_best_combination[i] = 1e30f;
  723. continue;
  724. }
  725. four_partitions_find_best_combination_for_bitcount(combined_best_error,
  726. formats_of_choice, qwt_bitcounts[i], &best_quantization_level, &best_quantization_level_mod, best_formats, &error_of_best_combination);
  727. error_of_best_combination += qwt_errors[i];
  728. errors_of_best_combination[i] = error_of_best_combination;
  729. best_quantization_levels[i] = best_quantization_level;
  730. best_quantization_levels_mod[i] = best_quantization_level_mod;
  731. best_ep_formats[i][0] = best_formats[0];
  732. best_ep_formats[i][1] = best_formats[1];
  733. best_ep_formats[i][2] = best_formats[2];
  734. best_ep_formats[i][3] = best_formats[3];
  735. }
  736. }
  737. // finally, go through the results and pick the 4 best-looking modes.
  738. int best_error_weights[4];
  739. for (i = 0; i < 4; i++)
  740. {
  741. float best_ep_error = 1e30f;
  742. int best_error_index = -1;
  743. for (j = 0; j < MAX_WEIGHT_MODES; j++)
  744. {
  745. if (errors_of_best_combination[j] < best_ep_error && best_quantization_levels[j] >= 5)
  746. {
  747. best_ep_error = errors_of_best_combination[j];
  748. best_error_index = j;
  749. }
  750. }
  751. best_error_weights[i] = best_error_index;
  752. if(best_error_index >= 0)
  753. {
  754. errors_of_best_combination[best_error_index] = 1e30f;
  755. }
  756. }
  757. for (i = 0; i < 4; i++)
  758. {
  759. quantized_weight[i] = best_error_weights[i];
  760. if (quantized_weight[i] >= 0)
  761. {
  762. quantization_level[i] = best_quantization_levels[best_error_weights[i]];
  763. quantization_level_mod[i] = best_quantization_levels_mod[best_error_weights[i]];
  764. for (j = 0; j < partition_count; j++)
  765. {
  766. partition_format_specifiers[i][j] = best_ep_formats[best_error_weights[i]][j];
  767. }
  768. }
  769. }
  770. }