2016-06-18 12:46:12 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* texture_loader_dds.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2016-06-18 12:46:12 +00:00
|
|
|
/*************************************************************************/
|
2021-01-01 19:13:46 +00:00
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
|
2016-06-18 12:46:12 +00: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-04 23:50:27 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
#include "texture_loader_dds.h"
|
2020-06-15 11:10:21 +00:00
|
|
|
|
2018-09-11 16:13:45 +00:00
|
|
|
#include "core/os/file_access.h"
|
2014-02-10 01:10:30 +00:00
|
|
|
|
Fix few GCC9 warnings:
thirdparty/assimp/include/assimp/material.inl: In member function 'aiReturn aiMaterial::Get(const char*, unsigned int, unsigned int, aiColor3D&) const':
thirdparty/assimp/include/assimp/material.inl:176:33: error: implicitly-declared 'aiColor3D& aiColor3D::operator=(const aiColor3D&)' is deprecated [-Werror=deprecated-copy]
176 | pOut = aiColor3D(c.r,c.g,c.b);
modules/dds/texture_loader_dds.cpp:167:50: error: comparison of integer expressions of different signedness: 'uint32_t' {aka 'unsigned int'} and 'int' [-Werror=sign-compare]
167 | if (format_flags & DDPF_FOURCC && format_fourcc == PF_FOURCC("DXT1")) {
| ^
[ 28%] modules/dds/texture_loader_dds.cpp:170:57: error: comparison of integer expressions of different signedness: 'uint32_t' {aka 'unsigned int'} and 'int' [-Werror=sign-compare]
170 | } else if (format_flags & DDPF_FOURCC && format_fourcc == PF_FOURCC("DXT3")) {
| ^
modules/dds/texture_loader_dds.cpp:174:57: error: comparison of integer expressions of different signedness: 'uint32_t' {aka 'unsigned int'} and 'int' [-Werror=sign-compare]
174 | } else if (format_flags & DDPF_FOURCC && format_fourcc == PF_FOURCC("DXT5")) {
| ^
modules/dds/texture_loader_dds.cpp:177:57: error: comparison of integer expressions of different signedness: 'uint32_t' {aka 'unsigned int'} and 'int' [-Werror=sign-compare]
177 | } else if (format_flags & DDPF_FOURCC && format_fourcc == PF_FOURCC("ATI1")) {
| ^
modules/dds/texture_loader_dds.cpp:180:57: error: comparison of integer expressions of different signedness: 'uint32_t' {aka 'unsigned int'} and 'int' [-Werror=sign-compare]
180 | } else if (format_flags & DDPF_FOURCC && format_fourcc == PF_FOURCC("ATI2")) {
| ^
modules/dds/texture_loader_dds.cpp:183:57: error: comparison of integer expressions of different signedness: 'uint32_t' {aka 'unsigned int'} and 'int' [-Werror=sign-compare]
183 | } else if (format_flags & DDPF_FOURCC && format_fourcc == PF_FOURCC("A2XY")) {
| ^
thirdparty/assimp/include/assimp/material.inl: In member function 'aiReturn aiMaterial::Get(const char*, unsigned int, unsigned int, aiColor3D&) const':
thirdparty/assimp/include/assimp/material.inl:176:33: error: implicitly-declared 'aiColor3D& aiColor3D::operator=(const aiColor3D&)' is deprecated [-Werror=deprecated-copy]
176 | pOut = aiColor3D(c.r,c.g,c.b);
2019-07-02 11:23:31 +00:00
|
|
|
#define PF_FOURCC(s) ((uint32_t)(((s)[3] << 24U) | ((s)[2] << 16U) | ((s)[1] << 8U) | ((s)[0])))
|
2019-05-07 09:26:58 +00:00
|
|
|
|
2020-06-15 11:10:21 +00:00
|
|
|
// Reference: https://docs.microsoft.com/en-us/windows/win32/direct3ddds/dds-header
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
enum {
|
2017-03-05 15:44:50 +00:00
|
|
|
DDS_MAGIC = 0x20534444,
|
|
|
|
DDSD_PITCH = 0x00000008,
|
|
|
|
DDSD_LINEARSIZE = 0x00080000,
|
|
|
|
DDSD_MIPMAPCOUNT = 0x00020000,
|
|
|
|
DDPF_FOURCC = 0x00000004,
|
|
|
|
DDPF_ALPHAPIXELS = 0x00000001,
|
|
|
|
DDPF_INDEXED = 0x00000020,
|
|
|
|
DDPF_RGB = 0x00000040,
|
2014-02-10 01:10:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
enum DDSFormat {
|
|
|
|
DDS_DXT1,
|
|
|
|
DDS_DXT3,
|
|
|
|
DDS_DXT5,
|
|
|
|
DDS_ATI1,
|
|
|
|
DDS_ATI2,
|
2019-05-07 09:26:58 +00:00
|
|
|
DDS_A2XY,
|
2014-02-10 01:10:30 +00:00
|
|
|
DDS_BGRA8,
|
|
|
|
DDS_BGR8,
|
|
|
|
DDS_RGBA8, //flipped in dds
|
|
|
|
DDS_RGB8, //flipped in dds
|
|
|
|
DDS_BGR5A1,
|
|
|
|
DDS_BGR565,
|
|
|
|
DDS_BGR10A2,
|
|
|
|
DDS_INDEXED,
|
|
|
|
DDS_LUMINANCE,
|
|
|
|
DDS_LUMINANCE_ALPHA,
|
|
|
|
DDS_MAX
|
|
|
|
};
|
|
|
|
|
|
|
|
struct DDSFormatInfo {
|
|
|
|
const char *name;
|
2021-02-08 09:57:18 +00:00
|
|
|
bool compressed = false;
|
|
|
|
bool palette = false;
|
|
|
|
uint32_t divisor = 0;
|
|
|
|
uint32_t block_size = 0;
|
|
|
|
Image::Format format = Image::Format::FORMAT_BPTC_RGBA;
|
2014-02-10 01:10:30 +00:00
|
|
|
};
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
static const DDSFormatInfo dds_format_info[DDS_MAX] = {
|
2019-05-07 09:26:58 +00:00
|
|
|
{ "DXT1/BC1", true, false, 4, 8, Image::FORMAT_DXT1 },
|
|
|
|
{ "DXT3/BC2", true, false, 4, 16, Image::FORMAT_DXT3 },
|
|
|
|
{ "DXT5/BC3", true, false, 4, 16, Image::FORMAT_DXT5 },
|
|
|
|
{ "ATI1/BC4", true, false, 4, 8, Image::FORMAT_RGTC_R },
|
|
|
|
{ "ATI2/3DC/BC5", true, false, 4, 16, Image::FORMAT_RGTC_RG },
|
|
|
|
{ "A2XY/DXN/BC5", true, false, 4, 16, Image::FORMAT_RGTC_RG },
|
2017-03-05 15:44:50 +00:00
|
|
|
{ "BGRA8", false, false, 1, 4, Image::FORMAT_RGBA8 },
|
|
|
|
{ "BGR8", false, false, 1, 3, Image::FORMAT_RGB8 },
|
|
|
|
{ "RGBA8", false, false, 1, 4, Image::FORMAT_RGBA8 },
|
|
|
|
{ "RGB8", false, false, 1, 3, Image::FORMAT_RGB8 },
|
|
|
|
{ "BGR5A1", false, false, 1, 2, Image::FORMAT_RGBA8 },
|
|
|
|
{ "BGR565", false, false, 1, 2, Image::FORMAT_RGB8 },
|
|
|
|
{ "BGR10A2", false, false, 1, 4, Image::FORMAT_RGBA8 },
|
|
|
|
{ "GRAYSCALE", false, false, 1, 1, Image::FORMAT_L8 },
|
|
|
|
{ "GRAYSCALE_ALPHA", false, false, 1, 2, Image::FORMAT_LA8 }
|
2014-02-10 01:10:30 +00:00
|
|
|
};
|
|
|
|
|
2021-02-11 17:18:45 +00:00
|
|
|
RES ResourceFormatDDS::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (r_error) {
|
2017-03-05 15:44:50 +00:00
|
|
|
*r_error = ERR_CANT_OPEN;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
Error err;
|
2017-03-05 15:44:50 +00:00
|
|
|
FileAccess *f = FileAccess::open(p_path, FileAccess::READ, &err);
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!f) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return RES();
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
FileAccessRef fref(f);
|
2020-05-14 14:41:43 +00:00
|
|
|
if (r_error) {
|
2017-03-05 15:44:50 +00:00
|
|
|
*r_error = ERR_FILE_CORRUPT;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2019-09-25 08:28:50 +00:00
|
|
|
ERR_FAIL_COND_V_MSG(err != OK, RES(), "Unable to open DDS texture file '" + p_path + "'.");
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
uint32_t magic = f->get_32();
|
|
|
|
uint32_t hsize = f->get_32();
|
|
|
|
uint32_t flags = f->get_32();
|
|
|
|
uint32_t height = f->get_32();
|
2018-10-03 15:03:19 +00:00
|
|
|
uint32_t width = f->get_32();
|
2014-02-10 01:10:30 +00:00
|
|
|
uint32_t pitch = f->get_32();
|
2017-02-21 09:59:19 +00:00
|
|
|
/* uint32_t depth = */ f->get_32();
|
2014-02-10 01:10:30 +00:00
|
|
|
uint32_t mipmaps = f->get_32();
|
|
|
|
|
|
|
|
//skip 11
|
2020-05-14 14:41:43 +00:00
|
|
|
for (int i = 0; i < 11; i++) {
|
2014-02-10 01:10:30 +00:00
|
|
|
f->get_32();
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
//validate
|
|
|
|
|
2020-06-15 11:10:21 +00:00
|
|
|
// We don't check DDSD_CAPS or DDSD_PIXELFORMAT, as they're mandatory when writing,
|
|
|
|
// but non-mandatory when reading (as some writers don't set them)...
|
|
|
|
if (magic != DDS_MAGIC || hsize != 124) {
|
2019-09-25 08:28:50 +00:00
|
|
|
ERR_FAIL_V_MSG(RES(), "Invalid or unsupported DDS texture file '" + p_path + "'.");
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-02-21 09:59:19 +00:00
|
|
|
/* uint32_t format_size = */ f->get_32();
|
2014-02-10 01:10:30 +00:00
|
|
|
uint32_t format_flags = f->get_32();
|
|
|
|
uint32_t format_fourcc = f->get_32();
|
|
|
|
uint32_t format_rgb_bits = f->get_32();
|
|
|
|
uint32_t format_red_mask = f->get_32();
|
|
|
|
uint32_t format_green_mask = f->get_32();
|
|
|
|
uint32_t format_blue_mask = f->get_32();
|
|
|
|
uint32_t format_alpha_mask = f->get_32();
|
|
|
|
|
2017-02-21 09:59:19 +00:00
|
|
|
/* uint32_t caps_1 = */ f->get_32();
|
|
|
|
/* uint32_t caps_2 = */ f->get_32();
|
|
|
|
/* uint32_t caps_ddsx = */ f->get_32();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
//reserved skip
|
|
|
|
f->get_32();
|
|
|
|
f->get_32();
|
|
|
|
|
2017-04-23 08:32:52 +00:00
|
|
|
/*
|
|
|
|
print_line("DDS width: "+itos(width));
|
2014-02-10 01:10:30 +00:00
|
|
|
print_line("DDS height: "+itos(height));
|
2017-04-23 08:32:52 +00:00
|
|
|
print_line("DDS mipmaps: "+itos(mipmaps));
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-04-23 08:32:52 +00:00
|
|
|
printf("fourcc: %x fflags: %x, rgbbits: %x, fsize: %x\n",format_fourcc,format_flags,format_rgb_bits,format_size);
|
|
|
|
printf("rmask: %x gmask: %x, bmask: %x, amask: %x\n",format_red_mask,format_green_mask,format_blue_mask,format_alpha_mask);
|
|
|
|
*/
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
//must avoid this later
|
2020-05-14 14:41:43 +00:00
|
|
|
while (f->get_position() < 128) {
|
2014-02-10 01:10:30 +00:00
|
|
|
f->get_8();
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
DDSFormat dds_format;
|
|
|
|
|
2019-05-07 09:26:58 +00:00
|
|
|
if (format_flags & DDPF_FOURCC && format_fourcc == PF_FOURCC("DXT1")) {
|
2017-03-05 15:44:50 +00:00
|
|
|
dds_format = DDS_DXT1;
|
2019-05-07 09:26:58 +00:00
|
|
|
} else if (format_flags & DDPF_FOURCC && format_fourcc == PF_FOURCC("DXT3")) {
|
2017-03-05 15:44:50 +00:00
|
|
|
dds_format = DDS_DXT3;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2019-05-07 09:26:58 +00:00
|
|
|
} else if (format_flags & DDPF_FOURCC && format_fourcc == PF_FOURCC("DXT5")) {
|
2017-03-05 15:44:50 +00:00
|
|
|
dds_format = DDS_DXT5;
|
2019-05-07 09:26:58 +00:00
|
|
|
} else if (format_flags & DDPF_FOURCC && format_fourcc == PF_FOURCC("ATI1")) {
|
2017-03-05 15:44:50 +00:00
|
|
|
dds_format = DDS_ATI1;
|
2019-05-07 09:26:58 +00:00
|
|
|
} else if (format_flags & DDPF_FOURCC && format_fourcc == PF_FOURCC("ATI2")) {
|
2017-03-05 15:44:50 +00:00
|
|
|
dds_format = DDS_ATI2;
|
2019-05-07 09:26:58 +00:00
|
|
|
} else if (format_flags & DDPF_FOURCC && format_fourcc == PF_FOURCC("A2XY")) {
|
|
|
|
dds_format = DDS_A2XY;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
} else if (format_flags & DDPF_RGB && format_flags & DDPF_ALPHAPIXELS && format_rgb_bits == 32 && format_red_mask == 0xff0000 && format_green_mask == 0xff00 && format_blue_mask == 0xff && format_alpha_mask == 0xff000000) {
|
|
|
|
dds_format = DDS_BGRA8;
|
|
|
|
} else if (format_flags & DDPF_RGB && !(format_flags & DDPF_ALPHAPIXELS) && format_rgb_bits == 24 && format_red_mask == 0xff0000 && format_green_mask == 0xff00 && format_blue_mask == 0xff) {
|
|
|
|
dds_format = DDS_BGR8;
|
|
|
|
} else if (format_flags & DDPF_RGB && format_flags & DDPF_ALPHAPIXELS && format_rgb_bits == 32 && format_red_mask == 0xff && format_green_mask == 0xff00 && format_blue_mask == 0xff0000 && format_alpha_mask == 0xff000000) {
|
|
|
|
dds_format = DDS_RGBA8;
|
|
|
|
} else if (format_flags & DDPF_RGB && !(format_flags & DDPF_ALPHAPIXELS) && format_rgb_bits == 24 && format_red_mask == 0xff && format_green_mask == 0xff00 && format_blue_mask == 0xff0000) {
|
|
|
|
dds_format = DDS_RGB8;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
} else if (format_flags & DDPF_RGB && format_flags & DDPF_ALPHAPIXELS && format_rgb_bits == 16 && format_red_mask == 0x00007c00 && format_green_mask == 0x000003e0 && format_blue_mask == 0x0000001f && format_alpha_mask == 0x00008000) {
|
|
|
|
dds_format = DDS_BGR5A1;
|
|
|
|
} else if (format_flags & DDPF_RGB && format_flags & DDPF_ALPHAPIXELS && format_rgb_bits == 32 && format_red_mask == 0x3ff00000 && format_green_mask == 0xffc00 && format_blue_mask == 0x3ff && format_alpha_mask == 0xc0000000) {
|
|
|
|
dds_format = DDS_BGR10A2;
|
|
|
|
} else if (format_flags & DDPF_RGB && !(format_flags & DDPF_ALPHAPIXELS) && format_rgb_bits == 16 && format_red_mask == 0x0000f800 && format_green_mask == 0x000007e0 && format_blue_mask == 0x0000001f) {
|
|
|
|
dds_format = DDS_BGR565;
|
|
|
|
} else if (!(format_flags & DDPF_ALPHAPIXELS) && format_rgb_bits == 8 && format_red_mask == 0xff && format_green_mask == 0xff && format_blue_mask == 0xff) {
|
|
|
|
dds_format = DDS_LUMINANCE;
|
|
|
|
} else if ((format_flags & DDPF_ALPHAPIXELS) && format_rgb_bits == 16 && format_red_mask == 0xff && format_green_mask == 0xff && format_blue_mask == 0xff && format_alpha_mask == 0xff00) {
|
|
|
|
dds_format = DDS_LUMINANCE_ALPHA;
|
|
|
|
} else if (format_flags & DDPF_INDEXED && format_rgb_bits == 8) {
|
|
|
|
dds_format = DDS_BGR565;
|
2014-02-10 01:10:30 +00:00
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
printf("unrecognized fourcc %x format_flags: %x - rgbbits %i - red_mask %x green mask %x blue mask %x alpha mask %x\n", format_fourcc, format_flags, format_rgb_bits, format_red_mask, format_green_mask, format_blue_mask, format_alpha_mask);
|
2019-09-25 08:28:50 +00:00
|
|
|
ERR_FAIL_V_MSG(RES(), "Unrecognized or unsupported color layout in DDS '" + p_path + "'.");
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!(flags & DDSD_MIPMAPCOUNT)) {
|
2017-03-05 15:44:50 +00:00
|
|
|
mipmaps = 1;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-02-17 21:06:54 +00:00
|
|
|
Vector<uint8_t> src_data;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
const DDSFormatInfo &info = dds_format_info[dds_format];
|
2014-02-10 01:10:30 +00:00
|
|
|
uint32_t w = width;
|
|
|
|
uint32_t h = height;
|
|
|
|
|
|
|
|
if (info.compressed) {
|
|
|
|
//compressed bc
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
uint32_t size = MAX(info.divisor, w) / info.divisor * MAX(info.divisor, h) / info.divisor * info.block_size;
|
|
|
|
ERR_FAIL_COND_V(size != pitch, RES());
|
|
|
|
ERR_FAIL_COND_V(!(flags & DDSD_LINEARSIZE), RES());
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (uint32_t i = 1; i < mipmaps; i++) {
|
|
|
|
w = MAX(1, w >> 1);
|
|
|
|
h = MAX(1, h >> 1);
|
|
|
|
uint32_t bsize = MAX(info.divisor, w) / info.divisor * MAX(info.divisor, h) / info.divisor * info.block_size;
|
2014-02-10 01:10:30 +00:00
|
|
|
//printf("%i x %i - block: %i\n",w,h,bsize);
|
2017-03-05 15:44:50 +00:00
|
|
|
size += bsize;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
src_data.resize(size);
|
2020-02-17 21:06:54 +00:00
|
|
|
uint8_t *wb = src_data.ptrw();
|
|
|
|
f->get_buffer(wb, size);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
} else if (info.palette) {
|
|
|
|
//indexed
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND_V(!(flags & DDSD_PITCH), RES());
|
|
|
|
ERR_FAIL_COND_V(format_rgb_bits != 8, RES());
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
uint32_t size = pitch * height;
|
|
|
|
ERR_FAIL_COND_V(size != width * height * info.block_size, RES());
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-24 20:45:31 +00:00
|
|
|
uint8_t palette[256 * 4];
|
|
|
|
f->get_buffer(palette, 256 * 4);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int colsize = 3;
|
|
|
|
for (int i = 0; i < 256; i++) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (palette[i * 4 + 3] < 255) {
|
2017-03-05 15:44:50 +00:00
|
|
|
colsize = 4;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2019-02-12 20:10:08 +00:00
|
|
|
int w2 = width;
|
|
|
|
int h2 = height;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (uint32_t i = 1; i < mipmaps; i++) {
|
2019-02-12 20:10:08 +00:00
|
|
|
w2 = (w2 + 1) >> 1;
|
|
|
|
h2 = (h2 + 1) >> 1;
|
|
|
|
size += w2 * h2 * info.block_size;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
src_data.resize(size + 256 * colsize);
|
2020-02-17 21:06:54 +00:00
|
|
|
uint8_t *wb = src_data.ptrw();
|
|
|
|
f->get_buffer(wb, size);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < 256; i++) {
|
|
|
|
int dst_ofs = size + i * colsize;
|
|
|
|
int src_ofs = i * 4;
|
2017-03-24 20:45:31 +00:00
|
|
|
wb[dst_ofs + 0] = palette[src_ofs + 2];
|
|
|
|
wb[dst_ofs + 1] = palette[src_ofs + 1];
|
|
|
|
wb[dst_ofs + 2] = palette[src_ofs + 0];
|
2020-05-14 14:41:43 +00:00
|
|
|
if (colsize == 4) {
|
2017-03-24 20:45:31 +00:00
|
|
|
wb[dst_ofs + 3] = palette[src_ofs + 3];
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
//uncompressed generic...
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
uint32_t size = width * height * info.block_size;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (uint32_t i = 1; i < mipmaps; i++) {
|
|
|
|
w = (w + 1) >> 1;
|
|
|
|
h = (h + 1) >> 1;
|
|
|
|
size += w * h * info.block_size;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (dds_format == DDS_BGR565) {
|
2017-03-05 15:44:50 +00:00
|
|
|
size = size * 3 / 2;
|
2020-05-14 14:41:43 +00:00
|
|
|
} else if (dds_format == DDS_BGR5A1) {
|
2017-03-05 15:44:50 +00:00
|
|
|
size = size * 2;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
src_data.resize(size);
|
2020-02-17 21:06:54 +00:00
|
|
|
uint8_t *wb = src_data.ptrw();
|
|
|
|
f->get_buffer(wb, size);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
switch (dds_format) {
|
2014-02-10 01:10:30 +00:00
|
|
|
case DDS_BGR5A1: {
|
|
|
|
// TO RGBA
|
2017-03-05 15:44:50 +00:00
|
|
|
int colcount = size / 4;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = colcount - 1; i >= 0; i--) {
|
|
|
|
int src_ofs = i * 2;
|
|
|
|
int dst_ofs = i * 4;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
uint8_t a = wb[src_ofs + 1] & 0x80;
|
|
|
|
uint8_t b = wb[src_ofs] & 0x1F;
|
|
|
|
uint8_t g = (wb[src_ofs] >> 5) | ((wb[src_ofs + 1] & 0x3) << 3);
|
|
|
|
uint8_t r = (wb[src_ofs + 1] >> 2) & 0x1F;
|
|
|
|
wb[dst_ofs + 0] = r << 3;
|
|
|
|
wb[dst_ofs + 1] = g << 3;
|
|
|
|
wb[dst_ofs + 2] = b << 3;
|
|
|
|
wb[dst_ofs + 3] = a ? 255 : 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case DDS_BGR565: {
|
2017-03-05 15:44:50 +00:00
|
|
|
int colcount = size / 3;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = colcount - 1; i >= 0; i--) {
|
|
|
|
int src_ofs = i * 2;
|
|
|
|
int dst_ofs = i * 3;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
uint8_t b = wb[src_ofs] & 0x1F;
|
|
|
|
uint8_t g = (wb[src_ofs] >> 5) | ((wb[src_ofs + 1] & 0x7) << 3);
|
|
|
|
uint8_t r = wb[src_ofs + 1] >> 3;
|
|
|
|
wb[dst_ofs + 0] = r << 3;
|
|
|
|
wb[dst_ofs + 1] = g << 2;
|
|
|
|
wb[dst_ofs + 2] = b << 3; //b<<3;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} break;
|
|
|
|
case DDS_BGR10A2: {
|
|
|
|
// TO RGBA
|
2017-03-05 15:44:50 +00:00
|
|
|
int colcount = size / 4;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = colcount - 1; i >= 0; i--) {
|
|
|
|
int ofs = i * 4;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
uint32_t w32 = uint32_t(wb[ofs + 0]) | (uint32_t(wb[ofs + 1]) << 8) | (uint32_t(wb[ofs + 2]) << 16) | (uint32_t(wb[ofs + 3]) << 24);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
uint8_t a = (w32 & 0xc0000000) >> 24;
|
|
|
|
uint8_t r = (w32 & 0x3ff00000) >> 22;
|
|
|
|
uint8_t g = (w32 & 0xffc00) >> 12;
|
|
|
|
uint8_t b = (w32 & 0x3ff) >> 2;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
wb[ofs + 0] = r;
|
|
|
|
wb[ofs + 1] = g;
|
|
|
|
wb[ofs + 2] = b;
|
|
|
|
wb[ofs + 3] = a == 0xc0 ? 255 : a; //0xc0 should be opaque
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case DDS_BGRA8: {
|
2017-03-05 15:44:50 +00:00
|
|
|
int colcount = size / 4;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < colcount; i++) {
|
|
|
|
SWAP(wb[i * 4 + 0], wb[i * 4 + 2]);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} break;
|
|
|
|
case DDS_BGR8: {
|
2017-03-05 15:44:50 +00:00
|
|
|
int colcount = size / 3;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < colcount; i++) {
|
|
|
|
SWAP(wb[i * 3 + 0], wb[i * 3 + 2]);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case DDS_RGBA8: {
|
2017-03-05 15:44:50 +00:00
|
|
|
/* do nothing either
|
2014-02-10 01:10:30 +00:00
|
|
|
int colcount = size/4;
|
|
|
|
|
|
|
|
for(int i=0;i<colcount;i++) {
|
|
|
|
uint8_t r = wb[i*4+1];
|
|
|
|
uint8_t g = wb[i*4+2];
|
|
|
|
uint8_t b = wb[i*4+3];
|
|
|
|
uint8_t a = wb[i*4+0];
|
|
|
|
|
|
|
|
wb[i*4+0]=r;
|
|
|
|
wb[i*4+1]=g;
|
|
|
|
wb[i*4+2]=b;
|
|
|
|
wb[i*4+3]=a;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
} break;
|
|
|
|
case DDS_RGB8: {
|
|
|
|
// do nothing
|
|
|
|
/*
|
|
|
|
int colcount = size/3;
|
|
|
|
|
|
|
|
for(int i=0;i<colcount;i++) {
|
|
|
|
SWAP( wb[i*3+0],wb[i*3+2] );
|
|
|
|
}*/
|
|
|
|
} break;
|
|
|
|
case DDS_LUMINANCE: {
|
|
|
|
// do nothing i guess?
|
|
|
|
|
|
|
|
} break;
|
|
|
|
case DDS_LUMINANCE_ALPHA: {
|
|
|
|
// do nothing i guess?
|
|
|
|
|
|
|
|
} break;
|
|
|
|
|
2019-04-09 15:08:36 +00:00
|
|
|
default: {
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-17 10:36:47 +00:00
|
|
|
Ref<Image> img = memnew(Image(width, height, mipmaps - 1, info.format, src_data));
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Ref<ImageTexture> texture = memnew(ImageTexture);
|
2014-02-10 01:10:30 +00:00
|
|
|
texture->create_from_image(img);
|
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (r_error) {
|
2017-03-05 15:44:50 +00:00
|
|
|
*r_error = OK;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2015-08-23 23:15:56 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
return texture;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceFormatDDS::get_recognized_extensions(List<String> *p_extensions) const {
|
|
|
|
p_extensions->push_back("dds");
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool ResourceFormatDDS::handles_type(const String &p_type) const {
|
2019-06-11 18:43:37 +00:00
|
|
|
return ClassDB::is_parent_class(p_type, "Texture2D");
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
String ResourceFormatDDS::get_resource_type(const String &p_path) const {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (p_path.get_extension().to_lower() == "dds") {
|
2014-02-10 01:10:30 +00:00
|
|
|
return "ImageTexture";
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
return "";
|
|
|
|
}
|