WebGPU in PyNGL
Alongside the OpenGL stack, PyNGL ships a parallel renderer built on
WebGPU via the
wgpu Python package. WebGPU is the
modern successor to OpenGL: instead of a hidden global state machine you
describe your rendering up front as explicit pipeline objects (shaders,
vertex layouts, and render state bundled together), then record draw calls
into a command encoder each frame. Shaders are written in
WGSL rather than GLSL.
Everything lives in the ncca.ngl.webgpu package, which exports four
things:
WebGPUWidget— a PySide6QWidgetbase class that hosts the renderer. It renders offscreen withwgpuinto a colour buffer that the widget blits to the screen withQPainter, so there is no OpenGL context, canvas, or swapchain to manage. Subclass it and implementpaintWebGPU()andresizeWebGPU(w, h).PipelineFactory— a registry that creates ready-made render pipelines (points, lines, triangles, triangle strips, and instanced geometry), mirroring the OpenGLVAOFactorypattern. You can register your own pipeline classes without touching library code.PipelineType— the enum of built-in pipeline types the factory knows about.NGLToWebGPU— helpers that translate NGL type names into WebGPU vertex formats and strides (e.g."vec3"→"float32x3").
All the maths and geometry classes are shared with the OpenGL stack —
Vec3, Mat4, look_at, perspective, and PrimData work unchanged.
One thing to remember
WebGPU's clip-space depth runs from 0 to 1, whereas OpenGL's runs from
−1 to 1. Always build projection matrices with
perspective(..., PerspMode.WebGPU) — with the default (OpenGL) mode
your geometry will clip or z-fight.
The pages in this section
- Getting Started with WebGPU — the
WebGPUWidgetlifecycle and a minimal working application. - The Built-in Pipelines — a tour of all
fourteen
PipelineTypes and theset_data/update_uniforms/rendercontract, following the bundled demo app. - Custom Pipelines — writing your own WGSL
shaders with
CustomShaderPipeline, or subclassingBaseWebGPUPipelineand registering it with the factory. - WebGPU API Reference — full auto-generated documentation for every class in the package.
Try it right now
The package ships a demo application that cycles through every built-in pipeline. If you have the library installed you can run it immediately:
uv run python -m ncca.ngl.webgpu
Use the Left / Right arrow keys to switch pipelines manually,
Space to pause the animation, A to toggle automatic switching, and
Escape to quit. Its source
(src/ncca/ngl/webgpu/__main__.py)
is the reference example for the built-in pipelines and is walked through
in The Built-in Pipelines.
For larger complete programs — lit meshes, textures, shadows, compute — see the WebGPU examples in PyNGLDemos.