From de29174d42c6f4b68dc797d39c871e4903ba7751 Mon Sep 17 00:00:00 2001
From: Philip Rebohle <philip.rebohle@tu-dortmund.de>
Date: Tue, 4 Dec 2018 18:50:11 +0100
Subject: [PATCH] Revert "[vr] Cosmetic code cleanup"

This reverts commit a3bf90f5a3fe050ffae353f80bc5608c7c9a1244.

Crashes SuperHot VR for some reason.
---
 src/dxvk/dxvk_openvr.cpp | 89 ++++++++++++++++++----------------------
 src/dxvk/dxvk_openvr.h   | 18 +++-----
 2 files changed, 45 insertions(+), 62 deletions(-)

diff --git a/src/dxvk/dxvk_openvr.cpp b/src/dxvk/dxvk_openvr.cpp
index 96556555..d667314f 100644
--- a/src/dxvk/dxvk_openvr.cpp
+++ b/src/dxvk/dxvk_openvr.cpp
@@ -57,10 +57,10 @@ namespace dxvk {
   void VrInstance::initInstanceExtensions() {
     std::lock_guard<std::mutex> lock(m_mutex);
 
-    if (!m_compositor)
+    if (m_compositor == nullptr)
       m_compositor = this->getCompositor();
 
-    if (!m_compositor || m_initializedInsExt)
+    if (m_compositor == nullptr || m_initializedInsExt)
       return;
     
     m_insExtensions = this->queryInstanceExtensions();
@@ -71,7 +71,7 @@ namespace dxvk {
   void VrInstance::initDeviceExtensions(const DxvkInstance* instance) {
     std::lock_guard<std::mutex> lock(m_mutex);
 
-    if (!m_compositor || m_initializedDevExt)
+    if (m_compositor == nullptr || m_initializedDevExt)
       return;
     
     for (uint32_t i = 0; instance->enumAdapters(i) != nullptr; i++) {
@@ -121,20 +121,40 @@ namespace dxvk {
     // Locate the OpenVR DLL if loaded by the process. Some
     // applications may not have OpenVR loaded at the time
     // they create the DXGI instance, so we try our own DLL.
-    m_ovrApi = this->loadLibrary();
-    m_loadedOvrApi = m_ovrApi != nullptr;
+#ifdef __WINE__
+    // on winelib, load native openvr_api directly
+    m_ovrApi = ::dlopen("libopenvr_api.so", RTLD_LAZY | RTLD_NOLOAD);
+
+    if (m_ovrApi == nullptr) {
+      m_ovrApi = ::dlopen("libopenvr_api_dxvk.so", RTLD_LAZY | RTLD_LOCAL);
+      m_loadedOvrApi = m_ovrApi != nullptr;
+    }
+#else
+    m_ovrApi = ::GetModuleHandle("openvr_api.dll");
+
+    if (m_ovrApi == nullptr) {
+      m_ovrApi = ::LoadLibrary("openvr_api_dxvk.dll");
+      m_loadedOvrApi = m_ovrApi != nullptr;
+    }
+#endif
     
-    if (!m_ovrApi) {
+    if (m_ovrApi == nullptr) {
       Logger::warn("OpenVR: Failed to locate module");
       return nullptr;
     }
     
     // Load method used to retrieve the IVRCompositor interface
-    g_vrFunctions.initInternal        = reinterpret_cast<VR_InitInternalProc>       (this->getSym("VR_InitInternal"));
-    g_vrFunctions.shutdownInternal    = reinterpret_cast<VR_ShutdownInternalProc>   (this->getSym("VR_ShutdownInternal"));
-    g_vrFunctions.getGenericInterface = reinterpret_cast<VR_GetGenericInterfaceProc>(this->getSym("VR_GetGenericInterface"));
+#ifdef __WINE__
+    g_vrFunctions.initInternal        = reinterpret_cast<VR_InitInternalProc>       (::dlsym(m_ovrApi, "VR_InitInternal"));
+    g_vrFunctions.shutdownInternal    = reinterpret_cast<VR_ShutdownInternalProc>   (::dlsym(m_ovrApi, "VR_ShutdownInternal"));
+    g_vrFunctions.getGenericInterface = reinterpret_cast<VR_GetGenericInterfaceProc>(::dlsym(m_ovrApi, "VR_GetGenericInterface"));
+#else
+    g_vrFunctions.initInternal        = reinterpret_cast<VR_InitInternalProc>       (::GetProcAddress(m_ovrApi, "VR_InitInternal"));
+    g_vrFunctions.shutdownInternal    = reinterpret_cast<VR_ShutdownInternalProc>   (::GetProcAddress(m_ovrApi, "VR_ShutdownInternal"));
+    g_vrFunctions.getGenericInterface = reinterpret_cast<VR_GetGenericInterfaceProc>(::GetProcAddress(m_ovrApi, "VR_GetGenericInterface"));
+#endif
     
-    if (!g_vrFunctions.getGenericInterface) {
+    if (g_vrFunctions.getGenericInterface == nullptr) {
       Logger::warn("OpenVR: VR_GetGenericInterface not found");
       return nullptr;
     }
@@ -145,9 +165,9 @@ namespace dxvk {
     vr::IVRCompositor* compositor = reinterpret_cast<vr::IVRCompositor*>(
       g_vrFunctions.getGenericInterface(vr::IVRCompositor_Version, &error));
     
-    if (error != vr::VRInitError_None || !compositor) {
-      if (!g_vrFunctions.initInternal
-       || !g_vrFunctions.shutdownInternal) {
+    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;
       }
@@ -165,7 +185,7 @@ namespace dxvk {
       compositor = reinterpret_cast<vr::IVRCompositor*>(
         g_vrFunctions.getGenericInterface(vr::IVRCompositor_Version, &error));
       
-      if (error != vr::VRInitError_None || !compositor) {
+      if (error != vr::VRInitError_None || compositor == nullptr) {
         Logger::warn("OpenVR: Failed to query compositor interface");
         this->shutdown();
         return nullptr;
@@ -181,45 +201,16 @@ namespace dxvk {
     if (m_initializedOpenVr)
       g_vrFunctions.shutdownInternal();
     
+#if __WINE__
     if (m_loadedOvrApi)
-      this->freeLibrary();
+      ::dlclose(m_ovrApi);
+#else
+    if (m_loadedOvrApi)
+      ::FreeLibrary(m_ovrApi);
+#endif
     
     m_initializedOpenVr = false;
     m_loadedOvrApi      = false;
   }
-
-
-  SoHandle VrInstance::loadLibrary() {
-    SoHandle handle = nullptr;
-    #ifdef __WINE__
-    // on winelib, load native openvr_api directly
-    if (!(handle = ::dlopen("libopenvr_api.so", RTLD_LAZY | RTLD_NOLOAD)))
-      handle = ::dlopen("libopenvr_api_dxvk.so", RTLD_LAZY | RTLD_LOCAL);
-    #else
-    if (!(handle = ::GetModuleHandle("openvr_api.dll")))
-      handle = ::LoadLibrary("openvr_api_dxvk.dll");
-    #endif
-    return handle;
-  }
-
-
-  void VrInstance::freeLibrary() {
-    #ifdef __WINE__
-    ::dlclose(m_ovrApi);
-    #else
-    ::FreeLibrary(m_ovrApi);
-    #endif
-  }
-
-  
-  void* VrInstance::getSym(const char* sym) {
-    #ifdef __WINE__
-    return reinterpret_cast<void*>(
-      ::dlsym(m_ovrApi, sym));
-    #else
-    return reinterpret_cast<void*>(
-      ::GetProcAddress(m_ovrApi, sym));
-    #endif
-  }
   
 }
diff --git a/src/dxvk/dxvk_openvr.h b/src/dxvk/dxvk_openvr.h
index f5058c87..858f57c3 100644
--- a/src/dxvk/dxvk_openvr.h
+++ b/src/dxvk/dxvk_openvr.h
@@ -5,12 +5,6 @@
 
 #include "dxvk_include.h"
 
-#ifdef __WINE__
-using SoHandle = void*;
-#else
-using SoHandle = HMODULE;
-#endif
-
 namespace vr {
   class IVRCompositor;
   class IVRSystem;
@@ -72,7 +66,11 @@ namespace dxvk {
 
     std::mutex            m_mutex;
     vr::IVRCompositor*    m_compositor = nullptr;
-    SoHandle              m_ovrApi     = nullptr;
+#ifdef __WINE__
+    void*                 m_ovrApi     = nullptr;
+#else
+    HMODULE               m_ovrApi     = nullptr;
+#endif
 
     bool m_loadedOvrApi      = false;
     bool m_initializedOpenVr = false;
@@ -93,12 +91,6 @@ namespace dxvk {
     vr::IVRCompositor* getCompositor();
 
     void shutdown();
-
-    SoHandle loadLibrary();
-
-    void freeLibrary();
-
-    void* getSym(const char* sym);
     
   };