Skip to content

wineopenxr: Account for QPC to CLOCK_MONOTONIC offset in time conversion - #9372

Closed
PhialsBasement wants to merge 2 commits into
ValveSoftware:bleeding-edgefrom
PhialsBasement:bleeding-edge
Closed

PhialsBasement wants to merge 2 commits into
ValveSoftware:bleeding-edgefrom
PhialsBasement:bleeding-edge

Conversation

@PhialsBasement

Copy link
Copy Markdown
Contributor

Follow-up to #9347. Addresses the QPC to CLOCK_MONOTONIC clock mismatch by computing the offset at each conversion call as suggested by gofman.

  • Adds get_qpc_to_monotonic_offset() helper that calls NtQueryPerformanceCounter and clock_gettime(CLOCK_MONOTONIC) to compute the current offset
  • Applies offset in both wine_xrConvertWin32PerformanceCounterToTimeKHR and wine_xrConvertTimeToWin32PerformanceCounterKHR
  • Offset is computed per-call since it may drift over time

Built and tested, works as expected.

@gofman Ready for review when you have time.

@Plagman
Plagman force-pushed the bleeding-edge branch 2 times, most recently from 96c3905 to e4967d2 Compare January 7, 2026 14:55

@gofman gofman left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Looks good to me in general, apart from the cosmetic edits I suggested.

Comment thread wineopenxr/openxr.c Outdated
#define NANOSECONDS_IN_A_SECOND 1000000000
#define TICKSPERSEC 10000000

static LONGLONG get_qpc_to_monotonic_offset(void)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Let's call it just qpc_to_monotonic_offset().

Comment thread wineopenxr/openxr.c Outdated
XrTime *time) {
wine_XrInstance *wine_instance = wine_instance_from_handle(instance);
struct timespec ts;
LONGLONG monotonic_qpc, offset;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Let's remove 'offset' variable and just use qpc_to_monotonic_offset() inline, that is going to appear in one place in a function anyway.

Comment thread wineopenxr/openxr.c Outdated
/* Convert QPC ticks to timespec (Wine QPC is 10MHz = 100ns ticks). */
ts.tv_sec = performanceCounter->QuadPart / TICKSPERSEC;
ts.tv_nsec = (performanceCounter->QuadPart % TICKSPERSEC) * (NANOSECONDS_IN_A_SECOND / TICKSPERSEC);
/* Get offset between QPC and CLOCK_MONOTONIC, then convert to monotonic time. */

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I think the comment is excessive, it says almost exactly what the simple code does anyway, let's remove it.

Comment thread wineopenxr/openxr.c Outdated
ts.tv_nsec = (performanceCounter->QuadPart % TICKSPERSEC) * (NANOSECONDS_IN_A_SECOND / TICKSPERSEC);
/* Get offset between QPC and CLOCK_MONOTONIC, then convert to monotonic time. */
offset = get_qpc_to_monotonic_offset();
monotonic_qpc = performanceCounter->QuadPart - offset;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

As mentioned above, just inline qpc_to_monotonic_offset() without extra variable.

Comment thread wineopenxr/openxr.c Outdated
LARGE_INTEGER *performanceCounter) {
wine_XrInstance *wine_instance = wine_instance_from_handle(instance);
struct timespec ts;
LONGLONG monotonic_qpc, offset;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Same here, let's get rid of offset variable.

Comment thread wineopenxr/openxr.c Outdated

performanceCounter->QuadPart = (ULONGLONG)ts.tv_sec * TICKSPERSEC +
ts.tv_nsec / (NANOSECONDS_IN_A_SECOND / TICKSPERSEC);
/* Convert timespec to monotonic QPC, then apply offset to get Windows QPC. */

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Same, let's remove the comment.

@PhialsBasement

Copy link
Copy Markdown
Contributor Author

All feedback has been addressed, renamed the function, removed the intermediate variable and comments, and inlined the function calls.

The QPC value from Windows applications needs adjustment before
converting to/from XrTime, as there's an offset between Windows QPC
(which starts from system boot) and CLOCK_MONOTONIC.

Compute the offset at each conversion call by comparing the current
QPC value with CLOCK_MONOTONIC, then apply it to the conversion.
@PhialsBasement

Copy link
Copy Markdown
Contributor Author

Accidentally added some outside files not related to the commit, fixed that now.

Plagman pushed a commit that referenced this pull request Jan 7, 2026
@gofman

gofman commented Jan 7, 2026

Copy link
Copy Markdown

Thanks!

This is now in Proton Experimental (bleeding-edge branch): https://github.com/ValveSoftware/Proton/commits/experimental-bleeding-edge-10.0-291487-20260107-pbf7abd-wa21b86f-df76b0e-v9ca0b9/

I pulled that before the last update but I only pulled the related patch and not submodules bump, that had only wineopenxr changes.

@PhialsBasement

Copy link
Copy Markdown
Contributor Author

Thanks!

This is now in Proton Experimental (bleeding-edge branch): https://github.com/ValveSoftware/Proton/commits/experimental-bleeding-edge-10.0-291487-20260107-pbf7abd-wa21b86f-df76b0e-v9ca0b9/

I pulled that before the last update but I only pulled the related patch and not submodules bump, that had only wineopenxr changes.

Thanks! Happy to help with more VR stuff if anything comes up.

BananaWorks07 pushed a commit to BananaWorks07/Proton that referenced this pull request Jan 16, 2026
Plagman pushed a commit that referenced this pull request Apr 16, 2026
@MysticMia MysticMia mentioned this pull request May 18, 2026
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.

2 participants