vOOlkan
An object oriented approach to Vulkan
Framebuffer.h
Go to the documentation of this file.
1#ifndef VULKAN_FRAMEBUFFER
2#define VULKAN_FRAMEBUFFER
3
4#include <vulkan/vulkan.h>
5#include <concepts>
6#include <vector>
7#include <utility>
8#include <iostream>
9
10#include "ImageView.h"
11#include "LogicalDevice.h"
12#include "RenderPass.h"
13#include "VulkanException.h"
14#include "Swapchain.h"
15
16
17namespace Vulkan { class Framebuffer; }
18
23public:
24
25 template<std::same_as<ImageView>... IV>
26 Framebuffer(const LogicalDevice& virtualGpu, const PipelineOptions::RenderPass& renderPass, std::pair<unsigned int, unsigned int> resolution, const IV&... imageViews) : virtualGpu{ virtualGpu }, resolution{ resolution } {
27 std::vector<VkImageView> attachments;
28 (attachments.push_back(+imageViews), ...);
29
30 if (renderPass.getAttachmentCount() != attachments.size()) {
31 throw VulkanException("Failed to create framebuffer!", "The number of attachments in the frame buffer must match the number of attachment descriptions in the render pass");
32 }
33
34 VkFramebufferCreateInfo framebufferInfo{};
35 framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
36 framebufferInfo.renderPass = +renderPass;
37 framebufferInfo.attachmentCount = attachments.size();
38 framebufferInfo.pAttachments = attachments.data();
39 framebufferInfo.width = resolution.first;
40 framebufferInfo.height = resolution.second;
41 framebufferInfo.layers = 1;
42
43 if (VkResult result = vkCreateFramebuffer(+virtualGpu, &framebufferInfo, nullptr, &framebuffer); result != VK_SUCCESS) {
44 throw VulkanException("Failed to create framebuffer!");
45 }
46
47 std::cout << "\n+ Framebuffer created";
48 }
49
50 Framebuffer(const Framebuffer&) = delete;
52
53
54 Framebuffer(Framebuffer&& movedFrom) noexcept : framebuffer{ movedFrom.framebuffer }, resolution{ movedFrom.resolution }, virtualGpu{ movedFrom.virtualGpu } {
55 movedFrom.framebuffer = nullptr;
56 std::cout << "\n> Framebuffer moved";
57 }
58
59 //FIXTHIS implement this the right way
61
63 vkDestroyFramebuffer(+virtualGpu, framebuffer, nullptr);
64 std::cout << "\n- Framebuffer destroyed";
65 }
66
67
68 const VkFramebuffer& operator+() const {
69 return framebuffer;
70 }
71
72
73 std::pair<unsigned int, unsigned int> getResolution() const {
74 return resolution;
75 }
76
77
87 template<std::same_as<ImageView>... IV>
88 static std::vector<Framebuffer> generateFramebufferForEachSwapchainImageView(const LogicalDevice& virtualGpu, const PipelineOptions::RenderPass& renderPass, const Swapchain& swapchain, const IV&... otherAttachments) {
89 std::vector<Framebuffer> framebuffers;
90
91 auto& images = swapchain.getImages();
92 for (const auto& image : images) {
93 auto& imageViews = image.getImageViews();
94 for (const auto& imageView : imageViews) {
95 framebuffers.emplace_back(virtualGpu, renderPass, swapchain.getResolution(), imageView.second, otherAttachments...);
96 }
97 }
98
99 return framebuffers;
100 }
101
102private:
103 VkFramebuffer framebuffer;
104 const LogicalDevice& virtualGpu;
105 std::pair<unsigned int, unsigned int> resolution;
106};
107
108
109#endif
A Framebuffer links toghether the real attachments (e.g. image views in the swapchain or depth buffer...
Definition: Framebuffer.h:22
Framebuffer(const LogicalDevice &virtualGpu, const PipelineOptions::RenderPass &renderPass, std::pair< unsigned int, unsigned int > resolution, const IV &... imageViews)
Definition: Framebuffer.h:26
Framebuffer & operator=(const Framebuffer &)=delete
Framebuffer(Framebuffer &&movedFrom) noexcept
Definition: Framebuffer.h:54
Framebuffer(const Framebuffer &)=delete
Framebuffer & operator=(Framebuffer &&)=default
const VkFramebuffer & operator+() const
Definition: Framebuffer.h:68
~Framebuffer()
Definition: Framebuffer.h:62
std::pair< unsigned int, unsigned int > getResolution() const
Definition: Framebuffer.h:73
static std::vector< Framebuffer > generateFramebufferForEachSwapchainImageView(const LogicalDevice &virtualGpu, const PipelineOptions::RenderPass &renderPass, const Swapchain &swapchain, const IV &... otherAttachments)
Creates a framebuffer for each image view in the specified swapchain. It also attaches the specified ...
Definition: Framebuffer.h:88
A logical device is an abstraction of the physical GPU which we can mainly use to send commands.
Definition: LogicalDevice.h:15
A RenderPass is the set of attachments, the way they are used, and the rendering work that is perform...
Definition: RenderPass.h:20
int getAttachmentCount() const
Definition: RenderPass.h:91
Object which holds images to be presented to the WindowSurface.
Definition: Swapchain.h:19
std::pair< unsigned int, unsigned int > getResolution() const
Returns the width and height of the images in this swapchain.
Definition: Swapchain.cpp:95
const std::vector< Image > & getImages() const
Returns the images of this swapchain.
Definition: Swapchain.cpp:90
Generic runtime exception thrown by Vulkan-related functions.
Definition: VulkanException.h:13
Types of queue families.
Definition: Animations.h:17