blue_engine_core 0.10.0

USE blue_engine THIS IS FOR INTERNAL USE
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
use crate::{
    CameraContainer, ObjectStorage, PipelineData, WindowSize,
    prelude::{ShaderSettings, TextureData},
    utils::default_resources::{DEFAULT_COLOR, DEFAULT_SHADER, DEFAULT_TEXTURE},
};

/// Main renderer class. this will contain all methods and data related to the renderer
#[derive(Debug)]
pub struct Renderer {
    /// A [`wgpu::Surface`] represents a platform-specific surface
    /// (e.g. a window) onto which rendered images may be presented.
    pub surface: Option<wgpu::Surface<'static>>,
    /// Context for all of the gpu objects
    pub instance: wgpu::Instance,
    /// Handle to a physical graphics and/or compute device.
    #[allow(unused)]
    pub adapter: wgpu::Adapter,
    /// Open connection to a graphics and/or compute device.
    pub device: wgpu::Device,
    /// Handle to a command queue on a device.
    pub queue: wgpu::Queue,
    /// Describes a [`wgpu::Surface`]
    pub config: wgpu::SurfaceConfiguration,
    /// The size of the window
    pub size: WindowSize,
    /// The texture bind group layout
    pub texture_bind_group_layout: wgpu::BindGroupLayout,
    /// The uniform bind group layout
    pub default_uniform_bind_group_layout: wgpu::BindGroupLayout,
    /// The depth buffer, used to render object depth
    pub depth_buffer: (wgpu::Texture, wgpu::TextureView, wgpu::Sampler),
    /// The default data used within the renderer
    pub default_data: Option<(crate::Textures, crate::Shaders, crate::UniformBuffers)>,
    /// The camera used in the engine
    pub camera: Option<crate::UniformBuffers>,
    /// Background clear color
    pub clear_color: wgpu::Color,
    /// Scissor cut section of the screen to render to
    /// (x, y, width, height)
    pub scissor_rect: Option<(u32, u32, u32, u32)>,
    /// The texture data that holds data for the headless mode
    #[cfg(feature = "headless")]
    pub headless_texture_data: Vec<u8>,
}
unsafe impl Sync for Renderer {}
unsafe impl Send for Renderer {}

impl Renderer {
    /// Creates a new renderer.
    pub(crate) async fn new(
        size: WindowSize,
        settings: crate::EngineSettings,
    ) -> Result<Self, crate::error::Error> {
        // The instance is a handle to our GPU
        let instance = wgpu::Instance::new(&wgpu::InstanceDescriptor {
            backends: settings.backends,
            ..Default::default()
        });

        match instance
            .request_adapter(&wgpu::RequestAdapterOptions {
                power_preference: settings.power_preference,
                compatible_surface: None,
                force_fallback_adapter: false,
            })
            .await
        {
            Ok(adapter) => {
                let (device, queue) = adapter
                    .request_device(&wgpu::DeviceDescriptor {
                        label: Some("Device"),
                        required_features: settings.features,
                        required_limits: settings.limits,
                        memory_hints: if settings.power_preference
                            == wgpu::PowerPreference::HighPerformance
                        {
                            wgpu::MemoryHints::Performance
                        } else {
                            wgpu::MemoryHints::MemoryUsage
                        },
                        trace: wgpu::Trace::Off,
                        ..Default::default()
                    })
                    .await?;

                let texture_format = wgpu::TextureFormat::Bgra8UnormSrgb;

                #[cfg(target_os = "android")]
                let texture_format = wgpu::TextureFormat::Rgba8UnormSrgb;

                let config = wgpu::SurfaceConfiguration {
                    usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
                    format: texture_format, //wgpu::TextureFormat::Bgra8UnormSrgb,
                    #[cfg(target_os = "android")]
                    width: 1080,
                    #[cfg(not(feature = "android"))]
                    width: size.0,
                    #[cfg(target_os = "android")]
                    height: 2300,
                    #[cfg(not(target_os = "android"))]
                    height: size.1,
                    #[cfg(target_os = "android")]
                    present_mode: wgpu::PresentMode::Mailbox,
                    #[cfg(not(target_os = "android"))]
                    present_mode: settings.present_mode,
                    alpha_mode: settings.alpha_mode,
                    view_formats: vec![texture_format],
                    desired_maximum_frame_latency: settings.desired_maximum_frame_latency,
                };

                let texture_bind_group_layout =
                    device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
                        entries: &[
                            wgpu::BindGroupLayoutEntry {
                                binding: 0,
                                visibility: wgpu::ShaderStages::FRAGMENT,
                                ty: wgpu::BindingType::Texture {
                                    sample_type: wgpu::TextureSampleType::Float {
                                        filterable: true,
                                    },
                                    view_dimension: wgpu::TextureViewDimension::D2,
                                    multisampled: false,
                                },
                                count: None,
                            },
                            wgpu::BindGroupLayoutEntry {
                                binding: 1,
                                visibility: wgpu::ShaderStages::FRAGMENT,
                                ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering), //comparison: false,
                                // filtering: true,
                                count: None,
                            },
                        ],
                        label: Some("texture_bind_group_layout"),
                    });

                let default_uniform_bind_group_layout =
                    device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
                        label: Some("uniform dynamic bind group layout"),
                        entries: &[wgpu::BindGroupLayoutEntry {
                            binding: 0,
                            visibility: wgpu::ShaderStages::VERTEX | wgpu::ShaderStages::FRAGMENT,
                            ty: wgpu::BindingType::Buffer {
                                ty: wgpu::BufferBindingType::Uniform,
                                has_dynamic_offset: false,
                                min_binding_size: None,
                            },
                            count: None,
                        }],
                    });

                let depth_buffer = Renderer::build_depth_buffer("Depth Buffer", &device, &config);

                let mut renderer = Self {
                    instance,
                    adapter,
                    surface: None,
                    device,
                    queue,
                    config,
                    size,

                    texture_bind_group_layout,
                    default_uniform_bind_group_layout,
                    depth_buffer,

                    default_data: None,
                    camera: None,
                    clear_color: wgpu::Color::BLACK,
                    scissor_rect: None,

                    #[cfg(feature = "headless")]
                    headless_texture_data: Vec::<u8>::with_capacity((size.0 * size.1) as usize * 4),
                };

                renderer.build_default_data();

                Ok(renderer)
            }
            Err(e) => Err(crate::error::Error::AdapterNotFound(e)),
        }
    }

    pub(crate) fn build_default_data(&mut self) {
        if let Ok(default_texture) = self.build_texture(
            "Default Texture",
            TextureData::Bytes(DEFAULT_TEXTURE.to_vec()),
            crate::prelude::TextureMode::Clamp,
            //crate::prelude::TextureFormat::PNG
        ) {
            let default_uniform = self.build_uniform_buffer(&vec![
                self.build_uniform_buffer_part("Transformation Matrix", crate::Matrix4::IDENTITY),
                self.build_uniform_buffer_part("Color", DEFAULT_COLOR),
            ]);

            let default_shader = self.build_shader(
                "Default Shader",
                DEFAULT_SHADER.to_string(),
                Some(&default_uniform.1),
                ShaderSettings::default(),
            );

            self.default_data = Some((default_texture, default_shader, default_uniform.0));
        } else {
            eprintln!("Could not build the default texture, there may be something wrong!");
            self.default_data = None;
        }
    }

    /// Resize the window.
    #[cfg(all(not(feature = "headless"), feature = "window"))]
    pub(crate) fn resize(&mut self, new_size: WindowSize) {
        // check if new_size is non-zero
        if new_size.0 != 0 && new_size.1 != 0 {
            self.size = new_size;
            self.config.width = new_size.0;
            self.config.height = new_size.1;
            #[cfg(not(target_os = "android"))]
            if let Some(surface) = self.surface.as_ref() {
                surface.configure(&self.device, &self.config);
                self.depth_buffer =
                    Self::build_depth_buffer("Depth Buffer", &self.device, &self.config);
            }
        }
    }

    /// Render the scene. Returns the command encoder, the texture view, and the surface texture.
    pub(crate) fn pre_render(
        &mut self,
        objects: &ObjectStorage,
        window_size: WindowSize,
        camera: &CameraContainer,
    ) -> Result<
        Option<(
            wgpu::CommandEncoder,
            wgpu::TextureView,
            Option<wgpu::SurfaceTexture>,
            Option<(wgpu::Buffer, wgpu::Texture)>,
        )>,
        wgpu::SurfaceError,
    > {
        #[cfg(not(feature = "headless"))]
        let surface = if let Some(ref surface) = self.surface {
            surface
        } else {
            return Ok(None);
        };
        #[cfg(not(feature = "headless"))]
        let frame = if let Ok(frame) = surface.get_current_texture() {
            frame
        } else {
            return Ok(None);
        };

        let mut encoder = self
            .device
            .create_command_encoder(&wgpu::CommandEncoderDescriptor {
                label: Some("Render Encoder"),
            });

        #[cfg(feature = "headless")]
        let render_target = self.device.create_texture(&wgpu::TextureDescriptor {
            size: wgpu::Extent3d {
                width: self.config.width,
                height: self.config.height,
                depth_or_array_layers: 1,
            },
            mip_level_count: 1,
            sample_count: 1,
            dimension: wgpu::TextureDimension::D2,
            format: self.config.format,
            usage: wgpu::TextureUsages::COPY_SRC | wgpu::TextureUsages::RENDER_ATTACHMENT,
            label: None,
            view_formats: &[self.config.format],
        });

        // ? There might be a way to enable both headless and normal rendering,
        // ? However the cost of it can increase a lot
        #[cfg(feature = "headless")]
        let view = render_target.create_view(&wgpu::TextureViewDescriptor::default());
        #[cfg(not(feature = "headless"))]
        let view = frame
            .texture
            .create_view(&wgpu::TextureViewDescriptor::default());

        #[cfg(feature = "headless")]
        let headless_output_staging_buffer = self.device.create_buffer(&wgpu::BufferDescriptor {
            label: None,
            size: self.headless_texture_data.capacity() as u64,
            usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::MAP_READ,
            mapped_at_creation: false,
        });

        let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
            label: Some("Render pass"),
            color_attachments: &[Some(wgpu::RenderPassColorAttachment {
                view: &view,
                resolve_target: None,
                ops: wgpu::Operations {
                    load: wgpu::LoadOp::Clear(self.clear_color),
                    store: wgpu::StoreOp::Store,
                },
                depth_slice: None,
            })],
            depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
                view: &self.depth_buffer.1,
                depth_ops: Some(wgpu::Operations {
                    load: wgpu::LoadOp::Clear(1.0),
                    store: wgpu::StoreOp::Store,
                }),
                stencil_ops: None,
            }),
            timestamp_writes: None,
            occlusion_query_set: None,
        });

        if let Some(scissor_rect) = self.scissor_rect {
            // check if scissor bounds are smaller than the window
            if scissor_rect.0 + scissor_rect.2 < window_size.0
                && scissor_rect.1 + scissor_rect.3 < window_size.1
            {
                render_pass.set_scissor_rect(
                    scissor_rect.0,
                    scissor_rect.1,
                    scissor_rect.2,
                    scissor_rect.3,
                );
            }
        }

        if let Some(default_data) = self.default_data.as_ref() {
            render_pass.set_bind_group(0, &default_data.0, &[]);
            render_pass.set_pipeline(&default_data.1);
        }

        // sort the object list in descending render order
        // ! There needs to be a better way for this, to not iterate twice
        let mut object_list = objects.iter().collect::<Vec<_>>();
        object_list.sort_by(|(_, a), (_, b)| a.render_order.cmp(&b.render_order).reverse());

        for (_, i) in object_list {
            if let Some(camera_data) = i.camera_effect.as_ref() {
                if let Some(camera) = camera.get(camera_data.as_ref()) {
                    render_pass.set_bind_group(1, &camera.uniform_data, &[]);
                }
            } else {
                if let Some(main_camera) = camera.get("main") {
                    render_pass.set_bind_group(1, &main_camera.uniform_data, &[]);
                }
            }

            if i.is_visible {
                let vertex_buffer = get_pipeline_vertex_buffer(&i.pipeline.vertex_buffer, objects);
                let shader = get_pipeline_shader(&i.pipeline.shader, objects);
                let texture = get_pipeline_texture(&i.pipeline.texture, objects);
                let uniform = get_pipeline_uniform_buffer(&i.pipeline.uniform, objects);

                // vertex
                if let Some(vertex_buffer) = vertex_buffer {
                    render_pass.set_vertex_buffer(0, vertex_buffer.vertex_buffer.slice(..));
                    render_pass.set_vertex_buffer(1, i.instance_buffer.slice(..));
                    render_pass.set_index_buffer(
                        vertex_buffer.index_buffer.slice(..),
                        #[cfg(not(feature = "u32"))]
                        wgpu::IndexFormat::Uint16,
                        #[cfg(feature = "u32")]
                        wgpu::IndexFormat::Uint32,
                    );

                    // shader
                    if let Some(shader) = shader {
                        render_pass.set_pipeline(shader);
                    }
                    // texture
                    if let Some(texture) = texture {
                        render_pass.set_bind_group(0, texture, &[]);
                    }
                    // uniform
                    if let Some(Some(uniform)) = uniform {
                        render_pass.set_bind_group(2, uniform, &[]);
                    }
                    render_pass.draw_indexed(0..vertex_buffer.length, 0, 0..i.instances.len() as _);
                }
            }
        }
        drop(render_pass);

        Ok(Some((
            encoder,
            view,
            #[cfg(feature = "headless")]
            None,
            #[cfg(not(feature = "headless"))]
            Some(frame),
            #[cfg(feature = "headless")]
            Some((headless_output_staging_buffer, render_target)),
            #[cfg(not(feature = "headless"))]
            None,
        )))
    }

    /// Render the scene.
    pub(crate) fn render(
        &mut self,
        encoder: wgpu::CommandEncoder,
        _frame: Option<wgpu::SurfaceTexture>,
        _headless: Option<(wgpu::Buffer, wgpu::Texture)>,
    ) {
        #[cfg(feature = "headless")]
        {
            #[allow(clippy::expect_used)]
            let (output_staging_buffer, render_target) =
                _headless.expect("Error unpacking headless content. This should not error!");

            let mut encoder = encoder;

            #[cfg(feature = "headless")]
            encoder.copy_texture_to_buffer(
                wgpu::TexelCopyTextureInfo {
                    texture: &render_target,
                    mip_level: 0,
                    origin: wgpu::Origin3d::ZERO,
                    aspect: wgpu::TextureAspect::All,
                },
                wgpu::TexelCopyBufferInfo {
                    buffer: &output_staging_buffer,
                    layout: wgpu::TexelCopyBufferLayout {
                        offset: 0,
                        // This needs to be a multiple of 256. Normally we would need to pad
                        // it but we here know it will work out anyways.
                        bytes_per_row: Some(self.config.width * 4),
                        rows_per_image: Some(self.config.height),
                    },
                },
                wgpu::Extent3d {
                    width: self.config.width,
                    height: self.config.height,
                    depth_or_array_layers: 1,
                },
            );

            // submit will accept anything that implements IntoIter
            self.queue.submit(Some(encoder.finish()));

            pollster::block_on(async {
                let buffer_slice = output_staging_buffer.slice(..);
                let (sender, receiver) = std::sync::mpsc::channel();
                buffer_slice.map_async(wgpu::MapMode::Read, move |r| sender.send(r).unwrap());
                self.device
                    .poll(wgpu::PollType::wait_indefinitely())
                    .unwrap();
                receiver.recv().unwrap().unwrap();
                {
                    let view = buffer_slice.get_mapped_range();
                    self.headless_texture_data.extend_from_slice(&view[..]);
                }
                output_staging_buffer.unmap();
            });
        }

        #[cfg(not(feature = "headless"))]
        if let Some(frame) = _frame {
            // submit will accept anything that implements IntoIter
            self.queue.submit(Some(encoder.finish()));

            frame.present();
        }
    }

    /// Sets the background color
    pub fn set_clear_color(&mut self, r: f64, g: f64, b: f64, a: f64) {
        self.clear_color = wgpu::Color { r, g, b, a }
    }
}

// =========================== Extract Pipeline Data ===========================
macro_rules! gen_pipeline {
    ($function_name:ident, $buffer_type:ty, $buffer_field:ident) => {
        fn $function_name<'a>(
            data: &'a PipelineData<$buffer_type>,
            objects: &'a ObjectStorage,
        ) -> Option<&'a $buffer_type> {
            match data {
                PipelineData::Copy(object_id) => {
                    let data = objects.get(object_id);
                    if let Some(data) = data {
                        $function_name(&data.pipeline.$buffer_field, objects)
                    } else {
                        None
                    }
                }
                PipelineData::Data(data) => Some(data),
            }
        }
    };
}

gen_pipeline!(
    get_pipeline_vertex_buffer,
    crate::VertexBuffers,
    vertex_buffer
);
gen_pipeline!(get_pipeline_shader, crate::Shaders, shader);
gen_pipeline!(get_pipeline_texture, crate::Textures, texture);

/// Get the pipeline uniform_buffer.
fn get_pipeline_uniform_buffer<'a>(
    data: &'a PipelineData<Option<crate::UniformBuffers>>,
    objects: &'a ObjectStorage,
) -> Option<&'a Option<crate::UniformBuffers>> {
    match data {
        PipelineData::Copy(object_id) => {
            let data = objects.get(object_id);
            if let Some(data) = data {
                get_pipeline_uniform_buffer(&data.pipeline.uniform, objects)
            } else {
                None
            }
        }
        PipelineData::Data(data) => Some(data),
    }
}