hmd.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * Copyright 2011-2017 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  4. */
  5. #include "hmd.h"
  6. namespace bgfx
  7. {
  8. VR::VR()
  9. : m_impl(NULL)
  10. , m_framesUntilReconnect(0)
  11. , m_enabled(false)
  12. {
  13. }
  14. void VR::init(VRImplI* _impl)
  15. {
  16. if (NULL == _impl)
  17. {
  18. return;
  19. }
  20. if (!_impl->init() )
  21. {
  22. return;
  23. }
  24. m_impl = _impl;
  25. m_impl->connect(&m_desc);
  26. if (!m_impl->isConnected() )
  27. {
  28. connectFailed();
  29. return;
  30. }
  31. m_hmdSize.m_w = m_desc.m_eyeSize[0].m_w + m_desc.m_eyeSize[1].m_w;
  32. m_hmdSize.m_h = bx::uint32_max(m_desc.m_eyeSize[0].m_h, m_desc.m_eyeSize[1].m_h);
  33. }
  34. void VR::shutdown()
  35. {
  36. if (NULL == m_impl)
  37. {
  38. return;
  39. }
  40. m_impl->destroySwapChain();
  41. if (m_impl->isConnected() )
  42. {
  43. m_impl->disconnect();
  44. }
  45. m_impl->shutdown();
  46. m_impl = NULL;
  47. m_enabled = false;
  48. }
  49. void VR::getViewport(uint8_t _eye, Rect* _viewport) const
  50. {
  51. _viewport->m_x = uint16_t(_eye * (m_desc.m_eyeSize[_eye].m_w + 1) );
  52. _viewport->m_y = 0;
  53. _viewport->m_width = uint16_t(m_desc.m_eyeSize[_eye].m_w);
  54. _viewport->m_height = uint16_t(m_desc.m_eyeSize[_eye].m_h);
  55. }
  56. void VR::makeRenderTargetActive()
  57. {
  58. BX_CHECK(m_enabled, "VR::renderEyeStart called while not enabled - render usage error");
  59. if (NULL != m_impl)
  60. {
  61. m_impl->makeRenderTargetActive(m_desc);
  62. }
  63. }
  64. void VR::recenter()
  65. {
  66. if (NULL != m_impl)
  67. {
  68. m_impl->recenter();
  69. }
  70. }
  71. void VR::preReset()
  72. {
  73. if (NULL != m_impl)
  74. {
  75. m_impl->destroyMirror();
  76. }
  77. m_enabled = false;
  78. }
  79. void VR::postReset(int _msaaSamples, int _mirrorWidth, int _mirrorHeight)
  80. {
  81. if (NULL != m_impl
  82. && m_impl->createSwapChain(m_desc, _msaaSamples, _mirrorWidth, _mirrorHeight) )
  83. {
  84. m_enabled = true;
  85. }
  86. }
  87. void VR::flip()
  88. {
  89. if (NULL == m_impl
  90. || !m_enabled)
  91. {
  92. return;
  93. }
  94. else if (!m_impl->isConnected()
  95. && !tryReconnect() )
  96. {
  97. return;
  98. }
  99. if (!m_impl->submitSwapChain(m_desc) )
  100. {
  101. m_impl->destroySwapChain();
  102. m_impl->disconnect();
  103. return;
  104. }
  105. }
  106. void VR::swap(HMD& _hmd)
  107. {
  108. _hmd.flags = BGFX_HMD_NONE;
  109. if (NULL == m_impl)
  110. {
  111. return;
  112. }
  113. _hmd.flags = BGFX_HMD_DEVICE_RESOLUTION;
  114. _hmd.deviceWidth = m_desc.m_deviceSize.m_w;
  115. _hmd.deviceHeight = m_desc.m_deviceSize.m_h;
  116. _hmd.width = uint16_t(m_hmdSize.m_w);
  117. _hmd.height = uint16_t(m_hmdSize.m_h);
  118. if (!m_impl->updateTracking(_hmd) )
  119. {
  120. m_impl->destroySwapChain();
  121. m_impl->disconnect();
  122. }
  123. if (!m_impl->isConnected() )
  124. {
  125. return;
  126. }
  127. for (int eye = 0; eye < 2; ++eye)
  128. {
  129. _hmd.eye[eye].fov[0] = m_desc.m_eyeFov[eye].m_up;
  130. _hmd.eye[eye].fov[1] = m_desc.m_eyeFov[eye].m_down;
  131. _hmd.eye[eye].fov[2] = m_desc.m_eyeFov[eye].m_left;
  132. _hmd.eye[eye].fov[3] = m_desc.m_eyeFov[eye].m_right;
  133. }
  134. m_impl->updateInput(_hmd);
  135. if (m_enabled)
  136. {
  137. _hmd.flags |= BGFX_HMD_RENDERING;
  138. }
  139. }
  140. bool VR::tryReconnect()
  141. {
  142. if (!m_impl)
  143. {
  144. return false;
  145. }
  146. BX_CHECK(!m_impl->isConnected(), "VR::tryReconnect called when already connected. Usage error");
  147. --m_framesUntilReconnect;
  148. if (m_framesUntilReconnect > 0)
  149. {
  150. return false;
  151. }
  152. m_framesUntilReconnect = 90;
  153. m_impl->connect(&m_desc);
  154. if (!m_impl->isConnected() )
  155. {
  156. connectFailed();
  157. return false;
  158. }
  159. m_hmdSize.m_w = m_desc.m_eyeSize[0].m_w + m_desc.m_eyeSize[1].m_w;
  160. m_hmdSize.m_h = bx::uint32_max(m_desc.m_eyeSize[0].m_h, m_desc.m_eyeSize[1].m_h);
  161. return true;
  162. }
  163. void VR::connectFailed()
  164. {
  165. // sane defaults
  166. m_desc.m_deviceSize.m_w = 2160;
  167. m_desc.m_deviceSize.m_h = 1200;
  168. m_desc.m_deviceType = 0;
  169. m_desc.m_refreshRate = 90.0f;
  170. m_desc.m_neckOffset[0] = 0.0805f;
  171. m_desc.m_neckOffset[1] = 0.075f;
  172. for (int eye = 0; eye < 2; ++eye)
  173. {
  174. m_desc.m_eyeFov[eye].m_up = 1.32928634f;
  175. m_desc.m_eyeFov[eye].m_down = 1.32928634f;
  176. }
  177. m_desc.m_eyeFov[0].m_left = 1.05865765f;
  178. m_desc.m_eyeFov[0].m_right = 1.09236801f;
  179. m_desc.m_eyeFov[1].m_left = 1.09236801f;
  180. m_desc.m_eyeFov[1].m_right = 1.05865765f;
  181. }
  182. } // namesapce bgfx