2016-06-18 14:46:12 +02:00
|
|
|
/*************************************************************************/
|
|
|
|
/* texture_loader_pvr.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2016-06-18 14:46:12 +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). */
|
2016-06-18 14:46:12 +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
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
#include "texture_loader_pvr.h"
|
2015-04-07 02:48:20 +02:00
|
|
|
#include "PvrTcEncoder.h"
|
|
|
|
#include "RgbaBitmap.h"
|
2018-09-11 18:13:45 +02:00
|
|
|
#include "core/os/file_access.h"
|
2017-03-05 16:44:50 +01:00
|
|
|
#include <string.h>
|
2019-01-25 11:49:06 +01:00
|
|
|
#include <new>
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
static void _pvrtc_decompress(Image *p_img);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
enum PVRFLags {
|
2017-03-05 16:44:50 +01:00
|
|
|
PVR_HAS_MIPMAPS = 0x00000100,
|
|
|
|
PVR_TWIDDLED = 0x00000200,
|
|
|
|
PVR_NORMAL_MAP = 0x00000400,
|
|
|
|
PVR_BORDER = 0x00000800,
|
|
|
|
PVR_CUBE_MAP = 0x00001000,
|
|
|
|
PVR_FALSE_MIPMAPS = 0x00002000,
|
|
|
|
PVR_VOLUME_TEXTURES = 0x00004000,
|
|
|
|
PVR_HAS_ALPHA = 0x00008000,
|
|
|
|
PVR_VFLIP = 0x00010000
|
2014-02-10 02:10:30 +01:00
|
|
|
};
|
|
|
|
|
2020-04-20 04:19:21 +02:00
|
|
|
RES ResourceFormatPVR::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, bool p_no_cache) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (r_error) {
|
2017-03-05 16:44:50 +01:00
|
|
|
*r_error = ERR_CANT_OPEN;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
Error err;
|
2017-03-05 16:44:50 +01:00
|
|
|
FileAccess *f = FileAccess::open(p_path, FileAccess::READ, &err);
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!f) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return RES();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
FileAccessRef faref(f);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND_V(err, RES());
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (r_error) {
|
2017-03-05 16:44:50 +01:00
|
|
|
*r_error = ERR_FILE_CORRUPT;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
uint32_t hsize = f->get_32();
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND_V(hsize != 52, RES());
|
2014-02-10 02:10:30 +01:00
|
|
|
uint32_t height = f->get_32();
|
|
|
|
uint32_t width = f->get_32();
|
|
|
|
uint32_t mipmaps = f->get_32();
|
|
|
|
uint32_t flags = f->get_32();
|
|
|
|
uint32_t surfsize = f->get_32();
|
2017-09-10 15:37:49 +02:00
|
|
|
f->seek(f->get_position() + 20); // bpp, rmask, gmask, bmask, amask
|
2017-03-05 16:44:50 +01:00
|
|
|
uint8_t pvrid[5] = { 0, 0, 0, 0, 0 };
|
|
|
|
f->get_buffer(pvrid, 4);
|
|
|
|
ERR_FAIL_COND_V(String((char *)pvrid) != "PVR!", RES());
|
2017-09-02 22:32:31 +02:00
|
|
|
f->get_32(); // surfcount
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
/*
|
2014-02-10 02:10:30 +01:00
|
|
|
print_line("height: "+itos(height));
|
|
|
|
print_line("width: "+itos(width));
|
|
|
|
print_line("mipmaps: "+itos(mipmaps));
|
|
|
|
print_line("flags: "+itos(flags));
|
|
|
|
print_line("surfsize: "+itos(surfsize));
|
|
|
|
print_line("bpp: "+itos(bpp));
|
|
|
|
print_line("rmask: "+itos(rmask));
|
|
|
|
print_line("gmask: "+itos(gmask));
|
|
|
|
print_line("bmask: "+itos(bmask));
|
|
|
|
print_line("amask: "+itos(amask));
|
|
|
|
print_line("surfcount: "+itos(surfcount));
|
2018-08-24 09:35:07 +02:00
|
|
|
*/
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-02-17 22:06:54 +01:00
|
|
|
Vector<uint8_t> data;
|
2014-02-10 02:10:30 +01:00
|
|
|
data.resize(surfsize);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND_V(data.size() == 0, RES());
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-02-17 22:06:54 +01:00
|
|
|
uint8_t *w = data.ptrw();
|
2017-03-05 16:44:50 +01:00
|
|
|
f->get_buffer(&w[0], surfsize);
|
2014-02-10 02:10:30 +01:00
|
|
|
err = f->get_error();
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND_V(err != OK, RES());
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Image::Format format = Image::FORMAT_MAX;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
switch (flags & 0xFF) {
|
2014-02-10 02:10:30 +01:00
|
|
|
case 0x18:
|
2020-05-10 13:00:47 +02:00
|
|
|
case 0xC:
|
|
|
|
format = (flags & PVR_HAS_ALPHA) ? Image::FORMAT_PVRTC2A : Image::FORMAT_PVRTC2;
|
|
|
|
break;
|
2014-02-10 02:10:30 +01:00
|
|
|
case 0x19:
|
2020-05-10 13:00:47 +02:00
|
|
|
case 0xD:
|
|
|
|
format = (flags & PVR_HAS_ALPHA) ? Image::FORMAT_PVRTC4A : Image::FORMAT_PVRTC4;
|
|
|
|
break;
|
2014-02-10 02:10:30 +01:00
|
|
|
case 0x16:
|
2017-03-05 16:44:50 +01:00
|
|
|
format = Image::FORMAT_L8;
|
|
|
|
break;
|
2014-02-10 02:10:30 +01:00
|
|
|
case 0x17:
|
2017-03-05 16:44:50 +01:00
|
|
|
format = Image::FORMAT_LA8;
|
|
|
|
break;
|
2014-02-10 02:10:30 +01:00
|
|
|
case 0x20:
|
|
|
|
case 0x80:
|
|
|
|
case 0x81:
|
2017-03-05 16:44:50 +01:00
|
|
|
format = Image::FORMAT_DXT1;
|
|
|
|
break;
|
2014-02-10 02:10:30 +01:00
|
|
|
case 0x21:
|
|
|
|
case 0x22:
|
|
|
|
case 0x82:
|
|
|
|
case 0x83:
|
2017-03-05 16:44:50 +01:00
|
|
|
format = Image::FORMAT_DXT3;
|
|
|
|
break;
|
2014-02-10 02:10:30 +01:00
|
|
|
case 0x23:
|
|
|
|
case 0x24:
|
|
|
|
case 0x84:
|
|
|
|
case 0x85:
|
2017-03-05 16:44:50 +01:00
|
|
|
format = Image::FORMAT_DXT5;
|
|
|
|
break;
|
2014-02-10 02:10:30 +01:00
|
|
|
case 0x4:
|
|
|
|
case 0x15:
|
2017-03-05 16:44:50 +01:00
|
|
|
format = Image::FORMAT_RGB8;
|
|
|
|
break;
|
2014-02-10 02:10:30 +01:00
|
|
|
case 0x5:
|
|
|
|
case 0x12:
|
2017-03-05 16:44:50 +01:00
|
|
|
format = Image::FORMAT_RGBA8;
|
|
|
|
break;
|
2014-02-10 02:10:30 +01:00
|
|
|
case 0x36:
|
2017-03-05 16:44:50 +01:00
|
|
|
format = Image::FORMAT_ETC;
|
|
|
|
break;
|
2014-02-10 02:10:30 +01:00
|
|
|
default:
|
2019-08-11 10:49:53 +02:00
|
|
|
ERR_FAIL_V_MSG(RES(), "Unsupported format in PVR texture: " + itos(flags & 0xFF) + ".");
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-05-17 12:36:47 +02:00
|
|
|
Ref<Image> image = memnew(Image(width, height, mipmaps, format, data));
|
|
|
|
ERR_FAIL_COND_V(image->empty(), RES());
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Ref<ImageTexture> texture = memnew(ImageTexture);
|
2019-06-11 20:43:37 +02:00
|
|
|
texture->create_from_image(image);
|
2015-08-24 01:15:56 +02:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (r_error) {
|
2017-03-05 16:44:50 +01:00
|
|
|
*r_error = OK;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return texture;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceFormatPVR::get_recognized_extensions(List<String> *p_extensions) const {
|
|
|
|
p_extensions->push_back("pvr");
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool ResourceFormatPVR::handles_type(const String &p_type) const {
|
2019-06-11 20:43:37 +02:00
|
|
|
return ClassDB::is_parent_class(p_type, "Texture2D");
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
String ResourceFormatPVR::get_resource_type(const String &p_path) const {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (p_path.get_extension().to_lower() == "pvr") {
|
2019-06-11 20:43:37 +02:00
|
|
|
return "Texture2D";
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
static void _compress_pvrtc4(Image *p_img) {
|
2017-05-17 12:36:47 +02:00
|
|
|
Ref<Image> img = p_img->duplicate();
|
2015-04-07 02:48:20 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool make_mipmaps = false;
|
2019-05-01 12:34:01 +02:00
|
|
|
if (!img->is_size_po2() || img->get_width() != img->get_height()) {
|
2017-05-17 12:36:47 +02:00
|
|
|
make_mipmaps = img->has_mipmaps();
|
2019-05-01 12:34:01 +02:00
|
|
|
img->resize_to_po2(true);
|
2015-04-07 02:48:20 +02:00
|
|
|
}
|
2017-05-17 12:36:47 +02:00
|
|
|
img->convert(Image::FORMAT_RGBA8);
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!img->has_mipmaps() && make_mipmaps) {
|
2017-05-17 12:36:47 +02:00
|
|
|
img->generate_mipmaps();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2015-04-07 02:48:20 +02:00
|
|
|
|
2017-05-17 12:36:47 +02:00
|
|
|
bool use_alpha = img->detect_alpha();
|
2015-04-07 02:48:20 +02:00
|
|
|
|
2017-05-17 12:36:47 +02:00
|
|
|
Ref<Image> new_img;
|
|
|
|
new_img.instance();
|
2019-05-01 12:34:01 +02:00
|
|
|
new_img->create(img->get_width(), img->get_height(), img->has_mipmaps(), use_alpha ? Image::FORMAT_PVRTC4A : Image::FORMAT_PVRTC4);
|
2017-05-17 12:36:47 +02:00
|
|
|
|
2020-02-17 22:06:54 +01:00
|
|
|
Vector<uint8_t> data = new_img->get_data();
|
2015-04-07 02:48:20 +02:00
|
|
|
{
|
2020-02-17 22:06:54 +01:00
|
|
|
uint8_t *wr = data.ptrw();
|
|
|
|
const uint8_t *r = img->get_data().ptr();
|
2015-04-07 02:48:20 +02:00
|
|
|
|
2017-05-17 12:36:47 +02:00
|
|
|
for (int i = 0; i <= new_img->get_mipmap_count(); i++) {
|
2017-03-05 16:44:50 +01:00
|
|
|
int ofs, size, w, h;
|
2017-05-17 12:36:47 +02:00
|
|
|
img->get_mipmap_offset_size_and_dimensions(i, ofs, size, w, h);
|
2017-03-05 16:44:50 +01:00
|
|
|
Javelin::RgbaBitmap bm(w, h);
|
2020-09-16 17:07:54 +02:00
|
|
|
void *dst = (void *)bm.GetData();
|
|
|
|
copymem(dst, &r[ofs], size);
|
|
|
|
Javelin::ColorRgba<unsigned char> *dp = bm.GetData();
|
2019-02-21 20:57:39 +01:00
|
|
|
for (int j = 0; j < size / 4; j++) {
|
2020-09-16 17:07:54 +02:00
|
|
|
/* red and blue colors are swapped. */
|
|
|
|
SWAP(dp[j].r, dp[j].b);
|
2015-04-07 02:48:20 +02:00
|
|
|
}
|
2017-05-17 12:36:47 +02:00
|
|
|
new_img->get_mipmap_offset_size_and_dimensions(i, ofs, size, w, h);
|
2017-03-05 16:44:50 +01:00
|
|
|
Javelin::PvrTcEncoder::EncodeRgba4Bpp(&wr[ofs], bm);
|
2015-04-07 02:48:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-17 12:36:47 +02:00
|
|
|
p_img->create(new_img->get_width(), new_img->get_height(), new_img->has_mipmaps(), new_img->get_format(), data);
|
2015-04-07 02:48:20 +02:00
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
ResourceFormatPVR::ResourceFormatPVR() {
|
2017-03-05 16:44:50 +01:00
|
|
|
Image::_image_decompress_pvrtc = _pvrtc_decompress;
|
|
|
|
Image::_image_compress_pvrtc4_func = _compress_pvrtc4;
|
|
|
|
Image::_image_compress_pvrtc2_func = _compress_pvrtc4;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2018-07-18 16:24:33 +02:00
|
|
|
/////////////////////////////////////////////////////////
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2018-07-18 16:24:33 +02:00
|
|
|
//PVRTC decompressor, Based on PVRTC decompressor by IMGTEC.
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2018-07-18 16:24:33 +02:00
|
|
|
/////////////////////////////////////////////////////////
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
#define PT_INDEX 2
|
|
|
|
#define BLK_Y_SIZE 4
|
|
|
|
#define BLK_X_MAX 8
|
|
|
|
#define BLK_X_2BPP 8
|
|
|
|
#define BLK_X_4BPP 4
|
|
|
|
|
|
|
|
#define WRAP_COORD(Val, Size) ((Val) & ((Size)-1))
|
|
|
|
|
|
|
|
/*
|
|
|
|
Define an expression to either wrap or clamp large or small vals to the
|
|
|
|
legal coordinate range
|
|
|
|
*/
|
|
|
|
#define LIMIT_COORD(Val, Size, p_tiled) \
|
2017-03-05 16:44:50 +01:00
|
|
|
((p_tiled) ? WRAP_COORD((Val), (Size)) : CLAMP((Val), 0, (Size)-1))
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
struct PVRTCBlock {
|
|
|
|
//blocks are 64 bits
|
|
|
|
uint32_t data[2];
|
|
|
|
};
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
_FORCE_INLINE_ bool is_po2(uint32_t p_input) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (p_input == 0) {
|
2020-05-14 11:00:19 +02:00
|
|
|
return false;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
uint32_t minus1 = p_input - 1;
|
2020-05-14 11:00:19 +02:00
|
|
|
return ((p_input | minus1) == (p_input ^ minus1)) ? true : false;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
static void unpack_5554(const PVRTCBlock *p_block, int p_ab_colors[2][4]) {
|
2014-02-10 02:10:30 +01:00
|
|
|
uint32_t raw_bits[2];
|
|
|
|
raw_bits[0] = p_block->data[1] & (0xFFFE);
|
|
|
|
raw_bits[1] = p_block->data[1] >> 16;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < 2; i++) {
|
|
|
|
if (raw_bits[i] & (1 << 15)) {
|
|
|
|
p_ab_colors[i][0] = (raw_bits[i] >> 10) & 0x1F;
|
|
|
|
p_ab_colors[i][1] = (raw_bits[i] >> 5) & 0x1F;
|
|
|
|
p_ab_colors[i][2] = raw_bits[i] & 0x1F;
|
2020-05-14 16:41:43 +02:00
|
|
|
if (i == 0) {
|
2017-03-05 16:44:50 +01:00
|
|
|
p_ab_colors[0][2] |= p_ab_colors[0][2] >> 4;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
p_ab_colors[i][3] = 0xF;
|
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
p_ab_colors[i][0] = (raw_bits[i] >> (8 - 1)) & 0x1E;
|
|
|
|
p_ab_colors[i][1] = (raw_bits[i] >> (4 - 1)) & 0x1E;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
p_ab_colors[i][0] |= p_ab_colors[i][0] >> 4;
|
|
|
|
p_ab_colors[i][1] |= p_ab_colors[i][1] >> 4;
|
|
|
|
|
|
|
|
p_ab_colors[i][2] = (raw_bits[i] & 0xF) << 1;
|
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (i == 0) {
|
2014-02-10 02:10:30 +01:00
|
|
|
p_ab_colors[0][2] |= p_ab_colors[0][2] >> 3;
|
2020-05-14 16:41:43 +02:00
|
|
|
} else {
|
2014-02-10 02:10:30 +01:00
|
|
|
p_ab_colors[0][2] |= p_ab_colors[0][2] >> 4;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
p_ab_colors[i][3] = (raw_bits[i] >> 11) & 0xE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
static void unpack_modulations(const PVRTCBlock *p_block, const int p_2bit, int p_modulation[8][16], int p_modulation_modes[8][16], int p_x, int p_y) {
|
2014-02-10 02:10:30 +01:00
|
|
|
int block_mod_mode = p_block->data[1] & 1;
|
|
|
|
uint32_t modulation_bits = p_block->data[0];
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_2bit && block_mod_mode) {
|
|
|
|
for (int y = 0; y < BLK_Y_SIZE; y++) {
|
|
|
|
for (int x = 0; x < BLK_X_2BPP; x++) {
|
|
|
|
p_modulation_modes[y + p_y][x + p_x] = block_mod_mode;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (((x ^ y) & 1) == 0) {
|
|
|
|
p_modulation[y + p_y][x + p_x] = modulation_bits & 3;
|
2014-02-10 02:10:30 +01:00
|
|
|
modulation_bits >>= 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
} else if (p_2bit) {
|
|
|
|
for (int y = 0; y < BLK_Y_SIZE; y++) {
|
|
|
|
for (int x = 0; x < BLK_X_2BPP; x++) {
|
|
|
|
p_modulation_modes[y + p_y][x + p_x] = block_mod_mode;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (modulation_bits & 1) {
|
2017-03-05 16:44:50 +01:00
|
|
|
p_modulation[y + p_y][x + p_x] = 0x3;
|
2020-05-14 16:41:43 +02:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
p_modulation[y + p_y][x + p_x] = 0x0;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
modulation_bits >>= 1;
|
|
|
|
}
|
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
} else {
|
|
|
|
for (int y = 0; y < BLK_Y_SIZE; y++) {
|
|
|
|
for (int x = 0; x < BLK_X_4BPP; x++) {
|
|
|
|
p_modulation_modes[y + p_y][x + p_x] = block_mod_mode;
|
|
|
|
p_modulation[y + p_y][x + p_x] = modulation_bits & 3;
|
2014-02-10 02:10:30 +01:00
|
|
|
modulation_bits >>= 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND(modulation_bits != 0);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void interpolate_colors(const int p_colorp[4], const int p_colorq[4], const int p_colorr[4], const int p_colors[4], bool p_2bit, const int x, const int y, int r_result[4]) {
|
|
|
|
int u, v, uscale;
|
|
|
|
int k;
|
|
|
|
|
|
|
|
int tmp1, tmp2;
|
|
|
|
|
|
|
|
int P[4], Q[4], R[4], S[4];
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (k = 0; k < 4; k++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
P[k] = p_colorp[k];
|
|
|
|
Q[k] = p_colorq[k];
|
|
|
|
R[k] = p_colorr[k];
|
|
|
|
S[k] = p_colors[k];
|
|
|
|
}
|
|
|
|
|
|
|
|
v = (y & 0x3) | ((~y & 0x2) << 1);
|
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (p_2bit) {
|
2014-02-10 02:10:30 +01:00
|
|
|
u = (x & 0x7) | ((~x & 0x4) << 1);
|
2020-05-14 16:41:43 +02:00
|
|
|
} else {
|
2014-02-10 02:10:30 +01:00
|
|
|
u = (x & 0x3) | ((~x & 0x2) << 1);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
v = v - BLK_Y_SIZE / 2;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_2bit) {
|
|
|
|
u = u - BLK_X_2BPP / 2;
|
2014-02-10 02:10:30 +01:00
|
|
|
uscale = 8;
|
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
u = u - BLK_X_4BPP / 2;
|
2014-02-10 02:10:30 +01:00
|
|
|
uscale = 4;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (k = 0; k < 4; k++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
tmp1 = P[k] * uscale + u * (Q[k] - P[k]);
|
|
|
|
tmp2 = R[k] * uscale + u * (S[k] - R[k]);
|
|
|
|
|
|
|
|
tmp1 = tmp1 * 4 + v * (tmp2 - tmp1);
|
|
|
|
|
|
|
|
r_result[k] = tmp1;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_2bit) {
|
|
|
|
for (k = 0; k < 3; k++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
r_result[k] >>= 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
r_result[3] >>= 1;
|
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
for (k = 0; k < 3; k++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
r_result[k] >>= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (k = 0; k < 4; k++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
ERR_FAIL_COND(r_result[k] >= 256);
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (k = 0; k < 3; k++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
r_result[k] += r_result[k] >> 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
r_result[3] += r_result[3] >> 4;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (k = 0; k < 4; k++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
ERR_FAIL_COND(r_result[k] >= 256);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
static void get_modulation_value(int x, int y, const int p_2bit, const int p_modulation[8][16], const int p_modulation_modes[8][16], int *r_mod, int *p_dopt) {
|
|
|
|
static const int rep_vals0[4] = { 0, 3, 5, 8 };
|
|
|
|
static const int rep_vals1[4] = { 0, 4, 4, 8 };
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
int mod_val;
|
|
|
|
|
|
|
|
y = (y & 0x3) | ((~y & 0x2) << 1);
|
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (p_2bit) {
|
2014-02-10 02:10:30 +01:00
|
|
|
x = (x & 0x7) | ((~x & 0x4) << 1);
|
2020-05-14 16:41:43 +02:00
|
|
|
} else {
|
2014-02-10 02:10:30 +01:00
|
|
|
x = (x & 0x3) | ((~x & 0x2) << 1);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
*p_dopt = 0;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_modulation_modes[y][x] == 0) {
|
2014-02-10 02:10:30 +01:00
|
|
|
mod_val = rep_vals0[p_modulation[y][x]];
|
2017-03-05 16:44:50 +01:00
|
|
|
} else if (p_2bit) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (((x ^ y) & 1) == 0) {
|
2014-02-10 02:10:30 +01:00
|
|
|
mod_val = rep_vals0[p_modulation[y][x]];
|
2020-05-14 16:41:43 +02:00
|
|
|
} else if (p_modulation_modes[y][x] == 1) {
|
2017-03-05 16:44:50 +01:00
|
|
|
mod_val = (rep_vals0[p_modulation[y - 1][x]] +
|
|
|
|
rep_vals0[p_modulation[y + 1][x]] +
|
|
|
|
rep_vals0[p_modulation[y][x - 1]] +
|
|
|
|
rep_vals0[p_modulation[y][x + 1]] + 2) /
|
|
|
|
4;
|
|
|
|
} else if (p_modulation_modes[y][x] == 2) {
|
|
|
|
mod_val = (rep_vals0[p_modulation[y][x - 1]] +
|
|
|
|
rep_vals0[p_modulation[y][x + 1]] + 1) /
|
|
|
|
2;
|
2014-02-10 02:10:30 +01:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
mod_val = (rep_vals0[p_modulation[y - 1][x]] +
|
|
|
|
rep_vals0[p_modulation[y + 1][x]] + 1) /
|
|
|
|
2;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
} else {
|
2014-02-10 02:10:30 +01:00
|
|
|
mod_val = rep_vals1[p_modulation[y][x]];
|
|
|
|
|
|
|
|
*p_dopt = p_modulation[y][x] == PT_INDEX;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
*r_mod = mod_val;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int disable_twiddling = 0;
|
|
|
|
|
|
|
|
static uint32_t twiddle_uv(uint32_t p_height, uint32_t p_width, uint32_t p_y, uint32_t p_x) {
|
|
|
|
uint32_t twiddled;
|
|
|
|
|
|
|
|
uint32_t min_dimension;
|
|
|
|
uint32_t max_value;
|
|
|
|
|
|
|
|
uint32_t scr_bit_pos;
|
|
|
|
uint32_t dst_bit_pos;
|
|
|
|
|
|
|
|
int shift_count;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND_V(p_y >= p_height, 0);
|
|
|
|
ERR_FAIL_COND_V(p_x >= p_width, 0);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND_V(!is_po2(p_height), 0);
|
|
|
|
ERR_FAIL_COND_V(!is_po2(p_width), 0);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_height < p_width) {
|
2014-02-10 02:10:30 +01:00
|
|
|
min_dimension = p_height;
|
2017-03-05 16:44:50 +01:00
|
|
|
max_value = p_x;
|
2014-02-10 02:10:30 +01:00
|
|
|
} else {
|
|
|
|
min_dimension = p_width;
|
2017-03-05 16:44:50 +01:00
|
|
|
max_value = p_y;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (disable_twiddling) {
|
2017-03-05 16:44:50 +01:00
|
|
|
return (p_y * p_width + p_x);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
scr_bit_pos = 1;
|
|
|
|
dst_bit_pos = 1;
|
2017-03-05 16:44:50 +01:00
|
|
|
twiddled = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
shift_count = 0;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
while (scr_bit_pos < min_dimension) {
|
|
|
|
if (p_y & scr_bit_pos) {
|
2014-02-10 02:10:30 +01:00
|
|
|
twiddled |= dst_bit_pos;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_x & scr_bit_pos) {
|
2014-02-10 02:10:30 +01:00
|
|
|
twiddled |= (dst_bit_pos << 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
scr_bit_pos <<= 1;
|
|
|
|
dst_bit_pos <<= 2;
|
|
|
|
shift_count += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
max_value >>= shift_count;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
twiddled |= (max_value << (2 * shift_count));
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return twiddled;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
static void decompress_pvrtc(PVRTCBlock *p_comp_img, const int p_2bit, const int p_width, const int p_height, const int p_tiled, unsigned char *p_dst) {
|
2014-02-10 02:10:30 +01:00
|
|
|
int x, y;
|
|
|
|
int i, j;
|
|
|
|
|
|
|
|
int block_x, blk_y;
|
|
|
|
int block_xp1, blk_yp1;
|
|
|
|
int x_block_size;
|
|
|
|
int block_width, block_height;
|
|
|
|
|
|
|
|
int p_x, p_y;
|
|
|
|
|
2018-04-19 13:04:41 +02:00
|
|
|
int p_modulation[8][16] = { { 0 } };
|
|
|
|
int p_modulation_modes[8][16] = { { 0 } };
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
int Mod, DoPT;
|
|
|
|
|
|
|
|
unsigned int u_pos;
|
|
|
|
|
|
|
|
// local neighbourhood of blocks
|
|
|
|
PVRTCBlock *p_blocks[2][2];
|
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
PVRTCBlock *prev[2][2] = { { nullptr, nullptr }, { nullptr, nullptr } };
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
int Reps[2][4];
|
2017-03-05 16:44:50 +01:00
|
|
|
} colors5554[2][2];
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
int ASig[4], BSig[4];
|
|
|
|
|
|
|
|
int r_result[4];
|
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (p_2bit) {
|
2014-02-10 02:10:30 +01:00
|
|
|
x_block_size = BLK_X_2BPP;
|
2020-05-14 16:41:43 +02:00
|
|
|
} else {
|
2014-02-10 02:10:30 +01:00
|
|
|
x_block_size = BLK_X_4BPP;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
block_width = MAX(2, p_width / x_block_size);
|
|
|
|
block_height = MAX(2, p_height / BLK_Y_SIZE);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (y = 0; y < p_height; y++) {
|
|
|
|
for (x = 0; x < p_width; x++) {
|
|
|
|
block_x = (x - x_block_size / 2);
|
|
|
|
blk_y = (y - BLK_Y_SIZE / 2);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
block_x = LIMIT_COORD(block_x, p_width, p_tiled);
|
|
|
|
blk_y = LIMIT_COORD(blk_y, p_height, p_tiled);
|
|
|
|
|
|
|
|
block_x /= x_block_size;
|
|
|
|
blk_y /= BLK_Y_SIZE;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
block_xp1 = LIMIT_COORD(block_x + 1, block_width, p_tiled);
|
|
|
|
blk_yp1 = LIMIT_COORD(blk_y + 1, block_height, p_tiled);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
p_blocks[0][0] = p_comp_img + twiddle_uv(block_height, block_width, blk_y, block_x);
|
|
|
|
p_blocks[0][1] = p_comp_img + twiddle_uv(block_height, block_width, blk_y, block_xp1);
|
|
|
|
p_blocks[1][0] = p_comp_img + twiddle_uv(block_height, block_width, blk_yp1, block_x);
|
|
|
|
p_blocks[1][1] = p_comp_img + twiddle_uv(block_height, block_width, blk_yp1, block_xp1);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (memcmp(prev, p_blocks, 4 * sizeof(void *)) != 0) {
|
2014-02-10 02:10:30 +01:00
|
|
|
p_y = 0;
|
2017-03-05 16:44:50 +01:00
|
|
|
for (i = 0; i < 2; i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
p_x = 0;
|
2017-03-05 16:44:50 +01:00
|
|
|
for (j = 0; j < 2; j++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
unpack_5554(p_blocks[i][j], colors5554[i][j].Reps);
|
|
|
|
|
2017-01-15 22:15:47 +01:00
|
|
|
unpack_modulations(
|
|
|
|
p_blocks[i][j],
|
|
|
|
p_2bit,
|
|
|
|
p_modulation,
|
|
|
|
p_modulation_modes,
|
|
|
|
p_x, p_y);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
p_x += x_block_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
p_y += BLK_Y_SIZE;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
memcpy(prev, p_blocks, 4 * sizeof(void *));
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-01-15 22:15:47 +01:00
|
|
|
interpolate_colors(
|
|
|
|
colors5554[0][0].Reps[0],
|
|
|
|
colors5554[0][1].Reps[0],
|
|
|
|
colors5554[1][0].Reps[0],
|
|
|
|
colors5554[1][1].Reps[0],
|
|
|
|
p_2bit, x, y,
|
|
|
|
ASig);
|
|
|
|
|
|
|
|
interpolate_colors(
|
|
|
|
colors5554[0][0].Reps[1],
|
|
|
|
colors5554[0][1].Reps[1],
|
|
|
|
colors5554[1][0].Reps[1],
|
|
|
|
colors5554[1][1].Reps[1],
|
|
|
|
p_2bit, x, y,
|
|
|
|
BSig);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
get_modulation_value(x, y, p_2bit, (const int(*)[16])p_modulation, (const int(*)[16])p_modulation_modes,
|
|
|
|
&Mod, &DoPT);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (i = 0; i < 4; i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
r_result[i] = ASig[i] * 8 + Mod * (BSig[i] - ASig[i]);
|
|
|
|
r_result[i] >>= 3;
|
|
|
|
}
|
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (DoPT) {
|
2014-02-10 02:10:30 +01:00
|
|
|
r_result[3] = 0;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
u_pos = (x + y * p_width) << 2;
|
|
|
|
p_dst[u_pos + 0] = (uint8_t)r_result[0];
|
|
|
|
p_dst[u_pos + 1] = (uint8_t)r_result[1];
|
|
|
|
p_dst[u_pos + 2] = (uint8_t)r_result[2];
|
|
|
|
p_dst[u_pos + 3] = (uint8_t)r_result[3];
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
static void _pvrtc_decompress(Image *p_img) {
|
|
|
|
ERR_FAIL_COND(p_img->get_format() != Image::FORMAT_PVRTC2 && p_img->get_format() != Image::FORMAT_PVRTC2A && p_img->get_format() != Image::FORMAT_PVRTC4 && p_img->get_format() != Image::FORMAT_PVRTC4A);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool _2bit = (p_img->get_format() == Image::FORMAT_PVRTC2 || p_img->get_format() == Image::FORMAT_PVRTC2A);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-02-17 22:06:54 +01:00
|
|
|
Vector<uint8_t> data = p_img->get_data();
|
|
|
|
const uint8_t *r = data.ptr();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-02-17 22:06:54 +01:00
|
|
|
Vector<uint8_t> newdata;
|
2017-03-05 16:44:50 +01:00
|
|
|
newdata.resize(p_img->get_width() * p_img->get_height() * 4);
|
2020-02-17 22:06:54 +01:00
|
|
|
uint8_t *w = newdata.ptrw();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-02-17 22:06:54 +01:00
|
|
|
decompress_pvrtc((PVRTCBlock *)r, _2bit, p_img->get_width(), p_img->get_height(), 0, (unsigned char *)w);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool make_mipmaps = p_img->has_mipmaps();
|
2017-05-17 12:36:47 +02:00
|
|
|
p_img->create(p_img->get_width(), p_img->get_height(), false, Image::FORMAT_RGBA8, newdata);
|
2020-05-14 16:41:43 +02:00
|
|
|
if (make_mipmaps) {
|
2017-05-17 12:36:47 +02:00
|
|
|
p_img->generate_mipmaps();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|