OpenXr: Use C++20 std::optional.

This commit is contained in:
Lubosz Sarnecki 2024-09-04 21:17:26 +02:00
parent bf64af0bcb
commit b351af1d4f
4 changed files with 13 additions and 17 deletions

View file

@ -9,6 +9,7 @@
#pragma once #pragma once
#include <optional>
#include <openxr/openxr.h> #include <openxr/openxr.h>
@ -58,9 +59,7 @@ public:
XrPath _handPaths[HAND_COUNT]; XrPath _handPaths[HAND_COUNT];
controller::Pose _lastHeadPose; controller::Pose _lastHeadPose;
XrTime _lastPredictedDisplayTime; std::optional<XrTime> _lastPredictedDisplayTime;
// TODO: Enable C++17 and use std::optional
bool _lastPredictedDisplayTimeInitialized = false;
bool _shouldQuit = false; bool _shouldQuit = false;
bool _shouldRunFrameCycle = false; bool _shouldRunFrameCycle = false;

View file

@ -67,13 +67,13 @@ inline static glm::mat4 fovToProjection(const XrFovf fov, const float near, cons
} }
glm::mat4 OpenXrDisplayPlugin::getEyeProjection(Eye eye, const glm::mat4& baseProjection) const { glm::mat4 OpenXrDisplayPlugin::getEyeProjection(Eye eye, const glm::mat4& baseProjection) const {
if (!_viewsInitialized) { if (!_views.has_value()) {
return baseProjection; return baseProjection;
} }
ViewFrustum frustum; ViewFrustum frustum;
frustum.setProjection(baseProjection); frustum.setProjection(baseProjection);
return fovToProjection(_views[(eye == Left) ? 0 : 1].fov, frustum.getNearClip(), frustum.getFarClip()); return fovToProjection(_views.value()[(eye == Left) ? 0 : 1].fov, frustum.getNearClip(), frustum.getFarClip());
} }
// TODO: This apparently wasn't right in the OpenVR plugin, but this is what it basically did. // TODO: This apparently wasn't right in the OpenVR plugin, but this is what it basically did.
@ -99,9 +99,11 @@ bool OpenXrDisplayPlugin::initViews() {
assert(_viewCount != 0); assert(_viewCount != 0);
_views = std::vector<XrView>();
for (uint32_t i = 0; i < _viewCount; i++) { for (uint32_t i = 0; i < _viewCount; i++) {
XrView view = { .type = XR_TYPE_VIEW }; XrView view = { .type = XR_TYPE_VIEW };
_views.push_back(view); _views.value().push_back(view);
XrViewConfigurationView viewConfig = { .type = XR_TYPE_VIEW_CONFIGURATION_VIEW }; XrViewConfigurationView viewConfig = { .type = XR_TYPE_VIEW_CONFIGURATION_VIEW };
_viewConfigs.push_back(viewConfig); _viewConfigs.push_back(viewConfig);
@ -363,7 +365,6 @@ bool OpenXrDisplayPlugin::beginFrameRender(uint32_t frameIndex) {
return false; return false;
_context->_lastPredictedDisplayTime = _lastFrameState.predictedDisplayTime; _context->_lastPredictedDisplayTime = _lastFrameState.predictedDisplayTime;
_context->_lastPredictedDisplayTimeInitialized = true;
std::vector<XrView> eye_views(_viewCount); std::vector<XrView> eye_views(_viewCount);
for (uint32_t i = 0; i < _viewCount; i++) { for (uint32_t i = 0; i < _viewCount; i++) {
@ -399,17 +400,15 @@ bool OpenXrDisplayPlugin::beginFrameRender(uint32_t frameIndex) {
.space = _context->_stageSpace, .space = _context->_stageSpace,
}; };
result = xrLocateViews(_context->_session, &viewLocateInfo, &_lastViewState, _viewCount, &_viewCount, _views.data()); result = xrLocateViews(_context->_session, &viewLocateInfo, &_lastViewState, _viewCount, &_viewCount, _views.value().data());
if (!xrCheck(_context->_instance, result, "Could not locate views")) if (!xrCheck(_context->_instance, result, "Could not locate views"))
return false; return false;
for (uint32_t i = 0; i < _viewCount; i++) { for (uint32_t i = 0; i < _viewCount; i++) {
_projectionLayerViews[i].pose = _views[i].pose; _projectionLayerViews[i].pose = _views.value()[i].pose;
_projectionLayerViews[i].fov = _views[i].fov; _projectionLayerViews[i].fov = _views.value()[i].fov;
} }
_viewsInitialized = true;
XrSpaceLocation headLocation = { XrSpaceLocation headLocation = {
.type = XR_TYPE_SPACE_LOCATION, .type = XR_TYPE_SPACE_LOCATION,
.pose = XR_INDENTITY_POSE, .pose = XR_INDENTITY_POSE,

View file

@ -66,9 +66,7 @@ private:
uint32_t _viewCount = 0; uint32_t _viewCount = 0;
std::vector<XrCompositionLayerProjectionView> _projectionLayerViews; std::vector<XrCompositionLayerProjectionView> _projectionLayerViews;
std::vector<XrView> _views; std::optional<std::vector<XrView>> _views;
// TODO: Enable C++17 and use std::optional
bool _viewsInitialized = false;
std::vector<XrViewConfigurationView> _viewConfigs; std::vector<XrViewConfigurationView> _viewConfigs;

View file

@ -228,8 +228,8 @@ XrSpaceLocation OpenXrInputPlugin::Action::getPose(uint32_t handId) {
.type = XR_TYPE_SPACE_LOCATION, .type = XR_TYPE_SPACE_LOCATION,
}; };
if (_context->_lastPredictedDisplayTimeInitialized) { if (_context->_lastPredictedDisplayTime.has_value()) {
result = xrLocateSpace(_poseSpaces[handId], _context->_stageSpace, _context->_lastPredictedDisplayTime, &location); result = xrLocateSpace(_poseSpaces[handId], _context->_stageSpace, _context->_lastPredictedDisplayTime.value(), &location);
xrCheck(_context->_instance, result, "Failed to locate hand space!"); xrCheck(_context->_instance, result, "Failed to locate hand space!");
} }