Skip to content

DXGI: CreateSwapChainForHwnd refuses what the host runtime's DXGI refuses (D3D-317) - #3531

Merged
ItsNotRexed merged 2 commits into
ItsNotRexed:mainfrom
dev-ewann:dxgi-swapchain-args
Nov 23, 2026
Merged

ItsNotRexed merged 2 commits into
ItsNotRexed:mainfrom
dev-ewann:dxgi-swapchain-args

Conversation

@dev-ewann

@dev-ewann dev-ewann commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: D3D-317 (claim)

What and why

IDXGIFactory2::CreateSwapChainForHwnd accepted descriptions that the host runtime's DXGI refuses for a D3D12 swap chain on a window, so a bad description returned S_OK here and failed on Windows. Measured side by side on the host (RTX 5070 / 621.14):

  • DXGI_ERROR_INVALID_CALL: the bitblt effects (DISCARD, SEQUENTIAL) and unknown effects; Scaling other than STRETCH/NONE; AlphaMode other than UNSPECIFIED/IGNORE; any Flags bit outside 0x2, 0x20, 0x40, 0x800, 0x1000, 0x2000; BufferUsage with SHARED, READ_ONLY, DISCARD_ON_PRESENT or UNORDERED_ACCESS; a null queue or an ID3D12Device in place of the queue; a null window, description or output pointer (ours was E_INVALIDARG / E_POINTER).
  • E_INVALIDARG: a width or height above 16384.

check_hwnd_swap_chain runs these checks before the swap chain is built; the build itself moves to create_swap_chain, which CreateSwapChainForCoreWindow now calls directly, so the CoreWindow path keeps its current checks (its host rules can't be compared without a real CoreWindow).

dxgi_swapchain_test gets a host comparison: 40 descriptions, each sent to our factory and to CreateDXGIFactory2 from the system dxgi.dll on a real window and host queue, with the HRESULTs compared.

Touches the same two files as #2791, #2808 and #2822 (other methods); whichever lands first, the other rebases.

Tested

On main f847748 with this commit (e627443), Windows 11 build 26300, RTX 5070, driver 621.14, MSVC 19.44, Vulkan SDK 1.4.363.1:

  • dxgi_swapchain_test: 40 host comparisons, 0 mismatches. Counter-check with main's dxgi_com.cpp and the new test: 25 mismatches.
  • Full ctest: 140/140 pass.
  • Mutations: 13/13 mutations of the added lines in dxgi_com.cpp make dxgi_swapchain_test fail.
  • No run over the game corpus (runtime-only change; no game files involved).

AI-assisted (Claude), on behalf of dev-ewann.

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. (n/a: no translator change)

🤖 Generated with Claude Code

…uses

Measured on RTX 5070 / 621.14: bitblt effects, window-incompatible
scaling and alpha modes, flags outside 0x2/0x20/0x40/0x800/0x1000/0x2000,
shared/read-only/discard-on-present/UAV usage, a null or non-queue
device, null window/description/output are DXGI_ERROR_INVALID_CALL; a
side above 16384 is E_INVALIDARG. dxgi_swapchain_test compares 40
descriptions with the host. The CoreWindow path keeps its old checks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@MatyEjsr

Copy link
Copy Markdown

Tested e6274435 on NVIDIA GeForce RTX 3090 (Ampere, driver 32.0.16.1742 / 617.42), Windows 11 (10.0.26200), VS 2022 Build Tools (MSVC 19.44.35230.0, Ninja RelWithDebInfo), Windows SDK 10.0.26100.0, Vulkan SDK 1.4.363.0: pass.

  • Incremental scripts\build_runtime.ps1 (147 actions): clean build, exit 0.
  • Targeted ctest -R ^(dxgi_swapchain|d3d12_present)$ (validation off): 2/2 pass (d3d12_present 1.02s, dxgi_swapchain 0.66s: CreateSwapChainForHwnd: 40 descriptions compared with the host runtime, 0 mismatches).
  • Targeted ctest -R ^(vk_sync_validation|dxgi_swapchain|d3d12_present)$ with LEONIDA_VK_VALIDATION=1 and VK_LOADER_LAYERS_DISABLE=~implicit~: 3/3 pass, vk_sync_validation confirmed active, 0 validation errors, 40/40 host dxgi.dll comparisons matched (d3d12_present 1.03s, dxgi_swapchain 0.70s).
  • Full ctest (validation off, no game files): 132 passed, 8 skipped, 1 disabled (gameinput), 0 failed (124.0s).

Code read of runtime/shims/dxgi_com.cpp looks clean: CreateSwapChainForHwnd clears *out before calling check_hwnd_swap_chain, validates the ID3D12CommandQueue interface and window-only flags/usages/dimensions against the measured host rules, and CreateSwapChainForCoreWindow routes directly to create_swap_chain so CoreWindow keeps its own path.

AI-assisted.

@Parakeet-Wheat

Copy link
Copy Markdown
Contributor

Tested e627443 on NVIDIA RTX 3070 (driver 610.88), Windows 11 26300, MSVC (VS 2022), Windows SDK 10.0.26100, Vulkan SDK 1.4.363: pass. My results agree with @MatyEjsr's 3090 run: dxgi_swapchain_test with validation gives 40 compared, 0 mismatches and 0 validation errors, and the full ctest gives 140/140. My additions are below.

  • Negative control: with main's dxgi_com.cpp and the new test, I get the same 25 mismatches you listed on the 5070. So the host rules are the same on 610.88 as on 621.14.
  • More host cases (added locally, not part of the PR). Every one matched the host's DXGI:
    • Formats: R8G8B8A8_UNORM, both _SRGB formats, R16G16B16A16_FLOAT, R10G10B10A2_UNORM, B8G8R8X8_UNORM, R32G32B32A32_FLOAT, UNKNOWN, D32_FLOAT, BC1_UNORM, R8_UNORM.
    • Other description fields: 0, 16 and 17 buffers; a 0 x 0 size and a width of 0 alone; stereo; MSAA quality 1 and sample count 0; usage BACK_BUFFER and SHADER_INPUT alone; premultiplied alpha with FP16.
    • Flag bits kWindowFlags refuses but the test never sends: 0x8 (RESTRICTED_CONTENT), 0x10 (RESTRICT_SHARED_RESOURCE_DRIVER), 0x100 (FULLSCREEN_VIDEO) and 0x200 (YUV_VIDEO). Also HW_PROTECTED with flip-sequential, DISPLAY_ONLY | ALLOW_MODE_SWITCH, ALLOW_TEARING | 0x4000, and ALLOW_TEARING | FRAME_LATENCY_WAITABLE_OBJECT.
    • Check order: width 16385 with effect DISCARD gives DXGI_ERROR_INVALID_CALL on both sides, so checking the effect before the size matches the host.

That's 72 descriptions in all, 0 mismatches. The four untested flag bits may be worth adding to the test, since the "any other bit" rule currently rests on 0x1, 0x4, 0x80, 0x400, 0x4000 and 0x80000000.

AI-assisted (Claude Code).

@Kam471 Kam471 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Tested e6274435 on Windows 11, MSVC 19.44.35228 + Ninja, real RTX 5070 Ti. Build needed -DBUILD_TESTING=ON and -DLEONIDA_BUILD_WINDOWS_RUNTIME=ON; the D3D12 shim tests are gated under if(TARGET g6rt ...) (runtime/CMakeLists.txt:997, test at :1586), so the OFF/OFF recipe in the description yields no target. With both on, dxgi_swapchain_test built 11/11.

  • dxgi_swapchain_test.exe -> 40 descriptions compared, 0 mismatches, PASS.
  • ctest -R dxgi_swapchain --output-on-failure -> 1/1 passed, 0.81 s.
  • The new refusals match this host's DXGI (0x887A0001): swap effect DISCARD/SEQUENTIAL/99, scaling ASPECT_RATIO_STRETCH/7, alpha PREMULTIPLIED/STRAIGHT/9, all invalid flags, SHARED/READ_ONLY/DISCARD_ON_PRESENT/UNORDERED_ACCESS usage, 1 buffer, MSAA 4, null queue/window/desc/out, device instead of queue; over-wide 16385 -> 0x80070057. Accepted cases match too (NONE, IGNORE, ALLOW_MODE_SWITCH, DISPLAY_ONLY, waitable, tearing, 0x1000/0x2000, usage 0/shader-input, flip-sequential x3).

Two follow-ups, neither blocking:

  1. Multi-fault precedence: a bad format, MSAA 4, or BufferCount 1 combined with width 16385 gives the shim E_INVALIDARG but the host DXGI_ERROR_INVALID_CALL, because the width check runs before the format/count/MSAA checks. The tests are one-fault-per-case, so they do not see it. Low severity (both FAILED), but worth matching the host order.
  2. BufferCount 16: both succeed, but GetDesc1 reports 8 from the shim vs 16 from the host (dxgi_swapchain.h:485 Vulkan maxImageCount clamp). That is pre-existing D3D-389 and is fixed in #3597.

Parity holds for every single-fault case. No game files.

…ore the size, like the host

A description with width 16385 and MSAA 4, one buffer or a bad format is
DXGI_ERROR_INVALID_CALL on the host runtime's DXGI, not E_INVALIDARG
(found by Kam471 in the review of ItsNotRexed#3531). Five multi-fault cases added
to the side-by-side comparison.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@zzEmmazz

Copy link
Copy Markdown
Contributor

Tested e6274435, applied alone on main f847748c: pass.
Windows 11 (26200), MSVC 14.50 (VS 2026), Ninja RelWithDebInfo. Two GPUs: Ryzen 7 9700X iGPU (RDNA2, AMD 32.0.21042.62) and RTX 3070 (NVIDIA 610.74), picked with LEONIDA_VK_DEVICE.

  • Test change only, on main's dxgi_com.cpp: dxgi_swapchain fails with CreateSwapChainForHwnd: 40 descriptions compared with the host runtime, 25 mismatches on both GPUs (for example usage SHARED: ours 0x00000000, host 0x887a0001). Same count as the description.
  • Whole PR: ctest -R "^(dxgi_swapchain|d3d12_present)$" 2/2 passed with LEONIDA_VK_DEVICE=radeon and 2/2 with LEONIDA_VK_DEVICE=3070; dxgi_swapchain prints 40 descriptions compared with the host runtime, 0 mismatches on both.
  • The test's host side is D3D12CreateDevice(nullptr, ...), which is the RTX 3070 on this PC in both runs. To cover the other adapters I ran a separate small DXGI/D3D12 program (no layer, a new window per case, a queue on the adapter named) on the RTX 3070, the Radeon iGPU and WARP:
    • SwapEffect 0, 1, 2, 5: DXGI_ERROR_INVALID_CALL; 3 and 4: S_OK.
    • Scaling 0, 1: S_OK; 2, 3: DXGI_ERROR_INVALID_CALL.
    • AlphaMode 0, 3: S_OK; 1, 2, 4: DXGI_ERROR_INVALID_CALL.
    • Flags, each single bit 0x1 to 0x8000: S_OK for 0x2, 0x20, 0x40, 0x800, 0x1000, 0x2000; DXGI_ERROR_INVALID_CALL for the other ten.
    • BufferUsage RENDER_TARGET_OUTPUT plus one bit: S_OK with 0x10 (SHADER_INPUT) and 0x40 (BACK_BUFFER); DXGI_ERROR_INVALID_CALL with 0x80 (SHARED), 0x100 (READ_ONLY), 0x200 (DISCARD_ON_PRESENT), 0x400 (UNORDERED_ACCESS).
    • null queue, an ID3D12Device as the queue, null window, null description, null output pointer: DXGI_ERROR_INVALID_CALL.
    • Width or height 16384: S_OK.
    • All of the above are the same on the three adapters and match the list in the description.
    • Width or height 16385: E_INVALIDARG on the RTX 3070 and the Radeon iGPU, but S_OK on WARP. So the 16384 limit is the adapter's, not DXGI's; with a WARP queue the host accepts what check_hwnd_swap_chain refuses.

Not checked: which error the host gives when two of these are wrong at once (the order of the checks); the CoreWindow path; the rest of ctest.

@dev-ewann

Copy link
Copy Markdown
Contributor Author

Thanks @Kam471. Follow-up 1 is fixed in dac96d2d: CreateSwapChainForHwnd now checks MSAA, buffer count and format before the 16384 size limit, so E_INVALIDARG only comes back when the size is the only fault. Five multi-fault cases were added to the side-by-side comparison (width 16385 with MSAA 4, with one buffer, with DXGI_FORMAT_UNKNOWN, with effect DISCARD, and height 16385 with BC1).

Tested dac96d2d (on main f847748) on an RTX 5070, driver 621.14, Windows 11 26300, MSVC 19.44:

  • dxgi_swapchain_test: 45 descriptions compared with the host, 0 mismatches, PASS. Full ctest: 140/140.
  • Same test with the previous shim (e6274435's dxgi_com.cpp): 4 mismatches (ours 0x80070057, host 0x887a0001 on the four MSAA/count/format cases).
  • Mutations: removing each of the 3 added checks one at a time makes the comparison fail (1, 1 and 2 mismatches). 3/3 killed.

Follow-up 2 (BufferCount 16 reported as 8) is left to #3597 (D3D-389).

AI-assisted (Claude), on behalf of dev-ewann.

@Vonsek

Vonsek commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested e627443 on Windows 11, RTX 5080 (driver 616.92) and AMD Radeon iGPU gfx1036 (driver 26.10.1): ctest -R "^(dxgi_swapchain|d3d12_present)$" (MSVC, RelWithDebInfo, LEONIDA_VK_DEVICE set per run, device line confirmed, VK_LOADER_LAYERS_DISABLE=~implicit~, no validation layer installed) — pass on both, 2/2 each.

  • RTX 5080: CreateSwapChainForHwnd: 40 descriptions compared with the host runtime, 0 mismatches.
  • AMD Radeon (gfx1036): CreateSwapChainForHwnd: 40 descriptions compared with the host runtime, 0 mismatches.

No negative control run.

AI-assisted.

@ricardotanaka

Copy link
Copy Markdown

Tested dac96d2d (merged onto main f847748c) on AMD Radeon RX 7600 (RDNA3, discrete), AMD driver 26.11.2 (LLPC, Vulkan 1.4.359), Windows 11 Pro 26200, MSVC 19.44.35230 + Ninja RelWithDebInfo, Windows SDK 10.0.26100, Vulkan SDK 1.4.363.0: pass.

  • ctest -R "^(dxgi_swapchain|d3d12_present)$": 2/2. CreateSwapChainForHwnd: 45 descriptions compared with the host runtime, 0 mismatches. The host side here is AMD's own D3D12 driver on the RX 7600 (the only adapter), so this is the first comparison of dac96d2d's multi-fault cases against a non-NVIDIA host.
  • Same with LEONIDA_VK_VALIDATION=1 and VK_LOADER_LAYERS_DISABLE=~implicit~: 3/3 with vk_sync_validation (confirmed active), 0 validation errors, 45/0 again.
  • Full ctest: 139/140 on the first run. loader failed with its own antivirus-timing message (freshly written PE images took 90 ms to read back); it is unrelated to this PR, passed 3/3 on rerun, and passes on main. Skipped as on main: the game-file tests, vk_sync_validation (without validation), gameinput (no SDL3).
  • Earlier, e6274435 on the same card (drivers 26.10.1 and 26.11.2): 40 descriptions, 0 mismatches; full ctest 140/140.

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

@traeb0n

traeb0n commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested e627443 (on its base main f847748) on NVIDIA GeForce RTX 5070, driver 621.01, Windows 11 Pro (26300), MSVC 14.44 (VS 2022 Build Tools, Ninja, RelWithDebInfo), Vulkan SDK 1.4.363, LEONIDA_VK_VALIDATION=1: pass.

  • Clean build of runtime/ (scripts/build_runtime.ps1): exit 0.
  • ctest -E loader -j 4: 128/128 passed, the same as main f847748 built and run first on this machine (128/128).
  • dxgi_swapchain, d3d12_present, xbox_present: 3/3 passed. dxgi_swapchain_test: PASS (CreateSwapChain, CreateSwapChainForHwnd, CreateSwapChainForCoreWindow, occlusion status, GetBuffer+GetDesc, clear+readback, Present, ResizeBuffers; 0 validation errors).

AI-assisted (Claude Code).

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.

9 participants