Merge pull request #86835 from nklbdev/Fix_data_format_storage_method_for_PortableCompressedTexture2D

Fix data format storage method for PortableCompressedTexture2D
This commit is contained in:
Rémi Verschelde 2024-01-06 23:19:58 +01:00 committed by GitHub
commit c8c483cf57
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 12 deletions

View File

@ -40,7 +40,6 @@ class CompressedTexture2D : public Texture2D {
public:
enum DataFormat {
DATA_FORMAT_UNDEFINED,
DATA_FORMAT_IMAGE,
DATA_FORMAT_PNG,
DATA_FORMAT_WEBP,

View File

@ -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<uint8_t> &p_data) {
if (p_data.size() == 0) {
@ -44,7 +43,7 @@ void PortableCompressedTexture2D::_set_data(const Vector<uint8_t> &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<uint8_t> &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<Image> &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<Image> &p_image, C
Vector<uint8_t> 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<Image> &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<uint8_t> budata = Image::basis_universal_packer(p_image, uc);
buffer.append_array(budata);
@ -181,7 +180,7 @@ void PortableCompressedTexture2D::create_from_image(const Ref<Image> &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<Image> copy = p_image->duplicate();
switch (p_compression_mode) {
case COMPRESSION_MODE_S3TC:

View File

@ -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,