Initialize a buffer for use with the graphics backend.
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 {
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;
199
200 vk::MemoryAllocateInfo alloc_info;
201 alloc_info.allocationSize = mem_requirements.size;
202
204 mem_requirements.memoryTypeBits,
205 vk::MemoryPropertyFlags(buffer->get_memory_properties()));
206
207 vk::DeviceMemory memory;
208 try {
210 } catch (const vk::SystemError& e) {
215 std::source_location::current(),
216 "Failed to allocate VkDeviceMemory: " + std::string(e.what()));
217 }
218
219 try {
221 } catch (const vk::SystemError& e) {
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 {
236 } catch (const vk::SystemError& e) {
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);
251
253 "VulkanBuffer initialized: {} bytes, modality: {}, VkBuffer: {:p}",
254 buffer->get_size_bytes(),
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.
@ 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.