Skip to content

Commit

Permalink
Add better async support for wgpu-native (#609)
Browse files Browse the repository at this point in the history
* Add proper async support for wgpu-native (to the extend that native supports it)

* fix codegen

* Apply for improved codegen
  • Loading branch information
almarklein authored Oct 2, 2024
1 parent b73abc3 commit 95d01d9
Show file tree
Hide file tree
Showing 5 changed files with 275 additions and 108 deletions.
17 changes: 11 additions & 6 deletions codegen/apipatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -648,12 +648,17 @@ def apply(self, code):
# Test that a matching check is done
unchecked = method_structs.difference(checked)
unchecked = list(sorted(unchecked.difference(ignore_structs)))
if (
methodname.endswith("_async")
and f"return self.{methodname[:-7]}" in code
):
pass
elif unchecked:
# Must we check, or does this method defer to another
defer_func_name = "_" + methodname
defer_line_starts = (
f"return self.{defer_func_name[:-7]}",
f"awaitable = self.{defer_func_name[:-7]}",
)
this_method_defers = any(
line.strip().startswith(defer_line_starts)
for line in code.splitlines()
)
if not this_method_defers and unchecked:
msg = f"missing check_struct in {methodname}: {unchecked}"
self.insert_line(j1, f"# FIXME: {msg}")
print(f"ERROR: {msg}")
Expand Down
16 changes: 12 additions & 4 deletions wgpu/_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ class GPU:
# IDL: Promise<GPUAdapter?> requestAdapter(optional GPURequestAdapterOptions options = {}); -> GPUPowerPreference powerPreference, boolean forceFallbackAdapter = false
@apidiff.change("arguments include canvas")
def request_adapter_sync(
self, *, power_preference=None, force_fallback_adapter=False, canvas=None
self,
*,
power_preference: enums.GPUPowerPreference = None,
force_fallback_adapter: bool = False,
canvas=None,
):
"""Sync version of `request_adapter_async()`.
Expand All @@ -108,7 +112,11 @@ def request_adapter_sync(
# IDL: Promise<GPUAdapter?> requestAdapter(optional GPURequestAdapterOptions options = {}); -> GPUPowerPreference powerPreference, boolean forceFallbackAdapter = false
@apidiff.change("arguments include canvas")
async def request_adapter_async(
self, *, power_preference=None, force_fallback_adapter=False, canvas=None
self,
*,
power_preference: enums.GPUPowerPreference = None,
force_fallback_adapter: bool = False,
canvas=None,
):
"""Create a `GPUAdapter`, the object that represents an abstract wgpu
implementation, from which one can request a `GPUDevice`.
Expand All @@ -117,8 +125,8 @@ async def request_adapter_async(
power_preference (PowerPreference): "high-performance" or "low-power".
force_fallback_adapter (bool): whether to use a (probably CPU-based)
fallback adapter.
canvas (WgpuCanvasInterface): The canvas that the adapter should
be able to render to. This can typically be left to None.
canvas : The canvas that the adapter should be able to render to. This can typically
be left to None. If given, the object must implement ``WgpuCanvasInterface``.
"""
# If this method gets called, no backend has been loaded yet, let's do that now!
from .backends.auto import gpu
Expand Down
Loading

0 comments on commit 95d01d9

Please sign in to comment.