2525constexpr bool bUseValidationLayers = true ;
2626
2727// chapter stage for refactors/changes
28- #define CHAPTER_STAGE 2
28+ #define CHAPTER_STAGE 3
2929
3030// we want to immediately abort when there is an error. In normal engines this would give an error message to the user, or perform a dump of state.
3131using namespace std ;
@@ -97,7 +97,7 @@ void VulkanEngine::cleanup()
9797 SDL_DestroyWindow (_window);
9898 }
9999}
100- void VulkanEngine::draw_main (VkCommandBuffer cmd)
100+ void VulkanEngine::draw_background (VkCommandBuffer cmd)
101101{
102102#if CHAPTER_STAGE == 0
103103// > draw_clear
@@ -120,7 +120,7 @@ void VulkanEngine::draw_main(VkCommandBuffer cmd)
120120 vkCmdBindDescriptorSets (cmd, VK_PIPELINE_BIND_POINT_COMPUTE, _gradientPipelineLayout, 0 , 1 , &_drawImageDescriptors, 0 , nullptr );
121121
122122 // execute the compute pipeline dispatch. We are using 16x16 workgroup size so we need to divide by it
123- vkCmdDispatch (cmd, std::ceil (_swapchainExtent .width / 16.0 ), std::ceil (_swapchainExtent .height / 16.0 ), 1 );
123+ vkCmdDispatch (cmd, std::ceil (_drawExtent .width / 16.0 ), std::ceil (_drawExtent .height / 16.0 ), 1 );
124124// < draw_comp
125125#elif CHAPTER_STAGE == 2
126126
@@ -208,7 +208,7 @@ void VulkanEngine::draw()
208208 // we will overwrite it all so we dont care about what was the older layout
209209 vkutil::transition_image (cmd, _drawImage.image , VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_GENERAL);
210210
211- draw_main (cmd);
211+ draw_background (cmd);
212212
213213 // transition the draw image and the swapchain image into their correct transfer layouts
214214 vkutil::transition_image (cmd, _drawImage.image , VK_IMAGE_LAYOUT_GENERAL, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
@@ -271,28 +271,35 @@ void VulkanEngine::run()
271271{
272272 SDL_Event e;
273273 bool bQuit = false ;
274- static bool skipDrawing = false ;
275274 // main loop
276275 while (!bQuit)
277276 {
278277 // Handle events on queue
279- while (SDL_PollEvent (&e) != 0 )
280- {
278+ while (SDL_PollEvent (&e) != 0 ) {
281279 // close the window when user alt-f4s or clicks the X button
282280 if (e.type == SDL_QUIT) bQuit = true ;
283- if (e. type == SDL_WINDOWEVENT_MINIMIZED) {
284- skipDrawing = true ;
285- }
286- if (e.type == SDL_WINDOWEVENT_RESTORED ) {
287- skipDrawing = false ;
288- }
289- if (e.type == SDL_WINDOWEVENT_RESIZED ) {
290-
291- rebuild_swapchain ();
281+
282+ if (e. type == SDL_WINDOWEVENT) {
283+
284+ if (e.window . event == SDL_WINDOWEVENT_MINIMIZED ) {
285+ stop_rendering = true ;
286+ }
287+ if (e.window . event == SDL_WINDOWEVENT_RESTORED ) {
288+ stop_rendering = false ;
289+ }
292290 }
291+
292+ // send SDL event to imgui for handling
293293 ImGui_ImplSDL2_ProcessEvent (&e);
294294 }
295295
296+ // do not draw if we are minimized
297+ if (stop_rendering) {
298+ // throttle the speed to avoid the endless spinning
299+ std::this_thread::sleep_for (std::chrono::milliseconds (100 ));
300+ continue ;
301+ }
302+
296303 // imgui new frame
297304 ImGui_ImplVulkan_NewFrame ();
298305 ImGui_ImplSDL2_NewFrame (_window);
@@ -318,12 +325,8 @@ void VulkanEngine::run()
318325 ImGui::Render ();
319326
320327// < imgui_bk
321- if (!skipDrawing) {
322- draw ();
323- }
324- else {
325- std::this_thread::sleep_for (std::chrono::milliseconds{100 });
326- }
328+
329+ draw ();
327330 }
328331}
329332
@@ -503,20 +506,22 @@ void VulkanEngine::init_swapchain()
503506 create_swapchain (_windowExtent.width , _windowExtent.height );
504507
505508// > init_swap
506- // depth image size will match the window
509+ // draw image size will match the window
507510 VkExtent3D drawImageExtent = {
508511 _windowExtent.width ,
509512 _windowExtent.height ,
510513 1
511514 };
512515
513- // hardcoding the depth format to 32 bit float
516+ // hardcoding the draw format to 32 bit float
514517 _drawImage.imageFormat = VK_FORMAT_R16G16B16A16_SFLOAT;
518+ _drawImage.imageExtent = drawImageExtent;
515519
516520 VkImageUsageFlags drawImageUsages{};
517521 drawImageUsages |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
518522 drawImageUsages |= VK_IMAGE_USAGE_TRANSFER_DST_BIT;
519523 drawImageUsages |= VK_IMAGE_USAGE_STORAGE_BIT;
524+ drawImageUsages |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
520525
521526 VkImageCreateInfo rimg_info = vkinit::image_create_info (_drawImage.imageFormat , drawImageUsages, drawImageExtent);
522527
@@ -560,7 +565,7 @@ void VulkanEngine::init_commands()
560565// > imm_cmd
561566 VK_CHECK (vkCreateCommandPool (_device, &commandPoolInfo, nullptr , &_immCommandPool));
562567
563- // allocate the default command buffer that we will use for rendering
568+ // allocate the command buffer for immediate submits
564569 VkCommandBufferAllocateInfo cmdAllocInfo = vkinit::command_buffer_allocate_info (_immCommandPool, 1 );
565570
566571 VK_CHECK (vkAllocateCommandBuffers (_device, &cmdAllocInfo, &_immCommandBuffer));
@@ -726,7 +731,7 @@ VK_CHECK(vkCreatePipelineLayout(_device, &computeLayout, nullptr, &_gradientPipe
726731 VkShaderModule computeDrawShader;
727732 if (!vkutil::load_shader_module (" ../../shaders/gradient.comp.spv" , _device, &computeDrawShader))
728733 {
729- std::cout << " Error when building the compute shader" << std::endl ;
734+ fmt::print ( " Error when building the compute shader \n " ) ;
730735 }
731736
732737 VkPipelineShaderStageCreateInfo stageinfo{};
@@ -757,7 +762,7 @@ VK_CHECK(vkCreatePipelineLayout(_device, &computeLayout, nullptr, &_gradientPipe
757762#elif CHAPTER_STAGE == 2
758763VkShaderModule computeDrawShader;
759764if (!vkutil::load_shader_module (" ../../shaders/gradient_color.comp.spv" , _device, &computeDrawShader)) {
760- std::cout << " Error when building the compute shader" << std::endl ;
765+ fmt::print ( " Error when building the compute shader \n " ) ;
761766}
762767
763768VkPipelineShaderStageCreateInfo stageinfo{};
@@ -785,12 +790,12 @@ _mainDeletionQueue.push_function([&]() {
785790// > comp_pipeline_multi
786791VkShaderModule gradientShader;
787792if (!vkutil::load_shader_module (" ../../shaders/gradient_color.comp.spv" , _device, &gradientShader)) {
788- std::cout << " Error when building the compute shader" << std::endl ;
793+ fmt::print ( " Error when building the compute shader \n " ) ;
789794}
790795
791796VkShaderModule skyShader;
792797if (!vkutil::load_shader_module (" ../../shaders/sky.comp.spv" , _device, &skyShader)) {
793- std::cout << " Error when building the compute shader" << std::endl ;
798+ fmt::print ( " Error when building the compute shader \n " ) ;
794799}
795800
796801VkPipelineShaderStageCreateInfo stageinfo{};
0 commit comments