diff --git a/MobileGL/MG_State/GLState/BufferState/BufferState.cpp b/MobileGL/MG_State/GLState/BufferState/BufferState.cpp index 0dec2446..438e9c92 100644 --- a/MobileGL/MG_State/GLState/BufferState/BufferState.cpp +++ b/MobileGL/MG_State/GLState/BufferState/BufferState.cpp @@ -36,6 +36,7 @@ namespace MobileGL { return m_bindingSlots[i]; } } + assert(false); } void BufferState::MarkBufferObjectForDeletion(Uint index) { diff --git a/MobileGL/MG_State/GLState/Core.cpp b/MobileGL/MG_State/GLState/Core.cpp index 54f75164..89d36fac 100644 --- a/MobileGL/MG_State/GLState/Core.cpp +++ b/MobileGL/MG_State/GLState/Core.cpp @@ -60,8 +60,12 @@ namespace MobileGL { void GLContext::MarkBufferObjectForDeletion(Uint index) { if (ValidateBufferObject(index)) { auto bufferObject = m_bufferState.GetBufferObject(index); - for (SizeT i = 0; i < m_vertexArrayState.GetAllVertexArrays().size(); ++i) { - auto vao = m_vertexArrayState.GetAllVertexArrays()[i]; + auto& vaos = m_vertexArrayState.GetAllVertexArrays(); + for (SizeT i = 0; i < vaos.size(); ++i) { + auto vao = vaos[i]; + if (vao == nullptr) + continue; + if (vao->GetIndexBufferBindingSlot().GetBoundObject() == bufferObject) { vao->GetIndexBufferBindingSlot().Bind(nullptr); } diff --git a/MobileGL/MG_State/GLState/VertexArrayState/VertexArrayState.cpp b/MobileGL/MG_State/GLState/VertexArrayState/VertexArrayState.cpp index 2ff2500f..630f510e 100644 --- a/MobileGL/MG_State/GLState/VertexArrayState/VertexArrayState.cpp +++ b/MobileGL/MG_State/GLState/VertexArrayState/VertexArrayState.cpp @@ -8,11 +8,11 @@ namespace MobileGL { } SharedPtr VertexArrayState::GetVertexArrayObject(Uint index) { - auto it = m_vertexArrays.find(index); - if (it != m_vertexArrays.end()) { - return it->second; - } - return nullptr; + if (index >= m_vertexArrays.size()) + // FIXME: report a GL error here + return nullptr; + + return m_vertexArrays[index]; } Vector VertexArrayState::GenerateNames(Uint number) { @@ -23,35 +23,37 @@ namespace MobileGL { void VertexArrayState::Bind(Uint index) { if (index == 0) { + // FIXME: should we make a dummy VAO at index 0? m_boundVertexArray = nullptr; return; } - auto it = m_vertexArrays.find(index); - if (it != m_vertexArrays.end()) { - m_boundVertexArray = it->second; - } - else { - m_boundVertexArray = nullptr; - } + m_boundVertexArray = GetVertexArrayObject(index); } SharedPtr VertexArrayState::CreateVertexArrayObject(Uint index) { - auto vao = MakeShared(); - m_vertexArrays[index] = vao; + if (index >= m_vertexArrays.size()) { + // power-of-2 reallocation + m_vertexArrays.reserve(std::bit_ceil(index + 1)); + m_vertexArrays.resize(index + 1, nullptr); + } + auto vao = m_vertexArrays[index] = MakeShared(); return vao; } void VertexArrayState::MarkVertexArrayForDeletion(Uint index) { if (m_indexGenerator.IsValid(index)) { - if (m_boundVertexArray && m_vertexArrays.find(index) != m_vertexArrays.end() && - m_vertexArrays[index] == m_boundVertexArray) { + if (m_boundVertexArray) { m_boundVertexArray = nullptr; } - m_vertexArrays.erase(index); + if (ValidateVertexArrayObject(index)) { + m_vertexArrays[index] = nullptr; + } + m_indexGenerator.Delete(index); } + // FIXME: report GL error here? } bool VertexArrayState::ValidateName(Uint index) const { @@ -59,20 +61,15 @@ namespace MobileGL { } bool VertexArrayState::ValidateVertexArrayObject(Uint index) const { - return m_vertexArrays.find(index) != m_vertexArrays.end(); + return index < m_vertexArrays.size() && m_vertexArrays[index] != nullptr; } SharedPtr VertexArrayState::GetBoundVertexArray() { return m_boundVertexArray; } - Vector> VertexArrayState::GetAllVertexArrays() { - Vector> arrays; - arrays.reserve(m_vertexArrays.size()); - for (const auto& pair : m_vertexArrays) { - arrays.push_back(pair.second); - } - return arrays; + Vector>& VertexArrayState::GetAllVertexArrays() { + return m_vertexArrays; } } } diff --git a/MobileGL/MG_State/GLState/VertexArrayState/VertexArrayState.h b/MobileGL/MG_State/GLState/VertexArrayState/VertexArrayState.h index d6b8707a..8c6cef59 100644 --- a/MobileGL/MG_State/GLState/VertexArrayState/VertexArrayState.h +++ b/MobileGL/MG_State/GLState/VertexArrayState/VertexArrayState.h @@ -15,10 +15,10 @@ namespace MobileGL { bool ValidateName(Uint index) const; bool ValidateVertexArrayObject(Uint index) const; SharedPtr GetBoundVertexArray(); - Vector> GetAllVertexArrays(); + Vector>& GetAllVertexArrays(); private: - UnorderedMap> m_vertexArrays; + Vector> m_vertexArrays; IndexGenerator m_indexGenerator; SharedPtr m_boundVertexArray; };