diff --git a/core/io/image.cpp b/core/io/image.cpp index 45f9599cbe38..96b3d4bb73a7 100644 --- a/core/io/image.cpp +++ b/core/io/image.cpp @@ -89,11 +89,13 @@ SavePNGFunc Image::save_png_func = nullptr; SaveJPGFunc Image::save_jpg_func = nullptr; SaveEXRFunc Image::save_exr_func = nullptr; SaveWebPFunc Image::save_webp_func = nullptr; +SaveDDSFunc Image::save_dds_func = nullptr; SavePNGBufferFunc Image::save_png_buffer_func = nullptr; SaveJPGBufferFunc Image::save_jpg_buffer_func = nullptr; SaveEXRBufferFunc Image::save_exr_buffer_func = nullptr; SaveWebPBufferFunc Image::save_webp_buffer_func = nullptr; +SaveDDSBufferFunc Image::save_dds_buffer_func = nullptr; // External loader function pointers. @@ -105,6 +107,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. @@ -2587,7 +2590,6 @@ Vector Image::save_jpg_to_buffer(float p_quality) const { return save_jpg_buffer_func(Ref((Image *)this), p_quality); } - Error Image::save_exr(const String &p_path, bool p_grayscale) const { if (save_exr_func == nullptr) { return ERR_UNAVAILABLE; @@ -2603,6 +2605,21 @@ Vector Image::save_exr_to_buffer(bool p_grayscale) const { return save_exr_buffer_func(Ref((Image *)this), p_grayscale); } +Error Image::save_dds(const String &p_path) const { + if (save_dds_func == nullptr) { + return ERR_UNAVAILABLE; + } + + return save_dds_func(p_path, Ref((Image *)this)); +} + +Vector Image::save_dds_to_buffer() const { + if (save_dds_buffer_func == nullptr) { + return Vector(); + } + return save_dds_buffer_func(Ref((Image *)this)); +} + Error Image::save_webp(const String &p_path, const bool p_lossy, const float p_quality) const { if (save_webp_func == nullptr) { return ERR_UNAVAILABLE; @@ -3524,6 +3541,9 @@ void Image::_bind_methods() { ClassDB::bind_method(D_METHOD("save_jpg_to_buffer", "quality"), &Image::save_jpg_to_buffer, DEFVAL(0.75)); ClassDB::bind_method(D_METHOD("save_exr", "path", "grayscale"), &Image::save_exr, DEFVAL(false)); ClassDB::bind_method(D_METHOD("save_exr_to_buffer", "grayscale"), &Image::save_exr_to_buffer, DEFVAL(false)); + ClassDB::bind_method(D_METHOD("save_dds", "path"), &Image::save_dds); + ClassDB::bind_method(D_METHOD("save_dds_to_buffer"), &Image::save_dds_to_buffer); + ClassDB::bind_method(D_METHOD("save_webp", "path", "lossy", "quality"), &Image::save_webp, DEFVAL(false), DEFVAL(0.75f)); ClassDB::bind_method(D_METHOD("save_webp_to_buffer", "lossy", "quality"), &Image::save_webp_to_buffer, DEFVAL(false), DEFVAL(0.75f)); @@ -3577,6 +3597,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)); @@ -4072,6 +4093,14 @@ Error Image::load_bmp_from_buffer(const Vector &p_array) { return _load_from_buffer(p_array, _bmp_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 Godot editor or export template binary with the `module_dds_enabled=yes` SCons option."); + return _load_from_buffer(p_array, _dds_mem_loader_func); +} + Error Image::load_svg_from_buffer(const Vector &p_array, float scale) { ERR_FAIL_NULL_V_MSG( _svg_scalable_mem_loader_func, diff --git a/core/io/image.h b/core/io/image.h index 992c43597df2..0addddebacc4 100644 --- a/core/io/image.h +++ b/core/io/image.h @@ -59,6 +59,9 @@ typedef Vector (*SaveWebPBufferFunc)(const Ref &p_img, const boo typedef Error (*SaveEXRFunc)(const String &p_path, const Ref &p_img, bool p_grayscale); typedef Vector (*SaveEXRBufferFunc)(const Ref &p_img, bool p_grayscale); +typedef Error (*SaveDDSFunc)(const String &p_path, const Ref &p_img); +typedef Vector (*SaveDDSBufferFunc)(const Ref &p_img); + class Image : public Resource { GDCLASS(Image, Resource); @@ -186,10 +189,12 @@ class Image : public Resource { static SaveJPGFunc save_jpg_func; static SaveEXRFunc save_exr_func; static SaveWebPFunc save_webp_func; + static SaveDDSFunc save_dds_func; static SavePNGBufferFunc save_png_buffer_func; static SaveEXRBufferFunc save_exr_buffer_func; static SaveJPGBufferFunc save_jpg_buffer_func; static SaveWebPBufferFunc save_webp_buffer_func; + static SaveDDSBufferFunc save_dds_buffer_func; // External loader function pointers. @@ -201,6 +206,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. @@ -334,9 +340,11 @@ class Image : public Resource { static Ref load_from_file(const String &p_path); Error save_png(const String &p_path) const; Error save_jpg(const String &p_path, float p_quality = 0.75) const; + Error save_dds(const String &p_path) const; Vector save_png_to_buffer() const; Vector save_jpg_to_buffer(float p_quality = 0.75) const; Vector save_exr_to_buffer(bool p_grayscale = false) const; + Vector save_dds_to_buffer() const; Error save_exr(const String &p_path, bool p_grayscale = false) const; Error save_webp(const String &p_path, const bool p_lossy = false, const float p_quality = 0.75f) const; Vector save_webp_to_buffer(const bool p_lossy = false, const float p_quality = 0.75f) const; @@ -403,6 +411,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..af96f97ae213 100644 --- a/doc/classes/Image.xml +++ b/doc/classes/Image.xml @@ -346,6 +346,14 @@ [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. + [b]Note:[/b] This method is only available in engine builds with the DDS module enabled. By default, the DDS module is enabled, but it can be disabled at build-time using the [code]module_dds_enabled=no[/code] SCons option. + + @@ -458,6 +466,21 @@ Rotates the image by [code]180[/code] degrees. The width and height of the image must be greater than [code]1[/code]. + + + + + Saves the image as a DDS (DirectDraw Surface) file to [param path]. DDS is a container format that can store textures in various compression formats, such as DXT1, DXT5, or BC7. This function will return [constant ERR_UNAVAILABLE] if Godot was compiled without the DDS module. + [b]Note:[/b] The DDS module may be disabled in certain builds, which means [method save_dds] will return [constant ERR_UNAVAILABLE] when it is called from an exported project. + + + + + + Saves the image as a DDS (DirectDraw Surface) file to a byte array. DDS is a container format that can store textures in various compression formats, such as DXT1, DXT5, or BC7. This function will return an empty byte array if Godot was compiled without the DDS module. + [b]Note:[/b] The DDS module may be disabled in certain builds, which means [method save_dds_to_buffer] will return an empty byte array when it is called from an exported project. + + diff --git a/modules/dds/dds.h b/modules/dds/dds.h new file mode 100644 index 000000000000..4881b258efa9 --- /dev/null +++ b/modules/dds/dds.h @@ -0,0 +1,206 @@ +/**************************************************************************/ +/* 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 DDS_H +#define DDS_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, + DDS_HEADER_SIZE = 124, + DDS_PIXELFORMAT_SIZE = 32, + + DDSD_PITCH = 0x00000008, + DDSD_LINEARSIZE = 0x00080000, + DDSD_MIPMAPCOUNT = 0x00020000, + DDSD_CAPS = 0x1, + DDSD_HEIGHT = 0x2, + DDSD_WIDTH = 0x4, + DDSD_PIXELFORMAT = 0x1000, + 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 } +}; + +#endif // DDS_H diff --git a/modules/dds/image_saver_dds.cpp b/modules/dds/image_saver_dds.cpp new file mode 100644 index 000000000000..fbd3adb92fbc --- /dev/null +++ b/modules/dds/image_saver_dds.cpp @@ -0,0 +1,480 @@ +/**************************************************************************/ +/* image_saver_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_saver_dds.h" + +#include "core/io/file_access.h" +#include "core/io/stream_peer.h" +#include "dds.h" + +Error save_dds(const String &p_path, const Ref &p_img) { + Vector buffer = save_dds_buffer(p_img); + + Ref file = FileAccess::open(p_path, FileAccess::WRITE); + if (file.is_null()) { + return ERR_CANT_CREATE; + } + + file->store_buffer(buffer.ptr(), buffer.size()); + + return OK; +} + +enum DDSFormatType { + DDFT_BITMASK, + DDFT_FOURCC, + DDFT_DXGI, +}; + +DDSFormatType _dds_format_get_type(DDSFormat p_format) { + switch (p_format) { + case DDS_DXT1: + case DDS_DXT3: + case DDS_DXT5: + case DDS_ATI1: + case DDS_ATI2: + case DDS_R16F: + case DDS_RG16F: + case DDS_RGBA16F: + case DDS_R32F: + case DDS_RG32F: + case DDS_RGBA32F: + return DDFT_FOURCC; + + case DDS_BC6S: + case DDS_BC6U: + case DDS_BC7: + case DDS_RGB9E5: + case DDS_RGB32F: + return DDFT_DXGI; + + default: + return DDFT_BITMASK; + } +} + +DDSFormat _image_format_to_dds_format(Image::Format p_image_format) { + switch (p_image_format) { + case Image::FORMAT_RGBAF: { + return DDS_RGBA32F; + } + case Image::FORMAT_RGBF: { + return DDS_RGB32F; + } + case Image::FORMAT_RGBAH: { + return DDS_RGBA16F; + } + case Image::FORMAT_RGF: { + return DDS_RG32F; + } + case Image::FORMAT_RGBA8: { + return DDS_RGBA8; + } + case Image::FORMAT_RGH: { + return DDS_RG16F; + } + case Image::FORMAT_RF: { + return DDS_R32F; + } + case Image::FORMAT_L8: + case Image::FORMAT_R8: { + return DDS_LUMINANCE; + } + case Image::FORMAT_RH: { + return DDS_R16F; + } + case Image::FORMAT_LA8: + case Image::FORMAT_RG8: { + return DDS_LUMINANCE_ALPHA; + } + case Image::FORMAT_RGBE9995: { + return DDS_RGB9E5; + } + case Image::FORMAT_DXT1: { + return DDS_DXT1; + } + case Image::FORMAT_DXT3: { + return DDS_DXT3; + } + case Image::FORMAT_DXT5: { + return DDS_DXT5; + } + case Image::FORMAT_RGTC_R: { + return DDS_ATI1; + } + case Image::FORMAT_RGTC_RG: { + return DDS_ATI2; + } + case Image::FORMAT_RGB8: { + return DDS_RGB8; + } + case Image::FORMAT_BPTC_RGBFU: { + return DDS_BC6U; + } + case Image::FORMAT_BPTC_RGBF: { + return DDS_BC6S; + } + case Image::FORMAT_BPTC_RGBA: { + return DDS_BC7; + } + default: { + return DDS_MAX; + } + } +} + +uint32_t _image_format_to_fourcc_format(Image::Format p_format) { + switch (p_format) { + case Image::FORMAT_DXT1: + return DDFCC_DXT1; + case Image::FORMAT_DXT3: + return DDFCC_DXT3; + case Image::FORMAT_DXT5: + return DDFCC_DXT5; + case Image::FORMAT_RGTC_R: + return DDFCC_ATI1; + case Image::FORMAT_RGTC_RG: + return DDFCC_ATI2; + case Image::FORMAT_RF: + return DDFCC_R32F; + case Image::FORMAT_RGF: + return DDFCC_RG32F; + case Image::FORMAT_RGBAF: + return DDFCC_RGBA32F; + case Image::FORMAT_RH: + return DDFCC_R16F; + case Image::FORMAT_RGH: + return DDFCC_RG16F; + case Image::FORMAT_RGBAH: + return DDFCC_RGBA16F; + + default: + return 0; + } +} + +uint32_t _image_format_to_dxgi_format(Image::Format p_format) { + switch (p_format) { + case Image::FORMAT_DXT1: + return DXGI_BC1_UNORM; + case Image::FORMAT_DXT3: + return DXGI_BC2_UNORM; + case Image::FORMAT_DXT5: + return DXGI_BC3_UNORM; + case Image::FORMAT_RGTC_R: + return DXGI_BC4_UNORM; + case Image::FORMAT_RGTC_RG: + return DXGI_BC5_UNORM; + case Image::FORMAT_BPTC_RGBFU: + return DXGI_BC6H_UF16; + case Image::FORMAT_BPTC_RGBF: + return DXGI_BC6H_SF16; + case Image::FORMAT_BPTC_RGBA: + return DXGI_BC7_UNORM; + case Image::FORMAT_RF: + return DXGI_R32_FLOAT; + case Image::FORMAT_RGF: + return DXGI_R32G32_FLOAT; + case Image::FORMAT_RGBF: + return DXGI_R32G32B32_FLOAT; + case Image::FORMAT_RGBAF: + return DXGI_R32G32B32A32_FLOAT; + case Image::FORMAT_RH: + return DXGI_R16_FLOAT; + case Image::FORMAT_RGH: + return DXGI_R16G16_FLOAT; + case Image::FORMAT_RGBAH: + return DXGI_R16G16B16A16_FLOAT; + case Image::FORMAT_RGBE9995: + return DXGI_R9G9B9E5; + + default: + return 0; + } +} + +void _get_dds_pixel_bitmask(Image::Format p_format, uint32_t &r_bit_count, uint32_t &r_red_mask, uint32_t &r_green_mask, uint32_t &r_blue_mask, uint32_t &r_alpha_mask) { + switch (p_format) { + case Image::FORMAT_R8: + case Image::FORMAT_L8: { + r_bit_count = 8; + r_red_mask = 0xff; + r_green_mask = 0; + r_blue_mask = 0; + r_alpha_mask = 0; + } break; + case Image::FORMAT_RG8: + case Image::FORMAT_LA8: { + r_bit_count = 16; + r_red_mask = 0xff; + r_green_mask = 0; + r_blue_mask = 0; + r_alpha_mask = 0xff00; + } break; + case Image::FORMAT_RGB8: { + // BGR8 + r_bit_count = 24; + r_red_mask = 0xff0000; + r_green_mask = 0xff00; + r_blue_mask = 0xff; + r_alpha_mask = 0; + } break; + case Image::FORMAT_RGBA8: { + r_bit_count = 32; + r_red_mask = 0xff; + r_green_mask = 0xff00; + r_blue_mask = 0xff0000; + r_alpha_mask = 0xff000000; + } break; + case Image::FORMAT_RGBA4444: { + // BGRA4444 + r_bit_count = 16; + r_red_mask = 0xf00; + r_green_mask = 0xf0; + r_blue_mask = 0xf; + r_alpha_mask = 0xf000; + } break; + case Image::FORMAT_RGB565: { + // BGR565 + r_bit_count = 16; + r_red_mask = 0xf800; + r_green_mask = 0x7e0; + r_blue_mask = 0x1f; + r_alpha_mask = 0; + } break; + + default: { + r_bit_count = 0; + r_red_mask = 0; + r_green_mask = 0; + r_blue_mask = 0; + r_alpha_mask = 0; + } break; + } +} + +Vector save_dds_buffer(const Ref &p_img) { + Ref stream_buffer; + stream_buffer.instantiate(); + + Ref image = p_img; + + stream_buffer->put_32(DDS_MAGIC); + stream_buffer->put_32(DDS_HEADER_SIZE); + + uint32_t flags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT | DDSD_PITCH | DDSD_LINEARSIZE; + + if (image->has_mipmaps()) { + flags |= DDSD_MIPMAPCOUNT; + } + + stream_buffer->put_32(flags); + + uint32_t height = image->get_height(); + stream_buffer->put_32(height); + + uint32_t width = image->get_width(); + stream_buffer->put_32(width); + + DDSFormat dds_format = _image_format_to_dds_format(image->get_format()); + const DDSFormatInfo &info = dds_format_info[dds_format]; + + uint32_t depth = 1; // Default depth for 2D textures + + uint32_t pitch; + if (info.compressed) { + pitch = ((MAX(info.divisor, width) + info.divisor - 1) / info.divisor) * ((MAX(info.divisor, height) + info.divisor - 1) / info.divisor) * info.block_size; + } else { + pitch = width * info.block_size; + } + + stream_buffer->put_32(pitch); + stream_buffer->put_32(depth); + + uint32_t mipmaps = image->has_mipmaps() ? (image->get_mipmap_count() + 1) : 1; + stream_buffer->put_32(mipmaps); + + uint32_t reserved = 0; + for (int i = 0; i < 11; i++) { + stream_buffer->put_32(reserved); + } + + stream_buffer->put_32(DDS_PIXELFORMAT_SIZE); + + uint32_t pf_flags = 0; + + if (info.compressed) { + pf_flags = DDPF_FOURCC; + } else { + pf_flags = DDPF_RGB; + + if (image->get_format() == Image::FORMAT_LA8 || image->get_format() == Image::FORMAT_RG8 || image->get_format() == Image::FORMAT_RGBA8 || image->get_format() == Image::FORMAT_RGBA4444) { + pf_flags |= DDPF_ALPHAPIXELS; + } + } + + stream_buffer->put_32(pf_flags); + + bool needs_pixeldata_swap = false; + + DDSFormatType format_type = _dds_format_get_type(dds_format); + + if (format_type == DDFT_BITMASK) { + // Uncompressed bitmasked. + stream_buffer->put_32(0); // FourCC + + uint32_t bit_count, r_mask, g_mask, b_mask, a_mask; + _get_dds_pixel_bitmask(image->get_format(), bit_count, r_mask, g_mask, b_mask, a_mask); + + stream_buffer->put_32(bit_count); + stream_buffer->put_32(r_mask); + stream_buffer->put_32(g_mask); + stream_buffer->put_32(b_mask); + stream_buffer->put_32(a_mask); + + if (image->get_format() == Image::FORMAT_RGBA4444 || image->get_format() == Image::FORMAT_RGB565 || image->get_format() == Image::FORMAT_RGB8) { + needs_pixeldata_swap = true; + } + } else if (format_type == DDFT_FOURCC) { + // FourCC. + uint32_t fourcc = _image_format_to_fourcc_format(image->get_format()); + stream_buffer->put_32(fourcc); + + stream_buffer->put_32(0); // Bit count + stream_buffer->put_32(0); // R Bitmask + stream_buffer->put_32(0); // G Bitmask + stream_buffer->put_32(0); // B Bitmask + stream_buffer->put_32(0); // A Bitmask + } else { + // DXGI format and DX10 header. + stream_buffer->put_32(DDFCC_DX10); + + stream_buffer->put_32(0); // Bit count + stream_buffer->put_32(0); // R Bitmask + stream_buffer->put_32(0); // G Bitmask + stream_buffer->put_32(0); // B Bitmask + stream_buffer->put_32(0); // A Bitmask + } + + uint32_t caps1 = info.compressed ? DDSD_LINEARSIZE : DDSD_PITCH; + stream_buffer->put_32(caps1); + + stream_buffer->put_32(0); // Caps2 + stream_buffer->put_32(0); // Caps3 + stream_buffer->put_32(0); // Caps4 + stream_buffer->put_32(0); // Reserved 2 + + if (format_type == DDFT_DXGI) { + // DX10 header. + uint32_t dxgi_format = _image_format_to_dxgi_format(image->get_format()); + stream_buffer->put_32(dxgi_format); + stream_buffer->put_32(DX10D_2D); + stream_buffer->put_32(0); // Misc flags 1 + stream_buffer->put_32(1); // Array size + stream_buffer->put_32(0); // Misc flags 2 + } + + for (uint32_t mip_i = 0; mip_i < mipmaps; mip_i++) { + uint32_t mip_width = MAX(1u, width >> mip_i); + uint32_t mip_height = MAX(1u, height >> mip_i); + + uint32_t expected_size = 0; + if (info.compressed) { + uint32_t blocks_x = (mip_width + info.divisor - 1) / info.divisor; + uint32_t blocks_y = (mip_height + info.divisor - 1) / info.divisor; + expected_size = blocks_x * blocks_y * info.block_size; + } else { + expected_size = mip_width * mip_height * info.block_size; + } + + if (needs_pixeldata_swap) { + // The image's channels need to be swapped. + Ref mip_image = image->get_image_from_mipmap(mip_i); + Vector data = mip_image->get_data(); + + ERR_FAIL_COND_V_MSG(data.size() != expected_size, Vector(), + "Image data size mismatch for mipmap level " + itos(mip_i) + + ". Expected size: " + itos(expected_size) + ", actual size: " + itos(data.size()) + "."); + + if (mip_image->get_format() == Image::FORMAT_RGBA4444) { + // RGBA4 to BGRA4 + const int64_t data_size = data.size(); + uint8_t *wb = data.ptrw(); + + for (int64_t data_i = 0; data_i < data_size; data_i += 2) { + uint8_t r = wb[data_i + 0] & 0x0F; + uint8_t b = wb[data_i + 1] & 0x0F; + + wb[data_i + 0] = (wb[data_i + 0] & 0xF0) | b; + wb[data_i + 1] = (wb[data_i + 1] & 0xF0) | r; + } + } else if (mip_image->get_format() == Image::FORMAT_RGB565) { + // RGB565 to BGR565 + const int64_t data_size = data.size(); + uint8_t *wb = data.ptrw(); + + for (int64_t data_i = 0; data_i < data_size; data_i += 2) { + uint16_t px = wb[data_i] | (wb[data_i + 1] << 8); + + uint8_t r = (px >> 11) & 0x1F; + uint8_t g = (px >> 5) & 0x3F; + uint8_t b = px & 0x1F; + + uint16_t out_px = (b << 11) | (g << 5) | r; + + wb[data_i + 0] = out_px & 0xFF; + wb[data_i + 1] = (out_px >> 8) & 0xFF; + } + } else if (mip_image->get_format() == Image::FORMAT_RGB8) { + // RGB8 to BGR8 + const int64_t data_size = data.size(); + uint8_t *wb = data.ptrw(); + + for (int64_t data_i = 0; data_i < data_size; data_i += 3) { + SWAP(wb[data_i], wb[data_i + 2]); + } + } + + stream_buffer->put_data(data.ptr(), data.size()); + } else { + int64_t ofs, size; + + image->get_mipmap_offset_and_size(mip_i, ofs, size); + + ERR_FAIL_COND_V_MSG(size != expected_size, Vector(), + "Image data size mismatch for mipmap level " + itos(mip_i) + + ". Expected size: " + itos(expected_size) + ", actual size: " + itos(size) + "."); + + stream_buffer->put_data(image->ptr() + ofs, size); + } + } + + return stream_buffer->get_data_array(); +} diff --git a/modules/dds/image_saver_dds.h b/modules/dds/image_saver_dds.h new file mode 100644 index 000000000000..872e44c0927e --- /dev/null +++ b/modules/dds/image_saver_dds.h @@ -0,0 +1,41 @@ +/**************************************************************************/ +/* image_saver_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_SAVER_DDS_H +#define IMAGE_SAVER_DDS_H + +#include "core/io/file_access_memory.h" +#include "core/io/image.h" +#include "core/io/image_loader.h" + +Error save_dds(const String &p_path, const Ref &p_img); +Vector save_dds_buffer(const Ref &p_img); + +#endif // IMAGE_SAVER_DDS_H diff --git a/modules/dds/register_types.cpp b/modules/dds/register_types.cpp index d336269eb379..d9b641d2c0c1 100644 --- a/modules/dds/register_types.cpp +++ b/modules/dds/register_types.cpp @@ -30,6 +30,7 @@ #include "register_types.h" +#include "image_saver_dds.h" #include "texture_loader_dds.h" static Ref resource_loader_dds; @@ -39,6 +40,9 @@ void initialize_dds_module(ModuleInitializationLevel p_level) { return; } + Image::save_dds_func = save_dds; + Image::save_dds_buffer_func = save_dds_buffer; + resource_loader_dds.instantiate(); ResourceLoader::add_resource_format_loader(resource_loader_dds); } @@ -50,4 +54,6 @@ void uninitialize_dds_module(ModuleInitializationLevel p_level) { ResourceLoader::remove_resource_format_loader(resource_loader_dds); resource_loader_dds.unref(); + Image::save_dds_func = nullptr; + Image::save_dds_buffer_func = nullptr; } diff --git a/modules/dds/texture_loader_dds.cpp b/modules/dds/texture_loader_dds.cpp index 667de70d1309..54eb782dd4e2 100644 --- a/modules/dds/texture_loader_dds.cpp +++ b/modules/dds/texture_loader_dds.cpp @@ -31,170 +31,11 @@ #include "texture_loader_dds.h" #include "core/io/file_access.h" +#include "core/io/file_access_memory.h" +#include "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) { +DDSFormat _dxgi_to_dds_format(uint32_t p_dxgi_format) { switch (p_dxgi_format) { case DXGI_R32G32B32A32_FLOAT: { return DDS_RGBA32F; @@ -543,32 +384,92 @@ static Ref _dds_load_layer(Ref p_file, DDSFormat p_dds_format 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; - } +static Vector> _dds_load_images(Ref f, DDSFormat dds_format, uint32_t width, uint32_t height, uint32_t mipmaps, uint32_t pitch, uint32_t flags, uint32_t layer_count) { + Vector src_data; + Vector> images; + images.resize(layer_count); - Error err; - Ref f = FileAccess::open(p_path, FileAccess::READ, &err); - if (f.is_null()) { - return Ref(); + 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); } - Ref fref(f); - if (r_error) { - *r_error = ERR_FILE_CORRUPT; + return images; +} + +static Ref _dds_create_texture(Vector> images, uint32_t dds_type, uint32_t width, uint32_t height, uint32_t layer_count, uint32_t mipmaps, Error *r_error) { + if ((dds_type & DDST_TYPE_MASK) == DDST_2D) { + if (dds_type & DDST_ARRAY) { + Ref texture = memnew(Texture2DArray()); + texture->create_from_images(images); + + if (r_error) { + *r_error = OK; + } + + return texture; + + } else { + if (r_error) { + *r_error = OK; + } + + return ImageTexture::create_from_image(images[0]); + } + + } else if ((dds_type & DDST_TYPE_MASK) == DDST_CUBEMAP) { + ERR_FAIL_COND_V(layer_count % 6 != 0, Ref()); + + if (dds_type & DDST_ARRAY) { + Ref texture = memnew(CubemapArray()); + texture->create_from_images(images); + + if (r_error) { + *r_error = OK; + } + + return texture; + + } else { + Ref texture = memnew(Cubemap()); + texture->create_from_images(images); + + if (r_error) { + *r_error = OK; + } + + return texture; + } + + } else if ((dds_type & DDST_TYPE_MASK) == DDST_3D) { + Ref texture = memnew(ImageTexture3D()); + texture->create(images[0]->get_format(), width, height, layer_count, mipmaps > 1, images); + + if (r_error) { + *r_error = OK; + } + + return texture; } - ERR_FAIL_COND_V_MSG(err != OK, Ref(), vformat("Unable to open DDS texture file '%s'.", p_path)); + return Ref(); +} + +static Ref _dds_create_texture_from_images(Vector> images, DDSFormat dds_format, uint32_t width, uint32_t height, uint32_t mipmaps, uint32_t pitch, uint32_t flags, uint32_t layer_count, uint32_t dds_type, Error *r_error) { + return _dds_create_texture(images, dds_type, width, height, layer_count, mipmaps, r_error); +} + +static Vector> _dds_load_images_from_buffer(Ref f, DDSFormat &dds_format, uint32_t &width, uint32_t &height, uint32_t &mipmaps, uint32_t &pitch, uint32_t &flags, uint32_t &layer_count, uint32_t &dds_type, const String &p_path = "") { + ERR_FAIL_COND_V_MSG(f.is_null(), Vector>(), vformat("Empty DDS texture file.")); + ERR_FAIL_COND_V_MSG(!f->get_length(), Vector>(), vformat("Empty DDS texture file.")); 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(); + flags = f->get_32(); + height = f->get_32(); + width = f->get_32(); + pitch = f->get_32(); uint32_t depth = f->get_32(); - uint32_t mipmaps = f->get_32(); + mipmaps = f->get_32(); // Skip reserved. for (int i = 0; i < 11; i++) { @@ -579,7 +480,7 @@ Ref ResourceFormatDDS::load(const String &p_path, const String &p_orig // 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)); + ERR_FAIL_V_MSG(Vector>(), vformat("Invalid or unsupported DDS texture file '%s'.", p_path)); } /* uint32_t format_size = */ f->get_32(); @@ -603,8 +504,8 @@ Ref ResourceFormatDDS::load(const String &p_path, const String &p_orig f->seek(128); } - uint32_t layer_count = 1; - uint32_t dds_type = DDST_2D; + layer_count = 1; + dds_type = DDST_2D; if (caps_2 & DDSC2_CUBEMAP) { dds_type = DDST_CUBEMAP; @@ -615,7 +516,7 @@ Ref ResourceFormatDDS::load(const String &p_path, const String &p_orig layer_count = depth; } - DDSFormat dds_format = DDS_MAX; + dds_format = DDS_MAX; if (format_flags & DDPF_FOURCC) { // FourCC formats. @@ -679,7 +580,7 @@ Ref ResourceFormatDDS::load(const String &p_path, const String &p_orig } break; default: { - ERR_FAIL_V_MSG(Ref(), vformat("Unrecognized or unsupported FourCC in DDS '%s'.", p_path)); + ERR_FAIL_V_MSG(Vector>(), vformat("Unrecognized or unsupported FourCC in DDS '%s'.", p_path)); } } @@ -745,77 +646,44 @@ Ref ResourceFormatDDS::load(const String &p_path, const String &p_orig // 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)); + ERR_FAIL_V_MSG(Vector>(), 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); + return _dds_load_images(f, dds_format, width, height, mipmaps, pitch, flags, layer_count); +} - 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); +static Ref _dds_load_from_buffer(Ref f, Error *r_error, const String &p_path = "") { + if (r_error) { + *r_error = ERR_FILE_CORRUPT; } - if ((dds_type & DDST_TYPE_MASK) == DDST_2D) { - if (dds_type & DDST_ARRAY) { - Ref texture = memnew(Texture2DArray()); - texture->create_from_images(images); - - if (r_error) { - *r_error = OK; - } - - return texture; - - } else { - if (r_error) { - *r_error = OK; - } - - return ImageTexture::create_from_image(images[0]); - } - - } else if ((dds_type & DDST_TYPE_MASK) == DDST_CUBEMAP) { - ERR_FAIL_COND_V(layer_count % 6 != 0, Ref()); - - if (dds_type & DDST_ARRAY) { - Ref texture = memnew(CubemapArray()); - texture->create_from_images(images); - - if (r_error) { - *r_error = OK; - } - - return texture; + DDSFormat dds_format; + uint32_t width, height, mipmaps, pitch, flags, layer_count, dds_type; - } else { - Ref texture = memnew(Cubemap()); - texture->create_from_images(images); - - if (r_error) { - *r_error = OK; - } - - return texture; - } - - } else if ((dds_type & DDST_TYPE_MASK) == DDST_3D) { - Ref texture = memnew(ImageTexture3D()); - texture->create(images[0]->get_format(), width, height, layer_count, mipmaps > 1, images); + Vector> images = _dds_load_images_from_buffer(f, dds_format, width, height, mipmaps, pitch, flags, layer_count, dds_type, p_path); + return _dds_create_texture_from_images(images, dds_format, width, height, mipmaps, pitch, flags, layer_count, dds_type, r_error); +} - if (r_error) { - *r_error = OK; - } +static Ref _dds_load_from_file(const String &p_path, Error *r_error) { + if (r_error) { + *r_error = ERR_CANT_OPEN; + } - return texture; + Error err; + Ref f = FileAccess::open(p_path, FileAccess::READ, &err); + if (f.is_null()) { + return Ref(); } - return Ref(); + return _dds_load_from_buffer(f, r_error, p_path); +} + +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) { + return _dds_load_from_file(p_path, r_error); } void ResourceFormatDDS::get_recognized_extensions(List *p_extensions) const { @@ -832,3 +700,24 @@ String ResourceFormatDDS::get_resource_type(const String &p_path) const { } return ""; } + +Ref load_mem_dds(const uint8_t *p_dds, int p_size) { + ERR_FAIL_NULL_V(p_dds, Ref()); + ERR_FAIL_COND_V(!p_size, Ref()); + Ref memfile; + memfile.instantiate(); + Error open_memfile_error = memfile->open_custom(p_dds, p_size); + ERR_FAIL_COND_V_MSG(open_memfile_error, Ref(), "Could not create memfile for DDS image buffer."); + + DDSFormat dds_format; + uint32_t width, height, mipmaps, pitch, flags, layer_count, dds_type; + + Vector> images = _dds_load_images_from_buffer(memfile, dds_format, width, height, mipmaps, pitch, flags, layer_count, dds_type); + ERR_FAIL_COND_V_MSG(images.is_empty(), Ref(), "Failed to load DDS image."); + + return images[0]; +} + +ResourceFormatDDS::ResourceFormatDDS() { + Image::_dds_mem_loader_func = load_mem_dds; +} diff --git a/modules/dds/texture_loader_dds.h b/modules/dds/texture_loader_dds.h index ce5b7f40450d..9f1248b1b5e3 100644 --- a/modules/dds/texture_loader_dds.h +++ b/modules/dds/texture_loader_dds.h @@ -40,6 +40,7 @@ class ResourceFormatDDS : public ResourceFormatLoader { virtual bool handles_type(const String &p_type) const override; virtual String get_resource_type(const String &p_path) const override; + ResourceFormatDDS(); virtual ~ResourceFormatDDS() {} }; diff --git a/tests/scene/test_dds_saver.h b/tests/scene/test_dds_saver.h new file mode 100644 index 000000000000..1f1dfb1e51b9 --- /dev/null +++ b/tests/scene/test_dds_saver.h @@ -0,0 +1,143 @@ +/**************************************************************************/ +/* test_dds_saver.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 TEST_DDS_SAVER_H +#define TEST_DDS_SAVER_H + +#include "modules/dds/image_saver_dds.h" + +#include "core/config/project_settings.h" +#include "core/io/dir_access.h" +#include "core/io/image.h" +#include "tests/core/config/test_project_settings.h" +#include "tests/test_macros.h" +#include "tests/test_utils.h" + +namespace TestDDSSaver { +void init(const String &p_test, const String &p_copy_target = String()) { + Error err; + // Setup project settings since it's needed for the import process. + String project_folder = TestUtils::get_temp_path(p_test.get_file().get_basename()); + Ref da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); + da->make_dir_recursive(project_folder.path_join(".godot").path_join("imported")); + // Initialize res:// to `project_folder`. + TestProjectSettingsInternalsAccessor::resource_path() = project_folder; + err = ProjectSettings::get_singleton()->setup(project_folder, String(), true); + + if (p_copy_target.is_empty()) { + return; + } + + // Copy all the necessary test data files to the res:// directory. + da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); + String test_data = String("tests/data").path_join(p_test); + da = DirAccess::open(test_data); + CHECK_MESSAGE(da.is_valid(), "Unable to open folder."); + da->list_dir_begin(); + for (String item = da->get_next(); !item.is_empty(); item = da->get_next()) { + if (!FileAccess::exists(test_data.path_join(item))) { + continue; + } + Ref output = FileAccess::open(p_copy_target.path_join(item), FileAccess::WRITE, &err); + CHECK_MESSAGE(err == OK, "Unable to open output file."); + output->store_buffer(FileAccess::get_file_as_bytes(test_data.path_join(item))); + output->close(); + } + da->list_dir_end(); +} + +// Metal rendering driver fails to run on Apple's software Metal implementation (paravirtual device) #101773 +#if !defined(MACOS_ENABLED) && !defined(LINUX_ENABLED) +TEST_CASE("[SceneTree][DDSSaver] Save DDS - Save valid image with mipmap") { + init("save_dds_valid_image_with_mipmap"); + Ref image = Image::create_empty(4, 4, false, Image::FORMAT_RGBA8); + image->fill(Color(1, 0, 0)); // Fill with red color + image->generate_mipmaps(); + image->compress_from_channels(Image::COMPRESS_S3TC, Image::USED_CHANNELS_RGBA); + Error err = save_dds("res://valid_image_with_mipmap.dds", image); + CHECK(err == OK); + + Ref loaded_image; + loaded_image.instantiate(); + Vector buffer = FileAccess::get_file_as_bytes("res://valid_image_with_mipmap.dds", &err); + CHECK(err == OK); + err = loaded_image->load_dds_from_buffer(buffer); + CHECK(err == OK); + Dictionary metrics = image->compute_image_metrics(loaded_image, false); + CHECK(metrics.size() > 0); + CHECK_MESSAGE(metrics.has("root_mean_squared"), "Metrics dictionary contains 'root_mean_squared'."); + float rms = metrics["root_mean_squared"]; + CHECK(rms == 0.0f); +} + +TEST_CASE("[SceneTree][DDSSaver] Save DDS - Save valid image with BPTC and S3TC compression") { + init("save_dds_valid_image_bptc_s3tc"); + Ref image_bptc = Image::create_empty(4, 4, false, Image::FORMAT_RGBA8); + image_bptc->fill(Color(0, 0, 1)); // Fill with blue color + image_bptc->compress_from_channels(Image::COMPRESS_BPTC, Image::USED_CHANNELS_RGBA); + Error err_bptc = image_bptc->save_dds("res://valid_image_bptc.dds"); + CHECK(err_bptc == OK); + + Ref image_s3tc = Image::create_empty(4, 4, false, Image::FORMAT_RGBA8); + image_s3tc->fill(Color(1, 1, 1)); // Fill with white color + image_s3tc->compress_from_channels(Image::COMPRESS_S3TC, Image::USED_CHANNELS_RGBA); + Error err_s3tc = image_s3tc->save_dds("res://valid_image_s3tc_combined.dds"); + CHECK(err_s3tc == OK); + + // Validate BPTC image + Ref loaded_image_bptc; + loaded_image_bptc.instantiate(); + Vector buffer_bptc = FileAccess::get_file_as_bytes("res://valid_image_bptc.dds", &err_bptc); + CHECK(err_bptc == OK); + err_bptc = loaded_image_bptc->load_dds_from_buffer(buffer_bptc); + CHECK(err_bptc == OK); + Dictionary metrics_bptc = image_bptc->compute_image_metrics(loaded_image_bptc, false); + CHECK(metrics_bptc.size() > 0); + CHECK_MESSAGE(metrics_bptc.has("root_mean_squared"), "Metrics dictionary contains 'root_mean_squared' for BPTC."); + float rms_bptc = metrics_bptc["root_mean_squared"]; + CHECK(rms_bptc == 0.0f); + + // Validate S3TC image + Ref loaded_image_s3tc; + loaded_image_s3tc.instantiate(); + Vector buffer_s3tc = FileAccess::get_file_as_bytes("res://valid_image_s3tc_combined.dds", &err_s3tc); + CHECK(err_s3tc == OK); + err_s3tc = loaded_image_s3tc->load_dds_from_buffer(buffer_s3tc); + CHECK(err_s3tc == OK); + Dictionary metrics_s3tc = image_s3tc->compute_image_metrics(loaded_image_s3tc, false); + CHECK(metrics_s3tc.size() > 0); + CHECK_MESSAGE(metrics_s3tc.has("root_mean_squared"), "Metrics dictionary contains 'root_mean_squared' for S3TC."); + float rms_s3tc = metrics_s3tc["root_mean_squared"]; + CHECK(rms_s3tc == 0.0f); +} +#endif // MACOS_ENABLED +} //namespace TestDDSSaver + +#endif // TEST_DDS_SAVER_H diff --git a/tests/test_main.cpp b/tests/test_main.cpp index af334dbbf4a6..d125e83942da 100644 --- a/tests/test_main.cpp +++ b/tests/test_main.cpp @@ -35,6 +35,7 @@ #ifdef TOOLS_ENABLED #include "editor/editor_paths.h" #include "editor/editor_settings.h" +#include "tests/scene/test_dds_saver.h" #endif // TOOLS_ENABLED #include "tests/core/config/test_project_settings.h"