diff --git a/attachments/05_window_surface.cpp b/attachments/05_window_surface.cpp index 8b240b82..b08fa68c 100644 --- a/attachments/05_window_surface.cpp +++ b/attachments/05_window_surface.cpp @@ -40,18 +40,14 @@ class HelloTriangleApplication { } private: - GLFWwindow* window = nullptr; - - vk::raii::Context context; - vk::raii::Instance instance = nullptr; + GLFWwindow * window = nullptr; + vk::raii::Context context; + vk::raii::Instance instance = nullptr; vk::raii::DebugUtilsMessengerEXT debugMessenger = nullptr; - vk::raii::SurfaceKHR surface = nullptr; - - vk::raii::PhysicalDevice physicalDevice = nullptr; - vk::raii::Device device = nullptr; - - vk::raii::Queue graphicsQueue = nullptr; - vk::raii::Queue presentQueue = nullptr; + vk::raii::SurfaceKHR surface = nullptr; + vk::raii::PhysicalDevice physicalDevice = nullptr; + vk::raii::Device device = nullptr; + vk::raii::Queue queue = nullptr; std::vector requiredDeviceExtension = { vk::KHRSwapchainExtensionName, @@ -201,53 +197,25 @@ class HelloTriangleApplication { } void createLogicalDevice() { - // find the index of the first queue family that supports graphics - std::vector queueFamilyProperties = physicalDevice.getQueueFamilyProperties(); + std::vector queueFamilyProperties = physicalDevice.getQueueFamilyProperties(); - // get the first index into queueFamilyProperties which supports graphics - auto graphicsQueueFamilyProperty = std::ranges::find_if( queueFamilyProperties, []( auto const & qfp ) - { return (qfp.queueFlags & vk::QueueFlagBits::eGraphics) != static_cast(0); } ); - - auto graphicsIndex = static_cast( std::distance( queueFamilyProperties.begin(), graphicsQueueFamilyProperty ) ); - - // determine a queueFamilyIndex that supports present - // first check if the graphicsIndex is good enough - auto presentIndex = physicalDevice.getSurfaceSupportKHR( graphicsIndex, *surface ) - ? graphicsIndex - : ~0; - if ( presentIndex == queueFamilyProperties.size() ) + // get the first index into queueFamilyProperties which supports both graphics and present + uint32_t queueIndex = ~0; + for (uint32_t qfpIndex = 0; qfpIndex < queueFamilyProperties.size(); qfpIndex++) { - // the graphicsIndex doesn't support present -> look for another family index that supports both - // graphics and present - for ( size_t i = 0; i < queueFamilyProperties.size(); i++ ) - { - if ( ( queueFamilyProperties[i].queueFlags & vk::QueueFlagBits::eGraphics ) && - physicalDevice.getSurfaceSupportKHR( static_cast( i ), *surface ) ) - { - graphicsIndex = static_cast( i ); - presentIndex = graphicsIndex; - break; - } - } - if ( presentIndex == queueFamilyProperties.size() ) - { - // there's nothing like a single family index that supports both graphics and present -> look for another - // family index that supports present - for ( size_t i = 0; i < queueFamilyProperties.size(); i++ ) - { - if ( physicalDevice.getSurfaceSupportKHR( static_cast( i ), *surface ) ) - { - presentIndex = static_cast( i ); - break; - } - } - } + if ((queueFamilyProperties[qfpIndex].queueFlags & vk::QueueFlagBits::eGraphics) && + physicalDevice.getSurfaceSupportKHR(qfpIndex, *surface)) + { + // found a queue family that supports both graphics and present + queueIndex = qfpIndex; + break; + } } - if ( ( graphicsIndex == queueFamilyProperties.size() ) || ( presentIndex == queueFamilyProperties.size() ) ) + if (queueIndex == ~0) { - throw std::runtime_error( "Could not find a queue for graphics or present -> terminating" ); + throw std::runtime_error("Could not find a queue for graphics and present -> terminating"); } - + // query for Vulkan 1.3 features vk::StructureChain featureChain = { {}, // vk::PhysicalDeviceFeatures2 @@ -257,7 +225,7 @@ class HelloTriangleApplication { // create a Device float queuePriority = 0.0f; - vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = graphicsIndex, .queueCount = 1, .pQueuePriorities = &queuePriority }; + vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = queueIndex, .queueCount = 1, .pQueuePriorities = &queuePriority }; vk::DeviceCreateInfo deviceCreateInfo{ .pNext = &featureChain.get(), .queueCreateInfoCount = 1, .pQueueCreateInfos = &deviceQueueCreateInfo, @@ -265,8 +233,7 @@ class HelloTriangleApplication { .ppEnabledExtensionNames = requiredDeviceExtension.data() }; device = vk::raii::Device( physicalDevice, deviceCreateInfo ); - graphicsQueue = vk::raii::Queue( device, graphicsIndex, 0 ); - presentQueue = vk::raii::Queue( device, presentIndex, 0 ); + queue = vk::raii::Queue( device, queueIndex, 0 ); } std::vector getRequiredExtensions() { diff --git a/attachments/06_swap_chain_creation.cpp b/attachments/06_swap_chain_creation.cpp index d86b06f0..5f668372 100644 --- a/attachments/06_swap_chain_creation.cpp +++ b/attachments/06_swap_chain_creation.cpp @@ -41,18 +41,14 @@ class HelloTriangleApplication { } private: - GLFWwindow* window = nullptr; - - vk::raii::Context context; - vk::raii::Instance instance = nullptr; + GLFWwindow * window = nullptr; + vk::raii::Context context; + vk::raii::Instance instance = nullptr; vk::raii::DebugUtilsMessengerEXT debugMessenger = nullptr; - vk::raii::SurfaceKHR surface = nullptr; - - vk::raii::PhysicalDevice physicalDevice = nullptr; - vk::raii::Device device = nullptr; - - vk::raii::Queue graphicsQueue = nullptr; - vk::raii::Queue presentQueue = nullptr; + vk::raii::SurfaceKHR surface = nullptr; + vk::raii::PhysicalDevice physicalDevice = nullptr; + vk::raii::Device device = nullptr; + vk::raii::Queue queue = nullptr; vk::raii::SwapchainKHR swapChain = nullptr; std::vector swapChainImages; @@ -209,51 +205,23 @@ class HelloTriangleApplication { } void createLogicalDevice() { - // find the index of the first queue family that supports graphics std::vector queueFamilyProperties = physicalDevice.getQueueFamilyProperties(); - // get the first index into queueFamilyProperties which supports graphics - auto graphicsQueueFamilyProperty = std::ranges::find_if( queueFamilyProperties, []( auto const & qfp ) - { return (qfp.queueFlags & vk::QueueFlagBits::eGraphics) != static_cast(0); } ); - - auto graphicsIndex = static_cast( std::distance( queueFamilyProperties.begin(), graphicsQueueFamilyProperty ) ); - - // determine a queueFamilyIndex that supports present - // first check if the graphicsIndex is good enough - auto presentIndex = physicalDevice.getSurfaceSupportKHR( graphicsIndex, *surface ) - ? graphicsIndex - : ~0; - if ( presentIndex == queueFamilyProperties.size() ) + // get the first index into queueFamilyProperties which supports both graphics and present + uint32_t queueIndex = ~0; + for (uint32_t qfpIndex = 0; qfpIndex < queueFamilyProperties.size(); qfpIndex++) { - // the graphicsIndex doesn't support present -> look for another family index that supports both - // graphics and present - for ( size_t i = 0; i < queueFamilyProperties.size(); i++ ) - { - if ( ( queueFamilyProperties[i].queueFlags & vk::QueueFlagBits::eGraphics ) && - physicalDevice.getSurfaceSupportKHR( static_cast( i ), *surface ) ) - { - graphicsIndex = static_cast( i ); - presentIndex = graphicsIndex; - break; - } - } - if ( presentIndex == queueFamilyProperties.size() ) + if ((queueFamilyProperties[qfpIndex].queueFlags & vk::QueueFlagBits::eGraphics) && + physicalDevice.getSurfaceSupportKHR(qfpIndex, *surface)) { - // there's nothing like a single family index that supports both graphics and present -> look for another - // family index that supports present - for ( size_t i = 0; i < queueFamilyProperties.size(); i++ ) - { - if ( physicalDevice.getSurfaceSupportKHR( static_cast( i ), *surface ) ) - { - presentIndex = static_cast( i ); - break; - } - } + // found a queue family that supports both graphics and present + queueIndex = qfpIndex; + break; } } - if ( ( graphicsIndex == queueFamilyProperties.size() ) || ( presentIndex == queueFamilyProperties.size() ) ) + if (queueIndex == ~0) { - throw std::runtime_error( "Could not find a queue for graphics or present -> terminating" ); + throw std::runtime_error("Could not find a queue for graphics and present -> terminating"); } // query for Vulkan 1.3 features @@ -265,7 +233,7 @@ class HelloTriangleApplication { // create a Device float queuePriority = 0.0f; - vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = graphicsIndex, .queueCount = 1, .pQueuePriorities = &queuePriority }; + vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = queueIndex, .queueCount = 1, .pQueuePriorities = &queuePriority }; vk::DeviceCreateInfo deviceCreateInfo{ .pNext = &featureChain.get(), .queueCreateInfoCount = 1, .pQueueCreateInfos = &deviceQueueCreateInfo, @@ -273,8 +241,7 @@ class HelloTriangleApplication { .ppEnabledExtensionNames = requiredDeviceExtension.data() }; device = vk::raii::Device( physicalDevice, deviceCreateInfo ); - graphicsQueue = vk::raii::Queue( device, graphicsIndex, 0 ); - presentQueue = vk::raii::Queue( device, presentIndex, 0 ); + queue = vk::raii::Queue( device, queueIndex, 0 ); } void createSwapChain() { diff --git a/attachments/07_image_views.cpp b/attachments/07_image_views.cpp index 4c3872f1..c43f981f 100644 --- a/attachments/07_image_views.cpp +++ b/attachments/07_image_views.cpp @@ -41,18 +41,14 @@ class HelloTriangleApplication { } private: - GLFWwindow* window = nullptr; - - vk::raii::Context context; - vk::raii::Instance instance = nullptr; + GLFWwindow * window = nullptr; + vk::raii::Context context; + vk::raii::Instance instance = nullptr; vk::raii::DebugUtilsMessengerEXT debugMessenger = nullptr; - vk::raii::SurfaceKHR surface = nullptr; - - vk::raii::PhysicalDevice physicalDevice = nullptr; - vk::raii::Device device = nullptr; - - vk::raii::Queue graphicsQueue = nullptr; - vk::raii::Queue presentQueue = nullptr; + vk::raii::SurfaceKHR surface = nullptr; + vk::raii::PhysicalDevice physicalDevice = nullptr; + vk::raii::Device device = nullptr; + vk::raii::Queue queue = nullptr; vk::raii::SwapchainKHR swapChain = nullptr; std::vector swapChainImages; @@ -210,51 +206,23 @@ class HelloTriangleApplication { } void createLogicalDevice() { - // find the index of the first queue family that supports graphics std::vector queueFamilyProperties = physicalDevice.getQueueFamilyProperties(); - // get the first index into queueFamilyProperties which supports graphics - auto graphicsQueueFamilyProperty = std::ranges::find_if( queueFamilyProperties, []( auto const & qfp ) - { return (qfp.queueFlags & vk::QueueFlagBits::eGraphics) != static_cast(0); } ); - - auto graphicsIndex = static_cast( std::distance( queueFamilyProperties.begin(), graphicsQueueFamilyProperty ) ); - - // determine a queueFamilyIndex that supports present - // first check if the graphicsIndex is good enough - auto presentIndex = physicalDevice.getSurfaceSupportKHR( graphicsIndex, *surface ) - ? graphicsIndex - : ~0; - if ( presentIndex == queueFamilyProperties.size() ) + // get the first index into queueFamilyProperties which supports both graphics and present + uint32_t queueIndex = ~0; + for (uint32_t qfpIndex = 0; qfpIndex < queueFamilyProperties.size(); qfpIndex++) { - // the graphicsIndex doesn't support present -> look for another family index that supports both - // graphics and present - for ( size_t i = 0; i < queueFamilyProperties.size(); i++ ) - { - if ( ( queueFamilyProperties[i].queueFlags & vk::QueueFlagBits::eGraphics ) && - physicalDevice.getSurfaceSupportKHR( static_cast( i ), *surface ) ) - { - graphicsIndex = static_cast( i ); - presentIndex = graphicsIndex; - break; - } - } - if ( presentIndex == queueFamilyProperties.size() ) + if ((queueFamilyProperties[qfpIndex].queueFlags & vk::QueueFlagBits::eGraphics) && + physicalDevice.getSurfaceSupportKHR(qfpIndex, *surface)) { - // there's nothing like a single family index that supports both graphics and present -> look for another - // family index that supports present - for ( size_t i = 0; i < queueFamilyProperties.size(); i++ ) - { - if ( physicalDevice.getSurfaceSupportKHR( static_cast( i ), *surface ) ) - { - presentIndex = static_cast( i ); - break; - } - } + // found a queue family that supports both graphics and present + queueIndex = qfpIndex; + break; } } - if ( ( graphicsIndex == queueFamilyProperties.size() ) || ( presentIndex == queueFamilyProperties.size() ) ) + if (queueIndex == ~0) { - throw std::runtime_error( "Could not find a queue for graphics or present -> terminating" ); + throw std::runtime_error("Could not find a queue for graphics and present -> terminating"); } // query for Vulkan 1.3 features @@ -266,7 +234,7 @@ class HelloTriangleApplication { // create a Device float queuePriority = 0.0f; - vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = graphicsIndex, .queueCount = 1, .pQueuePriorities = &queuePriority }; + vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = queueIndex, .queueCount = 1, .pQueuePriorities = &queuePriority }; vk::DeviceCreateInfo deviceCreateInfo{ .pNext = &featureChain.get(), .queueCreateInfoCount = 1, .pQueueCreateInfos = &deviceQueueCreateInfo, @@ -274,8 +242,7 @@ class HelloTriangleApplication { .ppEnabledExtensionNames = requiredDeviceExtension.data() }; device = vk::raii::Device( physicalDevice, deviceCreateInfo ); - graphicsQueue = vk::raii::Queue( device, graphicsIndex, 0 ); - presentQueue = vk::raii::Queue( device, presentIndex, 0 ); + queue = vk::raii::Queue( device, queueIndex, 0 ); } void createSwapChain() { diff --git a/attachments/08_graphics_pipeline.cpp b/attachments/08_graphics_pipeline.cpp index a01adf69..cecbccd8 100644 --- a/attachments/08_graphics_pipeline.cpp +++ b/attachments/08_graphics_pipeline.cpp @@ -41,18 +41,14 @@ class HelloTriangleApplication { } private: - GLFWwindow* window = nullptr; - - vk::raii::Context context; - vk::raii::Instance instance = nullptr; + GLFWwindow * window = nullptr; + vk::raii::Context context; + vk::raii::Instance instance = nullptr; vk::raii::DebugUtilsMessengerEXT debugMessenger = nullptr; - vk::raii::SurfaceKHR surface = nullptr; - - vk::raii::PhysicalDevice physicalDevice = nullptr; - vk::raii::Device device = nullptr; - - vk::raii::Queue graphicsQueue = nullptr; - vk::raii::Queue presentQueue = nullptr; + vk::raii::SurfaceKHR surface = nullptr; + vk::raii::PhysicalDevice physicalDevice = nullptr; + vk::raii::Device device = nullptr; + vk::raii::Queue queue = nullptr; vk::raii::SwapchainKHR swapChain = nullptr; std::vector swapChainImages; @@ -211,51 +207,23 @@ class HelloTriangleApplication { } void createLogicalDevice() { - // find the index of the first queue family that supports graphics std::vector queueFamilyProperties = physicalDevice.getQueueFamilyProperties(); - // get the first index into queueFamilyProperties which supports graphics - auto graphicsQueueFamilyProperty = std::ranges::find_if( queueFamilyProperties, []( auto const & qfp ) - { return (qfp.queueFlags & vk::QueueFlagBits::eGraphics) != static_cast(0); } ); - - auto graphicsIndex = static_cast( std::distance( queueFamilyProperties.begin(), graphicsQueueFamilyProperty ) ); - - // determine a queueFamilyIndex that supports present - // first check if the graphicsIndex is good enough - auto presentIndex = physicalDevice.getSurfaceSupportKHR( graphicsIndex, *surface ) - ? graphicsIndex - : ~0; - if ( presentIndex == queueFamilyProperties.size() ) + // get the first index into queueFamilyProperties which supports both graphics and present + uint32_t queueIndex = ~0; + for (uint32_t qfpIndex = 0; qfpIndex < queueFamilyProperties.size(); qfpIndex++) { - // the graphicsIndex doesn't support present -> look for another family index that supports both - // graphics and present - for ( size_t i = 0; i < queueFamilyProperties.size(); i++ ) - { - if ( ( queueFamilyProperties[i].queueFlags & vk::QueueFlagBits::eGraphics ) && - physicalDevice.getSurfaceSupportKHR( static_cast( i ), *surface ) ) - { - graphicsIndex = static_cast( i ); - presentIndex = graphicsIndex; - break; - } - } - if ( presentIndex == queueFamilyProperties.size() ) + if ((queueFamilyProperties[qfpIndex].queueFlags & vk::QueueFlagBits::eGraphics) && + physicalDevice.getSurfaceSupportKHR(qfpIndex, *surface)) { - // there's nothing like a single family index that supports both graphics and present -> look for another - // family index that supports present - for ( size_t i = 0; i < queueFamilyProperties.size(); i++ ) - { - if ( physicalDevice.getSurfaceSupportKHR( static_cast( i ), *surface ) ) - { - presentIndex = static_cast( i ); - break; - } - } + // found a queue family that supports both graphics and present + queueIndex = qfpIndex; + break; } } - if ( ( graphicsIndex == queueFamilyProperties.size() ) || ( presentIndex == queueFamilyProperties.size() ) ) + if (queueIndex == ~0) { - throw std::runtime_error( "Could not find a queue for graphics or present -> terminating" ); + throw std::runtime_error("Could not find a queue for graphics and present -> terminating"); } // query for Vulkan 1.3 features @@ -267,7 +235,7 @@ class HelloTriangleApplication { // create a Device float queuePriority = 0.0f; - vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = graphicsIndex, .queueCount = 1, .pQueuePriorities = &queuePriority }; + vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = queueIndex, .queueCount = 1, .pQueuePriorities = &queuePriority }; vk::DeviceCreateInfo deviceCreateInfo{ .pNext = &featureChain.get(), .queueCreateInfoCount = 1, .pQueueCreateInfos = &deviceQueueCreateInfo, @@ -275,8 +243,7 @@ class HelloTriangleApplication { .ppEnabledExtensionNames = requiredDeviceExtension.data() }; device = vk::raii::Device( physicalDevice, deviceCreateInfo ); - graphicsQueue = vk::raii::Queue( device, graphicsIndex, 0 ); - presentQueue = vk::raii::Queue( device, presentIndex, 0 ); + queue = vk::raii::Queue( device, queueIndex, 0 ); } void createSwapChain() { diff --git a/attachments/09_shader_modules.cpp b/attachments/09_shader_modules.cpp index 3bcc5006..09816eb5 100644 --- a/attachments/09_shader_modules.cpp +++ b/attachments/09_shader_modules.cpp @@ -42,18 +42,14 @@ class HelloTriangleApplication { } private: - GLFWwindow* window = nullptr; - - vk::raii::Context context; - vk::raii::Instance instance = nullptr; + GLFWwindow * window = nullptr; + vk::raii::Context context; + vk::raii::Instance instance = nullptr; vk::raii::DebugUtilsMessengerEXT debugMessenger = nullptr; - vk::raii::SurfaceKHR surface = nullptr; - - vk::raii::PhysicalDevice physicalDevice = nullptr; - vk::raii::Device device = nullptr; - - vk::raii::Queue graphicsQueue = nullptr; - vk::raii::Queue presentQueue = nullptr; + vk::raii::SurfaceKHR surface = nullptr; + vk::raii::PhysicalDevice physicalDevice = nullptr; + vk::raii::Device device = nullptr; + vk::raii::Queue queue = nullptr; vk::raii::SwapchainKHR swapChain = nullptr; std::vector swapChainImages; @@ -212,51 +208,23 @@ class HelloTriangleApplication { } void createLogicalDevice() { - // find the index of the first queue family that supports graphics std::vector queueFamilyProperties = physicalDevice.getQueueFamilyProperties(); - // get the first index into queueFamilyProperties which supports graphics - auto graphicsQueueFamilyProperty = std::ranges::find_if( queueFamilyProperties, []( auto const & qfp ) - { return (qfp.queueFlags & vk::QueueFlagBits::eGraphics) != static_cast(0); } ); - - auto graphicsIndex = static_cast( std::distance( queueFamilyProperties.begin(), graphicsQueueFamilyProperty ) ); - - // determine a queueFamilyIndex that supports present - // first check if the graphicsIndex is good enough - auto presentIndex = physicalDevice.getSurfaceSupportKHR( graphicsIndex, *surface ) - ? graphicsIndex - : ~0; - if ( presentIndex == queueFamilyProperties.size() ) + // get the first index into queueFamilyProperties which supports both graphics and present + uint32_t queueIndex = ~0; + for (uint32_t qfpIndex = 0; qfpIndex < queueFamilyProperties.size(); qfpIndex++) { - // the graphicsIndex doesn't support present -> look for another family index that supports both - // graphics and present - for ( size_t i = 0; i < queueFamilyProperties.size(); i++ ) - { - if ( ( queueFamilyProperties[i].queueFlags & vk::QueueFlagBits::eGraphics ) && - physicalDevice.getSurfaceSupportKHR( static_cast( i ), *surface ) ) - { - graphicsIndex = static_cast( i ); - presentIndex = graphicsIndex; - break; - } - } - if ( presentIndex == queueFamilyProperties.size() ) + if ((queueFamilyProperties[qfpIndex].queueFlags & vk::QueueFlagBits::eGraphics) && + physicalDevice.getSurfaceSupportKHR(qfpIndex, *surface)) { - // there's nothing like a single family index that supports both graphics and present -> look for another - // family index that supports present - for ( size_t i = 0; i < queueFamilyProperties.size(); i++ ) - { - if ( physicalDevice.getSurfaceSupportKHR( static_cast( i ), *surface ) ) - { - presentIndex = static_cast( i ); - break; - } - } + // found a queue family that supports both graphics and present + queueIndex = qfpIndex; + break; } } - if ( ( graphicsIndex == queueFamilyProperties.size() ) || ( presentIndex == queueFamilyProperties.size() ) ) + if (queueIndex == ~0) { - throw std::runtime_error( "Could not find a queue for graphics or present -> terminating" ); + throw std::runtime_error("Could not find a queue for graphics and present -> terminating"); } // query for Vulkan 1.3 features @@ -268,7 +236,7 @@ class HelloTriangleApplication { // create a Device float queuePriority = 0.0f; - vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = graphicsIndex, .queueCount = 1, .pQueuePriorities = &queuePriority }; + vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = queueIndex, .queueCount = 1, .pQueuePriorities = &queuePriority }; vk::DeviceCreateInfo deviceCreateInfo{ .pNext = &featureChain.get(), .queueCreateInfoCount = 1, .pQueueCreateInfos = &deviceQueueCreateInfo, @@ -276,8 +244,7 @@ class HelloTriangleApplication { .ppEnabledExtensionNames = requiredDeviceExtension.data() }; device = vk::raii::Device( physicalDevice, deviceCreateInfo ); - graphicsQueue = vk::raii::Queue( device, graphicsIndex, 0 ); - presentQueue = vk::raii::Queue( device, presentIndex, 0 ); + queue = vk::raii::Queue( device, queueIndex, 0 ); } void createSwapChain() { diff --git a/attachments/10_fixed_functions.cpp b/attachments/10_fixed_functions.cpp index a4e59a86..d0af79d4 100644 --- a/attachments/10_fixed_functions.cpp +++ b/attachments/10_fixed_functions.cpp @@ -42,18 +42,14 @@ class HelloTriangleApplication { } private: - GLFWwindow* window = nullptr; - - vk::raii::Context context; - vk::raii::Instance instance = nullptr; + GLFWwindow * window = nullptr; + vk::raii::Context context; + vk::raii::Instance instance = nullptr; vk::raii::DebugUtilsMessengerEXT debugMessenger = nullptr; - vk::raii::SurfaceKHR surface = nullptr; - - vk::raii::PhysicalDevice physicalDevice = nullptr; - vk::raii::Device device = nullptr; - - vk::raii::Queue graphicsQueue = nullptr; - vk::raii::Queue presentQueue = nullptr; + vk::raii::SurfaceKHR surface = nullptr; + vk::raii::PhysicalDevice physicalDevice = nullptr; + vk::raii::Device device = nullptr; + vk::raii::Queue queue = nullptr; vk::raii::SwapchainKHR swapChain = nullptr; std::vector swapChainImages; @@ -214,51 +210,23 @@ class HelloTriangleApplication { } void createLogicalDevice() { - // find the index of the first queue family that supports graphics std::vector queueFamilyProperties = physicalDevice.getQueueFamilyProperties(); - // get the first index into queueFamilyProperties which supports graphics - auto graphicsQueueFamilyProperty = std::ranges::find_if( queueFamilyProperties, []( auto const & qfp ) - { return (qfp.queueFlags & vk::QueueFlagBits::eGraphics) != static_cast(0); } ); - - auto graphicsIndex = static_cast( std::distance( queueFamilyProperties.begin(), graphicsQueueFamilyProperty ) ); - - // determine a queueFamilyIndex that supports present - // first check if the graphicsIndex is good enough - auto presentIndex = physicalDevice.getSurfaceSupportKHR( graphicsIndex, *surface ) - ? graphicsIndex - : ~0; - if ( presentIndex == queueFamilyProperties.size() ) + // get the first index into queueFamilyProperties which supports both graphics and present + uint32_t queueIndex = ~0; + for (uint32_t qfpIndex = 0; qfpIndex < queueFamilyProperties.size(); qfpIndex++) { - // the graphicsIndex doesn't support present -> look for another family index that supports both - // graphics and present - for ( size_t i = 0; i < queueFamilyProperties.size(); i++ ) - { - if ( ( queueFamilyProperties[i].queueFlags & vk::QueueFlagBits::eGraphics ) && - physicalDevice.getSurfaceSupportKHR( static_cast( i ), *surface ) ) - { - graphicsIndex = static_cast( i ); - presentIndex = graphicsIndex; - break; - } - } - if ( presentIndex == queueFamilyProperties.size() ) + if ((queueFamilyProperties[qfpIndex].queueFlags & vk::QueueFlagBits::eGraphics) && + physicalDevice.getSurfaceSupportKHR(qfpIndex, *surface)) { - // there's nothing like a single family index that supports both graphics and present -> look for another - // family index that supports present - for ( size_t i = 0; i < queueFamilyProperties.size(); i++ ) - { - if ( physicalDevice.getSurfaceSupportKHR( static_cast( i ), *surface ) ) - { - presentIndex = static_cast( i ); - break; - } - } + // found a queue family that supports both graphics and present + queueIndex = qfpIndex; + break; } } - if ( ( graphicsIndex == queueFamilyProperties.size() ) || ( presentIndex == queueFamilyProperties.size() ) ) + if (queueIndex == ~0) { - throw std::runtime_error( "Could not find a queue for graphics or present -> terminating" ); + throw std::runtime_error("Could not find a queue for graphics and present -> terminating"); } // query for Vulkan 1.3 features @@ -270,7 +238,7 @@ class HelloTriangleApplication { // create a Device float queuePriority = 0.0f; - vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = graphicsIndex, .queueCount = 1, .pQueuePriorities = &queuePriority }; + vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = queueIndex, .queueCount = 1, .pQueuePriorities = &queuePriority }; vk::DeviceCreateInfo deviceCreateInfo{ .pNext = &featureChain.get(), .queueCreateInfoCount = 1, .pQueueCreateInfos = &deviceQueueCreateInfo, @@ -278,8 +246,7 @@ class HelloTriangleApplication { .ppEnabledExtensionNames = requiredDeviceExtension.data() }; device = vk::raii::Device( physicalDevice, deviceCreateInfo ); - graphicsQueue = vk::raii::Queue( device, graphicsIndex, 0 ); - presentQueue = vk::raii::Queue( device, presentIndex, 0 ); + queue = vk::raii::Queue( device, queueIndex, 0 ); } void createSwapChain() { diff --git a/attachments/12_graphics_pipeline_complete.cpp b/attachments/12_graphics_pipeline_complete.cpp index bfdb8be2..70279ee5 100644 --- a/attachments/12_graphics_pipeline_complete.cpp +++ b/attachments/12_graphics_pipeline_complete.cpp @@ -42,18 +42,14 @@ class HelloTriangleApplication { } private: - GLFWwindow* window = nullptr; - - vk::raii::Context context; - vk::raii::Instance instance = nullptr; + GLFWwindow * window = nullptr; + vk::raii::Context context; + vk::raii::Instance instance = nullptr; vk::raii::DebugUtilsMessengerEXT debugMessenger = nullptr; - vk::raii::SurfaceKHR surface = nullptr; - - vk::raii::PhysicalDevice physicalDevice = nullptr; - vk::raii::Device device = nullptr; - - vk::raii::Queue graphicsQueue = nullptr; - vk::raii::Queue presentQueue = nullptr; + vk::raii::SurfaceKHR surface = nullptr; + vk::raii::PhysicalDevice physicalDevice = nullptr; + vk::raii::Device device = nullptr; + vk::raii::Queue queue = nullptr; vk::raii::SwapchainKHR swapChain = nullptr; std::vector swapChainImages; @@ -215,51 +211,23 @@ class HelloTriangleApplication { } void createLogicalDevice() { - // find the index of the first queue family that supports graphics std::vector queueFamilyProperties = physicalDevice.getQueueFamilyProperties(); - // get the first index into queueFamilyProperties which supports graphics - auto graphicsQueueFamilyProperty = std::ranges::find_if( queueFamilyProperties, []( auto const & qfp ) - { return (qfp.queueFlags & vk::QueueFlagBits::eGraphics) != static_cast(0); } ); - - auto graphicsIndex = static_cast( std::distance( queueFamilyProperties.begin(), graphicsQueueFamilyProperty ) ); - - // determine a queueFamilyIndex that supports present - // first check if the graphicsIndex is good enough - auto presentIndex = physicalDevice.getSurfaceSupportKHR( graphicsIndex, *surface ) - ? graphicsIndex - : ~0; - if ( presentIndex == queueFamilyProperties.size() ) + // get the first index into queueFamilyProperties which supports both graphics and present + uint32_t queueIndex = ~0; + for (uint32_t qfpIndex = 0; qfpIndex < queueFamilyProperties.size(); qfpIndex++) { - // the graphicsIndex doesn't support present -> look for another family index that supports both - // graphics and present - for ( size_t i = 0; i < queueFamilyProperties.size(); i++ ) - { - if ( ( queueFamilyProperties[i].queueFlags & vk::QueueFlagBits::eGraphics ) && - physicalDevice.getSurfaceSupportKHR( static_cast( i ), *surface ) ) - { - graphicsIndex = static_cast( i ); - presentIndex = graphicsIndex; - break; - } - } - if ( presentIndex == queueFamilyProperties.size() ) + if ((queueFamilyProperties[qfpIndex].queueFlags & vk::QueueFlagBits::eGraphics) && + physicalDevice.getSurfaceSupportKHR(qfpIndex, *surface)) { - // there's nothing like a single family index that supports both graphics and present -> look for another - // family index that supports present - for ( size_t i = 0; i < queueFamilyProperties.size(); i++ ) - { - if ( physicalDevice.getSurfaceSupportKHR( static_cast( i ), *surface ) ) - { - presentIndex = static_cast( i ); - break; - } - } + // found a queue family that supports both graphics and present + queueIndex = qfpIndex; + break; } } - if ( ( graphicsIndex == queueFamilyProperties.size() ) || ( presentIndex == queueFamilyProperties.size() ) ) + if (queueIndex == ~0) { - throw std::runtime_error( "Could not find a queue for graphics or present -> terminating" ); + throw std::runtime_error("Could not find a queue for graphics and present -> terminating"); } // query for Vulkan 1.3 features @@ -271,7 +239,7 @@ class HelloTriangleApplication { // create a Device float queuePriority = 0.0f; - vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = graphicsIndex, .queueCount = 1, .pQueuePriorities = &queuePriority }; + vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = queueIndex, .queueCount = 1, .pQueuePriorities = &queuePriority }; vk::DeviceCreateInfo deviceCreateInfo{ .pNext = &featureChain.get(), .queueCreateInfoCount = 1, .pQueueCreateInfos = &deviceQueueCreateInfo, @@ -279,8 +247,7 @@ class HelloTriangleApplication { .ppEnabledExtensionNames = requiredDeviceExtension.data() }; device = vk::raii::Device( physicalDevice, deviceCreateInfo ); - graphicsQueue = vk::raii::Queue( device, graphicsIndex, 0 ); - presentQueue = vk::raii::Queue( device, presentIndex, 0 ); + queue = vk::raii::Queue( device, queueIndex, 0 ); } void createSwapChain() { diff --git a/attachments/14_command_buffers.cpp b/attachments/14_command_buffers.cpp index 2c42e236..f0d24467 100644 --- a/attachments/14_command_buffers.cpp +++ b/attachments/14_command_buffers.cpp @@ -42,18 +42,15 @@ class HelloTriangleApplication { } private: - GLFWwindow* window = nullptr; - - vk::raii::Context context; - vk::raii::Instance instance = nullptr; + GLFWwindow * window = nullptr; + vk::raii::Context context; + vk::raii::Instance instance = nullptr; vk::raii::DebugUtilsMessengerEXT debugMessenger = nullptr; - vk::raii::SurfaceKHR surface = nullptr; - - vk::raii::PhysicalDevice physicalDevice = nullptr; - vk::raii::Device device = nullptr; - - vk::raii::Queue graphicsQueue = nullptr; - vk::raii::Queue presentQueue = nullptr; + vk::raii::SurfaceKHR surface = nullptr; + vk::raii::PhysicalDevice physicalDevice = nullptr; + vk::raii::Device device = nullptr; + uint32_t queueIndex = ~0; + vk::raii::Queue queue = nullptr; vk::raii::SwapchainKHR swapChain = nullptr; std::vector swapChainImages; @@ -65,7 +62,6 @@ class HelloTriangleApplication { vk::raii::Pipeline graphicsPipeline = nullptr; vk::raii::CommandPool commandPool = nullptr; vk::raii::CommandBuffer commandBuffer = nullptr; - uint32_t graphicsIndex = 0; std::vector requiredDeviceExtension = { vk::KHRSwapchainExtensionName, @@ -220,51 +216,22 @@ class HelloTriangleApplication { } void createLogicalDevice() { - // find the index of the first queue family that supports graphics std::vector queueFamilyProperties = physicalDevice.getQueueFamilyProperties(); - // get the first index into queueFamilyProperties which supports graphics - auto graphicsQueueFamilyProperty = std::ranges::find_if( queueFamilyProperties, []( auto const & qfp ) - { return (qfp.queueFlags & vk::QueueFlagBits::eGraphics) != static_cast(0); } ); - - graphicsIndex = static_cast( std::distance( queueFamilyProperties.begin(), graphicsQueueFamilyProperty ) ); - - // determine a queueFamilyIndex that supports present - // first check if the graphicsIndex is good enough - auto presentIndex = physicalDevice.getSurfaceSupportKHR( graphicsIndex, *surface ) - ? graphicsIndex - : ~0; - if ( presentIndex == queueFamilyProperties.size() ) + // get the first index into queueFamilyProperties which supports both graphics and present + for (uint32_t qfpIndex = 0; qfpIndex < queueFamilyProperties.size(); qfpIndex++) { - // the graphicsIndex doesn't support present -> look for another family index that supports both - // graphics and present - for ( size_t i = 0; i < queueFamilyProperties.size(); i++ ) - { - if ( ( queueFamilyProperties[i].queueFlags & vk::QueueFlagBits::eGraphics ) && - physicalDevice.getSurfaceSupportKHR( static_cast( i ), *surface ) ) - { - graphicsIndex = static_cast( i ); - presentIndex = graphicsIndex; - break; - } - } - if ( presentIndex == queueFamilyProperties.size() ) + if ((queueFamilyProperties[qfpIndex].queueFlags & vk::QueueFlagBits::eGraphics) && + physicalDevice.getSurfaceSupportKHR(qfpIndex, *surface)) { - // there's nothing like a single family index that supports both graphics and present -> look for another - // family index that supports present - for ( size_t i = 0; i < queueFamilyProperties.size(); i++ ) - { - if ( physicalDevice.getSurfaceSupportKHR( static_cast( i ), *surface ) ) - { - presentIndex = static_cast( i ); - break; - } - } + // found a queue family that supports both graphics and present + queueIndex = qfpIndex; + break; } } - if ( ( graphicsIndex == queueFamilyProperties.size() ) || ( presentIndex == queueFamilyProperties.size() ) ) + if (queueIndex == ~0) { - throw std::runtime_error( "Could not find a queue for graphics or present -> terminating" ); + throw std::runtime_error("Could not find a queue for graphics and present -> terminating"); } // query for Vulkan 1.3 features @@ -276,7 +243,7 @@ class HelloTriangleApplication { // create a Device float queuePriority = 0.0f; - vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = graphicsIndex, .queueCount = 1, .pQueuePriorities = &queuePriority }; + vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = queueIndex, .queueCount = 1, .pQueuePriorities = &queuePriority }; vk::DeviceCreateInfo deviceCreateInfo{ .pNext = &featureChain.get(), .queueCreateInfoCount = 1, .pQueueCreateInfos = &deviceQueueCreateInfo, @@ -284,8 +251,7 @@ class HelloTriangleApplication { .ppEnabledExtensionNames = requiredDeviceExtension.data() }; device = vk::raii::Device( physicalDevice, deviceCreateInfo ); - graphicsQueue = vk::raii::Queue( device, graphicsIndex, 0 ); - presentQueue = vk::raii::Queue( device, presentIndex, 0 ); + queue = vk::raii::Queue( device, queueIndex, 0 ); } void createSwapChain() { @@ -366,7 +332,7 @@ class HelloTriangleApplication { void createCommandPool() { vk::CommandPoolCreateInfo poolInfo{ .flags = vk::CommandPoolCreateFlagBits::eResetCommandBuffer, - .queueFamilyIndex = graphicsIndex }; + .queueFamilyIndex = queueIndex }; commandPool = vk::raii::CommandPool(device, poolInfo); } diff --git a/attachments/15_hello_triangle.cpp b/attachments/15_hello_triangle.cpp index ede44cf0..eedf7ef5 100644 --- a/attachments/15_hello_triangle.cpp +++ b/attachments/15_hello_triangle.cpp @@ -42,18 +42,15 @@ class HelloTriangleApplication { } private: - GLFWwindow* window = nullptr; - - vk::raii::Context context; - vk::raii::Instance instance = nullptr; + GLFWwindow * window = nullptr; + vk::raii::Context context; + vk::raii::Instance instance = nullptr; vk::raii::DebugUtilsMessengerEXT debugMessenger = nullptr; - vk::raii::SurfaceKHR surface = nullptr; - - vk::raii::PhysicalDevice physicalDevice = nullptr; - vk::raii::Device device = nullptr; - - vk::raii::Queue graphicsQueue = nullptr; - vk::raii::Queue presentQueue = nullptr; + vk::raii::SurfaceKHR surface = nullptr; + vk::raii::PhysicalDevice physicalDevice = nullptr; + vk::raii::Device device = nullptr; + uint32_t queueIndex = ~0; + vk::raii::Queue queue = nullptr; vk::raii::SwapchainKHR swapChain = nullptr; std::vector swapChainImages; @@ -66,7 +63,6 @@ class HelloTriangleApplication { vk::raii::CommandPool commandPool = nullptr; vk::raii::CommandBuffer commandBuffer = nullptr; - uint32_t graphicsIndex = 0; vk::raii::Semaphore presentCompleteSemaphore = nullptr; vk::raii::Semaphore renderFinishedSemaphore = nullptr; @@ -229,51 +225,22 @@ class HelloTriangleApplication { } void createLogicalDevice() { - // find the index of the first queue family that supports graphics std::vector queueFamilyProperties = physicalDevice.getQueueFamilyProperties(); - // get the first index into queueFamilyProperties which supports graphics - auto graphicsQueueFamilyProperty = std::ranges::find_if( queueFamilyProperties, []( auto const & qfp ) - { return (qfp.queueFlags & vk::QueueFlagBits::eGraphics) != static_cast(0); } ); - - graphicsIndex = static_cast( std::distance( queueFamilyProperties.begin(), graphicsQueueFamilyProperty ) ); - - // determine a queueFamilyIndex that supports present - // first check if the graphicsIndex is good enough - auto presentIndex = physicalDevice.getSurfaceSupportKHR( graphicsIndex, *surface ) - ? graphicsIndex - : ~0; - if ( presentIndex == queueFamilyProperties.size() ) + // get the first index into queueFamilyProperties which supports both graphics and present + for (uint32_t qfpIndex = 0; qfpIndex < queueFamilyProperties.size(); qfpIndex++) { - // the graphicsIndex doesn't support present -> look for another family index that supports both - // graphics and present - for ( size_t i = 0; i < queueFamilyProperties.size(); i++ ) - { - if ( ( queueFamilyProperties[i].queueFlags & vk::QueueFlagBits::eGraphics ) && - physicalDevice.getSurfaceSupportKHR( static_cast( i ), *surface ) ) - { - graphicsIndex = static_cast( i ); - presentIndex = graphicsIndex; - break; - } - } - if ( presentIndex == queueFamilyProperties.size() ) + if ((queueFamilyProperties[qfpIndex].queueFlags & vk::QueueFlagBits::eGraphics) && + physicalDevice.getSurfaceSupportKHR(qfpIndex, *surface)) { - // there's nothing like a single family index that supports both graphics and present -> look for another - // family index that supports present - for ( size_t i = 0; i < queueFamilyProperties.size(); i++ ) - { - if ( physicalDevice.getSurfaceSupportKHR( static_cast( i ), *surface ) ) - { - presentIndex = static_cast( i ); - break; - } - } + // found a queue family that supports both graphics and present + queueIndex = qfpIndex; + break; } } - if ( ( graphicsIndex == queueFamilyProperties.size() ) || ( presentIndex == queueFamilyProperties.size() ) ) + if (queueIndex == ~0) { - throw std::runtime_error( "Could not find a queue for graphics or present -> terminating" ); + throw std::runtime_error("Could not find a queue for graphics and present -> terminating"); } // query for Vulkan 1.3 features @@ -285,7 +252,7 @@ class HelloTriangleApplication { // create a Device float queuePriority = 0.0f; - vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = graphicsIndex, .queueCount = 1, .pQueuePriorities = &queuePriority }; + vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = queueIndex, .queueCount = 1, .pQueuePriorities = &queuePriority }; vk::DeviceCreateInfo deviceCreateInfo{ .pNext = &featureChain.get(), .queueCreateInfoCount = 1, .pQueueCreateInfos = &deviceQueueCreateInfo, @@ -293,8 +260,7 @@ class HelloTriangleApplication { .ppEnabledExtensionNames = requiredDeviceExtension.data() }; device = vk::raii::Device( physicalDevice, deviceCreateInfo ); - graphicsQueue = vk::raii::Queue( device, graphicsIndex, 0 ); - presentQueue = vk::raii::Queue( device, presentIndex, 0 ); + queue = vk::raii::Queue( device, queueIndex, 0 ); } void createSwapChain() { @@ -375,7 +341,7 @@ class HelloTriangleApplication { void createCommandPool() { vk::CommandPoolCreateInfo poolInfo{ .flags = vk::CommandPoolCreateFlagBits::eResetCommandBuffer, - .queueFamilyIndex = graphicsIndex }; + .queueFamilyIndex = queueIndex }; commandPool = vk::raii::CommandPool(device, poolInfo); } @@ -473,7 +439,7 @@ class HelloTriangleApplication { } void drawFrame() { - presentQueue.waitIdle(); + queue.waitIdle(); auto [result, imageIndex] = swapChain.acquireNextImage( UINT64_MAX, *presentCompleteSemaphore, nullptr ); recordCommandBuffer(imageIndex); @@ -483,13 +449,13 @@ class HelloTriangleApplication { const vk::SubmitInfo submitInfo{ .waitSemaphoreCount = 1, .pWaitSemaphores = &*presentCompleteSemaphore, .pWaitDstStageMask = &waitDestinationStageMask, .commandBufferCount = 1, .pCommandBuffers = &*commandBuffer, .signalSemaphoreCount = 1, .pSignalSemaphores = &*renderFinishedSemaphore }; - graphicsQueue.submit(submitInfo, *drawFence); + queue.submit(submitInfo, *drawFence); while ( vk::Result::eTimeout == device.waitForFences( *drawFence, vk::True, UINT64_MAX ) ) ; const vk::PresentInfoKHR presentInfoKHR{ .waitSemaphoreCount = 1, .pWaitSemaphores = &*renderFinishedSemaphore, .swapchainCount = 1, .pSwapchains = &*swapChain, .pImageIndices = &imageIndex }; - result = presentQueue.presentKHR( presentInfoKHR ); + result = queue.presentKHR( presentInfoKHR ); switch ( result ) { case vk::Result::eSuccess: break; diff --git a/attachments/16_frames_in_flight.cpp b/attachments/16_frames_in_flight.cpp index e385d656..35dedd6c 100644 --- a/attachments/16_frames_in_flight.cpp +++ b/attachments/16_frames_in_flight.cpp @@ -43,18 +43,15 @@ class HelloTriangleApplication { } private: - GLFWwindow* window = nullptr; - - vk::raii::Context context; - vk::raii::Instance instance = nullptr; + GLFWwindow * window = nullptr; + vk::raii::Context context; + vk::raii::Instance instance = nullptr; vk::raii::DebugUtilsMessengerEXT debugMessenger = nullptr; - vk::raii::SurfaceKHR surface = nullptr; - - vk::raii::PhysicalDevice physicalDevice = nullptr; - vk::raii::Device device = nullptr; - - vk::raii::Queue graphicsQueue = nullptr; - vk::raii::Queue presentQueue = nullptr; + vk::raii::SurfaceKHR surface = nullptr; + vk::raii::PhysicalDevice physicalDevice = nullptr; + vk::raii::Device device = nullptr; + uint32_t queueIndex = ~0; + vk::raii::Queue queue = nullptr; vk::raii::SwapchainKHR swapChain = nullptr; std::vector swapChainImages; @@ -67,7 +64,6 @@ class HelloTriangleApplication { vk::raii::CommandPool commandPool = nullptr; std::vector commandBuffers; - uint32_t graphicsIndex = 0; std::vector presentCompleteSemaphore; std::vector renderFinishedSemaphore; @@ -233,51 +229,22 @@ class HelloTriangleApplication { } void createLogicalDevice() { - // find the index of the first queue family that supports graphics std::vector queueFamilyProperties = physicalDevice.getQueueFamilyProperties(); - // get the first index into queueFamilyProperties which supports graphics - auto graphicsQueueFamilyProperty = std::ranges::find_if( queueFamilyProperties, []( auto const & qfp ) - { return (qfp.queueFlags & vk::QueueFlagBits::eGraphics) != static_cast(0); } ); - - graphicsIndex = static_cast( std::distance( queueFamilyProperties.begin(), graphicsQueueFamilyProperty ) ); - - // determine a queueFamilyIndex that supports present - // first check if the graphicsIndex is good enough - auto presentIndex = physicalDevice.getSurfaceSupportKHR( graphicsIndex, *surface ) - ? graphicsIndex - : ~0; - if ( presentIndex == queueFamilyProperties.size() ) + // get the first index into queueFamilyProperties which supports both graphics and present + for (uint32_t qfpIndex = 0; qfpIndex < queueFamilyProperties.size(); qfpIndex++) { - // the graphicsIndex doesn't support present -> look for another family index that supports both - // graphics and present - for ( size_t i = 0; i < queueFamilyProperties.size(); i++ ) - { - if ( ( queueFamilyProperties[i].queueFlags & vk::QueueFlagBits::eGraphics ) && - physicalDevice.getSurfaceSupportKHR( static_cast( i ), *surface ) ) - { - graphicsIndex = static_cast( i ); - presentIndex = graphicsIndex; - break; - } - } - if ( presentIndex == queueFamilyProperties.size() ) + if ((queueFamilyProperties[qfpIndex].queueFlags & vk::QueueFlagBits::eGraphics) && + physicalDevice.getSurfaceSupportKHR(qfpIndex, *surface)) { - // there's nothing like a single family index that supports both graphics and present -> look for another - // family index that supports present - for ( size_t i = 0; i < queueFamilyProperties.size(); i++ ) - { - if ( physicalDevice.getSurfaceSupportKHR( static_cast( i ), *surface ) ) - { - presentIndex = static_cast( i ); - break; - } - } + // found a queue family that supports both graphics and present + queueIndex = qfpIndex; + break; } } - if ( ( graphicsIndex == queueFamilyProperties.size() ) || ( presentIndex == queueFamilyProperties.size() ) ) + if (queueIndex == ~0) { - throw std::runtime_error( "Could not find a queue for graphics or present -> terminating" ); + throw std::runtime_error("Could not find a queue for graphics and present -> terminating"); } // query for Vulkan 1.3 features @@ -289,7 +256,7 @@ class HelloTriangleApplication { // create a Device float queuePriority = 0.0f; - vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = graphicsIndex, .queueCount = 1, .pQueuePriorities = &queuePriority }; + vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = queueIndex, .queueCount = 1, .pQueuePriorities = &queuePriority }; vk::DeviceCreateInfo deviceCreateInfo{ .pNext = &featureChain.get(), .queueCreateInfoCount = 1, .pQueueCreateInfos = &deviceQueueCreateInfo, @@ -297,8 +264,7 @@ class HelloTriangleApplication { .ppEnabledExtensionNames = requiredDeviceExtension.data() }; device = vk::raii::Device( physicalDevice, deviceCreateInfo ); - graphicsQueue = vk::raii::Queue( device, graphicsIndex, 0 ); - presentQueue = vk::raii::Queue( device, presentIndex, 0 ); + queue = vk::raii::Queue( device, queueIndex, 0 ); } void createSwapChain() { @@ -379,7 +345,7 @@ class HelloTriangleApplication { void createCommandPool() { vk::CommandPoolCreateInfo poolInfo{ .flags = vk::CommandPoolCreateFlagBits::eResetCommandBuffer, - .queueFamilyIndex = graphicsIndex }; + .queueFamilyIndex = queueIndex }; commandPool = vk::raii::CommandPool(device, poolInfo); } @@ -498,12 +464,12 @@ class HelloTriangleApplication { const vk::SubmitInfo submitInfo{ .waitSemaphoreCount = 1, .pWaitSemaphores = &*presentCompleteSemaphore[semaphoreIndex], .pWaitDstStageMask = &waitDestinationStageMask, .commandBufferCount = 1, .pCommandBuffers = &*commandBuffers[currentFrame], .signalSemaphoreCount = 1, .pSignalSemaphores = &*renderFinishedSemaphore[imageIndex] }; - graphicsQueue.submit(submitInfo, *inFlightFences[currentFrame]); + queue.submit(submitInfo, *inFlightFences[currentFrame]); const vk::PresentInfoKHR presentInfoKHR{ .waitSemaphoreCount = 1, .pWaitSemaphores = &*renderFinishedSemaphore[imageIndex], .swapchainCount = 1, .pSwapchains = &*swapChain, .pImageIndices = &imageIndex }; - result = presentQueue.presentKHR( presentInfoKHR ); + result = queue.presentKHR( presentInfoKHR ); switch ( result ) { case vk::Result::eSuccess: break; diff --git a/attachments/17_swap_chain_recreation.cpp b/attachments/17_swap_chain_recreation.cpp index dc383952..025d4432 100644 --- a/attachments/17_swap_chain_recreation.cpp +++ b/attachments/17_swap_chain_recreation.cpp @@ -43,18 +43,15 @@ class HelloTriangleApplication { } private: - GLFWwindow* window = nullptr; - - vk::raii::Context context; - vk::raii::Instance instance = nullptr; + GLFWwindow * window = nullptr; + vk::raii::Context context; + vk::raii::Instance instance = nullptr; vk::raii::DebugUtilsMessengerEXT debugMessenger = nullptr; - vk::raii::SurfaceKHR surface = nullptr; - - vk::raii::PhysicalDevice physicalDevice = nullptr; - vk::raii::Device device = nullptr; - - vk::raii::Queue graphicsQueue = nullptr; - vk::raii::Queue presentQueue = nullptr; + vk::raii::SurfaceKHR surface = nullptr; + vk::raii::PhysicalDevice physicalDevice = nullptr; + vk::raii::Device device = nullptr; + uint32_t queueIndex = ~0; + vk::raii::Queue queue = nullptr; vk::raii::SwapchainKHR swapChain = nullptr; std::vector swapChainImages; @@ -67,7 +64,6 @@ class HelloTriangleApplication { vk::raii::CommandPool commandPool = nullptr; std::vector commandBuffers; - uint32_t graphicsIndex = 0; std::vector presentCompleteSemaphore; std::vector renderFinishedSemaphore; @@ -259,51 +255,22 @@ class HelloTriangleApplication { } void createLogicalDevice() { - // find the index of the first queue family that supports graphics std::vector queueFamilyProperties = physicalDevice.getQueueFamilyProperties(); - // get the first index into queueFamilyProperties which supports graphics - auto graphicsQueueFamilyProperty = std::ranges::find_if( queueFamilyProperties, []( auto const & qfp ) - { return (qfp.queueFlags & vk::QueueFlagBits::eGraphics) != static_cast(0); } ); - - graphicsIndex = static_cast( std::distance( queueFamilyProperties.begin(), graphicsQueueFamilyProperty ) ); - - // determine a queueFamilyIndex that supports present - // first check if the graphicsIndex is good enough - auto presentIndex = physicalDevice.getSurfaceSupportKHR( graphicsIndex, *surface ) - ? graphicsIndex - : ~0; - if ( presentIndex == queueFamilyProperties.size() ) + // get the first index into queueFamilyProperties which supports both graphics and present + for (uint32_t qfpIndex = 0; qfpIndex < queueFamilyProperties.size(); qfpIndex++) { - // the graphicsIndex doesn't support present -> look for another family index that supports both - // graphics and present - for ( size_t i = 0; i < queueFamilyProperties.size(); i++ ) - { - if ( ( queueFamilyProperties[i].queueFlags & vk::QueueFlagBits::eGraphics ) && - physicalDevice.getSurfaceSupportKHR( static_cast( i ), *surface ) ) - { - graphicsIndex = static_cast( i ); - presentIndex = graphicsIndex; - break; - } - } - if ( presentIndex == queueFamilyProperties.size() ) + if ((queueFamilyProperties[qfpIndex].queueFlags & vk::QueueFlagBits::eGraphics) && + physicalDevice.getSurfaceSupportKHR(qfpIndex, *surface)) { - // there's nothing like a single family index that supports both graphics and present -> look for another - // family index that supports present - for ( size_t i = 0; i < queueFamilyProperties.size(); i++ ) - { - if ( physicalDevice.getSurfaceSupportKHR( static_cast( i ), *surface ) ) - { - presentIndex = static_cast( i ); - break; - } - } + // found a queue family that supports both graphics and present + queueIndex = qfpIndex; + break; } } - if ( ( graphicsIndex == queueFamilyProperties.size() ) || ( presentIndex == queueFamilyProperties.size() ) ) + if (queueIndex == ~0) { - throw std::runtime_error( "Could not find a queue for graphics or present -> terminating" ); + throw std::runtime_error("Could not find a queue for graphics and present -> terminating"); } // query for Vulkan 1.3 features @@ -315,7 +282,7 @@ class HelloTriangleApplication { // create a Device float queuePriority = 0.0f; - vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = graphicsIndex, .queueCount = 1, .pQueuePriorities = &queuePriority }; + vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = queueIndex, .queueCount = 1, .pQueuePriorities = &queuePriority }; vk::DeviceCreateInfo deviceCreateInfo{ .pNext = &featureChain.get(), .queueCreateInfoCount = 1, .pQueueCreateInfos = &deviceQueueCreateInfo, @@ -323,8 +290,7 @@ class HelloTriangleApplication { .ppEnabledExtensionNames = requiredDeviceExtension.data() }; device = vk::raii::Device( physicalDevice, deviceCreateInfo ); - graphicsQueue = vk::raii::Queue( device, graphicsIndex, 0 ); - presentQueue = vk::raii::Queue( device, presentIndex, 0 ); + queue = vk::raii::Queue( device, queueIndex, 0 ); } void createSwapChain() { @@ -405,7 +371,7 @@ class HelloTriangleApplication { void createCommandPool() { vk::CommandPoolCreateInfo poolInfo{ .flags = vk::CommandPoolCreateFlagBits::eResetCommandBuffer, - .queueFamilyIndex = graphicsIndex }; + .queueFamilyIndex = queueIndex }; commandPool = vk::raii::CommandPool(device, poolInfo); } @@ -533,12 +499,12 @@ class HelloTriangleApplication { const vk::SubmitInfo submitInfo{ .waitSemaphoreCount = 1, .pWaitSemaphores = &*presentCompleteSemaphore[semaphoreIndex], .pWaitDstStageMask = &waitDestinationStageMask, .commandBufferCount = 1, .pCommandBuffers = &*commandBuffers[currentFrame], .signalSemaphoreCount = 1, .pSignalSemaphores = &*renderFinishedSemaphore[imageIndex] }; - graphicsQueue.submit(submitInfo, *inFlightFences[currentFrame]); + queue.submit(submitInfo, *inFlightFences[currentFrame]); const vk::PresentInfoKHR presentInfoKHR{ .waitSemaphoreCount = 1, .pWaitSemaphores = &*renderFinishedSemaphore[imageIndex], .swapchainCount = 1, .pSwapchains = &*swapChain, .pImageIndices = &imageIndex }; - result = presentQueue.presentKHR( presentInfoKHR ); + result = queue.presentKHR( presentInfoKHR ); if (result == vk::Result::eErrorOutOfDateKHR || result == vk::Result::eSuboptimalKHR || framebufferResized) { framebufferResized = false; recreateSwapChain(); diff --git a/attachments/18_vertex_input.cpp b/attachments/18_vertex_input.cpp index f19569f8..f9ef2dc8 100644 --- a/attachments/18_vertex_input.cpp +++ b/attachments/18_vertex_input.cpp @@ -67,18 +67,15 @@ class HelloTriangleApplication { } private: - GLFWwindow* window = nullptr; - - vk::raii::Context context; - vk::raii::Instance instance = nullptr; + GLFWwindow * window = nullptr; + vk::raii::Context context; + vk::raii::Instance instance = nullptr; vk::raii::DebugUtilsMessengerEXT debugMessenger = nullptr; - vk::raii::SurfaceKHR surface = nullptr; - - vk::raii::PhysicalDevice physicalDevice = nullptr; - vk::raii::Device device = nullptr; - - vk::raii::Queue graphicsQueue = nullptr; - vk::raii::Queue presentQueue = nullptr; + vk::raii::SurfaceKHR surface = nullptr; + vk::raii::PhysicalDevice physicalDevice = nullptr; + vk::raii::Device device = nullptr; + uint32_t queueIndex = ~0; + vk::raii::Queue queue = nullptr; vk::raii::SwapchainKHR swapChain = nullptr; std::vector swapChainImages; @@ -91,7 +88,6 @@ class HelloTriangleApplication { vk::raii::CommandPool commandPool = nullptr; std::vector commandBuffers; - uint32_t graphicsIndex = 0; std::vector presentCompleteSemaphore; std::vector renderFinishedSemaphore; @@ -283,51 +279,22 @@ class HelloTriangleApplication { } void createLogicalDevice() { - // find the index of the first queue family that supports graphics std::vector queueFamilyProperties = physicalDevice.getQueueFamilyProperties(); - // get the first index into queueFamilyProperties which supports graphics - auto graphicsQueueFamilyProperty = std::ranges::find_if( queueFamilyProperties, []( auto const & qfp ) - { return (qfp.queueFlags & vk::QueueFlagBits::eGraphics) != static_cast(0); } ); - - graphicsIndex = static_cast( std::distance( queueFamilyProperties.begin(), graphicsQueueFamilyProperty ) ); - - // determine a queueFamilyIndex that supports present - // first check if the graphicsIndex is good enough - auto presentIndex = physicalDevice.getSurfaceSupportKHR( graphicsIndex, *surface ) - ? graphicsIndex - : ~0; - if ( presentIndex == queueFamilyProperties.size() ) + // get the first index into queueFamilyProperties which supports both graphics and present + for (uint32_t qfpIndex = 0; qfpIndex < queueFamilyProperties.size(); qfpIndex++) { - // the graphicsIndex doesn't support present -> look for another family index that supports both - // graphics and present - for ( size_t i = 0; i < queueFamilyProperties.size(); i++ ) - { - if ( ( queueFamilyProperties[i].queueFlags & vk::QueueFlagBits::eGraphics ) && - physicalDevice.getSurfaceSupportKHR( static_cast( i ), *surface ) ) - { - graphicsIndex = static_cast( i ); - presentIndex = graphicsIndex; - break; - } - } - if ( presentIndex == queueFamilyProperties.size() ) + if ((queueFamilyProperties[qfpIndex].queueFlags & vk::QueueFlagBits::eGraphics) && + physicalDevice.getSurfaceSupportKHR(qfpIndex, *surface)) { - // there's nothing like a single family index that supports both graphics and present -> look for another - // family index that supports present - for ( size_t i = 0; i < queueFamilyProperties.size(); i++ ) - { - if ( physicalDevice.getSurfaceSupportKHR( static_cast( i ), *surface ) ) - { - presentIndex = static_cast( i ); - break; - } - } + // found a queue family that supports both graphics and present + queueIndex = qfpIndex; + break; } } - if ( ( graphicsIndex == queueFamilyProperties.size() ) || ( presentIndex == queueFamilyProperties.size() ) ) + if (queueIndex == ~0) { - throw std::runtime_error( "Could not find a queue for graphics or present -> terminating" ); + throw std::runtime_error("Could not find a queue for graphics and present -> terminating"); } // query for Vulkan 1.3 features @@ -339,7 +306,7 @@ class HelloTriangleApplication { // create a Device float queuePriority = 0.0f; - vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = graphicsIndex, .queueCount = 1, .pQueuePriorities = &queuePriority }; + vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = queueIndex, .queueCount = 1, .pQueuePriorities = &queuePriority }; vk::DeviceCreateInfo deviceCreateInfo{ .pNext = &featureChain.get(), .queueCreateInfoCount = 1, .pQueueCreateInfos = &deviceQueueCreateInfo, @@ -347,8 +314,7 @@ class HelloTriangleApplication { .ppEnabledExtensionNames = requiredDeviceExtension.data() }; device = vk::raii::Device( physicalDevice, deviceCreateInfo ); - graphicsQueue = vk::raii::Queue( device, graphicsIndex, 0 ); - presentQueue = vk::raii::Queue( device, presentIndex, 0 ); + queue = vk::raii::Queue( device, queueIndex, 0 ); } void createSwapChain() { @@ -432,7 +398,7 @@ class HelloTriangleApplication { void createCommandPool() { vk::CommandPoolCreateInfo poolInfo{ .flags = vk::CommandPoolCreateFlagBits::eResetCommandBuffer, - .queueFamilyIndex = graphicsIndex }; + .queueFamilyIndex = queueIndex }; commandPool = vk::raii::CommandPool(device, poolInfo); } @@ -560,12 +526,12 @@ class HelloTriangleApplication { const vk::SubmitInfo submitInfo{ .waitSemaphoreCount = 1, .pWaitSemaphores = &*presentCompleteSemaphore[semaphoreIndex], .pWaitDstStageMask = &waitDestinationStageMask, .commandBufferCount = 1, .pCommandBuffers = &*commandBuffers[currentFrame], .signalSemaphoreCount = 1, .pSignalSemaphores = &*renderFinishedSemaphore[imageIndex] }; - graphicsQueue.submit(submitInfo, *inFlightFences[currentFrame]); + queue.submit(submitInfo, *inFlightFences[currentFrame]); const vk::PresentInfoKHR presentInfoKHR{ .waitSemaphoreCount = 1, .pWaitSemaphores = &*renderFinishedSemaphore[imageIndex], .swapchainCount = 1, .pSwapchains = &*swapChain, .pImageIndices = &imageIndex }; - result = presentQueue.presentKHR( presentInfoKHR ); + result = queue.presentKHR( presentInfoKHR ); if (result == vk::Result::eErrorOutOfDateKHR || result == vk::Result::eSuboptimalKHR || framebufferResized) { framebufferResized = false; recreateSwapChain(); diff --git a/attachments/19_vertex_buffer.cpp b/attachments/19_vertex_buffer.cpp index 277810a4..a038eaa0 100644 --- a/attachments/19_vertex_buffer.cpp +++ b/attachments/19_vertex_buffer.cpp @@ -67,18 +67,15 @@ class HelloTriangleApplication { } private: - GLFWwindow* window = nullptr; - - vk::raii::Context context; - vk::raii::Instance instance = nullptr; + GLFWwindow * window = nullptr; + vk::raii::Context context; + vk::raii::Instance instance = nullptr; vk::raii::DebugUtilsMessengerEXT debugMessenger = nullptr; - vk::raii::SurfaceKHR surface = nullptr; - - vk::raii::PhysicalDevice physicalDevice = nullptr; - vk::raii::Device device = nullptr; - - vk::raii::Queue graphicsQueue = nullptr; - vk::raii::Queue presentQueue = nullptr; + vk::raii::SurfaceKHR surface = nullptr; + vk::raii::PhysicalDevice physicalDevice = nullptr; + vk::raii::Device device = nullptr; + uint32_t queueIndex = ~0; + vk::raii::Queue queue = nullptr; vk::raii::SwapchainKHR swapChain = nullptr; std::vector swapChainImages; @@ -94,7 +91,6 @@ class HelloTriangleApplication { vk::raii::CommandPool commandPool = nullptr; std::vector commandBuffers; - uint32_t graphicsIndex = 0; std::vector presentCompleteSemaphore; std::vector renderFinishedSemaphore; @@ -287,51 +283,22 @@ class HelloTriangleApplication { } void createLogicalDevice() { - // find the index of the first queue family that supports graphics std::vector queueFamilyProperties = physicalDevice.getQueueFamilyProperties(); - // get the first index into queueFamilyProperties which supports graphics - auto graphicsQueueFamilyProperty = std::ranges::find_if( queueFamilyProperties, []( auto const & qfp ) - { return (qfp.queueFlags & vk::QueueFlagBits::eGraphics) != static_cast(0); } ); - - graphicsIndex = static_cast( std::distance( queueFamilyProperties.begin(), graphicsQueueFamilyProperty ) ); - - // determine a queueFamilyIndex that supports present - // first check if the graphicsIndex is good enough - auto presentIndex = physicalDevice.getSurfaceSupportKHR( graphicsIndex, *surface ) - ? graphicsIndex - : ~0; - if ( presentIndex == queueFamilyProperties.size() ) + // get the first index into queueFamilyProperties which supports both graphics and present + for (uint32_t qfpIndex = 0; qfpIndex < queueFamilyProperties.size(); qfpIndex++) { - // the graphicsIndex doesn't support present -> look for another family index that supports both - // graphics and present - for ( size_t i = 0; i < queueFamilyProperties.size(); i++ ) - { - if ( ( queueFamilyProperties[i].queueFlags & vk::QueueFlagBits::eGraphics ) && - physicalDevice.getSurfaceSupportKHR( static_cast( i ), *surface ) ) - { - graphicsIndex = static_cast( i ); - presentIndex = graphicsIndex; - break; - } - } - if ( presentIndex == queueFamilyProperties.size() ) + if ((queueFamilyProperties[qfpIndex].queueFlags & vk::QueueFlagBits::eGraphics) && + physicalDevice.getSurfaceSupportKHR(qfpIndex, *surface)) { - // there's nothing like a single family index that supports both graphics and present -> look for another - // family index that supports present - for ( size_t i = 0; i < queueFamilyProperties.size(); i++ ) - { - if ( physicalDevice.getSurfaceSupportKHR( static_cast( i ), *surface ) ) - { - presentIndex = static_cast( i ); - break; - } - } + // found a queue family that supports both graphics and present + queueIndex = qfpIndex; + break; } } - if ( ( graphicsIndex == queueFamilyProperties.size() ) || ( presentIndex == queueFamilyProperties.size() ) ) + if (queueIndex == ~0) { - throw std::runtime_error( "Could not find a queue for graphics or present -> terminating" ); + throw std::runtime_error("Could not find a queue for graphics and present -> terminating"); } // query for Vulkan 1.3 features @@ -343,7 +310,7 @@ class HelloTriangleApplication { // create a Device float queuePriority = 0.0f; - vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = graphicsIndex, .queueCount = 1, .pQueuePriorities = &queuePriority }; + vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = queueIndex, .queueCount = 1, .pQueuePriorities = &queuePriority }; vk::DeviceCreateInfo deviceCreateInfo{ .pNext = &featureChain.get(), .queueCreateInfoCount = 1, .pQueueCreateInfos = &deviceQueueCreateInfo, @@ -351,8 +318,7 @@ class HelloTriangleApplication { .ppEnabledExtensionNames = requiredDeviceExtension.data() }; device = vk::raii::Device( physicalDevice, deviceCreateInfo ); - graphicsQueue = vk::raii::Queue( device, graphicsIndex, 0 ); - presentQueue = vk::raii::Queue( device, presentIndex, 0 ); + queue = vk::raii::Queue( device, queueIndex, 0 ); } void createSwapChain() { @@ -434,7 +400,7 @@ class HelloTriangleApplication { void createCommandPool() { vk::CommandPoolCreateInfo poolInfo{ .flags = vk::CommandPoolCreateFlagBits::eResetCommandBuffer, - .queueFamilyIndex = graphicsIndex }; + .queueFamilyIndex = queueIndex }; commandPool = vk::raii::CommandPool(device, poolInfo); } @@ -590,12 +556,12 @@ class HelloTriangleApplication { const vk::SubmitInfo submitInfo{ .waitSemaphoreCount = 1, .pWaitSemaphores = &*presentCompleteSemaphore[semaphoreIndex], .pWaitDstStageMask = &waitDestinationStageMask, .commandBufferCount = 1, .pCommandBuffers = &*commandBuffers[currentFrame], .signalSemaphoreCount = 1, .pSignalSemaphores = &*renderFinishedSemaphore[imageIndex] }; - graphicsQueue.submit(submitInfo, *inFlightFences[currentFrame]); + queue.submit(submitInfo, *inFlightFences[currentFrame]); const vk::PresentInfoKHR presentInfoKHR{ .waitSemaphoreCount = 1, .pWaitSemaphores = &*renderFinishedSemaphore[imageIndex], .swapchainCount = 1, .pSwapchains = &*swapChain, .pImageIndices = &imageIndex }; - result = presentQueue.presentKHR( presentInfoKHR ); + result = queue.presentKHR( presentInfoKHR ); if (result == vk::Result::eErrorOutOfDateKHR || result == vk::Result::eSuboptimalKHR || framebufferResized) { framebufferResized = false; recreateSwapChain(); diff --git a/attachments/20_staging_buffer.cpp b/attachments/20_staging_buffer.cpp index 7a394a0a..ac0a61e0 100644 --- a/attachments/20_staging_buffer.cpp +++ b/attachments/20_staging_buffer.cpp @@ -67,18 +67,15 @@ class HelloTriangleApplication { } private: - GLFWwindow* window = nullptr; - - vk::raii::Context context; - vk::raii::Instance instance = nullptr; + GLFWwindow * window = nullptr; + vk::raii::Context context; + vk::raii::Instance instance = nullptr; vk::raii::DebugUtilsMessengerEXT debugMessenger = nullptr; - vk::raii::SurfaceKHR surface = nullptr; - - vk::raii::PhysicalDevice physicalDevice = nullptr; - vk::raii::Device device = nullptr; - - vk::raii::Queue graphicsQueue = nullptr; - vk::raii::Queue presentQueue = nullptr; + vk::raii::SurfaceKHR surface = nullptr; + vk::raii::PhysicalDevice physicalDevice = nullptr; + vk::raii::Device device = nullptr; + uint32_t queueIndex = ~0; + vk::raii::Queue queue = nullptr; vk::raii::SwapchainKHR swapChain = nullptr; std::vector swapChainImages; @@ -94,7 +91,6 @@ class HelloTriangleApplication { vk::raii::CommandPool commandPool = nullptr; std::vector commandBuffers; - uint32_t graphicsIndex = 0; std::vector presentCompleteSemaphore; std::vector renderFinishedSemaphore; @@ -287,51 +283,22 @@ class HelloTriangleApplication { } void createLogicalDevice() { - // find the index of the first queue family that supports graphics std::vector queueFamilyProperties = physicalDevice.getQueueFamilyProperties(); - // get the first index into queueFamilyProperties which supports graphics - auto graphicsQueueFamilyProperty = std::ranges::find_if( queueFamilyProperties, []( auto const & qfp ) - { return (qfp.queueFlags & vk::QueueFlagBits::eGraphics) != static_cast(0); } ); - - graphicsIndex = static_cast( std::distance( queueFamilyProperties.begin(), graphicsQueueFamilyProperty ) ); - - // determine a queueFamilyIndex that supports present - // first check if the graphicsIndex is good enough - auto presentIndex = physicalDevice.getSurfaceSupportKHR( graphicsIndex, *surface ) - ? graphicsIndex - : ~0; - if ( presentIndex == queueFamilyProperties.size() ) + // get the first index into queueFamilyProperties which supports both graphics and present + for (uint32_t qfpIndex = 0; qfpIndex < queueFamilyProperties.size(); qfpIndex++) { - // the graphicsIndex doesn't support present -> look for another family index that supports both - // graphics and present - for ( size_t i = 0; i < queueFamilyProperties.size(); i++ ) - { - if ( ( queueFamilyProperties[i].queueFlags & vk::QueueFlagBits::eGraphics ) && - physicalDevice.getSurfaceSupportKHR( static_cast( i ), *surface ) ) - { - graphicsIndex = static_cast( i ); - presentIndex = graphicsIndex; - break; - } - } - if ( presentIndex == queueFamilyProperties.size() ) + if ((queueFamilyProperties[qfpIndex].queueFlags & vk::QueueFlagBits::eGraphics) && + physicalDevice.getSurfaceSupportKHR(qfpIndex, *surface)) { - // there's nothing like a single family index that supports both graphics and present -> look for another - // family index that supports present - for ( size_t i = 0; i < queueFamilyProperties.size(); i++ ) - { - if ( physicalDevice.getSurfaceSupportKHR( static_cast( i ), *surface ) ) - { - presentIndex = static_cast( i ); - break; - } - } + // found a queue family that supports both graphics and present + queueIndex = qfpIndex; + break; } } - if ( ( graphicsIndex == queueFamilyProperties.size() ) || ( presentIndex == queueFamilyProperties.size() ) ) + if (queueIndex == ~0) { - throw std::runtime_error( "Could not find a queue for graphics or present -> terminating" ); + throw std::runtime_error("Could not find a queue for graphics and present -> terminating"); } // query for Vulkan 1.3 features @@ -343,7 +310,7 @@ class HelloTriangleApplication { // create a Device float queuePriority = 0.0f; - vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = graphicsIndex, .queueCount = 1, .pQueuePriorities = &queuePriority }; + vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = queueIndex, .queueCount = 1, .pQueuePriorities = &queuePriority }; vk::DeviceCreateInfo deviceCreateInfo{ .pNext = &featureChain.get(), .queueCreateInfoCount = 1, .pQueueCreateInfos = &deviceQueueCreateInfo, @@ -351,8 +318,7 @@ class HelloTriangleApplication { .ppEnabledExtensionNames = requiredDeviceExtension.data() }; device = vk::raii::Device( physicalDevice, deviceCreateInfo ); - graphicsQueue = vk::raii::Queue( device, graphicsIndex, 0 ); - presentQueue = vk::raii::Queue( device, presentIndex, 0 ); + queue = vk::raii::Queue( device, queueIndex, 0 ); } void createSwapChain() { @@ -434,7 +400,7 @@ class HelloTriangleApplication { void createCommandPool() { vk::CommandPoolCreateInfo poolInfo{ .flags = vk::CommandPoolCreateFlagBits::eResetCommandBuffer, - .queueFamilyIndex = graphicsIndex }; + .queueFamilyIndex = queueIndex }; commandPool = vk::raii::CommandPool(device, poolInfo); } @@ -468,8 +434,8 @@ class HelloTriangleApplication { commandCopyBuffer.begin(vk::CommandBufferBeginInfo { .flags = vk::CommandBufferUsageFlagBits::eOneTimeSubmit }); commandCopyBuffer.copyBuffer(*srcBuffer, *dstBuffer, vk::BufferCopy(0, 0, size)); commandCopyBuffer.end(); - graphicsQueue.submit(vk::SubmitInfo{ .commandBufferCount = 1, .pCommandBuffers = &*commandCopyBuffer }, nullptr); - graphicsQueue.waitIdle(); + queue.submit(vk::SubmitInfo{ .commandBufferCount = 1, .pCommandBuffers = &*commandCopyBuffer }, nullptr); + queue.waitIdle(); } uint32_t findMemoryType(uint32_t typeFilter, vk::MemoryPropertyFlags properties) { @@ -609,12 +575,12 @@ class HelloTriangleApplication { const vk::SubmitInfo submitInfo{ .waitSemaphoreCount = 1, .pWaitSemaphores = &*presentCompleteSemaphore[semaphoreIndex], .pWaitDstStageMask = &waitDestinationStageMask, .commandBufferCount = 1, .pCommandBuffers = &*commandBuffers[currentFrame], .signalSemaphoreCount = 1, .pSignalSemaphores = &*renderFinishedSemaphore[imageIndex] }; - graphicsQueue.submit(submitInfo, *inFlightFences[currentFrame]); + queue.submit(submitInfo, *inFlightFences[currentFrame]); const vk::PresentInfoKHR presentInfoKHR{ .waitSemaphoreCount = 1, .pWaitSemaphores = &*renderFinishedSemaphore[imageIndex], .swapchainCount = 1, .pSwapchains = &*swapChain, .pImageIndices = &imageIndex }; - result = presentQueue.presentKHR( presentInfoKHR ); + result = queue.presentKHR( presentInfoKHR ); if (result == vk::Result::eErrorOutOfDateKHR || result == vk::Result::eSuboptimalKHR || framebufferResized) { framebufferResized = false; recreateSwapChain(); diff --git a/attachments/21_index_buffer.cpp b/attachments/21_index_buffer.cpp index 2d8b07da..87109a01 100644 --- a/attachments/21_index_buffer.cpp +++ b/attachments/21_index_buffer.cpp @@ -72,18 +72,15 @@ class HelloTriangleApplication { } private: - GLFWwindow* window = nullptr; - - vk::raii::Context context; - vk::raii::Instance instance = nullptr; + GLFWwindow * window = nullptr; + vk::raii::Context context; + vk::raii::Instance instance = nullptr; vk::raii::DebugUtilsMessengerEXT debugMessenger = nullptr; - vk::raii::SurfaceKHR surface = nullptr; - - vk::raii::PhysicalDevice physicalDevice = nullptr; - vk::raii::Device device = nullptr; - - vk::raii::Queue graphicsQueue = nullptr; - vk::raii::Queue presentQueue = nullptr; + vk::raii::SurfaceKHR surface = nullptr; + vk::raii::PhysicalDevice physicalDevice = nullptr; + vk::raii::Device device = nullptr; + uint32_t queueIndex = ~0; + vk::raii::Queue queue = nullptr; vk::raii::SwapchainKHR swapChain = nullptr; std::vector swapChainImages; @@ -101,7 +98,6 @@ class HelloTriangleApplication { vk::raii::CommandPool commandPool = nullptr; std::vector commandBuffers; - uint32_t graphicsIndex = 0; std::vector presentCompleteSemaphore; std::vector renderFinishedSemaphore; @@ -296,51 +292,22 @@ class HelloTriangleApplication { } void createLogicalDevice() { - // find the index of the first queue family that supports graphics std::vector queueFamilyProperties = physicalDevice.getQueueFamilyProperties(); - // get the first index into queueFamilyProperties which supports graphics - auto graphicsQueueFamilyProperty = std::ranges::find_if( queueFamilyProperties, []( auto const & qfp ) - { return (qfp.queueFlags & vk::QueueFlagBits::eGraphics) != static_cast(0); } ); - - graphicsIndex = static_cast( std::distance( queueFamilyProperties.begin(), graphicsQueueFamilyProperty ) ); - - // determine a queueFamilyIndex that supports present - // first check if the graphicsIndex is good enough - auto presentIndex = physicalDevice.getSurfaceSupportKHR( graphicsIndex, *surface ) - ? graphicsIndex - : ~0; - if ( presentIndex == queueFamilyProperties.size() ) + // get the first index into queueFamilyProperties which supports both graphics and present + for (uint32_t qfpIndex = 0; qfpIndex < queueFamilyProperties.size(); qfpIndex++) { - // the graphicsIndex doesn't support present -> look for another family index that supports both - // graphics and present - for ( size_t i = 0; i < queueFamilyProperties.size(); i++ ) - { - if ( ( queueFamilyProperties[i].queueFlags & vk::QueueFlagBits::eGraphics ) && - physicalDevice.getSurfaceSupportKHR( static_cast( i ), *surface ) ) - { - graphicsIndex = static_cast( i ); - presentIndex = graphicsIndex; - break; - } - } - if ( presentIndex == queueFamilyProperties.size() ) + if ((queueFamilyProperties[qfpIndex].queueFlags & vk::QueueFlagBits::eGraphics) && + physicalDevice.getSurfaceSupportKHR(qfpIndex, *surface)) { - // there's nothing like a single family index that supports both graphics and present -> look for another - // family index that supports present - for ( size_t i = 0; i < queueFamilyProperties.size(); i++ ) - { - if ( physicalDevice.getSurfaceSupportKHR( static_cast( i ), *surface ) ) - { - presentIndex = static_cast( i ); - break; - } - } + // found a queue family that supports both graphics and present + queueIndex = qfpIndex; + break; } } - if ( ( graphicsIndex == queueFamilyProperties.size() ) || ( presentIndex == queueFamilyProperties.size() ) ) + if (queueIndex == ~0) { - throw std::runtime_error( "Could not find a queue for graphics or present -> terminating" ); + throw std::runtime_error("Could not find a queue for graphics and present -> terminating"); } // query for Vulkan 1.3 features @@ -352,7 +319,7 @@ class HelloTriangleApplication { // create a Device float queuePriority = 0.0f; - vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = graphicsIndex, .queueCount = 1, .pQueuePriorities = &queuePriority }; + vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = queueIndex, .queueCount = 1, .pQueuePriorities = &queuePriority }; vk::DeviceCreateInfo deviceCreateInfo{ .pNext = &featureChain.get(), .queueCreateInfoCount = 1, .pQueueCreateInfos = &deviceQueueCreateInfo, @@ -360,8 +327,7 @@ class HelloTriangleApplication { .ppEnabledExtensionNames = requiredDeviceExtension.data() }; device = vk::raii::Device( physicalDevice, deviceCreateInfo ); - graphicsQueue = vk::raii::Queue( device, graphicsIndex, 0 ); - presentQueue = vk::raii::Queue( device, presentIndex, 0 ); + queue = vk::raii::Queue( device, queueIndex, 0 ); } void createSwapChain() { @@ -443,7 +409,7 @@ class HelloTriangleApplication { void createCommandPool() { vk::CommandPoolCreateInfo poolInfo{ .flags = vk::CommandPoolCreateFlagBits::eResetCommandBuffer, - .queueFamilyIndex = graphicsIndex }; + .queueFamilyIndex = queueIndex }; commandPool = vk::raii::CommandPool(device, poolInfo); } @@ -493,8 +459,8 @@ class HelloTriangleApplication { commandCopyBuffer.begin(vk::CommandBufferBeginInfo { .flags = vk::CommandBufferUsageFlagBits::eOneTimeSubmit }); commandCopyBuffer.copyBuffer(*srcBuffer, *dstBuffer, vk::BufferCopy(0, 0, size)); commandCopyBuffer.end(); - graphicsQueue.submit(vk::SubmitInfo{ .commandBufferCount = 1, .pCommandBuffers = &*commandCopyBuffer }, nullptr); - graphicsQueue.waitIdle(); + queue.submit(vk::SubmitInfo{ .commandBufferCount = 1, .pCommandBuffers = &*commandCopyBuffer }, nullptr); + queue.waitIdle(); } uint32_t findMemoryType(uint32_t typeFilter, vk::MemoryPropertyFlags properties) { @@ -635,12 +601,12 @@ class HelloTriangleApplication { const vk::SubmitInfo submitInfo{ .waitSemaphoreCount = 1, .pWaitSemaphores = &*presentCompleteSemaphore[semaphoreIndex], .pWaitDstStageMask = &waitDestinationStageMask, .commandBufferCount = 1, .pCommandBuffers = &*commandBuffers[currentFrame], .signalSemaphoreCount = 1, .pSignalSemaphores = &*renderFinishedSemaphore[imageIndex] }; - graphicsQueue.submit(submitInfo, *inFlightFences[currentFrame]); + queue.submit(submitInfo, *inFlightFences[currentFrame]); const vk::PresentInfoKHR presentInfoKHR{ .waitSemaphoreCount = 1, .pWaitSemaphores = &*renderFinishedSemaphore[imageIndex], .swapchainCount = 1, .pSwapchains = &*swapChain, .pImageIndices = &imageIndex }; - result = presentQueue.presentKHR( presentInfoKHR ); + result = queue.presentKHR( presentInfoKHR ); if (result == vk::Result::eErrorOutOfDateKHR || result == vk::Result::eSuboptimalKHR || framebufferResized) { framebufferResized = false; recreateSwapChain(); diff --git a/attachments/22_descriptor_layout.cpp b/attachments/22_descriptor_layout.cpp index fdcb0c8a..85c038d8 100644 --- a/attachments/22_descriptor_layout.cpp +++ b/attachments/22_descriptor_layout.cpp @@ -82,18 +82,15 @@ class HelloTriangleApplication { } private: - GLFWwindow* window = nullptr; - - vk::raii::Context context; - vk::raii::Instance instance = nullptr; + GLFWwindow * window = nullptr; + vk::raii::Context context; + vk::raii::Instance instance = nullptr; vk::raii::DebugUtilsMessengerEXT debugMessenger = nullptr; - vk::raii::SurfaceKHR surface = nullptr; - - vk::raii::PhysicalDevice physicalDevice = nullptr; - vk::raii::Device device = nullptr; - - vk::raii::Queue graphicsQueue = nullptr; - vk::raii::Queue presentQueue = nullptr; + vk::raii::SurfaceKHR surface = nullptr; + vk::raii::PhysicalDevice physicalDevice = nullptr; + vk::raii::Device device = nullptr; + uint32_t queueIndex = ~0; + vk::raii::Queue queue = nullptr; vk::raii::SwapchainKHR swapChain = nullptr; std::vector swapChainImages; @@ -116,7 +113,6 @@ class HelloTriangleApplication { vk::raii::CommandPool commandPool = nullptr; std::vector commandBuffers; - uint32_t graphicsIndex = 0; std::vector presentCompleteSemaphore; std::vector renderFinishedSemaphore; @@ -312,51 +308,22 @@ class HelloTriangleApplication { } void createLogicalDevice() { - // find the index of the first queue family that supports graphics std::vector queueFamilyProperties = physicalDevice.getQueueFamilyProperties(); - // get the first index into queueFamilyProperties which supports graphics - auto graphicsQueueFamilyProperty = std::ranges::find_if( queueFamilyProperties, []( auto const & qfp ) - { return (qfp.queueFlags & vk::QueueFlagBits::eGraphics) != static_cast(0); } ); - - graphicsIndex = static_cast( std::distance( queueFamilyProperties.begin(), graphicsQueueFamilyProperty ) ); - - // determine a queueFamilyIndex that supports present - // first check if the graphicsIndex is good enough - auto presentIndex = physicalDevice.getSurfaceSupportKHR( graphicsIndex, *surface ) - ? graphicsIndex - : ~0; - if ( presentIndex == queueFamilyProperties.size() ) + // get the first index into queueFamilyProperties which supports both graphics and present + for (uint32_t qfpIndex = 0; qfpIndex < queueFamilyProperties.size(); qfpIndex++) { - // the graphicsIndex doesn't support present -> look for another family index that supports both - // graphics and present - for ( size_t i = 0; i < queueFamilyProperties.size(); i++ ) - { - if ( ( queueFamilyProperties[i].queueFlags & vk::QueueFlagBits::eGraphics ) && - physicalDevice.getSurfaceSupportKHR( static_cast( i ), *surface ) ) - { - graphicsIndex = static_cast( i ); - presentIndex = graphicsIndex; - break; - } - } - if ( presentIndex == queueFamilyProperties.size() ) + if ((queueFamilyProperties[qfpIndex].queueFlags & vk::QueueFlagBits::eGraphics) && + physicalDevice.getSurfaceSupportKHR(qfpIndex, *surface)) { - // there's nothing like a single family index that supports both graphics and present -> look for another - // family index that supports present - for ( size_t i = 0; i < queueFamilyProperties.size(); i++ ) - { - if ( physicalDevice.getSurfaceSupportKHR( static_cast( i ), *surface ) ) - { - presentIndex = static_cast( i ); - break; - } - } + // found a queue family that supports both graphics and present + queueIndex = qfpIndex; + break; } } - if ( ( graphicsIndex == queueFamilyProperties.size() ) || ( presentIndex == queueFamilyProperties.size() ) ) + if (queueIndex == ~0) { - throw std::runtime_error( "Could not find a queue for graphics or present -> terminating" ); + throw std::runtime_error("Could not find a queue for graphics and present -> terminating"); } // query for Vulkan 1.3 features @@ -368,7 +335,7 @@ class HelloTriangleApplication { // create a Device float queuePriority = 0.0f; - vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = graphicsIndex, .queueCount = 1, .pQueuePriorities = &queuePriority }; + vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = queueIndex, .queueCount = 1, .pQueuePriorities = &queuePriority }; vk::DeviceCreateInfo deviceCreateInfo{ .pNext = &featureChain.get(), .queueCreateInfoCount = 1, .pQueueCreateInfos = &deviceQueueCreateInfo, @@ -376,8 +343,7 @@ class HelloTriangleApplication { .ppEnabledExtensionNames = requiredDeviceExtension.data() }; device = vk::raii::Device( physicalDevice, deviceCreateInfo ); - graphicsQueue = vk::raii::Queue( device, graphicsIndex, 0 ); - presentQueue = vk::raii::Queue( device, presentIndex, 0 ); + queue = vk::raii::Queue( device, queueIndex, 0 ); } void createSwapChain() { @@ -465,7 +431,7 @@ class HelloTriangleApplication { void createCommandPool() { vk::CommandPoolCreateInfo poolInfo{ .flags = vk::CommandPoolCreateFlagBits::eResetCommandBuffer, - .queueFamilyIndex = graphicsIndex }; + .queueFamilyIndex = queueIndex }; commandPool = vk::raii::CommandPool(device, poolInfo); } @@ -531,8 +497,8 @@ class HelloTriangleApplication { commandCopyBuffer.begin(vk::CommandBufferBeginInfo { .flags = vk::CommandBufferUsageFlagBits::eOneTimeSubmit }); commandCopyBuffer.copyBuffer(*srcBuffer, *dstBuffer, vk::BufferCopy(0, 0, size)); commandCopyBuffer.end(); - graphicsQueue.submit(vk::SubmitInfo{ .commandBufferCount = 1, .pCommandBuffers = &*commandCopyBuffer }, nullptr); - graphicsQueue.waitIdle(); + queue.submit(vk::SubmitInfo{ .commandBufferCount = 1, .pCommandBuffers = &*commandCopyBuffer }, nullptr); + queue.waitIdle(); } uint32_t findMemoryType(uint32_t typeFilter, vk::MemoryPropertyFlags properties) { @@ -689,12 +655,12 @@ class HelloTriangleApplication { const vk::SubmitInfo submitInfo{ .waitSemaphoreCount = 1, .pWaitSemaphores = &*presentCompleteSemaphore[semaphoreIndex], .pWaitDstStageMask = &waitDestinationStageMask, .commandBufferCount = 1, .pCommandBuffers = &*commandBuffers[currentFrame], .signalSemaphoreCount = 1, .pSignalSemaphores = &*renderFinishedSemaphore[imageIndex] }; - graphicsQueue.submit(submitInfo, *inFlightFences[currentFrame]); + queue.submit(submitInfo, *inFlightFences[currentFrame]); const vk::PresentInfoKHR presentInfoKHR{ .waitSemaphoreCount = 1, .pWaitSemaphores = &*renderFinishedSemaphore[imageIndex], .swapchainCount = 1, .pSwapchains = &*swapChain, .pImageIndices = &imageIndex }; - result = presentQueue.presentKHR( presentInfoKHR ); + result = queue.presentKHR( presentInfoKHR ); if (result == vk::Result::eErrorOutOfDateKHR || result == vk::Result::eSuboptimalKHR || framebufferResized) { framebufferResized = false; recreateSwapChain(); diff --git a/attachments/23_descriptor_sets.cpp b/attachments/23_descriptor_sets.cpp index 3d23c10e..cf9e6480 100644 --- a/attachments/23_descriptor_sets.cpp +++ b/attachments/23_descriptor_sets.cpp @@ -82,18 +82,15 @@ class HelloTriangleApplication { } private: - GLFWwindow* window = nullptr; - - vk::raii::Context context; - vk::raii::Instance instance = nullptr; + GLFWwindow * window = nullptr; + vk::raii::Context context; + vk::raii::Instance instance = nullptr; vk::raii::DebugUtilsMessengerEXT debugMessenger = nullptr; - vk::raii::SurfaceKHR surface = nullptr; - - vk::raii::PhysicalDevice physicalDevice = nullptr; - vk::raii::Device device = nullptr; - - vk::raii::Queue graphicsQueue = nullptr; - vk::raii::Queue presentQueue = nullptr; + vk::raii::SurfaceKHR surface = nullptr; + vk::raii::PhysicalDevice physicalDevice = nullptr; + vk::raii::Device device = nullptr; + uint32_t queueIndex = ~0; + vk::raii::Queue queue = nullptr; vk::raii::SwapchainKHR swapChain = nullptr; std::vector swapChainImages; @@ -119,7 +116,6 @@ class HelloTriangleApplication { vk::raii::CommandPool commandPool = nullptr; std::vector commandBuffers; - uint32_t graphicsIndex = 0; std::vector presentCompleteSemaphore; std::vector renderFinishedSemaphore; @@ -317,51 +313,22 @@ class HelloTriangleApplication { } void createLogicalDevice() { - // find the index of the first queue family that supports graphics std::vector queueFamilyProperties = physicalDevice.getQueueFamilyProperties(); - // get the first index into queueFamilyProperties which supports graphics - auto graphicsQueueFamilyProperty = std::ranges::find_if( queueFamilyProperties, []( auto const & qfp ) - { return (qfp.queueFlags & vk::QueueFlagBits::eGraphics) != static_cast(0); } ); - - graphicsIndex = static_cast( std::distance( queueFamilyProperties.begin(), graphicsQueueFamilyProperty ) ); - - // determine a queueFamilyIndex that supports present - // first check if the graphicsIndex is good enough - auto presentIndex = physicalDevice.getSurfaceSupportKHR( graphicsIndex, *surface ) - ? graphicsIndex - : ~0; - if ( presentIndex == queueFamilyProperties.size() ) + // get the first index into queueFamilyProperties which supports both graphics and present + for (uint32_t qfpIndex = 0; qfpIndex < queueFamilyProperties.size(); qfpIndex++) { - // the graphicsIndex doesn't support present -> look for another family index that supports both - // graphics and present - for ( size_t i = 0; i < queueFamilyProperties.size(); i++ ) - { - if ( ( queueFamilyProperties[i].queueFlags & vk::QueueFlagBits::eGraphics ) && - physicalDevice.getSurfaceSupportKHR( static_cast( i ), *surface ) ) - { - graphicsIndex = static_cast( i ); - presentIndex = graphicsIndex; - break; - } - } - if ( presentIndex == queueFamilyProperties.size() ) + if ((queueFamilyProperties[qfpIndex].queueFlags & vk::QueueFlagBits::eGraphics) && + physicalDevice.getSurfaceSupportKHR(qfpIndex, *surface)) { - // there's nothing like a single family index that supports both graphics and present -> look for another - // family index that supports present - for ( size_t i = 0; i < queueFamilyProperties.size(); i++ ) - { - if ( physicalDevice.getSurfaceSupportKHR( static_cast( i ), *surface ) ) - { - presentIndex = static_cast( i ); - break; - } - } + // found a queue family that supports both graphics and present + queueIndex = qfpIndex; + break; } } - if ( ( graphicsIndex == queueFamilyProperties.size() ) || ( presentIndex == queueFamilyProperties.size() ) ) + if (queueIndex == ~0) { - throw std::runtime_error( "Could not find a queue for graphics or present -> terminating" ); + throw std::runtime_error("Could not find a queue for graphics and present -> terminating"); } // query for Vulkan 1.3 features @@ -373,7 +340,7 @@ class HelloTriangleApplication { // create a Device float queuePriority = 0.0f; - vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = graphicsIndex, .queueCount = 1, .pQueuePriorities = &queuePriority }; + vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = queueIndex, .queueCount = 1, .pQueuePriorities = &queuePriority }; vk::DeviceCreateInfo deviceCreateInfo{ .pNext = &featureChain.get(), .queueCreateInfoCount = 1, .pQueueCreateInfos = &deviceQueueCreateInfo, @@ -381,8 +348,7 @@ class HelloTriangleApplication { .ppEnabledExtensionNames = requiredDeviceExtension.data() }; device = vk::raii::Device( physicalDevice, deviceCreateInfo ); - graphicsQueue = vk::raii::Queue( device, graphicsIndex, 0 ); - presentQueue = vk::raii::Queue( device, presentIndex, 0 ); + queue = vk::raii::Queue( device, queueIndex, 0 ); } void createSwapChain() { @@ -470,7 +436,7 @@ class HelloTriangleApplication { void createCommandPool() { vk::CommandPoolCreateInfo poolInfo{ .flags = vk::CommandPoolCreateFlagBits::eResetCommandBuffer, - .queueFamilyIndex = graphicsIndex }; + .queueFamilyIndex = queueIndex }; commandPool = vk::raii::CommandPool(device, poolInfo); } @@ -555,8 +521,8 @@ class HelloTriangleApplication { commandCopyBuffer.begin(vk::CommandBufferBeginInfo { .flags = vk::CommandBufferUsageFlagBits::eOneTimeSubmit }); commandCopyBuffer.copyBuffer(*srcBuffer, *dstBuffer, vk::BufferCopy(0, 0, size)); commandCopyBuffer.end(); - graphicsQueue.submit(vk::SubmitInfo{ .commandBufferCount = 1, .pCommandBuffers = &*commandCopyBuffer }, nullptr); - graphicsQueue.waitIdle(); + queue.submit(vk::SubmitInfo{ .commandBufferCount = 1, .pCommandBuffers = &*commandCopyBuffer }, nullptr); + queue.waitIdle(); } uint32_t findMemoryType(uint32_t typeFilter, vk::MemoryPropertyFlags properties) { @@ -714,12 +680,12 @@ class HelloTriangleApplication { const vk::SubmitInfo submitInfo{ .waitSemaphoreCount = 1, .pWaitSemaphores = &*presentCompleteSemaphore[semaphoreIndex], .pWaitDstStageMask = &waitDestinationStageMask, .commandBufferCount = 1, .pCommandBuffers = &*commandBuffers[currentFrame], .signalSemaphoreCount = 1, .pSignalSemaphores = &*renderFinishedSemaphore[imageIndex] }; - graphicsQueue.submit(submitInfo, *inFlightFences[currentFrame]); + queue.submit(submitInfo, *inFlightFences[currentFrame]); const vk::PresentInfoKHR presentInfoKHR{ .waitSemaphoreCount = 1, .pWaitSemaphores = &*renderFinishedSemaphore[imageIndex], .swapchainCount = 1, .pSwapchains = &*swapChain, .pImageIndices = &imageIndex }; - result = presentQueue.presentKHR( presentInfoKHR ); + result = queue.presentKHR( presentInfoKHR ); if (result == vk::Result::eErrorOutOfDateKHR || result == vk::Result::eSuboptimalKHR || framebufferResized) { framebufferResized = false; recreateSwapChain(); diff --git a/attachments/24_texture_image.cpp b/attachments/24_texture_image.cpp index 1599a0c8..8347ffed 100644 --- a/attachments/24_texture_image.cpp +++ b/attachments/24_texture_image.cpp @@ -85,18 +85,15 @@ class HelloTriangleApplication { } private: - GLFWwindow* window = nullptr; - - vk::raii::Context context; - vk::raii::Instance instance = nullptr; + GLFWwindow * window = nullptr; + vk::raii::Context context; + vk::raii::Instance instance = nullptr; vk::raii::DebugUtilsMessengerEXT debugMessenger = nullptr; - vk::raii::SurfaceKHR surface = nullptr; - - vk::raii::PhysicalDevice physicalDevice = nullptr; - vk::raii::Device device = nullptr; - - vk::raii::Queue graphicsQueue = nullptr; - vk::raii::Queue presentQueue = nullptr; + vk::raii::SurfaceKHR surface = nullptr; + vk::raii::PhysicalDevice physicalDevice = nullptr; + vk::raii::Device device = nullptr; + uint32_t queueIndex = ~0; + vk::raii::Queue queue = nullptr; vk::raii::SwapchainKHR swapChain = nullptr; std::vector swapChainImages; @@ -125,7 +122,6 @@ class HelloTriangleApplication { vk::raii::CommandPool commandPool = nullptr; std::vector commandBuffers; - uint32_t graphicsIndex = 0; std::vector presentCompleteSemaphore; std::vector renderFinishedSemaphore; @@ -324,51 +320,22 @@ class HelloTriangleApplication { } void createLogicalDevice() { - // find the index of the first queue family that supports graphics std::vector queueFamilyProperties = physicalDevice.getQueueFamilyProperties(); - // get the first index into queueFamilyProperties which supports graphics - auto graphicsQueueFamilyProperty = std::ranges::find_if( queueFamilyProperties, []( auto const & qfp ) - { return (qfp.queueFlags & vk::QueueFlagBits::eGraphics) != static_cast(0); } ); - - graphicsIndex = static_cast( std::distance( queueFamilyProperties.begin(), graphicsQueueFamilyProperty ) ); - - // determine a queueFamilyIndex that supports present - // first check if the graphicsIndex is good enough - auto presentIndex = physicalDevice.getSurfaceSupportKHR( graphicsIndex, *surface ) - ? graphicsIndex - : ~0; - if ( presentIndex == queueFamilyProperties.size() ) + // get the first index into queueFamilyProperties which supports both graphics and present + for (uint32_t qfpIndex = 0; qfpIndex < queueFamilyProperties.size(); qfpIndex++) { - // the graphicsIndex doesn't support present -> look for another family index that supports both - // graphics and present - for ( size_t i = 0; i < queueFamilyProperties.size(); i++ ) - { - if ( ( queueFamilyProperties[i].queueFlags & vk::QueueFlagBits::eGraphics ) && - physicalDevice.getSurfaceSupportKHR( static_cast( i ), *surface ) ) - { - graphicsIndex = static_cast( i ); - presentIndex = graphicsIndex; - break; - } - } - if ( presentIndex == queueFamilyProperties.size() ) + if ((queueFamilyProperties[qfpIndex].queueFlags & vk::QueueFlagBits::eGraphics) && + physicalDevice.getSurfaceSupportKHR(qfpIndex, *surface)) { - // there's nothing like a single family index that supports both graphics and present -> look for another - // family index that supports present - for ( size_t i = 0; i < queueFamilyProperties.size(); i++ ) - { - if ( physicalDevice.getSurfaceSupportKHR( static_cast( i ), *surface ) ) - { - presentIndex = static_cast( i ); - break; - } - } + // found a queue family that supports both graphics and present + queueIndex = qfpIndex; + break; } } - if ( ( graphicsIndex == queueFamilyProperties.size() ) || ( presentIndex == queueFamilyProperties.size() ) ) + if (queueIndex == ~0) { - throw std::runtime_error( "Could not find a queue for graphics or present -> terminating" ); + throw std::runtime_error("Could not find a queue for graphics and present -> terminating"); } // query for Vulkan 1.3 features @@ -380,7 +347,7 @@ class HelloTriangleApplication { // create a Device float queuePriority = 0.0f; - vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = graphicsIndex, .queueCount = 1, .pQueuePriorities = &queuePriority }; + vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = queueIndex, .queueCount = 1, .pQueuePriorities = &queuePriority }; vk::DeviceCreateInfo deviceCreateInfo{ .pNext = &featureChain.get(), .queueCreateInfoCount = 1, .pQueueCreateInfos = &deviceQueueCreateInfo, @@ -388,8 +355,7 @@ class HelloTriangleApplication { .ppEnabledExtensionNames = requiredDeviceExtension.data() }; device = vk::raii::Device( physicalDevice, deviceCreateInfo ); - graphicsQueue = vk::raii::Queue( device, graphicsIndex, 0 ); - presentQueue = vk::raii::Queue( device, presentIndex, 0 ); + queue = vk::raii::Queue( device, queueIndex, 0 ); } void createSwapChain() { @@ -477,7 +443,7 @@ class HelloTriangleApplication { void createCommandPool() { vk::CommandPoolCreateInfo poolInfo{ .flags = vk::CommandPoolCreateFlagBits::eResetCommandBuffer, - .queueFamilyIndex = graphicsIndex }; + .queueFamilyIndex = queueIndex }; commandPool = vk::raii::CommandPool(device, poolInfo); } @@ -649,8 +615,8 @@ class HelloTriangleApplication { commandBuffer.end(); vk::SubmitInfo submitInfo{ .commandBufferCount = 1, .pCommandBuffers = &*commandBuffer }; - graphicsQueue.submit(submitInfo, nullptr); - graphicsQueue.waitIdle(); + queue.submit(submitInfo, nullptr); + queue.waitIdle(); } void copyBuffer(vk::raii::Buffer & srcBuffer, vk::raii::Buffer & dstBuffer, vk::DeviceSize size) { @@ -659,8 +625,8 @@ class HelloTriangleApplication { commandCopyBuffer.begin(vk::CommandBufferBeginInfo{ .flags = vk::CommandBufferUsageFlagBits::eOneTimeSubmit }); commandCopyBuffer.copyBuffer(*srcBuffer, *dstBuffer, vk::BufferCopy{ .size = size }); commandCopyBuffer.end(); - graphicsQueue.submit(vk::SubmitInfo{ .commandBufferCount = 1, .pCommandBuffers = &*commandCopyBuffer }, nullptr); - graphicsQueue.waitIdle(); + queue.submit(vk::SubmitInfo{ .commandBufferCount = 1, .pCommandBuffers = &*commandCopyBuffer }, nullptr); + queue.waitIdle(); } uint32_t findMemoryType(uint32_t typeFilter, vk::MemoryPropertyFlags properties) { @@ -818,12 +784,12 @@ class HelloTriangleApplication { const vk::SubmitInfo submitInfo{ .waitSemaphoreCount = 1, .pWaitSemaphores = &*presentCompleteSemaphore[semaphoreIndex], .pWaitDstStageMask = &waitDestinationStageMask, .commandBufferCount = 1, .pCommandBuffers = &*commandBuffers[currentFrame], .signalSemaphoreCount = 1, .pSignalSemaphores = &*renderFinishedSemaphore[imageIndex] }; - graphicsQueue.submit(submitInfo, *inFlightFences[currentFrame]); + queue.submit(submitInfo, *inFlightFences[currentFrame]); const vk::PresentInfoKHR presentInfoKHR{ .waitSemaphoreCount = 1, .pWaitSemaphores = &*renderFinishedSemaphore[imageIndex], .swapchainCount = 1, .pSwapchains = &*swapChain, .pImageIndices = &imageIndex }; - result = presentQueue.presentKHR( presentInfoKHR ); + result = queue.presentKHR( presentInfoKHR ); if (result == vk::Result::eErrorOutOfDateKHR || result == vk::Result::eSuboptimalKHR || framebufferResized) { framebufferResized = false; recreateSwapChain(); diff --git a/attachments/25_sampler.cpp b/attachments/25_sampler.cpp index 921e19e2..d5bacc59 100644 --- a/attachments/25_sampler.cpp +++ b/attachments/25_sampler.cpp @@ -85,18 +85,15 @@ class HelloTriangleApplication { } private: - GLFWwindow* window = nullptr; - - vk::raii::Context context; - vk::raii::Instance instance = nullptr; + GLFWwindow * window = nullptr; + vk::raii::Context context; + vk::raii::Instance instance = nullptr; vk::raii::DebugUtilsMessengerEXT debugMessenger = nullptr; - vk::raii::SurfaceKHR surface = nullptr; - - vk::raii::PhysicalDevice physicalDevice = nullptr; - vk::raii::Device device = nullptr; - - vk::raii::Queue graphicsQueue = nullptr; - vk::raii::Queue presentQueue = nullptr; + vk::raii::SurfaceKHR surface = nullptr; + vk::raii::PhysicalDevice physicalDevice = nullptr; + vk::raii::Device device = nullptr; + uint32_t queueIndex = ~0; + vk::raii::Queue queue = nullptr; vk::raii::SwapchainKHR swapChain = nullptr; std::vector swapChainImages; @@ -127,7 +124,6 @@ class HelloTriangleApplication { vk::raii::CommandPool commandPool = nullptr; std::vector commandBuffers; - uint32_t graphicsIndex = 0; std::vector presentCompleteSemaphore; std::vector renderFinishedSemaphore; @@ -329,51 +325,22 @@ class HelloTriangleApplication { } void createLogicalDevice() { - // find the index of the first queue family that supports graphics std::vector queueFamilyProperties = physicalDevice.getQueueFamilyProperties(); - // get the first index into queueFamilyProperties which supports graphics - auto graphicsQueueFamilyProperty = std::ranges::find_if( queueFamilyProperties, []( auto const & qfp ) - { return (qfp.queueFlags & vk::QueueFlagBits::eGraphics) != static_cast(0); } ); - - graphicsIndex = static_cast( std::distance( queueFamilyProperties.begin(), graphicsQueueFamilyProperty ) ); - - // determine a queueFamilyIndex that supports present - // first check if the graphicsIndex is good enough - auto presentIndex = physicalDevice.getSurfaceSupportKHR( graphicsIndex, *surface ) - ? graphicsIndex - : ~0; - if ( presentIndex == queueFamilyProperties.size() ) + // get the first index into queueFamilyProperties which supports both graphics and present + for (uint32_t qfpIndex = 0; qfpIndex < queueFamilyProperties.size(); qfpIndex++) { - // the graphicsIndex doesn't support present -> look for another family index that supports both - // graphics and present - for ( size_t i = 0; i < queueFamilyProperties.size(); i++ ) - { - if ( ( queueFamilyProperties[i].queueFlags & vk::QueueFlagBits::eGraphics ) && - physicalDevice.getSurfaceSupportKHR( static_cast( i ), *surface ) ) - { - graphicsIndex = static_cast( i ); - presentIndex = graphicsIndex; - break; - } - } - if ( presentIndex == queueFamilyProperties.size() ) + if ((queueFamilyProperties[qfpIndex].queueFlags & vk::QueueFlagBits::eGraphics) && + physicalDevice.getSurfaceSupportKHR(qfpIndex, *surface)) { - // there's nothing like a single family index that supports both graphics and present -> look for another - // family index that supports present - for ( size_t i = 0; i < queueFamilyProperties.size(); i++ ) - { - if ( physicalDevice.getSurfaceSupportKHR( static_cast( i ), *surface ) ) - { - presentIndex = static_cast( i ); - break; - } - } + // found a queue family that supports both graphics and present + queueIndex = qfpIndex; + break; } } - if ( ( graphicsIndex == queueFamilyProperties.size() ) || ( presentIndex == queueFamilyProperties.size() ) ) + if (queueIndex == ~0) { - throw std::runtime_error( "Could not find a queue for graphics or present -> terminating" ); + throw std::runtime_error("Could not find a queue for graphics and present -> terminating"); } // query for Vulkan 1.3 features @@ -385,7 +352,7 @@ class HelloTriangleApplication { // create a Device float queuePriority = 0.0f; - vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = graphicsIndex, .queueCount = 1, .pQueuePriorities = &queuePriority }; + vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = queueIndex, .queueCount = 1, .pQueuePriorities = &queuePriority }; vk::DeviceCreateInfo deviceCreateInfo{ .pNext = &featureChain.get(), .queueCreateInfoCount = 1, .pQueueCreateInfos = &deviceQueueCreateInfo, @@ -393,8 +360,7 @@ class HelloTriangleApplication { .ppEnabledExtensionNames = requiredDeviceExtension.data() }; device = vk::raii::Device( physicalDevice, deviceCreateInfo ); - graphicsQueue = vk::raii::Queue( device, graphicsIndex, 0 ); - presentQueue = vk::raii::Queue( device, presentIndex, 0 ); + queue = vk::raii::Queue( device, queueIndex, 0 ); } void createSwapChain() { @@ -482,7 +448,7 @@ class HelloTriangleApplication { void createCommandPool() { vk::CommandPoolCreateInfo poolInfo{ .flags = vk::CommandPoolCreateFlagBits::eResetCommandBuffer, - .queueFamilyIndex = graphicsIndex }; + .queueFamilyIndex = queueIndex }; commandPool = vk::raii::CommandPool(device, poolInfo); } @@ -684,8 +650,8 @@ class HelloTriangleApplication { commandBuffer.end(); vk::SubmitInfo submitInfo{ .commandBufferCount = 1, .pCommandBuffers = &*commandBuffer }; - graphicsQueue.submit(submitInfo, nullptr); - graphicsQueue.waitIdle(); + queue.submit(submitInfo, nullptr); + queue.waitIdle(); } void copyBuffer(vk::raii::Buffer & srcBuffer, vk::raii::Buffer & dstBuffer, vk::DeviceSize size) { @@ -694,8 +660,8 @@ class HelloTriangleApplication { commandCopyBuffer.begin(vk::CommandBufferBeginInfo{ .flags = vk::CommandBufferUsageFlagBits::eOneTimeSubmit }); commandCopyBuffer.copyBuffer(*srcBuffer, *dstBuffer, vk::BufferCopy{ .size = size }); commandCopyBuffer.end(); - graphicsQueue.submit(vk::SubmitInfo{ .commandBufferCount = 1, .pCommandBuffers = &*commandCopyBuffer }, nullptr); - graphicsQueue.waitIdle(); + queue.submit(vk::SubmitInfo{ .commandBufferCount = 1, .pCommandBuffers = &*commandCopyBuffer }, nullptr); + queue.waitIdle(); } uint32_t findMemoryType(uint32_t typeFilter, vk::MemoryPropertyFlags properties) { @@ -853,12 +819,12 @@ class HelloTriangleApplication { const vk::SubmitInfo submitInfo{ .waitSemaphoreCount = 1, .pWaitSemaphores = &*presentCompleteSemaphore[semaphoreIndex], .pWaitDstStageMask = &waitDestinationStageMask, .commandBufferCount = 1, .pCommandBuffers = &*commandBuffers[currentFrame], .signalSemaphoreCount = 1, .pSignalSemaphores = &*renderFinishedSemaphore[imageIndex] }; - graphicsQueue.submit(submitInfo, *inFlightFences[currentFrame]); + queue.submit(submitInfo, *inFlightFences[currentFrame]); const vk::PresentInfoKHR presentInfoKHR{ .waitSemaphoreCount = 1, .pWaitSemaphores = &*renderFinishedSemaphore[imageIndex], .swapchainCount = 1, .pSwapchains = &*swapChain, .pImageIndices = &imageIndex }; - result = presentQueue.presentKHR( presentInfoKHR ); + result = queue.presentKHR( presentInfoKHR ); if (result == vk::Result::eErrorOutOfDateKHR || result == vk::Result::eSuboptimalKHR || framebufferResized) { framebufferResized = false; recreateSwapChain(); diff --git a/attachments/26_texture_mapping.cpp b/attachments/26_texture_mapping.cpp index 333d60c3..fc132ade 100644 --- a/attachments/26_texture_mapping.cpp +++ b/attachments/26_texture_mapping.cpp @@ -87,18 +87,15 @@ class HelloTriangleApplication { } private: - GLFWwindow* window = nullptr; - - vk::raii::Context context; - vk::raii::Instance instance = nullptr; + GLFWwindow * window = nullptr; + vk::raii::Context context; + vk::raii::Instance instance = nullptr; vk::raii::DebugUtilsMessengerEXT debugMessenger = nullptr; - vk::raii::SurfaceKHR surface = nullptr; - - vk::raii::PhysicalDevice physicalDevice = nullptr; - vk::raii::Device device = nullptr; - - vk::raii::Queue graphicsQueue = nullptr; - vk::raii::Queue presentQueue = nullptr; + vk::raii::SurfaceKHR surface = nullptr; + vk::raii::PhysicalDevice physicalDevice = nullptr; + vk::raii::Device device = nullptr; + uint32_t queueIndex = ~0; + vk::raii::Queue queue = nullptr; vk::raii::SwapchainKHR swapChain = nullptr; std::vector swapChainImages; @@ -129,7 +126,6 @@ class HelloTriangleApplication { vk::raii::CommandPool commandPool = nullptr; std::vector commandBuffers; - uint32_t graphicsIndex = 0; std::vector presentCompleteSemaphore; std::vector renderFinishedSemaphore; @@ -331,51 +327,22 @@ class HelloTriangleApplication { } void createLogicalDevice() { - // find the index of the first queue family that supports graphics std::vector queueFamilyProperties = physicalDevice.getQueueFamilyProperties(); - // get the first index into queueFamilyProperties which supports graphics - auto graphicsQueueFamilyProperty = std::ranges::find_if( queueFamilyProperties, []( auto const & qfp ) - { return (qfp.queueFlags & vk::QueueFlagBits::eGraphics) != static_cast(0); } ); - - graphicsIndex = static_cast( std::distance( queueFamilyProperties.begin(), graphicsQueueFamilyProperty ) ); - - // determine a queueFamilyIndex that supports present - // first check if the graphicsIndex is good enough - auto presentIndex = physicalDevice.getSurfaceSupportKHR( graphicsIndex, *surface ) - ? graphicsIndex - : ~0; - if ( presentIndex == queueFamilyProperties.size() ) + // get the first index into queueFamilyProperties which supports both graphics and present + for (uint32_t qfpIndex = 0; qfpIndex < queueFamilyProperties.size(); qfpIndex++) { - // the graphicsIndex doesn't support present -> look for another family index that supports both - // graphics and present - for ( size_t i = 0; i < queueFamilyProperties.size(); i++ ) + if ((queueFamilyProperties[qfpIndex].queueFlags & vk::QueueFlagBits::eGraphics) && + physicalDevice.getSurfaceSupportKHR(qfpIndex, *surface)) { - if ( ( queueFamilyProperties[i].queueFlags & vk::QueueFlagBits::eGraphics ) && - physicalDevice.getSurfaceSupportKHR( static_cast( i ), *surface ) ) - { - graphicsIndex = static_cast( i ); - presentIndex = graphicsIndex; - break; - } - } - if ( presentIndex == queueFamilyProperties.size() ) - { - // there's nothing like a single family index that supports both graphics and present -> look for another - // family index that supports present - for ( size_t i = 0; i < queueFamilyProperties.size(); i++ ) - { - if ( physicalDevice.getSurfaceSupportKHR( static_cast( i ), *surface ) ) - { - presentIndex = static_cast( i ); - break; - } - } + // found a queue family that supports both graphics and present + queueIndex = qfpIndex; + break; } } - if ( ( graphicsIndex == queueFamilyProperties.size() ) || ( presentIndex == queueFamilyProperties.size() ) ) + if (queueIndex == ~0) { - throw std::runtime_error( "Could not find a queue for graphics or present -> terminating" ); + throw std::runtime_error("Could not find a queue for graphics and present -> terminating"); } // query for Vulkan 1.3 features @@ -387,7 +354,7 @@ class HelloTriangleApplication { // create a Device float queuePriority = 0.0f; - vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = graphicsIndex, .queueCount = 1, .pQueuePriorities = &queuePriority }; + vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = queueIndex, .queueCount = 1, .pQueuePriorities = &queuePriority }; vk::DeviceCreateInfo deviceCreateInfo{ .pNext = &featureChain.get(), .queueCreateInfoCount = 1, .pQueueCreateInfos = &deviceQueueCreateInfo, @@ -395,8 +362,7 @@ class HelloTriangleApplication { .ppEnabledExtensionNames = requiredDeviceExtension.data() }; device = vk::raii::Device( physicalDevice, deviceCreateInfo ); - graphicsQueue = vk::raii::Queue( device, graphicsIndex, 0 ); - presentQueue = vk::raii::Queue( device, presentIndex, 0 ); + queue = vk::raii::Queue( device, queueIndex, 0 ); } void createSwapChain() { @@ -488,7 +454,7 @@ class HelloTriangleApplication { void createCommandPool() { vk::CommandPoolCreateInfo poolInfo{ .flags = vk::CommandPoolCreateFlagBits::eResetCommandBuffer, - .queueFamilyIndex = graphicsIndex }; + .queueFamilyIndex = queueIndex }; commandPool = vk::raii::CommandPool(device, poolInfo); } @@ -761,8 +727,8 @@ class HelloTriangleApplication { commandBuffer.end(); vk::SubmitInfo submitInfo{ .commandBufferCount = 1, .pCommandBuffers = &*commandBuffer }; - graphicsQueue.submit(submitInfo, nullptr); - graphicsQueue.waitIdle(); + queue.submit(submitInfo, nullptr); + queue.waitIdle(); } void copyBuffer(vk::raii::Buffer & srcBuffer, vk::raii::Buffer & dstBuffer, vk::DeviceSize size) { @@ -771,8 +737,8 @@ class HelloTriangleApplication { commandCopyBuffer.begin(vk::CommandBufferBeginInfo{ .flags = vk::CommandBufferUsageFlagBits::eOneTimeSubmit }); commandCopyBuffer.copyBuffer(*srcBuffer, *dstBuffer, vk::BufferCopy{ .size = size }); commandCopyBuffer.end(); - graphicsQueue.submit(vk::SubmitInfo{ .commandBufferCount = 1, .pCommandBuffers = &*commandCopyBuffer }, nullptr); - graphicsQueue.waitIdle(); + queue.submit(vk::SubmitInfo{ .commandBufferCount = 1, .pCommandBuffers = &*commandCopyBuffer }, nullptr); + queue.waitIdle(); } uint32_t findMemoryType(uint32_t typeFilter, vk::MemoryPropertyFlags properties) { @@ -930,12 +896,12 @@ class HelloTriangleApplication { const vk::SubmitInfo submitInfo{ .waitSemaphoreCount = 1, .pWaitSemaphores = &*presentCompleteSemaphore[semaphoreIndex], .pWaitDstStageMask = &waitDestinationStageMask, .commandBufferCount = 1, .pCommandBuffers = &*commandBuffers[currentFrame], .signalSemaphoreCount = 1, .pSignalSemaphores = &*renderFinishedSemaphore[imageIndex] }; - graphicsQueue.submit(submitInfo, *inFlightFences[currentFrame]); + queue.submit(submitInfo, *inFlightFences[currentFrame]); const vk::PresentInfoKHR presentInfoKHR{ .waitSemaphoreCount = 1, .pWaitSemaphores = &*renderFinishedSemaphore[imageIndex], .swapchainCount = 1, .pSwapchains = &*swapChain, .pImageIndices = &imageIndex }; - result = presentQueue.presentKHR(presentInfoKHR); + result = queue.presentKHR(presentInfoKHR); if (result == vk::Result::eErrorOutOfDateKHR || result == vk::Result::eSuboptimalKHR || framebufferResized) { framebufferResized = false; recreateSwapChain(); diff --git a/attachments/27_depth_buffering.cpp b/attachments/27_depth_buffering.cpp index d475a262..b2110f69 100644 --- a/attachments/27_depth_buffering.cpp +++ b/attachments/27_depth_buffering.cpp @@ -94,18 +94,15 @@ class HelloTriangleApplication { } private: - GLFWwindow* window = nullptr; - - vk::raii::Context context; - vk::raii::Instance instance = nullptr; + GLFWwindow * window = nullptr; + vk::raii::Context context; + vk::raii::Instance instance = nullptr; vk::raii::DebugUtilsMessengerEXT debugMessenger = nullptr; - vk::raii::SurfaceKHR surface = nullptr; - - vk::raii::PhysicalDevice physicalDevice = nullptr; - vk::raii::Device device = nullptr; - - vk::raii::Queue graphicsQueue = nullptr; - vk::raii::Queue presentQueue = nullptr; + vk::raii::SurfaceKHR surface = nullptr; + vk::raii::PhysicalDevice physicalDevice = nullptr; + vk::raii::Device device = nullptr; + uint32_t queueIndex = ~0; + vk::raii::Queue queue = nullptr; vk::raii::SwapchainKHR swapChain = nullptr; std::vector swapChainImages; @@ -140,7 +137,6 @@ class HelloTriangleApplication { vk::raii::CommandPool commandPool = nullptr; std::vector commandBuffers; - uint32_t graphicsIndex = 0; std::vector presentCompleteSemaphore; std::vector renderFinishedSemaphore; @@ -344,51 +340,22 @@ class HelloTriangleApplication { } void createLogicalDevice() { - // find the index of the first queue family that supports graphics std::vector queueFamilyProperties = physicalDevice.getQueueFamilyProperties(); - // get the first index into queueFamilyProperties which supports graphics - auto graphicsQueueFamilyProperty = std::ranges::find_if( queueFamilyProperties, []( auto const & qfp ) - { return (qfp.queueFlags & vk::QueueFlagBits::eGraphics) != static_cast(0); } ); - - graphicsIndex = static_cast( std::distance( queueFamilyProperties.begin(), graphicsQueueFamilyProperty ) ); - - // determine a queueFamilyIndex that supports present - // first check if the graphicsIndex is good enough - auto presentIndex = physicalDevice.getSurfaceSupportKHR( graphicsIndex, *surface ) - ? graphicsIndex - : ~0; - if ( presentIndex == queueFamilyProperties.size() ) + // get the first index into queueFamilyProperties which supports both graphics and present + for (uint32_t qfpIndex = 0; qfpIndex < queueFamilyProperties.size(); qfpIndex++) { - // the graphicsIndex doesn't support present -> look for another family index that supports both - // graphics and present - for ( size_t i = 0; i < queueFamilyProperties.size(); i++ ) + if ((queueFamilyProperties[qfpIndex].queueFlags & vk::QueueFlagBits::eGraphics) && + physicalDevice.getSurfaceSupportKHR(qfpIndex, *surface)) { - if ( ( queueFamilyProperties[i].queueFlags & vk::QueueFlagBits::eGraphics ) && - physicalDevice.getSurfaceSupportKHR( static_cast( i ), *surface ) ) - { - graphicsIndex = static_cast( i ); - presentIndex = graphicsIndex; - break; - } - } - if ( presentIndex == queueFamilyProperties.size() ) - { - // there's nothing like a single family index that supports both graphics and present -> look for another - // family index that supports present - for ( size_t i = 0; i < queueFamilyProperties.size(); i++ ) - { - if ( physicalDevice.getSurfaceSupportKHR( static_cast( i ), *surface ) ) - { - presentIndex = static_cast( i ); - break; - } - } + // found a queue family that supports both graphics and present + queueIndex = qfpIndex; + break; } } - if ( ( graphicsIndex == queueFamilyProperties.size() ) || ( presentIndex == queueFamilyProperties.size() ) ) + if (queueIndex == ~0) { - throw std::runtime_error( "Could not find a queue for graphics or present -> terminating" ); + throw std::runtime_error("Could not find a queue for graphics and present -> terminating"); } // query for Vulkan 1.3 features @@ -400,7 +367,7 @@ class HelloTriangleApplication { // create a Device float queuePriority = 0.0f; - vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = graphicsIndex, .queueCount = 1, .pQueuePriorities = &queuePriority }; + vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = queueIndex, .queueCount = 1, .pQueuePriorities = &queuePriority }; vk::DeviceCreateInfo deviceCreateInfo{ .pNext = &featureChain.get(), .queueCreateInfoCount = 1, .pQueueCreateInfos = &deviceQueueCreateInfo, @@ -408,8 +375,7 @@ class HelloTriangleApplication { .ppEnabledExtensionNames = requiredDeviceExtension.data() }; device = vk::raii::Device( physicalDevice, deviceCreateInfo ); - graphicsQueue = vk::raii::Queue( device, graphicsIndex, 0 ); - presentQueue = vk::raii::Queue( device, presentIndex, 0 ); + queue = vk::raii::Queue( device, queueIndex, 0 ); } void createSwapChain() { @@ -545,7 +511,7 @@ class HelloTriangleApplication { void createCommandPool() { vk::CommandPoolCreateInfo poolInfo{ .flags = vk::CommandPoolCreateFlagBits::eResetCommandBuffer, - .queueFamilyIndex = graphicsIndex + .queueFamilyIndex = queueIndex }; commandPool = vk::raii::CommandPool(device, poolInfo); } @@ -851,8 +817,8 @@ class HelloTriangleApplication { commandBuffer.end(); vk::SubmitInfo submitInfo{ .commandBufferCount = 1, .pCommandBuffers = &*commandBuffer }; - graphicsQueue.submit(submitInfo, nullptr); - graphicsQueue.waitIdle(); + queue.submit(submitInfo, nullptr); + queue.waitIdle(); } void copyBuffer(vk::raii::Buffer & srcBuffer, vk::raii::Buffer & dstBuffer, vk::DeviceSize size) { @@ -861,8 +827,8 @@ class HelloTriangleApplication { commandCopyBuffer.begin(vk::CommandBufferBeginInfo{ .flags = vk::CommandBufferUsageFlagBits::eOneTimeSubmit }); commandCopyBuffer.copyBuffer(*srcBuffer, *dstBuffer, vk::BufferCopy{ .size = size }); commandCopyBuffer.end(); - graphicsQueue.submit(vk::SubmitInfo{ .commandBufferCount = 1, .pCommandBuffers = &*commandCopyBuffer }, nullptr); - graphicsQueue.waitIdle(); + queue.submit(vk::SubmitInfo{ .commandBufferCount = 1, .pCommandBuffers = &*commandCopyBuffer }, nullptr); + queue.waitIdle(); } uint32_t findMemoryType(uint32_t typeFilter, vk::MemoryPropertyFlags properties) { @@ -1058,12 +1024,12 @@ class HelloTriangleApplication { const vk::SubmitInfo submitInfo{ .waitSemaphoreCount = 1, .pWaitSemaphores = &*presentCompleteSemaphore[semaphoreIndex], .pWaitDstStageMask = &waitDestinationStageMask, .commandBufferCount = 1, .pCommandBuffers = &*commandBuffers[currentFrame], .signalSemaphoreCount = 1, .pSignalSemaphores = &*renderFinishedSemaphore[imageIndex] }; - graphicsQueue.submit(submitInfo, *inFlightFences[currentFrame]); + queue.submit(submitInfo, *inFlightFences[currentFrame]); const vk::PresentInfoKHR presentInfoKHR{ .waitSemaphoreCount = 1, .pWaitSemaphores = &*renderFinishedSemaphore[imageIndex], .swapchainCount = 1, .pSwapchains = &*swapChain, .pImageIndices = &imageIndex }; - result = presentQueue.presentKHR(presentInfoKHR); + result = queue.presentKHR(presentInfoKHR); if (result == vk::Result::eErrorOutOfDateKHR || result == vk::Result::eSuboptimalKHR || framebufferResized) { framebufferResized = false; recreateSwapChain(); diff --git a/attachments/28_model_loading.cpp b/attachments/28_model_loading.cpp index 3261d941..532123cb 100644 --- a/attachments/28_model_loading.cpp +++ b/attachments/28_model_loading.cpp @@ -95,18 +95,15 @@ class HelloTriangleApplication { } private: - GLFWwindow* window = nullptr; - - vk::raii::Context context; - vk::raii::Instance instance = nullptr; + GLFWwindow * window = nullptr; + vk::raii::Context context; + vk::raii::Instance instance = nullptr; vk::raii::DebugUtilsMessengerEXT debugMessenger = nullptr; - vk::raii::SurfaceKHR surface = nullptr; - - vk::raii::PhysicalDevice physicalDevice = nullptr; - vk::raii::Device device = nullptr; - - vk::raii::Queue graphicsQueue = nullptr; - vk::raii::Queue presentQueue = nullptr; + vk::raii::SurfaceKHR surface = nullptr; + vk::raii::PhysicalDevice physicalDevice = nullptr; + vk::raii::Device device = nullptr; + uint32_t queueIndex = ~0; + vk::raii::Queue queue = nullptr; vk::raii::SwapchainKHR swapChain = nullptr; std::vector swapChainImages; @@ -143,7 +140,6 @@ class HelloTriangleApplication { vk::raii::CommandPool commandPool = nullptr; std::vector commandBuffers; - uint32_t graphicsIndex = 0; std::vector presentCompleteSemaphore; std::vector renderFinishedSemaphore; @@ -348,51 +344,22 @@ class HelloTriangleApplication { } void createLogicalDevice() { - // find the index of the first queue family that supports graphics std::vector queueFamilyProperties = physicalDevice.getQueueFamilyProperties(); - // get the first index into queueFamilyProperties which supports graphics - auto graphicsQueueFamilyProperty = std::ranges::find_if( queueFamilyProperties, []( auto const & qfp ) - { return (qfp.queueFlags & vk::QueueFlagBits::eGraphics) != static_cast(0); } ); - - graphicsIndex = static_cast( std::distance( queueFamilyProperties.begin(), graphicsQueueFamilyProperty ) ); - - // determine a queueFamilyIndex that supports present - // first check if the graphicsIndex is good enough - auto presentIndex = physicalDevice.getSurfaceSupportKHR( graphicsIndex, *surface ) - ? graphicsIndex - : ~0; - if ( presentIndex == queueFamilyProperties.size() ) + // get the first index into queueFamilyProperties which supports both graphics and present + for (uint32_t qfpIndex = 0; qfpIndex < queueFamilyProperties.size(); qfpIndex++) { - // the graphicsIndex doesn't support present -> look for another family index that supports both - // graphics and present - for ( size_t i = 0; i < queueFamilyProperties.size(); i++ ) + if ((queueFamilyProperties[qfpIndex].queueFlags & vk::QueueFlagBits::eGraphics) && + physicalDevice.getSurfaceSupportKHR(qfpIndex, *surface)) { - if ( ( queueFamilyProperties[i].queueFlags & vk::QueueFlagBits::eGraphics ) && - physicalDevice.getSurfaceSupportKHR( static_cast( i ), *surface ) ) - { - graphicsIndex = static_cast( i ); - presentIndex = graphicsIndex; - break; - } - } - if ( presentIndex == queueFamilyProperties.size() ) - { - // there's nothing like a single family index that supports both graphics and present -> look for another - // family index that supports present - for ( size_t i = 0; i < queueFamilyProperties.size(); i++ ) - { - if ( physicalDevice.getSurfaceSupportKHR( static_cast( i ), *surface ) ) - { - presentIndex = static_cast( i ); - break; - } - } + // found a queue family that supports both graphics and present + queueIndex = qfpIndex; + break; } } - if ( ( graphicsIndex == queueFamilyProperties.size() ) || ( presentIndex == queueFamilyProperties.size() ) ) + if (queueIndex == ~0) { - throw std::runtime_error( "Could not find a queue for graphics or present -> terminating" ); + throw std::runtime_error("Could not find a queue for graphics and present -> terminating"); } // query for Vulkan 1.3 features @@ -404,7 +371,7 @@ class HelloTriangleApplication { // create a Device float queuePriority = 0.0f; - vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = graphicsIndex, .queueCount = 1, .pQueuePriorities = &queuePriority }; + vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = queueIndex, .queueCount = 1, .pQueuePriorities = &queuePriority }; vk::DeviceCreateInfo deviceCreateInfo{ .pNext = &featureChain.get(), .queueCreateInfoCount = 1, .pQueueCreateInfos = &deviceQueueCreateInfo, @@ -412,8 +379,7 @@ class HelloTriangleApplication { .ppEnabledExtensionNames = requiredDeviceExtension.data() }; device = vk::raii::Device( physicalDevice, deviceCreateInfo ); - graphicsQueue = vk::raii::Queue( device, graphicsIndex, 0 ); - presentQueue = vk::raii::Queue( device, presentIndex, 0 ); + queue = vk::raii::Queue( device, queueIndex, 0 ); } void createSwapChain() { @@ -549,7 +515,7 @@ class HelloTriangleApplication { void createCommandPool() { vk::CommandPoolCreateInfo poolInfo{ .flags = vk::CommandPoolCreateFlagBits::eResetCommandBuffer, - .queueFamilyIndex = graphicsIndex + .queueFamilyIndex = queueIndex }; commandPool = vk::raii::CommandPool(device, poolInfo); } @@ -896,8 +862,8 @@ class HelloTriangleApplication { commandBuffer.end(); vk::SubmitInfo submitInfo{ .commandBufferCount = 1, .pCommandBuffers = &*commandBuffer }; - graphicsQueue.submit(submitInfo, nullptr); - graphicsQueue.waitIdle(); + queue.submit(submitInfo, nullptr); + queue.waitIdle(); } void copyBuffer(vk::raii::Buffer & srcBuffer, vk::raii::Buffer & dstBuffer, vk::DeviceSize size) { @@ -906,8 +872,8 @@ class HelloTriangleApplication { commandCopyBuffer.begin(vk::CommandBufferBeginInfo{ .flags = vk::CommandBufferUsageFlagBits::eOneTimeSubmit }); commandCopyBuffer.copyBuffer(*srcBuffer, *dstBuffer, vk::BufferCopy{ .size = size }); commandCopyBuffer.end(); - graphicsQueue.submit(vk::SubmitInfo{ .commandBufferCount = 1, .pCommandBuffers = &*commandCopyBuffer }, nullptr); - graphicsQueue.waitIdle(); + queue.submit(vk::SubmitInfo{ .commandBufferCount = 1, .pCommandBuffers = &*commandCopyBuffer }, nullptr); + queue.waitIdle(); } uint32_t findMemoryType(uint32_t typeFilter, vk::MemoryPropertyFlags properties) { @@ -1103,12 +1069,12 @@ class HelloTriangleApplication { const vk::SubmitInfo submitInfo{ .waitSemaphoreCount = 1, .pWaitSemaphores = &*presentCompleteSemaphore[semaphoreIndex], .pWaitDstStageMask = &waitDestinationStageMask, .commandBufferCount = 1, .pCommandBuffers = &*commandBuffers[currentFrame], .signalSemaphoreCount = 1, .pSignalSemaphores = &*renderFinishedSemaphore[imageIndex] }; - graphicsQueue.submit(submitInfo, *inFlightFences[currentFrame]); + queue.submit(submitInfo, *inFlightFences[currentFrame]); const vk::PresentInfoKHR presentInfoKHR{ .waitSemaphoreCount = 1, .pWaitSemaphores = &*renderFinishedSemaphore[imageIndex], .swapchainCount = 1, .pSwapchains = &*swapChain, .pImageIndices = &imageIndex }; - result = presentQueue.presentKHR(presentInfoKHR); + result = queue.presentKHR(presentInfoKHR); if (result == vk::Result::eErrorOutOfDateKHR || result == vk::Result::eSuboptimalKHR || framebufferResized) { framebufferResized = false; recreateSwapChain(); diff --git a/attachments/29_mipmapping.cpp b/attachments/29_mipmapping.cpp index b8821288..1e59f6fd 100644 --- a/attachments/29_mipmapping.cpp +++ b/attachments/29_mipmapping.cpp @@ -95,18 +95,15 @@ class HelloTriangleApplication { } private: - GLFWwindow* window = nullptr; - - vk::raii::Context context; - vk::raii::Instance instance = nullptr; + GLFWwindow * window = nullptr; + vk::raii::Context context; + vk::raii::Instance instance = nullptr; vk::raii::DebugUtilsMessengerEXT debugMessenger = nullptr; - vk::raii::SurfaceKHR surface = nullptr; - - vk::raii::PhysicalDevice physicalDevice = nullptr; - vk::raii::Device device = nullptr; - - vk::raii::Queue graphicsQueue = nullptr; - vk::raii::Queue presentQueue = nullptr; + vk::raii::SurfaceKHR surface = nullptr; + vk::raii::PhysicalDevice physicalDevice = nullptr; + vk::raii::Device device = nullptr; + uint32_t queueIndex = ~0; + vk::raii::Queue queue = nullptr; vk::raii::SwapchainKHR swapChain = nullptr; std::vector swapChainImages; @@ -144,7 +141,6 @@ class HelloTriangleApplication { vk::raii::CommandPool commandPool = nullptr; std::vector commandBuffers; - uint32_t graphicsIndex = 0; std::vector presentCompleteSemaphore; std::vector renderFinishedSemaphore; @@ -348,51 +344,22 @@ class HelloTriangleApplication { } void createLogicalDevice() { - // find the index of the first queue family that supports graphics std::vector queueFamilyProperties = physicalDevice.getQueueFamilyProperties(); - // get the first index into queueFamilyProperties which supports graphics - auto graphicsQueueFamilyProperty = std::ranges::find_if( queueFamilyProperties, []( auto const & qfp ) - { return (qfp.queueFlags & vk::QueueFlagBits::eGraphics) != static_cast(0); } ); - - graphicsIndex = static_cast( std::distance( queueFamilyProperties.begin(), graphicsQueueFamilyProperty ) ); - - // determine a queueFamilyIndex that supports present - // first check if the graphicsIndex is good enough - auto presentIndex = physicalDevice.getSurfaceSupportKHR( graphicsIndex, *surface ) - ? graphicsIndex - : ~0; - if ( presentIndex == queueFamilyProperties.size() ) + // get the first index into queueFamilyProperties which supports both graphics and present + for (uint32_t qfpIndex = 0; qfpIndex < queueFamilyProperties.size(); qfpIndex++) { - // the graphicsIndex doesn't support present -> look for another family index that supports both - // graphics and present - for ( size_t i = 0; i < queueFamilyProperties.size(); i++ ) + if ((queueFamilyProperties[qfpIndex].queueFlags & vk::QueueFlagBits::eGraphics) && + physicalDevice.getSurfaceSupportKHR(qfpIndex, *surface)) { - if ( ( queueFamilyProperties[i].queueFlags & vk::QueueFlagBits::eGraphics ) && - physicalDevice.getSurfaceSupportKHR( static_cast( i ), *surface ) ) - { - graphicsIndex = static_cast( i ); - presentIndex = graphicsIndex; - break; - } - } - if ( presentIndex == queueFamilyProperties.size() ) - { - // there's nothing like a single family index that supports both graphics and present -> look for another - // family index that supports present - for ( size_t i = 0; i < queueFamilyProperties.size(); i++ ) - { - if ( physicalDevice.getSurfaceSupportKHR( static_cast( i ), *surface ) ) - { - presentIndex = static_cast( i ); - break; - } - } + // found a queue family that supports both graphics and present + queueIndex = qfpIndex; + break; } } - if ( ( graphicsIndex == queueFamilyProperties.size() ) || ( presentIndex == queueFamilyProperties.size() ) ) + if (queueIndex == ~0) { - throw std::runtime_error( "Could not find a queue for graphics or present -> terminating" ); + throw std::runtime_error("Could not find a queue for graphics and present -> terminating"); } // query for Vulkan 1.3 features @@ -404,7 +371,7 @@ class HelloTriangleApplication { // create a Device float queuePriority = 0.0f; - vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = graphicsIndex, .queueCount = 1, .pQueuePriorities = &queuePriority }; + vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = queueIndex, .queueCount = 1, .pQueuePriorities = &queuePriority }; vk::DeviceCreateInfo deviceCreateInfo{ .pNext = &featureChain.get(), .queueCreateInfoCount = 1, .pQueueCreateInfos = &deviceQueueCreateInfo, @@ -412,8 +379,7 @@ class HelloTriangleApplication { .ppEnabledExtensionNames = requiredDeviceExtension.data() }; device = vk::raii::Device( physicalDevice, deviceCreateInfo ); - graphicsQueue = vk::raii::Queue( device, graphicsIndex, 0 ); - presentQueue = vk::raii::Queue( device, presentIndex, 0 ); + queue = vk::raii::Queue( device, queueIndex, 0 ); } void createSwapChain() { @@ -549,7 +515,7 @@ class HelloTriangleApplication { void createCommandPool() { vk::CommandPoolCreateInfo poolInfo{ .flags = vk::CommandPoolCreateFlagBits::eResetCommandBuffer, - .queueFamilyIndex = graphicsIndex + .queueFamilyIndex = queueIndex }; commandPool = vk::raii::CommandPool(device, poolInfo); } @@ -962,8 +928,8 @@ class HelloTriangleApplication { commandBuffer.end(); vk::SubmitInfo submitInfo{ .commandBufferCount = 1, .pCommandBuffers = &*commandBuffer }; - graphicsQueue.submit(submitInfo, nullptr); - graphicsQueue.waitIdle(); + queue.submit(submitInfo, nullptr); + queue.waitIdle(); } void copyBuffer(vk::raii::Buffer & srcBuffer, vk::raii::Buffer & dstBuffer, vk::DeviceSize size) { @@ -972,8 +938,8 @@ class HelloTriangleApplication { commandCopyBuffer.begin(vk::CommandBufferBeginInfo{ .flags = vk::CommandBufferUsageFlagBits::eOneTimeSubmit }); commandCopyBuffer.copyBuffer(*srcBuffer, *dstBuffer, vk::BufferCopy{ .size = size }); commandCopyBuffer.end(); - graphicsQueue.submit(vk::SubmitInfo{ .commandBufferCount = 1, .pCommandBuffers = &*commandCopyBuffer }, nullptr); - graphicsQueue.waitIdle(); + queue.submit(vk::SubmitInfo{ .commandBufferCount = 1, .pCommandBuffers = &*commandCopyBuffer }, nullptr); + queue.waitIdle(); } uint32_t findMemoryType(uint32_t typeFilter, vk::MemoryPropertyFlags properties) { @@ -1169,12 +1135,12 @@ class HelloTriangleApplication { const vk::SubmitInfo submitInfo{ .waitSemaphoreCount = 1, .pWaitSemaphores = &*presentCompleteSemaphore[semaphoreIndex], .pWaitDstStageMask = &waitDestinationStageMask, .commandBufferCount = 1, .pCommandBuffers = &*commandBuffers[currentFrame], .signalSemaphoreCount = 1, .pSignalSemaphores = &*renderFinishedSemaphore[imageIndex] }; - graphicsQueue.submit(submitInfo, *inFlightFences[currentFrame]); + queue.submit(submitInfo, *inFlightFences[currentFrame]); const vk::PresentInfoKHR presentInfoKHR{ .waitSemaphoreCount = 1, .pWaitSemaphores = &*renderFinishedSemaphore[imageIndex], .swapchainCount = 1, .pSwapchains = &*swapChain, .pImageIndices = &imageIndex }; - result = presentQueue.presentKHR(presentInfoKHR); + result = queue.presentKHR(presentInfoKHR); if (result == vk::Result::eErrorOutOfDateKHR || result == vk::Result::eSuboptimalKHR || framebufferResized) { framebufferResized = false; recreateSwapChain(); diff --git a/attachments/30_multisampling.cpp b/attachments/30_multisampling.cpp index fde957cc..e63036d7 100644 --- a/attachments/30_multisampling.cpp +++ b/attachments/30_multisampling.cpp @@ -95,19 +95,16 @@ class HelloTriangleApplication { } private: - GLFWwindow* window = nullptr; - - vk::raii::Context context; - vk::raii::Instance instance = nullptr; + GLFWwindow * window = nullptr; + vk::raii::Context context; + vk::raii::Instance instance = nullptr; vk::raii::DebugUtilsMessengerEXT debugMessenger = nullptr; - vk::raii::SurfaceKHR surface = nullptr; - - vk::raii::PhysicalDevice physicalDevice = nullptr; - vk::SampleCountFlagBits msaaSamples = vk::SampleCountFlagBits::e1; - vk::raii::Device device = nullptr; - - vk::raii::Queue graphicsQueue = nullptr; - vk::raii::Queue presentQueue = nullptr; + vk::raii::SurfaceKHR surface = nullptr; + vk::raii::PhysicalDevice physicalDevice = nullptr; + vk::SampleCountFlagBits msaaSamples = vk::SampleCountFlagBits::e1; + vk::raii::Device device = nullptr; + uint32_t queueIndex = ~0; + vk::raii::Queue queue = nullptr; vk::raii::SwapchainKHR swapChain = nullptr; std::vector swapChainImages; @@ -149,7 +146,6 @@ class HelloTriangleApplication { vk::raii::CommandPool commandPool = nullptr; std::vector commandBuffers; - uint32_t graphicsIndex = 0; std::vector presentCompleteSemaphore; std::vector renderFinishedSemaphore; @@ -357,51 +353,22 @@ class HelloTriangleApplication { } void createLogicalDevice() { - // find the index of the first queue family that supports graphics std::vector queueFamilyProperties = physicalDevice.getQueueFamilyProperties(); - // get the first index into queueFamilyProperties which supports graphics - auto graphicsQueueFamilyProperty = std::ranges::find_if( queueFamilyProperties, []( auto const & qfp ) - { return (qfp.queueFlags & vk::QueueFlagBits::eGraphics) != static_cast(0); } ); - - graphicsIndex = static_cast( std::distance( queueFamilyProperties.begin(), graphicsQueueFamilyProperty ) ); - - // determine a queueFamilyIndex that supports present - // first check if the graphicsIndex is good enough - auto presentIndex = physicalDevice.getSurfaceSupportKHR( graphicsIndex, *surface ) - ? graphicsIndex - : ~0; - if ( presentIndex == queueFamilyProperties.size() ) + // get the first index into queueFamilyProperties which supports both graphics and present + for (uint32_t qfpIndex = 0; qfpIndex < queueFamilyProperties.size(); qfpIndex++) { - // the graphicsIndex doesn't support present -> look for another family index that supports both - // graphics and present - for ( size_t i = 0; i < queueFamilyProperties.size(); i++ ) + if ((queueFamilyProperties[qfpIndex].queueFlags & vk::QueueFlagBits::eGraphics) && + physicalDevice.getSurfaceSupportKHR(qfpIndex, *surface)) { - if ( ( queueFamilyProperties[i].queueFlags & vk::QueueFlagBits::eGraphics ) && - physicalDevice.getSurfaceSupportKHR( static_cast( i ), *surface ) ) - { - graphicsIndex = static_cast( i ); - presentIndex = graphicsIndex; - break; - } - } - if ( presentIndex == queueFamilyProperties.size() ) - { - // there's nothing like a single family index that supports both graphics and present -> look for another - // family index that supports present - for ( size_t i = 0; i < queueFamilyProperties.size(); i++ ) - { - if ( physicalDevice.getSurfaceSupportKHR( static_cast( i ), *surface ) ) - { - presentIndex = static_cast( i ); + // found a queue family that supports both graphics and present + queueIndex = qfpIndex; break; } } - } - } - if ( ( graphicsIndex == queueFamilyProperties.size() ) || ( presentIndex == queueFamilyProperties.size() ) ) + if (queueIndex == ~0) { - throw std::runtime_error( "Could not find a queue for graphics or present -> terminating" ); + throw std::runtime_error("Could not find a queue for graphics and present -> terminating"); } // query for Vulkan 1.3 features @@ -413,7 +380,7 @@ class HelloTriangleApplication { // create a Device float queuePriority = 0.0f; - vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = graphicsIndex, .queueCount = 1, .pQueuePriorities = &queuePriority }; + vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = queueIndex, .queueCount = 1, .pQueuePriorities = &queuePriority }; vk::DeviceCreateInfo deviceCreateInfo{ .pNext = &featureChain.get(), .queueCreateInfoCount = 1, .pQueueCreateInfos = &deviceQueueCreateInfo, @@ -421,8 +388,7 @@ class HelloTriangleApplication { .ppEnabledExtensionNames = requiredDeviceExtension.data() }; device = vk::raii::Device( physicalDevice, deviceCreateInfo ); - graphicsQueue = vk::raii::Queue( device, graphicsIndex, 0 ); - presentQueue = vk::raii::Queue( device, presentIndex, 0 ); + queue = vk::raii::Queue( device, queueIndex, 0 ); } void createSwapChain() { @@ -558,7 +524,7 @@ class HelloTriangleApplication { void createCommandPool() { vk::CommandPoolCreateInfo poolInfo{ .flags = vk::CommandPoolCreateFlagBits::eResetCommandBuffer, - .queueFamilyIndex = graphicsIndex + .queueFamilyIndex = queueIndex }; commandPool = vk::raii::CommandPool(device, poolInfo); } @@ -992,8 +958,8 @@ class HelloTriangleApplication { commandBuffer.end(); vk::SubmitInfo submitInfo{ .commandBufferCount = 1, .pCommandBuffers = &*commandBuffer }; - graphicsQueue.submit(submitInfo, nullptr); - graphicsQueue.waitIdle(); + queue.submit(submitInfo, nullptr); + queue.waitIdle(); } void copyBuffer(vk::raii::Buffer & srcBuffer, vk::raii::Buffer & dstBuffer, vk::DeviceSize size) { @@ -1002,8 +968,8 @@ class HelloTriangleApplication { commandCopyBuffer.begin(vk::CommandBufferBeginInfo{ .flags = vk::CommandBufferUsageFlagBits::eOneTimeSubmit }); commandCopyBuffer.copyBuffer(*srcBuffer, *dstBuffer, vk::BufferCopy{ .size = size }); commandCopyBuffer.end(); - graphicsQueue.submit(vk::SubmitInfo{ .commandBufferCount = 1, .pCommandBuffers = &*commandCopyBuffer }, nullptr); - graphicsQueue.waitIdle(); + queue.submit(vk::SubmitInfo{ .commandBufferCount = 1, .pCommandBuffers = &*commandCopyBuffer }, nullptr); + queue.waitIdle(); } uint32_t findMemoryType(uint32_t typeFilter, vk::MemoryPropertyFlags properties) { @@ -1241,12 +1207,12 @@ class HelloTriangleApplication { const vk::SubmitInfo submitInfo{ .waitSemaphoreCount = 1, .pWaitSemaphores = &*presentCompleteSemaphore[semaphoreIndex], .pWaitDstStageMask = &waitDestinationStageMask, .commandBufferCount = 1, .pCommandBuffers = &*commandBuffers[currentFrame], .signalSemaphoreCount = 1, .pSignalSemaphores = &*renderFinishedSemaphore[imageIndex] }; - graphicsQueue.submit(submitInfo, *inFlightFences[currentFrame]); + queue.submit(submitInfo, *inFlightFences[currentFrame]); const vk::PresentInfoKHR presentInfoKHR{ .waitSemaphoreCount = 1, .pWaitSemaphores = &*renderFinishedSemaphore[imageIndex], .swapchainCount = 1, .pSwapchains = &*swapChain, .pImageIndices = &imageIndex }; - result = presentQueue.presentKHR(presentInfoKHR); + result = queue.presentKHR(presentInfoKHR); if (result == vk::Result::eErrorOutOfDateKHR || result == vk::Result::eSuboptimalKHR || framebufferResized) { framebufferResized = false; recreateSwapChain(); diff --git a/attachments/31_compute_shader.cpp b/attachments/31_compute_shader.cpp index 72c54651..180aa3d4 100644 --- a/attachments/31_compute_shader.cpp +++ b/attachments/31_compute_shader.cpp @@ -77,19 +77,15 @@ class ComputeShaderApplication { } private: - GLFWwindow* window = nullptr; - - vk::raii::Context context; - vk::raii::Instance instance = nullptr; + GLFWwindow * window = nullptr; + vk::raii::Context context; + vk::raii::Instance instance = nullptr; vk::raii::DebugUtilsMessengerEXT debugMessenger = nullptr; - vk::raii::SurfaceKHR surface = nullptr; - - vk::raii::PhysicalDevice physicalDevice = nullptr; - vk::raii::Device device = nullptr; - - vk::raii::Queue graphicsQueue = nullptr; - vk::raii::Queue computeQueue = nullptr; - vk::raii::Queue presentQueue = nullptr; + vk::raii::SurfaceKHR surface = nullptr; + vk::raii::PhysicalDevice physicalDevice = nullptr; + vk::raii::Device device = nullptr; + uint32_t queueIndex = ~0; + vk::raii::Queue queue = nullptr; vk::raii::SwapchainKHR swapChain = nullptr; std::vector swapChainImages; @@ -118,7 +114,6 @@ class ComputeShaderApplication { vk::raii::CommandPool commandPool = nullptr; std::vector commandBuffers; std::vector computeCommandBuffers; - uint32_t graphicsAndComputeIndex = 0; vk::raii::Semaphore semaphore = nullptr; uint64_t timelineValue = 0; @@ -331,51 +326,23 @@ class ComputeShaderApplication { } void createLogicalDevice() { - // find the index of the first queue family that supports graphics std::vector queueFamilyProperties = physicalDevice.getQueueFamilyProperties(); - // get the first index into queueFamilyProperties which supports graphics and compute - auto graphicsAndComputeQueueFamilyProperty = std::ranges::find_if(queueFamilyProperties, []( auto const & qfp ) - { return (qfp.queueFlags & vk::QueueFlagBits::eGraphics && qfp.queueFlags & vk::QueueFlagBits::eCompute); } ); - - graphicsAndComputeIndex = static_cast( std::distance( queueFamilyProperties.begin(), graphicsAndComputeQueueFamilyProperty ) ); - - // determine a queueFamilyIndex that supports present - // first check if the graphicsIndex is good enough - auto presentIndex = physicalDevice.getSurfaceSupportKHR( graphicsAndComputeIndex, surface ) - ? graphicsAndComputeIndex - : ~0; - if ( presentIndex == queueFamilyProperties.size() ) + // get the first index into queueFamilyProperties which supports both graphics and present + for (uint32_t qfpIndex = 0; qfpIndex < queueFamilyProperties.size(); qfpIndex++) { - // the graphicsIndex doesn't support present -> look for another family index that supports both - // graphics and present - for ( size_t i = 0; i < queueFamilyProperties.size(); i++ ) + if ((queueFamilyProperties[qfpIndex].queueFlags & vk::QueueFlagBits::eGraphics) && + (queueFamilyProperties[qfpIndex].queueFlags & vk::QueueFlagBits::eCompute) && + physicalDevice.getSurfaceSupportKHR(qfpIndex, *surface)) { - if ( ( (queueFamilyProperties[i].queueFlags & vk::QueueFlagBits::eGraphics && queueFamilyProperties[i].queueFlags & vk::QueueFlagBits::eCompute) ) && - physicalDevice.getSurfaceSupportKHR( static_cast( i ), surface ) ) - { - graphicsAndComputeIndex = static_cast( i ); - presentIndex = graphicsAndComputeIndex; - break; - } - } - if ( presentIndex == queueFamilyProperties.size() ) - { - // there's nothing like a single family index that supports both graphics and present -> look for another - // family index that supports present - for ( size_t i = 0; i < queueFamilyProperties.size(); i++ ) - { - if ( physicalDevice.getSurfaceSupportKHR( static_cast( i ), surface ) ) - { - presentIndex = static_cast( i ); - break; - } - } + // found a queue family that supports both graphics and present + queueIndex = qfpIndex; + break; } } - if ( ( graphicsAndComputeIndex == queueFamilyProperties.size() ) || ( presentIndex == queueFamilyProperties.size() ) ) + if (queueIndex == ~0) { - throw std::runtime_error( "Could not find a queue for graphics or present -> terminating" ); + throw std::runtime_error("Could not find a queue for graphics and present -> terminating"); } // query for Vulkan 1.3 features @@ -392,7 +359,7 @@ class ComputeShaderApplication { // create a Device float queuePriority = 0.0f; - vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = graphicsAndComputeIndex, .queueCount = 1, .pQueuePriorities = &queuePriority }; + vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = queueIndex, .queueCount = 1, .pQueuePriorities = &queuePriority }; vk::DeviceCreateInfo deviceCreateInfo{ .pNext = &featureChain.get(), .queueCreateInfoCount = 1, .pQueueCreateInfos = &deviceQueueCreateInfo, @@ -400,9 +367,7 @@ class ComputeShaderApplication { .ppEnabledExtensionNames = requiredDeviceExtension.data() }; device = vk::raii::Device( physicalDevice, deviceCreateInfo ); - graphicsQueue = vk::raii::Queue( device, graphicsAndComputeIndex, 0 ); - computeQueue = vk::raii::Queue( device, graphicsAndComputeIndex, 0 ); - presentQueue = vk::raii::Queue( device, presentIndex, 0 ); + queue = vk::raii::Queue( device, queueIndex, 0 ); } void createSwapChain() { @@ -528,7 +493,7 @@ class ComputeShaderApplication { void createCommandPool() { vk::CommandPoolCreateInfo poolInfo{}; poolInfo.flags = vk::CommandPoolCreateFlagBits::eResetCommandBuffer; - poolInfo.queueFamilyIndex = graphicsAndComputeIndex; + poolInfo.queueFamilyIndex = queueIndex; commandPool = vk::raii::CommandPool(device, poolInfo); } @@ -660,8 +625,8 @@ class ComputeShaderApplication { vk::SubmitInfo submitInfo{}; submitInfo.commandBufferCount = 1; submitInfo.pCommandBuffers = &*commandBuffer; - graphicsQueue.submit(submitInfo, nullptr); - graphicsQueue.waitIdle(); + queue.submit(submitInfo, nullptr); + queue.waitIdle(); } void copyBuffer(const vk::raii::Buffer & srcBuffer, const vk::raii::Buffer & dstBuffer, vk::DeviceSize size) const { @@ -848,7 +813,7 @@ class ComputeShaderApplication { .pSignalSemaphores = &*semaphore }; - computeQueue.submit(computeSubmitInfo, nullptr); + queue.submit(computeSubmitInfo, nullptr); } { // Record graphics command buffer @@ -874,7 +839,7 @@ class ComputeShaderApplication { .pSignalSemaphores = &*semaphore }; - graphicsQueue.submit(graphicsSubmitInfo, nullptr); + queue.submit(graphicsSubmitInfo, nullptr); // Present the image (wait for graphics to finish) vk::SemaphoreWaitInfo waitInfo{ @@ -895,7 +860,7 @@ class ComputeShaderApplication { .pImageIndices = &imageIndex }; - result = presentQueue.presentKHR(presentInfo); + result = queue.presentKHR(presentInfo); if (result == vk::Result::eErrorOutOfDateKHR || result == vk::Result::eSuboptimalKHR || framebufferResized) { framebufferResized = false; recreateSwapChain(); diff --git a/attachments/32_ecosystem_utilities.cpp b/attachments/32_ecosystem_utilities.cpp index ced855ae..5036be15 100644 --- a/attachments/32_ecosystem_utilities.cpp +++ b/attachments/32_ecosystem_utilities.cpp @@ -95,20 +95,18 @@ class HelloTriangleApplication { } private: - GLFWwindow* window = nullptr; AppInfo appInfo; - vk::raii::Context context; - vk::raii::Instance instance = nullptr; + GLFWwindow * window = nullptr; + vk::raii::Context context; + vk::raii::Instance instance = nullptr; vk::raii::DebugUtilsMessengerEXT debugMessenger = nullptr; - vk::raii::SurfaceKHR surface = nullptr; - - vk::raii::PhysicalDevice physicalDevice = nullptr; - vk::SampleCountFlagBits msaaSamples = vk::SampleCountFlagBits::e1; - vk::raii::Device device = nullptr; - - vk::raii::Queue graphicsQueue = nullptr; - vk::raii::Queue presentQueue = nullptr; + vk::raii::SurfaceKHR surface = nullptr; + vk::raii::PhysicalDevice physicalDevice = nullptr; + vk::SampleCountFlagBits msaaSamples = vk::SampleCountFlagBits::e1; + vk::raii::Device device = nullptr; + uint32_t queueIndex = ~0; + vk::raii::Queue queue = nullptr; vk::raii::SwapchainKHR swapChain = nullptr; std::vector swapChainImages; @@ -154,7 +152,6 @@ class HelloTriangleApplication { vk::raii::CommandPool commandPool = nullptr; std::vector commandBuffers; - uint32_t graphicsIndex = 0; // Synchronization objects std::vector presentCompleteSemaphore; @@ -431,51 +428,22 @@ class HelloTriangleApplication { } void createLogicalDevice() { - // find the index of the first queue family that supports graphics std::vector queueFamilyProperties = physicalDevice.getQueueFamilyProperties(); - // get the first index into queueFamilyProperties which supports graphics - auto graphicsQueueFamilyProperty = std::ranges::find_if( queueFamilyProperties, []( auto const & qfp ) - { return (qfp.queueFlags & vk::QueueFlagBits::eGraphics) != static_cast(0); } ); - - graphicsIndex = static_cast( std::distance( queueFamilyProperties.begin(), graphicsQueueFamilyProperty ) ); - - // determine a queueFamilyIndex that supports present - // first check if the graphicsIndex is good enough - auto presentIndex = physicalDevice.getSurfaceSupportKHR( graphicsIndex, *surface ) - ? graphicsIndex - : ~0; - if ( presentIndex == queueFamilyProperties.size() ) + // get the first index into queueFamilyProperties which supports both graphics and present + for (uint32_t qfpIndex = 0; qfpIndex < queueFamilyProperties.size(); qfpIndex++) { - // the graphicsIndex doesn't support present -> look for another family index that supports both - // graphics and present - for ( size_t i = 0; i < queueFamilyProperties.size(); i++ ) - { - if ( ( queueFamilyProperties[i].queueFlags & vk::QueueFlagBits::eGraphics ) && - physicalDevice.getSurfaceSupportKHR( static_cast( i ), *surface ) ) - { - graphicsIndex = static_cast( i ); - presentIndex = graphicsIndex; - break; - } - } - if ( presentIndex == queueFamilyProperties.size() ) + if ((queueFamilyProperties[qfpIndex].queueFlags & vk::QueueFlagBits::eGraphics) && + physicalDevice.getSurfaceSupportKHR(qfpIndex, *surface)) { - // there's nothing like a single family index that supports both graphics and present -> look for another - // family index that supports present - for ( size_t i = 0; i < queueFamilyProperties.size(); i++ ) - { - if ( physicalDevice.getSurfaceSupportKHR( static_cast( i ), *surface ) ) - { - presentIndex = static_cast( i ); + // found a queue family that supports both graphics and present + queueIndex = qfpIndex; break; } } - } - } - if ( ( graphicsIndex == queueFamilyProperties.size() ) || ( presentIndex == queueFamilyProperties.size() ) ) + if (queueIndex == ~0) { - throw std::runtime_error( "Could not find a queue for graphics or present -> terminating" ); + throw std::runtime_error("Could not find a queue for graphics and present -> terminating"); } // Create device with appropriate features @@ -513,7 +481,7 @@ class HelloTriangleApplication { // create a Device float queuePriority = 0.0f; - vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = graphicsIndex, .queueCount = 1, .pQueuePriorities = &queuePriority }; + vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = queueIndex, .queueCount = 1, .pQueuePriorities = &queuePriority }; vk::DeviceCreateInfo deviceCreateInfo{ .pNext = &features, .queueCreateInfoCount = 1, @@ -523,8 +491,7 @@ class HelloTriangleApplication { }; device = vk::raii::Device( physicalDevice, deviceCreateInfo ); - graphicsQueue = vk::raii::Queue( device, graphicsIndex, 0 ); - presentQueue = vk::raii::Queue( device, presentIndex, 0 ); + queue = vk::raii::Queue( device, queueIndex, 0 ); } void createSwapChain() { @@ -793,7 +760,7 @@ class HelloTriangleApplication { void createCommandPool() { vk::CommandPoolCreateInfo poolInfo{ .flags = vk::CommandPoolCreateFlagBits::eResetCommandBuffer, - .queueFamilyIndex = graphicsIndex + .queueFamilyIndex = queueIndex }; commandPool = vk::raii::CommandPool(device, poolInfo); } @@ -1262,8 +1229,8 @@ class HelloTriangleApplication { commandBuffer.end(); vk::SubmitInfo submitInfo{ .commandBufferCount = 1, .pCommandBuffers = &*commandBuffer }; - graphicsQueue.submit(submitInfo, nullptr); - graphicsQueue.waitIdle(); + queue.submit(submitInfo, nullptr); + queue.waitIdle(); } void copyBuffer(vk::raii::Buffer & srcBuffer, vk::raii::Buffer & dstBuffer, vk::DeviceSize size) { @@ -1272,8 +1239,8 @@ class HelloTriangleApplication { commandCopyBuffer.begin(vk::CommandBufferBeginInfo{ .flags = vk::CommandBufferUsageFlagBits::eOneTimeSubmit }); commandCopyBuffer.copyBuffer(*srcBuffer, *dstBuffer, vk::BufferCopy{ .size = size }); commandCopyBuffer.end(); - graphicsQueue.submit(vk::SubmitInfo{ .commandBufferCount = 1, .pCommandBuffers = &*commandCopyBuffer }, nullptr); - graphicsQueue.waitIdle(); + queue.submit(vk::SubmitInfo{ .commandBufferCount = 1, .pCommandBuffers = &*commandCopyBuffer }, nullptr); + queue.waitIdle(); } uint32_t findMemoryType(uint32_t typeFilter, vk::MemoryPropertyFlags properties) { @@ -1600,7 +1567,7 @@ class HelloTriangleApplication { .pSignalSemaphores = signalSemaphores.data() }; - graphicsQueue.submit(submitInfo, *inFlightFences[currentFrame]); + queue.submit(submitInfo, *inFlightFences[currentFrame]); } else { // Use traditional binary semaphores vk::PipelineStageFlags waitDestinationStageMask(vk::PipelineStageFlagBits::eColorAttachmentOutput); @@ -1613,7 +1580,7 @@ class HelloTriangleApplication { .signalSemaphoreCount = 1, .pSignalSemaphores = &*renderFinishedSemaphore[currentFrame] }; - graphicsQueue.submit(submitInfo, *inFlightFences[currentFrame]); + queue.submit(submitInfo, *inFlightFences[currentFrame]); } const vk::PresentInfoKHR presentInfoKHR{ @@ -1623,7 +1590,7 @@ class HelloTriangleApplication { .pSwapchains = &*swapChain, .pImageIndices = &imageIndex }; - result = presentQueue.presentKHR(presentInfoKHR); + result = queue.presentKHR(presentInfoKHR); if (result == vk::Result::eErrorOutOfDateKHR || result == vk::Result::eSuboptimalKHR || framebufferResized) { framebufferResized = false; recreateSwapChain(); diff --git a/attachments/33_vulkan_profiles.cpp b/attachments/33_vulkan_profiles.cpp index a9bd7883..b50975fb 100644 --- a/attachments/33_vulkan_profiles.cpp +++ b/attachments/33_vulkan_profiles.cpp @@ -94,15 +94,15 @@ class HelloTriangleApplication { } private: - GLFWwindow* window = nullptr; - vk::raii::Context context; - vk::raii::Instance instance = nullptr; + GLFWwindow * window = nullptr; + vk::raii::Context context; + vk::raii::Instance instance = nullptr; vk::raii::DebugUtilsMessengerEXT debugMessenger = nullptr; - vk::raii::SurfaceKHR surface = nullptr; - vk::raii::PhysicalDevice physicalDevice = nullptr; - vk::raii::Device device = nullptr; - vk::raii::Queue graphicsQueue = nullptr; - vk::raii::Queue presentQueue = nullptr; + vk::raii::SurfaceKHR surface = nullptr; + vk::raii::PhysicalDevice physicalDevice = nullptr; + vk::raii::Device device = nullptr; + uint32_t queueIndex = ~0; + vk::raii::Queue queue = nullptr; vk::raii::SwapchainKHR swapChain = nullptr; std::vector swapChainImages; vk::Format swapChainImageFormat = {}; @@ -153,15 +153,6 @@ class HelloTriangleApplication { std::vector presentModes; }; - struct QueueFamilyIndices { - std::optional graphicsFamily; - std::optional presentFamily; - - bool isComplete() const { - return graphicsFamily.has_value() && presentFamily.has_value(); - } - }; - const std::vector requiredDeviceExtension = { VK_KHR_SWAPCHAIN_EXTENSION_NAME }; @@ -407,20 +398,26 @@ class HelloTriangleApplication { } void createLogicalDevice() { - QueueFamilyIndices indices = findQueueFamilies(physicalDevice); - - std::vector queueCreateInfos; - std::set uniqueQueueFamilies = {indices.graphicsFamily.value(), indices.presentFamily.value()}; + std::vector queueFamilyProperties = physicalDevice.getQueueFamilyProperties(); + + // get the first index into queueFamilyProperties which supports both graphics and present + for (uint32_t qfpIndex = 0; qfpIndex < queueFamilyProperties.size(); qfpIndex++) + { + if ((queueFamilyProperties[qfpIndex].queueFlags & vk::QueueFlagBits::eGraphics) && + physicalDevice.getSurfaceSupportKHR(qfpIndex, *surface)) + { + // found a queue family that supports both graphics and present + queueIndex = qfpIndex; + break; + } + } + if (queueIndex == ~0) + { + throw std::runtime_error("Could not find a queue for graphics and present -> terminating"); + } float queuePriority = 1.0f; - for (uint32_t queueFamily : uniqueQueueFamilies) { - vk::DeviceQueueCreateInfo queueCreateInfo{ - .queueFamilyIndex = queueFamily, - .queueCount = 1, - .pQueuePriorities = &queuePriority - }; - queueCreateInfos.push_back(queueCreateInfo); - } + vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = queueIndex, .queueCount = 1, .pQueuePriorities = &queuePriority }; if (appInfo.profileSupported) { // Create device with Best Practices profile @@ -440,8 +437,8 @@ class HelloTriangleApplication { // Create a vk::DeviceCreateInfo with the required features vk::DeviceCreateInfo vkDeviceCreateInfo{ .pNext = &features2, - .queueCreateInfoCount = static_cast(queueCreateInfos.size()), - .pQueueCreateInfos = queueCreateInfos.data(), + .queueCreateInfoCount = 1, + .pQueueCreateInfos = &deviceQueueCreateInfo, .enabledExtensionCount = static_cast(requiredDeviceExtension.size()), .ppEnabledExtensionNames = requiredDeviceExtension.data() }; @@ -457,8 +454,8 @@ class HelloTriangleApplication { deviceFeatures.sampleRateShading = VK_TRUE; vk::DeviceCreateInfo createInfo{ - .queueCreateInfoCount = static_cast(queueCreateInfos.size()), - .pQueueCreateInfos = queueCreateInfos.data(), + .queueCreateInfoCount = 1, + .pQueueCreateInfos = &deviceQueueCreateInfo, .enabledExtensionCount = static_cast(requiredDeviceExtension.size()), .ppEnabledExtensionNames = requiredDeviceExtension.data(), .pEnabledFeatures = &deviceFeatures @@ -469,8 +466,7 @@ class HelloTriangleApplication { std::cout << "Created logical device using manual feature selection" << std::endl; } - graphicsQueue = device.getQueue(indices.graphicsFamily.value(), 0); - presentQueue = device.getQueue(indices.presentFamily.value(), 0); + queue = device.getQueue(queueIndex, 0); } void createSwapChain() { @@ -492,20 +488,10 @@ class HelloTriangleApplication { .imageColorSpace = surfaceFormat.colorSpace, .imageExtent = extent, .imageArrayLayers = 1, - .imageUsage = vk::ImageUsageFlagBits::eColorAttachment + .imageUsage = vk::ImageUsageFlagBits::eColorAttachment, + .imageSharingMode = vk::SharingMode::eExclusive }; - QueueFamilyIndices indices = findQueueFamilies(physicalDevice); - uint32_t queueFamilyIndices[] = {indices.graphicsFamily.value(), indices.presentFamily.value()}; - - if (indices.graphicsFamily != indices.presentFamily) { - createInfo.imageSharingMode = vk::SharingMode::eConcurrent; - createInfo.queueFamilyIndexCount = 2; - createInfo.pQueueFamilyIndices = queueFamilyIndices; - } else { - createInfo.imageSharingMode = vk::SharingMode::eExclusive; - } - createInfo.preTransform = swapChainSupport.capabilities.currentTransform; createInfo.compositeAlpha = vk::CompositeAlphaFlagBitsKHR::eOpaque; createInfo.presentMode = presentMode; @@ -792,11 +778,9 @@ class HelloTriangleApplication { } void createCommandPool() { - QueueFamilyIndices queueFamilyIndices = findQueueFamilies(physicalDevice); - vk::CommandPoolCreateInfo poolInfo{ .flags = vk::CommandPoolCreateFlagBits::eResetCommandBuffer, - .queueFamilyIndex = queueFamilyIndices.graphicsFamily.value() + .queueFamilyIndex = queueIndex }; commandPool = device.createCommandPool(poolInfo); @@ -1307,8 +1291,8 @@ class HelloTriangleApplication { .pCommandBuffers = &*commandBuffer }; - graphicsQueue.submit(submitInfo, nullptr); - graphicsQueue.waitIdle(); + queue.submit(submitInfo, nullptr); + queue.waitIdle(); } void loadModel() { @@ -1573,7 +1557,7 @@ class HelloTriangleApplication { .signalSemaphoreCount = 1, .pSignalSemaphores = &*presentCompleteSemaphore[imageIndex] }; - graphicsQueue.submit(submitInfo, *inFlightFences[currentFrame]); + queue.submit(submitInfo, *inFlightFences[currentFrame]); const vk::PresentInfoKHR presentInfoKHR{ .waitSemaphoreCount = 1, @@ -1585,7 +1569,7 @@ class HelloTriangleApplication { vk::Result result; try { - result = presentQueue.presentKHR(presentInfoKHR); + result = queue.presentKHR(presentInfoKHR); } catch (vk::OutOfDateKHRError&) { result = vk::Result::eErrorOutOfDateKHR; } @@ -1711,33 +1695,6 @@ class HelloTriangleApplication { return details; } - - QueueFamilyIndices findQueueFamilies(vk::raii::PhysicalDevice device) { - QueueFamilyIndices indices; - - std::vector queueFamilies = device.getQueueFamilyProperties(); - - uint32_t i = 0; - for (const auto& queueFamily : queueFamilies) { - if (queueFamily.queueFlags & vk::QueueFlagBits::eGraphics) { - indices.graphicsFamily = i; - } - - vk::Bool32 presentSupport = device.getSurfaceSupportKHR(i, *surface); - - if (presentSupport) { - indices.presentFamily = i; - } - - if (indices.isComplete()) { - break; - } - - i++; - } - - return indices; - } }; int main() { diff --git a/attachments/34_android.cpp b/attachments/34_android.cpp index e72c21f0..977c8575 100644 --- a/attachments/34_android.cpp +++ b/attachments/34_android.cpp @@ -293,13 +293,14 @@ class HelloTriangleApplication { bool framebufferResized = false; // Vulkan objects - vk::raii::Context context; - vk::raii::Instance instance = nullptr; - vk::raii::SurfaceKHR surface = nullptr; - vk::raii::PhysicalDevice physicalDevice = nullptr; - vk::raii::Device device = nullptr; - vk::raii::Queue graphicsQueue = nullptr; - vk::raii::Queue presentQueue = nullptr; + vk::raii::Context context; + vk::raii::Instance instance = nullptr; + vk::raii::DebugUtilsMessengerEXT debugMessenger = nullptr; + vk::raii::SurfaceKHR surface = nullptr; + vk::raii::PhysicalDevice physicalDevice = nullptr; + vk::raii::Device device = nullptr; + uint32_t queueIndex = ~0; + vk::raii::Queue queue = nullptr; vk::raii::SwapchainKHR swapChain = nullptr; std::vector swapChainImages; vk::Format swapChainImageFormat = {}; @@ -336,16 +337,6 @@ class HelloTriangleApplication { std::vector vertices; std::vector indices; - // Queue family indices - struct QueueFamilyIndices { - std::optional graphicsFamily; - std::optional presentFamily; - - bool isComplete() const { - return graphicsFamily.has_value() && presentFamily.has_value(); - } - }; - // Swap chain support details struct SwapChainSupportDetails { vk::SurfaceCapabilitiesKHR capabilities; @@ -530,20 +521,26 @@ class HelloTriangleApplication { // Create logical device void createLogicalDevice() { - QueueFamilyIndices indices = findQueueFamilies(physicalDevice); + std::vector queueFamilyProperties = physicalDevice.getQueueFamilyProperties(); - std::vector queueCreateInfos; - std::set uniqueQueueFamilies = {indices.graphicsFamily.value(), indices.presentFamily.value()}; + // get the first index into queueFamilyProperties which supports both graphics and present + for (uint32_t qfpIndex = 0; qfpIndex < queueFamilyProperties.size(); qfpIndex++) + { + if ((queueFamilyProperties[qfpIndex].queueFlags & vk::QueueFlagBits::eGraphics) && + physicalDevice.getSurfaceSupportKHR(qfpIndex, *surface)) + { + // found a queue family that supports both graphics and present + queueIndex = qfpIndex; + break; + } + } + if (queueIndex == ~0) + { + throw std::runtime_error("Could not find a queue for graphics and present -> terminating"); + } float queuePriority = 1.0f; - for (uint32_t queueFamily : uniqueQueueFamilies) { - vk::DeviceQueueCreateInfo queueCreateInfo{ - .queueFamilyIndex = queueFamily, - .queueCount = 1, - .pQueuePriorities = &queuePriority - }; - queueCreateInfos.push_back(queueCreateInfo); - } + vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = queueIndex, .queueCount = 1, .pQueuePriorities = &queuePriority }; if (appInfo.profileSupported) { // Enable required features @@ -561,8 +558,8 @@ class HelloTriangleApplication { // Create a vk::DeviceCreateInfo with the required features vk::DeviceCreateInfo vkDeviceCreateInfo{ .pNext = &features2, - .queueCreateInfoCount = static_cast(queueCreateInfos.size()), - .pQueueCreateInfos = queueCreateInfos.data(), + .queueCreateInfoCount = 1, + .pQueueCreateInfos = &deviceQueueCreateInfo, .enabledExtensionCount = static_cast(deviceExtensions.size()), .ppEnabledExtensionNames = deviceExtensions.data() }; @@ -576,8 +573,8 @@ class HelloTriangleApplication { deviceFeatures.sampleRateShading = VK_TRUE; vk::DeviceCreateInfo createInfo{ - .queueCreateInfoCount = static_cast(queueCreateInfos.size()), - .pQueueCreateInfos = queueCreateInfos.data(), + .queueCreateInfoCount = 1, + .pQueueCreateInfos = &deviceQueueCreateInfo, .enabledExtensionCount = static_cast(deviceExtensions.size()), .ppEnabledExtensionNames = deviceExtensions.data(), .pEnabledFeatures = &deviceFeatures @@ -586,8 +583,7 @@ class HelloTriangleApplication { device = vk::raii::Device(physicalDevice, createInfo); } - graphicsQueue = device.getQueue(indices.graphicsFamily.value(), 0); - presentQueue = device.getQueue(indices.presentFamily.value(), 0); + queue = device.getQueue(queueIndex, 0); } // Create swap chain @@ -610,20 +606,10 @@ class HelloTriangleApplication { .imageColorSpace = surfaceFormat.colorSpace, .imageExtent = extent, .imageArrayLayers = 1, - .imageUsage = vk::ImageUsageFlagBits::eColorAttachment + .imageUsage = vk::ImageUsageFlagBits::eColorAttachment, + .imageSharingMode = vk::SharingMode::eExclusive }; - QueueFamilyIndices indices = findQueueFamilies(physicalDevice); - uint32_t queueFamilyIndices[] = {indices.graphicsFamily.value(), indices.presentFamily.value()}; - - if (indices.graphicsFamily != indices.presentFamily) { - createInfo.imageSharingMode = vk::SharingMode::eConcurrent; - createInfo.queueFamilyIndexCount = 2; - createInfo.pQueueFamilyIndices = queueFamilyIndices; - } else { - createInfo.imageSharingMode = vk::SharingMode::eExclusive; - } - createInfo.preTransform = swapChainSupport.capabilities.currentTransform; createInfo.compositeAlpha = vk::CompositeAlphaFlagBitsKHR::eOpaque; createInfo.presentMode = presentMode; @@ -894,11 +880,9 @@ class HelloTriangleApplication { // Create command pool void createCommandPool() { - QueueFamilyIndices queueFamilyIndices = findQueueFamilies(physicalDevice); - vk::CommandPoolCreateInfo poolInfo{ .flags = vk::CommandPoolCreateFlagBits::eResetCommandBuffer, - .queueFamilyIndex = queueFamilyIndices.graphicsFamily.value() + .queueFamilyIndex = queueIndex }; commandPool = device.createCommandPool(poolInfo); @@ -1310,7 +1294,7 @@ class HelloTriangleApplication { .signalSemaphoreCount = 1, .pSignalSemaphores = &*renderFinishedSemaphores[currentFrame] }; - graphicsQueue.submit(submitInfo, *inFlightFences[currentFrame]); + queue.submit(submitInfo, *inFlightFences[currentFrame]); const vk::PresentInfoKHR presentInfoKHR{ .waitSemaphoreCount = 1, @@ -1322,7 +1306,7 @@ class HelloTriangleApplication { vk::Result result; try { - result = presentQueue.presentKHR(presentInfoKHR); + result = queue.presentKHR(presentInfoKHR); } catch (vk::OutOfDateKHRError&) { result = vk::Result::eErrorOutOfDateKHR; } @@ -1452,33 +1436,6 @@ class HelloTriangleApplication { return details; } - // Find queue families - QueueFamilyIndices findQueueFamilies(vk::raii::PhysicalDevice device) { - QueueFamilyIndices indices; - - std::vector queueFamilies = device.getQueueFamilyProperties(); - - uint32_t i = 0; - for (const auto& queueFamily : queueFamilies) { - if (queueFamily.queueFlags & vk::QueueFlagBits::eGraphics) { - indices.graphicsFamily = i; - } - - vk::Bool32 presentSupport = device.getSurfaceSupportKHR(i, *surface); - if (presentSupport) { - indices.presentFamily = i; - } - - if (indices.isComplete()) { - break; - } - - i++; - } - - return indices; - } - // Create buffer void createBuffer(vk::DeviceSize size, vk::BufferUsageFlags usage, vk::MemoryPropertyFlags properties, vk::raii::Buffer& buffer, vk::raii::DeviceMemory& bufferMemory) { vk::BufferCreateInfo bufferInfo{ @@ -1529,8 +1486,8 @@ class HelloTriangleApplication { .pCommandBuffers = &*commandBuffer }; - graphicsQueue.submit(submitInfo, nullptr); - graphicsQueue.waitIdle(); + queue.submit(submitInfo, nullptr); + queue.waitIdle(); } // Find memory type @@ -1628,8 +1585,8 @@ class HelloTriangleApplication { .pCommandBuffers = &*commandBuffer }; - graphicsQueue.submit(submitInfo, nullptr); - graphicsQueue.waitIdle(); + queue.submit(submitInfo, nullptr); + queue.waitIdle(); } // Copy buffer to image @@ -1675,8 +1632,8 @@ class HelloTriangleApplication { .pCommandBuffers = &*commandBuffer }; - graphicsQueue.submit(submitInfo, nullptr); - graphicsQueue.waitIdle(); + queue.submit(submitInfo, nullptr); + queue.waitIdle(); } // Update uniform buffer diff --git a/attachments/35_gltf_ktx.cpp b/attachments/35_gltf_ktx.cpp index 29b11304..43c2fc2a 100644 --- a/attachments/35_gltf_ktx.cpp +++ b/attachments/35_gltf_ktx.cpp @@ -239,16 +239,14 @@ class VulkanApplication { #endif AppInfo appInfo; - vk::raii::Context context; - vk::raii::Instance instance = nullptr; + vk::raii::Context context; + vk::raii::Instance instance = nullptr; vk::raii::DebugUtilsMessengerEXT debugMessenger = nullptr; - vk::raii::SurfaceKHR surface = nullptr; - - vk::raii::PhysicalDevice physicalDevice = nullptr; - vk::raii::Device device = nullptr; - - vk::raii::Queue graphicsQueue = nullptr; - vk::raii::Queue presentQueue = nullptr; + vk::raii::SurfaceKHR surface = nullptr; + vk::raii::PhysicalDevice physicalDevice = nullptr; + vk::raii::Device device = nullptr; + uint32_t queueIndex = ~0; + vk::raii::Queue queue = nullptr; vk::raii::SwapchainKHR swapChain = nullptr; std::vector swapChainImages; @@ -286,7 +284,6 @@ class VulkanApplication { vk::raii::CommandPool commandPool = nullptr; std::vector commandBuffers; - uint32_t graphicsIndex = 0; std::vector presentCompleteSemaphore; std::vector renderFinishedSemaphore; @@ -523,51 +520,22 @@ class VulkanApplication { } void createLogicalDevice() { - // find the index of the first queue family that supports graphics std::vector queueFamilyProperties = physicalDevice.getQueueFamilyProperties(); - // get the first index into queueFamilyProperties which supports graphics - auto graphicsQueueFamilyProperty = std::ranges::find_if( queueFamilyProperties, []( auto const & qfp ) - { return (qfp.queueFlags & vk::QueueFlagBits::eGraphics) != static_cast(0); } ); - - graphicsIndex = static_cast( std::distance( queueFamilyProperties.begin(), graphicsQueueFamilyProperty ) ); - - // determine a queueFamilyIndex that supports present - // first check if the graphicsIndex is good enough - auto presentIndex = physicalDevice.getSurfaceSupportKHR( graphicsIndex, *surface ) - ? graphicsIndex - : ~0; - if ( presentIndex == queueFamilyProperties.size() ) + // get the first index into queueFamilyProperties which supports both graphics and present + for (uint32_t qfpIndex = 0; qfpIndex < queueFamilyProperties.size(); qfpIndex++) { - // the graphicsIndex doesn't support present -> look for another family index that supports both - // graphics and present - for ( size_t i = 0; i < queueFamilyProperties.size(); i++ ) - { - if ( ( queueFamilyProperties[i].queueFlags & vk::QueueFlagBits::eGraphics ) && - physicalDevice.getSurfaceSupportKHR( static_cast( i ), *surface ) ) - { - graphicsIndex = static_cast( i ); - presentIndex = graphicsIndex; - break; - } - } - if ( presentIndex == queueFamilyProperties.size() ) + if ((queueFamilyProperties[qfpIndex].queueFlags & vk::QueueFlagBits::eGraphics) && + physicalDevice.getSurfaceSupportKHR(qfpIndex, *surface)) { - // there's nothing like a single family index that supports both graphics and present -> look for another - // family index that supports present - for ( size_t i = 0; i < queueFamilyProperties.size(); i++ ) - { - if ( physicalDevice.getSurfaceSupportKHR( static_cast( i ), *surface ) ) - { - presentIndex = static_cast( i ); - break; - } - } + // found a queue family that supports both graphics and present + queueIndex = qfpIndex; + break; } } - if ( ( graphicsIndex == queueFamilyProperties.size() ) || ( presentIndex == queueFamilyProperties.size() ) ) + if (queueIndex == ~0) { - throw std::runtime_error( "Could not find a queue for graphics or present -> terminating" ); + throw std::runtime_error("Could not find a queue for graphics and present -> terminating"); } // query for Vulkan 1.3 features @@ -581,7 +549,7 @@ class VulkanApplication { features.pNext = &vulkan13Features; // create a Device float queuePriority = 0.0f; - vk::DeviceQueueCreateInfo deviceQueueCreateInfo { .queueFamilyIndex = graphicsIndex, .queueCount = 1, .pQueuePriorities = &queuePriority }; + vk::DeviceQueueCreateInfo deviceQueueCreateInfo { .queueFamilyIndex = queueIndex, .queueCount = 1, .pQueuePriorities = &queuePriority }; vk::DeviceCreateInfo deviceCreateInfo{ .pNext = &features, .queueCreateInfoCount = 1, @@ -593,8 +561,7 @@ class VulkanApplication { // Create the device with the appropriate features device = vk::raii::Device(physicalDevice, deviceCreateInfo); - graphicsQueue = vk::raii::Queue(device, graphicsIndex, 0); - presentQueue = vk::raii::Queue(device, presentIndex, 0); + queue = vk::raii::Queue(device, queueIndex, 0); } void createSwapChain() { @@ -725,7 +692,7 @@ class VulkanApplication { void createCommandPool() { vk::CommandPoolCreateInfo poolInfo{ .flags = vk::CommandPoolCreateFlagBits::eResetCommandBuffer, - .queueFamilyIndex = graphicsIndex + .queueFamilyIndex = queueIndex }; commandPool = vk::raii::CommandPool(device, poolInfo); } @@ -1169,8 +1136,8 @@ class VulkanApplication { commandBuffer.end(); vk::SubmitInfo submitInfo{ .commandBufferCount = 1, .pCommandBuffers = &*commandBuffer }; - graphicsQueue.submit(submitInfo, nullptr); - graphicsQueue.waitIdle(); + queue.submit(submitInfo, nullptr); + queue.waitIdle(); } void copyBuffer(vk::raii::Buffer & srcBuffer, vk::raii::Buffer & dstBuffer, vk::DeviceSize size) { @@ -1179,8 +1146,8 @@ class VulkanApplication { commandCopyBuffer.begin(vk::CommandBufferBeginInfo{ .flags = vk::CommandBufferUsageFlagBits::eOneTimeSubmit }); commandCopyBuffer.copyBuffer(*srcBuffer, *dstBuffer, vk::BufferCopy{ .size = size }); commandCopyBuffer.end(); - graphicsQueue.submit(vk::SubmitInfo{ .commandBufferCount = 1, .pCommandBuffers = &*commandCopyBuffer }, nullptr); - graphicsQueue.waitIdle(); + queue.submit(vk::SubmitInfo{ .commandBufferCount = 1, .pCommandBuffers = &*commandCopyBuffer }, nullptr); + queue.waitIdle(); } uint32_t findMemoryType(uint32_t typeFilter, vk::MemoryPropertyFlags properties) { @@ -1338,12 +1305,12 @@ class VulkanApplication { const vk::SubmitInfo submitInfo{ .waitSemaphoreCount = 1, .pWaitSemaphores = &*presentCompleteSemaphore[currentFrame], .pWaitDstStageMask = &waitDestinationStageMask, .commandBufferCount = 1, .pCommandBuffers = &*commandBuffers[currentFrame], .signalSemaphoreCount = 1, .pSignalSemaphores = &*renderFinishedSemaphore[imageIndex] }; - graphicsQueue.submit(submitInfo, *inFlightFences[currentFrame]); + queue.submit(submitInfo, *inFlightFences[currentFrame]); const vk::PresentInfoKHR presentInfoKHR{ .waitSemaphoreCount = 1, .pWaitSemaphores = &*renderFinishedSemaphore[imageIndex], .swapchainCount = 1, .pSwapchains = &*swapChain, .pImageIndices = &imageIndex }; - result = presentQueue.presentKHR(presentInfoKHR); + result = queue.presentKHR(presentInfoKHR); if (result == vk::Result::eErrorOutOfDateKHR || result == vk::Result::eSuboptimalKHR || framebufferResized) { framebufferResized = false; recreateSwapChain(); diff --git a/attachments/36_multiple_objects.cpp b/attachments/36_multiple_objects.cpp index 2260c1ce..5579f13f 100644 --- a/attachments/36_multiple_objects.cpp +++ b/attachments/36_multiple_objects.cpp @@ -287,16 +287,14 @@ class VulkanApplication { #endif AppInfo appInfo = {}; - vk::raii::Context context; - vk::raii::Instance instance = nullptr; + vk::raii::Context context; + vk::raii::Instance instance = nullptr; vk::raii::DebugUtilsMessengerEXT debugMessenger = nullptr; - vk::raii::SurfaceKHR surface = nullptr; - - vk::raii::PhysicalDevice physicalDevice = nullptr; - vk::raii::Device device = nullptr; - - vk::raii::Queue graphicsQueue = nullptr; - vk::raii::Queue presentQueue = nullptr; + vk::raii::SurfaceKHR surface = nullptr; + vk::raii::PhysicalDevice physicalDevice = nullptr; + vk::raii::Device device = nullptr; + uint32_t queueIndex = ~0; + vk::raii::Queue queue = nullptr; vk::raii::SwapchainKHR swapChain = nullptr; std::vector swapChainImages; @@ -332,7 +330,6 @@ class VulkanApplication { vk::raii::CommandPool commandPool = nullptr; std::vector commandBuffers; - uint32_t graphicsIndex = 0; std::vector presentCompleteSemaphore; std::vector renderFinishedSemaphore; @@ -587,51 +584,22 @@ class VulkanApplication { } void createLogicalDevice() { - // find the index of the first queue family that supports graphics std::vector queueFamilyProperties = physicalDevice.getQueueFamilyProperties(); - // get the first index into queueFamilyProperties which supports graphics - auto graphicsQueueFamilyProperty = std::ranges::find_if( queueFamilyProperties, []( auto const & qfp ) - { return (qfp.queueFlags & vk::QueueFlagBits::eGraphics) != static_cast(0); } ); - - graphicsIndex = static_cast( std::distance( queueFamilyProperties.begin(), graphicsQueueFamilyProperty ) ); - - // determine a queueFamilyIndex that supports present - // first check if the graphicsIndex is good enough - auto presentIndex = physicalDevice.getSurfaceSupportKHR( graphicsIndex, *surface ) - ? graphicsIndex - : ~0; - if ( presentIndex == queueFamilyProperties.size() ) + // get the first index into queueFamilyProperties which supports both graphics and present + for (uint32_t qfpIndex = 0; qfpIndex < queueFamilyProperties.size(); qfpIndex++) { - // the graphicsIndex doesn't support present -> look for another family index that supports both - // graphics and present - for ( size_t i = 0; i < queueFamilyProperties.size(); i++ ) - { - if ( ( queueFamilyProperties[i].queueFlags & vk::QueueFlagBits::eGraphics ) && - physicalDevice.getSurfaceSupportKHR( static_cast( i ), *surface ) ) - { - graphicsIndex = static_cast( i ); - presentIndex = graphicsIndex; - break; - } - } - if ( presentIndex == queueFamilyProperties.size() ) + if ((queueFamilyProperties[qfpIndex].queueFlags & vk::QueueFlagBits::eGraphics) && + physicalDevice.getSurfaceSupportKHR(qfpIndex, *surface)) { - // there's nothing like a single family index that supports both graphics and present -> look for another - // family index that supports present - for ( size_t i = 0; i < queueFamilyProperties.size(); i++ ) - { - if ( physicalDevice.getSurfaceSupportKHR( static_cast( i ), *surface ) ) - { - presentIndex = static_cast( i ); - break; - } - } + // found a queue family that supports both graphics and present + queueIndex = qfpIndex; + break; } } - if ( ( graphicsIndex == queueFamilyProperties.size() ) || ( presentIndex == queueFamilyProperties.size() ) ) + if (queueIndex == ~0) { - throw std::runtime_error( "Could not find a queue for graphics or present -> terminating" ); + throw std::runtime_error("Could not find a queue for graphics and present -> terminating"); } // query for Vulkan 1.3 features @@ -645,7 +613,7 @@ class VulkanApplication { features.pNext = &vulkan13Features; // create a Device float queuePriority = 0.0f; - vk::DeviceQueueCreateInfo deviceQueueCreateInfo { .queueFamilyIndex = graphicsIndex, .queueCount = 1, .pQueuePriorities = &queuePriority }; + vk::DeviceQueueCreateInfo deviceQueueCreateInfo { .queueFamilyIndex = queueIndex, .queueCount = 1, .pQueuePriorities = &queuePriority }; vk::DeviceCreateInfo deviceCreateInfo{ .pNext = &features, .queueCreateInfoCount = 1, @@ -657,8 +625,7 @@ class VulkanApplication { // Create the device with the appropriate features device = vk::raii::Device(physicalDevice, deviceCreateInfo); - graphicsQueue = vk::raii::Queue(device, graphicsIndex, 0); - presentQueue = vk::raii::Queue(device, presentIndex, 0); + queue = vk::raii::Queue(device, queueIndex, 0); } void createSwapChain() { @@ -789,7 +756,7 @@ class VulkanApplication { void createCommandPool() { vk::CommandPoolCreateInfo poolInfo{ .flags = vk::CommandPoolCreateFlagBits::eResetCommandBuffer, - .queueFamilyIndex = graphicsIndex + .queueFamilyIndex = queueIndex }; commandPool = vk::raii::CommandPool(device, poolInfo); } @@ -1261,8 +1228,8 @@ class VulkanApplication { commandBuffer.end(); vk::SubmitInfo submitInfo{ .commandBufferCount = 1, .pCommandBuffers = &*commandBuffer }; - graphicsQueue.submit(submitInfo, nullptr); - graphicsQueue.waitIdle(); + queue.submit(submitInfo, nullptr); + queue.waitIdle(); } void copyBuffer(vk::raii::Buffer & srcBuffer, vk::raii::Buffer & dstBuffer, vk::DeviceSize size) { @@ -1271,8 +1238,8 @@ class VulkanApplication { commandCopyBuffer.begin(vk::CommandBufferBeginInfo{ .flags = vk::CommandBufferUsageFlagBits::eOneTimeSubmit }); commandCopyBuffer.copyBuffer(*srcBuffer, *dstBuffer, vk::BufferCopy{ .size = size }); commandCopyBuffer.end(); - graphicsQueue.submit(vk::SubmitInfo{ .commandBufferCount = 1, .pCommandBuffers = &*commandCopyBuffer }, nullptr); - graphicsQueue.waitIdle(); + queue.submit(vk::SubmitInfo{ .commandBufferCount = 1, .pCommandBuffers = &*commandCopyBuffer }, nullptr); + queue.waitIdle(); } uint32_t findMemoryType(uint32_t typeFilter, vk::MemoryPropertyFlags properties) { @@ -1467,7 +1434,7 @@ class VulkanApplication { .signalSemaphoreCount = 1, .pSignalSemaphores = &*renderFinishedSemaphore[imageIndex] }; - graphicsQueue.submit(submitInfo, *inFlightFences[currentFrame]); + queue.submit(submitInfo, *inFlightFences[currentFrame]); const vk::PresentInfoKHR presentInfoKHR{ .waitSemaphoreCount = 1, @@ -1476,7 +1443,7 @@ class VulkanApplication { .pSwapchains = &*swapChain, .pImageIndices = &imageIndex }; - result = presentQueue.presentKHR(presentInfoKHR); + result = queue.presentKHR(presentInfoKHR); if (result == vk::Result::eErrorOutOfDateKHR || result == vk::Result::eSuboptimalKHR || framebufferResized) { framebufferResized = false; recreateSwapChain(); diff --git a/attachments/37_multithreading.cpp b/attachments/37_multithreading.cpp index db4dc7ff..3df4aaaf 100644 --- a/attachments/37_multithreading.cpp +++ b/attachments/37_multithreading.cpp @@ -146,18 +146,14 @@ class MultithreadedApplication { } private: - GLFWwindow* window = nullptr; - - vk::raii::Context context; - vk::raii::Instance instance = nullptr; - vk::raii::SurfaceKHR surface = nullptr; - + GLFWwindow * window = nullptr; + vk::raii::Context context; + vk::raii::Instance instance = nullptr; + vk::raii::SurfaceKHR surface = nullptr; vk::raii::PhysicalDevice physicalDevice = nullptr; - vk::raii::Device device = nullptr; - - vk::raii::Queue graphicsQueue = nullptr; - vk::raii::Queue computeQueue = nullptr; - vk::raii::Queue presentQueue = nullptr; + vk::raii::Device device = nullptr; + uint32_t queueIndex = ~0; + vk::raii::Queue queue = nullptr; vk::raii::SwapchainKHR swapChain = nullptr; std::vector swapChainImages; @@ -184,7 +180,6 @@ class MultithreadedApplication { vk::raii::CommandPool commandPool = nullptr; std::vector graphicsCommandBuffers; - uint32_t graphicsQueueFamilyIndex = 0; vk::raii::Semaphore timelineSemaphore = nullptr; uint64_t timelineValue = 0; @@ -461,7 +456,7 @@ class MultithreadedApplication { } void initThreadResources() { - resourceManager.createThreadCommandPools(device, graphicsQueueFamilyIndex, threadCount); + resourceManager.createThreadCommandPools(device, queueIndex, threadCount); resourceManager.allocateCommandBuffers(device, threadCount, 1); } @@ -539,41 +534,21 @@ class MultithreadedApplication { void createLogicalDevice() { std::vector queueFamilyProperties = physicalDevice.getQueueFamilyProperties(); - auto graphicsAndComputeQueueFamilyProperty = std::ranges::find_if(queueFamilyProperties, [](auto const & qfp) - { return (qfp.queueFlags & vk::QueueFlagBits::eGraphics && qfp.queueFlags & vk::QueueFlagBits::eCompute); }); - - graphicsQueueFamilyIndex = static_cast(std::distance(queueFamilyProperties.begin(), graphicsAndComputeQueueFamilyProperty)); - - auto presentIndex = physicalDevice.getSurfaceSupportKHR(graphicsQueueFamilyIndex, surface) - ? graphicsQueueFamilyIndex - : ~0; - if (presentIndex == queueFamilyProperties.size()) + // get the first index into queueFamilyProperties which supports both graphics and present + for (uint32_t qfpIndex = 0; qfpIndex < queueFamilyProperties.size(); qfpIndex++) { - for (size_t i = 0; i < queueFamilyProperties.size(); i++) + if ((queueFamilyProperties[qfpIndex].queueFlags & vk::QueueFlagBits::eGraphics) && + (queueFamilyProperties[qfpIndex].queueFlags & vk::QueueFlagBits::eCompute) && + physicalDevice.getSurfaceSupportKHR(qfpIndex, *surface)) { - if (((queueFamilyProperties[i].queueFlags & vk::QueueFlagBits::eGraphics && queueFamilyProperties[i].queueFlags & vk::QueueFlagBits::eCompute)) && - physicalDevice.getSurfaceSupportKHR(static_cast(i), surface)) - { - graphicsQueueFamilyIndex = static_cast(i); - presentIndex = graphicsQueueFamilyIndex; - break; - } - } - if (presentIndex == queueFamilyProperties.size()) - { - for (size_t i = 0; i < queueFamilyProperties.size(); i++) - { - if (physicalDevice.getSurfaceSupportKHR(static_cast(i), surface)) - { - presentIndex = static_cast(i); - break; - } - } + // found a queue family that supports both graphics and present + queueIndex = qfpIndex; + break; } } - if ((graphicsQueueFamilyIndex == queueFamilyProperties.size()) || (presentIndex == queueFamilyProperties.size())) + if (queueIndex == ~0) { - throw std::runtime_error("Could not find a queue for graphics or present -> terminating"); + throw std::runtime_error("Could not find a queue for graphics and present -> terminating"); } auto features = physicalDevice.getFeatures2(); @@ -590,7 +565,7 @@ class MultithreadedApplication { features.pNext = &vulkan13Features; float queuePriority = 0.0f; - vk::DeviceQueueCreateInfo deviceQueueCreateInfo{.queueFamilyIndex = graphicsQueueFamilyIndex, .queueCount = 1, .pQueuePriorities = &queuePriority}; + vk::DeviceQueueCreateInfo deviceQueueCreateInfo{.queueFamilyIndex = queueIndex, .queueCount = 1, .pQueuePriorities = &queuePriority}; vk::DeviceCreateInfo deviceCreateInfo{ .pNext = &features, .queueCreateInfoCount = 1, @@ -600,9 +575,7 @@ class MultithreadedApplication { }; device = vk::raii::Device(physicalDevice, deviceCreateInfo); - graphicsQueue = vk::raii::Queue(device, graphicsQueueFamilyIndex, 0); - computeQueue = vk::raii::Queue(device, graphicsQueueFamilyIndex, 0); - presentQueue = vk::raii::Queue(device, presentIndex, 0); + queue = vk::raii::Queue(device, queueIndex, 0); } void createSwapChain() { @@ -744,7 +717,7 @@ class MultithreadedApplication { void createCommandPool() { vk::CommandPoolCreateInfo poolInfo{}; poolInfo.flags = vk::CommandPoolCreateFlagBits::eResetCommandBuffer; - poolInfo.queueFamilyIndex = graphicsQueueFamilyIndex; + poolInfo.queueFamilyIndex = queueIndex; commandPool = vk::raii::CommandPool(device, poolInfo); } @@ -881,8 +854,8 @@ class MultithreadedApplication { vk::SubmitInfo submitInfo{}; submitInfo.commandBufferCount = 1; submitInfo.pCommandBuffers = &*commandBuffer; - graphicsQueue.submit(submitInfo, nullptr); - graphicsQueue.waitIdle(); + queue.submit(submitInfo, nullptr); + queue.waitIdle(); } void copyBuffer(const vk::raii::Buffer & srcBuffer, const vk::raii::Buffer & dstBuffer, vk::DeviceSize size) const { @@ -1161,7 +1134,7 @@ class MultithreadedApplication { // Submit compute work { std::lock_guard lock(queueSubmitMutex); - computeQueue.submit(computeSubmitInfo, nullptr); + queue.submit(computeSubmitInfo, nullptr); } // Set up graphics submission @@ -1191,7 +1164,7 @@ class MultithreadedApplication { // Submit graphics work { std::lock_guard lock(queueSubmitMutex); - graphicsQueue.submit(graphicsSubmitInfo, *inFlightFences[currentFrame]); + queue.submit(graphicsSubmitInfo, *inFlightFences[currentFrame]); } // Wait for graphics to complete before presenting @@ -1216,7 +1189,7 @@ class MultithreadedApplication { .pImageIndices = &imageIndex }; - result = presentQueue.presentKHR(presentInfo); + result = queue.presentKHR(presentInfo); // Move to the next frame currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT;