Skip to content

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

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

Exopus-0x65f7ad0 wants to merge 1 commit 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.

…ed (D3D-444)

An HWND swapchain created with DXGI_SCALING_NONE reported STRETCH from
GetDesc1 and its back buffer was stretched to the window. Host DXGI
(Windows 11, RTX 5060 Ti) keeps NONE and draws the back buffer unscaled
at the top-left corner; it also refuses ASPECT_RATIO_STRETCH (CoreWindow
and composition only) and values past it with DXGI_ERROR_INVALID_CALL on
an HWND swapchain, which the layer accepted.

- GetDesc1 reports the Scaling the swapchain was created with.
- Present's copy from the back buffer to the window's image follows it
  (runtime/src/present_scaling.h): STRETCH fills the window as before;
  NONE copies texel for texel to the top-left, cropped, nearest filter;
  ASPECT_RATIO_STRETCH (CoreWindow swapchains) keeps the aspect ratio,
  centred. What the copy doesn't cover is cleared to opaque black.
- CreateSwapChainForHwnd refuses ASPECT_RATIO_STRETCH and bad values;
  CreateSwapChainForCoreWindow still takes ASPECT_RATIO_STRETCH.

present_scaling_test: the rectangles for each mode (CPU only).
dxgi_swapchain_test: the refused values, and a NONE swapchain smaller
than its window keeps NONE and presents (clean under validation).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@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.

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.

3 participants