From 710da838d8867cacd2427bc0db5e5e914eda628f Mon Sep 17 00:00:00 2001 From: Lubosz Sarnecki Date: Wed, 4 Sep 2024 21:17:26 +0200 Subject: [PATCH] OpenXr: Use C++20 std::optional. --- plugins/openxr/src/OpenXrContext.h | 5 ++--- plugins/openxr/src/OpenXrDisplayPlugin.cpp | 17 ++++++++--------- plugins/openxr/src/OpenXrDisplayPlugin.h | 4 +--- plugins/openxr/src/OpenXrInputPlugin.cpp | 4 ++-- 4 files changed, 13 insertions(+), 17 deletions(-) diff --git a/plugins/openxr/src/OpenXrContext.h b/plugins/openxr/src/OpenXrContext.h index eed18eeeb8..82baf098f5 100644 --- a/plugins/openxr/src/OpenXrContext.h +++ b/plugins/openxr/src/OpenXrContext.h @@ -9,6 +9,7 @@ #pragma once +#include #include @@ -58,9 +59,7 @@ public: XrPath _handPaths[HAND_COUNT]; controller::Pose _lastHeadPose; - XrTime _lastPredictedDisplayTime; - // TODO: Enable C++17 and use std::optional - bool _lastPredictedDisplayTimeInitialized = false; + std::optional _lastPredictedDisplayTime; bool _shouldQuit = false; bool _shouldRunFrameCycle = false; diff --git a/plugins/openxr/src/OpenXrDisplayPlugin.cpp b/plugins/openxr/src/OpenXrDisplayPlugin.cpp index fd9177143a..efd8952a7e 100644 --- a/plugins/openxr/src/OpenXrDisplayPlugin.cpp +++ b/plugins/openxr/src/OpenXrDisplayPlugin.cpp @@ -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 { - if (!_viewsInitialized) { + if (!_views.has_value()) { return baseProjection; } ViewFrustum frustum; 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. @@ -99,9 +99,11 @@ bool OpenXrDisplayPlugin::initViews() { assert(_viewCount != 0); + _views = std::vector(); + for (uint32_t i = 0; i < _viewCount; i++) { XrView view = { .type = XR_TYPE_VIEW }; - _views.push_back(view); + _views.value().push_back(view); XrViewConfigurationView viewConfig = { .type = XR_TYPE_VIEW_CONFIGURATION_VIEW }; _viewConfigs.push_back(viewConfig); @@ -363,7 +365,6 @@ bool OpenXrDisplayPlugin::beginFrameRender(uint32_t frameIndex) { return false; _context->_lastPredictedDisplayTime = _lastFrameState.predictedDisplayTime; - _context->_lastPredictedDisplayTimeInitialized = true; std::vector eye_views(_viewCount); for (uint32_t i = 0; i < _viewCount; i++) { @@ -399,17 +400,15 @@ bool OpenXrDisplayPlugin::beginFrameRender(uint32_t frameIndex) { .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")) return false; for (uint32_t i = 0; i < _viewCount; i++) { - _projectionLayerViews[i].pose = _views[i].pose; - _projectionLayerViews[i].fov = _views[i].fov; + _projectionLayerViews[i].pose = _views.value()[i].pose; + _projectionLayerViews[i].fov = _views.value()[i].fov; } - _viewsInitialized = true; - XrSpaceLocation headLocation = { .type = XR_TYPE_SPACE_LOCATION, .pose = XR_INDENTITY_POSE, diff --git a/plugins/openxr/src/OpenXrDisplayPlugin.h b/plugins/openxr/src/OpenXrDisplayPlugin.h index ac83c8e094..01df5fcf27 100644 --- a/plugins/openxr/src/OpenXrDisplayPlugin.h +++ b/plugins/openxr/src/OpenXrDisplayPlugin.h @@ -66,9 +66,7 @@ private: uint32_t _viewCount = 0; std::vector _projectionLayerViews; - std::vector _views; - // TODO: Enable C++17 and use std::optional - bool _viewsInitialized = false; + std::optional> _views; std::vector _viewConfigs; diff --git a/plugins/openxr/src/OpenXrInputPlugin.cpp b/plugins/openxr/src/OpenXrInputPlugin.cpp index c16581efb5..f9823adf70 100644 --- a/plugins/openxr/src/OpenXrInputPlugin.cpp +++ b/plugins/openxr/src/OpenXrInputPlugin.cpp @@ -228,8 +228,8 @@ XrSpaceLocation OpenXrInputPlugin::Action::getPose(uint32_t handId) { .type = XR_TYPE_SPACE_LOCATION, }; - if (_context->_lastPredictedDisplayTimeInitialized) { - result = xrLocateSpace(_poseSpaces[handId], _context->_stageSpace, _context->_lastPredictedDisplayTime, &location); + if (_context->_lastPredictedDisplayTime.has_value()) { + result = xrLocateSpace(_poseSpaces[handId], _context->_stageSpace, _context->_lastPredictedDisplayTime.value(), &location); xrCheck(_context->_instance, result, "Failed to locate hand space!"); }