From b0e06cd08dcaef0a2b67cb6ef37131ee9ead9c22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Katharina=20Kr=C3=A4mer?= <kkraemer4@uni-koblenz.de> Date: Thu, 6 May 2021 10:15:19 +0200 Subject: [PATCH] [#16] added SwapChain class and added extension of swapchain to main --- config/Sources.cmake | 2 ++ projects/first_triangle/src/main.cpp | 7 ++++- src/vkcv/SwapChain.cpp | 40 ++++++++++++++++++++++++++++ src/vkcv/SwapChain.hpp | 28 +++++++++++++++++++ 4 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 src/vkcv/SwapChain.cpp create mode 100644 src/vkcv/SwapChain.hpp diff --git a/config/Sources.cmake b/config/Sources.cmake index 9a9f5574..2e84f237 100644 --- a/config/Sources.cmake +++ b/config/Sources.cmake @@ -6,4 +6,6 @@ set(vkcv_sources ${vkcv_source}/vkcv/Window.cpp ${vkcv_source}/vkcv/CoreManager.hpp ${vkcv_source}/vkcv/CoreManager.cpp + ${vkcv_source}/vkcv/SwapChain.hpp + ${vkcv_source}/vkcv/SwapChain.cpp ) diff --git a/projects/first_triangle/src/main.cpp b/projects/first_triangle/src/main.cpp index cc592f46..624d1a40 100644 --- a/projects/first_triangle/src/main.cpp +++ b/projects/first_triangle/src/main.cpp @@ -1,6 +1,7 @@ #include <iostream> #include <vkcv/Context.hpp> #include <vkcv/Window.hpp> +#include <vkcv/SwapChain.hpp> int main(int argc, const char** argv) { const char* applicationName = "First Triangle"; @@ -14,12 +15,16 @@ int main(int argc, const char** argv) { applicationName, VK_MAKE_VERSION(0, 0, 1), 20, - {vk::QueueFlagBits::eGraphics, vk::QueueFlagBits::eTransfer} + {vk::QueueFlagBits::eGraphics, vk::QueueFlagBits::eTransfer}, + {}, + {"VK_KHR_swapchain"} ); + GLFWwindow *glWindow = window.getWindow(); const vk::Instance& instance = context.getInstance(); const vk::PhysicalDevice& physicalDevice = context.getPhysicalDevice(); const vk::Device& device = context.getDevice(); + const vkcv::SwapChain& swapChain = vkcv::SwapChain::create(glWindow, instance, physicalDevice, device); std::cout << "Physical device: " << physicalDevice.getProperties().deviceName << std::endl; diff --git a/src/vkcv/SwapChain.cpp b/src/vkcv/SwapChain.cpp new file mode 100644 index 00000000..ed999fcd --- /dev/null +++ b/src/vkcv/SwapChain.cpp @@ -0,0 +1,40 @@ +#include "SwapChain.hpp" +#include "CoreManager.hpp" +#include <iostream> + +namespace vkcv { + + SwapChain::SwapChain(vk::SurfaceKHR surface) + : m_surface(surface) + {} + + SwapChain SwapChain::create(GLFWwindow* window, const vk::Instance& instance, const vk::PhysicalDevice& physicalDevice, const vk::Device& device){ + vk::SurfaceKHR surface = VK_NULL_HANDLE; + createSurface(window,surface,instance,physicalDevice); + vk::SurfaceCapabilitiesKHR surfaceCapabilities; + if(physicalDevice.getSurfaceCapabilitiesKHR(surface,&surfaceCapabilities) != vk::Result::eSuccess){ + throw std::runtime_error("cannot get surface capabilites. There is an issue with the surface."); + } + + + + + return SwapChain(surface); + + } + + void SwapChain::createSurface(GLFWwindow *window, vk::SurfaceKHR &surface, const vk::Instance &instance, const vk::PhysicalDevice& physicalDevice) { + //create surface + auto newSurface = VkSurfaceKHR(surface); + // 0 means VK_SUCCESS + //std::cout << "FAIL: " << glfwCreateWindowSurface(VkInstance(instance), window, nullptr, &newSurface) << std::endl; + if(glfwCreateWindowSurface(VkInstance(instance), window, nullptr, &newSurface) != VK_SUCCESS) { + throw std::runtime_error("failed to create a window surface!"); + } + vk::Bool32 surfaceSupport = false; + // ToDo: hierfuer brauchen wir jetzt den queuefamiliy Index -> siehe ToDo in Context.cpp + //if(physicalDevice.getSurfaceSupportKHR()) + + } + +} \ No newline at end of file diff --git a/src/vkcv/SwapChain.hpp b/src/vkcv/SwapChain.hpp new file mode 100644 index 00000000..925cc694 --- /dev/null +++ b/src/vkcv/SwapChain.hpp @@ -0,0 +1,28 @@ +#pragma once +#include "vulkan/vulkan.hpp" +#include "Context.hpp" +#include "Window.hpp" + + +// glfw is not initialized in this class because ist must be sure that there exists a context first +// glfw is already initialized by the context or the window class + +namespace vkcv { + + class SwapChain final { + private: + vk::SurfaceKHR m_surface; + + SwapChain(vk::SurfaceKHR); + + 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(SwapChain &&other) = default; + static SwapChain create(GLFWwindow *window, const vk::Instance& instance,const vk::PhysicalDevice& physicalDevice,const vk::Device& device); + static void createSurface(GLFWwindow *window, vk::SurfaceKHR& surface, const vk::Instance& instance, const vk::PhysicalDevice& physicalDevice); + + }; + +} \ No newline at end of file -- GitLab