From 589c7e4fef4acf3f1b7be02088065a4f1723228c Mon Sep 17 00:00:00 2001 From: Lubosz Sarnecki Date: Wed, 6 May 2020 16:38:12 +0200 Subject: [PATCH] aux/vk: Add buffer helper structs and functions. --- src/xrt/auxiliary/vk/vk_helpers.c | 50 +++++++++++++++++++++++++++++++ src/xrt/auxiliary/vk/vk_helpers.h | 17 +++++++++++ 2 files changed, 67 insertions(+) diff --git a/src/xrt/auxiliary/vk/vk_helpers.c b/src/xrt/auxiliary/vk/vk_helpers.c index b6bba2166..39b2ba7e4 100644 --- a/src/xrt/auxiliary/vk/vk_helpers.c +++ b/src/xrt/auxiliary/vk/vk_helpers.c @@ -1120,3 +1120,53 @@ vk_allocate_descriptor_sets(struct vk_bundle *vk, return true; } + +bool +vk_buffer_init(struct vk_bundle *vk, + VkDeviceSize size, + VkBufferUsageFlags usage, + VkMemoryPropertyFlags properties, + VkBuffer *out_buffer, + VkDeviceMemory *out_mem) +{ + VkBufferCreateInfo buffer_info = { + .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, + .size = size, + .usage = usage, + }; + + VkResult res = + vk->vkCreateBuffer(vk->device, &buffer_info, NULL, out_buffer); + vk_check_error("vkCreateBuffer", res, false); + + VkMemoryRequirements requirements; + vk->vkGetBufferMemoryRequirements(vk->device, *out_buffer, + &requirements); + + VkMemoryAllocateInfo alloc_info = { + .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, + .allocationSize = requirements.size}; + + if (!vk_get_memory_type(vk, requirements.memoryTypeBits, properties, + &alloc_info.memoryTypeIndex)) { + fprintf(stderr, + "Failed to find matching memoryTypeIndex for buffer\n"); + return false; + } + + res = vk->vkAllocateMemory(vk->device, &alloc_info, NULL, out_mem); + vk_check_error("vkAllocateMemory", res, false); + + res = vk->vkBindBufferMemory(vk->device, *out_buffer, *out_mem, 0); + vk_check_error("vkBindBufferMemory", res, false); + + return true; +} + +void +vk_buffer_destroy(struct vk_buffer *self, struct vk_bundle *vk) +{ + vk->vkDestroyBuffer(vk->device, self->handle, NULL); + vk->vkFreeMemory(vk->device, self->memory, NULL); +} + diff --git a/src/xrt/auxiliary/vk/vk_helpers.h b/src/xrt/auxiliary/vk/vk_helpers.h index cbe4ac0d8..795c5731b 100644 --- a/src/xrt/auxiliary/vk/vk_helpers.h +++ b/src/xrt/auxiliary/vk/vk_helpers.h @@ -180,6 +180,13 @@ struct vk_bundle // clang-format on }; +struct vk_buffer +{ + VkBuffer handle; + VkDeviceMemory memory; + uint32_t size; + void *data; +}; /* * @@ -417,6 +424,16 @@ vk_allocate_descriptor_sets(struct vk_bundle *vk, const VkDescriptorSetLayout *set_layout, VkDescriptorSet *sets); +bool +vk_buffer_init(struct vk_bundle *vk, + VkDeviceSize size, + VkBufferUsageFlags usage, + VkMemoryPropertyFlags properties, + VkBuffer *out_buffer, + VkDeviceMemory *out_mem); + +void +vk_buffer_destroy(struct vk_buffer *self, struct vk_bundle *vk); #ifdef __cplusplus }