From 05a03bee21bcdc594347c1575cbd603d28ddc143 Mon Sep 17 00:00:00 2001 From: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com> Date: Fri, 13 Nov 2026 21:22:28 -0500 Subject: [PATCH 1/2] Frame pacing groundwork: hardware flip queue probe and a two-route pacing test Refs #96. Read-only groundwork for frame generation presentation; no product code changes. - clean/fg/hwfq_probe.cpp asks the graphics kernel, through the documented D3DKMTQueryAdapterInfo queries, whether hardware GPU scheduling and the hardware flip queue are enabled for each adapter. It creates no device. - clean/fg/fg_pacing_test.cpp presents alternating "real" and "stand-in" frames at a fixed target interval through two public routes and reports how evenly they reach the screen from each route's own statistics: the composition swapchain with SetTargetTime (route A, Direct3D 11 displayable surfaces) and a DXGI flip-model swap chain on Direct3D 12 with a frame-latency waitable object (route B). It first prints read-only display context. - clean/fg/HWFQ-FINDINGS.md: what the test PC reports, what Microsoft's documentation says the routes need, what AMD's public frame generation documentation states about its pacing, and the first runs. Those runs gave no on-screen times, because no live monitor was attached; no interval figures are reported yet. Signed-off-by: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com> --- clean/fg/HWFQ-FINDINGS.md | 142 +++++++ clean/fg/fg_pacing_test.cpp | 719 ++++++++++++++++++++++++++++++++++++ clean/fg/hwfq_probe.cpp | 115 ++++++ 3 files changed, 976 insertions(+) create mode 100644 clean/fg/HWFQ-FINDINGS.md create mode 100644 clean/fg/fg_pacing_test.cpp create mode 100644 clean/fg/hwfq_probe.cpp diff --git a/clean/fg/HWFQ-FINDINGS.md b/clean/fg/HWFQ-FINDINGS.md new file mode 100644 index 00000000..302198bc --- /dev/null +++ b/clean/fg/HWFQ-FINDINGS.md @@ -0,0 +1,142 @@ +# Hardware flip queue and frame pacing: findings (read only) + +Status: groundwork for frame generation presentation and pacing (Refs #96). Nothing here claims that frame generation +works, how well it paces, or when it will ship. Every statement is either a quotation of public documentation or what +a read-only query or our own test program printed on one test PC (a Radeon RX 9070 XT). No driver, registry, display +or system setting was changed for any of it. + +## 1. Is the hardware flip queue active on the test PC? + +Yes, as far as the graphics kernel reports it. + +`clean/fg/hwfq_probe.cpp` (in this pull request) asks the graphics kernel through the documented `D3DKMTEnumAdapters2` +/ `D3DKMTQueryAdapterInfo` interface. It creates no device and does no GPU work. For the Radeon RX 9070 XT it printed: + + KMTQAITYPE_DRIVERVERSION: status 0x00000000, value 0xc80 + KMTQAITYPE_WDDM_2_9_CAPS: status 0x00000000, Value 0x00000006, HwSchSupportState 2 (STABLE), HwSchEnabled 1 + KMTQAITYPE_WDDM_3_0_CAPS: status 0x00000000, Value 0x0000000e, HwFlipQueueSupportState 2 (STABLE), HwFlipQueueEnabled 1, DisplayableSupported 1 + KMTQAITYPE_FLIPQUEUEINFO: status 0x00000000, MaxHardwareFlipQueueLength 0, MaxSoftwareFlipQueueLength 31, FlipFlags.FlipInterval 0 + +- Microsoft documents `D3DKMT_WDDM_3_0_CAPS.HwFlipQueueEnabled` as "Specifies whether the hardware flip queue is + currently enabled for this GPU" ([D3DKMT_WDDM_3_0_CAPS](https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/d3dkmdt/ns-d3dkmdt-d3dkmt_wddm_3_0_caps)). + The query type is `KMTQAITYPE_WDDM_3_0_CAPS`, "Supported starting with Windows 11, version 21H2" + ([KMTQUERYADAPTERINFOTYPE](https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/d3dkmthk/ne-d3dkmthk-_kmtqueryadapterinfotype)). +- `KMTQAITYPE_FLIPQUEUEINFO` is the older flip queue structure. The hardware flip queue's own driver capability is + `MaxHwQueuedFlips` in `DXGK_DRIVERCAPS`, which user mode does not read. So the `MaxHardwareFlipQueueLength 0` above + does not say anything about the hardware flip queue either way. +- The same PC, from `dxdiag /t` (read only): "Card name: AMD Radeon RX 9070 XT", "Driver Version: 32.0.32015.7029", + "Driver Date/Size: 10/17/2026", "Driver Model: WDDM 3.2", "Hardware Scheduling: DriverSupportState:Stable + Enabled:True", "Current Mode: 1920 x 1080 (32 bit) (60Hz)", "MPO Caps: Not Supported". +- Windows: `ver` printed "Microsoft Windows [Version 10.0.26200.9974]"; the registry's DisplayVersion is 25H2. +- The graphics drivers registry key (read only) holds `HwSchMode` = 0x2 (hardware-accelerated GPU scheduling on). + +What Microsoft says the feature needs ([Hardware flip queue](https://learn.microsoft.com/en-us/windows-hardware/drivers/display/hardware-flip-queue), +page dated 2025-04-24): +- "supported starting in Windows 11 (WDDM 3.0)". +- "Windows currently requires GPU hardware scheduling to be enabled in order for basic hardware flip queue to be enabled + on officially released drivers." +- "Advanced hardware flip queue requires both basic hardware flip queue and GPU hardware scheduling stage 2 + functionality to be present." The query above does not say whether the advanced form is in use. + +## 2. Which public presentation route gives target present times? + +- **Composition swapchain** (`presentation.h`: `IPresentationFactory`, `IPresentationManager`, + `IPresentationManager::SetTargetTime`, present statistics) + ([programming guide](https://learn.microsoft.com/en-us/windows/win32/comp_swapchain/comp-swapchain)). + - "a present can also have a *target time*, which is a system-relative timestamp (an interrupt-time) that describes + the ideal time that the present should be shown". + - Requirements: "GPU drivers supporting WDDM (Windows Device Driver Model) 2.0, and Windows 11 (Build + 10.0.22000.194) or greater. To use the composition swapchain API in the most performant way (direct scanout and + independent flip, or *iflip*), systems will need GPU drivers supporting WDDM 3.0." + - Surfaces must be allocated displayable to be eligible for independent flip. Microsoft documents that only for + Direct3D 11 surfaces. + - "If there are multiple *ready* presents, all but the latest ... will be *skipped*". +- **The hardware flip queue itself** takes a target time per queued flip ([Hardware flip queue](https://learn.microsoft.com/en-us/windows-hardware/drivers/display/hardware-flip-queue)). + - "When the OS submits a flip request to the hardware flip queue, it also sends the target flip time." + - "The OS automatically converts existing interval-based Present API requests to timestamp-based flip calls." + - For fixed refresh, the OS computes the target from the previous VSync and the present interval, minus half a + refresh period. +- **Plain DXGI flip-model swap chains** (`IDXGISwapChain4`, interval-based `Present`) have no app-chosen target time. + On a hardware-flip-queue system the OS turns their intervals into target times, per the page above. + +Which of the two routes lands evenly on this hardware is what `fg_pacing_test` is for (section 4). It has not been +measured yet. + +## 3. What AMD states about its own frame generation pacing (public sources only) + +From the GPUOpen manual "AMD FSR Frame Generation (Swapchain)" +(). The page's sidebar says FSR +SDK 2.4.0; it covers technique version 4.0.0, dated 2026-10-28 in its version history: +- Pacing: + - "The AMD FSR Frame Generation (Swapchain) handles frame pacing automatically." + - Starting with 4.0.0, "the final presentation timing can be delegated to the AMD display driver instead of being + paced by the swapchain itself". AMD calls this "Driver-Assisted Frame Pacing", "selected automatically when the + driver reports support for it". Otherwise "the swapchain transparently falls back to its own pacing". +- Requirements, from the page's table: "AMD Software: Adrenalin Edition driver version 26.10.1 or later" and "AMD + Radeon RX 9000 Series or later (AMD RDNA 4 architecture or later)". It is "optimized for Windows 11 with + Hardware-accelerated GPU scheduling enabled". The page gives no Windows build number. +- Conditions: + - "Exclusive fullscreen, or borderless fullscreen covering the display area"; + - VSync "Disabled"; + - "Presenting within the variable refresh rate window of the display"; + - "Switching to windowed presentation, enabling VSync, or presenting outside the VRR window" makes the swapchain + fall back. + - "VRR and tearing are only available in fullscreen mode." +- Swap chain: "On DirectX 12 it implements the `IDXGISwapChain4` interface"; the page advises + `GetFrameLatencyWaitableObject`. +- The page does not mention the hardware flip queue. These notes therefore record AMD's term, "driver-assisted frame + pacing", and make no claim about which display mechanism the driver uses for it. +- Whether this PC's driver 32.0.32015.7029 is Adrenalin 26.10.1 or later is not established here. AMD's 26.10.1 + release notes did not load when we tried them. + +Nothing from AMD's SDK is used in this pull request. + +## 4. The test programs and what they printed + +- `clean/fg/hwfq_probe.cpp`: section 1. +- `clean/fg/fg_pacing_test.cpp`: presents alternating "real" and "stand-in" frames (two low-contrast greys) at a fixed + target interval through two routes. It reports how evenly they reach the screen, using each route's own statistics: + - route A, the composition swapchain: a Direct3D 11 device, displayable surfaces, `SetTargetTime` per present, and + the PresentStatus / CompositionFrame / IndependentFlipFrame statistics; + - route B, Direct3D 12 with a DXGI flip-model swap chain: `IDXGISwapChain4`, `FLIP_DISCARD`, a frame-latency + waitable object, `Present(1, 0)` and `GetFrameStatistics`. + + Before presenting, it prints read-only context: the display mode and exact refresh period, the output's EDID identity, + tearing support, the console display state, and DWM's completed frames over one second. + +Build both from an x64 Native Tools prompt: + + cl /nologo /W4 /EHsc /std:c++17 /O2 hwfq_probe.cpp gdi32.lib dxgi.lib + cl /nologo /W4 /EHsc /std:c++17 /O2 fg_pacing_test.cpp d3d11.lib d3d12.lib dxgi.lib dcomp.lib user32.lib advapi32.lib mincore.lib + +### Runs on the test PC (2026-11-13): no on-screen times, because no live monitor was attached + +- 13 runs of 5 seconds each, each in its own locked GPU slot: route A and route B, windowed and borderless; for + route A also QPC versus interrupt-time targets and `ForceVSyncInterrupt(true)`. +- Every run exited 0. No run produced an on-screen interval, so this pull request reports no interval numbers. +- Route A, every run: + - The program printed "IsPresentationSupported 1, IsPresentationSupportedWithIndependentFlip 1" and + "D3D11_FEATURE_DISPLAYABLE DisplayableTexture 1". + - It then printed "statistics received: independent-flip 0, composition-frame 0, present-status 300", and per + present "skipped 200" (199 in some runs) with the rest queued. In the runs that printed per-present statistics, each composition frame took + three presents and showed the last ("skipped", "skipped", "queued"). + - Statistics for successive composition frames arrived about 77 to 80 ms apart. +- Route B, every run: `GetFrameStatistics` returned S_OK with PresentCount 0, SyncRefreshCount 0 and SyncQPCTime 0. It + printed "no statistic 300". A 5-second route B run (300 `Present(1, 0)` calls) took 16.5 to 17.5 s in its slot. +- DWM, before the test window drew: "completed composition frames" rose by 8 to 11 per second, with "framePeriod + 166666", that is 16.67 ms in 100 ns units. +- The output, read only: + - `QueryDisplayConfig` gave target "", output technology 10 (DisplayPort, external), "EDID ids valid 0, manufacturer + 0x0000 product 0x0000". + - `dxdiag` showed no monitor name or model, all colour primaries and luminance values 0, and "HDR Support: Unknown". + - `GUID_CONSOLE_DISPLAY_STATE` reported 1 (on). That is the power setting, not the panel. + - The session was the console session, not remote and not locked. The GPU engines were idle. +- Our reading, **inferred, not proven**: the DisplayPort monitor was off or in deep sleep (the owner was away). + - Without scanout VSync, DWM runs on a slow timer, nothing can be independent-flipped, and DXGI has no refresh + counts. That matches every line above. + - A process without administrator rights cannot read the panel's power state directly, and nothing was elevated or + changed to find out. + +**Not tested:** on-screen frame-to-frame evenness (mean, standard deviation, p99, misses) for either route. It needs a +live monitor (or a display emulator with an EDID). The same short runs will be repeated then, and their numbers added +with their records. diff --git a/clean/fg/fg_pacing_test.cpp b/clean/fg/fg_pacing_test.cpp new file mode 100644 index 00000000..53b54fb3 --- /dev/null +++ b/clean/fg/fg_pacing_test.cpp @@ -0,0 +1,719 @@ +// SPDX-License-Identifier: MIT +// fg_pacing_test: presents alternating "real" and "stand-in" frames at a fixed target interval and measures how evenly +// they reach the screen, through two public Windows presentation routes: +// +// route A composition swapchain (presentation.h): one present per frame with an app-chosen target time +// (IPresentationManager::SetTargetTime). On-screen times come from the API's present statistics: +// IIndependentFlipFramePresentStatistics::GetDisplayedTime when the present was independent-flipped, and +// for composed presents the composition frame's present time (DCompositionGetStatistics / +// DCompositionGetTargetStatistics). Displayable surfaces are a Direct3D 11 feature, so this route renders +// with a D3D11 device. +// route B DXGI flip model on Direct3D 12 (IDXGISwapChain4, DXGI_SWAP_EFFECT_FLIP_DISCARD, frame-latency waitable +// object, Present(1, 0)). On-screen times come from IDXGISwapChain::GetFrameStatistics. +// +// The two frame kinds are two low-contrast greys (no strobing). Nothing is installed or changed; the program only +// opens a window for the run. Output: one summary block on stdout and a CSV of per-present times (--out). +// Written for this project from Microsoft's public API documentation; no third-party code. +// +// usage: fg_pacing_test --route A|B [--mode window|borderless] [--seconds S] [--interval-refreshes N] [--out file.csv] +// [--timebase it|qpc] [--force-vsync-interrupt] [--verbose] +// It also prints read-only context first: display mode and refresh period, the output's EDID identity, tearing +// support, the console display state, and DWM's completed frames over one second. On-screen times need a live +// monitor: without one (no EDID, no scanout VSync) the APIs return no display times and the summary says so. +#define NOMINMAX +#define _CRT_SECURE_NO_WARNINGS +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +using Microsoft::WRL::ComPtr; + +namespace { + +struct Options { + char route = 'A'; + bool borderless = false; + double seconds = 5.0; + int interval_refreshes = 1; + bool qpc_targets = false; // --timebase qpc: pass QPC values to SetTargetTime instead of interrupt time + bool force_vsync = false; // --force-vsync-interrupt: IPresentationManager::ForceVSyncInterrupt(true) + bool verbose = false; // --verbose: the first presents' raw statistics and waits + std::string out; +}; + +struct Sample { // one present + UINT64 id = 0; + int kind = 0; // 0 real, 1 stand-in + double target_ms = 0; // requested time (route A) or present call time (route B), ms on the run's clock + double shown_ms = -1; // on-screen time from the API, ms on the same clock; -1 unknown + std::string how; // "iflip", "composed", "skipped", "canceled", "dxgi" +}; + +double g_qpc_ms = 0; // ms per QPC tick +UINT64 g_t0_qpc = 0; +ULONGLONG g_t0_it = 0; // interrupt time (100 ns) at the same moment + +double qpc_to_ms(UINT64 q) { return (static_cast(q) - static_cast(g_t0_qpc)) * g_qpc_ms; } +double it_to_ms(ULONGLONG t) { return (static_cast(t) - static_cast(g_t0_it)) / 10000.0; } + +void init_clocks() { + LARGE_INTEGER f, c; + QueryPerformanceFrequency(&f); + g_qpc_ms = 1000.0 / static_cast(f.QuadPart); + ULONGLONG it; + QueryInterruptTimePrecise(&it); + QueryPerformanceCounter(&c); + g_t0_it = it; + g_t0_qpc = static_cast(c.QuadPart); +} + +bool g_quit = false; +int g_display_state = -1; // GUID_CONSOLE_DISPLAY_STATE: 0 off, 1 on, 2 dimmed; -1 not reported +LRESULT CALLBACK wndproc(HWND h, UINT m, WPARAM w, LPARAM l) { + if (m == WM_POWERBROADCAST && w == PBT_POWERSETTINGCHANGE) { + const POWERBROADCAST_SETTING *ps = reinterpret_cast(l); + if (ps && IsEqualGUID(ps->PowerSetting, GUID_CONSOLE_DISPLAY_STATE) && ps->DataLength >= sizeof(DWORD)) + g_display_state = static_cast(*reinterpret_cast(ps->Data)); + return TRUE; + } + if (m == WM_CLOSE || (m == WM_KEYDOWN && w == VK_ESCAPE)) { + g_quit = true; + return 0; + } + return DefWindowProcW(h, m, w, l); +} + +void pump() { + MSG msg; + while (PeekMessageW(&msg, nullptr, 0, 0, PM_REMOVE)) { + TranslateMessage(&msg); + DispatchMessageW(&msg); + } +} + +HWND make_window(bool borderless, bool no_redirection, RECT *client) { + WNDCLASSW wc = {}; + wc.lpfnWndProc = wndproc; + wc.hInstance = GetModuleHandleW(nullptr); + wc.lpszClassName = L"wg_fg_pacing_test"; + wc.hCursor = LoadCursor(nullptr, IDC_ARROW); + RegisterClassW(&wc); + HMONITOR mon = MonitorFromPoint(POINT{0, 0}, MONITOR_DEFAULTTOPRIMARY); + MONITORINFO mi = {sizeof(mi)}; + GetMonitorInfoW(mon, &mi); + // Topmost: the program is started from a background process and may not get the foreground; an occluded window's + // presents are throttled by the system, which would measure the occlusion, not the presentation route. + DWORD ex = (no_redirection ? WS_EX_NOREDIRECTIONBITMAP : 0) | WS_EX_TOPMOST; + RECT r = mi.rcMonitor; + DWORD style = WS_POPUP; + if (!borderless) { + style = WS_OVERLAPPEDWINDOW; + r = RECT{mi.rcWork.left + 64, mi.rcWork.top + 64, mi.rcWork.left + 64 + 1280, mi.rcWork.top + 64 + 720}; + AdjustWindowRectEx(&r, style, FALSE, ex); + } + HWND h = CreateWindowExW(ex, wc.lpszClassName, L"WrongGPU FG pacing test (Esc ends)", style, r.left, r.top, + r.right - r.left, r.bottom - r.top, nullptr, nullptr, wc.hInstance, nullptr); + ShowWindow(h, SW_SHOW); + UpdateWindow(h); + GetClientRect(h, client); + return h; +} + +ComPtr pick_adapter() { + ComPtr f; + CreateDXGIFactory2(0, IID_PPV_ARGS(&f)); + ComPtr a; + for (UINT i = 0; f->EnumAdapterByGpuPreference(i, DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE, IID_PPV_ARGS(&a)) != + DXGI_ERROR_NOT_FOUND; + ++i) { + DXGI_ADAPTER_DESC1 d; + a->GetDesc1(&d); + if (!(d.Flags & DXGI_ADAPTER_FLAG_SOFTWARE)) return a; + } + return nullptr; +} + +// Read-only context: display mode, tearing support, and the user's DirectX graphics preferences string. +double print_context(HWND hwnd, const Options &o) { + HMONITOR mon = MonitorFromWindow(hwnd, MONITOR_DEFAULTTOPRIMARY); + MONITORINFOEXW mi = {}; + mi.cbSize = sizeof(mi); + GetMonitorInfoW(mon, &mi); + DEVMODEW dm = {}; + dm.dmSize = sizeof(dm); + EnumDisplaySettingsW(mi.szDevice, ENUM_CURRENT_SETTINGS, &dm); + std::printf("display: %ld x %ld, %lu Hz (EnumDisplaySettings), window mode %s\n", + static_cast(dm.dmPelsWidth), static_cast(dm.dmPelsHeight), + static_cast(dm.dmDisplayFrequency), o.borderless ? "borderless covering the display" : "windowed 1280x720"); + // The exact refresh period from DisplayConfig (read only). + double period_ms = dm.dmDisplayFrequency ? 1000.0 / dm.dmDisplayFrequency : 1000.0 / 60.0; + UINT32 np = 0, nm = 0; + if (GetDisplayConfigBufferSizes(QDC_ONLY_ACTIVE_PATHS, &np, &nm) == ERROR_SUCCESS) { + std::vector paths(np); + std::vector modes(nm); + if (QueryDisplayConfig(QDC_ONLY_ACTIVE_PATHS, &np, paths.data(), &nm, modes.data(), nullptr) == ERROR_SUCCESS) { + for (UINT32 i = 0; i < np; ++i) { + DISPLAYCONFIG_SOURCE_DEVICE_NAME sn = {}; + sn.header.type = DISPLAYCONFIG_DEVICE_INFO_GET_SOURCE_NAME; + sn.header.size = sizeof(sn); + sn.header.adapterId = paths[i].sourceInfo.adapterId; + sn.header.id = paths[i].sourceInfo.id; + if (DisplayConfigGetDeviceInfo(&sn.header) != ERROR_SUCCESS || wcscmp(sn.viewGdiDeviceName, mi.szDevice)) + continue; + DISPLAYCONFIG_TARGET_DEVICE_NAME tn = {}; + tn.header.type = DISPLAYCONFIG_DEVICE_INFO_GET_TARGET_NAME; + tn.header.size = sizeof(tn); + tn.header.adapterId = paths[i].targetInfo.adapterId; + tn.header.id = paths[i].targetInfo.id; + if (DisplayConfigGetDeviceInfo(&tn.header) == ERROR_SUCCESS) + std::printf("display: target \"%ls\", output technology %d, EDID ids valid %u, manufacturer 0x%04x " + "product 0x%04x, friendly name from EDID %u\n", + tn.monitorFriendlyDeviceName, static_cast(tn.outputTechnology), + tn.flags.edidIdsValid, tn.edidManufactureId, tn.edidProductCodeId, + tn.flags.friendlyNameFromEdid); + const DISPLAYCONFIG_RATIONAL &rr = paths[i].targetInfo.refreshRate; + if (rr.Numerator && rr.Denominator) { + period_ms = 1000.0 * rr.Denominator / rr.Numerator; + std::printf("display: refresh %u/%u Hz = %.4f Hz (QueryDisplayConfig), period %.4f ms\n", + rr.Numerator, rr.Denominator, static_cast(rr.Numerator) / rr.Denominator, + period_ms); + } + } + } + } + ComPtr f5; + BOOL tearing = FALSE; + if (SUCCEEDED(CreateDXGIFactory2(0, IID_PPV_ARGS(&f5)))) + f5->CheckFeatureSupport(DXGI_FEATURE_PRESENT_ALLOW_TEARING, &tearing, sizeof(tearing)); + std::printf("dxgi: DXGI_FEATURE_PRESENT_ALLOW_TEARING = %d\n", tearing); + wchar_t buf[512] = {}; + DWORD sz = sizeof(buf); + LSTATUS ls = RegGetValueW(HKEY_CURRENT_USER, L"Software\\Microsoft\\DirectX\\UserGpuPreferences", + L"DirectXUserGlobalSettings", RRF_RT_REG_SZ, nullptr, buf, &sz); + std::printf("registry (read): HKCU UserGpuPreferences DirectXUserGlobalSettings = %ls\n", + ls == ERROR_SUCCESS ? buf : L"(not set)"); + std::printf("vrr: no public API reports whether variable refresh is active on this output; see the two lines " + "above for what can be read\n"); + // The console display state: Windows sends the current value at once after registration (read only). + HPOWERNOTIFY pn = RegisterPowerSettingNotification(hwnd, &GUID_CONSOLE_DISPLAY_STATE, DEVICE_NOTIFY_WINDOW_HANDLE); + for (int k = 0; k < 20 && g_display_state < 0; ++k) { + pump(); + Sleep(10); + } + if (pn) UnregisterPowerSettingNotification(pn); + std::printf("display state (GUID_CONSOLE_DISPLAY_STATE): %d (0 off, 1 on, 2 dimmed, -1 not reported)\n", + g_display_state); + // DWM's own frame rate over one second (DCompositionGetFrameId / DCompositionGetStatistics, read only). + COMPOSITION_FRAME_ID f0 = 0, f1 = 0; + LARGE_INTEGER c0, c1; + QueryPerformanceCounter(&c0); + const HRESULT h0 = DCompositionGetFrameId(COMPOSITION_FRAME_ID_COMPLETED, &f0); + Sleep(1000); + const HRESULT h1 = DCompositionGetFrameId(COMPOSITION_FRAME_ID_COMPLETED, &f1); + QueryPerformanceCounter(&c1); + COMPOSITION_FRAME_STATS fst = {}; + COMPOSITION_TARGET_ID tids[4] = {}; + UINT ntid = 0; + const HRESULT h2 = DCompositionGetStatistics(f1, &fst, 4, tids, &ntid); + std::printf("dwm: completed composition frames %llu -> %llu over %.1f ms (hr 0x%08lx 0x%08lx); last frame stats hr " + "0x%08lx startTime %llu targetTime %llu framePeriod %llu, targets %u\n", + static_cast(f0), static_cast(f1), + qpc_to_ms(static_cast(c1.QuadPart)) - qpc_to_ms(static_cast(c0.QuadPart)), h0, h1, h2, + static_cast(fst.startTime), static_cast(fst.targetTime), + static_cast(fst.framePeriod), ntid); + return period_ms; +} + +// ------------------------------------------------------------------------------------------------------------------- +// route A: composition swapchain +int route_a(const Options &o, std::vector &samples, double &period_ms) { + RECT client; + HWND hwnd = make_window(o.borderless, true, &client); + period_ms = print_context(hwnd, o); + const UINT w = client.right - client.left, h = client.bottom - client.top; + + ComPtr adapter = pick_adapter(); + ComPtr dev; + ComPtr ctx; + HRESULT hr = D3D11CreateDevice(adapter.Get(), D3D_DRIVER_TYPE_UNKNOWN, nullptr, D3D11_CREATE_DEVICE_BGRA_SUPPORT, + nullptr, 0, D3D11_SDK_VERSION, &dev, nullptr, &ctx); + if (FAILED(hr)) { std::printf("route A: D3D11CreateDevice failed 0x%08lx\n", hr); return 3; } + + ComPtr factory; + hr = CreatePresentationFactory(dev.Get(), IID_PPV_ARGS(&factory)); + if (FAILED(hr)) { std::printf("route A: CreatePresentationFactory failed 0x%08lx\n", hr); return 3; } + const bool supported = factory->IsPresentationSupported() != 0; + const bool iflip_supported = factory->IsPresentationSupportedWithIndependentFlip() != 0; + std::printf("route A: IsPresentationSupported %d, IsPresentationSupportedWithIndependentFlip %d\n", supported, + iflip_supported); + if (!supported) return 4; + + BOOL displayable_ok = FALSE; + { + ComPtr d5; + D3D11_FEATURE_DATA_DISPLAYABLE fd = {}; + if (SUCCEEDED(dev.As(&d5)) && SUCCEEDED(d5->CheckFeatureSupport(D3D11_FEATURE_DISPLAYABLE, &fd, sizeof(fd)))) + displayable_ok = fd.DisplayableTexture; + } + std::printf("route A: D3D11_FEATURE_DISPLAYABLE DisplayableTexture %d\n", displayable_ok); + + ComPtr pm; + hr = factory->CreatePresentationManager(&pm); + if (FAILED(hr)) { std::printf("route A: CreatePresentationManager failed 0x%08lx\n", hr); return 3; } + + const int kBuffers = 4; + std::vector> tex(kBuffers); + std::vector> rtv(kBuffers); + std::vector> pbuf(kBuffers); + std::vector avail(kBuffers); + for (int i = 0; i < kBuffers; ++i) { + D3D11_TEXTURE2D_DESC td = {}; + td.Width = w; + td.Height = h; + td.MipLevels = 1; + td.ArraySize = 1; + td.Format = DXGI_FORMAT_B8G8R8A8_UNORM; + td.SampleDesc.Count = 1; + td.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE; + td.MiscFlags = D3D11_RESOURCE_MISC_SHARED | D3D11_RESOURCE_MISC_SHARED_NTHANDLE | + (displayable_ok ? D3D11_RESOURCE_MISC_SHARED_DISPLAYABLE : 0); + hr = dev->CreateTexture2D(&td, nullptr, &tex[i]); + if (FAILED(hr)) { std::printf("route A: CreateTexture2D failed 0x%08lx\n", hr); return 3; } + dev->CreateRenderTargetView(tex[i].Get(), nullptr, &rtv[i]); + hr = pm->AddBufferFromResource(tex[i].Get(), &pbuf[i]); + if (FAILED(hr)) { std::printf("route A: AddBufferFromResource failed 0x%08lx\n", hr); return 3; } + pbuf[i]->GetAvailableEvent(&avail[i]); + } + + HANDLE surface_handle = nullptr; + hr = DCompositionCreateSurfaceHandle(COMPOSITIONOBJECT_ALL_ACCESS, nullptr, &surface_handle); + if (FAILED(hr)) { std::printf("route A: DCompositionCreateSurfaceHandle failed 0x%08lx\n", hr); return 3; } + ComPtr surf; + hr = pm->CreatePresentationSurface(surface_handle, &surf); + if (FAILED(hr)) { std::printf("route A: CreatePresentationSurface failed 0x%08lx\n", hr); return 3; } + surf->SetAlphaMode(DXGI_ALPHA_MODE_IGNORE); + surf->SetColorSpace(DXGI_COLOR_SPACE_RGB_FULL_G22_NONE_P709); + + ComPtr dcomp; + hr = DCompositionCreateDevice3(nullptr, IID_PPV_ARGS(&dcomp)); + if (FAILED(hr)) { std::printf("route A: DCompositionCreateDevice3 failed 0x%08lx\n", hr); return 3; } + ComPtr target; + dcomp->CreateTargetForHwnd(hwnd, TRUE, &target); + ComPtr visual; + dcomp->CreateVisual(&visual); + ComPtr dsurf; + hr = dcomp->CreateSurfaceFromHandle(surface_handle, &dsurf); + if (FAILED(hr)) { std::printf("route A: CreateSurfaceFromHandle failed 0x%08lx\n", hr); return 3; } + visual->SetContent(dsurf.Get()); + target->SetRoot(visual.Get()); + dcomp->Commit(); + + if (o.force_vsync) { + const HRESULT fh = pm->ForceVSyncInterrupt(true); + std::printf("route A: ForceVSyncInterrupt(true) hr 0x%08lx\n", fh); + } + pm->EnablePresentStatisticsKind(PresentStatisticsKind_PresentStatus, true); + pm->EnablePresentStatisticsKind(PresentStatisticsKind_CompositionFrame, true); + pm->EnablePresentStatisticsKind(PresentStatisticsKind_IndependentFlipFrame, true); + HANDLE stats_event = nullptr; + pm->GetPresentStatisticsAvailableEvent(&stats_event); + + const double interval_ms = period_ms * o.interval_refreshes; + const int frames = static_cast(o.seconds * 1000.0 / interval_ms); + ULONGLONG start_it; + QueryInterruptTimePrecise(&start_it); + LARGE_INTEGER qf, qn; + QueryPerformanceFrequency(&qf); + QueryPerformanceCounter(&qn); + std::printf("route A: clocks now: QueryInterruptTimePrecise %llu, QueryPerformanceCounter %lld at %lld Hz; " + "target time base %s\n", + start_it, qn.QuadPart, qf.QuadPart, o.qpc_targets ? "QPC" : "interrupt time"); + // Target times in 100 ns units on the chosen time base; it_to_ms / qpc_to_ms map them back to the run clock. + const double ticks_per_ms = o.qpc_targets ? static_cast(qf.QuadPart) / 1000.0 : 10000.0; + const ULONGLONG base_now = o.qpc_targets ? static_cast(qn.QuadPart) : start_it; + const ULONGLONG first_target = base_now + static_cast(200.0 * ticks_per_ms); // 200 ms from now + auto target_to_ms = [&](ULONGLONG t) { return o.qpc_targets ? qpc_to_ms(t) : it_to_ms(t); }; + auto now_ticks = [&]() -> ULONGLONG { + if (o.qpc_targets) { + LARGE_INTEGER c; + QueryPerformanceCounter(&c); + return static_cast(c.QuadPart); + } + ULONGLONG it; + QueryInterruptTimePrecise(&it); + return it; + }; + int n_debug = 0; + std::map index; + int n_iflip_stats = 0, n_comp_stats = 0, n_status_stats = 0; + auto drain = [&]() { + for (;;) { + ComPtr st; + if (pm->GetNextPresentStatistics(&st) != S_OK || !st) break; + const UINT64 id = st->GetPresentId(); + auto it = index.find(id); + if (it == index.end()) continue; + Sample &s = samples[it->second]; + switch (st->GetKind()) { + case PresentStatisticsKind_IndependentFlipFrame: { + ComPtr fs; + if (SUCCEEDED(st.As(&fs))) { + s.shown_ms = it_to_ms(fs->GetDisplayedTime().value); + s.how = "iflip"; + ++n_iflip_stats; + } + break; + } + case PresentStatisticsKind_CompositionFrame: { + ComPtr cs; + if (SUCCEEDED(st.As(&cs))) { + ++n_comp_stats; + COMPOSITION_FRAME_ID fid = cs->GetCompositionFrameId(); + COMPOSITION_FRAME_STATS fst = {}; + COMPOSITION_TARGET_ID tids[4] = {}; + UINT ntid = 0; + if (SUCCEEDED(DCompositionGetStatistics(fid, &fst, 4, tids, &ntid)) && ntid > 0) { + COMPOSITION_TARGET_STATS ts = {}; + if (SUCCEEDED(DCompositionGetTargetStatistics(fid, &tids[0], &ts)) && ts.presentTime && + s.how != "iflip") { + s.shown_ms = qpc_to_ms(ts.presentTime); + s.how = "composed"; + } + } + } + break; + } + case PresentStatisticsKind_PresentStatus: { + ComPtr ps; + if (SUCCEEDED(st.As(&ps))) { + ++n_status_stats; + const PresentStatus v = ps->GetPresentStatus(); + if (o.verbose && n_debug < 12) { + ++n_debug; + std::printf("route A: debug present %llu status %d composition frame %llu target %.3f ms " + "now %.3f ms\n", + static_cast(id), static_cast(v), + static_cast(ps->GetCompositionFrameId()), s.target_ms, + target_to_ms(now_ticks())); + } + if (v == PresentStatus_Skipped) s.how = "skipped"; + if (v == PresentStatus_Canceled) s.how = "canceled"; + } + break; + } + default: break; + } + } + }; + + const float grey[2][4] = {{0.20f, 0.20f, 0.22f, 1.0f}, {0.24f, 0.24f, 0.26f, 1.0f}}; + const int kAhead = 3; // presents queued ahead of the display + for (int i = 0; i < frames && !g_quit; ++i) { + pump(); + drain(); + const int b = i % kBuffers; + const ULONGLONG w0 = now_ticks(); + WaitForSingleObject(avail[b], 1000); + if (o.verbose && i < 12) + std::printf("route A: debug frame %d buffer %d waited %.3f ms for it, now %.3f ms\n", i, b, + (now_ticks() - w0) / ticks_per_ms, target_to_ms(now_ticks())); + ctx->ClearRenderTargetView(rtv[b].Get(), grey[i & 1]); + ctx->Flush(); + surf->SetBuffer(pbuf[b].Get()); + const ULONGLONG t = first_target + static_cast(i * interval_ms * ticks_per_ms); + pm->SetTargetTime(SystemInterruptTime{t}); + Sample s; + s.id = pm->GetNextPresentId(); + s.kind = i & 1; + s.target_ms = target_to_ms(t); + index[s.id] = samples.size(); + samples.push_back(s); + hr = pm->Present(); + if (FAILED(hr)) { std::printf("route A: Present failed 0x%08lx at frame %d\n", hr, i); break; } + // Throttle: stay at most kAhead presents ahead of the target clock. + for (;;) { + const ULONGLONG now = now_ticks(); + if (t <= now + static_cast(kAhead * interval_ms * ticks_per_ms)) break; + WaitForSingleObject(stats_event, 2); + drain(); + pump(); + } + } + const DWORD end = GetTickCount() + 1500; // let the last statistics arrive + while (GetTickCount() < end) { + WaitForSingleObject(stats_event, 20); + drain(); + pump(); + } + std::printf("route A: statistics received: independent-flip %d, composition-frame %d, present-status %d\n", + n_iflip_stats, n_comp_stats, n_status_stats); + DestroyWindow(hwnd); + return 0; +} + +// ------------------------------------------------------------------------------------------------------------------- +// route B: DXGI flip model on D3D12 +int route_b(const Options &o, std::vector &samples, double &period_ms) { + RECT client; + HWND hwnd = make_window(o.borderless, false, &client); + period_ms = print_context(hwnd, o); + const UINT w = client.right - client.left, h = client.bottom - client.top; + + ComPtr adapter = pick_adapter(); + ComPtr dev; + HRESULT hr = D3D12CreateDevice(adapter.Get(), D3D_FEATURE_LEVEL_12_0, IID_PPV_ARGS(&dev)); + if (FAILED(hr)) { std::printf("route B: D3D12CreateDevice failed 0x%08lx\n", hr); return 3; } + D3D12_COMMAND_QUEUE_DESC qd = {}; + qd.Type = D3D12_COMMAND_LIST_TYPE_DIRECT; + ComPtr queue; + dev->CreateCommandQueue(&qd, IID_PPV_ARGS(&queue)); + + ComPtr f; + CreateDXGIFactory2(0, IID_PPV_ARGS(&f)); + const UINT kBuffers = 3; + DXGI_SWAP_CHAIN_DESC1 sd = {}; + sd.Width = w; + sd.Height = h; + sd.Format = DXGI_FORMAT_B8G8R8A8_UNORM; + sd.SampleDesc.Count = 1; + sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; + sd.BufferCount = kBuffers; + sd.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD; + sd.Flags = DXGI_SWAP_CHAIN_FLAG_FRAME_LATENCY_WAITABLE_OBJECT; + ComPtr sc1; + hr = f->CreateSwapChainForHwnd(queue.Get(), hwnd, &sd, nullptr, nullptr, &sc1); + if (FAILED(hr)) { std::printf("route B: CreateSwapChainForHwnd failed 0x%08lx\n", hr); return 3; } + f->MakeWindowAssociation(hwnd, DXGI_MWA_NO_ALT_ENTER); + ComPtr sc; + sc1.As(&sc); + sc->SetMaximumFrameLatency(2); + HANDLE waitable = sc->GetFrameLatencyWaitableObject(); + + D3D12_DESCRIPTOR_HEAP_DESC hd = {}; + hd.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV; + hd.NumDescriptors = kBuffers; + ComPtr heap; + dev->CreateDescriptorHeap(&hd, IID_PPV_ARGS(&heap)); + const UINT inc = dev->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_RTV); + std::vector> bufs(kBuffers); + for (UINT i = 0; i < kBuffers; ++i) { + sc->GetBuffer(i, IID_PPV_ARGS(&bufs[i])); + D3D12_CPU_DESCRIPTOR_HANDLE hnd = heap->GetCPUDescriptorHandleForHeapStart(); + hnd.ptr += static_cast(i) * inc; + dev->CreateRenderTargetView(bufs[i].Get(), nullptr, hnd); + } + std::vector> alloc(kBuffers); + for (auto &a : alloc) dev->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&a)); + ComPtr cl; + dev->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, alloc[0].Get(), nullptr, IID_PPV_ARGS(&cl)); + cl->Close(); + ComPtr fence; + dev->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence)); + HANDLE fev = CreateEventW(nullptr, FALSE, FALSE, nullptr); + std::vector fval(kBuffers, 0); + UINT64 next_fence = 1; + + ComPtr media; + const bool have_media = SUCCEEDED(sc.As(&media)); + std::map mode_count; + + // GetFrameStatistics returns, for the latest present that reached the screen, its PresentCount, the refresh it + // was shown at (PresentRefreshCount) and the latest VSync's refresh count and QPC time. A present's on-screen time + // is SyncQPCTime - (SyncRefreshCount - PresentRefreshCount) periods. + const double interval_refreshes = o.interval_refreshes; + const int frames = static_cast(o.seconds * 1000.0 / (period_ms * interval_refreshes)); + const float grey[2][4] = {{0.20f, 0.20f, 0.22f, 1.0f}, {0.24f, 0.24f, 0.26f, 1.0f}}; + std::map by_present_count; + int stats_fail = 0; + auto read_stats = [&]() { + DXGI_FRAME_STATISTICS fs = {}; + const HRESULT shr = sc->GetFrameStatistics(&fs); + static int n_debug_b = 0; + if (o.verbose && n_debug_b < 15) { + ++n_debug_b; + UINT last = 0; + sc->GetLastPresentCount(&last); + std::printf("route B: debug GetFrameStatistics hr 0x%08lx PresentCount %u PresentRefreshCount %u " + "SyncRefreshCount %u SyncQPCTime %lld; GetLastPresentCount %u\n", + shr, fs.PresentCount, fs.PresentRefreshCount, fs.SyncRefreshCount, fs.SyncQPCTime.QuadPart, + last); + } + if (FAILED(shr)) { ++stats_fail; return; } + auto it = by_present_count.find(fs.PresentCount); + if (it == by_present_count.end()) return; + Sample &s = samples[it->second]; + if (s.shown_ms >= 0) return; + const double sync_ms = qpc_to_ms(static_cast(fs.SyncQPCTime.QuadPart)); + s.shown_ms = sync_ms - static_cast(fs.SyncRefreshCount - fs.PresentRefreshCount) * period_ms; + s.how = "dxgi"; + if (have_media) { + DXGI_FRAME_STATISTICS_MEDIA fm = {}; + if (SUCCEEDED(media->GetFrameStatisticsMedia(&fm))) { + const char *m = fm.CompositionMode == DXGI_FRAME_PRESENTATION_MODE_COMPOSED ? "composed" + : fm.CompositionMode == DXGI_FRAME_PRESENTATION_MODE_OVERLAY ? "overlay" + : fm.CompositionMode == DXGI_FRAME_PRESENTATION_MODE_NONE ? "none" + : fm.CompositionMode == DXGI_FRAME_PRESENTATION_MODE_COMPOSITION_FAILURE ? "failure" + : "?"; + mode_count[m]++; + } + } + }; + + for (int i = 0; i < frames && !g_quit; ++i) { + pump(); + WaitForSingleObjectEx(waitable, 1000, TRUE); + const UINT b = sc->GetCurrentBackBufferIndex(); + if (fence->GetCompletedValue() < fval[b]) { + fence->SetEventOnCompletion(fval[b], fev); + WaitForSingleObject(fev, 1000); + } + alloc[b]->Reset(); + cl->Reset(alloc[b].Get(), nullptr); + D3D12_RESOURCE_BARRIER br = {}; + br.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION; + br.Transition.pResource = bufs[b].Get(); + br.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES; + br.Transition.StateBefore = D3D12_RESOURCE_STATE_PRESENT; + br.Transition.StateAfter = D3D12_RESOURCE_STATE_RENDER_TARGET; + cl->ResourceBarrier(1, &br); + D3D12_CPU_DESCRIPTOR_HANDLE hnd = heap->GetCPUDescriptorHandleForHeapStart(); + hnd.ptr += static_cast(b) * inc; + cl->ClearRenderTargetView(hnd, grey[i & 1], 0, nullptr); + std::swap(br.Transition.StateBefore, br.Transition.StateAfter); + cl->ResourceBarrier(1, &br); + cl->Close(); + ID3D12CommandList *lists[] = {cl.Get()}; + queue->ExecuteCommandLists(1, lists); + LARGE_INTEGER now; + QueryPerformanceCounter(&now); + hr = sc->Present(o.interval_refreshes, 0); + if (FAILED(hr)) { std::printf("route B: Present failed 0x%08lx at frame %d\n", hr, i); break; } + fval[b] = next_fence; + queue->Signal(fence.Get(), next_fence++); + UINT pc = 0; + sc->GetLastPresentCount(&pc); + Sample s; + s.id = pc; + s.kind = i & 1; + s.target_ms = qpc_to_ms(static_cast(now.QuadPart)); + by_present_count[pc] = samples.size(); + samples.push_back(s); + read_stats(); + } + for (int k = 0; k < 10; ++k) { + Sleep(static_cast(period_ms) + 1); + read_stats(); + } + std::printf("route B: GetFrameStatistics failures %d (DXGI_ERROR_FRAME_STATISTICS_DISJOINT is normal at start)\n", + stats_fail); + std::printf("route B: IDXGISwapChainMedia %s;", have_media ? "available" : "not available"); + for (auto &kv : mode_count) std::printf(" CompositionMode %s x%d;", kv.first.c_str(), kv.second); + std::printf("\n"); + // Drain the queue before releasing. + queue->Signal(fence.Get(), next_fence); + fence->SetEventOnCompletion(next_fence, fev); + WaitForSingleObject(fev, 2000); + DestroyWindow(hwnd); + return 0; +} + +// ------------------------------------------------------------------------------------------------------------------- +void summarize(const Options &o, const std::vector &samples, double period_ms) { + const double target = period_ms * o.interval_refreshes; + std::vector shown; + std::map how; + for (const Sample &s : samples) { + how[s.how.empty() ? "no statistic" : s.how]++; + if (s.shown_ms >= 0) shown.push_back(&s); + } + std::sort(shown.begin(), shown.end(), [](const Sample *a, const Sample *b) { return a->id < b->id; }); + // Skip the first 10 presents (start-up). + std::vector iv; + int misses = 0, repeats = 0; + for (size_t i = 11; i < shown.size(); ++i) { + if (shown[i]->id != shown[i - 1]->id + 1) continue; // only consecutive presents + const double d = shown[i]->shown_ms - shown[i - 1]->shown_ms; + iv.push_back(d); + if (std::fabs(d - target) > 0.5 * period_ms) ++misses; + if (d < 0.5 * period_ms) ++repeats; + } + std::printf("summary: route %c, mode %s, target interval %.4f ms (%d refresh(es) of %.4f ms), presents %zu\n", + o.route, o.borderless ? "borderless" : "window", target, o.interval_refreshes, period_ms, + samples.size()); + std::printf("summary: per-present result:"); + for (auto &kv : how) std::printf(" %s %d;", kv.first.c_str(), kv.second); + std::printf("\n"); + if (iv.empty()) { + std::printf("summary: no consecutive on-screen times, so no interval statistics\n"); + return; + } + double mean = 0; + for (double d : iv) mean += d; + mean /= iv.size(); + double var = 0; + for (double d : iv) var += (d - mean) * (d - mean); + const double sd = std::sqrt(var / iv.size()); + std::vector sorted = iv; + std::sort(sorted.begin(), sorted.end()); + auto pct = [&](double p) { return sorted[std::min(sorted.size() - 1, static_cast(p * sorted.size()))]; }; + std::printf("summary: on-screen interval over %zu consecutive pairs: mean %.4f ms, sd %.4f ms, p01 %.4f, p50 %.4f, " + "p99 %.4f, min %.4f, max %.4f ms; misses (|interval - target| > half a refresh) %d; shown within half " + "a refresh of the previous %d\n", + iv.size(), mean, sd, pct(0.01), pct(0.5), pct(0.99), sorted.front(), sorted.back(), misses, repeats); + if (!o.out.empty()) { + FILE *f = std::fopen(o.out.c_str(), "w"); + if (f) { + std::fprintf(f, "present_id,kind,target_or_call_ms,shown_ms,how\n"); + for (const Sample &s : samples) + std::fprintf(f, "%llu,%s,%.4f,%.4f,%s\n", static_cast(s.id), + s.kind ? "standin" : "real", s.target_ms, s.shown_ms, s.how.c_str()); + std::fclose(f); + } + } +} + +} // namespace + +int main(int argc, char **argv) { + Options o; + for (int i = 1; i < argc; ++i) { + const std::string a = argv[i]; + auto next = [&]() { return i + 1 < argc ? std::string(argv[++i]) : std::string(); }; + if (a == "--route") o.route = static_cast(std::toupper(next()[0])); + else if (a == "--mode") o.borderless = next() == "borderless"; + else if (a == "--seconds") o.seconds = std::atof(next().c_str()); + else if (a == "--interval-refreshes") o.interval_refreshes = std::max(1, std::atoi(next().c_str())); + else if (a == "--out") o.out = next(); + else if (a == "--timebase") o.qpc_targets = next() == "qpc"; + else if (a == "--force-vsync-interrupt") o.force_vsync = true; + else if (a == "--verbose") o.verbose = true; + else { + std::printf("usage: fg_pacing_test --route A|B [--mode window|borderless] [--seconds S] " + "[--interval-refreshes N] [--out file.csv] [--timebase it|qpc] [--force-vsync-interrupt] " + "[--verbose]\n"); + return 1; + } + } + o.seconds = std::min(std::max(o.seconds, 1.0), 30.0); + SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2); + init_clocks(); + std::vector samples; + double period_ms = 1000.0 / 60.0; + int rc = o.route == 'A' ? route_a(o, samples, period_ms) : route_b(o, samples, period_ms); + if (rc) return rc; + summarize(o, samples, period_ms); // period_ms: the exact refresh period print_context read + return 0; +} diff --git a/clean/fg/hwfq_probe.cpp b/clean/fg/hwfq_probe.cpp new file mode 100644 index 00000000..e3f15a8e --- /dev/null +++ b/clean/fg/hwfq_probe.cpp @@ -0,0 +1,115 @@ +// SPDX-License-Identifier: MIT +// hwfq_probe: prints, for every display adapter, what the graphics kernel reports about hardware GPU scheduling and the +// hardware flip queue. Read only: it opens adapters through the documented D3DKMT query interface (gdi32), reads +// capability structures and closes them again. It creates no device, changes no setting and does no GPU work. +// +// Sources (public Microsoft documentation): +// D3DKMTEnumAdapters2 / D3DKMTQueryAdapterInfo / KMTQUERYADAPTERINFOTYPE (d3dkmthk.h) +// D3DKMT_WDDM_2_9_CAPS (HwSchSupportState, HwSchEnabled), D3DKMT_WDDM_3_0_CAPS (HwFlipQueueSupportState, +// HwFlipQueueEnabled), D3DKMT_WDDM_3_1_CAPS, D3DKMT_FLIPQUEUEINFO, D3DKMT_DRIVERVERSION (d3dkmdt.h / d3dkmthk.h) +// "Hardware flip queue", Windows driver documentation. +// +// Build (x64 Native Tools prompt): cl /nologo /W4 /EHsc /std:c++17 hwfq_probe.cpp gdi32.lib dxgi.lib +#include +#include +#include +#include +#include +#include + +namespace { + +const char *support_state(UINT s) { + switch (s) { + case 0: return "ALWAYS_OFF"; + case 1: return "EXPERIMENTAL"; + case 2: return "STABLE"; + case 3: return "ALWAYS_ON"; + } + return "?"; +} + +template NTSTATUS query(D3DKMT_HANDLE h, KMTQUERYADAPTERINFOTYPE type, T *out) { + D3DKMT_QUERYADAPTERINFO q = {}; + q.hAdapter = h; + q.Type = type; + q.pPrivateDriverData = out; + q.PrivateDriverDataSize = sizeof(T); + return D3DKMTQueryAdapterInfo(&q); +} + +void print_dxgi_name(LUID luid) { + IDXGIFactory1 *f = nullptr; + if (FAILED(CreateDXGIFactory1(__uuidof(IDXGIFactory1), reinterpret_cast(&f)))) return; + IDXGIAdapter1 *a = nullptr; + for (UINT i = 0; f->EnumAdapters1(i, &a) != DXGI_ERROR_NOT_FOUND; ++i) { + DXGI_ADAPTER_DESC1 d = {}; + a->GetDesc1(&d); + if (d.AdapterLuid.LowPart == luid.LowPart && d.AdapterLuid.HighPart == luid.HighPart) + std::printf(" dxgi description: %ls (vendor 0x%04x, device 0x%04x, flags 0x%x)\n", d.Description, + d.VendorId, d.DeviceId, d.Flags); + a->Release(); + } + f->Release(); +} + +} // namespace + +int main() { + D3DKMT_ENUMADAPTERS2 e = {}; + NTSTATUS st = D3DKMTEnumAdapters2(&e); + if (st != 0) { + std::printf("D3DKMTEnumAdapters2 (count) failed: 0x%08lx\n", static_cast(st)); + return 2; + } + std::vector info(e.NumAdapters); + e.pAdapters = info.data(); + st = D3DKMTEnumAdapters2(&e); + if (st != 0) { + std::printf("D3DKMTEnumAdapters2 failed: 0x%08lx\n", static_cast(st)); + return 2; + } + std::printf("adapters: %u\n", e.NumAdapters); + for (ULONG i = 0; i < e.NumAdapters; ++i) { + const D3DKMT_ADAPTERINFO &ai = info[i]; + std::printf("adapter %lu: luid %08lx:%08lx, sources %lu\n", i, static_cast(ai.AdapterLuid.HighPart), + ai.AdapterLuid.LowPart, ai.NumOfSources); + print_dxgi_name(ai.AdapterLuid); + + D3DKMT_DRIVERVERSION wddm = {}; + st = query(ai.hAdapter, KMTQAITYPE_DRIVERVERSION, &wddm); + std::printf(" KMTQAITYPE_DRIVERVERSION: status 0x%08lx, value 0x%x\n", static_cast(st), + static_cast(wddm)); + + D3DKMT_WDDM_2_9_CAPS c29 = {}; + st = query(ai.hAdapter, KMTQAITYPE_WDDM_2_9_CAPS, &c29); + std::printf(" KMTQAITYPE_WDDM_2_9_CAPS: status 0x%08lx, Value 0x%08x, HwSchSupportState %u (%s), " + "HwSchEnabled %u\n", + static_cast(st), c29.Value, c29.HwSchSupportState, + support_state(c29.HwSchSupportState), c29.HwSchEnabled); + + D3DKMT_WDDM_3_0_CAPS c30 = {}; + st = query(ai.hAdapter, KMTQAITYPE_WDDM_3_0_CAPS, &c30); + std::printf(" KMTQAITYPE_WDDM_3_0_CAPS: status 0x%08lx, Value 0x%08x, HwFlipQueueSupportState %u (%s), " + "HwFlipQueueEnabled %u, DisplayableSupported %u\n", + static_cast(st), c30.Value, c30.HwFlipQueueSupportState, + support_state(c30.HwFlipQueueSupportState), c30.HwFlipQueueEnabled, c30.DisplayableSupported); + + D3DKMT_WDDM_3_1_CAPS c31 = {}; + st = query(ai.hAdapter, KMTQAITYPE_WDDM_3_1_CAPS, &c31); + std::printf(" KMTQAITYPE_WDDM_3_1_CAPS: status 0x%08lx, Value 0x%08x\n", static_cast(st), + c31.Value); + + D3DKMT_FLIPQUEUEINFO fq = {}; + st = query(ai.hAdapter, KMTQAITYPE_FLIPQUEUEINFO, &fq); + std::printf(" KMTQAITYPE_FLIPQUEUEINFO: status 0x%08lx, MaxHardwareFlipQueueLength %u, " + "MaxSoftwareFlipQueueLength %u, FlipFlags.FlipInterval %u\n", + static_cast(st), fq.MaxHardwareFlipQueueLength, fq.MaxSoftwareFlipQueueLength, + static_cast(fq.FlipFlags.FlipInterval)); + + D3DKMT_CLOSEADAPTER ca = {}; + ca.hAdapter = ai.hAdapter; + D3DKMTCloseAdapter(&ca); + } + return 0; +} From f5c938263b75391bc21a02c00ae093f7f0cb65ed Mon Sep 17 00:00:00 2001 From: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com> Date: Fri, 13 Nov 2026 22:11:04 -0500 Subject: [PATCH 2/2] Frame pacing experiment: AMD's frame generation swap chain with our stand-in frame Refs #96. Test only; nothing from AMD's SDK is added to the repository or shipped. - clean/fg/fetch_ffx_sdk.py fetches the few files the experiment needs from AMD's public FSR SDK at one pinned commit (tag v2.4.0) into a folder outside the repository: the API headers, the signed loader and frame generation DLLs, and the licence. Each file is checked against the commit's git blob hash, and a SHA-256 manifest is written. - clean/fg/ffx_swapchain_test.cpp creates the frame generation swap chain and the frame generation context through the documented API, and supplies our stand-in frame through frameGenerationCallback. AMD's own interpolation is never dispatched. It reports return codes and callback counts, the GPU memory of the idle context, on-screen intervals from the swap chain's frame statistics, and observable signs of the pacing path (runtime messages, process CPU time, busiest threads and their priorities). A --dwm-check mode reports DWM's completed frames per second, read only. - HWFQ-FINDINGS.md: how to fetch, build and run it. No results yet; they need a live monitor. Signed-off-by: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com> --- clean/fg/HWFQ-FINDINGS.md | 24 ++ clean/fg/fetch_ffx_sdk.py | 73 ++++ clean/fg/ffx_swapchain_test.cpp | 702 ++++++++++++++++++++++++++++++++ 3 files changed, 799 insertions(+) create mode 100644 clean/fg/fetch_ffx_sdk.py create mode 100644 clean/fg/ffx_swapchain_test.cpp diff --git a/clean/fg/HWFQ-FINDINGS.md b/clean/fg/HWFQ-FINDINGS.md index 302198bc..70892c71 100644 --- a/clean/fg/HWFQ-FINDINGS.md +++ b/clean/fg/HWFQ-FINDINGS.md @@ -137,6 +137,30 @@ Build both from an x64 Native Tools prompt: - A process without administrator rights cannot read the panel's power state directly, and nothing was elevated or changed to find out. +## 5. Experiment: AMD's frame generation swap chain with our stand-in frame (test only) + +`clean/fg/ffx_swapchain_test.cpp` measures whether AMD's public FSR SDK frame generation swap chain can present a frame +that we supply, and how it paces it. Nothing from the SDK is in this repository, and nothing is shipped. +- The test loads AMD's signed loader and frame generation DLLs from a folder outside the repository. +- It creates the frame generation swap chain and the frame generation context. +- It supplies our stand-in frame through the documented `frameGenerationCallback`. AMD's own interpolation is never + dispatched. +- It reports: + - return codes and callback counts; + - the GPU memory the API and DXGI report for the idle context; + - on-screen intervals from the swap chain's frame statistics; + - observable signs of the pacing path: the runtime's own messages, process CPU time, and the busiest threads with + their priorities. + +Fetch the SDK files at the pinned commit (tag v2.4.0), outside the repository, then build against them: + + python clean/fg/fetch_ffx_sdk.py + cl /nologo /W4 /EHsc /std:c++17 /O2 ffx_swapchain_test.cpp /I\Kits\FidelityFX\api\include /I\Kits\FidelityFX\framegeneration\include d3d12.lib dxgi.lib dcomp.lib user32.lib + ffx_swapchain_test --sdk \Kits\FidelityFX\signedbin --mode borderless --vsync 0 + +The licence of the fetched files is in `\Kits\FidelityFX\docs\license.md`. No results yet: they need a live +monitor, like section 4. + **Not tested:** on-screen frame-to-frame evenness (mean, standard deviation, p99, misses) for either route. It needs a live monitor (or a display emulator with an EDID). The same short runs will be repeated then, and their numbers added with their records. diff --git a/clean/fg/fetch_ffx_sdk.py b/clean/fg/fetch_ffx_sdk.py new file mode 100644 index 00000000..9d457f34 --- /dev/null +++ b/clean/fg/fetch_ffx_sdk.py @@ -0,0 +1,73 @@ +# SPDX-License-Identifier: MIT +"""Fetches the few files of AMD's public FSR SDK that clean/fg/ffx_swapchain_test.cpp needs, at one pinned commit, into +a folder OUTSIDE this repository. Nothing fetched here is committed, vendored or shipped: the test loads AMD's signed +DLLs from that folder and builds against its headers. + + python clean/fg/fetch_ffx_sdk.py + +Source: https://github.com/GPUOpen-LibrariesAndSDKs/FidelityFX-SDK, tag v2.4.0 = commit +7539ae8b6568ad29ed78a753404351daf3b5cca7 (licence: Kits/FidelityFX/docs/license.md, fetched with the rest). +Every file is checked against the git blob hash that GitHub's tree for that commit lists; a mismatch stops the script. +A MANIFEST.txt with each file's size and SHA-256 is written next to the files. Python standard library only. +""" +import hashlib +import json +import os +import sys +import urllib.request + +REPO = 'GPUOpen-LibrariesAndSDKs/FidelityFX-SDK' +COMMIT = '7539ae8b6568ad29ed78a753404351daf3b5cca7' +FILES = [ + 'Kits/FidelityFX/docs/license.md', + 'Kits/FidelityFX/api/include/ffx_api.h', + 'Kits/FidelityFX/api/include/ffx_api_types.h', + 'Kits/FidelityFX/api/include/ffx_api_loader.h', + 'Kits/FidelityFX/api/include/dx12/ffx_api_dx12.h', + 'Kits/FidelityFX/framegeneration/include/ffx_framegeneration.h', + 'Kits/FidelityFX/framegeneration/include/ffx_framegeneration_api_types.h', + 'Kits/FidelityFX/framegeneration/include/dx12/ffx_api_framegeneration_dx12.h', + 'Kits/FidelityFX/signedbin/amd_fidelityfx_loader_dx12.dll', + 'Kits/FidelityFX/signedbin/amd_fidelityfx_framegeneration_dx12.dll', +] + + +def get(url): + req = urllib.request.Request(url, headers={'User-Agent': 'wronggpu-fetch-ffx-sdk'}) + with urllib.request.urlopen(req, timeout=120) as r: + return r.read() + + +def git_blob_sha(data): + return hashlib.sha1(b'blob %d\0' % len(data) + data).hexdigest() + + +def main(): + if len(sys.argv) != 2: + print(__doc__) + return 1 + dest = os.path.abspath(sys.argv[1]) + tree = json.loads(get('https://api.github.com/repos/%s/git/trees/%s?recursive=1' % (REPO, COMMIT))) + blobs = {e['path']: e['sha'] for e in tree.get('tree', []) if e.get('type') == 'blob'} + lines = ['source https://github.com/%s commit %s' % (REPO, COMMIT)] + for path in FILES: + if path not in blobs: + print('not in the tree at %s: %s' % (COMMIT, path)) + return 2 + data = get('https://raw.githubusercontent.com/%s/%s/%s' % (REPO, COMMIT, path)) + if git_blob_sha(data) != blobs[path]: + print('git blob hash mismatch for %s' % path) + return 3 + out = os.path.join(dest, *path.split('/')) + os.makedirs(os.path.dirname(out), exist_ok=True) + with open(out, 'wb') as f: + f.write(data) + lines.append('%s %d %s' % (hashlib.sha256(data).hexdigest(), len(data), path)) + print('ok %9d %s' % (len(data), path)) + with open(os.path.join(dest, 'MANIFEST.txt'), 'w', encoding='utf-8', newline='\n') as f: + f.write('\n'.join(lines) + '\n') + return 0 + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/clean/fg/ffx_swapchain_test.cpp b/clean/fg/ffx_swapchain_test.cpp new file mode 100644 index 00000000..35760091 --- /dev/null +++ b/clean/fg/ffx_swapchain_test.cpp @@ -0,0 +1,702 @@ +// SPDX-License-Identifier: MIT +// ffx_swapchain_test: a measurement experiment (test only, nothing shipped). It drives AMD's public FSR SDK frame +// generation swap chain with OUR stand-in frame as the generated frame, and reports what can be observed. +// +// It creates, through the documented AMD FSR API (loaded at run time from AMD's signed DLLs in a folder outside this +// repository; see fetch_ffx_sdk.py): +// - the frame generation swap chain (ffxCreateContextDescFrameGenerationSwapChainForHwndDX12 + version struct), +// - the frame generation context (ffxCreateContextDescFrameGeneration + DX12 backend + version struct), +// and configures frameGenerationCallback (ffxConfigureDescFrameGeneration) with our own callback, which copies a +// stand-in image into the generated-frame output. AMD's own interpolation is never dispatched. +// +// It reports: +// a. return codes, callback calls and present counts: does the pairing work without AMD's interpolation; +// b. GPU memory: the API's own memory queries and the process's video memory before/after context creation; +// c. on-screen intervals from the swap chain's GetFrameStatistics (mean, sd, p99, misses), when it reports any; +// d. observable signs of the pacing path: the runtime's own messages (global debug callback), the process's CPU +// time, and the threads that used CPU during the run with their priorities. These are observations, not proof. +// +// Written for this project from AMD's public SDK documentation and headers (used at build time from the fetched +// folder, not copied here) and Microsoft's API documentation. +// +// usage: ffx_swapchain_test --sdk [--mode borderless|exclusive|window] +// [--vsync 0|1] [--real-fps F | --real-fps-factor X] [--seconds S] [--no-fg] [--out file.csv] +// ffx_swapchain_test --dwm-check (read only: DWM's completed frames over 1 s; exit 0 if >= 50) +#define NOMINMAX +#define _CRT_SECURE_NO_WARNINGS +#ifndef _WINDOWS +#define _WINDOWS +#endif +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +using Microsoft::WRL::ComPtr; + +namespace { + +struct Options { + std::wstring sdk; + std::string mode = "borderless"; + int vsync = 0; + double real_fps = 0; // 0: real_fps_factor x refresh rate + double real_fps_factor = 0.45; // 0.45: output (2 x real) at 0.9 x refresh, inside the VRR window + double seconds = 5.0; + bool no_fg = false; + bool dwm_check = false; + std::string out; +}; + +double g_qpc_ms = 0; +UINT64 g_t0 = 0; +double now_ms() { + LARGE_INTEGER c; + QueryPerformanceCounter(&c); + return (static_cast(c.QuadPart) - static_cast(g_t0)) * g_qpc_ms; +} +double qpc_to_ms(INT64 q) { return (static_cast(q) - static_cast(g_t0)) * g_qpc_ms; } + +bool g_quit = false; +LRESULT CALLBACK wndproc(HWND h, UINT m, WPARAM w, LPARAM l) { + if (m == WM_CLOSE || (m == WM_KEYDOWN && w == VK_ESCAPE)) { + g_quit = true; + return 0; + } + return DefWindowProcW(h, m, w, l); +} +void pump() { + MSG msg; + while (PeekMessageW(&msg, nullptr, 0, 0, PM_REMOVE)) { + TranslateMessage(&msg); + DispatchMessageW(&msg); + } +} + +// The runtime's own messages (ffxConfigureDescGlobalDebug), kept with their time. +std::vector g_messages; +void ffx_message(uint32_t type, const wchar_t *message) { + char buf[1024]; + std::snprintf(buf, sizeof(buf), "%.3f ms %s: %ls", now_ms(), type == FFX_API_MESSAGE_TYPE_ERROR ? "error" : "warning", + message ? message : L""); + g_messages.push_back(buf); +} + +double dwm_frames_per_second() { + COMPOSITION_FRAME_ID f0 = 0, f1 = 0; + const double t0 = now_ms(); + if (FAILED(DCompositionGetFrameId(COMPOSITION_FRAME_ID_COMPLETED, &f0))) return -1; + Sleep(1000); + if (FAILED(DCompositionGetFrameId(COMPOSITION_FRAME_ID_COMPLETED, &f1))) return -1; + return static_cast(f1 - f0) * 1000.0 / (now_ms() - t0); +} + +// Read-only display context; returns the refresh period in ms. +double print_display(HWND hwnd) { + HMONITOR mon = MonitorFromWindow(hwnd, MONITOR_DEFAULTTOPRIMARY); + MONITORINFOEXW mi = {}; + mi.cbSize = sizeof(mi); + GetMonitorInfoW(mon, &mi); + double period_ms = 1000.0 / 60.0; + UINT32 np = 0, nm = 0; + if (GetDisplayConfigBufferSizes(QDC_ONLY_ACTIVE_PATHS, &np, &nm) == ERROR_SUCCESS) { + std::vector paths(np); + std::vector modes(nm); + if (QueryDisplayConfig(QDC_ONLY_ACTIVE_PATHS, &np, paths.data(), &nm, modes.data(), nullptr) == ERROR_SUCCESS) { + for (UINT32 i = 0; i < np; ++i) { + DISPLAYCONFIG_SOURCE_DEVICE_NAME sn = {}; + sn.header.type = DISPLAYCONFIG_DEVICE_INFO_GET_SOURCE_NAME; + sn.header.size = sizeof(sn); + sn.header.adapterId = paths[i].sourceInfo.adapterId; + sn.header.id = paths[i].sourceInfo.id; + if (DisplayConfigGetDeviceInfo(&sn.header) != ERROR_SUCCESS || wcscmp(sn.viewGdiDeviceName, mi.szDevice)) + continue; + DISPLAYCONFIG_TARGET_DEVICE_NAME tn = {}; + tn.header.type = DISPLAYCONFIG_DEVICE_INFO_GET_TARGET_NAME; + tn.header.size = sizeof(tn); + tn.header.adapterId = paths[i].targetInfo.adapterId; + tn.header.id = paths[i].targetInfo.id; + if (DisplayConfigGetDeviceInfo(&tn.header) == ERROR_SUCCESS) + std::printf("display: target \"%ls\", output technology %d, EDID ids valid %u\n", + tn.monitorFriendlyDeviceName, static_cast(tn.outputTechnology), + tn.flags.edidIdsValid); + const DISPLAYCONFIG_RATIONAL &rr = paths[i].targetInfo.refreshRate; + if (rr.Numerator && rr.Denominator) { + period_ms = 1000.0 * rr.Denominator / rr.Numerator; + std::printf("display: refresh %.4f Hz, period %.4f ms\n", + static_cast(rr.Numerator) / rr.Denominator, period_ms); + } + } + } + } + return period_ms; +} + +D3D12_RESOURCE_STATES to_d3d12(uint32_t s) { + switch (s) { + case FFX_API_RESOURCE_STATE_UNORDERED_ACCESS: return D3D12_RESOURCE_STATE_UNORDERED_ACCESS; + case FFX_API_RESOURCE_STATE_COMPUTE_READ: return D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE; + case FFX_API_RESOURCE_STATE_PIXEL_READ: return D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE; + case FFX_API_RESOURCE_STATE_PIXEL_COMPUTE_READ: + return D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE | D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE; + case FFX_API_RESOURCE_STATE_COPY_SRC: return D3D12_RESOURCE_STATE_COPY_SOURCE; + case FFX_API_RESOURCE_STATE_COPY_DEST: return D3D12_RESOURCE_STATE_COPY_DEST; + case FFX_API_RESOURCE_STATE_GENERIC_READ: return D3D12_RESOURCE_STATE_GENERIC_READ; + case FFX_API_RESOURCE_STATE_PRESENT: return D3D12_RESOURCE_STATE_PRESENT; + case FFX_API_RESOURCE_STATE_RENDER_TARGET: return D3D12_RESOURCE_STATE_RENDER_TARGET; + default: return D3D12_RESOURCE_STATE_COMMON; + } +} + +void barrier(ID3D12GraphicsCommandList *cl, ID3D12Resource *r, D3D12_RESOURCE_STATES a, D3D12_RESOURCE_STATES b) { + if (a == b) return; + D3D12_RESOURCE_BARRIER br = {}; + br.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION; + br.Transition.pResource = r; + br.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES; + br.Transition.StateBefore = a; + br.Transition.StateAfter = b; + cl->ResourceBarrier(1, &br); +} + +// Our "generated frame": copy the stand-in image into every requested output. +struct CallbackState { + ID3D12Resource *standin = nullptr; + long calls = 0; + long outputs_written = 0; + long bad = 0; + std::map num_generated; +}; +ffxReturnCode_t our_frame_generation(ffxDispatchDescFrameGeneration *p, void *user) { + CallbackState *cs = static_cast(user); + ++cs->calls; + cs->num_generated[p ? p->numGeneratedFrames : 99]++; + if (!p || !p->commandList) { + ++cs->bad; + return FFX_API_RETURN_ERROR_PARAMETER; + } + ID3D12GraphicsCommandList *cl = static_cast(p->commandList); + for (uint32_t i = 0; i < p->numGeneratedFrames && i < 4; ++i) { + ID3D12Resource *dst = static_cast(p->outputs[i].resource); + if (!dst) { + ++cs->bad; + continue; + } + const D3D12_RESOURCE_STATES st = to_d3d12(p->outputs[i].state); + barrier(cl, dst, st, D3D12_RESOURCE_STATE_COPY_DEST); + cl->CopyResource(dst, cs->standin); + barrier(cl, dst, D3D12_RESOURCE_STATE_COPY_DEST, st); + ++cs->outputs_written; + } + return FFX_API_RETURN_OK; +} + +struct ThreadCpu { + DWORD tid; + ULONGLONG cpu100ns; + int priority; +}; +std::map thread_cpu() { + std::map m; + HANDLE snap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0); + if (snap == INVALID_HANDLE_VALUE) return m; + THREADENTRY32 te = {sizeof(te)}; + const DWORD pid = GetCurrentProcessId(); + for (BOOL ok = Thread32First(snap, &te); ok; ok = Thread32Next(snap, &te)) { + if (te.th32OwnerProcessID != pid) continue; + HANDLE th = OpenThread(THREAD_QUERY_LIMITED_INFORMATION, FALSE, te.th32ThreadID); + if (!th) continue; + FILETIME c, e, k, u; + if (GetThreadTimes(th, &c, &e, &k, &u)) { + const ULONGLONG t = (static_cast(k.dwHighDateTime) << 32 | k.dwLowDateTime) + + (static_cast(u.dwHighDateTime) << 32 | u.dwLowDateTime); + m[te.th32ThreadID] = ThreadCpu{te.th32ThreadID, t, GetThreadPriority(th)}; + } + CloseHandle(th); + } + CloseHandle(snap); + return m; +} + +UINT64 local_vram_usage(IDXGIAdapter3 *a) { + DXGI_QUERY_VIDEO_MEMORY_INFO vi = {}; + if (a && SUCCEEDED(a->QueryVideoMemoryInfo(0, DXGI_MEMORY_SEGMENT_GROUP_LOCAL, &vi))) return vi.CurrentUsage; + return 0; +} + +void print_versions(const ffxFunctions &fx, ID3D12Device *dev, uint64_t create_type, const char *what) { + uint64_t count = 0; + ffxQueryDescGetVersions q = {}; + q.header.type = FFX_API_QUERY_DESC_TYPE_GET_VERSIONS; + q.createDescType = create_type; + q.device = dev; + q.outputCount = &count; + ffxReturnCode_t rc = fx.Query(nullptr, &q.header); + std::vector ids(static_cast(count)); + std::vector names(static_cast(count)); + q.versionIds = ids.data(); + q.versionNames = names.data(); + if (count) rc = fx.Query(nullptr, &q.header); + std::printf("ffx: versions for %s: rc %u, count %llu:", what, rc, static_cast(count)); + for (uint64_t i = 0; i < count; ++i) std::printf(" [%s id 0x%llx]", names[i] ? names[i] : "?", ids[i]); + std::printf("\n"); +} + +} // namespace + +int main(int argc, char **argv) { + Options o; + for (int i = 1; i < argc; ++i) { + const std::string a = argv[i]; + auto next = [&]() { return i + 1 < argc ? std::string(argv[++i]) : std::string(); }; + if (a == "--sdk") { + const std::string s = next(); + o.sdk.assign(s.begin(), s.end()); + } else if (a == "--mode") o.mode = next(); + else if (a == "--vsync") o.vsync = std::atoi(next().c_str()); + else if (a == "--real-fps") o.real_fps = std::atof(next().c_str()); + else if (a == "--real-fps-factor") o.real_fps_factor = std::atof(next().c_str()); + else if (a == "--seconds") o.seconds = std::atof(next().c_str()); + else if (a == "--no-fg") o.no_fg = true; + else if (a == "--dwm-check") o.dwm_check = true; + else if (a == "--out") o.out = next(); + else { + std::printf("usage: ffx_swapchain_test --sdk [--mode borderless|exclusive|window] [--vsync 0|1] " + "[--real-fps F] [--seconds S] [--no-fg] [--out file.csv] | --dwm-check\n"); + return 1; + } + } + o.seconds = std::min(std::max(o.seconds, 1.0), 30.0); + SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2); + LARGE_INTEGER f, c; + QueryPerformanceFrequency(&f); + QueryPerformanceCounter(&c); + g_qpc_ms = 1000.0 / static_cast(f.QuadPart); + g_t0 = static_cast(c.QuadPart); + + if (o.dwm_check) { + const double fps = dwm_frames_per_second(); + std::printf("dwm-check: completed composition frames per second %.1f (need >= 50)\n", fps); + return fps >= 50.0 ? 0 : 3; + } + if (o.sdk.empty()) { + std::printf("--sdk is required\n"); + return 1; + } + + // ---- AMD's loader (signed DLL from the fetched SDK folder; the effect DLL is found next to it). + const std::wstring loader = o.sdk + L"\\amd_fidelityfx_loader_dx12.dll"; + HMODULE mod = LoadLibraryExW(loader.c_str(), nullptr, LOAD_WITH_ALTERED_SEARCH_PATH); + if (!mod) { + std::printf("ffx: cannot load %ls (error %lu)\n", loader.c_str(), GetLastError()); + return 4; + } + ffxFunctions fx = {}; + ffxLoadFunctions(&fx, mod); + if (!fx.CreateContext || !fx.Configure || !fx.Query || !fx.DestroyContext) { + std::printf("ffx: loader exports missing\n"); + return 4; + } + { + ffxConfigureDescGlobalDebug dbg = {}; + dbg.header.type = FFX_API_CONFIGURE_DESC_TYPE_GLOBALDEBUG; + dbg.fpMessage = ffx_message; + dbg.debugLevel = FFX_API_CONFIGURE_GLOBALDEBUG_LEVEL_VERBOSE; + std::printf("ffx: global debug callback configure rc %u\n", fx.Configure(nullptr, &dbg.header)); + } + + // ---- window + WNDCLASSW wc = {}; + wc.lpfnWndProc = wndproc; + wc.hInstance = GetModuleHandleW(nullptr); + wc.lpszClassName = L"wg_ffx_swapchain_test"; + wc.hCursor = LoadCursor(nullptr, IDC_ARROW); + RegisterClassW(&wc); + HMONITOR mon = MonitorFromPoint(POINT{0, 0}, MONITOR_DEFAULTTOPRIMARY); + MONITORINFO mi = {sizeof(mi)}; + GetMonitorInfoW(mon, &mi); + const bool windowed = o.mode == "window"; + RECT r = mi.rcMonitor; + DWORD style = WS_POPUP; + if (windowed) { + style = WS_OVERLAPPEDWINDOW; + r = RECT{mi.rcWork.left + 64, mi.rcWork.top + 64, mi.rcWork.left + 64 + 1280, mi.rcWork.top + 64 + 720}; + AdjustWindowRectEx(&r, style, FALSE, WS_EX_TOPMOST); + } + HWND hwnd = CreateWindowExW(WS_EX_TOPMOST, wc.lpszClassName, L"WrongGPU FFX swap chain test (Esc ends)", style, + r.left, r.top, r.right - r.left, r.bottom - r.top, nullptr, nullptr, wc.hInstance, + nullptr); + ShowWindow(hwnd, SW_SHOW); + UpdateWindow(hwnd); + RECT client; + GetClientRect(hwnd, &client); + const UINT w = client.right - client.left, h = client.bottom - client.top; + const double period_ms = print_display(hwnd); + std::printf("dwm: completed composition frames per second %.1f (before presenting)\n", dwm_frames_per_second()); + const double real_fps = o.real_fps > 0 ? o.real_fps : o.real_fps_factor * 1000.0 / period_ms; + std::printf("run: mode %s, vsync %d, real frames %.2f/s (output %.2f/s with frame generation), %s, %.0f s\n", + o.mode.c_str(), o.vsync, real_fps, o.no_fg ? real_fps : 2 * real_fps, + o.no_fg ? "frame generation OFF" : "frame generation ON (our stand-in frame)", o.seconds); + + // ---- D3D12 + ComPtr factory; + CreateDXGIFactory2(0, IID_PPV_ARGS(&factory)); + ComPtr adapter; + factory->EnumAdapterByGpuPreference(0, DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE, IID_PPV_ARGS(&adapter)); + ComPtr adapter3; + adapter.As(&adapter3); + ComPtr dev; + if (FAILED(D3D12CreateDevice(adapter.Get(), D3D_FEATURE_LEVEL_12_0, IID_PPV_ARGS(&dev)))) { + std::printf("D3D12CreateDevice failed\n"); + return 3; + } + D3D12_COMMAND_QUEUE_DESC qd = {}; + qd.Type = D3D12_COMMAND_LIST_TYPE_DIRECT; + ComPtr queue; + dev->CreateCommandQueue(&qd, IID_PPV_ARGS(&queue)); + BOOL tearing = FALSE; + factory->CheckFeatureSupport(DXGI_FEATURE_PRESENT_ALLOW_TEARING, &tearing, sizeof(tearing)); + std::printf("dxgi: DXGI_FEATURE_PRESENT_ALLOW_TEARING = %d\n", tearing); + + print_versions(fx, dev.Get(), FFX_API_CREATE_CONTEXT_DESC_TYPE_FRAMEGENERATIONSWAPCHAIN_FOR_HWND_DX12, + "frame generation swap chain (DX12)"); + print_versions(fx, dev.Get(), FFX_API_CREATE_CONTEXT_DESC_TYPE_FRAMEGENERATION, "frame generation"); + + // ---- the frame generation swap chain + const DXGI_FORMAT fmt = DXGI_FORMAT_R8G8B8A8_UNORM; + const UINT kBuffers = 3; + DXGI_SWAP_CHAIN_DESC1 sd = {}; + sd.Width = w; + sd.Height = h; + sd.Format = fmt; + sd.SampleDesc.Count = 1; + sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; + sd.BufferCount = kBuffers; + sd.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD; + sd.Flags = DXGI_SWAP_CHAIN_FLAG_FRAME_LATENCY_WAITABLE_OBJECT | (tearing ? DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING : 0); + IDXGISwapChain4 *proxy_raw = nullptr; + ffxCreateContextDescFrameGenerationSwapChainForHwndDX12 scd = {}; + scd.header.type = FFX_API_CREATE_CONTEXT_DESC_TYPE_FRAMEGENERATIONSWAPCHAIN_FOR_HWND_DX12; + scd.swapchain = &proxy_raw; + scd.hwnd = hwnd; + scd.desc = &sd; + scd.fullscreenDesc = nullptr; + scd.dxgiFactory = factory.Get(); + scd.gameQueue = queue.Get(); + ffxCreateContextDescFrameGenerationSwapChainVersionDX12 scv = {}; + scv.header.type = FFX_API_CREATE_CONTEXT_DESC_TYPE_FRAMEGENERATIONSWAPCHAIN_VERSION_DX12; + scv.version = FFX_FRAMEGENERATION_SWAPCHAIN_DX12_VERSION; + scd.header.pNext = &scv.header; + ffxContext sc_ctx = nullptr; + ffxReturnCode_t rc = fx.CreateContext(&sc_ctx, &scd.header, nullptr); + std::printf("ffx: create frame generation swap chain rc %u, swap chain %p\n", rc, static_cast(proxy_raw)); + if (rc != FFX_API_RETURN_OK || !proxy_raw) return 5; + ComPtr sc; + sc.Attach(proxy_raw); + factory->MakeWindowAssociation(hwnd, DXGI_MWA_NO_ALT_ENTER); + sc->SetMaximumFrameLatency(2); + HANDLE waitable = sc->GetFrameLatencyWaitableObject(); + if (o.mode == "exclusive") { + const HRESULT fh = sc->SetFullscreenState(TRUE, nullptr); + BOOL fs = FALSE; + sc->GetFullscreenState(&fs, nullptr); + std::printf("dxgi: SetFullscreenState(TRUE) hr 0x%08lx, GetFullscreenState %d\n", fh, fs); + } + + // ---- the frame generation context (AMD's interpolation is created but never dispatched) + const UINT64 vram0 = local_vram_usage(adapter3.Get()); + ffxCreateContextDescFrameGeneration fgd = {}; + fgd.header.type = FFX_API_CREATE_CONTEXT_DESC_TYPE_FRAMEGENERATION; + fgd.flags = 0; + fgd.displaySize = FfxApiDimensions2D{w, h}; + fgd.maxRenderSize = FfxApiDimensions2D{w, h}; + fgd.backBufferFormat = ffxApiGetSurfaceFormatDX12(fmt); + ffxCreateBackendDX12Desc backend = {}; + backend.header.type = FFX_API_CREATE_CONTEXT_DESC_TYPE_BACKEND_DX12; + backend.device = dev.Get(); + ffxCreateContextDescFrameGenerationVersion fgv = {}; + fgv.header.type = FFX_API_CREATE_CONTEXT_DESC_TYPE_FRAMEGENERATION_VERSION; + fgv.version = FFX_FRAMEGENERATION_VERSION; + fgd.header.pNext = &backend.header; + backend.header.pNext = &fgv.header; + ffxContext fg_ctx = nullptr; + rc = fx.CreateContext(&fg_ctx, &fgd.header, nullptr); + const UINT64 vram1 = local_vram_usage(adapter3.Get()); + std::printf("ffx: create frame generation context rc %u\n", rc); + std::printf("memory: process local video memory %.1f MiB before, %.1f MiB after the frame generation context " + "(+%.1f MiB)\n", + vram0 / 1048576.0, vram1 / 1048576.0, (static_cast(vram1) - static_cast(vram0)) / 1048576.0); + if (rc != FFX_API_RETURN_OK) return 6; + { + FfxApiEffectMemoryUsage mu = {}; + ffxQueryDescFrameGenerationGetGPUMemoryUsage q = {}; + q.header.type = FFX_API_QUERY_DESC_TYPE_FRAMEGENERATION_GPU_MEMORY_USAGE; + q.gpuMemoryUsageFrameGeneration = μ + const ffxReturnCode_t qrc = fx.Query(&fg_ctx, &q.header); + std::printf("memory: ffxQueryDescFrameGenerationGetGPUMemoryUsage rc %u total %.1f MiB aliasable %.1f MiB\n", + qrc, mu.totalUsageInBytes / 1048576.0, mu.aliasableUsageInBytes / 1048576.0); + FfxApiEffectMemoryUsage su = {}; + ffxQueryFrameGenerationSwapChainGetGPUMemoryUsageDX12 sq = {}; + sq.header.type = FFX_API_QUERY_DESC_TYPE_FRAMEGENERATIONSWAPCHAIN_GPU_MEMORY_USAGE_DX12; + sq.gpuMemoryUsageFrameGenerationSwapchain = &su; + const ffxReturnCode_t src = fx.Query(&sc_ctx, &sq.header); + std::printf("memory: swap chain GPU memory query rc %u total %.1f MiB aliasable %.1f MiB\n", src, + su.totalUsageInBytes / 1048576.0, su.aliasableUsageInBytes / 1048576.0); + } + + // ---- our resources: RTVs on the swap chain buffers, the stand-in image + D3D12_DESCRIPTOR_HEAP_DESC hd = {}; + hd.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV; + hd.NumDescriptors = kBuffers + 1; + ComPtr heap; + dev->CreateDescriptorHeap(&hd, IID_PPV_ARGS(&heap)); + const UINT inc = dev->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_RTV); + auto rtv = [&](UINT i) { + D3D12_CPU_DESCRIPTOR_HANDLE hnd = heap->GetCPUDescriptorHandleForHeapStart(); + hnd.ptr += static_cast(i) * inc; + return hnd; + }; + std::vector> bufs(kBuffers); + for (UINT i = 0; i < kBuffers; ++i) { + sc->GetBuffer(i, IID_PPV_ARGS(&bufs[i])); + dev->CreateRenderTargetView(bufs[i].Get(), nullptr, rtv(i)); + } + ComPtr standin; + { + D3D12_HEAP_PROPERTIES hp = {}; + hp.Type = D3D12_HEAP_TYPE_DEFAULT; + D3D12_RESOURCE_DESC rd = bufs[0]->GetDesc(); + rd.Flags = D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET; + dev->CreateCommittedResource(&hp, D3D12_HEAP_FLAG_NONE, &rd, D3D12_RESOURCE_STATE_RENDER_TARGET, nullptr, + IID_PPV_ARGS(&standin)); + dev->CreateRenderTargetView(standin.Get(), nullptr, rtv(kBuffers)); + } + std::vector> alloc(kBuffers); + for (auto &a : alloc) dev->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&a)); + ComPtr cl; + dev->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, alloc[0].Get(), nullptr, IID_PPV_ARGS(&cl)); + const float real_grey[4] = {0.20f, 0.20f, 0.22f, 1.0f}; + const float standin_grey[4] = {0.24f, 0.24f, 0.26f, 1.0f}; + cl->ClearRenderTargetView(rtv(kBuffers), standin_grey, 0, nullptr); + barrier(cl.Get(), standin.Get(), D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_COPY_SOURCE); + cl->Close(); + ID3D12CommandList *first[] = {cl.Get()}; + queue->ExecuteCommandLists(1, first); + ComPtr fence; + dev->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence)); + HANDLE fev = CreateEventW(nullptr, FALSE, FALSE, nullptr); + UINT64 next_fence = 1; + queue->Signal(fence.Get(), next_fence); + fence->SetEventOnCompletion(next_fence++, fev); + WaitForSingleObject(fev, 5000); + std::vector fval(kBuffers, 0); + + CallbackState cbs; + cbs.standin = standin.Get(); + ffxConfigureDescFrameGeneration cfg = {}; + cfg.header.type = FFX_API_CONFIGURE_DESC_TYPE_FRAMEGENERATION; + cfg.swapChain = sc.Get(); + cfg.presentCallback = nullptr; + cfg.frameGenerationCallback = our_frame_generation; + cfg.frameGenerationCallbackUserContext = &cbs; + cfg.frameGenerationEnabled = !o.no_fg; + cfg.allowAsyncWorkloads = false; + cfg.flags = 0; + cfg.onlyPresentGenerated = false; + cfg.generationRect = FfxApiRect2D{0, 0, static_cast(w), static_cast(h)}; + + // ---- run + struct Shown { + double ms; + UINT refresh; + }; + std::map shown; // swap chain PresentCount -> on-screen time + std::map cfg_rc; + long stats_fail = 0, stats_ok = 0; + HRESULT last_present_hr = S_OK; + long present_fail = 0; + auto poll_stats = [&]() { + DXGI_FRAME_STATISTICS fs = {}; + if (FAILED(sc->GetFrameStatistics(&fs))) { + ++stats_fail; + return; + } + ++stats_ok; + if (fs.PresentCount == 0 || fs.SyncQPCTime.QuadPart == 0 || shown.count(fs.PresentCount)) return; + const double t = qpc_to_ms(fs.SyncQPCTime.QuadPart) - + static_cast(fs.SyncRefreshCount - fs.PresentRefreshCount) * period_ms; + shown[fs.PresentCount] = Shown{t, fs.PresentRefreshCount}; + }; + HANDLE timer = CreateWaitableTimerExW(nullptr, nullptr, CREATE_WAITABLE_TIMER_HIGH_RESOLUTION, TIMER_ALL_ACCESS); + const double real_period = 1000.0 / real_fps; + const int frames = static_cast(o.seconds * real_fps); + FILETIME pc0, pe0, pk0, pu0, pc1, pe1, pk1, pu1; + GetProcessTimes(GetCurrentProcess(), &pc0, &pe0, &pk0, &pu0); + const std::map th0 = thread_cpu(); + const double run_t0 = now_ms(); + double next_t = run_t0; + UINT first_count = 0, last_count = 0; + sc->GetLastPresentCount(&first_count); + for (int i = 0; i < frames && !g_quit; ++i) { + pump(); + // Frame limiter: wait for the next real frame time, polling the swap chain's statistics every ~1 ms. + next_t += real_period; + while (now_ms() < next_t) { + poll_stats(); + LARGE_INTEGER due; + due.QuadPart = -10000; // 1 ms + SetWaitableTimer(timer, &due, 0, nullptr, nullptr, FALSE); + WaitForSingleObject(timer, 5); + } + WaitForSingleObjectEx(waitable, 1000, TRUE); + const UINT b = sc->GetCurrentBackBufferIndex(); + if (fence->GetCompletedValue() < fval[b]) { + fence->SetEventOnCompletion(fval[b], fev); + WaitForSingleObject(fev, 1000); + } + alloc[b]->Reset(); + cl->Reset(alloc[b].Get(), nullptr); + barrier(cl.Get(), bufs[b].Get(), D3D12_RESOURCE_STATE_PRESENT, D3D12_RESOURCE_STATE_RENDER_TARGET); + cl->ClearRenderTargetView(rtv(b), real_grey, 0, nullptr); + barrier(cl.Get(), bufs[b].Get(), D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_PRESENT); + cl->Close(); + ID3D12CommandList *lists[] = {cl.Get()}; + queue->ExecuteCommandLists(1, lists); + cfg.frameID = static_cast(i) + 1; + cfg_rc[fx.Configure(&fg_ctx, &cfg.header)]++; + const UINT flags = (o.vsync == 0 && tearing && o.mode != "exclusive") ? DXGI_PRESENT_ALLOW_TEARING : 0; + const HRESULT phr = sc->Present(o.vsync ? 1 : 0, flags); + if (FAILED(phr)) { + ++present_fail; + last_present_hr = phr; + } + fval[b] = next_fence; + queue->Signal(fence.Get(), next_fence++); + poll_stats(); + } + for (int k = 0; k < 100; ++k) { // let the last presents reach the screen + poll_stats(); + Sleep(1); + } + const double run_ms = now_ms() - run_t0; + sc->GetLastPresentCount(&last_count); + GetProcessTimes(GetCurrentProcess(), &pc1, &pe1, &pk1, &pu1); + const std::map th1 = thread_cpu(); + + // ---- shutdown, in AMD's documented order + cfg.frameGenerationEnabled = false; + cfg.frameGenerationCallback = nullptr; + cfg.frameGenerationCallbackUserContext = nullptr; + cfg.presentCallback = nullptr; + const ffxReturnCode_t dis_rc = fx.Configure(&fg_ctx, &cfg.header); + const ffxReturnCode_t fgd_rc = fx.DestroyContext(&fg_ctx, nullptr); + if (o.mode == "exclusive") sc->SetFullscreenState(FALSE, nullptr); + const ffxReturnCode_t scd_rc = fx.DestroyContext(&sc_ctx, nullptr); + queue->Signal(fence.Get(), next_fence); + fence->SetEventOnCompletion(next_fence, fev); + WaitForSingleObject(fev, 2000); + bufs.clear(); + const ULONG refs = sc.Reset(); + DestroyWindow(hwnd); + + // ---- report + std::printf("result a: frames %d in %.1f ms; configure rc:", frames, run_ms); + for (auto &kv : cfg_rc) std::printf(" %u x%ld;", kv.first, kv.second); + std::printf(" Present failures %ld (last hr 0x%08lx); callback calls %ld, outputs written %ld, bad %ld; " + "numGeneratedFrames:", + present_fail, last_present_hr, cbs.calls, cbs.outputs_written, cbs.bad); + for (auto &kv : cbs.num_generated) std::printf(" %u x%ld;", kv.first, kv.second); + std::printf(" swap chain present count %u -> %u (+%u for %d app presents)\n", first_count, last_count, + last_count - first_count, frames); + std::printf("result a: shutdown: disable configure rc %u, destroy frame generation context rc %u, destroy swap " + "chain context rc %u, swap chain refs after release %lu\n", + dis_rc, fgd_rc, scd_rc, refs); + auto ft = [](const FILETIME &x) { return (static_cast(x.dwHighDateTime) << 32) | x.dwLowDateTime; }; + const double cpu_ms = ((ft(pk1) + ft(pu1)) - (ft(pk0) + ft(pu0))) / 10000.0; + std::printf("result d: process CPU time during the run %.1f ms over %.1f ms wall (%.1f %% of one core)\n", cpu_ms, + run_ms, 100.0 * cpu_ms / run_ms); + std::vector> busy; + for (auto &kv : th1) { + auto it = th0.find(kv.first); + const ULONGLONG before = it == th0.end() ? 0 : it->second.cpu100ns; + busy.push_back({(kv.second.cpu100ns - before) / 10000.0, kv.second}); + } + std::sort(busy.begin(), busy.end(), [](const auto &a, const auto &b) { return a.first > b.first; }); + std::printf("result d: threads in the process at the end %zu (at the start of the run %zu); busiest:", th1.size(), + th0.size()); + for (size_t k = 0; k < busy.size() && k < 5; ++k) + std::printf(" [tid %lu %.1f ms (%.1f %%) priority %d%s]", busy[k].second.tid, busy[k].first, + 100.0 * busy[k].first / run_ms, busy[k].second.priority, + th0.count(busy[k].second.tid) ? "" : " new"); + std::printf("\n"); + std::printf("result d: runtime messages %zu\n", g_messages.size()); + for (size_t k = 0; k < g_messages.size() && k < 60; ++k) std::printf(" ffx message: %s\n", g_messages[k].c_str()); + + std::printf("result c: GetFrameStatistics ok %ld, failed %ld, distinct presents with an on-screen time %zu\n", + stats_ok, stats_fail, shown.size()); + const double target = (o.no_fg ? real_period : real_period / 2.0); + std::vector iv; + int misses = 0; + UINT prev = 0; + double prev_ms = 0; + int skipped_start = 0; + for (auto &kv : shown) { + if (skipped_start < 10) { // start-up + ++skipped_start; + prev = kv.first; + prev_ms = kv.second.ms; + continue; + } + if (kv.first == prev + 1) { + const double d = kv.second.ms - prev_ms; + iv.push_back(d); + if (std::fabs(d - target) > 0.5 * period_ms) ++misses; + } + prev = kv.first; + prev_ms = kv.second.ms; + } + if (iv.empty()) { + std::printf("result c: no consecutive on-screen times, so no interval statistics\n"); + } else { + double mean = 0; + for (double d : iv) mean += d; + mean /= iv.size(); + double var = 0; + for (double d : iv) var += (d - mean) * (d - mean); + std::vector s = iv; + std::sort(s.begin(), s.end()); + const double p99 = s[std::min(s.size() - 1, static_cast(0.99 * s.size()))]; + std::printf("result c: on-screen interval over %zu consecutive pairs: target %.4f ms, mean %.4f ms, sd %.4f ms, " + "p50 %.4f, p99 %.4f, min %.4f, max %.4f ms; misses (|interval - target| > half a refresh) %d\n", + iv.size(), target, mean, std::sqrt(var / iv.size()), s[s.size() / 2], p99, s.front(), s.back(), + misses); + } + if (!o.out.empty()) { + FILE *fo = std::fopen(o.out.c_str(), "w"); + if (fo) { + std::fprintf(fo, "present_count,shown_ms,present_refresh\n"); + for (auto &kv : shown) std::fprintf(fo, "%u,%.4f,%u\n", kv.first, kv.second.ms, kv.second.refresh); + std::fclose(fo); + } + } + FreeLibrary(mod); + return 0; +}