git.s-ol.nu ~forks/DiligentTools / a294546 TextureLoader / src / DDSLoader.cpp
a294546

Tree @a294546 (Download .tar.gz)

DDSLoader.cpp @a294546raw · history · blame

   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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
/*     Copyright 2015-2018 Egor Yusov
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PROPRIETARY RIGHTS.
 *
 *  In no event and under no legal theory, whether in tort (including negligence), 
 *  contract, or otherwise, unless required by applicable law (such as deliberate 
 *  and grossly negligent acts) or agreed to in writing, shall any Contributor be
 *  liable for any damages, including any direct, indirect, special, incidental, 
 *  or consequential damages of any character arising as a result of this License or 
 *  out of the use or inability to use the software (including but not limited to damages 
 *  for loss of goodwill, work stoppage, computer failure or malfunction, or any and 
 *  all other commercial damages or losses), even if such Contributor has been advised 
 *  of the possibility of such damages.
 */

//--------------------------------------------------------------------------------------
// File: DDSLoader.cpp
//
// Functions for loading a DDS texture and creating a Direct3D 11 runtime resource for it
//
// Note these functions are useful as a light-weight runtime loader for DDS files. For
// a full-featured DDS file reader, writer, and texture processing pipeline see
// the 'Texconv' sample and the 'DirectXTex' library.
//
// 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
// http://go.microsoft.com/fwlink/?LinkId=248929
//--------------------------------------------------------------------------------------

#include "pch.h"
#include "dxgiformat.h"
#include <memory>
#include <algorithm>
#include "DDSLoader.h"

#ifndef _In_
#   define _In_
#endif

#ifndef _Out_
#   define _Out_
#endif

#ifndef _Out_opt_
#   define _Out_opt_
#endif

#ifndef _Outptr_opt_
#   define _Outptr_opt_
#endif

using namespace Diligent;
using namespace Diligent;

// D3D11 definitions

enum D3D11_RESOURCE_DIMENSION
{
    D3D11_RESOURCE_DIMENSION_UNKNOWN	= 0,
    D3D11_RESOURCE_DIMENSION_BUFFER	    = 1,
    D3D11_RESOURCE_DIMENSION_TEXTURE1D	= 2,
    D3D11_RESOURCE_DIMENSION_TEXTURE2D	= 3,
    D3D11_RESOURCE_DIMENSION_TEXTURE3D	= 4
};

enum D3D11_RESOURCE_MISC_FLAG
{
    D3D11_RESOURCE_MISC_TEXTURECUBE	= 0x4L,
};

#ifndef D3D11_REQ_MIP_LEVELS
#   define	D3D11_REQ_MIP_LEVELS	( 15 )
#endif

#ifndef D3D11_REQ_TEXTURE1D_ARRAY_AXIS_DIMENSION
#   define	D3D11_REQ_TEXTURE1D_ARRAY_AXIS_DIMENSION	( 2048 )
#endif

#ifndef D3D11_REQ_TEXTURE1D_U_DIMENSION
#   define	D3D11_REQ_TEXTURE1D_U_DIMENSION	( 16384 )
#endif

#ifndef D3D11_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION
#   define	D3D11_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION	( 2048 )
#endif

#ifndef D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION
#   define	D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION	( 16384 )
#endif

#ifndef D3D11_REQ_TEXTURE3D_U_V_OR_W_DIMENSION
#   define	D3D11_REQ_TEXTURE3D_U_V_OR_W_DIMENSION	( 2048 )
#endif

#ifndef D3D11_REQ_TEXTURECUBE_DIMENSION
#   define	D3D11_REQ_TEXTURECUBE_DIMENSION	( 16384 )
#endif


//--------------------------------------------------------------------------------------
// Macros
//--------------------------------------------------------------------------------------
#ifndef MAKEFOURCC
    #define MAKEFOURCC(ch0, ch1, ch2, ch3)                                      \
                ((Uint32)(Uint8)(ch0)        | ((Uint32)(Uint8)(ch1) << 8) |    \
                ((Uint32)(Uint8)(ch2) << 16) | ((Uint32)(Uint8)(ch3) << 24))
#endif /* defined(MAKEFOURCC) */

//--------------------------------------------------------------------------------------
// DDS file structure definitions
//
// See DDS.h in the 'Texconv' sample and the 'DirectXTex' library
//--------------------------------------------------------------------------------------
#pragma pack(push, 1)

#define DDS_MAGIC 0x20534444 // "DDS "

struct DDS_PIXELFORMAT
{
    Uint32  size;
    Uint32  flags;
    Uint32  fourCC;
    Uint32  RGBBitCount;
    Uint32  RBitMask;
    Uint32  GBitMask;
    Uint32  BBitMask;
    Uint32  ABitMask;
};

#define DDS_FOURCC      0x00000004  // DDPF_FOURCC
#define DDS_RGB         0x00000040  // DDPF_RGB
#define DDS_RGBA        0x00000041  // DDPF_RGB | DDPF_ALPHAPIXELS
#define DDS_LUMINANCE   0x00020000  // DDPF_LUMINANCE
#define DDS_LUMINANCEA  0x00020001  // DDPF_LUMINANCE | DDPF_ALPHAPIXELS
#define DDS_ALPHA       0x00000002  // DDPF_ALPHA
#define DDS_PAL8        0x00000020  // DDPF_PALETTEINDEXED8

#define DDS_HEADER_FLAGS_TEXTURE        0x00001007  // DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT
#define DDS_HEADER_FLAGS_MIPMAP         0x00020000  // DDSD_MIPMAPCOUNT
#define DDS_HEADER_FLAGS_VOLUME         0x00800000  // DDSD_DEPTH
#define DDS_HEADER_FLAGS_PITCH          0x00000008  // DDSD_PITCH
#define DDS_HEADER_FLAGS_LINEARSIZE     0x00080000  // DDSD_LINEARSIZE

#define DDS_HEIGHT 0x00000002 // DDSD_HEIGHT
#define DDS_WIDTH  0x00000004 // DDSD_WIDTH

#define DDS_SURFACE_FLAGS_TEXTURE 0x00001000 // DDSCAPS_TEXTURE
#define DDS_SURFACE_FLAGS_MIPMAP  0x00400008 // DDSCAPS_COMPLEX | DDSCAPS_MIPMAP
#define DDS_SURFACE_FLAGS_CUBEMAP 0x00000008 // DDSCAPS_COMPLEX

#define DDS_CUBEMAP_POSITIVEX 0x00000600 // DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_POSITIVEX
#define DDS_CUBEMAP_NEGATIVEX 0x00000a00 // DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_NEGATIVEX
#define DDS_CUBEMAP_POSITIVEY 0x00001200 // DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_POSITIVEY
#define DDS_CUBEMAP_NEGATIVEY 0x00002200 // DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_NEGATIVEY
#define DDS_CUBEMAP_POSITIVEZ 0x00004200 // DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_POSITIVEZ
#define DDS_CUBEMAP_NEGATIVEZ 0x00008200 // DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_NEGATIVEZ

#define DDS_CUBEMAP_ALLFACES (DDS_CUBEMAP_POSITIVEX | DDS_CUBEMAP_NEGATIVEX |\
                              DDS_CUBEMAP_POSITIVEY | DDS_CUBEMAP_NEGATIVEY |\
                              DDS_CUBEMAP_POSITIVEZ | DDS_CUBEMAP_NEGATIVEZ)

#define DDS_CUBEMAP 0x00000200 // DDSCAPS2_CUBEMAP

#define DDS_FLAGS_VOLUME 0x00200000 // DDSCAPS2_VOLUME

enum DDS_MISC_FLAGS2
{
    DDS_MISC_FLAGS2_ALPHA_MODE_MASK = 0x7L,
};

enum DDS_ALPHA_MODE
{
    DDS_ALPHA_MODE_UNKNOWN       = 0,
    DDS_ALPHA_MODE_STRAIGHT      = 1,
    DDS_ALPHA_MODE_PREMULTIPLIED = 2,
    DDS_ALPHA_MODE_OPAQUE        = 3,
    DDS_ALPHA_MODE_CUSTOM        = 4,
};

typedef struct
{
    Uint32          size;
    Uint32          flags;
    Uint32          height;
    Uint32          width;
    Uint32          pitchOrLinearSize;
    Uint32          depth; // only if DDS_HEADER_FLAGS_VOLUME is set in flags
    Uint32          mipMapCount;
    Uint32          reserved1[11];
    DDS_PIXELFORMAT ddspf;
    Uint32          caps;
    Uint32          caps2;
    Uint32          caps3;
    Uint32          caps4;
    Uint32          reserved2;
} DDS_HEADER;


typedef struct
{
    DXGI_FORMAT dxgiFormat;
    Uint32      resourceDimension;
    Uint32      miscFlag; // see D3D11_RESOURCE_MISC_FLAG
    Uint32      arraySize;
    Uint32      miscFlags2;
} DDS_HEADER_DXT10;

#pragma pack(pop)



//--------------------------------------------------------------------------------------
// Return the BPP for a particular format
//--------------------------------------------------------------------------------------
static size_t BitsPerPixel(_In_ DXGI_FORMAT fmt)
{
    switch (fmt)
    {
    case DXGI_FORMAT_R32G32B32A32_TYPELESS:
    case DXGI_FORMAT_R32G32B32A32_FLOAT:
    case DXGI_FORMAT_R32G32B32A32_UINT:
    case DXGI_FORMAT_R32G32B32A32_SINT:
        return 128;

    case DXGI_FORMAT_R32G32B32_TYPELESS:
    case DXGI_FORMAT_R32G32B32_FLOAT:
    case DXGI_FORMAT_R32G32B32_UINT:
    case DXGI_FORMAT_R32G32B32_SINT:
        return 96;

    case DXGI_FORMAT_R16G16B16A16_TYPELESS:
    case DXGI_FORMAT_R16G16B16A16_FLOAT:
    case DXGI_FORMAT_R16G16B16A16_UNORM:
    case DXGI_FORMAT_R16G16B16A16_UINT:
    case DXGI_FORMAT_R16G16B16A16_SNORM:
    case DXGI_FORMAT_R16G16B16A16_SINT:
    case DXGI_FORMAT_R32G32_TYPELESS:
    case DXGI_FORMAT_R32G32_FLOAT:
    case DXGI_FORMAT_R32G32_UINT:
    case DXGI_FORMAT_R32G32_SINT:
    case DXGI_FORMAT_R32G8X24_TYPELESS:
    case DXGI_FORMAT_D32_FLOAT_S8X24_UINT:
    case DXGI_FORMAT_R32_FLOAT_X8X24_TYPELESS:
    case DXGI_FORMAT_X32_TYPELESS_G8X24_UINT:
        return 64;

    case DXGI_FORMAT_R10G10B10A2_TYPELESS:
    case DXGI_FORMAT_R10G10B10A2_UNORM:
    case DXGI_FORMAT_R10G10B10A2_UINT:
    case DXGI_FORMAT_R11G11B10_FLOAT:
    case DXGI_FORMAT_R8G8B8A8_TYPELESS:
    case DXGI_FORMAT_R8G8B8A8_UNORM:
    case DXGI_FORMAT_R8G8B8A8_UNORM_SRGB:
    case DXGI_FORMAT_R8G8B8A8_UINT:
    case DXGI_FORMAT_R8G8B8A8_SNORM:
    case DXGI_FORMAT_R8G8B8A8_SINT:
    case DXGI_FORMAT_R16G16_TYPELESS:
    case DXGI_FORMAT_R16G16_FLOAT:
    case DXGI_FORMAT_R16G16_UNORM:
    case DXGI_FORMAT_R16G16_UINT:
    case DXGI_FORMAT_R16G16_SNORM:
    case DXGI_FORMAT_R16G16_SINT:
    case DXGI_FORMAT_R32_TYPELESS:
    case DXGI_FORMAT_D32_FLOAT:
    case DXGI_FORMAT_R32_FLOAT:
    case DXGI_FORMAT_R32_UINT:
    case DXGI_FORMAT_R32_SINT:
    case DXGI_FORMAT_R24G8_TYPELESS:
    case DXGI_FORMAT_D24_UNORM_S8_UINT:
    case DXGI_FORMAT_R24_UNORM_X8_TYPELESS:
    case DXGI_FORMAT_X24_TYPELESS_G8_UINT:
    case DXGI_FORMAT_R9G9B9E5_SHAREDEXP:
    case DXGI_FORMAT_R8G8_B8G8_UNORM:
    case DXGI_FORMAT_G8R8_G8B8_UNORM:
    case DXGI_FORMAT_B8G8R8A8_UNORM:
    case DXGI_FORMAT_B8G8R8X8_UNORM:
    case DXGI_FORMAT_R10G10B10_XR_BIAS_A2_UNORM:
    case DXGI_FORMAT_B8G8R8A8_TYPELESS:
    case DXGI_FORMAT_B8G8R8A8_UNORM_SRGB:
    case DXGI_FORMAT_B8G8R8X8_TYPELESS:
    case DXGI_FORMAT_B8G8R8X8_UNORM_SRGB:
        return 32;

    case DXGI_FORMAT_R8G8_TYPELESS:
    case DXGI_FORMAT_R8G8_UNORM:
    case DXGI_FORMAT_R8G8_UINT:
    case DXGI_FORMAT_R8G8_SNORM:
    case DXGI_FORMAT_R8G8_SINT:
    case DXGI_FORMAT_R16_TYPELESS:
    case DXGI_FORMAT_R16_FLOAT:
    case DXGI_FORMAT_D16_UNORM:
    case DXGI_FORMAT_R16_UNORM:
    case DXGI_FORMAT_R16_UINT:
    case DXGI_FORMAT_R16_SNORM:
    case DXGI_FORMAT_R16_SINT:
    case DXGI_FORMAT_B5G6R5_UNORM:
    case DXGI_FORMAT_B5G5R5A1_UNORM:
    case DXGI_FORMAT_B4G4R4A4_UNORM:
        return 16;

    case DXGI_FORMAT_R8_TYPELESS:
    case DXGI_FORMAT_R8_UNORM:
    case DXGI_FORMAT_R8_UINT:
    case DXGI_FORMAT_R8_SNORM:
    case DXGI_FORMAT_R8_SINT:
    case DXGI_FORMAT_A8_UNORM:
        return 8;

    case DXGI_FORMAT_R1_UNORM:
        return 1;

    case DXGI_FORMAT_BC1_TYPELESS:
    case DXGI_FORMAT_BC1_UNORM:
    case DXGI_FORMAT_BC1_UNORM_SRGB:
    case DXGI_FORMAT_BC4_TYPELESS:
    case DXGI_FORMAT_BC4_UNORM:
    case DXGI_FORMAT_BC4_SNORM:
        return 4;

    case DXGI_FORMAT_BC2_TYPELESS:
    case DXGI_FORMAT_BC2_UNORM:
    case DXGI_FORMAT_BC2_UNORM_SRGB:
    case DXGI_FORMAT_BC3_TYPELESS:
    case DXGI_FORMAT_BC3_UNORM:
    case DXGI_FORMAT_BC3_UNORM_SRGB:
    case DXGI_FORMAT_BC5_TYPELESS:
    case DXGI_FORMAT_BC5_UNORM:
    case DXGI_FORMAT_BC5_SNORM:
    case DXGI_FORMAT_BC6H_TYPELESS:
    case DXGI_FORMAT_BC6H_UF16:
    case DXGI_FORMAT_BC6H_SF16:
    case DXGI_FORMAT_BC7_TYPELESS:
    case DXGI_FORMAT_BC7_UNORM:
    case DXGI_FORMAT_BC7_UNORM_SRGB:
        return 8;

    default:
        return 0;
    }
}


static TEXTURE_FORMAT DXGIFormatToTexFormat( DXGI_FORMAT TexFormat )
{
    switch(TexFormat)
    {
        case DXGI_FORMAT_UNKNOWN:                       return TEX_FORMAT_UNKNOWN;

        case DXGI_FORMAT_R32G32B32A32_TYPELESS:         return TEX_FORMAT_RGBA32_TYPELESS; 
        case DXGI_FORMAT_R32G32B32A32_FLOAT:            return TEX_FORMAT_RGBA32_FLOAT; 
        case DXGI_FORMAT_R32G32B32A32_UINT:             return TEX_FORMAT_RGBA32_UINT; 
        case DXGI_FORMAT_R32G32B32A32_SINT:             return TEX_FORMAT_RGBA32_SINT; 

        case DXGI_FORMAT_R32G32B32_TYPELESS:            return TEX_FORMAT_RGB32_TYPELESS; 
        case DXGI_FORMAT_R32G32B32_FLOAT:               return TEX_FORMAT_RGB32_FLOAT; 
        case DXGI_FORMAT_R32G32B32_UINT:                return TEX_FORMAT_RGB32_UINT; 
        case DXGI_FORMAT_R32G32B32_SINT:                return TEX_FORMAT_RGB32_SINT; 

        case DXGI_FORMAT_R16G16B16A16_TYPELESS:         return TEX_FORMAT_RGBA16_TYPELESS;
        case DXGI_FORMAT_R16G16B16A16_FLOAT:            return TEX_FORMAT_RGBA16_FLOAT;
        case DXGI_FORMAT_R16G16B16A16_UNORM:            return TEX_FORMAT_RGBA16_UNORM;
        case DXGI_FORMAT_R16G16B16A16_UINT:             return TEX_FORMAT_RGBA16_UINT;
        case DXGI_FORMAT_R16G16B16A16_SNORM:            return TEX_FORMAT_RGBA16_SNORM;
        case DXGI_FORMAT_R16G16B16A16_SINT:             return TEX_FORMAT_RGBA16_SINT;

        case DXGI_FORMAT_R32G32_TYPELESS:               return TEX_FORMAT_RG32_TYPELESS; 
        case DXGI_FORMAT_R32G32_FLOAT:                  return TEX_FORMAT_RG32_FLOAT; 
        case DXGI_FORMAT_R32G32_UINT:                   return TEX_FORMAT_RG32_UINT; 
        case DXGI_FORMAT_R32G32_SINT:                   return TEX_FORMAT_RG32_SINT; 

        case DXGI_FORMAT_R32G8X24_TYPELESS:             return TEX_FORMAT_R32G8X24_TYPELESS; 
        case DXGI_FORMAT_D32_FLOAT_S8X24_UINT:          return TEX_FORMAT_D32_FLOAT_S8X24_UINT; 
        case  DXGI_FORMAT_R32_FLOAT_X8X24_TYPELESS:     return TEX_FORMAT_R32_FLOAT_X8X24_TYPELESS;
        case DXGI_FORMAT_X32_TYPELESS_G8X24_UINT:       return TEX_FORMAT_X32_TYPELESS_G8X24_UINT; 

        case DXGI_FORMAT_R10G10B10A2_TYPELESS:          return TEX_FORMAT_RGB10A2_TYPELESS; 
        case DXGI_FORMAT_R10G10B10A2_UNORM:             return TEX_FORMAT_RGB10A2_UNORM; 
        case DXGI_FORMAT_R10G10B10A2_UINT:              return TEX_FORMAT_RGB10A2_UINT; 

        case DXGI_FORMAT_R11G11B10_FLOAT:               return TEX_FORMAT_R11G11B10_FLOAT; 

        case DXGI_FORMAT_R8G8B8A8_TYPELESS:             return TEX_FORMAT_RGBA8_TYPELESS; 
        case DXGI_FORMAT_R8G8B8A8_UNORM:                return TEX_FORMAT_RGBA8_UNORM; 
        case DXGI_FORMAT_R8G8B8A8_UNORM_SRGB:           return TEX_FORMAT_RGBA8_UNORM_SRGB; 
        case DXGI_FORMAT_R8G8B8A8_UINT:                 return TEX_FORMAT_RGBA8_UINT; 
        case DXGI_FORMAT_R8G8B8A8_SNORM:                return TEX_FORMAT_RGBA8_SNORM; 
        case DXGI_FORMAT_R8G8B8A8_SINT:                 return TEX_FORMAT_RGBA8_SINT; 

        case DXGI_FORMAT_R16G16_TYPELESS:               return TEX_FORMAT_RG16_TYPELESS; 
        case DXGI_FORMAT_R16G16_FLOAT:                  return TEX_FORMAT_RG16_FLOAT; 
        case DXGI_FORMAT_R16G16_UNORM:                  return TEX_FORMAT_RG16_UNORM; 
        case DXGI_FORMAT_R16G16_UINT:                   return TEX_FORMAT_RG16_UINT; 
        case DXGI_FORMAT_R16G16_SNORM:                  return TEX_FORMAT_RG16_SNORM; 
        case DXGI_FORMAT_R16G16_SINT:                   return TEX_FORMAT_RG16_SINT; 

        case DXGI_FORMAT_R32_TYPELESS:                  return TEX_FORMAT_R32_TYPELESS; 
        case DXGI_FORMAT_D32_FLOAT:                     return TEX_FORMAT_D32_FLOAT; 
        case DXGI_FORMAT_R32_FLOAT:                     return TEX_FORMAT_R32_FLOAT; 
        case DXGI_FORMAT_R32_UINT:                      return TEX_FORMAT_R32_UINT; 
        case DXGI_FORMAT_R32_SINT:                      return TEX_FORMAT_R32_SINT; 

        case DXGI_FORMAT_R24G8_TYPELESS:                return TEX_FORMAT_R24G8_TYPELESS; 
        case DXGI_FORMAT_D24_UNORM_S8_UINT:             return TEX_FORMAT_D24_UNORM_S8_UINT; 
        case DXGI_FORMAT_R24_UNORM_X8_TYPELESS:         return TEX_FORMAT_R24_UNORM_X8_TYPELESS; 
        case DXGI_FORMAT_X24_TYPELESS_G8_UINT:          return TEX_FORMAT_X24_TYPELESS_G8_UINT; 

        case DXGI_FORMAT_R8G8_TYPELESS:                 return TEX_FORMAT_RG8_TYPELESS; 
        case DXGI_FORMAT_R8G8_UNORM:                    return TEX_FORMAT_RG8_UNORM; 
        case DXGI_FORMAT_R8G8_UINT:                     return TEX_FORMAT_RG8_UINT; 
        case DXGI_FORMAT_R8G8_SNORM:                    return TEX_FORMAT_RG8_SNORM; 
        case DXGI_FORMAT_R8G8_SINT:                     return TEX_FORMAT_RG8_SINT; 

        case DXGI_FORMAT_R16_TYPELESS:                  return TEX_FORMAT_R16_TYPELESS; 
        case DXGI_FORMAT_R16_FLOAT:                     return TEX_FORMAT_R16_FLOAT; 
        case DXGI_FORMAT_D16_UNORM:                     return TEX_FORMAT_D16_UNORM; 
        case DXGI_FORMAT_R16_UNORM:                     return TEX_FORMAT_R16_UNORM; 
        case DXGI_FORMAT_R16_UINT:                      return TEX_FORMAT_R16_UINT; 
        case DXGI_FORMAT_R16_SNORM:                     return TEX_FORMAT_R16_SNORM; 
        case DXGI_FORMAT_R16_SINT:                      return TEX_FORMAT_R16_SINT; 

        case DXGI_FORMAT_R8_TYPELESS:                   return TEX_FORMAT_R8_TYPELESS; 
        case DXGI_FORMAT_R8_UNORM:                      return TEX_FORMAT_R8_UNORM; 
        case DXGI_FORMAT_R8_UINT:                       return TEX_FORMAT_R8_UINT; 
        case DXGI_FORMAT_R8_SNORM:                      return TEX_FORMAT_R8_SNORM; 
        case DXGI_FORMAT_R8_SINT:                       return TEX_FORMAT_R8_SINT; 
        case DXGI_FORMAT_A8_UNORM:                      return TEX_FORMAT_A8_UNORM; 

        case DXGI_FORMAT_R1_UNORM :                     return TEX_FORMAT_R1_UNORM; 
        case DXGI_FORMAT_R9G9B9E5_SHAREDEXP:            return TEX_FORMAT_RGB9E5_SHAREDEXP; 
        case DXGI_FORMAT_R8G8_B8G8_UNORM:               return TEX_FORMAT_RG8_B8G8_UNORM; 
        case DXGI_FORMAT_G8R8_G8B8_UNORM:               return TEX_FORMAT_G8R8_G8B8_UNORM; 

        case DXGI_FORMAT_BC1_TYPELESS:                  return TEX_FORMAT_BC1_TYPELESS; 
        case DXGI_FORMAT_BC1_UNORM:                     return TEX_FORMAT_BC1_UNORM; 
        case DXGI_FORMAT_BC1_UNORM_SRGB:                return TEX_FORMAT_BC1_UNORM_SRGB; 
        case DXGI_FORMAT_BC2_TYPELESS:                  return TEX_FORMAT_BC2_TYPELESS; 
        case DXGI_FORMAT_BC2_UNORM:                     return TEX_FORMAT_BC2_UNORM; 
        case DXGI_FORMAT_BC2_UNORM_SRGB:                return TEX_FORMAT_BC2_UNORM_SRGB; 
        case DXGI_FORMAT_BC3_TYPELESS:                  return TEX_FORMAT_BC3_TYPELESS; 
        case DXGI_FORMAT_BC3_UNORM:                     return TEX_FORMAT_BC3_UNORM; 
        case DXGI_FORMAT_BC3_UNORM_SRGB:                return TEX_FORMAT_BC3_UNORM_SRGB; 
        case DXGI_FORMAT_BC4_TYPELESS:                  return TEX_FORMAT_BC4_TYPELESS; 
        case DXGI_FORMAT_BC4_UNORM:                     return TEX_FORMAT_BC4_UNORM; 
        case DXGI_FORMAT_BC4_SNORM:                     return TEX_FORMAT_BC4_SNORM; 
        case DXGI_FORMAT_BC5_TYPELESS:                  return TEX_FORMAT_BC5_TYPELESS; 
        case DXGI_FORMAT_BC5_UNORM:                     return TEX_FORMAT_BC5_UNORM; 
        case DXGI_FORMAT_BC5_SNORM:                     return TEX_FORMAT_BC5_SNORM; 

        case DXGI_FORMAT_B5G6R5_UNORM:                  return TEX_FORMAT_B5G6R5_UNORM; 
        case DXGI_FORMAT_B5G5R5A1_UNORM:                return TEX_FORMAT_B5G5R5A1_UNORM; 
        case DXGI_FORMAT_B8G8R8A8_UNORM:                return TEX_FORMAT_BGRA8_UNORM; 
        case DXGI_FORMAT_B8G8R8X8_UNORM:                return TEX_FORMAT_BGRX8_UNORM; 

        case DXGI_FORMAT_R10G10B10_XR_BIAS_A2_UNORM:    return TEX_FORMAT_R10G10B10_XR_BIAS_A2_UNORM;

        case DXGI_FORMAT_B8G8R8A8_TYPELESS:             return TEX_FORMAT_BGRA8_TYPELESS; 
        case DXGI_FORMAT_B8G8R8A8_UNORM_SRGB:           return TEX_FORMAT_BGRA8_UNORM_SRGB; 
        case DXGI_FORMAT_B8G8R8X8_TYPELESS:             return TEX_FORMAT_BGRX8_TYPELESS; 
        case DXGI_FORMAT_B8G8R8X8_UNORM_SRGB:           return TEX_FORMAT_BGRX8_UNORM_SRGB; 

        case DXGI_FORMAT_BC6H_TYPELESS:                 return TEX_FORMAT_BC6H_TYPELESS; 
        case DXGI_FORMAT_BC6H_UF16:                     return TEX_FORMAT_BC6H_UF16; 
        case DXGI_FORMAT_BC6H_SF16:                     return TEX_FORMAT_BC6H_SF16; 
        case DXGI_FORMAT_BC7_TYPELESS :                 return TEX_FORMAT_BC7_TYPELESS; 
        case DXGI_FORMAT_BC7_UNORM:                     return TEX_FORMAT_BC7_UNORM; 
        case DXGI_FORMAT_BC7_UNORM_SRGB:                return TEX_FORMAT_BC7_UNORM_SRGB; 

        default: UNEXPECTED( "Unsupported DXGI formate" ); return TEX_FORMAT_UNKNOWN;
    }
}

//--------------------------------------------------------------------------------------
// Get surface information for a particular format
//--------------------------------------------------------------------------------------
static void GetSurfaceInfo(
    _In_ size_t width,
    _In_ size_t height,
    _In_ DXGI_FORMAT fmt,
    _Out_opt_ size_t* outNumBytes,
    _Out_opt_ size_t* outRowBytes,
    _Out_opt_ size_t* outNumRows
    )
{
    size_t numBytes = 0;
    size_t rowBytes = 0;
    size_t numRows = 0;

    bool bc = false;
    bool packed  = false;
    size_t bcnumBytesPerBlock = 0;
    switch (fmt)
    {
    case DXGI_FORMAT_BC1_TYPELESS:
    case DXGI_FORMAT_BC1_UNORM:
    case DXGI_FORMAT_BC1_UNORM_SRGB:
    case DXGI_FORMAT_BC4_TYPELESS:
    case DXGI_FORMAT_BC4_UNORM:
    case DXGI_FORMAT_BC4_SNORM:
        bc = true;
        bcnumBytesPerBlock = 8;
        break;

    case DXGI_FORMAT_BC2_TYPELESS:
    case DXGI_FORMAT_BC2_UNORM:
    case DXGI_FORMAT_BC2_UNORM_SRGB:
    case DXGI_FORMAT_BC3_TYPELESS:
    case DXGI_FORMAT_BC3_UNORM:
    case DXGI_FORMAT_BC3_UNORM_SRGB:
    case DXGI_FORMAT_BC5_TYPELESS:
    case DXGI_FORMAT_BC5_UNORM:
    case DXGI_FORMAT_BC5_SNORM:
    case DXGI_FORMAT_BC6H_TYPELESS:
    case DXGI_FORMAT_BC6H_UF16:
    case DXGI_FORMAT_BC6H_SF16:
    case DXGI_FORMAT_BC7_TYPELESS:
    case DXGI_FORMAT_BC7_UNORM:
    case DXGI_FORMAT_BC7_UNORM_SRGB:
        bc = true;
        bcnumBytesPerBlock = 16;
        break;

    case DXGI_FORMAT_R8G8_B8G8_UNORM:
    case DXGI_FORMAT_G8R8_G8B8_UNORM:
        packed = true;
        break;
    default: break;
    }

    if (bc)
    {
        size_t numBlocksWide = 0;
        if (width > 0)
        {
            numBlocksWide = std::max<size_t>(1, (width + 3) / 4);
        }
        size_t numBlocksHigh = 0;
        if (height > 0)
        {
            numBlocksHigh = std::max<size_t>(1, (height + 3) / 4);
        }
        rowBytes = numBlocksWide * bcnumBytesPerBlock;
        numRows = numBlocksHigh;
    }
    else if (packed)
    {
        rowBytes = ((width + 1) >> 1) * 4;
        numRows = height;
    }
    else
    {
        size_t bpp = BitsPerPixel(fmt);
        rowBytes = (width * bpp + 7) / 8; // round up to nearest byte
        numRows = height;
    }

    numBytes = rowBytes * numRows;
    if (outNumBytes)
    {
        *outNumBytes = numBytes;
    }
    if (outRowBytes)
    {
        *outRowBytes = rowBytes;
    }
    if (outNumRows)
    {
        *outNumRows = numRows;
    }
}


//--------------------------------------------------------------------------------------
#define ISBITMASK(r, g, b, a) (ddpf.RBitMask == r && ddpf.GBitMask == g && ddpf.BBitMask == b && ddpf.ABitMask == a)

static DXGI_FORMAT GetDXGIFormat(const DDS_PIXELFORMAT& ddpf)
{
    if (ddpf.flags & DDS_RGB)
    {
        // Note that sRGB formats are written using the "DX10" extended header

        switch (ddpf.RGBBitCount)
        {
        case 32:
            if (ISBITMASK(0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000))
            {
                return DXGI_FORMAT_R8G8B8A8_UNORM;
            }

            if (ISBITMASK(0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000))
            {
                return DXGI_FORMAT_B8G8R8A8_UNORM;
            }

            if (ISBITMASK(0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000))
            {
                return DXGI_FORMAT_B8G8R8X8_UNORM;
            }

            // No DXGI format maps to ISBITMASK(0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000) aka D3DFMT_X8B8G8R8

            // 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

            // For 'correct' writers, this should be 0x000003ff, 0x000ffc00, 0x3ff00000 for RGB data
            if (ISBITMASK(0x3ff00000, 0x000ffc00, 0x000003ff, 0xc0000000))
            {
                return DXGI_FORMAT_R10G10B10A2_UNORM;
            }

            // No DXGI format maps to ISBITMASK(0x000003ff, 0x000ffc00, 0x3ff00000, 0xc0000000) aka D3DFMT_A2R10G10B10

            if (ISBITMASK(0x0000ffff, 0xffff0000, 0x00000000, 0x00000000))
            {
                return DXGI_FORMAT_R16G16_UNORM;
            }

            if (ISBITMASK(0xffffffff, 0x00000000, 0x00000000, 0x00000000))
            {
                // Only 32-bit color channel format in D3D9 was R32F
                return DXGI_FORMAT_R32_FLOAT; // D3DX writes this out as a FourCC of 114
            }
            break;

        case 24:
            // No 24bpp DXGI formats aka D3DFMT_R8G8B8
            break;

        case 16:
            if (ISBITMASK(0x7c00, 0x03e0, 0x001f, 0x8000))
            {
                return DXGI_FORMAT_B5G5R5A1_UNORM;
            }
            if (ISBITMASK(0xf800, 0x07e0, 0x001f, 0x0000))
            {
                return DXGI_FORMAT_B5G6R5_UNORM;
            }

            // No DXGI format maps to ISBITMASK(0x7c00, 0x03e0, 0x001f, 0x0000) aka D3DFMT_X1R5G5B5
            if (ISBITMASK(0x0f00, 0x00f0, 0x000f, 0xf000))
            {
                return DXGI_FORMAT_B4G4R4A4_UNORM;
            }

            // No DXGI format maps to ISBITMASK(0x0f00, 0x00f0, 0x000f, 0x0000) aka D3DFMT_X4R4G4B4

            // No 3:3:2, 3:3:2:8, or paletted DXGI formats aka D3DFMT_A8R3G3B2, D3DFMT_R3G3B2, D3DFMT_P8, D3DFMT_A8P8, etc.
            break;
        }
    }
    else if (ddpf.flags & DDS_LUMINANCE)
    {
        if (8 == ddpf.RGBBitCount)
        {
            if (ISBITMASK(0x000000ff, 0x00000000, 0x00000000, 0x00000000))
            {
                return DXGI_FORMAT_R8_UNORM; // D3DX10/11 writes this out as DX10 extension
            }

            // No DXGI format maps to ISBITMASK(0x0f, 0x00, 0x00, 0xf0) aka D3DFMT_A4L4
        }

        if (16 == ddpf.RGBBitCount)
        {
            if (ISBITMASK(0x0000ffff, 0x00000000, 0x00000000, 0x00000000))
            {
                return DXGI_FORMAT_R16_UNORM; // D3DX10/11 writes this out as DX10 extension
            }
            if (ISBITMASK(0x000000ff, 0x00000000, 0x00000000, 0x0000ff00))
            {
                return DXGI_FORMAT_R8G8_UNORM; // D3DX10/11 writes this out as DX10 extension
            }
        }
    }
    else if (ddpf.flags & DDS_ALPHA)
    {
        if (8 == ddpf.RGBBitCount)
        {
            return DXGI_FORMAT_A8_UNORM;
        }
    }
    else if (ddpf.flags & DDS_FOURCC)
    {
        if (MAKEFOURCC('D', 'X', 'T', '1') == ddpf.fourCC)
        {
            return DXGI_FORMAT_BC1_UNORM;
        }
        if (MAKEFOURCC('D', 'X', 'T', '3') == ddpf.fourCC)
        {
            return DXGI_FORMAT_BC2_UNORM;
        }
        if (MAKEFOURCC('D', 'X', 'T', '5') == ddpf.fourCC)
        {
            return DXGI_FORMAT_BC3_UNORM;
        }

        // While pre-mulitplied alpha isn't directly supported by the DXGI formats,
        // they are basically the same as these BC formats so they can be mapped
        if (MAKEFOURCC('D', 'X', 'T', '2') == ddpf.fourCC)
        {
            return DXGI_FORMAT_BC2_UNORM;
        }
        if (MAKEFOURCC('D', 'X', 'T', '4') == ddpf.fourCC)
        {
            return DXGI_FORMAT_BC3_UNORM;
        }

        if (MAKEFOURCC('A', 'T', 'I', '1') == ddpf.fourCC)
        {
            return DXGI_FORMAT_BC4_UNORM;
        }
        if (MAKEFOURCC('B', 'C', '4', 'U') == ddpf.fourCC)
        {
            return DXGI_FORMAT_BC4_UNORM;
        }
        if (MAKEFOURCC('B', 'C', '4', 'S') == ddpf.fourCC)
        {
            return DXGI_FORMAT_BC4_SNORM;
        }

        if (MAKEFOURCC('A', 'T', 'I', '2') == ddpf.fourCC)
        {
            return DXGI_FORMAT_BC5_UNORM;
        }
        if (MAKEFOURCC('B', 'C', '5', 'U') == ddpf.fourCC)
        {
            return DXGI_FORMAT_BC5_UNORM;
        }
        if (MAKEFOURCC('B', 'C', '5', 'S') == ddpf.fourCC)
        {
            return DXGI_FORMAT_BC5_SNORM;
        }

        // BC6H and BC7 are written using the "DX10" extended header

        if (MAKEFOURCC('R', 'G', 'B', 'G') == ddpf.fourCC)
        {
            return DXGI_FORMAT_R8G8_B8G8_UNORM;
        }
        if (MAKEFOURCC('G', 'R', 'G', 'B') == ddpf.fourCC)
        {
            return DXGI_FORMAT_G8R8_G8B8_UNORM;
        }

        // Check for D3DFORMAT enums being set here
        switch (ddpf.fourCC)
        {
        case 36: // D3DFMT_A16B16G16R16
            return DXGI_FORMAT_R16G16B16A16_UNORM;

        case 110: // D3DFMT_Q16W16V16U16
            return DXGI_FORMAT_R16G16B16A16_SNORM;

        case 111: // D3DFMT_R16F
            return DXGI_FORMAT_R16_FLOAT;

        case 112: // D3DFMT_G16R16F
            return DXGI_FORMAT_R16G16_FLOAT;

        case 113: // D3DFMT_A16B16G16R16F
            return DXGI_FORMAT_R16G16B16A16_FLOAT;

        case 114: // D3DFMT_R32F
            return DXGI_FORMAT_R32_FLOAT;

        case 115: // D3DFMT_G32R32F
            return DXGI_FORMAT_R32G32_FLOAT;

        case 116: // D3DFMT_A32B32G32R32F
            return DXGI_FORMAT_R32G32B32A32_FLOAT;
        }
    }

    return DXGI_FORMAT_UNKNOWN;
}


//--------------------------------------------------------------------------------------
static DXGI_FORMAT MakeSRGB(_In_ DXGI_FORMAT format)
{
    switch (format)
    {
    case DXGI_FORMAT_R8G8B8A8_UNORM:
        return DXGI_FORMAT_R8G8B8A8_UNORM_SRGB;

    case DXGI_FORMAT_BC1_UNORM:
        return DXGI_FORMAT_BC1_UNORM_SRGB;

    case DXGI_FORMAT_BC2_UNORM:
        return DXGI_FORMAT_BC2_UNORM_SRGB;

    case DXGI_FORMAT_BC3_UNORM:
        return DXGI_FORMAT_BC3_UNORM_SRGB;

    case DXGI_FORMAT_B8G8R8A8_UNORM:
        return DXGI_FORMAT_B8G8R8A8_UNORM_SRGB;

    case DXGI_FORMAT_B8G8R8X8_UNORM:
        return DXGI_FORMAT_B8G8R8X8_UNORM_SRGB;

    case DXGI_FORMAT_BC7_UNORM:
        return DXGI_FORMAT_BC7_UNORM_SRGB;

    default:
        return format;
    }
}


//--------------------------------------------------------------------------------------
static void FillInitData(
    _In_ size_t width,
    _In_ size_t height,
    _In_ size_t depth,
    _In_ size_t mipCount,
    _In_ size_t arraySize,
    _In_ DXGI_FORMAT format,
    _In_ size_t maxsize,
    _In_ size_t bitSize,
    _In_ const Uint8* bitData,
    _Out_ size_t& twidth,
    _Out_ size_t& theight,
    _Out_ size_t& tdepth,
    _Out_ size_t& skipMip,
    _Out_ TextureSubResData *initData
    )
{
    if (!bitData || !initData)
    {
        LOG_ERROR_AND_THROW( "Invalid arguments" );
    }

    skipMip = 0;
    twidth = 0;
    theight = 0;
    tdepth = 0;

    size_t NumBytes = 0;
    size_t RowBytes = 0;
    size_t NumRows = 0;
    const Uint8* pSrcBits = bitData;
    const Uint8* pEndBits = bitData + bitSize;

    size_t index = 0;
    for (size_t j = 0; j < arraySize; j++)
    {
        size_t w = width;
        size_t h = height;
        size_t d = depth;
        for (size_t i = 0; i < mipCount; i++)
        {
            GetSurfaceInfo(w, h, format, &NumBytes, &RowBytes, &NumRows);

            if ((mipCount <= 1) || !maxsize || (w <= maxsize && h <= maxsize && d <= maxsize))
            {
                if (!twidth)
                {
                    twidth = w;
                    theight = h;
                    tdepth = d;
                }

                VERIFY_EXPR(index < mipCount * arraySize);
                initData[index].pData = (const void*)pSrcBits;
                initData[index].Stride = static_cast<Uint32>(RowBytes);
                initData[index].DepthStride = static_cast<Uint32>(NumBytes);
                ++index;
            }
            else if (!j)
            {
                // Count number of skipped mipmaps (first item only)
                ++skipMip;
            }

            if (pSrcBits + (NumBytes*d) > pEndBits)
            {
                LOG_ERROR_AND_THROW( "Out of bounds" );
            }

            pSrcBits += NumBytes * d;

            w = w >> 1;
            h = h >> 1;
            d = d >> 1;
            if (w == 0)
            {
                w = 1;
            }
            if (h == 0)
            {
                h = 1;
            }
            if (d == 0)
            {
                d = 1;
            }
        }
    }

    if (!index)
    {
        LOG_ERROR_AND_THROW( "Unknown error" );
    }
}


//--------------------------------------------------------------------------------------
static void CreateTexture(
    _In_ IRenderDevice* pDevice,
    _In_ Uint32 resDim,
    _In_ size_t width,
    _In_ size_t height,
    _In_ size_t depth,
    _In_ size_t mipCount,
    _In_ size_t arraySize,
    _In_ DXGI_FORMAT format,
    _In_ USAGE usage,
    _In_ const char *name,
    _In_ BIND_FLAGS bindFlags,
    _In_ CPU_ACCESS_FLAGS cpuAccessFlags,
    _In_ MISC_TEXTURE_FLAGS miscFlags,
    _In_ bool forceSRGB,
    _In_ bool isCubeMap,
    _In_ TextureSubResData* initData,
    _Outptr_opt_ ITexture** texture
    )
{
    if (!pDevice || !initData)
    {
        LOG_ERROR_AND_THROW("Invalid arguments");
    }


    if (forceSRGB)
    {
        format = MakeSRGB(format);
    }

    TextureDesc desc;
    desc.Name = name;
    desc.Width = static_cast<Uint32>(width);
    desc.MipLevels = static_cast<Uint32>(mipCount);
    desc.ArraySize = static_cast<Uint32>(arraySize);
    desc.Format = DXGIFormatToTexFormat(format);
    desc.Usage = usage;
    desc.BindFlags = bindFlags;
    desc.CPUAccessFlags = cpuAccessFlags;
    //desc.MiscFlags = miscFlags & ~D3D11_RESOURCE_MISC_TEXTURECUBE;

    TextureData InitData;
    InitData.pSubResources = initData;
    InitData.NumSubresources = static_cast<Uint32>(mipCount * arraySize);

    switch (resDim)
    {
        case D3D11_RESOURCE_DIMENSION_TEXTURE1D:
            {
                desc.Type = arraySize > 1 ? RESOURCE_DIM_TEX_1D_ARRAY : RESOURCE_DIM_TEX_1D;
                pDevice->CreateTexture( desc, InitData, texture );
                
            //    ID3D11Texture1D* tex = nullptr;
            //    hr = d3dDevice->CreateTexture1D(&desc, initData, &tex);

            //    if (SUCCEEDED(hr) && tex != 0)
            //    {
            //        if (textureView != 0)
            //        {
            //            D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc;
            //            memset(&SRVDesc, 0, sizeof(SRVDesc));
            //            SRVDesc.Format = format;

            //            if (arraySize > 1)
            //            {
            //                SRVDesc.ViewDimension = D3D_SRV_DIMENSION_TEXTURE1DARRAY;
            //                SRVDesc.Texture1DArray.MipLevels = desc.MipLevels;
            //                SRVDesc.Texture1DArray.ArraySize = static_cast<UINT>(arraySize);
            //            }
            //            else
            //            {
            //                SRVDesc.ViewDimension = D3D_SRV_DIMENSION_TEXTURE1D;
            //                SRVDesc.Texture1D.MipLevels = desc.MipLevels;
            //            }

            //            hr = d3dDevice->CreateShaderResourceView(tex, &SRVDesc, textureView);

            //            if (FAILED(hr))
            //            {
            //                tex->Release();
            //                return hr;
            //            }
            //        }

            //        if (texture != 0)
            //        {
            //            *texture = tex;
            //        }
            //        else
            //        {
            //            tex->Release();
            //        }
            //    }
            }
            break;

        case D3D11_RESOURCE_DIMENSION_TEXTURE2D:
            {
                desc.Type = isCubeMap ?
                    (arraySize/6 > 1 ? RESOURCE_DIM_TEX_CUBE_ARRAY : RESOURCE_DIM_TEX_CUBE) : 
                    (arraySize > 1 ? RESOURCE_DIM_TEX_2D_ARRAY : RESOURCE_DIM_TEX_2D);
                desc.Height = static_cast<Uint32>(height);

                //if (isCubeMap)
                //{
                //    desc.MiscFlags = miscFlags | D3D11_RESOURCE_MISC_TEXTURECUBE;
                //}
                //else
                //{
                //    desc.MiscFlags = miscFlags & ~D3D11_RESOURCE_MISC_TEXTURECUBE;
                //}

                pDevice->CreateTexture( desc, InitData, texture );

                //ID3D11Texture2D* tex = nullptr;
                //hr = d3dDevice->CreateTexture2D(&desc, initData, &tex);

                //if (SUCCEEDED(hr) && tex != 0)
                //{
                //    if (textureView != 0)
                //    {
                //        D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc;
                //        memset(&SRVDesc, 0, sizeof(SRVDesc));
                //        SRVDesc.Format = format;

                //        if (isCubeMap)
                //        {
                //            if (arraySize > 6)
                //            {
                //                SRVDesc.ViewDimension = D3D_SRV_DIMENSION_TEXTURECUBEARRAY;
                //                SRVDesc.TextureCubeArray.MipLevels = desc.MipLevels;

                //                // Earlier we set arraySize to (NumCubes * 6)
                //                SRVDesc.TextureCubeArray.NumCubes = static_cast<UINT>(arraySize / 6);
                //            }
                //            else
                //            {
                //                SRVDesc.ViewDimension = D3D_SRV_DIMENSION_TEXTURECUBE;
                //                SRVDesc.TextureCube.MipLevels = desc.MipLevels;
                //            }
                //        }
                //        else if (arraySize > 1)
                //        {
                //            SRVDesc.ViewDimension = D3D_SRV_DIMENSION_TEXTURE2DARRAY;
                //            SRVDesc.Texture2DArray.MipLevels = desc.MipLevels;
                //            SRVDesc.Texture2DArray.ArraySize = static_cast<UINT>(arraySize);
                //        }
                //        else
                //        {
                //            SRVDesc.ViewDimension = D3D_SRV_DIMENSION_TEXTURE2D;
                //            SRVDesc.Texture2D.MipLevels = desc.MipLevels;
                //        }

                //        hr = d3dDevice->CreateShaderResourceView(tex, &SRVDesc, textureView);

                //        if (FAILED(hr))
                //        {
                //            tex->Release();
                //            return hr;
                //        }
                //    }

                //    if (texture != 0)
                //    {
                //        *texture = tex;
                //    }
                //    else
                //    {
                //        tex->Release();
                //    }
                //}
            }
            break;

        case D3D11_RESOURCE_DIMENSION_TEXTURE3D:
            {
                desc.Type = RESOURCE_DIM_TEX_3D;
                desc.Height = static_cast<Uint32>(height);
                desc.Depth = static_cast<Uint32>(depth);

                //desc.MiscFlags = miscFlags & ~D3D11_RESOURCE_MISC_TEXTURECUBE;

                pDevice->CreateTexture( desc, InitData, texture );

                //ID3D11Texture3D* tex = nullptr;
                //hr = d3dDevice->CreateTexture3D(&desc, initData, &tex);

                //if (SUCCEEDED(hr) && tex != 0)
                //{
                //    if (textureView != 0)
                //    {
                //        D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc;
                //        memset(&SRVDesc, 0, sizeof(SRVDesc));
                //        SRVDesc.Format = format;
                //        SRVDesc.ViewDimension = D3D_SRV_DIMENSION_TEXTURE3D;
                //        SRVDesc.Texture3D.MipLevels = desc.MipLevels;

                //        hr = d3dDevice->CreateShaderResourceView(tex, &SRVDesc, textureView);

                //        if (FAILED(hr))
                //        {
                //            tex->Release();
                //            return hr;
                //        }
                //    }

                //    if (texture != 0)
                //    {
                //        *texture = tex;
                //    }
                //    else
                //    {
                //        tex->Release();
                //    }
                //}
            }
            break;

        default:
            LOG_ERROR_AND_THROW("Invalid resource dimension (", resDim, ")");
    }
}

//--------------------------------------------------------------------------------------
static void CreateTextureFromDDS(
    _In_ IRenderDevice* pDevice,
    _In_ const DDS_HEADER* header,
    _In_ const Uint8* bitData,
    _In_ size_t bitSize,
    _In_ size_t maxsize,
    _In_ USAGE usage,
    _In_ const char *name,
    _In_ BIND_FLAGS bindFlags,
    _In_ CPU_ACCESS_FLAGS cpuAccessFlags,
    _In_ MISC_TEXTURE_FLAGS miscFlags,
    _In_ bool forceSRGB,
    _Outptr_opt_ ITexture** texture
    )
{
    size_t width = header->width;
    size_t height = header->height;
    size_t depth = header->depth;

    Uint32 resDim = D3D11_RESOURCE_DIMENSION_UNKNOWN;
    size_t arraySize = 1;
    DXGI_FORMAT format = DXGI_FORMAT_UNKNOWN;
    bool isCubeMap = false;

    size_t mipCount = header->mipMapCount;
    if (0 == mipCount)
    {
        mipCount = 1;
    }


    if ((header->ddspf.flags & DDS_FOURCC) &&
        (MAKEFOURCC('D', 'X', '1', '0') == header->ddspf.fourCC))
    {
        auto d3d10ext = reinterpret_cast<const DDS_HEADER_DXT10*>((const char*)header + sizeof(DDS_HEADER));

        arraySize = d3d10ext->arraySize;
        if (arraySize == 0)
        {
            LOG_ERROR_AND_THROW("Array size is zero");
        }

        if (BitsPerPixel(d3d10ext->dxgiFormat) == 0)
        {
            LOG_ERROR_AND_THROW("Undefined DXGI format");
        }

        format = d3d10ext->dxgiFormat;

        switch (d3d10ext->resourceDimension)
        {
        case D3D11_RESOURCE_DIMENSION_TEXTURE1D:
            // D3DX writes 1D textures with a fixed Height of 1
            if ((header->flags & DDS_HEIGHT) && height != 1)
            {
                LOG_ERROR_AND_THROW("Unexpected height (", height, ") for texture 1D");
            }
            height = depth = 1;
            break;

        case D3D11_RESOURCE_DIMENSION_TEXTURE2D:
            if (d3d10ext->miscFlag & D3D11_RESOURCE_MISC_TEXTURECUBE)
            {
                arraySize *= 6;
                isCubeMap = true;
            }
            depth = 1;
            break;

        case D3D11_RESOURCE_DIMENSION_TEXTURE3D:
            if (!(header->flags & DDS_HEADER_FLAGS_VOLUME))
            {
                LOG_ERROR_AND_THROW("DDS_HEADER_FLAGS_VOLUME flag is not set");
            }

            if (arraySize > 1)
            {
                LOG_ERROR_AND_THROW("Texture3D arrays are not allowed");
            }
            break;

        default:
            LOG_ERROR_AND_THROW("Unknown resource dimension");
        }

        resDim = d3d10ext->resourceDimension;
    }
    else
    {
        format = GetDXGIFormat(header->ddspf);

        if (format == DXGI_FORMAT_UNKNOWN)
        {
            LOG_ERROR_AND_THROW("Unknown DXGIF format");
        }

        if (header->flags & DDS_HEADER_FLAGS_VOLUME)
        {
            resDim = D3D11_RESOURCE_DIMENSION_TEXTURE3D;
        }
        else
        {
            if (header->caps2 & DDS_CUBEMAP)
            {
                // We require all six faces to be defined
                if ((header->caps2 & DDS_CUBEMAP_ALLFACES) != DDS_CUBEMAP_ALLFACES)
                {
                    LOG_ERROR_AND_THROW("All six faces of a cubemap must be defined");
                }

                arraySize = 6;
                isCubeMap = true;
            }

            depth = 1;
            resDim = D3D11_RESOURCE_DIMENSION_TEXTURE2D;

            // Note there's no way for a legacy Direct3D 9 DDS to express a '1D' texture
        }

        VERIFY_EXPR(BitsPerPixel(format) != 0);
    }

    // Bound sizes (for security purposes we don't trust DDS file metadata larger than the D3D 11.x hardware requirements)
    if (mipCount > D3D11_REQ_MIP_LEVELS)
    {
        LOG_ERROR_AND_THROW("Too many mip levels specified");
    }

    switch (resDim)
    {
        case D3D11_RESOURCE_DIMENSION_TEXTURE1D:
            if ((arraySize > D3D11_REQ_TEXTURE1D_ARRAY_AXIS_DIMENSION) ||
                (width > D3D11_REQ_TEXTURE1D_U_DIMENSION))
            {
                LOG_ERROR_AND_THROW("Texture1D dimensions are out of bounds");
            }
            break;

        case D3D11_RESOURCE_DIMENSION_TEXTURE2D:
            if (isCubeMap)
            {
                // This is the right bound because we set arraySize to (NumCubes*6) above
                if ((arraySize > D3D11_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION) ||
                    (width > D3D11_REQ_TEXTURECUBE_DIMENSION) ||
                    (height > D3D11_REQ_TEXTURECUBE_DIMENSION))
                {
                    LOG_ERROR_AND_THROW("TextureCube dimensions are out of bounds");
                }
            }
            else if ((arraySize > D3D11_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION) ||
                    (width > D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION) ||
                    (height > D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION))
            {
                LOG_ERROR_AND_THROW("Texture2D dimensions are out of bounds");
            }
            break;

        case D3D11_RESOURCE_DIMENSION_TEXTURE3D:
            if ((arraySize > 1) ||
                (width > D3D11_REQ_TEXTURE3D_U_V_OR_W_DIMENSION) ||
                (height > D3D11_REQ_TEXTURE3D_U_V_OR_W_DIMENSION) ||
                (depth > D3D11_REQ_TEXTURE3D_U_V_OR_W_DIMENSION))
            {
                LOG_ERROR_AND_THROW("Texture3D dimensions are out of bounds");
            }
            break;
    }
    // Create the texture
    std::unique_ptr<TextureSubResData[]> initData(new TextureSubResData[mipCount * arraySize]);

    size_t skipMip = 0;
    size_t twidth = 0;
    size_t theight = 0;
    size_t tdepth = 0;
    FillInitData(width, height, depth, mipCount, arraySize, format, maxsize, bitSize, bitData, twidth, theight, tdepth, skipMip, initData.get());

    CreateTexture(pDevice, resDim, twidth, theight, tdepth, mipCount - skipMip, arraySize, format, usage, name, bindFlags, cpuAccessFlags, miscFlags, forceSRGB, isCubeMap, initData.get(), texture/*, textureView*/);

#if 0
    if (FAILED(hr) && !maxsize && (mipCount > 1))
    {
        // Retry with a maxsize determined by feature level
        switch (d3dDevice->GetFeatureLevel())
        {
        case D3D_FEATURE_LEVEL_9_1:
        case D3D_FEATURE_LEVEL_9_2:
            if (isCubeMap)
            {
                maxsize = D3D_FL9_1_REQ_TEXTURECUBE_DIMENSION;
            }
            else
            {
                maxsize = (resDim == D3D11_RESOURCE_DIMENSION_TEXTURE3D)
                    ? D3D_FL9_1_REQ_TEXTURE3D_U_V_OR_W_DIMENSION
                    : D3D_FL9_1_REQ_TEXTURE2D_U_OR_V_DIMENSION;
            }
            break;

        case D3D_FEATURE_LEVEL_9_3:
            maxsize = (resDim == D3D11_RESOURCE_DIMENSION_TEXTURE3D)
                ? D3D_FL9_1_REQ_TEXTURE3D_U_V_OR_W_DIMENSION
                : D3D_FL9_3_REQ_TEXTURE2D_U_OR_V_DIMENSION;
            break;

        default: // D3D_FEATURE_LEVEL_10_0 & D3D_FEATURE_LEVEL_10_1
            maxsize = (resDim == D3D11_RESOURCE_DIMENSION_TEXTURE3D)
                ? D3D10_REQ_TEXTURE3D_U_V_OR_W_DIMENSION
                : D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION;
            break;
        }

        FillInitData(width, height, depth, mipCount, arraySize, format, maxsize, bitSize, bitData, twidth, theight, tdepth, skipMip, initData.get());

        hr = CreateTexture(d3dDevice, resDim, twidth, theight, tdepth, mipCount - skipMip, arraySize, format, usage, bindFlags, cpuAccessFlags, miscFlags, forceSRGB, isCubeMap, initData.get(), texture, textureView);
    }
#endif
}

#if 0
//--------------------------------------------------------------------------------------
static D2D1_ALPHA_MODE GetAlphaMode(_In_ const DDS_HEADER* header)
{
    if (header->ddspf.flags & DDS_FOURCC)
    {
        if (MAKEFOURCC('D', 'X', '1', '0') == header->ddspf.fourCC)
        {
            auto d3d10ext = reinterpret_cast<const DDS_HEADER_DXT10*>((const char*)header + sizeof(DDS_HEADER));
            switch (d3d10ext->miscFlags2 & DDS_MISC_FLAGS2_ALPHA_MODE_MASK)
            {
            case DDS_ALPHA_MODE_STRAIGHT:
                return D2D1_ALPHA_MODE_STRAIGHT;

            case DDS_ALPHA_MODE_PREMULTIPLIED:
                return D2D1_ALPHA_MODE_PREMULTIPLIED;

            case DDS_ALPHA_MODE_OPAQUE:
            case DDS_ALPHA_MODE_CUSTOM:
                // No D2D1_ALPHA_MODE equivalent, so return "Ignore" for now
                return D2D1_ALPHA_MODE_IGNORE;
            }
        }
        else if ((MAKEFOURCC('D', 'X', 'T', '2') == header->ddspf.fourCC)
                  || (MAKEFOURCC('D', 'X', 'T', '4') == header->ddspf.fourCC))
        {
            return D2D1_ALPHA_MODE_PREMULTIPLIED;
        }
        // DXT1, DXT3, and DXT5 legacy files could be straight alpha or something else, so return "Unknown" to leave it up to the app
    }

    return D2D1_ALPHA_MODE_UNKNOWN;
}

#endif

//--------------------------------------------------------------------------------------
void CreateDDSTextureFromMemory(
    IRenderDevice* pDevice,
    const Uint8* ddsData,
    size_t ddsDataSize,
    ITexture** texture,
    size_t maxsize,
    /*D2D1_ALPHA_MODE* alphaMode,*/
    const char* name)
{
    return CreateDDSTextureFromMemoryEx(pDevice, ddsData, ddsDataSize, maxsize, USAGE_DEFAULT, name, BIND_SHADER_RESOURCE, CPU_ACCESS_NONE, MISC_TEXTURE_FLAG_NONE, false, texture/*, alphaMode*/);
}


//--------------------------------------------------------------------------------------
void CreateDDSTextureFromMemoryEx(
    IRenderDevice* pDevice,
    const Uint8* ddsData,
    size_t ddsDataSize,
    size_t maxsize,
    USAGE usage,
    const char *name,
    BIND_FLAGS bindFlags,
    CPU_ACCESS_FLAGS cpuAccessFlags,
    MISC_TEXTURE_FLAGS miscFlags,
    bool forceSRGB,
    ITexture** texture/*,
    D2D1_ALPHA_MODE* alphaMode*/
    )
{
    if (texture)
    {
        VERIFY( *texture == nullptr, "Overwriting reference to an existing object is unsafe");
        *texture = nullptr;
    }
    //if (textureView)
    //{
    //    *textureView = nullptr;
    //}
    //if (alphaMode)
    //{
    //    *alphaMode = D2D1_ALPHA_MODE_UNKNOWN;
    //}

    if (!pDevice || !ddsData || (!texture /*&& !textureView*/))
    {
        LOG_ERROR_AND_THROW("Invalid arguments");
    }

    // Validate DDS file in memory
    if (ddsDataSize < (sizeof(Uint32) + sizeof(DDS_HEADER)))
    {
        LOG_ERROR_AND_THROW("Invalid dds file");
    }

    Uint32 dwMagicNumber = *(const Uint32*)(ddsData);
    if (dwMagicNumber != DDS_MAGIC)
    {
        LOG_ERROR_AND_THROW("Invalid dds file");
    }

    auto header = reinterpret_cast<const DDS_HEADER*>(ddsData + sizeof(Uint32));

    // Verify header to validate DDS file
    if (header->size != sizeof(DDS_HEADER) ||
        header->ddspf.size != sizeof(DDS_PIXELFORMAT))
    {
        LOG_ERROR_AND_THROW("Invalid dds file header");
    }

    // Check for DX10 extension
    bool bDXT10Header = false;
    if ((header->ddspf.flags & DDS_FOURCC) &&
        (MAKEFOURCC('D', 'X', '1', '0') == header->ddspf.fourCC))
    {
        // Must be long enough for both headers and magic value
        if (ddsDataSize < (sizeof(DDS_HEADER) + sizeof(Uint32) + sizeof(DDS_HEADER_DXT10)))
        {
            LOG_ERROR_AND_THROW("Invalid DX10 extension");
        }

        bDXT10Header = true;
    }

    ptrdiff_t offset = sizeof(Uint32) + sizeof(DDS_HEADER) + (bDXT10Header ? sizeof(DDS_HEADER_DXT10) : 0);

    CreateTextureFromDDS(pDevice, header, ddsData + offset, ddsDataSize - offset, maxsize, usage, name, bindFlags, cpuAccessFlags, miscFlags, forceSRGB, texture/*, textureView*/);

    //if (alphaMode)
    //    *alphaMode = GetAlphaMode(header);
}