SimpleGL  1.1.0
A framework for platform independent rendering
logical_device.cpp
Go to the documentation of this file.
1 
9 
10 #include <stdexcept>
11 #include <vector>
12 #include <set>
13 
16 
17 using namespace std;
18 
19 namespace sgl {
20 
21  Device::Device(bool debugMode) {
22  PhysicalDevice* physicalDevice = VulkanContext::getCurrent()->getPhysicalDevice();
23 
24  /* Initialize the device queue create infos */
25  QueueFamilyIndices queueFamilyIndices = physicalDevice->getQueueFamilyIndices();
26  set<int32_t> queueFamilies = {queueFamilyIndices.graphics, queueFamilyIndices.presentation};
27  float queuePriority = 1.0f;
28  vector<VkDeviceQueueCreateInfo> queueCreateInfos;
29  for (set<int32_t>::iterator it = queueFamilies.begin(); it != queueFamilies.end(); it++) {
30  VkDeviceQueueCreateInfo queueCreateInfo = {};
31  queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
32  queueCreateInfo.queueFamilyIndex = *it;
33  queueCreateInfo.queueCount = 1;
34  queueCreateInfo.pQueuePriorities = &queuePriority;
35  queueCreateInfos.push_back(queueCreateInfo);
36  }
37 
38  /* Add required layers for compatibility reasons */
39  vector<const char*> requiredLayers;
40  if (debugMode) {
41  requiredLayers.push_back("VK_LAYER_LUNARG_standard_validation");
42  }
43 
44  /* Add required extensions */
45  vector<const char*> requiredExtensions;
46  requiredExtensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
47 
48  /* Initialize physical device features */
49  VkPhysicalDeviceFeatures physicalDeviceFeatures = {};
50 
51  /* Inizialize a device create info */
52  VkDeviceCreateInfo createInfo = {};
53  createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
54  createInfo.queueCreateInfoCount = queueCreateInfos.size();
55  createInfo.pQueueCreateInfos = queueCreateInfos.data();
56  createInfo.enabledLayerCount = requiredLayers.size();
57  createInfo.ppEnabledLayerNames = requiredLayers.data();
58  createInfo.enabledExtensionCount = requiredExtensions.size();
59  createInfo.ppEnabledExtensionNames = requiredExtensions.data();
60  createInfo.pEnabledFeatures = &physicalDeviceFeatures;
61 
62  /* Create the device */
63  if (vkCreateDevice(physicalDevice->getHandle(), &createInfo, nullptr, &handle) != VK_SUCCESS) {
64  throw runtime_error("Could not create a logical device!");
65  }
66 
67  /* Get device queues */
68  vkGetDeviceQueue(handle, queueFamilyIndices.graphics, 0, &queues.graphics);
69  vkGetDeviceQueue(handle, queueFamilyIndices.presentation, 0, &queues.presentation);
70  }
71 
72  Device::~Device(void) {
73  vkDestroyDevice(handle, nullptr);
74  }
75 
76  VkDevice Device::getHandle() {
77  return handle;
78  }
79 
80  Queues Device::getQueues() {
81  return queues;
82  }
83 }
This class wraps a Vulkan physical device and represents a graphics card.
int32_t presentation
The index of the presentation queue family.
This struct stores relevant device queues.
This struct stores relevant queue family indices.
Generic namespace for the SimpleGL framework.
Definition: application.hpp:18
VkPhysicalDevice getHandle()
Returns the handle of the physical device.
QueueFamilyIndices getQueueFamilyIndices()
Returns the queue family indices of the physical device.
const bool debugMode
Tells if the library is compiled in debug mode.
int32_t graphics
The index of the graphics queue family.