Skip to main content

dear_imgui_wgpu/renderer/
texture_api.rs

1use super::WgpuRenderer;
2use crate::{RendererError, RendererResult, WgpuTextureManager};
3
4impl WgpuRenderer {
5    /// Get the texture manager
6    pub fn texture_manager(&self) -> &WgpuTextureManager {
7        &self.texture_manager
8    }
9
10    /// Get the texture manager mutably
11    pub fn texture_manager_mut(&mut self) -> &mut WgpuTextureManager {
12        &mut self.texture_manager
13    }
14
15    /// Check if the renderer is initialized
16    pub fn is_initialized(&self) -> bool {
17        self.backend_data.is_some()
18    }
19
20    /// Update a single texture manually
21    ///
22    /// This corresponds to ImGui_ImplWGPU_UpdateTexture in the C++ implementation.
23    /// Use this when you need precise control over texture update timing.
24    ///
25    /// # Returns
26    ///
27    /// Returns a `TextureUpdateResult` that contains any status/ID updates that need
28    /// to be applied to the texture data. This follows Rust's principle of explicit
29    /// state management.
30    ///
31    /// # Example
32    ///
33    /// ```rust,no_run
34    /// # use dear_imgui_wgpu::*;
35    /// # fn example() -> Result<(), Box<dyn std::error::Error>> {
36    /// # // Assume `renderer` has already been created and initialized elsewhere.
37    /// # let mut renderer: WgpuRenderer = todo!();
38    /// # let mut texture_data = dear_imgui_rs::TextureData::new();
39    /// let result = renderer.update_texture(&texture_data)?;
40    /// result.apply_to(&mut texture_data);
41    /// # Ok(())
42    /// # }
43    /// ```
44    pub fn update_texture(
45        &mut self,
46        texture_data: &dear_imgui_rs::TextureData,
47    ) -> RendererResult<crate::TextureUpdateResult> {
48        if let Some(backend_data) = &mut self.backend_data {
49            let result = self.texture_manager.update_single_texture(
50                texture_data,
51                &backend_data.device,
52                &backend_data.queue,
53            )?;
54
55            // Invalidate any cached bind groups for this texture id so that subsequent
56            // draws will see the updated texture view.
57            match result {
58                crate::TextureUpdateResult::Created { texture_id } => {
59                    backend_data
60                        .render_resources
61                        .remove_image_bind_group(texture_id);
62                }
63                crate::TextureUpdateResult::Updated | crate::TextureUpdateResult::Destroyed => {
64                    let id = texture_data.tex_id();
65                    if !id.is_null() {
66                        backend_data.render_resources.remove_image_bind_group(id);
67                    }
68                }
69                crate::TextureUpdateResult::Failed | crate::TextureUpdateResult::NoAction => {}
70            }
71
72            Ok(result)
73        } else {
74            Err(RendererError::InvalidRenderState(
75                "Renderer not initialized".to_string(),
76            ))
77        }
78    }
79}