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
|
/************************************************************************************
Filename : CAPI_FrameTimeManager3.h
Content : Manage frame timing and pose prediction for rendering
Created : November 30, 2013
Authors : Volga Aksoy, Michael Antonov
Copyright : Copyright 2014 Oculus VR, Inc. All Rights reserved.
Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License");
you may not use the Oculus VR Rift SDK except in compliance with the License,
which is provided at the time of installation or download, or which
otherwise accompanies this software in either electronic or hard copy form.
You may obtain a copy of the License at
http://www.oculusvr.com/licenses/LICENSE-3.2
Unless required by applicable law or agreed to in writing, the Oculus VR SDK
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
************************************************************************************/
#ifndef OVR_CAPI_FrameTimeManager3_h
#define OVR_CAPI_FrameTimeManager3_h
#include <OVR_CAPI.h>
#include <Kernel/OVR_Timer.h>
#include <Extras/OVR_Math.h>
#include <Util/Util_Render_Stereo.h>
namespace OVR { namespace CAPI { namespace FTM3 {
void LogTime(const char* msg);
//-------------------------------------------------------------------------------------
// MedianCalculator
//
// Helper class to calculate statistics
class MedianCalculator
{
public:
MedianCalculator(int capacity);
void Clear();
// This function throws away data larger than 100 milliseconds,
// and it sanitizes negative time deltas replacing them with zero milliseconds.
void Add(double timeDelta);
int GetCount() const
{
return DataCount;
}
int GetCapacity() const
{
return Capacity;
}
bool AtCapacity() const
{
return DataCount >= Capacity;
}
double GetMedian();
bool GetStats(double& minValue, double& maxValue,
double& meanValue, double& medianValue);
private:
Array<double> Data;
Array<double> SortBuffer; // Buffer for quick select algorithm
double MinValue, MaxValue, MeanValue, MedianValue;
int Index;
int DataCount, Capacity;
bool Recalculate;
// Precondition: Count > 0
void doRecalculate();
};
// Helper used to compute AddFrameIndex to DisplayFrameIndex ratio, by tracking
// how much each has advanced over the recent frames.
struct FrameIndexMapper
{
enum { Capacity = 12 };
// Circular buffer starting with StartIndex
unsigned DisplayFrameIndices[Capacity];
unsigned AppFrameIndices[Capacity];
unsigned StartIndex;
unsigned Count;
FrameIndexMapper() : StartIndex(0), Count(0) { }
void Reset()
{ StartIndex = Count = 0; }
void Add(unsigned displayFrameIndex, unsigned appFrameIndex);
double GetAppToDisplayFrameRatio() const;
};
//-------------------------------------------------------------------------------------
// ***** FrameTimeManagerCore
// FrameTimeManager keeps track of rendered frame timing needed for predictions for
// orientations and time-warp.
//
// The following items are not in Core for now
//
// - TimewarpWaitDelta (how many seconds before EndFrame we start timewarp)
// this should be handled externally.
//
// - ScreenDelay (Screen delay from present to scan-out, as potentially reported by ScreenLatencyTracker.)
// this si rendering setup and HW specific
//
// - TimeWarpStartEndTimes (move matrices logic outside for now)
//
// - For now, always assume VSync on
class FrameTimeManagerCore
{
public:
// Describes last presented frame data.
struct Timing
{
// Hard-coded value or dynamic as reported by FrameTimeDeltas.GetMedianTimeDelta().
double FrameDelta;
// Application frame index for which we requested timing.
unsigned AppFrameIndex;
// HW frame index that we expect this will hit, the specified
// frame will start scan-out at ScanoutStartSeconds. Monotonically increasing.
unsigned DisplayFrameIndex;
// PostPresent & Flush (old approach) or reported scan-out time (new HW-reported approach).
double FrameSubmitSeconds;
// Ratio of (AppFrames/DisplayFrames) in
double AppToDisplayFrameRatio;
Timing()
{
memset(this, 0, sizeof(Timing));
}
};
public:
FrameTimeManagerCore(bool vsyncEnabled);
// Called on startup to provided data on HMD timing. DefayltTiming should include FrameDelta
// and other default values. This can also be called to reset timing.
void Initialize(const Timing& defaultTiming);
// Returns frame timing data for any thread to access, including
// - Simulating thread that may be running ahead
// - Rendering thread, which would treat as BeginFrame data
Timing GetAppFrameTiming(unsigned appFrameIndex);
// Returns frame timing values for a particular DisplayFrameIndex.
// Maintaining DisplayFrameIndex is the job of the caller, as it may be fied to OS
// VSync frame reporting functionality ad/or system clock.
Timing GetDisplayFrameTiming(unsigned displayFrameIndex);
// To be called from TW Thread when displayFrame has been submitted for present.
// This call is used to update lock-less timing frame basis. The provided
// values do the following:
// - Establish relationship between App and Display frame index.
// - Track the real frameDelta (difference between VSyncs)
void SubmitDisplayFrame(unsigned displayFrameIndex,
unsigned appFrameIndex,
double scanoutStartSeconds);
unsigned GetLastDisplayFrameIndex() const
{
return LastTiming.DisplayFrameIndex;
}
double GetLastDisplayFrameTime() const
{
return LastTiming.FrameSubmitSeconds;
}
double GetFrameDelta() const
{
return calcFrameDelta();
}
void SetVsync(bool enabled) { VsyncEnabled = enabled; }
// ovrFrameTiming ovrFrameTimingFromTiming(const Timing& timing) const;
private:
double calcFrameDelta() const;
// Timing changes if we have no Vsync (all prediction is reduced to fixed interval).
bool VsyncEnabled;
// Default VsyncToVSync value, received in Initialize.
double DefaultFrameDelta;
// Last timing
Timing LastTiming;
// Current (or last) frame timing info. Used as a source for LocklessTiming.
LocklessUpdater<Timing, Timing> LocklessTiming;
// Timings are collected through a median filter, to avoid outliers.
mutable MedianCalculator FrameTimeDeltas;
// Associates AppFrameIndex <->
FrameIndexMapper FrameIndices;
};
}}} // namespace OVR::CAPI::FTM3
#endif // OVR_CAPI_FrameTimeManager_h
|