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() {}
void InitializeGL();
static std::size_t
GetShaderKey(std::initializer_list<std::pair<GLenum, std::string>> shaders);
gl::OpenGLFunctions gl_;
bool glInitialized_ {false};
std::unordered_map<std::size_t, std::shared_ptr<gl::ShaderProgram>>
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<gl::ShaderProgram>
GlContext::GetShaderProgram(const std::string& vertexPath,
const std::string& fragmentPath)
@ -91,15 +107,16 @@ std::shared_ptr<gl::ShaderProgram> 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_;