Merge pull request #2425 from iced-rs/fix/image-cache-fighting

Fix windows fighting over shared `image::Cache`
This commit is contained in:
Héctor Ramón 2024-05-06 13:02:57 +02:00 committed by GitHub
commit db07b9ba9e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 80 additions and 70 deletions

View file

@ -70,7 +70,8 @@ fn benchmark(
Some(Antialiasing::MSAAx4), Some(Antialiasing::MSAAx4),
); );
let mut renderer = Renderer::new(&engine, Font::DEFAULT, Pixels::from(16)); let mut renderer =
Renderer::new(&device, &engine, Font::DEFAULT, Pixels::from(16));
let viewport = let viewport =
graphics::Viewport::with_physical_size(Size::new(3840, 2160), 2.0); graphics::Viewport::with_physical_size(Size::new(3840, 2160), 2.0);

View file

@ -157,7 +157,7 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut debug = Debug::new(); let mut debug = Debug::new();
let mut engine = Engine::new(&adapter, &device, &queue, format, None); let mut engine = Engine::new(&adapter, &device, &queue, format, None);
let mut renderer = let mut renderer =
Renderer::new(&engine, Font::default(), Pixels::from(16)); Renderer::new(&device, &engine, Font::default(), Pixels::from(16));
let mut state = program::State::new( let mut state = program::State::new(
controls, controls,

View file

@ -59,8 +59,11 @@ impl Engine {
} }
#[cfg(any(feature = "image", feature = "svg"))] #[cfg(any(feature = "image", feature = "svg"))]
pub fn image_cache(&self) -> &crate::image::cache::Shared { pub fn create_image_cache(
self.image_pipeline.cache() &self,
device: &wgpu::Device,
) -> crate::image::Cache {
self.image_pipeline.create_cache(device)
} }
pub fn submit( pub fn submit(

View file

@ -15,15 +15,23 @@ pub const SIZE: u32 = 2048;
use crate::core::Size; use crate::core::Size;
use crate::graphics::color; use crate::graphics::color;
use std::sync::Arc;
#[derive(Debug)] #[derive(Debug)]
pub struct Atlas { pub struct Atlas {
texture: wgpu::Texture, texture: wgpu::Texture,
texture_view: wgpu::TextureView, texture_view: wgpu::TextureView,
texture_bind_group: wgpu::BindGroup,
texture_layout: Arc<wgpu::BindGroupLayout>,
layers: Vec<Layer>, layers: Vec<Layer>,
} }
impl Atlas { impl Atlas {
pub fn new(device: &wgpu::Device, backend: wgpu::Backend) -> Self { pub fn new(
device: &wgpu::Device,
backend: wgpu::Backend,
texture_layout: Arc<wgpu::BindGroupLayout>,
) -> Self {
let layers = match backend { let layers = match backend {
// On the GL backend we start with 2 layers, to help wgpu figure // On the GL backend we start with 2 layers, to help wgpu figure
// out that this texture is `GL_TEXTURE_2D_ARRAY` rather than `GL_TEXTURE_2D` // out that this texture is `GL_TEXTURE_2D_ARRAY` rather than `GL_TEXTURE_2D`
@ -60,15 +68,27 @@ impl Atlas {
..Default::default() ..Default::default()
}); });
let texture_bind_group =
device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("iced_wgpu::image texture atlas bind group"),
layout: &texture_layout,
entries: &[wgpu::BindGroupEntry {
binding: 0,
resource: wgpu::BindingResource::TextureView(&texture_view),
}],
});
Atlas { Atlas {
texture, texture,
texture_view, texture_view,
texture_bind_group,
texture_layout,
layers, layers,
} }
} }
pub fn view(&self) -> &wgpu::TextureView { pub fn bind_group(&self) -> &wgpu::BindGroup {
&self.texture_view &self.texture_bind_group
} }
pub fn layer_count(&self) -> usize { pub fn layer_count(&self) -> usize {
@ -421,5 +441,17 @@ impl Atlas {
dimension: Some(wgpu::TextureViewDimension::D2Array), dimension: Some(wgpu::TextureViewDimension::D2Array),
..Default::default() ..Default::default()
}); });
self.texture_bind_group =
device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("iced_wgpu::image texture atlas bind group"),
layout: &self.texture_layout,
entries: &[wgpu::BindGroupEntry {
binding: 0,
resource: wgpu::BindingResource::TextureView(
&self.texture_view,
),
}],
});
} }
} }

View file

@ -1,8 +1,7 @@
use crate::core::{self, Size}; use crate::core::{self, Size};
use crate::image::atlas::{self, Atlas}; use crate::image::atlas::{self, Atlas};
use std::cell::{RefCell, RefMut}; use std::sync::Arc;
use std::rc::Rc;
#[derive(Debug)] #[derive(Debug)]
pub struct Cache { pub struct Cache {
@ -14,9 +13,13 @@ pub struct Cache {
} }
impl Cache { impl Cache {
pub fn new(device: &wgpu::Device, backend: wgpu::Backend) -> Self { pub fn new(
device: &wgpu::Device,
backend: wgpu::Backend,
layout: Arc<wgpu::BindGroupLayout>,
) -> Self {
Self { Self {
atlas: Atlas::new(device, backend), atlas: Atlas::new(device, backend, layout),
#[cfg(feature = "image")] #[cfg(feature = "image")]
raster: crate::image::raster::Cache::default(), raster: crate::image::raster::Cache::default(),
#[cfg(feature = "svg")] #[cfg(feature = "svg")]
@ -24,6 +27,10 @@ impl Cache {
} }
} }
pub fn bind_group(&self) -> &wgpu::BindGroup {
self.atlas.bind_group()
}
pub fn layer_count(&self) -> usize { pub fn layer_count(&self) -> usize {
self.atlas.layer_count() self.atlas.layer_count()
} }
@ -69,21 +76,6 @@ impl Cache {
) )
} }
pub fn create_bind_group(
&self,
device: &wgpu::Device,
layout: &wgpu::BindGroupLayout,
) -> wgpu::BindGroup {
device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("iced_wgpu::image texture atlas bind group"),
layout,
entries: &[wgpu::BindGroupEntry {
binding: 0,
resource: wgpu::BindingResource::TextureView(self.atlas.view()),
}],
})
}
pub fn trim(&mut self) { pub fn trim(&mut self) {
#[cfg(feature = "image")] #[cfg(feature = "image")]
self.raster.trim(&mut self.atlas); self.raster.trim(&mut self.atlas);
@ -92,16 +84,3 @@ impl Cache {
self.vector.trim(&mut self.atlas); self.vector.trim(&mut self.atlas);
} }
} }
#[derive(Debug, Clone)]
pub struct Shared(Rc<RefCell<Cache>>);
impl Shared {
pub fn new(cache: Cache) -> Self {
Self(Rc::new(RefCell::new(cache)))
}
pub fn lock(&self) -> RefMut<'_, Cache> {
self.0.borrow_mut()
}
}

View file

@ -13,7 +13,9 @@ use crate::core::{Rectangle, Size, Transformation};
use crate::Buffer; use crate::Buffer;
use bytemuck::{Pod, Zeroable}; use bytemuck::{Pod, Zeroable};
use std::mem; use std::mem;
use std::sync::Arc;
pub use crate::graphics::Image; pub use crate::graphics::Image;
@ -22,13 +24,11 @@ pub type Batch = Vec<Image>;
#[derive(Debug)] #[derive(Debug)]
pub struct Pipeline { pub struct Pipeline {
pipeline: wgpu::RenderPipeline, pipeline: wgpu::RenderPipeline,
backend: wgpu::Backend,
nearest_sampler: wgpu::Sampler, nearest_sampler: wgpu::Sampler,
linear_sampler: wgpu::Sampler, linear_sampler: wgpu::Sampler,
texture: wgpu::BindGroup, texture_layout: Arc<wgpu::BindGroupLayout>,
texture_version: usize,
texture_layout: wgpu::BindGroupLayout,
constant_layout: wgpu::BindGroupLayout, constant_layout: wgpu::BindGroupLayout,
cache: cache::Shared,
layers: Vec<Layer>, layers: Vec<Layer>,
prepare_layer: usize, prepare_layer: usize,
} }
@ -186,25 +186,20 @@ impl Pipeline {
multiview: None, multiview: None,
}); });
let cache = Cache::new(device, backend);
let texture = cache.create_bind_group(device, &texture_layout);
Pipeline { Pipeline {
pipeline, pipeline,
backend,
nearest_sampler, nearest_sampler,
linear_sampler, linear_sampler,
texture, texture_layout: Arc::new(texture_layout),
texture_version: cache.layer_count(),
texture_layout,
constant_layout, constant_layout,
cache: cache::Shared::new(cache),
layers: Vec::new(), layers: Vec::new(),
prepare_layer: 0, prepare_layer: 0,
} }
} }
pub fn cache(&self) -> &cache::Shared { pub fn create_cache(&self, device: &wgpu::Device) -> Cache {
&self.cache Cache::new(device, self.backend, self.texture_layout.clone())
} }
pub fn prepare( pub fn prepare(
@ -212,6 +207,7 @@ impl Pipeline {
device: &wgpu::Device, device: &wgpu::Device,
encoder: &mut wgpu::CommandEncoder, encoder: &mut wgpu::CommandEncoder,
belt: &mut wgpu::util::StagingBelt, belt: &mut wgpu::util::StagingBelt,
cache: &mut Cache,
images: &Batch, images: &Batch,
transformation: Transformation, transformation: Transformation,
scale: f32, scale: f32,
@ -221,8 +217,6 @@ impl Pipeline {
let nearest_instances: &mut Vec<Instance> = &mut Vec::new(); let nearest_instances: &mut Vec<Instance> = &mut Vec::new();
let linear_instances: &mut Vec<Instance> = &mut Vec::new(); let linear_instances: &mut Vec<Instance> = &mut Vec::new();
let mut cache = self.cache.lock();
for image in images { for image in images {
match &image { match &image {
#[cfg(feature = "image")] #[cfg(feature = "image")]
@ -288,16 +282,6 @@ impl Pipeline {
return; return;
} }
let texture_version = cache.layer_count();
if self.texture_version != texture_version {
log::debug!("Atlas has grown. Recreating bind group...");
self.texture =
cache.create_bind_group(device, &self.texture_layout);
self.texture_version = texture_version;
}
if self.layers.len() <= self.prepare_layer { if self.layers.len() <= self.prepare_layer {
self.layers.push(Layer::new( self.layers.push(Layer::new(
device, device,
@ -323,6 +307,7 @@ impl Pipeline {
pub fn render<'a>( pub fn render<'a>(
&'a self, &'a self,
cache: &'a Cache,
layer: usize, layer: usize,
bounds: Rectangle<u32>, bounds: Rectangle<u32>,
render_pass: &mut wgpu::RenderPass<'a>, render_pass: &mut wgpu::RenderPass<'a>,
@ -337,14 +322,13 @@ impl Pipeline {
bounds.height, bounds.height,
); );
render_pass.set_bind_group(1, &self.texture, &[]); render_pass.set_bind_group(1, cache.bind_group(), &[]);
layer.render(render_pass); layer.render(render_pass);
} }
} }
pub fn end_frame(&mut self) { pub fn end_frame(&mut self) {
self.cache.lock().trim();
self.prepare_layer = 0; self.prepare_layer = 0;
} }
} }

View file

@ -67,6 +67,8 @@ use crate::core::{
use crate::graphics::text::{Editor, Paragraph}; use crate::graphics::text::{Editor, Paragraph};
use crate::graphics::Viewport; use crate::graphics::Viewport;
use std::cell::RefCell;
/// A [`wgpu`] graphics renderer for [`iced`]. /// A [`wgpu`] graphics renderer for [`iced`].
/// ///
/// [`wgpu`]: https://github.com/gfx-rs/wgpu-rs /// [`wgpu`]: https://github.com/gfx-rs/wgpu-rs
@ -82,11 +84,12 @@ pub struct Renderer {
// TODO: Centralize all the image feature handling // TODO: Centralize all the image feature handling
#[cfg(any(feature = "svg", feature = "image"))] #[cfg(any(feature = "svg", feature = "image"))]
image_cache: image::cache::Shared, image_cache: RefCell<image::Cache>,
} }
impl Renderer { impl Renderer {
pub fn new( pub fn new(
_device: &wgpu::Device,
_engine: &Engine, _engine: &Engine,
default_font: Font, default_font: Font,
default_text_size: Pixels, default_text_size: Pixels,
@ -100,7 +103,7 @@ impl Renderer {
text_storage: text::Storage::new(), text_storage: text::Storage::new(),
#[cfg(any(feature = "svg", feature = "image"))] #[cfg(any(feature = "svg", feature = "image"))]
image_cache: _engine.image_cache().clone(), image_cache: RefCell::new(_engine.create_image_cache(_device)),
} }
} }
@ -122,6 +125,9 @@ impl Renderer {
self.triangle_storage.trim(); self.triangle_storage.trim();
self.text_storage.trim(); self.text_storage.trim();
#[cfg(any(feature = "svg", feature = "image"))]
self.image_cache.borrow_mut().trim();
} }
fn prepare( fn prepare(
@ -191,6 +197,7 @@ impl Renderer {
device, device,
encoder, encoder,
&mut engine.staging_belt, &mut engine.staging_belt,
&mut self.image_cache.borrow_mut(),
&layer.images, &layer.images,
viewport.projection(), viewport.projection(),
scale_factor, scale_factor,
@ -246,6 +253,8 @@ impl Renderer {
#[cfg(any(feature = "svg", feature = "image"))] #[cfg(any(feature = "svg", feature = "image"))]
let mut image_layer = 0; let mut image_layer = 0;
#[cfg(any(feature = "svg", feature = "image"))]
let image_cache = self.image_cache.borrow();
let scale_factor = viewport.scale_factor() as f32; let scale_factor = viewport.scale_factor() as f32;
let physical_bounds = Rectangle::<f32>::from(Rectangle::with_size( let physical_bounds = Rectangle::<f32>::from(Rectangle::with_size(
@ -359,6 +368,7 @@ impl Renderer {
#[cfg(any(feature = "svg", feature = "image"))] #[cfg(any(feature = "svg", feature = "image"))]
if !layer.images.is_empty() { if !layer.images.is_empty() {
engine.image_pipeline.render( engine.image_pipeline.render(
&image_cache,
image_layer, image_layer,
scissor_rect, scissor_rect,
&mut render_pass, &mut render_pass,
@ -509,7 +519,7 @@ impl core::image::Renderer for Renderer {
type Handle = core::image::Handle; type Handle = core::image::Handle;
fn measure_image(&self, handle: &Self::Handle) -> Size<u32> { fn measure_image(&self, handle: &Self::Handle) -> Size<u32> {
self.image_cache.lock().measure_image(handle) self.image_cache.borrow_mut().measure_image(handle)
} }
fn draw_image( fn draw_image(
@ -535,7 +545,7 @@ impl core::image::Renderer for Renderer {
#[cfg(feature = "svg")] #[cfg(feature = "svg")]
impl core::svg::Renderer for Renderer { impl core::svg::Renderer for Renderer {
fn measure_svg(&self, handle: &core::svg::Handle) -> Size<u32> { fn measure_svg(&self, handle: &core::svg::Handle) -> Size<u32> {
self.image_cache.lock().measure_svg(handle) self.image_cache.borrow_mut().measure_svg(handle)
} }
fn draw_svg( fn draw_svg(

View file

@ -290,6 +290,7 @@ impl graphics::Compositor for Compositor {
fn create_renderer(&self) -> Self::Renderer { fn create_renderer(&self) -> Self::Renderer {
Renderer::new( Renderer::new(
&self.device,
&self.engine, &self.engine,
self.settings.default_font, self.settings.default_font,
self.settings.default_text_size, self.settings.default_text_size,