SwapchainVk.h#

The Swapchain class. The Swapchain is the object wrapping the on-screen rendering Framebuffer images (aka front/backbuffers)

Includes#

  • PVRVk/DeviceVk.h

  • PVRVk/ImageVk.h

Included By#

Namespaces#

Classes#

Source Code#

#pragma once
#include "PVRVk/DeviceVk.h"
#include "PVRVk/ImageVk.h"
namespace pvrvk {
struct SwapchainCreateInfo
{
public:
    SwapchainCreateFlagsKHR flags;
    Surface surface;
    uint32_t minImageCount;
    Format imageFormat;
    ColorSpaceKHR imageColorSpace;
    Extent2D imageExtent;
    uint32_t imageArrayLayers;
    ImageUsageFlags imageUsage;
    SharingMode imageSharingMode;
    uint32_t numQueueFamilyIndex;
    const uint32_t* queueFamilyIndices;
    SurfaceTransformFlagsKHR preTransform;
    CompositeAlphaFlagsKHR compositeAlpha;
    PresentModeKHR presentMode;
    bool clipped;
    Swapchain oldSwapchain;

    SwapchainCreateInfo()
        : flags(SwapchainCreateFlagsKHR(0)), minImageCount(0), imageFormat(Format::e_UNDEFINED), imageColorSpace(ColorSpaceKHR::e_PASS_THROUGH_EXT), imageExtent(Extent2D()),
          imageArrayLayers(0), imageUsage(ImageUsageFlags::e_COLOR_ATTACHMENT_BIT), imageSharingMode(SharingMode::e_EXCLUSIVE), numQueueFamilyIndex(0),
          preTransform(SurfaceTransformFlagsKHR::e_IDENTITY_BIT_KHR), compositeAlpha(CompositeAlphaFlagsKHR::e_OPAQUE_BIT_KHR), presentMode(PresentModeKHR::e_FIFO_KHR), clipped(true)
    {}

    SwapchainCreateInfo(Surface surface, uint32_t minImageCount, Extent2D imageExtent, SwapchainCreateFlagsKHR flags, uint32_t numQueueFamilyIndex,
        const uint32_t* queueFamilyIndices, ImageUsageFlags imageUsage, Format imageFormat = Format::e_R8G8B8A8_UNORM,
        ColorSpaceKHR imageColorSpace = ColorSpaceKHR::e_PASS_THROUGH_EXT, PresentModeKHR presentMode = PresentModeKHR::e_FIFO_KHR, uint32_t imageArrayLayers = 1,
        SharingMode imageSharingMode = SharingMode::e_EXCLUSIVE, SurfaceTransformFlagsKHR preTransform = SurfaceTransformFlagsKHR::e_IDENTITY_BIT_KHR,
        CompositeAlphaFlagsKHR compositeAlpha = CompositeAlphaFlagsKHR::e_OPAQUE_BIT_KHR, bool clipped = true, Swapchain oldSwapchain = Swapchain())
        : flags(flags), surface(surface), minImageCount(minImageCount), imageFormat(imageFormat), imageColorSpace(imageColorSpace), imageExtent(imageExtent),
          imageArrayLayers(imageArrayLayers), imageUsage(imageUsage), imageSharingMode(imageSharingMode), numQueueFamilyIndex(numQueueFamilyIndex),
          queueFamilyIndices(queueFamilyIndices), preTransform(preTransform), compositeAlpha(compositeAlpha), presentMode(presentMode), clipped(clipped), oldSwapchain(oldSwapchain)
    {}
};

namespace impl {
class Swapchain_ : public PVRVkDeviceObjectBase<VkSwapchainKHR, ObjectType::e_SWAPCHAIN_KHR>, public DeviceObjectDebugUtils<Swapchain_>
{
private:
    friend class Device_;

    class make_shared_enabler
    {
    protected:
        make_shared_enabler() = default;
        friend class Swapchain_;
    };

    static Swapchain constructShared(const DeviceWeakPtr& device, Surface surface, const SwapchainCreateInfo& createInfo)
    {
        return std::make_shared<Swapchain_>(make_shared_enabler{}, device, surface, createInfo);
    }

    uint32_t _swapchainId;
    uint32_t _swapChainLength;
    std::vector<ImageView> _colorImageViews;
    Surface _surface;
    SwapchainCreateInfo _createInfo;

public:
    DECLARE_NO_COPY_SEMANTICS(Swapchain_)
    Swapchain_(make_shared_enabler, const DeviceWeakPtr& device, Surface surface, const SwapchainCreateInfo& createInfo);
    ~Swapchain_();

    bool acquireNextImage(uint64_t timeOut, Semaphore signalSemaphore, Fence signalFence);

    bool acquireNextImage(uint64_t timeOut, Semaphore signalSemaphore = Semaphore());

    uint32_t getSwapchainLength() const { return _swapChainLength; }

    const uint32_t& getSwapchainIndex() const { return _swapchainId; }

    const ImageView& getImageView(uint32_t swapchain) const
    {
        return _colorImageViews[swapchain];
    }

    ImageView& getImageView(uint32_t swapchain)
    {
        return _colorImageViews[swapchain];
    }

    const Image& getImage(uint32_t swapchain) const { return getImageView(swapchain)->getImage(); }

    Image& getImage(uint32_t swapchain) { return getImageView(swapchain)->getImage(); }

    Extent2D getDimension() const { return Extent2D(_createInfo.imageExtent.getWidth(), _createInfo.imageExtent.getHeight()); }

    bool isClipped() const { return _createInfo.clipped; }

    CompositeAlphaFlagsKHR getCompositeAlphaFlags() const { return _createInfo.compositeAlpha; }

    uint32_t getNumArrayLayers() const { return _createInfo.imageArrayLayers; }

    Format getImageFormat() const { return _createInfo.imageFormat; }

    ColorSpaceKHR getColorSpace() const { return _createInfo.imageColorSpace; }

    SurfaceTransformFlagsKHR getTransformFlags() const { return _createInfo.preTransform; }

    SharingMode getSharingMode() const { return _createInfo.imageSharingMode; }

    PresentModeKHR getPresentationMode() const { return _createInfo.presentMode; }

    uint32_t getNumQueueFamilyIndices() const { return _createInfo.numQueueFamilyIndex; }

    std::vector<uint32_t> getQueueFamilyIndices() const
    {
        std::vector<uint32_t> indices;
        for (uint32_t i = 0; i < _createInfo.numQueueFamilyIndex; i++) { indices.emplace_back(_createInfo.queueFamilyIndices[i]); }
        return indices;
    }

    ImageUsageFlags getUsage() const { return _createInfo.imageUsage; }

    bool supportsUsage(const ImageUsageFlags& imageUsage) const { return static_cast<uint32_t>(getUsage() & imageUsage) != 0; }
};
} // namespace impl
} // namespace pvrvk