From e42986adc345d9d630da42b8343b1223e05a7105 Mon Sep 17 00:00:00 2001 From: Camden Dixie O'Brien Date: Wed, 1 Oct 2025 16:10:54 +0100 Subject: [PATCH] Create buffer for bone matrices --- gpu.c | 16 ++++++++++++++++ gpu.h | 3 +++ 2 files changed, 19 insertions(+) diff --git a/gpu.c b/gpu.c index 646fc9a..7a5a8c7 100644 --- a/gpu.c +++ b/gpu.c @@ -462,6 +462,18 @@ void gpu_init(gpu_ctx_t *ctx, const gpu_config_t *config, model_t model) ctx->dev, ctx->mvp_buffer_mem, 0, mvp_buffer_size, 0, (void **)&ctx->mvps); + // Create and map bone matrix buffer + const size_t bone_matrix_buffer_size + = swapchain_img_count * model.bone_count * sizeof(mat4_t); + create_buffer( + ctx, bone_matrix_buffer_size, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, + VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT + | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, + &ctx->bone_matrix_buffer, &ctx->bone_matrix_buffer_mem); + vkMapMemory( + ctx->dev, ctx->bone_matrix_buffer_mem, 0, bone_matrix_buffer_size, 0, + (void **)&ctx->bone_matrices); + // Configure descriptor sets to use MVP buffer for (unsigned i = 0; i < swapchain_img_count; ++i) { const VkWriteDescriptorSet desc_write = { @@ -803,6 +815,10 @@ void gpu_cleanup(const gpu_ctx_t *ctx) vkDestroyRenderPass(ctx->dev, ctx->render_pass, nullptr); vkDestroyPipelineLayout(ctx->dev, ctx->pipeline_layout, nullptr); + vkUnmapMemory(ctx->dev, ctx->bone_matrix_buffer_mem); + vkDestroyBuffer(ctx->dev, ctx->bone_matrix_buffer, nullptr); + vkFreeMemory(ctx->dev, ctx->bone_matrix_buffer_mem, nullptr); + vkUnmapMemory(ctx->dev, ctx->mvp_buffer_mem); vkDestroyBuffer(ctx->dev, ctx->mvp_buffer, nullptr); vkFreeMemory(ctx->dev, ctx->mvp_buffer_mem, nullptr); diff --git a/gpu.h b/gpu.h index fa89023..c242544 100644 --- a/gpu.h +++ b/gpu.h @@ -37,6 +37,9 @@ typedef struct { VkBuffer mvp_buffer; VkDeviceMemory mvp_buffer_mem; mvp_t *mvps; + VkBuffer bone_matrix_buffer; + VkDeviceMemory bone_matrix_buffer_mem; + mat4_t *bone_matrices; VkPipelineLayout pipeline_layout; VkRenderPass render_pass; VkPipeline pipeline;