Skip to content

Commit 1c0ff51

Browse files
authored
Update to latest Webgpu (#166)
* new idl * apply * update changelog * fix * fix docs * black
1 parent decd887 commit 1c0ff51

File tree

11 files changed

+303
-71
lines changed

11 files changed

+303
-71
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Added:
3333

3434
Changed:
3535

36+
* Renamed `renderpass.set_blend_color` -> `set_blend_constant`.
3637
* Stricter validation of SpirV shaders.
3738
* Float32 texture formats must now use a non-filtering sampler and texture-sample-type.
3839
* Integer texture formats can no longer use a texture (use `textureLoad` instead).

codegen/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ tests, because we are the only users. If it breaks, we fix it.
2424

2525
## Links
2626

27-
* Spec and IDL: https://gpuweb.github.io/gpuweb/
27+
* WebGPU Spec: https://gpuweb.github.io/gpuweb/
28+
* IDL: https://gpuweb.github.io/gpuweb/webgpu.idl
2829
* C header: https://github.com/gfx-rs/wgpu/blob/master/ffi/wgpu.h
2930

3031

codegen/idlparser.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,10 @@ def resolve_type(self, typename):
224224
return name # ok
225225
elif name in self.classes:
226226
return f"'{name}'" # ok, but wrap in string because can be declared later
227+
elif name.startswith("HTML"):
228+
return "object" # anything, we ignore this stuff anyway
229+
elif name in ["OffscreenCanvas"]:
230+
return "object"
227231
else:
228232
assert name.startswith("GPU")
229233
name = name[3:]

docs/reference_wgpu.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,3 +245,6 @@ Other
245245

246246
.. autoclass:: wgpu.GPUUncapturedErrorEvent
247247
:members:
248+
249+
.. autoclass:: wgpu.GPUExternalTexture
250+
:members:

tests/test_rs_render.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ def test_render_orange_square_scissor():
422422
def cb(renderpass):
423423
renderpass.set_scissor_rect(0, 0, 32, 32)
424424
# Alse set blend color. Does not change outout, but covers the call.
425-
renderpass.set_blend_color((0, 0, 0, 1))
425+
renderpass.set_blend_constant((0, 0, 0, 1))
426426

427427
# Bindings and layout
428428
bind_group = None

wgpu/backends/rs.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,8 @@ def create_bind_group_layout(
588588
raise ValueError(
589589
"Bind group layout entry did not contain field 'buffer', 'sampler', 'texture', nor 'storage_texture'"
590590
)
591+
# Unreachable - fool the codegen
592+
check_struct("ExternalTextureBindingLayout", info)
591593
# H: nextInChain: WGPUChainedStruct *, binding: int, visibility: WGPUShaderStageFlags/int, buffer: WGPUBufferBindingLayout, sampler: WGPUSamplerBindingLayout, texture: WGPUTextureBindingLayout, storageTexture: WGPUStorageTextureBindingLayout
592594
c_entry = new_struct(
593595
"WGPUBindGroupLayoutEntry",
@@ -1802,7 +1804,7 @@ def set_scissor_rect(self, x, y, width, height):
18021804
self._internal, int(x), int(y), int(width), int(height)
18031805
)
18041806

1805-
def set_blend_color(self, color):
1807+
def set_blend_constant(self, color):
18061808
color = _tuple_from_tuple_or_dict(color, "rgba")
18071809
# H: r: float, g: float, b: float, a: float
18081810
c_color = new_struct_p(
@@ -2034,18 +2036,18 @@ def read_texture(self, source, data_layout, size):
20342036

20352037
return data
20362038

2037-
# FIXME: new method to implement -> does not exist in wgpu-native
2038-
def copy_image_bitmap_to_texture(self, source, destination, copy_size):
2039-
raise NotImplementedError()
2040-
20412039
# FIXME: new method to implement
20422040
def on_submitted_work_done(self):
20432041
raise NotImplementedError()
20442042

20452043

20462044
class GPUSwapChain(base.GPUSwapChain, GPUObjectBase):
2047-
def __init__(self, label, internal, device, canvas, format, usage):
2048-
super().__init__(label, internal, device, canvas, format, usage)
2045+
def __init__(
2046+
self, label, internal, device, canvas, format, usage, compositing_alpha_mode
2047+
):
2048+
super().__init__(
2049+
label, internal, device, canvas, format, usage, compositing_alpha_mode
2050+
)
20492051
assert internal is None # we set it later
20502052
self._surface_size = (-1, -1)
20512053
self._surface_id = None
@@ -2137,6 +2139,10 @@ class GPUUncapturedErrorEvent(base.GPUUncapturedErrorEvent):
21372139
pass
21382140

21392141

2142+
class GPUExternalTexture(base.GPUExternalTexture, GPUObjectBase):
2143+
pass
2144+
2145+
21402146
# %%
21412147

21422148

wgpu/base.py

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,14 @@
3030
"GPUBuffer",
3131
"GPUTexture",
3232
"GPUTextureView",
33+
"GPUExternalTexture",
3334
"GPUSampler",
3435
"GPUBindGroupLayout",
3536
"GPUBindGroup",
3637
"GPUPipelineLayout",
38+
"GPUShaderModule",
3739
"GPUCompilationMessage",
3840
"GPUCompilationInfo",
39-
"GPUShaderModule",
4041
"GPUPipelineBase",
4142
"GPUComputePipeline",
4243
"GPURenderPipeline",
@@ -125,17 +126,21 @@ def configure_swap_chain(
125126
device: "GPUDevice",
126127
format: "enums.TextureFormat",
127128
usage: "flags.TextureUsage" = 0x10,
129+
compositing_alpha_mode: "enums.CanvasCompositingAlphaMode" = "opaque",
128130
):
129131
"""Get a :class:`GPUSwapChain` object for this canvas.
130132
131133
Arguments:
132134
device (WgpuDevice): The GPU device object.
133135
format (TextureFormat): The texture format, e.g. "bgra8unorm-srgb".
134136
usage (TextureUsage): Default ``TextureUsage.OUTPUT_ATTACHMENT``.
137+
compositing_alpha_mode (CanvasCompositingAlphaMode): Default opaque.
135138
"""
136139
usage = usage or flags.TextureUsage.RENDER_ATTACHMENT
137140
GPUSwapChain = sys.modules[device.__module__].GPUSwapChain # noqa: N806
138-
return GPUSwapChain(label, None, device, self, format, usage)
141+
return GPUSwapChain(
142+
label, None, device, self, format, usage, compositing_alpha_mode
143+
)
139144

140145
# IDL: GPUTextureFormat getSwapChainPreferredFormat(GPUAdapter adapter);
141146
def get_swap_chain_preferred_format(self, adapter):
@@ -543,7 +548,7 @@ def create_shader_module(self, *, label="", code: str, source_map: dict = None):
543548
Arguments:
544549
label (str): A human readable label. Optional.
545550
code (str | bytes): The shader code, as WGSL text or binary SpirV
546-
(or an object implementing ``to_spirv()`` or ``to_bytes()``).
551+
(or an object implementing ``to_spirv()`` or ``to_bytes()``).
547552
"""
548553
raise NotImplementedError()
549554

@@ -781,6 +786,17 @@ def push_error_scope(self, filter):
781786
def pop_error_scope(self):
782787
raise NotImplementedError()
783788

789+
# IDL: GPUExternalTexture importExternalTexture(GPUExternalTextureDescriptor descriptor);
790+
@apidiff.hide("Specific to browsers.")
791+
def import_external_texture(
792+
self,
793+
*,
794+
label="",
795+
source: object,
796+
color_space: "enums.PredefinedColorSpace" = "srgb",
797+
):
798+
raise NotImplementedError()
799+
784800

785801
class GPUBuffer(GPUObjectBase):
786802
"""
@@ -1494,8 +1510,8 @@ def set_scissor_rect(self, x, y, width, height):
14941510
"""
14951511
raise NotImplementedError()
14961512

1497-
# IDL: undefined setBlendColor(GPUColor color);
1498-
def set_blend_color(self, color):
1513+
# IDL: undefined setBlendConstant(GPUColor color);
1514+
def set_blend_constant(self, color):
14991515
"""Set the blend color for the render pass.
15001516
15011517
Arguments:
@@ -1592,13 +1608,6 @@ def submit(self, command_buffers):
15921608
"""
15931609
raise NotImplementedError()
15941610

1595-
# IDL: undefined copyImageBitmapToTexture( GPUImageCopyImageBitmap source, GPUImageCopyTexture destination, GPUExtent3D copySize);
1596-
def copy_image_bitmap_to_texture(self, source, destination, copy_size):
1597-
"""
1598-
TODO: not yet available in wgpu-native
1599-
"""
1600-
raise NotImplementedError()
1601-
16021611
# IDL: undefined writeBuffer( GPUBuffer buffer, GPUSize64 bufferOffset, [AllowShared] BufferSource data, optional GPUSize64 dataOffset = 0, optional GPUSize64 size);
16031612
def write_buffer(self, buffer, buffer_offset, data, data_offset=0, size=None):
16041613
"""Takes the data contents and schedules a write operation of
@@ -1683,6 +1692,11 @@ def on_submitted_work_done(self):
16831692
"""TODO"""
16841693
raise NotImplementedError()
16851694

1695+
# IDL: undefined copyExternalImageToTexture( GPUImageCopyExternalImage source, GPUImageCopyTexture destination, GPUExtent3D copySize);
1696+
@apidiff.hide("Specific to browsers.")
1697+
def copy_external_image_to_texture(self, source, destination, copy_size):
1698+
raise NotImplementedError()
1699+
16861700

16871701
@apidiff.change(
16881702
"the swapchain should be used as a context manager to obtain the texture view"
@@ -1714,11 +1728,14 @@ class GPUSwapChain(GPUObjectBase):
17141728
You can obtain a swap chain using :func:`device.configure_swap_chain() <GPUDevice.configure_swap_chain>`.
17151729
"""
17161730

1717-
def __init__(self, label, internal, device, canvas, format, usage):
1731+
def __init__(
1732+
self, label, internal, device, canvas, format, usage, compositing_alpha_mode
1733+
):
17181734
super().__init__(label, internal, device)
17191735
self._canvas = canvas
17201736
self._format = format
17211737
self._usage = usage
1738+
self._compositing_alpha_mode = compositing_alpha_mode
17221739

17231740
# IDL: GPUTexture getCurrentTexture();
17241741
def get_current_texture(self):
@@ -1813,6 +1830,18 @@ def line_pos(self):
18131830
"""The position on the line in the shader source."""
18141831
raise NotImplementedError()
18151832

1833+
# IDL: readonly attribute unsigned long long offset;
1834+
@property
1835+
def offset(self):
1836+
"""Offset of ..."""
1837+
raise NotImplementedError()
1838+
1839+
# IDL: readonly attribute unsigned long long length;
1840+
@property
1841+
def length(self):
1842+
"""The length of the line?"""
1843+
raise NotImplementedError()
1844+
18161845

18171846
# FIXME: new class to implement
18181847
class GPUCompilationInfo:
@@ -1850,6 +1879,10 @@ def __init__(self, type, gpu_uncaptured_error_event_init_dict):
18501879
pass
18511880

18521881

1882+
class GPUExternalTexture(GPUObjectBase):
1883+
"""Ignore this - specific to browsers."""
1884+
1885+
18531886
# %%%%% Post processing
18541887

18551888
apidiff.remove_hidden_methods(globals())

wgpu/enums.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@ def __repr__(self):
2525
return f"<{self.__class__.__name__} {self._name}: {options}>"
2626

2727

28-
# There are 29 enums
28+
# There are 31 enums
29+
30+
PredefinedColorSpace = Enum(
31+
"PredefinedColorSpace",
32+
srgb="srgb",
33+
) #:
2934

3035
PowerPreference = Enum(
3136
"PowerPreference",
@@ -215,17 +220,17 @@ def __repr__(self):
215220
"BlendFactor",
216221
zero="zero",
217222
one="one",
218-
src_component="src-component",
219-
one_minus_src_component="one-minus-src-component",
223+
src="src",
224+
one_minus_src="one-minus-src",
220225
src_alpha="src-alpha",
221226
one_minus_src_alpha="one-minus-src-alpha",
222-
dst_component="dst-component",
223-
one_minus_dst_component="one-minus-dst-component",
227+
dst="dst",
228+
one_minus_dst="one-minus-dst",
224229
dst_alpha="dst-alpha",
225230
one_minus_dst_alpha="one-minus-dst-alpha",
226231
src_alpha_saturated="src-alpha-saturated",
227-
blendcolor_component="blendcolor-component",
228-
one_minus_blendcolor_component="one-minus-blendcolor-component",
232+
constant="constant",
233+
one_minus_constant="one-minus-constant",
229234
) #:
230235

231236
BlendOperation = Enum(
@@ -322,6 +327,12 @@ def __repr__(self):
322327
compute_shader_invocations="compute-shader-invocations",
323328
) #:
324329

330+
CanvasCompositingAlphaMode = Enum(
331+
"CanvasCompositingAlphaMode",
332+
opaque="opaque",
333+
premultiplied="premultiplied",
334+
) #:
335+
325336
DeviceLostReason = Enum(
326337
"DeviceLostReason",
327338
destroyed="destroyed",

wgpu/resources/codegen_report.md

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,42 @@
11
# Code generatation report
22
## Preparing
3-
* The webgpu.idl defines 33 classes with 81 functions
4-
* The webgpu.idl defines 5 flags, 29 enums, 50 structs
3+
* The webgpu.idl defines 34 classes with 82 functions
4+
* The webgpu.idl defines 5 flags, 31 enums, 52 structs
55
* The wgpu.h defines 106 functions
66
* The wgpu.h defines 5 flags, 36 enums, 58 structs
77
## Updating API
88
* Wrote 5 flags to flags.py
9-
* Wrote 29 enums to enums.py
10-
* Wrote 50 structs to structs.py
9+
* Wrote 31 enums to enums.py
10+
* Wrote 52 structs to structs.py
1111
### Patching API for base.py
1212
* Diffs for GPU: change request_adapter, change request_adapter_async
1313
* Diffs for GPUAdapter: add properties
14-
* Diffs for GPUDevice: add adapter, add create_buffer_with_data, hide pop_error_scope, hide push_error_scope
14+
* Diffs for GPUDevice: add adapter, add create_buffer_with_data, hide import_external_texture, hide pop_error_scope, hide push_error_scope
1515
* Diffs for GPUBuffer: add map_read, add map_write, add size, add usage, hide get_mapped_range, hide map_async, hide unmap
1616
* Diffs for GPUTexture: add dimension, add format, add mip_level_count, add sample_count, add size, add usage
1717
* Diffs for GPUTextureView: add size, add texture
1818
* Diffs for GPUComputePassEncoder: hide begin_pipeline_statistics_query, hide end_pipeline_statistics_query
1919
* Diffs for GPURenderPassEncoder: hide begin_pipeline_statistics_query, hide end_pipeline_statistics_query
20-
* Diffs for GPUQueue: add read_buffer, add read_texture
21-
* Validated 33 classes, 108 methods, 31 properties
20+
* Diffs for GPUQueue: add read_buffer, add read_texture, hide copy_external_image_to_texture
21+
* Validated 34 classes, 109 methods, 33 properties
2222
### Patching API for backends/rs.py
2323
* Diffs for GPUAdapter: add request_device_tracing
24-
* Validated 33 classes, 96 methods, 0 properties
24+
* Validated 34 classes, 95 methods, 0 properties
2525
## Validating rs.py
26+
* Enum PredefinedColorSpace missing in wgpu.h
2627
* Enum PowerPreference missing in wgpu.h
2728
* Enum FeatureName missing in wgpu.h
2829
* Enum field TextureFormat.depth16unorm missing in wgpu.h
2930
* Enum field TextureFormat.depth24unorm-stencil8 missing in wgpu.h
3031
* Enum field TextureFormat.depth32float-stencil8 missing in wgpu.h
3132
* Enum CompilationMessageType missing in wgpu.h
32-
* Enum field BlendFactor.src-component missing in wgpu.h
33-
* Enum field BlendFactor.one-minus-src-component missing in wgpu.h
34-
* Enum field BlendFactor.dst-component missing in wgpu.h
35-
* Enum field BlendFactor.one-minus-dst-component missing in wgpu.h
36-
* Enum field BlendFactor.blendcolor-component missing in wgpu.h
37-
* Enum field BlendFactor.one-minus-blendcolor-component missing in wgpu.h
33+
* Enum field BlendFactor.src missing in wgpu.h
34+
* Enum field BlendFactor.one-minus-src missing in wgpu.h
35+
* Enum field BlendFactor.dst missing in wgpu.h
36+
* Enum field BlendFactor.one-minus-dst missing in wgpu.h
37+
* Enum field BlendFactor.constant missing in wgpu.h
38+
* Enum field BlendFactor.one-minus-constant missing in wgpu.h
39+
* Enum CanvasCompositingAlphaMode missing in wgpu.h
3840
* Enum DeviceLostReason missing in wgpu.h
3941
* Wrote 169 enum mappings and 45 struct-field mappings to rs_mappings.py
4042
* Validated 65 C function calls

0 commit comments

Comments
 (0)