mirror of https://github.com/godotengine/godot
Implement loading DDS textures at run-time
Co-authored-by: Marcin Nowak <marcin.j.nowak@gmail.com>
This commit is contained in:
parent
74ce201cc4
commit
904f3a834b
|
|
@ -105,6 +105,7 @@ ImageMemLoadFunc Image::_tga_mem_loader_func = nullptr;
|
|||
ImageMemLoadFunc Image::_bmp_mem_loader_func = nullptr;
|
||||
ScalableImageMemLoadFunc Image::_svg_scalable_mem_loader_func = nullptr;
|
||||
ImageMemLoadFunc Image::_ktx_mem_loader_func = nullptr;
|
||||
ImageMemLoadFunc Image::_dds_mem_loader_func = nullptr;
|
||||
|
||||
// External VRAM compression function pointers.
|
||||
|
||||
|
|
@ -3577,6 +3578,7 @@ void Image::_bind_methods() {
|
|||
ClassDB::bind_method(D_METHOD("load_tga_from_buffer", "buffer"), &Image::load_tga_from_buffer);
|
||||
ClassDB::bind_method(D_METHOD("load_bmp_from_buffer", "buffer"), &Image::load_bmp_from_buffer);
|
||||
ClassDB::bind_method(D_METHOD("load_ktx_from_buffer", "buffer"), &Image::load_ktx_from_buffer);
|
||||
ClassDB::bind_method(D_METHOD("load_dds_from_buffer", "buffer"), &Image::load_dds_from_buffer);
|
||||
|
||||
ClassDB::bind_method(D_METHOD("load_svg_from_buffer", "buffer", "scale"), &Image::load_svg_from_buffer, DEFVAL(1.0));
|
||||
ClassDB::bind_method(D_METHOD("load_svg_from_string", "svg_str", "scale"), &Image::load_svg_from_string, DEFVAL(1.0));
|
||||
|
|
@ -4102,6 +4104,14 @@ Error Image::load_ktx_from_buffer(const Vector<uint8_t> &p_array) {
|
|||
return _load_from_buffer(p_array, _ktx_mem_loader_func);
|
||||
}
|
||||
|
||||
Error Image::load_dds_from_buffer(const Vector<uint8_t> &p_array) {
|
||||
ERR_FAIL_NULL_V_MSG(
|
||||
_dds_mem_loader_func,
|
||||
ERR_UNAVAILABLE,
|
||||
"The DDS module isn't enabled. Recompile the Redot editor or export template binary with the `module_dds_enabled=yes` SCons option.");
|
||||
return _load_from_buffer(p_array, _dds_mem_loader_func);
|
||||
}
|
||||
|
||||
void Image::convert_rg_to_ra_rgba8() {
|
||||
ERR_FAIL_COND(format != FORMAT_RGBA8);
|
||||
ERR_FAIL_COND(data.is_empty());
|
||||
|
|
|
|||
|
|
@ -201,6 +201,7 @@ public:
|
|||
static ImageMemLoadFunc _bmp_mem_loader_func;
|
||||
static ScalableImageMemLoadFunc _svg_scalable_mem_loader_func;
|
||||
static ImageMemLoadFunc _ktx_mem_loader_func;
|
||||
static ImageMemLoadFunc _dds_mem_loader_func;
|
||||
|
||||
// External VRAM compression function pointers.
|
||||
|
||||
|
|
@ -403,6 +404,7 @@ public:
|
|||
Error load_tga_from_buffer(const Vector<uint8_t> &p_array);
|
||||
Error load_bmp_from_buffer(const Vector<uint8_t> &p_array);
|
||||
Error load_ktx_from_buffer(const Vector<uint8_t> &p_array);
|
||||
Error load_dds_from_buffer(const Vector<uint8_t> &p_array);
|
||||
|
||||
Error load_svg_from_buffer(const Vector<uint8_t> &p_array, float scale = 1.0);
|
||||
Error load_svg_from_string(const String &p_svg_str, float scale = 1.0);
|
||||
|
|
|
|||
|
|
@ -346,6 +346,13 @@
|
|||
[b]Note:[/b] This method is only available in engine builds with the BMP module enabled. By default, the BMP module is enabled, but it can be disabled at build-time using the [code]module_bmp_enabled=no[/code] SCons option.
|
||||
</description>
|
||||
</method>
|
||||
<method name="load_dds_from_buffer">
|
||||
<return type="int" enum="Error" />
|
||||
<param index="0" name="buffer" type="PackedByteArray" />
|
||||
<description>
|
||||
Loads an image from the binary contents of a DDS file.
|
||||
</description>
|
||||
</method>
|
||||
<method name="load_from_file" qualifiers="static">
|
||||
<return type="Image" />
|
||||
<param index="0" name="path" type="String" />
|
||||
|
|
|
|||
|
|
@ -0,0 +1,757 @@
|
|||
/**************************************************************************/
|
||||
/* image_loader_dds.cpp */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* 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. */
|
||||
/**************************************************************************/
|
||||
|
||||
#include "image_loader_dds.h"
|
||||
|
||||
#include "core/error/error_list.h"
|
||||
#include "core/io/file_access.h"
|
||||
#include "core/io/file_access_memory.h"
|
||||
#include "core/io/image.h"
|
||||
|
||||
#define PF_FOURCC(s) ((uint32_t)(((s)[3] << 24U) | ((s)[2] << 16U) | ((s)[1] << 8U) | ((s)[0])))
|
||||
|
||||
// Reference: https://docs.microsoft.com/en-us/windows/win32/direct3ddds/dds-header
|
||||
|
||||
enum {
|
||||
DDS_MAGIC = 0x20534444,
|
||||
DDSD_PITCH = 0x00000008,
|
||||
DDSD_LINEARSIZE = 0x00080000,
|
||||
DDSD_MIPMAPCOUNT = 0x00020000,
|
||||
DDPF_ALPHAPIXELS = 0x00000001,
|
||||
DDPF_ALPHAONLY = 0x00000002,
|
||||
DDPF_FOURCC = 0x00000004,
|
||||
DDPF_RGB = 0x00000040,
|
||||
DDPF_RG_SNORM = 0x00080000,
|
||||
DDSC2_CUBEMAP = 0x200,
|
||||
DDSC2_VOLUME = 0x200000,
|
||||
DX10D_1D = 2,
|
||||
DX10D_2D = 3,
|
||||
DX10D_3D = 4,
|
||||
};
|
||||
|
||||
enum DDSFourCC {
|
||||
DDFCC_DXT1 = PF_FOURCC("DXT1"),
|
||||
DDFCC_DXT2 = PF_FOURCC("DXT2"),
|
||||
DDFCC_DXT3 = PF_FOURCC("DXT3"),
|
||||
DDFCC_DXT4 = PF_FOURCC("DXT4"),
|
||||
DDFCC_DXT5 = PF_FOURCC("DXT5"),
|
||||
DDFCC_ATI1 = PF_FOURCC("ATI1"),
|
||||
DDFCC_BC4U = PF_FOURCC("BC4U"),
|
||||
DDFCC_ATI2 = PF_FOURCC("ATI2"),
|
||||
DDFCC_BC5U = PF_FOURCC("BC5U"),
|
||||
DDFCC_A2XY = PF_FOURCC("A2XY"),
|
||||
DDFCC_DX10 = PF_FOURCC("DX10"),
|
||||
DDFCC_R16F = 111,
|
||||
DDFCC_RG16F = 112,
|
||||
DDFCC_RGBA16F = 113,
|
||||
DDFCC_R32F = 114,
|
||||
DDFCC_RG32F = 115,
|
||||
DDFCC_RGBA32F = 116
|
||||
};
|
||||
|
||||
// Reference: https://learn.microsoft.com/en-us/windows/win32/api/dxgiformat/ne-dxgiformat-dxgi_format
|
||||
enum DXGIFormat {
|
||||
DXGI_R32G32B32A32_FLOAT = 2,
|
||||
DXGI_R32G32B32_FLOAT = 6,
|
||||
DXGI_R16G16B16A16_FLOAT = 10,
|
||||
DXGI_R32G32_FLOAT = 16,
|
||||
DXGI_R10G10B10A2_UNORM = 24,
|
||||
DXGI_R8G8B8A8_UNORM = 28,
|
||||
DXGI_R8G8B8A8_UNORM_SRGB = 29,
|
||||
DXGI_R16G16_FLOAT = 34,
|
||||
DXGI_R32_FLOAT = 41,
|
||||
DXGI_R8G8_UNORM = 49,
|
||||
DXGI_R16_FLOAT = 54,
|
||||
DXGI_R8_UNORM = 61,
|
||||
DXGI_A8_UNORM = 65,
|
||||
DXGI_R9G9B9E5 = 67,
|
||||
DXGI_BC1_UNORM = 71,
|
||||
DXGI_BC1_UNORM_SRGB = 72,
|
||||
DXGI_BC2_UNORM = 74,
|
||||
DXGI_BC2_UNORM_SRGB = 75,
|
||||
DXGI_BC3_UNORM = 77,
|
||||
DXGI_BC3_UNORM_SRGB = 78,
|
||||
DXGI_BC4_UNORM = 80,
|
||||
DXGI_BC5_UNORM = 83,
|
||||
DXGI_B5G6R5_UNORM = 85,
|
||||
DXGI_B5G5R5A1_UNORM = 86,
|
||||
DXGI_B8G8R8A8_UNORM = 87,
|
||||
DXGI_BC6H_UF16 = 95,
|
||||
DXGI_BC6H_SF16 = 96,
|
||||
DXGI_BC7_UNORM = 98,
|
||||
DXGI_BC7_UNORM_SRGB = 99,
|
||||
DXGI_B4G4R4A4_UNORM = 115
|
||||
};
|
||||
|
||||
struct DDSFormatInfo {
|
||||
const char *name = nullptr;
|
||||
bool compressed = false;
|
||||
uint32_t divisor = 0;
|
||||
uint32_t block_size = 0;
|
||||
Image::Format format = Image::Format::FORMAT_BPTC_RGBA;
|
||||
};
|
||||
|
||||
static const DDSFormatInfo dds_format_info[ImageLoaderDDS::DDS_MAX] = {
|
||||
{ "DXT1/BC1", true, 4, 8, Image::FORMAT_DXT1 },
|
||||
{ "DXT2/DXT3/BC2", true, 4, 16, Image::FORMAT_DXT3 },
|
||||
{ "DXT4/DXT5/BC3", true, 4, 16, Image::FORMAT_DXT5 },
|
||||
{ "ATI1/BC4", true, 4, 8, Image::FORMAT_RGTC_R },
|
||||
{ "ATI2/A2XY/BC5", true, 4, 16, Image::FORMAT_RGTC_RG },
|
||||
{ "BC6UF", true, 4, 16, Image::FORMAT_BPTC_RGBFU },
|
||||
{ "BC6SF", true, 4, 16, Image::FORMAT_BPTC_RGBF },
|
||||
{ "BC7", true, 4, 16, Image::FORMAT_BPTC_RGBA },
|
||||
{ "R16F", false, 1, 2, Image::FORMAT_RH },
|
||||
{ "RG16F", false, 1, 4, Image::FORMAT_RGH },
|
||||
{ "RGBA16F", false, 1, 8, Image::FORMAT_RGBAH },
|
||||
{ "R32F", false, 1, 4, Image::FORMAT_RF },
|
||||
{ "RG32F", false, 1, 8, Image::FORMAT_RGF },
|
||||
{ "RGB32F", false, 1, 12, Image::FORMAT_RGBF },
|
||||
{ "RGBA32F", false, 1, 16, Image::FORMAT_RGBAF },
|
||||
{ "RGB9E5", false, 1, 4, Image::FORMAT_RGBE9995 },
|
||||
{ "RGB8", false, 1, 3, Image::FORMAT_RGB8 },
|
||||
{ "RGBA8", false, 1, 4, Image::FORMAT_RGBA8 },
|
||||
{ "BGR8", false, 1, 3, Image::FORMAT_RGB8 },
|
||||
{ "BGRA8", false, 1, 4, Image::FORMAT_RGBA8 },
|
||||
{ "BGR5A1", false, 1, 2, Image::FORMAT_RGBA8 },
|
||||
{ "BGR565", false, 1, 2, Image::FORMAT_RGB8 },
|
||||
{ "B2GR3", false, 1, 1, Image::FORMAT_RGB8 },
|
||||
{ "B2GR3A8", false, 1, 2, Image::FORMAT_RGBA8 },
|
||||
{ "BGR10A2", false, 1, 4, Image::FORMAT_RGBA8 },
|
||||
{ "RGB10A2", false, 1, 4, Image::FORMAT_RGBA8 },
|
||||
{ "BGRA4", false, 1, 2, Image::FORMAT_RGBA8 },
|
||||
{ "GRAYSCALE", false, 1, 1, Image::FORMAT_L8 },
|
||||
{ "GRAYSCALE_ALPHA", false, 1, 2, Image::FORMAT_LA8 },
|
||||
{ "GRAYSCALE_ALPHA_4", false, 1, 1, Image::FORMAT_LA8 }
|
||||
};
|
||||
|
||||
inline ImageLoaderDDS::DDSFormat _dxgi_to_dds_format(uint32_t p_dxgi_format) {
|
||||
switch (p_dxgi_format) {
|
||||
case DXGI_R32G32B32A32_FLOAT: {
|
||||
return ImageLoaderDDS::DDS_RGBA32F;
|
||||
}
|
||||
case DXGI_R32G32B32_FLOAT: {
|
||||
return ImageLoaderDDS::DDS_RGB32F;
|
||||
}
|
||||
case DXGI_R16G16B16A16_FLOAT: {
|
||||
return ImageLoaderDDS::DDS_RGBA16F;
|
||||
}
|
||||
case DXGI_R32G32_FLOAT: {
|
||||
return ImageLoaderDDS::DDS_RG32F;
|
||||
}
|
||||
case DXGI_R10G10B10A2_UNORM: {
|
||||
return ImageLoaderDDS::DDS_RGB10A2;
|
||||
}
|
||||
case DXGI_R8G8B8A8_UNORM:
|
||||
case DXGI_R8G8B8A8_UNORM_SRGB: {
|
||||
return ImageLoaderDDS::DDS_RGBA8;
|
||||
}
|
||||
case DXGI_R16G16_FLOAT: {
|
||||
return ImageLoaderDDS::DDS_RG16F;
|
||||
}
|
||||
case DXGI_R32_FLOAT: {
|
||||
return ImageLoaderDDS::DDS_R32F;
|
||||
}
|
||||
case DXGI_R8_UNORM:
|
||||
case DXGI_A8_UNORM: {
|
||||
return ImageLoaderDDS::DDS_LUMINANCE;
|
||||
}
|
||||
case DXGI_R16_FLOAT: {
|
||||
return ImageLoaderDDS::DDS_R16F;
|
||||
}
|
||||
case DXGI_R8G8_UNORM: {
|
||||
return ImageLoaderDDS::DDS_LUMINANCE_ALPHA;
|
||||
}
|
||||
case DXGI_R9G9B9E5: {
|
||||
return ImageLoaderDDS::DDS_RGB9E5;
|
||||
}
|
||||
case DXGI_BC1_UNORM:
|
||||
case DXGI_BC1_UNORM_SRGB: {
|
||||
return ImageLoaderDDS::DDS_DXT1;
|
||||
}
|
||||
case DXGI_BC2_UNORM:
|
||||
case DXGI_BC2_UNORM_SRGB: {
|
||||
return ImageLoaderDDS::DDS_DXT3;
|
||||
}
|
||||
case DXGI_BC3_UNORM:
|
||||
case DXGI_BC3_UNORM_SRGB: {
|
||||
return ImageLoaderDDS::DDS_DXT5;
|
||||
}
|
||||
case DXGI_BC4_UNORM: {
|
||||
return ImageLoaderDDS::DDS_ATI1;
|
||||
}
|
||||
case DXGI_BC5_UNORM: {
|
||||
return ImageLoaderDDS::DDS_ATI2;
|
||||
}
|
||||
case DXGI_B5G6R5_UNORM: {
|
||||
return ImageLoaderDDS::DDS_BGR565;
|
||||
}
|
||||
case DXGI_B5G5R5A1_UNORM: {
|
||||
return ImageLoaderDDS::DDS_BGR5A1;
|
||||
}
|
||||
case DXGI_B8G8R8A8_UNORM: {
|
||||
return ImageLoaderDDS::DDS_BGRA8;
|
||||
}
|
||||
case DXGI_BC6H_UF16: {
|
||||
return ImageLoaderDDS::DDS_BC6U;
|
||||
}
|
||||
case DXGI_BC6H_SF16: {
|
||||
return ImageLoaderDDS::DDS_BC6S;
|
||||
}
|
||||
case DXGI_BC7_UNORM:
|
||||
case DXGI_BC7_UNORM_SRGB: {
|
||||
return ImageLoaderDDS::DDS_BC7;
|
||||
}
|
||||
case DXGI_B4G4R4A4_UNORM: {
|
||||
return ImageLoaderDDS::DDS_BGRA4;
|
||||
}
|
||||
|
||||
default: {
|
||||
return ImageLoaderDDS::DDS_MAX;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static Ref<Image> _dds_mem_loader_func(const uint8_t *p_buffer, int p_buffer_len) {
|
||||
Ref<FileAccessMemory> memfile;
|
||||
memfile.instantiate();
|
||||
Error open_memfile_error = memfile->open_custom(p_buffer, p_buffer_len);
|
||||
ERR_FAIL_COND_V_MSG(open_memfile_error, Ref<Image>(), "Could not create memfile for DDS image buffer.");
|
||||
|
||||
Ref<Image> img;
|
||||
img.instantiate();
|
||||
Error load_error = ImageLoaderDDS().load_image(img, memfile, false, 1.0f);
|
||||
ERR_FAIL_COND_V_MSG(load_error, Ref<Image>(), "Failed to load DDS image.");
|
||||
return img;
|
||||
}
|
||||
|
||||
Error ImageLoaderDDS::load_layer(Ref<Image> p_layer, Ref<FileAccess> p_file, ImageLoaderDDS::DDSFormat p_dds_format, uint32_t p_width, uint32_t p_height, uint32_t p_mipmaps, uint32_t p_pitch, uint32_t p_flags, Vector<uint8_t> &r_src_data) {
|
||||
const DDSFormatInfo &info = dds_format_info[p_dds_format];
|
||||
|
||||
uint32_t w = p_width;
|
||||
uint32_t h = p_height;
|
||||
|
||||
if (info.compressed) {
|
||||
// BC compressed.
|
||||
w += w % info.divisor;
|
||||
h += h % info.divisor;
|
||||
if (w != p_width) {
|
||||
WARN_PRINT(vformat("%s: DDS width '%d' is not divisible by %d. This is not allowed as per the DDS specification, attempting to load anyway.", p_file->get_path(), p_width, info.divisor));
|
||||
}
|
||||
if (h != p_height) {
|
||||
WARN_PRINT(vformat("%s: DDS height '%d' is not divisible by %d. This is not allowed as per the DDS specification, attempting to load anyway.", p_file->get_path(), p_height, info.divisor));
|
||||
}
|
||||
|
||||
uint32_t size = MAX(info.divisor, w) / info.divisor * MAX(info.divisor, h) / info.divisor * info.block_size;
|
||||
|
||||
if (p_flags & DDSD_LINEARSIZE) {
|
||||
ERR_FAIL_COND_V_MSG(size != p_pitch, ERR_FILE_CORRUPT, "DDS header flags specify that a linear size of the top-level image is present, but the specified size does not match the expected value.");
|
||||
} else {
|
||||
ERR_FAIL_COND_V_MSG(p_pitch != 0, ERR_FILE_CORRUPT, "DDS header flags specify that no linear size will given for the top-level image, but a non-zero linear size value is present in the header.");
|
||||
}
|
||||
|
||||
for (uint32_t i = 1; i < p_mipmaps; i++) {
|
||||
w = MAX(1u, w >> 1);
|
||||
h = MAX(1u, h >> 1);
|
||||
|
||||
uint32_t bsize = MAX(info.divisor, w) / info.divisor * MAX(info.divisor, h) / info.divisor * info.block_size;
|
||||
size += bsize;
|
||||
}
|
||||
|
||||
r_src_data.resize(size);
|
||||
uint8_t *wb = r_src_data.ptrw();
|
||||
p_file->get_buffer(wb, size);
|
||||
|
||||
} else {
|
||||
// Generic uncompressed.
|
||||
uint32_t size = p_width * p_height * info.block_size;
|
||||
|
||||
for (uint32_t i = 1; i < p_mipmaps; i++) {
|
||||
w = (w + 1) >> 1;
|
||||
h = (h + 1) >> 1;
|
||||
size += w * h * info.block_size;
|
||||
}
|
||||
|
||||
// Calculate the space these formats will take up after decoding.
|
||||
switch (p_dds_format) {
|
||||
case DDS_BGR565:
|
||||
size = size * 3 / 2;
|
||||
break;
|
||||
|
||||
case DDS_BGR5A1:
|
||||
case DDS_BGRA4:
|
||||
case DDS_B2GR3A8:
|
||||
case DDS_LUMINANCE_ALPHA_4:
|
||||
size = size * 2;
|
||||
break;
|
||||
|
||||
case DDS_B2GR3:
|
||||
size = size * 3;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
r_src_data.resize(size);
|
||||
uint8_t *wb = r_src_data.ptrw();
|
||||
p_file->get_buffer(wb, size);
|
||||
|
||||
switch (p_dds_format) {
|
||||
case DDS_BGR5A1: {
|
||||
// To RGBA8.
|
||||
int colcount = size / 4;
|
||||
|
||||
for (int i = colcount - 1; i >= 0; i--) {
|
||||
int src_ofs = i * 2;
|
||||
int dst_ofs = i * 4;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
} break;
|
||||
case DDS_BGR565: {
|
||||
// To RGB8.
|
||||
int colcount = size / 3;
|
||||
|
||||
for (int i = colcount - 1; i >= 0; i--) {
|
||||
int src_ofs = i * 2;
|
||||
int dst_ofs = i * 3;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
} break;
|
||||
case DDS_BGRA4: {
|
||||
// To RGBA8.
|
||||
int colcount = size / 4;
|
||||
|
||||
for (int i = colcount - 1; i >= 0; i--) {
|
||||
int src_ofs = i * 2;
|
||||
int dst_ofs = i * 4;
|
||||
|
||||
uint8_t b = wb[src_ofs] & 0x0F;
|
||||
uint8_t g = wb[src_ofs] & 0xF0;
|
||||
uint8_t r = wb[src_ofs + 1] & 0x0F;
|
||||
uint8_t a = wb[src_ofs + 1] & 0xF0;
|
||||
|
||||
wb[dst_ofs] = (r << 4) | r;
|
||||
wb[dst_ofs + 1] = g | (g >> 4);
|
||||
wb[dst_ofs + 2] = (b << 4) | b;
|
||||
wb[dst_ofs + 3] = a | (a >> 4);
|
||||
}
|
||||
|
||||
} break;
|
||||
case DDS_B2GR3: {
|
||||
// To RGB8.
|
||||
int colcount = size / 3;
|
||||
|
||||
for (int i = colcount - 1; i >= 0; i--) {
|
||||
int src_ofs = i;
|
||||
int dst_ofs = i * 3;
|
||||
|
||||
uint8_t b = (wb[src_ofs] & 0x3) << 6;
|
||||
uint8_t g = (wb[src_ofs] & 0x1C) << 3;
|
||||
uint8_t r = (wb[src_ofs] & 0xE0);
|
||||
|
||||
wb[dst_ofs] = r;
|
||||
wb[dst_ofs + 1] = g;
|
||||
wb[dst_ofs + 2] = b;
|
||||
}
|
||||
|
||||
} break;
|
||||
case DDS_B2GR3A8: {
|
||||
// To RGBA8.
|
||||
int colcount = size / 4;
|
||||
|
||||
for (int i = colcount - 1; i >= 0; i--) {
|
||||
int src_ofs = i * 2;
|
||||
int dst_ofs = i * 4;
|
||||
|
||||
uint8_t b = (wb[src_ofs] & 0x3) << 6;
|
||||
uint8_t g = (wb[src_ofs] & 0x1C) << 3;
|
||||
uint8_t r = (wb[src_ofs] & 0xE0);
|
||||
uint8_t a = wb[src_ofs + 1];
|
||||
|
||||
wb[dst_ofs] = r;
|
||||
wb[dst_ofs + 1] = g;
|
||||
wb[dst_ofs + 2] = b;
|
||||
wb[dst_ofs + 3] = a;
|
||||
}
|
||||
|
||||
} break;
|
||||
case DDS_RGB10A2: {
|
||||
// To RGBA8.
|
||||
int colcount = size / 4;
|
||||
|
||||
for (int i = 0; i < colcount; i++) {
|
||||
int ofs = i * 4;
|
||||
|
||||
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);
|
||||
|
||||
// This method follows the 'standard' way of decoding 10-bit dds files,
|
||||
// which means the ones created with DirectXTex will be loaded incorrectly.
|
||||
uint8_t a = (w32 & 0xc0000000) >> 24;
|
||||
uint8_t r = (w32 & 0x3ff) >> 2;
|
||||
uint8_t g = (w32 & 0xffc00) >> 12;
|
||||
uint8_t b = (w32 & 0x3ff00000) >> 22;
|
||||
|
||||
wb[ofs + 0] = r;
|
||||
wb[ofs + 1] = g;
|
||||
wb[ofs + 2] = b;
|
||||
wb[ofs + 3] = a == 0xc0 ? 255 : a; // 0xc0 should be opaque.
|
||||
}
|
||||
|
||||
} break;
|
||||
case DDS_BGR10A2: {
|
||||
// To RGBA8.
|
||||
int colcount = size / 4;
|
||||
|
||||
for (int i = 0; i < colcount; i++) {
|
||||
int ofs = i * 4;
|
||||
|
||||
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);
|
||||
|
||||
// This method follows the 'standard' way of decoding 10-bit dds files,
|
||||
// which means the ones created with DirectXTex will be loaded incorrectly.
|
||||
uint8_t a = (w32 & 0xc0000000) >> 24;
|
||||
uint8_t r = (w32 & 0x3ff00000) >> 22;
|
||||
uint8_t g = (w32 & 0xffc00) >> 12;
|
||||
uint8_t b = (w32 & 0x3ff) >> 2;
|
||||
|
||||
wb[ofs + 0] = r;
|
||||
wb[ofs + 1] = g;
|
||||
wb[ofs + 2] = b;
|
||||
wb[ofs + 3] = a == 0xc0 ? 255 : a; // 0xc0 should be opaque.
|
||||
}
|
||||
|
||||
} break;
|
||||
|
||||
// Channel-swapped.
|
||||
case DDS_BGRA8: {
|
||||
// To RGBA8.
|
||||
int colcount = size / 4;
|
||||
|
||||
for (int i = 0; i < colcount; i++) {
|
||||
SWAP(wb[i * 4 + 0], wb[i * 4 + 2]);
|
||||
}
|
||||
|
||||
} break;
|
||||
case DDS_BGR8: {
|
||||
// To RGB8.
|
||||
int colcount = size / 3;
|
||||
|
||||
for (int i = 0; i < colcount; i++) {
|
||||
SWAP(wb[i * 3 + 0], wb[i * 3 + 2]);
|
||||
}
|
||||
|
||||
} break;
|
||||
|
||||
// Grayscale.
|
||||
case DDS_LUMINANCE_ALPHA_4: {
|
||||
// To LA8.
|
||||
int colcount = size / 2;
|
||||
|
||||
for (int i = colcount - 1; i >= 0; i--) {
|
||||
int src_ofs = i;
|
||||
int dst_ofs = i * 2;
|
||||
|
||||
uint8_t l = wb[src_ofs] & 0x0F;
|
||||
uint8_t a = wb[src_ofs] & 0xF0;
|
||||
|
||||
wb[dst_ofs] = (l << 4) | l;
|
||||
wb[dst_ofs + 1] = a | (a >> 4);
|
||||
}
|
||||
|
||||
} break;
|
||||
|
||||
default: {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
p_layer = memnew(Image(p_width, p_height, p_mipmaps > 1, info.format, r_src_data));
|
||||
return OK;
|
||||
}
|
||||
|
||||
Error ImageLoaderDDS::load_image_layers(Vector<Ref<Image>> &p_layers, Ref<FileAccess> p_file, uint32_t *r_dds_type) {
|
||||
uint32_t magic = p_file->get_32();
|
||||
uint32_t hsize = p_file->get_32();
|
||||
uint32_t flags = p_file->get_32();
|
||||
uint32_t height = p_file->get_32();
|
||||
uint32_t width = p_file->get_32();
|
||||
uint32_t pitch = p_file->get_32();
|
||||
uint32_t depth = p_file->get_32();
|
||||
uint32_t mipmaps = p_file->get_32();
|
||||
|
||||
// Skip reserved.
|
||||
for (int i = 0; i < 11; i++) {
|
||||
p_file->get_32();
|
||||
}
|
||||
|
||||
// Validate.
|
||||
// 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) {
|
||||
ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, vformat("Invalid or unsupported DDS texture file '%s'.", p_file->get_path()));
|
||||
}
|
||||
|
||||
/* uint32_t format_size = */ p_file->get_32();
|
||||
uint32_t format_flags = p_file->get_32();
|
||||
uint32_t format_fourcc = p_file->get_32();
|
||||
uint32_t format_rgb_bits = p_file->get_32();
|
||||
uint32_t format_red_mask = p_file->get_32();
|
||||
uint32_t format_green_mask = p_file->get_32();
|
||||
uint32_t format_blue_mask = p_file->get_32();
|
||||
uint32_t format_alpha_mask = p_file->get_32();
|
||||
|
||||
/* uint32_t caps_1 = */ p_file->get_32();
|
||||
uint32_t caps_2 = p_file->get_32();
|
||||
/* uint32_t caps_3 = */ p_file->get_32();
|
||||
/* uint32_t caps_4 = */ p_file->get_32();
|
||||
|
||||
// Skip reserved.
|
||||
p_file->get_32();
|
||||
|
||||
if (p_file->get_position() < 128) {
|
||||
p_file->seek(128);
|
||||
}
|
||||
|
||||
uint32_t layer_count = 1;
|
||||
if (r_dds_type) {
|
||||
*r_dds_type = DDST_2D;
|
||||
}
|
||||
|
||||
if (caps_2 & DDSC2_CUBEMAP) {
|
||||
if (r_dds_type) {
|
||||
*r_dds_type = DDST_CUBEMAP;
|
||||
}
|
||||
layer_count *= 6;
|
||||
|
||||
} else if (caps_2 & DDSC2_VOLUME) {
|
||||
if (r_dds_type) {
|
||||
*r_dds_type = DDST_3D;
|
||||
}
|
||||
layer_count = depth;
|
||||
}
|
||||
|
||||
DDSFormat dds_format = DDS_MAX;
|
||||
|
||||
if (format_flags & DDPF_FOURCC) {
|
||||
// FourCC formats.
|
||||
switch (format_fourcc) {
|
||||
case DDFCC_DXT1: {
|
||||
dds_format = DDS_DXT1;
|
||||
} break;
|
||||
case DDFCC_DXT2:
|
||||
case DDFCC_DXT3: {
|
||||
dds_format = DDS_DXT3;
|
||||
} break;
|
||||
case DDFCC_DXT4:
|
||||
case DDFCC_DXT5: {
|
||||
dds_format = DDS_DXT5;
|
||||
} break;
|
||||
case DDFCC_ATI1:
|
||||
case DDFCC_BC4U: {
|
||||
dds_format = DDS_ATI1;
|
||||
} break;
|
||||
case DDFCC_ATI2:
|
||||
case DDFCC_BC5U:
|
||||
case DDFCC_A2XY: {
|
||||
dds_format = DDS_ATI2;
|
||||
} break;
|
||||
case DDFCC_R16F: {
|
||||
dds_format = DDS_R16F;
|
||||
} break;
|
||||
case DDFCC_RG16F: {
|
||||
dds_format = DDS_RG16F;
|
||||
} break;
|
||||
case DDFCC_RGBA16F: {
|
||||
dds_format = DDS_RGBA16F;
|
||||
} break;
|
||||
case DDFCC_R32F: {
|
||||
dds_format = DDS_R32F;
|
||||
} break;
|
||||
case DDFCC_RG32F: {
|
||||
dds_format = DDS_RG32F;
|
||||
} break;
|
||||
case DDFCC_RGBA32F: {
|
||||
dds_format = DDS_RGBA32F;
|
||||
} break;
|
||||
case DDFCC_DX10: {
|
||||
uint32_t dxgi_format = p_file->get_32();
|
||||
uint32_t dimension = p_file->get_32();
|
||||
/* uint32_t misc_flags_1 = */ p_file->get_32();
|
||||
uint32_t array_size = p_file->get_32();
|
||||
/* uint32_t misc_flags_2 = */ p_file->get_32();
|
||||
|
||||
if (dimension == DX10D_3D) {
|
||||
if (r_dds_type) {
|
||||
*r_dds_type = DDST_3D;
|
||||
}
|
||||
layer_count = depth;
|
||||
}
|
||||
|
||||
if (array_size > 1) {
|
||||
layer_count *= array_size;
|
||||
if (r_dds_type) {
|
||||
*r_dds_type |= DDST_ARRAY;
|
||||
}
|
||||
}
|
||||
|
||||
dds_format = _dxgi_to_dds_format(dxgi_format);
|
||||
} break;
|
||||
|
||||
default: {
|
||||
ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, vformat("Unrecognized or unsupported FourCC in DDS '%s'.", p_file->get_path()));
|
||||
}
|
||||
}
|
||||
|
||||
} else if (format_flags & DDPF_RGB) {
|
||||
// Channel-bitmasked formats.
|
||||
if (format_flags & DDPF_ALPHAPIXELS) {
|
||||
// With alpha.
|
||||
if (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_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_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_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_rgb_bits == 32 && format_red_mask == 0x3ff && format_green_mask == 0xffc00 && format_blue_mask == 0x3ff00000 && format_alpha_mask == 0xc0000000) {
|
||||
dds_format = DDS_RGB10A2;
|
||||
} else if (format_rgb_bits == 16 && format_red_mask == 0xf00 && format_green_mask == 0xf0 && format_blue_mask == 0xf && format_alpha_mask == 0xf000) {
|
||||
dds_format = DDS_BGRA4;
|
||||
} else if (format_rgb_bits == 16 && format_red_mask == 0xe0 && format_green_mask == 0x1c && format_blue_mask == 0x3 && format_alpha_mask == 0xff00) {
|
||||
dds_format = DDS_B2GR3A8;
|
||||
}
|
||||
|
||||
} else {
|
||||
// Without alpha.
|
||||
if (format_rgb_bits == 24 && format_red_mask == 0xff0000 && format_green_mask == 0xff00 && format_blue_mask == 0xff) {
|
||||
dds_format = DDS_BGR8;
|
||||
} else if (format_rgb_bits == 24 && format_red_mask == 0xff && format_green_mask == 0xff00 && format_blue_mask == 0xff0000) {
|
||||
dds_format = DDS_RGB8;
|
||||
} else if (format_rgb_bits == 16 && format_red_mask == 0x0000f800 && format_green_mask == 0x000007e0 && format_blue_mask == 0x0000001f) {
|
||||
dds_format = DDS_BGR565;
|
||||
} else if (format_rgb_bits == 8 && format_red_mask == 0xe0 && format_green_mask == 0x1c && format_blue_mask == 0x3) {
|
||||
dds_format = DDS_B2GR3;
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
// Other formats.
|
||||
if (format_flags & DDPF_ALPHAONLY && format_rgb_bits == 8 && format_alpha_mask == 0xff) {
|
||||
// Alpha only.
|
||||
dds_format = DDS_LUMINANCE;
|
||||
}
|
||||
}
|
||||
|
||||
// Depending on the writer, luminance formats may or may not have the DDPF_RGB or DDPF_LUMINANCE flags defined,
|
||||
// so we check for these formats after everything else failed.
|
||||
if (dds_format == DDS_MAX) {
|
||||
if (format_flags & DDPF_ALPHAPIXELS) {
|
||||
// With alpha.
|
||||
if (format_rgb_bits == 16 && format_red_mask == 0xff && format_alpha_mask == 0xff00) {
|
||||
dds_format = DDS_LUMINANCE_ALPHA;
|
||||
} else if (format_rgb_bits == 8 && format_red_mask == 0xf && format_alpha_mask == 0xf0) {
|
||||
dds_format = DDS_LUMINANCE_ALPHA_4;
|
||||
}
|
||||
|
||||
} else {
|
||||
// Without alpha.
|
||||
if (format_rgb_bits == 8 && format_red_mask == 0xff) {
|
||||
dds_format = DDS_LUMINANCE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// No format detected, error.
|
||||
if (dds_format == DDS_MAX) {
|
||||
ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, vformat("Unrecognized or unsupported color layout in DDS '%s'.", p_file->get_path()));
|
||||
}
|
||||
|
||||
if (!(flags & DDSD_MIPMAPCOUNT)) {
|
||||
mipmaps = 1;
|
||||
}
|
||||
|
||||
Vector<uint8_t> src_data;
|
||||
|
||||
if (p_layers.size() == 0 || p_layers.size() > layer_count) {
|
||||
p_layers.resize(layer_count);
|
||||
}
|
||||
|
||||
for (uint32_t i = 0; i < p_layers.size(); i++) {
|
||||
Error err = load_layer(p_layers.write[i], p_file, dds_format, width, height, mipmaps, pitch, flags, src_data);
|
||||
if (err != OK) {
|
||||
return err;
|
||||
}
|
||||
}
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
Error ImageLoaderDDS::load_image(Ref<Image> p_image, Ref<FileAccess> f, BitField<ImageFormatLoader::LoaderFlags> p_flags, float p_scale) {
|
||||
Vector<Ref<Image>> images;
|
||||
images.resize(1);
|
||||
Error err = load_image_layers(images, f);
|
||||
|
||||
ERR_FAIL_COND_V(err != OK, err);
|
||||
|
||||
p_image = images[0];
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
ImageLoaderDDS::ImageLoaderDDS() {
|
||||
Image::_dds_mem_loader_func = _dds_mem_loader_func;
|
||||
}
|
||||
|
||||
void ImageLoaderDDS::get_recognized_extensions(List<String> *p_extensions) const {
|
||||
p_extensions->push_back("dds");
|
||||
}
|
||||
|
||||
bool ImageLoaderDDS::should_import(const String &p_resource_type) const {
|
||||
return false;
|
||||
}
|
||||
|
|
@ -0,0 +1,92 @@
|
|||
/**************************************************************************/
|
||||
/* image_loader_dds.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* 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. */
|
||||
/**************************************************************************/
|
||||
|
||||
#ifndef IMAGE_LOADER_DDS_H
|
||||
#define IMAGE_LOADER_DDS_H
|
||||
|
||||
#include "core/io/image_loader.h"
|
||||
|
||||
class ImageLoaderDDS : public ImageFormatLoader {
|
||||
public:
|
||||
// The legacy bitmasked format names here represent the actual data layout in the files,
|
||||
// while their official names are flipped (e.g. RGBA8 layout is officially called ABGR8).
|
||||
enum DDSFormat {
|
||||
DDS_DXT1,
|
||||
DDS_DXT3,
|
||||
DDS_DXT5,
|
||||
DDS_ATI1,
|
||||
DDS_ATI2,
|
||||
DDS_BC6U,
|
||||
DDS_BC6S,
|
||||
DDS_BC7,
|
||||
DDS_R16F,
|
||||
DDS_RG16F,
|
||||
DDS_RGBA16F,
|
||||
DDS_R32F,
|
||||
DDS_RG32F,
|
||||
DDS_RGB32F,
|
||||
DDS_RGBA32F,
|
||||
DDS_RGB9E5,
|
||||
DDS_RGB8,
|
||||
DDS_RGBA8,
|
||||
DDS_BGR8,
|
||||
DDS_BGRA8,
|
||||
DDS_BGR5A1,
|
||||
DDS_BGR565,
|
||||
DDS_B2GR3,
|
||||
DDS_B2GR3A8,
|
||||
DDS_BGR10A2,
|
||||
DDS_RGB10A2,
|
||||
DDS_BGRA4,
|
||||
DDS_LUMINANCE,
|
||||
DDS_LUMINANCE_ALPHA,
|
||||
DDS_LUMINANCE_ALPHA_4,
|
||||
DDS_MAX
|
||||
};
|
||||
|
||||
enum DDSType {
|
||||
DDST_2D = 1,
|
||||
DDST_CUBEMAP,
|
||||
DDST_3D,
|
||||
|
||||
DDST_TYPE_MASK = 0x7F,
|
||||
DDST_ARRAY = 0x80,
|
||||
};
|
||||
|
||||
static Error load_layer(Ref<Image> p_layer, Ref<FileAccess> p_file, DDSFormat p_dds_format, uint32_t p_width, uint32_t p_height, uint32_t p_mipmaps, uint32_t p_pitch, uint32_t p_flags, Vector<uint8_t> &r_src_data);
|
||||
static Error load_image_layers(Vector<Ref<Image>> &p_layers, Ref<FileAccess> p_file, uint32_t *r_dds_type = nullptr);
|
||||
|
||||
virtual Error load_image(Ref<Image> p_image, Ref<FileAccess> f, BitField<ImageFormatLoader::LoaderFlags> p_flags, float p_scale);
|
||||
virtual void get_recognized_extensions(List<String> *p_extensions) const;
|
||||
virtual bool should_import(const String &p_resource_type) const;
|
||||
ImageLoaderDDS();
|
||||
};
|
||||
|
||||
#endif // IMAGE_LOADER_DDS_H
|
||||
|
|
@ -30,9 +30,11 @@
|
|||
|
||||
#include "register_types.h"
|
||||
|
||||
#include "image_loader_dds.h"
|
||||
#include "texture_loader_dds.h"
|
||||
|
||||
static Ref<ResourceFormatDDS> resource_loader_dds;
|
||||
static Ref<ImageLoaderDDS> image_loader_dds;
|
||||
|
||||
void initialize_dds_module(ModuleInitializationLevel p_level) {
|
||||
if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
|
||||
|
|
@ -41,6 +43,9 @@ void initialize_dds_module(ModuleInitializationLevel p_level) {
|
|||
|
||||
resource_loader_dds.instantiate();
|
||||
ResourceLoader::add_resource_format_loader(resource_loader_dds);
|
||||
|
||||
image_loader_dds.instantiate();
|
||||
ImageLoader::add_image_format_loader(image_loader_dds);
|
||||
}
|
||||
|
||||
void uninitialize_dds_module(ModuleInitializationLevel p_level) {
|
||||
|
|
@ -50,4 +55,7 @@ void uninitialize_dds_module(ModuleInitializationLevel p_level) {
|
|||
|
||||
ResourceLoader::remove_resource_format_loader(resource_loader_dds);
|
||||
resource_loader_dds.unref();
|
||||
|
||||
ImageLoader::remove_image_format_loader(image_loader_dds);
|
||||
image_loader_dds.unref();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -31,518 +31,9 @@
|
|||
#include "texture_loader_dds.h"
|
||||
|
||||
#include "core/io/file_access.h"
|
||||
#include "image_loader_dds.h"
|
||||
#include "scene/resources/image_texture.h"
|
||||
|
||||
#define PF_FOURCC(s) ((uint32_t)(((s)[3] << 24U) | ((s)[2] << 16U) | ((s)[1] << 8U) | ((s)[0])))
|
||||
|
||||
// Reference: https://docs.microsoft.com/en-us/windows/win32/direct3ddds/dds-header
|
||||
|
||||
enum {
|
||||
DDS_MAGIC = 0x20534444,
|
||||
DDSD_PITCH = 0x00000008,
|
||||
DDSD_LINEARSIZE = 0x00080000,
|
||||
DDSD_MIPMAPCOUNT = 0x00020000,
|
||||
DDPF_ALPHAPIXELS = 0x00000001,
|
||||
DDPF_ALPHAONLY = 0x00000002,
|
||||
DDPF_FOURCC = 0x00000004,
|
||||
DDPF_RGB = 0x00000040,
|
||||
DDPF_RG_SNORM = 0x00080000,
|
||||
DDSC2_CUBEMAP = 0x200,
|
||||
DDSC2_VOLUME = 0x200000,
|
||||
DX10D_1D = 2,
|
||||
DX10D_2D = 3,
|
||||
DX10D_3D = 4,
|
||||
};
|
||||
|
||||
enum DDSFourCC {
|
||||
DDFCC_DXT1 = PF_FOURCC("DXT1"),
|
||||
DDFCC_DXT2 = PF_FOURCC("DXT2"),
|
||||
DDFCC_DXT3 = PF_FOURCC("DXT3"),
|
||||
DDFCC_DXT4 = PF_FOURCC("DXT4"),
|
||||
DDFCC_DXT5 = PF_FOURCC("DXT5"),
|
||||
DDFCC_ATI1 = PF_FOURCC("ATI1"),
|
||||
DDFCC_BC4U = PF_FOURCC("BC4U"),
|
||||
DDFCC_ATI2 = PF_FOURCC("ATI2"),
|
||||
DDFCC_BC5U = PF_FOURCC("BC5U"),
|
||||
DDFCC_A2XY = PF_FOURCC("A2XY"),
|
||||
DDFCC_DX10 = PF_FOURCC("DX10"),
|
||||
DDFCC_R16F = 111,
|
||||
DDFCC_RG16F = 112,
|
||||
DDFCC_RGBA16F = 113,
|
||||
DDFCC_R32F = 114,
|
||||
DDFCC_RG32F = 115,
|
||||
DDFCC_RGBA32F = 116
|
||||
};
|
||||
|
||||
// Reference: https://learn.microsoft.com/en-us/windows/win32/api/dxgiformat/ne-dxgiformat-dxgi_format
|
||||
enum DXGIFormat {
|
||||
DXGI_R32G32B32A32_FLOAT = 2,
|
||||
DXGI_R32G32B32_FLOAT = 6,
|
||||
DXGI_R16G16B16A16_FLOAT = 10,
|
||||
DXGI_R32G32_FLOAT = 16,
|
||||
DXGI_R10G10B10A2_UNORM = 24,
|
||||
DXGI_R8G8B8A8_UNORM = 28,
|
||||
DXGI_R8G8B8A8_UNORM_SRGB = 29,
|
||||
DXGI_R16G16_FLOAT = 34,
|
||||
DXGI_R32_FLOAT = 41,
|
||||
DXGI_R8G8_UNORM = 49,
|
||||
DXGI_R16_FLOAT = 54,
|
||||
DXGI_R8_UNORM = 61,
|
||||
DXGI_A8_UNORM = 65,
|
||||
DXGI_R9G9B9E5 = 67,
|
||||
DXGI_BC1_UNORM = 71,
|
||||
DXGI_BC1_UNORM_SRGB = 72,
|
||||
DXGI_BC2_UNORM = 74,
|
||||
DXGI_BC2_UNORM_SRGB = 75,
|
||||
DXGI_BC3_UNORM = 77,
|
||||
DXGI_BC3_UNORM_SRGB = 78,
|
||||
DXGI_BC4_UNORM = 80,
|
||||
DXGI_BC5_UNORM = 83,
|
||||
DXGI_B5G6R5_UNORM = 85,
|
||||
DXGI_B5G5R5A1_UNORM = 86,
|
||||
DXGI_B8G8R8A8_UNORM = 87,
|
||||
DXGI_BC6H_UF16 = 95,
|
||||
DXGI_BC6H_SF16 = 96,
|
||||
DXGI_BC7_UNORM = 98,
|
||||
DXGI_BC7_UNORM_SRGB = 99,
|
||||
DXGI_B4G4R4A4_UNORM = 115
|
||||
};
|
||||
|
||||
// The legacy bitmasked format names here represent the actual data layout in the files,
|
||||
// while their official names are flipped (e.g. RGBA8 layout is officially called ABGR8).
|
||||
enum DDSFormat {
|
||||
DDS_DXT1,
|
||||
DDS_DXT3,
|
||||
DDS_DXT5,
|
||||
DDS_ATI1,
|
||||
DDS_ATI2,
|
||||
DDS_BC6U,
|
||||
DDS_BC6S,
|
||||
DDS_BC7,
|
||||
DDS_R16F,
|
||||
DDS_RG16F,
|
||||
DDS_RGBA16F,
|
||||
DDS_R32F,
|
||||
DDS_RG32F,
|
||||
DDS_RGB32F,
|
||||
DDS_RGBA32F,
|
||||
DDS_RGB9E5,
|
||||
DDS_RGB8,
|
||||
DDS_RGBA8,
|
||||
DDS_BGR8,
|
||||
DDS_BGRA8,
|
||||
DDS_BGR5A1,
|
||||
DDS_BGR565,
|
||||
DDS_B2GR3,
|
||||
DDS_B2GR3A8,
|
||||
DDS_BGR10A2,
|
||||
DDS_RGB10A2,
|
||||
DDS_BGRA4,
|
||||
DDS_LUMINANCE,
|
||||
DDS_LUMINANCE_ALPHA,
|
||||
DDS_LUMINANCE_ALPHA_4,
|
||||
DDS_MAX
|
||||
};
|
||||
|
||||
enum DDSType {
|
||||
DDST_2D = 1,
|
||||
DDST_CUBEMAP,
|
||||
DDST_3D,
|
||||
|
||||
DDST_TYPE_MASK = 0x7F,
|
||||
DDST_ARRAY = 0x80,
|
||||
};
|
||||
|
||||
struct DDSFormatInfo {
|
||||
const char *name = nullptr;
|
||||
bool compressed = false;
|
||||
uint32_t divisor = 0;
|
||||
uint32_t block_size = 0;
|
||||
Image::Format format = Image::Format::FORMAT_BPTC_RGBA;
|
||||
};
|
||||
|
||||
static const DDSFormatInfo dds_format_info[DDS_MAX] = {
|
||||
{ "DXT1/BC1", true, 4, 8, Image::FORMAT_DXT1 },
|
||||
{ "DXT2/DXT3/BC2", true, 4, 16, Image::FORMAT_DXT3 },
|
||||
{ "DXT4/DXT5/BC3", true, 4, 16, Image::FORMAT_DXT5 },
|
||||
{ "ATI1/BC4", true, 4, 8, Image::FORMAT_RGTC_R },
|
||||
{ "ATI2/A2XY/BC5", true, 4, 16, Image::FORMAT_RGTC_RG },
|
||||
{ "BC6UF", true, 4, 16, Image::FORMAT_BPTC_RGBFU },
|
||||
{ "BC6SF", true, 4, 16, Image::FORMAT_BPTC_RGBF },
|
||||
{ "BC7", true, 4, 16, Image::FORMAT_BPTC_RGBA },
|
||||
{ "R16F", false, 1, 2, Image::FORMAT_RH },
|
||||
{ "RG16F", false, 1, 4, Image::FORMAT_RGH },
|
||||
{ "RGBA16F", false, 1, 8, Image::FORMAT_RGBAH },
|
||||
{ "R32F", false, 1, 4, Image::FORMAT_RF },
|
||||
{ "RG32F", false, 1, 8, Image::FORMAT_RGF },
|
||||
{ "RGB32F", false, 1, 12, Image::FORMAT_RGBF },
|
||||
{ "RGBA32F", false, 1, 16, Image::FORMAT_RGBAF },
|
||||
{ "RGB9E5", false, 1, 4, Image::FORMAT_RGBE9995 },
|
||||
{ "RGB8", false, 1, 3, Image::FORMAT_RGB8 },
|
||||
{ "RGBA8", false, 1, 4, Image::FORMAT_RGBA8 },
|
||||
{ "BGR8", false, 1, 3, Image::FORMAT_RGB8 },
|
||||
{ "BGRA8", false, 1, 4, Image::FORMAT_RGBA8 },
|
||||
{ "BGR5A1", false, 1, 2, Image::FORMAT_RGBA8 },
|
||||
{ "BGR565", false, 1, 2, Image::FORMAT_RGB8 },
|
||||
{ "B2GR3", false, 1, 1, Image::FORMAT_RGB8 },
|
||||
{ "B2GR3A8", false, 1, 2, Image::FORMAT_RGBA8 },
|
||||
{ "BGR10A2", false, 1, 4, Image::FORMAT_RGBA8 },
|
||||
{ "RGB10A2", false, 1, 4, Image::FORMAT_RGBA8 },
|
||||
{ "BGRA4", false, 1, 2, Image::FORMAT_RGBA8 },
|
||||
{ "GRAYSCALE", false, 1, 1, Image::FORMAT_L8 },
|
||||
{ "GRAYSCALE_ALPHA", false, 1, 2, Image::FORMAT_LA8 },
|
||||
{ "GRAYSCALE_ALPHA_4", false, 1, 1, Image::FORMAT_LA8 }
|
||||
};
|
||||
|
||||
inline DDSFormat _dxgi_to_dds_format(uint32_t p_dxgi_format) {
|
||||
switch (p_dxgi_format) {
|
||||
case DXGI_R32G32B32A32_FLOAT: {
|
||||
return DDS_RGBA32F;
|
||||
}
|
||||
case DXGI_R32G32B32_FLOAT: {
|
||||
return DDS_RGB32F;
|
||||
}
|
||||
case DXGI_R16G16B16A16_FLOAT: {
|
||||
return DDS_RGBA16F;
|
||||
}
|
||||
case DXGI_R32G32_FLOAT: {
|
||||
return DDS_RG32F;
|
||||
}
|
||||
case DXGI_R10G10B10A2_UNORM: {
|
||||
return DDS_RGB10A2;
|
||||
}
|
||||
case DXGI_R8G8B8A8_UNORM:
|
||||
case DXGI_R8G8B8A8_UNORM_SRGB: {
|
||||
return DDS_RGBA8;
|
||||
}
|
||||
case DXGI_R16G16_FLOAT: {
|
||||
return DDS_RG16F;
|
||||
}
|
||||
case DXGI_R32_FLOAT: {
|
||||
return DDS_R32F;
|
||||
}
|
||||
case DXGI_R8_UNORM:
|
||||
case DXGI_A8_UNORM: {
|
||||
return DDS_LUMINANCE;
|
||||
}
|
||||
case DXGI_R16_FLOAT: {
|
||||
return DDS_R16F;
|
||||
}
|
||||
case DXGI_R8G8_UNORM: {
|
||||
return DDS_LUMINANCE_ALPHA;
|
||||
}
|
||||
case DXGI_R9G9B9E5: {
|
||||
return DDS_RGB9E5;
|
||||
}
|
||||
case DXGI_BC1_UNORM:
|
||||
case DXGI_BC1_UNORM_SRGB: {
|
||||
return DDS_DXT1;
|
||||
}
|
||||
case DXGI_BC2_UNORM:
|
||||
case DXGI_BC2_UNORM_SRGB: {
|
||||
return DDS_DXT3;
|
||||
}
|
||||
case DXGI_BC3_UNORM:
|
||||
case DXGI_BC3_UNORM_SRGB: {
|
||||
return DDS_DXT5;
|
||||
}
|
||||
case DXGI_BC4_UNORM: {
|
||||
return DDS_ATI1;
|
||||
}
|
||||
case DXGI_BC5_UNORM: {
|
||||
return DDS_ATI2;
|
||||
}
|
||||
case DXGI_B5G6R5_UNORM: {
|
||||
return DDS_BGR565;
|
||||
}
|
||||
case DXGI_B5G5R5A1_UNORM: {
|
||||
return DDS_BGR5A1;
|
||||
}
|
||||
case DXGI_B8G8R8A8_UNORM: {
|
||||
return DDS_BGRA8;
|
||||
}
|
||||
case DXGI_BC6H_UF16: {
|
||||
return DDS_BC6U;
|
||||
}
|
||||
case DXGI_BC6H_SF16: {
|
||||
return DDS_BC6S;
|
||||
}
|
||||
case DXGI_BC7_UNORM:
|
||||
case DXGI_BC7_UNORM_SRGB: {
|
||||
return DDS_BC7;
|
||||
}
|
||||
case DXGI_B4G4R4A4_UNORM: {
|
||||
return DDS_BGRA4;
|
||||
}
|
||||
|
||||
default: {
|
||||
return DDS_MAX;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static Ref<Image> _dds_load_layer(Ref<FileAccess> p_file, DDSFormat p_dds_format, uint32_t p_width, uint32_t p_height, uint32_t p_mipmaps, uint32_t p_pitch, uint32_t p_flags, Vector<uint8_t> &r_src_data) {
|
||||
const DDSFormatInfo &info = dds_format_info[p_dds_format];
|
||||
|
||||
uint32_t w = p_width;
|
||||
uint32_t h = p_height;
|
||||
|
||||
if (info.compressed) {
|
||||
// BC compressed.
|
||||
w += w % info.divisor;
|
||||
h += h % info.divisor;
|
||||
if (w != p_width) {
|
||||
WARN_PRINT(vformat("%s: DDS width '%d' is not divisible by %d. This is not allowed as per the DDS specification, attempting to load anyway.", p_file->get_path(), p_width, info.divisor));
|
||||
}
|
||||
if (h != p_height) {
|
||||
WARN_PRINT(vformat("%s: DDS height '%d' is not divisible by %d. This is not allowed as per the DDS specification, attempting to load anyway.", p_file->get_path(), p_height, info.divisor));
|
||||
}
|
||||
|
||||
uint32_t size = MAX(info.divisor, w) / info.divisor * MAX(info.divisor, h) / info.divisor * info.block_size;
|
||||
|
||||
if (p_flags & DDSD_LINEARSIZE) {
|
||||
ERR_FAIL_COND_V_MSG(size != p_pitch, Ref<Resource>(), "DDS header flags specify that a linear size of the top-level image is present, but the specified size does not match the expected value.");
|
||||
} else {
|
||||
ERR_FAIL_COND_V_MSG(p_pitch != 0, Ref<Resource>(), "DDS header flags specify that no linear size will given for the top-level image, but a non-zero linear size value is present in the header.");
|
||||
}
|
||||
|
||||
for (uint32_t i = 1; i < p_mipmaps; i++) {
|
||||
w = MAX(1u, w >> 1);
|
||||
h = MAX(1u, h >> 1);
|
||||
|
||||
uint32_t bsize = MAX(info.divisor, w) / info.divisor * MAX(info.divisor, h) / info.divisor * info.block_size;
|
||||
size += bsize;
|
||||
}
|
||||
|
||||
r_src_data.resize(size);
|
||||
uint8_t *wb = r_src_data.ptrw();
|
||||
p_file->get_buffer(wb, size);
|
||||
|
||||
} else {
|
||||
// Generic uncompressed.
|
||||
uint32_t size = p_width * p_height * info.block_size;
|
||||
|
||||
for (uint32_t i = 1; i < p_mipmaps; i++) {
|
||||
w = (w + 1) >> 1;
|
||||
h = (h + 1) >> 1;
|
||||
size += w * h * info.block_size;
|
||||
}
|
||||
|
||||
// Calculate the space these formats will take up after decoding.
|
||||
switch (p_dds_format) {
|
||||
case DDS_BGR565:
|
||||
size = size * 3 / 2;
|
||||
break;
|
||||
|
||||
case DDS_BGR5A1:
|
||||
case DDS_BGRA4:
|
||||
case DDS_B2GR3A8:
|
||||
case DDS_LUMINANCE_ALPHA_4:
|
||||
size = size * 2;
|
||||
break;
|
||||
|
||||
case DDS_B2GR3:
|
||||
size = size * 3;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
r_src_data.resize(size);
|
||||
uint8_t *wb = r_src_data.ptrw();
|
||||
p_file->get_buffer(wb, size);
|
||||
|
||||
switch (p_dds_format) {
|
||||
case DDS_BGR5A1: {
|
||||
// To RGBA8.
|
||||
int colcount = size / 4;
|
||||
|
||||
for (int i = colcount - 1; i >= 0; i--) {
|
||||
int src_ofs = i * 2;
|
||||
int dst_ofs = i * 4;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
} break;
|
||||
case DDS_BGR565: {
|
||||
// To RGB8.
|
||||
int colcount = size / 3;
|
||||
|
||||
for (int i = colcount - 1; i >= 0; i--) {
|
||||
int src_ofs = i * 2;
|
||||
int dst_ofs = i * 3;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
} break;
|
||||
case DDS_BGRA4: {
|
||||
// To RGBA8.
|
||||
int colcount = size / 4;
|
||||
|
||||
for (int i = colcount - 1; i >= 0; i--) {
|
||||
int src_ofs = i * 2;
|
||||
int dst_ofs = i * 4;
|
||||
|
||||
uint8_t b = wb[src_ofs] & 0x0F;
|
||||
uint8_t g = wb[src_ofs] & 0xF0;
|
||||
uint8_t r = wb[src_ofs + 1] & 0x0F;
|
||||
uint8_t a = wb[src_ofs + 1] & 0xF0;
|
||||
|
||||
wb[dst_ofs] = (r << 4) | r;
|
||||
wb[dst_ofs + 1] = g | (g >> 4);
|
||||
wb[dst_ofs + 2] = (b << 4) | b;
|
||||
wb[dst_ofs + 3] = a | (a >> 4);
|
||||
}
|
||||
|
||||
} break;
|
||||
case DDS_B2GR3: {
|
||||
// To RGB8.
|
||||
int colcount = size / 3;
|
||||
|
||||
for (int i = colcount - 1; i >= 0; i--) {
|
||||
int src_ofs = i;
|
||||
int dst_ofs = i * 3;
|
||||
|
||||
uint8_t b = (wb[src_ofs] & 0x3) << 6;
|
||||
uint8_t g = (wb[src_ofs] & 0x1C) << 3;
|
||||
uint8_t r = (wb[src_ofs] & 0xE0);
|
||||
|
||||
wb[dst_ofs] = r;
|
||||
wb[dst_ofs + 1] = g;
|
||||
wb[dst_ofs + 2] = b;
|
||||
}
|
||||
|
||||
} break;
|
||||
case DDS_B2GR3A8: {
|
||||
// To RGBA8.
|
||||
int colcount = size / 4;
|
||||
|
||||
for (int i = colcount - 1; i >= 0; i--) {
|
||||
int src_ofs = i * 2;
|
||||
int dst_ofs = i * 4;
|
||||
|
||||
uint8_t b = (wb[src_ofs] & 0x3) << 6;
|
||||
uint8_t g = (wb[src_ofs] & 0x1C) << 3;
|
||||
uint8_t r = (wb[src_ofs] & 0xE0);
|
||||
uint8_t a = wb[src_ofs + 1];
|
||||
|
||||
wb[dst_ofs] = r;
|
||||
wb[dst_ofs + 1] = g;
|
||||
wb[dst_ofs + 2] = b;
|
||||
wb[dst_ofs + 3] = a;
|
||||
}
|
||||
|
||||
} break;
|
||||
case DDS_RGB10A2: {
|
||||
// To RGBA8.
|
||||
int colcount = size / 4;
|
||||
|
||||
for (int i = 0; i < colcount; i++) {
|
||||
int ofs = i * 4;
|
||||
|
||||
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);
|
||||
|
||||
// This method follows the 'standard' way of decoding 10-bit dds files,
|
||||
// which means the ones created with DirectXTex will be loaded incorrectly.
|
||||
uint8_t a = (w32 & 0xc0000000) >> 24;
|
||||
uint8_t r = (w32 & 0x3ff) >> 2;
|
||||
uint8_t g = (w32 & 0xffc00) >> 12;
|
||||
uint8_t b = (w32 & 0x3ff00000) >> 22;
|
||||
|
||||
wb[ofs + 0] = r;
|
||||
wb[ofs + 1] = g;
|
||||
wb[ofs + 2] = b;
|
||||
wb[ofs + 3] = a == 0xc0 ? 255 : a; // 0xc0 should be opaque.
|
||||
}
|
||||
|
||||
} break;
|
||||
case DDS_BGR10A2: {
|
||||
// To RGBA8.
|
||||
int colcount = size / 4;
|
||||
|
||||
for (int i = 0; i < colcount; i++) {
|
||||
int ofs = i * 4;
|
||||
|
||||
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);
|
||||
|
||||
// This method follows the 'standard' way of decoding 10-bit dds files,
|
||||
// which means the ones created with DirectXTex will be loaded incorrectly.
|
||||
uint8_t a = (w32 & 0xc0000000) >> 24;
|
||||
uint8_t r = (w32 & 0x3ff00000) >> 22;
|
||||
uint8_t g = (w32 & 0xffc00) >> 12;
|
||||
uint8_t b = (w32 & 0x3ff) >> 2;
|
||||
|
||||
wb[ofs + 0] = r;
|
||||
wb[ofs + 1] = g;
|
||||
wb[ofs + 2] = b;
|
||||
wb[ofs + 3] = a == 0xc0 ? 255 : a; // 0xc0 should be opaque.
|
||||
}
|
||||
|
||||
} break;
|
||||
|
||||
// Channel-swapped.
|
||||
case DDS_BGRA8: {
|
||||
// To RGBA8.
|
||||
int colcount = size / 4;
|
||||
|
||||
for (int i = 0; i < colcount; i++) {
|
||||
SWAP(wb[i * 4 + 0], wb[i * 4 + 2]);
|
||||
}
|
||||
|
||||
} break;
|
||||
case DDS_BGR8: {
|
||||
// To RGB8.
|
||||
int colcount = size / 3;
|
||||
|
||||
for (int i = 0; i < colcount; i++) {
|
||||
SWAP(wb[i * 3 + 0], wb[i * 3 + 2]);
|
||||
}
|
||||
|
||||
} break;
|
||||
|
||||
// Grayscale.
|
||||
case DDS_LUMINANCE_ALPHA_4: {
|
||||
// To LA8.
|
||||
int colcount = size / 2;
|
||||
|
||||
for (int i = colcount - 1; i >= 0; i--) {
|
||||
int src_ofs = i;
|
||||
int dst_ofs = i * 2;
|
||||
|
||||
uint8_t l = wb[src_ofs] & 0x0F;
|
||||
uint8_t a = wb[src_ofs] & 0xF0;
|
||||
|
||||
wb[dst_ofs] = (l << 4) | l;
|
||||
wb[dst_ofs + 1] = a | (a >> 4);
|
||||
}
|
||||
|
||||
} break;
|
||||
|
||||
default: {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return memnew(Image(p_width, p_height, p_mipmaps > 1, info.format, r_src_data));
|
||||
}
|
||||
|
||||
Ref<Resource> 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) {
|
||||
if (r_error) {
|
||||
*r_error = ERR_CANT_OPEN;
|
||||
|
|
@ -561,208 +52,16 @@ Ref<Resource> ResourceFormatDDS::load(const String &p_path, const String &p_orig
|
|||
|
||||
ERR_FAIL_COND_V_MSG(err != OK, Ref<Resource>(), vformat("Unable to open DDS texture file '%s'.", p_path));
|
||||
|
||||
uint32_t magic = f->get_32();
|
||||
uint32_t hsize = f->get_32();
|
||||
uint32_t flags = f->get_32();
|
||||
uint32_t height = f->get_32();
|
||||
uint32_t width = f->get_32();
|
||||
uint32_t pitch = f->get_32();
|
||||
uint32_t depth = f->get_32();
|
||||
uint32_t mipmaps = f->get_32();
|
||||
|
||||
// Skip reserved.
|
||||
for (int i = 0; i < 11; i++) {
|
||||
f->get_32();
|
||||
}
|
||||
|
||||
// Validate.
|
||||
// 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) {
|
||||
ERR_FAIL_V_MSG(Ref<Resource>(), vformat("Invalid or unsupported DDS texture file '%s'.", p_path));
|
||||
}
|
||||
|
||||
/* uint32_t format_size = */ f->get_32();
|
||||
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();
|
||||
|
||||
/* uint32_t caps_1 = */ f->get_32();
|
||||
uint32_t caps_2 = f->get_32();
|
||||
/* uint32_t caps_3 = */ f->get_32();
|
||||
/* uint32_t caps_4 = */ f->get_32();
|
||||
|
||||
// Skip reserved.
|
||||
f->get_32();
|
||||
|
||||
if (f->get_position() < 128) {
|
||||
f->seek(128);
|
||||
}
|
||||
|
||||
uint32_t layer_count = 1;
|
||||
uint32_t dds_type = DDST_2D;
|
||||
|
||||
if (caps_2 & DDSC2_CUBEMAP) {
|
||||
dds_type = DDST_CUBEMAP;
|
||||
layer_count *= 6;
|
||||
|
||||
} else if (caps_2 & DDSC2_VOLUME) {
|
||||
dds_type = DDST_3D;
|
||||
layer_count = depth;
|
||||
}
|
||||
|
||||
DDSFormat dds_format = DDS_MAX;
|
||||
|
||||
if (format_flags & DDPF_FOURCC) {
|
||||
// FourCC formats.
|
||||
switch (format_fourcc) {
|
||||
case DDFCC_DXT1: {
|
||||
dds_format = DDS_DXT1;
|
||||
} break;
|
||||
case DDFCC_DXT2:
|
||||
case DDFCC_DXT3: {
|
||||
dds_format = DDS_DXT3;
|
||||
} break;
|
||||
case DDFCC_DXT4:
|
||||
case DDFCC_DXT5: {
|
||||
dds_format = DDS_DXT5;
|
||||
} break;
|
||||
case DDFCC_ATI1:
|
||||
case DDFCC_BC4U: {
|
||||
dds_format = DDS_ATI1;
|
||||
} break;
|
||||
case DDFCC_ATI2:
|
||||
case DDFCC_BC5U:
|
||||
case DDFCC_A2XY: {
|
||||
dds_format = DDS_ATI2;
|
||||
} break;
|
||||
case DDFCC_R16F: {
|
||||
dds_format = DDS_R16F;
|
||||
} break;
|
||||
case DDFCC_RG16F: {
|
||||
dds_format = DDS_RG16F;
|
||||
} break;
|
||||
case DDFCC_RGBA16F: {
|
||||
dds_format = DDS_RGBA16F;
|
||||
} break;
|
||||
case DDFCC_R32F: {
|
||||
dds_format = DDS_R32F;
|
||||
} break;
|
||||
case DDFCC_RG32F: {
|
||||
dds_format = DDS_RG32F;
|
||||
} break;
|
||||
case DDFCC_RGBA32F: {
|
||||
dds_format = DDS_RGBA32F;
|
||||
} break;
|
||||
case DDFCC_DX10: {
|
||||
uint32_t dxgi_format = f->get_32();
|
||||
uint32_t dimension = f->get_32();
|
||||
/* uint32_t misc_flags_1 = */ f->get_32();
|
||||
uint32_t array_size = f->get_32();
|
||||
/* uint32_t misc_flags_2 = */ f->get_32();
|
||||
|
||||
if (dimension == DX10D_3D) {
|
||||
dds_type = DDST_3D;
|
||||
layer_count = depth;
|
||||
}
|
||||
|
||||
if (array_size > 1) {
|
||||
layer_count *= array_size;
|
||||
dds_type |= DDST_ARRAY;
|
||||
}
|
||||
|
||||
dds_format = _dxgi_to_dds_format(dxgi_format);
|
||||
} break;
|
||||
|
||||
default: {
|
||||
ERR_FAIL_V_MSG(Ref<Resource>(), vformat("Unrecognized or unsupported FourCC in DDS '%s'.", p_path));
|
||||
}
|
||||
}
|
||||
|
||||
} else if (format_flags & DDPF_RGB) {
|
||||
// Channel-bitmasked formats.
|
||||
if (format_flags & DDPF_ALPHAPIXELS) {
|
||||
// With alpha.
|
||||
if (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_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_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_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_rgb_bits == 32 && format_red_mask == 0x3ff && format_green_mask == 0xffc00 && format_blue_mask == 0x3ff00000 && format_alpha_mask == 0xc0000000) {
|
||||
dds_format = DDS_RGB10A2;
|
||||
} else if (format_rgb_bits == 16 && format_red_mask == 0xf00 && format_green_mask == 0xf0 && format_blue_mask == 0xf && format_alpha_mask == 0xf000) {
|
||||
dds_format = DDS_BGRA4;
|
||||
} else if (format_rgb_bits == 16 && format_red_mask == 0xe0 && format_green_mask == 0x1c && format_blue_mask == 0x3 && format_alpha_mask == 0xff00) {
|
||||
dds_format = DDS_B2GR3A8;
|
||||
}
|
||||
|
||||
} else {
|
||||
// Without alpha.
|
||||
if (format_rgb_bits == 24 && format_red_mask == 0xff0000 && format_green_mask == 0xff00 && format_blue_mask == 0xff) {
|
||||
dds_format = DDS_BGR8;
|
||||
} else if (format_rgb_bits == 24 && format_red_mask == 0xff && format_green_mask == 0xff00 && format_blue_mask == 0xff0000) {
|
||||
dds_format = DDS_RGB8;
|
||||
} else if (format_rgb_bits == 16 && format_red_mask == 0x0000f800 && format_green_mask == 0x000007e0 && format_blue_mask == 0x0000001f) {
|
||||
dds_format = DDS_BGR565;
|
||||
} else if (format_rgb_bits == 8 && format_red_mask == 0xe0 && format_green_mask == 0x1c && format_blue_mask == 0x3) {
|
||||
dds_format = DDS_B2GR3;
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
// Other formats.
|
||||
if (format_flags & DDPF_ALPHAONLY && format_rgb_bits == 8 && format_alpha_mask == 0xff) {
|
||||
// Alpha only.
|
||||
dds_format = DDS_LUMINANCE;
|
||||
}
|
||||
}
|
||||
|
||||
// Depending on the writer, luminance formats may or may not have the DDPF_RGB or DDPF_LUMINANCE flags defined,
|
||||
// so we check for these formats after everything else failed.
|
||||
if (dds_format == DDS_MAX) {
|
||||
if (format_flags & DDPF_ALPHAPIXELS) {
|
||||
// With alpha.
|
||||
if (format_rgb_bits == 16 && format_red_mask == 0xff && format_alpha_mask == 0xff00) {
|
||||
dds_format = DDS_LUMINANCE_ALPHA;
|
||||
} else if (format_rgb_bits == 8 && format_red_mask == 0xf && format_alpha_mask == 0xf0) {
|
||||
dds_format = DDS_LUMINANCE_ALPHA_4;
|
||||
}
|
||||
|
||||
} else {
|
||||
// Without alpha.
|
||||
if (format_rgb_bits == 8 && format_red_mask == 0xff) {
|
||||
dds_format = DDS_LUMINANCE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// No format detected, error.
|
||||
if (dds_format == DDS_MAX) {
|
||||
ERR_FAIL_V_MSG(Ref<Resource>(), vformat("Unrecognized or unsupported color layout in DDS '%s'.", p_path));
|
||||
}
|
||||
|
||||
if (!(flags & DDSD_MIPMAPCOUNT)) {
|
||||
mipmaps = 1;
|
||||
}
|
||||
|
||||
Vector<uint8_t> src_data;
|
||||
|
||||
Vector<Ref<Image>> images;
|
||||
images.resize(layer_count);
|
||||
uint32_t dds_type = 0;
|
||||
err = ImageLoaderDDS::load_image_layers(images, f, &dds_type);
|
||||
|
||||
for (uint32_t i = 0; i < layer_count; i++) {
|
||||
images.write[i] = _dds_load_layer(f, dds_format, width, height, mipmaps, pitch, flags, src_data);
|
||||
}
|
||||
ERR_FAIL_COND_V_MSG(err != OK, Ref<Resource>(), vformat("Unable to read layers in DDS texture file '%s'.", p_path));
|
||||
|
||||
if ((dds_type & DDST_TYPE_MASK) == DDST_2D) {
|
||||
if (dds_type & DDST_ARRAY) {
|
||||
uint32_t layer_count = images.size();
|
||||
|
||||
if ((dds_type & ImageLoaderDDS::DDST_TYPE_MASK) == ImageLoaderDDS::DDST_2D) {
|
||||
if (dds_type & ImageLoaderDDS::DDST_ARRAY) {
|
||||
Ref<Texture2DArray> texture = memnew(Texture2DArray());
|
||||
texture->create_from_images(images);
|
||||
|
||||
|
|
@ -780,10 +79,10 @@ Ref<Resource> ResourceFormatDDS::load(const String &p_path, const String &p_orig
|
|||
return ImageTexture::create_from_image(images[0]);
|
||||
}
|
||||
|
||||
} else if ((dds_type & DDST_TYPE_MASK) == DDST_CUBEMAP) {
|
||||
} else if ((dds_type & ImageLoaderDDS::DDST_TYPE_MASK) == ImageLoaderDDS::DDST_CUBEMAP) {
|
||||
ERR_FAIL_COND_V(layer_count % 6 != 0, Ref<Resource>());
|
||||
|
||||
if (dds_type & DDST_ARRAY) {
|
||||
if (dds_type & ImageLoaderDDS::DDST_ARRAY) {
|
||||
Ref<CubemapArray> texture = memnew(CubemapArray());
|
||||
texture->create_from_images(images);
|
||||
|
||||
|
|
@ -804,9 +103,9 @@ Ref<Resource> ResourceFormatDDS::load(const String &p_path, const String &p_orig
|
|||
return texture;
|
||||
}
|
||||
|
||||
} else if ((dds_type & DDST_TYPE_MASK) == DDST_3D) {
|
||||
} else if ((dds_type & ImageLoaderDDS::DDST_TYPE_MASK) == ImageLoaderDDS::DDST_3D) {
|
||||
Ref<ImageTexture3D> texture = memnew(ImageTexture3D());
|
||||
texture->create(images[0]->get_format(), width, height, layer_count, mipmaps > 1, images);
|
||||
texture->create(images[0]->get_format(), images[0]->get_width(), images[0]->get_height(), layer_count, images[0]->has_mipmaps(), images);
|
||||
|
||||
if (r_error) {
|
||||
*r_error = OK;
|
||||
|
|
|
|||
|
|
@ -123,6 +123,19 @@ TEST_CASE("[Image] Saving and loading") {
|
|||
"The BMP image should load successfully.");
|
||||
#endif // MODULE_BMP_ENABLED
|
||||
|
||||
#ifdef MODULE_DDS_ENABLED
|
||||
// Load DDS
|
||||
Ref<Image> image_dds = memnew(Image());
|
||||
Ref<FileAccess> f_dds = FileAccess::open(TestUtils::get_data_path("images/icon.dds"), FileAccess::READ, &err);
|
||||
REQUIRE(f_dds.is_valid());
|
||||
PackedByteArray data_dds;
|
||||
data_dds.resize(f_dds->get_length() + 1);
|
||||
f_dds->get_buffer(data_dds.ptrw(), f_dds->get_length());
|
||||
CHECK_MESSAGE(
|
||||
image_dds->load_dds_from_buffer(data_dds) == OK,
|
||||
"The DDS image should load successfully.");
|
||||
#endif // MODULE_DDS_ENABLED
|
||||
|
||||
#ifdef MODULE_JPG_ENABLED
|
||||
// Load JPG
|
||||
Ref<Image> image_jpg = memnew(Image());
|
||||
|
|
|
|||
Binary file not shown.
Loading…
Reference in New Issue