Skip to content

dxgi: Present refuses the flags the host refuses (new RT item) - #7540

Closed
AirKyzzZ wants to merge 1 commit into
ProjectLeonida:mainfrom
AirKyzzZ:fix/dxgi-present-flags
Closed

AirKyzzZ wants to merge 1 commit into
ProjectLeonida:mainfrom
AirKyzzZ:fix/dxgi-present-flags

Conversation

@AirKyzzZ

@AirKyzzZ AirKyzzZ commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: new RT item (claim)

What and why

SwapChain::Present only checked the sync interval and DXGI_PRESENT_ALLOW_TEARING, so it presented a frame for flags the host refuses, and DXGI_PRESENT_TEST combined with another flag answered S_OK. Measured on the host dxgi.dll with a flip-discard window swap chain over a D3D12 queue: the two stereo flags, any bit outside the documented DXGI_PRESENT_* values (0x80, 0x400 and up), DXGI_PRESENT_TEST together with any other flag, and DXGI_PRESENT_RESTRICT_TO_OUTPUT on a chain created without a restrict-to output are all DXGI_ERROR_INVALID_CALL, and nothing is presented (present count and back buffer index unchanged). DO_NOT_SEQUENCE, RESTART, DO_NOT_WAIT, USE_DURATION and RESTRICT_TO_OUTPUT on a restricted chain still present. Present now refuses the same set; Present1 goes through it too.

#4826 moves the body of Present into a helper; whichever lands second rebases, the hunk is seven lines at the top of Present.

Tested

Windows 10 22H2 (19045), RTX 2060 / NVIDIA 566.14, clang-cl 23.1.6 + lld-link, Windows SDK 10.0.26100, Ninja, on 668f70b28, run from the interactive desktop session (the swap chain test skips without one):

  • dxgi_swapchain_test: pass. The new checks send each refused flag set to Present(0, flags) and expect DXGI_ERROR_INVALID_CALL with nothing presented. With dxgi_com.cpp from main it fails at Present(0, 0x10).
  • dxgi_device, dxgi_swapchain_count: pass. d3d12_vulkan_device fails at tiles == 1 (line 6564, GetResourceTiling on a reserved texture), code this change does not touch.

No game files. AI-assisted (Claude Code).

@ItsNotRexed: an exe/recomp PR, as you asked.

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (no translator change)

Signed-off-by: Maxime Mansiet <AirKyzzZ@users.noreply.github.com>
@AirKyzzZ

Copy link
Copy Markdown
Contributor Author

Correction on the Tested section. Over SSH, dxgi_swapchain_test at 668f70b28 (Windows 10 22H2, RTX 2060 / NVIDIA 566.14, clang-cl 23.1.6) exits 77 with SKIP: the process has no interactive window station, ctest reports dxgi_swapchain ... ***Skipped, so the new checks only run from a desktop session. The kept log of the desktop run at 668f70b28 shows dxgi_swapchain passed (1.72 s), but there is no log left of the run with dxgi_com.cpp from main, so "fails at Present(0, 0x10)" is unconfirmed. Both get re-run from the desktop session and posted here.

@lpicoury-ux

Copy link
Copy Markdown

Tested 668f70b28 merged into main dde82f27e (local --no-commit merge, clean) on Windows 11 Home 10.0.26200, from the interactive desktop session (dxgi_swapchain ran, not skipped): pass. The negative control you couldn't confirm over SSH is confirmed below, and the host behaviour you measured on Windows 10 holds on Windows 11.

Setup: MSVC 19.44.35230 (VS 2022 Build Tools, CMake 3.31.6 + Ninja through scripts/build_runtime.ps1 -FetchSdl3), RelWithDebInfo, Windows SDK 10.0.26100, Vulkan SDK 1.4.363.0, NVIDIA GeForce RTX 4060 Ti (610.88). No game files.

result
main: ctest -R ^dxgi 3/3 passed
this PR merged into main: ctest -R ^dxgi 3/3 passed, 3 runs (dxgi_device, dxgi_swapchain, dxgi_swapchain_count)
new dxgi_swapchain_test.cpp + main's dxgi_com.cpp fails, 2/2 runs: FAIL: Present(0, 0x10) should be DXGI_ERROR_INVALID_CALL and present nothing
full ctest on the merge 258/263; the 5 failures are the same as on main and none is dxgi (d3d12_vulkan_device #7125, d3d12_indirect_dispatch_count #7133, d3d12_depth_resolve, d3d12_indirect_rt, and run_module_names, a WinError 206 from my checkout's long path)

Host check on Windows 11. A standalone probe (no Leonida code; source below) creates a 64×64, 2-buffer flip-discard swap chain with CreateSwapChainForHwnd over a D3D12 direct queue (unrestricted, no stereo), on the real dxgi.dll and the RTX 4060 Ti. It calls Present(0, flags) and checks whether GetLastPresentCount or GetCurrentBackBufferIndex moved:

flags host HRESULT presented
0 (control) S_OK yes
TEST alone S_OK no
STEREO_PREFER_RIGHT, STEREO_TEMPORARY_MONO DXGI_ERROR_INVALID_CALL no
RESTRICT_TO_OUTPUT (unrestricted chain) DXGI_ERROR_INVALID_CALL no
0x80, 0x400, 0x80000000 DXGI_ERROR_INVALID_CALL no
TEST with DO_NOT_SEQUENCE, RESTART, USE_DURATION or DO_NOT_WAIT DXGI_ERROR_INVALID_CALL no
DO_NOT_SEQUENCE, RESTART, DO_NOT_WAIT, USE_DURATION S_OK yes

That matches the rule in this PR on every case, including TEST|DO_NOT_WAIT, which the new test doesn't list. Not covered: RESTRICT_TO_OUTPUT on a chain that has a restrict-to output.

present_probe.cpp (cl /EHsc /std:c++20 present_probe.cpp d3d12.lib dxgi.lib user32.lib)
// Host DXGI probe: which Present flags does the real dxgi.dll refuse on a flip-discard window swap chain over a D3D12
// queue (unrestricted, non-stereo)? For each flag set: HRESULT, and whether a frame was presented (present count or
// back buffer index changed). Standalone, no Leonida code.
#include <windows.h>
#include <d3d12.h>
#include <dxgi1_6.h>
#include <cstdio>

static LRESULT CALLBACK wndproc(HWND h, UINT m, WPARAM w, LPARAM l) { return DefWindowProcW(h, m, w, l); }

static void pump() {
    MSG msg;
    while (PeekMessageW(&msg, nullptr, 0, 0, PM_REMOVE)) { TranslateMessage(&msg); DispatchMessageW(&msg); }
}

int main() {
    WNDCLASSW wc{};
    wc.lpfnWndProc = wndproc;
    wc.hInstance = GetModuleHandleW(nullptr);
    wc.lpszClassName = L"present_probe";
    RegisterClassW(&wc);
    HWND hwnd = CreateWindowExW(0, wc.lpszClassName, L"present_probe", WS_OVERLAPPEDWINDOW, 40, 40, 160, 160, nullptr,
                                nullptr, wc.hInstance, nullptr);
    if (!hwnd) { std::printf("CreateWindow failed\n"); return 2; }
    ShowWindow(hwnd, SW_SHOWNOACTIVATE);
    pump();

    ID3D12Device* device = nullptr;
    if (FAILED(D3D12CreateDevice(nullptr, D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&device)))) {
        std::printf("D3D12CreateDevice failed\n");
        return 2;
    }
    D3D12_COMMAND_QUEUE_DESC qd{};
    qd.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
    ID3D12CommandQueue* queue = nullptr;
    device->CreateCommandQueue(&qd, IID_PPV_ARGS(&queue));
    IDXGIFactory4* factory = nullptr;
    CreateDXGIFactory2(0, IID_PPV_ARGS(&factory));
    IDXGIAdapter1* adapter = nullptr;
    DXGI_ADAPTER_DESC1 ad{};
    if (SUCCEEDED(factory->EnumAdapterByLuid(device->GetAdapterLuid(), IID_PPV_ARGS(&adapter)))) adapter->GetDesc1(&ad);

    DXGI_SWAP_CHAIN_DESC1 sd{};
    sd.Width = 64;
    sd.Height = 64;
    sd.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
    sd.SampleDesc.Count = 1;
    sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
    sd.BufferCount = 2;
    sd.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD;
    IDXGISwapChain1* sc1 = nullptr;
    HRESULT hr = factory->CreateSwapChainForHwnd(queue, hwnd, &sd, nullptr, nullptr, &sc1);
    if (FAILED(hr)) { std::printf("CreateSwapChainForHwnd failed 0x%08lx\n", hr); return 2; }
    IDXGISwapChain3* sc = nullptr;
    sc1->QueryInterface(IID_PPV_ARGS(&sc));

    OSVERSIONINFOW ov{sizeof(ov)};
    using RtlGetVersionFn = LONG(WINAPI*)(OSVERSIONINFOW*);
    auto rtl = reinterpret_cast<RtlGetVersionFn>(GetProcAddress(GetModuleHandleW(L"ntdll.dll"), "RtlGetVersion"));
    if (rtl) rtl(&ov);
    std::printf("host: Windows %lu.%lu.%lu, adapter %ls\n", ov.dwMajorVersion, ov.dwMinorVersion, ov.dwBuildNumber,
                ad.Description);

    for (int i = 0; i < 3; ++i) { sc->Present(0, 0); pump(); Sleep(20); }

    struct Case { UINT flags; const char* name; };
    const Case cases[] = {
        {0x0, "0 (control)"},
        {0x1, "TEST alone"},
        {0x10, "STEREO_PREFER_RIGHT"},
        {0x20, "STEREO_TEMPORARY_MONO"},
        {0x40, "RESTRICT_TO_OUTPUT (unrestricted chain)"},
        {0x80, "undefined 0x80"},
        {0x400, "undefined 0x400"},
        {0x80000000u, "undefined 0x80000000"},
        {0x1 | 0x2, "TEST|DO_NOT_SEQUENCE"},
        {0x1 | 0x4, "TEST|RESTART"},
        {0x1 | 0x100, "TEST|USE_DURATION"},
        {0x1 | 0x8, "TEST|DO_NOT_WAIT"},
        {0x2, "DO_NOT_SEQUENCE"},
        {0x4, "RESTART"},
        {0x8, "DO_NOT_WAIT"},
        {0x100, "USE_DURATION"},
    };
    std::printf("%-12s %-42s %-12s %s\n", "flags", "case", "HRESULT", "presented");
    for (const Case& c : cases) {
        Sleep(30);
        pump();
        UINT count0 = 0, count1 = 0;
        sc->GetLastPresentCount(&count0);
        const UINT idx0 = sc->GetCurrentBackBufferIndex();
        hr = sc->Present(0, c.flags);
        pump();
        sc->GetLastPresentCount(&count1);
        const UINT idx1 = sc->GetCurrentBackBufferIndex();
        const bool presented = count1 != count0 || idx1 != idx0;
        const char* label = hr == DXGI_ERROR_INVALID_CALL ? " INVALID_CALL"
                            : hr == S_OK                  ? " S_OK"
                            : hr == DXGI_STATUS_OCCLUDED  ? " OCCLUDED"
                            : hr == DXGI_ERROR_WAS_STILL_DRAWING ? " STILL_DRAWING" : "";
        std::printf("0x%08x   %-42s 0x%08lx%s  %s\n", c.flags, c.name, static_cast<unsigned long>(hr), label,
                    presented ? "yes" : "no");
    }
    sc->Release();
    sc1->Release();
    queue->Release();
    device->Release();
    DestroyWindow(hwnd);
    return 0;
}

AI-assisted (Claude Code).

@sgonzalez-glitch

Copy link
Copy Markdown

Tested 668f70b28 (merged with #7541 and #7555 onto main dde82f27e) on NVIDIA GeForce RTX 2080, driver 621.24 (32.0.16.2124), Windows 11, MSVC 19.44 RelWithDebInfo: pass.

AI-assisted: yes

@sean4586

Copy link
Copy Markdown

Tested 668f70b28 on NVIDIA GeForce RTX 5070 Ti, driver 616.56, Vulkan SDK 1.4.363.0, Windows x64: pass (compared against base 86d1c862d).

  • ctest -R "^dxgi_swapchain$" (3 consecutive runs): 3/3 passed on 668f70b28 (1.24s, 1.18s, 1.19s) and 3/3 passed on base 86d1c862d (1.15s, 1.10s, 1.09s).
  • Full ctest (266/270, 4 pre-existing host failures), tests/semantic (445 files, 5906 tests), check.py (176/211 native, 207/211 w64), and run_gpu_tests.py (191/240 native, 222/240 w64, only pre-existing ms_wave_info_s3 failure): no regressions.

AI-assisted.

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.

5 participants