Skip to main content

dear_imgui_wgpu/
texture.rs

1//! Texture management for the WGPU renderer
2//!
3//! Application-owned texture views use opaque [`ExternalTextureId`] handles. Context-owned
4//! textures are keyed by pointer-free [`SnapshotTextureId`] values and are only changed by owned
5//! renderer requests.
6
7mod cache;
8mod cleanup;
9mod manager;
10mod resource;
11#[cfg(test)]
12mod tests;
13mod upload;
14
15use crate::{RenderResources, RendererError, RendererResult};
16use dear_imgui_rs::{
17    TextureId,
18    render::{SnapshotTextureId, TextureFeedback, TextureOp, TextureRequest, TextureUploadRect},
19    texture::{TextureFormat as ImGuiTextureFormat, TextureRect},
20};
21use std::collections::HashMap;
22use wgpu::*;
23
24pub(crate) use manager::WgpuTextureManager;
25pub(crate) use resource::OwnedWgpuTexture;
26
27/// Opaque handle for an application-owned WGPU texture view registered with a renderer.
28///
29/// The handle can be passed to Dear ImGui through [`Self::texture_id`], but cannot be forged from
30/// an arbitrary [`TextureId`]. Registration owns a clone of the WGPU view handle; the application
31/// remains responsible for the texture contents and must not explicitly destroy the underlying
32/// GPU resource while it is registered.
33#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
34#[repr(transparent)]
35pub struct ExternalTextureId(TextureId);
36
37impl ExternalTextureId {
38    pub(super) const fn new(texture_id: TextureId) -> Self {
39        Self(texture_id)
40    }
41
42    /// Returns the Dear ImGui texture identifier used by image widgets and draw-list commands.
43    #[must_use]
44    pub const fn texture_id(self) -> TextureId {
45        self.0
46    }
47}
48
49impl From<ExternalTextureId> for TextureId {
50    fn from(texture: ExternalTextureId) -> Self {
51        texture.texture_id()
52    }
53}