From e7f8c46fa680b400218c41120649935ddeae6ebf Mon Sep 17 00:00:00 2001
From: Tobias Frisch <tfrisch@uni-koblenz.de>
Date: Wed, 30 Jun 2021 22:54:08 +0200
Subject: [PATCH] [#82] Drop frames with to low size (minimum 2x2 pixel)

Signed-off-by: Tobias Frisch <tfrisch@uni-koblenz.de>
---
 include/vkcv/Window.hpp            |  7 +++++++
 projects/voxelization/src/main.cpp | 16 ++++++++++++++++
 src/vkcv/Core.cpp                  | 25 ++++++++++++++++++-------
 src/vkcv/Swapchain.cpp             |  9 ++++++---
 src/vkcv/Window.cpp                |  5 +++++
 5 files changed, 52 insertions(+), 10 deletions(-)

diff --git a/include/vkcv/Window.hpp b/include/vkcv/Window.hpp
index 7dc6c1b7..f3b3a8fe 100644
--- a/include/vkcv/Window.hpp
+++ b/include/vkcv/Window.hpp
@@ -157,6 +157,13 @@ namespace vkcv {
          * Destructor of #Window, terminates GLFW
          */
         virtual ~Window();
+        
+        /**
+         * gets the windows framebuffer size
+         * @param width
+         * @param height
+         */
+        void getFramebufferSize(int& width, int& height) const;
     };
 
 }
diff --git a/projects/voxelization/src/main.cpp b/projects/voxelization/src/main.cpp
index edc50c55..0270dd91 100644
--- a/projects/voxelization/src/main.cpp
+++ b/projects/voxelization/src/main.cpp
@@ -550,6 +550,14 @@ int main(int argc, const char** argv) {
 		if (!core.beginFrame(swapchainWidth, swapchainHeight)) {
 			continue;
 		}
+		
+		if ((swapchainWidth < 2) || (swapchainHeight < 2)) {
+			std::cerr << "A" << std::endl;
+		}
+		
+		if ((windowWidth < 2) || (windowHeight < 2)) {
+			std::cerr << "B" << std::endl;
+		}
 
 		if ((swapchainWidth != windowWidth) || ((swapchainHeight != windowHeight))) {
 			depthBuffer         = core.createImage(depthBufferFormat, swapchainWidth, swapchainHeight, 1, false, false, false, msaa).getHandle();
@@ -567,6 +575,14 @@ int main(int argc, const char** argv) {
 
 			bloomFlares.updateImageDimensions(windowWidth, windowHeight);
 		}
+		
+		if ((swapchainWidth < 2) || (swapchainHeight < 2)) {
+			std::cerr << "C" << std::endl;
+		}
+		
+		if ((windowWidth < 2) || (windowHeight < 2)) {
+			std::cerr << "D" << std::endl;
+		}
 
 		auto end = std::chrono::system_clock::now();
 		auto deltatime = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
diff --git a/src/vkcv/Core.cpp b/src/vkcv/Core.cpp
index ad89d0fa..df306be5 100644
--- a/src/vkcv/Core.cpp
+++ b/src/vkcv/Core.cpp
@@ -160,8 +160,24 @@ namespace vkcv
 			m_swapchain.updateSwapchain(m_Context, m_window);
 			const auto swapchainViews = createSwapchainImageViews(m_Context, m_swapchain);
 			const auto swapchainImages = m_Context.getDevice().getSwapchainImagesKHR(m_swapchain.getSwapchain());
-
-			m_ImageManager->setSwapchainImages(swapchainImages, swapchainViews, width, height, m_swapchain.getFormat());
+			
+			const auto& extent = m_swapchain.getExtent();
+
+			m_ImageManager->setSwapchainImages(
+					swapchainImages,
+					swapchainViews,
+					extent.width, extent.height,
+					m_swapchain.getFormat()
+			);
+		}
+		
+		const auto& extent = m_swapchain.getExtent();
+		
+		width = extent.width;
+		height = extent.height;
+		
+		if ((width < 2) || (height < 2)) {
+			return false;
 		}
 		
     	if (acquireSwapchainImage() != Result::SUCCESS) {
@@ -172,11 +188,6 @@ namespace vkcv
 		
 		m_Context.getDevice().waitIdle(); // TODO: this is a sin against graphics programming, but its getting late - Alex
 		
-		const auto& extent = m_swapchain.getExtent();
-		
-		width = extent.width;
-		height = extent.height;
-		
 		m_ImageManager->setCurrentSwapchainImageIndex(m_currentSwapchainImageIndex);
 
 		return (m_currentSwapchainImageIndex != std::numeric_limits<uint32_t>::max());
diff --git a/src/vkcv/Swapchain.cpp b/src/vkcv/Swapchain.cpp
index 2c5b3530..4ca8056b 100644
--- a/src/vkcv/Swapchain.cpp
+++ b/src/vkcv/Swapchain.cpp
@@ -78,10 +78,13 @@ namespace vkcv
         if(physicalDevice.getSurfaceCapabilitiesKHR(surface,&surfaceCapabilities) != vk::Result::eSuccess){
             throw std::runtime_error("cannot get surface capabilities. There is an issue with the surface.");
         }
-
+        
+        int fb_width, fb_height;
+        window.getFramebufferSize(fb_width, fb_height);
+        
         VkExtent2D extent2D = {
-                static_cast<uint32_t>(window.getWidth()),
-                static_cast<uint32_t>(window.getHeight())
+                static_cast<uint32_t>(fb_width),
+                static_cast<uint32_t>(fb_height)
         };
         
         extent2D.width = std::max(surfaceCapabilities.minImageExtent.width, std::min(surfaceCapabilities.maxImageExtent.width, extent2D.width));
diff --git a/src/vkcv/Window.cpp b/src/vkcv/Window.cpp
index 03a58a23..ea72582d 100644
--- a/src/vkcv/Window.cpp
+++ b/src/vkcv/Window.cpp
@@ -181,4 +181,9 @@ namespace vkcv {
     GLFWwindow *Window::getWindow() const {
         return m_window;
     }
+    
+    void Window::getFramebufferSize(int &width, int &height) const {
+		glfwGetFramebufferSize(m_window, &width, &height);
+    }
+    
 }
-- 
GitLab