From a6d65be36e972dfc78b460755b6c47fa031378ab Mon Sep 17 00:00:00 2001 From: walbourn_cp Date: Wed, 14 Nov 2012 14:16:55 -0800 Subject: [PATCH] DirectXTK: Added support for WIC2 when available to WICTextureLoader and ScreenGrab --- DirectXTK_Desktop_2010.vcxproj | 8 +-- DirectXTK_Desktop_2012.vcxproj | 8 +-- Src/ScreenGrab.cpp | 12 ++++- Src/WICTextureLoader.cpp | 92 +++++++++++++++++++++++++++------- 4 files changed, 93 insertions(+), 27 deletions(-) diff --git a/DirectXTK_Desktop_2010.vcxproj b/DirectXTK_Desktop_2010.vcxproj index bbe2570d7..d5319080c 100644 --- a/DirectXTK_Desktop_2010.vcxproj +++ b/DirectXTK_Desktop_2010.vcxproj @@ -231,7 +231,7 @@ Use Level3 Disabled - _WIN32_WINNT=0x0600;WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) + _WIN32_WINNT=0x0600;_WIN7_PLATFORM_UPDATE;WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) pch.h $(ProjectDir)Inc;%(AdditionalIncludeDirectories) @@ -245,7 +245,7 @@ Use Level3 Disabled - _WIN32_WINNT=0x0600;WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) + _WIN32_WINNT=0x0600;_WIN7_PLATFORM_UPDATE;WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) pch.h $(ProjectDir)Inc;%(AdditionalIncludeDirectories) @@ -261,7 +261,7 @@ MaxSpeed true true - _WIN32_WINNT=0x0600;WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions) + _WIN32_WINNT=0x0600;_WIN7_PLATFORM_UPDATE;WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions) pch.h $(ProjectDir)Inc;%(AdditionalIncludeDirectories) true @@ -280,7 +280,7 @@ MaxSpeed true true - _WIN32_WINNT=0x0600;WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions) + _WIN32_WINNT=0x0600;_WIN7_PLATFORM_UPDATE;WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions) pch.h $(ProjectDir)Inc;%(AdditionalIncludeDirectories) true diff --git a/DirectXTK_Desktop_2012.vcxproj b/DirectXTK_Desktop_2012.vcxproj index b331e501c..20a56b39f 100644 --- a/DirectXTK_Desktop_2012.vcxproj +++ b/DirectXTK_Desktop_2012.vcxproj @@ -234,7 +234,7 @@ Use Level3 Disabled - _WIN32_WINNT=0x0600;WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) + _WIN32_WINNT=0x0600;_WIN7_PLATFORM_UPDATE;WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) pch.h $(ProjectDir)Inc;%(AdditionalIncludeDirectories) @@ -248,7 +248,7 @@ Use Level3 Disabled - _WIN32_WINNT=0x0600;WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) + _WIN32_WINNT=0x0600;_WIN7_PLATFORM_UPDATE;WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) pch.h $(ProjectDir)Inc;%(AdditionalIncludeDirectories) @@ -264,7 +264,7 @@ MaxSpeed true true - _WIN32_WINNT=0x0600;WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions) + _WIN32_WINNT=0x0600;_WIN7_PLATFORM_UPDATE;WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions) pch.h $(ProjectDir)Inc;%(AdditionalIncludeDirectories) true @@ -283,7 +283,7 @@ MaxSpeed true true - _WIN32_WINNT=0x0600;WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions) + _WIN32_WINNT=0x0600;_WIN7_PLATFORM_UPDATE;WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions) pch.h $(ProjectDir)Inc;%(AdditionalIncludeDirectories) true diff --git a/Src/ScreenGrab.cpp b/Src/ScreenGrab.cpp index 8dd18b805..c43b51796 100644 --- a/Src/ScreenGrab.cpp +++ b/Src/ScreenGrab.cpp @@ -589,6 +589,7 @@ HRESULT DirectX::SaveDDSTextureToFile( _In_ ID3D11DeviceContext* pContext, namespace DirectX { +extern bool _IsWIC2(); extern IWICImagingFactory* _GetWIC(); } @@ -714,11 +715,18 @@ HRESULT DirectX::SaveWICTextureToFile( _In_ ID3D11DeviceContext* pContext, // Screenshots don’t typically include the alpha channel of the render target switch ( desc.Format ) { -#if (_WIN32_WINNT >= 0x0602 /*_WIN32_WINNT_WIN8*/) +#if (_WIN32_WINNT >= 0x0602 /*_WIN32_WINNT_WIN8*/) || defined(_WIN7_PLATFORM_UPDATE) case DXGI_FORMAT_R32G32B32A32_FLOAT: case DXGI_FORMAT_R16G16B16A16_FLOAT: case DXGI_FORMAT_R9G9B9E5_SHAREDEXP: - targetGuid = GUID_WICPixelFormat96bppRGBFloat; + if ( _IsWIC2() ) + { + targetGuid = GUID_WICPixelFormat96bppRGBFloat; + } + else + { + targetGuid = GUID_WICPixelFormat24bppBGR; + } break; #endif diff --git a/Src/WICTextureLoader.cpp b/Src/WICTextureLoader.cpp index 1ff84fd4c..389daac48 100644 --- a/Src/WICTextureLoader.cpp +++ b/Src/WICTextureLoader.cpp @@ -78,10 +78,6 @@ static WICTranslate g_WICFormats[] = { GUID_WICPixelFormat8bppGray, DXGI_FORMAT_R8_UNORM }, { GUID_WICPixelFormat8bppAlpha, DXGI_FORMAT_A8_UNORM }, - -#if (_WIN32_WINNT >= 0x0602 /*_WIN32_WINNT_WIN8*/) - { GUID_WICPixelFormat96bppRGBFloat, DXGI_FORMAT_R32G32B32_FLOAT }, -#endif }; //------------------------------------------------------------------------------------- @@ -154,22 +150,26 @@ static WICConvert g_WICConvert[] = { GUID_WICPixelFormat40bppCMYKAlpha, GUID_WICPixelFormat64bppRGBA }, // DXGI_FORMAT_R16G16B16A16_UNORM { GUID_WICPixelFormat80bppCMYKAlpha, GUID_WICPixelFormat64bppRGBA }, // DXGI_FORMAT_R16G16B16A16_UNORM -#if (_WIN32_WINNT >= 0x0602 /*_WIN32_WINNT_WIN8*/) +#if (_WIN32_WINNT >= 0x0602 /*_WIN32_WINNT_WIN8*/) || defined(_WIN7_PLATFORM_UPDATE) { GUID_WICPixelFormat32bppRGB, GUID_WICPixelFormat32bppRGBA }, // DXGI_FORMAT_R8G8B8A8_UNORM { GUID_WICPixelFormat64bppRGB, GUID_WICPixelFormat64bppRGBA }, // DXGI_FORMAT_R16G16B16A16_UNORM { GUID_WICPixelFormat64bppPRGBAHalf, GUID_WICPixelFormat64bppRGBAHalf }, // DXGI_FORMAT_R16G16B16A16_FLOAT - { GUID_WICPixelFormat96bppRGBFixedPoint, GUID_WICPixelFormat96bppRGBFloat }, // DXGI_FORMAT_R32G32B32_FLOAT -#else - { GUID_WICPixelFormat96bppRGBFixedPoint, GUID_WICPixelFormat128bppRGBAFloat }, // DXGI_FORMAT_R32G32B32A32_FLOAT #endif // We don't support n-channel formats }; +static bool g_WIC2 = false; + //-------------------------------------------------------------------------------------- namespace DirectX { +bool _IsWIC2() +{ + return g_WIC2; +} + IWICImagingFactory* _GetWIC() { static IWICImagingFactory* s_Factory = nullptr; @@ -177,6 +177,37 @@ IWICImagingFactory* _GetWIC() if ( s_Factory ) return s_Factory; +#if(_WIN32_WINNT >= 0x0602 /*_WIN32_WINNT_WIN8*/) || defined(_WIN7_PLATFORM_UPDATE) + HRESULT hr = CoCreateInstance( + CLSID_WICImagingFactory2, + nullptr, + CLSCTX_INPROC_SERVER, + __uuidof(IWICImagingFactory2), + (LPVOID*)&s_Factory + ); + + if ( SUCCEEDED(hr) ) + { + // WIC2 is available on Windows 8 and Windows 7 SP1 with KB 2670838 installed + g_WIC2 = true; + } + else + { + hr = CoCreateInstance( + CLSID_WICImagingFactory1, + nullptr, + CLSCTX_INPROC_SERVER, + __uuidof(IWICImagingFactory), + (LPVOID*)&s_Factory + ); + + if ( FAILED(hr) ) + { + s_Factory = nullptr; + return nullptr; + } + } +#else HRESULT hr = CoCreateInstance( CLSID_WICImagingFactory, nullptr, @@ -190,6 +221,7 @@ IWICImagingFactory* _GetWIC() s_Factory = nullptr; return nullptr; } +#endif return s_Factory; } @@ -206,6 +238,14 @@ static DXGI_FORMAT _WICToDXGI( const GUID& guid ) return g_WICFormats[i].format; } +#if (_WIN32_WINNT >= 0x0602 /*_WIN32_WINNT_WIN8*/) || defined(_WIN7_PLATFORM_UPDATE) + if ( g_WIC2 ) + { + if ( memcmp( &GUID_WICPixelFormat96bppRGBFloat, &guid, sizeof(GUID) ) == 0 ) + return DXGI_FORMAT_R32G32B32_FLOAT; + } +#endif + return DXGI_FORMAT_UNKNOWN; } @@ -319,16 +359,34 @@ static HRESULT CreateTextureFromWIC( _In_ ID3D11Device* d3dDevice, DXGI_FORMAT format = _WICToDXGI( pixelFormat ); if ( format == DXGI_FORMAT_UNKNOWN ) { - for( size_t i=0; i < _countof(g_WICConvert); ++i ) + if ( memcmp( &GUID_WICPixelFormat96bppRGBFixedPoint, &pixelFormat, sizeof(WICPixelFormatGUID) ) == 0 ) { - if ( memcmp( &g_WICConvert[i].source, &pixelFormat, sizeof(WICPixelFormatGUID) ) == 0 ) +#if (_WIN32_WINNT >= 0x0602 /*_WIN32_WINNT_WIN8*/) || defined(_WIN7_PLATFORM_UPDATE) + if ( g_WIC2 ) { - memcpy( &convertGUID, &g_WICConvert[i].target, sizeof(WICPixelFormatGUID) ); - - format = _WICToDXGI( g_WICConvert[i].target ); - assert( format != DXGI_FORMAT_UNKNOWN ); - bpp = _WICBitsPerPixel( convertGUID ); - break; + memcpy( &convertGUID, &GUID_WICPixelFormat96bppRGBFloat, sizeof(WICPixelFormatGUID) ); + format = DXGI_FORMAT_R32G32B32_FLOAT; + } + else +#endif + { + memcpy( &convertGUID, &GUID_WICPixelFormat128bppRGBAFloat, sizeof(WICPixelFormatGUID) ); + format = DXGI_FORMAT_R32G32B32A32_FLOAT; + } + } + else + { + for( size_t i=0; i < _countof(g_WICConvert); ++i ) + { + if ( memcmp( &g_WICConvert[i].source, &pixelFormat, sizeof(WICPixelFormatGUID) ) == 0 ) + { + memcpy( &convertGUID, &g_WICConvert[i].target, sizeof(WICPixelFormatGUID) ); + + format = _WICToDXGI( g_WICConvert[i].target ); + assert( format != DXGI_FORMAT_UNKNOWN ); + bpp = _WICBitsPerPixel( convertGUID ); + break; + } } } @@ -340,7 +398,7 @@ static HRESULT CreateTextureFromWIC( _In_ ID3D11Device* d3dDevice, bpp = _WICBitsPerPixel( pixelFormat ); } -#if (_WIN32_WINNT >= 0x0602 /*_WIN32_WINNT_WIN8*/) +#if (_WIN32_WINNT >= 0x0602 /*_WIN32_WINNT_WIN8*/) || defined(_WIN7_PLATFORM_UPDATE) if ( (format == DXGI_FORMAT_R32G32B32_FLOAT) && d3dContext != 0 && textureView != 0 ) { // Special case test for optional device support for autogen mipchains for R32G32B32_FLOAT