Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SwapChainPanel scales SwapChain content by DPI scale #8219

Closed
abenedik opened this issue Feb 22, 2023 · 2 comments
Closed

SwapChainPanel scales SwapChain content by DPI scale #8219

abenedik opened this issue Feb 22, 2023 · 2 comments
Labels
documentation An issue with existing documentation or a request for documenation of a new topic

Comments

@abenedik
Copy link

Describe the bug

When using SwapChainPanel with ISwapChainPanelNative then the SwapChain is scaled by the window's dpi scale even though the size of SwapChain perfectly matches the pixel size of the SwapChainPanel.

Steps to reproduce the bug

See the following sample (in c#): SwapChainPanelScaleProblem.zip

Steps:

  • Create DirectX 11 texture, for example with size 512 x 512
  • Set window size to the same size as the texture (+ size of window borders and title)
  • Create SwapChainPanel, add it to the Window and extract the ISwapChainPanelNative interface.
  • Create DirectX 11 SwapChain with the same size as the texture and attach it to the SwapChainPanelNative (call SetSwapChain)
  • Get back buffer from SwapChain (call dxSwapChain1.GetBackBuffer)
  • Copy the created texture into the back buffer (dx11Device.ImmediateContext.CopyResource)
  • Present SwapChain (dxSwapChain1.Present)

Workaround:
Set Scale (or RenderTransform with ScaleTransform) on SwapChainPanel to 1 / dpiScale. Also set the Width and Height of the SwapChainPanel to the size of the texture (note: Width and Height are in device independent units, but because we scaled down the content, we need to scale up the size otherwise the SwapChainPanel would crop the SwapChain).

Expected behavior

Because the size of SwapChain is the same as the pixel size of the SwapChainPanel, the whole content of the SwapChain should be shown. Instead the SwapChain is scaled by dpi scale so the bottom right part is not visible - see screenshots below:

Screenshots

Screenshots from the attached application (there is button to apply the scale workaround in the middle of the screenshot)

Expected result or result when using no DPI scaling:
Screenshot 2023-02-22 082957"

Using 125% scaling:
Screenshot 2023-02-22 083045

NuGet package version

WinUI 3 - Windows App SDK 1.2.3: 1.2.230118.102

Windows version

Windows 10 (21H2): Build 19044

Additional context

No response

@abenedik abenedik added the bug Something isn't working label Feb 22, 2023
@gabbybilka gabbybilka added the needs-triage Issue needs to be triaged by the area owners label Mar 13, 2023
@bpulliam bpulliam added documentation An issue with existing documentation or a request for documenation of a new topic and removed bug Something isn't working needs-triage Issue needs to be triaged by the area owners labels Jul 7, 2023
@JJBrychell
Copy link

This is currently as designed. The thing is, when you tell a swap chain that you want 512x512 pixels, those are logical pixels, so when the swap chain is displayed on the screen, it is actually 768x768 physical pixels. But when you set window size, that is in physical pixels so if you set the window size to 532x586 (512x512 plus chrome - check out ResizeClient so you don't worry about this part), you are only getting a client area of 512x512 physical pixels which will truncate some of the768x768.

So, you need to make sure that both the window and the swapchain are using the same number of pixels. You can do that by changing you window sizing call to account for the needed extra space OR you can (as done in the sample) scale the swapchain size to match the physical pixel size of the window. The latter provides a much better visual experience (since images are not being scaled), but the former is more honoring what the user has requested. It is up to the application to decide which approach is more appropriate.

Note: There can be some discussion around whether these two api should be using different coordinate scales, but that would be a different discussion as one is part of the OS and the other is part of a UI framework.

@DmitriyKomin
Copy link
Contributor

Hi @abenedik, as @JJBrychell noted the behavior is by design. The following MSDN Remark currently addresses this:

In order to maintain crisp vector rendering, you should listen for the CompositionScaleChanged event and query the CompositionScaleX and CompositionScaleY property values to account for the current UI scale, and potentially render again from DirectX. Otherwise XAML layout might do the scaling and your visuals might be degraded.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation An issue with existing documentation or a request for documenation of a new topic
Projects
None yet
Development

No branches or pull requests

5 participants