Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // These are initialized elsewhere as compute and graphics sync objects
- static VkSemaphoreCreateInfo vk_semaphore_info_init(void)
- {
- VkSemaphoreCreateInfo semaphore_info =
- {
- .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
- .pNext = NULL,
- .flags = 0
- };
- return semaphore_info;
- }
- static VkFenceCreateInfo vk_fence_info_init(void)
- {
- VkFenceCreateInfo fence_info =
- {
- .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
- .pNext = NULL,
- .flags = VK_FENCE_CREATE_SIGNALED_BIT
- };
- return fence_info;
- }
- VkSemaphore vk_semaphore_init(vk_device *d)
- {
- VkSemaphore semaphore = {0};
- VkSemaphoreCreateInfo semaphore_info = vk_semaphore_info_init();
- if (vkCreateSemaphore(d->device, &semaphore_info, NULL, &semaphore) != VK_SUCCESS)
- {
- printf("Failed to create semaphore\n");
- }
- return semaphore;
- }
- VkFence vk_fence_init(vk_device *d)
- {
- VkFence fence = {0};
- VkFenceCreateInfo fence_info = vk_fence_info_init();
- if (vkCreateFence(d->device, &fence_info, NULL, &fence) != VK_SUCCESS)
- {
- printf("Failed to create fence\n");
- }
- return fence;
- }
- 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)
- {
- VkSubmitInfo submit_info =
- {
- .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
- .pNext = NULL,
- .waitSemaphoreCount = wait_count,
- .pWaitSemaphores = img_available,
- .pWaitDstStageMask = stages,
- .commandBufferCount = cmd_buffer_count,
- .pCommandBuffers = cmd_buffers,
- .signalSemaphoreCount = signal_count,
- .pSignalSemaphores = render_finished
- };
- return submit_info;
- }
- static VkPresentInfoKHR vk_present_info_init(vk_swapchain *s, vk_sync_obj *so, uint32_t *img_index)
- {
- VkPresentInfoKHR present_info =
- {
- .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
- .pNext = NULL,
- .waitSemaphoreCount = 1,
- .pWaitSemaphores = &so->render_finished[s->current_frame],
- .swapchainCount = 1,
- .pSwapchains = &s->swapchain,
- .pImageIndices = img_index,
- .pResults = NULL
- };
- return present_info;
- }
- 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)
- {
- // Compute operations
- VkResult result = VK_SUCCESS;
- if (vkWaitForFences(d->device, 1, &cs->in_flight[s->current_frame], VK_TRUE, UINT64_MAX) != VK_SUCCESS)
- {
- printf("Failed to wait for compute fences\n");
- }
- if (vkResetFences(d->device, 1, &cs->in_flight[s->current_frame]) != VK_SUCCESS)
- {
- printf("Failed to reset compute fences\n");
- }
- if (vkResetCommandBuffer(p->compute_cmds[s->current_frame], 0) != VK_SUCCESS)
- {
- printf("Failed to reset compute command buffer\n");
- }
- vk_compute_cmds_record(p->compute, p->compute_layout, p->compute_cmds[s->current_frame], &p->compute_set.sets[s->current_frame]);
- VkSubmitInfo compute_info = vk_submit_info_init(NULL, 0, NULL, &p->compute_cmds[s->current_frame], 1, &cs->render_finished[s->current_frame], 1);
- if (vkQueueSubmit(d->queues.compute, 1, &compute_info, cs->in_flight[s->current_frame]) != VK_SUCCESS)
- {
- printf("Failed to submit compute queue\n");
- }
- // Graphics operations
- if (vkWaitForFences(d->device, 1, &gs->in_flight[s->current_frame], VK_TRUE, UINT64_MAX) != VK_SUCCESS)
- {
- printf("Failed to wait for graphics fences\n");
- }
- uint32_t img_index = 0;
- result = vkAcquireNextImageKHR(d->device, s->swapchain, UINT64_MAX, gs->img_available[s->current_frame], VK_NULL_HANDLE, &img_index);
- switch (result)
- {
- case VK_SUBOPTIMAL_KHR:
- case VK_ERROR_OUT_OF_DATE_KHR:
- // Another part of the code that's irrelevant
- *s = vk_swapchain_recreation(d, p->render_pass);
- break;
- case VK_SUCCESS:
- break;
- default:
- printf("Failed to acquire next swapchain image\n");
- break;
- }
- if (vkResetFences(d->device, 1, &gs->in_flight[s->current_frame]) != VK_SUCCESS)
- {
- printf("Failed to reset graphics fences\n");
- }
- if (vkResetCommandBuffer(p->graphics_cmds[s->current_frame], 0) != VK_SUCCESS)
- {
- printf("Failed to reset graphics command buffer\n");
- }
- vk_desc_set_update(&p->galaxy_set.camera[s->current_frame], c, s->img_extent);
- vk_desc_set_update(&p->graphics_set.camera[s->current_frame], c, s->img_extent);
- 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);
- VkSemaphore wait_semaphores[2] = {cs->render_finished[s->current_frame], gs->img_available[s->current_frame]};
- VkPipelineStageFlags wait_stages[2] = {VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT};
- 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);
- // All queues have been created successfully elsewhere
- if (vkQueueSubmit(d->queues.graphics, 1, &graphics_info, gs->in_flight[s->current_frame]) != VK_SUCCESS)
- {
- printf("Failed to submit graphics queue\n");
- }
- VkPresentInfoKHR present_info = vk_present_info_init(s, gs, &img_index);
- result = vkQueuePresentKHR(d->queues.present, &present_info);
- switch (result)
- {
- case VK_SUBOPTIMAL_KHR:
- case VK_ERROR_OUT_OF_DATE_KHR:
- *s = vk_swapchain_recreation(d, p->render_pass);
- if (framebuffer_resized)
- framebuffer_resized = false;
- break;
- case VK_SUCCESS:
- break;
- default:
- printf("Failed to present image\n");
- break;
- }
- //https://vulkan-tutorial.com/en/Drawing_a_triangle/Drawing/Frames_in_flight#comment-6378283909
- s->current_frame += 1;
- s->current_frame *= s->current_frame < MAX_FRAMES_IN_FLIGHT;
- }
Advertisement
Add Comment
Please, Sign In to add comment