vengine  0.1.0
3D graphics engine made with Vulkan
Loading...
Searching...
No Matches
buffer.cpp
Go to the documentation of this file.
1#include <cassert>
2#include <cstring>
3
4#include "VEngine/Buffer.hpp"
5
6VkDeviceSize ven::Buffer::getAlignment(const VkDeviceSize instanceSize, const VkDeviceSize minOffsetAlignment) {
7 if (minOffsetAlignment > 0) {
8 return (instanceSize + minOffsetAlignment - 1) & ~(minOffsetAlignment - 1);
9 }
10 return instanceSize;
11}
12
13ven::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}
14{
17}
18
20{
21 unmap();
22 vkDestroyBuffer(m_device.device(), m_buffer, nullptr);
23 vkFreeMemory(m_device.device(), m_memory, nullptr);
24}
25
26VkResult ven::Buffer::map(const VkDeviceSize size, const VkDeviceSize offset)
27{
28 assert(m_buffer && m_memory && "Called map on m_buffer before create");
29 return vkMapMemory(m_device.device(), m_memory, offset, size, 0, &m_mapped);
30}
31
33{
34 if (m_mapped != nullptr) {
35 vkUnmapMemory(m_device.device(), m_memory);
36 m_mapped = nullptr;
37 }
38}
39
40void ven::Buffer::writeToBuffer(const void *data, const VkDeviceSize size, const VkDeviceSize offset) const
41{
42 assert(m_mapped && "Cannot copy to unmapped m_buffer");
43
44 if (size == VK_WHOLE_SIZE) {
45 memcpy(m_mapped, data, m_bufferSize);
46 } else {
47 char *memOffset = static_cast<char *>(m_mapped);
48 memOffset += offset;
49 memcpy(memOffset, data, size);
50 }
51}
52
53VkResult ven::Buffer::flush(const VkDeviceSize size, const VkDeviceSize offset) const
54{
55 VkMappedMemoryRange mappedRange = {};
56 mappedRange.sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
57 mappedRange.memory = m_memory;
58 mappedRange.offset = offset;
59 mappedRange.size = size;
60 return vkFlushMappedMemoryRanges(m_device.device(), 1, &mappedRange);
61}
62
63VkResult ven::Buffer::invalidate(const VkDeviceSize size, const VkDeviceSize offset) const
64{
65 VkMappedMemoryRange mappedRange = {};
66 mappedRange.sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
67 mappedRange.memory = m_memory;
68 mappedRange.offset = offset;
69 mappedRange.size = size;
70 return vkInvalidateMappedMemoryRanges(m_device.device(), 1, &mappedRange);
71}
This file contains the Buffer class.
void unmap()
Unmap a mapped memory range.
Definition buffer.cpp:32
uint32_t m_instanceCount
Definition Buffer.hpp:153
VkDeviceSize m_bufferSize
Definition Buffer.hpp:151
VkBuffer m_buffer
Definition Buffer.hpp:148
VkResult map(VkDeviceSize size=VK_WHOLE_SIZE, VkDeviceSize offset=0)
Map a memory range of this buffer.
Definition buffer.cpp:26
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:53
VkDeviceSize m_alignmentSize
Definition Buffer.hpp:154
VkDeviceMemory m_memory
Definition Buffer.hpp:149
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:63
static VkDeviceSize getAlignment(VkDeviceSize instanceSize, VkDeviceSize minOffsetAlignment)
Returns the minimum instance size required to be compatible with devices minOffsetAlignment.
Definition buffer.cpp:6
Buffer(Device &device, VkDeviceSize instanceSize, uint32_t instanceCount, VkBufferUsageFlags usageFlags, VkMemoryPropertyFlags memoryPropertyFlags, VkDeviceSize minOffsetAlignment=1)
Definition buffer.cpp:13
VkMemoryPropertyFlags m_memoryPropertyFlags
Definition Buffer.hpp:156
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:40
VkBufferUsageFlags m_usageFlags
Definition Buffer.hpp:155
void createBuffer(VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags propertiesp, VkBuffer &buffer, VkDeviceMemory &bufferMemory) const
Definition device.cpp:384