Lugdunum  0.1.0
DeviceMemory.cpp
Go to the documentation of this file.
2 
7 
8 namespace lug {
9 namespace Graphics {
10 namespace Vulkan {
11 namespace API {
12 
13 DeviceMemory::DeviceMemory(VkDeviceMemory deviceMemory, const Device* device, VkDeviceSize size) : _deviceMemory(deviceMemory), _device(device), _size(size) {}
14 
16  _deviceMemory = deviceMemory._deviceMemory;
17  _device = deviceMemory._device;
18  _size = deviceMemory._size;
19  deviceMemory._deviceMemory = VK_NULL_HANDLE;
20  deviceMemory._device = nullptr;
21  deviceMemory._size = 0;
22 }
23 
25  destroy();
26 
27  _deviceMemory = deviceMemory._deviceMemory;
28  _device = deviceMemory._device;
29  _size = deviceMemory._size;
30  deviceMemory._deviceMemory = VK_NULL_HANDLE;
31  deviceMemory._device = nullptr;
32  deviceMemory._size = 0;
33 
34  return *this;
35 }
36 
38  destroy();
39 }
40 
42  if (_deviceMemory != VK_NULL_HANDLE) {
43  vkFreeMemory(static_cast<VkDevice>(*_device), _deviceMemory, nullptr);
44  _deviceMemory = VK_NULL_HANDLE;
45  }
46 }
47 
48 void* DeviceMemory::map(VkDeviceSize size, VkDeviceSize offset) const {
49  void* data = nullptr;
50 
51  VkResult result = vkMapMemory(static_cast<VkDevice>(*_device), _deviceMemory, offset, size, 0, &data);
52 
53  if (result != VK_SUCCESS) {
54  LUG_LOG.error("DeviceMemory: Can't map memory: {}", result);
55  return nullptr;
56  }
57 
58  return data;
59 }
60 
61 void* DeviceMemory::mapBuffer(const API::Buffer& buffer, VkDeviceSize size, VkDeviceSize offset) const {
62  if (buffer.getDeviceMemory() != this) {
63  LUG_LOG.error("DeviceMemory: Can't map memory of a buffer: The buffer uses a different device memory");
64  return nullptr;
65  }
66 
67  void* data = nullptr;
68 
69  VkResult result = vkMapMemory(
70  static_cast<VkDevice>(*_device),
72  offset + buffer.getDeviceMemoryOffset(),
73  size == VK_WHOLE_SIZE ? buffer.getRequirements().size - offset : size,
74  0,
75  &data
76  );
77 
78  if (result != VK_SUCCESS) {
79  LUG_LOG.error("DeviceMemory: Can't map memory of a buffer: {}", result);
80  return nullptr;
81  }
82 
83  return data;
84 }
85 
86 void* DeviceMemory::mapImage(const API::Image& image, VkDeviceSize size, VkDeviceSize offset) const {
87  if (image.getDeviceMemory() != this) {
88  LUG_LOG.error("DeviceMemory: Can't map memory of a image: The image uses a different device memory");
89  return nullptr;
90  }
91 
92  void* data = nullptr;
93 
94  VkResult result = vkMapMemory(
95  static_cast<VkDevice>(*_device),
97  offset + image.getDeviceMemoryOffset(),
98  size == VK_WHOLE_SIZE ? image.getRequirements().size - offset : size,
99  0,
100  &data
101  );
102 
103  if (result != VK_SUCCESS) {
104  LUG_LOG.error("DeviceMemory: Can't map memory of an image: {}", result);
105  return nullptr;
106  }
107 
108  return data;
109 }
110 
111 void DeviceMemory::unmap() const {
112  vkUnmapMemory(static_cast<VkDevice>(*_device), _deviceMemory);
113 }
114 
115 } // API
116 } // Vulkan
117 } // Graphics
118 } // lug
VkDeviceSize getDeviceMemoryOffset() const
Definition: Buffer.inl:9
VkDeviceSize getDeviceMemoryOffset() const
Definition: Image.inl:9
const VkMemoryRequirements & getRequirements() const
Definition: Buffer.inl:1
const VkMemoryRequirements & getRequirements() const
Definition: Image.inl:1
void * mapBuffer(const API::Buffer &buffer, VkDeviceSize size=VK_WHOLE_SIZE, VkDeviceSize offset=0) const
DeviceMemory & operator=(const DeviceMemory &)=delete
void * mapImage(const API::Image &image, VkDeviceSize size=VK_WHOLE_SIZE, VkDeviceSize offset=0) const
const DeviceMemory * getDeviceMemory() const
Definition: Image.inl:5
#define LUG_LOG
Definition: Logger.hpp:73
void * map(VkDeviceSize size=VK_WHOLE_SIZE, VkDeviceSize offset=0) const
const DeviceMemory * getDeviceMemory() const
Definition: Buffer.inl:5