Skip to main content

dear_imgui_wgpu/
lib.rs

1//! WGPU backend for Dear ImGui
2//!
3//! This crate provides a WGPU-based renderer for Dear ImGui, allowing you to
4//! render Dear ImGui interfaces using the WGPU graphics API.
5//!
6//! # Features
7//!
8//! - **WGPU version selection**: choose exactly one of:
9//!   - `wgpu-29` (default)
10//!   - `wgpu-28`
11//!   - `wgpu-27` (for ecosystems pinned to wgpu 27.x, e.g. some Bevy version trains)
12//! - **Modern texture management**: Full integration with Dear ImGui's ImTextureData system
13//! - **External textures**: Register existing `wgpu::Texture` resources for UI display,
14//!   with optional per-texture custom samplers.
15//! - **Gamma correction**: Automatic sRGB format detection and gamma correction
16//! - **Multi-frame buffering**: Support for multiple frames in flight
17//! - **Device object management**: Helpers to recreate device objects (pipelines/buffers/textures) after loss
18//! - **Multi-viewport support**: Support for multiple windows (feature-gated via `multi-viewport-winit` for winit or `multi-viewport-sdl3` for SDL3 on native targets)
19//!
20//! # Example
21//!
22//! ```rust,no_run
23//! use dear_imgui_rs::Context;
24//! use dear_imgui_wgpu::{WgpuRenderer, WgpuInitInfo};
25//! use wgpu::*;
26//!
27//! // Initialize WGPU device and queue
28//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
29//! let instance = Instance::new(&InstanceDescriptor::default());
30//! let adapter = instance.request_adapter(&RequestAdapterOptions::default()).await.unwrap();
31//! let (device, queue) = adapter.request_device(&DeviceDescriptor::default()).await?;
32//!
33//! // Create Dear ImGui context
34//! let mut imgui = Context::create();
35//!
36//! // Create renderer (recommended path)
37//! let init_info = WgpuInitInfo::new(device, queue, TextureFormat::Bgra8UnormSrgb);
38//! let mut renderer = WgpuRenderer::new(init_info, &mut imgui)?;
39//!
40//! // In your render loop:
41//! // imgui.new_frame();
42//! // ... build your UI ...
43//! // let draw_data = imgui.render();
44//! // renderer.render_draw_data(&draw_data, &mut render_pass)?;
45//! # Ok(())
46//! # }
47//! ```
48
49// Select a single wgpu version via features (default: wgpu-29).
50//
51// We keep the public API surface using `wgpu::*` types, but allow downstream crates to opt into a
52// specific major version to better match their ecosystem (e.g. Bevy).
53#[cfg(all(feature = "wgpu-27", any(feature = "wgpu-28", feature = "wgpu-29")))]
54compile_error!(
55    "Features `wgpu-27`, `wgpu-28`, and `wgpu-29` are mutually exclusive; enable only one."
56);
57#[cfg(all(feature = "wgpu-28", feature = "wgpu-29"))]
58compile_error!(
59    "Features `wgpu-27`, `wgpu-28`, and `wgpu-29` are mutually exclusive; enable only one."
60);
61#[cfg(not(any(feature = "wgpu-27", feature = "wgpu-28", feature = "wgpu-29")))]
62compile_error!(
63    "Either feature `wgpu-27`, `wgpu-28`, or `wgpu-29` must be enabled for dear-imgui-wgpu."
64);
65
66#[cfg(all(feature = "wgpu-27", feature = "webgl"))]
67compile_error!(
68    "Feature `webgl` selects the wgpu-29 WebGL route; use `webgl-wgpu27` with `wgpu-27`."
69);
70#[cfg(all(feature = "wgpu-27", feature = "webgpu"))]
71compile_error!(
72    "Feature `webgpu` selects the wgpu-29 WebGPU route; use `webgpu-wgpu27` with `wgpu-27`."
73);
74#[cfg(all(feature = "wgpu-28", feature = "webgl"))]
75compile_error!(
76    "Feature `webgl` selects the wgpu-29 WebGL route; use `webgl-wgpu28` with `wgpu-28`."
77);
78#[cfg(all(feature = "wgpu-28", feature = "webgpu"))]
79compile_error!(
80    "Feature `webgpu` selects the wgpu-29 WebGPU route; use `webgpu-wgpu28` with `wgpu-28`."
81);
82#[cfg(all(feature = "wgpu-28", feature = "webgl-wgpu27"))]
83compile_error!(
84    "Feature `webgl-wgpu27` is incompatible with `wgpu-28` (would pull multiple wgpu majors)."
85);
86#[cfg(all(feature = "wgpu-28", feature = "webgpu-wgpu27"))]
87compile_error!(
88    "Feature `webgpu-wgpu27` is incompatible with `wgpu-28` (would pull multiple wgpu majors)."
89);
90#[cfg(all(feature = "wgpu-27", feature = "webgl-wgpu28"))]
91compile_error!(
92    "Feature `webgl-wgpu28` is incompatible with `wgpu-27` (would pull multiple wgpu majors)."
93);
94#[cfg(all(feature = "wgpu-27", feature = "webgpu-wgpu28"))]
95compile_error!(
96    "Feature `webgpu-wgpu28` is incompatible with `wgpu-27` (would pull multiple wgpu majors)."
97);
98#[cfg(all(feature = "wgpu-29", feature = "webgl-wgpu27"))]
99compile_error!(
100    "Feature `webgl-wgpu27` is incompatible with `wgpu-29` (would pull multiple wgpu majors)."
101);
102#[cfg(all(feature = "wgpu-29", feature = "webgpu-wgpu27"))]
103compile_error!(
104    "Feature `webgpu-wgpu27` is incompatible with `wgpu-29` (would pull multiple wgpu majors)."
105);
106#[cfg(all(feature = "wgpu-29", feature = "webgl-wgpu28"))]
107compile_error!(
108    "Feature `webgl-wgpu28` is incompatible with `wgpu-29` (would pull multiple wgpu majors)."
109);
110#[cfg(all(feature = "wgpu-29", feature = "webgpu-wgpu28"))]
111compile_error!(
112    "Feature `webgpu-wgpu28` is incompatible with `wgpu-29` (would pull multiple wgpu majors)."
113);
114#[cfg(all(feature = "wgpu-27", feature = "webgl-wgpu29"))]
115compile_error!(
116    "Feature `webgl-wgpu29` is incompatible with `wgpu-27` (would pull multiple wgpu majors)."
117);
118#[cfg(all(feature = "wgpu-27", feature = "webgpu-wgpu29"))]
119compile_error!(
120    "Feature `webgpu-wgpu29` is incompatible with `wgpu-27` (would pull multiple wgpu majors)."
121);
122#[cfg(all(feature = "wgpu-28", feature = "webgl-wgpu29"))]
123compile_error!(
124    "Feature `webgl-wgpu29` is incompatible with `wgpu-28` (would pull multiple wgpu majors)."
125);
126#[cfg(all(feature = "wgpu-28", feature = "webgpu-wgpu29"))]
127compile_error!(
128    "Feature `webgpu-wgpu29` is incompatible with `wgpu-28` (would pull multiple wgpu majors)."
129);
130
131#[cfg(feature = "wgpu-27")]
132pub extern crate wgpu27 as wgpu;
133#[cfg(feature = "wgpu-28")]
134pub extern crate wgpu28 as wgpu;
135#[cfg(feature = "wgpu-29")]
136pub extern crate wgpu29 as wgpu;
137
138// Module declarations
139mod data;
140mod error;
141mod frame_resources;
142mod render_resources;
143mod renderer;
144mod shaders;
145mod texture;
146mod uniforms;
147
148// Re-exports
149pub use data::*;
150pub use error::*;
151pub use frame_resources::*;
152pub use render_resources::*;
153pub use renderer::*;
154pub use shaders::*;
155pub use texture::*;
156pub use uniforms::*;
157
158// Re-export multi-viewport helpers when enabled
159#[cfg(feature = "multi-viewport-winit")]
160pub use renderer::multi_viewport;
161#[cfg(feature = "multi-viewport-sdl3")]
162pub use renderer::multi_viewport_sdl3;
163
164/// Gamma correction mode for the WGPU renderer
165#[derive(Debug, Clone, Copy, PartialEq, Eq)]
166pub enum GammaMode {
167    /// Automatically pick gamma based on render target format (default)
168    Auto,
169    /// Force linear output (gamma = 1.0)
170    Linear,
171    /// Force gamma 2.2 curve (gamma = 2.2)
172    Gamma22,
173}