Changeset 502bd0b in opengl-game
- Timestamp:
- Sep 23, 2019, 2:02:47 AM (5 years ago)
- Branches:
- feature/imgui-sdl, master, points-test
- Children:
- 91c89f7
- Parents:
- c1c2021
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
vulkan-game.cpp
rc1c2021 r502bd0b 100 100 pickPhysicalDevice(deviceExtensions); 101 101 createLogicalDevice(validationLayers, deviceExtensions); 102 createSwapChain(); 102 103 } 103 104 … … 177 178 178 179 void VulkanGame::cleanupSwapChain() { 180 vkDestroySwapchainKHR(device, swapChain, nullptr); 179 181 } 180 182 … … 357 359 vkGetDeviceQueue(device, indices.presentFamily.value(), 0, &presentQueue); 358 360 } 361 362 void VulkanGame::createSwapChain() { 363 SwapChainSupportDetails swapChainSupport = VulkanUtils::querySwapChainSupport(physicalDevice, surface); 364 365 VkSurfaceFormatKHR surfaceFormat = VulkanUtils::chooseSwapSurfaceFormat(swapChainSupport.formats); 366 VkPresentModeKHR presentMode = VulkanUtils::chooseSwapPresentMode(swapChainSupport.presentModes); 367 VkExtent2D extent = VulkanUtils::chooseSwapExtent(swapChainSupport.capabilities, gui->getWindowWidth(), gui->getWindowHeight()); 368 369 uint32_t imageCount = swapChainSupport.capabilities.minImageCount + 1; 370 if (swapChainSupport.capabilities.maxImageCount > 0 && imageCount > swapChainSupport.capabilities.maxImageCount) { 371 imageCount = swapChainSupport.capabilities.maxImageCount; 372 } 373 374 VkSwapchainCreateInfoKHR createInfo = {}; 375 createInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR; 376 createInfo.surface = surface; 377 createInfo.minImageCount = imageCount; 378 createInfo.imageFormat = surfaceFormat.format; 379 createInfo.imageColorSpace = surfaceFormat.colorSpace; 380 createInfo.imageExtent = extent; 381 createInfo.imageArrayLayers = 1; 382 createInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; 383 384 QueueFamilyIndices indices = VulkanUtils::findQueueFamilies(physicalDevice, surface); 385 uint32_t queueFamilyIndices[] = { indices.graphicsFamily.value(), indices.presentFamily.value() }; 386 387 if (indices.graphicsFamily != indices.presentFamily) { 388 createInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT; 389 createInfo.queueFamilyIndexCount = 2; 390 createInfo.pQueueFamilyIndices = queueFamilyIndices; 391 } 392 else { 393 createInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE; 394 createInfo.queueFamilyIndexCount = 0; 395 createInfo.pQueueFamilyIndices = nullptr; 396 } 397 398 createInfo.preTransform = swapChainSupport.capabilities.currentTransform; 399 createInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR; 400 createInfo.presentMode = presentMode; 401 createInfo.clipped = VK_TRUE; 402 createInfo.oldSwapchain = VK_NULL_HANDLE; 403 404 if (vkCreateSwapchainKHR(device, &createInfo, nullptr, &swapChain) != VK_SUCCESS) { 405 throw runtime_error("failed to create swap chain!"); 406 } 407 408 vkGetSwapchainImagesKHR(device, swapChain, &imageCount, nullptr); 409 swapChainImages.resize(imageCount); 410 vkGetSwapchainImagesKHR(device, swapChain, &imageCount, swapChainImages.data()); 411 412 swapChainImageFormat = surfaceFormat.format; 413 swapChainExtent = extent; 414 } -
vulkan-game.hpp
rc1c2021 r502bd0b 33 33 VkQueue presentQueue; 34 34 35 VkSwapchainKHR swapChain; 36 vector<VkImage> swapChainImages; 37 VkFormat swapChainImageFormat; 38 VkExtent2D swapChainExtent; 39 35 40 bool initWindow(int width, int height, unsigned char guiFlags); 36 41 void initVulkan(); … … 49 54 const vector<const char*> validationLayers, 50 55 const vector<const char*>& deviceExtensions); 56 void createSwapChain(); 51 57 void cleanupSwapChain(); 52 58 -
vulkan-ref.cpp
rc1c2021 r502bd0b 168 168 VkQueue graphicsQueue; 169 169 VkQueue presentQueue; 170 /*** END OF REFACTORED CODE ***/171 170 172 171 VkSwapchainKHR swapChain; … … 174 173 VkFormat swapChainImageFormat; 175 174 VkExtent2D swapChainExtent; 175 /*** END OF REFACTORED CODE ***/ 176 176 vector<VkImageView> swapChainImageViews; 177 177 vector<VkFramebuffer> swapChainFramebuffers; … … 608 608 vkGetDeviceQueue(device, indices.presentFamily.value(), 0, &presentQueue); 609 609 } 610 /*** END OF REFACTORED CODE ***/611 610 612 611 void createSwapChain() { … … 663 662 } 664 663 665 /*** START OF REFACTORED CODE ***/666 664 SwapChainSupportDetails querySwapChainSupport(VkPhysicalDevice device) { 667 665 SwapChainSupportDetails details; … … 1948 1946 } 1949 1947 1948 /*** START OF REFACTORED CODE ***/ 1950 1949 vkDestroySwapchainKHR(device, swapChain, nullptr); 1950 /*** END OF REFACTORED CODE ***/ 1951 1951 1952 1952 for (size_t i = 0; i < swapChainImages.size(); i++) { -
vulkan-utils.cpp
rc1c2021 r502bd0b 125 125 return details; 126 126 } 127 128 VkSurfaceFormatKHR VulkanUtils::chooseSwapSurfaceFormat(const vector<VkSurfaceFormatKHR>& availableFormats) { 129 for (const auto& availableFormat : availableFormats) { 130 if (availableFormat.format == VK_FORMAT_B8G8R8A8_UNORM && availableFormat.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) { 131 return availableFormat; 132 } 133 } 134 135 return availableFormats[0]; 136 } 137 138 VkPresentModeKHR VulkanUtils::chooseSwapPresentMode(const vector<VkPresentModeKHR>& availablePresentModes) { 139 VkPresentModeKHR bestMode = VK_PRESENT_MODE_FIFO_KHR; 140 141 for (const auto& availablePresentMode : availablePresentModes) { 142 if (availablePresentMode == VK_PRESENT_MODE_MAILBOX_KHR) { 143 return availablePresentMode; 144 } 145 else if (availablePresentMode == VK_PRESENT_MODE_IMMEDIATE_KHR) { 146 bestMode = availablePresentMode; 147 } 148 } 149 150 return bestMode; 151 } 152 153 VkExtent2D VulkanUtils::chooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities, int width, int height) { 154 if (capabilities.currentExtent.width != numeric_limits<uint32_t>::max()) { 155 return capabilities.currentExtent; 156 } else { 157 VkExtent2D actualExtent = { 158 static_cast<uint32_t>(width), 159 static_cast<uint32_t>(height) 160 }; 161 162 actualExtent.width = std::max(capabilities.minImageExtent.width, std::min(capabilities.maxImageExtent.width, actualExtent.width)); 163 actualExtent.height = std::max(capabilities.minImageExtent.height, std::min(capabilities.maxImageExtent.height, actualExtent.height)); 164 165 return actualExtent; 166 } 167 } -
vulkan-utils.hpp
rc1c2021 r502bd0b 40 40 static bool checkDeviceExtensionSupport(VkPhysicalDevice physicalDevice, const vector<const char*>& deviceExtensions); 41 41 static SwapChainSupportDetails querySwapChainSupport(VkPhysicalDevice physicalDevice, VkSurfaceKHR surface); 42 static VkSurfaceFormatKHR chooseSwapSurfaceFormat(const vector<VkSurfaceFormatKHR>& availableFormats); 43 static VkPresentModeKHR chooseSwapPresentMode(const vector<VkPresentModeKHR>& availablePresentModes); 44 static VkExtent2D chooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities, int width, int height); 42 45 }; 43 46
Note:
See TracChangeset
for help on using the changeset viewer.