Lugdunum  0.1.0
Chunk.inl
Go to the documentation of this file.
1 template <size_t subBufferPerChunk, size_t subBufferSize>
2 inline bool Chunk<subBufferPerChunk, subBufferSize>::init(Renderer& renderer, std::set<uint32_t> queueFamilyIndices) {
3  // Allocate the memory
4  const auto& alignment = renderer.getDevice().getPhysicalDeviceInfo()->properties.limits.minUniformBufferOffsetAlignment;
5  VkDeviceSize subBufferSizeAligned = subBufferSize;
6 
7  if (subBufferSizeAligned % alignment) {
8  subBufferSizeAligned += alignment - subBufferSizeAligned % alignment;
9  }
10 
11  // Create buffer
12  {
13  API::Builder::Buffer bufferBuilder(renderer.getDevice());
14 
15  bufferBuilder.setQueueFamilyIndices(queueFamilyIndices);
16  bufferBuilder.setSize(subBufferSizeAligned * (subBufferPerChunk - 1) + subBufferSize);
17  bufferBuilder.setUsage(VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT);
18 
19  VkResult result{VK_SUCCESS};
20  if (!bufferBuilder.build(_buffer, &result)) {
21  LUG_LOG.error("BufferPool::Chunk: Can't create buffer: {}", result);
22  return false;
23  }
24  }
25 
26  // Create buffer memory
27  {
28  API::Builder::DeviceMemory deviceMemoryBuilder(renderer.getDevice());
29  deviceMemoryBuilder.setMemoryFlags(VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
30 
31  if (!deviceMemoryBuilder.addBuffer(_buffer)) {
32  LUG_LOG.error("BufferPool::Chunk: Can't add buffer to device memory");
33  return false;
34  }
35 
36  VkResult result{VK_SUCCESS};
37  if (!deviceMemoryBuilder.build(_bufferMemory, &result)) {
38  LUG_LOG.error("BufferPool::Chunk: Can't create device memory: {}", result);
39  return false;
40  }
41  }
42 
43  // Init subBuffers
44  for (uint32_t i = 0; i < subBufferPerChunk; ++i) {
45  _subBuffers[i] = SubBuffer(
46  &_buffer,
47  static_cast<uint32_t>(subBufferSizeAligned * i),
48  static_cast<uint32_t>(subBufferSize)
49  );
50  }
51 
52  return true;
53 }
54 
55 template <size_t subBufferPerChunk, size_t subBufferSize>
57  for (auto& subBuffer : _subBuffers) {
58  if (subBuffer._referenceCount == 0) {
59  return &subBuffer;
60  }
61  }
62 
63  return nullptr;
64 }
void setQueueFamilyIndices(std::set< uint32_t > queueFamilyIndices)
Definition: Buffer.inl:17
const PhysicalDeviceInfo * getPhysicalDeviceInfo() const
Definition: Device.inl:6
void setMemoryFlags(VkMemoryPropertyFlags flags)
Definition: DeviceMemory.inl:1
#define LUG_LOG
Definition: Logger.hpp:73
VkPhysicalDeviceProperties properties
Definition: Vulkan.hpp:181