From 8bd595989d03b7d18808a7e31ec3220024f31908 Mon Sep 17 00:00:00 2001 From: Camden Dixie O'Brien Date: Wed, 1 Oct 2025 16:29:27 +0100 Subject: [PATCH] Load bone matrices for each frame --- gpu.c | 12 +++++++++--- gpu.h | 4 +++- main.c | 2 +- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/gpu.c b/gpu.c index f320997..f775985 100644 --- a/gpu.c +++ b/gpu.c @@ -145,6 +145,9 @@ static void load_buffer( void gpu_init(gpu_ctx_t *ctx, const gpu_config_t *config, model_t model) { + ctx->index_count = model.index_count; + ctx->bone_count = model.bone_count; + // Initialise Vulkan instance. const VkApplicationInfo app_info = { .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO, @@ -776,7 +779,6 @@ void gpu_init(gpu_ctx_t *ctx, const gpu_config_t *config, model_t model) // Set up staging, vertex and index buffers const size_t vertex_data_size = model.vertex_count * sizeof(vertex_t); const size_t index_data_size = model.index_count * sizeof(vertex_t); - ctx->index_count = model.index_count; VkBuffer staging_buf; VkDeviceMemory staging_buf_mem; const size_t staging_buf_size = MAX(vertex_data_size, index_data_size); @@ -869,7 +871,8 @@ void gpu_cleanup(const gpu_ctx_t *ctx) vkDestroyInstance(ctx->inst, nullptr); } -void gpu_render(const gpu_ctx_t *ctx, const mvp_t *mvp) +void gpu_render( + const gpu_ctx_t *ctx, const mvp_t *mvp, const mat4_t *bone_matrices) { VkResult result; @@ -884,8 +887,11 @@ void gpu_render(const gpu_ctx_t *ctx, const mvp_t *mvp) &img_index); assert(result == VK_SUCCESS); - // Copy MVP matricies into uniform buffer for this image + // Copy MVP and bone matricies into uniform buffer for this image memcpy(ctx->mvps + img_index, mvp, sizeof(mvp_t)); + memcpy( + ctx->bone_matrices + (ctx->bone_count * img_index), bone_matrices, + ctx->bone_count * sizeof(mat4_t)); // Reset command buffer, then begin writing vkResetCommandBuffer(ctx->cmd_buf, 0); diff --git a/gpu.h b/gpu.h index c242544..497824f 100644 --- a/gpu.h +++ b/gpu.h @@ -53,6 +53,7 @@ typedef struct { VkBuffer index_buf; VkDeviceMemory index_buf_mem; size_t index_count; + size_t bone_count; } gpu_ctx_t; typedef struct { @@ -62,6 +63,7 @@ typedef struct { void gpu_init(gpu_ctx_t *ctx, const gpu_config_t *config, model_t model); void gpu_cleanup(const gpu_ctx_t *ctx); -void gpu_render(const gpu_ctx_t *ctx, const mvp_t *mvp); +void gpu_render( + const gpu_ctx_t *ctx, const mvp_t *mvp, const mat4_t *bone_matrices); #endif diff --git a/main.c b/main.c index cc8e9c0..af5c434 100644 --- a/main.c +++ b/main.c @@ -108,7 +108,7 @@ int main(int argc, char *argv[]) break; } - gpu_render(&gpu, &mvp); + gpu_render(&gpu, &mvp, anim.bone_matrices); } // Clean up