-
Notifications
You must be signed in to change notification settings - Fork 226
/
Copy pathtexconv.cpp
3803 lines (3277 loc) · 130 KB
/
texconv.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
//--------------------------------------------------------------------------------------
// File: TexConv.cpp
//
// DirectX Texture Converter for Xbox One
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//--------------------------------------------------------------------------------------
#ifndef _M_X64
#error This tool is only supported for x64 native
#endif
#pragma warning(push)
#pragma warning(disable : 4005)
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#define NODRAWTEXT
#define NOMCX
#define NOSERVICE
#define NOHELP
#pragma warning(pop)
#include <ShlObj.h>
#include <algorithm>
#include <cassert>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cwchar>
#include <cwctype>
#include <fstream>
#include <iterator>
#include <list>
#include <locale>
#include <memory>
#include <new>
#include <set>
#include <string>
#include <tuple>
#include <wrl\client.h>
#include <d3d11.h>
#include <dxgi.h>
#include <dxgiformat.h>
#include <wincodec.h>
#pragma warning(disable : 4619 4616 4091 4838 26812)
#include "DirectXTex.h"
#include "DirectXTexXbox.h"
#include "DirectXPackedVector.h"
#include <xdk.h>
#include <xg.h>
//Comment to remove support for OpenEXR (.exr)
#define USE_OPENEXR
#ifdef USE_OPENEXR
// See <https://github.com/Microsoft/DirectXTex/wiki/Adding-OpenEXR> for details
#include "DirectXTexEXR.h"
#endif
using namespace DirectX;
using namespace DirectX::PackedVector;
using Microsoft::WRL::ComPtr;
namespace
{
enum OPTIONS : uint64_t
{
OPT_RECURSIVE = 1,
OPT_FILELIST,
OPT_WIDTH,
OPT_HEIGHT,
OPT_MIPLEVELS,
OPT_FORMAT,
OPT_FILTER,
OPT_SRGBI,
OPT_SRGBO,
OPT_SRGB,
OPT_PREFIX,
OPT_SUFFIX,
OPT_OUTPUTDIR,
OPT_TOLOWER,
OPT_OVERWRITE,
OPT_FILETYPE,
OPT_HFLIP,
OPT_VFLIP,
OPT_DDS_DWORD_ALIGN,
OPT_DDS_BAD_DXTN_TAILS,
OPT_USE_DX10,
OPT_USE_DX9,
OPT_TGA20,
OPT_WIC_QUALITY,
OPT_WIC_LOSSLESS,
OPT_WIC_MULTIFRAME,
OPT_NOLOGO,
OPT_TIMING,
OPT_SEPALPHA,
OPT_NO_WIC,
OPT_TYPELESS_UNORM,
OPT_TYPELESS_FLOAT,
OPT_PREMUL_ALPHA,
OPT_DEMUL_ALPHA,
OPT_EXPAND_LUMINANCE,
OPT_TA_WRAP,
OPT_TA_MIRROR,
OPT_FORCE_SINGLEPROC,
OPT_GPU,
OPT_NOGPU,
OPT_FEATURE_LEVEL,
OPT_FIT_POWEROF2,
OPT_ALPHA_THRESHOLD,
OPT_ALPHA_WEIGHT,
OPT_NORMAL_MAP,
OPT_NORMAL_MAP_AMPLITUDE,
OPT_BC_COMPRESS,
OPT_COLORKEY,
OPT_TONEMAP,
OPT_X2_BIAS,
OPT_PRESERVE_ALPHA_COVERAGE,
OPT_INVERT_Y,
OPT_RECONSTRUCT_Z,
OPT_ROTATE_COLOR,
OPT_PAPER_WHITE_NITS,
OPT_BCNONMULT4FIX,
OPT_SWIZZLE,
OPT_USE_XBOX,
OPT_XGMODE,
OPT_MAX
};
enum
{
ROTATE_709_TO_HDR10 = 1,
ROTATE_HDR10_TO_709,
ROTATE_709_TO_2020,
ROTATE_2020_TO_709,
ROTATE_P3D65_TO_HDR10,
ROTATE_P3D65_TO_2020,
ROTATE_709_TO_P3D65,
ROTATE_P3D65_TO_709,
};
static_assert(OPT_MAX <= 64, "dwOptions is a unsigned int bitfield");
struct SConversion
{
wchar_t szSrc[MAX_PATH];
wchar_t szFolder[MAX_PATH];
};
template<typename T>
struct SValue
{
const wchar_t* name;
T value;
};
const SValue<uint64_t> g_pOptions[] =
{
{ L"r", OPT_RECURSIVE },
{ L"flist", OPT_FILELIST },
{ L"w", OPT_WIDTH },
{ L"h", OPT_HEIGHT },
{ L"m", OPT_MIPLEVELS },
{ L"f", OPT_FORMAT },
{ L"if", OPT_FILTER },
{ L"srgbi", OPT_SRGBI },
{ L"srgbo", OPT_SRGBO },
{ L"srgb", OPT_SRGB },
{ L"px", OPT_PREFIX },
{ L"sx", OPT_SUFFIX },
{ L"o", OPT_OUTPUTDIR },
{ L"l", OPT_TOLOWER },
{ L"y", OPT_OVERWRITE },
{ L"ft", OPT_FILETYPE },
{ L"hflip", OPT_HFLIP },
{ L"vflip", OPT_VFLIP },
{ L"dword", OPT_DDS_DWORD_ALIGN },
{ L"badtails", OPT_DDS_BAD_DXTN_TAILS },
{ L"dx10", OPT_USE_DX10 },
{ L"dx9", OPT_USE_DX9 },
{ L"tga20", OPT_TGA20 },
{ L"wicq", OPT_WIC_QUALITY },
{ L"wiclossless", OPT_WIC_LOSSLESS },
{ L"wicmulti", OPT_WIC_MULTIFRAME },
{ L"nologo", OPT_NOLOGO },
{ L"timing", OPT_TIMING },
{ L"sepalpha", OPT_SEPALPHA },
{ L"keepcoverage", OPT_PRESERVE_ALPHA_COVERAGE },
{ L"nowic", OPT_NO_WIC },
{ L"tu", OPT_TYPELESS_UNORM },
{ L"tf", OPT_TYPELESS_FLOAT },
{ L"pmalpha", OPT_PREMUL_ALPHA },
{ L"alpha", OPT_DEMUL_ALPHA },
{ L"xlum", OPT_EXPAND_LUMINANCE },
{ L"wrap", OPT_TA_WRAP },
{ L"mirror", OPT_TA_MIRROR },
{ L"singleproc", OPT_FORCE_SINGLEPROC },
{ L"gpu", OPT_GPU },
{ L"nogpu", OPT_NOGPU },
{ L"fl", OPT_FEATURE_LEVEL },
{ L"pow2", OPT_FIT_POWEROF2 },
{ L"at", OPT_ALPHA_THRESHOLD },
{ L"aw", OPT_ALPHA_WEIGHT },
{ L"nmap", OPT_NORMAL_MAP },
{ L"nmapamp", OPT_NORMAL_MAP_AMPLITUDE },
{ L"bc", OPT_BC_COMPRESS },
{ L"c", OPT_COLORKEY },
{ L"tonemap", OPT_TONEMAP },
{ L"x2bias", OPT_X2_BIAS },
{ L"inverty", OPT_INVERT_Y },
{ L"reconstructz", OPT_RECONSTRUCT_Z },
{ L"rotatecolor", OPT_ROTATE_COLOR },
{ L"nits", OPT_PAPER_WHITE_NITS },
{ L"fixbc4x4", OPT_BCNONMULT4FIX },
{ L"swizzle", OPT_SWIZZLE },
{ L"xbox", OPT_USE_XBOX },
{ L"xgmode", OPT_XGMODE },
{ nullptr, 0 }
};
#define DEFFMT(fmt) { L## #fmt, DXGI_FORMAT_ ## fmt }
const SValue<uint32_t> g_pFormats[] =
{
// List does not include _TYPELESS or depth/stencil formats
DEFFMT(R32G32B32A32_FLOAT),
DEFFMT(R32G32B32A32_UINT),
DEFFMT(R32G32B32A32_SINT),
DEFFMT(R32G32B32_FLOAT),
DEFFMT(R32G32B32_UINT),
DEFFMT(R32G32B32_SINT),
DEFFMT(R16G16B16A16_FLOAT),
DEFFMT(R16G16B16A16_UNORM),
DEFFMT(R16G16B16A16_UINT),
DEFFMT(R16G16B16A16_SNORM),
DEFFMT(R16G16B16A16_SINT),
DEFFMT(R32G32_FLOAT),
DEFFMT(R32G32_UINT),
DEFFMT(R32G32_SINT),
DEFFMT(R10G10B10A2_UNORM),
DEFFMT(R10G10B10A2_UINT),
DEFFMT(R11G11B10_FLOAT),
DEFFMT(R8G8B8A8_UNORM),
DEFFMT(R8G8B8A8_UNORM_SRGB),
DEFFMT(R8G8B8A8_UINT),
DEFFMT(R8G8B8A8_SNORM),
DEFFMT(R8G8B8A8_SINT),
DEFFMT(R16G16_FLOAT),
DEFFMT(R16G16_UNORM),
DEFFMT(R16G16_UINT),
DEFFMT(R16G16_SNORM),
DEFFMT(R16G16_SINT),
DEFFMT(R32_FLOAT),
DEFFMT(R32_UINT),
DEFFMT(R32_SINT),
DEFFMT(R8G8_UNORM),
DEFFMT(R8G8_UINT),
DEFFMT(R8G8_SNORM),
DEFFMT(R8G8_SINT),
DEFFMT(R16_FLOAT),
DEFFMT(R16_UNORM),
DEFFMT(R16_UINT),
DEFFMT(R16_SNORM),
DEFFMT(R16_SINT),
DEFFMT(R8_UNORM),
DEFFMT(R8_UINT),
DEFFMT(R8_SNORM),
DEFFMT(R8_SINT),
DEFFMT(A8_UNORM),
DEFFMT(R9G9B9E5_SHAREDEXP),
DEFFMT(R8G8_B8G8_UNORM),
DEFFMT(G8R8_G8B8_UNORM),
DEFFMT(BC1_UNORM),
DEFFMT(BC1_UNORM_SRGB),
DEFFMT(BC2_UNORM),
DEFFMT(BC2_UNORM_SRGB),
DEFFMT(BC3_UNORM),
DEFFMT(BC3_UNORM_SRGB),
DEFFMT(BC4_UNORM),
DEFFMT(BC4_SNORM),
DEFFMT(BC5_UNORM),
DEFFMT(BC5_SNORM),
DEFFMT(B5G6R5_UNORM),
DEFFMT(B5G5R5A1_UNORM),
// DXGI 1.1 formats
DEFFMT(B8G8R8A8_UNORM),
DEFFMT(B8G8R8X8_UNORM),
DEFFMT(R10G10B10_XR_BIAS_A2_UNORM),
DEFFMT(B8G8R8A8_UNORM_SRGB),
DEFFMT(B8G8R8X8_UNORM_SRGB),
DEFFMT(BC6H_UF16),
DEFFMT(BC6H_SF16),
DEFFMT(BC7_UNORM),
DEFFMT(BC7_UNORM_SRGB),
// DXGI 1.2 formats
DEFFMT(AYUV),
DEFFMT(Y410),
DEFFMT(Y416),
DEFFMT(YUY2),
DEFFMT(Y210),
DEFFMT(Y216),
// No support for legacy paletted video formats (AI44, IA44, P8, A8P8)
DEFFMT(B4G4R4A4_UNORM),
// Xbox One extended formats
{ L"R10G10B10_7E3_A2_FLOAT", DXGI_FORMAT(116) },
{ L"R10G10B10_6E4_A2_FLOAT", DXGI_FORMAT(117) },
{ L"R10G10B10_SNORM_A2_UNORM", DXGI_FORMAT(189) },
{ L"R4G4_UNORM", DXGI_FORMAT(190) },
{ nullptr, DXGI_FORMAT_UNKNOWN }
};
const SValue<uint32_t> g_pFormatAliases[] =
{
{ L"DXT1", DXGI_FORMAT_BC1_UNORM },
{ L"DXT2", DXGI_FORMAT_BC2_UNORM },
{ L"DXT3", DXGI_FORMAT_BC2_UNORM },
{ L"DXT4", DXGI_FORMAT_BC3_UNORM },
{ L"DXT5", DXGI_FORMAT_BC3_UNORM },
{ L"RGBA", DXGI_FORMAT_R8G8B8A8_UNORM },
{ L"BGRA", DXGI_FORMAT_B8G8R8A8_UNORM },
{ L"BGR", DXGI_FORMAT_B8G8R8X8_UNORM },
{ L"FP16", DXGI_FORMAT_R16G16B16A16_FLOAT },
{ L"FP32", DXGI_FORMAT_R32G32B32A32_FLOAT },
{ L"BPTC", DXGI_FORMAT_BC7_UNORM },
{ L"BPTC_FLOAT", DXGI_FORMAT_BC6H_UF16 },
{ nullptr, DXGI_FORMAT_UNKNOWN }
};
const SValue<uint32_t> g_pReadOnlyFormats[] =
{
DEFFMT(R32G32B32A32_TYPELESS),
DEFFMT(R32G32B32_TYPELESS),
DEFFMT(R16G16B16A16_TYPELESS),
DEFFMT(R32G32_TYPELESS),
DEFFMT(R32G8X24_TYPELESS),
DEFFMT(D32_FLOAT_S8X24_UINT),
DEFFMT(R32_FLOAT_X8X24_TYPELESS),
DEFFMT(X32_TYPELESS_G8X24_UINT),
DEFFMT(R10G10B10A2_TYPELESS),
DEFFMT(R8G8B8A8_TYPELESS),
DEFFMT(R16G16_TYPELESS),
DEFFMT(R32_TYPELESS),
DEFFMT(D32_FLOAT),
DEFFMT(R24G8_TYPELESS),
DEFFMT(D24_UNORM_S8_UINT),
DEFFMT(R24_UNORM_X8_TYPELESS),
DEFFMT(X24_TYPELESS_G8_UINT),
DEFFMT(R8G8_TYPELESS),
DEFFMT(R16_TYPELESS),
DEFFMT(R8_TYPELESS),
DEFFMT(BC1_TYPELESS),
DEFFMT(BC2_TYPELESS),
DEFFMT(BC3_TYPELESS),
DEFFMT(BC4_TYPELESS),
DEFFMT(BC5_TYPELESS),
// DXGI 1.1 formats
DEFFMT(B8G8R8A8_TYPELESS),
DEFFMT(B8G8R8X8_TYPELESS),
DEFFMT(BC6H_TYPELESS),
DEFFMT(BC7_TYPELESS),
// DXGI 1.2 formats
DEFFMT(NV12),
DEFFMT(P010),
DEFFMT(P016),
DEFFMT(420_OPAQUE),
DEFFMT(NV11),
// DXGI 1.3 formats
{ L"P208", DXGI_FORMAT(130) },
{ L"V208", DXGI_FORMAT(131) },
{ L"V408", DXGI_FORMAT(132) },
// Xbox One extended formats
{ L"D16_UNORM_S8_UINT", DXGI_FORMAT(118) },
{ L"R16_UNORM_X8_TYPELESS", DXGI_FORMAT(119) },
{ L"X16_TYPELESS_G8_UINT", DXGI_FORMAT(120) },
{ nullptr, DXGI_FORMAT_UNKNOWN }
};
const SValue<uint32_t> g_pFilters[] =
{
{ L"POINT", TEX_FILTER_POINT },
{ L"LINEAR", TEX_FILTER_LINEAR },
{ L"CUBIC", TEX_FILTER_CUBIC },
{ L"FANT", TEX_FILTER_FANT },
{ L"BOX", TEX_FILTER_BOX },
{ L"TRIANGLE", TEX_FILTER_TRIANGLE },
{ L"POINT_DITHER", TEX_FILTER_POINT | TEX_FILTER_DITHER },
{ L"LINEAR_DITHER", TEX_FILTER_LINEAR | TEX_FILTER_DITHER },
{ L"CUBIC_DITHER", TEX_FILTER_CUBIC | TEX_FILTER_DITHER },
{ L"FANT_DITHER", TEX_FILTER_FANT | TEX_FILTER_DITHER },
{ L"BOX_DITHER", TEX_FILTER_BOX | TEX_FILTER_DITHER },
{ L"TRIANGLE_DITHER", TEX_FILTER_TRIANGLE | TEX_FILTER_DITHER },
{ L"POINT_DITHER_DIFFUSION", TEX_FILTER_POINT | TEX_FILTER_DITHER_DIFFUSION },
{ L"LINEAR_DITHER_DIFFUSION", TEX_FILTER_LINEAR | TEX_FILTER_DITHER_DIFFUSION },
{ L"CUBIC_DITHER_DIFFUSION", TEX_FILTER_CUBIC | TEX_FILTER_DITHER_DIFFUSION },
{ L"FANT_DITHER_DIFFUSION", TEX_FILTER_FANT | TEX_FILTER_DITHER_DIFFUSION },
{ L"BOX_DITHER_DIFFUSION", TEX_FILTER_BOX | TEX_FILTER_DITHER_DIFFUSION },
{ L"TRIANGLE_DITHER_DIFFUSION", TEX_FILTER_TRIANGLE | TEX_FILTER_DITHER_DIFFUSION },
{ nullptr, TEX_FILTER_DEFAULT }
};
const SValue<uint32_t> g_pRotateColor[] =
{
{ L"709to2020", ROTATE_709_TO_2020 },
{ L"2020to709", ROTATE_2020_TO_709 },
{ L"709toHDR10", ROTATE_709_TO_HDR10 },
{ L"HDR10to709", ROTATE_HDR10_TO_709 },
{ L"P3D65to2020", ROTATE_P3D65_TO_2020 },
{ L"P3D65toHDR10", ROTATE_P3D65_TO_HDR10 },
{ L"709toP3D65", ROTATE_709_TO_P3D65 },
{ L"P3D65to709", ROTATE_P3D65_TO_709 },
{ nullptr, 0 },
};
#define CODEC_DDS 0xFFFF0001
#define CODEC_TGA 0xFFFF0002
#define CODEC_HDP 0xFFFF0003
#define CODEC_JXR 0xFFFF0004
#define CODEC_HDR 0xFFFF0005
#define CODEC_PPM 0xFFFF0006
#define CODEC_PFM 0xFFFF0007
#ifdef USE_OPENEXR
#define CODEC_EXR 0xFFFF0008
#endif
const SValue<uint32_t> g_pSaveFileTypes[] = // valid formats to write to
{
{ L"bmp", WIC_CODEC_BMP },
{ L"jpg", WIC_CODEC_JPEG },
{ L"jpeg", WIC_CODEC_JPEG },
{ L"png", WIC_CODEC_PNG },
{ L"dds", CODEC_DDS },
{ L"tga", CODEC_TGA },
{ L"hdr", CODEC_HDR },
{ L"tif", WIC_CODEC_TIFF },
{ L"tiff", WIC_CODEC_TIFF },
{ L"wdp", WIC_CODEC_WMP },
{ L"hdp", CODEC_HDP },
{ L"jxr", CODEC_JXR },
{ L"ppm", CODEC_PPM },
{ L"pfm", CODEC_PFM },
#ifdef USE_OPENEXR
{ L"exr", CODEC_EXR },
#endif
{ nullptr, CODEC_DDS }
};
const SValue<uint32_t> g_pFeatureLevels[] = // valid feature levels for -fl for maximimum size
{
{ L"9.1", 2048 },
{ L"9.2", 2048 },
{ L"9.3", 4096 },
{ L"10.0", 8192 },
{ L"10.1", 8192 },
{ L"11.0", 16384 },
{ L"11.1", 16384 },
{ L"12.0", 16384 },
{ L"12.1", 16384 },
{ L"12.2", 16384 },
{ nullptr, 0 },
};
}
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
HRESULT __cdecl LoadFromBMPEx(
_In_z_ const wchar_t* szFile,
_In_ WIC_FLAGS flags,
_Out_opt_ TexMetadata* metadata,
_Out_ ScratchImage& image) noexcept;
HRESULT __cdecl LoadFromPortablePixMap(
_In_z_ const wchar_t* szFile,
_Out_opt_ TexMetadata* metadata,
_Out_ ScratchImage& image) noexcept;
HRESULT __cdecl SaveToPortablePixMap(
_In_ const Image& image,
_In_z_ const wchar_t* szFile) noexcept;
HRESULT __cdecl LoadFromPortablePixMapHDR(
_In_z_ const wchar_t* szFile,
_Out_opt_ TexMetadata* metadata,
_Out_ ScratchImage& image) noexcept;
HRESULT __cdecl SaveToPortablePixMapHDR(
_In_ const Image& image,
_In_z_ const wchar_t* szFile) noexcept;
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
#pragma warning( disable : 4616 6211 )
namespace
{
inline HANDLE safe_handle(HANDLE h) noexcept { return (h == INVALID_HANDLE_VALUE) ? nullptr : h; }
struct find_closer { void operator()(HANDLE h) noexcept { assert(h != INVALID_HANDLE_VALUE); if (h) FindClose(h); } };
using ScopedFindHandle = std::unique_ptr<void, find_closer>;
constexpr static bool ispow2(size_t x)
{
return ((x != 0) && !(x & (x - 1)));
}
#ifdef _PREFAST_
#pragma prefast(disable : 26018, "Only used with static internal arrays")
#endif
template<typename T>
T LookupByName(const wchar_t *pName, const SValue<T> *pArray)
{
while (pArray->name)
{
if (!_wcsicmp(pName, pArray->name))
return pArray->value;
pArray++;
}
return 0;
}
template<typename T>
const wchar_t* LookupByValue(T value, const SValue<T> *pArray)
{
while (pArray->name)
{
if (value == pArray->value)
return pArray->name;
pArray++;
}
return L"";
}
void SearchForFiles(const wchar_t* path, std::list<SConversion>& files, bool recursive, const wchar_t* folder)
{
// Process files
WIN32_FIND_DATAW findData = {};
ScopedFindHandle hFile(safe_handle(FindFirstFileExW(path,
FindExInfoBasic, &findData,
FindExSearchNameMatch, nullptr,
FIND_FIRST_EX_LARGE_FETCH)));
if (hFile)
{
for (;;)
{
if (!(findData.dwFileAttributes & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_DIRECTORY)))
{
wchar_t drive[_MAX_DRIVE] = {};
wchar_t dir[_MAX_DIR] = {};
_wsplitpath_s(path, drive, _MAX_DRIVE, dir, _MAX_DIR, nullptr, 0, nullptr, 0);
SConversion conv = {};
_wmakepath_s(conv.szSrc, drive, dir, findData.cFileName, nullptr);
if (folder)
{
wcscpy_s(conv.szFolder, folder);
}
files.push_back(conv);
}
if (!FindNextFileW(hFile.get(), &findData))
break;
}
}
// Process directories
if (recursive)
{
wchar_t searchDir[MAX_PATH] = {};
{
wchar_t drive[_MAX_DRIVE] = {};
wchar_t dir[_MAX_DIR] = {};
_wsplitpath_s(path, drive, _MAX_DRIVE, dir, _MAX_DIR, nullptr, 0, nullptr, 0);
_wmakepath_s(searchDir, drive, dir, L"*", nullptr);
}
hFile.reset(safe_handle(FindFirstFileExW(searchDir,
FindExInfoBasic, &findData,
FindExSearchLimitToDirectories, nullptr,
FIND_FIRST_EX_LARGE_FETCH)));
if (!hFile)
return;
for (;;)
{
if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if (findData.cFileName[0] != L'.')
{
wchar_t subdir[MAX_PATH] = {};
auto subfolder = (folder)
? (std::wstring(folder) + std::wstring(findData.cFileName) + L"\\")
: (std::wstring(findData.cFileName) + L"\\");
{
wchar_t drive[_MAX_DRIVE] = {};
wchar_t dir[_MAX_DIR] = {};
wchar_t fname[_MAX_FNAME] = {};
wchar_t ext[_MAX_FNAME] = {};
_wsplitpath_s(path, drive, dir, fname, ext);
wcscat_s(dir, findData.cFileName);
_wmakepath_s(subdir, drive, dir, fname, ext);
}
SearchForFiles(subdir, files, recursive, subfolder.c_str());
}
}
if (!FindNextFileW(hFile.get(), &findData))
break;
}
}
}
void ProcessFileList(std::wifstream& inFile, std::list<SConversion>& files)
{
std::list<SConversion> flist;
std::set<std::wstring> excludes;
wchar_t fname[1024] = {};
for (;;)
{
inFile >> fname;
if (!inFile)
break;
if (*fname == L'#')
{
// Comment
}
else if (*fname == L'-')
{
if (flist.empty())
{
wprintf(L"WARNING: Ignoring the line '%ls' in -flist\n", fname);
}
else
{
if (wcspbrk(fname, L"?*") != nullptr)
{
std::list<SConversion> removeFiles;
SearchForFiles(&fname[1], removeFiles, false, nullptr);
for (auto it : removeFiles)
{
_wcslwr_s(it.szSrc);
excludes.insert(it.szSrc);
}
}
else
{
std::wstring name = (fname + 1);
std::transform(name.begin(), name.end(), name.begin(), towlower);
excludes.insert(name);
}
}
}
else if (wcspbrk(fname, L"?*") != nullptr)
{
SearchForFiles(fname, flist, false, nullptr);
}
else
{
SConversion conv = {};
wcscpy_s(conv.szSrc, MAX_PATH, fname);
flist.push_back(conv);
}
inFile.ignore(1000, '\n');
}
inFile.close();
if (!excludes.empty())
{
// Remove any excluded files
for (auto it = flist.begin(); it != flist.end();)
{
std::wstring name = it->szSrc;
std::transform(name.begin(), name.end(), name.begin(), towlower);
auto item = it;
++it;
if (excludes.find(name) != excludes.end())
{
flist.erase(item);
}
}
}
if (flist.empty())
{
wprintf(L"WARNING: No file names found in -flist\n");
}
else
{
files.splice(files.end(), flist);
}
}
void PrintFormat(DXGI_FORMAT Format)
{
for (auto pFormat = g_pFormats; pFormat->name; pFormat++)
{
if (static_cast<DXGI_FORMAT>(pFormat->value) == Format)
{
wprintf(L"%ls", pFormat->name);
return;
}
}
for (auto pFormat = g_pReadOnlyFormats; pFormat->name; pFormat++)
{
if (static_cast<DXGI_FORMAT>(pFormat->value) == Format)
{
wprintf(L"%ls", pFormat->name);
return;
}
}
wprintf(L"*UNKNOWN*");
}
void PrintInfo(const TexMetadata& info, bool isXbox)
{
wprintf(L" (%zux%zu", info.width, info.height);
if (TEX_DIMENSION_TEXTURE3D == info.dimension)
wprintf(L"x%zu", info.depth);
if (info.mipLevels > 1)
wprintf(L",%zu", info.mipLevels);
if (info.arraySize > 1)
wprintf(L",%zu", info.arraySize);
wprintf(L" ");
PrintFormat(info.format);
switch (info.dimension)
{
case TEX_DIMENSION_TEXTURE1D:
wprintf(L"%ls", (info.arraySize > 1) ? L" 1DArray" : L" 1D");
break;
case TEX_DIMENSION_TEXTURE2D:
if (info.IsCubemap())
{
wprintf(L"%ls", (info.arraySize > 6) ? L" CubeArray" : L" Cube");
}
else
{
wprintf(L"%ls", (info.arraySize > 1) ? L" 2DArray" : L" 2D");
}
break;
case TEX_DIMENSION_TEXTURE3D:
wprintf(L" 3D");
break;
}
switch (info.GetAlphaMode())
{
case TEX_ALPHA_MODE_OPAQUE:
wprintf(L" \x03B1:Opaque");
break;
case TEX_ALPHA_MODE_PREMULTIPLIED:
wprintf(L" \x03B1:PM");
break;
case TEX_ALPHA_MODE_STRAIGHT:
wprintf(L" \x03B1:NonPM");
break;
case TEX_ALPHA_MODE_CUSTOM:
wprintf(L" \x03B1:Custom");
break;
case TEX_ALPHA_MODE_UNKNOWN:
break;
}
if (isXbox)
{
wprintf(L" Xbox");
}
wprintf(L")");
}
void PrintList(size_t cch, const SValue<uint32_t> *pValue)
{
while (pValue->name)
{
const size_t cchName = wcslen(pValue->name);
if (cch + cchName + 2 >= 80)
{
wprintf(L"\n ");
cch = 6;
}
wprintf(L"%ls ", pValue->name);
cch += cchName + 2;
pValue++;
}
wprintf(L"\n");
}
void PrintLogo()
{
wprintf(L"Microsoft (R) DirectX Texture Converter for Xbox One\n");
wprintf(L"Copyright (C) Microsoft Corp. All rights reserved.\n");
#ifdef _DEBUG
wprintf(L"*** Debug build ***\n");
#endif
wprintf(L"\n");
}
_Success_(return)
bool GetDXGIFactory(_Outptr_ IDXGIFactory1** pFactory)
{
if (!pFactory)
return false;
*pFactory = nullptr;
typedef HRESULT(WINAPI* pfn_CreateDXGIFactory1)(REFIID riid, _Out_ void **ppFactory);
static pfn_CreateDXGIFactory1 s_CreateDXGIFactory1 = nullptr;
if (!s_CreateDXGIFactory1)
{
HMODULE hModDXGI = LoadLibraryW(L"dxgi.dll");
if (!hModDXGI)
return false;
s_CreateDXGIFactory1 = reinterpret_cast<pfn_CreateDXGIFactory1>(reinterpret_cast<void*>(GetProcAddress(hModDXGI, "CreateDXGIFactory1")));
if (!s_CreateDXGIFactory1)
return false;
}
return SUCCEEDED(s_CreateDXGIFactory1(IID_PPV_ARGS(pFactory)));
}
void PrintUsage()
{
PrintLogo();
static const wchar_t* const s_usage =
L"Usage: texconv <options> <files>\n"
L"\n"
L" -r wildcard filename search is recursive\n"
L" -r:flatten flatten the directory structure (default)\n"
L" -r:keep keep the directory structure\n"
L" -flist <filename> use text file with a list of input files (one per line)\n"
L"\n"
L" -w <n> width\n"
L" -h <n> height\n"
L" -m <n> miplevels\n"
L" -f <format> format\n"
L"\n"
L" -if <filter> image filtering\n"
L" -srgb{i|o} sRGB {input, output}\n"
L"\n"
L" -px <string> name prefix\n"
L" -sx <string> name suffix\n"
L" -o <directory> output directory\n"
L" -l force output filename to lower case\n"
L" -y overwrite existing output file (if any)\n"
L" -ft <filetype> output file type\n"
L"\n"
L" -hflip horizonal flip of source image\n"
L" -vflip vertical flip of source image\n"
L"\n"
L" -sepalpha resize/generate mips alpha channel separately\n"
L" from color channels\n"
L" -keepcoverage <ref> Preserve alpha coverage in mips for alpha test ref\n"
L"\n"
L" -nowic Force non-WIC filtering\n"
L" -wrap, -mirror texture addressing mode (wrap, mirror, or clamp)\n"
L" -pmalpha convert final texture to use premultiplied alpha\n"
L" -alpha convert premultiplied alpha to straight alpha\n"
L" -at <threshold> Alpha threshold used for BC1, RGBA5551, and WIC\n"
L" (defaults to 0.5)\n"
L"\n"
L" -fl <feature-level> Set maximum feature level target (defaults to 11.0)\n"
L" -pow2 resize to fit a power-of-2, respecting aspect ratio\n"
L"\n"
L" -nmap <options> converts height-map to normal-map\n"
L" options must be one or more of\n"
L" r, g, b, a, l, m, u, v, i, o\n"
L" -nmapamp <weight> normal map amplitude (defaults to 1.0)\n"
L"\n"
L" (DDS input only)\n"
L" -t{u|f} TYPELESS format is treated as UNORM or FLOAT\n"
L" -dword Use DWORD instead of BYTE alignment\n"
L" -badtails Fix for older DXTn with bad mipchain tails\n"
L" -fixbc4x4 Fix for odd-sized BC files that Direct3D can't load\n"
L" -xlum expand legacy L8, L16, and A8P8 formats\n"
L"\n"
L" (DDS output only)\n"
L" -dx10 Force use of 'DX10' extended header\n"
L" -dx9 Force use of legacy DX9 header\n"
L" -xbox Tile and use 'XBOX' variant of DDS\n"
L" -xgmode <mode> Tile using provided memory layout mode\n"
L"\n"
L" (TGA output only)\n"
L" -tga20 Write file including TGA 2.0 extension area\n"
L"\n"
L" (BMP, PNG, JPG, TIF, WDP output only)\n"
L" -wicq <quality> When writing images with WIC use quality (0.0 to 1.0)\n"
L" -wiclossless When writing images with WIC use lossless mode\n"
L" -wicmulti When writing images with WIC encode multiframe images\n"
L"\n"
L" -nologo suppress copyright message\n"
L" -timing Display elapsed processing time\n"
L"\n"
#ifdef _OPENMP
L" -singleproc Do not use multi-threaded compression\n"
#endif
L" -gpu <adapter> Select GPU for DirectCompute-based codecs (0 is default)\n"
L" -nogpu Do not use DirectCompute-based codecs\n"
L"\n"
L" -bc <options> Sets options for BC compression\n"
L" options must be one or more of\n"
L" d, u, q, x\n"
L" -aw <weight> BC7 GPU compressor weighting for alpha error metric\n"
L" (defaults to 1.0)\n"
L"\n"
L" -c <hex-RGB> colorkey (a.k.a. chromakey) transparency\n"
L" -rotatecolor <rot> rotates color primaries and/or applies a curve\n"
L" -nits <value> paper-white value in nits to use for HDR10 (def: 200.0)\n"
L" -tonemap Apply a tonemap operator based on maximum luminance\n"
L" -x2bias Enable *2 - 1 conversion cases for unorm/pos-only-float\n"
L" -inverty Invert Y (i.e. green) channel values\n"
L" -reconstructz Rebuild Z (blue) channel assuming X/Y are normals\n"
L" -swizzle <rgba> Swizzle image channels using HLSL-style mask\n";
wprintf(L"%ls", s_usage);
wprintf(L"\n <format>: ");
PrintList(13, g_pFormats);
wprintf(L" ");
PrintList(13, g_pFormatAliases);
wprintf(L"\n <filter>: ");
PrintList(13, g_pFilters);
wprintf(L"\n <rot>: ");
PrintList(13, g_pRotateColor);
wprintf(L"\n <filetype>: ");
PrintList(15, g_pSaveFileTypes);
wprintf(L"\n <feature-level>: ");
PrintList(13, g_pFeatureLevels);
ComPtr<IDXGIFactory1> dxgiFactory;
if (GetDXGIFactory(dxgiFactory.GetAddressOf()))
{
wprintf(L"\n <adapter>:\n");
ComPtr<IDXGIAdapter> adapter;
for (UINT adapterIndex = 0;
SUCCEEDED(dxgiFactory->EnumAdapters(adapterIndex, adapter.ReleaseAndGetAddressOf()));
++adapterIndex)
{
DXGI_ADAPTER_DESC desc;
if (SUCCEEDED(adapter->GetDesc(&desc)))
{
wprintf(L" %u: VID:%04X, PID:%04X - %ls\n", adapterIndex, desc.VendorId, desc.DeviceId, desc.Description);
}
}