2018-06-26 15:51:15 +02:00
|
|
|
#include "dxvk_instance.h"
|
2018-05-18 16:46:34 +02:00
|
|
|
#include "dxvk_openvr.h"
|
|
|
|
|
2018-06-05 01:03:59 +02:00
|
|
|
#ifdef __GNUC__
|
|
|
|
#pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
|
|
|
|
#endif
|
|
|
|
|
2018-05-18 16:46:34 +02:00
|
|
|
#include <openvr/openvr.hpp>
|
|
|
|
|
2018-06-30 18:49:29 +02:00
|
|
|
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*);
|
|
|
|
|
2018-05-18 16:46:34 +02:00
|
|
|
namespace dxvk {
|
|
|
|
|
2018-06-30 18:49:29 +02:00
|
|
|
struct VrFunctions {
|
|
|
|
VR_InitInternalProc initInternal = nullptr;
|
|
|
|
VR_ShutdownInternalProc shutdownInternal = nullptr;
|
|
|
|
VR_GetGenericInterfaceProc getGenericInterface = nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
VrFunctions g_vrFunctions;
|
|
|
|
VrInstance g_vrInstance;
|
2018-06-26 15:51:15 +02:00
|
|
|
|
|
|
|
VrInstance:: VrInstance() { }
|
|
|
|
VrInstance::~VrInstance() { }
|
2018-05-18 16:46:34 +02:00
|
|
|
|
|
|
|
|
2018-06-26 15:51:15 +02:00
|
|
|
vk::NameSet VrInstance::getInstanceExtensions() {
|
|
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
|
|
return m_insExtensions;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
vk::NameSet VrInstance::getDeviceExtensions(uint32_t adapterId) {
|
|
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
|
|
|
|
|
|
if (adapterId < m_devExtensions.size())
|
|
|
|
return m_devExtensions[adapterId];
|
2018-05-18 16:46:34 +02:00
|
|
|
|
2018-06-26 15:51:15 +02:00
|
|
|
return vk::NameSet();
|
2018-05-18 16:46:34 +02:00
|
|
|
}
|
2018-06-26 15:51:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
void VrInstance::initInstanceExtensions() {
|
|
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
|
|
|
2018-06-30 16:51:20 +02:00
|
|
|
if (m_compositor == nullptr)
|
|
|
|
m_compositor = this->getCompositor();
|
2018-06-26 15:51:15 +02:00
|
|
|
|
2018-06-30 16:51:20 +02:00
|
|
|
if (m_compositor == nullptr || m_initializedInsExt)
|
2018-06-26 15:51:15 +02:00
|
|
|
return;
|
|
|
|
|
2018-06-30 16:51:20 +02:00
|
|
|
m_insExtensions = this->queryInstanceExtensions();
|
2018-06-26 15:51:15 +02:00
|
|
|
m_initializedInsExt = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void VrInstance::initDeviceExtensions(const DxvkInstance* instance) {
|
|
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
|
|
|
2018-06-30 16:51:20 +02:00
|
|
|
if (m_compositor == nullptr || m_initializedDevExt)
|
2018-06-26 15:51:15 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
for (uint32_t i = 0; instance->enumAdapters(i) != nullptr; i++) {
|
|
|
|
m_devExtensions.push_back(this->queryDeviceExtensions(
|
2018-06-30 16:51:20 +02:00
|
|
|
instance->enumAdapters(i)->handle()));
|
2018-06-26 15:51:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
m_initializedDevExt = true;
|
2018-07-11 15:31:35 +02:00
|
|
|
this->shutdown();
|
2018-06-26 15:51:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-30 16:51:20 +02:00
|
|
|
vk::NameSet VrInstance::queryInstanceExtensions() const {
|
|
|
|
uint32_t len = m_compositor->GetVulkanInstanceExtensionsRequired(nullptr, 0);
|
2018-06-26 15:51:15 +02:00
|
|
|
std::vector<char> extensionList(len);
|
2018-06-30 16:51:20 +02:00
|
|
|
len = m_compositor->GetVulkanInstanceExtensionsRequired(extensionList.data(), len);
|
2018-06-26 15:51:15 +02:00
|
|
|
return parseExtensionList(std::string(extensionList.data(), len));
|
2018-05-18 16:46:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-30 16:51:20 +02:00
|
|
|
vk::NameSet VrInstance::queryDeviceExtensions(VkPhysicalDevice adapter) const {
|
|
|
|
uint32_t len = m_compositor->GetVulkanDeviceExtensionsRequired(adapter, nullptr, 0);
|
2018-06-26 15:51:15 +02:00
|
|
|
std::vector<char> extensionList(len);
|
2018-06-30 16:51:20 +02:00
|
|
|
len = m_compositor->GetVulkanDeviceExtensionsRequired(adapter, extensionList.data(), len);
|
2018-06-26 15:51:15 +02:00
|
|
|
return parseExtensionList(std::string(extensionList.data(), len));
|
2018-05-18 16:46:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-30 18:49:29 +02:00
|
|
|
vk::NameSet VrInstance::parseExtensionList(const std::string& str) const {
|
2018-05-18 16:46:34 +02:00
|
|
|
vk::NameSet result;
|
|
|
|
|
|
|
|
std::stringstream strstream(str);
|
|
|
|
std::string section;
|
|
|
|
|
|
|
|
while (std::getline(strstream, section, ' '))
|
|
|
|
result.add(section);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
vr::IVRCompositor* VrInstance::getCompositor() {
|
2018-07-11 15:31:35 +02:00
|
|
|
// Skip OpenVR initialization if requested
|
|
|
|
if (env::getEnvVar(L"DXVK_NO_VR") == "1")
|
|
|
|
return nullptr;
|
2018-05-18 16:46:34 +02:00
|
|
|
|
2018-07-11 15:31:35 +02:00
|
|
|
// 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 = ::GetModuleHandle("openvr_api.dll");
|
|
|
|
|
|
|
|
if (m_ovrApi == nullptr) {
|
|
|
|
m_ovrApi = ::LoadLibrary("openvr_api_dxvk.dll");
|
|
|
|
m_loadedOvrApi = m_ovrApi != nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_ovrApi == nullptr) {
|
2018-05-18 16:46:34 +02:00
|
|
|
Logger::warn("OpenVR: Failed to locate module");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load method used to retrieve the IVRCompositor interface
|
2018-07-11 15:31:35 +02:00
|
|
|
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"));
|
2018-05-18 16:46:34 +02:00
|
|
|
|
2018-06-30 18:49:29 +02:00
|
|
|
if (g_vrFunctions.getGenericInterface == nullptr) {
|
2018-05-18 16:46:34 +02:00
|
|
|
Logger::warn("OpenVR: VR_GetGenericInterface not found");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve the compositor interface
|
|
|
|
vr::EVRInitError error = vr::VRInitError_None;
|
|
|
|
|
2018-06-30 18:49:29 +02:00
|
|
|
vr::IVRCompositor* compositor = reinterpret_cast<vr::IVRCompositor*>(
|
|
|
|
g_vrFunctions.getGenericInterface(vr::IVRCompositor_Version, &error));
|
2018-05-18 16:46:34 +02:00
|
|
|
|
2018-06-30 18:49:29 +02:00
|
|
|
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");
|
2018-07-11 15:31:35 +02:00
|
|
|
this->shutdown();
|
2018-06-30 18:49:29 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
2018-05-18 16:46:34 +02:00
|
|
|
}
|
|
|
|
|
2018-06-30 16:51:20 +02:00
|
|
|
Logger::info("OpenVR: Compositor interface found");
|
2018-05-18 16:46:34 +02:00
|
|
|
return compositor;
|
|
|
|
}
|
2018-07-11 15:31:35 +02:00
|
|
|
|
|
|
|
|
|
|
|
void VrInstance::shutdown() {
|
|
|
|
if (m_initializedOpenVr)
|
|
|
|
g_vrFunctions.shutdownInternal();
|
|
|
|
|
|
|
|
if (m_loadedOvrApi)
|
|
|
|
::FreeLibrary(m_ovrApi);
|
|
|
|
|
|
|
|
m_initializedOpenVr = false;
|
|
|
|
m_loadedOvrApi = false;
|
|
|
|
}
|
2018-05-18 16:46:34 +02:00
|
|
|
|
|
|
|
}
|