summaryrefslogtreecommitdiffstats
path: root/wgpu/src/engine.rs
blob: e45b62b208cbcd60ea8e553abfd48b7cb9d24d8b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use crate::buffer;
use crate::graphics::Antialiasing;
use crate::primitive::pipeline;
use crate::quad;
use crate::text;
use crate::triangle;

#[allow(missing_debug_implementations)]
pub struct Engine {
    pub(crate) quad_pipeline: quad::Pipeline,
    pub(crate) text_pipeline: text::Pipeline,
    pub(crate) triangle_pipeline: triangle::Pipeline,
    pub(crate) _pipeline_storage: pipeline::Storage,
    #[cfg(any(feature = "image", feature = "svg"))]
    pub(crate) image_pipeline: crate::image::Pipeline,
    pub(crate) staging_belt: wgpu::util::StagingBelt,
}

impl Engine {
    pub fn new(
        _adapter: &wgpu::Adapter,
        device: &wgpu::Device,
        queue: &wgpu::Queue,
        format: wgpu::TextureFormat,
        antialiasing: Option<Antialiasing>, // TODO: Initialize AA pipelines lazily
    ) -> Self {
        let text_pipeline = text::Pipeline::new(device, queue, format);
        let quad_pipeline = quad::Pipeline::new(device, format);
        let triangle_pipeline =
            triangle::Pipeline::new(device, format, antialiasing);

        #[cfg(any(feature = "image", feature = "svg"))]
        let image_pipeline = {
            let backend = _adapter.get_info().backend;

            crate::image::Pipeline::new(device, format, backend)
        };

        Self {
            // TODO: Resize belt smartly (?)
            // It would be great if the `StagingBelt` API exposed methods
            // for introspection to detect when a resize may be worth it.
            staging_belt: wgpu::util::StagingBelt::new(
                buffer::MAX_WRITE_SIZE as u64,
            ),
            quad_pipeline,
            text_pipeline,
            triangle_pipeline,
            _pipeline_storage: pipeline::Storage::default(),

            #[cfg(any(feature = "image", feature = "svg"))]
            image_pipeline,
        }
    }

    #[cfg(any(feature = "image", feature = "svg"))]
    pub fn image_cache(&self) -> &crate::image::cache::Shared {
        self.image_pipeline.cache()
    }

    pub fn submit(
        &mut self,
        queue: &wgpu::Queue,
        encoder: wgpu::CommandEncoder,
    ) -> wgpu::SubmissionIndex {
        self.staging_belt.finish();
        let index = queue.submit(Some(encoder.finish()));
        self.staging_belt.recall();

        self.quad_pipeline.end_frame();
        self.text_pipeline.end_frame();
        self.triangle_pipeline.end_frame();

        #[cfg(any(feature = "image", feature = "svg"))]
        self.image_pipeline.end_frame();

        index
    }
}