aboutsummaryrefslogtreecommitdiff
path: root/src/device_context.cc
diff options
context:
space:
mode:
authorNicolas James <nj3ahxac@gmail.com>2026-03-12 20:42:37 +1100
committerNicolas James <nj3ahxac@gmail.com>2026-03-12 20:42:37 +1100
commit8ea01a571be073be00f8a77150f3d62ef5600b52 (patch)
treeab4e262cff0d591e1145e4986f2d635527f63163 /src/device_context.cc
parentf16c927ab9083ac4d0ce38ec3b62ca8677055b90 (diff)
Fix potential clock domain mismatch when using chrono::now()
Diffstat (limited to 'src/device_context.cc')
-rw-r--r--src/device_context.cc20
1 files changed, 14 insertions, 6 deletions
diff --git a/src/device_context.cc b/src/device_context.cc
index 0f606b5..99a4979 100644
--- a/src/device_context.cc
+++ b/src/device_context.cc
@@ -1,6 +1,7 @@
#include "device_context.hh"
#include "queue_context.hh"
+#include <time.h>
#include <utility>
#include <vulkan/vulkan_core.h>
@@ -36,6 +37,17 @@ DeviceContext::Clock::Clock(const DeviceContext& context) : device(context) {
DeviceContext::Clock::~Clock() {}
+DeviceContext::Clock::time_point_t DeviceContext::Clock::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 DeviceContext::Clock::calibrate() {
const auto infos = std::vector<VkCalibratedTimestampInfoKHR>{
{VK_STRUCTURE_TYPE_CALIBRATED_TIMESTAMP_INFO_EXT, nullptr,
@@ -75,14 +87,10 @@ DeviceContext::Clock::ticks_to_time(const std::uint64_t& ticks) const {
return is_negative ? -signed_abs_diff : signed_abs_diff;
}();
- // This will have issues because std::chrono::steady_clock::now(), which
- // we use for cpu time, may not be on the same time domain what was returned
- // by GetCalibratedTimestamps. It would be more robust to use the posix
- // gettime that vulkan guarantees it can be compared to instead.
-
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 + diff_nsec);
+ const auto delta = std::chrono::nanoseconds(
+ this->host_ns + static_cast<std::uint64_t>(diff_nsec));
return time_point_t{delta};
}