123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- #include "image_compress_etcpak.h"
- #include "core/os/os.h"
- #include "core/string/print_string.h"
- #include <ProcessDxtc.hpp>
- #include <ProcessRGB.hpp>
- EtcpakType _determine_etc_type(Image::UsedChannels p_channels) {
- switch (p_channels) {
- case Image::USED_CHANNELS_L:
- return EtcpakType::ETCPAK_TYPE_ETC1;
- case Image::USED_CHANNELS_LA:
- return EtcpakType::ETCPAK_TYPE_ETC2_ALPHA;
- case Image::USED_CHANNELS_R:
- return EtcpakType::ETCPAK_TYPE_ETC2;
- case Image::USED_CHANNELS_RG:
- return EtcpakType::ETCPAK_TYPE_ETC2_RA_AS_RG;
- case Image::USED_CHANNELS_RGB:
- return EtcpakType::ETCPAK_TYPE_ETC2;
- case Image::USED_CHANNELS_RGBA:
- return EtcpakType::ETCPAK_TYPE_ETC2_ALPHA;
- default:
- return EtcpakType::ETCPAK_TYPE_ETC2_ALPHA;
- }
- }
- EtcpakType _determine_dxt_type(Image::UsedChannels p_channels) {
- switch (p_channels) {
- case Image::USED_CHANNELS_L:
- return EtcpakType::ETCPAK_TYPE_DXT1;
- case Image::USED_CHANNELS_LA:
- return EtcpakType::ETCPAK_TYPE_DXT5;
- case Image::USED_CHANNELS_R:
- return EtcpakType::ETCPAK_TYPE_DXT5;
- case Image::USED_CHANNELS_RG:
- return EtcpakType::ETCPAK_TYPE_DXT5_RA_AS_RG;
- case Image::USED_CHANNELS_RGB:
- return EtcpakType::ETCPAK_TYPE_DXT1;
- case Image::USED_CHANNELS_RGBA:
- return EtcpakType::ETCPAK_TYPE_DXT5;
- default:
- return EtcpakType::ETCPAK_TYPE_DXT5;
- }
- }
- void _compress_etc1(Image *r_img) {
- _compress_etcpak(EtcpakType::ETCPAK_TYPE_ETC1, r_img);
- }
- void _compress_etc2(Image *r_img, Image::UsedChannels p_channels) {
- EtcpakType type = _determine_etc_type(p_channels);
- _compress_etcpak(type, r_img);
- }
- void _compress_bc(Image *r_img, Image::UsedChannels p_channels) {
- EtcpakType type = _determine_dxt_type(p_channels);
- _compress_etcpak(type, r_img);
- }
- void _compress_etcpak(EtcpakType p_compresstype, Image *r_img) {
- uint64_t start_time = OS::get_singleton()->get_ticks_msec();
- Image::Format img_format = r_img->get_format();
- if (img_format >= Image::FORMAT_DXT1) {
- return;
- }
- if (img_format > Image::FORMAT_RGBA8) {
-
- return;
- }
-
- if (img_format != Image::FORMAT_RGBA8) {
- r_img->convert(Image::FORMAT_RGBA8);
- }
-
- Image::Format target_format = Image::FORMAT_RGBA8;
- if (p_compresstype == EtcpakType::ETCPAK_TYPE_ETC1) {
- target_format = Image::FORMAT_ETC;
- r_img->convert_rgba8_to_bgra8();
- } else if (p_compresstype == EtcpakType::ETCPAK_TYPE_ETC2) {
- target_format = Image::FORMAT_ETC2_RGB8;
- r_img->convert_rgba8_to_bgra8();
- } else if (p_compresstype == EtcpakType::ETCPAK_TYPE_ETC2_RA_AS_RG) {
- target_format = Image::FORMAT_ETC2_RA_AS_RG;
- r_img->convert_rg_to_ra_rgba8();
- r_img->convert_rgba8_to_bgra8();
- } else if (p_compresstype == EtcpakType::ETCPAK_TYPE_ETC2_ALPHA) {
- target_format = Image::FORMAT_ETC2_RGBA8;
- r_img->convert_rgba8_to_bgra8();
- } else if (p_compresstype == EtcpakType::ETCPAK_TYPE_DXT1) {
- target_format = Image::FORMAT_DXT1;
- } else if (p_compresstype == EtcpakType::ETCPAK_TYPE_DXT5_RA_AS_RG) {
- target_format = Image::FORMAT_DXT5_RA_AS_RG;
- r_img->convert_rg_to_ra_rgba8();
- } else if (p_compresstype == EtcpakType::ETCPAK_TYPE_DXT5) {
- target_format = Image::FORMAT_DXT5;
- } else {
- ERR_FAIL_MSG("Invalid or unsupported etcpak compression format, not ETC or DXT.");
- }
-
- const bool mipmaps = r_img->has_mipmaps();
- int width = r_img->get_width();
- int height = r_img->get_height();
-
- int next_width = width <= 2 ? width : (width + 3) & ~3;
- int next_height = height <= 2 ? height : (height + 3) & ~3;
- if (next_width != width || next_height != height) {
- r_img->resize(next_width, next_height, Image::INTERPOLATE_LANCZOS);
- width = r_img->get_width();
- height = r_img->get_height();
- }
-
-
-
-
-
- const uint8_t *src_read = r_img->get_data().ptr();
- print_verbose(vformat("etcpak: Encoding image size %dx%d to format %s%s.", width, height, Image::get_format_name(target_format), mipmaps ? ", with mipmaps" : ""));
- int dest_size = Image::get_image_data_size(width, height, target_format, mipmaps);
- Vector<uint8_t> dest_data;
- dest_data.resize(dest_size);
- uint8_t *dest_write = dest_data.ptrw();
- int mip_count = mipmaps ? Image::get_image_required_mipmaps(width, height, target_format) : 0;
- Vector<uint32_t> padded_src;
- for (int i = 0; i < mip_count + 1; i++) {
-
- int orig_mip_w, orig_mip_h;
- int mip_ofs = Image::get_image_mipmap_offset_and_dimensions(width, height, target_format, i, orig_mip_w, orig_mip_h);
-
- ERR_FAIL_COND(mip_ofs % 8 != 0);
- uint64_t *dest_mip_write = (uint64_t *)&dest_write[mip_ofs];
-
- int mip_w = (orig_mip_w + 3) & ~3;
- int mip_h = (orig_mip_h + 3) & ~3;
- const uint32_t blocks = mip_w * mip_h / 16;
-
- int src_mip_ofs = r_img->get_mipmap_offset(i);
- const uint32_t *src_mip_read = (const uint32_t *)&src_read[src_mip_ofs];
-
- if (mip_w != orig_mip_w || mip_h != orig_mip_h) {
- padded_src.resize(mip_w * mip_h);
- uint32_t *ptrw = padded_src.ptrw();
- int x = 0, y = 0;
- for (y = 0; y < orig_mip_h; y++) {
- for (x = 0; x < orig_mip_w; x++) {
- ptrw[mip_w * y + x] = src_mip_read[orig_mip_w * y + x];
- }
-
- for (; x < mip_w; x++) {
- ptrw[mip_w * y + x] = ptrw[mip_w * y + x - 1];
- }
- }
-
- for (; y < mip_h; y++) {
- for (x = 0; x < mip_w; x++) {
- ptrw[mip_w * y + x] = ptrw[mip_w * y + x - mip_w];
- }
- }
-
- src_mip_read = padded_src.ptr();
- }
- if (p_compresstype == EtcpakType::ETCPAK_TYPE_ETC1) {
- CompressEtc1RgbDither(src_mip_read, dest_mip_write, blocks, mip_w);
- } else if (p_compresstype == EtcpakType::ETCPAK_TYPE_ETC2) {
- CompressEtc2Rgb(src_mip_read, dest_mip_write, blocks, mip_w, true);
- } else if (p_compresstype == EtcpakType::ETCPAK_TYPE_ETC2_ALPHA || p_compresstype == EtcpakType::ETCPAK_TYPE_ETC2_RA_AS_RG) {
- CompressEtc2Rgba(src_mip_read, dest_mip_write, blocks, mip_w, true);
- } else if (p_compresstype == EtcpakType::ETCPAK_TYPE_DXT1) {
- CompressDxt1Dither(src_mip_read, dest_mip_write, blocks, mip_w);
- } else if (p_compresstype == EtcpakType::ETCPAK_TYPE_DXT5 || p_compresstype == EtcpakType::ETCPAK_TYPE_DXT5_RA_AS_RG) {
- CompressDxt5(src_mip_read, dest_mip_write, blocks, mip_w);
- } else {
- ERR_FAIL_MSG("etcpak: Invalid or unsupported compression format.");
- }
- }
-
- r_img->set_data(width, height, mipmaps, target_format, dest_data);
- print_verbose(vformat("etcpak: Encoding took %s ms.", rtos(OS::get_singleton()->get_ticks_msec() - start_time)));
- }
|