VulkanWrapper.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #ifndef GAMINGCORE_VULKAN_WRAPPER_HPP
  2. #define GAMINGCORE_VULKAN_WRAPPER_HPP
  3. #include <core/List.hpp>
  4. #include <core/Meta.hpp>
  5. #include <core/Types.hpp>
  6. #include <vulkan/vulkan.h>
  7. #include "core/VulkanBase.hpp"
  8. namespace Core::Vulkan {
  9. bool init();
  10. void render();
  11. void destroy();
  12. template<typename T>
  13. struct BaseWrapper {
  14. VkDevice device = VK_NULL_HANDLE;
  15. T handle = VK_NULL_HANDLE;
  16. BaseWrapper() = default;
  17. BaseWrapper(const BaseWrapper&) = delete;
  18. BaseWrapper(BaseWrapper&& o) : BaseWrapper() {
  19. swap(o);
  20. }
  21. BaseWrapper& operator=(const BaseWrapper&) = delete;
  22. BaseWrapper& operator=(BaseWrapper&& o) {
  23. swap(o);
  24. o = BaseWrapper();
  25. }
  26. void swap(BaseWrapper& o) {
  27. Core::swap(device, o.device);
  28. Core::swap(handle, o.handle);
  29. }
  30. operator T() {
  31. return handle;
  32. }
  33. operator T*() {
  34. return &handle;
  35. }
  36. operator const T*() const {
  37. return &handle;
  38. }
  39. };
  40. struct ImageView : public BaseWrapper<VkImageView> {
  41. ImageView() = default;
  42. ImageView(ImageView&&) = default;
  43. ~ImageView();
  44. bool init(VkDevice d, VkImage image, VkFormat format);
  45. };
  46. struct Framebuffer : public BaseWrapper<VkFramebuffer> {
  47. Framebuffer() = default;
  48. Framebuffer(Framebuffer&&) = default;
  49. ~Framebuffer();
  50. bool init(const ImageView& iv, VkRenderPass rp, u32 width, u32 height);
  51. };
  52. struct Semaphore : public BaseWrapper<VkSemaphore> {
  53. Semaphore() = default;
  54. Semaphore(Semaphore&&) = default;
  55. ~Semaphore();
  56. bool init(VkDevice d);
  57. };
  58. struct Fence : public BaseWrapper<VkFence> {
  59. Fence() = default;
  60. Fence(Fence&&) = default;
  61. ~Fence();
  62. bool init(VkDevice d);
  63. void reset();
  64. void waitFor(u64 timeout = UINT64_MAX);
  65. };
  66. struct Swapchain : public BaseWrapper<VkSwapchainKHR> {
  67. Swapchain() = default;
  68. Swapchain(Swapchain&&) = default;
  69. ~Swapchain();
  70. struct Data {
  71. Base& base;
  72. VkExtent2D size{};
  73. VkSurfaceFormatKHR surfaceFormat{};
  74. VkPresentModeKHR presentMode{};
  75. VkSharingMode sharingMode{};
  76. List<u32> queueFamilies{};
  77. };
  78. bool init(Data& d);
  79. bool nextImage(u32& imageIndex, Semaphore& s, u64 timeout = UINT64_MAX);
  80. };
  81. struct ShaderModule : public BaseWrapper<VkShaderModule> {
  82. ShaderModule() = default;
  83. ShaderModule(ShaderModule&&) = default;
  84. ~ShaderModule();
  85. bool init(VkDevice d, const char* path);
  86. };
  87. struct PipelineLayout : public BaseWrapper<VkPipelineLayout> {
  88. PipelineLayout() = default;
  89. PipelineLayout(PipelineLayout&&) = default;
  90. ~PipelineLayout();
  91. bool init(VkDevice d);
  92. };
  93. struct SwapchainImages {
  94. List<VkImage> images{};
  95. List<ImageView> imageViews{};
  96. bool init(const Swapchain& s, VkFormat format);
  97. };
  98. struct RenderPass : public BaseWrapper<VkRenderPass> {
  99. RenderPass() = default;
  100. RenderPass(RenderPass&&) = default;
  101. ~RenderPass();
  102. bool init(VkDevice d, VkFormat format);
  103. };
  104. struct Pipeline : public BaseWrapper<VkPipeline> {
  105. VkViewport viewport;
  106. VkRect2D scissor;
  107. Pipeline();
  108. Pipeline(Pipeline&&) = default;
  109. ~Pipeline();
  110. void updateSize(u32 width, u32 height);
  111. bool init(PipelineLayout& pl, RenderPass& rp);
  112. };
  113. struct CommandPool : public BaseWrapper<VkCommandPool> {
  114. CommandPool() = default;
  115. CommandPool(CommandPool&&) = default;
  116. ~CommandPool();
  117. bool init(VkDevice d, u32 queueFamily);
  118. };
  119. struct CommandBuffer : public BaseWrapper<VkCommandBuffer> {
  120. CommandBuffer() = default;
  121. CommandBuffer(CommandBuffer&&) = default;
  122. bool init(CommandPool& cp);
  123. void reset();
  124. void begin();
  125. void end();
  126. void beginRenderPass(
  127. RenderPass& rp, Framebuffer& f, const VkExtent2D& size);
  128. void endRenderPass();
  129. void bindPipeline(Pipeline& p);
  130. void draw(u32 vertices);
  131. };
  132. struct Queue : public BaseWrapper<VkQueue> {
  133. Queue() = default;
  134. Queue(Queue&&) = default;
  135. bool init(VkDevice d, u32 queueFamily);
  136. void submit(
  137. CommandBuffer& cb, Fence& f, Semaphore& wait, Semaphore& signal,
  138. VkPipelineStageFlags flags);
  139. void present(Semaphore& signal, Swapchain& s, u32 index);
  140. };
  141. }
  142. #endif