Advertisement
Guest User

moltenvk example code

a guest
Nov 16th, 2019
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.56 KB | None | 0 0
  1. static void demo_draw(struct demo *demo) {
  2.     VkResult U_ASSERT_ONLY err;
  3.  
  4.     // Ensure no more than FRAME_LAG renderings are outstanding
  5.     vkWaitForFences(demo->device, 1, &demo->fences[demo->frame_index], VK_TRUE, UINT64_MAX);
  6.     vkResetFences(demo->device, 1, &demo->fences[demo->frame_index]);
  7.  
  8.     do {
  9.         // Get the index of the next available swapchain image:
  10.         err =
  11.             demo->fpAcquireNextImageKHR(demo->device, demo->swapchain, UINT64_MAX,
  12.                                         demo->image_acquired_semaphores[demo->frame_index], VK_NULL_HANDLE, &demo->current_buffer);
  13.  
  14.         if (err == VK_ERROR_OUT_OF_DATE_KHR) {
  15.             // demo->swapchain is out of date (e.g. the window was resized) and
  16.             // must be recreated:
  17.             demo_resize(demo);
  18.         } else if (err == VK_SUBOPTIMAL_KHR) {
  19.             // demo->swapchain is not as optimal as it could be, but the platform's
  20.             // presentation engine will still present the image correctly.
  21.             break;
  22.         } else {
  23.             assert(!err);
  24.         }
  25.     } while (err != VK_SUCCESS);
  26.  
  27.     demo_update_data_buffer(demo);
  28.  
  29.     if (demo->VK_GOOGLE_display_timing_enabled) {
  30.         // Look at what happened to previous presents, and make appropriate
  31.         // adjustments in timing:
  32.         DemoUpdateTargetIPD(demo);
  33.  
  34.         // Note: a real application would position its geometry to that it's in
  35.         // the correct locatoin for when the next image is presented.  It might
  36.         // also wait, so that there's less latency between any input and when
  37.         // the next image is rendered/presented.  This demo program is so
  38.         // simple that it doesn't do either of those.
  39.     }
  40.  
  41.     // Wait for the image acquired semaphore to be signaled to ensure
  42.     // that the image won't be rendered to until the presentation
  43.     // engine has fully released ownership to the application, and it is
  44.     // okay to render to the image.
  45.     VkPipelineStageFlags pipe_stage_flags;
  46.     VkSubmitInfo submit_info;
  47.     submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
  48.     submit_info.pNext = NULL;
  49.     submit_info.pWaitDstStageMask = &pipe_stage_flags;
  50.     pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
  51.     submit_info.waitSemaphoreCount = 1;
  52.     submit_info.pWaitSemaphores = &demo->image_acquired_semaphores[demo->frame_index];
  53.     submit_info.commandBufferCount = 1;
  54.     submit_info.pCommandBuffers = &demo->swapchain_image_resources[demo->current_buffer].cmd;
  55.     submit_info.signalSemaphoreCount = 1;
  56.     submit_info.pSignalSemaphores = &demo->draw_complete_semaphores[demo->frame_index];
  57.     err = vkQueueSubmit(demo->graphics_queue, 1, &submit_info, demo->fences[demo->frame_index]);
  58.     assert(!err);
  59.  
  60.     if (demo->separate_present_queue) {
  61.         // If we are using separate queues, change image ownership to the
  62.         // present queue before presenting, waiting for the draw complete
  63.         // semaphore and signalling the ownership released semaphore when finished
  64.         VkFence nullFence = VK_NULL_HANDLE;
  65.         pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
  66.         submit_info.waitSemaphoreCount = 1;
  67.         submit_info.pWaitSemaphores = &demo->draw_complete_semaphores[demo->frame_index];
  68.         submit_info.commandBufferCount = 1;
  69.         submit_info.pCommandBuffers = &demo->swapchain_image_resources[demo->current_buffer].graphics_to_present_cmd;
  70.         submit_info.signalSemaphoreCount = 1;
  71.         submit_info.pSignalSemaphores = &demo->image_ownership_semaphores[demo->frame_index];
  72.         err = vkQueueSubmit(demo->present_queue, 1, &submit_info, nullFence);
  73.         assert(!err);
  74.     }
  75.  
  76.     // If we are using separate queues we have to wait for image ownership,
  77.     // otherwise wait for draw complete
  78.     VkPresentInfoKHR present = {
  79.         .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
  80.         .pNext = NULL,
  81.         .waitSemaphoreCount = 1,
  82.         .pWaitSemaphores = (demo->separate_present_queue) ? &demo->image_ownership_semaphores[demo->frame_index]
  83.                                                           : &demo->draw_complete_semaphores[demo->frame_index],
  84.         .swapchainCount = 1,
  85.         .pSwapchains = &demo->swapchain,
  86.         .pImageIndices = &demo->current_buffer,
  87.     };
  88.  
  89.     VkRectLayerKHR rect;
  90.     VkPresentRegionKHR region;
  91.     VkPresentRegionsKHR regions;
  92.     if (demo->VK_KHR_incremental_present_enabled) {
  93.         // If using VK_KHR_incremental_present, we provide a hint of the region
  94.         // that contains changed content relative to the previously-presented
  95.         // image.  The implementation can use this hint in order to save
  96.         // work/power (by only copying the region in the hint).  The
  97.         // implementation is free to ignore the hint though, and so we must
  98.         // ensure that the entire image has the correctly-drawn content.
  99.         uint32_t eighthOfWidth = demo->width / 8;
  100.         uint32_t eighthOfHeight = demo->height / 8;
  101.  
  102.         rect.offset.x = eighthOfWidth;
  103.         rect.offset.y = eighthOfHeight;
  104.         rect.extent.width = eighthOfWidth * 6;
  105.         rect.extent.height = eighthOfHeight * 6;
  106.         rect.layer = 0;
  107.  
  108.         region.rectangleCount = 1;
  109.         region.pRectangles = ▭
  110.  
  111.         regions.sType = VK_STRUCTURE_TYPE_PRESENT_REGIONS_KHR;
  112.         regions.pNext = present.pNext;
  113.         regions.swapchainCount = present.swapchainCount;
  114.         regions.pRegions = &region;
  115.         present.pNext = &regions;
  116.     }
  117.  
  118.     if (demo->VK_GOOGLE_display_timing_enabled) {
  119.         VkPresentTimeGOOGLE ptime;
  120.         if (demo->prev_desired_present_time == 0) {
  121.             // This must be the first present for this swapchain.
  122.             //
  123.             // We don't know where we are relative to the presentation engine's
  124.             // display's refresh cycle.  We also don't know how long rendering
  125.             // takes.  Let's make a grossly-simplified assumption that the
  126.             // desiredPresentTime should be half way between now and
  127.             // now+target_IPD.  We will adjust over time.
  128.             uint64_t curtime = getTimeInNanoseconds();
  129.             if (curtime == 0) {
  130.                 // Since we didn't find out the current time, don't give a
  131.                 // desiredPresentTime:
  132.                 ptime.desiredPresentTime = 0;
  133.             } else {
  134.                 ptime.desiredPresentTime = curtime + (demo->target_IPD >> 1);
  135.             }
  136.         } else {
  137.             ptime.desiredPresentTime = (demo->prev_desired_present_time + demo->target_IPD);
  138.         }
  139.         ptime.presentID = demo->next_present_id++;
  140.         demo->prev_desired_present_time = ptime.desiredPresentTime;
  141.  
  142.         VkPresentTimesInfoGOOGLE present_time = {
  143.             .sType = VK_STRUCTURE_TYPE_PRESENT_TIMES_INFO_GOOGLE,
  144.             .pNext = present.pNext,
  145.             .swapchainCount = present.swapchainCount,
  146.             .pTimes = &ptime,
  147.         };
  148.         if (demo->VK_GOOGLE_display_timing_enabled) {
  149.             present.pNext = &present_time;
  150.         }
  151.     }
  152.  
  153.     err = demo->fpQueuePresentKHR(demo->present_queue, &present);
  154.     demo->frame_index += 1;
  155.     demo->frame_index %= FRAME_LAG;
  156.  
  157.     if (err == VK_ERROR_OUT_OF_DATE_KHR) {
  158.         // demo->swapchain is out of date (e.g. the window was resized) and
  159.         // must be recreated:
  160.         demo_resize(demo);
  161.     } else if (err == VK_SUBOPTIMAL_KHR) {
  162.         // demo->swapchain is not as optimal as it could be, but the platform's
  163.         // presentation engine will still present the image correctly.
  164.     } else {
  165.         assert(!err);
  166.     }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement