MayaFlux 0.4.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 167 of file BackendResoureManager.cpp.

168{
169 if (!buffer) {
171 "Attempted to initialize null VulkanBuffer");
172 return;
173 }
174
175 if (buffer->is_initialized()) {
177 "VulkanBuffer already initialized, skipping");
178 return;
179 }
180
181 vk::BufferCreateInfo buffer_info {};
182 buffer_info.size = buffer->get_size_bytes();
183 buffer_info.usage = buffer->get_usage_flags();
184 buffer_info.sharingMode = vk::SharingMode::eExclusive;
185
186 vk::Buffer vk_buffer;
187 try {
188 vk_buffer = m_context.get_device().createBuffer(buffer_info);
189 } catch (const vk::SystemError& e) {
193 std::source_location::current(),
194 "Failed to create VkBuffer: " + std::string(e.what()));
195 }
196
197 vk::MemoryRequirements mem_requirements;
198 mem_requirements = m_context.get_device().getBufferMemoryRequirements(vk_buffer);
199
200 vk::MemoryAllocateInfo alloc_info;
201 alloc_info.allocationSize = mem_requirements.size;
202
203 alloc_info.memoryTypeIndex = find_memory_type(
204 mem_requirements.memoryTypeBits,
205 vk::MemoryPropertyFlags(buffer->get_memory_properties()));
206
207 vk::DeviceMemory memory;
208 try {
209 memory = m_context.get_device().allocateMemory(alloc_info);
210 } catch (const vk::SystemError& e) {
211 m_context.get_device().destroyBuffer(vk_buffer);
215 std::source_location::current(),
216 "Failed to allocate VkDeviceMemory: " + std::string(e.what()));
217 }
218
219 try {
220 m_context.get_device().bindBufferMemory(vk_buffer, memory, 0);
221 } catch (const vk::SystemError& e) {
222 m_context.get_device().freeMemory(memory);
223 m_context.get_device().destroyBuffer(vk_buffer);
224
228 std::source_location::current(),
229 "Failed to bind buffer memory: " + std::string(e.what()));
230 }
231
232 void* mapped_ptr = nullptr;
233 if (buffer->is_host_visible()) {
234 try {
235 mapped_ptr = m_context.get_device().mapMemory(memory, 0, buffer->get_size_bytes());
236 } catch (const vk::SystemError& e) {
237 m_context.get_device().freeMemory(memory);
238 m_context.get_device().destroyBuffer(vk_buffer);
239
243 std::source_location::current(),
244 "Failed to map buffer memory: " + std::string(e.what()));
245 }
246 }
247
248 Buffers::VKBufferResources resources { .buffer = vk_buffer, .memory = memory, .mapped_ptr = mapped_ptr };
249 buffer->set_buffer_resources(resources);
250 m_managed_buffers.push_back(buffer);
251
253 "VulkanBuffer initialized: {} bytes, modality: {}, VkBuffer: {:p}",
254 buffer->get_size_bytes(),
255 Kakshya::modality_to_string(buffer->get_modality()),
256 (void*)buffer->get_buffer());
257}
#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:83

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: