Files
OpenVulkano/openVulkanoCpp/Vulkan/SwapChain.hpp
2020-10-21 23:16:11 +02:00

127 lines
2.7 KiB
C++

/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#pragma once
#include <vulkan/vulkan.hpp>
#include "Device.hpp"
#include "Image.hpp"
#include "FrameBuffer.hpp"
#include "../Base/UI/IWindow.hpp"
namespace openVulkanoCpp
{
namespace Vulkan
{
struct SwapChainImage : virtual public IImage
{
vk::Image image;
vk::ImageView view;
vk::Fence fence;
vk::Image GetImage() override
{
return image;
}
vk::ImageView GetView() override
{
return view;
}
};
class SwapChain final : public FrameBuffer
{
vk::SurfaceKHR surface;
std::vector<SwapChainImage> images;
Device* device = nullptr;
IVulkanWindow* window = nullptr;
vk::SurfaceFormatKHR surfaceFormat;
vk::PresentModeKHR presentMode;
vk::Viewport fullscreenViewport;
vk::Rect2D fullscreenScissor;
bool useVsync = false;
uint32_t preferredImageCount = 2; //TODO add option
vk::Extent2D size{0,0};
public:
vk::SwapchainKHR swapChain;
vk::Semaphore imageAvailableSemaphore;
SwapChain() = default;
~SwapChain() override { if (device) SwapChain::Close(); }
void Init(Device* device, vk::SurfaceKHR surface, IVulkanWindow* window);
void Close() override;
void Resize(uint32_t newWidth, uint32_t newHeight);
[[nodiscard]] vk::Extent2D GetSize() const
{
return size;
}
[[nodiscard]] const vk::Viewport& GetFullscreenViewport() const
{
return fullscreenViewport;
}
[[nodiscard]] const vk::Rect2D& GetFullscreenScissor() const
{
return fullscreenScissor;
}
uint32_t AcquireNextImage(const vk::Fence& fence = vk::Fence());
vk::Fence& GetCurrentSubmitFence()
{
return images[currentFrameBufferId].fence;
}
void Present(vk::Queue& queue ,std::vector<vk::Semaphore>& semaphores) const
{
queue.presentKHR(vk::PresentInfoKHR(semaphores.size(), semaphores.data(),
1, &swapChain, &currentFrameBufferId));
}
[[nodiscard]] bool UseVsync() const { return useVsync; }
void SetVsync(bool useVsync)
{ //TODO change swap chain
this->useVsync = useVsync;
}
[[nodiscard]] uint32_t GetImageCount() const
{
return images.size();
}
private:
void CreateSwapChain(vk::Extent2D size);
void CreateSwapChainImages();
void DestroySwapChain() const;
protected:
vk::Format FindColorFormat() override
{
return surfaceFormat.format;
}
virtual vk::PresentModeKHR ChosePresentMode();
virtual vk::SurfaceFormatKHR ChoseSurfaceFormat();
public:
std::vector<IImage*> GetImages() override;
};
}
}