Skip to main content

mln_gfx/
frame.rs

1//! How the renderer draws a frame into its target: it begins the frame,
2//! draws into the textures the target gives it, submits them, and ends the
3//! frame, which hands the image off.
4
5use mln_util::size::Size;
6
7use crate::gpu::{TargetFormats, TextureId};
8
9/// The textures of the backend's `Gpu` that a frame draws into.
10#[derive(Clone, Copy, Debug, PartialEq)]
11pub struct FrameImage {
12    /// In physical pixels.
13    pub size: Size,
14    pub color: TextureId,
15    pub depth_stencil: Option<TextureId>,
16    /// Their formats, as the frame's pipelines name them.
17    pub targets: TargetFormats,
18}
19
20/// How one side of a shared texture waits for the other's GPU work on it.
21#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
22pub enum SyncKind {
23    /// The work has finished, so there is nothing to wait for.
24    CpuComplete,
25    /// A `GLsync` in the share group of both sides.
26    OpenGlFence,
27    /// The work went to the queue that both sides submit to, so work
28    /// submitted to it later runs after it.
29    QueueOrder,
30    /// A timeline `VkSemaphore` and the value it reaches.
31    VulkanTimelineSemaphore,
32    /// A sync file descriptor, which signals once.
33    SyncFd,
34    /// An `MTLSharedEvent` and the value it reaches.
35    MetalSharedEvent,
36    /// A shared handle to an `ID3D12Fence` and the value it reaches.
37    D3d12Fence,
38}
39
40impl SyncKind {
41    /// Every kind, from the one that waits least to the one that waits most:
42    /// queue order waits for nothing, a GPU object makes only the GPU wait,
43    /// a sync file crosses APIs, and a finished frame makes the CPU wait.
44    pub(crate) const BY_PREFERENCE: [SyncKind; 7] = [
45        SyncKind::QueueOrder,
46        SyncKind::OpenGlFence,
47        SyncKind::VulkanTimelineSemaphore,
48        SyncKind::MetalSharedEvent,
49        SyncKind::D3d12Fence,
50        SyncKind::SyncFd,
51        SyncKind::CpuComplete,
52    ];
53
54    /// Whether a sync of this kind names an object to wait on.
55    #[must_use]
56    pub fn names_object(self) -> bool {
57        !matches!(self, SyncKind::CpuComplete | SyncKind::QueueOrder)
58    }
59
60    fn bit(self) -> u8 {
61        1 << self as u8
62    }
63}
64
65/// A set of [`SyncKind`]s.
66#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
67pub struct SyncKinds(u8);
68
69impl SyncKinds {
70    pub const NONE: SyncKinds = SyncKinds(0);
71
72    #[must_use]
73    pub fn of(kinds: &[SyncKind]) -> SyncKinds {
74        SyncKinds(kinds.iter().fold(0, |bits, kind| bits | kind.bit()))
75    }
76
77    #[must_use]
78    pub fn contains(self, kind: SyncKind) -> bool {
79        self.0 & kind.bit() != 0
80    }
81
82    #[must_use]
83    pub(crate) fn is_empty(self) -> bool {
84        self.0 == 0
85    }
86
87    #[must_use]
88    pub fn with(self, kind: SyncKind) -> SyncKinds {
89        SyncKinds(self.0 | kind.bit())
90    }
91
92    #[must_use]
93    pub fn union(self, other: SyncKinds) -> SyncKinds {
94        SyncKinds(self.0 | other.0)
95    }
96
97    pub fn iter(self) -> impl Iterator<Item = SyncKind> {
98        SyncKind::BY_PREFERENCE.into_iter().filter(move |kind| self.contains(*kind))
99    }
100
101    /// The kind of `self`, the ones a host accepts, that `supported` has and
102    /// that waits least. A host that names none accepts a finished frame
103    /// where `supported` has one, and queue order otherwise.
104    #[must_use]
105    pub fn pick(self, supported: SyncKinds) -> Option<SyncKind> {
106        let accepted = if !self.is_empty() {
107            self
108        } else if supported.contains(SyncKind::CpuComplete) {
109            SyncKinds::of(&[SyncKind::CpuComplete])
110        } else {
111            SyncKinds::of(&[SyncKind::QueueOrder])
112        };
113        SyncKind::BY_PREFERENCE.into_iter().find(|kind| accepted.contains(*kind) && supported.contains(*kind))
114    }
115}
116
117/// A point in one side's GPU work that the other side waits for before it
118/// uses a shared texture: the object to wait on, as an integer, for the
119/// kinds that name one, and the value a timeline reaches.
120#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
121pub struct GpuSync {
122    pub kind: SyncKind,
123    pub handle: u64,
124    pub value: u64,
125}
126
127impl GpuSync {
128    /// A sync of a kind that names no object.
129    #[must_use]
130    pub fn of(kind: SyncKind) -> GpuSync {
131        GpuSync { kind, handle: 0, value: 0 }
132    }
133}
134
135/// What the host's reads of a frame's texture end with, which a target
136/// waits for before it draws into the texture again. The target takes the
137/// sync file, or the `GLsync` that `sync` names; dropping it closes the sync
138/// file.
139#[derive(Debug)]
140pub struct ConsumerSync {
141    /// The kind, and the timeline or the `GLsync` with its value. A sync
142    /// file's descriptor is `sync_file`.
143    pub sync: GpuSync,
144    #[cfg(unix)]
145    pub sync_file: Option<std::os::fd::OwnedFd>,
146}
147
148impl ConsumerSync {
149    /// A sync of a kind that names no object, or names one that the host
150    /// keeps.
151    #[must_use]
152    pub fn of(sync: GpuSync) -> ConsumerSync {
153        ConsumerSync {
154            sync,
155            #[cfg(unix)]
156            sync_file: None,
157        }
158    }
159}
160
161/// Where a frame's image went when its frame ended.
162#[derive(Clone, Copy, Debug, PartialEq, Eq)]
163#[non_exhaustive]
164pub enum Handoff {
165    /// A window or surface shows it.
166    Presented,
167    /// The target keeps it, for its owner to read back.
168    ReadBack,
169    /// The host's texture at `slot` of its ring holds it, once the host
170    /// waits for `sync`. The object `sync` names stays the target's until
171    /// the host gives the slot back.
172    GivenBack { slot: usize, sync: GpuSync },
173    /// The target's own image at `slot` of its exported ring holds it, for
174    /// the host to acquire and read once it waits for `sync`. The image and
175    /// the object `sync` names stay the target's until the host gives the
176    /// slot back.
177    Queued { slot: usize, sync: GpuSync },
178    /// Nothing took it: the target had no image to hand off.
179    Dropped,
180}
181
182/// A target that a renderer draws frames into: a window's swapchain, or an
183/// image of the library's that is read back. It belongs to the thread that
184/// built it.
185pub trait FrameTarget {
186    /// Begins a frame, and returns the textures of the backend's `Gpu` that
187    /// the renderer draws it into, or `None` when the target has nothing to
188    /// draw into now: it has no surface, or the surface is hidden or out of
189    /// date. The caller then skips the frame. Until
190    /// [`end_frame`](Self::end_frame), each call returns the same image.
191    fn begin_frame(&self) -> Option<FrameImage>;
192
193    /// Hands the frame's image off, once the renderer has submitted the
194    /// frame's passes to the backend's `Gpu`.
195    fn end_frame(&self) -> Handoff;
196}