Debian Codegraph @

src/Viewer/VRManager.cxx

raw symbols SCIP
1 // Copyright (C) 2021 James Hogan
2 //
3 // This program is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU General Public License as
5 // published by the Free Software Foundation; either version 2 of the
6 // License, or (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful, but
9 // WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16
17 #include "VRManager.hxx"
18 #include "WindowBuilder.hxx"
19 #include "renderer.hxx"
20
21 #include <osgXR/Settings>
22
23 #include <simgear/scene/util/RenderConstants.hxx>
24 #include <simgear/scene/viewer/CompositorPass.hxx>
25
26 #include <Main/fg_props.hxx>
27 #include <Main/globals.hxx>
28
29 namespace flightgear
30 {
31
32 // Unfortunately, this can't be scoped inside VRManager::instance().
33 // If its initialisation completes after main() calls atexit(fgExitCleanup),
34 // then its destruction should take place before fgExitCleanup() is called.
35 static osg::ref_ptr<VRManager> managerInstance;
36
37 VRManager::VRManager() :
38 _reloadCompositorCallback(new ReloadCompositorCallback(this)),
39 _propXrLayersValidation("/sim/vr/openxr/layers/validation"),
40 _propXrExtensionsDepthInfo("/sim/vr/openxr/extensions/depth-info"),
41 _propXrExtensionsVisibilityMask("/sim/vr/openxr/extensions/visibility-mask"),
42 _propXrRuntimeName("/sim/vr/openxr/runtime/name"),
43 _propXrSystemName("/sim/vr/openxr/system/name"),
44 _propStateString("/sim/vr/state-string"),
45 _propPresent("/sim/vr/present"),
46 _propRunning("/sim/vr/running"),
47 _propEnabled("/sim/vr/enabled"),
48 _propDepthInfo("/sim/vr/depth-info"),
49 _propVisibilityMask("/sim/vr/visibility-mask"),
50 _propValidationLayer("/sim/vr/validation-layer"),
51 _propMode("/sim/vr/mode"),
52 _propSwapchainMode("/sim/vr/swapchain-mode"),
53 _propMirrorEnabled("/sim/vr/mirror-enabled"),
54 _propMirrorMode("/sim/vr/mirror-mode"),
55 _listenerEnabled(this, &osgXR::Manager::setEnabled),
56 _listenerDepthInfo(this, &VRManager::setDepthInfo),
57 _listenerVisibilityMask(this, &VRManager::setVisibilityMask),
58 _listenerValidationLayer(this, &VRManager::setValidationLayer),
59 _listenerMode(this, &VRManager::setVRMode),
60 _listenerSwapchainMode(this, &VRManager::setSwapchainMode),
61 _listenerMirrorMode(this, &VRManager::setMirrorMode)
62 {
63 uint32_t fgVersion = (FLIGHTGEAR_MAJOR_VERSION << 16 |
64 FLIGHTGEAR_MINOR_VERSION << 8 |
65 FLIGHTGEAR_PATCH_VERSION);
66 _settings->setApp("FlightGear", fgVersion);
67 _settings->preferEnvBlendMode(osgXR::Settings::BLEND_MODE_OPAQUE);
68
69 // Inform osgXR what node masks to use
70 setVisibilityMaskNodeMasks(simgear::NodeMask::LEFT_BIT,
71 simgear::NodeMask::RIGHT_BIT);
72
73 // Hook into viewer, but don't enable VR just yet
74 osgViewer::View *view = globals->get_renderer()->getView();
75 if (view) {
76 setViewer(globals->get_renderer()->getViewerBase());
77 view->apply(this);
78 }
79
80 syncReadOnlyProperties();
81
82 _propEnabled.node(true)->addChangeListener(&_listenerEnabled, true);
83 _propDepthInfo.node(true)->addChangeListener(&_listenerDepthInfo, true);
84 _propVisibilityMask.node(true)->addChangeListener(&_listenerVisibilityMask, true);
85 _propValidationLayer.node(true)->addChangeListener(&_listenerValidationLayer, true);
86 _propMode.node(true)->addChangeListener(&_listenerMode, true);
87 _propSwapchainMode.node(true)->addChangeListener(&_listenerSwapchainMode, true);
88 _propMirrorMode.node(true)->addChangeListener(&_listenerMirrorMode, true);
89
90 // No need for a change listener, but it should still be resolvable
91 _propMirrorEnabled.node(true);
92 }
93
94 VRManager* VRManager::instance(bool destroy)
95 {
96 static bool initialised = false;
97 if (destroy) {
98 if (initialised) {
99 managerInstance->destroyAndWait();
100 globals->get_renderer()->getViewerBase()->setRealizeOperation(nullptr);
101 globals->get_renderer()->getView()->apply(nullptr);
102 }
103 managerInstance = nullptr;
104 initialised = false;
105 return nullptr;
106 }
107 if (!initialised) {
108 managerInstance = new VRManager;
109 initialised = true;
110 }
111 return managerInstance;
112 }
113
114 void VRManager::reset()
115 {
116 auto composite_viewer = globals->get_renderer()->getCompositeViewer();
117
118 // Force retrigger of the CompositeViewer realize callback.
119 // We reuse the graphics context, so it will already be realized. This makes
120 // the CompositeViewer become realized without its realize operation
121 // callback being called, preventing osgXR from being reinitialised.
122
123 auto* viewerRealizeOp = composite_viewer->getRealizeOperation();
124 if (viewerRealizeOp) {
125 osgViewer::ViewerBase::Contexts contexts;
126 composite_viewer->getContexts(contexts);
127 for (auto citr = contexts.begin(); citr != contexts.end(); ++citr) {
128 osg::GraphicsContext* gc = *citr;
129
130 if (gc->valid()) {
131 gc->makeCurrent();
132 (*viewerRealizeOp)(gc);
133 gc->releaseContext();
134 }
135 }
136 }
137 }
138
139 void VRManager::syncProperties()
140 {
141 // If the state has changed, properties may need synchronising
142 if (checkAndResetStateChanged()) {
143 syncReadOnlyProperties();
144 syncSettingProperties();
145 }
146 }
147
148 void VRManager::syncReadOnlyProperties()
149 {
150 _propXrLayersValidation = hasValidationLayer();
151 _propXrExtensionsDepthInfo = hasDepthInfoExtension();
152 _propXrExtensionsVisibilityMask = hasVisibilityMaskExtension();
153 _propXrRuntimeName = getRuntimeName();
154 _propXrSystemName = getSystemName();
155
156 _propStateString = getStateString();
157 _propPresent = getPresent();
158 _propRunning = isRunning();
159 }
160
161 void VRManager::syncSettingProperties()
162 {
163 bool enabled = getEnabled();
164 if (_propEnabled != enabled)
165 _propEnabled = enabled;
166 }
167
168 bool VRManager::getUseMirror() const
169 {
170 return _propMirrorEnabled && isRunning();
171 }
172
173 void VRManager::setValidationLayer(bool validationLayer)
174 {
175 _settings->setValidationLayer(validationLayer);
176 syncSettings();
177 }
178
179 void VRManager::setDepthInfo(bool depthInfo)
180 {
181 _settings->setDepthInfo(depthInfo);
182 syncSettings();
183 }
184
185 void VRManager::setVisibilityMask(bool visibilityMask)
186 {
187 _settings->setVisibilityMask(visibilityMask);
188 syncSettings();
189 }
190
191 void VRManager::setVRMode(const std::string& mode)
192 {
193 osgXR::Settings::VRMode vrMode = osgXR::Settings::VRMODE_AUTOMATIC;
194
195 if (mode == "AUTOMATIC") {
196 vrMode = osgXR::Settings::VRMODE_AUTOMATIC;
197 } else if (mode == "SLAVE_CAMERAS") {
198 vrMode = osgXR::Settings::VRMODE_SLAVE_CAMERAS;
199 } else if (mode == "SCENE_VIEW") {
200 vrMode = osgXR::Settings::VRMODE_SCENE_VIEW;
201 }
202
203 _settings->setVRMode(vrMode);
204 syncSettings();
205 }
206
207 void VRManager::setSwapchainMode(const std::string& mode)
208 {
209 osgXR::Settings::SwapchainMode swapchainMode = osgXR::Settings::SWAPCHAIN_AUTOMATIC;
210
211 if (mode == "AUTOMATIC") {
212 swapchainMode = osgXR::Settings::SWAPCHAIN_AUTOMATIC;
213 } else if (mode == "MULTIPLE") {
214 swapchainMode = osgXR::Settings::SWAPCHAIN_MULTIPLE;
215 } else if (mode == "SINGLE") {
216 swapchainMode = osgXR::Settings::SWAPCHAIN_SINGLE;
217 }
218
219 _settings->setSwapchainMode(swapchainMode);
220 syncSettings();
221 }
222
223 void VRManager::setMirrorMode(const std::string& mode)
224 {
225 osgXR::MirrorSettings::MirrorMode mirrorMode = osgXR::MirrorSettings::MIRROR_AUTOMATIC;
226 int viewIndex = -1;
227
228 if (mode == "AUTOMATIC") {
229 mirrorMode = osgXR::MirrorSettings::MIRROR_AUTOMATIC;
230 } else if (mode == "NONE") {
231 mirrorMode = osgXR::MirrorSettings::MIRROR_NONE;
232 } else if (mode == "LEFT") {
233 mirrorMode = osgXR::MirrorSettings::MIRROR_SINGLE;
234 viewIndex = 0;
235 } else if (mode == "RIGHT") {
236 mirrorMode = osgXR::MirrorSettings::MIRROR_SINGLE;
237 viewIndex = 1;
238 } else if (mode == "LEFT_RIGHT") {
239 mirrorMode = osgXR::MirrorSettings::MIRROR_LEFT_RIGHT;
240 }
241
242 _settings->getMirrorSettings().setMirror(mirrorMode, viewIndex);
243 }
244
245 void VRManager::update()
246 {
247 osgXR::Manager::update();
248 syncProperties();
249 }
250
251 void VRManager::doCreateView(osgXR::View *xrView)
252 {
253 // Restarted in osgXR::Manager::update()
254 _viewer->stopThreading();
255
256 // Construct a property tree for the camera
257 SGPropertyNode_ptr camNode = new SGPropertyNode;
258 WindowBuilder *windowBuilder = WindowBuilder::getWindowBuilder();
259 setValue(camNode->getNode("window/name", true),
260 windowBuilder->getDefaultWindowName());
261
262 // Build a camera
263 CameraGroup *cgroup = CameraGroup::getDefault();
264 CameraInfo *info = cgroup->buildCamera(camNode);
265
266 // Notify osgXR about the new compositor's scene slave cameras
267 if (info) {
268 _camInfos[xrView] = info;
269 _xrViews[info] = xrView;
270 info->reloadCompositorCallback = _reloadCompositorCallback;
271
272 postReloadCompositor(cgroup, info);
273 }
274 }
275
276 void VRManager::doDestroyView(osgXR::View *xrView)
277 {
278 // Restarted in osgXR::Manager::update()
279 _viewer->stopThreading();
280
281 CameraGroup *cgroup = CameraGroup::getDefault();
282 auto it = _camInfos.find(xrView);
283 if (it != _camInfos.end()) {
284 osg::ref_ptr<CameraInfo> info = (*it).second;
285 _camInfos.erase(it);
286
287 auto it2 = _xrViews.find(info.get());
288 if (it2 != _xrViews.end())
289 _xrViews.erase(it2);
290
291 cgroup->removeCamera(info.get());
292 }
293 }
294
295 void VRManager::onRunning()
296 {
297 // Reload compositors to trigger switch to mirror of VR
298 CameraGroup *cgroup = CameraGroup::getDefault();
299 reloadCompositors(cgroup);
300 }
301
302 void VRManager::onStopped()
303 {
304 // As long as we're not in the process of destroying FlightGear, reload
305 // compositors to trigger switch away from mirror of VR
306 if (!isDestroying())
307 {
308 CameraGroup *cgroup = CameraGroup::getDefault();
309 reloadCompositors(cgroup);
310 }
311 }
312
313 void VRManager::preReloadCompositor(CameraGroup *cgroup, CameraInfo *info)
314 {
315 osgXR::View *xrView = _xrViews[info];
316
317 auto& passes = info->compositor->getPassList();
318 for (auto& pass: passes)
319 if (pass->type == "scene")
320 xrView->removeSlave(pass->camera);
321 }
322
323 void VRManager::postReloadCompositor(CameraGroup *cgroup, CameraInfo *info)
324 {
325 osgXR::View *xrView = _xrViews[info];
326
327 auto& passes = info->compositor->getPassList();
328 for (auto& pass: passes)
329 if (pass->type == "scene")
330 xrView->addSlave(pass->camera);
331 }
332
333 }