source: opengl-game/vulkan-utils.cpp@ 90a424f

feature/imgui-sdl points-test
Last change on this file since 90a424f was 90a424f, checked in by Dmitry Portnoy <dmitry.portnoy@…>, 5 years ago

In vulkangame, add code to create a Vulkan surface and pick a physical device

  • Property mode set to 100644
File size: 4.0 KB
Line 
1#include "vulkan-utils.hpp"
2
3#include <set>
4#include <string>
5
6bool VulkanUtils::checkValidationLayerSupport(const vector<const char*> &validationLayers) {
7 uint32_t layerCount;
8 vkEnumerateInstanceLayerProperties(&layerCount, nullptr);
9
10 vector<VkLayerProperties> availableLayers(layerCount);
11 vkEnumerateInstanceLayerProperties(&layerCount, availableLayers.data());
12
13 for (const char* layerName : validationLayers) {
14 bool layerFound = false;
15
16 for (const auto& layerProperties : availableLayers) {
17 if (strcmp(layerName, layerProperties.layerName) == 0) {
18 layerFound = true;
19 break;
20 }
21 }
22
23 if (!layerFound) {
24 return false;
25 }
26 }
27
28 return true;
29}
30
31VkResult VulkanUtils::createDebugUtilsMessengerEXT(VkInstance instance,
32 const VkDebugUtilsMessengerCreateInfoEXT* pCreateInfo,
33 const VkAllocationCallbacks* pAllocator,
34 VkDebugUtilsMessengerEXT* pDebugMessenger) {
35 auto func = (PFN_vkCreateDebugUtilsMessengerEXT) vkGetInstanceProcAddr(instance, "vkCreateDebugUtilsMessengerEXT");
36
37 if (func != nullptr) {
38 return func(instance, pCreateInfo, pAllocator, pDebugMessenger);
39 } else {
40 return VK_ERROR_EXTENSION_NOT_PRESENT;
41 }
42}
43
44void VulkanUtils::destroyDebugUtilsMessengerEXT(VkInstance instance,
45 VkDebugUtilsMessengerEXT debugMessenger,
46 const VkAllocationCallbacks* pAllocator) {
47 auto func = (PFN_vkDestroyDebugUtilsMessengerEXT) vkGetInstanceProcAddr(instance, "vkDestroyDebugUtilsMessengerEXT");
48
49 if (func != nullptr) {
50 func(instance, debugMessenger, pAllocator);
51 }
52}
53
54QueueFamilyIndices VulkanUtils::findQueueFamilies(VkPhysicalDevice device, VkSurfaceKHR surface) {
55 QueueFamilyIndices indices;
56
57 uint32_t queueFamilyCount = 0;
58 vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, nullptr);
59
60 vector<VkQueueFamilyProperties> queueFamilies(queueFamilyCount);
61 vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, queueFamilies.data());
62
63 int i = 0;
64 for (const auto& queueFamily : queueFamilies) {
65 if (queueFamily.queueCount > 0 && queueFamily.queueFlags & VK_QUEUE_GRAPHICS_BIT) {
66 indices.graphicsFamily = i;
67 }
68
69 VkBool32 presentSupport = false;
70 vkGetPhysicalDeviceSurfaceSupportKHR(device, i, surface, &presentSupport);
71
72 if (queueFamily.queueCount > 0 && presentSupport) {
73 indices.presentFamily = i;
74 }
75
76 if (indices.isComplete()) {
77 break;
78 }
79
80 i++;
81 }
82
83 return indices;
84}
85
86bool VulkanUtils::checkDeviceExtensionSupport(VkPhysicalDevice device, const vector<const char*>& deviceExtensions) {
87 uint32_t extensionCount;
88 vkEnumerateDeviceExtensionProperties(device, nullptr, &extensionCount, nullptr);
89
90 vector<VkExtensionProperties> availableExtensions(extensionCount);
91 vkEnumerateDeviceExtensionProperties(device, nullptr, &extensionCount, availableExtensions.data());
92
93 set<string> requiredExtensions(deviceExtensions.begin(), deviceExtensions.end());
94
95 for (const auto& extension : availableExtensions) {
96 requiredExtensions.erase(extension.extensionName);
97 }
98
99 return requiredExtensions.empty();
100}
101
102SwapChainSupportDetails VulkanUtils::querySwapChainSupport(VkPhysicalDevice device, VkSurfaceKHR surface) {
103 SwapChainSupportDetails details;
104
105 vkGetPhysicalDeviceSurfaceCapabilitiesKHR(device, surface, &details.capabilities);
106
107 uint32_t formatCount;
108 vkGetPhysicalDeviceSurfaceFormatsKHR(device, surface, &formatCount, nullptr);
109
110 if (formatCount != 0) {
111 details.formats.resize(formatCount);
112 vkGetPhysicalDeviceSurfaceFormatsKHR(device, surface, &formatCount, details.formats.data());
113 }
114
115 uint32_t presentModeCount;
116 vkGetPhysicalDeviceSurfacePresentModesKHR(device, surface, &presentModeCount, nullptr);
117
118 if (presentModeCount != 0) {
119 details.presentModes.resize(presentModeCount);
120 vkGetPhysicalDeviceSurfacePresentModesKHR(device, surface, &presentModeCount, details.presentModes.data());
121 }
122
123 return details;
124}
Note: See TracBrowser for help on using the repository browser.