Utilities

The wgpu library provides a few utilities. Note that the, except for help(), the functions below need to be explictly imported.

wgpu.help(*searches, dev=False)

Print constants, enums, structs, and functions that contain the given searches. If dev is True, will also print info from the definitions in .idl and .h, which can be useful during debugging and dev.

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).

wgpu.utils.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 ctypes arrays. The type of the array does not need to match the type with which the shader will interpret the buffer data (though it probably makes the code easier to follow).
  • output_arrays (dict) – A dict mapping int bindings to ctypes array types. This function uses the given type to determine the buffer size (in bytes), and returns arrays of matching type. Example: ctypes.c_float * 20. If you don’t care about the type (e.g. because you re-cast it later), you can just specify the buffer size using ctypes.c_ubyte * nbytes.
  • shader (bytes, shader-object) – The SpirV representing the shader, as raw bytes or an object implementing to_spirv() (e.g. a python_shader SpirV module).
  • 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 ctypes arrays. The keys match those of output_arrays, and the arrays are instances of the corresponding array types.

Return type:

output (dict)