wgpu/
lib.rs

1//! `wgpu` is a cross-platform, safe, pure-Rust graphics API. It runs natively on
2//! Vulkan, Metal, D3D12, and OpenGL; and on top of WebGL2 and WebGPU on wasm.
3//!
4//! The API is based on the [WebGPU standard][webgpu], but is a fully native Rust library.
5//! It serves as the core of the WebGPU integration in Firefox, Servo, and Deno.
6//!
7//! [webgpu]: https://gpuweb.github.io/gpuweb/
8//!
9//! ## Getting Started
10//!
11//! The main entry point to the API is the [`Instance`] type, from which you can create [`Adapter`], [`Device`], and [`Surface`].
12//!
13//! If you are new to `wgpu` and graphics programming, we recommend starting with [Learn Wgpu].
14//! <!-- Note, "Learn Wgpu" is using the capitalization style in their header, NOT our styling -->
15//!
16//! Additionally, [WebGPU Fundamentals] is a tutorial for WebGPU which is very similar to our API, minus differences between Rust and Javascript.
17//!
18//! We have a [wiki](https://github.com/gfx-rs/wgpu/wiki) which has information on useful architecture patterns, debugging tips, and more getting started information.
19//!
20//! There are examples for this version [available on GitHub](https://github.com/gfx-rs/wgpu/tree/v27/examples#readme).
21//!
22//! The API is refcounted, so all handles are cloneable, and if you create a resource which references another,
23//! it will automatically keep dependent resources alive.
24//!
25//! `wgpu` uses the coordinate systems of D3D and Metal. Depth ranges from [0, 1].
26//!
27//! | Render                | Texture                |
28//! | --------------------- | ---------------------- |
29//! | ![render_coordinates] | ![texture_coordinates] |
30//!
31//! `wgpu`'s MSRV is **1.88**.
32//!
33//! [Learn Wgpu]: https://sotrh.github.io/learn-wgpu/
34//! [WebGPU Fundamentals]: https://webgpufundamentals.org/
35//! [render_coordinates]: https://raw.githubusercontent.com/gfx-rs/wgpu/refs/heads/v27/docs/render_coordinates.png
36//! [texture_coordinates]: https://raw.githubusercontent.com/gfx-rs/wgpu/refs/heads/v27/docs/texture_coordinates.png
37//!
38//! ## Extension Specifications
39//!
40//! While the core of `wgpu` is based on the WebGPU standard, we also support extensions that allow for features that the standard does not have yet.
41//! For high-level documentation on how to use these extensions, see documentation on [`Features`] or the relevant specification:
42//!
43//! 🧪EXPERIMENTAL🧪 APIs are subject to change and may allow undefined behavior if used incorrectly.
44//!
45//! - 🧪EXPERIMENTAL🧪 [Ray Tracing](https://github.com/gfx-rs/wgpu/blob/v27/docs/api-specs/ray_tracing.md).
46//! - 🧪EXPERIMENTAL🧪 [Mesh Shading](https://github.com/gfx-rs/wgpu/blob/v27/docs/api-specs/mesh_shading.md).
47//!
48//! ## Shader Support
49//!
50//! `wgpu` can consume shaders in [WGSL](https://gpuweb.github.io/gpuweb/wgsl/), SPIR-V, and GLSL.
51//! Both [HLSL](https://github.com/Microsoft/DirectXShaderCompiler) and [GLSL](https://github.com/KhronosGroup/glslang)
52//! have compilers to target SPIR-V. All of these shader languages can be used with any backend as we handle all of the conversions. Additionally, support for these shader inputs is not going away.
53//!
54//! While WebGPU does not support any shading language other than WGSL, we will automatically convert your
55//! non-WGSL shaders if you're running on WebGPU.
56//!
57//! WGSL is always supported by default, but GLSL and SPIR-V need features enabled to compile in support.
58//!
59//! To enable WGSL shaders, enable the `wgsl` feature of `wgpu` (enabled by default).
60//! To enable SPIR-V shaders, enable the `spirv` feature of `wgpu`.
61//! To enable GLSL shaders, enable the `glsl` feature of `wgpu`.
62//!
63//! ## Feature flags
64#![doc = document_features::document_features!()]
65//!
66//! ### Feature Aliases
67//!
68//! These features aren't actually features on the crate itself, but a convenient shorthand for
69//! complicated cases.
70//!
71//! - **`wgpu_core`** --- Enabled when there is any non-webgpu backend enabled on the platform.
72//! - **`naga`** --- Enabled when target `glsl` or `spirv`` input is enabled, or when `wgpu_core` is enabled.
73//!
74
75#![no_std]
76#![cfg_attr(docsrs, feature(doc_cfg))]
77#![doc(html_logo_url = "https://raw.githubusercontent.com/gfx-rs/wgpu/trunk/logo.png")]
78#![warn(
79    clippy::alloc_instead_of_core,
80    clippy::allow_attributes,
81    clippy::std_instead_of_alloc,
82    clippy::std_instead_of_core,
83    missing_docs,
84    rust_2018_idioms,
85    unsafe_op_in_unsafe_fn
86)]
87#![allow(
88    // We need to investiagate these.
89    clippy::large_enum_variant,
90    // These degrade readability significantly.
91    clippy::bool_assert_comparison,
92    clippy::bool_comparison,
93)]
94// NOTE: Keep this in sync with `wgpu-core`.
95#![cfg_attr(not(send_sync), allow(clippy::arc_with_non_send_sync))]
96#![cfg_attr(not(any(wgpu_core, webgpu)), allow(unused))]
97
98extern crate alloc;
99#[cfg(std)]
100extern crate std;
101#[cfg(wgpu_core)]
102pub extern crate wgpu_core as wgc;
103#[cfg(wgpu_core)]
104pub extern crate wgpu_hal as hal;
105pub extern crate wgpu_types as wgt;
106
107//
108//
109// Modules
110//
111//
112
113mod api;
114mod backend;
115mod cmp;
116mod dispatch;
117mod macros;
118pub mod util;
119
120//
121//
122// Public re-exports
123//
124//
125
126#[cfg(custom)]
127pub use backend::custom;
128
129pub use api::*;
130pub use wgt::{
131    AdapterInfo, AddressMode, AllocatorReport, AstcBlock, AstcChannel, Backend, BackendOptions,
132    Backends, BindGroupLayoutEntry, BindingType, BlendComponent, BlendFactor, BlendOperation,
133    BlendState, BufferAddress, BufferBindingType, BufferSize, BufferTextureCopyInfo,
134    BufferTransition, BufferUsages, BufferUses, Color, ColorTargetState, ColorWrites,
135    CommandBufferDescriptor, CompareFunction, CompositeAlphaMode, CopyExternalImageDestInfo,
136    CoreCounters, DepthBiasState, DepthStencilState, DeviceLostReason, DeviceType,
137    DownlevelCapabilities, DownlevelFlags, DownlevelLimits, Dx12BackendOptions, Dx12Compiler,
138    Dx12SwapchainKind, Dx12UseFrameLatencyWaitableObject, DxcShaderModel, DynamicOffset,
139    ExperimentalFeatures, Extent3d, ExternalTextureFormat, ExternalTextureTransferFunction, Face,
140    Features, FeaturesWGPU, FeaturesWebGPU, FilterMode, FrontFace, GlBackendOptions,
141    GlFenceBehavior, Gles3MinorVersion, HalCounters, ImageSubresourceRange, ImmediateRange,
142    IndexFormat, InstanceDescriptor, InstanceFlags, InternalCounters, Limits, LoadOpDontCare,
143    MemoryBudgetThresholds, MemoryHints, MipmapFilterMode, MultisampleState, NoopBackendOptions,
144    Origin2d, Origin3d, PipelineStatisticsTypes, PollError, PollStatus, PolygonMode,
145    PowerPreference, PredefinedColorSpace, PresentMode, PresentationTimestamp, PrimitiveState,
146    PrimitiveTopology, QueryType, RenderBundleDepthStencil, RequestAdapterError,
147    SamplerBindingType, SamplerBorderColor, ShaderLocation, ShaderModel, ShaderRuntimeChecks,
148    ShaderStages, StencilFaceState, StencilOperation, StencilState, StorageTextureAccess,
149    SurfaceCapabilities, SurfaceStatus, TexelCopyBufferLayout, TextureAspect, TextureDimension,
150    TextureFormat, TextureFormatFeatureFlags, TextureFormatFeatures, TextureSampleType,
151    TextureTransition, TextureUsages, TextureUses, TextureViewDimension, Trace, VertexAttribute,
152    VertexFormat, VertexStepMode, WasmNotSend, WasmNotSendSync, WasmNotSync, COPY_BUFFER_ALIGNMENT,
153    COPY_BYTES_PER_ROW_ALIGNMENT, IMMEDIATES_ALIGNMENT, MAP_ALIGNMENT, MAXIMUM_SUBGROUP_MAX_SIZE,
154    MINIMUM_SUBGROUP_MIN_SIZE, QUERY_RESOLVE_BUFFER_ALIGNMENT, QUERY_SET_MAX_QUERIES, QUERY_SIZE,
155    VERTEX_ALIGNMENT,
156};
157
158#[expect(deprecated)]
159pub use wgt::VERTEX_STRIDE_ALIGNMENT;
160
161// wasm-only types, we try to keep as many types non-platform
162// specific, but these need to depend on web-sys.
163#[cfg(web)]
164pub use wgt::{CopyExternalImageSourceInfo, ExternalImageSource};
165
166/// Re-export of our `naga` dependency.
167///
168#[cfg(wgpu_core)]
169#[cfg_attr(docsrs, doc(cfg(any(wgpu_core, naga))))]
170// We re-export wgpu-core's re-export of naga, as we may not have direct access to it.
171pub use ::wgc::naga;
172/// Re-export of our `naga` dependency.
173///
174#[cfg(all(not(wgpu_core), naga))]
175#[cfg_attr(docsrs, doc(cfg(any(wgpu_core, naga))))]
176// If that's not available, we re-export our own.
177pub use naga;
178
179/// Re-export of our `raw-window-handle` dependency.
180///
181pub use raw_window_handle as rwh;
182
183/// Re-export of our `web-sys` dependency.
184///
185#[cfg(web)]
186pub use web_sys;
187
188#[doc(hidden)]
189pub use macros::helpers as __macro_helpers;