-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMainComponent.cpp
490 lines (427 loc) · 11.7 KB
/
MainComponent.cpp
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
#include "MainComponent.h"
#include "RenderPath.h"
#include "wiRenderer.h"
#include "wiHelper.h"
#include "wiTimer.h"
#include "wiInput.h"
#include "wiBackLog.h"
#include "MainComponent_BindLua.h"
#include "wiVersion.h"
#include "wiEnums.h"
#include "wiTextureHelper.h"
#include "wiProfiler.h"
#include "wiInitializer.h"
#include "wiStartupArguments.h"
#include "wiFont.h"
#include "wiImage.h"
#include "wiEvent.h"
#include "wiGraphicsDevice_DX11.h"
#include "wiGraphicsDevice_DX12.h"
#include "wiGraphicsDevice_Vulkan.h"
#include "Utility/replace_new.h"
#include <sstream>
#include <algorithm>
#ifdef GGREDUCED
#ifdef OPTICK_ENABLE
#include "optick.h"
#endif
#endif
using namespace wiGraphics;
void MainComponent::Initialize()
{
if (initialized)
{
return;
}
initialized = true;
wiInitializer::InitializeComponentsAsync();
}
void MainComponent::ActivatePath(RenderPath* component, float fadeSeconds, wiColor fadeColor)
{
if (component != nullptr)
{
component->init(canvas);
}
// Fade manager will activate on fadeout
fadeManager.Clear();
fadeManager.Start(fadeSeconds, fadeColor, [this, component]() {
if (GetActivePath() != nullptr)
{
GetActivePath()->Stop();
}
if (component != nullptr)
{
component->Start();
}
activePath = component;
});
fadeManager.Update(0); // If user calls ActivatePath without fadeout, it will be instant
}
void MainComponent::Run()
{
if (!initialized)
{
// Initialize in a lazy way, so the user application doesn't have to call this explicitly
Initialize();
initialized = true;
}
if (!wiInitializer::IsInitializeFinished())
{
#if GGREDUCED
// we do this elsewhere now
#else
// Until engine is not loaded, present initialization screen...
CommandList cmd = wiRenderer::GetDevice()->BeginCommandList();
wiRenderer::GetDevice()->RenderPassBegin(&swapChain, cmd);
wiImage::SetCanvas(canvas, cmd);
wiFont::SetCanvas(canvas, cmd);
Viewport viewport;
viewport.Width = (float)swapChain.desc.width;
viewport.Height = (float)swapChain.desc.height;
wiRenderer::GetDevice()->BindViewports(1, &viewport, cmd);
wiFontParams params;
params.posX = 5.f;
params.posY = 5.f;
std::string text = wiBackLog::getText();
float textheight = wiFont::textHeight(text, params);
float screenheight = canvas.GetLogicalHeight();
if (textheight > screenheight)
{
params.posY = screenheight - textheight;
}
wiFont::Draw(text, params, cmd);
wiRenderer::GetDevice()->RenderPassEnd(cmd);
wiRenderer::GetDevice()->SubmitCommandLists();
#endif
return;
}
#ifdef GGREDUCED
// only essential calls while booting up with splash
if (is_completely_loaded == false)
{
Update(0);
return;
}
#endif
#ifndef GGREDUCED
static bool startup_script = false;
if (!startup_script)
{
startup_script = true;
wiLua::RegisterObject(MainComponent_BindLua::className, "main", new MainComponent_BindLua(this));
wiLua::RunFile("startup.lua");
}
#endif
wiProfiler::BeginFrame();
deltaTime = float(std::max(0.0, timer.elapsed() / 1000.0));
#ifdef GGREDUCED
//LB: if leave app, timer.elapsed() is going to be huge on returning, causing weird animation issues
// so cap this to a maximum so it reduces the glitch the user sees (without causing anything over 30fps to misbehave)
double dThirtyFPSinMS = (1.0f / 30.0f);
if (deltaTime > dThirtyFPSinMS) deltaTime = dThirtyFPSinMS;
#endif
timer.record();
#ifdef GGREDUCED
//PE: We need to run always, as we can have many windows running at the same time.
if (1)
#else
if (is_window_active)
#endif
{
// If the application is active, run Update loops:
// Wake up the events that need to be executed on the main thread, in thread safe manner:
wiEvent::FireEvent(SYSTEM_EVENT_THREAD_SAFE_POINT, 0);
const float dt = framerate_lock ? (1.0f / targetFrameRate) : deltaTime;
fadeManager.Update(dt);
if (GetActivePath() != nullptr)
{
GetActivePath()->init(canvas);
GetActivePath()->PreUpdate();
}
// Fixed time update:
auto range = wiProfiler::BeginRangeCPU("Fixed Update");
{
if (frameskip)
{
deltaTimeAccumulator += dt;
if (deltaTimeAccumulator > 10)
{
// application probably lost control, fixed update would take too long
deltaTimeAccumulator = 0;
}
const float targetFrameRateInv = 1.0f / targetFrameRate;
while (deltaTimeAccumulator >= targetFrameRateInv)
{
FixedUpdate();
deltaTimeAccumulator -= targetFrameRateInv;
}
}
else
{
FixedUpdate();
}
}
wiProfiler::EndRange(range); // Fixed Update
// Variable-timed update:
Update(dt);
Render( 0 );
}
else
{
// If the application is not active, disable Update loops:
deltaTimeAccumulator = 0;
}
wiInput::Update(window);
#ifdef GGREDUCED
if (is_completely_loaded == false)
{
// we can present a loading splash while things load - done elsewhere in master.cpp
}
else
{
#endif
CommandList cmd = wiRenderer::GetDevice()->BeginCommandList();
wiRenderer::GetDevice()->RenderPassBegin(&swapChain, cmd);
{
wiImage::SetCanvas(canvas, cmd);
wiFont::SetCanvas(canvas, cmd);
Viewport viewport;
viewport.Width = (float)swapChain.desc.width;
viewport.Height = (float)swapChain.desc.height;
wiRenderer::GetDevice()->BindViewports(1, &viewport, cmd);
Compose(cmd);
wiRenderer::GetDevice()->RenderPassEnd(cmd);
wiProfiler::EndFrame(cmd);
#ifdef GGREDUCED
#ifdef OPTICK_ENABLE
OPTICK_EVENT("wiRenderer::SubmitCommandLists");
#endif
#endif
wiRenderer::GetDevice()->SubmitCommandLists();
}
#ifdef GGREDUCED
}
#endif
}
void MainComponent::Update(float dt)
{
auto range1 = wiProfiler::BeginRangeCPU("Update");
#ifndef GGREDUCED
wiLua::SetDeltaTime(double(dt));
wiLua::Update();
#endif
if (GetActivePath() != nullptr)
{
GetActivePath()->Update(dt);
GetActivePath()->PostUpdate();
}
wiProfiler::EndRange(range1);
}
void MainComponent::FixedUpdate()
{
wiBackLog::Update(canvas);
#ifndef GGREDUCED
wiLua::FixedUpdate();
#endif
if (GetActivePath() != nullptr)
{
GetActivePath()->FixedUpdate();
}
}
void MainComponent::Render( int mode )
{
auto range = wiProfiler::BeginRangeCPU("Render");
#ifndef GGREDUCED
wiLua::Render();
#endif
if (GetActivePath() != nullptr)
{
GetActivePath()->Render( mode );
}
wiProfiler::EndRange(range); // Render
}
void MainComponent::Compose(CommandList cmd)
{
auto range = wiProfiler::BeginRangeCPU("Compose");
if (GetActivePath() != nullptr)
{
GetActivePath()->Compose(cmd);
}
GraphicsDevice* device = wiRenderer::GetDevice();
if (fadeManager.IsActive())
{
// display fade rect
static wiImageParams fx;
fx.siz.x = canvas.GetLogicalWidth();
fx.siz.y = canvas.GetLogicalHeight();
fx.opacity = fadeManager.opacity;
wiImage::Draw(wiTextureHelper::getColor(fadeManager.color), fx, cmd);
}
// Draw the information display
if (infoDisplay.active)
{
std::stringstream ss("");
if (infoDisplay.watermark)
{
ss << "Wicked Engine " << wiVersion::GetVersionString() << " ";
#if defined(_ARM)
ss << "[ARM]";
#elif defined(_WIN64)
ss << "[64-bit]";
#elif defined(_WIN32)
ss << "[32-bit]";
#endif
#ifdef PLATFORM_UWP
ss << "[UWP]";
#endif
#ifdef WICKEDENGINE_BUILD_DX11
if (dynamic_cast<GraphicsDevice_DX11*>(device))
{
ss << "[DX11]";
}
#endif
#ifdef WICKEDENGINE_BUILD_DX12
if (dynamic_cast<GraphicsDevice_DX12*>(device))
{
ss << "[DX12]";
}
#endif
#ifdef WICKEDENGINE_BUILD_VULKAN
if (dynamic_cast<GraphicsDevice_Vulkan*>(device))
{
ss << "[Vulkan]";
}
#endif
#ifdef _DEBUG
ss << "[DEBUG]";
#endif
if (device->IsDebugDevice())
{
ss << "[debugdevice]";
}
ss << std::endl;
}
if (infoDisplay.resolution)
{
ss << "Resolution: " << canvas.GetPhysicalWidth() << " x " << canvas.GetPhysicalHeight() << " (" << canvas.GetDPI() << " dpi)" << std::endl;
}
if (infoDisplay.fpsinfo)
{
deltatimes[fps_avg_counter++ % arraysize(deltatimes)] = deltaTime;
float displaydeltatime = deltaTime;
if (fps_avg_counter > arraysize(deltatimes))
{
float avg_time = 0;
for (int i = 0; i < arraysize(deltatimes); ++i)
{
avg_time += deltatimes[i];
}
displaydeltatime = avg_time / arraysize(deltatimes);
}
ss.precision(2);
ss << std::fixed << 1.0f / displaydeltatime << " FPS" << std::endl;
}
if (infoDisplay.heap_allocation_counter)
{
ss << "Heap allocations per frame: " << number_of_allocs.load() << std::endl;
number_of_allocs.store(0);
}
#ifdef _DEBUG
ss << "Warning: This is a [DEBUG] build, performance will be slow!" << std::endl;
#endif
if (wiRenderer::GetDevice()->IsDebugDevice())
{
ss << "Warning: Graphics is in [debugdevice] mode, performance will be slow!" << std::endl;
}
ss.precision(2);
wiFont::Draw(ss.str(), wiFontParams(4, 4, infoDisplay.size, WIFALIGN_LEFT, WIFALIGN_TOP, wiColor(255,255,255,255), wiColor(0,0,0,255)), cmd);
if (infoDisplay.colorgrading_helper)
{
wiImage::Draw(wiTextureHelper::getColorGradeDefault(), wiImageParams(0, 0, 256.0f / canvas.GetDPIScaling(), 16.0f / canvas.GetDPIScaling()), cmd);
}
}
wiProfiler::DrawData(canvas, 4, 120, cmd);
wiBackLog::Draw(canvas, cmd);
wiProfiler::EndRange(range); // Compose
}
void MainComponent::SetWindow(wiPlatform::window_type window, bool fullscreen)
{
this->window = window;
// User can also create a graphics device if custom logic is desired, but they must do before this function!
if (wiRenderer::GetDevice() == nullptr)
{
bool debugdevice = wiStartupArguments::HasArgument("debugdevice");
bool gpuvalidation = wiStartupArguments::HasArgument("gpuvalidation");
bool use_dx11 = wiStartupArguments::HasArgument("dx11");
bool use_dx12 = wiStartupArguments::HasArgument("dx12");
bool use_vulkan = wiStartupArguments::HasArgument("vulkan");
#ifndef WICKEDENGINE_BUILD_DX11
if (use_dx11) {
wiHelper::messageBox("The engine was built without DX11 support!", "Error");
use_dx11 = false;
}
#endif
#ifndef WICKEDENGINE_BUILD_DX12
if (use_dx12) {
wiHelper::messageBox("The engine was built without DX12 support!", "Error");
use_dx12 = false;
}
#endif
#ifndef WICKEDENGINE_BUILD_VULKAN
if (use_vulkan) {
wiHelper::messageBox("The engine was built without Vulkan support!", "Error");
use_vulkan = false;
}
#endif
if (!use_dx11 && !use_dx12 && !use_vulkan)
{
#if defined(WICKEDENGINE_BUILD_DX11)
use_dx11 = true;
#elif defined(WICKEDENGINE_BUILD_DX12)
use_dx12 = true;
#elif defined(WICKEDENGINE_BUILD_VULKAN)
use_vulkan = true;
#else
wiBackLog::post("No rendering backend is enabled! Please enable at least one so we can use it as default");
assert(false);
#endif
}
assert(use_dx11 || use_dx12 || use_vulkan);
if (use_vulkan)
{
#ifdef WICKEDENGINE_BUILD_VULKAN
wiRenderer::SetShaderPath(wiRenderer::GetShaderPath() + "spirv/");
wiRenderer::SetDevice(std::make_shared<GraphicsDevice_Vulkan>(window, debugdevice));
#endif
}
else if (use_dx12)
{
#ifdef WICKEDENGINE_BUILD_DX12
wiRenderer::SetShaderPath(wiRenderer::GetShaderPath() + "hlsl6/");
wiRenderer::SetDevice(std::make_shared<GraphicsDevice_DX12>(debugdevice, gpuvalidation));
#endif
}
else if (use_dx11)
{
#ifdef WICKEDENGINE_BUILD_DX11
wiRenderer::SetShaderPath(wiRenderer::GetShaderPath() + "hlsl5/");
wiRenderer::SetDevice(std::make_shared<GraphicsDevice_DX11>(debugdevice));
#endif
}
}
canvas.init(window);
SwapChainDesc desc;
desc.width = canvas.GetPhysicalWidth();
desc.height = canvas.GetPhysicalHeight();
desc.buffercount = 3;
desc.format = FORMAT_R10G10B10A2_UNORM;
desc.vsync = swapChain.desc.vsync;
bool success = wiRenderer::GetDevice()->CreateSwapChain(&desc, window, &swapChain);
assert(success);
swapChainVsyncChangeEvent = wiEvent::Subscribe(SYSTEM_EVENT_SET_VSYNC, [this](uint64_t userdata) {
SwapChainDesc desc = swapChain.desc;
desc.vsync = userdata != 0;
bool success = wiRenderer::GetDevice()->CreateSwapChain(&desc, nullptr, &swapChain);
assert(success);
});
}