Skyscraper 2.0
OgreOpenXRViewProjection.cpp
Go to the documentation of this file.
1/*
2 Skyscraper 2.0 - OpenXR View Projection
3 Portions Copyright (C)2024 Ryan Thoryk
4 MIT license - see LICENSE file
5 https://www.skyscrapersim.net
6 https://sourceforge.net/projects/skyscraper/
7 Contact - ryan@skyscrapersim.net
8*/
9
10/*
11 Original work produced by Glastonbridge Software Limited. This code is provided under the MIT license.
12 https://github.com/glastonbridge/OgreOpenXRRenderWindow
13*/
14
16
17#include "OgreOpenXRState.h"
18#include "XrUtility/XrError.h"
19
20#undef near
21#undef far
22
26
28{
29 uint32_t viewCount = 2;
30 ConfigViews.resize(viewCount, { XR_TYPE_VIEW_CONFIGURATION_VIEW });
32 xrEnumerateViewConfigurationViews(
33 state->GetInstanceHandle().Get(), state->GetSystemId(), state->primaryViewConfigType, viewCount, &viewCount, ConfigViews.data())
34 );
35
36 // Using texture array for better performance, so requiring left/right views have identical sizes.
37 const XrViewConfigurationView& view = ConfigViews[0];
38 CHECK(ConfigViews[0].recommendedImageRectWidth ==
39 ConfigViews[1].recommendedImageRectWidth);
40 CHECK(ConfigViews[0].recommendedImageRectHeight ==
41 ConfigViews[1].recommendedImageRectHeight);
42 CHECK(ConfigViews[0].recommendedSwapchainSampleCount ==
43 ConfigViews[1].recommendedSwapchainSampleCount);
44
45 Views.resize(viewCount, { XR_TYPE_VIEW });
46 ProjectionLayerViews.resize(viewCount);
47 DepthInfoViews.resize(viewCount);
48}
49
55void Ogre::OpenXRViewProjection::CalculateViewProjections(std::vector<xr::math::ViewProjection>& viewProjections)
56{
57 float far = 0.1f, near = 20.0f;
58 uint32_t viewCount = 2;
59 for (uint32_t i = 0; i < viewCount; ++i) {
60 viewProjections[i] = { Views[i].pose, Views[i].fov, {near, far} };
61
62 ProjectionLayerViews[i] = { XR_TYPE_COMPOSITION_LAYER_PROJECTION_VIEW };
63 ProjectionLayerViews[i].pose = Views[i].pose;
64 ProjectionLayerViews[i].fov = Views[i].fov;
65 ProjectionLayerViews[i].subImage.imageArrayIndex = 0;
66
67 DepthInfoViews[i] = { XR_TYPE_COMPOSITION_LAYER_DEPTH_INFO_KHR };
68 DepthInfoViews[i].minDepth = 0;
69 DepthInfoViews[i].maxDepth = 1;
70 DepthInfoViews[i].nearZ = near;
71 DepthInfoViews[i].farZ = far;
72 DepthInfoViews[i].subImage.imageArrayIndex = i;
73
74 // Chain depth info struct to the corresponding projection layer view's next pointer
75 //ProjectionLayerViews[i].next = &DepthInfoViews[i];
76 }
77}
78
80{
81 return Views.size();
82}
83
85{
86 return ConfigViews[0].recommendedImageRectHeight;
87}
88
90{
91 return ConfigViews[0].recommendedImageRectWidth;
92}
93
95{
96 return ConfigViews[0].recommendedSwapchainSampleCount;
97}
98
99void Ogre::OpenXRViewProjection::UpdateXrViewInfo(XrViewState& ViewState, OpenXRState* state, XrTime displayTime)
100{
101 XrViewLocateInfo viewLocateInfo{ XR_TYPE_VIEW_LOCATE_INFO };
102 viewLocateInfo.viewConfigurationType = OpenXRState::primaryViewConfigType;
103 viewLocateInfo.displayTime = displayTime;
104 viewLocateInfo.space = state->getAppSpace().Get();
105
106 // The output view count of xrLocateViews is always same as xrEnumerateViewConfigurationViews.
107 // Therefore, Views can be preallocated and avoid two call idiom here.
108 uint32_t viewCapacityInput = (uint32_t)Views.size();
109 uint32_t viewCountOutput;
111 xrLocateViews(state->GetSession().Get(),
112 &viewLocateInfo,
113 &ViewState,
114 viewCapacityInput,
115 &viewCountOutput,
116 Views.data()));
117
118 CHECK(viewCountOutput == viewCapacityInput);
119}
#define CHECK(exp)
Definition XrError.h:59
#define CHECK_XRCMD(cmd)
Definition XrError.h:14
const uint64_t GetSystemId()
xr::SpaceHandle & getAppSpace()
static constexpr XrViewConfigurationType primaryViewConfigType
const xr::InstanceHandle & GetInstanceHandle()
xr::SessionHandle & GetSession()
void CalculateViewProjections(std::vector< xr::math::ViewProjection > &viewProjections)
void UpdateXrViewInfo(XrViewState &ViewState, OpenXRState *state, XrTime displayTime)
void Initialize(OpenXRState *state)
HandleType Get() const noexcept
Definition XrHandle.h:51