mirror of
https://github.com/MobileGL-Dev/MobileGL
synced 2026-09-08 04:08:32 +09:00
[Feat] (MG_State/Buffer): separate UploadData() into Resize()+UploadData()
This commit is contained in:
@@ -66,7 +66,8 @@ namespace MobileGL {
|
||||
public:
|
||||
using TargetEnum = BufferTarget;
|
||||
|
||||
void UploadData(DataPtr data);
|
||||
void Resize(SizeT size);
|
||||
void UploadData(DataPtr data, SizeT atOffset);
|
||||
void SetUsage(BufferUsage usage);
|
||||
SizeT GetSize() const;
|
||||
BufferUsage GetUsage() const;
|
||||
@@ -82,7 +83,8 @@ namespace MobileGL {
|
||||
Data m_data;
|
||||
Bool m_isMapped;
|
||||
BufferMappingAccessBit m_mappingAccess;
|
||||
UniquePtr<Range1D> m_dirtyRange;
|
||||
// UniquePtr<Range1D> m_dirtyRange;
|
||||
Range1D m_dirtyRange;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,11 +4,21 @@ 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);
|
||||
m_dirtyRange = MakeUnique<Range1D>(0, m_size);
|
||||
m_dirtyRange->UnionUpdate(0, m_size);
|
||||
void BufferObject::Resize(SizeT size) {
|
||||
m_size = size;
|
||||
// Always reserve to power-of-2 size to amortize reallocation cost
|
||||
m_data.reserve(std::bit_ceil(size));
|
||||
m_data.resize(size);
|
||||
// no dirty range == discard all data
|
||||
m_dirtyRange.Update(0, 0);
|
||||
}
|
||||
|
||||
void BufferObject::UploadData(DataPtr data, SizeT atOffset) {
|
||||
assert(atOffset + data.size <= m_size);
|
||||
// m_size = data.size;
|
||||
memcpy(m_data.data() + atOffset, data.data, data.size);
|
||||
// m_dirtyRange = MakeUnique<Range1D>(0, m_size);
|
||||
m_dirtyRange.UnionUpdate(atOffset, atOffset + data.size - 1);
|
||||
}
|
||||
|
||||
void* BufferObject::AcquireMemory(Bool markMapped, Bool read, Bool write) {
|
||||
@@ -39,7 +49,7 @@ namespace MobileGL {
|
||||
}
|
||||
|
||||
Range1D BufferObject::GetDirtyRange() const {
|
||||
return *m_dirtyRange;
|
||||
return m_dirtyRange;
|
||||
}
|
||||
|
||||
Bool BufferObject::IsMapped() const {
|
||||
|
||||
@@ -31,4 +31,28 @@ TEST_F(BufferTest, Binding) {
|
||||
indexSlot.Bind(obj2);
|
||||
ASSERT_TRUE(arraySlot.GetBoundObject() == obj2);
|
||||
ASSERT_TRUE(indexSlot.GetBoundObject() == obj2);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(BufferTest, PingPong) {
|
||||
auto& readSlot = glContext.GetBufferBindingSlot(BufferTarget::CopyRead);
|
||||
auto& writeSlot = glContext.GetBufferBindingSlot(BufferTarget::CopyWrite);
|
||||
{
|
||||
auto bufferNames = glContext.GenBufferNames(1);
|
||||
auto bufObj = glContext.CreateBufferObject(bufferNames[0]);
|
||||
|
||||
writeSlot.Bind(bufObj);
|
||||
readSlot.Bind(bufObj);
|
||||
}
|
||||
|
||||
auto bufWrite = writeSlot.GetBoundObject();
|
||||
|
||||
Vector<Int> data { 1, 2, 3, 4, 5 };
|
||||
|
||||
bufWrite->Resize(data.size());
|
||||
DataPtr ptr { .data = data.data(), .size = data.size() };
|
||||
bufWrite->UploadData(ptr, 0);
|
||||
|
||||
auto bufRead = readSlot.GetBoundObject();
|
||||
void* p = bufRead->AcquireMemory(true, true, false);
|
||||
ASSERT_TRUE(memcmp(data.data(), p, data.size()) == 0);
|
||||
}
|
||||
|
||||
+22
-15
@@ -71,33 +71,40 @@ namespace MobileGL {
|
||||
Fixed32
|
||||
};
|
||||
|
||||
// represents a range of [start, end)
|
||||
struct Range1D {
|
||||
const SizeT maxEnd;
|
||||
const SizeT minStart;
|
||||
SizeT start;
|
||||
SizeT end;
|
||||
// const SizeT maxEnd;
|
||||
// const SizeT minStart;
|
||||
SizeT start = 0;
|
||||
SizeT end = 0;
|
||||
|
||||
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");
|
||||
}
|
||||
// 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 Update(SizeT newStart, SizeT newEnd) {
|
||||
assert(newStart <= newEnd);
|
||||
start = newStart;
|
||||
end = newEnd;
|
||||
}
|
||||
|
||||
void UnionUpdate(SizeT newStart, SizeT newEnd) {
|
||||
if (newStart < minStart || newEnd > maxEnd) {
|
||||
throw RuntimeError("Range exceeds bounds");
|
||||
}
|
||||
// if (newStart < minStart || newEnd > maxEnd) {
|
||||
// throw RuntimeError("Range exceeds bounds");
|
||||
// }
|
||||
assert(newStart <= newEnd);
|
||||
start = std::min(start, newStart);
|
||||
end = std::max(end, newEnd);
|
||||
}
|
||||
|
||||
void IntersectionUpdate(SizeT newStart, SizeT newEnd) {
|
||||
assert(newStart <= newEnd);
|
||||
start = std::max(start, newStart);
|
||||
end = std::min(end, newEnd);
|
||||
if (start > end) {
|
||||
throw RuntimeError("Invalid intersection: start must be less than end");
|
||||
}
|
||||
assert(start <= end);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user