Overte C++ Documentation
OpenVrDisplayPlugin.h
1 //
2 // Created by Bradley Austin Davis on 2015/06/12
3 // Copyright 2015 High Fidelity, Inc.
4 //
5 // Distributed under the Apache License, Version 2.0.
6 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
7 //
8 #pragma once
9 
10 #include <QtGlobal>
11 
12 #include <openvr.h>
13 
14 #include <display-plugins/hmd/HmdDisplayPlugin.h>
15 
16 #include <graphics/Geometry.h>
17 
18 const float TARGET_RATE_OpenVr = 90.0f; // FIXME: get from sdk tracked device property? This number is vive-only.
19 
20 namespace gl {
21  class OffscreenContext;
22 }
23 class OpenVrSubmitThread;
24 class OffscreenGLCanvas;
25 static const size_t COMPOSITING_BUFFER_SIZE = 3;
26 
27 struct CompositeInfo {
28  using Queue = std::queue<CompositeInfo>;
29  using Array = std::array<CompositeInfo, COMPOSITING_BUFFER_SIZE>;
30 
31  gpu::TexturePointer texture;
32  uint32_t textureID { 0 };
33  glm::mat4 pose;
34  void* fence{ 0 };
35 };
36 
37 class OpenVrDisplayPlugin : public HmdDisplayPlugin {
38  using Parent = HmdDisplayPlugin;
39 public:
40  bool isSupported() const override;
41  const QString getName() const override;
42  bool getSupportsAutoSwitch() override final { return true; }
43 
44  glm::mat4 getEyeProjection(Eye eye, const glm::mat4& baseProjection) const override;
45  glm::mat4 getCullingProjection(const glm::mat4& baseProjection) const override;
46 
47  void init() override;
48 
49  float getTargetFrameRate() const override;
50  bool hasAsyncReprojection() const override { return _asyncReprojectionActive; }
51 
52  void customizeContext() override;
53  void uncustomizeContext() override;
54 
55  // Stereo specific methods
56  void resetSensors() override;
57  bool beginFrameRender(uint32_t frameIndex) override;
58  void cycleDebugOutput() override { _lockCurrentTexture = !_lockCurrentTexture; }
59 
60  bool suppressKeyboard() override;
61  void unsuppressKeyboard() override;
62  bool isKeyboardVisible() override;
63 
64  // Possibly needs an additional thread for VR submission
65  int getRequiredThreadCount() const override;
66 
67  QString getPreferredAudioInDevice() const override;
68  QString getPreferredAudioOutDevice() const override;
69 
70  QRectF getPlayAreaRect() override;
71 
72  virtual StencilMaskMode getStencilMaskMode() const override { return StencilMaskMode::MESH; }
73  virtual StencilMaskMeshOperator getStencilMaskMeshOperator() override;
74 
75  virtual void updateParameters(float visionSqueezeX, float visionSqueezeY, float visionSqueezeTransition,
76  int visionSqueezePerEye, float visionSqueezeGroundPlaneY,
77  float visionSqueezeSpotlightSize) override;
78 
79  glm::mat4 getSensorResetMatrix() const { return _sensorResetMat; }
80 
81 protected:
82  bool internalActivate() override;
83  void internalDeactivate() override;
84  void updatePresentPose() override;
85 
86  void compositeLayers() override;
87  void hmdPresent() override;
88  bool isHmdMounted() const override;
89  void postPreview() override;
90 
91 private:
92  vr::IVRSystem* _system { nullptr };
93  std::atomic<uint32_t> _keyboardSupressionCount{ 0 };
94 
95  vr::HmdMatrix34_t _lastGoodHMDPose;
96  mat4 _sensorResetMat;
97  bool _threadedSubmit { true };
98 
99  CompositeInfo::Array _compositeInfos;
100  size_t _renderingIndex { 0 };
101  std::shared_ptr<OpenVrSubmitThread> _submitThread;
102  std::shared_ptr<gl::OffscreenContext> _submitCanvas;
103  friend class OpenVrSubmitThread;
104 
105  bool _asyncReprojectionActive { false };
106 
107  bool _hmdMounted { false };
108 
109  std::array<graphics::MeshPointer, 2> _stencilMeshes;
110  bool _stencilMeshesInitialized { false };
111 
112  float _visionSqueezeX;
113  float _visionSqueezeY;
114  float _visionSqueezeTransition;
115  int _visionSqueezePerEye;
116  float _visionSqueezeGroundPlaneY;
117  float _visionSqueezeSpotlightSize;
118 };