Skip to content

Commit f98cddf

Browse files
committed
Merge pull request #101994 from fire/vsk-save-dds-4.4
Add DDS image load and save functionality
2 parents bb60b05 + 69b2812 commit f98cddf

File tree

10 files changed

+1154
-304
lines changed

10 files changed

+1154
-304
lines changed

core/io/image.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,13 @@ SavePNGFunc Image::save_png_func = nullptr;
8989
SaveJPGFunc Image::save_jpg_func = nullptr;
9090
SaveEXRFunc Image::save_exr_func = nullptr;
9191
SaveWebPFunc Image::save_webp_func = nullptr;
92+
SaveDDSFunc Image::save_dds_func = nullptr;
9293

9394
SavePNGBufferFunc Image::save_png_buffer_func = nullptr;
9495
SaveJPGBufferFunc Image::save_jpg_buffer_func = nullptr;
9596
SaveEXRBufferFunc Image::save_exr_buffer_func = nullptr;
9697
SaveWebPBufferFunc Image::save_webp_buffer_func = nullptr;
98+
SaveDDSBufferFunc Image::save_dds_buffer_func = nullptr;
9799

98100
// External loader function pointers.
99101

@@ -105,6 +107,7 @@ ImageMemLoadFunc Image::_tga_mem_loader_func = nullptr;
105107
ImageMemLoadFunc Image::_bmp_mem_loader_func = nullptr;
106108
ScalableImageMemLoadFunc Image::_svg_scalable_mem_loader_func = nullptr;
107109
ImageMemLoadFunc Image::_ktx_mem_loader_func = nullptr;
110+
ImageMemLoadFunc Image::_dds_mem_loader_func = nullptr;
108111

109112
// External VRAM compression function pointers.
110113

@@ -2603,6 +2606,21 @@ Vector<uint8_t> Image::save_exr_to_buffer(bool p_grayscale) const {
26032606
return save_exr_buffer_func(Ref<Image>((Image *)this), p_grayscale);
26042607
}
26052608

2609+
Error Image::save_dds(const String &p_path) const {
2610+
if (save_dds_func == nullptr) {
2611+
return ERR_UNAVAILABLE;
2612+
}
2613+
2614+
return save_dds_func(p_path, Ref<Image>((Image *)this));
2615+
}
2616+
2617+
Vector<uint8_t> Image::save_dds_to_buffer() const {
2618+
if (save_dds_buffer_func == nullptr) {
2619+
return Vector<uint8_t>();
2620+
}
2621+
return save_dds_buffer_func(Ref<Image>((Image *)this));
2622+
}
2623+
26062624
Error Image::save_webp(const String &p_path, const bool p_lossy, const float p_quality) const {
26072625
if (save_webp_func == nullptr) {
26082626
return ERR_UNAVAILABLE;
@@ -3524,6 +3542,9 @@ void Image::_bind_methods() {
35243542
ClassDB::bind_method(D_METHOD("save_jpg_to_buffer", "quality"), &Image::save_jpg_to_buffer, DEFVAL(0.75));
35253543
ClassDB::bind_method(D_METHOD("save_exr", "path", "grayscale"), &Image::save_exr, DEFVAL(false));
35263544
ClassDB::bind_method(D_METHOD("save_exr_to_buffer", "grayscale"), &Image::save_exr_to_buffer, DEFVAL(false));
3545+
ClassDB::bind_method(D_METHOD("save_dds", "path"), &Image::save_dds);
3546+
ClassDB::bind_method(D_METHOD("save_dds_to_buffer"), &Image::save_dds_to_buffer);
3547+
35273548
ClassDB::bind_method(D_METHOD("save_webp", "path", "lossy", "quality"), &Image::save_webp, DEFVAL(false), DEFVAL(0.75f));
35283549
ClassDB::bind_method(D_METHOD("save_webp_to_buffer", "lossy", "quality"), &Image::save_webp_to_buffer, DEFVAL(false), DEFVAL(0.75f));
35293550

@@ -3577,6 +3598,7 @@ void Image::_bind_methods() {
35773598
ClassDB::bind_method(D_METHOD("load_tga_from_buffer", "buffer"), &Image::load_tga_from_buffer);
35783599
ClassDB::bind_method(D_METHOD("load_bmp_from_buffer", "buffer"), &Image::load_bmp_from_buffer);
35793600
ClassDB::bind_method(D_METHOD("load_ktx_from_buffer", "buffer"), &Image::load_ktx_from_buffer);
3601+
ClassDB::bind_method(D_METHOD("load_dds_from_buffer", "buffer"), &Image::load_dds_from_buffer);
35803602

35813603
ClassDB::bind_method(D_METHOD("load_svg_from_buffer", "buffer", "scale"), &Image::load_svg_from_buffer, DEFVAL(1.0));
35823604
ClassDB::bind_method(D_METHOD("load_svg_from_string", "svg_str", "scale"), &Image::load_svg_from_string, DEFVAL(1.0));
@@ -4072,6 +4094,14 @@ Error Image::load_bmp_from_buffer(const Vector<uint8_t> &p_array) {
40724094
return _load_from_buffer(p_array, _bmp_mem_loader_func);
40734095
}
40744096

4097+
Error Image::load_dds_from_buffer(const Vector<uint8_t> &p_array) {
4098+
ERR_FAIL_NULL_V_MSG(
4099+
_dds_mem_loader_func,
4100+
ERR_UNAVAILABLE,
4101+
"The DDS module isn't enabled. Recompile the Godot editor or export template binary with the `module_dds_enabled=yes` SCons option.");
4102+
return _load_from_buffer(p_array, _dds_mem_loader_func);
4103+
}
4104+
40754105
Error Image::load_svg_from_buffer(const Vector<uint8_t> &p_array, float scale) {
40764106
ERR_FAIL_NULL_V_MSG(
40774107
_svg_scalable_mem_loader_func,

core/io/image.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ typedef Vector<uint8_t> (*SaveWebPBufferFunc)(const Ref<Image> &p_img, const boo
5858
typedef Error (*SaveEXRFunc)(const String &p_path, const Ref<Image> &p_img, bool p_grayscale);
5959
typedef Vector<uint8_t> (*SaveEXRBufferFunc)(const Ref<Image> &p_img, bool p_grayscale);
6060

61+
typedef Error (*SaveDDSFunc)(const String &p_path, const Ref<Image> &p_img);
62+
typedef Vector<uint8_t> (*SaveDDSBufferFunc)(const Ref<Image> &p_img);
63+
6164
class Image : public Resource {
6265
GDCLASS(Image, Resource);
6366

@@ -185,10 +188,12 @@ class Image : public Resource {
185188
static SaveJPGFunc save_jpg_func;
186189
static SaveEXRFunc save_exr_func;
187190
static SaveWebPFunc save_webp_func;
191+
static SaveDDSFunc save_dds_func;
188192
static SavePNGBufferFunc save_png_buffer_func;
189193
static SaveEXRBufferFunc save_exr_buffer_func;
190194
static SaveJPGBufferFunc save_jpg_buffer_func;
191195
static SaveWebPBufferFunc save_webp_buffer_func;
196+
static SaveDDSBufferFunc save_dds_buffer_func;
192197

193198
// External loader function pointers.
194199

@@ -200,6 +205,7 @@ class Image : public Resource {
200205
static ImageMemLoadFunc _bmp_mem_loader_func;
201206
static ScalableImageMemLoadFunc _svg_scalable_mem_loader_func;
202207
static ImageMemLoadFunc _ktx_mem_loader_func;
208+
static ImageMemLoadFunc _dds_mem_loader_func;
203209

204210
// External VRAM compression function pointers.
205211

@@ -333,9 +339,11 @@ class Image : public Resource {
333339
static Ref<Image> load_from_file(const String &p_path);
334340
Error save_png(const String &p_path) const;
335341
Error save_jpg(const String &p_path, float p_quality = 0.75) const;
342+
Error save_dds(const String &p_path) const;
336343
Vector<uint8_t> save_png_to_buffer() const;
337344
Vector<uint8_t> save_jpg_to_buffer(float p_quality = 0.75) const;
338345
Vector<uint8_t> save_exr_to_buffer(bool p_grayscale = false) const;
346+
Vector<uint8_t> save_dds_to_buffer() const;
339347
Error save_exr(const String &p_path, bool p_grayscale = false) const;
340348
Error save_webp(const String &p_path, const bool p_lossy = false, const float p_quality = 0.75f) const;
341349
Vector<uint8_t> save_webp_to_buffer(const bool p_lossy = false, const float p_quality = 0.75f) const;
@@ -402,6 +410,7 @@ class Image : public Resource {
402410
Error load_tga_from_buffer(const Vector<uint8_t> &p_array);
403411
Error load_bmp_from_buffer(const Vector<uint8_t> &p_array);
404412
Error load_ktx_from_buffer(const Vector<uint8_t> &p_array);
413+
Error load_dds_from_buffer(const Vector<uint8_t> &p_array);
405414

406415
Error load_svg_from_buffer(const Vector<uint8_t> &p_array, float scale = 1.0);
407416
Error load_svg_from_string(const String &p_svg_str, float scale = 1.0);

doc/classes/Image.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,14 @@
346346
[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.
347347
</description>
348348
</method>
349+
<method name="load_dds_from_buffer">
350+
<return type="int" enum="Error" />
351+
<param index="0" name="buffer" type="PackedByteArray" />
352+
<description>
353+
Loads an image from the binary contents of a DDS file.
354+
[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.
355+
</description>
356+
</method>
349357
<method name="load_from_file" qualifiers="static">
350358
<return type="Image" />
351359
<param index="0" name="path" type="String" />
@@ -458,6 +466,21 @@
458466
Rotates the image by [code]180[/code] degrees. The width and height of the image must be greater than [code]1[/code].
459467
</description>
460468
</method>
469+
<method name="save_dds" qualifiers="const">
470+
<return type="int" enum="Error" />
471+
<param index="0" name="path" type="String" />
472+
<description>
473+
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.
474+
[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.
475+
</description>
476+
</method>
477+
<method name="save_dds_to_buffer" qualifiers="const">
478+
<return type="PackedByteArray" />
479+
<description>
480+
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.
481+
[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.
482+
</description>
483+
</method>
461484
<method name="save_exr" qualifiers="const">
462485
<return type="int" enum="Error" />
463486
<param index="0" name="path" type="String" />

modules/dds/dds_enums.h

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
/**************************************************************************/
2+
/* dds_enums.h */
3+
/**************************************************************************/
4+
/* This file is part of: */
5+
/* GODOT ENGINE */
6+
/* https://godotengine.org */
7+
/**************************************************************************/
8+
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9+
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10+
/* */
11+
/* Permission is hereby granted, free of charge, to any person obtaining */
12+
/* a copy of this software and associated documentation files (the */
13+
/* "Software"), to deal in the Software without restriction, including */
14+
/* without limitation the rights to use, copy, modify, merge, publish, */
15+
/* distribute, sublicense, and/or sell copies of the Software, and to */
16+
/* permit persons to whom the Software is furnished to do so, subject to */
17+
/* the following conditions: */
18+
/* */
19+
/* The above copyright notice and this permission notice shall be */
20+
/* included in all copies or substantial portions of the Software. */
21+
/* */
22+
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23+
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24+
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25+
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26+
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27+
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28+
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29+
/**************************************************************************/
30+
31+
#pragma once
32+
33+
#include "core/io/image.h"
34+
35+
#define PF_FOURCC(m_s) ((uint32_t)(((m_s)[3] << 24U) | ((m_s)[2] << 16U) | ((m_s)[1] << 8U) | ((m_s)[0])))
36+
37+
// Reference: https://docs.microsoft.com/en-us/windows/win32/direct3ddds/dds-header
38+
39+
enum {
40+
DDS_MAGIC = 0x20534444,
41+
DDS_HEADER_SIZE = 124,
42+
DDS_PIXELFORMAT_SIZE = 32,
43+
44+
DDSD_PITCH = 0x00000008,
45+
DDSD_LINEARSIZE = 0x00080000,
46+
DDSD_MIPMAPCOUNT = 0x00020000,
47+
DDSD_CAPS = 0x1,
48+
DDSD_HEIGHT = 0x2,
49+
DDSD_WIDTH = 0x4,
50+
DDSD_PIXELFORMAT = 0x1000,
51+
DDPF_ALPHAPIXELS = 0x00000001,
52+
DDPF_ALPHAONLY = 0x00000002,
53+
DDPF_FOURCC = 0x00000004,
54+
DDPF_RGB = 0x00000040,
55+
DDPF_RG_SNORM = 0x00080000,
56+
57+
DDSC2_CUBEMAP = 0x200,
58+
DDSC2_VOLUME = 0x200000,
59+
60+
DX10D_1D = 2,
61+
DX10D_2D = 3,
62+
DX10D_3D = 4,
63+
};
64+
65+
enum DDSFourCC {
66+
DDFCC_DXT1 = PF_FOURCC("DXT1"),
67+
DDFCC_DXT2 = PF_FOURCC("DXT2"),
68+
DDFCC_DXT3 = PF_FOURCC("DXT3"),
69+
DDFCC_DXT4 = PF_FOURCC("DXT4"),
70+
DDFCC_DXT5 = PF_FOURCC("DXT5"),
71+
DDFCC_ATI1 = PF_FOURCC("ATI1"),
72+
DDFCC_BC4U = PF_FOURCC("BC4U"),
73+
DDFCC_ATI2 = PF_FOURCC("ATI2"),
74+
DDFCC_BC5U = PF_FOURCC("BC5U"),
75+
DDFCC_A2XY = PF_FOURCC("A2XY"),
76+
DDFCC_DX10 = PF_FOURCC("DX10"),
77+
DDFCC_R16F = 111,
78+
DDFCC_RG16F = 112,
79+
DDFCC_RGBA16F = 113,
80+
DDFCC_R32F = 114,
81+
DDFCC_RG32F = 115,
82+
DDFCC_RGBA32F = 116,
83+
};
84+
85+
// Reference: https://learn.microsoft.com/en-us/windows/win32/api/dxgiformat/ne-dxgiformat-dxgi_format
86+
enum DXGIFormat {
87+
DXGI_R32G32B32A32_FLOAT = 2,
88+
DXGI_R32G32B32_FLOAT = 6,
89+
DXGI_R16G16B16A16_FLOAT = 10,
90+
DXGI_R32G32_FLOAT = 16,
91+
DXGI_R10G10B10A2_UNORM = 24,
92+
DXGI_R8G8B8A8_UNORM = 28,
93+
DXGI_R8G8B8A8_UNORM_SRGB = 29,
94+
DXGI_R16G16_FLOAT = 34,
95+
DXGI_R32_FLOAT = 41,
96+
DXGI_R8G8_UNORM = 49,
97+
DXGI_R16_FLOAT = 54,
98+
DXGI_R8_UNORM = 61,
99+
DXGI_A8_UNORM = 65,
100+
DXGI_R9G9B9E5 = 67,
101+
DXGI_BC1_UNORM = 71,
102+
DXGI_BC1_UNORM_SRGB = 72,
103+
DXGI_BC2_UNORM = 74,
104+
DXGI_BC2_UNORM_SRGB = 75,
105+
DXGI_BC3_UNORM = 77,
106+
DXGI_BC3_UNORM_SRGB = 78,
107+
DXGI_BC4_UNORM = 80,
108+
DXGI_BC5_UNORM = 83,
109+
DXGI_B5G6R5_UNORM = 85,
110+
DXGI_B5G5R5A1_UNORM = 86,
111+
DXGI_B8G8R8A8_UNORM = 87,
112+
DXGI_BC6H_UF16 = 95,
113+
DXGI_BC6H_SF16 = 96,
114+
DXGI_BC7_UNORM = 98,
115+
DXGI_BC7_UNORM_SRGB = 99,
116+
DXGI_B4G4R4A4_UNORM = 115,
117+
};
118+
119+
// The legacy bitmasked format names here represent the actual data layout in the files,
120+
// while their official names are flipped (e.g. RGBA8 layout is officially called ABGR8).
121+
enum DDSFormat {
122+
DDS_DXT1,
123+
DDS_DXT3,
124+
DDS_DXT5,
125+
DDS_ATI1,
126+
DDS_ATI2,
127+
DDS_BC6U,
128+
DDS_BC6S,
129+
DDS_BC7,
130+
DDS_R16F,
131+
DDS_RG16F,
132+
DDS_RGBA16F,
133+
DDS_R32F,
134+
DDS_RG32F,
135+
DDS_RGB32F,
136+
DDS_RGBA32F,
137+
DDS_RGB9E5,
138+
DDS_RGB8,
139+
DDS_RGBA8,
140+
DDS_BGR8,
141+
DDS_BGRA8,
142+
DDS_BGR5A1,
143+
DDS_BGR565,
144+
DDS_B2GR3,
145+
DDS_B2GR3A8,
146+
DDS_BGR10A2,
147+
DDS_RGB10A2,
148+
DDS_BGRA4,
149+
DDS_LUMINANCE,
150+
DDS_LUMINANCE_ALPHA,
151+
DDS_LUMINANCE_ALPHA_4,
152+
DDS_MAX
153+
};
154+
155+
enum DDSType {
156+
DDST_2D = 1,
157+
DDST_CUBEMAP,
158+
DDST_3D,
159+
160+
DDST_TYPE_MASK = 0x7F,
161+
DDST_ARRAY = 0x80,
162+
};
163+
164+
struct DDSFormatInfo {
165+
const char *name = nullptr;
166+
bool compressed = false;
167+
uint32_t divisor = 0;
168+
uint32_t block_size = 0;
169+
Image::Format format = Image::Format::FORMAT_BPTC_RGBA;
170+
};
171+
172+
static const DDSFormatInfo dds_format_info[DDS_MAX] = {
173+
{ "DXT1/BC1", true, 4, 8, Image::FORMAT_DXT1 },
174+
{ "DXT2/DXT3/BC2", true, 4, 16, Image::FORMAT_DXT3 },
175+
{ "DXT4/DXT5/BC3", true, 4, 16, Image::FORMAT_DXT5 },
176+
{ "ATI1/BC4", true, 4, 8, Image::FORMAT_RGTC_R },
177+
{ "ATI2/A2XY/BC5", true, 4, 16, Image::FORMAT_RGTC_RG },
178+
{ "BC6UF", true, 4, 16, Image::FORMAT_BPTC_RGBFU },
179+
{ "BC6SF", true, 4, 16, Image::FORMAT_BPTC_RGBF },
180+
{ "BC7", true, 4, 16, Image::FORMAT_BPTC_RGBA },
181+
{ "R16F", false, 1, 2, Image::FORMAT_RH },
182+
{ "RG16F", false, 1, 4, Image::FORMAT_RGH },
183+
{ "RGBA16F", false, 1, 8, Image::FORMAT_RGBAH },
184+
{ "R32F", false, 1, 4, Image::FORMAT_RF },
185+
{ "RG32F", false, 1, 8, Image::FORMAT_RGF },
186+
{ "RGB32F", false, 1, 12, Image::FORMAT_RGBF },
187+
{ "RGBA32F", false, 1, 16, Image::FORMAT_RGBAF },
188+
{ "RGB9E5", false, 1, 4, Image::FORMAT_RGBE9995 },
189+
{ "RGB8", false, 1, 3, Image::FORMAT_RGB8 },
190+
{ "RGBA8", false, 1, 4, Image::FORMAT_RGBA8 },
191+
{ "BGR8", false, 1, 3, Image::FORMAT_RGB8 },
192+
{ "BGRA8", false, 1, 4, Image::FORMAT_RGBA8 },
193+
{ "BGR5A1", false, 1, 2, Image::FORMAT_RGBA8 },
194+
{ "BGR565", false, 1, 2, Image::FORMAT_RGB8 },
195+
{ "B2GR3", false, 1, 1, Image::FORMAT_RGB8 },
196+
{ "B2GR3A8", false, 1, 2, Image::FORMAT_RGBA8 },
197+
{ "BGR10A2", false, 1, 4, Image::FORMAT_RGBA8 },
198+
{ "RGB10A2", false, 1, 4, Image::FORMAT_RGBA8 },
199+
{ "BGRA4", false, 1, 2, Image::FORMAT_RGBA8 },
200+
{ "GRAYSCALE", false, 1, 1, Image::FORMAT_L8 },
201+
{ "GRAYSCALE_ALPHA", false, 1, 2, Image::FORMAT_LA8 },
202+
{ "GRAYSCALE_ALPHA_4", false, 1, 1, Image::FORMAT_LA8 },
203+
};

0 commit comments

Comments
 (0)