vOOlkan
An object oriented approach to Vulkan
Image.h
Go to the documentation of this file.
1#ifndef VULKAN_IMAGE
2#define VULKAN_IMAGE
3
4#include <vulkan/vulkan.h>
5#include <map>
6#include <string>
7
8#include "ImageView.h"
9#include "LogicalDevice.h"
10#include "VulkanException.h"
11#include "Buffer.h"
12
13namespace Vulkan { class Image; class LogicalDevice; class PhysicalDevice; class ImageView; namespace SwapchainOptions { class SurfaceFormat; } }
14
20 public:
21
22 Image(const VkImage& image, const LogicalDevice& virtualGpu, VkFormat format, std::pair<unsigned int, unsigned int> resolution);
23
24 Image(const Image&) = delete;
25 Image& operator=(const Image&) = delete;
26 Image(Image&&) noexcept;
27 Image& operator=(Image&&) noexcept;
28
29 ~Image();
30
31 const VkImage& operator+() const;
32
38 VkFormat getFormat() const;
39
40
48 const ImageView& generateImageView(std::string tag, const LogicalDevice& virtualGpu, VkImageAspectFlags type = VK_IMAGE_ASPECT_COLOR_BIT);
49
50
56 void eliminateImageView(std::string tag);
57
58
66 const ImageView& operator[](std::string tag) const;
67
68
74 const std::map<std::string, ImageView>& getImageViews() const;
75
76
77
78 friend void swap(Image& lhs, Image& rhs);
79
80
81 protected:
82 //Used only by move assignment operator in order to create an empty image
83 Image();
84
85 //Used by child classes only
86 template<std::same_as<VkMemoryPropertyFlagBits>... P>
87 Image(const LogicalDevice& virtualGpu, const PhysicalDevice& realGpu, VkFormat format, std::pair<unsigned int, unsigned int> resolution, VkImageTiling tiling, VkImageUsageFlags usage, P... memoryProperties) : virtualGpu{ &virtualGpu }, format{ format }, isSwapchainImage{ false }, layout{ VK_IMAGE_LAYOUT_UNDEFINED }, resolution{ resolution }{
88 VkImageCreateInfo imageInfo{};
89 imageInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
90 imageInfo.imageType = VK_IMAGE_TYPE_2D;
91 imageInfo.extent.width = resolution.first;
92 imageInfo.extent.height = resolution.second;
93 imageInfo.extent.depth = 1;
94 imageInfo.mipLevels = 1;
95 imageInfo.arrayLayers = 1;
96 imageInfo.format = format;
97 imageInfo.tiling = tiling;
98 imageInfo.initialLayout = layout;
99 imageInfo.usage = usage;
100 imageInfo.samples = VK_SAMPLE_COUNT_1_BIT;
101 imageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
102
103 if (VkResult result = vkCreateImage(+virtualGpu, &imageInfo, nullptr, &image); result != VK_SUCCESS) {
104 throw VulkanException("Failed to create image!", result);
105 }
106
107 VkMemoryRequirements memRequirements;
108 vkGetImageMemoryRequirements(+virtualGpu, image, &memRequirements);
109
110 VkMemoryAllocateInfo allocInfo{};
111 allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
112 allocInfo.allocationSize = memRequirements.size;
113 allocInfo.memoryTypeIndex = Buffers::Buffer::findSuitableMemoryIndex(realGpu, memRequirements.memoryTypeBits, memoryProperties...);
114
115 if (VkResult result = vkAllocateMemory(+virtualGpu, &allocInfo, nullptr, &allocatedMemory); result != VK_SUCCESS) {
116 throw VulkanException("Failed to allocate image memory!", result);
117 }
118
119 vkBindImageMemory(+virtualGpu, image, allocatedMemory, 0);
120 }
121
122
123 VkImage image;
124 VkFormat format;
125 VkImageLayout layout;
126 std::pair<unsigned int, unsigned int> resolution;
127 std::map<std::string, ImageView> views;
128 VkDeviceMemory allocatedMemory;
130 bool isSwapchainImage; //if an image is created automatically by the swapchain, it doesn't have to be explicitly destroyed
131};
132
133#endif
134
static uint32_t findSuitableMemoryIndex(const PhysicalDevice &realGpu, uint32_t suitableTypesBitmask, P... requiredMemoryProperties)
Definition: Buffer.h:61
An Image is an object representing what can be passed to the swapchain: the content of the image is w...
Definition: Image.h:19
VkFormat getFormat() const
Returns the format of the image, which is the same format of all the images in the swapchain of this ...
Definition: Image.cpp:57
std::pair< unsigned int, unsigned int > resolution
Definition: Image.h:126
VkDeviceMemory allocatedMemory
Definition: Image.h:128
LogicalDevice const * virtualGpu
Definition: Image.h:129
Image & operator=(const Image &)=delete
friend void swap(Image &lhs, Image &rhs)
void eliminateImageView(std::string tag)
Eliminates the specified image view.
Definition: Image.cpp:75
bool isSwapchainImage
Definition: Image.h:130
VkFormat format
Definition: Image.h:124
const ImageView & generateImageView(std::string tag, const LogicalDevice &virtualGpu, VkImageAspectFlags type=VK_IMAGE_ASPECT_COLOR_BIT)
Creates a new image view for this image, and adds it to the array of image views of this image,...
Definition: Image.cpp:63
const std::map< std::string, ImageView > & getImageViews() const
Returns all the image views of this image.
Definition: Image.cpp:90
Image(const Image &)=delete
VkImage image
Definition: Image.h:123
std::map< std::string, ImageView > views
Definition: Image.h:127
Image()
Definition: Image.cpp:19
VkImageLayout layout
Definition: Image.h:125
Part of an image where we can actually draw.
Definition: ImageView.h:12
A logical device is an abstraction of the physical GPU which we can mainly use to send commands.
Definition: LogicalDevice.h:15
Represents the GPU (or any other device) that will be used with Vulkan to perform computer graphics.
Definition: PhysicalDevice.h:17
The surface format defines the properties of the image where we will draw, such as the color space.
Definition: SwapchainSurfaceFormat.h:14
Types of queue families.
Definition: Animations.h:17