# 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. ## Install Save in your project root: # wgpu — Safe and Portable GPU Abstraction in Rust ## Quick Use ```bash cargo add wgpu # Minimal example: request adapter, create device, submit GPU commands # See https://github.com/gfx-rs/wgpu/tree/trunk/examples for runnable samples ``` ## 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 - https://github.com/gfx-rs/wgpu - https://wgpu.rs/ --- Source: https://tokrepo.com/en/workflows/asset-848785d1 Author: AI Open Source