From 80008db55da679eb7bf31489d05ed917cc2122a5 Mon Sep 17 00:00:00 2001
From: Sebastian Gaida <sebastian-gaida@gmx.de>
Date: Fri, 3 Sep 2021 18:41:25 +0200
Subject: [PATCH] [#89] add presentSupportCheck for Surface and use it

---
 include/vkcv/QueueManager.hpp        |  8 ++++++++
 projects/first_triangle/src/main.cpp |  2 +-
 src/vkcv/Core.cpp                    |  2 +-
 src/vkcv/QueueManager.cpp            | 19 +++++++++++++++++++
 src/vkcv/Swapchain.cpp               |  9 +++++----
 5 files changed, 34 insertions(+), 6 deletions(-)

diff --git a/include/vkcv/QueueManager.hpp b/include/vkcv/QueueManager.hpp
index 0919d20d..9d219c9d 100644
--- a/include/vkcv/QueueManager.hpp
+++ b/include/vkcv/QueueManager.hpp
@@ -39,6 +39,14 @@ namespace vkcv {
                 std::vector<std::pair<int, int>> &queuePairsCompute,
                 std::vector<std::pair<int, int>> &queuePairsTransfer);
 
+		/**
+		 * checks for surface support in the queues
+		 * @param physicalDevice to get the Queues
+		 * @param surface that needs to checked
+		 * @return
+		 */
+		static uint32_t checkSurfaceSupport(const vk::PhysicalDevice &physicalDevice, vk::SurfaceKHR &surface);
+
     private:
         std::vector<Queue> m_graphicsQueues;
         std::vector<Queue> m_computeQueues;
diff --git a/projects/first_triangle/src/main.cpp b/projects/first_triangle/src/main.cpp
index 78ec4afe..cad1a54a 100644
--- a/projects/first_triangle/src/main.cpp
+++ b/projects/first_triangle/src/main.cpp
@@ -18,7 +18,7 @@ int main(int argc, const char** argv) {
 		{ VK_KHR_SWAPCHAIN_EXTENSION_NAME }
 	);
 
-	vkcv::WindowHandle windowHandle = core.createWindow(applicationName, windowWidth, windowHeight, false);
+	vkcv::WindowHandle windowHandle = core.createWindow(applicationName, windowWidth, windowHeight, true);
 	vkcv::Window& window = core.getWindow(windowHandle);
 
 	auto triangleIndexBuffer = core.createBuffer<uint16_t>(vkcv::BufferType::INDEX, 3, vkcv::BufferMemoryType::DEVICE_LOCAL);
diff --git a/src/vkcv/Core.cpp b/src/vkcv/Core.cpp
index a4383acc..ff075c03 100644
--- a/src/vkcv/Core.cpp
+++ b/src/vkcv/Core.cpp
@@ -545,7 +545,7 @@ namespace vkcv
 		vk::Result result;
 		
 		try {
-			result = queueManager.getPresentQueue().handle.presentKHR(presentInfo);
+			result = m_Context.getDevice().getQueue(m_SwapchainManager->getSwapchain(swapchainHandle).getPresentQueueIndex(),0).presentKHR(presentInfo);
 		} catch (const vk::OutOfDateKHRError& e) {
 			result = vk::Result::eErrorOutOfDateKHR;
 		} catch (const vk::DeviceLostError& e) {
diff --git a/src/vkcv/QueueManager.cpp b/src/vkcv/QueueManager.cpp
index 3b50c4b2..6f118595 100644
--- a/src/vkcv/QueueManager.cpp
+++ b/src/vkcv/QueueManager.cpp
@@ -5,6 +5,7 @@
 
 #include "vkcv/QueueManager.hpp"
 #include "vkcv/Logger.hpp"
+#include "vkcv/Swapchain.hpp"
 
 namespace vkcv {
 
@@ -199,6 +200,24 @@ namespace vkcv {
     	return QueueManager( std::move(graphicsQueues), std::move(computeQueues), std::move(transferQueues), 0);
 	}
 
+	uint32_t QueueManager::checkSurfaceSupport(
+			const vk::PhysicalDevice &physicalDevice,
+			vk::SurfaceKHR &surface
+	) {
+		std::vector<vk::QueueFamilyProperties> qFamilyProperties = physicalDevice.getQueueFamilyProperties();
+		std::vector<vk::Bool32> presentSupport(qFamilyProperties.size(),-1);
+
+		for(uint32_t i = 0; i < qFamilyProperties.size(); i++)
+		{
+			physicalDevice.getSurfaceSupportKHR(i,surface,&presentSupport[i]);
+			if(presentSupport[i] == VK_TRUE){
+				return i;
+			}
+		}
+		vkcv_log(LogLevel::WARNING, "no supported present queueQ");
+		return 0;
+	}
+
 	QueueManager::QueueManager(std::vector<Queue>&& graphicsQueues, std::vector<Queue>&& computeQueues, std::vector<Queue>&& transferQueues, size_t presentIndex)
 	: m_graphicsQueues(graphicsQueues), m_computeQueues(computeQueues), m_transferQueues(transferQueues), m_presentIndex(presentIndex)
     {}
diff --git a/src/vkcv/Swapchain.cpp b/src/vkcv/Swapchain.cpp
index dc5c73df..90f58926 100644
--- a/src/vkcv/Swapchain.cpp
+++ b/src/vkcv/Swapchain.cpp
@@ -160,10 +160,11 @@ namespace vkcv
         const vk::Device& device = context.getDevice();
 
         Surface surface;
-        surface.handle       = createSurface(window.getWindow(), instance, physicalDevice);
-        surface.formats      = physicalDevice.getSurfaceFormatsKHR(surface.handle);
-        surface.capabilities = physicalDevice.getSurfaceCapabilitiesKHR(surface.handle);
-        surface.presentModes = physicalDevice.getSurfacePresentModesKHR(surface.handle);
+        surface.handle            = createSurface(window.getWindow(), instance, physicalDevice);
+        surface.formats           = physicalDevice.getSurfaceFormatsKHR(surface.handle);
+        surface.capabilities      = physicalDevice.getSurfaceCapabilitiesKHR(surface.handle);
+        surface.presentModes      = physicalDevice.getSurfacePresentModesKHR(surface.handle);
+		surface.presentQueueIndex = QueueManager::checkSurfaceSupport(physicalDevice, surface.handle);
 
         vk::Extent2D chosenExtent = chooseExtent(physicalDevice, surface.handle, window);
         vk::SurfaceFormatKHR chosenSurfaceFormat = chooseSurfaceFormat(physicalDevice, surface.handle);
-- 
GitLab