-
-
Notifications
You must be signed in to change notification settings - Fork 106
/
OculusVRLayers.h
346 lines (284 loc) · 11.1 KB
/
OculusVRLayers.h
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#pragma once
#include "vrb/Forward.h"
#include "vrb/MacroUtils.h"
#include "vrb/FBO.h"
#include "vrb/Color.h"
#include "vrb/Matrix.h"
#include "vrb/GLError.h"
#include "DeviceDelegate.h"
#include "VRLayer.h"
#include "VrApi.h"
#include "VrApi_Helpers.h"
#include "VrApi_SystemUtils.h"
#include "OculusSwapChain.h"
#include <memory>
namespace crow {
class OculusLayer;
typedef std::shared_ptr<OculusLayer> OculusLayerPtr;
struct SurfaceChangedTarget {
OculusLayer *layer;
SurfaceChangedTarget(OculusLayer *aLayer) : layer(aLayer) {};
};
typedef std::shared_ptr<SurfaceChangedTarget> SurfaceChangedTargetPtr;
typedef std::weak_ptr<SurfaceChangedTarget> SurfaceChangedTargetWeakPtr;
class OculusLayer {
public:
static bool sForceClip;
virtual void Init(JNIEnv *aEnv, vrb::RenderContextPtr &aContext) = 0;
virtual void Update(uint32_t aFrameIndex, const ovrTracking2 &aTracking, ovrTextureSwapChain *aClearSwapChain) = 0;
virtual const OculusSwapChainPtr& GetSwapChain() const = 0;
virtual const ovrLayerHeader2 *Header() const = 0;
virtual void SetCurrentEye(device::Eye aEye) = 0;
virtual bool IsDrawRequested() const = 0;
virtual bool GetDrawInFront() const = 0;
virtual void ClearRequestDraw() = 0;
virtual bool IsComposited() const = 0;
virtual void SetComposited(bool aValue) = 0;
virtual VRLayerPtr GetLayer() const = 0;
virtual void Destroy() = 0;
typedef std::function<void(const OculusSwapChainPtr&, GLenum aTarget, bool aBound)> BindDelegate;
virtual void SetBindDelegate(const BindDelegate &aDelegate) = 0;
virtual SurfaceChangedTargetPtr GetSurfaceChangedTarget() const = 0;
virtual void
HandleResize(const OculusSwapChainPtr& newSwapChain) = 0;
virtual ~OculusLayer() {}
};
template<class T, class U>
class OculusLayerBase : public OculusLayer {
public:
OculusSwapChainPtr swapChain;
SurfaceChangedTargetPtr surfaceChangedTarget;
T layer;
U ovrLayer;
void Init(JNIEnv *aEnv, vrb::RenderContextPtr &aContext) override {
layer->SetInitialized(true);
surfaceChangedTarget = std::make_shared<SurfaceChangedTarget>(this);
SurfaceChangedTargetWeakPtr weakTarget = surfaceChangedTarget;
layer->NotifySurfaceChanged(VRLayer::SurfaceChange::Create, [=]() {
SurfaceChangedTargetPtr target = weakTarget.lock();
if (target) {
target->layer->SetComposited(true);
}
});
}
virtual void
Update(uint32_t aFrameIndex, const ovrTracking2 &aTracking, ovrTextureSwapChain *aClearSwapChain) override {
vrb::Color tintColor = layer->GetTintColor();
if (!IsComposited() && (layer->GetClearColor().Alpha() > 0.0f)) {
tintColor = layer->GetClearColor();
tintColor.SetRGBA(convertColor(tintColor.Red()), convertColor(tintColor.Green()), convertColor(tintColor.Blue()), tintColor.Alpha());
}
ovrLayer.Header.ColorScale.x = tintColor.Red();
ovrLayer.Header.ColorScale.y = tintColor.Green();
ovrLayer.Header.ColorScale.z = tintColor.Blue();
ovrLayer.Header.ColorScale.w = tintColor.Alpha();
}
virtual const OculusSwapChainPtr& GetSwapChain() const override {
return swapChain;
}
const ovrLayerHeader2 *Header() const override {
return &ovrLayer.Header;
}
void SetCurrentEye(device::Eye aEye) override {
layer->SetCurrentEye(aEye);
}
virtual bool IsDrawRequested() const override {
return layer->IsDrawRequested() &&
((swapChain && IsComposited()) || layer->GetClearColor().Alpha() > 0.0f);
}
bool GetDrawInFront() const override {
return layer->GetDrawInFront();
}
void ClearRequestDraw() override {
layer->ClearRequestDraw();
}
bool IsComposited() const override {
return layer->IsComposited();
}
void SetComposited(bool aValue) override {
layer->SetComposited(aValue);
}
VRLayerPtr GetLayer() const override {
return layer;
}
void SetClipEnabled(bool aEnabled) {
if (aEnabled) {
ovrLayer.Header.Flags |= VRAPI_FRAME_LAYER_FLAG_CLIP_TO_TEXTURE_RECT;
} else {
ovrLayer.Header.Flags &= ~VRAPI_FRAME_LAYER_FLAG_CLIP_TO_TEXTURE_RECT;
}
}
void Destroy() override {
swapChain = nullptr;
layer->SetInitialized(false);
SetComposited(false);
layer->NotifySurfaceChanged(VRLayer::SurfaceChange::Destroy, nullptr);
}
void SetBindDelegate(const BindDelegate &aDelegate) override {}
SurfaceChangedTargetPtr GetSurfaceChangedTarget() const override {
return surfaceChangedTarget;
}
void HandleResize(const OculusSwapChainPtr& newSwapChain) override {}
ovrTextureSwapChain *GetTargetSwapChain(ovrTextureSwapChain *aClearSwapChain) {
return (IsComposited() || layer->GetClearColor().Alpha() == 0) ? swapChain->SwapChain() : aClearSwapChain;
}
protected:
virtual ~OculusLayerBase() {}
// Convert sRGB to linear RGB. Used to work around bug in Oculus compositor.
float convertColor(const float color) {
if (color > 0.04045f) {
return powf(color + 0.055f, 2.4f) / 1.055f;
} else {
return color / 12.92f;
}
}
};
template<typename T, typename U>
class OculusLayerSurface : public OculusLayerBase<T, U> {
public:
vrb::RenderContextWeak contextWeak;
JNIEnv *jniEnv = nullptr;
OculusLayer::BindDelegate bindDelegate;
void Init(JNIEnv *aEnv, vrb::RenderContextPtr &aContext) override {
this->jniEnv = aEnv;
this->contextWeak = aContext;
this->ovrLayer.Header.SrcBlend = VRAPI_FRAME_LAYER_BLEND_SRC_ALPHA;
this->ovrLayer.Header.DstBlend = VRAPI_FRAME_LAYER_BLEND_ONE_MINUS_SRC_ALPHA;
if (this->swapChain) {
return;
}
this->swapChain = CreateSwapChain();
this->layer->SetResizeDelegate([=] {
Resize();
});
OculusLayerBase<T, U>::Init(aEnv, aContext);
}
void Resize() {
if (!this->swapChain) {
return;
}
// Delay the destruction of the current swapChain until the new one is composited.
// This is required to prevent a black flicker when resizing.
OculusSwapChainPtr newSwapChain = CreateSwapChain();
this->layer->SetSurface(newSwapChain->AndroidSurface());
SurfaceChangedTargetWeakPtr weakTarget = this->surfaceChangedTarget;
this->layer->NotifySurfaceChanged(VRLayer::SurfaceChange::Create, [=]() {
SurfaceChangedTargetPtr target = weakTarget.lock();
if (target && target->layer) {
target->layer->HandleResize(newSwapChain);
}
});
}
void
HandleResize(const OculusSwapChainPtr& newSwapChain) override {
this->swapChain = newSwapChain;
this->SetComposited(true);
}
void Destroy() override {
this->layer->SetSurface(nullptr);
OculusLayerBase<T, U>::Destroy();
}
void SetBindDelegate(const OculusLayer::BindDelegate &aDelegate) override {
bindDelegate = aDelegate;
this->layer->SetBindDelegate([=](GLenum aTarget, bool aBind) {
if (bindDelegate) {
bindDelegate(this->swapChain, aTarget, aBind);
}
});
}
protected:
void TakeSurface(JNIEnv * aEnv, const OculusLayerPtr &aSource) {
this->swapChain = aSource->GetSwapChain();
this->jniEnv = aEnv;
this->surfaceChangedTarget = aSource->GetSurfaceChangedTarget();
if (this->surfaceChangedTarget) {
// Indicate that the first composite notification should be notified to this layer.
this->surfaceChangedTarget->layer = this;
}
this->SetComposited(aSource->IsComposited());
this->layer->SetInitialized(aSource->GetLayer()->IsInitialized());
this->layer->SetResizeDelegate([=] {
Resize();
});
}
OculusSwapChainPtr CreateSwapChain() {
OculusSwapChainPtr result;
if (this->layer->GetSurfaceType() == VRLayerQuad::SurfaceType::AndroidSurface) {
result = OculusSwapChain::CreateAndroidSurface(this->jniEnv, this->layer->GetWidth(), this->layer->GetHeight());
this->layer->SetSurface(result->AndroidSurface());
} else {
vrb::RenderContextPtr ctx = this->contextWeak.lock();
vrb::FBO::Attributes attributes;
bool buffered;
if (this->layer->GetLayerType() == VRLayer::LayerType::PROJECTION) {
buffered = true;
attributes.samples = 4;
attributes.depth = true;
} else {
buffered = false;
attributes.samples = 0;
attributes.depth = 0;
}
result = OculusSwapChain::CreateFBO(ctx, attributes,
this->layer->GetWidth(), this->layer->GetHeight(), buffered);
}
return result;
}
};
class OculusLayerQuad;
typedef std::shared_ptr<OculusLayerQuad> OculusLayerQuadPtr;
class OculusLayerQuad : public OculusLayerSurface<VRLayerQuadPtr, ovrLayerProjection2> {
public:
static OculusLayerQuadPtr
Create(JNIEnv *aEnv, const VRLayerQuadPtr &aLayer, const OculusLayerPtr &aSource = nullptr);
void Init(JNIEnv *aEnv, vrb::RenderContextPtr &aContext) override;
void Update(uint32_t aFrameIndex, const ovrTracking2 &aTracking, ovrTextureSwapChain *aClearSwapChain) override;
};
class OculusLayerCylinder;
typedef std::shared_ptr<OculusLayerCylinder> OculusLayerCylinderPtr;
class OculusLayerCylinder : public OculusLayerSurface<VRLayerCylinderPtr, ovrLayerCylinder2> {
public:
static OculusLayerCylinderPtr
Create(JNIEnv *aEnv, const VRLayerCylinderPtr &aLayer, const OculusLayerPtr &aSource = nullptr);
void Init(JNIEnv *aEnv, vrb::RenderContextPtr &aContext) override;
void Update(uint32_t aFrameIndex, const ovrTracking2 &aTracking, ovrTextureSwapChain *aClearSwapChain) override;
};
class OculusLayerProjection;
typedef std::shared_ptr<OculusLayerProjection> OculusLayerProjectionPtr;
class OculusLayerProjection : public OculusLayerSurface<VRLayerProjectionPtr, ovrLayerProjection2> {
public:
static OculusLayerProjectionPtr
Create(JNIEnv *aEnv, const VRLayerProjectionPtr &aLayer);
void Init(JNIEnv *aEnv, vrb::RenderContextPtr &aContext) override;
void Update(uint32_t aFrameIndex, const ovrTracking2 &aTracking, ovrTextureSwapChain *aClearSwapChain) override;
void SetBindDelegate(const OculusLayer::BindDelegate &aDelegate) override;
inline void SetProjectionMatrix(const ovrMatrix4f& aMatrix) { projectionMatrix = aMatrix; }
protected:
OculusSwapChainPtr secondSwapChain;
ovrMatrix4f projectionMatrix;
};
class OculusLayerCube;
typedef std::shared_ptr<OculusLayerCube> OculusLayerCubePtr;
class OculusLayerCube : public OculusLayerBase<VRLayerCubePtr, ovrLayerCube2> {
public:
static OculusLayerCubePtr Create(const VRLayerCubePtr &aLayer, GLint aInternalFormat);
void Init(JNIEnv *aEnv, vrb::RenderContextPtr &aContext) override;
void Update(uint32_t aFrameIndex, const ovrTracking2 &aTracking, ovrTextureSwapChain *aClearSwapChain) override;
void Destroy() override;
bool IsLoaded() const;
protected:
GLint glFormat;
};
class OculusLayerEquirect;
typedef std::shared_ptr<OculusLayerEquirect> OculusLayerEquirectPtr;
class OculusLayerEquirect : public OculusLayerBase<VRLayerEquirectPtr, ovrLayerEquirect2> {
public:
std::weak_ptr<OculusLayer> sourceLayer;
static OculusLayerEquirectPtr
Create(const VRLayerEquirectPtr &aLayer, const OculusLayerPtr &aSourceLayer);
void Init(JNIEnv *aEnv, vrb::RenderContextPtr &aContext) override;
void Update(uint32_t aFrameIndex, const ovrTracking2 &aTracking, ovrTextureSwapChain *aClearSwapChain) override;
void Destroy() override;
bool IsDrawRequested() const override;
};
}