hmd_openvr.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright 2011-2016 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  4. */
  5. #include "hmd_openvr.h"
  6. #include <openvr/openvr_capi.h>
  7. namespace bgfx
  8. {
  9. #if BX_PLATFORM_WINDOWS
  10. # define VR_CALLTYPE __cdecl
  11. #else
  12. # define VR_CALLTYPE
  13. #endif
  14. typedef uint32_t (VR_CALLTYPE *PFN_VR_INITINTERNAL)(EVRInitError* peError, EVRApplicationType eType);
  15. typedef void (VR_CALLTYPE *PFN_VR_SHUTDOWNINTERNAL)();
  16. typedef bool (VR_CALLTYPE *PFN_VR_ISHMDPRESENT)();
  17. typedef void* (VR_CALLTYPE *PFN_VR_GETGENERICINTERFACE)(const char* pchInterfaceVersion, EVRInitError* peError);
  18. typedef bool (VR_CALLTYPE *PFN_VR_ISRUNTIMEINSTALLED)();
  19. typedef bool (VR_CALLTYPE *PFN_VR_ISINTERFACEVERSIONVALID)(const char *pchInterfaceVersion);
  20. typedef uint32_t (VR_CALLTYPE *PFN_VR_GETINITTOKEN)();
  21. typedef const char* (VR_CALLTYPE *PFN_VR_GETVRINITERRORASSYMBOL)(EVRInitError error);
  22. typedef const char* (VR_CALLTYPE *PFN_VR_GETVRINITERRORASENGLISHDESCRIPTION)(EVRInitError error);
  23. PFN_VR_INITINTERNAL VR_InitInternal;
  24. PFN_VR_SHUTDOWNINTERNAL VR_ShutdownInternal;
  25. PFN_VR_ISHMDPRESENT VR_IsHmdPresent;
  26. PFN_VR_GETGENERICINTERFACE VR_GetGenericInterface;
  27. PFN_VR_ISRUNTIMEINSTALLED VR_IsRuntimeInstalled;
  28. PFN_VR_ISINTERFACEVERSIONVALID VR_IsInterfaceVersionValid;
  29. PFN_VR_GETINITTOKEN VR_GetInitToken;
  30. PFN_VR_GETVRINITERRORASSYMBOL VR_GetVRInitErrorAsSymbol;
  31. PFN_VR_GETVRINITERRORASENGLISHDESCRIPTION VR_GetVRInitErrorAsEnglishDescription;
  32. void* loadOpenVR()
  33. {
  34. void* openvrdll = bx::dlopen(
  35. #if BX_PLATFORM_LINUX
  36. "libopenvr_api.so"
  37. #elif BX_PLATFORM_OSX
  38. "libopenvr_api.dylib"
  39. #else
  40. "openvr_api.dll"
  41. #endif // BX_PLATFORM_*
  42. );
  43. if (NULL != openvrdll)
  44. {
  45. VR_InitInternal = (PFN_VR_INITINTERNAL )bx::dlsym(openvrdll, "VR_InitInternal");
  46. VR_ShutdownInternal = (PFN_VR_SHUTDOWNINTERNAL )bx::dlsym(openvrdll, "VR_ShutdownInternal");
  47. VR_IsHmdPresent = (PFN_VR_ISHMDPRESENT )bx::dlsym(openvrdll, "VR_IsHmdPresent");
  48. VR_GetGenericInterface = (PFN_VR_GETGENERICINTERFACE )bx::dlsym(openvrdll, "VR_GetGenericInterface");
  49. VR_IsRuntimeInstalled = (PFN_VR_ISRUNTIMEINSTALLED )bx::dlsym(openvrdll, "VR_IsRuntimeInstalled");
  50. VR_IsInterfaceVersionValid = (PFN_VR_ISINTERFACEVERSIONVALID)bx::dlsym(openvrdll, "VR_IsInterfaceVersionValid");
  51. VR_GetInitToken = (PFN_VR_GETINITTOKEN )bx::dlsym(openvrdll, "VR_GetInitToken");
  52. VR_GetVRInitErrorAsSymbol = (PFN_VR_GETVRINITERRORASSYMBOL )bx::dlsym(openvrdll, "VR_GetVRInitErrorAsSymbol");
  53. VR_GetVRInitErrorAsEnglishDescription = (PFN_VR_GETVRINITERRORASENGLISHDESCRIPTION)bx::dlsym(openvrdll, "VR_GetVRInitErrorAsEnglishDescription");
  54. if (NULL == VR_InitInternal
  55. && NULL == VR_ShutdownInternal
  56. && NULL == VR_IsHmdPresent
  57. && NULL == VR_GetGenericInterface
  58. && NULL == VR_IsRuntimeInstalled
  59. && NULL == VR_IsInterfaceVersionValid
  60. && NULL == VR_GetInitToken
  61. && NULL == VR_GetVRInitErrorAsSymbol
  62. && NULL == VR_GetVRInitErrorAsEnglishDescription)
  63. {
  64. bx::dlclose(openvrdll);
  65. return NULL;
  66. }
  67. EVRInitError err;
  68. uint32_t token = VR_InitInternal(&err, EVRApplicationType_VRApplication_Scene);
  69. BX_UNUSED(token);
  70. BX_TRACE("OpenVR: HMD is %spresent, Runtime is %sinstalled."
  71. , VR_IsHmdPresent() ? "" : "not "
  72. , VR_IsRuntimeInstalled() ? "" : "not "
  73. );
  74. }
  75. return openvrdll;
  76. }
  77. void unloadOpenVR(void* _openvrdll)
  78. {
  79. VR_ShutdownInternal();
  80. bx::dlclose(_openvrdll);
  81. }
  82. } // namespace bgfx