Changeset 7f60b28 in opengl-game for vulkan-utils.cpp


Ignore:
Timestamp:
Jan 24, 2021, 5:22:33 PM (4 years ago)
Author:
Dmitry Portnoy <dportnoy@…>
Branches:
feature/imgui-sdl
Children:
3f32dfd
Parents:
6a39266
Message:

Split VulkanUtils::querySwapChainSupport into three separate functions

File:
1 edited

Legend:

Unmodified
Added
Removed
  • vulkan-utils.cpp

    r6a39266 r7f60b28  
    22
    33#include <algorithm>
     4#include <cassert>
    45#include <set>
    56#include <stdexcept>
     
    78#define STB_IMAGE_IMPLEMENTATION
    89#include "stb_image.h" // TODO: Probably switch to SDL_image
    9 
    10 // TODO: Remove all instances of auto
    1110
    1211bool VulkanUtils::checkValidationLayerSupport(const vector<const char*> &validationLayers) {
     
    2019      bool layerFound = false;
    2120
    22       for (const auto& layerProperties : availableLayers) {
     21      for (const VkLayerProperties& layerProperties : availableLayers) {
    2322         if (strcmp(layerName, layerProperties.layerName) == 0) {
    2423            layerFound = true;
     
    6160
    6261// TODO: Change this to prefer one queue that supports both graphics and presentation
     62// Currently, if a queue family that supports only graphics and one that supports only presentation
     63// occur in the list before a queue family that supports both, they will be selected rather than the
     64// one that supports both
    6365QueueFamilyIndices VulkanUtils::findQueueFamilies(VkPhysicalDevice physicalDevice, VkSurfaceKHR surface) {
    6466   QueueFamilyIndices indices;
     
    7173
    7274   int i = 0;
    73    for (const auto& queueFamily : queueFamilies) {
     75   for (const VkQueueFamilyProperties& queueFamily : queueFamilies) {
    7476      if (queueFamily.queueCount > 0) {
    7577         if (queueFamily.queueFlags & VK_QUEUE_GRAPHICS_BIT) {
     
    104106   set<string> requiredExtensions(deviceExtensions.begin(), deviceExtensions.end());
    105107
    106    for (const auto& extension : availableExtensions) {
     108   for (const VkExtensionProperties& extension : availableExtensions) {
    107109      requiredExtensions.erase(extension.extensionName);
    108110   }
     
    111113}
    112114
    113 SwapChainSupportDetails VulkanUtils::querySwapChainSupport(VkPhysicalDevice physicalDevice, VkSurfaceKHR surface) {
    114    SwapChainSupportDetails details;
    115 
    116    vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physicalDevice, surface, &details.capabilities);
    117 
     115VkSurfaceCapabilitiesKHR VulkanUtils::querySwapChainCapabilities(VkPhysicalDevice physicalDevice,
     116      VkSurfaceKHR surface) {
     117   VkSurfaceCapabilitiesKHR capabilities;
     118
     119   vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physicalDevice, surface, &capabilities);
     120
     121   return capabilities;
     122}
     123
     124vector<VkSurfaceFormatKHR> VulkanUtils::querySwapChainFormats(VkPhysicalDevice physicalDevice, VkSurfaceKHR surface) {
    118125   uint32_t formatCount;
     126   vector<VkSurfaceFormatKHR> formats;
     127
    119128   vkGetPhysicalDeviceSurfaceFormatsKHR(physicalDevice, surface, &formatCount, nullptr);
    120129
    121130   if (formatCount != 0) {
    122       details.formats.resize(formatCount);
    123       vkGetPhysicalDeviceSurfaceFormatsKHR(physicalDevice, surface, &formatCount, details.formats.data());
    124    }
    125 
     131      formats.resize(formatCount);
     132      vkGetPhysicalDeviceSurfaceFormatsKHR(physicalDevice, surface, &formatCount, formats.data());
     133   }
     134
     135   return formats;
     136}
     137
     138vector<VkPresentModeKHR> VulkanUtils::querySwapChainPresentModes(VkPhysicalDevice physicalDevice,
     139      VkSurfaceKHR surface) {
    126140   uint32_t presentModeCount;
     141   vector<VkPresentModeKHR> presentModes;
     142
    127143   vkGetPhysicalDeviceSurfacePresentModesKHR(physicalDevice, surface, &presentModeCount, nullptr);
    128144
    129145   if (presentModeCount != 0) {
    130       details.presentModes.resize(presentModeCount);
    131       vkGetPhysicalDeviceSurfacePresentModesKHR(physicalDevice, surface, &presentModeCount, details.presentModes.data());
    132    }
    133 
    134    return details;
    135 }
    136 
    137 VkSurfaceFormatKHR VulkanUtils::chooseSwapSurfaceFormat(const vector<VkSurfaceFormatKHR>& availableFormats) {
    138    for (const auto& availableFormat : availableFormats) {
    139       if (availableFormat.format == VK_FORMAT_B8G8R8A8_UNORM && availableFormat.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) {
    140          return availableFormat;
     146      presentModes.resize(presentModeCount);
     147      vkGetPhysicalDeviceSurfacePresentModesKHR(physicalDevice, surface, &presentModeCount, presentModes.data());
     148   }
     149
     150   return presentModes;
     151}
     152
     153VkSurfaceFormatKHR VulkanUtils::chooseSwapSurfaceFormat(const vector<VkSurfaceFormatKHR>& availableFormats,
     154      const vector<VkFormat>& requestedFormats, VkColorSpaceKHR requestedColorSpace) {
     155   assert(requestedFormats.size() > 0);
     156
     157   if (availableFormats.size() == 1 && availableFormats[0].format == VK_FORMAT_UNDEFINED) {
     158      return { requestedFormats[0], requestedColorSpace };
     159   }
     160
     161   for (const VkFormat& requestedFormat : requestedFormats) {
     162      for (const VkSurfaceFormatKHR& availableFormat : availableFormats) {
     163         if (availableFormat.format == requestedFormat && availableFormat.colorSpace == requestedColorSpace) {
     164            return availableFormat;
     165         }
    141166      }
    142167   }
     
    145170}
    146171
    147 VkPresentModeKHR VulkanUtils::chooseSwapPresentMode(const vector<VkPresentModeKHR>& availablePresentModes) {
    148    VkPresentModeKHR bestMode = VK_PRESENT_MODE_FIFO_KHR;
    149 
    150    /* This functions effectively selects present modes in this order, which allows for unlimited framerate:
    151     * { VK_PRESENT_MODE_MAILBOX_KHR, VK_PRESENT_MODE_IMMEDIATE_KHR, VK_PRESENT_MODE_FIFO_KHR }
    152     *
    153     * To cap the framerate (I assume to the monitor refresh rate), just use:
    154     * { VK_PRESENT_MODE_FIFO_KHR }
    155     *
    156     * Would be better to make a more generic function that takes a list of prefered modes ordered by preference.
    157     * Example code:
    158     *
    159     * for (int request_i = 0; request_i < request_modes_count; request_i++)
    160     *    for (uint32_t avail_i = 0; avail_i < avail_count; avail_i++)
    161     *       if (request_modes[request_i] == avail_modes[avail_i])
    162     *          return request_modes[request_i];
    163     */
    164 
    165    for (const auto& availablePresentMode : availablePresentModes) {
    166       if (availablePresentMode == VK_PRESENT_MODE_MAILBOX_KHR) {
    167          return availablePresentMode;
    168       } else if (availablePresentMode == VK_PRESENT_MODE_IMMEDIATE_KHR) {
    169          bestMode = availablePresentMode;
    170       }
    171    }
    172 
    173    return bestMode;
     172VkPresentModeKHR VulkanUtils::chooseSwapPresentMode(const vector<VkPresentModeKHR>& availablePresentModes,
     173      const vector<VkPresentModeKHR>& requestedPresentModes) {
     174   assert(requestedPresentModes.size() > 0);
     175
     176   for (const VkPresentModeKHR& requestedPresentMode : requestedPresentModes) {
     177      for (const VkPresentModeKHR& availablePresentMode : availablePresentModes) {
     178         if (requestedPresentMode == availablePresentMode) {
     179            return requestedPresentMode;
     180         }
     181      }
     182   }
     183
     184   // If none of the requested modes are available, use VK_PRESENT_MODE_FIFO_KHR which is always available
     185   return VK_PRESENT_MODE_FIFO_KHR;
    174186}
    175187
     
    197209   viewInfo.format = format;
    198210
    199    viewInfo.components.r = VK_COMPONENT_SWIZZLE_IDENTITY;
    200    viewInfo.components.g = VK_COMPONENT_SWIZZLE_IDENTITY;
    201    viewInfo.components.b = VK_COMPONENT_SWIZZLE_IDENTITY;
    202    viewInfo.components.a = VK_COMPONENT_SWIZZLE_IDENTITY;
     211   viewInfo.components.r = VK_COMPONENT_SWIZZLE_R;
     212   viewInfo.components.g = VK_COMPONENT_SWIZZLE_G;
     213   viewInfo.components.b = VK_COMPONENT_SWIZZLE_B;
     214   viewInfo.components.a = VK_COMPONENT_SWIZZLE_A;
    203215
    204216   viewInfo.subresourceRange.aspectMask = aspectFlags;
Note: See TracChangeset for help on using the changeset viewer.