From e72ccc2080d138d64e90cab005987383fdee94f3 Mon Sep 17 00:00:00 2001 From: nklbdev Date: Sat, 6 Jan 2024 22:48:50 +0500 Subject: [PATCH] Fix lossless formats in PortableCompressedTexture2D (fix data format storing) --- scene/resources/compressed_texture.h | 1 - .../resources/portable_compressed_texture.cpp | 21 +++++++++---------- scene/resources/portable_compressed_texture.h | 8 +++++++ 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/scene/resources/compressed_texture.h b/scene/resources/compressed_texture.h index 932109617f7..5297d79cfe9 100644 --- a/scene/resources/compressed_texture.h +++ b/scene/resources/compressed_texture.h @@ -40,7 +40,6 @@ class CompressedTexture2D : public Texture2D { public: enum DataFormat { - DATA_FORMAT_UNDEFINED, DATA_FORMAT_IMAGE, DATA_FORMAT_PNG, DATA_FORMAT_WEBP, diff --git a/scene/resources/portable_compressed_texture.cpp b/scene/resources/portable_compressed_texture.cpp index a67f32f67e7..19a96d6ab27 100644 --- a/scene/resources/portable_compressed_texture.cpp +++ b/scene/resources/portable_compressed_texture.cpp @@ -33,7 +33,6 @@ #include "core/config/project_settings.h" #include "core/io/marshalls.h" #include "scene/resources/bit_map.h" -#include "scene/resources/compressed_texture.h" void PortableCompressedTexture2D::_set_data(const Vector &p_data) { if (p_data.size() == 0) { @@ -44,7 +43,7 @@ void PortableCompressedTexture2D::_set_data(const Vector &p_data) { uint32_t data_size = p_data.size(); ERR_FAIL_COND(data_size < 20); compression_mode = CompressionMode(decode_uint16(data)); - CompressedTexture2D::DataFormat data_format = CompressedTexture2D::DataFormat(decode_uint16(data + 2)); + DataFormat data_format = DataFormat(decode_uint16(data + 2)); format = Image::Format(decode_uint32(data + 4)); uint32_t mipmap_count = decode_uint32(data + 8); size.width = decode_uint32(data + 12); @@ -60,11 +59,11 @@ void PortableCompressedTexture2D::_set_data(const Vector &p_data) { case COMPRESSION_MODE_LOSSLESS: case COMPRESSION_MODE_LOSSY: { ImageMemLoadFunc loader_func; - if (data_format == CompressedTexture2D::DATA_FORMAT_UNDEFINED) { + if (data_format == DATA_FORMAT_UNDEFINED) { loader_func = nullptr; - } else if (data_format == CompressedTexture2D::DATA_FORMAT_PNG) { + } else if (data_format == DATA_FORMAT_PNG) { loader_func = Image::_png_mem_unpacker_func; - } else if (data_format == CompressedTexture2D::DATA_FORMAT_WEBP) { + } else if (data_format == DATA_FORMAT_WEBP) { loader_func = Image::_webp_mem_loader_func; } else { ERR_FAIL(); @@ -139,7 +138,7 @@ void PortableCompressedTexture2D::create_from_image(const Ref &p_image, C buffer.resize(20); encode_uint16(p_compression_mode, buffer.ptrw()); - encode_uint16(CompressedTexture2D::DATA_FORMAT_UNDEFINED, buffer.ptrw() + 2); + encode_uint16(DATA_FORMAT_UNDEFINED, buffer.ptrw() + 2); encode_uint32(p_image->get_format(), buffer.ptrw() + 4); encode_uint32(p_image->get_mipmap_count() + 1, buffer.ptrw() + 8); encode_uint32(p_image->get_width(), buffer.ptrw() + 12); @@ -155,14 +154,14 @@ void PortableCompressedTexture2D::create_from_image(const Ref &p_image, C Vector data; if (p_compression_mode == COMPRESSION_MODE_LOSSY) { data = Image::webp_lossy_packer(p_image->get_image_from_mipmap(i), p_lossy_quality); - encode_uint16(CompressedTexture2D::DATA_FORMAT_WEBP, buffer.ptrw() + 2); + encode_uint16(DATA_FORMAT_WEBP, buffer.ptrw() + 2); } else { if (use_webp) { data = Image::webp_lossless_packer(p_image->get_image_from_mipmap(i)); - encode_uint16(CompressedTexture2D::DATA_FORMAT_WEBP, buffer.ptrw() + 2); + encode_uint16(DATA_FORMAT_WEBP, buffer.ptrw() + 2); } else { data = Image::png_packer(p_image->get_image_from_mipmap(i)); - encode_uint16(CompressedTexture2D::DATA_FORMAT_PNG, buffer.ptrw() + 2); + encode_uint16(DATA_FORMAT_PNG, buffer.ptrw() + 2); } } int data_len = data.size(); @@ -172,7 +171,7 @@ void PortableCompressedTexture2D::create_from_image(const Ref &p_image, C } } break; case COMPRESSION_MODE_BASIS_UNIVERSAL: { - encode_uint16(CompressedTexture2D::DATA_FORMAT_BASIS_UNIVERSAL, buffer.ptrw() + 2); + encode_uint16(DATA_FORMAT_BASIS_UNIVERSAL, buffer.ptrw() + 2); Image::UsedChannels uc = p_image->detect_used_channels(p_normal_map ? Image::COMPRESS_SOURCE_NORMAL : Image::COMPRESS_SOURCE_GENERIC); Vector budata = Image::basis_universal_packer(p_image, uc); buffer.append_array(budata); @@ -181,7 +180,7 @@ void PortableCompressedTexture2D::create_from_image(const Ref &p_image, C case COMPRESSION_MODE_S3TC: case COMPRESSION_MODE_ETC2: case COMPRESSION_MODE_BPTC: { - encode_uint16(CompressedTexture2D::DATA_FORMAT_IMAGE, buffer.ptrw() + 2); + encode_uint16(DATA_FORMAT_IMAGE, buffer.ptrw() + 2); Ref copy = p_image->duplicate(); switch (p_compression_mode) { case COMPRESSION_MODE_S3TC: diff --git a/scene/resources/portable_compressed_texture.h b/scene/resources/portable_compressed_texture.h index 86d80e39f7e..3103c2daba5 100644 --- a/scene/resources/portable_compressed_texture.h +++ b/scene/resources/portable_compressed_texture.h @@ -39,6 +39,14 @@ class PortableCompressedTexture2D : public Texture2D { GDCLASS(PortableCompressedTexture2D, Texture2D); public: + enum DataFormat { + DATA_FORMAT_UNDEFINED, + DATA_FORMAT_IMAGE, + DATA_FORMAT_PNG, + DATA_FORMAT_WEBP, + DATA_FORMAT_BASIS_UNIVERSAL, + }; + enum CompressionMode { COMPRESSION_MODE_LOSSLESS, COMPRESSION_MODE_LOSSY,