From 4cc5177f6be93de23c38e9fe076d8eec0b335e34 Mon Sep 17 00:00:00 2001 From: Skyth <19259897+blueskythlikesclouds@users.noreply.github.com> Date: Mon, 3 Nov 2025 17:21:09 +0300 Subject: [PATCH] Create HWND swap chain when window transparency is disabled on D3D12. --- .../d3d12/rendering_device_driver_d3d12.cpp | 67 ++++++++++--------- platform/windows/display_server_windows.cpp | 4 +- 2 files changed, 40 insertions(+), 31 deletions(-) diff --git a/drivers/d3d12/rendering_device_driver_d3d12.cpp b/drivers/d3d12/rendering_device_driver_d3d12.cpp index 3c32692e538..c8b36362880 100644 --- a/drivers/d3d12/rendering_device_driver_d3d12.cpp +++ b/drivers/d3d12/rendering_device_driver_d3d12.cpp @@ -2932,6 +2932,15 @@ Error RenderingDeviceDriverD3D12::swap_chain_resize(CommandQueueID p_cmd_queue, _swap_chain_release(swap_chain); } +#ifdef DCOMP_ENABLED + bool create_for_composition = OS::get_singleton()->is_layered_allowed(); +#else + if (OS::get_singleton()->is_layered_allowed()) { + WARN_PRINT_ONCE("Window transparency is not supported without DirectComposition on D3D12."); + } + bool create_for_composition = false; +#endif + DXGI_SWAP_CHAIN_DESC1 swap_chain_desc = {}; if (swap_chain->d3d_swap_chain != nullptr) { _swap_chain_release_buffers(swap_chain); @@ -2945,7 +2954,7 @@ Error RenderingDeviceDriverD3D12::swap_chain_resize(CommandQueueID p_cmd_queue, swap_chain_desc.SampleDesc.Count = 1; swap_chain_desc.Flags = creation_flags; swap_chain_desc.Scaling = DXGI_SCALING_STRETCH; - if (OS::get_singleton()->is_layered_allowed()) { + if (create_for_composition) { swap_chain_desc.AlphaMode = DXGI_ALPHA_MODE_PREMULTIPLIED; has_comp_alpha[(uint64_t)p_cmd_queue.id] = true; } else { @@ -2956,16 +2965,12 @@ Error RenderingDeviceDriverD3D12::swap_chain_resize(CommandQueueID p_cmd_queue, swap_chain_desc.Height = surface->height; ComPtr swap_chain_1; -#ifdef DCOMP_ENABLED - res = context_driver->dxgi_factory_get()->CreateSwapChainForComposition(command_queue->d3d_queue.Get(), &swap_chain_desc, nullptr, swap_chain_1.GetAddressOf()); -#else - res = context_driver->dxgi_factory_get()->CreateSwapChainForHwnd(command_queue->d3d_queue.Get(), surface->hwnd, &swap_chain_desc, nullptr, nullptr, swap_chain_1.GetAddressOf()); - if (!SUCCEEDED(res) && swap_chain_desc.AlphaMode != DXGI_ALPHA_MODE_IGNORE) { - swap_chain_desc.AlphaMode = DXGI_ALPHA_MODE_IGNORE; - has_comp_alpha[(uint64_t)p_cmd_queue.id] = false; + if (create_for_composition) { + res = context_driver->dxgi_factory_get()->CreateSwapChainForComposition(command_queue->d3d_queue.Get(), &swap_chain_desc, nullptr, swap_chain_1.GetAddressOf()); + } else { res = context_driver->dxgi_factory_get()->CreateSwapChainForHwnd(command_queue->d3d_queue.Get(), surface->hwnd, &swap_chain_desc, nullptr, nullptr, swap_chain_1.GetAddressOf()); } -#endif + ERR_FAIL_COND_V(!SUCCEEDED(res), ERR_CANT_CREATE); swap_chain_1.As(&swap_chain->d3d_swap_chain); @@ -2976,34 +2981,36 @@ Error RenderingDeviceDriverD3D12::swap_chain_resize(CommandQueueID p_cmd_queue, } #ifdef DCOMP_ENABLED - if (surface->composition_device.Get() == nullptr) { - using PFN_DCompositionCreateDevice = HRESULT(WINAPI *)(IDXGIDevice *, REFIID, void **); - PFN_DCompositionCreateDevice pfn_DCompositionCreateDevice = (PFN_DCompositionCreateDevice)(void *)GetProcAddress(context_driver->lib_dcomp, "DCompositionCreateDevice"); - ERR_FAIL_NULL_V(pfn_DCompositionCreateDevice, ERR_CANT_CREATE); + if (create_for_composition) { + if (surface->composition_device.Get() == nullptr) { + using PFN_DCompositionCreateDevice = HRESULT(WINAPI *)(IDXGIDevice *, REFIID, void **); + PFN_DCompositionCreateDevice pfn_DCompositionCreateDevice = (PFN_DCompositionCreateDevice)(void *)GetProcAddress(context_driver->lib_dcomp, "DCompositionCreateDevice"); + ERR_FAIL_NULL_V(pfn_DCompositionCreateDevice, ERR_CANT_CREATE); - res = pfn_DCompositionCreateDevice(nullptr, IID_PPV_ARGS(surface->composition_device.GetAddressOf())); - ERR_FAIL_COND_V(!SUCCEEDED(res), ERR_CANT_CREATE); + res = pfn_DCompositionCreateDevice(nullptr, IID_PPV_ARGS(surface->composition_device.GetAddressOf())); + ERR_FAIL_COND_V(!SUCCEEDED(res), ERR_CANT_CREATE); - res = surface->composition_device->CreateTargetForHwnd(surface->hwnd, TRUE, surface->composition_target.GetAddressOf()); - ERR_FAIL_COND_V(!SUCCEEDED(res), ERR_CANT_CREATE); + res = surface->composition_device->CreateTargetForHwnd(surface->hwnd, TRUE, surface->composition_target.GetAddressOf()); + ERR_FAIL_COND_V(!SUCCEEDED(res), ERR_CANT_CREATE); - res = surface->composition_device->CreateVisual(surface->composition_visual.GetAddressOf()); - ERR_FAIL_COND_V(!SUCCEEDED(res), ERR_CANT_CREATE); + res = surface->composition_device->CreateVisual(surface->composition_visual.GetAddressOf()); + ERR_FAIL_COND_V(!SUCCEEDED(res), ERR_CANT_CREATE); - res = surface->composition_visual->SetContent(swap_chain->d3d_swap_chain.Get()); - ERR_FAIL_COND_V(!SUCCEEDED(res), ERR_CANT_CREATE); + res = surface->composition_visual->SetContent(swap_chain->d3d_swap_chain.Get()); + ERR_FAIL_COND_V(!SUCCEEDED(res), ERR_CANT_CREATE); - res = surface->composition_target->SetRoot(surface->composition_visual.Get()); - ERR_FAIL_COND_V(!SUCCEEDED(res), ERR_CANT_CREATE); + res = surface->composition_target->SetRoot(surface->composition_visual.Get()); + ERR_FAIL_COND_V(!SUCCEEDED(res), ERR_CANT_CREATE); - res = surface->composition_device->Commit(); - ERR_FAIL_COND_V(!SUCCEEDED(res), ERR_CANT_CREATE); - } else { - res = surface->composition_visual->SetContent(swap_chain->d3d_swap_chain.Get()); - ERR_FAIL_COND_V(!SUCCEEDED(res), ERR_CANT_CREATE); + res = surface->composition_device->Commit(); + ERR_FAIL_COND_V(!SUCCEEDED(res), ERR_CANT_CREATE); + } else { + res = surface->composition_visual->SetContent(swap_chain->d3d_swap_chain.Get()); + ERR_FAIL_COND_V(!SUCCEEDED(res), ERR_CANT_CREATE); - res = surface->composition_device->Commit(); - ERR_FAIL_COND_V(!SUCCEEDED(res), ERR_CANT_CREATE); + res = surface->composition_device->Commit(); + ERR_FAIL_COND_V(!SUCCEEDED(res), ERR_CANT_CREATE); + } } #endif diff --git a/platform/windows/display_server_windows.cpp b/platform/windows/display_server_windows.cpp index cd6633f6669..23d77dbabd8 100644 --- a/platform/windows/display_server_windows.cpp +++ b/platform/windows/display_server_windows.cpp @@ -2387,9 +2387,11 @@ void DisplayServerWindows::_get_window_style(bool p_main_window, bool p_initiali r_style |= WS_CLIPCHILDREN | WS_CLIPSIBLINGS; r_style_ex |= WS_EX_ACCEPTFILES; - if (OS::get_singleton()->get_current_rendering_driver_name() == "d3d12") { +#ifdef DCOMP_ENABLED + if (OS::get_singleton()->is_layered_allowed() && OS::get_singleton()->get_current_rendering_driver_name() == "d3d12") { r_style_ex |= WS_EX_NOREDIRECTIONBITMAP; } +#endif } void DisplayServerWindows::_update_window_style(WindowID p_window, bool p_repaint) {