Debian Codegraph @

modules/dds/texture_loader_dds.cpp

raw symbols SCIP
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 "dds_enums.h"
34
35 #include "core/io/file_access.h"
36 #include "core/io/file_access_memory.h"
37 #include "scene/resources/image_texture.h"
38
39 DDSFormat _dxgi_to_dds_format(uint32_t p_dxgi_format) {
40 switch (p_dxgi_format) {
41 case DXGI_R32G32B32A32_FLOAT: {
42 return DDS_RGBA32F;
43 }
44 case DXGI_R32G32B32_FLOAT: {
45 return DDS_RGB32F;
46 }
47 case DXGI_R16G16B16A16_FLOAT: {
48 return DDS_RGBA16F;
49 }
50 case DXGI_R16G16B16A16_UNORM: {
51 return DDS_RGBA16;
52 }
53 case DXGI_R16G16B16A16_UINT: {
54 return DDS_RGBA16I;
55 }
56 case DXGI_R32G32_FLOAT: {
57 return DDS_RG32F;
58 }
59 case DXGI_R10G10B10A2_UNORM: {
60 return DDS_RGB10A2;
61 }
62 case DXGI_R8G8B8A8_UNORM:
63 case DXGI_R8G8B8A8_UNORM_SRGB: {
64 return DDS_RGBA8;
65 }
66 case DXGI_R16G16_FLOAT: {
67 return DDS_RG16F;
68 }
69 case DXGI_R16G16_UNORM: {
70 return DDS_RG16;
71 }
72 case DXGI_R16G16_UINT: {
73 return DDS_RG16I;
74 }
75 case DXGI_R32_FLOAT: {
76 return DDS_R32F;
77 }
78 case DXGI_R8_UNORM:
79 case DXGI_A8_UNORM: {
80 return DDS_LUMINANCE;
81 }
82 case DXGI_R16_FLOAT: {
83 return DDS_R16F;
84 }
85 case DXGI_R16_UNORM: {
86 return DDS_R16;
87 }
88 case DXGI_R16_UINT: {
89 return DDS_R16I;
90 }
91 case DXGI_R8G8_UNORM: {
92 return DDS_LUMINANCE_ALPHA;
93 }
94 case DXGI_R9G9B9E5: {
95 return DDS_RGB9E5;
96 }
97 case DXGI_BC1_UNORM:
98 case DXGI_BC1_UNORM_SRGB: {
99 return DDS_DXT1;
100 }
101 case DXGI_BC2_UNORM:
102 case DXGI_BC2_UNORM_SRGB: {
103 return DDS_DXT3;
104 }
105 case DXGI_BC3_UNORM:
106 case DXGI_BC3_UNORM_SRGB: {
107 return DDS_DXT5;
108 }
109 case DXGI_BC4_UNORM: {
110 return DDS_ATI1;
111 }
112 case DXGI_BC5_UNORM: {
113 return DDS_ATI2;
114 }
115 case DXGI_B5G6R5_UNORM: {
116 return DDS_BGR565;
117 }
118 case DXGI_B5G5R5A1_UNORM: {
119 return DDS_BGR5A1;
120 }
121 case DXGI_B8G8R8A8_UNORM: {
122 return DDS_BGRA8;
123 }
124 case DXGI_BC6H_UF16: {
125 return DDS_BC6U;
126 }
127 case DXGI_BC6H_SF16: {
128 return DDS_BC6S;
129 }
130 case DXGI_BC7_UNORM:
131 case DXGI_BC7_UNORM_SRGB: {
132 return DDS_BC7;
133 }
134 case DXGI_B4G4R4A4_UNORM: {
135 return DDS_BGRA4;
136 }
137
138 default: {
139 return DDS_MAX;
140 }
141 }
142 }
143
144 static Ref<Image> _dds_load_layer(Ref<FileAccess> p_file, DDSFormat p_dds_format, uint32_t p_width, uint32_t p_height, uint32_t p_mipmaps, uint32_t p_pitch, uint32_t p_flags, Vector<uint8_t> &r_src_data) {
145 const DDSFormatInfo &info = dds_format_info[p_dds_format];
146
147 uint32_t w = p_width;
148 uint32_t h = p_height;
149
150 if (info.compressed) {
151 // BC compressed.
152 w += w % info.divisor;
153 h += h % info.divisor;
154 if (w != p_width) {
155 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));
156 }
157 if (h != p_height) {
158 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));
159 }
160
161 uint32_t size = MAX(1u, (w + 3) / 4) * MAX(1u, (h + 3) / 4) * info.block_size;
162
163 if (p_flags & DDSD_LINEARSIZE) {
164 ERR_FAIL_COND_V_MSG(size != p_pitch, Ref<Resource>(), "DDS header flags specify that a linear size of the top-level image is present, but the specified size does not match the expected value.");
165 } else {
166 ERR_FAIL_COND_V_MSG(p_pitch != 0, Ref<Resource>(), "DDS header flags specify that no linear size will given for the top-level image, but a non-zero linear size value is present in the header.");
167 }
168
169 for (uint32_t i = 1; i < p_mipmaps; i++) {
170 w = MAX(1u, w >> 1);
171 h = MAX(1u, h >> 1);
172
173 uint32_t bsize = MAX(1u, (w + 3) / 4) * MAX(1u, (h + 3) / 4) * info.block_size;
174 size += bsize;
175 }
176
177 r_src_data.resize(size);
178 uint8_t *wb = r_src_data.ptrw();
179 p_file->get_buffer(wb, size);
180
181 } else {
182 // Generic uncompressed.
183 uint32_t size = p_width * p_height * info.block_size;
184
185 for (uint32_t i = 1; i < p_mipmaps; i++) {
186 w = MAX(1u, w >> 1);
187 h = MAX(1u, h >> 1);
188 size += w * h * info.block_size;
189 }
190
191 // Calculate the space these formats will take up after decoding.
192 switch (p_dds_format) {
193 case DDS_BGR5A1:
194 case DDS_B2GR3A8:
195 case DDS_LUMINANCE_ALPHA_4:
196 size = size * 2;
197 break;
198
199 case DDS_B2GR3:
200 size = size * 3;
201 break;
202
203 default:
204 break;
205 }
206
207 r_src_data.resize(size);
208 uint8_t *wb = r_src_data.ptrw();
209 p_file->get_buffer(wb, size);
210
211 switch (p_dds_format) {
212 case DDS_BGR5A1: {
213 // To RGBA8.
214 int colcount = size / 4;
215
216 for (int i = colcount - 1; i >= 0; i--) {
217 int src_ofs = i * 2;
218 int dst_ofs = i * 4;
219
220 uint8_t a = wb[src_ofs + 1] & 0x80;
221 uint8_t b = wb[src_ofs] & 0x1F;
222 uint8_t g = (wb[src_ofs] >> 5) | ((wb[src_ofs + 1] & 0x3) << 3);
223 uint8_t r = (wb[src_ofs + 1] >> 2) & 0x1F;
224
225 wb[dst_ofs + 0] = r << 3;
226 wb[dst_ofs + 1] = g << 3;
227 wb[dst_ofs + 2] = b << 3;
228 wb[dst_ofs + 3] = a ? 255 : 0;
229 }
230
231 } break;
232 case DDS_BGRA4: {
233 // To RGBA4.
234 for (uint32_t i = 0; i < size; i += 2) {
235 uint8_t ar = wb[i + 0];
236 uint8_t gb = wb[i + 1];
237
238 wb[i + 0] = ((ar & 0x0F) << 4) | ((gb & 0xF0) >> 4);
239 wb[i + 1] = ((ar & 0xF0) >> 4) | ((gb & 0x0F) << 4);
240 }
241
242 } break;
243 case DDS_B2GR3: {
244 // To RGB8.
245 int colcount = size / 3;
246
247 for (int i = colcount - 1; i >= 0; i--) {
248 int src_ofs = i;
249 int dst_ofs = i * 3;
250
251 uint8_t b = (wb[src_ofs] & 0x3) << 6;
252 uint8_t g = (wb[src_ofs] & 0x1C) << 3;
253 uint8_t r = (wb[src_ofs] & 0xE0);
254
255 wb[dst_ofs] = r;
256 wb[dst_ofs + 1] = g;
257 wb[dst_ofs + 2] = b;
258 }
259
260 } break;
261 case DDS_B2GR3A8: {
262 // To RGBA8.
263 int colcount = size / 4;
264
265 for (int i = colcount - 1; i >= 0; i--) {
266 int src_ofs = i * 2;
267 int dst_ofs = i * 4;
268
269 uint8_t b = (wb[src_ofs] & 0x3) << 6;
270 uint8_t g = (wb[src_ofs] & 0x1C) << 3;
271 uint8_t r = (wb[src_ofs] & 0xE0);
272 uint8_t a = wb[src_ofs + 1];
273
274 wb[dst_ofs] = r;
275 wb[dst_ofs + 1] = g;
276 wb[dst_ofs + 2] = b;
277 wb[dst_ofs + 3] = a;
278 }
279
280 } break;
281 case DDS_RGB10A2: {
282 // To RGBA8.
283 int colcount = size / 4;
284
285 for (int i = 0; i < colcount; i++) {
286 int ofs = i * 4;
287
288 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);
289
290 // This method follows the 'standard' way of decoding 10-bit dds files,
291 // which means the ones created with DirectXTex will be loaded incorrectly.
292 uint8_t a = (w32 & 0xc0000000) >> 24;
293 uint8_t r = (w32 & 0x3ff) >> 2;
294 uint8_t g = (w32 & 0xffc00) >> 12;
295 uint8_t b = (w32 & 0x3ff00000) >> 22;
296
297 wb[ofs + 0] = r;
298 wb[ofs + 1] = g;
299 wb[ofs + 2] = b;
300 wb[ofs + 3] = a == 0xc0 ? 255 : a; // 0xc0 should be opaque.
301 }
302
303 } break;
304 case DDS_BGR10A2: {
305 // To RGBA8.
306 int colcount = size / 4;
307
308 for (int i = 0; i < colcount; i++) {
309 int ofs = i * 4;
310
311 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);
312
313 // This method follows the 'standard' way of decoding 10-bit dds files,
314 // which means the ones created with DirectXTex will be loaded incorrectly.
315 uint8_t a = (w32 & 0xc0000000) >> 24;
316 uint8_t r = (w32 & 0x3ff00000) >> 22;
317 uint8_t g = (w32 & 0xffc00) >> 12;
318 uint8_t b = (w32 & 0x3ff) >> 2;
319
320 wb[ofs + 0] = r;
321 wb[ofs + 1] = g;
322 wb[ofs + 2] = b;
323 wb[ofs + 3] = a == 0xc0 ? 255 : a; // 0xc0 should be opaque.
324 }
325
326 } break;
327
328 // Channel-swapped.
329 case DDS_BGRA8: {
330 // To RGBA8.
331 int colcount = size / 4;
332
333 for (int i = 0; i < colcount; i++) {
334 SWAP(wb[i * 4 + 0], wb[i * 4 + 2]);
335 }
336
337 } break;
338 case DDS_BGR8: {
339 // To RGB8.
340 int colcount = size / 3;
341
342 for (int i = 0; i < colcount; i++) {
343 SWAP(wb[i * 3 + 0], wb[i * 3 + 2]);
344 }
345
346 } break;
347
348 case DDS_RGBX8: {
349 // To RGB8.
350 int colcount = size / 4;
351
352 for (int i = 0; i < colcount; i++) {
353 int src_ofs = i * 4;
354 int dst_ofs = i * 3;
355
356 wb[dst_ofs + 0] = wb[src_ofs + 0];
357 wb[dst_ofs + 1] = wb[src_ofs + 1];
358 wb[dst_ofs + 2] = wb[src_ofs + 2];
359 }
360
361 r_src_data.resize(size * 3 / 4);
362
363 } break;
364 case DDS_BGRX8: {
365 // To RGB8.
366 int colcount = size / 4;
367
368 for (int i = 0; i < colcount; i++) {
369 int src_ofs = i * 4;
370 int dst_ofs = i * 3;
371
372 wb[dst_ofs + 0] = wb[src_ofs + 2];
373 wb[dst_ofs + 1] = wb[src_ofs + 1];
374 wb[dst_ofs + 2] = wb[src_ofs + 0];
375 }
376
377 r_src_data.resize(size * 3 / 4);
378
379 } break;
380
381 // Grayscale.
382 case DDS_LUMINANCE_ALPHA_4: {
383 // To LA8.
384 int colcount = size / 2;
385
386 for (int i = colcount - 1; i >= 0; i--) {
387 int src_ofs = i;
388 int dst_ofs = i * 2;
389
390 uint8_t l = wb[src_ofs] & 0x0F;
391 uint8_t a = wb[src_ofs] & 0xF0;
392
393 wb[dst_ofs] = (l << 4) | l;
394 wb[dst_ofs + 1] = a | (a >> 4);
395 }
396
397 } break;
398
399 default: {
400 }
401 }
402 }
403
404 return memnew(Image(p_width, p_height, p_mipmaps > 1, info.format, r_src_data));
405 }
406
407 static Vector<Ref<Image>> _dds_load_images(Ref<FileAccess> p_f, DDSFormat p_dds_format, uint32_t p_width, uint32_t p_height, uint32_t p_mipmaps, uint32_t p_pitch, uint32_t p_flags, uint32_t p_layer_count) {
408 Vector<uint8_t> src_data;
409 Vector<Ref<Image>> images;
410 images.resize(p_layer_count);
411
412 for (uint32_t i = 0; i < p_layer_count; i++) {
413 images.write[i] = _dds_load_layer(p_f, p_dds_format, p_width, p_height, p_mipmaps, p_pitch, p_flags, src_data);
414 ERR_FAIL_COND_V(images.write[i].is_null(), Vector<Ref<Image>>());
415 }
416
417 return images;
418 }
419
420 static Ref<Resource> _dds_create_texture(const Vector<Ref<Image>> &p_images, uint32_t p_dds_type, uint32_t p_width, uint32_t p_height, uint32_t p_layer_count, uint32_t p_mipmaps, Error *r_error) {
421 ERR_FAIL_COND_V(p_images.is_empty(), Ref<Resource>());
422
423 if ((p_dds_type & DDST_TYPE_MASK) == DDST_2D) {
424 if (p_dds_type & DDST_ARRAY) {
425 Ref<Texture2DArray> texture;
426 texture.instantiate();
427 texture->create_from_images(p_images);
428
429 if (r_error) {
430 *r_error = OK;
431 }
432
433 return texture;
434
435 } else {
436 if (r_error) {
437 *r_error = OK;
438 }
439
440 return ImageTexture::create_from_image(p_images[0]);
441 }
442
443 } else if ((p_dds_type & DDST_TYPE_MASK) == DDST_CUBEMAP) {
444 ERR_FAIL_COND_V(p_layer_count % 6 != 0, Ref<Resource>());
445
446 if (p_dds_type & DDST_ARRAY) {
447 Ref<CubemapArray> texture;
448 texture.instantiate();
449 texture->create_from_images(p_images);
450
451 if (r_error) {
452 *r_error = OK;
453 }
454
455 return texture;
456
457 } else {
458 Ref<Cubemap> texture;
459 texture.instantiate();
460 texture->create_from_images(p_images);
461
462 if (r_error) {
463 *r_error = OK;
464 }
465
466 return texture;
467 }
468
469 } else if ((p_dds_type & DDST_TYPE_MASK) == DDST_3D) {
470 Ref<ImageTexture3D> texture;
471 texture.instantiate();
472 texture->create(p_images[0]->get_format(), p_width, p_height, p_layer_count, p_mipmaps > 1, p_images);
473
474 if (r_error) {
475 *r_error = OK;
476 }
477
478 return texture;
479 }
480
481 return Ref<Resource>();
482 }
483
484 static Ref<Resource> _dds_create_texture_from_images(const Vector<Ref<Image>> &p_images, DDSFormat p_dds_format, uint32_t p_width, uint32_t p_height, uint32_t p_mipmaps, uint32_t p_pitch, uint32_t p_flags, uint32_t p_layer_count, uint32_t p_dds_type, Error *r_error) {
485 return _dds_create_texture(p_images, p_dds_type, p_width, p_height, p_layer_count, p_mipmaps, r_error);
486 }
487
488 static Vector<Ref<Image>> _dds_load_images_from_buffer(Ref<FileAccess> p_f, DDSFormat &r_dds_format, uint32_t &r_width, uint32_t &r_height, uint32_t &r_mipmaps, uint32_t &r_pitch, uint32_t &r_flags, uint32_t &r_layer_count, uint32_t &r_dds_type, const String &p_path = "") {
489 ERR_FAIL_COND_V_MSG(p_f.is_null(), Vector<Ref<Image>>(), vformat("Empty DDS texture file."));
490 ERR_FAIL_COND_V_MSG(!p_f->get_length(), Vector<Ref<Image>>(), vformat("Empty DDS texture file."));
491
492 uint32_t magic = p_f->get_32();
493 uint32_t hsize = p_f->get_32();
494 r_flags = p_f->get_32();
495 r_height = p_f->get_32();
496 r_width = p_f->get_32();
497 r_pitch = p_f->get_32();
498 uint32_t depth = p_f->get_32();
499 r_mipmaps = p_f->get_32();
500
501 // Skip reserved.
502 for (int i = 0; i < 11; i++) {
503 p_f->get_32();
504 }
505
506 // Validate.
507 // We don't check DDSD_CAPS or DDSD_PIXELFORMAT, as they're mandatory when writing,
508 // but non-mandatory when reading (as some writers don't set them).
509 if (magic != DDS_MAGIC || hsize != 124) {
510 ERR_FAIL_V_MSG(Vector<Ref<Image>>(), vformat("Invalid or unsupported DDS texture file '%s'.", p_path));
511 }
512
513 /* uint32_t format_size = */ p_f->get_32();
514 uint32_t format_flags = p_f->get_32();
515 uint32_t format_fourcc = p_f->get_32();
516 uint32_t format_rgb_bits = p_f->get_32();
517 uint32_t format_red_mask = p_f->get_32();
518 uint32_t format_green_mask = p_f->get_32();
519 uint32_t format_blue_mask = p_f->get_32();
520 uint32_t format_alpha_mask = p_f->get_32();
521
522 /* uint32_t caps_1 = */ p_f->get_32();
523 uint32_t caps_2 = p_f->get_32();
524 /* uint32_t caps_3 = */ p_f->get_32();
525 /* uint32_t caps_4 = */ p_f->get_32();
526
527 // Skip reserved.
528 p_f->get_32();
529
530 if (p_f->get_position() < 128) {
531 p_f->seek(128);
532 }
533
534 r_layer_count = 1;
535 r_dds_type = DDST_2D;
536
537 if (caps_2 & DDSC2_CUBEMAP) {
538 r_dds_type = DDST_CUBEMAP;
539 r_layer_count *= 6;
540
541 } else if (caps_2 & DDSC2_VOLUME) {
542 r_dds_type = DDST_3D;
543 r_layer_count = depth;
544 }
545
546 r_dds_format = DDS_MAX;
547
548 if (format_flags & DDPF_FOURCC) {
549 // FourCC formats.
550 switch (format_fourcc) {
551 case DDFCC_DXT1: {
552 r_dds_format = DDS_DXT1;
553 } break;
554 case DDFCC_DXT2:
555 case DDFCC_DXT3: {
556 r_dds_format = DDS_DXT3;
557 } break;
558 case DDFCC_DXT4:
559 case DDFCC_DXT5: {
560 r_dds_format = DDS_DXT5;
561 } break;
562 case DDFCC_ATI1:
563 case DDFCC_BC4U: {
564 r_dds_format = DDS_ATI1;
565 } break;
566 case DDFCC_ATI2:
567 case DDFCC_BC5U:
568 case DDFCC_A2XY: {
569 r_dds_format = DDS_ATI2;
570 } break;
571 case DDFCC_RGBA16: {
572 r_dds_format = DDS_RGBA16;
573 } break;
574 case DDFCC_R16F: {
575 r_dds_format = DDS_R16F;
576 } break;
577 case DDFCC_RG16F: {
578 r_dds_format = DDS_RG16F;
579 } break;
580 case DDFCC_RGBA16F: {
581 r_dds_format = DDS_RGBA16F;
582 } break;
583 case DDFCC_R32F: {
584 r_dds_format = DDS_R32F;
585 } break;
586 case DDFCC_RG32F: {
587 r_dds_format = DDS_RG32F;
588 } break;
589 case DDFCC_RGBA32F: {
590 r_dds_format = DDS_RGBA32F;
591 } break;
592 case DDFCC_DX10: {
593 uint32_t dxgi_format = p_f->get_32();
594 uint32_t dimension = p_f->get_32();
595 /* uint32_t misc_flags_1 = */ p_f->get_32();
596 uint32_t array_size = p_f->get_32();
597 /* uint32_t misc_flags_2 = */ p_f->get_32();
598
599 if (dimension == DX10D_3D) {
600 r_dds_type = DDST_3D;
601 r_layer_count = depth;
602 }
603
604 if (array_size > 1) {
605 r_layer_count *= array_size;
606 r_dds_type |= DDST_ARRAY;
607 }
608
609 r_dds_format = _dxgi_to_dds_format(dxgi_format);
610 } break;
611
612 default: {
613 ERR_FAIL_V_MSG(Vector<Ref<Image>>(), vformat("Unrecognized or unsupported FourCC in DDS '%s'.", p_path));
614 }
615 }
616
617 } else if (format_flags & DDPF_RGB) {
618 // Channel-bitmasked formats.
619 if (format_flags & DDPF_ALPHAPIXELS) {
620 // With alpha.
621 if (format_rgb_bits == 32 && format_red_mask == 0xff0000 && format_green_mask == 0xff00 && format_blue_mask == 0xff && format_alpha_mask == 0xff000000) {
622 r_dds_format = DDS_BGRA8;
623 } else if (format_rgb_bits == 32 && format_red_mask == 0xff && format_green_mask == 0xff00 && format_blue_mask == 0xff0000 && format_alpha_mask == 0xff000000) {
624 r_dds_format = DDS_RGBA8;
625 } else if (format_rgb_bits == 16 && format_red_mask == 0x00007c00 && format_green_mask == 0x000003e0 && format_blue_mask == 0x0000001f && format_alpha_mask == 0x00008000) {
626 r_dds_format = DDS_BGR5A1;
627 } else if (format_rgb_bits == 32 && format_red_mask == 0x3ff00000 && format_green_mask == 0xffc00 && format_blue_mask == 0x3ff && format_alpha_mask == 0xc0000000) {
628 r_dds_format = DDS_BGR10A2;
629 } else if (format_rgb_bits == 32 && format_red_mask == 0x3ff && format_green_mask == 0xffc00 && format_blue_mask == 0x3ff00000 && format_alpha_mask == 0xc0000000) {
630 r_dds_format = DDS_RGB10A2;
631 } else if (format_rgb_bits == 16 && format_red_mask == 0xf00 && format_green_mask == 0xf0 && format_blue_mask == 0xf && format_alpha_mask == 0xf000) {
632 r_dds_format = DDS_BGRA4;
633 } else if (format_rgb_bits == 16 && format_red_mask == 0xe0 && format_green_mask == 0x1c && format_blue_mask == 0x3 && format_alpha_mask == 0xff00) {
634 r_dds_format = DDS_B2GR3A8;
635 }
636
637 } else {
638 // Without alpha.
639 if (format_rgb_bits == 24 && format_red_mask == 0xff0000 && format_green_mask == 0xff00 && format_blue_mask == 0xff) {
640 r_dds_format = DDS_BGR8;
641 } else if (format_rgb_bits == 24 && format_red_mask == 0xff && format_green_mask == 0xff00 && format_blue_mask == 0xff0000) {
642 r_dds_format = DDS_RGB8;
643 } else if (format_rgb_bits == 16 && format_red_mask == 0x0000f800 && format_green_mask == 0x000007e0 && format_blue_mask == 0x0000001f) {
644 r_dds_format = DDS_BGR565;
645 } else if (format_rgb_bits == 8 && format_red_mask == 0xe0 && format_green_mask == 0x1c && format_blue_mask == 0x3) {
646 r_dds_format = DDS_B2GR3;
647 } else if (format_rgb_bits == 32 && format_red_mask == 0xff0000 && format_green_mask == 0xff00 && format_blue_mask == 0xff) {
648 r_dds_format = DDS_BGRX8;
649 } else if (format_rgb_bits == 32 && format_red_mask == 0xff && format_green_mask == 0xff00 && format_blue_mask == 0xff0000) {
650 r_dds_format = DDS_RGBX8;
651 }
652 }
653
654 if (format_rgb_bits == 32 && format_red_mask == 0xffff && format_green_mask == 0xffff0000) {
655 r_dds_format = DDS_RG16;
656 }
657
658 } else {
659 // Other formats.
660 if (format_flags & DDPF_ALPHAONLY && format_rgb_bits == 8 && format_alpha_mask == 0xff) {
661 // Alpha only.
662 r_dds_format = DDS_LUMINANCE;
663 }
664 }
665
666 // Depending on the writer, luminance formats may or may not have the DDPF_RGB or DDPF_LUMINANCE flags defined,
667 // so we check for these formats after everything else failed.
668 if (r_dds_format == DDS_MAX) {
669 if (format_flags & DDPF_ALPHAPIXELS) {
670 // With alpha.
671 if (format_rgb_bits == 16 && format_red_mask == 0xff && format_alpha_mask == 0xff00) {
672 r_dds_format = DDS_LUMINANCE_ALPHA;
673 } else if (format_rgb_bits == 8 && format_red_mask == 0xf && format_alpha_mask == 0xf0) {
674 r_dds_format = DDS_LUMINANCE_ALPHA_4;
675 }
676
677 } else {
678 // Without alpha.
679 if (format_rgb_bits == 8 && format_red_mask == 0xff) {
680 r_dds_format = DDS_LUMINANCE;
681 } else if (format_rgb_bits == 16 && format_red_mask == 0xffff) {
682 r_dds_format = DDS_R16;
683 }
684 }
685 }
686
687 // No format detected, error.
688 if (r_dds_format == DDS_MAX) {
689 ERR_FAIL_V_MSG(Vector<Ref<Image>>(), vformat("Unrecognized or unsupported color layout in DDS '%s'.", p_path));
690 }
691
692 if (!(r_flags & DDSD_MIPMAPCOUNT)) {
693 r_mipmaps = 1;
694 }
695
696 return _dds_load_images(p_f, r_dds_format, r_width, r_height, r_mipmaps, r_pitch, r_flags, r_layer_count);
697 }
698
699 static Ref<Resource> _dds_load_from_buffer(Ref<FileAccess> p_f, Error *r_error, const String &p_path = "") {
700 if (r_error) {
701 *r_error = ERR_FILE_CORRUPT;
702 }
703
704 DDSFormat dds_format;
705 uint32_t width = 0, height = 0, mipmaps = 0, pitch = 0, flags = 0, layer_count = 0, dds_type = 0;
706
707 Vector<Ref<Image>> images = _dds_load_images_from_buffer(p_f, dds_format, width, height, mipmaps, pitch, flags, layer_count, dds_type, p_path);
708 return _dds_create_texture_from_images(images, dds_format, width, height, mipmaps, pitch, flags, layer_count, dds_type, r_error);
709 }
710
711 static Ref<Resource> _dds_load_from_file(const String &p_path, Error *r_error) {
712 if (r_error) {
713 *r_error = ERR_CANT_OPEN;
714 }
715
716 Error err;
717 Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ, &err);
718 if (f.is_null()) {
719 return Ref<Resource>();
720 }
721
722 return _dds_load_from_buffer(f, r_error, p_path);
723 }
724
725 Ref<Resource> ResourceFormatDDS::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode) {
726 return _dds_load_from_file(p_path, r_error);
727 }
728
729 void ResourceFormatDDS::get_recognized_extensions(List<String> *p_extensions) const {
730 p_extensions->push_back("dds");
731 }
732
733 bool ResourceFormatDDS::handles_type(const String &p_type) const {
734 return ClassDB::is_parent_class(p_type, "Texture");
735 }
736
737 String ResourceFormatDDS::get_resource_type(const String &p_path) const {
738 if (p_path.has_extension("dds")) {
739 return "Texture";
740 }
741 return "";
742 }
743
744 Ref<Image> load_mem_dds(const uint8_t *p_dds, int p_size) {
745 ERR_FAIL_NULL_V(p_dds, Ref<Image>());
746 ERR_FAIL_COND_V(!p_size, Ref<Image>());
747 Ref<FileAccessMemory> memfile;
748 memfile.instantiate();
749 Error open_memfile_error = memfile->open_custom(p_dds, p_size);
750 ERR_FAIL_COND_V_MSG(open_memfile_error, Ref<Image>(), "Could not create memfile for DDS image buffer.");
751
752 DDSFormat dds_format;
753 uint32_t width, height, mipmaps, pitch, flags, layer_count, dds_type;
754
755 Vector<Ref<Image>> images = _dds_load_images_from_buffer(memfile, dds_format, width, height, mipmaps, pitch, flags, layer_count, dds_type);
756 ERR_FAIL_COND_V_MSG(images.is_empty(), Ref<Image>(), "Failed to load DDS image.");
757
758 return images[0];
759 }
760
761 ResourceFormatDDS::ResourceFormatDDS() {
762 Image::_dds_mem_loader_func = load_mem_dds;
763 }