VulkanWrapper.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. return *this;
  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. enum class SwapchainResult { SUCCESS, RECREATE, ERROR_RECREATE, ERROR };
  67. struct Swapchain : public BaseWrapper<VkSwapchainKHR> {
  68. Swapchain() = default;
  69. Swapchain(Swapchain&&) = default;
  70. ~Swapchain();
  71. struct Data {
  72. Base& base;
  73. VkExtent2D size{};
  74. VkSurfaceFormatKHR surfaceFormat{};
  75. VkPresentModeKHR presentMode{};
  76. VkSharingMode sharingMode{};
  77. List<u32> queueFamilies{};
  78. };
  79. bool init(Data& d);
  80. SwapchainResult nextImage(
  81. u32& imageIndex, Semaphore& s, u64 timeout = UINT64_MAX);
  82. };
  83. struct ShaderModule : public BaseWrapper<VkShaderModule> {
  84. ShaderModule() = default;
  85. ShaderModule(ShaderModule&&) = default;
  86. ~ShaderModule();
  87. bool init(VkDevice d, const char* path);
  88. };
  89. struct PipelineLayout : public BaseWrapper<VkPipelineLayout> {
  90. PipelineLayout() = default;
  91. PipelineLayout(PipelineLayout&&) = default;
  92. ~PipelineLayout();
  93. bool init(VkDevice d);
  94. };
  95. struct SwapchainImages {
  96. List<VkImage> images{};
  97. List<ImageView> imageViews{};
  98. List<Semaphore> renderFinishedSemaphore{};
  99. bool init(const Swapchain& s, VkFormat format);
  100. };
  101. struct RenderPass : public BaseWrapper<VkRenderPass> {
  102. RenderPass() = default;
  103. RenderPass(RenderPass&&) = default;
  104. ~RenderPass();
  105. bool init(VkDevice d, VkFormat format);
  106. };
  107. struct Pipeline : public BaseWrapper<VkPipeline> {
  108. VkViewport viewport;
  109. VkRect2D scissor;
  110. Pipeline();
  111. Pipeline(Pipeline&&) = default;
  112. ~Pipeline();
  113. void updateSize(u32 width, u32 height);
  114. bool init(
  115. PipelineLayout& pl, RenderPass& rp,
  116. const List<VkVertexInputBindingDescription>& bd,
  117. const List<VkVertexInputAttributeDescription>& ad);
  118. };
  119. struct CommandPool : public BaseWrapper<VkCommandPool> {
  120. CommandPool() = default;
  121. CommandPool(CommandPool&&) = default;
  122. ~CommandPool();
  123. bool init(VkDevice d, u32 queueFamily);
  124. };
  125. struct CommandBuffer : public BaseWrapper<VkCommandBuffer> {
  126. CommandBuffer() = default;
  127. CommandBuffer(CommandBuffer&&) = default;
  128. bool init(CommandPool& cp);
  129. void reset();
  130. void begin();
  131. void end();
  132. void beginRenderPass(
  133. RenderPass& rp, Framebuffer& f, const VkExtent2D& size);
  134. void endRenderPass();
  135. void bindPipeline(Pipeline& p);
  136. void draw(u32 vertices);
  137. };
  138. struct Queue : public BaseWrapper<VkQueue> {
  139. Queue() = default;
  140. Queue(Queue&&) = default;
  141. bool init(VkDevice d, u32 queueFamily);
  142. void submit(
  143. CommandBuffer& cb, Fence& f, Semaphore& wait, Semaphore& signal,
  144. VkPipelineStageFlags flags);
  145. SwapchainResult present(Semaphore& signal, Swapchain& s, u32 index);
  146. };
  147. struct VertexBuffer : public BaseWrapper<VkBuffer> {
  148. VkDeviceMemory memory;
  149. VertexBuffer();
  150. VertexBuffer(const VertexBuffer&) = delete;
  151. VertexBuffer(VertexBuffer&&) = default;
  152. ~VertexBuffer();
  153. VertexBuffer& operator=(const VertexBuffer&) = delete;
  154. bool init(Base& base, size_t size, const void* data);
  155. };
  156. }
  157. #endif