diff --git a/gpu.c b/gpu.c index 7a5a8c7..f320997 100644 --- a/gpu.c +++ b/gpu.c @@ -409,15 +409,24 @@ void gpu_init(gpu_ctx_t *ctx, const gpu_config_t *config, model_t model) }; // Create descriptor set layout + const VkDescriptorSetLayoutBinding desc_set_layout_bindings[] = { + { + .binding = 0, + .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, + .descriptorCount = 1, + .stageFlags = VK_SHADER_STAGE_VERTEX_BIT, + }, + { + .binding = 1, + .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, + .descriptorCount = 1, + .stageFlags = VK_SHADER_STAGE_VERTEX_BIT, + }, + }; const VkDescriptorSetLayoutCreateInfo desc_set_layout_config = { .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, - .bindingCount = 1, - .pBindings = &(VkDescriptorSetLayoutBinding) { - .binding = 0, - .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, - .descriptorCount = 1, - .stageFlags = VK_SHADER_STAGE_VERTEX_BIT, - }, + .bindingCount = NELEMS(desc_set_layout_bindings), + .pBindings = desc_set_layout_bindings, }; result = vkCreateDescriptorSetLayout( ctx->dev, &desc_set_layout_config, nullptr, &ctx->desc_set_layout); @@ -429,9 +438,9 @@ void gpu_init(gpu_ctx_t *ctx, const gpu_config_t *config, model_t model) .poolSizeCount = 1, .pPoolSizes = &(VkDescriptorPoolSize) { .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, - .descriptorCount = swapchain_img_count, + .descriptorCount = 2 * swapchain_img_count, }, - .maxSets = swapchain_img_count, + .maxSets = 2 * swapchain_img_count, }; result = vkCreateDescriptorPool( ctx->dev, &desc_pool_config, nullptr, &ctx->desc_pool); @@ -474,22 +483,38 @@ void gpu_init(gpu_ctx_t *ctx, const gpu_config_t *config, model_t model) ctx->dev, ctx->bone_matrix_buffer_mem, 0, bone_matrix_buffer_size, 0, (void **)&ctx->bone_matrices); - // Configure descriptor sets to use MVP buffer + // Configure descriptor sets to use MVP and bone matrix buffers for (unsigned i = 0; i < swapchain_img_count; ++i) { - const VkWriteDescriptorSet desc_write = { - .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, - .dstSet = ctx->desc_sets[i], - .dstBinding = 0, - .dstArrayElement = 0, - .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, - .descriptorCount = 1, - .pBufferInfo = &(VkDescriptorBufferInfo) { - .buffer = ctx->mvp_buffer, - .offset = i * sizeof(mvp_t), - .range = sizeof(mvp_t), + const VkWriteDescriptorSet desc_writes[] = { + { + .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, + .dstSet = ctx->desc_sets[i], + .dstBinding = 0, + .dstArrayElement = 0, + .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, + .descriptorCount = 1, + .pBufferInfo = &(VkDescriptorBufferInfo) { + .buffer = ctx->mvp_buffer, + .offset = i * sizeof(mvp_t), + .range = sizeof(mvp_t), + }, + }, + { + .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, + .dstSet = ctx->desc_sets[i], + .dstBinding = 1, + .dstArrayElement = 0, + .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, + .descriptorCount = 1, + .pBufferInfo = &(VkDescriptorBufferInfo) { + .buffer = ctx->bone_matrix_buffer, + .offset = i * model.bone_count * sizeof(mat4_t), + .range = model.bone_count * sizeof(mat4_t), + }, }, }; - vkUpdateDescriptorSets(ctx->dev, 1, &desc_write, 0, nullptr); + vkUpdateDescriptorSets( + ctx->dev, NELEMS(desc_writes), desc_writes, 0, nullptr); } // Set up input configuration