bevy_core_pipeline/deferred/
mod.rs

1pub mod copy_lighting_id;
2pub mod node;
3
4use core::ops::Range;
5
6use crate::prepass::{OpaqueNoLightmap3dBatchSetKey, OpaqueNoLightmap3dBinKey};
7use bevy_ecs::prelude::*;
8use bevy_render::sync_world::MainEntity;
9use bevy_render::{
10    render_phase::{
11        BinnedPhaseItem, CachedRenderPipelinePhaseItem, DrawFunctionId, PhaseItem,
12        PhaseItemExtraIndex,
13    },
14    render_resource::{CachedRenderPipelineId, TextureFormat},
15};
16
17pub const DEFERRED_PREPASS_FORMAT: TextureFormat = TextureFormat::Rgba32Uint;
18pub const DEFERRED_LIGHTING_PASS_ID_FORMAT: TextureFormat = TextureFormat::R8Uint;
19pub const DEFERRED_LIGHTING_PASS_ID_DEPTH_FORMAT: TextureFormat = TextureFormat::Depth16Unorm;
20
21/// Opaque phase of the 3D Deferred pass.
22///
23/// Sorted by pipeline, then by mesh to improve batching.
24///
25/// Used to render all 3D meshes with materials that have no transparency.
26#[derive(PartialEq, Eq, Hash)]
27pub struct Opaque3dDeferred {
28    /// Determines which objects can be placed into a *batch set*.
29    ///
30    /// Objects in a single batch set can potentially be multi-drawn together,
31    /// if it's enabled and the current platform supports it.
32    pub batch_set_key: OpaqueNoLightmap3dBatchSetKey,
33    /// Information that separates items into bins.
34    pub bin_key: OpaqueNoLightmap3dBinKey,
35    pub representative_entity: (Entity, MainEntity),
36    pub batch_range: Range<u32>,
37    pub extra_index: PhaseItemExtraIndex,
38}
39
40impl PhaseItem for Opaque3dDeferred {
41    #[inline]
42    fn entity(&self) -> Entity {
43        self.representative_entity.0
44    }
45
46    fn main_entity(&self) -> MainEntity {
47        self.representative_entity.1
48    }
49
50    #[inline]
51    fn draw_function(&self) -> DrawFunctionId {
52        self.batch_set_key.draw_function
53    }
54
55    #[inline]
56    fn batch_range(&self) -> &Range<u32> {
57        &self.batch_range
58    }
59
60    #[inline]
61    fn batch_range_mut(&mut self) -> &mut Range<u32> {
62        &mut self.batch_range
63    }
64
65    #[inline]
66    fn extra_index(&self) -> PhaseItemExtraIndex {
67        self.extra_index.clone()
68    }
69
70    #[inline]
71    fn batch_range_and_extra_index_mut(&mut self) -> (&mut Range<u32>, &mut PhaseItemExtraIndex) {
72        (&mut self.batch_range, &mut self.extra_index)
73    }
74}
75
76impl BinnedPhaseItem for Opaque3dDeferred {
77    type BatchSetKey = OpaqueNoLightmap3dBatchSetKey;
78    type BinKey = OpaqueNoLightmap3dBinKey;
79
80    #[inline]
81    fn new(
82        batch_set_key: Self::BatchSetKey,
83        bin_key: Self::BinKey,
84        representative_entity: (Entity, MainEntity),
85        batch_range: Range<u32>,
86        extra_index: PhaseItemExtraIndex,
87    ) -> Self {
88        Self {
89            batch_set_key,
90            bin_key,
91            representative_entity,
92            batch_range,
93            extra_index,
94        }
95    }
96}
97
98impl CachedRenderPipelinePhaseItem for Opaque3dDeferred {
99    #[inline]
100    fn cached_pipeline(&self) -> CachedRenderPipelineId {
101        self.batch_set_key.pipeline
102    }
103}
104
105/// Alpha mask phase of the 3D Deferred pass.
106///
107/// Sorted by pipeline, then by mesh to improve batching.
108///
109/// Used to render all meshes with a material with an alpha mask.
110pub struct AlphaMask3dDeferred {
111    /// Determines which objects can be placed into a *batch set*.
112    ///
113    /// Objects in a single batch set can potentially be multi-drawn together,
114    /// if it's enabled and the current platform supports it.
115    pub batch_set_key: OpaqueNoLightmap3dBatchSetKey,
116    /// Information that separates items into bins.
117    pub bin_key: OpaqueNoLightmap3dBinKey,
118    pub representative_entity: (Entity, MainEntity),
119    pub batch_range: Range<u32>,
120    pub extra_index: PhaseItemExtraIndex,
121}
122
123impl PhaseItem for AlphaMask3dDeferred {
124    #[inline]
125    fn entity(&self) -> Entity {
126        self.representative_entity.0
127    }
128
129    #[inline]
130    fn main_entity(&self) -> MainEntity {
131        self.representative_entity.1
132    }
133
134    #[inline]
135    fn draw_function(&self) -> DrawFunctionId {
136        self.batch_set_key.draw_function
137    }
138
139    #[inline]
140    fn batch_range(&self) -> &Range<u32> {
141        &self.batch_range
142    }
143
144    #[inline]
145    fn batch_range_mut(&mut self) -> &mut Range<u32> {
146        &mut self.batch_range
147    }
148
149    #[inline]
150    fn extra_index(&self) -> PhaseItemExtraIndex {
151        self.extra_index.clone()
152    }
153
154    #[inline]
155    fn batch_range_and_extra_index_mut(&mut self) -> (&mut Range<u32>, &mut PhaseItemExtraIndex) {
156        (&mut self.batch_range, &mut self.extra_index)
157    }
158}
159
160impl BinnedPhaseItem for AlphaMask3dDeferred {
161    type BatchSetKey = OpaqueNoLightmap3dBatchSetKey;
162    type BinKey = OpaqueNoLightmap3dBinKey;
163
164    fn new(
165        batch_set_key: Self::BatchSetKey,
166        bin_key: Self::BinKey,
167        representative_entity: (Entity, MainEntity),
168        batch_range: Range<u32>,
169        extra_index: PhaseItemExtraIndex,
170    ) -> Self {
171        Self {
172            batch_set_key,
173            bin_key,
174            representative_entity,
175            batch_range,
176            extra_index,
177        }
178    }
179}
180
181impl CachedRenderPipelinePhaseItem for AlphaMask3dDeferred {
182    #[inline]
183    fn cached_pipeline(&self) -> CachedRenderPipelineId {
184        self.batch_set_key.pipeline
185    }
186}