Skip to content

SwapChain: added "PRESENT_TIMING" and "GetPresentTimings" - #333

Merged
dzhdanNV merged 11 commits into
NVIDIA-RTX:mainfrom
Nullpoint56:swapchain-present-timing
Dec 2, 2026
Merged

dzhdanNV merged 11 commits into
NVIDIA-RTX:mainfrom
Nullpoint56:swapchain-present-timing

Conversation

@Nullpoint56

@Nullpoint56 Nullpoint56 commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

A frame loop that paces by the CPU time between its frames stutters even at a steady frame rate. Frames are shown at vblanks, and the loop can't see when a frame actually reached the screen (Alen Ladavac, "The Elusive Frame Timing: A Case Study for Smoothness Over Speed", GDC 2018). QueuePresent already takes a presentId, but nothing comes back for it. Knowing when each tracked presentation was displayed, and the refresh cycle, lets an application step its simulation by displayed time, notice missed vblanks and choose its own pacing. NRI adds no policy.

What I added

  • SwapChainBits::PRESENT_TIMING (requires features.presentTiming) and GetPresentTimings: non-blocking, returns per tracked presentation the time its first pixel left for the display (in the CPU clock of GetCalibratedTimestamps) and the refresh cycle.
  • VK: VK_EXT_present_timing uses VK_KHR_present_id2; presentation waiting uses VK_KHR_present_wait2. ID/wait support is queried per surface. The per-stage clock is converted to the CPU clock with calibrated timestamps and recovers when presentation clock domains change. Wrapped swap chains drop the timing flag. The legacy ID/wait extensions are not used as a fallback.
  • D3D12: GetFrameStatistics, at most one presentation per call.
  • METAL: MTLDrawable.presentedTime callbacks, converted to the uptime nanoseconds used by GetCalibratedTimestamps. A bounded ordered report queue has callback-owned state that can outlive the swap chain and device; it never retains drawables. Refresh duration and variable-refresh status are unknown.
  • D3D11 and WGPU: unsupported. NONE returns no timings.
  • No target time: the DXGI swap chain NRI uses has none, and NVIDIA's Vulkan driver supports only relative targets. Windows 11's composition swapchain has target times and present statistics (it accepts a D3D12 device), but it presents into a DirectComposition surface rather than an HWND, so it would be a separate swap chain path; out of scope here.

Validation

  • Windows x64 Debug and Release builds pass for D3D11, D3D12, Vulkan, WGPU, NONE, and Validation, with both Agility SDK and non-Agility configurations. Non-Agility builds select and include Windows SDK 10.0.20348 headers.
  • Temporary regression harnesses compile the actual methods with controlled native responses: D3D12 timing passes 12 checks, Vulkan timing passes 12 checks, and the ID2/WAIT2 migration passes 25 checks at b85c7fce. The Vulkan checks include an oversized capacity of 1024 while native query and scratch capacities remain bounded to 64. The original PR head reproduces three D3D12 failures and four Vulkan timing failures.
  • A local, uncommitted NRISamples sample exercises zero and sparse IDs, zero capacity, bounded draining, ordering, duplicates, timestamp plausibility, delayed polling, and queue saturation with native API and NRI validation enabled. D3D11 reports unsupported as expected.
  • Native Vulkan runs pass with API and NRI validation enabled: timing without WAITABLE returns 198 non-zero display timestamps; wait-only presentation completes 112 waits; timing-queue saturation with WAITABLE returns 64 valid display timestamps and completes 225 waits. Basic Vulkan presentation also passes on Intel.
  • Native D3D12 timing now passes on both the original and current PR (48 and 57 valid display timestamps respectively), without WAITABLE or DwmFlush. The earlier all-zero native statistics could not be reproduced; their historical trigger remains unproven.
  • Controlled Windows deployment tests reproduced zero raw Vulkan stage/calibration timestamps when the AgilitySDK directory selected by this sample executable's exports was absent. Restoring the matching runtime bundle recovered timings on both the original and current PR. These verification runs use the complete build output; this is an observed deployment dependency, not a general Vulkan API requirement.
  • Metal implementation has been reviewed against Apple documentation but has not been compiled or run on this Windows host. Real multi-display transitions remain untested.
  • Diff whitespace checks pass and changed source files preserve CRLF.

"QueuePresent" takes a "presentId", but nothing reports when that presentation reached the display,
so a frame loop can't pace by the time frames are shown. "SwapChainBits::PRESENT_TIMING" (requires
"features.presentTiming") unlocks "GetPresentTimings": per tracked presentation, the time its first
pixel left for the display in the CPU clock of "GetCalibratedTimestamps", and the refresh cycle.

VK: "VK_EXT_present_timing" + "VK_KHR_present_id2" + "VK_KHR_calibrated_timestamps". The only time
domain NVIDIA advertises is "PRESENT_STAGE_LOCAL", a clock per stage, so the first-pixel-out time is
converted with "vkGetCalibratedTimestampsKHR". A full timing queue fails the present, so timing is
requested only while a slot is free. Wrapped swap chains drop the flag. D3D12: "GetFrameStatistics",
matched to presentations by present count. Other backends: unsupported.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 1, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-02T00:21:05.661324Z 2b1990c New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c22985f26e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread Source/D3D12/SwapChainD3D12.hpp Resolved
Comment thread Source/VK/DeviceVK.hpp Resolved
D3D12: a disjoint frame-statistics sample also resets the measured refresh cycle, which may have
changed with it. VK: the calibrated timestamps functions are loaded by their KHR names when only
"VK_KHR_calibrated_timestamps" is enabled, so the CPU time domain (and "features.presentTiming") no
longer depends on the EXT extension.

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 691208791f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread Source/D3D12/SwapChainD3D12.hpp Resolved
…IMING"

Two threads presenting the same swap chain could both record the newer present count: the count
was sampled after "Present", outside any lock. With "PRESENT_TIMING" a dedicated lock now covers
"Present", "GetLastPresentCount" and the mapping update; "GetPresentTimings" still takes only the
short mapping lock, so it never waits behind a blocking "Present". Other swap chains are unchanged.

dzhdanNV commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator

Added five follow-up commits to address a few cases where valid presentations could produce missing or incorrect timing feedback:

  • b5319b0 — D3D12 timestamp arithmetic: DXGI’s clock sample can precede the actual presentation. The old unsigned subtraction could wrap and turn an ordinary queued frame into a nonsensical timestamp.
  • c3615ce — Vulkan surface support: Device support for present IDs does not guarantee support on every surface. Timing needs that surface capability too, otherwise enabling it can lead to invalid native calls.
  • e7b294e — Vulkan clock changes: Moving a window between displays can change its presentation clock, and feedback may use a fallback clock. Keeping the creation-time clock forever could lose timings indefinitely or use an unavailable calibration.
  • 6cbe34c — Metal support: Metal already exposes presentation feedback, so applications should be able to use it through NRI too. Callback-owned storage lets feedback finish safely after swapchain destruction without retaining drawables and exhausting the drawable pool.
  • 4471b57 — Concurrent D3D12 polling: A statistics sample and its cached-state update need to stay together. Otherwise concurrent queries around a mode change can process stale samples in the wrong order and corrupt the refresh-period estimate.

Kept VK_KHR_present_id because the existing present-wait path depends on it; dropping it would reduce compatibility.

Windows builds and 23 controlled regression checks pass. Actual display-timestamp verification and Metal platform testing remain incomplete.

@dzhdanNV

dzhdanNV commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator

Thanks for your work. We are still improving...

@Nullpoint56

Copy link
Copy Markdown
Contributor Author

Thanks for your work. We are still improving...

Thanks for the quick reaction and for the followup commits too, they were necessary. Improving NRI is cool : )

dzhdanNV commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator

8ce9384 migrates NRI to VK_KHR_present_id2 + VK_KHR_present_wait2 and removes the legacy path.

The main reason is the capability model: support depends on the actual surface, so checking it for each swapchain fits NRI’s independent devices and swapchains better. Present timing already uses ID2, making one consistent path simpler to maintain.

Android adoption matters too: the old wait extension had effectively zero coverage—0.61% in the recent-report query, versus 11.04% for wait2.

We’re deliberately starting clean, accepting the lower reported Windows/Linux coverage. If users encounter concrete compatibility gaps, we can add a fallback then.

The README and native-wrapper requirements are updated. Debug/Release builds and 25 migration regression checks pass; native Vulkan tests also passed waiting without timing and waiting through a full timing queue, including 64 valid display timestamps.

@dzhdanNV

dzhdanNV commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator

I think we are good to go now. Thanks!

@dzhdanNV
dzhdanNV merged commit 560996c into NVIDIA-RTX:main Dec 2, 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