Initialize a buffer for use with the graphics backend.
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 {
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;
104
105 vk::MemoryAllocateInfo alloc_info;
106 alloc_info.allocationSize = mem_requirements.size;
107
109 mem_requirements.memoryTypeBits,
110 vk::MemoryPropertyFlags(buffer->get_memory_properties()));
111
112 vk::DeviceMemory memory;
113 try {
115 } catch (const vk::SystemError& e) {
120 std::source_location::current(),
121 "Failed to allocate VkDeviceMemory: " + std::string(e.what()));
122 }
123
124 try {
126 } catch (const vk::SystemError& e) {
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 {
141 } catch (const vk::SystemError& e) {
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);
156
158 "VulkanBuffer initialized: {} bytes, modality: {}, VkBuffer: {:p}",
159 buffer->get_size_bytes(),
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.
@ 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.