[vr] Initialize and shut down OpenVR if necessary

This commit is contained in:
Philip Rebohle 2018-06-30 18:49:29 +02:00
parent cf6e3c1fbf
commit 092331232b
No known key found for this signature in database
GPG Key ID: C8CC613427A31C99
2 changed files with 52 additions and 18 deletions

View File

@ -7,9 +7,20 @@
#include <openvr/openvr.hpp>
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<GetGenericInterfaceProc>(
::GetProcAddress(ovrApi, "VR_GetGenericInterface"));
g_vrFunctions.initInternal = reinterpret_cast<VR_InitInternalProc> (::GetProcAddress(ovrApi, "VR_InitInternal"));
g_vrFunctions.shutdownInternal = reinterpret_cast<VR_ShutdownInternalProc> (::GetProcAddress(ovrApi, "VR_ShutdownInternal"));
g_vrFunctions.getGenericInterface = reinterpret_cast<VR_GetGenericInterfaceProc>(::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<vr::IVRCompositor*>(
vrGetGenericInterface(vr::IVRCompositor_Version, &error));
vr::IVRCompositor* compositor = reinterpret_cast<vr::IVRCompositor*>(
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<vr::IVRCompositor*>(
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");

View File

@ -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();
};