evan 2026.1.4
Evan
Loading...
Searching...
No Matches
XrSwapchainImage.cpp
1/*
2** ETIB PROJECT, 2026
3** evan
4** File description:
5** XrSwapchainImage
6*/
7
8#include "evan/openxr/XrSwapchainImage.hpp"
9
12{
13 this->getLogger().info() << "Initializing XrSwapchainImage";
14
17 nullptr);
18
19 _swapchain = properties.swapchain;
20 _device = properties.deviceContext.getDeviceBackend()->getDevice();
21 _height = properties.createInfo.height;
22 _width = properties.createInfo.width;
23
24 _format = static_cast<VkFormat>(properties.createInfo.format);
25 _extent = { properties.createInfo.width, properties.createInfo.height };
26
27 // A multiview swapchain hands out one array layer per view; the image
28 // views, color, depth and framebuffer resources must cover them all.
29 _arrayLayers = properties.createInfo.arraySize > 0
30 ? properties.createInfo.arraySize
31 : 1;
32
33 _swapchainImages.resize(swapchainImageCount,
37
38 _swapchainImagesBase =
39 reinterpret_cast<XrSwapchainImageBaseHeader *>(&_swapchainImages[0]);
40
42 &swapchainImageCount, _swapchainImagesBase);
43
44 for (auto &image: _swapchainImages) {
46 }
47 for (size_t i = 0; i < _swapchainImages.size(); ++i) {
48 _imageViews[i] =
49 properties.deviceContext.getDeviceBackend()
50 ->createImageView(_swapchainImages[i].image, _format,
52 .value;
53 }
54
55 if (properties.colorAttachmentMode
56 == ColorAttachmentMode::ResolveToSwapchain) {
57 this->createColorResources(*properties.deviceContext.getDeviceBackend(),
58 properties.msaaSamples);
59 }
60 this->createDepthResources(properties.deviceContext,
61 properties.msaaSamples);
63 properties.deviceContext.getDeviceBackend()->getDevice(),
64 properties.renderPass, properties.colorAttachmentMode);
65}
66
68// Public Methods //
70
72{
73 this->getLogger().info() << "Destroying XrSwapchainImage...";
74 this->cleanup();
75}
76
78{
79 (void)device;
80 this->cleanup();
81}
82
84// Private Methods //
86
87void evan::XrSwapchainImage::cleanup()
88{
89 this->getLogger().info()
90 << "Destroying XrSwapchainImage and releasing resources";
91
92 if (_device != VK_NULL_HANDLE) {
93 for (size_t i = 0; i < _swapchainImages.size(); ++i) {
94 this->getLogger().info()
95 << "Destroying image view and framebuffer for swapchain "
96 "image index: "
97 << i;
98 if (i < _imageViews.size() && _imageViews[i] != VK_NULL_HANDLE) {
99 vkDestroyImageView(_device, _imageViews[i], nullptr);
100 }
101 if (i < _framebuffers.size()
102 && _framebuffers[i] != VK_NULL_HANDLE) {
103 vkDestroyFramebuffer(_device, _framebuffers[i], nullptr);
104 }
105 }
106 }
107
108 _imageViews.clear();
109 _framebuffers.clear();
110
111 if (_device != VK_NULL_HANDLE) {
112 this->getLogger().info()
113 << "Destroying color and depth resources for swapchain image";
114 if (_colorView != VK_NULL_HANDLE) {
115 vkDestroyImageView(_device, _colorView, nullptr);
116 _colorView = VK_NULL_HANDLE;
117 }
118 if (_colorImage != VK_NULL_HANDLE) {
119 vkDestroyImage(_device, _colorImage, nullptr);
120 _colorImage = VK_NULL_HANDLE;
121 }
122 if (_colorMemory != VK_NULL_HANDLE) {
123 vkFreeMemory(_device, _colorMemory, nullptr);
124 _colorMemory = VK_NULL_HANDLE;
125 }
126 if (_depthView != VK_NULL_HANDLE) {
127 vkDestroyImageView(_device, _depthView, nullptr);
128 _depthView = VK_NULL_HANDLE;
129 }
130 if (_depthImage != VK_NULL_HANDLE) {
131 vkDestroyImage(_device, _depthImage, nullptr);
132 _depthImage = VK_NULL_HANDLE;
133 }
134 if (_depthMemory != VK_NULL_HANDLE) {
135 vkFreeMemory(_device, _depthMemory, nullptr);
136 _depthMemory = VK_NULL_HANDLE;
137 }
138 }
139
140 if (_swapchain != XR_NULL_HANDLE) {
141 xrDestroySwapchain(_swapchain);
142 _swapchain = XR_NULL_HANDLE;
143 }
144}
145
148{
149 presentInfo.swapchainCount = 0;
150 presentInfo.pSwapchains = nullptr;
151}
void createFramebuffers(VkDevice logicalDevice, VkRenderPass renderPass, ColorAttachmentMode mode)
Creates framebuffers for the swapchain images.
void createColorResources(const ADeviceBackend &deviceBackend, VkSampleCountFlagBits msaaSamples)
Creates a multisampled color attachment and its image view.
void createDepthResources(const DeviceContext &deviceContext, VkSampleCountFlagBits msaaSamples)
Creates depth resources for the swapchain images.
uint32_t _arrayLayers
The number of array layers of the swapchain and attachment images.
std::vector< VkImageView > _imageViews
std::vector< VkFramebuffer > _framebuffers
XrSwapchainImage(const CreateXrSwapchainImageProperties &properties)
Constructs an XrSwapchainImage instance using the provided properties.
~XrSwapchainImage() override
Releases the swapchain image resources if they were not already destroyed through destroy().
void destroy(VkDevice device) override
Destroys the swapchain images and releases associated resources.
void fillPresentInfo(VkPresentInfoKHR &presentInfo) const override
Fills the VkPresentInfoKHR structure with the necessary information for presenting the swapchain imag...
Small result wrapper used by helpers that must return both a status and a value (e....
Definition Error.hpp:89
Encapsulates properties for creating an XrSwapchainImage instance.
const DeviceContext & deviceContext
A reference to the device context, which provides access to Vulkan device resources and operations ne...