oculusVRHMDDevice.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/input/oculusVR/oculusVRHMDDevice.h"
  23. #include "platform/input/oculusVR/oculusVRDevice.h"
  24. #include "platform/input/oculusVR/oculusVRSensorDevice.h"
  25. #include "postFx/postEffectCommon.h"
  26. #include "gui/core/guiCanvas.h"
  27. #include "platform/input/oculusVR/oculusVRUtil.h"
  28. #include "gfx/D3D9/gfxD3D9Device.h"
  29. // Use D3D9 for win32
  30. #ifdef TORQUE_OS_WIN
  31. #define OVR_D3D_VERSION 9
  32. #include "OVR_CAPI_D3D.h"
  33. #define OCULUS_USE_D3D
  34. #else
  35. #include "OVR_CAPI_GL.h"
  36. #define OCULUS_USE_GL
  37. #endif
  38. extern GFXTextureObject *gLastStereoTexture;
  39. OculusVRHMDDevice::OculusVRHMDDevice() :
  40. mWindowSize(1280,800)
  41. {
  42. mIsValid = false;
  43. mDevice = NULL;
  44. mSupportedDistortionCaps = 0;
  45. mCurrentDistortionCaps = 0;
  46. mCurrentCaps = 0;
  47. mSupportedCaps = 0;
  48. mVsync = true;
  49. mTimewarp = true;
  50. mRenderConfigurationDirty = true;
  51. mCurrentPixelDensity = OculusVRDevice::smDesiredPixelDensity;
  52. mDesiredRenderingMode = GFXDevice::RS_StereoSideBySide;
  53. mRTFormat = GFXFormatR8G8B8A8;
  54. mDrawCanvas = NULL;
  55. mFrameReady = false;
  56. mConnection = NULL;
  57. mSensor = NULL;
  58. mActionCodeIndex = 0;
  59. }
  60. OculusVRHMDDevice::~OculusVRHMDDevice()
  61. {
  62. cleanUp();
  63. }
  64. void OculusVRHMDDevice::cleanUp()
  65. {
  66. onDeviceDestroy();
  67. if (mSensor)
  68. {
  69. delete mSensor;
  70. mSensor = NULL;
  71. }
  72. if(mDevice)
  73. {
  74. ovrHmd_Destroy(mDevice);
  75. mDevice = NULL;
  76. }
  77. mIsValid = false;
  78. }
  79. void OculusVRHMDDevice::set(ovrHmd hmd, U32 actionCodeIndex)
  80. {
  81. cleanUp();
  82. mIsValid = false;
  83. mRenderConfigurationDirty = true;
  84. mDevice = hmd;
  85. mSupportedCaps = hmd->HmdCaps;
  86. mCurrentCaps = mSupportedCaps & (ovrHmdCap_DynamicPrediction | ovrHmdCap_LowPersistence | (!mVsync ? ovrHmdCap_NoVSync : 0));
  87. mSupportedDistortionCaps = hmd->DistortionCaps;
  88. mCurrentDistortionCaps = mSupportedDistortionCaps & (ovrDistortionCap_TimeWarp | ovrDistortionCap_Vignette | ovrDistortionCap_Overdrive);
  89. mTimewarp = mSupportedDistortionCaps & ovrDistortionCap_TimeWarp;
  90. // DeviceInfo
  91. mProductName = hmd->ProductName;
  92. mManufacturer = hmd->Manufacturer;
  93. mVersion = hmd->FirmwareMajor;
  94. mDisplayDeviceName = hmd->DisplayDeviceName;
  95. mDisplayId = hmd->DisplayId;
  96. mDesktopPosition.x = hmd->WindowsPos.x;
  97. mDesktopPosition.y = hmd->WindowsPos.y;
  98. mResolution.x = hmd->Resolution.w;
  99. mResolution.y = hmd->Resolution.h;
  100. mProfileInterpupillaryDistance = ovrHmd_GetFloat(hmd, OVR_KEY_IPD, OVR_DEFAULT_IPD);
  101. mLensSeparation = ovrHmd_GetFloat(hmd, "LensSeparation", 0);
  102. ovrHmd_GetFloatArray(hmd, "ScreenSize", &mScreenSize.x, 2);
  103. dMemcpy(mCurrentFovPorts, mDevice->DefaultEyeFov, sizeof(mDevice->DefaultEyeFov));
  104. for (U32 i=0; i<2; i++)
  105. {
  106. mCurrentFovPorts[i].UpTan = mDevice->DefaultEyeFov[i].UpTan;
  107. mCurrentFovPorts[i].DownTan = mDevice->DefaultEyeFov[i].DownTan;
  108. mCurrentFovPorts[i].LeftTan = mDevice->DefaultEyeFov[i].LeftTan;
  109. mCurrentFovPorts[i].RightTan = mDevice->DefaultEyeFov[i].RightTan;
  110. }
  111. if (mDevice->HmdCaps & ovrHmdCap_ExtendDesktop)
  112. {
  113. mWindowSize = Point2I(mDevice->Resolution.w, mDevice->Resolution.h);
  114. }
  115. else
  116. {
  117. mWindowSize = Point2I(1100, 618);
  118. }
  119. mActionCodeIndex = actionCodeIndex;
  120. mIsValid = true;
  121. mSensor = new OculusVRSensorDevice();
  122. mSensor->set(mDevice, mActionCodeIndex);
  123. updateCaps();
  124. }
  125. void OculusVRHMDDevice::setIPD(F32 ipd)
  126. {
  127. mInterpupillaryDistance = ipd;
  128. }
  129. void OculusVRHMDDevice::setOptimalDisplaySize(GuiCanvas *canvas)
  130. {
  131. if (!mDevice)
  132. return;
  133. PlatformWindow *window = canvas->getPlatformWindow();
  134. GFXTarget *target = window->getGFXTarget();
  135. if (target && target->getSize() != mWindowSize)
  136. {
  137. GFXVideoMode newMode;
  138. newMode.antialiasLevel = 0;
  139. newMode.bitDepth = 32;
  140. newMode.fullScreen = false;
  141. newMode.refreshRate = 75;
  142. newMode.resolution = mWindowSize;
  143. newMode.wideScreen = false;
  144. window->setVideoMode(newMode);
  145. //AssertFatal(window->getClientExtent().x == mWindowSize[0] && window->getClientExtent().y == mWindowSize[1], "Window didn't resize to correct dimensions");
  146. }
  147. // Need to move window over to the rift side of the desktop
  148. if (mDevice->HmdCaps & ovrHmdCap_ExtendDesktop && !OculusVRDevice::smWindowDebug)
  149. {
  150. #ifndef OCULUS_WINDOW_DEBUG
  151. window->setPosition(getDesktopPosition());
  152. #endif
  153. }
  154. }
  155. bool OculusVRHMDDevice::isDisplayingWarning()
  156. {
  157. if (!mIsValid || !mDevice)
  158. return false;
  159. ovrHSWDisplayState displayState;
  160. ovrHmd_GetHSWDisplayState(mDevice, &displayState);
  161. return displayState.Displayed;
  162. }
  163. void OculusVRHMDDevice::dismissWarning()
  164. {
  165. if (!mIsValid || !mDevice)
  166. return;
  167. ovrHmd_DismissHSWDisplay(mDevice);
  168. }
  169. bool OculusVRHMDDevice::setupTargets()
  170. {
  171. ovrFovPort eyeFov[2] = {mDevice->DefaultEyeFov[0], mDevice->DefaultEyeFov[1]};
  172. mRecomendedEyeTargetSize[0] = ovrHmd_GetFovTextureSize(mDevice, ovrEye_Left, eyeFov[0], mCurrentPixelDensity);
  173. mRecomendedEyeTargetSize[1] = ovrHmd_GetFovTextureSize(mDevice, ovrEye_Right, eyeFov[1], mCurrentPixelDensity);
  174. // Calculate render target size
  175. if (mDesiredRenderingMode == GFXDevice::RS_StereoSideBySide)
  176. {
  177. // Setup a single texture, side-by-side viewports
  178. Point2I rtSize(
  179. mRecomendedEyeTargetSize[0].w + mRecomendedEyeTargetSize[1].w,
  180. mRecomendedEyeTargetSize[0].h > mRecomendedEyeTargetSize[1].h ? mRecomendedEyeTargetSize[0].h : mRecomendedEyeTargetSize[1].h
  181. );
  182. GFXFormat targetFormat = GFX->getActiveRenderTarget()->getFormat();
  183. mRTFormat = targetFormat;
  184. rtSize = generateRenderTarget(mStereoRT, mStereoTexture, mStereoDepthTexture, rtSize);
  185. // Left
  186. mEyeRenderSize[0] = rtSize;
  187. mEyeRT[0] = mStereoRT;
  188. mEyeTexture[0] = mStereoTexture;
  189. mEyeViewport[0] = RectI(Point2I(0,0), Point2I((mRecomendedEyeTargetSize[0].w+1)/2, mRecomendedEyeTargetSize[0].h));
  190. // Right
  191. mEyeRenderSize[1] = rtSize;
  192. mEyeRT[1] = mStereoRT;
  193. mEyeTexture[1] = mStereoTexture;
  194. mEyeViewport[1] = RectI(Point2I((mRecomendedEyeTargetSize[0].w+1)/2,0), Point2I((mRecomendedEyeTargetSize[1].w+1)/2, mRecomendedEyeTargetSize[1].h));
  195. gLastStereoTexture = mEyeTexture[0];
  196. }
  197. else
  198. {
  199. // No rendering, abort!
  200. return false;
  201. }
  202. return true;
  203. }
  204. String OculusVRHMDDevice::dumpMetrics()
  205. {
  206. StringBuilder sb;
  207. EulerF rot = mSensor->getEulerRotation();
  208. Point3F pos = mSensor->getPosition();
  209. FovPort eyeFov[2];
  210. this->getFovPorts(eyeFov);
  211. mSensor->getPositionTrackingAvailable();
  212. F32 ipd = this->getIPD();
  213. U32 lastStatus = mSensor->getLastTrackingStatus();
  214. sb.format(" | OVR Sensor %i | rot: %f %f %f, pos: %f %f %f, FOV (%f %f %f %f, %f %f %f %f), IPD %f, Track:%s%s, Disort:%s%s%s",
  215. mActionCodeIndex,
  216. rot.x, rot.y, rot.z,
  217. pos.x, pos.y, pos.z,
  218. eyeFov[0].upTan, eyeFov[0].downTan, eyeFov[0].leftTan, eyeFov[0].rightTan, eyeFov[1].upTan, eyeFov[1].downTan, eyeFov[1].leftTan, eyeFov[1].rightTan,
  219. getIPD(),
  220. lastStatus & ovrStatus_OrientationTracked ? " ORIENT" : "",
  221. lastStatus & ovrStatus_PositionTracked ? " POS" : "",
  222. mCurrentDistortionCaps & ovrDistortionCap_TimeWarp ? " TIMEWARP" : "",
  223. mCurrentDistortionCaps & ovrDistortionCap_Vignette ? " VIGNETTE" : "",
  224. mCurrentDistortionCaps & ovrDistortionCap_Overdrive ? " OVERDRIVE" : "");
  225. return sb.data();
  226. }
  227. void OculusVRHMDDevice::updateRenderInfo()
  228. {
  229. // Check console values first
  230. if (mCurrentPixelDensity != OculusVRDevice::smDesiredPixelDensity)
  231. {
  232. mRenderConfigurationDirty = true;
  233. mCurrentPixelDensity = OculusVRDevice::smDesiredPixelDensity;
  234. }
  235. if (!mIsValid || !mDevice || !mRenderConfigurationDirty)
  236. return;
  237. if (!mDrawCanvas)
  238. return;
  239. PlatformWindow *window = mDrawCanvas->getPlatformWindow();
  240. ovrFovPort eyeFov[2] = {mDevice->DefaultEyeFov[0], mDevice->DefaultEyeFov[1]};
  241. // Update window size if it's incorrect
  242. Point2I backbufferSize = mDrawCanvas->getBounds().extent;
  243. // Reset
  244. ovrHmd_ConfigureRendering(mDevice, NULL, 0, NULL, NULL);
  245. #ifdef OCULUS_USE_D3D
  246. // Generate render target textures
  247. GFXD3D9Device *d3d9GFX = dynamic_cast<GFXD3D9Device*>(GFX);
  248. if (d3d9GFX)
  249. {
  250. ovrD3D9Config cfg;
  251. cfg.D3D9.Header.API = ovrRenderAPI_D3D9;
  252. cfg.D3D9.Header.Multisample = 0;
  253. cfg.D3D9.Header.BackBufferSize = OVR::Sizei(backbufferSize.x, backbufferSize.y);
  254. cfg.D3D9.pDevice = d3d9GFX->getDevice();
  255. cfg.D3D9.pDevice->GetSwapChain(0, &cfg.D3D9.pSwapChain);
  256. // Finally setup!
  257. if (!setupTargets())
  258. {
  259. onDeviceDestroy();
  260. return;
  261. }
  262. ovrHmd_AttachToWindow(mDevice, window->getPlatformDrawable(), NULL, NULL);
  263. if (!ovrHmd_ConfigureRendering( mDevice, &cfg.Config, mCurrentDistortionCaps, eyeFov, mEyeRenderDesc ))
  264. {
  265. Con::errorf("Couldn't configure oculus rendering!");
  266. return;
  267. }
  268. }
  269. #endif
  270. #ifdef OCULUS_USE_GL
  271. // Generate render target textures
  272. GFXGLDevice *glGFX = dynamic_cast<GFXGLDevice*>(GFX);
  273. if (glGFX)
  274. {
  275. ovrGLConfig cfg;
  276. cfg.OGL.Header.API = ovrRenderAPI_OpenGL;
  277. cfg.OGL.Header.Multisample = 0;
  278. cfg.OGL.Header.BackBufferSize = OVR::Sizei(backbufferSize.x, backbufferSize.y);
  279. #ifdef WIN32
  280. cfg.OGL.Window = GetActiveWindow();//window->getPlatformDrawable();
  281. cfg.OGL.DC = wglGetCurrentDC();
  282. #else
  283. cfg.OGL.Disp = NULL;
  284. #endif
  285. // Finally setup!
  286. if (!setupTargets())
  287. {
  288. onDeviceDestroy();
  289. return;
  290. }
  291. ovrHmd_AttachToWindow(mDevice, window->getPlatformDrawable(), NULL, NULL);
  292. if (!ovrHmd_ConfigureRendering( mDevice, &cfg.Config, mCurrentDistortionCaps, eyeFov, mEyeRenderDesc ))
  293. {
  294. Con::errorf("Couldn't configure oculus rendering!");
  295. return;
  296. }
  297. }
  298. #endif
  299. mRenderConfigurationDirty = false;
  300. }
  301. Point2I OculusVRHMDDevice::generateRenderTarget(GFXTextureTargetRef &target, GFXTexHandle &texture, GFXTexHandle &depth, Point2I desiredSize)
  302. {
  303. // Texture size that we already have might be big enough.
  304. Point2I newRTSize;
  305. bool newRT = false;
  306. if (!target.getPointer())
  307. {
  308. target = GFX->allocRenderToTextureTarget();
  309. newRTSize = desiredSize;
  310. newRT = true;
  311. }
  312. else
  313. {
  314. Point2I currentSize = target->getSize();
  315. newRTSize = currentSize;
  316. }
  317. // %50 linear growth each time is a nice balance between being too greedy
  318. // for a 2D surface and too slow to prevent fragmentation.
  319. while ( newRTSize.x < desiredSize.x )
  320. {
  321. newRTSize.x += newRTSize.x/2;
  322. }
  323. while ( newRTSize.y < desiredSize.y )
  324. {
  325. newRTSize.y += newRTSize.y/2;
  326. }
  327. // Put some sane limits on it. 4k x 4k is fine for most modern video cards.
  328. // Nobody should be messing around with surfaces smaller than 4k pixels these days.
  329. newRTSize.setMin(Point2I(4096, 4096));
  330. newRTSize.setMax(Point2I(64, 64));
  331. // Stereo RT needs to be the same size as the recommended RT
  332. if ( newRT || texture.getWidthHeight() != newRTSize )
  333. {
  334. texture.set( newRTSize.x, newRTSize.y, mRTFormat, &VRTextureProfile, avar( "%s() - (line %d)", __FUNCTION__, __LINE__ ) );
  335. target->attachTexture( GFXTextureTarget::Color0, texture );
  336. Con::printf("generateRenderTarget generated %x", texture.getPointer());
  337. }
  338. if ( depth.getWidthHeight() != newRTSize )
  339. {
  340. depth.set( newRTSize.x, newRTSize.y, GFXFormatD24S8, &VRDepthProfile, avar( "%s() - (line %d)", __FUNCTION__, __LINE__ ) );
  341. target->attachTexture( GFXTextureTarget::DepthStencil, depth );
  342. Con::printf("generateRenderTarget generated depth %x", depth.getPointer());
  343. }
  344. return newRTSize;
  345. }
  346. void OculusVRHMDDevice::clearRenderTargets()
  347. {
  348. mStereoRT = NULL;
  349. mEyeRT[0] = NULL;
  350. mEyeRT[1] = NULL;
  351. }
  352. void OculusVRHMDDevice::updateCaps()
  353. {
  354. if (!mIsValid || !mDevice)
  355. return;
  356. U32 oldDistortionCaps = mCurrentDistortionCaps;
  357. // Distortion
  358. if (mTimewarp)
  359. {
  360. mCurrentDistortionCaps |= ovrDistortionCap_TimeWarp;
  361. }
  362. else
  363. {
  364. mCurrentDistortionCaps &= ~ovrDistortionCap_TimeWarp;
  365. }
  366. if (oldDistortionCaps != mCurrentDistortionCaps)
  367. {
  368. mRenderConfigurationDirty = true;
  369. }
  370. // Device
  371. if (!mVsync)
  372. {
  373. mCurrentCaps |= ovrHmdCap_NoVSync;
  374. }
  375. else
  376. {
  377. mCurrentCaps &= ~ovrHmdCap_NoVSync;
  378. }
  379. ovrHmd_SetEnabledCaps(mDevice, mCurrentCaps);
  380. }
  381. static bool sInFrame = false; // protects against recursive onStartFrame calls
  382. void OculusVRHMDDevice::onStartFrame()
  383. {
  384. if (!mIsValid || !mDevice || !mDrawCanvas || sInFrame || mFrameReady)
  385. return;
  386. sInFrame = true;
  387. #ifndef OCULUS_DEBUG_FRAME
  388. ovrHmd_BeginFrame(mDevice, 0);
  389. #endif
  390. ovrVector3f hmdToEyeViewOffset[2] = { mEyeRenderDesc[0].HmdToEyeViewOffset, mEyeRenderDesc[1].HmdToEyeViewOffset };
  391. ovrHmd_GetEyePoses(mDevice, 0, hmdToEyeViewOffset, mCurrentEyePoses, &mLastTrackingState);
  392. for (U32 i=0; i<2; i++)
  393. {
  394. mCurrentEyePoses[i].Position.x *= OculusVRDevice::smPositionTrackingScale;
  395. mCurrentEyePoses[i].Position.y *= OculusVRDevice::smPositionTrackingScale;
  396. mCurrentEyePoses[i].Position.z *= OculusVRDevice::smPositionTrackingScale;
  397. }
  398. sInFrame = false;
  399. mFrameReady = true;
  400. }
  401. void OculusVRHMDDevice::onEndFrame()
  402. {
  403. if (!mIsValid || !mDevice || !mDrawCanvas || sInFrame || !mFrameReady)
  404. return;
  405. Point2I eyeSize;
  406. GFXTarget *windowTarget = mDrawCanvas->getPlatformWindow()->getGFXTarget();
  407. #ifndef OCULUS_DEBUG_FRAME
  408. #ifdef OCULUS_USE_D3D
  409. GFXD3D9Device *d3d9GFX = dynamic_cast<GFXD3D9Device*>(GFX);
  410. if (d3d9GFX && mEyeRT[0].getPointer())
  411. {
  412. // Left
  413. ovrD3D9Texture eyeTextures[2];
  414. eyeSize = mEyeTexture[0].getWidthHeight();
  415. eyeTextures[0].D3D9.Header.API = ovrRenderAPI_D3D9;
  416. eyeTextures[0].D3D9.Header.RenderViewport.Pos.x = mEyeViewport[0].point.x;
  417. eyeTextures[0].D3D9.Header.RenderViewport.Pos.y = mEyeViewport[0].point.y;
  418. eyeTextures[0].D3D9.Header.RenderViewport.Size.w = mEyeViewport[0].extent.x;
  419. eyeTextures[0].D3D9.Header.RenderViewport.Size.h = mEyeViewport[0].extent.y;
  420. eyeTextures[0].D3D9.Header.TextureSize.w = eyeSize.x;
  421. eyeTextures[0].D3D9.Header.TextureSize.h = eyeSize.y;
  422. eyeTextures[0].D3D9.pTexture = mEyeRT[0].getPointer() ? static_cast<GFXD3D9TextureObject*>(mEyeTexture[0].getPointer())->get2DTex() : NULL;
  423. // Right
  424. eyeSize = mEyeTexture[1].getWidthHeight();
  425. eyeTextures[1].D3D9.Header.API = ovrRenderAPI_D3D9;
  426. eyeTextures[1].D3D9.Header.RenderViewport.Pos.x = mEyeViewport[1].point.x;
  427. eyeTextures[1].D3D9.Header.RenderViewport.Pos.y = mEyeViewport[1].point.y;
  428. eyeTextures[1].D3D9.Header.RenderViewport.Size.w = mEyeViewport[1].extent.x;
  429. eyeTextures[1].D3D9.Header.RenderViewport.Size.h = mEyeViewport[1].extent.y;
  430. eyeTextures[1].D3D9.Header.TextureSize.w = eyeSize.x;
  431. eyeTextures[1].D3D9.Header.TextureSize.h = eyeSize.y;
  432. eyeTextures[1].D3D9.pTexture = mEyeRT[0].getPointer() ? static_cast<GFXD3D9TextureObject*>(mEyeTexture[1].getPointer())->get2DTex() : NULL;
  433. // Submit!
  434. GFX->disableShaders();
  435. GFX->setActiveRenderTarget(windowTarget);
  436. GFX->clear(GFXClearZBuffer | GFXClearStencil | GFXClearTarget, ColorI(255,0,0), 1.0f, 0);
  437. ovrHmd_EndFrame(mDevice, mCurrentEyePoses, (ovrTexture*)(&eyeTextures[0]));
  438. }
  439. #endif
  440. #ifdef OCULUS_USE_GL
  441. GFXGLDevice *glGFX = dynamic_cast<GFXGLDevice*>(GFX);
  442. if (glGFX && mEyeRT[0].getPointer())
  443. {
  444. // Left
  445. ovrGLTexture eyeTextures[2];
  446. eyeSize = mEyeTexture[0].getWidthHeight();
  447. eyeTextures[0].OGL.Header.API = ovrRenderAPI_GL;
  448. eyeTextures[0].OGL.Header.RenderViewport.Pos.x = mEyeViewport[0].point.x;
  449. eyeTextures[0].OGL.Header.RenderViewport.Pos.y = mEyeViewport[0].point.y;
  450. eyeTextures[0].OGL.Header.RenderViewport.Size.w = mEyeViewport[0].extent.x;
  451. eyeTextures[0].OGL.Header.RenderViewport.Size.h = mEyeViewport[0].extent.y;
  452. eyeTextures[0].OGL.Header.TextureSize.w = eyeSize.x;
  453. eyeTextures[0].OGL.Header.TextureSize.h = eyeSize.y;
  454. eyeTextures[0].OGL.TexId = mEyeRT[0].getPointer() ? static_cast<GFXGLTextureObject*>(mEyeTexture[0].getPointer())->getHandle() : 0;
  455. // Right
  456. eyeSize = mEyeTexture[1].getWidthHeight();
  457. eyeTextures[1].OGL.Header.API = ovrRenderAPI_GL;
  458. eyeTextures[1].OGL.Header.RenderViewport.Pos.x = mEyeViewport[1].point.x;
  459. eyeTextures[1].OGL.Header.RenderViewport.Pos.y = mEyeViewport[1].point.y;
  460. eyeTextures[1].OGL.Header.RenderViewport.Size.w = mEyeViewport[1].extent.x;
  461. eyeTextures[1].OGL.Header.RenderViewport.Size.h = mEyeViewport[1].extent.y;
  462. eyeTextures[1].OGL.Header.TextureSize.w = eyeSize.x;
  463. eyeTextures[1].OGL.Header.TextureSize.h = eyeSize.y;
  464. eyeTextures[0].OGL.TexId = mEyeRT[1].getPointer() ? static_cast<GFXGLTextureObject*>(mEyeTexture[1].getPointer())->getHandle() : 0;
  465. // Submit!
  466. GFX->disableShaders();
  467. GFX->setActiveRenderTarget(windowTarget);
  468. GFX->clear(GFXClearZBuffer | GFXClearStencil | GFXClearTarget, ColorI(255,0,0), 1.0f, 0);
  469. ovrHmd_EndFrame(mDevice, mCurrentEyePoses, (ovrTexture*)(&eyeTextures[0]));
  470. }
  471. #endif
  472. #endif
  473. mFrameReady = false;
  474. }
  475. void OculusVRHMDDevice::getFrameEyePose(DisplayPose *outPose, U32 eyeId) const
  476. {
  477. // Directly set the rotation and position from the eye transforms
  478. ovrPosef pose = mCurrentEyePoses[eyeId];
  479. OVR::Quatf orientation = pose.Orientation;
  480. const OVR::Vector3f position = pose.Position;
  481. EulerF rotEuler;
  482. OculusVRUtil::convertRotation(orientation, rotEuler);
  483. outPose->orientation = rotEuler;
  484. outPose->position = Point3F(-position.x, position.z, -position.y);
  485. }
  486. void OculusVRHMDDevice::onDeviceDestroy()
  487. {
  488. if (!mIsValid || !mDevice)
  489. return;
  490. if (mStereoRT.getPointer())
  491. {
  492. mStereoRT->zombify();
  493. }
  494. if (mEyeRT[1].getPointer() && mEyeRT[1] != mStereoRT)
  495. {
  496. mEyeRT[0]->zombify();
  497. mEyeRT[1]->zombify();
  498. }
  499. mStereoRT = NULL;
  500. mStereoTexture = NULL;
  501. mStereoDepthTexture = NULL;
  502. mEyeTexture[0] = NULL;
  503. mEyeDepthTexture[0] = NULL;
  504. mEyeTexture[1] = NULL;
  505. mEyeDepthTexture[1] = NULL;
  506. mEyeRT[0] = NULL;
  507. mEyeRT[1] = NULL;
  508. mRenderConfigurationDirty = true;
  509. ovrHmd_ConfigureRendering(mDevice, NULL, 0, NULL, NULL);
  510. }