Hymn to Beauty
C++ 3D Engine
BufferAllocator.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <vector>
5#include <map>
6#include "Buffer.hpp"
7
8// There are two different strategies for temporary buffer allocation.
9// HYMN_BUFFER_STRATEGY_WHOLE:
10// Buffers are allocated of the requested size (eg. a 128B request results in a 128B buffer).
11// HYMN_BUFFER_STRATEGY_SUBALLOCATE
12// 1 MB large buffers are allocated and requests are satisfied by sub-allocating from these (eg. a 128B request results in an offset into a 1MB buffer).
13
14#define HYMN_BUFFER_STRATEGY_SUBALLOCATE 0
15#define HYMN_BUFFER_STRATEGY_WHOLE 1
16#define HYMN_BUFFER_STRATEGY HYMN_BUFFER_STRATEGY_SUBALLOCATE
17
18namespace Video {
19
21class RawBuffer {
22 public:
24 virtual ~RawBuffer() {};
25
27
32 virtual void Write(uint32_t offset, uint32_t size, const void* data) = 0;
33};
34
39
41 uint32_t offset;
42
44 uint32_t size;
45
48};
49
52 public:
54
57 explicit BufferAllocator(uint32_t frames);
58
60 virtual ~BufferAllocator();
61
63 void BeginFrame();
64
66
73 Buffer* CreateBuffer(Buffer::BufferUsage bufferUsage, uint32_t size, const void* data);
74
76
83 Buffer* CreateTemporaryBuffer(Buffer::BufferUsage bufferUsage, uint32_t size, const void* data);
84
85 protected:
87 static const uint32_t poolSize = 1024 * 1024;
88
89 private:
90 BufferAllocator(const BufferAllocator& other) = delete;
91
92 virtual uint32_t GetAlignment(Buffer::BufferUsage bufferUsage) = 0;
93
94 virtual RawBuffer* Allocate(Buffer::BufferUsage bufferUsage, bool temporary, unsigned int size) = 0;
95 BufferAllocation AllocateTemporary(Buffer::BufferUsage bufferUsage, unsigned int size);
96
97 virtual Buffer* CreateBufferObject(Buffer::BufferUsage bufferUsage, const BufferAllocation& allocation) = 0;
98 Buffer* CreateTemporaryBufferObject(Buffer::BufferUsage bufferUsage, const BufferAllocation& allocation);
99
100 uint32_t frame = 0;
101 uint32_t frames;
102
103 std::vector<Buffer*>* freeBufferObjects;
104 std::vector<Buffer*>* usedBufferObjects;
105
106 struct SubPool {
107 std::vector<RawBuffer*> rawBuffers;
108 uint32_t currentRawBuffer = 0;
109#if HYMN_BUFFER_STRATEGY == HYMN_BUFFER_STRATEGY_SUBALLOCATE
110 uint32_t currentOffset;
111#endif
112 };
113
114#if HYMN_BUFFER_STRATEGY == HYMN_BUFFER_STRATEGY_SUBALLOCATE
115 struct Pool {
116 SubPool subPools[static_cast<uint32_t>(Buffer::BufferUsage::COUNT)];
117 };
118#elif HYMN_BUFFER_STRATEGY == HYMN_BUFFER_STRATEGY_WHOLE
119 struct Pool {
120 std::map<uint32_t, SubPool> subPools[static_cast<uint32_t>(Buffer::BufferUsage::COUNT)];
121 };
122#endif
123
124 Pool* pools;
125};
126
127}
Responsible for allocating buffers.
Definition: BufferAllocator.hpp:51
Buffer * CreateTemporaryBuffer(Buffer::BufferUsage bufferUsage, uint32_t size, const void *data)
Create a temporary GPU buffer.
Definition: BufferAllocator.cpp:92
static const uint32_t poolSize
The size of each buffer in the pool.
Definition: BufferAllocator.hpp:87
virtual ~BufferAllocator()
Destructor.
Definition: BufferAllocator.cpp:24
BufferAllocator(uint32_t frames)
Create a new buffer allocator.
Definition: BufferAllocator.cpp:7
Buffer * CreateBuffer(Buffer::BufferUsage bufferUsage, uint32_t size, const void *data)
Create a GPU buffer.
Definition: BufferAllocator.cpp:73
void BeginFrame()
Call at the beginning of each frame.
Definition: BufferAllocator.cpp:53
A buffer containing GPU accessible data.
Definition: Buffer.hpp:8
BufferUsage
How the buffer is going to be used.
Definition: Buffer.hpp:11
@ COUNT
The number of different buffer usages.
A large buffer from which buffers are sub-allocated.
Definition: BufferAllocator.hpp:21
virtual void Write(uint32_t offset, uint32_t size, const void *data)=0
Write data to buffer.
virtual ~RawBuffer()
Destructor.
Definition: BufferAllocator.hpp:24
Definition: Editor.hpp:18
An allocation.
Definition: BufferAllocator.hpp:36
uint32_t offset
The offset into the buffer.
Definition: BufferAllocator.hpp:41
uint32_t size
The size of the allocation.
Definition: BufferAllocator.hpp:44
bool temporaryAllocation
Whether the allocation was temporary (buffer is not responsible for freeing memory).
Definition: BufferAllocator.hpp:47
RawBuffer * buffer
The buffer the allocation was made in.
Definition: BufferAllocator.hpp:38