Initialize a buffer for use with the graphics backend.
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 {
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;
179
180 vk::MemoryAllocateInfo alloc_info;
181 alloc_info.allocationSize = mem_requirements.size;
182
184 mem_requirements.memoryTypeBits,
185 vk::MemoryPropertyFlags(buffer->get_memory_properties()));
186
187 vk::DeviceMemory memory;
188 try {
190 } catch (const vk::SystemError& e) {
195 std::source_location::current(),
196 "Failed to allocate VkDeviceMemory: " + std::string(e.what()));
197 }
198
199 try {
201 } catch (const vk::SystemError& e) {
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 {
216 } catch (const vk::SystemError& e) {
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);
231
233 "VulkanBuffer initialized: {} bytes, modality: {}, VkBuffer: {:p}",
234 buffer->get_size_bytes(),
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.
@ 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.