1 #include <kiba/renderer/vulkan/command_buffer.h>
3 b8 vulkan_command_buffer_record(
vulkan_context *context, VkCommandBuffer command_buffer, u32 image_index) {
4 VkCommandBufferBeginInfo cb_begin_info = {
5 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
7 .pInheritanceInfo = NULL,
9 VK_CALL_B8(vkBeginCommandBuffer(command_buffer, &cb_begin_info));
11 VkFramebuffer fb = context->swap_chain.framebuffers[image_index];
12 VkClearValue clear_colors[] = {
13 {.color = {.float32 = {0.0f, 0.0f, 0.0f, 1.0f}}},
14 {.depthStencil = {.depth = 1.f, .stencil = 0}},
16 VkRenderPassBeginInfo rp_begin_info = {
17 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
18 .renderPass = context->pipeline.renderpass,
20 .renderArea = {.offset = {0, 0}, .extent = context->swap_chain.extent},
21 .clearValueCount =
sizeof(clear_colors) /
sizeof(clear_colors[0]),
22 .pClearValues = clear_colors,
24 vkCmdBeginRenderPass(command_buffer, &rp_begin_info, VK_SUBPASS_CONTENTS_INLINE);
26 vkCmdBindPipeline(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, context->pipeline.graphics_pipeline);
28 VkViewport viewport = {
31 .width = (f32) context->swap_chain.extent.width,
32 .height = (f32) context->swap_chain.extent.height,
36 vkCmdSetViewport(command_buffer, 0, 1, &viewport);
40 .extent = context->swap_chain.extent,
42 vkCmdSetScissor(command_buffer, 0, 1, &scissor);
45 VkDeviceSize offset = 0;
46 vkCmdBindVertexBuffers(command_buffer, 0, 1, &context->vertex_data.buffer, &offset);
49 vkCmdBindIndexBuffer(command_buffer, context->index_data.buffer, offset, VK_INDEX_TYPE_UINT32);
52 vkCmdBindDescriptorSets(command_buffer,
53 VK_PIPELINE_BIND_POINT_GRAPHICS,
54 context->pipeline.layout,
57 &context->pipeline.descriptor_sets[image_index],
61 vkCmdDrawIndexed(command_buffer, context->index_count, 1, 0, 0, 0);
63 vkCmdEndRenderPass(command_buffer);
65 VK_CALL_B8(vkEndCommandBuffer(command_buffer));
69 b8 vulkan_command_buffer_start_single_time_command(
vulkan_context *context, VkCommandBuffer *command_buffer) {
70 VkCommandBufferAllocateInfo alloc_info = {
71 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
72 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
73 .commandPool = context->device.graphics_queue.command_pool,
74 .commandBufferCount = 1,
76 VK_CALL_B8(vkAllocateCommandBuffers(context->device.logical, &alloc_info, command_buffer));
78 VkCommandBufferBeginInfo begin_info = {
79 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
80 .flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT,
82 VK_CALL_B8(vkBeginCommandBuffer(*command_buffer, &begin_info));
86 b8 vulkan_command_buffer_end_single_time_command(
vulkan_context *context, VkCommandBuffer *command_buffer) {
87 VK_CALL_B8(vkEndCommandBuffer(*command_buffer));
89 VkSubmitInfo submit_info = {
90 .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
91 .commandBufferCount = 1,
92 .pCommandBuffers = command_buffer,
94 VK_CALL_B8(vkQueueSubmit(context->device.graphics_queue.queue, 1, &submit_info, VK_NULL_HANDLE));
95 VK_CALL_B8(vkQueueWaitIdle(context->device.graphics_queue.queue));
97 vkFreeCommandBuffers(context->device.logical, context->device.graphics_queue.command_pool, 1, command_buffer);
#define KB_NULL
Value of an invalid ptr (nullptr).