From 092331232b6f92c9465468c6e9d8d9129c5ab539 Mon Sep 17 00:00:00 2001 From: Philip Rebohle Date: Sat, 30 Jun 2018 18:49:29 +0200 Subject: [PATCH] [vr] Initialize and shut down OpenVR if necessary --- src/dxvk/dxvk_openvr.cpp | 60 ++++++++++++++++++++++++++++++---------- src/dxvk/dxvk_openvr.h | 10 ++++--- 2 files changed, 52 insertions(+), 18 deletions(-) diff --git a/src/dxvk/dxvk_openvr.cpp b/src/dxvk/dxvk_openvr.cpp index ac6e3cde3..7b77923c0 100644 --- a/src/dxvk/dxvk_openvr.cpp +++ b/src/dxvk/dxvk_openvr.cpp @@ -7,9 +7,20 @@ #include +using VR_InitInternalProc = vr::IVRSystem* (VR_CALLTYPE *)(vr::EVRInitError*, vr::EVRApplicationType); +using VR_ShutdownInternalProc = void (VR_CALLTYPE *)(); +using VR_GetGenericInterfaceProc = void* (VR_CALLTYPE *)(const char*, vr::EVRInitError*); + namespace dxvk { - VrInstance g_vrInstance; + struct VrFunctions { + VR_InitInternalProc initInternal = nullptr; + VR_ShutdownInternalProc shutdownInternal = nullptr; + VR_GetGenericInterfaceProc getGenericInterface = nullptr; + }; + + VrFunctions g_vrFunctions; + VrInstance g_vrInstance; VrInstance:: VrInstance() { } VrInstance::~VrInstance() { } @@ -57,7 +68,7 @@ namespace dxvk { } if (m_initializedOpenVr) - /* shutdown */; + g_vrFunctions.shutdownInternal(); m_initializedDevExt = true; } @@ -79,7 +90,7 @@ namespace dxvk { } - vk::NameSet VrInstance::parseExtensionList(const std::string& str) { + vk::NameSet VrInstance::parseExtensionList(const std::string& str) const { vk::NameSet result; std::stringstream strstream(str); @@ -93,9 +104,6 @@ namespace dxvk { vr::IVRCompositor* VrInstance::getCompositor() { - using GetGenericInterfaceProc = - void* (VR_CALLTYPE *)(const char*, vr::EVRInitError*); - // Locate the OpenVR DLL if loaded by the process HMODULE ovrApi = ::GetModuleHandle("openvr_api.dll"); @@ -105,10 +113,11 @@ namespace dxvk { } // Load method used to retrieve the IVRCompositor interface - auto vrGetGenericInterface = reinterpret_cast( - ::GetProcAddress(ovrApi, "VR_GetGenericInterface")); + g_vrFunctions.initInternal = reinterpret_cast (::GetProcAddress(ovrApi, "VR_InitInternal")); + g_vrFunctions.shutdownInternal = reinterpret_cast (::GetProcAddress(ovrApi, "VR_ShutdownInternal")); + g_vrFunctions.getGenericInterface = reinterpret_cast(::GetProcAddress(ovrApi, "VR_GetGenericInterface")); - if (vrGetGenericInterface == nullptr) { + if (g_vrFunctions.getGenericInterface == nullptr) { Logger::warn("OpenVR: VR_GetGenericInterface not found"); return nullptr; } @@ -116,12 +125,35 @@ namespace dxvk { // Retrieve the compositor interface vr::EVRInitError error = vr::VRInitError_None; - auto compositor = reinterpret_cast( - vrGetGenericInterface(vr::IVRCompositor_Version, &error)); + vr::IVRCompositor* compositor = reinterpret_cast( + g_vrFunctions.getGenericInterface(vr::IVRCompositor_Version, &error)); - if (error != vr::VRInitError_None) { - Logger::warn(str::format("OpenVR: Failed to retrieve ", vr::IVRCompositor_Version)); - return nullptr; + if (error != vr::VRInitError_None || compositor == nullptr) { + if (g_vrFunctions.initInternal == nullptr + || g_vrFunctions.shutdownInternal == nullptr) { + Logger::warn("OpenVR: VR_InitInternal or VR_ShutdownInternal not found"); + return nullptr; + } + + // If the app has not initialized OpenVR yet, we need + // to do it now in order to grab a compositor instance + g_vrFunctions.initInternal(&error, vr::VRApplication_Background); + m_initializedOpenVr = error == vr::VRInitError_None; + + if (error != vr::VRInitError_None) { + Logger::warn("OpenVR: Failed to initialize OpenVR"); + return nullptr; + } + + compositor = reinterpret_cast( + g_vrFunctions.getGenericInterface(vr::IVRCompositor_Version, &error)); + + if (error != vr::VRInitError_None || compositor == nullptr) { + Logger::warn("OpenVR: Failed to query compositor interface"); + g_vrFunctions.shutdownInternal(); + m_initializedOpenVr = false; + return nullptr; + } } Logger::info("OpenVR: Compositor interface found"); diff --git a/src/dxvk/dxvk_openvr.h b/src/dxvk/dxvk_openvr.h index 1f96c2786..2d33aa3fe 100644 --- a/src/dxvk/dxvk_openvr.h +++ b/src/dxvk/dxvk_openvr.h @@ -7,12 +7,13 @@ namespace vr { class IVRCompositor; + class IVRSystem; } namespace dxvk { class DxvkInstance; - + /** * \brief OpenVR instance * @@ -64,7 +65,7 @@ namespace dxvk { private: std::mutex m_mutex; - vr::IVRCompositor* m_compositor; + vr::IVRCompositor* m_compositor = nullptr; bool m_initializedOpenVr = false; bool m_initializedInsExt = false; @@ -78,9 +79,10 @@ namespace dxvk { vk::NameSet queryDeviceExtensions( VkPhysicalDevice adapter) const; - static vk::NameSet parseExtensionList(const std::string& str); + vk::NameSet parseExtensionList( + const std::string& str) const; - static vr::IVRCompositor* getCompositor(); + vr::IVRCompositor* getCompositor(); };