Debian Codegraph @

3rdparty/osgXR/src/Swapchain.cpp

raw symbols SCIP
1 // SPDX-License-Identifier: LGPL-2.1-only
2 // Copyright (C) 2022 James Hogan <[email protected]>
3
4 #include "Swapchain.h"
5
6 #include "OpenXR/System.h"
7
8 #include "XRState.h"
9
10 #include <osg/Notify>
11 #include <osg/observer_ptr>
12
13 #include <osgViewer/Renderer>
14
15 #include <memory>
16 #include <sstream>
17
18 using namespace osgXR;
19
20 namespace {
21
22 // Draw callbacks
23
24 class InitialDrawCallback : public osg::Camera::DrawCallback
25 {
26 public:
27 InitialDrawCallback(std::shared_ptr<Swapchain::Private> &swapchain) :
28 _swapchain(swapchain)
29 {
30 }
31
32 void operator()(osg::RenderInfo& renderInfo) const override
33 {
34 auto swapchain = _swapchain.lock();
35 if (swapchain)
36 swapchain->initialDrawCallback(renderInfo);
37 }
38
39 protected:
40 std::weak_ptr<Swapchain::Private> _swapchain;
41 };
42
43 class PreDrawCallback : public osg::Camera::DrawCallback
44 {
45 public:
46 PreDrawCallback(std::shared_ptr<Swapchain::Private> &swapchain) :
47 _swapchain(swapchain)
48 {
49 swapchain->incNumDrawPasses();
50 }
51
52 ~PreDrawCallback()
53 {
54 auto swapchain = _swapchain.lock();
55 if (swapchain)
56 swapchain->decNumDrawPasses();
57 }
58
59 void operator()(osg::RenderInfo& renderInfo) const override
60 {
61 auto swapchain = _swapchain.lock();
62 if (swapchain)
63 swapchain->preDrawCallback(renderInfo);
64 }
65
66 protected:
67 std::weak_ptr<Swapchain::Private> _swapchain;
68 };
69
70 class PostDrawCallback : public osg::Camera::DrawCallback
71 {
72 public:
73 PostDrawCallback(std::shared_ptr<Swapchain::Private> &swapchain) :
74 _swapchain(swapchain)
75 {
76 }
77
78 void operator()(osg::RenderInfo& renderInfo) const override
79 {
80 auto swapchain = _swapchain.lock();
81 if (swapchain)
82 swapchain->postDrawCallback(renderInfo);
83 }
84
85 protected:
86 std::weak_ptr<Swapchain::Private> _swapchain;
87 };
88
89 }
90
91 // Internal API
92
93 Swapchain::Private::Private(uint32_t width, uint32_t height) :
94 _preferredRGBEncodingMask(0),
95 _allowedRGBEncodingMask(0),
96 _rgbBits(10), // 8-bits is unlikely to be sufficient for linear encodings
97 _alphaBits(0), // Alpha channel not required by default
98 _width(width),
99 _height(height),
100 _forcedAlpha(-1.0f),
101 _numDrawPasses(0),
102 _updated(true)
103 {
104 }
105
106 Swapchain::Private::~Private()
107 {
108 }
109
110 void Swapchain::Private::attachToCamera(std::shared_ptr<Private> &self,
111 osg::Camera *camera)
112 {
113 camera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER);
114 camera->setInitialDrawCallback(new InitialDrawCallback(self));
115 camera->setPreDrawCallback(new PreDrawCallback(self));
116 camera->setFinalDrawCallback(new PostDrawCallback(self));
117
118 // FIXME Do all cameras inherit the main display settings... eeek!
119 //camera->setDisplaySettings(osg::DisplaySettings::instance());
120
121 if (_swapchain.valid())
122 _swapchain->incNumDrawPasses(1); // FIXME HACK depends on where node is!
123 }
124
125 void Swapchain::Private::attachToMirror(std::shared_ptr<Private> &self,
126 osg::StateSet *stateSet)
127 {
128 _stateSets.push_back(stateSet);
129 }
130
131 void Swapchain::Private::preferRGBEncoding(Encoding encoding)
132 {
133 uint32_t mask = (1u << (unsigned int)encoding);
134 _preferredRGBEncodingMask |= mask;
135 _allowedRGBEncodingMask |= mask;
136 }
137
138 void Swapchain::Private::allowRGBEncoding(Encoding encoding)
139 {
140 uint32_t mask = (1u << (unsigned int)encoding);
141 _allowedRGBEncodingMask |= mask;
142 }
143
144 void Swapchain::Private::setRGBBits(unsigned int rgbBits)
145 {
146 _rgbBits = rgbBits;
147 }
148
149 unsigned int Swapchain::Private::getRGBBits() const
150 {
151 return _rgbBits;
152 }
153
154 void Swapchain::Private::setAlphaBits(unsigned int alphaBits)
155 {
156 _alphaBits = alphaBits;
157 }
158
159 unsigned int Swapchain::Private::getAlphaBits() const
160 {
161 return _alphaBits;
162 }
163
164 void Swapchain::Private::setWidth(uint32_t width)
165 {
166 if (width != _width)
167 _updated = true;
168 _width = width;
169 }
170
171 uint32_t Swapchain::Private::getWidth() const
172 {
173 return _width;
174 }
175
176 void Swapchain::Private::setHeight(uint32_t height)
177 {
178 if (height != _height)
179 _updated = true;
180 _height = height;
181 }
182
183 uint32_t Swapchain::Private::getHeight() const
184 {
185 return _height;
186 }
187
188 void Swapchain::Private::setForcedAlpha(float alpha)
189 {
190 if (alpha < 0.0f)
191 alpha = 0.0f;
192 if (alpha > 1.0f)
193 alpha = 1.0f;
194 _forcedAlpha = alpha;
195 }
196
197 void Swapchain::Private::disableForcedAlpha()
198 {
199 _forcedAlpha = -1.0f;
200 }
201
202 float Swapchain::Private::getForcedAlpha() const
203 {
204 return _forcedAlpha;
205 }
206
207 bool Swapchain::Private::setup(XRState *state, OpenXR::Session *session)
208 {
209 XRState *oldState = _state.get();
210 if (oldState)
211 {
212 if (oldState != state)
213 {
214 OSG_WARN << "osgXR: Swapchain XRState conflict" << std::endl;
215 return false;
216 }
217 if (!_updated)
218 return true;
219 }
220
221 OpenXR::System::ViewConfiguration::View view(_width, _height);
222 int64_t rgbaFormat = state->chooseRGBAFormat(_rgbBits, _alphaBits,
223 _preferredRGBEncodingMask,
224 _allowedRGBEncodingMask);
225 if (!rgbaFormat)
226 {
227 std::stringstream formats;
228 formats << std::hex;
229 for (int64_t format: session->getSwapchainFormats())
230 formats << " 0x" << format;
231 OSG_WARN << "osgXR: No supported custom swapchain format found in ["
232 << formats.str() << " ]" << std::endl;
233 return false;
234 }
235
236 _state = state;
237 _updated = false;
238 _session = session;
239 _swapchain = new XRState::XRSwapchain(state, session,
240 view, rgbaFormat,
241 0, GL_DEPTH_COMPONENT16);
242 if (!_swapchain->valid()) {
243 OSG_WARN << "osgXR: Invalid custom swapchain" << std::endl;
244 _swapchain = nullptr;
245 return false;
246 }
247 _swapchain->setForcedAlpha(_forcedAlpha);
248 _swapchain->incNumDrawPasses(_numDrawPasses);
249
250 return true;
251 }
252
253 bool Swapchain::Private::sync()
254 {
255 if (_updated && _session.valid())
256 return setup(_state.get(), _session.get());
257 return true;
258 }
259
260 void Swapchain::Private::cleanupSession()
261 {
262 _swapchain = nullptr;
263 _session = nullptr;
264 _state = nullptr;
265 }
266
267 bool Swapchain::Private::valid() const
268 {
269 return _swapchain.valid();
270 }
271
272 bool Swapchain::Private::released() const
273 {
274 return _swapchain.valid() && _swapchain->released();
275 }
276
277 void Swapchain::Private::initialDrawCallback(osg::RenderInfo &renderInfo)
278 {
279 if (_swapchain.valid())
280 {
281 // FIXME this isn't the ideal place, but it'll probably do in practice
282 // due to lack of concurrency
283 sync();
284 osg::GraphicsOperation *graphicsOperation = renderInfo.getCurrentCamera()->getRenderer();
285 osgViewer::Renderer *renderer = dynamic_cast<osgViewer::Renderer*>(graphicsOperation);
286 if (renderer != nullptr)
287 {
288 // Disable normal OSG FBO camera setup because it will undo the MSAA FBO configuration.
289 renderer->setCameraRequiresSetUp(false);
290 }
291
292 _state->startRendering(renderInfo.getState()->getFrameStamp());
293 }
294 }
295
296 void Swapchain::Private::preDrawCallback(osg::RenderInfo &renderInfo)
297 {
298 if (_swapchain.valid())
299 _swapchain->preDrawCallback(renderInfo);
300 }
301
302 void Swapchain::Private::postDrawCallback(osg::RenderInfo &renderInfo)
303 {
304 if (_swapchain.valid())
305 {
306 _swapchain->setForcedAlpha(_forcedAlpha);
307 _swapchain->postDrawCallback(renderInfo);
308
309 const osg::FrameStamp *stamp = renderInfo.getState()->getFrameStamp();
310 auto texture = _swapchain->getOsgTexture(stamp);
311 if (texture.valid())
312 {
313 for (auto it = _stateSets.begin(); it != _stateSets.end(); ++it)
314 {
315 osg::ref_ptr<osg::StateSet> stateSet = *it;
316 if (stateSet.valid())
317 // update state set's texture object
318 stateSet->setTextureAttributeAndModes(0, texture);
319 else
320 // clean up after stale state sets
321 it = _stateSets.erase(it);
322 }
323 }
324
325 // FIXME somewhere we should remove or reset the texture:
326 // stateSet->removeTextureAttribute(0, osg::StateAttribute::Type::TEXTURE);
327 }
328 }
329
330 OpenXR::SwapchainGroupSubImage Swapchain::Private::convertSubImage(const SubImage &subImage) const
331 {
332 OpenXR::System::ViewConfiguration::View::Viewport vp = {};
333 vp.x = subImage.getX();
334 vp.y = subImage.getY();
335 vp.width = subImage.getWidth();
336 if (!vp.width)
337 vp.width = _width;
338 vp.height = subImage.getHeight();
339 if (!vp.height)
340 vp.height = _height;
341 return OpenXR::SwapchainGroupSubImage(_swapchain.get(), vp);
342 }
343
344 // Public API
345
346 Swapchain::Swapchain(uint32_t width, uint32_t height) :
347 _private(new Private(width, height))
348 {
349 }
350
351 Swapchain::~Swapchain()
352 {
353 }
354
355 void Swapchain::attachToCamera(osg::Camera *camera)
356 {
357 _private->attachToCamera(_private, camera);
358 }
359
360 void Swapchain::attachToMirror(osg::StateSet *stateSet)
361 {
362 _private->attachToMirror(_private, stateSet);
363 }
364
365 void Swapchain::preferRGBEncoding(Swapchain::Encoding encoding)
366 {
367 _private->preferRGBEncoding(encoding);
368 }
369
370 void Swapchain::allowRGBEncoding(Swapchain::Encoding encoding)
371 {
372 _private->allowRGBEncoding(encoding);
373 }
374
375 void Swapchain::setRGBBits(unsigned int rgbBits)
376 {
377 _private->setRGBBits(rgbBits);
378 }
379
380 unsigned int Swapchain::getRGBBits() const
381 {
382 return _private->getRGBBits();
383 }
384
385 void Swapchain::setAlphaBits(unsigned int alphaBits)
386 {
387 _private->setAlphaBits(alphaBits);
388 }
389
390 unsigned int Swapchain::getAlphaBits() const
391 {
392 return _private->getAlphaBits();
393 }
394
395 void Swapchain::setSize(uint32_t width, uint32_t height)
396 {
397 _private->setWidth(width);
398 _private->setHeight(height);
399 }
400
401 void Swapchain::setWidth(uint32_t width)
402 {
403 _private->setWidth(width);
404 }
405
406 uint32_t Swapchain::getWidth() const
407 {
408 return _private->getWidth();
409 }
410
411 void Swapchain::setHeight(uint32_t height)
412 {
413 _private->setHeight(height);
414 }
415
416 uint32_t Swapchain::getHeight() const
417 {
418 return _private->getHeight();
419 }
420
421 void Swapchain::setForcedAlpha(float alpha)
422 {
423 _private->setForcedAlpha(alpha);
424 }
425
426 void Swapchain::disableForcedAlpha()
427 {
428 _private->disableForcedAlpha();
429 }
430
431 float Swapchain::getForcedAlpha() const
432 {
433 return _private->getForcedAlpha();
434 }