openhmd.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  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_unlock_mutex(ctx->update_mutex);
  174. return NULL;
  175. }
  176. device->rotation_correction.w = 1;
  177. device->settings = *settings;
  178. device->ctx = ctx;
  179. device->active_device_idx = ctx->num_active_devices;
  180. ctx->active_devices[ctx->num_active_devices++] = device;
  181. ohmd_unlock_mutex(ctx->update_mutex);
  182. if(device->settings.automatic_update)
  183. ohmd_set_up_update_thread(ctx);
  184. return device;
  185. }
  186. ohmd_unlock_mutex(ctx->update_mutex);
  187. ohmd_set_error(ctx, "no device with index: %d", index);
  188. return NULL;
  189. }
  190. ohmd_device* OHMD_APIENTRY ohmd_list_open_device(ohmd_context* ctx, int index)
  191. {
  192. ohmd_device_settings settings;
  193. settings.automatic_update = true;
  194. return ohmd_list_open_device_s(ctx, index, &settings);
  195. }
  196. int OHMD_APIENTRY ohmd_close_device(ohmd_device* device)
  197. {
  198. ohmd_lock_mutex(device->ctx->update_mutex);
  199. ohmd_context* ctx = device->ctx;
  200. int idx = device->active_device_idx;
  201. memmove(ctx->active_devices + idx, ctx->active_devices + idx + 1,
  202. sizeof(ohmd_device*) * (ctx->num_active_devices - idx - 1));
  203. device->close(device);
  204. ctx->num_active_devices--;
  205. for(int i = idx; i < ctx->num_active_devices; i++)
  206. ctx->active_devices[i]->active_device_idx--;
  207. ohmd_unlock_mutex(ctx->update_mutex);
  208. return OHMD_S_OK;
  209. }
  210. static int ohmd_device_getf_unp(ohmd_device* device, ohmd_float_value type, float* out)
  211. {
  212. switch(type){
  213. case OHMD_LEFT_EYE_GL_MODELVIEW_MATRIX: {
  214. vec3f point = {{0, 0, 0}};
  215. quatf rot = device->rotation;
  216. quatf tmp = device->rotation_correction;
  217. oquatf_mult_me(&tmp, &rot);
  218. rot = tmp;
  219. mat4x4f orient, world_shift, result;
  220. omat4x4f_init_look_at(&orient, &rot, &point);
  221. omat4x4f_init_translate(&world_shift, -device->position.x +(device->properties.ipd / 2.0f), -device->position.y, -device->position.z);
  222. omat4x4f_mult(&world_shift, &orient, &result);
  223. omat4x4f_transpose(&result, (mat4x4f*)out);
  224. return OHMD_S_OK;
  225. }
  226. case OHMD_RIGHT_EYE_GL_MODELVIEW_MATRIX: {
  227. vec3f point = {{0, 0, 0}};
  228. quatf rot = device->rotation;
  229. oquatf_mult_me(&rot, &device->rotation_correction);
  230. mat4x4f orient, world_shift, result;
  231. omat4x4f_init_look_at(&orient, &rot, &point);
  232. omat4x4f_init_translate(&world_shift, -device->position.x + -(device->properties.ipd / 2.0f), -device->position.y, -device->position.z);
  233. omat4x4f_mult(&world_shift, &orient, &result);
  234. omat4x4f_transpose(&result, (mat4x4f*)out);
  235. return OHMD_S_OK;
  236. }
  237. case OHMD_LEFT_EYE_GL_PROJECTION_MATRIX:
  238. omat4x4f_transpose(&device->properties.proj_left, (mat4x4f*)out);
  239. return OHMD_S_OK;
  240. case OHMD_RIGHT_EYE_GL_PROJECTION_MATRIX:
  241. omat4x4f_transpose(&device->properties.proj_right, (mat4x4f*)out);
  242. return OHMD_S_OK;
  243. case OHMD_SCREEN_HORIZONTAL_SIZE:
  244. *out = device->properties.hsize;
  245. return OHMD_S_OK;
  246. case OHMD_SCREEN_VERTICAL_SIZE:
  247. *out = device->properties.vsize;
  248. return OHMD_S_OK;
  249. case OHMD_LENS_HORIZONTAL_SEPARATION:
  250. *out = device->properties.lens_sep;
  251. return OHMD_S_OK;
  252. case OHMD_LENS_VERTICAL_POSITION:
  253. *out = device->properties.lens_vpos;
  254. return OHMD_S_OK;
  255. case OHMD_RIGHT_EYE_FOV:
  256. case OHMD_LEFT_EYE_FOV:
  257. *out = device->properties.fov;
  258. return OHMD_S_OK;
  259. case OHMD_RIGHT_EYE_ASPECT_RATIO:
  260. case OHMD_LEFT_EYE_ASPECT_RATIO:
  261. *out = device->properties.ratio;
  262. return OHMD_S_OK;
  263. case OHMD_EYE_IPD:
  264. *out = device->properties.ipd;
  265. return OHMD_S_OK;
  266. case OHMD_PROJECTION_ZFAR:
  267. *out = device->properties.zfar;
  268. return OHMD_S_OK;
  269. case OHMD_PROJECTION_ZNEAR:
  270. *out = device->properties.znear;
  271. return OHMD_S_OK;
  272. case OHMD_ROTATION_QUAT:
  273. {
  274. *(quatf*)out = device->rotation;
  275. oquatf_mult_me((quatf*)out, &device->rotation_correction);
  276. quatf tmp = device->rotation_correction;
  277. oquatf_mult_me(&tmp, (quatf*)out);
  278. *(quatf*)out = tmp;
  279. return OHMD_S_OK;
  280. }
  281. case OHMD_POSITION_VECTOR:
  282. {
  283. *(vec3f*)out = device->position;
  284. for(int i = 0; i < 3; i++)
  285. out[i] += device->position_correction.arr[i];
  286. return OHMD_S_OK;
  287. }
  288. case OHMD_UNIVERSAL_DISTORTION_K: {
  289. for (int i = 0; i < 4; i++) {
  290. out[i] = device->properties.universal_distortion_k[i];
  291. }
  292. return OHMD_S_OK;
  293. }
  294. case OHMD_UNIVERSAL_ABERRATION_K: {
  295. for (int i = 0; i < 3; i++) {
  296. out[i] = device->properties.universal_aberration_k[i];
  297. }
  298. return OHMD_S_OK;
  299. }
  300. default:
  301. return device->getf(device, type, out);
  302. }
  303. }
  304. int OHMD_APIENTRY ohmd_device_getf(ohmd_device* device, ohmd_float_value type, float* out)
  305. {
  306. ohmd_lock_mutex(device->ctx->update_mutex);
  307. int ret = ohmd_device_getf_unp(device, type, out);
  308. ohmd_unlock_mutex(device->ctx->update_mutex);
  309. return ret;
  310. }
  311. int ohmd_device_setf_unp(ohmd_device* device, ohmd_float_value type, const float* in)
  312. {
  313. switch(type){
  314. case OHMD_EYE_IPD:
  315. device->properties.ipd = *in;
  316. return OHMD_S_OK;
  317. case OHMD_PROJECTION_ZFAR:
  318. device->properties.zfar = *in;
  319. return OHMD_S_OK;
  320. case OHMD_PROJECTION_ZNEAR:
  321. device->properties.znear = *in;
  322. return OHMD_S_OK;
  323. case OHMD_ROTATION_QUAT:
  324. {
  325. // adjust rotation correction
  326. quatf q;
  327. int ret = device->getf(device, OHMD_ROTATION_QUAT, (float*)&q);
  328. if(ret != 0){
  329. return ret;
  330. }
  331. oquatf_diff(&q, (quatf*)in, &device->rotation_correction);
  332. return OHMD_S_OK;
  333. }
  334. case OHMD_POSITION_VECTOR:
  335. {
  336. // adjust position correction
  337. vec3f v;
  338. int ret = device->getf(device, OHMD_POSITION_VECTOR, (float*)&v);
  339. if(ret != 0){
  340. return ret;
  341. }
  342. for(int i = 0; i < 3; i++)
  343. device->position_correction.arr[i] = in[i] - v.arr[i];
  344. return OHMD_S_OK;
  345. }
  346. case OHMD_EXTERNAL_SENSOR_FUSION:
  347. {
  348. if(device->setf == NULL)
  349. return OHMD_S_UNSUPPORTED;
  350. return device->setf(device, type, in);
  351. }
  352. default:
  353. return OHMD_S_INVALID_PARAMETER;
  354. }
  355. }
  356. int OHMD_APIENTRY ohmd_device_setf(ohmd_device* device, ohmd_float_value type, const float* in)
  357. {
  358. ohmd_lock_mutex(device->ctx->update_mutex);
  359. int ret = ohmd_device_setf_unp(device, type, in);
  360. ohmd_unlock_mutex(device->ctx->update_mutex);
  361. return ret;
  362. }
  363. int OHMD_APIENTRY ohmd_device_geti(ohmd_device* device, ohmd_int_value type, int* out)
  364. {
  365. switch(type){
  366. case OHMD_SCREEN_HORIZONTAL_RESOLUTION:
  367. *out = device->properties.hres;
  368. return OHMD_S_OK;
  369. case OHMD_SCREEN_VERTICAL_RESOLUTION:
  370. *out = device->properties.vres;
  371. return OHMD_S_OK;
  372. case OHMD_CONTROL_COUNT:
  373. *out = device->properties.control_count;
  374. return OHMD_S_OK;
  375. case OHMD_CONTROLS_TYPES:
  376. memcpy(out, device->properties.controls_types, device->properties.control_count * sizeof(int));
  377. return OHMD_S_OK;
  378. case OHMD_CONTROLS_HINTS:
  379. memcpy(out, device->properties.controls_hints, device->properties.control_count * sizeof(int));
  380. return OHMD_S_OK;
  381. default:
  382. return OHMD_S_INVALID_PARAMETER;
  383. }
  384. }
  385. int OHMD_APIENTRY ohmd_device_seti(ohmd_device* device, ohmd_int_value type, const int* in)
  386. {
  387. switch(type){
  388. default:
  389. return OHMD_S_INVALID_PARAMETER;
  390. }
  391. }
  392. int ohmd_device_set_data_unp(ohmd_device* device, ohmd_data_value type, const void* in)
  393. {
  394. switch(type){
  395. case OHMD_DRIVER_DATA:
  396. device->set_data(device, OHMD_DRIVER_DATA, in);
  397. return OHMD_S_OK;
  398. case OHMD_DRIVER_PROPERTIES:
  399. device->set_data(device, OHMD_DRIVER_PROPERTIES, in);
  400. return OHMD_S_OK;
  401. default:
  402. return OHMD_S_INVALID_PARAMETER;
  403. }
  404. }
  405. int OHMD_APIENTRY ohmd_device_set_data(ohmd_device* device, ohmd_data_value type, const void* in)
  406. {
  407. ohmd_lock_mutex(device->ctx->update_mutex);
  408. int ret = ohmd_device_set_data_unp(device, type, in);
  409. ohmd_unlock_mutex(device->ctx->update_mutex);
  410. return ret;
  411. }
  412. ohmd_status OHMD_APIENTRY ohmd_device_settings_seti(ohmd_device_settings* settings, ohmd_int_settings key, const int* val)
  413. {
  414. switch(key){
  415. case OHMD_IDS_AUTOMATIC_UPDATE:
  416. settings->automatic_update = val[0] == 0 ? false : true;
  417. return OHMD_S_OK;
  418. default:
  419. return OHMD_S_INVALID_PARAMETER;
  420. }
  421. }
  422. ohmd_device_settings* OHMD_APIENTRY ohmd_device_settings_create(ohmd_context* ctx)
  423. {
  424. return ohmd_alloc(ctx, sizeof(ohmd_device_settings));
  425. }
  426. void OHMD_APIENTRY ohmd_device_settings_destroy(ohmd_device_settings* settings)
  427. {
  428. free(settings);
  429. }
  430. void* ohmd_allocfn(ohmd_context* ctx, const char* e_msg, size_t size)
  431. {
  432. void* ret = calloc(1, size);
  433. if(!ret)
  434. ohmd_set_error(ctx, "%s", e_msg);
  435. return ret;
  436. }
  437. void ohmd_set_default_device_properties(ohmd_device_properties* props)
  438. {
  439. props->ipd = 0.061f;
  440. props->znear = 0.1f;
  441. props->zfar = 1000.0f;
  442. ohmd_set_universal_distortion_k(props, 0, 0, 0, 1);
  443. ohmd_set_universal_aberration_k(props, 1.0, 1.0, 1.0);
  444. }
  445. void ohmd_calc_default_proj_matrices(ohmd_device_properties* props)
  446. {
  447. mat4x4f proj_base; // base projection matrix
  448. // Calculate where the lens is on each screen,
  449. // and with the given value offset the projection matrix.
  450. float screen_center = props->hsize / 4.0f;
  451. float lens_shift = screen_center - props->lens_sep / 2.0f;
  452. // XXX: on CV1, props->hsize > props->lens_sep / 2.0,
  453. // I am not sure about the implications, but just taking the absolute
  454. // value of the offset seems to work.
  455. float proj_offset = fabs(4.0f * lens_shift / props->hsize);
  456. // Setup the base projection matrix. Each eye mostly have the
  457. // same projection matrix with the exception of the offset.
  458. omat4x4f_init_perspective(&proj_base, props->fov, props->ratio, props->znear, props->zfar);
  459. // Setup the two adjusted projection matrices. Each is setup to deal
  460. // with the fact that the lens is not in the center of the screen.
  461. // These matrices only change of the hardware changes, so static.
  462. mat4x4f translate;
  463. omat4x4f_init_translate(&translate, proj_offset, 0, 0);
  464. omat4x4f_mult(&translate, &proj_base, &props->proj_left);
  465. omat4x4f_init_translate(&translate, -proj_offset, 0, 0);
  466. omat4x4f_mult(&translate, &proj_base, &props->proj_right);
  467. }
  468. void ohmd_set_universal_distortion_k(ohmd_device_properties* props, float a, float b, float c, float d)
  469. {
  470. props->universal_distortion_k[0] = a;
  471. props->universal_distortion_k[1] = b;
  472. props->universal_distortion_k[2] = c;
  473. props->universal_distortion_k[3] = d;
  474. }
  475. void ohmd_set_universal_aberration_k(ohmd_device_properties* props, float r, float g, float b)
  476. {
  477. props->universal_aberration_k[0] = r;
  478. props->universal_aberration_k[1] = g;
  479. props->universal_aberration_k[2] = b;
  480. }
  481. uint64_t ohmd_monotonic_per_sec(ohmd_context* ctx)
  482. {
  483. return ctx->monotonic_ticks_per_sec;
  484. }
  485. /*
  486. * Grabbed from druntime, good thing it's BOOST v1.0 as well.
  487. */
  488. uint64_t ohmd_monotonic_conv(uint64_t ticks, uint64_t srcTicksPerSecond, uint64_t dstTicksPerSecond)
  489. {
  490. // This would be more straightforward with floating point arithmetic,
  491. // but we avoid it here in order to avoid the rounding errors that that
  492. // introduces. Also, by splitting out the units in this way, we're able
  493. // to deal with much larger values before running into problems with
  494. // integer overflow.
  495. return ticks / srcTicksPerSecond * dstTicksPerSecond +
  496. ticks % srcTicksPerSecond * dstTicksPerSecond / srcTicksPerSecond;
  497. }