[Feat|Fix|Improvement] (All): Continue building the basic structure.

Feat: implement MG_Impl/Buffer
Feat: implement MG_State/ErrorState and integrate it into MG_State/GLContext
Feat: add buffer deletion management
Feat: introduce `INSERTION_POINT` (https://github.com/MobileGL-Dev/MobileGLCodeManager)
Feat: add tests about MG_Impl/Buffer and buffer deletion feature
Feat: add several converters
Fix: fix some compiling errors
Fix: fix some incorrect behaviors in MG_State/Buffer
Improvement: remove some current statements at the beginning of the source file, which will be added uniformly soon
Improvement: optimize some naming
This commit is contained in:
BZLZHH
2025-07-23 23:04:36 +08:00
parent 06804d94f8
commit e79a2fa809
37 changed files with 1377 additions and 118 deletions
@@ -16,7 +16,8 @@ namespace MobileGL {
DispatchIndirect,
DrawIndirect,
ShaderStorage,
BufferTargetCount
BufferTargetCount,
Unknown = -1
};
enum class BufferUsage {
@@ -28,7 +29,8 @@ namespace MobileGL {
StaticCopy,
DynamicDraw,
DynamicRead,
DynamicCopy
DynamicCopy,
Unknown = -1
};
enum class BufferMappingAccessBit : Uint {
@@ -38,7 +40,9 @@ namespace MobileGL {
InvalidateRange = 0x04,
InvalidateBuffer = 0x08,
FlushExplicit = 0x10,
Unsynchronized = 0x20
Unsynchronized = 0x20,
Persistent = 0x40,
Coherent = 0x80
};
inline BufferMappingAccessBit operator|(BufferMappingAccessBit a, BufferMappingAccessBit b) {
@@ -56,7 +60,7 @@ namespace MobileGL {
return static_cast<BufferMappingAccessBit>(static_cast<T>(a) & static_cast<T>(b));
}
inline bool any(BufferMappingAccessBit a) {
inline bool Any(BufferMappingAccessBit a) {
using T = std::underlying_type_t<BufferMappingAccessBit>;
return static_cast<T>(a) != 0;
}
@@ -76,7 +80,7 @@ namespace MobileGL {
void* AcquireMemoryRange(Range1D range, BufferMappingAccessBit access);
void ReleaseMemory();
void FlushMemoryRange(SizeT offset, SizeT length);
void UploadSubData(SizeT offset, SizeT size, const void* data);
void UploadSubData(DataPtr data, SizeT atOffset);
void CopyDataFrom(const SharedPtr<BufferObject>& src, SizeT srcOffset, SizeT dstOffset, SizeT size);
void ClearDirty();
@@ -84,7 +88,8 @@ namespace MobileGL {
SizeT GetSize() const;
BufferUsage GetUsage() const;
Range1D GetDirtyRange() const;
Range1D GetMappedRange() const;
BufferMappingAccessBit GetMappingAccess() const;
private:
Int m_id = 0;
SizeT m_size = 0;
@@ -31,8 +31,8 @@ namespace MobileGL {
void BufferObject::ReleaseMemory() {
if (!m_isMapped) return;
if (any(m_mappingAccess & BufferMappingAccessBit::Write)) { // if we wrote to the buffer
if (!any(m_mappingAccess & BufferMappingAccessBit::FlushExplicit)) { // if we didn't flush explicitly
if (Any(m_mappingAccess & BufferMappingAccessBit::Write)) { // if we wrote to the buffer
if (!Any(m_mappingAccess & BufferMappingAccessBit::FlushExplicit)) { // if we didn't flush explicitly
memcpy(m_data.data() + m_mappedRange.start,
m_stagingData.data(),
m_mappedRange.end - m_mappedRange.start);
@@ -50,8 +50,8 @@ namespace MobileGL {
void BufferObject::FlushMemoryRange(SizeT offset, SizeT length) {
assert(m_isMapped);
assert(any(m_mappingAccess & BufferMappingAccessBit::FlushExplicit));
assert(any(m_mappingAccess & BufferMappingAccessBit::Write));
assert(Any(m_mappingAccess & BufferMappingAccessBit::FlushExplicit));
assert(Any(m_mappingAccess & BufferMappingAccessBit::Write));
SizeT start = m_mappedRange.start + offset;
SizeT end = start + length;
@@ -61,12 +61,12 @@ namespace MobileGL {
m_dirtyRange.UnionUpdate(start, end);
}
void BufferObject::UploadSubData(SizeT offset, SizeT size, const void* data) {
void BufferObject::UploadSubData(DataPtr data, SizeT atOffset) {
assert(!m_isMapped);
assert(offset + size <= m_size);
assert(atOffset + data.size <= m_size);
memcpy(m_data.data() + offset, data, size);
m_dirtyRange.UnionUpdate(offset, offset + size);
memcpy(m_data.data() + atOffset, data.data, data.size);
m_dirtyRange.UnionUpdate(atOffset, atOffset + data.size);
}
void BufferObject::CopyDataFrom(const SharedPtr<BufferObject>& src, SizeT srcOffset, SizeT dstOffset, SizeT size) {
@@ -88,11 +88,11 @@ namespace MobileGL {
(write ? BufferMappingAccessBit::Write : BufferMappingAccessBit::Null);
m_mappedRange = { 0, m_size };
if (any(m_mappingAccess & BufferMappingAccessBit::Write)) {
if (Any(m_mappingAccess & BufferMappingAccessBit::Write)) {
m_stagingData.resize(m_size);
m_ownsStagingData = true;
if (!any(m_mappingAccess & (BufferMappingAccessBit::InvalidateRange |
if (!Any(m_mappingAccess & (BufferMappingAccessBit::InvalidateRange |
BufferMappingAccessBit::InvalidateBuffer))) {
memcpy(m_stagingData.data(), m_data.data(), m_size);
}
@@ -110,11 +110,11 @@ namespace MobileGL {
m_mappingAccess = access;
m_mappedRange = range;
if (any(access & BufferMappingAccessBit::Write)) {
if (Any(access & BufferMappingAccessBit::Write)) {
m_stagingData.resize(range.end - range.start);
m_ownsStagingData = true;
if (!any(access & (BufferMappingAccessBit::InvalidateRange |
if (!Any(access & (BufferMappingAccessBit::InvalidateRange |
BufferMappingAccessBit::InvalidateBuffer))) {
memcpy(m_stagingData.data(), m_data.data() + range.start, m_stagingData.size());
}
@@ -147,7 +147,22 @@ namespace MobileGL {
return m_isMapped;
}
Range1D BufferObject::GetMappedRange() const {
return m_isMapped ? m_mappedRange : Range1D{ 0, 0 };
}
BufferMappingAccessBit BufferObject::GetMappingAccess() const {
return m_isMapped ? m_mappingAccess : BufferMappingAccessBit::Null;
}
// BufferState
BufferState::BufferState()
: m_indexGenerator(1024, 1) {
for (SizeT i = 0; i < (SizeT)BufferTarget::BufferTargetCount; ++i) {
m_bindingSlots[i] = BindingSlot<BufferObject>((BufferTarget)i);
}
}
SharedPtr<BufferObject> BufferState::GetBufferObject(Uint index) {
auto it = m_bufferObjects.find(index);
if (it != m_bufferObjects.end()) {
@@ -171,6 +186,29 @@ namespace MobileGL {
BindingSlot<BufferObject>& BufferState::GetBindingSlot(BufferTarget target) {
return m_bindingSlots[(SizeT)target];
}
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) {
if (m_bindingSlots[i].GetBoundObject() == it->second) {
m_bindingSlots[i].Bind(nullptr);
}
}
m_bufferObjects.erase(it);
}
m_indexGenerator.Delete(index);
}
}
bool BufferState::ValidateName(Uint index) const {
return m_indexGenerator.IsValid(index);
}
bool BufferState::ValidateBufferObject(Uint index) const {
return m_bufferObjects.find(index) != m_bufferObjects.end();
}
}
}
}
@@ -5,16 +5,15 @@ namespace MobileGL {
namespace GLState {
class BufferState {
public:
BufferState() {
for (SizeT i = 0; i < (SizeT)BufferTarget::BufferTargetCount; ++i) {
m_bindingSlots[i] = BindingSlot<BufferObject>((BufferTarget)i);
}
}
BufferState();
SharedPtr<BufferObject> GetBufferObject(Uint index);
Vector<Uint> GenerateNames(Uint number);
SharedPtr<BufferObject> CreateBufferObject(Uint index);
BindingSlot<BufferObject>& GetBindingSlot(BufferTarget target);
void MarkBufferObjectForDeletion(Uint index);
bool ValidateName(Uint index) const;
bool ValidateBufferObject(Uint index) const;
private:
UnorderedMap<Uint, SharedPtr<BufferObject>> m_bufferObjects;
+50 -4
View File
@@ -3,20 +3,66 @@
namespace MobileGL {
namespace MG_State {
namespace GLState {
// Error
void GLContext::RecordError(ErrorCode code, SharedPtr<ErrorInfo> info) {
m_errorState.RecordError(code, info);
}
bool GLContext::HasGLError() const {
return m_errorState.HasGLError();
}
Optional<const Error> GLContext::PeekGLError() const {
return m_errorState.PeekGLError();
}
Optional<Error> GLContext::PopGLError() {
return m_errorState.PopGLError();
}
bool GLContext::HasNonGLError() const {
return m_errorState.HasNonGLError();
}
Optional<const Error> GLContext::PeekNonGLError() const {
return m_errorState.PeekNonGLError();
}
Optional<Error> GLContext::PopNonGLError() {
return m_errorState.PopNonGLError();
}
void GLContext::ClearErrors() {
m_errorState.Clear();
}
// Buffer
Vector<Uint> GLContext::GenBufferNames(Uint number) {
return bufferState_.GenerateNames(number);
return m_bufferState.GenerateNames(number);
}
SharedPtr<BufferObject> GLContext::GetBufferObject(Uint index) {
return bufferState_.GetBufferObject(index);
return m_bufferState.GetBufferObject(index);
}
BindingSlot<BufferObject>& GLContext::GetBufferBindingSlot(BufferTarget target) {
return bufferState_.GetBindingSlot(target);
return m_bufferState.GetBindingSlot(target);
}
SharedPtr<BufferObject> GLContext::CreateBufferObject(Uint index) {
return bufferState_.CreateBufferObject(index);
return m_bufferState.CreateBufferObject(index);
}
void GLContext::MarkBufferObjectForDeletion(Uint index) {
m_bufferState.MarkBufferObjectForDeletion(index);
}
bool GLContext::ValidateBufferName(Uint index) const {
return m_bufferState.ValidateName(index);
}
bool GLContext::ValidateBufferObject(Uint index) const {
return m_bufferState.ValidateBufferObject(index);
}
}
+18 -2
View File
@@ -7,15 +7,31 @@ namespace MobileGL {
public:
GLContext() = default;
// Error
void RecordError(ErrorCode code, SharedPtr<ErrorInfo> info = nullptr);
bool HasGLError() const;
Optional<const Error> PeekGLError() const;
Optional<Error> PopGLError();
bool HasNonGLError() const;
Optional<const Error> PeekNonGLError() const;
Optional<Error> PopNonGLError();
void ClearErrors();
// Buffer
Vector<Uint> GenBufferNames(Uint number);
SharedPtr<BufferObject> GetBufferObject(Uint index);
BindingSlot<BufferObject>& GetBufferBindingSlot(BufferTarget target);
SharedPtr<BufferObject> CreateBufferObject(Uint index);
SharedPtr<BufferObject> CreateBufferObject(Uint index);
void MarkBufferObjectForDeletion(Uint index);
bool ValidateBufferName(Uint index) const;
bool ValidateBufferObject(Uint index) const;
private:
// Error
ErrorState m_errorState;
// Buffer
BufferState bufferState_;
BufferState m_bufferState;
};
}
@@ -0,0 +1,58 @@
#include "../../../Includes.h"
namespace MobileGL {
namespace MG_State {
namespace GLState {
void ErrorState::RecordError(ErrorCode code, SharedPtr<ErrorInfo> info) {
if (code == ErrorCode::NoError) {
MGLOG_E("Recording Non-OpenGL error:\n%s",
info->ToString().c_str());
m_nonGLErrors.push_back(Error{ code, info });
}
else {
MGLOG_E("Recording OpenGL error (%s):\n%s",
MG_Util::ConvertGLEnumToString(MG_Util::ConvertErrorCodeToGLEnum(code)).c_str(),
info->ToString().c_str());
m_errors.push_back(Error{ code, info });
}
}
bool ErrorState::HasNonGLError() const {
return !m_nonGLErrors.empty();
}
Optional<const Error> ErrorState::PeekNonGLError() const {
if (m_nonGLErrors.empty()) return Optional<const Error>{ };
return Optional<const Error>{m_nonGLErrors.front()};
}
Optional<Error> ErrorState::PopNonGLError() {
if (m_nonGLErrors.empty()) return Optional<const Error>{ };
auto error = Move(m_nonGLErrors.front());
m_nonGLErrors.erase(m_nonGLErrors.begin());
return Optional<const Error>{ error };
}
bool ErrorState::HasGLError() const {
return !m_errors.empty();
}
Optional<const Error> ErrorState::PeekGLError() const {
if (m_errors.empty()) return Optional<const Error>{ };
return Optional<const Error>{m_errors.front()};
}
Optional<Error> ErrorState::PopGLError() {
if (m_errors.empty()) return Optional<const Error>{ };
auto error = Move(m_errors.front());
m_errors.erase(m_errors.begin());
return Optional<const Error>{ error };
}
void ErrorState::Clear() {
m_errors.clear();
m_nonGLErrors.clear();
}
}
}
}
@@ -0,0 +1,28 @@
#pragma once
namespace MobileGL {
struct Error {
ErrorCode code;
SharedPtr<ErrorInfo> info;
};
namespace MG_State {
namespace GLState {
class ErrorState {
public:
void RecordError(ErrorCode code, SharedPtr<ErrorInfo> info = nullptr);
bool HasNonGLError() const;
Optional<const Error> PeekNonGLError() const;
Optional<Error> PopNonGLError();
bool HasGLError() const;
Optional<const Error> PeekGLError() const;
Optional<Error> PopGLError();
void Clear();
private:
Vector<Error> m_errors;
Vector<Error> m_nonGLErrors;
};
}
}
}
@@ -0,0 +1,14 @@
#pragma once
namespace MobileGL {
enum class ErrorCode {
NoError = 0,
InvalidEnum,
InvalidValue,
InvalidOperation,
InvalidFramebufferOperation,
OutOfMemory,
StackUnderflow,
StackOverflow
};
}
@@ -0,0 +1,32 @@
#pragma once
namespace MobileGL {
class ErrorInfo {
public:
virtual ~ErrorInfo() = default;
virtual String ToString() const = 0;
};
class GenericErrorInfo : public ErrorInfo {
public:
explicit GenericErrorInfo(String message) : m_message(Move(message)) {}
explicit GenericErrorInfo(String m_prefix, String message) : m_message(Move(message)), m_prefix(Move(m_prefix)) {}
explicit GenericErrorInfo(String m_prefix, String m_prefix_2, String message) : m_message(Move(message)), m_prefix(Move(m_prefix)), m_prefix_2(Move(m_prefix_2)) {}
String ToString() const override {
StringStream ss;
if (m_prefix.has_value()) {
ss << "[" << m_prefix.value() << "] ";
}
if (m_prefix_2.has_value()) {
ss << "[" << m_prefix_2.value() << "] ";
}
ss << m_message;
return ss.str();
}
private:
String m_message;
Optional<String> m_prefix;
Optional<String> m_prefix_2;
};
}