From b2b1a256fec072bac92e714f7720c19db8755cae Mon Sep 17 00:00:00 2001 From: Camden Dixie O'Brien Date: Fri, 26 Sep 2025 12:02:44 +0100 Subject: [PATCH] Create procedure for setting up GPU buffers --- gpu.c | 87 ++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 48 insertions(+), 39 deletions(-) diff --git a/gpu.c b/gpu.c index 39790d2..25fb473 100644 --- a/gpu.c +++ b/gpu.c @@ -47,6 +47,50 @@ load_shader_module(VkDevice dev, const char *code, size_t code_size) return mod; } +static void create_buffer( + const gpu_ctx_t *ctx, VkDeviceSize size, VkBufferUsageFlags usage, + VkMemoryPropertyFlags props, VkBuffer *buf_out, VkDeviceMemory *mem_out) +{ + VkResult result; + + // Create buffer + const VkBufferCreateInfo buf_config = { + .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, + .size = size, + .usage = usage, + .sharingMode = VK_SHARING_MODE_EXCLUSIVE, + }; + result = vkCreateBuffer(ctx->dev, &buf_config, nullptr, buf_out); + assert(result == VK_SUCCESS); + + // Find appropriate memory type + VkMemoryRequirements requirements; + vkGetBufferMemoryRequirements(ctx->dev, *buf_out, &requirements); + VkPhysicalDeviceMemoryProperties mem_props; + vkGetPhysicalDeviceMemoryProperties(ctx->physical_dev, &mem_props); + uint32_t mem_type = UINT32_MAX; + for (unsigned i = 0; i < mem_props.memoryTypeCount; ++i) { + if ((requirements.memoryTypeBits & (1 << i)) == 0) + continue; + if ((mem_props.memoryTypes[i].propertyFlags & props) == props) { + mem_type = i; + break; + } + } + assert(mem_type != UINT32_MAX); + + // Allocate memory + VkMemoryAllocateInfo allocate_config = { + .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, + .allocationSize = requirements.size, + .memoryTypeIndex = mem_type, + }; + result = vkAllocateMemory(ctx->dev, &allocate_config, nullptr, mem_out); + assert(result == VK_SUCCESS); + + vkBindBufferMemory(ctx->dev, *buf_out, *mem_out, 0); +} + void gpu_init(gpu_ctx_t *ctx, const gpu_config_t *config) { // Initialise Vulkan instance. @@ -435,47 +479,12 @@ void gpu_init(gpu_ctx_t *ctx, const gpu_config_t *config) // Create vertex buffer ctx->vertex_data_size = config->vertex_data_size; - const VkBufferCreateInfo vertex_buf_config = { - .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, - .size = ctx->vertex_data_size, - .usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, - .sharingMode = VK_SHARING_MODE_EXCLUSIVE, - }; - result = vkCreateBuffer( - ctx->dev, &vertex_buf_config, nullptr, &ctx->vertex_buf); - assert(result == VK_SUCCESS); - - // Allocate memory to vertex buffer - const VkMemoryPropertyFlags mem_flags + const VkMemoryPropertyFlags vertex_buf_props = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT; - VkMemoryRequirements mem_requirements; - vkGetBufferMemoryRequirements( - ctx->dev, ctx->vertex_buf, &mem_requirements); - VkPhysicalDeviceMemoryProperties mem_props; - vkGetPhysicalDeviceMemoryProperties(ctx->physical_dev, &mem_props); - uint32_t mem_type = UINT32_MAX; - for (unsigned i = 0; i < mem_props.memoryTypeCount; ++i) { - const bool valid_type - = (mem_requirements.memoryTypeBits & (1 << i)) != 0; - const bool has_flags - = (mem_props.memoryTypes[i].propertyFlags & mem_flags) - == mem_flags; - if (valid_type && has_flags) { - mem_type = i; - break; - } - } - assert(mem_type != UINT32_MAX); - VkMemoryAllocateInfo allocate_config = { - .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, - .allocationSize = mem_requirements.size, - .memoryTypeIndex = mem_type, - }; - result = vkAllocateMemory( - ctx->dev, &allocate_config, nullptr, &ctx->vertex_buf_mem); - assert(result == VK_SUCCESS); - vkBindBufferMemory(ctx->dev, ctx->vertex_buf, ctx->vertex_buf_mem, 0); + create_buffer( + ctx, ctx->vertex_data_size, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, + vertex_buf_props, &ctx->vertex_buf, &ctx->vertex_buf_mem); // Copy vertex data into vertex buffer void *vertex_buf_ptr;