Moved global resource creation to startup instead of context creation.

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

1
.gitignore vendored
View file

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

View file

@ -905,7 +905,7 @@ lcStartupMode lcApplication::Initialize(const QList<QPair<QString, bool>>& 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()

View file

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

View file

@ -26,8 +26,8 @@
std::unique_ptr<QOpenGLContext> lcContext::mOffscreenContext;
std::unique_ptr<QOffscreenSurface> lcContext::mOffscreenSurface;
std::unique_ptr<lcContext> lcContext::mGlobalOffscreenContext;
lcProgram lcContext::mPrograms[static_cast<int>(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<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();
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()

View file

@ -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<QOpenGLContext> mOffscreenContext;
static std::unique_ptr<QOffscreenSurface> mOffscreenSurface;
static std::unique_ptr<lcContext> mGlobalOffscreenContext;
static lcProgram mPrograms[static_cast<int>(lcMaterialType::Count)];
static int mValidContexts;
Q_DECLARE_TR_FUNCTIONS(lcContext);
};

View file

@ -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::function<void(lcF
}
}
void lcPiecesLibrary::ReleaseBuffers(lcContext* Context)
void lcPiecesLibrary::ReleaseBuffers()
{
lcContext* Context = lcContext::GetGlobalOffscreenContext();
Context->MakeCurrent();
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<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
{
return true;

View file

@ -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<PieceInfo*>& SinglePieces, lcArray<PieceInfo*>& 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<std::unique_ptr<lcLibrarySource>> mSources;
QMutex mLoadMutex;
@ -223,7 +220,6 @@ protected:
QList<PieceInfo*> mLoadQueue;
QMutex mTextureMutex;
std::vector<lcTexture*> mTextureUploads;
lcStudStyle mStudStyle;

View file

@ -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);

View file

@ -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;
}