vOOlkan
An object oriented approach to Vulkan
Attachment.h
Go to the documentation of this file.
1#ifndef VULKAN_ATTACHMENT
2#define VULKAN_ATTACHMENT
3
4#include <vulkan/vulkan.h>
5#include <vector>
6
8#include "VulkanException.h"
9
10
12 class AttachmentDescription; enum class PredefinedAttachment; enum class AttachmentType;
13
14 template<typename T>
15 concept IsAttachment = requires(T t) {
16 std::same_as<T, AttachmentDescription> ||
17 std::same_as < T, std::pair<AttachmentDescription, VkImageLayout>> ||
18 std::same_as < T, std::pair<AttachmentDescription, AttachmentColorBlendingMode>>// && t.first.getType() == AttachmentType::COLOR) ||
19 || std::same_as < T, std::tuple<AttachmentDescription, VkImageLayout, AttachmentColorBlendingMode>>;// && std::get<0>(t).getType() == AttachmentType::COLOR);
20 };
21
22}
23
26};
27
28
34};
35
36
44 public:
45
52 AttachmentDescription(PredefinedAttachment predefined = PredefinedAttachment::STANDARD_COLOR) : attachment{}, attachmentReferenceLayout{} {
53 //struct for the attachment
54 switch (predefined) {
56 attachment.format = VK_FORMAT_B8G8R8A8_SRGB;
57 attachment.samples = VK_SAMPLE_COUNT_1_BIT;
58 attachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
59 attachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
60 attachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
61 attachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
62 attachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
63 attachment.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
64
65 //format reference
66 attachmentReferenceLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
67
68 //set type
70 break;
71
73 attachment.format = VK_FORMAT_D32_SFLOAT;
74 attachment.samples = VK_SAMPLE_COUNT_1_BIT;
75 attachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
76 attachment.storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
77 attachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
78 attachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
79 attachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
80 attachment.finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
81
82 //format reference
83 attachmentReferenceLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
84
85 //set type
87 break;
88 }
89 }
90
91
99 AttachmentDescription(const VkAttachmentDescription& baseAttachment, AttachmentType type, VkImageLayout referenceLayout = VK_IMAGE_LAYOUT_UNDEFINED) : attachment{ baseAttachment }, attachmentReferenceLayout{ referenceLayout }, type{ type } {}
100
101
105 VkAttachmentDescription& operator+() {
106 return attachment;
107 }
108
109
116 return type;
117 }
118
119
126 return attachmentReferenceLayout;
127 }
128
129
137 public:
145 BoundAttachmentDescription(const AttachmentDescription& attachment, int index, VkImageLayout layout, AttachmentColorBlendingMode colorBlendingMode) :
146 attachment{ attachment.attachment },
147 attachmentReference{}, type{ attachment.type },
148 colorBlendingMode{ colorBlendingMode } {
149 attachmentReference.attachment = index;
150 attachmentReference.layout = layout;
151 }
152
153
157 const VkAttachmentDescription& operator+() const {
158 return attachment;
159 }
160
161
168 return attachmentReference.attachment;
169 }
170
171
177 const AttachmentType& getType() const {
178 return type;
179 }
180
181
187 const VkAttachmentReference& getAttachmentReference() const {
188 return attachmentReference;
189 }
190
191
198 if (type == AttachmentType::COLOR) {
199 return colorBlendingMode;
200 }
201 throw VulkanException{ "This attachment is not a color attachment, therefore it hasn't got the color blending mode" };
202 }
203
204 private:
205 VkAttachmentDescription attachment;
206 VkAttachmentReference attachmentReference;
207 AttachmentType type;
208 AttachmentColorBlendingMode colorBlendingMode;
209 };
210
211
221 template<IsAttachment... A>
222 static std::vector<BoundAttachmentDescription> prepareAttachments(A... attachments) {
223 std::vector<BoundAttachmentDescription> boundAttachments; //array with the attachments which must have a complete VkAttachmentReference
224
225 //Put in the vector the attachment and layout to bind toghether. The right overload is called based on the type of the attachment argument (pair<AttachmentDescription, layout> or AttachmentDescription).
226 std::vector<std::tuple<AttachmentDescription, VkImageLayout, AttachmentColorBlendingMode>> attachmentLayoutColorBlendingModeTriplets;
227 (attachmentLayoutColorBlendingModeTriplets.push_back(parseAttachment(attachments)), ...);
228
229 //for each pair in the vector, add it to the array to return
230 for (const auto& alp : attachmentLayoutColorBlendingModeTriplets) {
231 boundAttachments.emplace_back(std::get<0>(alp), boundAttachments.size(), std::get<1>(alp), std::get<2>(alp)); //add the BoundAttachmentDescription to the array to return
232 }
233
234 return boundAttachments;
235 }
236
237
238 private:
239
240 //These 4 functions are used to return the attachment and its respective layout, which are then used in prepareAttachments(...) to build the array to create a subpass.
241 static std::tuple<AttachmentDescription, VkImageLayout, AttachmentColorBlendingMode> parseAttachment(const std::pair<AttachmentDescription, VkImageLayout>& attachment) {
242 if (attachment.first.type == AttachmentType::COLOR) {
243 return { attachment.first, attachment.second, AttachmentColorBlendingMode{PredefinedColorBlendingModes::STANDARD} };
244 }
245 return { attachment.first, attachment.second, AttachmentColorBlendingMode{} };
246 }
247
248 //These 4 functions are used to return the attachment and its respective layout, which are then used in prepareAttachments(...) to build the array to create a subpass.
249 static std::tuple<AttachmentDescription, VkImageLayout, AttachmentColorBlendingMode> parseAttachment(const AttachmentDescription& attachment) {
250 if (attachment.type == AttachmentType::COLOR) {
251 return { attachment, attachment.attachmentReferenceLayout, AttachmentColorBlendingMode{PredefinedColorBlendingModes::STANDARD} };
252 }
253 return { attachment, attachment.attachmentReferenceLayout, AttachmentColorBlendingMode{} };
254 }
255
256 //These 4 functions are used to return the attachment and its respective layout, which are then used in prepareAttachments(...) to build the array to create a subpass.
257 static std::tuple<AttachmentDescription, VkImageLayout, AttachmentColorBlendingMode> parseAttachment(const std::pair<AttachmentDescription, AttachmentColorBlendingMode>& attachment) {
258 return { attachment.first, attachment.first.attachmentReferenceLayout, attachment.second };
259 }
260
261 //These 4 functions are used to return the attachment and its respective layout, which are then used in prepareAttachments(...) to build the array to create a subpass.
262 static std::tuple<AttachmentDescription, VkImageLayout, AttachmentColorBlendingMode> parseAttachment(const std::tuple<AttachmentDescription, VkImageLayout, AttachmentColorBlendingMode>& attachment) {
263 return attachment;
264 }
265
266
267 VkAttachmentDescription attachment;
268 VkImageLayout attachmentReferenceLayout;
269 AttachmentType type;
270};
271
272#endif
A AttachmentColorBlendingMode describes how a render Subpass will write to a color attachment.
Definition: AttachmentColorBlendingMode.h:20
A BoundAttachmentDescription is an AttachmentDescription which ir ready to be used in a RenderPass an...
Definition: Attachment.h:136
int getAttachmentReferenceIndex() const
Returns the index with which this attachment will be referenced by a render Subpass.
Definition: Attachment.h:167
BoundAttachmentDescription(const AttachmentDescription &attachment, int index, VkImageLayout layout, AttachmentColorBlendingMode colorBlendingMode)
Creates an object starting from an AttachmentDescription.
Definition: Attachment.h:145
AttachmentColorBlendingMode getColorBlendingMode() const
If this attachment is a color attachment, it returns the color blending mode.
Definition: Attachment.h:197
const VkAttachmentReference & getAttachmentReference() const
Returns the underlying VkAttachmentReference struct, which is the struct used by a render Subpass to ...
Definition: Attachment.h:187
const VkAttachmentDescription & operator+() const
Access to the underlying VkAttachmentDescription Vulkan object.
Definition: Attachment.h:157
const AttachmentType & getType() const
Returns the type of this attachment, namely how the attachment will be used by a render Subpass.
Definition: Attachment.h:177
An AttachmentDescription is basically the description of render target where the GPU will draw.
Definition: Attachment.h:43
VkImageLayout & getAttachmentReferenceLayout()
Returns a modifiable reference to the default VkImageLayout used during BoundAttachmentDescription cr...
Definition: Attachment.h:125
AttachmentDescription(PredefinedAttachment predefined=PredefinedAttachment::STANDARD_COLOR)
Generates an attachment based on some standard attachment models.
Definition: Attachment.h:52
AttachmentDescription(const VkAttachmentDescription &baseAttachment, AttachmentType type, VkImageLayout referenceLayout=VK_IMAGE_LAYOUT_UNDEFINED)
Creates the AttachmentDescription starting from its Vulkan underlying struct.
Definition: Attachment.h:99
VkAttachmentDescription & operator+()
Access to the underlying VkAttachmentDescription Vulkan object.
Definition: Attachment.h:105
static std::vector< BoundAttachmentDescription > prepareAttachments(A... attachments)
Prepares the attachments to be used into a RenderPass and its render Subpass(es), fixing a position f...
Definition: Attachment.h:222
AttachmentType & getType()
Returns a modifiable reference to the type of this attachment, namely how the attachment will be used...
Definition: Attachment.h:115
Generic runtime exception thrown by Vulkan-related functions.
Definition: VulkanException.h:13
AttachmentType
Each AttachmentDescription, based on his properties, can be used by a render Subpass in different way...
Definition: Attachment.h:32
PredefinedAttachment
Definition: Attachment.h:24