Path: blob/master/modules/astcenc/image_compress_astcenc.cpp
10277 views
/**************************************************************************/1/* image_compress_astcenc.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "image_compress_astcenc.h"3132#include "core/os/os.h"33#include "core/string/print_string.h"3435#include <astcenc.h>3637#ifdef TOOLS_ENABLED38void _compress_astc(Image *r_img, Image::ASTCFormat p_format) {39const uint64_t start_time = OS::get_singleton()->get_ticks_msec();4041if (r_img->is_compressed()) {42return; // Do not compress, already compressed.43}4445const Image::Format src_format = r_img->get_format();46const bool is_hdr = src_format >= Image::FORMAT_RF && src_format <= Image::FORMAT_RGBE9995;4748if (src_format >= Image::FORMAT_RH && src_format <= Image::FORMAT_RGBAH) {49r_img->convert(Image::FORMAT_RGBAH);50} else if (src_format >= Image::FORMAT_RF && src_format <= Image::FORMAT_RGBE9995) {51r_img->convert(Image::FORMAT_RGBAF);52} else {53r_img->convert(Image::FORMAT_RGBA8);54}5556// Determine encoder output format from our enum.57const astcenc_profile profile = is_hdr ? ASTCENC_PRF_HDR : ASTCENC_PRF_LDR;5859Image::Format target_format = Image::FORMAT_MAX;60unsigned int block_x = 4;61unsigned int block_y = 4;6263if (p_format == Image::ASTCFormat::ASTC_FORMAT_4x4) {64if (is_hdr) {65target_format = Image::FORMAT_ASTC_4x4_HDR;66} else {67target_format = Image::FORMAT_ASTC_4x4;68}69} else if (p_format == Image::ASTCFormat::ASTC_FORMAT_8x8) {70if (is_hdr) {71target_format = Image::FORMAT_ASTC_8x8_HDR;72} else {73target_format = Image::FORMAT_ASTC_8x8;74}75block_x = 8;76block_y = 8;77}7879// Compress image data and (if required) mipmaps.80const bool has_mipmaps = r_img->has_mipmaps();81int width = r_img->get_width();82int height = r_img->get_height();83int required_width = (width % block_x) != 0 ? width + (block_x - (width % block_x)) : width;84int required_height = (height % block_y) != 0 ? height + (block_y - (height % block_y)) : height;8586if (width != required_width || height != required_height) {87// Resize texture to fit block size.88r_img->resize(required_width, required_height);89width = required_width;90height = required_height;91}9293print_verbose(vformat("astcenc: Encoding image size %dx%d to format %s%s.", width, height, Image::get_format_name(target_format), has_mipmaps ? ", with mipmaps" : ""));9495// Initialize astcenc.96const int64_t dest_size = Image::get_image_data_size(width, height, target_format, has_mipmaps);97Vector<uint8_t> dest_data;98dest_data.resize(dest_size);99uint8_t *dest_write = dest_data.ptrw();100101astcenc_config config;102config.block_x = block_x;103config.block_y = block_y;104config.profile = profile;105106const float quality = ASTCENC_PRE_MEDIUM;107astcenc_error status = astcenc_config_init(profile, block_x, block_y, 1, quality, 0, &config);108ERR_FAIL_COND_MSG(status != ASTCENC_SUCCESS,109vformat("astcenc: Configuration initialization failed: %s.", astcenc_get_error_string(status)));110111// Context allocation.112astcenc_context *context;113const unsigned int thread_count = 1; // Godot compresses multiple images each on a thread, which is more efficient for large amount of images imported.114status = astcenc_context_alloc(&config, thread_count, &context);115ERR_FAIL_COND_MSG(status != ASTCENC_SUCCESS,116vformat("astcenc: Context allocation failed: %s.", astcenc_get_error_string(status)));117118const int mip_count = has_mipmaps ? Image::get_image_required_mipmaps(width, height, target_format) : 0;119const uint8_t *src_data = r_img->ptr();120121for (int i = 0; i < mip_count + 1; i++) {122int src_mip_w, src_mip_h;123const int64_t src_ofs = Image::get_image_mipmap_offset_and_dimensions(width, height, r_img->get_format(), i, src_mip_w, src_mip_h);124const uint8_t *mip_data = &src_data[src_ofs];125126const int64_t dst_ofs = Image::get_image_mipmap_offset(width, height, target_format, i);127uint8_t *dest_mip_write = &dest_write[dst_ofs];128129// Ensure that mip offset is a multiple of 8 (etcpak expects uint64_t pointer).130if (unlikely(dst_ofs % 8 != 0)) {131astcenc_context_free(context);132ERR_FAIL_MSG("astcenc: Mip offset is not a multiple of 8.");133}134135// Compress image.136astcenc_image image;137image.dim_x = src_mip_w;138image.dim_y = src_mip_h;139image.dim_z = 1;140141if (r_img->get_format() == Image::FORMAT_RGBA8) {142image.data_type = ASTCENC_TYPE_U8;143} else if (r_img->get_format() == Image::FORMAT_RGBAH) {144image.data_type = ASTCENC_TYPE_F16;145} else {146image.data_type = ASTCENC_TYPE_F32;147}148149image.data = (void **)(&mip_data);150151// Compute the number of ASTC blocks in each dimension.152unsigned int block_count_x = (src_mip_w + block_x - 1) / block_x;153unsigned int block_count_y = (src_mip_h + block_y - 1) / block_y;154size_t comp_len = block_count_x * block_count_y * 16;155156const astcenc_swizzle swizzle = {157ASTCENC_SWZ_R, ASTCENC_SWZ_G, ASTCENC_SWZ_B, ASTCENC_SWZ_A158};159160status = astcenc_compress_image(context, &image, &swizzle, dest_mip_write, comp_len, 0);161ERR_BREAK_MSG(status != ASTCENC_SUCCESS,162vformat("astcenc: ASTC image compression failed: %s.", astcenc_get_error_string(status)));163164astcenc_compress_reset(context);165}166167astcenc_context_free(context);168169// Replace original image with compressed one.170r_img->set_data(width, height, has_mipmaps, target_format, dest_data);171172print_verbose(vformat("astcenc: Encoding took %d ms.", OS::get_singleton()->get_ticks_msec() - start_time));173}174#endif // TOOLS_ENABLED175176void _decompress_astc(Image *r_img) {177const uint64_t start_time = OS::get_singleton()->get_ticks_msec();178179// Determine decompression parameters from image format.180const Image::Format src_format = r_img->get_format();181182bool is_hdr = false;183unsigned int block_x = 0;184unsigned int block_y = 0;185186switch (src_format) {187case Image::FORMAT_ASTC_4x4: {188block_x = 4;189block_y = 4;190is_hdr = false;191} break;192case Image::FORMAT_ASTC_4x4_HDR: {193block_x = 4;194block_y = 4;195is_hdr = true;196} break;197case Image::FORMAT_ASTC_8x8: {198block_x = 8;199block_y = 8;200is_hdr = false;201} break;202case Image::FORMAT_ASTC_8x8_HDR: {203block_x = 8;204block_y = 8;205is_hdr = true;206} break;207default: {208ERR_FAIL_MSG(vformat("astcenc: Cannot decompress Image with a non-ASTC format: %s.", Image::get_format_name(src_format)));209} break;210}211212// Initialize astcenc.213const astcenc_profile profile = is_hdr ? ASTCENC_PRF_HDR : ASTCENC_PRF_LDR;214215astcenc_config config;216const float quality = ASTCENC_PRE_MEDIUM;217const uint32_t flags = ASTCENC_FLG_DECOMPRESS_ONLY;218219astcenc_error status = astcenc_config_init(profile, block_x, block_y, 1, quality, flags, &config);220ERR_FAIL_COND_MSG(status != ASTCENC_SUCCESS,221vformat("astcenc: Configuration initialization failed: %s.", astcenc_get_error_string(status)));222223// Context allocation.224astcenc_context *context = nullptr;225const unsigned int thread_count = 1;226227status = astcenc_context_alloc(&config, thread_count, &context);228ERR_FAIL_COND_MSG(status != ASTCENC_SUCCESS,229vformat("astcenc: Context allocation failed: %s.", astcenc_get_error_string(status)));230231const Image::Format target_format = is_hdr ? Image::FORMAT_RGBAH : Image::FORMAT_RGBA8;232233const bool has_mipmaps = r_img->has_mipmaps();234int width = r_img->get_width();235int height = r_img->get_height();236237const int64_t dest_size = Image::get_image_data_size(width, height, target_format, has_mipmaps);238Vector<uint8_t> dest_data;239dest_data.resize(dest_size);240uint8_t *dest_write = dest_data.ptrw();241242// Decompress image.243const int mip_count = has_mipmaps ? Image::get_image_required_mipmaps(width, height, target_format) : 0;244const uint8_t *src_data = r_img->ptr();245246for (int i = 0; i < mip_count + 1; i++) {247const int64_t src_ofs = Image::get_image_mipmap_offset(width, height, src_format, i);248const uint8_t *mip_data = &src_data[src_ofs];249250int64_t src_size;251if (i == mip_count) {252src_size = r_img->get_data_size() - src_ofs;253} else {254src_size = Image::get_image_mipmap_offset(width, height, src_format, i + 1) - src_ofs;255}256257int dst_mip_w, dst_mip_h;258const int64_t dst_ofs = Image::get_image_mipmap_offset_and_dimensions(width, height, target_format, i, dst_mip_w, dst_mip_h);259260// Ensure that mip offset is a multiple of 8 (etcpak expects uint64_t pointer).261ERR_FAIL_COND(dst_ofs % 8 != 0);262uint8_t *dest_mip_write = &dest_write[dst_ofs];263264astcenc_image image;265image.dim_x = dst_mip_w;266image.dim_y = dst_mip_h;267image.dim_z = 1;268image.data_type = is_hdr ? ASTCENC_TYPE_F16 : ASTCENC_TYPE_U8;269270image.data = (void **)(&dest_mip_write);271272const astcenc_swizzle swizzle = {273ASTCENC_SWZ_R, ASTCENC_SWZ_G, ASTCENC_SWZ_B, ASTCENC_SWZ_A274};275276status = astcenc_decompress_image(context, mip_data, src_size, &image, &swizzle, 0);277ERR_BREAK_MSG(status != ASTCENC_SUCCESS, vformat("astcenc: ASTC decompression failed: %s.", astcenc_get_error_string(status)));278ERR_BREAK_MSG(image.dim_z > 1, "astcenc: ASTC decompression failed because this is a 3D texture, which is not supported.");279280astcenc_compress_reset(context);281}282283astcenc_context_free(context);284285// Replace original image with compressed one.286r_img->set_data(width, height, has_mipmaps, target_format, dest_data);287288print_verbose(vformat("astcenc: Decompression took %d ms.", OS::get_singleton()->get_ticks_msec() - start_time));289}290291292