Skip to content

fix: add opengl drift correction for gpu zones#1394

Open
bruno-dasilva wants to merge 1 commit into
wolfpld:masterfrom
bruno-dasilva:bruno/opengl-drift-correction
Open

fix: add opengl drift correction for gpu zones#1394
bruno-dasilva wants to merge 1 commit into
wolfpld:masterfrom
bruno-dasilva:bruno/opengl-drift-correction

Conversation

@bruno-dasilva
Copy link
Copy Markdown

Fixes #1393

Description

Implements opengl drift correction using the existing api (similar to vulkan and d3d12).

Screenshots

Before

(no clock drift correction applied):
Screenshot 2026-06-06 170937
Later:
Screenshot 2026-06-06 170612

After:

image

Later:
image

AI Disclosure

Implemented via Claude Code, tested by me.

@bruno-dasilva bruno-dasilva force-pushed the bruno/opengl-drift-correction branch 2 times, most recently from 488c519 to 17afcee Compare June 6, 2026 21:37
@wolfpld
Copy link
Copy Markdown
Owner

wolfpld commented Jun 6, 2026

glGetInteger64v(GL_TIMESTAMP) forces CPU to GPU synchronization. This is a no go for the default configuration.

@bruno-dasilva
Copy link
Copy Markdown
Author

bruno-dasilva commented Jun 6, 2026

glGetInteger64v(GL_TIMESTAMP) forces CPU to GPU synchronization. This is a no go for the default configuration.

Thank you, good catch. I suppose there's two options:

  1. make a configurable non-default option
  2. find a way to do this without cpu<->gpu synchronization

@wolfpld
Copy link
Copy Markdown
Owner

wolfpld commented Jun 6, 2026

I don't think you would find any. Making this opt-in should be ok.

@bruno-dasilva
Copy link
Copy Markdown
Author

bruno-dasilva commented Jun 6, 2026

I don't think you would find any. Making this opt-in should be ok.

We could switch this over to a glQueryCounter maybe? the drift fix wouldn't be perfect (since it would be delayed at worst to a couple frames?) but could still be better than default? I'll try it out and report back.

Otherwise, happy to put the synchronous version behind a opt-in config. You know better than I, would that be best as a compiler flag or is there a better way to handle this config?

@wolfpld
Copy link
Copy Markdown
Owner

wolfpld commented Jun 6, 2026

Compiler flag, like the other existing config options.

@bruno-dasilva bruno-dasilva force-pushed the bruno/opengl-drift-correction branch from 17afcee to 9bb3f10 Compare June 7, 2026 00:14
@bruno-dasilva
Copy link
Copy Markdown
Author

For anyone else reading this in the future:
I did try and get the async approach to work but it's got a fundamental problem: getting a cpu timestamp whenever the gpu actually grabs the gpu timestamp. You'd need a Collect() at exactly the time the gpu has finished getting the timestamp. trying to do that within your regular frames will lead to random drifts every time you re-calibrate - worse than just letting things drift.
You could run a separate thread that polls the gpu consistently but that's frankly overkill and has its own can of worms.

So yea, I'll go with synchronous but behind a compiler flag.

@bruno-dasilva bruno-dasilva force-pushed the bruno/opengl-drift-correction branch from 9bb3f10 to e2ac8f7 Compare June 7, 2026 00:23
Comment thread manual/tracy.tex
Comment on lines +1704 to +1709
\subparagraph{Calibrated context}

By default, the OpenGL context is uncalibrated: the CPU and GPU clocks are aligned only once, when the context is created, so over long captures the two time domains may drift apart (section~\ref{options} describes correcting this drift manually). Defining \texttt{TRACY\_OPENGL\_AUTO\_CALIBRATION} before including \texttt{TracyOpenGL.hpp} enables periodic recalibration instead: roughly once per second Tracy samples the GPU and CPU clocks together and emits a calibration event, allowing the profiler to track and remove the drift automatically.

This is opt-in because OpenGL exposes no atomic CPU+GPU timestamp query (unlike Vulkan's \texttt{VK\_EXT\_calibrated\_timestamps} or Direct3D~12, whose contexts are always calibrated). Recalibration therefore reads the GPU clock with \texttt{glGetInteger64v(GL\_TIMESTAMP)}, which forces a CPU/GPU synchronization (a pipeline stall) each time it runs. Enable it only when the improved long-capture alignment is worth the periodic stall.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

feel free to edit the position/content of this to match y'alls standards - not sure if worth mentioning vulkan here for example.

@bruno-dasilva
Copy link
Copy Markdown
Author

just to confirm the alignment was correct I added a glfinish() at the end of my frames, and you can see it line up more or less perfectly:
image

@wolfpld
Copy link
Copy Markdown
Owner

wolfpld commented Jun 7, 2026

Please also add a line in meson.options.

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.

Implement OpenGL Automatic Drift Correction

2 participants