Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions DrvOpenXR/DrvOpenXR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,11 @@ struct InstancePlan {
XrGraphicsApiSupportedFlags apiFlags = 0;
};

// The running instance's plan, set once at start-up: an instance made again after a loss has the same
static InstancePlan currentPlan;
// A new instance after a loss whose headset isn't there yet (RemakeInstance keeps it); until FullShutdown
static bool madeAgain = false;

// The extensions OpenLamina enables, of those available, for a session of that kind
InstancePlan PlanInstance(const std::set<std::string>& available, SessionKind kind)
{
Expand Down Expand Up @@ -521,6 +526,7 @@ IBackend* DrvOpenXR::CreateOpenXRBackend(const char* startupInfo, vr::EVRInitErr
// Create the OpenXR instance - this is the overall handle that connects us to the runtime, or take the one
// VR_IsHmdPresent made for a scene application
const InstancePlan plan = PlanInstance(availableExtensions, choice.kind);
currentPlan = plan;
const std::vector<const char*>& extensions = plan.extensions;
const XrGraphicsApiSupportedFlags apiFlags = plan.apiFlags;
xr_instance = TakeProbedInstance(plan, &xr_instance_version);
Expand Down Expand Up @@ -656,6 +662,45 @@ XrResult DrvOpenXR::FindSystem()
return CreateSystemID();
}

XrResult DrvOpenXR::RemakeInstance()
{
#ifdef _DEBUG
// It went with the old instance
dbgMessenger = NULL;
#endif
// A new instance made at an earlier try is kept while the headset isn't there yet
if (!madeAgain) {
if (xr_instance) {
if (XR_FAILED(xrDestroyInstance(xr_instance)))
OOVR_LOG("xrDestroyInstance of the lost instance failed");
xr_instance = XR_NULL_HANDLE;
}
if (XrResult res = MakeInstance(currentPlan, &xr_instance, &xr_instance_version); XR_FAILED(res)) {
OOVR_LOGF("The OpenXR instance can't be made again yet: %d", (int)res);
xr_instance = XR_NULL_HANDLE;
return res;
}
// The old function table isn't freed: a thread outside the session lock may still be reading it
xr_ext = new XrExt(currentPlan.apiFlags, currentPlan.extensions);
madeAgain = true;
}
if (XrResult res = CreateSystemID(); XR_FAILED(res))
return res;
uint32_t viewCount = 0;
if (XrResult res = xrEnumerateViewConfigurationViews(xr_instance, xr_system, XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO, 0, &viewCount, nullptr); XR_FAILED(res))
return res;
std::vector<XrViewConfigurationView> views(viewCount, { XR_TYPE_VIEW_CONFIGURATION_VIEW });
if (XrResult res = xrEnumerateViewConfigurationViews(xr_instance, xr_system, XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO, viewCount, &viewCount, views.data()); XR_FAILED(res))
return res;
// The game sized its textures for two views: a headset with another count isn't one to go on with
if (viewCount != XruEyeCount)
return XR_ERROR_VIEW_CONFIGURATION_TYPE_UNSUPPORTED;
xr_views_list = views;
madeAgain = false; // the next loss makes another
OOVR_LOG("Made the OpenXR instance again");
return XR_SUCCESS;
}

void DrvOpenXR::KeepOverlaysInstance()
{
if (currentBackend)
Expand Down Expand Up @@ -726,6 +771,7 @@ void DrvOpenXR::FullShutdown()
}

initialised = false;
madeAgain = false;
currentBackend = nullptr;
SetCurrentSessionKind(SessionKind::Scene);
}
108 changes: 100 additions & 8 deletions DrvOpenXR/XrBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2119,6 +2119,10 @@ void XrBackend::PumpEvents()
std::unique_lock<std::mutex> pumpLock(pumping, std::try_to_lock);
if (!pumpLock.owns_lock())
return;
// Nor while another thread makes the instance or session again (it holds the session lock, and pumps itself)
SessionLock sessionLock(xr_session, SessionLock::TryShared{});
if (!sessionLock.IsHeld())
return;

// Poll for OpenXR events
while (true) {
Expand All @@ -2127,7 +2131,7 @@ void XrBackend::PumpEvents()
if (XR_FAILED(res)) {
OOVR_LOG_ONCE("xrPollEvent failed: no events this frame");
if (res == XR_ERROR_INSTANCE_LOST)
OnSessionLost();
OnInstanceLost();
break;
}

Expand All @@ -2152,11 +2156,11 @@ void XrBackend::PumpEvents()
continue;
}

// The instance is going away (the runtime updating or shutting down): its session with it
// The instance is going away (the runtime updating or shutting down): it's made again, with its session
if (ev.type == XR_TYPE_EVENT_DATA_INSTANCE_LOSS_PENDING) {
OOVR_LOG("The OpenXR instance will be lost");
if (!exitExpected)
OnSessionLost();
OnInstanceLost();
continue;
}

Expand Down Expand Up @@ -2379,7 +2383,9 @@ bool XrBackend::WaitForEvents(const std::function<bool()>& done, int timeoutMs)
void XrBackend::OnFrameCallFailed(XrResult res)
{
renderingFrame = false;
if (res == XR_ERROR_SESSION_LOST || res == XR_ERROR_INSTANCE_LOST || ++frameCallFailures >= 5)
if (res == XR_ERROR_INSTANCE_LOST)
OnInstanceLost();
else if (res == XR_ERROR_SESSION_LOST || ++frameCallFailures >= 5)
OnSessionLost();
// No waited frame to pace the game: about one frame's wait instead of a busy loop
std::this_thread::sleep_for(std::chrono::milliseconds(11));
Expand All @@ -2403,6 +2409,70 @@ void XrBackend::OnSessionLost()
}
}

void XrBackend::OnInstanceLost()
{
if (!instanceLost.exchange(true))
OOVR_LOG("The OpenXR instance is lost: making it again once the runtime is back");
OnSessionLost();
}

// A new instance's xrCreateSession wants the graphics requirements of the binding's API asked of it first
static void AskGraphicsRequirements(const void* binding)
{
const auto* header = (const XrBaseInStructure*)binding;
if (!header)
return;
switch (header->type) {
#if defined(_WIN32) && defined(SUPPORT_DX) && defined(SUPPORT_DX11)
case XR_TYPE_GRAPHICS_BINDING_D3D11_KHR: {
XrGraphicsRequirementsD3D11KHR requirements{ XR_TYPE_GRAPHICS_REQUIREMENTS_D3D11_KHR };
OC_XR_LOG(xr_ext->xrGetD3D11GraphicsRequirementsKHR(xr_instance, xr_system, &requirements));
break;
}
#endif
#if defined(_WIN32) && defined(SUPPORT_DX) && defined(SUPPORT_DX12)
case XR_TYPE_GRAPHICS_BINDING_D3D12_KHR: {
XrGraphicsRequirementsD3D12KHR requirements{ XR_TYPE_GRAPHICS_REQUIREMENTS_D3D12_KHR };
OC_XR_LOG(xr_ext->xrGetD3D12GraphicsRequirementsKHR(xr_instance, xr_system, &requirements));
break;
}
#endif
#if defined(SUPPORT_VK)
case XR_TYPE_GRAPHICS_BINDING_VULKAN_KHR: {
XrGraphicsRequirementsVulkanKHR requirements{ XR_TYPE_GRAPHICS_REQUIREMENTS_VULKAN_KHR };
OC_XR_LOG(xr_ext->xrGetVulkanGraphicsRequirementsKHR(xr_instance, xr_system, &requirements));
// The session's device must be the one the new instance names
const auto* vk = (const XrGraphicsBindingVulkanKHR*)binding;
VkPhysicalDevice wanted = VK_NULL_HANDLE;
if (XR_SUCCEEDED(xr_ext->xrGetVulkanGraphicsDeviceKHR(xr_instance, xr_system, vk->instance, &wanted)) && wanted != vk->physicalDevice)
OOVR_LOG("The new OpenXR instance wants another Vulkan device than the session's: its session may fail");
break;
}
#endif
#if defined(SUPPORT_GL)
#ifdef _WIN32
case XR_TYPE_GRAPHICS_BINDING_OPENGL_WIN32_KHR:
#else
case XR_TYPE_GRAPHICS_BINDING_OPENGL_XLIB_KHR:
#endif
{
XrGraphicsRequirementsOpenGLKHR requirements{ XR_TYPE_GRAPHICS_REQUIREMENTS_OPENGL_KHR };
OC_XR_LOG(xr_ext->xrGetOpenGLGraphicsRequirementsKHR(xr_instance, xr_system, &requirements));
break;
}
#endif
#if defined(SUPPORT_GLES)
case XR_TYPE_GRAPHICS_BINDING_OPENGL_ES_ANDROID_KHR: {
XrGraphicsRequirementsOpenGLESKHR requirements{ XR_TYPE_GRAPHICS_REQUIREMENTS_OPENGL_ES_KHR };
OC_XR_LOG(xr_ext->xrGetOpenGLESGraphicsRequirementsKHR(xr_instance, xr_system, &requirements));
break;
}
#endif
default:
break;
}
}

void XrBackend::RecoverLostSession(std::mutex* passLock)
{
if (!sessionLost || quitting || ownFramesStopped)
Expand Down Expand Up @@ -2432,16 +2502,38 @@ void XrBackend::RecoverLostSession(std::mutex* passLock)
pass = std::unique_lock<std::mutex>(*passLock);
if (!sessionLost || quitting)
return;
// The lost session first, once: its handles are no use any more
// With a lost instance, the game's actions go first (forgotten: they went with it, and are made again on the new
// one), with the paths and the trackers' list of devices made on it. The info set goes with the session.
if (instanceLost) {
if (BaseInput* input = GetUnsafeBaseInput())
input->_ForgetInstance();
subactionPaths[0] = subactionPaths[1] = XR_NULL_PATH;
generic_trackers_generation = UINT64_MAX;
generic_trackers_session = XR_NULL_HANDLE;
}
// The lost session, once: its handles are no use any more
if (!lostSessionDestroyed) {
if (xr_gbl || xr_session.get())
DrvOpenXR::ShutdownSession();
lostSessionDestroyed = true;
}
if (XR_FAILED(DrvOpenXR::FindSystem()))
if (instanceLost) {
if (XR_FAILED(DrvOpenXR::RemakeInstance()))
return; // no runtime or headset yet: the next try in a second
instanceLost = false;
} else if (XrResult res = DrvOpenXR::FindSystem(); XR_FAILED(res)) {
if (res == XR_ERROR_INSTANCE_LOST)
instanceLost = true; // the instance went too while the session was lost: made again at the next try
return; // no headset yet: the next try in a second
if (XR_SUCCEEDED(RebuildSession(RebuildReason::Loss)) && !sessionLost)
OOVR_LOG("The OpenXR session is back");
}
// Asked of the system found (on a new instance, or a new system id), as xrCreateSession wants
AskGraphicsRequirements(GetCurrentGraphicsBinding());
if (XR_FAILED(RebuildSession(RebuildReason::Loss)) || sessionLost)
return;
// After a new instance: the game's actions and bindings, from its manifest, on it and its session
if (BaseInput* input = GetUnsafeBaseInput())
input->_RebuildForNewInstance();
OOVR_LOG("The OpenXR session is back");
}

// Own frames call xrEndFrame on their own thread, where the runtime may use the session's device while the game's
Expand Down
4 changes: 4 additions & 0 deletions DrvOpenXR/XrBackend.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ class XrBackend : public IBackend {
std::atomic<bool> sawStopping{ false };
// The game has been told the devices are deactivated (a loss), and not yet that they're back
std::atomic<bool> devicesDeactivated{ false };
// The instance was lost with the session (XR_ERROR_INSTANCE_LOST, or INSTANCE_LOSS_PENDING): made again first
std::atomic<bool> instanceLost{ false };
// The instance loss: lost with the session (OnSessionLost)
void OnInstanceLost();
// The last try to find the system again (RecoverLostSession), and whether the lost session is destroyed yet
std::atomic<std::chrono::steady_clock::rep> lastRecoveryTry{ 0 }; // 0 before the first
bool lostSessionDestroyed = false;
Expand Down
3 changes: 3 additions & 0 deletions DrvOpenXR/XrDriverPrivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,8 @@ XrResult SetupSession();
void ShutdownSession();
// xrGetSystem for the headset again (after a loss): XR_SUCCESS once it is there
XrResult FindSystem();
// After the instance was lost (its session already ended): the old instance destroyed, a new one made on the same
// extensions, with its function pointers, system and views. XR_SUCCESS once there's a headset on it.
XrResult RemakeInstance();
void FullShutdown();
} // namespace DrvOpenXR
2 changes: 2 additions & 0 deletions OpenOVR/Reimpl/BaseCompositor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,7 @@ void BaseCompositor::UnlockGLSharedTextureForAccess(glSharedTextureHandle_t glSh

uint32_t BaseCompositor::GetVulkanInstanceExtensionsRequired(char* pchValue, uint32_t unBufferSize)
{
auto lock = xr_session.lock_shared(); // held while asking: a new instance waits for it
if (pchValue && unBufferSize)
pchValue[0] = 0;
#if defined(SUPPORT_VK)
Expand All @@ -914,6 +915,7 @@ uint32_t BaseCompositor::GetVulkanInstanceExtensionsRequired(char* pchValue, uin

uint32_t BaseCompositor::GetVulkanDeviceExtensionsRequired(VkPhysicalDevice_T* pPhysicalDevice, char* pchValue, uint32_t unBufferSize)
{
auto lock = xr_session.lock_shared(); // held while asking: a new instance waits for it
if (pchValue && unBufferSize)
pchValue[0] = 0;
#if defined(SUPPORT_VK)
Expand Down
Loading
Loading