Lugdunum  0.1.0
DeviceMemory.cpp
Go to the documentation of this file.
2 
3 #include <vector>
4 
8 
9 namespace lug {
10 namespace Graphics {
11 namespace Vulkan {
12 namespace API {
13 namespace Builder {
14 
15 DeviceMemory::DeviceMemory(const API::Device& device) : _device{device} {}
16 
17 bool DeviceMemory::build(API::DeviceMemory& deviceMemory, VkResult* returnResult) {
19 
20  // Find the total size and the offset for each elements
21  VkDeviceSize size = 0;
22 
23  std::vector<VkDeviceSize> offsetBuffers(_buffers.size());
24  for (uint32_t i = 0; i < _buffers.size(); ++i) {
25  const auto& requirements = _buffers[i]->getRequirements();
26 
27  if (size % requirements.alignment) {
28  size += requirements.alignment - size % requirements.alignment;
29  }
30 
31  offsetBuffers[i] = size;
32  size += requirements.size;
33  }
34 
35  std::vector<VkDeviceSize> offsetImages(_images.size());
36  for (uint32_t i = 0; i < _images.size(); ++i) {
37  const auto& requirements = _images[i]->getRequirements();
38 
39  if (size % requirements.alignment) {
40  size += requirements.alignment - size % requirements.alignment;
41  }
42 
43  offsetImages[i] = size;
44  size += requirements.size;
45  }
46 
47  // Create the device memory creation information for vkAllocateMemory
48  const VkMemoryAllocateInfo createInfo{
49  /* createInfo.sType */ VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
50  /* createInfo.pNext */ nullptr,
51  /* createInfo.allocationSize */ size,
52  /* createInfo.memoryTypeIndex */ memoryTypeIndex
53  };
54 
55  // Create the device memory
56  VkDeviceMemory vkDeviceMemory{VK_NULL_HANDLE};
57  VkResult result = vkAllocateMemory(static_cast<VkDevice>(_device), &createInfo, nullptr, &vkDeviceMemory);
58 
59  if (returnResult) {
60  *returnResult = result;
61  }
62 
63  if (result != VK_SUCCESS) {
64  return false;
65  }
66 
67  deviceMemory = API::DeviceMemory(vkDeviceMemory, &_device, size);
68 
69  // Bind all the buffers into the memory
70  for (uint32_t i = 0; i < _buffers.size(); ++i) {
71  _buffers[i]->bindMemory(deviceMemory, offsetBuffers[i]);
72  }
73 
74  // Bind all the images into the memory
75  for (uint32_t i = 0; i < _images.size(); ++i) {
76  _images[i]->bindMemory(deviceMemory, offsetImages[i]);
77  }
78 
79  return true;
80 }
81 
82 std::unique_ptr<API::DeviceMemory> DeviceMemory::build(VkResult* returnResult) {
83  std::unique_ptr<API::DeviceMemory> buffer = std::make_unique<API::DeviceMemory>();
84  return build(*buffer, returnResult) ? std::move(buffer) : nullptr;
85 }
86 
88  _memoryTypeBits &= buffer.getRequirements().memoryTypeBits;
89 
90  if (!_memoryTypeBits) {
91  return false;
92  }
93 
94  _buffers.push_back(&buffer);
95  return true;
96 }
97 
99  _memoryTypeBits &= image.getRequirements().memoryTypeBits;
100 
101  if (!_memoryTypeBits) {
102  return false;
103  }
104 
105  _images.push_back(&image);
106  return true;
107 }
108 
109 uint32_t DeviceMemory::findMemoryType(const API::Device& device, uint32_t memoryTypeBits, VkMemoryPropertyFlags requiredFlags) {
110  const PhysicalDeviceInfo* physicalDeviceInfo = device.getPhysicalDeviceInfo();
111 
112  for (uint32_t i = 0; i < physicalDeviceInfo->memoryProperties.memoryTypeCount; i++) {
113  if (memoryTypeBits & (1 << i)) {
114  const VkMemoryType& type = physicalDeviceInfo->memoryProperties.memoryTypes[i];
115 
116  if (type.propertyFlags & requiredFlags) {
117  return i;
118  }
119  }
120  }
121 
122  return 0;
123 }
124 
125 } // Builder
126 } // API
127 } // Vulkan
128 } // Graphics
129 } // lug
VkPhysicalDeviceMemoryProperties memoryProperties
Definition: Vulkan.hpp:184
bool build(API::DeviceMemory &instance, VkResult *returnResult=nullptr)
const Renderer::Requirements requirements
Definition: Core.hpp:11
const VkMemoryRequirements & getRequirements() const
Definition: Buffer.inl:1
const VkMemoryRequirements & getRequirements() const
Definition: Image.inl:1
const PhysicalDeviceInfo * getPhysicalDeviceInfo() const
Definition: Device.inl:6
static uint32_t findMemoryType(const API::Device &device, uint32_t memoryTypeBits, VkMemoryPropertyFlags requiredFlags)
DeviceMemory(const API::Device &deviceMemory)