crossxtex/DirectXTex/DirectXTexDDS.cpp

2210 lines
83 KiB
C++
Raw Normal View History

2016-08-22 18:26:36 +00:00
//-------------------------------------------------------------------------------------
// DirectXTexDDS.cpp
//
// DirectX Texture Library - Microsoft DirectDraw Surface (DDS) file format reader/writer
//
// Copyright (c) Microsoft Corporation. All rights reserved.
2018-02-24 06:24:46 +00:00
// Licensed under the MIT License.
2016-08-22 18:26:36 +00:00
//
// http://go.microsoft.com/fwlink/?LinkId=248926
//-------------------------------------------------------------------------------------
2018-06-13 01:07:34 +00:00
#include "DirectXTexp.h"
2016-08-22 18:26:36 +00:00
#include "dds.h"
2016-09-09 02:09:46 +00:00
using namespace DirectX;
2017-07-12 07:56:51 +00:00
static_assert(static_cast<int>(TEX_DIMENSION_TEXTURE1D) == static_cast<int>(DDS_DIMENSION_TEXTURE1D), "header enum mismatch");
static_assert(static_cast<int>(TEX_DIMENSION_TEXTURE2D) == static_cast<int>(DDS_DIMENSION_TEXTURE2D), "header enum mismatch");
static_assert(static_cast<int>(TEX_DIMENSION_TEXTURE3D) == static_cast<int>(DDS_DIMENSION_TEXTURE3D), "header enum mismatch");
2017-01-07 01:16:43 +00:00
2016-08-22 18:26:36 +00:00
namespace
{
2016-09-09 02:09:46 +00:00
//-------------------------------------------------------------------------------------
// Legacy format mapping table (used for DDS files without 'DX10' extended header)
//-------------------------------------------------------------------------------------
enum CONVERSION_FLAGS
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
CONV_FLAGS_NONE = 0x0,
CONV_FLAGS_EXPAND = 0x1, // Conversion requires expanded pixel size
CONV_FLAGS_NOALPHA = 0x2, // Conversion requires setting alpha to known value
CONV_FLAGS_SWIZZLE = 0x4, // BGR/RGB order swizzling required
CONV_FLAGS_PAL8 = 0x8, // Has an 8-bit palette
CONV_FLAGS_888 = 0x10, // Source is an 8:8:8 (24bpp) format
CONV_FLAGS_565 = 0x20, // Source is a 5:6:5 (16bpp) format
CONV_FLAGS_5551 = 0x40, // Source is a 5:5:5:1 (16bpp) format
CONV_FLAGS_4444 = 0x80, // Source is a 4:4:4:4 (16bpp) format
CONV_FLAGS_44 = 0x100, // Source is a 4:4 (8bpp) format
CONV_FLAGS_332 = 0x200, // Source is a 3:3:2 (8bpp) format
CONV_FLAGS_8332 = 0x400, // Source is a 8:3:3:2 (16bpp) format
CONV_FLAGS_A8P8 = 0x800, // Has an 8-bit palette with an alpha channel
CONV_FLAGS_DX10 = 0x10000, // Has the 'DX10' extension header
CONV_FLAGS_PMALPHA = 0x20000, // Contains premultiplied alpha data
CONV_FLAGS_L8 = 0x40000, // Source is a 8 luminance format
CONV_FLAGS_L16 = 0x80000, // Source is a 16 luminance format
CONV_FLAGS_A8L8 = 0x100000, // Source is a 8:8 luminance format
};
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
struct LegacyDDS
{
DXGI_FORMAT format;
DWORD convFlags;
DDS_PIXELFORMAT ddpf;
};
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
const LegacyDDS g_LegacyDDSMap[] =
{
{ DXGI_FORMAT_BC1_UNORM, CONV_FLAGS_NONE, DDSPF_DXT1 }, // D3DFMT_DXT1
{ DXGI_FORMAT_BC2_UNORM, CONV_FLAGS_NONE, DDSPF_DXT3 }, // D3DFMT_DXT3
{ DXGI_FORMAT_BC3_UNORM, CONV_FLAGS_NONE, DDSPF_DXT5 }, // D3DFMT_DXT5
{ DXGI_FORMAT_BC2_UNORM, CONV_FLAGS_PMALPHA, DDSPF_DXT2 }, // D3DFMT_DXT2
{ DXGI_FORMAT_BC3_UNORM, CONV_FLAGS_PMALPHA, DDSPF_DXT4 }, // D3DFMT_DXT4
{ DXGI_FORMAT_BC4_UNORM, CONV_FLAGS_NONE, DDSPF_BC4_UNORM },
{ DXGI_FORMAT_BC4_SNORM, CONV_FLAGS_NONE, DDSPF_BC4_SNORM },
{ DXGI_FORMAT_BC5_UNORM, CONV_FLAGS_NONE, DDSPF_BC5_UNORM },
{ DXGI_FORMAT_BC5_SNORM, CONV_FLAGS_NONE, DDSPF_BC5_SNORM },
{ DXGI_FORMAT_BC4_UNORM, CONV_FLAGS_NONE, { sizeof(DDS_PIXELFORMAT), DDS_FOURCC, MAKEFOURCC('A', 'T', 'I', '1'), 0, 0, 0, 0, 0 } },
{ DXGI_FORMAT_BC5_UNORM, CONV_FLAGS_NONE, { sizeof(DDS_PIXELFORMAT), DDS_FOURCC, MAKEFOURCC('A', 'T', 'I', '2'), 0, 0, 0, 0, 0 } },
{ DXGI_FORMAT_R8G8_B8G8_UNORM, CONV_FLAGS_NONE, DDSPF_R8G8_B8G8 }, // D3DFMT_R8G8_B8G8
{ DXGI_FORMAT_G8R8_G8B8_UNORM, CONV_FLAGS_NONE, DDSPF_G8R8_G8B8 }, // D3DFMT_G8R8_G8B8
{ DXGI_FORMAT_B8G8R8A8_UNORM, CONV_FLAGS_NONE, DDSPF_A8R8G8B8 }, // D3DFMT_A8R8G8B8 (uses DXGI 1.1 format)
{ DXGI_FORMAT_B8G8R8X8_UNORM, CONV_FLAGS_NONE, DDSPF_X8R8G8B8 }, // D3DFMT_X8R8G8B8 (uses DXGI 1.1 format)
{ DXGI_FORMAT_R8G8B8A8_UNORM, CONV_FLAGS_NONE, DDSPF_A8B8G8R8 }, // D3DFMT_A8B8G8R8
{ DXGI_FORMAT_R8G8B8A8_UNORM, CONV_FLAGS_NOALPHA, DDSPF_X8B8G8R8 }, // D3DFMT_X8B8G8R8
{ DXGI_FORMAT_R16G16_UNORM, CONV_FLAGS_NONE, DDSPF_G16R16 }, // D3DFMT_G16R16
{ DXGI_FORMAT_R10G10B10A2_UNORM, CONV_FLAGS_SWIZZLE, { sizeof(DDS_PIXELFORMAT), DDS_RGBA, 0, 32, 0x000003ff, 0x000ffc00, 0x3ff00000, 0xc0000000 } }, // D3DFMT_A2R10G10B10 (D3DX reversal issue workaround)
{ DXGI_FORMAT_R10G10B10A2_UNORM, CONV_FLAGS_NONE, { sizeof(DDS_PIXELFORMAT), DDS_RGBA, 0, 32, 0x3ff00000, 0x000ffc00, 0x000003ff, 0xc0000000 } }, // D3DFMT_A2B10G10R10 (D3DX reversal issue workaround)
2016-09-09 02:09:46 +00:00
{ DXGI_FORMAT_R8G8B8A8_UNORM, CONV_FLAGS_EXPAND
| CONV_FLAGS_NOALPHA
| CONV_FLAGS_888, DDSPF_R8G8B8 }, // D3DFMT_R8G8B8
{ DXGI_FORMAT_B5G6R5_UNORM, CONV_FLAGS_565, DDSPF_R5G6B5 }, // D3DFMT_R5G6B5
{ DXGI_FORMAT_B5G5R5A1_UNORM, CONV_FLAGS_5551, DDSPF_A1R5G5B5 }, // D3DFMT_A1R5G5B5
{ DXGI_FORMAT_B5G5R5A1_UNORM, CONV_FLAGS_5551
| CONV_FLAGS_NOALPHA, { sizeof(DDS_PIXELFORMAT), DDS_RGB, 0, 16, 0x7c00, 0x03e0, 0x001f, 0x0000 } }, // D3DFMT_X1R5G5B5
{ DXGI_FORMAT_R8G8B8A8_UNORM, CONV_FLAGS_EXPAND
| CONV_FLAGS_8332, { sizeof(DDS_PIXELFORMAT), DDS_RGBA, 0, 16, 0x00e0, 0x001c, 0x0003, 0xff00 } }, // D3DFMT_A8R3G3B2
2016-09-09 02:09:46 +00:00
{ DXGI_FORMAT_B5G6R5_UNORM, CONV_FLAGS_EXPAND
| CONV_FLAGS_332, { sizeof(DDS_PIXELFORMAT), DDS_RGB, 0, 8, 0xe0, 0x1c, 0x03, 0x00 } }, // D3DFMT_R3G3B2
{ DXGI_FORMAT_R8_UNORM, CONV_FLAGS_NONE, DDSPF_L8 }, // D3DFMT_L8
{ DXGI_FORMAT_R16_UNORM, CONV_FLAGS_NONE, DDSPF_L16 }, // D3DFMT_L16
{ DXGI_FORMAT_R8G8_UNORM, CONV_FLAGS_NONE, DDSPF_A8L8 }, // D3DFMT_A8L8
{ DXGI_FORMAT_R8G8_UNORM, CONV_FLAGS_NONE, DDSPF_A8L8_ALT }, // D3DFMT_A8L8 (alternative bitcount)
2016-09-09 02:09:46 +00:00
{ DXGI_FORMAT_A8_UNORM, CONV_FLAGS_NONE, DDSPF_A8 }, // D3DFMT_A8
{ DXGI_FORMAT_R16G16B16A16_UNORM, CONV_FLAGS_NONE, { sizeof(DDS_PIXELFORMAT), DDS_FOURCC, 36, 0, 0, 0, 0, 0 } }, // D3DFMT_A16B16G16R16
{ DXGI_FORMAT_R16G16B16A16_SNORM, CONV_FLAGS_NONE, { sizeof(DDS_PIXELFORMAT), DDS_FOURCC, 110, 0, 0, 0, 0, 0 } }, // D3DFMT_Q16W16V16U16
{ DXGI_FORMAT_R16_FLOAT, CONV_FLAGS_NONE, { sizeof(DDS_PIXELFORMAT), DDS_FOURCC, 111, 0, 0, 0, 0, 0 } }, // D3DFMT_R16F
{ DXGI_FORMAT_R16G16_FLOAT, CONV_FLAGS_NONE, { sizeof(DDS_PIXELFORMAT), DDS_FOURCC, 112, 0, 0, 0, 0, 0 } }, // D3DFMT_G16R16F
{ DXGI_FORMAT_R16G16B16A16_FLOAT, CONV_FLAGS_NONE, { sizeof(DDS_PIXELFORMAT), DDS_FOURCC, 113, 0, 0, 0, 0, 0 } }, // D3DFMT_A16B16G16R16F
{ DXGI_FORMAT_R32_FLOAT, CONV_FLAGS_NONE, { sizeof(DDS_PIXELFORMAT), DDS_FOURCC, 114, 0, 0, 0, 0, 0 } }, // D3DFMT_R32F
{ DXGI_FORMAT_R32G32_FLOAT, CONV_FLAGS_NONE, { sizeof(DDS_PIXELFORMAT), DDS_FOURCC, 115, 0, 0, 0, 0, 0 } }, // D3DFMT_G32R32F
{ DXGI_FORMAT_R32G32B32A32_FLOAT, CONV_FLAGS_NONE, { sizeof(DDS_PIXELFORMAT), DDS_FOURCC, 116, 0, 0, 0, 0, 0 } }, // D3DFMT_A32B32G32R32F
{ DXGI_FORMAT_R32_FLOAT, CONV_FLAGS_NONE, { sizeof(DDS_PIXELFORMAT), DDS_RGB, 0, 32, 0xffffffff, 0x00000000, 0x00000000, 0x00000000 } }, // D3DFMT_R32F (D3DX uses FourCC 114 instead)
{ DXGI_FORMAT_R8G8B8A8_UNORM, CONV_FLAGS_EXPAND
| CONV_FLAGS_PAL8
| CONV_FLAGS_A8P8, { sizeof(DDS_PIXELFORMAT), DDS_PAL8A, 0, 16, 0, 0, 0, 0 } }, // D3DFMT_A8P8
2016-09-09 02:09:46 +00:00
{ DXGI_FORMAT_R8G8B8A8_UNORM, CONV_FLAGS_EXPAND
| CONV_FLAGS_PAL8, { sizeof(DDS_PIXELFORMAT), DDS_PAL8, 0, 8, 0, 0, 0, 0 } }, // D3DFMT_P8
{ DXGI_FORMAT_B4G4R4A4_UNORM, CONV_FLAGS_4444, DDSPF_A4R4G4B4 }, // D3DFMT_A4R4G4B4 (uses DXGI 1.2 format)
{ DXGI_FORMAT_B4G4R4A4_UNORM, CONV_FLAGS_NOALPHA
| CONV_FLAGS_4444, { sizeof(DDS_PIXELFORMAT), DDS_RGB, 0, 16, 0x0f00, 0x00f0, 0x000f, 0x0000 } }, // D3DFMT_X4R4G4B4 (uses DXGI 1.2 format)
{ DXGI_FORMAT_B4G4R4A4_UNORM, CONV_FLAGS_EXPAND
| CONV_FLAGS_44, { sizeof(DDS_PIXELFORMAT), DDS_LUMINANCEA,0, 8, 0x0f, 0x00, 0x00, 0xf0 } }, // D3DFMT_A4L4 (uses DXGI 1.2 format)
2016-09-09 02:09:46 +00:00
{ DXGI_FORMAT_YUY2, CONV_FLAGS_NONE, DDSPF_YUY2 }, // D3DFMT_YUY2 (uses DXGI 1.2 format)
{ DXGI_FORMAT_YUY2, CONV_FLAGS_SWIZZLE, { sizeof(DDS_PIXELFORMAT), DDS_FOURCC, MAKEFOURCC('U','Y','V','Y'), 0, 0, 0, 0, 0 } }, // D3DFMT_UYVY (uses DXGI 1.2 format)
{ DXGI_FORMAT_R8G8_SNORM, CONV_FLAGS_NONE, DDSPF_V8U8 }, // D3DFMT_V8U8
{ DXGI_FORMAT_R8G8B8A8_SNORM, CONV_FLAGS_NONE, DDSPF_Q8W8V8U8 }, // D3DFMT_Q8W8V8U8
{ DXGI_FORMAT_R16G16_SNORM, CONV_FLAGS_NONE, DDSPF_V16U16 }, // D3DFMT_V16U16
};
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
// Note that many common DDS reader/writers (including D3DX) swap the
// the RED/BLUE masks for 10:10:10:2 formats. We assumme
// below that the 'backwards' header mask is being used since it is most
// likely written by D3DX. The more robust solution is to use the 'DX10'
// header extension and specify the DXGI_FORMAT_R10G10B10A2_UNORM format directly
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
// We do not support the following legacy Direct3D 9 formats:
// BumpDuDv D3DFMT_A2W10V10U10
// BumpLuminance D3DFMT_L6V5U5, D3DFMT_X8L8V8U8
// FourCC 117 D3DFMT_CxV8U8
// ZBuffer D3DFMT_D16_LOCKABLE
// FourCC 82 D3DFMT_D32F_LOCKABLE
2016-08-22 18:26:36 +00:00
// We do not support the following known FourCC codes:
// FourCC CTX1 (Xbox 360 only)
// FourCC EAR, EARG, ET2, ET2A (Ericsson Texture Compression)
DXGI_FORMAT GetDXGIFormat(const DDS_HEADER& hdr, const DDS_PIXELFORMAT& ddpf, DWORD flags, _Inout_ DWORD& convFlags)
2016-08-22 18:26:36 +00:00
{
2018-02-24 07:46:30 +00:00
uint32_t ddpfFlags = ddpf.flags;
if (hdr.reserved1[9] == MAKEFOURCC('N', 'V', 'T', 'T'))
{
// Clear out non-standard nVidia DDS flags
ddpfFlags &= ~0xC0000000 /* DDPF_SRGB | DDPF_NORMAL */;
}
2016-09-09 02:09:46 +00:00
const size_t MAP_SIZE = sizeof(g_LegacyDDSMap) / sizeof(LegacyDDS);
size_t index = 0;
for (index = 0; index < MAP_SIZE; ++index)
{
const LegacyDDS* entry = &g_LegacyDDSMap[index];
2016-08-22 18:26:36 +00:00
if ((ddpfFlags & DDS_FOURCC) && (entry->ddpf.flags & DDS_FOURCC))
2016-09-09 02:09:46 +00:00
{
// In case of FourCC codes, ignore any other bits in ddpf.flags
if (ddpf.fourCC == entry->ddpf.fourCC)
break;
}
else if (ddpfFlags == entry->ddpf.flags)
{
if (entry->ddpf.flags & DDS_PAL8)
2016-09-09 02:09:46 +00:00
{
2018-02-24 07:46:30 +00:00
if (ddpf.RGBBitCount == entry->ddpf.RGBBitCount)
2016-09-09 02:09:46 +00:00
break;
}
2018-02-24 07:46:30 +00:00
else if (entry->ddpf.flags & DDS_ALPHA)
2016-11-29 02:09:22 +00:00
{
2018-02-24 07:46:30 +00:00
if (ddpf.RGBBitCount == entry->ddpf.RGBBitCount
&& ddpf.ABitMask == entry->ddpf.ABitMask)
2016-11-29 02:09:22 +00:00
break;
}
2018-02-24 07:46:30 +00:00
else if (entry->ddpf.flags & DDS_LUMINANCE)
2016-09-09 02:09:46 +00:00
{
2018-02-24 07:46:30 +00:00
if (entry->ddpf.flags & DDS_ALPHAPIXELS)
2016-11-29 02:09:22 +00:00
{
// LUMINANCEA
2018-02-24 07:46:30 +00:00
if (ddpf.RGBBitCount == entry->ddpf.RGBBitCount
&& ddpf.RBitMask == entry->ddpf.RBitMask
&& ddpf.ABitMask == entry->ddpf.ABitMask)
2016-11-29 02:09:22 +00:00
break;
}
else
{
// LUMINANCE
2018-02-24 07:46:30 +00:00
if (ddpf.RGBBitCount == entry->ddpf.RGBBitCount
&& ddpf.RBitMask == entry->ddpf.RBitMask)
2016-11-29 02:09:22 +00:00
break;
}
}
2018-02-24 07:46:30 +00:00
else if (entry->ddpf.flags & DDS_BUMPDUDV)
2016-11-29 02:09:22 +00:00
{
2018-02-24 07:46:30 +00:00
if (ddpf.RGBBitCount == entry->ddpf.RGBBitCount
&& ddpf.RBitMask == entry->ddpf.RBitMask
&& ddpf.GBitMask == entry->ddpf.GBitMask
&& ddpf.BBitMask == entry->ddpf.BBitMask
&& ddpf.ABitMask == entry->ddpf.ABitMask)
2016-09-09 02:09:46 +00:00
break;
}
2018-02-24 07:46:30 +00:00
else if (ddpf.RGBBitCount == entry->ddpf.RGBBitCount)
2016-11-29 02:09:22 +00:00
{
2018-02-24 07:46:30 +00:00
if (entry->ddpf.flags & DDS_ALPHAPIXELS)
2016-11-29 02:09:22 +00:00
{
// RGBA
2018-02-24 07:46:30 +00:00
if (ddpf.RBitMask == entry->ddpf.RBitMask
&& ddpf.GBitMask == entry->ddpf.GBitMask
&& ddpf.BBitMask == entry->ddpf.BBitMask
&& ddpf.ABitMask == entry->ddpf.ABitMask)
2016-11-29 02:09:22 +00:00
break;
}
else
{
// RGB
2018-02-24 07:46:30 +00:00
if (ddpf.RBitMask == entry->ddpf.RBitMask
&& ddpf.GBitMask == entry->ddpf.GBitMask
&& ddpf.BBitMask == entry->ddpf.BBitMask)
2016-11-29 02:09:22 +00:00
break;
}
}
2016-09-09 02:09:46 +00:00
}
}
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
if (index >= MAP_SIZE)
return DXGI_FORMAT_UNKNOWN;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
DWORD cflags = g_LegacyDDSMap[index].convFlags;
DXGI_FORMAT format = g_LegacyDDSMap[index].format;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
if ((cflags & CONV_FLAGS_EXPAND) && (flags & DDS_FLAGS_NO_LEGACY_EXPANSION))
return DXGI_FORMAT_UNKNOWN;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
if ((format == DXGI_FORMAT_R10G10B10A2_UNORM) && (flags & DDS_FLAGS_NO_R10B10G10A2_FIXUP))
{
cflags ^= CONV_FLAGS_SWIZZLE;
}
2016-08-22 18:26:36 +00:00
2018-02-24 07:46:30 +00:00
if ((hdr.reserved1[9] == MAKEFOURCC('N', 'V', 'T', 'T'))
&& (ddpf.flags & 0x40000000 /* DDPF_SRGB */))
{
format = MakeSRGB(format);
}
2016-09-09 02:09:46 +00:00
convFlags = cflags;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
return format;
2016-08-22 18:26:36 +00:00
}
2016-09-09 02:09:46 +00:00
//-------------------------------------------------------------------------------------
// Decodes DDS header including optional DX10 extended header
//-------------------------------------------------------------------------------------
HRESULT DecodeDDSHeader(
_In_reads_bytes_(size) const void* pSource,
size_t size,
DWORD flags,
_Out_ TexMetadata& metadata,
_Inout_ DWORD& convFlags)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
if (!pSource)
return E_INVALIDARG;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
memset(&metadata, 0, sizeof(TexMetadata));
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
if (size < (sizeof(DDS_HEADER) + sizeof(uint32_t)))
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
return HRESULT_FROM_WIN32(ERROR_INVALID_DATA);
2016-08-22 18:26:36 +00:00
}
2016-09-09 02:09:46 +00:00
// DDS files always start with the same magic number ("DDS ")
2018-03-29 19:00:16 +00:00
auto dwMagicNumber = *static_cast<const uint32_t*>(pSource);
2016-09-09 02:09:46 +00:00
if (dwMagicNumber != DDS_MAGIC)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
return E_FAIL;
2016-08-22 18:26:36 +00:00
}
2018-06-13 01:07:34 +00:00
auto pHeader = reinterpret_cast<const DDS_HEADER*>(static_cast<const uint8_t*>(pSource) + sizeof(uint32_t));
2016-09-09 02:09:46 +00:00
// Verify header to validate DDS file
2018-02-24 07:46:30 +00:00
if (pHeader->size != sizeof(DDS_HEADER)
|| pHeader->ddspf.size != sizeof(DDS_PIXELFORMAT))
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
return E_FAIL;
2016-08-22 18:26:36 +00:00
}
2018-02-24 07:46:30 +00:00
metadata.mipLevels = pHeader->mipMapCount;
2016-09-09 02:09:46 +00:00
if (metadata.mipLevels == 0)
metadata.mipLevels = 1;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
// Check for DX10 extension
2018-02-24 07:46:30 +00:00
if ((pHeader->ddspf.flags & DDS_FOURCC)
&& (MAKEFOURCC('D', 'X', '1', '0') == pHeader->ddspf.fourCC))
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
// Buffer must be big enough for both headers and magic value
if (size < (sizeof(DDS_HEADER) + sizeof(uint32_t) + sizeof(DDS_HEADER_DXT10)))
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
return E_FAIL;
2016-08-22 18:26:36 +00:00
}
2018-06-13 01:07:34 +00:00
auto d3d10ext = reinterpret_cast<const DDS_HEADER_DXT10*>(static_cast<const uint8_t*>(pSource) + sizeof(uint32_t) + sizeof(DDS_HEADER));
2016-09-09 02:09:46 +00:00
convFlags |= CONV_FLAGS_DX10;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
metadata.arraySize = d3d10ext->arraySize;
if (metadata.arraySize == 0)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
return HRESULT_FROM_WIN32(ERROR_INVALID_DATA);
2016-08-22 18:26:36 +00:00
}
2016-09-09 02:09:46 +00:00
metadata.format = d3d10ext->dxgiFormat;
if (!IsValid(metadata.format) || IsPalettized(metadata.format))
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED);
2016-08-22 18:26:36 +00:00
}
2017-07-12 07:56:51 +00:00
static_assert(static_cast<int>(TEX_MISC_TEXTURECUBE) == static_cast<int>(DDS_RESOURCE_MISC_TEXTURECUBE), "DDS header mismatch");
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
metadata.miscFlags = d3d10ext->miscFlag & ~TEX_MISC_TEXTURECUBE;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
switch (d3d10ext->resourceDimension)
{
case DDS_DIMENSION_TEXTURE1D:
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
// D3DX writes 1D textures with a fixed Height of 1
2018-02-24 07:46:30 +00:00
if ((pHeader->flags & DDS_HEIGHT) && pHeader->height != 1)
2016-09-09 02:09:46 +00:00
{
return HRESULT_FROM_WIN32(ERROR_INVALID_DATA);
}
2016-08-22 18:26:36 +00:00
2018-02-24 07:46:30 +00:00
metadata.width = pHeader->width;
2016-09-09 02:09:46 +00:00
metadata.height = 1;
metadata.depth = 1;
metadata.dimension = TEX_DIMENSION_TEXTURE1D;
break;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
case DDS_DIMENSION_TEXTURE2D:
if (d3d10ext->miscFlag & DDS_RESOURCE_MISC_TEXTURECUBE)
{
metadata.miscFlags |= TEX_MISC_TEXTURECUBE;
metadata.arraySize *= 6;
}
2016-08-22 18:26:36 +00:00
2018-02-24 07:46:30 +00:00
metadata.width = pHeader->width;
metadata.height = pHeader->height;
2016-09-09 02:09:46 +00:00
metadata.depth = 1;
metadata.dimension = TEX_DIMENSION_TEXTURE2D;
break;
case DDS_DIMENSION_TEXTURE3D:
2018-02-24 07:46:30 +00:00
if (!(pHeader->flags & DDS_HEADER_FLAGS_VOLUME))
2016-09-09 02:09:46 +00:00
{
return HRESULT_FROM_WIN32(ERROR_INVALID_DATA);
}
if (metadata.arraySize > 1)
return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED);
2018-02-24 07:46:30 +00:00
metadata.width = pHeader->width;
metadata.height = pHeader->height;
metadata.depth = pHeader->depth;
2016-09-09 02:09:46 +00:00
metadata.dimension = TEX_DIMENSION_TEXTURE3D;
break;
default:
return HRESULT_FROM_WIN32(ERROR_INVALID_DATA);
}
2017-07-12 07:56:51 +00:00
static_assert(static_cast<int>(TEX_MISC2_ALPHA_MODE_MASK) == static_cast<int>(DDS_MISC_FLAGS2_ALPHA_MODE_MASK), "DDS header mismatch");
2016-09-09 02:09:46 +00:00
2017-07-12 07:56:51 +00:00
static_assert(static_cast<int>(TEX_ALPHA_MODE_UNKNOWN) == static_cast<int>(DDS_ALPHA_MODE_UNKNOWN), "DDS header mismatch");
static_assert(static_cast<int>(TEX_ALPHA_MODE_STRAIGHT) == static_cast<int>(DDS_ALPHA_MODE_STRAIGHT), "DDS header mismatch");
static_assert(static_cast<int>(TEX_ALPHA_MODE_PREMULTIPLIED) == static_cast<int>(DDS_ALPHA_MODE_PREMULTIPLIED), "DDS header mismatch");
static_assert(static_cast<int>(TEX_ALPHA_MODE_OPAQUE) == static_cast<int>(DDS_ALPHA_MODE_OPAQUE), "DDS header mismatch");
static_assert(static_cast<int>(TEX_ALPHA_MODE_CUSTOM) == static_cast<int>(DDS_ALPHA_MODE_CUSTOM), "DDS header mismatch");
2016-09-09 02:09:46 +00:00
metadata.miscFlags2 = d3d10ext->miscFlags2;
2016-08-22 18:26:36 +00:00
}
2016-09-09 02:09:46 +00:00
else
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
metadata.arraySize = 1;
2018-02-24 07:46:30 +00:00
if (pHeader->flags & DDS_HEADER_FLAGS_VOLUME)
2016-09-09 02:09:46 +00:00
{
2018-02-24 07:46:30 +00:00
metadata.width = pHeader->width;
metadata.height = pHeader->height;
metadata.depth = pHeader->depth;
2016-09-09 02:09:46 +00:00
metadata.dimension = TEX_DIMENSION_TEXTURE3D;
}
else
2016-08-22 18:26:36 +00:00
{
2018-02-24 07:46:30 +00:00
if (pHeader->caps2 & DDS_CUBEMAP)
2016-09-09 02:09:46 +00:00
{
// We require all six faces to be defined
2018-02-24 07:46:30 +00:00
if ((pHeader->caps2 & DDS_CUBEMAP_ALLFACES) != DDS_CUBEMAP_ALLFACES)
2016-09-09 02:09:46 +00:00
return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED);
metadata.arraySize = 6;
metadata.miscFlags |= TEX_MISC_TEXTURECUBE;
}
2018-02-24 07:46:30 +00:00
metadata.width = pHeader->width;
metadata.height = pHeader->height;
2016-09-09 02:09:46 +00:00
metadata.depth = 1;
metadata.dimension = TEX_DIMENSION_TEXTURE2D;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
// Note there's no way for a legacy Direct3D 9 DDS to express a '1D' texture
2016-08-22 18:26:36 +00:00
}
metadata.format = GetDXGIFormat(*pHeader, pHeader->ddspf, flags, convFlags);
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
if (metadata.format == DXGI_FORMAT_UNKNOWN)
return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED);
if (convFlags & CONV_FLAGS_PMALPHA)
metadata.miscFlags2 |= TEX_ALPHA_MODE_PREMULTIPLIED;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
// Special flag for handling LUMINANCE legacy formats
if (flags & DDS_FLAGS_EXPAND_LUMINANCE)
{
switch (metadata.format)
{
case DXGI_FORMAT_R8_UNORM:
metadata.format = DXGI_FORMAT_R8G8B8A8_UNORM;
convFlags |= CONV_FLAGS_L8 | CONV_FLAGS_EXPAND;
break;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
case DXGI_FORMAT_R8G8_UNORM:
metadata.format = DXGI_FORMAT_R8G8B8A8_UNORM;
convFlags |= CONV_FLAGS_A8L8 | CONV_FLAGS_EXPAND;
break;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
case DXGI_FORMAT_R16_UNORM:
metadata.format = DXGI_FORMAT_R16G16B16A16_UNORM;
convFlags |= CONV_FLAGS_L16 | CONV_FLAGS_EXPAND;
break;
2017-07-12 07:56:51 +00:00
default:
break;
2016-09-09 02:09:46 +00:00
}
}
}
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
// Special flag for handling BGR DXGI 1.1 formats
if (flags & DDS_FLAGS_FORCE_RGB)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
switch (metadata.format)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
case DXGI_FORMAT_B8G8R8A8_UNORM:
2016-08-22 18:26:36 +00:00
metadata.format = DXGI_FORMAT_R8G8B8A8_UNORM;
2016-09-09 02:09:46 +00:00
convFlags |= CONV_FLAGS_SWIZZLE;
2016-08-22 18:26:36 +00:00
break;
2016-09-09 02:09:46 +00:00
case DXGI_FORMAT_B8G8R8X8_UNORM:
2016-08-22 18:26:36 +00:00
metadata.format = DXGI_FORMAT_R8G8B8A8_UNORM;
2016-09-09 02:09:46 +00:00
convFlags |= CONV_FLAGS_SWIZZLE | CONV_FLAGS_NOALPHA;
2016-08-22 18:26:36 +00:00
break;
2016-09-09 02:09:46 +00:00
case DXGI_FORMAT_B8G8R8A8_TYPELESS:
metadata.format = DXGI_FORMAT_R8G8B8A8_TYPELESS;
convFlags |= CONV_FLAGS_SWIZZLE;
2016-08-22 18:26:36 +00:00
break;
2016-09-09 02:09:46 +00:00
case DXGI_FORMAT_B8G8R8A8_UNORM_SRGB:
metadata.format = DXGI_FORMAT_R8G8B8A8_UNORM_SRGB;
convFlags |= CONV_FLAGS_SWIZZLE;
break;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
case DXGI_FORMAT_B8G8R8X8_TYPELESS:
metadata.format = DXGI_FORMAT_R8G8B8A8_TYPELESS;
convFlags |= CONV_FLAGS_SWIZZLE | CONV_FLAGS_NOALPHA;
break;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
case DXGI_FORMAT_B8G8R8X8_UNORM_SRGB:
metadata.format = DXGI_FORMAT_R8G8B8A8_UNORM_SRGB;
convFlags |= CONV_FLAGS_SWIZZLE | CONV_FLAGS_NOALPHA;
break;
2017-07-12 07:56:51 +00:00
default:
break;
2016-09-09 02:09:46 +00:00
}
2016-08-22 18:26:36 +00:00
}
2016-09-09 02:09:46 +00:00
// Special flag for handling 16bpp formats
if (flags & DDS_FLAGS_NO_16BPP)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
switch (metadata.format)
{
case DXGI_FORMAT_B5G6R5_UNORM:
case DXGI_FORMAT_B5G5R5A1_UNORM:
case DXGI_FORMAT_B4G4R4A4_UNORM:
metadata.format = DXGI_FORMAT_R8G8B8A8_UNORM;
convFlags |= CONV_FLAGS_EXPAND;
if (metadata.format == DXGI_FORMAT_B5G6R5_UNORM)
convFlags |= CONV_FLAGS_NOALPHA;
2018-06-13 01:07:34 +00:00
break;
2017-07-12 07:56:51 +00:00
default:
break;
2016-09-09 02:09:46 +00:00
}
2016-08-22 18:26:36 +00:00
}
2016-09-09 02:09:46 +00:00
return S_OK;
}
2016-08-22 18:26:36 +00:00
}
//-------------------------------------------------------------------------------------
// Encodes DDS file header (magic value, header, optional DX10 extended header)
//-------------------------------------------------------------------------------------
_Use_decl_annotations_
2016-09-09 02:09:46 +00:00
HRESULT DirectX::_EncodeDDSHeader(
const TexMetadata& metadata,
DWORD flags,
void* pDestination,
size_t maxsize,
size_t& required)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
if (!IsValid(metadata.format))
2016-08-22 18:26:36 +00:00
return E_INVALIDARG;
2016-09-09 02:09:46 +00:00
if (IsPalettized(metadata.format))
return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED);
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
if (metadata.arraySize > 1)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
if ((metadata.arraySize != 6) || (metadata.dimension != TEX_DIMENSION_TEXTURE2D) || !(metadata.IsCubemap()))
2016-08-22 18:26:36 +00:00
{
// Texture1D arrays, Texture2D arrays, and Cubemap arrays must be stored using 'DX10' extended header
flags |= DDS_FLAGS_FORCE_DX10_EXT;
}
}
2016-09-09 02:09:46 +00:00
if (flags & DDS_FLAGS_FORCE_DX10_EXT_MISC2)
2016-08-22 18:26:36 +00:00
{
flags |= DDS_FLAGS_FORCE_DX10_EXT;
}
2018-01-18 23:02:05 +00:00
DDS_PIXELFORMAT ddpf = {};
2016-09-09 02:09:46 +00:00
if (!(flags & DDS_FLAGS_FORCE_DX10_EXT))
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
switch (metadata.format)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
case DXGI_FORMAT_R8G8B8A8_UNORM: memcpy_s(&ddpf, sizeof(ddpf), &DDSPF_A8B8G8R8, sizeof(DDS_PIXELFORMAT)); break;
case DXGI_FORMAT_R16G16_UNORM: memcpy_s(&ddpf, sizeof(ddpf), &DDSPF_G16R16, sizeof(DDS_PIXELFORMAT)); break;
case DXGI_FORMAT_R8G8_UNORM: memcpy_s(&ddpf, sizeof(ddpf), &DDSPF_A8L8, sizeof(DDS_PIXELFORMAT)); break;
case DXGI_FORMAT_R16_UNORM: memcpy_s(&ddpf, sizeof(ddpf), &DDSPF_L16, sizeof(DDS_PIXELFORMAT)); break;
case DXGI_FORMAT_R8_UNORM: memcpy_s(&ddpf, sizeof(ddpf), &DDSPF_L8, sizeof(DDS_PIXELFORMAT)); break;
case DXGI_FORMAT_A8_UNORM: memcpy_s(&ddpf, sizeof(ddpf), &DDSPF_A8, sizeof(DDS_PIXELFORMAT)); break;
case DXGI_FORMAT_R8G8_B8G8_UNORM: memcpy_s(&ddpf, sizeof(ddpf), &DDSPF_R8G8_B8G8, sizeof(DDS_PIXELFORMAT)); break;
case DXGI_FORMAT_G8R8_G8B8_UNORM: memcpy_s(&ddpf, sizeof(ddpf), &DDSPF_G8R8_G8B8, sizeof(DDS_PIXELFORMAT)); break;
case DXGI_FORMAT_BC1_UNORM: memcpy_s(&ddpf, sizeof(ddpf), &DDSPF_DXT1, sizeof(DDS_PIXELFORMAT)); break;
case DXGI_FORMAT_BC2_UNORM: memcpy_s(&ddpf, sizeof(ddpf), metadata.IsPMAlpha() ? (&DDSPF_DXT2) : (&DDSPF_DXT3), sizeof(DDS_PIXELFORMAT)); break;
case DXGI_FORMAT_BC3_UNORM: memcpy_s(&ddpf, sizeof(ddpf), metadata.IsPMAlpha() ? (&DDSPF_DXT4) : (&DDSPF_DXT5), sizeof(DDS_PIXELFORMAT)); break;
case DXGI_FORMAT_BC4_UNORM: memcpy_s(&ddpf, sizeof(ddpf), &DDSPF_BC4_UNORM, sizeof(DDS_PIXELFORMAT)); break;
case DXGI_FORMAT_BC4_SNORM: memcpy_s(&ddpf, sizeof(ddpf), &DDSPF_BC4_SNORM, sizeof(DDS_PIXELFORMAT)); break;
case DXGI_FORMAT_BC5_UNORM: memcpy_s(&ddpf, sizeof(ddpf), &DDSPF_BC5_UNORM, sizeof(DDS_PIXELFORMAT)); break;
case DXGI_FORMAT_BC5_SNORM: memcpy_s(&ddpf, sizeof(ddpf), &DDSPF_BC5_SNORM, sizeof(DDS_PIXELFORMAT)); break;
case DXGI_FORMAT_B5G6R5_UNORM: memcpy_s(&ddpf, sizeof(ddpf), &DDSPF_R5G6B5, sizeof(DDS_PIXELFORMAT)); break;
case DXGI_FORMAT_B5G5R5A1_UNORM: memcpy_s(&ddpf, sizeof(ddpf), &DDSPF_A1R5G5B5, sizeof(DDS_PIXELFORMAT)); break;
case DXGI_FORMAT_R8G8_SNORM: memcpy_s(&ddpf, sizeof(ddpf), &DDSPF_V8U8, sizeof(DDS_PIXELFORMAT)); break;
case DXGI_FORMAT_R8G8B8A8_SNORM: memcpy_s(&ddpf, sizeof(ddpf), &DDSPF_Q8W8V8U8, sizeof(DDS_PIXELFORMAT)); break;
case DXGI_FORMAT_R16G16_SNORM: memcpy_s(&ddpf, sizeof(ddpf), &DDSPF_V16U16, sizeof(DDS_PIXELFORMAT)); break;
case DXGI_FORMAT_B8G8R8A8_UNORM: memcpy_s(&ddpf, sizeof(ddpf), &DDSPF_A8R8G8B8, sizeof(DDS_PIXELFORMAT)); break; // DXGI 1.1
case DXGI_FORMAT_B8G8R8X8_UNORM: memcpy_s(&ddpf, sizeof(ddpf), &DDSPF_X8R8G8B8, sizeof(DDS_PIXELFORMAT)); break; // DXGI 1.1
case DXGI_FORMAT_B4G4R4A4_UNORM: memcpy_s(&ddpf, sizeof(ddpf), &DDSPF_A4R4G4B4, sizeof(DDS_PIXELFORMAT)); break; // DXGI 1.2
case DXGI_FORMAT_YUY2: memcpy_s(&ddpf, sizeof(ddpf), &DDSPF_YUY2, sizeof(DDS_PIXELFORMAT)); break; // DXGI 1.2
2016-08-22 18:26:36 +00:00
// Legacy D3DX formats using D3DFMT enum value as FourCC
case DXGI_FORMAT_R32G32B32A32_FLOAT:
2018-02-24 07:46:30 +00:00
ddpf.size = sizeof(DDS_PIXELFORMAT); ddpf.flags = DDS_FOURCC; ddpf.fourCC = 116; // D3DFMT_A32B32G32R32F
2016-08-22 18:26:36 +00:00
break;
case DXGI_FORMAT_R16G16B16A16_FLOAT:
2018-02-24 07:46:30 +00:00
ddpf.size = sizeof(DDS_PIXELFORMAT); ddpf.flags = DDS_FOURCC; ddpf.fourCC = 113; // D3DFMT_A16B16G16R16F
2016-08-22 18:26:36 +00:00
break;
case DXGI_FORMAT_R16G16B16A16_UNORM:
2018-02-24 07:46:30 +00:00
ddpf.size = sizeof(DDS_PIXELFORMAT); ddpf.flags = DDS_FOURCC; ddpf.fourCC = 36; // D3DFMT_A16B16G16R16
2016-08-22 18:26:36 +00:00
break;
case DXGI_FORMAT_R16G16B16A16_SNORM:
2018-02-24 07:46:30 +00:00
ddpf.size = sizeof(DDS_PIXELFORMAT); ddpf.flags = DDS_FOURCC; ddpf.fourCC = 110; // D3DFMT_Q16W16V16U16
2016-08-22 18:26:36 +00:00
break;
case DXGI_FORMAT_R32G32_FLOAT:
2018-02-24 07:46:30 +00:00
ddpf.size = sizeof(DDS_PIXELFORMAT); ddpf.flags = DDS_FOURCC; ddpf.fourCC = 115; // D3DFMT_G32R32F
2016-08-22 18:26:36 +00:00
break;
2016-09-09 02:09:46 +00:00
case DXGI_FORMAT_R16G16_FLOAT:
2018-02-24 07:46:30 +00:00
ddpf.size = sizeof(DDS_PIXELFORMAT); ddpf.flags = DDS_FOURCC; ddpf.fourCC = 112; // D3DFMT_G16R16F
2016-08-22 18:26:36 +00:00
break;
case DXGI_FORMAT_R32_FLOAT:
2018-02-24 07:46:30 +00:00
ddpf.size = sizeof(DDS_PIXELFORMAT); ddpf.flags = DDS_FOURCC; ddpf.fourCC = 114; // D3DFMT_R32F
2016-08-22 18:26:36 +00:00
break;
case DXGI_FORMAT_R16_FLOAT:
2018-02-24 07:46:30 +00:00
ddpf.size = sizeof(DDS_PIXELFORMAT); ddpf.flags = DDS_FOURCC; ddpf.fourCC = 111; // D3DFMT_R16F
2016-08-22 18:26:36 +00:00
break;
2017-07-12 07:56:51 +00:00
default:
break;
2016-08-22 18:26:36 +00:00
}
}
required = sizeof(uint32_t) + sizeof(DDS_HEADER);
2018-02-24 07:46:30 +00:00
if (ddpf.size == 0)
2016-08-22 18:26:36 +00:00
required += sizeof(DDS_HEADER_DXT10);
2016-09-09 02:09:46 +00:00
if (!pDestination)
2016-08-22 18:26:36 +00:00
return S_OK;
2016-09-09 02:09:46 +00:00
if (maxsize < required)
2016-08-22 18:26:36 +00:00
return E_NOT_SUFFICIENT_BUFFER;
2018-03-29 19:00:16 +00:00
*static_cast<uint32_t*>(pDestination) = DDS_MAGIC;
2016-08-22 18:26:36 +00:00
2018-03-29 19:00:16 +00:00
auto header = reinterpret_cast<DDS_HEADER*>(static_cast<uint8_t*>(pDestination) + sizeof(uint32_t));
2016-09-09 02:09:46 +00:00
assert(header);
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
memset(header, 0, sizeof(DDS_HEADER));
2018-02-24 07:46:30 +00:00
header->size = sizeof(DDS_HEADER);
header->flags = DDS_HEADER_FLAGS_TEXTURE;
header->caps = DDS_SURFACE_FLAGS_TEXTURE;
2016-08-22 18:26:36 +00:00
if (metadata.mipLevels > 0)
{
2018-02-24 07:46:30 +00:00
header->flags |= DDS_HEADER_FLAGS_MIPMAP;
2016-08-22 18:26:36 +00:00
2017-01-07 01:16:43 +00:00
if (metadata.mipLevels > UINT16_MAX)
2016-08-22 18:26:36 +00:00
return E_INVALIDARG;
2018-02-24 07:46:30 +00:00
header->mipMapCount = static_cast<uint32_t>(metadata.mipLevels);
2016-08-22 18:26:36 +00:00
2018-02-24 07:46:30 +00:00
if (header->mipMapCount > 1)
header->caps |= DDS_SURFACE_FLAGS_MIPMAP;
2016-08-22 18:26:36 +00:00
}
2016-09-09 02:09:46 +00:00
switch (metadata.dimension)
2016-08-22 18:26:36 +00:00
{
case TEX_DIMENSION_TEXTURE1D:
2016-09-09 02:09:46 +00:00
if (metadata.width > UINT32_MAX)
2016-08-22 18:26:36 +00:00
return E_INVALIDARG;
2018-02-24 07:46:30 +00:00
header->width = static_cast<uint32_t>(metadata.width);
header->height = header->depth = 1;
2016-08-22 18:26:36 +00:00
break;
case TEX_DIMENSION_TEXTURE2D:
2016-09-09 02:09:46 +00:00
if (metadata.height > UINT32_MAX
|| metadata.width > UINT32_MAX)
2016-08-22 18:26:36 +00:00
return E_INVALIDARG;
2018-02-24 07:46:30 +00:00
header->height = static_cast<uint32_t>(metadata.height);
header->width = static_cast<uint32_t>(metadata.width);
header->depth = 1;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
if (metadata.IsCubemap())
2016-08-22 18:26:36 +00:00
{
2018-02-24 07:46:30 +00:00
header->caps |= DDS_SURFACE_FLAGS_CUBEMAP;
header->caps2 |= DDS_CUBEMAP_ALLFACES;
2016-08-22 18:26:36 +00:00
}
break;
case TEX_DIMENSION_TEXTURE3D:
2016-09-09 02:09:46 +00:00
if (metadata.height > UINT32_MAX
|| metadata.width > UINT32_MAX
2017-01-07 01:16:43 +00:00
|| metadata.depth > UINT16_MAX)
2016-08-22 18:26:36 +00:00
return E_INVALIDARG;
2018-02-24 07:46:30 +00:00
header->flags |= DDS_HEADER_FLAGS_VOLUME;
header->caps2 |= DDS_FLAGS_VOLUME;
header->height = static_cast<uint32_t>(metadata.height);
header->width = static_cast<uint32_t>(metadata.width);
header->depth = static_cast<uint32_t>(metadata.depth);
2016-08-22 18:26:36 +00:00
break;
default:
return E_FAIL;
}
size_t rowPitch, slicePitch;
HRESULT hr = ComputePitch(metadata.format, metadata.width, metadata.height, rowPitch, slicePitch, CP_FLAGS_NONE);
if (FAILED(hr))
return hr;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
if (slicePitch > UINT32_MAX
|| rowPitch > UINT32_MAX)
2016-08-22 18:26:36 +00:00
return E_FAIL;
2016-09-09 02:09:46 +00:00
if (IsCompressed(metadata.format))
2016-08-22 18:26:36 +00:00
{
2018-02-24 07:46:30 +00:00
header->flags |= DDS_HEADER_FLAGS_LINEARSIZE;
header->pitchOrLinearSize = static_cast<uint32_t>(slicePitch);
2016-08-22 18:26:36 +00:00
}
else
{
2018-02-24 07:46:30 +00:00
header->flags |= DDS_HEADER_FLAGS_PITCH;
header->pitchOrLinearSize = static_cast<uint32_t>(rowPitch);
2016-08-22 18:26:36 +00:00
}
2018-02-24 07:46:30 +00:00
if (ddpf.size == 0)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
memcpy_s(&header->ddspf, sizeof(header->ddspf), &DDSPF_DX10, sizeof(DDS_PIXELFORMAT));
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
auto ext = reinterpret_cast<DDS_HEADER_DXT10*>(reinterpret_cast<uint8_t*>(header) + sizeof(DDS_HEADER));
assert(ext);
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
memset(ext, 0, sizeof(DDS_HEADER_DXT10));
2016-08-22 18:26:36 +00:00
ext->dxgiFormat = metadata.format;
ext->resourceDimension = metadata.dimension;
2017-01-07 01:16:43 +00:00
if (metadata.arraySize > UINT16_MAX)
2016-08-22 18:26:36 +00:00
return E_INVALIDARG;
2017-07-12 07:56:51 +00:00
static_assert(static_cast<int>(TEX_MISC_TEXTURECUBE) == static_cast<int>(DDS_RESOURCE_MISC_TEXTURECUBE), "DDS header mismatch");
2016-09-09 02:09:46 +00:00
2016-08-22 18:26:36 +00:00
ext->miscFlag = metadata.miscFlags & ~TEX_MISC_TEXTURECUBE;
2016-09-09 02:09:46 +00:00
if (metadata.miscFlags & TEX_MISC_TEXTURECUBE)
2016-08-22 18:26:36 +00:00
{
ext->miscFlag |= TEX_MISC_TEXTURECUBE;
2016-09-09 02:09:46 +00:00
assert((metadata.arraySize % 6) == 0);
ext->arraySize = static_cast<UINT>(metadata.arraySize / 6);
2016-08-22 18:26:36 +00:00
}
else
{
2016-09-09 02:09:46 +00:00
ext->arraySize = static_cast<UINT>(metadata.arraySize);
2016-08-22 18:26:36 +00:00
}
2017-07-12 07:56:51 +00:00
static_assert(static_cast<int>(TEX_MISC2_ALPHA_MODE_MASK) == static_cast<int>(DDS_MISC_FLAGS2_ALPHA_MODE_MASK), "DDS header mismatch");
2016-08-22 18:26:36 +00:00
2017-07-12 07:56:51 +00:00
static_assert(static_cast<int>(TEX_ALPHA_MODE_UNKNOWN) == static_cast<int>(DDS_ALPHA_MODE_UNKNOWN), "DDS header mismatch");
static_assert(static_cast<int>(TEX_ALPHA_MODE_STRAIGHT) == static_cast<int>(DDS_ALPHA_MODE_STRAIGHT), "DDS header mismatch");
static_assert(static_cast<int>(TEX_ALPHA_MODE_PREMULTIPLIED) == static_cast<int>(DDS_ALPHA_MODE_PREMULTIPLIED), "DDS header mismatch");
static_assert(static_cast<int>(TEX_ALPHA_MODE_OPAQUE) == static_cast<int>(DDS_ALPHA_MODE_OPAQUE), "DDS header mismatch");
static_assert(static_cast<int>(TEX_ALPHA_MODE_CUSTOM) == static_cast<int>(DDS_ALPHA_MODE_CUSTOM), "DDS header mismatch");
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
if (flags & DDS_FLAGS_FORCE_DX10_EXT_MISC2)
2016-08-22 18:26:36 +00:00
{
// This was formerly 'reserved'. D3DX10 and D3DX11 will fail if this value is anything other than 0
ext->miscFlags2 = metadata.miscFlags2;
}
}
else
{
2016-09-09 02:09:46 +00:00
memcpy_s(&header->ddspf, sizeof(header->ddspf), &ddpf, sizeof(ddpf));
2016-08-22 18:26:36 +00:00
}
return S_OK;
}
2016-09-09 02:09:46 +00:00
namespace
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
//-------------------------------------------------------------------------------------
// Converts an image row with optional clearing of alpha value to 1.0
// Returns true if supported, false if expansion case not supported
//-------------------------------------------------------------------------------------
enum TEXP_LEGACY_FORMAT
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
TEXP_LEGACY_UNKNOWN = 0,
TEXP_LEGACY_R8G8B8,
TEXP_LEGACY_R3G3B2,
TEXP_LEGACY_A8R3G3B2,
TEXP_LEGACY_P8,
TEXP_LEGACY_A8P8,
TEXP_LEGACY_A4L4,
TEXP_LEGACY_B4G4R4A4,
TEXP_LEGACY_L8,
TEXP_LEGACY_L16,
TEXP_LEGACY_A8L8
};
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
inline TEXP_LEGACY_FORMAT _FindLegacyFormat(DWORD flags)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
TEXP_LEGACY_FORMAT lformat = TEXP_LEGACY_UNKNOWN;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
if (flags & CONV_FLAGS_PAL8)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
lformat = (flags & CONV_FLAGS_A8P8) ? TEXP_LEGACY_A8P8 : TEXP_LEGACY_P8;
2016-08-22 18:26:36 +00:00
}
2016-09-09 02:09:46 +00:00
else if (flags & CONV_FLAGS_888)
lformat = TEXP_LEGACY_R8G8B8;
else if (flags & CONV_FLAGS_332)
lformat = TEXP_LEGACY_R3G3B2;
else if (flags & CONV_FLAGS_8332)
lformat = TEXP_LEGACY_A8R3G3B2;
else if (flags & CONV_FLAGS_44)
lformat = TEXP_LEGACY_A4L4;
else if (flags & CONV_FLAGS_4444)
lformat = TEXP_LEGACY_B4G4R4A4;
else if (flags & CONV_FLAGS_L8)
lformat = TEXP_LEGACY_L8;
else if (flags & CONV_FLAGS_L16)
lformat = TEXP_LEGACY_L16;
else if (flags & CONV_FLAGS_A8L8)
lformat = TEXP_LEGACY_A8L8;
return lformat;
}
_Success_(return != false)
bool LegacyExpandScanline(
_Out_writes_bytes_(outSize) void* pDestination,
size_t outSize,
_In_ DXGI_FORMAT outFormat,
_In_reads_bytes_(inSize) const void* pSource,
size_t inSize,
_In_ TEXP_LEGACY_FORMAT inFormat,
_In_reads_opt_(256) const uint32_t* pal8,
_In_ DWORD flags)
{
assert(pDestination && outSize > 0);
assert(pSource && inSize > 0);
assert(IsValid(outFormat) && !IsPlanar(outFormat) && !IsPalettized(outFormat));
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
switch (inFormat)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
case TEXP_LEGACY_R8G8B8:
if (outFormat != DXGI_FORMAT_R8G8B8A8_UNORM)
return false;
// D3DFMT_R8G8B8 -> DXGI_FORMAT_R8G8B8A8_UNORM
if (inSize >= 3 && outSize >= 4)
2016-08-22 18:26:36 +00:00
{
2018-03-29 19:00:16 +00:00
const uint8_t * __restrict sPtr = static_cast<const uint8_t*>(pSource);
uint32_t * __restrict dPtr = static_cast<uint32_t*>(pDestination);
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
for (size_t ocount = 0, icount = 0; ((icount < (inSize - 2)) && (ocount < (outSize - 3))); icount += 3, ocount += 4)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
// 24bpp Direct3D 9 files are actually BGR, so need to swizzle as well
uint32_t t1 = (*(sPtr) << 16);
uint32_t t2 = (*(sPtr + 1) << 8);
uint32_t t3 = *(sPtr + 2);
2016-08-22 18:26:36 +00:00
*(dPtr++) = t1 | t2 | t3 | 0xff000000;
2016-09-09 02:09:46 +00:00
sPtr += 3;
2016-08-22 18:26:36 +00:00
}
return true;
}
return false;
2016-09-09 02:09:46 +00:00
case TEXP_LEGACY_R3G3B2:
switch (outFormat)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
case DXGI_FORMAT_R8G8B8A8_UNORM:
// D3DFMT_R3G3B2 -> DXGI_FORMAT_R8G8B8A8_UNORM
if (inSize >= 1 && outSize >= 4)
2016-08-22 18:26:36 +00:00
{
2018-03-29 19:00:16 +00:00
const uint8_t* __restrict sPtr = static_cast<const uint8_t*>(pSource);
uint32_t * __restrict dPtr = static_cast<uint32_t*>(pDestination);
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
for (size_t ocount = 0, icount = 0; ((icount < inSize) && (ocount < (outSize - 3))); ++icount, ocount += 4)
{
uint8_t t = *(sPtr++);
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
uint32_t t1 = (t & 0xe0) | ((t & 0xe0) >> 3) | ((t & 0xc0) >> 6);
uint32_t t2 = ((t & 0x1c) << 11) | ((t & 0x1c) << 8) | ((t & 0x18) << 5);
uint32_t t3 = ((t & 0x03) << 22) | ((t & 0x03) << 20) | ((t & 0x03) << 18) | ((t & 0x03) << 16);
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
*(dPtr++) = t1 | t2 | t3 | 0xff000000;
}
return true;
}
return false;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
case DXGI_FORMAT_B5G6R5_UNORM:
// D3DFMT_R3G3B2 -> DXGI_FORMAT_B5G6R5_UNORM
if (inSize >= 1 && outSize >= 2)
{
2018-03-29 19:00:16 +00:00
const uint8_t* __restrict sPtr = static_cast<const uint8_t*>(pSource);
uint16_t * __restrict dPtr = static_cast<uint16_t*>(pDestination);
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
for (size_t ocount = 0, icount = 0; ((icount < inSize) && (ocount < (outSize - 1))); ++icount, ocount += 2)
{
2018-06-13 01:07:34 +00:00
unsigned t = *(sPtr++);
2016-08-22 18:26:36 +00:00
2018-06-13 01:07:34 +00:00
unsigned t1 = ((t & 0xe0u) << 8) | ((t & 0xc0u) << 5);
unsigned t2 = ((t & 0x1cu) << 6) | ((t & 0x1cu) << 3);
unsigned t3 = ((t & 0x03u) << 3) | ((t & 0x03u) << 1) | ((t & 0x02) >> 1);
2016-08-22 18:26:36 +00:00
2018-06-13 01:07:34 +00:00
*(dPtr++) = static_cast<uint16_t>(t1 | t2 | t3);
2016-09-09 02:09:46 +00:00
}
return true;
}
return false;
2017-07-12 07:56:51 +00:00
default:
return false;
2016-08-22 18:26:36 +00:00
}
2016-09-09 02:09:46 +00:00
case TEXP_LEGACY_A8R3G3B2:
if (outFormat != DXGI_FORMAT_R8G8B8A8_UNORM)
return false;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
// D3DFMT_A8R3G3B2 -> DXGI_FORMAT_R8G8B8A8_UNORM
if (inSize >= 2 && outSize >= 4)
2016-08-22 18:26:36 +00:00
{
2018-03-29 19:00:16 +00:00
const uint16_t* __restrict sPtr = static_cast<const uint16_t*>(pSource);
uint32_t * __restrict dPtr = static_cast<uint32_t*>(pDestination);
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
for (size_t ocount = 0, icount = 0; ((icount < (inSize - 1)) && (ocount < (outSize - 3))); icount += 2, ocount += 4)
{
uint16_t t = *(sPtr++);
uint32_t t1 = (t & 0x00e0) | ((t & 0x00e0) >> 3) | ((t & 0x00c0) >> 6);
uint32_t t2 = ((t & 0x001c) << 11) | ((t & 0x001c) << 8) | ((t & 0x0018) << 5);
uint32_t t3 = ((t & 0x0003) << 22) | ((t & 0x0003) << 20) | ((t & 0x0003) << 18) | ((t & 0x0003) << 16);
uint32_t ta = (flags & TEXP_SCANLINE_SETALPHA) ? 0xff000000 : ((t & 0xff00) << 16);
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
*(dPtr++) = t1 | t2 | t3 | ta;
}
return true;
}
2016-08-22 18:26:36 +00:00
return false;
2016-09-09 02:09:46 +00:00
case TEXP_LEGACY_P8:
if ((outFormat != DXGI_FORMAT_R8G8B8A8_UNORM) || !pal8)
return false;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
// D3DFMT_P8 -> DXGI_FORMAT_R8G8B8A8_UNORM
if (inSize >= 1 && outSize >= 4)
2016-08-22 18:26:36 +00:00
{
2018-03-29 19:00:16 +00:00
const uint8_t* __restrict sPtr = static_cast<const uint8_t*>(pSource);
uint32_t * __restrict dPtr = static_cast<uint32_t*>(pDestination);
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
for (size_t ocount = 0, icount = 0; ((icount < inSize) && (ocount < (outSize - 3))); ++icount, ocount += 4)
{
uint8_t t = *(sPtr++);
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
*(dPtr++) = pal8[t];
}
return true;
2016-08-22 18:26:36 +00:00
}
2016-09-09 02:09:46 +00:00
return false;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
case TEXP_LEGACY_A8P8:
if ((outFormat != DXGI_FORMAT_R8G8B8A8_UNORM) || !pal8)
return false;
// D3DFMT_A8P8 -> DXGI_FORMAT_R8G8B8A8_UNORM
if (inSize >= 2 && outSize >= 4)
2016-08-22 18:26:36 +00:00
{
2018-03-29 19:00:16 +00:00
const uint16_t* __restrict sPtr = static_cast<const uint16_t*>(pSource);
uint32_t * __restrict dPtr = static_cast<uint32_t*>(pDestination);
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
for (size_t ocount = 0, icount = 0; ((icount < (inSize - 1)) && (ocount < (outSize - 3))); icount += 2, ocount += 4)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
uint16_t t = *(sPtr++);
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
uint32_t t1 = pal8[t & 0xff];
uint32_t ta = (flags & TEXP_SCANLINE_SETALPHA) ? 0xff000000 : ((t & 0xff00) << 16);
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
*(dPtr++) = t1 | ta;
2016-08-22 18:26:36 +00:00
}
return true;
}
return false;
2016-09-09 02:09:46 +00:00
case TEXP_LEGACY_A4L4:
switch (outFormat)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
case DXGI_FORMAT_B4G4R4A4_UNORM:
// D3DFMT_A4L4 -> DXGI_FORMAT_B4G4R4A4_UNORM
if (inSize >= 1 && outSize >= 2)
{
2018-03-29 19:00:16 +00:00
const uint8_t * __restrict sPtr = static_cast<const uint8_t*>(pSource);
uint16_t * __restrict dPtr = static_cast<uint16_t*>(pDestination);
2016-09-09 02:09:46 +00:00
for (size_t ocount = 0, icount = 0; ((icount < inSize) && (ocount < (outSize - 1))); ++icount, ocount += 2)
{
2018-06-13 01:07:34 +00:00
unsigned t = *(sPtr++);
2016-09-09 02:09:46 +00:00
2018-06-13 01:07:34 +00:00
unsigned t1 = (t & 0x0fu);
unsigned ta = (flags & TEXP_SCANLINE_SETALPHA) ? 0xf000u : ((t & 0xf0u) << 8);
2016-08-22 18:26:36 +00:00
2018-06-13 01:07:34 +00:00
*(dPtr++) = static_cast<uint16_t>(t1 | (t1 << 4) | (t1 << 8) | ta);
2016-09-09 02:09:46 +00:00
}
return true;
}
return false;
case DXGI_FORMAT_R8G8B8A8_UNORM:
// D3DFMT_A4L4 -> DXGI_FORMAT_R8G8B8A8_UNORM
if (inSize >= 1 && outSize >= 4)
2016-08-22 18:26:36 +00:00
{
2018-03-29 19:00:16 +00:00
const uint8_t * __restrict sPtr = static_cast<const uint8_t*>(pSource);
uint32_t * __restrict dPtr = static_cast<uint32_t*>(pDestination);
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
for (size_t ocount = 0, icount = 0; ((icount < inSize) && (ocount < (outSize - 3))); ++icount, ocount += 4)
{
uint8_t t = *(sPtr++);
uint32_t t1 = ((t & 0x0f) << 4) | (t & 0x0f);
uint32_t ta = (flags & TEXP_SCANLINE_SETALPHA) ? 0xff000000 : (((t & 0xf0) << 24) | ((t & 0xf0) << 20));
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
*(dPtr++) = t1 | (t1 << 8) | (t1 << 16) | ta;
}
return true;
2016-08-22 18:26:36 +00:00
}
2016-09-09 02:09:46 +00:00
return false;
2017-07-12 07:56:51 +00:00
default:
return false;
2016-08-22 18:26:36 +00:00
}
2016-09-09 02:09:46 +00:00
case TEXP_LEGACY_B4G4R4A4:
if (outFormat != DXGI_FORMAT_R8G8B8A8_UNORM)
return false;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
// D3DFMT_A4R4G4B4 -> DXGI_FORMAT_R8G8B8A8_UNORM
if (inSize >= 2 && outSize >= 4)
2016-08-22 18:26:36 +00:00
{
2018-03-29 19:00:16 +00:00
const uint16_t * __restrict sPtr = static_cast<const uint16_t*>(pSource);
uint32_t * __restrict dPtr = static_cast<uint32_t*>(pDestination);
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
for (size_t ocount = 0, icount = 0; ((icount < (inSize - 1)) && (ocount < (outSize - 3))); icount += 2, ocount += 4)
{
uint16_t t = *(sPtr++);
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
uint32_t t1 = ((t & 0x0f00) >> 4) | ((t & 0x0f00) >> 8);
uint32_t t2 = ((t & 0x00f0) << 8) | ((t & 0x00f0) << 4);
uint32_t t3 = ((t & 0x000f) << 20) | ((t & 0x000f) << 16);
uint32_t ta = (flags & TEXP_SCANLINE_SETALPHA) ? 0xff000000 : (((t & 0xf000) << 16) | ((t & 0xf000) << 12));
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
*(dPtr++) = t1 | t2 | t3 | ta;
}
return true;
}
2016-08-22 18:26:36 +00:00
return false;
2016-09-09 02:09:46 +00:00
case TEXP_LEGACY_L8:
if (outFormat != DXGI_FORMAT_R8G8B8A8_UNORM)
return false;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
// D3DFMT_L8 -> DXGI_FORMAT_R8G8B8A8_UNORM
if (inSize >= 1 && outSize >= 4)
2016-08-22 18:26:36 +00:00
{
2018-03-29 19:00:16 +00:00
const uint8_t * __restrict sPtr = static_cast<const uint8_t*>(pSource);
uint32_t * __restrict dPtr = static_cast<uint32_t*>(pDestination);
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
for (size_t ocount = 0, icount = 0; ((icount < inSize) && (ocount < (outSize - 3))); ++icount, ocount += 4)
{
uint32_t t1 = *(sPtr++);
uint32_t t2 = (t1 << 8);
uint32_t t3 = (t1 << 16);
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
*(dPtr++) = t1 | t2 | t3 | 0xff000000;
}
return true;
}
2016-08-22 18:26:36 +00:00
return false;
2016-09-09 02:09:46 +00:00
case TEXP_LEGACY_L16:
if (outFormat != DXGI_FORMAT_R16G16B16A16_UNORM)
return false;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
// D3DFMT_L16 -> DXGI_FORMAT_R16G16B16A16_UNORM
if (inSize >= 2 && outSize >= 8)
2016-08-22 18:26:36 +00:00
{
2018-03-29 19:00:16 +00:00
const uint16_t* __restrict sPtr = static_cast<const uint16_t*>(pSource);
uint64_t * __restrict dPtr = static_cast<uint64_t*>(pDestination);
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
for (size_t ocount = 0, icount = 0; ((icount < (inSize - 1)) && (ocount < (outSize - 7))); icount += 2, ocount += 8)
{
uint16_t t = *(sPtr++);
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
uint64_t t1 = t;
uint64_t t2 = (t1 << 16);
uint64_t t3 = (t1 << 32);
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
*(dPtr++) = t1 | t2 | t3 | 0xffff000000000000;
}
return true;
}
2016-08-22 18:26:36 +00:00
return false;
2016-09-09 02:09:46 +00:00
case TEXP_LEGACY_A8L8:
if (outFormat != DXGI_FORMAT_R8G8B8A8_UNORM)
return false;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
// D3DFMT_A8L8 -> DXGI_FORMAT_R8G8B8A8_UNORM
if (inSize >= 2 && outSize >= 4)
2016-08-22 18:26:36 +00:00
{
2018-03-29 19:00:16 +00:00
const uint16_t* __restrict sPtr = static_cast<const uint16_t*>(pSource);
uint32_t * __restrict dPtr = static_cast<uint32_t*>(pDestination);
2016-09-09 02:09:46 +00:00
for (size_t ocount = 0, icount = 0; ((icount < (inSize - 1)) && (ocount < (outSize - 3))); icount += 2, ocount += 4)
{
uint16_t t = *(sPtr++);
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
uint32_t t1 = (t & 0xff);
uint32_t t2 = (t1 << 8);
uint32_t t3 = (t1 << 16);
uint32_t ta = (flags & TEXP_SCANLINE_SETALPHA) ? 0xff000000 : ((t & 0xff00) << 16);
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
*(dPtr++) = t1 | t2 | t3 | ta;
}
return true;
2016-08-22 18:26:36 +00:00
}
2016-09-09 02:09:46 +00:00
return false;
2017-07-12 07:56:51 +00:00
default:
return false;
}
2016-08-22 18:26:36 +00:00
}
2016-09-09 02:09:46 +00:00
//-------------------------------------------------------------------------------------
// Converts or copies image data from pPixels into scratch image data
//-------------------------------------------------------------------------------------
HRESULT CopyImage(
_In_reads_bytes_(size) const void* pPixels,
_In_ size_t size,
_In_ const TexMetadata& metadata,
_In_ DWORD cpFlags,
_In_ DWORD convFlags,
_In_reads_opt_(256) const uint32_t *pal8,
_In_ const ScratchImage& image)
{
assert(pPixels);
assert(image.GetPixels());
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
if (!size)
return E_FAIL;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
if (convFlags & CONV_FLAGS_EXPAND)
{
if (convFlags & CONV_FLAGS_888)
cpFlags |= CP_FLAGS_24BPP;
else if (convFlags & (CONV_FLAGS_565 | CONV_FLAGS_5551 | CONV_FLAGS_4444 | CONV_FLAGS_8332 | CONV_FLAGS_A8P8 | CONV_FLAGS_L16 | CONV_FLAGS_A8L8))
cpFlags |= CP_FLAGS_16BPP;
else if (convFlags & (CONV_FLAGS_44 | CONV_FLAGS_332 | CONV_FLAGS_PAL8 | CONV_FLAGS_L8))
cpFlags |= CP_FLAGS_8BPP;
}
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
size_t pixelSize, nimages;
if (!_DetermineImageArray(metadata, cpFlags, nimages, pixelSize))
return HRESULT_FROM_WIN32(ERROR_ARITHMETIC_OVERFLOW);
2016-09-09 02:09:46 +00:00
if ((nimages == 0) || (nimages != image.GetImageCount()))
{
return E_FAIL;
}
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
if (pixelSize > size)
{
2018-03-16 19:52:21 +00:00
return HRESULT_FROM_WIN32(ERROR_HANDLE_EOF);
2016-09-09 02:09:46 +00:00
}
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
std::unique_ptr<Image[]> timages(new (std::nothrow) Image[nimages]);
if (!timages)
{
return E_OUTOFMEMORY;
}
2016-08-22 18:26:36 +00:00
2018-06-13 01:07:34 +00:00
if (!_SetupImageArray(
const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(pPixels)),
pixelSize,
metadata,
cpFlags,
timages.get(),
nimages))
2016-09-09 02:09:46 +00:00
{
return E_FAIL;
}
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
if (nimages != image.GetImageCount())
{
return E_FAIL;
}
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
const Image* images = image.GetImages();
if (!images)
{
return E_FAIL;
}
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
DWORD tflags = (convFlags & CONV_FLAGS_NOALPHA) ? TEXP_SCANLINE_SETALPHA : 0;
if (convFlags & CONV_FLAGS_SWIZZLE)
tflags |= TEXP_SCANLINE_LEGACY;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
switch (metadata.dimension)
{
case TEX_DIMENSION_TEXTURE1D:
case TEX_DIMENSION_TEXTURE2D:
2016-08-22 18:26:36 +00:00
{
size_t index = 0;
2016-09-09 02:09:46 +00:00
for (size_t item = 0; item < metadata.arraySize; ++item)
2016-08-22 18:26:36 +00:00
{
size_t lastgood = 0;
2016-09-09 02:09:46 +00:00
for (size_t level = 0; level < metadata.mipLevels; ++level, ++index)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
if (index >= nimages)
2016-08-22 18:26:36 +00:00
return E_FAIL;
2016-09-09 02:09:46 +00:00
if (images[index].height != timages[index].height)
2016-08-22 18:26:36 +00:00
return E_FAIL;
2016-09-09 02:09:46 +00:00
size_t dpitch = images[index].rowPitch;
size_t spitch = timages[index].rowPitch;
2016-08-22 18:26:36 +00:00
2018-03-29 19:00:16 +00:00
const uint8_t *pSrc = timages[index].pixels;
2016-09-09 02:09:46 +00:00
if (!pSrc)
2016-08-22 18:26:36 +00:00
return E_POINTER;
2016-09-09 02:09:46 +00:00
uint8_t *pDest = images[index].pixels;
if (!pDest)
2016-08-22 18:26:36 +00:00
return E_POINTER;
2016-09-09 02:09:46 +00:00
if (IsCompressed(metadata.format))
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
size_t csize = std::min<size_t>(images[index].slicePitch, timages[index].slicePitch);
memcpy_s(pDest, images[index].slicePitch, pSrc, csize);
if (cpFlags & CP_FLAGS_BAD_DXTN_TAILS)
{
if (images[index].width < 4 || images[index].height < 4)
{
csize = std::min<size_t>(images[index].slicePitch, timages[lastgood].slicePitch);
memcpy_s(pDest, images[index].slicePitch, timages[lastgood].pixels, csize);
}
else
{
lastgood = index;
}
}
2016-08-22 18:26:36 +00:00
}
2016-09-09 02:09:46 +00:00
else if (IsPlanar(metadata.format))
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
size_t count = ComputeScanlines(metadata.format, images[index].height);
if (!count)
2016-08-22 18:26:36 +00:00
return E_UNEXPECTED;
2016-09-09 02:09:46 +00:00
size_t csize = std::min<size_t>(dpitch, spitch);
for (size_t h = 0; h < count; ++h)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
memcpy_s(pDest, dpitch, pSrc, csize);
2016-08-22 18:26:36 +00:00
pSrc += spitch;
pDest += dpitch;
}
}
else
{
2016-09-09 02:09:46 +00:00
for (size_t h = 0; h < images[index].height; ++h)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
if (convFlags & CONV_FLAGS_EXPAND)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
if (convFlags & (CONV_FLAGS_565 | CONV_FLAGS_5551 | CONV_FLAGS_4444))
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
if (!_ExpandScanline(pDest, dpitch, DXGI_FORMAT_R8G8B8A8_UNORM,
pSrc, spitch,
(convFlags & CONV_FLAGS_565) ? DXGI_FORMAT_B5G6R5_UNORM : DXGI_FORMAT_B5G5R5A1_UNORM,
tflags))
2016-08-22 18:26:36 +00:00
return E_FAIL;
}
else
{
2016-09-09 02:09:46 +00:00
TEXP_LEGACY_FORMAT lformat = _FindLegacyFormat(convFlags);
if (!LegacyExpandScanline(pDest, dpitch, metadata.format,
pSrc, spitch, lformat, pal8,
tflags))
2016-08-22 18:26:36 +00:00
return E_FAIL;
}
}
2016-09-09 02:09:46 +00:00
else if (convFlags & CONV_FLAGS_SWIZZLE)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
_SwizzleScanline(pDest, dpitch, pSrc, spitch,
metadata.format, tflags);
2016-08-22 18:26:36 +00:00
}
else
{
2016-09-09 02:09:46 +00:00
_CopyScanline(pDest, dpitch, pSrc, spitch,
metadata.format, tflags);
2016-08-22 18:26:36 +00:00
}
pSrc += spitch;
pDest += dpitch;
}
}
}
}
}
break;
2016-09-09 02:09:46 +00:00
case TEX_DIMENSION_TEXTURE3D:
2016-08-22 18:26:36 +00:00
{
size_t index = 0;
size_t d = metadata.depth;
size_t lastgood = 0;
2016-09-09 02:09:46 +00:00
for (size_t level = 0; level < metadata.mipLevels; ++level)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
for (size_t slice = 0; slice < d; ++slice, ++index)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
if (index >= nimages)
2016-08-22 18:26:36 +00:00
return E_FAIL;
2016-09-09 02:09:46 +00:00
if (images[index].height != timages[index].height)
2016-08-22 18:26:36 +00:00
return E_FAIL;
2016-09-09 02:09:46 +00:00
size_t dpitch = images[index].rowPitch;
size_t spitch = timages[index].rowPitch;
2016-08-22 18:26:36 +00:00
2018-03-29 19:00:16 +00:00
const uint8_t *pSrc = timages[index].pixels;
2016-09-09 02:09:46 +00:00
if (!pSrc)
2016-08-22 18:26:36 +00:00
return E_POINTER;
2016-09-09 02:09:46 +00:00
uint8_t *pDest = images[index].pixels;
if (!pDest)
2016-08-22 18:26:36 +00:00
return E_POINTER;
2016-09-09 02:09:46 +00:00
if (IsCompressed(metadata.format))
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
size_t csize = std::min<size_t>(images[index].slicePitch, timages[index].slicePitch);
memcpy_s(pDest, images[index].slicePitch, pSrc, csize);
if (cpFlags & CP_FLAGS_BAD_DXTN_TAILS)
{
if (images[index].width < 4 || images[index].height < 4)
{
csize = std::min<size_t>(images[index].slicePitch, timages[lastgood + slice].slicePitch);
memcpy_s(pDest, images[index].slicePitch, timages[lastgood + slice].pixels, csize);
}
else if (!slice)
{
lastgood = index;
}
}
2016-08-22 18:26:36 +00:00
}
2016-09-09 02:09:46 +00:00
else if (IsPlanar(metadata.format))
2016-08-22 18:26:36 +00:00
{
// Direct3D does not support any planar formats for Texture3D
2016-09-09 02:09:46 +00:00
return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED);
2016-08-22 18:26:36 +00:00
}
else
{
2016-09-09 02:09:46 +00:00
for (size_t h = 0; h < images[index].height; ++h)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
if (convFlags & CONV_FLAGS_EXPAND)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
if (convFlags & (CONV_FLAGS_565 | CONV_FLAGS_5551 | CONV_FLAGS_4444))
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
if (!_ExpandScanline(pDest, dpitch, DXGI_FORMAT_R8G8B8A8_UNORM,
pSrc, spitch,
(convFlags & CONV_FLAGS_565) ? DXGI_FORMAT_B5G6R5_UNORM : DXGI_FORMAT_B5G5R5A1_UNORM,
tflags))
2016-08-22 18:26:36 +00:00
return E_FAIL;
}
else
{
2016-09-09 02:09:46 +00:00
TEXP_LEGACY_FORMAT lformat = _FindLegacyFormat(convFlags);
if (!LegacyExpandScanline(pDest, dpitch, metadata.format,
pSrc, spitch, lformat, pal8,
tflags))
2016-08-22 18:26:36 +00:00
return E_FAIL;
}
}
2016-09-09 02:09:46 +00:00
else if (convFlags & CONV_FLAGS_SWIZZLE)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
_SwizzleScanline(pDest, dpitch, pSrc, spitch, metadata.format, tflags);
2016-08-22 18:26:36 +00:00
}
else
{
2016-09-09 02:09:46 +00:00
_CopyScanline(pDest, dpitch, pSrc, spitch, metadata.format, tflags);
2016-08-22 18:26:36 +00:00
}
pSrc += spitch;
pDest += dpitch;
}
}
}
2016-09-09 02:09:46 +00:00
if (d > 1)
2016-08-22 18:26:36 +00:00
d >>= 1;
}
}
break;
2016-09-09 02:09:46 +00:00
default:
return E_FAIL;
}
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
return S_OK;
}
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
HRESULT CopyImageInPlace(DWORD convFlags, _In_ const ScratchImage& image)
{
if (!image.GetPixels())
return E_FAIL;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
const Image* images = image.GetImages();
if (!images)
return E_FAIL;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
const TexMetadata& metadata = image.GetMetadata();
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
if (IsPlanar(metadata.format))
return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED);
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
DWORD tflags = (convFlags & CONV_FLAGS_NOALPHA) ? TEXP_SCANLINE_SETALPHA : 0;
if (convFlags & CONV_FLAGS_SWIZZLE)
tflags |= TEXP_SCANLINE_LEGACY;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
for (size_t i = 0; i < image.GetImageCount(); ++i)
{
const Image* img = &images[i];
uint8_t *pPixels = img->pixels;
if (!pPixels)
return E_POINTER;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
size_t rowPitch = img->rowPitch;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
for (size_t h = 0; h < img->height; ++h)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
if (convFlags & CONV_FLAGS_SWIZZLE)
{
_SwizzleScanline(pPixels, rowPitch, pPixels, rowPitch, metadata.format, tflags);
}
else
{
_CopyScanline(pPixels, rowPitch, pPixels, rowPitch, metadata.format, tflags);
}
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
pPixels += rowPitch;
}
2016-08-22 18:26:36 +00:00
}
2016-09-09 02:09:46 +00:00
return S_OK;
}
2016-08-22 18:26:36 +00:00
}
//=====================================================================================
// Entry-points
//=====================================================================================
//-------------------------------------------------------------------------------------
// Obtain metadata from DDS file in memory/on disk
//-------------------------------------------------------------------------------------
_Use_decl_annotations_
2016-09-09 02:09:46 +00:00
HRESULT DirectX::GetMetadataFromDDSMemory(
const void* pSource,
size_t size,
DWORD flags,
TexMetadata& metadata)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
if (!pSource || size == 0)
2016-08-22 18:26:36 +00:00
return E_INVALIDARG;
DWORD convFlags = 0;
2016-09-09 02:09:46 +00:00
return DecodeDDSHeader(pSource, size, flags, metadata, convFlags);
2016-08-22 18:26:36 +00:00
}
_Use_decl_annotations_
2016-09-09 02:09:46 +00:00
HRESULT DirectX::GetMetadataFromDDSFile(
const wchar_t* szFile,
DWORD flags,
TexMetadata& metadata)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
if (!szFile)
2016-08-22 18:26:36 +00:00
return E_INVALIDARG;
#if (_WIN32_WINNT >= _WIN32_WINNT_WIN8)
2016-09-09 02:09:46 +00:00
ScopedHandle hFile(safe_handle(CreateFile2(szFile, GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING, nullptr)));
2016-08-22 18:26:36 +00:00
#else
2016-09-09 02:09:46 +00:00
ScopedHandle hFile(safe_handle(CreateFileW(szFile, GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING,
FILE_FLAG_SEQUENTIAL_SCAN, nullptr)));
2016-08-22 18:26:36 +00:00
#endif
2016-09-09 02:09:46 +00:00
if (!hFile)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
return HRESULT_FROM_WIN32(GetLastError());
2016-08-22 18:26:36 +00:00
}
// Get the file size
FILE_STANDARD_INFO fileInfo;
2016-09-09 02:09:46 +00:00
if (!GetFileInformationByHandleEx(hFile.get(), FileStandardInfo, &fileInfo, sizeof(fileInfo)))
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
return HRESULT_FROM_WIN32(GetLastError());
2016-08-22 18:26:36 +00:00
}
// File is too big for 32-bit allocation, so reject read (4 GB should be plenty large enough for a valid DDS file)
2016-09-09 02:09:46 +00:00
if (fileInfo.EndOfFile.HighPart > 0)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
return HRESULT_FROM_WIN32(ERROR_FILE_TOO_LARGE);
2016-08-22 18:26:36 +00:00
}
// Need at least enough data to fill the standard header and magic number to be a valid DDS
2016-09-09 02:09:46 +00:00
if (fileInfo.EndOfFile.LowPart < (sizeof(DDS_HEADER) + sizeof(uint32_t)))
2016-08-22 18:26:36 +00:00
{
return E_FAIL;
}
// Read the header in (including extended header if present)
const size_t MAX_HEADER_SIZE = sizeof(uint32_t) + sizeof(DDS_HEADER) + sizeof(DDS_HEADER_DXT10);
2018-03-29 19:00:16 +00:00
uint8_t header[MAX_HEADER_SIZE] = {};
2016-08-22 18:26:36 +00:00
DWORD bytesRead = 0;
2016-09-09 02:09:46 +00:00
if (!ReadFile(hFile.get(), header, MAX_HEADER_SIZE, &bytesRead, nullptr))
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
return HRESULT_FROM_WIN32(GetLastError());
2016-08-22 18:26:36 +00:00
}
DWORD convFlags = 0;
2016-09-09 02:09:46 +00:00
return DecodeDDSHeader(header, bytesRead, flags, metadata, convFlags);
2016-08-22 18:26:36 +00:00
}
//-------------------------------------------------------------------------------------
// Load a DDS file in memory
//-------------------------------------------------------------------------------------
_Use_decl_annotations_
2016-09-09 02:09:46 +00:00
HRESULT DirectX::LoadFromDDSMemory(
const void* pSource,
size_t size,
DWORD flags,
TexMetadata* metadata,
ScratchImage& image)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
if (!pSource || size == 0)
2016-08-22 18:26:36 +00:00
return E_INVALIDARG;
image.Release();
DWORD convFlags = 0;
TexMetadata mdata;
2016-09-09 02:09:46 +00:00
HRESULT hr = DecodeDDSHeader(pSource, size, flags, mdata, convFlags);
if (FAILED(hr))
2016-08-22 18:26:36 +00:00
return hr;
size_t offset = sizeof(uint32_t) + sizeof(DDS_HEADER);
2016-09-09 02:09:46 +00:00
if (convFlags & CONV_FLAGS_DX10)
2016-08-22 18:26:36 +00:00
offset += sizeof(DDS_HEADER_DXT10);
2016-09-09 02:09:46 +00:00
assert(offset <= size);
2016-08-22 18:26:36 +00:00
const uint32_t *pal8 = nullptr;
2016-09-09 02:09:46 +00:00
if (convFlags & CONV_FLAGS_PAL8)
2016-08-22 18:26:36 +00:00
{
2018-03-29 19:00:16 +00:00
pal8 = reinterpret_cast<const uint32_t*>(static_cast<const uint8_t*>(pSource) + offset);
2016-09-09 02:09:46 +00:00
assert(pal8);
offset += (256 * sizeof(uint32_t));
if (size < offset)
2016-08-22 18:26:36 +00:00
return E_FAIL;
}
2016-09-09 02:09:46 +00:00
hr = image.Initialize(mdata);
if (FAILED(hr))
2016-08-22 18:26:36 +00:00
return hr;
DWORD cflags = CP_FLAGS_NONE;
if (flags & DDS_FLAGS_LEGACY_DWORD)
{
cflags |= CP_FLAGS_LEGACY_DWORD;
}
if (flags & DDS_FLAGS_BAD_DXTN_TAILS)
{
cflags |= CP_FLAGS_BAD_DXTN_TAILS;
}
2018-03-29 19:00:16 +00:00
const void* pPixels = static_cast<const uint8_t*>(pSource) + offset;
2016-09-09 02:09:46 +00:00
assert(pPixels);
hr = CopyImage(pPixels,
size - offset,
mdata,
cflags,
convFlags,
pal8,
image);
2016-09-09 02:09:46 +00:00
if (FAILED(hr))
2016-08-22 18:26:36 +00:00
{
image.Release();
return hr;
}
2016-09-09 02:09:46 +00:00
if (metadata)
memcpy(metadata, &mdata, sizeof(TexMetadata));
2016-08-22 18:26:36 +00:00
return S_OK;
}
//-------------------------------------------------------------------------------------
// Load a DDS file from disk
//-------------------------------------------------------------------------------------
_Use_decl_annotations_
2016-09-09 02:09:46 +00:00
HRESULT DirectX::LoadFromDDSFile(
const wchar_t* szFile,
DWORD flags,
TexMetadata* metadata,
ScratchImage& image)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
if (!szFile)
2016-08-22 18:26:36 +00:00
return E_INVALIDARG;
image.Release();
#if (_WIN32_WINNT >= _WIN32_WINNT_WIN8)
2016-09-09 02:09:46 +00:00
ScopedHandle hFile(safe_handle(CreateFile2(szFile, GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING, nullptr)));
2016-08-22 18:26:36 +00:00
#else
2016-09-09 02:09:46 +00:00
ScopedHandle hFile(safe_handle(CreateFileW(szFile, GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING,
FILE_FLAG_SEQUENTIAL_SCAN, nullptr)));
2016-08-22 18:26:36 +00:00
#endif
2016-09-09 02:09:46 +00:00
if (!hFile)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
return HRESULT_FROM_WIN32(GetLastError());
2016-08-22 18:26:36 +00:00
}
// Get the file size
FILE_STANDARD_INFO fileInfo;
2016-09-09 02:09:46 +00:00
if (!GetFileInformationByHandleEx(hFile.get(), FileStandardInfo, &fileInfo, sizeof(fileInfo)))
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
return HRESULT_FROM_WIN32(GetLastError());
2016-08-22 18:26:36 +00:00
}
// File is too big for 32-bit allocation, so reject read (4 GB should be plenty large enough for a valid DDS file)
2016-09-09 02:09:46 +00:00
if (fileInfo.EndOfFile.HighPart > 0)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
return HRESULT_FROM_WIN32(ERROR_FILE_TOO_LARGE);
2016-08-22 18:26:36 +00:00
}
// Need at least enough data to fill the standard header and magic number to be a valid DDS
2016-09-09 02:09:46 +00:00
if (fileInfo.EndOfFile.LowPart < (sizeof(DDS_HEADER) + sizeof(uint32_t)))
2016-08-22 18:26:36 +00:00
{
return E_FAIL;
}
// Read the header in (including extended header if present)
const size_t MAX_HEADER_SIZE = sizeof(uint32_t) + sizeof(DDS_HEADER) + sizeof(DDS_HEADER_DXT10);
2018-03-29 19:00:16 +00:00
uint8_t header[MAX_HEADER_SIZE] = {};
2016-08-22 18:26:36 +00:00
DWORD bytesRead = 0;
2016-09-09 02:09:46 +00:00
if (!ReadFile(hFile.get(), header, MAX_HEADER_SIZE, &bytesRead, nullptr))
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
return HRESULT_FROM_WIN32(GetLastError());
2016-08-22 18:26:36 +00:00
}
DWORD convFlags = 0;
TexMetadata mdata;
2016-09-09 02:09:46 +00:00
HRESULT hr = DecodeDDSHeader(header, bytesRead, flags, mdata, convFlags);
if (FAILED(hr))
2016-08-22 18:26:36 +00:00
return hr;
DWORD offset = MAX_HEADER_SIZE;
2016-09-09 02:09:46 +00:00
if (!(convFlags & CONV_FLAGS_DX10))
2016-08-22 18:26:36 +00:00
{
// Must reset file position since we read more than the standard header above
2017-07-12 07:56:51 +00:00
LARGE_INTEGER filePos = { { sizeof(uint32_t) + sizeof(DDS_HEADER), 0 } };
2018-06-13 01:07:34 +00:00
if (!SetFilePointerEx(hFile.get(), filePos, nullptr, FILE_BEGIN))
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
return HRESULT_FROM_WIN32(GetLastError());
2016-08-22 18:26:36 +00:00
}
offset = sizeof(uint32_t) + sizeof(DDS_HEADER);
}
std::unique_ptr<uint32_t[]> pal8;
2016-09-09 02:09:46 +00:00
if (convFlags & CONV_FLAGS_PAL8)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
pal8.reset(new (std::nothrow) uint32_t[256]);
if (!pal8)
2016-08-22 18:26:36 +00:00
{
return E_OUTOFMEMORY;
}
2016-09-09 02:09:46 +00:00
if (!ReadFile(hFile.get(), pal8.get(), 256 * sizeof(uint32_t), &bytesRead, nullptr))
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
return HRESULT_FROM_WIN32(GetLastError());
2016-08-22 18:26:36 +00:00
}
2016-09-09 02:09:46 +00:00
if (bytesRead != (256 * sizeof(uint32_t)))
2016-08-22 18:26:36 +00:00
{
return E_FAIL;
}
2016-09-09 02:09:46 +00:00
offset += (256 * sizeof(uint32_t));
2016-08-22 18:26:36 +00:00
}
2016-09-09 02:09:46 +00:00
DWORD remaining = fileInfo.EndOfFile.LowPart - offset;
if (remaining == 0)
2016-08-22 18:26:36 +00:00
return E_FAIL;
2016-09-09 02:09:46 +00:00
hr = image.Initialize(mdata);
if (FAILED(hr))
2016-08-22 18:26:36 +00:00
return hr;
if ((convFlags & CONV_FLAGS_EXPAND) || (flags & (DDS_FLAGS_LEGACY_DWORD | DDS_FLAGS_BAD_DXTN_TAILS)))
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
std::unique_ptr<uint8_t[]> temp(new (std::nothrow) uint8_t[remaining]);
if (!temp)
2016-08-22 18:26:36 +00:00
{
image.Release();
return E_OUTOFMEMORY;
}
2016-09-09 02:09:46 +00:00
if (!ReadFile(hFile.get(), temp.get(), remaining, &bytesRead, nullptr))
2016-08-22 18:26:36 +00:00
{
image.Release();
2016-09-09 02:09:46 +00:00
return HRESULT_FROM_WIN32(GetLastError());
2016-08-22 18:26:36 +00:00
}
2016-09-09 02:09:46 +00:00
if (bytesRead != remaining)
2016-08-22 18:26:36 +00:00
{
image.Release();
return E_FAIL;
}
DWORD cflags = CP_FLAGS_NONE;
if (flags & DDS_FLAGS_LEGACY_DWORD)
{
cflags |= CP_FLAGS_LEGACY_DWORD;
}
if (flags & DDS_FLAGS_BAD_DXTN_TAILS)
{
cflags |= CP_FLAGS_BAD_DXTN_TAILS;
}
hr = CopyImage(temp.get(),
remaining,
mdata,
cflags,
convFlags,
pal8.get(),
image);
2016-09-09 02:09:46 +00:00
if (FAILED(hr))
2016-08-22 18:26:36 +00:00
{
image.Release();
return hr;
}
}
else
{
2016-09-09 02:09:46 +00:00
if (remaining < image.GetPixelsSize())
2016-08-22 18:26:36 +00:00
{
image.Release();
2018-03-16 19:52:21 +00:00
return HRESULT_FROM_WIN32(ERROR_HANDLE_EOF);
2016-08-22 18:26:36 +00:00
}
if (image.GetPixelsSize() > UINT32_MAX)
{
image.Release();
return HRESULT_FROM_WIN32(ERROR_ARITHMETIC_OVERFLOW);
}
2016-09-09 02:09:46 +00:00
if (!ReadFile(hFile.get(), image.GetPixels(), static_cast<DWORD>(image.GetPixelsSize()), &bytesRead, nullptr))
2016-08-22 18:26:36 +00:00
{
image.Release();
2016-09-09 02:09:46 +00:00
return HRESULT_FROM_WIN32(GetLastError());
2016-08-22 18:26:36 +00:00
}
2016-09-09 02:09:46 +00:00
if (convFlags & (CONV_FLAGS_SWIZZLE | CONV_FLAGS_NOALPHA))
2016-08-22 18:26:36 +00:00
{
// Swizzle/copy image in place
2016-09-09 02:09:46 +00:00
hr = CopyImageInPlace(convFlags, image);
if (FAILED(hr))
2016-08-22 18:26:36 +00:00
{
image.Release();
return hr;
}
}
}
2016-09-09 02:09:46 +00:00
if (metadata)
memcpy(metadata, &mdata, sizeof(TexMetadata));
2016-08-22 18:26:36 +00:00
return S_OK;
}
//-------------------------------------------------------------------------------------
// Save a DDS file to memory
//-------------------------------------------------------------------------------------
_Use_decl_annotations_
2016-09-09 02:09:46 +00:00
HRESULT DirectX::SaveToDDSMemory(
const Image* images,
size_t nimages,
const TexMetadata& metadata,
DWORD flags,
Blob& blob)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
if (!images || (nimages == 0))
2016-08-22 18:26:36 +00:00
return E_INVALIDARG;
// Determine memory required
size_t required = 0;
2018-06-13 01:07:34 +00:00
HRESULT hr = _EncodeDDSHeader(metadata, flags, nullptr, 0, required);
2016-09-09 02:09:46 +00:00
if (FAILED(hr))
2016-08-22 18:26:36 +00:00
return hr;
bool fastpath = true;
2016-09-09 02:09:46 +00:00
for (size_t i = 0; i < nimages; ++i)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
if (!images[i].pixels)
2016-08-22 18:26:36 +00:00
return E_POINTER;
2016-09-09 02:09:46 +00:00
if (images[i].format != metadata.format)
2016-08-22 18:26:36 +00:00
return E_FAIL;
size_t ddsRowPitch, ddsSlicePitch;
hr = ComputePitch(metadata.format, images[i].width, images[i].height, ddsRowPitch, ddsSlicePitch, CP_FLAGS_NONE);
if (FAILED(hr))
return hr;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
assert(images[i].rowPitch > 0);
assert(images[i].slicePitch > 0);
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
if ((images[i].rowPitch != ddsRowPitch) || (images[i].slicePitch != ddsSlicePitch))
2016-08-22 18:26:36 +00:00
{
fastpath = false;
}
required += ddsSlicePitch;
}
2016-09-09 02:09:46 +00:00
assert(required > 0);
2016-08-22 18:26:36 +00:00
blob.Release();
2016-09-09 02:09:46 +00:00
hr = blob.Initialize(required);
if (FAILED(hr))
2016-08-22 18:26:36 +00:00
return hr;
2018-03-29 19:00:16 +00:00
auto pDestination = static_cast<uint8_t*>(blob.GetBufferPointer());
2016-09-09 02:09:46 +00:00
assert(pDestination);
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
hr = _EncodeDDSHeader(metadata, flags, pDestination, blob.GetBufferSize(), required);
if (FAILED(hr))
2016-08-22 18:26:36 +00:00
{
blob.Release();
return hr;
}
size_t remaining = blob.GetBufferSize() - required;
pDestination += required;
2016-09-09 02:09:46 +00:00
if (!remaining)
2016-08-22 18:26:36 +00:00
{
blob.Release();
return E_FAIL;
}
2018-06-13 01:07:34 +00:00
switch (static_cast<DDS_RESOURCE_DIMENSION>(metadata.dimension))
2016-08-22 18:26:36 +00:00
{
case DDS_DIMENSION_TEXTURE1D:
case DDS_DIMENSION_TEXTURE2D:
2016-09-09 02:09:46 +00:00
{
size_t index = 0;
for (size_t item = 0; item < metadata.arraySize; ++item)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
for (size_t level = 0; level < metadata.mipLevels; ++level)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
if (index >= nimages)
{
blob.Release();
return E_FAIL;
}
if (fastpath)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
size_t pixsize = images[index].slicePitch;
if (memcpy_s(pDestination, remaining, images[index].pixels, pixsize))
2016-08-22 18:26:36 +00:00
{
blob.Release();
return E_FAIL;
}
2016-09-09 02:09:46 +00:00
pDestination += pixsize;
remaining -= pixsize;
}
else
{
size_t ddsRowPitch, ddsSlicePitch;
hr = ComputePitch(metadata.format, images[index].width, images[index].height, ddsRowPitch, ddsSlicePitch, CP_FLAGS_NONE);
if (FAILED(hr))
{
blob.Release();
return hr;
}
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
size_t rowPitch = images[index].rowPitch;
2016-08-22 18:26:36 +00:00
2016-10-28 00:45:47 +00:00
const uint8_t * __restrict sPtr = images[index].pixels;
2018-03-29 19:00:16 +00:00
uint8_t * __restrict dPtr = pDestination;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
size_t lines = ComputeScanlines(metadata.format, images[index].height);
size_t csize = std::min<size_t>(rowPitch, ddsRowPitch);
size_t tremaining = remaining;
for (size_t j = 0; j < lines; ++j)
{
if (memcpy_s(dPtr, tremaining, sPtr, csize))
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
blob.Release();
return E_FAIL;
2016-08-22 18:26:36 +00:00
}
2016-09-09 02:09:46 +00:00
sPtr += rowPitch;
dPtr += ddsRowPitch;
tremaining -= ddsRowPitch;
2016-08-22 18:26:36 +00:00
}
2016-09-09 02:09:46 +00:00
pDestination += ddsSlicePitch;
remaining -= ddsSlicePitch;
2016-08-22 18:26:36 +00:00
}
2016-09-09 02:09:46 +00:00
++index;
2016-08-22 18:26:36 +00:00
}
}
2016-09-09 02:09:46 +00:00
}
break;
2016-08-22 18:26:36 +00:00
case DDS_DIMENSION_TEXTURE3D:
2016-09-09 02:09:46 +00:00
{
if (metadata.arraySize != 1)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
blob.Release();
return E_FAIL;
}
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
size_t d = metadata.depth;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
size_t index = 0;
for (size_t level = 0; level < metadata.mipLevels; ++level)
{
for (size_t slice = 0; slice < d; ++slice)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
if (index >= nimages)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
blob.Release();
return E_FAIL;
}
if (fastpath)
{
size_t pixsize = images[index].slicePitch;
if (memcpy_s(pDestination, remaining, images[index].pixels, pixsize))
2016-08-22 18:26:36 +00:00
{
blob.Release();
return E_FAIL;
}
2016-09-09 02:09:46 +00:00
pDestination += pixsize;
remaining -= pixsize;
}
else
{
size_t ddsRowPitch, ddsSlicePitch;
hr = ComputePitch(metadata.format, images[index].width, images[index].height, ddsRowPitch, ddsSlicePitch, CP_FLAGS_NONE);
if (FAILED(hr))
{
blob.Release();
return hr;
}
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
size_t rowPitch = images[index].rowPitch;
2016-08-22 18:26:36 +00:00
2016-10-28 00:45:47 +00:00
const uint8_t * __restrict sPtr = images[index].pixels;
2018-03-29 19:00:16 +00:00
uint8_t * __restrict dPtr = pDestination;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
size_t lines = ComputeScanlines(metadata.format, images[index].height);
size_t csize = std::min<size_t>(rowPitch, ddsRowPitch);
size_t tremaining = remaining;
for (size_t j = 0; j < lines; ++j)
{
if (memcpy_s(dPtr, tremaining, sPtr, csize))
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
blob.Release();
return E_FAIL;
2016-08-22 18:26:36 +00:00
}
2016-09-09 02:09:46 +00:00
sPtr += rowPitch;
dPtr += ddsRowPitch;
tremaining -= ddsRowPitch;
2016-08-22 18:26:36 +00:00
}
2016-09-09 02:09:46 +00:00
pDestination += ddsSlicePitch;
remaining -= ddsSlicePitch;
2016-08-22 18:26:36 +00:00
}
2016-09-09 02:09:46 +00:00
++index;
2016-08-22 18:26:36 +00:00
}
2016-09-09 02:09:46 +00:00
if (d > 1)
d >>= 1;
2016-08-22 18:26:36 +00:00
}
2016-09-09 02:09:46 +00:00
}
break;
2016-08-22 18:26:36 +00:00
default:
blob.Release();
return E_FAIL;
}
return S_OK;
}
//-------------------------------------------------------------------------------------
// Save a DDS file to disk
//-------------------------------------------------------------------------------------
_Use_decl_annotations_
2016-09-09 02:09:46 +00:00
HRESULT DirectX::SaveToDDSFile(
const Image* images,
size_t nimages,
const TexMetadata& metadata,
DWORD flags,
const wchar_t* szFile)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
if (!szFile)
2016-08-22 18:26:36 +00:00
return E_INVALIDARG;
// Create DDS Header
const size_t MAX_HEADER_SIZE = sizeof(uint32_t) + sizeof(DDS_HEADER) + sizeof(DDS_HEADER_DXT10);
uint8_t header[MAX_HEADER_SIZE];
size_t required;
2016-09-09 02:09:46 +00:00
HRESULT hr = _EncodeDDSHeader(metadata, flags, header, MAX_HEADER_SIZE, required);
if (FAILED(hr))
2016-08-22 18:26:36 +00:00
return hr;
// Create file and write header
#if (_WIN32_WINNT >= _WIN32_WINNT_WIN8)
2016-09-09 02:09:46 +00:00
ScopedHandle hFile(safe_handle(CreateFile2(szFile, GENERIC_WRITE | DELETE, 0, CREATE_ALWAYS, nullptr)));
2016-08-22 18:26:36 +00:00
#else
2016-09-09 02:09:46 +00:00
ScopedHandle hFile(safe_handle(CreateFileW(szFile, GENERIC_WRITE | DELETE, 0, nullptr, CREATE_ALWAYS, 0, nullptr)));
2016-08-22 18:26:36 +00:00
#endif
2016-09-09 02:09:46 +00:00
if (!hFile)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
return HRESULT_FROM_WIN32(GetLastError());
2016-08-22 18:26:36 +00:00
}
auto_delete_file delonfail(hFile.get());
DWORD bytesWritten;
2016-09-09 02:09:46 +00:00
if (!WriteFile(hFile.get(), header, static_cast<DWORD>(required), &bytesWritten, nullptr))
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
return HRESULT_FROM_WIN32(GetLastError());
2016-08-22 18:26:36 +00:00
}
2016-09-09 02:09:46 +00:00
if (bytesWritten != required)
2016-08-22 18:26:36 +00:00
{
return E_FAIL;
}
// Write images
2018-06-13 01:07:34 +00:00
switch (static_cast<DDS_RESOURCE_DIMENSION>(metadata.dimension))
2016-08-22 18:26:36 +00:00
{
case DDS_DIMENSION_TEXTURE1D:
case DDS_DIMENSION_TEXTURE2D:
2016-09-09 02:09:46 +00:00
{
size_t index = 0;
for (size_t item = 0; item < metadata.arraySize; ++item)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
for (size_t level = 0; level < metadata.mipLevels; ++level, ++index)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
if (index >= nimages)
return E_FAIL;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
if (!images[index].pixels)
return E_POINTER;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
assert(images[index].rowPitch > 0);
assert(images[index].slicePitch > 0);
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
size_t ddsRowPitch, ddsSlicePitch;
hr = ComputePitch(metadata.format, images[index].width, images[index].height, ddsRowPitch, ddsSlicePitch, CP_FLAGS_NONE);
if (FAILED(hr))
return hr;
2016-08-22 18:26:36 +00:00
if ((images[index].slicePitch == ddsSlicePitch) && (ddsSlicePitch <= UINT32_MAX))
2016-09-09 02:09:46 +00:00
{
if (!WriteFile(hFile.get(), images[index].pixels, static_cast<DWORD>(ddsSlicePitch), &bytesWritten, nullptr))
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
return HRESULT_FROM_WIN32(GetLastError());
}
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
if (bytesWritten != ddsSlicePitch)
{
return E_FAIL;
2016-08-22 18:26:36 +00:00
}
2016-09-09 02:09:46 +00:00
}
else
{
size_t rowPitch = images[index].rowPitch;
if (rowPitch < ddsRowPitch)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
// DDS uses 1-byte alignment, so if this is happening then the input pitch isn't actually a full line of data
return E_FAIL;
}
2016-08-22 18:26:36 +00:00
if (ddsRowPitch > UINT32_MAX)
return HRESULT_FROM_WIN32(ERROR_ARITHMETIC_OVERFLOW);
2016-10-28 00:45:47 +00:00
const uint8_t * __restrict sPtr = images[index].pixels;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
size_t lines = ComputeScanlines(metadata.format, images[index].height);
for (size_t j = 0; j < lines; ++j)
{
if (!WriteFile(hFile.get(), sPtr, static_cast<DWORD>(ddsRowPitch), &bytesWritten, nullptr))
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
return HRESULT_FROM_WIN32(GetLastError());
}
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
if (bytesWritten != ddsRowPitch)
{
return E_FAIL;
2016-08-22 18:26:36 +00:00
}
2016-09-09 02:09:46 +00:00
sPtr += rowPitch;
2016-08-22 18:26:36 +00:00
}
}
}
}
2016-09-09 02:09:46 +00:00
}
break;
2016-08-22 18:26:36 +00:00
case DDS_DIMENSION_TEXTURE3D:
2016-09-09 02:09:46 +00:00
{
if (metadata.arraySize != 1)
return E_FAIL;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
size_t d = metadata.depth;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
size_t index = 0;
for (size_t level = 0; level < metadata.mipLevels; ++level)
{
for (size_t slice = 0; slice < d; ++slice, ++index)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
if (index >= nimages)
return E_FAIL;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
if (!images[index].pixels)
return E_POINTER;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
assert(images[index].rowPitch > 0);
assert(images[index].slicePitch > 0);
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
size_t ddsRowPitch, ddsSlicePitch;
hr = ComputePitch(metadata.format, images[index].width, images[index].height, ddsRowPitch, ddsSlicePitch, CP_FLAGS_NONE);
if (FAILED(hr))
return hr;
2016-08-22 18:26:36 +00:00
if ((images[index].slicePitch == ddsSlicePitch) && (ddsSlicePitch <= UINT32_MAX))
2016-09-09 02:09:46 +00:00
{
if (!WriteFile(hFile.get(), images[index].pixels, static_cast<DWORD>(ddsSlicePitch), &bytesWritten, nullptr))
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
return HRESULT_FROM_WIN32(GetLastError());
}
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
if (bytesWritten != ddsSlicePitch)
{
return E_FAIL;
2016-08-22 18:26:36 +00:00
}
2016-09-09 02:09:46 +00:00
}
else
{
size_t rowPitch = images[index].rowPitch;
if (rowPitch < ddsRowPitch)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
// DDS uses 1-byte alignment, so if this is happening then the input pitch isn't actually a full line of data
return E_FAIL;
}
2016-08-22 18:26:36 +00:00
if (ddsRowPitch > UINT32_MAX)
return HRESULT_FROM_WIN32(ERROR_ARITHMETIC_OVERFLOW);
2016-10-28 00:45:47 +00:00
const uint8_t * __restrict sPtr = images[index].pixels;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
size_t lines = ComputeScanlines(metadata.format, images[index].height);
for (size_t j = 0; j < lines; ++j)
{
if (!WriteFile(hFile.get(), sPtr, static_cast<DWORD>(ddsRowPitch), &bytesWritten, nullptr))
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
return HRESULT_FROM_WIN32(GetLastError());
}
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
if (bytesWritten != ddsRowPitch)
{
return E_FAIL;
2016-08-22 18:26:36 +00:00
}
2016-09-09 02:09:46 +00:00
sPtr += rowPitch;
2016-08-22 18:26:36 +00:00
}
}
}
2016-09-09 02:09:46 +00:00
if (d > 1)
d >>= 1;
2016-08-22 18:26:36 +00:00
}
2016-09-09 02:09:46 +00:00
}
break;
2016-08-22 18:26:36 +00:00
default:
return E_FAIL;
}
delonfail.clear();
return S_OK;
}