Monado OpenXR Runtime
m_clock_offset.h
Go to the documentation of this file.
1
// Copyright 2022, Collabora, Ltd.
2
// SPDX-License-Identifier: BSL-1.0
3
/*!
4
* @file
5
* @brief Helpers to estimate offsets between clocks
6
* @author Mateo de Mayo <mateo.demayo@collabora.com>
7
* @ingroup aux_math
8
*/
9
10
#pragma once
11
12
#include "
util/u_time.h
"
13
14
#ifdef __cplusplus
15
extern
"C"
{
16
#endif
17
18
/*!
19
* Helper to estimate the offset between two clocks using exponential smoothing.
20
*
21
* Given a sample from two timestamp domains A and B that should have been
22
* sampled as close as possible, together with an estimate of the offset between
23
* A clock and B clock (or zero), it applies a smoothing average on the
24
* estimated offset and returns @p a in B clock.
25
*
26
* @param freq About how many times per second this function is called. Helps setting a good decay value.
27
* @param a Timestamp in clock A of the event
28
* @param b Timestamp in clock B of the event
29
* @param[in,out] inout_a2b Pointer to the current offset estimate from A to B, or 0 if unknown.
30
* Value pointed-to will be updated.
31
* @return timepoint_ns @p a in B clock
32
*/
33
static
inline
timepoint_ns
34
m_clock_offset_a2b
(
float
freq,
timepoint_ns
a,
timepoint_ns
b,
time_duration_ns
*inout_a2b)
35
{
36
// This formulation of exponential filtering uses a fixed-precision integer for the
37
// alpha value and operates on the delta between the old and new a2b to avoid
38
// precision / overflow problems.
39
40
// Totally arbitrary way of computing alpha, if you have a better one, replace it
41
const
time_duration_ns
alpha = 1000 * (1.0 - 12.5 / freq);
// Weight to put on accumulated a2b
42
time_duration_ns
old_a2b = *inout_a2b;
43
time_duration_ns
got_a2b = b - a;
44
time_duration_ns
new_a2b;
45
if
(old_a2b == 0) {
// a2b has not been set yet
46
new_a2b = got_a2b;
47
}
else
{
48
new_a2b = ((old_a2b - got_a2b) * alpha) / 1000 + got_a2b;
49
}
50
*inout_a2b = new_a2b;
51
return
a + new_a2b;
52
}
53
54
#ifdef __cplusplus
55
}
56
#endif
timepoint_ns
int64_t timepoint_ns
Integer timestamp type.
Definition:
u_time.h:70
time_duration_ns
int64_t time_duration_ns
Integer duration type in nanoseconds.
Definition:
u_time.h:81
m_clock_offset_a2b
static timepoint_ns m_clock_offset_a2b(float freq, timepoint_ns a, timepoint_ns b, time_duration_ns *inout_a2b)
Helper to estimate the offset between two clocks using exponential smoothing.
Definition:
m_clock_offset.h:34
u_time.h
Time-keeping: a clock that is steady, convertible to system time, and ideally high-resolution.
auxiliary
math
m_clock_offset.h
Generated by
1.9.4