GLFWWidget.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #ifndef VKW_GLFW_VULKAN_WIDGET3_H
  2. #define VKW_GLFW_VULKAN_WIDGET3_H
  3. #include "VKWVulkanWindow.h"
  4. #include "Adapters/GLFWVulkanWindowAdapter.h"
  5. #include "VulkanApplication.h"
  6. #include "Frame.h"
  7. #include <iostream>
  8. #include <thread>
  9. namespace vkw {
  10. /**
  11. * @brief The SDLVulkanWidget struct
  12. *
  13. * The SDLVulkanWidget use an SDL_Window to provide
  14. * a drawing surface. This mimics the QtVulkanWidget
  15. * so that they can be used interchangeably without
  16. * much modification.
  17. */
  18. class GLFWVulkanWidget : public VKWVulkanWindow
  19. {
  20. public:
  21. struct CreateInfo
  22. {
  23. std::string windowTitle;
  24. uint32_t width;
  25. uint32_t height;
  26. InstanceInitilizationInfo2 instanceInfo;
  27. DeviceInitilizationInfo2 deviceInfo;
  28. SurfaceInitilizationInfo2 surfaceInfo;
  29. };
  30. ~GLFWVulkanWidget()
  31. {
  32. }
  33. GLFWVulkanWindowAdapter * m_adapter = nullptr;
  34. CreateInfo m_createInfo;
  35. void create(CreateInfo const &C)
  36. {
  37. m_createInfo = C;
  38. m_adapter = new GLFWVulkanWindowAdapter();
  39. m_adapter->createWindow( m_createInfo.windowTitle.c_str(),
  40. static_cast<int>(m_createInfo.width),
  41. static_cast<int>(m_createInfo.height));
  42. setWindowAdapater(m_adapter);
  43. createVulkanInstance(m_createInfo.instanceInfo);
  44. createVulkanSurface(m_createInfo.surfaceInfo);
  45. createVulkanDevice(m_createInfo.deviceInfo);
  46. }
  47. void finalize(Application * app)
  48. {
  49. app->releaseSwapChainResources();
  50. app->releaseResources();
  51. }
  52. void render( Application * app)
  53. {
  54. auto fr = acquireNextFrame();
  55. fr.beginCommandBuffer();
  56. app->m_renderNextFrame = false;
  57. app->render(fr);
  58. fr.endCommandBuffer();
  59. frameReady(fr);
  60. }
  61. void _initSwapchainVars(Application * app)
  62. {
  63. app->m_swapChainSize = getSwapchainExtent();
  64. app->m_swapChainFormat = getSwapchainFormat();
  65. app->m_swapChainDepthFormat= getDepthFormat();
  66. app->m_concurrentFrameCount= static_cast<uint32_t>(m_swapchainFrameBuffers.size());
  67. app->m_defaultRenderPass = m_renderPass;
  68. app->m_swapchainImageViews = m_swapchainImageViews;
  69. app->m_swapchainImages = m_swapchainImages;
  70. app->m_currentSwapchainIndex=0;
  71. }
  72. int exec(Application * app)
  73. {
  74. return exec(app, [](){});
  75. }
  76. /**
  77. * @brief exec
  78. * @return
  79. *
  80. * Similar to Qt's app.exec(). this will
  81. * loop until the the windows is closed
  82. */
  83. template<typename SDL_MAIN_LOOP_CALLABLE>
  84. int exec(Application * app, SDL_MAIN_LOOP_CALLABLE && mainLoop)
  85. {
  86. app->m_device = getDevice();
  87. app->m_physicalDevice = getPhysicalDevice();
  88. app->m_instance = getInstance();
  89. app->m_graphicsQueue = getGraphicsQueue();
  90. app->m_presentQueue = getPresentQueue();
  91. app->m_graphicsQueueIndex = getGraphicsQueueIndex();
  92. app->m_presentQueueIndex = getPresentQueueIndex();
  93. _initSwapchainVars(app);
  94. app->initResources();
  95. app->initSwapChainResources();
  96. while( !glfwWindowShouldClose(m_adapter->m_window) )
  97. {
  98. glfwPollEvents();
  99. bool resize = m_adapter->requiresResize();
  100. if( app->shouldQuit() )
  101. {
  102. break;
  103. }
  104. if(resize)
  105. {
  106. app->releaseSwapChainResources();
  107. rebuildSwapchain();
  108. _initSwapchainVars(app);
  109. app->initSwapChainResources();
  110. m_adapter->clearRequireResize();
  111. }
  112. if( app->shouldRender() )
  113. {
  114. mainLoop();
  115. render(app);
  116. }
  117. }
  118. app->releaseSwapChainResources();
  119. app->releaseResources();
  120. destroy();
  121. return 0;
  122. }
  123. /**
  124. * @brief frameReady
  125. *
  126. * Call this function to present the frame.
  127. */
  128. void frameReady(Frame & fr)
  129. {
  130. submitFrame(fr);
  131. presentFrame(fr);
  132. waitForPresent();
  133. }
  134. };
  135. }
  136. #endif