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 the Rust wgpu lib and exposing it with a Pythonic API similar to WebGPU.
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. Based on wgpu-native.
To get an idea of what this API looks like have a look at triangle.py and the other examples.
Status
This is experimental, work in progress, you probably don't want to use this just yet!
- We have a few working examples!
- Support for Windows and Linux. Support for MacOS is underway.
- We have not fully implemented the API yet.
- The API may change. We're still figuring out what works best.
- The API may change more. Until WebGPU settles as a standard, its specification may change, and with that our API will probably too.
Installation
pip install wgpu
pip install python-shader # optional - our examples use this to define shaders
The library ships with Rust binaries for Windows, MacOS and Linux. If you want to use
a custom build instead, you can set the environment variable WGPU_LIB_PATH
.
Platform requirements
Under the hood, wgpu
runs on Vulkan or Metal, and eventually also DX12 or OpenGL.
On Windows 10, things should just work. On older Windows versions you may need to install the Vulkan drivers (or wait for the DX12 backend to become more 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
.
Note that on Linux, the tk
canvas does not work. 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. At the moment, we've not implemented drawing to a window yet (see #29).
Usage
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
GUI integration
To render to the screen you can use a variety of GUI toolkits:
# Tk is included with Python (though this canvas only seems to work on Windows)
from wgpu.gui.tk import WgpuCanvas
# GLFW is a great lightweight windowing toolkit. Install with `pip install glfw`
from wgpu.gui.glfw import WgpuCanvas
# Visualizations can be embedded as a widget in a Qt application.
# Import PySide2, PyQt5, PySide or PyQt4 before running the line below.
# The code will detect and use the library that is imported.
from wgpu.gui.qt 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.
Web support
We are considering future support for compiling (Python) visualizations to the web via PScript and Flexx. We try to keep that option open as long as it does not get in the way too much. No promises.
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
python setup.py develop
, this will also install our only runtime dependencycffi
- 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.
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.
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
Hashes for wgpu-0.1.4-py3-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9fd2811cc2fef13867121aa353525c6ea4b41a7c30e45ef8df56906b4c93ec35 |
|
MD5 | ca14b1594bc9938b554f3459d91c459e |
|
BLAKE2b-256 | 40576260a538e3bc66d908f00854b734259376c67f3d72e0f4f1675fcadafd3a |
Hashes for wgpu-0.1.4-py3-none-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0d681efe44e0f063bffa04c0907734ddf427e5c3d17e5cf9c2fce9c9af2877d7 |
|
MD5 | d0e7f47317ce0d6c8c519b518c76203a |
|
BLAKE2b-256 | dcbfb89aeb728a8b26d2d7cdc9766b80b0bcc4e6eaba578f0120a80df7d48441 |
Hashes for wgpu-0.1.4-py3-none-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6790b6186ea4cc0055e9ff4ca33f866042dae62bb8647389baee0f76e09100ed |
|
MD5 | 878b7a3115c8c1fcd4ef4db516c9b793 |
|
BLAKE2b-256 | 8a3f39bf7ff4d1871103cf910cc7a37641bd07ebce1292e10c32ad122fefd7cf |
Hashes for wgpu-0.1.4-py3-none-macosx_10_13_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f988cb0fcbafc3566d46f44c62ad1b706d4c378c98e004f4498d293c2e237f65 |
|
MD5 | f9672b98c4397263e10c2a438048da50 |
|
BLAKE2b-256 | 8a7947111396983c1399776588b652c66a4675244c19e61365f84c6f6aa198b9 |