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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
use crate::{internals::GpuBatch, Canvas, Images, Picture, Pipeline, Recorder};
use wgpu::util::DeviceExt as _;

pub type Image = u32;

pub struct Renderer {
    pub recorder: Recorder<Image>,
    pub batch: GpuBatch,
    pub pipeline: Pipeline,
    pub images: Images<Image>,

    pub(crate) view_buffer: wgpu::Buffer,
    pub(crate) view_binding: wgpu::BindGroup,
    pub(crate) depth_stencil: wgpu::TextureView,
    pub(crate) width: u32,
    pub(crate) height: u32,

    image_index: Image,
}

impl Renderer {
    pub fn new(device: &wgpu::Device, width: u32, height: u32) -> Self {
        let images = Images::new(device);
        let batch = GpuBatch::new(device);
        let pipeline = Pipeline::new(device, &images.layout);
        let recorder = Recorder::default();

        let contents = crate::combine_viewport(width, height);
        let view_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
            label: Some("reui::Viewport"),
            contents: bytemuck::bytes_of(&contents),
            usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
        });

        let view_binding = device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: Some("reui::Viewport"),
            layout: &pipeline.view_layout,
            entries: &[wgpu::BindGroupEntry {
                binding: 0,
                resource: view_buffer.as_entire_binding(),
            }],
        });

        let depth_stencil = create_depth_texture(device, width, height);

        Self {
            batch,
            pipeline,
            recorder,
            images,

            view_buffer,
            view_binding,
            depth_stencil,
            width,
            height,

            image_index: 0,
        }
    }

    pub fn upload_image(
        &mut self,
        device: &wgpu::Device,
        queue: &wgpu::Queue,
        label: Option<&str>,
        width: u32,
        height: u32,
        data: &[u8],
        sampler: Option<&wgpu::Sampler>,
    ) -> Image {
        let image_key = self.image_index;
        drop(self.images.upload(
            device, queue, image_key, label, width, height, data, sampler,
        ));
        self.image_index += 1;
        image_key
    }

    pub fn start(
        &mut self,
        device: &wgpu::Device,
        queue: &wgpu::Queue,
        width: u32,
        height: u32,
    ) -> Canvas<Image> {
        self.recorder.clear();

        if self.width != width || self.height != height {
            self.depth_stencil = create_depth_texture(device, width, height);
        }

        let viewport = crate::combine_viewport(width, height);
        queue.write_buffer(&self.view_buffer, 0, bytemuck::bytes_of(&viewport));

        self.width = width;
        self.height = height;

        Canvas::new(&mut self.recorder, &self.images)
    }

    pub fn flush(
        &mut self,
        encoder: &mut wgpu::CommandEncoder,
        staging_belt: &mut wgpu::util::StagingBelt,
        device: &wgpu::Device,
        view: &wgpu::TextureView,
        clear_color: Option<wgpu::Color>,
    ) {
        self.batch
            .staging(encoder, staging_belt, device, &self.recorder.batch);

        let bundle = Picture::new(
            device,
            &self.view_binding,
            0,
            &self.pipeline,
            &self.batch,
            &self.images,
            &self.recorder.calls,
        );

        crate::render_pictures(
            encoder,
            view,
            &self.depth_stencil,
            &bundle,
            clear_color,
            true,
        )
    }
}

fn create_depth_texture(device: &wgpu::Device, width: u32, height: u32) -> wgpu::TextureView {
    device
        .create_texture(&wgpu::TextureDescriptor {
            label: Some("reui::DepthStencil"),
            size: wgpu::Extent3d {
                width,
                height,
                depth_or_array_layers: 1,
            },
            mip_level_count: 1,
            sample_count: 1,
            dimension: wgpu::TextureDimension::D2,
            format: wgpu::TextureFormat::Depth24PlusStencil8,
            usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
            view_formats: &[],
        })
        .create_view(&wgpu::TextureViewDescriptor::default())
}