ranim_render/pipelines/
oit_resolve.rs

1use std::ops::Deref;
2
3use crate::{
4    ResolutionInfo, WgpuContext,
5    resource::{GpuResource, OUTPUT_TEXTURE_FORMAT},
6};
7
8pub struct OITResolvePipeline {
9    pipeline: wgpu::RenderPipeline,
10}
11
12impl Deref for OITResolvePipeline {
13    type Target = wgpu::RenderPipeline;
14    fn deref(&self) -> &Self::Target {
15        &self.pipeline
16    }
17}
18
19impl OITResolvePipeline {
20    pub fn depth_bind_group_layout(ctx: &WgpuContext) -> wgpu::BindGroupLayout {
21        ctx.device
22            .create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
23                label: Some("OIT Resolve Depth BGL"),
24                entries: &[wgpu::BindGroupLayoutEntry {
25                    binding: 0,
26                    visibility: wgpu::ShaderStages::FRAGMENT,
27                    ty: wgpu::BindingType::Texture {
28                        sample_type: wgpu::TextureSampleType::Depth,
29                        view_dimension: wgpu::TextureViewDimension::D2,
30                        multisampled: false,
31                    },
32                    count: None,
33                }],
34            })
35    }
36}
37
38impl GpuResource for OITResolvePipeline {
39    fn new(wgpu_ctx: &WgpuContext) -> Self {
40        let WgpuContext { device, .. } = wgpu_ctx;
41
42        let module =
43            &device.create_shader_module(wgpu::include_wgsl!("./shaders/oit_resolve.wgsl"));
44
45        let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
46            label: Some("OIT Resolve Pipeline Layout"),
47            bind_group_layouts: &[
48                &ResolutionInfo::create_bind_group_layout(wgpu_ctx),
49                &Self::depth_bind_group_layout(wgpu_ctx),
50            ],
51            push_constant_ranges: &[],
52        });
53
54        let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
55            label: Some("OIT Resolve Pipeline"),
56            layout: Some(&pipeline_layout),
57            vertex: wgpu::VertexState {
58                module,
59                entry_point: Some("vs_main"),
60                buffers: &[],
61                compilation_options: wgpu::PipelineCompilationOptions::default(),
62            },
63            fragment: Some(wgpu::FragmentState {
64                module,
65                entry_point: Some("fs_main"),
66                compilation_options: wgpu::PipelineCompilationOptions::default(),
67                targets: &[Some(wgpu::ColorTargetState {
68                    format: OUTPUT_TEXTURE_FORMAT,
69                    blend: Some(wgpu::BlendState::ALPHA_BLENDING),
70                    write_mask: wgpu::ColorWrites::ALL,
71                })],
72            }),
73            primitive: wgpu::PrimitiveState {
74                topology: wgpu::PrimitiveTopology::TriangleList,
75                ..Default::default()
76            },
77            depth_stencil: None, // No depth attachment
78            multisample: wgpu::MultisampleState {
79                count: 1,
80                mask: !0,
81                alpha_to_coverage_enabled: false,
82            },
83            multiview: None,
84            cache: None,
85        });
86
87        Self { pipeline }
88    }
89}