Lugdunum  0.1.0
Buffer.cpp
Go to the documentation of this file.
2 
3 #include <cstring>
4 
7 
8 namespace lug {
9 namespace Graphics {
10 namespace Vulkan {
11 namespace API {
12 
14  VkBuffer buffer,
15  const Device* device) : _buffer(buffer), _device(device) {
16  vkGetBufferMemoryRequirements(static_cast<VkDevice>(*device), _buffer, &_requirements);
17 }
18 
20  _buffer = buffer._buffer;
21  _device = buffer._device;
23  _requirements = buffer._requirements;
24  _deviceMemoryOffset = buffer._deviceMemoryOffset;
25  buffer._buffer = VK_NULL_HANDLE;
26  buffer._device = nullptr;
27  buffer._deviceMemory = nullptr;
28  buffer._requirements = {};
29  buffer._deviceMemoryOffset = 0;
30 }
31 
33  destroy();
34 
35  _buffer = buffer._buffer;
36  _device = buffer._device;
38  _requirements = buffer._requirements;
39  _deviceMemoryOffset = buffer._deviceMemoryOffset;
40  buffer._buffer = VK_NULL_HANDLE;
41  buffer._device = nullptr;
42  buffer._deviceMemory = nullptr;
43  buffer._requirements = {};
44  buffer._deviceMemoryOffset = 0;
45 
46  return *this;
47 }
48 
50  destroy();
51 }
52 
54  if (_buffer != VK_NULL_HANDLE) {
55  vkDestroyBuffer(static_cast<VkDevice>(*_device), _buffer, nullptr);
56  _buffer = VK_NULL_HANDLE;
57  }
58 }
59 
60 void Buffer::bindMemory(const DeviceMemory& deviceMemory, VkDeviceSize memoryOffset) {
61  _deviceMemory = &deviceMemory;
62  _deviceMemoryOffset = memoryOffset;
63 
64  vkBindBufferMemory(static_cast<VkDevice>(*_device), static_cast<VkBuffer>(_buffer), static_cast<VkDeviceMemory>(deviceMemory), memoryOffset);
65 }
66 
67 bool Buffer::updateData(const void* data, VkDeviceSize size, VkDeviceSize offset) const {
68  void* gpuData = _deviceMemory->mapBuffer(*this, size, offset);
69 
70  if (!gpuData) {
71  return false;
72  }
73 
74  memcpy(gpuData, data, size);
76 
77  return true;
78 }
79 
80 } // API
81 } // Vulkan
82 } // Graphics
83 } // lug
void bindMemory(const DeviceMemory &deviceMemory, VkDeviceSize memoryOffset=0)
Definition: Buffer.cpp:60
const DeviceMemory * _deviceMemory
Definition: Buffer.hpp:56
VkMemoryRequirements _requirements
Definition: Buffer.hpp:59
void * mapBuffer(const API::Buffer &buffer, VkDeviceSize size=VK_WHOLE_SIZE, VkDeviceSize offset=0) const
Buffer & operator=(const Buffer &)=delete
bool updateData(const void *data, VkDeviceSize size, VkDeviceSize offset=0) const
Definition: Buffer.cpp:67