ambient_app 0.2.1

Ambient app implementation. Host-only.
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
use std::sync::Arc;

use ambient_core::{asset_cache, gpu, main_scene, ui_scene, window::window_physical_size};
use ambient_ecs::{components, query, FrameEvent, System, SystemGroup, World};
use ambient_gizmos::render::GizmoRenderer;
use ambient_gpu::{
    blit::{Blitter, BlitterKey},
    gpu::Gpu,
    shader_module::DEPTH_FORMAT,
    texture::{Texture, TextureView},
};
use ambient_renderer::{renderer_stats, RenderTarget, Renderer, RendererConfig, RendererTarget};
use ambient_std::{asset_cache::SyncAssetKeyExt, color::Color};
use ambient_ui_native::app_background_color;
use glam::{uvec2, UVec2};
use parking_lot::Mutex;
use wgpu::FilterMode;
use winit::{
    dpi::PhysicalSize,
    event::{Event, WindowEvent},
};

components!("app_renderers", {
    ui_renderer: Arc<Mutex<UIRender>>,
    examples_renderer: Arc<Mutex<ExamplesRender>>,
});

pub fn systems() -> SystemGroup<Event<'static, ()>> {
    SystemGroup::new(
        "app_renderers",
        vec![
            query(ui_renderer()).to_system(|q, world, qs, event| {
                for (_, ui_render) in q.collect_cloned(world, qs) {
                    let mut ui_render = ui_render.lock();
                    match &event {
                        Event::WindowEvent {
                            event: WindowEvent::Resized(size),
                            ..
                        } => ui_render.resize(size),
                        Event::WindowEvent {
                            event: WindowEvent::ScaleFactorChanged { new_inner_size, .. },
                            ..
                        } => {
                            ui_render.resize(new_inner_size);
                        }
                        _ => {}
                    }
                    let cleared = matches!(event, Event::MainEventsCleared);
                    if cleared {
                        ui_render.render(world);
                    }
                }
            }),
            query(examples_renderer()).to_system(|q, world, qs, event| {
                for (_, examples_render) in q.collect_cloned(world, qs) {
                    let mut examples_render = examples_render.lock();
                    match event {
                        Event::WindowEvent {
                            event: WindowEvent::Resized(size),
                            ..
                        } => examples_render.resize(size),
                        Event::MainEventsCleared => {
                            examples_render.run(world, &FrameEvent);
                        }
                        _ => {}
                    }
                }
            }),
        ],
    )
}

pub struct ExamplesRender {
    gpu: Arc<Gpu>,
    main: Option<Renderer>,
    ui: Option<Renderer>,
    blit: Arc<Blitter>,
    render_target: RenderTarget,
    size: UVec2,
}

impl ExamplesRender {
    pub fn new(world: &mut World, ui: bool, main: bool) -> Self {
        let gpu = world.resource(gpu()).clone();
        let assets = world.resource(asset_cache()).clone();
        world
            .add_component(world.resource_entity(), renderer_stats(), "".to_string())
            .unwrap();
        let wind_size = *world.resource(ambient_core::window::window_physical_size());

        tracing::debug!("Creating render target");
        let render_target = RenderTarget::new(gpu.clone(), wind_size, None);

        tracing::debug!("Creating self");

        let is_srgb = gpu.swapchain_format().is_srgb();
        let gamma_correction = if !is_srgb {
            tracing::info!(
                "Output format is not in srgb colorspace. Applying manual gamma correction"
            );
            Some(2.2)
        } else {
            None
        };

        Self {
            main: if main {
                tracing::debug!("Creating renderer");
                let mut renderer = Renderer::new(
                    world,
                    world.resource(asset_cache()).clone(),
                    RendererConfig {
                        scene: main_scene(),
                        shadows: true,
                        ..Default::default()
                    },
                );

                tracing::debug!("Creating gizmo renderer");
                renderer.post_transparent = Some(Box::new(GizmoRenderer::new(&assets)));
                Some(renderer)
            } else {
                None
            },
            ui: if ui {
                Some(Renderer::new(
                    world,
                    world.resource(asset_cache()).clone(),
                    RendererConfig {
                        scene: ui_scene(),
                        shadows: false,
                        ..Default::default()
                    },
                ))
            } else {
                None
            },
            blit: BlitterKey {
                format: gpu.swapchain_format().into(),
                min_filter: FilterMode::Nearest,
                gamma_correction,
            }
            .get(&world.resource(asset_cache()).clone()),
            render_target,
            gpu,
            size: wind_size,
        }
    }
    fn resize(&mut self, size: &PhysicalSize<u32>) {
        self.size = uvec2(size.width, size.height);

        if size.width > 0 && size.height > 0 {
            self.render_target =
                RenderTarget::new(self.gpu.clone(), uvec2(size.width, size.height), None);
        }
    }

    pub fn dump_to_tmp_file(&self) {
        std::fs::create_dir_all("tmp").unwrap();
        let mut f = std::fs::File::create("tmp/renderer.txt").expect("Unable to create file");
        self.dump(&mut f);
        tracing::info!("Wrote renderer to tmp/renderer.txt");
    }
    #[allow(dead_code)]
    pub fn n_entities(&self) -> usize {
        self.main.as_ref().map(|x| x.n_entities()).unwrap_or(0)
            + self.ui.as_ref().map(|x| x.n_entities()).unwrap_or(0)
    }
    pub fn stats(&self) -> String {
        if let Some(main) = &self.main {
            main.stats()
        } else {
            String::new()
        }
    }
    pub fn dump(&self, f: &mut dyn std::io::Write) {
        if let Some(main) = &self.main {
            writeln!(f, "## MAIN ##").unwrap();
            main.dump(f);
        }
        if let Some(ui) = &self.ui {
            writeln!(f, "## UI ##").unwrap();
            ui.dump(f);
        }
    }
}

impl std::fmt::Debug for ExamplesRender {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Renderer").finish()
    }
}

impl System for ExamplesRender {
    fn run(&mut self, world: &mut World, _: &FrameEvent) {
        // tracing::info!("ExamplesRenderer");
        ambient_profiling::scope!("Renderers.run");
        let mut encoder = self
            .gpu
            .device
            .create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
        let mut post_submit = Vec::new();

        if let Some(main) = &mut self.main {
            ambient_profiling::scope!("Main");
            main.render(
                world,
                &mut encoder,
                &mut post_submit,
                RendererTarget::Target(&self.render_target),
                Some(Color::rgba(0.0, 0., 0.0, 1.)),
            );
        }

        if let Some(ui) = &mut self.ui {
            // tracing::info!("Drawing UI");
            ambient_profiling::scope!("UI");
            ui.render(
                world,
                &mut encoder,
                &mut post_submit,
                RendererTarget::Target(&self.render_target),
                if self.main.is_some() {
                    None
                } else {
                    Some(app_background_color())
                },
            );
        }

        if let Some(surface) = &self.gpu.surface {
            if self.size.x > 0 && self.size.y > 0 {
                let frame = {
                    ambient_profiling::scope!("Get swapchain texture");
                    match surface.get_current_texture() {
                        Ok(v) => v,
                        // Reconfigure the surface if lost
                        Err(wgpu::SurfaceError::Lost) => {
                            tracing::warn!("Surface lost");
                            self.gpu.resize(PhysicalSize {
                                width: self.size.x,
                                height: self.size.y,
                            });
                            return;
                        }
                        // The system is out of memory, we should probably quit
                        Err(wgpu::SurfaceError::OutOfMemory) => panic!("Out of memory"),
                        // All other errors (Outdated, Timeout) should be resolved by the next frame
                        Err(err) => {
                            tracing::warn!("{err:?}");
                            return;
                        }
                    }
                };
                let frame_view = frame
                    .texture
                    .create_view(&wgpu::TextureViewDescriptor::default());
                self.blit.run(
                    &mut encoder,
                    &self.render_target.color_buffer_view,
                    &frame_view,
                );

                {
                    ambient_profiling::scope!("Submit");
                    self.gpu.queue.submit(Some(encoder.finish()));
                }
                {
                    ambient_profiling::scope!("Present");
                    frame.present();
                }
            } else {
                ambient_profiling::scope!("Submit");
                self.gpu.queue.submit(Some(encoder.finish()));
            }
        } else {
            {
                ambient_profiling::scope!("Submit");
                self.gpu.queue.submit(Some(encoder.finish()));
            }
        }

        for action in post_submit.into_iter() {
            action();
        }

        world
            .set(world.resource_entity(), renderer_stats(), self.stats())
            .unwrap();
    }
}

pub struct UIRender {
    gpu: Arc<Gpu>,
    ui_renderer: Renderer,
    depth_buffer_view: Arc<TextureView>,
    normals_view: Arc<TextureView>,
}

impl UIRender {
    pub fn new(world: &mut World) -> Self {
        let gpu = world.resource(gpu()).clone();
        let size = *world.resource(window_physical_size());

        let depth_buffer = Arc::new(Self::create_depth_buffer(
            gpu.clone(),
            &PhysicalSize::new(size.x, size.y),
        ));

        let normals = Arc::new(Texture::new(
            gpu.clone(),
            &wgpu::TextureDescriptor {
                label: Some("RenderTarget.depth_buffer"),
                size: wgpu::Extent3d {
                    width: size.x,
                    height: size.y,
                    depth_or_array_layers: 1,
                },
                mip_level_count: 1,
                sample_count: 1,
                dimension: wgpu::TextureDimension::D2,
                format: wgpu::TextureFormat::Rgba8Snorm,
                usage: wgpu::TextureUsages::RENDER_ATTACHMENT
                    | wgpu::TextureUsages::TEXTURE_BINDING,
                view_formats: &[],
            },
        ));

        let assets = world.resource(asset_cache()).clone();
        let mut ui_renderer = Renderer::new(
            world,
            world.resource(asset_cache()).clone(),
            RendererConfig {
                scene: ui_scene(),
                shadows: false,
                ..Default::default()
            },
        );
        ui_renderer.post_transparent = Some(Box::new(GizmoRenderer::new(&assets)));
        Self {
            ui_renderer,
            depth_buffer_view: Arc::new(depth_buffer.create_view(&Default::default())),
            gpu,
            normals_view: Arc::new(normals.create_view(&Default::default())),
        }
    }

    fn create_depth_buffer(gpu: Arc<Gpu>, size: &PhysicalSize<u32>) -> Texture {
        Texture::new(
            gpu,
            &wgpu::TextureDescriptor {
                label: Some("RenderTarget.depth_buffer"),
                size: wgpu::Extent3d {
                    width: size.width,
                    height: size.height,
                    depth_or_array_layers: 1,
                },
                mip_level_count: 1,
                sample_count: 1,
                dimension: wgpu::TextureDimension::D2,
                format: DEPTH_FORMAT,
                usage: wgpu::TextureUsages::RENDER_ATTACHMENT
                    | wgpu::TextureUsages::TEXTURE_BINDING
                    | wgpu::TextureUsages::COPY_SRC,
                view_formats: &[],
            },
        )
    }

    fn resize(&mut self, size: &PhysicalSize<u32>) {
        let depth_buffer = Arc::new(Self::create_depth_buffer(self.gpu.clone(), size));
        self.depth_buffer_view = Arc::new(depth_buffer.create_view(&Default::default()));
    }

    fn render(&mut self, world: &mut World) {
        let gpu = world.resource(gpu()).clone();
        let mut encoder = gpu
            .device
            .create_command_encoder(&wgpu::CommandEncoderDescriptor {
                label: Some("UIRenderer"),
            });
        let frame = {
            ambient_profiling::scope!("Get swapchain texture");
            gpu.surface
                .as_ref()
                .unwrap()
                .get_current_texture()
                .expect("Failed to acquire next swap chain texture")
        };

        let window_size = world.resource(window_physical_size());
        let frame_view = frame
            .texture
            .create_view(&wgpu::TextureViewDescriptor::default());
        let mut post_submit = Vec::new();

        tracing::info!("Drawing UI");

        self.ui_renderer.render(
            world,
            &mut encoder,
            &mut post_submit,
            RendererTarget::Direct {
                color: &frame_view,
                depth: &self.depth_buffer_view,
                size: wgpu::Extent3d {
                    width: window_size.x,
                    height: window_size.y,
                    depth_or_array_layers: 1,
                },
                normals: &self.normals_view,
            },
            Some(app_background_color()),
        );
        {
            ambient_profiling::scope!("Submit");
            gpu.queue.submit(Some(encoder.finish()));
        }
        {
            ambient_profiling::scope!("Present");
            frame.present();
        }
        for action in post_submit {
            action();
        }
    }
}