Skip to content

Commit 6b8936d

Browse files
walbourn_cpwalbourn_cp
walbourn_cp
authored and
walbourn_cp
committed
DDSTextureLoader/WICTextureLoader: Fixed forceSRGB logic (DirectxTK Codeplex issue 851)
Integrated some code review feedback
1 parent 171f49b commit 6b8936d

File tree

3 files changed

+58
-37
lines changed

3 files changed

+58
-37
lines changed

DDSTextureLoader/DDSTextureLoader.cpp

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ static HRESULT LoadTextureDataFromFile( _In_z_ const wchar_t* fileName,
207207
}
208208

209209
// create enough space for the file data
210-
ddsData.reset( new uint8_t[ FileSize.LowPart ] );
210+
ddsData.reset( new (std::nothrow) uint8_t[ FileSize.LowPart ] );
211211
if (!ddsData )
212212
{
213213
return E_OUTOFMEMORY;
@@ -763,7 +763,9 @@ static HRESULT FillInitData( _In_ size_t width,
763763
_Out_writes_(mipCount*arraySize) D3D11_SUBRESOURCE_DATA* initData )
764764
{
765765
if ( !bitData || !initData )
766+
{
766767
return E_POINTER;
768+
}
767769

768770
skipMip = 0;
769771
twidth = 0;
@@ -801,6 +803,8 @@ static HRESULT FillInitData( _In_ size_t width,
801803
tdepth = d;
802804
}
803805

806+
assert(index < mipCount * arraySize);
807+
_Analysis_assume_(index < mipCount * arraySize);
804808
initData[index].pSysMem = ( const void* )pSrcBits;
805809
initData[index].SysMemPitch = static_cast<UINT>( RowBytes );
806810
initData[index].SysMemSlicePitch = static_cast<UINT>( NumBytes );
@@ -862,6 +866,11 @@ static HRESULT CreateD3DResources( _In_ ID3D11Device* d3dDevice,
862866

863867
HRESULT hr = E_FAIL;
864868

869+
if ( forceSRGB )
870+
{
871+
format = MakeSRGB( format );
872+
}
873+
865874
switch ( resDim )
866875
{
867876
case D3D11_RESOURCE_DIMENSION_TEXTURE1D:
@@ -887,12 +896,7 @@ static HRESULT CreateD3DResources( _In_ ID3D11Device* d3dDevice,
887896
{
888897
D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc;
889898
memset( &SRVDesc, 0, sizeof( SRVDesc ) );
890-
if ( forceSRGB )
891-
{
892-
SRVDesc.Format = MakeSRGB( format );
893-
}
894-
else
895-
SRVDesc.Format = format;
899+
SRVDesc.Format = format;
896900

897901
if (arraySize > 1)
898902
{
@@ -959,12 +963,7 @@ static HRESULT CreateD3DResources( _In_ ID3D11Device* d3dDevice,
959963
{
960964
D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc;
961965
memset( &SRVDesc, 0, sizeof( SRVDesc ) );
962-
if ( forceSRGB )
963-
{
964-
SRVDesc.Format = MakeSRGB( format );
965-
}
966-
else
967-
SRVDesc.Format = format;
966+
SRVDesc.Format = format;
968967

969968
if (isCubeMap)
970969
{
@@ -1042,12 +1041,7 @@ static HRESULT CreateD3DResources( _In_ ID3D11Device* d3dDevice,
10421041
{
10431042
D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc;
10441043
memset( &SRVDesc, 0, sizeof( SRVDesc ) );
1045-
if ( forceSRGB )
1046-
{
1047-
SRVDesc.Format = MakeSRGB( format );
1048-
}
1049-
else
1050-
SRVDesc.Format = format;
1044+
SRVDesc.Format = format;
10511045

10521046
SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE3D;
10531047
SRVDesc.Texture3D.MipLevels = desc.MipLevels;
@@ -1251,7 +1245,7 @@ static HRESULT CreateTextureFromDDS( _In_ ID3D11Device* d3dDevice,
12511245
}
12521246

12531247
// Create the texture
1254-
std::unique_ptr<D3D11_SUBRESOURCE_DATA[]> initData( new D3D11_SUBRESOURCE_DATA[ mipCount * arraySize ] );
1248+
std::unique_ptr<D3D11_SUBRESOURCE_DATA[]> initData( new (std::nothrow) D3D11_SUBRESOURCE_DATA[ mipCount * arraySize ] );
12551249
if ( !initData )
12561250
{
12571251
return E_OUTOFMEMORY;
@@ -1343,6 +1337,15 @@ HRESULT DirectX::CreateDDSTextureFromMemoryEx( ID3D11Device* d3dDevice,
13431337
ID3D11Resource** texture,
13441338
ID3D11ShaderResourceView** textureView )
13451339
{
1340+
if ( texture )
1341+
{
1342+
*texture = nullptr;
1343+
}
1344+
if ( textureView )
1345+
{
1346+
*textureView = nullptr;
1347+
}
1348+
13461349
if (!d3dDevice || !ddsData || (!texture && !textureView))
13471350
{
13481351
return E_INVALIDARG;
@@ -1430,6 +1433,15 @@ HRESULT DirectX::CreateDDSTextureFromFileEx( ID3D11Device* d3dDevice,
14301433
ID3D11Resource** texture,
14311434
ID3D11ShaderResourceView** textureView )
14321435
{
1436+
if ( texture )
1437+
{
1438+
*texture = nullptr;
1439+
}
1440+
if ( textureView )
1441+
{
1442+
*textureView = nullptr;
1443+
}
1444+
14331445
if (!d3dDevice || !fileName || (!texture && !textureView))
14341446
{
14351447
return E_INVALIDARG;

ScreenGrab/ScreenGrab.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,9 @@ HRESULT DirectX::SaveDDSTextureToFile( _In_ ID3D11DeviceContext* pContext,
788788
}
789789

790790
// Setup pixels
791-
std::unique_ptr<uint8_t> pixels( new uint8_t[ slicePitch ] );
791+
std::unique_ptr<uint8_t> pixels( new (std::nothrow) uint8_t[ slicePitch ] );
792+
if (!pixels)
793+
return E_OUTOFMEMORY;
792794

793795
D3D11_MAPPED_SUBRESOURCE mapped;
794796
hr = pContext->Map( pStaging.Get(), 0, D3D11_MAP_READ, 0, &mapped );
@@ -841,9 +843,7 @@ HRESULT DirectX::SaveWICTextureToFile( _In_ ID3D11DeviceContext* pContext,
841843
_In_opt_ const GUID* targetFormat )
842844
{
843845
if ( !fileName )
844-
{
845846
return E_INVALIDARG;
846-
}
847847

848848
D3D11_TEXTURE2D_DESC desc = { 0 };
849849
ScopedObject<ID3D11Texture2D> pStaging;

WICTextureLoader/WICTextureLoader.cpp

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,9 @@ static HRESULT CreateTextureFromWIC( _In_ ID3D11Device* d3dDevice,
508508
size_t rowPitch = ( twidth * bpp + 7 ) / 8;
509509
size_t imageSize = rowPitch * theight;
510510

511-
std::unique_ptr<uint8_t[]> temp( new uint8_t[ imageSize ] );
511+
std::unique_ptr<uint8_t[]> temp( new (std::nothrow) uint8_t[ imageSize ] );
512+
if (!temp)
513+
return E_OUTOFMEMORY;
512514

513515
// Load image data
514516
if ( memcmp( &convertGUID, &pixelFormat, sizeof(GUID) ) == 0
@@ -603,7 +605,7 @@ static HRESULT CreateTextureFromWIC( _In_ ID3D11Device* d3dDevice,
603605
desc.Height = theight;
604606
desc.MipLevels = (autogen) ? 0 : 1;
605607
desc.ArraySize = 1;
606-
desc.Format = format;
608+
desc.Format = (forceSRGB) ? MakeSRGB( format ) : format;
607609
desc.SampleDesc.Count = 1;
608610
desc.SampleDesc.Quality = 0;
609611
desc.Usage = usage;
@@ -633,12 +635,7 @@ static HRESULT CreateTextureFromWIC( _In_ ID3D11Device* d3dDevice,
633635
{
634636
D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc;
635637
memset( &SRVDesc, 0, sizeof( SRVDesc ) );
636-
if ( forceSRGB )
637-
{
638-
SRVDesc.Format = MakeSRGB( format );
639-
}
640-
else
641-
SRVDesc.Format = format;
638+
SRVDesc.Format = desc.Format;
642639

643640
SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
644641
SRVDesc.Texture2D.MipLevels = (autogen) ? -1 : 1;
@@ -701,15 +698,20 @@ HRESULT DirectX::CreateWICTextureFromMemoryEx( ID3D11Device* d3dDevice,
701698
ID3D11Resource** texture,
702699
ID3D11ShaderResourceView** textureView )
703700
{
704-
if (!d3dDevice || !wicData || (!texture && !textureView))
701+
if ( texture )
705702
{
706-
return E_INVALIDARG;
703+
*texture = nullptr;
707704
}
705+
if ( textureView )
706+
{
707+
*textureView = nullptr;
708+
}
709+
710+
if (!d3dDevice || !wicData || (!texture && !textureView))
711+
return E_INVALIDARG;
708712

709713
if ( !wicDataSize )
710-
{
711714
return E_FAIL;
712-
}
713715

714716
#ifdef _M_AMD64
715717
if ( wicDataSize > 0xFFFFFFFF )
@@ -787,10 +789,17 @@ HRESULT DirectX::CreateWICTextureFromFileEx( ID3D11Device* d3dDevice,
787789
ID3D11Resource** texture,
788790
ID3D11ShaderResourceView** textureView )
789791
{
790-
if (!d3dDevice || !fileName || (!texture && !textureView))
792+
if ( texture )
791793
{
792-
return E_INVALIDARG;
794+
*texture = nullptr;
793795
}
796+
if ( textureView )
797+
{
798+
*textureView = nullptr;
799+
}
800+
801+
if (!d3dDevice || !fileName || (!texture && !textureView))
802+
return E_INVALIDARG;
794803

795804
IWICImagingFactory* pWIC = _GetWIC();
796805
if ( !pWIC )

0 commit comments

Comments
 (0)