| 1 | // SPDX-License-Identifier: LGPL-2.1-only |
| 2 | // Copyright (C) 2021 James Hogan <[email protected]> |
| 3 | |
| 4 | #include "Compositor.h" |
| 5 | #include "DepthInfo.h" |
| 6 | #include "Space.h" |
| 7 | #include "SwapchainGroupSubImage.h" |
| 8 | |
| 9 | #include <cassert> |
| 10 | |
| 11 | using namespace osgXR::OpenXR; |
| 12 | |
| 13 | // CompositionLayerProjection |
| 14 | |
| 15 | void CompositionLayerProjection::addView(osg::ref_ptr<Session::Frame> frame, uint32_t viewIndex, |
| 16 | const SwapchainGroup::SubImage &subImage, |
| 17 | const DepthInfo *depthInfo) |
| 18 | { |
| 19 | assert(viewIndex < _projViews.size()); |
| 20 | |
| 21 | XrCompositionLayerProjectionView &projView = _projViews[viewIndex]; |
| 22 | projView = { XR_TYPE_COMPOSITION_LAYER_PROJECTION_VIEW }; |
| 23 | projView.pose = frame->getViewPose(viewIndex); |
| 24 | projView.fov = frame->getViewFov(viewIndex); |
| 25 | subImage.getXrSubImage(&projView.subImage); |
| 26 | |
| 27 | if (depthInfo && subImage.depthValid()) |
| 28 | { |
| 29 | // depth info |
| 30 | XrCompositionLayerDepthInfoKHR &xrDepthInfo = _depthInfos[viewIndex]; |
| 31 | xrDepthInfo = { XR_TYPE_COMPOSITION_LAYER_DEPTH_INFO_KHR }; |
| 32 | subImage.getDepthXrSubImage(&xrDepthInfo.subImage); |
| 33 | xrDepthInfo.minDepth = depthInfo->getMinDepth(); |
| 34 | xrDepthInfo.maxDepth = depthInfo->getMaxDepth(); |
| 35 | xrDepthInfo.nearZ = depthInfo->getNearZ(); |
| 36 | xrDepthInfo.farZ = depthInfo->getFarZ(); |
| 37 | |
| 38 | // add depth info to projection view chain |
| 39 | projView.next = &xrDepthInfo; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | const XrCompositionLayerBaseHeader *CompositionLayerProjection::getXr() |
| 44 | { |
| 45 | unsigned int validDepthInfos = 0; |
| 46 | for (unsigned int i = 0; i < _projViews.size(); ++i) |
| 47 | { |
| 48 | auto &view = _projViews[i]; |
| 49 | auto &depthInfo = _depthInfos[i]; |
| 50 | if (view.type != XR_TYPE_COMPOSITION_LAYER_PROJECTION_VIEW) |
| 51 | { |
| 52 | // Eek, some views have been omitted! |
| 53 | OSG_WARN << "osgXR: Partial projection views!" << std::endl; |
| 54 | } |
| 55 | |
| 56 | if (depthInfo.type == XR_TYPE_COMPOSITION_LAYER_DEPTH_INFO_KHR) |
| 57 | ++validDepthInfos; |
| 58 | } |
| 59 | |
| 60 | // Sanity check that depth info is entirely missing or complete |
| 61 | if (validDepthInfos > 0 && validDepthInfos < _projViews.size()) |
| 62 | { |
| 63 | OSG_WARN << "osgXR: Partial projection depth info, disabling depth information" << std::endl; |
| 64 | for (auto &view: _projViews) |
| 65 | view.next = nullptr; |
| 66 | } |
| 67 | |
| 68 | _layer.layerFlags = _layerFlags; |
| 69 | _layer.space = _space->getXrSpace(); |
| 70 | _layer.viewCount = _projViews.size(); |
| 71 | _layer.views = _projViews.data(); |
| 72 | return reinterpret_cast<const XrCompositionLayerBaseHeader*>(&_layer); |
| 73 | } |
| 74 | |
| 75 | // CompositionLayerQuad |
| 76 | |
| 77 | void CompositionLayerQuad::setSubImage(const SwapchainGroup::SubImage &subImage) |
| 78 | { |
| 79 | subImage.getXrSubImage(&_layer.subImage); |
| 80 | } |
| 81 | |
| 82 | const XrCompositionLayerBaseHeader *CompositionLayerQuad::getXr() |
| 83 | { |
| 84 | _layer.layerFlags = _layerFlags; |
| 85 | _layer.space = _space->getXrSpace(); |
| 86 | return reinterpret_cast<const XrCompositionLayerBaseHeader*>(&_layer); |
| 87 | } |