Stop creating image pipeline when unnecessary
This commit is contained in:
parent
fc55e3a3df
commit
4e7159c22c
11 changed files with 114 additions and 86 deletions
|
|
@ -1,18 +1,23 @@
|
||||||
|
mod atlas;
|
||||||
|
|
||||||
#[cfg(feature = "image")]
|
#[cfg(feature = "image")]
|
||||||
mod raster;
|
mod raster;
|
||||||
|
|
||||||
#[cfg(feature = "svg")]
|
#[cfg(feature = "svg")]
|
||||||
mod vector;
|
mod vector;
|
||||||
|
|
||||||
use crate::{texture, Transformation};
|
use crate::Transformation;
|
||||||
|
use atlas::Atlas;
|
||||||
|
|
||||||
use iced_native::{image, svg, Rectangle};
|
use iced_native::Rectangle;
|
||||||
|
use std::cell::RefCell;
|
||||||
use std::mem;
|
use std::mem;
|
||||||
|
|
||||||
#[cfg(any(feature = "image", feature = "svg"))]
|
#[cfg(feature = "image")]
|
||||||
use std::cell::RefCell;
|
use iced_native::image;
|
||||||
|
|
||||||
#[cfg(any(feature = "image", feature = "svg"))]
|
#[cfg(feature = "svg")]
|
||||||
use crate::texture::atlas;
|
use iced_native::svg;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Pipeline {
|
pub struct Pipeline {
|
||||||
|
|
@ -30,7 +35,7 @@ pub struct Pipeline {
|
||||||
texture: wgpu::BindGroup,
|
texture: wgpu::BindGroup,
|
||||||
texture_version: usize,
|
texture_version: usize,
|
||||||
texture_layout: wgpu::BindGroupLayout,
|
texture_layout: wgpu::BindGroupLayout,
|
||||||
texture_atlas: texture::Atlas,
|
texture_atlas: Atlas,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Pipeline {
|
impl Pipeline {
|
||||||
|
|
@ -216,7 +221,7 @@ impl Pipeline {
|
||||||
usage: wgpu::BufferUsage::VERTEX | wgpu::BufferUsage::COPY_DST,
|
usage: wgpu::BufferUsage::VERTEX | wgpu::BufferUsage::COPY_DST,
|
||||||
});
|
});
|
||||||
|
|
||||||
let texture_atlas = texture::Atlas::new(device);
|
let texture_atlas = Atlas::new(device);
|
||||||
|
|
||||||
let texture = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
let texture = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||||
layout: &texture_layout,
|
layout: &texture_layout,
|
||||||
|
|
@ -284,33 +289,29 @@ impl Pipeline {
|
||||||
|
|
||||||
for image in images {
|
for image in images {
|
||||||
match &image.handle {
|
match &image.handle {
|
||||||
Handle::Raster(_handle) => {
|
#[cfg(feature = "image")]
|
||||||
#[cfg(feature = "image")]
|
Handle::Raster(handle) => {
|
||||||
{
|
if let Some(atlas_entry) = raster_cache.upload(
|
||||||
if let Some(atlas_entry) = raster_cache.upload(
|
handle,
|
||||||
_handle,
|
device,
|
||||||
device,
|
encoder,
|
||||||
encoder,
|
&mut self.texture_atlas,
|
||||||
&mut self.texture_atlas,
|
) {
|
||||||
) {
|
add_instances(image, atlas_entry, instances);
|
||||||
add_instances(image, atlas_entry, instances);
|
}
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
Handle::Vector(_handle) => {
|
#[cfg(feature = "svg")]
|
||||||
#[cfg(feature = "svg")]
|
Handle::Vector(handle) => {
|
||||||
{
|
if let Some(atlas_entry) = vector_cache.upload(
|
||||||
if let Some(atlas_entry) = vector_cache.upload(
|
handle,
|
||||||
_handle,
|
image.size,
|
||||||
image.size,
|
_scale,
|
||||||
_scale,
|
device,
|
||||||
device,
|
encoder,
|
||||||
encoder,
|
&mut self.texture_atlas,
|
||||||
&mut self.texture_atlas,
|
) {
|
||||||
) {
|
add_instances(image, atlas_entry, instances);
|
||||||
add_instances(image, atlas_entry, instances);
|
}
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -432,7 +433,10 @@ pub struct Image {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum Handle {
|
pub enum Handle {
|
||||||
|
#[cfg(feature = "image")]
|
||||||
Raster(image::Handle),
|
Raster(image::Handle),
|
||||||
|
|
||||||
|
#[cfg(feature = "svg")]
|
||||||
Vector(svg::Handle),
|
Vector(svg::Handle),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -479,7 +483,6 @@ struct Uniforms {
|
||||||
transform: [f32; 16],
|
transform: [f32; 16],
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(feature = "image", feature = "svg"))]
|
|
||||||
fn add_instances(
|
fn add_instances(
|
||||||
image: &Image,
|
image: &Image,
|
||||||
entry: &atlas::Entry,
|
entry: &atlas::Entry,
|
||||||
|
|
@ -516,7 +519,6 @@ fn add_instances(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(feature = "image", feature = "svg"))]
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn add_instance(
|
fn add_instance(
|
||||||
position: [f32; 2],
|
position: [f32; 2],
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::texture::atlas::{self, allocator};
|
use crate::image::atlas::{self, allocator};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum Allocation {
|
pub enum Allocation {
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::texture::atlas;
|
use crate::image::atlas;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum Entry {
|
pub enum Entry {
|
||||||
|
|
@ -10,6 +10,7 @@ pub enum Entry {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Entry {
|
impl Entry {
|
||||||
|
#[cfg(feature = "image")]
|
||||||
pub fn size(&self) -> (u32, u32) {
|
pub fn size(&self) -> (u32, u32) {
|
||||||
match self {
|
match self {
|
||||||
Entry::Contiguous(allocation) => allocation.size(),
|
Entry::Contiguous(allocation) => allocation.size(),
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::texture::atlas::Allocator;
|
use crate::image::atlas::Allocator;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum Layer {
|
pub enum Layer {
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::texture::atlas::{self, Atlas};
|
use crate::image::atlas::{self, Atlas};
|
||||||
use iced_native::image;
|
use iced_native::image;
|
||||||
use std::collections::{HashMap, HashSet};
|
use std::collections::{HashMap, HashSet};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::texture::atlas::{self, Atlas};
|
use crate::image::atlas::{self, Atlas};
|
||||||
use iced_native::svg;
|
use iced_native::svg;
|
||||||
use std::collections::{HashMap, HashSet};
|
use std::collections::{HashMap, HashSet};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -30,13 +30,11 @@ pub mod triangle;
|
||||||
pub mod widget;
|
pub mod widget;
|
||||||
pub mod window;
|
pub mod window;
|
||||||
|
|
||||||
mod image;
|
|
||||||
mod primitive;
|
mod primitive;
|
||||||
mod quad;
|
mod quad;
|
||||||
mod renderer;
|
mod renderer;
|
||||||
mod target;
|
mod target;
|
||||||
mod text;
|
mod text;
|
||||||
mod texture;
|
|
||||||
mod transformation;
|
mod transformation;
|
||||||
mod viewport;
|
mod viewport;
|
||||||
|
|
||||||
|
|
@ -52,6 +50,11 @@ pub use viewport::Viewport;
|
||||||
#[doc(no_inline)]
|
#[doc(no_inline)]
|
||||||
pub use widget::*;
|
pub use widget::*;
|
||||||
|
|
||||||
pub(crate) use self::image::Image;
|
|
||||||
pub(crate) use quad::Quad;
|
pub(crate) use quad::Quad;
|
||||||
pub(crate) use transformation::Transformation;
|
pub(crate) use transformation::Transformation;
|
||||||
|
|
||||||
|
#[cfg(any(feature = "image", feature = "svg"))]
|
||||||
|
mod image;
|
||||||
|
|
||||||
|
#[cfg(any(feature = "image", feature = "svg"))]
|
||||||
|
pub(crate) use self::image::Image;
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,11 @@
|
||||||
use crate::{
|
use crate::{
|
||||||
image, quad, text, triangle, Defaults, Image, Primitive, Quad, Settings,
|
quad, text, triangle, Defaults, Primitive, Quad, Settings, Target,
|
||||||
Target, Transformation,
|
Transformation,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#[cfg(any(feature = "image", feature = "svg"))]
|
||||||
|
use crate::{image, Image};
|
||||||
|
|
||||||
use iced_native::{
|
use iced_native::{
|
||||||
layout, Background, Color, Layout, MouseCursor, Point, Rectangle, Vector,
|
layout, Background, Color, Layout, MouseCursor, Point, Rectangle, Vector,
|
||||||
Widget,
|
Widget,
|
||||||
|
|
@ -16,18 +20,22 @@ mod widget;
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Renderer {
|
pub struct Renderer {
|
||||||
quad_pipeline: quad::Pipeline,
|
quad_pipeline: quad::Pipeline,
|
||||||
image_pipeline: image::Pipeline,
|
|
||||||
text_pipeline: text::Pipeline,
|
text_pipeline: text::Pipeline,
|
||||||
triangle_pipeline: crate::triangle::Pipeline,
|
triangle_pipeline: triangle::Pipeline,
|
||||||
|
|
||||||
|
#[cfg(any(feature = "image", feature = "svg"))]
|
||||||
|
image_pipeline: image::Pipeline,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Layer<'a> {
|
struct Layer<'a> {
|
||||||
bounds: Rectangle<u32>,
|
bounds: Rectangle<u32>,
|
||||||
offset: Vector<u32>,
|
offset: Vector<u32>,
|
||||||
quads: Vec<Quad>,
|
quads: Vec<Quad>,
|
||||||
images: Vec<Image>,
|
|
||||||
meshes: Vec<(Point, Arc<triangle::Mesh2D>)>,
|
meshes: Vec<(Point, Arc<triangle::Mesh2D>)>,
|
||||||
text: Vec<wgpu_glyph::Section<'a>>,
|
text: Vec<wgpu_glyph::Section<'a>>,
|
||||||
|
|
||||||
|
#[cfg(any(feature = "image", feature = "svg"))]
|
||||||
|
images: Vec<Image>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Layer<'a> {
|
impl<'a> Layer<'a> {
|
||||||
|
|
@ -36,9 +44,11 @@ impl<'a> Layer<'a> {
|
||||||
bounds,
|
bounds,
|
||||||
offset,
|
offset,
|
||||||
quads: Vec::new(),
|
quads: Vec::new(),
|
||||||
images: Vec::new(),
|
|
||||||
text: Vec::new(),
|
text: Vec::new(),
|
||||||
meshes: Vec::new(),
|
meshes: Vec::new(),
|
||||||
|
|
||||||
|
#[cfg(any(feature = "image", feature = "svg"))]
|
||||||
|
images: Vec::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -51,19 +61,22 @@ impl Renderer {
|
||||||
let text_pipeline =
|
let text_pipeline =
|
||||||
text::Pipeline::new(device, settings.format, settings.default_font);
|
text::Pipeline::new(device, settings.format, settings.default_font);
|
||||||
let quad_pipeline = quad::Pipeline::new(device, settings.format);
|
let quad_pipeline = quad::Pipeline::new(device, settings.format);
|
||||||
let image_pipeline =
|
|
||||||
crate::image::Pipeline::new(device, settings.format);
|
|
||||||
let triangle_pipeline = triangle::Pipeline::new(
|
let triangle_pipeline = triangle::Pipeline::new(
|
||||||
device,
|
device,
|
||||||
settings.format,
|
settings.format,
|
||||||
settings.antialiasing,
|
settings.antialiasing,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
#[cfg(any(feature = "image", feature = "svg"))]
|
||||||
|
let image_pipeline = image::Pipeline::new(device, settings.format);
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
quad_pipeline,
|
quad_pipeline,
|
||||||
image_pipeline,
|
|
||||||
text_pipeline,
|
text_pipeline,
|
||||||
triangle_pipeline,
|
triangle_pipeline,
|
||||||
|
|
||||||
|
#[cfg(any(feature = "image", feature = "svg"))]
|
||||||
|
image_pipeline,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -116,6 +129,7 @@ impl Renderer {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(any(feature = "image", feature = "svg"))]
|
||||||
self.image_pipeline.trim_cache();
|
self.image_pipeline.trim_cache();
|
||||||
|
|
||||||
*mouse_cursor
|
*mouse_cursor
|
||||||
|
|
@ -223,20 +237,6 @@ impl Renderer {
|
||||||
border_color: border_color.into_linear(),
|
border_color: border_color.into_linear(),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
Primitive::Image { handle, bounds } => {
|
|
||||||
layer.images.push(Image {
|
|
||||||
handle: image::Handle::Raster(handle.clone()),
|
|
||||||
position: [bounds.x, bounds.y],
|
|
||||||
size: [bounds.width, bounds.height],
|
|
||||||
});
|
|
||||||
}
|
|
||||||
Primitive::Svg { handle, bounds } => {
|
|
||||||
layer.images.push(Image {
|
|
||||||
handle: image::Handle::Vector(handle.clone()),
|
|
||||||
position: [bounds.x, bounds.y],
|
|
||||||
size: [bounds.width, bounds.height],
|
|
||||||
});
|
|
||||||
}
|
|
||||||
Primitive::Mesh2D { origin, buffers } => {
|
Primitive::Mesh2D { origin, buffers } => {
|
||||||
layer.meshes.push((*origin, buffers.clone()));
|
layer.meshes.push((*origin, buffers.clone()));
|
||||||
}
|
}
|
||||||
|
|
@ -264,6 +264,28 @@ impl Renderer {
|
||||||
layers.push(new_layer);
|
layers.push(new_layer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "image")]
|
||||||
|
Primitive::Image { handle, bounds } => {
|
||||||
|
layer.images.push(Image {
|
||||||
|
handle: image::Handle::Raster(handle.clone()),
|
||||||
|
position: [bounds.x, bounds.y],
|
||||||
|
size: [bounds.width, bounds.height],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
#[cfg(not(feature = "image"))]
|
||||||
|
Primitive::Image { .. } => {}
|
||||||
|
|
||||||
|
#[cfg(feature = "svg")]
|
||||||
|
Primitive::Svg { handle, bounds } => {
|
||||||
|
layer.images.push(Image {
|
||||||
|
handle: image::Handle::Vector(handle.clone()),
|
||||||
|
position: [bounds.x, bounds.y],
|
||||||
|
size: [bounds.width, bounds.height],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
#[cfg(not(feature = "svg"))]
|
||||||
|
Primitive::Svg { .. } => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -346,23 +368,26 @@ impl Renderer {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if layer.images.len() > 0 {
|
#[cfg(any(feature = "image", feature = "svg"))]
|
||||||
let translated_and_scaled = transformation
|
{
|
||||||
* Transformation::scale(scale_factor, scale_factor)
|
if layer.images.len() > 0 {
|
||||||
* Transformation::translate(
|
let translated_and_scaled = transformation
|
||||||
-(layer.offset.x as f32),
|
* Transformation::scale(scale_factor, scale_factor)
|
||||||
-(layer.offset.y as f32),
|
* Transformation::translate(
|
||||||
);
|
-(layer.offset.x as f32),
|
||||||
|
-(layer.offset.y as f32),
|
||||||
|
);
|
||||||
|
|
||||||
self.image_pipeline.draw(
|
self.image_pipeline.draw(
|
||||||
device,
|
device,
|
||||||
encoder,
|
encoder,
|
||||||
&layer.images,
|
&layer.images,
|
||||||
translated_and_scaled,
|
translated_and_scaled,
|
||||||
bounds,
|
bounds,
|
||||||
target,
|
target,
|
||||||
scale_factor,
|
scale_factor,
|
||||||
);
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if layer.text.len() > 0 {
|
if layer.text.len() > 0 {
|
||||||
|
|
|
||||||
|
|
@ -1,3 +0,0 @@
|
||||||
pub mod atlas;
|
|
||||||
|
|
||||||
pub use atlas::Atlas;
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue