DirectXTex: Make ScopedObject match Microsoft::WRL::ComPtr, use Microsoft::WRL::ComPtr when possible

This commit is contained in:
walbourn_cp
2013-07-26 16:44:00 -07:00
parent 70afb86830
commit 1d3e1d0904
6 changed files with 172 additions and 44 deletions

View File

@@ -567,7 +567,7 @@ HRESULT CreateShaderResourceViewEx( ID3D11Device* pDevice, const Image* srcImage
if ( FAILED(hr) )
return hr;
assert( !resource.IsNull() );
assert( resource );
D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc;
memset( &SRVDesc, 0, sizeof(SRVDesc) );
@@ -660,11 +660,11 @@ HRESULT CaptureTexture( ID3D11Device* pDevice, ID3D11DeviceContext* pContext, ID
case D3D11_RESOURCE_DIMENSION_TEXTURE1D:
{
ScopedObject<ID3D11Texture1D> pTexture;
hr = pSource->QueryInterface( __uuidof(ID3D11Texture1D), (void**) &pTexture );
hr = pSource->QueryInterface( __uuidof(ID3D11Texture1D), reinterpret_cast<void**>( pTexture.GetAddressOf() ) );
if ( FAILED(hr) )
break;
assert( pTexture.Get() );
assert( pTexture );
D3D11_TEXTURE1D_DESC desc;
pTexture->GetDesc( &desc );
@@ -679,7 +679,7 @@ HRESULT CaptureTexture( ID3D11Device* pDevice, ID3D11DeviceContext* pContext, ID
if ( FAILED(hr) )
break;
assert( pStaging.Get() );
assert( pStaging );
pContext->CopyResource( pStaging.Get(), pSource );
@@ -704,11 +704,11 @@ HRESULT CaptureTexture( ID3D11Device* pDevice, ID3D11DeviceContext* pContext, ID
case D3D11_RESOURCE_DIMENSION_TEXTURE2D:
{
ScopedObject<ID3D11Texture2D> pTexture;
hr = pSource->QueryInterface( __uuidof(ID3D11Texture2D), (void**) &pTexture );
hr = pSource->QueryInterface( __uuidof(ID3D11Texture2D), reinterpret_cast<void**>( pTexture.GetAddressOf() ) );
if ( FAILED(hr) )
break;
assert( pTexture.Get() );
assert( pTexture );
D3D11_TEXTURE2D_DESC desc;
pTexture->GetDesc( &desc );
@@ -724,7 +724,7 @@ HRESULT CaptureTexture( ID3D11Device* pDevice, ID3D11DeviceContext* pContext, ID
if ( FAILED(hr) )
break;
assert( pTemp.Get() );
assert( pTemp );
DXGI_FORMAT fmt = desc.Format;
if ( IsTypeless(fmt) )
@@ -763,7 +763,7 @@ HRESULT CaptureTexture( ID3D11Device* pDevice, ID3D11DeviceContext* pContext, ID
if ( FAILED(hr) )
break;
assert( pStaging.Get() );
assert( pStaging );
pContext->CopyResource( pStaging.Get(), pTemp.Get() );
}
@@ -778,7 +778,7 @@ HRESULT CaptureTexture( ID3D11Device* pDevice, ID3D11DeviceContext* pContext, ID
if ( FAILED(hr) )
break;
assert( pStaging.Get() );
assert( pStaging );
pContext->CopyResource( pStaging.Get(), pSource );
}
@@ -805,11 +805,11 @@ HRESULT CaptureTexture( ID3D11Device* pDevice, ID3D11DeviceContext* pContext, ID
case D3D11_RESOURCE_DIMENSION_TEXTURE3D:
{
ScopedObject<ID3D11Texture3D> pTexture;
hr = pSource->QueryInterface( __uuidof(ID3D11Texture3D), (void**) &pTexture );
hr = pSource->QueryInterface( __uuidof(ID3D11Texture3D), reinterpret_cast<void**>( pTexture.GetAddressOf() ) );
if ( FAILED(hr) )
break;
assert( pTexture.Get() );
assert( pTexture );
D3D11_TEXTURE3D_DESC desc;
pTexture->GetDesc( &desc );
@@ -824,7 +824,7 @@ HRESULT CaptureTexture( ID3D11Device* pDevice, ID3D11DeviceContext* pContext, ID
if ( FAILED(hr) )
break;
assert( pStaging.Get() );
assert( pStaging );
pContext->CopyResource( pStaging.Get(), pSource );

View File

@@ -178,7 +178,7 @@ HRESULT _ResizeSeparateColorAndAlpha( _In_ IWICImagingFactory* pWIC, _In_ IWICBi
ScopedObject<IWICPixelFormatInfo> pixelFormatInfo;
if ( SUCCEEDED(hr) )
{
hr = componentInfo->QueryInterface( IID_PPV_ARGS( &pixelFormatInfo ) );
hr = componentInfo.As( &pixelFormatInfo );
}
UINT bitsPerPixel = 0;
@@ -474,7 +474,7 @@ static HRESULT _GenerateMipMapsUsingWIC( _In_ const Image& baseImage, _In_ DWORD
return hr;
ScopedObject<IWICPixelFormatInfo2> pixelFormatInfo;
hr = componentInfo->QueryInterface( IID_PPV_ARGS( &pixelFormatInfo ) );
hr = componentInfo.As( &pixelFormatInfo );
if ( FAILED(hr) )
return hr;

View File

@@ -46,7 +46,7 @@ static HRESULT _PerformResizeUsingWIC( _In_ const Image& srcImage, _In_ DWORD fi
return hr;
ScopedObject<IWICPixelFormatInfo2> pixelFormatInfo;
hr = componentInfo->QueryInterface( IID_PPV_ARGS( &pixelFormatInfo ) );
hr = componentInfo.As( &pixelFormatInfo );
if ( FAILED(hr) )
return hr;

View File

@@ -39,10 +39,21 @@ inline HANDLE safe_handle( HANDLE h ) { return (h == INVALID_HANDLE_VALUE) ? 0 :
//---------------------------------------------------------------------------------
#if defined(_MSC_VER) && (_MSC_VER >= 1610)
#include <wrl.h>
template<class T> class ScopedObject : public Microsoft::WRL::ComPtr<T> {};
#else
template<class T> class ScopedObject
{
public:
explicit ScopedObject( T *p = 0 ) : _pointer(p) {}
ScopedObject() : _pointer(nullptr) {}
ScopedObject( T *p ) : _pointer(p) { if (_pointer) { _pointer->AddRef(); } }
ScopedObject( const ScopedObject& other ) : _pointer(other._pointer) { if (_pointer) { _pointer->AddRef(); } }
~ScopedObject()
{
if ( _pointer )
@@ -52,19 +63,51 @@ public:
}
}
bool IsNull() const { return (!_pointer); }
operator bool() const { return (_pointer != nullptr); }
T& operator= (_In_opt_ T* other)
{
if ( _pointer != other )
{
if ( _pointer) { _pointer->Release(); }
_pointer = other;
if ( other ) { other->AddRef() };
}
return *this;
}
ScopedObject& operator= (const ScopedObject& other)
{
if ( _pointer != other._pointer )
{
if ( _pointer) { _pointer->Release(); }
_pointer = other._pointer;
if ( other._pointer ) { other._pointer->AddRef(); }
}
return *this;
}
T& operator*() { return *_pointer; }
T* operator->() { return _pointer; }
T* operator->() const { return _pointer; }
T** operator&() { return &_pointer; }
void Reset(T *p = 0) { if ( _pointer ) { _pointer->Release(); } _pointer = p; }
void Reset() { if ( _pointer ) { _pointer->Release(); _pointer = nullptr; } }
T* Get() const { return _pointer; }
T** GetAddressOf() { return &_pointer; }
T** ReleaseAndGetAddressOf() { if ( _pointer ) { _pointer->Release(); _pointer = nullptr; } return &_pointer; }
template<typename U>
HRESULT As(_Inout_ U* p) { return _pointer->QueryInterface( _uuidof(U), reinterpret_cast<void**>( p ) ); }
template<typename U>
HRESULT As(_Out_ ScopedObject<U>* p ) { return _pointer->QueryInterface( _uuidof(U), reinterpret_cast<void**>( p->ReleaseAndGetAddressOf() ) ); }
private:
ScopedObject(const ScopedObject&);
ScopedObject& operator=(const ScopedObject&);
T* _pointer;
};
#endif