vOOlkan
An object oriented approach to Vulkan
Barriers.h
Go to the documentation of this file.
1#ifndef VULKAN_BARRIERS
2#define VULKAN_BARRIERS
3
4#include <vulkan/vulkan.h>
5
6#include "Image.h"
7#include "VulkanException.h"
8
9
11
13public:
14
15 ImageMemoryBarrier(const Image& image, const VkImageLayout& oldLayout, const VkImageLayout& newLayout) {
16 auto maskInfo = getTransitionInfo(oldLayout, newLayout);
17
18 barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
19 barrier.pNext = nullptr;
20 barrier.oldLayout = oldLayout;
21 barrier.newLayout = newLayout;
22 barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
23 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
24 barrier.image = +image;
25 barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
26 barrier.subresourceRange.baseMipLevel = 0;
27 barrier.subresourceRange.levelCount = 1;
28 barrier.subresourceRange.baseArrayLayer = 0;
29 barrier.subresourceRange.layerCount = 1;
30 barrier.srcAccessMask = maskInfo.sourceMask;
31 barrier.dstAccessMask = maskInfo.destinationMask;
32 }
33
34
35 const VkImageMemoryBarrier& operator+() const {
36 return barrier;
37 }
38
39
40
42 VkPipelineStageFlagBits sourceStage, destinationStage;
43 VkAccessFlagBits sourceMask, destinationMask;
44 };
45
46
47
51 static TransitionInfo getTransitionInfo(const VkImageLayout& oldLayout, const VkImageLayout& newLayout) {
52 VkPipelineStageFlagBits sourceStage, destinationStage;
53 VkAccessFlagBits sourceMask, destinationMask;
54
55 if (oldLayout == VK_IMAGE_LAYOUT_UNDEFINED && newLayout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) {
56 sourceMask = VK_ACCESS_NONE;
57 destinationMask = VK_ACCESS_TRANSFER_WRITE_BIT;
58 sourceStage = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
59 destinationStage = VK_PIPELINE_STAGE_TRANSFER_BIT;
60 }
61 else if (oldLayout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL && newLayout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
62 sourceMask = VK_ACCESS_TRANSFER_WRITE_BIT;
63 destinationMask = VK_ACCESS_SHADER_READ_BIT;
64 sourceStage = VK_PIPELINE_STAGE_TRANSFER_BIT;
65 destinationStage = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
66 }
67 else {
68 throw VulkanException("Unsupported layout transition!");
69 }
70
71 return TransitionInfo{ sourceStage, destinationStage, sourceMask, destinationMask };
72 }
73
74
75private:
76 VkImageMemoryBarrier barrier;
77
78};
79
80#endif
An Image is an object representing what can be passed to the swapchain: the content of the image is w...
Definition: Image.h:19
const VkImageMemoryBarrier & operator+() const
Definition: Barriers.h:35
static TransitionInfo getTransitionInfo(const VkImageLayout &oldLayout, const VkImageLayout &newLayout)
Returns the right values for the mask and source/destination stage for a transition based on the old ...
Definition: Barriers.h:51
ImageMemoryBarrier(const Image &image, const VkImageLayout &oldLayout, const VkImageLayout &newLayout)
Definition: Barriers.h:15
Generic runtime exception thrown by Vulkan-related functions.
Definition: VulkanException.h:13
Definition: Barriers.h:10
VkPipelineStageFlagBits sourceStage
Definition: Barriers.h:42
VkPipelineStageFlagBits destinationStage
Definition: Barriers.h:42