Foray Library
rapid prototyping framework for crossplatform development of vulkan hardware ray tracing applications
Loading...
Searching...
No Matches
foray_managedbuffer.hpp
Go to the documentation of this file.
1#pragma once
2#include "../foray_vma.hpp"
3#include "../foray_vulkan.hpp"
5#include "foray_context.hpp"
7
8namespace foray::core {
9
11 class ManagedBuffer : public VulkanResource<VkObjectType::VK_OBJECT_TYPE_BUFFER>
12 {
13 public:
16 {
18 VkBufferCreateInfo BufferCreateInfo{};
20 VmaAllocationCreateInfo AllocationCreateInfo{};
22 VkDeviceSize Alignment{};
24 std::string Name{};
25
29 CreateInfo(VkBufferUsageFlags usage, VkDeviceSize size, VmaMemoryUsage memoryUsage, VmaAllocationCreateFlags flags = {}, std::string_view name = "");
30 };
31
32 public:
33 ManagedBuffer() : VulkanResource("Unnamed Buffer"){};
34
37 void Create(Context* context, const CreateInfo& createInfo);
40 void CreateForStaging(Context* context, VkDeviceSize size, const void* data = nullptr, std::string_view bufferName = {});
43 void Create(Context* context, VkBufferUsageFlags usage, VkDeviceSize size, VmaMemoryUsage memoryUsage, VmaAllocationCreateFlags flags = {}, std::string_view name = "");
44
45 virtual void Destroy() override;
46
47 virtual bool Exists() const override { return !!mAllocation; }
48
53 void WriteDataDeviceLocal(const void* data, VkDeviceSize size, VkDeviceSize offset = 0);
59 void WriteDataDeviceLocal(HostSyncCommandBuffer& cmdBuffer, const void* data, VkDeviceSize size, VkDeviceSize offset = 0);
60
62 void Map(void*& data);
64 void Unmap();
65
68 void MapAndWrite(const void* data, size_t size = 0);
69
70 inline virtual ~ManagedBuffer()
71 {
72 if(!!mAllocation)
73 {
74 Destroy();
75 }
76 }
77
79 FORAY_GETTER_V(IsMapped);
80 FORAY_GETTER_V(Allocation);
81 FORAY_GETTER_CR(AllocationInfo);
84 FORAY_GETTER_V(Alignment);
85
87 VkDeviceAddress GetDeviceAddress() const;
88
90 virtual void SetName(std::string_view name) override;
91
93 inline VkDescriptorBufferInfo GetVkDescriptorBufferInfo() const { return VkDescriptorBufferInfo{.buffer = mBuffer, .offset = 0, .range = mSize}; }
95 void FillVkDescriptorBufferInfo(VkDescriptorBufferInfo& bufferInfo) const;
96
97 protected:
99 VkBuffer mBuffer{};
100 VmaAllocation mAllocation{};
101 VmaAllocationInfo mAllocationInfo{};
102 VkDeviceSize mSize = {};
103 VkDeviceSize mAlignment = {};
104 bool mIsMapped = false;
105
107 };
108
109} // namespace foray::core
Extension of the commandbuffer wrapper for temporary host synchronized command buffer execution.
Definition foray_commandbuffer.hpp:41
Wraps allocation and lifetime functionality of a VkBuffer.
Definition foray_managedbuffer.hpp:12
void MapAndWrite(const void *data, size_t size=0)
Attempts to map buffer, write data, unmap.
virtual void Destroy() override
Destroy the resource.
FORAY_GETTER_CR(AllocationInfo)
ManagedBuffer()
Definition foray_managedbuffer.hpp:33
VmaAllocation mAllocation
Definition foray_managedbuffer.hpp:100
VkDeviceSize mSize
Definition foray_managedbuffer.hpp:102
VkDescriptorBufferInfo GetVkDescriptorBufferInfo() const
Fills VkDescriptorBufferInfo object with zero offset and full buffer size.
Definition foray_managedbuffer.hpp:93
void Create(Context *context, VkBufferUsageFlags usage, VkDeviceSize size, VmaMemoryUsage memoryUsage, VmaAllocationCreateFlags flags={}, std::string_view name="")
Simplified version of Create that omits the use of a create info but should be sufficient for many us...
void Create(Context *context, const CreateInfo &createInfo)
Creates the buffer based on createInfo.
void WriteDataDeviceLocal(const void *data, VkDeviceSize size, VkDeviceSize offset=0)
Employ a staging buffer to upload data.
void FillVkDescriptorBufferInfo(VkDescriptorBufferInfo &bufferInfo) const
Same as GetVkDescriptorBufferInfo, but writing to an existing object.
VkDeviceAddress GetDeviceAddress() const
Gets the buffers device address.
void WriteDataDeviceLocal(HostSyncCommandBuffer &cmdBuffer, const void *data, VkDeviceSize size, VkDeviceSize offset=0)
Create staging buffer, copy data to it, copy data to device buffer, destroy staging buffer.
void Map(void *&data)
Maps the buffer to memory address data.
void Unmap()
Unmaps the buffer.
VkDeviceSize mAlignment
Definition foray_managedbuffer.hpp:103
virtual ~ManagedBuffer()
Definition foray_managedbuffer.hpp:70
VkBuffer mBuffer
Definition foray_managedbuffer.hpp:99
void CreateForStaging(Context *context, VkDeviceSize size, const void *data=nullptr, std::string_view bufferName={})
Creates the buffer with VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VMA_MEMORY_USAGE_AUTO_PREFER_HOST,...
VmaAllocationInfo mAllocationInfo
Definition foray_managedbuffer.hpp:101
virtual void SetName(std::string_view name) override
Sets the buffers name. Decorates VkBuffer, VmaAllocation and VulkanResource base class.
bool mIsMapped
Definition foray_managedbuffer.hpp:104
Context * mContext
Definition foray_managedbuffer.hpp:98
virtual bool Exists() const override
Return true, if the managed resource is allocated.
Definition foray_managedbuffer.hpp:47
ManagedResource variant which automates GetTypeName() overloading by returning a stringified version ...
Definition foray_managedresource.hpp:59
Definition foray_commandbuffer.hpp:6
Non owning context object.
Definition foray_context.hpp:16
Combines all structs used for initialization.
Definition foray_managedbuffer.hpp:16
VmaAllocationCreateInfo AllocationCreateInfo
Allocation create info used to configure Vma.
Definition foray_managedbuffer.hpp:20
std::string Name
Debug name assigned to both VmaAllocation and VkBuffer vulkan object.
Definition foray_managedbuffer.hpp:24
CreateInfo(VkBufferUsageFlags usage, VkDeviceSize size, VmaMemoryUsage memoryUsage, VmaAllocationCreateFlags flags={}, std::string_view name="")
Shorthand for most commonly used fields.
VkBufferCreateInfo BufferCreateInfo
Buffer Create info used to configure Vulkan buffer create.
Definition foray_managedbuffer.hpp:18
VkDeviceSize Alignment
If non zero, Vma's aligned buffer allocate function is invoked.
Definition foray_managedbuffer.hpp:22
CreateInfo()
Default constructor (initializes .sType field and nulls everything else)