#include "resources/buffer.h" #include "vulkan_helper.h" #include namespace iris { void *Buffer_tt::map() { if (!mapped_data.has_value()) { CHECK_VULKAN(vmaMapMemory(allocator, allocation, &mapped_data.value())); } return mapped_data.value(); } void Buffer_tt::unmap() { if (mapped_data.has_value()) { vmaUnmapMemory(allocator, allocation); mapped_data.reset(); } else { spdlog::warn("Buffer is not mapped, skipping"); } } void Buffer_tt::release() { if (handle != VK_NULL_HANDLE) { spdlog::debug("Destroying buffer: 0x{:x}", (uint64_t)handle); vkDeviceWaitIdle(device); vmaDestroyBuffer(allocator, handle, allocation); handle = VK_NULL_HANDLE; } } std::shared_ptr Device::create_buffer(BufferDesc desc) { VkBufferCreateInfo buffer_create_info = { .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, .size = desc.size, .usage = desc.buffer_usage, }; VmaAllocationCreateInfo alloc_create_info = { .usage = desc.memory_usage, }; std::shared_ptr buffer = std::make_shared(); buffer->device = this->device; buffer->allocator = this->allocator; buffer->desc = desc; CHECK_VULKAN(vmaCreateBuffer( allocator, &buffer_create_info, &alloc_create_info, &buffer->handle, &buffer->allocation, VK_NULL_HANDLE)); spdlog::debug("Created buffer: 0x{:x}", (uint64_t)buffer->handle); return buffer; } } // namespace iris