rafx_framework/render_features/jobs/prepare/
submit_packet.rs

1use crate::render_features::render_features_prelude::*;
2
3/// The `SubmitPacket` is the data that must be prepared from
4/// in order for the `RenderFeature`'s `WriteJob` to create each
5/// draw call. The draw calls may reference data in either the
6/// `FramePacket` or the `SubmitPacket`. Each draw call is represented
7/// by exactly 1 `SubmitNode`. In order to allocate the `SubmitPacket`,
8/// the `RenderFeature` is given a reference to the populated `FramePacket`
9/// from that frame. The `RenderFeature` **must** size the `SubmitPacket`
10/// appropriately. The `SubmitPacket` **must** be considered
11/// immutable after the `Prepare` step has finished.
12pub trait SubmitPacketData {
13    /// All data that is either unassociated with or shared by
14    /// any of the submit nodes across each `RenderView`.
15    type PerFrameSubmitData: Sync + Send;
16
17    /// All data for any submit nodes associated with a particular
18    /// `Entity` and `RenderObject`.
19    type RenderObjectInstanceSubmitData: Sync + Send;
20
21    /// All data that can be shared by the submit nodes for
22    /// this `RenderFeature`'s `WriteJob` in this `RenderView`
23    /// and `RenderPhase`.
24    type PerViewSubmitData: Sync + Send;
25
26    /// All data for any submit nodes associated with a particular
27    /// `Entity`, `RenderObject`, and `RenderView`.
28    type RenderObjectInstancePerViewSubmitData: Sync + Send;
29
30    /// The data needed by this `RenderFeature`'s `WriteJob` for
31    /// each draw call in this `RenderView` and `RenderPhase` .
32    type SubmitNodeData: Sync + Send;
33
34    /// The `RenderFeature` associated with the `SubmitNodeBlock`s.
35    /// This is used to find the correct `WriteJob` when writing
36    /// the `PreparedRenderData`.
37    type RenderFeature: RenderFeature;
38
39    // TODO(dvd): see issue #29661 <https://github.com/rust-lang/rust/issues/29661> for more information
40    // type SubmitPacket = SubmitPacket<Self>;
41}
42
43/// Read documentation on `SubmitPacketData`.
44pub struct SubmitPacket<SubmitPacketDataT: SubmitPacketData> {
45    feature_index: RenderFeatureIndex,
46
47    pub(crate) per_frame_submit_data: AtomicOnceCell<SubmitPacketDataT::PerFrameSubmitData>,
48    pub(crate) render_object_instances_submit_data:
49        AtomicOnceCellArray<SubmitPacketDataT::RenderObjectInstanceSubmitData>,
50
51    view_submit_packets: Vec<ViewSubmitPacket<SubmitPacketDataT>>,
52}
53
54impl<SubmitPacketDataT: SubmitPacketData + Send + Sync + 'static> SubmitPacket<SubmitPacketDataT> {
55    pub fn new(
56        feature_index: RenderFeatureIndex,
57        num_render_object_instances: usize,
58        view_submit_packets: Vec<ViewSubmitPacket<SubmitPacketDataT>>,
59    ) -> Self {
60        #[cfg(debug_assertions)]
61        for (i, vp) in view_submit_packets.iter().enumerate() {
62            assert_eq!(i, vp.view_frame_index() as usize);
63        }
64
65        Self {
66            feature_index,
67            per_frame_submit_data: AtomicOnceCell::new(),
68            render_object_instances_submit_data: AtomicOnceCellArray::with_capacity(
69                num_render_object_instances,
70            ),
71            view_submit_packets,
72        }
73    }
74
75    pub fn view_submit_packets(&self) -> &Vec<ViewSubmitPacket<SubmitPacketDataT>> {
76        &self.view_submit_packets
77    }
78
79    pub fn view_submit_packet(
80        &self,
81        view_frame_index: ViewFrameIndex,
82    ) -> &ViewSubmitPacket<SubmitPacketDataT> {
83        self.view_submit_packets
84            .get(view_frame_index as usize)
85            .unwrap_or_else(|| {
86                panic!(
87                    "ViewSubmitPacket with ViewFrameIndex {} was not found in {}.",
88                    view_frame_index,
89                    std::any::type_name::<SubmitPacketDataT>()
90                )
91            })
92    }
93
94    pub fn render_object_instances_submit_data(
95        &self
96    ) -> &AtomicOnceCellArray<SubmitPacketDataT::RenderObjectInstanceSubmitData> {
97        &self.render_object_instances_submit_data
98    }
99
100    pub fn per_frame_submit_data(&self) -> &AtomicOnceCell<SubmitPacketDataT::PerFrameSubmitData> {
101        &self.per_frame_submit_data
102    }
103}
104
105impl<SubmitPacketDataT: 'static + Send + Sync + SubmitPacketData> RenderFeatureSubmitPacket
106    for SubmitPacket<SubmitPacketDataT>
107{
108    fn render_feature_view_submit_packet(
109        &self,
110        view_index: ViewFrameIndex,
111    ) -> &dyn RenderFeatureViewSubmitPacket {
112        self.view_submit_packet(view_index)
113    }
114
115    fn feature_index(&self) -> u32 {
116        self.feature_index
117    }
118}