VulkanApplication.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. #ifndef VKW_SDLQT_VULKANAPPLICATION_H
  2. #define VKW_SDLQT_VULKANAPPLICATION_H
  3. #include "vulkan_include.h"
  4. #include <vector>
  5. #include <string>
  6. #include "Frame.h"
  7. namespace vkw
  8. {
  9. class Application
  10. {
  11. public:
  12. virtual ~Application()
  13. {
  14. }
  15. /**
  16. * @brief init
  17. * @param System
  18. *
  19. * This function will be called to initilize
  20. * the all the memory/objects you need.
  21. *
  22. * This should basically be used as your constructor.
  23. *
  24. * The swapchain may not have been created by this point
  25. *
  26. */
  27. virtual void initResources() = 0;
  28. /**
  29. * @brief releaseResources
  30. *
  31. * This is called when the vulkan application is about to be shut down
  32. * Use this to release all vulkan resources.
  33. */
  34. virtual void releaseResources() = 0;
  35. /**
  36. * @brief initSwapChainResources
  37. *
  38. *
  39. * This method is called whenever the swapchain changes its size.
  40. * we can use this method to allocate any offscreen render targets.
  41. * that might be dependent on the swapchain size.
  42. */
  43. virtual void initSwapChainResources() = 0;
  44. /**
  45. * @brief releaseSwapChainResources
  46. *
  47. * This method gets called whenever the swapchain has been
  48. * resized. This method will be called to release any
  49. * memory or resources which was allocated by a previous call to
  50. * initSwapChainResources()
  51. *
  52. * After this method is called, another call to initSwapChainResources()
  53. * will automatically be called.
  54. */
  55. virtual void releaseSwapChainResources() = 0;
  56. /**
  57. * @brief preRender
  58. *
  59. * Called prior to rendering the frame. You can use this method
  60. * to update any descriptor sets that will be used for the n
  61. * next frame.
  62. */
  63. virtual void preRender() {};
  64. /**
  65. * @brief render
  66. * @param frame
  67. *
  68. * The render() method is called at each frame and at
  69. * a rate determiend by the RenderSurface.
  70. *
  71. * frame contains the following information which you can use
  72. * : frame.
  73. *
  74. * frame.defaultRenderPass - the default render pass
  75. * frame.currentFrameBuffer - the current framebuffer in the render pass;
  76. * frame.currentCommandBuffer - the command buffer to be used to draw;
  77. * frame.swapChainImageSize - the extents of the swapchain;
  78. */
  79. virtual void render(Frame &frame) = 0;
  80. virtual void nativeWindowEvent(void const * e)
  81. {
  82. (void)e;
  83. }
  84. virtual void postRender() {};
  85. //=========================================================================
  86. void requestNextFrame()
  87. {
  88. renderNextFrame();
  89. }
  90. void renderNextFrame()
  91. {
  92. m_renderNextFrame=true;
  93. }
  94. bool shouldRender() const
  95. {
  96. return m_renderNextFrame;
  97. }
  98. //=========================================================================
  99. VkExtent2D swapchainImageSize() const
  100. {
  101. return m_swapChainSize;
  102. }
  103. VkFormat colorFormat() const
  104. {
  105. return m_swapChainFormat;
  106. }
  107. VkFormat depthStencilFormat() const
  108. {
  109. return m_swapChainDepthFormat;
  110. }
  111. uint32_t swapchainImageCount() const
  112. {
  113. return static_cast<uint32_t>(m_swapchainImages.size());
  114. }
  115. uint32_t currentSwapchainImageIndex() const
  116. {
  117. return m_currentSwapchainIndex;
  118. }
  119. VkImage swapchainImage(uint32_t indx) const
  120. {
  121. return m_swapchainImages.at(indx);
  122. }
  123. VkImageView swapchainImageView(uint32_t indx) const
  124. {
  125. return m_swapchainImageViews.at(indx);
  126. }
  127. VkDevice getDevice() const
  128. {
  129. return m_device;
  130. }
  131. VkPhysicalDevice getPhysicalDevice() const
  132. {
  133. return m_physicalDevice;
  134. }
  135. VkInstance getInstance() const
  136. {
  137. return m_instance;
  138. }
  139. VkQueue getGraphicsQueue() const
  140. {
  141. return m_graphicsQueue;
  142. }
  143. uint32_t getGraphicsQueueFamilyIndex() const
  144. {
  145. return static_cast<uint32_t>(m_graphicsQueueIndex);
  146. }
  147. //=========================================================================
  148. uint32_t concurrentFrameCount() const
  149. {
  150. return m_concurrentFrameCount;
  151. }
  152. VkRenderPass getDefaultRenderPass() const
  153. {
  154. return m_defaultRenderPass;
  155. }
  156. void quit()
  157. {
  158. m_quit=true;
  159. }
  160. bool shouldQuit() const
  161. {
  162. return m_quit;
  163. }
  164. uint64_t currentFrameNumber() const
  165. {
  166. return m_currentFrameNumber;
  167. }
  168. protected:
  169. friend class SDLVulkanWidget3;
  170. VkInstance m_instance;
  171. VkDevice m_device;
  172. VkPhysicalDevice m_physicalDevice;
  173. VkExtent2D m_swapChainSize;
  174. VkFormat m_swapChainFormat = VkFormat::VK_FORMAT_UNDEFINED;
  175. VkFormat m_swapChainDepthFormat= VkFormat::VK_FORMAT_UNDEFINED;
  176. uint32_t m_concurrentFrameCount=0;
  177. uint32_t m_currentSwapchainIndex=0;
  178. std::vector<VkImage> m_swapchainImages;
  179. std::vector<VkImageView> m_swapchainImageViews;
  180. VkRenderPass m_defaultRenderPass;
  181. bool m_quit=false;
  182. bool m_renderNextFrame=true;
  183. uint64_t m_currentFrameNumber=0;
  184. VkQueue m_graphicsQueue;
  185. VkQueue m_presentQueue;
  186. int32_t m_graphicsQueueIndex=-1;
  187. int32_t m_presentQueueIndex =-1;
  188. friend class QTVulkanWidget;
  189. friend class SDLVulkanWidget;
  190. friend class SDLVulkanWidget2;
  191. friend class QtVulkanWidget2;
  192. friend class SDLVulkanWidget3;
  193. friend class QTRenderer;
  194. friend class GLFWVulkanWidget;
  195. };
  196. }
  197. #endif