source: opengl-game/vulkan-utils.cpp@ fe5c3ba

feature/imgui-sdl points-test
Last change on this file since fe5c3ba was fe5c3ba, checked in by Dmitry Portnoy <dmp1488@…>, 5 years ago

In vulkangame, change the pickPhysicalDevice() and isDeviceSuitable() functions to take the deviceExtensions list as a parameter

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