diff options
| author | Nicolas James <nj3ahxac@gmail.com> | 2026-03-29 20:44:23 +1100 |
|---|---|---|
| committer | Nicolas James <nj3ahxac@gmail.com> | 2026-03-29 20:44:23 +1100 |
| commit | 681bd5096ee416b50dd7338de30af7b3db385a36 (patch) | |
| tree | 358b6bc6f9a3af66729b8ac3b15dd38cc0f4bd2a /src/device_clock.cc | |
| parent | d5ef2dbbd77c69dd93e92d5b7046a65c2361b59b (diff) | |
Implement Reflex - break AntiLag in the process. Remove AntiLag1. WIP
Diffstat (limited to 'src/device_clock.cc')
| -rw-r--r-- | src/device_clock.cc | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/src/device_clock.cc b/src/device_clock.cc new file mode 100644 index 0000000..52c86d3 --- /dev/null +++ b/src/device_clock.cc @@ -0,0 +1,73 @@ +#include "device_clock.hh" +#include "device_context.hh" + +#include <vulkan/vulkan_core.h> + +#include <cassert> +#include <time.h> + +namespace low_latency { + +DeviceClock::DeviceClock(const DeviceContext& context) : device(context) { + this->calibrate(); +} + +DeviceClock::~DeviceClock() {} + +DeviceClock::time_point_t DeviceClock::now() { + auto ts = timespec{}; + if (clock_gettime(CLOCK_MONOTONIC, &ts)) { + throw errno; + } + + return time_point_t{std::chrono::seconds{ts.tv_sec} + + std::chrono::nanoseconds{ts.tv_nsec}}; +} + +void DeviceClock::calibrate() { + const auto infos = std::vector<VkCalibratedTimestampInfoKHR>{ + {VK_STRUCTURE_TYPE_CALIBRATED_TIMESTAMP_INFO_EXT, nullptr, + VK_TIME_DOMAIN_DEVICE_EXT}, + {VK_STRUCTURE_TYPE_CALIBRATED_TIMESTAMP_INFO_EXT, nullptr, + VK_TIME_DOMAIN_CLOCK_MONOTONIC_EXT}}; + + struct CalibratedResult { + std::uint64_t device; + std::uint64_t host; + }; + auto calibrated_result = CalibratedResult{}; + + THROW_NON_VKSUCCESS(device.vtable.GetCalibratedTimestampsKHR( + device.device, 2, std::data(infos), &calibrated_result.device, + &this->error_bound)); + + this->device_ticks = calibrated_result.device; + this->host_ns = calibrated_result.host; +} + +DeviceClock::time_point_t +DeviceClock::ticks_to_time(const std::uint64_t& ticks) const { + const auto& pd = device.physical_device.properties; + const auto ns_tick = static_cast<double>(pd->limits.timestampPeriod); + + const auto diff = [&]() -> auto { + auto a = this->device_ticks; + auto b = ticks; + const auto is_negative = a > b; + if (is_negative) { + std::swap(a, b); + } + const auto abs_diff = b - a; + assert(abs_diff <= std::numeric_limits<std::int64_t>::max()); + const auto signed_abs_diff = static_cast<std::int64_t>(abs_diff); + return is_negative ? -signed_abs_diff : signed_abs_diff; + }(); + + const auto diff_nsec = + static_cast<std::int64_t>(static_cast<double>(diff) * ns_tick + 0.5); + const auto delta = std::chrono::nanoseconds( + this->host_ns + static_cast<std::uint64_t>(diff_nsec)); + return time_point_t{delta}; +} + +} // namespace low_latency
\ No newline at end of file |
