Fuck this
This commit is contained in:
@@ -148,7 +148,7 @@ namespace fgl::engine
|
||||
camera_controller.moveInPlaneXZ( m_window.window(), delta_time, viewer );
|
||||
camera.setView( viewer.getPosition(), viewer.getRotation() );
|
||||
|
||||
if ( auto [ command_buffer, gui_command_buffer ] = m_renderer.beginFrame(); *command_buffer )
|
||||
if ( auto& command_buffer = m_renderer.beginFrame(); *command_buffer )
|
||||
{
|
||||
ZoneScopedN( "Render" );
|
||||
//Update
|
||||
@@ -159,7 +159,6 @@ namespace fgl::engine
|
||||
FrameInfo frame_info { frame_index,
|
||||
delta_time,
|
||||
command_buffer,
|
||||
gui_command_buffer,
|
||||
{ camera, viewer.getTransform() },
|
||||
global_descriptor_sets[ frame_index ],
|
||||
m_game_objects_root,
|
||||
|
||||
@@ -79,7 +79,6 @@ namespace fgl::engine
|
||||
float frame_time;
|
||||
|
||||
vk::raii::CommandBuffer& command_buffer;
|
||||
vk::raii::CommandBuffer& gui_command_buffer;
|
||||
|
||||
struct
|
||||
{
|
||||
|
||||
@@ -4,8 +4,6 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace fgl::engine
|
||||
{
|
||||
class ImageView;
|
||||
@@ -24,14 +22,11 @@ namespace fgl::engine
|
||||
a.setIndex( std::declval< std::uint32_t >() )
|
||||
};
|
||||
{
|
||||
a.attachImageView( std::declval< std::uint16_t >(), std::declval< std::shared_ptr< ImageView > >() )
|
||||
a.linkImages( std::declval< std::vector< std::shared_ptr< Image > >& >() )
|
||||
};
|
||||
{
|
||||
a.linkImage( std::declval< std::uint16_t >(), std::declval< Image& >() )
|
||||
};
|
||||
{
|
||||
a.resources()
|
||||
} -> std::same_as< AttachmentResources& >;
|
||||
a.view( std::declval< std::uint32_t >() )
|
||||
} -> std::same_as< ImageView& >;
|
||||
{
|
||||
a.m_clear_value
|
||||
} -> std::same_as< vk::ClearValue& >;
|
||||
|
||||
@@ -136,7 +136,6 @@ namespace fgl::engine::gui
|
||||
void drawMainGUI( FrameInfo& info )
|
||||
{
|
||||
ZoneScoped;
|
||||
TracyVkZone( info.tracy_ctx, *info.gui_command_buffer, "ImGui Rendering" );
|
||||
beginImGui();
|
||||
|
||||
// TODO: Maybe play with docks again some other time
|
||||
@@ -148,7 +147,7 @@ namespace fgl::engine::gui
|
||||
drawEntityInfo( info );
|
||||
drawFilesystemGUI( info );
|
||||
|
||||
endImGui( info.gui_command_buffer );
|
||||
endImGui( info.command_buffer );
|
||||
}
|
||||
|
||||
static GameObject* selected_object { nullptr };
|
||||
|
||||
@@ -15,6 +15,7 @@ namespace fgl::engine
|
||||
return view.lock();
|
||||
else
|
||||
{
|
||||
assert( m_handle );
|
||||
auto ptr { std::make_shared< ImageView >( m_handle ) };
|
||||
view = ptr;
|
||||
return ptr;
|
||||
|
||||
@@ -79,23 +79,19 @@ namespace fgl::engine
|
||||
description.finalLayout = final_layout;
|
||||
}
|
||||
|
||||
void attachImageView( std::uint16_t frame_idx, std::shared_ptr< ImageView > image_view )
|
||||
void linkImage( std::shared_ptr< Image > image )
|
||||
{
|
||||
auto& image_views = m_attachment_resources.m_image_views;
|
||||
if ( image_views.size() <= frame_idx ) image_views.resize( frame_idx + 1 );
|
||||
image_views[ frame_idx ] = std::move( image_view );
|
||||
auto& itter { m_attachment_resources.m_images.emplace_back( std::move( image ) ) };
|
||||
m_attachment_resources.m_image_views.emplace_back( itter->getView() );
|
||||
}
|
||||
|
||||
void linkImage( std::uint16_t frame_idx, Image& image ) { attachImageView( frame_idx, image.getView() ); }
|
||||
|
||||
void linkImages( std::vector< Image >& images )
|
||||
void linkImages( std::vector< std::shared_ptr< Image > >& images )
|
||||
{
|
||||
for ( std::uint16_t i = 0; i < images.size(); ++i )
|
||||
for ( auto image : images )
|
||||
{
|
||||
linkImage( i, images[ i ] );
|
||||
auto& itter { m_attachment_resources.m_images.emplace_back( std::move( image ) ) };
|
||||
m_attachment_resources.m_image_views.emplace_back( itter->getView() );
|
||||
}
|
||||
|
||||
log::info( "Linked {} images to the swapchain color", images.size() );
|
||||
}
|
||||
|
||||
void createResources( const std::uint32_t count, vk::Extent2D extent )
|
||||
@@ -132,8 +128,6 @@ namespace fgl::engine
|
||||
}
|
||||
}
|
||||
|
||||
AttachmentResources& resources() { return m_attachment_resources; }
|
||||
|
||||
vk::AttachmentDescription& desc() { return description; }
|
||||
|
||||
std::uint32_t getIndex() const
|
||||
@@ -144,9 +138,17 @@ namespace fgl::engine
|
||||
return m_index;
|
||||
}
|
||||
|
||||
ImageView& view( const FrameIndex index ) { return *m_attachment_resources.m_image_views[ index ]; }
|
||||
ImageView& view( const FrameIndex index )
|
||||
{
|
||||
assert( index < m_attachment_resources.m_image_views.size() );
|
||||
return *m_attachment_resources.m_image_views[ index ];
|
||||
}
|
||||
|
||||
Image& image( const FrameIndex index ) { return *m_attachment_resources.m_images[ index ]; }
|
||||
Image& image( const FrameIndex index )
|
||||
{
|
||||
assert( index < m_attachment_resources.m_images.size() );
|
||||
return *m_attachment_resources.m_images[ index ];
|
||||
}
|
||||
|
||||
void setName( const std::string str )
|
||||
{
|
||||
@@ -154,6 +156,7 @@ namespace fgl::engine
|
||||
auto& image_views { m_attachment_resources.m_image_views };
|
||||
|
||||
assert( images.size() == image_views.size() );
|
||||
assert( images.size() > 0 );
|
||||
|
||||
for ( std::size_t i = 0; i < images.size(); ++i )
|
||||
{
|
||||
|
||||
@@ -6,8 +6,8 @@
|
||||
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
#include <unordered_set>
|
||||
#include <iostream>
|
||||
#include <unordered_set>
|
||||
|
||||
#include "engine/logging/logging.hpp"
|
||||
|
||||
|
||||
@@ -74,7 +74,7 @@ namespace fgl::engine
|
||||
void Renderer::recreateSwapchain()
|
||||
{
|
||||
ZoneScoped;
|
||||
std::cout << "Rebuilding swap chain" << std::endl;
|
||||
log::info( "Rebuilding swapchain" );
|
||||
auto extent { m_window.getExtent() };
|
||||
|
||||
while ( extent.width == 0 || extent.height == 0 )
|
||||
@@ -97,7 +97,7 @@ namespace fgl::engine
|
||||
}
|
||||
}
|
||||
|
||||
std::pair< vk::raii::CommandBuffer&, vk::raii::CommandBuffer& > Renderer::beginFrame()
|
||||
vk::raii::CommandBuffer& Renderer::beginFrame()
|
||||
{
|
||||
assert( !is_frame_started && "Cannot begin frame while frame is already in progress" );
|
||||
auto [ result, present_index ] = m_swapchain->acquireNextImage();
|
||||
@@ -113,7 +113,6 @@ namespace fgl::engine
|
||||
|
||||
is_frame_started = true;
|
||||
auto& command_buffer { getCurrentCommandbuffer() };
|
||||
auto& gui_command_buffer { getCurrentGuiCommandBuffer() };
|
||||
|
||||
vk::CommandBufferBeginInfo begin_info {};
|
||||
begin_info.pNext = VK_NULL_HANDLE;
|
||||
@@ -122,22 +121,7 @@ namespace fgl::engine
|
||||
|
||||
command_buffer.begin( begin_info );
|
||||
|
||||
vk::CommandBufferInheritanceInfo inheritance_info {};
|
||||
inheritance_info.framebuffer =
|
||||
this->getSwapChain().getFrameBuffer( current_frame_index, current_present_index );
|
||||
inheritance_info.renderPass = this->getSwapChainRenderPass();
|
||||
inheritance_info.subpass = 2;
|
||||
|
||||
vk::CommandBufferBeginInfo gui_begin_info {};
|
||||
gui_begin_info.pInheritanceInfo = &inheritance_info;
|
||||
gui_begin_info.flags = vk::CommandBufferUsageFlagBits::eRenderPassContinue;
|
||||
|
||||
gui_command_buffer.begin( gui_begin_info );
|
||||
|
||||
setViewport( gui_command_buffer );
|
||||
setScissor( gui_command_buffer );
|
||||
|
||||
return { command_buffer, gui_command_buffer };
|
||||
return command_buffer;
|
||||
}
|
||||
|
||||
void Renderer::endFrame()
|
||||
|
||||
@@ -11,7 +11,6 @@
|
||||
|
||||
#include "Device.hpp"
|
||||
#include "SwapChain.hpp"
|
||||
#include "engine/Window.hpp"
|
||||
|
||||
//clang-format: off
|
||||
#include <tracy/TracyVulkan.hpp>
|
||||
@@ -81,7 +80,7 @@ namespace fgl::engine
|
||||
|
||||
float getAspectRatio() const { return m_swapchain->extentAspectRatio(); }
|
||||
|
||||
std::pair< vk::raii::CommandBuffer&, vk::raii::CommandBuffer& > beginFrame();
|
||||
vk::raii::CommandBuffer& beginFrame();
|
||||
void endFrame();
|
||||
|
||||
void setViewport( const vk::raii::CommandBuffer& buffer );
|
||||
|
||||
@@ -20,9 +20,17 @@ namespace fgl::engine
|
||||
m_swapchain_support( Device::getInstance().getSwapChainSupport() ),
|
||||
m_surface_format( chooseSwapSurfaceFormat( m_swapchain_support.formats ) ),
|
||||
m_swap_chain_format( m_surface_format.format ),
|
||||
m_swap_chain_depth_format( findDepthFormat() ),
|
||||
m_swap_chain_extent( chooseSwapExtent( m_swapchain_support.capabilities ) ),
|
||||
windowExtent( extent ),
|
||||
m_render_attachments( getSwapChainImageFormat(), findDepthFormat() )
|
||||
m_swapchain_extent( extent ),
|
||||
m_swapchain( createSwapChain() ),
|
||||
m_swap_chain_images( createSwapchainImages() ),
|
||||
m_render_attachments( getSwapChainImageFormat(), findDepthFormat() ),
|
||||
m_render_pass( createRenderPass() ),
|
||||
m_swap_chain_buffers( createFramebuffers() ),
|
||||
m_clear_values( populateAttachmentClearValues() ),
|
||||
m_gbuffer_descriptor_set( createGBufferDescriptors() ),
|
||||
m_gbuffer_composite_descriptor_set( createGBufferCompositeDescriptors() )
|
||||
{
|
||||
init();
|
||||
}
|
||||
@@ -33,9 +41,17 @@ namespace fgl::engine
|
||||
m_swapchain_support( Device::getInstance().getSwapChainSupport() ),
|
||||
m_surface_format( chooseSwapSurfaceFormat( m_swapchain_support.formats ) ),
|
||||
m_swap_chain_format( m_surface_format.format ),
|
||||
m_swap_chain_depth_format( findDepthFormat() ),
|
||||
m_swap_chain_extent( chooseSwapExtent( m_swapchain_support.capabilities ) ),
|
||||
windowExtent( extent ),
|
||||
m_render_attachments( getSwapChainImageFormat(), findDepthFormat() )
|
||||
m_swapchain_extent( extent ),
|
||||
m_swapchain( createSwapChain() ),
|
||||
m_swap_chain_images( createSwapchainImages() ),
|
||||
m_render_attachments( getSwapChainImageFormat(), findDepthFormat() ),
|
||||
m_render_pass( createRenderPass() ),
|
||||
m_swap_chain_buffers( createFramebuffers() ),
|
||||
m_clear_values( populateAttachmentClearValues() ),
|
||||
m_gbuffer_descriptor_set( createGBufferDescriptors() ),
|
||||
m_gbuffer_composite_descriptor_set( createGBufferCompositeDescriptors() )
|
||||
{
|
||||
init();
|
||||
old_swap_chain.reset();
|
||||
@@ -50,9 +66,6 @@ namespace fgl::engine
|
||||
|
||||
void SwapChain::init()
|
||||
{
|
||||
createSwapChain();
|
||||
createRenderPass();
|
||||
createFramebuffers();
|
||||
createSyncObjects();
|
||||
}
|
||||
|
||||
@@ -66,7 +79,7 @@ namespace fgl::engine
|
||||
!= vk::Result::eSuccess )
|
||||
throw std::runtime_error( "failed to wait for fences!" );
|
||||
|
||||
auto result { swapChain.acquireNextImage(
|
||||
auto result { m_swapchain.acquireNextImage(
|
||||
std::numeric_limits< uint64_t >::max(),
|
||||
imageAvailableSemaphores[ current_frame_index ] // must be a not signaled semaphore
|
||||
) };
|
||||
@@ -114,7 +127,7 @@ namespace fgl::engine
|
||||
|
||||
presentInfo.setWaitSemaphores( signaled_semaphores );
|
||||
|
||||
std::vector< vk::SwapchainKHR > swapchains { swapChain };
|
||||
std::vector< vk::SwapchainKHR > swapchains { m_swapchain };
|
||||
presentInfo.setSwapchains( swapchains );
|
||||
|
||||
std::array< std::uint32_t, 1 > indicies { { current_present_index } };
|
||||
@@ -133,31 +146,31 @@ namespace fgl::engine
|
||||
return vk::Result::eSuccess;
|
||||
}
|
||||
|
||||
void SwapChain::createSwapChain()
|
||||
vk::raii::SwapchainKHR SwapChain::createSwapChain()
|
||||
{
|
||||
ZoneScoped;
|
||||
|
||||
const vk::PresentModeKHR presentMode { chooseSwapPresentMode( m_swapchain_support.presentModes ) };
|
||||
const vk::PresentModeKHR present_mode { chooseSwapPresentMode( m_swapchain_support.presentModes ) };
|
||||
|
||||
uint32_t imageCount = m_swapchain_support.capabilities.minImageCount + 1;
|
||||
uint32_t image_count = m_swapchain_support.capabilities.minImageCount + 1;
|
||||
if ( m_swapchain_support.capabilities.maxImageCount > 0
|
||||
&& imageCount > m_swapchain_support.capabilities.maxImageCount )
|
||||
&& image_count > m_swapchain_support.capabilities.maxImageCount )
|
||||
{
|
||||
imageCount = m_swapchain_support.capabilities.maxImageCount;
|
||||
image_count = m_swapchain_support.capabilities.maxImageCount;
|
||||
}
|
||||
|
||||
vk::SwapchainCreateInfoKHR createInfo = {};
|
||||
createInfo.surface = Device::getInstance().surface();
|
||||
vk::SwapchainCreateInfoKHR create_info = {};
|
||||
create_info.surface = Device::getInstance().surface();
|
||||
|
||||
createInfo.minImageCount = imageCount;
|
||||
createInfo.imageFormat = m_surface_format.format;
|
||||
createInfo.imageColorSpace = m_surface_format.colorSpace;
|
||||
createInfo.imageExtent = m_swap_chain_extent;
|
||||
createInfo.imageArrayLayers = 1;
|
||||
createInfo.imageUsage = vk::ImageUsageFlagBits::eColorAttachment;
|
||||
create_info.minImageCount = image_count;
|
||||
create_info.imageFormat = m_surface_format.format;
|
||||
create_info.imageColorSpace = m_surface_format.colorSpace;
|
||||
create_info.imageExtent = m_swap_chain_extent;
|
||||
create_info.imageArrayLayers = 1;
|
||||
create_info.imageUsage = vk::ImageUsageFlagBits::eColorAttachment;
|
||||
|
||||
std::uint32_t graphics_family { m_phy_device.queueInfo().getIndex( vk::QueueFlagBits::eGraphics ) };
|
||||
std::uint32_t present_family { m_phy_device.queueInfo().getPresentIndex() };
|
||||
const std::uint32_t graphics_family { m_phy_device.queueInfo().getIndex( vk::QueueFlagBits::eGraphics ) };
|
||||
const std::uint32_t present_family { m_phy_device.queueInfo().getPresentIndex() };
|
||||
|
||||
const uint32_t queueFamilyIndices[] = { graphics_family, present_family };
|
||||
|
||||
@@ -165,38 +178,49 @@ namespace fgl::engine
|
||||
{
|
||||
// If the familys are not the same then the swapchain must be shared between
|
||||
// both queues.
|
||||
createInfo.imageSharingMode = vk::SharingMode::eConcurrent;
|
||||
createInfo.queueFamilyIndexCount = 2;
|
||||
createInfo.pQueueFamilyIndices = queueFamilyIndices;
|
||||
create_info.imageSharingMode = vk::SharingMode::eConcurrent;
|
||||
create_info.queueFamilyIndexCount = 2;
|
||||
create_info.pQueueFamilyIndices = queueFamilyIndices;
|
||||
}
|
||||
else
|
||||
{
|
||||
createInfo.imageSharingMode = vk::SharingMode::eExclusive;
|
||||
createInfo.queueFamilyIndexCount = 0; // Optional
|
||||
createInfo.pQueueFamilyIndices = nullptr; // Optional
|
||||
create_info.imageSharingMode = vk::SharingMode::eExclusive;
|
||||
create_info.queueFamilyIndexCount = 0; // Optional
|
||||
create_info.pQueueFamilyIndices = nullptr; // Optional
|
||||
}
|
||||
|
||||
createInfo.preTransform = m_swapchain_support.capabilities.currentTransform;
|
||||
createInfo.compositeAlpha = vk::CompositeAlphaFlagBitsKHR::eOpaque;
|
||||
create_info.preTransform = m_swapchain_support.capabilities.currentTransform;
|
||||
create_info.compositeAlpha = vk::CompositeAlphaFlagBitsKHR::eOpaque;
|
||||
|
||||
createInfo.presentMode = presentMode;
|
||||
createInfo.clipped = VK_TRUE;
|
||||
create_info.presentMode = present_mode;
|
||||
create_info.clipped = VK_TRUE;
|
||||
|
||||
createInfo.oldSwapchain = old_swap_chain == nullptr ? VK_NULL_HANDLE : *old_swap_chain->swapChain;
|
||||
create_info.oldSwapchain = ( old_swap_chain == nullptr ? VK_NULL_HANDLE : *old_swap_chain->m_swapchain );
|
||||
|
||||
swapChain = Device::getInstance()->createSwapchainKHR( createInfo );
|
||||
|
||||
std::vector< vk::Image > swap_chain_images { swapChain.getImages() };
|
||||
|
||||
for ( std::uint64_t i = 0; i < swap_chain_images.size(); i++ )
|
||||
{
|
||||
auto& itter = m_swap_chain_images.emplace_back(
|
||||
m_swap_chain_extent, m_surface_format.format, swap_chain_images[ i ], createInfo.imageUsage );
|
||||
itter.setName( "Swapchain image: " + std::to_string( i ) );
|
||||
}
|
||||
return Device::getInstance()->createSwapchainKHR( create_info );
|
||||
}
|
||||
|
||||
void SwapChain::createRenderPass()
|
||||
std::vector< std::shared_ptr< Image > > SwapChain::createSwapchainImages()
|
||||
{
|
||||
std::vector< vk::Image > swap_chain_images { m_swapchain.getImages() };
|
||||
std::vector< std::shared_ptr< Image > > images {};
|
||||
images.reserve( swap_chain_images.size() );
|
||||
|
||||
for ( std::uint64_t i = 0; i < swap_chain_images.size(); ++i )
|
||||
{
|
||||
images.emplace_back( std::make_shared< Image >(
|
||||
m_swap_chain_extent,
|
||||
m_surface_format.format,
|
||||
swap_chain_images[ i ],
|
||||
vk::ImageUsageFlagBits::eColorAttachment ) );
|
||||
}
|
||||
|
||||
assert( swap_chain_images.size() == images.size() );
|
||||
|
||||
return images;
|
||||
}
|
||||
|
||||
vk::raii::RenderPass SwapChain::createRenderPass()
|
||||
{
|
||||
ZoneScoped;
|
||||
|
||||
@@ -235,13 +259,13 @@ namespace fgl::engine
|
||||
Subpass<
|
||||
vk::PipelineBindPoint::eGraphics,
|
||||
UsedAttachment< DepthAttachment, vk::ImageLayout::eDepthStencilAttachmentOptimal >,
|
||||
UsedAttachment< ColoredPresentAttachment, vk::ImageLayout::eColorAttachmentOptimal >,
|
||||
UsedAttachment< ColorAttachment, vk::ImageLayout::eColorAttachmentOptimal >,
|
||||
InputAttachment< ColorAttachment, vk::ImageLayout::eShaderReadOnlyOptimal >,
|
||||
InputAttachment< ColorAttachment, vk::ImageLayout::eShaderReadOnlyOptimal >,
|
||||
InputAttachment< ColorAttachment, vk::ImageLayout::eShaderReadOnlyOptimal > >
|
||||
composite_subpass { 1,
|
||||
m_render_attachments.depth,
|
||||
m_render_attachments.color,
|
||||
m_gbuffer_attachments.composite,
|
||||
m_gbuffer_attachments.position,
|
||||
m_gbuffer_attachments.normal,
|
||||
m_gbuffer_attachments.albedo };
|
||||
@@ -249,8 +273,6 @@ namespace fgl::engine
|
||||
composite_subpass.registerDependencyFromExternal(
|
||||
vk::AccessFlagBits::eColorAttachmentWrite, vk::PipelineStageFlagBits::eColorAttachmentOutput );
|
||||
|
||||
composite_subpass.registerFullDependency( g_buffer_subpass );
|
||||
|
||||
// For color attachments
|
||||
composite_subpass.registerDependencyFrom(
|
||||
g_buffer_subpass,
|
||||
@@ -288,7 +310,7 @@ namespace fgl::engine
|
||||
composite_subpass,
|
||||
vk::AccessFlagBits::eColorAttachmentWrite,
|
||||
vk::PipelineStageFlagBits::eColorAttachmentOutput,
|
||||
vk::AccessFlagBits::eShaderRead,
|
||||
vk::AccessFlagBits::eShaderRead | vk::AccessFlagBits::eInputAttachmentRead,
|
||||
vk::PipelineStageFlagBits::eFragmentShader,
|
||||
vk::DependencyFlagBits::eByRegion );
|
||||
|
||||
@@ -311,13 +333,14 @@ namespace fgl::engine
|
||||
render_pass_builder.registerSubpass( composite_subpass );
|
||||
render_pass_builder.registerSubpass( gui_subpass );
|
||||
|
||||
m_render_pass = render_pass_builder.create();
|
||||
return render_pass_builder.create();
|
||||
}
|
||||
|
||||
template < is_attachment... Attachments >
|
||||
std::vector< vk::ImageView > fillViewsFromAttachments( uint8_t frame_idx, Attachments&&... attachments )
|
||||
std::vector< std::shared_ptr< ImageView > >
|
||||
fillViewsFromAttachments( uint8_t frame_idx, Attachments&&... attachments )
|
||||
{
|
||||
std::vector< vk::ImageView > views {};
|
||||
std::vector< std::shared_ptr< ImageView > > views {};
|
||||
|
||||
views.resize( sizeof...( Attachments ) );
|
||||
|
||||
@@ -326,17 +349,11 @@ namespace fgl::engine
|
||||
return views;
|
||||
}
|
||||
|
||||
void SwapChain::createFramebuffers()
|
||||
std::vector< std::vector< vk::raii::Framebuffer > > SwapChain::createFramebuffers()
|
||||
{
|
||||
ZoneScoped;
|
||||
|
||||
// Render target resource management
|
||||
for ( int i = 0; i < imageCount(); ++i )
|
||||
{
|
||||
auto& image { m_swap_chain_images[ i ] };
|
||||
|
||||
image.setName( "SwapChainImage: " + std::to_string( i ) );
|
||||
}
|
||||
assert( imageCount() > 0 );
|
||||
|
||||
m_render_attachments.color.linkImages( m_swap_chain_images );
|
||||
|
||||
@@ -361,54 +378,13 @@ namespace fgl::engine
|
||||
{ 0.0f, 0.0f, 0.0f, 0.0f } } ) );
|
||||
m_gbuffer_attachments.composite.setClear( vk::ClearColorValue( std::array< float, 4 > {
|
||||
{ 0.0f, 0.0f, 0.0f, 0.0f } } ) );
|
||||
;
|
||||
|
||||
populateAttachmentClearValues(
|
||||
m_render_attachments.color,
|
||||
m_render_attachments.depth,
|
||||
m_gbuffer_attachments.position,
|
||||
m_gbuffer_attachments.normal,
|
||||
m_gbuffer_attachments.albedo,
|
||||
m_gbuffer_attachments.composite );
|
||||
|
||||
for ( FrameIndex i = 0; i < SwapChain::MAX_FRAMES_IN_FLIGHT; ++i )
|
||||
{
|
||||
auto gbuffer_set { std::make_unique< descriptors::DescriptorSet >( GBufferDescriptorSet::createLayout() ) };
|
||||
|
||||
gbuffer_set->setMaxIDX( 2 );
|
||||
|
||||
gbuffer_set->bindAttachment(
|
||||
0, m_gbuffer_attachments.position.view( i ), vk::ImageLayout::eShaderReadOnlyOptimal );
|
||||
|
||||
gbuffer_set
|
||||
->bindAttachment( 1, m_gbuffer_attachments.normal.view( i ), vk::ImageLayout::eShaderReadOnlyOptimal );
|
||||
|
||||
gbuffer_set
|
||||
->bindAttachment( 2, m_gbuffer_attachments.albedo.view( i ), vk::ImageLayout::eShaderReadOnlyOptimal );
|
||||
|
||||
gbuffer_set->update();
|
||||
|
||||
m_gbuffer_descriptor_set.at( i ) = std::move( gbuffer_set );
|
||||
|
||||
auto composite_set {
|
||||
std::make_unique< descriptors::DescriptorSet >( GBufferCompositeDescriptorSet::createLayout() )
|
||||
};
|
||||
|
||||
composite_set->setMaxIDX( 0 );
|
||||
composite_set->bindAttachment(
|
||||
0, m_gbuffer_attachments.composite.view( i ), vk::ImageLayout::eShaderReadOnlyOptimal );
|
||||
|
||||
composite_set->update();
|
||||
|
||||
m_gbuffer_composite_descriptor_set.at( i ) = std::move( composite_set );
|
||||
}
|
||||
|
||||
m_swap_chain_buffers.clear();
|
||||
m_swap_chain_buffers.reserve( imageCount() );
|
||||
std::vector< std::vector< vk::raii::Framebuffer > > framebuffers {};
|
||||
|
||||
for ( std::uint16_t present_idx = 0; present_idx < imageCount(); ++present_idx )
|
||||
{
|
||||
std::vector< vk::raii::Framebuffer > temp {};
|
||||
temp.reserve( SwapChain::MAX_FRAMES_IN_FLIGHT );
|
||||
|
||||
for ( FrameIndex frame_idx = 0; frame_idx < SwapChain::MAX_FRAMES_IN_FLIGHT; frame_idx++ )
|
||||
{
|
||||
@@ -434,8 +410,10 @@ namespace fgl::engine
|
||||
temp.emplace_back( Device::getInstance()->createFramebuffer( framebufferInfo ) );
|
||||
}
|
||||
|
||||
m_swap_chain_buffers.emplace_back( std::move( temp ) );
|
||||
framebuffers.emplace_back( std::move( temp ) );
|
||||
}
|
||||
|
||||
return framebuffers;
|
||||
}
|
||||
|
||||
void SwapChain::createSyncObjects()
|
||||
@@ -534,7 +512,7 @@ namespace fgl::engine
|
||||
}
|
||||
else
|
||||
{
|
||||
vk::Extent2D actualExtent = windowExtent;
|
||||
vk::Extent2D actualExtent = m_swapchain_extent;
|
||||
actualExtent.width = std::
|
||||
max( capabilities.minImageExtent.width,
|
||||
std::min( capabilities.maxImageExtent.width, actualExtent.width ) );
|
||||
@@ -546,6 +524,57 @@ namespace fgl::engine
|
||||
}
|
||||
}
|
||||
|
||||
std::array< std::unique_ptr< descriptors::DescriptorSet >, SwapChain::MAX_FRAMES_IN_FLIGHT > SwapChain::
|
||||
createGBufferDescriptors()
|
||||
{
|
||||
std::array< std::unique_ptr< descriptors::DescriptorSet >, SwapChain::MAX_FRAMES_IN_FLIGHT > descriptors {};
|
||||
|
||||
for ( FrameIndex frame_idx = 0; frame_idx < SwapChain::MAX_FRAMES_IN_FLIGHT; ++frame_idx )
|
||||
{
|
||||
auto gbuffer_set { std::make_unique< descriptors::DescriptorSet >( GBufferDescriptorSet::createLayout() ) };
|
||||
|
||||
gbuffer_set->setMaxIDX( 2 );
|
||||
|
||||
gbuffer_set->bindAttachment(
|
||||
0, m_gbuffer_attachments.position.view( frame_idx ), vk::ImageLayout::eShaderReadOnlyOptimal );
|
||||
|
||||
gbuffer_set->bindAttachment(
|
||||
1, m_gbuffer_attachments.normal.view( frame_idx ), vk::ImageLayout::eShaderReadOnlyOptimal );
|
||||
|
||||
gbuffer_set->bindAttachment(
|
||||
2, m_gbuffer_attachments.albedo.view( frame_idx ), vk::ImageLayout::eShaderReadOnlyOptimal );
|
||||
|
||||
gbuffer_set->update();
|
||||
|
||||
descriptors.at( frame_idx ) = std::move( gbuffer_set );
|
||||
}
|
||||
|
||||
return descriptors;
|
||||
}
|
||||
|
||||
std::array< std::unique_ptr< descriptors::DescriptorSet >, SwapChain::MAX_FRAMES_IN_FLIGHT > SwapChain::
|
||||
createGBufferCompositeDescriptors()
|
||||
{
|
||||
std::array< std::unique_ptr< descriptors::DescriptorSet >, SwapChain::MAX_FRAMES_IN_FLIGHT > descriptors {};
|
||||
|
||||
for ( FrameIndex frame_idx = 0; frame_idx < SwapChain::MAX_FRAMES_IN_FLIGHT; ++frame_idx )
|
||||
{
|
||||
auto composite_set {
|
||||
std::make_unique< descriptors::DescriptorSet >( GBufferCompositeDescriptorSet::createLayout() )
|
||||
};
|
||||
|
||||
composite_set->setMaxIDX( 0 );
|
||||
composite_set->bindAttachment(
|
||||
0, m_gbuffer_attachments.composite.view( frame_idx ), vk::ImageLayout::eShaderReadOnlyOptimal );
|
||||
|
||||
composite_set->update();
|
||||
|
||||
m_gbuffer_composite_descriptor_set.at( frame_idx ) = std::move( composite_set );
|
||||
}
|
||||
|
||||
return descriptors;
|
||||
}
|
||||
|
||||
vk::Format SwapChain::findDepthFormat()
|
||||
{
|
||||
ZoneScoped;
|
||||
|
||||
@@ -27,26 +27,14 @@ namespace fgl::engine
|
||||
SwapChainSupportDetails m_swapchain_support;
|
||||
vk::SurfaceFormatKHR m_surface_format;
|
||||
|
||||
vk::Format m_swap_chain_format { vk::Format::eUndefined };
|
||||
vk::Format m_swap_chain_depth_format { findDepthFormat() };
|
||||
vk::Extent2D m_swap_chain_extent { 0, 0 };
|
||||
vk::Format m_swap_chain_format;
|
||||
vk::Format m_swap_chain_depth_format;
|
||||
vk::Extent2D m_swap_chain_extent;
|
||||
|
||||
std::vector< std::vector< vk::raii::Framebuffer > > m_swap_chain_buffers {};
|
||||
vk::raii::RenderPass m_render_pass { VK_NULL_HANDLE };
|
||||
vk::Extent2D m_swapchain_extent;
|
||||
|
||||
std::vector< Image > m_swap_chain_images {};
|
||||
|
||||
vk::Extent2D windowExtent;
|
||||
|
||||
vk::raii::SwapchainKHR swapChain { VK_NULL_HANDLE };
|
||||
|
||||
std::vector< vk::raii::Semaphore > imageAvailableSemaphores {};
|
||||
std::vector< vk::raii::Semaphore > renderFinishedSemaphores {};
|
||||
std::vector< vk::raii::Fence > in_flight_fences {};
|
||||
std::vector< vk::Fence > images_in_flight {};
|
||||
size_t current_frame_index { 0 };
|
||||
|
||||
std::vector< vk::ClearValue > m_clear_values {};
|
||||
vk::raii::SwapchainKHR m_swapchain;
|
||||
std::vector< std::shared_ptr< Image > > m_swap_chain_images;
|
||||
|
||||
//! Attachments for the final render target
|
||||
struct
|
||||
@@ -63,17 +51,51 @@ namespace fgl::engine
|
||||
ColorAttachment composite { vk::Format::eR8G8B8A8Unorm };
|
||||
} m_gbuffer_attachments {};
|
||||
|
||||
vk::raii::RenderPass m_render_pass;
|
||||
|
||||
std::vector< std::vector< vk::raii::Framebuffer > > m_swap_chain_buffers;
|
||||
|
||||
std::vector< vk::raii::Semaphore > imageAvailableSemaphores {};
|
||||
std::vector< vk::raii::Semaphore > renderFinishedSemaphores {};
|
||||
std::vector< vk::raii::Fence > in_flight_fences {};
|
||||
std::vector< vk::Fence > images_in_flight {};
|
||||
FrameIndex current_frame_index { 0 };
|
||||
|
||||
std::vector< vk::ClearValue > m_clear_values {};
|
||||
|
||||
std::array< std::unique_ptr< descriptors::DescriptorSet >, SwapChain::MAX_FRAMES_IN_FLIGHT >
|
||||
m_gbuffer_descriptor_set;
|
||||
|
||||
std::array< std::unique_ptr< descriptors::DescriptorSet >, SwapChain::MAX_FRAMES_IN_FLIGHT >
|
||||
m_gbuffer_composite_descriptor_set;
|
||||
|
||||
void init();
|
||||
void createSwapChain();
|
||||
void createRenderPass();
|
||||
void createFramebuffers();
|
||||
[[nodiscard]] vk::raii::SwapchainKHR createSwapChain();
|
||||
[[nodiscard]] std::vector< std::shared_ptr< Image > > createSwapchainImages();
|
||||
[[nodiscard]] vk::raii::RenderPass createRenderPass();
|
||||
[[nodiscard]] std::vector< std::vector< vk::raii::Framebuffer > > createFramebuffers();
|
||||
void createSyncObjects();
|
||||
|
||||
template < is_attachment... Attachments >
|
||||
void populateAttachmentClearValues( Attachments&... attachments )
|
||||
std::vector< vk::ClearValue > populateAttachmentClearValues()
|
||||
{
|
||||
m_clear_values.resize( sizeof...( Attachments ) );
|
||||
( ( m_clear_values[ attachments.getIndex() ] = attachments.m_clear_value ), ... );
|
||||
return populateAttachmentClearValues(
|
||||
m_render_attachments.color,
|
||||
m_render_attachments.depth,
|
||||
m_gbuffer_attachments.position,
|
||||
m_gbuffer_attachments.normal,
|
||||
m_gbuffer_attachments.albedo,
|
||||
m_gbuffer_attachments.composite );
|
||||
}
|
||||
|
||||
template < is_attachment... Attachments >
|
||||
std::vector< vk::ClearValue > populateAttachmentClearValues( Attachments&... attachments )
|
||||
{
|
||||
std::vector< vk::ClearValue > values {};
|
||||
|
||||
values.resize( sizeof...( Attachments ) );
|
||||
( ( values[ attachments.getIndex() ] = attachments.m_clear_value ), ... );
|
||||
|
||||
return values;
|
||||
}
|
||||
|
||||
// Helper functions
|
||||
@@ -82,10 +104,10 @@ namespace fgl::engine
|
||||
vk::Extent2D chooseSwapExtent( const vk::SurfaceCapabilitiesKHR& capabilities );
|
||||
|
||||
std::array< std::unique_ptr< descriptors::DescriptorSet >, SwapChain::MAX_FRAMES_IN_FLIGHT >
|
||||
m_gbuffer_descriptor_set {};
|
||||
createGBufferDescriptors();
|
||||
|
||||
std::array< std::unique_ptr< descriptors::DescriptorSet >, SwapChain::MAX_FRAMES_IN_FLIGHT >
|
||||
m_gbuffer_composite_descriptor_set {};
|
||||
createGBufferCompositeDescriptors();
|
||||
|
||||
public:
|
||||
|
||||
@@ -94,16 +116,12 @@ namespace fgl::engine
|
||||
descriptors::DescriptorSet& getGBufferDescriptor( std::uint16_t frame_idx ) const
|
||||
{
|
||||
assert( frame_idx < SwapChain::MAX_FRAMES_IN_FLIGHT && "Frame index out of range" );
|
||||
assert(
|
||||
m_gbuffer_descriptor_set.size() == SwapChain::MAX_FRAMES_IN_FLIGHT
|
||||
&& "GBuffer descriptor set not initialized" );
|
||||
return *m_gbuffer_descriptor_set[ frame_idx ];
|
||||
}
|
||||
|
||||
descriptors::DescriptorSet& getGBufferCompositeDescriptor( uint16_t frame_idx ) const
|
||||
{
|
||||
assert( frame_idx < SwapChain::MAX_FRAMES_IN_FLIGHT && "Frame index out of range" );
|
||||
|
||||
return *m_gbuffer_composite_descriptor_set[ frame_idx ];
|
||||
}
|
||||
|
||||
@@ -117,7 +135,11 @@ namespace fgl::engine
|
||||
|
||||
vk::raii::RenderPass& getRenderPass() { return m_render_pass; }
|
||||
|
||||
std::uint16_t imageCount() const { return m_swap_chain_images.size(); }
|
||||
std::uint16_t imageCount() const
|
||||
{
|
||||
assert( m_swap_chain_images.size() > 0 );
|
||||
return m_swap_chain_images.size();
|
||||
}
|
||||
|
||||
vk::Format getSwapChainImageFormat() const
|
||||
{
|
||||
@@ -127,9 +149,15 @@ namespace fgl::engine
|
||||
|
||||
vk::Extent2D getSwapChainExtent() const { return m_swap_chain_extent; }
|
||||
|
||||
uint32_t width() const { return m_swap_chain_extent.width; }
|
||||
uint32_t width() const
|
||||
{
|
||||
return m_swap_chain_extent.width;
|
||||
}
|
||||
|
||||
uint32_t height() const { return m_swap_chain_extent.height; }
|
||||
uint32_t height() const
|
||||
{
|
||||
return m_swap_chain_extent.height;
|
||||
}
|
||||
|
||||
bool compareSwapFormats( const SwapChain& other ) const
|
||||
{
|
||||
|
||||
@@ -25,6 +25,9 @@ namespace fgl::engine
|
||||
vk::raii::CommandBuffer& CompositionSystem::setupSystem( FrameInfo& info )
|
||||
{
|
||||
auto& command_buffer { info.command_buffer };
|
||||
|
||||
command_buffer.nextSubpass( vk::SubpassContents::eInline );
|
||||
|
||||
m_composite_pipeline->bind( command_buffer );
|
||||
|
||||
m_composite_pipeline
|
||||
@@ -35,8 +38,8 @@ namespace fgl::engine
|
||||
|
||||
void CompositionSystem::pass( FrameInfo& info )
|
||||
{
|
||||
info.command_buffer.nextSubpass( vk::SubpassContents::eInline );
|
||||
auto& command_buffer { setupSystem( info ) };
|
||||
TracyVkZone( info.tracy_ctx, *command_buffer, "Composition Pass" );
|
||||
|
||||
command_buffer.draw( 3, 1, 0, 0 );
|
||||
}
|
||||
|
||||
@@ -24,7 +24,9 @@ namespace fgl::engine
|
||||
|
||||
vk::raii::CommandBuffer& GuiSystem::setupSystem( FrameInfo& info )
|
||||
{
|
||||
auto& command_buffer { info.gui_command_buffer };
|
||||
auto& command_buffer { info.command_buffer };
|
||||
|
||||
command_buffer.nextSubpass( vk::SubpassContents::eInline );
|
||||
|
||||
m_pipeline
|
||||
->bindDescriptor( command_buffer, GBufferCompositeDescriptorSet::m_set_idx, info.gbuffer_composite_set );
|
||||
@@ -37,16 +39,12 @@ namespace fgl::engine
|
||||
void GuiSystem::pass( FrameInfo& info )
|
||||
{
|
||||
auto& command_buffer { setupSystem( info ) };
|
||||
TracyVkZone( info.tracy_ctx, *command_buffer, "Debug GUI Pass" );
|
||||
|
||||
command_buffer.draw( 3, 1, 0, 0 );
|
||||
|
||||
//Handle GUI
|
||||
gui::drawMainGUI( info );
|
||||
|
||||
command_buffer.end();
|
||||
|
||||
info.command_buffer.nextSubpass( vk::SubpassContents::eSecondaryCommandBuffers );
|
||||
info.command_buffer.executeCommands( { info.gui_command_buffer } );
|
||||
}
|
||||
|
||||
} // namespace fgl::engine
|
||||
|
||||
Reference in New Issue
Block a user