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. 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 (img_format >= Image::FORMAT_DXT1) {
  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. int 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. int 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. int 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. ERR_FAIL_COND(dst_ofs % 8 != 0);
  112. uint8_t *dest_mip_write = (uint8_t *)&dest_write[dst_ofs];
  113. // Compress image.
  114. astcenc_image image;
  115. image.dim_x = src_mip_w;
  116. image.dim_y = src_mip_h;
  117. image.dim_z = 1;
  118. image.data_type = ASTCENC_TYPE_U8;
  119. if (is_hdr) {
  120. image.data_type = ASTCENC_TYPE_F32;
  121. }
  122. image.data = (void **)(&slices);
  123. // Compute the number of ASTC blocks in each dimension.
  124. unsigned int block_count_x = (src_mip_w + block_x - 1) / block_x;
  125. unsigned int block_count_y = (src_mip_h + block_y - 1) / block_y;
  126. size_t comp_len = block_count_x * block_count_y * 16;
  127. const astcenc_swizzle swizzle = {
  128. ASTCENC_SWZ_R, ASTCENC_SWZ_G, ASTCENC_SWZ_B, ASTCENC_SWZ_A
  129. };
  130. status = astcenc_compress_image(context, &image, &swizzle, dest_mip_write, comp_len, 0);
  131. ERR_BREAK_MSG(status != ASTCENC_SUCCESS,
  132. vformat("astcenc: ASTC image compression failed: %s.", astcenc_get_error_string(status)));
  133. astcenc_compress_reset(context);
  134. }
  135. astcenc_context_free(context);
  136. // Replace original image with compressed one.
  137. r_img->set_data(width, height, mipmaps, target_format, dest_data);
  138. print_verbose(vformat("astcenc: Encoding took %s ms.", rtos(OS::get_singleton()->get_ticks_msec() - start_time)));
  139. }
  140. void _decompress_astc(Image *r_img) {
  141. uint64_t start_time = OS::get_singleton()->get_ticks_msec();
  142. // Determine decompression parameters from image format.
  143. Image::Format img_format = r_img->get_format();
  144. bool is_hdr = false;
  145. unsigned int block_x = 0;
  146. unsigned int block_y = 0;
  147. if (img_format == Image::FORMAT_ASTC_4x4) {
  148. block_x = 4;
  149. block_y = 4;
  150. is_hdr = false;
  151. } else if (img_format == Image::FORMAT_ASTC_4x4_HDR) {
  152. block_x = 4;
  153. block_y = 4;
  154. is_hdr = true;
  155. } else if (img_format == Image::FORMAT_ASTC_8x8) {
  156. block_x = 8;
  157. block_y = 8;
  158. is_hdr = false;
  159. } else if (img_format == Image::FORMAT_ASTC_8x8_HDR) {
  160. block_x = 8;
  161. block_y = 8;
  162. is_hdr = true;
  163. } else {
  164. ERR_FAIL_MSG("astcenc: Cannot decompress Image with a non-ASTC format.");
  165. }
  166. // Initialize astcenc.
  167. astcenc_profile profile = ASTCENC_PRF_LDR;
  168. if (is_hdr) {
  169. profile = ASTCENC_PRF_HDR;
  170. }
  171. astcenc_config config;
  172. const float quality = ASTCENC_PRE_MEDIUM;
  173. astcenc_error status = astcenc_config_init(profile, block_x, block_y, 1, quality, 0, &config);
  174. ERR_FAIL_COND_MSG(status != ASTCENC_SUCCESS,
  175. vformat("astcenc: Configuration initialization failed: %s.", astcenc_get_error_string(status)));
  176. // Context allocation.
  177. astcenc_context *context = nullptr;
  178. const unsigned int thread_count = 1;
  179. status = astcenc_context_alloc(&config, thread_count, &context);
  180. ERR_FAIL_COND_MSG(status != ASTCENC_SUCCESS,
  181. vformat("astcenc: Context allocation failed: %s.", astcenc_get_error_string(status)));
  182. Image::Format target_format = is_hdr ? Image::FORMAT_RGBAF : Image::FORMAT_RGBA8;
  183. const bool mipmaps = r_img->has_mipmaps();
  184. int width = r_img->get_width();
  185. int height = r_img->get_height();
  186. int dest_size = Image::get_image_data_size(width, height, target_format, mipmaps);
  187. Vector<uint8_t> dest_data;
  188. dest_data.resize(dest_size);
  189. uint8_t *dest_write = dest_data.ptrw();
  190. // Decompress image.
  191. Vector<uint8_t> image_data = r_img->get_data();
  192. int mip_count = mipmaps ? Image::get_image_required_mipmaps(width, height, target_format) : 0;
  193. for (int i = 0; i < mip_count + 1; i++) {
  194. int src_mip_w, src_mip_h;
  195. int src_ofs = Image::get_image_mipmap_offset_and_dimensions(width, height, r_img->get_format(), i, src_mip_w, src_mip_h);
  196. const uint8_t *src_data = &image_data.ptr()[src_ofs];
  197. int src_size;
  198. if (i == mip_count) {
  199. src_size = image_data.size() - src_ofs;
  200. } else {
  201. int auxw, auxh;
  202. src_size = Image::get_image_mipmap_offset_and_dimensions(width, height, r_img->get_format(), i + 1, auxw, auxh) - src_ofs;
  203. }
  204. int dst_mip_w, dst_mip_h;
  205. int dst_ofs = Image::get_image_mipmap_offset_and_dimensions(width, height, target_format, i, dst_mip_w, dst_mip_h);
  206. // Ensure that mip offset is a multiple of 8 (etcpak expects uint64_t pointer).
  207. ERR_FAIL_COND(dst_ofs % 8 != 0);
  208. uint8_t *dest_mip_write = (uint8_t *)&dest_write[dst_ofs];
  209. astcenc_image image;
  210. image.dim_x = dst_mip_w;
  211. image.dim_y = dst_mip_h;
  212. image.dim_z = 1;
  213. image.data_type = ASTCENC_TYPE_U8;
  214. if (is_hdr) {
  215. target_format = Image::FORMAT_RGBAF;
  216. image.data_type = ASTCENC_TYPE_F32;
  217. }
  218. image.data = (void **)(&dest_mip_write);
  219. const astcenc_swizzle swizzle = {
  220. ASTCENC_SWZ_R, ASTCENC_SWZ_G, ASTCENC_SWZ_B, ASTCENC_SWZ_A
  221. };
  222. status = astcenc_decompress_image(context, src_data, src_size, &image, &swizzle, 0);
  223. ERR_BREAK_MSG(status != ASTCENC_SUCCESS,
  224. vformat("astcenc: ASTC decompression failed: %s.", astcenc_get_error_string(status)));
  225. ERR_BREAK_MSG(image.dim_z > 1,
  226. "astcenc: ASTC decompression failed because this is a 3D texture, which is not supported.");
  227. astcenc_compress_reset(context);
  228. }
  229. astcenc_context_free(context);
  230. // Replace original image with compressed one.
  231. r_img->set_data(width, height, mipmaps, target_format, dest_data);
  232. print_verbose(vformat("astcenc: Decompression took %s ms.", rtos(OS::get_singleton()->get_ticks_msec() - start_time)));
  233. }