-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathframebuffer.cc
148 lines (113 loc) · 4.96 KB
/
framebuffer.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <vkpp/framebuffer.hh>
#include <vkpp/device.hh>
#include <vkpp/exception.hh>
#include <utility>
namespace vkpp {
Framebuffer::Framebuffer(VkDevice& device,
RenderPass& render_pass,
std::vector<ImageView>& image_views,
const VkExtent2D& extent)
: extent { extent },
render_pass { render_pass.get_handle() },
device { device } {
auto create_info = partially_create_info();
create_info.attachmentCount = image_views.size();
this->image_views.reserve(image_views.size());
for (auto& image_view : image_views) {
this->image_views.push_back(image_view.get_handle());
}
create_info.pAttachments = this->image_views.data();
if (VkResult error = vkCreateFramebuffer(device, &create_info, nullptr, &handle)) {
throw Exception { error, "couldn't create framebuffer!" };
}
}
Framebuffer::Framebuffer(Device& device,
RenderPass& render_pass,
std::vector<ImageView>& image_views,
const VkExtent2D& extent)
: Framebuffer { device.get_handle(), render_pass,
image_views, extent } { }
Framebuffer::Framebuffer(Device& device,
RenderPass& render_pass,
ImageView& attachment,
const VkExtent2D& extent)
: Framebuffer { device.get_handle(), render_pass,
attachment, extent } {}
Framebuffer::Framebuffer(VkDevice& device,
RenderPass& render_pass,
ImageView& image_view,
const VkExtent2D& extent)
: extent { extent },
render_pass { render_pass.get_handle() },
device { device } {
auto create_info = partially_create_info();
create_info.attachmentCount = 1;
this->image_views.reserve(1);
this->image_views.push_back(image_view.get_handle());
create_info.pAttachments = this->image_views.data();
if (VkResult error = vkCreateFramebuffer(device, &create_info, nullptr, &handle)) {
throw Exception { error, "couldn't create framebuffer!" };
}
}
Framebuffer::Framebuffer(VkDevice& device,
RenderPass& render_pass,
ImageView& color_attachment,
ImageView& depth_attachment,
const VkExtent2D& extent)
: extent { extent },
render_pass { render_pass.get_handle() },
device { device } {
auto create_info = partially_create_info();
create_info.attachmentCount = 2;
this->image_views.reserve(2);
this->image_views.push_back(color_attachment.get_handle());
this->image_views.push_back(depth_attachment.get_handle());
create_info.pAttachments = this->image_views.data();
if (VkResult error = vkCreateFramebuffer(device, &create_info, nullptr, &handle)) {
throw Exception { error, "couldn't create framebuffer!" };
}
}
Framebuffer::~Framebuffer() noexcept {
if (handle != VK_NULL_HANDLE) {
vkDestroyFramebuffer(device, handle, nullptr);
}
}
Framebuffer::Framebuffer(Framebuffer&& framebuffer) noexcept {
swap(*this, framebuffer);
}
Framebuffer& Framebuffer::operator=(Framebuffer&& framebuffer) noexcept {
swap(*this, framebuffer);
return *this;
}
void swap(Framebuffer& lhs, Framebuffer& rhs) {
using std::swap;
swap(lhs.extent, rhs.extent);
swap(lhs.image_views, rhs.image_views);
swap(lhs.handle, rhs.handle);
swap(lhs.render_pass, rhs.render_pass);
swap(lhs.device, rhs.device);
}
VkFramebuffer& Framebuffer::get_handle() {
return handle;
}
VkRenderPass& Framebuffer::get_current_render_pass() {
return render_pass;
}
const std::vector<VkImageView>& Framebuffer::get_attachments() const {
return image_views;
}
const VkExtent2D& Framebuffer::get_extent() const {
return extent;
}
VkFramebufferCreateInfo Framebuffer::partially_create_info() {
VkFramebufferCreateInfo create_info;
create_info.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
create_info.pNext = nullptr;
create_info.flags = 0;
create_info.renderPass = render_pass;
create_info.width = extent.width;
create_info.height = extent.height;
create_info.layers = 1;
return create_info;
}
}