Moved global resource creation to startup instead of context creation. Fixes #645.

This commit is contained in:
Leonardo Zide 2021-03-10 21:21:47 -08:00
parent 44d0e92b50
commit 233affe3fc
9 changed files with 79 additions and 86 deletions

1
.gitignore vendored
View file

@ -32,5 +32,6 @@ Makefile
library.bin library.bin
leocad_plugin_import.cpp leocad_plugin_import.cpp
leocad_resource.* leocad_resource.*
povray
uic_wrapper.bat uic_wrapper.bat
/tools/setup/*.exe /tools/setup/*.exe

View file

@ -905,7 +905,7 @@ lcStartupMode lcApplication::Initialize(const QList<QPair<QString, bool>>& Libra
if (Options.Exit) if (Options.Exit)
return lcStartupMode::Success; return lcStartupMode::Success;
if (!InitializeRenderer()) if (!lcContext::InitializeRenderer())
{ {
StdErr << tr("Error creating OpenGL context.\n"); StdErr << tr("Error creating OpenGL context.\n");
return lcStartupMode::Error; return lcStartupMode::Error;
@ -1206,20 +1206,7 @@ void lcApplication::Shutdown()
delete mLibrary; delete mLibrary;
mLibrary = nullptr; mLibrary = nullptr;
ShutdownRenderer(); lcContext::ShutdownRenderer();
}
bool lcApplication::InitializeRenderer()
{
if (!lcContext::CreateOffscreenContext())
return false;
return true;
}
void lcApplication::ShutdownRenderer()
{
lcContext::DestroyOffscreenContext();
} }
void lcApplication::ShowPreferencesDialog() void lcApplication::ShowPreferencesDialog()

View file

@ -169,8 +169,6 @@ public:
QByteArray mClipboard; QByteArray mClipboard;
protected: protected:
bool InitializeRenderer();
void ShutdownRenderer();
void UpdateStyle(); void UpdateStyle();
QString GetTabLayoutKey() const; QString GetTabLayoutKey() const;

View file

@ -26,8 +26,8 @@
std::unique_ptr<QOpenGLContext> lcContext::mOffscreenContext; std::unique_ptr<QOpenGLContext> lcContext::mOffscreenContext;
std::unique_ptr<QOffscreenSurface> lcContext::mOffscreenSurface; std::unique_ptr<QOffscreenSurface> lcContext::mOffscreenSurface;
std::unique_ptr<lcContext> lcContext::mGlobalOffscreenContext;
lcProgram lcContext::mPrograms[static_cast<int>(lcMaterialType::Count)]; lcProgram lcContext::mPrograms[static_cast<int>(lcMaterialType::Count)];
int lcContext::mValidContexts;
lcContext::lcContext() lcContext::lcContext()
{ {
@ -74,21 +74,60 @@ lcContext::lcContext()
lcContext::~lcContext() lcContext::~lcContext()
{ {
if (mValid) }
{
mValidContexts--;
if (!mValidContexts) bool lcContext::InitializeRenderer()
{ {
if (!CreateOffscreenContext())
return false;
mGlobalOffscreenContext = std::unique_ptr<lcContext>(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(); gStringCache.Reset();
gTexFont.Reset(); gTexFont.Reset();
lcGetPiecesLibrary()->ReleaseBuffers(this); lcView::DestroyResources(Context);
lcView::DestroyResources(this); Context->DestroyResources();
DestroyResources(); lcViewSphere::DestroyResources(Context);
lcViewSphere::DestroyResources(this);
} mGlobalOffscreenContext.reset();
lcContext::DestroyOffscreenContext();
} }
lcContext* lcContext::GetGlobalOffscreenContext()
{
return mGlobalOffscreenContext.get();
} }
bool lcContext::CreateOffscreenContext() bool lcContext::CreateOffscreenContext()
@ -312,31 +351,6 @@ void lcContext::SetGLContext(QOpenGLContext* Context, QOpenGLWidget* Widget)
MakeCurrent(); MakeCurrent();
initializeOpenGLFunctions(); 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() void lcContext::SetOffscreenContext()

View file

@ -99,8 +99,9 @@ public:
lcContext(const lcContext&) = delete; lcContext(const lcContext&) = delete;
lcContext& operator=(const lcContext&) = delete; lcContext& operator=(const lcContext&) = delete;
static bool CreateOffscreenContext(); static bool InitializeRenderer();
static void DestroyOffscreenContext(); static void ShutdownRenderer();
static lcContext* GetGlobalOffscreenContext();
void CreateResources(); void CreateResources();
void DestroyResources(); void DestroyResources();
@ -199,12 +200,14 @@ public:
void BindMesh(const lcMesh* Mesh); void BindMesh(const lcMesh* Mesh);
protected: protected:
static bool CreateOffscreenContext();
static void DestroyOffscreenContext();
void CreateShaderPrograms(); void CreateShaderPrograms();
void FlushState(); void FlushState();
QOpenGLWidget* mWidget = nullptr; QOpenGLWidget* mWidget = nullptr;
QOpenGLContext* mContext = nullptr; QOpenGLContext* mContext = nullptr;
bool mValid = false;
GLuint mVertexBufferObject; GLuint mVertexBufferObject;
GLuint mIndexBufferObject; GLuint mIndexBufferObject;
@ -242,9 +245,9 @@ protected:
static std::unique_ptr<QOpenGLContext> mOffscreenContext; static std::unique_ptr<QOpenGLContext> mOffscreenContext;
static std::unique_ptr<QOffscreenSurface> mOffscreenSurface; static std::unique_ptr<QOffscreenSurface> mOffscreenSurface;
static std::unique_ptr<lcContext> mGlobalOffscreenContext;
static lcProgram mPrograms[static_cast<int>(lcMaterialType::Count)]; static lcProgram mPrograms[static_cast<int>(lcMaterialType::Count)];
static int mValidContexts;
Q_DECLARE_TR_FUNCTIONS(lcContext); Q_DECLARE_TR_FUNCTIONS(lcContext);
}; };

View file

@ -52,6 +52,7 @@ lcPiecesLibrary::~lcPiecesLibrary()
mCancelLoading = true; mCancelLoading = true;
WaitForLoadQueue(); WaitForLoadQueue();
Unload(); Unload();
ReleaseBuffers();
} }
void lcPiecesLibrary::Unload() void lcPiecesLibrary::Unload()
@ -1427,10 +1428,14 @@ void lcPiecesLibrary::GetPieceFile(const char* PieceName, std::function<void(lcF
} }
} }
void lcPiecesLibrary::ReleaseBuffers(lcContext* Context) void lcPiecesLibrary::ReleaseBuffers()
{ {
lcContext* Context = lcContext::GetGlobalOffscreenContext();
Context->MakeCurrent();
Context->DestroyVertexBuffer(mVertexBuffer); Context->DestroyVertexBuffer(mVertexBuffer);
Context->DestroyIndexBuffer(mIndexBuffer); Context->DestroyIndexBuffer(mIndexBuffer);
mBuffersDirty = true; mBuffersDirty = true;
} }
@ -1506,6 +1511,7 @@ void lcPiecesLibrary::UnloadUnusedParts()
bool lcPiecesLibrary::LoadTexture(lcTexture* Texture) bool lcPiecesLibrary::LoadTexture(lcTexture* Texture)
{ {
QMutexLocker Lock(&mTextureMutex);
char FileName[2*LC_MAXPATH]; char FileName[2*LC_MAXPATH];
if (mZipFiles[static_cast<int>(lcZipFileType::Official)]) if (mZipFiles[static_cast<int>(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 bool lcPiecesLibrary::SupportsStudStyle() const
{ {
return true; return true;

View file

@ -117,9 +117,7 @@ public:
~lcPiecesLibrary(); ~lcPiecesLibrary();
lcPiecesLibrary(const lcPiecesLibrary&) = delete; lcPiecesLibrary(const lcPiecesLibrary&) = delete;
lcPiecesLibrary(lcPiecesLibrary&&) = delete;
lcPiecesLibrary& operator=(const lcPiecesLibrary&) = delete; lcPiecesLibrary& operator=(const lcPiecesLibrary&) = delete;
lcPiecesLibrary& operator=(lcPiecesLibrary&&) = delete;
bool Load(const QString& LibraryPath, bool ShowProgress); bool Load(const QString& LibraryPath, bool ShowProgress);
void LoadColors(); void LoadColors();
@ -139,8 +137,6 @@ public:
lcTexture* FindTexture(const char* TextureName, Project* CurrentProject, bool SearchProjectFolder); lcTexture* FindTexture(const char* TextureName, Project* CurrentProject, bool SearchProjectFolder);
bool LoadTexture(lcTexture* Texture); bool LoadTexture(lcTexture* Texture);
void ReleaseTexture(lcTexture* Texture); void ReleaseTexture(lcTexture* Texture);
void QueueTextureUpload(lcTexture* Texture);
void UploadTextures(lcContext* Context);
bool PieceInCategory(PieceInfo* Info, const char* CategoryKeywords) const; bool PieceInCategory(PieceInfo* Info, const char* CategoryKeywords) const;
void GetCategoryEntries(int CategoryIndex, bool GroupPieces, lcArray<PieceInfo*>& SinglePieces, lcArray<PieceInfo*>& GroupedPieces); void GetCategoryEntries(int CategoryIndex, bool GroupPieces, lcArray<PieceInfo*>& SinglePieces, lcArray<PieceInfo*>& GroupedPieces);
@ -177,7 +173,6 @@ public:
return mCancelLoading; return mCancelLoading;
} }
void ReleaseBuffers(lcContext* Context);
void UpdateBuffers(lcContext* Context); void UpdateBuffers(lcContext* Context);
void UnloadUnusedParts(); void UnloadUnusedParts();
@ -216,6 +211,8 @@ protected:
static bool IsStudStylePrimitive(const char* FileName); static bool IsStudStylePrimitive(const char* FileName);
void UpdateStudStyleSource(); void UpdateStudStyleSource();
void ReleaseBuffers();
std::vector<std::unique_ptr<lcLibrarySource>> mSources; std::vector<std::unique_ptr<lcLibrarySource>> mSources;
QMutex mLoadMutex; QMutex mLoadMutex;
@ -223,7 +220,6 @@ protected:
QList<PieceInfo*> mLoadQueue; QList<PieceInfo*> mLoadQueue;
QMutex mTextureMutex; QMutex mTextureMutex;
std::vector<lcTexture*> mTextureUploads;
lcStudStyle mStudStyle; lcStudStyle mStudStyle;

View file

@ -398,7 +398,6 @@ void lcScene::Draw(lcContext* Context) const
{ {
// TODO: find a better place for these updates // TODO: find a better place for these updates
lcGetPiecesLibrary()->UpdateBuffers(Context); lcGetPiecesLibrary()->UpdateBuffers(Context);
lcGetPiecesLibrary()->UploadTextures(Context);
Context->SetViewMatrix(mViewMatrix); Context->SetViewMatrix(mViewMatrix);

View file

@ -159,7 +159,9 @@ void lcTexture::CreateGridTexture()
mRefCount = 1; mRefCount = 1;
mFlags = LC_TEXTURE_WRAPU | LC_TEXTURE_WRAPV | LC_TEXTURE_MIPMAPS | LC_TEXTURE_ANISOTROPIC; 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() bool lcTexture::Load()
@ -329,7 +331,10 @@ bool lcTexture::Load(int Flags)
Image.ResizePow2(); Image.ResizePow2();
mFlags = Flags; mFlags = Flags;
lcGetPiecesLibrary()->QueueTextureUpload(this); lcContext* Context = lcContext::GetGlobalOffscreenContext();
Context->MakeCurrent();
Upload(Context);
return true; return true;
} }