bevy_pbr/prepass/
prepass_bindings.rs

1use bevy_core_pipeline::prepass::ViewPrepassTextures;
2use bevy_render::render_resource::{
3    binding_types::{
4        texture_2d, texture_2d_multisampled, texture_depth_2d, texture_depth_2d_multisampled,
5    },
6    BindGroupLayoutEntryBuilder, TextureAspect, TextureSampleType, TextureView,
7    TextureViewDescriptor,
8};
9use bevy_utils::default;
10
11use crate::MeshPipelineViewLayoutKey;
12
13pub fn get_bind_group_layout_entries(
14    layout_key: MeshPipelineViewLayoutKey,
15) -> [Option<BindGroupLayoutEntryBuilder>; 4] {
16    let mut entries: [Option<BindGroupLayoutEntryBuilder>; 4] = [None; 4];
17
18    let multisampled = layout_key.contains(MeshPipelineViewLayoutKey::MULTISAMPLED);
19
20    if layout_key.contains(MeshPipelineViewLayoutKey::DEPTH_PREPASS) {
21        // Depth texture
22        entries[0] = if multisampled {
23            Some(texture_depth_2d_multisampled())
24        } else {
25            Some(texture_depth_2d())
26        };
27    }
28
29    if layout_key.contains(MeshPipelineViewLayoutKey::NORMAL_PREPASS) {
30        // Normal texture
31        entries[1] = if multisampled {
32            Some(texture_2d_multisampled(TextureSampleType::Float {
33                filterable: false,
34            }))
35        } else {
36            Some(texture_2d(TextureSampleType::Float { filterable: false }))
37        };
38    }
39
40    if layout_key.contains(MeshPipelineViewLayoutKey::MOTION_VECTOR_PREPASS) {
41        // Motion Vectors texture
42        entries[2] = if multisampled {
43            Some(texture_2d_multisampled(TextureSampleType::Float {
44                filterable: false,
45            }))
46        } else {
47            Some(texture_2d(TextureSampleType::Float { filterable: false }))
48        };
49    }
50
51    if layout_key.contains(MeshPipelineViewLayoutKey::DEFERRED_PREPASS) {
52        // Deferred texture
53        entries[3] = Some(texture_2d(TextureSampleType::Uint));
54    }
55
56    entries
57}
58
59pub fn get_bindings(prepass_textures: Option<&ViewPrepassTextures>) -> [Option<TextureView>; 4] {
60    let depth_desc = TextureViewDescriptor {
61        label: Some("prepass_depth"),
62        aspect: TextureAspect::DepthOnly,
63        ..default()
64    };
65    let depth_view = prepass_textures
66        .and_then(|x| x.depth.as_ref())
67        .map(|texture| texture.texture.texture.create_view(&depth_desc));
68
69    [
70        depth_view,
71        prepass_textures.and_then(|pt| pt.normal_view().cloned()),
72        prepass_textures.and_then(|pt| pt.motion_vectors_view().cloned()),
73        prepass_textures.and_then(|pt| pt.deferred_view().cloned()),
74    ]
75}