|
From: <axl...@us...> - 2009-04-25 21:01:58
|
Revision: 217
http://hgengine.svn.sourceforge.net/hgengine/?rev=217&view=rev
Author: axlecrusher
Date: 2009-04-25 21:01:43 +0000 (Sat, 25 Apr 2009)
Log Message:
-----------
Many updates focused on threaded loading.
Modified Paths:
--------------
Mercury2/src/ImageLoader.cpp
Mercury2/src/ImageLoader.h
Mercury2/src/MSemaphore.cpp
Mercury2/src/MSemaphore.h
Mercury2/src/MercuryAsset.cpp
Mercury2/src/MercuryAsset.h
Mercury2/src/MercuryNode.cpp
Mercury2/src/MercuryNode.h
Mercury2/src/MercuryString.h
Mercury2/src/Texture.cpp
Mercury2/src/Texture.h
Modified: Mercury2/src/ImageLoader.cpp
===================================================================
--- Mercury2/src/ImageLoader.cpp 2009-04-25 14:20:15 UTC (rev 216)
+++ Mercury2/src/ImageLoader.cpp 2009-04-25 21:01:43 UTC (rev 217)
@@ -1,8 +1,20 @@
#include <ImageLoader.h>
#include <MercuryUtil.h>
+#include <Texture.h>
using namespace std;
+void* ImageLoader::ImageLoaderThread(void* d)
+{
+ ThreadData *pd = (ThreadData*)d;
+ ThreadData data = *pd;
+ delete pd;
+ RawImageData* imageData = data.imageloader->LoadImage( data.filename );
+ ((Texture*)data.asset)->SetRawData(imageData);
+ data.asset->LoadedCallback();
+ return 0;
+}
+
ImageLoader& ImageLoader::GetInstance()
{
static ImageLoader* instance = NULL;
@@ -24,7 +36,7 @@
MercuryFile* f = FILEMAN.Open( filename );
char fingerprint[4];
fingerprint[3] = 0;
-
+
if( !f )
{
printf( "Error opening image: %s\n", filename.c_str() );
@@ -49,6 +61,14 @@
return NULL;
}
+void ImageLoader::LoadImageThreaded(MercuryAsset* t, const MString& filename)
+{
+ ThreadData* data = new ThreadData(this, t, filename);
+ MercuryThread loaderThread;
+ loaderThread.HaltOnDestroy(false);
+ loaderThread.Create( ImageLoader::ImageLoaderThread, data, true );
+}
+
/****************************************************************************
* Copyright (C) 2008 by Joshua Allen *
* *
Modified: Mercury2/src/ImageLoader.h
===================================================================
--- Mercury2/src/ImageLoader.h 2009-04-25 14:20:15 UTC (rev 216)
+++ Mercury2/src/ImageLoader.h 2009-04-25 21:01:43 UTC (rev 217)
@@ -7,17 +7,35 @@
#include <list>
#include <MercuryUtil.h>
+#include <MercuryAsset.h>
+
class ImageLoader
{
public:
static ImageLoader& GetInstance();
bool RegisterFactoryCallback(const MString& type, Callback1R< MercuryFile *, RawImageData* >);
RawImageData* LoadImage(const MString& filename);
-
+ void LoadImageThreaded(MercuryAsset* t, const MString& filename);
+
private:
+ static void* ImageLoaderThread(void* d);
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;
+ }
+ MercuryAsset* asset;
+ MString filename;
+ ImageLoader* imageloader;
+};
+
static InstanceCounter<ImageLoader> ILcounter("ImageLoader");
#define REGISTER_IMAGE_TYPE(fingerprint,functor)\
Modified: Mercury2/src/MSemaphore.cpp
===================================================================
--- Mercury2/src/MSemaphore.cpp 2009-04-25 14:20:15 UTC (rev 216)
+++ Mercury2/src/MSemaphore.cpp 2009-04-25 21:01:43 UTC (rev 217)
@@ -25,6 +25,22 @@
return __sync_add_and_fetch(&m_counter, 1);
}
+void MSemaphore::WaitAndSet(unsigned long value, unsigned long newVal)
+{
+ while ( !__sync_bool_compare_and_swap(&m_counter, value, newVal) );
+}
+
+MSemaphoreLock::MSemaphoreLock(MSemaphore* s)
+ :m_s(s)
+{
+ m_s->WaitAndSet(0,1);
+}
+
+MSemaphoreLock::~MSemaphoreLock()
+{
+ m_s->WaitAndSet(1,0);
+}
+
MSemaphoreIncOnDestroy::MSemaphoreIncOnDestroy(MSemaphore* s)
:m_s(s)
{
Modified: Mercury2/src/MSemaphore.h
===================================================================
--- Mercury2/src/MSemaphore.h 2009-04-25 14:20:15 UTC (rev 216)
+++ Mercury2/src/MSemaphore.h 2009-04-25 21:01:43 UTC (rev 217)
@@ -10,11 +10,21 @@
unsigned long ReadAndClear();
unsigned long Decrement();
unsigned long Increment();
+ void WaitAndSet(unsigned long value, unsigned long newVal);
private:
unsigned long m_counter;
};
+class MSemaphoreLock
+{
+ public:
+ MSemaphoreLock(MSemaphore* s);
+ ~MSemaphoreLock();
+ private:
+ MSemaphore* m_s;
+};
+
class MSemaphoreIncOnDestroy
{
public:
Modified: Mercury2/src/MercuryAsset.cpp
===================================================================
--- Mercury2/src/MercuryAsset.cpp 2009-04-25 14:20:15 UTC (rev 216)
+++ Mercury2/src/MercuryAsset.cpp 2009-04-25 21:01:43 UTC (rev 217)
@@ -18,6 +18,19 @@
rn->AddRender(this);
}
+void MercuryAsset::SetLoadState(LoadState ls)
+{
+ MSemaphoreLock lock( &m_lock );
+ m_loadState = ls;
+}
+
+LoadState MercuryAsset::GetLoadState()
+{
+ MSemaphoreLock lock( &m_lock );
+ return m_loadState;
+}
+
+
AssetFactory& AssetFactory::GetInstance()
{
static AssetFactory* instance = NULL;
Modified: Mercury2/src/MercuryAsset.h
===================================================================
--- Mercury2/src/MercuryAsset.h 2009-04-25 14:20:15 UTC (rev 216)
+++ Mercury2/src/MercuryAsset.h 2009-04-25 21:01:43 UTC (rev 217)
@@ -7,7 +7,15 @@
#include <map>
#include <MercuryMatrix.h>
#include <BoundingBox.h>
+#include <MSemaphore.h>
+enum LoadState
+{
+ LOADING,
+ LOADED,
+ NONE
+};
+
class MercuryAsset : public RefBase, MessageHandler
{
public:
@@ -23,12 +31,20 @@
///Loads an asset from an XMLAsset representing itself
virtual void LoadFromXML(const XMLNode& node) {};
+ virtual void LoadedCallback() {};
+
inline void IsInstanced(bool b) { m_isInstanced = b; }
inline const BoundingVolume* GetBoundingVolume() const { return m_boundingVolume; }
protected:
+ void SetLoadState(LoadState ls);
+ LoadState GetLoadState();
+
bool m_isInstanced;
BoundingVolume* m_boundingVolume;
+ private:
+ LoadState m_loadState;
+ MSemaphore m_lock;
};
class AssetFactory
Modified: Mercury2/src/MercuryNode.cpp
===================================================================
--- Mercury2/src/MercuryNode.cpp 2009-04-25 14:20:15 UTC (rev 216)
+++ Mercury2/src/MercuryNode.cpp 2009-04-25 21:01:43 UTC (rev 217)
@@ -110,6 +110,7 @@
void MercuryNode::LoadFromXML(const XMLNode& node)
{
+ SetName( node.Attribute("name") );
//Not much to do here except run through all the children nodes
for (XMLNode child = node.Child(); child.IsValid(); child = child.NextNode())
{
Modified: Mercury2/src/MercuryNode.h
===================================================================
--- Mercury2/src/MercuryNode.h 2009-04-25 14:20:15 UTC (rev 216)
+++ Mercury2/src/MercuryNode.h 2009-04-25 21:01:43 UTC (rev 217)
@@ -41,7 +41,7 @@
const std::list< MercuryNode* >& Children() const { return m_children; }
virtual void Update(float dTime) {};
- void RecursiveUpdate(float dTime);
+ virtual void RecursiveUpdate(float dTime);
void ThreadedUpdate(float dTime);
///Run on parent when a child is added
@@ -62,7 +62,9 @@
GENRTTI(MercuryNode);
inline static bool NeedsRebuild() { bool t=m_rebuildRenderGraph; m_rebuildRenderGraph = false; return t; }
-
+ inline void SetName(const MString& name) { m_name = name; }
+ inline MString GetName() const { return m_name; }
+
protected:
std::list< MercuryNode* > m_children; //These nodes are unique, not instanced
MercuryNode* m_parent;
@@ -70,6 +72,7 @@
MercuryNode* m_nextSibling;
static bool m_rebuildRenderGraph;
+ MString m_name;
};
Modified: Mercury2/src/MercuryString.h
===================================================================
--- Mercury2/src/MercuryString.h 2009-04-25 14:20:15 UTC (rev 216)
+++ Mercury2/src/MercuryString.h 2009-04-25 21:01:43 UTC (rev 217)
@@ -32,7 +32,7 @@
inline const char * c_str() const { return m_sCur; }
inline unsigned long length() const { return m_iLen; }
inline unsigned long size() const { return m_iLen; }
- inline bool empty() const { return m_iLen == 0; }
+ inline bool empty() const { return m_iLen <= 0; }
void append( const MString & app );
void append( const char app );
Modified: Mercury2/src/Texture.cpp
===================================================================
--- Mercury2/src/Texture.cpp 2009-04-25 14:20:15 UTC (rev 216)
+++ Mercury2/src/Texture.cpp 2009-04-25 21:01:43 UTC (rev 217)
@@ -41,11 +41,12 @@
rn->AddPostRender( this );
}
-void Texture::LoadFromRaw(const RawImageData* raw)
+void Texture::LoadFromRaw()
{
+ if ( !m_raw ) return;
if ( !m_textureID ) glGenTextures(1, &m_textureID);
- m_raw = raw;
+// m_raw = raw;
int ByteType;
switch (m_raw->m_ColorByteType)
@@ -90,6 +91,11 @@
void Texture::Render(const MercuryNode* node)
{
+ if (GetLoadState() == LOADED)
+ {
+ LoadFromRaw();
+ SetLoadState(NONE);
+ }
BindTexture();
}
@@ -100,7 +106,7 @@
void Texture::LoadFromXML(const XMLNode& node)
{
- LoadImage( node.Attribute("file") );
+ LoadImage( node.Attribute("file") );
}
void Texture::BindTexture()
@@ -132,11 +138,30 @@
{
ADD_ASSET_INSTANCE(Texture, path, this);
m_filename = path;
- RawImageData* d = ImageLoader::GetInstance().LoadImage( m_filename );
- if (d) LoadFromRaw( d );
+ SetLoadState(LOADING);
+
+ MercuryThread loaderThread;
+// ImageLoader::LoadImageThreaded(this, m_filename );
+ ImageLoader::GetInstance().LoadImageThreaded(this, m_filename );
+// LoadFromRaw();
+// RawImageData* d = ImageLoader::GetInstance().LoadImage( m_filename );
+// if (d) LoadFromRaw( d );
+// m_raw = d;
}
}
+void Texture::LoadedCallback()
+{
+ printf("loaded!!!!!\n");
+ SetLoadState(LOADED);
+}
+
+void Texture::SetRawData(RawImageData* raw)
+{
+ SAFE_DELETE(m_raw);
+ m_raw = raw;
+}
+
Texture* Texture::Generate()
{
return new Texture();
Modified: Mercury2/src/Texture.h
===================================================================
--- Mercury2/src/Texture.h 2009-04-25 14:20:15 UTC (rev 216)
+++ Mercury2/src/Texture.h 2009-04-25 21:01:43 UTC (rev 217)
@@ -17,13 +17,16 @@
virtual void LoadFromXML(const XMLNode& node);
- void LoadFromRaw(const RawImageData* raw);
+ 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; }
static Texture* Generate();
static MAutoPtr< Texture > LoadFromFile(const MString& path);
+
+ void SetRawData(RawImageData* raw);
private:
void LoadImage(const MString& path);
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|