ranim_render/pipelines/
mesh_item.rs

1use std::ops::Deref;
2
3use crate::{
4    ResolutionInfo, WgpuContext,
5    primitives::{mesh_items::MeshItemsBuffer, viewport::ViewportBindGroup},
6    resource::{GpuResource, OUTPUT_TEXTURE_FORMAT},
7};
8
9pub struct MeshItemColorPipeline {
10    pipeline: wgpu::RenderPipeline,
11}
12
13impl Deref for MeshItemColorPipeline {
14    type Target = wgpu::RenderPipeline;
15    fn deref(&self) -> &Self::Target {
16        &self.pipeline
17    }
18}
19
20impl GpuResource for MeshItemColorPipeline {
21    fn new(ctx: &WgpuContext) -> Self {
22        let module = &ctx
23            .device
24            .create_shader_module(wgpu::include_wgsl!("./shaders/mesh_item.wgsl"));
25        let layout = ctx
26            .device
27            .create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
28                label: Some("MeshItem Color Pipeline Layout"),
29                bind_group_layouts: &[
30                    &ResolutionInfo::create_bind_group_layout(ctx),
31                    &ViewportBindGroup::bind_group_layout(ctx),
32                    &MeshItemsBuffer::render_bind_group_layout(ctx),
33                ],
34                push_constant_ranges: &[],
35            });
36        let vertex_buffer_layouts = MeshItemsBuffer::vertex_buffer_layouts();
37        let pipeline = ctx
38            .device
39            .create_render_pipeline(&wgpu::RenderPipelineDescriptor {
40                label: Some("MeshItem Color Pipeline"),
41                layout: Some(&layout),
42                vertex: wgpu::VertexState {
43                    module,
44                    entry_point: Some("vs_main"),
45                    buffers: &vertex_buffer_layouts,
46                    compilation_options: wgpu::PipelineCompilationOptions::default(),
47                },
48                fragment: Some(wgpu::FragmentState {
49                    module,
50                    entry_point: Some("fs_color"),
51                    compilation_options: wgpu::PipelineCompilationOptions::default(),
52                    targets: &[Some(wgpu::ColorTargetState {
53                        format: OUTPUT_TEXTURE_FORMAT,
54                        blend: Some(wgpu::BlendState::ALPHA_BLENDING),
55                        write_mask: wgpu::ColorWrites::ALL,
56                    })],
57                }),
58                primitive: wgpu::PrimitiveState {
59                    topology: wgpu::PrimitiveTopology::TriangleList,
60                    ..Default::default()
61                },
62                depth_stencil: Some(wgpu::DepthStencilState {
63                    format: wgpu::TextureFormat::Depth32Float,
64                    depth_write_enabled: false,
65                    depth_compare: wgpu::CompareFunction::LessEqual,
66                    stencil: wgpu::StencilState::default(),
67                    bias: wgpu::DepthBiasState::default(),
68                }),
69                multisample: wgpu::MultisampleState {
70                    count: 1,
71                    mask: !0,
72                    alpha_to_coverage_enabled: false,
73                },
74                multiview: None,
75                cache: None,
76            });
77        Self { pipeline }
78    }
79}
80
81pub struct MeshItemDepthPipeline {
82    pipeline: wgpu::RenderPipeline,
83}
84
85impl Deref for MeshItemDepthPipeline {
86    type Target = wgpu::RenderPipeline;
87    fn deref(&self) -> &Self::Target {
88        &self.pipeline
89    }
90}
91
92impl GpuResource for MeshItemDepthPipeline {
93    fn new(ctx: &WgpuContext) -> Self {
94        let module = &ctx
95            .device
96            .create_shader_module(wgpu::include_wgsl!("./shaders/mesh_item.wgsl"));
97        let layout = ctx
98            .device
99            .create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
100                label: Some("MeshItem Depth Pipeline Layout"),
101                bind_group_layouts: &[
102                    &ResolutionInfo::create_bind_group_layout(ctx),
103                    &ViewportBindGroup::bind_group_layout(ctx),
104                    &MeshItemsBuffer::render_bind_group_layout(ctx),
105                ],
106                push_constant_ranges: &[],
107            });
108        let vertex_buffer_layouts = MeshItemsBuffer::vertex_buffer_layouts();
109        let pipeline = ctx
110            .device
111            .create_render_pipeline(&wgpu::RenderPipelineDescriptor {
112                label: Some("MeshItem Depth Pipeline"),
113                layout: Some(&layout),
114                vertex: wgpu::VertexState {
115                    module,
116                    entry_point: Some("vs_main"),
117                    buffers: &vertex_buffer_layouts,
118                    compilation_options: wgpu::PipelineCompilationOptions::default(),
119                },
120                fragment: Some(wgpu::FragmentState {
121                    module,
122                    entry_point: Some("fs_depth"),
123                    compilation_options: wgpu::PipelineCompilationOptions::default(),
124                    targets: &[],
125                }),
126                primitive: wgpu::PrimitiveState {
127                    topology: wgpu::PrimitiveTopology::TriangleList,
128                    ..Default::default()
129                },
130                depth_stencil: Some(wgpu::DepthStencilState {
131                    format: wgpu::TextureFormat::Depth32Float,
132                    depth_write_enabled: true,
133                    depth_compare: wgpu::CompareFunction::Less,
134                    stencil: wgpu::StencilState::default(),
135                    bias: wgpu::DepthBiasState::default(),
136                }),
137                multisample: wgpu::MultisampleState {
138                    count: 1,
139                    mask: !0,
140                    alpha_to_coverage_enabled: false,
141                },
142                multiview: None,
143                cache: None,
144            });
145        Self { pipeline }
146    }
147}