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