Skip to content

Conversation

n1ght-hunter
Copy link
Contributor

@n1ght-hunter n1ght-hunter commented Apr 16, 2025

Connections
#3486
#7108
#5150

Description
add support for builtin DComp swapchains to the dx12 backend.
My main goal for this was to get window transparency to work on windows

Testing
Tested using a basic wgpu app with transparency

Squash or Rebase?
Squash

Checklist

  • Run cargo fmt.
  • Run taplo format.
  • Run cargo clippy --tests. If applicable, add:
    • --target wasm32-unknown-unknown
  • Run cargo xtask test to run tests.
  • If this contains user-facing changes, add a CHANGELOG.md entry.

I dont know if its normal or not but when running cargo xtask test i had 201 tests fail with code 0xc0000005: Invalid access to memory location. (os error 998) this is run on windows 10

@n1ght-hunter n1ght-hunter requested review from crowlKats and a team as code owners April 16, 2025 06:17
Copy link
Member

@cwfitzgerald cwfitzgerald left a comment

Choose a reason for hiding this comment

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

Generally looking good, some comments.

@cwfitzgerald cwfitzgerald self-assigned this Apr 16, 2025
@n1ght-hunter
Copy link
Contributor Author

n1ght-hunter commented Apr 17, 2025

will get to this in the next day or two

@n1ght-hunter
Copy link
Contributor Author

@cwfitzgerald would you be able to re review when you get time thanks

Copy link
Member

@cwfitzgerald cwfitzgerald left a comment

Choose a reason for hiding this comment

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

Sorry for the delay, looks good in general with a few small comments. I also think that the hardcoded list of blending modes needs to be adjusted based on if DXGI or DComp is selected

@Anutrix
Copy link

Anutrix commented May 30, 2025

Might totally be unfounded and not apply here but DirectComposition on Windows has issues with NVIDIA GPU when used with multiple monitors with different and high(60+) refresh rates. This was seen on Firefox so might be specific to it.
Recent ticket: https://bugzilla.mozilla.org/show_bug.cgi?id=1968876 . Couldn't find original ticket.

@cwfitzgerald
Copy link
Member

I definitely wouldn't worry about it here. That issue seems to be Windows 10 specific too.

@melody-rs
Copy link
Contributor

Oh! Thank you for doing this, transparency support in wgpu has been a pretty big pain point for me in the past

@n1ght-hunter
Copy link
Contributor Author

Im just traveling abroad atm should be able to get to this in the next week or so

@cwfitzgerald
Copy link
Member

Boop :)

@MarijnS95
Copy link
Contributor

So if I understand this correctly, normally one would create and manage a composition (device, target) externally, and pass either the IDCompositionVisual (where wgpu will insert the internally-crated IDXGISwapChain1 as content into the visual) or a HANDLE to an opaque "composition surface" (which the caller should insert as the content of a visual themselves - useful for cross-process sharing) into wgpu surface creation. Allowing users to render into arbitrary surfaces with wgpu while possibly combining it with a much more complicated composition/transformation tree and surfaces filled by other sources.

This PR seems to abstract away the most basic form of that, by creating and managing that DirectComposition device and a single Visual spanning the full Target that wraps the window (where hopefully "someone" disables the backing redirection buffer...) so that users no longer have to deal with it?

i.e. maybe this PR should be retitled and have a better description of what it achieves. Technically "dcomp on Windows" is already supported :)

@n1ght-hunter
Copy link
Contributor Author

i have updated this pr now. i believe i have fixed all comments except for title of pr.
im terrible with coming up with titles so suggestions are welcome

@cwfitzgerald cwfitzgerald changed the title Add dcomp on windows Add support for builtin DComp swapchains Aug 13, 2025
Copy link
Member

@cwfitzgerald cwfitzgerald left a comment

Choose a reason for hiding this comment

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

Sorry for another round of comments, we're almost there!

/// Use the DXGI presentation system.
#[default]
Dxgi,
/// Use the DirectComposition presentation system.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
/// Use the DirectComposition presentation system.
/// Use a DXGI swapchain made from a DirectComposition visual made from the window.
///
/// This supports transparent windows, but does not support fullscreen optimization.

Copy link
Contributor

@MarijnS95 MarijnS95 Aug 14, 2025

Choose a reason for hiding this comment

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

@cwfitzgerald these user-facing documentation bits need links to upstream documentation exactly how all this works. Perhaps we need to explain that users cannot or should not use this in conjunction with SurfaceTargetUnsafe::CompositionVisual/SurfaceTargetUnsafe::SurfaceHandle if they're already managing a layer tree externally (themselves).


Can you backup (link) exactly how this doesn't support "fullscreen optimization" (like borderless fullscreen)? I was under the impression that this uses flip-style present as well which should allow for hardware-compositable/presentable planes in the sawpchain?

EDIT: Or are you saying this because translucent surfaces inevitably require the underlying plane(s) to be rendered and blended (hopefully accelerated in hardware by MPO blending) too? If the user disables alpha blending on the swapchain it should still be able to make this optimization.

Copy link
Contributor

Choose a reason for hiding this comment

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

Should we also recommend users to set WS_EX_NOREDIRECTIONBITMAP?

Copy link
Member

Choose a reason for hiding this comment

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

these user-facing documentation bits need links to upstream documentation exactly how all this works. Perhaps we need to explain that users cannot or should not use this in conjunction with SurfaceTargetUnsafe::CompositionVisual/SurfaceTargetUnsafe::SurfaceHandle if they're already managing a layer tree externally (themselves).

I agree, though we can leave this to another PR and spin off an issue from this one, I don't want to hold it up too long as I've already let it sit a while.

Can you backup (link) exactly how this doesn't support "fullscreen optimization" (like borderless fullscreen)? I was under the impression that this uses flip-style present as well which should allow for hardware-compositable/presentable planes in the sawpchain?

I thought I had a conversation about this with Jesse on the d3d12 discord a while ago but I can't find it, so I'll ask again and see what they say.

Should we also recommend users to set WS_EX_NOREDIRECTIONBITMAP?

I honestly don't know.

Copy link
Member

Choose a reason for hiding this comment

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

Re: mpo

I was wrong! Things made from visuals are still eligible for full screen optimization.

@n1ght-hunter
Copy link
Contributor Author

@cwfitzgerald done :)

profiling::scope!("DirectComposition::DCompositionCreateDevice");
unsafe { DirectComposition::DCompositionCreateDevice2(None) }.map_err(|err| {
log::error!("DirectComposition::DCompositionCreateDevice failed: {err}");
crate::SurfaceError::Other("DirectComposition::DCompositionCreateDevice")
Copy link
Contributor

Choose a reason for hiding this comment

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

It's surprising that SurfaceError doesn't have an inner error like InstanceError.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

whats the go with? i presume that is for another pr if required?

/// Use the DXGI presentation system.
#[default]
Dxgi,
/// Use the DirectComposition presentation system.
Copy link
Contributor

@MarijnS95 MarijnS95 Aug 14, 2025

Choose a reason for hiding this comment

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

@cwfitzgerald these user-facing documentation bits need links to upstream documentation exactly how all this works. Perhaps we need to explain that users cannot or should not use this in conjunction with SurfaceTargetUnsafe::CompositionVisual/SurfaceTargetUnsafe::SurfaceHandle if they're already managing a layer tree externally (themselves).


Can you backup (link) exactly how this doesn't support "fullscreen optimization" (like borderless fullscreen)? I was under the impression that this uses flip-style present as well which should allow for hardware-compositable/presentable planes in the sawpchain?

EDIT: Or are you saying this because translucent surfaces inevitably require the underlying plane(s) to be rendered and blended (hopefully accelerated in hardware by MPO blending) too? If the user disables alpha blending on the swapchain it should still be able to make this optimization.

/// Use the DXGI presentation system.
#[default]
Dxgi,
/// Use the DirectComposition presentation system.
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we also recommend users to set WS_EX_NOREDIRECTIONBITMAP?

@melody-rs
Copy link
Contributor

melody-rs commented Aug 14, 2025

Could we maybe get an example showing how to use DComp to make window transparency work?

Window transparency with wgpu has been a frustration for a while now and having a working example to base things off of would be super helpful imo

@n1ght-hunter
Copy link
Contributor Author

Could we maybe get an example showing how to use DComp to make window transparency work?

Window transparency with wgpu has been a frustration for a while now and having a working example to base things off of would be super helpful imo

sure i have a testing example i can its basiclly just a triangle with partly clear background

Co-authored-by: Marijn Suijten <[email protected]>
@cwfitzgerald
Copy link
Member

Could we maybe get an example showing how to use DComp to make window transparency work?

I think we should, but we can do this as a follow up.

@cwfitzgerald
Copy link
Member

@n1ght-hunter casual poke!

Copy link
Member

@cwfitzgerald cwfitzgerald left a comment

Choose a reason for hiding this comment

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

I have a few final tweaks, but I'll push a commit to do them so we don't need to do another round of this.

@cwfitzgerald cwfitzgerald force-pushed the dev/windows-os-transparent branch from 3b85cea to a21b4d1 Compare September 11, 2025 00:09
@cwfitzgerald cwfitzgerald enabled auto-merge (squash) September 11, 2025 00:12
@cwfitzgerald cwfitzgerald merged commit 3902b0c into gfx-rs:trunk Sep 11, 2025
40 checks passed

This creates a single `IDCompositionVisual` over the entire window that is used by the `Surface`. If a user wants to manage the composition tree themselves, they should create their own device and composition, and pass the relevant visual down into `wgpu` via `IDCompositionTarget::CompositionVisual` or `IDCompositionTarget::SurfaceHandle`.
This creates a single `IDCompositionVisual` over the entire window that is used by the mf`Surface`. If a user wants to manage the composition tree themselves, they should create their own device and composition, and pass the relevant visual down into `wgpu` via `SurfaceTargetUnsafe::CompositionVisual`.
Copy link
Contributor

Choose a reason for hiding this comment

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

@cwfitzgerald what is an mf`Surface`?

Also SurfaceTargetUnsafe::SurfaceHandle is still an option, especially when shared across processes.

Copy link
Member

Choose a reason for hiding this comment

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

what is an mfSurface?

A typo 😅

Thanks for catching these issues, I'll push a PR fixing these up.

Comment on lines +472 to +473
/// - `DxgiFromVisual` or `Visual`
/// - `DxgiFromHwnd` or `Hwnd` for [`Self::DxgiFromHwnd`]
Copy link
Contributor

Choose a reason for hiding this comment

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

Does the first line miss for [`Self::DxgiFromVisual`], to match the Hwnd line?

/// This supports transparent windows, but does not support fullscreen optimization.
DirectComposition,
/// This creates a single [`IDCompositionVisual`] over the entire window that is used by the `Surface`.
/// If a user wants to manage the composition tree themselves, they should create their own device and
Copy link
Contributor

Choose a reason for hiding this comment

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

I hope it's clear to users that you don't mean to create a graphics device but an IDCompositionDevice 😅

@@ -449,29 +449,36 @@ impl NoopBackendOptions {
pub enum Dx12SwapchainKind {
/// Use a DXGI swapchain made directly from the window's HWND.
///
/// This supports fullscreen optimization, making borderless windows just as efficient as exclusive fullscreen. It does not support transparent windows.
/// This does not support transparency but has better support from developer tooling from RenderDoc.
Copy link
Contributor

Choose a reason for hiding this comment

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

from developer tooling like RenderDoc?

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