Godot Engine – Multi-platform 2D and 3D game engine
Use resizing to align textures to the next block size when compressing
BlueCube3310c020a9f15f04
diff --git a/modules/astcenc/image_compress_astcenc.cpp b/modules/astcenc/image_compress_astcenc.cppindex 6c23b17f6ef..bc5064ca416 100644--- a/modules/astcenc/image_compress_astcenc.cpp+++ b/modules/astcenc/image_compress_astcenc.cpp@@ -98,16 +98,15 @@ void _compress_astc(Image *r_img, Image::UsedChannels p_channels, Image::Compres// Compress image data and (if required) mipmaps.const bool has_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;+ width = (width % block_x) != 0 ? width + (block_x - (width % block_x)) : width;+ height = (height % block_y) != 0 ? height + (block_y - (height % block_y)) : height;++ if (r_img->get_width() != width || r_img->get_height() != height) {+ // Align the image to the block size.+ r_img->resize(width, height, Image::INTERPOLATE_NEAREST);}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" : ""));diff --git a/modules/basis_universal/image_compress_basisu.cpp b/modules/basis_universal/image_compress_basisu.cppindex bcac8553bc7..097a010f639 100644--- a/modules/basis_universal/image_compress_basisu.cpp+++ b/modules/basis_universal/image_compress_basisu.cpp@@ -56,36 +56,6 @@ void basis_universal_init() {}#ifdef TOOLS_ENABLED-template <typename T>-inline void _basisu_pad_mipmap(const uint8_t *p_image_mip_data, Vector<uint8_t> &r_mip_data_padded, int p_next_width, int p_next_height, int p_width, int p_height, int64_t p_size) {- // Source mip's data interpreted as 32-bit RGBA blocks to help with copying pixel data.- const T *mip_src_data = reinterpret_cast<const T *>(p_image_mip_data);-- // Reserve space in the padded buffer.- r_mip_data_padded.resize(p_next_width * p_next_height * sizeof(T));- T *data_padded_ptr = reinterpret_cast<T *>(r_mip_data_padded.ptrw());-- // Pad mipmap to the nearest block by smearing.- int x = 0, y = 0;- for (y = 0; y < p_height; y++) {- for (x = 0; x < p_width; x++) {- data_padded_ptr[p_next_width * y + x] = mip_src_data[p_width * y + x];- }-- // First, smear in x.- for (; x < p_next_width; x++) {- data_padded_ptr[p_next_width * y + x] = data_padded_ptr[p_next_width * y + x - 1];- }- }-- // Then, smear in y.- for (; y < p_next_height; y++) {- for (x = 0; x < p_next_width; x++) {- data_padded_ptr[p_next_width * y + x] = data_padded_ptr[p_next_width * y + x - p_next_width];- }- }-}-Vector<uint8_t> basis_universal_packer(const Ref<Image> &p_image, Image::UsedChannels p_channels, const Image::BasisUniversalPackerParams &p_basisu_params) {init_mutex.lock();if (!initialized) {@@ -175,53 +145,28 @@ Vector<uint8_t> basis_universal_packer(const Ref<Image> &p_image, Image::UsedCha// Copy the source image data with mipmaps into BasisU.{- const int orig_width = image->get_width();- const int orig_height = image->get_height();-- bool is_res_div_4 = (orig_width % 4 == 0) && (orig_height % 4 == 0);+ int next_width = (image->get_width() + 3) & ~0x03;+ int next_height = (image->get_height() + 3) & ~0x03;- // Image's resolution rounded up to the nearest values divisible by 4.- int next_width = orig_width <= 2 ? orig_width : (orig_width + 3) & ~3;- int next_height = orig_height <= 2 ? orig_height : (orig_height + 3) & ~3;+ if (image->get_width() != next_width || image->get_height() != next_height) {+ // Align the image to 4x4 texels.+ image->resize(next_width, next_height, Image::INTERPOLATE_NEAREST);+ }Vector<uint8_t> image_data = image->get_data();basisu::vector<basisu::image> basisu_mipmaps;basisu::vector<basisu::imagef> basisu_mipmaps_hdr;- // Buffer for storing padded mipmap data.- Vector<uint8_t> mip_data_padded;-for (int32_t i = 0; i <= image->get_mipmap_count(); i++) {int64_t ofs, size;- int width, height;- image->get_mipmap_offset_size_and_dimensions(i, ofs, size, width, height);+ int mip_width, mip_height;+ image->get_mipmap_offset_size_and_dimensions(i, ofs, size, mip_width, mip_height);const uint8_t *image_mip_data = image_data.ptr() + ofs;- // Pad the mipmap's data if its resolution isn't divisible by 4.- if (image->has_mipmaps() && !is_res_div_4 && (width > 2 && height > 2) && (width != next_width || height != next_height)) {- if (is_hdr) {- _basisu_pad_mipmap<BasisRGBAF>(image_mip_data, mip_data_padded, next_width, next_height, width, height, size);- } else {- _basisu_pad_mipmap<uint32_t>(image_mip_data, mip_data_padded, next_width, next_height, width, height, size);- }-- // Override the image_mip_data pointer with our temporary Vector.- image_mip_data = reinterpret_cast<const uint8_t *>(mip_data_padded.ptr());-- // Override the mipmap's properties.- width = next_width;- height = next_height;- size = mip_data_padded.size();- }-- // Get the next mipmap's resolution.- next_width /= 2;- next_height /= 2;-// Copy the source mipmap's data to a BasisU image.if (is_hdr) {- basisu::imagef basisu_image(width, height);+ basisu::imagef basisu_image(mip_width, mip_height);memcpy(reinterpret_cast<uint8_t *>(basisu_image.get_ptr()), image_mip_data, size);if (i == 0) {@@ -231,7 +176,7 @@ Vector<uint8_t> basis_universal_packer(const Ref<Image> &p_image, Image::UsedCha}} else {- basisu::image basisu_image(width, height);+ basisu::image basisu_image(mip_width, mip_height);memcpy(basisu_image.get_ptr(), image_mip_data, size);if (i == 0) {diff --git a/modules/basis_universal/image_compress_basisu.h b/modules/basis_universal/image_compress_basisu.hindex b30d049524e..3952282d817 100644--- a/modules/basis_universal/image_compress_basisu.h+++ b/modules/basis_universal/image_compress_basisu.h@@ -46,13 +46,6 @@ constexpr uint32_t BASIS_DECOMPRESS_FLAG_KTX2 = 1 << 31;void basis_universal_init();#ifdef TOOLS_ENABLED-struct BasisRGBAF {- uint32_t r;- uint32_t g;- uint32_t b;- uint32_t a;-};-Vector<uint8_t> basis_universal_packer(const Ref<Image> &p_image, Image::UsedChannels p_channels, const Image::BasisUniversalPackerParams &p_basisu_params);#endifdiff --git a/modules/bcdec/image_decompress_bcdec.cpp b/modules/bcdec/image_decompress_bcdec.cppindex dfc805d92e7..f66b930722c 100644--- a/modules/bcdec/image_decompress_bcdec.cpp+++ b/modules/bcdec/image_decompress_bcdec.cpp@@ -272,7 +272,7 @@ void image_decompress_bcdec(Image *p_image) {break;}- int mm_count = p_image->get_mipmap_count();+ int mm_count = p_image->has_mipmaps() ? Image::get_image_required_mipmaps(width, height, target_format) : 0;int64_t target_size = Image::get_image_data_size(width, height, target_format, p_image->has_mipmaps());// Decompressed data.diff --git a/modules/betsy/image_compress_betsy.cpp b/modules/betsy/image_compress_betsy.cppindex 447187034cd..eaae2886013 100644--- a/modules/betsy/image_compress_betsy.cpp+++ b/modules/betsy/image_compress_betsy.cpp@@ -441,11 +441,12 @@ Error BetsyCompressor::_compress(BetsyFormat p_format, Image *r_img) {return ERR_INVALID_DATA;}- int img_width = r_img->get_width();- int img_height = r_img->get_height();- if (img_width % 4 != 0 || img_height % 4 != 0) {- img_width = img_width <= 2 ? img_width : (img_width + 3) & ~3;- img_height = img_height <= 2 ? img_height : (img_height + 3) & ~3;+ int img_width = (r_img->get_width() + 3) & ~0x03;+ int img_height = (r_img->get_height() + 3) & ~0x03;++ if (r_img->get_width() != img_width || r_img->get_height() != img_height) {+ // Align the image to 4x4 texels.+ r_img->resize(img_width, img_height, Image::INTERPOLATE_NEAREST);}Error err = OK;@@ -549,38 +550,9 @@ Error BetsyCompressor::_compress(BetsyFormat p_format, Image *r_img) {dst_texture_format.height = (height + 3) >> 2;dst_texture_format.width = (width + 3) >> 2;- // Pad textures to nearest block by smearing.- if (width != src_mip_w || height != src_mip_h) {- const uint8_t *src_mip_read = r_img->ptr() + src_mip_ofs;-- // Reserve the buffer for padded image data.- int px_size = Image::get_format_pixel_size(r_img->get_format());- src_image_ptr[0].resize(width * height * px_size);- uint8_t *ptrw = src_image_ptr[0].ptrw();-- int x = 0, y = 0;- for (y = 0; y < src_mip_h; y++) {- for (x = 0; x < src_mip_w; x++) {- memcpy(ptrw + (width * y + x) * px_size, src_mip_read + (src_mip_w * y + x) * px_size, px_size);- }-- // First, smear in x.- for (; x < width; x++) {- memcpy(ptrw + (width * y + x) * px_size, ptrw + (width * y + x - 1) * px_size, px_size);- }- }-- // Then, smear in y.- for (; y < height; y++) {- for (x = 0; x < width; x++) {- memcpy(ptrw + (width * y + x) * px_size, ptrw + (width * y + x - width) * px_size, px_size);- }- }- } else {- // Create a buffer filled with the source mip layer data.- src_image_ptr[0].resize(src_mip_size);- memcpy(src_image_ptr[0].ptrw(), r_img->ptr() + src_mip_ofs, src_mip_size);- }+ // Create a buffer filled with the source mip layer data.+ src_image_ptr[0].resize(src_mip_size);+ memcpy(src_image_ptr[0].ptrw(), r_img->ptr() + src_mip_ofs, src_mip_size);// Create the textures on the GPU.RID src_texture;diff --git a/modules/cvtt/image_compress_cvtt.cpp b/modules/cvtt/image_compress_cvtt.cppindex 51db6dbef4c..da3c3222101 100644--- a/modules/cvtt/image_compress_cvtt.cpp+++ b/modules/cvtt/image_compress_cvtt.cpp@@ -148,12 +148,12 @@ void image_compress_cvtt(Image *p_image, Image::UsedChannels p_channels, Image::return; //do not compress, already compressed}- int w = p_image->get_width();- int h = p_image->get_height();+ int w = (p_image->get_width() + 3) & ~0x03;+ int h = (p_image->get_height() + 3) & ~0x03;- if (w % 4 != 0 || h % 4 != 0) {- w = w <= 2 ? w : (w + 3) & ~3;- h = h <= 2 ? h : (h + 3) & ~3;+ if (p_image->get_width() != w || p_image->get_height() != h) {+ // Align the image to 4x4 texels.+ p_image->resize(w, h, Image::INTERPOLATE_NEAREST);}bool is_ldr = (p_image->get_format() <= Image::FORMAT_RGBA8);diff --git a/modules/etcpak/image_compress_etcpak.cpp b/modules/etcpak/image_compress_etcpak.cppindex 1d8c936079d..531a5ba97a9 100644--- a/modules/etcpak/image_compress_etcpak.cpp+++ b/modules/etcpak/image_compress_etcpak.cpp@@ -163,8 +163,6 @@ void _compress_etcpak(EtcpakType p_compress_type, Image *r_img) {// Compress image data and (if required) mipmaps.const bool has_mipmaps = r_img->has_mipmaps();- int width = r_img->get_width();- int height = r_img->get_height();/*The first mipmap level of a compressed texture must be a multiple of 4. Quote from D3D11.3 spec:@@ -184,9 +182,12 @@ void _compress_etcpak(EtcpakType p_compress_type, Image *r_img) {the surface pitch, which can encompass additional padding beyond the physical surface size.*/- if (width % 4 != 0 || height % 4 != 0) {- width = width <= 2 ? width : (width + 3) & ~3;- height = height <= 2 ? height : (height + 3) & ~3;+ int width = (r_img->get_width() + 3) & ~0x03;+ int height = (r_img->get_height() + 3) & ~0x03;++ if (r_img->get_width() != width || r_img->get_height() != height) {+ // Align the image to 4x4 texels.+ r_img->resize(width, height, Image::INTERPOLATE_NEAREST);}// Multiple-of-4 should be guaranteed by above.diff --git a/modules/etcpak/image_decompress_etcpak.cpp b/modules/etcpak/image_decompress_etcpak.cppindex 9aff3bbbb9d..1e2011ae887 100644--- a/modules/etcpak/image_decompress_etcpak.cpp+++ b/modules/etcpak/image_decompress_etcpak.cpp@@ -214,7 +214,7 @@ void _decompress_etc(Image *p_image) {break;}- int mm_count = p_image->get_mipmap_count();+ int mm_count = p_image->has_mipmaps() ? Image::get_image_required_mipmaps(width, height, target_format) : 0;int64_t target_size = Image::get_image_data_size(width, height, target_format, p_image->has_mipmaps());// Decompressed data.