From 5c2677df733fc7f8a5d12ae3d0e1e648566db2bb Mon Sep 17 00:00:00 2001 From: gramanas Date: Fri, 14 Jun 2024 00:10:58 +0300 Subject: VMA initial integration --- src/vksetup.h | 81 ++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 49 insertions(+), 32 deletions(-) (limited to 'src/vksetup.h') diff --git a/src/vksetup.h b/src/vksetup.h index 608975b..adf4699 100644 --- a/src/vksetup.h +++ b/src/vksetup.h @@ -47,8 +47,7 @@ #include -#define VMA_STATIC_VULKAN_FUNCTIONS 0 -#define VMA_DYNAMIC_VULKAN_FUNCTIONS 1 + #include "vk_mem_alloc.h" #include "../lib/cglm/include/cglm/cglm.h" @@ -164,12 +163,14 @@ typedef struct vks_swapchain { typedef struct vks_buffer { VkBuffer handle; VkDeviceMemory memory; + VmaAllocation allocation; } vks_buffer; typedef struct vks_image { VkImage handle; VkDeviceMemory memory; VkImageView view; + VmaAllocation allocation; } vks_image; typedef struct vks_pipeline { @@ -991,13 +992,11 @@ VKSDEF void vks_cleanup_swapchain(const vks_context vk) { vkDestroyImageView(vk.device, vk.color_image.view, NULL); - vkDestroyImage(vk.device, vk.color_image.handle, NULL); - vkFreeMemory(vk.device, vk.color_image.memory, NULL); + vmaDestroyImage(vk.allocator, vk.color_image.handle, vk.color_image.allocation); vkDestroyImageView(vk.device, vk.depth_image.view, NULL); - vkDestroyImage(vk.device, vk.depth_image.handle, NULL); - vkFreeMemory(vk.device, vk.depth_image.memory, NULL); - + vmaDestroyImage(vk.allocator, vk.depth_image.handle, vk.depth_image.allocation); + for (uint32_t i = 0; i < vk.swapchain.image_count; i++) { vkDestroyImageView(vk.device, vk.swapchain.image_views[i], NULL); } @@ -1040,12 +1039,29 @@ vks_create_vulkan_context(vks_context* vk) /* TODO: Create vks_get_queue helpers?? */ _vulkan_create_logical_device(vk); + _vulkan_create_command_pool(vk); /* Create swapchain */ /* TODO: Make swapchain api */ _vulkan_create_swap_chain(vk); + + /* VMA POC */ + VmaVulkanFunctions vulkanFunctions = {0}; + vulkanFunctions.vkGetInstanceProcAddr = &vkGetInstanceProcAddr; + vulkanFunctions.vkGetDeviceProcAddr = &vkGetDeviceProcAddr; + + VmaAllocatorCreateInfo allocatorCreateInfo = {0}; + allocatorCreateInfo.flags = VMA_ALLOCATOR_CREATE_EXT_MEMORY_BUDGET_BIT; + allocatorCreateInfo.vulkanApiVersion = VK_API_VERSION_1_2; + allocatorCreateInfo.physicalDevice = vk->physical_device; + allocatorCreateInfo.device = vk->device; + allocatorCreateInfo.instance = vk->instance; + allocatorCreateInfo.pVulkanFunctions = &vulkanFunctions; + + vmaCreateAllocator(&allocatorCreateInfo, &vk->allocator); + /* Create resources */ _vulkan_create_color_resources(vk); _vulkan_create_depth_resources(vk); @@ -1287,21 +1303,15 @@ vks_create_image(const vks_context vk, imageInfo.samples = numSamples; imageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE; - VK_CHECK(vkCreateImage(vk.device, &imageInfo, NULL, &image->handle)); + VmaAllocationCreateInfo allocInfo = {0}; + allocInfo.usage = VMA_MEMORY_USAGE_AUTO; - VkMemoryRequirements memRequirements; - vkGetImageMemoryRequirements(vk.device, image->handle, &memRequirements); - - VkMemoryAllocateInfo allocInfo = { 0 }; - allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; - allocInfo.allocationSize = memRequirements.size; - allocInfo.memoryTypeIndex = - _find_memory_type(vk, memRequirements.memoryTypeBits, properties); - - // TODO: group allocations etc... (allocations limited by hardware) - VK_CHECK(vkAllocateMemory(vk.device, &allocInfo, NULL, &image->memory)); - - vkBindImageMemory(vk.device, image->handle, image->memory, 0); + VK_CHECK(vmaCreateImage(vk.allocator, + &imageInfo, + &allocInfo, + &image->handle, + &image->allocation, + NULL)); } VKSDEF void @@ -1317,19 +1327,26 @@ vks_create_buffer(const vks_context vk, bufferInfo.usage = usage; bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE; - VK_CHECK(vkCreateBuffer(vk.device, &bufferInfo, NULL, &buffer->handle)); - VkMemoryRequirements mem_requirements; - vkGetBufferMemoryRequirements(vk.device, buffer->handle, &mem_requirements); + VmaAllocationCreateInfo allocInfo = {0}; + allocInfo.usage = VMA_MEMORY_USAGE_AUTO; + allocInfo.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT; + + //if (properties & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) + + vmaCreateBuffer(vk.allocator, &bufferInfo, &allocInfo, &buffer->handle, &buffer->allocation, NULL); + + /* VkMemoryRequirements mem_requirements; */ + /* vkGetBufferMemoryRequirements(vk.device, buffer->handle, &mem_requirements); */ - VkMemoryAllocateInfo allocInfo = { 0 }; - allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; - allocInfo.allocationSize = mem_requirements.size; - allocInfo.memoryTypeIndex = - _find_memory_type(vk, mem_requirements.memoryTypeBits, properties); + /* VkMemoryAllocateInfo allocInfo = { 0 }; */ + /* allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; */ + /* allocInfo.allocationSize = mem_requirements.size; */ + /* allocInfo.memoryTypeIndex = */ + /* _find_memory_type(vk, mem_requirements.memoryTypeBits, properties); */ - // TODO: group allocations etc... (allocations limited by hardware) - VK_CHECK(vkAllocateMemory(vk.device, &allocInfo, NULL, &buffer->memory)); - VK_CHECK(vkBindBufferMemory(vk.device, buffer->handle, buffer->memory, 0)); + /* // TODO: group allocations etc... (allocations limited by hardware) */ + /* VK_CHECK(vkAllocateMemory(vk.device, &allocInfo, NULL, &buffer->memory)); */ + /* VK_CHECK(vkBindBufferMemory(vk.device, buffer->handle, buffer->memory, 0)); */ } #endif /* VKSETUP_IMPLEMENTATION */ -- cgit v1.2.3