From ea641249637835abbc74ded71f20000cdff1dd06 Mon Sep 17 00:00:00 2001 From: BZLZHH Date: Thu, 17 Jul 2025 13:21:28 +0800 Subject: [PATCH] [Feat] (MG_State/GLState): Implement BufferState. --- .../GLState/BufferState/BufferObject.h | 7 ++- .../GLState/BufferState/BufferState.cpp | 43 ++++++++++++++++--- .../GLState/BufferState/BufferState.h | 24 +++-------- MobileGL/MG_State/GLState/Core.cpp | 25 +++++++++++ MobileGL/MG_State/GLState/Core.h | 11 ++--- MobileGL/MG_Util/Types.h | 26 +++++++++++ 6 files changed, 105 insertions(+), 31 deletions(-) diff --git a/MobileGL/MG_State/GLState/BufferState/BufferObject.h b/MobileGL/MG_State/GLState/BufferState/BufferObject.h index a3009c68..fbab2607 100644 --- a/MobileGL/MG_State/GLState/BufferState/BufferObject.h +++ b/MobileGL/MG_State/GLState/BufferState/BufferObject.h @@ -33,6 +33,7 @@ namespace MobileGL { }; enum class BufferMappingAccessBit : Uint { + Null = 0x00, Read = 0x01, Write = 0x02, InvalidateRange = 0x04, @@ -65,13 +66,15 @@ namespace MobileGL { namespace GLState { class BufferObject { public: + using TargetEnum = BufferTarget; + void UploadData(DataPtr data); void SetUsage(BufferUsage usage); SizeT GetSize() const; BufferUsage GetUsage() const; Range1D GetDirtyRange() const; - SharedPtr AquireMemory(Bool markMapped); - SharedPtr AquireMemoryRange(Range1D range, BufferMappingAccessBit access); + void* AquireMemory(Bool markMapped, Bool read, Bool write); + void* AquireMemoryRange(Range1D range, BufferMappingAccessBit access); Bool IsMapped() const; private: diff --git a/MobileGL/MG_State/GLState/BufferState/BufferState.cpp b/MobileGL/MG_State/GLState/BufferState/BufferState.cpp index eb47c699..59914b06 100644 --- a/MobileGL/MG_State/GLState/BufferState/BufferState.cpp +++ b/MobileGL/MG_State/GLState/BufferState/BufferState.cpp @@ -3,6 +3,7 @@ namespace MobileGL { namespace MG_State { namespace GLState { + // BufferObject void BufferObject::UploadData(DataPtr data) { m_size = data.size; memcpy(m_data.data(), data.data, data.size); @@ -10,18 +11,19 @@ namespace MobileGL { m_dirtyRange->Extend(0, m_size); } - SharedPtr BufferObject::AquireMemory(Bool markMapped = true) { + void* BufferObject::AquireMemory(Bool markMapped = true, Bool read, Bool write) { if (markMapped) { m_isMapped = true; - m_mappingAccess = BufferMappingAccessBit::Read | BufferMappingAccessBit::Write; + m_mappingAccess = (read ? BufferMappingAccessBit::Read : BufferMappingAccessBit::Null) | + (write ? BufferMappingAccessBit::Write : BufferMappingAccessBit::Null); } - return MakeShared(m_data); + return m_data.data(); } - SharedPtr BufferObject::AquireMemoryRange(Range1D range, BufferMappingAccessBit access) { + void* BufferObject::AquireMemoryRange(Range1D range, BufferMappingAccessBit access) { m_isMapped = true; m_mappingAccess = access; - return MakeShared(m_data); + return m_data.data() + range.start; } void BufferObject::SetUsage(BufferUsage usage) { @@ -43,6 +45,37 @@ namespace MobileGL { Bool BufferObject::IsMapped() const { return m_isMapped; } + + // BufferState + SharedPtr BufferState::GetBufferObject(Uint index) { + auto it = m_bufferObjects.find(index); + if (it != m_bufferObjects.end()) { + return it->second; + } + return nullptr; + } + + Vector BufferState::GenerateNames(Uint number) { + Vector buffers(number); + m_indexGenerator.Generate(number, buffers.data()); + return buffers; + } + + SharedPtr BufferState::CreateBufferObject(Uint index) { + auto bufferObject = MakeShared(); + m_bufferObjects[index] = bufferObject; + return bufferObject; + } + + BindingSlot& BufferState::GetBindingSlot(BufferTarget target) { + for (auto& slot : m_bindingSlots) { + if (slot.GetTarget() == target) { + return slot; + } + } + m_bindingSlots.emplace_back(target); + return m_bindingSlots.back(); + } } } } \ No newline at end of file diff --git a/MobileGL/MG_State/GLState/BufferState/BufferState.h b/MobileGL/MG_State/GLState/BufferState/BufferState.h index 63cc3e5f..8e9b02d0 100644 --- a/MobileGL/MG_State/GLState/BufferState/BufferState.h +++ b/MobileGL/MG_State/GLState/BufferState/BufferState.h @@ -5,29 +5,15 @@ namespace MobileGL { namespace GLState { class BufferState { public: - SharedPtr GetBufferObjectByIndex(Uint index) { - auto it = m_bufferObjects.find(index); - if (it != m_bufferObjects.end()) { - return it->second; - } - return nullptr; - } - - Vector GenerateByNum(Uint number) { - Vector buffers; - m_indexGenerator.Generate(number, reinterpret_cast(&buffers)); - return buffers; - } - - SharedPtr CreateBufferObject(Uint index) { - auto bufferObject = MakeShared(); - m_bufferObjects[index] = bufferObject; - return bufferObject; - } + SharedPtr GetBufferObject(Uint index); + Vector GenerateNames(Uint number); + SharedPtr CreateBufferObject(Uint index); + BindingSlot& GetBindingSlot(BufferTarget target); private: UnorderedMap> m_bufferObjects; IndexGenerator m_indexGenerator; + Vector> m_bindingSlots; }; } } diff --git a/MobileGL/MG_State/GLState/Core.cpp b/MobileGL/MG_State/GLState/Core.cpp index 0b8af78e..69b6751d 100644 --- a/MobileGL/MG_State/GLState/Core.cpp +++ b/MobileGL/MG_State/GLState/Core.cpp @@ -1 +1,26 @@ #include "../../Includes.h" + +namespace MobileGL { + namespace MG_State { + namespace GLState { + Vector GLContext::GenBufferNames(Uint number) { + return bufferState_.GenerateNames(number); + } + + SharedPtr GLContext::GetBufferObject(Uint index) { + return bufferState_.GetBufferObject(index); + } + + BindingSlot& GLContext::GetBufferBindingSort(BufferTarget target) { + return bufferState_.GetBindingSlot(target); + } + + SharedPtr GLContext::CreateBufferObject(Uint index) { + return bufferState_.CreateBufferObject(index); + } + + } + + GLState::GLContext* pGLContext; + } +} \ No newline at end of file diff --git a/MobileGL/MG_State/GLState/Core.h b/MobileGL/MG_State/GLState/Core.h index e238b0fc..2ee4c1d7 100644 --- a/MobileGL/MG_State/GLState/Core.h +++ b/MobileGL/MG_State/GLState/Core.h @@ -8,16 +8,17 @@ namespace MobileGL { GLContext() = default; // Buffer - Vector GenBuffersByNum(Uint number); - SharedPtr GetBufferObjByIndex(Uint index); - SharedPtr GetBufferObjByTarget(BufferTarget target); - SharedPtr CreateBufferObject(Uint index); - void BindBufferTarget(BufferTarget target, SharedPtr bufferObject); + Vector GenBufferNames(Uint number); + SharedPtr GetBufferObject(Uint index); + BindingSlot& GetBufferBindingSort(BufferTarget target); + SharedPtr CreateBufferObject(Uint index); private: // Buffer BufferState bufferState_; }; } + + extern GLState::GLContext* pGLContext; } } \ No newline at end of file diff --git a/MobileGL/MG_Util/Types.h b/MobileGL/MG_Util/Types.h index c9311409..0de2928b 100644 --- a/MobileGL/MG_Util/Types.h +++ b/MobileGL/MG_Util/Types.h @@ -93,6 +93,32 @@ namespace MobileGL { } }; + template + class BindingSlot { + public: + using TargetEnum = ObjectType::TargetEnum; + + explicit BindingSlot(TargetEnum target) + : m_target(target), m_boundObject(nullptr) { + } + + void Bind(SharedPtr object) { + m_boundObject = object; + } + + SharedPtr GetBoundObject() const { + return m_boundObject; + } + + TargetEnum GetTarget() const { + return m_target; + } + + private: + TargetEnum m_target; + SharedPtr m_boundObject; + }; + struct Version { Int Major; Int Minor;