OpenGLDevice: Use stable VAO cache pointers and a default VAO

This commit is contained in:
Stenzek committed 2026-10-13 21:06:40 +10:00
1 parent 65056bdba8
commit 0e138ae200
4 files changed
+25 -19

No files matched your search

+6
View File
@@ -759,6 +759,10 @@ void OpenGLDevice::DestroyFramebuffer(GLuint fbo)
bool OpenGLDevice::CreateBuffers()
{
// Core profile has no default VAO, and the index buffer binding is VAO state, so we need one bound at all times.
glGenVertexArrays(1, &m_default_vao);
glBindVertexArray(m_default_vao);
if (!(m_vertex_buffer = OpenGLStreamBuffer::Create(GL_ARRAY_BUFFER, VERTEX_BUFFER_SIZE)) ||
!(m_index_buffer = OpenGLStreamBuffer::Create(GL_ELEMENT_ARRAY_BUFFER, INDEX_BUFFER_SIZE)) ||
!(m_uniform_buffer = OpenGLStreamBuffer::Create(GL_UNIFORM_BUFFER, UNIFORM_BUFFER_SIZE)) ||
@@ -816,6 +820,8 @@ void OpenGLDevice::DestroyBuffers()
m_uniform_buffer.reset();
m_index_buffer.reset();
m_vertex_buffer.reset();
if (m_default_vao != 0)
glDeleteVertexArrays(1, &m_default_vao);
}
GPUPresentResult OpenGLDevice::BeginPresent(GPUSwapChain* swap_chain, u32 clear_color)
+5 -4
View File
@@ -130,8 +130,8 @@ public:
void PostLinkProgram(const GPUPipeline::GraphicsConfig& plconfig, GLuint program_id);
void UnrefProgram(const OpenGLPipeline::ProgramCacheKey& key);
OpenGLPipeline::VertexArrayCache::const_iterator LookupVAOCache(const OpenGLPipeline::VertexArrayCacheKey& key,
Error* error);
const OpenGLPipeline::VertexArrayCache::value_type* LookupVAOCache(const OpenGLPipeline::VertexArrayCacheKey& key,
Error* error);
GLuint CreateVAO(std::span<const GPUPipeline::VertexAttribute> attributes, u32 stride, Error* error);
void UnrefVAO(const OpenGLPipeline::VertexArrayCacheKey& key);
@@ -207,8 +207,9 @@ private:
OpenGLPipeline::ProgramCache m_program_cache;
GPUFramebufferManager<GLuint, CreateFramebuffer, DestroyFramebuffer> m_framebuffer_manager;
// VAO cache - fixed max as key
OpenGLPipeline::VertexArrayCache::const_iterator m_last_vao = m_vao_cache.cend();
// VAO cache - fixed max as key. Pointers to elements are stable, iterators are not.
const OpenGLPipeline::VertexArrayCache::value_type* m_last_vao = nullptr;
GLuint m_default_vao = 0;
GPUPipeline::BlendState m_last_blend_state = {};
GPUPipeline::RasterizationState m_last_rasterization_state = {};
GPUPipeline::DepthState m_last_depth_state = {};
+11 -12
View File
@@ -516,14 +516,14 @@ void OpenGLDevice::UnrefProgram(const OpenGLPipeline::ProgramCacheKey& key)
m_program_cache.erase(it);
}
OpenGLPipeline::VertexArrayCache::const_iterator
const OpenGLPipeline::VertexArrayCache::value_type*
OpenGLDevice::LookupVAOCache(const OpenGLPipeline::VertexArrayCacheKey& key, Error* error)
{
OpenGLPipeline::VertexArrayCache::iterator it = m_vao_cache.find(key);
if (it != m_vao_cache.end())
{
it->second.reference_count++;
return it;
return &(*it);
}
OpenGLPipeline::VertexArrayCacheItem item;
@@ -531,10 +531,10 @@ OpenGLDevice::LookupVAOCache(const OpenGLPipeline::VertexArrayCacheKey& key, Err
CreateVAO(std::span<const GPUPipeline::VertexAttribute>(key.vertex_attributes, key.num_vertex_attributes),
key.vertex_attribute_stride, error);
if (item.vao_id == 0)
return m_vao_cache.cend();
return nullptr;
item.reference_count = 1;
return m_vao_cache.emplace(key, item).first;
return &(*m_vao_cache.emplace(key, item).first);
}
GLuint OpenGLDevice::CreateVAO(std::span<const GPUPipeline::VertexAttribute> attributes, u32 stride, Error* error)
@@ -565,8 +565,7 @@ GLuint OpenGLDevice::CreateVAO(std::span<const GPUPipeline::VertexAttribute> att
glVertexAttribPointer(i, va.components, m.type, m.normalized, stride, ptr);
}
if (m_last_vao != m_vao_cache.cend())
glBindVertexArray(m_last_vao->second.vao_id);
glBindVertexArray(m_last_vao ? m_last_vao->second.vao_id : m_default_vao);
return vao;
}
@@ -608,17 +607,17 @@ void OpenGLDevice::UnrefVAO(const OpenGLPipeline::VertexArrayCacheKey& key)
if ((--it->second.reference_count) > 0)
return;
if (m_last_vao == it)
if (m_last_vao == &(*it))
{
m_last_vao = m_vao_cache.cend();
glBindVertexArray(0);
m_last_vao = nullptr;
glBindVertexArray(m_default_vao);
}
glDeleteVertexArrays(1, &it->second.vao_id);
m_vao_cache.erase(it);
}
OpenGLPipeline::OpenGLPipeline(const ProgramCacheKey& key, GLuint program, VertexArrayCache::const_iterator vao,
OpenGLPipeline::OpenGLPipeline(const ProgramCacheKey& key, GLuint program, const VertexArrayCache::value_type* vao,
const RasterizationState& rs, const DepthState& ds, const BlendState& bs,
GLenum topology)
: m_key(key), m_vao(vao), m_program(program), m_blend_state(bs), m_rasterization_state(rs), m_depth_state(ds),
@@ -662,8 +661,8 @@ std::unique_ptr<GPUPipeline> OpenGLDevice::CreatePipeline(const GPUPipeline::Gra
if (program_id == 0)
return {};
const OpenGLPipeline::VertexArrayCache::const_iterator vao = LookupVAOCache(pkey.va_key, error);
if (vao == m_vao_cache.cend())
const OpenGLPipeline::VertexArrayCache::value_type* vao = LookupVAOCache(pkey.va_key, error);
if (!vao)
{
UnrefProgram(pkey);
return {};
+3 -3
View File
@@ -99,7 +99,7 @@ public:
~OpenGLPipeline() override;
ALWAYS_INLINE GLuint GetProgram() const { return m_program; }
ALWAYS_INLINE VertexArrayCache::const_iterator GetVAO() const { return m_vao; }
ALWAYS_INLINE const VertexArrayCache::value_type* GetVAO() const { return m_vao; }
ALWAYS_INLINE const RasterizationState& GetRasterizationState() const { return m_rasterization_state; }
ALWAYS_INLINE const DepthState& GetDepthState() const { return m_depth_state; }
ALWAYS_INLINE const BlendState& GetBlendState() const { return m_blend_state; }
@@ -110,11 +110,11 @@ public:
#endif
private:
OpenGLPipeline(const ProgramCacheKey& key, GLuint program, VertexArrayCache::const_iterator vao,
OpenGLPipeline(const ProgramCacheKey& key, GLuint program, const VertexArrayCache::value_type* vao,
const RasterizationState& rs, const DepthState& ds, const BlendState& bs, GLenum topology);
ProgramCacheKey m_key;
VertexArrayCache::const_iterator m_vao;
const VertexArrayCache::value_type* m_vao;
GLuint m_program;
BlendState m_blend_state;
RasterizationState m_rasterization_state;