/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* renderer.h :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/12/18 17:14:45 by maldavid #+# #+# */ /* Updated: 2023/03/31 17:59:09 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef __RENDERER__ #define __RENDERER__ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace mlx { struct Vertex { glm::vec2 pos; glm::vec3 color; glm::vec2 uv; static VkVertexInputBindingDescription getBindingDescription() { VkVertexInputBindingDescription bindingDescription{}; bindingDescription.binding = 0; bindingDescription.stride = sizeof(Vertex); bindingDescription.inputRate = VK_VERTEX_INPUT_RATE_VERTEX; return bindingDescription; } static std::array getAttributeDescriptions() { std::array attributeDescriptions; attributeDescriptions[0].binding = 0; attributeDescriptions[0].location = 0; attributeDescriptions[0].format = VK_FORMAT_R32G32_SFLOAT; attributeDescriptions[0].offset = offsetof(Vertex, pos); attributeDescriptions[1].binding = 0; attributeDescriptions[1].location = 1; attributeDescriptions[1].format = VK_FORMAT_R32G32B32_SFLOAT; attributeDescriptions[1].offset = offsetof(Vertex, color); attributeDescriptions[2].binding = 0; attributeDescriptions[2].location = 2; attributeDescriptions[2].format = VK_FORMAT_R32G32_SFLOAT; attributeDescriptions[2].offset = offsetof(Vertex, uv); return attributeDescriptions; } }; class Renderer { public: Renderer() = default; void init(); bool beginFrame(); void endFrame(); void destroy(); inline class MLX_Window* getWindow() { return _window; } inline void setWindow(class MLX_Window* window) { _window = window; } inline Surface& getSurface() noexcept { return _surface; } inline CmdPool& getCmdPool() noexcept { return _cmd_pool; } inline UBO* getUniformBuffer() noexcept { return _uniform_buffer.get(); } inline SwapChain& getSwapChain() noexcept { return _swapchain; } inline Semaphore& getSemaphore() noexcept { return _semaphore; } inline RenderPass& getRenderPass() noexcept { return _pass; } inline CmdBuffer& getCmdBuffer(int i) noexcept { return _cmd_buffers[i]; } inline GraphicPipeline& getPipeline() noexcept { return _pipeline; } inline CmdBuffer& getActiveCmdBuffer() noexcept { return _cmd_buffers[_active_image_index]; } inline DescriptorSet& getVertDescriptorSet() noexcept { return _vert_set; } inline DescriptorSet& getFragDescriptorSet() noexcept { return _frag_set; } inline DescriptorSetLayout& getVertDescriptorSetLayout() noexcept { return _vert_layout; } inline DescriptorSetLayout& getFragDescriptorSetLayout() noexcept { return _frag_layout; } inline PixelPutPipeline& getPixelPutPipeline() noexcept { return _pixel_put_pipeline; } inline uint32_t getActiveImageIndex() noexcept { return _active_image_index; } inline uint32_t getImageIndex() noexcept { return _image_index; } constexpr inline void requireFrameBufferResize(int index) noexcept { _framebufferResized = true; } ~Renderer() = default; private: PixelPutPipeline _pixel_put_pipeline; GraphicPipeline _pipeline; RenderPass _pass; Surface _surface; CmdPool _cmd_pool; SwapChain _swapchain; Semaphore _semaphore; DescriptorPool _desc_pool; DescriptorSetLayout _vert_layout; DescriptorSetLayout _frag_layout; DescriptorSet _vert_set; DescriptorSet _frag_set; std::array _cmd_buffers; std::unique_ptr _uniform_buffer = nullptr; class MLX_Window* _window = nullptr; uint32_t _active_image_index = 0; uint32_t _image_index = 0; bool _framebufferResized = false; }; } #endif