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

View File

@ -190,10 +190,21 @@ static const DDS_PIXELFORMAT DDSPF_DX10 =
{ sizeof(DDS_PIXELFORMAT), DDS_FOURCC, MAKEFOURCC('D','X','1','0'), 0, 0, 0, 0, 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 )
@ -203,23 +214,55 @@ 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
//---------------------------------------------------------------------------------
struct handle_closer { void operator()(HANDLE h) { if (h) CloseHandle(h); } };
@ -541,11 +584,11 @@ static HRESULT CaptureTexture( _In_ ID3D11DeviceContext* pContext,
return HRESULT_FROM_WIN32( ERROR_NOT_SUPPORTED );
ScopedObject<ID3D11Texture2D> pTexture;
HRESULT hr = pSource->QueryInterface( __uuidof(ID3D11Texture2D), (void**) &pTexture );
HRESULT hr = pSource->QueryInterface( __uuidof(ID3D11Texture2D), reinterpret_cast<void**>( pTexture.GetAddressOf() ) );
if ( FAILED(hr) )
return hr;
assert( pTexture.Get() );
assert( pTexture );
pTexture->GetDesc( &desc );
@ -563,7 +606,7 @@ static HRESULT CaptureTexture( _In_ ID3D11DeviceContext* pContext,
if ( FAILED(hr) )
return hr;
assert( pTemp.Get() );
assert( pTemp );
DXGI_FORMAT fmt = EnsureNotTypeless( desc.Format );
@ -593,15 +636,14 @@ static HRESULT CaptureTexture( _In_ ID3D11DeviceContext* pContext,
if ( FAILED(hr) )
return hr;
assert( pStaging.Get() );
assert( pStaging );
pContext->CopyResource( pStaging.Get(), pTemp.Get() );
}
else if ( (desc.Usage == D3D11_USAGE_STAGING) && (desc.CPUAccessFlags & D3D11_CPU_ACCESS_READ) )
{
// Handle case where the source is already a staging texture we can use directly
pTexture->AddRef();
pStaging.Reset( pTexture.Get() );
pStaging = pTexture;
}
else
{
@ -615,7 +657,7 @@ static HRESULT CaptureTexture( _In_ ID3D11DeviceContext* pContext,
if ( FAILED(hr) )
return hr;
assert( pStaging.Get() );
assert( pStaging );
pContext->CopyResource( pStaging.Get(), pSource );
}

View File

@ -53,10 +53,21 @@
#endif
//---------------------------------------------------------------------------------
#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 )
@ -66,23 +77,55 @@ 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
//--------------------------------------------------------------------------------------
template<UINT TNameLength>
@ -312,7 +355,7 @@ static size_t _WICBitsPerPixel( REFGUID targetGuid )
return 0;
ScopedObject<IWICPixelFormatInfo> pfinfo;
if ( FAILED( cinfo->QueryInterface( __uuidof(IWICPixelFormatInfo), reinterpret_cast<void**>( &pfinfo ) ) ) )
if ( FAILED( cinfo.As( &pfinfo ) ) )
return 0;
UINT bpp;