Foray Library
rapid prototyping framework for crossplatform development of vulkan hardware ray tracing applications
Loading...
Searching...
No Matches
foray_managedvectorbuffer.hpp
Go to the documentation of this file.
1#pragma once
2#include "../core/foray_managedbuffer.hpp"
3#include <optional>
4
5namespace foray::util {
7 {
8 VkDeviceSize offset = 0;
9 VkDeviceSize count = 0;
10
11 inline BufferSection Merge(const BufferSection& other)
12 {
13 VkDeviceSize start = std::min(offset, other.offset);
14 VkDeviceSize end = std::max(offset + count, other.offset + other.count);
15 return BufferSection{.offset = start, .count = end - start};
16 }
17 };
18
20 template <typename TClass>
22 {
23 public:
24 const bool DeviceLocal;
25
26 inline ManagedVectorBuffer() : DeviceLocal(false) {}
27 inline explicit ManagedVectorBuffer(core::Context* context) : DeviceLocal(false), mContext(context) {}
28 inline explicit ManagedVectorBuffer(core::Context* context, bool deviceLocal) : DeviceLocal(deviceLocal), mContext(context) {}
29
30 void InitOrUpdate(std::optional<BufferSection> section = {});
31 virtual void Destroy() override;
32 virtual bool Exists() const override { return mBuffer.Exists(); }
33
34 inline virtual std::string GetName() const { return mBuffer.GetName(); }
35 inline virtual void SetName(std::string_view name) override;
36
37 inline virtual ~ManagedVectorBuffer() { Destroy(); }
38
39 FORAY_PROPERTY_V(Context)
40 FORAY_GETTER_MR(Vector)
41 FORAY_GETTER_CR(Vector)
42 FORAY_GETTER_MR(Buffer)
43 FORAY_GETTER_CR(Buffer)
44 FORAY_GETTER_V(DeviceCount)
45 FORAY_GETTER_V(DeviceCapacity)
46
47 inline VkDeviceSize GetDeviceSize() { return mDeviceCapacity * sizeof(TClass); }
48
49 protected:
50 inline void CreateBuffer(VkDeviceSize capacity);
51 inline void UploadToBuffer(BufferSection section);
52
54 std::vector<TClass> mVector = {};
56 VkDeviceSize mDeviceCount = 0;
57 VkDeviceSize mDeviceCapacity = 0;
58 void* mHostMemoryMap = nullptr;
59 };
60
61 template <typename TClass>
62 void ManagedVectorBuffer<TClass>::InitOrUpdate(std::optional<BufferSection> section)
63 {
64 if(section.has_value())
65 {
66 FORAY_ASSERTFMT(section.value().offset + section.value().count <= mVector.size(),
67 "Buffer Update: BufferSection(offset = {}, count = {}) indicates vector read out of bounds (vector.size() = {})!", section.value().offset,
68 section.value().count, mVector.size())
69 }
70
71 BufferSection updateSection = section.has_value() ? section.value() : BufferSection{.offset = 0, .count = mVector.size()};
72
73 VkDeviceSize totalCountInVector = (VkDeviceSize)mVector.size();
74
75 if(totalCountInVector > mDeviceCapacity)
76 {
77 Destroy();
78 CreateBuffer(totalCountInVector + (totalCountInVector / 4));
79
80 updateSection = BufferSection{.offset = 0, .count = totalCountInVector};
81 }
82 UploadToBuffer(updateSection);
83 mDeviceCount = totalCountInVector;
84 }
85
86 template <typename TClass>
88 {
89 mDeviceCapacity = capacity;
90
91 VkDeviceSize bufferSize = mDeviceCapacity * sizeof(TClass);
92
93 mBuffer.Create(mContext, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT, bufferSize,
94 DeviceLocal ? VmaMemoryUsage::VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE : VmaMemoryUsage::VMA_MEMORY_USAGE_AUTO,
95 DeviceLocal ? 0 : VmaAllocationCreateFlagBits::VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT);
96 if(!DeviceLocal)
97 {
98 mBuffer.Map(mHostMemoryMap);
99 }
100 }
101 template <typename TClass>
103 {
104 TClass* uploadData = mVector.data() + section.offset;
105
106 if(DeviceLocal)
107 {
108 VkDeviceSize deviceSize = section.count * sizeof(TClass);
109 VkDeviceSize deviceOffset = section.offset * sizeof(TClass);
110 mBuffer.WriteDataDeviceLocal(uploadData, deviceSize, deviceOffset);
111 }
112 else
113 {
114 size_t memorySize = section.count * sizeof(TClass);
115 size_t memoryOffset = section.offset * sizeof(TClass);
116
117 uint8_t* memoryDestination = reinterpret_cast<uint8_t*>(mHostMemoryMap) + memoryOffset;
118
119 memcpy(reinterpret_cast<void*>(memoryDestination), uploadData, memorySize);
120 }
121 }
122 template <typename TClass>
124 {
125 if(!DeviceLocal && mBuffer.GetIsMapped())
126 {
127 mBuffer.Unmap();
128 }
129 mBuffer.Destroy();
130 }
131
132 template <typename TClass>
133 inline void ManagedVectorBuffer<TClass>::SetName(std::string_view name)
134 {
135 mBuffer.SetName(name);
136 }
137
138} // namespace foray
Wraps allocation and lifetime functionality of a VkBuffer.
Definition foray_managedbuffer.hpp:12
virtual bool Exists() const override
Return true, if the managed resource is allocated.
Definition foray_managedbuffer.hpp:47
Base class enforcing common interface for all classes wrapping a device resource.
Definition foray_managedresource.hpp:11
std::string_view GetName() const
Return a custom name for the object.
Definition foray_managedresource.hpp:40
Class maintaining a resizable array of templated classes in a managed buffer.
Definition foray_managedvectorbuffer.hpp:22
ManagedVectorBuffer()
Definition foray_managedvectorbuffer.hpp:26
VkDeviceSize mDeviceCapacity
Definition foray_managedvectorbuffer.hpp:57
void UploadToBuffer(BufferSection section)
Definition foray_managedvectorbuffer.hpp:102
std::vector< TClass > mVector
Definition foray_managedvectorbuffer.hpp:54
virtual std::string GetName() const
Definition foray_managedvectorbuffer.hpp:34
virtual void SetName(std::string_view name) override
Set a custom name for the object.
Definition foray_managedvectorbuffer.hpp:133
core::Context * mContext
Definition foray_managedvectorbuffer.hpp:53
void * mHostMemoryMap
Definition foray_managedvectorbuffer.hpp:58
virtual bool Exists() const override
Return true, if the managed resource is allocated.
Definition foray_managedvectorbuffer.hpp:32
void CreateBuffer(VkDeviceSize capacity)
Definition foray_managedvectorbuffer.hpp:87
virtual ~ManagedVectorBuffer()
Definition foray_managedvectorbuffer.hpp:37
ManagedVectorBuffer(core::Context *context, bool deviceLocal)
Definition foray_managedvectorbuffer.hpp:28
VkDeviceSize GetDeviceSize()
Definition foray_managedvectorbuffer.hpp:47
const bool DeviceLocal
Definition foray_managedvectorbuffer.hpp:24
void InitOrUpdate(std::optional< BufferSection > section={})
Definition foray_managedvectorbuffer.hpp:62
VkDeviceSize mDeviceCount
Definition foray_managedvectorbuffer.hpp:56
ManagedVectorBuffer(core::Context *context)
Definition foray_managedvectorbuffer.hpp:27
core::ManagedBuffer mBuffer
Definition foray_managedvectorbuffer.hpp:55
virtual void Destroy() override
Destroy the resource.
Definition foray_managedvectorbuffer.hpp:123
#define FORAY_GETTER_V(member)
Return value.
Definition foray_basics.hpp:39
#define FORAY_GETTER_CR(member)
Return constant reference.
Definition foray_basics.hpp:60
#define FORAY_GETTER_MR(member)
Return mutable reference.
Definition foray_basics.hpp:54
#define FORAY_PROPERTY_V(member)
Getter+Setter shorthand for value types.
Definition foray_basics.hpp:81
#define FORAY_ASSERTFMT(val, fmt,...)
Assertion macro for formatted error messages.
Definition foray_exception.hpp:72
Definition foray_dualbuffer.hpp:5
Non owning context object.
Definition foray_context.hpp:16
Definition foray_managedvectorbuffer.hpp:7
VkDeviceSize offset
Definition foray_managedvectorbuffer.hpp:8
BufferSection Merge(const BufferSection &other)
Definition foray_managedvectorbuffer.hpp:11
VkDeviceSize count
Definition foray_managedvectorbuffer.hpp:9