- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathViewerBase.cpp
More file actions
Latest commit
168 lines (126 loc) · 5.09 KB
/
Copy pathViewerBase.cpp
File metadata and controls
168 lines (126 loc) · 5.09 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
#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(constint w, constint h, constint validation_level, constbool novsync, constbool nohwrt,
constbool 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, novsync, nohwrt, nosubgroup, device_name)) {
throwstd::runtime_error("Initialization failed!");
}
InitOptickGPUProfiler();
snd_ctx_ = std::make_unique<Snd::Context>();
snd_ctx_->Init(log_);
unsignedint 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>();
{
usingnamespacestd::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));
}
state_manager_ = std::make_unique<ViewerStateManager>();
ui_renderer_ = std::make_unique<Gui::Renderer>(*ren_ctx_);
if (!ui_renderer_->Init()) {
throwstd::runtime_error("Couldn't initialize UI renderer!");
}
ui_root_ = std::make_unique<Gui::RootElement>(Gui::Vec2i(w, h));
}
Eng::ViewerBase::~ViewerBase() = default;
voidEng::ViewerBase::Resize(constint w, constint h, constbool novsync) {
width = w;
height = h;
ren_ctx_->Resize(width, height, novsync);
ui_root_->set_zone(Gui::Vec2i{width, height});
ui_root_->Resize();
}
voidEng::ViewerBase::Start() {}
voidEng::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();
}
}
voidEng::ViewerBase::Quit() { terminated = true; }
#if defined(REN_VK_BACKEND)
#include<Ren/Vk/VKCtx.h>
voidEng::ViewerBase::InitOptickGPUProfiler() {
Ren::ApiContext &api = ren_ctx_->api();
Optick::VulkanFunctions functions = {
api.vkGetPhysicalDeviceProperties,
(PFN_vkCreateQueryPool_)api.vkCreateQueryPool,
(PFN_vkCreateCommandPool_)api.vkCreateCommandPool,
(PFN_vkAllocateCommandBuffers_)api.vkAllocateCommandBuffers,
(PFN_vkCreateFence_)api.vkCreateFence,
api.vkCmdResetQueryPool,
(PFN_vkQueueSubmit_)api.vkQueueSubmit,
(PFN_vkWaitForFences_)api.vkWaitForFences,
(PFN_vkResetCommandBuffer_)api.vkResetCommandBuffer,
(PFN_vkCmdWriteTimestamp_)api.vkCmdWriteTimestamp,
(PFN_vkGetQueryPoolResults_)api.vkGetQueryPoolResults,
(PFN_vkBeginCommandBuffer_)api.vkBeginCommandBuffer,
(PFN_vkEndCommandBuffer_)api.vkEndCommandBuffer,
(PFN_vkResetFences_)api.vkResetFences,
api.vkDestroyCommandPool,
api.vkDestroyQueryPool,
api.vkDestroyFence,
api.vkFreeCommandBuffers,
};
OPTICK_GPU_INIT_VULKAN(&api.device, &api.physical_device, &api.graphics_queue, &api.graphics_family_index, 1,
&functions);
}
#else
voidEng::ViewerBase::InitOptickGPUProfiler() {}
#endif