iced_wgpu/
engine.rs

1use crate::buffer;
2use crate::graphics::Antialiasing;
3use crate::primitive;
4use crate::quad;
5use crate::text;
6use crate::triangle;
7
8#[allow(missing_debug_implementations)]
9pub struct Engine {
10    pub(crate) staging_belt: wgpu::util::StagingBelt,
11    pub(crate) format: wgpu::TextureFormat,
12
13    pub(crate) quad_pipeline: quad::Pipeline,
14    pub(crate) text_pipeline: text::Pipeline,
15    pub(crate) triangle_pipeline: triangle::Pipeline,
16    #[cfg(any(feature = "image", feature = "svg"))]
17    pub(crate) image_pipeline: crate::image::Pipeline,
18    pub(crate) primitive_storage: primitive::Storage,
19}
20
21impl Engine {
22    pub fn new(
23        _adapter: &wgpu::Adapter,
24        device: &wgpu::Device,
25        queue: &wgpu::Queue,
26        format: wgpu::TextureFormat,
27        antialiasing: Option<Antialiasing>, // TODO: Initialize AA pipelines lazily
28    ) -> Self {
29        let text_pipeline = text::Pipeline::new(device, queue, format);
30        let quad_pipeline = quad::Pipeline::new(device, format);
31        let triangle_pipeline =
32            triangle::Pipeline::new(device, format, antialiasing);
33
34        #[cfg(any(feature = "image", feature = "svg"))]
35        let image_pipeline = {
36            let backend = _adapter.get_info().backend;
37
38            crate::image::Pipeline::new(device, format, backend)
39        };
40
41        Self {
42            // TODO: Resize belt smartly (?)
43            // It would be great if the `StagingBelt` API exposed methods
44            // for introspection to detect when a resize may be worth it.
45            staging_belt: wgpu::util::StagingBelt::new(
46                buffer::MAX_WRITE_SIZE as u64,
47            ),
48            format,
49
50            quad_pipeline,
51            text_pipeline,
52            triangle_pipeline,
53
54            #[cfg(any(feature = "image", feature = "svg"))]
55            image_pipeline,
56
57            primitive_storage: primitive::Storage::default(),
58        }
59    }
60
61    #[cfg(any(feature = "image", feature = "svg"))]
62    pub fn create_image_cache(
63        &self,
64        device: &wgpu::Device,
65    ) -> crate::image::Cache {
66        self.image_pipeline.create_cache(device)
67    }
68
69    pub fn submit(
70        &mut self,
71        queue: &wgpu::Queue,
72        encoder: wgpu::CommandEncoder,
73    ) -> wgpu::SubmissionIndex {
74        self.staging_belt.finish();
75        let index = queue.submit(Some(encoder.finish()));
76        self.staging_belt.recall();
77
78        self.quad_pipeline.end_frame();
79        self.text_pipeline.end_frame();
80        self.triangle_pipeline.end_frame();
81
82        #[cfg(any(feature = "image", feature = "svg"))]
83        self.image_pipeline.end_frame();
84
85        index
86    }
87}