SimpleGL  1.1.0
A framework for platform independent rendering
vulkan_instance.cpp
Go to the documentation of this file.
1 
9 
10 #include <stdexcept>
11 #include <vector>
12 
13 #include <GLFW/glfw3.h>
14 
15 using namespace std;
16 
17 namespace sgl {
18 
19  Instance::Instance(bool debugMode) {
20  /* Initialize the application info */
21  VkApplicationInfo appInfo = {};
22  appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
23  appInfo.pApplicationName = "SimpleGL Application (Vulkan backend)";
24  appInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0);
25  appInfo.pEngineName = "SimpleGL";
26  appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0);
27  appInfo.apiVersion = VK_API_VERSION_1_0;
28 
29  /* Add required layers */
30  vector<const char*> requiredLayers;
31  if (debugMode) {
32  requiredLayers.push_back("VK_LAYER_LUNARG_standard_validation");
33  }
34 
35  /* Get required GLFW extensions */
36  uint32_t glfwExtensionCount = 0;
37  const char** glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount);
38 
39  /* Add additional required extensions */
40  vector<const char*> requiredExtensions(glfwExtensions, glfwExtensions + glfwExtensionCount);
41  if (debugMode) {
42  requiredExtensions.push_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME);
43  }
44 
45  /* Initialize the create info */
46  VkInstanceCreateInfo createInfo = {};
47  createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
48  createInfo.pApplicationInfo = &appInfo;
49  createInfo.enabledLayerCount = requiredLayers.size();
50  createInfo.ppEnabledLayerNames = requiredLayers.data();
51  createInfo.enabledExtensionCount = requiredExtensions.size();
52  createInfo.ppEnabledExtensionNames = requiredExtensions.data();
53 
54  /* Create the Vulkan instance */
55  if (vkCreateInstance(&createInfo, nullptr, &handle) != VK_SUCCESS) {
56  throw runtime_error("Could not create Vulkan instance!");
57  }
58  }
59 
60  Instance::~Instance(void) {
61  /* Destroy Vulkan instance */
62  vkDestroyInstance(handle, nullptr);
63  }
64 
65  VkInstance Instance::getHandle() {
66  return handle;
67  }
68 
69  PFN_vkVoidFunction Instance::getProcAddr(std::string name) {
70  return vkGetInstanceProcAddr(handle, name.c_str());
71  }
72 
73  std::vector<VkPhysicalDevice> Instance::enumeratePhysicalDevices() {
74  /* Get number of available graphic cards */
75  uint32_t physicalDeviceCount = 0;
76  vkEnumeratePhysicalDevices(handle, &physicalDeviceCount, nullptr);
77  if (physicalDeviceCount == 0) {
78  throw runtime_error("Failed to find a GPU with Vulkan support!");
79  }
80 
81  /* Get all available graphic cards */
82  vector<VkPhysicalDevice> physicalDevices(physicalDeviceCount);
83  vkEnumeratePhysicalDevices(handle, &physicalDeviceCount, physicalDevices.data());
84 
85  return physicalDevices;
86  }
87 }
Generic namespace for the SimpleGL framework.
Definition: application.hpp:18
const bool debugMode
Tells if the library is compiled in debug mode.