astcenc_entry.cpp 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427
  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 for the library entrypoint.
  19. */
  20. #include <array>
  21. #include <cstring>
  22. #include <new>
  23. #include "astcenc.h"
  24. #include "astcenc_internal_entry.h"
  25. #include "astcenc_diagnostic_trace.h"
  26. /**
  27. * @brief Record of the quality tuning parameter values.
  28. *
  29. * See the @c astcenc_config structure for detailed parameter documentation.
  30. *
  31. * Note that the mse_overshoot entries are scaling factors relative to the base MSE to hit db_limit.
  32. * A 20% overshoot is harder to hit for a higher base db_limit, so we may actually use lower ratios
  33. * for the more through search presets because the underlying db_limit is so much higher.
  34. */
  35. struct astcenc_preset_config
  36. {
  37. float quality;
  38. unsigned int tune_partition_count_limit;
  39. unsigned int tune_2partition_index_limit;
  40. unsigned int tune_3partition_index_limit;
  41. unsigned int tune_4partition_index_limit;
  42. unsigned int tune_block_mode_limit;
  43. unsigned int tune_refinement_limit;
  44. unsigned int tune_candidate_limit;
  45. unsigned int tune_2partitioning_candidate_limit;
  46. unsigned int tune_3partitioning_candidate_limit;
  47. unsigned int tune_4partitioning_candidate_limit;
  48. float tune_db_limit_a_base;
  49. float tune_db_limit_b_base;
  50. float tune_mse_overshoot;
  51. float tune_2_partition_early_out_limit_factor;
  52. float tune_3_partition_early_out_limit_factor;
  53. float tune_2_plane_early_out_limit_correlation;
  54. };
  55. /**
  56. * @brief The static presets for high bandwidth encodings (x < 25 texels per block).
  57. */
  58. static const std::array<astcenc_preset_config, 6> preset_configs_high {{
  59. {
  60. ASTCENC_PRE_FASTEST,
  61. 2, 10, 6, 4, 43, 2, 2, 2, 2, 2, 85.2f, 63.2f, 3.5f, 1.0f, 1.0f, 0.85f
  62. }, {
  63. ASTCENC_PRE_FAST,
  64. 3, 18, 10, 8, 55, 3, 3, 2, 2, 2, 85.2f, 63.2f, 3.5f, 1.0f, 1.0f, 0.90f
  65. }, {
  66. ASTCENC_PRE_MEDIUM,
  67. 4, 34, 28, 16, 77, 3, 3, 2, 2, 2, 95.0f, 70.0f, 2.5f, 1.1f, 1.05f, 0.95f
  68. }, {
  69. ASTCENC_PRE_THOROUGH,
  70. 4, 82, 60, 30, 94, 4, 4, 3, 2, 2, 105.0f, 77.0f, 10.0f, 1.35f, 1.15f, 0.97f
  71. }, {
  72. ASTCENC_PRE_VERYTHOROUGH,
  73. 4, 256, 128, 64, 98, 4, 6, 20, 14, 8, 200.0f, 200.0f, 10.0f, 1.6f, 1.4f, 0.98f
  74. }, {
  75. ASTCENC_PRE_EXHAUSTIVE,
  76. 4, 512, 512, 512, 100, 4, 8, 32, 32, 32, 200.0f, 200.0f, 10.0f, 2.0f, 2.0f, 0.99f
  77. }
  78. }};
  79. /**
  80. * @brief The static presets for medium bandwidth encodings (25 <= x < 64 texels per block).
  81. */
  82. static const std::array<astcenc_preset_config, 6> preset_configs_mid {{
  83. {
  84. ASTCENC_PRE_FASTEST,
  85. 2, 10, 6, 4, 43, 2, 2, 2, 2, 2, 85.2f, 63.2f, 3.5f, 1.0f, 1.0f, 0.80f
  86. }, {
  87. ASTCENC_PRE_FAST,
  88. 3, 18, 12, 10, 55, 3, 3, 2, 2, 2, 85.2f, 63.2f, 3.5f, 1.0f, 1.0f, 0.85f
  89. }, {
  90. ASTCENC_PRE_MEDIUM,
  91. 4, 34, 28, 16, 77, 3, 3, 2, 2, 2, 95.0f, 70.0f, 3.0f, 1.1f, 1.05f, 0.90f
  92. }, {
  93. ASTCENC_PRE_THOROUGH,
  94. 4, 82, 60, 30, 94, 4, 4, 3, 2, 2, 105.0f, 77.0f, 10.0f, 1.4f, 1.2f, 0.95f
  95. }, {
  96. ASTCENC_PRE_VERYTHOROUGH,
  97. 4, 256, 128, 64, 98, 4, 6, 12, 8, 3, 200.0f, 200.0f, 10.0f, 1.6f, 1.4f, 0.98f
  98. }, {
  99. ASTCENC_PRE_EXHAUSTIVE,
  100. 4, 256, 256, 256, 100, 4, 8, 32, 32, 32, 200.0f, 200.0f, 10.0f, 2.0f, 2.0f, 0.99f
  101. }
  102. }};
  103. /**
  104. * @brief The static presets for low bandwidth encodings (64 <= x texels per block).
  105. */
  106. static const std::array<astcenc_preset_config, 6> preset_configs_low {{
  107. {
  108. ASTCENC_PRE_FASTEST,
  109. 2, 10, 6, 4, 40, 2, 2, 2, 2, 2, 85.0f, 63.0f, 3.5f, 1.0f, 1.0f, 0.80f
  110. }, {
  111. ASTCENC_PRE_FAST,
  112. 2, 18, 12, 10, 55, 3, 3, 2, 2, 2, 85.0f, 63.0f, 3.5f, 1.0f, 1.0f, 0.85f
  113. }, {
  114. ASTCENC_PRE_MEDIUM,
  115. 3, 34, 28, 16, 77, 3, 3, 2, 2, 2, 95.0f, 70.0f, 3.5f, 1.1f, 1.05f, 0.90f
  116. }, {
  117. ASTCENC_PRE_THOROUGH,
  118. 4, 82, 60, 30, 93, 4, 4, 3, 2, 2, 105.0f, 77.0f, 10.0f, 1.3f, 1.2f, 0.97f
  119. }, {
  120. ASTCENC_PRE_VERYTHOROUGH,
  121. 4, 256, 128, 64, 98, 4, 6, 9, 5, 2, 200.0f, 200.0f, 10.0f, 1.6f, 1.4f, 0.98f
  122. }, {
  123. ASTCENC_PRE_EXHAUSTIVE,
  124. 4, 256, 256, 256, 100, 4, 8, 32, 32, 32, 200.0f, 200.0f, 10.0f, 2.0f, 2.0f, 0.99f
  125. }
  126. }};
  127. /**
  128. * @brief Validate CPU floating point meets assumptions made in the codec.
  129. *
  130. * The codec is written with the assumption that a float threaded through the @c if32 union will be
  131. * stored and reloaded as a 32-bit IEEE-754 float with round-to-nearest rounding. This is always the
  132. * case in an IEEE-754 compliant system, however not every system or compilation mode is actually
  133. * IEEE-754 compliant. This normally fails if the code is compiled with fast math enabled.
  134. *
  135. * @return Return @c ASTCENC_SUCCESS if validated, otherwise an error on failure.
  136. */
  137. static astcenc_error validate_cpu_float()
  138. {
  139. if32 p;
  140. volatile float xprec_testval = 2.51f;
  141. p.f = xprec_testval + 12582912.0f;
  142. float q = p.f - 12582912.0f;
  143. if (q != 3.0f)
  144. {
  145. return ASTCENC_ERR_BAD_CPU_FLOAT;
  146. }
  147. return ASTCENC_SUCCESS;
  148. }
  149. /**
  150. * @brief Validate CPU ISA support meets the requirements of this build of the library.
  151. *
  152. * Each library build is statically compiled for a particular set of CPU ISA features, such as the
  153. * SIMD support or other ISA extensions such as POPCNT. This function checks that the host CPU
  154. * actually supports everything this build needs.
  155. *
  156. * @return Return @c ASTCENC_SUCCESS if validated, otherwise an error on failure.
  157. */
  158. static astcenc_error validate_cpu_isa()
  159. {
  160. #if ASTCENC_SSE >= 41
  161. if (!cpu_supports_sse41())
  162. {
  163. return ASTCENC_ERR_BAD_CPU_ISA;
  164. }
  165. #endif
  166. #if ASTCENC_POPCNT >= 1
  167. if (!cpu_supports_popcnt())
  168. {
  169. return ASTCENC_ERR_BAD_CPU_ISA;
  170. }
  171. #endif
  172. #if ASTCENC_F16C >= 1
  173. if (!cpu_supports_f16c())
  174. {
  175. return ASTCENC_ERR_BAD_CPU_ISA;
  176. }
  177. #endif
  178. #if ASTCENC_AVX >= 2
  179. if (!cpu_supports_avx2())
  180. {
  181. return ASTCENC_ERR_BAD_CPU_ISA;
  182. }
  183. #endif
  184. return ASTCENC_SUCCESS;
  185. }
  186. /**
  187. * @brief Validate config profile.
  188. *
  189. * @param profile The profile to check.
  190. *
  191. * @return Return @c ASTCENC_SUCCESS if validated, otherwise an error on failure.
  192. */
  193. static astcenc_error validate_profile(
  194. astcenc_profile profile
  195. ) {
  196. // Values in this enum are from an external user, so not guaranteed to be
  197. // bounded to the enum values
  198. switch (static_cast<int>(profile))
  199. {
  200. case ASTCENC_PRF_LDR_SRGB:
  201. case ASTCENC_PRF_LDR:
  202. case ASTCENC_PRF_HDR_RGB_LDR_A:
  203. case ASTCENC_PRF_HDR:
  204. return ASTCENC_SUCCESS;
  205. default:
  206. return ASTCENC_ERR_BAD_PROFILE;
  207. }
  208. }
  209. /**
  210. * @brief Validate block size.
  211. *
  212. * @param block_x The block x dimensions.
  213. * @param block_y The block y dimensions.
  214. * @param block_z The block z dimensions.
  215. *
  216. * @return Return @c ASTCENC_SUCCESS if validated, otherwise an error on failure.
  217. */
  218. static astcenc_error validate_block_size(
  219. unsigned int block_x,
  220. unsigned int block_y,
  221. unsigned int block_z
  222. ) {
  223. // Test if this is a legal block size at all
  224. bool is_legal = (((block_z <= 1) && is_legal_2d_block_size(block_x, block_y)) ||
  225. ((block_z >= 2) && is_legal_3d_block_size(block_x, block_y, block_z)));
  226. if (!is_legal)
  227. {
  228. return ASTCENC_ERR_BAD_BLOCK_SIZE;
  229. }
  230. // Test if this build has sufficient capacity for this block size
  231. bool have_capacity = (block_x * block_y * block_z) <= BLOCK_MAX_TEXELS;
  232. if (!have_capacity)
  233. {
  234. return ASTCENC_ERR_NOT_IMPLEMENTED;
  235. }
  236. return ASTCENC_SUCCESS;
  237. }
  238. /**
  239. * @brief Validate flags.
  240. *
  241. * @param flags The flags to check.
  242. *
  243. * @return Return @c ASTCENC_SUCCESS if validated, otherwise an error on failure.
  244. */
  245. static astcenc_error validate_flags(
  246. unsigned int flags
  247. ) {
  248. // Flags field must not contain any unknown flag bits
  249. unsigned int exMask = ~ASTCENC_ALL_FLAGS;
  250. if (popcount(flags & exMask) != 0)
  251. {
  252. return ASTCENC_ERR_BAD_FLAGS;
  253. }
  254. // Flags field must only contain at most a single map type
  255. exMask = ASTCENC_FLG_MAP_NORMAL
  256. | ASTCENC_FLG_MAP_RGBM;
  257. if (popcount(flags & exMask) > 1)
  258. {
  259. return ASTCENC_ERR_BAD_FLAGS;
  260. }
  261. return ASTCENC_SUCCESS;
  262. }
  263. #if !defined(ASTCENC_DECOMPRESS_ONLY)
  264. /**
  265. * @brief Validate single channel compression swizzle.
  266. *
  267. * @param swizzle The swizzle to check.
  268. *
  269. * @return Return @c ASTCENC_SUCCESS if validated, otherwise an error on failure.
  270. */
  271. static astcenc_error validate_compression_swz(
  272. astcenc_swz swizzle
  273. ) {
  274. // Not all enum values are handled; SWZ_Z is invalid for compression
  275. switch (static_cast<int>(swizzle))
  276. {
  277. case ASTCENC_SWZ_R:
  278. case ASTCENC_SWZ_G:
  279. case ASTCENC_SWZ_B:
  280. case ASTCENC_SWZ_A:
  281. case ASTCENC_SWZ_0:
  282. case ASTCENC_SWZ_1:
  283. return ASTCENC_SUCCESS;
  284. default:
  285. return ASTCENC_ERR_BAD_SWIZZLE;
  286. }
  287. }
  288. /**
  289. * @brief Validate overall compression swizzle.
  290. *
  291. * @param swizzle The swizzle to check.
  292. *
  293. * @return Return @c ASTCENC_SUCCESS if validated, otherwise an error on failure.
  294. */
  295. static astcenc_error validate_compression_swizzle(
  296. const astcenc_swizzle& swizzle
  297. ) {
  298. if (validate_compression_swz(swizzle.r) ||
  299. validate_compression_swz(swizzle.g) ||
  300. validate_compression_swz(swizzle.b) ||
  301. validate_compression_swz(swizzle.a))
  302. {
  303. return ASTCENC_ERR_BAD_SWIZZLE;
  304. }
  305. return ASTCENC_SUCCESS;
  306. }
  307. #endif
  308. /**
  309. * @brief Validate single channel decompression swizzle.
  310. *
  311. * @param swizzle The swizzle to check.
  312. *
  313. * @return Return @c ASTCENC_SUCCESS if validated, otherwise an error on failure.
  314. */
  315. static astcenc_error validate_decompression_swz(
  316. astcenc_swz swizzle
  317. ) {
  318. // Values in this enum are from an external user, so not guaranteed to be
  319. // bounded to the enum values
  320. switch (static_cast<int>(swizzle))
  321. {
  322. case ASTCENC_SWZ_R:
  323. case ASTCENC_SWZ_G:
  324. case ASTCENC_SWZ_B:
  325. case ASTCENC_SWZ_A:
  326. case ASTCENC_SWZ_0:
  327. case ASTCENC_SWZ_1:
  328. case ASTCENC_SWZ_Z:
  329. return ASTCENC_SUCCESS;
  330. default:
  331. return ASTCENC_ERR_BAD_SWIZZLE;
  332. }
  333. }
  334. /**
  335. * @brief Validate overall decompression swizzle.
  336. *
  337. * @param swizzle The swizzle to check.
  338. *
  339. * @return Return @c ASTCENC_SUCCESS if validated, otherwise an error on failure.
  340. */
  341. static astcenc_error validate_decompression_swizzle(
  342. const astcenc_swizzle& swizzle
  343. ) {
  344. if (validate_decompression_swz(swizzle.r) ||
  345. validate_decompression_swz(swizzle.g) ||
  346. validate_decompression_swz(swizzle.b) ||
  347. validate_decompression_swz(swizzle.a))
  348. {
  349. return ASTCENC_ERR_BAD_SWIZZLE;
  350. }
  351. return ASTCENC_SUCCESS;
  352. }
  353. /**
  354. * Validate that an incoming configuration is in-spec.
  355. *
  356. * This function can respond in two ways:
  357. *
  358. * * Numerical inputs that have valid ranges are clamped to those valid ranges. No error is thrown
  359. * for out-of-range inputs in this case.
  360. * * Numerical inputs and logic inputs are are logically invalid and which make no sense
  361. * algorithmically will return an error.
  362. *
  363. * @param[in,out] config The input compressor configuration.
  364. *
  365. * @return Return @c ASTCENC_SUCCESS if validated, otherwise an error on failure.
  366. */
  367. static astcenc_error validate_config(
  368. astcenc_config &config
  369. ) {
  370. astcenc_error status;
  371. status = validate_profile(config.profile);
  372. if (status != ASTCENC_SUCCESS)
  373. {
  374. return status;
  375. }
  376. status = validate_flags(config.flags);
  377. if (status != ASTCENC_SUCCESS)
  378. {
  379. return status;
  380. }
  381. status = validate_block_size(config.block_x, config.block_y, config.block_z);
  382. if (status != ASTCENC_SUCCESS)
  383. {
  384. return status;
  385. }
  386. #if defined(ASTCENC_DECOMPRESS_ONLY)
  387. // Decompress-only builds only support decompress-only contexts
  388. if (!(config.flags & ASTCENC_FLG_DECOMPRESS_ONLY))
  389. {
  390. return ASTCENC_ERR_BAD_PARAM;
  391. }
  392. #endif
  393. config.rgbm_m_scale = astc::max(config.rgbm_m_scale, 1.0f);
  394. config.tune_partition_count_limit = astc::clamp(config.tune_partition_count_limit, 1u, 4u);
  395. config.tune_2partition_index_limit = astc::clamp(config.tune_2partition_index_limit, 1u, BLOCK_MAX_PARTITIONINGS);
  396. config.tune_3partition_index_limit = astc::clamp(config.tune_3partition_index_limit, 1u, BLOCK_MAX_PARTITIONINGS);
  397. config.tune_4partition_index_limit = astc::clamp(config.tune_4partition_index_limit, 1u, BLOCK_MAX_PARTITIONINGS);
  398. config.tune_block_mode_limit = astc::clamp(config.tune_block_mode_limit, 1u, 100u);
  399. config.tune_refinement_limit = astc::max(config.tune_refinement_limit, 1u);
  400. config.tune_candidate_limit = astc::clamp(config.tune_candidate_limit, 1u, TUNE_MAX_TRIAL_CANDIDATES);
  401. config.tune_2partitioning_candidate_limit = astc::clamp(config.tune_2partitioning_candidate_limit, 1u, TUNE_MAX_PARTITIONING_CANDIDATES);
  402. config.tune_3partitioning_candidate_limit = astc::clamp(config.tune_3partitioning_candidate_limit, 1u, TUNE_MAX_PARTITIONING_CANDIDATES);
  403. config.tune_4partitioning_candidate_limit = astc::clamp(config.tune_4partitioning_candidate_limit, 1u, TUNE_MAX_PARTITIONING_CANDIDATES);
  404. config.tune_db_limit = astc::max(config.tune_db_limit, 0.0f);
  405. config.tune_mse_overshoot = astc::max(config.tune_mse_overshoot, 1.0f);
  406. config.tune_2_partition_early_out_limit_factor = astc::max(config.tune_2_partition_early_out_limit_factor, 0.0f);
  407. config.tune_3_partition_early_out_limit_factor = astc::max(config.tune_3_partition_early_out_limit_factor, 0.0f);
  408. config.tune_2_plane_early_out_limit_correlation = astc::max(config.tune_2_plane_early_out_limit_correlation, 0.0f);
  409. // Specifying a zero weight color component is not allowed; force to small value
  410. float max_weight = astc::max(astc::max(config.cw_r_weight, config.cw_g_weight),
  411. astc::max(config.cw_b_weight, config.cw_a_weight));
  412. if (max_weight > 0.0f)
  413. {
  414. max_weight /= 1000.0f;
  415. config.cw_r_weight = astc::max(config.cw_r_weight, max_weight);
  416. config.cw_g_weight = astc::max(config.cw_g_weight, max_weight);
  417. config.cw_b_weight = astc::max(config.cw_b_weight, max_weight);
  418. config.cw_a_weight = astc::max(config.cw_a_weight, max_weight);
  419. }
  420. // If all color components error weights are zero then return an error
  421. else
  422. {
  423. return ASTCENC_ERR_BAD_PARAM;
  424. }
  425. return ASTCENC_SUCCESS;
  426. }
  427. /* See header for documentation. */
  428. astcenc_error astcenc_config_init(
  429. astcenc_profile profile,
  430. unsigned int block_x,
  431. unsigned int block_y,
  432. unsigned int block_z,
  433. float quality,
  434. unsigned int flags,
  435. astcenc_config* configp
  436. ) {
  437. astcenc_error status;
  438. // Check basic library compatibility options here so they are checked early. Note, these checks
  439. // are repeated in context_alloc for cases where callers use a manually defined config struct
  440. status = validate_cpu_isa();
  441. if (status != ASTCENC_SUCCESS)
  442. {
  443. return status;
  444. }
  445. status = validate_cpu_float();
  446. if (status != ASTCENC_SUCCESS)
  447. {
  448. return status;
  449. }
  450. // Zero init all config fields; although most of will be over written
  451. astcenc_config& config = *configp;
  452. std::memset(&config, 0, sizeof(config));
  453. // Process the block size
  454. block_z = astc::max(block_z, 1u); // For 2D blocks Z==0 is accepted, but convert to 1
  455. status = validate_block_size(block_x, block_y, block_z);
  456. if (status != ASTCENC_SUCCESS)
  457. {
  458. return status;
  459. }
  460. config.block_x = block_x;
  461. config.block_y = block_y;
  462. config.block_z = block_z;
  463. float texels = static_cast<float>(block_x * block_y * block_z);
  464. float ltexels = logf(texels) / logf(10.0f);
  465. // Process the performance quality level or preset; note that this must be done before we
  466. // process any additional settings, such as color profile and flags, which may replace some of
  467. // these settings with more use case tuned values
  468. if (quality < ASTCENC_PRE_FASTEST ||
  469. quality > ASTCENC_PRE_EXHAUSTIVE)
  470. {
  471. return ASTCENC_ERR_BAD_QUALITY;
  472. }
  473. static const std::array<astcenc_preset_config, 6>* preset_configs;
  474. int texels_int = block_x * block_y * block_z;
  475. if (texels_int < 25)
  476. {
  477. preset_configs = &preset_configs_high;
  478. }
  479. else if (texels_int < 64)
  480. {
  481. preset_configs = &preset_configs_mid;
  482. }
  483. else
  484. {
  485. preset_configs = &preset_configs_low;
  486. }
  487. // Determine which preset to use, or which pair to interpolate
  488. size_t start;
  489. size_t end;
  490. for (end = 0; end < preset_configs->size(); end++)
  491. {
  492. if ((*preset_configs)[end].quality >= quality)
  493. {
  494. break;
  495. }
  496. }
  497. start = end == 0 ? 0 : end - 1;
  498. // Start and end node are the same - so just transfer the values.
  499. if (start == end)
  500. {
  501. config.tune_partition_count_limit = (*preset_configs)[start].tune_partition_count_limit;
  502. config.tune_2partition_index_limit = (*preset_configs)[start].tune_2partition_index_limit;
  503. config.tune_3partition_index_limit = (*preset_configs)[start].tune_3partition_index_limit;
  504. config.tune_4partition_index_limit = (*preset_configs)[start].tune_4partition_index_limit;
  505. config.tune_block_mode_limit = (*preset_configs)[start].tune_block_mode_limit;
  506. config.tune_refinement_limit = (*preset_configs)[start].tune_refinement_limit;
  507. config.tune_candidate_limit = astc::min((*preset_configs)[start].tune_candidate_limit, TUNE_MAX_TRIAL_CANDIDATES);
  508. config.tune_2partitioning_candidate_limit = astc::min((*preset_configs)[start].tune_2partitioning_candidate_limit, TUNE_MAX_PARTITIONING_CANDIDATES);
  509. config.tune_3partitioning_candidate_limit = astc::min((*preset_configs)[start].tune_3partitioning_candidate_limit, TUNE_MAX_PARTITIONING_CANDIDATES);
  510. config.tune_4partitioning_candidate_limit = astc::min((*preset_configs)[start].tune_4partitioning_candidate_limit, TUNE_MAX_PARTITIONING_CANDIDATES);
  511. config.tune_db_limit = astc::max((*preset_configs)[start].tune_db_limit_a_base - 35 * ltexels,
  512. (*preset_configs)[start].tune_db_limit_b_base - 19 * ltexels);
  513. config.tune_mse_overshoot = (*preset_configs)[start].tune_mse_overshoot;
  514. config.tune_2_partition_early_out_limit_factor = (*preset_configs)[start].tune_2_partition_early_out_limit_factor;
  515. config.tune_3_partition_early_out_limit_factor =(*preset_configs)[start].tune_3_partition_early_out_limit_factor;
  516. config.tune_2_plane_early_out_limit_correlation = (*preset_configs)[start].tune_2_plane_early_out_limit_correlation;
  517. }
  518. // Start and end node are not the same - so interpolate between them
  519. else
  520. {
  521. auto& node_a = (*preset_configs)[start];
  522. auto& node_b = (*preset_configs)[end];
  523. float wt_range = node_b.quality - node_a.quality;
  524. assert(wt_range > 0);
  525. // Compute interpolation factors
  526. float wt_node_a = (node_b.quality - quality) / wt_range;
  527. float wt_node_b = (quality - node_a.quality) / wt_range;
  528. #define LERP(param) ((node_a.param * wt_node_a) + (node_b.param * wt_node_b))
  529. #define LERPI(param) astc::flt2int_rtn(\
  530. (static_cast<float>(node_a.param) * wt_node_a) + \
  531. (static_cast<float>(node_b.param) * wt_node_b))
  532. #define LERPUI(param) static_cast<unsigned int>(LERPI(param))
  533. config.tune_partition_count_limit = LERPI(tune_partition_count_limit);
  534. config.tune_2partition_index_limit = LERPI(tune_2partition_index_limit);
  535. config.tune_3partition_index_limit = LERPI(tune_3partition_index_limit);
  536. config.tune_4partition_index_limit = LERPI(tune_4partition_index_limit);
  537. config.tune_block_mode_limit = LERPI(tune_block_mode_limit);
  538. config.tune_refinement_limit = LERPI(tune_refinement_limit);
  539. config.tune_candidate_limit = astc::min(LERPUI(tune_candidate_limit),
  540. TUNE_MAX_TRIAL_CANDIDATES);
  541. config.tune_2partitioning_candidate_limit = astc::min(LERPUI(tune_2partitioning_candidate_limit),
  542. BLOCK_MAX_PARTITIONINGS);
  543. config.tune_3partitioning_candidate_limit = astc::min(LERPUI(tune_3partitioning_candidate_limit),
  544. BLOCK_MAX_PARTITIONINGS);
  545. config.tune_4partitioning_candidate_limit = astc::min(LERPUI(tune_4partitioning_candidate_limit),
  546. BLOCK_MAX_PARTITIONINGS);
  547. config.tune_db_limit = astc::max(LERP(tune_db_limit_a_base) - 35 * ltexels,
  548. LERP(tune_db_limit_b_base) - 19 * ltexels);
  549. config.tune_mse_overshoot = LERP(tune_mse_overshoot);
  550. config.tune_2_partition_early_out_limit_factor = LERP(tune_2_partition_early_out_limit_factor);
  551. config.tune_3_partition_early_out_limit_factor = LERP(tune_3_partition_early_out_limit_factor);
  552. config.tune_2_plane_early_out_limit_correlation = LERP(tune_2_plane_early_out_limit_correlation);
  553. #undef LERP
  554. #undef LERPI
  555. #undef LERPUI
  556. }
  557. // Set heuristics to the defaults for each color profile
  558. config.cw_r_weight = 1.0f;
  559. config.cw_g_weight = 1.0f;
  560. config.cw_b_weight = 1.0f;
  561. config.cw_a_weight = 1.0f;
  562. config.a_scale_radius = 0;
  563. config.rgbm_m_scale = 0.0f;
  564. config.profile = profile;
  565. // Values in this enum are from an external user, so not guaranteed to be
  566. // bounded to the enum values
  567. switch (static_cast<int>(profile))
  568. {
  569. case ASTCENC_PRF_LDR:
  570. case ASTCENC_PRF_LDR_SRGB:
  571. break;
  572. case ASTCENC_PRF_HDR_RGB_LDR_A:
  573. case ASTCENC_PRF_HDR:
  574. config.tune_db_limit = 999.0f;
  575. break;
  576. default:
  577. return ASTCENC_ERR_BAD_PROFILE;
  578. }
  579. // Flags field must not contain any unknown flag bits
  580. status = validate_flags(flags);
  581. if (status != ASTCENC_SUCCESS)
  582. {
  583. return status;
  584. }
  585. if (flags & ASTCENC_FLG_MAP_NORMAL)
  586. {
  587. // Normal map encoding uses L+A blocks, so allow one more partitioning
  588. // than normal. We need need fewer bits for endpoints, so more likely
  589. // to be able to use more partitions than an RGB/RGBA block
  590. config.tune_partition_count_limit = astc::min(config.tune_partition_count_limit + 1u, 4u);
  591. config.cw_g_weight = 0.0f;
  592. config.cw_b_weight = 0.0f;
  593. config.tune_2_partition_early_out_limit_factor *= 1.5f;
  594. config.tune_3_partition_early_out_limit_factor *= 1.5f;
  595. config.tune_2_plane_early_out_limit_correlation = 0.99f;
  596. // Normals are prone to blocking artifacts on smooth curves
  597. // so force compressor to try harder here ...
  598. config.tune_db_limit *= 1.03f;
  599. }
  600. else if (flags & ASTCENC_FLG_MAP_RGBM)
  601. {
  602. config.rgbm_m_scale = 5.0f;
  603. config.cw_a_weight = 2.0f * config.rgbm_m_scale;
  604. }
  605. else // (This is color data)
  606. {
  607. // This is a very basic perceptual metric for RGB color data, which weights error
  608. // significance by the perceptual luminance contribution of each color channel. For
  609. // luminance the usual weights to compute luminance from a linear RGB value are as
  610. // follows:
  611. //
  612. // l = r * 0.3 + g * 0.59 + b * 0.11
  613. //
  614. // ... but we scale these up to keep a better balance between color and alpha. Note
  615. // that if the content is using alpha we'd recommend using the -a option to weight
  616. // the color contribution by the alpha transparency.
  617. if (flags & ASTCENC_FLG_USE_PERCEPTUAL)
  618. {
  619. config.cw_r_weight = 0.30f * 2.25f;
  620. config.cw_g_weight = 0.59f * 2.25f;
  621. config.cw_b_weight = 0.11f * 2.25f;
  622. }
  623. }
  624. config.flags = flags;
  625. return ASTCENC_SUCCESS;
  626. }
  627. /* See header for documentation. */
  628. astcenc_error astcenc_context_alloc(
  629. const astcenc_config* configp,
  630. unsigned int thread_count,
  631. astcenc_context** context
  632. ) {
  633. astcenc_error status;
  634. const astcenc_config& config = *configp;
  635. status = validate_cpu_isa();
  636. if (status != ASTCENC_SUCCESS)
  637. {
  638. return status;
  639. }
  640. status = validate_cpu_float();
  641. if (status != ASTCENC_SUCCESS)
  642. {
  643. return status;
  644. }
  645. if (thread_count == 0)
  646. {
  647. return ASTCENC_ERR_BAD_PARAM;
  648. }
  649. #if defined(ASTCENC_DIAGNOSTICS)
  650. // Force single threaded compressor use in diagnostic mode.
  651. if (thread_count != 1)
  652. {
  653. return ASTCENC_ERR_BAD_PARAM;
  654. }
  655. #endif
  656. astcenc_context* ctxo = new astcenc_context;
  657. astcenc_contexti* ctx = &ctxo->context;
  658. ctx->thread_count = thread_count;
  659. ctx->config = config;
  660. ctx->working_buffers = nullptr;
  661. // These are allocated per-compress, as they depend on image size
  662. ctx->input_alpha_averages = nullptr;
  663. // Copy the config first and validate the copy (we may modify it)
  664. status = validate_config(ctx->config);
  665. if (status != ASTCENC_SUCCESS)
  666. {
  667. delete ctxo;
  668. return status;
  669. }
  670. ctx->bsd = aligned_malloc<block_size_descriptor>(sizeof(block_size_descriptor), ASTCENC_VECALIGN);
  671. bool can_omit_modes = static_cast<bool>(config.flags & ASTCENC_FLG_SELF_DECOMPRESS_ONLY);
  672. init_block_size_descriptor(config.block_x, config.block_y, config.block_z,
  673. can_omit_modes,
  674. config.tune_partition_count_limit,
  675. static_cast<float>(config.tune_block_mode_limit) / 100.0f,
  676. *ctx->bsd);
  677. #if !defined(ASTCENC_DECOMPRESS_ONLY)
  678. // Do setup only needed by compression
  679. if (!(status & ASTCENC_FLG_DECOMPRESS_ONLY))
  680. {
  681. // Turn a dB limit into a per-texel error for faster use later
  682. if ((ctx->config.profile == ASTCENC_PRF_LDR) || (ctx->config.profile == ASTCENC_PRF_LDR_SRGB))
  683. {
  684. ctx->config.tune_db_limit = astc::pow(0.1f, ctx->config.tune_db_limit * 0.1f) * 65535.0f * 65535.0f;
  685. }
  686. else
  687. {
  688. ctx->config.tune_db_limit = 0.0f;
  689. }
  690. size_t worksize = sizeof(compression_working_buffers) * thread_count;
  691. ctx->working_buffers = aligned_malloc<compression_working_buffers>(worksize, ASTCENC_VECALIGN);
  692. static_assert((sizeof(compression_working_buffers) % ASTCENC_VECALIGN) == 0,
  693. "compression_working_buffers size must be multiple of vector alignment");
  694. if (!ctx->working_buffers)
  695. {
  696. aligned_free<block_size_descriptor>(ctx->bsd);
  697. delete ctxo;
  698. *context = nullptr;
  699. return ASTCENC_ERR_OUT_OF_MEM;
  700. }
  701. }
  702. #endif
  703. #if defined(ASTCENC_DIAGNOSTICS)
  704. ctx->trace_log = new TraceLog(ctx->config.trace_file_path);
  705. if (!ctx->trace_log->m_file)
  706. {
  707. return ASTCENC_ERR_DTRACE_FAILURE;
  708. }
  709. trace_add_data("block_x", config.block_x);
  710. trace_add_data("block_y", config.block_y);
  711. trace_add_data("block_z", config.block_z);
  712. #endif
  713. *context = ctxo;
  714. #if !defined(ASTCENC_DECOMPRESS_ONLY)
  715. prepare_angular_tables();
  716. #endif
  717. return ASTCENC_SUCCESS;
  718. }
  719. /* See header dor documentation. */
  720. void astcenc_context_free(
  721. astcenc_context* ctxo
  722. ) {
  723. if (ctxo)
  724. {
  725. astcenc_contexti* ctx = &ctxo->context;
  726. aligned_free<compression_working_buffers>(ctx->working_buffers);
  727. aligned_free<block_size_descriptor>(ctx->bsd);
  728. #if defined(ASTCENC_DIAGNOSTICS)
  729. delete ctx->trace_log;
  730. #endif
  731. delete ctxo;
  732. }
  733. }
  734. #if !defined(ASTCENC_DECOMPRESS_ONLY)
  735. /**
  736. * @brief Compress an image, after any preflight has completed.
  737. *
  738. * @param[out] ctxo The compressor context.
  739. * @param thread_index The thread index.
  740. * @param image The intput image.
  741. * @param swizzle The input swizzle.
  742. * @param[out] buffer The output array for the compressed data.
  743. */
  744. static void compress_image(
  745. astcenc_context& ctxo,
  746. unsigned int thread_index,
  747. const astcenc_image& image,
  748. const astcenc_swizzle& swizzle,
  749. uint8_t* buffer
  750. ) {
  751. astcenc_contexti& ctx = ctxo.context;
  752. const block_size_descriptor& bsd = *ctx.bsd;
  753. astcenc_profile decode_mode = ctx.config.profile;
  754. image_block blk;
  755. int block_x = bsd.xdim;
  756. int block_y = bsd.ydim;
  757. int block_z = bsd.zdim;
  758. blk.texel_count = static_cast<uint8_t>(block_x * block_y * block_z);
  759. int dim_x = image.dim_x;
  760. int dim_y = image.dim_y;
  761. int dim_z = image.dim_z;
  762. int xblocks = (dim_x + block_x - 1) / block_x;
  763. int yblocks = (dim_y + block_y - 1) / block_y;
  764. int zblocks = (dim_z + block_z - 1) / block_z;
  765. int block_count = zblocks * yblocks * xblocks;
  766. int row_blocks = xblocks;
  767. int plane_blocks = xblocks * yblocks;
  768. // Populate the block channel weights
  769. blk.channel_weight = vfloat4(ctx.config.cw_r_weight,
  770. ctx.config.cw_g_weight,
  771. ctx.config.cw_b_weight,
  772. ctx.config.cw_a_weight);
  773. // Use preallocated scratch buffer
  774. auto& temp_buffers = ctx.working_buffers[thread_index];
  775. // Only the first thread actually runs the initializer
  776. ctxo.manage_compress.init(block_count);
  777. // Determine if we can use an optimized load function
  778. bool needs_swz = (swizzle.r != ASTCENC_SWZ_R) || (swizzle.g != ASTCENC_SWZ_G) ||
  779. (swizzle.b != ASTCENC_SWZ_B) || (swizzle.a != ASTCENC_SWZ_A);
  780. bool needs_hdr = (decode_mode == ASTCENC_PRF_HDR) ||
  781. (decode_mode == ASTCENC_PRF_HDR_RGB_LDR_A);
  782. bool use_fast_load = !needs_swz && !needs_hdr &&
  783. block_z == 1 && image.data_type == ASTCENC_TYPE_U8;
  784. auto load_func = load_image_block;
  785. if (use_fast_load)
  786. {
  787. load_func = load_image_block_fast_ldr;
  788. }
  789. // All threads run this processing loop until there is no work remaining
  790. while (true)
  791. {
  792. unsigned int count;
  793. unsigned int base = ctxo.manage_compress.get_task_assignment(16, count);
  794. if (!count)
  795. {
  796. break;
  797. }
  798. for (unsigned int i = base; i < base + count; i++)
  799. {
  800. // Decode i into x, y, z block indices
  801. int z = i / plane_blocks;
  802. unsigned int rem = i - (z * plane_blocks);
  803. int y = rem / row_blocks;
  804. int x = rem - (y * row_blocks);
  805. // Test if we can apply some basic alpha-scale RDO
  806. bool use_full_block = true;
  807. if (ctx.config.a_scale_radius != 0 && block_z == 1)
  808. {
  809. int start_x = x * block_x;
  810. int end_x = astc::min(dim_x, start_x + block_x);
  811. int start_y = y * block_y;
  812. int end_y = astc::min(dim_y, start_y + block_y);
  813. // SATs accumulate error, so don't test exactly zero. Test for
  814. // less than 1 alpha in the expanded block footprint that
  815. // includes the alpha radius.
  816. int x_footprint = block_x + 2 * (ctx.config.a_scale_radius - 1);
  817. int y_footprint = block_y + 2 * (ctx.config.a_scale_radius - 1);
  818. float footprint = static_cast<float>(x_footprint * y_footprint);
  819. float threshold = 0.9f / (255.0f * footprint);
  820. // Do we have any alpha values?
  821. use_full_block = false;
  822. for (int ay = start_y; ay < end_y; ay++)
  823. {
  824. for (int ax = start_x; ax < end_x; ax++)
  825. {
  826. float a_avg = ctx.input_alpha_averages[ay * dim_x + ax];
  827. if (a_avg > threshold)
  828. {
  829. use_full_block = true;
  830. ax = end_x;
  831. ay = end_y;
  832. }
  833. }
  834. }
  835. }
  836. // Fetch the full block for compression
  837. if (use_full_block)
  838. {
  839. load_func(decode_mode, image, blk, bsd, x * block_x, y * block_y, z * block_z, swizzle);
  840. // Scale RGB error contribution by the maximum alpha in the block
  841. // This encourages preserving alpha accuracy in regions with high
  842. // transparency, and can buy up to 0.5 dB PSNR.
  843. if (ctx.config.flags & ASTCENC_FLG_USE_ALPHA_WEIGHT)
  844. {
  845. float alpha_scale = blk.data_max.lane<3>() * (1.0f / 65535.0f);
  846. blk.channel_weight = vfloat4(ctx.config.cw_r_weight * alpha_scale,
  847. ctx.config.cw_g_weight * alpha_scale,
  848. ctx.config.cw_b_weight * alpha_scale,
  849. ctx.config.cw_a_weight);
  850. }
  851. }
  852. // Apply alpha scale RDO - substitute constant color block
  853. else
  854. {
  855. blk.origin_texel = vfloat4::zero();
  856. blk.data_min = vfloat4::zero();
  857. blk.data_mean = vfloat4::zero();
  858. blk.data_max = vfloat4::zero();
  859. blk.grayscale = true;
  860. }
  861. int offset = ((z * yblocks + y) * xblocks + x) * 16;
  862. uint8_t *bp = buffer + offset;
  863. physical_compressed_block* pcb = reinterpret_cast<physical_compressed_block*>(bp);
  864. compress_block(ctx, blk, *pcb, temp_buffers);
  865. }
  866. ctxo.manage_compress.complete_task_assignment(count);
  867. }
  868. }
  869. /**
  870. * @brief Compute regional averages in an image.
  871. *
  872. * This function can be called by multiple threads, but only after a single
  873. * thread calls the setup function @c init_compute_averages().
  874. *
  875. * Results are written back into @c img->input_alpha_averages.
  876. *
  877. * @param[out] ctx The context.
  878. * @param ag The average and variance arguments created during setup.
  879. */
  880. static void compute_averages(
  881. astcenc_context& ctx,
  882. const avg_args &ag
  883. ) {
  884. pixel_region_args arg = ag.arg;
  885. arg.work_memory = new vfloat4[ag.work_memory_size];
  886. int size_x = ag.img_size_x;
  887. int size_y = ag.img_size_y;
  888. int size_z = ag.img_size_z;
  889. int step_xy = ag.blk_size_xy;
  890. int step_z = ag.blk_size_z;
  891. int y_tasks = (size_y + step_xy - 1) / step_xy;
  892. // All threads run this processing loop until there is no work remaining
  893. while (true)
  894. {
  895. unsigned int count;
  896. unsigned int base = ctx.manage_avg.get_task_assignment(16, count);
  897. if (!count)
  898. {
  899. break;
  900. }
  901. for (unsigned int i = base; i < base + count; i++)
  902. {
  903. int z = (i / (y_tasks)) * step_z;
  904. int y = (i - (z * y_tasks)) * step_xy;
  905. arg.size_z = astc::min(step_z, size_z - z);
  906. arg.offset_z = z;
  907. arg.size_y = astc::min(step_xy, size_y - y);
  908. arg.offset_y = y;
  909. for (int x = 0; x < size_x; x += step_xy)
  910. {
  911. arg.size_x = astc::min(step_xy, size_x - x);
  912. arg.offset_x = x;
  913. compute_pixel_region_variance(ctx.context, arg);
  914. }
  915. }
  916. ctx.manage_avg.complete_task_assignment(count);
  917. }
  918. delete[] arg.work_memory;
  919. }
  920. #endif
  921. /* See header for documentation. */
  922. astcenc_error astcenc_compress_image(
  923. astcenc_context* ctxo,
  924. astcenc_image* imagep,
  925. const astcenc_swizzle* swizzle,
  926. uint8_t* data_out,
  927. size_t data_len,
  928. unsigned int thread_index
  929. ) {
  930. #if defined(ASTCENC_DECOMPRESS_ONLY)
  931. (void)ctxo;
  932. (void)imagep;
  933. (void)swizzle;
  934. (void)data_out;
  935. (void)data_len;
  936. (void)thread_index;
  937. return ASTCENC_ERR_BAD_CONTEXT;
  938. #else
  939. astcenc_contexti* ctx = &ctxo->context;
  940. astcenc_error status;
  941. astcenc_image& image = *imagep;
  942. if (ctx->config.flags & ASTCENC_FLG_DECOMPRESS_ONLY)
  943. {
  944. return ASTCENC_ERR_BAD_CONTEXT;
  945. }
  946. status = validate_compression_swizzle(*swizzle);
  947. if (status != ASTCENC_SUCCESS)
  948. {
  949. return status;
  950. }
  951. if (thread_index >= ctx->thread_count)
  952. {
  953. return ASTCENC_ERR_BAD_PARAM;
  954. }
  955. unsigned int block_x = ctx->config.block_x;
  956. unsigned int block_y = ctx->config.block_y;
  957. unsigned int block_z = ctx->config.block_z;
  958. unsigned int xblocks = (image.dim_x + block_x - 1) / block_x;
  959. unsigned int yblocks = (image.dim_y + block_y - 1) / block_y;
  960. unsigned int zblocks = (image.dim_z + block_z - 1) / block_z;
  961. // Check we have enough output space (16 bytes per block)
  962. size_t size_needed = xblocks * yblocks * zblocks * 16;
  963. if (data_len < size_needed)
  964. {
  965. return ASTCENC_ERR_OUT_OF_MEM;
  966. }
  967. // If context thread count is one then implicitly reset
  968. if (ctx->thread_count == 1)
  969. {
  970. astcenc_compress_reset(ctxo);
  971. }
  972. if (ctx->config.a_scale_radius != 0)
  973. {
  974. // First thread to enter will do setup, other threads will subsequently
  975. // enter the critical section but simply skip over the initialization
  976. auto init_avg = [ctx, &image, swizzle]() {
  977. // Perform memory allocations for the destination buffers
  978. size_t texel_count = image.dim_x * image.dim_y * image.dim_z;
  979. ctx->input_alpha_averages = new float[texel_count];
  980. return init_compute_averages(
  981. image, ctx->config.a_scale_radius, *swizzle,
  982. ctx->avg_preprocess_args);
  983. };
  984. // Only the first thread actually runs the initializer
  985. ctxo->manage_avg.init(init_avg);
  986. // All threads will enter this function and dynamically grab work
  987. compute_averages(*ctxo, ctx->avg_preprocess_args);
  988. }
  989. // Wait for compute_averages to complete before compressing
  990. ctxo->manage_avg.wait();
  991. compress_image(*ctxo, thread_index, image, *swizzle, data_out);
  992. // Wait for compress to complete before freeing memory
  993. ctxo->manage_compress.wait();
  994. auto term_compress = [ctx]() {
  995. delete[] ctx->input_alpha_averages;
  996. ctx->input_alpha_averages = nullptr;
  997. };
  998. // Only the first thread to arrive actually runs the term
  999. ctxo->manage_compress.term(term_compress);
  1000. return ASTCENC_SUCCESS;
  1001. #endif
  1002. }
  1003. /* See header for documentation. */
  1004. astcenc_error astcenc_compress_reset(
  1005. astcenc_context* ctxo
  1006. ) {
  1007. #if defined(ASTCENC_DECOMPRESS_ONLY)
  1008. (void)ctxo;
  1009. return ASTCENC_ERR_BAD_CONTEXT;
  1010. #else
  1011. astcenc_contexti* ctx = &ctxo->context;
  1012. if (ctx->config.flags & ASTCENC_FLG_DECOMPRESS_ONLY)
  1013. {
  1014. return ASTCENC_ERR_BAD_CONTEXT;
  1015. }
  1016. ctxo->manage_avg.reset();
  1017. ctxo->manage_compress.reset();
  1018. return ASTCENC_SUCCESS;
  1019. #endif
  1020. }
  1021. /* See header for documentation. */
  1022. astcenc_error astcenc_decompress_image(
  1023. astcenc_context* ctxo,
  1024. const uint8_t* data,
  1025. size_t data_len,
  1026. astcenc_image* image_outp,
  1027. const astcenc_swizzle* swizzle,
  1028. unsigned int thread_index
  1029. ) {
  1030. astcenc_error status;
  1031. astcenc_image& image_out = *image_outp;
  1032. astcenc_contexti* ctx = &ctxo->context;
  1033. // Today this doesn't matter (working set on stack) but might in future ...
  1034. if (thread_index >= ctx->thread_count)
  1035. {
  1036. return ASTCENC_ERR_BAD_PARAM;
  1037. }
  1038. status = validate_decompression_swizzle(*swizzle);
  1039. if (status != ASTCENC_SUCCESS)
  1040. {
  1041. return status;
  1042. }
  1043. unsigned int block_x = ctx->config.block_x;
  1044. unsigned int block_y = ctx->config.block_y;
  1045. unsigned int block_z = ctx->config.block_z;
  1046. unsigned int xblocks = (image_out.dim_x + block_x - 1) / block_x;
  1047. unsigned int yblocks = (image_out.dim_y + block_y - 1) / block_y;
  1048. unsigned int zblocks = (image_out.dim_z + block_z - 1) / block_z;
  1049. int row_blocks = xblocks;
  1050. int plane_blocks = xblocks * yblocks;
  1051. // Check we have enough output space (16 bytes per block)
  1052. size_t size_needed = xblocks * yblocks * zblocks * 16;
  1053. if (data_len < size_needed)
  1054. {
  1055. return ASTCENC_ERR_OUT_OF_MEM;
  1056. }
  1057. image_block blk;
  1058. blk.texel_count = static_cast<uint8_t>(block_x * block_y * block_z);
  1059. // If context thread count is one then implicitly reset
  1060. if (ctx->thread_count == 1)
  1061. {
  1062. astcenc_decompress_reset(ctxo);
  1063. }
  1064. // Only the first thread actually runs the initializer
  1065. ctxo->manage_decompress.init(zblocks * yblocks * xblocks);
  1066. // All threads run this processing loop until there is no work remaining
  1067. while (true)
  1068. {
  1069. unsigned int count;
  1070. unsigned int base = ctxo->manage_decompress.get_task_assignment(128, count);
  1071. if (!count)
  1072. {
  1073. break;
  1074. }
  1075. for (unsigned int i = base; i < base + count; i++)
  1076. {
  1077. // Decode i into x, y, z block indices
  1078. int z = i / plane_blocks;
  1079. unsigned int rem = i - (z * plane_blocks);
  1080. int y = rem / row_blocks;
  1081. int x = rem - (y * row_blocks);
  1082. unsigned int offset = (((z * yblocks + y) * xblocks) + x) * 16;
  1083. const uint8_t* bp = data + offset;
  1084. const physical_compressed_block& pcb = *reinterpret_cast<const physical_compressed_block*>(bp);
  1085. symbolic_compressed_block scb;
  1086. physical_to_symbolic(*ctx->bsd, pcb, scb);
  1087. decompress_symbolic_block(ctx->config.profile, *ctx->bsd,
  1088. x * block_x, y * block_y, z * block_z,
  1089. scb, blk);
  1090. store_image_block(image_out, blk, *ctx->bsd,
  1091. x * block_x, y * block_y, z * block_z, *swizzle);
  1092. }
  1093. ctxo->manage_decompress.complete_task_assignment(count);
  1094. }
  1095. return ASTCENC_SUCCESS;
  1096. }
  1097. /* See header for documentation. */
  1098. astcenc_error astcenc_decompress_reset(
  1099. astcenc_context* ctxo
  1100. ) {
  1101. ctxo->manage_decompress.reset();
  1102. return ASTCENC_SUCCESS;
  1103. }
  1104. /* See header for documentation. */
  1105. astcenc_error astcenc_get_block_info(
  1106. astcenc_context* ctxo,
  1107. const uint8_t data[16],
  1108. astcenc_block_info* info
  1109. ) {
  1110. #if defined(ASTCENC_DECOMPRESS_ONLY)
  1111. (void)ctxo;
  1112. (void)data;
  1113. (void)info;
  1114. return ASTCENC_ERR_BAD_CONTEXT;
  1115. #else
  1116. astcenc_contexti* ctx = &ctxo->context;
  1117. // Decode the compressed data into a symbolic form
  1118. const physical_compressed_block&pcb = *reinterpret_cast<const physical_compressed_block*>(data);
  1119. symbolic_compressed_block scb;
  1120. physical_to_symbolic(*ctx->bsd, pcb, scb);
  1121. // Fetch the appropriate partition and decimation tables
  1122. block_size_descriptor& bsd = *ctx->bsd;
  1123. // Start from a clean slate
  1124. memset(info, 0, sizeof(*info));
  1125. // Basic info we can always populate
  1126. info->profile = ctx->config.profile;
  1127. info->block_x = ctx->config.block_x;
  1128. info->block_y = ctx->config.block_y;
  1129. info->block_z = ctx->config.block_z;
  1130. info->texel_count = bsd.texel_count;
  1131. // Check for error blocks first
  1132. info->is_error_block = scb.block_type == SYM_BTYPE_ERROR;
  1133. if (info->is_error_block)
  1134. {
  1135. return ASTCENC_SUCCESS;
  1136. }
  1137. // Check for constant color blocks second
  1138. info->is_constant_block = scb.block_type == SYM_BTYPE_CONST_F16 ||
  1139. scb.block_type == SYM_BTYPE_CONST_U16;
  1140. if (info->is_constant_block)
  1141. {
  1142. return ASTCENC_SUCCESS;
  1143. }
  1144. // Otherwise handle a full block ; known to be valid after conditions above have been checked
  1145. int partition_count = scb.partition_count;
  1146. const auto& pi = bsd.get_partition_info(partition_count, scb.partition_index);
  1147. const block_mode& bm = bsd.get_block_mode(scb.block_mode);
  1148. const decimation_info& di = bsd.get_decimation_info(bm.decimation_mode);
  1149. info->weight_x = di.weight_x;
  1150. info->weight_y = di.weight_y;
  1151. info->weight_z = di.weight_z;
  1152. info->is_dual_plane_block = bm.is_dual_plane != 0;
  1153. info->partition_count = scb.partition_count;
  1154. info->partition_index = scb.partition_index;
  1155. info->dual_plane_component = scb.plane2_component;
  1156. info->color_level_count = get_quant_level(scb.get_color_quant_mode());
  1157. info->weight_level_count = get_quant_level(bm.get_weight_quant_mode());
  1158. // Unpack color endpoints for each active partition
  1159. for (unsigned int i = 0; i < scb.partition_count; i++)
  1160. {
  1161. bool rgb_hdr;
  1162. bool a_hdr;
  1163. vint4 endpnt[2];
  1164. unpack_color_endpoints(ctx->config.profile,
  1165. scb.color_formats[i],
  1166. scb.color_values[i],
  1167. rgb_hdr, a_hdr,
  1168. endpnt[0], endpnt[1]);
  1169. // Store the color endpoint mode info
  1170. info->color_endpoint_modes[i] = scb.color_formats[i];
  1171. info->is_hdr_block = info->is_hdr_block || rgb_hdr || a_hdr;
  1172. // Store the unpacked and decoded color endpoint
  1173. vmask4 hdr_mask(rgb_hdr, rgb_hdr, rgb_hdr, a_hdr);
  1174. for (int j = 0; j < 2; j++)
  1175. {
  1176. vint4 color_lns = lns_to_sf16(endpnt[j]);
  1177. vint4 color_unorm = unorm16_to_sf16(endpnt[j]);
  1178. vint4 datai = select(color_unorm, color_lns, hdr_mask);
  1179. store(float16_to_float(datai), info->color_endpoints[i][j]);
  1180. }
  1181. }
  1182. // Unpack weights for each texel
  1183. int weight_plane1[BLOCK_MAX_TEXELS];
  1184. int weight_plane2[BLOCK_MAX_TEXELS];
  1185. unpack_weights(bsd, scb, di, bm.is_dual_plane, weight_plane1, weight_plane2);
  1186. for (unsigned int i = 0; i < bsd.texel_count; i++)
  1187. {
  1188. info->weight_values_plane1[i] = static_cast<float>(weight_plane1[i]) * (1.0f / WEIGHTS_TEXEL_SUM);
  1189. if (info->is_dual_plane_block)
  1190. {
  1191. info->weight_values_plane2[i] = static_cast<float>(weight_plane2[i]) * (1.0f / WEIGHTS_TEXEL_SUM);
  1192. }
  1193. }
  1194. // Unpack partition assignments for each texel
  1195. for (unsigned int i = 0; i < bsd.texel_count; i++)
  1196. {
  1197. info->partition_assignment[i] = pi.partition_of_texel[i];
  1198. }
  1199. return ASTCENC_SUCCESS;
  1200. #endif
  1201. }
  1202. /* See header for documentation. */
  1203. const char* astcenc_get_error_string(
  1204. astcenc_error status
  1205. ) {
  1206. // Values in this enum are from an external user, so not guaranteed to be
  1207. // bounded to the enum values
  1208. switch (static_cast<int>(status))
  1209. {
  1210. case ASTCENC_SUCCESS:
  1211. return "ASTCENC_SUCCESS";
  1212. case ASTCENC_ERR_OUT_OF_MEM:
  1213. return "ASTCENC_ERR_OUT_OF_MEM";
  1214. case ASTCENC_ERR_BAD_CPU_FLOAT:
  1215. return "ASTCENC_ERR_BAD_CPU_FLOAT";
  1216. case ASTCENC_ERR_BAD_CPU_ISA:
  1217. return "ASTCENC_ERR_BAD_CPU_ISA";
  1218. case ASTCENC_ERR_BAD_PARAM:
  1219. return "ASTCENC_ERR_BAD_PARAM";
  1220. case ASTCENC_ERR_BAD_BLOCK_SIZE:
  1221. return "ASTCENC_ERR_BAD_BLOCK_SIZE";
  1222. case ASTCENC_ERR_BAD_PROFILE:
  1223. return "ASTCENC_ERR_BAD_PROFILE";
  1224. case ASTCENC_ERR_BAD_QUALITY:
  1225. return "ASTCENC_ERR_BAD_QUALITY";
  1226. case ASTCENC_ERR_BAD_FLAGS:
  1227. return "ASTCENC_ERR_BAD_FLAGS";
  1228. case ASTCENC_ERR_BAD_SWIZZLE:
  1229. return "ASTCENC_ERR_BAD_SWIZZLE";
  1230. case ASTCENC_ERR_BAD_CONTEXT:
  1231. return "ASTCENC_ERR_BAD_CONTEXT";
  1232. case ASTCENC_ERR_NOT_IMPLEMENTED:
  1233. return "ASTCENC_ERR_NOT_IMPLEMENTED";
  1234. #if defined(ASTCENC_DIAGNOSTICS)
  1235. case ASTCENC_ERR_DTRACE_FAILURE:
  1236. return "ASTCENC_ERR_DTRACE_FAILURE";
  1237. #endif
  1238. default:
  1239. return nullptr;
  1240. }
  1241. }