comfy_wgpu/
debug.rs

1use crate::*;
2
3pub fn render_debug(
4    context: &GraphicsContext,
5    shaders: &mut ShaderMap,
6    enable_z_buffer: bool,
7    quad_ubg: &UniformBindGroup,
8    texture_layout: &wgpu::BindGroupLayout,
9    depth_texture: &Arc<Texture>,
10    bind_groups: Vec<&wgpu::BindGroup>,
11    pipelines: &mut HashMap<String, wgpu::RenderPipeline>,
12    surface_view: &wgpu::TextureView,
13) {
14    let _span = span!("render_debug");
15
16    let size = 0.3;
17
18    let quads: Vec<_> = bind_groups
19        .iter()
20        .enumerate()
21        .map(|(i, _)| {
22            QuadUniform {
23                clip_position: [
24                    1.0 - size / 2.0, // - size / 3.0 * i as f32,
25                    0.8 - size / 2.0 - 2.0 * size * i as f32,
26                ],
27                size: [size, size],
28            }
29        })
30        .collect();
31
32    let debug_render_pipeline = pipelines
33        .entry(if enable_z_buffer { "debug-z" } else { "debug" }.into())
34        .or_insert_with(|| {
35            let debug_shader_id = create_shader(
36                shaders,
37                "debug",
38                &sprite_shader_from_fragment(engine_shader_source!("debug")),
39                HashMap::new(),
40            )
41            .unwrap();
42
43            create_render_pipeline_with_layout(
44                "Debug",
45                &context.device,
46                context.config.borrow().format,
47                &[&texture_layout, &quad_ubg.layout],
48                &[],
49                // TODO: .shaders.get_or_err(...)
50                shaders.get(debug_shader_id).unwrap(),
51                BlendMode::Alpha,
52                enable_z_buffer,
53            )
54            .expect("debug pipeline creation failed")
55        });
56
57
58    for (i, bind_group) in bind_groups.iter().enumerate() {
59        context.queue.write_buffer(
60            &quad_ubg.buffer,
61            0,
62            bytemuck::cast_slice(&[quads[i]]),
63        );
64
65        let mut encoder = context.device.simple_encoder("Debug Render Encoder");
66        {
67            let mut render_pass =
68                encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
69                    label: Some("Debug Render Pass"),
70                    color_attachments: &[Some(
71                        wgpu::RenderPassColorAttachment {
72                            view: surface_view,
73                            resolve_target: None,
74                            ops: wgpu::Operations {
75                                load: color_to_clear_op(None),
76                                store: wgpu::StoreOp::Store,
77                            },
78                        },
79                    )],
80                    depth_stencil_attachment: depth_stencil_attachment(
81                        enable_z_buffer,
82                        &depth_texture.view,
83                        false,
84                    ),
85                    timestamp_writes: None,
86                    occlusion_query_set: None,
87                });
88
89
90            render_pass.set_pipeline(debug_render_pipeline);
91            render_pass.set_bind_group(0, bind_group, &[]);
92            render_pass.set_bind_group(1, &quad_ubg.bind_group, &[]);
93            render_pass.draw(0..6, 0..1);
94        }
95
96        context.queue.submit(std::iter::once(encoder.finish()));
97    }
98}