diff --git a/src/engine/EngineContext.cpp b/src/engine/EngineContext.cpp index 85c03a3..513f8df 100644 --- a/src/engine/EngineContext.cpp +++ b/src/engine/EngineContext.cpp @@ -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() }; diff --git a/src/engine/FrameInfo.hpp b/src/engine/FrameInfo.hpp index 214504c..89f9cde 100644 --- a/src/engine/FrameInfo.hpp +++ b/src/engine/FrameInfo.hpp @@ -10,18 +10,24 @@ #include //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 { diff --git a/src/engine/Camera.cpp b/src/engine/camera/Camera.cpp similarity index 90% rename from src/engine/Camera.cpp rename to src/engine/camera/Camera.cpp index eb42c70..7d432c2 100644 --- a/src/engine/Camera.cpp +++ b/src/engine/camera/Camera.cpp @@ -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 ) { diff --git a/src/engine/Camera.hpp b/src/engine/camera/Camera.hpp similarity index 91% rename from src/engine/Camera.hpp rename to src/engine/camera/Camera.hpp index 26a5b09..bef86fd 100644 --- a/src/engine/Camera.hpp +++ b/src/engine/camera/Camera.hpp @@ -12,11 +12,13 @@ #include #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; } diff --git a/src/engine/camera/CameraSwapchain.cpp b/src/engine/camera/CameraSwapchain.cpp new file mode 100644 index 0000000..f20423b --- /dev/null +++ b/src/engine/camera/CameraSwapchain.cpp @@ -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 \ No newline at end of file diff --git a/src/engine/camera/CameraSwapchain.hpp b/src/engine/camera/CameraSwapchain.hpp new file mode 100644 index 0000000..cd85036 --- /dev/null +++ b/src/engine/camera/CameraSwapchain.hpp @@ -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 \ No newline at end of file diff --git a/src/engine/debug/drawers.cpp b/src/engine/debug/drawers.cpp index 1e046ff..5918526 100644 --- a/src/engine/debug/drawers.cpp +++ b/src/engine/debug/drawers.cpp @@ -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" diff --git a/src/engine/gui/core.cpp b/src/engine/gui/core.cpp index 7bac810..8a226a2 100644 --- a/src/engine/gui/core.cpp +++ b/src/engine/gui/core.cpp @@ -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 diff --git a/src/engine/gui/preview.cpp b/src/engine/gui/preview.cpp index df06c90..4fa32ba 100644 --- a/src/engine/gui/preview.cpp +++ b/src/engine/gui/preview.cpp @@ -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 { diff --git a/src/engine/rendering/Attachment.hpp b/src/engine/rendering/Attachment.hpp index b145c31..84bafa1 100644 --- a/src/engine/rendering/Attachment.hpp +++ b/src/engine/rendering/Attachment.hpp @@ -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, diff --git a/src/engine/rendering/Renderer.cpp b/src/engine/rendering/Renderer.cpp index f0ae9d2..2ea778d 100644 --- a/src/engine/rendering/Renderer.cpp +++ b/src/engine/rendering/Renderer.cpp @@ -16,6 +16,7 @@ #include "Device.hpp" #include "SwapChain.hpp" #include "engine/Window.hpp" +#include "engine/descriptors/DescriptorSet.hpp" //clang-format: off #include diff --git a/src/engine/rendering/SwapChain.cpp b/src/engine/rendering/SwapChain.cpp index 8fb5411..b4547d6 100644 --- a/src/engine/rendering/SwapChain.cpp +++ b/src/engine/rendering/SwapChain.cpp @@ -11,6 +11,7 @@ #include "RenderPassBuilder.hpp" #include "Subpass.hpp" #include "engine/assets/TransferManager.hpp" +#include "engine/descriptors/DescriptorSet.hpp" namespace fgl::engine { diff --git a/src/engine/rendering/SwapChain.hpp b/src/engine/rendering/SwapChain.hpp index 1f7149a..7dcf9ef 100644 --- a/src/engine/rendering/SwapChain.hpp +++ b/src/engine/rendering/SwapChain.hpp @@ -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(); diff --git a/src/engine/systems/CullingSystem.cpp b/src/engine/systems/CullingSystem.cpp index d1a48d8..bd380bb 100644 --- a/src/engine/systems/CullingSystem.cpp +++ b/src/engine/systems/CullingSystem.cpp @@ -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 {