mirror of
https://github.com/seekrs/MacroLibX.git
synced 2026-01-12 15:13:34 +00:00
begenning the refactor
This commit is contained in:
53
runtime/Sources/Renderer/Renderpass/Framebuffer.cpp
git.filemode.normal_file
53
runtime/Sources/Renderer/Renderpass/Framebuffer.cpp
git.filemode.normal_file
@@ -0,0 +1,53 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* vk_framebuffer.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/10/06 18:18:06 by maldavid #+# #+# */
|
||||
/* Updated: 2024/03/25 19:03:35 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <pre_compiled.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;
|
||||
|
||||
VkResult res = vkCreateFramebuffer(Render_Core::get().getDevice().get(), &framebufferInfo, nullptr, &_framebuffer);
|
||||
if(res != VK_SUCCESS)
|
||||
core::error::report(e_kind::fatal_error, "Vulkan : failed to create a framebuffer, %s", RCore::verbaliseResultVk(res));
|
||||
#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;
|
||||
#ifdef DEBUG
|
||||
core::error::report(e_kind::message, "Vulkan : destroyed a framebuffer");
|
||||
#endif
|
||||
}
|
||||
}
|
||||
121
runtime/Sources/Renderer/Renderpass/Renderpass.cpp
git.filemode.normal_file
121
runtime/Sources/Renderer/Renderpass/Renderpass.cpp
git.filemode.normal_file
@@ -0,0 +1,121 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* vk_render_pass.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/10/06 18:21:36 by maldavid #+# #+# */
|
||||
/* Updated: 2024/03/25 19:03:37 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <pre_compiled.h>
|
||||
|
||||
#include "vk_render_pass.h"
|
||||
#include <renderer/core/render_core.h>
|
||||
#include <renderer/renderer.h>
|
||||
#include <renderer/renderpass/vk_framebuffer.h>
|
||||
#include <core/profiler.h>
|
||||
|
||||
namespace mlx
|
||||
{
|
||||
static const VkClearValue clearColor = {{{ 0.f, 0.f, 0.f, 1.0f }}}; // wtf, this mess to satisfy a warning
|
||||
|
||||
void RenderPass::init(VkFormat attachement_format, VkImageLayout layout)
|
||||
{
|
||||
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 = layout;
|
||||
|
||||
VkAttachmentReference colorAttachmentRef{};
|
||||
colorAttachmentRef.attachment = 0;
|
||||
colorAttachmentRef.layout = (layout == VK_IMAGE_LAYOUT_PRESENT_SRC_KHR ? VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL : layout);
|
||||
|
||||
VkSubpassDescription subpass1{};
|
||||
subpass1.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
|
||||
subpass1.colorAttachmentCount = 1;
|
||||
subpass1.pColorAttachments = &colorAttachmentRef;
|
||||
|
||||
VkSubpassDescription subpasses[] = { subpass1 };
|
||||
|
||||
std::vector<VkSubpassDependency> subpassesDeps;
|
||||
subpassesDeps.emplace_back();
|
||||
subpassesDeps.back().srcSubpass = VK_SUBPASS_EXTERNAL;
|
||||
subpassesDeps.back().dstSubpass = 0;
|
||||
subpassesDeps.back().srcStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
|
||||
subpassesDeps.back().dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
|
||||
subpassesDeps.back().srcAccessMask = VK_ACCESS_MEMORY_READ_BIT;
|
||||
subpassesDeps.back().dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
|
||||
subpassesDeps.back().dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT;
|
||||
|
||||
subpassesDeps.emplace_back();
|
||||
subpassesDeps.back().srcSubpass = 0;
|
||||
subpassesDeps.back().dstSubpass = VK_SUBPASS_EXTERNAL;
|
||||
subpassesDeps.back().srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
|
||||
subpassesDeps.back().dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
|
||||
subpassesDeps.back().srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
|
||||
subpassesDeps.back().dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
|
||||
subpassesDeps.back().dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT;
|
||||
|
||||
VkRenderPassCreateInfo renderPassInfo{};
|
||||
renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
|
||||
renderPassInfo.attachmentCount = 1;
|
||||
renderPassInfo.pAttachments = &colorAttachment;
|
||||
renderPassInfo.subpassCount = sizeof(subpasses) / sizeof(VkSubpassDescription);
|
||||
renderPassInfo.pSubpasses = subpasses;
|
||||
renderPassInfo.dependencyCount = static_cast<std::uint32_t>(subpassesDeps.size());
|
||||
renderPassInfo.pDependencies = subpassesDeps.data();
|
||||
|
||||
VkResult res = vkCreateRenderPass(Render_Core::get().getDevice().get(), &renderPassInfo, nullptr, &_render_pass);
|
||||
if(res != VK_SUCCESS)
|
||||
core::error::report(e_kind::fatal_error, "Vulkan : failed to create render pass, %s", RCore::verbaliseResultVk(res));
|
||||
#ifdef DEBUG
|
||||
core::error::report(e_kind::message, "Vulkan : created new render pass");
|
||||
#endif
|
||||
}
|
||||
|
||||
void RenderPass::begin(class CmdBuffer& cmd, class FrameBuffer& fb)
|
||||
{
|
||||
MLX_PROFILE_FUNCTION();
|
||||
if(_is_running)
|
||||
return;
|
||||
|
||||
VkRenderPassBeginInfo renderPassInfo{};
|
||||
renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
|
||||
renderPassInfo.renderPass = _render_pass;
|
||||
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)
|
||||
{
|
||||
MLX_PROFILE_FUNCTION();
|
||||
if(!_is_running)
|
||||
return;
|
||||
vkCmdEndRenderPass(cmd.get());
|
||||
_is_running = false;
|
||||
}
|
||||
|
||||
void RenderPass::destroy() noexcept
|
||||
{
|
||||
vkDestroyRenderPass(Render_Core::get().getDevice().get(), _render_pass, nullptr);
|
||||
_render_pass = VK_NULL_HANDLE;
|
||||
#ifdef DEBUG
|
||||
core::error::report(e_kind::message, "Vulkan : destroyed a renderpass");
|
||||
#endif
|
||||
}
|
||||
}
|
||||
152
runtime/Sources/Renderer/Renderpass/Swapchain.cpp
git.filemode.normal_file
152
runtime/Sources/Renderer/Renderpass/Swapchain.cpp
git.filemode.normal_file
@@ -0,0 +1,152 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* vk_swapchain.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/10/06 18:22:28 by maldavid #+# #+# */
|
||||
/* Updated: 2024/03/25 23:09:33 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <pre_compiled.h>
|
||||
|
||||
#include <renderer/core/render_core.h>
|
||||
#include <renderer/renderer.h>
|
||||
#include <platform/window.h>
|
||||
|
||||
namespace mlx
|
||||
{
|
||||
void SwapChain::init(Renderer* renderer)
|
||||
{
|
||||
VkDevice device = Render_Core::get().getDevice().get();
|
||||
|
||||
_renderer = renderer;
|
||||
_swapchain_support = querySwapChainSupport(Render_Core::get().getDevice().getPhysicalDevice());
|
||||
|
||||
VkSurfaceFormatKHR surfaceFormat = renderer->getSurface().chooseSwapSurfaceFormat(_swapchain_support.formats);
|
||||
VkPresentModeKHR presentMode = chooseSwapPresentMode(_swapchain_support.present_modes);
|
||||
_extent = chooseSwapExtent(_swapchain_support.capabilities);
|
||||
|
||||
std::uint32_t imageCount = _swapchain_support.capabilities.minImageCount + 1;
|
||||
if(_swapchain_support.capabilities.maxImageCount > 0 && imageCount > _swapchain_support.capabilities.maxImageCount)
|
||||
imageCount = _swapchain_support.capabilities.maxImageCount;
|
||||
|
||||
Queues::QueueFamilyIndices indices = Render_Core::get().getQueue().findQueueFamilies(Render_Core::get().getDevice().getPhysicalDevice());
|
||||
std::uint32_t queueFamilyIndices[] = { indices.graphics_family.value(), indices.present_family.value() };
|
||||
|
||||
VkSwapchainCreateInfoKHR createInfo{};
|
||||
createInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
|
||||
createInfo.surface = renderer->getSurface().get();
|
||||
createInfo.minImageCount = imageCount;
|
||||
createInfo.imageFormat = surfaceFormat.format;
|
||||
createInfo.imageColorSpace = surfaceFormat.colorSpace;
|
||||
createInfo.imageExtent = _extent;
|
||||
createInfo.imageArrayLayers = 1;
|
||||
createInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
|
||||
createInfo.preTransform = _swapchain_support.capabilities.currentTransform;
|
||||
createInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
|
||||
createInfo.presentMode = presentMode;
|
||||
createInfo.clipped = VK_TRUE;
|
||||
createInfo.oldSwapchain = VK_NULL_HANDLE;
|
||||
if(indices.graphics_family != indices.present_family)
|
||||
{
|
||||
createInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT;
|
||||
createInfo.queueFamilyIndexCount = 2;
|
||||
createInfo.pQueueFamilyIndices = queueFamilyIndices;
|
||||
}
|
||||
else
|
||||
createInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
||||
|
||||
VkResult res = vkCreateSwapchainKHR(device, &createInfo, nullptr, &_swapchain);
|
||||
if(res != VK_SUCCESS)
|
||||
core::error::report(e_kind::fatal_error, "Vulkan : failed to create the swapchain, %s", RCore::verbaliseResultVk(res));
|
||||
|
||||
std::vector<VkImage> tmp;
|
||||
vkGetSwapchainImagesKHR(device, _swapchain, &imageCount, nullptr);
|
||||
_images.resize(imageCount);
|
||||
tmp.resize(imageCount);
|
||||
vkGetSwapchainImagesKHR(device, _swapchain, &imageCount, tmp.data());
|
||||
|
||||
for(std::size_t i = 0; i < imageCount; i++)
|
||||
{
|
||||
_images[i].create(tmp[i], surfaceFormat.format, _extent.width, _extent.height);
|
||||
_images[i].transitionLayout(VK_IMAGE_LAYOUT_PRESENT_SRC_KHR);
|
||||
_images[i].createImageView(VK_IMAGE_VIEW_TYPE_2D, VK_IMAGE_ASPECT_COLOR_BIT);
|
||||
}
|
||||
|
||||
_swapchain_image_format = surfaceFormat.format;
|
||||
#ifdef DEBUG
|
||||
core::error::report(e_kind::message, "Vulkan : created new swapchain");
|
||||
#endif
|
||||
}
|
||||
|
||||
SwapChain::SwapChainSupportDetails SwapChain::querySwapChainSupport(VkPhysicalDevice device)
|
||||
{
|
||||
SwapChain::SwapChainSupportDetails details;
|
||||
VkSurfaceKHR surface = _renderer->getSurface().get();
|
||||
|
||||
if(vkGetPhysicalDeviceSurfaceCapabilitiesKHR(device, surface, &details.capabilities) != VK_SUCCESS)
|
||||
core::error::report(e_kind::fatal_error, "Vulkan : unable to retrieve surface capabilities");
|
||||
|
||||
std::uint32_t formatCount = 0;
|
||||
vkGetPhysicalDeviceSurfaceFormatsKHR(device, surface, &formatCount, nullptr);
|
||||
|
||||
if(formatCount != 0)
|
||||
{
|
||||
details.formats.resize(formatCount);
|
||||
vkGetPhysicalDeviceSurfaceFormatsKHR(device, surface, &formatCount, details.formats.data());
|
||||
}
|
||||
|
||||
std::uint32_t presentModeCount;
|
||||
vkGetPhysicalDeviceSurfacePresentModesKHR(device, surface, &presentModeCount, nullptr);
|
||||
|
||||
if(presentModeCount != 0)
|
||||
{
|
||||
details.present_modes.resize(presentModeCount);
|
||||
vkGetPhysicalDeviceSurfacePresentModesKHR(device, surface, &presentModeCount, details.present_modes.data());
|
||||
}
|
||||
|
||||
return details;
|
||||
}
|
||||
|
||||
VkPresentModeKHR SwapChain::chooseSwapPresentMode([[maybe_unused]] const std::vector<VkPresentModeKHR>& availablePresentModes)
|
||||
{
|
||||
// in the future, you may choose to activate vsync or not
|
||||
return VK_PRESENT_MODE_IMMEDIATE_KHR;
|
||||
}
|
||||
|
||||
VkExtent2D SwapChain::chooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities)
|
||||
{
|
||||
if(capabilities.currentExtent.width != std::numeric_limits<std::uint32_t>::max())
|
||||
return capabilities.currentExtent;
|
||||
|
||||
int width, height;
|
||||
glfwGetFramebufferSize(_renderer->getWindow()->getNativeWindow(), &width, &height);
|
||||
|
||||
VkExtent2D actualExtent = { static_cast<std::uint32_t>(width), static_cast<std::uint32_t>(height) };
|
||||
|
||||
actualExtent.width = std::clamp(actualExtent.width, capabilities.minImageExtent.width, capabilities.maxImageExtent.width);
|
||||
actualExtent.height = std::clamp(actualExtent.height, capabilities.minImageExtent.height, capabilities.maxImageExtent.height);
|
||||
|
||||
return actualExtent;
|
||||
}
|
||||
|
||||
void SwapChain::recreate()
|
||||
{
|
||||
destroy();
|
||||
init(_renderer);
|
||||
}
|
||||
|
||||
void SwapChain::destroy() noexcept
|
||||
{
|
||||
if(_swapchain == VK_NULL_HANDLE)
|
||||
return;
|
||||
vkDeviceWaitIdle(Render_Core::get().getDevice().get());
|
||||
vkDestroySwapchainKHR(Render_Core::get().getDevice().get(), _swapchain, nullptr);
|
||||
_swapchain = VK_NULL_HANDLE;
|
||||
for(Image& img : _images)
|
||||
img.destroyImageView();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user