Skip to content

Commit 72a1f29

Browse files
walbourn_cpwalbourn_cp
walbourn_cp
authored and
walbourn_cp
committed
DirectXTex: WIC2 support for ScreenGrab & WICTextureLoader
1 parent 6a90510 commit 72a1f29

File tree

3 files changed

+114
-20
lines changed

3 files changed

+114
-20
lines changed

DirectXTex/DirectXTexUtil.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ IWICImagingFactory* _GetWIC()
153153

154154
if ( SUCCEEDED(hr) )
155155
{
156+
// WIC2 is available on Windows 8 and Windows 7 SP1 with KB 2670838 installed
156157
g_WIC2 = true;
157158
}
158159
else

ScreenGrab/ScreenGrab.cpp

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -624,13 +624,46 @@ static HRESULT CaptureTexture( _In_ ID3D11DeviceContext* pContext,
624624
//--------------------------------------------------------------------------------------
625625
#if !defined(WINAPI_FAMILY) || (WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP)
626626

627+
static bool g_WIC2 = false;
628+
627629
static IWICImagingFactory* _GetWIC()
628630
{
629631
static IWICImagingFactory* s_Factory = nullptr;
630632

631633
if ( s_Factory )
632634
return s_Factory;
633635

636+
#if(_WIN32_WINNT >= 0x0602 /*_WIN32_WINNT_WIN8*/) || defined(_WIN7_PLATFORM_UPDATE)
637+
HRESULT hr = CoCreateInstance(
638+
CLSID_WICImagingFactory2,
639+
nullptr,
640+
CLSCTX_INPROC_SERVER,
641+
__uuidof(IWICImagingFactory2),
642+
(LPVOID*)&s_Factory
643+
);
644+
645+
if ( SUCCEEDED(hr) )
646+
{
647+
// WIC2 is available on Windows 8 and Windows 7 SP1 with KB 2670838 installed
648+
g_WIC2 = true;
649+
}
650+
else
651+
{
652+
hr = CoCreateInstance(
653+
CLSID_WICImagingFactory1,
654+
nullptr,
655+
CLSCTX_INPROC_SERVER,
656+
__uuidof(IWICImagingFactory),
657+
(LPVOID*)&s_Factory
658+
);
659+
660+
if ( FAILED(hr) )
661+
{
662+
s_Factory = nullptr;
663+
return nullptr;
664+
}
665+
}
666+
#else
634667
HRESULT hr = CoCreateInstance(
635668
CLSID_WICImagingFactory,
636669
nullptr,
@@ -644,10 +677,10 @@ static IWICImagingFactory* _GetWIC()
644677
s_Factory = nullptr;
645678
return nullptr;
646679
}
680+
#endif
647681

648682
return s_Factory;
649683
}
650-
651684
#endif
652685

653686

@@ -923,11 +956,18 @@ HRESULT DirectX::SaveWICTextureToFile( _In_ ID3D11DeviceContext* pContext,
923956
// Screenshots don’t typically include the alpha channel of the render target
924957
switch ( desc.Format )
925958
{
926-
#if (_WIN32_WINNT >= 0x0602 /*_WIN32_WINNT_WIN8*/)
959+
#if (_WIN32_WINNT >= 0x0602 /*_WIN32_WINNT_WIN8*/) || defined(_WIN7_PLATFORM_UPDATE)
927960
case DXGI_FORMAT_R32G32B32A32_FLOAT:
928961
case DXGI_FORMAT_R16G16B16A16_FLOAT:
929962
case DXGI_FORMAT_R9G9B9E5_SHAREDEXP:
930-
targetGuid = GUID_WICPixelFormat96bppRGBFloat;
963+
if ( g_WIC2 )
964+
{
965+
targetGuid = GUID_WICPixelFormat96bppRGBFloat;
966+
}
967+
else
968+
{
969+
targetGuid = GUID_WICPixelFormat24bppBGR;
970+
}
931971
break;
932972
#endif
933973

WICTextureLoader/WICTextureLoader.cpp

Lines changed: 70 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,6 @@ static WICTranslate g_WICFormats[] =
126126
{ GUID_WICPixelFormat8bppGray, DXGI_FORMAT_R8_UNORM },
127127

128128
{ GUID_WICPixelFormat8bppAlpha, DXGI_FORMAT_A8_UNORM },
129-
130-
#if (_WIN32_WINNT >= 0x0602 /*_WIN32_WINNT_WIN8*/)
131-
{ GUID_WICPixelFormat96bppRGBFloat, DXGI_FORMAT_R32G32B32_FLOAT },
132-
#endif
133129
};
134130

135131
//-------------------------------------------------------------------------------------
@@ -202,18 +198,17 @@ static WICConvert g_WICConvert[] =
202198
{ GUID_WICPixelFormat40bppCMYKAlpha, GUID_WICPixelFormat64bppRGBA }, // DXGI_FORMAT_R16G16B16A16_UNORM
203199
{ GUID_WICPixelFormat80bppCMYKAlpha, GUID_WICPixelFormat64bppRGBA }, // DXGI_FORMAT_R16G16B16A16_UNORM
204200

205-
#if (_WIN32_WINNT >= 0x0602 /*_WIN32_WINNT_WIN8*/)
201+
#if (_WIN32_WINNT >= 0x0602 /*_WIN32_WINNT_WIN8*/) || defined(_WIN7_PLATFORM_UPDATE)
206202
{ GUID_WICPixelFormat32bppRGB, GUID_WICPixelFormat32bppRGBA }, // DXGI_FORMAT_R8G8B8A8_UNORM
207203
{ GUID_WICPixelFormat64bppRGB, GUID_WICPixelFormat64bppRGBA }, // DXGI_FORMAT_R16G16B16A16_UNORM
208204
{ GUID_WICPixelFormat64bppPRGBAHalf, GUID_WICPixelFormat64bppRGBAHalf }, // DXGI_FORMAT_R16G16B16A16_FLOAT
209-
{ GUID_WICPixelFormat96bppRGBFixedPoint, GUID_WICPixelFormat96bppRGBFloat }, // DXGI_FORMAT_R32G32B32_FLOAT
210-
#else
211-
{ GUID_WICPixelFormat96bppRGBFixedPoint, GUID_WICPixelFormat128bppRGBAFloat }, // DXGI_FORMAT_R32G32B32A32_FLOAT
212205
#endif
213206

214207
// We don't support n-channel formats
215208
};
216209

210+
static bool g_WIC2 = false;
211+
217212
//--------------------------------------------------------------------------------------
218213
static IWICImagingFactory* _GetWIC()
219214
{
@@ -222,6 +217,37 @@ static IWICImagingFactory* _GetWIC()
222217
if ( s_Factory )
223218
return s_Factory;
224219

220+
#if(_WIN32_WINNT >= 0x0602 /*_WIN32_WINNT_WIN8*/) || defined(_WIN7_PLATFORM_UPDATE)
221+
HRESULT hr = CoCreateInstance(
222+
CLSID_WICImagingFactory2,
223+
nullptr,
224+
CLSCTX_INPROC_SERVER,
225+
__uuidof(IWICImagingFactory2),
226+
(LPVOID*)&s_Factory
227+
);
228+
229+
if ( SUCCEEDED(hr) )
230+
{
231+
// WIC2 is available on Windows 8 and Windows 7 SP1 with KB 2670838 installed
232+
g_WIC2 = true;
233+
}
234+
else
235+
{
236+
hr = CoCreateInstance(
237+
CLSID_WICImagingFactory1,
238+
nullptr,
239+
CLSCTX_INPROC_SERVER,
240+
__uuidof(IWICImagingFactory),
241+
(LPVOID*)&s_Factory
242+
);
243+
244+
if ( FAILED(hr) )
245+
{
246+
s_Factory = nullptr;
247+
return nullptr;
248+
}
249+
}
250+
#else
225251
HRESULT hr = CoCreateInstance(
226252
CLSID_WICImagingFactory,
227253
nullptr,
@@ -235,6 +261,7 @@ static IWICImagingFactory* _GetWIC()
235261
s_Factory = nullptr;
236262
return nullptr;
237263
}
264+
#endif
238265

239266
return s_Factory;
240267
}
@@ -248,6 +275,14 @@ static DXGI_FORMAT _WICToDXGI( const GUID& guid )
248275
return g_WICFormats[i].format;
249276
}
250277

278+
#if (_WIN32_WINNT >= 0x0602 /*_WIN32_WINNT_WIN8*/) || defined(_WIN7_PLATFORM_UPDATE)
279+
if ( g_WIC2 )
280+
{
281+
if ( memcmp( &GUID_WICPixelFormat96bppRGBFloat, &guid, sizeof(GUID) ) == 0 )
282+
return DXGI_FORMAT_R32G32B32_FLOAT;
283+
}
284+
#endif
285+
251286
return DXGI_FORMAT_UNKNOWN;
252287
}
253288

@@ -361,16 +396,34 @@ static HRESULT CreateTextureFromWIC( _In_ ID3D11Device* d3dDevice,
361396
DXGI_FORMAT format = _WICToDXGI( pixelFormat );
362397
if ( format == DXGI_FORMAT_UNKNOWN )
363398
{
364-
for( size_t i=0; i < _countof(g_WICConvert); ++i )
399+
if ( memcmp( &GUID_WICPixelFormat96bppRGBFixedPoint, &pixelFormat, sizeof(WICPixelFormatGUID) ) == 0 )
365400
{
366-
if ( memcmp( &g_WICConvert[i].source, &pixelFormat, sizeof(WICPixelFormatGUID) ) == 0 )
401+
#if (_WIN32_WINNT >= 0x0602 /*_WIN32_WINNT_WIN8*/) || defined(_WIN7_PLATFORM_UPDATE)
402+
if ( g_WIC2 )
367403
{
368-
memcpy( &convertGUID, &g_WICConvert[i].target, sizeof(WICPixelFormatGUID) );
369-
370-
format = _WICToDXGI( g_WICConvert[i].target );
371-
assert( format != DXGI_FORMAT_UNKNOWN );
372-
bpp = _WICBitsPerPixel( convertGUID );
373-
break;
404+
memcpy( &convertGUID, &GUID_WICPixelFormat96bppRGBFloat, sizeof(WICPixelFormatGUID) );
405+
format = DXGI_FORMAT_R32G32B32_FLOAT;
406+
}
407+
else
408+
#endif
409+
{
410+
memcpy( &convertGUID, &GUID_WICPixelFormat128bppRGBAFloat, sizeof(WICPixelFormatGUID) );
411+
format = DXGI_FORMAT_R32G32B32A32_FLOAT;
412+
}
413+
}
414+
else
415+
{
416+
for( size_t i=0; i < _countof(g_WICConvert); ++i )
417+
{
418+
if ( memcmp( &g_WICConvert[i].source, &pixelFormat, sizeof(WICPixelFormatGUID) ) == 0 )
419+
{
420+
memcpy( &convertGUID, &g_WICConvert[i].target, sizeof(WICPixelFormatGUID) );
421+
422+
format = _WICToDXGI( g_WICConvert[i].target );
423+
assert( format != DXGI_FORMAT_UNKNOWN );
424+
bpp = _WICBitsPerPixel( convertGUID );
425+
break;
426+
}
374427
}
375428
}
376429

@@ -382,7 +435,7 @@ static HRESULT CreateTextureFromWIC( _In_ ID3D11Device* d3dDevice,
382435
bpp = _WICBitsPerPixel( pixelFormat );
383436
}
384437

385-
#if (_WIN32_WINNT >= 0x0602 /*_WIN32_WINNT_WIN8*/)
438+
#if (_WIN32_WINNT >= 0x0602 /*_WIN32_WINNT_WIN8*/) || defined(_WIN7_PLATFORM_UPDATE)
386439
if ( (format == DXGI_FORMAT_R32G32B32_FLOAT) && d3dContext != 0 && textureView != 0 )
387440
{
388441
// Special case test for optional device support for autogen mipchains for R32G32B32_FLOAT

0 commit comments

Comments
 (0)