Lugdunum  0.1.0
BufferPool.inl
Go to the documentation of this file.
1 template <size_t subBufferPerChunk, size_t subBufferSize>
2 inline BufferPool<subBufferPerChunk, subBufferSize>::BufferPool(Renderer& renderer, std::set<uint32_t> queueFamilyIndices)
3  : _renderer(renderer), _queueFamilyIndices(queueFamilyIndices)
4 {}
5 
6 template <size_t subBufferPerChunk, size_t subBufferSize>
7 inline std::tuple<bool, const SubBuffer*> BufferPool<subBufferPerChunk, subBufferSize>::allocate(size_t hash, bool dirty) {
8  auto it = _subBuffersInUse.find(hash);
9  if (it == _subBuffersInUse.end() || dirty) {
10  SubBuffer* subBuffer = allocateNewBuffer();
11 
12  if (!subBuffer) {
13  return std::make_tuple(false, nullptr);
14  }
15 
16  subBuffer->setHash(hash);
17  _subBuffersInUse[hash] = subBuffer;
18 
19  return std::make_tuple(true, subBuffer);
20  }
21 
22  it->second->_referenceCount += 1;
23  return std::make_tuple(false, it->second);
24 }
25 
26 template <size_t subBufferPerChunk, size_t subBufferSize>
28  if (!buffer) {
29  return;
30  }
31 
32  const_cast<SubBuffer*>(buffer)->_referenceCount -= 1;
33 
34  const auto it = _subBuffersInUse.find(buffer->getHash());
35  if (buffer->_referenceCount == 0 && it != _subBuffersInUse.end() && it->second == buffer) {
36  _subBuffersInUse.erase(buffer->getHash());
37  }
38 }
39 
40 template <size_t subBufferPerChunk, size_t subBufferSize>
42  for (auto& chunk : _chunks) {
43  SubBuffer* subBuffer = chunk.getFreeSubBuffer();
44 
45  if (subBuffer) {
46  subBuffer->_referenceCount += 1;
47  return subBuffer;
48  }
49  }
50 
51  // Reallocate a new chunk
52  {
53  _chunks.emplace_back();
54 
55  if (_chunks.back().init(_renderer, _queueFamilyIndices)) {
56  return allocateNewBuffer();
57  }
58 
59  _chunks.pop_back();
60  }
61 
62  return nullptr;
63 }