2016-08-22 18:26:36 +00:00
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
// File: WICTextureLoader.cpp
|
|
|
|
//
|
2016-09-23 05:34:50 +00:00
|
|
|
// Function for loading a WIC image and creating a Direct3D runtime texture for it
|
2016-08-22 18:26:36 +00:00
|
|
|
// (auto-generating mipmaps if possible)
|
|
|
|
//
|
|
|
|
// Note: Assumes application has already called CoInitializeEx
|
|
|
|
//
|
|
|
|
// Warning: CreateWICTexture* functions are not thread-safe if given a d3dContext instance for
|
|
|
|
// auto-gen mipmap support.
|
|
|
|
//
|
|
|
|
// Note these functions are useful for images created as simple 2D textures. For
|
|
|
|
// more complex resources, DDSTextureLoader is an excellent light-weight runtime loader.
|
|
|
|
// For a full-featured DDS file reader, writer, and texture processing pipeline see
|
|
|
|
// the 'Texconv' sample and the 'DirectXTex' library.
|
|
|
|
//
|
|
|
|
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
|
|
|
|
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
|
|
|
|
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
|
|
|
|
// PARTICULAR PURPOSE.
|
|
|
|
//
|
|
|
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
//
|
|
|
|
// http://go.microsoft.com/fwlink/?LinkId=248926
|
|
|
|
// http://go.microsoft.com/fwlink/?LinkId=248929
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// We could load multi-frame images (TIFF/GIF) into a texture array.
|
|
|
|
// For now, we just load the first frame (note: DirectXTex supports multi-frame images)
|
|
|
|
|
|
|
|
#include "WICTextureLoader.h"
|
|
|
|
|
|
|
|
#include <dxgiformat.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include <wincodec.h>
|
|
|
|
|
|
|
|
#include <wrl\client.h>
|
|
|
|
|
2017-01-07 01:16:43 +00:00
|
|
|
#include <algorithm>
|
2016-08-22 18:26:36 +00:00
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
#if !defined(NO_D3D11_DEBUG_NAME) && ( defined(_DEBUG) || defined(PROFILE) )
|
|
|
|
#pragma comment(lib,"dxguid.lib")
|
|
|
|
#endif
|
|
|
|
|
2016-09-23 05:34:50 +00:00
|
|
|
using namespace DirectX;
|
2016-08-22 18:26:36 +00:00
|
|
|
using Microsoft::WRL::ComPtr;
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
template<UINT TNameLength>
|
2016-09-23 05:34:50 +00:00
|
|
|
inline void SetDebugObjectName(_In_ ID3D11DeviceChild* resource, _In_ const char(&name)[TNameLength])
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
2016-09-23 05:34:50 +00:00
|
|
|
#if !defined(NO_D3D11_DEBUG_NAME) && ( defined(_DEBUG) || defined(PROFILE) )
|
2016-08-22 18:26:36 +00:00
|
|
|
resource->SetPrivateData(WKPDID_D3DDebugObjectName, TNameLength - 1, name);
|
2016-09-23 05:34:50 +00:00
|
|
|
#else
|
2016-08-22 18:26:36 +00:00
|
|
|
UNREFERENCED_PARAMETER(resource);
|
|
|
|
UNREFERENCED_PARAMETER(name);
|
2016-09-23 05:34:50 +00:00
|
|
|
#endif
|
2016-08-22 18:26:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------------------
|
|
|
|
// WIC Pixel Format Translation Data
|
|
|
|
//-------------------------------------------------------------------------------------
|
|
|
|
struct WICTranslate
|
|
|
|
{
|
|
|
|
GUID wic;
|
|
|
|
DXGI_FORMAT format;
|
|
|
|
};
|
|
|
|
|
2016-09-01 08:33:23 +00:00
|
|
|
const WICTranslate g_WICFormats[] =
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
|
|
|
{ GUID_WICPixelFormat128bppRGBAFloat, DXGI_FORMAT_R32G32B32A32_FLOAT },
|
|
|
|
|
|
|
|
{ GUID_WICPixelFormat64bppRGBAHalf, DXGI_FORMAT_R16G16B16A16_FLOAT },
|
|
|
|
{ GUID_WICPixelFormat64bppRGBA, DXGI_FORMAT_R16G16B16A16_UNORM },
|
|
|
|
|
|
|
|
{ GUID_WICPixelFormat32bppRGBA, DXGI_FORMAT_R8G8B8A8_UNORM },
|
|
|
|
{ GUID_WICPixelFormat32bppBGRA, DXGI_FORMAT_B8G8R8A8_UNORM }, // DXGI 1.1
|
|
|
|
{ GUID_WICPixelFormat32bppBGR, DXGI_FORMAT_B8G8R8X8_UNORM }, // DXGI 1.1
|
|
|
|
|
|
|
|
{ GUID_WICPixelFormat32bppRGBA1010102XR, DXGI_FORMAT_R10G10B10_XR_BIAS_A2_UNORM }, // DXGI 1.1
|
|
|
|
{ GUID_WICPixelFormat32bppRGBA1010102, DXGI_FORMAT_R10G10B10A2_UNORM },
|
|
|
|
|
|
|
|
{ GUID_WICPixelFormat16bppBGRA5551, DXGI_FORMAT_B5G5R5A1_UNORM },
|
|
|
|
{ GUID_WICPixelFormat16bppBGR565, DXGI_FORMAT_B5G6R5_UNORM },
|
|
|
|
|
|
|
|
{ GUID_WICPixelFormat32bppGrayFloat, DXGI_FORMAT_R32_FLOAT },
|
|
|
|
{ GUID_WICPixelFormat16bppGrayHalf, DXGI_FORMAT_R16_FLOAT },
|
|
|
|
{ GUID_WICPixelFormat16bppGray, DXGI_FORMAT_R16_UNORM },
|
|
|
|
{ GUID_WICPixelFormat8bppGray, DXGI_FORMAT_R8_UNORM },
|
|
|
|
|
|
|
|
{ GUID_WICPixelFormat8bppAlpha, DXGI_FORMAT_A8_UNORM },
|
|
|
|
};
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------------------
|
|
|
|
// WIC Pixel Format nearest conversion table
|
|
|
|
//-------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
struct WICConvert
|
|
|
|
{
|
|
|
|
GUID source;
|
|
|
|
GUID target;
|
|
|
|
};
|
|
|
|
|
2016-09-01 08:33:23 +00:00
|
|
|
const WICConvert g_WICConvert[] =
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
|
|
|
// Note target GUID in this conversion table must be one of those directly supported formats (above).
|
|
|
|
|
|
|
|
{ GUID_WICPixelFormatBlackWhite, GUID_WICPixelFormat8bppGray }, // DXGI_FORMAT_R8_UNORM
|
|
|
|
|
|
|
|
{ GUID_WICPixelFormat1bppIndexed, GUID_WICPixelFormat32bppRGBA }, // DXGI_FORMAT_R8G8B8A8_UNORM
|
|
|
|
{ GUID_WICPixelFormat2bppIndexed, GUID_WICPixelFormat32bppRGBA }, // DXGI_FORMAT_R8G8B8A8_UNORM
|
|
|
|
{ GUID_WICPixelFormat4bppIndexed, GUID_WICPixelFormat32bppRGBA }, // DXGI_FORMAT_R8G8B8A8_UNORM
|
|
|
|
{ GUID_WICPixelFormat8bppIndexed, GUID_WICPixelFormat32bppRGBA }, // DXGI_FORMAT_R8G8B8A8_UNORM
|
|
|
|
|
|
|
|
{ GUID_WICPixelFormat2bppGray, GUID_WICPixelFormat8bppGray }, // DXGI_FORMAT_R8_UNORM
|
|
|
|
{ GUID_WICPixelFormat4bppGray, GUID_WICPixelFormat8bppGray }, // DXGI_FORMAT_R8_UNORM
|
|
|
|
|
|
|
|
{ GUID_WICPixelFormat16bppGrayFixedPoint, GUID_WICPixelFormat16bppGrayHalf }, // DXGI_FORMAT_R16_FLOAT
|
|
|
|
{ GUID_WICPixelFormat32bppGrayFixedPoint, GUID_WICPixelFormat32bppGrayFloat }, // DXGI_FORMAT_R32_FLOAT
|
|
|
|
|
|
|
|
{ GUID_WICPixelFormat16bppBGR555, GUID_WICPixelFormat16bppBGRA5551 }, // DXGI_FORMAT_B5G5R5A1_UNORM
|
|
|
|
|
|
|
|
{ GUID_WICPixelFormat32bppBGR101010, GUID_WICPixelFormat32bppRGBA1010102 }, // DXGI_FORMAT_R10G10B10A2_UNORM
|
|
|
|
|
|
|
|
{ GUID_WICPixelFormat24bppBGR, GUID_WICPixelFormat32bppRGBA }, // DXGI_FORMAT_R8G8B8A8_UNORM
|
|
|
|
{ GUID_WICPixelFormat24bppRGB, GUID_WICPixelFormat32bppRGBA }, // DXGI_FORMAT_R8G8B8A8_UNORM
|
|
|
|
{ GUID_WICPixelFormat32bppPBGRA, GUID_WICPixelFormat32bppRGBA }, // DXGI_FORMAT_R8G8B8A8_UNORM
|
|
|
|
{ GUID_WICPixelFormat32bppPRGBA, GUID_WICPixelFormat32bppRGBA }, // DXGI_FORMAT_R8G8B8A8_UNORM
|
|
|
|
|
|
|
|
{ GUID_WICPixelFormat48bppRGB, GUID_WICPixelFormat64bppRGBA }, // DXGI_FORMAT_R16G16B16A16_UNORM
|
|
|
|
{ GUID_WICPixelFormat48bppBGR, GUID_WICPixelFormat64bppRGBA }, // DXGI_FORMAT_R16G16B16A16_UNORM
|
|
|
|
{ GUID_WICPixelFormat64bppBGRA, GUID_WICPixelFormat64bppRGBA }, // DXGI_FORMAT_R16G16B16A16_UNORM
|
|
|
|
{ GUID_WICPixelFormat64bppPRGBA, GUID_WICPixelFormat64bppRGBA }, // DXGI_FORMAT_R16G16B16A16_UNORM
|
|
|
|
{ GUID_WICPixelFormat64bppPBGRA, GUID_WICPixelFormat64bppRGBA }, // DXGI_FORMAT_R16G16B16A16_UNORM
|
|
|
|
|
|
|
|
{ GUID_WICPixelFormat48bppRGBFixedPoint, GUID_WICPixelFormat64bppRGBAHalf }, // DXGI_FORMAT_R16G16B16A16_FLOAT
|
|
|
|
{ GUID_WICPixelFormat48bppBGRFixedPoint, GUID_WICPixelFormat64bppRGBAHalf }, // DXGI_FORMAT_R16G16B16A16_FLOAT
|
|
|
|
{ GUID_WICPixelFormat64bppRGBAFixedPoint, GUID_WICPixelFormat64bppRGBAHalf }, // DXGI_FORMAT_R16G16B16A16_FLOAT
|
|
|
|
{ GUID_WICPixelFormat64bppBGRAFixedPoint, GUID_WICPixelFormat64bppRGBAHalf }, // DXGI_FORMAT_R16G16B16A16_FLOAT
|
|
|
|
{ GUID_WICPixelFormat64bppRGBFixedPoint, GUID_WICPixelFormat64bppRGBAHalf }, // DXGI_FORMAT_R16G16B16A16_FLOAT
|
|
|
|
{ GUID_WICPixelFormat64bppRGBHalf, GUID_WICPixelFormat64bppRGBAHalf }, // DXGI_FORMAT_R16G16B16A16_FLOAT
|
|
|
|
{ GUID_WICPixelFormat48bppRGBHalf, GUID_WICPixelFormat64bppRGBAHalf }, // DXGI_FORMAT_R16G16B16A16_FLOAT
|
|
|
|
|
|
|
|
{ GUID_WICPixelFormat128bppPRGBAFloat, GUID_WICPixelFormat128bppRGBAFloat }, // DXGI_FORMAT_R32G32B32A32_FLOAT
|
|
|
|
{ GUID_WICPixelFormat128bppRGBFloat, GUID_WICPixelFormat128bppRGBAFloat }, // DXGI_FORMAT_R32G32B32A32_FLOAT
|
|
|
|
{ GUID_WICPixelFormat128bppRGBAFixedPoint, GUID_WICPixelFormat128bppRGBAFloat }, // DXGI_FORMAT_R32G32B32A32_FLOAT
|
|
|
|
{ GUID_WICPixelFormat128bppRGBFixedPoint, GUID_WICPixelFormat128bppRGBAFloat }, // DXGI_FORMAT_R32G32B32A32_FLOAT
|
|
|
|
{ GUID_WICPixelFormat32bppRGBE, GUID_WICPixelFormat128bppRGBAFloat }, // DXGI_FORMAT_R32G32B32A32_FLOAT
|
|
|
|
|
2017-01-12 19:11:34 +00:00
|
|
|
{ GUID_WICPixelFormat32bppCMYK, GUID_WICPixelFormat32bppRGBA }, // DXGI_FORMAT_R8G8B8A8_UNORM
|
2016-08-22 18:26:36 +00:00
|
|
|
{ GUID_WICPixelFormat64bppCMYK, GUID_WICPixelFormat64bppRGBA }, // DXGI_FORMAT_R16G16B16A16_UNORM
|
2017-01-12 19:11:34 +00:00
|
|
|
{ GUID_WICPixelFormat40bppCMYKAlpha, GUID_WICPixelFormat32bppRGBA }, // DXGI_FORMAT_R8G8B8A8_UNORM
|
2016-08-22 18:26:36 +00:00
|
|
|
{ GUID_WICPixelFormat80bppCMYKAlpha, GUID_WICPixelFormat64bppRGBA }, // DXGI_FORMAT_R16G16B16A16_UNORM
|
|
|
|
|
2016-09-01 08:33:23 +00:00
|
|
|
#if (_WIN32_WINNT >= _WIN32_WINNT_WIN8) || defined(_WIN7_PLATFORM_UPDATE)
|
2016-08-22 18:26:36 +00:00
|
|
|
{ GUID_WICPixelFormat32bppRGB, GUID_WICPixelFormat32bppRGBA }, // DXGI_FORMAT_R8G8B8A8_UNORM
|
|
|
|
{ GUID_WICPixelFormat64bppRGB, GUID_WICPixelFormat64bppRGBA }, // DXGI_FORMAT_R16G16B16A16_UNORM
|
|
|
|
{ GUID_WICPixelFormat64bppPRGBAHalf, GUID_WICPixelFormat64bppRGBAHalf }, // DXGI_FORMAT_R16G16B16A16_FLOAT
|
2016-09-01 08:33:23 +00:00
|
|
|
#endif
|
2016-08-22 18:26:36 +00:00
|
|
|
|
|
|
|
// We don't support n-channel formats
|
|
|
|
};
|
|
|
|
|
|
|
|
bool g_WIC2 = false;
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
IWICImagingFactory* _GetWIC()
|
|
|
|
{
|
2016-09-01 08:33:23 +00:00
|
|
|
static INIT_ONCE s_initOnce = INIT_ONCE_STATIC_INIT;
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-02 08:29:53 +00:00
|
|
|
IWICImagingFactory* factory = nullptr;
|
2016-09-01 08:33:23 +00:00
|
|
|
InitOnceExecuteOnce(&s_initOnce,
|
2016-09-01 23:54:42 +00:00
|
|
|
[](PINIT_ONCE, PVOID, PVOID *factory) -> BOOL
|
2016-09-23 05:34:50 +00:00
|
|
|
{
|
|
|
|
#if (_WIN32_WINNT >= _WIN32_WINNT_WIN8) || defined(_WIN7_PLATFORM_UPDATE)
|
|
|
|
HRESULT hr = CoCreateInstance(
|
|
|
|
CLSID_WICImagingFactory2,
|
|
|
|
nullptr,
|
|
|
|
CLSCTX_INPROC_SERVER,
|
|
|
|
__uuidof(IWICImagingFactory2),
|
|
|
|
factory
|
|
|
|
);
|
|
|
|
|
|
|
|
if (SUCCEEDED(hr))
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
2016-09-23 05:34:50 +00:00
|
|
|
// WIC2 is available on Windows 10, Windows 8.x, and Windows 7 SP1 with KB 2670838 installed
|
|
|
|
g_WIC2 = true;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
hr = CoCreateInstance(
|
|
|
|
CLSID_WICImagingFactory1,
|
2016-09-01 08:33:23 +00:00
|
|
|
nullptr,
|
|
|
|
CLSCTX_INPROC_SERVER,
|
2016-09-01 23:54:42 +00:00
|
|
|
__uuidof(IWICImagingFactory),
|
2016-09-23 05:34:50 +00:00
|
|
|
factory
|
|
|
|
);
|
|
|
|
return SUCCEEDED(hr) ? TRUE : FALSE;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
return SUCCEEDED(CoCreateInstance(
|
|
|
|
CLSID_WICImagingFactory,
|
|
|
|
nullptr,
|
|
|
|
CLSCTX_INPROC_SERVER,
|
|
|
|
__uuidof(IWICImagingFactory),
|
|
|
|
factory)) ? TRUE : FALSE;
|
|
|
|
#endif
|
|
|
|
}, nullptr, reinterpret_cast<LPVOID*>(&factory));
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-02 08:29:53 +00:00
|
|
|
return factory;
|
2016-08-22 18:26:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------
|
2016-09-23 05:34:50 +00:00
|
|
|
DXGI_FORMAT _WICToDXGI(const GUID& guid)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
2016-09-23 05:34:50 +00:00
|
|
|
for (size_t i = 0; i < _countof(g_WICFormats); ++i)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
2016-09-23 05:34:50 +00:00
|
|
|
if (memcmp(&g_WICFormats[i].wic, &guid, sizeof(GUID)) == 0)
|
2016-08-22 18:26:36 +00:00
|
|
|
return g_WICFormats[i].format;
|
|
|
|
}
|
|
|
|
|
2016-09-23 05:34:50 +00:00
|
|
|
#if (_WIN32_WINNT >= _WIN32_WINNT_WIN8) || defined(_WIN7_PLATFORM_UPDATE)
|
|
|
|
if (g_WIC2)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
2016-09-23 05:34:50 +00:00
|
|
|
if (memcmp(&GUID_WICPixelFormat96bppRGBFloat, &guid, sizeof(GUID)) == 0)
|
2016-08-22 18:26:36 +00:00
|
|
|
return DXGI_FORMAT_R32G32B32_FLOAT;
|
|
|
|
}
|
2016-09-23 05:34:50 +00:00
|
|
|
#endif
|
2016-08-22 18:26:36 +00:00
|
|
|
|
|
|
|
return DXGI_FORMAT_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------
|
2016-09-23 05:34:50 +00:00
|
|
|
size_t _WICBitsPerPixel(REFGUID targetGuid)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
2016-09-23 05:34:50 +00:00
|
|
|
auto pWIC = _GetWIC();
|
|
|
|
if (!pWIC)
|
2016-08-22 18:26:36 +00:00
|
|
|
return 0;
|
2016-09-23 05:34:50 +00:00
|
|
|
|
2016-08-22 18:26:36 +00:00
|
|
|
ComPtr<IWICComponentInfo> cinfo;
|
2016-09-23 05:34:50 +00:00
|
|
|
if (FAILED(pWIC->CreateComponentInfo(targetGuid, cinfo.GetAddressOf())))
|
2016-08-22 18:26:36 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
WICComponentType type;
|
2016-09-23 05:34:50 +00:00
|
|
|
if (FAILED(cinfo->GetComponentType(&type)))
|
2016-08-22 18:26:36 +00:00
|
|
|
return 0;
|
|
|
|
|
2016-09-23 05:34:50 +00:00
|
|
|
if (type != WICPixelFormat)
|
2016-08-22 18:26:36 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
ComPtr<IWICPixelFormatInfo> pfinfo;
|
2016-09-23 05:34:50 +00:00
|
|
|
if (FAILED(cinfo.As(&pfinfo)))
|
2016-08-22 18:26:36 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
UINT bpp;
|
2016-09-23 05:34:50 +00:00
|
|
|
if (FAILED(pfinfo->GetBitsPerPixel(&bpp)))
|
2016-08-22 18:26:36 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
return bpp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------
|
2016-09-23 05:34:50 +00:00
|
|
|
DXGI_FORMAT MakeSRGB(_In_ DXGI_FORMAT format)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
2016-09-23 05:34:50 +00:00
|
|
|
switch (format)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
|
|
|
case DXGI_FORMAT_R8G8B8A8_UNORM:
|
|
|
|
return DXGI_FORMAT_R8G8B8A8_UNORM_SRGB;
|
|
|
|
|
|
|
|
case DXGI_FORMAT_BC1_UNORM:
|
|
|
|
return DXGI_FORMAT_BC1_UNORM_SRGB;
|
|
|
|
|
|
|
|
case DXGI_FORMAT_BC2_UNORM:
|
|
|
|
return DXGI_FORMAT_BC2_UNORM_SRGB;
|
|
|
|
|
|
|
|
case DXGI_FORMAT_BC3_UNORM:
|
|
|
|
return DXGI_FORMAT_BC3_UNORM_SRGB;
|
|
|
|
|
|
|
|
case DXGI_FORMAT_B8G8R8A8_UNORM:
|
|
|
|
return DXGI_FORMAT_B8G8R8A8_UNORM_SRGB;
|
|
|
|
|
|
|
|
case DXGI_FORMAT_B8G8R8X8_UNORM:
|
|
|
|
return DXGI_FORMAT_B8G8R8X8_UNORM_SRGB;
|
|
|
|
|
|
|
|
case DXGI_FORMAT_BC7_UNORM:
|
|
|
|
return DXGI_FORMAT_BC7_UNORM_SRGB;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return format;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------
|
2016-09-23 05:34:50 +00:00
|
|
|
HRESULT CreateTextureFromWIC(_In_ ID3D11Device* d3dDevice,
|
2016-08-22 18:26:36 +00:00
|
|
|
_In_opt_ ID3D11DeviceContext* d3dContext,
|
|
|
|
_In_ IWICBitmapFrameDecode *frame,
|
|
|
|
_In_ size_t maxsize,
|
|
|
|
_In_ D3D11_USAGE usage,
|
|
|
|
_In_ unsigned int bindFlags,
|
|
|
|
_In_ unsigned int cpuAccessFlags,
|
|
|
|
_In_ unsigned int miscFlags,
|
2016-09-23 05:34:50 +00:00
|
|
|
_In_ unsigned int loadFlags,
|
|
|
|
_Outptr_opt_ ID3D11Resource** texture,
|
|
|
|
_Outptr_opt_ ID3D11ShaderResourceView** textureView)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
|
|
|
UINT width, height;
|
2016-09-23 05:34:50 +00:00
|
|
|
HRESULT hr = frame->GetSize(&width, &height);
|
|
|
|
if (FAILED(hr))
|
2016-08-22 18:26:36 +00:00
|
|
|
return hr;
|
|
|
|
|
2016-09-23 05:34:50 +00:00
|
|
|
assert(width > 0 && height > 0);
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-23 05:34:50 +00:00
|
|
|
if (!maxsize)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
|
|
|
// This is a bit conservative because the hardware could support larger textures than
|
|
|
|
// the Feature Level defined minimums, but doing it this way is much easier and more
|
|
|
|
// performant for WIC than the 'fail and retry' model used by DDSTextureLoader
|
|
|
|
|
2016-09-23 05:34:50 +00:00
|
|
|
switch (d3dDevice->GetFeatureLevel())
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
2016-09-23 05:34:50 +00:00
|
|
|
case D3D_FEATURE_LEVEL_9_1:
|
|
|
|
case D3D_FEATURE_LEVEL_9_2:
|
|
|
|
maxsize = 2048 /*D3D_FL9_1_REQ_TEXTURE2D_U_OR_V_DIMENSION*/;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case D3D_FEATURE_LEVEL_9_3:
|
|
|
|
maxsize = 4096 /*D3D_FL9_3_REQ_TEXTURE2D_U_OR_V_DIMENSION*/;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case D3D_FEATURE_LEVEL_10_0:
|
|
|
|
case D3D_FEATURE_LEVEL_10_1:
|
|
|
|
maxsize = 8192 /*D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION*/;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
maxsize = D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION;
|
|
|
|
break;
|
2016-08-22 18:26:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-23 05:34:50 +00:00
|
|
|
assert(maxsize > 0);
|
2016-08-22 18:26:36 +00:00
|
|
|
|
|
|
|
UINT twidth, theight;
|
2016-09-23 05:34:50 +00:00
|
|
|
if (width > maxsize || height > maxsize)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
|
|
|
float ar = static_cast<float>(height) / static_cast<float>(width);
|
2016-09-23 05:34:50 +00:00
|
|
|
if (width > height)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
2016-09-23 05:34:50 +00:00
|
|
|
twidth = static_cast<UINT>(maxsize);
|
2016-12-08 22:26:41 +00:00
|
|
|
theight = std::max<UINT>(1, static_cast<UINT>(static_cast<float>(maxsize) * ar));
|
2016-08-22 18:26:36 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-09-23 05:34:50 +00:00
|
|
|
theight = static_cast<UINT>(maxsize);
|
2016-12-08 22:26:41 +00:00
|
|
|
twidth = std::max<UINT>(1, static_cast<UINT>(static_cast<float>(maxsize) / ar));
|
2016-08-22 18:26:36 +00:00
|
|
|
}
|
2016-09-23 05:34:50 +00:00
|
|
|
assert(twidth <= maxsize && theight <= maxsize);
|
2016-08-22 18:26:36 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
twidth = width;
|
|
|
|
theight = height;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine format
|
|
|
|
WICPixelFormatGUID pixelFormat;
|
2016-09-23 05:34:50 +00:00
|
|
|
hr = frame->GetPixelFormat(&pixelFormat);
|
|
|
|
if (FAILED(hr))
|
2016-08-22 18:26:36 +00:00
|
|
|
return hr;
|
|
|
|
|
|
|
|
WICPixelFormatGUID convertGUID;
|
2016-09-23 05:34:50 +00:00
|
|
|
memcpy(&convertGUID, &pixelFormat, sizeof(WICPixelFormatGUID));
|
2016-08-22 18:26:36 +00:00
|
|
|
|
|
|
|
size_t bpp = 0;
|
|
|
|
|
2016-09-23 05:34:50 +00:00
|
|
|
DXGI_FORMAT format = _WICToDXGI(pixelFormat);
|
|
|
|
if (format == DXGI_FORMAT_UNKNOWN)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
2016-09-23 05:34:50 +00:00
|
|
|
if (memcmp(&GUID_WICPixelFormat96bppRGBFixedPoint, &pixelFormat, sizeof(WICPixelFormatGUID)) == 0)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
2016-09-23 05:34:50 +00:00
|
|
|
#if (_WIN32_WINNT >= _WIN32_WINNT_WIN8) || defined(_WIN7_PLATFORM_UPDATE)
|
|
|
|
if (g_WIC2)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
2016-09-23 05:34:50 +00:00
|
|
|
memcpy(&convertGUID, &GUID_WICPixelFormat96bppRGBFloat, sizeof(WICPixelFormatGUID));
|
2016-08-22 18:26:36 +00:00
|
|
|
format = DXGI_FORMAT_R32G32B32_FLOAT;
|
2016-12-08 22:26:41 +00:00
|
|
|
bpp = 96;
|
2016-08-22 18:26:36 +00:00
|
|
|
}
|
|
|
|
else
|
2016-09-23 05:34:50 +00:00
|
|
|
#endif
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
2016-09-23 05:34:50 +00:00
|
|
|
memcpy(&convertGUID, &GUID_WICPixelFormat128bppRGBAFloat, sizeof(WICPixelFormatGUID));
|
2016-08-22 18:26:36 +00:00
|
|
|
format = DXGI_FORMAT_R32G32B32A32_FLOAT;
|
2016-12-08 22:26:41 +00:00
|
|
|
bpp = 128;
|
2016-08-22 18:26:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-09-23 05:34:50 +00:00
|
|
|
for (size_t i = 0; i < _countof(g_WICConvert); ++i)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
2016-09-23 05:34:50 +00:00
|
|
|
if (memcmp(&g_WICConvert[i].source, &pixelFormat, sizeof(WICPixelFormatGUID)) == 0)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
2016-09-23 05:34:50 +00:00
|
|
|
memcpy(&convertGUID, &g_WICConvert[i].target, sizeof(WICPixelFormatGUID));
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-23 05:34:50 +00:00
|
|
|
format = _WICToDXGI(g_WICConvert[i].target);
|
|
|
|
assert(format != DXGI_FORMAT_UNKNOWN);
|
|
|
|
bpp = _WICBitsPerPixel(convertGUID);
|
2016-08-22 18:26:36 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-23 05:34:50 +00:00
|
|
|
if (format == DXGI_FORMAT_UNKNOWN)
|
|
|
|
return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED);
|
2016-08-22 18:26:36 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-09-23 05:34:50 +00:00
|
|
|
bpp = _WICBitsPerPixel(pixelFormat);
|
2016-08-22 18:26:36 +00:00
|
|
|
}
|
|
|
|
|
2016-09-23 05:34:50 +00:00
|
|
|
#if (_WIN32_WINNT >= _WIN32_WINNT_WIN8) || defined(_WIN7_PLATFORM_UPDATE)
|
|
|
|
if ((format == DXGI_FORMAT_R32G32B32_FLOAT) && d3dContext != 0 && textureView != 0)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
|
|
|
// Special case test for optional device support for autogen mipchains for R32G32B32_FLOAT
|
|
|
|
UINT fmtSupport = 0;
|
2016-09-23 05:34:50 +00:00
|
|
|
hr = d3dDevice->CheckFormatSupport(DXGI_FORMAT_R32G32B32_FLOAT, &fmtSupport);
|
|
|
|
if (FAILED(hr) || !(fmtSupport & D3D11_FORMAT_SUPPORT_MIP_AUTOGEN))
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
|
|
|
// Use R32G32B32A32_FLOAT instead which is required for Feature Level 10.0 and up
|
2016-09-23 05:34:50 +00:00
|
|
|
memcpy(&convertGUID, &GUID_WICPixelFormat128bppRGBAFloat, sizeof(WICPixelFormatGUID));
|
2016-08-22 18:26:36 +00:00
|
|
|
format = DXGI_FORMAT_R32G32B32A32_FLOAT;
|
|
|
|
bpp = 128;
|
|
|
|
}
|
|
|
|
}
|
2016-09-23 05:34:50 +00:00
|
|
|
#endif
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-23 05:34:50 +00:00
|
|
|
if (!bpp)
|
2016-08-22 18:26:36 +00:00
|
|
|
return E_FAIL;
|
|
|
|
|
|
|
|
// Handle sRGB formats
|
2016-09-23 05:34:50 +00:00
|
|
|
if (loadFlags & WIC_LOADER_FORCE_SRGB)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
2016-09-23 05:34:50 +00:00
|
|
|
format = MakeSRGB(format);
|
2016-08-22 18:26:36 +00:00
|
|
|
}
|
2016-09-23 05:34:50 +00:00
|
|
|
else if (!(loadFlags & WIC_LOADER_IGNORE_SRGB))
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
|
|
|
ComPtr<IWICMetadataQueryReader> metareader;
|
2016-09-23 05:34:50 +00:00
|
|
|
if (SUCCEEDED(frame->GetMetadataQueryReader(metareader.GetAddressOf())))
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
|
|
|
GUID containerFormat;
|
2016-09-23 05:34:50 +00:00
|
|
|
if (SUCCEEDED(metareader->GetContainerFormat(&containerFormat)))
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
|
|
|
// Check for sRGB colorspace metadata
|
|
|
|
bool sRGB = false;
|
|
|
|
|
|
|
|
PROPVARIANT value;
|
2016-09-23 05:34:50 +00:00
|
|
|
PropVariantInit(&value);
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-23 05:34:50 +00:00
|
|
|
if (memcmp(&containerFormat, &GUID_ContainerFormatPng, sizeof(GUID)) == 0)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
|
|
|
// Check for sRGB chunk
|
2016-09-23 05:34:50 +00:00
|
|
|
if (SUCCEEDED(metareader->GetMetadataByName(L"/sRGB/RenderingIntent", &value)) && value.vt == VT_UI1)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
|
|
|
sRGB = true;
|
|
|
|
}
|
|
|
|
}
|
2016-09-23 05:34:50 +00:00
|
|
|
else if (SUCCEEDED(metareader->GetMetadataByName(L"System.Image.ColorSpace", &value)) && value.vt == VT_UI2 && value.uiVal == 1)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
|
|
|
sRGB = true;
|
|
|
|
}
|
|
|
|
|
2016-12-08 22:26:41 +00:00
|
|
|
(void)PropVariantClear(&value);
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-23 05:34:50 +00:00
|
|
|
if (sRGB)
|
|
|
|
format = MakeSRGB(format);
|
2016-08-22 18:26:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify our target format is supported by the current device
|
|
|
|
// (handles WDDM 1.0 or WDDM 1.1 device driver cases as well as DirectX 11.0 Runtime without 16bpp format support)
|
|
|
|
UINT support = 0;
|
2016-09-23 05:34:50 +00:00
|
|
|
hr = d3dDevice->CheckFormatSupport(format, &support);
|
|
|
|
if (FAILED(hr) || !(support & D3D11_FORMAT_SUPPORT_TEXTURE2D))
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
|
|
|
// Fallback to RGBA 32-bit format which is supported by all devices
|
2016-09-23 05:34:50 +00:00
|
|
|
memcpy(&convertGUID, &GUID_WICPixelFormat32bppRGBA, sizeof(WICPixelFormatGUID));
|
2016-08-22 18:26:36 +00:00
|
|
|
format = DXGI_FORMAT_R8G8B8A8_UNORM;
|
|
|
|
bpp = 32;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate temporary memory for image
|
2016-09-23 05:34:50 +00:00
|
|
|
size_t rowPitch = (twidth * bpp + 7) / 8;
|
2016-08-22 18:26:36 +00:00
|
|
|
size_t imageSize = rowPitch * theight;
|
|
|
|
|
2016-09-23 05:34:50 +00:00
|
|
|
std::unique_ptr<uint8_t[]> temp(new (std::nothrow) uint8_t[imageSize]);
|
2016-08-22 18:26:36 +00:00
|
|
|
if (!temp)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
|
|
|
// Load image data
|
2016-09-23 05:34:50 +00:00
|
|
|
if (memcmp(&convertGUID, &pixelFormat, sizeof(GUID)) == 0
|
|
|
|
&& twidth == width
|
|
|
|
&& theight == height)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
|
|
|
// No format conversion or resize needed
|
2016-09-23 05:34:50 +00:00
|
|
|
hr = frame->CopyPixels(0, static_cast<UINT>(rowPitch), static_cast<UINT>(imageSize), temp.get());
|
|
|
|
if (FAILED(hr))
|
2016-08-22 18:26:36 +00:00
|
|
|
return hr;
|
|
|
|
}
|
2016-09-23 05:34:50 +00:00
|
|
|
else if (twidth != width || theight != height)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
|
|
|
// Resize
|
2016-09-23 05:34:50 +00:00
|
|
|
auto pWIC = _GetWIC();
|
|
|
|
if (!pWIC)
|
2016-08-22 18:26:36 +00:00
|
|
|
return E_NOINTERFACE;
|
|
|
|
|
|
|
|
ComPtr<IWICBitmapScaler> scaler;
|
2016-09-23 05:34:50 +00:00
|
|
|
hr = pWIC->CreateBitmapScaler(scaler.GetAddressOf());
|
|
|
|
if (FAILED(hr))
|
2016-08-22 18:26:36 +00:00
|
|
|
return hr;
|
|
|
|
|
2016-09-23 05:34:50 +00:00
|
|
|
hr = scaler->Initialize(frame, twidth, theight, WICBitmapInterpolationModeFant);
|
|
|
|
if (FAILED(hr))
|
2016-08-22 18:26:36 +00:00
|
|
|
return hr;
|
|
|
|
|
|
|
|
WICPixelFormatGUID pfScaler;
|
2016-09-23 05:34:50 +00:00
|
|
|
hr = scaler->GetPixelFormat(&pfScaler);
|
|
|
|
if (FAILED(hr))
|
2016-08-22 18:26:36 +00:00
|
|
|
return hr;
|
|
|
|
|
2016-09-23 05:34:50 +00:00
|
|
|
if (memcmp(&convertGUID, &pfScaler, sizeof(GUID)) == 0)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
|
|
|
// No format conversion needed
|
2016-09-23 05:34:50 +00:00
|
|
|
hr = scaler->CopyPixels(0, static_cast<UINT>(rowPitch), static_cast<UINT>(imageSize), temp.get());
|
|
|
|
if (FAILED(hr))
|
2016-08-22 18:26:36 +00:00
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ComPtr<IWICFormatConverter> FC;
|
2016-09-23 05:34:50 +00:00
|
|
|
hr = pWIC->CreateFormatConverter(FC.GetAddressOf());
|
|
|
|
if (FAILED(hr))
|
2016-08-22 18:26:36 +00:00
|
|
|
return hr;
|
|
|
|
|
|
|
|
BOOL canConvert = FALSE;
|
2016-09-23 05:34:50 +00:00
|
|
|
hr = FC->CanConvert(pfScaler, convertGUID, &canConvert);
|
|
|
|
if (FAILED(hr) || !canConvert)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
|
|
|
return E_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
2017-07-03 23:04:01 +00:00
|
|
|
hr = FC->Initialize(scaler.Get(), convertGUID, WICBitmapDitherTypeErrorDiffusion, nullptr, 0, WICBitmapPaletteTypeMedianCut);
|
2016-09-23 05:34:50 +00:00
|
|
|
if (FAILED(hr))
|
2016-08-22 18:26:36 +00:00
|
|
|
return hr;
|
|
|
|
|
2016-09-23 05:34:50 +00:00
|
|
|
hr = FC->CopyPixels(0, static_cast<UINT>(rowPitch), static_cast<UINT>(imageSize), temp.get());
|
|
|
|
if (FAILED(hr))
|
2016-08-22 18:26:36 +00:00
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Format conversion but no resize
|
2016-09-23 05:34:50 +00:00
|
|
|
auto pWIC = _GetWIC();
|
|
|
|
if (!pWIC)
|
2016-08-22 18:26:36 +00:00
|
|
|
return E_NOINTERFACE;
|
|
|
|
|
|
|
|
ComPtr<IWICFormatConverter> FC;
|
2016-09-23 05:34:50 +00:00
|
|
|
hr = pWIC->CreateFormatConverter(FC.GetAddressOf());
|
|
|
|
if (FAILED(hr))
|
2016-08-22 18:26:36 +00:00
|
|
|
return hr;
|
|
|
|
|
|
|
|
BOOL canConvert = FALSE;
|
2016-09-23 05:34:50 +00:00
|
|
|
hr = FC->CanConvert(pixelFormat, convertGUID, &canConvert);
|
|
|
|
if (FAILED(hr) || !canConvert)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
|
|
|
return E_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
2017-07-03 23:04:01 +00:00
|
|
|
hr = FC->Initialize(frame, convertGUID, WICBitmapDitherTypeErrorDiffusion, nullptr, 0, WICBitmapPaletteTypeMedianCut);
|
2016-09-23 05:34:50 +00:00
|
|
|
if (FAILED(hr))
|
2016-08-22 18:26:36 +00:00
|
|
|
return hr;
|
|
|
|
|
2016-09-23 05:34:50 +00:00
|
|
|
hr = FC->CopyPixels(0, static_cast<UINT>(rowPitch), static_cast<UINT>(imageSize), temp.get());
|
|
|
|
if (FAILED(hr))
|
2016-08-22 18:26:36 +00:00
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// See if format is supported for auto-gen mipmaps (varies by feature level)
|
|
|
|
bool autogen = false;
|
2016-09-23 05:34:50 +00:00
|
|
|
if (d3dContext != 0 && textureView != 0) // Must have context and shader-view to auto generate mipmaps
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
|
|
|
UINT fmtSupport = 0;
|
2016-09-23 05:34:50 +00:00
|
|
|
hr = d3dDevice->CheckFormatSupport(format, &fmtSupport);
|
|
|
|
if (SUCCEEDED(hr) && (fmtSupport & D3D11_FORMAT_SUPPORT_MIP_AUTOGEN))
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
|
|
|
autogen = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create texture
|
|
|
|
D3D11_TEXTURE2D_DESC desc;
|
|
|
|
desc.Width = twidth;
|
|
|
|
desc.Height = theight;
|
|
|
|
desc.MipLevels = (autogen) ? 0 : 1;
|
|
|
|
desc.ArraySize = 1;
|
|
|
|
desc.Format = format;
|
|
|
|
desc.SampleDesc.Count = 1;
|
|
|
|
desc.SampleDesc.Quality = 0;
|
|
|
|
desc.Usage = usage;
|
|
|
|
desc.CPUAccessFlags = cpuAccessFlags;
|
|
|
|
|
2016-09-23 05:34:50 +00:00
|
|
|
if (autogen)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
|
|
|
desc.BindFlags = bindFlags | D3D11_BIND_RENDER_TARGET;
|
|
|
|
desc.MiscFlags = miscFlags | D3D11_RESOURCE_MISC_GENERATE_MIPS;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
desc.BindFlags = bindFlags;
|
|
|
|
desc.MiscFlags = miscFlags;
|
|
|
|
}
|
|
|
|
|
|
|
|
D3D11_SUBRESOURCE_DATA initData;
|
|
|
|
initData.pSysMem = temp.get();
|
2016-09-23 05:34:50 +00:00
|
|
|
initData.SysMemPitch = static_cast<UINT>(rowPitch);
|
|
|
|
initData.SysMemSlicePitch = static_cast<UINT>(imageSize);
|
2016-08-22 18:26:36 +00:00
|
|
|
|
|
|
|
ID3D11Texture2D* tex = nullptr;
|
2016-09-23 05:34:50 +00:00
|
|
|
hr = d3dDevice->CreateTexture2D(&desc, (autogen) ? nullptr : &initData, &tex);
|
|
|
|
if (SUCCEEDED(hr) && tex != 0)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
|
|
|
if (textureView != 0)
|
|
|
|
{
|
|
|
|
D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc = {};
|
|
|
|
SRVDesc.Format = desc.Format;
|
|
|
|
|
|
|
|
SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
|
|
|
|
SRVDesc.Texture2D.MipLevels = (autogen) ? -1 : 1;
|
|
|
|
|
2016-09-23 05:34:50 +00:00
|
|
|
hr = d3dDevice->CreateShaderResourceView(tex, &SRVDesc, textureView);
|
|
|
|
if (FAILED(hr))
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
|
|
|
tex->Release();
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
2016-09-23 05:34:50 +00:00
|
|
|
if (autogen)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
2016-09-23 05:34:50 +00:00
|
|
|
assert(d3dContext != 0);
|
|
|
|
d3dContext->UpdateSubresource(tex, 0, nullptr, temp.get(), static_cast<UINT>(rowPitch), static_cast<UINT>(imageSize));
|
|
|
|
d3dContext->GenerateMips(*textureView);
|
2016-08-22 18:26:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (texture != 0)
|
|
|
|
{
|
|
|
|
*texture = tex;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SetDebugObjectName(tex, "WICTextureLoader");
|
|
|
|
tex->Release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
_Use_decl_annotations_
|
2016-09-23 05:34:50 +00:00
|
|
|
HRESULT DirectX::CreateWICTextureFromMemory(ID3D11Device* d3dDevice,
|
|
|
|
const uint8_t* wicData,
|
|
|
|
size_t wicDataSize,
|
|
|
|
ID3D11Resource** texture,
|
|
|
|
ID3D11ShaderResourceView** textureView,
|
|
|
|
size_t maxsize)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
2016-09-23 05:34:50 +00:00
|
|
|
return CreateWICTextureFromMemoryEx(d3dDevice, nullptr, wicData, wicDataSize, maxsize,
|
|
|
|
D3D11_USAGE_DEFAULT, D3D11_BIND_SHADER_RESOURCE, 0, 0, WIC_LOADER_DEFAULT,
|
|
|
|
texture, textureView);
|
2016-08-22 18:26:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_Use_decl_annotations_
|
2016-09-23 05:34:50 +00:00
|
|
|
HRESULT DirectX::CreateWICTextureFromMemory(ID3D11Device* d3dDevice,
|
|
|
|
ID3D11DeviceContext* d3dContext,
|
|
|
|
const uint8_t* wicData,
|
|
|
|
size_t wicDataSize,
|
|
|
|
ID3D11Resource** texture,
|
|
|
|
ID3D11ShaderResourceView** textureView,
|
|
|
|
size_t maxsize)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
2016-09-23 05:34:50 +00:00
|
|
|
return CreateWICTextureFromMemoryEx(d3dDevice, d3dContext, wicData, wicDataSize, maxsize,
|
|
|
|
D3D11_USAGE_DEFAULT, D3D11_BIND_SHADER_RESOURCE, 0, 0, WIC_LOADER_DEFAULT,
|
|
|
|
texture, textureView);
|
2016-08-22 18:26:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_Use_decl_annotations_
|
2016-09-23 05:34:50 +00:00
|
|
|
HRESULT DirectX::CreateWICTextureFromMemoryEx(ID3D11Device* d3dDevice,
|
|
|
|
const uint8_t* wicData,
|
|
|
|
size_t wicDataSize,
|
|
|
|
size_t maxsize,
|
|
|
|
D3D11_USAGE usage,
|
|
|
|
unsigned int bindFlags,
|
|
|
|
unsigned int cpuAccessFlags,
|
|
|
|
unsigned int miscFlags,
|
|
|
|
unsigned int loadFlags,
|
|
|
|
ID3D11Resource** texture,
|
|
|
|
ID3D11ShaderResourceView** textureView)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
2016-09-23 05:34:50 +00:00
|
|
|
return CreateWICTextureFromMemoryEx(d3dDevice, nullptr, wicData, wicDataSize, maxsize,
|
|
|
|
usage, bindFlags, cpuAccessFlags, miscFlags, loadFlags,
|
|
|
|
texture, textureView);
|
2016-08-22 18:26:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_Use_decl_annotations_
|
2016-09-23 05:34:50 +00:00
|
|
|
HRESULT DirectX::CreateWICTextureFromMemoryEx(ID3D11Device* d3dDevice,
|
|
|
|
ID3D11DeviceContext* d3dContext,
|
|
|
|
const uint8_t* wicData,
|
|
|
|
size_t wicDataSize,
|
|
|
|
size_t maxsize,
|
|
|
|
D3D11_USAGE usage,
|
|
|
|
unsigned int bindFlags,
|
|
|
|
unsigned int cpuAccessFlags,
|
|
|
|
unsigned int miscFlags,
|
|
|
|
unsigned int loadFlags,
|
|
|
|
ID3D11Resource** texture,
|
|
|
|
ID3D11ShaderResourceView** textureView)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
2016-09-23 05:34:50 +00:00
|
|
|
if (texture)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
|
|
|
*texture = nullptr;
|
|
|
|
}
|
2016-09-23 05:34:50 +00:00
|
|
|
if (textureView)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
|
|
|
*textureView = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!d3dDevice || !wicData || (!texture && !textureView))
|
|
|
|
return E_INVALIDARG;
|
|
|
|
|
2016-09-23 05:34:50 +00:00
|
|
|
if (!wicDataSize)
|
2016-08-22 18:26:36 +00:00
|
|
|
return E_FAIL;
|
|
|
|
|
2016-09-23 05:34:50 +00:00
|
|
|
if (wicDataSize > UINT32_MAX)
|
|
|
|
return HRESULT_FROM_WIN32(ERROR_FILE_TOO_LARGE);
|
2016-08-22 18:26:36 +00:00
|
|
|
|
2016-09-23 05:34:50 +00:00
|
|
|
auto pWIC = _GetWIC();
|
|
|
|
if (!pWIC)
|
2016-08-22 18:26:36 +00:00
|
|
|
return E_NOINTERFACE;
|
|
|
|
|
|
|
|
// Create input stream for memory
|
|
|
|
ComPtr<IWICStream> stream;
|
2016-09-23 05:34:50 +00:00
|
|
|
HRESULT hr = pWIC->CreateStream(stream.GetAddressOf());
|
|
|
|
if (FAILED(hr))
|
2016-08-22 18:26:36 +00:00
|
|
|
return hr;
|
|
|
|
|
2016-09-23 05:34:50 +00:00
|
|
|
hr = stream->InitializeFromMemory(const_cast<uint8_t*>(wicData), static_cast<DWORD>(wicDataSize));
|
|
|
|
if (FAILED(hr))
|
2016-08-22 18:26:36 +00:00
|
|
|
return hr;
|
|
|
|
|
|
|
|
// Initialize WIC
|
|
|
|
ComPtr<IWICBitmapDecoder> decoder;
|
2016-09-23 05:34:50 +00:00
|
|
|
hr = pWIC->CreateDecoderFromStream(stream.Get(), 0, WICDecodeMetadataCacheOnDemand, decoder.GetAddressOf());
|
|
|
|
if (FAILED(hr))
|
2016-08-22 18:26:36 +00:00
|
|
|
return hr;
|
|
|
|
|
|
|
|
ComPtr<IWICBitmapFrameDecode> frame;
|
2016-09-23 05:34:50 +00:00
|
|
|
hr = decoder->GetFrame(0, frame.GetAddressOf());
|
|
|
|
if (FAILED(hr))
|
2016-08-22 18:26:36 +00:00
|
|
|
return hr;
|
|
|
|
|
2016-09-23 05:34:50 +00:00
|
|
|
hr = CreateTextureFromWIC(d3dDevice, d3dContext, frame.Get(), maxsize,
|
|
|
|
usage, bindFlags, cpuAccessFlags, miscFlags, loadFlags,
|
|
|
|
texture, textureView);
|
|
|
|
if (FAILED(hr))
|
2016-08-22 18:26:36 +00:00
|
|
|
return hr;
|
|
|
|
|
|
|
|
if (texture != 0 && *texture != 0)
|
|
|
|
{
|
|
|
|
SetDebugObjectName(*texture, "WICTextureLoader");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (textureView != 0 && *textureView != 0)
|
|
|
|
{
|
|
|
|
SetDebugObjectName(*textureView, "WICTextureLoader");
|
|
|
|
}
|
|
|
|
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
_Use_decl_annotations_
|
2016-09-23 05:34:50 +00:00
|
|
|
HRESULT DirectX::CreateWICTextureFromFile(ID3D11Device* d3dDevice,
|
|
|
|
const wchar_t* fileName,
|
|
|
|
ID3D11Resource** texture,
|
|
|
|
ID3D11ShaderResourceView** textureView,
|
|
|
|
size_t maxsize)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
2016-09-23 05:34:50 +00:00
|
|
|
return CreateWICTextureFromFileEx(d3dDevice, nullptr, fileName, maxsize,
|
|
|
|
D3D11_USAGE_DEFAULT, D3D11_BIND_SHADER_RESOURCE, 0, 0, WIC_LOADER_DEFAULT,
|
|
|
|
texture, textureView);
|
2016-08-22 18:26:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_Use_decl_annotations_
|
2016-09-23 05:34:50 +00:00
|
|
|
HRESULT DirectX::CreateWICTextureFromFile(ID3D11Device* d3dDevice,
|
|
|
|
ID3D11DeviceContext* d3dContext,
|
|
|
|
const wchar_t* fileName,
|
|
|
|
ID3D11Resource** texture,
|
|
|
|
ID3D11ShaderResourceView** textureView,
|
|
|
|
size_t maxsize)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
2016-09-23 05:34:50 +00:00
|
|
|
return CreateWICTextureFromFileEx(d3dDevice, d3dContext, fileName, maxsize,
|
|
|
|
D3D11_USAGE_DEFAULT, D3D11_BIND_SHADER_RESOURCE, 0, 0, WIC_LOADER_DEFAULT,
|
|
|
|
texture, textureView);
|
2016-08-22 18:26:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_Use_decl_annotations_
|
2016-09-23 05:34:50 +00:00
|
|
|
HRESULT DirectX::CreateWICTextureFromFileEx(ID3D11Device* d3dDevice,
|
|
|
|
const wchar_t* fileName,
|
|
|
|
size_t maxsize,
|
|
|
|
D3D11_USAGE usage,
|
|
|
|
unsigned int bindFlags,
|
|
|
|
unsigned int cpuAccessFlags,
|
|
|
|
unsigned int miscFlags,
|
|
|
|
unsigned int loadFlags,
|
|
|
|
ID3D11Resource** texture,
|
|
|
|
ID3D11ShaderResourceView** textureView)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
2016-09-23 05:34:50 +00:00
|
|
|
return CreateWICTextureFromFileEx(d3dDevice, nullptr, fileName, maxsize,
|
|
|
|
usage, bindFlags, cpuAccessFlags, miscFlags, loadFlags,
|
|
|
|
texture, textureView);
|
2016-08-22 18:26:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_Use_decl_annotations_
|
2016-09-23 05:34:50 +00:00
|
|
|
HRESULT DirectX::CreateWICTextureFromFileEx(ID3D11Device* d3dDevice,
|
|
|
|
ID3D11DeviceContext* d3dContext,
|
|
|
|
const wchar_t* fileName,
|
|
|
|
size_t maxsize,
|
|
|
|
D3D11_USAGE usage,
|
|
|
|
unsigned int bindFlags,
|
|
|
|
unsigned int cpuAccessFlags,
|
|
|
|
unsigned int miscFlags,
|
|
|
|
unsigned int loadFlags,
|
|
|
|
ID3D11Resource** texture,
|
|
|
|
ID3D11ShaderResourceView** textureView)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
2016-09-23 05:34:50 +00:00
|
|
|
if (texture)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
|
|
|
*texture = nullptr;
|
|
|
|
}
|
2016-09-23 05:34:50 +00:00
|
|
|
if (textureView)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
|
|
|
*textureView = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!d3dDevice || !fileName || (!texture && !textureView))
|
|
|
|
return E_INVALIDARG;
|
|
|
|
|
2016-09-23 05:34:50 +00:00
|
|
|
auto pWIC = _GetWIC();
|
|
|
|
if (!pWIC)
|
2016-08-22 18:26:36 +00:00
|
|
|
return E_NOINTERFACE;
|
|
|
|
|
|
|
|
// Initialize WIC
|
|
|
|
ComPtr<IWICBitmapDecoder> decoder;
|
2016-09-23 05:34:50 +00:00
|
|
|
HRESULT hr = pWIC->CreateDecoderFromFilename(fileName, 0, GENERIC_READ, WICDecodeMetadataCacheOnDemand, decoder.GetAddressOf());
|
|
|
|
if (FAILED(hr))
|
2016-08-22 18:26:36 +00:00
|
|
|
return hr;
|
|
|
|
|
|
|
|
ComPtr<IWICBitmapFrameDecode> frame;
|
2016-09-23 05:34:50 +00:00
|
|
|
hr = decoder->GetFrame(0, frame.GetAddressOf());
|
|
|
|
if (FAILED(hr))
|
2016-08-22 18:26:36 +00:00
|
|
|
return hr;
|
|
|
|
|
2016-09-23 05:34:50 +00:00
|
|
|
hr = CreateTextureFromWIC(d3dDevice, d3dContext, frame.Get(), maxsize,
|
|
|
|
usage, bindFlags, cpuAccessFlags, miscFlags, loadFlags,
|
|
|
|
texture, textureView);
|
2016-08-22 18:26:36 +00:00
|
|
|
|
|
|
|
#if !defined(NO_D3D11_DEBUG_NAME) && ( defined(_DEBUG) || defined(PROFILE) )
|
2016-09-23 05:34:50 +00:00
|
|
|
if (SUCCEEDED(hr))
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
|
|
|
if (texture != 0 || textureView != 0)
|
|
|
|
{
|
2017-01-10 22:51:52 +00:00
|
|
|
char strFileA[MAX_PATH];
|
2016-09-23 05:34:50 +00:00
|
|
|
int result = WideCharToMultiByte(CP_ACP,
|
|
|
|
WC_NO_BEST_FIT_CHARS,
|
|
|
|
fileName,
|
|
|
|
-1,
|
|
|
|
strFileA,
|
|
|
|
MAX_PATH,
|
|
|
|
nullptr,
|
|
|
|
FALSE
|
|
|
|
);
|
|
|
|
if (result > 0)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
2017-01-10 22:51:52 +00:00
|
|
|
const char* pstrName = strrchr(strFileA, '\\');
|
2016-08-22 18:26:36 +00:00
|
|
|
if (!pstrName)
|
|
|
|
{
|
|
|
|
pstrName = strFileA;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pstrName++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (texture != 0 && *texture != 0)
|
|
|
|
{
|
2016-09-23 05:34:50 +00:00
|
|
|
(*texture)->SetPrivateData(WKPDID_D3DDebugObjectName,
|
|
|
|
static_cast<UINT>(strnlen_s(pstrName, MAX_PATH)),
|
|
|
|
pstrName
|
|
|
|
);
|
2016-08-22 18:26:36 +00:00
|
|
|
}
|
|
|
|
|
2016-09-23 05:34:50 +00:00
|
|
|
if (textureView != 0 && *textureView != 0)
|
2016-08-22 18:26:36 +00:00
|
|
|
{
|
2016-09-23 05:34:50 +00:00
|
|
|
(*textureView)->SetPrivateData(WKPDID_D3DDebugObjectName,
|
|
|
|
static_cast<UINT>(strnlen_s(pstrName, MAX_PATH)),
|
|
|
|
pstrName
|
|
|
|
);
|
2016-08-22 18:26:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return hr;
|
|
|
|
}
|