openhmd.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. /*
  2. * OpenHMD - Free and Open Source API and drivers for immersive technology.
  3. * Copyright (C) 2013 Fredrik Hultin.
  4. * Copyright (C) 2013 Jakob Bornecrantz.
  5. * Distributed under the Boost 1.0 licence, see LICENSE for full text.
  6. */
  7. /* Main Lib Implementation */
  8. #include "openhmdi.h"
  9. #include "shaders.h"
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <stdio.h>
  13. // Running automatic updates at 1000 Hz
  14. #define AUTOMATIC_UPDATE_SLEEP (1.0 / 1000.0)
  15. ohmd_context* OHMD_APIENTRY ohmd_ctx_create(void)
  16. {
  17. ohmd_context* ctx = calloc(1, sizeof(ohmd_context));
  18. if(!ctx){
  19. LOGE("could not allocate RAM for context");
  20. return NULL;
  21. }
  22. ohmd_monotonic_init(ctx);
  23. #if DRIVER_OCULUS_RIFT
  24. ctx->drivers[ctx->num_drivers++] = ohmd_create_oculus_rift_drv(ctx);
  25. #endif
  26. #if DRIVER_DEEPOON
  27. ctx->drivers[ctx->num_drivers++] = ohmd_create_deepoon_drv(ctx);
  28. #endif
  29. #if DRIVER_HTC_VIVE
  30. ctx->drivers[ctx->num_drivers++] = ohmd_create_htc_vive_drv(ctx);
  31. #endif
  32. #if DRIVER_WMR
  33. ctx->drivers[ctx->num_drivers++] = ohmd_create_wmr_drv(ctx);
  34. #endif
  35. #if DRIVER_PSVR
  36. ctx->drivers[ctx->num_drivers++] = ohmd_create_psvr_drv(ctx);
  37. #endif
  38. #if DRIVER_NOLO
  39. ctx->drivers[ctx->num_drivers++] = ohmd_create_nolo_drv(ctx);
  40. #endif
  41. #if DRIVER_XGVR
  42. ctx->drivers[ctx->num_drivers++] = ohmd_create_xgvr_drv(ctx);
  43. #endif
  44. #if DRIVER_EXTERNAL
  45. ctx->drivers[ctx->num_drivers++] = ohmd_create_external_drv(ctx);
  46. #endif
  47. #if DRIVER_ANDROID
  48. ctx->drivers[ctx->num_drivers++] = ohmd_create_android_drv(ctx);
  49. #endif
  50. // add dummy driver last to make it the lowest priority
  51. ctx->drivers[ctx->num_drivers++] = ohmd_create_dummy_drv(ctx);
  52. ctx->update_request_quit = false;
  53. return ctx;
  54. }
  55. void OHMD_APIENTRY ohmd_ctx_destroy(ohmd_context* ctx)
  56. {
  57. ctx->update_request_quit = true;
  58. for(int i = 0; i < ctx->num_active_devices; i++){
  59. ctx->active_devices[i]->close(ctx->active_devices[i]);
  60. }
  61. for(int i = 0; i < ctx->num_drivers; i++){
  62. ctx->drivers[i]->destroy(ctx->drivers[i]);
  63. }
  64. if(ctx->update_thread){
  65. ohmd_destroy_thread(ctx->update_thread);
  66. ohmd_destroy_mutex(ctx->update_mutex);
  67. }
  68. free(ctx);
  69. }
  70. void OHMD_APIENTRY ohmd_ctx_update(ohmd_context* ctx)
  71. {
  72. for(int i = 0; i < ctx->num_active_devices; i++){
  73. ohmd_device* dev = ctx->active_devices[i];
  74. if(!dev->settings.automatic_update && dev->update)
  75. dev->update(dev);
  76. ohmd_lock_mutex(ctx->update_mutex);
  77. dev->getf(dev, OHMD_POSITION_VECTOR, (float*)&dev->position);
  78. dev->getf(dev, OHMD_ROTATION_QUAT, (float*)&dev->rotation);
  79. ohmd_unlock_mutex(ctx->update_mutex);
  80. }
  81. }
  82. const char* OHMD_APIENTRY ohmd_ctx_get_error(ohmd_context* ctx)
  83. {
  84. return ctx->error_msg;
  85. }
  86. int OHMD_APIENTRY ohmd_ctx_probe(ohmd_context* ctx)
  87. {
  88. memset(&ctx->list, 0, sizeof(ohmd_device_list));
  89. for(int i = 0; i < ctx->num_drivers; i++){
  90. ctx->drivers[i]->get_device_list(ctx->drivers[i], &ctx->list);
  91. }
  92. return ctx->list.num_devices;
  93. }
  94. int OHMD_APIENTRY ohmd_gets(ohmd_string_description type, const char ** out)
  95. {
  96. switch(type){
  97. case OHMD_GLSL_DISTORTION_VERT_SRC:
  98. *out = distortion_vert;
  99. return OHMD_S_OK;
  100. case OHMD_GLSL_DISTORTION_FRAG_SRC:
  101. *out = distortion_frag;
  102. return OHMD_S_OK;
  103. case OHMD_GLSL_330_DISTORTION_VERT_SRC:
  104. *out = distortion_vert_330;
  105. return OHMD_S_OK;
  106. case OHMD_GLSL_330_DISTORTION_FRAG_SRC:
  107. *out = distortion_frag_330;
  108. return OHMD_S_OK;
  109. default:
  110. return OHMD_S_UNSUPPORTED;
  111. }
  112. }
  113. const char* OHMD_APIENTRY ohmd_list_gets(ohmd_context* ctx, int index, ohmd_string_value type)
  114. {
  115. if(index >= ctx->list.num_devices)
  116. return NULL;
  117. switch(type){
  118. case OHMD_VENDOR:
  119. return ctx->list.devices[index].vendor;
  120. case OHMD_PRODUCT:
  121. return ctx->list.devices[index].product;
  122. case OHMD_PATH:
  123. return ctx->list.devices[index].path;
  124. default:
  125. return NULL;
  126. }
  127. }
  128. int OHMD_APIENTRY ohmd_list_geti(ohmd_context* ctx, int index, ohmd_int_value type, int* out)
  129. {
  130. if(index >= ctx->list.num_devices)
  131. return OHMD_S_INVALID_PARAMETER;
  132. switch(type){
  133. case OHMD_DEVICE_CLASS:
  134. *out = ctx->list.devices[index].device_class;
  135. return OHMD_S_OK;
  136. case OHMD_DEVICE_FLAGS:
  137. *out = ctx->list.devices[index].device_flags;
  138. return OHMD_S_OK;
  139. default:
  140. return OHMD_S_INVALID_PARAMETER;
  141. }
  142. }
  143. static unsigned int ohmd_update_thread(void* arg)
  144. {
  145. ohmd_context* ctx = (ohmd_context*)arg;
  146. while(!ctx->update_request_quit)
  147. {
  148. ohmd_lock_mutex(ctx->update_mutex);
  149. for(int i = 0; i < ctx->num_active_devices; i++){
  150. if(ctx->active_devices[i]->settings.automatic_update && ctx->active_devices[i]->update)
  151. ctx->active_devices[i]->update(ctx->active_devices[i]);
  152. }
  153. ohmd_unlock_mutex(ctx->update_mutex);
  154. ohmd_sleep(AUTOMATIC_UPDATE_SLEEP);
  155. }
  156. return 0;
  157. }
  158. static void ohmd_set_up_update_thread(ohmd_context* ctx)
  159. {
  160. if(!ctx->update_thread){
  161. ctx->update_mutex = ohmd_create_mutex(ctx);
  162. ctx->update_thread = ohmd_create_thread(ctx, ohmd_update_thread, ctx);
  163. }
  164. }
  165. ohmd_device* OHMD_APIENTRY ohmd_list_open_device_s(ohmd_context* ctx, int index, ohmd_device_settings* settings)
  166. {
  167. ohmd_lock_mutex(ctx->update_mutex);
  168. if(index >= 0 && index < ctx->list.num_devices){
  169. ohmd_device_desc* desc = &ctx->list.devices[index];
  170. ohmd_driver* driver = (ohmd_driver*)desc->driver_ptr;
  171. ohmd_device* device = driver->open_device(driver, desc);
  172. if (device == NULL) {
  173. ohmd_set_error(ctx, "Could not open device with index: %d, check device permissions?", index);
  174. ohmd_unlock_mutex(ctx->update_mutex);
  175. return NULL;
  176. }
  177. device->rotation_correction.w = 1;
  178. device->settings = *settings;
  179. device->ctx = ctx;
  180. device->active_device_idx = ctx->num_active_devices;
  181. ctx->active_devices[ctx->num_active_devices++] = device;
  182. ohmd_unlock_mutex(ctx->update_mutex);
  183. if(device->settings.automatic_update)
  184. ohmd_set_up_update_thread(ctx);
  185. return device;
  186. }
  187. ohmd_unlock_mutex(ctx->update_mutex);
  188. ohmd_set_error(ctx, "no device with index: %d", index);
  189. return NULL;
  190. }
  191. ohmd_device* OHMD_APIENTRY ohmd_list_open_device(ohmd_context* ctx, int index)
  192. {
  193. ohmd_device_settings settings;
  194. settings.automatic_update = true;
  195. return ohmd_list_open_device_s(ctx, index, &settings);
  196. }
  197. int OHMD_APIENTRY ohmd_close_device(ohmd_device* device)
  198. {
  199. ohmd_lock_mutex(device->ctx->update_mutex);
  200. ohmd_context* ctx = device->ctx;
  201. int idx = device->active_device_idx;
  202. memmove(ctx->active_devices + idx, ctx->active_devices + idx + 1,
  203. sizeof(ohmd_device*) * (ctx->num_active_devices - idx - 1));
  204. device->close(device);
  205. ctx->num_active_devices--;
  206. for(int i = idx; i < ctx->num_active_devices; i++)
  207. ctx->active_devices[i]->active_device_idx--;
  208. ohmd_unlock_mutex(ctx->update_mutex);
  209. return OHMD_S_OK;
  210. }
  211. static int ohmd_device_getf_unp(ohmd_device* device, ohmd_float_value type, float* out)
  212. {
  213. switch(type){
  214. case OHMD_LEFT_EYE_GL_MODELVIEW_MATRIX: {
  215. vec3f point = {{0, 0, 0}};
  216. quatf rot = device->rotation;
  217. oquatf_mult_me(&rot, &device->rotation_correction);
  218. mat4x4f orient, world_shift, result;
  219. omat4x4f_init_look_at(&orient, &rot, &point);
  220. omat4x4f_init_translate(&world_shift, -device->position.x +(device->properties.ipd / 2.0f), -device->position.y, -device->position.z);
  221. omat4x4f_mult(&world_shift, &orient, &result);
  222. omat4x4f_transpose(&result, (mat4x4f*)out);
  223. return OHMD_S_OK;
  224. }
  225. case OHMD_RIGHT_EYE_GL_MODELVIEW_MATRIX: {
  226. vec3f point = {{0, 0, 0}};
  227. quatf rot = device->rotation;
  228. oquatf_mult_me(&rot, &device->rotation_correction);
  229. mat4x4f orient, world_shift, result;
  230. omat4x4f_init_look_at(&orient, &rot, &point);
  231. omat4x4f_init_translate(&world_shift, -device->position.x + -(device->properties.ipd / 2.0f), -device->position.y, -device->position.z);
  232. omat4x4f_mult(&world_shift, &orient, &result);
  233. omat4x4f_transpose(&result, (mat4x4f*)out);
  234. return OHMD_S_OK;
  235. }
  236. case OHMD_LEFT_EYE_GL_PROJECTION_MATRIX:
  237. omat4x4f_transpose(&device->properties.proj_left, (mat4x4f*)out);
  238. return OHMD_S_OK;
  239. case OHMD_RIGHT_EYE_GL_PROJECTION_MATRIX:
  240. omat4x4f_transpose(&device->properties.proj_right, (mat4x4f*)out);
  241. return OHMD_S_OK;
  242. case OHMD_SCREEN_HORIZONTAL_SIZE:
  243. *out = device->properties.hsize;
  244. return OHMD_S_OK;
  245. case OHMD_SCREEN_VERTICAL_SIZE:
  246. *out = device->properties.vsize;
  247. return OHMD_S_OK;
  248. case OHMD_LENS_HORIZONTAL_SEPARATION:
  249. *out = device->properties.lens_sep;
  250. return OHMD_S_OK;
  251. case OHMD_LENS_VERTICAL_POSITION:
  252. *out = device->properties.lens_vpos;
  253. return OHMD_S_OK;
  254. case OHMD_RIGHT_EYE_FOV:
  255. case OHMD_LEFT_EYE_FOV:
  256. *out = device->properties.fov;
  257. return OHMD_S_OK;
  258. case OHMD_RIGHT_EYE_ASPECT_RATIO:
  259. case OHMD_LEFT_EYE_ASPECT_RATIO:
  260. *out = device->properties.ratio;
  261. return OHMD_S_OK;
  262. case OHMD_EYE_IPD:
  263. *out = device->properties.ipd;
  264. return OHMD_S_OK;
  265. case OHMD_PROJECTION_ZFAR:
  266. *out = device->properties.zfar;
  267. return OHMD_S_OK;
  268. case OHMD_PROJECTION_ZNEAR:
  269. *out = device->properties.znear;
  270. return OHMD_S_OK;
  271. case OHMD_ROTATION_QUAT:
  272. {
  273. *(quatf*)out = device->rotation;
  274. oquatf_mult_me((quatf*)out, &device->rotation_correction);
  275. return OHMD_S_OK;
  276. }
  277. case OHMD_POSITION_VECTOR:
  278. {
  279. *(vec3f*)out = device->position;
  280. for(int i = 0; i < 3; i++)
  281. out[i] += device->position_correction.arr[i];
  282. return OHMD_S_OK;
  283. }
  284. case OHMD_UNIVERSAL_DISTORTION_K: {
  285. for (int i = 0; i < 4; i++) {
  286. out[i] = device->properties.universal_distortion_k[i];
  287. }
  288. return OHMD_S_OK;
  289. }
  290. case OHMD_UNIVERSAL_ABERRATION_K: {
  291. for (int i = 0; i < 3; i++) {
  292. out[i] = device->properties.universal_aberration_k[i];
  293. }
  294. return OHMD_S_OK;
  295. }
  296. default:
  297. return device->getf(device, type, out);
  298. }
  299. }
  300. int OHMD_APIENTRY ohmd_device_getf(ohmd_device* device, ohmd_float_value type, float* out)
  301. {
  302. ohmd_lock_mutex(device->ctx->update_mutex);
  303. int ret = ohmd_device_getf_unp(device, type, out);
  304. ohmd_unlock_mutex(device->ctx->update_mutex);
  305. return ret;
  306. }
  307. int ohmd_device_setf_unp(ohmd_device* device, ohmd_float_value type, const float* in)
  308. {
  309. switch(type){
  310. case OHMD_EYE_IPD:
  311. device->properties.ipd = *in;
  312. return OHMD_S_OK;
  313. case OHMD_PROJECTION_ZFAR:
  314. device->properties.zfar = *in;
  315. return OHMD_S_OK;
  316. case OHMD_PROJECTION_ZNEAR:
  317. device->properties.znear = *in;
  318. return OHMD_S_OK;
  319. case OHMD_ROTATION_QUAT:
  320. {
  321. // adjust rotation correction
  322. quatf q;
  323. int ret = device->getf(device, OHMD_ROTATION_QUAT, (float*)&q);
  324. if(ret != 0){
  325. return ret;
  326. }
  327. oquatf_diff(&q, (quatf*)in, &device->rotation_correction);
  328. return OHMD_S_OK;
  329. }
  330. case OHMD_POSITION_VECTOR:
  331. {
  332. // adjust position correction
  333. vec3f v;
  334. int ret = device->getf(device, OHMD_POSITION_VECTOR, (float*)&v);
  335. if(ret != 0){
  336. return ret;
  337. }
  338. for(int i = 0; i < 3; i++)
  339. device->position_correction.arr[i] = in[i] - v.arr[i];
  340. return OHMD_S_OK;
  341. }
  342. case OHMD_EXTERNAL_SENSOR_FUSION:
  343. {
  344. if(device->setf == NULL)
  345. return OHMD_S_UNSUPPORTED;
  346. return device->setf(device, type, in);
  347. }
  348. default:
  349. return OHMD_S_INVALID_PARAMETER;
  350. }
  351. }
  352. int OHMD_APIENTRY ohmd_device_setf(ohmd_device* device, ohmd_float_value type, const float* in)
  353. {
  354. ohmd_lock_mutex(device->ctx->update_mutex);
  355. int ret = ohmd_device_setf_unp(device, type, in);
  356. ohmd_unlock_mutex(device->ctx->update_mutex);
  357. return ret;
  358. }
  359. int OHMD_APIENTRY ohmd_device_geti(ohmd_device* device, ohmd_int_value type, int* out)
  360. {
  361. switch(type){
  362. case OHMD_SCREEN_HORIZONTAL_RESOLUTION:
  363. *out = device->properties.hres;
  364. return OHMD_S_OK;
  365. case OHMD_SCREEN_VERTICAL_RESOLUTION:
  366. *out = device->properties.vres;
  367. return OHMD_S_OK;
  368. case OHMD_CONTROL_COUNT:
  369. *out = device->properties.control_count;
  370. return OHMD_S_OK;
  371. case OHMD_CONTROLS_TYPES:
  372. memcpy(out, device->properties.controls_types, device->properties.control_count * sizeof(int));
  373. return OHMD_S_OK;
  374. case OHMD_CONTROLS_HINTS:
  375. memcpy(out, device->properties.controls_hints, device->properties.control_count * sizeof(int));
  376. return OHMD_S_OK;
  377. default:
  378. return OHMD_S_INVALID_PARAMETER;
  379. }
  380. }
  381. int OHMD_APIENTRY ohmd_device_seti(ohmd_device* device, ohmd_int_value type, const int* in)
  382. {
  383. switch(type){
  384. default:
  385. return OHMD_S_INVALID_PARAMETER;
  386. }
  387. }
  388. int ohmd_device_set_data_unp(ohmd_device* device, ohmd_data_value type, const void* in)
  389. {
  390. switch(type){
  391. case OHMD_DRIVER_DATA:
  392. device->set_data(device, OHMD_DRIVER_DATA, in);
  393. return OHMD_S_OK;
  394. case OHMD_DRIVER_PROPERTIES:
  395. device->set_data(device, OHMD_DRIVER_PROPERTIES, in);
  396. return OHMD_S_OK;
  397. default:
  398. return OHMD_S_INVALID_PARAMETER;
  399. }
  400. }
  401. int OHMD_APIENTRY ohmd_device_set_data(ohmd_device* device, ohmd_data_value type, const void* in)
  402. {
  403. ohmd_lock_mutex(device->ctx->update_mutex);
  404. int ret = ohmd_device_set_data_unp(device, type, in);
  405. ohmd_unlock_mutex(device->ctx->update_mutex);
  406. return ret;
  407. }
  408. ohmd_status OHMD_APIENTRY ohmd_device_settings_seti(ohmd_device_settings* settings, ohmd_int_settings key, const int* val)
  409. {
  410. switch(key){
  411. case OHMD_IDS_AUTOMATIC_UPDATE:
  412. settings->automatic_update = val[0] == 0 ? false : true;
  413. return OHMD_S_OK;
  414. default:
  415. return OHMD_S_INVALID_PARAMETER;
  416. }
  417. }
  418. ohmd_device_settings* OHMD_APIENTRY ohmd_device_settings_create(ohmd_context* ctx)
  419. {
  420. return ohmd_alloc(ctx, sizeof(ohmd_device_settings));
  421. }
  422. void OHMD_APIENTRY ohmd_device_settings_destroy(ohmd_device_settings* settings)
  423. {
  424. free(settings);
  425. }
  426. void* ohmd_allocfn(ohmd_context* ctx, const char* e_msg, size_t size)
  427. {
  428. void* ret = calloc(1, size);
  429. if(!ret)
  430. ohmd_set_error(ctx, "%s", e_msg);
  431. return ret;
  432. }
  433. void ohmd_set_default_device_properties(ohmd_device_properties* props)
  434. {
  435. props->ipd = 0.061f;
  436. props->znear = 0.1f;
  437. props->zfar = 1000.0f;
  438. ohmd_set_universal_distortion_k(props, 0, 0, 0, 1);
  439. ohmd_set_universal_aberration_k(props, 1.0, 1.0, 1.0);
  440. }
  441. void ohmd_calc_default_proj_matrices(ohmd_device_properties* props)
  442. {
  443. mat4x4f proj_base; // base projection matrix
  444. // Calculate where the lens is on each screen,
  445. // and with the given value offset the projection matrix.
  446. float screen_center = props->hsize / 4.0f;
  447. float lens_shift = screen_center - props->lens_sep / 2.0f;
  448. // XXX: on CV1, props->hsize > props->lens_sep / 2.0,
  449. // I am not sure about the implications, but just taking the absolute
  450. // value of the offset seems to work.
  451. float proj_offset = fabs(4.0f * lens_shift / props->hsize);
  452. // Setup the base projection matrix. Each eye mostly have the
  453. // same projection matrix with the exception of the offset.
  454. omat4x4f_init_perspective(&proj_base, props->fov, props->ratio, props->znear, props->zfar);
  455. // Setup the two adjusted projection matrices. Each is setup to deal
  456. // with the fact that the lens is not in the center of the screen.
  457. // These matrices only change of the hardware changes, so static.
  458. mat4x4f translate;
  459. omat4x4f_init_translate(&translate, proj_offset, 0, 0);
  460. omat4x4f_mult(&translate, &proj_base, &props->proj_left);
  461. omat4x4f_init_translate(&translate, -proj_offset, 0, 0);
  462. omat4x4f_mult(&translate, &proj_base, &props->proj_right);
  463. }
  464. void ohmd_set_universal_distortion_k(ohmd_device_properties* props, float a, float b, float c, float d)
  465. {
  466. props->universal_distortion_k[0] = a;
  467. props->universal_distortion_k[1] = b;
  468. props->universal_distortion_k[2] = c;
  469. props->universal_distortion_k[3] = d;
  470. }
  471. void ohmd_set_universal_aberration_k(ohmd_device_properties* props, float r, float g, float b)
  472. {
  473. props->universal_aberration_k[0] = r;
  474. props->universal_aberration_k[1] = g;
  475. props->universal_aberration_k[2] = b;
  476. }
  477. uint64_t ohmd_monotonic_per_sec(ohmd_context* ctx)
  478. {
  479. return ctx->monotonic_ticks_per_sec;
  480. }
  481. /*
  482. * Grabbed from druntime, good thing it's BOOST v1.0 as well.
  483. */
  484. uint64_t ohmd_monotonic_conv(uint64_t ticks, uint64_t srcTicksPerSecond, uint64_t dstTicksPerSecond)
  485. {
  486. // This would be more straightforward with floating point arithmetic,
  487. // but we avoid it here in order to avoid the rounding errors that that
  488. // introduces. Also, by splitting out the units in this way, we're able
  489. // to deal with much larger values before running into problems with
  490. // integer overflow.
  491. return ticks / srcTicksPerSecond * dstTicksPerSecond +
  492. ticks % srcTicksPerSecond * dstTicksPerSecond / srcTicksPerSecond;
  493. }