OVR_CAPIShim.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424
  1. /************************************************************************************
  2. Filename : OVR_CAPIShim.c
  3. Content : CAPI DLL user library
  4. Created : November 20, 2014
  5. Copyright : Copyright 2014 Oculus VR, LLC All Rights reserved.
  6. Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License");
  7. you may not use the Oculus VR Rift SDK except in compliance with the License,
  8. which is provided at the time of installation or download, or which
  9. otherwise accompanies this software in either electronic or hard copy form.
  10. You may obtain a copy of the License at
  11. http://www.oculusvr.com/licenses/LICENSE-3.2
  12. Unless required by applicable law or agreed to in writing, the Oculus VR SDK
  13. distributed under the License is distributed on an "AS IS" BASIS,
  14. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. See the License for the specific language governing permissions and
  16. limitations under the License.
  17. ************************************************************************************/
  18. #include "OVR_CAPI.h"
  19. #include "OVR_Version.h"
  20. #include "OVR_ErrorCode.h"
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <stddef.h>
  24. #include <stdint.h>
  25. #include <string.h>
  26. #include <ctype.h>
  27. #include <assert.h>
  28. #if defined(_WIN32)
  29. #if defined(_MSC_VER)
  30. #pragma warning(push, 0)
  31. #endif
  32. #include <Windows.h>
  33. #if defined(_MSC_VER)
  34. #pragma warning(pop)
  35. #endif
  36. #include "../Include/OVR_CAPI_D3D.h"
  37. #else
  38. #if defined(__APPLE__)
  39. #include <mach-o/dyld.h>
  40. #include <sys/syslimits.h>
  41. #include <libgen.h>
  42. #include <pwd.h>
  43. #include <unistd.h>
  44. #endif
  45. #include <dlfcn.h>
  46. #include <sys/stat.h>
  47. #include <unistd.h>
  48. #endif
  49. #include "../Include/OVR_CAPI_GL.h"
  50. #if defined(_MSC_VER)
  51. #pragma warning(push)
  52. #pragma warning(disable: 4996) // 'getenv': This function or variable may be unsafe.
  53. #endif
  54. // SHA-256 of string 'OculusSDK0.8'
  55. static const uint8_t VersionSHA256[] = {
  56. 0x47, 0x38, 0x2c, 0xfe, 0x10, 0xc6, 0x96, 0x98, 0xdc, 0x90, 0x32, 0xe0, 0x55, 0x2a, 0xe6, 0x0f,
  57. 0x33, 0x2e, 0x27, 0xb0, 0xa8, 0x07, 0xfe, 0xff, 0xb6, 0x7e, 0x79, 0x70, 0xa6, 0xb2, 0x89, 0xcc};
  58. static const uint8_t VersionXOR = 0xc9;
  59. // -----------------------------------------------------------------------------------
  60. // ***** OVR_ENABLE_DEVELOPER_SEARCH
  61. //
  62. // If defined then our shared library loading code searches for developer build
  63. // directories.
  64. //
  65. #if !defined(OVR_ENABLE_DEVELOPER_SEARCH)
  66. #endif
  67. // -----------------------------------------------------------------------------------
  68. // ***** OVR_BUILD_DEBUG
  69. //
  70. // Defines OVR_BUILD_DEBUG when the compiler default debug preprocessor is set.
  71. //
  72. // If you want to control the behavior of these flags, then explicitly define
  73. // either -DOVR_BUILD_RELEASE or -DOVR_BUILD_DEBUG in the compiler arguments.
  74. #if !defined(OVR_BUILD_DEBUG) && !defined(OVR_BUILD_RELEASE)
  75. #if defined(_MSC_VER)
  76. #if defined(_DEBUG)
  77. #define OVR_BUILD_DEBUG
  78. #endif
  79. #else
  80. #if defined(DEBUG)
  81. #define OVR_BUILD_DEBUG
  82. #endif
  83. #endif
  84. #endif
  85. //-----------------------------------------------------------------------------------
  86. // ***** FilePathCharType, ModuleHandleType, ModuleFunctionType
  87. //
  88. #if defined(_WIN32) // We need to use wchar_t on Microsoft platforms, as that's the native file system character type.
  89. #define FilePathCharType wchar_t // #define instead of typedef because debuggers (VC++, XCode) don't recognize typedef'd types as a string type.
  90. typedef HMODULE ModuleHandleType;
  91. typedef FARPROC ModuleFunctionType;
  92. #else
  93. #define FilePathCharType char
  94. typedef void* ModuleHandleType;
  95. typedef void* ModuleFunctionType;
  96. #endif
  97. #define ModuleHandleTypeNull ((ModuleHandleType)NULL)
  98. #define ModuleFunctionTypeNull ((ModuleFunctionType)NULL)
  99. //-----------------------------------------------------------------------------------
  100. // ***** OVR_MAX_PATH
  101. //
  102. #if !defined(OVR_MAX_PATH)
  103. #if defined(_WIN32)
  104. #define OVR_MAX_PATH _MAX_PATH
  105. #elif defined(__APPLE__)
  106. #define OVR_MAX_PATH PATH_MAX
  107. #else
  108. #define OVR_MAX_PATH 1024
  109. #endif
  110. #endif
  111. //-----------------------------------------------------------------------------------
  112. // ***** OVR_DECLARE_IMPORT
  113. //
  114. // Creates typedef and pointer declaration for a function of a given signature.
  115. // The typedef is <FunctionName>Type, and the pointer is <FunctionName>Ptr.
  116. //
  117. // Example usage:
  118. // int MultiplyValues(float x, float y); // Assume this function exists in an external shared library. We don't actually need to redeclare it.
  119. // OVR_DECLARE_IMPORT(int, MultiplyValues, (float x, float y)) // This creates a local typedef and pointer for it.
  120. #define OVR_DECLARE_IMPORT(ReturnValue, FunctionName, Arguments) \
  121. typedef ReturnValue (OVR_CDECL *FunctionName##Type)Arguments; \
  122. FunctionName##Type FunctionName##Ptr = NULL;
  123. //-----------------------------------------------------------------------------------
  124. // ***** OVR_GETFUNCTION
  125. //
  126. // Loads <FunctionName>Ptr from hLibOVR if not already loaded.
  127. // Assumes a variable named <FunctionName>Ptr of type <FunctionName>Type exists which is called <FunctionName> in LibOVR.
  128. //
  129. // Example usage:
  130. // OVR_GETFUNCTION(MultiplyValues) // Normally this would be done on library init and not before every usage.
  131. // int result = MultiplyValuesPtr(3.f, 4.f);
  132. #if !defined(OVR_DLSYM)
  133. #if defined(_WIN32)
  134. #define OVR_DLSYM(dlImage, name) GetProcAddress(dlImage, name)
  135. #else
  136. #define OVR_DLSYM(dlImage, name) dlsym(dlImage, name)
  137. #endif
  138. #endif
  139. #define OVR_GETFUNCTION(f) \
  140. if(!f##Ptr) \
  141. { \
  142. union \
  143. { \
  144. f##Type p1; \
  145. ModuleFunctionType p2; \
  146. } u; \
  147. u.p2 = OVR_DLSYM(hLibOVR, #f); \
  148. f##Ptr = u.p1; \
  149. }
  150. static size_t OVR_strlcpy(char* dest, const char* src, size_t destsize)
  151. {
  152. const char* s = src;
  153. size_t n = destsize;
  154. if(n && --n)
  155. {
  156. do{
  157. if((*dest++ = *s++) == 0)
  158. break;
  159. } while(--n);
  160. }
  161. if(!n)
  162. {
  163. if(destsize)
  164. *dest = 0;
  165. while(*s++)
  166. { }
  167. }
  168. return (size_t)((s - src) - 1);
  169. }
  170. static size_t OVR_strlcat(char* dest, const char* src, size_t destsize)
  171. {
  172. const size_t d = destsize ? strlen(dest) : 0;
  173. const size_t s = strlen(src);
  174. const size_t t = s + d;
  175. if(t < destsize)
  176. memcpy(dest + d, src, (s + 1) * sizeof(*src));
  177. else
  178. {
  179. if(destsize)
  180. {
  181. memcpy(dest + d, src, ((destsize - d) - 1) * sizeof(*src));
  182. dest[destsize - 1] = 0;
  183. }
  184. }
  185. return t;
  186. }
  187. #if defined(__APPLE__)
  188. static ovrBool OVR_strend(const char* pStr, const char* pFind, size_t strLength, size_t findLength)
  189. {
  190. if(strLength == (size_t)-1)
  191. strLength = strlen(pStr);
  192. if(findLength == (size_t)-1)
  193. findLength = strlen(pFind);
  194. if(strLength >= findLength)
  195. return (strcmp(pStr + strLength - findLength, pFind) == 0);
  196. return ovrFalse;
  197. }
  198. static ovrBool OVR_isBundleFolder(const char* filePath)
  199. {
  200. static const char* extensionArray[] = { ".app", ".bundle", ".framework", ".plugin", ".kext" };
  201. size_t i;
  202. for(i = 0; i < sizeof(extensionArray)/sizeof(extensionArray[0]); i++)
  203. {
  204. if(OVR_strend(filePath, extensionArray[i], (size_t)-1, (size_t)-1))
  205. return ovrTrue;
  206. }
  207. return ovrFalse;
  208. }
  209. #endif
  210. #if defined(OVR_ENABLE_DEVELOPER_SEARCH)
  211. // Returns true if the path begins with the given prefix.
  212. // Doesn't support non-ASCII paths, else the return value may be incorrect.
  213. static int OVR_PathStartsWith(const FilePathCharType* path, const char* prefix)
  214. {
  215. while(*prefix)
  216. {
  217. if(tolower((unsigned char)*path++) != tolower((unsigned char)*prefix++))
  218. return ovrFalse;
  219. }
  220. return ovrTrue;
  221. }
  222. #endif
  223. static ovrBool OVR_GetCurrentWorkingDirectory(FilePathCharType* directoryPath, size_t directoryPathCapacity)
  224. {
  225. #if defined(_WIN32)
  226. DWORD dwSize = GetCurrentDirectoryW((DWORD)directoryPathCapacity, directoryPath);
  227. if((dwSize > 0) && (directoryPathCapacity > 1)) // Test > 1 so we have room to possibly append a \ char.
  228. {
  229. size_t length = wcslen(directoryPath);
  230. if((length == 0) || ((directoryPath[length - 1] != L'\\') && (directoryPath[length - 1] != L'/')))
  231. {
  232. directoryPath[length++] = L'\\';
  233. directoryPath[length] = L'\0';
  234. }
  235. return ovrTrue;
  236. }
  237. #else
  238. char* cwd = getcwd(directoryPath, directoryPathCapacity);
  239. if(cwd && directoryPath[0] && (directoryPathCapacity > 1)) // Test > 1 so we have room to possibly append a / char.
  240. {
  241. size_t length = strlen(directoryPath);
  242. if((length == 0) || (directoryPath[length - 1] != '/'))
  243. {
  244. directoryPath[length++] = '/';
  245. directoryPath[length] = '\0';
  246. }
  247. return ovrTrue;
  248. }
  249. #endif
  250. if(directoryPathCapacity > 0)
  251. directoryPath[0] = '\0';
  252. return ovrFalse;
  253. }
  254. // The appContainer argument is specific currently to only Macintosh. If true and the application is a .app bundle then it returns the
  255. // location of the bundle and not the path to the executable within the bundle. Else return the path to the executable binary itself.
  256. // The moduleHandle refers to the relevant dynamic (a.k.a. shared) library. The main executable is the main module, and each of the shared
  257. // libraries is a module. This way you can specify that you want to know the directory of the given shared library, which may be different
  258. // from the main executable. If the moduleHandle is NULL then the current application module is used.
  259. static ovrBool OVR_GetCurrentApplicationDirectory(FilePathCharType* directoryPath, size_t directoryPathCapacity, ovrBool appContainer, ModuleHandleType moduleHandle)
  260. {
  261. #if defined(_WIN32)
  262. DWORD length = GetModuleFileNameW(moduleHandle, directoryPath, (DWORD)directoryPathCapacity);
  263. DWORD pos;
  264. if((length != 0) && (length < (DWORD)directoryPathCapacity)) // If there wasn't an error and there was enough capacity...
  265. {
  266. for(pos = length; (pos > 0) && (directoryPath[pos] != '\\') && (directoryPath[pos] != '/'); --pos)
  267. {
  268. if((directoryPath[pos - 1] != '\\') && (directoryPath[pos - 1] != '/'))
  269. directoryPath[pos - 1] = 0;
  270. }
  271. return ovrTrue;
  272. }
  273. (void)appContainer; // Not used on this platform.
  274. #elif defined(__APPLE__)
  275. uint32_t directoryPathCapacity32 = (uint32_t)directoryPathCapacity;
  276. int result = _NSGetExecutablePath(directoryPath, &directoryPathCapacity32);
  277. if(result == 0) // If success...
  278. {
  279. char realPath[OVR_MAX_PATH];
  280. if(realpath(directoryPath, realPath)) // realpath returns the canonicalized absolute file path.
  281. {
  282. size_t length = 0;
  283. if(appContainer) // If the caller wants the path to the containing bundle...
  284. {
  285. char containerPath[OVR_MAX_PATH];
  286. ovrBool pathIsContainer;
  287. OVR_strlcpy(containerPath, realPath, sizeof(containerPath));
  288. pathIsContainer = OVR_isBundleFolder(containerPath);
  289. while(!pathIsContainer && strncmp(containerPath, ".", OVR_MAX_PATH) && strncmp(containerPath, "/", OVR_MAX_PATH)) // While the container we're looking for is not found and while the path doesn't start with a . or /
  290. {
  291. OVR_strlcpy(containerPath, dirname(containerPath), sizeof(containerPath));
  292. pathIsContainer = OVR_isBundleFolder(containerPath);
  293. }
  294. if(pathIsContainer)
  295. length = OVR_strlcpy(directoryPath, containerPath, directoryPathCapacity);
  296. }
  297. if(length == 0) // If not set above in the appContainer block...
  298. length = OVR_strlcpy(directoryPath, realPath, directoryPathCapacity);
  299. while(length-- && (directoryPath[length] != '/'))
  300. directoryPath[length] = '\0'; // Strip the file name from the file path, leaving a trailing / char.
  301. return ovrTrue;
  302. }
  303. }
  304. (void)moduleHandle; // Not used on this platform.
  305. #else
  306. ssize_t length = readlink("/proc/self/exe", directoryPath, directoryPathCapacity);
  307. ssize_t pos;
  308. if(length > 0)
  309. {
  310. for(pos = length; (pos > 0) && (directoryPath[pos] != '/'); --pos)
  311. {
  312. if(directoryPath[pos - 1] != '/')
  313. directoryPath[pos - 1] = '\0';
  314. }
  315. return ovrTrue;
  316. }
  317. (void)appContainer; // Not used on this platform.
  318. (void)moduleHandle;
  319. #endif
  320. if(directoryPathCapacity > 0)
  321. directoryPath[0] = '\0';
  322. return ovrFalse;
  323. }
  324. #if defined(_WIN32) || defined(OVR_ENABLE_DEVELOPER_SEARCH) // Used only in these cases
  325. // Get the file path to the current module's (DLL or EXE) directory within the current process.
  326. // Will be different from the process module handle if the current module is a DLL and is in a different directory than the EXE module.
  327. // If successful then directoryPath will be valid and ovrTrue is returned, else directoryPath will be empty and ovrFalse is returned.
  328. static ovrBool OVR_GetCurrentModuleDirectory(FilePathCharType* directoryPath, size_t directoryPathCapacity, ovrBool appContainer)
  329. {
  330. #if defined(_WIN32)
  331. HMODULE hModule;
  332. BOOL result = GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, (LPCWSTR)(uintptr_t)OVR_GetCurrentModuleDirectory, &hModule);
  333. if(result)
  334. OVR_GetCurrentApplicationDirectory(directoryPath, directoryPathCapacity, ovrTrue, hModule);
  335. else
  336. directoryPath[0] = 0;
  337. (void)appContainer;
  338. return directoryPath[0] ? ovrTrue : ovrFalse;
  339. #else
  340. return OVR_GetCurrentApplicationDirectory(directoryPath, directoryPathCapacity, appContainer, NULL);
  341. #endif
  342. }
  343. #endif
  344. static ModuleHandleType OVR_OpenLibrary(const FilePathCharType* libraryPath)
  345. {
  346. #if defined(_WIN32)
  347. return LoadLibraryW(libraryPath);
  348. #else
  349. // Don't bother trying to dlopen() a file that is not even there.
  350. if (access(libraryPath, X_OK | R_OK ) != 0)
  351. {
  352. return NULL;
  353. }
  354. dlerror(); // Clear any previous dlopen() errors
  355. // Use RTLD_NOW because we don't want unexpected stalls at runtime, and the library isn't very large.
  356. // Use RTLD_LOCAL to avoid unilaterally exporting resolved symbols to the rest of this process.
  357. void *lib = dlopen(libraryPath, RTLD_NOW | RTLD_LOCAL);
  358. if (!lib)
  359. {
  360. #if defined(__APPLE__)
  361. // TODO: Output the error in whatever logging system OSX uses (jhughes)
  362. #else // __APPLE__
  363. fprintf(stderr, "ERROR: Can't load '%s':\n%s\n", libraryPath, dlerror());
  364. #endif // __APPLE__
  365. }
  366. return lib;
  367. #endif
  368. }
  369. static void OVR_CloseLibrary(ModuleHandleType hLibrary)
  370. {
  371. if (hLibrary)
  372. {
  373. #if defined(_WIN32)
  374. // We may need to consider what to do in the case that the library is in an exception state.
  375. // In a Windows C++ DLL, all global objects (including static members of classes) will be constructed just
  376. // before the calling of the DllMain with DLL_PROCESS_ATTACH and they will be destroyed just after
  377. // the call of the DllMain with DLL_PROCESS_DETACH. We may need to intercept DLL_PROCESS_DETACH and
  378. // have special handling for the case that the DLL is broken.
  379. FreeLibrary(hLibrary);
  380. #else
  381. dlclose(hLibrary);
  382. #endif
  383. }
  384. }
  385. // Returns a valid ModuleHandleType (e.g. Windows HMODULE) or returns ModuleHandleTypeNull (e.g. NULL).
  386. // The caller is required to eventually call OVR_CloseLibrary on a valid return handle.
  387. //
  388. static ModuleHandleType OVR_FindLibraryPath(int requestedProductVersion, int requestedMajorVersion,
  389. FilePathCharType* libraryPath, size_t libraryPathCapacity)
  390. {
  391. ModuleHandleType moduleHandle;
  392. int printfResult;
  393. FilePathCharType developerDir[OVR_MAX_PATH] = { '\0' };
  394. #if defined(_MSC_VER)
  395. #if defined(_WIN64)
  396. const char* pBitDepth = "64";
  397. #else
  398. const char* pBitDepth = "32";
  399. #endif
  400. #elif defined(__APPLE__)
  401. // For Apple platforms we are using a Universal Binary LibOVRRT dylib which has both 32 and 64 in it.
  402. #else // Other Unix.
  403. #if defined(__x86_64__)
  404. const char* pBitDepth = "64";
  405. #else
  406. const char* pBitDepth = "32";
  407. #endif
  408. #endif
  409. moduleHandle = ModuleHandleTypeNull;
  410. if(libraryPathCapacity)
  411. libraryPath[0] = '\0';
  412. // Note: OVR_ENABLE_DEVELOPER_SEARCH is deprecated in favor of the simpler LIBOVR_DLL_DIR, as the edge
  413. // case uses of the former created some complications that may be best solved by simply using a LIBOVR_DLL_DIR
  414. // environment variable which the user can set in their debugger or system environment variables.
  415. #if (defined(_MSC_VER) || defined(_WIN32)) && !defined(OVR_FILE_PATH_SEPARATOR)
  416. #define OVR_FILE_PATH_SEPARATOR "\\"
  417. #else
  418. #define OVR_FILE_PATH_SEPARATOR "/"
  419. #endif
  420. {
  421. const char* pLibOvrDllDir = getenv("LIBOVR_DLL_DIR"); // Example value: /dev/OculusSDK/Main/LibOVR/Mac/Debug/
  422. if(pLibOvrDllDir)
  423. {
  424. char developerDir8[OVR_MAX_PATH];
  425. size_t length = OVR_strlcpy(developerDir8, pLibOvrDllDir, sizeof(developerDir8)); // If missing a trailing path separator then append one.
  426. if((length > 0) && (length < sizeof(developerDir8)) && (developerDir8[length - 1] != OVR_FILE_PATH_SEPARATOR[0]))
  427. {
  428. length = OVR_strlcat(developerDir8, OVR_FILE_PATH_SEPARATOR, sizeof(developerDir8));
  429. if(length < sizeof(developerDir8))
  430. {
  431. #if defined(_WIN32)
  432. size_t i;
  433. for(i = 0; i <= length; ++i) // ASCII conversion of 8 to 16 bit text.
  434. developerDir[i] = (FilePathCharType)(uint8_t)developerDir8[i];
  435. #else
  436. OVR_strlcpy(developerDir, developerDir8, sizeof(developerDir));
  437. #endif
  438. }
  439. }
  440. }
  441. }
  442. // Support checking for a developer library location override via the OVR_SDK_ROOT environment variable.
  443. // This pathway is deprecated in favor of using LIBOVR_DLL_DIR instead.
  444. #if defined(OVR_ENABLE_DEVELOPER_SEARCH)
  445. if (!developerDir[0]) // If not already set by LIBOVR_DLL_PATH...
  446. {
  447. // __FILE__ maps to <sdkRoot>/LibOVR/Src/OVR_CAPIShim.c
  448. char sdkRoot[OVR_MAX_PATH];
  449. char* pLibOVR;
  450. size_t i;
  451. // We assume that __FILE__ returns a full path, which isn't the case for some compilers.
  452. // Need to compile with /FC under VC++ for __FILE__ to expand to the full file path.
  453. // clang expands __FILE__ to a full path by default.
  454. OVR_strlcpy(sdkRoot, __FILE__, sizeof(sdkRoot));
  455. for(i = 0; sdkRoot[i]; ++i)
  456. sdkRoot[i] = (char)tolower(sdkRoot[i]); // Microsoft doesn't maintain case.
  457. pLibOVR = strstr(sdkRoot, "libovr");
  458. if(pLibOVR && (pLibOVR > sdkRoot))
  459. pLibOVR[-1] = '\0';
  460. else
  461. sdkRoot[0] = '\0';
  462. if(sdkRoot[0])
  463. {
  464. // We want to use a developer version of the library only if the application is also being executed from
  465. // a developer location. Ideally we would do this by checking that the relative path from the executable to
  466. // the shared library is the same at runtime as it was when the executable was first built, but we don't have
  467. // an easy way to do that from here and it would require some runtime help from the application code.
  468. // Instead we verify that the application is simply in the same developer tree that was was when built.
  469. // We could put in some additional logic to make it very likely to know if the EXE is in its original location.
  470. FilePathCharType modulePath[OVR_MAX_PATH];
  471. const ovrBool pathMatch = OVR_GetCurrentModuleDirectory(modulePath, OVR_MAX_PATH, ovrTrue) &&
  472. (OVR_PathStartsWith(modulePath, sdkRoot) == ovrTrue);
  473. if(pathMatch == ovrFalse)
  474. {
  475. sdkRoot[0] = '\0'; // The application module is not in the developer tree, so don't try to use the developer shared library.
  476. }
  477. }
  478. if(sdkRoot[0])
  479. {
  480. #if defined(OVR_BUILD_DEBUG)
  481. const char* pConfigDirName = "Debug";
  482. #else
  483. const char* pConfigDirName = "Release";
  484. #endif
  485. #if defined(_MSC_VER)
  486. #if defined(_WIN64)
  487. const char* pArchDirName = "x64";
  488. #else
  489. const char* pArchDirName = "Win32";
  490. #endif
  491. #else
  492. #if defined(__x86_64__)
  493. const char* pArchDirName = "x86_64";
  494. #else
  495. const char* pArchDirName = "i386";
  496. #endif
  497. #endif
  498. #if defined(_MSC_VER) && (_MSC_VER == 1600)
  499. const char* pCompilerVersion = "VS2010";
  500. #elif defined(_MSC_VER) && (_MSC_VER == 1700)
  501. const char* pCompilerVersion = "VS2012";
  502. #elif defined(_MSC_VER) && (_MSC_VER == 1800)
  503. const char* pCompilerVersion = "VS2013";
  504. #elif defined(_MSC_VER) && (_MSC_VER == 1900)
  505. const char* pCompilerVersion = "VS2014";
  506. #endif
  507. #if defined(_WIN32)
  508. int count = swprintf_s(developerDir, OVR_MAX_PATH, L"%hs\\LibOVR\\Lib\\Windows\\%hs\\%hs\\%hs\\",
  509. sdkRoot, pArchDirName, pConfigDirName, pCompilerVersion);
  510. #elif defined(__APPLE__)
  511. // Apple/XCode doesn't let you specify an arch in build paths, which is OK if we build a universal binary.
  512. (void)pArchDirName;
  513. int count = snprintf(developerDir, OVR_MAX_PATH, "%s/LibOVR/Lib/Mac/%s/",
  514. sdkRoot, pConfigDirName);
  515. #else
  516. int count = snprintf(developerDir, OVR_MAX_PATH, "%s/LibOVR/Lib/Linux/%s/%s/",
  517. sdkRoot, pArchDirName, pConfigDirName);
  518. #endif
  519. if((count < 0) || (count >= (int)OVR_MAX_PATH)) // If there was an error or capacity overflow... clear the string.
  520. {
  521. developerDir[0] = '\0';
  522. }
  523. }
  524. }
  525. #endif // OVR_ENABLE_DEVELOPER_SEARCH
  526. {
  527. FilePathCharType cwDir[OVR_MAX_PATH]; // Will be filled in below.
  528. FilePathCharType appDir[OVR_MAX_PATH];
  529. size_t i;
  530. #if defined(_WIN32)
  531. FilePathCharType moduleDir[OVR_MAX_PATH];
  532. const FilePathCharType* directoryArray[5];
  533. directoryArray[0] = cwDir;
  534. directoryArray[1] = moduleDir;
  535. directoryArray[2] = appDir;
  536. directoryArray[3] = developerDir; // Developer directory.
  537. directoryArray[4] = L""; // No directory, which causes Windows to use the standard search strategy to find the DLL.
  538. OVR_GetCurrentModuleDirectory(moduleDir, sizeof(moduleDir)/sizeof(moduleDir[0]), ovrTrue);
  539. #elif defined(__APPLE__)
  540. // https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/dyld.1.html
  541. FilePathCharType homeDir[OVR_MAX_PATH];
  542. FilePathCharType homeFrameworkDir[OVR_MAX_PATH];
  543. const FilePathCharType* directoryArray[5];
  544. size_t homeDirLength = 0;
  545. const char* pHome = getenv("HOME"); // Try getting the HOME environment variable.
  546. if (pHome)
  547. {
  548. homeDirLength = OVR_strlcpy(homeDir, pHome, sizeof(homeDir));
  549. }
  550. else
  551. {
  552. // https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man3/getpwuid_r.3.html
  553. const long pwBufferSize = sysconf(_SC_GETPW_R_SIZE_MAX);
  554. if (pwBufferSize != -1)
  555. {
  556. char pwBuffer[pwBufferSize];
  557. struct passwd pw;
  558. struct passwd* pwResult = NULL;
  559. if ((getpwuid_r(getuid(), &pw, pwBuffer, pwBufferSize, &pwResult) == 0) && pwResult)
  560. homeDirLength = OVR_strlcpy(homeDir, pw.pw_dir, sizeof(homeDir));
  561. }
  562. }
  563. if (homeDirLength)
  564. {
  565. if (homeDir[homeDirLength - 1] == '/')
  566. homeDir[homeDirLength - 1] = '\0';
  567. OVR_strlcpy(homeFrameworkDir, homeDir, sizeof(homeFrameworkDir));
  568. OVR_strlcat(homeFrameworkDir, "/Library/Frameworks/", sizeof(homeFrameworkDir));
  569. }
  570. else
  571. {
  572. homeFrameworkDir[0] = '\0';
  573. }
  574. directoryArray[0] = cwDir;
  575. directoryArray[1] = appDir;
  576. directoryArray[2] = homeFrameworkDir; // ~/Library/Frameworks/
  577. directoryArray[3] = "/Library/Frameworks/"; // DYLD_FALLBACK_FRAMEWORK_PATH
  578. directoryArray[4] = developerDir; // Developer directory.
  579. #else
  580. #define STR1(x) #x
  581. #define STR(x) STR1(x)
  582. #ifdef LIBDIR
  583. #define TEST_LIB_DIR STR(LIBDIR) "/"
  584. #else
  585. #define TEST_LIB_DIR appDir
  586. #endif
  587. const FilePathCharType* directoryArray[5];
  588. directoryArray[0] = cwDir;
  589. directoryArray[1] = TEST_LIB_DIR; // Directory specified by LIBDIR if defined.
  590. directoryArray[2] = developerDir; // Developer directory.
  591. directoryArray[3] = "/usr/local/lib/";
  592. directoryArray[4] = "/usr/lib/";
  593. #endif
  594. OVR_GetCurrentWorkingDirectory(cwDir, sizeof(cwDir) / sizeof(cwDir[0]));
  595. OVR_GetCurrentApplicationDirectory(appDir, sizeof(appDir) / sizeof(appDir[0]), ovrTrue, NULL);
  596. // Versioned file expectations.
  597. // Windows: LibOVRRT<BIT_DEPTH>_<PRODUCT_VERSION>_<MAJOR_VERSION>.dll // Example: LibOVRRT64_1_1.dll -- LibOVRRT 64 bit, product 1, major version 1, minor/patch/build numbers unspecified in the name.
  598. // Mac: LibOVRRT_<PRODUCT_VERSION>.framework/Versions/<MAJOR_VERSION>/LibOVRRT_<PRODUCT_VERSION> // We are not presently using the .framework bundle's Current directory to hold the version number. This may change.
  599. // Linux: libOVRRT<BIT_DEPTH>_<PRODUCT_VERSION>.so.<MAJOR_VERSION> // The file on disk may contain a minor version number, but a symlink is used to map this major-only version to it.
  600. // Since we are manually loading the LibOVR dynamic library, we need to look in various locations for a file
  601. // that matches our requirements. The functionality required is somewhat similar to the operating system's
  602. // dynamic loader functionality. Each OS has some differences in how this is handled.
  603. // Future versions of this may iterate over all libOVRRT.so.* files in the directory and use the one that matches our requirements.
  604. //
  605. // We need to look for a library that matches the product version and major version of the caller's request,
  606. // and that library needs to support a minor version that is >= the requested minor version. Currently we
  607. // don't test the minor version here, as the library is named based only on the product and major version.
  608. // Currently the minor version test is handled via the initialization of the library and the initialization
  609. // fails if minor version cannot be supported by the library. The reason this is done during initialization
  610. // is that the library can at runtime support multiple minor versions based on the user's request. To the
  611. // external user, all that matters it that they call ovr_Initialize with a requested version and it succeeds
  612. // or fails.
  613. //
  614. // The product version is something that is at a higher level than the major version, and is not something that's
  615. // always seen in libraries (an example is the well-known LibXml2 library, in which the 2 is essentially the product version).
  616. for(i = 0; i < sizeof(directoryArray)/sizeof(directoryArray[0]); ++i)
  617. {
  618. #if defined(_WIN32)
  619. printfResult = swprintf(libraryPath, libraryPathCapacity, L"%lsLibOVRRT%hs_%d_%d.dll", directoryArray[i], pBitDepth, requestedProductVersion, requestedMajorVersion);
  620. #elif defined(__APPLE__)
  621. // https://developer.apple.com/library/mac/documentation/MacOSX/Conceptual/BPFrameworks/Concepts/VersionInformation.html
  622. // Macintosh application bundles have the option of embedding dependent frameworks within the application
  623. // bundle itself. A problem with that is that it doesn't support vendor-supplied updates to the framework.
  624. printfResult = snprintf(libraryPath, libraryPathCapacity, "%sLibOVRRT_%d.framework/Versions/%d/LibOVRRT_%d", directoryArray[i], requestedProductVersion, requestedMajorVersion, requestedProductVersion);
  625. #else // Unix
  626. // Applications that depend on the OS (e.g. ld-linux / ldd) can rely on the library being in a common location
  627. // such as /usr/lib or can rely on the -rpath linker option to embed a path for the OS to check for the library,
  628. // or can rely on the LD_LIBRARY_PATH environment variable being set. It's generally not recommended that applications
  629. // depend on LD_LIBRARY_PATH be globally modified, partly due to potentialy security issues.
  630. // Currently we check the current application directory, current working directory, and then /usr/lib and possibly others.
  631. printfResult = snprintf(libraryPath, libraryPathCapacity, "%slibOVRRT%s_%d.so.%d", directoryArray[i], pBitDepth, requestedProductVersion, requestedMajorVersion);
  632. #endif
  633. if((printfResult >= 0) && (printfResult < (int)libraryPathCapacity))
  634. {
  635. moduleHandle = OVR_OpenLibrary(libraryPath);
  636. if(moduleHandle != ModuleHandleTypeNull)
  637. return moduleHandle;
  638. }
  639. }
  640. }
  641. return moduleHandle;
  642. }
  643. //-----------------------------------------------------------------------------------
  644. // ***** hLibOVR
  645. //
  646. // global handle to the LivOVR shared library.
  647. //
  648. static ModuleHandleType hLibOVR = NULL;
  649. // This function is currently unsupported.
  650. ModuleHandleType ovr_GetLibOVRRTHandle()
  651. {
  652. return hLibOVR;
  653. }
  654. //-----------------------------------------------------------------------------------
  655. // ***** Function declarations
  656. //
  657. // To consider: Move OVR_DECLARE_IMPORT and the declarations below to OVR_CAPI.h
  658. //
  659. OVR_DECLARE_IMPORT(ovrBool, ovr_InitializeRenderingShimVersion, (int requestedMinorVersion))
  660. OVR_DECLARE_IMPORT(ovrResult, ovr_Initialize, (const ovrInitParams* params))
  661. OVR_DECLARE_IMPORT(ovrBool, ovr_Shutdown, ())
  662. OVR_DECLARE_IMPORT(const char*, ovr_GetVersionString, ())
  663. OVR_DECLARE_IMPORT(void, ovr_GetLastErrorInfo, (ovrErrorInfo* errorInfo))
  664. OVR_DECLARE_IMPORT(ovrHmdDesc, ovr_GetHmdDesc, (ovrSession session))
  665. OVR_DECLARE_IMPORT(ovrResult, ovr_Create, (ovrSession* pSession, ovrGraphicsLuid* pLuid))
  666. OVR_DECLARE_IMPORT(void, ovr_Destroy, (ovrSession session))
  667. OVR_DECLARE_IMPORT(ovrResult, ovr_GetSessionStatus, (ovrSession session, ovrSessionStatus* sessionStatus))
  668. OVR_DECLARE_IMPORT(unsigned int, ovr_GetEnabledCaps, (ovrSession session))
  669. OVR_DECLARE_IMPORT(void, ovr_SetEnabledCaps, (ovrSession session, unsigned int hmdCaps))
  670. OVR_DECLARE_IMPORT(unsigned int, ovr_GetTrackingCaps, (ovrSession session))
  671. OVR_DECLARE_IMPORT(ovrResult, ovr_ConfigureTracking, (ovrSession session, unsigned int requestedTrackingCaps, unsigned int requiredTrackingCaps))
  672. OVR_DECLARE_IMPORT(void, ovr_RecenterPose, (ovrSession session))
  673. OVR_DECLARE_IMPORT(ovrTrackingState, ovr_GetTrackingState, (ovrSession session, double absTime, ovrBool latencyMarker))
  674. OVR_DECLARE_IMPORT(ovrResult, ovr_GetInputState, (ovrSession session, unsigned int controllerMask, ovrInputState*))
  675. OVR_DECLARE_IMPORT(ovrResult, ovr_SetControllerVibration, (ovrSession session, unsigned int controllerTypeMask, float frequency, float amplitude))
  676. OVR_DECLARE_IMPORT(ovrSizei, ovr_GetFovTextureSize, (ovrSession session, ovrEyeType eye, ovrFovPort fov, float pixelsPerDisplayPixel))
  677. OVR_DECLARE_IMPORT(ovrResult, ovr_SubmitFrame, (ovrSession session, long long frameIndex, const ovrViewScaleDesc* viewScaleDesc, ovrLayerHeader const * const * layerPtrList, unsigned int layerCount))
  678. OVR_DECLARE_IMPORT(ovrEyeRenderDesc, ovr_GetRenderDesc, (ovrSession session, ovrEyeType eyeType, ovrFovPort fov))
  679. OVR_DECLARE_IMPORT(double, ovr_GetPredictedDisplayTime, (ovrSession session, long long frameIndex))
  680. OVR_DECLARE_IMPORT(double, ovr_GetTimeInSeconds, ())
  681. OVR_DECLARE_IMPORT(void, ovr_ResetBackOfHeadTracking, (ovrSession session))
  682. OVR_DECLARE_IMPORT(void, ovr_ResetMulticameraTracking, (ovrSession session))
  683. OVR_DECLARE_IMPORT(ovrBool, ovr_GetBool, (ovrSession session, const char* propertyName, ovrBool defaultVal))
  684. OVR_DECLARE_IMPORT(ovrBool, ovr_SetBool, (ovrSession session, const char* propertyName, ovrBool value))
  685. OVR_DECLARE_IMPORT(int, ovr_GetInt, (ovrSession session, const char* propertyName, int defaultVal))
  686. OVR_DECLARE_IMPORT(ovrBool, ovr_SetInt, (ovrSession session, const char* propertyName, int value))
  687. OVR_DECLARE_IMPORT(float, ovr_GetFloat, (ovrSession session, const char* propertyName, float defaultVal))
  688. OVR_DECLARE_IMPORT(ovrBool, ovr_SetFloat, (ovrSession session, const char* propertyName, float value))
  689. OVR_DECLARE_IMPORT(unsigned int, ovr_GetFloatArray, (ovrSession session, const char* propertyName, float values[], unsigned int arraySize))
  690. OVR_DECLARE_IMPORT(ovrBool, ovr_SetFloatArray, (ovrSession session, const char* propertyName, const float values[], unsigned int arraySize))
  691. OVR_DECLARE_IMPORT(const char*, ovr_GetString, (ovrSession session, const char* propertyName, const char* defaultVal))
  692. OVR_DECLARE_IMPORT(ovrBool, ovr_SetString, (ovrSession session, const char* propertyName, const char* value))
  693. OVR_DECLARE_IMPORT(int, ovr_TraceMessage, (int level, const char* message))
  694. #if defined (_WIN32)
  695. OVR_DECLARE_IMPORT(ovrResult, ovr_CreateSwapTextureSetD3D11, (ovrSession session, ID3D11Device* device, const D3D11_TEXTURE2D_DESC* desc, unsigned int miscFlags, ovrSwapTextureSet** outTextureSet))
  696. OVR_DECLARE_IMPORT(ovrResult, ovr_CreateMirrorTextureD3D11, (ovrSession session, ID3D11Device* device, const D3D11_TEXTURE2D_DESC* desc, unsigned int miscFlags, ovrTexture** outMirrorTexture))
  697. #endif
  698. OVR_DECLARE_IMPORT(ovrResult, ovr_CreateSwapTextureSetGL, (ovrSession session, GLuint format, int width, int height, ovrSwapTextureSet** outTextureSet))
  699. OVR_DECLARE_IMPORT(ovrResult, ovr_CreateMirrorTextureGL, (ovrSession session, GLuint format, int width, int height, ovrTexture** outMirrorTexture))
  700. OVR_DECLARE_IMPORT(void, ovr_DestroySwapTextureSet, (ovrSession session, ovrSwapTextureSet* textureSet))
  701. OVR_DECLARE_IMPORT(void, ovr_DestroyMirrorTexture, (ovrSession session, ovrTexture* textureSet))
  702. OVR_DECLARE_IMPORT(ovrResult, ovr_SetQueueAheadFraction, (ovrSession session, float queueAheadFraction))
  703. OVR_DECLARE_IMPORT(ovrResult, ovr_Lookup, (const char* name, void** data));
  704. static ovrResult OVR_LoadSharedLibrary(int requestedProductVersion, int requestedMajorVersion)
  705. {
  706. FilePathCharType filePath[OVR_MAX_PATH];
  707. if(hLibOVR)
  708. return ovrSuccess;
  709. hLibOVR = OVR_FindLibraryPath(requestedProductVersion, requestedMajorVersion,
  710. filePath, sizeof(filePath) / sizeof(filePath[0]));
  711. if(!hLibOVR)
  712. return ovrError_LibLoad;
  713. OVR_GETFUNCTION(ovr_InitializeRenderingShimVersion)
  714. OVR_GETFUNCTION(ovr_Initialize)
  715. OVR_GETFUNCTION(ovr_Shutdown)
  716. OVR_GETFUNCTION(ovr_GetVersionString)
  717. OVR_GETFUNCTION(ovr_GetLastErrorInfo)
  718. OVR_GETFUNCTION(ovr_GetHmdDesc)
  719. OVR_GETFUNCTION(ovr_Create)
  720. OVR_GETFUNCTION(ovr_Destroy)
  721. OVR_GETFUNCTION(ovr_GetSessionStatus)
  722. OVR_GETFUNCTION(ovr_GetEnabledCaps)
  723. OVR_GETFUNCTION(ovr_SetEnabledCaps)
  724. OVR_GETFUNCTION(ovr_GetTrackingCaps)
  725. OVR_GETFUNCTION(ovr_ConfigureTracking)
  726. OVR_GETFUNCTION(ovr_RecenterPose)
  727. OVR_GETFUNCTION(ovr_GetTrackingState)
  728. OVR_GETFUNCTION(ovr_GetInputState)
  729. OVR_GETFUNCTION(ovr_SetControllerVibration)
  730. OVR_GETFUNCTION(ovr_GetFovTextureSize)
  731. OVR_GETFUNCTION(ovr_SubmitFrame)
  732. OVR_GETFUNCTION(ovr_GetRenderDesc)
  733. OVR_GETFUNCTION(ovr_GetPredictedDisplayTime)
  734. OVR_GETFUNCTION(ovr_GetTimeInSeconds)
  735. OVR_GETFUNCTION(ovr_ResetBackOfHeadTracking)
  736. OVR_GETFUNCTION(ovr_ResetMulticameraTracking)
  737. OVR_GETFUNCTION(ovr_GetBool)
  738. OVR_GETFUNCTION(ovr_SetBool)
  739. OVR_GETFUNCTION(ovr_GetInt)
  740. OVR_GETFUNCTION(ovr_SetInt)
  741. OVR_GETFUNCTION(ovr_GetFloat)
  742. OVR_GETFUNCTION(ovr_SetFloat)
  743. OVR_GETFUNCTION(ovr_GetFloatArray)
  744. OVR_GETFUNCTION(ovr_SetFloatArray)
  745. OVR_GETFUNCTION(ovr_GetString)
  746. OVR_GETFUNCTION(ovr_SetString)
  747. OVR_GETFUNCTION(ovr_TraceMessage)
  748. #if defined (_WIN32)
  749. OVR_GETFUNCTION(ovr_CreateSwapTextureSetD3D11)
  750. OVR_GETFUNCTION(ovr_CreateMirrorTextureD3D11)
  751. #endif
  752. OVR_GETFUNCTION(ovr_CreateSwapTextureSetGL)
  753. OVR_GETFUNCTION(ovr_CreateMirrorTextureGL)
  754. OVR_GETFUNCTION(ovr_DestroySwapTextureSet)
  755. OVR_GETFUNCTION(ovr_DestroyMirrorTexture)
  756. OVR_GETFUNCTION(ovr_SetQueueAheadFraction)
  757. OVR_GETFUNCTION(ovr_Lookup)
  758. return ovrSuccess;
  759. }
  760. static void OVR_UnloadSharedLibrary()
  761. {
  762. // To consider: Make all pointers be part of a struct and memset the struct to 0 here.
  763. ovr_InitializeRenderingShimVersionPtr = NULL;
  764. ovr_InitializePtr = NULL;
  765. ovr_ShutdownPtr = NULL;
  766. ovr_GetVersionStringPtr = NULL;
  767. ovr_GetLastErrorInfoPtr = NULL;
  768. ovr_GetHmdDescPtr = NULL;
  769. ovr_CreatePtr = NULL;
  770. ovr_DestroyPtr = NULL;
  771. ovr_GetSessionStatusPtr = NULL;
  772. ovr_GetEnabledCapsPtr = NULL;
  773. ovr_SetEnabledCapsPtr = NULL;
  774. ovr_GetTrackingCapsPtr = NULL;
  775. ovr_ConfigureTrackingPtr = NULL;
  776. ovr_RecenterPosePtr = NULL;
  777. ovr_GetTrackingStatePtr = NULL;
  778. ovr_GetInputStatePtr = NULL;
  779. ovr_SetControllerVibrationPtr = NULL;
  780. ovr_GetFovTextureSizePtr = NULL;
  781. ovr_SubmitFramePtr = NULL;
  782. ovr_GetRenderDescPtr = NULL;
  783. ovr_GetPredictedDisplayTimePtr = NULL;
  784. ovr_GetTimeInSecondsPtr = NULL;
  785. ovr_ResetBackOfHeadTrackingPtr = NULL;
  786. ovr_ResetMulticameraTrackingPtr = NULL;
  787. ovr_GetBoolPtr = NULL;
  788. ovr_SetBoolPtr = NULL;
  789. ovr_GetIntPtr = NULL;
  790. ovr_SetIntPtr = NULL;
  791. ovr_GetFloatPtr = NULL;
  792. ovr_SetFloatPtr = NULL;
  793. ovr_GetFloatArrayPtr = NULL;
  794. ovr_SetFloatArrayPtr = NULL;
  795. ovr_GetStringPtr = NULL;
  796. ovr_SetStringPtr = NULL;
  797. ovr_TraceMessagePtr = NULL;
  798. #if defined (_WIN32)
  799. ovr_CreateSwapTextureSetD3D11Ptr = NULL;
  800. ovr_CreateMirrorTextureD3D11Ptr = NULL;
  801. #endif
  802. ovr_CreateSwapTextureSetGLPtr = NULL;
  803. ovr_CreateMirrorTextureGLPtr = NULL;
  804. ovr_DestroySwapTextureSetPtr = NULL;
  805. ovr_DestroyMirrorTexturePtr = NULL;
  806. ovr_SetQueueAheadFractionPtr = NULL;
  807. ovr_LookupPtr = NULL;
  808. OVR_CloseLibrary(hLibOVR);
  809. hLibOVR = NULL;
  810. }
  811. OVR_PUBLIC_FUNCTION(ovrBool) ovr_InitializeRenderingShim()
  812. {
  813. #if 1
  814. return ovrTrue;
  815. #else
  816. return ovr_InitializeRenderingShimVersion(OVR_MINOR_VERSION);
  817. #endif
  818. }
  819. OVR_PUBLIC_FUNCTION(ovrBool) ovr_InitializeRenderingShimVersion(int requestedMinorVersion)
  820. {
  821. // By design we ignore the build version in the library search.
  822. ovrBool initializeResult;
  823. ovrResult result = OVR_LoadSharedLibrary(OVR_PRODUCT_VERSION, OVR_MAJOR_VERSION);
  824. if (result != ovrSuccess)
  825. return ovrFalse;
  826. initializeResult = ovr_InitializeRenderingShimVersionPtr(requestedMinorVersion);
  827. if (initializeResult == ovrFalse)
  828. OVR_UnloadSharedLibrary();
  829. return initializeResult;
  830. }
  831. // These defaults are also in CAPI.cpp
  832. static const ovrInitParams DefaultParams = {
  833. ovrInit_RequestVersion, // Flags
  834. OVR_MINOR_VERSION, // RequestedMinorVersion
  835. 0, // LogCallback
  836. 0, // UserData
  837. 0, // ConnectionTimeoutSeconds
  838. OVR_ON64("") // pad0
  839. };
  840. OVR_PUBLIC_FUNCTION(ovrResult) ovr_Initialize(const ovrInitParams* inputParams)
  841. {
  842. ovrResult result;
  843. ovrInitParams params;
  844. // Do something with our version signature hash to prevent
  845. // it from being optimized out. In this case, compute
  846. // a cheap CRC.
  847. uint8_t crc = 0;
  848. size_t i;
  849. for (i = 0; i < sizeof(VersionSHA256); ++i)
  850. {
  851. crc ^= VersionSHA256[i];
  852. }
  853. assert(crc == VersionXOR);
  854. if (crc != VersionXOR)
  855. {
  856. return ovrError_Initialize;
  857. }
  858. if (!inputParams)
  859. {
  860. params = DefaultParams;
  861. }
  862. else
  863. {
  864. params = *inputParams;
  865. // If not requesting a particular minor version,
  866. if (!(params.Flags & ovrInit_RequestVersion))
  867. {
  868. // Enable requesting the default minor version.
  869. params.Flags |= ovrInit_RequestVersion;
  870. params.RequestedMinorVersion = OVR_MINOR_VERSION;
  871. }
  872. }
  873. // Clear non-writable bits provided by client code.
  874. params.Flags &= ovrinit_WritableBits;
  875. // By design we ignore the build version in the library search.
  876. result = OVR_LoadSharedLibrary(OVR_PRODUCT_VERSION, OVR_MAJOR_VERSION);
  877. if (result != ovrSuccess)
  878. return result;
  879. result = ovr_InitializePtr(&params);
  880. if (result != ovrSuccess)
  881. OVR_UnloadSharedLibrary();
  882. return result;
  883. }
  884. OVR_PUBLIC_FUNCTION(void) ovr_Shutdown()
  885. {
  886. if (!ovr_ShutdownPtr)
  887. return;
  888. ovr_ShutdownPtr();
  889. OVR_UnloadSharedLibrary();
  890. }
  891. OVR_PUBLIC_FUNCTION(const char*) ovr_GetVersionString()
  892. {
  893. // We don't directly return the value of the DLL ovr_GetVersionStringPtr call,
  894. // because that call returns a pointer to memory within the DLL. If the DLL goes
  895. // away then that pointer becomes invalid while the process may still be holding
  896. // onto it. So we save a local copy of it which is always valid.
  897. static char dllVersionStringLocal[32];
  898. const char* dllVersionString;
  899. if (!ovr_GetVersionStringPtr)
  900. return "(Unable to load LibOVR)";
  901. dllVersionString = ovr_GetVersionStringPtr(); // Guaranteed to always be valid.
  902. assert(dllVersionString != NULL);
  903. OVR_strlcpy(dllVersionStringLocal, dllVersionString, sizeof(dllVersionStringLocal));
  904. return dllVersionStringLocal;
  905. }
  906. OVR_PUBLIC_FUNCTION(void) ovr_GetLastErrorInfo(ovrErrorInfo* errorInfo)
  907. {
  908. if (!ovr_GetLastErrorInfoPtr)
  909. {
  910. memset(errorInfo, 0, sizeof(ovrErrorInfo));
  911. errorInfo->Result = ovrError_LibLoad;
  912. }
  913. else
  914. ovr_GetLastErrorInfoPtr(errorInfo);
  915. }
  916. OVR_PUBLIC_FUNCTION(ovrHmdDesc) ovr_GetHmdDesc(ovrSession session)
  917. {
  918. if (!ovr_GetHmdDescPtr)
  919. {
  920. ovrHmdDesc hmdDesc;
  921. memset(&hmdDesc, 0, sizeof(hmdDesc));
  922. hmdDesc.Type = ovrHmd_None;
  923. return hmdDesc;
  924. }
  925. return ovr_GetHmdDescPtr(session);
  926. }
  927. OVR_PUBLIC_FUNCTION(ovrResult) ovr_Create(ovrSession* pSession, ovrGraphicsLuid* pLuid)
  928. {
  929. if (!ovr_CreatePtr)
  930. return ovrError_NotInitialized;
  931. return ovr_CreatePtr(pSession, pLuid);
  932. }
  933. OVR_PUBLIC_FUNCTION(void) ovr_Destroy(ovrSession session)
  934. {
  935. if (!ovr_DestroyPtr)
  936. return;
  937. ovr_DestroyPtr(session);
  938. }
  939. OVR_PUBLIC_FUNCTION(ovrResult) ovr_GetSessionStatus(ovrSession session, ovrSessionStatus* sessionStatus)
  940. {
  941. if (!ovr_GetSessionStatusPtr)
  942. return ovrError_NotInitialized;
  943. return ovr_GetSessionStatusPtr(session, sessionStatus);
  944. }
  945. OVR_PUBLIC_FUNCTION(unsigned int) ovr_GetEnabledCaps(ovrSession session)
  946. {
  947. if (!ovr_GetEnabledCapsPtr)
  948. return 0;
  949. return ovr_GetEnabledCapsPtr(session);
  950. }
  951. OVR_PUBLIC_FUNCTION(void) ovr_SetEnabledCaps(ovrSession session, unsigned int hmdCaps)
  952. {
  953. if (!ovr_SetEnabledCapsPtr)
  954. return;
  955. ovr_SetEnabledCapsPtr(session, hmdCaps);
  956. }
  957. OVR_PUBLIC_FUNCTION(unsigned int) ovr_GetTrackingCaps(ovrSession session)
  958. {
  959. if (!ovr_GetTrackingCapsPtr)
  960. return 0;
  961. return ovr_GetTrackingCapsPtr(session);
  962. }
  963. OVR_PUBLIC_FUNCTION(ovrResult) ovr_ConfigureTracking(ovrSession session, unsigned int requestedTrackingCaps,
  964. unsigned int requiredTrackingCaps)
  965. {
  966. if (!ovr_ConfigureTrackingPtr)
  967. return ovrError_NotInitialized;
  968. return ovr_ConfigureTrackingPtr(session, requestedTrackingCaps, requiredTrackingCaps);
  969. }
  970. OVR_PUBLIC_FUNCTION(void) ovr_RecenterPose(ovrSession session)
  971. {
  972. if (!ovr_RecenterPosePtr)
  973. return;
  974. ovr_RecenterPosePtr(session);
  975. }
  976. OVR_PUBLIC_FUNCTION(ovrTrackingState) ovr_GetTrackingState(ovrSession session, double absTime, ovrBool latencyMarker)
  977. {
  978. if (!ovr_GetTrackingStatePtr)
  979. {
  980. ovrTrackingState nullTrackingState;
  981. memset(&nullTrackingState, 0, sizeof(nullTrackingState));
  982. return nullTrackingState;
  983. }
  984. return ovr_GetTrackingStatePtr(session, absTime, latencyMarker);
  985. }
  986. OVR_PUBLIC_FUNCTION(ovrResult) ovr_GetInputState(ovrSession session, unsigned int controllerMask, ovrInputState* inputState)
  987. {
  988. if (!ovr_GetInputStatePtr)
  989. {
  990. if (inputState)
  991. memset(inputState, 0, sizeof(ovrInputState));
  992. return ovrError_NotInitialized;
  993. }
  994. return ovr_GetInputStatePtr(session, controllerMask, inputState);
  995. }
  996. OVR_PUBLIC_FUNCTION(ovrResult) ovr_SetControllerVibration(ovrSession session, unsigned int controllerTypeMask, float frequency, float amplitude)
  997. {
  998. if (!ovr_SetControllerVibrationPtr)
  999. {
  1000. return ovrError_NotInitialized;
  1001. }
  1002. return ovr_SetControllerVibrationPtr(session, controllerTypeMask, frequency, amplitude);
  1003. }
  1004. OVR_PUBLIC_FUNCTION(ovrSizei) ovr_GetFovTextureSize(ovrSession session, ovrEyeType eye, ovrFovPort fov,
  1005. float pixelsPerDisplayPixel)
  1006. {
  1007. if (!ovr_GetFovTextureSizePtr)
  1008. {
  1009. ovrSizei nullSize;
  1010. memset(&nullSize, 0, sizeof(nullSize));
  1011. return nullSize;
  1012. }
  1013. return ovr_GetFovTextureSizePtr(session, eye, fov, pixelsPerDisplayPixel);
  1014. }
  1015. #if defined (_WIN32)
  1016. OVR_PUBLIC_FUNCTION(ovrResult) ovr_CreateSwapTextureSetD3D11(ovrSession session,
  1017. ID3D11Device* device,
  1018. const D3D11_TEXTURE2D_DESC* desc,
  1019. unsigned int miscFlags,
  1020. ovrSwapTextureSet** outTextureSet)
  1021. {
  1022. if (!ovr_CreateSwapTextureSetD3D11Ptr)
  1023. return ovrError_NotInitialized;
  1024. return ovr_CreateSwapTextureSetD3D11Ptr(session, device, desc, miscFlags, outTextureSet);
  1025. }
  1026. OVR_PUBLIC_FUNCTION(ovrResult) ovr_CreateMirrorTextureD3D11(ovrSession session,
  1027. ID3D11Device* device,
  1028. const D3D11_TEXTURE2D_DESC* desc,
  1029. unsigned int miscFlags,
  1030. ovrTexture** outMirrorTexture)
  1031. {
  1032. if (!ovr_CreateMirrorTextureD3D11Ptr)
  1033. return ovrError_NotInitialized;
  1034. return ovr_CreateMirrorTextureD3D11Ptr(session, device, desc, miscFlags, outMirrorTexture);
  1035. }
  1036. #endif
  1037. OVR_PUBLIC_FUNCTION(ovrResult) ovr_CreateSwapTextureSetGL(ovrSession session, GLuint format,
  1038. int width, int height,
  1039. ovrSwapTextureSet** outTextureSet)
  1040. {
  1041. if (!ovr_CreateSwapTextureSetGLPtr)
  1042. return ovrError_NotInitialized;
  1043. return ovr_CreateSwapTextureSetGLPtr(session, format, width, height, outTextureSet);
  1044. }
  1045. OVR_PUBLIC_FUNCTION(ovrResult) ovr_CreateMirrorTextureGL(ovrSession session, GLuint format,
  1046. int width, int height,
  1047. ovrTexture** outMirrorTexture)
  1048. {
  1049. if (!ovr_CreateMirrorTextureGLPtr)
  1050. return ovrError_NotInitialized;
  1051. return ovr_CreateMirrorTextureGLPtr(session, format, width, height, outMirrorTexture);
  1052. }
  1053. OVR_PUBLIC_FUNCTION(void) ovr_DestroySwapTextureSet(ovrSession session, ovrSwapTextureSet* textureSet)
  1054. {
  1055. if (!ovr_DestroySwapTextureSetPtr)
  1056. return;
  1057. ovr_DestroySwapTextureSetPtr(session, textureSet);
  1058. }
  1059. OVR_PUBLIC_FUNCTION(void) ovr_DestroyMirrorTexture(ovrSession session, ovrTexture* mirrorTexture)
  1060. {
  1061. if (!ovr_DestroyMirrorTexturePtr)
  1062. return;
  1063. ovr_DestroyMirrorTexturePtr(session, mirrorTexture);
  1064. }
  1065. OVR_PUBLIC_FUNCTION(ovrResult) ovr_SetQueueAheadFraction(ovrSession session, float queueAheadFraction)
  1066. {
  1067. if (!ovr_SetQueueAheadFractionPtr)
  1068. return ovrError_NotInitialized;
  1069. return ovr_SetQueueAheadFractionPtr(session, queueAheadFraction);
  1070. }
  1071. OVR_PUBLIC_FUNCTION(ovrResult) ovr_SubmitFrame(ovrSession session, long long frameIndex, const ovrViewScaleDesc* viewScaleDesc, ovrLayerHeader const * const * layerPtrList, unsigned int layerCount)
  1072. {
  1073. if (!ovr_SubmitFramePtr)
  1074. return ovrError_NotInitialized;
  1075. return ovr_SubmitFramePtr(session, frameIndex, viewScaleDesc, layerPtrList, layerCount);
  1076. }
  1077. OVR_PUBLIC_FUNCTION(ovrEyeRenderDesc) ovr_GetRenderDesc(ovrSession session, ovrEyeType eyeType, ovrFovPort fov)
  1078. {
  1079. if (!ovr_GetRenderDescPtr)
  1080. {
  1081. ovrEyeRenderDesc nullEyeRenderDesc;
  1082. memset(&nullEyeRenderDesc, 0, sizeof(nullEyeRenderDesc));
  1083. return nullEyeRenderDesc;
  1084. }
  1085. return ovr_GetRenderDescPtr(session, eyeType, fov);
  1086. }
  1087. OVR_PUBLIC_FUNCTION(double) ovr_GetPredictedDisplayTime(ovrSession session, long long frameIndex)
  1088. {
  1089. if (!ovr_GetPredictedDisplayTimePtr)
  1090. return 0.0;
  1091. return ovr_GetPredictedDisplayTimePtr(session, frameIndex);
  1092. }
  1093. OVR_PUBLIC_FUNCTION(double) ovr_GetTimeInSeconds()
  1094. {
  1095. if (!ovr_GetTimeInSecondsPtr)
  1096. return 0.;
  1097. return ovr_GetTimeInSecondsPtr();
  1098. }
  1099. OVR_PUBLIC_FUNCTION(void) ovr_ResetBackOfHeadTracking(ovrSession session)
  1100. {
  1101. if (!ovr_ResetBackOfHeadTrackingPtr)
  1102. return;
  1103. ovr_ResetBackOfHeadTrackingPtr(session);
  1104. }
  1105. OVR_PUBLIC_FUNCTION(void) ovr_ResetMulticameraTracking(ovrSession session)
  1106. {
  1107. if (!ovr_ResetMulticameraTrackingPtr)
  1108. return;
  1109. ovr_ResetMulticameraTrackingPtr(session);
  1110. }
  1111. OVR_PUBLIC_FUNCTION(ovrBool) ovr_GetBool(ovrSession session, const char* propertyName, ovrBool defaultVal)
  1112. {
  1113. if (!ovr_GetBoolPtr)
  1114. return ovrFalse;
  1115. return ovr_GetBoolPtr(session, propertyName, defaultVal);
  1116. }
  1117. OVR_PUBLIC_FUNCTION(ovrBool) ovr_SetBool(ovrSession session, const char* propertyName, ovrBool value)
  1118. {
  1119. if (!ovr_SetBoolPtr)
  1120. return ovrFalse;
  1121. return ovr_SetBoolPtr(session, propertyName, value);
  1122. }
  1123. OVR_PUBLIC_FUNCTION(int) ovr_GetInt(ovrSession session, const char* propertyName, int defaultVal)
  1124. {
  1125. if (!ovr_GetIntPtr)
  1126. return 0;
  1127. return ovr_GetIntPtr(session, propertyName, defaultVal);
  1128. }
  1129. OVR_PUBLIC_FUNCTION(ovrBool) ovr_SetInt(ovrSession session, const char* propertyName, int value)
  1130. {
  1131. if (!ovr_SetIntPtr)
  1132. return ovrFalse;
  1133. return ovr_SetIntPtr(session, propertyName, value);
  1134. }
  1135. OVR_PUBLIC_FUNCTION(float) ovr_GetFloat(ovrSession session, const char* propertyName, float defaultVal)
  1136. {
  1137. if (!ovr_GetFloatPtr)
  1138. return 0.f;
  1139. return ovr_GetFloatPtr(session, propertyName, defaultVal);
  1140. }
  1141. OVR_PUBLIC_FUNCTION(ovrBool) ovr_SetFloat(ovrSession session, const char* propertyName, float value)
  1142. {
  1143. if (!ovr_SetFloatPtr)
  1144. return ovrFalse;
  1145. return ovr_SetFloatPtr(session, propertyName, value);
  1146. }
  1147. OVR_PUBLIC_FUNCTION(unsigned int) ovr_GetFloatArray(ovrSession session, const char* propertyName,
  1148. float values[], unsigned int arraySize)
  1149. {
  1150. if (!ovr_GetFloatArrayPtr)
  1151. return 0;
  1152. return ovr_GetFloatArrayPtr(session, propertyName, values, arraySize);
  1153. }
  1154. OVR_PUBLIC_FUNCTION(ovrBool) ovr_SetFloatArray(ovrSession session, const char* propertyName,
  1155. const float values[], unsigned int arraySize)
  1156. {
  1157. if (!ovr_SetFloatArrayPtr)
  1158. return ovrFalse;
  1159. return ovr_SetFloatArrayPtr(session, propertyName, values, arraySize);
  1160. }
  1161. OVR_PUBLIC_FUNCTION(const char*) ovr_GetString(ovrSession session, const char* propertyName,
  1162. const char* defaultVal)
  1163. {
  1164. if (!ovr_GetStringPtr)
  1165. return "(Unable to load LibOVR)";
  1166. return ovr_GetStringPtr(session, propertyName, defaultVal);
  1167. }
  1168. OVR_PUBLIC_FUNCTION(ovrBool) ovr_SetString(ovrSession session, const char* propertyName,
  1169. const char* value)
  1170. {
  1171. if (!ovr_SetStringPtr)
  1172. return ovrFalse;
  1173. return ovr_SetStringPtr(session, propertyName, value);
  1174. }
  1175. OVR_PUBLIC_FUNCTION(int) ovr_TraceMessage(int level, const char* message)
  1176. {
  1177. if (!ovr_TraceMessagePtr)
  1178. return -1;
  1179. return ovr_TraceMessagePtr(level, message);
  1180. }
  1181. OVR_PUBLIC_FUNCTION(ovrResult) ovr_Lookup(const char* name, void** data)
  1182. {
  1183. if (!ovr_LookupPtr)
  1184. return ovrError_NotInitialized;
  1185. return ovr_LookupPtr(name, data);
  1186. }
  1187. #if defined(_MSC_VER)
  1188. #pragma warning(pop)
  1189. #endif