Learning WebGPU Part 1 Introduction
Introduction
I have been teaching the Unit “Computing for Graphics and Animsion” for a number of year, and this in its current form is an extension and modification of a Unit going back to the days of immediate mode OpenGL.
It currently teaches elements of Modern OpenGL, GLSL in the style of “approaching zero driver overhead” and using a number of modern C++ (17) techniques.
When we revalidated the course last year we decided to move to using Python as our core programming language as we are using it across the whole of the NCCA in a unit called Procedural Content Creation this means I need to update the course to use Python and as part of this I decided to move to a more modern rendering API.
Considerations
There are a number of considerations when designing a new curriculum and deciding upon the technology stack to use.
Ideally I would like it to be fully open source, and to run on the 3 core operating systems (Windows, MacOS and Linux). I would also like it to be as simple as possible to install and use, and to have a low barrier to entry.
In our labs we predominantly use Linux (RHEL 8 but moving to REHL 9 soon), but many of our students have windows laptops and I do most of my development on a Mac.
Other considerations are adding extra “value” elements to the unit to allow students to learn skills that a required or useful in industry such as
- PySide / PyQt
- Git
- Test Driven Development
Some History
I’m old enough to remember the days when there were many graphics API’s (I started my graphics journey with the White Book and C in the 90’s) then everyone decided to merge into OpenGL and for many years this was the way to go, especially for teaching.
I have managed to update my code base and my graphics library NGL up to date for many years and go from OpenGL 2.1 to 4.1, however once Apple decided to deprecate OpenGL and the rise of Vulkan and DirectX 12 I decided it was time to move on and use something more modern.
So may Graphics API’s
It now seems most platforms have a specific API for their hardware, Apple has Metal, Microsoft has DirectX 12 and the Khronos group has Vulkan. Vulkan would work across all platforms but installation and setup is a bit of a pain, and it is a very low level API not really suitable for teaching 2nd year students.
An aside to WebGL
I have use WebGL in the past via the excellent three.js library, and this is a great way to get 3D graphics in the browser and some of my lecture slides, however I used WebASM and Emscripten to compile my C++ code to run in the browser. I really dislike JavaScript and the whole web development stack so decided agains that.
WebGPU
Over the last few years I have been following the development of WebGPU, and it seems to be a good fit for my needs. It is a modern API that is designed to be cross platform and is a high level API that is similar to Vulkan but with a lot of the complexity removed.
It also gives a relatively transferable skill of understanding the modern GPU pipeline whilst not being quite as complex as Vulkan.
Python and WebGPU
I have decided to use wgpu-py as my core library, this is a Python wrapper around the WebGPU API and also have a number of extra features such as a simple windowing system (which I’m not going to use).
Risks
There are a number of risks with this approach, the main one is that WebGPU is still in development and the specification is still in flux.
However as cited on the wgpu-py project page
- Until WebGPU settles as a standard, its specification may change, and with that our API will probably too. Check the changelog when you upgrade!
- Coverage of the WebGPU spec is complete enough to build e.g. pygfx.
- Test coverage of the API is close to 100%.
- Support for Windows, Linux (x86 and aarch64), and MacOS (Intel and M1).
nccapy
I already have a teaching library for python called nccapy which is a collection of utility functions and classes that I have developed over the years and use in other teaching already. This also has a lot of the NGL functionality ported to Python. The plan is to use some of this code in the WebGPU demos and to extend it as needed. This will also allow a fallback to OpenGL if WebGPU is not available.
Initial Commit
TODO :-
- Create a new repository for the WebGPU demos here
- Understand the webgpu-py library and demos
- Create a framework for the new demos (PySide / PyQt)
- Port elements of NGL to Python (stand alone then into nccapy)
- Port most of the existing OpenGL demos to WebGPU
- Write new Lecture Notes
afterword
I have actually done quite a bit of this work already, but I thought it would be useful to document my progress and to share my thoughts on the process. I will be updating this blog as I progress through the development of the new unit, and the next few posts will be my current progress, one I have written this all up I will attempt to post as I write new things.