Utils
The wgpu library provides a few utilities. Note that most functions below need to be explicitly imported.
Logger
Errors, warnings, and info messages (including messages generated by
wgpu-native) are logged using Python’s default logging mechanics. The
wgpu logger instance is in wgpu.logger
, but can also be obtained
via:
import logging
logger = logging.getLogger("wgpu")
Diagnostics
To print a full diagnostic report:
wgpu.diagnostics.print_report()
To inspect (for example) the total buffer usage:
>>> counts = wgpu.diagnostics.object_counts.get_dict()
>>> print(counts["Buffer"])
{'count': 3, 'resource_mem': 784}
- class wgpu._diagnostics.DiagnosticsRoot
Root object to access wgpu diagnostics (i.e.
wgpu.diagnostics
).Per-topic diagnostics can be accessed as attributes on this object. These include
system
,wgpu_native_info
,versions
,object_counts
,wgpu_native_counts
.- get_dict()
Get a dict that represents the full diagnostics info.
The keys are the diagnostic topics, and the values are dicts of dicts. See e.g.
wgpu.diagnostics.counts.get_dict()
for a topic-specific dict.
- get_report()
Get the full textual diagnostic report (as a str).
- print_report()
Convenience method to print the full diagnostics report.
- class wgpu.DiagnosticsBase(name)
Object that represents diagnostics on a specific topic.
This is a base class that must be subclassed to provide diagnostics on a certain topic. Typically only
get_dict()
needs to be implemented. Instantiating the class registers it with the root diagnostics object.- get_dict()
Get the diagnostics for this topic, in the form of a Python dict.
Subclasses must implement this method. The dict can be a simple map of keys to values (str, int, float):
foo: 1 bar: 2
If the values are dicts, the data has a table-like layout, with the keys representing the table header:
count mem Adapter: 1 264 Buffer: 4 704
Subdicts are also supported, which results in multi-row entries. In the report, the keys of the subdicts have colons behind them:
count mem backend o v e el_size Adapter: 1 264 vulkan: 1 0 0 264 d3d12: 1 0 0 220 Buffer: 4 704 vulkan: 4 0 0 176 d3d12: 0 0 0 154
- get_report()
Get the textual diagnostics report for this topic.
- get_subscript()
Get informative text that helps interpret the report.
Subclasses can implement this method. The text will show below the table in the report.
- print_report()
Print the diagnostics report for this topic.
Base class for flags and enums
- class wgpu.utils.BaseEnum
Base class for flags and enums.
Looks like Python’s builtin Enum class, but is simpler; fields are simply ints or strings.
Get default device
- wgpu.utils.get_default_device()
Get a wgpu device object. If this succeeds, it’s likely that the WGPU lib is usable on this system. If not, this call will probably exit (Rust panic). When called multiple times, returns the same global device object (useful for e.g. unit tests).
Compute with buffers
from wgpu.utils.compute import compute_with_buffers
- wgpu.utils.compute.compute_with_buffers(input_arrays, output_arrays, shader, n=None)
Apply the given compute shader to the given input_arrays and return output arrays. Both input and output arrays are represented on the GPU using storage buffer objects.
- Parameters:
input_arrays (dict) – A dict mapping int bindings to arrays. The array can be anything that supports the buffer protocol, including bytes, memoryviews, ctypes arrays and numpy arrays. The type and shape of the array does not need to match the type with which the shader will interpret the buffer data (though it probably makes your code easier to follow).
output_arrays (dict) – A dict mapping int bindings to output shapes. If the value is int, it represents the size (in bytes) of the buffer. If the value is a tuple, its last element specifies the format (see below), and the preceding elements specify the shape. These are used to
cast()
the memoryview object before it is returned. If the value is a ctypes array type, the result will be cast to that instead of a memoryview. Note that any buffer that is NOT in the output arrays dict will be considered readonly in the shader.shader (str or bytes) – The shader as a string of WGSL code or SpirV bytes.
n (int, tuple, optional) – The dispatch counts. Can be an int or a 3-tuple of ints to specify (x, y, z). If not given or None, the length of the first output array type is used.
- Returns:
A dict mapping int bindings to memoryviews.
- Return type:
output (dict)
The format characters to cast a
memoryview
are hard to remember, so here’s a refresher:“b” and “B” are signed and unsigned 8-bit ints.
“h” and “H” are signed and unsigned 16-bit ints.
“i” and “I” are signed and unsigned 32-bit ints.
“e” and “f” are 16-bit and 32-bit floats.