VulkanWrapper.hpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. bool init(const Swapchain& s, VkFormat format);
  99. };
  100. struct RenderPass : public BaseWrapper<VkRenderPass> {
  101. RenderPass() = default;
  102. RenderPass(RenderPass&&) = default;
  103. ~RenderPass();
  104. bool init(VkDevice d, VkFormat format);
  105. };
  106. struct Pipeline : public BaseWrapper<VkPipeline> {
  107. VkViewport viewport;
  108. VkRect2D scissor;
  109. Pipeline();
  110. Pipeline(Pipeline&&) = default;
  111. ~Pipeline();
  112. void updateSize(u32 width, u32 height);
  113. bool init(
  114. PipelineLayout& pl, RenderPass& rp,
  115. const List<VkVertexInputBindingDescription>& bd,
  116. const List<VkVertexInputAttributeDescription>& ad);
  117. };
  118. struct CommandPool : public BaseWrapper<VkCommandPool> {
  119. CommandPool() = default;
  120. CommandPool(CommandPool&&) = default;
  121. ~CommandPool();
  122. bool init(VkDevice d, u32 queueFamily);
  123. };
  124. struct CommandBuffer : public BaseWrapper<VkCommandBuffer> {
  125. CommandBuffer() = default;
  126. CommandBuffer(CommandBuffer&&) = default;
  127. bool init(CommandPool& cp);
  128. void reset();
  129. void begin();
  130. void end();
  131. void beginRenderPass(
  132. RenderPass& rp, Framebuffer& f, const VkExtent2D& size);
  133. void endRenderPass();
  134. void bindPipeline(Pipeline& p);
  135. void draw(u32 vertices);
  136. };
  137. struct Queue : public BaseWrapper<VkQueue> {
  138. Queue() = default;
  139. Queue(Queue&&) = default;
  140. bool init(VkDevice d, u32 queueFamily);
  141. void submit(
  142. CommandBuffer& cb, Fence& f, Semaphore& wait, Semaphore& signal,
  143. VkPipelineStageFlags flags);
  144. SwapchainResult present(Semaphore& signal, Swapchain& s, u32 index);
  145. };
  146. struct VertexBuffer : public BaseWrapper<VkBuffer> {
  147. VkDeviceMemory memory;
  148. VertexBuffer();
  149. VertexBuffer(const VertexBuffer&) = delete;
  150. VertexBuffer(VertexBuffer&&) = default;
  151. ~VertexBuffer();
  152. VertexBuffer& operator=(const VertexBuffer&) = delete;
  153. bool init(Base& base, size_t size, const void* data);
  154. };
  155. }
  156. #endif