123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- #ifndef GAMINGCORE_VULKAN_WRAPPER_HPP
- #define GAMINGCORE_VULKAN_WRAPPER_HPP
- #include <core/List.hpp>
- #include <core/Meta.hpp>
- #include <core/Types.hpp>
- #include <vulkan/vulkan.h>
- #include "core/VulkanBase.hpp"
- namespace Core::Vulkan {
- bool init();
- void render();
- void destroy();
- template<typename T>
- struct BaseWrapper {
- VkDevice device = VK_NULL_HANDLE;
- T handle = VK_NULL_HANDLE;
- BaseWrapper() = default;
- BaseWrapper(const BaseWrapper&) = delete;
- BaseWrapper(BaseWrapper&& o) : BaseWrapper() {
- swap(o);
- }
- BaseWrapper& operator=(const BaseWrapper&) = delete;
- BaseWrapper& operator=(BaseWrapper&& o) {
- swap(o);
- return *this;
- }
- void swap(BaseWrapper& o) {
- Core::swap(device, o.device);
- Core::swap(handle, o.handle);
- }
- operator T() {
- return handle;
- }
- operator T*() {
- return &handle;
- }
- operator const T*() const {
- return &handle;
- }
- };
- struct ImageView : public BaseWrapper<VkImageView> {
- ImageView() = default;
- ImageView(ImageView&&) = default;
- ~ImageView();
- bool init(VkDevice d, VkImage image, VkFormat format);
- };
- struct Framebuffer : public BaseWrapper<VkFramebuffer> {
- Framebuffer() = default;
- Framebuffer(Framebuffer&&) = default;
- ~Framebuffer();
- bool init(const ImageView& iv, VkRenderPass rp, u32 width, u32 height);
- };
- struct Semaphore : public BaseWrapper<VkSemaphore> {
- Semaphore() = default;
- Semaphore(Semaphore&&) = default;
- ~Semaphore();
- bool init(VkDevice d);
- };
- struct Fence : public BaseWrapper<VkFence> {
- Fence() = default;
- Fence(Fence&&) = default;
- ~Fence();
- bool init(VkDevice d);
- void reset();
- void waitFor(u64 timeout = UINT64_MAX);
- };
- enum class SwapchainResult { SUCCESS, RECREATE, ERROR_RECREATE, ERROR };
- struct Swapchain : public BaseWrapper<VkSwapchainKHR> {
- Swapchain() = default;
- Swapchain(Swapchain&&) = default;
- ~Swapchain();
- struct Data {
- Base& base;
- VkExtent2D size{};
- VkSurfaceFormatKHR surfaceFormat{};
- VkPresentModeKHR presentMode{};
- VkSharingMode sharingMode{};
- List<u32> queueFamilies{};
- };
- bool init(Data& d);
- SwapchainResult nextImage(
- u32& imageIndex, Semaphore& s, u64 timeout = UINT64_MAX);
- };
- struct ShaderModule : public BaseWrapper<VkShaderModule> {
- ShaderModule() = default;
- ShaderModule(ShaderModule&&) = default;
- ~ShaderModule();
- bool init(VkDevice d, const char* path);
- };
- struct PipelineLayout : public BaseWrapper<VkPipelineLayout> {
- PipelineLayout() = default;
- PipelineLayout(PipelineLayout&&) = default;
- ~PipelineLayout();
- bool init(VkDevice d);
- };
- struct SwapchainImages {
- List<VkImage> images{};
- List<ImageView> imageViews{};
- bool init(const Swapchain& s, VkFormat format);
- };
- struct RenderPass : public BaseWrapper<VkRenderPass> {
- RenderPass() = default;
- RenderPass(RenderPass&&) = default;
- ~RenderPass();
- bool init(VkDevice d, VkFormat format);
- };
- struct Pipeline : public BaseWrapper<VkPipeline> {
- VkViewport viewport;
- VkRect2D scissor;
- Pipeline();
- Pipeline(Pipeline&&) = default;
- ~Pipeline();
- void updateSize(u32 width, u32 height);
- bool init(
- PipelineLayout& pl, RenderPass& rp,
- const List<VkVertexInputBindingDescription>& bd,
- const List<VkVertexInputAttributeDescription>& ad);
- };
- struct CommandPool : public BaseWrapper<VkCommandPool> {
- CommandPool() = default;
- CommandPool(CommandPool&&) = default;
- ~CommandPool();
- bool init(VkDevice d, u32 queueFamily);
- };
- struct CommandBuffer : public BaseWrapper<VkCommandBuffer> {
- CommandBuffer() = default;
- CommandBuffer(CommandBuffer&&) = default;
- bool init(CommandPool& cp);
- void reset();
- void begin();
- void end();
- void beginRenderPass(
- RenderPass& rp, Framebuffer& f, const VkExtent2D& size);
- void endRenderPass();
- void bindPipeline(Pipeline& p);
- void draw(u32 vertices);
- };
- struct Queue : public BaseWrapper<VkQueue> {
- Queue() = default;
- Queue(Queue&&) = default;
- bool init(VkDevice d, u32 queueFamily);
- void submit(
- CommandBuffer& cb, Fence& f, Semaphore& wait, Semaphore& signal,
- VkPipelineStageFlags flags);
- SwapchainResult present(Semaphore& signal, Swapchain& s, u32 index);
- };
- struct VertexBuffer : public BaseWrapper<VkBuffer> {
- VkDeviceMemory memory;
- VertexBuffer();
- VertexBuffer(const VertexBuffer&) = delete;
- VertexBuffer(VertexBuffer&&) = default;
- ~VertexBuffer();
- VertexBuffer& operator=(const VertexBuffer&) = delete;
- bool init(Base& base, size_t size, const void* data);
- };
- }
- #endif
|