oculusVRHMDDevice.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. #ifndef _OCULUSVRHMDDEVICE_H_
  23. #define _OCULUSVRHMDDEVICE_H_
  24. #include "core/util/str.h"
  25. #include "math/mQuat.h"
  26. #include "math/mPoint2.h"
  27. #include "math/mPoint3.h"
  28. #include "math/mPoint4.h"
  29. #include "platform/input/oculusVR/oculusVRConstants.h"
  30. #include "platform/types.h"
  31. #include "gfx/gfxTextureHandle.h"
  32. #include "math/mRect.h"
  33. #include "gfx/gfxDevice.h"
  34. #include "OVR_CAPI.h"
  35. class GuiCanvas;
  36. class GameConnection;
  37. struct DisplayPose;
  38. class OculusVRSensorDevice;
  39. struct OculusTexture;
  40. class OculusVRHMDDevice
  41. {
  42. public:
  43. enum SimulationTypes {
  44. ST_RIFT_PREVIEW,
  45. };
  46. protected:
  47. bool mIsValid;
  48. bool mVsync;
  49. bool mTimewarp;
  50. bool mRenderConfigurationDirty;
  51. bool mFrameReady;
  52. ovrHmd mDevice;
  53. U32 mSupportedCaps;
  54. U32 mCurrentCaps;
  55. // From OVR::DeviceInfo
  56. String mProductName;
  57. String mManufacturer;
  58. U32 mVersion;
  59. // Device type (D3D11, etc)
  60. String mDisplayDeviceType;
  61. // Adapter index
  62. S32 mDisplayId;
  63. // Whole screen resolution
  64. Point2I mResolution;
  65. // Physical screen size in meters
  66. Point2F mScreenSize;
  67. // Physical distance between lens centers, in meters
  68. F32 mLensSeparation;
  69. // Physical distance between the user's eye centers as defined in the current profile
  70. F32 mProfileInterpupillaryDistance;
  71. // Physical distance between the user's eye centers
  72. F32 mInterpupillaryDistance;
  73. // The amount to offset the projection matrix to account for the eye not being in the
  74. // center of the screen.
  75. Point2F mProjectionCenterOffset;
  76. // Current pose of eyes
  77. ovrEyeRenderDesc mEyeRenderDesc[2];
  78. GameConnection *mConnection;
  79. OculusVRSensorDevice *mSensor;
  80. U32 mActionCodeIndex;
  81. ovrGraphicsLuid mLuid;
  82. protected:
  83. void updateRenderInfo();
  84. public:
  85. OculusVRHMDDevice();
  86. ~OculusVRHMDDevice();
  87. void cleanUp();
  88. // Set the HMD properties based on information from the OVR device
  89. void set(ovrHmd hmd, ovrGraphicsLuid luid, U32 actionCodeIndex);
  90. // Sets optimal display size for canvas
  91. void setOptimalDisplaySize(GuiCanvas *canvas);
  92. bool isValid() const {return mIsValid;}
  93. const char* getProductName() const { return mProductName.c_str(); }
  94. const char* getManufacturer() const { return mManufacturer.c_str(); }
  95. U32 getVersion() const { return mVersion; }
  96. // Windows display device name used in EnumDisplaySettings/CreateDC
  97. const char* getDisplayDeviceType () const { return mDisplayDeviceType.c_str(); }
  98. // MacOS display ID
  99. S32 getDisplayDeviceId() const { return mDisplayId; }
  100. // Whole screen resolution
  101. const Point2I& getResolution() const { return mResolution; }
  102. // Physical screen size in meters
  103. const Point2F& getScreenSize() const { return mScreenSize; }
  104. // Physical distance between lens centers, in meters
  105. F32 getLensSeparation() const { return mLensSeparation; }
  106. // Physical distance between the user's eye centers as defined by the current profile
  107. F32 getProfileIPD() const { return mProfileInterpupillaryDistance; }
  108. // Physical distance between the user's eye centers
  109. F32 getIPD() const { return mInterpupillaryDistance; }
  110. // Set a new physical distance between the user's eye centers
  111. void setIPD(F32 ipd);
  112. // The amount to offset the projection matrix to account for the eye not being in the
  113. // center of the screen.
  114. const Point2F& getProjectionCenterOffset() const { return mProjectionCenterOffset; }
  115. void getStereoViewports(RectI *dest) const { dMemcpy(dest, mEyeViewport, sizeof(mEyeViewport)); }
  116. void getStereoTargets(GFXTextureTarget **dest) const { dest[0] = mEyeRT[0]; dest[1] = mEyeRT[1]; }
  117. void getFovPorts(FovPort *dest) const { dMemcpy(dest, &mRenderLayer.Fov[0], sizeof(mRenderLayer.Fov)); }
  118. /// Returns eye offsets in torque coordinate space, i.e. z being up, x being left-right, and y being depth (forward).
  119. void getEyeOffsets(Point3F *offsets) const {
  120. offsets[0] = Point3F(-mEyeRenderDesc[0].HmdToEyeViewOffset.x, mEyeRenderDesc[0].HmdToEyeViewOffset.z, -mEyeRenderDesc[0].HmdToEyeViewOffset.y);
  121. offsets[1] = Point3F(-mEyeRenderDesc[1].HmdToEyeViewOffset.x, mEyeRenderDesc[1].HmdToEyeViewOffset.z, -mEyeRenderDesc[1].HmdToEyeViewOffset.y); }
  122. void getFrameEyePose(DisplayPose *outPose, U32 eyeId) const;
  123. void updateCaps();
  124. void onStartFrame();
  125. void onEndFrame();
  126. void onDeviceDestroy();
  127. Point2I generateRenderTarget(GFXTextureTargetRef &target, GFXTexHandle &depth, Point2I desiredSize);
  128. void clearRenderTargets();
  129. bool isDisplayingWarning();
  130. void dismissWarning();
  131. bool setupTargets();
  132. /// Designates canvas we are drawing to. Also updates render targets
  133. void setDrawCanvas(GuiCanvas *canvas) { if (mDrawCanvas != canvas) { mDrawCanvas = canvas; } updateRenderInfo(); }
  134. virtual void setCurrentConnection(GameConnection *connection) { mConnection = connection; }
  135. virtual GameConnection* getCurrentConnection() { return mConnection; }
  136. GFXTexHandle getPreviewTexture();
  137. String dumpMetrics();
  138. // Stereo RT
  139. GFXTexHandle mDebugStereoTexture;
  140. GFXTexHandle mStereoDepthTexture;
  141. GFXTextureTargetRef mStereoRT;
  142. // Eye RTs (if we are using separate targets)
  143. GFXTextureTargetRef mEyeRT[2];
  144. // Desired viewport for each eye
  145. RectI mEyeViewport[2];
  146. F32 mCurrentPixelDensity;
  147. F32 smDesiredPixelDensity;
  148. ovrTrackingState mLastTrackingState;
  149. OculusTexture* mTextureSwapSet;
  150. ovrLayerEyeFov mRenderLayer;
  151. ovrLayerDirect mDebugRenderLayer;
  152. ovrViewScaleDesc mScaleDesc;
  153. ovrTexture* mDebugMirrorTexture;
  154. GFXTexHandle mDebugMirrorTextureHandle;
  155. GFXDevice::GFXDeviceRenderStyles mDesiredRenderingMode;
  156. GFXFormat mRTFormat;
  157. // Canvas we should be drawing
  158. GuiCanvas *mDrawCanvas;
  159. OculusVRSensorDevice *getSensorDevice() { return mSensor; }
  160. };
  161. #endif // _OCULUSVRHMDDEVICE_H_