Skip to content

dxgi: keep DXGI_SCALING_NONE on HWND swapchains and present it unscaled (D3D-444) - #4231

Open
Exopus-0x65f7ad0 wants to merge 2 commits into
ItsNotRexed:mainfrom
Exopus-0x65f7ad0:d3d12/dxgi-scaling-none
Open

Exopus-0x65f7ad0 wants to merge 2 commits into
ItsNotRexed:mainfrom
Exopus-0x65f7ad0:d3d12/dxgi-scaling-none

Conversation

@Exopus-0x65f7ad0

Copy link
Copy Markdown

Claim: #4084 (D3D-444, found by @jeremygold02)

Problem

An HWND swapchain created with DXGI_SCALING_NONE reported DXGI_SCALING_STRETCH from GetDesc1 (dxgi_com.cpp hard-coded it), and Present stretched its back buffer to the window (dxgi_swapchain.h blitted to the full surface extent). A title asking for unscaled output got a stretched image.

Measured on host DXGI for a flip-model HWND swapchain (Windows 11, RTX 5060 Ti, driver 621.14), creating each DXGI_SCALING and reading GetDesc1 back:

Scaling CreateSwapChainForHwnd GetDesc1().Scaling layer on main
STRETCH (0) S_OK 0 S_OK, 0
NONE (1) S_OK 1 S_OK, 0
ASPECT_RATIO_STRETCH (2) DXGI_ERROR_INVALID_CALL — S_OK, 0
3, 4 DXGI_ERROR_INVALID_CALL — S_OK, 0

Change

  • GetDesc1 reports the Scaling the swapchain was created with.
  • New runtime/src/present_scaling.h (present_rect, pure arithmetic) gives Present's copy from the back buffer to the window's image: STRETCH fills the window (as before, linear filter); NONE copies texel for texel to the top-left corner, cropped where the back buffer is larger, nearest filter; ASPECT_RATIO_STRETCH (reachable only from CreateSwapChainForCoreWindow) scales to the largest aspect-keeping size, centred. Whatever the copy doesn't cover is cleared to opaque black first (vkCmdClearColorImage, then a transfer→transfer barrier), so it's never the undefined contents of a freshly acquired image.
  • CreateSwapChainForHwnd refuses ASPECT_RATIO_STRETCH and values past it with DXGI_ERROR_INVALID_CALL, as the host does; CreateSwapChainForCoreWindow (which shared the HWND path) still accepts ASPECT_RATIO_STRETCH.

Tests

  • New present_scaling_test (ctest present_scaling, CPU only): the rectangles for each mode, smaller/larger/same-size back buffers, letterboxing, rounding, an empty source and sizes near 2^16 (64-bit products). 14 cases, OK.
  • dxgi_swapchain_test: ASPECT_RATIO_STRETCH, 3 and 4 are DXGI_ERROR_INVALID_CALL on an HWND swapchain; a 64x48 NONE swapchain in a 128x128 window keeps NONE in GetDesc1 and presents (the clear-then-unscaled-copy path). On main it fails at the first check (the layer creates an ASPECT_RATIO_STRETCH HWND swapchain). With the change: PASS, also with LEONIDA_VK_VALIDATION=1 (0 VUID messages).
  • Full cmake --build + ctest -C RelWithDebInfo -j6: 137/137 passed, 8 skipped/disabled for missing game files, Bink or SDL, as on main. tests/test_ctest_registration.py: OK.

Limitations

  • The pixels of an unscaled present aren't read back by the test (the window's surface isn't readable from it); the placement is covered by present_rect's unit test and the Vulkan commands by the validation run.
  • What DWM shows outside an unscaled back buffer on the host isn't measured; the layer clears that part to opaque black.
  • Legacy IDXGIFactory::CreateSwapChain still maps to STRETCH, as before.

AI-assisted (Claude Code). Tested on Windows 11 with an RTX 5060 Ti, driver 621.14.

@Vonsek

Vonsek commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested 7464bef on Windows 11, MSVC BuildTools 2022 (Ninja, RelWithDebInfo, -j 4), PR head on top of main aa2988c: fail, does not build. gta6rt_d3d12_com stops at runtime/shims/d3d12_vulkan_device.cpp:

runtime\src\vulkan_present.h(50): error C2011: 'leonida::PresentRect': 'struct' type redefinition
runtime\src\vulkan_present.h(57): error C2079: 'leonida::PresentPlane::source_rect' uses undefined struct 'leonida::PresentRect'
runtime\shims\d3d12_vulkan_device.cpp(7391): error C2664: 'VkResult leonida::VulkanPresenter::present(leonida::ResourceId,bool,const leonida::PresentRect *,bool)': cannot convert argument 3 from '<error type> *' to 'const leonida::PresentRect *'

The new runtime/src/present_scaling.h defines struct PresentRect (line 20) in namespace leonida, and runtime/src/vulkan_present.h already defines a different leonida::PresentRect (line 50, the source-rectangle struct from 4d62f48); the d3d12 shim includes both. A rename in one of them would likely clear it.

What did run: present_scaling_test (CPU only, built fresh) passes via ctest on both the RTX 5080 (616.92) and the AMD Radeon RDNA2 iGPU (26.10.1). dxgi_swapchain_test could not be rebuilt against this head (it links the failing library), so I have no valid result for it. No Vulkan validation layer is installed here.

AI-assisted.

@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.

Build failure at 7464bef2 on Windows 11, MSVC /W4, LEONIDA_BUILD_WINDOWS_RUNTIME=ON:

present_scaling.h(20): error C2011: 'leonida::PresentRect': 'struct' type redefinition
vulkan_present.h(50): note: see declaration of 'leonida::PresentRect'
d3d12_vulkan_device.cpp(7391): error C2664: 'VulkanPresenter::present(..., const leonida::PresentRect *, ...)'

runtime/src/present_scaling.h adds struct PresentRect { src_width, src_height, dst_x, dst_y, dst_width, dst_height, scaled, covers }, but runtime/src/vulkan_present.h already declares struct PresentRect { left, top, right, bottom } in the same leonida namespace, and d3d12_vulkan_device.cpp includes both (transitively). The standalone present_scaling_test compiles, but the runtime target that includes the presenter does not, so the windows build cannot link this branch.

Suggested fix: rename the new struct (e.g. ScalingRect/PresentBlitRect) and its uses in present_scaling.h/present_scaling_test.cpp, or reuse the existing PresentRect with an explicit conversion at the call site. Nothing else in the diff looked wrong; I could not run the swapchain/scaling tests until this compiles.

@akimfomin

Copy link
Copy Markdown

Independent check of the host behavior mentioned under Limitations. Stock Windows DXGI/D3D11 on Windows 11 build 28000.3501, RTX 4070 Ti SUPER (617.14), with a 64x48 solid red back buffer in a 128x128 HWND. I sampled ten pixels per case, including both sides of the expected boundaries, using Present1 with both FLIP_SEQUENTIAL and FLIP_DISCARD: 90/90 expected pixels matched for each flip effect.

  • STRETCH control: all sampled pixels were red.
  • NONE, ordinary HWND: red through x=63 and y=47. Starting at x=64 or y=48, the uncovered part was black by default, then blue and green after SetBackgroundColor and Present1.
  • NONE, WS_EX_LAYOUTRTL: red starts at x=64 in the upper 48 rows, with the specified background on the left and below.

This matches Microsoft's DXGI_SCALING documentation. At 7464bef2, the proposed Vulkan path clears to opaque black and the NONE rectangle always starts at x=0, so explicit background colors and RTL alignment are two measurable gaps with the host. The standalone present_scaling_test passed all 14 CPU cases at that head on Windows. The pixel measurement used the system DXGI as a reference. Synthetic frames only. AI-assisted.

@jeremygold02 jeremygold02 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.

The stock Vulkan target fails to build at 7464bef2 with the type collision already reported here. After renaming only the new PresentRect type and its uses in present_scaling.h, dxgi_swapchain.h and present_scaling_test.cpp locally, I measured the displayed image. The proposed HWND blit restored native placement and cropping in the D3D-444 fixture on both Windows adapters.

Runtime GetDesc1 for NONE NONE exact RGB pairs STRETCH exact RGB pairs
Parent aa2988cc STRETCH 16/48 48/48
7464bef2 with local type rename NONE 48/48 48/48

With NONE, the 32×16 source occupied the upper-left of the 64×32 client. The 128×64 source showed its upper-left crop. I sampled six points after DWM composition, switched the source colors between Present and Present1, and checked each result against native D3D12 on the same adapter LUID. The 96 native reference samples were identical across the two runs. Fixture, sample coordinates and parent readings

On this Windows 11 Home 26200.9974 desktop, native D3D12 and the diagnostic build both returned opaque blue from GetBackgroundColor before the first present. All 16 uncovered samples were RGB 0/0/0 on both paths. This was an ordinary left-to-right Win32 popup with B8G8R8A8_UNORM, two buffers, FLIP_DISCARD and ALPHA_MODE_IGNORE. The D3D11 run on build 28000.3501 observed colored borders under its conditions. D3D11 measurements

Intel Core Ultra 7 265K, MSVC 19.44 x64 Release. The NVIDIA GeForce RTX 5070 (621.01 / 32.0.16.2101) and Intel(R) Graphics (32.0.101.9150) each ran both native D3D12 and Leonida Vulkan. Synthetic frames, Vulkan validation off.

AI-assisted by GPT-6.2 Astra.

@akimfomin

Copy link
Copy Markdown

Thanks for checking native D3D12, @jeremygold02. I ran a matched native D3D11/D3D12 comparison on the same adapter LUID on Windows 11 28000.3501, RTX 4070 Ti SUPER 617.14. Both used a 64x48 back buffer in a 128x128 HWND, FLIP_DISCARD, alpha ignore, and both BGRA8 and RGBA8.

For DXGI_SCALING_NONE, both APIs showed black by default, then the requested blue and green outside the buffer. LTR aligned left and WS_EX_LAYOUTRTL aligned right. All 18 corresponding sets of 10 client pixel samples matched between APIs for each of Present and Present1, including a STRETCH control.

To check timing separately, I created fresh native D3D12 BGRA8 LTR swapchains. Setting blue before the first Present1 produced blue in 7/7 uncovered samples on the first presented frame. On another fresh swapchain the default was black in 7/7, then setting blue after the first present made all seven samples blue on the second. This confirms colored D3D12 borders on this Windows/driver combination. The difference from the black border observed on your system remains open. This follow-up measured the native Windows reference only. Synthetic frames. AI-assisted.

@Exopus-0x65f7ad0

Copy link
Copy Markdown
Author

Pushed 117ddbb: present_scaling.h's rectangle type was named leonida::PresentRect, which runtime/src/vulkan_present.h already defines for the PresentX planes, so a unit seeing both headers (dxgi_com.h → dxgi_swapchain.h, and vulkan_present.h) failed with C2011 in the full configuration. It's now SwapchainBlit / swapchain_blit(). Rebuilt from an empty build folder with -DLEONIDA_LLVM_MC (so the translated-shader D3D12 tests are built too): LEONIDA_VK_VALIDATION=1 ctest 145/145 passed, 0 VUIDs, RTX 5060 Ti / 621.14.

@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.

Re-checked the pushed head \117ddbb3\ on Windows 11, RTX 5070 Ti with \LEONIDA_BUILD_WINDOWS_RUNTIME=ON:

  • The runtime now builds: \present_scaling.h\ uses \SwapchainBlit, so \d3d12_vulkan_device.cpp\ no longer sees two \leonida::PresentRect\ definitions (the earlier C2011/C2664 are gone).
  • \present_scaling_test.exe\ -> OK, exit 0.
  • \dxgi_swapchain_test.exe\ -> PASS (all swapchain cases, 0 validation errors).
  • \ctest -R 'present_scaling|dxgi_swapchain'\ -> 2/2 passed (0.01 s / 0.53 s).

No blocking issue.

@akimfomin

Copy link
Copy Markdown

I measured the displayed pixels on the current 117ddbb3 against aa2988cc on Windows 11 28000.3501, RTX 4070 Ti SUPER / 617.14. The isolated Win32/Vulkan harness compiled the unmodified swapchain backend from each revision. It sampled the presented synthetic frames after DWM composition, in BGRA8 and RGBA8.

  • With a 64×48 red source in a 128×128 client, the PR restores native placement for a normal LTR window. It matches 10/10 common native D3D12 sample points per format, up from 3/10 on the base. The PR draws red through x=63 and y=47 with black padding after those boundaries.
  • With WS_EX_LAYOUTRTL, the PR produces the same left-aligned pixels as LTR. Native Windows D3D11 and D3D12 put the red region at x=64–127 on this host. The PR matches 4/10 common native D3D12 sample points per format in RTL, compared with 3/10 for the base. This confirms the alignment gap in the displayed frame.
  • With a 192×160 red/blue source in the 128×128 client, the PR crops the top-left under NONE for both window styles. STRETCH controls agree between revisions and still show both colors.

This checks the presentation backend and real window output. The probe does not exercise the Leonida COM shim or SetBackgroundColor. Native reference measurements. Synthetic frames only. AI-assisted (Codex).

Exopus-0x65f7ad0 added a commit to Exopus-0x65f7ad0/Leonida that referenced this pull request Nov 23, 2026
…ght-aligns in RTL windows

Measured against native D3D11/D3D12 on ItsNotRexed#4231 (@akimfomin, @jeremygold02):
with DXGI_SCALING_NONE the part of the window outside the back buffer
takes the colour SetBackgroundColor set (black by default), and in a
WS_EX_LAYOUTRTL window the back buffer sits at the top-right corner.
The layer cleared to opaque black and always used the top-left corner.

- swapchain_blit takes the window's reading direction; NONE starts at
  dst_width - copy width in a right-to-left window (the other modes don't
  depend on it). The style is read on every present.
- The clear uses the background colour, passed on by SetBackgroundColor
  (opaque: the alpha isn't composed), black until then.

present_scaling_test: RTL cases for NONE, STRETCH and ASPECT_RATIO_STRETCH.
dxgi_swapchain_test: a NONE swapchain sets a background colour before
presenting.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Exopus-0x65f7ad0

Copy link
Copy Markdown
Author

Thanks @akimfomin and @jeremygold02 for measuring the displayed pixels. Pushed 15ff0af0 for the two gaps you found against native D3D11/D3D12:

  • Background colour: what the copy doesn't cover is now cleared to the colour SetBackgroundColor set (opaque; the alpha isn't composed), black until one is set. It was always black before. Your two hosts disagree on whether a coloured border shows (blue on 28000.3501, black on 26200.9974 with the same call); this follows the documented behaviour and the newer build.
  • Right-to-left windows: with DXGI_SCALING_NONE in a WS_EX_LAYOUTRTL window the back buffer now sits at the top-right corner (x = width − copy width), read from the window's style on every present. STRETCH and ASPECT_RATIO_STRETCH don't depend on the direction.

Checks: present_scaling_test gains RTL cases (19 cases, OK); dxgi_swapchain_test sets a background colour on the NONE swapchain before presenting; full ctest -j4 with LEONIDA_VK_VALIDATION=1 and -DLEONIDA_LLVM_MC: 145/145, 0 VUIDs (RTX 5060 Ti / 621.14). I don't have a screen-sampling harness here, so I haven't measured the RTL and coloured-border pixels myself; @akimfomin, if your harness can rerun the RTL and SetBackgroundColor cases on this head, that would confirm it.

@akimfomin

akimfomin commented Nov 23, 2026 •

Copy link
Copy Markdown

Thanks for the fixes. I repeated the screen samples at 15ff0af0 against 117ddbb3, on the same Windows 11 28000.3501 and RTX 4070 Ti SUPER / 617.14. This time I also ran a portable harness through Leonida’s IDXGIFactory2, IDXGISwapChain1::Present1 and SetBackgroundColor, using real Vulkan window output. Its local MinGW port only adapts the unused command-queue GetDesc call and declares the usual COM IIDs.

Confirmed for the 64×48 source in a 128×128 client, with BGRA8/RGBA8 and black, blue and green backgrounds. The current head matches all 60/60 common native pixels in LTR (previously 32/60) and all 60/60 in RTL (previously 8/60). STRETCH still matches. The actual GetBackgroundColor round-trip and the displayed pixels agree.

One more RTL case with a source larger than the window. I put red in the top-left 128×128 of a 192×160 back buffer and blue elsewhere. Stock D3D12 NONE in RTL shows source columns 64–191. 15ff0af0 still reads columns 0–127. That gives 6/12 matching samples per format. Native LTR takes the left crop, and native RTL still takes the right crop after changing the style on the same swapchain.

I tested a narrow follow-up locally. Add a zero-initialized src_x to SwapchainBlit, set r.src_x = src_width - r.src_width alongside the existing r.dst_x assignment in the RTL NONE branch, and use [src_x, src_x + src_width] for the two Vulkan source X offsets (with src_width here being rect.src_width). That gives 12/12 against native in each format. The other 360 sampled pixels were unchanged. The CPU geometry test passed with wider/shorter RTL cases asserting the source offset. I can share the patch or review the next revision.

Real composed window samples, synthetic frames only. AI-assisted (Codex).

@theantipopau

Copy link
Copy Markdown

Tested 15ff0af0 (PR head, applied to aa2988cc) on AMD Radeon RX 9070 XT (RDNA4), Windows 11 Pro 10.0.26300, AMD driver 32.0.32029.2010 (Vulkan 1.4.359): pass.

  • Build: MSVC 19.44.35230 (VS 2022), Windows SDK 10.0.26100.0, Vulkan SDK 1.4.357.0, CMake 4.4.4 + Ninja, RelWithDebInfo.
  • present_scaling_test: OK (all SwapchainBlit cases).
  • dxgi_swapchain_test: PASS, device: AMD Radeon RX 9070 XT — including the 64x48 DXGI_SCALING_NONE swapchain in the 128x128 window: GetDesc1 keeps DXGI_SCALING_NONE, and Present completes through the clear + unscaled copy.
  • Same test with LEONIDA_VK_VALIDATION=1: PASS, 0 validation errors.
  • python -I tests/test_ctest_registration.py: OK.

This is the RDNA4 data point the item was missing. As the PR notes, the presented pixels are not read back by the test; placement is covered by present_scaling_test and the Vulkan commands by validation.

No game data.

Exopus-0x65f7ad0 added a commit to Exopus-0x65f7ad0/Leonida that referenced this pull request Nov 23, 2026
…shows its right part

Measured against native D3D12 by @akimfomin on ItsNotRexed#4231: with
DXGI_SCALING_NONE in a WS_EX_LAYOUTRTL window, a back buffer wider than
the window is cropped to its right part (source columns 64..191 of 192 in
a 128-wide window); the layer took the left part. SwapchainBlit gains a
source x, set to src_width - copy width in that case, and the blit reads
from it.

present_scaling_test: wider-than-window RTL cases check the source offset.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Exopus-0x65f7ad0

Copy link
Copy Markdown
Author

Thanks @akimfomin, and @theantipopau for the RDNA4 run. Pushed ac579d94 with the wider-than-window RTL case: SwapchainBlit has a source x, set to src_width - copy width for NONE in a right-to-left window, and the blit reads [src_x, src_x + width] (your follow-up, as you measured it: native D3D12 shows source columns 64–191 of a 192-wide buffer in a 128-wide RTL window). present_scaling_test checks the offset for 192×160, 200×100 and 300×300 sources in a 128×128 RTL window; full ctest -j4 with validation: 145/145, RTX 5060 Ti / 621.14. Your 60/60 LTR and RTL results shouldn't move, as the change only applies when the buffer is wider than the RTL window; a rerun of the 192×160 RTL case on this head would close it.

@akimfomin

Copy link
Copy Markdown

Confirmed ac579d94 on the same RTX 4070 Ti SUPER / 617.14 and Windows 11 28000.3501. I rebuilt the same portable Leonida DXGI + Vulkan probe from the new head and repeated all 384 composed samples.

  • The 192×160 source in a 128×128 RTL NONE window now matches all 12/12 native D3D12 points in both BGRA8 and RGBA8. It shows source columns 64–191 as expected.
  • The other 360 points stayed unchanged, including the smaller source, black/blue/green padding, ordinary LTR and the STRETCH controls. The current present_scaling_test also passed.

Same MinGW harness scope as the previous report. Synthetic frames only. Thanks for taking the follow-up. AI-assisted (Codex).

Exopus-0x65f7ad0 and others added 2 commits November 23, 2026 04:58
…ed (D3D-444)

GetDesc1 reported DXGI_SCALING_STRETCH whatever the app asked for, and
Present always stretched the back buffer over the window. The swapchain
now stores the scaling mode and returns it, and a DXGI_SCALING_NONE
present copies the back buffer 1:1 to the top-left corner (top-right in
a WS_EX_LAYOUTRTL window, showing the buffer's right part when it is
wider), filling the rest with the SetBackgroundColor colour, with a
nearest filter. DXGI_SCALING_ASPECT_RATIO_STRETCH and values past it
are refused on HWND swapchains with DXGI_ERROR_INVALID_CALL, as on the
host. The blit rectangle is computed in present_scaling.h
(SwapchainBlit) and checked by present_scaling_test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The swapchain now loads vkCmdClearColorImage to fill what an unscaled
back buffer leaves uncovered, and create() fails when a function is
missing, so the mock driver this test (from ItsNotRexed#3280, D3D-389) builds its
table from needs the entry too. A no-op, like its other vkCmd mocks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Exopus-0x65f7ad0

Copy link
Copy Markdown
Author

Rebased onto main (7b7d73f8) and squashed the four commits into one (e0b87c7e), plus one test fix (e67b5ecb):

Clean build with -DLEONIDA_LLVM_MC (RTX 5060 Ti / 621.14, MSVC 14.51, Vulkan SDK 1.4.363.0): full LEONIDA_VK_VALIDATION=1 ctest -j4 161/162 before the mock fix (only dxgi_swapchain_count), then -R "dxgi|present" 7/7 with it (present_scaling, dxgi_swapchain, dxgi_swapchain_count, d3d12_present, xbox_present …), 0 VUIDs. AI-assisted (Claude Code).

@Exopus-0x65f7ad0

Copy link
Copy Markdown
Author

Full suite on the pushed head e67b5ecb: LEONIDA_VK_VALIDATION=1 ctest -j4 162/162, 0 VUIDs (RTX 5060 Ti / 621.14).

@0klavair

Copy link
Copy Markdown

Tested e67b5ecb (merged onto main 3eb4410b) on NVIDIA GeForce RTX 5060 (Blackwell), driver 621.01, Windows 11 23H2 22631.6199, MSVC 19.44 (VS 2022 Build Tools), Vulkan SDK 1.4.363: pass.

No game files. AI-assisted.

@Vonsek

Vonsek commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested e67b5ec on Windows 11 (MSVC BuildTools 2022, Ninja, RelWithDebInfo, -j 4; no Vulkan validation layer installed), one GPU at a time, device line confirmed: AMD Radeon RDNA2 iGPU (driver 26.10.1) and NVIDIA RTX 5080 (driver 616.92). Ran ctest -R "^(dxgi_swapchain|dxgi_swapchain_count|present_scaling)$" (the tests this PR changes): pass on both GPUs, 3/3 each. No game files.

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.

7 participants