Lugdunum  0.1.0
Image.cpp
Go to the documentation of this file.
2 
6 
7 namespace lug {
8 namespace Graphics {
9 namespace Vulkan {
10 namespace API {
11 
12 Image::Image(VkImage image, const Device* device, const Extent& extent, VkFormat format, bool swapchainImage) :
13  _image(image), _device(device), _extent(extent), _format(format), _swapchainImage(swapchainImage) {
14  if (_image != VK_NULL_HANDLE) {
15  vkGetImageMemoryRequirements(static_cast<VkDevice>(*device), _image, &_requirements);
16  }
17 }
18 
19 Image::Image(Image&& image) {
20  _image = image._image;
21  _device = image._device;
22  _swapchainImage = image._swapchainImage;
23  _extent = image._extent;
24  _requirements = image._requirements;
26  _deviceMemoryOffset = image._deviceMemoryOffset;
27  _format = image._format;
28 
29  image._image = VK_NULL_HANDLE;
30  image._device = nullptr;
31  image._extent = {0, 0};
32  image._requirements = {};
33  image._deviceMemory = nullptr;
34  image._deviceMemoryOffset = 0;
35  image._format = VK_FORMAT_UNDEFINED;
36 }
37 
39  destroy();
40 
41  _image = image._image;
42  _device = image._device;
43  _swapchainImage = image._swapchainImage;
44  _extent = image._extent;
45  _requirements = image._requirements;
47  _deviceMemoryOffset = image._deviceMemoryOffset;
48  _format = image._format;
49 
50  image._image = VK_NULL_HANDLE;
51  image._device = nullptr;
52  image._extent = {0, 0};
53  image._requirements = {};
54  image._deviceMemory = nullptr;
55  image._deviceMemoryOffset = 0;
56  image._format = VK_FORMAT_UNDEFINED;
57 
58  return *this;
59 }
60 
62  // Swapchain images are automatically destroyed when destroying swapchain
63  if (!_swapchainImage) {
64  destroy();
65  }
66 }
67 
69  if (_image != VK_NULL_HANDLE) {
70  vkDestroyImage(static_cast<VkDevice>(*_device), _image, nullptr);
71  _image = VK_NULL_HANDLE;
72  }
73 }
74 
75 void Image::bindMemory(const DeviceMemory& deviceMemory, VkDeviceSize memoryOffset) {
76  _deviceMemory = &deviceMemory;
77  _deviceMemoryOffset = memoryOffset;
78 
79  vkBindImageMemory(static_cast<VkDevice>(*_device), _image, static_cast<VkDeviceMemory>(deviceMemory), memoryOffset);
80 }
81 
82 VkFormat Image::findSupportedFormat(const Device& device, const std::set<VkFormat>& formats, VkImageTiling tiling, VkFormatFeatureFlags features) {
83  for (auto format: formats) {
84  if (isFormatSupported(device, format, tiling, features)) {
85  return format;
86  }
87  }
88 
89  LUG_LOG.error("Image: Can't find supported format format given features");
90  return VK_FORMAT_UNDEFINED;
91 }
92 
93 bool Image::isFormatSupported(const Device& device, VkFormat format, VkImageTiling tiling, VkFormatFeatureFlags features) {
94  const PhysicalDeviceInfo* physicalDeviceInfo = device.getPhysicalDeviceInfo();
95 
96  if (physicalDeviceInfo->formatProperties.find(format) == physicalDeviceInfo->formatProperties.end()) {
97  LUG_LOG.warn("Image::isFormatSupported: the format does not exists in physicalDeviceInfo->formatProperties");
98  return false;
99  }
100 
101  const VkFormatProperties& formatProperties = physicalDeviceInfo->formatProperties.at(format);
102 
103  if (tiling == VK_IMAGE_TILING_LINEAR && (formatProperties.linearTilingFeatures & features) == features) {
104  return true;
105  } else if (tiling == VK_IMAGE_TILING_OPTIMAL && (formatProperties.optimalTilingFeatures & features) == features) {
106  return true;
107  }
108 
109  return false;
110 }
111 
112 } // API
113 } // Vulkan
114 } // Graphics
115 } // lug
Image & operator=(const Image &)=delete
VkMemoryRequirements _requirements
Definition: Image.hpp:80
void bindMemory(const DeviceMemory &deviceMemory, VkDeviceSize memoryOffset=0)
Definition: Image.cpp:75
const PhysicalDeviceInfo * getPhysicalDeviceInfo() const
Definition: Device.inl:6
static VkFormat findSupportedFormat(const Device &device, const std::set< VkFormat > &formats, VkImageTiling tiling, VkFormatFeatureFlags features)
Definition: Image.cpp:82
std::unordered_map< VkFormat, VkFormatProperties > formatProperties
Definition: Vulkan.hpp:190
#define LUG_LOG
Definition: Logger.hpp:73
const DeviceMemory * _deviceMemory
Definition: Image.hpp:72
static bool isFormatSupported(const Device &device, VkFormat format, VkImageTiling tiling, VkFormatFeatureFlags features)
Definition: Image.cpp:93
VkDeviceSize _deviceMemoryOffset
Definition: Image.hpp:73