From: <axl...@us...> - 2009-04-26 16:46:21
|
Revision: 219 http://hgengine.svn.sourceforge.net/hgengine/?rev=219&view=rev Author: axlecrusher Date: 2009-04-26 16:46:17 +0000 (Sun, 26 Apr 2009) Log Message: ----------- more improvements to threaded loading HGMDL now thread loading Modified Paths: -------------- Mercury2/src/HGMDLModel.cpp Mercury2/src/HGMDLModel.h Mercury2/src/ImageLoader.cpp Mercury2/src/ImageLoader.h Mercury2/src/MercuryAsset.cpp Mercury2/src/MercuryAsset.h Mercury2/src/RenderableNode.h Mercury2/src/Texture.cpp Mercury2/src/Texture.h Modified: Mercury2/src/HGMDLModel.cpp =================================================================== --- Mercury2/src/HGMDLModel.cpp 2009-04-26 15:37:56 UTC (rev 218) +++ Mercury2/src/HGMDLModel.cpp 2009-04-26 16:46:17 UTC (rev 219) @@ -21,7 +21,7 @@ LoadHGMDL( path ); } -void HGMDLModel::LoadModel(MercuryFile* hgmdl) +void HGMDLModel::LoadModel(MercuryFile* hgmdl, HGMDLModel* model) { char fingerPrint[5]; fingerPrint[4] = 0; @@ -54,14 +54,15 @@ { MAutoPtr< HGMDLMesh > mesh( new HGMDLMesh() ); mesh->LoadFromFile( hgmdl ); - m_meshes.push_back(mesh); + model->m_meshes.push_back(mesh); } } void HGMDLModel::Render(const MercuryNode* node) { - for(uint16_t i = 0; i < m_meshes.size(); ++i) - m_meshes[i]->Render(node); + if ( GetLoadState() != LOADING ) + for(uint16_t i = 0; i < m_meshes.size(); ++i) + m_meshes[i]->Render(node); } void HGMDLModel::LoadHGMDL( const MString& path ) @@ -69,16 +70,14 @@ if ( m_isInstanced ) return; if ( !path.empty() ) { + SetLoadState(LOADING); ADD_ASSET_INSTANCE(HGMDLModel, path, this); m_path = path; - MercuryFile * f = FILEMAN.Open( path ); - if( !f ) - { - printf( "Could not open file: \"%s\" for model\n", path.c_str() ); - return; - } - LoadModel( f ); - delete f; + + LoaderThreadData* ltd = new LoaderThreadData( this ); + MercuryThread loaderThread; + loaderThread.HaltOnDestroy(false); + loaderThread.Create( LoaderThread, ltd ); } } @@ -88,6 +87,29 @@ return new HGMDLModel(); } +void* HGMDLModel::LoaderThread(void* d) +{ + LoaderThreadData *pd = (LoaderThreadData*)d; + LoaderThreadData data = *pd; + delete pd; + + HGMDLModel *model = (HGMDLModel*)data.asset.Ptr(); + + MercuryFile * f = FILEMAN.Open( model->m_path ); + if( !f ) + { + printf( "Could not open file: \"%s\" for model\n", model->m_path.c_str() ); + return 0; + } + + LoadModel(f, model); + + delete f; + + model->LoadedCallback(); + return 0; +} + /**************************************************************************** * Copyright (C) 2008 by Joshua Allen * * * Modified: Mercury2/src/HGMDLModel.h =================================================================== --- Mercury2/src/HGMDLModel.h 2009-04-26 15:37:56 UTC (rev 218) +++ Mercury2/src/HGMDLModel.h 2009-04-26 16:46:17 UTC (rev 219) @@ -15,18 +15,20 @@ virtual void LoadFromXML(const XMLNode& node); - void LoadModel(MercuryFile* hgmdl); + static void LoadModel(MercuryFile* hgmdl, HGMDLModel* model); static HGMDLModel* Generate(); virtual void Render(const MercuryNode* node); private: void LoadHGMDL( const MString& path ); + static void* LoaderThread(void* d); - MString m_path; std::vector< MAutoPtr< HGMDLMesh > > m_meshes; }; + + #endif /**************************************************************************** Modified: Mercury2/src/ImageLoader.cpp =================================================================== --- Mercury2/src/ImageLoader.cpp 2009-04-26 15:37:56 UTC (rev 218) +++ Mercury2/src/ImageLoader.cpp 2009-04-26 16:46:17 UTC (rev 219) @@ -6,11 +6,12 @@ void* ImageLoader::ImageLoaderThread(void* d) { - ThreadData *pd = (ThreadData*)d; - ThreadData data = *pd; + LoaderThreadData *pd = (LoaderThreadData*)d; + LoaderThreadData data = *pd; + delete pd; + Texture *texture = (Texture*)data.asset.Ptr(); - delete pd; - RawImageData* imageData = data.imageloader->LoadImage( data.filename ); + RawImageData* imageData = GetInstance().LoadImage( data.asset->Path() ); texture->SetRawData(imageData); data.asset->LoadedCallback(); return 0; @@ -62,9 +63,9 @@ return NULL; } -void ImageLoader::LoadImageThreaded(MercuryAsset* t, const MString& filename) +void ImageLoader::LoadImageThreaded(MercuryAsset* t) { - ThreadData* data = new ThreadData(this, t, filename); + LoaderThreadData* data = new LoaderThreadData(t); MercuryThread loaderThread; loaderThread.HaltOnDestroy(false); loaderThread.Create( ImageLoader::ImageLoaderThread, data, true ); Modified: Mercury2/src/ImageLoader.h =================================================================== --- Mercury2/src/ImageLoader.h 2009-04-26 15:37:56 UTC (rev 218) +++ Mercury2/src/ImageLoader.h 2009-04-26 16:46:17 UTC (rev 219) @@ -15,29 +15,13 @@ static ImageLoader& GetInstance(); bool RegisterFactoryCallback(const MString& type, Callback1R< MercuryFile *, RawImageData* >); RawImageData* LoadImage(const MString& filename); - void LoadImageThreaded(MercuryAsset* t, const MString& filename); + void LoadImageThreaded(MercuryAsset* t); private: static void* ImageLoaderThread(void* d); - std::list< std::pair< MString, Callback1R< MercuryFile*, RawImageData* > > > m_factoryCallbacks; + std::list< std::pair< MString, Callback1R< MercuryFile*, RawImageData* > > > m_factoryCallbacks; }; -class ThreadData -{ - public: - ThreadData(ImageLoader* il, MercuryAsset* a, const MString& f) - { - asset = a; - filename = f; - imageloader = il; - } - - //use and autoptr here to prevent crashes if asset is removed during load - MAutoPtr< MercuryAsset > asset; - MString filename; - ImageLoader* imageloader; -}; - static InstanceCounter<ImageLoader> ILcounter("ImageLoader"); #define REGISTER_IMAGE_TYPE(fingerprint,functor)\ Modified: Mercury2/src/MercuryAsset.cpp =================================================================== --- Mercury2/src/MercuryAsset.cpp 2009-04-26 15:37:56 UTC (rev 218) +++ Mercury2/src/MercuryAsset.cpp 2009-04-26 16:46:17 UTC (rev 219) @@ -2,7 +2,7 @@ #include <RenderableNode.h> MercuryAsset::MercuryAsset() - :m_isInstanced(false), m_boundingVolume(NULL) + :m_isInstanced(false), m_boundingVolume(NULL), m_loadState(NONE) { } @@ -30,6 +30,10 @@ return m_loadState; } +void MercuryAsset::LoadedCallback() +{ + SetLoadState( LOADED ); +} AssetFactory& AssetFactory::GetInstance() { Modified: Mercury2/src/MercuryAsset.h =================================================================== --- Mercury2/src/MercuryAsset.h 2009-04-26 15:37:56 UTC (rev 218) +++ Mercury2/src/MercuryAsset.h 2009-04-26 16:46:17 UTC (rev 219) @@ -16,6 +16,9 @@ NONE }; +/* Assets are stored in renderable nodes with MAuto pointers. +The renderable nodes handle the memory management +*/ class MercuryAsset : public RefBase, MessageHandler { public: @@ -31,22 +34,36 @@ ///Loads an asset from an XMLAsset representing itself virtual void LoadFromXML(const XMLNode& node) {}; - virtual void LoadedCallback() {}; + virtual void LoadedCallback(); //thread safe inline void IsInstanced(bool b) { m_isInstanced = b; } inline const BoundingVolume* GetBoundingVolume() const { return m_boundingVolume; } + inline const MString& Path() const { return m_path; } protected: - void SetLoadState(LoadState ls); - LoadState GetLoadState(); + void SetLoadState(LoadState ls); //thread safe + LoadState GetLoadState(); //thread safe bool m_isInstanced; BoundingVolume* m_boundingVolume; + MString m_path; private: LoadState m_loadState; MSemaphore m_lock; }; +class LoaderThreadData +{ + public: + LoaderThreadData(MercuryAsset* a) + { + asset = a; + } + + //use and autoptr here to prevent crashes if asset is removed during load + MAutoPtr< MercuryAsset > asset; +}; + class AssetFactory { public: @@ -62,6 +79,7 @@ std::list< std::pair< MString, Callback0R< MAutoPtr<MercuryAsset> > > > m_factoryCallbacks; + //the actual storage point is in renderable nodes static std::map<MString, MercuryAsset*> m_assetInstances; }; Modified: Mercury2/src/RenderableNode.h =================================================================== --- Mercury2/src/RenderableNode.h 2009-04-26 15:37:56 UTC (rev 218) +++ Mercury2/src/RenderableNode.h 2009-04-26 16:46:17 UTC (rev 219) @@ -40,9 +40,10 @@ bool m_hidden; private: bool IsInAssetList(MercuryAsset* asset) const; - - std::list< MAutoPtr< MercuryAsset > > m_assets; ///serves as a holder for memory + //The asset is actually stored here + std::list< MAutoPtr< MercuryAsset > > m_assets; + //we will just use normal pointers here because we don't want to waste too much time //dereferencing the autopointer. As a precaution when assets are added to these lists, //they must exist in m_assets. Modified: Mercury2/src/Texture.cpp =================================================================== --- Mercury2/src/Texture.cpp 2009-04-26 15:37:56 UTC (rev 218) +++ Mercury2/src/Texture.cpp 2009-04-26 16:46:17 UTC (rev 219) @@ -24,7 +24,7 @@ Texture::~Texture() { - REMOVE_ASSET_INSTANCE(TEXTURE, m_filename); + REMOVE_ASSET_INSTANCE(TEXTURE, m_path); if (m_textureID) glDeleteTextures(1, &m_textureID); m_textureID = 0; @@ -136,13 +136,13 @@ if (m_isInstanced) return; if ( !path.empty() ) { - ADD_ASSET_INSTANCE(Texture, path, this); - m_filename = path; SetLoadState(LOADING); + ADD_ASSET_INSTANCE(Texture, path, this); + m_path = path; - MercuryThread loaderThread; +// MercuryThread loaderThread; // ImageLoader::LoadImageThreaded(this, m_filename ); - ImageLoader::GetInstance().LoadImageThreaded(this, m_filename ); + ImageLoader::GetInstance().LoadImageThreaded(this); // LoadFromRaw(); // RawImageData* d = ImageLoader::GetInstance().LoadImage( m_filename ); // if (d) LoadFromRaw( d ); @@ -150,12 +150,6 @@ } } -void Texture::LoadedCallback() -{ - printf("loaded!!!!!\n"); - SetLoadState(LOADED); -} - void Texture::SetRawData(RawImageData* raw) { SAFE_DELETE(m_raw); @@ -170,7 +164,7 @@ MAutoPtr< Texture > Texture::LoadFromFile(const MString& path) { MAutoPtr< MercuryAsset > t( AssetFactory::GetInstance().Generate("Texture", path) ); - Texture *a = (Texture*)&(*t); + Texture *a = (Texture*)t.Ptr(); a->LoadImage( path ); return a; } Modified: Mercury2/src/Texture.h =================================================================== --- Mercury2/src/Texture.h 2009-04-26 15:37:56 UTC (rev 218) +++ Mercury2/src/Texture.h 2009-04-26 16:46:17 UTC (rev 219) @@ -18,7 +18,6 @@ virtual void LoadFromXML(const XMLNode& node); void LoadFromRaw(); - virtual void LoadedCallback(); inline static unsigned short NumberActiveTextures() { return m_activeTextures; } inline static uint32_t ReadAndResetBindCount() { uint32_t t = m_textureBinds; m_textureBinds = 0; return t; } @@ -41,7 +40,7 @@ static unsigned short m_activeTextures; static uint32_t m_textureBinds; - MString m_filename; +// MString m_filename; }; #endif This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |