Chillzy

Untitled

Nov 6th, 2024
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.36 KB | Source Code | 0 0
  1. // These are initialized elsewhere as compute and graphics sync objects
  2. static VkSemaphoreCreateInfo vk_semaphore_info_init(void)
  3. {
  4.     VkSemaphoreCreateInfo semaphore_info =
  5.     {
  6.         .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
  7.         .pNext = NULL,
  8.         .flags = 0
  9.     };
  10.  
  11.     return semaphore_info;
  12. }
  13.  
  14. static VkFenceCreateInfo vk_fence_info_init(void)
  15. {
  16.     VkFenceCreateInfo fence_info =
  17.     {
  18.         .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
  19.         .pNext = NULL,
  20.         .flags = VK_FENCE_CREATE_SIGNALED_BIT
  21.     };
  22.  
  23.     return fence_info;
  24. }
  25.  
  26. VkSemaphore vk_semaphore_init(vk_device *d)
  27. {
  28.     VkSemaphore semaphore = {0};
  29.     VkSemaphoreCreateInfo semaphore_info = vk_semaphore_info_init();
  30.  
  31.     if (vkCreateSemaphore(d->device, &semaphore_info, NULL, &semaphore) != VK_SUCCESS)
  32.     {
  33.         printf("Failed to create semaphore\n");
  34.     }
  35.  
  36.     return semaphore;
  37. }
  38.  
  39. VkFence vk_fence_init(vk_device *d)
  40. {
  41.     VkFence fence = {0};
  42.     VkFenceCreateInfo fence_info = vk_fence_info_init();
  43.  
  44.     if (vkCreateFence(d->device, &fence_info, NULL, &fence) != VK_SUCCESS)
  45.     {
  46.         printf("Failed to create fence\n");
  47.     }
  48.  
  49.     return fence;
  50. }
  51.  
  52. static VkSubmitInfo vk_submit_info_init(VkSemaphore *img_available, uint32_t wait_count, VkPipelineStageFlags *stages, VkCommandBuffer *cmd_buffers, uint32_t cmd_buffer_count, VkSemaphore *render_finished, uint32_t signal_count)
  53. {
  54.     VkSubmitInfo submit_info =
  55.     {
  56.         .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
  57.         .pNext = NULL,
  58.         .waitSemaphoreCount = wait_count,
  59.         .pWaitSemaphores = img_available,
  60.         .pWaitDstStageMask = stages,
  61.         .commandBufferCount = cmd_buffer_count,
  62.         .pCommandBuffers = cmd_buffers,
  63.         .signalSemaphoreCount = signal_count,
  64.         .pSignalSemaphores = render_finished
  65.     };
  66.  
  67.     return submit_info;
  68. }
  69.  
  70. static VkPresentInfoKHR vk_present_info_init(vk_swapchain *s, vk_sync_obj *so, uint32_t *img_index)
  71. {
  72.     VkPresentInfoKHR present_info =
  73.     {
  74.         .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
  75.         .pNext = NULL,
  76.         .waitSemaphoreCount = 1,
  77.         .pWaitSemaphores = &so->render_finished[s->current_frame],
  78.         .swapchainCount = 1,
  79.         .pSwapchains = &s->swapchain,
  80.         .pImageIndices = img_index,
  81.         .pResults = NULL
  82.     };
  83.  
  84.     return present_info;
  85. }
  86.  
  87. void vk_present_img(vk_device *d, vk_swapchain *s, vk_pipeline *p, vk_sync_obj *cs, vk_sync_obj *gs, camera *c, vk_buffer *planet, vk_buffer *star, vk_buffer *ib, uint32_t draw_count, bool wireframe_enable)
  88. {
  89.     // Compute operations
  90.     VkResult result = VK_SUCCESS;
  91.  
  92.     if (vkWaitForFences(d->device, 1, &cs->in_flight[s->current_frame], VK_TRUE, UINT64_MAX) != VK_SUCCESS)
  93.     {
  94.         printf("Failed to wait for compute fences\n");
  95.     }
  96.  
  97.     if (vkResetFences(d->device, 1, &cs->in_flight[s->current_frame]) != VK_SUCCESS)
  98.     {
  99.         printf("Failed to reset compute fences\n");
  100.     }
  101.  
  102.     if (vkResetCommandBuffer(p->compute_cmds[s->current_frame], 0) != VK_SUCCESS)
  103.     {
  104.         printf("Failed to reset compute command buffer\n");
  105.     }
  106.  
  107.     vk_compute_cmds_record(p->compute, p->compute_layout, p->compute_cmds[s->current_frame], &p->compute_set.sets[s->current_frame]);
  108.  
  109.     VkSubmitInfo compute_info = vk_submit_info_init(NULL, 0, NULL, &p->compute_cmds[s->current_frame], 1, &cs->render_finished[s->current_frame], 1);
  110.  
  111.     if (vkQueueSubmit(d->queues.compute, 1, &compute_info, cs->in_flight[s->current_frame]) != VK_SUCCESS)
  112.     {
  113.         printf("Failed to submit compute queue\n");
  114.     }
  115.  
  116.     // Graphics operations
  117.     if (vkWaitForFences(d->device, 1, &gs->in_flight[s->current_frame], VK_TRUE, UINT64_MAX) != VK_SUCCESS)
  118.     {
  119.         printf("Failed to wait for graphics fences\n");
  120.     }
  121.  
  122.     uint32_t img_index = 0;
  123.     result = vkAcquireNextImageKHR(d->device, s->swapchain, UINT64_MAX, gs->img_available[s->current_frame], VK_NULL_HANDLE, &img_index);
  124.  
  125.     switch (result)
  126.     {
  127.         case VK_SUBOPTIMAL_KHR:
  128.         case VK_ERROR_OUT_OF_DATE_KHR:
  129.             // Another part of the code that's irrelevant
  130.             *s = vk_swapchain_recreation(d, p->render_pass);
  131.             break;
  132.  
  133.         case VK_SUCCESS:
  134.             break;
  135.  
  136.         default:
  137.             printf("Failed to acquire next swapchain image\n");
  138.             break;
  139.     }
  140.  
  141.     if (vkResetFences(d->device, 1, &gs->in_flight[s->current_frame]) != VK_SUCCESS)
  142.     {
  143.         printf("Failed to reset graphics fences\n");
  144.     }
  145.  
  146.     if (vkResetCommandBuffer(p->graphics_cmds[s->current_frame], 0) != VK_SUCCESS)
  147.     {
  148.         printf("Failed to reset graphics command buffer\n");
  149.     }
  150.  
  151.     vk_desc_set_update(&p->galaxy_set.camera[s->current_frame], c, s->img_extent);
  152.     vk_desc_set_update(&p->graphics_set.camera[s->current_frame], c, s->img_extent);
  153.  
  154.     vk_cmd_buffers_record(s, p->galaxy, p->galaxy_layout, &p->galaxy_set.sets[s->current_frame], p->render_pass, p->graphics_cmds[s->current_frame], img_index);
  155.  
  156.     VkSemaphore wait_semaphores[2] = {cs->render_finished[s->current_frame], gs->img_available[s->current_frame]};
  157.     VkPipelineStageFlags wait_stages[2] = {VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT};
  158.  
  159.     VkSubmitInfo graphics_info = vk_submit_info_init(wait_semaphores, 2, wait_stages, &p->graphics_cmds[s->current_frame], 1, &gs->render_finished[s->current_frame], 1);
  160.  
  161.     // All queues have been created successfully elsewhere
  162.     if (vkQueueSubmit(d->queues.graphics, 1, &graphics_info, gs->in_flight[s->current_frame]) != VK_SUCCESS)
  163.     {
  164.         printf("Failed to submit graphics queue\n");
  165.     }
  166.  
  167.     VkPresentInfoKHR present_info = vk_present_info_init(s, gs, &img_index);
  168.  
  169.     result = vkQueuePresentKHR(d->queues.present, &present_info);
  170.  
  171.     switch (result)
  172.     {
  173.         case VK_SUBOPTIMAL_KHR:
  174.         case VK_ERROR_OUT_OF_DATE_KHR:
  175.             *s = vk_swapchain_recreation(d, p->render_pass);
  176.             if (framebuffer_resized)
  177.                 framebuffer_resized = false;
  178.             break;
  179.  
  180.         case VK_SUCCESS:
  181.             break;
  182.  
  183.         default:
  184.             printf("Failed to present image\n");
  185.             break;
  186.     }
  187.  
  188.     //https://vulkan-tutorial.com/en/Drawing_a_triangle/Drawing/Frames_in_flight#comment-6378283909
  189.     s->current_frame += 1;
  190.     s->current_frame *= s->current_frame < MAX_FRAMES_IN_FLIGHT;
  191. }
Advertisement
Add Comment
Please, Sign In to add comment