Skip to main content

Next generation GPU API for Python

Project description

CI Documentation Status PyPI version

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 replace pip with pipenv 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.
  • 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 the wgpu.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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

wgpu-0.7.6.tar.gz (89.9 kB view details)

Uploaded Source

Built Distributions

wgpu-0.7.6-py3-none-win_amd64.whl (5.1 MB view details)

Uploaded Python 3 Windows x86-64

wgpu-0.7.6-py3-none-win32.whl (4.7 MB view details)

Uploaded Python 3 Windows x86

wgpu-0.7.6-py3-none-manylinux_2_24_x86_64.whl (27.5 MB view details)

Uploaded Python 3 manylinux: glibc 2.24+ x86-64

wgpu-0.7.6-py3-none-manylinux_2_24_i686.whl (27.8 MB view details)

Uploaded Python 3 manylinux: glibc 2.24+ i686

wgpu-0.7.6-py3-none-macosx_11_0_arm64.whl (5.0 MB view details)

Uploaded Python 3 macOS 11.0+ ARM64

wgpu-0.7.6-py3-none-macosx_10_9_x86_64.whl (5.1 MB view details)

Uploaded Python 3 macOS 10.9+ x86-64

File details

Details for the file wgpu-0.7.6.tar.gz.

File metadata

  • Download URL: wgpu-0.7.6.tar.gz
  • Upload date:
  • Size: 89.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.12

File hashes

Hashes for wgpu-0.7.6.tar.gz
Algorithm Hash digest
SHA256 c096e13b0578cd82cf66931b21f302d3bcb0b9551dea99eb8b396c086a6e3565
MD5 6ea033864cc01198f0792cdda275e1cd
BLAKE2b-256 108c191df45eeb44dfb9696f4da8b004b44c2a5b9072d3aa11d5bca40597ba6f

See more details on using hashes here.

File details

Details for the file wgpu-0.7.6-py3-none-win_amd64.whl.

File metadata

  • Download URL: wgpu-0.7.6-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.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.12

File hashes

Hashes for wgpu-0.7.6-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 9a2b8685ccb2a382d79699d8b36f93fcb40fe75d8e425c4f73beec00110310b9
MD5 9187db55a370300e2724d53010272099
BLAKE2b-256 b50321dc73013d64b45e6cc0a7e592f51b2119370ae46cad3e72e011057f6ff3

See more details on using hashes here.

File details

Details for the file wgpu-0.7.6-py3-none-win32.whl.

File metadata

  • Download URL: wgpu-0.7.6-py3-none-win32.whl
  • Upload date:
  • Size: 4.7 MB
  • Tags: Python 3, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.12

File hashes

Hashes for wgpu-0.7.6-py3-none-win32.whl
Algorithm Hash digest
SHA256 3ab9da2e94d325b5a4d55affe27bad6ee13423fdf1bb62bce7e5901ec24208b3
MD5 0aab91bf58f3e7d59f18522e35bb4a4a
BLAKE2b-256 218c6e59b89270400ad45fbac1d1766b25c797b5a4fa8df914453392273d8fc2

See more details on using hashes here.

File details

Details for the file wgpu-0.7.6-py3-none-manylinux_2_24_x86_64.whl.

File metadata

  • Download URL: wgpu-0.7.6-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.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.12

File hashes

Hashes for wgpu-0.7.6-py3-none-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 b144e5ccab19d9eeb0ff277a645817d09b118a3b6e5f0ef39d4640f57d47043d
MD5 e7f835fe901ea6f09db399c38333a08a
BLAKE2b-256 ace29a807d099e9e79b1f08acd0c34958774eef2cc62ea2b141df8c7d84347fd

See more details on using hashes here.

File details

Details for the file wgpu-0.7.6-py3-none-manylinux_2_24_i686.whl.

File metadata

  • Download URL: wgpu-0.7.6-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.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.12

File hashes

Hashes for wgpu-0.7.6-py3-none-manylinux_2_24_i686.whl
Algorithm Hash digest
SHA256 b98d4f97d1f861a39986e0c4ce74951ce89577b477b218355d5ca386904aba14
MD5 64b17aa0782c285a420f4b52bf5ab156
BLAKE2b-256 aa8b78a3f1c0c549d077a9b2182259f62ff9de2162459d6acb776b434c432c51

See more details on using hashes here.

File details

Details for the file wgpu-0.7.6-py3-none-macosx_11_0_arm64.whl.

File metadata

  • Download URL: wgpu-0.7.6-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.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.12

File hashes

Hashes for wgpu-0.7.6-py3-none-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d9569faa0338aead9413ef6f52d424f5510f693cc974dfcdc5cf69b82a1b8d61
MD5 7c19a6b33e730d116e18917143c188e5
BLAKE2b-256 41e7e1873cfcec17a6e708fa1e3be3d97b3e421ef8aa7437eb8efc6c3f6c6ff3

See more details on using hashes here.

File details

Details for the file wgpu-0.7.6-py3-none-macosx_10_9_x86_64.whl.

File metadata

  • Download URL: wgpu-0.7.6-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.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.12

File hashes

Hashes for wgpu-0.7.6-py3-none-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 597b178f67082b5219850572af00b54441cb084cf5930a379702eff10ee7b9ba
MD5 1f6e8a70014eb8e53daf099966b8240a
BLAKE2b-256 af81462c901354b2ecd32a42bb8255b2df9d51ef9f1d401feda898771b5c9456

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page