SimpleGL  1.1.0
A framework for platform independent rendering
backend_vk.cpp
Go to the documentation of this file.
1 
9 
10 #include <cstdlib>
11 #include <cstring>
12 
13 #include <vector>
14 #include <string>
15 
16 #include <vulkan/vulkan.h>
17 
18 #include <GLFW/glfw3.h>
19 
20 #include <SimpleGL/logging.hpp>
21 
22 using namespace std;
23 
24 namespace sgl {
25 
26  void initVK() {
27  Logger::logInfo("Checking Vulkan support...");
28  if (glfwVulkanSupported()) {
29  Logger::logInfo("Vulkan is supported!");
30  } else {
31  Logger::logError("Vulkan is not supported, you may want to update your graphics driver.");
32  exit(EXIT_FAILURE);
33  }
34 
35  Logger::logInfo("Checking Vulkan layers...");
36 
37  /* Get number of available layers */
38  uint32_t availableLayerCount = 0;
39  vkEnumerateInstanceLayerProperties(&availableLayerCount, nullptr);
40 
41  /* Get all available layers */
42  vector<VkLayerProperties> availableLayers(availableLayerCount);
43  vkEnumerateInstanceLayerProperties(&availableLayerCount, availableLayers.data());
44 
45  /* Add required layers */
46  vector<const char*> requiredLayers;
47  requiredLayers.push_back("VK_LAYER_LUNARG_standard_validation");
48 
49  /* Check required layers */
50  for (uint32_t i = 0; i < requiredLayers.size(); i++) {
51  bool found = false;
52  for (uint32_t j = 0; j < availableLayers.size(); j++) {
53  if (strcmp(requiredLayers[i], availableLayers[j].layerName)) {
54  found = true;
55  break;
56  }
57  }
58  if (found) {
59  Logger::logInfo(string(requiredLayers[i]) + " is supported!");
60  } else {
61  Logger::logError(string(requiredLayers[i]) + " is not supported.");
62  exit(EXIT_FAILURE);
63  }
64  }
65 
66  Logger::logInfo("Checking Vulkan extensions...");
67 
68  /* Get number of available extensions */
69  uint32_t availableExtensionCount = 0;
70  vkEnumerateInstanceExtensionProperties(nullptr, &availableExtensionCount, nullptr);
71 
72  /* Get all available extensions */
73  vector<VkExtensionProperties> availableExtensions(availableExtensionCount);
74  vkEnumerateInstanceExtensionProperties(nullptr, &availableExtensionCount, availableExtensions.data());
75 
76  /* Get required GLFW extensions */
77  uint32_t glfwExtensionCount = 0;
78  const char** glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount);
79 
80  /* Add additional required extensions */
81  vector<const char*> requiredExtensions(glfwExtensions, glfwExtensions + glfwExtensionCount);
82  requiredExtensions.push_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME);
83 
84  /* Check required extensions */
85  for (uint32_t i = 0; i < requiredExtensions.size(); i++) {
86  bool found = false;
87  for (uint32_t j = 0; j < availableExtensions.size(); j++) {
88  if (strcmp(requiredExtensions[i], availableExtensions[j].extensionName)) {
89  found = true;
90  break;
91  }
92  }
93  if (found) {
94  Logger::logInfo(string(requiredExtensions[i]) + " is supported!");
95  } else {
96  Logger::logError(string(requiredExtensions[i]) + " is not supported.");
97  exit(EXIT_FAILURE);
98  }
99  }
100  }
101 }
SIMPLEGL_VK_EXPORT void initVK()
Initializes the Vulkan backend, this will also check available extensions and should be called after ...
Definition: backend_vk.cpp:26
Generic namespace for the SimpleGL framework.
Definition: application.hpp:18