ranim/cmd/preview/
depth_visual.rs

1use crate::render::utils::WgpuContext;
2use std::borrow::Cow;
3
4pub struct DepthVisualPipeline {
5    pub pipeline: wgpu::RenderPipeline,
6    pub bind_group_layout: wgpu::BindGroupLayout,
7}
8
9impl DepthVisualPipeline {
10    pub fn new(wgpu_ctx: &WgpuContext) -> Self {
11        let WgpuContext { device, .. } = wgpu_ctx;
12
13        let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
14            label: Some("Depth Visual Shader"),
15            source: wgpu::ShaderSource::Wgsl(Cow::Borrowed(include_str!(
16                "./shaders/depth_visual.wgsl"
17            ))),
18        });
19
20        let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
21            label: Some("Depth Visual Bind Group Layout"),
22            entries: &[wgpu::BindGroupLayoutEntry {
23                binding: 0,
24                visibility: wgpu::ShaderStages::FRAGMENT,
25                ty: wgpu::BindingType::Texture {
26                    sample_type: wgpu::TextureSampleType::Depth,
27                    view_dimension: wgpu::TextureViewDimension::D2,
28                    multisampled: false,
29                },
30                count: None,
31            }],
32        });
33
34        let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
35            label: Some("Depth Visual Pipeline Layout"),
36            bind_group_layouts: &[&bind_group_layout],
37            push_constant_ranges: &[],
38        });
39
40        let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
41            label: Some("Depth Visual Pipeline"),
42            layout: Some(&pipeline_layout),
43            vertex: wgpu::VertexState {
44                module: &shader,
45                entry_point: Some("vs_main"),
46                buffers: &[],
47                compilation_options: wgpu::PipelineCompilationOptions::default(),
48            },
49            fragment: Some(wgpu::FragmentState {
50                module: &shader,
51                entry_point: Some("fs_main"),
52                compilation_options: wgpu::PipelineCompilationOptions::default(),
53                targets: &[Some(wgpu::ColorTargetState {
54                    format: wgpu::TextureFormat::Rgba8Unorm,
55                    blend: None,
56                    write_mask: wgpu::ColorWrites::ALL,
57                })],
58            }),
59            primitive: wgpu::PrimitiveState {
60                topology: wgpu::PrimitiveTopology::TriangleList,
61                ..Default::default()
62            },
63            depth_stencil: None,
64            multisample: wgpu::MultisampleState::default(),
65            multiview: None,
66            cache: None,
67        });
68
69        Self {
70            pipeline,
71            bind_group_layout,
72        }
73    }
74}