Implement support for multiple text layers in iced_wgpu
This commit is contained in:
parent
baf51a8fcf
commit
ba258f8fbc
2 changed files with 35 additions and 13 deletions
|
|
@ -105,6 +105,8 @@ impl Backend {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self.text_pipeline.end_frame();
|
||||||
|
|
||||||
#[cfg(any(feature = "image", feature = "svg"))]
|
#[cfg(any(feature = "image", feature = "svg"))]
|
||||||
self.image_pipeline.trim_cache(device, encoder);
|
self.image_pipeline.trim_cache(device, encoder);
|
||||||
}
|
}
|
||||||
|
|
@ -180,6 +182,7 @@ impl Backend {
|
||||||
device,
|
device,
|
||||||
queue,
|
queue,
|
||||||
&layer.text,
|
&layer.text,
|
||||||
|
layer.bounds,
|
||||||
scale_factor,
|
scale_factor,
|
||||||
target_size,
|
target_size,
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,14 @@
|
||||||
pub use iced_native::text::Hit;
|
pub use iced_native::text::Hit;
|
||||||
|
|
||||||
use iced_graphics::layer::Text;
|
use iced_graphics::layer::Text;
|
||||||
use iced_native::{Font, Size};
|
use iced_native::{Font, Rectangle, Size};
|
||||||
|
|
||||||
#[allow(missing_debug_implementations)]
|
#[allow(missing_debug_implementations)]
|
||||||
pub struct Pipeline {
|
pub struct Pipeline {
|
||||||
renderer: glyphon::TextRenderer,
|
renderers: Vec<glyphon::TextRenderer>,
|
||||||
atlas: glyphon::TextAtlas,
|
atlas: glyphon::TextAtlas,
|
||||||
cache: glyphon::SwashCache<'static>,
|
cache: glyphon::SwashCache<'static>,
|
||||||
|
layer: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Share with `iced_graphics`
|
// TODO: Share with `iced_graphics`
|
||||||
|
|
@ -23,9 +24,10 @@ impl Pipeline {
|
||||||
_multithreading: bool,
|
_multithreading: bool,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
Pipeline {
|
Pipeline {
|
||||||
renderer: glyphon::TextRenderer::new(device, queue),
|
renderers: Vec::new(),
|
||||||
atlas: glyphon::TextAtlas::new(device, queue, format),
|
atlas: glyphon::TextAtlas::new(device, queue, format),
|
||||||
cache: glyphon::SwashCache::new(&FONT_SYSTEM),
|
cache: glyphon::SwashCache::new(&FONT_SYSTEM),
|
||||||
|
layer: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -34,9 +36,17 @@ impl Pipeline {
|
||||||
device: &wgpu::Device,
|
device: &wgpu::Device,
|
||||||
queue: &wgpu::Queue,
|
queue: &wgpu::Queue,
|
||||||
sections: &[Text<'_>],
|
sections: &[Text<'_>],
|
||||||
|
bounds: Rectangle,
|
||||||
scale_factor: f32,
|
scale_factor: f32,
|
||||||
target_size: Size<u32>,
|
target_size: Size<u32>,
|
||||||
) {
|
) {
|
||||||
|
if self.renderers.len() <= self.layer {
|
||||||
|
self.renderers
|
||||||
|
.push(glyphon::TextRenderer::new(device, queue));
|
||||||
|
}
|
||||||
|
|
||||||
|
let renderer = &mut self.renderers[self.layer];
|
||||||
|
|
||||||
let buffers: Vec<_> = sections
|
let buffers: Vec<_> = sections
|
||||||
.iter()
|
.iter()
|
||||||
.map(|section| {
|
.map(|section| {
|
||||||
|
|
@ -73,6 +83,13 @@ impl Pipeline {
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
|
let bounds = glyphon::TextBounds {
|
||||||
|
left: (bounds.x * scale_factor) as i32,
|
||||||
|
top: (bounds.y * scale_factor) as i32,
|
||||||
|
right: ((bounds.x + bounds.width) * scale_factor) as i32,
|
||||||
|
bottom: ((bounds.y + bounds.height) * scale_factor) as i32,
|
||||||
|
};
|
||||||
|
|
||||||
let text_areas: Vec<_> = sections
|
let text_areas: Vec<_> = sections
|
||||||
.iter()
|
.iter()
|
||||||
.zip(buffers.iter())
|
.zip(buffers.iter())
|
||||||
|
|
@ -80,18 +97,11 @@ impl Pipeline {
|
||||||
buffer,
|
buffer,
|
||||||
left: (section.bounds.x * scale_factor) as i32,
|
left: (section.bounds.x * scale_factor) as i32,
|
||||||
top: (section.bounds.y * scale_factor) as i32,
|
top: (section.bounds.y * scale_factor) as i32,
|
||||||
bounds: glyphon::TextBounds {
|
bounds,
|
||||||
left: (section.bounds.x * scale_factor) as i32,
|
|
||||||
top: (section.bounds.y * scale_factor) as i32,
|
|
||||||
right: ((section.bounds.x + section.bounds.width)
|
|
||||||
* scale_factor) as i32,
|
|
||||||
bottom: ((section.bounds.y + section.bounds.height)
|
|
||||||
* scale_factor) as i32,
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
self.renderer
|
renderer
|
||||||
.prepare(
|
.prepare(
|
||||||
device,
|
device,
|
||||||
queue,
|
queue,
|
||||||
|
|
@ -126,9 +136,18 @@ impl Pipeline {
|
||||||
depth_stencil_attachment: None,
|
depth_stencil_attachment: None,
|
||||||
});
|
});
|
||||||
|
|
||||||
self.renderer
|
let renderer = &mut self.renderers[self.layer];
|
||||||
|
|
||||||
|
renderer
|
||||||
.render(&self.atlas, &mut render_pass)
|
.render(&self.atlas, &mut render_pass)
|
||||||
.expect("Render text");
|
.expect("Render text");
|
||||||
|
|
||||||
|
self.layer += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn end_frame(&mut self) {
|
||||||
|
self.renderers.truncate(self.layer);
|
||||||
|
self.layer = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn measure(
|
pub fn measure(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue