1/**************************************************************************/
2/* texture_loader_dds.cpp */
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#include "texture_loader_dds.h"
32
33#include "core/io/file_access.h"
34#include "scene/resources/image_texture.h"
35
36#define PF_FOURCC(s) ((uint32_t)(((s)[3] << 24U) | ((s)[2] << 16U) | ((s)[1] << 8U) | ((s)[0])))
37
38// Reference: https://docs.microsoft.com/en-us/windows/win32/direct3ddds/dds-header
39
40enum {
41 DDS_MAGIC = 0x20534444,
42 DDSD_PITCH = 0x00000008,
43 DDSD_LINEARSIZE = 0x00080000,
44 DDSD_MIPMAPCOUNT = 0x00020000,
45 DDPF_FOURCC = 0x00000004,
46 DDPF_ALPHAPIXELS = 0x00000001,
47 DDPF_INDEXED = 0x00000020,
48 DDPF_RGB = 0x00000040,
49};
50
51enum DDSFormat {
52 DDS_DXT1,
53 DDS_DXT3,
54 DDS_DXT5,
55 DDS_ATI1,
56 DDS_ATI2,
57 DDS_A2XY,
58 DDS_BGRA8,
59 DDS_BGR8,
60 DDS_RGBA8, //flipped in dds
61 DDS_RGB8, //flipped in dds
62 DDS_BGR5A1,
63 DDS_BGR565,
64 DDS_BGR10A2,
65 DDS_LUMINANCE,
66 DDS_LUMINANCE_ALPHA,
67 DDS_MAX
68};
69
70struct DDSFormatInfo {
71 const char *name = nullptr;
72 bool compressed = false;
73 bool palette = false;
74 uint32_t divisor = 0;
75 uint32_t block_size = 0;
76 Image::Format format = Image::Format::FORMAT_BPTC_RGBA;
77};
78
79static const DDSFormatInfo dds_format_info[DDS_MAX] = {
80 { "DXT1/BC1", true, false, 4, 8, Image::FORMAT_DXT1 },
81 { "DXT3/BC2", true, false, 4, 16, Image::FORMAT_DXT3 },
82 { "DXT5/BC3", true, false, 4, 16, Image::FORMAT_DXT5 },
83 { "ATI1/BC4", true, false, 4, 8, Image::FORMAT_RGTC_R },
84 { "ATI2/3DC/BC5", true, false, 4, 16, Image::FORMAT_RGTC_RG },
85 { "A2XY/DXN/BC5", true, false, 4, 16, Image::FORMAT_RGTC_RG },
86 { "BGRA8", false, false, 1, 4, Image::FORMAT_RGBA8 },
87 { "BGR8", false, false, 1, 3, Image::FORMAT_RGB8 },
88 { "RGBA8", false, false, 1, 4, Image::FORMAT_RGBA8 },
89 { "RGB8", false, false, 1, 3, Image::FORMAT_RGB8 },
90 { "BGR5A1", false, false, 1, 2, Image::FORMAT_RGBA8 },
91 { "BGR565", false, false, 1, 2, Image::FORMAT_RGB8 },
92 { "BGR10A2", false, false, 1, 4, Image::FORMAT_RGBA8 },
93 { "GRAYSCALE", false, false, 1, 1, Image::FORMAT_L8 },
94 { "GRAYSCALE_ALPHA", false, false, 1, 2, Image::FORMAT_LA8 }
95};
96
97Ref<Resource> ResourceFormatDDS::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode) {
98 if (r_error) {
99 *r_error = ERR_CANT_OPEN;
100 }
101
102 Error err;
103 Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ, &err);
104 if (f.is_null()) {
105 return Ref<Resource>();
106 }
107
108 Ref<FileAccess> fref(f);
109 if (r_error) {
110 *r_error = ERR_FILE_CORRUPT;
111 }
112
113 ERR_FAIL_COND_V_MSG(err != OK, Ref<Resource>(), "Unable to open DDS texture file '" + p_path + "'.");
114
115 uint32_t magic = f->get_32();
116 uint32_t hsize = f->get_32();
117 uint32_t flags = f->get_32();
118 uint32_t height = f->get_32();
119 uint32_t width = f->get_32();
120 uint32_t pitch = f->get_32();
121 /* uint32_t depth = */ f->get_32();
122 uint32_t mipmaps = f->get_32();
123
124 //skip 11
125 for (int i = 0; i < 11; i++) {
126 f->get_32();
127 }
128
129 //validate
130
131 // We don't check DDSD_CAPS or DDSD_PIXELFORMAT, as they're mandatory when writing,
132 // but non-mandatory when reading (as some writers don't set them)...
133 if (magic != DDS_MAGIC || hsize != 124) {
134 ERR_FAIL_V_MSG(Ref<Resource>(), "Invalid or unsupported DDS texture file '" + p_path + "'.");
135 }
136
137 /* uint32_t format_size = */ f->get_32();
138 uint32_t format_flags = f->get_32();
139 uint32_t format_fourcc = f->get_32();
140 uint32_t format_rgb_bits = f->get_32();
141 uint32_t format_red_mask = f->get_32();
142 uint32_t format_green_mask = f->get_32();
143 uint32_t format_blue_mask = f->get_32();
144 uint32_t format_alpha_mask = f->get_32();
145
146 /* uint32_t caps_1 = */ f->get_32();
147 /* uint32_t caps_2 = */ f->get_32();
148 /* uint32_t caps_ddsx = */ f->get_32();
149
150 //reserved skip
151 f->get_32();
152 f->get_32();
153
154 /*
155 print_line("DDS width: "+itos(width));
156 print_line("DDS height: "+itos(height));
157 print_line("DDS mipmaps: "+itos(mipmaps));
158
159 printf("fourcc: %x fflags: %x, rgbbits: %x, fsize: %x\n",format_fourcc,format_flags,format_rgb_bits,format_size);
160 printf("rmask: %x gmask: %x, bmask: %x, amask: %x\n",format_red_mask,format_green_mask,format_blue_mask,format_alpha_mask);
161 */
162
163 //must avoid this later
164 while (f->get_position() < 128) {
165 f->get_8();
166 }
167
168 DDSFormat dds_format;
169
170 if (format_flags & DDPF_FOURCC && format_fourcc == PF_FOURCC("DXT1")) {
171 dds_format = DDS_DXT1;
172 } else if (format_flags & DDPF_FOURCC && format_fourcc == PF_FOURCC("DXT3")) {
173 dds_format = DDS_DXT3;
174
175 } else if (format_flags & DDPF_FOURCC && format_fourcc == PF_FOURCC("DXT5")) {
176 dds_format = DDS_DXT5;
177 } else if (format_flags & DDPF_FOURCC && format_fourcc == PF_FOURCC("ATI1")) {
178 dds_format = DDS_ATI1;
179 } else if (format_flags & DDPF_FOURCC && format_fourcc == PF_FOURCC("ATI2")) {
180 dds_format = DDS_ATI2;
181 } else if (format_flags & DDPF_FOURCC && format_fourcc == PF_FOURCC("A2XY")) {
182 dds_format = DDS_A2XY;
183
184 } else if (format_flags & DDPF_RGB && format_flags & DDPF_ALPHAPIXELS && format_rgb_bits == 32 && format_red_mask == 0xff0000 && format_green_mask == 0xff00 && format_blue_mask == 0xff && format_alpha_mask == 0xff000000) {
185 dds_format = DDS_BGRA8;
186 } else if (format_flags & DDPF_RGB && !(format_flags & DDPF_ALPHAPIXELS) && format_rgb_bits == 24 && format_red_mask == 0xff0000 && format_green_mask == 0xff00 && format_blue_mask == 0xff) {
187 dds_format = DDS_BGR8;
188 } else if (format_flags & DDPF_RGB && format_flags & DDPF_ALPHAPIXELS && format_rgb_bits == 32 && format_red_mask == 0xff && format_green_mask == 0xff00 && format_blue_mask == 0xff0000 && format_alpha_mask == 0xff000000) {
189 dds_format = DDS_RGBA8;
190 } else if (format_flags & DDPF_RGB && !(format_flags & DDPF_ALPHAPIXELS) && format_rgb_bits == 24 && format_red_mask == 0xff && format_green_mask == 0xff00 && format_blue_mask == 0xff0000) {
191 dds_format = DDS_RGB8;
192
193 } else if (format_flags & DDPF_RGB && format_flags & DDPF_ALPHAPIXELS && format_rgb_bits == 16 && format_red_mask == 0x00007c00 && format_green_mask == 0x000003e0 && format_blue_mask == 0x0000001f && format_alpha_mask == 0x00008000) {
194 dds_format = DDS_BGR5A1;
195 } else if (format_flags & DDPF_RGB && format_flags & DDPF_ALPHAPIXELS && format_rgb_bits == 32 && format_red_mask == 0x3ff00000 && format_green_mask == 0xffc00 && format_blue_mask == 0x3ff && format_alpha_mask == 0xc0000000) {
196 dds_format = DDS_BGR10A2;
197 } else if (format_flags & DDPF_RGB && !(format_flags & DDPF_ALPHAPIXELS) && format_rgb_bits == 16 && format_red_mask == 0x0000f800 && format_green_mask == 0x000007e0 && format_blue_mask == 0x0000001f) {
198 dds_format = DDS_BGR565;
199 } else if (!(format_flags & DDPF_ALPHAPIXELS) && format_rgb_bits == 8 && format_red_mask == 0xff) {
200 dds_format = DDS_LUMINANCE;
201 } else if ((format_flags & DDPF_ALPHAPIXELS) && format_rgb_bits == 16 && format_red_mask == 0xff && format_alpha_mask == 0xff00) {
202 dds_format = DDS_LUMINANCE_ALPHA;
203 } else if (format_flags & DDPF_INDEXED && format_rgb_bits == 8) {
204 dds_format = DDS_BGR565;
205 } else {
206 //printf("unrecognized fourcc %x format_flags: %x - rgbbits %i - red_mask %x green mask %x blue mask %x alpha mask %x\n", format_fourcc, format_flags, format_rgb_bits, format_red_mask, format_green_mask, format_blue_mask, format_alpha_mask);
207 ERR_FAIL_V_MSG(Ref<Resource>(), "Unrecognized or unsupported color layout in DDS '" + p_path + "'.");
208 }
209
210 if (!(flags & DDSD_MIPMAPCOUNT)) {
211 mipmaps = 1;
212 }
213
214 Vector<uint8_t> src_data;
215
216 const DDSFormatInfo &info = dds_format_info[dds_format];
217 uint32_t w = width;
218 uint32_t h = height;
219
220 if (info.compressed) {
221 //compressed bc
222
223 uint32_t size = MAX(info.divisor, w) / info.divisor * MAX(info.divisor, h) / info.divisor * info.block_size;
224 ERR_FAIL_COND_V(size != pitch, Ref<Resource>());
225 ERR_FAIL_COND_V(!(flags & DDSD_LINEARSIZE), Ref<Resource>());
226
227 for (uint32_t i = 1; i < mipmaps; i++) {
228 w = MAX(1u, w >> 1);
229 h = MAX(1u, h >> 1);
230 uint32_t bsize = MAX(info.divisor, w) / info.divisor * MAX(info.divisor, h) / info.divisor * info.block_size;
231 //printf("%i x %i - block: %i\n",w,h,bsize);
232 size += bsize;
233 }
234
235 src_data.resize(size);
236 uint8_t *wb = src_data.ptrw();
237 f->get_buffer(wb, size);
238
239 } else if (info.palette) {
240 //indexed
241 ERR_FAIL_COND_V(!(flags & DDSD_PITCH), Ref<Resource>());
242 ERR_FAIL_COND_V(format_rgb_bits != 8, Ref<Resource>());
243
244 uint32_t size = pitch * height;
245 ERR_FAIL_COND_V(size != width * height * info.block_size, Ref<Resource>());
246
247 uint8_t palette[256 * 4];
248 f->get_buffer(palette, 256 * 4);
249
250 int colsize = 3;
251 for (int i = 0; i < 256; i++) {
252 if (palette[i * 4 + 3] < 255) {
253 colsize = 4;
254 }
255 }
256
257 int w2 = width;
258 int h2 = height;
259
260 for (uint32_t i = 1; i < mipmaps; i++) {
261 w2 = (w2 + 1) >> 1;
262 h2 = (h2 + 1) >> 1;
263 size += w2 * h2 * info.block_size;
264 }
265
266 src_data.resize(size + 256 * colsize);
267 uint8_t *wb = src_data.ptrw();
268 f->get_buffer(wb, size);
269
270 for (int i = 0; i < 256; i++) {
271 int dst_ofs = size + i * colsize;
272 int src_ofs = i * 4;
273 wb[dst_ofs + 0] = palette[src_ofs + 2];
274 wb[dst_ofs + 1] = palette[src_ofs + 1];
275 wb[dst_ofs + 2] = palette[src_ofs + 0];
276 if (colsize == 4) {
277 wb[dst_ofs + 3] = palette[src_ofs + 3];
278 }
279 }
280 } else {
281 //uncompressed generic...
282
283 uint32_t size = width * height * info.block_size;
284
285 for (uint32_t i = 1; i < mipmaps; i++) {
286 w = (w + 1) >> 1;
287 h = (h + 1) >> 1;
288 size += w * h * info.block_size;
289 }
290
291 if (dds_format == DDS_BGR565) {
292 size = size * 3 / 2;
293 } else if (dds_format == DDS_BGR5A1) {
294 size = size * 2;
295 }
296
297 src_data.resize(size);
298 uint8_t *wb = src_data.ptrw();
299 f->get_buffer(wb, size);
300
301 switch (dds_format) {
302 case DDS_BGR5A1: {
303 // TO RGBA
304 int colcount = size / 4;
305
306 for (int i = colcount - 1; i >= 0; i--) {
307 int src_ofs = i * 2;
308 int dst_ofs = i * 4;
309
310 uint8_t a = wb[src_ofs + 1] & 0x80;
311 uint8_t b = wb[src_ofs] & 0x1F;
312 uint8_t g = (wb[src_ofs] >> 5) | ((wb[src_ofs + 1] & 0x3) << 3);
313 uint8_t r = (wb[src_ofs + 1] >> 2) & 0x1F;
314 wb[dst_ofs + 0] = r << 3;
315 wb[dst_ofs + 1] = g << 3;
316 wb[dst_ofs + 2] = b << 3;
317 wb[dst_ofs + 3] = a ? 255 : 0;
318 }
319 } break;
320 case DDS_BGR565: {
321 int colcount = size / 3;
322
323 for (int i = colcount - 1; i >= 0; i--) {
324 int src_ofs = i * 2;
325 int dst_ofs = i * 3;
326
327 uint8_t b = wb[src_ofs] & 0x1F;
328 uint8_t g = (wb[src_ofs] >> 5) | ((wb[src_ofs + 1] & 0x7) << 3);
329 uint8_t r = wb[src_ofs + 1] >> 3;
330 wb[dst_ofs + 0] = r << 3;
331 wb[dst_ofs + 1] = g << 2;
332 wb[dst_ofs + 2] = b << 3; //b<<3;
333 }
334
335 } break;
336 case DDS_BGR10A2: {
337 // TO RGBA
338 int colcount = size / 4;
339
340 for (int i = colcount - 1; i >= 0; i--) {
341 int ofs = i * 4;
342
343 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);
344
345 uint8_t a = (w32 & 0xc0000000) >> 24;
346 uint8_t r = (w32 & 0x3ff00000) >> 22;
347 uint8_t g = (w32 & 0xffc00) >> 12;
348 uint8_t b = (w32 & 0x3ff) >> 2;
349
350 wb[ofs + 0] = r;
351 wb[ofs + 1] = g;
352 wb[ofs + 2] = b;
353 wb[ofs + 3] = a == 0xc0 ? 255 : a; //0xc0 should be opaque
354 }
355 } break;
356 case DDS_BGRA8: {
357 int colcount = size / 4;
358
359 for (int i = 0; i < colcount; i++) {
360 SWAP(wb[i * 4 + 0], wb[i * 4 + 2]);
361 }
362
363 } break;
364 case DDS_BGR8: {
365 int colcount = size / 3;
366
367 for (int i = 0; i < colcount; i++) {
368 SWAP(wb[i * 3 + 0], wb[i * 3 + 2]);
369 }
370 } break;
371 case DDS_RGBA8: {
372 /* do nothing either
373 int colcount = size/4;
374
375 for(int i=0;i<colcount;i++) {
376 uint8_t r = wb[i*4+1];
377 uint8_t g = wb[i*4+2];
378 uint8_t b = wb[i*4+3];
379 uint8_t a = wb[i*4+0];
380
381 wb[i*4+0]=r;
382 wb[i*4+1]=g;
383 wb[i*4+2]=b;
384 wb[i*4+3]=a;
385 }
386 */
387 } break;
388 case DDS_RGB8: {
389 // do nothing
390 /*
391 int colcount = size/3;
392
393 for(int i=0;i<colcount;i++) {
394 SWAP( wb[i*3+0],wb[i*3+2] );
395 }*/
396 } break;
397 case DDS_LUMINANCE: {
398 // do nothing i guess?
399
400 } break;
401 case DDS_LUMINANCE_ALPHA: {
402 // do nothing i guess?
403
404 } break;
405
406 default: {
407 }
408 }
409 }
410
411 Ref<Image> img = memnew(Image(width, height, mipmaps - 1, info.format, src_data));
412 Ref<ImageTexture> texture = ImageTexture::create_from_image(img);
413
414 if (r_error) {
415 *r_error = OK;
416 }
417
418 return texture;
419}
420
421void ResourceFormatDDS::get_recognized_extensions(List<String> *p_extensions) const {
422 p_extensions->push_back("dds");
423}
424
425bool ResourceFormatDDS::handles_type(const String &p_type) const {
426 return ClassDB::is_parent_class(p_type, "Texture2D");
427}
428
429String ResourceFormatDDS::get_resource_type(const String &p_path) const {
430 if (p_path.get_extension().to_lower() == "dds") {
431 return "ImageTexture";
432 }
433 return "";
434}
435