Lugdunum  0.1.0
Device.cpp
Go to the documentation of this file.
2 
4 
5 namespace lug {
6 namespace Graphics {
7 namespace Vulkan {
8 namespace API {
9 
10 Device::Device(VkDevice device, const PhysicalDeviceInfo* physicalDeviceInfo) : _device(device), _physicalDeviceInfo(physicalDeviceInfo) {}
11 
13  _device = device._device;
14  _physicalDeviceInfo = device._physicalDeviceInfo;
15  device._device = VK_NULL_HANDLE;
16  device._physicalDeviceInfo = nullptr;
17 }
18 
20  destroy();
21 
22  _device = device._device;
23  _physicalDeviceInfo = device._physicalDeviceInfo;
24  device._device = VK_NULL_HANDLE;
25  device._physicalDeviceInfo = nullptr;
26 
27  return *this;
28 }
29 
31  destroy();
32 }
33 
34 const API::QueueFamily* Device::getQueueFamily(VkQueueFlags flags, bool supportPresentation) const {
35  const API::QueueFamily* returnQueue = nullptr;
36 
37  for (auto& queueFamily : _queueFamilies) {
38  if ((queueFamily.getFlags() & flags) == flags && (!supportPresentation || queueFamily.supportsPresentation())) {
39  if (returnQueue == nullptr || queueFamily.getFlags() == flags || (supportPresentation && flags == 0 && queueFamily.getFlags() & VK_QUEUE_GRAPHICS_BIT)) {
40  returnQueue = &queueFamily;
41  }
42  }
43  }
44 
45  if (!returnQueue && flags & VK_QUEUE_TRANSFER_BIT) {
46  return getQueueFamily((flags & ~VK_QUEUE_TRANSFER_BIT) | VK_QUEUE_GRAPHICS_BIT, supportPresentation);
47  }
48 
49  return returnQueue;
50 }
51 
52 API::QueueFamily* Device::getQueueFamily(VkQueueFlags flags, bool supportPresentation) {
53  API::QueueFamily* returnQueue = nullptr;
54 
55  for (auto& queueFamily : _queueFamilies) {
56  if ((queueFamily.getFlags() & flags) == flags && (!supportPresentation || queueFamily.supportsPresentation())) {
57  if (returnQueue == nullptr || queueFamily.getFlags() == flags || (supportPresentation && flags == 0 && queueFamily.getFlags() & VK_QUEUE_GRAPHICS_BIT)) {
58  returnQueue = &queueFamily;
59  }
60  }
61  }
62 
63  if (!returnQueue && flags & VK_QUEUE_TRANSFER_BIT) {
64  return getQueueFamily((flags & ~VK_QUEUE_TRANSFER_BIT) | VK_QUEUE_GRAPHICS_BIT, supportPresentation);
65  }
66 
67  return returnQueue;
68 }
69 
70 const API::Queue* Device::getQueue(const std::string& queueName) const {
71  for (auto& queueFamily : _queueFamilies) {
72  const API::Queue* queue = queueFamily.getQueue(queueName);
73 
74  if (queue) {
75  return queue;
76  }
77  }
78 
79  return nullptr;
80 }
81 
82 bool Device::waitIdle() const {
83  VkResult result = vkDeviceWaitIdle(_device);
84 
85  if (result != VK_SUCCESS) {
86  LUG_LOG.error("Device::waitIdle: Can't wait for queue work: {}", result);
87  return false;
88  }
89 
90  return true;
91 }
92 
94  _queueFamilies.clear();
95 
96  if (_device != VK_NULL_HANDLE) {
97  vkDeviceWaitIdle(_device);
98  vkDestroyDevice(_device, nullptr);
99  _device = VK_NULL_HANDLE;
100  }
101 }
102 
103 } // API
104 } // Vulkan
105 } // Graphics
106 } // lug
const API::QueueFamily * getQueueFamily(VkQueueFlags flags, bool supportPresentation=false) const
Definition: Device.cpp:34
const API::Queue * getQueue(const std::string &queueName) const
Definition: Device.cpp:70
#define LUG_LOG
Definition: Logger.hpp:73
const PhysicalDeviceInfo * _physicalDeviceInfo
Definition: Device.hpp:57
std::vector< QueueFamily > _queueFamilies
Definition: Device.hpp:58
Device & operator=(const Device &)=delete