From 2ead02037dc89e987fbc0a021fe470e29d226cfd Mon Sep 17 00:00:00 2001 From: Chuyan Zhang Date: Sat, 7 Sep 2024 00:40:38 -0700 Subject: Add more encapsulation, use VMA for allocation --- src/vulkan_helper.cpp | 91 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 89 insertions(+), 2 deletions(-) (limited to 'src/vulkan_helper.cpp') diff --git a/src/vulkan_helper.cpp b/src/vulkan_helper.cpp index 84a2b6d..e533d2d 100644 --- a/src/vulkan_helper.cpp +++ b/src/vulkan_helper.cpp @@ -3,17 +3,19 @@ #include #include #include +#include +#include namespace iris { -Device::Device(std::vector layers, std::vector instance_extensions) { +Device::Device(std::vector layers, std::vector instance_extensions) { VkApplicationInfo app_info = { .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO, .pApplicationName = "IrisRenderer", .applicationVersion = VK_MAKE_VERSION(1, 0, 0), .pEngineName = "No Engine", .engineVersion = VK_MAKE_VERSION(1, 0, 0), - .apiVersion = VK_API_VERSION_1_0, + .apiVersion = VK_API_VERSION_1_3, }; // Create the Vulkan instance @@ -140,6 +142,91 @@ Device::Device(std::vector layers, std::vector instan main_queue_family_index, 0, &graphics_queue); + + // Create the memory allocator + VmaAllocatorCreateInfo allocator_info = { + .physicalDevice = physical_device, + .device = device, + .instance = instance, + }; + CHECK_VULKAN(vmaCreateAllocator( + &allocator_info, + &allocator)); +} + +Device::~Device() { + vmaDestroyAllocator(allocator); + vkDestroyDevice(device, VK_NULL_HANDLE); + vkDestroyInstance(instance, VK_NULL_HANDLE); +} + + +Buffer_t::~Buffer_t() { + vmaDestroyBuffer(allocator, buffer, allocation); +} + +Buffer Device::create_buffer(VkDeviceSize size, + VkBufferUsageFlags usage, + VmaMemoryUsage memory_usage) { + VkBufferCreateInfo buffer_info = { + .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, + .size = size, + .usage = usage, + }; + VmaAllocationCreateInfo allocation_info = { + .usage = memory_usage, + }; + Buffer_t buffer = { + .allocator = this->allocator, + .flags = usage, + .size = size, + }; + CHECK_VULKAN(vmaCreateBuffer( + allocator, + &buffer_info, + &allocation_info, + &buffer.buffer, + &buffer.allocation, + VK_NULL_HANDLE)); + return std::make_shared(buffer); +} + +Texture2D_t::~Texture2D_t() { + vmaDestroyImage(allocator, image, allocation); + // TODO: optionally destroy image view, if created +} + +Texture2D Device::create_texture(VkExtent2D extent, + VkFormat format, + VkImageUsageFlags usage, + VmaMemoryUsage memory_usage) { + VkImageCreateInfo image_info = { + .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, + .imageType = VK_IMAGE_TYPE_2D, + .format = format, + .extent = {extent.width, extent.height, 1}, + .mipLevels = 1, + .arrayLayers = 1, + .samples = VK_SAMPLE_COUNT_1_BIT, + .tiling = VK_IMAGE_TILING_OPTIMAL, + .usage = usage, + }; + VmaAllocationCreateInfo allocation_info = { + .usage = memory_usage, + }; + Texture2D_t texture = { + .allocator = this->allocator, + .flags = usage, + .extent = extent, + }; + CHECK_VULKAN(vmaCreateImage( + allocator, + &image_info, + &allocation_info, + &texture.image, + &texture.allocation, + VK_NULL_HANDLE)); + return std::make_shared(texture); } } // namespace iris \ No newline at end of file -- cgit v1.2.3-70-g09d2