Repository: gfx-rs/wgpu (language: rust)

Issue:
DX12 with DirectComposition transparency not working
<!-- Thank you for filing this! Please read the [debugging tips](https://github.com/gfx-rs/wgpu/wiki/Debugging-wgpu-Applications).
That may let you investigate on your own, or provide additional information that helps us to assist.-->

**Description**

Using DX12 backend to render into a DirectComposition Visual, alpha channel doesn't affect the render.

**Repro steps**

The repro can be found here, https://github.com/amrbashir/wgpu-dcomp-repro/blob/master/src/main.rs

It is adapted from a wgpu basic example and windows-rs DirectComposition example https://github.com/microsoft/windows-rs/blob/master/crates/samples/windows/dcomp/src/main.rs 

**Expected vs observed behavior**

I expected to be able to create semi-transparent colors using DirectComposition + wpgu but instead, I only get `Opaque` composite alpha mode.

**Extra materials**

This is DirectComposition FillRectangle

![Image](https://github.com/user-attachments/assets/e206a04a-9b78-4c7a-9c3a-d18c8e5c6b38)

This is wgpu rendering to same visual, using the same color and alpha

![Image](https://github.com/user-attachments/assets/8d9c8af5-58cb-4364-a6e2-2bab0639a3b1)

**Platform**
Information about your OS, version of `wgpu`, your tech stack, etc.
OS: Windows 11
wpgu: 24
wgpu-backend: DX12
windows-rs: 0.58


Discussion / hints:
This should be a fairly simple fix - this is caused by us just never testing transparency with a DComp swapchain.

- https://github.com/gfx-rs/wgpu/blob/trunk/wgpu-hal/src/auxil/dxgi/conv.rs#L284 needs to be modified to actually properly map modes.
- https://github.com/gfx-rs/wgpu/blob/trunk/wgpu-hal/src/dx12/adapter.rs#L866 needs to present different lists if it's targetting a normal hwnd swapchain, or a DComp one.

Would you be able to submit a PR for this?
yeah sure I can take a crack at it, thanks for pointing out where I should look

Fix patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 78e6ebde2f..647bfc87ab 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -106,6 +106,7 @@ By @brodycj in [#6924](https://github.com/gfx-rs/wgpu/pull/6924).
 
 - Fix HLSL storage format generation. By @Vecvec in [#6993](https://github.com/gfx-rs/wgpu/pull/6993) and [#7104](https://github.com/gfx-rs/wgpu/pull/7104)
 - Fix 3D storage texture bindings. By @SparkyPotato in [#7071](https://github.com/gfx-rs/wgpu/pull/7071)
+- Fix DX12 composite alpha modes. By @amrbashir in [#7117](https://github.com/gfx-rs/wgpu/pull/7117)
 
 #### WebGPU
 
diff --git a/wgpu-hal/src/auxil/dxgi/conv.rs b/wgpu-hal/src/auxil/dxgi/conv.rs
index 2cf75cd4e2..13e6199b8b 100644
--- a/wgpu-hal/src/auxil/dxgi/conv.rs
+++ b/wgpu-hal/src/auxil/dxgi/conv.rs
@@ -281,6 +281,13 @@ pub fn map_vertex_format(format: wgt::VertexFormat) -> Dxgi::Common::DXGI_FORMAT
     }
 }
 
-pub fn map_acomposite_alpha_mode(_mode: wgt::CompositeAlphaMode) -> Dxgi::Common::DXGI_ALPHA_MODE {
-    Dxgi::Common::DXGI_ALPHA_MODE_IGNORE
+pub fn map_acomposite_alpha_mode(mode: wgt::CompositeAlphaMode) -> Dxgi::Common::DXGI_ALPHA_MODE {
+    match mode {
+        wgt::CompositeAlphaMode::PreMultiplied => Dxgi::Common::DXGI_ALPHA_MODE_PREMULTIPLIED,
+        wgt::CompositeAlphaMode::PostMultiplied => Dxgi::Common::DXGI_ALPHA_MODE_STRAIGHT,
+        wgt::CompositeAlphaMode::Opaque => Dxgi::Common::DXGI_ALPHA_MODE_IGNORE,
+        wgt::CompositeAlphaMode::Auto | wgt::CompositeAlphaMode::Inherit => {
+            Dxgi::Common::DXGI_ALPHA_MODE_UNSPECIFIED
+        }
+    }
 }
diff --git a/wgpu-hal/src/dx12/adapter.rs b/wgpu-hal/src/dx12/adapter.rs
index 8211fd9f94..edb03a3ffc 100644
--- a/wgpu-hal/src/dx12/adapter.rs
+++ b/wgpu-hal/src/dx12/adapter.rs
@@ -863,7 +863,18 @@ impl crate::Adapter for super::Adapter {
                 | wgt::TextureUses::COPY_SRC
                 | wgt::TextureUses::COPY_DST,
             present_modes,
-            composite_alpha_modes: vec![wgt::CompositeAlphaMode::Opaque],
+            composite_alpha_modes: match surface.target {
+                SurfaceTarget::WndHandle(_) => vec![wgt::CompositeAlphaMode::Opaque],
+                SurfaceTarget::Visual(_)
+                | SurfaceTarget::SurfaceHandle(_)
+                | SurfaceTarget::SwapChainPanel(_) => vec![
+                    wgt::CompositeAlphaMode::Auto,
+                    wgt::CompositeAlphaMode::Inherit,
+                    wgt::CompositeAlphaMode::Opaque,
+                    wgt::CompositeAlphaMode::PostMultiplied,
+                    wgt::CompositeAlphaMode::PreMultiplied,
+                ],
+            },
         })
     }
 


Test patch: