Skip to main content

mln_gfx/
frame.rs

1//! How the renderer draws a frame into its target: it begins the frame, draws
2//! into the image the target gives it, and ends the frame, which hands the
3//! image off.
4
5use std::any::Any;
6use std::rc::Rc;
7
8use mln_util::size::Size;
9
10use crate::command_encoder::CommandEncoder;
11
12/// What a render pass draws into: a frame's image, or a texture that the
13/// renderer draws into ahead of the frame. Each backend downcasts it to its
14/// own type.
15pub trait DrawTarget: Any {
16    /// The size in physical pixels.
17    fn size(&self) -> Size;
18    fn as_any(&self) -> &dyn Any;
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 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 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.
186pub trait FrameTarget {
187    /// Begins a frame, and returns what the renderer draws it into, or `None`
188    /// when the target has nothing to draw into now: it has no surface, or
189    /// the surface is hidden or out of date. The caller then skips the frame.
190    /// Until [`end_frame`](Self::end_frame), each call returns the same
191    /// image.
192    fn begin_frame(&self) -> Option<Rc<dyn DrawTarget>>;
193
194    /// Submits what the renderer recorded into `encoder` for the frame, and
195    /// hands the frame's image off.
196    fn end_frame(&self, encoder: &dyn CommandEncoder) -> Handoff;
197}