basisu_comp.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. // basisu_comp.h
  2. // Copyright (C) 2019-2021 Binomial LLC. All Rights Reserved.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #pragma once
  16. #include "basisu_frontend.h"
  17. #include "basisu_backend.h"
  18. #include "basisu_basis_file.h"
  19. #include "../transcoder/basisu_transcoder.h"
  20. #include "basisu_uastc_enc.h"
  21. #define BASISU_LIB_VERSION 116
  22. #define BASISU_LIB_VERSION_STRING "1.16"
  23. #ifndef BASISD_SUPPORT_KTX2
  24. #error BASISD_SUPPORT_KTX2 is undefined
  25. #endif
  26. #ifndef BASISD_SUPPORT_KTX2_ZSTD
  27. #error BASISD_SUPPORT_KTX2_ZSTD is undefined
  28. #endif
  29. #if !BASISD_SUPPORT_KTX2
  30. #error BASISD_SUPPORT_KTX2 must be enabled when building the encoder. To reduce code size if KTX2 support is not needed, set BASISD_SUPPORT_KTX2_ZSTD to 0
  31. #endif
  32. namespace basisu
  33. {
  34. struct opencl_context;
  35. typedef opencl_context* opencl_context_ptr;
  36. const uint32_t BASISU_MAX_SUPPORTED_TEXTURE_DIMENSION = 16384;
  37. // Allow block's color distance to increase by 1.5 while searching for an alternative nearby endpoint.
  38. const float BASISU_DEFAULT_ENDPOINT_RDO_THRESH = 1.5f;
  39. // Allow block's color distance to increase by 1.25 while searching the selector history buffer for a close enough match.
  40. const float BASISU_DEFAULT_SELECTOR_RDO_THRESH = 1.25f;
  41. const int BASISU_DEFAULT_QUALITY = 128;
  42. const float BASISU_DEFAULT_HYBRID_SEL_CB_QUALITY_THRESH = 2.0f;
  43. const uint32_t BASISU_MAX_IMAGE_DIMENSION = 16384;
  44. const uint32_t BASISU_QUALITY_MIN = 1;
  45. const uint32_t BASISU_QUALITY_MAX = 255;
  46. const uint32_t BASISU_MAX_ENDPOINT_CLUSTERS = basisu_frontend::cMaxEndpointClusters;
  47. const uint32_t BASISU_MAX_SELECTOR_CLUSTERS = basisu_frontend::cMaxSelectorClusters;
  48. const uint32_t BASISU_MAX_SLICES = 0xFFFFFF;
  49. const int BASISU_RDO_UASTC_DICT_SIZE_DEFAULT = 4096; // 32768;
  50. const int BASISU_RDO_UASTC_DICT_SIZE_MIN = 64;
  51. const int BASISU_RDO_UASTC_DICT_SIZE_MAX = 65536;
  52. struct image_stats
  53. {
  54. image_stats()
  55. {
  56. clear();
  57. }
  58. void clear()
  59. {
  60. m_filename.clear();
  61. m_width = 0;
  62. m_height = 0;
  63. m_basis_rgb_avg_psnr = 0.0f;
  64. m_basis_rgba_avg_psnr = 0.0f;
  65. m_basis_a_avg_psnr = 0.0f;
  66. m_basis_luma_709_psnr = 0.0f;
  67. m_basis_luma_601_psnr = 0.0f;
  68. m_basis_luma_709_ssim = 0.0f;
  69. m_bc7_rgb_avg_psnr = 0.0f;
  70. m_bc7_rgba_avg_psnr = 0.0f;
  71. m_bc7_a_avg_psnr = 0.0f;
  72. m_bc7_luma_709_psnr = 0.0f;
  73. m_bc7_luma_601_psnr = 0.0f;
  74. m_bc7_luma_709_ssim = 0.0f;
  75. m_best_etc1s_rgb_avg_psnr = 0.0f;
  76. m_best_etc1s_luma_709_psnr = 0.0f;
  77. m_best_etc1s_luma_601_psnr = 0.0f;
  78. m_best_etc1s_luma_709_ssim = 0.0f;
  79. }
  80. std::string m_filename;
  81. uint32_t m_width;
  82. uint32_t m_height;
  83. // .basis compressed (ETC1S or UASTC statistics)
  84. float m_basis_rgb_avg_psnr;
  85. float m_basis_rgba_avg_psnr;
  86. float m_basis_a_avg_psnr;
  87. float m_basis_luma_709_psnr;
  88. float m_basis_luma_601_psnr;
  89. float m_basis_luma_709_ssim;
  90. // BC7 statistics
  91. float m_bc7_rgb_avg_psnr;
  92. float m_bc7_rgba_avg_psnr;
  93. float m_bc7_a_avg_psnr;
  94. float m_bc7_luma_709_psnr;
  95. float m_bc7_luma_601_psnr;
  96. float m_bc7_luma_709_ssim;
  97. // Highest achievable quality ETC1S statistics
  98. float m_best_etc1s_rgb_avg_psnr;
  99. float m_best_etc1s_luma_709_psnr;
  100. float m_best_etc1s_luma_601_psnr;
  101. float m_best_etc1s_luma_709_ssim;
  102. };
  103. template<bool def>
  104. struct bool_param
  105. {
  106. bool_param() :
  107. m_value(def),
  108. m_changed(false)
  109. {
  110. }
  111. void clear()
  112. {
  113. m_value = def;
  114. m_changed = false;
  115. }
  116. operator bool() const
  117. {
  118. return m_value;
  119. }
  120. bool operator= (bool v)
  121. {
  122. m_value = v;
  123. m_changed = true;
  124. return m_value;
  125. }
  126. bool was_changed() const { return m_changed; }
  127. void set_changed(bool flag) { m_changed = flag; }
  128. bool m_value;
  129. bool m_changed;
  130. };
  131. template<typename T>
  132. struct param
  133. {
  134. param(T def, T min_v, T max_v) :
  135. m_value(def),
  136. m_def(def),
  137. m_min(min_v),
  138. m_max(max_v),
  139. m_changed(false)
  140. {
  141. }
  142. void clear()
  143. {
  144. m_value = m_def;
  145. m_changed = false;
  146. }
  147. operator T() const
  148. {
  149. return m_value;
  150. }
  151. T operator= (T v)
  152. {
  153. m_value = clamp<T>(v, m_min, m_max);
  154. m_changed = true;
  155. return m_value;
  156. }
  157. T operator *= (T v)
  158. {
  159. m_value *= v;
  160. m_changed = true;
  161. return m_value;
  162. }
  163. bool was_changed() const { return m_changed; }
  164. void set_changed(bool flag) { m_changed = flag; }
  165. T m_value;
  166. T m_def;
  167. T m_min;
  168. T m_max;
  169. bool m_changed;
  170. };
  171. struct basis_compressor_params
  172. {
  173. basis_compressor_params() :
  174. m_compression_level((int)BASISU_DEFAULT_COMPRESSION_LEVEL, 0, (int)BASISU_MAX_COMPRESSION_LEVEL),
  175. m_selector_rdo_thresh(BASISU_DEFAULT_SELECTOR_RDO_THRESH, 0.0f, 1e+10f),
  176. m_endpoint_rdo_thresh(BASISU_DEFAULT_ENDPOINT_RDO_THRESH, 0.0f, 1e+10f),
  177. m_mip_scale(1.0f, .000125f, 4.0f),
  178. m_mip_smallest_dimension(1, 1, 16384),
  179. m_max_endpoint_clusters(512),
  180. m_max_selector_clusters(512),
  181. m_quality_level(-1),
  182. m_pack_uastc_flags(cPackUASTCLevelDefault),
  183. m_rdo_uastc_quality_scalar(1.0f, 0.001f, 50.0f),
  184. m_rdo_uastc_dict_size(BASISU_RDO_UASTC_DICT_SIZE_DEFAULT, BASISU_RDO_UASTC_DICT_SIZE_MIN, BASISU_RDO_UASTC_DICT_SIZE_MAX),
  185. m_rdo_uastc_max_smooth_block_error_scale(UASTC_RDO_DEFAULT_SMOOTH_BLOCK_MAX_ERROR_SCALE, 1.0f, 300.0f),
  186. m_rdo_uastc_smooth_block_max_std_dev(UASTC_RDO_DEFAULT_MAX_SMOOTH_BLOCK_STD_DEV, .01f, 65536.0f),
  187. m_rdo_uastc_max_allowed_rms_increase_ratio(UASTC_RDO_DEFAULT_MAX_ALLOWED_RMS_INCREASE_RATIO, .01f, 100.0f),
  188. m_rdo_uastc_skip_block_rms_thresh(UASTC_RDO_DEFAULT_SKIP_BLOCK_RMS_THRESH, .01f, 100.0f),
  189. m_resample_width(0, 1, 16384),
  190. m_resample_height(0, 1, 16384),
  191. m_resample_factor(0.0f, .00125f, 100.0f),
  192. m_ktx2_uastc_supercompression(basist::KTX2_SS_NONE),
  193. m_ktx2_zstd_supercompression_level(6, INT_MIN, INT_MAX),
  194. m_pJob_pool(nullptr)
  195. {
  196. clear();
  197. }
  198. void clear()
  199. {
  200. m_uastc.clear();
  201. m_use_opencl.clear();
  202. m_status_output.clear();
  203. m_source_filenames.clear();
  204. m_source_alpha_filenames.clear();
  205. m_source_images.clear();
  206. m_source_mipmap_images.clear();
  207. m_out_filename.clear();
  208. m_y_flip.clear();
  209. m_debug.clear();
  210. m_validate_etc1s.clear();
  211. m_debug_images.clear();
  212. m_perceptual.clear();
  213. m_no_selector_rdo.clear();
  214. m_selector_rdo_thresh.clear();
  215. m_read_source_images.clear();
  216. m_write_output_basis_files.clear();
  217. m_compression_level.clear();
  218. m_compute_stats.clear();
  219. m_check_for_alpha.clear();
  220. m_force_alpha.clear();
  221. m_multithreading.clear();
  222. m_swizzle[0] = 0;
  223. m_swizzle[1] = 1;
  224. m_swizzle[2] = 2;
  225. m_swizzle[3] = 3;
  226. m_renormalize.clear();
  227. m_disable_hierarchical_endpoint_codebooks.clear();
  228. m_no_endpoint_rdo.clear();
  229. m_endpoint_rdo_thresh.clear();
  230. m_mip_gen.clear();
  231. m_mip_scale.clear();
  232. m_mip_filter = "kaiser";
  233. m_mip_scale = 1.0f;
  234. m_mip_srgb.clear();
  235. m_mip_premultiplied.clear();
  236. m_mip_renormalize.clear();
  237. m_mip_wrapping.clear();
  238. m_mip_fast.clear();
  239. m_mip_smallest_dimension.clear();
  240. m_max_endpoint_clusters = 0;
  241. m_max_selector_clusters = 0;
  242. m_quality_level = -1;
  243. m_tex_type = basist::cBASISTexType2D;
  244. m_userdata0 = 0;
  245. m_userdata1 = 0;
  246. m_us_per_frame = 0;
  247. m_pack_uastc_flags = cPackUASTCLevelDefault;
  248. m_rdo_uastc.clear();
  249. m_rdo_uastc_quality_scalar.clear();
  250. m_rdo_uastc_max_smooth_block_error_scale.clear();
  251. m_rdo_uastc_smooth_block_max_std_dev.clear();
  252. m_rdo_uastc_max_allowed_rms_increase_ratio.clear();
  253. m_rdo_uastc_skip_block_rms_thresh.clear();
  254. m_rdo_uastc_favor_simpler_modes_in_rdo_mode.clear();
  255. m_rdo_uastc_multithreading.clear();
  256. m_resample_width.clear();
  257. m_resample_height.clear();
  258. m_resample_factor.clear();
  259. m_pGlobal_codebooks = nullptr;
  260. m_create_ktx2_file.clear();
  261. m_ktx2_uastc_supercompression = basist::KTX2_SS_NONE;
  262. m_ktx2_key_values.clear();
  263. m_ktx2_zstd_supercompression_level.clear();
  264. m_ktx2_srgb_transfer_func.clear();
  265. m_validate_output_data.clear();
  266. m_pJob_pool = nullptr;
  267. }
  268. // True to generate UASTC .basis file data, otherwise ETC1S.
  269. bool_param<false> m_uastc;
  270. bool_param<false> m_use_opencl;
  271. // If m_read_source_images is true, m_source_filenames (and optionally m_source_alpha_filenames) contains the filenames of PNG images to read.
  272. // Otherwise, the compressor processes the images in m_source_images.
  273. basisu::vector<std::string> m_source_filenames;
  274. basisu::vector<std::string> m_source_alpha_filenames;
  275. basisu::vector<image> m_source_images;
  276. // Stores mipmaps starting from level 1. Level 0 is still stored in m_source_images, as usual.
  277. // If m_source_mipmaps isn't empty, automatic mipmap generation isn't done. m_source_mipmaps.size() MUST equal m_source_images.size() or the compressor returns an error.
  278. // The compressor applies the user-provided swizzling (in m_swizzle) to these images.
  279. basisu::vector< basisu::vector<image> > m_source_mipmap_images;
  280. // Filename of the output basis file
  281. std::string m_out_filename;
  282. // The params are done this way so we can detect when the user has explictly changed them.
  283. // Flip images across Y axis
  284. bool_param<false> m_y_flip;
  285. // If true, the compressor will print basis status to stdout during compression.
  286. bool_param<true> m_status_output;
  287. // Output debug information during compression
  288. bool_param<false> m_debug;
  289. bool_param<false> m_validate_etc1s;
  290. // m_debug_images is pretty slow
  291. bool_param<false> m_debug_images;
  292. // ETC1S compression level, from 0 to BASISU_MAX_COMPRESSION_LEVEL (higher is slower).
  293. // This parameter controls numerous internal encoding speed vs. compression efficiency/performance tradeoffs.
  294. // Note this is NOT the same as the ETC1S quality level, and most users shouldn't change this.
  295. param<int> m_compression_level;
  296. // Use perceptual sRGB colorspace metrics instead of linear
  297. bool_param<true> m_perceptual;
  298. // Disable selector RDO, for faster compression but larger files
  299. bool_param<false> m_no_selector_rdo;
  300. param<float> m_selector_rdo_thresh;
  301. bool_param<false> m_no_endpoint_rdo;
  302. param<float> m_endpoint_rdo_thresh;
  303. // Read source images from m_source_filenames/m_source_alpha_filenames
  304. bool_param<false> m_read_source_images;
  305. // Write the output basis file to disk using m_out_filename
  306. bool_param<false> m_write_output_basis_files;
  307. // Compute and display image metrics
  308. bool_param<false> m_compute_stats;
  309. // Check to see if any input image has an alpha channel, if so then the output basis file will have alpha channels
  310. bool_param<true> m_check_for_alpha;
  311. // Always put alpha slices in the output basis file, even when the input doesn't have alpha
  312. bool_param<false> m_force_alpha;
  313. bool_param<true> m_multithreading;
  314. // Split the R channel to RGB and the G channel to alpha, then write a basis file with alpha channels
  315. char m_swizzle[4];
  316. bool_param<false> m_renormalize;
  317. // If true the front end will not use 2 level endpoint codebook searching, for slightly higher quality but much slower execution.
  318. // Note some m_compression_level's disable this automatically.
  319. bool_param<false> m_disable_hierarchical_endpoint_codebooks;
  320. // mipmap generation parameters
  321. bool_param<false> m_mip_gen;
  322. param<float> m_mip_scale;
  323. std::string m_mip_filter;
  324. bool_param<false> m_mip_srgb;
  325. bool_param<true> m_mip_premultiplied; // not currently supported
  326. bool_param<false> m_mip_renormalize;
  327. bool_param<true> m_mip_wrapping;
  328. bool_param<true> m_mip_fast;
  329. param<int> m_mip_smallest_dimension;
  330. // Codebook size (quality) control.
  331. // If m_quality_level != -1, it controls the quality level. It ranges from [1,255] or [BASISU_QUALITY_MIN, BASISU_QUALITY_MAX].
  332. // Otherwise m_max_endpoint_clusters/m_max_selector_clusters controls the codebook sizes directly.
  333. uint32_t m_max_endpoint_clusters;
  334. uint32_t m_max_selector_clusters;
  335. int m_quality_level;
  336. // m_tex_type, m_userdata0, m_userdata1, m_framerate - These fields go directly into the Basis file header.
  337. basist::basis_texture_type m_tex_type;
  338. uint32_t m_userdata0;
  339. uint32_t m_userdata1;
  340. uint32_t m_us_per_frame;
  341. // cPackUASTCLevelDefault, etc.
  342. uint32_t m_pack_uastc_flags;
  343. bool_param<false> m_rdo_uastc;
  344. param<float> m_rdo_uastc_quality_scalar;
  345. param<int> m_rdo_uastc_dict_size;
  346. param<float> m_rdo_uastc_max_smooth_block_error_scale;
  347. param<float> m_rdo_uastc_smooth_block_max_std_dev;
  348. param<float> m_rdo_uastc_max_allowed_rms_increase_ratio;
  349. param<float> m_rdo_uastc_skip_block_rms_thresh;
  350. bool_param<true> m_rdo_uastc_favor_simpler_modes_in_rdo_mode;
  351. bool_param<true> m_rdo_uastc_multithreading;
  352. param<int> m_resample_width;
  353. param<int> m_resample_height;
  354. param<float> m_resample_factor;
  355. const basist::basisu_lowlevel_etc1s_transcoder *m_pGlobal_codebooks;
  356. // KTX2 specific parameters.
  357. // Internally, the compressor always creates a .basis file then it converts that lossless to KTX2.
  358. bool_param<false> m_create_ktx2_file;
  359. basist::ktx2_supercompression m_ktx2_uastc_supercompression;
  360. basist::ktx2_transcoder::key_value_vec m_ktx2_key_values;
  361. param<int> m_ktx2_zstd_supercompression_level;
  362. bool_param<false> m_ktx2_srgb_transfer_func;
  363. bool_param<false> m_validate_output_data;
  364. job_pool *m_pJob_pool;
  365. };
  366. // Important: basisu_encoder_init() MUST be called first before using this class.
  367. class basis_compressor
  368. {
  369. BASISU_NO_EQUALS_OR_COPY_CONSTRUCT(basis_compressor);
  370. public:
  371. basis_compressor();
  372. ~basis_compressor();
  373. // Note it *should* be possible to call init() multiple times with different inputs, but this scenario isn't well tested. Ideally, create 1 object, compress, then delete it.
  374. bool init(const basis_compressor_params &params);
  375. enum error_code
  376. {
  377. cECSuccess = 0,
  378. cECFailedInitializing,
  379. cECFailedReadingSourceImages,
  380. cECFailedValidating,
  381. cECFailedEncodeUASTC,
  382. cECFailedFrontEnd,
  383. cECFailedFontendExtract,
  384. cECFailedBackend,
  385. cECFailedCreateBasisFile,
  386. cECFailedWritingOutput,
  387. cECFailedUASTCRDOPostProcess,
  388. cECFailedCreateKTX2File
  389. };
  390. error_code process();
  391. // The output .basis file will always be valid of process() succeeded.
  392. const uint8_vec &get_output_basis_file() const { return m_output_basis_file; }
  393. // The output .ktx2 file will only be valid if m_create_ktx2_file was true and process() succeeded.
  394. const uint8_vec& get_output_ktx2_file() const { return m_output_ktx2_file; }
  395. const basisu::vector<image_stats> &get_stats() const { return m_stats; }
  396. uint32_t get_basis_file_size() const { return m_basis_file_size; }
  397. double get_basis_bits_per_texel() const { return m_basis_bits_per_texel; }
  398. bool get_any_source_image_has_alpha() const { return m_any_source_image_has_alpha; }
  399. bool get_opencl_failed() const { return m_opencl_failed; }
  400. private:
  401. basis_compressor_params m_params;
  402. opencl_context_ptr m_pOpenCL_context;
  403. basisu::vector<image> m_slice_images;
  404. basisu::vector<image_stats> m_stats;
  405. uint32_t m_basis_file_size;
  406. double m_basis_bits_per_texel;
  407. basisu_backend_slice_desc_vec m_slice_descs;
  408. uint32_t m_total_blocks;
  409. basisu_frontend m_frontend;
  410. pixel_block_vec m_source_blocks;
  411. basisu::vector<gpu_image> m_frontend_output_textures;
  412. basisu::vector<gpu_image> m_best_etc1s_images;
  413. basisu::vector<image> m_best_etc1s_images_unpacked;
  414. basisu_backend m_backend;
  415. basisu_file m_basis_file;
  416. basisu::vector<gpu_image> m_decoded_output_textures;
  417. basisu::vector<image> m_decoded_output_textures_unpacked;
  418. basisu::vector<gpu_image> m_decoded_output_textures_bc7;
  419. basisu::vector<image> m_decoded_output_textures_unpacked_bc7;
  420. uint8_vec m_output_basis_file;
  421. uint8_vec m_output_ktx2_file;
  422. basisu::vector<gpu_image> m_uastc_slice_textures;
  423. basisu_backend_output m_uastc_backend_output;
  424. bool m_any_source_image_has_alpha;
  425. bool m_opencl_failed;
  426. bool read_source_images();
  427. bool extract_source_blocks();
  428. bool process_frontend();
  429. bool extract_frontend_texture_data();
  430. bool process_backend();
  431. bool create_basis_file_and_transcode();
  432. bool write_output_files_and_compute_stats();
  433. error_code encode_slices_to_uastc();
  434. bool generate_mipmaps(const image &img, basisu::vector<image> &mips, bool has_alpha);
  435. bool validate_texture_type_constraints();
  436. bool validate_ktx2_constraints();
  437. void get_dfd(uint8_vec& dfd, const basist::ktx2_header& hdr);
  438. bool create_ktx2_file();
  439. };
  440. // Alternative simple C-style wrapper API around the basis_compressor class.
  441. // This doesn't expose every encoder feature, but it's enough to get going.
  442. // Important: basisu_encoder_init() MUST be called first before calling these functions.
  443. //
  444. // Input parameters:
  445. // source_images: Array of "image" objects, one per mipmap level, largest mipmap level first.
  446. // OR
  447. // pImageRGBA: pointer to a 32-bpp RGBx or RGBA raster image, R first in memory, A last. Top scanline first in memory.
  448. // width/height/pitch_in_pixels: dimensions of pImageRGBA
  449. //
  450. // flags_and_quality: Combination of the above flags logically OR'd with the ETC1S or UASTC level, i.e. "cFlagSRGB | cFlagGenMipsClamp | cFlagThreaded | 128" or "cFlagSRGB | cFlagGenMipsClamp | cFlagUASTC | cFlagThreaded | cPackUASTCLevelDefault".
  451. // In ETC1S mode, the lower 8-bits are the ETC1S quality level which ranges from [1,255] (higher=better quality/larger files)
  452. // In UASTC mode, the lower 8-bits are the UASTC pack level (see cPackUASTCLevelFastest, etc.). Fastest/lowest quality is 0, so be sure to set it correctly.
  453. //
  454. // uastc_rdo_quality: Float UASTC RDO quality level (0=no change, higher values lower quality but increase compressibility, initially try .5-1.5)
  455. //
  456. // pSize: Returns the output data's compressed size in bytes
  457. //
  458. // Return value is the compressed .basis or .ktx2 file data, or nullptr on failure. Must call basis_free() to free it.
  459. enum
  460. {
  461. cFlagUseOpenCL = 1 << 8, // use OpenCL if available
  462. cFlagThreaded = 1 << 9, // use multiple threads for compression
  463. cFlagDebug = 1 << 10, // enable debug output
  464. cFlagKTX2 = 1 << 11, // generate a KTX2 file
  465. cFlagKTX2UASTCSuperCompression = 1 << 12, // use KTX2 Zstd supercompression on UASTC files
  466. cFlagSRGB = 1 << 13, // input texture is sRGB, use perceptual colorspace metrics, also use sRGB filtering during mipmap gen, and also sets KTX2 output transfer func to sRGB
  467. cFlagGenMipsClamp = 1 << 14, // generate mipmaps with clamp addressing
  468. cFlagGenMipsWrap = 1 << 15, // generate mipmaps with wrap addressing
  469. cFlagYFlip = 1 << 16, // flip source image on Y axis before compression
  470. cFlagUASTC = 1 << 17, // use UASTC compression vs. ETC1S
  471. cFlagUASTCRDO = 1 << 18 // use RDO postprocessing when generating UASTC files (must set uastc_rdo_quality to the quality scalar)
  472. };
  473. // This function accepts an array of source images.
  474. // If more than one image is provided, it's assumed the images form a mipmap pyramid and automatic mipmap generation is disabled.
  475. void* basis_compress(
  476. const basisu::vector<image> &source_images,
  477. uint32_t flags_and_quality, float uastc_rdo_quality,
  478. size_t* pSize,
  479. image_stats* pStats = nullptr);
  480. // This function only accepts a single source image.
  481. void* basis_compress(
  482. const uint8_t* pImageRGBA, uint32_t width, uint32_t height, uint32_t pitch_in_pixels,
  483. uint32_t flags_and_quality, float uastc_rdo_quality,
  484. size_t* pSize,
  485. image_stats* pStats = nullptr);
  486. // Frees the dynamically allocated file data returned by basis_compress().
  487. void basis_free_data(void* p);
  488. // Parallel compression API
  489. struct parallel_results
  490. {
  491. double m_total_time;
  492. basis_compressor::error_code m_error_code;
  493. uint8_vec m_basis_file;
  494. uint8_vec m_ktx2_file;
  495. basisu::vector<image_stats> m_stats;
  496. double m_basis_bits_per_texel;
  497. bool m_any_source_image_has_alpha;
  498. parallel_results()
  499. {
  500. clear();
  501. }
  502. void clear()
  503. {
  504. m_total_time = 0.0f;
  505. m_error_code = basis_compressor::cECFailedInitializing;
  506. m_basis_file.clear();
  507. m_ktx2_file.clear();
  508. m_stats.clear();
  509. m_basis_bits_per_texel = 0.0f;
  510. m_any_source_image_has_alpha = false;
  511. }
  512. };
  513. // Compresses an array of input textures across total_threads threads using the basis_compressor class.
  514. // Compressing multiple textures at a time is substantially more efficient than just compressing one at a time.
  515. // total_threads must be >= 1.
  516. bool basis_parallel_compress(
  517. uint32_t total_threads,
  518. const basisu::vector<basis_compressor_params> &params_vec,
  519. basisu::vector< parallel_results > &results_vec);
  520. } // namespace basisu