astcenc.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  1. // SPDX-License-Identifier: Apache-2.0
  2. // ----------------------------------------------------------------------------
  3. // Copyright 2020-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 The core astcenc codec library interface.
  19. *
  20. * This interface is the entry point to the core astcenc codec. It aims to be easy to use for
  21. * non-experts, but also to allow experts to have fine control over the compressor heuristics if
  22. * needed. The core codec only handles compression and decompression, transferring all inputs and
  23. * outputs via memory buffers. To catch obvious input/output buffer sizing issues, which can cause
  24. * security and stability problems, all transfer buffers are explicitly sized.
  25. *
  26. * While the aim is that we keep this interface mostly stable, it should be viewed as a mutable
  27. * interface tied to a specific source version. We are not trying to maintain backwards
  28. * compatibility across codec versions.
  29. *
  30. * The API state management is based around an explicit context object, which is the context for all
  31. * allocated memory resources needed to compress and decompress a single image. A context can be
  32. * used to sequentially compress multiple images using the same configuration, allowing setup
  33. * overheads to be amortized over multiple images, which is particularly important when images are
  34. * small.
  35. *
  36. * Multi-threading can be used two ways.
  37. *
  38. * * An application wishing to process multiple images in parallel can allocate multiple
  39. * contexts and assign each context to a thread.
  40. * * An application wishing to process a single image in using multiple threads can configure
  41. * contexts for multi-threaded use, and invoke astcenc_compress/decompress() once per thread
  42. * for faster processing. The caller is responsible for creating the worker threads, and
  43. * synchronizing between images.
  44. *
  45. * Extended instruction set support
  46. * ================================
  47. *
  48. * This library supports use of extended instruction sets, such as SSE4.1 and AVX2. These are
  49. * enabled at compile time when building the library. There is no runtime checking in the core
  50. * library that the instruction sets used are actually available. Checking compatibility is the
  51. * responsibility of the calling code.
  52. *
  53. * Threading
  54. * =========
  55. *
  56. * In pseudo-code, the usage for manual user threading looks like this:
  57. *
  58. * // Configure the compressor run
  59. * astcenc_config my_config;
  60. * astcenc_config_init(..., &my_config);
  61. *
  62. * // Power users can tweak <my_config> settings here ...
  63. *
  64. * // Allocate working state given config and thread_count
  65. * astcenc_context* my_context;
  66. * astcenc_context_alloc(&my_config, thread_count, &my_context);
  67. *
  68. * // Compress each image using these config settings
  69. * foreach image:
  70. * // For each thread in the thread pool
  71. * for i in range(0, thread_count):
  72. * astcenc_compress_image(my_context, &my_input, my_output, i);
  73. *
  74. * astcenc_compress_reset(my_context);
  75. *
  76. * // Clean up
  77. * astcenc_context_free(my_context);
  78. *
  79. * Images
  80. * ======
  81. *
  82. * The codec supports compressing single images, which can be either 2D images or volumetric 3D
  83. * images. Calling code is responsible for any handling of aggregate types, such as mipmap chains,
  84. * texture arrays, or sliced 3D textures.
  85. *
  86. * Images are passed in as an astcenc_image structure. Inputs can be either 8-bit unorm, 16-bit
  87. * half-float, or 32-bit float, as indicated by the data_type field.
  88. *
  89. * Images can be any dimension; there is no requirement to be a multiple of the ASTC block size.
  90. *
  91. * Data is always passed in as 4 color components, and accessed as an array of 2D image slices. Data
  92. * within an image slice is always tightly packed without padding. Addressing looks like this:
  93. *
  94. * data[z_coord][y_coord * x_dim * 4 + x_coord * 4 ] // Red
  95. * data[z_coord][y_coord * x_dim * 4 + x_coord * 4 + 1] // Green
  96. * data[z_coord][y_coord * x_dim * 4 + x_coord * 4 + 2] // Blue
  97. * data[z_coord][y_coord * x_dim * 4 + x_coord * 4 + 3] // Alpha
  98. *
  99. * Common compressor usage
  100. * =======================
  101. *
  102. * One of the most important things for coding image quality is to align the input data component
  103. * count with the ASTC color endpoint mode. This avoids wasting bits encoding components you don't
  104. * actually need in the endpoint colors.
  105. *
  106. * | Input data | Encoding swizzle | Sampling swizzle |
  107. * | ------------ | ---------------- | ---------------- |
  108. * | 1 component | RRR1 | .[rgb] |
  109. * | 2 components | RRRG | .[rgb]a |
  110. * | 3 components | RGB1 | .rgb |
  111. * | 4 components | RGBA | .rgba |
  112. *
  113. * The 1 and 2 component modes recommend sampling from "g" to recover the luminance value as this
  114. * provide best compatibility with other texture formats where the green component may be stored at
  115. * higher precision than the others, such as RGB565. For ASTC any of the RGB components can be used;
  116. * the luminance endpoint component will be returned for all three.
  117. *
  118. * When using the normal map compression mode ASTC will store normals as a two component X+Y map.
  119. * Input images must contain unit-length normalized and should be passed in using a two component
  120. * swizzle. The astcenc command line tool defaults to an RRRG swizzle, but some developers prefer
  121. * to use GGGR for compatability with BC5n which will work just as well. The Z component can be
  122. * recovered programmatically in shader code, using knowledge that the vector is unit length and
  123. * that Z must be positive for a tangent-space normal map.
  124. *
  125. * Decompress-only usage
  126. * =====================
  127. *
  128. * For some use cases it is useful to have a cut-down context and/or library which supports
  129. * decompression but not compression.
  130. *
  131. * A context can be made decompress-only using the ASTCENC_FLG_DECOMPRESS_ONLY flag when the context
  132. * is allocated. These contexts have lower dynamic memory footprint than a full context.
  133. *
  134. * The entire library can be made decompress-only by building the files with the define
  135. * ASTCENC_DECOMPRESS_ONLY set. In this build the context will be smaller, and the library will
  136. * exclude the functionality which is only needed for compression. This reduces the binary size by
  137. * ~180KB. For these builds contexts must be created with the ASTCENC_FLG_DECOMPRESS_ONLY flag.
  138. *
  139. * Note that context structures returned by a library built as decompress-only are incompatible with
  140. * a library built with compression included, and visa versa, as they have different sizes and
  141. * memory layout.
  142. *
  143. * Self-decompress-only usage
  144. * ==========================
  145. *
  146. * ASTC is a complex format with a large search space. The parts of this search space that are
  147. * searched is determined by heuristics that are, in part, tied to the quality level used when
  148. * creating the context.
  149. *
  150. * A normal context is capable of decompressing any ASTC texture, including those generated by other
  151. * compressors with unknown heuristics. This is the most flexible implementation, but forces the
  152. * data tables used by the codec to include entries that are not needed during compression. This
  153. * can slow down context creation by a significant amount, especially for the faster compression
  154. * modes where few data table entries are actually used. To optimize this use case the context can
  155. * be created with the ASTCENC_FLG_SELF_DECOMPRESS_ONLY flag. This tells the compressor that it will
  156. * only be asked to decompress images that it compressed itself, allowing the data tables to
  157. * exclude entries that are not needed by the current compression configuration. This reduces the
  158. * size of the context data tables in memory and improves context creation performance. Note that,
  159. * as of the 3.6 release, this flag no longer affects compression performance.
  160. *
  161. * Using this flag while attempting to decompress an valid image which was created by another
  162. * compressor, or even another astcenc compressor version or configuration, may result in blocks
  163. * returning as solid magenta or NaN value error blocks.
  164. */
  165. #ifndef ASTCENC_INCLUDED
  166. #define ASTCENC_INCLUDED
  167. #include <cstddef>
  168. #include <cstdint>
  169. #if defined(ASTCENC_DYNAMIC_LIBRARY)
  170. #if defined(_MSC_VER)
  171. #define ASTCENC_PUBLIC extern "C" __declspec(dllexport)
  172. #else
  173. #define ASTCENC_PUBLIC extern "C" __attribute__ ((visibility ("default")))
  174. #endif
  175. #else
  176. #define ASTCENC_PUBLIC
  177. #endif
  178. /* ============================================================================
  179. Data declarations
  180. ============================================================================ */
  181. /**
  182. * @brief An opaque structure; see astcenc_internal.h for definition.
  183. */
  184. struct astcenc_context;
  185. /**
  186. * @brief A codec API error code.
  187. */
  188. enum astcenc_error {
  189. /** @brief The call was successful. */
  190. ASTCENC_SUCCESS = 0,
  191. /** @brief The call failed due to low memory, or undersized I/O buffers. */
  192. ASTCENC_ERR_OUT_OF_MEM,
  193. /** @brief The call failed due to the build using fast math. */
  194. ASTCENC_ERR_BAD_CPU_FLOAT,
  195. /** @brief The call failed due to an out-of-spec parameter. */
  196. ASTCENC_ERR_BAD_PARAM,
  197. /** @brief The call failed due to an out-of-spec block size. */
  198. ASTCENC_ERR_BAD_BLOCK_SIZE,
  199. /** @brief The call failed due to an out-of-spec color profile. */
  200. ASTCENC_ERR_BAD_PROFILE,
  201. /** @brief The call failed due to an out-of-spec quality value. */
  202. ASTCENC_ERR_BAD_QUALITY,
  203. /** @brief The call failed due to an out-of-spec component swizzle. */
  204. ASTCENC_ERR_BAD_SWIZZLE,
  205. /** @brief The call failed due to an out-of-spec flag set. */
  206. ASTCENC_ERR_BAD_FLAGS,
  207. /** @brief The call failed due to the context not supporting the operation. */
  208. ASTCENC_ERR_BAD_CONTEXT,
  209. /** @brief The call failed due to unimplemented functionality. */
  210. ASTCENC_ERR_NOT_IMPLEMENTED,
  211. #if defined(ASTCENC_DIAGNOSTICS)
  212. /** @brief The call failed due to an issue with diagnostic tracing. */
  213. ASTCENC_ERR_DTRACE_FAILURE,
  214. #endif
  215. };
  216. /**
  217. * @brief A codec color profile.
  218. */
  219. enum astcenc_profile {
  220. /** @brief The LDR sRGB color profile. */
  221. ASTCENC_PRF_LDR_SRGB = 0,
  222. /** @brief The LDR linear color profile. */
  223. ASTCENC_PRF_LDR,
  224. /** @brief The HDR RGB with LDR alpha color profile. */
  225. ASTCENC_PRF_HDR_RGB_LDR_A,
  226. /** @brief The HDR RGBA color profile. */
  227. ASTCENC_PRF_HDR
  228. };
  229. /** @brief The fastest, lowest quality, search preset. */
  230. static const float ASTCENC_PRE_FASTEST = 0.0f;
  231. /** @brief The fast search preset. */
  232. static const float ASTCENC_PRE_FAST = 10.0f;
  233. /** @brief The medium quality search preset. */
  234. static const float ASTCENC_PRE_MEDIUM = 60.0f;
  235. /** @brief The thorough quality search preset. */
  236. static const float ASTCENC_PRE_THOROUGH = 98.0f;
  237. /** @brief The thorough quality search preset. */
  238. static const float ASTCENC_PRE_VERYTHOROUGH = 99.0f;
  239. /** @brief The exhaustive, highest quality, search preset. */
  240. static const float ASTCENC_PRE_EXHAUSTIVE = 100.0f;
  241. /**
  242. * @brief A codec component swizzle selector.
  243. */
  244. enum astcenc_swz
  245. {
  246. /** @brief Select the red component. */
  247. ASTCENC_SWZ_R = 0,
  248. /** @brief Select the green component. */
  249. ASTCENC_SWZ_G = 1,
  250. /** @brief Select the blue component. */
  251. ASTCENC_SWZ_B = 2,
  252. /** @brief Select the alpha component. */
  253. ASTCENC_SWZ_A = 3,
  254. /** @brief Use a constant zero component. */
  255. ASTCENC_SWZ_0 = 4,
  256. /** @brief Use a constant one component. */
  257. ASTCENC_SWZ_1 = 5,
  258. /** @brief Use a reconstructed normal vector Z component. */
  259. ASTCENC_SWZ_Z = 6
  260. };
  261. /**
  262. * @brief A texel component swizzle.
  263. */
  264. struct astcenc_swizzle
  265. {
  266. /** @brief The red component selector. */
  267. astcenc_swz r;
  268. /** @brief The green component selector. */
  269. astcenc_swz g;
  270. /** @brief The blue component selector. */
  271. astcenc_swz b;
  272. /** @brief The alpha component selector. */
  273. astcenc_swz a;
  274. };
  275. /**
  276. * @brief A texel component data format.
  277. */
  278. enum astcenc_type
  279. {
  280. /** @brief Unorm 8-bit data per component. */
  281. ASTCENC_TYPE_U8 = 0,
  282. /** @brief 16-bit float per component. */
  283. ASTCENC_TYPE_F16 = 1,
  284. /** @brief 32-bit float per component. */
  285. ASTCENC_TYPE_F32 = 2
  286. };
  287. /**
  288. * @brief Enable normal map compression.
  289. *
  290. * Input data will be treated a two component normal map, storing X and Y, and the codec will
  291. * optimize for angular error rather than simple linear PSNR. In this mode the input swizzle should
  292. * be e.g. rrrg (the default ordering for ASTC normals on the command line) or gggr (the ordering
  293. * used by BC5n).
  294. */
  295. static const unsigned int ASTCENC_FLG_MAP_NORMAL = 1 << 0;
  296. /**
  297. * @brief Enable alpha weighting.
  298. *
  299. * The input alpha value is used for transparency, so errors in the RGB components are weighted by
  300. * the transparency level. This allows the codec to more accurately encode the alpha value in areas
  301. * where the color value is less significant.
  302. */
  303. static const unsigned int ASTCENC_FLG_USE_ALPHA_WEIGHT = 1 << 2;
  304. /**
  305. * @brief Enable perceptual error metrics.
  306. *
  307. * This mode enables perceptual compression mode, which will optimize for perceptual error rather
  308. * than best PSNR. Only some input modes support perceptual error metrics.
  309. */
  310. static const unsigned int ASTCENC_FLG_USE_PERCEPTUAL = 1 << 3;
  311. /**
  312. * @brief Create a decompression-only context.
  313. *
  314. * This mode disables support for compression. This enables context allocation to skip some
  315. * transient buffer allocation, resulting in lower memory usage.
  316. */
  317. static const unsigned int ASTCENC_FLG_DECOMPRESS_ONLY = 1 << 4;
  318. /**
  319. * @brief Create a self-decompression context.
  320. *
  321. * This mode configures the compressor so that it is only guaranteed to be able to decompress images
  322. * that were actually created using the current context. This is the common case for compression use
  323. * cases, and setting this flag enables additional optimizations, but does mean that the context
  324. * cannot reliably decompress arbitrary ASTC images.
  325. */
  326. static const unsigned int ASTCENC_FLG_SELF_DECOMPRESS_ONLY = 1 << 5;
  327. /**
  328. * @brief Enable RGBM map compression.
  329. *
  330. * Input data will be treated as HDR data that has been stored in an LDR RGBM-encoded wrapper
  331. * format. Data must be preprocessed by the user to be in LDR RGBM format before calling the
  332. * compression function, this flag is only used to control the use of RGBM-specific heuristics and
  333. * error metrics.
  334. *
  335. * IMPORTANT: The ASTC format is prone to bad failure modes with unconstrained RGBM data; very small
  336. * M values can round to zero due to quantization and result in black or white pixels. It is highly
  337. * recommended that the minimum value of M used in the encoding is kept above a lower threshold (try
  338. * 16 or 32). Applying this threshold reduces the number of very dark colors that can be
  339. * represented, but is still higher precision than 8-bit LDR.
  340. *
  341. * When this flag is set the value of @c rgbm_m_scale in the context must be set to the RGBM scale
  342. * factor used during reconstruction. This defaults to 5 when in RGBM mode.
  343. *
  344. * It is recommended that the value of @c cw_a_weight is set to twice the value of the multiplier
  345. * scale, ensuring that the M value is accurately encoded. This defaults to 10 when in RGBM mode,
  346. * matching the default scale factor.
  347. */
  348. static const unsigned int ASTCENC_FLG_MAP_RGBM = 1 << 6;
  349. /**
  350. * @brief The bit mask of all valid flags.
  351. */
  352. static const unsigned int ASTCENC_ALL_FLAGS =
  353. ASTCENC_FLG_MAP_NORMAL |
  354. ASTCENC_FLG_MAP_RGBM |
  355. ASTCENC_FLG_USE_ALPHA_WEIGHT |
  356. ASTCENC_FLG_USE_PERCEPTUAL |
  357. ASTCENC_FLG_DECOMPRESS_ONLY |
  358. ASTCENC_FLG_SELF_DECOMPRESS_ONLY;
  359. /**
  360. * @brief The config structure.
  361. *
  362. * This structure will initially be populated by a call to astcenc_config_init, but power users may
  363. * modify it before calling astcenc_context_alloc. See astcenccli_toplevel_help.cpp for full user
  364. * documentation of the power-user settings.
  365. *
  366. * Note for any settings which are associated with a specific color component, the value in the
  367. * config applies to the component that exists after any compression data swizzle is applied.
  368. */
  369. struct astcenc_config
  370. {
  371. /** @brief The color profile. */
  372. astcenc_profile profile;
  373. /** @brief The set of set flags. */
  374. unsigned int flags;
  375. /** @brief The ASTC block size X dimension. */
  376. unsigned int block_x;
  377. /** @brief The ASTC block size Y dimension. */
  378. unsigned int block_y;
  379. /** @brief The ASTC block size Z dimension. */
  380. unsigned int block_z;
  381. /** @brief The red component weight scale for error weighting (-cw). */
  382. float cw_r_weight;
  383. /** @brief The green component weight scale for error weighting (-cw). */
  384. float cw_g_weight;
  385. /** @brief The blue component weight scale for error weighting (-cw). */
  386. float cw_b_weight;
  387. /** @brief The alpha component weight scale for error weighting (-cw). */
  388. float cw_a_weight;
  389. /**
  390. * @brief The radius for any alpha-weight scaling (-a).
  391. *
  392. * It is recommended that this is set to 1 when using FLG_USE_ALPHA_WEIGHT on a texture that
  393. * will be sampled using linear texture filtering to minimize color bleed out of transparent
  394. * texels that are adjacent to non-transparent texels.
  395. */
  396. unsigned int a_scale_radius;
  397. /** @brief The RGBM scale factor for the shared multiplier (-rgbm). */
  398. float rgbm_m_scale;
  399. /**
  400. * @brief The maximum number of partitions searched (-partitioncountlimit).
  401. *
  402. * Valid values are between 1 and 4.
  403. */
  404. unsigned int tune_partition_count_limit;
  405. /**
  406. * @brief The maximum number of partitions searched (-2partitionindexlimit).
  407. *
  408. * Valid values are between 1 and 1024.
  409. */
  410. unsigned int tune_2partition_index_limit;
  411. /**
  412. * @brief The maximum number of partitions searched (-3partitionindexlimit).
  413. *
  414. * Valid values are between 1 and 1024.
  415. */
  416. unsigned int tune_3partition_index_limit;
  417. /**
  418. * @brief The maximum number of partitions searched (-4partitionindexlimit).
  419. *
  420. * Valid values are between 1 and 1024.
  421. */
  422. unsigned int tune_4partition_index_limit;
  423. /**
  424. * @brief The maximum centile for block modes searched (-blockmodelimit).
  425. *
  426. * Valid values are between 1 and 100.
  427. */
  428. unsigned int tune_block_mode_limit;
  429. /**
  430. * @brief The maximum iterative refinements applied (-refinementlimit).
  431. *
  432. * Valid values are between 1 and N; there is no technical upper limit
  433. * but little benefit is expected after N=4.
  434. */
  435. unsigned int tune_refinement_limit;
  436. /**
  437. * @brief The number of trial candidates per mode search (-candidatelimit).
  438. *
  439. * Valid values are between 1 and TUNE_MAX_TRIAL_CANDIDATES.
  440. */
  441. unsigned int tune_candidate_limit;
  442. /**
  443. * @brief The number of trial partitionings per search (-2partitioncandidatelimit).
  444. *
  445. * Valid values are between 1 and TUNE_MAX_PARTITIONING_CANDIDATES.
  446. */
  447. unsigned int tune_2partitioning_candidate_limit;
  448. /**
  449. * @brief The number of trial partitionings per search (-3partitioncandidatelimit).
  450. *
  451. * Valid values are between 1 and TUNE_MAX_PARTITIONING_CANDIDATES.
  452. */
  453. unsigned int tune_3partitioning_candidate_limit;
  454. /**
  455. * @brief The number of trial partitionings per search (-4partitioncandidatelimit).
  456. *
  457. * Valid values are between 1 and TUNE_MAX_PARTITIONING_CANDIDATES.
  458. */
  459. unsigned int tune_4partitioning_candidate_limit;
  460. /**
  461. * @brief The dB threshold for stopping block search (-dblimit).
  462. *
  463. * This option is ineffective for HDR textures.
  464. */
  465. float tune_db_limit;
  466. /**
  467. * @brief The amount of MSE overshoot needed to early-out trials.
  468. *
  469. * The first early-out is for 1 partition, 1 plane trials, where we try a minimal encode using
  470. * the high probability block modes. This can short-cut compression for simple blocks.
  471. *
  472. * The second early-out is for refinement trials, where we can exit refinement once quality is
  473. * reached.
  474. */
  475. float tune_mse_overshoot;
  476. /**
  477. * @brief The threshold for skipping 3.1/4.1 trials (-2partitionlimitfactor).
  478. *
  479. * This option is further scaled for normal maps, so it skips less often.
  480. */
  481. float tune_2partition_early_out_limit_factor;
  482. /**
  483. * @brief The threshold for skipping 4.1 trials (-3partitionlimitfactor).
  484. *
  485. * This option is further scaled for normal maps, so it skips less often.
  486. */
  487. float tune_3partition_early_out_limit_factor;
  488. /**
  489. * @brief The threshold for skipping two weight planes (-2planelimitcorrelation).
  490. *
  491. * This option is ineffective for normal maps.
  492. */
  493. float tune_2plane_early_out_limit_correlation;
  494. #if defined(ASTCENC_DIAGNOSTICS)
  495. /**
  496. * @brief The path to save the diagnostic trace data to.
  497. *
  498. * This option is not part of the public API, and requires special builds
  499. * of the library.
  500. */
  501. const char* trace_file_path;
  502. #endif
  503. };
  504. /**
  505. * @brief An uncompressed 2D or 3D image.
  506. *
  507. * 3D image are passed in as an array of 2D slices. Each slice has identical
  508. * size and color format.
  509. */
  510. struct astcenc_image
  511. {
  512. /** @brief The X dimension of the image, in texels. */
  513. unsigned int dim_x;
  514. /** @brief The Y dimension of the image, in texels. */
  515. unsigned int dim_y;
  516. /** @brief The Z dimension of the image, in texels. */
  517. unsigned int dim_z;
  518. /** @brief The data type per component. */
  519. astcenc_type data_type;
  520. /** @brief The array of 2D slices, of length @c dim_z. */
  521. void** data;
  522. };
  523. /**
  524. * @brief A block encoding metadata query result.
  525. *
  526. * If the block is an error block or a constant color block or an error block all fields other than
  527. * the profile, block dimensions, and error/constant indicator will be zero.
  528. */
  529. struct astcenc_block_info
  530. {
  531. /** @brief The block encoding color profile. */
  532. astcenc_profile profile;
  533. /** @brief The number of texels in the X dimension. */
  534. unsigned int block_x;
  535. /** @brief The number of texels in the Y dimension. */
  536. unsigned int block_y;
  537. /** @brief The number of texel in the Z dimension. */
  538. unsigned int block_z;
  539. /** @brief The number of texels in the block. */
  540. unsigned int texel_count;
  541. /** @brief True if this block is an error block. */
  542. bool is_error_block;
  543. /** @brief True if this block is a constant color block. */
  544. bool is_constant_block;
  545. /** @brief True if this block is an HDR block. */
  546. bool is_hdr_block;
  547. /** @brief True if this block uses two weight planes. */
  548. bool is_dual_plane_block;
  549. /** @brief The number of partitions if not constant color. */
  550. unsigned int partition_count;
  551. /** @brief The partition index if 2 - 4 partitions used. */
  552. unsigned int partition_index;
  553. /** @brief The component index of the second plane if dual plane. */
  554. unsigned int dual_plane_component;
  555. /** @brief The color endpoint encoding mode for each partition. */
  556. unsigned int color_endpoint_modes[4];
  557. /** @brief The number of color endpoint quantization levels. */
  558. unsigned int color_level_count;
  559. /** @brief The number of weight quantization levels. */
  560. unsigned int weight_level_count;
  561. /** @brief The number of weights in the X dimension. */
  562. unsigned int weight_x;
  563. /** @brief The number of weights in the Y dimension. */
  564. unsigned int weight_y;
  565. /** @brief The number of weights in the Z dimension. */
  566. unsigned int weight_z;
  567. /** @brief The unpacked color endpoints for each partition. */
  568. float color_endpoints[4][2][4];
  569. /** @brief The per-texel interpolation weights for the block. */
  570. float weight_values_plane1[216];
  571. /** @brief The per-texel interpolation weights for the block. */
  572. float weight_values_plane2[216];
  573. /** @brief The per-texel partition assignments for the block. */
  574. uint8_t partition_assignment[216];
  575. };
  576. /**
  577. * Populate a codec config based on default settings.
  578. *
  579. * Power users can edit the returned config struct to fine tune before allocating the context.
  580. *
  581. * @param profile Color profile.
  582. * @param block_x ASTC block size X dimension.
  583. * @param block_y ASTC block size Y dimension.
  584. * @param block_z ASTC block size Z dimension.
  585. * @param quality Search quality preset / effort level. Either an
  586. * @c ASTCENC_PRE_* value, or a effort level between 0
  587. * and 100. Performance is not linear between 0 and 100.
  588. * @param flags A valid set of @c ASTCENC_FLG_* flag bits.
  589. * @param[out] config Output config struct to populate.
  590. *
  591. * @return @c ASTCENC_SUCCESS on success, or an error if the inputs are invalid
  592. * either individually, or in combination.
  593. */
  594. ASTCENC_PUBLIC astcenc_error astcenc_config_init(
  595. astcenc_profile profile,
  596. unsigned int block_x,
  597. unsigned int block_y,
  598. unsigned int block_z,
  599. float quality,
  600. unsigned int flags,
  601. astcenc_config* config);
  602. /**
  603. * @brief Allocate a new codec context based on a config.
  604. *
  605. * This function allocates all of the memory resources and threads needed by the codec. This can be
  606. * slow, so it is recommended that contexts are reused to serially compress or decompress multiple
  607. * images to amortize setup cost.
  608. *
  609. * Contexts can be allocated to support only decompression using the @c ASTCENC_FLG_DECOMPRESS_ONLY
  610. * flag when creating the configuration. The compression functions will fail if invoked. For a
  611. * decompress-only library build the @c ASTCENC_FLG_DECOMPRESS_ONLY flag must be set when creating
  612. * any context.
  613. *
  614. * @param[in] config Codec config.
  615. * @param thread_count Thread count to configure for.
  616. * @param[out] context Location to store an opaque context pointer.
  617. *
  618. * @return @c ASTCENC_SUCCESS on success, or an error if context creation failed.
  619. */
  620. ASTCENC_PUBLIC astcenc_error astcenc_context_alloc(
  621. const astcenc_config* config,
  622. unsigned int thread_count,
  623. astcenc_context** context);
  624. /**
  625. * @brief Compress an image.
  626. *
  627. * A single context can only compress or decompress a single image at a time.
  628. *
  629. * For a context configured for multi-threading, any set of the N threads can call this function.
  630. * Work will be dynamically scheduled across the threads available. Each thread must have a unique
  631. * @c thread_index.
  632. *
  633. * @param context Codec context.
  634. * @param[in,out] image An input image, in 2D slices.
  635. * @param swizzle Compression data swizzle, applied before compression.
  636. * @param[out] data_out Pointer to output data array.
  637. * @param data_len Length of the output data array.
  638. * @param thread_index Thread index [0..N-1] of calling thread.
  639. *
  640. * @return @c ASTCENC_SUCCESS on success, or an error if compression failed.
  641. */
  642. ASTCENC_PUBLIC astcenc_error astcenc_compress_image(
  643. astcenc_context* context,
  644. astcenc_image* image,
  645. const astcenc_swizzle* swizzle,
  646. uint8_t* data_out,
  647. size_t data_len,
  648. unsigned int thread_index);
  649. /**
  650. * @brief Reset the codec state for a new compression.
  651. *
  652. * The caller is responsible for synchronizing threads in the worker thread pool. This function must
  653. * only be called when all threads have exited the @c astcenc_compress_image() function for image N,
  654. * but before any thread enters it for image N + 1.
  655. *
  656. * Calling this is not required (but won't hurt), if the context is created for single threaded use.
  657. *
  658. * @param context Codec context.
  659. *
  660. * @return @c ASTCENC_SUCCESS on success, or an error if reset failed.
  661. */
  662. ASTCENC_PUBLIC astcenc_error astcenc_compress_reset(
  663. astcenc_context* context);
  664. /**
  665. * @brief Decompress an image.
  666. *
  667. * @param context Codec context.
  668. * @param[in] data Pointer to compressed data.
  669. * @param data_len Length of the compressed data, in bytes.
  670. * @param[in,out] image_out Output image.
  671. * @param swizzle Decompression data swizzle, applied after decompression.
  672. * @param thread_index Thread index [0..N-1] of calling thread.
  673. *
  674. * @return @c ASTCENC_SUCCESS on success, or an error if decompression failed.
  675. */
  676. ASTCENC_PUBLIC astcenc_error astcenc_decompress_image(
  677. astcenc_context* context,
  678. const uint8_t* data,
  679. size_t data_len,
  680. astcenc_image* image_out,
  681. const astcenc_swizzle* swizzle,
  682. unsigned int thread_index);
  683. /**
  684. * @brief Reset the codec state for a new decompression.
  685. *
  686. * The caller is responsible for synchronizing threads in the worker thread pool. This function must
  687. * only be called when all threads have exited the @c astcenc_decompress_image() function for image
  688. * N, but before any thread enters it for image N + 1.
  689. *
  690. * Calling this is not required (but won't hurt), if the context is created for single threaded use.
  691. *
  692. * @param context Codec context.
  693. *
  694. * @return @c ASTCENC_SUCCESS on success, or an error if reset failed.
  695. */
  696. ASTCENC_PUBLIC astcenc_error astcenc_decompress_reset(
  697. astcenc_context* context);
  698. /**
  699. * Free the compressor context.
  700. *
  701. * @param context The codec context.
  702. */
  703. ASTCENC_PUBLIC void astcenc_context_free(
  704. astcenc_context* context);
  705. /**
  706. * @brief Provide a high level summary of a block's encoding.
  707. *
  708. * This feature is primarily useful for codec developers but may be useful for developers building
  709. * advanced content packaging pipelines.
  710. *
  711. * @param context Codec context.
  712. * @param data One block of compressed ASTC data.
  713. * @param info The output info structure to populate.
  714. *
  715. * @return @c ASTCENC_SUCCESS if the block was decoded, or an error otherwise. Note that this
  716. * function will return success even if the block itself was an error block encoding, as the
  717. * decode was correctly handled.
  718. */
  719. ASTCENC_PUBLIC astcenc_error astcenc_get_block_info(
  720. astcenc_context* context,
  721. const uint8_t data[16],
  722. astcenc_block_info* info);
  723. /**
  724. * @brief Get a printable string for specific status code.
  725. *
  726. * @param status The status value.
  727. *
  728. * @return A human readable nul-terminated string.
  729. */
  730. ASTCENC_PUBLIC const char* astcenc_get_error_string(
  731. astcenc_error status);
  732. #endif