2#include "../core/foray_managedbuffer.hpp"
20 template <
typename TClass>
35 inline virtual void SetName(std::string_view name)
override;
61 template <
typename TClass>
64 if(section.has_value())
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())
73 VkDeviceSize totalCountInVector = (VkDeviceSize)mVector.size();
75 if(totalCountInVector > mDeviceCapacity)
78 CreateBuffer(totalCountInVector + (totalCountInVector / 4));
82 UploadToBuffer(updateSection);
83 mDeviceCount = totalCountInVector;
86 template <
typename TClass>
89 mDeviceCapacity = capacity;
91 VkDeviceSize bufferSize = mDeviceCapacity *
sizeof(TClass);
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);
98 mBuffer.Map(mHostMemoryMap);
101 template <
typename TClass>
104 TClass* uploadData = mVector.data() + section.
offset;
108 VkDeviceSize deviceSize = section.
count *
sizeof(TClass);
109 VkDeviceSize deviceOffset = section.
offset *
sizeof(TClass);
110 mBuffer.WriteDataDeviceLocal(uploadData, deviceSize, deviceOffset);
114 size_t memorySize = section.
count *
sizeof(TClass);
115 size_t memoryOffset = section.
offset *
sizeof(TClass);
117 uint8_t* memoryDestination =
reinterpret_cast<uint8_t*
>(mHostMemoryMap) + memoryOffset;
119 memcpy(
reinterpret_cast<void*
>(memoryDestination), uploadData, memorySize);
122 template <
typename TClass>
125 if(!DeviceLocal && mBuffer.GetIsMapped())
132 template <
typename TClass>
135 mBuffer.SetName(name);
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