From d30c1e6a8f017ce3837fc97c2532508b49891100 Mon Sep 17 00:00:00 2001
From: Kusok <118438257+kus04e4ek@users.noreply.github.com>
Date: Fri, 26 Jul 2024 21:30:59 +0800
Subject: [PATCH] Fall back to D3D12 if Vulkan is not supported and vice versa
---
doc/classes/ProjectSettings.xml | 8 +++++
main/main.cpp | 3 ++
platform/windows/display_server_windows.cpp | 35 ++++++++++++++++++---
3 files changed, 42 insertions(+), 4 deletions(-)
diff --git a/doc/classes/ProjectSettings.xml b/doc/classes/ProjectSettings.xml
index 4f7f3728646..22c25dd5e57 100644
--- a/doc/classes/ProjectSettings.xml
+++ b/doc/classes/ProjectSettings.xml
@@ -2765,6 +2765,14 @@
Windows override for [member rendering/rendering_device/driver].
+
+ If [code]true[/code], the forward renderer will fall back to Direct3D 12 if Vulkan is not supported.
+ [b]Note:[/b] This setting is implemented only on Windows.
+
+
+ If [code]true[/code], the forward renderer will fall back to Vulkan if Direct3D 12 is not supported.
+ [b]Note:[/b] This setting is implemented only on Windows.
+
Enable the pipeline cache that is saved to disk if the graphics API supports it.
[b]Note:[/b] This property is unable to control the pipeline caching the GPU driver itself does. Only turn this off along with deleting the contents of the driver's cache if you wish to simulate the experience a user will get when starting the game for the first time.
diff --git a/main/main.cpp b/main/main.cpp
index e1d53e7f1be..c7c0c08fad9 100644
--- a/main/main.cpp
+++ b/main/main.cpp
@@ -1956,6 +1956,9 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph
GLOBAL_DEF_RST_NOVAL(PropertyInfo(Variant::STRING, "rendering/rendering_device/driver.android", PROPERTY_HINT_ENUM, driver_hints), default_driver);
GLOBAL_DEF_RST_NOVAL(PropertyInfo(Variant::STRING, "rendering/rendering_device/driver.ios", PROPERTY_HINT_ENUM, driver_hints), default_driver);
GLOBAL_DEF_RST_NOVAL(PropertyInfo(Variant::STRING, "rendering/rendering_device/driver.macos", PROPERTY_HINT_ENUM, driver_hints), default_driver);
+
+ GLOBAL_DEF_RST("rendering/rendering_device/fallback_to_vulkan", true);
+ GLOBAL_DEF_RST("rendering/rendering_device/fallback_to_d3d12", true);
}
{
diff --git a/platform/windows/display_server_windows.cpp b/platform/windows/display_server_windows.cpp
index f29048b16d7..ae8d1cace02 100644
--- a/platform/windows/display_server_windows.cpp
+++ b/platform/windows/display_server_windows.cpp
@@ -5929,10 +5929,37 @@ DisplayServerWindows::DisplayServerWindows(const String &p_rendering_driver, Win
if (rendering_context) {
if (rendering_context->initialize() != OK) {
- memdelete(rendering_context);
- rendering_context = nullptr;
- r_error = ERR_UNAVAILABLE;
- return;
+ bool failed = true;
+#if defined(VULKAN_ENABLED)
+ bool fallback_to_vulkan = GLOBAL_GET("rendering/rendering_device/fallback_to_vulkan");
+ if (failed && fallback_to_vulkan && rendering_driver != "vulkan") {
+ memdelete(rendering_context);
+ rendering_context = memnew(RenderingContextDriverVulkanWindows);
+ if (rendering_context->initialize() == OK) {
+ WARN_PRINT("Your video card drivers seem not to support Direct3D 12, switching to Vulkan.");
+ rendering_driver = "vulkan";
+ failed = false;
+ }
+ }
+#endif
+#if defined(D3D12_ENABLED)
+ bool fallback_to_d3d12 = GLOBAL_GET("rendering/rendering_device/fallback_to_d3d12");
+ if (failed && fallback_to_d3d12 && rendering_driver != "d3d12") {
+ memdelete(rendering_context);
+ rendering_context = memnew(RenderingContextDriverD3D12);
+ if (rendering_context->initialize() == OK) {
+ WARN_PRINT("Your video card drivers seem not to support Vulkan, switching to Direct3D 12.");
+ rendering_driver = "d3d12";
+ failed = false;
+ }
+ }
+#endif
+ if (failed) {
+ memdelete(rendering_context);
+ rendering_context = nullptr;
+ r_error = ERR_UNAVAILABLE;
+ return;
+ }
}
}
#endif