fixing compatibility, workign on renderer

This commit is contained in:
Kbz-8
2022-12-18 04:04:10 +01:00
parent f52c4e51c9
commit 4a67aab716
48 changed files with 6535 additions and 75 deletions

158
src/renderer/swapchain/vk_swapchain.cpp git.filemode.normal_file
View File

@@ -0,0 +1,158 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* vk_swapchain.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/06 18:22:28 by maldavid #+# #+# */
/* Updated: 2022/12/18 01:01:32 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#include <renderer/core/render_core.h>
#include <platform/window.h>
#include <SDL2/SDL_vulkan.h>
#include <algorithm>
namespace mlx
{
void SwapChain::init()
{
_swapChainSupport = querySwapChainSupport(Render_Core::get().getDevice().getPhysicalDevice());
VkSurfaceFormatKHR surfaceFormat = Render_Core::get().getSurface().chooseSwapSurfaceFormat(_swapChainSupport.formats);
VkPresentModeKHR presentMode = VK_PRESENT_MODE_IMMEDIATE_KHR;
VkExtent2D extent = chooseSwapExtent(_swapChainSupport.capabilities);
uint32_t imageCount = _swapChainSupport.capabilities.minImageCount + 1;
if(_swapChainSupport.capabilities.maxImageCount > 0 && imageCount > _swapChainSupport.capabilities.maxImageCount)
imageCount = _swapChainSupport.capabilities.maxImageCount;
VkSwapchainCreateInfoKHR createInfo{};
createInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
createInfo.surface = Render_Core::get().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;
Queues::QueueFamilyIndices indices = Render_Core::get().getQueue().findQueueFamilies(Render_Core::get().getDevice().getPhysicalDevice());
uint32_t queueFamilyIndices[] = {indices.graphicsFamily.value(), indices.presentFamily.value()};
if(indices.graphicsFamily != indices.presentFamily)
{
createInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT;
createInfo.queueFamilyIndexCount = 2;
createInfo.pQueueFamilyIndices = queueFamilyIndices;
}
else
createInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
createInfo.preTransform = _swapChainSupport.capabilities.currentTransform;
createInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
createInfo.presentMode = presentMode;
createInfo.clipped = VK_TRUE;
createInfo.oldSwapchain = VK_NULL_HANDLE;
VkDevice device = Render_Core::get().getDevice().get();
if(vkCreateSwapchainKHR(device, &createInfo, nullptr, &_swapChain) != VK_SUCCESS)
core::error::report(e_kind::fatal_error, "Vulkan : failed to create swapchain");
vkGetSwapchainImagesKHR(device, _swapChain, &imageCount, nullptr);
_swapChainImages.resize(imageCount);
vkGetSwapchainImagesKHR(device, _swapChain, &imageCount, _swapChainImages.data());
_swapChainImageFormat = surfaceFormat.format;
_swapChainExtent = extent;
_imageViews.resize(_swapChainImages.size());
for(size_t i = 0; i < _swapChainImages.size(); i++)
{
_imageViews.emplace_back();
_imageViews.back().init(this, _swapChainImages[i]);
}
}
void SwapChain::initFB()
{
_framebuffers.resize(_imageViews.size());
for(size_t i = 0; i < _imageViews.size(); i++)
{
_framebuffers.emplace_back();
_framebuffers.back().init(this, _imageViews[i]);
}
}
SwapChain::SwapChainSupportDetails SwapChain::querySwapChainSupport(VkPhysicalDevice device)
{
SwapChain::SwapChainSupportDetails details;
VkSurfaceKHR surface = Render_Core::get().getSurface().get();
vkGetPhysicalDeviceSurfaceCapabilitiesKHR(device, surface, &details.capabilities);
uint32_t formatCount = 0;
vkGetPhysicalDeviceSurfaceFormatsKHR(device, surface, &formatCount, nullptr);
if(formatCount != 0)
{
details.formats.resize(formatCount);
vkGetPhysicalDeviceSurfaceFormatsKHR(device, surface, &formatCount, details.formats.data());
}
return details;
}
VkExtent2D SwapChain::chooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities)
{
if(capabilities.currentExtent.width != std::numeric_limits<uint32_t>::max())
return capabilities.currentExtent;
int width, height;
SDL_Vulkan_GetDrawableSize(Render_Core::get().getWindow()->getNativeWindow(), &width, &height);
VkExtent2D actualExtent = { static_cast<uint32_t>(width), static_cast<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()
{
destroyFB();
destroy();
Render_Core::get().getRenderPass().destroy();
init();
Render_Core::get().getRenderPass().init();
initFB();
}
void SwapChain::destroyFB() noexcept
{
vkDeviceWaitIdle(Render_Core::get().getDevice().get());
for(size_t i = 0; i < _framebuffers.size(); i++)
_framebuffers[i].destroy();
}
void SwapChain::destroy() noexcept
{
vkDeviceWaitIdle(Render_Core::get().getDevice().get());
for(size_t i = 0; i < _imageViews.size(); i++)
_imageViews[i].destroy();
if(_swapChain != VK_NULL_HANDLE)
vkDestroySwapchainKHR(Render_Core::get().getDevice().get(), _swapChain, nullptr);
_swapChain = VK_NULL_HANDLE;
}
}