Refactor high quality texture import

* Only two texture import modes for low/high quality now:
  * S3TC/BPTC
  * ETC2/ASTC
* Makes sense given this is the general preferred and most compatible combination in most platforms.
* Removed lossy_quality from VRAM texture compression options. It was unused everywhere.
* Added a new "high_quality" option to texture import. When enabled, it uses BPTC/ASTC (BC7/ASTC4x4) instead of S3TC/ETC2 (DXT1-5/ETC2,ETCA).
* Changed MacOS export settings so required texture formats depend on the architecture selected.

This solves the following problems:

* Makes it simpler to import textures as high quality, without having to worry about the specific format used.
* As the editor can now run on platforms such as web, Mac OS with Apple Silicion and Android, it should no longer be assumed that S3TC/BPTC is available by default for it.
This commit is contained in:
Juan Linietsky
2023-01-25 12:17:11 +01:00
parent 17a8597355
commit 28f51ba547
30 changed files with 457 additions and 265 deletions

View File

@ -35,7 +35,7 @@
#include <astcenc.h>
void _compress_astc(Image *r_img, float p_lossy_quality, Image::ASTCFormat p_format) {
void _compress_astc(Image *r_img, Image::ASTCFormat p_format) {
uint64_t start_time = OS::get_singleton()->get_ticks_msec();
// TODO: See how to handle lossy quality.
@ -83,65 +83,91 @@ void _compress_astc(Image *r_img, float p_lossy_quality, Image::ASTCFormat p_for
const bool mipmaps = r_img->has_mipmaps();
int width = r_img->get_width();
int height = r_img->get_height();
int required_width = (width % block_x) != 0 ? width + (block_x - (width % block_x)) : width;
int required_height = (height % block_y) != 0 ? height + (block_y - (height % block_y)) : height;
if (width != required_width || height != required_height) {
// Resize texture to fit block size.
r_img->resize(required_width, required_height);
width = required_width;
height = required_height;
}
print_verbose(vformat("astcenc: Encoding image size %dx%d to format %s%s.", width, height, Image::get_format_name(target_format), mipmaps ? ", with mipmaps" : ""));
// Initialize astcenc.
int dest_size = Image::get_image_data_size(width, height, target_format, mipmaps);
Vector<uint8_t> dest_data;
dest_data.resize(dest_size);
uint8_t *dest_write = dest_data.ptrw();
astcenc_config config;
config.block_x = block_x;
config.block_y = block_y;
config.profile = profile;
const float quality = ASTCENC_PRE_MEDIUM;
astcenc_error status = astcenc_config_init(profile, block_x, block_y, block_x, quality, 0, &config);
const float quality = ASTCENC_PRE_MEDIUM;
astcenc_error status = astcenc_config_init(profile, block_x, block_y, 1, quality, 0, &config);
ERR_FAIL_COND_MSG(status != ASTCENC_SUCCESS,
vformat("astcenc: Configuration initialization failed: %s.", astcenc_get_error_string(status)));
// Context allocation.
astcenc_context *context;
const unsigned int thread_count = OS::get_singleton()->get_processor_count();
const unsigned int thread_count = 1; // Godot compresses multiple images each on a thread, which is more efficient for large amount of images imported.
status = astcenc_context_alloc(&config, thread_count, &context);
ERR_FAIL_COND_MSG(status != ASTCENC_SUCCESS,
vformat("astcenc: Context allocation failed: %s.", astcenc_get_error_string(status)));
// Compress image.
Vector<uint8_t> image_data = r_img->get_data();
uint8_t *slices = image_data.ptrw();
astcenc_image image;
image.dim_x = width;
image.dim_y = height;
image.dim_z = 1;
image.data_type = ASTCENC_TYPE_U8;
if (is_hdr) {
image.data_type = ASTCENC_TYPE_F32;
int mip_count = mipmaps ? Image::get_image_required_mipmaps(width, height, target_format) : 0;
for (int i = 0; i < mip_count + 1; i++) {
int src_mip_w, src_mip_h;
int src_ofs = Image::get_image_mipmap_offset_and_dimensions(width, height, r_img->get_format(), i, src_mip_w, src_mip_h);
const uint8_t *slices = &image_data.ptr()[src_ofs];
int dst_mip_w, dst_mip_h;
int dst_ofs = Image::get_image_mipmap_offset_and_dimensions(width, height, target_format, i, dst_mip_w, dst_mip_h);
// Ensure that mip offset is a multiple of 8 (etcpak expects uint64_t pointer).
ERR_FAIL_COND(dst_ofs % 8 != 0);
uint8_t *dest_mip_write = (uint8_t *)&dest_write[dst_ofs];
// Compress image.
astcenc_image image;
image.dim_x = src_mip_w;
image.dim_y = src_mip_h;
image.dim_z = 1;
image.data_type = ASTCENC_TYPE_U8;
if (is_hdr) {
image.data_type = ASTCENC_TYPE_F32;
}
image.data = (void **)(&slices);
// Compute the number of ASTC blocks in each dimension.
unsigned int block_count_x = (src_mip_w + block_x - 1) / block_x;
unsigned int block_count_y = (src_mip_h + block_y - 1) / block_y;
size_t comp_len = block_count_x * block_count_y * 16;
const astcenc_swizzle swizzle = {
ASTCENC_SWZ_R, ASTCENC_SWZ_G, ASTCENC_SWZ_B, ASTCENC_SWZ_A
};
status = astcenc_compress_image(context, &image, &swizzle, dest_mip_write, comp_len, 0);
ERR_BREAK_MSG(status != ASTCENC_SUCCESS,
vformat("astcenc: ASTC image compression failed: %s.", astcenc_get_error_string(status)));
astcenc_compress_reset(context);
}
image.data = reinterpret_cast<void **>(&slices);
// Compute the number of ASTC blocks in each dimension.
unsigned int block_count_x = (width + block_x - 1) / block_x;
unsigned int block_count_y = (height + block_y - 1) / block_y;
size_t comp_len = block_count_x * block_count_y * 16;
Vector<uint8_t> compressed_data;
compressed_data.resize(comp_len);
compressed_data.fill(0);
const astcenc_swizzle swizzle = {
ASTCENC_SWZ_R, ASTCENC_SWZ_G, ASTCENC_SWZ_B, ASTCENC_SWZ_A
};
status = astcenc_compress_image(context, &image, &swizzle, compressed_data.ptrw(), comp_len, 0);
ERR_FAIL_COND_MSG(status != ASTCENC_SUCCESS,
vformat("astcenc: ASTC image compression failed: %s.", astcenc_get_error_string(status)));
astcenc_context_free(context);
// Replace original image with compressed one.
r_img->set_data(width, height, mipmaps, target_format, compressed_data);
r_img->set_data(width, height, mipmaps, target_format, dest_data);
print_verbose(vformat("astcenc: Encoding took %s ms.", rtos(OS::get_singleton()->get_ticks_msec() - start_time)));
}
@ -184,68 +210,81 @@ void _decompress_astc(Image *r_img) {
astcenc_config config;
const float quality = ASTCENC_PRE_MEDIUM;
astcenc_error status = astcenc_config_init(profile, block_x, block_y, block_x, quality, 0, &config);
astcenc_error status = astcenc_config_init(profile, block_x, block_y, 1, quality, 0, &config);
ERR_FAIL_COND_MSG(status != ASTCENC_SUCCESS,
vformat("astcenc: Configuration initialization failed: %s.", astcenc_get_error_string(status)));
// Context allocation.
astcenc_context *context = nullptr;
const unsigned int thread_count = OS::get_singleton()->get_processor_count();
const unsigned int thread_count = 1;
status = astcenc_context_alloc(&config, thread_count, &context);
ERR_FAIL_COND_MSG(status != ASTCENC_SUCCESS,
vformat("astcenc: Context allocation failed: %s.", astcenc_get_error_string(status)));
// Decompress image.
Image::Format target_format = is_hdr ? Image::FORMAT_RGBAF : Image::FORMAT_RGBA8;
const bool mipmaps = r_img->has_mipmaps();
int width = r_img->get_width();
int height = r_img->get_height();
int dest_size = Image::get_image_data_size(width, height, target_format, mipmaps);
Vector<uint8_t> dest_data;
dest_data.resize(dest_size);
uint8_t *dest_write = dest_data.ptrw();
astcenc_image image;
image.dim_x = width;
image.dim_y = height;
image.dim_z = 1;
image.data_type = ASTCENC_TYPE_U8;
Image::Format target_format = Image::FORMAT_RGBA8;
if (is_hdr) {
target_format = Image::FORMAT_RGBAF;
image.data_type = ASTCENC_TYPE_F32;
}
// Decompress image.
Vector<uint8_t> image_data = r_img->get_data();
int mip_count = mipmaps ? Image::get_image_required_mipmaps(width, height, target_format) : 0;
Vector<uint8_t> new_image_data;
new_image_data.resize(Image::get_image_data_size(width, height, target_format, false));
new_image_data.fill(0);
uint8_t *slices = new_image_data.ptrw();
image.data = reinterpret_cast<void **>(&slices);
for (int i = 0; i < mip_count + 1; i++) {
int src_mip_w, src_mip_h;
const astcenc_swizzle swizzle = {
ASTCENC_SWZ_R, ASTCENC_SWZ_G, ASTCENC_SWZ_B, ASTCENC_SWZ_A
};
int src_ofs = Image::get_image_mipmap_offset_and_dimensions(width, height, r_img->get_format(), i, src_mip_w, src_mip_h);
const uint8_t *src_data = &image_data.ptr()[src_ofs];
int src_size;
if (i == mip_count) {
src_size = image_data.size() - src_ofs;
} else {
int auxw, auxh;
src_size = Image::get_image_mipmap_offset_and_dimensions(width, height, r_img->get_format(), i + 1, auxw, auxh) - src_ofs;
}
status = astcenc_decompress_image(context, image_data.ptr(), image_data.size(), &image, &swizzle, 0);
ERR_FAIL_COND_MSG(status != ASTCENC_SUCCESS,
vformat("astcenc: ASTC decompression failed: %s.", astcenc_get_error_string(status)));
ERR_FAIL_COND_MSG(image.dim_z > 1,
"astcenc: ASTC decompression failed because this is a 3D texture, which is not supported.");
int dst_mip_w, dst_mip_h;
int dst_ofs = Image::get_image_mipmap_offset_and_dimensions(width, height, target_format, i, dst_mip_w, dst_mip_h);
// Ensure that mip offset is a multiple of 8 (etcpak expects uint64_t pointer).
ERR_FAIL_COND(dst_ofs % 8 != 0);
uint8_t *dest_mip_write = (uint8_t *)&dest_write[dst_ofs];
astcenc_image image;
image.dim_x = dst_mip_w;
image.dim_y = dst_mip_h;
image.dim_z = 1;
image.data_type = ASTCENC_TYPE_U8;
if (is_hdr) {
target_format = Image::FORMAT_RGBAF;
image.data_type = ASTCENC_TYPE_F32;
}
image.data = (void **)(&dest_mip_write);
const astcenc_swizzle swizzle = {
ASTCENC_SWZ_R, ASTCENC_SWZ_G, ASTCENC_SWZ_B, ASTCENC_SWZ_A
};
status = astcenc_decompress_image(context, src_data, src_size, &image, &swizzle, 0);
ERR_BREAK_MSG(status != ASTCENC_SUCCESS,
vformat("astcenc: ASTC decompression failed: %s.", astcenc_get_error_string(status)));
ERR_BREAK_MSG(image.dim_z > 1,
"astcenc: ASTC decompression failed because this is a 3D texture, which is not supported.");
astcenc_compress_reset(context);
}
astcenc_context_free(context);
// Replace original image with compressed one.
Image::Format image_format = Image::FORMAT_RGBA8;
if (image.data_type == ASTCENC_TYPE_F32) {
image_format = Image::FORMAT_RGBAF;
} else if (image.data_type == ASTCENC_TYPE_U8) {
image_format = Image::FORMAT_RGBA8;
} else if (image.data_type == ASTCENC_TYPE_F16) {
image_format = Image::FORMAT_RGBAH;
} else {
ERR_FAIL_MSG("astcenc: ASTC decompression failed with an unknown format.");
}
r_img->set_data(image.dim_x, image.dim_y, mipmaps, image_format, new_image_data);
r_img->set_data(width, height, mipmaps, target_format, dest_data);
print_verbose(vformat("astcenc: Decompression took %s ms.", rtos(OS::get_singleton()->get_ticks_msec() - start_time)));
}

View File

@ -33,7 +33,7 @@
#include "core/io/image.h"
void _compress_astc(Image *r_img, float p_lossy_quality, Image::ASTCFormat p_format);
void _compress_astc(Image *r_img, Image::ASTCFormat p_format);
void _decompress_astc(Image *r_img);
#endif // IMAGE_COMPRESS_ASTCENC_H