astcenc_internal.h 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174
  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. /**
  18. * @brief Functions and data declarations.
  19. */
  20. #ifndef ASTCENC_INTERNAL_INCLUDED
  21. #define ASTCENC_INTERNAL_INCLUDED
  22. #include <algorithm>
  23. #include <cstddef>
  24. #include <cstdint>
  25. #if defined(ASTCENC_DIAGNOSTICS)
  26. #include <cstdio>
  27. #endif
  28. #include <cstdlib>
  29. #include "astcenc.h"
  30. #include "astcenc_mathlib.h"
  31. #include "astcenc_vecmathlib.h"
  32. /**
  33. * @brief Make a promise to the compiler's optimizer.
  34. *
  35. * A promise is an expression that the optimizer is can assume is true for to help it generate
  36. * faster code. Common use cases for this are to promise that a for loop will iterate more than
  37. * once, or that the loop iteration count is a multiple of a vector length, which avoids pre-loop
  38. * checks and can avoid loop tails if loops are unrolled by the auto-vectorizer.
  39. */
  40. #if defined(NDEBUG)
  41. #if !defined(__clang__) && defined(_MSC_VER)
  42. #define promise(cond) __assume(cond)
  43. #elif defined(__clang__)
  44. #if __has_builtin(__builtin_assume)
  45. #define promise(cond) __builtin_assume(cond)
  46. #elif __has_builtin(__builtin_unreachable)
  47. #define promise(cond) if (!(cond)) { __builtin_unreachable(); }
  48. #else
  49. #define promise(cond)
  50. #endif
  51. #else // Assume GCC
  52. #define promise(cond) if (!(cond)) { __builtin_unreachable(); }
  53. #endif
  54. #else
  55. #define promise(cond) assert(cond)
  56. #endif
  57. /* ============================================================================
  58. Constants
  59. ============================================================================ */
  60. #if !defined(ASTCENC_BLOCK_MAX_TEXELS)
  61. #define ASTCENC_BLOCK_MAX_TEXELS 216 // A 3D 6x6x6 block
  62. #endif
  63. /** @brief The maximum number of texels a block can support (6x6x6 block). */
  64. static constexpr unsigned int BLOCK_MAX_TEXELS { ASTCENC_BLOCK_MAX_TEXELS };
  65. /** @brief The maximum number of components a block can support. */
  66. static constexpr unsigned int BLOCK_MAX_COMPONENTS { 4 };
  67. /** @brief The maximum number of partitions a block can support. */
  68. static constexpr unsigned int BLOCK_MAX_PARTITIONS { 4 };
  69. /** @brief The number of partitionings, per partition count, suported by the ASTC format. */
  70. static constexpr unsigned int BLOCK_MAX_PARTITIONINGS { 1024 };
  71. /** @brief The maximum number of weights used during partition selection for texel clustering. */
  72. static constexpr uint8_t BLOCK_MAX_KMEANS_TEXELS { 64 };
  73. /** @brief The maximum number of weights a block can support. */
  74. static constexpr unsigned int BLOCK_MAX_WEIGHTS { 64 };
  75. /** @brief The maximum number of weights a block can support per plane in 2 plane mode. */
  76. static constexpr unsigned int BLOCK_MAX_WEIGHTS_2PLANE { BLOCK_MAX_WEIGHTS / 2 };
  77. /** @brief The minimum number of weight bits a candidate encoding must encode. */
  78. static constexpr unsigned int BLOCK_MIN_WEIGHT_BITS { 24 };
  79. /** @brief The maximum number of weight bits a candidate encoding can encode. */
  80. static constexpr unsigned int BLOCK_MAX_WEIGHT_BITS { 96 };
  81. /** @brief The index indicating a bad (unused) block mode in the remap array. */
  82. static constexpr uint16_t BLOCK_BAD_BLOCK_MODE { 0xFFFFu };
  83. /** @brief The index indicating a bad (unused) partitioning in the remap array. */
  84. static constexpr uint16_t BLOCK_BAD_PARTITIONING { 0xFFFFu };
  85. /** @brief The number of partition index bits supported by the ASTC format . */
  86. static constexpr unsigned int PARTITION_INDEX_BITS { 10 };
  87. /** @brief The offset of the plane 2 weights in shared weight arrays. */
  88. static constexpr unsigned int WEIGHTS_PLANE2_OFFSET { BLOCK_MAX_WEIGHTS_2PLANE };
  89. /** @brief The sum of quantized weights for one texel. */
  90. static constexpr float WEIGHTS_TEXEL_SUM { 16.0f };
  91. /** @brief The number of block modes supported by the ASTC format. */
  92. static constexpr unsigned int WEIGHTS_MAX_BLOCK_MODES { 2048 };
  93. /** @brief The number of weight grid decimation modes supported by the ASTC format. */
  94. static constexpr unsigned int WEIGHTS_MAX_DECIMATION_MODES { 87 };
  95. /** @brief The high default error used to initialize error trackers. */
  96. static constexpr float ERROR_CALC_DEFAULT { 1e30f };
  97. /**
  98. * @brief The minimum texel count for a block to use the one partition fast path.
  99. *
  100. * This setting skips 4x4 and 5x4 block sizes.
  101. */
  102. static constexpr unsigned int TUNE_MIN_TEXELS_MODE0_FASTPATH { 24 };
  103. /**
  104. * @brief The maximum number of candidate encodings tested for each encoding mode.
  105. *
  106. * This can be dynamically reduced by the compression quality preset.
  107. */
  108. static constexpr unsigned int TUNE_MAX_TRIAL_CANDIDATES { 8 };
  109. /**
  110. * @brief The maximum number of candidate partitionings tested for each encoding mode.
  111. *
  112. * This can be dynamically reduced by the compression quality preset.
  113. */
  114. static constexpr unsigned int TUNE_MAX_PARTITIONING_CANDIDATES { 32 };
  115. /**
  116. * @brief The maximum quant level using full angular endpoint search method.
  117. *
  118. * The angular endpoint search is used to find the min/max weight that should
  119. * be used for a given quantization level. It is effective but expensive, so
  120. * we only use it where it has the most value - low quant levels with wide
  121. * spacing. It is used below TUNE_MAX_ANGULAR_QUANT (inclusive). Above this we
  122. * assume the min weight is 0.0f, and the max weight is 1.0f.
  123. *
  124. * Note the angular algorithm is vectorized, and using QUANT_12 exactly fills
  125. * one 8-wide vector. Decreasing by one doesn't buy much performance, and
  126. * increasing by one is disproportionately expensive.
  127. */
  128. static constexpr unsigned int TUNE_MAX_ANGULAR_QUANT { 7 }; /* QUANT_12 */
  129. static_assert((BLOCK_MAX_TEXELS % ASTCENC_SIMD_WIDTH) == 0,
  130. "BLOCK_MAX_TEXELS must be multiple of ASTCENC_SIMD_WIDTH");
  131. static_assert(BLOCK_MAX_TEXELS <= 216,
  132. "BLOCK_MAX_TEXELS must not be greater than 216");
  133. static_assert((BLOCK_MAX_WEIGHTS % ASTCENC_SIMD_WIDTH) == 0,
  134. "BLOCK_MAX_WEIGHTS must be multiple of ASTCENC_SIMD_WIDTH");
  135. static_assert((WEIGHTS_MAX_BLOCK_MODES % ASTCENC_SIMD_WIDTH) == 0,
  136. "WEIGHTS_MAX_BLOCK_MODES must be multiple of ASTCENC_SIMD_WIDTH");
  137. /* ============================================================================
  138. Commonly used data structures
  139. ============================================================================ */
  140. /**
  141. * @brief The ASTC endpoint formats.
  142. *
  143. * Note, the values here are used directly in the encoding in the format so do not rearrange.
  144. */
  145. enum endpoint_formats
  146. {
  147. FMT_LUMINANCE = 0,
  148. FMT_LUMINANCE_DELTA = 1,
  149. FMT_HDR_LUMINANCE_LARGE_RANGE = 2,
  150. FMT_HDR_LUMINANCE_SMALL_RANGE = 3,
  151. FMT_LUMINANCE_ALPHA = 4,
  152. FMT_LUMINANCE_ALPHA_DELTA = 5,
  153. FMT_RGB_SCALE = 6,
  154. FMT_HDR_RGB_SCALE = 7,
  155. FMT_RGB = 8,
  156. FMT_RGB_DELTA = 9,
  157. FMT_RGB_SCALE_ALPHA = 10,
  158. FMT_HDR_RGB = 11,
  159. FMT_RGBA = 12,
  160. FMT_RGBA_DELTA = 13,
  161. FMT_HDR_RGB_LDR_ALPHA = 14,
  162. FMT_HDR_RGBA = 15
  163. };
  164. /**
  165. * @brief The ASTC quantization methods.
  166. *
  167. * Note, the values here are used directly in the encoding in the format so do not rearrange.
  168. */
  169. enum quant_method
  170. {
  171. QUANT_2 = 0,
  172. QUANT_3 = 1,
  173. QUANT_4 = 2,
  174. QUANT_5 = 3,
  175. QUANT_6 = 4,
  176. QUANT_8 = 5,
  177. QUANT_10 = 6,
  178. QUANT_12 = 7,
  179. QUANT_16 = 8,
  180. QUANT_20 = 9,
  181. QUANT_24 = 10,
  182. QUANT_32 = 11,
  183. QUANT_40 = 12,
  184. QUANT_48 = 13,
  185. QUANT_64 = 14,
  186. QUANT_80 = 15,
  187. QUANT_96 = 16,
  188. QUANT_128 = 17,
  189. QUANT_160 = 18,
  190. QUANT_192 = 19,
  191. QUANT_256 = 20
  192. };
  193. /**
  194. * @brief The number of levels use by an ASTC quantization method.
  195. *
  196. * @param method The quantization method
  197. *
  198. * @return The number of levels used by @c method.
  199. */
  200. static inline unsigned int get_quant_level(quant_method method)
  201. {
  202. switch (method)
  203. {
  204. case QUANT_2: return 2;
  205. case QUANT_3: return 3;
  206. case QUANT_4: return 4;
  207. case QUANT_5: return 5;
  208. case QUANT_6: return 6;
  209. case QUANT_8: return 8;
  210. case QUANT_10: return 10;
  211. case QUANT_12: return 12;
  212. case QUANT_16: return 16;
  213. case QUANT_20: return 20;
  214. case QUANT_24: return 24;
  215. case QUANT_32: return 32;
  216. case QUANT_40: return 40;
  217. case QUANT_48: return 48;
  218. case QUANT_64: return 64;
  219. case QUANT_80: return 80;
  220. case QUANT_96: return 96;
  221. case QUANT_128: return 128;
  222. case QUANT_160: return 160;
  223. case QUANT_192: return 192;
  224. case QUANT_256: return 256;
  225. }
  226. // Unreachable - the enum is fully described
  227. return 0;
  228. }
  229. /**
  230. * @brief Computed metrics about a partition in a block.
  231. */
  232. struct partition_metrics
  233. {
  234. /** @brief The error-weighted average color in the partition. */
  235. vfloat4 avg;
  236. /** @brief The dominant error-weighted direction in the partition. */
  237. vfloat4 dir;
  238. };
  239. /**
  240. * @brief Computed lines for a a three component analysis.
  241. */
  242. struct partition_lines3
  243. {
  244. /** @brief Line for uncorrelated chroma. */
  245. line3 uncor_line;
  246. /** @brief Line for correlated chroma, passing though the origin. */
  247. line3 samec_line;
  248. /** @brief Post-processed line for uncorrelated chroma. */
  249. processed_line3 uncor_pline;
  250. /** @brief Post-processed line for correlated chroma, passing though the origin. */
  251. processed_line3 samec_pline;
  252. /**
  253. * @brief The length of the line for uncorrelated chroma.
  254. *
  255. * This is used for both the uncorrelated and same chroma lines - they are normally very similar
  256. * and only used for the relative ranking of partitionings against one another.
  257. */
  258. float line_length;
  259. };
  260. /**
  261. * @brief The partition information for a single partition.
  262. *
  263. * ASTC has a total of 1024 candidate partitions for each of 2/3/4 partition counts, although this
  264. * 1024 includes seeds that generate duplicates of other seeds and seeds that generate completely
  265. * empty partitions. These are both valid encodings, but astcenc will skip both during compression
  266. * as they are not useful.
  267. */
  268. struct partition_info
  269. {
  270. /** @brief The number of partitions in this partitioning. */
  271. uint16_t partition_count;
  272. /** @brief The index (seed) of this partitioning. */
  273. uint16_t partition_index;
  274. /**
  275. * @brief The number of texels in each partition.
  276. *
  277. * Note that some seeds result in zero texels assigned to a partition. These are valid, but are
  278. * skipped by this compressor as there is no point spending bits encoding an unused endpoints.
  279. */
  280. uint8_t partition_texel_count[BLOCK_MAX_PARTITIONS];
  281. /** @brief The partition of each texel in the block. */
  282. uint8_t partition_of_texel[BLOCK_MAX_TEXELS];
  283. /** @brief The list of texels in each partition. */
  284. uint8_t texels_of_partition[BLOCK_MAX_PARTITIONS][BLOCK_MAX_TEXELS];
  285. };
  286. /**
  287. * @brief The weight grid information for a single decimation pattern.
  288. *
  289. * ASTC can store one weight per texel, but is also capable of storing lower resolution weight grids
  290. * that are interpolated during decompression to assign a with to a texel. Storing fewer weights
  291. * can free up a substantial amount of bits that we can then spend on more useful things, such as
  292. * more accurate endpoints and weights, or additional partitions.
  293. *
  294. * This data structure is used to store information about a single weight grid decimation pattern,
  295. * for a single block size.
  296. */
  297. struct decimation_info
  298. {
  299. /** @brief The total number of texels in the block. */
  300. uint8_t texel_count;
  301. /** @brief The maximum number of stored weights that contribute to each texel, between 1 and 4. */
  302. uint8_t max_texel_weight_count;
  303. /** @brief The total number of weights stored. */
  304. uint8_t weight_count;
  305. /** @brief The number of stored weights in the X dimension. */
  306. uint8_t weight_x;
  307. /** @brief The number of stored weights in the Y dimension. */
  308. uint8_t weight_y;
  309. /** @brief The number of stored weights in the Z dimension. */
  310. uint8_t weight_z;
  311. /**
  312. * @brief The number of weights that contribute to each texel.
  313. * Value is between 1 and 4.
  314. */
  315. uint8_t texel_weight_count[BLOCK_MAX_TEXELS];
  316. /**
  317. * @brief The weight index of the N weights that are interpolated for each texel.
  318. * Stored transposed to improve vectorization.
  319. */
  320. uint8_t texel_weights_tr[4][BLOCK_MAX_TEXELS];
  321. /**
  322. * @brief The bilinear contribution of the N weights that are interpolated for each texel.
  323. * Value is between 0 and 16, stored transposed to improve vectorization.
  324. */
  325. uint8_t texel_weight_contribs_int_tr[4][BLOCK_MAX_TEXELS];
  326. /**
  327. * @brief The bilinear contribution of the N weights that are interpolated for each texel.
  328. * Value is between 0 and 1, stored transposed to improve vectorization.
  329. */
  330. alignas(ASTCENC_VECALIGN) float texel_weight_contribs_float_tr[4][BLOCK_MAX_TEXELS];
  331. /** @brief The number of texels that each stored weight contributes to. */
  332. uint8_t weight_texel_count[BLOCK_MAX_WEIGHTS];
  333. /**
  334. * @brief The list of texels that use a specific weight index.
  335. * Stored transposed to improve vectorization.
  336. */
  337. uint8_t weight_texels_tr[BLOCK_MAX_TEXELS][BLOCK_MAX_WEIGHTS];
  338. /**
  339. * @brief The bilinear contribution to the N texels that use each weight.
  340. * Value is between 0 and 1, stored transposed to improve vectorization.
  341. */
  342. alignas(ASTCENC_VECALIGN) float weights_texel_contribs_tr[BLOCK_MAX_TEXELS][BLOCK_MAX_WEIGHTS];
  343. /**
  344. * @brief The bilinear contribution to the Nth texel that uses each weight.
  345. * Value is between 0 and 1, stored transposed to improve vectorization.
  346. */
  347. float texel_contrib_for_weight[BLOCK_MAX_TEXELS][BLOCK_MAX_WEIGHTS];
  348. };
  349. /**
  350. * @brief Metadata for single block mode for a specific block size.
  351. */
  352. struct block_mode
  353. {
  354. /** @brief The block mode index in the ASTC encoded form. */
  355. uint16_t mode_index;
  356. /** @brief The decimation mode index in the compressor reindexed list. */
  357. uint8_t decimation_mode;
  358. /** @brief The weight quantization used by this block mode. */
  359. uint8_t quant_mode;
  360. /** @brief The weight quantization used by this block mode. */
  361. uint8_t weight_bits;
  362. /** @brief Is a dual weight plane used by this block mode? */
  363. uint8_t is_dual_plane : 1;
  364. /**
  365. * @brief Get the weight quantization used by this block mode.
  366. *
  367. * @return The quantization level.
  368. */
  369. inline quant_method get_weight_quant_mode() const
  370. {
  371. return static_cast<quant_method>(this->quant_mode);
  372. }
  373. };
  374. /**
  375. * @brief Metadata for single decimation mode for a specific block size.
  376. */
  377. struct decimation_mode
  378. {
  379. /** @brief The max weight precision for 1 plane, or -1 if not supported. */
  380. int8_t maxprec_1plane;
  381. /** @brief The max weight precision for 2 planes, or -1 if not supported. */
  382. int8_t maxprec_2planes;
  383. /**
  384. * @brief Bitvector indicating weight quant modes used by active 1 plane block modes.
  385. *
  386. * Bit 0 = QUANT_2, Bit 1 = QUANT_3, etc.
  387. */
  388. uint16_t refprec_1plane;
  389. /**
  390. * @brief Bitvector indicating weight quant methods used by active 2 plane block modes.
  391. *
  392. * Bit 0 = QUANT_2, Bit 1 = QUANT_3, etc.
  393. */
  394. uint16_t refprec_2planes;
  395. /**
  396. * @brief Set a 1 plane weight quant as active.
  397. *
  398. * @param weight_quant The quant method to set.
  399. */
  400. void set_ref_1plane(quant_method weight_quant)
  401. {
  402. refprec_1plane |= (1 << weight_quant);
  403. }
  404. /**
  405. * @brief Test if this mode is active below a given 1 plane weight quant (inclusive).
  406. *
  407. * @param max_weight_quant The max quant method to test.
  408. */
  409. bool is_ref_1plane(quant_method max_weight_quant) const
  410. {
  411. uint16_t mask = static_cast<uint16_t>((1 << (max_weight_quant + 1)) - 1);
  412. return (refprec_1plane & mask) != 0;
  413. }
  414. /**
  415. * @brief Set a 2 plane weight quant as active.
  416. *
  417. * @param weight_quant The quant method to set.
  418. */
  419. void set_ref_2plane(quant_method weight_quant)
  420. {
  421. refprec_2planes |= static_cast<uint16_t>(1 << weight_quant);
  422. }
  423. /**
  424. * @brief Test if this mode is active below a given 2 plane weight quant (inclusive).
  425. *
  426. * @param max_weight_quant The max quant method to test.
  427. */
  428. bool is_ref_2plane(quant_method max_weight_quant) const
  429. {
  430. uint16_t mask = static_cast<uint16_t>((1 << (max_weight_quant + 1)) - 1);
  431. return (refprec_2planes & mask) != 0;
  432. }
  433. };
  434. /**
  435. * @brief Data tables for a single block size.
  436. *
  437. * The decimation tables store the information to apply weight grid dimension reductions. We only
  438. * store the decimation modes that are actually needed by the current context; many of the possible
  439. * modes will be unused (too many weights for the current block size or disabled by heuristics). The
  440. * actual number of weights stored is @c decimation_mode_count, and the @c decimation_modes and
  441. * @c decimation_tables arrays store the active modes contiguously at the start of the array. These
  442. * entries are not stored in any particular order.
  443. *
  444. * The block mode tables store the unpacked block mode settings. Block modes are stored in the
  445. * compressed block as an 11 bit field, but for any given block size and set of compressor
  446. * heuristics, only a subset of the block modes will be used. The actual number of block modes
  447. * stored is indicated in @c block_mode_count, and the @c block_modes array store the active modes
  448. * contiguously at the start of the array. These entries are stored in incrementing "packed" value
  449. * order, which doesn't mean much once unpacked. To allow decompressors to reference the packed data
  450. * efficiently the @c block_mode_packed_index array stores the mapping between physical ID and the
  451. * actual remapped array index.
  452. */
  453. struct block_size_descriptor
  454. {
  455. /** @brief The block X dimension, in texels. */
  456. uint8_t xdim;
  457. /** @brief The block Y dimension, in texels. */
  458. uint8_t ydim;
  459. /** @brief The block Z dimension, in texels. */
  460. uint8_t zdim;
  461. /** @brief The block total texel count. */
  462. uint8_t texel_count;
  463. /**
  464. * @brief The number of stored decimation modes which are "always" modes.
  465. *
  466. * Always modes are stored at the start of the decimation_modes list.
  467. */
  468. unsigned int decimation_mode_count_always;
  469. /** @brief The number of stored decimation modes for selected encodings. */
  470. unsigned int decimation_mode_count_selected;
  471. /** @brief The number of stored decimation modes for any encoding. */
  472. unsigned int decimation_mode_count_all;
  473. /**
  474. * @brief The number of stored block modes which are "always" modes.
  475. *
  476. * Always modes are stored at the start of the block_modes list.
  477. */
  478. unsigned int block_mode_count_1plane_always;
  479. /** @brief The number of stored block modes for active 1 plane encodings. */
  480. unsigned int block_mode_count_1plane_selected;
  481. /** @brief The number of stored block modes for active 1 and 2 plane encodings. */
  482. unsigned int block_mode_count_1plane_2plane_selected;
  483. /** @brief The number of stored block modes for any encoding. */
  484. unsigned int block_mode_count_all;
  485. /** @brief The number of selected partitionings for 1/2/3/4 partitionings. */
  486. unsigned int partitioning_count_selected[BLOCK_MAX_PARTITIONS];
  487. /** @brief The number of partitionings for 1/2/3/4 partitionings. */
  488. unsigned int partitioning_count_all[BLOCK_MAX_PARTITIONS];
  489. /** @brief The active decimation modes, stored in low indices. */
  490. decimation_mode decimation_modes[WEIGHTS_MAX_DECIMATION_MODES];
  491. /** @brief The active decimation tables, stored in low indices. */
  492. alignas(ASTCENC_VECALIGN) decimation_info decimation_tables[WEIGHTS_MAX_DECIMATION_MODES];
  493. /** @brief The packed block mode array index, or @c BLOCK_BAD_BLOCK_MODE if not active. */
  494. uint16_t block_mode_packed_index[WEIGHTS_MAX_BLOCK_MODES];
  495. /** @brief The active block modes, stored in low indices. */
  496. block_mode block_modes[WEIGHTS_MAX_BLOCK_MODES];
  497. /** @brief The active partition tables, stored in low indices per-count. */
  498. partition_info partitionings[(3 * BLOCK_MAX_PARTITIONINGS) + 1];
  499. /**
  500. * @brief The packed partition table array index, or @c BLOCK_BAD_PARTITIONING if not active.
  501. *
  502. * Indexed by partition_count - 2, containing 2, 3 and 4 partitions.
  503. */
  504. uint16_t partitioning_packed_index[3][BLOCK_MAX_PARTITIONINGS];
  505. /** @brief The active texels for k-means partition selection. */
  506. uint8_t kmeans_texels[BLOCK_MAX_KMEANS_TEXELS];
  507. /**
  508. * @brief The canonical 2-partition coverage pattern used during block partition search.
  509. *
  510. * Indexed by remapped index, not physical index.
  511. */
  512. uint64_t coverage_bitmaps_2[BLOCK_MAX_PARTITIONINGS][2];
  513. /**
  514. * @brief The canonical 3-partition coverage pattern used during block partition search.
  515. *
  516. * Indexed by remapped index, not physical index.
  517. */
  518. uint64_t coverage_bitmaps_3[BLOCK_MAX_PARTITIONINGS][3];
  519. /**
  520. * @brief The canonical 4-partition coverage pattern used during block partition search.
  521. *
  522. * Indexed by remapped index, not physical index.
  523. */
  524. uint64_t coverage_bitmaps_4[BLOCK_MAX_PARTITIONINGS][4];
  525. /**
  526. * @brief Get the block mode structure for index @c block_mode.
  527. *
  528. * This function can only return block modes that are enabled by the current compressor config.
  529. * Decompression from an arbitrary source should not use this without first checking that the
  530. * packed block mode index is not @c BLOCK_BAD_BLOCK_MODE.
  531. *
  532. * @param block_mode The packed block mode index.
  533. *
  534. * @return The block mode structure.
  535. */
  536. const block_mode& get_block_mode(unsigned int block_mode) const
  537. {
  538. unsigned int packed_index = this->block_mode_packed_index[block_mode];
  539. assert(packed_index != BLOCK_BAD_BLOCK_MODE && packed_index < this->block_mode_count_all);
  540. return this->block_modes[packed_index];
  541. }
  542. /**
  543. * @brief Get the decimation mode structure for index @c decimation_mode.
  544. *
  545. * This function can only return decimation modes that are enabled by the current compressor
  546. * config. The mode array is stored packed, but this is only ever indexed by the packed index
  547. * stored in the @c block_mode and never exists in an unpacked form.
  548. *
  549. * @param decimation_mode The packed decimation mode index.
  550. *
  551. * @return The decimation mode structure.
  552. */
  553. const decimation_mode& get_decimation_mode(unsigned int decimation_mode) const
  554. {
  555. return this->decimation_modes[decimation_mode];
  556. }
  557. /**
  558. * @brief Get the decimation info structure for index @c decimation_mode.
  559. *
  560. * This function can only return decimation modes that are enabled by the current compressor
  561. * config. The mode array is stored packed, but this is only ever indexed by the packed index
  562. * stored in the @c block_mode and never exists in an unpacked form.
  563. *
  564. * @param decimation_mode The packed decimation mode index.
  565. *
  566. * @return The decimation info structure.
  567. */
  568. const decimation_info& get_decimation_info(unsigned int decimation_mode) const
  569. {
  570. return this->decimation_tables[decimation_mode];
  571. }
  572. /**
  573. * @brief Get the partition info table for a given partition count.
  574. *
  575. * @param partition_count The number of partitions we want the table for.
  576. *
  577. * @return The pointer to the table of 1024 entries (for 2/3/4 parts) or 1 entry (for 1 part).
  578. */
  579. const partition_info* get_partition_table(unsigned int partition_count) const
  580. {
  581. if (partition_count == 1)
  582. {
  583. partition_count = 5;
  584. }
  585. unsigned int index = (partition_count - 2) * BLOCK_MAX_PARTITIONINGS;
  586. return this->partitionings + index;
  587. }
  588. /**
  589. * @brief Get the partition info structure for a given partition count and seed.
  590. *
  591. * @param partition_count The number of partitions we want the info for.
  592. * @param index The partition seed (between 0 and 1023).
  593. *
  594. * @return The partition info structure.
  595. */
  596. const partition_info& get_partition_info(unsigned int partition_count, unsigned int index) const
  597. {
  598. unsigned int packed_index = 0;
  599. if (partition_count >= 2)
  600. {
  601. packed_index = this->partitioning_packed_index[partition_count - 2][index];
  602. }
  603. assert(packed_index != BLOCK_BAD_PARTITIONING && packed_index < this->partitioning_count_all[partition_count - 1]);
  604. auto& result = get_partition_table(partition_count)[packed_index];
  605. assert(index == result.partition_index);
  606. return result;
  607. }
  608. /**
  609. * @brief Get the partition info structure for a given partition count and seed.
  610. *
  611. * @param partition_count The number of partitions we want the info for.
  612. * @param packed_index The raw array offset.
  613. *
  614. * @return The partition info structure.
  615. */
  616. const partition_info& get_raw_partition_info(unsigned int partition_count, unsigned int packed_index) const
  617. {
  618. assert(packed_index != BLOCK_BAD_PARTITIONING && packed_index < this->partitioning_count_all[partition_count - 1]);
  619. auto& result = get_partition_table(partition_count)[packed_index];
  620. return result;
  621. }
  622. };
  623. /**
  624. * @brief The image data for a single block.
  625. *
  626. * The @c data_[rgba] fields store the image data in an encoded SoA float form designed for easy
  627. * vectorization. Input data is converted to float and stored as values between 0 and 65535. LDR
  628. * data is stored as direct UNORM data, HDR data is stored as LNS data.
  629. *
  630. * The @c rgb_lns and @c alpha_lns fields that assigned a per-texel use of HDR are only used during
  631. * decompression. The current compressor will always use HDR endpoint formats when in HDR mode.
  632. */
  633. struct image_block
  634. {
  635. /** @brief The input (compress) or output (decompress) data for the red color component. */
  636. alignas(ASTCENC_VECALIGN) float data_r[BLOCK_MAX_TEXELS];
  637. /** @brief The input (compress) or output (decompress) data for the green color component. */
  638. alignas(ASTCENC_VECALIGN) float data_g[BLOCK_MAX_TEXELS];
  639. /** @brief The input (compress) or output (decompress) data for the blue color component. */
  640. alignas(ASTCENC_VECALIGN) float data_b[BLOCK_MAX_TEXELS];
  641. /** @brief The input (compress) or output (decompress) data for the alpha color component. */
  642. alignas(ASTCENC_VECALIGN) float data_a[BLOCK_MAX_TEXELS];
  643. /** @brief The number of texels in the block. */
  644. uint8_t texel_count;
  645. /** @brief The original data for texel 0 for constant color block encoding. */
  646. vfloat4 origin_texel;
  647. /** @brief The min component value of all texels in the block. */
  648. vfloat4 data_min;
  649. /** @brief The mean component value of all texels in the block. */
  650. vfloat4 data_mean;
  651. /** @brief The max component value of all texels in the block. */
  652. vfloat4 data_max;
  653. /** @brief The relative error significance of the color channels. */
  654. vfloat4 channel_weight;
  655. /** @brief Is this grayscale block where R == G == B for all texels? */
  656. bool grayscale;
  657. /** @brief Set to 1 if a texel is using HDR RGB endpoints (decompression only). */
  658. uint8_t rgb_lns[BLOCK_MAX_TEXELS];
  659. /** @brief Set to 1 if a texel is using HDR alpha endpoints (decompression only). */
  660. uint8_t alpha_lns[BLOCK_MAX_TEXELS];
  661. /** @brief The X position of this block in the input or output image. */
  662. unsigned int xpos;
  663. /** @brief The Y position of this block in the input or output image. */
  664. unsigned int ypos;
  665. /** @brief The Z position of this block in the input or output image. */
  666. unsigned int zpos;
  667. /**
  668. * @brief Get an RGBA texel value from the data.
  669. *
  670. * @param index The texel index.
  671. *
  672. * @return The texel in RGBA component ordering.
  673. */
  674. inline vfloat4 texel(unsigned int index) const
  675. {
  676. return vfloat4(data_r[index],
  677. data_g[index],
  678. data_b[index],
  679. data_a[index]);
  680. }
  681. /**
  682. * @brief Get an RGB texel value from the data.
  683. *
  684. * @param index The texel index.
  685. *
  686. * @return The texel in RGB0 component ordering.
  687. */
  688. inline vfloat4 texel3(unsigned int index) const
  689. {
  690. return vfloat3(data_r[index],
  691. data_g[index],
  692. data_b[index]);
  693. }
  694. /**
  695. * @brief Get the default alpha value for endpoints that don't store it.
  696. *
  697. * The default depends on whether the alpha endpoint is LDR or HDR.
  698. *
  699. * @return The alpha value in the scaled range used by the compressor.
  700. */
  701. inline float get_default_alpha() const
  702. {
  703. return this->alpha_lns[0] ? static_cast<float>(0x7800) : static_cast<float>(0xFFFF);
  704. }
  705. /**
  706. * @brief Test if a single color channel is constant across the block.
  707. *
  708. * Constant color channels are easier to compress as interpolating between two identical colors
  709. * always returns the same value, irrespective of the weight used. They therefore can be ignored
  710. * for the purposes of weight selection and use of a second weight plane.
  711. *
  712. * @return @c true if the channel is constant across the block, @c false otherwise.
  713. */
  714. inline bool is_constant_channel(int channel) const
  715. {
  716. vmask4 lane_mask = vint4::lane_id() == vint4(channel);
  717. vmask4 color_mask = this->data_min == this->data_max;
  718. return any(lane_mask & color_mask);
  719. }
  720. /**
  721. * @brief Test if this block is a luminance block with constant 1.0 alpha.
  722. *
  723. * @return @c true if the block is a luminance block , @c false otherwise.
  724. */
  725. inline bool is_luminance() const
  726. {
  727. float default_alpha = this->get_default_alpha();
  728. bool alpha1 = (this->data_min.lane<3>() == default_alpha) &&
  729. (this->data_max.lane<3>() == default_alpha);
  730. return this->grayscale && alpha1;
  731. }
  732. /**
  733. * @brief Test if this block is a luminance block with variable alpha.
  734. *
  735. * @return @c true if the block is a luminance + alpha block , @c false otherwise.
  736. */
  737. inline bool is_luminancealpha() const
  738. {
  739. float default_alpha = this->get_default_alpha();
  740. bool alpha1 = (this->data_min.lane<3>() == default_alpha) &&
  741. (this->data_max.lane<3>() == default_alpha);
  742. return this->grayscale && !alpha1;
  743. }
  744. };
  745. /**
  746. * @brief Data structure storing the color endpoints for a block.
  747. */
  748. struct endpoints
  749. {
  750. /** @brief The number of partition endpoints stored. */
  751. unsigned int partition_count;
  752. /** @brief The colors for endpoint 0. */
  753. vfloat4 endpt0[BLOCK_MAX_PARTITIONS];
  754. /** @brief The colors for endpoint 1. */
  755. vfloat4 endpt1[BLOCK_MAX_PARTITIONS];
  756. };
  757. /**
  758. * @brief Data structure storing the color endpoints and weights.
  759. */
  760. struct endpoints_and_weights
  761. {
  762. /** @brief True if all active values in weight_error_scale are the same. */
  763. bool is_constant_weight_error_scale;
  764. /** @brief The color endpoints. */
  765. endpoints ep;
  766. /** @brief The ideal weight for each texel; may be undecimated or decimated. */
  767. alignas(ASTCENC_VECALIGN) float weights[BLOCK_MAX_TEXELS];
  768. /** @brief The ideal weight error scaling for each texel; may be undecimated or decimated. */
  769. alignas(ASTCENC_VECALIGN) float weight_error_scale[BLOCK_MAX_TEXELS];
  770. };
  771. /**
  772. * @brief Utility storing estimated errors from choosing particular endpoint encodings.
  773. */
  774. struct encoding_choice_errors
  775. {
  776. /** @brief Error of using LDR RGB-scale instead of complete endpoints. */
  777. float rgb_scale_error;
  778. /** @brief Error of using HDR RGB-scale instead of complete endpoints. */
  779. float rgb_luma_error;
  780. /** @brief Error of using luminance instead of RGB. */
  781. float luminance_error;
  782. /** @brief Error of discarding alpha and using a constant 1.0 alpha. */
  783. float alpha_drop_error;
  784. /** @brief Can we use delta offset encoding? */
  785. bool can_offset_encode;
  786. /** @brief Can we use blue contraction encoding? */
  787. bool can_blue_contract;
  788. };
  789. /**
  790. * @brief Preallocated working buffers, allocated per thread during context creation.
  791. */
  792. struct alignas(ASTCENC_VECALIGN) compression_working_buffers
  793. {
  794. /** @brief Ideal endpoints and weights for plane 1. */
  795. endpoints_and_weights ei1;
  796. /** @brief Ideal endpoints and weights for plane 2. */
  797. endpoints_and_weights ei2;
  798. /**
  799. * @brief Decimated ideal weight values in the ~0-1 range.
  800. *
  801. * Note that values can be slightly below zero or higher than one due to
  802. * endpoint extents being inside the ideal color representation.
  803. *
  804. * For two planes, second plane starts at @c WEIGHTS_PLANE2_OFFSET offsets.
  805. */
  806. alignas(ASTCENC_VECALIGN) float dec_weights_ideal[WEIGHTS_MAX_DECIMATION_MODES * BLOCK_MAX_WEIGHTS];
  807. /**
  808. * @brief Decimated quantized weight values in the unquantized 0-64 range.
  809. *
  810. * For two planes, second plane starts at @c WEIGHTS_PLANE2_OFFSET offsets.
  811. */
  812. uint8_t dec_weights_uquant[WEIGHTS_MAX_BLOCK_MODES * BLOCK_MAX_WEIGHTS];
  813. /** @brief Error of the best encoding combination for each block mode. */
  814. alignas(ASTCENC_VECALIGN) float errors_of_best_combination[WEIGHTS_MAX_BLOCK_MODES];
  815. /** @brief The best color quant for each block mode. */
  816. uint8_t best_quant_levels[WEIGHTS_MAX_BLOCK_MODES];
  817. /** @brief The best color quant for each block mode if modes are the same and we have spare bits. */
  818. uint8_t best_quant_levels_mod[WEIGHTS_MAX_BLOCK_MODES];
  819. /** @brief The best endpoint format for each partition. */
  820. uint8_t best_ep_formats[WEIGHTS_MAX_BLOCK_MODES][BLOCK_MAX_PARTITIONS];
  821. /** @brief The total bit storage needed for quantized weights for each block mode. */
  822. int8_t qwt_bitcounts[WEIGHTS_MAX_BLOCK_MODES];
  823. /** @brief The cumulative error for quantized weights for each block mode. */
  824. float qwt_errors[WEIGHTS_MAX_BLOCK_MODES];
  825. /** @brief The low weight value in plane 1 for each block mode. */
  826. float weight_low_value1[WEIGHTS_MAX_BLOCK_MODES];
  827. /** @brief The high weight value in plane 1 for each block mode. */
  828. float weight_high_value1[WEIGHTS_MAX_BLOCK_MODES];
  829. /** @brief The low weight value in plane 1 for each quant level and decimation mode. */
  830. float weight_low_values1[WEIGHTS_MAX_DECIMATION_MODES][TUNE_MAX_ANGULAR_QUANT + 1];
  831. /** @brief The high weight value in plane 1 for each quant level and decimation mode. */
  832. float weight_high_values1[WEIGHTS_MAX_DECIMATION_MODES][TUNE_MAX_ANGULAR_QUANT + 1];
  833. /** @brief The low weight value in plane 2 for each block mode. */
  834. float weight_low_value2[WEIGHTS_MAX_BLOCK_MODES];
  835. /** @brief The high weight value in plane 2 for each block mode. */
  836. float weight_high_value2[WEIGHTS_MAX_BLOCK_MODES];
  837. /** @brief The low weight value in plane 2 for each quant level and decimation mode. */
  838. float weight_low_values2[WEIGHTS_MAX_DECIMATION_MODES][TUNE_MAX_ANGULAR_QUANT + 1];
  839. /** @brief The high weight value in plane 2 for each quant level and decimation mode. */
  840. float weight_high_values2[WEIGHTS_MAX_DECIMATION_MODES][TUNE_MAX_ANGULAR_QUANT + 1];
  841. };
  842. struct dt_init_working_buffers
  843. {
  844. uint8_t weight_count_of_texel[BLOCK_MAX_TEXELS];
  845. uint8_t grid_weights_of_texel[BLOCK_MAX_TEXELS][4];
  846. uint8_t weights_of_texel[BLOCK_MAX_TEXELS][4];
  847. uint8_t texel_count_of_weight[BLOCK_MAX_WEIGHTS];
  848. uint8_t texels_of_weight[BLOCK_MAX_WEIGHTS][BLOCK_MAX_TEXELS];
  849. uint8_t texel_weights_of_weight[BLOCK_MAX_WEIGHTS][BLOCK_MAX_TEXELS];
  850. };
  851. /**
  852. * @brief Weight quantization transfer table.
  853. *
  854. * ASTC can store texel weights at many quantization levels, so for performance we store essential
  855. * information about each level as a precomputed data structure. Unquantized weights are integers
  856. * or floats in the range [0, 64].
  857. *
  858. * This structure provides a table, used to estimate the closest quantized weight for a given
  859. * floating-point weight. For each quantized weight, the corresponding unquantized values. For each
  860. * quantized weight, a previous-value and a next-value.
  861. */
  862. struct quant_and_transfer_table
  863. {
  864. /** @brief The unscrambled unquantized value. */
  865. int8_t quant_to_unquant[32];
  866. /** @brief The scrambling order: scrambled_quant = map[unscrambled_quant]. */
  867. int8_t scramble_map[32];
  868. /** @brief The unscrambling order: unscrambled_unquant = map[scrambled_quant]. */
  869. int8_t unscramble_and_unquant_map[32];
  870. /**
  871. * @brief A table of previous-and-next weights, indexed by the current unquantized value.
  872. * * bits 7:0 = previous-index, unquantized
  873. * * bits 15:8 = next-index, unquantized
  874. */
  875. uint16_t prev_next_values[65];
  876. };
  877. /** @brief The precomputed quant and transfer table. */
  878. extern const quant_and_transfer_table quant_and_xfer_tables[12];
  879. /** @brief The block is an error block, and will return error color or NaN. */
  880. static constexpr uint8_t SYM_BTYPE_ERROR { 0 };
  881. /** @brief The block is a constant color block using FP16 colors. */
  882. static constexpr uint8_t SYM_BTYPE_CONST_F16 { 1 };
  883. /** @brief The block is a constant color block using UNORM16 colors. */
  884. static constexpr uint8_t SYM_BTYPE_CONST_U16 { 2 };
  885. /** @brief The block is a normal non-constant color block. */
  886. static constexpr uint8_t SYM_BTYPE_NONCONST { 3 };
  887. /**
  888. * @brief A symbolic representation of a compressed block.
  889. *
  890. * The symbolic representation stores the unpacked content of a single
  891. * @c physical_compressed_block, in a form which is much easier to access for
  892. * the rest of the compressor code.
  893. */
  894. struct symbolic_compressed_block
  895. {
  896. /** @brief The block type, one of the @c SYM_BTYPE_* constants. */
  897. uint8_t block_type;
  898. /** @brief The number of partitions; valid for @c NONCONST blocks. */
  899. uint8_t partition_count;
  900. /** @brief Non-zero if the color formats matched; valid for @c NONCONST blocks. */
  901. uint8_t color_formats_matched;
  902. /** @brief The plane 2 color component, or -1 if single plane; valid for @c NONCONST blocks. */
  903. int8_t plane2_component;
  904. /** @brief The block mode; valid for @c NONCONST blocks. */
  905. uint16_t block_mode;
  906. /** @brief The partition index; valid for @c NONCONST blocks if 2 or more partitions. */
  907. uint16_t partition_index;
  908. /** @brief The endpoint color formats for each partition; valid for @c NONCONST blocks. */
  909. uint8_t color_formats[BLOCK_MAX_PARTITIONS];
  910. /** @brief The endpoint color quant mode; valid for @c NONCONST blocks. */
  911. quant_method quant_mode;
  912. /** @brief The error of the current encoding; valid for @c NONCONST blocks. */
  913. float errorval;
  914. // We can't have both of these at the same time
  915. union {
  916. /** @brief The constant color; valid for @c CONST blocks. */
  917. int constant_color[BLOCK_MAX_COMPONENTS];
  918. /** @brief The quantized endpoint color pairs; valid for @c NONCONST blocks. */
  919. uint8_t color_values[BLOCK_MAX_PARTITIONS][8];
  920. };
  921. /** @brief The quantized and decimated weights.
  922. *
  923. * Weights are stored in the 0-64 unpacked range allowing them to be used
  924. * directly in encoding passes without per-use unpacking. Packing happens
  925. * when converting to/from the physical bitstream encoding.
  926. *
  927. * If dual plane, the second plane starts at @c weights[WEIGHTS_PLANE2_OFFSET].
  928. */
  929. uint8_t weights[BLOCK_MAX_WEIGHTS];
  930. /**
  931. * @brief Get the weight quantization used by this block mode.
  932. *
  933. * @return The quantization level.
  934. */
  935. inline quant_method get_color_quant_mode() const
  936. {
  937. return this->quant_mode;
  938. }
  939. };
  940. /**
  941. * @brief A physical representation of a compressed block.
  942. *
  943. * The physical representation stores the raw bytes of the format in memory.
  944. */
  945. struct physical_compressed_block
  946. {
  947. /** @brief The ASTC encoded data for a single block. */
  948. uint8_t data[16];
  949. };
  950. /**
  951. * @brief Parameter structure for @c compute_pixel_region_variance().
  952. *
  953. * This function takes a structure to avoid spilling arguments to the stack on every function
  954. * invocation, as there are a lot of parameters.
  955. */
  956. struct pixel_region_args
  957. {
  958. /** @brief The image to analyze. */
  959. const astcenc_image* img;
  960. /** @brief The component swizzle pattern. */
  961. astcenc_swizzle swz;
  962. /** @brief Should the algorithm bother with Z axis processing? */
  963. bool have_z;
  964. /** @brief The kernel radius for alpha processing. */
  965. unsigned int alpha_kernel_radius;
  966. /** @brief The X dimension of the working data to process. */
  967. unsigned int size_x;
  968. /** @brief The Y dimension of the working data to process. */
  969. unsigned int size_y;
  970. /** @brief The Z dimension of the working data to process. */
  971. unsigned int size_z;
  972. /** @brief The X position of first src and dst data in the data set. */
  973. unsigned int offset_x;
  974. /** @brief The Y position of first src and dst data in the data set. */
  975. unsigned int offset_y;
  976. /** @brief The Z position of first src and dst data in the data set. */
  977. unsigned int offset_z;
  978. /** @brief The working memory buffer. */
  979. vfloat4 *work_memory;
  980. };
  981. /**
  982. * @brief Parameter structure for @c compute_averages_proc().
  983. */
  984. struct avg_args
  985. {
  986. /** @brief The arguments for the nested variance computation. */
  987. pixel_region_args arg;
  988. /** @brief The image X dimensions. */
  989. unsigned int img_size_x;
  990. /** @brief The image Y dimensions. */
  991. unsigned int img_size_y;
  992. /** @brief The image Z dimensions. */
  993. unsigned int img_size_z;
  994. /** @brief The maximum working block dimensions in X and Y dimensions. */
  995. unsigned int blk_size_xy;
  996. /** @brief The maximum working block dimensions in Z dimensions. */
  997. unsigned int blk_size_z;
  998. /** @brief The working block memory size. */
  999. unsigned int work_memory_size;
  1000. };
  1001. #if defined(ASTCENC_DIAGNOSTICS)
  1002. /* See astcenc_diagnostic_trace header for details. */
  1003. class TraceLog;
  1004. #endif
  1005. /**
  1006. * @brief The astcenc compression context.
  1007. */
  1008. struct astcenc_contexti
  1009. {
  1010. /** @brief The configuration this context was created with. */
  1011. astcenc_config config;
  1012. /** @brief The thread count supported by this context. */
  1013. unsigned int thread_count;
  1014. /** @brief The block size descriptor this context was created with. */
  1015. block_size_descriptor* bsd;
  1016. /*
  1017. * Fields below here are not needed in a decompress-only build, but some remain as they are
  1018. * small and it avoids littering the code with #ifdefs. The most significant contributors to
  1019. * large structure size are omitted.
  1020. */
  1021. /** @brief The input image alpha channel averages table, may be @c nullptr if not needed. */
  1022. float* input_alpha_averages;
  1023. /** @brief The scratch working buffers, one per thread (see @c thread_count). */
  1024. compression_working_buffers* working_buffers;
  1025. #if !defined(ASTCENC_DECOMPRESS_ONLY)
  1026. /** @brief The pixel region and variance worker arguments. */
  1027. avg_args avg_preprocess_args;
  1028. #endif
  1029. #if defined(ASTCENC_DIAGNOSTICS)
  1030. /**
  1031. * @brief The diagnostic trace logger.
  1032. *
  1033. * Note that this is a singleton, so can only be used in single threaded mode. It only exists
  1034. * here so we have a reference to close the file at the end of the capture.
  1035. */
  1036. TraceLog* trace_log;
  1037. #endif
  1038. };
  1039. /* ============================================================================
  1040. Functionality for managing block sizes and partition tables.
  1041. ============================================================================ */
  1042. /**
  1043. * @brief Populate the block size descriptor for the target block size.
  1044. *
  1045. * This will also initialize the partition table metadata, which is stored as part of the BSD
  1046. * structure.
  1047. *
  1048. * @param x_texels The number of texels in the block X dimension.
  1049. * @param y_texels The number of texels in the block Y dimension.
  1050. * @param z_texels The number of texels in the block Z dimension.
  1051. * @param can_omit_modes Can we discard modes and partitionings that astcenc won't use?
  1052. * @param partition_count_cutoff The partition count cutoff to use, if we can omit partitionings.
  1053. * @param mode_cutoff The block mode percentile cutoff [0-1].
  1054. * @param[out] bsd The descriptor to initialize.
  1055. */
  1056. void init_block_size_descriptor(
  1057. unsigned int x_texels,
  1058. unsigned int y_texels,
  1059. unsigned int z_texels,
  1060. bool can_omit_modes,
  1061. unsigned int partition_count_cutoff,
  1062. float mode_cutoff,
  1063. block_size_descriptor& bsd);
  1064. /**
  1065. * @brief Populate the partition tables for the target block size.
  1066. *
  1067. * Note the @c bsd descriptor must be initialized by calling @c init_block_size_descriptor() before
  1068. * calling this function.
  1069. *
  1070. * @param[out] bsd The block size information structure to populate.
  1071. * @param can_omit_partitionings True if we can we drop partitionings that astcenc won't use.
  1072. * @param partition_count_cutoff The partition count cutoff to use, if we can omit partitionings.
  1073. */
  1074. void init_partition_tables(
  1075. block_size_descriptor& bsd,
  1076. bool can_omit_partitionings,
  1077. unsigned int partition_count_cutoff);
  1078. /**
  1079. * @brief Get the percentile table for 2D block modes.
  1080. *
  1081. * This is an empirically determined prioritization of which block modes to use in the search in
  1082. * terms of their centile (lower centiles = more useful).
  1083. *
  1084. * Returns a dynamically allocated array; caller must free with delete[].
  1085. *
  1086. * @param xdim The block x size.
  1087. * @param ydim The block y size.
  1088. *
  1089. * @return The unpacked table.
  1090. */
  1091. const float* get_2d_percentile_table(
  1092. unsigned int xdim,
  1093. unsigned int ydim);
  1094. /**
  1095. * @brief Query if a 2D block size is legal.
  1096. *
  1097. * @return True if legal, false otherwise.
  1098. */
  1099. bool is_legal_2d_block_size(
  1100. unsigned int xdim,
  1101. unsigned int ydim);
  1102. /**
  1103. * @brief Query if a 3D block size is legal.
  1104. *
  1105. * @return True if legal, false otherwise.
  1106. */
  1107. bool is_legal_3d_block_size(
  1108. unsigned int xdim,
  1109. unsigned int ydim,
  1110. unsigned int zdim);
  1111. /* ============================================================================
  1112. Functionality for managing BISE quantization and unquantization.
  1113. ============================================================================ */
  1114. /**
  1115. * @brief The precomputed table for quantizing color values.
  1116. *
  1117. * Converts unquant value in 0-255 range into quant value in 0-255 range.
  1118. * No BISE scrambling is applied at this stage.
  1119. *
  1120. * The BISE encoding results in ties where available quant<256> values are
  1121. * equidistant the available quant<BISE> values. This table stores two values
  1122. * for each input - one for use with a negative residual, and one for use with
  1123. * a positive residual.
  1124. *
  1125. * Indexed by [quant_mode - 4][data_value * 2 + residual].
  1126. */
  1127. extern const uint8_t color_unquant_to_uquant_tables[17][512];
  1128. /**
  1129. * @brief The precomputed table for packing quantized color values.
  1130. *
  1131. * Converts quant value in 0-255 range into packed quant value in 0-N range,
  1132. * with BISE scrambling applied.
  1133. *
  1134. * Indexed by [quant_mode - 4][data_value].
  1135. */
  1136. extern const uint8_t color_uquant_to_scrambled_pquant_tables[17][256];
  1137. /**
  1138. * @brief The precomputed table for unpacking color values.
  1139. *
  1140. * Converts quant value in 0-N range into unpacked value in 0-255 range,
  1141. * with BISE unscrambling applied.
  1142. *
  1143. * Indexed by [quant_mode - 4][data_value].
  1144. */
  1145. extern const uint8_t* color_scrambled_pquant_to_uquant_tables[17];
  1146. /**
  1147. * @brief The precomputed quant mode storage table.
  1148. *
  1149. * Indexing by [integer_count/2][bits] gives us the quantization level for a given integer count and
  1150. * number of compressed storage bits. Returns -1 for cases where the requested integer count cannot
  1151. * ever fit in the supplied storage size.
  1152. */
  1153. extern const int8_t quant_mode_table[10][128];
  1154. /**
  1155. * @brief Encode a packed string using BISE.
  1156. *
  1157. * Note that BISE can return strings that are not a whole number of bytes in length, and ASTC can
  1158. * start storing strings in a block at arbitrary bit offsets in the encoded data.
  1159. *
  1160. * @param quant_level The BISE alphabet size.
  1161. * @param character_count The number of characters in the string.
  1162. * @param input_data The unpacked string, one byte per character.
  1163. * @param[in,out] output_data The output packed string.
  1164. * @param bit_offset The starting offset in the output storage.
  1165. */
  1166. void encode_ise(
  1167. quant_method quant_level,
  1168. unsigned int character_count,
  1169. const uint8_t* input_data,
  1170. uint8_t* output_data,
  1171. unsigned int bit_offset);
  1172. /**
  1173. * @brief Decode a packed string using BISE.
  1174. *
  1175. * Note that BISE input strings are not a whole number of bytes in length, and ASTC can start
  1176. * strings at arbitrary bit offsets in the encoded data.
  1177. *
  1178. * @param quant_level The BISE alphabet size.
  1179. * @param character_count The number of characters in the string.
  1180. * @param input_data The packed string.
  1181. * @param[in,out] output_data The output storage, one byte per character.
  1182. * @param bit_offset The starting offset in the output storage.
  1183. */
  1184. void decode_ise(
  1185. quant_method quant_level,
  1186. unsigned int character_count,
  1187. const uint8_t* input_data,
  1188. uint8_t* output_data,
  1189. unsigned int bit_offset);
  1190. /**
  1191. * @brief Return the number of bits needed to encode an ISE sequence.
  1192. *
  1193. * This implementation assumes that the @c quant level is untrusted, given it may come from random
  1194. * data being decompressed, so we return an arbitrary unencodable size if that is the case.
  1195. *
  1196. * @param character_count The number of items in the sequence.
  1197. * @param quant_level The desired quantization level.
  1198. *
  1199. * @return The number of bits needed to encode the BISE string.
  1200. */
  1201. unsigned int get_ise_sequence_bitcount(
  1202. unsigned int character_count,
  1203. quant_method quant_level);
  1204. /* ============================================================================
  1205. Functionality for managing color partitioning.
  1206. ============================================================================ */
  1207. /**
  1208. * @brief Compute averages and dominant directions for each partition in a 2 component texture.
  1209. *
  1210. * @param pi The partition info for the current trial.
  1211. * @param blk The image block color data to be compressed.
  1212. * @param component1 The first component included in the analysis.
  1213. * @param component2 The second component included in the analysis.
  1214. * @param[out] pm The output partition metrics.
  1215. * - Only pi.partition_count array entries actually get initialized.
  1216. * - Direction vectors @c pm.dir are not normalized.
  1217. */
  1218. void compute_avgs_and_dirs_2_comp(
  1219. const partition_info& pi,
  1220. const image_block& blk,
  1221. unsigned int component1,
  1222. unsigned int component2,
  1223. partition_metrics pm[BLOCK_MAX_PARTITIONS]);
  1224. /**
  1225. * @brief Compute averages and dominant directions for each partition in a 3 component texture.
  1226. *
  1227. * @param pi The partition info for the current trial.
  1228. * @param blk The image block color data to be compressed.
  1229. * @param omitted_component The component excluded from the analysis.
  1230. * @param[out] pm The output partition metrics.
  1231. * - Only pi.partition_count array entries actually get initialized.
  1232. * - Direction vectors @c pm.dir are not normalized.
  1233. */
  1234. void compute_avgs_and_dirs_3_comp(
  1235. const partition_info& pi,
  1236. const image_block& blk,
  1237. unsigned int omitted_component,
  1238. partition_metrics pm[BLOCK_MAX_PARTITIONS]);
  1239. /**
  1240. * @brief Compute averages and dominant directions for each partition in a 3 component texture.
  1241. *
  1242. * This is a specialization of @c compute_avgs_and_dirs_3_comp where the omitted component is
  1243. * always alpha, a common case during partition search.
  1244. *
  1245. * @param pi The partition info for the current trial.
  1246. * @param blk The image block color data to be compressed.
  1247. * @param[out] pm The output partition metrics.
  1248. * - Only pi.partition_count array entries actually get initialized.
  1249. * - Direction vectors @c pm.dir are not normalized.
  1250. */
  1251. void compute_avgs_and_dirs_3_comp_rgb(
  1252. const partition_info& pi,
  1253. const image_block& blk,
  1254. partition_metrics pm[BLOCK_MAX_PARTITIONS]);
  1255. /**
  1256. * @brief Compute averages and dominant directions for each partition in a 4 component texture.
  1257. *
  1258. * @param pi The partition info for the current trial.
  1259. * @param blk The image block color data to be compressed.
  1260. * @param[out] pm The output partition metrics.
  1261. * - Only pi.partition_count array entries actually get initialized.
  1262. * - Direction vectors @c pm.dir are not normalized.
  1263. */
  1264. void compute_avgs_and_dirs_4_comp(
  1265. const partition_info& pi,
  1266. const image_block& blk,
  1267. partition_metrics pm[BLOCK_MAX_PARTITIONS]);
  1268. /**
  1269. * @brief Compute the RGB error for uncorrelated and same chroma projections.
  1270. *
  1271. * The output of compute averages and dirs is post processed to define two lines, both of which go
  1272. * through the mean-color-value. One line has a direction defined by the dominant direction; this
  1273. * is used to assess the error from using an uncorrelated color representation. The other line goes
  1274. * through (0,0,0) and is used to assess the error from using an RGBS color representation.
  1275. *
  1276. * This function computes the squared error when using these two representations.
  1277. *
  1278. * @param pi The partition info for the current trial.
  1279. * @param blk The image block color data to be compressed.
  1280. * @param[in,out] plines Processed line inputs, and line length outputs.
  1281. * @param[out] uncor_error The cumulative error for using the uncorrelated line.
  1282. * @param[out] samec_error The cumulative error for using the same chroma line.
  1283. */
  1284. void compute_error_squared_rgb(
  1285. const partition_info& pi,
  1286. const image_block& blk,
  1287. partition_lines3 plines[BLOCK_MAX_PARTITIONS],
  1288. float& uncor_error,
  1289. float& samec_error);
  1290. /**
  1291. * @brief Compute the RGBA error for uncorrelated and same chroma projections.
  1292. *
  1293. * The output of compute averages and dirs is post processed to define two lines, both of which go
  1294. * through the mean-color-value. One line has a direction defined by the dominant direction; this
  1295. * is used to assess the error from using an uncorrelated color representation. The other line goes
  1296. * through (0,0,0,1) and is used to assess the error from using an RGBS color representation.
  1297. *
  1298. * This function computes the squared error when using these two representations.
  1299. *
  1300. * @param pi The partition info for the current trial.
  1301. * @param blk The image block color data to be compressed.
  1302. * @param uncor_plines Processed uncorrelated partition lines for each partition.
  1303. * @param samec_plines Processed same chroma partition lines for each partition.
  1304. * @param[out] line_lengths The length of each components deviation from the line.
  1305. * @param[out] uncor_error The cumulative error for using the uncorrelated line.
  1306. * @param[out] samec_error The cumulative error for using the same chroma line.
  1307. */
  1308. void compute_error_squared_rgba(
  1309. const partition_info& pi,
  1310. const image_block& blk,
  1311. const processed_line4 uncor_plines[BLOCK_MAX_PARTITIONS],
  1312. const processed_line4 samec_plines[BLOCK_MAX_PARTITIONS],
  1313. float line_lengths[BLOCK_MAX_PARTITIONS],
  1314. float& uncor_error,
  1315. float& samec_error);
  1316. /**
  1317. * @brief Find the best set of partitions to trial for a given block.
  1318. *
  1319. * On return the @c best_partitions list will contain the two best partition
  1320. * candidates; one assuming data has uncorrelated chroma and one assuming the
  1321. * data has correlated chroma. The best candidate is returned first in the list.
  1322. *
  1323. * @param bsd The block size information.
  1324. * @param blk The image block color data to compress.
  1325. * @param partition_count The number of partitions in the block.
  1326. * @param partition_search_limit The number of candidate partition encodings to trial.
  1327. * @param[out] best_partitions The best partition candidates.
  1328. * @param requested_candidates The number of requested partitionings. May return fewer if
  1329. * candidates are not available.
  1330. *
  1331. * @return The actual number of candidates returned.
  1332. */
  1333. unsigned int find_best_partition_candidates(
  1334. const block_size_descriptor& bsd,
  1335. const image_block& blk,
  1336. unsigned int partition_count,
  1337. unsigned int partition_search_limit,
  1338. unsigned int best_partitions[TUNE_MAX_PARTITIONING_CANDIDATES],
  1339. unsigned int requested_candidates);
  1340. /* ============================================================================
  1341. Functionality for managing images and image related data.
  1342. ============================================================================ */
  1343. /**
  1344. * @brief Setup computation of regional averages in an image.
  1345. *
  1346. * This must be done by only a single thread per image, before any thread calls
  1347. * @c compute_averages().
  1348. *
  1349. * Results are written back into @c img->input_alpha_averages.
  1350. *
  1351. * @param img The input image data, also holds output data.
  1352. * @param alpha_kernel_radius The kernel radius (in pixels) for alpha mods.
  1353. * @param swz Input data component swizzle.
  1354. * @param[out] ag The average variance arguments to init.
  1355. *
  1356. * @return The number of tasks in the processing stage.
  1357. */
  1358. unsigned int init_compute_averages(
  1359. const astcenc_image& img,
  1360. unsigned int alpha_kernel_radius,
  1361. const astcenc_swizzle& swz,
  1362. avg_args& ag);
  1363. /**
  1364. * @brief Compute averages for a pixel region.
  1365. *
  1366. * The routine computes both in a single pass, using a summed-area table to decouple the running
  1367. * time from the averaging/variance kernel size.
  1368. *
  1369. * @param[out] ctx The compressor context storing the output data.
  1370. * @param arg The input parameter structure.
  1371. */
  1372. void compute_pixel_region_variance(
  1373. astcenc_contexti& ctx,
  1374. const pixel_region_args& arg);
  1375. /**
  1376. * @brief Load a single image block from the input image.
  1377. *
  1378. * @param decode_mode The compression color profile.
  1379. * @param img The input image data.
  1380. * @param[out] blk The image block to populate.
  1381. * @param bsd The block size information.
  1382. * @param xpos The block X coordinate in the input image.
  1383. * @param ypos The block Y coordinate in the input image.
  1384. * @param zpos The block Z coordinate in the input image.
  1385. * @param swz The swizzle to apply on load.
  1386. */
  1387. void load_image_block(
  1388. astcenc_profile decode_mode,
  1389. const astcenc_image& img,
  1390. image_block& blk,
  1391. const block_size_descriptor& bsd,
  1392. unsigned int xpos,
  1393. unsigned int ypos,
  1394. unsigned int zpos,
  1395. const astcenc_swizzle& swz);
  1396. /**
  1397. * @brief Load a single image block from the input image.
  1398. *
  1399. * This specialized variant can be used only if the block is 2D LDR U8 data,
  1400. * with no swizzle.
  1401. *
  1402. * @param decode_mode The compression color profile.
  1403. * @param img The input image data.
  1404. * @param[out] blk The image block to populate.
  1405. * @param bsd The block size information.
  1406. * @param xpos The block X coordinate in the input image.
  1407. * @param ypos The block Y coordinate in the input image.
  1408. * @param zpos The block Z coordinate in the input image.
  1409. * @param swz The swizzle to apply on load.
  1410. */
  1411. void load_image_block_fast_ldr(
  1412. astcenc_profile decode_mode,
  1413. const astcenc_image& img,
  1414. image_block& blk,
  1415. const block_size_descriptor& bsd,
  1416. unsigned int xpos,
  1417. unsigned int ypos,
  1418. unsigned int zpos,
  1419. const astcenc_swizzle& swz);
  1420. /**
  1421. * @brief Store a single image block to the output image.
  1422. *
  1423. * @param[out] img The output image data.
  1424. * @param blk The image block to export.
  1425. * @param bsd The block size information.
  1426. * @param xpos The block X coordinate in the input image.
  1427. * @param ypos The block Y coordinate in the input image.
  1428. * @param zpos The block Z coordinate in the input image.
  1429. * @param swz The swizzle to apply on store.
  1430. */
  1431. void store_image_block(
  1432. astcenc_image& img,
  1433. const image_block& blk,
  1434. const block_size_descriptor& bsd,
  1435. unsigned int xpos,
  1436. unsigned int ypos,
  1437. unsigned int zpos,
  1438. const astcenc_swizzle& swz);
  1439. /* ============================================================================
  1440. Functionality for computing endpoint colors and weights for a block.
  1441. ============================================================================ */
  1442. /**
  1443. * @brief Compute ideal endpoint colors and weights for 1 plane of weights.
  1444. *
  1445. * The ideal endpoints define a color line for the partition. For each texel the ideal weight
  1446. * defines an exact position on the partition color line. We can then use these to assess the error
  1447. * introduced by removing and quantizing the weight grid.
  1448. *
  1449. * @param blk The image block color data to compress.
  1450. * @param pi The partition info for the current trial.
  1451. * @param[out] ei The endpoint and weight values.
  1452. */
  1453. void compute_ideal_colors_and_weights_1plane(
  1454. const image_block& blk,
  1455. const partition_info& pi,
  1456. endpoints_and_weights& ei);
  1457. /**
  1458. * @brief Compute ideal endpoint colors and weights for 2 planes of weights.
  1459. *
  1460. * The ideal endpoints define a color line for the partition. For each texel the ideal weight
  1461. * defines an exact position on the partition color line. We can then use these to assess the error
  1462. * introduced by removing and quantizing the weight grid.
  1463. *
  1464. * @param bsd The block size information.
  1465. * @param blk The image block color data to compress.
  1466. * @param plane2_component The component assigned to plane 2.
  1467. * @param[out] ei1 The endpoint and weight values for plane 1.
  1468. * @param[out] ei2 The endpoint and weight values for plane 2.
  1469. */
  1470. void compute_ideal_colors_and_weights_2planes(
  1471. const block_size_descriptor& bsd,
  1472. const image_block& blk,
  1473. unsigned int plane2_component,
  1474. endpoints_and_weights& ei1,
  1475. endpoints_and_weights& ei2);
  1476. /**
  1477. * @brief Compute the optimal unquantized weights for a decimation table.
  1478. *
  1479. * After computing ideal weights for the case for a complete weight grid, we we want to compute the
  1480. * ideal weights for the case where weights exist only for some texels. We do this with a
  1481. * steepest-descent grid solver which works as follows:
  1482. *
  1483. * First, for each actual weight, perform a weighted averaging of the texels affected by the weight.
  1484. * Then, set step size to <some initial value> and attempt one step towards the original ideal
  1485. * weight if it helps to reduce error.
  1486. *
  1487. * @param ei The non-decimated endpoints and weights.
  1488. * @param di The selected weight decimation.
  1489. * @param[out] dec_weight_ideal_value The ideal values for the decimated weight set.
  1490. */
  1491. void compute_ideal_weights_for_decimation(
  1492. const endpoints_and_weights& ei,
  1493. const decimation_info& di,
  1494. float* dec_weight_ideal_value);
  1495. /**
  1496. * @brief Compute the optimal quantized weights for a decimation table.
  1497. *
  1498. * We test the two closest weight indices in the allowed quantization range and keep the weight that
  1499. * is the closest match.
  1500. *
  1501. * @param di The selected weight decimation.
  1502. * @param low_bound The lowest weight allowed.
  1503. * @param high_bound The highest weight allowed.
  1504. * @param dec_weight_ideal_value The ideal weight set.
  1505. * @param[out] dec_weight_quant_uvalue The output quantized weight as a float.
  1506. * @param[out] dec_weight_uquant The output quantized weight as encoded int.
  1507. * @param quant_level The desired weight quant level.
  1508. */
  1509. void compute_quantized_weights_for_decimation(
  1510. const decimation_info& di,
  1511. float low_bound,
  1512. float high_bound,
  1513. const float* dec_weight_ideal_value,
  1514. float* dec_weight_quant_uvalue,
  1515. uint8_t* dec_weight_uquant,
  1516. quant_method quant_level);
  1517. /**
  1518. * @brief Compute the error of a decimated weight set for 1 plane.
  1519. *
  1520. * After computing ideal weights for the case with one weight per texel, we want to compute the
  1521. * error for decimated weight grids where weights are stored at a lower resolution. This function
  1522. * computes the error of the reduced grid, compared to the full grid.
  1523. *
  1524. * @param eai The ideal weights for the full grid.
  1525. * @param di The selected weight decimation.
  1526. * @param dec_weight_quant_uvalue The quantized weights for the decimated grid.
  1527. *
  1528. * @return The accumulated error.
  1529. */
  1530. float compute_error_of_weight_set_1plane(
  1531. const endpoints_and_weights& eai,
  1532. const decimation_info& di,
  1533. const float* dec_weight_quant_uvalue);
  1534. /**
  1535. * @brief Compute the error of a decimated weight set for 2 planes.
  1536. *
  1537. * After computing ideal weights for the case with one weight per texel, we want to compute the
  1538. * error for decimated weight grids where weights are stored at a lower resolution. This function
  1539. * computes the error of the reduced grid, compared to the full grid.
  1540. *
  1541. * @param eai1 The ideal weights for the full grid and plane 1.
  1542. * @param eai2 The ideal weights for the full grid and plane 2.
  1543. * @param di The selected weight decimation.
  1544. * @param dec_weight_quant_uvalue_plane1 The quantized weights for the decimated grid plane 1.
  1545. * @param dec_weight_quant_uvalue_plane2 The quantized weights for the decimated grid plane 2.
  1546. *
  1547. * @return The accumulated error.
  1548. */
  1549. float compute_error_of_weight_set_2planes(
  1550. const endpoints_and_weights& eai1,
  1551. const endpoints_and_weights& eai2,
  1552. const decimation_info& di,
  1553. const float* dec_weight_quant_uvalue_plane1,
  1554. const float* dec_weight_quant_uvalue_plane2);
  1555. /**
  1556. * @brief Pack a single pair of color endpoints as effectively as possible.
  1557. *
  1558. * The user requests a base color endpoint mode in @c format, but the quantizer may choose a
  1559. * delta-based representation. It will report back the format variant it actually used.
  1560. *
  1561. * @param color0 The input unquantized color0 endpoint for absolute endpoint pairs.
  1562. * @param color1 The input unquantized color1 endpoint for absolute endpoint pairs.
  1563. * @param rgbs_color The input unquantized RGBS variant endpoint for same chroma endpoints.
  1564. * @param rgbo_color The input unquantized RGBS variant endpoint for HDR endpoints.
  1565. * @param format The desired base format.
  1566. * @param[out] output The output storage for the quantized colors/
  1567. * @param quant_level The quantization level requested.
  1568. *
  1569. * @return The actual endpoint mode used.
  1570. */
  1571. uint8_t pack_color_endpoints(
  1572. vfloat4 color0,
  1573. vfloat4 color1,
  1574. vfloat4 rgbs_color,
  1575. vfloat4 rgbo_color,
  1576. int format,
  1577. uint8_t* output,
  1578. quant_method quant_level);
  1579. /**
  1580. * @brief Unpack a single pair of encoded endpoints.
  1581. *
  1582. * Endpoints must be unscrambled and converted into the 0-255 range before calling this functions.
  1583. *
  1584. * @param decode_mode The decode mode (LDR, HDR).
  1585. * @param format The color endpoint mode used.
  1586. * @param input The raw array of encoded input integers. The length of this array
  1587. * depends on @c format; it can be safely assumed to be large enough.
  1588. * @param[out] rgb_hdr Is the endpoint using HDR for the RGB channels?
  1589. * @param[out] alpha_hdr Is the endpoint using HDR for the A channel?
  1590. * @param[out] output0 The output color for endpoint 0.
  1591. * @param[out] output1 The output color for endpoint 1.
  1592. */
  1593. void unpack_color_endpoints(
  1594. astcenc_profile decode_mode,
  1595. int format,
  1596. const uint8_t* input,
  1597. bool& rgb_hdr,
  1598. bool& alpha_hdr,
  1599. vint4& output0,
  1600. vint4& output1);
  1601. /**
  1602. * @brief Unpack a set of quantized and decimated weights.
  1603. *
  1604. * TODO: Can we skip this for non-decimated weights now that the @c scb is
  1605. * already storing unquantized weights?
  1606. *
  1607. * @param bsd The block size information.
  1608. * @param scb The symbolic compressed encoding.
  1609. * @param di The weight grid decimation table.
  1610. * @param is_dual_plane @c true if this is a dual plane block, @c false otherwise.
  1611. * @param[out] weights_plane1 The output array for storing the plane 1 weights.
  1612. * @param[out] weights_plane2 The output array for storing the plane 2 weights.
  1613. */
  1614. void unpack_weights(
  1615. const block_size_descriptor& bsd,
  1616. const symbolic_compressed_block& scb,
  1617. const decimation_info& di,
  1618. bool is_dual_plane,
  1619. int weights_plane1[BLOCK_MAX_TEXELS],
  1620. int weights_plane2[BLOCK_MAX_TEXELS]);
  1621. /**
  1622. * @brief Identify, for each mode, which set of color endpoint produces the best result.
  1623. *
  1624. * Returns the best @c tune_candidate_limit best looking modes, along with the ideal color encoding
  1625. * combination for each. The modified quantization level can be used when all formats are the same,
  1626. * as this frees up two additional bits of storage.
  1627. *
  1628. * @param pi The partition info for the current trial.
  1629. * @param blk The image block color data to compress.
  1630. * @param ep The ideal endpoints.
  1631. * @param qwt_bitcounts Bit counts for different quantization methods.
  1632. * @param qwt_errors Errors for different quantization methods.
  1633. * @param tune_candidate_limit The max number of candidates to return, may be less.
  1634. * @param start_block_mode The first block mode to inspect.
  1635. * @param end_block_mode The last block mode to inspect.
  1636. * @param[out] partition_format_specifiers The best formats per partition.
  1637. * @param[out] block_mode The best packed block mode indexes.
  1638. * @param[out] quant_level The best color quant level.
  1639. * @param[out] quant_level_mod The best color quant level if endpoints are the same.
  1640. * @param[out] tmpbuf Preallocated scratch buffers for the compressor.
  1641. *
  1642. * @return The actual number of candidate matches returned.
  1643. */
  1644. unsigned int compute_ideal_endpoint_formats(
  1645. const partition_info& pi,
  1646. const image_block& blk,
  1647. const endpoints& ep,
  1648. const int8_t* qwt_bitcounts,
  1649. const float* qwt_errors,
  1650. unsigned int tune_candidate_limit,
  1651. unsigned int start_block_mode,
  1652. unsigned int end_block_mode,
  1653. uint8_t partition_format_specifiers[TUNE_MAX_TRIAL_CANDIDATES][BLOCK_MAX_PARTITIONS],
  1654. int block_mode[TUNE_MAX_TRIAL_CANDIDATES],
  1655. quant_method quant_level[TUNE_MAX_TRIAL_CANDIDATES],
  1656. quant_method quant_level_mod[TUNE_MAX_TRIAL_CANDIDATES],
  1657. compression_working_buffers& tmpbuf);
  1658. /**
  1659. * @brief For a given 1 plane weight set recompute the endpoint colors.
  1660. *
  1661. * As we quantize and decimate weights the optimal endpoint colors may change slightly, so we must
  1662. * recompute the ideal colors for a specific weight set.
  1663. *
  1664. * @param blk The image block color data to compress.
  1665. * @param pi The partition info for the current trial.
  1666. * @param di The weight grid decimation table.
  1667. * @param dec_weights_uquant The quantized weight set.
  1668. * @param[in,out] ep The color endpoints (modifed in place).
  1669. * @param[out] rgbs_vectors The RGB+scale vectors for LDR blocks.
  1670. * @param[out] rgbo_vectors The RGB+offset vectors for HDR blocks.
  1671. */
  1672. void recompute_ideal_colors_1plane(
  1673. const image_block& blk,
  1674. const partition_info& pi,
  1675. const decimation_info& di,
  1676. const uint8_t* dec_weights_uquant,
  1677. endpoints& ep,
  1678. vfloat4 rgbs_vectors[BLOCK_MAX_PARTITIONS],
  1679. vfloat4 rgbo_vectors[BLOCK_MAX_PARTITIONS]);
  1680. /**
  1681. * @brief For a given 2 plane weight set recompute the endpoint colors.
  1682. *
  1683. * As we quantize and decimate weights the optimal endpoint colors may change slightly, so we must
  1684. * recompute the ideal colors for a specific weight set.
  1685. *
  1686. * @param blk The image block color data to compress.
  1687. * @param bsd The block_size descriptor.
  1688. * @param di The weight grid decimation table.
  1689. * @param dec_weights_uquant_plane1 The quantized weight set for plane 1.
  1690. * @param dec_weights_uquant_plane2 The quantized weight set for plane 2.
  1691. * @param[in,out] ep The color endpoints (modifed in place).
  1692. * @param[out] rgbs_vector The RGB+scale color for LDR blocks.
  1693. * @param[out] rgbo_vector The RGB+offset color for HDR blocks.
  1694. * @param plane2_component The component assigned to plane 2.
  1695. */
  1696. void recompute_ideal_colors_2planes(
  1697. const image_block& blk,
  1698. const block_size_descriptor& bsd,
  1699. const decimation_info& di,
  1700. const uint8_t* dec_weights_uquant_plane1,
  1701. const uint8_t* dec_weights_uquant_plane2,
  1702. endpoints& ep,
  1703. vfloat4& rgbs_vector,
  1704. vfloat4& rgbo_vector,
  1705. int plane2_component);
  1706. /**
  1707. * @brief Expand the angular tables needed for the alternative to PCA that we use.
  1708. */
  1709. void prepare_angular_tables();
  1710. /**
  1711. * @brief Compute the angular endpoints for one plane for each block mode.
  1712. *
  1713. * @param only_always Only consider block modes that are always enabled.
  1714. * @param bsd The block size descriptor for the current trial.
  1715. * @param dec_weight_ideal_value The ideal decimated unquantized weight values.
  1716. * @param max_weight_quant The maximum block mode weight quantization allowed.
  1717. * @param[out] tmpbuf Preallocated scratch buffers for the compressor.
  1718. */
  1719. void compute_angular_endpoints_1plane(
  1720. bool only_always,
  1721. const block_size_descriptor& bsd,
  1722. const float* dec_weight_ideal_value,
  1723. unsigned int max_weight_quant,
  1724. compression_working_buffers& tmpbuf);
  1725. /**
  1726. * @brief Compute the angular endpoints for two planes for each block mode.
  1727. *
  1728. * @param bsd The block size descriptor for the current trial.
  1729. * @param dec_weight_ideal_value The ideal decimated unquantized weight values.
  1730. * @param max_weight_quant The maximum block mode weight quantization allowed.
  1731. * @param[out] tmpbuf Preallocated scratch buffers for the compressor.
  1732. */
  1733. void compute_angular_endpoints_2planes(
  1734. const block_size_descriptor& bsd,
  1735. const float* dec_weight_ideal_value,
  1736. unsigned int max_weight_quant,
  1737. compression_working_buffers& tmpbuf);
  1738. /* ============================================================================
  1739. Functionality for high level compression and decompression access.
  1740. ============================================================================ */
  1741. /**
  1742. * @brief Compress an image block into a physical block.
  1743. *
  1744. * @param ctx The compressor context and configuration.
  1745. * @param blk The image block color data to compress.
  1746. * @param[out] pcb The physical compressed block output.
  1747. * @param[out] tmpbuf Preallocated scratch buffers for the compressor.
  1748. */
  1749. void compress_block(
  1750. const astcenc_contexti& ctx,
  1751. const image_block& blk,
  1752. physical_compressed_block& pcb,
  1753. compression_working_buffers& tmpbuf);
  1754. /**
  1755. * @brief Decompress a symbolic block in to an image block.
  1756. *
  1757. * @param decode_mode The decode mode (LDR, HDR, etc).
  1758. * @param bsd The block size information.
  1759. * @param xpos The X coordinate of the block in the overall image.
  1760. * @param ypos The Y coordinate of the block in the overall image.
  1761. * @param zpos The Z coordinate of the block in the overall image.
  1762. * @param[out] blk The decompressed image block color data.
  1763. */
  1764. void decompress_symbolic_block(
  1765. astcenc_profile decode_mode,
  1766. const block_size_descriptor& bsd,
  1767. int xpos,
  1768. int ypos,
  1769. int zpos,
  1770. const symbolic_compressed_block& scb,
  1771. image_block& blk);
  1772. /**
  1773. * @brief Compute the error between a symbolic block and the original input data.
  1774. *
  1775. * This function is specialized for 2 plane and 1 partition search.
  1776. *
  1777. * In RGBM mode this will reject blocks that attempt to encode a zero M value.
  1778. *
  1779. * @param config The compressor config.
  1780. * @param bsd The block size information.
  1781. * @param scb The symbolic compressed encoding.
  1782. * @param blk The original image block color data.
  1783. *
  1784. * @return Returns the computed error, or a negative value if the encoding
  1785. * should be rejected for any reason.
  1786. */
  1787. float compute_symbolic_block_difference_2plane(
  1788. const astcenc_config& config,
  1789. const block_size_descriptor& bsd,
  1790. const symbolic_compressed_block& scb,
  1791. const image_block& blk);
  1792. /**
  1793. * @brief Compute the error between a symbolic block and the original input data.
  1794. *
  1795. * This function is specialized for 1 plane and N partition search.
  1796. *
  1797. * In RGBM mode this will reject blocks that attempt to encode a zero M value.
  1798. *
  1799. * @param config The compressor config.
  1800. * @param bsd The block size information.
  1801. * @param scb The symbolic compressed encoding.
  1802. * @param blk The original image block color data.
  1803. *
  1804. * @return Returns the computed error, or a negative value if the encoding
  1805. * should be rejected for any reason.
  1806. */
  1807. float compute_symbolic_block_difference_1plane(
  1808. const astcenc_config& config,
  1809. const block_size_descriptor& bsd,
  1810. const symbolic_compressed_block& scb,
  1811. const image_block& blk);
  1812. /**
  1813. * @brief Compute the error between a symbolic block and the original input data.
  1814. *
  1815. * This function is specialized for 1 plane and 1 partition search.
  1816. *
  1817. * In RGBM mode this will reject blocks that attempt to encode a zero M value.
  1818. *
  1819. * @param config The compressor config.
  1820. * @param bsd The block size information.
  1821. * @param scb The symbolic compressed encoding.
  1822. * @param blk The original image block color data.
  1823. *
  1824. * @return Returns the computed error, or a negative value if the encoding
  1825. * should be rejected for any reason.
  1826. */
  1827. float compute_symbolic_block_difference_1plane_1partition(
  1828. const astcenc_config& config,
  1829. const block_size_descriptor& bsd,
  1830. const symbolic_compressed_block& scb,
  1831. const image_block& blk);
  1832. /**
  1833. * @brief Convert a symbolic representation into a binary physical encoding.
  1834. *
  1835. * It is assumed that the symbolic encoding is valid and encodable, or
  1836. * previously flagged as an error block if an error color it to be encoded.
  1837. *
  1838. * @param bsd The block size information.
  1839. * @param scb The symbolic representation.
  1840. * @param[out] pcb The binary encoded data.
  1841. */
  1842. void symbolic_to_physical(
  1843. const block_size_descriptor& bsd,
  1844. const symbolic_compressed_block& scb,
  1845. physical_compressed_block& pcb);
  1846. /**
  1847. * @brief Convert a binary physical encoding into a symbolic representation.
  1848. *
  1849. * This function can cope with arbitrary input data; output blocks will be
  1850. * flagged as an error block if the encoding is invalid.
  1851. *
  1852. * @param bsd The block size information.
  1853. * @param pcb The binary encoded data.
  1854. * @param[out] scb The output symbolic representation.
  1855. */
  1856. void physical_to_symbolic(
  1857. const block_size_descriptor& bsd,
  1858. const physical_compressed_block& pcb,
  1859. symbolic_compressed_block& scb);
  1860. /* ============================================================================
  1861. Platform-specific functions.
  1862. ============================================================================ */
  1863. /**
  1864. * @brief Allocate an aligned memory buffer.
  1865. *
  1866. * Allocated memory must be freed by aligned_free;
  1867. *
  1868. * @param size The desired buffer size.
  1869. * @param align The desired buffer alignment; must be 2^N.
  1870. *
  1871. * @return The memory buffer pointer or nullptr on allocation failure.
  1872. */
  1873. template<typename T>
  1874. T* aligned_malloc(size_t size, size_t align)
  1875. {
  1876. void* ptr;
  1877. int error = 0;
  1878. #if defined(_WIN32)
  1879. ptr = _aligned_malloc(size, align);
  1880. #else
  1881. error = posix_memalign(&ptr, align, size);
  1882. #endif
  1883. if (error || (!ptr))
  1884. {
  1885. return nullptr;
  1886. }
  1887. return static_cast<T*>(ptr);
  1888. }
  1889. /**
  1890. * @brief Free an aligned memory buffer.
  1891. *
  1892. * @param ptr The buffer to free.
  1893. */
  1894. template<typename T>
  1895. void aligned_free(T* ptr)
  1896. {
  1897. #if defined(_WIN32)
  1898. _aligned_free(reinterpret_cast<void*>(ptr));
  1899. #else
  1900. free(reinterpret_cast<void*>(ptr));
  1901. #endif
  1902. }
  1903. #endif