Skip to main content

dear_imgui_wgpu/renderer/
external_textures.rs

1use super::WgpuRenderer;
2use crate::{ExternalTextureId, RendererError, RendererResult};
3
4impl WgpuRenderer {
5    /// Registers an application-owned WGPU texture view for Dear ImGui rendering.
6    ///
7    /// The renderer clones the view handle. The application retains ownership of the texture
8    /// contents and must not explicitly destroy the underlying GPU resource while it is
9    /// registered.
10    pub fn register_external_texture(
11        &mut self,
12        view: &wgpu::TextureView,
13    ) -> RendererResult<ExternalTextureId> {
14        self.ensure_renderer_contract()?;
15        self.texture_manager.register_external_view(view)
16    }
17
18    /// Replaces the WGPU view associated with an external texture handle.
19    ///
20    /// Stale handles, handles from another renderer, and already-unregistered handles are
21    /// rejected without changing renderer state.
22    pub fn update_external_texture(
23        &mut self,
24        texture: ExternalTextureId,
25        view: &wgpu::TextureView,
26    ) -> RendererResult<()> {
27        self.ensure_renderer_contract()?;
28        let backend = self.backend_data.as_mut().ok_or_else(|| {
29            RendererError::InvalidRenderState("WGPU renderer is not initialized".to_owned())
30        })?;
31        self.texture_manager.update_external_view(texture, view)?;
32        backend
33            .render_resources
34            .remove_image_bind_group(texture.texture_id());
35        Ok(())
36    }
37
38    /// Unregisters an application-owned external texture view.
39    ///
40    /// The underlying WGPU texture remains application-owned and is not destroyed by this call.
41    pub fn unregister_external_texture(
42        &mut self,
43        texture: ExternalTextureId,
44    ) -> RendererResult<()> {
45        self.ensure_renderer_contract()?;
46        let backend = self.backend_data.as_mut().ok_or_else(|| {
47            RendererError::InvalidRenderState("WGPU renderer is not initialized".to_owned())
48        })?;
49        self.texture_manager.remove_external_view(texture)?;
50        backend
51            .render_resources
52            .remove_image_bind_group(texture.texture_id());
53        Ok(())
54    }
55}