HoviTron Video Pipeline
Public Member Functions
SwapchainCommon Class Reference

Class that encapsulate a vkSwapchainHHR object and the methods linked to it. More...

#include <SwapchainCommon.h>

Inheritance diagram for SwapchainCommon:
SwapchainAbstract

Public Member Functions

 SwapchainCommon (VkSurfaceKHR &surface, WindowAbstract *window, VulkanWrapper *wrapper)
 
std::tuple< uint32_t, std::optional< vk::Semaphore > > acquireImage () override
 
void presentImage (uint32_t imageIndex, vk::Semaphore &renderingFinnished) override
 
void presentImage (uint32_t imageIndex, vk::Fence &renderingFinnishedFence) override
 
vk::ImageView getSwapchainImageView (int elem) override
 
vk::Image getSwapchainImage (int index) override
 
void cleanup () override
 
int getAttachmentSize () override
 
vk::Image getCurrentImage () override
 
virtual std::tuple< uint32_t, std::optional< vk::Semaphore > > acquireImage ()=0
 
virtual void presentImage (uint32_t imageIndex, vk::Semaphore &renderingFinnished)=0
 
virtual void presentImage (uint32_t imageIndex, vk::Fence &renderingFinnishedFence)=0
 
virtual void cleanup ()=0
 
virtual vk::ImageView getSwapchainImageView (int elem)=0
 
virtual int getAttachmentSize ()=0
 
virtual vk::Image getSwapchainImage (int index)=0
 
virtual vk::Image getCurrentImage ()=0
 

Additional Inherited Members

- Data Fields inherited from SwapchainAbstract
std::variant< vk::SwapchainKHR > handle
 
vk::Extent2D swapchainExtent
 
vk::Format swapchainFormat
 
- Protected Attributes inherited from SwapchainAbstract
std::vector< vk::ImageView > swapchainImageViews
 
VulkanWrapperwraps = nullptr
 
WindowAbstractwindow = nullptr
 

Detailed Description

Class that encapsulate a vkSwapchainHHR object and the methods linked to it.

Class that encapsulates an OpenXR swapchain and functions associated to it.

Definition at line 36 of file SwapchainCommon.h.

Constructor & Destructor Documentation

◆ SwapchainCommon()

SwapchainCommon::SwapchainCommon ( VkSurfaceKHR &  surface,
WindowAbstract window,
VulkanWrapper wrapper 
)

Constructor

Definition at line 23 of file SwapchainCommon.cpp.

24{
25 assert(window != nullptr);
26 assert(surface != VK_NULL_HANDLE);
27 assert(wrapper != nullptr);
28
29 this->wraps = wrapper;
30 this->window = window;
32
33 vk::SurfaceFormatKHR surfaceFormat = chooseSwapSurfaceFormat(swapChainSupport.formats);
34 vk::PresentModeKHR presentMode = chooseSwapPresentMode(swapChainSupport.presentModes);
35 vk::Extent2D extent = chooseSwapExtent(swapChainSupport.capabilities);
36
37 uint32_t imageCount = swapChainSupport.capabilities.minImageCount + 1;
38 if (swapChainSupport.capabilities.maxImageCount > 0 && imageCount > swapChainSupport.capabilities.maxImageCount) {
39 imageCount = swapChainSupport.capabilities.maxImageCount;
40 }
41
43 uint32_t queueFamilyIndices[] = { indices.graphicsFamily.value(), indices.presentFamily.value() };
44
45 int queueFamilyIndexCount = 0;
46 auto imageSharingMode = vk::SharingMode::eExclusive;
47 if (indices.graphicsFamily != indices.presentFamily) {
48 imageSharingMode = vk::SharingMode::eConcurrent;
49 queueFamilyIndexCount = 2;
50 }
51#ifdef __ANDROID__
52 //composite alpha flag
53 vk::CompositeAlphaFlagBitsKHR compositeAlpha = vk::CompositeAlphaFlagBitsKHR::eInherit;
54#else
55 vk::CompositeAlphaFlagBitsKHR compositeAlpha = vk::CompositeAlphaFlagBitsKHR::eOpaque;
56#endif
57
58 auto preTransform = swapChainSupport.capabilities.currentTransform;
59#ifdef __ANDROID__
60 preTransform = vk::SurfaceTransformFlagBitsKHR::eIdentity;
61#endif
62
63 vk::SwapchainCreateInfoKHR swapChainCreateInfo(
64 vk::SwapchainCreateFlagsKHR(), //flags
65 static_cast<vk::SurfaceKHR>(surface), //vk::surface
66 imageCount, //numberOfImage
67 surfaceFormat.format, //format
68 surfaceFormat.colorSpace, //colorSpace
69 extent, //vk::Extend2D
70 1, //imageArrayLayers
71 vk::ImageUsageFlagBits::eStorage | vk::ImageUsageFlagBits::eColorAttachment | vk::ImageUsageFlagBits::eInputAttachment | vk::ImageUsageFlagBits::eTransferDst, //imageUsage
72 imageSharingMode, //imageSharingMode
73 queueFamilyIndexCount, //Queue family index count
74 queueFamilyIndexCount > 0 ? queueFamilyIndices : nullptr, //Queue family indices
75 preTransform, //preTransform
76 compositeAlpha, //compositeAlpha //TODO check Alpha
77 presentMode, //presentMode
78 true, //clipped
79 nullptr); //oldSwapchain
80
81
82 swapchainFormat = surfaceFormat.format;
83 swapchainExtent = extent;
84
85 //create the swapChain
86 swapChain = wraps->context.device.createSwapchainKHR(swapChainCreateInfo);
87 //create the images for the swapChain
88 swapChainImages = wraps->context.device.getSwapchainImagesKHR(swapChain);
89
90 handle = swapChain;
91
92
93 createSwapchainImageView();
94 createAcquireSemaphores();
95}
vk::Extent2D swapchainExtent
std::variant< vk::SwapchainKHR > handle
vk::Format swapchainFormat
VulkanWrapper * wraps
WindowAbstract * window
vk::PhysicalDevice physicalDevice
Definition: VulkanContext.h:85
vk::Device device
Definition: VulkanContext.h:87
QueueFamilyIndices findQueueFamilies(vk::PhysicalDevice device)
VulkanContext context
virtual SwapChainSupportDetails querySwapChainSupport(vk::PhysicalDevice device)=0
Struct to encapsulate the indice of the queues families.
Definition: VulkanContext.h:38
std::optional< uint32_t > graphicsFamily
Definition: VulkanContext.h:40
std::optional< uint32_t > presentFamily
Definition: VulkanContext.h:42
Struct that contains the capability for the sapchain, the formats and the present mode supported.
Definition: commonVulkan.h:87
vk::SurfaceCapabilitiesKHR capabilities
Definition: commonVulkan.h:89
std::vector< vk::SurfaceFormatKHR > formats
Definition: commonVulkan.h:91
std::vector< vk::PresentModeKHR > presentModes
Definition: commonVulkan.h:93

Member Function Documentation

◆ acquireImage()

std::tuple< uint32_t, std::optional< vk::Semaphore > > SwapchainCommon::acquireImage ( )
overridevirtual

Acquire the next image that will be the rendering target and optionnaly the semaphore signalling when the image is available

Implements SwapchainAbstract.

Definition at line 97 of file SwapchainCommon.cpp.

98{
99 uint32_t imageIndex;
100 vk::Result acquireResult;
101
102 std::tie(acquireResult, imageIndex) = wraps->context.device.acquireNextImageKHR(swapChain, std::numeric_limits<uint64_t>::max(), imageAvailableSemaphores[currentFrame], nullptr);
103 if (acquireResult != vk::Result::eSuccess && acquireResult != vk::Result::eSuboptimalKHR) {
104 throw std::runtime_error("failed to acquire swap chain image!");
105 }
106 std::optional<vk::Semaphore> sem = imageAvailableSemaphores[currentFrame];
107 return std::tuple<uint32_t, std::optional<vk::Semaphore> >(imageIndex, sem );
108}

◆ cleanup()

void SwapchainCommon::cleanup ( )
overridevirtual

cleanUp ressources

Implements SwapchainAbstract.

Definition at line 215 of file SwapchainCommon.cpp.

216{
217 PRINT("test clean");
218 for (auto & imageView : swapchainImageViews) {
219 wraps->context.device.destroyImageView(imageView);
220 }
221 swapchainImageViews.clear();
222
223 for (auto & sem : imageAvailableSemaphores) {
224 wraps->context.device.destroySemaphore(sem);
225 }
226 imageAvailableSemaphores.clear();
227
228 wraps->context.device.destroySwapchainKHR(swapChain);
229
230}
std::vector< vk::ImageView > swapchainImageViews

◆ getAttachmentSize()

int SwapchainCommon::getAttachmentSize ( )
overridevirtual

Return the size of the attachment

Implements SwapchainAbstract.

Definition at line 232 of file SwapchainCommon.cpp.

233{
234 return swapchainImageViews.size();
235}

◆ getCurrentImage()

vk::Image SwapchainCommon::getCurrentImage ( )
overridevirtual

Return the current vk::Image in use

Implements SwapchainAbstract.

Definition at line 237 of file SwapchainCommon.cpp.

238{
239 return swapChainImages[currentFrame];
240}

◆ getSwapchainImage()

vk::Image SwapchainCommon::getSwapchainImage ( int  index)
overridevirtual

Return vk::Image corresponding to the requested index

Implements SwapchainAbstract.

Definition at line 209 of file SwapchainCommon.cpp.

210{
211 assert(index < swapChainImages.size());
212 return swapChainImages[index];
213}

◆ getSwapchainImageView()

vk::ImageView SwapchainCommon::getSwapchainImageView ( int  elem)
overridevirtual

Used to retrieve the vk::ImageView corresponding to the image used

Implements SwapchainAbstract.

Definition at line 203 of file SwapchainCommon.cpp.

204{
205 assert(elem < swapchainImageViews.size());
206 return swapchainImageViews[elem];
207}

◆ presentImage() [1/2]

void SwapchainCommon::presentImage ( uint32_t  imageIndex,
vk::Fence &  renderingFinnishedFence 
)
overridevirtual

Called to signal that rendering work have been submitted to the GPU, and that the fence have to be used for synchronization

Implements SwapchainAbstract.

Definition at line 141 of file SwapchainCommon.cpp.

142{
143 assert(fence != (vk::Fence) VK_NULL_HANDLE);
144 const uint32_t timeoutNs = 1 * 1000 * 1000 * 1000;
145 for (int i = 0; i < 5; ++i) {
146 auto res = wraps->context.device.waitForFences(1, &fence, VK_TRUE, timeoutNs);
147 if (res == vk::Result::eSuccess) {
148 // Buffer can be executed multiple times...
149 break;
150 }
151 PRINT("%s", "Waiting for CmdBuffer fence timed out, retrying...");
152 }
153
154 vk::PresentInfoKHR presentInfo{ 0,
155 nullptr,
156 1,
157 &swapChain,
158 &imageIndex,
159 nullptr };
160
161 try {
162 vk::Result resultPresent = wraps->context.presentQueue.presentKHR(presentInfo);
163 if (resultPresent == vk::Result::eSuboptimalKHR || wraps->framebufferResized) {
164#ifndef __ANDROID__
165 //not doing this for ANDROID for the moment TODO
166 wraps->framebufferResized = false;
168#endif
169 }
170 else if (resultPresent != vk::Result::eSuccess) {
171 throw std::runtime_error("failed to present swap chain image!");
172 }
173 }
174 catch (vk::OutOfDateKHRError) {
176 return;
177 }
178
179
180 currentFrame = (currentFrame + 1) % swapchainImageViews.size();
181}
vk::Queue presentQueue
Definition: VulkanContext.h:91
void recreateSwapChain()
bool framebufferResized

◆ presentImage() [2/2]

void SwapchainCommon::presentImage ( uint32_t  imageIndex,
vk::Semaphore &  renderingFinnished 
)
overridevirtual

Called to signal that rendering work have been submitted to the GPU, and that the Semaphore have to be used for synchronization

Implements SwapchainAbstract.

Definition at line 110 of file SwapchainCommon.cpp.

111{
112 vk::PresentInfoKHR presentInfo{ 1,
113 &renderingFinnished,
114 1,
115 &swapChain,
116 &imageIndex,
117 nullptr };
118
119 try {
120
121 vk::Result resultPresent = wraps->context.presentQueue.presentKHR(presentInfo);
122 if (resultPresent == vk::Result::eSuboptimalKHR || wraps->framebufferResized) {
123#ifndef __ANDROID__
124 //not doing this for ANDROID for the moment TODO
125 wraps->framebufferResized = false;
127#endif
128 }
129 else if (resultPresent != vk::Result::eSuccess) {
130 throw std::runtime_error("failed to present swap chain image!");
131 }
132 }
133 catch (vk::OutOfDateKHRError) {
135 return;
136 }
137
138 currentFrame= (currentFrame + 1) % swapchainImageViews.size();
139}

The documentation for this class was generated from the following files: