Changeset 90a424f in opengl-game
- Timestamp:
- Sep 16, 2019, 7:04:53 PM (5 years ago)
- Branches:
- feature/imgui-sdl, master, points-test
- Children:
- fe5c3ba
- Parents:
- c6fec84
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
vulkan-game.cpp
rc6fec84 r90a424f 93 93 createVulkanInstance(validationLayers); 94 94 setupDebugMessenger(); 95 createVulkanSurface(); 96 pickPhysicalDevice(); 95 97 } 96 98 … … 230 232 return VK_FALSE; 231 233 } 234 235 void VulkanGame::createVulkanSurface() { 236 if (gui->createVulkanSurface(instance, &surface) == RTWO_ERROR) { 237 throw runtime_error("failed to create window surface!"); 238 } 239 } 240 241 void VulkanGame::pickPhysicalDevice() { 242 uint32_t deviceCount = 0; 243 vkEnumeratePhysicalDevices(instance, &deviceCount, nullptr); 244 245 if (deviceCount == 0) { 246 throw runtime_error("failed to find GPUs with Vulkan support!"); 247 } 248 249 vector<VkPhysicalDevice> devices(deviceCount); 250 vkEnumeratePhysicalDevices(instance, &deviceCount, devices.data()); 251 252 cout << endl << "Graphics cards:" << endl; 253 for (const VkPhysicalDevice& device : devices) { 254 if (isDeviceSuitable(device)) { 255 physicalDevice = device; 256 break; 257 } 258 } 259 cout << endl; 260 261 if (physicalDevice == VK_NULL_HANDLE) { 262 throw runtime_error("failed to find a suitable GPU!"); 263 } 264 } 265 266 bool VulkanGame::isDeviceSuitable(VkPhysicalDevice device) { 267 const vector<const char*> deviceExtensions = { 268 VK_KHR_SWAPCHAIN_EXTENSION_NAME 269 }; 270 271 VkPhysicalDeviceProperties deviceProperties; 272 vkGetPhysicalDeviceProperties(device, &deviceProperties); 273 274 cout << "Device: " << deviceProperties.deviceName << endl; 275 276 QueueFamilyIndices indices = VulkanUtils::findQueueFamilies(device, surface); 277 bool extensionsSupported = VulkanUtils::checkDeviceExtensionSupport(device, deviceExtensions); 278 bool swapChainAdequate = false; 279 280 if (extensionsSupported) { 281 SwapChainSupportDetails swapChainSupport = VulkanUtils::querySwapChainSupport(device, surface); 282 swapChainAdequate = !swapChainSupport.formats.empty() && !swapChainSupport.presentModes.empty(); 283 } 284 285 VkPhysicalDeviceFeatures supportedFeatures; 286 vkGetPhysicalDeviceFeatures(device, &supportedFeatures); 287 288 return indices.isComplete() && extensionsSupported && swapChainAdequate && supportedFeatures.samplerAnisotropy; 289 } -
vulkan-game.hpp
rc6fec84 r90a424f 26 26 VkInstance instance; 27 27 VkDebugUtilsMessengerEXT debugMessenger; 28 VkSurfaceKHR surface; 29 VkPhysicalDevice physicalDevice = VK_NULL_HANDLE; 28 30 29 31 bool initWindow(int width, int height, unsigned char guiFlags); … … 35 37 void setupDebugMessenger(); 36 38 void populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo); 39 void createVulkanSurface(); 40 void pickPhysicalDevice(); 41 bool isDeviceSuitable(VkPhysicalDevice device); 37 42 38 43 static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback( -
vulkan-ref.cpp
rc6fec84 r90a424f 32 32 const int MAX_FRAMES_IN_FLIGHT = 2; 33 33 34 /*** START OF REFACTORED CODE ***/ 34 35 #ifdef NDEBUG 35 36 const bool enableValidationLayers = false; … … 60 61 vector<VkPresentModeKHR> presentModes; 61 62 }; 63 /*** END OF REFACTORED CODE ***/ 62 64 63 65 struct Vertex { … … 159 161 VkInstance instance; 160 162 VkDebugUtilsMessengerEXT debugMessenger; 163 VkSurfaceKHR surface; 164 165 VkPhysicalDevice physicalDevice = VK_NULL_HANDLE; 161 166 /*** END OF REFACTORED CODE ***/ 162 VkSurfaceKHR surface;163 164 VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;165 167 VkDevice device; 166 168 … … 312 314 createInstance(); 313 315 setupDebugMessenger(); 314 /*** END OF REFACTORED CODE ***/315 316 createSurface(); 316 317 pickPhysicalDevice(); 318 /*** END OF REFACTORED CODE ***/ 317 319 createLogicalDevice(); 318 320 createSwapChain(); … … 491 493 createInfo.pfnUserCallback = debugCallback; 492 494 } 493 /*** END OF REFACTORED CODE ***/494 495 495 496 void createSurface() { … … 560 561 return requiredExtensions.empty(); 561 562 } 563 /*** END OF REFACTORED CODE ***/ 562 564 563 565 void createLogicalDevice() { … … 661 663 } 662 664 665 /*** START OF REFACTORED CODE ***/ 663 666 SwapChainSupportDetails querySwapChainSupport(VkPhysicalDevice device) { 664 667 SwapChainSupportDetails details; … … 684 687 return details; 685 688 } 689 /*** END OF REFACTORED CODE ***/ 686 690 687 691 VkSurfaceFormatKHR chooseSwapSurfaceFormat(const vector<VkSurfaceFormatKHR>& availableFormats) { … … 1034 1038 } 1035 1039 1040 /*** START OF REFACTORED CODE ***/ 1036 1041 QueueFamilyIndices findQueueFamilies(VkPhysicalDevice device) { 1037 1042 QueueFamilyIndices indices; … … 1065 1070 return indices; 1066 1071 } 1072 /*** END OF REFACTORED CODE ***/ 1067 1073 1068 1074 void createDepthResources() { -
vulkan-utils.cpp
rc6fec84 r90a424f 1 1 #include "vulkan-utils.hpp" 2 3 #include <set> 4 #include <string> 2 5 3 6 bool VulkanUtils::checkValidationLayerSupport(const vector<const char*> &validationLayers) { … … 48 51 } 49 52 } 53 54 QueueFamilyIndices 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 86 bool 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 102 SwapChainSupportDetails 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 } -
vulkan-utils.hpp
rc6fec84 r90a424f 2 2 #define _VULKAN_UTILS_H 3 3 4 #include <optional> 4 5 #include <vector> 5 6 … … 7 8 8 9 using namespace std; 10 11 struct QueueFamilyIndices { 12 optional<uint32_t> graphicsFamily; 13 optional<uint32_t> presentFamily; 14 15 bool isComplete() { 16 return graphicsFamily.has_value() && presentFamily.has_value(); 17 } 18 }; 19 20 struct SwapChainSupportDetails { 21 VkSurfaceCapabilitiesKHR capabilities; 22 vector<VkSurfaceFormatKHR> formats; 23 vector<VkPresentModeKHR> presentModes; 24 }; 9 25 10 26 class VulkanUtils { … … 20 36 VkDebugUtilsMessengerEXT debugMessenger, 21 37 const VkAllocationCallbacks* pAllocator); 38 39 static QueueFamilyIndices findQueueFamilies(VkPhysicalDevice device, VkSurfaceKHR surface); 40 static bool checkDeviceExtensionSupport(VkPhysicalDevice device, const vector<const char*>& deviceExtensions); 41 static SwapChainSupportDetails querySwapChainSupport(VkPhysicalDevice device, VkSurfaceKHR surface); 22 42 }; 23 43
Note:
See TracChangeset
for help on using the changeset viewer.