MayaFlux 0.1.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ initialize_buffer()

void MayaFlux::Core::BackendResourceManager::initialize_buffer ( const std::shared_ptr< Buffers::VKBuffer > &  buffer)

Initialize a buffer for use with the graphics backend.

Parameters
bufferShared pointer to the buffer to initialize

Definition at line 72 of file BackendResoureManager.cpp.

73{
74 if (!buffer) {
76 "Attempted to initialize null VulkanBuffer");
77 return;
78 }
79
80 if (buffer->is_initialized()) {
82 "VulkanBuffer already initialized, skipping");
83 return;
84 }
85
86 vk::BufferCreateInfo buffer_info {};
87 buffer_info.size = buffer->get_size_bytes();
88 buffer_info.usage = buffer->get_usage_flags();
89 buffer_info.sharingMode = vk::SharingMode::eExclusive;
90
91 vk::Buffer vk_buffer;
92 try {
93 vk_buffer = m_context.get_device().createBuffer(buffer_info);
94 } catch (const vk::SystemError& e) {
98 std::source_location::current(),
99 "Failed to create VkBuffer: " + std::string(e.what()));
100 }
101
102 vk::MemoryRequirements mem_requirements;
103 mem_requirements = m_context.get_device().getBufferMemoryRequirements(vk_buffer);
104
105 vk::MemoryAllocateInfo alloc_info;
106 alloc_info.allocationSize = mem_requirements.size;
107
108 alloc_info.memoryTypeIndex = find_memory_type(
109 mem_requirements.memoryTypeBits,
110 vk::MemoryPropertyFlags(buffer->get_memory_properties()));
111
112 vk::DeviceMemory memory;
113 try {
114 memory = m_context.get_device().allocateMemory(alloc_info);
115 } catch (const vk::SystemError& e) {
116 m_context.get_device().destroyBuffer(vk_buffer);
120 std::source_location::current(),
121 "Failed to allocate VkDeviceMemory: " + std::string(e.what()));
122 }
123
124 try {
125 m_context.get_device().bindBufferMemory(vk_buffer, memory, 0);
126 } catch (const vk::SystemError& e) {
127 m_context.get_device().freeMemory(memory);
128 m_context.get_device().destroyBuffer(vk_buffer);
129
133 std::source_location::current(),
134 "Failed to bind buffer memory: " + std::string(e.what()));
135 }
136
137 void* mapped_ptr = nullptr;
138 if (buffer->is_host_visible()) {
139 try {
140 mapped_ptr = m_context.get_device().mapMemory(memory, 0, buffer->get_size_bytes());
141 } catch (const vk::SystemError& e) {
142 m_context.get_device().freeMemory(memory);
143 m_context.get_device().destroyBuffer(vk_buffer);
144
148 std::source_location::current(),
149 "Failed to map buffer memory: " + std::string(e.what()));
150 }
151 }
152
153 Buffers::VKBufferResources resources { .buffer = vk_buffer, .memory = memory, .mapped_ptr = mapped_ptr };
154 buffer->set_buffer_resources(resources);
155 m_managed_buffers.push_back(buffer);
156
158 "VulkanBuffer initialized: {} bytes, modality: {}, VkBuffer: {:p}",
159 buffer->get_size_bytes(),
160 Kakshya::modality_to_string(buffer->get_modality()),
161 (void*)buffer->get_buffer());
162}
#define MF_INFO(comp, ctx,...)
#define MF_ERROR(comp, ctx,...)
#define MF_WARN(comp, ctx,...)
uint32_t find_memory_type(uint32_t type_filter, vk::MemoryPropertyFlags properties) const
Find a suitable memory type for Vulkan buffer allocation.
std::vector< std::shared_ptr< Buffers::VKBuffer > > m_managed_buffers
vk::Device get_device() const
Get logical device.
Definition VKContext.hpp:49
@ GraphicsBackend
Graphics/visual rendering backend (Vulkan, OpenGL)
void error_rethrow(Component component, Context context, std::source_location location=std::source_location::current(), std::string_view additional_context="")
Catch and log an exception, then rethrow it.
@ Core
Core engine, backend, subsystems.
std::string_view modality_to_string(DataModality modality)
Convert DataModality enum to string representation.
Definition NDData.cpp:80

References MayaFlux::Buffers::VKBufferResources::buffer, MayaFlux::Journal::Core, find_memory_type(), MayaFlux::Core::VKContext::get_device(), MayaFlux::Journal::GraphicsBackend, m_context, m_managed_buffers, MF_ERROR, MF_INFO, MF_WARN, and MayaFlux::Kakshya::modality_to_string().

Referenced by download_image_data(), setup_backend_service(), and upload_image_data().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: