From 153524023fdb855d578b3431268ac0ec91fd6dcd Mon Sep 17 00:00:00 2001 From: Joshua Batty Date: Mon, 29 Aug 2022 22:09:34 +1000 Subject: [PATCH] egui-wgpu: port over missing update_wgpu_texture methods from `egui_wgpu_backend` (#1981) * add back missing update_egui_texture_from_wgpu_texture methods to renderer module * clippy --- crates/egui-wgpu/src/renderer.rs | 71 ++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/crates/egui-wgpu/src/renderer.rs b/crates/egui-wgpu/src/renderer.rs index 606bb12a..d5c3711a 100644 --- a/crates/egui-wgpu/src/renderer.rs +++ b/crates/egui-wgpu/src/renderer.rs @@ -606,6 +606,35 @@ impl RenderPass { ) } + /// Registers a `wgpu::Texture` with an existing `egui::TextureId`. + /// + /// This enables applications to reuse `TextureId`s. + pub fn update_egui_texture_from_wgpu_texture( + &mut self, + device: &wgpu::Device, + texture: &wgpu::TextureView, + texture_filter: wgpu::FilterMode, + id: egui::TextureId, + ) { + self.update_egui_texture_from_wgpu_texture_with_sampler_options( + device, + texture, + wgpu::SamplerDescriptor { + label: Some( + format!( + "egui_user_image_{}_texture_sampler", + self.next_user_texture_id + ) + .as_str(), + ), + mag_filter: texture_filter, + min_filter: texture_filter, + ..Default::default() + }, + id, + ); + } + /// Registers a `wgpu::Texture` with a `egui::TextureId` while also accepting custom /// `wgpu::SamplerDescriptor` options. /// @@ -655,6 +684,48 @@ impl RenderPass { id } + /// Registers a `wgpu::Texture` with an existing `egui::TextureId` while also accepting custom + /// `wgpu::SamplerDescriptor` options. + /// + /// This allows applications to reuse `TextureId`s created with custom sampler options. + #[allow(clippy::needless_pass_by_value)] // false positive + pub fn update_egui_texture_from_wgpu_texture_with_sampler_options( + &mut self, + device: &wgpu::Device, + texture: &wgpu::TextureView, + sampler_descriptor: wgpu::SamplerDescriptor<'_>, + id: egui::TextureId, + ) { + let (_user_texture, user_texture_binding) = self + .textures + .get_mut(&id) + .expect("Tried to update a texture that has not been allocated yet."); + + let sampler = device.create_sampler(&wgpu::SamplerDescriptor { + compare: None, + ..sampler_descriptor + }); + + let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some( + format!("egui_user_{}_texture_bind_group", self.next_user_texture_id).as_str(), + ), + layout: &self.texture_bind_group_layout, + entries: &[ + wgpu::BindGroupEntry { + binding: 0, + resource: wgpu::BindingResource::TextureView(texture), + }, + wgpu::BindGroupEntry { + binding: 1, + resource: wgpu::BindingResource::Sampler(&sampler), + }, + ], + }); + + *user_texture_binding = bind_group; + } + /// Uploads the uniform, vertex and index data used by the render pass. /// Should be called before `execute()`. pub fn update_buffers(