aboutsummaryrefslogtreecommitdiffstats
path: root/LibOVR/Src/CAPI/CAPI_DistortionTiming.cpp
blob: 6780ee63d0c1574dc0f56c7d78f01a0cd706e59f (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
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
/************************************************************************************

Filename    :   CAPI_DistortionTiming.cpp
Content     :   Implements timing for the distortion renderer
Created     :   Dec 16, 2014
Authors     :   Chris Taylor

Copyright   :   Copyright 2014 Oculus VR, LLC 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.

************************************************************************************/

#include "CAPI_DistortionRenderer.h"

#ifdef OVR_OS_WIN32
#include "../Displays/OVR_Win32_Dxgi_Display.h" // Display driver timing info
#endif

namespace OVR { namespace CAPI {


//-----------------------------------------------------------------------------
// Timing Constants

// Number of milliseconds to pad on top of the timewarp draw call measured time
// in order to account for random variations in execution time due to preemption.
// If this is set too low the rendering will occasionally judder.
static const double kJITPreemptBufferTime   = 0.004;    // 4 milliseconds

// When validating measured frame intervals, the following constants
// bound the acceptable measurements.
static const double kMinFrameInterval       = 0.001;    // 1 millisecond
static const double kMaxFrameInterval       = 0.020;    // 20 milliseconds

// If the last known Vsync time is older than this age limit,
// then we should not use it for extrapolating to current time.
static const double kVsyncDataAgeLimit      = 10.;      // 10 seconds

// When Vsync is off and we have no idea when the last frame started,
// assume this amount of time has elapsed since the frame started.
static const double kNoVsyncInfoFrameTime   = 0.002;    // 2 milliseconds

#ifdef OVR_OS_WIN32
// The latest driver provides a post-present vsync-to-scanout delay
// that is roughly zero.  The actual measured latency should be
// about the same as this.
static const double kExpectedDriverLatency  = 0.0002f;  // 200 microseconds
#endif

// Number from a hat for post-present latency when Vsync is off.
static const double kExpectedNoVSyncLatency = 0.003;    // 3 milliseconds

// Number of timewarp render time samples to collect
static const int kTimewarpRenderTimeSamples = 12;       // 12 samples

// Adding a fuzz time because the last known Vsync time is sometimes fuzzy and
// we don't want to predict behind a whole frame.  This is most often used in
// app rendered and D3D9 renderers and on Win/Mac/Linux with OpenGL.
static const double kFuzzyVsyncBufferTime   = kJITPreemptBufferTime;
// Currently set to the same fuzz factor used for JIT preemption because the
// same amount of error is accounted for by both constants.

// Even when the Vsync timing data source is precise we should add some kind of
// buffer in to avoid floating point rounding or unexpected sync problems.
static const double kExactVsyncBufferTime   = 0.001;    // 1 millisecond


//-----------------------------------------------------------------------------
// Helper Functions

// Based on LastKnownVsyncTime, predict time when the previous frame Vsync occurred.
// If it has no data it will still provide a reasonable estimate of last Vsync time.
static double calculateFrameStartTime(double now,
                                      double lastKnownVsyncTime,
                                      double lastKnownVsyncFuzzBuffer,
                                      double frameInterval)
{
    // Calculate time since last known vsync
    // Adding a fuzz time because the last known Vsync time is sometimes fuzzy and
    // we don't want to predict behind a frame.
    const double delta = now - lastKnownVsyncTime + lastKnownVsyncFuzzBuffer;

    // If last known vsync time was too long ago,
    if (delta < 0. ||
        delta > kVsyncDataAgeLimit)
    {
        // We have no idea when Vsync will happen!

        // Assume we are some time into the frame when this is called.
        return now - kNoVsyncInfoFrameTime;
    }

    // Calculate number of Vsyncs since the last known Vsync time.
    int numVsyncs = (int)(delta / frameInterval);

    // Calculate the last Vsync time.
    double lastFrameVsyncTime = lastKnownVsyncTime + numVsyncs * frameInterval;

    // Sanity checking...
    OVR_ASSERT(lastFrameVsyncTime - now > -0.16 && lastFrameVsyncTime - now < 0.30);

    return lastFrameVsyncTime;
}


//-----------------------------------------------------------------------------
// DistortionTiming : Initialization

DistortionTimer::DistortionTimer() :
    LastPresentTime(0),
    LastKnownVsyncTime(0),
    LastKnownVsyncFuzzBuffer(0),
    AppFrameIndex(0),
    DistortionRenderTimes(kTimewarpRenderTimeSamples),
    EstimatedTimewarpRenderTime(0),
  #ifdef OVR_OS_WIN32
    DeviceHandle(nullptr),
  #endif
    LatencyTester(nullptr),
    RenderState(nullptr),
    ScreenSwitchingDelay(0),
    TimeManager(true),
    LastTimewarpFrameEndTime(0),
    AlreadyInitialized(false),
    CurrentFrameTimewarpTiming(),
    LastTimewarpIMUTime(0)
{
    Reset();
}

void DistortionTimer::Reset()
{
    // Clear state
    LastKnownVsyncTime       = 0.;
    LastKnownVsyncFuzzBuffer = 0.;
    LastPresentTime          = 0.;
    LastTimewarpFrameEndTime = 0.;
    AppFrameIndex            = 0;

    ClearAppTimingUpdater();

    // Does not clear the distortion render times because this data is still good
    //DistortionRenderTimes.Clear();
    //EstimatedTimewarpRenderTime = 0.;
    //LatencyTester = nullptr;
    //RenderState = nullptr;
}

DistortionTimer::~DistortionTimer()
{
    RenderState   = nullptr;
    LatencyTester = nullptr;
}

bool DistortionTimer::Initialize(HMDRenderState const * renderState,
                                 FrameLatencyTracker const * lagTester)
{
    if (AlreadyInitialized)
    {
        OVR_ASSERT(renderState == RenderState && lagTester == LatencyTester);
        return true;
    }

    if (!renderState || !lagTester)
    {
        OVR_ASSERT(false);
        return false;
    }

    // Store members
    RenderState   = renderState;
    LatencyTester = lagTester;

#ifdef OVR_OS_WIN32
    // If in direct mode,
    if (!RenderState->OurHMDInfo.InCompatibilityMode)
    {
        // Attempt to open the driver
        DeviceHandle = CreateFile(L"\\\\.\\ovr_video",
            GENERIC_READ | GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, 0, nullptr);
    }
#endif

    HmdRenderInfo::ShutterInfo const& shutter = RenderState->RenderInfo.Shutter;

    // Calculate the screen switching delay from shutter info.
    ScreenSwitchingDelay = shutter.PixelSettleTime * 0.5 + shutter.PixelPersistence * 0.5;

    // Set default frame delta for the TimeManager.
    TimeMan::Timing defaultTiming;
    defaultTiming.FrameDelta = shutter.VsyncToNextVsync;
    TimeManager.Initialize(defaultTiming);

    AlreadyInitialized = true;
    return true;
}


//-----------------------------------------------------------------------------
// DistortionTiming : Helper Member Functions

double DistortionTimer::getFrameInterval() const
{
    // Get the latest frame interval from the time manager.
    double frameInterval = TimeManager.GetFrameDelta();

    // If bad data is coming from the frame delta calculator,
    if (frameInterval < kMinFrameInterval ||
        frameInterval > kMaxFrameInterval)
    {
        // Use the shutter value by default.
        HmdRenderInfo::ShutterInfo const& shutter = RenderState->RenderInfo.Shutter;
        frameInterval = shutter.VsyncToNextVsync;
    }

    return frameInterval;
}

double DistortionTimer::getScanoutDelay()
{
    // If Vsync is off,
    if ((RenderState->EnabledHmdCaps & ovrHmdCap_NoVSync) != 0)
        return kExpectedNoVSyncLatency;

    double vsyncToScanoutDelay = 0.;

    // If latency tester results are not available,
    if (!LatencyTester || !LatencyTester->GetVsyncToScanout(vsyncToScanoutDelay))
    {
        // Use a reasonable default post-present latency estimate.
#ifdef OVR_OS_WIN32
        vsyncToScanoutDelay = RenderState->OurHMDInfo.InCompatibilityMode ?
            RenderState->RenderInfo.Shutter.VsyncToNextVsync : kExpectedDriverLatency;
#else
        // FIXME: This is a heuristic value that may need to be better tuned later
        // as the Mac/Linux render architecture solidifies.
        vsyncToScanoutDelay = 0.0007; // Observed as 0.7 ms on Linux
#endif
    }

    // Clamp the result be zero or positive.
    if (vsyncToScanoutDelay < 0.)
        vsyncToScanoutDelay = 0;

    return vsyncToScanoutDelay;
}

#ifdef OVR_OS_WIN32

bool DistortionTimer::getDriverVsyncTime(double* previousKnownVsyncTime)
{
    // If using the driver,
    if (!RenderState->OurHMDInfo.InCompatibilityMode)
    {
        ULONG riftId = (ULONG)RenderState->OurHMDInfo.ShimInfo.DeviceNumber;
        UINT64 results[2];
        ULONG bytesReturned = 0;

        BOOL success = DeviceIoControl(DeviceHandle.Get(), IOCTL_RIFTMGR_GETCURRENTFRAMEINFO, &riftId,
            sizeof(riftId), results, sizeof(results), &bytesReturned, nullptr);

        if (success)
        {
            // Calculate Vsync time in seconds based on QPC from display driver.
            *previousKnownVsyncTime = results[1] * Timer::GetPerfFrequencyInverse();
            return true;
        }
    }

    return false;
}

#endif // OVR_OS_WIN32


//-----------------------------------------------------------------------------
// DistortionTiming : Timewarp Timing

void DistortionTimer::AddDistortionTimeMeasurement(double distortionTimeSeconds)
{
    // Accumulate the new measurement.
    DistortionRenderTimes.Add(distortionTimeSeconds);

    // If enough measurements are collected now,
    if (!NeedDistortionTimeMeasurement())
    {
        EstimatedTimewarpRenderTime = DistortionRenderTimes.GetMedian();
    }
}

void DistortionTimer::submitDisplayFrame(double frameEndTime, double frameInterval)
{
    // Get the last display frame index
    uint32_t frameIndex = TimeManager.GetLastDisplayFrameIndex();
    double lastTime     = TimeManager.GetLastDisplayFrameTime();

    // If a previous submit time was recorded,
    if (lastTime > 0.)
    {
        // Calculate number of elapsed frames since last submit
        int elapsed = (int)((frameEndTime - lastTime + frameInterval * 0.5) / frameInterval);

        frameIndex += elapsed;
    }

    // Submit this display frame to the TimeManager
    TimeManager.SubmitDisplayFrame(frameIndex, AppFrameIndex, frameEndTime);
}

void DistortionTimer::updateLastKnownVsyncTime(double previousKnownVsyncTime)
{
    // Assume the data is exact.
    LastKnownVsyncFuzzBuffer = kExactVsyncBufferTime;

    // If previous vsync time was not provided,
    if (previousKnownVsyncTime <= 0.)
    {
#ifdef OVR_OS_WIN32
        // If the display driver was not helpful,
        if (!getDriverVsyncTime(&previousKnownVsyncTime))
#endif
        {
            // Use the last fuzzy vsync time and frame index
            // Add in a fuzz factor to prevent from predicting behind a whole frame!
            previousKnownVsyncTime   = LastPresentTime;

            // The data is pretty fuzzy so increase the buffer time.
            LastKnownVsyncFuzzBuffer = kFuzzyVsyncBufferTime;
        }
    }

    // Update last known vsync time
    LastKnownVsyncTime = previousKnownVsyncTime;
}

double DistortionTimer::getJITTimewarpTime(double frameEndTime)
{
    // If there is no timing information available for the timewarp draw call,
    if (EstimatedTimewarpRenderTime <= 0.)
    {
        // Disable JIT until we have some idea how long timewarp draw call takes.
        return 0.;
    }

    // Calculate Just-in-Time timewarp time
    return frameEndTime - EstimatedTimewarpRenderTime - kJITPreemptBufferTime;
}

// Rolls the previous known vsync time forward and then checks queue-ahead conditions
void DistortionTimer::CalculateTimewarpTiming(uint32_t frameIndex, double previousKnownVsyncTime)
{
    // Update LastKnownVsyncTime from previous known vsync time.
    updateLastKnownVsyncTime(previousKnownVsyncTime);

    // Calculate the frame start time from available information.
    const double frameInterval  = getFrameInterval();
    const double frameStartTime = calculateFrameStartTime(
        Timer::GetSeconds(),
        LastKnownVsyncTime,
        LastKnownVsyncFuzzBuffer,
        frameInterval);
    const double scanoutDelay   = getScanoutDelay();

    // If Vsync is off,
    if ((RenderState->EnabledHmdCaps & ovrHmdCap_NoVSync) != 0)
    {
        // Always render for current frame start-end times.
        CurrentFrameTimewarpTiming.ScanoutTime      = frameStartTime + scanoutDelay;
        CurrentFrameTimewarpTiming.JIT_TimewarpTime = 0.; // JIT disabled when Vsync is off

        // Reset the last timewarp frame end time when Vsync is turned off.
        LastTimewarpFrameEndTime = 0.;

        // Set the reference point for the scanout delay to the frame start time when Vsync
        // is off.
        LatencyTesterPresentTime = frameStartTime;
    }
    else // Vsync is on:
    {
        // Calculate frame end time with Vsync on.
        double frameEndTime = frameStartTime + frameInterval;

        // If JIT is turned off,
        if (!(RenderState->DistortionCaps & ovrDistortionCap_TimewarpJitDelay))
        {
#ifdef OVR_SUPPORT_QUEUE_AHEAD
            // Without JIT it can render ahead a frame.
            // If Vsync is on and it targets the same end of frame time twice
            // then the second timewarp render is queued ahead a frame, as two
            // consecutive distortion renders cannot target the same frame twice.

            // If the last frame end time is about the same as this one,
            if (fabs(LastTimewarpFrameEndTime - frameEndTime) < frameInterval * 0.25)
            {
                // Skip ahead to the next frame time.
                frameEndTime += frameInterval;
            }
#endif

            // Set JIT time to zero so that if JIT is turned off after this,
            // that the JIT wait code will be skipped and timing will be right
            // for this frame.
            CurrentFrameTimewarpTiming.JIT_TimewarpTime = 0.;
        }
        else
        {
            // JIT timewarp is enabled, so provide a time estimate.
            CurrentFrameTimewarpTiming.JIT_TimewarpTime = getJITTimewarpTime(frameEndTime);
        }

        // Record the new frame end time.
        LastTimewarpFrameEndTime = frameEndTime;

        // Scanout is based on frame end time when Vsync is on due to potential queue-ahead.
        CurrentFrameTimewarpTiming.ScanoutTime = frameEndTime + scanoutDelay;

        // Update the TimeManager.
        submitDisplayFrame(frameEndTime, frameInterval);

        // Set the reference point for the scanout delay to the frame end time when Vsync
        // is on.  This way our calculations will work out where we add scanout delay to
        // get the actual scanout time from this reference point in the future.
        LatencyTesterPresentTime = frameEndTime;
    }

    // Update lockless app timing base values
    LocklessAppTimingBase appTimingBase;
    appTimingBase.FrameInterval        = frameInterval;
    appTimingBase.LastEndFrameIndex    = frameIndex;
    appTimingBase.LastStartFrameTime   = frameStartTime;
    appTimingBase.LastKnownVsyncTime   = LastKnownVsyncTime;
    appTimingBase.ScanoutDelay         = scanoutDelay;
    appTimingBase.ScreenSwitchingDelay = ScreenSwitchingDelay;
    appTimingBase.VsyncFuzzFactor      = LastKnownVsyncFuzzBuffer;
    appTimingBase.IsValid              = 1;
    LocklessAppTimingBaseUpdater.SetState(appTimingBase);

    // Get eye timewarp times
    // NOTE: Approximating scanline start-end interval with Vsync-Vsync interval here.
    CalculateEyeTimewarpTimes(
        CurrentFrameTimewarpTiming.ScanoutTime + ScreenSwitchingDelay,
        frameInterval,
        RenderState->RenderInfo.Shutter.Type,
        CurrentFrameTimewarpTiming.EyeStartEndTimes[0],
        CurrentFrameTimewarpTiming.EyeStartEndTimes[1]);
}


//-----------------------------------------------------------------------------
// AppDistortionTimer

AppRenderTimer::AppRenderTimer() :
    AppTimingBaseUpdater(nullptr)
{
}

AppRenderTimer::~AppRenderTimer()
{
}

void AppRenderTimer::GetAppTimingForIndex(AppTiming& result, bool vsyncOn, uint32_t frameIndex)
{
    /*
        This code has to handle two big cases:

            Queue-Ahead:

        In this case the application is requesting poses for an upcoming frame, which is
        very common.  We need to predict ahead potentially beyond the next frame scanout
        time to a following scanout time.

            Missed Frames:

        In this case the rendering
            (1) game physics/other code ate too much CPU time and delayed the frame, or
            (2) the render command queuing took too long, or
            (3) took too long to complete on the GPU.

        Regarding (1):
            Game code is pretty much out of the way in the case of Unity which has
            two threads: A game code thread and a render thread.  So in a real game
            engine it's mainly due to too much render complexity not CPU game logic.

        Regarding (2):
            Distortion is done after the game queues render commands, and so
            the timewarp timing calculation can get pushed off into the next frame
            and actually get timed correctly.

        So as a result judder is mainly due to GPU performance, as other other sources
        of frame drops are mitigated.
    */

    if (!IsValid())
    {
        OVR_ASSERT(false);
        result.Clear();
        return;
    }

    LocklessAppTimingBase base = AppTimingBaseUpdater->GetState();

    // If no timing data is available,
    if (!base.IsValid)
    {
        result.Clear();
        return;
    }

    int32_t deltaIndex = (int32_t)(frameIndex - base.LastEndFrameIndex);

    // Calculate the end frame time.
    // Vsync on: This is the targeted Vsync for the provided frame index.
    // Vsync off: This is the middle of the frame requested by index.
    double endFrameTime;
    if (vsyncOn)
    {
        endFrameTime = base.LastStartFrameTime + base.FrameInterval * (deltaIndex + 1);
    }
    else
    {
        endFrameTime = base.LastStartFrameTime + base.FrameInterval * 0.5;
        endFrameTime += base.FrameInterval * deltaIndex;
    }

    // If targeted Vsync is now in the past,
    const double now = Timer::GetSeconds();
    if (now + base.VsyncFuzzFactor > endFrameTime)
    {
        // Assume there is no queue-ahead, so we should target the very
        // next upcoming Vsync
        double frameStartTime = calculateFrameStartTime(now, base.LastKnownVsyncTime,
                                                        base.VsyncFuzzFactor,
                                                        base.FrameInterval);
        if (vsyncOn)
        {
            // End frame time is just one frame ahead of the frame start
            endFrameTime = frameStartTime + base.FrameInterval;
        }
        else
        {
            // End frame time is half way through the current frame
            endFrameTime = frameStartTime + base.FrameInterval * 0.5;
        }
    }

    // Add Vsync-Scanout delay to get scanout time
    double scanoutTime = endFrameTime + base.ScanoutDelay;

    // Construct app frame information object
    result.FrameInterval       = base.FrameInterval;
    result.ScanoutStartTime    = scanoutTime;
    // NOTE: Approximating scanline start-end interval with Vsync-Vsync interval here.
    result.VisibleMidpointTime = scanoutTime + base.ScreenSwitchingDelay + base.FrameInterval * 0.5;
}


//-----------------------------------------------------------------------------
// AppTimingHistory

AppTimingHistory::AppTimingHistory()
{
    Clear();
}

AppTimingHistory::~AppTimingHistory()
{
}

void AppTimingHistory::Clear()
{
    LastWriteIndex = 0;
    memset(History, 0, sizeof(History));
}

void AppTimingHistory::SetScanoutTimeForFrame(uint32_t frameIndex, double scanoutTime)
{
    if (++LastWriteIndex >= kFramesMax)
    {
        LastWriteIndex = 0;
    }

    History[LastWriteIndex].FrameIndex = frameIndex;
    History[LastWriteIndex].ScanoutTime = scanoutTime;
}

double AppTimingHistory::LookupScanoutTime(uint32_t frameIndex)
{
    // Check last written entry first
    if (History[LastWriteIndex].FrameIndex == frameIndex)
    {
        return History[LastWriteIndex].ScanoutTime;
    }

    for (int i = 0; i < kFramesMax; ++i)
    {
        if (History[i].FrameIndex == frameIndex)
        {
            return History[i].ScanoutTime;
        }
    }

    return 0.;
}


}} // namespace OVR::CAPI