From 344d97b0916387639ac4eaaf5e7bc96acffdc1b9 Mon Sep 17 00:00:00 2001 From: Vasiliy Makarov Date: Wed, 16 Sep 2020 18:11:11 +0300 Subject: [PATCH] Fix import logic for VRAM compressed texture formats Compressed stream texture now saves right image dimensions for every compression format. Also it allows to compressor resize image on it's side to make it more optimized. So ETC1 format becames smaller then before. Pvrtc format always saves square texture. --- editor/import/resource_importer_texture.cpp | 53 ++++++++++++++------- 1 file changed, 36 insertions(+), 17 deletions(-) diff --git a/editor/import/resource_importer_texture.cpp b/editor/import/resource_importer_texture.cpp index 88547280ceac..976328e98e8e 100644 --- a/editor/import/resource_importer_texture.cpp +++ b/editor/import/resource_importer_texture.cpp @@ -227,21 +227,8 @@ void ResourceImporterTexture::_save_stex(const Ref &p_image, const String f->store_8('S'); f->store_8('T'); //godot streamable texture - bool resize_to_po2 = false; - - if (p_compress_mode == COMPRESS_VIDEO_RAM && p_force_po2_for_compressed && (p_mipmaps || p_texture_flags & Texture::FLAG_REPEAT)) { - resize_to_po2 = true; - f->store_16(next_power_of_2(p_image->get_width())); - f->store_16(p_image->get_width()); - f->store_16(next_power_of_2(p_image->get_height())); - f->store_16(p_image->get_height()); - } else { - f->store_16(p_image->get_width()); - f->store_16(0); - f->store_16(p_image->get_height()); - f->store_16(0); - } - f->store_32(p_texture_flags); + int orig_width = p_image->get_width(); + int orig_height = p_image->get_height(); uint32_t format = 0; @@ -263,6 +250,12 @@ void ResourceImporterTexture::_save_stex(const Ref &p_image, const String switch (p_compress_mode) { case COMPRESS_LOSSLESS: { + f->store_16(orig_width); + f->store_16(0); + f->store_16(orig_height); + f->store_16(0); + f->store_32(p_texture_flags); + Ref image = p_image->duplicate(); if (p_mipmaps) { image->generate_mipmaps(); @@ -292,6 +285,13 @@ void ResourceImporterTexture::_save_stex(const Ref &p_image, const String } break; case COMPRESS_LOSSY: { + + f->store_16(orig_width); + f->store_16(0); + f->store_16(orig_height); + f->store_16(0); + f->store_32(p_texture_flags); + Ref image = p_image->duplicate(); if (p_mipmaps) { image->generate_mipmaps(); @@ -322,9 +322,15 @@ void ResourceImporterTexture::_save_stex(const Ref &p_image, const String case COMPRESS_VIDEO_RAM: { Ref image = p_image->duplicate(); - if (resize_to_po2) { - image->resize_to_po2(); + + if (p_force_po2_for_compressed || p_mipmaps || p_texture_flags & Texture::FLAG_REPEAT) { + if (p_vram_compression == Image::COMPRESS_PVRTC4 || p_vram_compression == Image::COMPRESS_ETC) { + // we don't need to resize image there, because encoder will resize it by more proper way + } else { + image->resize_to_po2(); + } } + if (p_mipmaps) { image->generate_mipmaps(p_force_normal); } @@ -342,6 +348,13 @@ void ResourceImporterTexture::_save_stex(const Ref &p_image, const String image->compress(p_vram_compression, csource, p_lossy_quality); } + // save image dimensions after compressing, because the compressor can resize it + f->store_16(image->get_width()); + f->store_16(orig_width); + f->store_16(image->get_height()); + f->store_16(orig_height); + f->store_32(p_texture_flags); + format |= image->get_format(); f->store_32(format); @@ -353,6 +366,12 @@ void ResourceImporterTexture::_save_stex(const Ref &p_image, const String } break; case COMPRESS_UNCOMPRESSED: { + f->store_16(orig_width); + f->store_16(0); + f->store_16(orig_height); + f->store_16(0); + f->store_32(p_texture_flags); + Ref image = p_image->duplicate(); if (p_mipmaps) { image->generate_mipmaps();