Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
CommandBufferPool.cpp
Go to the documentation of this file.
1
2
4
5namespace Divide {
6namespace GFX {
7
9
10void InitPools(const size_t poolSizeFactor)
11{
13
14 g_sCommandBufferPool = new CommandBufferPool(poolSizeFactor);
15}
16
17void DestroyPools() noexcept
18{
20}
21
22CommandBufferPool::CommandBufferPool(const size_t poolSizeFactor)
23 : _poolSizeFactor( poolSizeFactor )
24{
25 _pool.resize( _poolSizeFactor, nullptr);
26 _freeList.resize( _poolSizeFactor, std::make_pair(true, U8_ZERO));
27}
28
30{
31}
32
34{
36 for (auto& it : _freeList )
37 {
38 it.first = true; // is available = true
39 ++it.second; // inc generation
40 }
41}
42
43Handle<CommandBuffer> CommandBufferPool::allocateBuffer( const char* name, const size_t reservedCmdCount )
44{
46 return allocateBufferLocked(name, reservedCmdCount);
47}
48
49Handle<CommandBuffer> CommandBufferPool::allocateBufferLocked( const char* name, size_t reservedCmdCount, bool retry )
50{
52
53 bool found = false;
54 for (auto& it : _freeList)
55 {
56 if (it.first)
57 {
58 it.first = false;
59 ret._generation = it.second;
60 CommandBuffer*& buf = _pool[ret._index];
61 if (buf)
62 {
63 buf->clear( name, reservedCmdCount );
64 }
65 else
66 {
67 buf = _memPool.newElement();
68 }
69 found = true;
70 break;
71 }
72 ++ret._index;
73 }
74
75 if (!found)
76 {
77 if (retry)
78 {
80 }
81
82 const size_t newSize = _pool.size() + _poolSizeFactor;
83 _pool.resize( newSize, nullptr );
84 _freeList.resize( newSize, std::make_pair( true, U8_ZERO ) );
85 return allocateBufferLocked(name, reservedCmdCount, true);
86 }
87
88 return ret;
89}
90
92{
93 if ( handle != INVALID_HANDLE<CommandBuffer>)
94 {
96 auto& it = _freeList[handle._index];
97 if (it.second == handle._generation)
98 {
99 it.first = true;
100 ++it.second;
101 }
102 else
103 {
104 // Pool was reset and all command buffers deallocated in bulk
105 NOP();
106 }
107
108 handle = INVALID_HANDLE<CommandBuffer>;
109 }
110}
111
113{
114 if ( handle != INVALID_HANDLE<CommandBuffer> )
115 {
117 if ( _freeList[handle._index].second == handle._generation )
118 {
119 return _pool[handle._index];
120 }
121 }
122
123 return nullptr;
124}
125
127{
129
130 return g_sCommandBufferPool->get( handle );
131}
132
133Handle<CommandBuffer> AllocateCommandBuffer(const char* name, const size_t reservedCmdCount)
134{
136
137 return g_sCommandBufferPool->allocateBuffer(name, reservedCmdCount);
138}
139
141{
143
145}
146
147}; //namespace GFX
148}; //namespace Divide
#define DIVIDE_ASSERT(...)
#define DIVIDE_UNEXPECTED_CALL()
#define NOP()
#define PROFILE_SCOPE_AUTO(CATEGORY)
Definition: Profiler.h:87
vector< CommandBuffer * > _pool
MemoryPool< CommandBuffer, prevPOW2(sizeof(CommandBuffer)) *(1u<< 3u)> _memPool
Handle< CommandBuffer > allocateBuffer(const char *name, size_t reservedCmdCount)
CommandBufferPool(size_t poolSizeFactor)
void deallocateBuffer(Handle< CommandBuffer > &handle)
Handle< CommandBuffer > allocateBufferLocked(const char *name, size_t reservedCmdCount, bool retry=false)
CommandBuffer * get(Handle< CommandBuffer > handle)
vector< std::pair< bool, U8 > > _freeList
void DestroyPools() noexcept
CommandBuffer * Get(Handle< CommandBuffer > handle)
static CommandBufferPool * g_sCommandBufferPool
void DeallocateCommandBuffer(Handle< CommandBuffer > &buffer)
Handle< CommandBuffer > AllocateCommandBuffer(const char *name, const size_t reservedCmdCount)
void InitPools(const size_t poolSizeFactor)
constexpr Optick::Category::Type Graphics
Definition: Profiler.h:60
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
std::lock_guard< mutex > LockGuard
Definition: SharedMutex.h:55
std::shared_lock< mutex > SharedLock
Definition: SharedMutex.h:49
constexpr U8 U8_ZERO