From 6b8936dc50d6bca45196c2da1c18634d2932b800 Mon Sep 17 00:00:00 2001 From: walbourn_cp Date: Mon, 28 Jan 2013 14:24:03 -0800 Subject: [PATCH] DDSTextureLoader/WICTextureLoader: Fixed forceSRGB logic (DirectxTK Codeplex issue 851) Integrated some code review feedback --- DDSTextureLoader/DDSTextureLoader.cpp | 52 ++++++++++++++++----------- ScreenGrab/ScreenGrab.cpp | 6 ++-- WICTextureLoader/WICTextureLoader.cpp | 37 +++++++++++-------- 3 files changed, 58 insertions(+), 37 deletions(-) diff --git a/DDSTextureLoader/DDSTextureLoader.cpp b/DDSTextureLoader/DDSTextureLoader.cpp index f436c5d..7d96d88 100644 --- a/DDSTextureLoader/DDSTextureLoader.cpp +++ b/DDSTextureLoader/DDSTextureLoader.cpp @@ -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( RowBytes ); initData[index].SysMemSlicePitch = static_cast( 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 initData( new D3D11_SUBRESOURCE_DATA[ mipCount * arraySize ] ); + std::unique_ptr 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; diff --git a/ScreenGrab/ScreenGrab.cpp b/ScreenGrab/ScreenGrab.cpp index 4d6a366..4fcd7f9 100644 --- a/ScreenGrab/ScreenGrab.cpp +++ b/ScreenGrab/ScreenGrab.cpp @@ -788,7 +788,9 @@ HRESULT DirectX::SaveDDSTextureToFile( _In_ ID3D11DeviceContext* pContext, } // Setup pixels - std::unique_ptr pixels( new uint8_t[ slicePitch ] ); + std::unique_ptr 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 pStaging; diff --git a/WICTextureLoader/WICTextureLoader.cpp b/WICTextureLoader/WICTextureLoader.cpp index 2f4a6a8..9e6c22b 100644 --- a/WICTextureLoader/WICTextureLoader.cpp +++ b/WICTextureLoader/WICTextureLoader.cpp @@ -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 temp( new uint8_t[ imageSize ] ); + std::unique_ptr 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 )