Don't call glGenTextures every time the texture atlas updates

This commit is contained in:
Dan Paulat 2023-09-10 21:44:19 -05:00
parent d78e650368
commit 555fbf479a
3 changed files with 25 additions and 13 deletions

View file

@ -26,11 +26,15 @@ public:
} }
~Impl() {} ~Impl() {}
void InitializeGL();
static std::size_t static std::size_t
GetShaderKey(std::initializer_list<std::pair<GLenum, std::string>> shaders); GetShaderKey(std::initializer_list<std::pair<GLenum, std::string>> shaders);
gl::OpenGLFunctions gl_; gl::OpenGLFunctions gl_;
bool glInitialized_ {false};
std::unordered_map<std::size_t, std::shared_ptr<gl::ShaderProgram>> std::unordered_map<std::size_t, std::shared_ptr<gl::ShaderProgram>>
shaderProgramMap_; shaderProgramMap_;
std::mutex shaderProgramMutex_; std::mutex shaderProgramMutex_;
@ -57,6 +61,18 @@ std::uint64_t GlContext::texture_buffer_count() const
return p->textureBufferCount_; return p->textureBufferCount_;
} }
void GlContext::Impl::InitializeGL()
{
if (glInitialized_)
{
return;
}
gl_.glGenTextures(1, &textureAtlas_);
glInitialized_ = true;
}
std::shared_ptr<gl::ShaderProgram> std::shared_ptr<gl::ShaderProgram>
GlContext::GetShaderProgram(const std::string& vertexPath, GlContext::GetShaderProgram(const std::string& vertexPath,
const std::string& fragmentPath) const std::string& fragmentPath)
@ -91,15 +107,16 @@ std::shared_ptr<gl::ShaderProgram> GlContext::GetShaderProgram(
GLuint GlContext::GetTextureAtlas() GLuint GlContext::GetTextureAtlas()
{ {
p->InitializeGL();
std::unique_lock lock(p->textureMutex_); std::unique_lock lock(p->textureMutex_);
auto& textureAtlas = util::TextureAtlas::Instance(); auto& textureAtlas = util::TextureAtlas::Instance();
if (p->textureAtlas_ == GL_INVALID_INDEX || if (p->textureBufferCount_ != textureAtlas.BuildCount())
p->textureBufferCount_ != textureAtlas.BuildCount())
{ {
p->textureBufferCount_ = textureAtlas.BuildCount(); p->textureBufferCount_ = textureAtlas.BuildCount();
p->textureAtlas_ = textureAtlas.BufferAtlas(p->gl_); textureAtlas.BufferAtlas(p->gl_, p->textureAtlas_);
} }
return p->textureAtlas_; return p->textureAtlas_;

View file

@ -323,10 +323,8 @@ void TextureAtlas::BuildAtlas(std::size_t width, std::size_t height)
++p->buildCount_; ++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_); std::shared_lock lock(p->atlasMutex_);
if (p->atlasArray_.size() > 0u && p->atlasArray_[0].width() > 0 && if (p->atlasArray_.size() > 0u && p->atlasArray_[0].width() > 0 &&
@ -354,7 +352,6 @@ GLuint TextureAtlas::BufferAtlas(gl::OpenGLFunctions& gl)
lock.unlock(); lock.unlock();
gl.glGenTextures(1, &texture);
gl.glBindTexture(GL_TEXTURE_2D_ARRAY, texture); gl.glBindTexture(GL_TEXTURE_2D_ARRAY, texture);
gl.glTexParameteri( gl.glTexParameteri(
@ -375,8 +372,6 @@ GLuint TextureAtlas::BufferAtlas(gl::OpenGLFunctions& gl)
GL_UNSIGNED_BYTE, GL_UNSIGNED_BYTE,
pixelData.data()); pixelData.data());
} }
return texture;
} }
TextureAttributes TextureAtlas::GetTextureAttributes(const std::string& name) TextureAttributes TextureAtlas::GetTextureAttributes(const std::string& name)

View file

@ -72,10 +72,10 @@ public:
std::uint64_t BuildCount() const; std::uint64_t BuildCount() const;
void RegisterTexture(const std::string& name, const std::string& path); void RegisterTexture(const std::string& name, const std::string& path);
bool CacheTexture(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 BuildAtlas(std::size_t width, std::size_t height);
GLuint BufferAtlas(gl::OpenGLFunctions& gl); void BufferAtlas(gl::OpenGLFunctions& gl, GLuint texture);
TextureAttributes GetTextureAttributes(const std::string& name); TextureAttributes GetTextureAttributes(const std::string& name);