Implement framebuffers for cameras

This commit is contained in:
2024-07-11 07:51:44 -04:00
parent 4eeda8fe2e
commit 45ada7fcb0
14 changed files with 214 additions and 41 deletions

View File

@@ -14,6 +14,7 @@
#include "KeyboardMovementController.hpp"
#include "assets/stores.hpp"
#include "buffers/HostSingleT.hpp"
#include "camera/Camera.hpp"
#include "engine/Average.hpp"
#include "engine/assets/TransferManager.hpp"
#include "engine/buffers/UniqueFrameSuballocation.hpp"
@@ -95,7 +96,7 @@ namespace fgl::engine
global_descriptor_sets[ i ].update();
}
Camera camera {};
Camera camera { vk::Extent2D( 1920, 1080 ) };
debug::setDebugDrawingCamera( camera );
auto viewer { GameObject::createGameObject() };

View File

@@ -10,18 +10,24 @@
#include <tracy/TracyVulkan.hpp>
//clang-format: on
#include "Camera.hpp"
#include "engine/descriptors/Descriptor.hpp"
#include "engine/descriptors/DescriptorSet.hpp"
#include "engine/descriptors/DescriptorSetLayout.hpp"
#include "engine/tree/octtree/OctTreeNode.hpp"
#include "descriptors/Descriptor.hpp"
#include "descriptors/DescriptorSetLayout.hpp"
#include "primitives/Frustum.hpp"
#include "rendering/types.hpp"
#define MAX_LIGHTS 10
namespace fgl::engine
{
class GameObject;
namespace descriptors
{
class DescriptorSet;
}
struct TransformComponent;
class SwapChain;
class Camera;
struct PointLight
{

View File

@@ -71,6 +71,20 @@ namespace fgl::engine
frustum = translation_matrix * base_frustum;
}
Camera::Camera( const vk::Extent2D extent ) :
m_extent( extent ),
m_swapchain( std::make_shared< CameraSwapchain >( m_extent ) )
{
this->setPerspectiveProjection( 90.0f, 16.0f / 9.0f, constants::NEAR_PLANE, constants::FAR_PLANE );
this->setView( WorldCoordinate( constants::CENTER ), Rotation( 0.0f, 0.0f, 0.0f ) );
}
void Camera::setExtent( const vk::Extent2D extent )
{
m_extent = extent;
m_swapchain = std::make_shared< CameraSwapchain >( m_extent );
}
Frustum< CoordinateSpace::Model >
createFrustum( const float aspect, const float fov_y, const float near, const float far )
{

View File

@@ -12,11 +12,13 @@
#include <glm/gtx/string_cast.hpp>
#pragma GCC diagnostic pop
#include "constants.hpp"
#include "CameraSwapchain.hpp"
#include "engine/constants.hpp"
#include "engine/primitives/Frustum.hpp"
#include "engine/primitives/TransformComponent.hpp"
#include "engine/primitives/matricies/Matrix.hpp"
#include "engine/primitives/points/Coordinate.hpp"
#include "engine/rendering/SwapChain.hpp"
namespace fgl::engine
{
@@ -26,12 +28,6 @@ namespace fgl::engine
class Camera
{
#ifdef EXPOSE_CAMERA_INTERNAL
public:
#endif
Matrix< MatrixType::CameraToScreen > projection_matrix { 1.0f };
Matrix< MatrixType::WorldToCamera > view_matrix { 1.0f };
@@ -45,17 +41,19 @@ namespace fgl::engine
Rotation current_rotation {};
vk::Extent2D m_extent;
std::shared_ptr< CameraSwapchain > m_swapchain;
Matrix< MatrixType::ModelToWorld > frustumTranslationMatrix() const;
void updateFrustum();
public:
Camera()
{
this->setPerspectiveProjection( 90.0f, 16.0f / 9.0f, constants::NEAR_PLANE, constants::FAR_PLANE );
this->setView( WorldCoordinate( constants::CENTER ), Rotation( 0.0f, 0.0f, 0.0f ) );
}
Camera( const vk::Extent2D extent );
void setExtent( const vk::Extent2D extent );
Rotation getRotation() const { return current_rotation; }

View File

@@ -0,0 +1,104 @@
//
// Created by kj16609 on 7/11/24.
//
#include "CameraSwapchain.hpp"
namespace fgl::engine
{
vk::raii::RenderPass CameraSwapchain::creaeteRenderpass()
{
RenderPassBuilder builder;
builder.registerAttachments( output.composite, output.depth, gbuffer.position, gbuffer.normal, gbuffer.albedo );
using UsedColorAttachment = UsedAttachment< ColorAttachment, vk::ImageLayout::eColorAttachmentOptimal >;
Subpass<
vk::PipelineBindPoint::eGraphics,
UsedAttachment< DepthAttachment, vk::ImageLayout::eDepthAttachmentOptimal >,
UsedColorAttachment,
UsedColorAttachment,
UsedColorAttachment >
g_buffer_subpass { 0, output.depth, gbuffer.position, gbuffer.normal, gbuffer.albedo };
g_buffer_subpass.registerDependencyFromExternal(
vk::AccessFlagBits::eDepthStencilAttachmentWrite,
vk::PipelineStageFlagBits::eEarlyFragmentTests | vk::PipelineStageFlagBits::eLateFragmentTests );
g_buffer_subpass.registerDependencyFromExternal(
vk::AccessFlagBits::eColorAttachmentWrite, vk::PipelineStageFlagBits::eColorAttachmentOutput );
Subpass<
vk::PipelineBindPoint::eGraphics,
UsedAttachment< ColorAttachment, vk::ImageLayout::eColorAttachmentOptimal >,
InputAttachment< ColorAttachment, vk::ImageLayout::eShaderReadOnlyOptimal >,
InputAttachment< ColorAttachment, vk::ImageLayout::eShaderReadOnlyOptimal >,
InputAttachment< ColorAttachment, vk::ImageLayout::eShaderReadOnlyOptimal > >
composite_subpass { 1, output.composite, gbuffer.position, gbuffer.normal, gbuffer.albedo };
composite_subpass.registerDependencyFromExternal(
vk::AccessFlagBits::eColorAttachmentWrite, vk::PipelineStageFlagBits::eColorAttachmentOutput );
// For color attachments
composite_subpass.registerDependencyFrom(
g_buffer_subpass,
vk::AccessFlagBits::eColorAttachmentWrite,
vk::PipelineStageFlagBits::eColorAttachmentOutput,
vk::AccessFlagBits::eInputAttachmentRead,
vk::PipelineStageFlagBits::eFragmentShader,
vk::DependencyFlagBits::eByRegion );
composite_subpass.registerDependencyToExternal(
vk::AccessFlagBits::eColorAttachmentWrite,
vk::PipelineStageFlagBits::eColorAttachmentOutput,
vk::AccessFlagBits::eShaderRead | vk::AccessFlagBits::eInputAttachmentRead,
vk::PipelineStageFlagBits::eFragmentShader );
//Done
builder.registerSubpass( g_buffer_subpass );
builder.registerSubpass( composite_subpass );
return builder.create();
}
std::vector< vk::raii::Framebuffer > CameraSwapchain::createFrambuffers()
{
const auto image_count { SwapChain::MAX_FRAMES_IN_FLIGHT };
gbuffer.position.createResourceSpread( image_count, m_extent );
gbuffer.normal.createResourceSpread( image_count, m_extent );
gbuffer.albedo.createResourceSpread( image_count, m_extent );
output.composite.createResourceSpread( image_count, m_extent, vk::ImageUsageFlagBits::eSampled );
output.depth.createResourceSpread( image_count, m_extent );
std::vector< vk::raii::Framebuffer > buffers {};
buffers.reserve( image_count );
for ( FrameIndex i = 0; i < image_count; ++i )
{
std::vector< vk::ImageView > attachments {
getViewsForFrame( i, gbuffer.position, gbuffer.normal, gbuffer.albedo, output.composite, output.depth )
};
vk::FramebufferCreateInfo info {};
info.renderPass = m_renderpass;
info.setAttachments( attachments );
info.width = m_extent.width;
info.height = m_extent.height;
info.layers = 1;
buffers.emplace_back( Device::getInstance()->createFramebuffer( info ) );
}
return buffers;
}
CameraSwapchain::CameraSwapchain( const vk::Extent2D extent ) :
m_extent( extent ),
m_renderpass( creaeteRenderpass() ),
m_framebuffers( createFrambuffers() )
{}
} // namespace fgl::engine

View File

@@ -0,0 +1,43 @@
//
// Created by kj16609 on 7/11/24.
//
#pragma once
#include "engine/rendering/Attachment.hpp"
#include "engine/rendering/SwapChain.hpp"
namespace fgl::engine
{
class CameraSwapchain
{
struct
{
ColorAttachment position { vk::Format::eR16G16B16A16Sfloat };
ColorAttachment normal { vk::Format::eR16G16B16A16Sfloat };
ColorAttachment albedo { vk::Format::eR8G8B8A8Unorm };
} gbuffer {};
struct
{
//Final attachments
ColorAttachment composite { vk::Format::eR8G8B8A8Unorm };
DepthAttachment depth { SwapChain::findDepthFormat() };
} output {};
vk::Extent2D m_extent;
vk::raii::RenderPass m_renderpass;
std::vector< vk::raii::Framebuffer > m_framebuffers;
public:
vk::raii::RenderPass creaeteRenderpass();
std::vector< vk::raii::Framebuffer > createFrambuffers();
CameraSwapchain( const vk::Extent2D extent );
};
} // namespace fgl::engine

View File

@@ -4,7 +4,7 @@
#include "drawers.hpp"
#include "engine/Camera.hpp"
#include "engine/camera/Camera.hpp"
#include "engine/primitives/boxes/AxisAlignedBoundingBox.hpp"
#include "engine/primitives/boxes/AxisAlignedBoundingCube.hpp"
#include "engine/primitives/boxes/OrientedBoundingBox.hpp"

View File

@@ -19,6 +19,7 @@
#include "engine/model/Model.hpp"
#include "engine/rendering/Device.hpp"
#include "engine/rendering/Renderer.hpp"
#include "engine/tree/octtree/OctTreeNode.hpp"
#include "preview.hpp"
namespace fgl::engine::gui

View File

@@ -21,6 +21,7 @@
#include "engine/model/Model.hpp"
#include "engine/model/builders/SceneBuilder.hpp"
#include "engine/rendering/SwapChain.hpp"
#include "engine/tree/octtree/OctTreeNode.hpp"
namespace fgl::engine::gui
{

View File

@@ -179,6 +179,30 @@ namespace fgl::engine
requires is_wrapped_attachment< T >
using UnwrappedAttachment = std::conditional_t< is_wrapped_attachment< T >, typename T::Attachment, T >;
//Helper functions
template < is_attachment... Attachments >
static std::vector< vk::ImageView > getViewsForFrame( const std::uint8_t frame_idx, Attachments... attachments )
{
std::vector< vk::ImageView > view {};
view.resize( sizeof...( Attachments ) );
( ( view[ attachments.getIndex() ] = *attachments.getView( frame_idx ) ), ... );
return view;
}
template < is_attachment... Attachments >
static std::vector< vk::ClearValue > gatherClearValues( Attachments... attachments )
{
std::vector< vk::ClearValue > clear_values {};
clear_values.resize( sizeof...( Attachments ) );
( ( clear_values[ attachments.getIndex() ] = attachments.m_clear_value ), ... );
return clear_values;
}
using ColoredPresentAttachment = Attachment<
vk::AttachmentLoadOp::eClear,
vk::AttachmentStoreOp::eStore,

View File

@@ -16,6 +16,7 @@
#include "Device.hpp"
#include "SwapChain.hpp"
#include "engine/Window.hpp"
#include "engine/descriptors/DescriptorSet.hpp"
//clang-format: off
#include <tracy/TracyVulkan.hpp>

View File

@@ -11,6 +11,7 @@
#include "RenderPassBuilder.hpp"
#include "Subpass.hpp"
#include "engine/assets/TransferManager.hpp"
#include "engine/descriptors/DescriptorSet.hpp"
namespace fgl::engine
{

View File

@@ -88,28 +88,6 @@ namespace fgl::engine
static vk::PresentModeKHR chooseSwapPresentMode( const std::vector< vk::PresentModeKHR >& present_modes );
vk::Extent2D chooseSwapExtent( const vk::SurfaceCapabilitiesKHR& capabilities ) const;
template < is_attachment... Attachments >
static std::vector< vk::ImageView > getViewsForFrame( const std::uint8_t frame_idx, Attachments... attachments )
{
std::vector< vk::ImageView > view {};
view.resize( sizeof...( Attachments ) );
( ( view[ attachments.getIndex() ] = *attachments.getView( frame_idx ) ), ... );
return view;
}
template < is_attachment... Attachments >
static std::vector< vk::ClearValue > gatherClearValues( Attachments... attachments )
{
std::vector< vk::ClearValue > clear_values {};
clear_values.resize( sizeof...( Attachments ) );
( ( clear_values[ attachments.getIndex() ] = attachments.m_clear_value ), ... );
return clear_values;
}
std::vector< std::unique_ptr< descriptors::DescriptorSet > > createGBufferDescriptors();
std::vector< std::unique_ptr< descriptors::DescriptorSet > > createCompositeDescriptors();

View File

@@ -9,6 +9,7 @@
#include "engine/FrameInfo.hpp"
#include "engine/debug/drawers.hpp"
#include "engine/model/Model.hpp"
#include "engine/tree/octtree/OctTreeNode.hpp"
namespace fgl::engine
{