-
Notifications
You must be signed in to change notification settings - Fork 0
/
ViewerBase.cpp
169 lines (127 loc) · 5.16 KB
/
ViewerBase.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
#include "ViewerBase.h"
#include <random>
#include <thread>
#include <Gui/BaseElement.h>
#include <Gui/Renderer.h>
#include <Ren/Context.h>
#include <Snd/Context.h>
#include <Sys/AssetFileIO.h>
#include <Sys/Json.h>
#include <Sys/ThreadPool.h>
#include <Sys/Time_.h>
#include <optick/optick.h>
#include "Log.h"
#include "ViewerStateManager.h"
#include "renderer/Renderer.h"
#include "scene/PhysicsManager.h"
#include "scene/SceneManager.h"
#include "utils/FlowControl.h"
#include "utils/Random.h"
#include "utils/ShaderLoader.h"
Eng::ViewerBase::ViewerBase(const int w, const int h, const int validation_level, const bool nohwrt,
const bool nosubgroup, ILog *log, std::string_view device_name)
: log_(log), width(w), height(h) {
terminated = false;
// Sys::InitWorker();
ren_ctx_ = std::make_unique<Ren::Context>();
if (!ren_ctx_->Init(w, h, log_, validation_level, nohwrt, nosubgroup, device_name)) {
throw std::runtime_error("Initialization failed!");
}
InitOptickGPUProfiler();
snd_ctx_ = std::make_unique<Snd::Context>();
snd_ctx_->Init(log_);
unsigned int num_threads = std::max(std::thread::hardware_concurrency(), 1u);
threads_ = std::make_unique<Sys::ThreadPool>(num_threads, Sys::eThreadPriority::Normal, "worker");
input_manager_ = std::make_unique<InputManager>();
shader_loader_ = std::make_unique<ShaderLoader>(*ren_ctx_);
flow_control_ = std::make_unique<FlowControl>(2 * NET_UPDATE_DELTA, NET_UPDATE_DELTA);
random_ = std::make_unique<Random>(std::random_device{}());
renderer_ = std::make_unique<Renderer>(*ren_ctx_, *shader_loader_, *random_, *threads_);
physics_manager_ = std::make_unique<PhysicsManager>();
{
using namespace std::placeholders;
path_config_t paths;
scene_manager_ = std::make_unique<SceneManager>(*ren_ctx_, *shader_loader_, snd_ctx_.get(), *threads_, paths);
scene_manager_->SetPipelineInitializer(
std::bind(&Renderer::InitPipelinesForProgram, renderer(), _1, _2, _3, _4));
}
state_manager_ = std::make_unique<ViewerStateManager>();
ui_renderer_ = std::make_unique<Gui::Renderer>(*ren_ctx_);
if (!ui_renderer_->Init()) {
throw std::runtime_error("Couldn't initialize UI renderer!");
}
ui_root_ = std::make_unique<Gui::RootElement>(Gui::Vec2i(w, h));
}
Eng::ViewerBase::~ViewerBase() = default;
void Eng::ViewerBase::Resize(const int w, const int h) {
width = w;
height = h;
ren_ctx_->Resize(width, height);
ui_root_->set_zone(Gui::Vec2i{width, height});
ui_root_->Resize();
}
void Eng::ViewerBase::Start() {}
void Eng::ViewerBase::Frame() {
OPTICK_EVENT("ViewerBase::Frame");
FrameInfo &fr = fr_info_;
fr.cur_time_us = Sys::GetTimeUs();
if (fr.cur_time_us < fr.prev_time_us) {
fr.prev_time_us = 0;
}
fr.delta_time_us = fr.cur_time_us - fr.prev_time_us;
if (fr.delta_time_us > 200000) {
fr.delta_time_us = 200000;
}
fr.prev_time_us = fr.cur_time_us;
fr.time_acc_us += fr.delta_time_us;
uint64_t poll_time_point = fr.cur_time_us - fr.time_acc_us;
while (fr.time_acc_us >= UPDATE_DELTA) {
input_event_t evt;
while (input_manager_->PollEvent(poll_time_point, evt)) {
state_manager_->HandleInput(evt, input_manager_->keys_state());
}
state_manager_->UpdateFixed(UPDATE_DELTA);
fr.time_acc_us -= UPDATE_DELTA;
poll_time_point += UPDATE_DELTA;
}
fr.time_fract = double(fr.time_acc_us) / UPDATE_DELTA;
{
OPTICK_EVENT("state_manager->UpdateAnim");
state_manager_->UpdateAnim(fr_info_.delta_time_us);
}
{
OPTICK_EVENT("state_manager->Draw");
state_manager_->Draw();
}
}
void Eng::ViewerBase::Quit() { terminated = true; }
#if defined(REN_VK_BACKEND)
#include <Ren/VKCtx.h>
void Eng::ViewerBase::InitOptickGPUProfiler() {
Ren::ApiContext *api_ctx = ren_ctx_->api_ctx();
Optick::VulkanFunctions functions = {
api_ctx->vkGetPhysicalDeviceProperties,
(PFN_vkCreateQueryPool_)api_ctx->vkCreateQueryPool,
(PFN_vkCreateCommandPool_)api_ctx->vkCreateCommandPool,
(PFN_vkAllocateCommandBuffers_)api_ctx->vkAllocateCommandBuffers,
(PFN_vkCreateFence_)api_ctx->vkCreateFence,
api_ctx->vkCmdResetQueryPool,
(PFN_vkQueueSubmit_)api_ctx->vkQueueSubmit,
(PFN_vkWaitForFences_)api_ctx->vkWaitForFences,
(PFN_vkResetCommandBuffer_)api_ctx->vkResetCommandBuffer,
(PFN_vkCmdWriteTimestamp_)api_ctx->vkCmdWriteTimestamp,
(PFN_vkGetQueryPoolResults_)api_ctx->vkGetQueryPoolResults,
(PFN_vkBeginCommandBuffer_)api_ctx->vkBeginCommandBuffer,
(PFN_vkEndCommandBuffer_)api_ctx->vkEndCommandBuffer,
(PFN_vkResetFences_)api_ctx->vkResetFences,
api_ctx->vkDestroyCommandPool,
api_ctx->vkDestroyQueryPool,
api_ctx->vkDestroyFence,
api_ctx->vkFreeCommandBuffers,
};
OPTICK_GPU_INIT_VULKAN(&api_ctx->device, &api_ctx->physical_device, &api_ctx->graphics_queue,
&api_ctx->graphics_family_index, 1, &functions);
}
#else
void Eng::ViewerBase::InitOptickGPUProfiler() {}
#endif