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
slotmap::new_key_type! { pub struct ViewBuilderHandle; }

pub fn new_renderer_callback(
    view_builder: re_renderer::ViewBuilder,
    viewport: egui::Rect,
    clear_color: re_renderer::Rgba,
) -> egui::PaintCallback {
    egui_wgpu::Callback::new_paint_callback(
        viewport,
        ReRendererCallback {
            view_builder,
            clear_color,
        },
    )
}

struct ReRendererCallback {
    view_builder: re_renderer::ViewBuilder,
    clear_color: re_renderer::Rgba,
}

impl egui_wgpu::CallbackTrait for ReRendererCallback {
    // TODO(andreas): Prepare callbacks should run in parallel.
    //                Command buffer recording may be fairly expensive in the future!
    //                Sticking to egui's current model, each prepare callback could fork of a task and in finish_prepare we wait for them.
    fn prepare(
        &self,
        _device: &wgpu::Device,
        _queue: &wgpu::Queue,
        _screen_descriptor: &egui_wgpu::ScreenDescriptor,
        _egui_encoder: &mut wgpu::CommandEncoder,
        paint_callback_resources: &mut egui_wgpu::CallbackResources,
    ) -> Vec<wgpu::CommandBuffer> {
        let Some(ctx) = paint_callback_resources.get::<re_renderer::RenderContext>() else {
            re_log::error_once!(
                "Failed to execute egui prepare callback. No render context available."
            );
            return Vec::new();
        };

        match self.view_builder.draw(ctx, self.clear_color) {
            Ok(command_buffer) => vec![command_buffer],
            Err(err) => {
                re_log::error_once!("Failed to fill view builder: {err}");
                // TODO(andreas): It would be nice to paint an error message instead.
                Vec::new()
            }
        }
    }

    fn paint(
        &self,
        _info: egui::PaintCallbackInfo,
        render_pass: &mut wgpu::RenderPass<'static>,
        paint_callback_resources: &egui_wgpu::CallbackResources,
    ) {
        let Some(ctx) = paint_callback_resources.get::<re_renderer::RenderContext>() else {
            // TODO(#4433): Shouldn't show up like this.
            re_log::error_once!(
                "Failed to execute egui draw callback. No render context available."
            );
            return;
        };
        self.view_builder.composite(ctx, render_pass);
    }
}