Skip to main content

rend3_imgui/
lib.rs

1//! Render routine integrating imgui into a rend3 rendergraph.
2//!
3//! Call [`ImguiRenderRoutine::add_to_graph`] to add it to the graph.
4
5use imgui_wgpu::RendererConfig;
6use rend3::{
7    graph::{RenderGraph, RenderPassTarget, RenderPassTargets, RenderTargetHandle},
8    types::{Color, TextureFormat},
9    Renderer,
10};
11
12/// An instance of a render routine, holding in imgui_wgpu renderer.
13pub struct ImguiRenderRoutine {
14    pub renderer: imgui_wgpu::Renderer,
15}
16
17impl ImguiRenderRoutine {
18    /// Imgui will always output gamma-encoded color. It will determine if to do
19    /// this in the shader manually based on the output format.
20    pub fn new(renderer: &Renderer, imgui: &mut imgui::Context, output_format: TextureFormat) -> Self {
21        let base = if output_format.describe().srgb {
22            RendererConfig::new()
23        } else {
24            RendererConfig::new_srgb()
25        };
26
27        let renderer = imgui_wgpu::Renderer::new(
28            imgui,
29            &renderer.device,
30            &renderer.queue,
31            RendererConfig {
32                texture_format: output_format,
33                ..base
34            },
35        );
36
37        Self { renderer }
38    }
39
40    pub fn add_to_graph<'node>(
41        &'node mut self,
42        graph: &mut RenderGraph<'node>,
43        draw_data: &'node imgui::DrawData,
44        output: RenderTargetHandle,
45    ) {
46        let mut builder = graph.add_node("imgui");
47
48        let output_handle = builder.add_render_target_output(output);
49
50        let rpass_handle = builder.add_renderpass(RenderPassTargets {
51            targets: vec![RenderPassTarget {
52                color: output_handle,
53                clear: Color::BLACK,
54                resolve: None,
55            }],
56            depth_stencil: None,
57        });
58
59        let pt_handle = builder.passthrough_ref_mut(self);
60
61        builder.build(move |pt, renderer, encoder_or_pass, _temps, _ready, _graph_data| {
62            let this = pt.get_mut(pt_handle);
63            let rpass = encoder_or_pass.get_rpass(rpass_handle);
64
65            this.renderer
66                .render(draw_data, &renderer.queue, &renderer.device, rpass)
67                .unwrap();
68        })
69    }
70}