diff options
Diffstat (limited to 'modules')
-rw-r--r-- | modules/basis_universal/SCsub | 38 | ||||
-rw-r--r-- | modules/basis_universal/config.py | 5 | ||||
-rw-r--r-- | modules/basis_universal/register_types.cpp | 262 | ||||
-rw-r--r-- | modules/basis_universal/register_types.h | 32 | ||||
-rw-r--r-- | modules/basis_universal/texture_bu.cpp | 203 | ||||
-rw-r--r-- | modules/basis_universal/texture_bu.h | 47 | ||||
-rw-r--r-- | modules/cvtt/image_compress_cvtt.cpp | 4 | ||||
-rw-r--r-- | modules/cvtt/image_compress_cvtt.h | 2 | ||||
-rw-r--r-- | modules/etc/image_etc.cpp | 59 | ||||
-rw-r--r-- | modules/squish/image_compress_squish.cpp | 62 | ||||
-rw-r--r-- | modules/squish/image_compress_squish.h | 2 |
11 files changed, 614 insertions, 102 deletions
diff --git a/modules/basis_universal/SCsub b/modules/basis_universal/SCsub new file mode 100644 index 0000000000..5fe994cda9 --- /dev/null +++ b/modules/basis_universal/SCsub @@ -0,0 +1,38 @@ +#!/usr/bin/env python +import string + +Import('env') +Import('env_modules') + +env_bu = env_modules.Clone() +env_bu.Append(CPPPATH=["#thirdparty/basis_universal", "#thirdparty/basis_universal/transcoder"]) + +if env['target'] == "debug": + env_bu.Append(CPPFLAGS=["-DBASISU_DEVEL_MESSAGES=1", "-DBASISD_ENABLE_DEBUG_FLAGS=1"]) + +tool_sources = string.split(""" + #thirdparty/basis_universal/basisu_astc_decomp.cpp + #thirdparty/basis_universal/basisu_backend.cpp + #thirdparty/basis_universal/basisu_basis_file.cpp + #thirdparty/basis_universal/basisu_comp.cpp + #thirdparty/basis_universal/basisu_enc.cpp + #thirdparty/basis_universal/basisu_etc.cpp + #thirdparty/basis_universal/basisu_frontend.cpp + #thirdparty/basis_universal/basisu_global_selector_palette_helpers.cpp + #thirdparty/basis_universal/basisu_gpu_texture.cpp + #thirdparty/basis_universal/basisu_pvrtc1_4.cpp + #thirdparty/basis_universal/basisu_resample_filters.cpp + #thirdparty/basis_universal/basisu_resampler.cpp + #thirdparty/basis_universal/basisu_ssim.cpp + #thirdparty/basis_universal/basisu_tool.cpp + #thirdparty/basis_universal/lodepng.cpp + """) + +transcoder_sources = ["#thirdparty/basis_universal/transcoder/basisu_transcoder.cpp"] + +if env['tools']: + env_bu.add_source_files(env.modules_sources, tool_sources) + +env_bu.add_source_files(env.modules_sources, transcoder_sources) + +env_bu.add_source_files(env.modules_sources, "*.cpp") diff --git a/modules/basis_universal/config.py b/modules/basis_universal/config.py new file mode 100644 index 0000000000..1c8cd12a2d --- /dev/null +++ b/modules/basis_universal/config.py @@ -0,0 +1,5 @@ +def can_build(env, platform): + return True + +def configure(env): + pass diff --git a/modules/basis_universal/register_types.cpp b/modules/basis_universal/register_types.cpp new file mode 100644 index 0000000000..226567982b --- /dev/null +++ b/modules/basis_universal/register_types.cpp @@ -0,0 +1,262 @@ +#include "register_types.h" + +#include "core/os/os.h" +#include "texture_bu.h" + +#ifdef TOOLS_ENABLED +#include "thirdparty/basis_universal/basisu_comp.h" +#endif + +#include "thirdparty/basis_universal/transcoder/basisu.h" + +#include "servers/visual_server.h" + +enum BasisDecompressFormat { + BASIS_DECOMPRESS_RG, + BASIS_DECOMPRESS_RGB, + BASIS_DECOMPRESS_RGBA, + BASIS_DECOMPRESS_RG_AS_RA +}; + +//workaround for lack of ETC2 RG +#define USE_RG_AS_RGBA + +basist::etc1_global_selector_codebook *sel_codebook = nullptr; + +static PoolVector<uint8_t> basis_universal_packer(const Ref<Image> &p_image, Image::UsedChannels p_channels) { + + PoolVector<uint8_t> budata; + +#ifdef TOOLS_ENABLED + + { + Ref<Image> image = p_image->duplicate(); + + // unfortunately, basis universal does not support compressing supplied mipmaps, + // so for the time being, only compressing individual images will have to do. + + if (image->has_mipmaps()) { + image->clear_mipmaps(); + } + if (image->get_format() != Image::FORMAT_RGBA8) { + image->convert(Image::FORMAT_RGBA8); + } + + basisu::image buimg(image->get_width(), image->get_height()); + + { + PoolVector<uint8_t> vec = image->get_data(); + PoolVector<uint8_t>::Read r = vec.read(); + + memcpy(buimg.get_ptr(), r.ptr(), vec.size()); + } + + //image->save_png("pepeche.png"); + + basisu::basis_compressor_params params; + params.m_max_endpoint_clusters = 512; + params.m_max_selector_clusters = 512; + params.m_multithreading = true; + params.m_no_hybrid_sel_cb = true; //fixme, default on this causes crashes + params.m_pSel_codebook = sel_codebook; + params.m_quality_level = 0; + //params.m_disable_hierarchical_endpoint_codebooks = true; + //params.m_no_selector_rdo = true; + + basisu::job_pool jpool(OS::get_singleton()->get_processor_count()); + params.m_pJob_pool = &jpool; + + params.m_mip_gen = false; //sorry, please some day support provided mipmaps. + params.m_source_images.push_back(buimg); + + BasisDecompressFormat decompress_format; + params.m_check_for_alpha = false; + + switch (p_channels) { + case Image::USED_CHANNELS_L: { + decompress_format = BASIS_DECOMPRESS_RGB; + } break; + case Image::USED_CHANNELS_LA: { + params.m_force_alpha = true; + decompress_format = BASIS_DECOMPRESS_RGBA; + } break; + case Image::USED_CHANNELS_R: { + decompress_format = BASIS_DECOMPRESS_RGB; + } break; + case Image::USED_CHANNELS_RG: { +#ifdef USE_RG_AS_RGBA + image->convert_rg_to_ra_rgba8(); + decompress_format = BASIS_DECOMPRESS_RG_AS_RA; + +#else + + params.m_seperate_rg_to_color_alpha = true; + decompress_format = BASIS_DECOMPRESS_RG; + +#endif + + } break; + case Image::USED_CHANNELS_RGB: { + decompress_format = BASIS_DECOMPRESS_RGB; + } break; + case Image::USED_CHANNELS_RGBA: { + params.m_force_alpha = true; + decompress_format = BASIS_DECOMPRESS_RGBA; + } break; + } + + basisu::basis_compressor c; + c.init(params); + + int buerr = c.process(); + ERR_FAIL_COND_V(buerr != basisu::basis_compressor::cECSuccess, budata); + + const basisu::uint8_vec &buvec = c.get_output_basis_file(); + budata.resize(buvec.size() + 4); + + { + PoolVector<uint8_t>::Write w = budata.write(); + uint32_t *decf = (uint32_t *)w.ptr(); + *decf = decompress_format; + memcpy(w.ptr() + 4, &buvec[0], buvec.size()); + } + } + +#endif + return budata; +} + +static Ref<Image> basis_universal_unpacker(const PoolVector<uint8_t> &p_buffer) { + Ref<Image> image; + + PoolVector<uint8_t>::Read r = p_buffer.read(); + const uint8_t *ptr = r.ptr(); + int size = p_buffer.size(); + + basist::transcoder_texture_format format; + Image::Format imgfmt; + + switch (*(uint32_t *)(ptr)) { + case BASIS_DECOMPRESS_RG: { + + if (VS::get_singleton()->has_os_feature("rgtc")) { + format = basist::cTFBC5; // get this from renderer + imgfmt = Image::FORMAT_RGTC_RG; + } else if (VS::get_singleton()->has_os_feature("etc2")) { + //unfortunately, basis universal does not support + // + ERR_FAIL_V(image); //unimplemented here + //format = basist::cTFETC1; // get this from renderer + //imgfmt = Image::FORMAT_RGTC_RG; + } else { + //decompress + } + } break; + case BASIS_DECOMPRESS_RGB: { + if (VS::get_singleton()->has_os_feature("bptc")) { + format = basist::cTFBC7_M6_OPAQUE_ONLY; // get this from renderer + imgfmt = Image::FORMAT_BPTC_RGBA; + } else if (VS::get_singleton()->has_os_feature("s3tc")) { + format = basist::cTFBC1; // get this from renderer + imgfmt = Image::FORMAT_DXT1; + } else if (VS::get_singleton()->has_os_feature("etc")) { + + format = basist::cTFETC1; // get this from renderer + imgfmt = Image::FORMAT_ETC; + } else { + format = basist::cTFBGR565; // get this from renderer + imgfmt = Image::FORMAT_RGB565; + } + + } break; + case BASIS_DECOMPRESS_RGBA: { + if (VS::get_singleton()->has_os_feature("bptc")) { + format = basist::cTFBC7_M5; // get this from renderer + imgfmt = Image::FORMAT_BPTC_RGBA; + } else if (VS::get_singleton()->has_os_feature("s3tc")) { + format = basist::cTFBC3; // get this from renderer + imgfmt = Image::FORMAT_DXT5; + } else if (VS::get_singleton()->has_os_feature("etc2")) { + format = basist::cTFETC2; // get this from renderer + imgfmt = Image::FORMAT_ETC2_RGBA8; + } else { + //gles2 most likely + format = basist::cTFRGBA4444; // get this from renderer + imgfmt = Image::FORMAT_RGBA4444; + } + } break; + case BASIS_DECOMPRESS_RG_AS_RA: { + if (VS::get_singleton()->has_os_feature("s3tc")) { + format = basist::cTFBC3; // get this from renderer + imgfmt = Image::FORMAT_DXT5_RA_AS_RG; + } else if (VS::get_singleton()->has_os_feature("etc2")) { + format = basist::cTFETC2; // get this from renderer + imgfmt = Image::FORMAT_ETC2_RGBA8; + } else { + //gles2 most likely, bad for normalmaps, nothing to do about this. + format = basist::cTFRGBA32; + imgfmt = Image::FORMAT_RGBA8; + } + } break; + } + + ptr += 4; + size -= 4; + + basist::basisu_transcoder tr(NULL); + + ERR_FAIL_COND_V(!tr.validate_header(ptr, size), image); + + basist::basisu_image_info info; + tr.get_image_info(ptr, size, info, 0); + + int block_size = basist::basis_get_bytes_per_block(format); + PoolVector<uint8_t> gpudata; + gpudata.resize(info.m_total_blocks * block_size); + + { + PoolVector<uint8_t>::Write w = gpudata.write(); + uint8_t *dst = w.ptr(); + for (int i = 0; i < gpudata.size(); i++) + dst[i] = 0x00; + + int ofs = 0; + tr.start_transcoding(ptr, size); + for (uint32_t i = 0; i < info.m_total_levels; i++) { + + basist::basisu_image_level_info level; + tr.get_image_level_info(ptr, size, level, 0, i); + + bool ret = tr.transcode_image_level(ptr, size, 0, i, dst + ofs, level.m_total_blocks - i, format); + if (!ret) { + printf("failed! on level %i\n", i); + break; + }; + + ofs += level.m_total_blocks * block_size; + }; + }; + + image.instance(); + image->create(info.m_width, info.m_height, info.m_total_levels > 1, imgfmt, gpudata); + + return image; +} + +void register_basis_universal_types() { +#ifdef TOOLS_ENABLED + sel_codebook = new basist::etc1_global_selector_codebook(basist::g_global_selector_cb_size, basist::g_global_selector_cb); + Image::basis_universal_packer = basis_universal_packer; +#endif + Image::basis_universal_unpacker = basis_universal_unpacker; + // ClassDB::register_class<TextureBU>(); +} + +void unregister_basis_universal_types() { + +#ifdef TOOLS_ENABLED + delete sel_codebook; +#endif + Image::basis_universal_packer = NULL; + Image::basis_universal_unpacker = NULL; +} diff --git a/modules/basis_universal/register_types.h b/modules/basis_universal/register_types.h new file mode 100644 index 0000000000..3e9acc0896 --- /dev/null +++ b/modules/basis_universal/register_types.h @@ -0,0 +1,32 @@ +/*************************************************************************/ +/* register_types.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +void register_basis_universal_types(); +void unregister_basis_universal_types(); diff --git a/modules/basis_universal/texture_bu.cpp b/modules/basis_universal/texture_bu.cpp new file mode 100644 index 0000000000..73a945b33d --- /dev/null +++ b/modules/basis_universal/texture_bu.cpp @@ -0,0 +1,203 @@ +#include "texture_bu.h" +#if 0 +#include "core/os/os.h" + +#ifdef TOOLS_ENABLED +#include "basisu_comp.h" +#endif + +#include "transcoder/basisu.h" + +void TextureBU::_bind_methods() { + + ClassDB::bind_method(D_METHOD("set_bu_data", "data"), &TextureBU::set_bu_data); + ClassDB::bind_method(D_METHOD("get_bu_data"), &TextureBU::get_data); + ClassDB::bind_method(D_METHOD("import"), &TextureBU::import); + + ADD_PROPERTY(PropertyInfo(Variant::POOL_BYTE_ARRAY, "bu_data"), "set_bu_data", "get_bu_data"); + +}; + +int TextureBU::get_width() const { + + return tex_size.x; +}; + +int TextureBU::get_height() const { + + return tex_size.y; +}; + +RID TextureBU::get_rid() const { + + return texture; +}; + + +bool TextureBU::has_alpha() const { + + return false; +}; + +void TextureBU::set_flags(uint32_t p_flags) { + + flags = p_flags; + VisualServer::get_singleton()->texture_set_flags(texture, p_flags); +}; + +uint32_t TextureBU::get_flags() const { + + return flags; +}; + + +void TextureBU::set_bu_data(const PoolVector<uint8_t>& p_data) { + +#ifdef TOOLS_ENABLED + data = p_data; +#endif + + PoolVector<uint8_t>::Read r = p_data.read(); + const void* ptr = r.ptr(); + int size = p_data.size(); + + basist::transcoder_texture_format format; + Image::Format imgfmt; + + if (OS::get_singleton()->has_feature("s3tc")) { + + format = basist::cTFBC3; // get this from renderer + imgfmt = Image::FORMAT_DXT5; + + } else if (OS::get_singleton()->has_feature("etc2")) { + + format = basist::cTFETC2; + imgfmt = Image::FORMAT_ETC2_RGBA8; + }; + + basist::basisu_transcoder tr(NULL); + + ERR_FAIL_COND(!tr.validate_header(ptr, size)); + + basist::basisu_image_info info; + tr.get_image_info(ptr, size, info, 0); + tex_size = Size2(info.m_width, info.m_height); + + int block_size = basist::basis_get_bytes_per_block(format); + PoolVector<uint8_t> gpudata; + gpudata.resize(info.m_total_blocks * block_size); + + { + PoolVector<uint8_t>::Write w = gpudata.write(); + uint8_t* dst = w.ptr(); + for (int i=0; i<gpudata.size(); i++) + dst[i] = 0x00; + + int ofs = 0; + tr.start_transcoding(ptr, size); + for (int i=0; i<info.m_total_levels; i++) { + + basist::basisu_image_level_info level; + tr.get_image_level_info(ptr, size, level, 0, i); + + bool ret = tr.transcode_image_level(ptr, size, 0, i, dst + ofs, level.m_total_blocks - i, format); + if (!ret) { + printf("failed! on level %i\n", i); + break; + }; + + ofs += level.m_total_blocks * block_size; + }; + }; + + Ref<Image> img; + img.instance(); + img->create(info.m_width, info.m_height, info.m_total_levels > 1, imgfmt, gpudata); + + VisualServer::get_singleton()->texture_allocate(texture, tex_size.x, tex_size.y, 0, img->get_format(), VS::TEXTURE_TYPE_2D, flags); + VisualServer::get_singleton()->texture_set_data(texture, img); +}; + +Error TextureBU::import(const Ref<Image>& p_img) { + +#ifdef TOOLS_ENABLED + + PoolVector<uint8_t> budata; + + { + Image::Format format = p_img->get_format(); + if (format != Image::FORMAT_RGB8 && format != Image::FORMAT_RGBA8) { + ERR_FAIL_V(ERR_INVALID_PARAMETER); + return ERR_INVALID_PARAMETER; + }; + + Ref<Image> copy = p_img->duplicate(); + if (format == Image::FORMAT_RGB8) + copy->convert(Image::FORMAT_RGBA8); + + basisu::image buimg(p_img->get_width(), p_img->get_height()); + int size = p_img->get_width() * p_img->get_height() * 4; + + PoolVector<uint8_t> vec = copy->get_data(); + { + PoolVector<uint8_t>::Read r = vec.read(); + memcpy(buimg.get_ptr(), r.ptr(), size); + }; + + basisu::basis_compressor_params params; + params.m_max_endpoint_clusters = 512; + params.m_max_selector_clusters = 512; + params.m_multithreading = true; + + basisu::job_pool jpool(1); + params.m_pJob_pool = &jpool; + + params.m_mip_gen = p_img->get_mipmap_count() > 0; + params.m_source_images.push_back(buimg); + + basisu::basis_compressor c; + c.init(params); + + int buerr = c.process(); + if (buerr != basisu::basis_compressor::cECSuccess) { + ERR_FAIL_V(ERR_INVALID_PARAMETER); + return ERR_INVALID_PARAMETER; + }; + + const basisu::uint8_vec& buvec = c.get_output_basis_file(); + budata.resize(buvec.size()); + + { + PoolVector<uint8_t>::Write w = budata.write(); + memcpy(w.ptr(), &buvec[0], budata.size()); + }; + }; + + set_bu_data(budata); + + return OK; +#else + + return ERR_UNAVAILABLE; +#endif +}; + + +PoolVector<uint8_t> TextureBU::get_bu_data() const { + + return data; +}; + +TextureBU::TextureBU() { + + flags = FLAGS_DEFAULT; + texture = VisualServer::get_singleton()->texture_create(); +}; + + +TextureBU::~TextureBU() { + + VisualServer::get_singleton()->free(texture); +}; + +#endif diff --git a/modules/basis_universal/texture_bu.h b/modules/basis_universal/texture_bu.h new file mode 100644 index 0000000000..9ec9194a96 --- /dev/null +++ b/modules/basis_universal/texture_bu.h @@ -0,0 +1,47 @@ +#include "scene/resources/texture.h" + +#ifdef TOOLS_ENABLED +#include "thirdparty/basis_universal/basisu_comp.h" +#endif + +#include "thirdparty/basis_universal/transcoder/basisu.h" + +#if 0 +class TextureBU : public Texture { + + GDCLASS(TextureBU, Texture); + RES_BASE_EXTENSION("butex"); + + RID texture; + Size2 tex_size; + + uint32_t flags; + + PoolVector<uint8_t> data; + + static void _bind_methods(); + +public: + + virtual int get_width() const; + virtual int get_height() const; + virtual RID get_rid() const; + virtual bool has_alpha() const; + + virtual void set_flags(uint32_t p_flags); + virtual uint32_t get_flags() const; + + + Error import(const Ref<Image> &p_img); + + void set_bu_data(const PoolVector<uint8_t>& p_data); + + PoolVector<uint8_t> get_bu_data() const; + String get_img_path() const; + + TextureBU(); + ~TextureBU(); + +}; + +#endif diff --git a/modules/cvtt/image_compress_cvtt.cpp b/modules/cvtt/image_compress_cvtt.cpp index c261350e89..4d762b7a7b 100644 --- a/modules/cvtt/image_compress_cvtt.cpp +++ b/modules/cvtt/image_compress_cvtt.cpp @@ -136,7 +136,7 @@ static void _digest_job_queue(void *p_job_queue) { } } -void image_compress_cvtt(Image *p_image, float p_lossy_quality, Image::CompressSource p_source) { +void image_compress_cvtt(Image *p_image, float p_lossy_quality, Image::UsedChannels p_channels) { if (p_image->get_format() >= Image::FORMAT_BPTC_RGBA) return; //do not compress, already compressed @@ -167,7 +167,7 @@ void image_compress_cvtt(Image *p_image, float p_lossy_quality, Image::CompressS flags |= cvtt::Flags::BC7_RespectPunchThrough; - if (p_source == Image::COMPRESS_SOURCE_NORMAL) { + if (p_channels == Image::USED_CHANNELS_RG) { //guessing this is a normalmap flags |= cvtt::Flags::Uniform; } diff --git a/modules/cvtt/image_compress_cvtt.h b/modules/cvtt/image_compress_cvtt.h index ecb876ecc6..c1772199af 100644 --- a/modules/cvtt/image_compress_cvtt.h +++ b/modules/cvtt/image_compress_cvtt.h @@ -33,7 +33,7 @@ #include "core/image.h" -void image_compress_cvtt(Image *p_image, float p_lossy_quality, Image::CompressSource p_source); +void image_compress_cvtt(Image *p_image, float p_lossy_quality, Image::UsedChannels p_channels); void image_decompress_cvtt(Image *p_image); #endif // IMAGE_COMPRESS_CVTT_H diff --git a/modules/etc/image_etc.cpp b/modules/etc/image_etc.cpp index b80138c99d..006902462b 100644 --- a/modules/etc/image_etc.cpp +++ b/modules/etc/image_etc.cpp @@ -36,18 +36,18 @@ #include "core/os/os.h" #include "core/print_string.h" -static Image::Format _get_etc2_mode(Image::DetectChannels format) { +static Image::Format _get_etc2_mode(Image::UsedChannels format) { switch (format) { - case Image::DETECTED_R: + case Image::USED_CHANNELS_R: return Image::FORMAT_ETC2_R11; - case Image::DETECTED_RG: + case Image::USED_CHANNELS_RG: return Image::FORMAT_ETC2_RG11; - case Image::DETECTED_RGB: + case Image::USED_CHANNELS_RGB: return Image::FORMAT_ETC2_RGB8; - case Image::DETECTED_RGBA: + case Image::USED_CHANNELS_RGBA: return Image::FORMAT_ETC2_RGBA8; // TODO: would be nice if we could use FORMAT_ETC2_RGB8A1 for FORMAT_RGBA5551 @@ -88,47 +88,8 @@ static Etc::Image::Format _image_format_to_etc2comp_format(Image::Format format) } } -static void _compress_etc(Image *p_img, float p_lossy_quality, bool force_etc1_format, Image::CompressSource p_source) { +static void _compress_etc(Image *p_img, float p_lossy_quality, bool force_etc1_format, Image::UsedChannels p_channels) { Image::Format img_format = p_img->get_format(); - Image::DetectChannels detected_channels = p_img->get_detected_channels(); - - if (p_source == Image::COMPRESS_SOURCE_LAYERED) { - //keep what comes in - switch (p_img->get_format()) { - case Image::FORMAT_L8: { - detected_channels = Image::DETECTED_L; - } break; - case Image::FORMAT_LA8: { - detected_channels = Image::DETECTED_LA; - } break; - case Image::FORMAT_R8: { - detected_channels = Image::DETECTED_R; - } break; - case Image::FORMAT_RG8: { - detected_channels = Image::DETECTED_RG; - } break; - case Image::FORMAT_RGB8: { - detected_channels = Image::DETECTED_RGB; - } break; - case Image::FORMAT_RGBA8: - case Image::FORMAT_RGBA4444: - case Image::FORMAT_RGBA5551: { - detected_channels = Image::DETECTED_RGBA; - } break; - default: { - } - } - } - - if (p_source == Image::COMPRESS_SOURCE_SRGB && (detected_channels == Image::DETECTED_R || detected_channels == Image::DETECTED_RG)) { - //R and RG do not support SRGB - detected_channels = Image::DETECTED_RGB; - } - - if (p_source == Image::COMPRESS_SOURCE_NORMAL) { - //use RG channels only for normal - detected_channels = Image::DETECTED_RG; - } if (img_format >= Image::FORMAT_DXT1) { return; //do not compress, already compressed @@ -153,7 +114,7 @@ static void _compress_etc(Image *p_img, float p_lossy_quality, bool force_etc1_f uint32_t imgw = p_img->get_width(), imgh = p_img->get_height(); - Image::Format etc_format = force_etc1_format ? Image::FORMAT_ETC : _get_etc2_mode(detected_channels); + Image::Format etc_format = force_etc1_format ? Image::FORMAT_ETC : _get_etc2_mode(p_channels); Ref<Image> img = p_img->duplicate(); @@ -241,11 +202,11 @@ static void _compress_etc(Image *p_img, float p_lossy_quality, bool force_etc1_f } static void _compress_etc1(Image *p_img, float p_lossy_quality) { - _compress_etc(p_img, p_lossy_quality, true, Image::COMPRESS_SOURCE_GENERIC); + _compress_etc(p_img, p_lossy_quality, true, Image::USED_CHANNELS_RGB); } -static void _compress_etc2(Image *p_img, float p_lossy_quality, Image::CompressSource p_source) { - _compress_etc(p_img, p_lossy_quality, false, p_source); +static void _compress_etc2(Image *p_img, float p_lossy_quality, Image::UsedChannels p_channels) { + _compress_etc(p_img, p_lossy_quality, false, p_channels); } void _register_etc_compress_func() { diff --git a/modules/squish/image_compress_squish.cpp b/modules/squish/image_compress_squish.cpp index 58b8115dfc..2f680fd3d0 100644 --- a/modules/squish/image_compress_squish.cpp +++ b/modules/squish/image_compress_squish.cpp @@ -50,7 +50,7 @@ void image_decompress_squish(Image *p_image) { 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) { + } 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; @@ -71,9 +71,13 @@ void image_decompress_squish(Image *p_image) { } p_image->create(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) { + p_image->convert_ra_rgba8_to_rg(); + } } -void image_compress_squish(Image *p_image, float p_lossy_quality, Image::CompressSource p_source) { +void image_compress_squish(Image *p_image, float p_lossy_quality, Image::UsedChannels p_channels) { if (p_image->get_format() >= Image::FORMAT_DXT1) return; //do not compress, already compressed @@ -92,75 +96,35 @@ void image_compress_squish(Image *p_image, float p_lossy_quality, Image::Compres Image::Format target_format = Image::FORMAT_RGBA8; - Image::DetectChannels dc = p_image->get_detected_channels(); - - if (p_source == Image::COMPRESS_SOURCE_LAYERED) { - //keep what comes in - switch (p_image->get_format()) { - case Image::FORMAT_L8: { - dc = Image::DETECTED_L; - } break; - case Image::FORMAT_LA8: { - dc = Image::DETECTED_LA; - } break; - case Image::FORMAT_R8: { - dc = Image::DETECTED_R; - } break; - case Image::FORMAT_RG8: { - dc = Image::DETECTED_RG; - } break; - case Image::FORMAT_RGB8: { - dc = Image::DETECTED_RGB; - } break; - case Image::FORMAT_RGBA8: - case Image::FORMAT_RGBA4444: - case Image::FORMAT_RGBA5551: { - dc = Image::DETECTED_RGBA; - } break; - default: { - } - } - } - p_image->convert(Image::FORMAT_RGBA8); //still uses RGBA to convert - if (p_source == Image::COMPRESS_SOURCE_SRGB && (dc == Image::DETECTED_R || dc == Image::DETECTED_RG)) { - //R and RG do not support SRGB - dc = Image::DETECTED_RGB; - } - - if (p_source == Image::COMPRESS_SOURCE_NORMAL) { - //R and RG do not support SRGB - dc = Image::DETECTED_RG; - } - - switch (dc) { - case Image::DETECTED_L: { + switch (p_channels) { + case Image::USED_CHANNELS_L: { target_format = Image::FORMAT_DXT1; squish_comp |= squish::kDxt1; } break; - case Image::DETECTED_LA: { + case Image::USED_CHANNELS_LA: { target_format = Image::FORMAT_DXT5; squish_comp |= squish::kDxt5; } break; - case Image::DETECTED_R: { + case Image::USED_CHANNELS_R: { target_format = Image::FORMAT_RGTC_R; squish_comp |= squish::kBc4; } break; - case Image::DETECTED_RG: { + case Image::USED_CHANNELS_RG: { target_format = Image::FORMAT_RGTC_RG; squish_comp |= squish::kBc5; } break; - case Image::DETECTED_RGB: { + case Image::USED_CHANNELS_RGB: { target_format = Image::FORMAT_DXT1; squish_comp |= squish::kDxt1; } break; - case Image::DETECTED_RGBA: { + case Image::USED_CHANNELS_RGBA: { //TODO, should convert both, then measure which one does a better job target_format = Image::FORMAT_DXT5; diff --git a/modules/squish/image_compress_squish.h b/modules/squish/image_compress_squish.h index b5a209ceb9..19e6d57474 100644 --- a/modules/squish/image_compress_squish.h +++ b/modules/squish/image_compress_squish.h @@ -33,7 +33,7 @@ #include "core/image.h" -void image_compress_squish(Image *p_image, float p_lossy_quality, Image::CompressSource p_source); +void image_compress_squish(Image *p_image, float p_lossy_quality, Image::UsedChannels p_channels); void image_decompress_squish(Image *p_image); #endif // IMAGE_COMPRESS_SQUISH_H |