mirror of
https://github.com/seekrs/MacroLibX.git
synced 2026-01-12 23:23:34 +00:00
cleaning renderpass, framebuffers and swapchain code, setting all vulkan resources to NULL after destroy
This commit is contained in:
48
src/renderer/renderpass/vk_framebuffer.cpp
git.filemode.normal_file
48
src/renderer/renderpass/vk_framebuffer.cpp
git.filemode.normal_file
@@ -0,0 +1,48 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* vk_framebuffer.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/10/06 18:18:06 by maldavid #+# #+# */
|
||||
/* Updated: 2023/11/18 17:20:23 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <volk.h>
|
||||
#include <renderer/core/render_core.h>
|
||||
#include <renderer/renderer.h>
|
||||
#include <renderer/images/vk_image.h>
|
||||
|
||||
namespace mlx
|
||||
{
|
||||
void FrameBuffer::init(RenderPass& renderpass, Image& image)
|
||||
{
|
||||
VkImageView attachments[] = { image.getImageView() };
|
||||
|
||||
_width = image.getWidth();
|
||||
_height = image.getHeight();
|
||||
|
||||
VkFramebufferCreateInfo framebufferInfo{};
|
||||
framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
|
||||
framebufferInfo.renderPass = renderpass.get();
|
||||
framebufferInfo.attachmentCount = 1;
|
||||
framebufferInfo.pAttachments = attachments;
|
||||
framebufferInfo.width = _width;
|
||||
framebufferInfo.height = _height;
|
||||
framebufferInfo.layers = 1;
|
||||
|
||||
if(vkCreateFramebuffer(Render_Core::get().getDevice().get(), &framebufferInfo, nullptr, &_framebuffer) != VK_SUCCESS)
|
||||
core::error::report(e_kind::fatal_error, "Vulkan : failed to create a framebuffer");
|
||||
#ifdef DEBUG
|
||||
core::error::report(e_kind::message, "Vulkan : created new framebuffer");
|
||||
#endif
|
||||
}
|
||||
|
||||
void FrameBuffer::destroy() noexcept
|
||||
{
|
||||
vkDestroyFramebuffer(Render_Core::get().getDevice().get(), _framebuffer, nullptr);
|
||||
_framebuffer = VK_NULL_HANDLE;
|
||||
}
|
||||
}
|
||||
38
src/renderer/renderpass/vk_framebuffer.h
git.filemode.normal_file
38
src/renderer/renderpass/vk_framebuffer.h
git.filemode.normal_file
@@ -0,0 +1,38 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* vk_framebuffer.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/10/06 18:19:44 by maldavid #+# #+# */
|
||||
/* Updated: 2023/11/18 16:44:16 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef __MLX_VK_FRAMEBUFFER__
|
||||
#define __MLX_VK_FRAMEBUFFER__
|
||||
|
||||
#include <volk.h>
|
||||
|
||||
namespace mlx
|
||||
{
|
||||
class FrameBuffer
|
||||
{
|
||||
public:
|
||||
void init(class RenderPass& renderpass, class Image& image);
|
||||
void destroy() noexcept;
|
||||
|
||||
inline VkFramebuffer& operator()() noexcept { return _framebuffer; }
|
||||
inline VkFramebuffer& get() noexcept { return _framebuffer; }
|
||||
inline uint32_t getWidth() const noexcept { return _width; }
|
||||
inline uint32_t getHeight() const noexcept { return _height; }
|
||||
|
||||
private:
|
||||
VkFramebuffer _framebuffer = VK_NULL_HANDLE;
|
||||
uint32_t _width = 0;
|
||||
uint32_t _height = 0;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // __MLX_VK_FRAMEBUFFER__
|
||||
89
src/renderer/renderpass/vk_render_pass.cpp
git.filemode.normal_file
89
src/renderer/renderpass/vk_render_pass.cpp
git.filemode.normal_file
@@ -0,0 +1,89 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* vk_render_pass.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/10/06 18:21:36 by maldavid #+# #+# */
|
||||
/* Updated: 2023/11/18 15:58:26 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "vk_render_pass.h"
|
||||
#include <renderer/core/render_core.h>
|
||||
#include <renderer/renderer.h>
|
||||
#include <renderer/renderpass/vk_framebuffer.h>
|
||||
|
||||
namespace mlx
|
||||
{
|
||||
static const VkClearValue clearColor = { 0.0f, 0.0f, 0.0f, 1.0f };
|
||||
|
||||
void RenderPass::init(VkFormat attachement_format)
|
||||
{
|
||||
VkAttachmentDescription colorAttachment{};
|
||||
colorAttachment.format = attachement_format;
|
||||
colorAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
|
||||
colorAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
|
||||
colorAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
|
||||
colorAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
|
||||
colorAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
|
||||
colorAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
||||
colorAttachment.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
|
||||
|
||||
VkAttachmentReference colorAttachmentRef{};
|
||||
colorAttachmentRef.attachment = 0;
|
||||
colorAttachmentRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
|
||||
|
||||
VkSubpassDescription subpass{};
|
||||
subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
|
||||
subpass.colorAttachmentCount = 1;
|
||||
subpass.pColorAttachments = &colorAttachmentRef;
|
||||
|
||||
VkRenderPassCreateInfo renderPassInfo{};
|
||||
renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
|
||||
renderPassInfo.attachmentCount = 1;
|
||||
renderPassInfo.pAttachments = &colorAttachment;
|
||||
renderPassInfo.subpassCount = 1;
|
||||
renderPassInfo.pSubpasses = &subpass;
|
||||
|
||||
if(vkCreateRenderPass(Render_Core::get().getDevice().get(), &renderPassInfo, nullptr, &_renderPass) != VK_SUCCESS)
|
||||
core::error::report(e_kind::fatal_error, "Vulkan : failed to create render pass");
|
||||
#ifdef DEBUG
|
||||
core::error::report(e_kind::message, "Vulkan : created new render pass");
|
||||
#endif
|
||||
}
|
||||
|
||||
void RenderPass::begin(class CmdBuffer& cmd, class FrameBuffer& fb)
|
||||
{
|
||||
if(_is_running)
|
||||
return;
|
||||
|
||||
VkRenderPassBeginInfo renderPassInfo{};
|
||||
renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
|
||||
renderPassInfo.renderPass = _renderPass;
|
||||
renderPassInfo.framebuffer = fb.get();
|
||||
renderPassInfo.renderArea.offset = { 0, 0 };
|
||||
renderPassInfo.renderArea.extent = { fb.getWidth(), fb.getHeight() };
|
||||
renderPassInfo.clearValueCount = 1;
|
||||
renderPassInfo.pClearValues = &clearColor;
|
||||
|
||||
vkCmdBeginRenderPass(cmd.get(), &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE);
|
||||
|
||||
_is_running = true;
|
||||
}
|
||||
|
||||
void RenderPass::end(class CmdBuffer& cmd)
|
||||
{
|
||||
if(!_is_running)
|
||||
return;
|
||||
|
||||
vkCmdEndRenderPass(cmd.get());
|
||||
_is_running = false;
|
||||
}
|
||||
|
||||
void RenderPass::destroy() noexcept
|
||||
{
|
||||
vkDestroyRenderPass(Render_Core::get().getDevice().get(), _renderPass, nullptr);
|
||||
}
|
||||
}
|
||||
38
src/renderer/renderpass/vk_render_pass.h
git.filemode.normal_file
38
src/renderer/renderpass/vk_render_pass.h
git.filemode.normal_file
@@ -0,0 +1,38 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* vk_render_pass.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/10/06 18:22:00 by maldavid #+# #+# */
|
||||
/* Updated: 2023/11/18 15:58:12 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef __MLX_VK_RENDER_PASS__
|
||||
#define __MLX_VK_RENDER_PASS__
|
||||
|
||||
#include <volk.h>
|
||||
|
||||
namespace mlx
|
||||
{
|
||||
class RenderPass
|
||||
{
|
||||
public:
|
||||
void init(VkFormat attachement_format);
|
||||
void destroy() noexcept;
|
||||
|
||||
void begin(class CmdBuffer& cmd, class FrameBuffer& fb);
|
||||
void end(class CmdBuffer& cmd);
|
||||
|
||||
inline VkRenderPass& operator()() noexcept { return _renderPass; }
|
||||
inline VkRenderPass& get() noexcept { return _renderPass; }
|
||||
|
||||
private:
|
||||
VkRenderPass _renderPass = VK_NULL_HANDLE;
|
||||
bool _is_running = false;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // __MLX_VK_RENDER_PASS__
|
||||
Reference in New Issue
Block a user