image_compress_basisu.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /**************************************************************************/
  2. /* image_compress_basisu.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "image_compress_basisu.h"
  31. #include "core/os/os.h"
  32. #include "core/string/print_string.h"
  33. #include "servers/rendering_server.h"
  34. #include <transcoder/basisu_transcoder.h>
  35. #ifdef TOOLS_ENABLED
  36. #include <encoder/basisu_comp.h>
  37. #endif
  38. void basis_universal_init() {
  39. #ifdef TOOLS_ENABLED
  40. basisu::basisu_encoder_init();
  41. #endif
  42. basist::basisu_transcoder_init();
  43. }
  44. #ifdef TOOLS_ENABLED
  45. template <typename T>
  46. inline void _basisu_pad_mipmap(const uint8_t *p_image_mip_data, Vector<uint8_t> &r_mip_data_padded, int p_next_width, int p_next_height, int p_width, int p_height, int64_t p_size) {
  47. // Source mip's data interpreted as 32-bit RGBA blocks to help with copying pixel data.
  48. const T *mip_src_data = reinterpret_cast<const T *>(p_image_mip_data);
  49. // Reserve space in the padded buffer.
  50. r_mip_data_padded.resize(p_next_width * p_next_height * sizeof(T));
  51. T *data_padded_ptr = reinterpret_cast<T *>(r_mip_data_padded.ptrw());
  52. // Pad mipmap to the nearest block by smearing.
  53. int x = 0, y = 0;
  54. for (y = 0; y < p_height; y++) {
  55. for (x = 0; x < p_width; x++) {
  56. data_padded_ptr[p_next_width * y + x] = mip_src_data[p_width * y + x];
  57. }
  58. // First, smear in x.
  59. for (; x < p_next_width; x++) {
  60. data_padded_ptr[p_next_width * y + x] = data_padded_ptr[p_next_width * y + x - 1];
  61. }
  62. }
  63. // Then, smear in y.
  64. for (; y < p_next_height; y++) {
  65. for (x = 0; x < p_next_width; x++) {
  66. data_padded_ptr[p_next_width * y + x] = data_padded_ptr[p_next_width * y + x - p_next_width];
  67. }
  68. }
  69. }
  70. Vector<uint8_t> basis_universal_packer(const Ref<Image> &p_image, Image::UsedChannels p_channels) {
  71. uint64_t start_time = OS::get_singleton()->get_ticks_msec();
  72. Ref<Image> image = p_image->duplicate();
  73. bool is_hdr = false;
  74. if (image->get_format() <= Image::FORMAT_RGB565) {
  75. image->convert(Image::FORMAT_RGBA8);
  76. } else if (image->get_format() <= Image::FORMAT_RGBE9995) {
  77. image->convert(Image::FORMAT_RGBAF);
  78. is_hdr = true;
  79. }
  80. basisu::basis_compressor_params params;
  81. params.m_uastc = true;
  82. params.m_quality_level = basisu::BASISU_QUALITY_MIN;
  83. params.m_pack_uastc_flags &= ~basisu::cPackUASTCLevelMask;
  84. params.m_pack_uastc_flags |= basisu::cPackUASTCLevelFastest;
  85. params.m_rdo_uastc = 0.0f;
  86. params.m_rdo_uastc_quality_scalar = 0.0f;
  87. params.m_rdo_uastc_dict_size = 1024;
  88. params.m_mip_fast = true;
  89. params.m_multithreading = true;
  90. params.m_check_for_alpha = false;
  91. if (!OS::get_singleton()->is_stdout_verbose()) {
  92. params.m_print_stats = false;
  93. params.m_compute_stats = false;
  94. params.m_status_output = false;
  95. }
  96. basisu::job_pool job_pool(OS::get_singleton()->get_processor_count());
  97. params.m_pJob_pool = &job_pool;
  98. BasisDecompressFormat decompress_format = BASIS_DECOMPRESS_MAX;
  99. if (is_hdr) {
  100. decompress_format = BASIS_DECOMPRESS_HDR_RGB;
  101. params.m_hdr = true;
  102. params.m_uastc_hdr_options.set_quality_level(0);
  103. } else {
  104. switch (p_channels) {
  105. case Image::USED_CHANNELS_L: {
  106. decompress_format = BASIS_DECOMPRESS_RGB;
  107. } break;
  108. case Image::USED_CHANNELS_LA: {
  109. params.m_force_alpha = true;
  110. decompress_format = BASIS_DECOMPRESS_RGBA;
  111. } break;
  112. case Image::USED_CHANNELS_R: {
  113. decompress_format = BASIS_DECOMPRESS_R;
  114. } break;
  115. case Image::USED_CHANNELS_RG: {
  116. params.m_force_alpha = true;
  117. image->convert_rg_to_ra_rgba8();
  118. decompress_format = BASIS_DECOMPRESS_RG;
  119. } break;
  120. case Image::USED_CHANNELS_RGB: {
  121. decompress_format = BASIS_DECOMPRESS_RGB;
  122. } break;
  123. case Image::USED_CHANNELS_RGBA: {
  124. params.m_force_alpha = true;
  125. decompress_format = BASIS_DECOMPRESS_RGBA;
  126. } break;
  127. }
  128. }
  129. ERR_FAIL_COND_V(decompress_format == BASIS_DECOMPRESS_MAX, Vector<uint8_t>());
  130. // Copy the source image data with mipmaps into BasisU.
  131. {
  132. const int orig_width = image->get_width();
  133. const int orig_height = image->get_height();
  134. bool is_res_div_4 = (orig_width % 4 == 0) && (orig_height % 4 == 0);
  135. // Image's resolution rounded up to the nearest values divisible by 4.
  136. int next_width = orig_width <= 2 ? orig_width : (orig_width + 3) & ~3;
  137. int next_height = orig_height <= 2 ? orig_height : (orig_height + 3) & ~3;
  138. Vector<uint8_t> image_data = image->get_data();
  139. basisu::vector<basisu::image> basisu_mipmaps;
  140. basisu::vector<basisu::imagef> basisu_mipmaps_hdr;
  141. // Buffer for storing padded mipmap data.
  142. Vector<uint8_t> mip_data_padded;
  143. for (int32_t i = 0; i <= image->get_mipmap_count(); i++) {
  144. int64_t ofs, size;
  145. int width, height;
  146. image->get_mipmap_offset_size_and_dimensions(i, ofs, size, width, height);
  147. const uint8_t *image_mip_data = image_data.ptr() + ofs;
  148. // Pad the mipmap's data if its resolution isn't divisible by 4.
  149. if (image->has_mipmaps() && !is_res_div_4 && (width > 2 && height > 2) && (width != next_width || height != next_height)) {
  150. if (is_hdr) {
  151. _basisu_pad_mipmap<BasisRGBAF>(image_mip_data, mip_data_padded, next_width, next_height, width, height, size);
  152. } else {
  153. _basisu_pad_mipmap<uint32_t>(image_mip_data, mip_data_padded, next_width, next_height, width, height, size);
  154. }
  155. // Override the image_mip_data pointer with our temporary Vector.
  156. image_mip_data = reinterpret_cast<const uint8_t *>(mip_data_padded.ptr());
  157. // Override the mipmap's properties.
  158. width = next_width;
  159. height = next_height;
  160. size = mip_data_padded.size();
  161. }
  162. // Get the next mipmap's resolution.
  163. next_width /= 2;
  164. next_height /= 2;
  165. // Copy the source mipmap's data to a BasisU image.
  166. if (is_hdr) {
  167. basisu::imagef basisu_image(width, height);
  168. memcpy(reinterpret_cast<uint8_t *>(basisu_image.get_ptr()), image_mip_data, size);
  169. if (i == 0) {
  170. params.m_source_images_hdr.push_back(basisu_image);
  171. } else {
  172. basisu_mipmaps_hdr.push_back(basisu_image);
  173. }
  174. } else {
  175. basisu::image basisu_image(width, height);
  176. memcpy(basisu_image.get_ptr(), image_mip_data, size);
  177. if (i == 0) {
  178. params.m_source_images.push_back(basisu_image);
  179. } else {
  180. basisu_mipmaps.push_back(basisu_image);
  181. }
  182. }
  183. }
  184. if (is_hdr) {
  185. params.m_source_mipmap_images_hdr.push_back(basisu_mipmaps_hdr);
  186. } else {
  187. params.m_source_mipmap_images.push_back(basisu_mipmaps);
  188. }
  189. }
  190. // Encode the image data.
  191. basisu::basis_compressor compressor;
  192. compressor.init(params);
  193. int basisu_err = compressor.process();
  194. ERR_FAIL_COND_V(basisu_err != basisu::basis_compressor::cECSuccess, Vector<uint8_t>());
  195. const basisu::uint8_vec &basisu_encoded = compressor.get_output_basis_file();
  196. Vector<uint8_t> basisu_data;
  197. basisu_data.resize(basisu_encoded.size() + 4);
  198. uint8_t *basisu_data_ptr = basisu_data.ptrw();
  199. // Copy the encoded BasisU data into the output buffer.
  200. *(uint32_t *)basisu_data_ptr = decompress_format;
  201. memcpy(basisu_data_ptr + 4, basisu_encoded.get_ptr(), basisu_encoded.size());
  202. print_verbose(vformat("BasisU: Encoding a %dx%d image with %d mipmaps took %d ms.", p_image->get_width(), p_image->get_height(), p_image->get_mipmap_count(), OS::get_singleton()->get_ticks_msec() - start_time));
  203. return basisu_data;
  204. }
  205. #endif // TOOLS_ENABLED
  206. Ref<Image> basis_universal_unpacker_ptr(const uint8_t *p_data, int p_size) {
  207. uint64_t start_time = OS::get_singleton()->get_ticks_msec();
  208. Ref<Image> image;
  209. ERR_FAIL_NULL_V_MSG(p_data, image, "Cannot unpack invalid BasisUniversal data.");
  210. const uint8_t *src_ptr = p_data;
  211. int src_size = p_size;
  212. basist::transcoder_texture_format basisu_format = basist::transcoder_texture_format::cTFTotalTextureFormats;
  213. Image::Format image_format = Image::FORMAT_MAX;
  214. // Get supported compression formats.
  215. bool bptc_supported = RS::get_singleton()->has_os_feature("bptc");
  216. bool astc_supported = RS::get_singleton()->has_os_feature("astc");
  217. bool rgtc_supported = RS::get_singleton()->has_os_feature("rgtc");
  218. bool s3tc_supported = RS::get_singleton()->has_os_feature("s3tc");
  219. bool etc2_supported = RS::get_singleton()->has_os_feature("etc2");
  220. bool needs_ra_rg_swap = false;
  221. bool needs_rg_trim = false;
  222. BasisDecompressFormat decompress_format = (BasisDecompressFormat)(*(uint32_t *)(src_ptr));
  223. switch (decompress_format) {
  224. case BASIS_DECOMPRESS_R: {
  225. if (rgtc_supported) {
  226. basisu_format = basist::transcoder_texture_format::cTFBC4_R;
  227. image_format = Image::FORMAT_RGTC_R;
  228. } else if (s3tc_supported) {
  229. basisu_format = basist::transcoder_texture_format::cTFBC1;
  230. image_format = Image::FORMAT_DXT1;
  231. } else if (etc2_supported) {
  232. basisu_format = basist::transcoder_texture_format::cTFETC2_EAC_R11;
  233. image_format = Image::FORMAT_ETC2_R11;
  234. } else {
  235. // No supported VRAM compression formats, decompress.
  236. basisu_format = basist::transcoder_texture_format::cTFRGBA32;
  237. image_format = Image::FORMAT_RGBA8;
  238. needs_rg_trim = true;
  239. }
  240. } break;
  241. case BASIS_DECOMPRESS_RG: {
  242. if (rgtc_supported) {
  243. basisu_format = basist::transcoder_texture_format::cTFBC5_RG;
  244. image_format = Image::FORMAT_RGTC_RG;
  245. } else if (s3tc_supported) {
  246. basisu_format = basist::transcoder_texture_format::cTFBC3;
  247. image_format = Image::FORMAT_DXT5_RA_AS_RG;
  248. } else if (etc2_supported) {
  249. basisu_format = basist::transcoder_texture_format::cTFETC2_EAC_RG11;
  250. image_format = Image::FORMAT_ETC2_RG11;
  251. } else {
  252. // No supported VRAM compression formats, decompress.
  253. basisu_format = basist::transcoder_texture_format::cTFRGBA32;
  254. image_format = Image::FORMAT_RGBA8;
  255. needs_ra_rg_swap = true;
  256. needs_rg_trim = true;
  257. }
  258. } break;
  259. case BASIS_DECOMPRESS_RG_AS_RA: {
  260. if (s3tc_supported) {
  261. basisu_format = basist::transcoder_texture_format::cTFBC3;
  262. image_format = Image::FORMAT_DXT5_RA_AS_RG;
  263. } else if (etc2_supported) {
  264. basisu_format = basist::transcoder_texture_format::cTFETC2;
  265. image_format = Image::FORMAT_ETC2_RA_AS_RG;
  266. } else {
  267. // No supported VRAM compression formats, decompress.
  268. basisu_format = basist::transcoder_texture_format::cTFRGBA32;
  269. image_format = Image::FORMAT_RGBA8;
  270. needs_ra_rg_swap = true;
  271. needs_rg_trim = true;
  272. }
  273. } break;
  274. case BASIS_DECOMPRESS_RGB: {
  275. if (bptc_supported) {
  276. basisu_format = basist::transcoder_texture_format::cTFBC7_M6_OPAQUE_ONLY;
  277. image_format = Image::FORMAT_BPTC_RGBA;
  278. } else if (astc_supported) {
  279. basisu_format = basist::transcoder_texture_format::cTFASTC_4x4_RGBA;
  280. image_format = Image::FORMAT_ASTC_4x4;
  281. } else if (s3tc_supported) {
  282. basisu_format = basist::transcoder_texture_format::cTFBC1;
  283. image_format = Image::FORMAT_DXT1;
  284. } else if (etc2_supported) {
  285. basisu_format = basist::transcoder_texture_format::cTFETC1;
  286. image_format = Image::FORMAT_ETC2_RGB8;
  287. } else {
  288. // No supported VRAM compression formats, decompress.
  289. basisu_format = basist::transcoder_texture_format::cTFRGBA32;
  290. image_format = Image::FORMAT_RGBA8;
  291. }
  292. } break;
  293. case BASIS_DECOMPRESS_RGBA: {
  294. if (bptc_supported) {
  295. basisu_format = basist::transcoder_texture_format::cTFBC7_M5;
  296. image_format = Image::FORMAT_BPTC_RGBA;
  297. } else if (astc_supported) {
  298. basisu_format = basist::transcoder_texture_format::cTFASTC_4x4_RGBA;
  299. image_format = Image::FORMAT_ASTC_4x4;
  300. } else if (s3tc_supported) {
  301. basisu_format = basist::transcoder_texture_format::cTFBC3;
  302. image_format = Image::FORMAT_DXT5;
  303. } else if (etc2_supported) {
  304. basisu_format = basist::transcoder_texture_format::cTFETC2;
  305. image_format = Image::FORMAT_ETC2_RGBA8;
  306. } else {
  307. // No supported VRAM compression formats, decompress.
  308. basisu_format = basist::transcoder_texture_format::cTFRGBA32;
  309. image_format = Image::FORMAT_RGBA8;
  310. }
  311. } break;
  312. case BASIS_DECOMPRESS_HDR_RGB: {
  313. if (bptc_supported) {
  314. basisu_format = basist::transcoder_texture_format::cTFBC6H;
  315. image_format = Image::FORMAT_BPTC_RGBFU;
  316. } else if (astc_supported) {
  317. basisu_format = basist::transcoder_texture_format::cTFASTC_HDR_4x4_RGBA;
  318. image_format = Image::FORMAT_ASTC_4x4_HDR;
  319. } else {
  320. // No supported VRAM compression formats, decompress.
  321. basisu_format = basist::transcoder_texture_format::cTFRGB_9E5;
  322. image_format = Image::FORMAT_RGBE9995;
  323. }
  324. } break;
  325. default: {
  326. ERR_FAIL_V(image);
  327. } break;
  328. }
  329. src_ptr += 4;
  330. src_size -= 4;
  331. basist::basisu_transcoder transcoder;
  332. ERR_FAIL_COND_V(!transcoder.validate_header(src_ptr, src_size), image);
  333. transcoder.start_transcoding(src_ptr, src_size);
  334. basist::basisu_image_info basisu_info;
  335. transcoder.get_image_info(src_ptr, src_size, basisu_info, 0);
  336. // Create the buffer for transcoded/decompressed data.
  337. Vector<uint8_t> out_data;
  338. out_data.resize(Image::get_image_data_size(basisu_info.m_width, basisu_info.m_height, image_format, basisu_info.m_total_levels > 1));
  339. uint8_t *dst = out_data.ptrw();
  340. memset(dst, 0, out_data.size());
  341. for (uint32_t i = 0; i < basisu_info.m_total_levels; i++) {
  342. basist::basisu_image_level_info basisu_level;
  343. transcoder.get_image_level_info(src_ptr, src_size, basisu_level, 0, i);
  344. uint32_t mip_block_or_pixel_count = Image::is_format_compressed(image_format) ? basisu_level.m_total_blocks : basisu_level.m_orig_width * basisu_level.m_orig_height;
  345. int64_t ofs = Image::get_image_mipmap_offset(basisu_info.m_width, basisu_info.m_height, image_format, i);
  346. bool result = transcoder.transcode_image_level(src_ptr, src_size, 0, i, dst + ofs, mip_block_or_pixel_count, basisu_format);
  347. if (!result) {
  348. print_line(vformat("BasisUniversal cannot unpack level %d.", i));
  349. break;
  350. }
  351. }
  352. image = Image::create_from_data(basisu_info.m_width, basisu_info.m_height, basisu_info.m_total_levels > 1, image_format, out_data);
  353. if (needs_ra_rg_swap) {
  354. // Swap uncompressed RA-as-RG texture's color channels.
  355. image->convert_ra_rgba8_to_rg();
  356. }
  357. if (needs_rg_trim) {
  358. // Remove unnecessary color channels from uncompressed textures.
  359. if (decompress_format == BASIS_DECOMPRESS_R) {
  360. image->convert(Image::FORMAT_R8);
  361. } else if (decompress_format == BASIS_DECOMPRESS_RG || decompress_format == BASIS_DECOMPRESS_RG_AS_RA) {
  362. image->convert(Image::FORMAT_RG8);
  363. }
  364. }
  365. print_verbose(vformat("BasisU: Transcoding a %dx%d image with %d mipmaps into %s took %d ms.",
  366. image->get_width(), image->get_height(), image->get_mipmap_count(), Image::get_format_name(image_format), OS::get_singleton()->get_ticks_msec() - start_time));
  367. return image;
  368. }
  369. Ref<Image> basis_universal_unpacker(const Vector<uint8_t> &p_buffer) {
  370. return basis_universal_unpacker_ptr(p_buffer.ptr(), p_buffer.size());
  371. }