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 147 of file BackendResoureManager.cpp.

148{
149 if (!buffer) {
151 "Attempted to initialize null VulkanBuffer");
152 return;
153 }
154
155 if (buffer->is_initialized()) {
157 "VulkanBuffer already initialized, skipping");
158 return;
159 }
160
161 vk::BufferCreateInfo buffer_info {};
162 buffer_info.size = buffer->get_size_bytes();
163 buffer_info.usage = buffer->get_usage_flags();
164 buffer_info.sharingMode = vk::SharingMode::eExclusive;
165
166 vk::Buffer vk_buffer;
167 try {
168 vk_buffer = m_context.get_device().createBuffer(buffer_info);
169 } catch (const vk::SystemError& e) {
173 std::source_location::current(),
174 "Failed to create VkBuffer: " + std::string(e.what()));
175 }
176
177 vk::MemoryRequirements mem_requirements;
178 mem_requirements = m_context.get_device().getBufferMemoryRequirements(vk_buffer);
179
180 vk::MemoryAllocateInfo alloc_info;
181 alloc_info.allocationSize = mem_requirements.size;
182
183 alloc_info.memoryTypeIndex = find_memory_type(
184 mem_requirements.memoryTypeBits,
185 vk::MemoryPropertyFlags(buffer->get_memory_properties()));
186
187 vk::DeviceMemory memory;
188 try {
189 memory = m_context.get_device().allocateMemory(alloc_info);
190 } catch (const vk::SystemError& e) {
191 m_context.get_device().destroyBuffer(vk_buffer);
195 std::source_location::current(),
196 "Failed to allocate VkDeviceMemory: " + std::string(e.what()));
197 }
198
199 try {
200 m_context.get_device().bindBufferMemory(vk_buffer, memory, 0);
201 } catch (const vk::SystemError& e) {
202 m_context.get_device().freeMemory(memory);
203 m_context.get_device().destroyBuffer(vk_buffer);
204
208 std::source_location::current(),
209 "Failed to bind buffer memory: " + std::string(e.what()));
210 }
211
212 void* mapped_ptr = nullptr;
213 if (buffer->is_host_visible()) {
214 try {
215 mapped_ptr = m_context.get_device().mapMemory(memory, 0, buffer->get_size_bytes());
216 } catch (const vk::SystemError& e) {
217 m_context.get_device().freeMemory(memory);
218 m_context.get_device().destroyBuffer(vk_buffer);
219
223 std::source_location::current(),
224 "Failed to map buffer memory: " + std::string(e.what()));
225 }
226 }
227
228 Buffers::VKBufferResources resources { .buffer = vk_buffer, .memory = memory, .mapped_ptr = mapped_ptr };
229 buffer->set_buffer_resources(resources);
230 m_managed_buffers.push_back(buffer);
231
233 "VulkanBuffer initialized: {} bytes, modality: {}, VkBuffer: {:p}",
234 buffer->get_size_bytes(),
235 Kakshya::modality_to_string(buffer->get_modality()),
236 (void*)buffer->get_buffer());
237}
#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: