image_compress_astcenc.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /**************************************************************************/
  2. /* image_compress_astcenc.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_astcenc.h"
  31. #include "core/os/os.h"
  32. #include "core/string/print_string.h"
  33. #include <astcenc.h>
  34. #ifdef TOOLS_ENABLED
  35. void _compress_astc(Image *r_img, Image::ASTCFormat p_format) {
  36. const uint64_t start_time = OS::get_singleton()->get_ticks_msec();
  37. if (r_img->is_compressed()) {
  38. return; // Do not compress, already compressed.
  39. }
  40. const Image::Format src_format = r_img->get_format();
  41. const bool is_hdr = src_format >= Image::FORMAT_RF && src_format <= Image::FORMAT_RGBE9995;
  42. if (src_format >= Image::FORMAT_RH && src_format <= Image::FORMAT_RGBAH) {
  43. r_img->convert(Image::FORMAT_RGBAH);
  44. } else if (src_format >= Image::FORMAT_RF && src_format <= Image::FORMAT_RGBE9995) {
  45. r_img->convert(Image::FORMAT_RGBAF);
  46. } else {
  47. r_img->convert(Image::FORMAT_RGBA8);
  48. }
  49. // Determine encoder output format from our enum.
  50. const astcenc_profile profile = is_hdr ? ASTCENC_PRF_HDR : ASTCENC_PRF_LDR;
  51. Image::Format target_format = Image::FORMAT_MAX;
  52. unsigned int block_x = 4;
  53. unsigned int block_y = 4;
  54. if (p_format == Image::ASTCFormat::ASTC_FORMAT_4x4) {
  55. if (is_hdr) {
  56. target_format = Image::FORMAT_ASTC_4x4_HDR;
  57. } else {
  58. target_format = Image::FORMAT_ASTC_4x4;
  59. }
  60. } else if (p_format == Image::ASTCFormat::ASTC_FORMAT_8x8) {
  61. if (is_hdr) {
  62. target_format = Image::FORMAT_ASTC_8x8_HDR;
  63. } else {
  64. target_format = Image::FORMAT_ASTC_8x8;
  65. }
  66. block_x = 8;
  67. block_y = 8;
  68. }
  69. // Compress image data and (if required) mipmaps.
  70. const bool has_mipmaps = r_img->has_mipmaps();
  71. int width = r_img->get_width();
  72. int height = r_img->get_height();
  73. int required_width = (width % block_x) != 0 ? width + (block_x - (width % block_x)) : width;
  74. int required_height = (height % block_y) != 0 ? height + (block_y - (height % block_y)) : height;
  75. if (width != required_width || height != required_height) {
  76. // Resize texture to fit block size.
  77. r_img->resize(required_width, required_height);
  78. width = required_width;
  79. height = required_height;
  80. }
  81. print_verbose(vformat("astcenc: Encoding image size %dx%d to format %s%s.", width, height, Image::get_format_name(target_format), has_mipmaps ? ", with mipmaps" : ""));
  82. // Initialize astcenc.
  83. const int64_t dest_size = Image::get_image_data_size(width, height, target_format, has_mipmaps);
  84. Vector<uint8_t> dest_data;
  85. dest_data.resize(dest_size);
  86. uint8_t *dest_write = dest_data.ptrw();
  87. astcenc_config config;
  88. config.block_x = block_x;
  89. config.block_y = block_y;
  90. config.profile = profile;
  91. const float quality = ASTCENC_PRE_MEDIUM;
  92. astcenc_error status = astcenc_config_init(profile, block_x, block_y, 1, quality, 0, &config);
  93. ERR_FAIL_COND_MSG(status != ASTCENC_SUCCESS,
  94. vformat("astcenc: Configuration initialization failed: %s.", astcenc_get_error_string(status)));
  95. // Context allocation.
  96. astcenc_context *context;
  97. const unsigned int thread_count = 1; // Godot compresses multiple images each on a thread, which is more efficient for large amount of images imported.
  98. status = astcenc_context_alloc(&config, thread_count, &context);
  99. ERR_FAIL_COND_MSG(status != ASTCENC_SUCCESS,
  100. vformat("astcenc: Context allocation failed: %s.", astcenc_get_error_string(status)));
  101. const int mip_count = has_mipmaps ? Image::get_image_required_mipmaps(width, height, target_format) : 0;
  102. const uint8_t *src_data = r_img->ptr();
  103. for (int i = 0; i < mip_count + 1; i++) {
  104. int src_mip_w, src_mip_h;
  105. const int64_t src_ofs = Image::get_image_mipmap_offset_and_dimensions(width, height, r_img->get_format(), i, src_mip_w, src_mip_h);
  106. const uint8_t *mip_data = &src_data[src_ofs];
  107. const int64_t dst_ofs = Image::get_image_mipmap_offset(width, height, target_format, i);
  108. uint8_t *dest_mip_write = &dest_write[dst_ofs];
  109. // Ensure that mip offset is a multiple of 8 (etcpak expects uint64_t pointer).
  110. if (unlikely(dst_ofs % 8 != 0)) {
  111. astcenc_context_free(context);
  112. ERR_FAIL_MSG("astcenc: Mip offset is not a multiple of 8.");
  113. }
  114. // Compress image.
  115. astcenc_image image;
  116. image.dim_x = src_mip_w;
  117. image.dim_y = src_mip_h;
  118. image.dim_z = 1;
  119. if (r_img->get_format() == Image::FORMAT_RGBA8) {
  120. image.data_type = ASTCENC_TYPE_U8;
  121. } else if (r_img->get_format() == Image::FORMAT_RGBAH) {
  122. image.data_type = ASTCENC_TYPE_F16;
  123. } else {
  124. image.data_type = ASTCENC_TYPE_F32;
  125. }
  126. image.data = (void **)(&mip_data);
  127. // Compute the number of ASTC blocks in each dimension.
  128. unsigned int block_count_x = (src_mip_w + block_x - 1) / block_x;
  129. unsigned int block_count_y = (src_mip_h + block_y - 1) / block_y;
  130. size_t comp_len = block_count_x * block_count_y * 16;
  131. const astcenc_swizzle swizzle = {
  132. ASTCENC_SWZ_R, ASTCENC_SWZ_G, ASTCENC_SWZ_B, ASTCENC_SWZ_A
  133. };
  134. status = astcenc_compress_image(context, &image, &swizzle, dest_mip_write, comp_len, 0);
  135. ERR_BREAK_MSG(status != ASTCENC_SUCCESS,
  136. vformat("astcenc: ASTC image compression failed: %s.", astcenc_get_error_string(status)));
  137. astcenc_compress_reset(context);
  138. }
  139. astcenc_context_free(context);
  140. // Replace original image with compressed one.
  141. r_img->set_data(width, height, has_mipmaps, target_format, dest_data);
  142. print_verbose(vformat("astcenc: Encoding took %d ms.", OS::get_singleton()->get_ticks_msec() - start_time));
  143. }
  144. #endif // TOOLS_ENABLED
  145. void _decompress_astc(Image *r_img) {
  146. const uint64_t start_time = OS::get_singleton()->get_ticks_msec();
  147. // Determine decompression parameters from image format.
  148. const Image::Format src_format = r_img->get_format();
  149. bool is_hdr = false;
  150. unsigned int block_x = 0;
  151. unsigned int block_y = 0;
  152. switch (src_format) {
  153. case Image::FORMAT_ASTC_4x4: {
  154. block_x = 4;
  155. block_y = 4;
  156. is_hdr = false;
  157. } break;
  158. case Image::FORMAT_ASTC_4x4_HDR: {
  159. block_x = 4;
  160. block_y = 4;
  161. is_hdr = true;
  162. } break;
  163. case Image::FORMAT_ASTC_8x8: {
  164. block_x = 8;
  165. block_y = 8;
  166. is_hdr = false;
  167. } break;
  168. case Image::FORMAT_ASTC_8x8_HDR: {
  169. block_x = 8;
  170. block_y = 8;
  171. is_hdr = true;
  172. } break;
  173. default: {
  174. ERR_FAIL_MSG(vformat("astcenc: Cannot decompress Image with a non-ASTC format: %s.", Image::get_format_name(src_format)));
  175. } break;
  176. }
  177. // Initialize astcenc.
  178. const astcenc_profile profile = is_hdr ? ASTCENC_PRF_HDR : ASTCENC_PRF_LDR;
  179. astcenc_config config;
  180. const float quality = ASTCENC_PRE_MEDIUM;
  181. const uint32_t flags = ASTCENC_FLG_DECOMPRESS_ONLY;
  182. astcenc_error status = astcenc_config_init(profile, block_x, block_y, 1, quality, flags, &config);
  183. ERR_FAIL_COND_MSG(status != ASTCENC_SUCCESS,
  184. vformat("astcenc: Configuration initialization failed: %s.", astcenc_get_error_string(status)));
  185. // Context allocation.
  186. astcenc_context *context = nullptr;
  187. const unsigned int thread_count = 1;
  188. status = astcenc_context_alloc(&config, thread_count, &context);
  189. ERR_FAIL_COND_MSG(status != ASTCENC_SUCCESS,
  190. vformat("astcenc: Context allocation failed: %s.", astcenc_get_error_string(status)));
  191. const Image::Format target_format = is_hdr ? Image::FORMAT_RGBAH : Image::FORMAT_RGBA8;
  192. const bool has_mipmaps = r_img->has_mipmaps();
  193. int width = r_img->get_width();
  194. int height = r_img->get_height();
  195. const int64_t dest_size = Image::get_image_data_size(width, height, target_format, has_mipmaps);
  196. Vector<uint8_t> dest_data;
  197. dest_data.resize(dest_size);
  198. uint8_t *dest_write = dest_data.ptrw();
  199. // Decompress image.
  200. const int mip_count = has_mipmaps ? Image::get_image_required_mipmaps(width, height, target_format) : 0;
  201. const uint8_t *src_data = r_img->ptr();
  202. for (int i = 0; i < mip_count + 1; i++) {
  203. const int64_t src_ofs = Image::get_image_mipmap_offset(width, height, src_format, i);
  204. const uint8_t *mip_data = &src_data[src_ofs];
  205. int64_t src_size;
  206. if (i == mip_count) {
  207. src_size = r_img->get_data_size() - src_ofs;
  208. } else {
  209. src_size = Image::get_image_mipmap_offset(width, height, src_format, i + 1) - src_ofs;
  210. }
  211. int dst_mip_w, dst_mip_h;
  212. const int64_t dst_ofs = Image::get_image_mipmap_offset_and_dimensions(width, height, target_format, i, dst_mip_w, dst_mip_h);
  213. // Ensure that mip offset is a multiple of 8 (etcpak expects uint64_t pointer).
  214. ERR_FAIL_COND(dst_ofs % 8 != 0);
  215. uint8_t *dest_mip_write = &dest_write[dst_ofs];
  216. astcenc_image image;
  217. image.dim_x = dst_mip_w;
  218. image.dim_y = dst_mip_h;
  219. image.dim_z = 1;
  220. image.data_type = is_hdr ? ASTCENC_TYPE_F16 : ASTCENC_TYPE_U8;
  221. image.data = (void **)(&dest_mip_write);
  222. const astcenc_swizzle swizzle = {
  223. ASTCENC_SWZ_R, ASTCENC_SWZ_G, ASTCENC_SWZ_B, ASTCENC_SWZ_A
  224. };
  225. status = astcenc_decompress_image(context, mip_data, src_size, &image, &swizzle, 0);
  226. ERR_BREAK_MSG(status != ASTCENC_SUCCESS, vformat("astcenc: ASTC decompression failed: %s.", astcenc_get_error_string(status)));
  227. ERR_BREAK_MSG(image.dim_z > 1, "astcenc: ASTC decompression failed because this is a 3D texture, which is not supported.");
  228. astcenc_compress_reset(context);
  229. }
  230. astcenc_context_free(context);
  231. // Replace original image with compressed one.
  232. r_img->set_data(width, height, has_mipmaps, target_format, dest_data);
  233. print_verbose(vformat("astcenc: Decompression took %d ms.", OS::get_singleton()->get_ticks_msec() - start_time));
  234. }