diff --git a/saco/d3d9/common/DXUTmisc.cpp b/saco/d3d9/common/DXUTmisc.cpp index 67c34b7..91d28ff 100644 --- a/saco/d3d9/common/DXUTmisc.cpp +++ b/saco/d3d9/common/DXUTmisc.cpp @@ -12,6 +12,12 @@ //-------------------------------------------------------------------------------------- // Global/Static Members //-------------------------------------------------------------------------------------- +CDXUTResourceCache& DXUTGetGlobalResourceCache() +{ + // Using an accessor function gives control of the construction order + static CDXUTResourceCache cache; + return cache; +} CDXUTTimer* DXUTGetGlobalTimer() { // Using an accessor function gives control of the construction order @@ -321,6 +327,77 @@ bool DXUTFindMediaSearchParentDirs( TCHAR* strSearchPath, int cchSearch, TCHAR* } +//-------------------------------------------------------------------------------------- +// CDXUTResourceCache +//-------------------------------------------------------------------------------------- + +CDXUTResourceCache::~CDXUTResourceCache() +{ + OnDestroyDevice(); + + m_TextureCache.RemoveAll(); + m_EffectCache.RemoveAll(); + m_FontCache.RemoveAll(); +} + +//-------------------------------------------------------------------------------------- +HRESULT CDXUTResourceCache::OnResetDevice( IDirect3DDevice9 *pd3dDevice ) +{ + // Call OnResetDevice on all effect and font objects + for( int i = 0; i < m_EffectCache.GetSize(); ++i ) + m_EffectCache[i].pEffect->OnResetDevice(); + for( int i = 0; i < m_FontCache.GetSize(); ++i ) + m_FontCache[i].pFont->OnResetDevice(); + + + return S_OK; +} + + +//-------------------------------------------------------------------------------------- +HRESULT CDXUTResourceCache::OnLostDevice() +{ + // Call OnLostDevice on all effect and font objects + for( int i = 0; i < m_EffectCache.GetSize(); ++i ) + m_EffectCache[i].pEffect->OnLostDevice(); + for( int i = 0; i < m_FontCache.GetSize(); ++i ) + m_FontCache[i].pFont->OnLostDevice(); + + // Release all the default pool textures + for( int i = m_TextureCache.GetSize() - 1; i >= 0; --i ) + if( m_TextureCache[i].Pool == D3DPOOL_DEFAULT ) + { + SAFE_RELEASE( m_TextureCache[i].pTexture ); + m_TextureCache.Remove( i ); // Remove the entry + } + + return S_OK; +} + + +//-------------------------------------------------------------------------------------- +HRESULT CDXUTResourceCache::OnDestroyDevice() +{ + // Release all resources + for( int i = m_EffectCache.GetSize() - 1; i >= 0; --i ) + { + SAFE_RELEASE( m_EffectCache[i].pEffect ); + m_EffectCache.Remove( i ); + } + for( int i = m_FontCache.GetSize() - 1; i >= 0; --i ) + { + SAFE_RELEASE( m_FontCache[i].pFont ); + m_FontCache.Remove( i ); + } + for( int i = m_TextureCache.GetSize() - 1; i >= 0; --i ) + { + SAFE_RELEASE( m_TextureCache[i].pTexture ); + m_TextureCache.Remove( i ); + } + + return S_OK; +} + //-------------------------------------------------------------------------------------- // Direct3D9 dynamic linking support -- calls top-level D3D9 APIs with graceful diff --git a/saco/d3d9/common/DXUTmisc.h b/saco/d3d9/common/DXUTmisc.h index 55699c1..4105613 100644 --- a/saco/d3d9/common/DXUTmisc.h +++ b/saco/d3d9/common/DXUTmisc.h @@ -83,6 +83,70 @@ protected: LONGLONG m_llBaseTime; }; + +//----------------------------------------------------------------------------- +// Resource cache for textures, fonts, meshs, and effects. +// Use DXUTGetGlobalResourceCache() to access the global cache +//----------------------------------------------------------------------------- + +enum DXUTCACHE_SOURCELOCATION { DXUTCACHE_LOCATION_FILE, DXUTCACHE_LOCATION_RESOURCE }; + +struct DXUTCache_Texture +{ + DXUTCACHE_SOURCELOCATION Location; + TCHAR wszSource[MAX_PATH]; + HMODULE hSrcModule; + UINT Width; + UINT Height; + UINT Depth; + UINT MipLevels; + DWORD Usage; + D3DFORMAT Format; + D3DPOOL Pool; + D3DRESOURCETYPE Type; + IDirect3DBaseTexture9 *pTexture; +}; + +struct DXUTCache_Font : public D3DXFONT_DESC +{ + ID3DXFont *pFont; +}; + +struct DXUTCache_Effect +{ + DXUTCACHE_SOURCELOCATION Location; + TCHAR wszSource[MAX_PATH]; + HMODULE hSrcModule; + DWORD dwFlags; + ID3DXEffect *pEffect; +}; + + +class CDXUTResourceCache +{ +public: + ~CDXUTResourceCache(); + + +public: + HRESULT OnResetDevice( IDirect3DDevice9 *pd3dDevice ); + HRESULT OnLostDevice(); + HRESULT OnDestroyDevice(); + +protected: + friend CDXUTResourceCache& DXUTGetGlobalResourceCache(); + friend HRESULT DXUTInitialize3DEnvironment(); + friend HRESULT DXUTReset3DEnvironment(); + friend void DXUTCleanup3DEnvironment( bool bReleaseSettings ); + + CDXUTResourceCache() { } + + CGrowableArray< DXUTCache_Texture > m_TextureCache; + CGrowableArray< DXUTCache_Effect > m_EffectCache; + CGrowableArray< DXUTCache_Font > m_FontCache; +}; + +CDXUTResourceCache& DXUTGetGlobalResourceCache(); //-------------------------------------------------------------------------------------- // Implementation of CGrowableArray //--------------------------------------------------------------------------------------