Next generation GPU API for Python
Project description
wgpu-py
Next generation GPU API for Python
Introduction
In short, this is a Python lib wrapping wgpu-native and exposing it with a Pythonic API similar to the WebGPU spec.
The OpenGL API is old and showing it's cracks. New API's like Vulkan,
Metal and DX12 provide a modern way to control the GPU, but these API's
are too low-level for general use. The WebGPU API follows the same concepts, but with
a simpler (higher level) spelling. The Python wgpu
library brings the
WebGPU API to Python.
To get an idea of what this API looks like have a look at triangle.py and the other examples.
Status
The wgpu-API has not settled yet, use with care!
- Coverage of the WebGPU spec is nearly complete.
- Test coverage of the API is 100%.
- Support for Windows, Linux, and MacOS (Intel and M1).
- Until WebGPU settles as a standard, its specification may change, and with that our API will probably too. Check the changelog when you upgrade!
Installation
pip install wgpu
The wheels include the prebuilt binaries. If you want to use
a custom build instead, you can set the environment variable WGPU_LIB_PATH
.
You probably also want to install glwf
(for desktop) and/or jupyter_rfb
(for Jupyter).
Platform requirements
Under the hood, wgpu
runs on Vulkan, Metal, or DX12. The wgpu-backend is selected automatically, but can be overridden by setting the WGPU_BACKEND_TYPE
environment variable to "Vulkan", "Metal", "D3D12", "D3D11", or "OpenGL".
On Windows 10+, things should just work. On older Windows versions you may need to install the Vulkan drivers. You may want to force "Vulkan" while "D3D12" is less mature.
On Linux, it's advisable to install the proprietary drivers of your GPU
(if you have a dedicated GPU). You may need to apt install mesa-vulkan-drivers
.
Wayland currently only works with the GLFW canvas (and is unstable).
On MacOS you need at least 10.13 (High Sierra) to have Vulkan support.
Usage
Also see the online documentation.
The full API is accessable via the main namespace:
import wgpu
But to use it, you need to select a backend first. You do this by importing it. There is currently only one backend:
import wgpu.backend.rs
To render to the screen you can use a variety of GUI toolkits:
# The auto backend selects either the glfw, qt or jupyter backend
from wgpu.gui.auto import WgpuCanvas, run, call_later
# Visualizations can be embedded as a widget in a Qt application.
# Import PySide6, PyQt6, PySide2 or PyQt5 before running the line below.
# The code will detect and use the library that is imported.
from wgpu.gui.qt import WgpuCanvas
# Visualizations can be embedded as a widget in a wx application.
from wgpu.gui.wx import WgpuCanvas
Some functions in the original wgpu-native
API are async. In the Python API,
the default functions are all sync (blocking), making things easy for general use.
Async versions of these functions are available, so wgpu can also work
well with Asyncio or Trio.
License
This code is distributed under the 2-clause BSD license.
Developers
- Clone the repo.
- Install devtools using
pip install -r dev-requirements.txt
(you can replacepip
withpipenv
to install to a virtualenv). - Install wgpu-py in editable mode by running
pip install -e .
, this will also install runtime dependencies as needed. - Run
python download-wgpu-native.py
to download the upstream wgpu-native binaries.- Or alternatively point the
WGPU_LIB_PATH
environment variable to a custom build.
- Or alternatively point the
- Use
black .
to apply autoformatting. - Use
flake8 .
to check for flake errors. - Use
pytest .
to run the tests. - Use
pip wheel --no-deps .
to build a wheel.
Changing the upstream wgpu-native version
- Use the optional arguments to
python download-wgpu-native.py --help
to download a different version of the upstream wgpu-native binaries. - The file
wgpu/resources/wgpu_native-version
will be updated by the script to track which version we depend upon.
Example screenshot verification
Include the comment # test_example = true
in an example to have pytest run it as part of the test suite.
To support testing an example, ensure the following requirements are met:
- The
WgpuCanvas
class is imported from thewgpu.gui.auto
module. - The
canvas
instance is exposed as a global in the module.
By default the test will simply verify that the example can be executed without an error, and that an image can be rendered using the canvas.
If a screenshot for the example is available in the examples/screenshots
folder, the test will additionally
verify that the rendered image matches the screenshot.
You can (re)generate the reference screenshot like so: pytest -k test_examples --regenerate-screenshots
If CI fails on screenshot verification, the build will regenerate screenshots and make them available as build artifacts so you can download and inspect the differences, and debug locally.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
File details
Details for the file wgpu-0.7.3.tar.gz
.
File metadata
- Download URL: wgpu-0.7.3.tar.gz
- Upload date:
- Size: 88.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e097c318e5ffc307c80a557a5adab562a12041c2e36bd662f645851e0a123926 |
|
MD5 | eb86ebb4e243477cb59045aa54e40f7a |
|
BLAKE2b-256 | 57e7d48ef0c3d2f3e5777b38b825e96fc1e364179cb20bf56c65c369160e8b20 |
File details
Details for the file wgpu-0.7.3-py3-none-win_amd64.whl
.
File metadata
- Download URL: wgpu-0.7.3-py3-none-win_amd64.whl
- Upload date:
- Size: 5.1 MB
- Tags: Python 3, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 65b4ab536a7d1e744a10114bc7ff21b7975bc9685d456140f0a5b6077719e0ca |
|
MD5 | a7c8da892a0e7753a9d490a20f667d72 |
|
BLAKE2b-256 | 307e274e2c70e998c8edfb494a326915eafddf758148ae50c2bb47a3464f949e |
File details
Details for the file wgpu-0.7.3-py3-none-win32.whl
.
File metadata
- Download URL: wgpu-0.7.3-py3-none-win32.whl
- Upload date:
- Size: 4.7 MB
- Tags: Python 3, Windows x86
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 56eff85f2fa6f1031cde91ba5b8604d23acc89f2151354bf717e055ffc94b2a9 |
|
MD5 | 2fef87db64f973b6efc1ad31f0acfc67 |
|
BLAKE2b-256 | 158ecb6c60a92c397d38baab2b737e1ae3f07e91940e23d05e66d0d5a6b8e313 |
File details
Details for the file wgpu-0.7.3-py3-none-manylinux_2_24_x86_64.whl
.
File metadata
- Download URL: wgpu-0.7.3-py3-none-manylinux_2_24_x86_64.whl
- Upload date:
- Size: 27.5 MB
- Tags: Python 3, manylinux: glibc 2.24+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a188e719b48d052ee7a1f3108858cb0b3b77909f2ba6b4d0594558eb8d915838 |
|
MD5 | ac94db1ff56386749880760973e784d4 |
|
BLAKE2b-256 | a9b05c2a58b9261b254868e2251a9f4800876c760de07f2edad35e5fa23daed5 |
File details
Details for the file wgpu-0.7.3-py3-none-manylinux_2_24_i686.whl
.
File metadata
- Download URL: wgpu-0.7.3-py3-none-manylinux_2_24_i686.whl
- Upload date:
- Size: 27.8 MB
- Tags: Python 3, manylinux: glibc 2.24+ i686
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ae4fc9024e52e98e988578e89a8596807f7334036fb6bc174eb9b6f4deed4c54 |
|
MD5 | 11ce151d501b33f13e1195523f603e01 |
|
BLAKE2b-256 | a385059c6d1c16d9dbf9225b04db98b275003ba65717e29a66a4281ff85c2b9d |
File details
Details for the file wgpu-0.7.3-py3-none-macosx_11_0_arm64.whl
.
File metadata
- Download URL: wgpu-0.7.3-py3-none-macosx_11_0_arm64.whl
- Upload date:
- Size: 5.0 MB
- Tags: Python 3, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 26b6af33fafd0ac0329b376cd9e1b8e9d27320d746859065a2c13b89f587a5d8 |
|
MD5 | 926e0d478ff134694d15c8721a31fa10 |
|
BLAKE2b-256 | 9021820d5e07612a37527b11c53c6fbd904d12348270c70aeb1d8a8de3ef0726 |
File details
Details for the file wgpu-0.7.3-py3-none-macosx_10_9_x86_64.whl
.
File metadata
- Download URL: wgpu-0.7.3-py3-none-macosx_10_9_x86_64.whl
- Upload date:
- Size: 5.1 MB
- Tags: Python 3, macOS 10.9+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 348085f1e20b0fd1e780b9a386d16032ed5851e9b9020016d7ff40609c81938c |
|
MD5 | 4a53ff536e8a9d1b91090b15d10a9913 |
|
BLAKE2b-256 | 2b335c0caa20318375544816894a1285972a5af53837aa5c1b598b7f2f044423 |