vengine  0.0.1
3D graphics engine
Loading...
Searching...
No Matches
buffer.cpp
Go to the documentation of this file.
1#include <cstring>
2
4
5ven::Buffer::Buffer(Device &device, const VkDeviceSize instanceSize, const uint32_t instanceCount, const VkBufferUsageFlags usageFlags, const VkMemoryPropertyFlags memoryPropertyFlags, const VkDeviceSize minOffsetAlignment) : m_device{device}, m_instanceSize{instanceSize}, m_instanceCount{instanceCount}, m_alignmentSize(getAlignment(instanceSize, minOffsetAlignment)), m_usageFlags{usageFlags}, m_memoryPropertyFlags{memoryPropertyFlags}
6{
9}
10
12{
13 unmap();
14 vkDestroyBuffer(m_device.device(), m_buffer, nullptr);
15 vkFreeMemory(m_device.device(), m_memory, nullptr);
16}
17
18VkResult ven::Buffer::map(const VkDeviceSize size, const VkDeviceSize offset)
19{
20 assert(m_buffer && m_memory && "Called map on buffer before create");
21 return vkMapMemory(m_device.device(), m_memory, offset, size, 0, &m_mapped);
22}
23
25{
26 if (m_mapped != nullptr) {
27 vkUnmapMemory(m_device.device(), m_memory);
28 m_mapped = nullptr;
29 }
30}
31
32void ven::Buffer::writeToBuffer(const void *data, const VkDeviceSize size, const VkDeviceSize offset) const
33{
34 assert(m_mapped && "Cannot copy to unmapped buffer");
35
36 if (size == VK_WHOLE_SIZE) {
37 memcpy(m_mapped, data, m_bufferSize);
38 } else {
39 auto *memOffset = static_cast<char *>(m_mapped);
40 memOffset += offset;
41 memcpy(memOffset, data, size);
42 }
43}
44
45VkResult ven::Buffer::flush(const VkDeviceSize size, const VkDeviceSize offset) const
46{
47 VkMappedMemoryRange mappedRange = {};
48 mappedRange.sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
49 mappedRange.memory = m_memory;
50 mappedRange.offset = offset;
51 mappedRange.size = size;
52 return vkFlushMappedMemoryRanges(m_device.device(), 1, &mappedRange);
53}
54
55VkResult ven::Buffer::invalidate(const VkDeviceSize size, const VkDeviceSize offset) const
56{
57 VkMappedMemoryRange mappedRange = {};
58 mappedRange.sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
59 mappedRange.memory = m_memory;
60 mappedRange.offset = offset;
61 mappedRange.size = size;
62 return vkInvalidateMappedMemoryRanges(m_device.device(), 1, &mappedRange);
63}
This file contains the Buffer class.
void unmap()
Unmap a mapped memory range.
Definition buffer.cpp:24
uint32_t m_instanceCount
Definition Buffer.hpp:158
VkDeviceSize m_bufferSize
Definition Buffer.hpp:156
VkBuffer m_buffer
Definition Buffer.hpp:153
VkResult map(VkDeviceSize size=VK_WHOLE_SIZE, VkDeviceSize offset=0)
Map a memory range of this buffer.
Definition buffer.cpp:18
VkResult flush(VkDeviceSize size=VK_WHOLE_SIZE, VkDeviceSize offset=0) const
Flush a memory range of the buffer to make it visible to the device.
Definition buffer.cpp:45
VkDeviceSize m_alignmentSize
Definition Buffer.hpp:159
VkDeviceMemory m_memory
Definition Buffer.hpp:154
VkResult invalidate(VkDeviceSize size=VK_WHOLE_SIZE, VkDeviceSize offset=0) const
Invalidate a memory range of the buffer to make it visible to the host.
Definition buffer.cpp:55
Buffer(Device &device, VkDeviceSize instanceSize, uint32_t instanceCount, VkBufferUsageFlags usageFlags, VkMemoryPropertyFlags memoryPropertyFlags, VkDeviceSize minOffsetAlignment=1)
Definition buffer.cpp:5
VkMemoryPropertyFlags m_memoryPropertyFlags
Definition Buffer.hpp:161
void writeToBuffer(const void *data, VkDeviceSize size=VK_WHOLE_SIZE, VkDeviceSize offset=0) const
Copies the specified data to the mapped buffer.
Definition buffer.cpp:32
VkBufferUsageFlags m_usageFlags
Definition Buffer.hpp:160
Class for device.
Definition Device.hpp:35
void createBuffer(VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, VkBuffer &buffer, VkDeviceMemory &bufferMemory) const
Definition device.cpp:384