GLMemory: Allow "rewind"

This commit is contained in:
Henrik Rydgård 2023-05-23 14:41:27 +02:00
parent 05ddd77d90
commit 72df93a2f1

View file

@ -78,6 +78,9 @@ public:
void Reset() { offset_ = 0; }
// Utility for users of this class, not used internally.
const uint32_t INVALID_OFFSET = 0xFFFFFFFF;
private:
// Needs context in case of defragment.
void Begin() {
@ -105,7 +108,9 @@ public:
return writePtr_ != nullptr;
}
// Recommended - write directly into the buffer through the returned pointer.
// Recommended - lets you write directly into the buffer through the returned pointer.
// If you didn't end up using all the memory you grabbed here, then before calling Allocate or Push
// again, call Rewind (see below).
uint8_t *Allocate(uint32_t numBytes, uint32_t alignment, GLRBuffer **buf, uint32_t *bindOffset) {
uint32_t offset = ((uint32_t)offset_ + alignment - 1) & ~(alignment - 1);
if (offset + numBytes <= size_) {
@ -130,6 +135,12 @@ public:
return bindOffset;
}
// If you didn't use all of the previous allocation you just made (obviously can't be another one),
// you can return memory to the buffer by specifying the offset up until which you wrote data.
void Rewind(uint32_t offset) {
offset_ = offset;
}
size_t GetOffset() const { return offset_; }
size_t GetTotalSize() const;