allocation-less history
This commit is contained in:
+40
-77
@@ -21,17 +21,15 @@
|
|||||||
|
|
||||||
#include "clock_offset.h"
|
#include "clock_offset.h"
|
||||||
#include "os/os_time.h"
|
#include "os/os_time.h"
|
||||||
#include "util/u_logging.h"
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <list>
|
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <openxr/openxr.h>
|
#include <openxr/openxr.h>
|
||||||
|
|
||||||
namespace wivrn
|
namespace wivrn
|
||||||
{
|
{
|
||||||
|
|
||||||
template <typename Derived, typename Data, XrDuration extrapolation = 0, size_t MaxSamples = 10>
|
template <typename Derived, typename Data, size_t MaxSamples = 10>
|
||||||
class history
|
class history
|
||||||
{
|
{
|
||||||
struct TimedData : public Data
|
struct TimedData : public Data
|
||||||
@@ -39,9 +37,9 @@ class history
|
|||||||
XrTime produced_timestamp;
|
XrTime produced_timestamp;
|
||||||
XrTime at_timestamp_ns;
|
XrTime at_timestamp_ns;
|
||||||
};
|
};
|
||||||
|
std::array<TimedData, MaxSamples> data{};
|
||||||
|
|
||||||
std::mutex mutex;
|
std::mutex mutex;
|
||||||
std::list<TimedData> data;
|
|
||||||
XrTime last_request;
|
XrTime last_request;
|
||||||
XrTime last_produced;
|
XrTime last_produced;
|
||||||
|
|
||||||
@@ -59,43 +57,26 @@ protected:
|
|||||||
bool active = produced - last_request < 1'000'000'000;
|
bool active = produced - last_request < 1'000'000'000;
|
||||||
last_produced = std::max(produced, last_produced);
|
last_produced = std::max(produced, last_produced);
|
||||||
|
|
||||||
// Discard outdated data, packets could be reordered
|
TimedData * target = data.data();
|
||||||
if (not data.empty())
|
if (offset)
|
||||||
{
|
{
|
||||||
// keep only one sample if the clock_offset is unreliable
|
for (auto & item: data)
|
||||||
if (not offset)
|
|
||||||
{
|
{
|
||||||
U_LOG_T("not using history: clock_offset not stable");
|
// Discard reordered packets
|
||||||
data.clear();
|
if (item.produced_timestamp > produced)
|
||||||
data.emplace_back(sample, produced, t);
|
return active;
|
||||||
return active;
|
|
||||||
|
if (std::abs(item.at_timestamp_ns - t) < 2'000'000)
|
||||||
|
{
|
||||||
|
target = &item;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (item.at_timestamp_ns < target->at_timestamp_ns)
|
||||||
|
target = &item;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data.back().produced_timestamp > produced)
|
|
||||||
return active;
|
|
||||||
}
|
}
|
||||||
|
*target = TimedData(sample, produced, t);
|
||||||
// Discard outdated predictions
|
|
||||||
for (auto it = data.begin(); it != data.end();)
|
|
||||||
{
|
|
||||||
if (std::abs(it->at_timestamp_ns - t) < 2'000'000 and it->produced_timestamp < produced)
|
|
||||||
it = data.erase(it);
|
|
||||||
else
|
|
||||||
++it;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Insert the new sample
|
|
||||||
auto it = std::lower_bound(data.begin(), data.end(), t, [](TimedData & sample, int64_t t) { return sample.at_timestamp_ns < t; });
|
|
||||||
if (it == data.end())
|
|
||||||
data.emplace_back(sample, produced, t);
|
|
||||||
else if (it->at_timestamp_ns == t)
|
|
||||||
*it = TimedData(sample, produced, t);
|
|
||||||
else
|
|
||||||
data.emplace(it, sample, produced, t);
|
|
||||||
|
|
||||||
while (data.size() > MaxSamples)
|
|
||||||
data.pop_front();
|
|
||||||
|
|
||||||
return active;
|
return active;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -107,57 +88,39 @@ public:
|
|||||||
|
|
||||||
last_request = os_monotonic_get_ns();
|
last_request = os_monotonic_get_ns();
|
||||||
|
|
||||||
if (data.empty())
|
TimedData * before = nullptr;
|
||||||
{
|
TimedData * after = nullptr;
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
if (at_timestamp_ns - data.back().at_timestamp_ns > 1'000'000'000)
|
for (auto & item: data)
|
||||||
{
|
{
|
||||||
// stale data
|
if (not item.at_timestamp_ns)
|
||||||
data.clear();
|
continue;
|
||||||
return {};
|
if (item.at_timestamp_ns < at_timestamp_ns)
|
||||||
}
|
|
||||||
|
|
||||||
if (data.size() == 1)
|
|
||||||
{
|
|
||||||
return {ex, data.front()};
|
|
||||||
}
|
|
||||||
|
|
||||||
if (data.front().at_timestamp_ns > at_timestamp_ns)
|
|
||||||
{
|
|
||||||
if constexpr (extrapolation)
|
|
||||||
{
|
{
|
||||||
auto second = data.begin();
|
if (not before or before->at_timestamp_ns < item.at_timestamp_ns)
|
||||||
auto first = second++;
|
before = &item;
|
||||||
at_timestamp_ns = std::max(at_timestamp_ns, data.front().at_timestamp_ns - extrapolation);
|
|
||||||
return {ex, Derived::extrapolate(*first, *second, first->at_timestamp_ns, second->at_timestamp_ns, at_timestamp_ns)};
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
return {ex, data.front()};
|
|
||||||
}
|
|
||||||
|
|
||||||
for (auto after = data.begin(), before = after++; after != data.end(); before = after++)
|
|
||||||
{
|
|
||||||
if (after->at_timestamp_ns > at_timestamp_ns)
|
|
||||||
{
|
{
|
||||||
float t = float(after->at_timestamp_ns - at_timestamp_ns) /
|
if (not after or after->at_timestamp_ns > item.at_timestamp_ns)
|
||||||
(after->at_timestamp_ns - before->at_timestamp_ns);
|
after = &item;
|
||||||
return {ex, Derived::interpolate(*before, *after, t)};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if constexpr (extrapolation)
|
if (before and after)
|
||||||
{
|
{
|
||||||
auto prev = data.rbegin();
|
float t = float(after->at_timestamp_ns - at_timestamp_ns) /
|
||||||
auto last = prev++;
|
(after->at_timestamp_ns - before->at_timestamp_ns);
|
||||||
at_timestamp_ns = std::min(at_timestamp_ns, data.back().at_timestamp_ns + extrapolation);
|
return {ex, Derived::interpolate(*before, *after, t)};
|
||||||
return {ex, Derived::extrapolate(*prev, *last, prev->at_timestamp_ns, last->at_timestamp_ns, at_timestamp_ns)};
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return {ex, data.back()};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (before)
|
||||||
|
return {ex, *before};
|
||||||
|
|
||||||
|
if (after)
|
||||||
|
return {ex, *after};
|
||||||
|
|
||||||
|
return {};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
} // namespace wivrn
|
} // namespace wivrn
|
||||||
|
|||||||
Reference in New Issue
Block a user