Vulkan Schnee 0.0.1 (22840dc8)
High-performance rendering engine
Loading...
Searching...
No Matches
Engine.h
1#pragma once
2
3#include "Engine/Asset/AssetHandles.h"
4#include "Engine/Audio/AudioEngine.h"
5#include "Engine/Files/AssetPath.h"
6#include "Engine/Material/RuntimeMaterialLease.h"
7#include "Engine/Mesh/AssetManager.h"
8#include "Engine/Core/TimerManager.h"
9#include "Engine/Entity/EntityContainer.h"
10#include "Engine/Physics/CollisionProfileRegistry.h"
11#include "Engine/Physics/PhysicsEngine.h"
12#include "Engine/World/WindField.h"
13#include "Engine/World/SceneManager.h"
14
15#include <Engine/Core/ApplicationContext.h>
16#include <Engine/Input/XrInputHandler.h>
17#include <vector>
18#include <vulkan/vulkan_core.h>
19#include <future>
20#include <memory>
21#include <atomic>
22
23#include "Engine/Debug/Debugger.h"
24#include "Engine/Input/InputManager.h"
25#include "Engine/Logging/TracyVkContextManager.h"
26
27class NamedThreadPool;
28
29namespace Input
30{
31 class InputManager;
32 class XrRig;
33}
34
35namespace Engine {
36
37 namespace Debug::UI
38 {
39 class ImGuiManager;
40 } // namespace Debug::UI
41 class GameModule;
42
43 namespace Core
44 {
45 class AssetRegistry;
46 class TimerManager;
47 }
48
49 namespace Assets
50 {
51 class Material;
52 }
53
54 namespace Rendering
55 {
56 class Headset;
57 class MirrorView;
58 class Renderer;
59 }
60
67 class EngineKern {
68 public:
73
78
83
88 void run(GameModule& module);
89
97 Asset::MeshPath materialPath,
98 std::unique_ptr<Assets::Material> material
99 );
100
106
111 void setFreezeCulling(bool freeze) const;
112
116 [[nodiscard]] bool isFreezeCulling() const;
117
118 private:
120 double gameTime_ = 0.0f;
121 public:
122
126 [[nodiscard]] double getDeltaTimeSeconds() const;
127
131 [[nodiscard]] double getGlobalTimeSeconds() const;
132
137 [[nodiscard]] Core::ApplicationContext * getContext() const { return applicationContext_.get(); }
138
139 private:
140 double deltaTimeSeconds_ = 0.0;
141
147
148 std::unique_ptr<Core::AssetManager> assetManager_ = nullptr;
149 std::unique_ptr<Core::AssetRegistry> assetRegistry_ = nullptr;
150
151 // Game reference. Owned by the caller; the engine only borrows its address.
152 GameModule* module_ = nullptr;
153
154 std::unique_ptr<Input::InputManager> inputManager_;
155 uint64_t frameCounter_ = 0;
156
161
166 public:
170 [[nodiscard]] uint32_t getRenderableSceneObjectCount() const;
171
176 [[nodiscard]] Rendering::Renderer * getRenderer() const;
177
182 [[nodiscard]] Input::XrInputHandler& getXrInputHandler() const { return inputManager_->getXrInputHandler(); }
183
188 [[nodiscard]] Input::InputManager* getInputManager() const { return inputManager_.get(); }
189
194 [[nodiscard]] Input::XrRig& getXrRig() const { return inputManager_->getXrRig(); }
195
200 [[nodiscard]] Core::TimerManager * getTimerManager() const { return timerManager_.get(); }
201
206 [[nodiscard]] Rendering::Headset * getHeadset() const { return headset_.get(); }
207
211 [[nodiscard]] World::WindField& getWindField() { return windField_; }
212
216 [[nodiscard]] const World::WindField& getWindField() const { return windField_; }
217
222
227 private:
228
230
231 std::unique_ptr<Core::ApplicationContext> applicationContext_;
232 std::unique_ptr<Rendering::MirrorView> mirrorView_;
233 std::unique_ptr<Rendering::Headset> headset_;
234 std::unique_ptr<Rendering::Renderer> renderer_;
235 std::unique_ptr<Debug::UI::ImGuiManager> imguiManager_;
236
237 // Async frame completion for pipelined rendering
238 // This allows xrWaitFrame(N+1) to overlap with GPU work from frame N
239 std::future<void> frameCompletionFuture_;
240 std::atomic<bool> previousFrameXrComplete_{true};
241
242 // After a headset sleep/wake state transition, force several frames to
243 // complete synchronously (endXrFrame on the main thread). This prevents
244 // the engine from rapid-firing frames that saturate the XR compositor
245 // queue and cause xrEndFrame to block indefinitely while the main thread
246 // deadlocks at frameCompletionFuture_.wait().
248
249 // Named thread pool for XR frame operations (shows up properly in Tracy)
250 std::unique_ptr<NamedThreadPool> xrFrameThreadPool_;
251
252 std::unique_ptr<Core::TimerManager> timerManager_;
253
261 void completeFrameAsync(VkFence graphicsFence, bool presentMirror);
262
266 void createStartupScene() const;
267
272
278
279#ifdef ENABLE_TRACY
280 Debug::Tracy::TracyVkContextManager tracyContextManager_;
281#endif
282
287
288 public:
289
294 [[nodiscard]] const std::unique_ptr<Core::SceneManager>& getSceneManager() const { return sceneManager_; }
295 private:
296
301 void calculateFrameTime( double& lastFrameTimeMilliseconds );
302
308 void fixLoadingAssets() const;
309
315
320 void update( double & gameTime );
321
326 static void physicsFrameUpdate();
327
332
337
342 [[nodiscard]] bool isMirrorViewMinimized() const;
343
347 void mainLoop();
348
352 void cleanup();
353
355
356 public:
361 template <typename T>
362 void loadScene() {
363 sceneManager_->loadScene<T>();
364 }
365
371
372 private:
373 std::unique_ptr<Core::SceneManager> sceneManager_ = nullptr;
374
376
380 std::unique_ptr<Core::PhysicsEngine> physicsEngine_ = nullptr;
381 std::unique_ptr<Physics::CollisionProfileRegistry> collisionProfileRegistry_ = nullptr;
382
386 std::unique_ptr<Core::AudioEngine> audioEngine_ = nullptr;
387
390 };
391
396 public:
400 virtual ~EngineManager() = default;
401
406
412
417 void setEngine(EngineKern* engineInstance);
418
419 protected:
421
425 EngineManager() = default;
426
427 EngineManager(const EngineManager&) = delete;
429 };
430
431
432}
Move-only ownership of a material created at runtime.
The application context is the core class which stores the basic openxr and vulkan objects.
Schedules one-shot callbacks against engine time.
Owns the engine runtime and provides access to engine subsystems.
Definition Engine.h:67
static void processMirrorWindowEvents()
processes glfw window events like key presses or window events
void mainLoop()
Main engine loop where the rendering and game steps happen.
std::atomic< bool > previousFrameXrComplete_
Definition Engine.h:240
void update(double &gameTime)
Function which causes all objects to tick.
double deltaTimeSeconds_
Definition Engine.h:140
World::WindField windField_
Definition Engine.h:388
Core::TimerManager * getTimerManager() const
Returns the engine timer manager.
Definition Engine.h:200
uint32_t getRenderableSceneObjectCount() const
Counts ECS entities with static mesh render data.
void unregisterRuntimeMaterial(Assets::MaterialHandle handle)
Unloads a material previously created with registerRuntimeMaterial().
void initVulkan()
Initialisation of all vulkan related resources.
double getGlobalTimeSeconds() const
Returns engine simulation time in seconds.
void updateAudioListener()
Pushes the headset pose into the audio engine's HRTF listener each frame.
void createTimerManager()
Creates the manager for timing by seconds. It is not time accurate. Is a minimum threshold where we w...
std::unique_ptr< Core::AssetRegistry > assetRegistry_
Definition Engine.h:149
void createStartupScene() const
Creates the initial scene the engine loads before an actual level is loaded.
World::WindField & getWindField()
Returns mutable wind-field state used by wind-aware components.
Definition Engine.h:211
std::unique_ptr< Debug::UI::ImGuiManager > imguiManager_
Definition Engine.h:235
Debugger debugger_
Definition Engine.h:119
const std::unique_ptr< Core::SceneManager > & getSceneManager() const
Returns the scene manager.
Definition Engine.h:294
void captureRenderdocFrame()
Captures a frame for render doc if the define RENDERDOC_ENABLED is set.
std::unique_ptr< Core::AudioEngine > audioEngine_
Definition Engine.h:386
void createPhysicsWorld()
PHYSICS.
Physics::CollisionProfileRegistry * getCollisionProfileRegistry() const
Returns the active collision profile registry.
Definition Engine.h:370
bool isMirrorViewMinimized() const
Checks the glfw window attributes if the window is minimized.
std::unique_ptr< Rendering::MirrorView > mirrorView_
Definition Engine.h:232
std::unique_ptr< Rendering::Renderer > renderer_
Definition Engine.h:234
uint64_t frameCounter_
Definition Engine.h:155
void createVulkanDebugMessenger()
void run(GameModule &module)
Starts the engine, runs the main loop, then shuts subsystems down.
void loadScene()
Queues a scene change after the current frame completes.
Definition Engine.h:362
const World::WindField & getWindField() const
Returns read-only wind-field state used by wind-aware components.
Definition Engine.h:216
void completeFrameAsync(VkFence graphicsFence, bool presentMirror)
Runs on background thread: waits for GPU fence, ends XR frame, presents mirror This allows CPU work t...
Rendering::Headset * getHeadset() const
Returns the active XR headset wrapper.
Definition Engine.h:206
void tryCreateApplicationContext()
will try to create a window and if there is no openxr runtime detected it will wait 5 seconds and try...
void createAudioEngine()
AUDIO.
void fixLoadingAssets() const
When assets have been requested but are not loaded yet the static mesh data is in a limbo where it ha...
bool isFreezeCulling() const
Returns whether renderer culling updates are frozen.
std::future< void > frameCompletionFuture_
Definition Engine.h:239
std::unique_ptr< Core::AssetManager > assetManager_
Definition Engine.h:148
Assets::RuntimeMaterialLease registerRuntimeMaterial(Asset::MeshPath materialPath, std::unique_ptr< Assets::Material > material)
Registers a material created at runtime and not loaded from a static asset file.
static void physicsFrameUpdate()
Steps the physics simulation and synchronizes physics body transforms with the ECS....
std::unique_ptr< Core::ApplicationContext > applicationContext_
Definition Engine.h:231
GameModule * module_
Definition Engine.h:152
std::unique_ptr< Core::SceneManager > sceneManager_
Definition Engine.h:373
void setFreezeCulling(bool freeze) const
Enables or disables renderer culling freeze for debug inspection.
int forceSyncFramesRemaining_
Definition Engine.h:247
const Entities::EntityContainer & getPersistentEntities() const
Returns read-only access to entities that persist independently of the active scene.
Definition Engine.h:226
Entities::EntityContainer persistentEntities_
Definition Engine.h:389
void loadEngineConfig()
Loads the engine config and checks whether it exists.
std::unique_ptr< Physics::CollisionProfileRegistry > collisionProfileRegistry_
Definition Engine.h:381
Rendering::Renderer * getRenderer() const
Returns the renderer subsystem.
std::unique_ptr< Input::InputManager > inputManager_
Definition Engine.h:154
void createInputManager()
Manages all kinds of inputs the engine has. Has subclasses for device specific implementations.
int64_t lastPredictedDisplayTime_
Predicted display time of the previous XR frame, used to derive simulation delta time from the runtim...
Definition Engine.h:146
Input::XrInputHandler & getXrInputHandler() const
Returns the XR input handler.
Definition Engine.h:182
void processResourceLoadingPipelines()
Collects and processes the pipelines which have finished and dispatches the new steps....
Input::InputManager * getInputManager() const
Returns the input action manager.
Definition Engine.h:188
void cleanup()
Cleanup of pointers for vulkan and subsystems.
std::unique_ptr< Core::PhysicsEngine > physicsEngine_
Definition Engine.h:380
~EngineKern()
Destroys the engine object after run() has completed cleanup.
Input::XrRig & getXrRig() const
Returns resolved XR rig poses.
Definition Engine.h:194
void calculateFrameTime(double &lastFrameTimeMilliseconds)
Calculates the delta time.
EngineKern()
Creates an uninitialized engine instance.
std::unique_ptr< NamedThreadPool > xrFrameThreadPool_
Definition Engine.h:250
Entities::EntityContainer & getPersistentEntities()
Returns entities that persist independently of the active scene.
Definition Engine.h:221
std::unique_ptr< Core::TimerManager > timerManager_
Definition Engine.h:252
void destroyPhysicsWorld()
std::unique_ptr< Rendering::Headset > headset_
Definition Engine.h:233
Core::ApplicationContext * getContext() const
Returns the Vulkan application context.
Definition Engine.h:137
double getDeltaTimeSeconds() const
Returns the duration of the previous frame in seconds.
static EngineManager & getInstance()
Returns the singleton engine manager instance.
virtual ~EngineManager()=default
Destroys the engine manager interface.
Engine::EngineKern * engine_
Active engine instance, or null when none is registered.
Definition Engine.h:420
EngineKern * getEngineModule()
Returns the active engine.
EngineManager()=default
Creates an empty engine manager.
void setEngine(EngineKern *engineInstance)
Registers the active engine pointer.
EngineManager & operator=(const EngineManager &)=delete
cannot be copy-assigned.
EngineManager(const EngineManager &)=delete
cannot be copied.
Owns entity lifetime and deferred destruction for a scene or subsystem.
Base interface implemented by game-specific engine modules.
Definition GameModule.h:35
Registers collision profile names, enum mappings, and pair responses.
Owns the OpenXR session, XR swapchain, eye transforms, and per-frame XR submission state.
Definition Headset.h:55
Procedural wind field used by glider flight, particles, and debug rendering.
Definition WindField.h:76
Action-based input dispatcher for polled input values and callbacks.
Creates OpenXR controller actions from the profile registry and samples the input state.
Resolves raw XR play-space poses into world-space tracked hand poses.
Definition XrRig.h:44
AssetHandle< MaterialHandleTag > MaterialHandle
Handle to a material payload.
Identifies a named mesh-like asset inside an asset file.
Definition AssetPath.h:89