VulkanWrapper.hpp 5.6 KB

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