From 256e9fbcda3f981f99e6d82288869c23039b9f8c Mon Sep 17 00:00:00 2001 From: Berend Klein Haneveld Date: Thu, 10 Jun 2021 21:26:38 +0200 Subject: [PATCH 1/4] Force Vulkan only on Windows --- wgpu/backends/rs.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/wgpu/backends/rs.py b/wgpu/backends/rs.py index d22db11a..c6c5a966 100644 --- a/wgpu/backends/rs.py +++ b/wgpu/backends/rs.py @@ -30,6 +30,7 @@ import os +import sys import ctypes import logging import ctypes.util @@ -217,7 +218,9 @@ def request_adapter(self, *, canvas, power_preference=None): # mature than Metal too, so let's just force that for now. # See https://github.com/gfx-rs/wgpu/issues/1416 # force_backend = lib.WGPUBackendType_Vulkan - force_backend = enum_str2int["BackendType"]["Vulkan"] + force_backend = enum_str2int["BackendType"][ + "Vulkan" if sys.platform.startswith("win") else "Null" + ] # H: chain: WGPUChainedStruct, backend: WGPUBackendType extras = new_struct_p( From b9f1b3ea35ccf6b247af541fbd53175ee91049a5 Mon Sep 17 00:00:00 2001 From: Berend Klein Haneveld Date: Fri, 11 Jun 2021 13:19:24 +0200 Subject: [PATCH 2/4] Use WGPU_BACKEND_TYPE env for forcing backend type --- wgpu/backends/rs.py | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/wgpu/backends/rs.py b/wgpu/backends/rs.py index c6c5a966..c5435000 100644 --- a/wgpu/backends/rs.py +++ b/wgpu/backends/rs.py @@ -213,14 +213,35 @@ def request_adapter(self, *, canvas, power_preference=None): else: surface_id = get_surface_id_from_canvas(canvas) - # Force Vulkan on Windows, to avoid DX12 which seems to ignore - # the NVidia control panel settings. I guess Vulkan is more - # mature than Metal too, so let's just force that for now. + # Try to read the WGPU_BACKEND_TYPE environment variable to see + # if a backend should be forced. When you run into trouble with + # the automatic selection of wgpu, you can use this variable + # to force a specific backend. For instance, on Windows you + # might want to force Vulkan, to avoid DX12 which seems to ignore + # the NVidia control panel settings. # See https://github.com/gfx-rs/wgpu/issues/1416 - # force_backend = lib.WGPUBackendType_Vulkan - force_backend = enum_str2int["BackendType"][ - "Vulkan" if sys.platform.startswith("win") else "Null" - ] + force_backend = enum_str2int["BackendType"]["Null"] + if "WGPU_BACKEND_TYPE" in os.environ: + try: + backend = int(os.environ["WGPU_BACKEND_TYPE"]) + value = next( + ( + key + for key, value in enum_str2int["BackendType"].items() + if value == backend + ), + None, + ) + if not value: + logger.warn(f"Invalid value for WGPU_BACKEND_TYPE: '{backend}'") + else: + logger.warn(f"Forcing backend: {value} ({backend})") + force_backend = backend + except ValueError: + logger.warn( + "Could not parse value for WGPU_BACKEND_TYPE: " + f"{os.environ.get('WGPU_BACKEND_TYPE')}." + ) # H: chain: WGPUChainedStruct, backend: WGPUBackendType extras = new_struct_p( From 4477b3ddf5d05091730c187070f018df7d9ee765 Mon Sep 17 00:00:00 2001 From: Berend Klein Haneveld Date: Fri, 11 Jun 2021 13:29:45 +0200 Subject: [PATCH 3/4] Use backend name instead of int --- wgpu/backends/rs.py | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/wgpu/backends/rs.py b/wgpu/backends/rs.py index c5435000..df8ba765 100644 --- a/wgpu/backends/rs.py +++ b/wgpu/backends/rs.py @@ -223,24 +223,13 @@ def request_adapter(self, *, canvas, power_preference=None): force_backend = enum_str2int["BackendType"]["Null"] if "WGPU_BACKEND_TYPE" in os.environ: try: - backend = int(os.environ["WGPU_BACKEND_TYPE"]) - value = next( - ( - key - for key, value in enum_str2int["BackendType"].items() - if value == backend - ), - None, - ) - if not value: - logger.warn(f"Invalid value for WGPU_BACKEND_TYPE: '{backend}'") - else: - logger.warn(f"Forcing backend: {value} ({backend})") - force_backend = backend - except ValueError: + backend = os.environ["WGPU_BACKEND_TYPE"] + force_backend = enum_str2int["BackendType"][backend] + logger.warn(f"Forcing backend: {backend} ({force_backend})") + except KeyError: logger.warn( - "Could not parse value for WGPU_BACKEND_TYPE: " - f"{os.environ.get('WGPU_BACKEND_TYPE')}." + f"Invalid value for WGPU_BACKEND_TYPE: '{backend}'.\n" + f"Valid values are: {list(enum_str2int['BackendType'].keys())}" ) # H: chain: WGPUChainedStruct, backend: WGPUBackendType From 1a231f372eb5c88e15ca042aefeebce8181ea2bf Mon Sep 17 00:00:00 2001 From: Berend Klein Haneveld Date: Fri, 11 Jun 2021 13:32:17 +0200 Subject: [PATCH 4/4] Remove unused import --- wgpu/backends/rs.py | 1 - 1 file changed, 1 deletion(-) diff --git a/wgpu/backends/rs.py b/wgpu/backends/rs.py index df8ba765..7b69c719 100644 --- a/wgpu/backends/rs.py +++ b/wgpu/backends/rs.py @@ -30,7 +30,6 @@ import os -import sys import ctypes import logging import ctypes.util