Skip to content

Handle GPU timestamp overflow#791

Open
Poohl wants to merge 3 commits into
DiligentGraphics:masterfrom
Poohl:fix_timestamp_overflow
Open

Handle GPU timestamp overflow#791
Poohl wants to merge 3 commits into
DiligentGraphics:masterfrom
Poohl:fix_timestamp_overflow

Conversation

@Poohl
Copy link
Copy Markdown

@Poohl Poohl commented May 21, 2026

Vulkan only guarantees 36 bits of timestamp, some Intel IGPUs (Core i7-14700HX) report

 timestampValidBits          = 36
 timestampPeriod                                 = 52.0833

resulting in an overflow after just one hour.

This commit implements proper handling for an overflow while measuring time (assuming you don't have two) and loggs a warning on overflow.

@Poohl Poohl requested a review from TheMostDiligent as a code owner May 21, 2026 14:40
@TheMostDiligent
Copy link
Copy Markdown
Contributor

This change does not seem to solve the problem. The spec says:

timestampValidBits is the unsigned integer count of meaningful bits in the timestamps written via vkCmdWriteTimestamp2 or vkCmdWriteTimestamp. The valid range for the count is 36 to 64 bits, or a value of 0, indicating no support for timestamps. Bits outside the valid range are guaranteed to be zeros.

So ANDing bits with the mask should not really help much. In particular this line will not save from counter wrapping around and will return a very large value, which will incorrect. It should be clamped to zero instead.

(EndCounter - StartCounter) & ((1ull << TimestampValidBits) - 1ull);

Besides, the tests on CI fail and I expect this may happen because TimestampValidBits is 64.

This comment was marked as low quality.

@Poohl
Copy link
Copy Markdown
Author

Poohl commented Jun 1, 2026

I fixed the UB if TimestampValidBits is 64.

But the anding fixes the wrap around case, EndCounter - StartCounter will underflow (start counter is huge and end counter is small), causing all the invalid bits to become 1.

Small example with 16 bit uints and 8 valid bits:

end:      0x0003
start:    0x00FE
e-s:      0xFF05
mask:     0x00FF
out:      0x0005
             

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants