Repository navigation
Expand file tree
/
Copy pathFEngine.cpp
More file actions
586 lines (464 loc) · 13.5 KB
/
Copy pathFEngine.cpp
File metadata and controls
586 lines (464 loc) · 13.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
#include "FEngine.h"
using namespace FocalEngine;
#ifdef FOCAL_ENGINE_SHARED
extern "C" __declspec(dllexport) void* GetEngine()
{
return FEngine::GetInstancePointer();
}
#endif
FEngine::FEngine()
{
}
FEngine::~FEngine()
{
}
// Included in .cpp rather than .h to prevent engine version macros from being
// exposed to script DLLs built in header-only mode (ENGINE_HEADERS_ONLY).
// Script DLLs capture their own build version at compile time, allowing the
// engine to detect version mismatches when loading them at runtime.
#include "Core/VersionInfo/FOCAL_ENGINE_Version.h"
#include "Core/VersionInfo/FEVersionInfo.h"
FE_DEFINE_VERSION_INFO(FOCAL_ENGINE_)
std::string FEngine::GetEngineVersion()
{
return GetFOCAL_ENGINE_VersionInfo().GetVersion();
}
int FEngine::GetEngineBuildNumber()
{
return GetFOCAL_ENGINE_VersionInfo().BuildNumber;
}
std::string FEngine::GetEngineBuildTimestamp()
{
return GetFOCAL_ENGINE_VersionInfo().BuildTimestamp;
}
std::string FEngine::GetEngineBuildInfo()
{
return GetFOCAL_ENGINE_VersionInfo().GetBuildInfo();
}
std::string FEngine::GetFullVersion()
{
return "Focal Engine " + GetFOCAL_ENGINE_VersionInfo().GetFullVersionString();/* GetEngineVersion() + " " + GetEngineBuildInfo();*/
}
bool FEngine::IsNotTerminated()
{
return APPLICATION.IsNotTerminated();
}
void FEngine::InternalUpdate(const bool bInternalCall)
{
CurrentDeltaTime = CPUTime + GPUTime;
ViewportCheckForModification();
if (!bInternalCall)
PLUGIN_MANAGER.CheckScenesForModification();
INSTANCED_RENDERING_SYSTEM.Update();
SCENE_MANAGER.Update();
CAMERA_SYSTEM.Update(CurrentDeltaTime);
VIRTUAL_UI_SYSTEM.Update();
// Plugins are updated before native scripts, so data that plugins provide reaches scripts in the same frame.
// Internal frame (RenderTo) should not call plugins, they are called once per frame.
if (!bInternalCall)
PLUGIN_MANAGER.Update(CurrentDeltaTime);
NATIVE_SCRIPT_SYSTEM.Update(CurrentDeltaTime);
for (size_t i = 0; i < OnAfterUpdateCallbacks.size(); i++)
{
if (OnAfterUpdateCallbacks[i] == nullptr)
continue;
OnAfterUpdateCallbacks[i]();
}
// Instead of updating TRANSFORM_SYSTEM in the beginning of the frame, we update it here.
// To ensure that all the other systems are updated before the TRANSFORM_SYSTEM will kick in.
TRANSFORM_SYSTEM.Update();
INPUT.Update();
}
void FEngine::BeginFrame(const bool bInternalCall)
{
if (!APPLICATION.IsNotTerminated())
return;
if (!bInternalCall)
TIME.BeginTimeStamp();
// FE_FIX_ME: Without graphics scenes are not updated yet, their systems need GPU.
if (APPLICATION.GetGraphicsMode() == FEGraphicsMode::None)
{
APPLICATION.BeginFrame();
CurrentDeltaTime = CPUTime + GPUTime;
if (!bInternalCall)
PLUGIN_MANAGER.Update(CurrentDeltaTime);
for (size_t i = 0; i < OnAfterUpdateCallbacks.size(); i++)
{
if (OnAfterUpdateCallbacks[i] != nullptr)
OnAfterUpdateCallbacks[i]();
}
return;
}
FE_GL_ERROR(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT));
RENDERER.BeginFrameDebugLines();
APPLICATION.BeginFrame();
if (APPLICATION.GetMainWindow() == nullptr)
return;
APPLICATION.GetMainWindow()->BeginFrame();
#ifdef FE_DEBUG_ENABLED
std::vector<FEUUID> ShaderList = RESOURCE_MANAGER.GetShaderIDList();
const std::vector<FEUUID> TemporaryList = RESOURCE_MANAGER.GetEnginePrivateShaderIDList();
for (size_t i = 0; i < TemporaryList.size(); i++)
{
ShaderList.push_back(TemporaryList[i]);
}
for (size_t i = 0; i < ShaderList.size(); i++)
{
if (RESOURCE_MANAGER.GetShader(ShaderList[i])->IsDebugRequest())
{
RESOURCE_MANAGER.GetShader(ShaderList[i])->ThisFrameDebugBind = 0;
}
}
#endif
InternalUpdate(bInternalCall);
}
void FEngine::Render(const bool bInternalCall)
{
if (APPLICATION.GetGraphicsMode() == FEGraphicsMode::None)
{
if (!bInternalCall)
CPUTime = TIME.EndTimeStamp();
return;
}
if (!bInternalCall)
PLUGIN_MANAGER.BeforeRender();
// Internal frame could start while plugins are already skipped, so previous value is restored instead of being cleared.
const bool bWasInternalFrame = PLUGIN_MANAGER.bInternalFrame;
if (bInternalCall)
PLUGIN_MANAGER.bInternalFrame = true;
std::vector<FEScene*> ActiveScenes = SCENE_MANAGER.GetScenesByFlagMask(FESceneFlag::Active | FESceneFlag::Renderable);
for (size_t i = 0; i < ActiveScenes.size(); i++)
RENDERER.Render(ActiveScenes[i]);
if (bVRActive)
OpenXR_MANAGER.Update();
PLUGIN_MANAGER.bInternalFrame = bWasInternalFrame;
if (!bInternalCall)
PLUGIN_MANAGER.AfterRender();
APPLICATION.GetMainWindow()->Render();
if (!bInternalCall)
CPUTime = TIME.EndTimeStamp();
}
void FEngine::EndFrame(const bool bInternalCall)
{
if (APPLICATION.GetGraphicsMode() == FEGraphicsMode::None)
{
APPLICATION.EndFrame();
if (!bInternalCall && !APPLICATION.IsNotTerminated())
PLUGIN_MANAGER.Shutdown();
CurentFrameIndex++;
return;
}
RENDERER.EndFrameDebugLines();
INPUT.EndFrame();
if (!bInternalCall)
TIME.BeginTimeStamp();
APPLICATION.GetMainWindow()->EndFrame();
APPLICATION.EndFrame();
if (!bInternalCall)
GPUTime = TIME.EndTimeStamp();
if (!bInternalCall && !APPLICATION.IsNotTerminated())
PLUGIN_MANAGER.Shutdown();
// FE_FIX_ME: Since AssetPackage doesn't extract assets directly to memory, we need to delete the directory after the frame completes.
if (RESOURCE_MANAGER.PrivateEngineAssetPackage != nullptr)
{
FILE_SYSTEM.DeleteDirectory(FILE_SYSTEM.GetCurrentWorkingPath() + "/SubSystems");
delete RESOURCE_MANAGER.PrivateEngineAssetPackage;
RESOURCE_MANAGER.PrivateEngineAssetPackage = nullptr;
}
CurentFrameIndex++;
}
void FEngine::InitWindow(const int Width, const int Height, std::string WindowTitle)
{
FEWindow* NewWindow = APPLICATION.AddWindow(Width, Height, WindowTitle);
if (NewWindow == nullptr)
{
LOG.Add("FEngine::InitWindow: Window could not be created", "FE_LOG_GENERAL", FE_LOG_ERROR);
return;
}
// Early initialization of INPUT system.
INPUT;
APPLICATION.GetMainWindow()->AddOnResizeCallback(&FEngine::WindowResizeCallback);
APPLICATION.GetMainWindow()->AddOnDropCallback(&FEngine::DropCallback);
APPLICATION.GetMainWindow()->AddOnTerminateCallback([]() { PLUGIN_MANAGER.Shutdown(); OpenXR_MANAGER.Shutdown(); });
CreateViewport(NewWindow);
FE_GL_ERROR(glEnable(GL_DEPTH_TEST));
// tessellation parameter
FE_GL_ERROR(glPatchParameteri(GL_PATCH_VERTICES, 4));
RENDERER.Init();
// Early initialization of the systems.
TRANSFORM_SYSTEM;
LIGHT_SYSTEM;
CAMERA_SYSTEM;
POINT_CLOUD_SYSTEM;
INSTANCED_RENDERING_SYSTEM;
TERRAIN_SYSTEM;
VOLUME_SYSTEM;
SKY_DOME_SYSTEM;
PREFAB_INSTANCE_SYSTEM;
VIRTUAL_UI_SYSTEM;
NATIVE_SCRIPT_SYSTEM;
PLUGIN_MANAGER.Initialize();
}
void FEngine::InitHeadless()
{
if (!APPLICATION.SetGraphicsMode(FEGraphicsMode::None))
return;
PLUGIN_MANAGER.Initialize();
}
void FEngine::SetWindowCaption(const std::string NewCaption)
{
if (APPLICATION.GetMainWindow() != nullptr)
APPLICATION.GetMainWindow()->SetTitle(NewCaption);
}
void FEngine::AddWindowResizeCallback(void(*Func)(int, int))
{
if (Func != nullptr)
ClientWindowResizeCallbacks.push_back(Func);
}
void FEngine::AddWindowCloseCallback(void(*Func)())
{
APPLICATION.GetMainWindow()->AddOnCloseCallback(Func);
}
void FEngine::WindowResizeCallback(const int Width, const int Height)
{
for (size_t i = 0; i < ENGINE.ClientWindowResizeCallbacks.size(); i++)
{
if (ENGINE.ClientWindowResizeCallbacks[i] == nullptr)
continue;
ENGINE.ClientWindowResizeCallbacks[i](Width, Height);
}
}
void FEngine::RenderTo(FEFramebuffer* RenderTo)
{
RenderTo->Bind();
BeginFrame(true);
Render(true);
RenderTo->UnBind();
}
double FEngine::GetCpuTime()
{
return CPUTime;
}
double FEngine::GetGpuTime()
{
return GPUTime;
}
double FEngine::GetDeltaTime()
{
return CurrentDeltaTime;
}
FEPostProcess* FEngine::CreatePostProcess(const std::string Name, int ScreenWidth, int ScreenHeight)
{
if (ScreenWidth < 2 || ScreenHeight < 2)
{
ScreenWidth = ENGINE.GetDefaultViewport()->GetWidth();
ScreenHeight = ENGINE.GetDefaultViewport()->GetHeight();
}
return RESOURCE_MANAGER.CreatePostProcess(ScreenWidth, ScreenHeight, Name);
}
void FEngine::Terminate()
{
// Without graphics application could exit without another frame, so plugins are unloaded right away.
if (APPLICATION.GetGraphicsMode() == FEGraphicsMode::None)
{
APPLICATION.Close();
PLUGIN_MANAGER.Shutdown();
return;
}
OpenXR_MANAGER.Shutdown();
APPLICATION.Close();
}
void FEngine::SaveScreenshot(std::string FileName, FEScene* SceneToWorkWith)
{
RENDERER.SaveScreenshot(FileName, SceneToWorkWith);
}
void FEngine::DropCallback(const int Count, const char** Paths)
{
for (size_t i = 0; i < ENGINE.ClientDropCallbacks.size(); i++)
{
if (ENGINE.ClientDropCallbacks[i] == nullptr)
continue;
ENGINE.ClientDropCallbacks[i](Count, Paths);
}
}
void FEngine::AddDropCallback(void(*Func)(int, const char**))
{
if (Func != nullptr)
ClientDropCallbacks.push_back(Func);
}
bool FEngine::IsVsyncEnabled()
{
return bVsyncEnabled;
}
void FEngine::SetVsyncEnabled(bool NewValue)
{
bVsyncEnabled = NewValue;
if (bVsyncEnabled)
{
glfwSwapInterval(1);
}
else
{
glfwSwapInterval(0);
}
}
void FEngine::DisableVR()
{
bVRActive = false;
bVRInitializedCorrectly = false;
OpenXR_MANAGER.Shutdown();
}
bool FEngine::EnableVR(FERenderingPipeline VRRenderingPipeline)
{
this->VRRenderingPipeline = VRRenderingPipeline;
if (!bVRInitializedCorrectly)
bVRInitializedCorrectly = OpenXR_MANAGER.Init();
bVRActive = bVRInitializedCorrectly;
return bVRActive;
}
FERenderingPipeline FEngine::GetVRRenderingPipeline() const
{
return VRRenderingPipeline;
}
std::string FEngine::GetVRApplicationVisibleName() const
{
return VRApplicationVisibleName;
}
void FEngine::SetVRApplicationVisibleName(const std::string& NewName)
{
if (NewName.empty())
{
LOG.Add("VR Application Visible Name cannot be empty.", "FE_LOG_OPENXR");
return;
}
VRApplicationVisibleName = NewName;
}
bool FEngine::IsVRInitializedCorrectly()
{
return bVRInitializedCorrectly;
}
bool FEngine::IsVREnabled()
{
return bVRActive;
}
void FEngine::AddOnAfterUpdateCallback(std::function<void()> Callback)
{
OnAfterUpdateCallbacks.push_back(Callback);
}
FEUUID FEngine::CreateViewport(ImGuiWindow* ImGuiWindowPointer)
{
for (size_t i = 0; i < Viewports.size(); i++)
{
if (Viewports[i]->WindowHandle == ImGuiWindowPointer)
return Viewports[i]->ID;
}
FEViewport* NewViewport = new FEViewport();
NewViewport->Type = FE_VIEWPORT_IMGUI_WINDOW;
NewViewport->WindowHandle = ImGuiWindowPointer;
Viewports.push_back(NewViewport);
return NewViewport->ID;
}
FEUUID FEngine::CreateViewport(FEWindow* FEWindowPointer)
{
for (size_t i = 0; i < Viewports.size(); i++)
{
if (Viewports[i]->WindowHandle == FEWindowPointer)
return Viewports[i]->ID;
}
FEViewport* NewViewport = new FEViewport();
NewViewport->Type = FE_VIEWPORT_FEWINDOW;
NewViewport->WindowHandle = FEWindowPointer;
Viewports.push_back(NewViewport);
return NewViewport->ID;
}
FEViewport* FEngine::GetViewport(FEUUID ViewportID)
{
for (size_t i = 0; i < Viewports.size(); i++)
{
if (Viewports[i]->ID == ViewportID)
return Viewports[i];
}
return nullptr;
}
void FEngine::ViewportCheckForModificationIndividual(FEViewport* ViewPort, bool& bMoved, bool& bResize)
{
bMoved = false;
bResize = false;
switch (ViewPort->Type)
{
case FE_VIEWPORT_VIRTUAL:
return;
case FE_VIEWPORT_OS_WINDOW:
return;
case FE_VIEWPORT_GLFW_WINDOW:
return;
case FE_VIEWPORT_FEWINDOW:
{
FEWindow* Window = static_cast<FEWindow*>(ViewPort->WindowHandle);
if (ViewPort->X != 0 || ViewPort->Y != 0)
bMoved = true;
ViewPort->X = 0;
ViewPort->Y = 0;
if (ViewPort->Width != Window->GetWidth() || ViewPort->Height != Window->GetHeight())
bResize = true;
ViewPort->Width = Window->GetWidth();
ViewPort->Height = Window->GetHeight();
return;
}
case FE_VIEWPORT_IMGUI_WINDOW:
{
ImGuiWindow* Window = static_cast<ImGuiWindow*>(ViewPort->WindowHandle);
if (ViewPort->X != static_cast<int>(Window->ContentRegionRect.GetTL().x) || ViewPort->Y != static_cast<int>(Window->ContentRegionRect.GetTL().y))
bMoved = true;
ViewPort->X = static_cast<int>(Window->ContentRegionRect.GetTL().x);
ViewPort->Y = static_cast<int>(Window->ContentRegionRect.GetTL().y);
if (ViewPort->Width != static_cast<int>(Window->ContentRegionRect.GetWidth()) || ViewPort->Height != static_cast<int>(Window->ContentRegionRect.GetHeight()))
bResize = true;
ViewPort->Width = static_cast<int>(Window->ContentRegionRect.GetWidth());
ViewPort->Height = static_cast<int>(Window->ContentRegionRect.GetHeight());
return;
}
}
}
void FEngine::AddOnViewportMovedCallback(std::function<void(FEUUID)> Callback)
{
OnViewportMovedCallbacks.push_back(Callback);
}
void FEngine::ViewportCheckForModification()
{
for (size_t i = 0; i < Viewports.size(); i++)
{
bool bMoved, bResize;
ViewportCheckForModificationIndividual(Viewports[i], bMoved, bResize);
if (bMoved)
{
for (size_t j = 0; j < OnViewportMovedCallbacks.size(); j++)
{
if (OnViewportMovedCallbacks[j] != nullptr)
OnViewportMovedCallbacks[j](Viewports[i]->ID);
}
}
if (bResize)
{
for (size_t j = 0; j < OnViewportResizeCallbacks.size(); j++)
{
if (OnViewportResizeCallbacks[j] != nullptr)
OnViewportResizeCallbacks[j](Viewports[i]->ID);
}
}
}
}
void FEngine::AddOnViewportResizeCallback(std::function<void(FEUUID)> Callback)
{
OnViewportResizeCallbacks.push_back(Callback);
}
FEViewport* FEngine::GetDefaultViewport()
{
if (Viewports.size() == 0)
return nullptr;
return Viewports[0];
}
unsigned long long FEngine::GetCurrentFrameIndex()
{
return CurentFrameIndex;
}