Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update to wgpu-native 22.1 #547

Merged
merged 45 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
ec811b7
update to latest idl
Vipitis Aug 4, 2024
2d02899
update headers
Vipitis Aug 4, 2024
eabef10
fix adapter properties
Vipitis Aug 4, 2024
18ee534
fix perferred format
Vipitis Aug 4, 2024
0b6f02d
remaining report
Vipitis Aug 4, 2024
0f3d294
fix error callback
Vipitis Aug 5, 2024
ee686c5
set default format
Vipitis Aug 5, 2024
a72ce93
cube progress
Vipitis Aug 6, 2024
5f10556
working cube and triangle
Vipitis Aug 8, 2024
57600f1
make ._release() part of end()
Vipitis Aug 10, 2024
299c29c
implement _get_surface_capabilities as private method
Vipitis Aug 10, 2024
afdfe42
update whitespaces and ids in expected errrors
Vipitis Aug 10, 2024
8e19cc5
surpress depth slice warning
Vipitis Aug 10, 2024
880bb0f
use tagged release
Vipitis Aug 29, 2024
734985d
handle device lost as error too
Vipitis Sep 1, 2024
b806471
lint
Vipitis Sep 1, 2024
e02c878
decode error type from message
Vipitis Sep 1, 2024
47b11f2
update to 22.1.0.2
Vipitis Sep 3, 2024
c5ec55d
Merge branch 'main' into wgpu22-1
almarklein Sep 4, 2024
ee8ec56
Merge branch 'main' into wgpu22-1
almarklein Sep 4, 2024
d9bf136
remove typo
almarklein Sep 4, 2024
e75c3ff
Yes, device must be passed to constructor, not self
almarklein Sep 4, 2024
b628c8f
device-lost
almarklein Sep 4, 2024
d6975c9
bind stuff to device, not adapter
almarklein Sep 5, 2024
4117b42
check errors
almarklein Sep 9, 2024
1bf48ec
allow rust to release adpater-info members
almarklein Sep 9, 2024
9f99b23
tone_mapping is a placeholder for now
almarklein Sep 9, 2024
dcd2cc8
cleanup
almarklein Sep 9, 2024
a226748
disable on_submitted_work_done, and add comments
almarklein Sep 9, 2024
c8511ed
todos, flake
almarklein Sep 9, 2024
1fd8b73
fix memtests
almarklein Sep 9, 2024
89fbde4
flake and clean
almarklein Sep 9, 2024
e59ec66
help pypy maybe
almarklein Sep 9, 2024
572c434
codegen
almarklein Sep 9, 2024
989db92
guard against null values
almarklein Sep 9, 2024
b49ba8e
revert storing pass encoders
almarklein Sep 9, 2024
ec93171
Merge branch 'wgpu22-1' of github.com:Vipitis/wgpu-py into wgpu22-1
almarklein Sep 9, 2024
4505f0b
refactor capabilities
almarklein Sep 9, 2024
7303636
fix
almarklein Sep 9, 2024
916047d
Fix that surfaces were not compatible
almarklein Sep 11, 2024
be1ceb2
Update how to handle capabilities of incompatible surfaces
almarklein Sep 11, 2024
a32db95
black/flake
almarklein Sep 11, 2024
af66a97
clean up
almarklein Sep 11, 2024
0434de7
codegen
almarklein Sep 11, 2024
dc44af9
Merge branch 'main' into wgpu22-1
almarklein Sep 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion examples/cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@

# Prepare present context
present_context = canvas.get_context()
render_texture_format = present_context.get_preferred_format(device.adapter)
# render_texture_format = present_context.get_preferred_format(device.adapter)
render_texture_format = None # triggers the preferred format now?
present_context.configure(device=device, format=render_texture_format)


Expand Down
8 changes: 5 additions & 3 deletions wgpu/_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ def configure(
usage: "flags.TextureUsage" = 0x10,
view_formats: "List[enums.TextureFormat]" = [],
color_space: str = "srgb",
tone_mapping: "structs.CanvasToneMapping" = {},
alpha_mode: "enums.CanvasAlphaMode" = "opaque",
):
"""Configures the presentation context for the associated canvas.
Expand All @@ -222,6 +223,7 @@ def configure(
color_space (PredefinedColorSpace): The color space that values written
into textures returned by ``get_current_texture()`` should be displayed with.
Default "srgb".
#TODO: docstring for tone_mapping
alpha_mode (enums.CanvasAlphaMode): Determines the effect that alpha values
will have on the content of textures returned by ``get_current_texture()``
when read, displayed, or used as an image source. Default "opaque".
Expand Down Expand Up @@ -380,16 +382,16 @@ def is_fallback_adapter(self):
# IDL: [SameObject] readonly attribute GPUAdapterInfo info;
@property
def info(self):
"""A dict with information about this adapter, such as the vendor and devicen name."""
"""A dict with information about this adapter, such as the vendor and device name."""
# Note: returns a dict rather than an GPUAdapterInfo instance.
return self._adapter_info

@apidiff.add("Useful in multi-gpu environments")
@property
def summary(self):
"""A one-line summary of the info of this adapter (name, adapter_type, backend_type)."""
"""A one-line summary of the info of this adapter (description, adapter_type, backend_type)."""
d = self._adapter_info
return f"{d['device']} ({d['adapter_type']}) via {d['backend_type']}"
return f"{d['description']} ({d['adapter_type']}) via {d['backend_type']}"
almarklein marked this conversation as resolved.
Show resolved Hide resolved


class GPUObjectBase:
Expand Down
4 changes: 2 additions & 2 deletions wgpu/backends/wgpu_native/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@


# The wgpu-native version that we target/expect
__version__ = "0.19.4.1"
__commit_sha__ = "d89e5a93f5fd0bb3ce28f09571dbbefea37712c9"
__version__ = "22.1.0"
__commit_sha__ = "7304430548b11110b157bb29f620bd0b3e8e77f1"
version_info = tuple(map(int, __version__.split(".")))
_check_expected_version(version_info) # produces a warning on mismatch

Expand Down
121 changes: 74 additions & 47 deletions wgpu/backends/wgpu_native/_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ def callback(status, result, message, userdata):
nonlocal adapter_id
adapter_id = result

# H: void f(WGPUInstance instance, WGPURequestAdapterOptions const * options, WGPURequestAdapterCallback callback, void * userdata)
# H: void f(WGPUInstance instance, WGPURequestAdapterOptions const * options, WGPUInstanceRequestAdapterCallback callback, void * userdata)
libf.wgpuInstanceRequestAdapter(get_wgpu_instance(), struct, callback, ffi.NULL)

# For now, Rust will call the callback immediately
Expand Down Expand Up @@ -329,22 +329,22 @@ def enumerate_adapters(self):
def _create_adapter(self, adapter_id):
# ----- Get adapter info

# H: nextInChain: WGPUChainedStructOut *, vendorID: int, vendorName: char *, architecture: char *, deviceID: int, name: char *, driverDescription: char *, adapterType: WGPUAdapterType, backendType: WGPUBackendType
# H: nextInChain: WGPUChainedStructOut *, vendor: char *, architecture: char *, device: char *, description: char *, backendType: WGPUBackendType, adapterType: WGPUAdapterType, vendorID: int, deviceID: int
c_properties = new_struct_p(
"WGPUAdapterProperties *",
"WGPUAdapterInfo *",
# not used: nextInChain
# not used: deviceID
# not used: vendorID
# not used: name
# not used: driverDescription
# not used: adapterType
# not used: backendType
# not used: vendorName
# not used: vendor
# not used: architecture
# not used: device
# not used: description
# not used: backendType
# not used: adapterType
# not used: vendorID
# not used: deviceID
)

# H: void f(WGPUAdapter adapter, WGPUAdapterProperties * properties)
libf.wgpuAdapterGetProperties(adapter_id, c_properties)
# H: void f(WGPUAdapter adapter, WGPUAdapterInfo * info)
libf.wgpuAdapterGetInfo(adapter_id, c_properties)

def to_py_str(key):
char_p = getattr(c_properties, key)
Expand All @@ -354,12 +354,13 @@ def to_py_str(key):

# Populate a dict according to the WebGPU spec: https://gpuweb.github.io/gpuweb/#gpuadapterinfo
# And add all other info we get from wgpu-native too.
# note: description is human readable. device is a PCI ID.
adapter_info = {
# Spec
"vendor": to_py_str("vendorName"),
"vendor": to_py_str("vendor"),
"architecture": to_py_str("architecture"),
"device": to_py_str("name"),
"description": to_py_str("driverDescription"),
"device": to_py_str("device"),
"description": to_py_str("description"),
# Extra
"vendor_id": c_properties.vendorID,
"device_id": c_properties.deviceID,
Expand Down Expand Up @@ -389,7 +390,11 @@ def to_py_str(key):
# WebGPU features
features = set()
for f in sorted(enums.FeatureName):
if f in ["clip-distances"]:
if f in [
"clip-distances",
"dual-source-blending",
"texture-compression-bc-sliced-3d",
]:
continue # not supported by wgpu-native yet
key = f"FeatureName.{f}"
i = enummap[key]
Expand Down Expand Up @@ -434,6 +439,7 @@ def _get_surface_id(self):
self._surface_id = get_surface_id_from_canvas(self._get_canvas())
return self._surface_id

# FIXME: was configure(self, *, device: "GPUDevice", format: "enums.TextureFormat", usage: "flags.TextureUsage" = 0x10, view_formats: "List[enums.TextureFormat]" = [], color_space: str = "srgb", alpha_mode: "enums.CanvasAlphaMode" = "opaque"):
def configure(
self,
*,
Expand All @@ -442,6 +448,7 @@ def configure(
usage: "flags.TextureUsage" = 0x10,
view_formats: "List[enums.TextureFormat]" = [],
color_space: str = "srgb",
tone_mapping: "structs.CanvasToneMapping" = {},
alpha_mode: "enums.CanvasAlphaMode" = "opaque",
):
# Handle inputs
Expand All @@ -458,17 +465,19 @@ def configure(
c_view_formats = ffi.new("WGPUTextureFormat []", view_formats_list)
# Lookup alpha mode, needs explicit conversion because enum names mismatch
c_alpha_mode = getattr(lib, f"WGPUCompositeAlphaMode_{alpha_mode.capitalize()}")
# The format is used as-is
if format is None:
format = self.get_preferred_format(device.adapter)
# The color_space is not used for now
color_space
# TODO: tone mapping ?
check_struct("CanvasToneMapping", tone_mapping)

# Get what's supported

# H: nextInChain: WGPUChainedStructOut *, formatCount: int, formats: WGPUTextureFormat *, presentModeCount: int, presentModes: WGPUPresentMode *, alphaModeCount: int, alphaModes: WGPUCompositeAlphaMode *
formats = ffi.new("WGPUTextureFormat []", [1, 2])

# H: nextInChain: WGPUChainedStructOut *, usages: WGPUTextureUsageFlags/int, formatCount: int, formats: WGPUTextureFormat *, presentModeCount: int, presentModes: WGPUPresentMode *, alphaModeCount: int, alphaModes: WGPUCompositeAlphaMode *
capabilities = new_struct_p(
"WGPUSurfaceCapabilities *",
usages=usage,
# not used: formatCount
# not used: formats
# not used: presentModeCount
Expand Down Expand Up @@ -499,7 +508,13 @@ def configure(
str_val = enum_int2str["CompositeAlphaMode"][int_val]
capable_alpha_modes.append(str_val.lower())

# H: void f(WGPUSurfaceCapabilities capabilities)
# The format is used as-is
if format is None and capabilities.formatCount > 0:
format = enum_int2str["TextureFormat"][capabilities.formats[0]]
else:
raise RuntimeError("No formats supported by the surface.") # doesn't really make sense, maybe should be higher up.
almarklein marked this conversation as resolved.
Show resolved Hide resolved

# H: void f(WGPUSurfaceCapabilities surfaceCapabilities)
libf.wgpuSurfaceCapabilitiesFreeMembers(capabilities[0])

# Check if input is supported
Expand Down Expand Up @@ -726,12 +741,12 @@ def present(self):
libf.wgpuSurfacePresent(self._get_surface_id())
self._drop_texture()

# TODO: replace according to https://github.com/webgpu-native/webgpu-headers/issues/290
def get_preferred_format(self, adapter):
# H: WGPUTextureFormat f(WGPUSurface surface, WGPUAdapter adapter)
format = libf.wgpuSurfaceGetPreferredFormat(
self._get_surface_id(), adapter._internal
)
return enum_int2str["TextureFormat"][format]
if self._config is not None:
return enum_int2str["TextureFormat"][self._config.formats[0]]
# to keep old code working, returning None as a quick fix.
return None

def _release(self):
self._drop_texture()
Expand Down Expand Up @@ -843,9 +858,30 @@ def device_lost_callback(c_reason, c_message, userdata):
# Keep the ref alive
self._device_lost_callback = device_lost_callback

# ----- Uncaptured error

# TODO: this is moved from the uncaptuedErrroCallback that was in GPUDevice previously, test if it works once all other changes are fixed.
@ffi.callback("void(WGPUErrorType, char *, void *)")
def uncaptured_error_callback(c_type, c_message, userdata):
error_type = enum_int2str["ErrorType"].get(c_type, "Unknown")
message = ffi.string(c_message).decode(errors="ignore")
message = "\n".join(line.rstrip() for line in message.splitlines())
error_handler.handle_error(error_type, message)

# keep the ref alive
self._error_callback = uncaptured_error_callback

# H: nextInChain: WGPUChainedStruct *, callback: WGPUErrorCallback, userdata: void *
uncaptured_error_callback_info = new_struct(
"WGPUUncapturedErrorCallbackInfo",
callback=uncaptured_error_callback,
userdata=ffi.NULL,
# not used: nextInChain
)

# ----- Request device

# H: nextInChain: WGPUChainedStruct *, label: char *, requiredFeatureCount: int, requiredFeatures: WGPUFeatureName *, requiredLimits: WGPURequiredLimits *, defaultQueue: WGPUQueueDescriptor, deviceLostCallback: WGPUDeviceLostCallback, deviceLostUserdata: void *
# H: nextInChain: WGPUChainedStruct *, label: char *, requiredFeatureCount: int, requiredFeatures: WGPUFeatureName *, requiredLimits: WGPURequiredLimits *, defaultQueue: WGPUQueueDescriptor, deviceLostCallback: WGPUDeviceLostCallback, deviceLostUserdata: void *, uncapturedErrorCallbackInfo: WGPUUncapturedErrorCallbackInfo
struct = new_struct_p(
"WGPUDeviceDescriptor *",
label=to_c_label(label),
Expand All @@ -855,6 +891,7 @@ def device_lost_callback(c_reason, c_message, userdata):
requiredLimits=c_required_limits,
defaultQueue=queue_struct,
deviceLostCallback=device_lost_callback,
uncapturedErrorCallbackInfo=uncaptured_error_callback_info,
# not used: deviceLostUserdata
)

Expand All @@ -871,7 +908,7 @@ def callback(status, result, message, userdata):
nonlocal device_id
device_id = result

# H: void f(WGPUAdapter adapter, WGPUDeviceDescriptor const * descriptor, WGPURequestDeviceCallback callback, void * userdata)
# H: void f(WGPUAdapter adapter, WGPUDeviceDescriptor const * descriptor, WGPUAdapterRequestDeviceCallback callback, void * userdata)
libf.wgpuAdapterRequestDevice(self._internal, struct, callback, ffi.NULL)

if device_id is None: # pragma: no cover
Expand All @@ -896,7 +933,11 @@ def callback(status, result, message, userdata):
# WebGPU features
features = set()
for f in sorted(enums.FeatureName):
if f in ["clip-distances"]:
if f in [
"clip-distances",
"dual-source-blending",
"texture-compression-bc-sliced-3d",
]:
continue # not supported by wgpu-native yet
key = f"FeatureName.{f}"
i = enummap[key]
Expand Down Expand Up @@ -946,21 +987,6 @@ class GPUDevice(classes.GPUDevice, GPUObjectBase):
def __init__(self, label, internal, adapter, features, limits, queue):
super().__init__(label, internal, adapter, features, limits, queue)

@ffi.callback("void(WGPUErrorType, char *, void *)")
def uncaptured_error_callback(c_type, c_message, userdata):
error_type = enum_int2str["ErrorType"].get(c_type, "Unknown")
message = ffi.string(c_message).decode(errors="ignore")
message = "\n".join(line.rstrip() for line in message.splitlines())
error_handler.handle_error(error_type, message)

# Keep the ref alive
self._uncaptured_error_callback = uncaptured_error_callback

# H: void f(WGPUDevice device, WGPUErrorCallback callback, void * userdata)
libf.wgpuDeviceSetUncapturedErrorCallback(
self._internal, uncaptured_error_callback, ffi.NULL
)

def _poll(self):
# Internal function
if self._internal:
Expand Down Expand Up @@ -1790,7 +1816,7 @@ def callback(status_, user_data_p):

# Map it
self._map_state = enums.BufferMapState.pending
# H: void f(WGPUBuffer buffer, WGPUMapModeFlags mode, size_t offset, size_t size, WGPUBufferMapCallback callback, void * userdata)
# H: void f(WGPUBuffer buffer, WGPUMapModeFlags mode, size_t offset, size_t size, WGPUBufferMapAsyncCallback callback, void * userdata)
libf.wgpuBufferMapAsync(
self._internal, map_mode, offset, size, callback, ffi.NULL
)
Expand Down Expand Up @@ -2063,7 +2089,7 @@ def get_compilation_info(self):
# else:
# pass
#
# H: void f(WGPUShaderModule shaderModule, WGPUCompilationInfoCallback callback, void * userdata)
# H: void f(WGPUShaderModule shaderModule, WGPUShaderModuleGetCompilationInfoCallback callback, void * userdata)
# libf.wgpuShaderModuleGetCompilationInfo(self._internal, callback, ffi.NULL)
#
# self._device._poll()
Expand Down Expand Up @@ -2367,7 +2393,7 @@ def begin_render_pass(
b=clear_value[2],
a=clear_value[3],
)
# H: nextInChain: WGPUChainedStruct *, view: WGPUTextureView, resolveTarget: WGPUTextureView, loadOp: WGPULoadOp, storeOp: WGPUStoreOp, clearValue: WGPUColor
# H: nextInChain: WGPUChainedStruct *, view: WGPUTextureView, depthSlice: int, resolveTarget: WGPUTextureView, loadOp: WGPULoadOp, storeOp: WGPUStoreOp, clearValue: WGPUColor
c_attachment = new_struct(
"WGPURenderPassColorAttachment",
view=texture_view_id,
Expand All @@ -2377,6 +2403,7 @@ def begin_render_pass(
clearValue=c_clear_value,
# not used: resolveTarget
# not used: nextInChain
# not used: depthSlice
)
c_color_attachments_list.append(c_attachment)
c_color_attachments_array = ffi.new(
Expand Down
9 changes: 5 additions & 4 deletions wgpu/backends/wgpu_native/_mappings.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

# flake8: noqa

# There are 235 enum mappings
# There are 236 enum mappings

enummap = {
"AddressMode.clamp-to-edge": 2,
Expand Down Expand Up @@ -48,7 +48,8 @@
"CullMode.back": 2,
"CullMode.front": 1,
"CullMode.none": 0,
"DeviceLostReason.destroyed": 1,
"DeviceLostReason.destroyed": 2,
"DeviceLostReason.unknown": 1,
"ErrorFilter.internal": 2,
"ErrorFilter.out-of-memory": 1,
"ErrorFilter.validation": 0,
Expand Down Expand Up @@ -336,8 +337,8 @@
5: "DeviceLost",
},
"DeviceLostReason": {
0: "unknown",
1: "destroyed",
1: "unknown",
2: "destroyed",
},
"TextureFormat": {
0: "Undefined",
Expand Down
Loading
Loading