[Fix] (MG_State/BufferState, VertexArrayState): Correct IBO binding slot.

This commit is contained in:
BZLZHH
2025-08-03 18:56:50 +08:00
parent 972a4efed1
commit 3d18e702b0
13 changed files with 168 additions and 61 deletions
@@ -5,8 +5,8 @@ namespace MobileGL {
namespace GLState {
BufferState::BufferState()
: m_indexGenerator(1024, 1) {
for (SizeT i = 0; i < (SizeT)BufferTarget::BufferTargetCount; ++i) {
m_bindingSlots[i] = BindingSlot<BufferObject>((BufferTarget)i);
for (SizeT i = 0; i < m_bindingSlots.size(); ++i) {
m_bindingSlots[i] = BindingSlot<BufferObject>(GlobalBufferTargets[i]);
}
}
@@ -31,14 +31,18 @@ namespace MobileGL {
}
BindingSlot<BufferObject>& BufferState::GetBindingSlot(BufferTarget target) {
return m_bindingSlots[(SizeT)target];
for (SizeT i = 0; i < m_bindingSlots.size(); ++i) {
if (m_bindingSlots[i].GetTarget() == target) {
return m_bindingSlots[i];
}
}
}
void BufferState::MarkBufferObjectForDeletion(Uint index) {
if (m_indexGenerator.IsValid(index)) {
auto it = m_bufferObjects.find(index);
if (it != m_bufferObjects.end()) {
for (SizeT i = 0; i < (SizeT)BufferTarget::BufferTargetCount; ++i) {
for (SizeT i = 0; i < m_bindingSlots.size(); ++i) {
if (m_bindingSlots[i].GetBoundObject() == it->second) {
m_bindingSlots[i].Bind(nullptr);
}
@@ -3,6 +3,21 @@
namespace MobileGL {
namespace MG_State {
namespace GLState {
constexpr const auto GlobalBufferTargets = ToArray(
BufferTarget::Vertex,
BufferTarget::Uniform,
BufferTarget::CopyRead,
BufferTarget::CopyWrite,
BufferTarget::PixelPack,
BufferTarget::PixelUnpack,
BufferTarget::Query,
BufferTarget::Texture,
BufferTarget::TransformFeedback,
BufferTarget::AtomicCounter,
BufferTarget::DispatchIndirect,
BufferTarget::DrawIndirect,
BufferTarget::ShaderStorage);
class BufferState {
public:
BufferState();
@@ -18,7 +33,7 @@ namespace MobileGL {
private:
UnorderedMap<Uint, SharedPtr<BufferObject>> m_bufferObjects;
IndexGenerator<Uint> m_indexGenerator;
Array<BindingSlot<BufferObject>, (SizeT)BufferTarget::BufferTargetCount> m_bindingSlots;
Array<BindingSlot<BufferObject>, GlobalBufferTargets.size()> m_bindingSlots;
};
}
}
+19
View File
@@ -46,6 +46,10 @@ namespace MobileGL {
}
BindingSlot<BufferObject>& GLContext::GetBufferBindingSlot(BufferTarget target) {
if (target == BufferTarget::Index) {
return m_vertexArrayState.GetBoundVertexArray()->GetIndexBufferBindingSlot();
}
return m_bufferState.GetBindingSlot(target);
}
@@ -54,6 +58,21 @@ 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];
if (vao->GetIndexBufferBindingSlot().GetBoundObject() == bufferObject) {
vao->GetIndexBufferBindingSlot().Bind(nullptr);
}
for (SizeT j = 0; j < VertexArrayObject::MAX_VERTEX_ATTRIBS; ++j) {
if (vao->GetAttribute(j).Buffer == bufferObject) {
vao->BindAttributeBuffer(j, nullptr);
}
}
}
}
m_bufferState.MarkBufferObjectForDeletion(index);
}
@@ -53,12 +53,8 @@ namespace MobileGL {
m_attributes[index].Buffer = buffer;
}
void VertexArrayObject::BindElementBuffer(const SharedPtr<BufferObject>& buffer) {
m_elementBuffer = buffer;
}
SharedPtr<BufferObject> VertexArrayObject::GetElementBuffer() const {
return m_elementBuffer;
BindingSlot<BufferObject>& VertexArrayObject::GetIndexBufferBindingSlot() {
return m_indexBufferBindingSlot;
}
const VertexAttribute& VertexArrayObject::GetAttribute(Uint index) const {
@@ -28,14 +28,13 @@ namespace MobileGL {
void BindAttributeBuffer(Uint index, const SharedPtr<BufferObject>& buffer);
void BindElementBuffer(const SharedPtr<BufferObject>& buffer);
SharedPtr<BufferObject> GetElementBuffer() const;
BindingSlot<BufferObject>& GetIndexBufferBindingSlot();
const VertexAttribute& GetAttribute(Uint index) const;
private:
Array<VertexAttribute, MAX_VERTEX_ATTRIBS> m_attributes;
SharedPtr<BufferObject> m_elementBuffer;
BindingSlot<BufferObject> m_indexBufferBindingSlot;
};
}
}
@@ -65,6 +65,15 @@ namespace MobileGL {
SharedPtr<VertexArrayObject> VertexArrayState::GetBoundVertexArray() {
return m_boundVertexArray;
}
Vector<SharedPtr<VertexArrayObject>> VertexArrayState::GetAllVertexArrays() {
Vector<SharedPtr<VertexArrayObject>> arrays;
arrays.reserve(m_vertexArrays.size());
for (const auto& pair : m_vertexArrays) {
arrays.push_back(pair.second);
}
return arrays;
}
}
}
}
@@ -15,6 +15,7 @@ namespace MobileGL {
bool ValidateName(Uint index) const;
bool ValidateVertexArrayObject(Uint index) const;
SharedPtr<VertexArrayObject> GetBoundVertexArray();
Vector<SharedPtr<VertexArrayObject>> GetAllVertexArrays();
private:
UnorderedMap<Uint, SharedPtr<VertexArrayObject>> m_vertexArrays;