SGCT master (487fac4)
Simple Graphics Cluster Toolkit
Loading...
Searching...
No Matches
engine.h
Go to the documentation of this file.
1/*****************************************************************************************
2 * SGCT *
3 * Simple Graphics Cluster Toolkit *
4 * *
5 * Copyright (c) 2012-2026 *
6 * For conditions of distribution and use, see copyright notice in LICENSE.md *
7 ****************************************************************************************/
8
9#ifndef __SGCT__ENGINE__H__
10#define __SGCT__ENGINE__H__
11
12#include <sgct/sgctexports.h>
13
14#include <sgct/actions.h>
15#include <sgct/callbackdata.h>
16#include <sgct/config.h>
17#include <sgct/keys.h>
18#include <sgct/math.h>
19#include <sgct/modifiers.h>
20#include <sgct/mouse.h>
21#include <sgct/window.h>
22#include <algorithm>
23#include <array>
24#include <cstddef>
25#include <cstdint>
26#include <filesystem>
27#include <memory>
28#include <optional>
29#include <string>
30#include <string_view>
31#include <thread>
32#include <utility>
33#include <vector>
34
35namespace sgct {
36
37struct Configuration;
38class Node;
40class User;
41
57 std::optional<std::filesystem::path> path = std::nullopt);
58
65SGCT_EXPORT double time();
66
71class SGCT_EXPORT Engine {
72public:
73 using DrawFunction = void (*)(const RenderData&);
74
81 struct SGCT_EXPORT Statistics {
84 static constexpr int HistoryLength = 128;
85
87 std::array<double, HistoryLength> frametimes = {};
88
90 std::array<double, HistoryLength> drawTimes = {};
91
93 std::array<double, HistoryLength> syncTimes = {};
94
96 std::array<double, HistoryLength> loopTimeMin = {};
97
99 std::array<double, HistoryLength> loopTimeMax = {};
100
104 double dt() const;
105
109 double avgDt() const;
110
114 double minDt() const;
115
119 double maxDt() const;
120 };
121
122 struct SGCT_EXPORT Settings {
126 bool createDebugContext = false;
127
133 int8_t swapInterval = 1;
134
138 bool printSyncMessage = true;
139
142 bool captureBackBuffer = false;
143
144 bool useDepthTexture = false;
145 bool useNormalTexture = false;
146 bool usePositionTexture = false;
147
150 float syncTimeout = 60.f;
151
152 struct SS {
154 std::filesystem::path capturePath;
155
156 // The number of capture threads
157 int nCaptureThreads = std::max(std::thread::hardware_concurrency() / 2, 1u);
158
160 bool addNodeName = false;
161
163 bool addWindowName = true;
164
166 std::string prefix;
167
172 std::optional<std::pair<uint64_t, uint64_t>> limits;
173 } capture;
174 };
175
180 struct SGCT_EXPORT Callbacks {
184 void (*preWindow)() = nullptr;
185
189 void (*initOpenGL)(GLFWwindow*) = nullptr;
190
192 void (*preSync)() = nullptr;
193
195 void (*postSyncPreDraw)() = nullptr;
196
199 void (*draw)(const RenderData&) = nullptr;
200
203 void (*draw2D)(const RenderData&) = nullptr;
204
206 void (*postDraw)() = nullptr;
207
210 void (*cleanup)() = nullptr;
211
214 std::vector<std::byte> (*encode)() = nullptr;
215
218 void (*decode)(const std::vector<std::byte>&) = nullptr;
219
221 void (*externalDecode)(const char*, int) = nullptr;
222
224 void (*externalStatus)(bool) = nullptr;
225
227 void (*dataTransferDecode)(void*, int, int, int) = nullptr;
228
230 void (*dataTransferStatus)(bool, int) = nullptr;
231
233 void (*dataTransferAcknowledge)(int, int) = nullptr;
234
236 void (*keyboard)(Key, Modifier, Action, int, Window*) = nullptr;
237
239 void (*character)(unsigned int, int, Window*) = nullptr;
240
243
245 void (*mousePos)(double, double, Window*) = nullptr;
246
248 void (*mouseScroll)(double, double, Window*) = nullptr;
249
251 void (*drop)(const std::vector<std::string_view>&) = nullptr;
252 };
253
265 static Engine& instance();
266
282 static void create(config::Cluster cluster, Callbacks callbacks,
283 const Configuration& arg);
284
290 static void destroy();
291
296 void terminate();
297
304 void exec();
305
313 const Statistics& statistics() const;
314
320 float nearClipPlane() const;
321
327 float farClipPlane() const;
328
336 void setNearAndFarClippingPlanes(float nearClippingPlane, float farClippingPlane);
337
342 void updateFrustums() const;
343
350 const Window* focusedWindow() const;
351
358 void setStatsGraphVisibility(bool value);
359
367 float statsGraphScale() const;
368
376 void setStatsGraphScale(float scale);
377
386
396
411 void takeScreenshot(std::vector<int> windowIds = std::vector<int>());
412
417
424 void setScreenshotNumber(unsigned int number);
425
432 unsigned int screenShotNumber() const;
433
442
444
445
451 const Node& thisNode() const;
452
460 const std::vector<std::unique_ptr<Window>>& windows() const;
461
467 static User& defaultUser();
468
476 bool isMaster() const;
477
484 unsigned int currentFrameNumber() const;
485
491 void setCapturePath(std::filesystem::path path);
492
497 void setCaptureFromBackBuffer(bool state);
498
500
501 const Settings& settings() const;
502
503private:
509 static Engine* _instance;
510
519 Engine(config::Cluster cluster, Callbacks callbacks, const Configuration& config);
520
524 ~Engine();
525
530 void initialize();
531
536 void frameLockPreStage();
537
542 void frameLockPostStage();
543
550 void waitForAllWindowsInSwapGroupToOpen();
551
553 void (*_preWindowFn)() = nullptr;
554
556 void (*_initOpenGLFn)(GLFWwindow*) = nullptr;
557
559 void (*_preSyncFn)() = nullptr;
560
562 void (*_postSyncPreDrawFn)() = nullptr;
563
565 void (*_drawFn)(const RenderData&) = nullptr;
566
568 void (*_draw2DFn)(const RenderData&) = nullptr;
569
571 void (*_postDrawFn)() = nullptr;
572
574 void (*_cleanupFn)() = nullptr;
575
578 float _nearClipPlane = 0.1f;
579
582 float _farClipPlane = 100.f;
583
585 Statistics _statistics;
586
588 double _statsPrevTimestamp = 0.0;
589
592 std::unique_ptr<StatisticsRenderer> _statisticsRenderer;
593
595 bool _shouldTakeScreenshot = false;
596
598 bool _shouldTerminate = false;
599
602 std::vector<int> _shouldTakeScreenshotIds;
603
604 Settings _settings;
605
606 std::unique_ptr<std::thread> _thread;
607
608 unsigned int _frameCounter = 0;
609 unsigned int _shotCounter = 0;
610};
611
612} // namespace sgct
613
614#endif // __SGCT__ENGINE__H__
void exec()
This function starts the SGCT render loop in which the rendering, synchronization,...
const Settings & settings() const
unsigned int currentFrameNumber() const
Returns the number of the current frame.
void takeScreenshot(std::vector< int > windowIds=std::vector< int >())
Takes an RGBA screenshot and saves it as a PNG file.
float statsGraphScale() const
Returns the current scaling value used to render the statistics graphs if they are enabled.
bool isMaster() const
Returns if this Node is the master in a clustered environment.
void setScreenshotNumber(unsigned int number)
Sets the number that the next screenshot will recieve with the next call of takeScreenshot.
void(*)(const RenderData &) DrawFunction
Definition engine.h:73
void setCaptureFromBackBuffer(bool state)
Set if capture should capture warped from backbuffer instead of texture.
static void create(config::Cluster cluster, Callbacks callbacks, const Configuration &arg)
Creates the singleton Engine that is accessible through the Engine::instance function.
void resetScreenshotNumber()
Resets the screenshot number to 0.
vec2 statsGraphOffset() const
Returns the current offset value used to render the statistics graphics away from the center of the s...
void setStatsGraphOffset(vec2 offset)
Sets the new offet value for the statistics graph rendering.
Engine::DrawFunction drawFunction() const
This function returns the draw function to be used in internal classes that need to repeatedly call t...
StatisticsRenderer * statisticsRenderer()
const Node & thisNode() const
Returns a reference to the node that represents this computer.
const Statistics & statistics() const
Returns the Engine::Statistics object that contains all collected information about the frametimes,...
void setStatsGraphVisibility(bool value)
Determines whether the graph displaying the rendering stats is being displayed or not.
void terminate()
Signals to SGCT that the application should be terminated at the end of the next frame.
void setNearAndFarClippingPlanes(float nearClippingPlane, float farClippingPlane)
Set the near and far clipping planes.
void setStatsGraphScale(float scale)
Sets the new scaling value for the statistics graph rendering.
void setCapturePath(std::filesystem::path path)
Set capture/screenshot path used by SGCT.
unsigned int screenShotNumber() const
Returns the number the next screenshot will receive upon the next call of takeScreenshot.
const std::vector< std::unique_ptr< Window > > & windows() const
Returns a list of all windows for the current node.
void updateFrustums() const
This functions updates the frustum of all viewports.
float nearClipPlane() const
Returns the distance to the near clipping plane in meters.
Engine::DrawFunction draw2DFunction() const
static User & defaultUser()
Returns a pointer to the default user, i.e.
const Window * focusedWindow() const
Return the Window that currently has the focus.
static void destroy()
Destroys the singleton Engine instance that was created by Engine::create and that is accessible thro...
float farClipPlane() const
Returns the distance to the far clipping plane in meters.
static Engine & instance()
Returns the global Engine object that is created through the Engine::create function.
Definition node.h:24
Definition statisticsrenderer.h:23
Helper class for setting user variables.
Definition user.h:24
Definition window.h:46
Definition clustermanager.h:22
Definition actions.h:16
double time()
Returns the number of seconds since the program start.
Action
Definition actions.h:18
MouseButton
Definition mouse.h:18
Key
Definition keys.h:18
Modifier
Definition modifiers.h:18
config::Cluster loadCluster(std::optional< std::filesystem::path > path=std::nullopt)
Loads the cluster information from the provided path.
Definition commandline.h:22
This struct holds all of the callback functions that can be used by the client library to be called d...
Definition engine.h:180
void(*) draw2D(const RenderData &)
This function is be called after overlays and post effects has been drawn and can used to render text...
Definition engine.h:203
void(*) dataTransferStatus(bool, int)
This function is called when the connection status changes.
Definition engine.h:230
void(*) cleanup()
This is called before all SGCT components will be destroyed.
Definition engine.h:210
void(*) drop(const std::vector< std::string_view > &)
Drop files to any window. All windows are connected to this callback.
Definition engine.h:251
void(*) keyboard(Key, Modifier, Action, int, Window *)
This function sets the keyboard callback (GLFW wrapper) for all windows.
Definition engine.h:236
void(*) dataTransferDecode(void *, int, int, int)
This function is called when a TCP message is received.
Definition engine.h:227
void(*) mouseScroll(double, double, Window *)
All windows are connected to this callback.
Definition engine.h:248
void(*) draw(const RenderData &)
This function draws the scene and could be called several times per frame as it's called once per vie...
Definition engine.h:199
std::vector< std::byte >(*) encode()
This function is called to encode all shared data that is sent to the connected nodes in a clustered ...
Definition engine.h:214
void(*) preWindow()
This function is called before the window is created (before OpenGL context is created).
Definition engine.h:184
void(*) decode(const std::vector< std::byte > &)
This function is called by decode all shared data sent to us from the master.
Definition engine.h:218
void(*) mouseButton(MouseButton, Modifier, Action, Window *)
This function sets the mouse button callback (GLFW wrapper) for all windows.
Definition engine.h:242
void(*) postDraw()
This function is called after the draw stage but before the OpenGL buffer swap.
Definition engine.h:206
void(*) externalDecode(const char *, int)
This function is called when a TCP message is received.
Definition engine.h:221
void(*) preSync()
This function is called before the synchronization stage.
Definition engine.h:192
void(*) externalStatus(bool)
This function is called when the connection status changes.
Definition engine.h:224
void(*) postSyncPreDraw()
This function is called once per frame after sync but before draw stage.
Definition engine.h:195
void(*) initOpenGL(GLFWwindow *)
This function is called once before the starting the render loop and after creation of the OpenGL con...
Definition engine.h:189
void(*) dataTransferAcknowledge(int, int)
This function is called when data is successfully sent.
Definition engine.h:233
void(*) character(unsigned int, int, Window *)
All windows are connected to this callback.
Definition engine.h:239
void(*) mousePos(double, double, Window *)
All windows are connected to this callback.
Definition engine.h:245
Definition engine.h:152
bool addWindowName
If set to true, the window name is added to screenshots.
Definition engine.h:163
std::optional< std::pair< uint64_t, uint64_t > > limits
Information about the screenshot limits.
Definition engine.h:172
int nCaptureThreads
Definition engine.h:157
std::string prefix
The prefix to be used for all screenshots.
Definition engine.h:166
bool addNodeName
If set to true, the node name is added to screenshots.
Definition engine.h:160
std::filesystem::path capturePath
The location where the screenshots are being saved.
Definition engine.h:154
Definition engine.h:122
float syncTimeout
The number of seconds that SGCT will wait for the master or clients to connect before aborting.
Definition engine.h:150
bool usePositionTexture
Definition engine.h:146
bool createDebugContext
Stores the configuration option whether the created OpenGL contexts should be debug contexts or regul...
Definition engine.h:126
bool captureBackBuffer
Get if capture should use backbuffer data or texture.
Definition engine.h:142
int8_t swapInterval
Sets the swap interval to be used by the application -1 = adaptive sync(Nvidia) 0 = vertical sync off...
Definition engine.h:133
bool useDepthTexture
Definition engine.h:144
bool useNormalTexture
Definition engine.h:145
bool printSyncMessage
If this is true, a log message is printed to the console while a client is waiting for the master to ...
Definition engine.h:138
Structure with all statistics gathered about different frametimes.
Definition engine.h:81
std::array< double, HistoryLength > loopTimeMin
The lowest time recorded for network communication between master and clients.
Definition engine.h:96
std::array< double, HistoryLength > syncTimes
The amount of time spend synchronizing the state between master and clients.
Definition engine.h:93
std::array< double, HistoryLength > frametimes
The times that contain the entire time spending processing the frames.
Definition engine.h:87
std::array< double, HistoryLength > drawTimes
The amount of time spend rendering the 2D and 3D components of the frame.
Definition engine.h:90
std::array< double, HistoryLength > loopTimeMax
The highest time recorded for network communication between master and clients.
Definition engine.h:99
static constexpr int HistoryLength
For how many frames are the history values collected before the oldest values are replaced.
Definition engine.h:84
Definition callbackdata.h:22
Definition config.h:453
Definition math.h:47