ranim_render/pipelines/
vitem.rs

1use std::ops::Deref;
2
3use crate::{
4    ResolutionInfo, WgpuContext,
5    primitives::{viewport::ViewportBindGroup, vitems::VItemsBuffer},
6    resource::{GpuResource, OUTPUT_TEXTURE_FORMAT},
7};
8
9// MARK: Compute pipeline
10
11pub struct VItemComputePipeline {
12    pipeline: wgpu::ComputePipeline,
13}
14
15impl Deref for VItemComputePipeline {
16    type Target = wgpu::ComputePipeline;
17    fn deref(&self) -> &Self::Target {
18        &self.pipeline
19    }
20}
21
22impl GpuResource for VItemComputePipeline {
23    fn new(ctx: &WgpuContext) -> Self {
24        let module = &ctx
25            .device
26            .create_shader_module(wgpu::include_wgsl!("./shaders/vitem_compute.wgsl"));
27        let layout = ctx
28            .device
29            .create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
30                label: Some("VItem Compute Pipeline Layout"),
31                bind_group_layouts: &[&VItemsBuffer::compute_bind_group_layout(ctx)],
32                push_constant_ranges: &[],
33            });
34        let pipeline = ctx
35            .device
36            .create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
37                label: Some("VItem Compute Pipeline"),
38                layout: Some(&layout),
39                module,
40                entry_point: Some("cs_main"),
41                compilation_options: wgpu::PipelineCompilationOptions::default(),
42                cache: None,
43            });
44        Self { pipeline }
45    }
46}
47
48// MARK: Color pipeline
49
50pub struct VItemColorPipeline {
51    pipeline: wgpu::RenderPipeline,
52}
53
54impl Deref for VItemColorPipeline {
55    type Target = wgpu::RenderPipeline;
56    fn deref(&self) -> &Self::Target {
57        &self.pipeline
58    }
59}
60
61impl GpuResource for VItemColorPipeline {
62    fn new(ctx: &WgpuContext) -> Self {
63        let module = &ctx
64            .device
65            .create_shader_module(wgpu::include_wgsl!("./shaders/vitem.wgsl"));
66        let layout = ctx
67            .device
68            .create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
69                label: Some("VItem Color Pipeline Layout"),
70                bind_group_layouts: &[
71                    &ResolutionInfo::create_bind_group_layout(ctx),
72                    &ViewportBindGroup::bind_group_layout(ctx),
73                    &VItemsBuffer::render_bind_group_layout(ctx),
74                ],
75                push_constant_ranges: &[],
76            });
77        let pipeline = ctx
78            .device
79            .create_render_pipeline(&wgpu::RenderPipelineDescriptor {
80                label: Some("VItem Color Pipeline"),
81                layout: Some(&layout),
82                vertex: wgpu::VertexState {
83                    module,
84                    entry_point: Some("vs_main"),
85                    buffers: &[],
86                    compilation_options: wgpu::PipelineCompilationOptions::default(),
87                },
88                fragment: Some(wgpu::FragmentState {
89                    module,
90                    entry_point: Some("fs_main"),
91                    compilation_options: wgpu::PipelineCompilationOptions::default(),
92                    targets: &[Some(wgpu::ColorTargetState {
93                        format: OUTPUT_TEXTURE_FORMAT,
94                        blend: Some(wgpu::BlendState::ALPHA_BLENDING),
95                        write_mask: wgpu::ColorWrites::ALL,
96                    })],
97                }),
98                primitive: wgpu::PrimitiveState {
99                    topology: wgpu::PrimitiveTopology::TriangleStrip,
100                    ..Default::default()
101                },
102                depth_stencil: Some(wgpu::DepthStencilState {
103                    format: wgpu::TextureFormat::Depth32Float,
104                    depth_write_enabled: false,
105                    depth_compare: wgpu::CompareFunction::LessEqual,
106                    stencil: wgpu::StencilState::default(),
107                    bias: wgpu::DepthBiasState::default(),
108                }),
109                multisample: wgpu::MultisampleState {
110                    count: 1,
111                    mask: !0,
112                    alpha_to_coverage_enabled: false,
113                },
114                multiview: None,
115                cache: None,
116            });
117        Self { pipeline }
118    }
119}
120
121// MARK: Depth pipeline
122
123pub struct VItemDepthPipeline {
124    pipeline: wgpu::RenderPipeline,
125}
126
127impl Deref for VItemDepthPipeline {
128    type Target = wgpu::RenderPipeline;
129    fn deref(&self) -> &Self::Target {
130        &self.pipeline
131    }
132}
133
134impl GpuResource for VItemDepthPipeline {
135    fn new(ctx: &WgpuContext) -> Self {
136        let module = &ctx
137            .device
138            .create_shader_module(wgpu::include_wgsl!("./shaders/vitem.wgsl"));
139        let layout = ctx
140            .device
141            .create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
142                label: Some("VItem Depth Pipeline Layout"),
143                bind_group_layouts: &[
144                    &ResolutionInfo::create_bind_group_layout(ctx),
145                    &ViewportBindGroup::bind_group_layout(ctx),
146                    &VItemsBuffer::render_bind_group_layout(ctx),
147                ],
148                push_constant_ranges: &[],
149            });
150        let pipeline = ctx
151            .device
152            .create_render_pipeline(&wgpu::RenderPipelineDescriptor {
153                label: Some("VItem Depth Pipeline"),
154                layout: Some(&layout),
155                vertex: wgpu::VertexState {
156                    module,
157                    entry_point: Some("vs_main"),
158                    buffers: &[],
159                    compilation_options: wgpu::PipelineCompilationOptions::default(),
160                },
161                fragment: Some(wgpu::FragmentState {
162                    module,
163                    entry_point: Some("fs_depth_only"),
164                    compilation_options: wgpu::PipelineCompilationOptions::default(),
165                    targets: &[],
166                }),
167                primitive: wgpu::PrimitiveState {
168                    topology: wgpu::PrimitiveTopology::TriangleStrip,
169                    ..Default::default()
170                },
171                depth_stencil: Some(wgpu::DepthStencilState {
172                    format: wgpu::TextureFormat::Depth32Float,
173                    depth_write_enabled: true,
174                    depth_compare: wgpu::CompareFunction::Less,
175                    stencil: wgpu::StencilState::default(),
176                    bias: wgpu::DepthBiasState::default(),
177                }),
178                multisample: wgpu::MultisampleState {
179                    count: 1,
180                    mask: !0,
181                    alpha_to_coverage_enabled: false,
182                },
183                multiview: None,
184                cache: None,
185            });
186        Self { pipeline }
187    }
188}