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