From 555fbf479ac9af356446e71bb53187981b865167 Mon Sep 17 00:00:00 2001 From: Dan Paulat Date: Sun, 10 Sep 2023 21:44:19 -0500 Subject: [PATCH] Don't call glGenTextures every time the texture atlas updates --- scwx-qt/source/scwx/qt/gl/gl_context.cpp | 23 ++++++++++++++++--- scwx-qt/source/scwx/qt/util/texture_atlas.cpp | 7 +----- scwx-qt/source/scwx/qt/util/texture_atlas.hpp | 8 +++---- 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/scwx-qt/source/scwx/qt/gl/gl_context.cpp b/scwx-qt/source/scwx/qt/gl/gl_context.cpp index 64e2df46..592d12cb 100644 --- a/scwx-qt/source/scwx/qt/gl/gl_context.cpp +++ b/scwx-qt/source/scwx/qt/gl/gl_context.cpp @@ -26,11 +26,15 @@ public: } ~Impl() {} + void InitializeGL(); + static std::size_t GetShaderKey(std::initializer_list> shaders); gl::OpenGLFunctions gl_; + bool glInitialized_ {false}; + std::unordered_map> shaderProgramMap_; std::mutex shaderProgramMutex_; @@ -57,6 +61,18 @@ std::uint64_t GlContext::texture_buffer_count() const return p->textureBufferCount_; } +void GlContext::Impl::InitializeGL() +{ + if (glInitialized_) + { + return; + } + + gl_.glGenTextures(1, &textureAtlas_); + + glInitialized_ = true; +} + std::shared_ptr GlContext::GetShaderProgram(const std::string& vertexPath, const std::string& fragmentPath) @@ -91,15 +107,16 @@ std::shared_ptr GlContext::GetShaderProgram( GLuint GlContext::GetTextureAtlas() { + p->InitializeGL(); + std::unique_lock lock(p->textureMutex_); auto& textureAtlas = util::TextureAtlas::Instance(); - if (p->textureAtlas_ == GL_INVALID_INDEX || - p->textureBufferCount_ != textureAtlas.BuildCount()) + if (p->textureBufferCount_ != textureAtlas.BuildCount()) { p->textureBufferCount_ = textureAtlas.BuildCount(); - p->textureAtlas_ = textureAtlas.BufferAtlas(p->gl_); + textureAtlas.BufferAtlas(p->gl_, p->textureAtlas_); } return p->textureAtlas_; diff --git a/scwx-qt/source/scwx/qt/util/texture_atlas.cpp b/scwx-qt/source/scwx/qt/util/texture_atlas.cpp index ad4502db..c6a29d00 100644 --- a/scwx-qt/source/scwx/qt/util/texture_atlas.cpp +++ b/scwx-qt/source/scwx/qt/util/texture_atlas.cpp @@ -323,10 +323,8 @@ void TextureAtlas::BuildAtlas(std::size_t width, std::size_t height) ++p->buildCount_; } -GLuint TextureAtlas::BufferAtlas(gl::OpenGLFunctions& gl) +void TextureAtlas::BufferAtlas(gl::OpenGLFunctions& gl, GLuint texture) { - GLuint texture = GL_INVALID_INDEX; - std::shared_lock lock(p->atlasMutex_); if (p->atlasArray_.size() > 0u && p->atlasArray_[0].width() > 0 && @@ -354,7 +352,6 @@ GLuint TextureAtlas::BufferAtlas(gl::OpenGLFunctions& gl) lock.unlock(); - gl.glGenTextures(1, &texture); gl.glBindTexture(GL_TEXTURE_2D_ARRAY, texture); gl.glTexParameteri( @@ -375,8 +372,6 @@ GLuint TextureAtlas::BufferAtlas(gl::OpenGLFunctions& gl) GL_UNSIGNED_BYTE, pixelData.data()); } - - return texture; } TextureAttributes TextureAtlas::GetTextureAttributes(const std::string& name) diff --git a/scwx-qt/source/scwx/qt/util/texture_atlas.hpp b/scwx-qt/source/scwx/qt/util/texture_atlas.hpp index 5cc77889..813962d2 100644 --- a/scwx-qt/source/scwx/qt/util/texture_atlas.hpp +++ b/scwx-qt/source/scwx/qt/util/texture_atlas.hpp @@ -72,10 +72,10 @@ public: std::uint64_t BuildCount() const; - void RegisterTexture(const std::string& name, const std::string& path); - bool CacheTexture(const std::string& name, const std::string& path); - void BuildAtlas(std::size_t width, std::size_t height); - GLuint BufferAtlas(gl::OpenGLFunctions& gl); + void RegisterTexture(const std::string& name, const std::string& path); + bool CacheTexture(const std::string& name, const std::string& path); + void BuildAtlas(std::size_t width, std::size_t height); + void BufferAtlas(gl::OpenGLFunctions& gl, GLuint texture); TextureAttributes GetTextureAttributes(const std::string& name);