Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix squish DXT5 RA-As-RG channel swapping #85967

Merged
merged 1 commit into from
Dec 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 32 additions & 14 deletions modules/squish/image_decompress_squish.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ void image_decompress_squish(Image *p_image) {
int w = p_image->get_width();
int h = p_image->get_height();

Image::Format source_format = p_image->get_format();
Image::Format target_format = Image::FORMAT_RGBA8;

Vector<uint8_t> data;
int target_size = Image::get_image_data_size(w, h, target_format, p_image->has_mipmaps());
int mm_count = p_image->get_mipmap_count();
Expand All @@ -45,33 +47,49 @@ void image_decompress_squish(Image *p_image) {
const uint8_t *rb = p_image->get_data().ptr();
uint8_t *wb = data.ptrw();

int squish_flags = Image::FORMAT_MAX;
if (p_image->get_format() == Image::FORMAT_DXT1) {
squish_flags = squish::kDxt1;
} else if (p_image->get_format() == Image::FORMAT_DXT3) {
squish_flags = squish::kDxt3;
} else if (p_image->get_format() == Image::FORMAT_DXT5 || p_image->get_format() == Image::FORMAT_DXT5_RA_AS_RG) {
squish_flags = squish::kDxt5;
} else if (p_image->get_format() == Image::FORMAT_RGTC_R) {
squish_flags = squish::kBc4;
} else if (p_image->get_format() == Image::FORMAT_RGTC_RG) {
squish_flags = squish::kBc5;
} else {
ERR_FAIL_MSG("Squish: Can't decompress unknown format: " + itos(p_image->get_format()) + ".");
int squish_flags = 0;

switch (source_format) {
case Image::FORMAT_DXT1:
squish_flags = squish::kDxt1;
break;

case Image::FORMAT_DXT3:
squish_flags = squish::kDxt3;
break;

case Image::FORMAT_DXT5:
case Image::FORMAT_DXT5_RA_AS_RG:
squish_flags = squish::kDxt5;
break;

case Image::FORMAT_RGTC_R:
squish_flags = squish::kBc4;
break;

case Image::FORMAT_RGTC_RG:
squish_flags = squish::kBc5;
break;

default:
ERR_FAIL_MSG("Squish: Can't decompress unknown format: " + itos(p_image->get_format()) + ".");
break;
}

for (int i = 0; i <= mm_count; i++) {
int src_ofs = 0, mipmap_size = 0, mipmap_w = 0, mipmap_h = 0;
p_image->get_mipmap_offset_size_and_dimensions(i, src_ofs, mipmap_size, mipmap_w, mipmap_h);

int dst_ofs = Image::get_image_mipmap_offset(p_image->get_width(), p_image->get_height(), target_format, i);
squish::DecompressImage(&wb[dst_ofs], w, h, &rb[src_ofs], squish_flags);

w >>= 1;
h >>= 1;
}

p_image->set_data(p_image->get_width(), p_image->get_height(), p_image->has_mipmaps(), target_format, data);

if (p_image->get_format() == Image::FORMAT_DXT5_RA_AS_RG) {
if (source_format == Image::FORMAT_DXT5_RA_AS_RG) {
p_image->convert_ra_rgba8_to_rg();
}
}
Loading