home *** CD-ROM | disk | FTP | other *** search
/ Introduction to 3D Game …ogramming with DirectX 12 / Introduction-to-3D-Game-Programming-with-DirectX-12.ISO / Code.Textures / Common / DDSTextureLoader.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2016-03-02  |  77.8 KB  |  2,368 lines

  1. //--------------------------------------------------------------------------------------
  2. // File: DDSTextureLoader.cpp
  3. //
  4. // Functions for loading a DDS texture and creating a Direct3D 11 runtime resource for it
  5. //
  6. // Note these functions are useful as a light-weight runtime loader for DDS files. For
  7. // a full-featured DDS file reader, writer, and texture processing pipeline see
  8. // the 'Texconv' sample and the 'DirectXTex' library.
  9. //
  10. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  11. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  12. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  13. // PARTICULAR PURPOSE.
  14. //
  15. // Copyright (c) Microsoft Corporation. All rights reserved.
  16. //
  17. // http://go.microsoft.com/fwlink/?LinkId=248926
  18. // http://go.microsoft.com/fwlink/?LinkId=248929
  19. //--------------------------------------------------------------------------------------
  20.  
  21. #include <assert.h>
  22. #include <algorithm>
  23. #include <memory>
  24. #include <wrl.h>
  25.  
  26. #include "DDSTextureLoader.h" 
  27.  
  28. using namespace Microsoft::WRL;
  29.  
  30. #if !defined(NO_D3D11_DEBUG_NAME) && ( defined(_DEBUG) || defined(PROFILE) )
  31. #pragma comment(lib,"dxguid.lib")
  32. #endif
  33.  
  34. using namespace DirectX;
  35.  
  36. //--------------------------------------------------------------------------------------
  37. // Macros
  38. //--------------------------------------------------------------------------------------
  39. #ifndef MAKEFOURCC
  40.     #define MAKEFOURCC(ch0, ch1, ch2, ch3)                              \
  41.                 ((uint32_t)(uint8_t)(ch0) | ((uint32_t)(uint8_t)(ch1) << 8) |       \
  42.                 ((uint32_t)(uint8_t)(ch2) << 16) | ((uint32_t)(uint8_t)(ch3) << 24 ))
  43. #endif /* defined(MAKEFOURCC) */
  44.  
  45. //--------------------------------------------------------------------------------------
  46. // DDS file structure definitions
  47. //
  48. // See DDS.h in the 'Texconv' sample and the 'DirectXTex' library
  49. //--------------------------------------------------------------------------------------
  50. #pragma pack(push,1)
  51.  
  52. const uint32_t DDS_MAGIC = 0x20534444; // "DDS "
  53.  
  54. struct DDS_PIXELFORMAT
  55. {
  56.     uint32_t    size;
  57.     uint32_t    flags;
  58.     uint32_t    fourCC;
  59.     uint32_t    RGBBitCount;
  60.     uint32_t    RBitMask;
  61.     uint32_t    GBitMask;
  62.     uint32_t    BBitMask;
  63.     uint32_t    ABitMask;
  64. };
  65.  
  66. #define DDS_FOURCC      0x00000004  // DDPF_FOURCC
  67. #define DDS_RGB         0x00000040  // DDPF_RGB
  68. #define DDS_LUMINANCE   0x00020000  // DDPF_LUMINANCE
  69. #define DDS_ALPHA       0x00000002  // DDPF_ALPHA
  70.  
  71. #define DDS_HEADER_FLAGS_VOLUME         0x00800000  // DDSD_DEPTH
  72.  
  73. #define DDS_HEIGHT 0x00000002 // DDSD_HEIGHT
  74. #define DDS_WIDTH  0x00000004 // DDSD_WIDTH
  75.  
  76. #define DDS_CUBEMAP_POSITIVEX 0x00000600 // DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_POSITIVEX
  77. #define DDS_CUBEMAP_NEGATIVEX 0x00000a00 // DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_NEGATIVEX
  78. #define DDS_CUBEMAP_POSITIVEY 0x00001200 // DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_POSITIVEY
  79. #define DDS_CUBEMAP_NEGATIVEY 0x00002200 // DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_NEGATIVEY
  80. #define DDS_CUBEMAP_POSITIVEZ 0x00004200 // DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_POSITIVEZ
  81. #define DDS_CUBEMAP_NEGATIVEZ 0x00008200 // DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_NEGATIVEZ
  82.  
  83. #define DDS_CUBEMAP_ALLFACES ( DDS_CUBEMAP_POSITIVEX | DDS_CUBEMAP_NEGATIVEX |\
  84.                                DDS_CUBEMAP_POSITIVEY | DDS_CUBEMAP_NEGATIVEY |\
  85.                                DDS_CUBEMAP_POSITIVEZ | DDS_CUBEMAP_NEGATIVEZ )
  86.  
  87. #define DDS_CUBEMAP 0x00000200 // DDSCAPS2_CUBEMAP
  88.  
  89. enum DDS_MISC_FLAGS2
  90. {
  91.     DDS_MISC_FLAGS2_ALPHA_MODE_MASK = 0x7L,
  92. };
  93.  
  94. struct DDS_HEADER
  95. {
  96.     uint32_t        size;
  97.     uint32_t        flags;
  98.     uint32_t        height;
  99.     uint32_t        width;
  100.     uint32_t        pitchOrLinearSize;
  101.     uint32_t        depth; // only if DDS_HEADER_FLAGS_VOLUME is set in flags
  102.     uint32_t        mipMapCount;
  103.     uint32_t        reserved1[11];
  104.     DDS_PIXELFORMAT ddspf;
  105.     uint32_t        caps;
  106.     uint32_t        caps2;
  107.     uint32_t        caps3;
  108.     uint32_t        caps4;
  109.     uint32_t        reserved2;
  110. };
  111.  
  112. struct DDS_HEADER_DXT10
  113. {
  114.     DXGI_FORMAT     dxgiFormat;
  115.     uint32_t        resourceDimension;
  116.     uint32_t        miscFlag; // see D3D11_RESOURCE_MISC_FLAG
  117.     uint32_t        arraySize;
  118.     uint32_t        miscFlags2;
  119. };
  120.  
  121. #pragma pack(pop)
  122.  
  123. //--------------------------------------------------------------------------------------
  124. namespace
  125. {
  126.  
  127. struct handle_closer { void operator()(HANDLE h) { if (h) CloseHandle(h); } };
  128.  
  129. typedef public std::unique_ptr<void, handle_closer> ScopedHandle;
  130.  
  131. inline HANDLE safe_handle( HANDLE h ) { return (h == INVALID_HANDLE_VALUE) ? 0 : h; }
  132.  
  133. template<UINT TNameLength>
  134. inline void SetDebugObjectName(_In_ ID3D11DeviceChild* resource, _In_ const char (&name)[TNameLength])
  135. {
  136. #if defined(_DEBUG) || defined(PROFILE)
  137.     resource->SetPrivateData(WKPDID_D3DDebugObjectName, TNameLength - 1, name);
  138. #else
  139.     UNREFERENCED_PARAMETER(resource);
  140.     UNREFERENCED_PARAMETER(name);
  141. #endif
  142. }
  143.  
  144. };
  145.  
  146. //--------------------------------------------------------------------------------------
  147. static HRESULT LoadTextureDataFromFile( _In_z_ const wchar_t* fileName,
  148.                                         std::unique_ptr<uint8_t[]>& ddsData,
  149.                                         DDS_HEADER** header,
  150.                                         uint8_t** bitData,
  151.                                         size_t* bitSize
  152.                                       )
  153. {
  154.     if (!header || !bitData || !bitSize)
  155.     {
  156.         return E_POINTER;
  157.     }
  158.  
  159.     // open the file
  160. #if (_WIN32_WINNT >= _WIN32_WINNT_WIN8)
  161.     ScopedHandle hFile( safe_handle( CreateFile2( fileName,
  162.                                                   GENERIC_READ,
  163.                                                   FILE_SHARE_READ,
  164.                                                   OPEN_EXISTING,
  165.                                                   nullptr ) ) );
  166. #else
  167.     ScopedHandle hFile( safe_handle( CreateFileW( fileName,
  168.                                                   GENERIC_READ,
  169.                                                   FILE_SHARE_READ,
  170.                                                   nullptr,
  171.                                                   OPEN_EXISTING,
  172.                                                   FILE_ATTRIBUTE_NORMAL,
  173.                                                   nullptr ) ) );
  174. #endif
  175.  
  176.     if ( !hFile )
  177.     {
  178.         return HRESULT_FROM_WIN32( GetLastError() );
  179.     }
  180.  
  181.     // Get the file size
  182.     LARGE_INTEGER FileSize = { 0 };
  183.  
  184. #if (_WIN32_WINNT >= _WIN32_WINNT_VISTA)
  185.     FILE_STANDARD_INFO fileInfo;
  186.     if ( !GetFileInformationByHandleEx( hFile.get(), FileStandardInfo, &fileInfo, sizeof(fileInfo) ) )
  187.     {
  188.         return HRESULT_FROM_WIN32( GetLastError() );
  189.     }
  190.     FileSize = fileInfo.EndOfFile;
  191. #else
  192.     GetFileSizeEx( hFile.get(), &FileSize );
  193. #endif
  194.  
  195.     // File is too big for 32-bit allocation, so reject read
  196.     if (FileSize.HighPart > 0)
  197.     {
  198.         return E_FAIL;
  199.     }
  200.  
  201.     // Need at least enough data to fill the header and magic number to be a valid DDS
  202.     if (FileSize.LowPart < ( sizeof(DDS_HEADER) + sizeof(uint32_t) ) )
  203.     {
  204.         return E_FAIL;
  205.     }
  206.  
  207.     // create enough space for the file data
  208.     ddsData.reset( new (std::nothrow) uint8_t[ FileSize.LowPart ] );
  209.     if (!ddsData)
  210.     {
  211.         return E_OUTOFMEMORY;
  212.     }
  213.  
  214.     // read the data in
  215.     DWORD BytesRead = 0;
  216.     if (!ReadFile( hFile.get(),
  217.                    ddsData.get(),
  218.                    FileSize.LowPart,
  219.                    &BytesRead,
  220.                    nullptr
  221.                  ))
  222.     {
  223.         return HRESULT_FROM_WIN32( GetLastError() );
  224.     }
  225.  
  226.     if (BytesRead < FileSize.LowPart)
  227.     {
  228.         return E_FAIL;
  229.     }
  230.  
  231.     // DDS files always start with the same magic number ("DDS ")
  232.     uint32_t dwMagicNumber = *( const uint32_t* )( ddsData.get() );
  233.     if (dwMagicNumber != DDS_MAGIC)
  234.     {
  235.         return E_FAIL;
  236.     }
  237.  
  238.     auto hdr = reinterpret_cast<DDS_HEADER*>( ddsData.get() + sizeof( uint32_t ) );
  239.  
  240.     // Verify header to validate DDS file
  241.     if (hdr->size != sizeof(DDS_HEADER) ||
  242.         hdr->ddspf.size != sizeof(DDS_PIXELFORMAT))
  243.     {
  244.         return E_FAIL;
  245.     }
  246.  
  247.     // Check for DX10 extension
  248.     bool bDXT10Header = false;
  249.     if ((hdr->ddspf.flags & DDS_FOURCC) &&
  250.         (MAKEFOURCC( 'D', 'X', '1', '0' ) == hdr->ddspf.fourCC))
  251.     {
  252.         // Must be long enough for both headers and magic value
  253.         if (FileSize.LowPart < ( sizeof(DDS_HEADER) + sizeof(uint32_t) + sizeof(DDS_HEADER_DXT10) ) )
  254.         {
  255.             return E_FAIL;
  256.         }
  257.  
  258.         bDXT10Header = true;
  259.     }
  260.  
  261.     // setup the pointers in the process request
  262.     *header = hdr;
  263.     ptrdiff_t offset = sizeof( uint32_t ) + sizeof( DDS_HEADER )
  264.                        + (bDXT10Header ? sizeof( DDS_HEADER_DXT10 ) : 0);
  265.     *bitData = ddsData.get() + offset;
  266.     *bitSize = FileSize.LowPart - offset;
  267.  
  268.     return S_OK;
  269. }
  270.  
  271.  
  272. //--------------------------------------------------------------------------------------
  273. // Return the BPP for a particular format
  274. //--------------------------------------------------------------------------------------
  275. static size_t BitsPerPixel( _In_ DXGI_FORMAT fmt )
  276. {
  277.     switch( fmt )
  278.     {
  279.     case DXGI_FORMAT_R32G32B32A32_TYPELESS:
  280.     case DXGI_FORMAT_R32G32B32A32_FLOAT:
  281.     case DXGI_FORMAT_R32G32B32A32_UINT:
  282.     case DXGI_FORMAT_R32G32B32A32_SINT:
  283.         return 128;
  284.  
  285.     case DXGI_FORMAT_R32G32B32_TYPELESS:
  286.     case DXGI_FORMAT_R32G32B32_FLOAT:
  287.     case DXGI_FORMAT_R32G32B32_UINT:
  288.     case DXGI_FORMAT_R32G32B32_SINT:
  289.         return 96;
  290.  
  291.     case DXGI_FORMAT_R16G16B16A16_TYPELESS:
  292.     case DXGI_FORMAT_R16G16B16A16_FLOAT:
  293.     case DXGI_FORMAT_R16G16B16A16_UNORM:
  294.     case DXGI_FORMAT_R16G16B16A16_UINT:
  295.     case DXGI_FORMAT_R16G16B16A16_SNORM:
  296.     case DXGI_FORMAT_R16G16B16A16_SINT:
  297.     case DXGI_FORMAT_R32G32_TYPELESS:
  298.     case DXGI_FORMAT_R32G32_FLOAT:
  299.     case DXGI_FORMAT_R32G32_UINT:
  300.     case DXGI_FORMAT_R32G32_SINT:
  301.     case DXGI_FORMAT_R32G8X24_TYPELESS:
  302.     case DXGI_FORMAT_D32_FLOAT_S8X24_UINT:
  303.     case DXGI_FORMAT_R32_FLOAT_X8X24_TYPELESS:
  304.     case DXGI_FORMAT_X32_TYPELESS_G8X24_UINT:
  305.     case DXGI_FORMAT_Y416:
  306.     case DXGI_FORMAT_Y210:
  307.     case DXGI_FORMAT_Y216:
  308.         return 64;
  309.  
  310.     case DXGI_FORMAT_R10G10B10A2_TYPELESS:
  311.     case DXGI_FORMAT_R10G10B10A2_UNORM:
  312.     case DXGI_FORMAT_R10G10B10A2_UINT:
  313.     case DXGI_FORMAT_R11G11B10_FLOAT:
  314.     case DXGI_FORMAT_R8G8B8A8_TYPELESS:
  315.     case DXGI_FORMAT_R8G8B8A8_UNORM:
  316.     case DXGI_FORMAT_R8G8B8A8_UNORM_SRGB:
  317.     case DXGI_FORMAT_R8G8B8A8_UINT:
  318.     case DXGI_FORMAT_R8G8B8A8_SNORM:
  319.     case DXGI_FORMAT_R8G8B8A8_SINT:
  320.     case DXGI_FORMAT_R16G16_TYPELESS:
  321.     case DXGI_FORMAT_R16G16_FLOAT:
  322.     case DXGI_FORMAT_R16G16_UNORM:
  323.     case DXGI_FORMAT_R16G16_UINT:
  324.     case DXGI_FORMAT_R16G16_SNORM:
  325.     case DXGI_FORMAT_R16G16_SINT:
  326.     case DXGI_FORMAT_R32_TYPELESS:
  327.     case DXGI_FORMAT_D32_FLOAT:
  328.     case DXGI_FORMAT_R32_FLOAT:
  329.     case DXGI_FORMAT_R32_UINT:
  330.     case DXGI_FORMAT_R32_SINT:
  331.     case DXGI_FORMAT_R24G8_TYPELESS:
  332.     case DXGI_FORMAT_D24_UNORM_S8_UINT:
  333.     case DXGI_FORMAT_R24_UNORM_X8_TYPELESS:
  334.     case DXGI_FORMAT_X24_TYPELESS_G8_UINT:
  335.     case DXGI_FORMAT_R9G9B9E5_SHAREDEXP:
  336.     case DXGI_FORMAT_R8G8_B8G8_UNORM:
  337.     case DXGI_FORMAT_G8R8_G8B8_UNORM:
  338.     case DXGI_FORMAT_B8G8R8A8_UNORM:
  339.     case DXGI_FORMAT_B8G8R8X8_UNORM:
  340.     case DXGI_FORMAT_R10G10B10_XR_BIAS_A2_UNORM:
  341.     case DXGI_FORMAT_B8G8R8A8_TYPELESS:
  342.     case DXGI_FORMAT_B8G8R8A8_UNORM_SRGB:
  343.     case DXGI_FORMAT_B8G8R8X8_TYPELESS:
  344.     case DXGI_FORMAT_B8G8R8X8_UNORM_SRGB:
  345.     case DXGI_FORMAT_AYUV:
  346.     case DXGI_FORMAT_Y410:
  347.     case DXGI_FORMAT_YUY2:
  348.         return 32;
  349.  
  350.     case DXGI_FORMAT_P010:
  351.     case DXGI_FORMAT_P016:
  352.         return 24;
  353.  
  354.     case DXGI_FORMAT_R8G8_TYPELESS:
  355.     case DXGI_FORMAT_R8G8_UNORM:
  356.     case DXGI_FORMAT_R8G8_UINT:
  357.     case DXGI_FORMAT_R8G8_SNORM:
  358.     case DXGI_FORMAT_R8G8_SINT:
  359.     case DXGI_FORMAT_R16_TYPELESS:
  360.     case DXGI_FORMAT_R16_FLOAT:
  361.     case DXGI_FORMAT_D16_UNORM:
  362.     case DXGI_FORMAT_R16_UNORM:
  363.     case DXGI_FORMAT_R16_UINT:
  364.     case DXGI_FORMAT_R16_SNORM:
  365.     case DXGI_FORMAT_R16_SINT:
  366.     case DXGI_FORMAT_B5G6R5_UNORM:
  367.     case DXGI_FORMAT_B5G5R5A1_UNORM:
  368.     case DXGI_FORMAT_A8P8:
  369.     case DXGI_FORMAT_B4G4R4A4_UNORM:
  370.         return 16;
  371.  
  372.     case DXGI_FORMAT_NV12:
  373.     case DXGI_FORMAT_420_OPAQUE:
  374.     case DXGI_FORMAT_NV11:
  375.         return 12;
  376.  
  377.     case DXGI_FORMAT_R8_TYPELESS:
  378.     case DXGI_FORMAT_R8_UNORM:
  379.     case DXGI_FORMAT_R8_UINT:
  380.     case DXGI_FORMAT_R8_SNORM:
  381.     case DXGI_FORMAT_R8_SINT:
  382.     case DXGI_FORMAT_A8_UNORM:
  383.     case DXGI_FORMAT_AI44:
  384.     case DXGI_FORMAT_IA44:
  385.     case DXGI_FORMAT_P8:
  386.         return 8;
  387.  
  388.     case DXGI_FORMAT_R1_UNORM:
  389.         return 1;
  390.  
  391.     case DXGI_FORMAT_BC1_TYPELESS:
  392.     case DXGI_FORMAT_BC1_UNORM:
  393.     case DXGI_FORMAT_BC1_UNORM_SRGB:
  394.     case DXGI_FORMAT_BC4_TYPELESS:
  395.     case DXGI_FORMAT_BC4_UNORM:
  396.     case DXGI_FORMAT_BC4_SNORM:
  397.         return 4;
  398.  
  399.     case DXGI_FORMAT_BC2_TYPELESS:
  400.     case DXGI_FORMAT_BC2_UNORM:
  401.     case DXGI_FORMAT_BC2_UNORM_SRGB:
  402.     case DXGI_FORMAT_BC3_TYPELESS:
  403.     case DXGI_FORMAT_BC3_UNORM:
  404.     case DXGI_FORMAT_BC3_UNORM_SRGB:
  405.     case DXGI_FORMAT_BC5_TYPELESS:
  406.     case DXGI_FORMAT_BC5_UNORM:
  407.     case DXGI_FORMAT_BC5_SNORM:
  408.     case DXGI_FORMAT_BC6H_TYPELESS:
  409.     case DXGI_FORMAT_BC6H_UF16:
  410.     case DXGI_FORMAT_BC6H_SF16:
  411.     case DXGI_FORMAT_BC7_TYPELESS:
  412.     case DXGI_FORMAT_BC7_UNORM:
  413.     case DXGI_FORMAT_BC7_UNORM_SRGB:
  414.         return 8;
  415.  
  416.     default:
  417.         return 0;
  418.     }
  419. }
  420.  
  421.  
  422. //--------------------------------------------------------------------------------------
  423. // Get surface information for a particular format
  424. //--------------------------------------------------------------------------------------
  425. static void GetSurfaceInfo( _In_ size_t width,
  426.                             _In_ size_t height,
  427.                             _In_ DXGI_FORMAT fmt,
  428.                             _Out_opt_ size_t* outNumBytes,
  429.                             _Out_opt_ size_t* outRowBytes,
  430.                             _Out_opt_ size_t* outNumRows )
  431. {
  432.     size_t numBytes = 0;
  433.     size_t rowBytes = 0;
  434.     size_t numRows = 0;
  435.  
  436.     bool bc = false;
  437.     bool packed = false;
  438.     bool planar = false;
  439.     size_t bpe = 0;
  440.     switch (fmt)
  441.     {
  442.     case DXGI_FORMAT_BC1_TYPELESS:
  443.     case DXGI_FORMAT_BC1_UNORM:
  444.     case DXGI_FORMAT_BC1_UNORM_SRGB:
  445.     case DXGI_FORMAT_BC4_TYPELESS:
  446.     case DXGI_FORMAT_BC4_UNORM:
  447.     case DXGI_FORMAT_BC4_SNORM:
  448.         bc=true;
  449.         bpe = 8;
  450.         break;
  451.  
  452.     case DXGI_FORMAT_BC2_TYPELESS:
  453.     case DXGI_FORMAT_BC2_UNORM:
  454.     case DXGI_FORMAT_BC2_UNORM_SRGB:
  455.     case DXGI_FORMAT_BC3_TYPELESS:
  456.     case DXGI_FORMAT_BC3_UNORM:
  457.     case DXGI_FORMAT_BC3_UNORM_SRGB:
  458.     case DXGI_FORMAT_BC5_TYPELESS:
  459.     case DXGI_FORMAT_BC5_UNORM:
  460.     case DXGI_FORMAT_BC5_SNORM:
  461.     case DXGI_FORMAT_BC6H_TYPELESS:
  462.     case DXGI_FORMAT_BC6H_UF16:
  463.     case DXGI_FORMAT_BC6H_SF16:
  464.     case DXGI_FORMAT_BC7_TYPELESS:
  465.     case DXGI_FORMAT_BC7_UNORM:
  466.     case DXGI_FORMAT_BC7_UNORM_SRGB:
  467.         bc = true;
  468.         bpe = 16;
  469.         break;
  470.  
  471.     case DXGI_FORMAT_R8G8_B8G8_UNORM:
  472.     case DXGI_FORMAT_G8R8_G8B8_UNORM:
  473.     case DXGI_FORMAT_YUY2:
  474.         packed = true;
  475.         bpe = 4;
  476.         break;
  477.  
  478.     case DXGI_FORMAT_Y210:
  479.     case DXGI_FORMAT_Y216:
  480.         packed = true;
  481.         bpe = 8;
  482.         break;
  483.  
  484.     case DXGI_FORMAT_NV12:
  485.     case DXGI_FORMAT_420_OPAQUE:
  486.         planar = true;
  487.         bpe = 2;
  488.         break;
  489.  
  490.     case DXGI_FORMAT_P010:
  491.     case DXGI_FORMAT_P016:
  492.         planar = true;
  493.         bpe = 4;
  494.         break;
  495.     }
  496.  
  497.     if (bc)
  498.     {
  499.         size_t numBlocksWide = 0;
  500.         if (width > 0)
  501.         {
  502.             numBlocksWide = std::max<size_t>( 1, (width + 3) / 4 );
  503.         }
  504.         size_t numBlocksHigh = 0;
  505.         if (height > 0)
  506.         {
  507.             numBlocksHigh = std::max<size_t>( 1, (height + 3) / 4 );
  508.         }
  509.         rowBytes = numBlocksWide * bpe;
  510.         numRows = numBlocksHigh;
  511.         numBytes = rowBytes * numBlocksHigh;
  512.     }
  513.     else if (packed)
  514.     {
  515.         rowBytes = ( ( width + 1 ) >> 1 ) * bpe;
  516.         numRows = height;
  517.         numBytes = rowBytes * height;
  518.     }
  519.     else if ( fmt == DXGI_FORMAT_NV11 )
  520.     {
  521.         rowBytes = ( ( width + 3 ) >> 2 ) * 4;
  522.         numRows = height * 2; // Direct3D makes this simplifying assumption, although it is larger than the 4:1:1 data
  523.         numBytes = rowBytes * numRows;
  524.     }
  525.     else if (planar)
  526.     {
  527.         rowBytes = ( ( width + 1 ) >> 1 ) * bpe;
  528.         numBytes = ( rowBytes * height ) + ( ( rowBytes * height + 1 ) >> 1 );
  529.         numRows = height + ( ( height + 1 ) >> 1 );
  530.     }
  531.     else
  532.     {
  533.         size_t bpp = BitsPerPixel( fmt );
  534.         rowBytes = ( width * bpp + 7 ) / 8; // round up to nearest byte
  535.         numRows = height;
  536.         numBytes = rowBytes * height;
  537.     }
  538.  
  539.     if (outNumBytes)
  540.     {
  541.         *outNumBytes = numBytes;
  542.     }
  543.     if (outRowBytes)
  544.     {
  545.         *outRowBytes = rowBytes;
  546.     }
  547.     if (outNumRows)
  548.     {
  549.         *outNumRows = numRows;
  550.     }
  551. }
  552.  
  553.  
  554. //--------------------------------------------------------------------------------------
  555. #define ISBITMASK( r,g,b,a ) ( ddpf.RBitMask == r && ddpf.GBitMask == g && ddpf.BBitMask == b && ddpf.ABitMask == a )
  556.  
  557. static DXGI_FORMAT GetDXGIFormat( const DDS_PIXELFORMAT& ddpf )
  558. {
  559.     if (ddpf.flags & DDS_RGB)
  560.     {
  561.         // Note that sRGB formats are written using the "DX10" extended header
  562.  
  563.         switch (ddpf.RGBBitCount)
  564.         {
  565.         case 32:
  566.             if (ISBITMASK(0x000000ff,0x0000ff00,0x00ff0000,0xff000000))
  567.             {
  568.                 return DXGI_FORMAT_R8G8B8A8_UNORM;
  569.             }
  570.  
  571.             if (ISBITMASK(0x00ff0000,0x0000ff00,0x000000ff,0xff000000))
  572.             {
  573.                 return DXGI_FORMAT_B8G8R8A8_UNORM;
  574.             }
  575.  
  576.             if (ISBITMASK(0x00ff0000,0x0000ff00,0x000000ff,0x00000000))
  577.             {
  578.                 return DXGI_FORMAT_B8G8R8X8_UNORM;
  579.             }
  580.  
  581.             // No DXGI format maps to ISBITMASK(0x000000ff,0x0000ff00,0x00ff0000,0x00000000) aka D3DFMT_X8B8G8R8
  582.  
  583.             // Note that many common DDS reader/writers (including D3DX) swap the
  584.             // the RED/BLUE masks for 10:10:10:2 formats. We assume
  585.             // below that the 'backwards' header mask is being used since it is most
  586.             // likely written by D3DX. The more robust solution is to use the 'DX10'
  587.             // header extension and specify the DXGI_FORMAT_R10G10B10A2_UNORM format directly
  588.  
  589.             // For 'correct' writers, this should be 0x000003ff,0x000ffc00,0x3ff00000 for RGB data
  590.             if (ISBITMASK(0x3ff00000,0x000ffc00,0x000003ff,0xc0000000))
  591.             {
  592.                 return DXGI_FORMAT_R10G10B10A2_UNORM;
  593.             }
  594.  
  595.             // No DXGI format maps to ISBITMASK(0x000003ff,0x000ffc00,0x3ff00000,0xc0000000) aka D3DFMT_A2R10G10B10
  596.  
  597.             if (ISBITMASK(0x0000ffff,0xffff0000,0x00000000,0x00000000))
  598.             {
  599.                 return DXGI_FORMAT_R16G16_UNORM;
  600.             }
  601.  
  602.             if (ISBITMASK(0xffffffff,0x00000000,0x00000000,0x00000000))
  603.             {
  604.                 // Only 32-bit color channel format in D3D9 was R32F
  605.                 return DXGI_FORMAT_R32_FLOAT; // D3DX writes this out as a FourCC of 114
  606.             }
  607.             break;
  608.  
  609.         case 24:
  610.             // No 24bpp DXGI formats aka D3DFMT_R8G8B8
  611.             break;
  612.  
  613.         case 16:
  614.             if (ISBITMASK(0x7c00,0x03e0,0x001f,0x8000))
  615.             {
  616.                 return DXGI_FORMAT_B5G5R5A1_UNORM;
  617.             }
  618.             if (ISBITMASK(0xf800,0x07e0,0x001f,0x0000))
  619.             {
  620.                 return DXGI_FORMAT_B5G6R5_UNORM;
  621.             }
  622.  
  623.             // No DXGI format maps to ISBITMASK(0x7c00,0x03e0,0x001f,0x0000) aka D3DFMT_X1R5G5B5
  624.  
  625.             if (ISBITMASK(0x0f00,0x00f0,0x000f,0xf000))
  626.             {
  627.                 return DXGI_FORMAT_B4G4R4A4_UNORM;
  628.             }
  629.  
  630.             // No DXGI format maps to ISBITMASK(0x0f00,0x00f0,0x000f,0x0000) aka D3DFMT_X4R4G4B4
  631.  
  632.             // No 3:3:2, 3:3:2:8, or paletted DXGI formats aka D3DFMT_A8R3G3B2, D3DFMT_R3G3B2, D3DFMT_P8, D3DFMT_A8P8, etc.
  633.             break;
  634.         }
  635.     }
  636.     else if (ddpf.flags & DDS_LUMINANCE)
  637.     {
  638.         if (8 == ddpf.RGBBitCount)
  639.         {
  640.             if (ISBITMASK(0x000000ff,0x00000000,0x00000000,0x00000000))
  641.             {
  642.                 return DXGI_FORMAT_R8_UNORM; // D3DX10/11 writes this out as DX10 extension
  643.             }
  644.  
  645.             // No DXGI format maps to ISBITMASK(0x0f,0x00,0x00,0xf0) aka D3DFMT_A4L4
  646.         }
  647.  
  648.         if (16 == ddpf.RGBBitCount)
  649.         {
  650.             if (ISBITMASK(0x0000ffff,0x00000000,0x00000000,0x00000000))
  651.             {
  652.                 return DXGI_FORMAT_R16_UNORM; // D3DX10/11 writes this out as DX10 extension
  653.             }
  654.             if (ISBITMASK(0x000000ff,0x00000000,0x00000000,0x0000ff00))
  655.             {
  656.                 return DXGI_FORMAT_R8G8_UNORM; // D3DX10/11 writes this out as DX10 extension
  657.             }
  658.         }
  659.     }
  660.     else if (ddpf.flags & DDS_ALPHA)
  661.     {
  662.         if (8 == ddpf.RGBBitCount)
  663.         {
  664.             return DXGI_FORMAT_A8_UNORM;
  665.         }
  666.     }
  667.     else if (ddpf.flags & DDS_FOURCC)
  668.     {
  669.         if (MAKEFOURCC( 'D', 'X', 'T', '1' ) == ddpf.fourCC)
  670.         {
  671.             return DXGI_FORMAT_BC1_UNORM;
  672.         }
  673.         if (MAKEFOURCC( 'D', 'X', 'T', '3' ) == ddpf.fourCC)
  674.         {
  675.             return DXGI_FORMAT_BC2_UNORM;
  676.         }
  677.         if (MAKEFOURCC( 'D', 'X', 'T', '5' ) == ddpf.fourCC)
  678.         {
  679.             return DXGI_FORMAT_BC3_UNORM;
  680.         }
  681.  
  682.         // While pre-multiplied alpha isn't directly supported by the DXGI formats,
  683.         // they are basically the same as these BC formats so they can be mapped
  684.         if (MAKEFOURCC( 'D', 'X', 'T', '2' ) == ddpf.fourCC)
  685.         {
  686.             return DXGI_FORMAT_BC2_UNORM;
  687.         }
  688.         if (MAKEFOURCC( 'D', 'X', 'T', '4' ) == ddpf.fourCC)
  689.         {
  690.             return DXGI_FORMAT_BC3_UNORM;
  691.         }
  692.  
  693.         if (MAKEFOURCC( 'A', 'T', 'I', '1' ) == ddpf.fourCC)
  694.         {
  695.             return DXGI_FORMAT_BC4_UNORM;
  696.         }
  697.         if (MAKEFOURCC( 'B', 'C', '4', 'U' ) == ddpf.fourCC)
  698.         {
  699.             return DXGI_FORMAT_BC4_UNORM;
  700.         }
  701.         if (MAKEFOURCC( 'B', 'C', '4', 'S' ) == ddpf.fourCC)
  702.         {
  703.             return DXGI_FORMAT_BC4_SNORM;
  704.         }
  705.  
  706.         if (MAKEFOURCC( 'A', 'T', 'I', '2' ) == ddpf.fourCC)
  707.         {
  708.             return DXGI_FORMAT_BC5_UNORM;
  709.         }
  710.         if (MAKEFOURCC( 'B', 'C', '5', 'U' ) == ddpf.fourCC)
  711.         {
  712.             return DXGI_FORMAT_BC5_UNORM;
  713.         }
  714.         if (MAKEFOURCC( 'B', 'C', '5', 'S' ) == ddpf.fourCC)
  715.         {
  716.             return DXGI_FORMAT_BC5_SNORM;
  717.         }
  718.  
  719.         // BC6H and BC7 are written using the "DX10" extended header
  720.  
  721.         if (MAKEFOURCC( 'R', 'G', 'B', 'G' ) == ddpf.fourCC)
  722.         {
  723.             return DXGI_FORMAT_R8G8_B8G8_UNORM;
  724.         }
  725.         if (MAKEFOURCC( 'G', 'R', 'G', 'B' ) == ddpf.fourCC)
  726.         {
  727.             return DXGI_FORMAT_G8R8_G8B8_UNORM;
  728.         }
  729.  
  730.         if (MAKEFOURCC('Y','U','Y','2') == ddpf.fourCC)
  731.         {
  732.             return DXGI_FORMAT_YUY2;
  733.         }
  734.  
  735.         // Check for D3DFORMAT enums being set here
  736.         switch( ddpf.fourCC )
  737.         {
  738.         case 36: // D3DFMT_A16B16G16R16
  739.             return DXGI_FORMAT_R16G16B16A16_UNORM;
  740.  
  741.         case 110: // D3DFMT_Q16W16V16U16
  742.             return DXGI_FORMAT_R16G16B16A16_SNORM;
  743.  
  744.         case 111: // D3DFMT_R16F
  745.             return DXGI_FORMAT_R16_FLOAT;
  746.  
  747.         case 112: // D3DFMT_G16R16F
  748.             return DXGI_FORMAT_R16G16_FLOAT;
  749.  
  750.         case 113: // D3DFMT_A16B16G16R16F
  751.             return DXGI_FORMAT_R16G16B16A16_FLOAT;
  752.  
  753.         case 114: // D3DFMT_R32F
  754.             return DXGI_FORMAT_R32_FLOAT;
  755.  
  756.         case 115: // D3DFMT_G32R32F
  757.             return DXGI_FORMAT_R32G32_FLOAT;
  758.  
  759.         case 116: // D3DFMT_A32B32G32R32F
  760.             return DXGI_FORMAT_R32G32B32A32_FLOAT;
  761.         }
  762.     }
  763.  
  764.     return DXGI_FORMAT_UNKNOWN;
  765. }
  766.  
  767.  
  768. //--------------------------------------------------------------------------------------
  769. static DXGI_FORMAT MakeSRGB( _In_ DXGI_FORMAT format )
  770. {
  771.     switch( format )
  772.     {
  773.     case DXGI_FORMAT_R8G8B8A8_UNORM:
  774.         return DXGI_FORMAT_R8G8B8A8_UNORM_SRGB;
  775.  
  776.     case DXGI_FORMAT_BC1_UNORM:
  777.         return DXGI_FORMAT_BC1_UNORM_SRGB;
  778.  
  779.     case DXGI_FORMAT_BC2_UNORM:
  780.         return DXGI_FORMAT_BC2_UNORM_SRGB;
  781.  
  782.     case DXGI_FORMAT_BC3_UNORM:
  783.         return DXGI_FORMAT_BC3_UNORM_SRGB;
  784.  
  785.     case DXGI_FORMAT_B8G8R8A8_UNORM:
  786.         return DXGI_FORMAT_B8G8R8A8_UNORM_SRGB;
  787.  
  788.     case DXGI_FORMAT_B8G8R8X8_UNORM:
  789.         return DXGI_FORMAT_B8G8R8X8_UNORM_SRGB;
  790.  
  791.     case DXGI_FORMAT_BC7_UNORM:
  792.         return DXGI_FORMAT_BC7_UNORM_SRGB;
  793.  
  794.     default:
  795.         return format;
  796.     }
  797. }
  798.  
  799.  
  800. //--------------------------------------------------------------------------------------
  801. static HRESULT FillInitData( _In_ size_t width,
  802.                              _In_ size_t height,
  803.                              _In_ size_t depth,
  804.                              _In_ size_t mipCount,
  805.                              _In_ size_t arraySize,
  806.                              _In_ DXGI_FORMAT format,
  807.                              _In_ size_t maxsize,
  808.                              _In_ size_t bitSize,
  809.                              _In_reads_bytes_(bitSize) const uint8_t* bitData,
  810.                              _Out_ size_t& twidth,
  811.                              _Out_ size_t& theight,
  812.                              _Out_ size_t& tdepth,
  813.                              _Out_ size_t& skipMip,
  814.                              _Out_writes_(mipCount*arraySize) D3D11_SUBRESOURCE_DATA* initData )
  815. {
  816.     if ( !bitData || !initData )
  817.     {
  818.         return E_POINTER;
  819.     }
  820.  
  821.     skipMip = 0;
  822.     twidth = 0;
  823.     theight = 0;
  824.     tdepth = 0;
  825.  
  826.     size_t NumBytes = 0;
  827.     size_t RowBytes = 0;
  828.     const uint8_t* pSrcBits = bitData;
  829.     const uint8_t* pEndBits = bitData + bitSize;
  830.  
  831.     size_t index = 0;
  832.     for( size_t j = 0; j < arraySize; j++ )
  833.     {
  834.         size_t w = width;
  835.         size_t h = height;
  836.         size_t d = depth;
  837.         for( size_t i = 0; i < mipCount; i++ )
  838.         {
  839.             GetSurfaceInfo( w,
  840.                             h,
  841.                             format,
  842.                             &NumBytes,
  843.                             &RowBytes,
  844.                             nullptr
  845.                           );
  846.  
  847.             if ( (mipCount <= 1) || !maxsize || (w <= maxsize && h <= maxsize && d <= maxsize) )
  848.             {
  849.                 if ( !twidth )
  850.                 {
  851.                     twidth = w;
  852.                     theight = h;
  853.                     tdepth = d;
  854.                 }
  855.  
  856.                 assert(index < mipCount * arraySize);
  857.                 _Analysis_assume_(index < mipCount * arraySize);
  858.                 initData[index].pSysMem = ( const void* )pSrcBits;
  859.                 initData[index].SysMemPitch = static_cast<UINT>( RowBytes );
  860.                 initData[index].SysMemSlicePitch = static_cast<UINT>( NumBytes );
  861.                 ++index;
  862.             }
  863.             else if ( !j )
  864.             {
  865.                 // Count number of skipped mipmaps (first item only)
  866.                 ++skipMip;
  867.             }
  868.  
  869.             if (pSrcBits + (NumBytes*d) > pEndBits)
  870.             {
  871.                 return HRESULT_FROM_WIN32( ERROR_HANDLE_EOF );
  872.             }
  873.   
  874.             pSrcBits += NumBytes * d;
  875.  
  876.             w = w >> 1;
  877.             h = h >> 1;
  878.             d = d >> 1;
  879.             if (w == 0)
  880.             {
  881.                 w = 1;
  882.             }
  883.             if (h == 0)
  884.             {
  885.                 h = 1;
  886.             }
  887.             if (d == 0)
  888.             {
  889.                 d = 1;
  890.             }
  891.         }
  892.     }
  893.  
  894.     return (index > 0) ? S_OK : E_FAIL;
  895. }
  896.  
  897. static HRESULT FillInitData12(_In_ size_t width,
  898.     _In_ size_t height,
  899.     _In_ size_t depth,
  900.     _In_ size_t mipCount,
  901.     _In_ size_t arraySize,
  902.     _In_ DXGI_FORMAT format,
  903.     _In_ size_t maxsize,
  904.     _In_ size_t bitSize,
  905.     _In_reads_bytes_(bitSize) const uint8_t* bitData,
  906.     _Out_ size_t& twidth,
  907.     _Out_ size_t& theight,
  908.     _Out_ size_t& tdepth,
  909.     _Out_ size_t& skipMip,
  910.     _Out_writes_(mipCount*arraySize) D3D12_SUBRESOURCE_DATA* initData
  911.     )
  912. {
  913.     if (!bitData || !initData)
  914.     {
  915.         return E_POINTER;
  916.     }
  917.  
  918.     skipMip = 0;
  919.     twidth = 0;
  920.     theight = 0;
  921.     tdepth = 0;
  922.  
  923.     size_t NumBytes = 0;
  924.     size_t RowBytes = 0;
  925.     const uint8_t* pSrcBits = bitData;
  926.     const uint8_t* pEndBits = bitData + bitSize;
  927.  
  928.     size_t index = 0;
  929.     for (size_t j = 0; j < arraySize; j++)
  930.     {
  931.         size_t w = width;
  932.         size_t h = height;
  933.         size_t d = depth;
  934.         for (size_t i = 0; i < mipCount; i++)
  935.         {
  936.             GetSurfaceInfo(w,
  937.                 h,
  938.                 format,
  939.                 &NumBytes,
  940.                 &RowBytes,
  941.                 nullptr
  942.                 );
  943.  
  944.             if ((mipCount <= 1) || !maxsize || (w <= maxsize && h <= maxsize && d <= maxsize))
  945.             {
  946.                 if (!twidth)
  947.                 {
  948.                     twidth = w;
  949.                     theight = h;
  950.                     tdepth = d;
  951.                 }
  952.  
  953.                 assert(index < mipCount * arraySize);
  954.                 _Analysis_assume_(index < mipCount * arraySize);
  955.                 initData[index]./*pSysMem*/pData = (const void*)pSrcBits;
  956.                 initData[index]./*SysMemPitch*/RowPitch = static_cast<UINT>(RowBytes);
  957.                 initData[index]./*SysMemSlicePitch*/SlicePitch = static_cast<UINT>(NumBytes);
  958.                 ++index;
  959.             }
  960.             else if (!j)
  961.             {
  962.                 // Count number of skipped mipmaps (first item only)
  963.                 ++skipMip;
  964.             }
  965.  
  966.             if (pSrcBits + (NumBytes*d) > pEndBits)
  967.             {
  968.                 return HRESULT_FROM_WIN32(ERROR_HANDLE_EOF);
  969.             }
  970.  
  971.             pSrcBits += NumBytes * d;
  972.  
  973.             w = w >> 1;
  974.             h = h >> 1;
  975.             d = d >> 1;
  976.             if (w == 0)
  977.             {
  978.                 w = 1;
  979.             }
  980.             if (h == 0)
  981.             {
  982.                 h = 1;
  983.             }
  984.             if (d == 0)
  985.             {
  986.                 d = 1;
  987.             }
  988.         }
  989.     }
  990.  
  991.     return (index > 0) ? S_OK : E_FAIL;
  992. }
  993.  
  994. //--------------------------------------------------------------------------------------
  995. static HRESULT CreateD3DResources( _In_ ID3D11Device* d3dDevice,
  996.                                    _In_ uint32_t resDim,
  997.                                    _In_ size_t width,
  998.                                    _In_ size_t height,
  999.                                    _In_ size_t depth,
  1000.                                    _In_ size_t mipCount,
  1001.                                    _In_ size_t arraySize,
  1002.                                    _In_ DXGI_FORMAT format,
  1003.                                    _In_ D3D11_USAGE usage,
  1004.                                    _In_ unsigned int bindFlags,
  1005.                                    _In_ unsigned int cpuAccessFlags,
  1006.                                    _In_ unsigned int miscFlags,
  1007.                                    _In_ bool forceSRGB,
  1008.                                    _In_ bool isCubeMap,
  1009.                                    _In_reads_opt_(mipCount*arraySize) D3D11_SUBRESOURCE_DATA* initData,
  1010.                                    _Outptr_opt_ ID3D11Resource** texture,
  1011.                                    _Outptr_opt_ ID3D11ShaderResourceView** textureView )
  1012. {
  1013.     if ( !d3dDevice )
  1014.         return E_POINTER;
  1015.  
  1016.     HRESULT hr = E_FAIL;
  1017.  
  1018.     if ( forceSRGB )
  1019.     {
  1020.         format = MakeSRGB( format );
  1021.     }
  1022.  
  1023.     switch ( resDim ) 
  1024.     {
  1025.         case D3D11_RESOURCE_DIMENSION_TEXTURE1D:
  1026.             {
  1027.                 D3D11_TEXTURE1D_DESC desc;
  1028.                 desc.Width = static_cast<UINT>( width ); 
  1029.                 desc.MipLevels = static_cast<UINT>( mipCount );
  1030.                 desc.ArraySize = static_cast<UINT>( arraySize );
  1031.                 desc.Format = format;
  1032.                 desc.Usage = usage;
  1033.                 desc.BindFlags = bindFlags;
  1034.                 desc.CPUAccessFlags = cpuAccessFlags;
  1035.                 desc.MiscFlags = miscFlags & ~D3D11_RESOURCE_MISC_TEXTURECUBE;
  1036.  
  1037.                 ID3D11Texture1D* tex = nullptr;
  1038.                 hr = d3dDevice->CreateTexture1D( &desc,
  1039.                                                  initData,
  1040.                                                  &tex
  1041.                                                );
  1042.                 if (SUCCEEDED( hr ) && tex != 0)
  1043.                 {
  1044.                     if (textureView != 0)
  1045.                     {
  1046.                         D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc;
  1047.                         memset( &SRVDesc, 0, sizeof( SRVDesc ) );
  1048.                         SRVDesc.Format = format;
  1049.  
  1050.                         if (arraySize > 1)
  1051.                         {
  1052.                             SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE1DARRAY;
  1053.                             SRVDesc.Texture1DArray.MipLevels = (!mipCount) ? -1 : desc.MipLevels;
  1054.                             SRVDesc.Texture1DArray.ArraySize = static_cast<UINT>( arraySize );
  1055.                         }
  1056.                         else
  1057.                         {
  1058.                             SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE1D;
  1059.                             SRVDesc.Texture1D.MipLevels = (!mipCount) ? -1 : desc.MipLevels;
  1060.                         }
  1061.  
  1062.                         hr = d3dDevice->CreateShaderResourceView( tex,
  1063.                                                                   &SRVDesc,
  1064.                                                                   textureView
  1065.                                                                 );
  1066.                         if ( FAILED(hr) )
  1067.                         {
  1068.                             tex->Release();
  1069.                             return hr;
  1070.                         }
  1071.                     }
  1072.  
  1073.                     if (texture != 0)
  1074.                     {
  1075.                         *texture = tex;
  1076.                     }
  1077.                     else
  1078.                     {
  1079.                         SetDebugObjectName(tex, "DDSTextureLoader");
  1080.                         tex->Release();
  1081.                     }
  1082.                 }
  1083.             }
  1084.            break;
  1085.  
  1086.         case D3D11_RESOURCE_DIMENSION_TEXTURE2D:
  1087.             {
  1088.                 D3D11_TEXTURE2D_DESC desc;
  1089.                 desc.Width = static_cast<UINT>( width );
  1090.                 desc.Height = static_cast<UINT>( height );
  1091.                 desc.MipLevels = static_cast<UINT>( mipCount );
  1092.                 desc.ArraySize = static_cast<UINT>( arraySize );
  1093.                 desc.Format = format;
  1094.                 desc.SampleDesc.Count = 1;
  1095.                 desc.SampleDesc.Quality = 0;
  1096.                 desc.Usage = usage;
  1097.                 desc.BindFlags = bindFlags;
  1098.                 desc.CPUAccessFlags = cpuAccessFlags;
  1099.                 if ( isCubeMap )
  1100.                 {
  1101.                     desc.MiscFlags = miscFlags | D3D11_RESOURCE_MISC_TEXTURECUBE;
  1102.                 }
  1103.                 else
  1104.                 {
  1105.                     desc.MiscFlags = miscFlags & ~D3D11_RESOURCE_MISC_TEXTURECUBE;
  1106.                 }
  1107.  
  1108.                 ID3D11Texture2D* tex = nullptr;
  1109.                 hr = d3dDevice->CreateTexture2D( &desc,
  1110.                                                  initData,
  1111.                                                  &tex
  1112.                                                );
  1113.                 if (SUCCEEDED( hr ) && tex != 0)
  1114.                 {
  1115.                     if (textureView != 0)
  1116.                     {
  1117.                         D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc;
  1118.                         memset( &SRVDesc, 0, sizeof( SRVDesc ) );
  1119.                         SRVDesc.Format = format;
  1120.  
  1121.                         if ( isCubeMap )
  1122.                         {
  1123.                             if (arraySize > 6)
  1124.                             {
  1125.                                 SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBEARRAY;
  1126.                                 SRVDesc.TextureCubeArray.MipLevels = (!mipCount) ? -1 : desc.MipLevels;
  1127.  
  1128.                                 // Earlier we set arraySize to (NumCubes * 6)
  1129.                                 SRVDesc.TextureCubeArray.NumCubes = static_cast<UINT>( arraySize / 6 );
  1130.                             }
  1131.                             else
  1132.                             {
  1133.                                 SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBE;
  1134.                                 SRVDesc.TextureCube.MipLevels = (!mipCount) ? -1 : desc.MipLevels;
  1135.                             }
  1136.                         }
  1137.                         else if (arraySize > 1)
  1138.                         {
  1139.                             SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2DARRAY;
  1140.                             SRVDesc.Texture2DArray.MipLevels = (!mipCount) ? -1 : desc.MipLevels;
  1141.                             SRVDesc.Texture2DArray.ArraySize = static_cast<UINT>( arraySize );
  1142.                         }
  1143.                         else
  1144.                         {
  1145.                             SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
  1146.                             SRVDesc.Texture2D.MipLevels = (!mipCount) ? -1 : desc.MipLevels;
  1147.                         }
  1148.  
  1149.                         hr = d3dDevice->CreateShaderResourceView( tex,
  1150.                                                                   &SRVDesc,
  1151.                                                                   textureView
  1152.                                                                 );
  1153.                         if ( FAILED(hr) )
  1154.                         {
  1155.                             tex->Release();
  1156.                             return hr;
  1157.                         }
  1158.                     }
  1159.  
  1160.                     if (texture != 0)
  1161.                     {
  1162.                         *texture = tex;
  1163.                     }
  1164.                     else
  1165.                     {
  1166.                         SetDebugObjectName(tex, "DDSTextureLoader");
  1167.                         tex->Release();
  1168.                     }
  1169.                 }
  1170.             }
  1171.             break;
  1172.  
  1173.         case D3D11_RESOURCE_DIMENSION_TEXTURE3D:
  1174.             {
  1175.                 D3D11_TEXTURE3D_DESC desc;
  1176.                 desc.Width = static_cast<UINT>( width );
  1177.                 desc.Height = static_cast<UINT>( height );
  1178.                 desc.Depth = static_cast<UINT>( depth );
  1179.                 desc.MipLevels = static_cast<UINT>( mipCount );
  1180.                 desc.Format = format;
  1181.                 desc.Usage = usage;
  1182.                 desc.BindFlags = bindFlags;
  1183.                 desc.CPUAccessFlags = cpuAccessFlags;
  1184.                 desc.MiscFlags = miscFlags & ~D3D11_RESOURCE_MISC_TEXTURECUBE;
  1185.  
  1186.                 ID3D11Texture3D* tex = nullptr;
  1187.                 hr = d3dDevice->CreateTexture3D( &desc,
  1188.                                                  initData,
  1189.                                                  &tex
  1190.                                                );
  1191.                 if (SUCCEEDED( hr ) && tex != 0)
  1192.                 {
  1193.                     if (textureView != 0)
  1194.                     {
  1195.                         D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc;
  1196.                         memset( &SRVDesc, 0, sizeof( SRVDesc ) );
  1197.                         SRVDesc.Format = format;
  1198.  
  1199.                         SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE3D;
  1200.                         SRVDesc.Texture3D.MipLevels = (!mipCount) ? -1 : desc.MipLevels;
  1201.  
  1202.                         hr = d3dDevice->CreateShaderResourceView( tex,
  1203.                                                                   &SRVDesc,
  1204.                                                                   textureView
  1205.                                                                 );
  1206.                         if ( FAILED(hr) )
  1207.                         {
  1208.                             tex->Release();
  1209.                             return hr;
  1210.                         }
  1211.                     }
  1212.  
  1213.                     if (texture != 0)
  1214.                     {
  1215.                         *texture = tex;
  1216.                     }
  1217.                     else
  1218.                     {
  1219.                         SetDebugObjectName(tex, "DDSTextureLoader");
  1220.                         tex->Release();
  1221.                     }
  1222.                 }
  1223.             }
  1224.             break; 
  1225.     }
  1226.  
  1227.     return hr;
  1228. }
  1229.  
  1230. static HRESULT CreateD3DResources12(
  1231.     ID3D12Device* device,
  1232.     ID3D12GraphicsCommandList* cmdList,
  1233.     _In_ uint32_t resDim,
  1234.     _In_ size_t width,
  1235.     _In_ size_t height,
  1236.     _In_ size_t depth,
  1237.     _In_ size_t mipCount,
  1238.     _In_ size_t arraySize,
  1239.     _In_ DXGI_FORMAT format,
  1240.     _In_ bool forceSRGB,
  1241.     _In_ bool isCubeMap,
  1242.     _In_reads_opt_(mipCount*arraySize) D3D12_SUBRESOURCE_DATA* initData,
  1243.     ComPtr<ID3D12Resource>& texture,
  1244.     ComPtr<ID3D12Resource>& textureUploadHeap
  1245.     )
  1246. {
  1247.     if (device == nullptr)
  1248.         return E_POINTER;
  1249.  
  1250.     if (forceSRGB)
  1251.         format = MakeSRGB(format);
  1252.  
  1253.     HRESULT hr = E_FAIL;
  1254.     switch (resDim)
  1255.     {
  1256.     case D3D12_RESOURCE_DIMENSION_TEXTURE2D:
  1257.     {
  1258.         D3D12_RESOURCE_DESC texDesc;
  1259.         ZeroMemory(&texDesc, sizeof(D3D12_RESOURCE_DESC));
  1260.         texDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
  1261.         texDesc.Alignment = 0;
  1262.         texDesc.Width = width;
  1263.         texDesc.Height = (uint32_t)height;
  1264.         texDesc.DepthOrArraySize = (depth > 1) ? (uint16_t)depth : (uint16_t)arraySize;
  1265.         texDesc.MipLevels = (uint16_t)mipCount;
  1266.         texDesc.Format = format;
  1267.         texDesc.SampleDesc.Count = 1;
  1268.         texDesc.SampleDesc.Quality = 0;
  1269.         texDesc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN;
  1270.         texDesc.Flags = D3D12_RESOURCE_FLAG_NONE;
  1271.  
  1272.         hr = device->CreateCommittedResource(
  1273.             &CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT),
  1274.             D3D12_HEAP_FLAG_NONE,
  1275.             &texDesc,
  1276.             D3D12_RESOURCE_STATE_COMMON,
  1277.             nullptr,
  1278.             IID_PPV_ARGS(&texture)
  1279.             );
  1280.  
  1281.         if (FAILED(hr))
  1282.         {
  1283.             texture = nullptr;
  1284.             return hr;
  1285.         }
  1286.         else
  1287.         {
  1288.             const UINT num2DSubresources = texDesc.DepthOrArraySize * texDesc.MipLevels;
  1289.             const UINT64 uploadBufferSize = GetRequiredIntermediateSize(texture.Get(), 0, num2DSubresources);
  1290.  
  1291.             hr = device->CreateCommittedResource(
  1292.                 &CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD),
  1293.                 D3D12_HEAP_FLAG_NONE,
  1294.                 &CD3DX12_RESOURCE_DESC::Buffer(uploadBufferSize),
  1295.                 D3D12_RESOURCE_STATE_GENERIC_READ,
  1296.                 nullptr,
  1297.                 IID_PPV_ARGS(&textureUploadHeap));
  1298.             if (FAILED(hr))
  1299.             {
  1300.                 texture = nullptr;
  1301.                 return hr;
  1302.             }
  1303.             else
  1304.             {
  1305.                 cmdList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(texture.Get(),
  1306.                     D3D12_RESOURCE_STATE_COMMON, D3D12_RESOURCE_STATE_COPY_DEST));
  1307.  
  1308.                 // Use Heap-allocating UpdateSubresources implementation for variable number of subresources (which is the case for textures).
  1309.                 UpdateSubresources(cmdList, texture.Get(), textureUploadHeap.Get(), 0, 0, num2DSubresources, initData);
  1310.  
  1311.                 cmdList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(texture.Get(),
  1312.                     D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE));
  1313.             }
  1314.         }
  1315.     } break;
  1316.     }
  1317.  
  1318.     return hr;
  1319. }
  1320.  
  1321.  
  1322. //--------------------------------------------------------------------------------------
  1323. static HRESULT CreateTextureFromDDS( _In_ ID3D11Device* d3dDevice,
  1324.                                      _In_opt_ ID3D11DeviceContext* d3dContext,
  1325.                                      _In_ const DDS_HEADER* header,
  1326.                                      _In_reads_bytes_(bitSize) const uint8_t* bitData,
  1327.                                      _In_ size_t bitSize,
  1328.                                      _In_ size_t maxsize,
  1329.                                      _In_ D3D11_USAGE usage,
  1330.                                      _In_ unsigned int bindFlags,
  1331.                                      _In_ unsigned int cpuAccessFlags,
  1332.                                      _In_ unsigned int miscFlags,
  1333.                                      _In_ bool forceSRGB,
  1334.                                      _Outptr_opt_ ID3D11Resource** texture,
  1335.                                      _Outptr_opt_ ID3D11ShaderResourceView** textureView )
  1336. {
  1337.     HRESULT hr = S_OK;
  1338.  
  1339.     UINT width = header->width;
  1340.     UINT height = header->height;
  1341.     UINT depth = header->depth;
  1342.  
  1343.     uint32_t resDim = D3D11_RESOURCE_DIMENSION_UNKNOWN;
  1344.     UINT arraySize = 1;
  1345.     DXGI_FORMAT format = DXGI_FORMAT_UNKNOWN;
  1346.     bool isCubeMap = false;
  1347.  
  1348.     size_t mipCount = header->mipMapCount;
  1349.     if (0 == mipCount)
  1350.     {
  1351.         mipCount = 1;
  1352.     }
  1353.  
  1354.     if ((header->ddspf.flags & DDS_FOURCC) &&
  1355.         (MAKEFOURCC( 'D', 'X', '1', '0' ) == header->ddspf.fourCC ))
  1356.     {
  1357.         auto d3d10ext = reinterpret_cast<const DDS_HEADER_DXT10*>( (const char*)header + sizeof(DDS_HEADER) );
  1358.  
  1359.         arraySize = d3d10ext->arraySize;
  1360.         if (arraySize == 0)
  1361.         {
  1362.            return HRESULT_FROM_WIN32( ERROR_INVALID_DATA );
  1363.         }
  1364.  
  1365.         switch( d3d10ext->dxgiFormat )
  1366.         {
  1367.         case DXGI_FORMAT_AI44:
  1368.         case DXGI_FORMAT_IA44:
  1369.         case DXGI_FORMAT_P8:
  1370.         case DXGI_FORMAT_A8P8:
  1371.             return HRESULT_FROM_WIN32( ERROR_NOT_SUPPORTED );
  1372.  
  1373.         default:
  1374.             if ( BitsPerPixel( d3d10ext->dxgiFormat ) == 0 )
  1375.             {
  1376.                 return HRESULT_FROM_WIN32( ERROR_NOT_SUPPORTED );
  1377.             }
  1378.         }
  1379.            
  1380.         format = d3d10ext->dxgiFormat;
  1381.  
  1382.         switch ( d3d10ext->resourceDimension )
  1383.         {
  1384.         case D3D11_RESOURCE_DIMENSION_TEXTURE1D:
  1385.             // D3DX writes 1D textures with a fixed Height of 1
  1386.             if ((header->flags & DDS_HEIGHT) && height != 1)
  1387.             {
  1388.                 return HRESULT_FROM_WIN32( ERROR_INVALID_DATA );
  1389.             }
  1390.             height = depth = 1;
  1391.             break;
  1392.  
  1393.         case D3D11_RESOURCE_DIMENSION_TEXTURE2D:
  1394.             if (d3d10ext->miscFlag & D3D11_RESOURCE_MISC_TEXTURECUBE)
  1395.             {
  1396.                 arraySize *= 6;
  1397.                 isCubeMap = true;
  1398.             }
  1399.             depth = 1;
  1400.             break;
  1401.  
  1402.         case D3D11_RESOURCE_DIMENSION_TEXTURE3D:
  1403.             if (!(header->flags & DDS_HEADER_FLAGS_VOLUME))
  1404.             {
  1405.                 return HRESULT_FROM_WIN32( ERROR_INVALID_DATA );
  1406.             }
  1407.  
  1408.             if (arraySize > 1)
  1409.             {
  1410.                 return HRESULT_FROM_WIN32( ERROR_NOT_SUPPORTED );
  1411.             }
  1412.             break;
  1413.  
  1414.         default:
  1415.             return HRESULT_FROM_WIN32( ERROR_NOT_SUPPORTED );
  1416.         }
  1417.  
  1418.         resDim = d3d10ext->resourceDimension;
  1419.     }
  1420.     else
  1421.     {
  1422.         format = GetDXGIFormat( header->ddspf );
  1423.  
  1424.         if (format == DXGI_FORMAT_UNKNOWN)
  1425.         {
  1426.            return HRESULT_FROM_WIN32( ERROR_NOT_SUPPORTED );
  1427.         }
  1428.  
  1429.         if (header->flags & DDS_HEADER_FLAGS_VOLUME)
  1430.         {
  1431.             resDim = D3D11_RESOURCE_DIMENSION_TEXTURE3D;
  1432.         }
  1433.         else 
  1434.         {
  1435.             if (header->caps2 & DDS_CUBEMAP)
  1436.             {
  1437.                 // We require all six faces to be defined
  1438.                 if ((header->caps2 & DDS_CUBEMAP_ALLFACES ) != DDS_CUBEMAP_ALLFACES)
  1439.                 {
  1440.                     return HRESULT_FROM_WIN32( ERROR_NOT_SUPPORTED );
  1441.                 }
  1442.  
  1443.                 arraySize = 6;
  1444.                 isCubeMap = true;
  1445.             }
  1446.  
  1447.             depth = 1;
  1448.             resDim = D3D11_RESOURCE_DIMENSION_TEXTURE2D;
  1449.  
  1450.             // Note there's no way for a legacy Direct3D 9 DDS to express a '1D' texture
  1451.         }
  1452.  
  1453.         assert( BitsPerPixel( format ) != 0 );
  1454.     }
  1455.  
  1456.     // Bound sizes (for security purposes we don't trust DDS file metadata larger than the D3D 11.x hardware requirements)
  1457.     if (mipCount > D3D11_REQ_MIP_LEVELS)
  1458.     {
  1459.         return HRESULT_FROM_WIN32( ERROR_NOT_SUPPORTED );
  1460.     }
  1461.  
  1462.     switch ( resDim )
  1463.     {
  1464.     case D3D11_RESOURCE_DIMENSION_TEXTURE1D:
  1465.         if ((arraySize > D3D11_REQ_TEXTURE1D_ARRAY_AXIS_DIMENSION) ||
  1466.             (width > D3D11_REQ_TEXTURE1D_U_DIMENSION) )
  1467.         {
  1468.             return HRESULT_FROM_WIN32( ERROR_NOT_SUPPORTED );
  1469.         }
  1470.         break;
  1471.  
  1472.     case D3D11_RESOURCE_DIMENSION_TEXTURE2D:
  1473.         if ( isCubeMap )
  1474.         {
  1475.             // This is the right bound because we set arraySize to (NumCubes*6) above
  1476.             if ((arraySize > D3D11_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION) ||
  1477.                 (width > D3D11_REQ_TEXTURECUBE_DIMENSION) ||
  1478.                 (height > D3D11_REQ_TEXTURECUBE_DIMENSION))
  1479.             {
  1480.                 return HRESULT_FROM_WIN32( ERROR_NOT_SUPPORTED );
  1481.             }
  1482.         }
  1483.         else if ((arraySize > D3D11_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION) ||
  1484.                     (width > D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION) ||
  1485.                     (height > D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION))
  1486.         {
  1487.             return HRESULT_FROM_WIN32( ERROR_NOT_SUPPORTED );
  1488.         }
  1489.         break;
  1490.  
  1491.     case D3D11_RESOURCE_DIMENSION_TEXTURE3D:
  1492.         if ((arraySize > 1) ||
  1493.             (width > D3D11_REQ_TEXTURE3D_U_V_OR_W_DIMENSION) ||
  1494.             (height > D3D11_REQ_TEXTURE3D_U_V_OR_W_DIMENSION) ||
  1495.             (depth > D3D11_REQ_TEXTURE3D_U_V_OR_W_DIMENSION) )
  1496.         {
  1497.             return HRESULT_FROM_WIN32( ERROR_NOT_SUPPORTED );
  1498.         }
  1499.         break;
  1500.  
  1501.     default:
  1502.         return HRESULT_FROM_WIN32( ERROR_NOT_SUPPORTED );
  1503.     }
  1504.  
  1505.     bool autogen = false;
  1506.     if ( mipCount == 1 && d3dContext != 0 && textureView != 0 ) // Must have context and shader-view to auto generate mipmaps
  1507.     {
  1508.         // See if format is supported for auto-gen mipmaps (varies by feature level)
  1509.         UINT fmtSupport = 0;
  1510.         hr = d3dDevice->CheckFormatSupport( format, &fmtSupport );
  1511.         if ( SUCCEEDED(hr) && ( fmtSupport & D3D11_FORMAT_SUPPORT_MIP_AUTOGEN ) )
  1512.         {
  1513.             // 10level9 feature levels do not support auto-gen mipgen for volume textures
  1514.             if ( ( resDim != D3D11_RESOURCE_DIMENSION_TEXTURE3D )
  1515.                  || ( d3dDevice->GetFeatureLevel() >= D3D_FEATURE_LEVEL_10_0 ) )
  1516.             {
  1517.                 autogen = true;
  1518.             }
  1519.         }
  1520.     }
  1521.  
  1522.     if ( autogen )
  1523.     {
  1524.         // Create texture with auto-generated mipmaps
  1525.         ID3D11Resource* tex = nullptr;
  1526.         hr = CreateD3DResources( d3dDevice, resDim, width, height, depth, 0, arraySize,
  1527.                                  format, usage,
  1528.                                  bindFlags | D3D11_BIND_RENDER_TARGET,
  1529.                                  cpuAccessFlags,
  1530.                                  miscFlags | D3D11_RESOURCE_MISC_GENERATE_MIPS, forceSRGB,
  1531.                                  isCubeMap, nullptr, &tex, textureView );
  1532.         if ( SUCCEEDED(hr) )
  1533.         {
  1534.             size_t numBytes = 0;
  1535.             size_t rowBytes = 0;
  1536.             GetSurfaceInfo( width, height, format, &numBytes, &rowBytes, nullptr );
  1537.  
  1538.             if ( numBytes > bitSize )
  1539.             {
  1540.                 (*textureView)->Release();
  1541.                 *textureView = nullptr;
  1542.                 tex->Release();
  1543.                 return HRESULT_FROM_WIN32( ERROR_HANDLE_EOF );
  1544.             }
  1545.  
  1546.             D3D11_SHADER_RESOURCE_VIEW_DESC desc;
  1547.             (*textureView)->GetDesc( &desc );
  1548.  
  1549.             UINT mipLevels = 1;
  1550.  
  1551.             switch( desc.ViewDimension )
  1552.             {
  1553.             case D3D_SRV_DIMENSION_TEXTURE1D:       mipLevels = desc.Texture1D.MipLevels; break;
  1554.             case D3D_SRV_DIMENSION_TEXTURE1DARRAY:  mipLevels = desc.Texture1DArray.MipLevels; break;
  1555.             case D3D_SRV_DIMENSION_TEXTURE2D:       mipLevels = desc.Texture2D.MipLevels; break;
  1556.             case D3D_SRV_DIMENSION_TEXTURE2DARRAY:  mipLevels = desc.Texture2DArray.MipLevels; break;
  1557.             case D3D_SRV_DIMENSION_TEXTURECUBE:     mipLevels = desc.TextureCube.MipLevels; break;
  1558.             case D3D_SRV_DIMENSION_TEXTURECUBEARRAY:mipLevels = desc.TextureCubeArray.MipLevels; break;
  1559.             case D3D_SRV_DIMENSION_TEXTURE3D:       mipLevels = desc.Texture3D.MipLevels; break;
  1560.             default:
  1561.                 (*textureView)->Release();
  1562.                 *textureView = nullptr;
  1563.                 tex->Release();
  1564.                 return E_UNEXPECTED;
  1565.             }
  1566.  
  1567.             if ( arraySize > 1 )
  1568.             {
  1569.                 const uint8_t* pSrcBits = bitData;
  1570.                 const uint8_t* pEndBits = bitData + bitSize;
  1571.                 for( UINT item = 0; item < arraySize; ++item )
  1572.                 {
  1573.                     if ( (pSrcBits + numBytes) > pEndBits )
  1574.                     {
  1575.                         (*textureView)->Release();
  1576.                         *textureView = nullptr;
  1577.                         tex->Release();
  1578.                         return HRESULT_FROM_WIN32( ERROR_HANDLE_EOF );
  1579.                     }
  1580.  
  1581.                     UINT res = D3D11CalcSubresource( 0, item, mipLevels );
  1582.                     d3dContext->UpdateSubresource( tex, res, nullptr, pSrcBits, static_cast<UINT>(rowBytes), static_cast<UINT>(numBytes) );
  1583.                     pSrcBits += numBytes;
  1584.                 }
  1585.             }
  1586.             else
  1587.             {
  1588.                 d3dContext->UpdateSubresource( tex, 0, nullptr, bitData, static_cast<UINT>(rowBytes), static_cast<UINT>(numBytes) );
  1589.             }
  1590.  
  1591.             d3dContext->GenerateMips( *textureView );
  1592.  
  1593.             if ( texture )
  1594.             {
  1595.                 *texture = tex;
  1596.             }
  1597.             else
  1598.             {
  1599.                 tex->Release();
  1600.             }
  1601.         }
  1602.     }
  1603.     else
  1604.     {
  1605.         // Create the texture
  1606.         std::unique_ptr<D3D11_SUBRESOURCE_DATA[]> initData( new (std::nothrow) D3D11_SUBRESOURCE_DATA[ mipCount * arraySize ] );
  1607.         if ( !initData )
  1608.         {
  1609.             return E_OUTOFMEMORY;
  1610.         }
  1611.  
  1612.         size_t skipMip = 0;
  1613.         size_t twidth = 0;
  1614.         size_t theight = 0;
  1615.         size_t tdepth = 0;
  1616.         hr = FillInitData( width, height, depth, mipCount, arraySize, format, maxsize, bitSize, bitData,
  1617.                            twidth, theight, tdepth, skipMip, initData.get() );
  1618.  
  1619.         if ( SUCCEEDED(hr) )
  1620.         {
  1621.             hr = CreateD3DResources( d3dDevice, resDim, twidth, theight, tdepth, mipCount - skipMip, arraySize,
  1622.                                      format, usage, bindFlags, cpuAccessFlags, miscFlags, forceSRGB,
  1623.                                      isCubeMap, initData.get(), texture, textureView );
  1624.  
  1625.             if ( FAILED(hr) && !maxsize && (mipCount > 1) )
  1626.             {
  1627.                 // Retry with a maxsize determined by feature level
  1628.                 switch( d3dDevice->GetFeatureLevel() )
  1629.                 {
  1630.                 case D3D_FEATURE_LEVEL_9_1:
  1631.                 case D3D_FEATURE_LEVEL_9_2:
  1632.                     if ( isCubeMap )
  1633.                     {
  1634.                         maxsize = 512 /*D3D_FL9_1_REQ_TEXTURECUBE_DIMENSION*/;
  1635.                     }
  1636.                     else
  1637.                     {
  1638.                         maxsize = (resDim == D3D11_RESOURCE_DIMENSION_TEXTURE3D)
  1639.                                   ? 256 /*D3D_FL9_1_REQ_TEXTURE3D_U_V_OR_W_DIMENSION*/
  1640.                                   : 2048 /*D3D_FL9_1_REQ_TEXTURE2D_U_OR_V_DIMENSION*/;
  1641.                     }
  1642.                     break;
  1643.  
  1644.                 case D3D_FEATURE_LEVEL_9_3:
  1645.                     maxsize = (resDim == D3D11_RESOURCE_DIMENSION_TEXTURE3D)
  1646.                               ? 256 /*D3D_FL9_1_REQ_TEXTURE3D_U_V_OR_W_DIMENSION*/
  1647.                               : 4096 /*D3D_FL9_3_REQ_TEXTURE2D_U_OR_V_DIMENSION*/;
  1648.                     break;
  1649.  
  1650.                 default: // D3D_FEATURE_LEVEL_10_0 & D3D_FEATURE_LEVEL_10_1
  1651.                     maxsize = (resDim == D3D11_RESOURCE_DIMENSION_TEXTURE3D)
  1652.                               ? 2048 /*D3D10_REQ_TEXTURE3D_U_V_OR_W_DIMENSION*/
  1653.                               : 8192 /*D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION*/;
  1654.                     break;
  1655.                 }
  1656.  
  1657.                 hr = FillInitData( width, height, depth, mipCount, arraySize, format, maxsize, bitSize, bitData,
  1658.                                    twidth, theight, tdepth, skipMip, initData.get() );
  1659.                 if ( SUCCEEDED(hr) )
  1660.                 {
  1661.                     hr = CreateD3DResources( d3dDevice, resDim, twidth, theight, tdepth, mipCount - skipMip, arraySize,
  1662.                                              format, usage, bindFlags, cpuAccessFlags, miscFlags, forceSRGB,
  1663.                                              isCubeMap, initData.get(), texture, textureView );
  1664.                 }
  1665.             }
  1666.         }
  1667.     }
  1668.  
  1669.     return hr;
  1670. }
  1671.  
  1672. static HRESULT CreateTextureFromDDS12(
  1673.     _In_ ID3D12Device* device,
  1674.     _In_opt_ ID3D12GraphicsCommandList* cmdList,
  1675.     _In_ const DDS_HEADER* header,
  1676.     _In_reads_bytes_(bitSize) const uint8_t* bitData,
  1677.     _In_ size_t bitSize,
  1678.     _In_ size_t maxsize,
  1679.     _In_ bool forceSRGB,
  1680.     ComPtr<ID3D12Resource>& texture,
  1681.     ComPtr<ID3D12Resource>& textureUploadHeap)
  1682. {
  1683.     HRESULT hr = S_OK;
  1684.  
  1685.     UINT width = header->width;
  1686.     UINT height = header->height;
  1687.     UINT depth = header->depth;
  1688.  
  1689.     uint32_t resDim = D3D12_RESOURCE_DIMENSION_UNKNOWN;
  1690.     UINT arraySize = 1;
  1691.     DXGI_FORMAT format = DXGI_FORMAT_UNKNOWN;
  1692.     bool isCubeMap = false;
  1693.  
  1694.     size_t mipCount = header->mipMapCount;
  1695.     if (0 == mipCount) mipCount = 1;
  1696.  
  1697.     if ((header->ddspf.flags & DDS_FOURCC) && (MAKEFOURCC('D', 'X', '1', '0') == header->ddspf.fourCC))
  1698.     {
  1699.         auto d3d10ext = reinterpret_cast<const DDS_HEADER_DXT10*>((const char*)header + sizeof(DDS_HEADER));
  1700.  
  1701.         arraySize = d3d10ext->arraySize;
  1702.         if (arraySize == 0)
  1703.             return HRESULT_FROM_WIN32(ERROR_INVALID_DATA);
  1704.  
  1705.         switch (d3d10ext->dxgiFormat)
  1706.         {
  1707.         case DXGI_FORMAT_AI44:
  1708.         case DXGI_FORMAT_IA44:
  1709.         case DXGI_FORMAT_P8:
  1710.         case DXGI_FORMAT_A8P8:
  1711.             return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED);
  1712.  
  1713.         default:
  1714.             if (BitsPerPixel(d3d10ext->dxgiFormat) == 0)
  1715.                 return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED);
  1716.         }
  1717.  
  1718.         format = d3d10ext->dxgiFormat;
  1719.  
  1720.         switch (d3d10ext->resourceDimension)
  1721.         {
  1722.         case D3D11_RESOURCE_DIMENSION_TEXTURE1D:
  1723.             if ((header->flags & DDS_HEIGHT) && height != 1)
  1724.                 return HRESULT_FROM_WIN32(ERROR_INVALID_DATA);
  1725.             height = depth = 1;
  1726.             break;
  1727.  
  1728.         case D3D11_RESOURCE_DIMENSION_TEXTURE2D:
  1729.             if (d3d10ext->miscFlag & D3D11_RESOURCE_MISC_TEXTURECUBE)
  1730.             {
  1731.                 arraySize *= 6;
  1732.                 isCubeMap = true;
  1733.             }
  1734.             depth = 1;
  1735.             break;
  1736.  
  1737.         case D3D11_RESOURCE_DIMENSION_TEXTURE3D:
  1738.             if (!(header->flags & DDS_HEADER_FLAGS_VOLUME))
  1739.                 return HRESULT_FROM_WIN32(ERROR_INVALID_DATA);
  1740.             if (arraySize > 1)
  1741.                 return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED);
  1742.             break;
  1743.  
  1744.         default:
  1745.             return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED);
  1746.         }
  1747.  
  1748.         switch (d3d10ext->resourceDimension)
  1749.         {
  1750.         case D3D11_RESOURCE_DIMENSION_TEXTURE1D:
  1751.             resDim = D3D12_RESOURCE_DIMENSION_TEXTURE1D;
  1752.             break;
  1753.         case D3D11_RESOURCE_DIMENSION_TEXTURE2D:
  1754.             resDim = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
  1755.             break;
  1756.         case D3D11_RESOURCE_DIMENSION_TEXTURE3D:
  1757.             resDim = D3D12_RESOURCE_DIMENSION_TEXTURE3D;
  1758.             break;
  1759.         }
  1760.     }
  1761.     else
  1762.     {
  1763.         format = GetDXGIFormat(header->ddspf);
  1764.  
  1765.         if (format == DXGI_FORMAT_UNKNOWN)
  1766.             return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED);
  1767.  
  1768.         if (header->flags & DDS_HEADER_FLAGS_VOLUME)
  1769.         {
  1770.             resDim = D3D12_RESOURCE_DIMENSION_TEXTURE3D;
  1771.         }
  1772.         else
  1773.         {
  1774.             if (header->caps2 & DDS_CUBEMAP)
  1775.             {
  1776.                 if ((header->caps2 & DDS_CUBEMAP_ALLFACES) != DDS_CUBEMAP_ALLFACES)
  1777.                     return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED);
  1778.                 arraySize = 6;
  1779.                 isCubeMap = true;
  1780.             }
  1781.  
  1782.             depth = 1;
  1783.             resDim = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
  1784.         }
  1785.  
  1786.         assert(BitsPerPixel(format) != 0);
  1787.     }
  1788.  
  1789.     // Bound sizes (for security purposes we don't trust DDS file metadata larger than the D3D 11.x hardware requirements)
  1790.     if (mipCount > D3D12_REQ_MIP_LEVELS)
  1791.     {
  1792.         return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED);
  1793.     }
  1794.  
  1795.     switch (resDim)
  1796.     {
  1797.     case D3D12_RESOURCE_DIMENSION_TEXTURE1D:
  1798.         if ((arraySize > D3D12_REQ_TEXTURE1D_ARRAY_AXIS_DIMENSION) ||
  1799.             (width > D3D12_REQ_TEXTURE1D_U_DIMENSION))
  1800.         {
  1801.             return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED);
  1802.         }
  1803.         break;
  1804.  
  1805.     case D3D12_RESOURCE_DIMENSION_TEXTURE2D:
  1806.         if (isCubeMap)
  1807.         {
  1808.             // This is the right bound because we set arraySize to (NumCubes*6) above
  1809.             if ((arraySize > D3D12_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION) ||
  1810.                 (width > D3D12_REQ_TEXTURECUBE_DIMENSION) ||
  1811.                 (height > D3D12_REQ_TEXTURECUBE_DIMENSION))
  1812.             {
  1813.                 return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED);
  1814.             }
  1815.         }
  1816.         else if ((arraySize > D3D12_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION) ||
  1817.             (width > D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION) ||
  1818.             (height > D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION))
  1819.         {
  1820.             return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED);
  1821.         }
  1822.         break;
  1823.  
  1824.     case D3D12_RESOURCE_DIMENSION_TEXTURE3D:
  1825.         if ((arraySize > 1) ||
  1826.             (width > D3D12_REQ_TEXTURE3D_U_V_OR_W_DIMENSION) ||
  1827.             (height > D3D12_REQ_TEXTURE3D_U_V_OR_W_DIMENSION) ||
  1828.             (depth > D3D12_REQ_TEXTURE3D_U_V_OR_W_DIMENSION))
  1829.         {
  1830.             return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED);
  1831.         }
  1832.         break;
  1833.  
  1834.     default:
  1835.         return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED);
  1836.     }
  1837.  
  1838.     // Create the texture
  1839.     std::unique_ptr<D3D12_SUBRESOURCE_DATA[]> initData(
  1840.         new (std::nothrow) D3D12_SUBRESOURCE_DATA[mipCount * arraySize]
  1841.         );
  1842.  
  1843.     if (!initData)
  1844.     {
  1845.         return E_OUTOFMEMORY;
  1846.     }
  1847.  
  1848.     size_t skipMip = 0;
  1849.     size_t twidth = 0;
  1850.     size_t theight = 0;
  1851.     size_t tdepth = 0;
  1852.  
  1853.     hr = FillInitData12(
  1854.         width, height, depth, mipCount, arraySize, format, maxsize, bitSize, bitData,
  1855.         twidth, theight, tdepth, skipMip, initData.get()
  1856.         );
  1857.  
  1858.     if (SUCCEEDED(hr))
  1859.     {
  1860.         hr = CreateD3DResources12(
  1861.             device, cmdList,
  1862.             resDim, twidth, theight, tdepth,
  1863.             mipCount - skipMip,
  1864.             arraySize,
  1865.             format,
  1866.             false, // forceSRGB
  1867.             isCubeMap,
  1868.             initData.get(),
  1869.             texture, 
  1870.             textureUploadHeap);
  1871.     }
  1872.  
  1873.     return hr;
  1874. }
  1875.  
  1876. //--------------------------------------------------------------------------------------
  1877. static DDS_ALPHA_MODE GetAlphaMode( _In_ const DDS_HEADER* header )
  1878. {
  1879.     if ( header->ddspf.flags & DDS_FOURCC )
  1880.     {
  1881.         if ( MAKEFOURCC( 'D', 'X', '1', '0' ) == header->ddspf.fourCC )
  1882.         {
  1883.             auto d3d10ext = reinterpret_cast<const DDS_HEADER_DXT10*>( (const char*)header + sizeof(DDS_HEADER) );
  1884.             auto mode = static_cast<DDS_ALPHA_MODE>( d3d10ext->miscFlags2 & DDS_MISC_FLAGS2_ALPHA_MODE_MASK );
  1885.             switch( mode )
  1886.             {
  1887.             case DDS_ALPHA_MODE_STRAIGHT:
  1888.             case DDS_ALPHA_MODE_PREMULTIPLIED:
  1889.             case DDS_ALPHA_MODE_OPAQUE:
  1890.             case DDS_ALPHA_MODE_CUSTOM:
  1891.                 return mode;
  1892.             }
  1893.         }
  1894.         else if ( ( MAKEFOURCC( 'D', 'X', 'T', '2' ) == header->ddspf.fourCC )
  1895.                   || ( MAKEFOURCC( 'D', 'X', 'T', '4' ) == header->ddspf.fourCC ) )
  1896.         {
  1897.             return DDS_ALPHA_MODE_PREMULTIPLIED;
  1898.         }
  1899.     }
  1900.  
  1901.     return DDS_ALPHA_MODE_UNKNOWN;
  1902. }
  1903.  
  1904.  
  1905. //--------------------------------------------------------------------------------------
  1906. _Use_decl_annotations_
  1907. HRESULT DirectX::CreateDDSTextureFromMemory( ID3D11Device* d3dDevice,
  1908.                                              const uint8_t* ddsData,
  1909.                                              size_t ddsDataSize,
  1910.                                              ID3D11Resource** texture,
  1911.                                              ID3D11ShaderResourceView** textureView,
  1912.                                              size_t maxsize,
  1913.                                              DDS_ALPHA_MODE* alphaMode )
  1914. {
  1915.     return CreateDDSTextureFromMemoryEx( d3dDevice, nullptr, ddsData, ddsDataSize, maxsize,
  1916.                                          D3D11_USAGE_DEFAULT, D3D11_BIND_SHADER_RESOURCE, 0, 0, false,
  1917.                                          texture, textureView, alphaMode );
  1918. }
  1919.  
  1920. _Use_decl_annotations_
  1921. HRESULT DirectX::CreateDDSTextureFromMemory12(
  1922.     ID3D12Device* device,
  1923.     _In_ ID3D12GraphicsCommandList* cmdList,
  1924.     _In_reads_bytes_(ddsDataSize) const uint8_t* ddsData,
  1925.     _In_ size_t ddsDataSize,
  1926.     ComPtr<ID3D12Resource>& texture,
  1927.     ComPtr<ID3D12Resource>& textureUploadHeap,
  1928.     _In_ size_t maxsize,
  1929.     _Out_opt_ DDS_ALPHA_MODE* alphaMode
  1930.     )
  1931. {
  1932.     if (alphaMode)
  1933.         (*alphaMode) = DDS_ALPHA_MODE_UNKNOWN;
  1934.  
  1935.     if (!device || !cmdList || !ddsData || !ddsDataSize)
  1936.     {
  1937.         return E_INVALIDARG;
  1938.     }
  1939.  
  1940.     uint32_t dwMagicNumber = *(const uint32_t*)(ddsData);
  1941.     if (dwMagicNumber != DDS_MAGIC)
  1942.     {
  1943.         return E_FAIL;
  1944.     }
  1945.  
  1946.     auto header = reinterpret_cast<const DDS_HEADER*>(ddsData + sizeof(uint32_t));
  1947.  
  1948.     // Verify header to validate DDS file
  1949.     if (header->size != sizeof(DDS_HEADER) ||
  1950.         header->ddspf.size != sizeof(DDS_PIXELFORMAT))
  1951.     {
  1952.         return E_FAIL;
  1953.     }
  1954.  
  1955.     // Check for DX10 extension
  1956.     bool bDXT10Header = false;
  1957.     if ((header->ddspf.flags & DDS_FOURCC) &&
  1958.         (MAKEFOURCC('D', 'X', '1', '0') == header->ddspf.fourCC))
  1959.     {
  1960.         // Must be long enough for both headers and magic value
  1961.         if (ddsDataSize < (sizeof(DDS_HEADER) + sizeof(uint32_t) + sizeof(DDS_HEADER_DXT10)))
  1962.         {
  1963.             return E_FAIL;
  1964.         }
  1965.  
  1966.         bDXT10Header = true;
  1967.     }
  1968.  
  1969.     ptrdiff_t offset = sizeof(uint32_t)
  1970.         + sizeof(DDS_HEADER)
  1971.         + (bDXT10Header ? sizeof(DDS_HEADER_DXT10) : 0);
  1972.  
  1973.     HRESULT hr = CreateTextureFromDDS12(
  1974.         device,
  1975.         cmdList,
  1976.         header,
  1977.         ddsData + offset,
  1978.         ddsDataSize - offset,
  1979.         maxsize,
  1980.         false,
  1981.         texture,
  1982.         textureUploadHeap
  1983.         );
  1984.  
  1985.     if (SUCCEEDED(hr))
  1986.     {
  1987.         if (alphaMode)
  1988.             (*alphaMode) = GetAlphaMode(header);
  1989.     }
  1990.  
  1991.     return hr;
  1992. }
  1993.  
  1994. _Use_decl_annotations_
  1995. HRESULT DirectX::CreateDDSTextureFromMemory( ID3D11Device* d3dDevice,
  1996.                                              ID3D11DeviceContext* d3dContext,
  1997.                                              const uint8_t* ddsData,
  1998.                                              size_t ddsDataSize,
  1999.                                              ID3D11Resource** texture,
  2000.                                              ID3D11ShaderResourceView** textureView,
  2001.                                              size_t maxsize,
  2002.                                              DDS_ALPHA_MODE* alphaMode )
  2003. {
  2004.     return CreateDDSTextureFromMemoryEx( d3dDevice, d3dContext, ddsData, ddsDataSize, maxsize,
  2005.                                          D3D11_USAGE_DEFAULT, D3D11_BIND_SHADER_RESOURCE, 0, 0, false,
  2006.                                          texture, textureView, alphaMode );
  2007. }
  2008.  
  2009. _Use_decl_annotations_
  2010. HRESULT DirectX::CreateDDSTextureFromMemoryEx( ID3D11Device* d3dDevice,
  2011.                                                const uint8_t* ddsData,
  2012.                                                size_t ddsDataSize,
  2013.                                                size_t maxsize,
  2014.                                                D3D11_USAGE usage,
  2015.                                                unsigned int bindFlags,
  2016.                                                unsigned int cpuAccessFlags,
  2017.                                                unsigned int miscFlags,
  2018.                                                bool forceSRGB,
  2019.                                                ID3D11Resource** texture,
  2020.                                                ID3D11ShaderResourceView** textureView,
  2021.                                                DDS_ALPHA_MODE* alphaMode )
  2022. {
  2023.     return CreateDDSTextureFromMemoryEx( d3dDevice, nullptr, ddsData, ddsDataSize, maxsize,
  2024.                                          usage, bindFlags, cpuAccessFlags, miscFlags, forceSRGB,
  2025.                                          texture, textureView, alphaMode );
  2026. }
  2027.  
  2028. _Use_decl_annotations_
  2029. HRESULT DirectX::CreateDDSTextureFromMemoryEx( ID3D11Device* d3dDevice,
  2030.                                                ID3D11DeviceContext* d3dContext,
  2031.                                                const uint8_t* ddsData,
  2032.                                                size_t ddsDataSize,
  2033.                                                size_t maxsize,
  2034.                                                D3D11_USAGE usage,
  2035.                                                unsigned int bindFlags,
  2036.                                                unsigned int cpuAccessFlags,
  2037.                                                unsigned int miscFlags,
  2038.                                                bool forceSRGB,
  2039.                                                ID3D11Resource** texture,
  2040.                                                ID3D11ShaderResourceView** textureView,
  2041.                                                DDS_ALPHA_MODE* alphaMode )
  2042. {
  2043.     if ( texture )
  2044.     {
  2045.         *texture = nullptr;
  2046.     }
  2047.     if ( textureView )
  2048.     {
  2049.         *textureView = nullptr;
  2050.     }
  2051.     if ( alphaMode )
  2052.     {
  2053.         *alphaMode = DDS_ALPHA_MODE_UNKNOWN;
  2054.     }
  2055.  
  2056.     if (!d3dDevice || !ddsData || (!texture && !textureView))
  2057.     {
  2058.         return E_INVALIDARG;
  2059.     }
  2060.  
  2061.     // Validate DDS file in memory
  2062.     if (ddsDataSize < (sizeof(uint32_t) + sizeof(DDS_HEADER)))
  2063.     {
  2064.         return E_FAIL;
  2065.     }
  2066.  
  2067.     uint32_t dwMagicNumber = *( const uint32_t* )( ddsData );
  2068.     if (dwMagicNumber != DDS_MAGIC)
  2069.     {
  2070.         return E_FAIL;
  2071.     }
  2072.  
  2073.     auto header = reinterpret_cast<const DDS_HEADER*>( ddsData + sizeof( uint32_t ) );
  2074.  
  2075.     // Verify header to validate DDS file
  2076.     if (header->size != sizeof(DDS_HEADER) ||
  2077.         header->ddspf.size != sizeof(DDS_PIXELFORMAT))
  2078.     {
  2079.         return E_FAIL;
  2080.     }
  2081.  
  2082.     // Check for DX10 extension
  2083.     bool bDXT10Header = false;
  2084.     if ((header->ddspf.flags & DDS_FOURCC) &&
  2085.         (MAKEFOURCC( 'D', 'X', '1', '0' ) == header->ddspf.fourCC) )
  2086.     {
  2087.         // Must be long enough for both headers and magic value
  2088.         if (ddsDataSize < (sizeof(DDS_HEADER) + sizeof(uint32_t) + sizeof(DDS_HEADER_DXT10)))
  2089.         {
  2090.             return E_FAIL;
  2091.         }
  2092.  
  2093.         bDXT10Header = true;
  2094.     }
  2095.  
  2096.     ptrdiff_t offset = sizeof( uint32_t )
  2097.                        + sizeof( DDS_HEADER )
  2098.                        + (bDXT10Header ? sizeof( DDS_HEADER_DXT10 ) : 0);
  2099.  
  2100.     HRESULT hr = CreateTextureFromDDS( d3dDevice, d3dContext, header,
  2101.                                        ddsData + offset, ddsDataSize - offset, maxsize,
  2102.                                        usage, bindFlags, cpuAccessFlags, miscFlags, forceSRGB,
  2103.                                        texture, textureView );
  2104.     if ( SUCCEEDED(hr) )
  2105.     {
  2106.         if (texture != 0 && *texture != 0)
  2107.         {
  2108.             SetDebugObjectName(*texture, "DDSTextureLoader");
  2109.         }
  2110.  
  2111.         if (textureView != 0 && *textureView != 0)
  2112.         {
  2113.             SetDebugObjectName(*textureView, "DDSTextureLoader");
  2114.         }
  2115.  
  2116.         if ( alphaMode )
  2117.             *alphaMode = GetAlphaMode( header );
  2118.     }
  2119.  
  2120.     return hr;
  2121. }
  2122.  
  2123. //--------------------------------------------------------------------------------------
  2124. _Use_decl_annotations_
  2125. HRESULT DirectX::CreateDDSTextureFromFile( ID3D11Device* d3dDevice,
  2126.                                            const wchar_t* fileName,
  2127.                                            ID3D11Resource** texture,
  2128.                                            ID3D11ShaderResourceView** textureView,
  2129.                                            size_t maxsize,
  2130.                                            DDS_ALPHA_MODE* alphaMode )
  2131. {
  2132.     return CreateDDSTextureFromFileEx( d3dDevice, nullptr, fileName, maxsize,
  2133.                                        D3D11_USAGE_DEFAULT, D3D11_BIND_SHADER_RESOURCE, 0, 0, false,
  2134.                                        texture, textureView, alphaMode );
  2135. }
  2136.  
  2137. HRESULT DirectX::CreateDDSTextureFromFile12(_In_ ID3D12Device* device,
  2138.     _In_ ID3D12GraphicsCommandList* cmdList,
  2139.     _In_z_ const wchar_t* szFileName,
  2140.     _Out_ ComPtr<ID3D12Resource>& texture,
  2141.     _Out_ ComPtr<ID3D12Resource>& textureUploadHeap,
  2142.     _In_ size_t maxsize,
  2143.     _Out_opt_ DDS_ALPHA_MODE* alphaMode)
  2144. {
  2145.     if (texture)
  2146.     {
  2147.         texture = nullptr;
  2148.     }
  2149.     if (textureUploadHeap)
  2150.     {
  2151.         textureUploadHeap = nullptr;
  2152.     }
  2153.     if (alphaMode)
  2154.     {
  2155.         *alphaMode = DDS_ALPHA_MODE_UNKNOWN;
  2156.     }
  2157.  
  2158.     if (!device || !szFileName)
  2159.     {
  2160.         return E_INVALIDARG;
  2161.     }
  2162.  
  2163.     DDS_HEADER* header = nullptr;
  2164.     uint8_t* bitData = nullptr;
  2165.     size_t bitSize = 0;
  2166.  
  2167.     std::unique_ptr<uint8_t[]> ddsData;
  2168.     HRESULT hr = LoadTextureDataFromFile(szFileName, ddsData, &header, &bitData, &bitSize);
  2169.     if (FAILED(hr))
  2170.     {
  2171.         return hr;
  2172.     }
  2173.  
  2174.     hr = CreateTextureFromDDS12(device, cmdList, header,
  2175.         bitData, bitSize, maxsize, false, texture, textureUploadHeap);
  2176.  
  2177.     if (SUCCEEDED(hr))
  2178.     {
  2179. /*
  2180. #if !defined(NO_D3D11_DEBUG_NAME) && ( defined(_DEBUG) || defined(PROFILE) )
  2181.         if (texture != 0 || textureView != 0)
  2182.         {
  2183.             CHAR strFileA[MAX_PATH];
  2184.             int result = WideCharToMultiByte(CP_ACP,
  2185.                 WC_NO_BEST_FIT_CHARS,
  2186.                 fileName,
  2187.                 -1,
  2188.                 strFileA,
  2189.                 MAX_PATH,
  2190.                 nullptr,
  2191.                 FALSE
  2192.                 );
  2193.             if (result > 0)
  2194.             {
  2195.                 const CHAR* pstrName = strrchr(strFileA, '\\');
  2196.                 if (!pstrName)
  2197.                 {
  2198.                     pstrName = strFileA;
  2199.                 }
  2200.                 else
  2201.                 {
  2202.                     pstrName++;
  2203.                 }
  2204.  
  2205.                 if (texture != 0 && *texture != 0)
  2206.                 {
  2207.                     (*texture)->SetPrivateData(WKPDID_D3DDebugObjectName,
  2208.                         static_cast<UINT>(strnlen_s(pstrName, MAX_PATH)),
  2209.                         pstrName
  2210.                         );
  2211.                 }
  2212.  
  2213.                 if (textureView != 0 && *textureView != 0)
  2214.                 {
  2215.                     (*textureView)->SetPrivateData(WKPDID_D3DDebugObjectName,
  2216.                         static_cast<UINT>(strnlen_s(pstrName, MAX_PATH)),
  2217.                         pstrName
  2218.                         );
  2219.                 }
  2220.             }
  2221.         }
  2222. #endif
  2223. */
  2224.         if (alphaMode)
  2225.             *alphaMode = GetAlphaMode(header);
  2226.     }
  2227.  
  2228.     return hr;
  2229. }
  2230.  
  2231. _Use_decl_annotations_
  2232. HRESULT DirectX::CreateDDSTextureFromFile( ID3D11Device* d3dDevice,
  2233.                                            ID3D11DeviceContext* d3dContext,
  2234.                                            const wchar_t* fileName,
  2235.                                            ID3D11Resource** texture,
  2236.                                            ID3D11ShaderResourceView** textureView,
  2237.                                            size_t maxsize,
  2238.                                            DDS_ALPHA_MODE* alphaMode )
  2239. {
  2240.     return CreateDDSTextureFromFileEx( d3dDevice, d3dContext, fileName, maxsize,
  2241.                                        D3D11_USAGE_DEFAULT, D3D11_BIND_SHADER_RESOURCE, 0, 0, false,
  2242.                                        texture, textureView, alphaMode );
  2243. }
  2244.  
  2245. _Use_decl_annotations_
  2246. HRESULT DirectX::CreateDDSTextureFromFileEx( ID3D11Device* d3dDevice,
  2247.                                              const wchar_t* fileName,
  2248.                                              size_t maxsize,
  2249.                                              D3D11_USAGE usage,
  2250.                                              unsigned int bindFlags,
  2251.                                              unsigned int cpuAccessFlags,
  2252.                                              unsigned int miscFlags,
  2253.                                              bool forceSRGB,
  2254.                                              ID3D11Resource** texture,
  2255.                                              ID3D11ShaderResourceView** textureView,
  2256.                                              DDS_ALPHA_MODE* alphaMode )
  2257. {
  2258.     return CreateDDSTextureFromFileEx( d3dDevice, nullptr, fileName, maxsize,
  2259.                                        usage, bindFlags, cpuAccessFlags, miscFlags, forceSRGB,
  2260.                                        texture, textureView, alphaMode );
  2261. }
  2262.  
  2263. _Use_decl_annotations_
  2264. HRESULT DirectX::CreateDDSTextureFromFileEx( ID3D11Device* d3dDevice,
  2265.                                              ID3D11DeviceContext* d3dContext,
  2266.                                              const wchar_t* fileName,
  2267.                                              size_t maxsize,
  2268.                                              D3D11_USAGE usage,
  2269.                                              unsigned int bindFlags,
  2270.                                              unsigned int cpuAccessFlags,
  2271.                                              unsigned int miscFlags,
  2272.                                              bool forceSRGB,
  2273.                                              ID3D11Resource** texture,
  2274.                                              ID3D11ShaderResourceView** textureView,
  2275.                                              DDS_ALPHA_MODE* alphaMode )
  2276. {
  2277.     if ( texture )
  2278.     {
  2279.         *texture = nullptr;
  2280.     }
  2281.     if ( textureView )
  2282.     {
  2283.         *textureView = nullptr;
  2284.     }
  2285.     if ( alphaMode )
  2286.     {
  2287.         *alphaMode = DDS_ALPHA_MODE_UNKNOWN;
  2288.     }
  2289.  
  2290.     if (!d3dDevice || !fileName || (!texture && !textureView))
  2291.     {
  2292.         return E_INVALIDARG;
  2293.     }
  2294.  
  2295.     DDS_HEADER* header = nullptr;
  2296.     uint8_t* bitData = nullptr;
  2297.     size_t bitSize = 0;
  2298.  
  2299.     std::unique_ptr<uint8_t[]> ddsData;
  2300.     HRESULT hr = LoadTextureDataFromFile( fileName,
  2301.                                           ddsData,
  2302.                                           &header,
  2303.                                           &bitData,
  2304.                                           &bitSize
  2305.                                         );
  2306.     if (FAILED(hr))
  2307.     {
  2308.         return hr;
  2309.     }
  2310.  
  2311.     hr = CreateTextureFromDDS( d3dDevice, d3dContext, header,
  2312.                                bitData, bitSize, maxsize,
  2313.                                usage, bindFlags, cpuAccessFlags, miscFlags, forceSRGB,
  2314.                                texture, textureView );
  2315.  
  2316.     if ( SUCCEEDED(hr) )
  2317.     {
  2318. #if !defined(NO_D3D11_DEBUG_NAME) && ( defined(_DEBUG) || defined(PROFILE) )
  2319.         if (texture != 0 || textureView != 0)
  2320.         {
  2321.             CHAR strFileA[MAX_PATH];
  2322.             int result = WideCharToMultiByte( CP_ACP,
  2323.                                               WC_NO_BEST_FIT_CHARS,
  2324.                                               fileName,
  2325.                                               -1,
  2326.                                               strFileA,
  2327.                                               MAX_PATH,
  2328.                                               nullptr,
  2329.                                               FALSE
  2330.                                );
  2331.             if ( result > 0 )
  2332.             {
  2333.                 const CHAR* pstrName = strrchr( strFileA, '\\' );
  2334.                 if (!pstrName)
  2335.                 {
  2336.                     pstrName = strFileA;
  2337.                 }
  2338.                 else
  2339.                 {
  2340.                     pstrName++;
  2341.                 }
  2342.  
  2343.                 if (texture != 0 && *texture != 0)
  2344.                 {
  2345.                     (*texture)->SetPrivateData( WKPDID_D3DDebugObjectName,
  2346.                                                 static_cast<UINT>( strnlen_s(pstrName, MAX_PATH) ),
  2347.                                                 pstrName
  2348.                                               );
  2349.                 }
  2350.  
  2351.                 if (textureView != 0 && *textureView != 0 )
  2352.                 {
  2353.                     (*textureView)->SetPrivateData( WKPDID_D3DDebugObjectName,
  2354.                                                     static_cast<UINT>( strnlen_s(pstrName, MAX_PATH) ),
  2355.                                                     pstrName
  2356.                                                   );
  2357.                 }
  2358.             }
  2359.         }
  2360. #endif
  2361.  
  2362.         if ( alphaMode )
  2363.             *alphaMode = GetAlphaMode( header );
  2364.     }
  2365.  
  2366.     return hr;
  2367. }
  2368.