-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathvsgviewer.cpp
More file actions
409 lines (338 loc) · 16.2 KB
/
vsgviewer.cpp
File metadata and controls
409 lines (338 loc) · 16.2 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
#include <vsg/all.h>
#ifdef vsgXchange_FOUND
# include <vsgXchange/all.h>
#endif
#include <algorithm>
#include <chrono>
#include <iostream>
#include <thread>
vsg::ref_ptr<vsg::Node> createTextureQuad(vsg::ref_ptr<vsg::Data> sourceData, vsg::ref_ptr<vsg::Options> options)
{
auto builder = vsg::Builder::create();
builder->options = options;
vsg::StateInfo state;
state.image = sourceData;
state.lighting = false;
vsg::GeometryInfo geom;
geom.dy.set(0.0f, 0.0f, 1.0f);
geom.dz.set(0.0f, -1.0f, 0.0f);
return builder->createQuad(geom, state);
}
void enableGenerateDebugInfo(vsg::ref_ptr<vsg::Options> options)
{
auto shaderHints = vsg::ShaderCompileSettings::create();
shaderHints->generateDebugInfo = true;
auto& text = options->shaderSets["text"] = vsg::createTextShaderSet(options);
text->defaultShaderHints = shaderHints;
auto& flat = options->shaderSets["flat"] = vsg::createFlatShadedShaderSet(options);
flat->defaultShaderHints = shaderHints;
auto& phong = options->shaderSets["phong"] = vsg::createPhongShaderSet(options);
phong->defaultShaderHints = shaderHints;
auto& pbr = options->shaderSets["pbr"] = vsg::createPhysicsBasedRenderingShaderSet(options);
pbr->defaultShaderHints = shaderHints;
}
int main(int argc, char** argv)
{
try
{
// set up defaults and read command line arguments to override them
vsg::CommandLine arguments(&argc, argv);
// if we want to redirect std::cout and std::cerr to the vsg::Logger call vsg::Logger::redirect_stdout()
if (arguments.read({"--redirect-std", "-r"})) vsg::Logger::instance()->redirect_std();
// set up vsg::Options to pass in filepaths, ReaderWriters and other IO related options to use when reading and writing files.
auto options = vsg::Options::create();
options->sharedObjects = vsg::SharedObjects::create();
options->fileCache = vsg::getEnv("VSG_FILE_CACHE");
options->paths = vsg::getEnvPaths("VSG_FILE_PATH");
#ifdef vsgXchange_all
// add vsgXchange's support for reading and writing 3rd party file formats
options->add(vsgXchange::all::create());
#endif
arguments.read(options);
auto windowTraits = vsg::WindowTraits::create();
windowTraits->windowTitle = "vsgviewer";
windowTraits->debugLayer = arguments.read({"--debug", "-d"});
windowTraits->apiDumpLayer = arguments.read({"--api", "-a"});
windowTraits->synchronizationLayer = arguments.read("--sync");
windowTraits->debugUtils = arguments.read("--debug-utils");
bool reportAverageFrameRate = arguments.read("--fps");
if (arguments.read("--double-buffer")) windowTraits->swapchainPreferences.imageCount = 2;
if (arguments.read("--triple-buffer")) windowTraits->swapchainPreferences.imageCount = 3; // default
if (arguments.read("--IMMEDIATE")) { windowTraits->swapchainPreferences.presentMode = VK_PRESENT_MODE_IMMEDIATE_KHR; }
if (arguments.read("--FIFO")) windowTraits->swapchainPreferences.presentMode = VK_PRESENT_MODE_FIFO_KHR;
if (arguments.read("--FIFO_RELAXED")) windowTraits->swapchainPreferences.presentMode = VK_PRESENT_MODE_FIFO_RELAXED_KHR;
if (arguments.read("--MAILBOX")) windowTraits->swapchainPreferences.presentMode = VK_PRESENT_MODE_MAILBOX_KHR;
if (arguments.read({"-t", "--test"}))
{
windowTraits->swapchainPreferences.presentMode = VK_PRESENT_MODE_IMMEDIATE_KHR;
windowTraits->fullscreen = true;
reportAverageFrameRate = true;
}
if (arguments.read({"--st", "--small-test"}))
{
windowTraits->swapchainPreferences.presentMode = VK_PRESENT_MODE_IMMEDIATE_KHR;
windowTraits->width = 192, windowTraits->height = 108;
windowTraits->decoration = false;
reportAverageFrameRate = true;
}
bool multiThreading = arguments.read("--mt");
if (arguments.read({"--fullscreen", "--fs"})) windowTraits->fullscreen = true;
if (arguments.read({"--window", "-w"}, windowTraits->width, windowTraits->height)) { windowTraits->fullscreen = false; }
if (arguments.read({"--no-frame", "--nf"})) windowTraits->decoration = false;
if (arguments.read("--or")) windowTraits->overrideRedirect = true;
auto maxTime = arguments.value(std::numeric_limits<double>::max(), "--max-time");
if (arguments.read("--d32")) windowTraits->depthFormat = VK_FORMAT_D32_SFLOAT;
if (arguments.read("--sRGB")) windowTraits->swapchainPreferences.surfaceFormat = {VK_FORMAT_B8G8R8A8_SRGB, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR};
if (arguments.read("--RGB")) windowTraits->swapchainPreferences.surfaceFormat = {VK_FORMAT_B8G8R8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR};
arguments.read("--screen", windowTraits->screenNum);
arguments.read("--display", windowTraits->display);
arguments.read("--samples", windowTraits->samples);
if (int log_level = 0; arguments.read("--log-level", log_level)) vsg::Logger::instance()->level = vsg::Logger::Level(log_level);
auto numFrames = arguments.value(-1, "-f");
auto pathFilename = arguments.value<vsg::Path>("", "-p");
auto loadLevels = arguments.value(0, "--load-levels");
auto maxPagedLOD = arguments.value(0, "--maxPagedLOD");
auto horizonMountainHeight = arguments.value(0.0, "--hmh");
auto nearFarRatio = arguments.value<double>(0.001, "--nfr");
if (arguments.read("--rgb")) options->mapRGBtoRGBAHint = false;
bool depthClamp = arguments.read({"--dc", "--depthClamp"});
if (depthClamp)
{
std::cout << "Enabled depth clamp." << std::endl;
auto deviceFeatures = windowTraits->deviceFeatures = vsg::DeviceFeatures::create();
deviceFeatures->get().samplerAnisotropy = VK_TRUE;
deviceFeatures->get().depthClamp = VK_TRUE;
}
vsg::ref_ptr<vsg::ResourceHints> resourceHints;
if (auto resourceHintsFilename = arguments.value<vsg::Path>("", "--rh"))
{
resourceHints = vsg::read_cast<vsg::ResourceHints>(resourceHintsFilename, options);
}
if (auto outputResourceHintsFilename = arguments.value<vsg::Path>("", "--orh"))
{
if (!resourceHints) resourceHints = vsg::ResourceHints::create();
vsg::write(resourceHints, outputResourceHintsFilename, options);
return 0;
}
if (arguments.read({"--shader-debug-info", "--sdi"}))
{
enableGenerateDebugInfo(options);
windowTraits->deviceExtensionNames.push_back(VK_KHR_SHADER_NON_SEMANTIC_INFO_EXTENSION_NAME);
}
if (int log_level = 0; arguments.read("--log-level", log_level)) vsg::Logger::instance()->level = vsg::Logger::Level(log_level);
auto logFilename = arguments.value<vsg::Path>("", "--log");
vsg::ref_ptr<vsg::Instrumentation> instrumentation;
if (arguments.read({"--gpu-annotation", "--ga"}) && vsg::isExtensionSupported(VK_EXT_DEBUG_UTILS_EXTENSION_NAME))
{
windowTraits->debugUtils = true;
auto gpu_instrumentation = vsg::GpuAnnotation::create();
if (arguments.read("--name"))
gpu_instrumentation->labelType = vsg::GpuAnnotation::SourceLocation_name;
else if (arguments.read("--className"))
gpu_instrumentation->labelType = vsg::GpuAnnotation::Object_className;
else if (arguments.read("--func"))
gpu_instrumentation->labelType = vsg::GpuAnnotation::SourceLocation_function;
instrumentation = gpu_instrumentation;
}
else if (arguments.read({"--profiler", "--pr"}))
{
// set Profiler options
auto settings = vsg::Profiler::Settings::create();
arguments.read("--cpu", settings->cpu_instrumentation_level);
arguments.read("--gpu", settings->gpu_instrumentation_level);
arguments.read("--log-size", settings->log_size);
// create the profiler
instrumentation = vsg::Profiler::create(settings);
}
vsg::Affinity affinity;
uint32_t cpu = 0;
while (arguments.read("-c", cpu))
{
affinity.cpus.insert(cpu);
}
// should animations be automatically played
auto autoPlay = !arguments.read({"--no-auto-play", "--nop"});
if (arguments.errors()) return arguments.writeErrorMessages(std::cerr);
if (argc <= 1)
{
std::cout << "Please specify a 3d model or image file on the command line." << std::endl;
return 1;
}
auto group = vsg::Group::create();
vsg::Path path;
// read any vsg files
for (int i = 1; i < argc; ++i)
{
vsg::Path filename = arguments[i];
path = vsg::filePath(filename);
auto object = vsg::read(filename, options);
if (auto node = object.cast<vsg::Node>())
{
group->addChild(node);
}
else if (auto data = object.cast<vsg::Data>())
{
if (auto textureGeometry = createTextureQuad(data, options))
{
group->addChild(textureGeometry);
}
}
else if (object)
{
std::cout << "Unable to view object of type " << object->className() << std::endl;
}
else
{
std::cout << "Unable to load file " << filename << std::endl;
}
}
if (group->children.empty())
{
return 1;
}
vsg::ref_ptr<vsg::Node> vsg_scene;
if (group->children.size() == 1)
vsg_scene = group->children[0];
else
vsg_scene = group;
// create the viewer and assign window(s) to it
auto viewer = vsg::Viewer::create();
auto window = vsg::Window::create(windowTraits);
if (!window)
{
std::cout << "Could not create window." << std::endl;
return 1;
}
viewer->addWindow(window);
// compute the bounds of the scene graph to help position camera
vsg::ComputeBounds computeBounds;
vsg_scene->accept(computeBounds);
vsg::dvec3 centre = (computeBounds.bounds.min + computeBounds.bounds.max) * 0.5;
double radius = vsg::length(computeBounds.bounds.max - computeBounds.bounds.min) * 0.6;
// set up the camera
auto lookAt = vsg::LookAt::create(centre + vsg::dvec3(0.0, -radius * 3.5, 0.0), centre, vsg::dvec3(0.0, 0.0, 1.0));
vsg::ref_ptr<vsg::ProjectionMatrix> perspective;
auto ellipsoidModel = vsg_scene->getRefObject<vsg::EllipsoidModel>("EllipsoidModel");
if (ellipsoidModel)
{
perspective = vsg::EllipsoidPerspective::create(lookAt, ellipsoidModel, 30.0, static_cast<double>(window->extent2D().width) / static_cast<double>(window->extent2D().height), nearFarRatio, horizonMountainHeight);
}
else
{
perspective = vsg::Perspective::create(30.0, static_cast<double>(window->extent2D().width) / static_cast<double>(window->extent2D().height), nearFarRatio * radius, radius * 4.5);
}
auto camera = vsg::Camera::create(perspective, lookAt, vsg::ViewportState::create(window->extent2D()));
// add close handler to respond to the close window button and pressing escape
viewer->addEventHandler(vsg::CloseHandler::create(viewer));
auto cameraAnimation = vsg::CameraAnimationHandler::create(camera, pathFilename, options);
viewer->addEventHandler(cameraAnimation);
if (autoPlay && cameraAnimation->animation)
{
cameraAnimation->play();
if (reportAverageFrameRate && maxTime == std::numeric_limits<double>::max())
{
maxTime = cameraAnimation->animation->maxTime();
}
}
viewer->addEventHandler(vsg::Trackball::create(camera, ellipsoidModel));
// if required preload specific number of PagedLOD levels.
if (loadLevels > 0)
{
vsg::LoadPagedLOD loadPagedLOD(camera, loadLevels);
auto startTime = vsg::clock::now();
vsg_scene->accept(loadPagedLOD);
auto time = std::chrono::duration<float, std::chrono::milliseconds::period>(vsg::clock::now() - startTime).count();
std::cout << "No. of tiles loaded " << loadPagedLOD.numTiles << " in " << time << "ms." << std::endl;
}
auto commandGraph = vsg::createCommandGraphForView(window, camera, vsg_scene);
viewer->assignRecordAndSubmitTaskAndPresentation({commandGraph});
if (instrumentation) viewer->assignInstrumentation(instrumentation);
if (multiThreading)
{
viewer->setupThreading();
if (affinity)
{
auto cpu_itr = affinity.cpus.begin();
// set affinity of main thread
if (cpu_itr != affinity.cpus.end())
{
std::cout << "vsg::setAffinity() " << *cpu_itr << std::endl;
vsg::setAffinity(vsg::Affinity(*cpu_itr++));
}
for (auto& thread : viewer->threads)
{
if (thread.joinable() && cpu_itr != affinity.cpus.end())
{
std::cout << "vsg::setAffinity(" << thread.get_id() << ") " << *cpu_itr << std::endl;
vsg::setAffinity(thread, vsg::Affinity(*cpu_itr++));
}
}
}
}
else if (affinity)
{
std::cout << "vsg::setAffinity(";
for (auto cpu_num : affinity.cpus)
{
std::cout << " " << cpu_num;
}
std::cout << " )" << std::endl;
vsg::setAffinity(affinity);
}
viewer->compile(resourceHints);
if (maxPagedLOD > 0)
{
// set targetMaxNumPagedLODWithHighResSubgraphs after Viewer::compile() as it will assign any DatabasePager if required.
for (auto& task : viewer->recordAndSubmitTasks)
{
if (task->databasePager) task->databasePager->targetMaxNumPagedLODWithHighResSubgraphs = maxPagedLOD;
}
}
if (autoPlay)
{
// find any animation groups in the loaded scene graph and play the first animation in each of the animation groups.
auto animationGroups = vsg::visit<vsg::FindAnimations>(vsg_scene).animationGroups;
for (auto ag : animationGroups)
{
if (!ag->animations.empty()) viewer->animationManager->play(ag->animations.front());
}
}
viewer->start_point() = vsg::clock::now();
// rendering main loop
while (viewer->advanceToNextFrame() && (numFrames < 0 || (numFrames--) > 0) && (viewer->getFrameStamp()->simulationTime < maxTime))
{
// pass any events into EventHandlers assigned to the Viewer
viewer->handleEvents();
viewer->update();
viewer->recordAndSubmit();
viewer->present();
}
if (reportAverageFrameRate)
{
auto fs = viewer->getFrameStamp();
double fps = static_cast<double>(fs->frameCount) / std::chrono::duration<double, std::chrono::seconds::period>(vsg::clock::now() - viewer->start_point()).count();
std::cout << "Average frame rate = " << fps << " fps" << std::endl;
}
if (auto profiler = instrumentation.cast<vsg::Profiler>())
{
instrumentation->finish();
if (logFilename)
{
std::ofstream fout(logFilename);
profiler->log->report(fout);
}
else
{
profiler->log->report(std::cout);
}
}
}
catch (const vsg::Exception& ve)
{
for (int i = 0; i < argc; ++i) std::cerr << argv[i] << " ";
std::cerr << "\n[Exception] - " << ve.message << " result = " << ve.result << std::endl;
return 1;
}
// clean up done automatically thanks to ref_ptr<>
return 0;
}