2017-04-26 17:49:08 +02:00
|
|
|
/*************************************************************************/
|
|
|
|
/* image_etc.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2017-04-26 17:49:08 +02:00
|
|
|
/*************************************************************************/
|
2020-01-01 11:16:22 +01:00
|
|
|
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
|
2017-04-26 17:49:08 +02:00
|
|
|
/* */
|
|
|
|
/* 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. */
|
|
|
|
/*************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2017-04-26 17:49:08 +02:00
|
|
|
#include "image_etc.h"
|
|
|
|
#include "Etc.h"
|
|
|
|
#include "EtcFilter.h"
|
2018-09-11 18:13:45 +02:00
|
|
|
#include "core/image.h"
|
|
|
|
#include "core/os/copymem.h"
|
|
|
|
#include "core/os/os.h"
|
|
|
|
#include "core/print_string.h"
|
2017-04-26 17:49:08 +02:00
|
|
|
|
2019-09-27 04:16:44 +02:00
|
|
|
static Image::Format _get_etc2_mode(Image::UsedChannels format) {
|
2017-04-26 17:49:08 +02:00
|
|
|
switch (format) {
|
2019-09-27 04:16:44 +02:00
|
|
|
case Image::USED_CHANNELS_R:
|
2017-04-26 17:49:08 +02:00
|
|
|
return Image::FORMAT_ETC2_R11;
|
|
|
|
|
2019-09-27 04:16:44 +02:00
|
|
|
case Image::USED_CHANNELS_RG:
|
2017-04-26 17:49:08 +02:00
|
|
|
return Image::FORMAT_ETC2_RG11;
|
|
|
|
|
2019-09-27 04:16:44 +02:00
|
|
|
case Image::USED_CHANNELS_RGB:
|
2017-04-26 17:49:08 +02:00
|
|
|
return Image::FORMAT_ETC2_RGB8;
|
|
|
|
|
2019-09-27 04:16:44 +02:00
|
|
|
case Image::USED_CHANNELS_RGBA:
|
2017-04-26 17:49:08 +02:00
|
|
|
return Image::FORMAT_ETC2_RGBA8;
|
|
|
|
|
2018-09-26 13:13:56 +02:00
|
|
|
// TODO: would be nice if we could use FORMAT_ETC2_RGB8A1 for FORMAT_RGBA5551
|
|
|
|
default:
|
|
|
|
// TODO: Kept for compatibility, but should be investigated whether it's correct or if it should error out
|
|
|
|
return Image::FORMAT_ETC2_RGBA8;
|
2017-04-26 17:49:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static Etc::Image::Format _image_format_to_etc2comp_format(Image::Format format) {
|
|
|
|
switch (format) {
|
|
|
|
case Image::FORMAT_ETC:
|
|
|
|
return Etc::Image::Format::ETC1;
|
|
|
|
|
|
|
|
case Image::FORMAT_ETC2_R11:
|
|
|
|
return Etc::Image::Format::R11;
|
|
|
|
|
|
|
|
case Image::FORMAT_ETC2_R11S:
|
|
|
|
return Etc::Image::Format::SIGNED_R11;
|
|
|
|
|
|
|
|
case Image::FORMAT_ETC2_RG11:
|
|
|
|
return Etc::Image::Format::RG11;
|
|
|
|
|
|
|
|
case Image::FORMAT_ETC2_RG11S:
|
|
|
|
return Etc::Image::Format::SIGNED_RG11;
|
|
|
|
|
|
|
|
case Image::FORMAT_ETC2_RGB8:
|
|
|
|
return Etc::Image::Format::RGB8;
|
|
|
|
|
|
|
|
case Image::FORMAT_ETC2_RGBA8:
|
|
|
|
return Etc::Image::Format::RGBA8;
|
|
|
|
|
|
|
|
case Image::FORMAT_ETC2_RGB8A1:
|
|
|
|
return Etc::Image::Format::RGB8A1;
|
|
|
|
|
2018-09-26 13:13:56 +02:00
|
|
|
default:
|
|
|
|
ERR_FAIL_V(Etc::Image::Format::UNKNOWN);
|
|
|
|
}
|
2017-04-26 17:49:08 +02:00
|
|
|
}
|
|
|
|
|
2019-09-27 04:16:44 +02:00
|
|
|
static void _compress_etc(Image *p_img, float p_lossy_quality, bool force_etc1_format, Image::UsedChannels p_channels) {
|
2017-04-26 17:49:08 +02:00
|
|
|
Image::Format img_format = p_img->get_format();
|
2017-06-17 02:47:28 +02:00
|
|
|
|
2017-04-26 17:49:08 +02:00
|
|
|
if (img_format >= Image::FORMAT_DXT1) {
|
|
|
|
return; //do not compress, already compressed
|
|
|
|
}
|
|
|
|
|
|
|
|
if (img_format > Image::FORMAT_RGBA8) {
|
|
|
|
// TODO: we should be able to handle FORMAT_RGBA4444 and FORMAT_RGBA5551 eventually
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-27 09:05:04 +02:00
|
|
|
// FIXME: Commented out during Vulkan rebase.
|
|
|
|
/*
|
2020-01-03 20:10:44 +01:00
|
|
|
if (force_etc1_format) {
|
|
|
|
// If VRAM compression is using ETC, but image has alpha, convert to RGBA4444 or LA8
|
2020-01-03 06:37:48 +01:00
|
|
|
// This saves space while maintaining the alpha channel
|
2019-09-27 09:05:04 +02:00
|
|
|
if (detected_channels == Image::USED_CHANNELS_RGBA) {
|
2020-01-03 20:10:44 +01:00
|
|
|
p_img->convert(Image::FORMAT_RGBA4444);
|
|
|
|
return;
|
2019-09-27 09:05:04 +02:00
|
|
|
} else if (detected_channels == Image::USE_CHANNELS_LA) {
|
2020-01-03 20:10:44 +01:00
|
|
|
p_img->convert(Image::FORMAT_LA8);
|
|
|
|
return;
|
|
|
|
}
|
2020-01-03 06:37:48 +01:00
|
|
|
}
|
2019-09-27 09:05:04 +02:00
|
|
|
*/
|
2020-01-03 06:37:48 +01:00
|
|
|
|
2017-08-31 23:30:35 +02:00
|
|
|
uint32_t imgw = p_img->get_width(), imgh = p_img->get_height();
|
2017-04-26 17:49:08 +02:00
|
|
|
|
2019-09-27 04:16:44 +02:00
|
|
|
Image::Format etc_format = force_etc1_format ? Image::FORMAT_ETC : _get_etc2_mode(p_channels);
|
2017-04-26 17:49:08 +02:00
|
|
|
|
|
|
|
Ref<Image> img = p_img->duplicate();
|
|
|
|
|
|
|
|
if (img->get_format() != Image::FORMAT_RGBA8)
|
|
|
|
img->convert(Image::FORMAT_RGBA8); //still uses RGBA to convert
|
|
|
|
|
2018-01-12 15:46:42 +01:00
|
|
|
if (img->has_mipmaps()) {
|
|
|
|
if (next_power_of_2(imgw) != imgw || next_power_of_2(imgh) != imgh) {
|
|
|
|
img->resize_to_po2();
|
|
|
|
imgw = img->get_width();
|
|
|
|
imgh = img->get_height();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (imgw % 4 != 0 || imgh % 4 != 0) {
|
|
|
|
if (imgw % 4) {
|
|
|
|
imgw += 4 - imgw % 4;
|
|
|
|
}
|
|
|
|
if (imgh % 4) {
|
|
|
|
imgh += 4 - imgh % 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
img->resize(imgw, imgh);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-26 17:49:08 +02:00
|
|
|
PoolVector<uint8_t>::Read r = img->get_data().read();
|
2019-10-28 08:07:29 +01:00
|
|
|
ERR_FAIL_COND(!r.ptr());
|
2017-04-26 17:49:08 +02:00
|
|
|
|
2018-09-26 17:38:02 +02:00
|
|
|
unsigned int target_size = Image::get_image_data_size(imgw, imgh, etc_format, p_img->has_mipmaps());
|
2017-11-08 22:18:12 +01:00
|
|
|
int mmc = 1 + (p_img->has_mipmaps() ? Image::get_image_required_mipmaps(imgw, imgh, etc_format) : 0);
|
2017-04-26 17:49:08 +02:00
|
|
|
|
|
|
|
PoolVector<uint8_t> dst_data;
|
|
|
|
dst_data.resize(target_size);
|
|
|
|
|
|
|
|
PoolVector<uint8_t>::Write w = dst_data.write();
|
|
|
|
|
|
|
|
// prepare parameters to be passed to etc2comp
|
2017-06-17 10:51:50 +02:00
|
|
|
int num_cpus = OS::get_singleton()->get_processor_count();
|
2017-04-26 17:49:08 +02:00
|
|
|
int encoding_time = 0;
|
2017-06-17 02:47:28 +02:00
|
|
|
float effort = 0.0; //default, reasonable time
|
|
|
|
|
|
|
|
if (p_lossy_quality > 0.75)
|
|
|
|
effort = 0.4;
|
|
|
|
else if (p_lossy_quality > 0.85)
|
|
|
|
effort = 0.6;
|
|
|
|
else if (p_lossy_quality > 0.95)
|
|
|
|
effort = 0.8;
|
|
|
|
|
|
|
|
Etc::ErrorMetric error_metric = Etc::ErrorMetric::RGBX; // NOTE: we can experiment with other error metrics
|
2017-04-26 17:49:08 +02:00
|
|
|
Etc::Image::Format etc2comp_etc_format = _image_format_to_etc2comp_format(etc_format);
|
|
|
|
|
|
|
|
int wofs = 0;
|
2017-07-06 00:36:56 +02:00
|
|
|
|
2018-08-24 09:35:07 +02:00
|
|
|
print_verbose("ETC: Begin encoding, format: " + Image::get_format_name(etc_format));
|
2017-07-06 00:36:56 +02:00
|
|
|
uint64_t t = OS::get_singleton()->get_ticks_msec();
|
2017-11-08 22:18:12 +01:00
|
|
|
for (int i = 0; i < mmc; i++) {
|
2017-04-26 17:49:08 +02:00
|
|
|
// convert source image to internal etc2comp format (which is equivalent to Image::FORMAT_RGBAF)
|
|
|
|
// NOTE: We can alternatively add a case to Image::convert to handle Image::FORMAT_RGBAF conversion.
|
|
|
|
int mipmap_ofs = 0, mipmap_size = 0, mipmap_w = 0, mipmap_h = 0;
|
|
|
|
img->get_mipmap_offset_size_and_dimensions(i, mipmap_ofs, mipmap_size, mipmap_w, mipmap_h);
|
|
|
|
const uint8_t *src = &r[mipmap_ofs];
|
|
|
|
|
|
|
|
Etc::ColorFloatRGBA *src_rgba_f = new Etc::ColorFloatRGBA[mipmap_w * mipmap_h];
|
2017-11-08 22:18:12 +01:00
|
|
|
for (int j = 0; j < mipmap_w * mipmap_h; j++) {
|
|
|
|
int si = j * 4; // RGBA8
|
|
|
|
src_rgba_f[j] = Etc::ColorFloatRGBA::ConvertFromRGBA8(src[si], src[si + 1], src[si + 2], src[si + 3]);
|
2017-04-26 17:49:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char *etc_data = NULL;
|
|
|
|
unsigned int etc_data_len = 0;
|
|
|
|
unsigned int extended_width = 0, extended_height = 0;
|
|
|
|
Etc::Encode((float *)src_rgba_f, mipmap_w, mipmap_h, etc2comp_etc_format, error_metric, effort, num_cpus, num_cpus, &etc_data, &etc_data_len, &extended_width, &extended_height, &encoding_time);
|
|
|
|
|
2017-11-08 22:18:12 +01:00
|
|
|
CRASH_COND(wofs + etc_data_len > target_size);
|
2017-04-26 17:49:08 +02:00
|
|
|
memcpy(&w[wofs], etc_data, etc_data_len);
|
|
|
|
wofs += etc_data_len;
|
|
|
|
|
|
|
|
delete[] etc_data;
|
|
|
|
delete[] src_rgba_f;
|
|
|
|
}
|
2017-11-08 22:18:12 +01:00
|
|
|
|
2018-08-24 09:35:07 +02:00
|
|
|
print_verbose("ETC: Time encoding: " + rtos(OS::get_singleton()->get_ticks_msec() - t));
|
2017-04-26 17:49:08 +02:00
|
|
|
|
2017-11-08 22:18:12 +01:00
|
|
|
p_img->create(imgw, imgh, p_img->has_mipmaps(), etc_format, dst_data);
|
2017-04-26 17:49:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void _compress_etc1(Image *p_img, float p_lossy_quality) {
|
2019-09-27 04:16:44 +02:00
|
|
|
_compress_etc(p_img, p_lossy_quality, true, Image::USED_CHANNELS_RGB);
|
2017-04-26 17:49:08 +02:00
|
|
|
}
|
|
|
|
|
2019-09-27 04:16:44 +02:00
|
|
|
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);
|
2017-04-26 17:49:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void _register_etc_compress_func() {
|
|
|
|
|
|
|
|
Image::_image_compress_etc1_func = _compress_etc1;
|
|
|
|
Image::_image_compress_etc2_func = _compress_etc2;
|
|
|
|
}
|