Vulkan Schnee 0.0.1
High-performance rendering engine
Loading...
Searching...
No Matches
DataBuffer.cpp
Go to the documentation of this file.
2#include "plog/Log.h"
4
5#include <sstream>
6
7namespace EngineCore {
9 VkPhysicalDevice physicalDevice,
10 VmaAllocator allocator,
11 const VkBufferUsageFlags bufferUsageFlags,
12 const VkMemoryPropertyFlags memoryProperties,
13 const VkDeviceSize size)
15 {
16 VkBufferCreateInfo bufferCI{ VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };
17 bufferCI.size = size;
18 bufferCI.usage = bufferUsageFlags;
19 bufferCI.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
20
21 VmaAllocationCreateInfo allocCI{};
22 allocCI.usage = VMA_MEMORY_USAGE_AUTO;
23
24 // Configure VMA based on requested memory properties
25 if (memoryProperties & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT)
26 {
27 allocCI.flags |= VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT;
28 }
29 if (memoryProperties & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT)
30 {
31 allocCI.flags |= VMA_ALLOCATION_CREATE_MAPPED_BIT;
32 }
33 if ((memoryProperties & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) &&
34 !(memoryProperties & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT))
35 {
36 allocCI.usage = VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE;
37 }
38
39 if (vmaCreateBuffer(allocator_, &bufferCI, &allocCI, &buffer, &allocation_, nullptr) != VK_SUCCESS)
40 {
41 THROW_ERROR("Failed to create DataBuffer with VMA");
42 }
43 }
44
46 {
47 if (allocator_ && buffer)
48 {
49 vmaDestroyBuffer(allocator_, buffer, allocation_);
50 buffer = VK_NULL_HANDLE;
51 allocation_ = VK_NULL_HANDLE;
52 }
53 }
54
55 bool DataBuffer::copyTo(const DataBuffer& target, VkCommandBuffer commandBuffer, VkQueue queue) const
56 {
57 VkCommandBufferBeginInfo beginInfo{ VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO };
58 beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
59 PLOG_THROW_FN_VK(vkBeginCommandBuffer(commandBuffer, &beginInfo));
60
61 VkBufferCopy copyRegion{};
62 copyRegion.size = size;
63 vkCmdCopyBuffer(commandBuffer, buffer, target.getBuffer(), 1u, &copyRegion);
64
65 PLOG_THROW_FN_VK(vkEndCommandBuffer(commandBuffer));
66
67
68 VkSubmitInfo submitInfo{ VK_STRUCTURE_TYPE_SUBMIT_INFO };
69 submitInfo.commandBufferCount = 1u;
70 submitInfo.pCommandBuffers = &commandBuffer;
71 PLOG_THROW_FN_VK(vkQueueSubmit(queue, 1u, &submitInfo, VK_NULL_HANDLE));
72
73
74 PLOG_THROW_FN_VK(vkQueueWaitIdle(queue));
75
76 return true;
77 }
78
79 void* DataBuffer::map() const
80 {
81 void* data = nullptr;
83 {
84 if (vmaMapMemory(allocator_, allocation_, &data) != VK_SUCCESS)
85 {
86 PLOGE << "Failed to map DataBuffer memory";
87 return nullptr;
88 }
89 }
90 return data;
91 }
92
93 void DataBuffer::unmap() const
94 {
96 {
97 vmaUnmapMemory(allocator_, allocation_);
98 }
99 }
100
101 VkBuffer DataBuffer::getBuffer() const
102 {
103 return buffer;
104 }
105}
VmaAllocation allocation_
Definition DataBuffer.h:33
DataBuffer(VkDevice device, VkPhysicalDevice physicalDevice, VmaAllocator allocator, VkBufferUsageFlags bufferUsageFlags, VkMemoryPropertyFlags memoryProperties, VkDeviceSize size)
Definition DataBuffer.cpp:8
VkBuffer getBuffer() const
bool copyTo(const DataBuffer &target, VkCommandBuffer commandBuffer, VkQueue queue) const
VkPhysicalDevice physicalDevice
Definition DataBuffer.h:29
VmaAllocator allocator_
Definition DataBuffer.h:30
Log category system implementation.