diff --git a/.gitignore b/.gitignore index fcee8ad4..8dfb3119 100644 --- a/.gitignore +++ b/.gitignore @@ -32,5 +32,6 @@ Makefile library.bin leocad_plugin_import.cpp leocad_resource.* +povray uic_wrapper.bat /tools/setup/*.exe diff --git a/common/lc_application.cpp b/common/lc_application.cpp index ae352148..24d6f98b 100644 --- a/common/lc_application.cpp +++ b/common/lc_application.cpp @@ -905,7 +905,7 @@ lcStartupMode lcApplication::Initialize(const QList>& Libra if (Options.Exit) return lcStartupMode::Success; - if (!InitializeRenderer()) + if (!lcContext::InitializeRenderer()) { StdErr << tr("Error creating OpenGL context.\n"); return lcStartupMode::Error; @@ -1206,20 +1206,7 @@ void lcApplication::Shutdown() delete mLibrary; mLibrary = nullptr; - ShutdownRenderer(); -} - -bool lcApplication::InitializeRenderer() -{ - if (!lcContext::CreateOffscreenContext()) - return false; - - return true; -} - -void lcApplication::ShutdownRenderer() -{ - lcContext::DestroyOffscreenContext(); + lcContext::ShutdownRenderer(); } void lcApplication::ShowPreferencesDialog() diff --git a/common/lc_application.h b/common/lc_application.h index fa495116..4eef1997 100644 --- a/common/lc_application.h +++ b/common/lc_application.h @@ -169,8 +169,6 @@ public: QByteArray mClipboard; protected: - bool InitializeRenderer(); - void ShutdownRenderer(); void UpdateStyle(); QString GetTabLayoutKey() const; diff --git a/common/lc_context.cpp b/common/lc_context.cpp index 1bda6576..49e11b63 100644 --- a/common/lc_context.cpp +++ b/common/lc_context.cpp @@ -26,8 +26,8 @@ std::unique_ptr lcContext::mOffscreenContext; std::unique_ptr lcContext::mOffscreenSurface; +std::unique_ptr lcContext::mGlobalOffscreenContext; lcProgram lcContext::mPrograms[static_cast(lcMaterialType::Count)]; -int lcContext::mValidContexts; lcContext::lcContext() { @@ -74,21 +74,60 @@ lcContext::lcContext() lcContext::~lcContext() { - if (mValid) - { - mValidContexts--; +} - if (!mValidContexts) - { - gStringCache.Reset(); - gTexFont.Reset(); +bool lcContext::InitializeRenderer() +{ + if (!CreateOffscreenContext()) + return false; - lcGetPiecesLibrary()->ReleaseBuffers(this); - lcView::DestroyResources(this); - DestroyResources(); - lcViewSphere::DestroyResources(this); - } - } + mGlobalOffscreenContext = std::unique_ptr(new(lcContext)); + + lcContext* Context = mGlobalOffscreenContext.get(); + Context->SetOffscreenContext(); + + lcInitializeGLExtensions(mOffscreenContext.get()); + + // TODO: Find a better place for the grid texture and font + gStringCache.Initialize(Context); + gTexFont.Initialize(Context); + + Context->CreateResources(); + lcView::CreateResources(Context); + lcViewSphere::CreateResources(Context); + + if (!gSupportsShaderObjects && lcGetPreferences().mShadingMode == lcShadingMode::DefaultLights) + lcGetPreferences().mShadingMode = lcShadingMode::Flat; + + if (!gSupportsShaderObjects && lcGetPreferences().mDrawConditionalLines) + lcGetPreferences().mDrawConditionalLines = false; + + if (!gSupportsFramebufferObject) + gMainWindow->GetPartSelectionWidget()->DisableIconMode(); + + return true; +} + +void lcContext::ShutdownRenderer() +{ + mGlobalOffscreenContext->MakeCurrent(); + lcContext* Context = mGlobalOffscreenContext.get(); + + gStringCache.Reset(); + gTexFont.Reset(); + + lcView::DestroyResources(Context); + Context->DestroyResources(); + lcViewSphere::DestroyResources(Context); + + mGlobalOffscreenContext.reset(); + + lcContext::DestroyOffscreenContext(); +} + +lcContext* lcContext::GetGlobalOffscreenContext() +{ + return mGlobalOffscreenContext.get(); } bool lcContext::CreateOffscreenContext() @@ -312,31 +351,6 @@ void lcContext::SetGLContext(QOpenGLContext* Context, QOpenGLWidget* Widget) MakeCurrent(); initializeOpenGLFunctions(); - - if (!mValidContexts) - { - lcInitializeGLExtensions(Context); - - // TODO: Find a better place for the grid texture and font - gStringCache.Initialize(this); - gTexFont.Initialize(this); - - CreateResources(); - lcView::CreateResources(this); - lcViewSphere::CreateResources(this); - - if (!gSupportsShaderObjects && lcGetPreferences().mShadingMode == lcShadingMode::DefaultLights) - lcGetPreferences().mShadingMode = lcShadingMode::Flat; - - if (!gSupportsShaderObjects && lcGetPreferences().mDrawConditionalLines) - lcGetPreferences().mDrawConditionalLines = false; - - if (!gSupportsFramebufferObject) - gMainWindow->GetPartSelectionWidget()->DisableIconMode(); - } - - mValid = true; - mValidContexts++; } void lcContext::SetOffscreenContext() diff --git a/common/lc_context.h b/common/lc_context.h index e4e27087..d2a6a4ff 100644 --- a/common/lc_context.h +++ b/common/lc_context.h @@ -99,8 +99,9 @@ public: lcContext(const lcContext&) = delete; lcContext& operator=(const lcContext&) = delete; - static bool CreateOffscreenContext(); - static void DestroyOffscreenContext(); + static bool InitializeRenderer(); + static void ShutdownRenderer(); + static lcContext* GetGlobalOffscreenContext(); void CreateResources(); void DestroyResources(); @@ -199,12 +200,14 @@ public: void BindMesh(const lcMesh* Mesh); protected: + static bool CreateOffscreenContext(); + static void DestroyOffscreenContext(); + void CreateShaderPrograms(); void FlushState(); QOpenGLWidget* mWidget = nullptr; QOpenGLContext* mContext = nullptr; - bool mValid = false; GLuint mVertexBufferObject; GLuint mIndexBufferObject; @@ -242,9 +245,9 @@ protected: static std::unique_ptr mOffscreenContext; static std::unique_ptr mOffscreenSurface; + static std::unique_ptr mGlobalOffscreenContext; static lcProgram mPrograms[static_cast(lcMaterialType::Count)]; - static int mValidContexts; Q_DECLARE_TR_FUNCTIONS(lcContext); }; diff --git a/common/lc_library.cpp b/common/lc_library.cpp index fd82d046..c4c54479 100644 --- a/common/lc_library.cpp +++ b/common/lc_library.cpp @@ -52,6 +52,7 @@ lcPiecesLibrary::~lcPiecesLibrary() mCancelLoading = true; WaitForLoadQueue(); Unload(); + ReleaseBuffers(); } void lcPiecesLibrary::Unload() @@ -1427,10 +1428,14 @@ void lcPiecesLibrary::GetPieceFile(const char* PieceName, std::functionMakeCurrent(); Context->DestroyVertexBuffer(mVertexBuffer); Context->DestroyIndexBuffer(mIndexBuffer); + mBuffersDirty = true; } @@ -1506,6 +1511,7 @@ void lcPiecesLibrary::UnloadUnusedParts() bool lcPiecesLibrary::LoadTexture(lcTexture* Texture) { + QMutexLocker Lock(&mTextureMutex); char FileName[2*LC_MAXPATH]; if (mZipFiles[static_cast(lcZipFileType::Official)]) @@ -1541,22 +1547,6 @@ void lcPiecesLibrary::ReleaseTexture(lcTexture* Texture) } } -void lcPiecesLibrary::QueueTextureUpload(lcTexture* Texture) -{ - QMutexLocker Lock(&mTextureMutex); - mTextureUploads.push_back(Texture); -} - -void lcPiecesLibrary::UploadTextures(lcContext* Context) -{ - QMutexLocker Lock(&mTextureMutex); - - for (lcTexture* Texture : mTextureUploads) - Texture->Upload(Context); - - mTextureUploads.clear(); -} - bool lcPiecesLibrary::SupportsStudStyle() const { return true; diff --git a/common/lc_library.h b/common/lc_library.h index b79d25da..b3fe464d 100644 --- a/common/lc_library.h +++ b/common/lc_library.h @@ -117,9 +117,7 @@ public: ~lcPiecesLibrary(); lcPiecesLibrary(const lcPiecesLibrary&) = delete; - lcPiecesLibrary(lcPiecesLibrary&&) = delete; lcPiecesLibrary& operator=(const lcPiecesLibrary&) = delete; - lcPiecesLibrary& operator=(lcPiecesLibrary&&) = delete; bool Load(const QString& LibraryPath, bool ShowProgress); void LoadColors(); @@ -139,8 +137,6 @@ public: lcTexture* FindTexture(const char* TextureName, Project* CurrentProject, bool SearchProjectFolder); bool LoadTexture(lcTexture* Texture); void ReleaseTexture(lcTexture* Texture); - void QueueTextureUpload(lcTexture* Texture); - void UploadTextures(lcContext* Context); bool PieceInCategory(PieceInfo* Info, const char* CategoryKeywords) const; void GetCategoryEntries(int CategoryIndex, bool GroupPieces, lcArray& SinglePieces, lcArray& GroupedPieces); @@ -177,7 +173,6 @@ public: return mCancelLoading; } - void ReleaseBuffers(lcContext* Context); void UpdateBuffers(lcContext* Context); void UnloadUnusedParts(); @@ -216,6 +211,8 @@ protected: static bool IsStudStylePrimitive(const char* FileName); void UpdateStudStyleSource(); + void ReleaseBuffers(); + std::vector> mSources; QMutex mLoadMutex; @@ -223,7 +220,6 @@ protected: QList mLoadQueue; QMutex mTextureMutex; - std::vector mTextureUploads; lcStudStyle mStudStyle; diff --git a/common/lc_scene.cpp b/common/lc_scene.cpp index 39a4fcb3..d73fde30 100644 --- a/common/lc_scene.cpp +++ b/common/lc_scene.cpp @@ -398,7 +398,6 @@ void lcScene::Draw(lcContext* Context) const { // TODO: find a better place for these updates lcGetPiecesLibrary()->UpdateBuffers(Context); - lcGetPiecesLibrary()->UploadTextures(Context); Context->SetViewMatrix(mViewMatrix); diff --git a/common/lc_texture.cpp b/common/lc_texture.cpp index f3562746..24c4dc37 100644 --- a/common/lc_texture.cpp +++ b/common/lc_texture.cpp @@ -159,7 +159,9 @@ void lcTexture::CreateGridTexture() mRefCount = 1; mFlags = LC_TEXTURE_WRAPU | LC_TEXTURE_WRAPV | LC_TEXTURE_MIPMAPS | LC_TEXTURE_ANISOTROPIC; - lcGetPiecesLibrary()->QueueTextureUpload(this); + lcContext* Context = lcContext::GetGlobalOffscreenContext(); + Context->MakeCurrent(); + Upload(Context); } bool lcTexture::Load() @@ -329,7 +331,10 @@ bool lcTexture::Load(int Flags) Image.ResizePow2(); mFlags = Flags; - lcGetPiecesLibrary()->QueueTextureUpload(this); + lcContext* Context = lcContext::GetGlobalOffscreenContext(); + Context->MakeCurrent(); + Upload(Context); + return true; }