Foundation
Loading...
Searching...
No Matches
Application.hpp
Go to the documentation of this file.
1#pragma once
2#include <Core/Core.hpp>
4
5#include <Async/Future.hpp>
6#include <Async/Thread.hpp>
8
10#include <RHICore/Common.hpp>
12
13#include <Atomics/Atomic.hpp>
18{
19 using namespace Core;
20 using namespace RenderCore;
25 {
26 size_t deviceIndex{0};
27 String windowTitle{"Application"};
37 bool initOnResize{false};
42 .enableAsyncCompute = true,
43 .enablePresent = true,
44 .numRenderThreads = std::max(1u, std::thread::hardware_concurrency() - 1)
45 };
46 };
51 {
52 void Execute();
53 void CreateSwapchain();
54 void InitializeInternal();
55 void InitializeRenderer();
56 void RenderWorker();
57
58 public:
66 {
67 // [Frame Number, Perf Counter [ns]]
68 using FTick = glm::vec<2, size_t>;
69 // States
71 FTick Tick(FTick const& t)
72 {
73 if (t.x <= begin.x)
74 {
75 begin = t; // Reset
76 return delta;
77 }
78 delta = t - begin;
79 begin = t;
80 return delta;
81 }
82 // Returns the average FPS over the last sample duration.
83 [[nodiscard]] size_t GetFPS() const
84 {
85 if (delta.y == 0)
86 return 0;
87 return static_cast<size_t>(1e9 * delta.x / delta.y);
88 }
89 const size_t kTimingSampleDuration{static_cast<size_t>(1.0 * 1e9) /* 1 second */};
90 };
91
92 protected:
94
98
101
104 // The render thread. This is started in RunForever(),
105 // and owns the Renderer instance and therefore all device resources.
106 // Window events are also polled here - doing so on main thread seperately could make
107 // @ref OnBeforeFrame and @ref OnAfterFrame usage difficult.
111
112 // Should the Render thread reset the renderer on the next frame?
114 // Has the Render thread started?
116 // Should the application exit?
117 bool mAppShouldClose{false};
128 virtual void OnDeviceSetup() { /* nop */ }
141 virtual void OnBeforeFrame() { /* nop */ }
154 virtual void OnAfterFrame() { /* nop */ }
169 virtual void OnRendererSetup() = 0;
180 virtual void OnRendererPostSetup() { /* nop */ }
196 virtual void OnApplicationTick() { /* nop */ }
197
198 public:
204 template <typename Backend, typename... Args>
205 void Initialize(ApplicationInitDesc const& desc = {}, Args&&... args)
206 {
207 // XXX: Backends are expected to take Allocator* as the first argument
208 mDesc = desc;
209 mRHI.reset();
210 mRHI =
212 }
217 {
218 auto [w, h] = mWindow.GetFramebufferSize();
219 return {w, h};
220 }
224 [[nodiscard]] FrameTiming GetTiming() const { return mTiming; }
228 [[nodiscard]] Renderer* GetRenderer() const { return mRenderer.get(); }
233 {
234 CHECK_MSG(mSwapchain, "No swapchain created. Did you initialize the application with present=true?");
235 return mSwapchain.Get();
236 }
255 void RunForever();
259 void WaitForFrame();
273 void Shutdown();
274 };
275} // namespace Foundation::Rendering
#define CHECK_MSG(expr, format_str,...)
Definition Logging.hpp:31
General Purpose Allocator (GPA) interface.
Definition Allocator.hpp:24
Allocator * Ptr()
Definition Allocator.hpp:38
General purpose heap allocator.
Definition HeapAllocator.hpp:11
Application base class. Handles initialization and shutdown of the native platform,...
Definition Application.hpp:48
Class representing a window for the platform.
Definition Application.hpp:15
Pair< uint32_t, uint32_t > GetFramebufferSize() const
Definition Application.cpp:28
Definition Swapchain.hpp:19
Renderer implementing a Frame Graph system with automatic resource tracking and synchronization.
Definition Renderer.hpp:78
Template base class for rendering applications.
Definition Application.hpp:51
Async::Thread mRenderThread
Definition Application.hpp:108
Allocator * GetAllocator()
Retrieve the allocator used for general application allocations.
Definition Application.hpp:244
void ResetRendererOnNextFrame()
Reset the renderer on the next frame, calling OnRendererSetup internally.
Definition Application.cpp:151
virtual void OnApplicationTick()
Action to take on every application tick.
Definition Application.hpp:196
ApplicationInitDesc mDesc
Definition Application.hpp:93
RHISwapchain * GetSwapchain() const
Retrieve the current RHISwapchain instance.
Definition Application.hpp:232
void Initialize(ApplicationInitDesc const &desc={}, Args &&... args)
Initialize the application with the specified RHI backend.
Definition Application.hpp:205
RHIExtent2D GetFramebufferSize() const
Retrieve the framebuffer size of the current window.
Definition Application.hpp:216
void WaitForFrame()
Wait for the render thread to start, or the next frame to be rendered.
Definition Application.cpp:146
FrameTiming mTiming
Definition Application.hpp:103
virtual void OnAfterFrame()
Action to take after each frame is executed.
Definition Application.hpp:154
void InitializeRenderer()
Definition Application.cpp:36
void CreateSwapchain()
Definition Application.cpp:17
void Shutdown()
Flag the application to exit.
Definition Application.cpp:152
virtual void OnRendererPostSetup()
Action to take after the renderer has been set up.
Definition Application.hpp:180
Renderer * GetRenderer() const
Retrieve the underlying Renderer instance.
Definition Application.hpp:228
bool mRenderThreadStarted
Definition Application.hpp:115
RHIApplicationScopedObjectHandle< RHIDevice > mDevice
Definition Application.hpp:99
virtual void OnRendererSetup()=0
Set up the renderer by creating passes, resources, and other configurations.
Async::Mutex mRenderMutex
Definition Application.hpp:110
virtual void OnDeviceSetup()
Actions to take after device specific resources has been set up.
Definition Application.hpp:128
Native::NativeWindow * GetNativeWindow()
Retrieve the current NativeWindow instance.
Definition Application.hpp:240
RHIDeviceScopedObjectHandle< RHISwapchain > mSwapchain
Definition Application.hpp:100
Allocator * GetRendererAllocator()
Retrieve the allocator used for renderer allocations.
Definition Application.hpp:248
DefaultAllocator mAlloc
Definition Application.hpp:95
void Execute()
Definition Application.cpp:64
UniquePtr< Renderer > mRenderer
Definition Application.hpp:102
bool mRenderThreadReset
Definition Application.hpp:113
bool mAppShouldClose
Definition Application.hpp:117
Native::NativeWindow mWindow
Definition Application.hpp:96
UniquePtr< RHIApplication > mRHI
Definition Application.hpp:97
void RunForever()
Start the Render thread and run the application loop indefinitely, until the window is closed or the ...
Definition Application.cpp:127
Async::Condition mRenderFrame
Definition Application.hpp:109
void RenderWorker()
Definition Application.cpp:80
virtual void OnBeforeFrame()
Action to take before each frame is executed.
Definition Application.hpp:141
void InitializeInternal()
Definition Application.cpp:49
DefaultAllocator mAllocRenderer
Definition Application.hpp:95
FrameTiming GetTiming() const
Retrieve the timing information of the last frame.
Definition Application.hpp:224
std::condition_variable Condition
Definition Future.hpp:14
std::jthread Thread
Alias of std::jthread.
Definition Thread.hpp:10
std::mutex Mutex
Definition Future.hpp:15
std::basic_string< char > String
Alias for std::basic_string<char>, without an explicit allocator constructor.
Definition Container.hpp:141
std::unique_ptr< T, StlDeleter< T > > UniquePtr
std::unique_ptr with custom deleter that uses a Foundation::Core::Allocator to deallocate memory.
Definition Allocator.hpp:161
glm::vec< 2, uint32_t > RHIExtent2D
Definition Common.hpp:10
Reference implementations of real-time rendering routines.
Definition Application.hpp:18
Parameters for Renderer creation.
Definition Renderer.hpp:17
bool enableAsyncCompute
Enable Async Compute support.
Definition Renderer.hpp:32
Initialization parameters for RenderApplication.
Definition Application.hpp:25
RHIExtent2D windowSize
Definition Application.hpp:28
size_t deviceIndex
Definition Application.hpp:26
RendererDesc renderer
Parameters for the initializing the underlying Renderer.
Definition Application.hpp:41
String windowTitle
Definition Application.hpp:27
bool initOnResize
Reinitialize the renderer when the window is resized.
Definition Application.hpp:37
Rolling frame timing information for performance measurements.
Definition Application.hpp:66
glm::vec< 2, size_t > FTick
Definition Application.hpp:68
size_t GetFPS() const
Definition Application.hpp:83
FTick begin
Definition Application.hpp:70
const size_t kTimingSampleDuration
Definition Application.hpp:89
FTick delta
Definition Application.hpp:70
FTick Tick(FTick const &t)
Definition Application.hpp:71