Skip to content

Commit

Permalink
Fix lossless formats in PortableCompressedTexture2D (fix data format …
Browse files Browse the repository at this point in the history
…storing)
  • Loading branch information
nklbdev committed Jan 6, 2024
1 parent 89cc635 commit e72ccc2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
1 change: 0 additions & 1 deletion scene/resources/compressed_texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ class CompressedTexture2D : public Texture2D {

public:
enum DataFormat {
DATA_FORMAT_UNDEFINED,
DATA_FORMAT_IMAGE,
DATA_FORMAT_PNG,
DATA_FORMAT_WEBP,
Expand Down
21 changes: 10 additions & 11 deletions scene/resources/portable_compressed_texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
Expand All @@ -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();
Expand Down Expand Up @@ -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);
Expand All @@ -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();
Expand All @@ -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);
Expand All @@ -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:
Expand Down
8 changes: 8 additions & 0 deletions scene/resources/portable_compressed_texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit e72ccc2

Please sign in to comment.