DDSTextureLoader/WICTextureLoader: Fixed forceSRGB logic (DirectxTK Codeplex issue 851)

Integrated some code review feedback
This commit is contained in:
walbourn_cp 2013-01-28 14:24:03 -08:00
parent 171f49b741
commit 6b8936dc50
3 changed files with 58 additions and 37 deletions

View File

@ -207,7 +207,7 @@ static HRESULT LoadTextureDataFromFile( _In_z_ const wchar_t* fileName,
}
// create enough space for the file data
ddsData.reset( new uint8_t[ FileSize.LowPart ] );
ddsData.reset( new (std::nothrow) uint8_t[ FileSize.LowPart ] );
if (!ddsData )
{
return E_OUTOFMEMORY;
@ -763,7 +763,9 @@ static HRESULT FillInitData( _In_ size_t width,
_Out_writes_(mipCount*arraySize) D3D11_SUBRESOURCE_DATA* initData )
{
if ( !bitData || !initData )
{
return E_POINTER;
}
skipMip = 0;
twidth = 0;
@ -801,6 +803,8 @@ static HRESULT FillInitData( _In_ size_t width,
tdepth = d;
}
assert(index < mipCount * arraySize);
_Analysis_assume_(index < mipCount * arraySize);
initData[index].pSysMem = ( const void* )pSrcBits;
initData[index].SysMemPitch = static_cast<UINT>( RowBytes );
initData[index].SysMemSlicePitch = static_cast<UINT>( NumBytes );
@ -862,6 +866,11 @@ static HRESULT CreateD3DResources( _In_ ID3D11Device* d3dDevice,
HRESULT hr = E_FAIL;
if ( forceSRGB )
{
format = MakeSRGB( format );
}
switch ( resDim )
{
case D3D11_RESOURCE_DIMENSION_TEXTURE1D:
@ -887,12 +896,7 @@ static HRESULT CreateD3DResources( _In_ ID3D11Device* d3dDevice,
{
D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc;
memset( &SRVDesc, 0, sizeof( SRVDesc ) );
if ( forceSRGB )
{
SRVDesc.Format = MakeSRGB( format );
}
else
SRVDesc.Format = format;
SRVDesc.Format = format;
if (arraySize > 1)
{
@ -959,12 +963,7 @@ static HRESULT CreateD3DResources( _In_ ID3D11Device* d3dDevice,
{
D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc;
memset( &SRVDesc, 0, sizeof( SRVDesc ) );
if ( forceSRGB )
{
SRVDesc.Format = MakeSRGB( format );
}
else
SRVDesc.Format = format;
SRVDesc.Format = format;
if (isCubeMap)
{
@ -1042,12 +1041,7 @@ static HRESULT CreateD3DResources( _In_ ID3D11Device* d3dDevice,
{
D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc;
memset( &SRVDesc, 0, sizeof( SRVDesc ) );
if ( forceSRGB )
{
SRVDesc.Format = MakeSRGB( format );
}
else
SRVDesc.Format = format;
SRVDesc.Format = format;
SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE3D;
SRVDesc.Texture3D.MipLevels = desc.MipLevels;
@ -1251,7 +1245,7 @@ static HRESULT CreateTextureFromDDS( _In_ ID3D11Device* d3dDevice,
}
// Create the texture
std::unique_ptr<D3D11_SUBRESOURCE_DATA[]> initData( new D3D11_SUBRESOURCE_DATA[ mipCount * arraySize ] );
std::unique_ptr<D3D11_SUBRESOURCE_DATA[]> initData( new (std::nothrow) D3D11_SUBRESOURCE_DATA[ mipCount * arraySize ] );
if ( !initData )
{
return E_OUTOFMEMORY;
@ -1343,6 +1337,15 @@ HRESULT DirectX::CreateDDSTextureFromMemoryEx( ID3D11Device* d3dDevice,
ID3D11Resource** texture,
ID3D11ShaderResourceView** textureView )
{
if ( texture )
{
*texture = nullptr;
}
if ( textureView )
{
*textureView = nullptr;
}
if (!d3dDevice || !ddsData || (!texture && !textureView))
{
return E_INVALIDARG;
@ -1430,6 +1433,15 @@ HRESULT DirectX::CreateDDSTextureFromFileEx( ID3D11Device* d3dDevice,
ID3D11Resource** texture,
ID3D11ShaderResourceView** textureView )
{
if ( texture )
{
*texture = nullptr;
}
if ( textureView )
{
*textureView = nullptr;
}
if (!d3dDevice || !fileName || (!texture && !textureView))
{
return E_INVALIDARG;

View File

@ -788,7 +788,9 @@ HRESULT DirectX::SaveDDSTextureToFile( _In_ ID3D11DeviceContext* pContext,
}
// Setup pixels
std::unique_ptr<uint8_t> pixels( new uint8_t[ slicePitch ] );
std::unique_ptr<uint8_t> pixels( new (std::nothrow) uint8_t[ slicePitch ] );
if (!pixels)
return E_OUTOFMEMORY;
D3D11_MAPPED_SUBRESOURCE mapped;
hr = pContext->Map( pStaging.Get(), 0, D3D11_MAP_READ, 0, &mapped );
@ -841,9 +843,7 @@ HRESULT DirectX::SaveWICTextureToFile( _In_ ID3D11DeviceContext* pContext,
_In_opt_ const GUID* targetFormat )
{
if ( !fileName )
{
return E_INVALIDARG;
}
D3D11_TEXTURE2D_DESC desc = { 0 };
ScopedObject<ID3D11Texture2D> pStaging;

View File

@ -508,7 +508,9 @@ static HRESULT CreateTextureFromWIC( _In_ ID3D11Device* d3dDevice,
size_t rowPitch = ( twidth * bpp + 7 ) / 8;
size_t imageSize = rowPitch * theight;
std::unique_ptr<uint8_t[]> temp( new uint8_t[ imageSize ] );
std::unique_ptr<uint8_t[]> temp( new (std::nothrow) uint8_t[ imageSize ] );
if (!temp)
return E_OUTOFMEMORY;
// Load image data
if ( memcmp( &convertGUID, &pixelFormat, sizeof(GUID) ) == 0
@ -603,7 +605,7 @@ static HRESULT CreateTextureFromWIC( _In_ ID3D11Device* d3dDevice,
desc.Height = theight;
desc.MipLevels = (autogen) ? 0 : 1;
desc.ArraySize = 1;
desc.Format = format;
desc.Format = (forceSRGB) ? MakeSRGB( format ) : format;
desc.SampleDesc.Count = 1;
desc.SampleDesc.Quality = 0;
desc.Usage = usage;
@ -633,12 +635,7 @@ static HRESULT CreateTextureFromWIC( _In_ ID3D11Device* d3dDevice,
{
D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc;
memset( &SRVDesc, 0, sizeof( SRVDesc ) );
if ( forceSRGB )
{
SRVDesc.Format = MakeSRGB( format );
}
else
SRVDesc.Format = format;
SRVDesc.Format = desc.Format;
SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
SRVDesc.Texture2D.MipLevels = (autogen) ? -1 : 1;
@ -701,15 +698,20 @@ HRESULT DirectX::CreateWICTextureFromMemoryEx( ID3D11Device* d3dDevice,
ID3D11Resource** texture,
ID3D11ShaderResourceView** textureView )
{
if (!d3dDevice || !wicData || (!texture && !textureView))
if ( texture )
{
return E_INVALIDARG;
*texture = nullptr;
}
if ( textureView )
{
*textureView = nullptr;
}
if (!d3dDevice || !wicData || (!texture && !textureView))
return E_INVALIDARG;
if ( !wicDataSize )
{
return E_FAIL;
}
#ifdef _M_AMD64
if ( wicDataSize > 0xFFFFFFFF )
@ -787,10 +789,17 @@ HRESULT DirectX::CreateWICTextureFromFileEx( ID3D11Device* d3dDevice,
ID3D11Resource** texture,
ID3D11ShaderResourceView** textureView )
{
if (!d3dDevice || !fileName || (!texture && !textureView))
if ( texture )
{
return E_INVALIDARG;
*texture = nullptr;
}
if ( textureView )
{
*textureView = nullptr;
}
if (!d3dDevice || !fileName || (!texture && !textureView))
return E_INVALIDARG;
IWICImagingFactory* pWIC = _GetWIC();
if ( !pWIC )