Transfer editor specific code to a separate binary
This commit is contained in:
@@ -12,87 +12,149 @@
|
||||
#include <iostream>
|
||||
|
||||
#include "KeyboardMovementController.hpp"
|
||||
#include "assets/stores.hpp"
|
||||
#include "buffers/HostSingleT.hpp"
|
||||
#include "camera/Camera.hpp"
|
||||
#include "camera/CameraManager.hpp"
|
||||
#include "engine/Average.hpp"
|
||||
#include "engine/assets/TransferManager.hpp"
|
||||
#include "engine/buffers/UniqueFrameSuballocation.hpp"
|
||||
#include "engine/debug/drawers.hpp"
|
||||
#include "engine/literals/size.hpp"
|
||||
#include "engine/model/prebuilt/terrainModel.hpp"
|
||||
#include "gui/core.hpp"
|
||||
#include "model/builders/SceneBuilder.hpp"
|
||||
|
||||
namespace fgl::engine
|
||||
{
|
||||
constexpr float MAX_DELTA_TIME { 0.5 };
|
||||
|
||||
EngineContext::EngineContext()
|
||||
EngineContext::EngineContext() :
|
||||
m_ubo_buffer_pool( 512_KiB, vk::BufferUsageFlagBits::eUniformBuffer, vk::MemoryPropertyFlagBits::eHostVisible ),
|
||||
m_matrix_info_pool(
|
||||
512_KiB,
|
||||
vk::BufferUsageFlagBits::eVertexBuffer,
|
||||
vk::MemoryPropertyFlagBits::eDeviceLocal | vk::MemoryPropertyFlagBits::eHostVisible ),
|
||||
m_draw_parameter_pool(
|
||||
16_KiB,
|
||||
vk::BufferUsageFlagBits::eIndirectBuffer,
|
||||
vk::MemoryPropertyFlagBits::eDeviceLocal | vk::MemoryPropertyFlagBits::eHostVisible ),
|
||||
m_delta_time_ms( 0.0 )
|
||||
{
|
||||
ZoneScoped;
|
||||
using namespace fgl::literals::size_literals;
|
||||
|
||||
memory::TransferManager::createInstance( device, 512_MiB );
|
||||
|
||||
#if ENABLE_IMGUI
|
||||
initImGui();
|
||||
#endif
|
||||
loadGameObjects();
|
||||
memory::TransferManager::createInstance( device, 128_MiB );
|
||||
}
|
||||
|
||||
static Average< float, 60 * 15 > rolling_ms_average;
|
||||
|
||||
void EngineContext::processInput()
|
||||
{
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
void EngineContext::tickDeltaTime()
|
||||
{
|
||||
// Get delta time
|
||||
const auto now { std::chrono::high_resolution_clock::now() };
|
||||
const std::chrono::duration< double, std::micro > time_diff { last_tick - now };
|
||||
last_tick = now;
|
||||
|
||||
m_delta_time_ms = time_diff.count() * 1000.0f;
|
||||
}
|
||||
|
||||
void EngineContext::tickSimulation()
|
||||
{
|
||||
// TODO: This is where we'll start doing physics stuff.
|
||||
// The first step here should be culling things that aren't needed to be ticked.
|
||||
// Perhaps implementing a tick system that doesn't care about the refresh rate might be good?
|
||||
// That way we can still tick consistantly without actually needing to render anything.
|
||||
}
|
||||
|
||||
void EngineContext::renderCameras( FrameInfo frame_info )
|
||||
{
|
||||
auto& camera_manager { CameraManager::getInstance() };
|
||||
for ( auto& current_camera_ptr : camera_manager.getCameras() )
|
||||
{
|
||||
if ( current_camera_ptr.expired() ) continue;
|
||||
|
||||
auto sh_camera { current_camera_ptr.lock() };
|
||||
|
||||
Camera& current_camera { *sh_camera };
|
||||
|
||||
current_camera.pass( frame_info );
|
||||
}
|
||||
}
|
||||
|
||||
void EngineContext::renderFrame()
|
||||
{
|
||||
if ( auto& command_buffer = m_renderer.beginFrame(); *command_buffer )
|
||||
{
|
||||
ZoneScopedN( "Render" );
|
||||
const FrameIndex frame_index { m_renderer.getFrameIndex() };
|
||||
const PresentIndex present_idx { m_renderer.getPresentIndex() };
|
||||
|
||||
auto& camera_manager { CameraManager::getInstance() };
|
||||
|
||||
FrameInfo frame_info { frame_index,
|
||||
present_idx,
|
||||
m_delta_time_ms,
|
||||
command_buffer,
|
||||
nullptr, // Camera
|
||||
camera_manager.getCameras(),
|
||||
// global_descriptor_sets[ frame_index ],
|
||||
m_game_objects_root,
|
||||
m_renderer.getCurrentTracyCTX(),
|
||||
m_matrix_info_pool,
|
||||
m_draw_parameter_pool,
|
||||
*this->m_vertex_buffer,
|
||||
*this->m_index_buffer,
|
||||
m_renderer.getSwapChain().getInputDescriptor( present_idx ),
|
||||
this->m_renderer.getSwapChain() };
|
||||
|
||||
TracyVkCollect( frame_info.tracy_ctx, *command_buffer );
|
||||
|
||||
//TODO: Setup semaphores to make this pass not always required.
|
||||
memory::TransferManager::getInstance().recordOwnershipTransferDst( command_buffer );
|
||||
|
||||
//TODO: Add some way of 'activating' cameras. We don't need to render cameras that aren't active.
|
||||
renderCameras( frame_info );
|
||||
|
||||
m_renderer.clearInputImage( command_buffer );
|
||||
|
||||
//primary_camera
|
||||
// .copyOutput( command_buffer, frame_index, m_renderer.getSwapChain().getInputImage( present_idx ) );
|
||||
|
||||
m_renderer.beginSwapchainRendererPass( command_buffer );
|
||||
|
||||
m_gui_system.pass( frame_info );
|
||||
|
||||
// TODO: Implement some way we can record extra things into the command buffer during this stage.
|
||||
// We'll probably just use multiple command buffers and allow the caller to pass some in with flags on where to put them
|
||||
renderGui( frame_info );
|
||||
|
||||
m_renderer.endSwapchainRendererPass( command_buffer );
|
||||
|
||||
m_renderer.endFrame();
|
||||
|
||||
memory::TransferManager::getInstance().dump();
|
||||
|
||||
FrameMark;
|
||||
}
|
||||
}
|
||||
|
||||
Window& EngineContext::getWindow()
|
||||
{
|
||||
return m_window;
|
||||
}
|
||||
|
||||
float EngineContext::getWindowAspectRatio()
|
||||
{
|
||||
return m_renderer.getAspectRatio();
|
||||
}
|
||||
|
||||
void EngineContext::run()
|
||||
{
|
||||
TracyCZoneN( TRACY_PrepareEngine, "Inital Run", true );
|
||||
std::cout << "Starting main loop run" << std::endl;
|
||||
memory::Buffer global_ubo_buffer { 512_KiB,
|
||||
vk::BufferUsageFlagBits::eUniformBuffer,
|
||||
vk::MemoryPropertyFlagBits::eHostVisible }; // 512 KB
|
||||
|
||||
//Camera prep
|
||||
|
||||
CameraManager camera_manager {};
|
||||
|
||||
PerFrameSuballocation< HostSingleT< PointLight > > point_lights { global_ubo_buffer,
|
||||
SwapChain::MAX_FRAMES_IN_FLIGHT };
|
||||
|
||||
std::shared_ptr< Texture > debug_tex {
|
||||
getTextureStore().load( "assets/textures/DebugTexture.png", vk::Format::eR8G8B8A8Unorm )
|
||||
};
|
||||
|
||||
constexpr std::uint32_t matrix_default_size { 64_MiB };
|
||||
constexpr std::uint32_t draw_parameter_default_size { 64_MiB };
|
||||
|
||||
std::vector< memory::Buffer > matrix_info_buffers {};
|
||||
|
||||
std::vector< memory::Buffer > draw_parameter_buffers {};
|
||||
|
||||
// std::vector< descriptors::DescriptorSet > global_descriptor_sets {};
|
||||
|
||||
for ( int i = 0; i < SwapChain::MAX_FRAMES_IN_FLIGHT; ++i )
|
||||
{
|
||||
matrix_info_buffers.emplace_back(
|
||||
matrix_default_size,
|
||||
vk::BufferUsageFlagBits::eVertexBuffer,
|
||||
vk::MemoryPropertyFlagBits::eDeviceLocal | vk::MemoryPropertyFlagBits::eHostVisible );
|
||||
|
||||
draw_parameter_buffers.emplace_back(
|
||||
draw_parameter_default_size,
|
||||
vk::BufferUsageFlagBits::eIndirectBuffer,
|
||||
vk::MemoryPropertyFlagBits::eDeviceLocal | vk::MemoryPropertyFlagBits::eHostVisible );
|
||||
|
||||
// global_descriptor_sets.emplace_back( GlobalDescriptorSet::createLayout() );
|
||||
}
|
||||
|
||||
// for ( std::uint8_t i = 0; i < SwapChain::MAX_FRAMES_IN_FLIGHT; ++i )
|
||||
// {
|
||||
// global_descriptor_sets[ i ].setMaxIDX( 2 );
|
||||
// global_descriptor_sets[ i ].bindUniformBuffer( 2, point_lights[ i ] );
|
||||
// global_descriptor_sets[ i ].update();
|
||||
// }
|
||||
|
||||
auto viewer { GameObject::createGameObject() };
|
||||
|
||||
@@ -104,26 +166,11 @@ namespace fgl::engine
|
||||
|
||||
auto previous_frame_start { std::chrono::high_resolution_clock::now() };
|
||||
|
||||
//camera.setOrthographicProjection( -aspect, aspect, -1, 1, -1, 1 );
|
||||
const float aspect { m_renderer.getAspectRatio() };
|
||||
|
||||
auto& editor_camera { camera_manager.getPrimary() };
|
||||
// auto secondary_camera { camera_manager.createCamera( { 1920, 1080 } ) };
|
||||
|
||||
editor_camera
|
||||
.setPerspectiveProjection( glm::radians( 90.0f ), aspect, constants::NEAR_PLANE, constants::FAR_PLANE );
|
||||
// secondary_camera
|
||||
// ->setPerspectiveProjection( glm::radians( 90.0f ), aspect, constants::NEAR_PLANE, constants::FAR_PLANE );
|
||||
|
||||
const auto old_aspect_ratio { m_renderer.getAspectRatio() };
|
||||
|
||||
camera_controller.moveInPlaneXZ( m_window.window(), 0.0, viewer );
|
||||
editor_camera.setView( viewer.getPosition(), viewer.getRotation() );
|
||||
// secondary_camera->setView( viewer.getPosition(), viewer.getRotation() );
|
||||
|
||||
TracyCZoneEnd( TRACY_PrepareEngine );
|
||||
|
||||
while ( !m_window.shouldClose() )
|
||||
while ( good() )
|
||||
{
|
||||
memory::TransferManager::getInstance().submitNow();
|
||||
|
||||
@@ -132,88 +179,14 @@ namespace fgl::engine
|
||||
|
||||
const auto new_time { std::chrono::high_resolution_clock::now() };
|
||||
|
||||
{
|
||||
//Calculate time change from previous frame and add to accumulator
|
||||
const auto time_diff { new_time - previous_frame_start };
|
||||
rolling_ms_average.push(
|
||||
static_cast< float >( std::chrono::duration_cast< std::chrono::microseconds >( time_diff ).count() )
|
||||
/ 1000.0f );
|
||||
previous_frame_start = new_time;
|
||||
}
|
||||
|
||||
auto delta_time { std::chrono::duration< float >( new_time - current_time ).count() };
|
||||
|
||||
current_time = new_time;
|
||||
delta_time = glm::min( delta_time, MAX_DELTA_TIME );
|
||||
|
||||
if ( old_aspect_ratio != m_renderer.getAspectRatio() )
|
||||
{
|
||||
editor_camera.setPerspectiveProjection(
|
||||
glm::radians( 90.0f ), m_renderer.getAspectRatio(), constants::NEAR_PLANE, constants::FAR_PLANE );
|
||||
}
|
||||
|
||||
camera_controller.moveInPlaneXZ( m_window.window(), delta_time, viewer );
|
||||
editor_camera.setView( viewer.getPosition(), viewer.getRotation() );
|
||||
|
||||
if ( auto& command_buffer = m_renderer.beginFrame(); *command_buffer )
|
||||
{
|
||||
ZoneScopedN( "Render" );
|
||||
const FrameIndex frame_index { m_renderer.getFrameIndex() };
|
||||
const PresentIndex present_idx { m_renderer.getPresentIndex() };
|
||||
|
||||
FrameInfo frame_info { frame_index,
|
||||
present_idx,
|
||||
delta_time,
|
||||
command_buffer,
|
||||
{ nullptr, viewer.getTransform() },
|
||||
camera_manager.getCameras(),
|
||||
// global_descriptor_sets[ frame_index ],
|
||||
m_game_objects_root,
|
||||
m_renderer.getCurrentTracyCTX(),
|
||||
matrix_info_buffers[ frame_index ],
|
||||
draw_parameter_buffers[ frame_index ],
|
||||
*this->m_vertex_buffer,
|
||||
*this->m_index_buffer,
|
||||
m_renderer.getSwapChain().getInputDescriptor( present_idx ),
|
||||
this->m_renderer.getSwapChain() };
|
||||
|
||||
#if TRACY_ENABLE
|
||||
//auto& tracy_ctx { frame_info.tracy_ctx };
|
||||
#endif
|
||||
|
||||
TracyVkCollect( frame_info.tracy_ctx, *command_buffer );
|
||||
|
||||
//TODO: Setup semaphores to make this pass not always required.
|
||||
memory::TransferManager::getInstance().recordOwnershipTransferDst( command_buffer );
|
||||
|
||||
for ( auto& current_camera_ptr : camera_manager.getCameras() )
|
||||
{
|
||||
if ( current_camera_ptr.expired() ) continue;
|
||||
|
||||
auto sh_camera { current_camera_ptr.lock() };
|
||||
|
||||
Camera& current_camera { *sh_camera };
|
||||
|
||||
current_camera.pass( frame_info );
|
||||
}
|
||||
|
||||
m_renderer.clearInputImage( command_buffer );
|
||||
|
||||
//primary_camera
|
||||
// .copyOutput( command_buffer, frame_index, m_renderer.getSwapChain().getInputImage( present_idx ) );
|
||||
|
||||
m_renderer.beginSwapchainRendererPass( command_buffer );
|
||||
|
||||
m_gui_system.pass( frame_info );
|
||||
|
||||
m_renderer.endSwapchainRendererPass( command_buffer );
|
||||
|
||||
m_renderer.endFrame();
|
||||
|
||||
memory::TransferManager::getInstance().dump();
|
||||
|
||||
FrameMark;
|
||||
}
|
||||
renderFrame();
|
||||
|
||||
using namespace std::chrono_literals;
|
||||
std::this_thread::sleep_for( 13ms );
|
||||
@@ -247,18 +220,21 @@ namespace fgl::engine
|
||||
log::info( "Finished loading game object" );
|
||||
}
|
||||
|
||||
void EngineContext::initImGui()
|
||||
{
|
||||
#if ENABLE_IMGUI
|
||||
gui::initGui( m_window, m_renderer );
|
||||
#endif
|
||||
}
|
||||
|
||||
EngineContext::~EngineContext()
|
||||
{
|
||||
#if ENABLE_IMGUI
|
||||
#if EXPOSE_IMGUI_FUNCS
|
||||
gui::cleanupImGui();
|
||||
#endif
|
||||
}
|
||||
|
||||
bool EngineContext::good()
|
||||
{
|
||||
return !m_window.shouldClose();
|
||||
}
|
||||
|
||||
void EngineContext::handleTransfers()
|
||||
{
|
||||
memory::TransferManager::getInstance().submitNow();
|
||||
}
|
||||
|
||||
} // namespace fgl::engine
|
||||
|
||||
Reference in New Issue
Block a user