From 9649302da8c4be2ceaa28ee9f07d93e89270ecd3 Mon Sep 17 00:00:00 2001 From: Marcos Slomp Date: Wed, 1 Apr 2026 15:38:31 -0700 Subject: [PATCH] Distance utility --- public/tracy/TracyD3D12.hpp | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/public/tracy/TracyD3D12.hpp b/public/tracy/TracyD3D12.hpp index 9751ef45..f06adcf0 100644 --- a/public/tracy/TracyD3D12.hpp +++ b/public/tracy/TracyD3D12.hpp @@ -369,7 +369,8 @@ namespace tracy UINT64 gpuZoneBeginTimestamp = timestampBuffer[queryId]; UINT64 gpuZoneEndTimestamp = timestampBuffer[queryId+1]; - int64_t diff = static_cast(gpuZoneEndTimestamp - m_window.shadowBuffer[shadowIdx+1]); + UINT64 baselineTimestamp = m_window.shadowBuffer[shadowIdx+1]; + int64_t diff = Distance(baselineTimestamp, gpuZoneEndTimestamp); if (diff == 0) continue; if (diff < 0) @@ -415,13 +416,9 @@ namespace tracy } private: - // Update the window's latest known GPU timestamp only if ts is strictly "ahead" - // of the current value. Uses a signed-difference comparison to be robust against - // counter wrap-around (though UINT64 at any conceivable clock rate won't wrap - // within the lifetime of the process). tracy_force_inline void UpdateLatestKnownGpuTimestamp(UINT64 timestamp) { - int64_t diff = static_cast(timestamp - m_window.latestKnownGpuTimestamp); + int64_t diff = Distance(m_window.latestKnownGpuTimestamp, timestamp); if (diff > 0) m_window.latestKnownGpuTimestamp = timestamp; } @@ -457,9 +454,10 @@ namespace tracy return static_cast(logicalSlot % RingSize()); } - tracy_force_inline uint32_t RingCount(uint64_t begin, uint64_t end) const + tracy_force_inline static int64_t Distance(uint64_t begin, uint64_t end) { - return static_cast(end - begin); + // difference accounting for unsigned wrap-around + return static_cast(end - begin); } tracy_force_inline uint32_t NextQueryId() @@ -478,7 +476,7 @@ namespace tracy // immediately upon m_queryCounter being incremented, the late timestamp // query may be collected as if it was the timestamp of the new query... const uint64_t seqIdx = m_queryCounter.fetch_add(2, std::memory_order_relaxed); - if (RingCount(m_previousCheckpoint.load(), seqIdx) >= RingSize()) + if (Distance(m_previousCheckpoint, seqIdx) >= RingSize()) { ZoneScopedC(Color::Red4); ZoneValue(int64_t(m_contextId));