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