diff --git a/include/eepp/graphics/ctextureloader.hpp b/include/eepp/graphics/ctextureloader.hpp index 48f2b3d40..0c39fb58d 100644 --- a/include/eepp/graphics/ctextureloader.hpp +++ b/include/eepp/graphics/ctextureloader.hpp @@ -84,14 +84,6 @@ class EE_API cTextureLoader : public cObjectLoader { /** @return The texture instance ( if it was loaded ). */ cTexture * GetTexture() const; - - /** In the case that the image loading is made outside the GL context main thread, - ** you'll need to force the use of the GL Shared Context - ** @see cWindow::IsThreadedGLContext */ - void ForceUseGLSharedContext( bool force ); - - /** @return If the use of a gl shared context to load the texture is being forced */ - const bool& ForceUseGLSharedContext() const; protected: Uint32 mLoadType; // From memory, from path, from pack Uint8 * mPixels; // Texture Info @@ -107,7 +99,6 @@ class EE_API cTextureLoader : public cObjectLoader { EE_CLAMP_MODE mClampMode; bool mCompressTexture; bool mLocalCopy; - bool mForceGLThreaded; cPack * mPack; cIOStream * mStream; @@ -125,7 +116,7 @@ class EE_API cTextureLoader : public cObjectLoader { int mImgType; int mIsCompressed; - cClock mTE; + cClock mTE; void LoadFile(); void LoadFromPath(); diff --git a/include/eepp/system/cthread.hpp b/include/eepp/system/cthread.hpp index 44be23022..dfd484e8a 100755 --- a/include/eepp/system/cthread.hpp +++ b/include/eepp/system/cthread.hpp @@ -14,6 +14,9 @@ class EE_API cThread : NonCopyable { public: typedef void (*FuncType)(void*); + /** @return The current thread id */ + static Uint32 GetCurrentThreadId(); + /** @brief Construct the thread from a functor with no argument ** This constructor works for function objects, as well ** as free function. @@ -102,6 +105,9 @@ class EE_API cThread : NonCopyable { ** on some operating systems. You should rather try to make ** the thread function terminate by itself. */ void Terminate(); + + /** @return The id of the thread */ + Uint32 Id(); protected: cThread(); private: diff --git a/include/eepp/window/cengine.hpp b/include/eepp/window/cengine.hpp index 98baa19f4..29806e33a 100755 --- a/include/eepp/window/cengine.hpp +++ b/include/eepp/window/cengine.hpp @@ -109,7 +109,7 @@ class EE_API cEngine { */ ContextSettings CreateContextSettings( cIniFile * ini, std::string iniKeyName = "EEPP" ); - /** Enabling Shared GL Context allows asynchronous OpenGL resource loading ( only if is supported by the backend, SDL 2 backend is the only one supported ). + /** Enabling Shared GL Context allows asynchronous OpenGL resource loading ( only if is supported by the backend and the OS, SDL 2 backend is the only one supported ). ** If the cTextureLoader is threaded, will upload the texture in another thread to the GPU. So, it will not block the main rendering thread. ** Shared GL Context is disabled by default. */ @@ -122,6 +122,9 @@ class EE_API cEngine { /** @return If the Shared GL Context is enabled and ready to use. */ bool IsSharedGLContextEnabled(); + + /** @return The id of the thread that was used to initialize the OpenGL Context. */ + Uint32 GetMainThreadId(); protected: friend class cWindow; @@ -129,6 +132,7 @@ class EE_API cEngine { std::list mWindows; cWindow * mWindow; bool mSharedGLContext; + Uint32 mMainThreadId; cEngine(); diff --git a/src/eepp/graphics/ctextureloader.cpp b/src/eepp/graphics/ctextureloader.cpp index b57acf1d4..e5524a09a 100644 --- a/src/eepp/graphics/ctextureloader.cpp +++ b/src/eepp/graphics/ctextureloader.cpp @@ -76,7 +76,6 @@ cTextureLoader::cTextureLoader( cIOStream& Stream, mClampMode(ClampMode), mCompressTexture(CompressTexture), mLocalCopy(KeepLocalCopy), - mForceGLThreaded(false), mPack(NULL), mStream(&Stream), mImagePtr(NULL), @@ -108,7 +107,6 @@ cTextureLoader::cTextureLoader( const std::string& Filepath, mClampMode(ClampMode), mCompressTexture(CompressTexture), mLocalCopy(KeepLocalCopy), - mForceGLThreaded(false), mPack(NULL), mStream(NULL), mImagePtr(NULL), @@ -141,7 +139,6 @@ cTextureLoader::cTextureLoader( const unsigned char * ImagePtr, mClampMode(ClampMode), mCompressTexture(CompressTexture), mLocalCopy(KeepLocalCopy), - mForceGLThreaded(false), mPack(NULL), mStream(NULL), mImagePtr(ImagePtr), @@ -174,7 +171,6 @@ cTextureLoader::cTextureLoader( cPack * Pack, mClampMode(ClampMode), mCompressTexture(CompressTexture), mLocalCopy(KeepLocalCopy), - mForceGLThreaded(false), mPack(Pack), mStream(NULL), mImagePtr(NULL), @@ -210,7 +206,6 @@ cTextureLoader::cTextureLoader( const unsigned char * Pixels, mClampMode(ClampMode), mCompressTexture(CompressTexture), mLocalCopy(KeepLocalCopy), - mForceGLThreaded(false), mPack(NULL), mStream(NULL), mImagePtr(NULL), @@ -288,7 +283,7 @@ void cTextureLoader::LoadFromPath() { } if ( NULL == mPixels ) { - eePRINTL( "Filed to load: %s. Reason: ", mFilepath.c_str(), stbi_failure_reason() ); + eePRINTL( "Filed to load: %s. Reason: %s", mFilepath.c_str(), stbi_failure_reason() ); if ( STBI_jpeg == mImgType ) { mPixels = jpgd::decompress_jpeg_image_from_file( mFilepath.c_str(), &mImgWidth, &mImgHeight, &mChannels, 3 ); @@ -345,7 +340,7 @@ void cTextureLoader::LoadFromMemory() { } if ( NULL == mPixels ) { - eePRINTL( stbi_failure_reason() ); + eePRINTL( "Filed to load image from memory. Reason: %s", stbi_failure_reason() ); if ( STBI_jpeg == mImgType ) { mPixels = jpgd::decompress_jpeg_image_from_memory( mImagePtr, mSize, &mImgWidth, &mImgHeight, &mChannels, 3 ); @@ -428,8 +423,10 @@ void cTextureLoader::LoadFromPixels() { flags = ( mClampMode == CLAMP_REPEAT) ? (flags | SOIL_FLAG_TEXTURE_REPEATS) : flags; flags = ( mCompressTexture ) ? ( flags | SOIL_FLAG_COMPRESS_TO_DXT ) : flags; - if ( ( mThreaded || mForceGLThreaded ) && - ( mForceGLThreaded || cEngine::instance()->IsSharedGLContextEnabled() ) && + bool ForceGLThreaded = cThread::GetCurrentThreadId() != cEngine::instance()->GetMainThreadId(); + + if ( ( mThreaded || ForceGLThreaded ) && + ( ForceGLThreaded || cEngine::instance()->IsSharedGLContextEnabled() ) && cEngine::instance()->GetCurrentWindow()->IsThreadedGLContext() ) { cEngine::instance()->GetCurrentWindow()->SetGLContextThread(); @@ -464,8 +461,8 @@ void cTextureLoader::LoadFromPixels() { GLi->BindTexture( GL_TEXTURE_2D, PreviousTexture ); - if ( ( mThreaded || mForceGLThreaded ) && - ( mForceGLThreaded || cEngine::instance()->IsSharedGLContextEnabled() ) && + if ( ( mThreaded || ForceGLThreaded ) && + ( ForceGLThreaded || cEngine::instance()->IsSharedGLContextEnabled() ) && cEngine::instance()->GetCurrentWindow()->IsThreadedGLContext() ) { cEngine::instance()->GetCurrentWindow()->UnsetGLContextThread(); @@ -500,7 +497,7 @@ void cTextureLoader::LoadFromPixels() { eePRINTL( "Texture %s loaded in %4.3f ms.", mFilepath.c_str(), mTE.Elapsed().AsMilliseconds() ); } else { - eePRINTL( "Failed to create texture. Reason: ", SOIL_last_result() ); + eePRINTL( "Failed to create texture. Reason: %s", SOIL_last_result() ); } if ( TEX_LT_PIXELS != mLoadType ) { @@ -554,14 +551,6 @@ cTexture * cTextureLoader::GetTexture() const { return NULL; } -const bool& cTextureLoader::ForceUseGLSharedContext() const { - return mForceGLThreaded; -} - -void cTextureLoader::ForceUseGLSharedContext( bool force ) { - mForceGLThreaded = force; -} - void cTextureLoader::Unload() { if ( mLoaded ) { cTextureFactory::instance()->Remove( mTexId ); @@ -583,7 +572,6 @@ void cTextureLoader::Reset() { mSize = 0; mTexLoaded = false; mDirectUpload = false; - mForceGLThreaded = false; mImgType = STBI_unknown; mIsCompressed = 0; } diff --git a/src/eepp/system/cthread.cpp b/src/eepp/system/cthread.cpp index a14e91b9e..7280eb102 100755 --- a/src/eepp/system/cthread.cpp +++ b/src/eepp/system/cthread.cpp @@ -3,6 +3,10 @@ namespace EE { namespace System { +Uint32 cThread::GetCurrentThreadId() { + return Platform::cThreadImpl::GetCurrentThreadId(); +} + cThread::cThread() : mThreadImpl(NULL), mEntryPoint(NULL) @@ -38,6 +42,10 @@ void cThread::Terminate() { } } +Uint32 cThread::Id() { + return mThreadImpl->Id(); +} + void cThread::Run() { mEntryPoint->Run(); } diff --git a/src/eepp/system/platform/posix/cthreadimpl.cpp b/src/eepp/system/platform/posix/cthreadimpl.cpp index 437e1e9de..935df1207 100644 --- a/src/eepp/system/platform/posix/cthreadimpl.cpp +++ b/src/eepp/system/platform/posix/cthreadimpl.cpp @@ -6,6 +6,10 @@ namespace EE { namespace System { namespace Platform { #if defined( EE_PLATFORM_POSIX ) +Uint32 cThreadImpl::GetCurrentThreadId() { + return (Uint32)pthread_self(); +} + cThreadImpl::cThreadImpl( cThread * owner ) : mIsActive(false) { @@ -38,6 +42,10 @@ void cThreadImpl::Terminate() { } } +Uint32 cThreadImpl::Id() { + return (Uint32)mThread; +} + void * cThreadImpl::EntryPoint( void * userData ) { // The Thread instance is stored in the user data cThread * owner = static_cast( userData ); diff --git a/src/eepp/system/platform/posix/cthreadimpl.hpp b/src/eepp/system/platform/posix/cthreadimpl.hpp index 263c90d1e..b9d87ad82 100644 --- a/src/eepp/system/platform/posix/cthreadimpl.hpp +++ b/src/eepp/system/platform/posix/cthreadimpl.hpp @@ -15,11 +15,15 @@ namespace Platform { class cThreadImpl { public: + static Uint32 GetCurrentThreadId(); + cThreadImpl( cThread * owner ); void Wait(); void Terminate(); + + Uint32 Id(); protected: static void * EntryPoint( void* userData ); diff --git a/src/eepp/system/platform/win/cthreadimpl.cpp b/src/eepp/system/platform/win/cthreadimpl.cpp index 2c1daa0ee..b6f672425 100644 --- a/src/eepp/system/platform/win/cthreadimpl.cpp +++ b/src/eepp/system/platform/win/cthreadimpl.cpp @@ -6,6 +6,10 @@ namespace EE { namespace System { namespace Platform { #if EE_PLATFORM == EE_PLATFORM_WIN +Uint32 cThreadImpl::GetCurrentThreadId() { + return (Uint32)::GetCurrentThreadId(); +} + cThreadImpl::cThreadImpl( cThread * owner ) { mThread = reinterpret_cast( _beginthreadex( NULL, 0, &cThreadImpl::EntryPoint, owner, 0, &mThreadId ) ); @@ -28,6 +32,10 @@ void cThreadImpl::Terminate() { } } +Uint32 cThreadImpl::Id() { + return (Uint32)mThreadId; +} + unsigned int __stdcall cThreadImpl::EntryPoint( void * userData ) { // The Thread instance is stored in the user data cThread * owner = static_cast( userData ); diff --git a/src/eepp/system/platform/win/cthreadimpl.hpp b/src/eepp/system/platform/win/cthreadimpl.hpp index d43e1469b..4fa22c6ad 100644 --- a/src/eepp/system/platform/win/cthreadimpl.hpp +++ b/src/eepp/system/platform/win/cthreadimpl.hpp @@ -18,11 +18,15 @@ namespace Platform { class cThreadImpl { public: + static Uint32 GetCurrentThreadId(); + cThreadImpl( cThread * owner ); void Wait(); void Terminate(); + + Uint32 Id(); protected: static unsigned int __stdcall EntryPoint(void* userData); diff --git a/src/eepp/window/cengine.cpp b/src/eepp/window/cengine.cpp index e7fde3450..704b9eaae 100755 --- a/src/eepp/window/cengine.cpp +++ b/src/eepp/window/cengine.cpp @@ -42,7 +42,8 @@ SINGLETON_DECLARE_IMPLEMENTATION(cEngine) cEngine::cEngine() : mBackend( NULL ), mWindow( NULL ), - mSharedGLContext( false ) + mSharedGLContext( false ), + mMainThreadId( 0 ) { cTextureAtlasManager::CreateSingleton(); } @@ -165,6 +166,8 @@ cWindow * cEngine::CreateWindow( WindowSettings Settings, ContextSettings Contex if ( NULL != mWindow ) { Settings.Backend = mWindow->GetWindowInfo()->WindowConfig.Backend; + } else { + mMainThreadId = cThread::GetCurrentThreadId(); } switch ( Settings.Backend ) { @@ -356,4 +359,8 @@ bool cEngine::IsSharedGLContextEnabled() { return mSharedGLContext; } +Uint32 cEngine::GetMainThreadId() { + return mMainThreadId; +} + }} diff --git a/src/test/eetest.cpp b/src/test/eetest.cpp index 0700dcfbd..f0a81f9db 100644 --- a/src/test/eetest.cpp +++ b/src/test/eetest.cpp @@ -822,6 +822,11 @@ void cEETest::LoadTextures() { PakTest = eeNew( cZip, () ); #ifndef EE_GLES + + #if defined( EE_X11_PLATFORM ) || EE_PLATFORM == EE_PLATFORM_WIN || EE_PLATFORM == EE_PLATFORM_MACOSX + cEngine::instance()->EnableSharedGLContext(); + #endif + PakTest->Open( MyPath + "test.zip" ); std::vector files = PakTest->GetFileList();