wimrend 0.1.0

Wgpu Immediate Mode RENDerer
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
//! # wimrend - WgpuImmediateModeRENDerer
//!
//! This crate provides a simple immediate mode renderer for use in wgpu and winit environments.
//!
//! Adapted from [wgpu tutorial](https://sotrh.github.io/learn-wgpu/), but contains mostly custom
//! code.

use std::collections::BTreeMap;
use std::mem;
use std::sync::Arc;
use wgpu::*;

use nalgebra as na;

pub mod material;
pub mod mesh;
pub mod mesh_manager;
mod multisampler;
pub mod render_pipeline_manager;
pub mod render_state;
pub mod texture;
pub mod vertex;

use material::Material;
use mesh::Mesh;
use mesh_manager::{MeshDescriptor, MeshManager};
use render_pipeline_manager::RenderPipelineManager;
use texture::Texture;

use anyhow::Result;

/// Arc for when hashing the underlying object is too costly.
///
/// This arc essentially compares and hashes the pointer of the object as opposed to the object
/// itself. This becomes very useful when singleton arcs are widely used.
#[repr(transparent)]
struct UniqueArc<T>(Arc<T>);

impl<T> core::ops::Deref for UniqueArc<T> {
    type Target = Arc<T>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<T> PartialEq for UniqueArc<T> {
    fn eq(&self, other: &Self) -> bool {
        Arc::as_ptr(self) == Arc::as_ptr(other)
    }
}

impl<T> Eq for UniqueArc<T> {}

impl<T> Ord for UniqueArc<T> {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        Arc::as_ptr(self).cmp(&Arc::as_ptr(other))
    }
}

impl<T> PartialOrd for UniqueArc<T> {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl<T> From<Arc<T>> for UniqueArc<T> {
    fn from(arc: Arc<T>) -> Self {
        Self(arc)
    }
}

#[repr(C)]
#[derive(Default, Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
pub struct UniformData {
    proj: [f32; 4 * 4],
    resolution: [f32; 4],
}

/// Data bound to all shaders in the form of a uniform buffer.
///
/// This structure contains camera data, as well as other properties to be bound as a uniform
/// buffer.
pub struct UniformObject {
    data: UniformData,
    uniform_buffer: Buffer,
    uniform_bind_group: BindGroup,
    bounds: Option<[f32; 4]>,
}

fn calc_view(pos: na::Point3<f32>, rot: na::UnitQuaternion<f32>) -> na::Matrix4<f32> {
    na::Matrix4::look_at_rh(
        &pos,
        &(pos + rot.transform_vector(&na::Vector3::new(0.0, 1.0, 0.0))),
        &rot.transform_vector(&na::Vector3::new(0.0, 0.0, 1.0)),
    )
}

impl UniformObject {
    fn new(pipeline_manager: &mut RenderPipelineManager) -> Self {
        let uniform_buffer = pipeline_manager.device().create_buffer(&BufferDescriptor {
            label: Some("Uniform Buffer"),
            size: mem::size_of::<UniformData>() as BufferAddress,
            usage: BufferUsages::UNIFORM | BufferUsages::COPY_DST,
            mapped_at_creation: false,
        });

        let uniform_bind_group =
            pipeline_manager
                .device()
                .create_bind_group(&BindGroupDescriptor {
                    layout: pipeline_manager.uniform_bind_group_layout(),
                    entries: &[BindGroupEntry {
                        binding: 0,
                        resource: uniform_buffer.as_entire_binding(),
                    }],
                    label: Some("uniform_bind_group"),
                });

        Self {
            data: Default::default(),
            uniform_buffer,
            uniform_bind_group,
            bounds: None,
        }
    }

    /// Update camera projection matrix.
    ///
    /// # Arguments
    ///
    /// * `proj` - projection matrix.
    /// * `pos` - position of the camera.
    /// * `rot` - rotation of the camera.
    pub fn update_projection(
        &mut self,
        proj: &na::Matrix4<f32>,
        pos: na::Point3<f32>,
        rot: na::UnitQuaternion<f32>,
    ) {
        let view = calc_view(pos, rot);
        #[rustfmt::skip]
        pub const OPENGL_TO_WGPU_MATRIX: na::Matrix4<f32> = na::matrix![
            1.0, 0.0, 0.0, 0.0;
            0.0, 1.0, 0.0, 0.0;
            0.0, 0.0, 0.5, 0.0;
            0.0, 0.0, 0.5, 1.0
        ];
        let mat = OPENGL_TO_WGPU_MATRIX * proj * view;
        self.data.proj.copy_from_slice(mat.as_slice());
    }

    /// Update viewport bounds.
    ///
    /// # Arguments
    ///
    /// * `x` - horizontal start of the viewport.
    /// * `y` - vertical start of the viewport.
    /// * `w` - width of the viewport.
    /// * `h` - height of the viewport.
    pub fn update_bounds(&mut self, x: f32, y: f32, w: f32, h: f32) {
        self.bounds = Some([x, y, w, h]);
    }

    /// Get screen resolution.
    pub fn resolution(&self) -> [f32; 2] {
        [self.data.resolution[0], self.data.resolution[1]]
    }

    fn update_internal(&mut self, screen: &SurfaceConfiguration) {
        self.data.resolution[0] = screen.width as f32;
        self.data.resolution[1] = screen.height as f32;
    }

    fn update_buffers(&self, queue: &Queue) {
        queue.write_buffer(&self.uniform_buffer, 0, bytemuck::bytes_of(&self.data));
    }
}

type TextureMap = BTreeMap<Option<UniqueArc<Texture>>, MeshMap>;
type MeshMap = BTreeMap<UniqueArc<MeshDescriptor>, Vec<RenderObject>>;

pub struct Renderer {
    device: Arc<Device>,
    queue: Arc<Queue>,
    pipeline_manager: RenderPipelineManager,
    mesh_manager: MeshManager,
    obj_count: usize,
    objects: BTreeMap<UniqueArc<RenderPipeline>, TextureMap>,
    instance_buffer: Option<(usize, Buffer)>,
    line_material: Arc<Material>,
    camera: UniformObject,
}

impl Renderer {
    /// Create a new renderer.
    ///
    /// # Arguments
    ///
    /// * `device` - device to render to.
    /// * `queue` - queue of write operations.
    /// * `surface_format` - format of all textures.
    pub fn new(
        device: Arc<Device>,
        queue: Arc<Queue>,
        surface_format: TextureFormat,
        msaa_samples: u32,
    ) -> Self {
        let mut pipeline_manager =
            RenderPipelineManager::new(device.clone(), surface_format, msaa_samples);

        Self {
            mesh_manager: MeshManager::new(device.clone()),
            camera: UniformObject::new(&mut pipeline_manager),
            line_material: Material::line(&mut pipeline_manager).into(),
            pipeline_manager,
            device,
            queue,
            obj_count: 0,
            objects: Default::default(),
            instance_buffer: None,
        }
    }

    fn reset(&mut self) {
        self.obj_count = 0;
        self.objects.clear();
        self.camera.bounds = None;
    }

    /// Update all buffers before rendering.
    pub fn update_buffers(&mut self) {
        self.camera.update_buffers(&self.queue);

        if self.instance_buffer.as_ref().map(|(s, _)| *s).unwrap_or(0) < self.obj_count {
            let buffer = self.device.create_buffer(&BufferDescriptor {
                label: Some("Camera Buffer"),
                size: RenderObject::desc().array_stride * self.obj_count as BufferAddress,
                usage: BufferUsages::VERTEX | BufferUsages::COPY_DST,
                mapped_at_creation: false,
            });

            self.instance_buffer = Some((self.obj_count, buffer));
        }

        if let Some((_, buf)) = &self.instance_buffer {
            let mut offset = 0;
            for o in self
                .objects
                .values()
                .flat_map(|m| m.values().flat_map(|m| m.values().flat_map(|m| m.iter())))
            {
                offset = o.write_buffer(buf, offset, &self.queue);
            }
        }
    }

    /// Render all objects.
    ///
    /// # Arguments
    ///
    /// * `render_pass` - render pass to draw in.
    pub fn render<'a>(&'a self, render_pass: &mut wgpu::RenderPass<'a>) {
        if let Some([x, y, w, h]) = self.camera.bounds {
            render_pass.set_viewport(x, y, w, h, 0f32, 1f32);
        }

        // First setup any buffers used.
        self.mesh_manager.set_buffers(render_pass);

        if let Some((_, ibuf)) = &self.instance_buffer {
            render_pass.set_vertex_buffer(1, ibuf.slice(..));
        }

        let mut count = 0;

        // Instanced draw loop.
        for (pipeline, objects) in &self.objects {
            // Pipeline for all the objects following.
            render_pass.set_pipeline(pipeline);

            // Bind the uniform buffer.
            render_pass.set_bind_group(0, &self.camera.uniform_bind_group, &[]);

            for (texture, objects) in objects {
                // Bind a texture if there is any.
                if let Some(texture) = texture {
                    render_pass.set_bind_group(1, &texture.bind_group, &[]);
                }
                for (mesh, objects) in objects {
                    // Perform instanced draw of all objects with matching material and mesh.
                    let c2 = objects.len() as u32;
                    render_pass.draw_indexed(
                        mesh.indices.clone(),
                        mesh.base_vertex,
                        count..(count + c2),
                    );
                    count += c2;
                }
            }
        }
    }

    /// Get mutable reference to underlying camera.
    pub fn uniform_mut(&mut self) -> &mut UniformObject {
        &mut self.camera
    }

    /// Get mutable reference to underlying pipeline manager.
    ///
    /// This manager needs to be passed upon creating materials.
    pub fn pipeline_manager_mut(&mut self) -> &mut RenderPipelineManager {
        &mut self.pipeline_manager
    }

    /// Create a texture from RGBA image.
    ///
    /// # Arguments
    ///
    /// * `device` - Device to create the texture on.
    /// * `queue` - Command queue to write the texture through.
    /// * `label` - Optional identification label.
    /// * `rgba` - Image data.
    /// * `height` - Height of the texture.
    pub fn texture_from_rgba(
        &self,
        label: Option<&'static str>,
        rgba: &[u8],
        height: usize,
    ) -> Result<Texture> {
        Texture::from_rgba_frame(
            &self.device,
            &self.queue,
            self.pipeline_manager.texture_bind_group_layout(),
            label,
            rgba,
            height,
        )
    }

    /// Draw an object in scene.
    ///
    /// # Arguments
    ///
    /// * `mesh` - 3D mesh to draw.
    /// * `transform` - Combined translation, rotation and scale transformation.
    /// * `colour` - RGBA colour of this object instance.
    /// * `material` - Material used by the object.
    pub fn obj_raw(
        &mut self,
        mesh: Arc<Mesh>,
        transform: na::Matrix4<f32>,
        colour: na::Vector4<f32>,
        material: Arc<Material>,
    ) {
        let mesh = self.mesh_manager.descriptor(mesh);

        self.obj_count += 1;

        self.objects
            .entry(material.pipeline().clone().into())
            .or_default()
            .entry(material.texture().clone().map(<_>::into))
            .or_default()
            .entry(mesh.into())
            .or_default()
            .push(RenderObject { transform, colour });
    }

    /// Draw an object in scene.
    ///
    /// # Arguments
    ///
    /// * `mesh` - 3D mesh to draw.
    /// * `position` - Position of the object.
    /// * `rotation` - Rotation of the object.
    /// * `scale` - Scale of the object.
    /// * `colour` - RGBA colour of this object instance.
    /// * `material` - Material used by the object.
    pub fn obj(
        &mut self,
        mesh: Arc<Mesh>,
        position: na::Point3<f32>,
        rotation: na::UnitQuaternion<f32>,
        scale: na::Vector3<f32>,
        colour: na::Vector4<f32>,
        material: Arc<Material>,
    ) {
        let tr = na::Matrix4::new_translation(&position.coords);
        let rot = rotation.to_homogeneous();
        let scale = na::matrix![
            scale.x, 0.0, 0.0, 0.0;
            0.0, scale.y, 0.0, 0.0;
            0.0, 0.0, scale.z, 0.0;
            0.0, 0.0, 0.0, 1.0
        ];
        self.obj_raw(mesh, tr * rot * scale, colour, material)
    }

    /// Draw a line in scene.
    ///
    /// # Arguments
    ///
    /// * `start` - starting point of the line.
    /// * `end` - ending point of the line.
    /// * `thickness` - thickness of line in pixels.
    /// * `colour` - RGBA colour of the line.
    pub fn line(
        &mut self,
        start: na::Point3<f32>,
        end: na::Point3<f32>,
        thickness: f32,
        colour: na::Vector4<f32>,
    ) {
        // The matrix in the line shader is merely start and end positions,
        // not an actual transformation.

        let transform = na::matrix![
            start.x, start.y, start.z, 1.0;
            end.x, end.y, end.z, 1.0;
            thickness, 0.0, 0.0, 0.0;
            0.0, 0.0, 0.0, 0.0
        ]
        .transpose();

        self.obj_raw(Mesh::quad(), transform, colour, self.line_material.clone());
    }
}

struct RenderObject {
    pub transform: na::Matrix4<f32>,
    pub colour: na::Vector4<f32>,
}

impl RenderObject {
    pub fn desc<'a>() -> VertexBufferLayout<'a> {
        VertexBufferLayout {
            array_stride: mem::size_of::<[f32; 4 * 5]>() as BufferAddress,
            step_mode: VertexStepMode::Instance,
            attributes: &[
                VertexAttribute {
                    offset: 0,
                    shader_location: 4,
                    format: VertexFormat::Float32x4,
                },
                VertexAttribute {
                    offset: mem::size_of::<[f32; 4]>() as BufferAddress,
                    shader_location: 5,
                    format: VertexFormat::Float32x4,
                },
                VertexAttribute {
                    offset: mem::size_of::<[f32; 4 * 2]>() as BufferAddress,
                    shader_location: 6,
                    format: VertexFormat::Float32x4,
                },
                VertexAttribute {
                    offset: mem::size_of::<[f32; 4 * 3]>() as BufferAddress,
                    shader_location: 7,
                    format: VertexFormat::Float32x4,
                },
                VertexAttribute {
                    offset: mem::size_of::<[f32; 4 * 4]>() as BufferAddress,
                    shader_location: 8,
                    format: VertexFormat::Float32x4,
                },
            ],
        }
    }

    pub fn write_buffer(
        &self,
        buffer: &Buffer,
        offset: BufferAddress,
        queue: &Queue,
    ) -> BufferAddress {
        queue.write_buffer(
            buffer,
            offset,
            bytemuck::cast_slice(self.transform.as_slice()),
        );
        queue.write_buffer(
            buffer,
            offset + mem::size_of::<[f32; 4 * 4]>() as BufferAddress,
            bytemuck::cast_slice(self.colour.as_slice()),
        );
        offset + mem::size_of::<[f32; 4 * 5]>() as BufferAddress
    }
}