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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
use crate::{
    BufferPass, BufferStore, GpuDevice, GpuWindow, GraphicsError, Index,
    Layout, LayoutStorage, OtherError, PipeLineLayout, PipelineStorage,
    StaticBufferObject,
};
use cosmic_text::FontSystem;
use slotmap::SlotMap;
use std::rc::Rc;

use winit::{dpi::PhysicalSize, event::WindowEvent, window::Window};

///Handles the Window, Device and buffer stores.
pub struct GpuRenderer {
    pub(crate) window: GpuWindow,
    pub(crate) device: GpuDevice,
    pub(crate) buffer_stores: SlotMap<Index, BufferStore>,
    pub(crate) layout_storage: LayoutStorage,
    pub(crate) pipeline_storage: PipelineStorage,
    pub(crate) depthbuffer: wgpu::TextureView,
    pub(crate) framebuffer: Option<wgpu::TextureView>,
    pub(crate) frame: Option<wgpu::SurfaceTexture>,
    pub font_sys: FontSystem,
    pub buffer_object: StaticBufferObject,
}

pub trait SetBuffers<'a, 'b>
where
    'b: 'a,
{
    fn set_buffers(&mut self, buffer: BufferPass<'b>);
}

impl<'a, 'b> SetBuffers<'a, 'b> for wgpu::RenderPass<'a>
where
    'b: 'a,
{
    fn set_buffers(&mut self, buffer: BufferPass<'b>) {
        self.set_vertex_buffer(0, buffer.vertex_buffer.slice(..));
        self.set_index_buffer(
            buffer.index_buffer.slice(..),
            wgpu::IndexFormat::Uint32,
        );
    }
}

impl GpuRenderer {
    pub fn new(window: GpuWindow, device: GpuDevice) -> Self {
        let buffer_object = StaticBufferObject::create_buffer(&device);
        let depth_buffer = window.create_depth_texture(&device);

        Self {
            window,
            device,
            buffer_stores: SlotMap::with_capacity_and_key(1024),
            layout_storage: LayoutStorage::new(),
            pipeline_storage: PipelineStorage::new(),
            depthbuffer: depth_buffer,
            framebuffer: None,
            frame: None,
            font_sys: FontSystem::new(),
            buffer_object,
        }
    }

    pub fn adapter(&self) -> &wgpu::Adapter {
        self.window.adapter()
    }

    pub fn resize(
        &mut self,
        size: PhysicalSize<u32>,
    ) -> Result<(), GraphicsError> {
        self.window.resize(&self.device, size)
    }

    pub fn frame_buffer(&self) -> &Option<wgpu::TextureView> {
        &self.framebuffer
    }

    pub fn depth_buffer(&self) -> &wgpu::TextureView {
        &self.depthbuffer
    }

    pub fn size(&self) -> PhysicalSize<f32> {
        self.window.size
    }

    pub fn inner_size(&self) -> PhysicalSize<u32> {
        self.window.inner_size
    }

    pub fn surface(&self) -> &wgpu::Surface {
        &self.window.surface
    }

    pub fn surface_format(&self) -> wgpu::TextureFormat {
        self.window.surface_format
    }

    pub fn update(
        &mut self,
        event: &WindowEvent,
    ) -> Result<bool, GraphicsError> {
        let frame = match self.window.update(&self.device, event)? {
            Some(frame) => frame,
            _ => return Ok(false),
        };

        self.framebuffer = Some(
            frame
                .texture
                .create_view(&wgpu::TextureViewDescriptor::default()),
        );
        self.frame = Some(frame);

        Ok(true)
    }

    pub fn window(&self) -> &Window {
        &self.window.window
    }

    pub fn update_depth_texture(&mut self) {
        self.depthbuffer = self.window.create_depth_texture(&self.device);
    }

    pub fn present(&mut self) -> Result<(), GraphicsError> {
        self.framebuffer = None;

        match self.frame.take() {
            Some(frame) => {
                frame.present();
                Ok(())
            }
            None => Err(GraphicsError::Other(OtherError::new(
                "Frame does not Exist. Did you forget to update the renderer?",
            ))),
        }
    }

    pub fn device(&self) -> &wgpu::Device {
        &self.device.device
    }

    pub fn gpu_device(&self) -> &GpuDevice {
        &self.device
    }

    pub fn queue(&self) -> &wgpu::Queue {
        &self.device.queue
    }

    pub fn font_sys(&self) -> &FontSystem {
        &self.font_sys
    }

    pub fn font_sys_mut(&mut self) -> &mut FontSystem {
        &mut self.font_sys
    }

    pub fn new_buffer(
        &mut self,
        store_size: usize,
        index_size: usize,
    ) -> Index {
        self.buffer_stores
            .insert(BufferStore::new(store_size, index_size))
    }

    pub fn default_buffer(&mut self) -> Index {
        self.buffer_stores.insert(BufferStore::default())
    }

    pub fn remove_buffer(&mut self, index: Index) {
        let _ = self.buffer_stores.remove(index);
    }

    pub fn get_buffer(&self, index: Index) -> Option<&BufferStore> {
        self.buffer_stores.get(index)
    }

    pub fn get_buffer_mut(&mut self, index: Index) -> Option<&mut BufferStore> {
        self.buffer_stores.get_mut(index)
    }

    pub fn create_layout<K: Layout>(
        &mut self,
        layout: K,
    ) -> Rc<wgpu::BindGroupLayout> {
        self.layout_storage.create_layout(&mut self.device, layout)
    }

    pub fn create_pipelines(&mut self, surface_format: wgpu::TextureFormat) {
        self.pipeline_storage.create_pipeline(
            &mut self.device,
            &mut self.layout_storage,
            surface_format,
            crate::ImageRenderPipeline,
        );

        self.pipeline_storage.create_pipeline(
            &mut self.device,
            &mut self.layout_storage,
            surface_format,
            crate::MapRenderPipeline,
        );

        self.pipeline_storage.create_pipeline(
            &mut self.device,
            &mut self.layout_storage,
            surface_format,
            crate::TextRenderPipeline,
        );

        self.pipeline_storage.create_pipeline(
            &mut self.device,
            &mut self.layout_storage,
            surface_format,
            crate::Mesh2DRenderPipeline,
        );

        self.pipeline_storage.create_pipeline(
            &mut self.device,
            &mut self.layout_storage,
            surface_format,
            crate::LightRenderPipeline,
        );

        self.pipeline_storage.create_pipeline(
            &mut self.device,
            &mut self.layout_storage,
            surface_format,
            crate::RectRenderPipeline,
        );
    }

    pub fn get_pipelines<K: PipeLineLayout>(
        &self,
        pipeline: K,
    ) -> Option<&wgpu::RenderPipeline> {
        self.pipeline_storage.get_pipeline(pipeline)
    }
}