-
Notifications
You must be signed in to change notification settings - Fork 52
/
DirectXTexDDS.cpp
2002 lines (1661 loc) · 76.2 KB
/
DirectXTexDDS.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//-------------------------------------------------------------------------------------
// DirectXTexDDS.cpp
//
// DirectX Texture Library - Microsoft DirectDraw Surface (DDS) file format reader/writer
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// http://go.microsoft.com/fwlink/?LinkId=248926
//-------------------------------------------------------------------------------------
#include "directxtexp.h"
#include "dds.h"
namespace DirectX
{
//-------------------------------------------------------------------------------------
// Legacy format mapping table (used for DDS files without 'DX10' extended header)
//-------------------------------------------------------------------------------------
enum CONVERSION_FLAGS
{
CONV_FLAGS_NONE = 0x0,
CONV_FLAGS_EXPAND = 0x1, // Conversion requires expanded pixel size
CONV_FLAGS_NOALPHA = 0x2, // Conversion requires setting alpha to known value
CONV_FLAGS_SWIZZLE = 0x4, // BGR/RGB order swizzling required
CONV_FLAGS_PAL8 = 0x8, // Has an 8-bit palette
CONV_FLAGS_888 = 0x10, // Source is an 8:8:8 (24bpp) format
CONV_FLAGS_565 = 0x20, // Source is a 5:6:5 (16bpp) format
CONV_FLAGS_5551 = 0x40, // Source is a 5:5:5:1 (16bpp) format
CONV_FLAGS_4444 = 0x80, // Source is a 4:4:4:4 (16bpp) format
CONV_FLAGS_44 = 0x100, // Source is a 4:4 (8bpp) format
CONV_FLAGS_332 = 0x200, // Source is a 3:3:2 (8bpp) format
CONV_FLAGS_8332 = 0x400, // Source is a 8:3:3:2 (16bpp) format
CONV_FLAGS_A8P8 = 0x800, // Has an 8-bit palette with an alpha channel
CONV_FLAGS_DX10 = 0x10000, // Has the 'DX10' extension header
CONV_FLAGS_PMALPHA = 0x20000, // Contains premultiplied alpha data
CONV_FLAGS_L8 = 0x40000, // Source is a 8 luminance format
CONV_FLAGS_L16 = 0x80000, // Source is a 16 luminance format
CONV_FLAGS_A8L8 = 0x100000, // Source is a 8:8 luminance format
};
struct LegacyDDS
{
DXGI_FORMAT format;
DWORD convFlags;
DDS_PIXELFORMAT ddpf;
};
const LegacyDDS g_LegacyDDSMap[] =
{
{ DXGI_FORMAT_BC1_UNORM, CONV_FLAGS_NONE, DDSPF_DXT1 }, // D3DFMT_DXT1
{ DXGI_FORMAT_BC2_UNORM, CONV_FLAGS_NONE, DDSPF_DXT3 }, // D3DFMT_DXT3
{ DXGI_FORMAT_BC3_UNORM, CONV_FLAGS_NONE, DDSPF_DXT5 }, // D3DFMT_DXT5
{ DXGI_FORMAT_BC2_UNORM, CONV_FLAGS_PMALPHA, DDSPF_DXT2 }, // D3DFMT_DXT2
{ DXGI_FORMAT_BC3_UNORM, CONV_FLAGS_PMALPHA, DDSPF_DXT4 }, // D3DFMT_DXT4
{ DXGI_FORMAT_BC4_UNORM, CONV_FLAGS_NONE, DDSPF_BC4_UNORM },
{ DXGI_FORMAT_BC4_SNORM, CONV_FLAGS_NONE, DDSPF_BC4_SNORM },
{ DXGI_FORMAT_BC5_UNORM, CONV_FLAGS_NONE, DDSPF_BC5_UNORM },
{ DXGI_FORMAT_BC5_SNORM, CONV_FLAGS_NONE, DDSPF_BC5_SNORM },
{ DXGI_FORMAT_BC4_UNORM, CONV_FLAGS_NONE, { sizeof(DDS_PIXELFORMAT), DDS_FOURCC, MAKEFOURCC( 'A', 'T', 'I', '1' ), 0, 0, 0, 0, 0 } },
{ DXGI_FORMAT_BC5_UNORM, CONV_FLAGS_NONE, { sizeof(DDS_PIXELFORMAT), DDS_FOURCC, MAKEFOURCC( 'A', 'T', 'I', '2' ), 0, 0, 0, 0, 0 } },
{ DXGI_FORMAT_R8G8_B8G8_UNORM, CONV_FLAGS_NONE, DDSPF_R8G8_B8G8 }, // D3DFMT_R8G8_B8G8
{ DXGI_FORMAT_G8R8_G8B8_UNORM, CONV_FLAGS_NONE, DDSPF_G8R8_G8B8 }, // D3DFMT_G8R8_G8B8
{ DXGI_FORMAT_B8G8R8A8_UNORM, CONV_FLAGS_NONE, DDSPF_A8R8G8B8 }, // D3DFMT_A8R8G8B8 (uses DXGI 1.1 format)
{ DXGI_FORMAT_B8G8R8X8_UNORM, CONV_FLAGS_NONE, DDSPF_X8R8G8B8 }, // D3DFMT_X8R8G8B8 (uses DXGI 1.1 format)
{ DXGI_FORMAT_R8G8B8A8_UNORM, CONV_FLAGS_NONE, DDSPF_A8B8G8R8 }, // D3DFMT_A8B8G8R8
{ DXGI_FORMAT_R8G8B8A8_UNORM, CONV_FLAGS_NOALPHA, DDSPF_X8B8G8R8 }, // D3DFMT_X8B8G8R8
{ DXGI_FORMAT_R16G16_UNORM, CONV_FLAGS_NONE, DDSPF_G16R16 }, // D3DFMT_G16R16
{ DXGI_FORMAT_R10G10B10A2_UNORM, CONV_FLAGS_SWIZZLE, { sizeof(DDS_PIXELFORMAT), DDS_RGB, 0, 32, 0x000003ff, 0x000ffc00, 0x3ff00000, 0xc0000000 } }, // D3DFMT_A2R10G10B10 (D3DX reversal issue workaround)
{ DXGI_FORMAT_R10G10B10A2_UNORM, CONV_FLAGS_NONE, { sizeof(DDS_PIXELFORMAT), DDS_RGB, 0, 32, 0x3ff00000, 0x000ffc00, 0x000003ff, 0xc0000000 } }, // D3DFMT_A2B10G10R10 (D3DX reversal issue workaround)
{ DXGI_FORMAT_R8G8B8A8_UNORM, CONV_FLAGS_EXPAND
| CONV_FLAGS_NOALPHA
| CONV_FLAGS_888, DDSPF_R8G8B8 }, // D3DFMT_R8G8B8
{ DXGI_FORMAT_B5G6R5_UNORM, CONV_FLAGS_565, DDSPF_R5G6B5 }, // D3DFMT_R5G6B5
{ DXGI_FORMAT_B5G5R5A1_UNORM, CONV_FLAGS_5551, DDSPF_A1R5G5B5 }, // D3DFMT_A1R5G5B5
{ DXGI_FORMAT_B5G5R5A1_UNORM, CONV_FLAGS_5551
| CONV_FLAGS_NOALPHA, { sizeof(DDS_PIXELFORMAT), DDS_RGB, 0, 16, 0x7c00, 0x03e0, 0x001f, 0x0000 } }, // D3DFMT_X1R5G5B5
{ DXGI_FORMAT_R8G8B8A8_UNORM, CONV_FLAGS_EXPAND
| CONV_FLAGS_8332, { sizeof(DDS_PIXELFORMAT), DDS_RGB, 0, 16, 0x00e0, 0x001c, 0x0003, 0xff00 } }, // D3DFMT_A8R3G3B2
{ DXGI_FORMAT_B5G6R5_UNORM, CONV_FLAGS_EXPAND
| CONV_FLAGS_332, { sizeof(DDS_PIXELFORMAT), DDS_RGB, 0, 8, 0xe0, 0x1c, 0x03, 0x00 } }, // D3DFMT_R3G3B2
{ DXGI_FORMAT_R8_UNORM, CONV_FLAGS_NONE, DDSPF_L8 }, // D3DFMT_L8
{ DXGI_FORMAT_R16_UNORM, CONV_FLAGS_NONE, DDSPF_L16 }, // D3DFMT_L16
{ DXGI_FORMAT_R8G8_UNORM, CONV_FLAGS_NONE, DDSPF_A8L8 }, // D3DFMT_A8L8
{ DXGI_FORMAT_A8_UNORM, CONV_FLAGS_NONE, DDSPF_A8 }, // D3DFMT_A8
{ DXGI_FORMAT_R16G16B16A16_UNORM, CONV_FLAGS_NONE, { sizeof(DDS_PIXELFORMAT), DDS_FOURCC, 36, 0, 0, 0, 0, 0 } }, // D3DFMT_A16B16G16R16
{ DXGI_FORMAT_R16G16B16A16_SNORM, CONV_FLAGS_NONE, { sizeof(DDS_PIXELFORMAT), DDS_FOURCC, 110, 0, 0, 0, 0, 0 } }, // D3DFMT_Q16W16V16U16
{ DXGI_FORMAT_R16_FLOAT, CONV_FLAGS_NONE, { sizeof(DDS_PIXELFORMAT), DDS_FOURCC, 111, 0, 0, 0, 0, 0 } }, // D3DFMT_R16F
{ DXGI_FORMAT_R16G16_FLOAT, CONV_FLAGS_NONE, { sizeof(DDS_PIXELFORMAT), DDS_FOURCC, 112, 0, 0, 0, 0, 0 } }, // D3DFMT_G16R16F
{ DXGI_FORMAT_R16G16B16A16_FLOAT, CONV_FLAGS_NONE, { sizeof(DDS_PIXELFORMAT), DDS_FOURCC, 113, 0, 0, 0, 0, 0 } }, // D3DFMT_A16B16G16R16F
{ DXGI_FORMAT_R32_FLOAT, CONV_FLAGS_NONE, { sizeof(DDS_PIXELFORMAT), DDS_FOURCC, 114, 0, 0, 0, 0, 0 } }, // D3DFMT_R32F
{ DXGI_FORMAT_R32G32_FLOAT, CONV_FLAGS_NONE, { sizeof(DDS_PIXELFORMAT), DDS_FOURCC, 115, 0, 0, 0, 0, 0 } }, // D3DFMT_G32R32F
{ DXGI_FORMAT_R32G32B32A32_FLOAT, CONV_FLAGS_NONE, { sizeof(DDS_PIXELFORMAT), DDS_FOURCC, 116, 0, 0, 0, 0, 0 } }, // D3DFMT_A32B32G32R32F
{ DXGI_FORMAT_R32_FLOAT, CONV_FLAGS_NONE, { sizeof(DDS_PIXELFORMAT), DDS_RGB, 0, 32, 0xffffffff, 0x00000000, 0x00000000, 0x00000000 } }, // D3DFMT_R32F (D3DX uses FourCC 114 instead)
{ DXGI_FORMAT_R8G8B8A8_UNORM, CONV_FLAGS_EXPAND
| CONV_FLAGS_PAL8
| CONV_FLAGS_A8P8, { sizeof(DDS_PIXELFORMAT), DDS_PAL8, 0, 16, 0, 0, 0, 0 } }, // D3DFMT_A8P8
{ DXGI_FORMAT_R8G8B8A8_UNORM, CONV_FLAGS_EXPAND
| CONV_FLAGS_PAL8, { sizeof(DDS_PIXELFORMAT), DDS_PAL8, 0, 8, 0, 0, 0, 0 } }, // D3DFMT_P8
{ DXGI_FORMAT_B4G4R4A4_UNORM, CONV_FLAGS_4444, DDSPF_A4R4G4B4 }, // D3DFMT_A4R4G4B4 (uses DXGI 1.2 format)
{ DXGI_FORMAT_B4G4R4A4_UNORM, CONV_FLAGS_NOALPHA
| CONV_FLAGS_4444, { sizeof(DDS_PIXELFORMAT), DDS_RGB, 0, 16, 0x0f00, 0x00f0, 0x000f, 0x0000 } }, // D3DFMT_X4R4G4B4 (uses DXGI 1.2 format)
{ DXGI_FORMAT_B4G4R4A4_UNORM, CONV_FLAGS_EXPAND
| CONV_FLAGS_44, { sizeof(DDS_PIXELFORMAT), DDS_LUMINANCE, 0, 8, 0x0f, 0x00, 0x00, 0xf0 } }, // D3DFMT_A4L4 (uses DXGI 1.2 format)
{ DXGI_FORMAT_YUY2, CONV_FLAGS_NONE, DDSPF_YUY2 }, // D3DFMT_YUY2 (uses DXGI 1.2 format)
{ DXGI_FORMAT_YUY2, CONV_FLAGS_SWIZZLE, { sizeof(DDS_PIXELFORMAT), DDS_FOURCC, MAKEFOURCC('U','Y','V','Y'), 0, 0, 0, 0, 0 } }, // D3DFMT_UYVY (uses DXGI 1.2 format)
};
// Note that many common DDS reader/writers (including D3DX) swap the
// the RED/BLUE masks for 10:10:10:2 formats. We assumme
// below that the 'backwards' header mask is being used since it is most
// likely written by D3DX. The more robust solution is to use the 'DX10'
// header extension and specify the DXGI_FORMAT_R10G10B10A2_UNORM format directly
// We do not support the following legacy Direct3D 9 formats:
// BumpDuDv D3DFMT_V8U8, D3DFMT_Q8W8V8U8, D3DFMT_V16U16, D3DFMT_A2W10V10U10
// BumpLuminance D3DFMT_L6V5U5, D3DFMT_X8L8V8U8
// FourCC 117 D3DFMT_CxV8U8
// ZBuffer D3DFMT_D16_LOCKABLE
// FourCC 82 D3DFMT_D32F_LOCKABLE
static DXGI_FORMAT _GetDXGIFormat( const DDS_PIXELFORMAT& ddpf, DWORD flags, _Inout_ DWORD& convFlags )
{
const size_t MAP_SIZE = sizeof(g_LegacyDDSMap) / sizeof(LegacyDDS);
size_t index = 0;
for( index = 0; index < MAP_SIZE; ++index )
{
const LegacyDDS* entry = &g_LegacyDDSMap[index];
if ( ddpf.dwFlags & entry->ddpf.dwFlags )
{
if ( entry->ddpf.dwFlags & DDS_FOURCC )
{
if ( ddpf.dwFourCC == entry->ddpf.dwFourCC )
break;
}
else if ( entry->ddpf.dwFlags & DDS_PAL8 )
{
if ( ddpf.dwRGBBitCount == entry->ddpf.dwRGBBitCount )
break;
}
else if ( ddpf.dwRGBBitCount == entry->ddpf.dwRGBBitCount )
{
// RGB, RGBA, ALPHA, LUMINANCE
if ( ddpf.dwRBitMask == entry->ddpf.dwRBitMask
&& ddpf.dwGBitMask == entry->ddpf.dwGBitMask
&& ddpf.dwBBitMask == entry->ddpf.dwBBitMask
&& ddpf.dwABitMask == entry->ddpf.dwABitMask )
break;
}
}
}
if ( index >= MAP_SIZE )
return DXGI_FORMAT_UNKNOWN;
DWORD cflags = g_LegacyDDSMap[index].convFlags;
DXGI_FORMAT format = g_LegacyDDSMap[index].format;
if ( (cflags & CONV_FLAGS_EXPAND) && (flags & DDS_FLAGS_NO_LEGACY_EXPANSION) )
return DXGI_FORMAT_UNKNOWN;
if ( (format == DXGI_FORMAT_R10G10B10A2_UNORM) && (flags & DDS_FLAGS_NO_R10B10G10A2_FIXUP) )
{
cflags ^= CONV_FLAGS_SWIZZLE;
}
convFlags = cflags;
return format;
}
//-------------------------------------------------------------------------------------
// Decodes DDS header including optional DX10 extended header
//-------------------------------------------------------------------------------------
static HRESULT _DecodeDDSHeader( _In_reads_bytes_(size) LPCVOID pSource, size_t size, DWORD flags, _Out_ TexMetadata& metadata,
_Inout_ DWORD& convFlags )
{
if ( !pSource )
return E_INVALIDARG;
memset( &metadata, 0, sizeof(TexMetadata) );
if ( size < (sizeof(DDS_HEADER) + sizeof(uint32_t)) )
{
return HRESULT_FROM_WIN32( ERROR_INVALID_DATA );
}
// DDS files always start with the same magic number ("DDS ")
uint32_t dwMagicNumber = *reinterpret_cast<const uint32_t*>(pSource);
if ( dwMagicNumber != DDS_MAGIC )
{
return E_FAIL;
}
auto pHeader = reinterpret_cast<const DDS_HEADER*>( (const uint8_t*)pSource + sizeof( uint32_t ) );
// Verify header to validate DDS file
if ( pHeader->dwSize != sizeof(DDS_HEADER)
|| pHeader->ddspf.dwSize != sizeof(DDS_PIXELFORMAT) )
{
return E_FAIL;
}
metadata.mipLevels = pHeader->dwMipMapCount;
if ( metadata.mipLevels == 0 )
metadata.mipLevels = 1;
// Check for DX10 extension
if ( (pHeader->ddspf.dwFlags & DDS_FOURCC)
&& (MAKEFOURCC( 'D', 'X', '1', '0' ) == pHeader->ddspf.dwFourCC) )
{
// Buffer must be big enough for both headers and magic value
if ( size < ( sizeof(DDS_HEADER) + sizeof(uint32_t) + sizeof(DDS_HEADER_DXT10) ) )
{
return E_FAIL;
}
auto d3d10ext = reinterpret_cast<const DDS_HEADER_DXT10*>( (const uint8_t*)pSource + sizeof( uint32_t ) + sizeof(DDS_HEADER) );
convFlags |= CONV_FLAGS_DX10;
metadata.arraySize = d3d10ext->arraySize;
if ( metadata.arraySize == 0 )
{
return HRESULT_FROM_WIN32( ERROR_INVALID_DATA );
}
metadata.format = d3d10ext->dxgiFormat;
if ( !IsValid( metadata.format ) || IsPalettized( metadata.format ) )
{
return HRESULT_FROM_WIN32( ERROR_NOT_SUPPORTED );
}
static_assert( TEX_MISC_TEXTURECUBE == DDS_RESOURCE_MISC_TEXTURECUBE, "DDS header mismatch");
metadata.miscFlags = d3d10ext->miscFlag & ~TEX_MISC_TEXTURECUBE;
switch ( d3d10ext->resourceDimension )
{
case DDS_DIMENSION_TEXTURE1D:
// D3DX writes 1D textures with a fixed Height of 1
if ( (pHeader->dwFlags & DDS_HEIGHT) && pHeader->dwHeight != 1 )
{
return HRESULT_FROM_WIN32( ERROR_INVALID_DATA );
}
metadata.width = pHeader->dwWidth;
metadata.height = 1;
metadata.depth = 1;
metadata.dimension = TEX_DIMENSION_TEXTURE1D;
break;
case DDS_DIMENSION_TEXTURE2D:
if ( d3d10ext->miscFlag & DDS_RESOURCE_MISC_TEXTURECUBE )
{
metadata.miscFlags |= TEX_MISC_TEXTURECUBE;
metadata.arraySize *= 6;
}
metadata.width = pHeader->dwWidth;
metadata.height = pHeader->dwHeight;
metadata.depth = 1;
metadata.dimension = TEX_DIMENSION_TEXTURE2D;
break;
case DDS_DIMENSION_TEXTURE3D:
if ( !(pHeader->dwFlags & DDS_HEADER_FLAGS_VOLUME) )
{
return HRESULT_FROM_WIN32( ERROR_INVALID_DATA );
}
if ( metadata.arraySize > 1 )
return HRESULT_FROM_WIN32( ERROR_NOT_SUPPORTED );
metadata.width = pHeader->dwWidth;
metadata.height = pHeader->dwHeight;
metadata.depth = pHeader->dwDepth;
metadata.dimension = TEX_DIMENSION_TEXTURE3D;
break;
default:
return HRESULT_FROM_WIN32( ERROR_INVALID_DATA );
}
static_assert( TEX_MISC2_ALPHA_MODE_MASK == DDS_MISC_FLAGS2_ALPHA_MODE_MASK, "DDS header mismatch");
static_assert( TEX_ALPHA_MODE_UNKNOWN == DDS_ALPHA_MODE_UNKNOWN, "DDS header mismatch");
static_assert( TEX_ALPHA_MODE_STRAIGHT == DDS_ALPHA_MODE_STRAIGHT, "DDS header mismatch");
static_assert( TEX_ALPHA_MODE_PREMULTIPLIED == DDS_ALPHA_MODE_PREMULTIPLIED, "DDS header mismatch");
static_assert( TEX_ALPHA_MODE_OPAQUE == DDS_ALPHA_MODE_OPAQUE, "DDS header mismatch");
static_assert( TEX_ALPHA_MODE_CUSTOM == DDS_ALPHA_MODE_CUSTOM, "DDS header mismatch");
metadata.miscFlags2 = d3d10ext->miscFlags2;
}
else
{
metadata.arraySize = 1;
if ( pHeader->dwFlags & DDS_HEADER_FLAGS_VOLUME )
{
metadata.width = pHeader->dwWidth;
metadata.height = pHeader->dwHeight;
metadata.depth = pHeader->dwDepth;
metadata.dimension = TEX_DIMENSION_TEXTURE3D;
}
else
{
if ( pHeader->dwCaps2 & DDS_CUBEMAP )
{
// We require all six faces to be defined
if ( (pHeader->dwCaps2 & DDS_CUBEMAP_ALLFACES ) != DDS_CUBEMAP_ALLFACES )
return HRESULT_FROM_WIN32( ERROR_NOT_SUPPORTED );
metadata.arraySize = 6;
metadata.miscFlags |= TEX_MISC_TEXTURECUBE;
}
metadata.width = pHeader->dwWidth;
metadata.height = pHeader->dwHeight;
metadata.depth = 1;
metadata.dimension = TEX_DIMENSION_TEXTURE2D;
// Note there's no way for a legacy Direct3D 9 DDS to express a '1D' texture
}
metadata.format = _GetDXGIFormat( pHeader->ddspf, flags, convFlags );
if ( metadata.format == DXGI_FORMAT_UNKNOWN )
return HRESULT_FROM_WIN32( ERROR_NOT_SUPPORTED );
if ( convFlags & CONV_FLAGS_PMALPHA )
metadata.miscFlags2 |= TEX_ALPHA_MODE_PREMULTIPLIED;
// Special flag for handling LUMINANCE legacy formats
if ( flags & DDS_FLAGS_EXPAND_LUMINANCE )
{
switch ( metadata.format )
{
case DXGI_FORMAT_R8_UNORM:
metadata.format = DXGI_FORMAT_R8G8B8A8_UNORM;
convFlags |= CONV_FLAGS_L8 | CONV_FLAGS_EXPAND;
break;
case DXGI_FORMAT_R8G8_UNORM:
metadata.format = DXGI_FORMAT_R8G8B8A8_UNORM;
convFlags |= CONV_FLAGS_A8L8 | CONV_FLAGS_EXPAND;
break;
case DXGI_FORMAT_R16_UNORM:
metadata.format = DXGI_FORMAT_R16G16B16A16_UNORM;
convFlags |= CONV_FLAGS_L16 | CONV_FLAGS_EXPAND;
break;
}
}
}
// Special flag for handling BGR DXGI 1.1 formats
if (flags & DDS_FLAGS_FORCE_RGB)
{
switch ( metadata.format )
{
case DXGI_FORMAT_B8G8R8A8_UNORM:
metadata.format = DXGI_FORMAT_R8G8B8A8_UNORM;
convFlags |= CONV_FLAGS_SWIZZLE;
break;
case DXGI_FORMAT_B8G8R8X8_UNORM:
metadata.format = DXGI_FORMAT_R8G8B8A8_UNORM;
convFlags |= CONV_FLAGS_SWIZZLE | CONV_FLAGS_NOALPHA;
break;
case DXGI_FORMAT_B8G8R8A8_TYPELESS:
metadata.format = DXGI_FORMAT_R8G8B8A8_TYPELESS;
convFlags |= CONV_FLAGS_SWIZZLE;
break;
case DXGI_FORMAT_B8G8R8A8_UNORM_SRGB:
metadata.format = DXGI_FORMAT_R8G8B8A8_UNORM_SRGB;
convFlags |= CONV_FLAGS_SWIZZLE;
break;
case DXGI_FORMAT_B8G8R8X8_TYPELESS:
metadata.format = DXGI_FORMAT_R8G8B8A8_TYPELESS;
convFlags |= CONV_FLAGS_SWIZZLE | CONV_FLAGS_NOALPHA;
break;
case DXGI_FORMAT_B8G8R8X8_UNORM_SRGB:
metadata.format = DXGI_FORMAT_R8G8B8A8_UNORM_SRGB;
convFlags |= CONV_FLAGS_SWIZZLE | CONV_FLAGS_NOALPHA;
break;
}
}
// Special flag for handling 16bpp formats
if (flags & DDS_FLAGS_NO_16BPP)
{
switch ( metadata.format )
{
case DXGI_FORMAT_B5G6R5_UNORM:
case DXGI_FORMAT_B5G5R5A1_UNORM:
case DXGI_FORMAT_B4G4R4A4_UNORM:
metadata.format = DXGI_FORMAT_R8G8B8A8_UNORM;
convFlags |= CONV_FLAGS_EXPAND;
if ( metadata.format == DXGI_FORMAT_B5G6R5_UNORM )
convFlags |= CONV_FLAGS_NOALPHA;
}
}
return S_OK;
}
//-------------------------------------------------------------------------------------
// Encodes DDS file header (magic value, header, optional DX10 extended header)
//-------------------------------------------------------------------------------------
_Use_decl_annotations_
HRESULT _EncodeDDSHeader( const TexMetadata& metadata, DWORD flags,
LPVOID pDestination, size_t maxsize, size_t& required )
{
if ( !IsValid( metadata.format ) )
return E_INVALIDARG;
if ( IsPalettized( metadata.format ) )
return HRESULT_FROM_WIN32( ERROR_NOT_SUPPORTED );
if ( metadata.arraySize > 1 )
{
if ( (metadata.arraySize != 6) || (metadata.dimension != TEX_DIMENSION_TEXTURE2D) || !(metadata.IsCubemap()) )
{
// Texture1D arrays, Texture2D arrays, and Cubemap arrays must be stored using 'DX10' extended header
flags |= DDS_FLAGS_FORCE_DX10_EXT;
}
}
if ( flags & DDS_FLAGS_FORCE_DX10_EXT_MISC2 )
{
flags |= DDS_FLAGS_FORCE_DX10_EXT;
}
DDS_PIXELFORMAT ddpf = { 0 };
if ( !(flags & DDS_FLAGS_FORCE_DX10_EXT) )
{
switch( metadata.format )
{
case DXGI_FORMAT_R8G8B8A8_UNORM: memcpy_s( &ddpf, sizeof(ddpf), &DDSPF_A8B8G8R8, sizeof(DDS_PIXELFORMAT) ); break;
case DXGI_FORMAT_R16G16_UNORM: memcpy_s( &ddpf, sizeof(ddpf), &DDSPF_G16R16, sizeof(DDS_PIXELFORMAT) ); break;
case DXGI_FORMAT_R8G8_UNORM: memcpy_s( &ddpf, sizeof(ddpf), &DDSPF_A8L8, sizeof(DDS_PIXELFORMAT) ); break;
case DXGI_FORMAT_R16_UNORM: memcpy_s( &ddpf, sizeof(ddpf), &DDSPF_L16, sizeof(DDS_PIXELFORMAT) ); break;
case DXGI_FORMAT_R8_UNORM: memcpy_s( &ddpf, sizeof(ddpf), &DDSPF_L8, sizeof(DDS_PIXELFORMAT) ); break;
case DXGI_FORMAT_A8_UNORM: memcpy_s( &ddpf, sizeof(ddpf), &DDSPF_A8, sizeof(DDS_PIXELFORMAT) ); break;
case DXGI_FORMAT_R8G8_B8G8_UNORM: memcpy_s( &ddpf, sizeof(ddpf), &DDSPF_R8G8_B8G8, sizeof(DDS_PIXELFORMAT) ); break;
case DXGI_FORMAT_G8R8_G8B8_UNORM: memcpy_s( &ddpf, sizeof(ddpf), &DDSPF_G8R8_G8B8, sizeof(DDS_PIXELFORMAT) ); break;
case DXGI_FORMAT_BC1_UNORM: memcpy_s( &ddpf, sizeof(ddpf), &DDSPF_DXT1, sizeof(DDS_PIXELFORMAT) ); break;
case DXGI_FORMAT_BC2_UNORM: memcpy_s( &ddpf, sizeof(ddpf), metadata.IsPMAlpha() ? (&DDSPF_DXT2) : (&DDSPF_DXT3), sizeof(DDS_PIXELFORMAT) ); break;
case DXGI_FORMAT_BC3_UNORM: memcpy_s( &ddpf, sizeof(ddpf), metadata.IsPMAlpha() ? (&DDSPF_DXT4) : (&DDSPF_DXT5), sizeof(DDS_PIXELFORMAT) ); break;
case DXGI_FORMAT_BC4_UNORM: memcpy_s( &ddpf, sizeof(ddpf), &DDSPF_BC4_UNORM, sizeof(DDS_PIXELFORMAT) ); break;
case DXGI_FORMAT_BC4_SNORM: memcpy_s( &ddpf, sizeof(ddpf), &DDSPF_BC4_SNORM, sizeof(DDS_PIXELFORMAT) ); break;
case DXGI_FORMAT_BC5_UNORM: memcpy_s( &ddpf, sizeof(ddpf), &DDSPF_BC5_UNORM, sizeof(DDS_PIXELFORMAT) ); break;
case DXGI_FORMAT_BC5_SNORM: memcpy_s( &ddpf, sizeof(ddpf), &DDSPF_BC5_SNORM, sizeof(DDS_PIXELFORMAT) ); break;
case DXGI_FORMAT_B5G6R5_UNORM: memcpy_s( &ddpf, sizeof(ddpf), &DDSPF_R5G6B5, sizeof(DDS_PIXELFORMAT) ); break;
case DXGI_FORMAT_B5G5R5A1_UNORM: memcpy_s( &ddpf, sizeof(ddpf), &DDSPF_A1R5G5B5, sizeof(DDS_PIXELFORMAT) ); break;
case DXGI_FORMAT_B8G8R8A8_UNORM: memcpy_s( &ddpf, sizeof(ddpf), &DDSPF_A8R8G8B8, sizeof(DDS_PIXELFORMAT) ); break; // DXGI 1.1
case DXGI_FORMAT_B8G8R8X8_UNORM: memcpy_s( &ddpf, sizeof(ddpf), &DDSPF_X8R8G8B8, sizeof(DDS_PIXELFORMAT) ); break; // DXGI 1.1
case DXGI_FORMAT_B4G4R4A4_UNORM: memcpy_s( &ddpf, sizeof(ddpf), &DDSPF_A4R4G4B4, sizeof(DDS_PIXELFORMAT) ); break; // DXGI 1.2
case DXGI_FORMAT_YUY2: memcpy_s( &ddpf, sizeof(ddpf), &DDSPF_YUY2, sizeof(DDS_PIXELFORMAT) ); break; // DXGI 1.2
// Legacy D3DX formats using D3DFMT enum value as FourCC
case DXGI_FORMAT_R32G32B32A32_FLOAT:
ddpf.dwSize = sizeof(DDS_PIXELFORMAT); ddpf.dwFlags = DDS_FOURCC; ddpf.dwFourCC = 116; // D3DFMT_A32B32G32R32F
break;
case DXGI_FORMAT_R16G16B16A16_FLOAT:
ddpf.dwSize = sizeof(DDS_PIXELFORMAT); ddpf.dwFlags = DDS_FOURCC; ddpf.dwFourCC = 113; // D3DFMT_A16B16G16R16F
break;
case DXGI_FORMAT_R16G16B16A16_UNORM:
ddpf.dwSize = sizeof(DDS_PIXELFORMAT); ddpf.dwFlags = DDS_FOURCC; ddpf.dwFourCC = 36; // D3DFMT_A16B16G16R16
break;
case DXGI_FORMAT_R16G16B16A16_SNORM:
ddpf.dwSize = sizeof(DDS_PIXELFORMAT); ddpf.dwFlags = DDS_FOURCC; ddpf.dwFourCC = 110; // D3DFMT_Q16W16V16U16
break;
case DXGI_FORMAT_R32G32_FLOAT:
ddpf.dwSize = sizeof(DDS_PIXELFORMAT); ddpf.dwFlags = DDS_FOURCC; ddpf.dwFourCC = 115; // D3DFMT_G32R32F
break;
case DXGI_FORMAT_R16G16_FLOAT:
ddpf.dwSize = sizeof(DDS_PIXELFORMAT); ddpf.dwFlags = DDS_FOURCC; ddpf.dwFourCC = 112; // D3DFMT_G16R16F
break;
case DXGI_FORMAT_R32_FLOAT:
ddpf.dwSize = sizeof(DDS_PIXELFORMAT); ddpf.dwFlags = DDS_FOURCC; ddpf.dwFourCC = 114; // D3DFMT_R32F
break;
case DXGI_FORMAT_R16_FLOAT:
ddpf.dwSize = sizeof(DDS_PIXELFORMAT); ddpf.dwFlags = DDS_FOURCC; ddpf.dwFourCC = 111; // D3DFMT_R16F
break;
}
}
required = sizeof(uint32_t) + sizeof(DDS_HEADER);
if ( ddpf.dwSize == 0 )
required += sizeof(DDS_HEADER_DXT10);
if ( !pDestination )
return S_OK;
if ( maxsize < required )
return E_NOT_SUFFICIENT_BUFFER;
*reinterpret_cast<uint32_t*>(pDestination) = DDS_MAGIC;
auto header = reinterpret_cast<DDS_HEADER*>( reinterpret_cast<uint8_t*>(pDestination) + sizeof(uint32_t) );
assert( header );
memset( header, 0, sizeof(DDS_HEADER ) );
header->dwSize = sizeof( DDS_HEADER );
header->dwFlags = DDS_HEADER_FLAGS_TEXTURE;
header->dwCaps = DDS_SURFACE_FLAGS_TEXTURE;
if (metadata.mipLevels > 0)
{
header->dwFlags |= DDS_HEADER_FLAGS_MIPMAP;
#ifdef _M_X64
if ( metadata.mipLevels > 0xFFFFFFFF )
return E_INVALIDARG;
#endif
header->dwMipMapCount = static_cast<uint32_t>( metadata.mipLevels );
if ( header->dwMipMapCount > 1 )
header->dwCaps |= DDS_SURFACE_FLAGS_MIPMAP;
}
switch( metadata.dimension )
{
case TEX_DIMENSION_TEXTURE1D:
#ifdef _M_X64
if ( metadata.width > 0xFFFFFFFF )
return E_INVALIDARG;
#endif
header->dwWidth = static_cast<uint32_t>( metadata.width );
header->dwHeight = header->dwDepth = 1;
break;
case TEX_DIMENSION_TEXTURE2D:
#ifdef _M_X64
if ( metadata.height > 0xFFFFFFFF
|| metadata.width > 0xFFFFFFFF)
return E_INVALIDARG;
#endif
header->dwHeight = static_cast<uint32_t>( metadata.height );
header->dwWidth = static_cast<uint32_t>( metadata.width );
header->dwDepth = 1;
if ( metadata.IsCubemap() )
{
header->dwCaps |= DDS_SURFACE_FLAGS_CUBEMAP;
header->dwCaps2 |= DDS_CUBEMAP_ALLFACES;
}
break;
case TEX_DIMENSION_TEXTURE3D:
#ifdef _M_X64
if ( metadata.height > 0xFFFFFFFF
|| metadata.width > 0xFFFFFFFF
|| metadata.depth > 0xFFFFFFFF )
return E_INVALIDARG;
#endif
header->dwFlags |= DDS_HEADER_FLAGS_VOLUME;
header->dwCaps2 |= DDS_FLAGS_VOLUME;
header->dwHeight = static_cast<uint32_t>( metadata.height );
header->dwWidth = static_cast<uint32_t>( metadata.width );
header->dwDepth = static_cast<uint32_t>( metadata.depth );
break;
default:
return E_FAIL;
}
size_t rowPitch, slicePitch;
ComputePitch( metadata.format, metadata.width, metadata.height, rowPitch, slicePitch, CP_FLAGS_NONE );
#ifdef _M_X64
if ( slicePitch > 0xFFFFFFFF
|| rowPitch > 0xFFFFFFFF )
return E_FAIL;
#endif
if ( IsCompressed( metadata.format ) )
{
header->dwFlags |= DDS_HEADER_FLAGS_LINEARSIZE;
header->dwPitchOrLinearSize = static_cast<uint32_t>( slicePitch );
}
else
{
header->dwFlags |= DDS_HEADER_FLAGS_PITCH;
header->dwPitchOrLinearSize = static_cast<uint32_t>( rowPitch );
}
if ( ddpf.dwSize == 0 )
{
memcpy_s( &header->ddspf, sizeof(header->ddspf), &DDSPF_DX10, sizeof(DDS_PIXELFORMAT) );
auto ext = reinterpret_cast<DDS_HEADER_DXT10*>( reinterpret_cast<uint8_t*>(header) + sizeof(DDS_HEADER) );
assert( ext );
memset( ext, 0, sizeof(DDS_HEADER_DXT10) );
ext->dxgiFormat = metadata.format;
ext->resourceDimension = metadata.dimension;
#ifdef _M_X64
if ( metadata.arraySize > 0xFFFFFFFF )
return E_INVALIDARG;
#endif
static_assert( TEX_MISC_TEXTURECUBE == DDS_RESOURCE_MISC_TEXTURECUBE, "DDS header mismatch");
ext->miscFlag = metadata.miscFlags & ~TEX_MISC_TEXTURECUBE;
if ( metadata.miscFlags & TEX_MISC_TEXTURECUBE )
{
ext->miscFlag |= TEX_MISC_TEXTURECUBE;
assert( (metadata.arraySize % 6) == 0 );
ext->arraySize = static_cast<UINT>( metadata.arraySize / 6 );
}
else
{
ext->arraySize = static_cast<UINT>( metadata.arraySize );
}
static_assert( TEX_MISC2_ALPHA_MODE_MASK == DDS_MISC_FLAGS2_ALPHA_MODE_MASK, "DDS header mismatch");
static_assert( TEX_ALPHA_MODE_UNKNOWN == DDS_ALPHA_MODE_UNKNOWN, "DDS header mismatch");
static_assert( TEX_ALPHA_MODE_STRAIGHT == DDS_ALPHA_MODE_STRAIGHT, "DDS header mismatch");
static_assert( TEX_ALPHA_MODE_PREMULTIPLIED == DDS_ALPHA_MODE_PREMULTIPLIED, "DDS header mismatch");
static_assert( TEX_ALPHA_MODE_OPAQUE == DDS_ALPHA_MODE_OPAQUE, "DDS header mismatch");
static_assert( TEX_ALPHA_MODE_CUSTOM == DDS_ALPHA_MODE_CUSTOM, "DDS header mismatch");
if ( flags & DDS_FLAGS_FORCE_DX10_EXT_MISC2 )
{
// This was formerly 'reserved'. D3DX10 and D3DX11 will fail if this value is anything other than 0
ext->miscFlags2 = metadata.miscFlags2;
}
}
else
{
memcpy_s( &header->ddspf, sizeof(header->ddspf), &ddpf, sizeof(ddpf) );
}
return S_OK;
}
//-------------------------------------------------------------------------------------
// Converts an image row with optional clearing of alpha value to 1.0
// Returns true if supported, false if expansion case not supported
//-------------------------------------------------------------------------------------
enum TEXP_LEGACY_FORMAT
{
TEXP_LEGACY_UNKNOWN = 0,
TEXP_LEGACY_R8G8B8,
TEXP_LEGACY_R3G3B2,
TEXP_LEGACY_A8R3G3B2,
TEXP_LEGACY_P8,
TEXP_LEGACY_A8P8,
TEXP_LEGACY_A4L4,
TEXP_LEGACY_B4G4R4A4,
TEXP_LEGACY_L8,
TEXP_LEGACY_L16,
TEXP_LEGACY_A8L8
};
inline static TEXP_LEGACY_FORMAT _FindLegacyFormat( DWORD flags )
{
TEXP_LEGACY_FORMAT lformat = TEXP_LEGACY_UNKNOWN;
if ( flags & CONV_FLAGS_PAL8 )
{
lformat = ( flags & CONV_FLAGS_A8P8 ) ? TEXP_LEGACY_A8P8 : TEXP_LEGACY_P8;
}
else if ( flags & CONV_FLAGS_888 )
lformat = TEXP_LEGACY_R8G8B8;
else if ( flags & CONV_FLAGS_332 )
lformat = TEXP_LEGACY_R3G3B2;
else if ( flags & CONV_FLAGS_8332 )
lformat = TEXP_LEGACY_A8R3G3B2;
else if ( flags & CONV_FLAGS_44 )
lformat = TEXP_LEGACY_A4L4;
else if ( flags & CONV_FLAGS_4444 )
lformat = TEXP_LEGACY_B4G4R4A4;
else if ( flags & CONV_FLAGS_L8 )
lformat = TEXP_LEGACY_L8;
else if ( flags & CONV_FLAGS_L16 )
lformat = TEXP_LEGACY_L16;
else if ( flags & CONV_FLAGS_A8L8 )
lformat = TEXP_LEGACY_A8L8;
return lformat;
}
_Success_(return != false)
static bool _LegacyExpandScanline( _Out_writes_bytes_(outSize) LPVOID pDestination, size_t outSize, _In_ DXGI_FORMAT outFormat,
_In_reads_bytes_(inSize) LPCVOID pSource, size_t inSize, _In_ TEXP_LEGACY_FORMAT inFormat,
_In_reads_opt_(256) const uint32_t* pal8, _In_ DWORD flags )
{
assert( pDestination && outSize > 0 );
assert( pSource && inSize > 0 );
assert( IsValid(outFormat) && !IsPlanar(outFormat) && !IsPalettized(outFormat) );
switch( inFormat )
{
case TEXP_LEGACY_R8G8B8:
if ( outFormat != DXGI_FORMAT_R8G8B8A8_UNORM )
return false;
// D3DFMT_R8G8B8 -> DXGI_FORMAT_R8G8B8A8_UNORM
if ( inSize >= 3 && outSize >= 4 )
{
const uint8_t * __restrict sPtr = reinterpret_cast<const uint8_t*>(pSource);
uint32_t * __restrict dPtr = reinterpret_cast<uint32_t*>(pDestination);
for( size_t ocount = 0, icount = 0; ( ( icount < ( inSize - 2 ) ) && ( ocount < ( outSize - 3 ) ) ); icount += 3, ocount += 4 )
{
// 24bpp Direct3D 9 files are actually BGR, so need to swizzle as well
uint32_t t1 = ( *(sPtr) << 16 );
uint32_t t2 = ( *(sPtr+1) << 8 );
uint32_t t3 = *(sPtr+2);
*(dPtr++) = t1 | t2 | t3 | 0xff000000;
sPtr += 3;
}
return true;
}
return false;
case TEXP_LEGACY_R3G3B2:
switch( outFormat )
{
case DXGI_FORMAT_R8G8B8A8_UNORM:
// D3DFMT_R3G3B2 -> DXGI_FORMAT_R8G8B8A8_UNORM
if ( inSize >= 1 && outSize >= 4 )
{
const uint8_t* __restrict sPtr = reinterpret_cast<const uint8_t*>(pSource);
uint32_t * __restrict dPtr = reinterpret_cast<uint32_t*>(pDestination);
for( size_t ocount = 0, icount = 0; ( ( icount < inSize ) && ( ocount < ( outSize - 3 ) ) ); ++icount, ocount += 4 )
{
uint8_t t = *(sPtr++);
uint32_t t1 = (t & 0xe0) | ((t & 0xe0) >> 3) | ((t & 0xc0) >> 6);
uint32_t t2 = ((t & 0x1c) << 11) | ((t & 0x1c) << 8) | ((t & 0x18) << 5);
uint32_t t3 = ((t & 0x03) << 22) | ((t & 0x03) << 20) | ((t & 0x03) << 18) | ((t & 0x03) << 16);
*(dPtr++) = t1 | t2 | t3 | 0xff000000;
}
return true;
}
return false;
case DXGI_FORMAT_B5G6R5_UNORM:
// D3DFMT_R3G3B2 -> DXGI_FORMAT_B5G6R5_UNORM
if ( inSize >= 1 && outSize >= 2 )
{
const uint8_t* __restrict sPtr = reinterpret_cast<const uint8_t*>(pSource);
uint16_t * __restrict dPtr = reinterpret_cast<uint16_t*>(pDestination);
for( size_t ocount = 0, icount = 0; ( ( icount < inSize ) && ( ocount < ( outSize - 1 ) ) ); ++icount, ocount += 2 )
{
uint8_t t = *(sPtr++);
uint16_t t1 = ((t & 0xe0) << 8) | ((t & 0xc0) << 5);
uint16_t t2 = ((t & 0x1c) << 6) | ((t & 0x1c) << 3);
uint16_t t3 = ((t & 0x03) << 3) | ((t & 0x03) << 1) | ((t & 0x02) >> 1);
*(dPtr++) = t1 | t2 | t3;
}
return true;
}
return false;
}
break;
case TEXP_LEGACY_A8R3G3B2:
if ( outFormat != DXGI_FORMAT_R8G8B8A8_UNORM )
return false;
// D3DFMT_A8R3G3B2 -> DXGI_FORMAT_R8G8B8A8_UNORM
if ( inSize >= 2 && outSize >= 4 )
{
const uint16_t* __restrict sPtr = reinterpret_cast<const uint16_t*>(pSource);
uint32_t * __restrict dPtr = reinterpret_cast<uint32_t*>(pDestination);
for( size_t ocount = 0, icount = 0; ( ( icount < ( inSize - 1 ) ) && ( ocount < ( outSize - 3 ) ) ); icount += 2, ocount += 4 )
{
uint16_t t = *(sPtr++);
uint32_t t1 = (t & 0x00e0) | ((t & 0x00e0) >> 3) | ((t & 0x00c0) >> 6);
uint32_t t2 = ((t & 0x001c) << 11) | ((t & 0x001c) << 8) | ((t & 0x0018) << 5);
uint32_t t3 = ((t & 0x0003) << 22) | ((t & 0x0003) << 20) | ((t & 0x0003) << 18) | ((t & 0x0003) << 16);
uint32_t ta = ( flags & TEXP_SCANLINE_SETALPHA ) ? 0xff000000 : ((t & 0xff00) << 16);
*(dPtr++) = t1 | t2 | t3 | ta;
}
return true;
}
return false;
case TEXP_LEGACY_P8:
if ( (outFormat != DXGI_FORMAT_R8G8B8A8_UNORM) || !pal8 )
return false;
// D3DFMT_P8 -> DXGI_FORMAT_R8G8B8A8_UNORM
if ( inSize >= 1 && outSize >= 4 )
{
const uint8_t* __restrict sPtr = reinterpret_cast<const uint8_t*>(pSource);
uint32_t * __restrict dPtr = reinterpret_cast<uint32_t*>(pDestination);
for( size_t ocount = 0, icount = 0; ( ( icount < inSize ) && ( ocount < ( outSize - 3 ) ) ); ++icount, ocount += 4 )
{
uint8_t t = *(sPtr++);
*(dPtr++) = pal8[ t ];
}
return true;
}
return false;
case TEXP_LEGACY_A8P8:
if ( (outFormat != DXGI_FORMAT_R8G8B8A8_UNORM) || !pal8 )
return false;
// D3DFMT_A8P8 -> DXGI_FORMAT_R8G8B8A8_UNORM
if ( inSize >= 2 && outSize >= 4 )
{
const uint16_t* __restrict sPtr = reinterpret_cast<const uint16_t*>(pSource);
uint32_t * __restrict dPtr = reinterpret_cast<uint32_t*>(pDestination);
for( size_t ocount = 0, icount = 0; ( ( icount < ( inSize - 1 ) ) && ( ocount < ( outSize - 3 ) ) ); icount += 2, ocount += 4 )
{
uint16_t t = *(sPtr++);
uint32_t t1 = pal8[ t & 0xff ];
uint32_t ta = ( flags & TEXP_SCANLINE_SETALPHA ) ? 0xff000000 : ((t & 0xff00) << 16);
*(dPtr++) = t1 | ta;
}
return true;
}
return false;
case TEXP_LEGACY_A4L4:
switch( outFormat )
{
case DXGI_FORMAT_B4G4R4A4_UNORM :
// D3DFMT_A4L4 -> DXGI_FORMAT_B4G4R4A4_UNORM
if ( inSize >= 1 && outSize >= 2 )
{
const uint8_t * __restrict sPtr = reinterpret_cast<const uint8_t*>(pSource);
uint16_t * __restrict dPtr = reinterpret_cast<uint16_t*>(pDestination);
for( size_t ocount = 0, icount = 0; ( ( icount < inSize ) && ( ocount < ( outSize - 1 ) ) ); ++icount, ocount += 2 )
{
uint8_t t = *(sPtr++);
uint16_t t1 = (t & 0x0f);
uint16_t ta = ( flags & TEXP_SCANLINE_SETALPHA ) ? 0xf000 : ((t & 0xf0) << 8);
*(dPtr++) = t1 | (t1 << 4) | (t1 << 8) | ta;
}
return true;
}
return false;
case DXGI_FORMAT_R8G8B8A8_UNORM:
// D3DFMT_A4L4 -> DXGI_FORMAT_R8G8B8A8_UNORM
if ( inSize >= 1 && outSize >= 4 )
{
const uint8_t * __restrict sPtr = reinterpret_cast<const uint8_t*>(pSource);
uint32_t * __restrict dPtr = reinterpret_cast<uint32_t*>(pDestination);
for( size_t ocount = 0, icount = 0; ( ( icount < inSize ) && ( ocount < ( outSize - 3 ) ) ); ++icount, ocount += 4 )
{
uint8_t t = *(sPtr++);
uint32_t t1 = ((t & 0x0f) << 4) | (t & 0x0f);
uint32_t ta = ( flags & TEXP_SCANLINE_SETALPHA ) ? 0xff000000 : (((t & 0xf0) << 24) | ((t & 0xf0) << 20));
*(dPtr++) = t1 | (t1 << 8) | (t1 << 16) | ta;
}
return true;
}
return false;
}
break;
case TEXP_LEGACY_B4G4R4A4:
if (outFormat != DXGI_FORMAT_R8G8B8A8_UNORM)
return false;
// D3DFMT_A4R4G4B4 -> DXGI_FORMAT_R8G8B8A8_UNORM
if ( inSize >= 2 && outSize >= 4 )
{
const uint16_t * __restrict sPtr = reinterpret_cast<const uint16_t*>(pSource);
uint32_t * __restrict dPtr = reinterpret_cast<uint32_t*>(pDestination);
for( size_t ocount = 0, icount = 0; ( ( icount < ( inSize - 1 ) ) && ( ocount < ( outSize - 3 ) ) ); icount += 2, ocount += 4 )
{
uint16_t t = *(sPtr++);
uint32_t t1 = ((t & 0x0f00) >> 4) | ((t & 0x0f00) >> 8);
uint32_t t2 = ((t & 0x00f0) << 8) | ((t & 0x00f0) << 4);
uint32_t t3 = ((t & 0x000f) << 20) | ((t & 0x000f) << 16);
uint32_t ta = ( flags & TEXP_SCANLINE_SETALPHA ) ? 0xff000000 : (((t & 0xf000) << 16) | ((t & 0xf000) << 12));
*(dPtr++) = t1 | t2 | t3 | ta;
}
return true;
}
return false;
case TEXP_LEGACY_L8:
if (outFormat != DXGI_FORMAT_R8G8B8A8_UNORM)
return false;
// D3DFMT_L8 -> DXGI_FORMAT_R8G8B8A8_UNORM
if ( inSize >= 1 && outSize >= 4 )
{
const uint8_t * __restrict sPtr = reinterpret_cast<const uint8_t*>(pSource);
uint32_t * __restrict dPtr = reinterpret_cast<uint32_t*>(pDestination);
for( size_t ocount = 0, icount = 0; ( ( icount < inSize ) && ( ocount < ( outSize - 3 ) ) ); ++icount, ocount += 4 )
{
uint32_t t1 = *(sPtr++);
uint32_t t2 = (t1 << 8);
uint32_t t3 = (t1 << 16);
*(dPtr++) = t1 | t2 | t3 | 0xff000000;
}
return true;
}
return false;
case TEXP_LEGACY_L16:
if (outFormat != DXGI_FORMAT_R16G16B16A16_UNORM)
return false;
// D3DFMT_L16 -> DXGI_FORMAT_R16G16B16A16_UNORM
if ( inSize >= 2 && outSize >= 8 )
{
const uint16_t* __restrict sPtr = reinterpret_cast<const uint16_t*>(pSource);
uint64_t * __restrict dPtr = reinterpret_cast<uint64_t*>(pDestination);
for( size_t ocount = 0, icount = 0; ( ( icount < ( inSize - 1 ) ) && ( ocount < ( outSize - 7 ) ) ); icount += 2, ocount += 8 )
{
uint16_t t = *(sPtr++);
uint64_t t1 = t;
uint64_t t2 = (t1 << 16);
uint64_t t3 = (t1 << 32);
*(dPtr++) = t1 | t2 | t3 | 0xffff000000000000;
}
return true;
}
return false;
case TEXP_LEGACY_A8L8:
if (outFormat != DXGI_FORMAT_R8G8B8A8_UNORM)
return false;
// D3DFMT_A8L8 -> DXGI_FORMAT_R8G8B8A8_UNORM
if ( inSize >= 2 && outSize >= 4 )
{
const uint16_t* __restrict sPtr = reinterpret_cast<const uint16_t*>(pSource);
uint32_t * __restrict dPtr = reinterpret_cast<uint32_t*>(pDestination);