Cette page est affichée en anglais. Une traduction française est en cours.
ConfigsMay 14, 2026·3 min de lecture

wgpu — Safe and Portable GPU Abstraction in Rust

wgpu is a cross-platform Rust library implementing the WebGPU API standard. It provides a safe, portable interface to GPU hardware that runs natively on Vulkan, Metal, DX12, and OpenGL, as well as in the browser via WebAssembly and WebGPU.

Prêt pour agents

Cet actif peut être lu et installé directement par les agents

TokRepo expose une commande CLI universelle, un contrat d'installation, le metadata JSON, un plan selon l'adaptateur et le contenu raw pour aider les agents à juger l'adaptation, le risque et les prochaines actions.

Native · 98/100Policy : autoriser
Surface agent
Tout agent MCP/CLI
Type
Skill
Installation
Single
Confiance
Confiance : Established
Point d'entrée
wgpu Overview
Commande CLI universelle
npx tokrepo install 848785d1-4f6e-11f1-9bc6-00163e2b0d79

Introduction

wgpu implements the WebGPU specification in Rust, providing a modern GPU programming interface that works across all major platforms. It abstracts over Vulkan, Metal, DirectX 12, and OpenGL ES backends while maintaining the safety guarantees that Rust developers expect. The same API runs natively on desktop and mobile or compiles to WebAssembly for browser-based GPU workloads.

What wgpu Does

  • Provides a safe Rust API for GPU compute and rendering based on the WebGPU standard
  • Targets Vulkan, Metal, DX12, OpenGL ES, and browser WebGPU as backends
  • Manages GPU resources (buffers, textures, pipelines, bind groups) with Rust ownership semantics
  • Supports compute shaders for GPGPU workloads alongside traditional graphics rendering
  • Compiles to WebAssembly for running GPU code in browsers that support WebGPU

Architecture Overview

wgpu is structured as a frontend API layer (wgpu crate) over a hardware abstraction layer (wgpu-hal) that maps to native graphics APIs. The wgpu-core crate implements resource tracking, validation, and command recording. Shader code is written in WGSL or cross-compiled from GLSL/SPIR-V via the naga shader translator, which is bundled with the project. The architecture mirrors the WebGPU spec closely, making it straightforward to port code between native Rust and browser WebGPU.

Self-Hosting & Configuration

  • Add wgpu as a dependency in Cargo.toml with desired feature flags for backends
  • Request an adapter with instance.request_adapter() to select a GPU
  • Create a device and queue from the adapter for submitting GPU work
  • Write shaders in WGSL (recommended) or use naga to convert from GLSL or SPIR-V
  • For WebAssembly builds, compile with wasm-pack and use the browser's native WebGPU support

Key Features

  • Memory-safe GPU programming with Rust's borrow checker preventing use-after-free and data races
  • Single API targeting six backends (Vulkan, Metal, DX12, OpenGL ES, WebGPU, and DX11 legacy)
  • Bundled naga shader compiler for WGSL, GLSL, SPIR-V, and HLSL translation
  • Powers the Firefox WebGPU implementation, ensuring spec conformance
  • Active ecosystem with frameworks like Bevy and libraries like egui building on top of wgpu

Comparison with Similar Tools

  • Vulkano — Rust-native Vulkan bindings; lower-level with direct Vulkan access but no cross-backend portability
  • ash — thin Vulkan FFI wrapper for Rust; maximum control but requires manual synchronization and validation
  • glow — OpenGL bindings for Rust; simpler but limited to OpenGL's older programming model
  • Metal-rs — Rust bindings for Apple Metal; macOS/iOS only
  • Dawn — Google's C++ WebGPU implementation; used in Chrome, similar API surface but in C++ instead of Rust

FAQ

Q: Can wgpu run in the browser? A: Yes. wgpu compiles to WebAssembly and uses the browser's native WebGPU API. This requires a browser with WebGPU support (Chrome 113+, Firefox Nightly, Safari Technology Preview).

Q: What shader language does wgpu use? A: WGSL is the primary shader language, matching the WebGPU specification. You can also use GLSL or SPIR-V, which the bundled naga compiler translates at build time or runtime.

Q: Is wgpu production-ready? A: Yes. wgpu is used in production by Firefox for its WebGPU implementation, by the Bevy game engine, and by numerous compute and visualization projects. The API follows semantic versioning.

Q: How does performance compare to raw Vulkan? A: wgpu adds a thin abstraction layer with validation overhead in debug mode. In release builds, the overhead is minimal. For most applications, the portability and safety benefits outweigh the small performance cost.

Sources

Fil de discussion

Connectez-vous pour rejoindre la discussion.
Aucun commentaire pour l'instant. Soyez le premier à partager votre avis.

Actifs similaires