Changeset 7f60b28 in opengl-game
- Timestamp:
- Jan 24, 2021, 5:22:33 PM (4 years ago)
- Branches:
- feature/imgui-sdl
- Children:
- 3f32dfd
- Parents:
- 6a39266
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
main-vulkan.cpp
r6a39266 r7f60b28 19 19 20 20 int __main(int argc, char* argv[]) { 21 const int MAX_FRAMES_IN_FLIGHT = 2;22 23 21 cout << "Starting Vulkan Game..." << endl; 24 22 25 VulkanGame game (MAX_FRAMES_IN_FLIGHT);23 VulkanGame game; 26 24 27 25 try { -
vulkan-utils.cpp
r6a39266 r7f60b28 2 2 3 3 #include <algorithm> 4 #include <cassert> 4 5 #include <set> 5 6 #include <stdexcept> … … 7 8 #define STB_IMAGE_IMPLEMENTATION 8 9 #include "stb_image.h" // TODO: Probably switch to SDL_image 9 10 // TODO: Remove all instances of auto11 10 12 11 bool VulkanUtils::checkValidationLayerSupport(const vector<const char*> &validationLayers) { … … 20 19 bool layerFound = false; 21 20 22 for (const auto& layerProperties : availableLayers) {21 for (const VkLayerProperties& layerProperties : availableLayers) { 23 22 if (strcmp(layerName, layerProperties.layerName) == 0) { 24 23 layerFound = true; … … 61 60 62 61 // 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 63 65 QueueFamilyIndices VulkanUtils::findQueueFamilies(VkPhysicalDevice physicalDevice, VkSurfaceKHR surface) { 64 66 QueueFamilyIndices indices; … … 71 73 72 74 int i = 0; 73 for (const auto& queueFamily : queueFamilies) {75 for (const VkQueueFamilyProperties& queueFamily : queueFamilies) { 74 76 if (queueFamily.queueCount > 0) { 75 77 if (queueFamily.queueFlags & VK_QUEUE_GRAPHICS_BIT) { … … 104 106 set<string> requiredExtensions(deviceExtensions.begin(), deviceExtensions.end()); 105 107 106 for (const auto& extension : availableExtensions) {108 for (const VkExtensionProperties& extension : availableExtensions) { 107 109 requiredExtensions.erase(extension.extensionName); 108 110 } … … 111 113 } 112 114 113 SwapChainSupportDetails VulkanUtils::querySwapChainSupport(VkPhysicalDevice physicalDevice, VkSurfaceKHR surface) { 114 SwapChainSupportDetails details; 115 116 vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physicalDevice, surface, &details.capabilities); 117 115 VkSurfaceCapabilitiesKHR VulkanUtils::querySwapChainCapabilities(VkPhysicalDevice physicalDevice, 116 VkSurfaceKHR surface) { 117 VkSurfaceCapabilitiesKHR capabilities; 118 119 vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physicalDevice, surface, &capabilities); 120 121 return capabilities; 122 } 123 124 vector<VkSurfaceFormatKHR> VulkanUtils::querySwapChainFormats(VkPhysicalDevice physicalDevice, VkSurfaceKHR surface) { 118 125 uint32_t formatCount; 126 vector<VkSurfaceFormatKHR> formats; 127 119 128 vkGetPhysicalDeviceSurfaceFormatsKHR(physicalDevice, surface, &formatCount, nullptr); 120 129 121 130 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 138 vector<VkPresentModeKHR> VulkanUtils::querySwapChainPresentModes(VkPhysicalDevice physicalDevice, 139 VkSurfaceKHR surface) { 126 140 uint32_t presentModeCount; 141 vector<VkPresentModeKHR> presentModes; 142 127 143 vkGetPhysicalDeviceSurfacePresentModesKHR(physicalDevice, surface, &presentModeCount, nullptr); 128 144 129 145 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 153 VkSurfaceFormatKHR 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 } 141 166 } 142 167 } … … 145 170 } 146 171 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; 172 VkPresentModeKHR 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; 174 186 } 175 187 … … 197 209 viewInfo.format = format; 198 210 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; 203 215 204 216 viewInfo.subresourceRange.aspectMask = aspectFlags; -
vulkan-utils.hpp
r6a39266 r7f60b28 8 8 #include <vulkan/vulkan.h> 9 9 10 // TODO: Ideally, vulkan-utils should not have things speciic to windowing apis (glfw, sdl, sfml, etc.). 11 // Check what these inclydes are for and if that functionality can be moved 10 12 #include <SDL2/SDL.h> 11 13 #include <SDL2/SDL_vulkan.h> … … 20 22 return graphicsFamily.has_value() && presentFamily.has_value(); 21 23 } 22 };23 24 struct SwapChainSupportDetails {25 VkSurfaceCapabilitiesKHR capabilities;26 vector<VkSurfaceFormatKHR> formats;27 vector<VkPresentModeKHR> presentModes;28 24 }; 29 25 … … 50 46 static bool checkDeviceExtensionSupport(VkPhysicalDevice physicalDevice, 51 47 const vector<const char*>& deviceExtensions); 52 static SwapChainSupportDetails querySwapChainSupport(VkPhysicalDevice physicalDevice, 53 VkSurfaceKHR surface); 54 static VkSurfaceFormatKHR chooseSwapSurfaceFormat(const vector<VkSurfaceFormatKHR>& availableFormats); 55 static VkPresentModeKHR chooseSwapPresentMode(const vector<VkPresentModeKHR>& availablePresentModes); 48 static VkSurfaceCapabilitiesKHR querySwapChainCapabilities(VkPhysicalDevice physicalDevice, 49 VkSurfaceKHR surface); 50 static vector<VkSurfaceFormatKHR> querySwapChainFormats(VkPhysicalDevice physicalDevice, VkSurfaceKHR surface); 51 static vector<VkPresentModeKHR> querySwapChainPresentModes(VkPhysicalDevice physicalDevice, 52 VkSurfaceKHR surface); 53 static VkSurfaceFormatKHR chooseSwapSurfaceFormat(const vector<VkSurfaceFormatKHR>& availableFormats, 54 const vector<VkFormat>& requestedFormats, VkColorSpaceKHR requestedColorSpace); 55 static VkPresentModeKHR chooseSwapPresentMode(const vector<VkPresentModeKHR>& availablePresentModes, 56 const vector<VkPresentModeKHR>& requestedPresentModes); 56 57 static VkExtent2D chooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities, int width, int height); 57 58 static VkImageView createImageView(VkDevice device, VkImage image, VkFormat format,
Note:
See TracChangeset
for help on using the changeset viewer.