diff --git a/src/main.cpp b/src/main.cpp index b912ec2..b83d866 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -47,6 +47,7 @@ class VulkanApp { vk::DebugUtilsMessengerUnique mDebugMessenger; vk::DispatchLoaderDynamic mLoader; + vk::UniqueSurfaceKHR mSurface; vk::PhysicalDevice mPhysicalDevice; vk::UniqueDevice mDevice; @@ -54,12 +55,11 @@ class VulkanApp { vk::Queue mGraphicsQueue; vk::Queue mPresentQueue; - vk::UniqueSurfaceKHR mSurface; - - vk::UniqueSwapchainKHR mSwapChain; - std::vector mSwapChainImages; - vk::Format mSwapChainImageFormat; - vk::Extent2D mSwapChainExtent; + vk::UniqueSwapchainKHR mSwapChain; + std::vector mSwapChainImages; + vk::Format mSwapChainImageFormat; + vk::Extent2D mSwapChainExtent; + std::vector mSwapChainImageViews; struct QueueFamilyIndices { std::optional graphics_family; @@ -92,6 +92,7 @@ class VulkanApp { pickPhysicalDevice(); createLogicalDevice(); createSwapChain(); + createImageViews(); } fn mainLoop() -> void { @@ -220,52 +221,6 @@ class VulkanApp { mPresentQueue = mDevice->getQueue(indices.present_family.value(), 0); } - fn isDeviceSuitable(vk::PhysicalDevice device) -> bool { - QueueFamilyIndices indices = findQueueFamilies(device); - - bool extensionsSupported = checkDeviceExtensionSupport(device); - - bool swapChainAdequate = false; - - if (extensionsSupported) { - SwapChainSupportDetails swapChainSupport = querySwapChainSupport(device); - swapChainAdequate = !swapChainSupport.formats.empty() && !swapChainSupport.present_modes.empty(); - } - - return indices.isComplete() && extensionsSupported && swapChainAdequate; - } - - static fn checkDeviceExtensionSupport(vk::PhysicalDevice device) -> bool { - std::vector availableExtensions = device.enumerateDeviceExtensionProperties(); - - std::set requiredExtensions(deviceExtensions.begin(), deviceExtensions.end()); - - for (const auto& extension : availableExtensions) requiredExtensions.erase(extension.extensionName); - - return requiredExtensions.empty(); - } - - fn findQueueFamilies(vk::PhysicalDevice device) -> QueueFamilyIndices { - QueueFamilyIndices indices; - - std::vector queueFamilies = device.getQueueFamilyProperties(); - - for (u32 i = 0; i < queueFamilies.size(); i++) { - if (queueFamilies[i].queueFlags & vk::QueueFlagBits::eGraphics) - indices.graphics_family = i; - - vk::Bool32 presentSupport = device.getSurfaceSupportKHR(i, mSurface.get()); - - if (presentSupport) - indices.present_family = i; - - if (indices.isComplete()) - break; - } - - return indices; - } - fn createSwapChain() -> void { SwapChainSupportDetails swapChainSupport = querySwapChainSupport(mPhysicalDevice); @@ -310,14 +265,27 @@ class VulkanApp { mSwapChainExtent = extent; } - fn querySwapChainSupport(vk::PhysicalDevice device) -> SwapChainSupportDetails { - SwapChainSupportDetails details; + fn createImageViews() -> void { + mSwapChainImageViews.resize(mSwapChainImages.size()); - details.capabilities = device.getSurfaceCapabilitiesKHR(mSurface.get()); - details.formats = device.getSurfaceFormatsKHR(mSurface.get()); - details.present_modes = device.getSurfacePresentModesKHR(mSurface.get()); + for (u32 i = 0; i < mSwapChainImages.size(); i++) { + vk::ImageViewCreateInfo createInfo { + .image = mSwapChainImages[i], + .viewType = vk::ImageViewType::e2D, + .format = mSwapChainImageFormat, + .components = { .r = vk::ComponentSwizzle::eIdentity, + .g = vk::ComponentSwizzle::eIdentity, + .b = vk::ComponentSwizzle::eIdentity, + .a = vk::ComponentSwizzle::eIdentity }, + .subresourceRange = { .aspectMask = vk::ImageAspectFlagBits::eColor, + .baseMipLevel = 0, + .levelCount = 1, + .baseArrayLayer = 0, + .layerCount = 1 }, + }; - return details; + mSwapChainImageViews[i] = mDevice->createImageViewUnique(createInfo); + } } static fn chooseSwapSurfaceFormat(const std::vector& availableFormats @@ -356,6 +324,62 @@ class VulkanApp { return actualExtent; } + fn querySwapChainSupport(vk::PhysicalDevice device) -> SwapChainSupportDetails { + SwapChainSupportDetails details; + + details.capabilities = device.getSurfaceCapabilitiesKHR(mSurface.get()); + details.formats = device.getSurfaceFormatsKHR(mSurface.get()); + details.present_modes = device.getSurfacePresentModesKHR(mSurface.get()); + + return details; + } + + fn isDeviceSuitable(vk::PhysicalDevice device) -> bool { + QueueFamilyIndices indices = findQueueFamilies(device); + + bool extensionsSupported = checkDeviceExtensionSupport(device); + + bool swapChainAdequate = false; + + if (extensionsSupported) { + SwapChainSupportDetails swapChainSupport = querySwapChainSupport(device); + swapChainAdequate = !swapChainSupport.formats.empty() && !swapChainSupport.present_modes.empty(); + } + + return indices.isComplete() && extensionsSupported && swapChainAdequate; + } + + static fn checkDeviceExtensionSupport(vk::PhysicalDevice device) -> bool { + std::vector availableExtensions = device.enumerateDeviceExtensionProperties(); + + std::set requiredExtensions(deviceExtensions.begin(), deviceExtensions.end()); + + for (const auto& extension : availableExtensions) requiredExtensions.erase(extension.extensionName); + + return requiredExtensions.empty(); + } + + fn findQueueFamilies(vk::PhysicalDevice device) -> QueueFamilyIndices { + QueueFamilyIndices indices; + + std::vector queueFamilies = device.getQueueFamilyProperties(); + + for (u32 i = 0; i < queueFamilies.size(); i++) { + if (queueFamilies[i].queueFlags & vk::QueueFlagBits::eGraphics) + indices.graphics_family = i; + + vk::Bool32 presentSupport = device.getSurfaceSupportKHR(i, mSurface.get()); + + if (presentSupport) + indices.present_family = i; + + if (indices.isComplete()) + break; + } + + return indices; + } + static fn getRequiredExtensions() -> std::vector { std::span extensionsSpan = vkfw::getRequiredInstanceExtensions();