gloss_renderer/forward_renderer/render_passes/
debug_pass.rs

1/// Renders a default triangle to screen, no need for a vertex or index buffer
2pub struct DebugPass {
3    render_pipeline: wgpu::RenderPipeline,
4}
5
6impl DebugPass {
7    pub fn new(device: &wgpu::Device, surface_format: &wgpu::TextureFormat) -> Self {
8        let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
9            label: Some("Shader"),
10            source: wgpu::ShaderSource::Wgsl(include_str!("../../../shaders/minimal_tri.wgsl").into()),
11        });
12
13        let render_pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
14            label: Some("Debug Pipeline Layout"),
15            bind_group_layouts: &[],
16            push_constant_ranges: &[],
17        });
18
19        let render_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
20            label: Some("Debug Pipeline"),
21            layout: Some(&render_pipeline_layout),
22            vertex: wgpu::VertexState {
23                module: &shader,
24                entry_point: "vs_main",
25                buffers: &[],
26                compilation_options: wgpu::PipelineCompilationOptions::default(),
27            },
28            fragment: Some(wgpu::FragmentState {
29                module: &shader,
30                entry_point: "fs_main",
31                targets: &[Some(wgpu::ColorTargetState {
32                    format: *surface_format,
33                    // format: wgpu::TextureFormat::Bgra8UnormSrgb,
34                    // format: wgpu::TextureFormat::Rgba8UnormSrgb,
35                    blend: Some(wgpu::BlendState {
36                        color: wgpu::BlendComponent::REPLACE,
37                        alpha: wgpu::BlendComponent::REPLACE,
38                    }),
39                    write_mask: wgpu::ColorWrites::ALL,
40                })],
41                compilation_options: wgpu::PipelineCompilationOptions::default(),
42            }),
43            primitive: wgpu::PrimitiveState {
44                topology: wgpu::PrimitiveTopology::TriangleList,
45                strip_index_format: None,
46                front_face: wgpu::FrontFace::Ccw,
47                cull_mode: Some(wgpu::Face::Back),
48                // Setting this to anything other than Fill requires Features::POLYGON_MODE_LINE
49                // or Features::POLYGON_MODE_POINT
50                polygon_mode: wgpu::PolygonMode::Fill,
51                // Requires Features::DEPTH_CLIP_CONTROL
52                unclipped_depth: false,
53                // Requires Features::CONSERVATIVE_RASTERIZATION
54                conservative: false,
55            },
56            depth_stencil: None,
57            multisample: wgpu::MultisampleState {
58                count: 1,
59                mask: !0,
60                alpha_to_coverage_enabled: false,
61            },
62            // If the pipeline will be used with a multiview render pass, this
63            // indicates how many array layers the attachments will have.
64            multiview: None,
65            cache: None,
66        });
67
68        Self { render_pipeline }
69    }
70
71    pub fn run(&self, device: &wgpu::Device, queue: &wgpu::Queue, out_view: &wgpu::TextureView) {
72        let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
73            label: Some("Debug Encoder"),
74        });
75
76        {
77            let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
78                label: Some("Debug Pass"),
79                color_attachments: &[Some(wgpu::RenderPassColorAttachment {
80                    view: out_view,
81                    resolve_target: None,
82                    ops: wgpu::Operations {
83                        load: wgpu::LoadOp::Clear(wgpu::Color {
84                            r: 0.1,
85                            g: 0.2,
86                            b: 0.3,
87                            a: 1.0,
88                        }),
89                        store: wgpu::StoreOp::Store,
90                    },
91                })],
92                depth_stencil_attachment: None,
93                timestamp_writes: None,
94                occlusion_query_set: None,
95            });
96
97            render_pass.set_pipeline(&self.render_pipeline);
98            render_pass.draw(0..3, 0..1);
99        }
100        queue.submit(Some(encoder.finish()));
101    }
102}