ascending_graphics 0.38.3

A graphical rendering library for 2D, using wgpu and winit.
Documentation
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
use crate::{
    BufferPass, GpuDevice, GpuWindow, GraphicsError, IBOStore, Index, Layout,
    LayoutStorage, OtherError, PipeLineLayout, PipelineStorage,
    StaticVertexBuffer, VBOStore,
};
use cosmic_text::FontSystem;
use slotmap::SlotMap;
use std::sync::Arc;

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

/// Handles the [`GpuWindow`], [`GpuDevice`] and [`BufferStore`]'s.
/// Also handles other information important to Rendering to the GPU.
///
#[derive(Debug)]
pub struct GpuRenderer {
    pub(crate) window: GpuWindow,
    pub(crate) device: GpuDevice,
    pub(crate) ibo_stores: SlotMap<Index, IBOStore>,
    pub(crate) vbo_stores: SlotMap<Index, VBOStore>,
    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: StaticVertexBuffer,
    pub backend: wgpu::Backend,
}

/// Used to deturmine which Pipelines to enable when using the Renderer.
///
#[derive(Debug, Default, Copy, Clone)]
pub struct EnabledPipelines {
    pub image_pipeline: bool,
    pub anim_image_pipeline: bool,
    pub map_pipeline: bool,
    pub text_pipeline: bool,
    pub mesh2d_pipeline: bool,
    pub light_pipeline: bool,
    pub rect_pipeline: bool,
}

impl EnabledPipelines {
    pub fn all() -> Self {
        Self {
            image_pipeline: true,
            anim_image_pipeline: true,
            map_pipeline: true,
            text_pipeline: true,
            mesh2d_pipeline: true,
            light_pipeline: true,
            rect_pipeline: true,
        }
    }
}

/// Trait to allow [`wgpu::RenderPass`] to Set the Vertex and Index buffers.
///
pub trait SetBuffers<'a, 'b>
where
    'b: 'a,
{
    /// Sets the Vertex and Index buffers from a [`BufferPass`]
    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 {
    /// Creates a New GpuRenderer.
    ///
    pub fn new(window: GpuWindow, device: GpuDevice) -> Self {
        let buffer_object = StaticVertexBuffer::create_buffer(&device);
        let depth_buffer = window.create_depth_texture(&device);
        let backend = window.adapter.get_info().backend;

        Self {
            window,
            device,
            ibo_stores: SlotMap::with_capacity_and_key(1024),
            //we use a lower amount here since meshes tend to be grouped more and might not always be used.
            vbo_stores: SlotMap::with_capacity_and_key(8),
            layout_storage: LayoutStorage::new(),
            pipeline_storage: PipelineStorage::new(),
            depthbuffer: depth_buffer,
            framebuffer: None,
            frame: None,
            font_sys: FontSystem::new(),
            buffer_object,
            backend,
        }
    }

    /// Returns a reference to [`wgpu::Adapter`].
    ///
    pub fn adapter(&self) -> &wgpu::Adapter {
        self.window.adapter()
    }

    /// Resizes the Window.
    ///
    pub fn resize(&mut self, size: PhysicalSize<u32>) {
        self.window.resize(&self.device, size);
    }

    /// Returns a reference to the Optional [`wgpu::TextureView`]: frame buffer.
    ///
    pub fn frame_buffer(&self) -> &Option<wgpu::TextureView> {
        &self.framebuffer
    }

    /// Returns a reference to [`wgpu::TextureView`].
    ///
    pub fn depth_buffer(&self) -> &wgpu::TextureView {
        &self.depthbuffer
    }

    /// Returns the windows [`PhysicalSize`].
    ///
    pub fn size(&self) -> PhysicalSize<f32> {
        self.window.size
    }

    /// Returns the windows inner [`PhysicalSize`].
    ///
    pub fn inner_size(&self) -> PhysicalSize<u32> {
        self.window.inner_size
    }

    /// Returns a reference to [`wgpu::Surface`].
    ///
    pub fn surface(&self) -> &wgpu::Surface<'_> {
        &self.window.surface
    }

    /// Returns the surfaces [`wgpu::TextureFormat`].
    ///
    pub fn surface_format(&self) -> wgpu::TextureFormat {
        self.window.surface_format
    }

    /// Called to update the Optional Framebuffer with a Buffer we use to render.
    /// Will return weither the frame buffer could have been processed or not.
    /// If not we should skip rendering till we can get a frame buffer.
    ///
    pub fn update(
        &mut self,
        instance: &wgpu::Instance,
        event: &WindowEvent,
    ) -> Result<bool, GraphicsError> {
        let frame = match self.window.update(instance, &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)
    }

    /// Returns a reference to [`Window`].
    ///
    pub fn window(&self) -> &Window {
        &self.window.window
    }

    /// Updates the Internally Stored Depth Buffer.
    ///
    pub fn update_depth_texture(&mut self) {
        self.depthbuffer = self.window.create_depth_texture(&self.device);
    }

    /// Presents the Current frame Buffer to the Window if Some().
    /// If the frame buffer does not Exist will return a Error.
    ///
    pub fn present(&mut self) -> Result<(), GraphicsError> {
        self.framebuffer = None;

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

    /// Returns a reference to [`wgpu::Device`].
    ///
    pub fn device(&self) -> &wgpu::Device {
        &self.device.device
    }

    /// Returns a reference to [`GpuDevice`].
    ///
    pub fn gpu_device(&self) -> &GpuDevice {
        &self.device
    }

    /// Returns a reference to [`wgpu::Queue`].
    ///
    pub fn queue(&self) -> &wgpu::Queue {
        &self.device.queue
    }

    /// Returns a reference to [`FontSystem`].
    ///
    pub fn font_sys(&self) -> &FontSystem {
        &self.font_sys
    }

    /// Returns a mutable reference to [`FontSystem`].
    ///
    pub fn font_sys_mut(&mut self) -> &mut FontSystem {
        &mut self.font_sys
    }

    /// Creates a New [`IBOStore`] with set sizes for Rendering Object Data Storage and
    /// Returns its [`Index`] for Referencing it.
    ///
    pub fn new_ibo_store(&mut self, store_size: usize) -> Index {
        self.ibo_stores.insert(IBOStore::new(store_size))
    }

    /// Creates a New [`VBOStore`] with set sizes for Rendering Object Data Storage and
    /// Returns its [`Index`] for Referencing it.
    ///
    pub fn new_vbo_store(
        &mut self,
        store_size: usize,
        index_size: usize,
    ) -> Index {
        self.vbo_stores
            .insert(VBOStore::new(store_size, index_size))
    }

    /// Creates a New [`IBOStore`] with default sizes for Rendering Object Data Storage and
    /// Returns its [`Index`] for Referencing it.
    ///
    pub fn default_ibo_store(&mut self) -> Index {
        self.ibo_stores.insert(IBOStore::default())
    }

    /// Creates a New [`VBOStore`] with default sizes for Rendering Object Data Storage and
    /// Returns its [`Index`] for Referencing it.
    ///
    pub fn default_vbo_store(&mut self) -> Index {
        self.vbo_stores.insert(VBOStore::default())
    }

    /// Removes a [`IBOStore`] using its [`Index`].
    ///
    pub fn remove_ibo_store(&mut self, index: Index) {
        let _ = self.ibo_stores.remove(index);
    }

    /// Removes a [`VBOStore`] using its [`Index`].
    ///
    pub fn remove_vbo_store(&mut self, index: Index) {
        let _ = self.vbo_stores.remove(index);
    }

    /// Gets a optional reference to [`IBOStore`] using its [`Index`].
    ///
    pub fn get_ibo_store(&self, index: Index) -> Option<&IBOStore> {
        self.ibo_stores.get(index)
    }

    /// Gets a optional reference to [`VBOStore`] using its [`Index`].
    ///
    pub fn get_vbo_store(&self, index: Index) -> Option<&VBOStore> {
        self.vbo_stores.get(index)
    }

    /// Gets a optional mutable reference to [`IBOStore`] using its [`Index`].
    ///
    pub fn get_ibo_store_mut(&mut self, index: Index) -> Option<&mut IBOStore> {
        self.ibo_stores.get_mut(index)
    }

    /// Gets a optional mutable reference to [`VBOStore`] using its [`Index`].
    ///
    pub fn get_vbo_store_mut(&mut self, index: Index) -> Option<&mut VBOStore> {
        self.vbo_stores.get_mut(index)
    }

    /// Creates new BindGroupLayout from Generic K and Returns a Reference Counter to them.
    ///
    pub fn create_layout<K: Layout>(
        &mut self,
        layout: K,
    ) -> Arc<wgpu::BindGroupLayout> {
        self.layout_storage.create_layout(&mut self.device, layout)
    }

    /// Returns a Reference Counter to the layout Or None if it does not yet Exist.
    ///
    pub fn get_layout<K: Layout>(
        &self,
        layout: K,
    ) -> Option<Arc<wgpu::BindGroupLayout>> {
        self.layout_storage.get_layout(layout)
    }

    /// Creates each supported rendering objects pipeline.
    ///
    pub fn create_pipelines(
        &mut self,
        pipelined_enabled: EnabledPipelines,
        surface_format: wgpu::TextureFormat,
    ) {
        if pipelined_enabled.image_pipeline {
            self.pipeline_storage.create_pipeline(
                &mut self.device,
                &mut self.layout_storage,
                surface_format,
                crate::ImageRenderPipeline,
            );
        }

        if pipelined_enabled.anim_image_pipeline {
            self.pipeline_storage.create_pipeline(
                &mut self.device,
                &mut self.layout_storage,
                surface_format,
                crate::AnimImageRenderPipeline,
            );
        }

        if pipelined_enabled.map_pipeline {
            self.pipeline_storage.create_pipeline(
                &mut self.device,
                &mut self.layout_storage,
                surface_format,
                crate::MapRenderPipeline,
            );
        }

        if pipelined_enabled.text_pipeline {
            self.pipeline_storage.create_pipeline(
                &mut self.device,
                &mut self.layout_storage,
                surface_format,
                crate::TextRenderPipeline,
            );
        }

        if pipelined_enabled.mesh2d_pipeline {
            self.pipeline_storage.create_pipeline(
                &mut self.device,
                &mut self.layout_storage,
                surface_format,
                crate::Mesh2DRenderPipeline,
            );
        }

        if pipelined_enabled.light_pipeline {
            self.pipeline_storage.create_pipeline(
                &mut self.device,
                &mut self.layout_storage,
                surface_format,
                crate::LightRenderPipeline,
            );
        }

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

    /// Gets a optional reference of [`wgpu::RenderPipeline`]
    ///
    pub fn get_pipelines<K: PipeLineLayout>(
        &self,
        pipeline: K,
    ) -> Option<&wgpu::RenderPipeline> {
        self.pipeline_storage.get_pipeline(pipeline)
    }
}