mirror of
https://github.com/MobileGL-Dev/MobileGL
synced 2026-09-08 12:18:30 +09:00
[Feat] (MG_State/GLState): Implement BufferObject.
This commit is contained in:
@@ -34,6 +34,9 @@ add_library(${CMAKE_PROJECT_NAME} SHARED
|
||||
MobileGL/MG_Backend/Init.cpp
|
||||
|
||||
MobileGL/MG_Util/Pipelines/Shader/GLSLtoSPIRVPipeline.cpp
|
||||
|
||||
MobileGL/MG_State/GLState/Core.cpp
|
||||
MobileGL/MG_State/GLState/BufferState/BufferState.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE
|
||||
|
||||
@@ -72,6 +72,7 @@
|
||||
#include <GL/gl.h>
|
||||
#include <GL/glext.h>
|
||||
|
||||
#include "MG_Util/Miscellany/IndexGenerator.h"
|
||||
#include "MG_Util/GLExtensions.h"
|
||||
#include "MG_Util/Types.h"
|
||||
#include "MG_Util/Debug/Log.h"
|
||||
@@ -90,3 +91,7 @@
|
||||
#include "MG_Util/Pipelines/Pipeline.hpp"
|
||||
|
||||
#include "MG_Util/Pipelines/Shader/GLSLtoSPIRVPipeline.h"
|
||||
|
||||
#include "MG_State/GLState/BufferState/BufferObject.h"
|
||||
#include "MG_State/GLState/BufferState/BufferState.h"
|
||||
#include "MG_State/GLState/Core.h"
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
#pragma once
|
||||
|
||||
namespace MobileGL {
|
||||
enum class BufferTarget {
|
||||
Vertex,
|
||||
Index,
|
||||
Uniform,
|
||||
Array,
|
||||
ElementArray,
|
||||
CopyRead,
|
||||
CopyWrite,
|
||||
PixelPack,
|
||||
PixelUnpack,
|
||||
Query,
|
||||
Texture,
|
||||
TransformFeedback,
|
||||
AtomicCounter,
|
||||
DispatchIndirect,
|
||||
DrawIndirect,
|
||||
ShaderStorage
|
||||
};
|
||||
|
||||
enum class BufferUsage {
|
||||
StreamDraw,
|
||||
StreamRead,
|
||||
StreamCopy,
|
||||
StaticDraw,
|
||||
StaticRead,
|
||||
StaticCopy,
|
||||
DynamicDraw,
|
||||
DynamicRead,
|
||||
DynamicCopy
|
||||
};
|
||||
|
||||
enum class BufferMappingAccessBit : Uint {
|
||||
Read = 0x01,
|
||||
Write = 0x02,
|
||||
InvalidateRange = 0x04,
|
||||
InvalidateBuffer = 0x08,
|
||||
FlushExplicit = 0x10,
|
||||
Unsynchronized = 0x20
|
||||
};
|
||||
|
||||
inline BufferMappingAccessBit operator|(BufferMappingAccessBit a, BufferMappingAccessBit b) {
|
||||
using T = std::underlying_type_t<BufferMappingAccessBit>;
|
||||
return static_cast<BufferMappingAccessBit>(static_cast<T>(a) | static_cast<T>(b));
|
||||
}
|
||||
|
||||
inline BufferMappingAccessBit& operator|=(BufferMappingAccessBit& a, BufferMappingAccessBit b) {
|
||||
a = a | b;
|
||||
return a;
|
||||
}
|
||||
|
||||
inline BufferMappingAccessBit operator&(BufferMappingAccessBit a, BufferMappingAccessBit b) {
|
||||
using T = std::underlying_type_t<BufferMappingAccessBit>;
|
||||
return static_cast<BufferMappingAccessBit>(static_cast<T>(a) & static_cast<T>(b));
|
||||
}
|
||||
|
||||
inline bool any(BufferMappingAccessBit a) {
|
||||
using T = std::underlying_type_t<BufferMappingAccessBit>;
|
||||
return static_cast<T>(a) != 0;
|
||||
}
|
||||
|
||||
namespace MG_State {
|
||||
namespace GLState {
|
||||
class BufferObject {
|
||||
public:
|
||||
void UploadData(DataPtr data);
|
||||
void SetUsage(BufferUsage usage);
|
||||
SizeT GetSize() const;
|
||||
BufferUsage GetUsage() const;
|
||||
Range1D GetDirtyRange() const;
|
||||
SharedPtr<Data> AquireMemory(Bool markMapped);
|
||||
SharedPtr<Data> AquireMemoryRange(Range1D range, BufferMappingAccessBit access);
|
||||
Bool IsMapped() const;
|
||||
|
||||
private:
|
||||
Int m_id = 0;
|
||||
SizeT m_size = 0;
|
||||
BufferUsage m_usage = BufferUsage::StaticDraw;
|
||||
Data m_data;
|
||||
Bool m_isMapped;
|
||||
BufferMappingAccessBit m_mappingAccess;
|
||||
UniquePtr<Range1D> m_dirtyRange;
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
#include "../../../Includes.h"
|
||||
|
||||
namespace MobileGL {
|
||||
namespace MG_State {
|
||||
namespace GLState {
|
||||
void BufferObject::UploadData(DataPtr data) {
|
||||
m_size = data.size;
|
||||
memcpy(m_data.data(), data.data, data.size);
|
||||
m_dirtyRange = MakeUnique<Range1D>(0, m_size);
|
||||
m_dirtyRange->Extend(0, m_size);
|
||||
}
|
||||
|
||||
SharedPtr<Data> BufferObject::AquireMemory(Bool markMapped = true) {
|
||||
if (markMapped) {
|
||||
m_isMapped = true;
|
||||
m_mappingAccess = BufferMappingAccessBit::Read | BufferMappingAccessBit::Write;
|
||||
}
|
||||
return MakeShared<Data>(m_data);
|
||||
}
|
||||
|
||||
SharedPtr<Data> BufferObject::AquireMemoryRange(Range1D range, BufferMappingAccessBit access) {
|
||||
m_isMapped = true;
|
||||
m_mappingAccess = access;
|
||||
return MakeShared<Data>(m_data);
|
||||
}
|
||||
|
||||
void BufferObject::SetUsage(BufferUsage usage) {
|
||||
m_usage = usage;
|
||||
}
|
||||
|
||||
SizeT BufferObject::GetSize() const {
|
||||
return m_size;
|
||||
}
|
||||
|
||||
BufferUsage BufferObject::GetUsage() const {
|
||||
return m_usage;
|
||||
}
|
||||
|
||||
Range1D BufferObject::GetDirtyRange() const {
|
||||
return *m_dirtyRange;
|
||||
}
|
||||
|
||||
Bool BufferObject::IsMapped() const {
|
||||
return m_isMapped;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
#pragma once
|
||||
|
||||
namespace MobileGL {
|
||||
namespace MG_State {
|
||||
namespace GLState {
|
||||
class BufferState {
|
||||
public:
|
||||
SharedPtr<BufferObject> GetBufferObjectByIndex(Uint index) {
|
||||
auto it = m_bufferObjects.find(index);
|
||||
if (it != m_bufferObjects.end()) {
|
||||
return it->second;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Vector<Uint> GenerateByNum(Uint number) {
|
||||
Vector<Uint> buffers;
|
||||
m_indexGenerator.Generate(number, reinterpret_cast<Uint*>(&buffers));
|
||||
return buffers;
|
||||
}
|
||||
|
||||
SharedPtr<BufferObject> CreateBufferObject(Uint index) {
|
||||
auto bufferObject = MakeShared<BufferObject>();
|
||||
m_bufferObjects[index] = bufferObject;
|
||||
return bufferObject;
|
||||
}
|
||||
|
||||
private:
|
||||
UnorderedMap<Uint, SharedPtr<BufferObject>> m_bufferObjects;
|
||||
IndexGenerator<Uint> m_indexGenerator;
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
#include "../../Includes.h"
|
||||
@@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
namespace MobileGL {
|
||||
namespace MG_State {
|
||||
namespace GLState {
|
||||
class GLContext {
|
||||
public:
|
||||
GLContext() = default;
|
||||
|
||||
// Buffer
|
||||
Vector<Uint> GenBuffersByNum(Uint number);
|
||||
SharedPtr<BufferObject> GetBufferObjByIndex(Uint index);
|
||||
SharedPtr<BufferObject> GetBufferObjByTarget(BufferTarget target);
|
||||
SharedPtr<BufferObject> CreateBufferObject(Uint index);
|
||||
void BindBufferTarget(BufferTarget target, SharedPtr<BufferObject> bufferObject);
|
||||
|
||||
private:
|
||||
// Buffer
|
||||
BufferState bufferState_;
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
#pragma once
|
||||
|
||||
namespace MobileGL {
|
||||
template <typename IndexType>
|
||||
class IndexGenerator {
|
||||
public:
|
||||
IndexGenerator() : next_index_(1) {}
|
||||
explicit IndexGenerator(IndexType n) : next_index_(1) {
|
||||
is_valid_.reserve(n);
|
||||
}
|
||||
|
||||
void Generate(size_t n, IndexType* indices) {
|
||||
if (n <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
size_t count_from_freed = std::min(n, freed_indices_.size());
|
||||
for (size_t i = 0; i < count_from_freed; ++i) {
|
||||
indices[i] = freed_indices_.back();
|
||||
freed_indices_.pop_back();
|
||||
if (indices[i] >= is_valid_.size()) {
|
||||
is_valid_.resize(static_cast<size_t>(indices[i]) + 1, false);
|
||||
}
|
||||
is_valid_[indices[i]] = true;
|
||||
}
|
||||
|
||||
for (size_t i = count_from_freed; i < n; ++i) {
|
||||
indices[i] = next_index_++;
|
||||
if (indices[i] >= is_valid_.size()) {
|
||||
is_valid_.resize(static_cast<size_t>(indices[i]) + 1, false);
|
||||
}
|
||||
is_valid_[indices[i]] = true;
|
||||
}
|
||||
}
|
||||
|
||||
void Delete(IndexType index) {
|
||||
if (index < is_valid_.size() && is_valid_[index] == true) {
|
||||
is_valid_[index] = false;
|
||||
freed_indices_.push_back(index);
|
||||
}
|
||||
}
|
||||
|
||||
bool IsValid(IndexType index) const {
|
||||
return index < is_valid_.size() && is_valid_[index] == true;
|
||||
}
|
||||
|
||||
private:
|
||||
IndexType next_index_;
|
||||
std::vector<IndexType> freed_indices_;
|
||||
std::vector<bool> is_valid_;
|
||||
};
|
||||
}
|
||||
@@ -58,6 +58,41 @@ namespace MobileGL {
|
||||
SizeT size;
|
||||
};
|
||||
|
||||
enum class DataType {
|
||||
Int8,
|
||||
Uint8,
|
||||
Int16,
|
||||
Uint16,
|
||||
Int32,
|
||||
Uint32,
|
||||
Float16,
|
||||
Float32,
|
||||
Float64,
|
||||
Fixed32
|
||||
};
|
||||
|
||||
struct Range1D {
|
||||
const SizeT maxEnd;
|
||||
const SizeT minStart;
|
||||
SizeT start;
|
||||
SizeT end;
|
||||
|
||||
Range1D(SizeT minStart_, SizeT maxEnd_)
|
||||
: minStart(minStart_), maxEnd(maxEnd_), start(minStart_), end(minStart_) {
|
||||
if (minStart_ >= maxEnd_) {
|
||||
throw RuntimeError("Invalid range: minStart must be less than maxEnd");
|
||||
}
|
||||
}
|
||||
|
||||
void Extend(SizeT newStart, SizeT newEnd) {
|
||||
if (newStart < minStart || newEnd > maxEnd) {
|
||||
throw RuntimeError("Range exceeds bounds");
|
||||
}
|
||||
start = std::min(start, newStart);
|
||||
end = std::max(end, newEnd);
|
||||
}
|
||||
};
|
||||
|
||||
struct Version {
|
||||
Int Major;
|
||||
Int Minor;
|
||||
|
||||
Reference in New Issue
Block a user