-
Notifications
You must be signed in to change notification settings - Fork 14
/
main.cc
261 lines (194 loc) · 7.37 KB
/
main.cc
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
#include <thread>
#include "jetstream/base.hh"
using namespace Jetstream;
int main(int argc, char* argv[]) {
// Parse command line arguments.
Backend::Config backendConfig;
Viewport::Config viewportConfig;
Render::Window::Config renderConfig;
std::string flowgraphPath;
Device prefferedBackend = Device::None;
for (int i = 1; i < argc; i++) {
const std::string arg = std::string(argv[i]);
if (arg == "--headless") {
backendConfig.headless = true;
continue;
}
if (arg == "--endpoint") {
if (i + 1 < argc) {
viewportConfig.endpoint = argv[++i];
}
continue;
}
if (arg == "--backend") {
// TODO: Add check for valid backend.
if (i + 1 < argc) {
prefferedBackend = StringToDevice(argv[++i]);
}
continue;
}
if (arg == "--no-validation") {
backendConfig.validationEnabled = false;
continue;
}
if (arg == "--no-vsync") {
viewportConfig.vsync = false;
continue;
}
if (arg == "--no-hw-acceleration") {
viewportConfig.hardwareAcceleration = false;
continue;
}
if (arg == "--benchmark") {
// TODO: Add check for valid output type.
std::string outputType = "markdown";
if (i + 1 < argc) {
outputType = std::string(argv[++i]);
}
Benchmark::Run(outputType);
return 0;
}
if (arg == "--framerate") {
if (i + 1 < argc) {
viewportConfig.framerate = std::stoul(argv[++i]);
}
continue;
}
if (arg == "--multisampling") {
if (i + 1 < argc) {
backendConfig.multisampling = std::stoul(argv[++i]);
}
continue;
}
if (arg == "--size") {
if (i + 2 < argc) {
viewportConfig.size.x = std::stoul(argv[++i]);
viewportConfig.size.y = std::stoul(argv[++i]);
}
continue;
}
if (arg == "--codec") {
// TODO: Add check for valid codec.
if (i + 1 < argc) {
viewportConfig.codec = Viewport::StringToVideoCodec(argv[++i]);
}
continue;
}
if (arg == "--device-id") {
if (i + 1 < argc) {
backendConfig.deviceId = std::stoul(argv[++i]);
}
continue;
}
if (arg == "--staging-buffer") {
if (i + 1 < argc) {
backendConfig.stagingBufferSize = std::stoul(argv[++i])*1024*1024;
}
continue;
}
if (arg == "--scale") {
if (i + 1 < argc) {
renderConfig.scale = std::stof(argv[++i]);
}
continue;
}
if (arg == "--help" || arg == "-h") {
std::cout << "Usage: " << argv[0] << " [options] [flowgraph]" << std::endl;
std::cout << "Options:" << std::endl;
std::cout << " --headless Enable headless mode." << std::endl;
std::cout << " --endpoint [endpoint] Set the endpoint of the headless viewport (`1.1.1.1:8000`, `./vid.mp4`, etc). Default: `/tmp/cyberether`" << std::endl;
std::cout << " --backend [backend] Set the preferred backend (`Metal`, `Vulkan`, or `WebGPU`)." << std::endl;
std::cout << " --framerate [value] Set the framerate of the headless viewport (FPS). Default: `60`" << std::endl;
std::cout << " --multisampling [value] Set the multisampling anti-aliasing factor (`1`, `4`, or `8`). Default: `4`" << std::endl;
std::cout << " --codec [codec] Set the video codec of the headless viewport. Default: `FFV1`" << std::endl;
std::cout << " --size [width] [height] Set the initial size of the viewport. Default: `1920 1080`" << std::endl;
std::cout << " --scale [scale] Set the scale of the render window. Default: `1.0`" << std::endl;
std::cout << " --benchmark [type] Run the benchmark and output the results (`markdown`, `json`, or `csv`). Default: `markdown`" << std::endl;
std::cout << " --no-hw-acceleration Disable hardware acceleration. Enabled otherwise." << std::endl;
std::cout << "Other Options:" << std::endl;
std::cout << " --staging-buffer [size] Set the staging buffer size (MB). Default: `64`" << std::endl;
std::cout << " --device-id [id] Set the physical device ID. Default: `0`" << std::endl;
std::cout << " --no-validation Disable Vulkan validation layers. Enabled otherwise." << std::endl;
std::cout << " --no-vsync Disable vsync. Enabled otherwise." << std::endl;
std::cout << "Other:" << std::endl;
std::cout << " --help, -h Print this help message." << std::endl;
std::cout << " --version, -v Print the version." << std::endl;
return 0;
}
if (arg == "--version" || arg == "-v") {
std::cout << "CyberEther v" << JETSTREAM_VERSION_STR << "-" << JETSTREAM_BUILD_TYPE << std::endl;
return 0;
}
flowgraphPath = arg;
}
// Instance creation.
Instance instance;
// Configure instance.
Instance::Config config = {
.preferredDevice = prefferedBackend,
.enableCompositor = true,
.backendConfig = backendConfig,
.viewportConfig = viewportConfig,
.renderConfig = renderConfig
};
JST_CHECK_THROW(instance.build(config));
// Load flowgraph if provided.
if (!flowgraphPath.empty()) {
JST_CHECK_THROW(instance.flowgraph().create());
JST_CHECK_THROW(instance.flowgraph().importFromFile(flowgraphPath));
}
// Start instance.
instance.start();
// Start compute thread.
auto computeThread = std::thread([&]{
while (instance.computing()) {
JST_CHECK_THROW(instance.compute());
}
});
// Start graphical thread.
auto graphicalThreadLoop = [](void* arg) {
Instance* instance = reinterpret_cast<Instance*>(arg);
if (instance->begin() == Result::SKIP) {
return;
}
JST_CHECK_THROW(instance->present());
if (instance->end() == Result::SKIP) {
return;
}
};
#ifdef JST_OS_BROWSER
emscripten_set_main_loop_arg(graphicalThreadLoop, &instance, 0, 1);
#else
auto graphicalThread = std::thread([&]{
while (instance.presenting()) {
graphicalThreadLoop(&instance);
}
});
#endif
// Start input polling.
#ifdef JST_OS_BROWSER
emscripten_runtime_keepalive_push();
#else
while (instance.viewport().keepRunning()) {
instance.viewport().waitEvents();
}
#endif
// Stop instance and wait for threads.
instance.reset();
instance.stop();
if (computeThread.joinable()) {
computeThread.join();
}
#ifdef JST_OS_BROWSER
emscripten_cancel_main_loop();
#else
if (graphicalThread.joinable()) {
graphicalThread.join();
}
#endif
// Destroy instance.
instance.destroy();
// Destroy backend.
Backend::DestroyAll();
return 0;
}