summaryrefslogtreecommitdiffstats
path: root/gfx/vr/service/OculusSession.h
blob: e1feeb205298db033c849bf3b6a0c2bff598d271 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef GFX_VR_SERVICE_OCULUSSESSION_H
#define GFX_VR_SERVICE_OCULUSSESSION_H

#include "VRSession.h"

#include "mozilla/gfx/2D.h"
#include "moz_external_vr.h"
#include "nsTArray.h"
#include "oculus/ovr_capi_dynamic.h"
#include "prlink.h"
#include "ShaderDefinitionsD3D11.h"  // for VertexShaderConstants and PixelShaderConstants

struct ID3D11Device;

namespace mozilla {
namespace layers {
struct VertexShaderConstants;
struct PixelShaderConstants;
}  // namespace layers
namespace gfx {

class OculusSession : public VRSession {
 public:
  OculusSession();
  virtual ~OculusSession();

  bool Initialize(mozilla::gfx::VRSystemState& aSystemState,
                  bool aDetectRuntimesOnly) override;
  void Shutdown() override;
  void ProcessEvents(mozilla::gfx::VRSystemState& aSystemState) override;
  void StartFrame(mozilla::gfx::VRSystemState& aSystemState) override;
  bool StartPresentation() override;
  void StopPresentation() override;
  bool SubmitFrame(const mozilla::gfx::VRLayer_Stereo_Immersive& aLayer,
                   ID3D11Texture2D* aTexture) override;
  void VibrateHaptic(uint32_t aControllerIdx, uint32_t aHapticIndex,
                     float aIntensity, float aDuration) override;
  void StopVibrateHaptic(uint32_t aControllerIdx) override;
  void StopAllHaptics() override;

 private:
  bool LoadOvrLib();
  void UnloadOvrLib();
  bool StartLib(ovrInitFlags aFlags);
  void StopLib();
  bool StartSession();
  void StopSession();
  bool StartRendering();
  void StopRendering();
  bool CreateD3DObjects();
  bool CreateShaders();
  void DestroyShaders();
  void CoverTransitions();
  void UpdateVisibility();
  bool ChangeVisibility(bool bVisible);
  bool InitState(mozilla::gfx::VRSystemState& aSystemState);
  void UpdateStageParameters(mozilla::gfx::VRDisplayState& aState);
  void UpdateEyeParameters(mozilla::gfx::VRSystemState& aState);
  void UpdateHeadsetPose(mozilla::gfx::VRSystemState& aState);
  void UpdateControllers(VRSystemState& aState);
  void UpdateControllerInputs(VRSystemState& aState,
                              const ovrInputState& aInputState);
  void UpdateHaptics();
  void EnumerateControllers(VRSystemState& aState,
                            const ovrInputState& aInputState);
  void UpdateControllerPose(VRSystemState& aState,
                            const ovrInputState& aInputState);
  void UpdateTelemetry(VRSystemState& aSystemState);
  bool IsPresentationReady() const;
  bool UpdateConstantBuffers();

  PRLibrary* mOvrLib;
  ovrSession mSession;
  ovrInitFlags mInitFlags;
  ovrTextureSwapChain mTextureSet;
  nsTArray<RefPtr<ID3D11RenderTargetView>> mRTView;
  nsTArray<RefPtr<ID3D11Texture2D>> mTexture;
  nsTArray<RefPtr<ID3D11ShaderResourceView>> mSRV;

  ID3D11VertexShader* mQuadVS;
  ID3D11PixelShader* mQuadPS;
  RefPtr<ID3D11SamplerState> mLinearSamplerState;
  layers::VertexShaderConstants mVSConstants;
  layers::PixelShaderConstants mPSConstants;
  RefPtr<ID3D11Buffer> mVSConstantBuffer;
  RefPtr<ID3D11Buffer> mPSConstantBuffer;
  RefPtr<ID3D11Buffer> mVertexBuffer;
  RefPtr<ID3D11InputLayout> mInputLayout;

  IntSize mPresentationSize;
  ovrFovPort mFOVPort[2];

  // Most recent HMD eye poses, from start of frame
  ovrPosef mFrameStartPose[2];

  float mRemainingVibrateTime[2];
  float mHapticPulseIntensity[2];
  TimeStamp mLastHapticUpdate;

  // The timestamp of the last ending presentation
  TimeStamp mLastPresentationEnd;
  bool mIsPresenting;
};

}  // namespace gfx
}  // namespace mozilla

#endif  // GFX_VR_SERVICE_OCULUSSESSION_H