From a48f433adf9c1972d4c3d597eee422925278b1af Mon Sep 17 00:00:00 2001
From: Sebastian Gaida <gaida@ca-digit.com>
Date: Thu, 13 May 2021 11:25:53 +0200
Subject: [PATCH] [#16] added ImageViews and temp fixed exception

added functionality to ImageViews
temporarily fixed the access violation to be able to compile
---
 include/vkcv/Core.hpp      |  7 +++---
 include/vkcv/SwapChain.hpp | 12 +++++++---
 src/vkcv/Context.cpp       |  2 ++
 src/vkcv/Core.cpp          | 47 +++++++++++++++++++++++++++++++++++---
 src/vkcv/SwapChain.cpp     | 19 +++++++++++----
 5 files changed, 73 insertions(+), 14 deletions(-)

diff --git a/include/vkcv/Core.hpp b/include/vkcv/Core.hpp
index e78586d0..d840e0ec 100644
--- a/include/vkcv/Core.hpp
+++ b/include/vkcv/Core.hpp
@@ -26,19 +26,20 @@ namespace vkcv
          *
          * @param context encapsulates various Vulkan objects
          */
-        Core(Context &&context, const Window &window, SwapChain &swapChain) noexcept;
+        Core(Context &&context, const Window &window, SwapChain swapChain,  std::vector<vk::ImageView> imageViews) noexcept;
         // explicit destruction of default constructor
         Core() = delete;
 
         Context m_Context;
-        SwapChain& m_swapchain;
+        SwapChain m_swapchain;
+        std::vector<vk::ImageView> m_swapchainImageViews;
         const Window& m_window;
 
     public:
         /**
          * Destructor of #Core destroys the Vulkan objects contained in the core's context.
          */
-        ~Core() noexcept = default;
+        ~Core();
 
         /**
          * Copy-constructor of #Core is deleted!
diff --git a/include/vkcv/SwapChain.hpp b/include/vkcv/SwapChain.hpp
index 42e408b9..5dad2d95 100644
--- a/include/vkcv/SwapChain.hpp
+++ b/include/vkcv/SwapChain.hpp
@@ -15,13 +15,14 @@ namespace vkcv {
         vk::SurfaceKHR m_surface;
         const vkcv::Context& m_context;
 		vk::SwapchainKHR m_swapchain;
+		vk::SurfaceFormatKHR m_format;
 		
-        SwapChain(vk::SurfaceKHR surface, const vkcv::Context &context, vk::SwapchainKHR swapchain);
+        SwapChain(vk::SurfaceKHR surface, const vkcv::Context &context, vk::SwapchainKHR swapchain, vk::SurfaceFormatKHR format);
 
     public:
         // bin mir grade unsicher wegen der Mehrfachinstanziierung der Klasse
         // es muessen ja oefter mal neue erstellt werden, aber diese existieren ja nicht gleichzeitig, oder?
-        SwapChain(const SwapChain &other) = delete;
+        SwapChain(const SwapChain &other) = default;
         SwapChain(SwapChain &&other) = default;
 
         /**
@@ -31,8 +32,13 @@ namespace vkcv {
         [[nodiscard]]
         vk::SwapchainKHR getSwapchain();
 
+        [[nodiscard]]
+        vk::SurfaceKHR getSurface();
+
+        [[nodiscard]]
+        vk::SurfaceFormatKHR getSurfaceFormat();
+
         static SwapChain create(const Window &window, const Context &context);
-       
         virtual ~SwapChain();
     };
 
diff --git a/src/vkcv/Context.cpp b/src/vkcv/Context.cpp
index d5261285..816fded8 100644
--- a/src/vkcv/Context.cpp
+++ b/src/vkcv/Context.cpp
@@ -1,3 +1,4 @@
+#include <iostream>
 #include "vkcv/Context.hpp"
 
 namespace vkcv
@@ -35,6 +36,7 @@ namespace vkcv
 
     Context::~Context() noexcept
     {
+        std::cout<< " Context " << std::endl;
         m_Device.destroy();
         m_Instance.destroy();
     }
diff --git a/src/vkcv/Core.cpp b/src/vkcv/Core.cpp
index b36e2d94..c60b9337 100644
--- a/src/vkcv/Core.cpp
+++ b/src/vkcv/Core.cpp
@@ -308,7 +308,36 @@ namespace vkcv
         Context context(instance, physicalDevice, device);
 
         SwapChain swapChain = SwapChain::create(window, context);
-        return Core(std::move(context) , window, swapChain);
+
+
+        std::vector<vk::Image> swapChainImages = device.getSwapchainImagesKHR(swapChain.getSwapchain());
+        std::vector<vk::ImageView> imageViews;
+        imageViews.reserve( swapChainImages.size() );
+        //here can be swizzled with vk::ComponentSwizzle if needed
+        // ToDo: we need the format from the surface object
+        vk::ComponentMapping componentMapping(
+                vk::ComponentSwizzle::eR,
+                vk::ComponentSwizzle::eG,
+                vk::ComponentSwizzle::eB,
+                vk::ComponentSwizzle::eA );
+
+        vk::ImageSubresourceRange subResourceRange( vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1 );
+
+        for ( auto image : swapChainImages )
+        {
+            vk::ImageViewCreateInfo imageViewCreateInfo(
+                    vk::ImageViewCreateFlags(),
+                    image,
+                    vk::ImageViewType::e2D,
+                    swapChain.getSurfaceFormat().format,
+                    componentMapping,
+                    subResourceRange
+            );
+
+            imageViews.push_back( device.createImageView( imageViewCreateInfo ) );
+        }
+
+        return Core(std::move(context) , window, swapChain, imageViews);
     }
 
     const Context &Core::getContext() const
@@ -316,9 +345,21 @@ namespace vkcv
         return m_Context;
     }
 
-    Core::Core(Context &&context, const Window &window , SwapChain &swapChain) noexcept :
+    Core::Core(Context &&context, const Window &window , SwapChain swapChain,  std::vector<vk::ImageView> imageViews) noexcept :
             m_Context(std::move(context)),
             m_window(window),
-            m_swapchain(swapChain)
+            m_swapchain(swapChain),
+            m_swapchainImageViews(imageViews)
     {}
+
+    Core::~Core() {
+        std::cout<< " Core " << std::endl;
+
+        for( auto image: m_swapchainImageViews ){
+            m_Context.getDevice().destroyImageView(image);
+        }
+
+        m_Context.getDevice().destroySwapchainKHR(m_swapchain.getSwapchain());
+        m_Context.getInstance().destroySurfaceKHR( m_swapchain.getSurface() );
+    }
 }
diff --git a/src/vkcv/SwapChain.cpp b/src/vkcv/SwapChain.cpp
index 6bb3d229..3a3209e7 100644
--- a/src/vkcv/SwapChain.cpp
+++ b/src/vkcv/SwapChain.cpp
@@ -3,14 +3,22 @@
 
 namespace vkcv {
 
-    SwapChain::SwapChain(vk::SurfaceKHR surface, const vkcv::Context &context, vk::SwapchainKHR swapchain)
-        : m_surface(surface), m_context(context), m_swapchain(swapchain)
+    SwapChain::SwapChain(vk::SurfaceKHR surface, const vkcv::Context &context, vk::SwapchainKHR swapchain, vk::SurfaceFormatKHR format )
+        : m_surface(surface), m_context(context), m_swapchain(swapchain), m_format( format)
     {}
 
     vk::SwapchainKHR SwapChain::getSwapchain() {
         return m_swapchain;
     }
 
+    vk::SurfaceKHR SwapChain::getSurface() {
+        return m_surface;
+    }
+
+    vk::SurfaceFormatKHR SwapChain::getSurfaceFormat(){
+        return m_format;
+    }
+
     vk::SurfaceKHR createSurface(GLFWwindow *window, const vk::Instance &instance, const vk::PhysicalDevice& physicalDevice) {
         //create surface
         VkSurfaceKHR surface;
@@ -132,13 +140,14 @@ namespace vkcv {
 
         vk::SwapchainKHR swapchain = device.createSwapchainKHR(swapchainCreateInfo);
 
-        return SwapChain(surface, context, swapchain);
+        return SwapChain(surface, context, swapchain, surfaceFormat);
     }
 
 
     SwapChain::~SwapChain() {
-        m_context.getDevice().destroySwapchainKHR( m_swapchain );
-        m_context.getInstance().destroySurfaceKHR( m_surface );
+        std::cout<< " Swap " << std::endl;
+//        m_context.getDevice().destroySwapchainKHR( m_swapchain );
+//        m_context.getInstance().destroySurfaceKHR( m_surface );
     }
 
 }
-- 
GitLab