astcenc_entry.cpp 41 KB

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