From 74ce201cc4ee7c198980f9adee523fbcac580379 Mon Sep 17 00:00:00 2001 From: Spartan322 Date: Tue, 17 Dec 2024 18:30:39 -0500 Subject: [PATCH 1/2] Add support for specifying `ImageFormatLoader` Resource import --- core/io/image_loader.cpp | 28 +++++++++++++++---- core/io/image_loader.h | 9 ++++-- doc/classes/ImageFormatLoaderExtension.xml | 8 ++++++ editor/import/resource_importer_bitmask.cpp | 2 +- editor/import/resource_importer_image.cpp | 2 +- editor/import/resource_importer_imagefont.cpp | 2 +- .../resource_importer_layered_texture.cpp | 2 +- editor/import/resource_importer_texture.cpp | 2 +- .../resource_importer_texture_atlas.cpp | 2 +- 9 files changed, 43 insertions(+), 14 deletions(-) diff --git a/core/io/image_loader.cpp b/core/io/image_loader.cpp index 51c4c49bc58c..f7b11ef2141e 100644 --- a/core/io/image_loader.cpp +++ b/core/io/image_loader.cpp @@ -36,7 +36,15 @@ void ImageFormatLoader::_bind_methods() { BIND_BITFIELD_FLAG(FLAG_CONVERT_COLORS); } -bool ImageFormatLoader::recognize(const String &p_extension) const { +bool ImageFormatLoader::should_import(const String &p_resource_type) const { + return true; +} + +bool ImageFormatLoader::recognize(const String &p_extension, const String &p_resource_type) const { + if (!should_import(p_resource_type)) { + return false; + } + List extensions; get_recognized_extensions(&extensions); for (const String &E : extensions) { @@ -63,6 +71,12 @@ void ImageFormatLoaderExtension::get_recognized_extensions(List *p_exten } } +bool ImageFormatLoaderExtension::should_import(const String &p_resource_type) const { + bool should_import = true; + GDVIRTUAL_CALL(_should_import, p_resource_type, should_import); + return should_import; +} + void ImageFormatLoaderExtension::add_format_loader() { ImageLoader::add_image_format_loader(this); } @@ -73,6 +87,7 @@ void ImageFormatLoaderExtension::remove_format_loader() { void ImageFormatLoaderExtension::_bind_methods() { GDVIRTUAL_BIND(_get_recognized_extensions); + GDVIRTUAL_BIND(_should_import, "resource_type"); GDVIRTUAL_BIND(_load_image, "image", "fileaccess", "flags", "scale"); ClassDB::bind_method(D_METHOD("add_format_loader"), &ImageFormatLoaderExtension::add_format_loader); ClassDB::bind_method(D_METHOD("remove_format_loader"), &ImageFormatLoaderExtension::remove_format_loader); @@ -108,15 +123,18 @@ Error ImageLoader::load_image(const String &p_file, Ref p_image, Ref *p_extensions) { +void ImageLoader::get_recognized_extensions(List *p_extensions, const String &p_resource_type) { for (int i = 0; i < loader.size(); i++) { + if (!loader[i]->should_import(p_resource_type)) { + continue; + } loader[i]->get_recognized_extensions(p_extensions); } } -Ref ImageLoader::recognize(const String &p_extension) { +Ref ImageLoader::recognize(const String &p_extension, const String &p_resource_type) { for (int i = 0; i < loader.size(); i++) { - if (loader[i]->recognize(p_extension)) { + if (loader[i]->recognize(p_extension, p_resource_type)) { return loader[i]; } } @@ -167,7 +185,7 @@ Ref ResourceFormatLoaderImage::load(const String &p_path, const String int idx = -1; for (int i = 0; i < ImageLoader::loader.size(); i++) { - if (ImageLoader::loader[i]->recognize(extension)) { + if (ImageLoader::loader[i]->recognize(extension, "Image")) { idx = i; break; } diff --git a/core/io/image_loader.h b/core/io/image_loader.h index 26af650344dc..6bdb7e25aa45 100644 --- a/core/io/image_loader.h +++ b/core/io/image_loader.h @@ -60,7 +60,8 @@ class ImageFormatLoader : public RefCounted { virtual Error load_image(Ref p_image, Ref p_fileaccess, BitField p_flags = FLAG_NONE, float p_scale = 1.0) = 0; virtual void get_recognized_extensions(List *p_extensions) const = 0; - bool recognize(const String &p_extension) const; + virtual bool should_import(const String &p_resource_type) const; + bool recognize(const String &p_extension, const String &p_resource_type = "") const; public: virtual ~ImageFormatLoader() {} @@ -77,11 +78,13 @@ class ImageFormatLoaderExtension : public ImageFormatLoader { public: virtual Error load_image(Ref p_image, Ref p_fileaccess, BitField p_flags = FLAG_NONE, float p_scale = 1.0) override; virtual void get_recognized_extensions(List *p_extensions) const override; + virtual bool should_import(const String &p_resource_type) const override; void add_format_loader(); void remove_format_loader(); GDVIRTUAL0RC(PackedStringArray, _get_recognized_extensions); + GDVIRTUAL1RC(bool, _should_import, String); GDVIRTUAL4R(Error, _load_image, Ref, Ref, BitField, float); }; @@ -92,8 +95,8 @@ class ImageLoader { protected: public: static Error load_image(const String &p_file, Ref p_image, Ref p_custom = Ref(), BitField p_flags = ImageFormatLoader::FLAG_NONE, float p_scale = 1.0); - static void get_recognized_extensions(List *p_extensions); - static Ref recognize(const String &p_extension); + static void get_recognized_extensions(List *p_extensions, const String &p_resource_type = ""); + static Ref recognize(const String &p_extension, const String &p_resource_type = ""); static void add_image_format_loader(Ref p_loader); static void remove_image_format_loader(Ref p_loader); diff --git a/doc/classes/ImageFormatLoaderExtension.xml b/doc/classes/ImageFormatLoaderExtension.xml index a7556695184a..4cca36c69933 100644 --- a/doc/classes/ImageFormatLoaderExtension.xml +++ b/doc/classes/ImageFormatLoaderExtension.xml @@ -26,6 +26,14 @@ Loads the content of [param fileaccess] into the provided [param image]. + + + + + [b]Optional.[/b] + Returns [code]true[/code] if [param resource_type] should import. + + diff --git a/editor/import/resource_importer_bitmask.cpp b/editor/import/resource_importer_bitmask.cpp index 8441a4966684..fa8192a5ff46 100644 --- a/editor/import/resource_importer_bitmask.cpp +++ b/editor/import/resource_importer_bitmask.cpp @@ -44,7 +44,7 @@ String ResourceImporterBitMap::get_visible_name() const { } void ResourceImporterBitMap::get_recognized_extensions(List *p_extensions) const { - ImageLoader::get_recognized_extensions(p_extensions); + ImageLoader::get_recognized_extensions(p_extensions, get_resource_type()); } String ResourceImporterBitMap::get_save_extension() const { diff --git a/editor/import/resource_importer_image.cpp b/editor/import/resource_importer_image.cpp index 2807909e6ab4..7ab37122f4c6 100644 --- a/editor/import/resource_importer_image.cpp +++ b/editor/import/resource_importer_image.cpp @@ -42,7 +42,7 @@ String ResourceImporterImage::get_visible_name() const { } void ResourceImporterImage::get_recognized_extensions(List *p_extensions) const { - ImageLoader::get_recognized_extensions(p_extensions); + ImageLoader::get_recognized_extensions(p_extensions, get_resource_type()); } String ResourceImporterImage::get_save_extension() const { diff --git a/editor/import/resource_importer_imagefont.cpp b/editor/import/resource_importer_imagefont.cpp index e0a8cd924b7d..66a4d043c5a8 100644 --- a/editor/import/resource_importer_imagefont.cpp +++ b/editor/import/resource_importer_imagefont.cpp @@ -43,7 +43,7 @@ String ResourceImporterImageFont::get_visible_name() const { void ResourceImporterImageFont::get_recognized_extensions(List *p_extensions) const { if (p_extensions) { - ImageLoader::get_recognized_extensions(p_extensions); + ImageLoader::get_recognized_extensions(p_extensions, get_resource_type()); } } diff --git a/editor/import/resource_importer_layered_texture.cpp b/editor/import/resource_importer_layered_texture.cpp index 33186bc42c24..7fbddfbb5beb 100644 --- a/editor/import/resource_importer_layered_texture.cpp +++ b/editor/import/resource_importer_layered_texture.cpp @@ -78,7 +78,7 @@ String ResourceImporterLayeredTexture::get_visible_name() const { } void ResourceImporterLayeredTexture::get_recognized_extensions(List *p_extensions) const { - ImageLoader::get_recognized_extensions(p_extensions); + ImageLoader::get_recognized_extensions(p_extensions, get_resource_type()); } String ResourceImporterLayeredTexture::get_save_extension() const { diff --git a/editor/import/resource_importer_texture.cpp b/editor/import/resource_importer_texture.cpp index 594d7d9321a2..3018a1dde5bd 100644 --- a/editor/import/resource_importer_texture.cpp +++ b/editor/import/resource_importer_texture.cpp @@ -176,7 +176,7 @@ String ResourceImporterTexture::get_visible_name() const { } void ResourceImporterTexture::get_recognized_extensions(List *p_extensions) const { - ImageLoader::get_recognized_extensions(p_extensions); + ImageLoader::get_recognized_extensions(p_extensions, get_resource_type()); } String ResourceImporterTexture::get_save_extension() const { diff --git a/editor/import/resource_importer_texture_atlas.cpp b/editor/import/resource_importer_texture_atlas.cpp index 9346cabd8821..c450fc6d05ff 100644 --- a/editor/import/resource_importer_texture_atlas.cpp +++ b/editor/import/resource_importer_texture_atlas.cpp @@ -51,7 +51,7 @@ String ResourceImporterTextureAtlas::get_visible_name() const { } void ResourceImporterTextureAtlas::get_recognized_extensions(List *p_extensions) const { - ImageLoader::get_recognized_extensions(p_extensions); + ImageLoader::get_recognized_extensions(p_extensions, get_resource_type()); } String ResourceImporterTextureAtlas::get_save_extension() const { From 904f3a834bf5dda65567d952d99ea41bda926568 Mon Sep 17 00:00:00 2001 From: Spartan322 Date: Tue, 14 Jan 2025 12:26:40 -0500 Subject: [PATCH 2/2] Implement loading DDS textures at run-time Co-authored-by: Marcin Nowak --- core/io/image.cpp | 10 + core/io/image.h | 2 + doc/classes/Image.xml | 7 + modules/dds/image_loader_dds.cpp | 757 +++++++++++++++++++++++++++++ modules/dds/image_loader_dds.h | 92 ++++ modules/dds/register_types.cpp | 8 + modules/dds/texture_loader_dds.cpp | 725 +-------------------------- tests/core/io/test_image.h | 13 + tests/data/images/icon.dds | Bin 0 -> 87536 bytes 9 files changed, 901 insertions(+), 713 deletions(-) create mode 100644 modules/dds/image_loader_dds.cpp create mode 100644 modules/dds/image_loader_dds.h create mode 100644 tests/data/images/icon.dds diff --git a/core/io/image.cpp b/core/io/image.cpp index 05586b3d58f8..e016df9ec914 100644 --- a/core/io/image.cpp +++ b/core/io/image.cpp @@ -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 &p_array) { return _load_from_buffer(p_array, _ktx_mem_loader_func); } +Error Image::load_dds_from_buffer(const Vector &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()); diff --git a/core/io/image.h b/core/io/image.h index 992c43597df2..975ea63db4c9 100644 --- a/core/io/image.h +++ b/core/io/image.h @@ -201,6 +201,7 @@ class Image : public Resource { 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 @@ class Image : public Resource { Error load_tga_from_buffer(const Vector &p_array); Error load_bmp_from_buffer(const Vector &p_array); Error load_ktx_from_buffer(const Vector &p_array); + Error load_dds_from_buffer(const Vector &p_array); Error load_svg_from_buffer(const Vector &p_array, float scale = 1.0); Error load_svg_from_string(const String &p_svg_str, float scale = 1.0); diff --git a/doc/classes/Image.xml b/doc/classes/Image.xml index 4421318be701..9f75407fe45e 100644 --- a/doc/classes/Image.xml +++ b/doc/classes/Image.xml @@ -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. + + + + + Loads an image from the binary contents of a DDS file. + + diff --git a/modules/dds/image_loader_dds.cpp b/modules/dds/image_loader_dds.cpp new file mode 100644 index 000000000000..80fad1ecb65e --- /dev/null +++ b/modules/dds/image_loader_dds.cpp @@ -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 _dds_mem_loader_func(const uint8_t *p_buffer, int p_buffer_len) { + Ref memfile; + memfile.instantiate(); + Error open_memfile_error = memfile->open_custom(p_buffer, p_buffer_len); + ERR_FAIL_COND_V_MSG(open_memfile_error, Ref(), "Could not create memfile for DDS image buffer."); + + Ref img; + img.instantiate(); + Error load_error = ImageLoaderDDS().load_image(img, memfile, false, 1.0f); + ERR_FAIL_COND_V_MSG(load_error, Ref(), "Failed to load DDS image."); + return img; +} + +Error ImageLoaderDDS::load_layer(Ref p_layer, Ref 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 &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> &p_layers, Ref 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 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 p_image, Ref f, BitField p_flags, float p_scale) { + Vector> 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 *p_extensions) const { + p_extensions->push_back("dds"); +} + +bool ImageLoaderDDS::should_import(const String &p_resource_type) const { + return false; +} diff --git a/modules/dds/image_loader_dds.h b/modules/dds/image_loader_dds.h new file mode 100644 index 000000000000..4e285da15e8e --- /dev/null +++ b/modules/dds/image_loader_dds.h @@ -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 p_layer, Ref 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 &r_src_data); + static Error load_image_layers(Vector> &p_layers, Ref p_file, uint32_t *r_dds_type = nullptr); + + virtual Error load_image(Ref p_image, Ref f, BitField p_flags, float p_scale); + virtual void get_recognized_extensions(List *p_extensions) const; + virtual bool should_import(const String &p_resource_type) const; + ImageLoaderDDS(); +}; + +#endif // IMAGE_LOADER_DDS_H diff --git a/modules/dds/register_types.cpp b/modules/dds/register_types.cpp index d336269eb379..b4d406eda9db 100644 --- a/modules/dds/register_types.cpp +++ b/modules/dds/register_types.cpp @@ -30,9 +30,11 @@ #include "register_types.h" +#include "image_loader_dds.h" #include "texture_loader_dds.h" static Ref resource_loader_dds; +static Ref 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(); } diff --git a/modules/dds/texture_loader_dds.cpp b/modules/dds/texture_loader_dds.cpp index 667de70d1309..ed023a326f34 100644 --- a/modules/dds/texture_loader_dds.cpp +++ b/modules/dds/texture_loader_dds.cpp @@ -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 _dds_load_layer(Ref 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 &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(), "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(), "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 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 ResourceFormatDDS::load(const String &p_path, const String &p_orig ERR_FAIL_COND_V_MSG(err != OK, Ref(), 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(), 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(), 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(), vformat("Unrecognized or unsupported color layout in DDS '%s'.", p_path)); - } - - if (!(flags & DDSD_MIPMAPCOUNT)) { - mipmaps = 1; - } - - Vector src_data; - Vector> 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(), vformat("Unable to read layers in DDS texture file '%s'.", p_path)); + + uint32_t layer_count = images.size(); - if ((dds_type & DDST_TYPE_MASK) == DDST_2D) { - if (dds_type & DDST_ARRAY) { + if ((dds_type & ImageLoaderDDS::DDST_TYPE_MASK) == ImageLoaderDDS::DDST_2D) { + if (dds_type & ImageLoaderDDS::DDST_ARRAY) { Ref texture = memnew(Texture2DArray()); texture->create_from_images(images); @@ -780,10 +79,10 @@ Ref 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()); - if (dds_type & DDST_ARRAY) { + if (dds_type & ImageLoaderDDS::DDST_ARRAY) { Ref texture = memnew(CubemapArray()); texture->create_from_images(images); @@ -804,9 +103,9 @@ Ref 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 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; diff --git a/tests/core/io/test_image.h b/tests/core/io/test_image.h index fb56d181673a..4685ad1efb31 100644 --- a/tests/core/io/test_image.h +++ b/tests/core/io/test_image.h @@ -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_dds = memnew(Image()); + Ref 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_jpg = memnew(Image()); diff --git a/tests/data/images/icon.dds b/tests/data/images/icon.dds new file mode 100644 index 0000000000000000000000000000000000000000..8a9de402cbf13d8d031586d450e0e0cb48df294d GIT binary patch literal 87536 zcmeHw4|JT>b?2Lr1d<$ML#!4O8bptTZMjAvv5ZX(9xKzc$e9Gu%xEUAYH3sNS?ZFF z+Qgc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