image_compress_astcenc.cpp 11 KB

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